Address.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 <https://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. // Don't undefine this yet, new code will depend on it so that it can be tracked down and changed.
  28. #define Address_ROT64
  29. struct Address
  30. {
  31. /** The protocol version of the node. */
  32. uint32_t protocolVersion;
  33. /** unused */
  34. uint32_t padding;
  35. union {
  36. struct {
  37. // tricksy: this is effectively a 64 bit rotate of the following bytes array
  38. uint32_t three_be;
  39. uint32_t four_be;
  40. uint32_t one_be;
  41. uint32_t two_be;
  42. } ints;
  43. struct {
  44. uint64_t two_be;
  45. uint64_t one_be;
  46. } longs;
  47. uint8_t bytes[Address_SEARCH_TARGET_SIZE];
  48. } ip6;
  49. uint8_t key[Address_KEY_SIZE];
  50. uint64_t path;
  51. };
  52. #define Address_SIZE (8 + Address_SEARCH_TARGET_SIZE + Address_KEY_SIZE + Address_NETWORK_ADDR_SIZE)
  53. Assert_compileTime(sizeof(struct Address) == Address_SIZE);
  54. struct Address_List
  55. {
  56. int length;
  57. struct Address* elems;
  58. };
  59. struct Address_List* Address_List_new(uint32_t length, struct Allocator* alloc);
  60. uint32_t Address_getPrefix(struct Address* addr);
  61. uint32_t Address_prefixForIp6(uint8_t ip6[16]);
  62. uint32_t Address_prefixForSearchTarget(const uint8_t searchTarget[16]);
  63. /**
  64. * Address_serialize and Address_parse use the following format:
  65. *
  66. * 1 2 3
  67. * 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7
  68. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  69. * 0 | |
  70. * + +
  71. * 4 | |
  72. * + +
  73. * 8 | |
  74. * + +
  75. * 12 | |
  76. * + Public Key +
  77. * 16 | |
  78. * + +
  79. * 20 | |
  80. * + +
  81. * 24 | |
  82. * + +
  83. * 28 | |
  84. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  85. * 32 | |
  86. * + Route Label +
  87. * 36 | |
  88. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  89. */
  90. void Address_serialize(uint8_t output[Address_SERIALIZED_SIZE], const struct Address* addr);
  91. void Address_parse(struct Address* addr, const uint8_t input[Address_SERIALIZED_SIZE]);
  92. bool Address_isSame(const struct Address* addr,
  93. const struct Address* addr2);
  94. bool Address_isSameIp(const struct Address* addr,
  95. const struct Address* addr2);
  96. bool Address_equalsSearchTarget(struct Address* addr,
  97. const uint8_t searchTarget[Address_SEARCH_TARGET_SIZE]);
  98. void Address_forKey(struct Address* out, const uint8_t key[Address_KEY_SIZE]);
  99. void Address_printIp(uint8_t output[40], struct Address* addr);
  100. void Address_printShortIp(uint8_t output[40], struct Address* addr);
  101. void Address_print(uint8_t output[60], struct Address* addr);
  102. String* Address_toString(struct Address* addr, struct Allocator* alloc);
  103. struct Address* Address_fromString(String* str, struct Allocator* alloc);
  104. struct Address* Address_clone(struct Address* orig, struct Allocator* alloc);
  105. int Address_xorcmp(uint32_t target,
  106. uint32_t negativeIfCloser,
  107. uint32_t positiveIfCloser);
  108. /**
  109. * Return which node is closer to the target.
  110. *
  111. * @param target the address to test distance against.
  112. * @param negativeIfCloser one address to check distance.
  113. * @param positiveIfCloser another address to check distance.
  114. * @return -1 if negativeIfCloser is closer to target, 1 if positiveIfCloser is closer
  115. * 0 if they are both the same distance.
  116. */
  117. int Address_closest(struct Address* target,
  118. struct Address* negativeIfCloser,
  119. struct Address* positiveIfCloser);
  120. #endif