Endpoint.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. switch(_t) {
  52. case INETADDR:
  53. data[0] = (uint8_t)INETADDR;
  54. return 1 + reinterpret_cast<const InetAddress *>(&_v.sa)->marshal(data+1);
  55. case DNSNAME:
  56. data[0] = (uint8_t)DNSNAME;
  57. p = 1;
  58. for (;;) {
  59. if ((data[p] = (uint8_t)_v.dns.name[p-1]) == 0)
  60. break;
  61. ++p;
  62. if (p == (ZT_ENDPOINT_MAX_NAME_SIZE+1))
  63. return -1;
  64. }
  65. data[p++] = (uint8_t)(_v.dns.port >> 8U);
  66. data[p++] = (uint8_t)_v.dns.port;
  67. return p;
  68. case ZEROTIER:
  69. data[0] = (uint8_t)ZEROTIER;
  70. data[1] = (uint8_t)(_v.zt.a >> 32U);
  71. data[2] = (uint8_t)(_v.zt.a >> 24U);
  72. data[3] = (uint8_t)(_v.zt.a >> 16U);
  73. data[4] = (uint8_t)(_v.zt.a >> 8U);
  74. data[5] = (uint8_t)_v.zt.a;
  75. memcpy(data + 6,_v.zt.idh,ZT_IDENTITY_HASH_SIZE);
  76. return (ZT_IDENTITY_HASH_SIZE + 6);
  77. case URL:
  78. data[0] = (uint8_t)URL;
  79. p = 1;
  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[0] = (uint8_t)ETHERNET;
  90. data[1] = (uint8_t)(_v.eth >> 40U);
  91. data[2] = (uint8_t)(_v.eth >> 32U);
  92. data[3] = (uint8_t)(_v.eth >> 24U);
  93. data[4] = (uint8_t)(_v.eth >> 16U);
  94. data[5] = (uint8_t)(_v.eth >> 8U);
  95. data[6] = (uint8_t)_v.eth;
  96. return 7;
  97. default:
  98. data[0] = (uint8_t)NIL;
  99. return 1;
  100. }
  101. }
  102. int Endpoint::unmarshal(const uint8_t *restrict data,const int len)
  103. {
  104. if (len <= 0)
  105. return -1;
  106. int p;
  107. switch((Type)data[0]) {
  108. case NIL:
  109. _t = NIL;
  110. return 1;
  111. case INETADDR:
  112. _t = INETADDR;
  113. return reinterpret_cast<InetAddress *>(&_v.sa)->unmarshal(data+1,len-1);
  114. case DNSNAME:
  115. if (len < 4)
  116. return -1;
  117. _t = DNSNAME;
  118. p = 1;
  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_MAX_NAME_SIZE+1))||(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 < (ZT_IDENTITY_HASH_SIZE + 6))
  133. return -1;
  134. _t = ZEROTIER;
  135. _v.zt.a = ((uint64_t)data[1]) << 32U;
  136. _v.zt.a |= ((uint64_t)data[2]) << 24U;
  137. _v.zt.a |= ((uint64_t)data[3]) << 16U;
  138. _v.zt.a |= ((uint64_t)data[4]) << 8U;
  139. _v.zt.a |= (uint64_t)data[5];
  140. memcpy(_v.zt.idh,data + 6,ZT_IDENTITY_HASH_SIZE);
  141. return (ZT_IDENTITY_HASH_SIZE + 6);
  142. case URL:
  143. if (len < 2)
  144. return -1;
  145. _t = URL;
  146. p = 1;
  147. for (;;) {
  148. if ((_v.url[p-1] = (char)data[p]) == 0) {
  149. ++p;
  150. break;
  151. }
  152. ++p;
  153. if ((p >= (ZT_ENDPOINT_MAX_NAME_SIZE+1))||(p >= len))
  154. return -1;
  155. }
  156. return p;
  157. case ETHERNET:
  158. if (len < 7)
  159. return -1;
  160. _t = ZEROTIER;
  161. _v.eth = ((uint64_t)data[1]) << 40U;
  162. _v.eth |= ((uint64_t)data[2]) << 32U;
  163. _v.eth |= ((uint64_t)data[3]) << 24U;
  164. _v.eth |= ((uint64_t)data[4]) << 16U;
  165. _v.eth |= ((uint64_t)data[5]) << 8U;
  166. _v.eth |= (uint64_t)data[6];
  167. return 7;
  168. default:
  169. // Unrecognized endpoint types not yet specified must start with a byte
  170. // length size so that older versions of ZeroTier can skip them.
  171. if (len < 2)
  172. return -1;
  173. _t = UNRECOGNIZED;
  174. return 1 + (int)data[1];
  175. }
  176. return false;
  177. }
  178. } // namespace ZeroTier