Use BSDi derived if_nametoindex(), if_indextoname(), if_nameindex()
and if_freenameindex(). Obtained from: KAME MFC after: 2 weeks
This commit is contained in:
parent
e812f24f6d
commit
8071d8d70d
@ -10,7 +10,7 @@ SRCS+= addr2ascii.c ascii2addr.c base64.c ether_addr.c getaddrinfo.c \
|
||||
getnetbydns.c getnetbyht.c getnetbynis.c getnetnamadr.c \
|
||||
getproto.c getprotoent.c getprotoname.c getservbyname.c \
|
||||
getservbyport.c getservent.c herror.c hesiod.c inet_addr.c \
|
||||
ifname.c inet_lnaof.c \
|
||||
if_indextoname.c if_nameindex.c if_nametoindex.c inet_lnaof.c \
|
||||
inet_makeaddr.c inet_net_ntop.c inet_net_pton.c inet_neta.c \
|
||||
inet_netof.c inet_network.c inet_ntoa.c inet_ntop.c \
|
||||
inet_pton.c ip6opt.c linkaddr.c map_v4v6.c name6.c ns_addr.c \
|
||||
|
@ -1,26 +1,19 @@
|
||||
.\" Copyright (c) 1983, 1991, 1993
|
||||
.\" The Regents of the University of California. All rights reserved.
|
||||
.\" $KAME: if_indextoname.3,v 1.10 2000/11/24 08:13:51 itojun Exp $
|
||||
.\" BSDI Id: if_indextoname.3,v 2.2 2000/04/17 22:38:05 dab Exp
|
||||
.\"
|
||||
.\" Copyright (c) 1997, 2000
|
||||
.\" Berkeley Software Design, Inc. All rights reserved.
|
||||
.\"
|
||||
.\" Redistribution and use in source and binary forms, with or without
|
||||
.\" modification, are permitted provided that the following conditions
|
||||
.\" are met:
|
||||
.\" 1. Redistributions of source code must retain the above copyright
|
||||
.\" notice, this list of conditions and the following disclaimer.
|
||||
.\" 2. Redistributions in binary form must reproduce the above copyright
|
||||
.\" notice, this list of conditions and the following disclaimer in the
|
||||
.\" documentation and/or other materials provided with the distribution.
|
||||
.\" 3. All advertising materials mentioning features or use of this software
|
||||
.\" must display the following acknowledgement:
|
||||
.\" This product includes software developed by the University of
|
||||
.\" California, Berkeley and its contributors.
|
||||
.\" 4. Neither the name of the University nor the names of its contributors
|
||||
.\" may be used to endorse or promote products derived from this software
|
||||
.\" without specific prior written permission.
|
||||
.\"
|
||||
.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
||||
.\" THIS SOFTWARE IS PROVIDED BY Berkeley Software Design, Inc. ``AS IS'' AND
|
||||
.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
||||
.\" ARE DISCLAIMED. IN NO EVENT SHALL Berkeley Software Design, Inc. BE LIABLE
|
||||
.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
@ -29,10 +22,9 @@
|
||||
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
.\" SUCH DAMAGE.
|
||||
.\"
|
||||
.\" From: @(#)rcmd.3 8.1 (Berkeley) 6/4/93
|
||||
.\" $FreeBSD$
|
||||
.\"
|
||||
.Dd May 21, 1998
|
||||
.Dd "July 11, 1997"
|
||||
.Dt IF_NAMETOINDEX 3
|
||||
.Os
|
||||
.Sh NAME
|
||||
@ -40,103 +32,104 @@
|
||||
.Nm if_indextoname ,
|
||||
.Nm if_nameindex ,
|
||||
.Nm if_freenameindex
|
||||
.Nd convert interface index to name, and vice versa
|
||||
.Sh LIBRARY
|
||||
.Lb libc
|
||||
.Nd provide mappings between interface names and indexes
|
||||
.Sh SYNOPSIS
|
||||
.In sys/types.h
|
||||
.In sys/socket.h
|
||||
.In net/if.h
|
||||
.Ft "unsigned int"
|
||||
.Ft unsigned int
|
||||
.Fn if_nametoindex "const char *ifname"
|
||||
.Ft "char *"
|
||||
.Ft char *
|
||||
.Fn if_indextoname "unsigned int ifindex" "char *ifname"
|
||||
.Ft "struct if_nameindex *"
|
||||
.Ft struct if_nameindex *
|
||||
.Fn if_nameindex "void"
|
||||
.Ft "void"
|
||||
.Ft void
|
||||
.Fn if_freenameindex "struct if_nameindex *ptr"
|
||||
.Sh DESCRIPTION
|
||||
The functions map interface index to readable interface name
|
||||
(such as
|
||||
.Dq Li lo0 ) ,
|
||||
and vice versa.
|
||||
.Pp
|
||||
.Fn if_nametoindex
|
||||
converts readable interface name to interface index
|
||||
.Pp positive integer value .
|
||||
If the specified interface does not exist, 0 will be returned.
|
||||
.Pp
|
||||
.Fn if_indextoname
|
||||
converts interface index to readable interface name.
|
||||
The
|
||||
.Fa ifname
|
||||
argument must point to a buffer of at least
|
||||
.Dv IF_NAMESIZE
|
||||
bytes into which the interface name corresponding to the specified index is
|
||||
returned.
|
||||
.Dv ( IF_NAMESIZE
|
||||
is also defined in
|
||||
.Aq Pa net/if.h
|
||||
and its value includes a terminating null byte at the end of the
|
||||
interface name.)
|
||||
This pointer is also the return value of the function.
|
||||
If there is no interface corresponding to the specified index,
|
||||
.Dv NULL
|
||||
is returned.
|
||||
.Fn if_nametoindex
|
||||
function maps the interface name specified in
|
||||
.Ar ifname
|
||||
to its corresponding index.
|
||||
If the specified interface does not exist, it returns 0.
|
||||
.Pp
|
||||
The
|
||||
.Fn if_indextoname
|
||||
function maps the interface index specified in
|
||||
.Ar ifindex
|
||||
to it corresponding name, which is copied into the
|
||||
buffer pointed to by
|
||||
.Ar ifname ,
|
||||
which must be of at least IFNAMSIZ bytes.
|
||||
This pointer is also the return value of the function.
|
||||
If there is no interface corresponding to the specified
|
||||
index, NULL is returned.
|
||||
.Pp
|
||||
The
|
||||
.Fn if_nameindex
|
||||
returns an array of
|
||||
.Fa if_nameindex
|
||||
structures.
|
||||
.Fa if_nametoindex
|
||||
is also defined in
|
||||
.Aq Pa net/if.h ,
|
||||
and is as follows:
|
||||
.Bd -literal -offset
|
||||
struct if_nameindex {
|
||||
function returns an array of
|
||||
.Nm if_nameindex
|
||||
structures, one structure per interface, as
|
||||
defined in the include file
|
||||
.Aq Pa net/if.h .
|
||||
The
|
||||
.Nm if_nameindex
|
||||
structure contains at least the following entries:
|
||||
.Bd -literal
|
||||
unsigned int if_index; /* 1, 2, ... */
|
||||
char *if_name; /* null terminated name: "le0", ... */
|
||||
};
|
||||
.Ed
|
||||
.Pp
|
||||
The end of the array of structures is indicated by a structure with
|
||||
an
|
||||
.Fa if_index
|
||||
The end of the array of structures is indicated by a structure with an
|
||||
.Nm if_index
|
||||
of 0 and an
|
||||
.Fa if_name
|
||||
of
|
||||
.Dv NULL .
|
||||
The function returns a
|
||||
.Dv NULL
|
||||
pointer upon an error.
|
||||
The memory used for this array of structures along with the interface
|
||||
names pointed to by the
|
||||
.Fa if_name
|
||||
members is obtained dynamically.
|
||||
This memory is freed by the
|
||||
.Fn if_freenameindex
|
||||
function.
|
||||
.Nm if_name
|
||||
of NULL.
|
||||
A NULL pointer is returned upon an error.
|
||||
.Pp
|
||||
The
|
||||
.Fn if_freenameindex
|
||||
takes a pointer that was returned by
|
||||
.Fn if_nameindex
|
||||
as argument
|
||||
.Pq Fa ptr ,
|
||||
and it reclaims the region allocated.
|
||||
.Sh DIAGNOSTICS
|
||||
function frees the dynamic memory that was
|
||||
allocated by
|
||||
.Fn if_nameindex .
|
||||
.Sh RETURN VALUES
|
||||
Upon successful completion,
|
||||
.Fn if_nametoindex
|
||||
returns 0 on error, positive integer on success.
|
||||
returns the index number of the interface.
|
||||
If the interface is not found, a value of 0 is returned and
|
||||
.Va errno
|
||||
is set to
|
||||
.Er ENXIO .
|
||||
A value of 0 is also returned if an error
|
||||
occurs while retrieving the list of interfaces via
|
||||
.Xr getifaddrs 3 .
|
||||
.Pp
|
||||
Upon successful completion,
|
||||
.Fn if_indextoname
|
||||
and
|
||||
returns
|
||||
.Ar ifname .
|
||||
If the interface is not found, a NULL pointer is returned and
|
||||
.Va errno
|
||||
is set to
|
||||
.Er ENXIO .
|
||||
A NULL pointer is also returned if an error
|
||||
occurs while retrieving the list of interfaces via
|
||||
.Xr getifaddrs 3 .
|
||||
.Pp
|
||||
The
|
||||
.Fn if_nameindex
|
||||
return
|
||||
.Dv NULL
|
||||
on errors.
|
||||
returns a NULL pointer if an error
|
||||
occurs while retrieving the list of interfaces via
|
||||
.Xr getifaddrs 3 ,
|
||||
or if sufficient memory cannot be allocated.
|
||||
.Sh SEE ALSO
|
||||
R. Gilligan, S. Thomson, J. Bound, and W. Stevens,
|
||||
``Basic Socket Interface Extensions for IPv6,'' RFC2553, March 1999.
|
||||
.Sh HISTORY
|
||||
The implementation first appeared in WIDE Hydrangea IPv6 protocol stack kit.
|
||||
.Xr getifaddrs 3 ,
|
||||
.Xr networking 4
|
||||
.Sh STANDARDS
|
||||
These functions are defined in ``Basic Socket Interface Extensions for IPv6''
|
||||
(RFC2533).
|
||||
The
|
||||
.Fn if_nametoindex ,
|
||||
.Fn if_indextoname ,
|
||||
.Fn if_nameindex ,
|
||||
and
|
||||
.Fn if_freenameindex
|
||||
functions conform to RFC 2553.
|
||||
.Sh HISTORY
|
||||
The implementation first appeared in BSDI BSD/OS.
|
||||
|
88
lib/libc/net/if_indextoname.c
Normal file
88
lib/libc/net/if_indextoname.c
Normal file
@ -0,0 +1,88 @@
|
||||
/* $KAME: if_indextoname.c,v 1.7 2000/11/08 03:09:30 itojun Exp $ */
|
||||
|
||||
/*-
|
||||
* Copyright (c) 1997, 2000
|
||||
* Berkeley Software Design, Inc. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY Berkeley Software Design, Inc. ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL Berkeley Software Design, Inc. BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*
|
||||
* BSDI Id: if_indextoname.c,v 2.3 2000/04/17 22:38:05 dab Exp
|
||||
*/
|
||||
|
||||
#include <sys/cdefs.h>
|
||||
__FBSDID("$FreeBSD$");
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <net/if_dl.h>
|
||||
#include <net/if.h>
|
||||
#include <ifaddrs.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
|
||||
/*
|
||||
* From RFC 2533:
|
||||
*
|
||||
* The second function maps an interface index into its corresponding
|
||||
* name.
|
||||
*
|
||||
* #include <net/if.h>
|
||||
*
|
||||
* char *if_indextoname(unsigned int ifindex, char *ifname);
|
||||
*
|
||||
* The ifname argument must point to a buffer of at least IF_NAMESIZE
|
||||
* bytes into which the interface name corresponding to the specified
|
||||
* index is returned. (IF_NAMESIZE is also defined in <net/if.h> and
|
||||
* its value includes a terminating null byte at the end of the
|
||||
* interface name.) This pointer is also the return value of the
|
||||
* function. If there is no interface corresponding to the specified
|
||||
* index, NULL is returned, and errno is set to ENXIO, if there was a
|
||||
* system error (such as running out of memory), if_indextoname returns
|
||||
* NULL and errno would be set to the proper value (e.g., ENOMEM).
|
||||
*/
|
||||
|
||||
char *
|
||||
if_indextoname(unsigned int ifindex, char *ifname)
|
||||
{
|
||||
struct ifaddrs *ifaddrs, *ifa;
|
||||
int error = 0;
|
||||
|
||||
if (getifaddrs(&ifaddrs) < 0)
|
||||
return(NULL); /* getifaddrs properly set errno */
|
||||
|
||||
for (ifa = ifaddrs; ifa != NULL; ifa = ifa->ifa_next) {
|
||||
if (ifa->ifa_addr &&
|
||||
ifa->ifa_addr->sa_family == AF_LINK &&
|
||||
ifindex == ((struct sockaddr_dl*)ifa->ifa_addr)->sdl_index)
|
||||
break;
|
||||
}
|
||||
|
||||
if (ifa == NULL) {
|
||||
error = ENXIO;
|
||||
ifname = NULL;
|
||||
}
|
||||
else
|
||||
strncpy(ifname, ifa->ifa_name, IFNAMSIZ);
|
||||
|
||||
freeifaddrs(ifaddrs);
|
||||
|
||||
errno = error;
|
||||
return(ifname);
|
||||
}
|
147
lib/libc/net/if_nameindex.c
Normal file
147
lib/libc/net/if_nameindex.c
Normal file
@ -0,0 +1,147 @@
|
||||
/* $KAME: if_nameindex.c,v 1.8 2000/11/24 08:20:01 itojun Exp $ */
|
||||
|
||||
/*-
|
||||
* Copyright (c) 1997, 2000
|
||||
* Berkeley Software Design, Inc. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY Berkeley Software Design, Inc. ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL Berkeley Software Design, Inc. BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*
|
||||
* BSDI Id: if_nameindex.c,v 2.3 2000/04/17 22:38:05 dab Exp
|
||||
*/
|
||||
|
||||
#include <sys/cdefs.h>
|
||||
__FBSDID("$FreeBSD$");
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <net/if_dl.h>
|
||||
#include <net/if.h>
|
||||
#include <ifaddrs.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
/*
|
||||
* From RFC 2553:
|
||||
*
|
||||
* 4.3 Return All Interface Names and Indexes
|
||||
*
|
||||
* The if_nameindex structure holds the information about a single
|
||||
* interface and is defined as a result of including the <net/if.h>
|
||||
* header.
|
||||
*
|
||||
* struct if_nameindex {
|
||||
* unsigned int if_index;
|
||||
* char *if_name;
|
||||
* };
|
||||
*
|
||||
* The final function returns an array of if_nameindex structures, one
|
||||
* structure per interface.
|
||||
*
|
||||
* struct if_nameindex *if_nameindex(void);
|
||||
*
|
||||
* The end of the array of structures is indicated by a structure with
|
||||
* an if_index of 0 and an if_name of NULL. The function returns a NULL
|
||||
* pointer upon an error, and would set errno to the appropriate value.
|
||||
*
|
||||
* The memory used for this array of structures along with the interface
|
||||
* names pointed to by the if_name members is obtained dynamically.
|
||||
* This memory is freed by the next function.
|
||||
*
|
||||
* 4.4. Free Memory
|
||||
*
|
||||
* The following function frees the dynamic memory that was allocated by
|
||||
* if_nameindex().
|
||||
*
|
||||
* #include <net/if.h>
|
||||
*
|
||||
* void if_freenameindex(struct if_nameindex *ptr);
|
||||
*
|
||||
* The argument to this function must be a pointer that was returned by
|
||||
* if_nameindex().
|
||||
*/
|
||||
|
||||
struct if_nameindex *
|
||||
if_nameindex(void)
|
||||
{
|
||||
struct ifaddrs *ifaddrs, *ifa;
|
||||
unsigned int ni;
|
||||
int nbytes;
|
||||
struct if_nameindex *ifni, *ifni2;
|
||||
char *cp;
|
||||
|
||||
if (getifaddrs(&ifaddrs) < 0)
|
||||
return(NULL);
|
||||
|
||||
/*
|
||||
* First, find out how many interfaces there are, and how
|
||||
* much space we need for the string names.
|
||||
*/
|
||||
ni = 0;
|
||||
nbytes = 0;
|
||||
for (ifa = ifaddrs; ifa != NULL; ifa = ifa->ifa_next) {
|
||||
if (ifa->ifa_addr &&
|
||||
ifa->ifa_addr->sa_family == AF_LINK) {
|
||||
nbytes += strlen(ifa->ifa_name) + 1;
|
||||
ni++;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Next, allocate a chunk of memory, use the first part
|
||||
* for the array of structures, and the last part for
|
||||
* the strings.
|
||||
*/
|
||||
cp = malloc((ni + 1) * sizeof(struct if_nameindex) + nbytes);
|
||||
ifni = (struct if_nameindex *)cp;
|
||||
if (ifni == NULL)
|
||||
goto out;
|
||||
cp += (ni + 1) * sizeof(struct if_nameindex);
|
||||
|
||||
/*
|
||||
* Now just loop through the list of interfaces again,
|
||||
* filling in the if_nameindex array and making copies
|
||||
* of all the strings.
|
||||
*/
|
||||
ifni2 = ifni;
|
||||
for (ifa = ifaddrs; ifa != NULL; ifa = ifa->ifa_next) {
|
||||
if (ifa->ifa_addr &&
|
||||
ifa->ifa_addr->sa_family == AF_LINK) {
|
||||
ifni2->if_index =
|
||||
((struct sockaddr_dl*)ifa->ifa_addr)->sdl_index;
|
||||
ifni2->if_name = cp;
|
||||
strcpy(cp, ifa->ifa_name);
|
||||
ifni2++;
|
||||
cp += strlen(cp) + 1;
|
||||
}
|
||||
}
|
||||
/*
|
||||
* Finally, don't forget to terminate the array.
|
||||
*/
|
||||
ifni2->if_index = 0;
|
||||
ifni2->if_name = NULL;
|
||||
out:
|
||||
freeifaddrs(ifaddrs);
|
||||
return(ifni);
|
||||
}
|
||||
|
||||
void
|
||||
if_freenameindex(struct if_nameindex *ptr)
|
||||
{
|
||||
free(ptr);
|
||||
}
|
83
lib/libc/net/if_nametoindex.c
Normal file
83
lib/libc/net/if_nametoindex.c
Normal file
@ -0,0 +1,83 @@
|
||||
/* $KAME: if_nametoindex.c,v 1.6 2000/11/24 08:18:54 itojun Exp $ */
|
||||
|
||||
/*-
|
||||
* Copyright (c) 1997, 2000
|
||||
* Berkeley Software Design, Inc. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY Berkeley Software Design, Inc. ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL Berkeley Software Design, Inc. BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*
|
||||
* BSDI Id: if_nametoindex.c,v 2.3 2000/04/17 22:38:05 dab Exp
|
||||
*/
|
||||
|
||||
#include <sys/cdefs.h>
|
||||
__FBSDID("$FreeBSD$");
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <net/if.h>
|
||||
#include <net/if_dl.h>
|
||||
#include <ifaddrs.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
|
||||
/*
|
||||
* From RFC 2553:
|
||||
*
|
||||
* 4.1 Name-to-Index
|
||||
*
|
||||
*
|
||||
* The first function maps an interface name into its corresponding
|
||||
* index.
|
||||
*
|
||||
* #include <net/if.h>
|
||||
*
|
||||
* unsigned int if_nametoindex(const char *ifname);
|
||||
*
|
||||
* If the specified interface name does not exist, the return value is
|
||||
* 0, and errno is set to ENXIO. If there was a system error (such as
|
||||
* running out of memory), the return value is 0 and errno is set to the
|
||||
* proper value (e.g., ENOMEM).
|
||||
*/
|
||||
|
||||
unsigned int
|
||||
if_nametoindex(const char *ifname)
|
||||
{
|
||||
struct ifaddrs *ifaddrs, *ifa;
|
||||
unsigned int ni;
|
||||
|
||||
if (getifaddrs(&ifaddrs) < 0)
|
||||
return(0);
|
||||
|
||||
ni = 0;
|
||||
|
||||
for (ifa = ifaddrs; ifa != NULL; ifa = ifa->ifa_next) {
|
||||
if (ifa->ifa_addr &&
|
||||
ifa->ifa_addr->sa_family == AF_LINK &&
|
||||
strcmp(ifa->ifa_name, ifname) == 0) {
|
||||
ni = ((struct sockaddr_dl*)ifa->ifa_addr)->sdl_index;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
freeifaddrs(ifaddrs);
|
||||
if (!ni)
|
||||
errno = ENXIO;
|
||||
return(ni);
|
||||
}
|
@ -1,236 +0,0 @@
|
||||
/* $KAME: ifname.c,v 1.4 2001/08/20 02:32:40 itojun Exp $ */
|
||||
|
||||
/*
|
||||
* Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of the project nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <sys/cdefs.h>
|
||||
__FBSDID("$FreeBSD$");
|
||||
|
||||
/*
|
||||
* TODO:
|
||||
* - prototype defs into arpa/inet.h, not net/if.h (bsd-api-new-02)
|
||||
*/
|
||||
|
||||
#include <sys/param.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/sockio.h>
|
||||
#include <sys/sysctl.h>
|
||||
#include <net/if.h>
|
||||
#include <net/route.h>
|
||||
#include <net/if_dl.h>
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define ROUNDUP(a) \
|
||||
((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
|
||||
#define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
|
||||
|
||||
static unsigned int
|
||||
if_onametoindex(ifname)
|
||||
const char *ifname;
|
||||
{
|
||||
struct if_nameindex *iff = if_nameindex(), *ifx;
|
||||
int ret;
|
||||
|
||||
if (iff == NULL) return 0;
|
||||
ifx = iff;
|
||||
while (ifx->if_name != NULL) {
|
||||
if (strcmp(ifx->if_name, ifname) == 0) {
|
||||
ret = ifx->if_index;
|
||||
if_freenameindex(iff);
|
||||
return ret;
|
||||
}
|
||||
ifx++;
|
||||
}
|
||||
if_freenameindex(iff);
|
||||
errno = ENXIO;
|
||||
return 0;
|
||||
}
|
||||
|
||||
unsigned int
|
||||
if_nametoindex(ifname)
|
||||
const char *ifname;
|
||||
{
|
||||
int s;
|
||||
struct ifreq ifr;
|
||||
|
||||
s = socket(AF_INET, SOCK_DGRAM, 0);
|
||||
if (s == -1)
|
||||
return (0);
|
||||
strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
|
||||
if (ioctl(s, SIOCGIFINDEX, &ifr) == -1) {
|
||||
close (s);
|
||||
return (if_onametoindex(ifname));
|
||||
}
|
||||
close(s);
|
||||
return (ifr.ifr_index);
|
||||
}
|
||||
|
||||
char *
|
||||
if_indextoname(ifindex, ifname)
|
||||
unsigned int ifindex;
|
||||
char *ifname; /* at least IF_NAMESIZE */
|
||||
{
|
||||
struct if_nameindex *iff = if_nameindex(), *ifx;
|
||||
char *cp, *dp;
|
||||
|
||||
if (iff == NULL) return NULL;
|
||||
ifx = iff;
|
||||
while (ifx->if_index != 0) {
|
||||
if (ifx->if_index == ifindex) {
|
||||
cp = ifname;
|
||||
dp = ifx->if_name;
|
||||
while ((*cp++ = *dp++)) ;
|
||||
if_freenameindex(iff);
|
||||
return (ifname);
|
||||
}
|
||||
ifx++;
|
||||
}
|
||||
if_freenameindex(iff);
|
||||
errno = ENXIO;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct if_nameindex *
|
||||
if_nameindex()
|
||||
{
|
||||
size_t needed;
|
||||
int mib[6], i, ifn = 0, off = 0, hlen;
|
||||
char *buf = NULL, *lim, *next, *cp, *ifbuf = NULL;
|
||||
struct rt_msghdr *rtm;
|
||||
struct if_msghdr *ifm;
|
||||
struct sockaddr *sa;
|
||||
struct sockaddr_dl *sdl;
|
||||
struct if_nameindex *ret = NULL;
|
||||
static int ifxs = 64; /* initial upper limit */
|
||||
struct _ifx {
|
||||
int if_index;
|
||||
int if_off;
|
||||
} *ifx = NULL;
|
||||
|
||||
mib[0] = CTL_NET;
|
||||
mib[1] = PF_ROUTE;
|
||||
mib[2] = 0; /* protocol */
|
||||
mib[3] = 0; /* wildcard address family */
|
||||
mib[4] = NET_RT_IFLIST;
|
||||
mib[5] = 0; /* no flags */
|
||||
if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
|
||||
return NULL;
|
||||
if ((buf = malloc(needed)) == NULL) {
|
||||
errno = ENOMEM;
|
||||
goto end;
|
||||
}
|
||||
/* XXX: we may have allocated too much than necessary */
|
||||
if ((ifbuf = malloc(needed)) == NULL) {
|
||||
errno = ENOMEM;
|
||||
goto end;
|
||||
}
|
||||
if ((ifx = (struct _ifx *)malloc(sizeof(*ifx) * ifxs)) == NULL) {
|
||||
errno = ENOMEM;
|
||||
goto end;
|
||||
}
|
||||
if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) {
|
||||
/* sysctl has set errno */
|
||||
goto end;
|
||||
}
|
||||
lim = buf + needed;
|
||||
for (next = buf; next < lim; next += rtm->rtm_msglen) {
|
||||
rtm = (struct rt_msghdr *)next;
|
||||
if (rtm->rtm_version != RTM_VERSION) {
|
||||
errno = EPROTONOSUPPORT;
|
||||
goto end;
|
||||
}
|
||||
switch (rtm->rtm_type) {
|
||||
case RTM_IFINFO:
|
||||
ifm = (struct if_msghdr *)rtm;
|
||||
ifx[ifn].if_index = ifm->ifm_index;
|
||||
ifx[ifn].if_off = off;
|
||||
cp = (char *)(ifm + 1);
|
||||
for (i = 1; i; i <<= 1) {
|
||||
if (i & ifm->ifm_addrs) {
|
||||
sa = (struct sockaddr *)cp;
|
||||
if (i == RTA_IFP &&
|
||||
sa->sa_family == AF_LINK) {
|
||||
sdl = (struct sockaddr_dl *)sa;
|
||||
memcpy(ifbuf + off,
|
||||
sdl->sdl_data,
|
||||
sdl->sdl_nlen);
|
||||
off += sdl->sdl_nlen;
|
||||
*(ifbuf + off) = '\0';
|
||||
off++;
|
||||
}
|
||||
ADVANCE(cp, sa);
|
||||
}
|
||||
}
|
||||
if (++ifn == ifxs) {
|
||||
/* we need more memory */
|
||||
struct _ifx *newifx;
|
||||
|
||||
ifxs *= 2;
|
||||
if ((newifx = (struct _ifx *)malloc(sizeof(*newifx) * ifxs)) == NULL) {
|
||||
errno = ENOMEM;
|
||||
goto end;
|
||||
}
|
||||
|
||||
/* copy and free old data */
|
||||
memcpy(newifx, ifx, (sizeof(*ifx) * ifxs) / 2);
|
||||
free(ifx);
|
||||
ifx = newifx;
|
||||
}
|
||||
}
|
||||
}
|
||||
hlen = sizeof(struct if_nameindex) * (ifn + 1);
|
||||
if ((cp = (char *)malloc(hlen + off)) == NULL) {
|
||||
errno = ENOMEM;
|
||||
goto end;
|
||||
}
|
||||
bcopy(ifbuf, cp + hlen, off);
|
||||
ret = (struct if_nameindex *)cp;
|
||||
for (i = 0; i < ifn; i++) {
|
||||
ret[i].if_index = ifx[i].if_index;
|
||||
ret[i].if_name = cp + hlen + ifx[i].if_off;
|
||||
}
|
||||
ret[ifn].if_index = 0;
|
||||
ret[ifn].if_name = NULL;
|
||||
|
||||
end:
|
||||
if (buf) free(buf);
|
||||
if (ifbuf) free(ifbuf);
|
||||
if (ifx) free(ifx);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
void if_freenameindex(ptr)
|
||||
struct if_nameindex *ptr;
|
||||
{
|
||||
free(ptr);
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user