I want to write a c code to display the network connection type in ubuntu.I found a library called connman (connection manager) and also i got a function connman_service_get_type for getting the connection type. But i don't get how to use that function in code. Can any one show me a sample code using the function "connman_service_get_type"

1

There are 1 answers

0
elryno On

ConnMan is a connection manager that is used to manage a device's network connections. It is not a general-purpose library for managing/querying network information. The function that you identify does not work outside the context of ConnMan.

If you are trying to determine if the interface is a wireless interface then you can do it by looking in the sysfs interface to the kernel (typically mounted at /sys). To do this, check for the existence of the /sys/class/net/NETDEVICE/wireless directory, where NETDEVICE is the device name. If the directory exists, it's a wireless interface.

This can also be done programmatically (in C) as shown in this snippet.

int check_wireless(const char* ifname, char* protocol) {
  int sock = -1;
  struct iwreq pwrq;
  memset(&pwrq, 0, sizeof(pwrq));
  strncpy(pwrq.ifr_name, ifname, IFNAMSIZ);

  if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
    perror("socket");
    return 0;
  }

  if (ioctl(sock, SIOCGIWNAME, &pwrq) != -1) {
    if (protocol) strncpy(protocol, pwrq.u.name, IFNAMSIZ);
    close(sock);
    return 1;
  }

  close(sock);
  return 0;
}

A complete example of using this code can be found at https://gist.github.com/edufelipe/6108057. This example uses the IOCTL interface to the kernel to get the information.

If you are really looking for more information about the interface type than if it is wireless or not, then here is an example of a shell script used by OpenSUSE (git repo) to determine 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
}

This could also be done from C code.