开发者

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开发者_如何学C application?

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

Thanks.


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...


If the interface is present in /proc/net/wireless, it is a wireless interface. Otherwise, it is not.


Thanks for all the inputs.

The solution I come up:

  1. get all active interfaces' names and ip addresses from /proc/net/dev

  2. get the current interface by mapping the ip address used

  3. Check whether the current interface is wireless by looking at /proc/net/wireless


By the interface which is used, eth or wlan.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜