18071d8d7SHajimu UMEMOTO /* $KAME: if_nameindex.c,v 1.8 2000/11/24 08:20:01 itojun Exp $ */
28071d8d7SHajimu UMEMOTO
38071d8d7SHajimu UMEMOTO /*-
4*9b10f59aSPedro F. Giffuni * SPDX-License-Identifier: BSD-1-Clause
5*9b10f59aSPedro F. Giffuni *
68071d8d7SHajimu UMEMOTO * Copyright (c) 1997, 2000
78071d8d7SHajimu UMEMOTO * Berkeley Software Design, Inc. All rights reserved.
88071d8d7SHajimu UMEMOTO *
98071d8d7SHajimu UMEMOTO * Redistribution and use in source and binary forms, with or without
108071d8d7SHajimu UMEMOTO * modification, are permitted provided that the following conditions
118071d8d7SHajimu UMEMOTO * are met:
128071d8d7SHajimu UMEMOTO * 1. Redistributions of source code must retain the above copyright
138071d8d7SHajimu UMEMOTO * notice, this list of conditions and the following disclaimer.
148071d8d7SHajimu UMEMOTO *
158071d8d7SHajimu UMEMOTO * THIS SOFTWARE IS PROVIDED BY Berkeley Software Design, Inc. ``AS IS'' AND
168071d8d7SHajimu UMEMOTO * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
178071d8d7SHajimu UMEMOTO * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
188071d8d7SHajimu UMEMOTO * ARE DISCLAIMED. IN NO EVENT SHALL Berkeley Software Design, Inc. BE LIABLE
198071d8d7SHajimu UMEMOTO * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
208071d8d7SHajimu UMEMOTO * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
218071d8d7SHajimu UMEMOTO * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
228071d8d7SHajimu UMEMOTO * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
238071d8d7SHajimu UMEMOTO * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
248071d8d7SHajimu UMEMOTO * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
258071d8d7SHajimu UMEMOTO * SUCH DAMAGE.
268071d8d7SHajimu UMEMOTO *
278071d8d7SHajimu UMEMOTO * BSDI Id: if_nameindex.c,v 2.3 2000/04/17 22:38:05 dab Exp
288071d8d7SHajimu UMEMOTO */
298071d8d7SHajimu UMEMOTO
308071d8d7SHajimu UMEMOTO #include <sys/types.h>
318071d8d7SHajimu UMEMOTO #include <sys/socket.h>
328071d8d7SHajimu UMEMOTO #include <net/if_dl.h>
338071d8d7SHajimu UMEMOTO #include <net/if.h>
348071d8d7SHajimu UMEMOTO #include <ifaddrs.h>
358071d8d7SHajimu UMEMOTO #include <stdlib.h>
368071d8d7SHajimu UMEMOTO #include <string.h>
378071d8d7SHajimu UMEMOTO
388071d8d7SHajimu UMEMOTO /*
398071d8d7SHajimu UMEMOTO * From RFC 2553:
408071d8d7SHajimu UMEMOTO *
418071d8d7SHajimu UMEMOTO * 4.3 Return All Interface Names and Indexes
428071d8d7SHajimu UMEMOTO *
438071d8d7SHajimu UMEMOTO * The if_nameindex structure holds the information about a single
448071d8d7SHajimu UMEMOTO * interface and is defined as a result of including the <net/if.h>
458071d8d7SHajimu UMEMOTO * header.
468071d8d7SHajimu UMEMOTO *
478071d8d7SHajimu UMEMOTO * struct if_nameindex {
488071d8d7SHajimu UMEMOTO * unsigned int if_index;
498071d8d7SHajimu UMEMOTO * char *if_name;
508071d8d7SHajimu UMEMOTO * };
518071d8d7SHajimu UMEMOTO *
528071d8d7SHajimu UMEMOTO * The final function returns an array of if_nameindex structures, one
538071d8d7SHajimu UMEMOTO * structure per interface.
548071d8d7SHajimu UMEMOTO *
558071d8d7SHajimu UMEMOTO * struct if_nameindex *if_nameindex(void);
568071d8d7SHajimu UMEMOTO *
578071d8d7SHajimu UMEMOTO * The end of the array of structures is indicated by a structure with
588071d8d7SHajimu UMEMOTO * an if_index of 0 and an if_name of NULL. The function returns a NULL
598071d8d7SHajimu UMEMOTO * pointer upon an error, and would set errno to the appropriate value.
608071d8d7SHajimu UMEMOTO *
618071d8d7SHajimu UMEMOTO * The memory used for this array of structures along with the interface
628071d8d7SHajimu UMEMOTO * names pointed to by the if_name members is obtained dynamically.
638071d8d7SHajimu UMEMOTO * This memory is freed by the next function.
648071d8d7SHajimu UMEMOTO *
658071d8d7SHajimu UMEMOTO * 4.4. Free Memory
668071d8d7SHajimu UMEMOTO *
678071d8d7SHajimu UMEMOTO * The following function frees the dynamic memory that was allocated by
688071d8d7SHajimu UMEMOTO * if_nameindex().
698071d8d7SHajimu UMEMOTO *
708071d8d7SHajimu UMEMOTO * #include <net/if.h>
718071d8d7SHajimu UMEMOTO *
728071d8d7SHajimu UMEMOTO * void if_freenameindex(struct if_nameindex *ptr);
738071d8d7SHajimu UMEMOTO *
748071d8d7SHajimu UMEMOTO * The argument to this function must be a pointer that was returned by
758071d8d7SHajimu UMEMOTO * if_nameindex().
768071d8d7SHajimu UMEMOTO */
778071d8d7SHajimu UMEMOTO
788071d8d7SHajimu UMEMOTO struct if_nameindex *
if_nameindex(void)798071d8d7SHajimu UMEMOTO if_nameindex(void)
808071d8d7SHajimu UMEMOTO {
818071d8d7SHajimu UMEMOTO struct ifaddrs *ifaddrs, *ifa;
828071d8d7SHajimu UMEMOTO unsigned int ni;
838071d8d7SHajimu UMEMOTO int nbytes;
848071d8d7SHajimu UMEMOTO struct if_nameindex *ifni, *ifni2;
858071d8d7SHajimu UMEMOTO char *cp;
868071d8d7SHajimu UMEMOTO
878071d8d7SHajimu UMEMOTO if (getifaddrs(&ifaddrs) < 0)
888071d8d7SHajimu UMEMOTO return(NULL);
898071d8d7SHajimu UMEMOTO
908071d8d7SHajimu UMEMOTO /*
918071d8d7SHajimu UMEMOTO * First, find out how many interfaces there are, and how
928071d8d7SHajimu UMEMOTO * much space we need for the string names.
938071d8d7SHajimu UMEMOTO */
948071d8d7SHajimu UMEMOTO ni = 0;
958071d8d7SHajimu UMEMOTO nbytes = 0;
968071d8d7SHajimu UMEMOTO for (ifa = ifaddrs; ifa != NULL; ifa = ifa->ifa_next) {
978071d8d7SHajimu UMEMOTO if (ifa->ifa_addr &&
988071d8d7SHajimu UMEMOTO ifa->ifa_addr->sa_family == AF_LINK) {
998071d8d7SHajimu UMEMOTO nbytes += strlen(ifa->ifa_name) + 1;
1008071d8d7SHajimu UMEMOTO ni++;
1018071d8d7SHajimu UMEMOTO }
1028071d8d7SHajimu UMEMOTO }
1038071d8d7SHajimu UMEMOTO
1048071d8d7SHajimu UMEMOTO /*
1058071d8d7SHajimu UMEMOTO * Next, allocate a chunk of memory, use the first part
1068071d8d7SHajimu UMEMOTO * for the array of structures, and the last part for
1078071d8d7SHajimu UMEMOTO * the strings.
1088071d8d7SHajimu UMEMOTO */
1098071d8d7SHajimu UMEMOTO cp = malloc((ni + 1) * sizeof(struct if_nameindex) + nbytes);
1108071d8d7SHajimu UMEMOTO ifni = (struct if_nameindex *)cp;
1118071d8d7SHajimu UMEMOTO if (ifni == NULL)
1128071d8d7SHajimu UMEMOTO goto out;
1138071d8d7SHajimu UMEMOTO cp += (ni + 1) * sizeof(struct if_nameindex);
1148071d8d7SHajimu UMEMOTO
1158071d8d7SHajimu UMEMOTO /*
1168071d8d7SHajimu UMEMOTO * Now just loop through the list of interfaces again,
1178071d8d7SHajimu UMEMOTO * filling in the if_nameindex array and making copies
1188071d8d7SHajimu UMEMOTO * of all the strings.
1198071d8d7SHajimu UMEMOTO */
1208071d8d7SHajimu UMEMOTO ifni2 = ifni;
1218071d8d7SHajimu UMEMOTO for (ifa = ifaddrs; ifa != NULL; ifa = ifa->ifa_next) {
1228071d8d7SHajimu UMEMOTO if (ifa->ifa_addr &&
1238071d8d7SHajimu UMEMOTO ifa->ifa_addr->sa_family == AF_LINK) {
1248071d8d7SHajimu UMEMOTO ifni2->if_index =
125cf0d539fSMarcel Moolenaar LLINDEX((struct sockaddr_dl*)ifa->ifa_addr);
1268071d8d7SHajimu UMEMOTO ifni2->if_name = cp;
1278071d8d7SHajimu UMEMOTO strcpy(cp, ifa->ifa_name);
1288071d8d7SHajimu UMEMOTO ifni2++;
1298071d8d7SHajimu UMEMOTO cp += strlen(cp) + 1;
1308071d8d7SHajimu UMEMOTO }
1318071d8d7SHajimu UMEMOTO }
1328071d8d7SHajimu UMEMOTO /*
1338071d8d7SHajimu UMEMOTO * Finally, don't forget to terminate the array.
1348071d8d7SHajimu UMEMOTO */
1358071d8d7SHajimu UMEMOTO ifni2->if_index = 0;
1368071d8d7SHajimu UMEMOTO ifni2->if_name = NULL;
1378071d8d7SHajimu UMEMOTO out:
1388071d8d7SHajimu UMEMOTO freeifaddrs(ifaddrs);
1398071d8d7SHajimu UMEMOTO return(ifni);
1408071d8d7SHajimu UMEMOTO }
1418071d8d7SHajimu UMEMOTO
1428071d8d7SHajimu UMEMOTO void
if_freenameindex(struct if_nameindex * ptr)1438071d8d7SHajimu UMEMOTO if_freenameindex(struct if_nameindex *ptr)
1448071d8d7SHajimu UMEMOTO {
1458071d8d7SHajimu UMEMOTO free(ptr);
1468071d8d7SHajimu UMEMOTO }
147