InetAddress.hpp 9.7 KB

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