1
0

Address.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. // 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. void Address_serialize(uint8_t output[Address_SERIALIZED_SIZE], const struct Address* addr);
  64. void Address_parse(struct Address* addr, const uint8_t input[Address_SERIALIZED_SIZE]);
  65. bool Address_isSame(const struct Address* addr,
  66. const struct Address* addr2);
  67. bool Address_isSameIp(const struct Address* addr,
  68. const struct Address* addr2);
  69. bool Address_equalsSearchTarget(struct Address* addr,
  70. const uint8_t searchTarget[Address_SEARCH_TARGET_SIZE]);
  71. void Address_forKey(struct Address* out, const uint8_t key[Address_KEY_SIZE]);
  72. void Address_printIp(uint8_t output[40], struct Address* addr);
  73. void Address_printShortIp(uint8_t output[40], struct Address* addr);
  74. void Address_print(uint8_t output[60], struct Address* addr);
  75. String* Address_toString(struct Address* addr, struct Allocator* alloc);
  76. struct Address* Address_fromString(String* str, struct Allocator* alloc);
  77. int Address_xorcmp(uint32_t target,
  78. uint32_t negativeIfCloser,
  79. uint32_t positiveIfCloser);
  80. /**
  81. * Return which node is closer to the target.
  82. *
  83. * @param target the address to test distance against.
  84. * @param negativeIfCloser one address to check distance.
  85. * @param positiveIfCloser another address to check distance.
  86. * @return -1 if negativeIfCloser is closer to target, 1 if positiveIfCloser is closer
  87. * 0 if they are both the same distance.
  88. */
  89. int Address_closest(struct Address* target,
  90. struct Address* negativeIfCloser,
  91. struct Address* positiveIfCloser);
  92. #endif