ip_utils.cpp 9.49 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 21
 *
 *  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.
 */

#include "ip_utils.h"
22 23 24
#ifdef s_addr
    #undef s_addr
#endif
25 26
#include "logger.h"

27
#include "sip/sip_utils.h"
Adrien Béraud's avatar
Adrien Béraud committed
28

29
#include <sys/types.h>
30
#include <unistd.h>
31
#include <limits.h>
32

33 34 35 36
#if defined(__ANDROID__) || defined(RING_UWP) || (defined(TARGET_OS_IOS) && TARGET_OS_IOS)
#include "client/ring_signal.h"
#endif

Edric Milaret's avatar
Edric Milaret committed
37 38 39 40 41
#ifdef _WIN32
#define InetPtonA inet_pton
WINSOCK_API_LINKAGE INT WSAAPI InetPtonA(INT Family, LPCSTR pStringBuf, PVOID pAddr);
#endif

42 43 44 45 46 47 48 49 50
#ifndef HOST_NAME_MAX
#ifdef MAX_COMPUTERNAME_LENGTH
#define HOST_NAME_MAX MAX_COMPUTERNAME_LENGTH
#else
// Max 255 chars as per RFC 1035
#define HOST_NAME_MAX 255
#endif
#endif

Adrien Béraud's avatar
Adrien Béraud committed
51
namespace jami {
52

53 54 55 56 57 58 59 60 61
std::string
ip_utils::getHostname()
{
    char hostname[HOST_NAME_MAX];
    if (gethostname(hostname, HOST_NAME_MAX))
        return {};
    return hostname;
}

62 63 64 65 66 67 68 69 70 71 72 73 74
std::string
ip_utils::getDeviceName()
{
#if defined(__ANDROID__) || defined(RING_UWP) || (defined(TARGET_OS_IOS) && TARGET_OS_IOS)
    std::vector<std::string> deviceNames;
    emitSignal<DRing::ConfigurationSignal::GetDeviceName>(&deviceNames);
    if (not deviceNames.empty()) {
        return deviceNames[0];
    }
#endif
    return getHostname();
}

Adrien Béraud's avatar
Adrien Béraud committed
75
std::vector<IpAddr>
76
ip_utils::getAddrList(const std::string &name, pj_uint16_t family)
77
{
Adrien Béraud's avatar
Adrien Béraud committed
78
    std::vector<IpAddr> ipList;
79 80
    if (name.empty())
        return ipList;
81 82 83 84
    if (IpAddr::isValid(name, family)) {
        ipList.push_back(name);
        return ipList;
    }
85

86
    static constexpr unsigned MAX_ADDR_NUM = 128;
87 88
    pj_addrinfo res[MAX_ADDR_NUM];
    unsigned addr_num = MAX_ADDR_NUM;
89
    const pj_str_t pjname(sip_utils::CONST_PJ_STR(name));
90
    auto status = pj_getaddrinfo(family, &pjname, &addr_num, res);
91
    if (status != PJ_SUCCESS) {
92
        JAMI_ERR("Error resolving %.*s : %s", (int)name.size(), name.c_str(),
93
                 sip_utils::sip_strerror(status).c_str());
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
        return ipList;
    }

    for (unsigned i=0; i<addr_num; i++) {
        bool found = false;
        for (const auto& ip : ipList)
            if (!pj_sockaddr_cmp(&ip, &res[i].ai_addr)) {
                found = true;
                break;
            }
        if (!found)
            ipList.push_back(res[i].ai_addr);
    }

    return ipList;
}

111
bool
Adrien Béraud's avatar
Adrien Béraud committed
112
ip_utils::haveCommonAddr(const std::vector<IpAddr>& a, const std::vector<IpAddr>& b)
113 114 115
{
    for (const auto &i : a) {
        for (const auto &j : b) {
Adrien Béraud's avatar
Adrien Béraud committed
116
            if (i == j) return true;
117 118 119 120 121
        }
    }
    return false;
}

Adrien Béraud's avatar
Adrien Béraud committed
122
IpAddr
123 124
ip_utils::getLocalAddr(pj_uint16_t family)
{
125
    IpAddr ip_addr {};
Adrien Béraud's avatar
Adrien Béraud committed
126 127 128 129
    pj_status_t status = pj_gethostip(family, ip_addr.pjPtr());
    if (status == PJ_SUCCESS) {
        return ip_addr;
    }
Adrien Béraud's avatar
Adrien Béraud committed
130
    JAMI_WARN("Could not get preferred address familly (%s)", (family == pj_AF_INET6()) ? "IPv6" : "IPv4");
131
    family = (family == pj_AF_INET()) ? pj_AF_INET6() : pj_AF_INET();
132
    status = pj_gethostip(family, ip_addr.pjPtr());
133 134 135
    if (status == PJ_SUCCESS) {
        return ip_addr;
    }
Adrien Béraud's avatar
Adrien Béraud committed
136
    JAMI_ERR("Could not get local IP");
137 138 139
    return ip_addr;
}

Adrien Béraud's avatar
Adrien Béraud committed
140
IpAddr
141 142 143 144
ip_utils::getInterfaceAddr(const std::string &interface, pj_uint16_t family)
{
    if (interface == DEFAULT_INTERFACE)
        return getLocalAddr(family);
Tristan Matthews's avatar
Tristan Matthews committed
145

146
    IpAddr addr {};
Tristan Matthews's avatar
Tristan Matthews committed
147

Edric Milaret's avatar
Edric Milaret committed
148
#ifndef _WIN32
149 150
    const auto unix_family = family == pj_AF_INET() ? AF_INET : AF_INET6;

151
    int fd = socket(unix_family, SOCK_DGRAM, 0);
Tristan Matthews's avatar
Tristan Matthews committed
152
    if (fd < 0) {
Adrien Béraud's avatar
Adrien Béraud committed
153
        JAMI_ERR("Could not open socket: %m");
Adrien Béraud's avatar
Adrien Béraud committed
154
        return addr;
155
    }
Tristan Matthews's avatar
Tristan Matthews committed
156 157 158

    if (unix_family == AF_INET6) {
        int val = family != pj_AF_UNSPEC();
159
        if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, (void *) &val, sizeof(val)) < 0) {
Adrien Béraud's avatar
Adrien Béraud committed
160
            JAMI_ERR("Could not setsockopt: %m");
Tristan Matthews's avatar
Tristan Matthews committed
161
            close(fd);
Adrien Béraud's avatar
Adrien Béraud committed
162
            return addr;
Tristan Matthews's avatar
Tristan Matthews committed
163
        }
164
    }
Tristan Matthews's avatar
Tristan Matthews committed
165

166 167 168 169 170 171 172 173 174 175 176
    ifreq ifr;
    strncpy(ifr.ifr_name, interface.c_str(), sizeof ifr.ifr_name);
    // guarantee that ifr_name is NULL-terminated
    ifr.ifr_name[sizeof(ifr.ifr_name) - 1] = '\0';

    memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr));
    ifr.ifr_addr.sa_family = unix_family;

    ioctl(fd, SIOCGIFADDR, &ifr);
    close(fd);

Adrien Béraud's avatar
Adrien Béraud committed
177 178 179
    addr = ifr.ifr_addr;
    if (addr.isUnspecified())
        return getLocalAddr(addr.getFamily());
Edric Milaret's avatar
Edric Milaret committed
180 181 182 183 184 185 186 187 188 189
#else // _WIN32
    struct addrinfo hints;
    struct addrinfo *result = NULL;
    struct sockaddr_in  *sockaddr_ipv4;
    struct sockaddr_in6 *sockaddr_ipv6;

    ZeroMemory(&hints, sizeof(hints));

    DWORD dwRetval = getaddrinfo(interface.c_str(), "0", &hints, &result);
    if (dwRetval != 0) {
Adrien Béraud's avatar
Adrien Béraud committed
190
        JAMI_ERR("getaddrinfo failed with error: %lu", dwRetval);
Edric Milaret's avatar
Edric Milaret committed
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
        return addr;
    }

    switch (result->ai_family) {
        sockaddr_ipv4 = (struct sockaddr_in *) result->ai_addr;
        addr = sockaddr_ipv4->sin_addr;
        break;
        case AF_INET6:
        sockaddr_ipv6 = (struct sockaddr_in6 *) result->ai_addr;
        addr = sockaddr_ipv6->sin6_addr;
        break;
        default:
        break;
    }

    if (addr.isUnspecified())
            return getLocalAddr(addr.getFamily());
#endif // !_WIN32
209

Adrien Béraud's avatar
Adrien Béraud committed
210
    return addr;
211 212 213 214 215 216 217
}

std::vector<std::string>
ip_utils::getAllIpInterfaceByName()
{
    std::vector<std::string> ifaceList;
    ifaceList.push_back("default");
Edric Milaret's avatar
Edric Milaret committed
218 219 220
#ifndef _WIN32
    static ifreq ifreqs[20];
    ifconf ifconf;
221 222 223 224 225 226 227 228 229 230 231 232 233 234

    ifconf.ifc_buf = (char*) (ifreqs);
    ifconf.ifc_len = sizeof(ifreqs);

    int sock = socket(AF_INET6, SOCK_STREAM, 0);

    if (sock >= 0) {
        if (ioctl(sock, SIOCGIFCONF, &ifconf) >= 0)
            for (unsigned i = 0; i < ifconf.ifc_len / sizeof(ifreq); ++i)
                ifaceList.push_back(std::string(ifreqs[i].ifr_name));

        close(sock);
    }

Edric Milaret's avatar
Edric Milaret committed
235
#else
Adrien Béraud's avatar
Adrien Béraud committed
236
        JAMI_ERR("Not implemented yet. (iphlpapi.h problem)");
Edric Milaret's avatar
Edric Milaret committed
237
#endif
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
    return ifaceList;
}

std::vector<std::string>
ip_utils::getAllIpInterface()
{
    pj_sockaddr addrList[16];
    unsigned addrCnt = PJ_ARRAY_SIZE(addrList);

    std::vector<std::string> ifaceList;

    if (pj_enum_ip_interface(pj_AF_UNSPEC(), &addrCnt, addrList) == PJ_SUCCESS) {
        for (unsigned i = 0; i < addrCnt; i++) {
            char addr[PJ_INET6_ADDRSTRLEN];
            pj_sockaddr_print(&addrList[i], addr, sizeof(addr), 0);
            ifaceList.push_back(std::string(addr));
        }
    }

    return ifaceList;
}

260 261 262
std::vector<IpAddr>
ip_utils::getLocalNameservers()
{
263
    std::vector<IpAddr> res;
Edric Milaret's avatar
Edric Milaret committed
264
#if defined __ANDROID__ || defined _WIN32 || TARGET_OS_IPHONE
265 266 267 268
#ifdef _MSC_VER
#pragma message (__FILE__ "(" STR2(__LINE__) ") : -NOTE- " "Not implemented")
#else
#warning "Not implemented"
269
#endif
270
#else
271 272 273
    if (not (_res.options & RES_INIT))
        res_init();
    res.insert(res.end(), _res.nsaddr_list, _res.nsaddr_list + _res.nscount);
274
#endif
275 276 277
    return res;
}

278
bool
Adrien Béraud's avatar
Adrien Béraud committed
279
IpAddr::isValid(const std::string &address, pj_uint16_t family)
280
{
281
    const pj_str_t pjstring(sip_utils::CONST_PJ_STR(address));
282 283 284 285 286 287 288 289 290 291 292 293 294
    pj_str_t ret_str;
    pj_uint16_t ret_port;
    int ret_family;
    auto status = pj_sockaddr_parse2(pj_AF_UNSPEC(), 0, &pjstring, &ret_str, &ret_port, &ret_family);
    if (status != PJ_SUCCESS || (family != pj_AF_UNSPEC() && ret_family != family))
        return false;

    char buf[PJ_INET6_ADDRSTRLEN];
    pj_str_t addr_with_null = {buf, 0};
    pj_strncpy_with_null(&addr_with_null, &ret_str, sizeof(buf));
    struct sockaddr sa;
    return inet_pton(ret_family==pj_AF_INET6()?AF_INET6:AF_INET, buf, &(sa.sa_data)) == 1;
}
Adrien Béraud's avatar
Adrien Béraud committed
295 296 297 298 299 300

bool
IpAddr::isUnspecified() const
{
    switch (addr.addr.sa_family) {
    case AF_INET:
301
        return IN_IS_ADDR_UNSPECIFIED(&addr.ipv4.sin_addr);
Adrien Béraud's avatar
Adrien Béraud committed
302
    case AF_INET6:
303
        return IN6_IS_ADDR_UNSPECIFIED(reinterpret_cast<const in6_addr*>(&addr.ipv6.sin6_addr));
Adrien Béraud's avatar
Adrien Béraud committed
304 305 306 307 308 309 310 311 312 313
    default:
        return true;
    }
}

bool
IpAddr::isLoopback() const
{
    switch (addr.addr.sa_family) {
    case AF_INET: {
314 315
        auto addr_host = ntohl(addr.ipv4.sin_addr.s_addr);
        uint8_t b1 = (uint8_t)(addr_host >> 24);
Adrien Béraud's avatar
Adrien Béraud committed
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
        return b1 == 127;
    }
    case AF_INET6:
        return IN6_IS_ADDR_LOOPBACK(reinterpret_cast<const in6_addr*>(&addr.ipv6.sin6_addr));
    default:
        return false;
    }
}

bool
IpAddr::isPrivate() const
{
    if (isLoopback()) {
        return true;
    }
    switch (addr.addr.sa_family) {
332 333
    case AF_INET: {
        auto addr_host = ntohl(addr.ipv4.sin_addr.s_addr);
Adrien Béraud's avatar
Adrien Béraud committed
334
        uint8_t b1, b2;
335 336
        b1 = (uint8_t)(addr_host >> 24);
        b2 = (uint8_t)((addr_host >> 16) & 0x0ff);
Adrien Béraud's avatar
Adrien Béraud committed
337 338 339 340 341 342 343 344 345 346
        // 10.x.y.z
        if (b1 == 10)
            return true;
        // 172.16.0.0 - 172.31.255.255
        if ((b1 == 172) && (b2 >= 16) && (b2 <= 31))
            return true;
        // 192.168.0.0 - 192.168.255.255
        if ((b1 == 192) && (b2 == 168))
            return true;
        return false;
347
    }
Adrien Béraud's avatar
Adrien Béraud committed
348 349 350 351 352 353 354 355 356 357
    case AF_INET6: {
        const pj_uint8_t* addr6 = reinterpret_cast<const pj_uint8_t*>(&addr.ipv6.sin6_addr);
        if (addr6[0] == 0xfc)
            return true;
        return false;
    }
    default:
        return false;
    }
}
358

Adrien Béraud's avatar
Adrien Béraud committed
359
} // namespace jami