man-pages/man2/getpeername.2.html
2021-03-31 01:06:50 +01:00

202 lines
4.7 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML><HEAD><TITLE>Man page of GETPEERNAME</TITLE>
</HEAD><BODY>
<H1>GETPEERNAME</H1>
Section: Linux Programmer's Manual (2)<BR>Updated: 2017-09-15<BR><A HREF="#index">Index</A>
<A HREF="/cgi-bin/man/man2html">Return to Main Contents</A><HR>
<A NAME="lbAB">&nbsp;</A>
<H2>NAME</H2>
getpeername - get name of connected peer socket
<A NAME="lbAC">&nbsp;</A>
<H2>SYNOPSIS</H2>
<B>#include &lt;<A HREF="file:///usr/include/sys/socket.h">sys/socket.h</A>&gt;</B>
<P>
<B>int getpeername(int </B><I>sockfd</I><B>, struct sockaddr *</B><I>addr</I><B>, socklen_t *</B><I>addrlen</I><B>);</B>
<A NAME="lbAD">&nbsp;</A>
<H2>DESCRIPTION</H2>
<B>getpeername</B>()
returns the address of the peer connected to the socket
<I>sockfd</I>,
in the buffer pointed to by
<I>addr</I>.
The
<I>addrlen</I>
argument should be initialized to indicate the amount of space pointed to
by
<I>addr</I>.
On return it contains the actual size of the name returned (in bytes).
The name is truncated if the buffer provided is too small.
<P>
The returned address is truncated if the buffer provided is too small;
in this case,
<I>addrlen</I>
will return a value greater than was supplied to the call.
<A NAME="lbAE">&nbsp;</A>
<H2>RETURN VALUE</H2>
On success, zero is returned.
On error, -1 is returned, and
<I>errno</I>
is set appropriately.
<A NAME="lbAF">&nbsp;</A>
<H2>ERRORS</H2>
<DL COMPACT>
<DT id="1"><B>EBADF</B>
<DD>
The argument
<I>sockfd</I>
is not a valid file descriptor.
<DT id="2"><B>EFAULT</B>
<DD>
The
<I>addr</I>
argument points to memory not in a valid part of the
process address space.
<DT id="3"><B>EINVAL</B>
<DD>
<I>addrlen</I>
is invalid (e.g., is negative).
<DT id="4"><B>ENOBUFS</B>
<DD>
Insufficient resources were available in the system
to perform the operation.
<DT id="5"><B>ENOTCONN</B>
<DD>
The socket is not connected.
<DT id="6"><B>ENOTSOCK</B>
<DD>
The file descriptor
<I>sockfd</I>
does not refer to a socket.
</DL>
<A NAME="lbAG">&nbsp;</A>
<H2>CONFORMING TO</H2>
POSIX.1-2001, POSIX.1-2008, SVr4, 4.4BSD
(<B>getpeername</B>()
first appeared in 4.2BSD).
<A NAME="lbAH">&nbsp;</A>
<H2>NOTES</H2>
For background on the
<I>socklen_t</I>
type, see
<B><A HREF="/cgi-bin/man/man2html?2+accept">accept</A></B>(2).
<P>
For stream sockets, once a
<B><A HREF="/cgi-bin/man/man2html?2+connect">connect</A></B>(2)
has been performed, either socket can call
<B>getpeername</B>()
to obtain the address of the peer socket.
On the other hand, datagram sockets are connectionless.
Calling
<B><A HREF="/cgi-bin/man/man2html?2+connect">connect</A></B>(2)
on a datagram socket merely sets the peer address for outgoing
datagrams sent with
<B><A HREF="/cgi-bin/man/man2html?2+write">write</A></B>(2)
or
<B><A HREF="/cgi-bin/man/man2html?2+recv">recv</A></B>(2).
The caller of
<B><A HREF="/cgi-bin/man/man2html?2+connect">connect</A></B>(2)
can use
<B>getpeername</B>()
to obtain the peer address that it earlier set for the socket.
However, the peer socket is unaware of this information, and calling
<B>getpeername</B>()
on the peer socket will return no useful information (unless a
<B><A HREF="/cgi-bin/man/man2html?2+connect">connect</A></B>(2)
call was also executed on the peer).
Note also that the receiver of a datagram can obtain
the address of the sender when using
<B><A HREF="/cgi-bin/man/man2html?2+recvfrom">recvfrom</A></B>(2).
<A NAME="lbAI">&nbsp;</A>
<H2>SEE ALSO</H2>
<B><A HREF="/cgi-bin/man/man2html?2+accept">accept</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+bind">bind</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+getsockname">getsockname</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?7+ip">ip</A></B>(7),
<B><A HREF="/cgi-bin/man/man2html?7+socket">socket</A></B>(7),
<B><A HREF="/cgi-bin/man/man2html?7+unix">unix</A></B>(7)
<A NAME="lbAJ">&nbsp;</A>
<H2>COLOPHON</H2>
This page is part of release 5.05 of the Linux
<I>man-pages</I>
project.
A description of the project,
information about reporting bugs,
and the latest version of this page,
can be found at
<A HREF="https://www.kernel.org/doc/man-pages/.">https://www.kernel.org/doc/man-pages/.</A>
<P>
<HR>
<A NAME="index">&nbsp;</A><H2>Index</H2>
<DL>
<DT id="7"><A HREF="#lbAB">NAME</A><DD>
<DT id="8"><A HREF="#lbAC">SYNOPSIS</A><DD>
<DT id="9"><A HREF="#lbAD">DESCRIPTION</A><DD>
<DT id="10"><A HREF="#lbAE">RETURN VALUE</A><DD>
<DT id="11"><A HREF="#lbAF">ERRORS</A><DD>
<DT id="12"><A HREF="#lbAG">CONFORMING TO</A><DD>
<DT id="13"><A HREF="#lbAH">NOTES</A><DD>
<DT id="14"><A HREF="#lbAI">SEE ALSO</A><DD>
<DT id="15"><A HREF="#lbAJ">COLOPHON</A><DD>
</DL>
<HR>
This document was created by
<A HREF="/cgi-bin/man/man2html">man2html</A>,
using the manual pages.<BR>
Time: 00:05:32 GMT, March 31, 2021
</BODY>
</HTML>