ip_utils.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /*
  2. * Copyright (C) 2004-2017 Savoir-faire Linux Inc.
  3. *
  4. * Author: Adrien Béraud <adrien.beraud@savoirfairelinux.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. */
  20. #ifndef IP_UTILS_H_
  21. #define IP_UTILS_H_
  22. extern "C" {
  23. #include <pjlib.h>
  24. }
  25. #ifdef HAVE_CONFIG
  26. #include <config.h>
  27. #endif
  28. #include <ciso646> // fix windows compiler bug
  29. #ifdef _WIN32
  30. #define _WIN32_WINNT 0x0A00
  31. #include <ws2tcpip.h>
  32. //define in mingw
  33. #ifdef interface
  34. #undef interface
  35. #endif
  36. #else
  37. #include <arpa/inet.h>
  38. #include <netinet/in.h>
  39. #include <arpa/nameser.h>
  40. #include <resolv.h>
  41. #include <netdb.h>
  42. #include <netinet/ip.h>
  43. #include <net/if.h>
  44. #include <sys/ioctl.h>
  45. #endif
  46. #include <string>
  47. #include <vector>
  48. /* An IPv4 equivalent to IN6_IS_ADDR_UNSPECIFIED */
  49. #ifndef IN_IS_ADDR_UNSPECIFIED
  50. #define IN_IS_ADDR_UNSPECIFIED(a) (((long int) (a)->s_addr) == 0x00000000)
  51. #endif /* IN_IS_ADDR_UNSPECIFIED */
  52. namespace ring {
  53. /**
  54. * Binary representation of an IP address.
  55. */
  56. class IpAddr {
  57. public:
  58. IpAddr(uint16_t family = AF_UNSPEC) : addr() {
  59. addr.addr.sa_family = family;
  60. }
  61. // From a sockaddr-type structure
  62. IpAddr(const IpAddr& other) : addr(other.addr) {}
  63. IpAddr(const pj_sockaddr& ip) : addr(ip) {}
  64. IpAddr(const sockaddr* ip, socklen_t len) : addr() {
  65. memcpy(&addr, ip, len);
  66. }
  67. IpAddr(const sockaddr_in& ip) : addr() {
  68. memcpy(&addr, &ip, sizeof(sockaddr_in));
  69. }
  70. IpAddr(const sockaddr_in6& ip) : addr() {
  71. memcpy(&addr, &ip, sizeof(sockaddr_in6));
  72. }
  73. IpAddr(const sockaddr& ip) : addr() {
  74. memcpy(&addr, &ip, ip.sa_family == AF_INET6 ? sizeof addr.ipv6 : sizeof addr.ipv4);
  75. }
  76. IpAddr(const sockaddr_storage& ip) : IpAddr(*reinterpret_cast<const sockaddr*>(&ip)) {}
  77. IpAddr(const in6_addr& ip) : addr() {
  78. addr.addr.sa_family = AF_INET6;
  79. memcpy(&addr.ipv6.sin6_addr, &ip, sizeof(in6_addr));
  80. }
  81. IpAddr(const in_addr& ip) : addr() {
  82. addr.addr.sa_family = AF_INET;
  83. memcpy(&addr.ipv4.sin_addr, &ip, sizeof(in_addr));
  84. }
  85. // From a string
  86. IpAddr(const std::string& str, pj_uint16_t family = AF_UNSPEC) : addr() {
  87. if (str.empty()) {
  88. addr.addr.sa_family = AF_UNSPEC;
  89. return;
  90. }
  91. pj_str_t pjstring;
  92. pj_cstr(&pjstring, str.c_str());
  93. auto status = pj_sockaddr_parse(family, 0, &pjstring, &addr);
  94. if (status != PJ_SUCCESS)
  95. addr.addr.sa_family = AF_UNSPEC;
  96. }
  97. // Is defined
  98. inline explicit operator bool() const {
  99. return isIpv4() or isIpv6();
  100. }
  101. inline operator pj_sockaddr& () {
  102. return addr;
  103. }
  104. inline operator const pj_sockaddr& () const {
  105. return addr;
  106. }
  107. inline operator pj_sockaddr_in& () {
  108. return addr.ipv4;
  109. }
  110. inline operator const pj_sockaddr_in& () const {
  111. assert(addr.addr.sa_family != AF_INET6);
  112. return addr.ipv4;
  113. }
  114. inline operator pj_sockaddr_in6& () {
  115. return addr.ipv6;
  116. }
  117. inline operator const pj_sockaddr_in6& () const {
  118. assert(addr.addr.sa_family == AF_INET6);
  119. return addr.ipv6;
  120. }
  121. inline operator sockaddr& (){
  122. return reinterpret_cast<sockaddr&>(addr);
  123. }
  124. inline operator sockaddr_storage (){
  125. sockaddr_storage ss;
  126. memcpy(&ss, &addr, getLength());
  127. return ss;
  128. }
  129. inline const pj_sockaddr* pjPtr() const {
  130. return &addr;
  131. }
  132. inline pj_sockaddr* pjPtr() {
  133. return &addr;
  134. }
  135. inline operator std::string () const {
  136. return toString();
  137. }
  138. std::string toString(bool include_port=false, bool force_ipv6_brackets=false) const {
  139. std::string str(PJ_INET6_ADDRSTRLEN, (char)0);
  140. if (include_port) force_ipv6_brackets = true;
  141. pj_sockaddr_print(&addr, &(*str.begin()), PJ_INET6_ADDRSTRLEN, (include_port?1:0)|(force_ipv6_brackets?2:0));
  142. str.resize(std::char_traits<char>::length(str.c_str()));
  143. return str;
  144. }
  145. void setPort(uint16_t port) {
  146. pj_sockaddr_set_port(&addr, port);
  147. }
  148. inline uint16_t getPort() const {
  149. if (not *this)
  150. return 0;
  151. return pj_sockaddr_get_port(&addr);
  152. }
  153. inline socklen_t getLength() const {
  154. if (not *this)
  155. return 0;
  156. return pj_sockaddr_get_len(&addr);
  157. }
  158. inline uint16_t getFamily() const {
  159. return addr.addr.sa_family;
  160. }
  161. inline bool isIpv4() const {
  162. return addr.addr.sa_family == AF_INET;
  163. }
  164. inline bool isIpv6() const {
  165. return addr.addr.sa_family == AF_INET6;
  166. }
  167. /**
  168. * Return true if address is a loopback IP address.
  169. */
  170. bool isLoopback() const;
  171. /**
  172. * Return true if address is not a public IP address.
  173. */
  174. bool isPrivate() const;
  175. bool isUnspecified() const;
  176. /**
  177. * Return true if address is a valid IPv6.
  178. */
  179. inline static bool isIpv6(const std::string& address) {
  180. return isValid(address, AF_INET6);
  181. }
  182. /**
  183. * Return true if address is a valid IP address of specified family (if provided) or of any kind (default).
  184. * Does not resolve hostnames.
  185. */
  186. static bool isValid(const std::string& address, pj_uint16_t family = pj_AF_UNSPEC());
  187. private:
  188. pj_sockaddr addr;
  189. };
  190. // IpAddr helpers
  191. inline bool operator==(const IpAddr& lhs, const IpAddr& rhs) { return !pj_sockaddr_cmp(&lhs, &rhs); }
  192. inline bool operator!=(const IpAddr& lhs, const IpAddr& rhs) { return !(lhs == rhs); }
  193. namespace ip_utils {
  194. static const char *const DEFAULT_INTERFACE = "default";
  195. std::string getHostname();
  196. /**
  197. * Return the generic "any host" IP address of the specified family.
  198. * If family is unspecified, default to pj_AF_INET6() (IPv6).
  199. */
  200. IpAddr getAnyHostAddr(pj_uint16_t family = pj_AF_UNSPEC());
  201. /**
  202. * Return the first host IP address of the specified family.
  203. * If no address of the specified family is found, another family will
  204. * be tried.
  205. * Ex. : if family is pj_AF_INET6() (IPv6/default) and the system does not
  206. * have an IPv6 address, an IPv4 address will be returned if available.
  207. *
  208. * If family is unspecified, default to pj_AF_INET6() if compiled
  209. * with IPv6, or pj_AF_INET() otherwise.
  210. */
  211. IpAddr getLocalAddr(pj_uint16_t family = pj_AF_UNSPEC());
  212. /**
  213. * Get the IP address of the network interface interface with the specified
  214. * address family, or of any address family if unspecified (default).
  215. */
  216. IpAddr getInterfaceAddr(const std::string &interface, pj_uint16_t family = pj_AF_UNSPEC());
  217. /**
  218. * List all the interfaces on the system and return
  219. * a vector list containing their name (eth0, eth0:1 ...).
  220. * @param void
  221. * @return std::vector<std::string> A std::string vector
  222. * of interface name available on all of the interfaces on
  223. * the system.
  224. */
  225. std::vector<std::string> getAllIpInterfaceByName();
  226. /**
  227. * List all the interfaces on the system and return
  228. * a vector list containing their IP address.
  229. * @param void
  230. * @return std::vector<std::string> A std::string vector
  231. * of IP address available on all of the interfaces on
  232. * the system.
  233. */
  234. std::vector<std::string> getAllIpInterface();
  235. std::vector<IpAddr> getAddrList(const std::string &name, pj_uint16_t family = pj_AF_UNSPEC());
  236. bool haveCommonAddr(const std::vector<IpAddr>& a, const std::vector<IpAddr>& b);
  237. std::vector<IpAddr> getLocalNameservers();
  238. } // namespace ip_utils
  239. } // namespace ring
  240. #endif // IP_UTILS_H_