Address.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. #include "dht/Address.h"
  16. #include "crypto/AddressCalc.h"
  17. #include "crypto/Key.h"
  18. #include "util/AddrTools.h"
  19. #include "util/Assert.h"
  20. #include "util/Bits.h"
  21. #include "util/Endian.h"
  22. #include "util/Hex.h"
  23. #include "util/Defined.h"
  24. struct Address_List* Address_List_new(uint32_t length, struct Allocator* alloc)
  25. {
  26. struct Address_List* out = Allocator_malloc(alloc, sizeof(struct Address_List));
  27. out->length = length;
  28. out->elems = Allocator_calloc(alloc, Address_SIZE, length);
  29. return out;
  30. }
  31. uint32_t Address_prefixForIp6(uint8_t ip6[16])
  32. {
  33. uint32_t word = ((uint32_t*)ip6)[ Defined(Address_ROT64) ? 2 : 0 ];
  34. return Endian_bigEndianToHost32(word);
  35. }
  36. uint32_t Address_getPrefix(struct Address* addr)
  37. {
  38. if (addr->ip6.ints.one_be == 0
  39. && addr->ip6.ints.two_be == 0
  40. && addr->ip6.ints.three_be == 0
  41. && addr->ip6.ints.four_be == 0)
  42. {
  43. AddressCalc_addressForPublicKey(addr->ip6.bytes, addr->key);
  44. }
  45. return Endian_bigEndianToHost32(addr->ip6.ints.one_be);
  46. }
  47. uint32_t Address_prefixForSearchTarget(const uint8_t searchTarget[16])
  48. {
  49. uint32_t prefix_be;
  50. Bits_memcpyConst(&prefix_be, &searchTarget[8], 4);
  51. return Endian_bigEndianToHost32(prefix_be);
  52. }
  53. void Address_serialize(uint8_t output[Address_SERIALIZED_SIZE], const struct Address* addr)
  54. {
  55. Bits_memcpyConst(output, addr->key, Address_SERIALIZED_SIZE);
  56. if (!Endian_isBigEndian()) {
  57. uint64_t path_be = Endian_hostToBigEndian64(addr->path);
  58. Bits_memcpyConst(output + Address_KEY_SIZE, &path_be, Address_NETWORK_ADDR_SIZE);
  59. }
  60. }
  61. void Address_parse(struct Address* addr, const uint8_t input[Address_SERIALIZED_SIZE])
  62. {
  63. Bits_memset(addr->ip6.bytes, 0, 16);
  64. Bits_memcpyConst(addr->key, input, Address_SERIALIZED_SIZE);
  65. addr->path = Endian_bigEndianToHost64(addr->path);
  66. }
  67. bool Address_isSame(const struct Address* addr,
  68. const struct Address* addr2)
  69. {
  70. return Bits_memcmp(addr->key, addr2->key, Address_SERIALIZED_SIZE) == 0;
  71. }
  72. bool Address_isSameIp(const struct Address* addr,
  73. const struct Address* addr2)
  74. {
  75. return Bits_memcmp(addr->ip6.bytes, addr2->ip6.bytes, 16) == 0;
  76. }
  77. bool Address_equalsSearchTarget(struct Address* addr,
  78. const uint8_t searchTarget[Address_SEARCH_TARGET_SIZE])
  79. {
  80. Address_getPrefix(addr);
  81. return Bits_memcmp(addr->ip6.bytes, searchTarget, Address_SEARCH_TARGET_SIZE);
  82. }
  83. void Address_forKey(struct Address* out, const uint8_t key[Address_KEY_SIZE])
  84. {
  85. Bits_memcpyConst(out->key, key, Address_KEY_SIZE);
  86. AddressCalc_addressForPublicKey(out->ip6.bytes, key);
  87. }
  88. void Address_printIp(uint8_t output[40], struct Address* addr)
  89. {
  90. Address_getPrefix(addr);
  91. AddrTools_printIp(output, addr->ip6.bytes);
  92. }
  93. void Address_printShortIp(uint8_t output[40], struct Address* addr)
  94. {
  95. Address_getPrefix(addr);
  96. AddrTools_printShortIp(output, addr->ip6.bytes);
  97. }
  98. void Address_print(uint8_t output[60], struct Address* addr)
  99. {
  100. Address_printIp(output, addr);
  101. output[39] = '@';
  102. AddrTools_printPath(output + 40, addr->path);
  103. }
  104. int Address_xorcmp(uint32_t target,
  105. uint32_t negativeIfCloser,
  106. uint32_t positiveIfCloser)
  107. {
  108. if (negativeIfCloser == positiveIfCloser) {
  109. return 0;
  110. }
  111. uint32_t ref = Endian_bigEndianToHost32(target);
  112. return ((Endian_bigEndianToHost32(negativeIfCloser) ^ ref)
  113. < (Endian_bigEndianToHost32(positiveIfCloser) ^ ref)) ? -1 : 1;
  114. }
  115. /**
  116. * Return which node is closer to the target.
  117. *
  118. * @param target the address to test distance against.
  119. * @param negativeIfCloser one address to check distance.
  120. * @param positiveIfCloser another address to check distance.
  121. * @return -1 if negativeIfCloser is closer to target, 1 if positiveIfCloser is closer
  122. * 0 if they are both the same distance.
  123. */
  124. int Address_closest(struct Address* target,
  125. struct Address* negativeIfCloser,
  126. struct Address* positiveIfCloser)
  127. {
  128. Address_getPrefix(target);
  129. Address_getPrefix(negativeIfCloser);
  130. Address_getPrefix(positiveIfCloser);
  131. int ret = 0;
  132. #define Address_COMPARE(part) \
  133. if ((ret = Address_xorcmp(target->ip6.ints.part, \
  134. negativeIfCloser->ip6.ints.part, \
  135. positiveIfCloser->ip6.ints.part))) \
  136. { \
  137. return ret; \
  138. }
  139. Address_COMPARE(one_be)
  140. Address_COMPARE(two_be)
  141. Address_COMPARE(three_be)
  142. Address_COMPARE(four_be)
  143. return 0;
  144. #undef Address_COMPARE
  145. }
  146. String* Address_toString(struct Address* addr, struct Allocator* alloc)
  147. {
  148. struct Allocator* temp = Allocator_child(alloc);
  149. String* key = Key_stringify(addr->key, temp);
  150. String* path = String_newBinary(NULL, 19, temp);
  151. AddrTools_printPath(path->bytes, addr->path);
  152. String* out = String_printf(alloc, "v%u.%s.%s", addr->protocolVersion, path->bytes, key->bytes);
  153. Allocator_free(temp);
  154. return out;
  155. }
  156. struct Address* Address_fromString(String* str, struct Allocator* alloc)
  157. {
  158. // v6.0000.0000.0000.0001.yw4hn81kh3f9n39ff3qhnhdl8ngd662utbpgtjp5q4b0yxqv4by0.k
  159. if (str->len < 77) { return NULL; }
  160. if (str->bytes[0] != 'v') { return NULL; }
  161. struct Address addr = { .protocolVersion = 0 };
  162. int i = 1;
  163. for (; str->bytes[i] && str->bytes[i] != '.'; i++) {
  164. Assert_true(i < 77);
  165. if (str->bytes[i] > '9' || str->bytes[i] < '0') { return NULL; }
  166. addr.protocolVersion *= 10;
  167. addr.protocolVersion += (str->bytes[i] - '0');
  168. }
  169. if (str->bytes[i] != '.') { return NULL; }
  170. i++;
  171. // 0000.0000.0000.0001.yw4hn81kh3f9n39ff3qhnhdl8ngd662utbpgtjp5q4b0yxqv4by0.k
  172. if (str->len - i != 74) { return NULL; }
  173. if (str->bytes[i+19] != '.') { return NULL; }
  174. str->bytes[i+19] = '\0';
  175. int ret = AddrTools_parsePath(&addr.path, &str->bytes[i]);
  176. str->bytes[i+19] = '.';
  177. if (ret) { return NULL; }
  178. String keyPart = { .len = str->len - i - 20, .bytes = &str->bytes[i + 20] };
  179. Assert_true(keyPart.len == 54 && keyPart.bytes[53] == 'k');
  180. if (Key_parse(&keyPart, addr.key, addr.ip6.bytes)) { return NULL; }
  181. return Allocator_clone(alloc, &addr);
  182. }