Address.c 6.5 KB

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