2
0

InetAddress.hpp 7.7 KB

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