Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 22 Jul 2011 07:07:04 GMT
From:      Sreeram B S <sreeramb@netapp.com>
To:        freebsd-gnats-submit@FreeBSD.org
Subject:   i386/159100: ioctl SIOCGIFCONF reports interface names which are blank
Message-ID:  <201107220707.p6M774o2085379@red.freebsd.org>
Resent-Message-ID: <201107220710.p6M7A55Y069555@freefall.freebsd.org>

next in thread | raw e-mail | index | archive | help

>Number:         159100
>Category:       i386
>Synopsis:       ioctl SIOCGIFCONF reports interface names which are blank
>Confidential:   no
>Severity:       non-critical
>Priority:       medium
>Responsible:    freebsd-i386
>State:          open
>Quarter:        
>Keywords:       
>Date-Required:
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Fri Jul 22 07:10:05 UTC 2011
>Closed-Date:
>Last-Modified:
>Originator:     Sreeram B S
>Release:        7.2
>Organization:
NetApp
>Environment:
FreeBSD bsd213-64.lab.eng.btc.netapp.in 7.2-RELEASE FreeBSD 7.2-RELEASE #0: Fri May  1 08:49:13 UTC 2009     root@walker.cse.buffalo.edu:/usr/obj/usr/src/sys/GENERIC  i386
>Description:
Hi,
I have written a program to output interface names. This program uses the ioctl SIOCGIFCONF. The program compiles successfully, but upon execution, the output contains valid interface names *and* bland interface names, as well. The program and its output are as follows:

**********************************************************
bsd213-64# cat ifaddr.c
/* This is a program to identify the interfaces and their ip-addresses. */

#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <net/if.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>

int main() {
int sock, temp;
u_char buf[1024], *ptr;
struct ifconf ifc;
struct ifreq *ifr;

/* Create a UDP socket. */
sock = socket(AF_INET, SOCK_DGRAM, 0);
if (sock < 0) {
perror("Socket");
exit(-1);
}
printf("Created the socket.\n");

/* Call ioctl() to get the interface information. */
ifc.ifc_buf = buf;
ifc.ifc_len = 1024;
temp = ioctl(sock, SIOCGIFCONF, &ifc);
if (temp < 0) {
perror("SIOCIFCONF");
close(sock);
exit(-2);
}

printf("Got the information of the interfaces.\n");
printf("Information length: %d\n", ifc.ifc_len);

/* Run through each interface now. */
for (ptr = ifc.ifc_buf; ptr < buf + ifc.ifc_len;)
{
ifr = (struct ifreq *)ptr;
printf(" %s : \n", ifr->ifr_name);
ptr = ptr + sizeof(struct ifreq);
}
close(sock);
exit(0);
}

######## Output ##########

bsd213-64# ./ifaddr
Created the socket.
Got the information of the interfaces.
Information length: 368
bce0 :
i :
:
:
bce1 :
:
8 :
:
:
:
:
:
bsd213-64#
******************************************************************

But upon running the same program, absolutely same (without even a single change), on a Linux machine or a Solaris 10 machine, the output contains only valid interface names, which is the expected behavior.

*****************************************************************
[sreeramb@sreeramb1-lxp misc]$ uname -o
GNU/Linux
[sreeramb@sreeramb1-lxp misc]$
[sreeramb@sreeramb1-lxp misc]$ gcc ifaddr.c -o ifaddr
[sreeramb@sreeramb1-lxp misc]$ ./ifaddr
Created the socket.
Got the information of the interfaces.
Information length: 96
lo :
eth0 :
virbr0 :
[sreeramb@sreeramb1-lxp misc]$
*****************************************************************

This means that the data returned by the ioctl SIOCGIFCONF is incorrect or the format is messed up. This needs attention and has to be corrected.
Please note that I understand that there are other methods to obtain the interfaces and IP addresses like using getipaddrs() system call etc. But incorrect behavior of SIOCGIFCONF needs attention because:

1. This leads to portability problems. Any program using this ioctl and running well on other OSes cannot be portable on FreeBSD because of the problem in the ioctl.
2. Moreover, this ioctl is important since almost all network related applications use it and it should be intact.

Please provide a fix for this.

Regards,
Sreeram
>How-To-Repeat:
Please compile and execute the program mentioned above.
>Fix:


>Release-Note:
>Audit-Trail:
>Unformatted:



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201107220707.p6M774o2085379>