! Please note that this is a snapshot of our old Bugzilla server, which is read only since May 29, 2020. Please go to gitlab.xfce.org for our new server !
Use of ifconfig
Status:
RESOLVED: FIXED
Product:
Xfce4-wavelan-plugin
Component:
General

Comments

Description E. Batalha 2014-04-16 00:16:12 CEST
I'm running a linuxfromscratch distribution.

I compiled and installed the wavelan plugin (0.5.11) but it was crashing every time I tried to add it to the panel.

I've realised the problem was the use of popen("/sbin/ifconfig -a", "r") in the wavelan_query_interfaces function. By default, linuxfromscratch does not install ifconfig. 

I propose that the function reads from /proc/net/dev instead, to find the list of interfaces, like this.

static GList*
wavelan_query_interfaces (void)
{
  GList *interfaces = NULL;
  gchar  line[1024];
  FILE  *fp;
  gint   m,n;

  TRACE ("Entered wavelan_query_interface");

  fp = fopen ("/proc/net/dev", "r");
  if (fp != NULL)
    {
      fgets (line, 1024, fp); //Ignore the first 2 lines
      fgets (line, 1024, fp);

      while (fgets (line, 1024, fp) != NULL)
        {
          for (m = 0; !isalnum(line[m]); ++m);
          for (n = m; isalnum (line[n]); ++n);
          line[n] = '\0';

          interfaces = g_list_append (interfaces, g_strdup (&line[m]));
        }

      pclose (fp);
    }

  return interfaces;
}
Comment 1 Landry Breuil editbugs 2014-04-16 21:59:07 CEST
Thing is, ifconfig -a is used on all systems and is somewhat portable, while using /proc is linux-only. Boooo.
Comment 2 Miguel Guedes 2014-04-17 02:09:39 CEST
I didn't know that -- what do the BSDs use, Landry?
Comment 3 Landry Breuil editbugs 2014-04-17 07:19:38 CEST
Given that this codepath in wavelan.c isnt within and #ifdef maze, i suppose *BSDs also use ifconfig -a, and we're lucky the output is vaguely similar to the one on linux, because afaict it correctly lists the interfaces for me on OpenBSD.
Comment 4 Azamat H. Hackimov editbugs 2014-04-17 09:12:10 CEST
Maybe getifaddrs(3)? It's portable on most *nixes.
Comment 5 Landry Breuil editbugs 2014-04-17 09:22:13 CEST
(In reply to Azamat H. Hackimov from comment #4)
> Maybe getifaddrs(3)? It's portable on most *nixes.

Hmmm.. if you provide a patch for this, i'll happily test it on OpenBSD :)
Comment 6 E. Batalha 2014-04-17 22:14:24 CEST
How about like this? It seems to work well in linux.
Can you test in bsd?

static GList*
wavelan_query_interfaces (void)
{
  GList *interfaces = NULL;

  struct ifaddrs *ifaddr, *ifa;

  TRACE ("Entered wavelan_query_interface");

  if (getifaddrs(&ifaddr) == -1) {
    return interfaces;
  }

  for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
    if (ifa->ifa_addr == NULL)
      continue;
    if (ifa->ifa_addr->sa_family == AF_PACKET)
      interfaces = g_list_append (interfaces, g_strdup (ifa->ifa_name));
  }

  freeifaddrs(ifaddr);

  return interfaces;
}
Comment 7 Landry Breuil editbugs 2014-04-17 23:01:17 CEST
t.c:16: error: 'AF_PACKET' undeclared (first use in this function)

Seems AF_PACKET is linux only, and on bsd AF_LINK should be used instead. At least on OpenBSD here it works.
http://stackoverflow.com/questions/9148425/freebsd-network-interface-information

renton:/tmp/ $cat t.c
#include <sys/types.h>
#include <sys/socket.h>
#include <ifaddrs.h>
#include <stdio.h>

int main() {
  struct ifaddrs *ifaddr, *ifa;
  if (getifaddrs(&ifaddr) == -1) {
    return 1;
  }
  for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
    if (ifa->ifa_addr == NULL)
      continue;
    if (ifa->ifa_addr->sa_family == AF_LINK)
      printf ("iface:%s\n",ifa->ifa_name);
  }
  return 0;
}
renton:/tmp/ $make t
cc -O2 -pipe    -o t t.c 
renton:/tmp/ $./t  
iface:lo0
iface:em0
iface:fxp0
iface:enc0
iface:pflog0
Comment 8 Landry Breuil editbugs 2014-04-20 10:47:14 CEST
http://git.xfce.org/panel-plugins/xfce4-wavelan-plugin/commit/?id=9d36f449f2b6f363d24ccef78edf6c414809a140 - if anyone on linux could test git master and confirm the interfaces are properly listed...
Comment 9 E. Batalha 2014-04-20 19:08:11 CEST
I can confirm that the git master version worked fine for me on linuxfromscratch (v7.4). 
Cannot comment on other distributions.

Bug #10822

Reported by:
E. Batalha
Reported on: 2014-04-16
Last modified on: 2014-04-20

People

Assignee:
Landry Breuil
CC List:
3 users

Version

Version:
unspecified

Attachments

Additional information