i386/159099: ioctl SIOCGIFCONF reports interface names which are blank

Sreeram B S sreeramabs at netapp.com
Fri Jul 22 06:50:09 UTC 2011


>Number:         159099
>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 06:50:08 UTC 2011
>Closed-Date:
>Last-Modified:
>Originator:     Sreeram B S
>Release:        7.2
>Organization:
>Environment:
bsd213-64# uname -a
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 at walker.cse.buffalo.edu:/usr/obj/usr/src/sys/GENERIC  i386
bsd213-64#
>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 at sreeramb1-lxp misc]$ uname -o
GNU/Linux
[sreeramb at sreeramb1-lxp misc]$
[sreeramb at sreeramb1-lxp misc]$ gcc ifaddr.c -o ifaddr
[sreeramb at sreeramb1-lxp misc]$ ./ifaddr
Created the socket.
Got the information of the interfaces.
Information length: 96
    lo : 
    eth0 : 
    virbr0 : 
[sreeramb at 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 pasted above.
>Fix:


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


More information about the freebsd-i386 mailing list