xref: /openbsd-src/lib/libc/net/if_indextoname.3 (revision 22cd8fe17b8dad801a7a056aa22eb66840f3ba34)
1*22cd8fe1Sjmc.\"	$OpenBSD: if_indextoname.3,v 1.16 2015/11/21 07:48:10 jmc Exp $
24c989683Sderaadt.\" Copyright (c) 1983, 1991, 1993
34c989683Sderaadt.\"	The Regents of the University of California.  All rights reserved.
44c989683Sderaadt.\"
54c989683Sderaadt.\" Redistribution and use in source and binary forms, with or without
64c989683Sderaadt.\" modification, are permitted provided that the following conditions
74c989683Sderaadt.\" are met:
84c989683Sderaadt.\" 1. Redistributions of source code must retain the above copyright
94c989683Sderaadt.\"    notice, this list of conditions and the following disclaimer.
104c989683Sderaadt.\" 2. Redistributions in binary form must reproduce the above copyright
114c989683Sderaadt.\"    notice, this list of conditions and the following disclaimer in the
124c989683Sderaadt.\"    documentation and/or other materials provided with the distribution.
136580fee3Smillert.\" 3. Neither the name of the University nor the names of its contributors
144c989683Sderaadt.\"    may be used to endorse or promote products derived from this software
154c989683Sderaadt.\"    without specific prior written permission.
164c989683Sderaadt.\"
174c989683Sderaadt.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
184c989683Sderaadt.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
194c989683Sderaadt.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
204c989683Sderaadt.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
214c989683Sderaadt.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
224c989683Sderaadt.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
234c989683Sderaadt.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
244c989683Sderaadt.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
254c989683Sderaadt.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
264c989683Sderaadt.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
274c989683Sderaadt.\" SUCH DAMAGE.
284c989683Sderaadt.\"
294c989683Sderaadt.\"     From: @(#)rcmd.3	8.1 (Berkeley) 6/4/93
304c989683Sderaadt.\"
31*22cd8fe1Sjmc.Dd $Mdocdate: November 21 2015 $
324c989683Sderaadt.Dt IF_NAMETOINDEX 3
333a03493dSaaron.Os
344c989683Sderaadt.Sh NAME
354c989683Sderaadt.Nm if_nametoindex ,
364c989683Sderaadt.Nm if_indextoname ,
374c989683Sderaadt.Nm if_nameindex ,
38be4ef03eSderaadt.Nm if_freenameindex
394c989683Sderaadt.Nd convert interface index to name, and vice versa
404c989683Sderaadt.Sh SYNOPSIS
4164d4e987Stedu.In sys/types.h
4264d4e987Stedu.In sys/socket.h
4364d4e987Stedu.In net/if.h
444c989683Sderaadt.Ft "unsigned int"
454c989683Sderaadt.Fn if_nametoindex "const char *ifname"
464c989683Sderaadt.Ft "char *"
474c989683Sderaadt.Fn if_indextoname "unsigned int ifindex" "char *ifname"
484c989683Sderaadt.Ft "struct if_nameindex *"
494c989683Sderaadt.Fn if_nameindex "void"
504c989683Sderaadt.Ft "void"
51be4ef03eSderaadt.Fn if_freenameindex "struct if_nameindex *ptr"
524c989683Sderaadt.Sh DESCRIPTION
533a03493dSaaronThese functions map interface indexes to interface names (such as
543a03493dSaaron.Dq lo0 ) ,
553a03493dSaaronand vice versa.
564c989683Sderaadt.Pp
573a03493dSaaronThe
584c989683Sderaadt.Fn if_nametoindex
593a03493dSaaronfunction converts an interface name specified by the
603a03493dSaaron.Fa ifname
613a03493dSaaronargument to an interface index (positive integer value).
624c989683SderaadtIf the specified interface does not exist, 0 will be returned.
634c989683Sderaadt.Pp
644c989683Sderaadt.Fn if_indextoname
653a03493dSaaronconverts an interface index specified by the
663a03493dSaaron.Fa ifindex
673a03493dSaaronargument to an interface name.
684c989683SderaadtThe
694c989683Sderaadt.Fa ifname
704c989683Sderaadtargument must point to a buffer of at least
714c989683Sderaadt.Dv IF_NAMESIZE
724c989683Sderaadtbytes into which the interface name corresponding to the specified index is
734c989683Sderaadtreturned.
743a03493dSaaron.Pf ( Dv IF_NAMESIZE
754c989683Sderaadtis also defined in
76369bef3aSschwarze.In net/if.h
771e5ede29Scloderand its value includes a terminating NUL byte at the end of the
783a03493dSaaroninterface name.)
794c989683SderaadtThis pointer is also the return value of the function.
804c989683SderaadtIf there is no interface corresponding to the specified index,
814c989683Sderaadt.Dv NULL
824c989683Sderaadtis returned.
834c989683Sderaadt.Pp
844c989683Sderaadt.Fn if_nameindex
854c989683Sderaadtreturns an array of
865a14067eSjaredy.Vt if_nameindex
874c989683Sderaadtstructures.
885a14067eSjaredy.Vt if_nameindex
894c989683Sderaadtis also defined in
90369bef3aSschwarze.In net/if.h ,
914c989683Sderaadtand is as follows:
925a14067eSjaredy.Bd -literal -offset indent
934c989683Sderaadtstruct if_nameindex {
944c989683Sderaadt	unsigned int	  if_index;	/* 1, 2, ... */
955a14067eSjaredy	char		 *if_name;	/* NUL-terminated name */
964c989683Sderaadt};
974c989683Sderaadt.Ed
984c989683Sderaadt.Pp
994c989683SderaadtThe end of the array of structures is indicated by a structure with
1004c989683Sderaadtan
1014c989683Sderaadt.Fa if_index
1024c989683Sderaadtof 0 and an
1034c989683Sderaadt.Fa if_name
1044c989683Sderaadtof
1054c989683Sderaadt.Dv NULL .
1063a03493dSaaronThe function returns a null pointer on error.
1074c989683SderaadtThe memory used for this array of structures along with the interface
1084c989683Sderaadtnames pointed to by the
1094c989683Sderaadt.Fa if_name
1104c989683Sderaadtmembers is obtained dynamically.
1114c989683SderaadtThis memory is freed by the
112be4ef03eSderaadt.Fn if_freenameindex
1134c989683Sderaadtfunction.
114be4ef03eSderaadt.Pp
115be4ef03eSderaadt.Fn if_freenameindex
116be4ef03eSderaadttakes a pointer that was returned by
117be4ef03eSderaadt.Fn if_nameindex
118be4ef03eSderaadtas argument
119be4ef03eSderaadt.Pq Fa ptr ,
120be4ef03eSderaadtand it reclaims the region allocated.
1214c989683Sderaadt.Sh DIAGNOSTICS
1224c989683Sderaadt.Fn if_nametoindex
1234c989683Sderaadtreturns 0 on error, positive integer on success.
1244c989683Sderaadt.Fn if_indextoname
1254c989683Sderaadtand
1264c989683Sderaadt.Fn if_nameindex
1274c989683Sderaadtreturn
1284c989683Sderaadt.Dv NULL
1294c989683Sderaadton errors.
1304c989683Sderaadt.Sh SEE ALSO
1314aeed91dSmillert.Xr getifaddrs 3 ,
132*22cd8fe1Sjmc.Xr netintro 4
133cc15c4a4Sjmc.Sh STANDARDS
1345a14067eSjaredy.Rs
1355a14067eSjaredy.%A R. Gilligan
1365a14067eSjaredy.%A S. Thomson
1375a14067eSjaredy.%A J. Bound
138bdcf79d2Sjmc.%A J. McCann
1395a14067eSjaredy.%A W. Stevens
140bdcf79d2Sjmc.%D February 2003
141bdcf79d2Sjmc.%R RFC 3493
1425a14067eSjaredy.%T Basic Socket Interface Extensions for IPv6
1435a14067eSjaredy.Re
144