InetAddress.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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 <cstring>
  14. #include <cstdint>
  15. #include "Constants.hpp"
  16. #include "InetAddress.hpp"
  17. #include "Utils.hpp"
  18. namespace ZeroTier {
  19. const InetAddress InetAddress::LO4((const void *)("\x7f\x00\x00\x01"),4,0);
  20. const InetAddress InetAddress::LO6((const void *)("\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01"),16,0);
  21. const InetAddress InetAddress::NIL;
  22. InetAddress::IpScope InetAddress::ipScope() const
  23. {
  24. switch(ss_family) {
  25. case AF_INET: {
  26. const uint32_t ip = Utils::ntoh((uint32_t)reinterpret_cast<const struct sockaddr_in *>(this)->sin_addr.s_addr);
  27. switch(ip >> 24U) {
  28. case 0x00: return IP_SCOPE_NONE; // 0.0.0.0/8 (reserved, never used)
  29. case 0x06: return IP_SCOPE_PSEUDOPRIVATE; // 6.0.0.0/8 (US Army)
  30. case 0x0a: return IP_SCOPE_PRIVATE; // 10.0.0.0/8
  31. case 0x0b: //return IP_SCOPE_PSEUDOPRIVATE; // 11.0.0.0/8 (US DoD)
  32. case 0x15: //return IP_SCOPE_PSEUDOPRIVATE; // 21.0.0.0/8 (US DDN-RVN)
  33. case 0x16: //return IP_SCOPE_PSEUDOPRIVATE; // 22.0.0.0/8 (US DISA)
  34. case 0x19: //return IP_SCOPE_PSEUDOPRIVATE; // 25.0.0.0/8 (UK Ministry of Defense)
  35. case 0x1a: //return IP_SCOPE_PSEUDOPRIVATE; // 26.0.0.0/8 (US DISA)
  36. case 0x1c: //return IP_SCOPE_PSEUDOPRIVATE; // 28.0.0.0/8 (US DSI-North)
  37. case 0x1d: //return IP_SCOPE_PSEUDOPRIVATE; // 29.0.0.0/8 (US DISA)
  38. case 0x1e: //return IP_SCOPE_PSEUDOPRIVATE; // 30.0.0.0/8 (US DISA)
  39. case 0x33: //return IP_SCOPE_PSEUDOPRIVATE; // 51.0.0.0/8 (UK Department of Social Security)
  40. case 0x37: //return IP_SCOPE_PSEUDOPRIVATE; // 55.0.0.0/8 (US DoD)
  41. case 0x38: return IP_SCOPE_PSEUDOPRIVATE; // 56.0.0.0/8 (US Postal Service)
  42. case 0x64:
  43. if ((ip & 0xffc00000) == 0x64400000) return IP_SCOPE_PRIVATE; // 100.64.0.0/10
  44. break;
  45. case 0x7f: return IP_SCOPE_LOOPBACK; // 127.0.0.0/8
  46. case 0xa9:
  47. if ((ip & 0xffff0000) == 0xa9fe0000) return IP_SCOPE_LINK_LOCAL; // 169.254.0.0/16
  48. break;
  49. case 0xac:
  50. if ((ip & 0xfff00000) == 0xac100000) return IP_SCOPE_PRIVATE; // 172.16.0.0/12
  51. break;
  52. case 0xc0:
  53. if ((ip & 0xffff0000) == 0xc0a80000) return IP_SCOPE_PRIVATE; // 192.168.0.0/16
  54. break;
  55. case 0xff: return IP_SCOPE_NONE; // 255.0.0.0/8 (broadcast, or unused/unusable)
  56. }
  57. switch(ip >> 28) {
  58. case 0xe: return IP_SCOPE_MULTICAST; // 224.0.0.0/4
  59. case 0xf: return IP_SCOPE_PSEUDOPRIVATE; // 240.0.0.0/4 ("reserved," usually unusable)
  60. }
  61. return IP_SCOPE_GLOBAL;
  62. } break;
  63. case AF_INET6: {
  64. const unsigned char *ip = reinterpret_cast<const unsigned char *>(reinterpret_cast<const struct sockaddr_in6 *>(this)->sin6_addr.s6_addr);
  65. if ((ip[0] & 0xf0) == 0xf0) {
  66. if (ip[0] == 0xff) return IP_SCOPE_MULTICAST; // ff00::/8
  67. if ((ip[0] == 0xfe)&&((ip[1] & 0xc0) == 0x80)) {
  68. unsigned int k = 2;
  69. while ((!ip[k])&&(k < 15)) ++k;
  70. if ((k == 15)&&(ip[15] == 0x01))
  71. return IP_SCOPE_LOOPBACK; // fe80::1/128
  72. else return IP_SCOPE_LINK_LOCAL; // fe80::/10
  73. }
  74. if ((ip[0] & 0xfe) == 0xfc) return IP_SCOPE_PRIVATE; // fc00::/7
  75. }
  76. unsigned int k = 0;
  77. while ((!ip[k])&&(k < 15)) ++k;
  78. if (k == 15) { // all 0's except last byte
  79. if (ip[15] == 0x01) return IP_SCOPE_LOOPBACK; // ::1/128
  80. if (ip[15] == 0x00) return IP_SCOPE_NONE; // ::/128
  81. }
  82. return IP_SCOPE_GLOBAL;
  83. } break;
  84. }
  85. return IP_SCOPE_NONE;
  86. }
  87. void InetAddress::set(const void *ipBytes,unsigned int ipLen,unsigned int port)
  88. {
  89. memset(this,0,sizeof(InetAddress));
  90. if (ipLen == 4) {
  91. uint32_t ipb[1];
  92. memcpy(ipb,ipBytes,4);
  93. ss_family = AF_INET;
  94. reinterpret_cast<struct sockaddr_in *>(this)->sin_addr.s_addr = ipb[0];
  95. reinterpret_cast<struct sockaddr_in *>(this)->sin_port = Utils::hton((uint16_t)port);
  96. } else if (ipLen == 16) {
  97. ss_family = AF_INET6;
  98. memcpy(reinterpret_cast<struct sockaddr_in6 *>(this)->sin6_addr.s6_addr,ipBytes,16);
  99. reinterpret_cast<struct sockaddr_in6 *>(this)->sin6_port = Utils::hton((uint16_t)port);
  100. }
  101. }
  102. char *InetAddress::toString(char buf[64]) const
  103. {
  104. char *p = toIpString(buf);
  105. if (*p) {
  106. while (*p) ++p;
  107. *(p++) = '/';
  108. Utils::decimal(port(),p);
  109. }
  110. return buf;
  111. }
  112. char *InetAddress::toIpString(char buf[64]) const
  113. {
  114. buf[0] = (char)0;
  115. switch(ss_family) {
  116. case AF_INET: {
  117. #ifdef _WIN32
  118. inet_ntop(AF_INET, (void*)&reinterpret_cast<const struct sockaddr_in *>(this)->sin_addr.s_addr, buf, INET_ADDRSTRLEN);
  119. #else
  120. inet_ntop(AF_INET, &reinterpret_cast<const struct sockaddr_in *>(this)->sin_addr.s_addr, buf, INET_ADDRSTRLEN);
  121. #endif
  122. } break;
  123. case AF_INET6: {
  124. #ifdef _WIN32
  125. inet_ntop(AF_INET6, (void*)reinterpret_cast<const struct sockaddr_in6 *>(this)->sin6_addr.s6_addr, buf, INET6_ADDRSTRLEN);
  126. #else
  127. inet_ntop(AF_INET6, reinterpret_cast<const struct sockaddr_in6 *>(this)->sin6_addr.s6_addr, buf, INET6_ADDRSTRLEN);
  128. #endif
  129. } break;
  130. }
  131. return buf;
  132. }
  133. bool InetAddress::fromString(const char *ipSlashPort)
  134. {
  135. char buf[64];
  136. memset(this,0,sizeof(InetAddress));
  137. if (!*ipSlashPort)
  138. return true;
  139. if (!Utils::scopy(buf,sizeof(buf),ipSlashPort))
  140. return false;
  141. char *portAt = buf;
  142. while ((*portAt)&&(*portAt != '/'))
  143. ++portAt;
  144. unsigned int port = 0;
  145. if (*portAt) {
  146. *(portAt++) = (char)0;
  147. port = Utils::strToUInt(portAt) & 0xffffU;
  148. }
  149. if (strchr(buf,':')) {
  150. struct sockaddr_in6 *const in6 = reinterpret_cast<struct sockaddr_in6 *>(this);
  151. inet_pton(AF_INET6, buf, &in6->sin6_addr.s6_addr);
  152. in6->sin6_family = AF_INET6;
  153. in6->sin6_port = Utils::hton((uint16_t)port);
  154. return true;
  155. } else if (strchr(buf,'.')) {
  156. struct sockaddr_in *const in = reinterpret_cast<struct sockaddr_in *>(this);
  157. inet_pton(AF_INET, buf, &in->sin_addr.s_addr);
  158. in->sin_family = AF_INET;
  159. in->sin_port = Utils::hton((uint16_t)port);
  160. return true;
  161. } else {
  162. return false;
  163. }
  164. }
  165. InetAddress InetAddress::netmask() const
  166. {
  167. InetAddress r(*this);
  168. switch(r.ss_family) {
  169. case AF_INET:
  170. reinterpret_cast<struct sockaddr_in *>(&r)->sin_addr.s_addr = Utils::hton((uint32_t)(0xffffffffU << (32 - netmaskBits())));
  171. break;
  172. case AF_INET6: {
  173. uint64_t nm[2];
  174. const unsigned int bits = netmaskBits();
  175. if(bits) {
  176. nm[0] = Utils::hton((uint64_t)((bits >= 64) ? 0xffffffffffffffffULL : (0xffffffffffffffffULL << (64 - bits))));
  177. nm[1] = Utils::hton((uint64_t)((bits <= 64) ? 0ULL : (0xffffffffffffffffULL << (128 - bits))));
  178. } else {
  179. nm[0] = 0;
  180. nm[1] = 0;
  181. }
  182. memcpy(reinterpret_cast<struct sockaddr_in6 *>(&r)->sin6_addr.s6_addr,nm,16);
  183. } break;
  184. }
  185. return r;
  186. }
  187. InetAddress InetAddress::broadcast() const
  188. {
  189. if (ss_family == AF_INET) {
  190. InetAddress r(*this);
  191. reinterpret_cast<struct sockaddr_in *>(&r)->sin_addr.s_addr |= Utils::hton((uint32_t)(0xffffffffU >> netmaskBits()));
  192. return r;
  193. }
  194. return InetAddress();
  195. }
  196. InetAddress InetAddress::network() const
  197. {
  198. InetAddress r(*this);
  199. switch(r.ss_family) {
  200. case AF_INET:
  201. reinterpret_cast<struct sockaddr_in *>(&r)->sin_addr.s_addr &= Utils::hton((uint32_t)(0xffffffffU << (32 - netmaskBits())));
  202. break;
  203. case AF_INET6: {
  204. uint64_t nm[2];
  205. const unsigned int bits = netmaskBits();
  206. memcpy(nm,reinterpret_cast<struct sockaddr_in6 *>(&r)->sin6_addr.s6_addr,16);
  207. nm[0] &= Utils::hton((uint64_t)((bits >= 64) ? 0xffffffffffffffffULL : (0xffffffffffffffffULL << (64 - bits))));
  208. nm[1] &= Utils::hton((uint64_t)((bits <= 64) ? 0ULL : (0xffffffffffffffffULL << (128 - bits))));
  209. memcpy(reinterpret_cast<struct sockaddr_in6 *>(&r)->sin6_addr.s6_addr,nm,16);
  210. } break;
  211. }
  212. return r;
  213. }
  214. bool InetAddress::isEqualPrefix(const InetAddress &addr) const
  215. {
  216. if (addr.ss_family == ss_family) {
  217. switch(ss_family) {
  218. case AF_INET6: {
  219. const InetAddress mask(netmask());
  220. InetAddress addr_mask(addr.netmask());
  221. const uint8_t *n = reinterpret_cast<const uint8_t *>(reinterpret_cast<const struct sockaddr_in6 *>(&addr_mask)->sin6_addr.s6_addr);
  222. const uint8_t *m = reinterpret_cast<const uint8_t *>(reinterpret_cast<const struct sockaddr_in6 *>(&mask)->sin6_addr.s6_addr);
  223. const uint8_t *a = reinterpret_cast<const uint8_t *>(reinterpret_cast<const struct sockaddr_in6 *>(&addr)->sin6_addr.s6_addr);
  224. const uint8_t *b = reinterpret_cast<const uint8_t *>(reinterpret_cast<const struct sockaddr_in6 *>(this)->sin6_addr.s6_addr);
  225. for(unsigned int i=0;i<16;++i) {
  226. if ((a[i] & m[i]) != (b[i] & n[i]))
  227. return false;
  228. }
  229. return true;
  230. }
  231. }
  232. }
  233. return false;
  234. }
  235. bool InetAddress::containsAddress(const InetAddress &addr) const
  236. {
  237. if (addr.ss_family == ss_family) {
  238. switch(ss_family) {
  239. case AF_INET: {
  240. const unsigned int bits = netmaskBits();
  241. if (bits == 0)
  242. return true;
  243. return ( (Utils::ntoh((uint32_t)reinterpret_cast<const struct sockaddr_in *>(&addr)->sin_addr.s_addr) >> (32 - bits)) == (Utils::ntoh((uint32_t)reinterpret_cast<const struct sockaddr_in *>(this)->sin_addr.s_addr) >> (32 - bits)) );
  244. }
  245. case AF_INET6: {
  246. const InetAddress mask(netmask());
  247. const uint8_t *m = reinterpret_cast<const uint8_t *>(reinterpret_cast<const struct sockaddr_in6 *>(&mask)->sin6_addr.s6_addr);
  248. const uint8_t *a = reinterpret_cast<const uint8_t *>(reinterpret_cast<const struct sockaddr_in6 *>(&addr)->sin6_addr.s6_addr);
  249. const uint8_t *b = reinterpret_cast<const uint8_t *>(reinterpret_cast<const struct sockaddr_in6 *>(this)->sin6_addr.s6_addr);
  250. for(unsigned int i=0;i<16;++i) {
  251. if ((a[i] & m[i]) != b[i])
  252. return false;
  253. }
  254. return true;
  255. }
  256. }
  257. }
  258. return false;
  259. }
  260. bool InetAddress::isNetwork() const
  261. {
  262. switch(ss_family) {
  263. case AF_INET: {
  264. unsigned int bits = netmaskBits();
  265. if (bits <= 0)
  266. return false;
  267. if (bits >= 32)
  268. return false;
  269. uint32_t ip = Utils::ntoh((uint32_t)reinterpret_cast<const struct sockaddr_in *>(this)->sin_addr.s_addr);
  270. return ((ip & (0xffffffffU >> bits)) == 0);
  271. }
  272. case AF_INET6: {
  273. unsigned int bits = netmaskBits();
  274. if (bits <= 0)
  275. return false;
  276. if (bits >= 128)
  277. return false;
  278. const unsigned char *ip = reinterpret_cast<const unsigned char *>(reinterpret_cast<const struct sockaddr_in6 *>(this)->sin6_addr.s6_addr);
  279. unsigned int p = bits / 8;
  280. if ((ip[p++] & (0xffU >> (bits % 8))) != 0)
  281. return false;
  282. while (p < 16) {
  283. if (ip[p++])
  284. return false;
  285. }
  286. return true;
  287. }
  288. }
  289. return false;
  290. }
  291. bool InetAddress::operator==(const InetAddress &a) const
  292. {
  293. if (ss_family == a.ss_family) {
  294. switch(ss_family) {
  295. case AF_INET:
  296. return (
  297. (reinterpret_cast<const struct sockaddr_in *>(this)->sin_port == reinterpret_cast<const struct sockaddr_in *>(&a)->sin_port)&&
  298. (reinterpret_cast<const struct sockaddr_in *>(this)->sin_addr.s_addr == reinterpret_cast<const struct sockaddr_in *>(&a)->sin_addr.s_addr));
  299. break;
  300. case AF_INET6:
  301. return (
  302. (reinterpret_cast<const struct sockaddr_in6 *>(this)->sin6_port == reinterpret_cast<const struct sockaddr_in6 *>(&a)->sin6_port)&&
  303. (reinterpret_cast<const struct sockaddr_in6 *>(this)->sin6_flowinfo == reinterpret_cast<const struct sockaddr_in6 *>(&a)->sin6_flowinfo)&&
  304. (memcmp(reinterpret_cast<const struct sockaddr_in6 *>(this)->sin6_addr.s6_addr,reinterpret_cast<const struct sockaddr_in6 *>(&a)->sin6_addr.s6_addr,16) == 0)&&
  305. (reinterpret_cast<const struct sockaddr_in6 *>(this)->sin6_scope_id == reinterpret_cast<const struct sockaddr_in6 *>(&a)->sin6_scope_id));
  306. break;
  307. default:
  308. return (memcmp(this,&a,sizeof(InetAddress)) == 0);
  309. }
  310. }
  311. return false;
  312. }
  313. bool InetAddress::operator<(const InetAddress &a) const
  314. {
  315. if (ss_family < a.ss_family)
  316. return true;
  317. else if (ss_family == a.ss_family) {
  318. switch(ss_family) {
  319. case AF_INET:
  320. if (reinterpret_cast<const struct sockaddr_in *>(this)->sin_port < reinterpret_cast<const struct sockaddr_in *>(&a)->sin_port)
  321. return true;
  322. else if (reinterpret_cast<const struct sockaddr_in *>(this)->sin_port == reinterpret_cast<const struct sockaddr_in *>(&a)->sin_port) {
  323. if (reinterpret_cast<const struct sockaddr_in *>(this)->sin_addr.s_addr < reinterpret_cast<const struct sockaddr_in *>(&a)->sin_addr.s_addr)
  324. return true;
  325. }
  326. break;
  327. case AF_INET6:
  328. if (reinterpret_cast<const struct sockaddr_in6 *>(this)->sin6_port < reinterpret_cast<const struct sockaddr_in6 *>(&a)->sin6_port)
  329. return true;
  330. else if (reinterpret_cast<const struct sockaddr_in6 *>(this)->sin6_port == reinterpret_cast<const struct sockaddr_in6 *>(&a)->sin6_port) {
  331. if (reinterpret_cast<const struct sockaddr_in6 *>(this)->sin6_flowinfo < reinterpret_cast<const struct sockaddr_in6 *>(&a)->sin6_flowinfo)
  332. return true;
  333. else if (reinterpret_cast<const struct sockaddr_in6 *>(this)->sin6_flowinfo == reinterpret_cast<const struct sockaddr_in6 *>(&a)->sin6_flowinfo) {
  334. if (memcmp(reinterpret_cast<const struct sockaddr_in6 *>(this)->sin6_addr.s6_addr,reinterpret_cast<const struct sockaddr_in6 *>(&a)->sin6_addr.s6_addr,16) < 0)
  335. return true;
  336. else if (memcmp(reinterpret_cast<const struct sockaddr_in6 *>(this)->sin6_addr.s6_addr,reinterpret_cast<const struct sockaddr_in6 *>(&a)->sin6_addr.s6_addr,16) == 0) {
  337. if (reinterpret_cast<const struct sockaddr_in6 *>(this)->sin6_scope_id < reinterpret_cast<const struct sockaddr_in6 *>(&a)->sin6_scope_id)
  338. return true;
  339. }
  340. }
  341. }
  342. break;
  343. default:
  344. return (memcmp(this,&a,sizeof(InetAddress)) < 0);
  345. }
  346. }
  347. return false;
  348. }
  349. InetAddress InetAddress::makeIpv6LinkLocal(const MAC &mac)
  350. {
  351. InetAddress r;
  352. sockaddr_in6 *const sin6 = reinterpret_cast<sockaddr_in6 *>(&r);
  353. sin6->sin6_family = AF_INET6;
  354. sin6->sin6_addr.s6_addr[0] = 0xfe;
  355. sin6->sin6_addr.s6_addr[1] = 0x80;
  356. sin6->sin6_addr.s6_addr[2] = 0x00;
  357. sin6->sin6_addr.s6_addr[3] = 0x00;
  358. sin6->sin6_addr.s6_addr[4] = 0x00;
  359. sin6->sin6_addr.s6_addr[5] = 0x00;
  360. sin6->sin6_addr.s6_addr[6] = 0x00;
  361. sin6->sin6_addr.s6_addr[7] = 0x00;
  362. sin6->sin6_addr.s6_addr[8] = mac[0] & 0xfdU;
  363. sin6->sin6_addr.s6_addr[9] = mac[1];
  364. sin6->sin6_addr.s6_addr[10] = mac[2];
  365. sin6->sin6_addr.s6_addr[11] = 0xff;
  366. sin6->sin6_addr.s6_addr[12] = 0xfe;
  367. sin6->sin6_addr.s6_addr[13] = mac[3];
  368. sin6->sin6_addr.s6_addr[14] = mac[4];
  369. sin6->sin6_addr.s6_addr[15] = mac[5];
  370. sin6->sin6_port = Utils::hton((uint16_t)64);
  371. return r;
  372. }
  373. InetAddress InetAddress::makeIpv6rfc4193(uint64_t nwid,uint64_t zeroTierAddress)
  374. {
  375. InetAddress r;
  376. sockaddr_in6 *const sin6 = reinterpret_cast<sockaddr_in6 *>(&r);
  377. sin6->sin6_family = AF_INET6;
  378. sin6->sin6_addr.s6_addr[0] = 0xfd;
  379. sin6->sin6_addr.s6_addr[1] = (uint8_t)(nwid >> 56U);
  380. sin6->sin6_addr.s6_addr[2] = (uint8_t)(nwid >> 48U);
  381. sin6->sin6_addr.s6_addr[3] = (uint8_t)(nwid >> 40U);
  382. sin6->sin6_addr.s6_addr[4] = (uint8_t)(nwid >> 32U);
  383. sin6->sin6_addr.s6_addr[5] = (uint8_t)(nwid >> 24U);
  384. sin6->sin6_addr.s6_addr[6] = (uint8_t)(nwid >> 16U);
  385. sin6->sin6_addr.s6_addr[7] = (uint8_t)(nwid >> 8U);
  386. sin6->sin6_addr.s6_addr[8] = (uint8_t)nwid;
  387. sin6->sin6_addr.s6_addr[9] = 0x99;
  388. sin6->sin6_addr.s6_addr[10] = 0x93;
  389. sin6->sin6_addr.s6_addr[11] = (uint8_t)(zeroTierAddress >> 32U);
  390. sin6->sin6_addr.s6_addr[12] = (uint8_t)(zeroTierAddress >> 24U);
  391. sin6->sin6_addr.s6_addr[13] = (uint8_t)(zeroTierAddress >> 16U);
  392. sin6->sin6_addr.s6_addr[14] = (uint8_t)(zeroTierAddress >> 8U);
  393. sin6->sin6_addr.s6_addr[15] = (uint8_t)zeroTierAddress;
  394. sin6->sin6_port = Utils::hton((uint16_t)88); // /88 includes 0xfd + network ID, discriminating by device ID below that
  395. return r;
  396. }
  397. InetAddress InetAddress::makeIpv66plane(uint64_t nwid,uint64_t zeroTierAddress)
  398. {
  399. nwid ^= (nwid >> 32U);
  400. InetAddress r;
  401. sockaddr_in6 *const sin6 = reinterpret_cast<sockaddr_in6 *>(&r);
  402. sin6->sin6_family = AF_INET6;
  403. sin6->sin6_addr.s6_addr[0] = 0xfc;
  404. sin6->sin6_addr.s6_addr[1] = (uint8_t)(nwid >> 24U);
  405. sin6->sin6_addr.s6_addr[2] = (uint8_t)(nwid >> 16U);
  406. sin6->sin6_addr.s6_addr[3] = (uint8_t)(nwid >> 8U);
  407. sin6->sin6_addr.s6_addr[4] = (uint8_t)nwid;
  408. sin6->sin6_addr.s6_addr[5] = (uint8_t)(zeroTierAddress >> 32U);
  409. sin6->sin6_addr.s6_addr[6] = (uint8_t)(zeroTierAddress >> 24U);
  410. sin6->sin6_addr.s6_addr[7] = (uint8_t)(zeroTierAddress >> 16U);
  411. sin6->sin6_addr.s6_addr[8] = (uint8_t)(zeroTierAddress >> 8U);
  412. sin6->sin6_addr.s6_addr[9] = (uint8_t)zeroTierAddress;
  413. sin6->sin6_addr.s6_addr[15] = 0x01;
  414. sin6->sin6_port = Utils::hton((uint16_t)40);
  415. return r;
  416. }
  417. } // namespace ZeroTier