2
0

Endpoint.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. void Endpoint::toString(char s[ZT_ENDPOINT_STRING_SIZE_MAX]) const noexcept
  17. {
  18. static const char *const s_endpointTypeChars = ZT_ENDPOINT_TYPE_CHAR_INDEX;
  19. static_assert(ZT_ENDPOINT_STRING_SIZE_MAX > (ZT_INETADDRESS_STRING_SIZE_MAX + 4), "overflow");
  20. static_assert(ZT_ENDPOINT_STRING_SIZE_MAX > (ZT_FINGERPRINT_STRING_SIZE_MAX + 4), "overflow");
  21. switch (this->type) {
  22. default:
  23. s[0] = s_endpointTypeChars[ZT_ENDPOINT_TYPE_NIL];
  24. s[1] = 0;
  25. break;
  26. case ZT_ENDPOINT_TYPE_ZEROTIER:
  27. s[0] = s_endpointTypeChars[ZT_ENDPOINT_TYPE_ZEROTIER];
  28. s[1] = '/';
  29. zt().toString(s + 2);
  30. break;
  31. case ZT_ENDPOINT_TYPE_ETHERNET:
  32. case ZT_ENDPOINT_TYPE_WIFI_DIRECT:
  33. case ZT_ENDPOINT_TYPE_BLUETOOTH:
  34. s[0] = s_endpointTypeChars[this->type];
  35. s[1] = '/';
  36. eth().toString(s + 2);
  37. break;
  38. case ZT_ENDPOINT_TYPE_IP:
  39. case ZT_ENDPOINT_TYPE_IP_UDP:
  40. case ZT_ENDPOINT_TYPE_IP_TCP:
  41. case ZT_ENDPOINT_TYPE_IP_HTTP2:
  42. s[0] = s_endpointTypeChars[this->type];
  43. s[1] = '/';
  44. ip().toString(s + 2);
  45. break;
  46. }
  47. }
  48. bool Endpoint::fromString(const char *s) noexcept
  49. {
  50. memoryZero(this);
  51. if ((!s) || (!*s))
  52. return true;
  53. const char *start = strchr(s, '/');
  54. if (start) {
  55. char tmp[16];
  56. for (unsigned int i = 0;i < 15;++i) {
  57. if ((tmp[i] = s[i]) == 0)
  58. break;
  59. }
  60. tmp[15] = 0;
  61. this->type = (ZT_EndpointType)Utils::strToUInt(tmp);
  62. ++start;
  63. Fingerprint tmpfp;
  64. MAC tmpmac;
  65. switch (this->type) {
  66. case ZT_ENDPOINT_TYPE_NIL:
  67. break;
  68. case ZT_ENDPOINT_TYPE_ZEROTIER:
  69. if (!tmpfp.fromString(start))
  70. return false;
  71. this->value.fp = tmpfp;
  72. break;
  73. case ZT_ENDPOINT_TYPE_ETHERNET:
  74. case ZT_ENDPOINT_TYPE_WIFI_DIRECT:
  75. case ZT_ENDPOINT_TYPE_BLUETOOTH:
  76. tmpmac.fromString(start);
  77. this->value.mac = tmpmac.toInt();
  78. break;
  79. case ZT_ENDPOINT_TYPE_IP:
  80. case ZT_ENDPOINT_TYPE_IP_UDP:
  81. case ZT_ENDPOINT_TYPE_IP_TCP:
  82. case ZT_ENDPOINT_TYPE_IP_HTTP2:
  83. if (!asInetAddress(this->value.ss).fromString(start))
  84. return false;
  85. default:
  86. this->type = ZT_ENDPOINT_TYPE_NIL;
  87. return false;
  88. }
  89. } else {
  90. if (Utils::strToUInt(s) != (unsigned int)ZT_ENDPOINT_TYPE_NIL)
  91. return false;
  92. }
  93. return true;
  94. }
  95. int Endpoint::marshal(uint8_t data[ZT_ENDPOINT_MARSHAL_SIZE_MAX]) const noexcept
  96. {
  97. switch (this->type) {
  98. //case ZT_ENDPOINT_TYPE_NIL:
  99. default:
  100. // NIL endpoints get serialized like NIL InetAddress instances.
  101. data[0] = ZT_ENDPOINT_TYPE_NIL;
  102. return 1;
  103. case ZT_ENDPOINT_TYPE_ZEROTIER:
  104. data[0] = 16 + ZT_ENDPOINT_TYPE_ZEROTIER;
  105. Address(this->value.fp.address).copyTo(data + 1);
  106. Utils::copy<ZT_FINGERPRINT_HASH_SIZE>(data + 1 + ZT_ADDRESS_LENGTH, this->value.fp.hash);
  107. return 1 + ZT_ADDRESS_LENGTH + ZT_FINGERPRINT_HASH_SIZE;
  108. case ZT_ENDPOINT_TYPE_ETHERNET:
  109. case ZT_ENDPOINT_TYPE_WIFI_DIRECT:
  110. case ZT_ENDPOINT_TYPE_BLUETOOTH:
  111. data[0] = 16 + (uint8_t)this->type;
  112. MAC(this->value.mac).copyTo(data + 1);
  113. return 7;
  114. case ZT_ENDPOINT_TYPE_IP_UDP:
  115. // Default UDP mode gets serialized to look exactly like an InetAddress.
  116. return asInetAddress(this->value.ss).marshal(data);
  117. case ZT_ENDPOINT_TYPE_IP:
  118. case ZT_ENDPOINT_TYPE_IP_TCP:
  119. case ZT_ENDPOINT_TYPE_IP_HTTP2:
  120. // Other IP types get serialized as new version Endpoint instances with type.
  121. data[0] = 16 + (uint8_t)this->type;
  122. return 1 + asInetAddress(this->value.ss).marshal(data + 1);
  123. }
  124. }
  125. int Endpoint::unmarshal(const uint8_t *restrict data, int len) noexcept
  126. {
  127. memoryZero(this);
  128. if (unlikely(len <= 0))
  129. return -1;
  130. // Serialized endpoints with type bytes less than 16 are passed through
  131. // to the unmarshal method of InetAddress and considered UDP endpoints.
  132. // This allows backward compatibility with old endpoint fields in the
  133. // protocol that were serialized InetAddress instances.
  134. if (data[0] < 16) {
  135. switch (data[0]) {
  136. case 0:
  137. return 1;
  138. case 4:
  139. case 6:
  140. this->type = ZT_ENDPOINT_TYPE_IP_UDP;
  141. return asInetAddress(this->value.ss).unmarshal(data, len);
  142. }
  143. return -1;
  144. }
  145. switch ((this->type = (ZT_EndpointType)(data[0] - 16))) {
  146. case ZT_ENDPOINT_TYPE_NIL:
  147. return 1;
  148. case ZT_ENDPOINT_TYPE_ZEROTIER:
  149. if (len >= (1 + ZT_ADDRESS_LENGTH + ZT_FINGERPRINT_HASH_SIZE)) {
  150. this->value.fp.address = Address(data + 1).toInt();
  151. Utils::copy<ZT_FINGERPRINT_HASH_SIZE>(this->value.fp.hash, data + 1 + ZT_ADDRESS_LENGTH);
  152. return 1 + ZT_ADDRESS_LENGTH + ZT_FINGERPRINT_HASH_SIZE;
  153. }
  154. return -1;
  155. case ZT_ENDPOINT_TYPE_ETHERNET:
  156. case ZT_ENDPOINT_TYPE_WIFI_DIRECT:
  157. case ZT_ENDPOINT_TYPE_BLUETOOTH:
  158. if (len >= 7) {
  159. MAC tmp;
  160. tmp.setTo(data + 1);
  161. this->value.mac = tmp.toInt();
  162. return 7;
  163. }
  164. return -1;
  165. case ZT_ENDPOINT_TYPE_IP:
  166. case ZT_ENDPOINT_TYPE_IP_UDP:
  167. case ZT_ENDPOINT_TYPE_IP_TCP:
  168. case ZT_ENDPOINT_TYPE_IP_HTTP2:
  169. return asInetAddress(this->value.ss).unmarshal(data + 1, len - 1);
  170. default:
  171. break;
  172. }
  173. // Unrecognized types can still be passed over in a valid stream if they are
  174. // prefixed by a 16-bit size. This allows forward compatibility with future
  175. // endpoint types.
  176. this->type = ZT_ENDPOINT_TYPE_NIL;
  177. if (len < 3)
  178. return -1;
  179. const int unrecLen = 1 + (int) Utils::loadBigEndian<uint16_t>(data + 1);
  180. return (unrecLen > len) ? -1 : unrecLen;
  181. }
  182. bool Endpoint::operator==(const Endpoint &ep) const noexcept
  183. {
  184. if (this->type == ep.type) {
  185. switch(this->type) {
  186. case ZT_ENDPOINT_TYPE_ZEROTIER:
  187. return zt() == ep.zt();
  188. case ZT_ENDPOINT_TYPE_ETHERNET:
  189. case ZT_ENDPOINT_TYPE_WIFI_DIRECT:
  190. case ZT_ENDPOINT_TYPE_BLUETOOTH:
  191. return this->value.mac == ep.value.mac;
  192. case ZT_ENDPOINT_TYPE_IP:
  193. case ZT_ENDPOINT_TYPE_IP_UDP:
  194. case ZT_ENDPOINT_TYPE_IP_TCP:
  195. case ZT_ENDPOINT_TYPE_IP_HTTP2:
  196. return ip() == ep.ip();
  197. default:
  198. return true;
  199. }
  200. }
  201. return false;
  202. }
  203. bool Endpoint::operator<(const Endpoint &ep) const noexcept
  204. {
  205. if (this->type == ep.type) {
  206. switch(this->type) {
  207. case ZT_ENDPOINT_TYPE_ZEROTIER:
  208. return zt() < ep.zt();
  209. case ZT_ENDPOINT_TYPE_ETHERNET:
  210. case ZT_ENDPOINT_TYPE_WIFI_DIRECT:
  211. case ZT_ENDPOINT_TYPE_BLUETOOTH:
  212. return this->value.mac < ep.value.mac;
  213. case ZT_ENDPOINT_TYPE_IP:
  214. case ZT_ENDPOINT_TYPE_IP_UDP:
  215. case ZT_ENDPOINT_TYPE_IP_TCP:
  216. case ZT_ENDPOINT_TYPE_IP_HTTP2:
  217. return ip() < ep.ip();
  218. default:
  219. return true;
  220. }
  221. }
  222. return (int)this->type < (int)ep.type;
  223. }
  224. } // namespace ZeroTier