InetAddress.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2015 ZeroTier, Inc.
  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. * --
  19. *
  20. * ZeroTier may be used and distributed under the terms of the GPLv3, which
  21. * are available at: http://www.gnu.org/licenses/gpl-3.0.html
  22. *
  23. * If you would like to embed ZeroTier into a commercial application or
  24. * redistribute it in a modified binary form, please contact ZeroTier Networks
  25. * LLC. Start here: http://www.zerotier.com/
  26. */
  27. #ifndef ZT_INETADDRESS_HPP
  28. #define ZT_INETADDRESS_HPP
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <stdint.h>
  32. #include <string>
  33. #include "Constants.hpp"
  34. #include "../include/ZeroTierOne.h"
  35. #include "Utils.hpp"
  36. #include "MAC.hpp"
  37. namespace ZeroTier {
  38. /**
  39. * Extends sockaddr_storage with friendly C++ methods
  40. *
  41. * This is basically a "mixin" for sockaddr_storage. It adds methods and
  42. * operators, but does not modify the structure. This can be cast to/from
  43. * sockaddr_storage and used interchangeably. Don't change this as it's
  44. * used in a few places.
  45. */
  46. struct InetAddress : public sockaddr_storage
  47. {
  48. /**
  49. * Loopback IPv4 address (no port)
  50. */
  51. static const InetAddress LO4;
  52. /**
  53. * Loopback IPV6 address (no port)
  54. */
  55. static const InetAddress LO6;
  56. /**
  57. * 0.0.0.0/0
  58. */
  59. static const InetAddress DEFAULT4;
  60. /**
  61. * ::/0
  62. */
  63. static const InetAddress DEFAULT6;
  64. /**
  65. * IP address scope
  66. *
  67. * Do not change these numeric index values without taking a look
  68. * at SelfAwareness. Values 1-5 are mapped onto an array index.
  69. */
  70. enum IpScope
  71. {
  72. IP_SCOPE_NONE = 0, // not an IP address -- also the number of classes, must be last entry
  73. IP_SCOPE_LINK_LOCAL = 1, // 169.254.x.x, IPv6 LL
  74. IP_SCOPE_PRIVATE = 2, // 10.x.x.x, etc.
  75. IP_SCOPE_PSEUDOPRIVATE = 3, // 28.x.x.x, etc. -- unofficially unrouted IP blocks often "bogarted"
  76. IP_SCOPE_SHARED = 4, // 100.64.0.0/10, shared space for e.g. carrier-grade NAT
  77. IP_SCOPE_GLOBAL = 5, // globally routable IP address (all others)
  78. IP_SCOPE_LOOPBACK = 6, // 127.0.0.1
  79. IP_SCOPE_MULTICAST = 7 // 224.0.0.0 and other multicast IPs
  80. };
  81. InetAddress() throw() { memset(this,0,sizeof(InetAddress)); }
  82. InetAddress(const InetAddress &a) throw() { memcpy(this,&a,sizeof(InetAddress)); }
  83. InetAddress(const struct sockaddr_storage &ss) throw() { *this = ss; }
  84. InetAddress(const struct sockaddr_storage *ss) throw() { *this = ss; }
  85. InetAddress(const struct sockaddr &sa) throw() { *this = sa; }
  86. InetAddress(const struct sockaddr *sa) throw() { *this = sa; }
  87. InetAddress(const struct sockaddr_in &sa) throw() { *this = sa; }
  88. InetAddress(const struct sockaddr_in *sa) throw() { *this = sa; }
  89. InetAddress(const struct sockaddr_in6 &sa) throw() { *this = sa; }
  90. InetAddress(const struct sockaddr_in6 *sa) throw() { *this = sa; }
  91. InetAddress(const void *ipBytes,unsigned int ipLen,unsigned int port) throw() { this->set(ipBytes,ipLen,port); }
  92. InetAddress(const uint32_t ipv4,unsigned int port) throw() { this->set(&ipv4,4,port); }
  93. InetAddress(const std::string &ip,unsigned int port) throw() { this->set(ip,port); }
  94. InetAddress(const std::string &ipSlashPort) throw() { this->fromString(ipSlashPort); }
  95. InetAddress(const char *ipSlashPort) throw() { this->fromString(std::string(ipSlashPort)); }
  96. inline InetAddress &operator=(const InetAddress &a)
  97. throw()
  98. {
  99. memcpy(this,&a,sizeof(InetAddress));
  100. return *this;
  101. }
  102. inline InetAddress &operator=(const struct sockaddr_storage &ss)
  103. throw()
  104. {
  105. memcpy(this,&ss,sizeof(InetAddress));
  106. return *this;
  107. }
  108. inline InetAddress &operator=(const struct sockaddr_storage *ss)
  109. throw()
  110. {
  111. memcpy(this,ss,sizeof(InetAddress));
  112. return *this;
  113. }
  114. inline InetAddress &operator=(const struct sockaddr_in &sa)
  115. throw()
  116. {
  117. memset(this,0,sizeof(InetAddress));
  118. memcpy(this,&sa,sizeof(struct sockaddr_in));
  119. return *this;
  120. }
  121. inline InetAddress &operator=(const struct sockaddr_in *sa)
  122. throw()
  123. {
  124. memset(this,0,sizeof(InetAddress));
  125. memcpy(this,sa,sizeof(struct sockaddr_in));
  126. return *this;
  127. }
  128. inline InetAddress &operator=(const struct sockaddr_in6 &sa)
  129. throw()
  130. {
  131. memset(this,0,sizeof(InetAddress));
  132. memcpy(this,&sa,sizeof(struct sockaddr_in6));
  133. return *this;
  134. }
  135. inline InetAddress &operator=(const struct sockaddr_in6 *sa)
  136. throw()
  137. {
  138. memset(this,0,sizeof(InetAddress));
  139. memcpy(this,sa,sizeof(struct sockaddr_in6));
  140. return *this;
  141. }
  142. inline InetAddress &operator=(const struct sockaddr &sa)
  143. throw()
  144. {
  145. memset(this,0,sizeof(InetAddress));
  146. switch(sa.sa_family) {
  147. case AF_INET:
  148. memcpy(this,&sa,sizeof(struct sockaddr_in));
  149. break;
  150. case AF_INET6:
  151. memcpy(this,&sa,sizeof(struct sockaddr_in6));
  152. break;
  153. }
  154. return *this;
  155. }
  156. inline InetAddress &operator=(const struct sockaddr *sa)
  157. throw()
  158. {
  159. memset(this,0,sizeof(InetAddress));
  160. switch(sa->sa_family) {
  161. case AF_INET:
  162. memcpy(this,sa,sizeof(struct sockaddr_in));
  163. break;
  164. case AF_INET6:
  165. memcpy(this,sa,sizeof(struct sockaddr_in6));
  166. break;
  167. }
  168. return *this;
  169. }
  170. /**
  171. * @return IP scope classification (e.g. loopback, link-local, private, global)
  172. */
  173. IpScope ipScope() const
  174. throw();
  175. /**
  176. * Set from a string-format IP and a port
  177. *
  178. * @param ip IP address in V4 or V6 ASCII notation
  179. * @param port Port or 0 for none
  180. */
  181. void set(const std::string &ip,unsigned int port)
  182. throw();
  183. /**
  184. * Set from a raw IP and port number
  185. *
  186. * @param ipBytes Bytes of IP address in network byte order
  187. * @param ipLen Length of IP address: 4 or 16
  188. * @param port Port number or 0 for none
  189. */
  190. void set(const void *ipBytes,unsigned int ipLen,unsigned int port)
  191. throw();
  192. /**
  193. * Set the port component
  194. *
  195. * @param port Port, 0 to 65535
  196. */
  197. inline void setPort(unsigned int port)
  198. throw()
  199. {
  200. switch(ss_family) {
  201. case AF_INET:
  202. reinterpret_cast<struct sockaddr_in *>(this)->sin_port = Utils::hton((uint16_t)port);
  203. break;
  204. case AF_INET6:
  205. reinterpret_cast<struct sockaddr_in6 *>(this)->sin6_port = Utils::hton((uint16_t)port);
  206. break;
  207. }
  208. }
  209. /**
  210. * @return ASCII IP/port format representation
  211. */
  212. std::string toString() const;
  213. /**
  214. * @return IP portion only, in ASCII string format
  215. */
  216. std::string toIpString() const;
  217. /**
  218. * @param ipSlashPort ASCII IP/port format notation
  219. */
  220. void fromString(const std::string &ipSlashPort);
  221. /**
  222. * @return Port or 0 if no port component defined
  223. */
  224. inline unsigned int port() const
  225. throw()
  226. {
  227. switch(ss_family) {
  228. case AF_INET: return Utils::ntoh((uint16_t)(reinterpret_cast<const struct sockaddr_in *>(this)->sin_port));
  229. case AF_INET6: return Utils::ntoh((uint16_t)(reinterpret_cast<const struct sockaddr_in6 *>(this)->sin6_port));
  230. default: return 0;
  231. }
  232. }
  233. /**
  234. * Alias for port()
  235. *
  236. * This just aliases port() to make code more readable when netmask bits
  237. * are stuffed there, as they are in Network, EthernetTap, and a few other
  238. * spots.
  239. *
  240. * @return Netmask bits
  241. */
  242. inline unsigned int netmaskBits() const throw() { return port(); }
  243. /**
  244. * Construct a full netmask as an InetAddress
  245. */
  246. InetAddress netmask() const
  247. throw();
  248. /**
  249. * Constructs a broadcast address from a network/netmask address
  250. *
  251. * @return Broadcast address (only IP portion is meaningful)
  252. */
  253. InetAddress broadcast() const
  254. throw();
  255. /**
  256. * @return True if this is an IPv4 address
  257. */
  258. inline bool isV4() const throw() { return (ss_family == AF_INET); }
  259. /**
  260. * @return True if this is an IPv6 address
  261. */
  262. inline bool isV6() const throw() { return (ss_family == AF_INET6); }
  263. /**
  264. * Force type to IPv4
  265. */
  266. inline void setV4() throw() { ss_family = AF_INET; }
  267. /**
  268. * Force type to IPv6
  269. */
  270. inline void setV6() throw() { ss_family = AF_INET6; }
  271. /**
  272. * @return Length of sockaddr_in if IPv4, sockaddr_in6 if IPv6
  273. */
  274. inline unsigned int saddrLen() const
  275. throw()
  276. {
  277. switch(ss_family) {
  278. case AF_INET: return sizeof(struct sockaddr_in);
  279. case AF_INET6: return sizeof(struct sockaddr_in6);
  280. default: return 0;
  281. }
  282. }
  283. /**
  284. * @return Raw sockaddr_in structure (valid if IPv4)
  285. */
  286. inline const struct sockaddr_in *saddr4() const throw() { return reinterpret_cast<const struct sockaddr_in *>(this); }
  287. /**
  288. * @return Raw sockaddr_in6 structure (valid if IPv6)
  289. */
  290. inline const struct sockaddr_in6 *saddr6() const throw() { return reinterpret_cast<const struct sockaddr_in6 *>(this); }
  291. /**
  292. * @return pointer to raw IP address bytes
  293. */
  294. inline const void *rawIpData() const
  295. throw()
  296. {
  297. switch(ss_family) {
  298. case AF_INET: return (const void *)&(reinterpret_cast<const struct sockaddr_in *>(this)->sin_addr.s_addr);
  299. case AF_INET6: return (const void *)(reinterpret_cast<const struct sockaddr_in6 *>(this)->sin6_addr.s6_addr);
  300. default: return 0;
  301. }
  302. }
  303. /**
  304. * @return pointer to raw IP address bytes
  305. */
  306. inline void *rawIpData()
  307. throw()
  308. {
  309. switch(ss_family) {
  310. case AF_INET: return (void *)&(reinterpret_cast<struct sockaddr_in *>(this)->sin_addr.s_addr);
  311. case AF_INET6: return (void *)(reinterpret_cast<struct sockaddr_in6 *>(this)->sin6_addr.s6_addr);
  312. default: return 0;
  313. }
  314. }
  315. /**
  316. * @param a InetAddress to compare again
  317. * @return True if only IP portions are equal (false for non-IP or null addresses)
  318. */
  319. inline bool ipsEqual(const InetAddress &a) const
  320. {
  321. switch(ss_family) {
  322. case AF_INET: return (reinterpret_cast<const struct sockaddr_in *>(this)->sin_addr.s_addr == reinterpret_cast<const struct sockaddr_in *>(&a)->sin_addr.s_addr);
  323. case AF_INET6: return (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);
  324. }
  325. return false;
  326. }
  327. /**
  328. * Set to null/zero
  329. */
  330. inline void zero() throw() { memset(this,0,sizeof(InetAddress)); }
  331. /**
  332. * @return True if address family is non-zero
  333. */
  334. inline operator bool() const throw() { return (ss_family != 0); }
  335. inline bool operator==(const InetAddress &a) const throw() { return (memcmp(this,&a,sizeof(InetAddress)) == 0); }
  336. inline bool operator!=(const InetAddress &a) const throw() { return !(*this == a); }
  337. inline bool operator<(const InetAddress &a) const throw() { return (memcmp(this,&a,sizeof(InetAddress)) < 0); }
  338. inline bool operator>(const InetAddress &a) const throw() { return (a < *this); }
  339. inline bool operator<=(const InetAddress &a) const throw() { return !(a < *this); }
  340. inline bool operator>=(const InetAddress &a) const throw() { return !(*this < a); }
  341. /**
  342. * @param mac MAC address seed
  343. * @return IPv6 link-local address
  344. */
  345. static InetAddress makeIpv6LinkLocal(const MAC &mac)
  346. throw();
  347. };
  348. } // namespace ZeroTier
  349. #endif