GETSOCKNAME(2) GETSOCKNAME(2) NAME getsockname - get socket name SYNOPSIS #include <sys/types.h> #include <sys/socket.h> int getsockname (int s, struct sockaddr *name, socklen_t *namelen); #if _XOPEN_SOURCE >= 500 int getsockname (int s, struct sockaddr *name, socklen_t *namelen); #elif _XOPEN_SOURCE < 500 int getsockname (int s, struct sockaddr *name, size_t *namelen); #endif DESCRIPTION Getsockname returns the current name for the specified socket. The namelen parameter should be initialized to indicate the amount of space pointed to by name. On return it contains the actual size of the name returned (in bytes). DIAGNOSTICS A 0 is returned if the call succeeds, -1 if it fails. ERRORS The call succeeds unless: [EBADF] The argument s is not a valid descriptor. [ENOTSOCK] The argument s is a file, not a socket. [ENOBUFS] Insufficient resources were available in the system to perform the operation. [EFAULT] The name parameter points to memory not in a valid part of the process address space. SEE ALSO bind(2), socket(2), getpeername(2) NOTES ABI-compliant versions of the above call can be obtained from libsocket.so. This call does not return useful results when used on sockets of type AF_UNIX, unless the socket was explicitly bound to a pathname. There are three types of getsockname functions in n32 and 64 bit C libraries for IRIX 6.5.19 and later versions. One is the normal type when _XOPEN_SOURCE is not defined; the second is XPG5 type when _XOPEN_SOURCE is set to >= 500; and the third is XPG4 type when _XOPEN_SOURCE set to < 500. The difference between these functions is in the third argument type to getsockname. Refer <sys/socket.h> for alternate definitions of socklen_t type. 1. For the normal case when _XOPEN_SOURCE is not defined, third argument type, a pointer to a socklen_t type, will actually be a pointer to an int and the normal getsockname is used. 2. When _XOPEN_SOURCE is set to >= 500, third argument type, a pointer to a socklen_t type, will actually be a pointer to a u_int32_t type and xpg5 type function will be used. 3. When _XOPEN_SOURCE is set to < 500, third argument type will be a pointer to a size_t and xpg4 type function will be used. XPG5 type function is not supported in o32 C library. The XPG5 type getsockname function is actually defined as a static inline function in <sys/socket.h>, and it calls a new function _xpg5_getsockname which is specific to IRIX 6.5.19 and later. Therefore applications that call XPG5 type getsockname should check the existence of the new symbol. #include <sys/socket.h> #include <optional_sym.h> if (_MIPS_SYMBOL_PRESENT(_xpg5_getsockname)) { getsockname(s, &name, &namelen); } else { ... } Because the static inline function is defined in each source file that includes <sys/socket.h>, these static functions will have different addresses in any cases that inline expansion is not performed. This may cause problems if the address of the function is examined in programs. To avoid this problem, use -D_XPG5_GETSOCKNAME_USER_DEFINED compile option to disable the static inline definition in <sys/socket.h>, and define a user defined function with below definition: int * getsockname(int _s, struct sockaddr *_name, socklen_t *_namelen) { return(_xpg5_getsockname(_s, _name, _namelen)); } Use the compile option always, when a user defined XPG5 getsockname function is required. Page 2