xref: /minix3/lib/libc/net/if_nameindex.c (revision f14fb602092e015ff630df58e17c2a9cd57d29b3)
1*f14fb602SLionel Sambuc /*	$NetBSD: if_nameindex.c,v 1.7 2012/03/13 21:13:41 christos Exp $	*/
22fe8fb19SBen Gras /*	$KAME: if_nameindex.c,v 1.8 2000/11/24 08:20:01 itojun Exp $	*/
32fe8fb19SBen Gras 
42fe8fb19SBen Gras /*-
52fe8fb19SBen Gras  * Copyright (c) 1997, 2000
62fe8fb19SBen Gras  *	Berkeley Software Design, Inc.  All rights reserved.
72fe8fb19SBen Gras  *
82fe8fb19SBen Gras  * Redistribution and use in source and binary forms, with or without
92fe8fb19SBen Gras  * modification, are permitted provided that the following conditions
102fe8fb19SBen Gras  * are met:
112fe8fb19SBen Gras  * 1. Redistributions of source code must retain the above copyright
122fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer.
132fe8fb19SBen Gras  *
142fe8fb19SBen Gras  * THIS SOFTWARE IS PROVIDED BY Berkeley Software Design, Inc. ``AS IS'' AND
152fe8fb19SBen Gras  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
162fe8fb19SBen Gras  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
172fe8fb19SBen Gras  * ARE DISCLAIMED.  IN NO EVENT SHALL Berkeley Software Design, Inc. BE LIABLE
182fe8fb19SBen Gras  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
192fe8fb19SBen Gras  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
202fe8fb19SBen Gras  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
212fe8fb19SBen Gras  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
222fe8fb19SBen Gras  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
232fe8fb19SBen Gras  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
242fe8fb19SBen Gras  * SUCH DAMAGE.
252fe8fb19SBen Gras  *
262fe8fb19SBen Gras  *	BSDI Id: if_nameindex.c,v 2.3 2000/04/17 22:38:05 dab Exp
272fe8fb19SBen Gras  */
282fe8fb19SBen Gras 
292fe8fb19SBen Gras #include <sys/cdefs.h>
302fe8fb19SBen Gras #if defined(LIBC_SCCS) && !defined(lint)
31*f14fb602SLionel Sambuc __RCSID("$NetBSD: if_nameindex.c,v 1.7 2012/03/13 21:13:41 christos Exp $");
322fe8fb19SBen Gras #endif /* LIBC_SCCS and not lint */
332fe8fb19SBen Gras 
342fe8fb19SBen Gras #include "namespace.h"
352fe8fb19SBen Gras #include <sys/types.h>
362fe8fb19SBen Gras #include <sys/socket.h>
372fe8fb19SBen Gras #include <net/if_dl.h>
382fe8fb19SBen Gras #include <net/if.h>
392fe8fb19SBen Gras #include <ifaddrs.h>
402fe8fb19SBen Gras #include <stdlib.h>
412fe8fb19SBen Gras #include <string.h>
422fe8fb19SBen Gras 
432fe8fb19SBen Gras #ifdef __weak_alias
__weak_alias(if_nameindex,_if_nameindex)442fe8fb19SBen Gras __weak_alias(if_nameindex,_if_nameindex)
452fe8fb19SBen Gras __weak_alias(if_freenameindex,_if_freenameindex)
462fe8fb19SBen Gras #endif
472fe8fb19SBen Gras /*
482fe8fb19SBen Gras  * From RFC 2553:
492fe8fb19SBen Gras  *
502fe8fb19SBen Gras  * 4.3 Return All Interface Names and Indexes
512fe8fb19SBen Gras  *
522fe8fb19SBen Gras  *    The if_nameindex structure holds the information about a single
532fe8fb19SBen Gras  *    interface and is defined as a result of including the <net/if.h>
542fe8fb19SBen Gras  *    header.
552fe8fb19SBen Gras  *
562fe8fb19SBen Gras  *       struct if_nameindex {
572fe8fb19SBen Gras  *         unsigned int   if_index;
582fe8fb19SBen Gras  *         char          *if_name;
592fe8fb19SBen Gras  *       };
602fe8fb19SBen Gras  *
612fe8fb19SBen Gras  *    The final function returns an array of if_nameindex structures, one
622fe8fb19SBen Gras  *    structure per interface.
632fe8fb19SBen Gras  *
642fe8fb19SBen Gras  *       struct if_nameindex  *if_nameindex(void);
652fe8fb19SBen Gras  *
662fe8fb19SBen Gras  *    The end of the array of structures is indicated by a structure with
672fe8fb19SBen Gras  *    an if_index of 0 and an if_name of NULL.  The function returns a NULL
682fe8fb19SBen Gras  *    pointer upon an error, and would set errno to the appropriate value.
692fe8fb19SBen Gras  *
702fe8fb19SBen Gras  *    The memory used for this array of structures along with the interface
712fe8fb19SBen Gras  *    names pointed to by the if_name members is obtained dynamically.
722fe8fb19SBen Gras  *    This memory is freed by the next function.
732fe8fb19SBen Gras  *
742fe8fb19SBen Gras  * 4.4.  Free Memory
752fe8fb19SBen Gras  *
762fe8fb19SBen Gras  *    The following function frees the dynamic memory that was allocated by
772fe8fb19SBen Gras  *    if_nameindex().
782fe8fb19SBen Gras  *
792fe8fb19SBen Gras  *        #include <net/if.h>
802fe8fb19SBen Gras  *
812fe8fb19SBen Gras  *        void  if_freenameindex(struct if_nameindex *ptr);
822fe8fb19SBen Gras  *
832fe8fb19SBen Gras  *    The argument to this function must be a pointer that was returned by
842fe8fb19SBen Gras  *    if_nameindex().
852fe8fb19SBen Gras  */
862fe8fb19SBen Gras 
872fe8fb19SBen Gras struct if_nameindex *
882fe8fb19SBen Gras if_nameindex(void)
892fe8fb19SBen Gras {
902fe8fb19SBen Gras 	struct ifaddrs *ifaddrs, *ifa;
91*f14fb602SLionel Sambuc 	size_t nbytes, ni;
922fe8fb19SBen Gras 	struct if_nameindex *ifni, *ifni2;
932fe8fb19SBen Gras 	char *cp;
942fe8fb19SBen Gras 
952fe8fb19SBen Gras 	if (getifaddrs(&ifaddrs) < 0)
962fe8fb19SBen Gras 		return(NULL);
972fe8fb19SBen Gras 
982fe8fb19SBen Gras 	/*
992fe8fb19SBen Gras 	 * First, find out how many interfaces there are, and how
1002fe8fb19SBen Gras 	 * much space we need for the string names.
1012fe8fb19SBen Gras 	 */
1022fe8fb19SBen Gras 	ni = 0;
1032fe8fb19SBen Gras 	nbytes = 0;
1042fe8fb19SBen Gras 	for (ifa = ifaddrs; ifa != NULL; ifa = ifa->ifa_next) {
1052fe8fb19SBen Gras 		if (ifa->ifa_addr &&
1062fe8fb19SBen Gras 		    ifa->ifa_addr->sa_family == AF_LINK) {
1072fe8fb19SBen Gras 			nbytes += strlen(ifa->ifa_name) + 1;
1082fe8fb19SBen Gras 			ni++;
1092fe8fb19SBen Gras 		}
1102fe8fb19SBen Gras 	}
1112fe8fb19SBen Gras 
1122fe8fb19SBen Gras 	/*
1132fe8fb19SBen Gras 	 * Next, allocate a chunk of memory, use the first part
1142fe8fb19SBen Gras 	 * for the array of structures, and the last part for
1152fe8fb19SBen Gras 	 * the strings.
1162fe8fb19SBen Gras 	 */
1172fe8fb19SBen Gras 	cp = malloc((ni + 1) * sizeof(struct if_nameindex) + nbytes);
1182fe8fb19SBen Gras 	ifni = (struct if_nameindex *)(void *)cp;
1192fe8fb19SBen Gras 	if (ifni == NULL)
1202fe8fb19SBen Gras 		goto out;
1212fe8fb19SBen Gras 	cp += (ni + 1) * sizeof(struct if_nameindex);
1222fe8fb19SBen Gras 
1232fe8fb19SBen Gras 	/*
1242fe8fb19SBen Gras 	 * Now just loop through the list of interfaces again,
1252fe8fb19SBen Gras 	 * filling in the if_nameindex array and making copies
1262fe8fb19SBen Gras 	 * of all the strings.
1272fe8fb19SBen Gras 	 */
1282fe8fb19SBen Gras 	ifni2 = ifni;
1292fe8fb19SBen Gras 	for (ifa = ifaddrs; ifa != NULL; ifa = ifa->ifa_next) {
1302fe8fb19SBen Gras 		if (ifa->ifa_addr &&
1312fe8fb19SBen Gras 		    ifa->ifa_addr->sa_family == AF_LINK) {
1322fe8fb19SBen Gras 			ifni2->if_index =
1332fe8fb19SBen Gras 			    ((struct sockaddr_dl*)
1342fe8fb19SBen Gras 			    (void *)ifa->ifa_addr)->sdl_index;
1352fe8fb19SBen Gras 			ifni2->if_name = cp;
1362fe8fb19SBen Gras 			strcpy(cp, ifa->ifa_name);
1372fe8fb19SBen Gras 			ifni2++;
1382fe8fb19SBen Gras 			cp += strlen(cp) + 1;
1392fe8fb19SBen Gras 		}
1402fe8fb19SBen Gras 	}
1412fe8fb19SBen Gras 	/*
1422fe8fb19SBen Gras 	 * Finally, don't forget to terminate the array.
1432fe8fb19SBen Gras 	 */
1442fe8fb19SBen Gras 	ifni2->if_index = 0;
1452fe8fb19SBen Gras 	ifni2->if_name = NULL;
1462fe8fb19SBen Gras out:
1472fe8fb19SBen Gras 	freeifaddrs(ifaddrs);
1482fe8fb19SBen Gras 	return(ifni);
1492fe8fb19SBen Gras }
1502fe8fb19SBen Gras 
1512fe8fb19SBen Gras void
if_freenameindex(struct if_nameindex * ptr)1522fe8fb19SBen Gras if_freenameindex(struct if_nameindex *ptr)
1532fe8fb19SBen Gras {
1542fe8fb19SBen Gras 	free(ptr);
1552fe8fb19SBen Gras }
156