1.\" $OpenBSD: getpeername.2,v 1.16 2001/06/26 19:56:52 dugsong Exp $ 2.\" $NetBSD: getpeername.2,v 1.6 1995/10/12 15:40:56 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.\" @(#)getpeername.2 8.1 (Berkeley) 6/4/93 36.\" 37.Dd July 17, 1999 38.Dt GETPEERNAME 2 39.Os 40.Sh NAME 41.Nm getpeername 42.Nd get name of connected peer 43.Sh SYNOPSIS 44.Fd #include <sys/types.h> 45.Fd #include <sys/socket.h> 46.Ft int 47.Fn getpeername "int s" "struct sockaddr *name" "socklen_t *namelen" 48.Sh DESCRIPTION 49.Fn getpeername 50returns the address information of the peer connected to socket 51.Fa s . 52One common use occurs when a process inherits an open socket, such as 53TCP servers forked from 54.Xr inetd 8 . 55In this scenario, 56.Fn getpeername 57is used to determine the connecting client's IP address. 58.Pp 59.Fn getpeername 60takes three parameters: 61.Pp 62.Fa s 63Contains the file descriptor of the socket whose peer should be looked up. 64.Pp 65.Fa name 66Points to a 67.Li sockaddr 68structure that will hold the address information for the connected peer. 69Normal use requires one to use a structure 70specific to the protocol family in use, such as 71.Li sockaddr_in 72(IPv4) or 73.Li sockaddr_in6 74(IPv6), cast to a (struct sockaddr *). 75.Pp 76For greater portability, especially with the newer protocol families, the new 77.Li struct sockaddr_storage 78should be used. 79.Li sockaddr_storage 80is large enough to hold any of the other sockaddr_* variants. 81On return, it can be cast to the correct sockaddr type, 82based the protocol family contained in its ss_family field. 83.Pp 84.Fa namelen 85Indicates the amount of space pointed to by 86.Fa name , 87in bytes. 88.Pp 89If address information for the local end of the socket is required, the 90.Xr getsockname 2 91function should be used instead. 92.Pp 93If 94.Fa name 95does not point to enough space to hold the entire socket address, the 96result will be truncated to 97.Fa namelen 98bytes. 99.Sh RETURN VALUES 100If the call succeeds, a 0 is returned and 101.Fa namelen 102is set to the actual size of the socket address returned in 103.Fa name . 104Otherwise, 105.Va errno 106is set and a value of \-1 is returned. 107.Sh ERRORS 108On failure, 109.Va errno 110is set to one of the following: 111.Bl -tag -width Er 112.It Bq Er EBADF 113The argument 114.Fa s 115is not a valid descriptor. 116.It Bq Er ENOTSOCK 117The argument 118.Fa s 119is a file, not a socket. 120.It Bq Er ENOTCONN 121The socket is not connected. 122.It Bq Er ENOBUFS 123Insufficient resources were available in the system 124to perform the operation. 125.It Bq Er EFAULT 126The 127.Fa name 128parameter points to memory not in a valid part of the 129process address space. 130.El 131.Sh SEE ALSO 132.Xr accept 2 , 133.Xr bind 2 , 134.Xr getsockname 2 , 135.Xr getpeereid 2 , 136.Xr socket 2 137.Sh HISTORY 138The 139.Fn getpeername 140function call appeared in 141.Bx 4.2 . 142