1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
|
.\" $OpenBSD: if_indextoname.3,v 1.15 2014/01/21 03:15:45 schwarze Exp $
.\" Copyright (c) 1983, 1991, 1993
.\" The Regents of the University of California. 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 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
.\" 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
.\" 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.
.\"
.\" From: @(#)rcmd.3 8.1 (Berkeley) 6/4/93
.\"
.Dd $Mdocdate: January 21 2014 $
.Dt IF_NAMETOINDEX 3
.Os
.Sh NAME
.Nm if_nametoindex ,
.Nm if_indextoname ,
.Nm if_nameindex ,
.Nm if_freenameindex
.Nd convert interface index to name, and vice versa
.Sh SYNOPSIS
.In sys/types.h
.In sys/socket.h
.In net/if.h
.Ft "unsigned int"
.Fn if_nametoindex "const char *ifname"
.Ft "char *"
.Fn if_indextoname "unsigned int ifindex" "char *ifname"
.Ft "struct if_nameindex *"
.Fn if_nameindex "void"
.Ft "void"
.Fn if_freenameindex "struct if_nameindex *ptr"
.Sh DESCRIPTION
These functions map interface indexes to interface names (such as
.Dq lo0 ) ,
and vice versa.
.Pp
The
.Fn if_nametoindex
function converts an interface name specified by the
.Fa ifname
argument to an interface index (positive integer value).
If the specified interface does not exist, 0 will be returned.
.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.
.Pf ( Dv IF_NAMESIZE
is also defined in
.In net/if.h
and its value includes a terminating NUL 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.
.Pp
.Fn if_nameindex
returns an array of
.Vt if_nameindex
structures.
.Vt if_nameindex
is also defined in
.In net/if.h ,
and is as follows:
.Bd -literal -offset indent
struct if_nameindex {
unsigned int if_index; /* 1, 2, ... */
char *if_name; /* NUL-terminated name */
};
.Ed
.Pp
The end of the array of structures is indicated by a structure with
an
.Fa if_index
of 0 and an
.Fa if_name
of
.Dv NULL .
The function returns a null pointer on 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.
.Pp
.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
.Fn if_nametoindex
returns 0 on error, positive integer on success.
.Fn if_indextoname
and
.Fn if_nameindex
return
.Dv NULL
on errors.
.Sh SEE ALSO
.Xr getifaddrs 3 ,
.Xr networking 4
.Sh STANDARDS
.Rs
.%A R. Gilligan
.%A S. Thomson
.%A J. Bound
.%A J. McCann
.%A W. Stevens
.%D February 2003
.%R RFC 3493
.%T Basic Socket Interface Extensions for IPv6
.Re
|