InetAddress.hpp 7.9 KB

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