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::net {
21 
22 static nsresult ListInterfaceAddresses(int aFd, const char* aIface,
23                                        AddrMapType& aAddrMap);
24 
DoListAddresses(AddrMapType & aAddrMap)25 nsresult DoListAddresses(AddrMapType& aAddrMap) {
26   int fd = socket(AF_INET, SOCK_DGRAM, 0);
27   if (fd < 0) {
28     return NS_ERROR_FAILURE;
29   }
30 
31   auto autoCloseSocket = MakeScopeExit([&] { close(fd); });
32 
33   struct ifconf ifconf;
34   /* 16k of space should be enough to list all interfaces.  Worst case, if it's
35    * not then we will error out and fail to list addresses.  This should only
36    * happen on pathological machines with way too many interfaces.
37    */
38   char buf[16384];
39 
40   ifconf.ifc_len = sizeof(buf);
41   ifconf.ifc_buf = buf;
42   if (ioctl(fd, SIOCGIFCONF, &ifconf) != 0) {
43     return NS_ERROR_FAILURE;
44   }
45 
46   struct ifreq* ifreq = ifconf.ifc_req;
47   int i = 0;
48   while (i < ifconf.ifc_len) {
49     size_t len = sizeof(struct ifreq);
50 
51     DebugOnly<nsresult> rv =
52         ListInterfaceAddresses(fd, ifreq->ifr_name, aAddrMap);
53     NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "ListInterfaceAddresses failed");
54 
55     ifreq = (struct ifreq*)((char*)ifreq + len);
56     i += len;
57   }
58 
59   return NS_OK;
60 }
61 
ListInterfaceAddresses(int aFd,const char * aInterface,AddrMapType & aAddrMap)62 static nsresult ListInterfaceAddresses(int aFd, const char* aInterface,
63                                        AddrMapType& aAddrMap) {
64   struct ifreq ifreq;
65   memset(&ifreq, 0, sizeof(struct ifreq));
66   strncpy(ifreq.ifr_name, aInterface, IFNAMSIZ - 1);
67   if (ioctl(aFd, SIOCGIFADDR, &ifreq) != 0) {
68     return NS_ERROR_FAILURE;
69   }
70 
71   char host[128];
72   switch (ifreq.ifr_addr.sa_family) {
73     case AF_INET:
74     case AF_INET6:
75       getnameinfo(&ifreq.ifr_addr, sizeof(ifreq.ifr_addr), host, sizeof(host),
76                   nullptr, 0, NI_NUMERICHOST);
77       break;
78     case AF_UNSPEC:
79       return NS_OK;
80     default:
81       // Unknown family.
82       return NS_OK;
83   }
84 
85   nsCString ifaceStr;
86   ifaceStr.AssignASCII(aInterface);
87 
88   nsCString addrStr;
89   addrStr.AssignASCII(host);
90 
91   aAddrMap.Put(ifaceStr, addrStr);
92 
93   return NS_OK;
94 }
95 
96 }  // namespace mozilla::net
97