]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man2/getpeername.2
Many pages: Use correct letter case in page titles (TH)
[thirdparty/man-pages.git] / man2 / getpeername.2
CommitLineData
fea681da
MK
1.\" Copyright (c) 1983, 1991 The Regents of the University of California.
2.\" All rights reserved.
3.\"
47009d5e 4.\" SPDX-License-Identifier: BSD-4-Clause-UC
fea681da
MK
5.\"
6.\" @(#)getpeername.2 6.5 (Berkeley) 3/10/91
7.\"
8.\" Modified Sat Jul 24 16:37:50 1993 by Rik Faith <faith@cs.unc.edu>
9.\" Modified Thu Jul 30 14:37:50 1993 by Martin Schulze <joey@debian.org>
10.\" Modified Sun Mar 28 21:26:46 1999 by Andries Brouwer <aeb@cwi.nl>
c11b1abf 11.\" Modified 17 Jul 2002, Michael Kerrisk <mtk.manpages@gmail.com>
fea681da
MK
12.\" Added 'socket' to NAME, so that "man -k socket" will show this page.
13.\"
4c1c5274 14.TH getpeername 2 (date) "Linux man-pages (unreleased)"
fea681da
MK
15.SH NAME
16getpeername \- get name of connected peer socket
4cabe0bf
AC
17.SH LIBRARY
18Standard C library
8fc3b2cf 19.RI ( libc ", " \-lc )
fea681da 20.SH SYNOPSIS
c7db92b9 21.nf
fea681da 22.B #include <sys/socket.h>
68e4db0a 23.PP
8de93cea
AC
24.BI "int getpeername(int " sockfd ", struct sockaddr *restrict " addr ,
25.BI " socklen_t *restrict " addrlen );
c7db92b9 26.fi
fea681da 27.SH DESCRIPTION
7145b9a9 28.BR getpeername ()
0b11419e 29returns the address of the peer connected to the socket
c4e7b714 30.IR sockfd ,
0b11419e 31in the buffer pointed to by
a2b8b6d6 32.IR addr .
fea681da 33The
a2b8b6d6 34.I addrlen
c4bb193f 35argument should be initialized to indicate the amount of space pointed to
fea681da 36by
a2b8b6d6 37.IR addr .
c13182ef
MK
38On return it contains the actual size of the name returned (in bytes).
39The name is truncated if the buffer provided is too small.
efeece04 40.PP
cb159038
MK
41The returned address is truncated if the buffer provided is too small;
42in this case,
a2b8b6d6 43.I addrlen
cb159038 44will return a value greater than was supplied to the call.
47297adb 45.SH RETURN VALUE
c13182ef
MK
46On success, zero is returned.
47On error, \-1 is returned, and
fea681da 48.I errno
f6a4078b 49is set to indicate the error.
fea681da
MK
50.SH ERRORS
51.TP
52.B EBADF
53The argument
3e4088f4 54.I sockfd
d9cb0d7d 55is not a valid file descriptor.
fea681da
MK
56.TP
57.B EFAULT
c13182ef 58The
a2b8b6d6 59.I addr
c4bb193f 60argument points to memory not in a valid part of the
fea681da
MK
61process address space.
62.TP
16c892d3 63.B EINVAL
a2b8b6d6 64.I addrlen
16c892d3
MK
65is invalid (e.g., is negative).
66.TP
fea681da
MK
67.B ENOBUFS
68Insufficient resources were available in the system
69to perform the operation.
70.TP
71.B ENOTCONN
72The socket is not connected.
73.TP
74.B ENOTSOCK
deedfd97 75The file descriptor
3e4088f4 76.I sockfd
deedfd97 77does not refer to a socket.
3113c7f3 78.SH STANDARDS
209a8546
MK
79POSIX.1-2001, POSIX.1-2008, SVr4, 4.4BSD
80.RB ( getpeername ()
81first appeared in 4.2BSD).
19c98696 82.SH NOTES
ec5df7af
MK
83For background on the
84.I socklen_t
85type, see
fea681da 86.BR accept (2).
efeece04 87.PP
0c56d917
MK
88For stream sockets, once a
89.BR connect (2)
90has been performed, either socket can call
91.BR getpeername ()
92to obtain the address of the peer socket.
93On the other hand, datagram sockets are connectionless.
94Calling
95.BR connect (2)
96on a datagram socket merely sets the peer address for outgoing
97datagrams sent with
98.BR write (2)
99or
100.BR recv (2).
101The caller of
102.BR connect (2)
103can use
104.BR getpeername ()
105to obtain the peer address that it earlier set for the socket.
106However, the peer socket is unaware of this information, and calling
107.BR getpeername ()
108on the peer socket will return no useful information (unless a
109.BR connect (2)
110call was also executed on the peer).
111Note also that the receiver of a datagram can obtain
112the address of the sender when using
113.BR recvfrom (2).
47297adb 114.SH SEE ALSO
fea681da
MK
115.BR accept (2),
116.BR bind (2),
ecae8a08
MK
117.BR getsockname (2),
118.BR ip (7),
5dfedcae 119.BR socket (7),
ecae8a08 120.BR unix (7)