bind(2) — 4 BSD
NAME
bind − bind a name to a socket
SYNOPSIS
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h> /∗ for Unix domain only ∗/
#include <netinet/in.h> /∗ for Internet domain only ∗/
int bind (s, name, namelen)
int s;
struct sockaddr ∗name;
int namelen;
DESCRIPTION
bind assigns a name to an unnamed socket. After being created by the socket(2) system call, a socket exists in a name space (address family) but has no name assigned. bind requests the name be assigned to the socket. Since the system call is applicable to various communication domains, the address formats varies.
BINDING NAMES TO SOCKETS IN CX/UX
The system creates a file of socket type in the file system when binding name to a socket. This file must not have existed and and must be deleted by the caller when it is no longer needed (using unlink(2)). The file created is a side-effect of the current implementation, and will not be created in future versions of the Unix ipc domain.
The argument name points to the following structure in the file <sys/un.h>:
struct sockaddr_un {
short sun_family;
char sun_path[XXX];
};
Sun_family is set with AF_UNIX, and sun_path is set with the socket name. The length of sun_path is 108, as defined by the 88open Binary Compatibility Standard (BCS). The name is null terminated if it is less than the maximum length of sun_path. Namelen argument can be up to 111 characters.
Notes The C-compiler operator sizeof(struct sockaddr_un) is very architecture specific. Application programmers should not specify "sizeof(struct sockaddr_un)" as the value of the namelen argument when binding in the CX/UX domain.
BINDING NAMES TO SOCKETS IN INTERNET
The argument namelen must be at most 112. The argument name points to the following structure in the file <inet/in.h>:
struct sockaddr_in {
short sin_family;
u_short sin_port;
struct in_addr sin_addr;
char sin_zero[8];
};
The field sin_port must be in Internet byte order. If the field sin_addr is left unspecified (NULL value), the system will assign a default address. If the process reads or writes through a socket without binding first, the system will assign that socket a default name.
RETURN VALUE
If the bind is successful, a 0 value is returned. A return value of −1 indicates an error, which is further specified in the global errno.
ERRORS
The bind call will fail if:
[EBADF] S is not a valid descriptor.
[ENOTSOCK] S is not a socket.
[EADDRNOTAVAIL] The specified address is not available from the local machine.
[EADDRINUSE] The specified address is already in use.
[EINVAL] The socket is already bound to an address.
[EPERM] The requested address is protected, and the current user has inadequate permission to access it.
[EFAULT] The name parameter is not in a valid part of the user address space.
SECURITY
On CX/SX systems, write permission to the file created by a UX domain bind is needed in order to send or connect to the socket. By default the file created by a UX domain bind has all access bits turned on, a process may then perform a chmod(2) to restrict the access.
NOTE
This call is defined in the 88open Binary and Object Compatibility Standards’ Networking Supplements (BCSNS and OCSNS) for use in BCS/OCS compliant networking applications. OCS/OCSNS-defined functions may be accessed by passing OCS options to cc(1) and ld(1).
SEE ALSO
connect(2), listen(2), socket(2), getsockname(2). The rules used in name binding vary between communication domains. Consult the manual entries in Section 4 for detailed information.
CX/UX Programmer’s Reference Manual