Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/netwerk/base/NetworkInfoServiceLinux.cpp
Line
Count
Source (jump to first uncovered line)
1
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
3
/* This Source Code Form is subject to the terms of the Mozilla Public
4
 * License, v. 2.0. If a copy of the MPL was not distributed with this
5
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7
#include <string.h>
8
#include <unistd.h>
9
#include <sys/ioctl.h>
10
#include <sys/socket.h>
11
#include <sys/types.h>
12
#include <net/if.h>
13
#include <netdb.h>
14
15
#include "mozilla/DebugOnly.h"
16
#include "mozilla/ScopeExit.h"
17
18
#include "NetworkInfoServiceImpl.h"
19
20
namespace mozilla {
21
namespace net {
22
23
static nsresult
24
ListInterfaceAddresses(int aFd, const char* aIface, AddrMapType& aAddrMap);
25
26
nsresult
27
DoListAddresses(AddrMapType& aAddrMap)
28
0
{
29
0
    int fd = socket(AF_INET, SOCK_DGRAM, 0);
30
0
    if (fd < 0) {
31
0
        return NS_ERROR_FAILURE;
32
0
    }
33
0
34
0
    auto autoCloseSocket = MakeScopeExit([&] {
35
0
      close(fd);
36
0
    });
37
0
38
0
    struct ifconf ifconf;
39
0
    /* 16k of space should be enough to list all interfaces.  Worst case, if it's
40
0
     * not then we will error out and fail to list addresses.  This should only
41
0
     * happen on pathological machines with way too many interfaces.
42
0
     */
43
0
    char buf[16384];
44
0
45
0
    ifconf.ifc_len = sizeof(buf);
46
0
    ifconf.ifc_buf = buf;
47
0
    if (ioctl(fd, SIOCGIFCONF, &ifconf) != 0) {
48
0
        return NS_ERROR_FAILURE;
49
0
    }
50
0
51
0
    struct ifreq* ifreq = ifconf.ifc_req;
52
0
    int i = 0;
53
0
    while (i < ifconf.ifc_len) {
54
0
        size_t len = sizeof(struct ifreq);
55
0
56
0
        DebugOnly<nsresult> rv =
57
0
          ListInterfaceAddresses(fd, ifreq->ifr_name, aAddrMap);
58
0
        NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "ListInterfaceAddresses failed");
59
0
60
0
        ifreq = (struct ifreq*) ((char*)ifreq + len);
61
0
        i += len;
62
0
    }
63
0
64
0
    return NS_OK;
65
0
}
66
67
static nsresult
68
ListInterfaceAddresses(int aFd, const char* aInterface, AddrMapType& aAddrMap)
69
0
{
70
0
    struct ifreq ifreq;
71
0
    memset(&ifreq, 0, sizeof(struct ifreq));
72
0
    strncpy(ifreq.ifr_name, aInterface, IFNAMSIZ - 1);
73
0
    if (ioctl(aFd, SIOCGIFADDR, &ifreq) != 0) {
74
0
        return NS_ERROR_FAILURE;
75
0
    }
76
0
77
0
    char host[128];
78
0
    int family;
79
0
    switch(family=ifreq.ifr_addr.sa_family) {
80
0
      case AF_INET:
81
0
      case AF_INET6:
82
0
        getnameinfo(&ifreq.ifr_addr, sizeof(ifreq.ifr_addr), host, sizeof(host), nullptr, 0, NI_NUMERICHOST);
83
0
        break;
84
0
      case AF_UNSPEC:
85
0
        return NS_OK;
86
0
      default:
87
0
        // Unknown family.
88
0
        return NS_OK;
89
0
    }
90
0
91
0
    nsCString ifaceStr;
92
0
    ifaceStr.AssignASCII(aInterface);
93
0
94
0
    nsCString addrStr;
95
0
    addrStr.AssignASCII(host);
96
0
97
0
    aAddrMap.Put(ifaceStr, addrStr);
98
0
99
0
    return NS_OK;
100
0
}
101
102
} // namespace net
103
} // namespace mozilla