1GETPEERNAME(2) Linux Programmer's Manual GETPEERNAME(2)
2
3
4
6 getpeername - get name of connected peer socket
7
9 #include <sys/socket.h>
10
11 int getpeername(int sockfd, struct sockaddr *restrict addr,
12 socklen_t *restrict addrlen);
13
15 getpeername() returns the address of the peer connected to the socket
16 sockfd, in the buffer pointed to by addr. The addrlen argument should
17 be initialized to indicate the amount of space pointed to by addr. On
18 return it contains the actual size of the name returned (in bytes).
19 The name is truncated if the buffer provided is too small.
20
21 The returned address is truncated if the buffer provided is too small;
22 in this case, addrlen will return a value greater than was supplied to
23 the call.
24
26 On success, zero is returned. On error, -1 is returned, and errno is
27 set to indicate the error.
28
30 EBADF The argument sockfd is not a valid file descriptor.
31
32 EFAULT The addr argument points to memory not in a valid part of the
33 process address space.
34
35 EINVAL addrlen is invalid (e.g., is negative).
36
37 ENOBUFS
38 Insufficient resources were available in the system to perform
39 the operation.
40
41 ENOTCONN
42 The socket is not connected.
43
44 ENOTSOCK
45 The file descriptor sockfd does not refer to a socket.
46
48 POSIX.1-2001, POSIX.1-2008, SVr4, 4.4BSD (getpeername() first appeared
49 in 4.2BSD).
50
52 For background on the socklen_t type, see accept(2).
53
54 For stream sockets, once a connect(2) has been performed, either socket
55 can call getpeername() to obtain the address of the peer socket. On
56 the other hand, datagram sockets are connectionless. Calling con‐
57 nect(2) on a datagram socket merely sets the peer address for outgoing
58 datagrams sent with write(2) or recv(2). The caller of connect(2) can
59 use getpeername() to obtain the peer address that it earlier set for
60 the socket. However, the peer socket is unaware of this information,
61 and calling getpeername() on the peer socket will return no useful in‐
62 formation (unless a connect(2) call was also executed on the peer).
63 Note also that the receiver of a datagram can obtain the address of the
64 sender when using recvfrom(2).
65
67 accept(2), bind(2), getsockname(2), ip(7), socket(7), unix(7)
68
70 This page is part of release 5.12 of the Linux man-pages project. A
71 description of the project, information about reporting bugs, and the
72 latest version of this page, can be found at
73 https://www.kernel.org/doc/man-pages/.
74
75
76
77Linux 2021-03-22 GETPEERNAME(2)