xref: /openbsd/lib/libc/sys/getsockname.2 (revision ee4ffdb6)
1.\"	$OpenBSD: getsockname.2,v 1.30 2015/09/10 17:55:21 schwarze 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: September 10 2015 $
34.Dt GETSOCKNAME 2
35.Os
36.Sh NAME
37.Nm getsockname
38.Nd get socket name
39.Sh SYNOPSIS
40.In sys/socket.h
41.Ft int
42.Fn getsockname "int s" "struct sockaddr *name" "socklen_t *namelen"
43.Sh DESCRIPTION
44.Fn getsockname
45returns the locally bound address information for a specified socket.
46.Pp
47Common uses of this function are as follows:
48.Bl -bullet
49.It
50When
51.Xr bind 2
52is called with a port number of 0 (indicating the kernel should pick
53an ephemeral port)
54.Fn getsockname
55is used to retrieve the kernel-assigned port number.
56.It
57When a process calls
58.Xr bind 2
59on a wildcard IP address,
60.Fn getsockname
61is used to retrieve the local IP address for the connection.
62.It
63When a function wishes to know the address family of a socket,
64.Fn getsockname
65can be used.
66.El
67.Pp
68.Fn getsockname
69takes three parameters:
70.Pp
71.Fa s
72contains the file descriptor for the socket to be looked up.
73.Pp
74.Fa name
75points to a
76.Li sockaddr
77structure which will hold the resulting address information.
78Normal use requires one to use a structure
79specific to the protocol family in use, such as
80.Li sockaddr_in
81(IPv4) or
82.Li sockaddr_in6
83(IPv6), cast to a (struct sockaddr *).
84.Pp
85For greater portability (such as newer protocol families) the new
86structure sockaddr_storage exists.
87.Li sockaddr_storage
88is large enough to hold any of the other sockaddr_* variants.
89On return, it should be cast to the correct sockaddr type,
90according to the current protocol family.
91.Pp
92.Fa namelen
93indicates the amount of space pointed to by
94.Fa name ,
95in bytes.
96Upon return,
97.Fa namelen
98is set to the actual size of the returned address information.
99.Pp
100If the address of the destination socket for a given socket connection is
101needed, the
102.Xr getpeername 2
103function should be used instead.
104.Pp
105If
106.Fa name
107does not point to enough space to hold the entire socket address, the
108result will be truncated to
109.Fa namelen
110bytes.
111.Sh RETURN VALUES
112On success,
113.Fn getsockname
114returns a 0, and
115.Fa namelen
116is set to the actual size of the socket address returned in
117.Fa name .
118Otherwise,
119.Va errno
120is set, and a value of \-1 is returned.
121.Sh ERRORS
122If
123.Fn getsockname
124fails,
125.Va errno
126is set to one of the following:
127.Bl -tag -width Er
128.It Bq Er EBADF
129The argument
130.Fa s
131is not a valid descriptor.
132.It Bq Er ENOTSOCK
133The argument
134.Fa s
135is a file, not a socket.
136.It Bq Er ENOBUFS
137Insufficient resources were available in the system
138to perform the operation.
139.It Bq Er EFAULT
140The
141.Fa name
142or
143.Fa namelen
144parameter points to memory not in a valid part of the
145process address space.
146.El
147.Sh SEE ALSO
148.Xr accept 2 ,
149.Xr bind 2 ,
150.Xr getpeername 2 ,
151.Xr socket 2 ,
152.Xr getpeereid 3
153.Sh STANDARDS
154The
155.Fn getsockname
156function conforms to
157.St -p1003.1-2008 .
158.Sh HISTORY
159The
160.Fn getsockname
161function call appeared in
162.Bx 4.2 .
163