From 1bc25f5914b45b2e5487151b8e416808ac72eef4 Mon Sep 17 00:00:00 2001 From: itojun Date: Fri, 24 Nov 2000 08:21:12 +0000 Subject: [PATCH] strictly conform to RFC2553 (specifically ENXIO case). sync with KAME --- lib/libc/net/if_indextoname.3 | 19 +++++++++++----- lib/libc/net/if_indextoname.c | 42 ++++++++++++++++++++--------------- lib/libc/net/if_nameindex.c | 30 +++++++++++++------------ lib/libc/net/if_nametoindex.c | 23 ++++++++++++------- 4 files changed, 69 insertions(+), 45 deletions(-) diff --git a/lib/libc/net/if_indextoname.3 b/lib/libc/net/if_indextoname.3 index f8b82328060c..67b115366905 100644 --- a/lib/libc/net/if_indextoname.3 +++ b/lib/libc/net/if_indextoname.3 @@ -1,5 +1,5 @@ -.\" $NetBSD: if_indextoname.3,v 1.6 2000/09/21 10:43:49 ad Exp $ -.\" $KAME: if_indextoname.3,v 1.8 2000/04/24 10:12:36 itojun Exp $ +.\" $NetBSD: if_indextoname.3,v 1.7 2000/11/24 08:21:12 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 .\" .\" 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 of 0 and an .Nm if_name -of NULL. A NULL pointer is returned upon an error. +of NULL. +A NULL pointer is returned upon an error. .Pp The .Fn if_freenameindex @@ -92,7 +93,11 @@ allocated by Upon successful completion, .Fn if_nametoindex 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 .Xr getifaddrs 3 . .Pp @@ -100,7 +105,11 @@ Upon successful completion, .Fn if_indextoname returns .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 .Xr getifaddrs 3 . .Pp diff --git a/lib/libc/net/if_indextoname.c b/lib/libc/net/if_indextoname.c index 22e462aabd87..b1a46b0e13d7 100644 --- a/lib/libc/net/if_indextoname.c +++ b/lib/libc/net/if_indextoname.c @@ -1,5 +1,5 @@ -/* $NetBSD: if_indextoname.c,v 1.3 2000/07/06 02:54:55 christos Exp $ */ -/* $KAME: if_indextoname.c,v 1.4 2000/04/24 10:08:41 itojun Exp $ */ +/* $NetBSD: if_indextoname.c,v 1.4 2000/11/24 08:21:12 itojun Exp $ */ +/* $KAME: if_indextoname.c,v 1.7 2000/11/08 03:09:30 itojun Exp $ */ /*- * Copyright (c) 1997, 2000 @@ -28,7 +28,7 @@ #include #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 */ #include "namespace.h" @@ -39,38 +39,41 @@ __RCSID("$NetBSD: if_indextoname.c,v 1.3 2000/07/06 02:54:55 christos Exp $"); #include #include #include +#include #ifdef __weak_alias __weak_alias(if_indextoname,_if_indextoname) #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 - * name. + * #include * - * #include + * 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 IFNAMSIZ bytes - * into which the interface name corresponding to the specified index is - * returned. (IFNAMSIZ is also defined in and its value - * includes a terminating null byte at the end of the interface name.) - * This pointer is also the return value of the function. If there is - * no interface corresponding to the specified index, NULL is returned. + * The ifname argument must point to a buffer of at least IF_NAMESIZE + * bytes into which the interface name corresponding to the specified + * index is returned. (IF_NAMESIZE is also defined in and + * its value includes a terminating null byte at the end of the + * interface name.) This pointer is also the return value of the + * function. If there is no interface corresponding to the specified + * index, NULL is returned, and errno is set to ENXIO, if there was a + * 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 * if_indextoname(unsigned int ifindex, char *ifname) { struct ifaddrs *ifaddrs, *ifa; + int error = 0; if (getifaddrs(&ifaddrs) < 0) - return(NULL); + return(NULL); /* getifaddrs properly set errno */ for (ifa = ifaddrs; ifa != NULL; ifa = ifa->ifa_next) { if (ifa->ifa_addr && @@ -80,12 +83,15 @@ if_indextoname(unsigned int ifindex, char *ifname) break; } - if (ifa == NULL) + if (ifa == NULL) { + error = ENXIO; ifname = NULL; + } else strncpy(ifname, ifa->ifa_name, IFNAMSIZ); freeifaddrs(ifaddrs); + errno = error; return(ifname); } diff --git a/lib/libc/net/if_nameindex.c b/lib/libc/net/if_nameindex.c index 8386687ebc1e..f30d3400353f 100644 --- a/lib/libc/net/if_nameindex.c +++ b/lib/libc/net/if_nameindex.c @@ -1,5 +1,5 @@ -/* $NetBSD: if_nameindex.c,v 1.4 2000/07/24 12:06:55 itojun Exp $ */ -/* $KAME: if_nameindex.c,v 1.5 2000/07/24 12:03:31 itojun Exp $ */ +/* $NetBSD: if_nameindex.c,v 1.5 2000/11/24 08:21:12 itojun Exp $ */ +/* $KAME: if_nameindex.c,v 1.8 2000/11/24 08:20:01 itojun Exp $ */ /*- * Copyright (c) 1997, 2000 @@ -28,7 +28,7 @@ #include #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 */ #include "namespace.h" @@ -45,25 +45,27 @@ __weak_alias(if_nameindex,_if_nameindex) __weak_alias(if_freenameindex,_if_freenameindex) #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 + * header. + * + * struct if_nameindex { + * unsigned int if_index; + * char *if_name; + * }; * * The final function returns an array of if_nameindex structures, one * structure per interface. * - * #include - * - * struct if_nameindex { - * unsigned int if_index; / * 1, 2, ... * / - * char *if_name; / * null terminated name: "le0", ... * / - * }; - * - * struct if_nameindex *if_nameindex(void); + * struct if_nameindex *if_nameindex(void); * * 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 - * 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 * names pointed to by the if_name members is obtained dynamically. diff --git a/lib/libc/net/if_nametoindex.c b/lib/libc/net/if_nametoindex.c index f068f9dcddd5..3e8297d1b68d 100644 --- a/lib/libc/net/if_nametoindex.c +++ b/lib/libc/net/if_nametoindex.c @@ -1,5 +1,5 @@ -/* $NetBSD: if_nametoindex.c,v 1.3 2000/07/06 02:56:25 christos Exp $ */ -/* $KAME: if_nametoindex.c,v 1.4 2000/04/24 10:08:41 itojun Exp $ */ +/* $NetBSD: if_nametoindex.c,v 1.4 2000/11/24 08:21:12 itojun Exp $ */ +/* $KAME: if_nametoindex.c,v 1.6 2000/11/24 08:18:54 itojun Exp $ */ /*- * Copyright (c) 1997, 2000 @@ -28,7 +28,7 @@ #include #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 */ #include "namespace.h" @@ -39,24 +39,29 @@ __RCSID("$NetBSD: if_nametoindex.c,v 1.3 2000/07/06 02:56:25 christos Exp $"); #include #include #include +#include #ifdef __weak_alias __weak_alias(if_nametoindex,_if_nametoindex) #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 * index. * - * #include + * #include * - * 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 @@ -81,5 +86,7 @@ if_nametoindex(const char *ifname) } freeifaddrs(ifaddrs); + if (!ni) + errno = ENXIO; return(ni); }