InetAddress.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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. * IP address scope
  58. */
  59. enum IpScope
  60. {
  61. IP_SCOPE_NONE = 0, // not an IP address -- also the number of classes, must be last entry
  62. IP_SCOPE_LINK_LOCAL = 1, // 169.254.x.x, IPv6 LL
  63. IP_SCOPE_PRIVATE = 2, // 10.x.x.x, etc.
  64. IP_SCOPE_PSEUDOPRIVATE = 3, // 28.x.x.x, etc. -- unofficially unrouted IP blocks often "bogarted"
  65. IP_SCOPE_SHARED = 4, // 100.64.0.0/10, shared space for e.g. carrier-grade NAT
  66. IP_SCOPE_GLOBAL = 5, // globally routable IP address (all others)
  67. IP_SCOPE_LOOPBACK = 6, // 127.0.0.1
  68. IP_SCOPE_MULTICAST = 7 // 224.0.0.0 and other multicast IPs
  69. };
  70. InetAddress() throw() { memset(this,0,sizeof(InetAddress)); }
  71. InetAddress(const InetAddress &a) throw() { memcpy(this,&a,sizeof(InetAddress)); }
  72. InetAddress(const InetAddress *a) throw() { memcpy(this,a,sizeof(InetAddress)); }
  73. InetAddress(const struct sockaddr_storage &ss) throw() { *this = ss; }
  74. InetAddress(const struct sockaddr_storage *ss) throw() { *this = ss; }
  75. InetAddress(const struct sockaddr &sa) throw() { *this = sa; }
  76. InetAddress(const struct sockaddr *sa) throw() { *this = sa; }
  77. InetAddress(const struct sockaddr_in &sa) throw() { *this = sa; }
  78. InetAddress(const struct sockaddr_in *sa) throw() { *this = sa; }
  79. InetAddress(const struct sockaddr_in6 &sa) throw() { *this = sa; }
  80. InetAddress(const struct sockaddr_in6 *sa) throw() { *this = sa; }
  81. InetAddress(const void *ipBytes,unsigned int ipLen,unsigned int port) throw() { this->set(ipBytes,ipLen,port); }
  82. InetAddress(const uint32_t ipv4,unsigned int port) throw() { this->set(&ipv4,4,port); }
  83. InetAddress(const std::string &ip,unsigned int port) throw() { this->set(ip,port); }
  84. InetAddress(const std::string &ipSlashPort) throw() { this->fromString(ipSlashPort); }
  85. InetAddress(const char *ipSlashPort) throw() { this->fromString(std::string(ipSlashPort)); }
  86. inline InetAddress &operator=(const InetAddress &a)
  87. throw()
  88. {
  89. memcpy(this,&a,sizeof(InetAddress));
  90. return *this;
  91. }
  92. inline InetAddress &operator=(const InetAddress *a)
  93. throw()
  94. {
  95. memcpy(this,a,sizeof(InetAddress));
  96. return *this;
  97. }
  98. inline InetAddress &operator=(const struct sockaddr_storage &ss)
  99. throw()
  100. {
  101. memcpy(this,&ss,sizeof(InetAddress));
  102. return *this;
  103. }
  104. inline InetAddress &operator=(const struct sockaddr_storage *ss)
  105. throw()
  106. {
  107. memcpy(this,ss,sizeof(InetAddress));
  108. return *this;
  109. }
  110. inline InetAddress &operator=(const struct sockaddr_in &sa)
  111. throw()
  112. {
  113. memset(this,0,sizeof(InetAddress));
  114. memcpy(this,&sa,sizeof(struct sockaddr_in));
  115. return *this;
  116. }
  117. inline InetAddress &operator=(const struct sockaddr_in *sa)
  118. throw()
  119. {
  120. memset(this,0,sizeof(InetAddress));
  121. memcpy(this,sa,sizeof(struct sockaddr_in));
  122. return *this;
  123. }
  124. inline InetAddress &operator=(const struct sockaddr_in6 &sa)
  125. throw()
  126. {
  127. memset(this,0,sizeof(InetAddress));
  128. memcpy(this,&sa,sizeof(struct sockaddr_in6));
  129. return *this;
  130. }
  131. inline InetAddress &operator=(const struct sockaddr_in6 *sa)
  132. throw()
  133. {
  134. memset(this,0,sizeof(InetAddress));
  135. memcpy(this,sa,sizeof(struct sockaddr_in6));
  136. return *this;
  137. }
  138. inline InetAddress &operator=(const struct sockaddr &sa)
  139. throw()
  140. {
  141. memset(this,0,sizeof(InetAddress));
  142. switch(sa.sa_family) {
  143. case AF_INET:
  144. memcpy(this,&sa,sizeof(struct sockaddr_in));
  145. break;
  146. case AF_INET6:
  147. memcpy(this,&sa,sizeof(struct sockaddr_in6));
  148. break;
  149. }
  150. return *this;
  151. }
  152. inline InetAddress &operator=(const struct sockaddr *sa)
  153. throw()
  154. {
  155. memset(this,0,sizeof(InetAddress));
  156. switch(sa->sa_family) {
  157. case AF_INET:
  158. memcpy(this,sa,sizeof(struct sockaddr_in));
  159. break;
  160. case AF_INET6:
  161. memcpy(this,sa,sizeof(struct sockaddr_in6));
  162. break;
  163. }
  164. return *this;
  165. }
  166. /**
  167. * @return IP scope classification (e.g. loopback, link-local, private, global)
  168. */
  169. IpScope ipScope() const
  170. throw();
  171. /**
  172. * Set from a string-format IP and a port
  173. *
  174. * @param ip IP address in V4 or V6 ASCII notation
  175. * @param port Port or 0 for none
  176. */
  177. void set(const std::string &ip,unsigned int port)
  178. throw();
  179. /**
  180. * Set from a raw IP and port number
  181. *
  182. * @param ipBytes Bytes of IP address in network byte order
  183. * @param ipLen Length of IP address: 4 or 16
  184. * @param port Port number or 0 for none
  185. */
  186. void set(const void *ipBytes,unsigned int ipLen,unsigned int port)
  187. throw();
  188. /**
  189. * Set the port component
  190. *
  191. * @param port Port, 0 to 65535
  192. */
  193. inline void setPort(unsigned int port)
  194. throw()
  195. {
  196. switch(ss_family) {
  197. case AF_INET:
  198. reinterpret_cast<struct sockaddr_in *>(this)->sin_port = Utils::hton((uint16_t)port);
  199. break;
  200. case AF_INET6:
  201. reinterpret_cast<struct sockaddr_in6 *>(this)->sin6_port = Utils::hton((uint16_t)port);
  202. break;
  203. }
  204. }
  205. /**
  206. * @return ASCII IP/port format representation
  207. */
  208. std::string toString() const;
  209. /**
  210. * @return IP portion only, in ASCII string format
  211. */
  212. std::string toIpString() const;
  213. /**
  214. * @param ipSlashPort ASCII IP/port format notation
  215. */
  216. void fromString(const std::string &ipSlashPort);
  217. /**
  218. * @return Port or 0 if no port component defined
  219. */
  220. inline unsigned int port() const
  221. throw()
  222. {
  223. switch(ss_family) {
  224. case AF_INET: return Utils::ntoh((uint16_t)(reinterpret_cast<const struct sockaddr_in *>(this)->sin_port));
  225. case AF_INET6: return Utils::ntoh((uint16_t)(reinterpret_cast<const struct sockaddr_in6 *>(this)->sin6_port));
  226. default: return 0;
  227. }
  228. }
  229. /**
  230. * Alias for port()
  231. *
  232. * This just aliases port() to make code more readable when netmask bits
  233. * are stuffed there, as they are in Network, EthernetTap, and a few other
  234. * spots.
  235. *
  236. * @return Netmask bits
  237. */
  238. inline unsigned int netmaskBits() const throw() { return port(); }
  239. /**
  240. * Alias for port()
  241. *
  242. * This just aliases port() because for gateways we use this field to
  243. * store the gateway metric.
  244. *
  245. * @return Gateway metric
  246. */
  247. inline unsigned int metric() const throw() { return port(); }
  248. /**
  249. * Construct a full netmask as an InetAddress
  250. */
  251. InetAddress netmask() const
  252. throw();
  253. /**
  254. * Constructs a broadcast address from a network/netmask address
  255. *
  256. * @return Broadcast address (only IP portion is meaningful)
  257. */
  258. InetAddress broadcast() const
  259. throw();
  260. /**
  261. * @return True if this is an IPv4 address
  262. */
  263. inline bool isV4() const throw() { return (ss_family == AF_INET); }
  264. /**
  265. * @return True if this is an IPv6 address
  266. */
  267. inline bool isV6() const throw() { return (ss_family == AF_INET6); }
  268. /**
  269. * @return pointer to raw IP address bytes
  270. */
  271. inline const void *rawIpData() const
  272. throw()
  273. {
  274. switch(ss_family) {
  275. case AF_INET: return (const void *)&(reinterpret_cast<const struct sockaddr_in *>(this)->sin_addr.s_addr);
  276. case AF_INET6: return (const void *)(reinterpret_cast<const struct sockaddr_in6 *>(this)->sin6_addr.s6_addr);
  277. default: return 0;
  278. }
  279. }
  280. /**
  281. * @return pointer to raw IP address bytes
  282. */
  283. inline void *rawIpData()
  284. throw()
  285. {
  286. switch(ss_family) {
  287. case AF_INET: return (void *)&(reinterpret_cast<struct sockaddr_in *>(this)->sin_addr.s_addr);
  288. case AF_INET6: return (void *)(reinterpret_cast<struct sockaddr_in6 *>(this)->sin6_addr.s6_addr);
  289. default: return 0;
  290. }
  291. }
  292. /**
  293. * @param a InetAddress to compare again
  294. * @return True if only IP portions are equal (false for non-IP or null addresses)
  295. */
  296. inline bool ipsEqual(const InetAddress &a) const
  297. {
  298. switch(ss_family) {
  299. 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);
  300. 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);
  301. }
  302. return false;
  303. }
  304. /**
  305. * Set to null/zero
  306. */
  307. inline void zero() throw() { memset(this,0,sizeof(InetAddress)); }
  308. /**
  309. * Check whether this is a network/route rather than an IP assignment
  310. *
  311. * A network is an IP/netmask where everything after the netmask is
  312. * zero e.g. 10.0.0.0/8.
  313. *
  314. * @return True if everything after netmask bits is zero
  315. */
  316. bool isNetwork() const
  317. throw();
  318. /**
  319. * @return True if address family is non-zero
  320. */
  321. inline operator bool() const throw() { return (ss_family != 0); }
  322. bool operator==(const InetAddress &a) const throw();
  323. bool operator<(const InetAddress &a) const throw();
  324. inline bool operator!=(const InetAddress &a) const throw() { return !(*this == a); }
  325. inline bool operator>(const InetAddress &a) const throw() { return (a < *this); }
  326. inline bool operator<=(const InetAddress &a) const throw() { return !(a < *this); }
  327. inline bool operator>=(const InetAddress &a) const throw() { return !(*this < a); }
  328. /**
  329. * @param mac MAC address seed
  330. * @return IPv6 link-local address
  331. */
  332. static InetAddress makeIpv6LinkLocal(const MAC &mac)
  333. throw();
  334. };
  335. } // namespace ZeroTier
  336. #endif