]>
git.saurik.com Git - apple/libinfo.git/blob - gen.subproj/if_nameindex.c
2 * Copyright (c) 2018 Apple Inc. All rights reserved.
4 /* $KAME: if_nameindex.c,v 1.8 2000/11/24 08:20:01 itojun Exp $ */
7 * Copyright (c) 1997, 2000
8 * Berkeley Software Design, Inc. All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
16 * THIS SOFTWARE IS PROVIDED BY Berkeley Software Design, Inc. ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL Berkeley Software Design, Inc. BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * BSDI Id: if_nameindex.c,v 2.3 2000/04/17 22:38:05 dab Exp
31 #include "libinfo_common.h"
33 #include <sys/types.h>
34 #include <sys/socket.h>
35 #include <net/if_dl.h>
40 #include <os/overflow.h>
41 #include <sys/errno.h>
46 * 4.3 Return All Interface Names and Indexes
48 * The if_nameindex structure holds the information about a single
49 * interface and is defined as a result of including the <net/if.h>
52 * struct if_nameindex {
53 * unsigned int if_index;
57 * The final function returns an array of if_nameindex structures, one
58 * structure per interface.
60 * struct if_nameindex *if_nameindex(void);
62 * The end of the array of structures is indicated by a structure with
63 * an if_index of 0 and an if_name of NULL. The function returns a NULL
64 * pointer upon an error, and would set errno to the appropriate value.
66 * The memory used for this array of structures along with the interface
67 * names pointed to by the if_name members is obtained dynamically.
68 * This memory is freed by the next function.
72 * The following function frees the dynamic memory that was allocated by
77 * void if_freenameindex(struct if_nameindex *ptr);
79 * The argument to this function must be a pointer that was returned by
87 struct ifaddrs
*ifaddrs
, *ifa
;
90 struct if_nameindex
*ifni
= NULL
, *ifni2
;
94 if (getifaddrs(&ifaddrs
) < 0)
98 * First, find out how many interfaces there are, and how
99 * much space we need for the string names.
103 for (ifa
= ifaddrs
; ifa
!= NULL
; ifa
= ifa
->ifa_next
) {
105 ifa
->ifa_addr
->sa_family
== AF_LINK
) {
107 * Security Check: Verify nbytes and ni do not overflow
109 if (os_add_overflow(nbytes
, strlen(ifa
->ifa_name
) + 1, &nbytes
) ||
110 os_add_overflow(ni
, 1, &ni
)) {
118 * Security Check: Verify cpsz does not overflow in next 3 operations
120 if (os_add_overflow(ni
, 1, &cpsz
)) {
124 if (os_mul_overflow(cpsz
, sizeof(struct if_nameindex
), &cpsz
)) {
128 if (os_add_overflow(cpsz
, nbytes
, &cpsz
)) {
133 * Next, allocate a chunk of memory, use the first part
134 * for the array of structures, and the last part for
138 ifni
= (struct if_nameindex
*)cp
;
141 cp
+= (ni
+ 1) * sizeof(struct if_nameindex
);
144 * Now just loop through the list of interfaces again,
145 * filling in the if_nameindex array and making copies
146 * of all the strings.
149 for (ifa
= ifaddrs
; ifa
!= NULL
; ifa
= ifa
->ifa_next
) {
151 ifa
->ifa_addr
->sa_family
== AF_LINK
) {
153 ((struct sockaddr_dl
*)ifa
->ifa_addr
)->sdl_index
;
155 strcpy(cp
, ifa
->ifa_name
);
157 cp
+= strlen(cp
) + 1;
161 * Finally, don't forget to terminate the array.
164 ifni2
->if_name
= NULL
;
166 freeifaddrs(ifaddrs
);
173 if_freenameindex(struct if_nameindex
*ptr
)