InetAddress.hpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. /*
  2. * ZeroTier One - Global Peer to Peer Ethernet
  3. * Copyright (C) 2011-2014 ZeroTier Networks LLC
  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 "Utils.hpp"
  35. #include "MAC.hpp"
  36. #ifdef __WINDOWS__
  37. #include <WinSock2.h>
  38. #include <WS2tcpip.h>
  39. #include <Windows.h>
  40. #else
  41. #include <netinet/in.h>
  42. #include <arpa/inet.h>
  43. #endif
  44. namespace ZeroTier {
  45. /**
  46. * Wrapper for sockaddr structures for IPV4 and IPV6
  47. *
  48. * Note: this class is raw memcpy'able, which is used in a couple places.
  49. */
  50. class InetAddress
  51. {
  52. public:
  53. /**
  54. * Address type
  55. */
  56. enum AddressType
  57. {
  58. TYPE_NULL = 0,
  59. TYPE_IPV4 = AF_INET,
  60. TYPE_IPV6 = AF_INET6
  61. };
  62. /**
  63. * Loopback IPv4 address (no port)
  64. */
  65. static const InetAddress LO4;
  66. /**
  67. * Loopback IPV6 address (no port)
  68. */
  69. static const InetAddress LO6;
  70. /**
  71. * 0.0.0.0/0
  72. */
  73. static const InetAddress DEFAULT4;
  74. /**
  75. * ::/0
  76. */
  77. static const InetAddress DEFAULT6;
  78. InetAddress() throw() { memset(&_sa,0,sizeof(_sa)); }
  79. InetAddress(const InetAddress &a) throw() { memcpy(&_sa,&a._sa,sizeof(_sa)); }
  80. InetAddress(const struct sockaddr *sa) throw() { this->set(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(&_sa,&a._sa,sizeof(_sa));
  90. return *this;
  91. }
  92. /**
  93. * Set from an OS-level sockaddr structure
  94. *
  95. * @param sa Socket address (V4 or V6)
  96. */
  97. inline void set(const struct sockaddr *sa)
  98. throw()
  99. {
  100. switch(sa->sa_family) {
  101. case AF_INET: memcpy(&_sa.sin,sa,sizeof(struct sockaddr_in)); break;
  102. case AF_INET6: memcpy(&_sa.sin6,sa,sizeof(struct sockaddr_in6)); break;
  103. default: memset(&_sa,0,sizeof(_sa)); break;
  104. }
  105. }
  106. /**
  107. * Set from a string-format IP and a port
  108. *
  109. * @param ip IP address in V4 or V6 ASCII notation
  110. * @param port Port or 0 for none
  111. */
  112. void set(const std::string &ip,unsigned int port)
  113. throw();
  114. /**
  115. * Set from a raw IP and port number
  116. *
  117. * @param ipBytes Bytes of IP address in network byte order
  118. * @param ipLen Length of IP address: 4 or 16
  119. * @param port Port number or 0 for none
  120. */
  121. void set(const void *ipBytes,unsigned int ipLen,unsigned int port)
  122. throw();
  123. /**
  124. * Set the port component
  125. *
  126. * @param port Port, 0 to 65535
  127. */
  128. inline void setPort(unsigned int port)
  129. throw()
  130. {
  131. if (_sa.saddr.sa_family == AF_INET)
  132. _sa.sin.sin_port = Utils::hton((uint16_t)port);
  133. else if (_sa.saddr.sa_family == AF_INET6)
  134. _sa.sin6.sin6_port = Utils::hton((uint16_t)port);
  135. }
  136. /**
  137. * @return True if this is a link-local IP address
  138. */
  139. bool isLinkLocal() const
  140. throw();
  141. /**
  142. * @return True if this ip/netmask would represent a default route (e.g. 0.0.0.0/0)
  143. */
  144. bool isDefaultRoute() const
  145. throw();
  146. /**
  147. * @return True if this is a loopback address
  148. */
  149. inline bool isLoopback() const
  150. throw()
  151. {
  152. return ((*this == LO4)||(*this == LO6));
  153. }
  154. /**
  155. * @return ASCII IP/port format representation
  156. */
  157. std::string toString() const;
  158. /**
  159. * @param ipSlashPort ASCII IP/port format notation
  160. */
  161. void fromString(const std::string &ipSlashPort);
  162. /**
  163. * @return IP portion only, in ASCII string format
  164. */
  165. std::string toIpString() const;
  166. /**
  167. * @return Port or 0 if no port component defined
  168. */
  169. inline unsigned int port() const
  170. throw()
  171. {
  172. switch(_sa.saddr.sa_family) {
  173. case AF_INET: return Utils::ntoh((uint16_t)_sa.sin.sin_port);
  174. case AF_INET6: return Utils::ntoh((uint16_t)_sa.sin6.sin6_port);
  175. default: return 0;
  176. }
  177. }
  178. /**
  179. * Alias for port()
  180. *
  181. * This just aliases port() to make code more readable when netmask bits
  182. * are stuffed there, as they are in Network, EthernetTap, and a few other
  183. * spots.
  184. *
  185. * @return Netmask bits
  186. */
  187. inline unsigned int netmaskBits() const throw() { return port(); }
  188. /**
  189. * Construct a full netmask as an InetAddress
  190. */
  191. InetAddress netmask() const
  192. throw();
  193. /**
  194. * Constructs a broadcast address from a network/netmask address
  195. *
  196. * @return Broadcast address (only IP portion is meaningful)
  197. */
  198. InetAddress broadcast() const
  199. throw();
  200. /**
  201. * @return True if this is an IPv4 address
  202. */
  203. inline bool isV4() const throw() { return (_sa.saddr.sa_family == AF_INET); }
  204. /**
  205. * @return True if this is an IPv6 address
  206. */
  207. inline bool isV6() const throw() { return (_sa.saddr.sa_family == AF_INET6); }
  208. /**
  209. * @return Address type or TYPE_NULL if not defined
  210. */
  211. inline AddressType type() const throw() { return (AddressType)_sa.saddr.sa_family; }
  212. /**
  213. * Force type to IPv4
  214. */
  215. inline void setV4() throw() { _sa.saddr.sa_family = AF_INET; }
  216. /**
  217. * Force type to IPv6
  218. */
  219. inline void setV6() throw() { _sa.saddr.sa_family = AF_INET6; }
  220. /**
  221. * @return Raw sockaddr structure
  222. */
  223. inline struct sockaddr *saddr() throw() { return &(_sa.saddr); }
  224. inline const struct sockaddr *saddr() const throw() { return &(_sa.saddr); }
  225. /**
  226. * @return Length of sockaddr_in if IPv4, sockaddr_in6 if IPv6
  227. */
  228. inline unsigned int saddrLen() const
  229. throw()
  230. {
  231. switch(_sa.saddr.sa_family) {
  232. case AF_INET: return sizeof(struct sockaddr_in);
  233. case AF_INET6: return sizeof(struct sockaddr_in6);
  234. default: return 0;
  235. }
  236. }
  237. /**
  238. * @return Combined length of internal structure, room for either V4 or V6
  239. */
  240. inline unsigned int saddrSpaceLen() const throw() { return sizeof(_sa); }
  241. /**
  242. * @return Raw sockaddr_in structure (valid if IPv4)
  243. */
  244. inline const struct sockaddr_in *saddr4() const throw() { return &(_sa.sin); }
  245. /**
  246. * @return Raw sockaddr_in6 structure (valid if IPv6)
  247. */
  248. inline const struct sockaddr_in6 *saddr6() const throw() { return &(_sa.sin6); }
  249. /**
  250. * @return Raw IP address (4 bytes for IPv4, 16 bytes for IPv6)
  251. */
  252. inline void *rawIpData() throw() { return ((_sa.saddr.sa_family == AF_INET) ? (void *)(&(_sa.sin.sin_addr.s_addr)) : (void *)_sa.sin6.sin6_addr.s6_addr); }
  253. inline const void *rawIpData() const throw() { return ((_sa.saddr.sa_family == AF_INET) ? (void *)(&(_sa.sin.sin_addr.s_addr)) : (void *)_sa.sin6.sin6_addr.s6_addr); }
  254. /**
  255. * Compare only the IP portions of addresses, ignoring port/netmask
  256. *
  257. * @param a Address to compare
  258. * @return True if both addresses are of the same (valid) type and their IPs match
  259. */
  260. inline bool ipsEqual(const InetAddress &a) const
  261. throw()
  262. {
  263. if (_sa.saddr.sa_family == a._sa.saddr.sa_family) {
  264. switch(_sa.saddr.sa_family) {
  265. case AF_INET:
  266. return (_sa.sin.sin_addr.s_addr == a._sa.sin.sin_addr.s_addr);
  267. case AF_INET6:
  268. return (!memcmp(_sa.sin6.sin6_addr.s6_addr,a._sa.sin6.sin6_addr.s6_addr,16));
  269. }
  270. }
  271. return false;
  272. }
  273. /**
  274. * Compare IP/netmask with another IP/netmask
  275. *
  276. * @param ipnet IP/netmask to compare with
  277. * @return True if [netmask] bits match
  278. */
  279. bool sameNetworkAs(const InetAddress &ipnet) const
  280. throw();
  281. /**
  282. * Determine whether this address is within an ip/netmask
  283. *
  284. * @param ipnet IP/netmask
  285. * @return True if this address is within this network
  286. */
  287. bool within(const InetAddress &ipnet) const
  288. throw();
  289. /**
  290. * Set to null/zero
  291. */
  292. inline void zero() throw() { memset(&_sa,0,sizeof(_sa)); }
  293. /**
  294. * @return True if address family is non-zero
  295. */
  296. inline operator bool() const throw() { return ((_sa.saddr.sa_family == AF_INET)||(_sa.saddr.sa_family == AF_INET6)); }
  297. bool operator==(const InetAddress &a) const throw();
  298. inline bool operator!=(const InetAddress &a) const throw() { return !(*this == a); }
  299. bool operator<(const InetAddress &a) const throw();
  300. inline bool operator>(const InetAddress &a) const throw() { return (a < *this); }
  301. inline bool operator<=(const InetAddress &a) const throw() { return !(a < *this); }
  302. inline bool operator>=(const InetAddress &a) const throw() { return !(*this < a); }
  303. /**
  304. * @param mac MAC address seed
  305. * @return IPv6 link-local address
  306. */
  307. static InetAddress makeIpv6LinkLocal(const MAC &mac)
  308. throw();
  309. private:
  310. union {
  311. struct sockaddr saddr;
  312. struct sockaddr_in sin;
  313. struct sockaddr_in6 sin6;
  314. } _sa;
  315. };
  316. } // namespace ZeroTier
  317. #endif