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