1 |
27 |
unneback |
.\" $OpenBSD: getsockname.2,v 1.16 2001/08/06 10:42:26 mpech Exp $
|
2 |
|
|
.\" $NetBSD: getsockname.2,v 1.6 1995/10/12 15:41:00 jtc Exp $
|
3 |
|
|
.\"
|
4 |
|
|
.\" Copyright (c) 1983, 1991, 1993
|
5 |
|
|
.\" The Regents of the University of California. All rights reserved.
|
6 |
|
|
.\"
|
7 |
|
|
.\" Redistribution and use in source and binary forms, with or without
|
8 |
|
|
.\" modification, are permitted provided that the following conditions
|
9 |
|
|
.\" are met:
|
10 |
|
|
.\" 1. Redistributions of source code must retain the above copyright
|
11 |
|
|
.\" notice, this list of conditions and the following disclaimer.
|
12 |
|
|
.\" 2. Redistributions in binary form must reproduce the above copyright
|
13 |
|
|
.\" notice, this list of conditions and the following disclaimer in the
|
14 |
|
|
.\" documentation and/or other materials provided with the distribution.
|
15 |
|
|
.\" 3. All advertising materials mentioning features or use of this software
|
16 |
|
|
.\" must display the following acknowledgement:
|
17 |
|
|
.\" This product includes software developed by the University of
|
18 |
|
|
.\" California, Berkeley and its contributors.
|
19 |
|
|
.\" 4. Neither the name of the University nor the names of its contributors
|
20 |
|
|
.\" may be used to endorse or promote products derived from this software
|
21 |
|
|
.\" without specific prior written permission.
|
22 |
|
|
.\"
|
23 |
|
|
.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
24 |
|
|
.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
25 |
|
|
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
26 |
|
|
.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
27 |
|
|
.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
28 |
|
|
.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
29 |
|
|
.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
30 |
|
|
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
31 |
|
|
.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
32 |
|
|
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
33 |
|
|
.\" SUCH DAMAGE.
|
34 |
|
|
.\"
|
35 |
|
|
.\" @(#)getsockname.2 8.1 (Berkeley) 6/4/93
|
36 |
|
|
.\"
|
37 |
|
|
.Dd July 17, 1999
|
38 |
|
|
.Dt GETSOCKNAME 2
|
39 |
|
|
.Os
|
40 |
|
|
.Sh NAME
|
41 |
|
|
.Nm getsockname
|
42 |
|
|
.Nd get socket name
|
43 |
|
|
.Sh SYNOPSIS
|
44 |
|
|
.Fd #include
|
45 |
|
|
.Fd #include
|
46 |
|
|
.Ft int
|
47 |
|
|
.Fn getsockname "int s" "struct sockaddr *name" "socklen_t *namelen"
|
48 |
|
|
.Sh DESCRIPTION
|
49 |
|
|
.Fn getsockname
|
50 |
|
|
returns the locally bound address information for a specified socket.
|
51 |
|
|
.Pp
|
52 |
|
|
Common uses of this function are as follows:
|
53 |
|
|
.Bl -bullet
|
54 |
|
|
.It
|
55 |
|
|
When
|
56 |
|
|
.Xr bind 2
|
57 |
|
|
is called with a port number of 0 (indicating the kernel should pick
|
58 |
|
|
an ephemeral port)
|
59 |
|
|
.Fn getsockname
|
60 |
|
|
is used to retrieve the kernel-assigned port number.
|
61 |
|
|
.It
|
62 |
|
|
When a process calls
|
63 |
|
|
.Xr bind 2
|
64 |
|
|
on a wildcard IP address,
|
65 |
|
|
.Fn getsockname
|
66 |
|
|
is used to retrieve the local IP address for the connection.
|
67 |
|
|
.It
|
68 |
|
|
When a function wishes to know the address family of a socket,
|
69 |
|
|
.Fn getsockname
|
70 |
|
|
can be used.
|
71 |
|
|
.El
|
72 |
|
|
.Pp
|
73 |
|
|
.Fn getsockname
|
74 |
|
|
takes three parameters:
|
75 |
|
|
.Pp
|
76 |
|
|
.Fa s ,
|
77 |
|
|
Contains the file desriptor for the socket to be looked up.
|
78 |
|
|
.Pp
|
79 |
|
|
.Fa name
|
80 |
|
|
points to a
|
81 |
|
|
.Li sockaddr
|
82 |
|
|
structure which will hold the resulting address information.
|
83 |
|
|
Normal use requires one to use a structure
|
84 |
|
|
specific to the protocol family in use, such as
|
85 |
|
|
.Li sockaddr_in
|
86 |
|
|
(IPv4) or
|
87 |
|
|
.Li sockaddr_in6
|
88 |
|
|
(IPv6), cast to a (struct sockaddr *).
|
89 |
|
|
.Pp
|
90 |
|
|
For greater portability (such as newer protocol families) the new
|
91 |
|
|
structure sockaddr_storage exists.
|
92 |
|
|
.Li sockaddr_storage
|
93 |
|
|
is large enough to hold any of the other sockaddr_* variants.
|
94 |
|
|
On return, it should be cast to the correct sockaddr type,
|
95 |
|
|
according to the current protocol family.
|
96 |
|
|
.Pp
|
97 |
|
|
.Fa namelen
|
98 |
|
|
Indicates the amount of space pointed to by
|
99 |
|
|
.Fa name ,
|
100 |
|
|
in bytes.
|
101 |
|
|
Upon return,
|
102 |
|
|
.Fa namelen
|
103 |
|
|
is set to the actual size of the returned address information.
|
104 |
|
|
.Pp
|
105 |
|
|
If the address of the destination socket for a given socket connection is
|
106 |
|
|
needed, the
|
107 |
|
|
.Xr getpeername 2
|
108 |
|
|
function should be used instead.
|
109 |
|
|
.Pp
|
110 |
|
|
If
|
111 |
|
|
.Fa name
|
112 |
|
|
does not point to enough space to hold the entire socket address, the
|
113 |
|
|
result will be truncated to
|
114 |
|
|
.Fa namelen
|
115 |
|
|
bytes.
|
116 |
|
|
.Sh RETURN VALUES
|
117 |
|
|
On success,
|
118 |
|
|
.Fn getsockname
|
119 |
|
|
returns a 0, and
|
120 |
|
|
.Fa namelen
|
121 |
|
|
is set to the actual size of the socket address returned in
|
122 |
|
|
.Fa name .
|
123 |
|
|
Otherwise,
|
124 |
|
|
.Va errno
|
125 |
|
|
is set, and a value of \-1 is returned.
|
126 |
|
|
.Sh ERRORS
|
127 |
|
|
If
|
128 |
|
|
.Fn getsockname
|
129 |
|
|
fails,
|
130 |
|
|
.Va errno
|
131 |
|
|
is set to one of the following:
|
132 |
|
|
.Bl -tag -width Er
|
133 |
|
|
.It Bq Er EBADF
|
134 |
|
|
The argument
|
135 |
|
|
.Fa s
|
136 |
|
|
is not a valid descriptor.
|
137 |
|
|
.It Bq Er ENOTSOCK
|
138 |
|
|
The argument
|
139 |
|
|
.Fa s
|
140 |
|
|
is a file, not a socket.
|
141 |
|
|
.It Bq Er ENOBUFS
|
142 |
|
|
Insufficient resources were available in the system
|
143 |
|
|
to perform the operation.
|
144 |
|
|
.It Bq Er EFAULT
|
145 |
|
|
The
|
146 |
|
|
.Fa name
|
147 |
|
|
parameter points to memory not in a valid part of the
|
148 |
|
|
process address space.
|
149 |
|
|
.El
|
150 |
|
|
.Sh SEE ALSO
|
151 |
|
|
.Xr accept 2 ,
|
152 |
|
|
.Xr bind 2 ,
|
153 |
|
|
.Xr getpeername 2 ,
|
154 |
|
|
.Xr getpeereid 2 ,
|
155 |
|
|
.Xr socket 2
|
156 |
|
|
.Sh BUGS
|
157 |
|
|
Names bound to sockets in the UNIX domain are inaccessible;
|
158 |
|
|
.Nm getsockname
|
159 |
|
|
returns a zero length name.
|
160 |
|
|
.Sh HISTORY
|
161 |
|
|
The
|
162 |
|
|
.Fn getsockname
|
163 |
|
|
function call appeared in
|
164 |
|
|
.Bx 4.2 .
|