1*f14fb602SLionel Sambuc.\" $NetBSD: getpeername.2,v 1.21 2011/06/03 08:07:48 wiz Exp $ 22fe8fb19SBen Gras.\" 32fe8fb19SBen Gras.\" Copyright (c) 1983, 1991, 1993 42fe8fb19SBen Gras.\" The Regents of the University of California. All rights reserved. 52fe8fb19SBen Gras.\" 62fe8fb19SBen Gras.\" Redistribution and use in source and binary forms, with or without 72fe8fb19SBen Gras.\" modification, are permitted provided that the following conditions 82fe8fb19SBen Gras.\" are met: 92fe8fb19SBen Gras.\" 1. Redistributions of source code must retain the above copyright 102fe8fb19SBen Gras.\" notice, this list of conditions and the following disclaimer. 112fe8fb19SBen Gras.\" 2. Redistributions in binary form must reproduce the above copyright 122fe8fb19SBen Gras.\" notice, this list of conditions and the following disclaimer in the 132fe8fb19SBen Gras.\" documentation and/or other materials provided with the distribution. 142fe8fb19SBen Gras.\" 3. Neither the name of the University nor the names of its contributors 152fe8fb19SBen Gras.\" may be used to endorse or promote products derived from this software 162fe8fb19SBen Gras.\" without specific prior written permission. 172fe8fb19SBen Gras.\" 182fe8fb19SBen Gras.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 192fe8fb19SBen Gras.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 202fe8fb19SBen Gras.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 212fe8fb19SBen Gras.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 222fe8fb19SBen Gras.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 232fe8fb19SBen Gras.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 242fe8fb19SBen Gras.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 252fe8fb19SBen Gras.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 262fe8fb19SBen Gras.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 272fe8fb19SBen Gras.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 282fe8fb19SBen Gras.\" SUCH DAMAGE. 292fe8fb19SBen Gras.\" 302fe8fb19SBen Gras.\" @(#)getpeername.2 8.1 (Berkeley) 6/4/93 312fe8fb19SBen Gras.\" 32*f14fb602SLionel Sambuc.Dd June 3, 2011 332fe8fb19SBen Gras.Dt GETPEERNAME 2 342fe8fb19SBen Gras.Os 352fe8fb19SBen Gras.Sh NAME 362fe8fb19SBen Gras.Nm getpeername 372fe8fb19SBen Gras.Nd get name of connected peer 382fe8fb19SBen Gras.Sh LIBRARY 392fe8fb19SBen Gras.Lb libc 402fe8fb19SBen Gras.Sh SYNOPSIS 412fe8fb19SBen Gras.In sys/socket.h 422fe8fb19SBen Gras.Ft int 43*f14fb602SLionel Sambuc.Fn getpeername \ 44*f14fb602SLionel Sambuc"int s" "struct sockaddr * restrict name" "socklen_t * restrict namelen" 452fe8fb19SBen Gras.Sh DESCRIPTION 46*f14fb602SLionel SambucThe 472fe8fb19SBen Gras.Fn getpeername 48*f14fb602SLionel Sambucfunction returns the name of the peer connected to the socket 492fe8fb19SBen Gras.Fa s . 502fe8fb19SBen GrasOne common use occurs when a process inherits an open socket, such as 512fe8fb19SBen GrasTCP servers forked from 522fe8fb19SBen Gras.Xr inetd 8 . 532fe8fb19SBen GrasIn this scenario, 542fe8fb19SBen Gras.Fn getpeername 552fe8fb19SBen Grasis used to determine the connecting client's IP address. 562fe8fb19SBen Gras.Pp 57*f14fb602SLionel SambucThe function takes three parameters: 58*f14fb602SLionel Sambuc.Bl -tag -width namelen -offset indent 59*f14fb602SLionel Sambuc.It Fa s 602fe8fb19SBen Grascontains the file descriptor of the socket whose peer should be looked up. 61*f14fb602SLionel Sambuc.It Fa name 622fe8fb19SBen Graspoints to a 632fe8fb19SBen Gras.Li sockaddr 642fe8fb19SBen Grasstructure that will hold the address information for the connected peer. 652fe8fb19SBen GrasNormal use requires one to use a structure 662fe8fb19SBen Grasspecific to the protocol family in use, such as 672fe8fb19SBen Gras.Li sockaddr_in 682fe8fb19SBen Gras(IPv4) or 692fe8fb19SBen Gras.Li sockaddr_in6 702fe8fb19SBen Gras(IPv6), cast to a (struct sockaddr *). 712fe8fb19SBen Gras.Pp 722fe8fb19SBen GrasFor greater portability, especially with the newer protocol families, the new 732fe8fb19SBen Gras.Li struct sockaddr_storage 742fe8fb19SBen Grasshould be used. 752fe8fb19SBen Gras.Li sockaddr_storage 762fe8fb19SBen Grasis large enough to hold any of the other sockaddr_* variants. 772fe8fb19SBen GrasOn return, it can be cast to the correct sockaddr type, 782fe8fb19SBen Grasbased on the protocol family contained in its ss_family field. 79*f14fb602SLionel Sambuc.It Fa namelen 802fe8fb19SBen Grasindicates the amount of space pointed to by 812fe8fb19SBen Gras.Fa name , 822fe8fb19SBen Grasin bytes. 83*f14fb602SLionel Sambuc.El 842fe8fb19SBen Gras.Pp 852fe8fb19SBen GrasIf address information for the local end of the socket is required, the 862fe8fb19SBen Gras.Xr getsockname 2 872fe8fb19SBen Grasfunction should be used instead. 882fe8fb19SBen Gras.Pp 892fe8fb19SBen GrasIf 902fe8fb19SBen Gras.Fa name 912fe8fb19SBen Grasdoes not point to enough space to hold the entire socket address, the 922fe8fb19SBen Grasresult will be truncated to 932fe8fb19SBen Gras.Fa namelen 942fe8fb19SBen Grasbytes. 952fe8fb19SBen Gras.Sh RETURN VALUES 962fe8fb19SBen GrasIf the call succeeds, a 0 is returned and 972fe8fb19SBen Gras.Fa namelen 982fe8fb19SBen Grasis set to the actual size of the socket address returned in 992fe8fb19SBen Gras.Fa name . 1002fe8fb19SBen GrasOtherwise, 1012fe8fb19SBen Gras.Va errno 1022fe8fb19SBen Grasis set and a value of \-1 is returned. 1032fe8fb19SBen Gras.Sh ERRORS 1042fe8fb19SBen GrasThe call succeeds unless: 1052fe8fb19SBen Gras.Bl -tag -width Er 1062fe8fb19SBen Gras.It Bq Er EBADF 1072fe8fb19SBen GrasThe argument 1082fe8fb19SBen Gras.Fa s 1092fe8fb19SBen Grasis not a valid descriptor. 1102fe8fb19SBen Gras.It Bq Er EFAULT 1112fe8fb19SBen GrasThe 1122fe8fb19SBen Gras.Fa name 1132fe8fb19SBen Grasparameter points to memory not in a valid part of the 1142fe8fb19SBen Grasprocess address space. 115*f14fb602SLionel Sambuc.It Bq Er ENOBUFS 116*f14fb602SLionel SambucInsufficient resources were available in the system 117*f14fb602SLionel Sambucto perform the operation. 118*f14fb602SLionel Sambuc.It Bq Er ENOTCONN 119*f14fb602SLionel SambucThe socket is not connected. 120*f14fb602SLionel Sambuc.It Bq Er ENOTSOCK 121*f14fb602SLionel SambucThe argument 122*f14fb602SLionel Sambuc.Fa s 123*f14fb602SLionel Sambucis a file, not a socket. 1242fe8fb19SBen Gras.El 1252fe8fb19SBen Gras.Sh SEE ALSO 1262fe8fb19SBen Gras.Xr accept 2 , 1272fe8fb19SBen Gras.Xr bind 2 , 1282fe8fb19SBen Gras.Xr getsockname 2 , 1292fe8fb19SBen Gras.Xr socket 2 130*f14fb602SLionel Sambuc.Sh STANDARDS 131*f14fb602SLionel SambucThe function conforms to 132*f14fb602SLionel Sambuc.St -p1003.1-2008 . 1332fe8fb19SBen Gras.Sh HISTORY 1342fe8fb19SBen GrasThe 1352fe8fb19SBen Gras.Fn getpeername 1362fe8fb19SBen Grasfunction call appeared in 1372fe8fb19SBen Gras.Bx 4.2 . 138