tools/adflib: build only host variant which is used by Sam440 target
[AROS.git] / workbench / network / stacks / AROSTCP / bsdsocket / api / if_nametoindex.c
blob1f3ab30682591f1c19b2ccfe8ff13a1014d12f02
1 /* $KAME: if_nametoindex.c,v 1.0 2005/11/14 16:23:54 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_nametoindex.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 <sys/sockio.h>
38 #include <net/route.h>
39 #include <net/if.h>
40 #include <net/if_dl.h>
41 #include <net/if_protos.h>
42 #include <api/amiga_api.h>
43 #include <api/ifaddrs.h>
44 #include <string.h>
45 #include <errno.h>
47 #include "miami_api.h"
50 * From RFC 2553:
52 * 4.1 Name-to-Index
55 * The first function maps an interface name into its corresponding
56 * index.
58 * #include <net/if.h>
60 * unsigned int if_nametoindex(const char *ifname);
62 * If the specified interface name does not exist, the return value is
63 * 0, and errno is set to ENXIO. If there was a system error (such as
64 * running out of memory), the return value is 0 and errno is set to the
65 * proper value (e.g., ENOMEM).
68 unsigned int
69 __if_nametoindex(char *ifname, struct SocketBase *SocketBase)
71 // int s;
72 struct ifnet *ifp;
73 struct ifaddrs *ifaddrs, *ifa;
74 unsigned int ni;
76 ifp = ifunit(ifname);
77 if (ifp)
78 return ifp->if_index;
80 if (getifaddrs(&ifaddrs, SocketBase) < 0)
81 return(0);
83 ni = 0;
85 for (ifa = ifaddrs; ifa != NULL; ifa = ifa->ifa_next) {
86 if (ifa->ifa_addr &&
87 ifa->ifa_addr->sa_family == AF_LINK &&
88 strcmp(ifa->ifa_name, ifname) == 0) {
89 ni = ((struct sockaddr_dl*)ifa->ifa_addr)->sdl_index;
90 break;
94 freeifaddrs(ifaddrs);
95 if (!ni)
96 writeErrnoValue(SocketBase, ENXIO);
97 return(ni);
100 AROS_LH1(LONG, if_nametoindex,
101 AROS_LHA(char *, ifname, A0),
102 struct MiamiBase *, MiamiBase, 46, Miami
105 AROS_LIBFUNC_INIT
107 return __if_nametoindex(ifname, MiamiBase->_SocketBase);
109 AROS_LIBFUNC_EXIT