LCOV - code coverage report
Current view: top level - netwerk/base - NetworkInfoServiceLinux.cpp (source / functions) Hit Total Coverage
Test: output.info Lines: 0 37 0.0 %
Date: 2017-07-14 16:53:18 Functions: 0 3 0.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.13