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 *addr, socklen_t *addrlen);
12
14 getpeername() returns the address of the peer connected to the socket
15 sockfd, in the buffer pointed to by addr. The addrlen argument should
16 be initialized to indicate the amount of space pointed to by addr. On
17 return it contains the actual size of the name returned (in bytes).
18 The name is truncated if the buffer provided is too small.
19
20 The returned address is truncated if the buffer provided is too small;
21 in this case, addrlen will return a value greater than was supplied to
22 the call.
23
25 On success, zero is returned. On error, -1 is returned, and errno is
26 set appropriately.
27
29 EBADF The argument sockfd is not a valid descriptor.
30
31 EFAULT The addr argument points to memory not in a valid part of the
32 process address space.
33
34 EINVAL addrlen is invalid (e.g., is negative).
35
36 ENOBUFS
37 Insufficient resources were available in the system to perform
38 the operation.
39
40 ENOTCONN
41 The socket is not connected.
42
43 ENOTSOCK
44 The argument sockfd is a file, not a socket.
45
47 SVr4, 4.4BSD (the getpeername() function call first appeared in
48 4.2BSD), POSIX.1-2001.
49
51 The third argument of getpeername() is in reality an int * (and this is
52 what 4.x BSD and libc4 and libc5 have). Some POSIX confusion resulted
53 in the present socklen_t, also used by glibc. See also accept(2).
54
55 For stream sockets, once a connect(2) has been performed, either socket
56 can call getpeername() to obtain the address of the peer socket. On
57 the other hand, datagram sockets are connectionless. Calling con‐
58 nect(2) on a datagram socket merely sets the peer address for outgoing
59 datagrams sent with write(2) or recv(2). The caller of connect(2) can
60 use getpeername() to obtain the peer address that it earlier set for
61 the socket. However, the peer socket is unaware of this information,
62 and calling getpeername() on the peer socket will return no useful
63 information (unless a connect(2) call was also executed on the peer).
64 Note also that the receiver of a datagram can obtain the address of the
65 sender when using recvfrom(2).
66
68 accept(2), bind(2), getsockname(2), ip(7), socket(7), unix(7)
69
71 This page is part of release 3.53 of the Linux man-pages project. A
72 description of the project, and information about reporting bugs, can
73 be found at http://www.kernel.org/doc/man-pages/.
74
75
76
77Linux 2013-02-12 GETPEERNAME(2)