Wildcard addresses simplify local address assignment in the Internet domains AF_INET and AF_INET6.
Assigning an Internet address with a wildcard
You use the bind() function to assign a local name (address) to a socket (see
"Assigning a name to a socket ").
Instead of a particular Internet address, you can also specify INADDR_ANY (for AF_INET) or IN6ADDR_ANY (for AF_INET6) as the Internet address. INADDR_ANY and IN6ADDR_ANY are defined as fixed constants in <netinet/in.h>.
When you use bind() to assign a socket s a name whose Internet address is specified as INADDR_ANY or IN6ADDR_ANY, this means:
Receiving messages:
The socket s bound to INADDR_ANY can receive messages over all the IPv4 network interfaces of its host. This allows socket s to receive all messages addressed to the port number of s and any valid IPv4 address of the host on which socket s resides. For example, if the host has IPv4 addresses 128.32.0.4 and 10.0.0.78, a process to which socket s is assigned can accept connection requests which are addressed to 128.32.0.4 or 10.0.0.78.
The socket s bound to IN6ADDR_ANY can receive messages over all the IPv4 and IPv6 network interfaces of its host. This allows socket s to receive all messages addressed to the port number of s and any valid IPv4 or IPV6 address of the host on which socket s resides. For example, if the host has IPv4 or IPv6 address 128.32.0.4 or 3FFE:0:0:0:A00:6FF:FE08:9A6B, a task to which socket s is assigned can accept connection requests which are addressed to 128.32.0.4 or 3FFE:0:0:0:A00:6FF:FE08:9A6B.
Sending messages:
The socket s bound to INADRR_ANY can send messages over any IPv4 network interface on its host.
The socket s bound to IN6ADDR_ANY can send messages over any network interfaces on its host.
This allows the socket s bound to INADDR_ANY to address any other socket that can be reached via an IPv4 network interface of the host on which socket s resides.
The socket s bound to IN6ADDR_ANY, on the other hand, can address any other socket that can be reached via any network interface of the host on which socket s resides.
The following examples show how a process can bind a local name to a socket without an Internet address being specified. The process only has to specify the port number:
For AF_INET:
#include <sys/types.h> #include <netinet/in.h> #define MYPORT 2222 ... struct sockaddr_in sin; int s; ... s = socket(AF_INET, SOCK_STREAM, 0); sin.sin_family = AF_INET; sin.sin_addr.s_addr = htonl(INADDR_ANY); sin.sin_port = htons(MYPORT); bind(s, &sin, sizeof sin);
For AF_INET6:
#include <sys.types.h> #include <netinet.in.h> #define MYPORT 2222 ... struct in6_addr inaddr_any = IN6ADDR_ANY_INIT; struct sockaddr_in6 sin6; int s; ... s = socket(AF_INET6, SOCK_STREAM, 0); memset(&sin6, 0 , sizeof sin6); sin6.sin6_family = AF_INET6; memcpy(sin6.sin6_addr.s6_addr,sin6addr_any.s6_addr, 16); sin6.sin6_port = htons(MYPORT); bind(s, &sin6, sizeof sin6);
Assigning a port number with a wildcard
A local port can remain unspecified (0 specified). In this case, the system selects a suitable port number for it. The following examples show how a process assigns a socket a local address without specifying the local port number:
For AF_INET:
struct sockaddr_in sin; ... s = socket(AF_INET; SOCK_STREAM, 0); sin.sin_family=AF_INET; sin.sin_addr.s_addr=htonl(INADDR_ANY); sin.sin_port = htons(0); bind(s, &sin, sizeof sin);
For AF_INET6:
struct sockaddr_in6 sin6; struct in6_addr in6addr_any = IN6ADDR_ANY_INIT; ... s = socket(AF_INET6, SOCK_STREAM, 0); memset(&sin6, 0 , sizeof sin6); sin6.sin6_family = AF_INET6; memcpy(sin6.sin6_addr.s6_addr,in6addr_any.s6_addr, 16); sin6.sin6_port = htons(0); bind(s, &sin6, sizeof sin6);