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