strictly conform to RFC2553 (specifically ENXIO case). sync with KAME

This commit is contained in:
itojun 2000-11-24 08:21:12 +00:00
parent 50ac5d898b
commit 1bc25f5914
4 changed files with 69 additions and 45 deletions

View File

@ -1,5 +1,5 @@
.\" $NetBSD: if_indextoname.3,v 1.6 2000/09/21 10:43:49 ad Exp $ .\" $NetBSD: if_indextoname.3,v 1.7 2000/11/24 08:21:12 itojun Exp $
.\" $KAME: if_indextoname.3,v 1.8 2000/04/24 10:12:36 itojun Exp $ .\" $KAME: if_indextoname.3,v 1.10 2000/11/24 08:13:51 itojun Exp $
.\" BSDI Id: if_indextoname.3,v 2.2 2000/04/17 22:38:05 dab Exp .\" BSDI Id: if_indextoname.3,v 2.2 2000/04/17 22:38:05 dab Exp
.\" .\"
.\" Copyright (c) 1997, 2000 .\" Copyright (c) 1997, 2000
@ -81,7 +81,8 @@ The end of the array of structures is indicated by a structure with an
.Nm if_index .Nm if_index
of 0 and an of 0 and an
.Nm if_name .Nm if_name
of NULL. A NULL pointer is returned upon an error. of NULL.
A NULL pointer is returned upon an error.
.Pp .Pp
The The
.Fn if_freenameindex .Fn if_freenameindex
@ -92,7 +93,11 @@ allocated by
Upon successful completion, Upon successful completion,
.Fn if_nametoindex .Fn if_nametoindex
returns the index number of the interface. returns the index number of the interface.
A value of 0 is returned if the interface is not found or an error If the interface is not found, a value of 0 is returned and
.Va errno
is set to
.Er ENXIO .
A value of 0 is also returned if an error
occurs while retrieving the list of interfaces via occurs while retrieving the list of interfaces via
.Xr getifaddrs 3 . .Xr getifaddrs 3 .
.Pp .Pp
@ -100,7 +105,11 @@ Upon successful completion,
.Fn if_indextoname .Fn if_indextoname
returns returns
.Ar ifname . .Ar ifname .
A NULL pointer is returned if the interface is not found or an error If the interface is not found, a NULL pointer is returned and
.Va errno
is set to
.Er ENXIO .
A NULL pointer is also returned if an error
occurs while retrieving the list of interfaces via occurs while retrieving the list of interfaces via
.Xr getifaddrs 3 . .Xr getifaddrs 3 .
.Pp .Pp

View File

@ -1,5 +1,5 @@
/* $NetBSD: if_indextoname.c,v 1.3 2000/07/06 02:54:55 christos Exp $ */ /* $NetBSD: if_indextoname.c,v 1.4 2000/11/24 08:21:12 itojun Exp $ */
/* $KAME: if_indextoname.c,v 1.4 2000/04/24 10:08:41 itojun Exp $ */ /* $KAME: if_indextoname.c,v 1.7 2000/11/08 03:09:30 itojun Exp $ */
/*- /*-
* Copyright (c) 1997, 2000 * Copyright (c) 1997, 2000
@ -28,7 +28,7 @@
#include <sys/cdefs.h> #include <sys/cdefs.h>
#if defined(LIBC_SCCS) && !defined(lint) #if defined(LIBC_SCCS) && !defined(lint)
__RCSID("$NetBSD: if_indextoname.c,v 1.3 2000/07/06 02:54:55 christos Exp $"); __RCSID("$NetBSD: if_indextoname.c,v 1.4 2000/11/24 08:21:12 itojun Exp $");
#endif /* LIBC_SCCS and not lint */ #endif /* LIBC_SCCS and not lint */
#include "namespace.h" #include "namespace.h"
@ -39,38 +39,41 @@ __RCSID("$NetBSD: if_indextoname.c,v 1.3 2000/07/06 02:54:55 christos Exp $");
#include <ifaddrs.h> #include <ifaddrs.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <errno.h>
#ifdef __weak_alias #ifdef __weak_alias
__weak_alias(if_indextoname,_if_indextoname) __weak_alias(if_indextoname,_if_indextoname)
#endif #endif
/* /*
* From RFC 2133: * From RFC 2533:
* *
* 4.2. Index-to-Name * The second function maps an interface index into its corresponding
* name.
* *
* The second function maps an interface index into its corresponding * #include <net/if.h>
* name.
* *
* #include <net/if.h> * char *if_indextoname(unsigned int ifindex, char *ifname);
* *
* char *if_indextoname(unsigned int ifindex, char *ifname); * The ifname argument must point to a buffer of at least IF_NAMESIZE
* * bytes into which the interface name corresponding to the specified
* The ifname argument must point to a buffer of at least IFNAMSIZ bytes * index is returned. (IF_NAMESIZE is also defined in <net/if.h> and
* into which the interface name corresponding to the specified index is * its value includes a terminating null byte at the end of the
* returned. (IFNAMSIZ is also defined in <net/if.h> and its value * interface name.) This pointer is also the return value of the
* includes a terminating null byte at the end of the interface name.) * function. If there is no interface corresponding to the specified
* This pointer is also the return value of the function. If there is * index, NULL is returned, and errno is set to ENXIO, if there was a
* no interface corresponding to the specified index, NULL is returned. * system error (such as running out of memory), if_indextoname returns
* NULL and errno would be set to the proper value (e.g., ENOMEM).
*/ */
char * char *
if_indextoname(unsigned int ifindex, char *ifname) if_indextoname(unsigned int ifindex, char *ifname)
{ {
struct ifaddrs *ifaddrs, *ifa; struct ifaddrs *ifaddrs, *ifa;
int error = 0;
if (getifaddrs(&ifaddrs) < 0) if (getifaddrs(&ifaddrs) < 0)
return(NULL); return(NULL); /* getifaddrs properly set errno */
for (ifa = ifaddrs; ifa != NULL; ifa = ifa->ifa_next) { for (ifa = ifaddrs; ifa != NULL; ifa = ifa->ifa_next) {
if (ifa->ifa_addr && if (ifa->ifa_addr &&
@ -80,12 +83,15 @@ if_indextoname(unsigned int ifindex, char *ifname)
break; break;
} }
if (ifa == NULL) if (ifa == NULL) {
error = ENXIO;
ifname = NULL; ifname = NULL;
}
else else
strncpy(ifname, ifa->ifa_name, IFNAMSIZ); strncpy(ifname, ifa->ifa_name, IFNAMSIZ);
freeifaddrs(ifaddrs); freeifaddrs(ifaddrs);
errno = error;
return(ifname); return(ifname);
} }

View File

@ -1,5 +1,5 @@
/* $NetBSD: if_nameindex.c,v 1.4 2000/07/24 12:06:55 itojun Exp $ */ /* $NetBSD: if_nameindex.c,v 1.5 2000/11/24 08:21:12 itojun Exp $ */
/* $KAME: if_nameindex.c,v 1.5 2000/07/24 12:03:31 itojun Exp $ */ /* $KAME: if_nameindex.c,v 1.8 2000/11/24 08:20:01 itojun Exp $ */
/*- /*-
* Copyright (c) 1997, 2000 * Copyright (c) 1997, 2000
@ -28,7 +28,7 @@
#include <sys/cdefs.h> #include <sys/cdefs.h>
#if defined(LIBC_SCCS) && !defined(lint) #if defined(LIBC_SCCS) && !defined(lint)
__RCSID("$NetBSD: if_nameindex.c,v 1.4 2000/07/24 12:06:55 itojun Exp $"); __RCSID("$NetBSD: if_nameindex.c,v 1.5 2000/11/24 08:21:12 itojun Exp $");
#endif /* LIBC_SCCS and not lint */ #endif /* LIBC_SCCS and not lint */
#include "namespace.h" #include "namespace.h"
@ -45,25 +45,27 @@ __weak_alias(if_nameindex,_if_nameindex)
__weak_alias(if_freenameindex,_if_freenameindex) __weak_alias(if_freenameindex,_if_freenameindex)
#endif #endif
/* /*
* From RFC 2133: * From RFC 2553:
* *
* 4.3. Return All Interface Names and Indexes * 4.3 Return All Interface Names and Indexes
*
* The if_nameindex structure holds the information about a single
* interface and is defined as a result of including the <net/if.h>
* header.
*
* struct if_nameindex {
* unsigned int if_index;
* char *if_name;
* };
* *
* The final function returns an array of if_nameindex structures, one * The final function returns an array of if_nameindex structures, one
* structure per interface. * structure per interface.
* *
* #include <net/if.h> * struct if_nameindex *if_nameindex(void);
*
* struct if_nameindex {
* unsigned int if_index; / * 1, 2, ... * /
* char *if_name; / * null terminated name: "le0", ... * /
* };
*
* struct if_nameindex *if_nameindex(void);
* *
* The end of the array of structures is indicated by a structure with * The end of the array of structures is indicated by a structure with
* an if_index of 0 and an if_name of NULL. The function returns a NULL * an if_index of 0 and an if_name of NULL. The function returns a NULL
* pointer upon an error. * pointer upon an error, and would set errno to the appropriate value.
* *
* The memory used for this array of structures along with the interface * The memory used for this array of structures along with the interface
* names pointed to by the if_name members is obtained dynamically. * names pointed to by the if_name members is obtained dynamically.

View File

@ -1,5 +1,5 @@
/* $NetBSD: if_nametoindex.c,v 1.3 2000/07/06 02:56:25 christos Exp $ */ /* $NetBSD: if_nametoindex.c,v 1.4 2000/11/24 08:21:12 itojun Exp $ */
/* $KAME: if_nametoindex.c,v 1.4 2000/04/24 10:08:41 itojun Exp $ */ /* $KAME: if_nametoindex.c,v 1.6 2000/11/24 08:18:54 itojun Exp $ */
/*- /*-
* Copyright (c) 1997, 2000 * Copyright (c) 1997, 2000
@ -28,7 +28,7 @@
#include <sys/cdefs.h> #include <sys/cdefs.h>
#if defined(LIBC_SCCS) && !defined(lint) #if defined(LIBC_SCCS) && !defined(lint)
__RCSID("$NetBSD: if_nametoindex.c,v 1.3 2000/07/06 02:56:25 christos Exp $"); __RCSID("$NetBSD: if_nametoindex.c,v 1.4 2000/11/24 08:21:12 itojun Exp $");
#endif /* LIBC_SCCS and not lint */ #endif /* LIBC_SCCS and not lint */
#include "namespace.h" #include "namespace.h"
@ -39,24 +39,29 @@ __RCSID("$NetBSD: if_nametoindex.c,v 1.3 2000/07/06 02:56:25 christos Exp $");
#include <ifaddrs.h> #include <ifaddrs.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <errno.h>
#ifdef __weak_alias #ifdef __weak_alias
__weak_alias(if_nametoindex,_if_nametoindex) __weak_alias(if_nametoindex,_if_nametoindex)
#endif #endif
/* /*
* From RFC 2133: * From RFC 2553:
*
* 4.1 Name-to-Index
* *
* 4.1. Name-to-Index
* *
* The first function maps an interface name into its corresponding * The first function maps an interface name into its corresponding
* index. * index.
* *
* #include <net/if.h> * #include <net/if.h>
* *
* unsigned int if_nametoindex(const char *ifname); * unsigned int if_nametoindex(const char *ifname);
* *
* If the specified interface does not exist, the return value is 0. * If the specified interface name does not exist, the return value is
* 0, and errno is set to ENXIO. If there was a system error (such as
* running out of memory), the return value is 0 and errno is set to the
* proper value (e.g., ENOMEM).
*/ */
unsigned int unsigned int
@ -81,5 +86,7 @@ if_nametoindex(const char *ifname)
} }
freeifaddrs(ifaddrs); freeifaddrs(ifaddrs);
if (!ni)
errno = ENXIO;
return(ni); return(ni);
} }