Endpoint.cpp 7.2 KB

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