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