ip_utils.h 9.35 KB
Newer Older
1
/*
2
 *  Copyright (C) 2004-2019 Savoir-faire Linux Inc.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 *
 *  Author: Adrien Béraud <adrien.beraud@savoirfairelinux.com>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.
 */

21
#pragma once
22

23 24
#include <sstream> // include before pjlib.h to fix macros issues with pjlib.h

25
extern "C" {
26
#include <pjlib.h>
27
}
Adrien Béraud's avatar
Adrien Béraud committed
28

29 30 31 32
#ifdef HAVE_CONFIG
 #include <config.h>
#endif

33 34
#include <ciso646> // fix windows compiler bug

Edric Milaret's avatar
Edric Milaret committed
35
#ifdef _WIN32
36 37 38 39 40
    #ifdef RING_UWP
        #define _WIN32_WINNT 0x0A00
    #else
        #define _WIN32_WINNT 0x0601
    #endif
Edric Milaret's avatar
Edric Milaret committed
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
    #include <ws2tcpip.h>

    //define in mingw
    #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
57 58 59 60

#include <string>
#include <vector>

61 62 63 64 65 66

/* An IPv4 equivalent to IN6_IS_ADDR_UNSPECIFIED */
#ifndef IN_IS_ADDR_UNSPECIFIED
#define IN_IS_ADDR_UNSPECIFIED(a) (((long int) (a)->s_addr) == 0x00000000)
#endif /* IN_IS_ADDR_UNSPECIFIED */

Adrien Béraud's avatar
Adrien Béraud committed
67
namespace jami {
Adrien Béraud's avatar
Adrien Béraud committed
68

69 70 71
/**
 * Binary representation of an IP address.
 */
Adrien Béraud's avatar
Adrien Béraud committed
72 73
class IpAddr {
public:
74 75 76 77 78 79 80
    IpAddr() : IpAddr(AF_UNSPEC) {}
    IpAddr(const IpAddr&) = default;
    IpAddr(IpAddr&&) = default;
    IpAddr& operator=(const IpAddr&) = default;
    IpAddr& operator=(IpAddr&&) = default;

    explicit IpAddr(uint16_t family) : addr() {
Adrien Béraud's avatar
Adrien Béraud committed
81 82 83 84
        addr.addr.sa_family = family;
    }

    IpAddr(const pj_sockaddr& ip) : addr(ip) {}
85 86 87 88 89 90 91 92 93

    IpAddr(const pj_sockaddr& ip, socklen_t len) : addr() {
        if (len > sizeof(addr))
            throw std::invalid_argument("IpAddr(): length overflows internal storage type");
        memcpy(&addr, &ip, len);
    }

    IpAddr(const sockaddr& ip) : addr() {
        memcpy(&addr, &ip, ip.sa_family == AF_INET6 ? sizeof addr.ipv6 : sizeof addr.ipv4);
94 95
    }

Adrien Béraud's avatar
Adrien Béraud committed
96
    IpAddr(const sockaddr_in& ip) : addr() {
97
        static_assert(sizeof(ip) <= sizeof(addr), "sizeof(sockaddr_in) too large");
Adrien Béraud's avatar
Adrien Béraud committed
98 99
        memcpy(&addr, &ip, sizeof(sockaddr_in));
    }
100

Adrien Béraud's avatar
Adrien Béraud committed
101
    IpAddr(const sockaddr_in6& ip) : addr() {
102
        static_assert(sizeof(ip) <= sizeof(addr), "sizeof(sockaddr_in6) too large");
Adrien Béraud's avatar
Adrien Béraud committed
103 104
        memcpy(&addr, &ip, sizeof(sockaddr_in6));
    }
105

106
    IpAddr(const sockaddr_storage& ip) : IpAddr(*reinterpret_cast<const sockaddr*>(&ip)) {}
107

108
    IpAddr(const in_addr& ip) : addr() {
109
        static_assert(sizeof(ip) <= sizeof(addr), "sizeof(in_addr) too large");
110 111 112
        addr.addr.sa_family = AF_INET;
        memcpy(&addr.ipv4.sin_addr, &ip, sizeof(in_addr));
    }
Adrien Béraud's avatar
Adrien Béraud committed
113

114 115 116 117 118 119
    IpAddr(const in6_addr& ip) : addr() {
        static_assert(sizeof(ip) <= sizeof(addr), "sizeof(in6_addr) too large");
        addr.addr.sa_family = AF_INET6;
        memcpy(&addr.ipv6.sin6_addr, &ip, sizeof(in6_addr));
    }

Adrien Béraud's avatar
Adrien Béraud committed
120
    IpAddr(const std::string& str, pj_uint16_t family = AF_UNSPEC) : addr() {
121 122 123 124
        if (str.empty()) {
            addr.addr.sa_family = AF_UNSPEC;
            return;
        }
125
        const pj_str_t pjstring {(char*)str.c_str(), (pj_ssize_t)str.size()};
Adrien Béraud's avatar
Adrien Béraud committed
126 127 128 129 130 131
        auto status = pj_sockaddr_parse(family, 0, &pjstring, &addr);
        if (status != PJ_SUCCESS)
            addr.addr.sa_family = AF_UNSPEC;
    }

    // Is defined
132
    inline explicit operator bool() const {
Adrien Béraud's avatar
Adrien Béraud committed
133 134 135
        return isIpv4() or isIpv6();
    }

136 137 138 139
    inline explicit operator bool() {
        return isIpv4() or isIpv6();
    }

Adrien Béraud's avatar
Adrien Béraud committed
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
    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;
    }

166 167
    inline operator const sockaddr& () const {
        return reinterpret_cast<const sockaddr&>(addr);
Adrien Béraud's avatar
Adrien Béraud committed
168 169
    }

170 171
    inline operator const sockaddr* () const {
        return reinterpret_cast<const sockaddr*>(&addr);
172 173
    }

174
    inline operator sockaddr_storage (){
175
        sockaddr_storage ss;
176 177 178 179
        memcpy(&ss, &addr, getLength());
        return ss;
    }

180 181 182 183
    inline const pj_sockaddr* pjPtr() const {
        return &addr;
    }

Adrien Béraud's avatar
Adrien Béraud committed
184 185 186 187 188 189 190 191 192
    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 {
193
        if (addr.addr.sa_family == AF_UNSPEC) return {};
Adrien Béraud's avatar
Adrien Béraud committed
194 195 196 197 198 199 200 201 202 203 204 205
        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 {
206 207
        if (not *this)
            return 0;
Adrien Béraud's avatar
Adrien Béraud committed
208 209 210
        return pj_sockaddr_get_port(&addr);
    }

211
    inline socklen_t getLength() const {
212 213
        if (not *this)
            return 0;
214 215 216
        return pj_sockaddr_get_len(&addr);
    }

Adrien Béraud's avatar
Adrien Béraud committed
217 218 219 220 221 222 223 224 225 226 227
    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;
    }
228

229
    /**
Adrien Béraud's avatar
Adrien Béraud committed
230
     * Return true if address is a loopback IP address.
231
     */
Adrien Béraud's avatar
Adrien Béraud committed
232
    bool isLoopback() const;
233 234

    /**
Adrien Béraud's avatar
Adrien Béraud committed
235
     * Return true if address is not a public IP address.
236
     */
Adrien Béraud's avatar
Adrien Béraud committed
237 238 239
    bool isPrivate() const;

    bool isUnspecified() const;
240 241

    /**
Adrien Béraud's avatar
Adrien Béraud committed
242
     * Return true if address is a valid IPv6.
243
     */
Adrien Béraud's avatar
Adrien Béraud committed
244 245 246 247 248 249 250 251 252 253 254
    inline static bool isIpv6(const std::string& address) {
        return isValid(address, AF_INET6);
    }

    /**
     * Return true if address is a valid IP address of specified family (if provided) or of any kind (default).
     * Does not resolve hostnames.
     */
    static bool isValid(const std::string& address, pj_uint16_t family = pj_AF_UNSPEC());

private:
255
    pj_sockaddr addr {};
Adrien Béraud's avatar
Adrien Béraud committed
256 257
};

258 259 260
// IpAddr helpers
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); }
261 262 263 264
inline bool operator<(const IpAddr& lhs, const IpAddr& rhs) { return pj_sockaddr_cmp(&lhs, &rhs) < 0; }
inline bool operator>(const IpAddr& lhs, const IpAddr& rhs) { return pj_sockaddr_cmp(&lhs, &rhs) > 0; }
inline bool operator<=(const IpAddr& lhs, const IpAddr& rhs) { return pj_sockaddr_cmp(&lhs, &rhs) <= 0; }
inline bool operator>=(const IpAddr& lhs, const IpAddr& rhs) { return pj_sockaddr_cmp(&lhs, &rhs) >= 0; }
265

Adrien Béraud's avatar
Adrien Béraud committed
266
namespace ip_utils {
267

268
static const char *const DEFAULT_INTERFACE = "default";
269

270 271
std::string getHostname();

272 273
std::string getDeviceName();

274 275 276 277
/**
 * Return the generic "any host" IP address of the specified family.
 * If family is unspecified, default to pj_AF_INET6() (IPv6).
 */
Adrien Béraud's avatar
Adrien Béraud committed
278
inline IpAddr getAnyHostAddr(pj_uint16_t family)  { return IpAddr(family); }
279 280 281 282 283 284 285 286 287 288 289

/**
 * Return the first host IP address of the specified family.
 * If no address of the specified family is found, another family will
 * be tried.
 * Ex. : if family is pj_AF_INET6() (IPv6/default) and the system does not
 * have an IPv6 address, an IPv4 address will be returned if available.
 *
 * If family is unspecified, default to pj_AF_INET6() if compiled
 * with IPv6, or pj_AF_INET() otherwise.
 */
Adrien Béraud's avatar
Adrien Béraud committed
290
IpAddr getLocalAddr(pj_uint16_t family);
291

292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
/**
 * Get the IP address of the network interface interface with the specified
 * address family, or of any address family if unspecified (default).
 */
IpAddr getInterfaceAddr(const std::string &interface, pj_uint16_t family = pj_AF_UNSPEC());

/**
 * List all the interfaces on the system and return
 * a vector list containing their name (eth0, eth0:1 ...).
 * @param void
 * @return std::vector<std::string> A std::string vector
 * of interface name available on all of the interfaces on
 * the system.
 */
std::vector<std::string> getAllIpInterfaceByName();

/**
 * List all the interfaces on the system and return
 * a vector list containing their IP address.
 * @param void
 * @return std::vector<std::string> A std::string vector
 * of IP address available on all of the interfaces on
 * the system.
 */
std::vector<std::string> getAllIpInterface();
317

318
std::vector<IpAddr> getAddrList(const std::string &name, pj_uint16_t family = pj_AF_UNSPEC());
319

320
bool haveCommonAddr(const std::vector<IpAddr>& a, const std::vector<IpAddr>& b);
321

322
std::vector<IpAddr> getLocalNameservers();
323

324
} // namespace ip_utils
Adrien Béraud's avatar
Adrien Béraud committed
325
} // namespace jami