bind() — Bind a Name to a Socket
| Standards/Extensions | C or C++ | Dependencies |
|---|---|---|
POSIX.1
XPG4.2 OpenExtensions |
both | POSIX(ON) |
Format
#define _XOPEN_SOURCE_EXTENDED 1
#include <sys/socket.h>
int bind(int socket, const struct sockaddr *address, size_t address_len);#define _OE_SOCKETS
#include <sys/types.h>
#include <sys/socket.h>
int bind(int socket, struct sockaddr *address, int *address_len);General Description
- socket
- The socket descriptor returned by a previous socket() call.
- address
- The pointer to a
sockaddrstructure containing the name that is to be bound to socket. - address_len
- The size of address in bytes.
The socket parameter is a socket descriptor of any type created by calling socket().
The address parameter is a pointer to a buffer containing the name to be bound to socket. The address_len parameter is the size, in bytes, of the buffer pointed to by address. For AF_UNIX, this function creates a file that you later need to unlink besides closing the socket.
Sockets in the AF_INET Domain
sockaddr_in,
as defined in the netinet/in.h header file.
struct in_addr
{
in_addr_t s_addr;
};
struct sockaddr_in {
unsigned char sin_len; /* length of sockaddr struct */
unsigned char sin_family; /* addressing family */
unsigned short sin_port; /* port number */
struct in_addr sin_addr; /* IP address */
unsigned char sin_zero[8]; /* unassigned */
};The sin_len field must be set to either 0 or sizeof(struct
sockaddr_in). Both values are treated the same.
The sin_family field must be set to AF_INET.
The sin_port field is set to the port to which the
application must bind. It must be specified in network byte order.
If sin_port is set to 0, the caller leaves
it to the system to assign an available port.
The application can call getsockname() to discover the port number
assigned.
The sin_addr.s_addr field is set to the internet address and must be specified in network byte order. On hosts with more than one network interface (called multihomed hosts), a caller can select the interface to which it is to bind. Subsequently, only UDP packets and TCP connection requests from this interface (which match the bound name) are routed to the application.
If this field is set to the constant INADDR_ANY, as defined in netinet/in.h, the caller is requesting that the socket be bound to all network interfaces on the host. Subsequently, UDP packets and TCP connections from all interfaces (which match the bound name) are routed to the application. This becomes important when a server offers a service to multiple networks. By leaving the address unspecified, the server can accept all UDP packets and TCP connection requests made for its port, regardless of the network interface on which the requests arrived.
The sin_zero field is not used and must be set to all zeros.
Sockets in the AF_INET6 Domain
struct sockaddr_in6 {
uint8_t sin6_len; /* length of sockaddr structure */
sa_family_t sin6_family; /* addressing family */
in_port_t sin6_port; /* port number */
uint32_t sin6_flowinfo; /* ignored */
struct in6_addr sin6_addr; /* IP address */
uint32_t sin6_scope_id; /* scope ID */
};The sin6_len field must be set to either 0 or sizeof(struct
sockaddr_in6). Both values are treated the same.
The sin6_family must be set to AF_INET6.
The sin6_port field is set to the port to which the socket is bound. It must be specified in network byte order.
The sin6_flowinfo field is currently unsupported so its contents are ignored.
The sin6_addr.s6_addr field is set to the internet address of the interface to which the socket is bound. It must be specified in network byte order.
The sin6_scope_id field identifies a set of interfaces as appropriate for the scope of the address carried in the sin6_addr field. For link local addresses, the sin6_scope_id can be used to specify the outgoing interface index. The z/VM® stack supports sin6_scope_id for link local addresses only
Sockets in the AF_IUCV Domain
sockaddr_iucv,
as defined in the saiucv.h header file.
struct sockaddr_iucv {
unsigned char siucv_len; /* length of sockaddr struct */
unsigned char siucv_family; /* addressing family */
unsigned short siucv_port; /* port number */
unsigned long siucv_addr; /* address */
unsigned char siucv_nodeid[8]; /* nodeid to connect to */
unsigned char siucv_userid[8]; /* userid to connect to */
unsigned char siucv_name[8]; /* iucvname for connect */
};The siucv_len field must be set to either 0 or sizeof(struct
sockaddr_iucv). Both values are treated the same.
The siucv_family field must be set to AF_IUCV.
The siucv_port, siucv_addr, and siucv_nodeid fields are reserved for future use. The siucv_port and siucv_addr fields must be zeroed. The siucv_nodeid field must be set to exactly 8 blank characters.
The siucv_userid field need not be initialized for a bind() call. The VM user ID running the calling program will be used.
Sockets in the AF_UNIX Domain
sockaddr_un,
as defined in the sys/un.h header file.
struct sockaddr_un {
unsigned char sun_len; /* length of sockaddr struc */
unsigned char sun_family; /* addressing family */
char sun_path[108]; /* pathname */
};The sun_len field must be set to either 0 or
a value greater than or equal to SUN_LEN(&sa), where sa is
the name of the sockaddr_un variable, but less than or equal
to sizeof(struct sockaddr_un). The SUN_LEN()
macro, which is defined in sys/un.h, evaluates to an expression that
returns the total length of the used portion of the sockaddr_un structure,
when sun_path has been filled in with a null-terminated
file name. The length returned by SUN_LEN() does not include the
terminating null character. The SUN_LEN() macro may be used to
derive a value for the address_len argument too. If 0 is
specified for sun_len, the address_len value
determines how long the path name is. If sun_len is nonzero,
the lesser of sun_len and address_len is used.
In either case, if a null character appears in the string before the
given length, path name is considered to end there.
The sun_family field must be set to AF_UNIX.
The sun_path field should contain the name of the file that will represent the open socket. It need not be null delimited, although it is recommended that it is, so that the SUN_LEN() macro can be used. A file by this name will be created in the Byte File System, so the form of the path name should follow the POSIX conventions. Because the client process must specify this file on its connect() function call, the path name specified should be an absolute path name (beginning with a single slash character). The bind() call will not replace an existing file with the given name, so the unlink() function should be used first to erase any such file, if the file may already exist (say, from a previous, perhaps abnormally terminated, execution of the program). Similarly, the close() function will not erase this file when the socket is closed, so the application should use unlink() after closing the socket to erase the file. The file must exist when any other application uses the connect() function to connect to the socket.
Notes:
- For AF_UNIX, when a bind() is issued, a file is created with a mode of 660. In order to allow other users to access this file, you should issue a chmod() to modify this mode.
- For AF_UNIX, when closing sockets that were bound, you should also use unlink() to delete the file created at bind() time.
- The path name the client uses on the bind() must be unique.
- The sendto() call must specify the path name associated with the server.
- For AF_INET and AF_INET6, the user must have appropriate privileges to bind to a port in the range from 1 to 1023.
Special Behavior for C++
To use this function with C++, you must use the _XOPEN_SOURCE_EXTENDED 1 feature test macro.
Returned Value
If successful, bind() returns a value of 0.
-1 and
sets errno to one of the following values under the conditions described:
- EACCES
- Permission is denied.
For AF_INET and AF_INET6 only, permission is denied. A user that is not in the obeylist attempted to bind to a port between 0-1023 when RESTRICTLOWPORTS has been specified on the ASSORTEDPARMS statement. Or, a user attempted to bind to a port that has previously been reserved using a PORT statement in the TCP/IP cofiguration file or through obeyfile processing.
- EADDRINUSE
- For AF_INET AF_INET6, and AF_IUCV only, the address is already in use. For information about the AF_INET domain, see the SO_REUSEADDR option described under getsockopt() — Get Options Associated with a Socket and the SO_REUSEADDR option described under setsockopt() — Set Options Associated with a Socket.
- EADDRNOTAVAIL
- The address specified is not valid on this host. For example, the internet address does not specify a valid network interface.
- EAFNOSUPPORT
- The address family is not supported (it is not AF_INET, AF_INET6, AF_IUCV, or AF_UNIX), or the family field in the sockaddr structure does not match the family value specified on the socket() function.
- EBADF
- The socket parameter is not a valid file descriptor.
- EFAULT
- The address and address_len parameters result in an attempt to copy the address into a nonwritable portion of the caller's virtual storage.
- EINVAL
- The socket is already bound to an address—for example, trying to bind a name to a socket that is already connected—or the socket was shut down. This error also occurs for the AF_IUCV domain if the siucv_nodeid field in address is not all blanks or if the siucv_addr or siucv_port fields are nonzero.
- EIO
- A network or transport failure has occurred.
- ENOBUFS
- The bind() function is unable to obtain a buffer due to insufficient storage.
- ENOTSOCK
- The descriptor is a valid file descriptor, but it is not a socket.
- EOPNOTSUPP
- The socket type of the specified socket does not support binding to an address.
- EPERM
- The user is not authorized to bind to the port specified.
- EACCES
- The process does not have search permission on a component of the path prefix, or it does not have write access to the directory of the requested name.
- EDESTADDRREQ
- The address argument is a null pointer.
- EEXIST
- The file name already exists. You should issue unlink() before issuing bind().
- EIO
- An I/O error occurred.
- ELOOP
- Too many symbolic links were encountered in translating the path name in address.
- ENAMETOOLONG
- A component of a path name exceeded NAME_MAX characters, or an entire path name exceeded PATH_MAX characters.
- ENOENT
- A component of the path name does not name an existing file or the path name is an empty string.
- ENOTDIR
- A component of the path prefix of the path name in address is not a directory.
- EROFS
- The name would reside on a read-only file system.
Example
The following are examples of the bind() call. It is a good idea to zero the structure before using it to ensure that the name requested does not set any reserved fields.
AF_INET Domain Example
int rc;
int s;
struct sockaddr_in myname;
/* Bind to a specific interface in the internet domain */
/* make sure the sin_zero field is cleared */
memset(&myname, 0, sizeof(myname));
myname.sin_family = AF_INET;
myname.sin_addr.s_addr = inet_addr("129.5.24.1"); /* specific interface */
myname.sin_port = htons(1024);
⋮
rc = bind(s, (struct sockaddr *) &myname, sizeof(myname));
/* Bind to all network interfaces in the internet domain */
/* make sure the sin_zero field is cleared */
memset(&myname, 0, sizeof(myname));
myname.sin_family = AF_INET;
myname.sin_addr.s_addr = INADDR_ANY; /* specific interface */
myname.sin_port = htons(1024);
⋮
rc = bind(s, (struct sockaddr *) &myname, sizeof(myname));
/* Bind to a specific interface in the internet domain.
Let the system choose a port */
/* make sure the sin_zero field is cleared */
memset(&myname, 0, sizeof(myname));
myname.sin_family = AF_INET;
myname.sin_addr.s_addr = inet_addr("129.5.24.1");
/* specific interface */
myname.sin_port = 0;
⋮
rc = bind(s, (struct sockaddr *) &myname,
sizeof(myname));AF_INET6 Domain Example
int s; /* Socket descriptor. */
int rc;
char v6addr[16] = "C2C1::129:5:24:1"; /* IPv6 IP address. */
struct sockaddr_in6 myname;
struct in6_addr IN6ADDR_ANY = IN6ADDR_ANY_INIT; /* Init required by VM. */
/* Bind to a specific interface in the AF_INET6 domain. */
/* (Not specifying flow info and scope ID.) */
memset(&myname,0,sizeof(myname));
myname.sin6_len = sizeof(myname);
myname.sin6_family = AF_INET6;
myname.sin6_addr = inet_pton(AF_INET6,(char *)&v6addr,&(myname.sin6_addr));
myname.sin6_port = htons(1024);
rc = bind(s,struct sockaddr *)&myname,sizeof(myname));
/* Bind to all network interfaces in the AF_INET6 domain. */
memset(&myname,0,sizeof(myname));
myname.sin6_len = sizeof(myname);
myname.sin6_family = AF_INET6;
myname.sin6_addr = IN6ADDR_ANY;
myname.sin6_port = htons(1024);
rc = bind(s,struct sockaddr *)&myname,sizeof(myname));AF_IUCV Domain Example
int rc;
int s;
struct sockaddr_iucv myname;
#define SERVER_NAME "APPL1"
/* Bind to a name in the IUCV domain */
/* make sure the siucv_addr, siucv_port fields are zeroed and the
siucv_nodeid field is set to blanks */
memset(&myname, 0, sizeof myname);
myname.siucv_family = AF_IUCV;
memset(myname.siucv_nodeid, ' ', sizeof myname.siucv_nodeid);
memset(myname.siucv_name, ' ', sizeof myname.siucv_name);
memcpy(myname.siucv_name, SERVER_NAME, sizeof SERVER_NAME -1);
rc = bind(s, (struct sockaddr *) &myname, sizeof(myname));AF_UNIX Domain Example
int rc;
int s;
/* Bind to a name in the UNIX domain */
struct sockaddr_un myname;
char socket_name[]="/tmp/socket.for._";
⋮
memset(&myname, 0, sizeof(myname));
myname.sun_family = AF_UNIX;
strcpy(myname.sun_path,socket_name);
myname.sun_len = sizeof(myname.sun_path);
⋮
rc = bind(s, (struct sockaddr *) &myname, SUN_LEN(&myname));Related Information
- connect() — Connect a Socket
- gethostbyname() — Get a Host Entry by Name
- getnetbyname() — Get a Network Entry by Name
- getsockname() — Get Name of a Socket
- htons() — Translate an Unsigned Short Integer into Network Byte Order
- inet_addr() — Translate an Internet Address into Network Order
- listen() — Prepare Server for Incoming Client Requests
- socket() — Create a Socket