]>
Commit | Line | Data |
---|---|---|
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 | .\" | |
45186a5d | 14 | .TH GETPEERNAME 2 2021-03-22 "Linux man-pages (unreleased)" |
fea681da MK |
15 | .SH NAME |
16 | getpeername \- get name of connected peer socket | |
4cabe0bf AC |
17 | .SH LIBRARY |
18 | Standard 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 | 29 | returns the address of the peer connected to the socket |
c4e7b714 | 30 | .IR sockfd , |
0b11419e | 31 | in the buffer pointed to by |
a2b8b6d6 | 32 | .IR addr . |
fea681da | 33 | The |
a2b8b6d6 | 34 | .I addrlen |
c4bb193f | 35 | argument should be initialized to indicate the amount of space pointed to |
fea681da | 36 | by |
a2b8b6d6 | 37 | .IR addr . |
c13182ef MK |
38 | On return it contains the actual size of the name returned (in bytes). |
39 | The name is truncated if the buffer provided is too small. | |
efeece04 | 40 | .PP |
cb159038 MK |
41 | The returned address is truncated if the buffer provided is too small; |
42 | in this case, | |
a2b8b6d6 | 43 | .I addrlen |
cb159038 | 44 | will return a value greater than was supplied to the call. |
47297adb | 45 | .SH RETURN VALUE |
c13182ef MK |
46 | On success, zero is returned. |
47 | On error, \-1 is returned, and | |
fea681da | 48 | .I errno |
f6a4078b | 49 | is set to indicate the error. |
fea681da MK |
50 | .SH ERRORS |
51 | .TP | |
52 | .B EBADF | |
53 | The argument | |
3e4088f4 | 54 | .I sockfd |
d9cb0d7d | 55 | is not a valid file descriptor. |
fea681da MK |
56 | .TP |
57 | .B EFAULT | |
c13182ef | 58 | The |
a2b8b6d6 | 59 | .I addr |
c4bb193f | 60 | argument points to memory not in a valid part of the |
fea681da MK |
61 | process address space. |
62 | .TP | |
16c892d3 | 63 | .B EINVAL |
a2b8b6d6 | 64 | .I addrlen |
16c892d3 MK |
65 | is invalid (e.g., is negative). |
66 | .TP | |
fea681da MK |
67 | .B ENOBUFS |
68 | Insufficient resources were available in the system | |
69 | to perform the operation. | |
70 | .TP | |
71 | .B ENOTCONN | |
72 | The socket is not connected. | |
73 | .TP | |
74 | .B ENOTSOCK | |
deedfd97 | 75 | The file descriptor |
3e4088f4 | 76 | .I sockfd |
deedfd97 | 77 | does not refer to a socket. |
3113c7f3 | 78 | .SH STANDARDS |
209a8546 MK |
79 | POSIX.1-2001, POSIX.1-2008, SVr4, 4.4BSD |
80 | .RB ( getpeername () | |
81 | first appeared in 4.2BSD). | |
19c98696 | 82 | .SH NOTES |
ec5df7af MK |
83 | For background on the |
84 | .I socklen_t | |
85 | type, see | |
fea681da | 86 | .BR accept (2). |
efeece04 | 87 | .PP |
0c56d917 MK |
88 | For stream sockets, once a |
89 | .BR connect (2) | |
90 | has been performed, either socket can call | |
91 | .BR getpeername () | |
92 | to obtain the address of the peer socket. | |
93 | On the other hand, datagram sockets are connectionless. | |
94 | Calling | |
95 | .BR connect (2) | |
96 | on a datagram socket merely sets the peer address for outgoing | |
97 | datagrams sent with | |
98 | .BR write (2) | |
99 | or | |
100 | .BR recv (2). | |
101 | The caller of | |
102 | .BR connect (2) | |
103 | can use | |
104 | .BR getpeername () | |
105 | to obtain the peer address that it earlier set for the socket. | |
106 | However, the peer socket is unaware of this information, and calling | |
107 | .BR getpeername () | |
108 | on the peer socket will return no useful information (unless a | |
109 | .BR connect (2) | |
110 | call was also executed on the peer). | |
111 | Note also that the receiver of a datagram can obtain | |
112 | the 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) |