Endpoint.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. namespace ZeroTier {
  15. bool Endpoint::operator==(const Endpoint &ep) const
  16. {
  17. if (_t == ep._t) {
  18. switch(_t) {
  19. default: return true;
  20. case INETADDR_V4:
  21. case INETADDR_V6: return (inetAddr() == ep.inetAddr());
  22. case DNSNAME: return ((_v.dns.port == ep._v.dns.port)&&(strcmp(_v.dns.name,ep._v.dns.name) == 0));
  23. case ZEROTIER: return ((_v.zt.a == ep._v.zt.a)&&(memcmp(_v.zt.idh,ep._v.zt.idh,sizeof(_v.zt.idh)) == 0));
  24. case URL: return (strcmp(_v.url,ep._v.url) == 0);
  25. case ETHERNET: return (_v.eth == ep._v.eth);
  26. }
  27. }
  28. return false;
  29. }
  30. bool Endpoint::operator<(const Endpoint &ep) const
  31. {
  32. if ((int)_t < (int)ep._t) {
  33. return true;
  34. } else if (_t == ep._t) {
  35. int ncmp;
  36. switch(_t) {
  37. case INETADDR_V4:
  38. case INETADDR_V6:
  39. return (inetAddr() < ep.inetAddr());
  40. case DNSNAME:
  41. ncmp = strcmp(_v.dns.name,ep._v.dns.name);
  42. return ((ncmp < 0) ? true : (ncmp == 0)&&(_v.dns.port < ep._v.dns.port));
  43. case ZEROTIER: return (_v.zt.a < ep._v.zt.a) ? true : ((_v.zt.a == ep._v.zt.a)&&(memcmp(_v.zt.idh,ep._v.zt.idh,sizeof(_v.zt.idh)) < 0));
  44. case URL: return (strcmp(_v.url,ep._v.url) < 0);
  45. case ETHERNET: return (_v.eth < ep._v.eth);
  46. default: return false;
  47. }
  48. }
  49. return false;
  50. }
  51. int Endpoint::marshal(uint8_t data[ZT_ENDPOINT_MARSHAL_SIZE_MAX]) const
  52. {
  53. int p;
  54. data[0] = (uint8_t)_t;
  55. Utils::storeBigEndian(data + 1,(int16_t)_l[0]);
  56. Utils::storeBigEndian(data + 3,(int16_t)_l[1]);
  57. Utils::storeBigEndian(data + 5,(int16_t)_l[2]);
  58. switch(_t) {
  59. case INETADDR_V4:
  60. case INETADDR_V6:
  61. return 7 + reinterpret_cast<const InetAddress *>(&_v.sa)->marshal(data+1);
  62. case DNSNAME:
  63. p = 7;
  64. for (;;) {
  65. if ((data[p] = (uint8_t)_v.dns.name[p-1]) == 0)
  66. break;
  67. ++p;
  68. if (p == (ZT_ENDPOINT_MAX_NAME_SIZE+1))
  69. return -1;
  70. }
  71. data[p++] = (uint8_t)(_v.dns.port >> 8U);
  72. data[p++] = (uint8_t)_v.dns.port;
  73. return p;
  74. case ZEROTIER:
  75. data[7] = (uint8_t)(_v.zt.a >> 32U);
  76. data[8] = (uint8_t)(_v.zt.a >> 24U);
  77. data[9] = (uint8_t)(_v.zt.a >> 16U);
  78. data[10] = (uint8_t)(_v.zt.a >> 8U);
  79. data[11] = (uint8_t)_v.zt.a;
  80. memcpy(data + 12,_v.zt.idh,ZT_IDENTITY_HASH_SIZE);
  81. return ZT_IDENTITY_HASH_SIZE + 12;
  82. case URL:
  83. p = 7;
  84. for (;;) {
  85. if ((data[p] = (uint8_t)_v.url[p-1]) == 0)
  86. break;
  87. ++p;
  88. if (p == (ZT_ENDPOINT_MAX_NAME_SIZE+1))
  89. return -1;
  90. }
  91. return p;
  92. case ETHERNET:
  93. data[7] = (uint8_t)(_v.eth >> 40U);
  94. data[8] = (uint8_t)(_v.eth >> 32U);
  95. data[9] = (uint8_t)(_v.eth >> 24U);
  96. data[10] = (uint8_t)(_v.eth >> 16U);
  97. data[11] = (uint8_t)(_v.eth >> 8U);
  98. data[12] = (uint8_t)_v.eth;
  99. return 13;
  100. default:
  101. data[0] = (uint8_t)NIL;
  102. return 7;
  103. }
  104. }
  105. int Endpoint::unmarshal(const uint8_t *restrict data,const int len)
  106. {
  107. if (len < 7)
  108. return -1;
  109. int p;
  110. _t = (Type)data[0];
  111. _l[0] = Utils::loadBigEndian<int16_t>(data + 1);
  112. _l[1] = Utils::loadBigEndian<int16_t>(data + 3);
  113. _l[2] = Utils::loadBigEndian<int16_t>(data + 5);
  114. switch(_t) {
  115. case NIL:
  116. return 7;
  117. case INETADDR_V4:
  118. case INETADDR_V6:
  119. return 7 + reinterpret_cast<InetAddress *>(&_v.sa)->unmarshal(data+7,len-7);
  120. case DNSNAME:
  121. if (len < 10)
  122. return -1;
  123. p = 7;
  124. for (;;) {
  125. if ((_v.dns.name[p-1] = (char)data[p]) == 0) {
  126. ++p;
  127. break;
  128. }
  129. ++p;
  130. if ((p >= (ZT_ENDPOINT_MARSHAL_SIZE_MAX-2))||(p >= (len-2)))
  131. return -1;
  132. }
  133. _v.dns.port = (uint16_t)(((unsigned int)data[p++]) << 8U);
  134. _v.dns.port |= (uint16_t)data[p++];
  135. return p;
  136. case ZEROTIER:
  137. if (len < 60)
  138. return -1;
  139. _v.zt.a = ((uint64_t)data[7]) << 32U;
  140. _v.zt.a |= ((uint64_t)data[8]) << 24U;
  141. _v.zt.a |= ((uint64_t)data[9]) << 16U;
  142. _v.zt.a |= ((uint64_t)data[10]) << 8U;
  143. _v.zt.a |= (uint64_t)data[11];
  144. memcpy(_v.zt.idh,data + 12,48);
  145. return 60;
  146. case URL:
  147. if (len < 8)
  148. return -1;
  149. p = 7;
  150. for (;;) {
  151. if ((_v.url[p-1] = (char)data[p]) == 0) {
  152. ++p;
  153. break;
  154. }
  155. ++p;
  156. if ((p >= (ZT_ENDPOINT_MAX_NAME_SIZE+1))||(p >= len))
  157. return -1;
  158. }
  159. return p;
  160. case ETHERNET:
  161. if (len < 13)
  162. return -1;
  163. _v.eth = ((uint64_t)data[7]) << 40U;
  164. _v.eth |= ((uint64_t)data[8]) << 32U;
  165. _v.eth |= ((uint64_t)data[9]) << 24U;
  166. _v.eth |= ((uint64_t)data[10]) << 16U;
  167. _v.eth |= ((uint64_t)data[11]) << 8U;
  168. _v.eth |= (uint64_t)data[12];
  169. return 13;
  170. default:
  171. // Unrecognized endpoint types not yet specified must start with a byte
  172. // length size so that older versions of ZeroTier can skip them.
  173. if (len < 8)
  174. return -1;
  175. return 8 + (int)data[7];
  176. }
  177. }
  178. } // namespace ZeroTier