|
Packit |
7cfc04 |
.\" Copyright (c) 1983, 1991 The Regents of the University of California.
|
|
Packit |
7cfc04 |
.\" All rights reserved.
|
|
Packit |
7cfc04 |
.\"
|
|
Packit |
7cfc04 |
.\" %%%LICENSE_START(BSD_4_CLAUSE_UCB)
|
|
Packit |
7cfc04 |
.\" Redistribution and use in source and binary forms, with or without
|
|
Packit |
7cfc04 |
.\" modification, are permitted provided that the following conditions
|
|
Packit |
7cfc04 |
.\" are met:
|
|
Packit |
7cfc04 |
.\" 1. Redistributions of source code must retain the above copyright
|
|
Packit |
7cfc04 |
.\" notice, this list of conditions and the following disclaimer.
|
|
Packit |
7cfc04 |
.\" 2. Redistributions in binary form must reproduce the above copyright
|
|
Packit |
7cfc04 |
.\" notice, this list of conditions and the following disclaimer in the
|
|
Packit |
7cfc04 |
.\" documentation and/or other materials provided with the distribution.
|
|
Packit |
7cfc04 |
.\" 3. All advertising materials mentioning features or use of this software
|
|
Packit |
7cfc04 |
.\" must display the following acknowledgement:
|
|
Packit |
7cfc04 |
.\" This product includes software developed by the University of
|
|
Packit |
7cfc04 |
.\" California, Berkeley and its contributors.
|
|
Packit |
7cfc04 |
.\" 4. Neither the name of the University nor the names of its contributors
|
|
Packit |
7cfc04 |
.\" may be used to endorse or promote products derived from this software
|
|
Packit |
7cfc04 |
.\" without specific prior written permission.
|
|
Packit |
7cfc04 |
.\"
|
|
Packit |
7cfc04 |
.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
Packit |
7cfc04 |
.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
Packit |
7cfc04 |
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
Packit |
7cfc04 |
.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
Packit |
7cfc04 |
.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
Packit |
7cfc04 |
.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
Packit |
7cfc04 |
.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
Packit |
7cfc04 |
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
Packit |
7cfc04 |
.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
Packit |
7cfc04 |
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
Packit |
7cfc04 |
.\" SUCH DAMAGE.
|
|
Packit |
7cfc04 |
.\" %%%LICENSE_END
|
|
Packit |
7cfc04 |
.\"
|
|
Packit |
7cfc04 |
.\" @(#)getpeername.2 6.5 (Berkeley) 3/10/91
|
|
Packit |
7cfc04 |
.\"
|
|
Packit |
7cfc04 |
.\" Modified Sat Jul 24 16:37:50 1993 by Rik Faith <faith@cs.unc.edu>
|
|
Packit |
7cfc04 |
.\" Modified Thu Jul 30 14:37:50 1993 by Martin Schulze <joey@debian.org>
|
|
Packit |
7cfc04 |
.\" Modified Sun Mar 28 21:26:46 1999 by Andries Brouwer <aeb@cwi.nl>
|
|
Packit |
7cfc04 |
.\" Modified 17 Jul 2002, Michael Kerrisk <mtk.manpages@gmail.com>
|
|
Packit |
7cfc04 |
.\" Added 'socket' to NAME, so that "man -k socket" will show this page.
|
|
Packit |
7cfc04 |
.\"
|
|
Packit |
7cfc04 |
.TH GETPEERNAME 2 2017-09-15 "Linux" "Linux Programmer's Manual"
|
|
Packit |
7cfc04 |
.SH NAME
|
|
Packit |
7cfc04 |
getpeername \- get name of connected peer socket
|
|
Packit |
7cfc04 |
.SH SYNOPSIS
|
|
Packit |
7cfc04 |
.B #include <sys/socket.h>
|
|
Packit |
7cfc04 |
.PP
|
|
Packit |
7cfc04 |
.BI "int getpeername(int " sockfd ", struct sockaddr *" addr \
|
|
Packit |
7cfc04 |
", socklen_t *" addrlen );
|
|
Packit |
7cfc04 |
.SH DESCRIPTION
|
|
Packit |
7cfc04 |
.BR getpeername ()
|
|
Packit |
7cfc04 |
returns the address of the peer connected to the socket
|
|
Packit |
7cfc04 |
.IR sockfd ,
|
|
Packit |
7cfc04 |
in the buffer pointed to by
|
|
Packit |
7cfc04 |
.IR addr .
|
|
Packit |
7cfc04 |
The
|
|
Packit |
7cfc04 |
.I addrlen
|
|
Packit |
7cfc04 |
argument should be initialized to indicate the amount of space pointed to
|
|
Packit |
7cfc04 |
by
|
|
Packit |
7cfc04 |
.IR addr .
|
|
Packit |
7cfc04 |
On return it contains the actual size of the name returned (in bytes).
|
|
Packit |
7cfc04 |
The name is truncated if the buffer provided is too small.
|
|
Packit |
7cfc04 |
.PP
|
|
Packit |
7cfc04 |
The returned address is truncated if the buffer provided is too small;
|
|
Packit |
7cfc04 |
in this case,
|
|
Packit |
7cfc04 |
.I addrlen
|
|
Packit |
7cfc04 |
will return a value greater than was supplied to the call.
|
|
Packit |
7cfc04 |
.SH RETURN VALUE
|
|
Packit |
7cfc04 |
On success, zero is returned.
|
|
Packit |
7cfc04 |
On error, \-1 is returned, and
|
|
Packit |
7cfc04 |
.I errno
|
|
Packit |
7cfc04 |
is set appropriately.
|
|
Packit |
7cfc04 |
.SH ERRORS
|
|
Packit |
7cfc04 |
.TP
|
|
Packit |
7cfc04 |
.B EBADF
|
|
Packit |
7cfc04 |
The argument
|
|
Packit |
7cfc04 |
.I sockfd
|
|
Packit |
7cfc04 |
is not a valid file descriptor.
|
|
Packit |
7cfc04 |
.TP
|
|
Packit |
7cfc04 |
.B EFAULT
|
|
Packit |
7cfc04 |
The
|
|
Packit |
7cfc04 |
.I addr
|
|
Packit |
7cfc04 |
argument points to memory not in a valid part of the
|
|
Packit |
7cfc04 |
process address space.
|
|
Packit |
7cfc04 |
.TP
|
|
Packit |
7cfc04 |
.B EINVAL
|
|
Packit |
7cfc04 |
.I addrlen
|
|
Packit |
7cfc04 |
is invalid (e.g., is negative).
|
|
Packit |
7cfc04 |
.TP
|
|
Packit |
7cfc04 |
.B ENOBUFS
|
|
Packit |
7cfc04 |
Insufficient resources were available in the system
|
|
Packit |
7cfc04 |
to perform the operation.
|
|
Packit |
7cfc04 |
.TP
|
|
Packit |
7cfc04 |
.B ENOTCONN
|
|
Packit |
7cfc04 |
The socket is not connected.
|
|
Packit |
7cfc04 |
.TP
|
|
Packit |
7cfc04 |
.B ENOTSOCK
|
|
Packit |
7cfc04 |
The file descriptor
|
|
Packit |
7cfc04 |
.I sockfd
|
|
Packit |
7cfc04 |
does not refer to a socket.
|
|
Packit |
7cfc04 |
.SH CONFORMING TO
|
|
Packit |
7cfc04 |
POSIX.1-2001, POSIX.1-2008, SVr4, 4.4BSD
|
|
Packit |
7cfc04 |
.RB ( getpeername ()
|
|
Packit |
7cfc04 |
first appeared in 4.2BSD).
|
|
Packit |
7cfc04 |
.SH NOTES
|
|
Packit |
7cfc04 |
For background on the
|
|
Packit |
7cfc04 |
.I socklen_t
|
|
Packit |
7cfc04 |
type, see
|
|
Packit |
7cfc04 |
.BR accept (2).
|
|
Packit |
7cfc04 |
.PP
|
|
Packit |
7cfc04 |
For stream sockets, once a
|
|
Packit |
7cfc04 |
.BR connect (2)
|
|
Packit |
7cfc04 |
has been performed, either socket can call
|
|
Packit |
7cfc04 |
.BR getpeername ()
|
|
Packit |
7cfc04 |
to obtain the address of the peer socket.
|
|
Packit |
7cfc04 |
On the other hand, datagram sockets are connectionless.
|
|
Packit |
7cfc04 |
Calling
|
|
Packit |
7cfc04 |
.BR connect (2)
|
|
Packit |
7cfc04 |
on a datagram socket merely sets the peer address for outgoing
|
|
Packit |
7cfc04 |
datagrams sent with
|
|
Packit |
7cfc04 |
.BR write (2)
|
|
Packit |
7cfc04 |
or
|
|
Packit |
7cfc04 |
.BR recv (2).
|
|
Packit |
7cfc04 |
The caller of
|
|
Packit |
7cfc04 |
.BR connect (2)
|
|
Packit |
7cfc04 |
can use
|
|
Packit |
7cfc04 |
.BR getpeername ()
|
|
Packit |
7cfc04 |
to obtain the peer address that it earlier set for the socket.
|
|
Packit |
7cfc04 |
However, the peer socket is unaware of this information, and calling
|
|
Packit |
7cfc04 |
.BR getpeername ()
|
|
Packit |
7cfc04 |
on the peer socket will return no useful information (unless a
|
|
Packit |
7cfc04 |
.BR connect (2)
|
|
Packit |
7cfc04 |
call was also executed on the peer).
|
|
Packit |
7cfc04 |
Note also that the receiver of a datagram can obtain
|
|
Packit |
7cfc04 |
the address of the sender when using
|
|
Packit |
7cfc04 |
.BR recvfrom (2).
|
|
Packit |
7cfc04 |
.SH SEE ALSO
|
|
Packit |
7cfc04 |
.BR accept (2),
|
|
Packit |
7cfc04 |
.BR bind (2),
|
|
Packit |
7cfc04 |
.BR getsockname (2),
|
|
Packit |
7cfc04 |
.BR ip (7),
|
|
Packit |
7cfc04 |
.BR socket (7),
|
|
Packit |
7cfc04 |
.BR unix (7)
|
|
Packit |
7cfc04 |
.SH COLOPHON
|
|
Packit |
7cfc04 |
This page is part of release 4.15 of the Linux
|
|
Packit |
7cfc04 |
.I man-pages
|
|
Packit |
7cfc04 |
project.
|
|
Packit |
7cfc04 |
A description of the project,
|
|
Packit |
7cfc04 |
information about reporting bugs,
|
|
Packit |
7cfc04 |
and the latest version of this page,
|
|
Packit |
7cfc04 |
can be found at
|
|
Packit |
7cfc04 |
\%https://www.kernel.org/doc/man\-pages/.
|