1.\" $NetBSD: getsockname.2,v 1.23 2003/08/07 16:43:59 agc 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. Neither the name of the University nor the names of its contributors 15.\" may be used to endorse or promote products derived from this software 16.\" without specific prior written permission. 17.\" 18.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 19.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 22.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28.\" SUCH DAMAGE. 29.\" 30.\" @(#)getsockname.2 8.1 (Berkeley) 6/4/93 31.\" 32.Dd August 11, 2002 33.Dt GETSOCKNAME 2 34.Os 35.Sh NAME 36.Nm getsockname 37.Nd get socket name 38.Sh LIBRARY 39.Lb libc 40.Sh SYNOPSIS 41.In sys/socket.h 42.Ft int 43.Fn getsockname "int s" "struct sockaddr * restrict name" "socklen_t * restrict 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 123The call succeeds unless: 124.Bl -tag -width Er 125.It Bq Er EBADF 126The argument 127.Fa s 128is not a valid descriptor. 129.It Bq Er ENOTSOCK 130The argument 131.Fa s 132is a file, not a socket. 133.It Bq Er EINVAL 134The socket has been shut down. 135.It Bq Er ENOBUFS 136Insufficient resources were available in the system to perform the operation. 137.It Bq Er EFAULT 138The 139.Fa name 140parameter points to memory not in a valid part of the process address space. 141.El 142.Sh SEE ALSO 143.Xr bind 2 , 144.Xr socket 2 145.Sh HISTORY 146The 147.Fn getsockname 148function call appeared in 149.Bx 4.2 . 150.Sh BUGS 151Names bound to sockets in the 152.Ux 153domain are inaccessible; 154.Fn getsockname 155returns a zero length name. 156