if_nameindex.c revision 1.3 1 /* $NetBSD: if_nameindex.c,v 1.3 2000/07/06 02:55:45 christos 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 <sys/cdefs.h>
30 #if defined(LIBC_SCCS) && !defined(lint)
31 __RCSID("$NetBSD: if_nameindex.c,v 1.3 2000/07/06 02:55:45 christos Exp $");
32 #endif /* LIBC_SCCS and not lint */
33
34 #include "namespace.h"
35 #include <sys/types.h>
36 #include <sys/socket.h>
37 #include <net/if_dl.h>
38 #include <net/if.h>
39 #include <ifaddrs.h>
40 #include <stdlib.h>
41 #include <string.h>
42
43 #ifdef __weak_alias
44 __weak_alias(if_nameindex,_if_nameindex)
45 __weak_alias(if_freenameindex,_if_freenameindex)
46 #endif
47 /*
48 * From RFC 2133:
49 *
50 * 4.3. Return All Interface Names and Indexes
51 *
52 * The final function returns an array of if_nameindex structures, one
53 * structure per interface.
54 *
55 * #include <net/if.h>
56 *
57 * struct if_nameindex {
58 * unsigned int if_index; / * 1, 2, ... * /
59 * char *if_name; / * null terminated name: "le0", ... * /
60 * };
61 *
62 * struct if_nameindex *if_nameindex(void);
63 *
64 * The end of the array of structures is indicated by a structure with
65 * an if_index of 0 and an if_name of NULL. The function returns a NULL
66 * pointer upon an error.
67 *
68 * The memory used for this array of structures along with the interface
69 * names pointed to by the if_name members is obtained dynamically.
70 * This memory is freed by the next function.
71 *
72 * 4.4. Free Memory
73 *
74 * The following function frees the dynamic memory that was allocated by
75 * if_nameindex().
76 *
77 * #include <net/if.h>
78 *
79 * void if_freenameindex(struct if_nameindex *ptr);
80 *
81 * The argument to this function must be a pointer that was returned by
82 * if_nameindex().
83 */
84
85 struct if_nameindex *
86 if_nameindex(void)
87 {
88 struct ifaddrs *ifaddrs, *ifa;
89 unsigned int ni;
90 int nbytes;
91 struct if_nameindex *ifni, *ifni2;
92 char *cp;
93
94 if (getifaddrs(&ifaddrs) < 0)
95 return(NULL);
96
97 /*
98 * First, find out how many interfaces there are, and how
99 * much space we need for the string names.
100 */
101 ni = 0;
102 nbytes = 0;
103 for (ifa = ifaddrs; ifa != NULL; ifa = ifa->ifa_next) {
104 if (ifa->ifa_addr &&
105 ifa->ifa_addr->sa_family == AF_LINK) {
106 nbytes += strlen(ifa->ifa_name) + 1;
107 ni++;
108 }
109 }
110
111 /*
112 * Next, allocate a chunk of memory, use the first part
113 * for the array of structures, and the last part for
114 * the strings.
115 */
116 cp = malloc(ni*(sizeof(struct if_nameindex) + 1) + nbytes);
117 ifni = (struct if_nameindex *)(void *)cp;
118 if (ifni == NULL)
119 goto out;
120 cp += ni*(sizeof(struct if_nameindex) + 1);
121
122 /*
123 * Now just loop through the list of interfaces again,
124 * filling in the if_nameindex array and making copies
125 * of all the strings.
126 */
127 ifni2 = ifni;
128 for (ifa = ifaddrs; ifa != NULL; ifa = ifa->ifa_next) {
129 if (ifa->ifa_addr &&
130 ifa->ifa_addr->sa_family == AF_LINK) {
131 ifni2->if_index =
132 ((struct sockaddr_dl*)
133 (void *)ifa->ifa_addr)->sdl_index;
134 ifni2->if_name = cp;
135 strcpy(cp, ifa->ifa_name);
136 ifni2++;
137 cp += strlen(cp) + 1;
138 }
139 }
140 /*
141 * Finally, don't forget to terminate the array.
142 */
143 ifni2->if_index = 0;
144 ifni2->if_name = NULL;
145 out:
146 freeifaddrs(ifaddrs);
147 return(ifni);
148 }
149
150 void
151 if_freenameindex(struct if_nameindex *ptr)
152 {
153 free(ptr);
154 }
155