Determining the type of network connection in Linux

How to determine the type of network connection, for example, wired or Wi-Fi, in a C ++ Linux application?

If the device has several network interfaces, I would like to determine the type of connection for the interface used.

Thank you

+4
source share
4 answers

Thanks for all the input.

The solution I came up with:

  • get all active interface names and IP addresses from /proc/net/dev

  • get current interface by matching used ip address

  • Check if the current interface is wireless by looking at /proc/net/wireless

0
source

I also searched for this answer. I found an interesting piece of code in the opened Gitorious . Apparently, they use the following code snippet 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 need to find the official documentation to confirm what the value in / sys / class / net / $ IF / type means, but this function already explains a lot.

EDIT: ok, I read a little more about sysfs, and finding it to be a pain in the ass. I did not find the proper documentation.

As you may know, this information is taken from the kernel for presentation in user space. So I ended up looking at the sysfs sources and the kernel to understand what the type attribute is. I believe that part of the answer should be found in net-sysfs.c as well as in linux / device.h. I just can’t understand how all this is connected. I stopped when I saw that I needed to understand all these macros ...

+5
source

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

+1
source

In the interface that is used, eth or wlan.

0
source

Source: https://habr.com/ru/post/1332562/


All Articles