Endpoint.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * Copyright (c)2013-2020 ZeroTier, Inc.
  3. *
  4. * Use of this software is governed by the Business Source License included
  5. * in the LICENSE.TXT file in the project's root directory.
  6. *
  7. * Change Date: 2024-01-01
  8. *
  9. * On the date above, in accordance with the Business Source License, use
  10. * of this software will be governed by version 2.0 of the Apache License.
  11. */
  12. /****/
  13. #include "Endpoint.hpp"
  14. #include "Utils.hpp"
  15. namespace ZeroTier {
  16. Endpoint::Endpoint(const InetAddress &sa,const Protocol proto) noexcept // NOLINT(cppcoreguidelines-pro-type-member-init,hicpp-member-init)
  17. {
  18. switch (sa.family()) {
  19. case AF_INET:
  20. _t = TYPE_INETADDR_V4;
  21. break;
  22. case AF_INET6:
  23. _t = TYPE_INETADDR_V6;
  24. break;
  25. default:
  26. _t = TYPE_NIL;
  27. return;
  28. }
  29. _proto = proto;
  30. asInetAddress(_v.sa) = sa;
  31. }
  32. bool Endpoint::operator==(const Endpoint &ep) const noexcept
  33. {
  34. if ((_t == ep._t)&&(_proto == ep._proto)) {
  35. switch(_t) {
  36. default:
  37. return true;
  38. case TYPE_ZEROTIER:
  39. return ((_v.zt.address == ep._v.zt.address) && (memcmp(_v.zt.hash,ep._v.zt.hash,sizeof(_v.zt.hash)) == 0));
  40. case TYPE_ETHERNET:
  41. return memcmp(_v.eth,ep._v.eth,6) == 0;
  42. case TYPE_INETADDR_V4:
  43. case TYPE_INETADDR_V6:
  44. return asInetAddress(_v.sa) == asInetAddress(ep._v.sa);
  45. }
  46. }
  47. return false;
  48. }
  49. bool Endpoint::operator<(const Endpoint &ep) const noexcept
  50. {
  51. if ((int)_t < (int)ep._t) {
  52. return true;
  53. } else if (_t == ep._t) {
  54. if ((int)_proto < (int)ep._proto) {
  55. return true;
  56. } else {
  57. switch (_t) {
  58. case TYPE_ZEROTIER:
  59. return (_v.zt.address < ep._v.zt.address) ? true : ((_v.zt.address == ep._v.zt.address) && (memcmp(_v.zt.hash,ep._v.zt.hash,sizeof(_v.zt.hash)) < 0));
  60. case TYPE_ETHERNET:
  61. return memcmp(_v.eth,ep._v.eth,6) < 0;
  62. case TYPE_INETADDR_V4:
  63. case TYPE_INETADDR_V6:
  64. return asInetAddress(_v.sa) < asInetAddress(ep._v.sa);
  65. default:
  66. return false;
  67. }
  68. }
  69. }
  70. return false;
  71. }
  72. int Endpoint::marshal(uint8_t data[ZT_ENDPOINT_MARSHAL_SIZE_MAX]) const noexcept
  73. {
  74. data[0] = (uint8_t)_t;
  75. Utils::storeBigEndian(data + 1,(uint16_t)_proto);
  76. Utils::storeBigEndian(data + 3,(uint16_t)_l[0]);
  77. Utils::storeBigEndian(data + 5,(uint16_t)_l[1]);
  78. Utils::storeBigEndian(data + 7,(uint16_t)_l[2]);
  79. int p;
  80. switch(_t) {
  81. case TYPE_ZEROTIER:
  82. data[9] = (uint8_t)(_v.zt.address >> 32U);
  83. data[10] = (uint8_t)(_v.zt.address >> 24U);
  84. data[11] = (uint8_t)(_v.zt.address >> 16U);
  85. data[12] = (uint8_t)(_v.zt.address >> 8U);
  86. data[13] = (uint8_t)_v.zt.address;
  87. Utils::copy<ZT_FINGERPRINT_HASH_SIZE>(data + 14,_v.zt.hash);
  88. return ZT_FINGERPRINT_HASH_SIZE + 14;
  89. case TYPE_ETHERNET:
  90. Utils::copy<6>(data + 9,_v.eth);
  91. return 15;
  92. case TYPE_INETADDR_V4:
  93. case TYPE_INETADDR_V6:
  94. p = 9 + asInetAddress(_v.sa).marshal(data + 7);
  95. if (p <= 9)
  96. return -1;
  97. return p;
  98. default:
  99. data[0] = (uint8_t)TYPE_NIL;
  100. return 1;
  101. }
  102. }
  103. int Endpoint::unmarshal(const uint8_t *restrict data,const int len) noexcept
  104. {
  105. if (len < 1)
  106. return -1;
  107. _t = (Type)data[0];
  108. if (_t == TYPE_NIL)
  109. return 1;
  110. _proto = (Protocol)Utils::loadBigEndian<uint16_t>(data + 1);
  111. _l[0] = (int)Utils::loadBigEndian<uint16_t>(data + 3);
  112. _l[1] = (int)Utils::loadBigEndian<uint16_t>(data + 5);
  113. _l[2] = (int)Utils::loadBigEndian<uint16_t>(data + 7);
  114. int p;
  115. switch(_t) {
  116. case TYPE_ZEROTIER:
  117. if (len < (14 + ZT_FINGERPRINT_HASH_SIZE))
  118. return -1;
  119. _v.zt.address = ((uint64_t)data[9]) << 32U;
  120. _v.zt.address |= ((uint64_t)data[10]) << 24U;
  121. _v.zt.address |= ((uint64_t)data[11]) << 16U;
  122. _v.zt.address |= ((uint64_t)data[12]) << 8U;
  123. _v.zt.address |= (uint64_t)data[13];
  124. Utils::copy<ZT_FINGERPRINT_HASH_SIZE>(_v.zt.hash,data + 14);
  125. return ZT_FINGERPRINT_HASH_SIZE + 14;
  126. case TYPE_ETHERNET:
  127. if (len < 15)
  128. return -1;
  129. Utils::copy<6>(_v.eth,data + 9);
  130. return 15;
  131. case TYPE_INETADDR_V4:
  132. case TYPE_INETADDR_V6:
  133. if (len <= 9)
  134. return -1;
  135. p = 9 + asInetAddress(_v.sa).unmarshal(data + 9,len - 9);
  136. if ((p <= 9)||(p >= len))
  137. return -1;
  138. return p;
  139. default:
  140. // Unrecognized endpoint types not yet specified must start with a 16-bit
  141. // length so that older versions of ZeroTier can skip them.
  142. if (len < 11)
  143. return -1;
  144. p = 11 + (int)Utils::loadBigEndian<uint16_t>(data + 9);
  145. return (p > len) ? -1 : p;
  146. }
  147. }
  148. } // namespace ZeroTier