1
0

Address.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #ifndef Address_H
  16. #define Address_H
  17. #include "benc/String.h"
  18. #include "memory/Allocator.h"
  19. #include "util/Linker.h"
  20. Linker_require("dht/Address.c")
  21. #include <stdint.h>
  22. #include <stdbool.h>
  23. #define Address_KEY_SIZE 32
  24. #define Address_NETWORK_ADDR_SIZE 8
  25. #define Address_SEARCH_TARGET_SIZE 16
  26. #define Address_SERIALIZED_SIZE 40
  27. struct Address
  28. {
  29. /** The protocol version of the node. */
  30. uint32_t protocolVersion;
  31. /** unused */
  32. uint32_t padding;
  33. union {
  34. struct {
  35. // tricksy: this is effectively a 64 bit rotate of the following bytes array
  36. uint32_t three_be;
  37. uint32_t four_be;
  38. uint32_t one_be;
  39. uint32_t two_be;
  40. } ints;
  41. struct {
  42. uint64_t two_be;
  43. uint64_t one_be;
  44. } longs;
  45. uint8_t bytes[Address_SEARCH_TARGET_SIZE];
  46. } ip6;
  47. uint8_t key[Address_KEY_SIZE];
  48. uint64_t path;
  49. };
  50. #define Address_SIZE (8 + Address_SEARCH_TARGET_SIZE + Address_KEY_SIZE + Address_NETWORK_ADDR_SIZE)
  51. Assert_compileTime(sizeof(struct Address) == Address_SIZE);
  52. struct Address_List
  53. {
  54. int length;
  55. struct Address* elems;
  56. };
  57. struct Address_List* Address_List_new(uint32_t length, struct Allocator* alloc);
  58. uint32_t Address_getPrefix(struct Address* addr);
  59. uint32_t Address_prefixForSearchTarget(const uint8_t searchTarget[16]);
  60. void Address_serialize(uint8_t output[Address_SERIALIZED_SIZE], const struct Address* addr);
  61. void Address_parse(struct Address* addr, const uint8_t input[Address_SERIALIZED_SIZE]);
  62. bool Address_isSame(const struct Address* addr,
  63. const struct Address* addr2);
  64. bool Address_isSameIp(const struct Address* addr,
  65. const struct Address* addr2);
  66. bool Address_equalsSearchTarget(struct Address* addr,
  67. const uint8_t searchTarget[Address_SEARCH_TARGET_SIZE]);
  68. void Address_forKey(struct Address* out, const uint8_t key[Address_KEY_SIZE]);
  69. void Address_printIp(uint8_t output[40], struct Address* addr);
  70. void Address_print(uint8_t output[60], struct Address* addr);
  71. String* Address_toString(struct Address* addr, struct Allocator* alloc);
  72. struct Address* Address_fromString(String* str, struct Allocator* alloc);
  73. int Address_xorcmp(uint32_t target,
  74. uint32_t negativeIfCloser,
  75. uint32_t positiveIfCloser);
  76. /**
  77. * Return which node is closer to the target.
  78. *
  79. * @param target the address to test distance against.
  80. * @param negativeIfCloser one address to check distance.
  81. * @param positiveIfCloser another address to check distance.
  82. * @return -1 if negativeIfCloser is closer to target, 1 if positiveIfCloser is closer
  83. * 0 if they are both the same distance.
  84. */
  85. int Address_closest(struct Address* target,
  86. struct Address* negativeIfCloser,
  87. struct Address* positiveIfCloser);
  88. #endif