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