tools/adflib: build only host variant which is used by Sam440 target
[AROS.git] / workbench / network / stacks / AROSTCP / bsdsocket / api / if_nameindex.c
blobbd8135ff3fb500854b135877a5811b5a3a69edc2
1 /* $KAME: if_nameindex.c,v 1.0 2005/11/14 16:49:01 sonic Exp $ */
3 /*-
4 * Copyright (c) 1997, 2000
5 * Berkeley Software Design, Inc. All rights reserved.
6 * Copyright (c) 2005 - 2006
7 * Pavel Fedin
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * THIS SOFTWARE IS PROVIDED BY Berkeley Software Design, Inc. ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL Berkeley Software Design, Inc. BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
27 * BSDI Id: if_nameindex.c,v 2.3 2000/04/17 22:38:05 dab Exp
30 //#include <emul/emulregs.h>
31 #include <exec/libraries.h>
33 #include <conf.h>
34 #include <sys/cdefs.h>
35 #include <sys/types.h>
36 #include <sys/socket.h>
37 #include <net/if_dl.h>
38 #include <net/if.h>
39 #include <api/amiga_api.h>
40 #include <api/ifaddrs.h>
41 #include <string.h>
43 #include "miami_api.h"
46 * From RFC 2553:
48 * 4.3 Return All Interface Names and Indexes
50 * The if_nameindex structure holds the information about a single
51 * interface and is defined as a result of including the <net/if.h>
52 * header.
54 * struct if_nameindex {
55 * unsigned int if_index;
56 * char *if_name;
57 * };
59 * The final function returns an array of if_nameindex structures, one
60 * structure per interface.
62 * struct if_nameindex *if_nameindex(void);
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, and would set errno to the appropriate value.
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.
72 * 4.4. Free Memory
74 * The following function frees the dynamic memory that was allocated by
75 * if_nameindex().
77 * #include <net/if.h>
79 * void if_freenameindex(struct if_nameindex *ptr);
81 * The argument to this function must be a pointer that was returned by
82 * if_nameindex().
85 AROS_LH0(struct if_nameindex *, if_nameindex,
86 struct MiamiBase *, MiamiBase, 48, Miami
89 AROS_LIBFUNC_INIT
91 struct ifaddrs *ifaddrs, *ifa;
92 unsigned int ni;
93 int nbytes;
94 struct if_nameindex *ifni, *ifni2;
95 char *cp;
97 if (getifaddrs(&ifaddrs, MiamiBase->_SocketBase) < 0)
98 return(NULL);
101 * First, find out how many interfaces there are, and how
102 * much space we need for the string names.
104 ni = 0;
105 nbytes = 0;
106 for (ifa = ifaddrs; ifa != NULL; ifa = ifa->ifa_next) {
107 if (ifa->ifa_addr &&
108 ifa->ifa_addr->sa_family == AF_LINK) {
109 nbytes += strlen(ifa->ifa_name) + 1;
110 ni++;
115 * Next, allocate a chunk of memory, use the first part
116 * for the array of structures, and the last part for
117 * the strings.
119 cp = bsd_malloc((ni + 1) * sizeof(struct if_nameindex) + nbytes, NULL, NULL);
120 ifni = (struct if_nameindex *)cp;
121 if (ifni == NULL)
122 goto out;
123 cp += (ni + 1) * sizeof(struct if_nameindex);
126 * Now just loop through the list of interfaces again,
127 * filling in the if_nameindex array and making copies
128 * of all the strings.
130 ifni2 = ifni;
131 for (ifa = ifaddrs; ifa != NULL; ifa = ifa->ifa_next) {
132 if (ifa->ifa_addr &&
133 ifa->ifa_addr->sa_family == AF_LINK) {
134 ifni2->if_index =
135 ((struct sockaddr_dl*)ifa->ifa_addr)->sdl_index;
136 ifni2->if_name = cp;
137 strcpy(cp, ifa->ifa_name);
138 ifni2++;
139 cp += strlen(cp) + 1;
143 * Finally, don't forget to terminate the array.
145 ifni2->if_index = 0;
146 ifni2->if_name = NULL;
147 out:
148 freeifaddrs(ifaddrs);
149 return(ifni);
151 AROS_LIBFUNC_EXIT
154 AROS_LH1(void, if_freenameindex,
155 AROS_LHA(struct if_nameindex **, ptr, D0),
156 struct MiamiBase *, MiamiBase, 49, Miami
159 AROS_LIBFUNC_INIT
161 bsd_free(ptr, NULL);
163 AROS_LIBFUNC_EXIT