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