Standards
Standards / Extensions |
C or C++ |
Dependencies |
XPG4.2
Single UNIX Specification, Version 3
|
both |
|
Format
X/Open: #define _XOPEN_SOURCE_EXTENDED 1
#include <sys/socket.h>
int getpeername(int socket, struct sockaddr *__restrict__ name,
socklen_t *__restrict__ namelen);
Berkeley
sockets: #define _OE_SOCKETS
#include <sys/types.h>
#include <sys/socket.h>
int getpeername(int socket, struct sockaddr *name,
int *namelen);
General description
The getpeername()
call returns the name of the peer connected to socket descriptor
socket.
namelen must
be initialized to indicate the size of the space pointed to by
name and
is set to the number of bytes copied into the space before the call
returns. The size of the peer name is returned in bytes. If the actual
length of the address is greater than the length of the supplied
sockaddr,
the stored address is truncated. The
sa_len member
of the store structure contains the length of the untruncated address.
- Parameter
- Description
- socket
- The socket descriptor.
- name
- The Internet address of the connected socket that is filled by
getpeername() before it returns. The exact format of name is
determined by the domain in which communication occurs.
- namelen
- Must initially point to an integer that contains the size in bytes
of the storage pointed to by name. On return,
that integer contains the size required to represent the address of
the connecting socket. If this value is larger than the size supplied
on input, then the information contained in sockaddr is truncated
to the length supplied on input. If name is
NULL, namelen is ignored.
Sockets in the AF_INET6 domain: For
an AF_INET6 socket, the address is returned in a sockaddr_in6 address
structure. The sockaddr_in6 structure is defined in the header file netinet/in.h.
Special
behavior for C++: To
use this function with
C++, you
must use the _XOPEN_SOURCE_EXTENDED 1 feature test macro.
Note: The
getpeername() function has a dependency on the level of the Enhanced
ASCII Extensions. See
Enhanced ASCII support for
details.
Returned value
If successful, getpeername()
returns 0.
If unsuccessful, getpeername() returns -1 and sets
errno to one of the following values:
- Error Code
- Description
- EBADF
- The socket parameter is not a valid
socket descriptor.
- EFAULT
- Using the name and namelen parameters
as specified would result in an attempt to access storage outside
of the caller's address space.
- EINVAL
- The namelen parameter is not a valid
length. The socket has been shut down.
- ENOBUFS
- getpeername() is unable to process the request due to insufficient
storage.
- ENOTCONN
- The socket is not in the connected state.
- ENOTSOCK
- The descriptor is for a file, not for a socket.
- EOPTNOTSUPP
- The operation is not supported for the socket protocol.