From nobody@FreeBSD.org  Fri Jul 22 07:07:05 2011
Return-Path: <nobody@FreeBSD.org>
Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34])
	by hub.freebsd.org (Postfix) with ESMTP id 42A8C106566B
	for <freebsd-gnats-submit@FreeBSD.org>; Fri, 22 Jul 2011 07:07:05 +0000 (UTC)
	(envelope-from nobody@FreeBSD.org)
Received: from red.freebsd.org (red.freebsd.org [IPv6:2001:4f8:fff6::22])
	by mx1.freebsd.org (Postfix) with ESMTP id 2794B8FC08
	for <freebsd-gnats-submit@FreeBSD.org>; Fri, 22 Jul 2011 07:07:05 +0000 (UTC)
Received: from red.freebsd.org (localhost [127.0.0.1])
	by red.freebsd.org (8.14.4/8.14.4) with ESMTP id p6M7741n085380
	for <freebsd-gnats-submit@FreeBSD.org>; Fri, 22 Jul 2011 07:07:04 GMT
	(envelope-from nobody@red.freebsd.org)
Received: (from nobody@localhost)
	by red.freebsd.org (8.14.4/8.14.4/Submit) id p6M774o2085379;
	Fri, 22 Jul 2011 07:07:04 GMT
	(envelope-from nobody)
Message-Id: <201107220707.p6M774o2085379@red.freebsd.org>
Date: Fri, 22 Jul 2011 07:07:04 GMT
From: Sreeram B S <sreeramb@netapp.com>
To: freebsd-gnats-submit@FreeBSD.org
Subject: ioctl SIOCGIFCONF reports interface names which are blank
X-Send-Pr-Version: www-3.1
X-GNATS-Notify:

>Number:         159100
>Category:       kern
>Synopsis:       [ioctl] ioctl SIOCGIFCONF reports interface names which are blank
>Confidential:   no
>Severity:       non-critical
>Priority:       medium
>Responsible:    freebsd-net
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Fri Jul 22 07:10:05 UTC 2011
>Closed-Date:    Mon Jul 25 02:59:43 UTC 2011
>Last-Modified:  Mon Jul 25 02:59:43 UTC 2011
>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:
State-Changed-From-To: open->closed 
State-Changed-By: linimon 
State-Changed-When: Mon Jul 25 02:58:18 UTC 2011 
State-Changed-Why:  
Duplicate of kern/159099. 


Responsible-Changed-From-To: freebsd-i386->freebsd-net 
Responsible-Changed-By: linimon 
Responsible-Changed-When: Mon Jul 25 02:58:18 UTC 2011 
Responsible-Changed-Why:  

http://www.freebsd.org/cgi/query-pr.cgi?pr=159100 
>Unformatted:
