Endpoint.cpp 4.8 KB

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