#include <sys/types.h> #include <sys/socket.h> int getpeername(int socket_descriptor, struct sockaddr *destination_address, int *address_length)
#define _XOPEN_SOURCE 520 #include <sys/socket.h> int getpeername(int socket_descriptor, struct sockaddr *destination_address, socklen_t *address_length)
The getpeername() function is used to retrieve the destination address to which the socket is connected.
There are two versions of the API, as shown above. The base i5/OS API uses BSD 4.3 structures and syntax. The other uses syntax and structures compatible with the UNIX 98 programming interface specifications. You can select the UNIX 98 compatible interface with the _XOPEN_SOURCE macro.
The BSD 4.3 structure is:
struct sockaddr { u_short sa_family; char sa_data[14]; };
The BSD 4.4/UNIX 98 compatible structure is:
typedef uchar sa_family_t; struct sockaddr { uint8_t sa_len; sa_family_t sa_family; char sa_data[14]; };
The BSD 4.4 sa_len field is the length of the address. The sa_family field identifies the address family to which the address belongs, and sa_data is the address whose format is dependent on the address family.
Note: See the usage notes about using different address families with sockaddr_storage.
No authorization is required.
getpeername() returns an integer. Possible values are:
When getpeername() fails, errno can be set to one of the following:
[EBADF] | Descriptor not valid. |
[EFAULT] | Bad address.
The system detected an address which was not valid while attempting to access the destination_address or address_length parameters. |
[EINVAL] | Parameter not valid.
The address_length parameter specifies a negative value. |
[EIO] | Input/output error. |
[ENOBUFS] | There is not enough buffer space for the
requested operation. |
[ENOTCONN] | Requested operation requires a connection. |
[ENOTSOCK] | The specified descriptor does not reference a
socket. |
[EUNKNOWN] | Unknown system state. |
[EUNATCH] | The protocol required to support the specified address family is not available at this time. |
Message ID | Error Message Text |
---|---|
CPE3418 E | Possible APAR condition or hardware failure. |
CPF9872 E | Program or service program &1 in library &2 ended. Reason code &3. |
CPFA081 E | Unable to set return value or error code. |
The BSD 4.3 structure is:
#define _SS_MAXSIZE 304 #define _SS_ALIGNSIZE (sizeof (char*)) #define _SS_PAD1SIZE (_SS_ALIGNSIZE - sizeof(sa_family_t)) #define _SS_PAD2SIZE (_SS_MAXSIZE - (sizeof(sa_family_t)+ _SS_PAD1SIZE + _SS_ALIGNSIZE)) struct sockaddr_storage { sa_family_t ss_family; char _ss_pad1[_SS_PAD1SIZE]; char* _ss_align; char _ss_pad2[_SS_PAD2SIZE]; };
The BSD 4.4/UNIX 98 compatible structure is:
#define _SS_MAXSIZE 304 #define _SS_ALIGNSIZE (sizeof (char*)) #define _SS_PAD1SIZE (_SS_ALIGNSIZE - (sizeof(uint8_t) + sizeof(sa_family_t))) #define _SS_PAD2SIZE (_SS_MAXSIZE - (sizeof(uint8_t) + sizeof(sa_family_t)+ _SS_PAD1SIZE + _SS_ALIGNSIZE)) struct sockaddr_storage { uint8_t ss_len; sa_family_t ss_family; char _ss_pad1[_SS_PAD1SIZE]; char* _ss_align; char _ss_pad2[_SS_PAD2SIZE]; };
Top | UNIX-Type APIs | APIs by category |