InetAddress.hpp 10 KB

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