Back to index

lshw  02.16
Classes | Defines | Typedefs | Functions
network.cc File Reference
#include "version.h"
#include "config.h"
#include "network.h"
#include "osutils.h"
#include "sysfs.h"
#include "options.h"
#include "heuristics.h"
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <net/if_arp.h>
#include <linux/sockios.h>
#include <net/if.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <string>
#include <sys/types.h>

Go to the source code of this file.

Classes

struct  ethtool_cmd
struct  ethtool_drvinfo
struct  ethtool_value

Defines

#define ARPHRD_IEEE1394   24
#define ARPHRD_SIT   776
#define SIOCETHTOOL   0x8946
#define IFNAMSIZ   32
#define SIOCGIWNAME   0x8B01 /* get name == wireless protocol */
#define ETHTOOL_BUSINFO_LEN   32
#define ETHTOOL_GSET   0x00000001 /* Get settings. */
#define ETHTOOL_GDRVINFO   0x00000003 /* Get driver info. */
#define ETHTOOL_GLINK   0x0000000a /* Get link status (ethtool_value) */
#define SUPPORTED_10baseT_Half   (1 << 0)
#define SUPPORTED_10baseT_Full   (1 << 1)
#define SUPPORTED_100baseT_Half   (1 << 2)
#define SUPPORTED_100baseT_Full   (1 << 3)
#define SUPPORTED_1000baseT_Half   (1 << 4)
#define SUPPORTED_1000baseT_Full   (1 << 5)
#define SUPPORTED_Autoneg   (1 << 6)
#define SUPPORTED_TP   (1 << 7)
#define SUPPORTED_AUI   (1 << 8)
#define SUPPORTED_MII   (1 << 9)
#define SUPPORTED_FIBRE   (1 << 10)
#define SUPPORTED_BNC   (1 << 11)
#define SUPPORTED_10000baseT_Full   (1 << 12)
#define SPEED_10   10
#define SPEED_100   100
#define SPEED_1000   1000
#define SPEED_10000   10000
#define DUPLEX_HALF   0x00
#define DUPLEX_FULL   0x01
#define PORT_TP   0x00
#define PORT_AUI   0x01
#define PORT_MII   0x02
#define PORT_FIBRE   0x03
#define PORT_BNC   0x04
#define XCVR_INTERNAL   0x00
#define XCVR_EXTERNAL   0x01
#define XCVR_DUMMY1   0x02
#define XCVR_DUMMY2   0x03
#define XCVR_DUMMY3   0x04
#define AUTONEG_DISABLE   0x00
#define AUTONEG_ENABLE   0x01

Typedefs

typedef unsigned long long u64
typedef __uint32_t u32
typedef __uint16_t u16
typedef __uint8_t u8

Functions

 __ID ("@(#) $Id: network.cc 2433 2012-01-10 22:01:30Z lyonel $")
bool load_interfaces (vector< string > &interfaces)
static string getmac (const unsigned char *mac)
static const char * hwname (int t)
static string print_ip (struct sockaddr_in *in)
static void scan_ip (hwNode &interface)
static bool isVirtual (const string &MAC)
bool scan_network (hwNode &n)

Class Documentation

struct ethtool_cmd

Definition at line 56 of file network.cc.

Class Members
u32 advertising
u8 autoneg
u32 cmd
u8 duplex
u32 maxrxpkt
u32 maxtxpkt
u8 phy_address
u8 port
u32 reserved
u16 speed
u32 supported
u8 transceiver
struct ethtool_drvinfo

Definition at line 81 of file network.cc.

Class Members
char bus_info
u32 cmd
char driver
u32 eedump_len
char fw_version
u32 n_stats
u32 regdump_len
char reserved1
char reserved2
u32 testinfo_len
char version
struct ethtool_value

Definition at line 100 of file network.cc.

Class Members
u32 cmd
u32 data

Define Documentation

#define ARPHRD_IEEE1394   24

Definition at line 42 of file network.cc.

#define ARPHRD_SIT   776

Definition at line 45 of file network.cc.

#define AUTONEG_DISABLE   0x00

Definition at line 150 of file network.cc.

#define AUTONEG_ENABLE   0x01

Definition at line 151 of file network.cc.

#define DUPLEX_FULL   0x01

Definition at line 134 of file network.cc.

#define DUPLEX_HALF   0x00

Definition at line 133 of file network.cc.

#define ETHTOOL_BUSINFO_LEN   32

Definition at line 79 of file network.cc.

#define ETHTOOL_GDRVINFO   0x00000003 /* Get driver info. */

Definition at line 108 of file network.cc.

#define ETHTOOL_GLINK   0x0000000a /* Get link status (ethtool_value) */

Definition at line 109 of file network.cc.

#define ETHTOOL_GSET   0x00000001 /* Get settings. */

Definition at line 107 of file network.cc.

#define IFNAMSIZ   32

Definition at line 73 of file network.cc.

#define PORT_AUI   0x01

Definition at line 138 of file network.cc.

#define PORT_BNC   0x04

Definition at line 141 of file network.cc.

#define PORT_FIBRE   0x03

Definition at line 140 of file network.cc.

#define PORT_MII   0x02

Definition at line 139 of file network.cc.

#define PORT_TP   0x00

Definition at line 137 of file network.cc.

#define SIOCETHTOOL   0x8946

Definition at line 49 of file network.cc.

#define SIOCGIWNAME   0x8B01 /* get name == wireless protocol */

Definition at line 75 of file network.cc.

#define SPEED_10   10

Definition at line 127 of file network.cc.

#define SPEED_100   100

Definition at line 128 of file network.cc.

#define SPEED_1000   1000

Definition at line 129 of file network.cc.

#define SPEED_10000   10000

Definition at line 130 of file network.cc.

#define SUPPORTED_10000baseT_Full   (1 << 12)

Definition at line 124 of file network.cc.

#define SUPPORTED_1000baseT_Full   (1 << 5)

Definition at line 117 of file network.cc.

#define SUPPORTED_1000baseT_Half   (1 << 4)

Definition at line 116 of file network.cc.

#define SUPPORTED_100baseT_Full   (1 << 3)

Definition at line 115 of file network.cc.

#define SUPPORTED_100baseT_Half   (1 << 2)

Definition at line 114 of file network.cc.

#define SUPPORTED_10baseT_Full   (1 << 1)

Definition at line 113 of file network.cc.

#define SUPPORTED_10baseT_Half   (1 << 0)

Definition at line 112 of file network.cc.

#define SUPPORTED_AUI   (1 << 8)

Definition at line 120 of file network.cc.

#define SUPPORTED_Autoneg   (1 << 6)

Definition at line 118 of file network.cc.

#define SUPPORTED_BNC   (1 << 11)

Definition at line 123 of file network.cc.

#define SUPPORTED_FIBRE   (1 << 10)

Definition at line 122 of file network.cc.

#define SUPPORTED_MII   (1 << 9)

Definition at line 121 of file network.cc.

#define SUPPORTED_TP   (1 << 7)

Definition at line 119 of file network.cc.

#define XCVR_DUMMY1   0x02

Definition at line 146 of file network.cc.

#define XCVR_DUMMY2   0x03

Definition at line 147 of file network.cc.

#define XCVR_DUMMY3   0x04

Definition at line 148 of file network.cc.

#define XCVR_EXTERNAL   0x01

Definition at line 145 of file network.cc.

#define XCVR_INTERNAL   0x00

Definition at line 144 of file network.cc.


Typedef Documentation

typedef __uint16_t u16

Definition at line 53 of file network.cc.

typedef __uint32_t u32

Definition at line 52 of file network.cc.

typedef unsigned long long u64

Definition at line 51 of file network.cc.

typedef __uint8_t u8

Definition at line 54 of file network.cc.


Function Documentation

__ID ( "@(#) $Id: network.cc 2433 2012-01-10 22:01:30Z lyonel $"  )
static string getmac ( const unsigned char *  mac) [static]

Definition at line 181 of file network.cc.

{
  char buff[5];
  string result = "";
  bool valid = false;

  for (int i = 0; i < 6; i++)
  {
    snprintf(buff, sizeof(buff), "%02x", mac[i]);

    valid |= (mac[i] != 0);

    if (i == 0)
      result = string(buff);
    else
      result += ":" + string(buff);
  }

  if (valid)
    return result;
  else
    return "";
}

Here is the caller graph for this function:

static const char* hwname ( int  t) [static]

Definition at line 206 of file network.cc.

{
  switch (t)
  {
    case ARPHRD_ETHER:
      return _("Ethernet");
    case ARPHRD_SLIP:
      return _("SLIP");
    case ARPHRD_LOOPBACK:
      return _("loopback");
    case ARPHRD_FDDI:
      return _("FDDI");
    case ARPHRD_IEEE1394:
      return _("IEEE1394");
    case ARPHRD_IRDA:
      return _("IRDA");
    case ARPHRD_PPP:
      return _("PPP");
    case ARPHRD_X25:
      return _("X25");
    case ARPHRD_TUNNEL:
      return _("IPtunnel");
    case ARPHRD_DLCI:
      return _("Framerelay.DLCI");
    case ARPHRD_FRAD:
      return _("Framerelay.AD");
    case ARPHRD_TUNNEL6:
      return _("IP6tunnel");
    case ARPHRD_SIT:
      return _("IP6inIP4");
    default:
      return "";
  }
}

Here is the caller graph for this function:

static bool isVirtual ( const string &  MAC) [static]

Definition at line 279 of file network.cc.

{
  if (MAC.length() < 8)
    return false;

  string manufacturer = uppercase(MAC.substr(0, 8));

  if ((manufacturer == "00:05:69") ||
    (manufacturer == "00:0C:29") || (manufacturer == "00:50:56"))
    return true;     // VMware
  if (manufacturer == "00:1C:42")
    return true;     // Parallels
  if (manufacturer == "0A:00:27")
    return true;     // VirtualBox

  return false;
}

Here is the call graph for this function:

Here is the caller graph for this function:

bool load_interfaces ( vector< string > &  interfaces)

Definition at line 153 of file network.cc.

{
  vector < string > procnetdev;

  interfaces.clear();
  if (!loadfile("/proc/net/dev", procnetdev))
    return false;

  if (procnetdev.size() <= 2)
    return false;

// get rid of header (2 lines)
  procnetdev.erase(procnetdev.begin());
  procnetdev.erase(procnetdev.begin());

  for (unsigned int i = 0; i < procnetdev.size(); i++)
  {
// extract interfaces names
    size_t pos = procnetdev[i].find(':');

    if (pos != string::npos)
      interfaces.push_back(hw::strip(procnetdev[i].substr(0, pos)));
  }

  return true;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static string print_ip ( struct sockaddr_in *  in) [static]

Definition at line 242 of file network.cc.

{
  return string(inet_ntoa(in->sin_addr));
}

Here is the caller graph for this function:

static void scan_ip ( hwNode interface) [static]

Definition at line 248 of file network.cc.

{
  int fd = socket(AF_INET, SOCK_DGRAM, 0);

  if (fd > 0)
  {
    struct ifreq ifr;

// get IP address
    memset(&ifr, 0, sizeof(ifr));
    strcpy(ifr.ifr_name, interface.getLogicalName().c_str());
    ifr.ifr_addr.sa_family = AF_INET;
    if (ioctl(fd, SIOCGIFADDR, &ifr) == 0)
    {
// IP address is in ifr.ifr_addr
      interface.setConfig("ip", ::enabled("output:sanitize")?REMOVED:print_ip((sockaddr_in *) (&ifr.ifr_addr)));
      strcpy(ifr.ifr_name, interface.getLogicalName().c_str());
      if ((interface.getConfig("point-to-point") == "yes")
        && (ioctl(fd, SIOCGIFDSTADDR, &ifr) == 0))
      {
// remote PPP address is in ifr.ifr_dstaddr
        interface.setConfig("remoteip",
          print_ip((sockaddr_in *) & ifr.ifr_dstaddr));
      }
    }

    close(fd);
  }
}

Here is the call graph for this function:

Here is the caller graph for this function:

bool scan_network ( hwNode n)

Definition at line 298 of file network.cc.

{
  vector < string > interfaces;
  char buffer[2 * IFNAMSIZ + 1];

  if (!load_interfaces(interfaces))
    return false;

  int fd = socket(PF_INET, SOCK_DGRAM, 0);

  if (fd >= 0)
  {
    struct ifreq ifr;
    struct ethtool_drvinfo drvinfo;
    struct ethtool_cmd ecmd;
    struct ethtool_value edata;

    for (unsigned int i = 0; i < interfaces.size(); i++)
    {
      hwNode interface("network",
        hw::network);

      interface.setLogicalName(interfaces[i]);
      interface.claim();
      interface.addHint("icon", string("network"));

      string businfo = sysfs_getbusinfo(sysfs::entry::byClass("net", interface.getLogicalName()));
      interface.setBusInfo(businfo);

//scan_mii(fd, interface);
      scan_ip(interface);

      memset(&ifr, 0, sizeof(ifr));
      strcpy(ifr.ifr_name, interfaces[i].c_str());
      if (ioctl(fd, SIOCGIFFLAGS, &ifr) == 0)
      {
#ifdef IFF_PORTSEL
        if (ifr.ifr_flags & IFF_PORTSEL)
        {
          if (ifr.ifr_flags & IFF_AUTOMEDIA)
            interface.setConfig("automedia", "yes");
        }
#endif

        if (ifr.ifr_flags & IFF_UP)
          interface.enable();
        else
          interface.disable();
        if (ifr.ifr_flags & IFF_BROADCAST)
          interface.setConfig("broadcast", "yes");
        if (ifr.ifr_flags & IFF_DEBUG)
          interface.setConfig("debug", "yes");
        if (ifr.ifr_flags & IFF_LOOPBACK)
          interface.setConfig("loopback", "yes");
        if (ifr.ifr_flags & IFF_POINTOPOINT)
          interface.setConfig("point-to-point", "yes");
        if (ifr.ifr_flags & IFF_PROMISC)
          interface.setConfig("promiscuous", "yes");
        if (ifr.ifr_flags & IFF_SLAVE)
          interface.setConfig("slave", "yes");
        if (ifr.ifr_flags & IFF_MASTER)
          interface.setConfig("master", "yes");
        if (ifr.ifr_flags & IFF_MULTICAST)
          interface.setConfig("multicast", "yes");
      }

      memset(&ifr, 0, sizeof(ifr));
      strcpy(ifr.ifr_name, interfaces[i].c_str());
// get MAC address
      if (ioctl(fd, SIOCGIFHWADDR, &ifr) == 0)
      {
        string hwaddr = getmac((unsigned char *) ifr.ifr_hwaddr.sa_data);
        interface.addCapability(hwname(ifr.ifr_hwaddr.sa_family));
        if (ifr.ifr_hwaddr.sa_family >= 256)
          interface.addCapability("logical", _("Logical interface"));
        else
          interface.addCapability("physical", _("Physical interface"));
        interface.setDescription(string(hwname(ifr.ifr_hwaddr.sa_family)) +
          " interface");
        interface.setSerial(hwaddr);

        if (isVirtual(interface.getSerial()))
          interface.addCapability("logical", _("Logical interface"));
      }

// check for wireless extensions
      memset(buffer, 0, sizeof(buffer));
      strncpy(buffer, interfaces[i].c_str(), sizeof(buffer));
      if (ioctl(fd, SIOCGIWNAME, &buffer) == 0)
      {
        interface.addCapability("wireless", _("Wireless-LAN"));
        interface.setConfig("wireless", hw::strip(buffer + IFNAMSIZ));
        interface.setDescription(_("Wireless interface"));
        interface.addHint("icon", string("wifi"));
        interface.addHint("bus.icon", string("radio"));
      }

      edata.cmd = ETHTOOL_GLINK;
      memset(&ifr, 0, sizeof(ifr));
      strcpy(ifr.ifr_name, interfaces[i].c_str());
      ifr.ifr_data = (caddr_t) &edata;
      if (ioctl(fd, SIOCETHTOOL, &ifr) == 0)
      {
        interface.setConfig("link", edata.data ? "yes":"no");
      }

      ecmd.cmd = ETHTOOL_GSET;
      memset(&ifr, 0, sizeof(ifr));
      strcpy(ifr.ifr_name, interfaces[i].c_str());
      ifr.ifr_data = (caddr_t) &ecmd;
      if (ioctl(fd, SIOCETHTOOL, &ifr) == 0)
      {
        if(ecmd.supported & SUPPORTED_TP)
          interface.addCapability("tp", _("twisted pair"));
        if(ecmd.supported & SUPPORTED_AUI)
          interface.addCapability("aui", _("AUI"));
        if(ecmd.supported & SUPPORTED_BNC)
          interface.addCapability("bnc", _("BNC"));
        if(ecmd.supported & SUPPORTED_MII)
          interface.addCapability("mii", _("Media Independant Interface"));
        if(ecmd.supported & SUPPORTED_FIBRE)
          interface.addCapability("fibre",_( "optical fibre"));
        if(ecmd.supported & SUPPORTED_10baseT_Half)
        {
          interface.addCapability("10bt", _("10Mbit/s"));
          interface.setCapacity(10000000L);
        }
        if(ecmd.supported & SUPPORTED_10baseT_Full)
        {
          interface.addCapability("10bt-fd", _("10Mbit/s (full duplex)"));
          interface.setCapacity(10000000L);
        }
        if(ecmd.supported & SUPPORTED_100baseT_Half)
        {
          interface.addCapability("100bt", _("100Mbit/s"));
          interface.setCapacity(100000000L);
        }
        if(ecmd.supported & SUPPORTED_100baseT_Full)
        {
          interface.addCapability("100bt-fd", _("100Mbit/s (full duplex)"));
          interface.setCapacity(100000000L);
        }
        if(ecmd.supported & SUPPORTED_1000baseT_Half)
        {
          interface.addCapability("1000bt", "1Gbit/s");
          interface.setCapacity(1000000000L);
        }
        if(ecmd.supported & SUPPORTED_1000baseT_Full)
        {
          interface.addCapability("1000bt-fd", _("1Gbit/s (full duplex)"));
          interface.setCapacity(1000000000L);
        }
        if(ecmd.supported & SUPPORTED_Autoneg)
          interface.addCapability("autonegotiation", _("Auto-negotiation"));

        switch(ecmd.speed)
        {
          case SPEED_10:
            interface.setConfig("speed", "10Mbit/s");
            interface.setSize(10000000L);
            break;
          case SPEED_100:
            interface.setConfig("speed", "100Mbit/s");
            interface.setSize(100000000L);
            break;
          case SPEED_1000:
            interface.setConfig("speed", "1Gbit/s");
            interface.setSize(1000000000L);
            break;
        }
        switch(ecmd.duplex)
        {
          case DUPLEX_HALF:
            interface.setConfig("duplex", "half");
            break;
          case DUPLEX_FULL:
            interface.setConfig("duplex", "full");
            break;
        }
        switch(ecmd.port)
        {
          case PORT_TP:
            interface.setConfig("port", "twisted pair");
            break;
          case PORT_AUI:
            interface.setConfig("port", "AUI");
            break;
          case PORT_BNC:
            interface.setConfig("port", "BNC");
            break;
          case PORT_MII:
            interface.setConfig("port", "MII");
            break;
          case PORT_FIBRE:
            interface.setConfig("port", "fibre");
            break;
        }
        interface.setConfig("autonegotiation", (ecmd.autoneg == AUTONEG_DISABLE) ?  "off" : "on");
      }

      drvinfo.cmd = ETHTOOL_GDRVINFO;
      memset(&ifr, 0, sizeof(ifr));
      strcpy(ifr.ifr_name, interfaces[i].c_str());
      ifr.ifr_data = (caddr_t) & drvinfo;
      if (ioctl(fd, SIOCETHTOOL, &ifr) == 0)
      {
        interface.setConfig("driver", drvinfo.driver);
        interface.setConfig("driverversion", drvinfo.version);
        interface.setConfig("firmware", drvinfo.fw_version);
        if (interface.getBusInfo() == "")
          interface.setBusInfo(guessBusInfo(drvinfo.bus_info));
      }

      if(sysfs::entry::byClass("net", interface.getLogicalName()).hassubdir("bridge"))
        interface.addCapability("logical", _("Logical interface"));

      if (hwNode * existing = n.findChildByBusInfo(interface.getBusInfo()))
      {
        existing->merge(interface);
        if(interface.getDescription()!="")
          existing->setDescription(interface.getDescription());
      }
      else
      {
        existing = n.findChildByLogicalName(interface.getLogicalName());
        if (existing)
        {
          existing->merge(interface);
        }
        else
        {
// we don't care about loopback and "logical" interfaces
          if (!interface.isCapable("loopback") &&
            !interface.isCapable("logical"))
            n.addChild(interface);
        }
      }
    }

    close(fd);
    return true;
  }
  else
    return false;
}

Here is the call graph for this function:

Here is the caller graph for this function: