InetAddress.cpp 20 KB

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