Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect network connection type in Linux

How can I detect the network connection type, e.g., whether it is wired or Wi-Fi, in a C++ Linux application?

If the device has multiple network interfaces, I would like to detect the connection type for the interface being used.

Thanks.

like image 914
derekwei Avatar asked Dec 17 '10 22:12

derekwei


People also ask

How do I see network connections in Linux?

netstat command – It is used to display network connections, routing tables, interface statistics, masquerade connections, and multicast memberships. ifconfig command – It is used to display or configure a network interface. nmcli command – A command to show or configure a network interface on Linux.

How do I see TCP connections in Linux?

The tcptrack command displays the status of TCP connections that it sees on a given network interface. tcptrack monitors their state and displays information such as state, source/destination addresses and bandwidth usage in a sorted, updated list very much like the top command.


1 Answers

I have been looking for that answer, too. I found an interesting piece of code in opensuse gitorious. Apparently, they use the following piece of code to get the interface type:

get_iface_type () {
    local IF=$1 TYPE
    test -n "$IF" || return 1
    test -d /sys/class/net/$IF || return 2
    case "`cat /sys/class/net/$IF/type`" in
            1)
                TYPE=eth
                # Ethernet, may also be wireless, ...
                if test -d /sys/class/net/$IF/wireless -o \
                        -L /sys/class/net/$IF/phy80211 ; then
                    TYPE=wlan
                elif test -d /sys/class/net/$IF/bridge ; then
                    TYPE=bridge
                elif test -f /proc/net/vlan/$IF ; then
                    TYPE=vlan
                elif test -d /sys/class/net/$IF/bonding ; then
                    TYPE=bond
                elif test -f /sys/class/net/$IF/tun_flags ; then
                    TYPE=tap
                elif test -d /sys/devices/virtual/net/$IF ; then
                    case $IF in
                      (dummy*) TYPE=dummy ;;
                    esac
                fi
                ;;
           24)  TYPE=eth ;; # firewire ;; # IEEE 1394 IPv4 - RFC 2734
           32)  # InfiniBand
            if test -d /sys/class/net/$IF/bonding ; then
                TYPE=bond
            elif test -d /sys/class/net/$IF/create_child ; then
                TYPE=ib
            else
                TYPE=ibchild
            fi
                ;;
          512)  TYPE=ppp ;;
          768)  TYPE=ipip ;; # IPIP tunnel
          769)  TYPE=ip6tnl ;; # IP6IP6 tunnel
          772)  TYPE=lo ;;
          776)  TYPE=sit ;; # sit0 device - IPv6-in-IPv4
          778)  TYPE=gre ;; # GRE over IP
          783)  TYPE=irda ;; # Linux-IrDA
          801)  TYPE=wlan_aux ;;
        65534)  TYPE=tun ;;
    esac
    # The following case statement still has to be replaced by something
    # which does not rely on the interface names.
    case $IF in
        ippp*|isdn*) TYPE=isdn;;
        mip6mnha*)   TYPE=mip6mnha;;
    esac
    test -n "$TYPE" && echo $TYPE && return 0
    return 3
}

I still have to find official documentation to confirm what the value in /sys/class/net/$IF/type means, but this function already explains a lot.

EDIT: ok, I've read about sysfs a little more, and finding that out is a pain in the ass. I've not found any proper documentation.

As you may know, this information is pulled from the kernel in order to be presented in userspace. So I ended up looking in the sources of sysfs and in the kernel in order to understand what is this "type" attribute. I believe that part of the answer should be found in net-sysfs.c, and also in linux/device.h. I just can't figure out how this stuff is connected. I stopped when I saw that I needed to understand all these macros...

like image 169
user48678 Avatar answered Sep 28 '22 08:09

user48678