1
0

Address.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 "crypto/AddressCalc.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/platform/libc/strlen.h"
  24. #include <stdint.h>
  25. #include <stdbool.h>
  26. #define Address_KEY_SIZE 32
  27. #define Address_NETWORK_ADDR_SIZE 8
  28. #define Address_SEARCH_TARGET_SIZE 16
  29. #define Address_SERIALIZED_SIZE 40
  30. struct Address
  31. {
  32. union {
  33. struct {
  34. // tricksy: this is effectively a 64 bit rotate of the following bytes array
  35. uint32_t three;
  36. uint32_t four;
  37. uint32_t one;
  38. uint32_t two;
  39. } ints;
  40. uint8_t bytes[Address_SEARCH_TARGET_SIZE];
  41. } ip6;
  42. uint8_t key[Address_KEY_SIZE];
  43. uint64_t path;
  44. };
  45. #define Address_SIZE (Address_SEARCH_TARGET_SIZE + Address_KEY_SIZE + Address_NETWORK_ADDR_SIZE)
  46. Assert_compileTime(sizeof(struct Address) == Address_SIZE);
  47. static inline uint32_t Address_getPrefix(struct Address* addr)
  48. {
  49. if (addr->ip6.ints.one == 0
  50. && addr->ip6.ints.two == 0
  51. && addr->ip6.ints.three == 0
  52. && addr->ip6.ints.four == 0)
  53. {
  54. AddressCalc_addressForPublicKey(addr->ip6.bytes, addr->key);
  55. }
  56. return Endian_bigEndianToHost32(addr->ip6.ints.one);
  57. }
  58. static inline uint32_t Address_prefixForSearchTarget(const uint8_t searchTarget[16])
  59. {
  60. uint32_t prefix_be;
  61. Bits_memcpyConst(&prefix_be, &searchTarget[8], 4);
  62. return Endian_bigEndianToHost32(prefix_be);
  63. }
  64. static inline void Address_serialize(uint8_t output[Address_SERIALIZED_SIZE],
  65. const struct Address* addr)
  66. {
  67. Bits_memcpyConst(output, addr->key, Address_SERIALIZED_SIZE);
  68. if (!Endian_isBigEndian()) {
  69. uint64_t path_be = Endian_hostToBigEndian64(addr->path);
  70. Bits_memcpyConst(output + Address_KEY_SIZE, &path_be, Address_NETWORK_ADDR_SIZE);
  71. }
  72. }
  73. static inline void Address_parse(struct Address* addr,
  74. const uint8_t input[Address_SERIALIZED_SIZE])
  75. {
  76. Bits_memset(addr->ip6.bytes, 0, 16);
  77. Bits_memcpyConst(addr->key, input, Address_SERIALIZED_SIZE);
  78. addr->path = Endian_bigEndianToHost64(addr->path);
  79. }
  80. static inline bool Address_isSame(const struct Address* addr,
  81. const struct Address* addr2)
  82. {
  83. return Bits_memcmp(addr->key, addr2->key, Address_SERIALIZED_SIZE) == 0;
  84. }
  85. static inline bool Address_isSameIp(const struct Address* addr,
  86. const struct Address* addr2)
  87. {
  88. return Bits_memcmp(addr->ip6.bytes, addr2->ip6.bytes, 16) == 0;
  89. }
  90. static inline bool Address_equalsSearchTarget(
  91. struct Address* addr,
  92. const uint8_t searchTarget[Address_SEARCH_TARGET_SIZE])
  93. {
  94. Address_getPrefix(addr);
  95. return Bits_memcmp(addr->ip6.bytes, searchTarget, Address_SEARCH_TARGET_SIZE);
  96. }
  97. static inline void Address_forKey(struct Address* out, const uint8_t key[Address_KEY_SIZE])
  98. {
  99. Bits_memcpyConst(out->key, key, Address_KEY_SIZE);
  100. AddressCalc_addressForPublicKey(out->ip6.bytes, key);
  101. }
  102. static inline void Address_printIp(uint8_t output[40], struct Address* addr)
  103. {
  104. Address_getPrefix(addr);
  105. AddrTools_printIp(output, addr->ip6.bytes);
  106. }
  107. static inline void Address_print(uint8_t output[60], struct Address* addr)
  108. {
  109. Address_printIp(output, addr);
  110. output[39] = '@';
  111. AddrTools_printPath(output + 40, addr->path);
  112. }
  113. #endif