use BSDI-origin if_{index,name}* functions. they use getifaddrs(3) as
backend and are more robust against SIOCGIFCONF alignment issue. now getifaddrs always takes care of the issue. (sync with kame tree) use weak symbol just in case.
This commit is contained in:
parent
c6bf4b09bd
commit
92db021bdc
|
@ -1,4 +1,4 @@
|
|||
/* $NetBSD: namespace.h,v 1.49 2000/04/24 09:27:30 itojun Exp $ */
|
||||
/* $NetBSD: namespace.h,v 1.50 2000/04/24 10:24:46 itojun Exp $ */
|
||||
|
||||
/*-
|
||||
* Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
|
||||
|
@ -233,6 +233,10 @@
|
|||
#define hesiod_init _hesiod_init
|
||||
#define hesiod_resolve _hesiod_resolve
|
||||
#define hesiod_to_bind _hesiod_to_bind
|
||||
#define if_freenameindex _if_freenameindex
|
||||
#define if_indextoname _if_indextoname
|
||||
#define if_nameindex _if_nameindex
|
||||
#define if_nametoindex _if_nametoindex
|
||||
#define in6addr_any _in6addr_any
|
||||
#define in6addr_linklocal_allnodes _in6addr_linklocal_allnodes
|
||||
#define in6addr_loopback _in6addr_loopback
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
# $NetBSD: Makefile.inc,v 1.53 2000/03/14 14:34:50 sjg Exp $
|
||||
# $NetBSD: Makefile.inc,v 1.54 2000/04/24 10:24:47 itojun Exp $
|
||||
# @(#)Makefile.inc 8.2 (Berkeley) 9/5/93
|
||||
|
||||
# net sources
|
||||
|
@ -18,10 +18,8 @@ SRCS+= __dn_comp.c __res_close.c __res_send.c _inet_aton.c _inet_pton.c
|
|||
CPPFLAGS+=-DRESOLVSORT -I.
|
||||
|
||||
# IPv6
|
||||
SRCS+= getaddrinfo.c getnameinfo.c ifname.c ip6opt.c rthdr.c vars6.c
|
||||
# need to revisit
|
||||
#SRCS+= name6.c freehostent.c
|
||||
#CPPFLAGS+= -DUSE_GETIPNODEBY
|
||||
SRCS+= getaddrinfo.c getnameinfo.c ip6opt.c rthdr.c vars6.c
|
||||
SRCS+= if_indextoname.c if_nameindex.c if_nametoindex.c
|
||||
|
||||
LPREFIX=_nsyy
|
||||
YPREFIX=_nsyy
|
||||
|
|
|
@ -1,26 +1,20 @@
|
|||
.\" Copyright (c) 1983, 1991, 1993
|
||||
.\" The Regents of the University of California. All rights reserved.
|
||||
.\" $NetBSD: if_indextoname.3,v 1.5 2000/04/24 10:24:47 itojun Exp $
|
||||
.\" $KAME: if_indextoname.3,v 1.8 2000/04/24 10:12:36 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 +23,7 @@
|
|||
.\" 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
|
||||
.\" KAME Id: if_indextoname.3,v 1.4 1999/12/10 04:07:00 itojun Exp
|
||||
.\"
|
||||
.Dd May 21, 1998
|
||||
.Dd "July 11, 1997"
|
||||
.Dt IF_NAMETOINDEX 3
|
||||
.Os
|
||||
.Sh NAME
|
||||
|
@ -40,119 +31,95 @@
|
|||
.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
|
||||
.Fd #include <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
|
||||
.Po
|
||||
such as
|
||||
.Dq Li lo0
|
||||
.Pc
|
||||
, and vice versa.
|
||||
.Pp
|
||||
The
|
||||
.Fn if_nametoindex
|
||||
function
|
||||
converts an interface name specified by the
|
||||
.Fa ifname
|
||||
argument to an interface index
|
||||
.Pq positive integer value .
|
||||
If the specified interface does not exist, 0 will be returned.
|
||||
function maps the interface name specified in
|
||||
.Ar ifname
|
||||
to its corresponding index.
|
||||
If the specified interface does not exist, it returns 0.
|
||||
.Pp
|
||||
.Fn if_indextoname
|
||||
converts an interface index specified by the
|
||||
.Fa ifindex
|
||||
argument to an 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.
|
||||
.Po
|
||||
.Dv IF_NAMESIZE
|
||||
is also defined in
|
||||
.Aq Li net/if.h
|
||||
and its value includes a terminating null byte at the end of the
|
||||
interface name.
|
||||
.Pc
|
||||
.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,
|
||||
.Dv NULL
|
||||
is returned.
|
||||
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 Li 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.
|
||||
A value of 0 is returned if the interface is not found or an error
|
||||
occurs while retrieving the list of interfaces via
|
||||
.Xr getifaddrs 3 .
|
||||
.Pp
|
||||
Upon successful completion,
|
||||
.Fn if_indextoname
|
||||
and
|
||||
returns
|
||||
.Ar ifname .
|
||||
A NULL pointer is returned if the interface is not found or 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
|
||||
.Rs
|
||||
.%A R. Gilligan
|
||||
.%A S. Thomson
|
||||
.%A J. Bound
|
||||
.%A W. Stevens
|
||||
.%T Basic Socket Interface Extensions for IPv6
|
||||
.%R RFC2553
|
||||
.%D March 1999
|
||||
.Re
|
||||
.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
|
||||
.Dq Basic Socket Interface Extensions for IPv6
|
||||
.Pq 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.
|
||||
|
|
|
@ -0,0 +1,85 @@
|
|||
/* $NetBSD: if_indextoname.c,v 1.1 2000/04/24 10:24:47 itojun Exp $ */
|
||||
/* $KAME: if_indextoname.c,v 1.4 2000/04/24 10:08:41 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 "namespace.h"
|
||||
#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>
|
||||
|
||||
#ifdef __weak_alias
|
||||
__weak_alias(if_indextoname,_if_indextoname)
|
||||
#endif
|
||||
|
||||
/*
|
||||
* From RFC 2133:
|
||||
*
|
||||
* 4.2. Index-to-Name
|
||||
*
|
||||
* 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 IFNAMSIZ bytes
|
||||
* into which the interface name corresponding to the specified index is
|
||||
* returned. (IFNAMSIZ 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.
|
||||
*/
|
||||
|
||||
char *
|
||||
if_indextoname(unsigned int ifindex, char *ifname)
|
||||
{
|
||||
struct ifaddrs *ifaddrs, *ifa;
|
||||
|
||||
if (getifaddrs(&ifaddrs) < 0)
|
||||
return(NULL);
|
||||
|
||||
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)
|
||||
ifname = NULL;
|
||||
else
|
||||
strncpy(ifname, ifa->ifa_name, IFNAMSIZ);
|
||||
|
||||
freeifaddrs(ifaddrs);
|
||||
|
||||
return(ifname);
|
||||
}
|
|
@ -0,0 +1,148 @@
|
|||
/* $NetBSD: if_nameindex.c,v 1.1 2000/04/24 10:24:47 itojun Exp $ */
|
||||
/* $KAME: if_nameindex.c,v 1.3 2000/04/24 10:08:41 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 "namespace.h"
|
||||
#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>
|
||||
|
||||
#ifdef __weak_alias
|
||||
__weak_alias(if_nameindex,_if_nameindex)
|
||||
__weak_alias(if_freenameindex,_if_freenameindex)
|
||||
#endif
|
||||
/*
|
||||
* From RFC 2133:
|
||||
*
|
||||
* 4.3. Return All Interface Names and Indexes
|
||||
*
|
||||
* The final function returns an array of if_nameindex structures, one
|
||||
* structure per interface.
|
||||
*
|
||||
* #include <net/if.h>
|
||||
*
|
||||
* struct if_nameindex {
|
||||
* unsigned int if_index; / * 1, 2, ... * /
|
||||
* char *if_name; / * null terminated name: "le0", ... * /
|
||||
* };
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* 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*(sizeof(struct if_nameindex) + 1) + nbytes);
|
||||
ifni = (struct if_nameindex *)cp;
|
||||
if (ifni == NULL)
|
||||
goto out;
|
||||
cp += ni*(sizeof(struct if_nameindex) + 1);
|
||||
|
||||
/*
|
||||
* 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);
|
||||
}
|
|
@ -0,0 +1,79 @@
|
|||
/* $NetBSD: if_nametoindex.c,v 1.1 2000/04/24 10:24:47 itojun Exp $ */
|
||||
/* $KAME: if_nametoindex.c,v 1.4 2000/04/24 10:08:41 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 "namespace.h"
|
||||
#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>
|
||||
|
||||
#ifdef __weak_alias
|
||||
__weak_alias(if_nametoindex,_if_nametoindex)
|
||||
#endif
|
||||
|
||||
/*
|
||||
* From RFC 2133:
|
||||
*
|
||||
* 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 does not exist, the return value is 0.
|
||||
*/
|
||||
|
||||
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);
|
||||
return(ni);
|
||||
}
|
|
@ -1,224 +0,0 @@
|
|||
/* $NetBSD: ifname.c,v 1.7 2000/01/23 00:01:35 mycroft 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.
|
||||
*/
|
||||
/*
|
||||
* 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/sysctl.h>
|
||||
#include <net/if.h>
|
||||
#include <net/route.h>
|
||||
#include <net/if_dl.h>
|
||||
|
||||
#include <assert.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))
|
||||
|
||||
unsigned int
|
||||
if_nametoindex(ifname)
|
||||
const char *ifname;
|
||||
{
|
||||
struct if_nameindex *iff = if_nameindex(), *ifx;
|
||||
int ret;
|
||||
|
||||
_DIAGASSERT(ifname != NULL);
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
char *
|
||||
if_indextoname(ifindex, ifname)
|
||||
unsigned int ifindex;
|
||||
char *ifname; /* at least IF_NAMESIZE */
|
||||
{
|
||||
struct if_nameindex *iff = if_nameindex(), *ifx;
|
||||
char *cp, *dp;
|
||||
|
||||
_DIAGASSERT(ifname != NULL);
|
||||
|
||||
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;
|
||||
size_t 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,
|
||||
(size_t)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;
|
||||
}
|
||||
memcpy(cp + hlen, ifbuf, (size_t)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;
|
||||
{
|
||||
|
||||
_DIAGASSERT(ptr != NULL);
|
||||
|
||||
free(ptr);
|
||||
}
|
Loading…
Reference in New Issue