Endpoint.cpp 5.7 KB

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