Home | History | Annotate | Line # | Download | only in net
if_nameindex.c revision 1.1
      1 /*	$NetBSD: if_nameindex.c,v 1.1 2000/04/24 10:24:47 itojun Exp $	*/
      2 /*	$KAME: if_nameindex.c,v 1.3 2000/04/24 10:08:41 itojun Exp $	*/
      3 
      4 /*-
      5  * Copyright (c) 1997, 2000
      6  *	Berkeley Software Design, Inc.  All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  *
     14  * THIS SOFTWARE IS PROVIDED BY Berkeley Software Design, Inc. ``AS IS'' AND
     15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     17  * ARE DISCLAIMED.  IN NO EVENT SHALL Berkeley Software Design, Inc. BE LIABLE
     18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     24  * SUCH DAMAGE.
     25  *
     26  *	BSDI Id: if_nameindex.c,v 2.3 2000/04/17 22:38:05 dab Exp
     27  */
     28 
     29 #include "namespace.h"
     30 #include <sys/types.h>
     31 #include <sys/socket.h>
     32 #include <net/if_dl.h>
     33 #include <net/if.h>
     34 #include <ifaddrs.h>
     35 #include <stdlib.h>
     36 #include <string.h>
     37 
     38 #ifdef __weak_alias
     39 __weak_alias(if_nameindex,_if_nameindex)
     40 __weak_alias(if_freenameindex,_if_freenameindex)
     41 #endif
     42 /*
     43  * From RFC 2133:
     44  *
     45  * 4.3.  Return All Interface Names and Indexes
     46  *
     47  *    The final function returns an array of if_nameindex structures, one
     48  *    structure per interface.
     49  *
     50  *        #include <net/if.h>
     51  *
     52  *        struct if_nameindex {
     53  *          unsigned int   if_index;  / * 1, 2, ... * /
     54  *          char          *if_name;   / * null terminated name: "le0", ... * /
     55  *        };
     56  *
     57  *        struct if_nameindex  *if_nameindex(void);
     58  *
     59  *    The end of the array of structures is indicated by a structure with
     60  *    an if_index of 0 and an if_name of NULL.  The function returns a NULL
     61  *    pointer upon an error.
     62  *
     63  *    The memory used for this array of structures along with the interface
     64  *    names pointed to by the if_name members is obtained dynamically.
     65  *    This memory is freed by the next function.
     66  *
     67  * 4.4.  Free Memory
     68  *
     69  *    The following function frees the dynamic memory that was allocated by
     70  *    if_nameindex().
     71  *
     72  *        #include <net/if.h>
     73  *
     74  *        void  if_freenameindex(struct if_nameindex *ptr);
     75  *
     76  *    The argument to this function must be a pointer that was returned by
     77  *    if_nameindex().
     78  */
     79 
     80 struct if_nameindex *
     81 if_nameindex(void)
     82 {
     83 	struct ifaddrs *ifaddrs, *ifa;
     84 	unsigned int ni;
     85 	int nbytes;
     86 	struct if_nameindex *ifni, *ifni2;
     87 	char *cp;
     88 
     89 	if (getifaddrs(&ifaddrs) < 0)
     90 		return(NULL);
     91 
     92 	/*
     93 	 * First, find out how many interfaces there are, and how
     94 	 * much space we need for the string names.
     95 	 */
     96 	ni = 0;
     97 	nbytes = 0;
     98 	for (ifa = ifaddrs; ifa != NULL; ifa = ifa->ifa_next) {
     99 		if (ifa->ifa_addr &&
    100 		    ifa->ifa_addr->sa_family == AF_LINK) {
    101 			nbytes += strlen(ifa->ifa_name) + 1;
    102 			ni++;
    103 		}
    104 	}
    105 
    106 	/*
    107 	 * Next, allocate a chunk of memory, use the first part
    108 	 * for the array of structures, and the last part for
    109 	 * the strings.
    110 	 */
    111 	cp = malloc(ni*(sizeof(struct if_nameindex) + 1) + nbytes);
    112 	ifni = (struct if_nameindex *)cp;
    113 	if (ifni == NULL)
    114 		goto out;
    115 	cp += ni*(sizeof(struct if_nameindex) + 1);
    116 
    117 	/*
    118 	 * Now just loop through the list of interfaces again,
    119 	 * filling in the if_nameindex array and making copies
    120 	 * of all the strings.
    121 	 */
    122 	ifni2 = ifni;
    123 	for (ifa = ifaddrs; ifa != NULL; ifa = ifa->ifa_next) {
    124 		if (ifa->ifa_addr &&
    125 		    ifa->ifa_addr->sa_family == AF_LINK) {
    126 			ifni2->if_index =
    127 			    ((struct sockaddr_dl*)ifa->ifa_addr)->sdl_index;
    128 			ifni2->if_name = cp;
    129 			strcpy(cp, ifa->ifa_name);
    130 			ifni2++;
    131 			cp += strlen(cp) + 1;
    132 		}
    133 	}
    134 	/*
    135 	 * Finally, don't forget to terminate the array.
    136 	 */
    137 	ifni2->if_index = 0;
    138 	ifni2->if_name = NULL;
    139 out:
    140 	freeifaddrs(ifaddrs);
    141 	return(ifni);
    142 }
    143 
    144 void
    145 if_freenameindex(struct if_nameindex *ptr)
    146 {
    147 	free(ptr);
    148 }
    149