InetAddress.hpp 9.4 KB

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