123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294 |
- #ifndef IP_UTILS_H_
- #define IP_UTILS_H_
- extern "C" {
- #include <pjlib.h>
- }
- #ifdef HAVE_CONFIG
- #include <config.h>
- #endif
- #include <ciso646>
- #ifdef _WIN32
- #define _WIN32_WINNT 0x0A00
- #include <ws2tcpip.h>
-
- #ifdef interface
- #undef interface
- #endif
- #else
- #include <arpa/inet.h>
- #include <netinet/in.h>
- #include <arpa/nameser.h>
- #include <resolv.h>
- #include <netdb.h>
- #include <netinet/ip.h>
- #include <net/if.h>
- #include <sys/ioctl.h>
- #endif
- #include <string>
- #include <vector>
- #ifndef IN_IS_ADDR_UNSPECIFIED
- #define IN_IS_ADDR_UNSPECIFIED(a) (((long int) (a)->s_addr) == 0x00000000)
- #endif
- namespace ring {
- class IpAddr {
- public:
- IpAddr(uint16_t family = AF_UNSPEC) : addr() {
- addr.addr.sa_family = family;
- }
-
- IpAddr(const IpAddr& other) : addr(other.addr) {}
- IpAddr(const pj_sockaddr& ip) : addr(ip) {}
- IpAddr(const sockaddr* ip, socklen_t len) : addr() {
- memcpy(&addr, ip, len);
- }
- IpAddr(const sockaddr_in& ip) : addr() {
- memcpy(&addr, &ip, sizeof(sockaddr_in));
- }
- IpAddr(const sockaddr_in6& ip) : addr() {
- memcpy(&addr, &ip, sizeof(sockaddr_in6));
- }
- IpAddr(const sockaddr& ip) : addr() {
- memcpy(&addr, &ip, ip.sa_family == AF_INET6 ? sizeof addr.ipv6 : sizeof addr.ipv4);
- }
- IpAddr(const sockaddr_storage& ip) : IpAddr(*reinterpret_cast<const sockaddr*>(&ip)) {}
- IpAddr(const in6_addr& ip) : addr() {
- addr.addr.sa_family = AF_INET6;
- memcpy(&addr.ipv6.sin6_addr, &ip, sizeof(in6_addr));
- }
- IpAddr(const in_addr& ip) : addr() {
- addr.addr.sa_family = AF_INET;
- memcpy(&addr.ipv4.sin_addr, &ip, sizeof(in_addr));
- }
-
- IpAddr(const std::string& str, pj_uint16_t family = AF_UNSPEC) : addr() {
- if (str.empty()) {
- addr.addr.sa_family = AF_UNSPEC;
- return;
- }
- pj_str_t pjstring;
- pj_cstr(&pjstring, str.c_str());
- auto status = pj_sockaddr_parse(family, 0, &pjstring, &addr);
- if (status != PJ_SUCCESS)
- addr.addr.sa_family = AF_UNSPEC;
- }
-
- inline explicit operator bool() const {
- return isIpv4() or isIpv6();
- }
- inline operator pj_sockaddr& () {
- return addr;
- }
- inline operator const pj_sockaddr& () const {
- return addr;
- }
- inline operator pj_sockaddr_in& () {
- return addr.ipv4;
- }
- inline operator const pj_sockaddr_in& () const {
- assert(addr.addr.sa_family != AF_INET6);
- return addr.ipv4;
- }
- inline operator pj_sockaddr_in6& () {
- return addr.ipv6;
- }
- inline operator const pj_sockaddr_in6& () const {
- assert(addr.addr.sa_family == AF_INET6);
- return addr.ipv6;
- }
- inline operator sockaddr& (){
- return reinterpret_cast<sockaddr&>(addr);
- }
- inline operator sockaddr_storage (){
- sockaddr_storage ss;
- memcpy(&ss, &addr, getLength());
- return ss;
- }
- inline const pj_sockaddr* pjPtr() const {
- return &addr;
- }
- inline pj_sockaddr* pjPtr() {
- return &addr;
- }
- inline operator std::string () const {
- return toString();
- }
- std::string toString(bool include_port=false, bool force_ipv6_brackets=false) const {
- std::string str(PJ_INET6_ADDRSTRLEN, (char)0);
- if (include_port) force_ipv6_brackets = true;
- pj_sockaddr_print(&addr, &(*str.begin()), PJ_INET6_ADDRSTRLEN, (include_port?1:0)|(force_ipv6_brackets?2:0));
- str.resize(std::char_traits<char>::length(str.c_str()));
- return str;
- }
- void setPort(uint16_t port) {
- pj_sockaddr_set_port(&addr, port);
- }
- inline uint16_t getPort() const {
- if (not *this)
- return 0;
- return pj_sockaddr_get_port(&addr);
- }
- inline socklen_t getLength() const {
- if (not *this)
- return 0;
- return pj_sockaddr_get_len(&addr);
- }
- inline uint16_t getFamily() const {
- return addr.addr.sa_family;
- }
- inline bool isIpv4() const {
- return addr.addr.sa_family == AF_INET;
- }
- inline bool isIpv6() const {
- return addr.addr.sa_family == AF_INET6;
- }
-
- bool isLoopback() const;
-
- bool isPrivate() const;
- bool isUnspecified() const;
-
- inline static bool isIpv6(const std::string& address) {
- return isValid(address, AF_INET6);
- }
-
- static bool isValid(const std::string& address, pj_uint16_t family = pj_AF_UNSPEC());
- private:
- pj_sockaddr addr;
- };
- inline bool operator==(const IpAddr& lhs, const IpAddr& rhs) { return !pj_sockaddr_cmp(&lhs, &rhs); }
- inline bool operator!=(const IpAddr& lhs, const IpAddr& rhs) { return !(lhs == rhs); }
- namespace ip_utils {
- static const char *const DEFAULT_INTERFACE = "default";
- std::string getHostname();
- IpAddr getAnyHostAddr(pj_uint16_t family = pj_AF_UNSPEC());
- IpAddr getLocalAddr(pj_uint16_t family = pj_AF_UNSPEC());
- IpAddr getInterfaceAddr(const std::string &interface, pj_uint16_t family = pj_AF_UNSPEC());
- std::vector<std::string> getAllIpInterfaceByName();
- std::vector<std::string> getAllIpInterface();
- std::vector<IpAddr> getAddrList(const std::string &name, pj_uint16_t family = pj_AF_UNSPEC());
- bool haveCommonAddr(const std::vector<IpAddr>& a, const std::vector<IpAddr>& b);
- std::vector<IpAddr> getLocalNameservers();
- }
- }
- #endif
|