Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

Assigning addresses with wildcards (AF_INET, AF_INET6)

&pagelevel(4)&pagelevel

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. Instead of a concrete 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 a 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:

  • The socket s bound to INADDR_ANY can receive messages via 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 lies. For example, if the host has IPv4 addresses 128.32.0.4 and 10.0.0.78, a task to which socket s is assigned can accept connection requests which are addressed to 128.32.0.4 and 10.0.0.78.

  • The socket s bound to IN6ADDR_ANY can receive messages via 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 lies. For example, if the host has IPv4 or IPv6 address 128.32.0.4 or 3FFE:1:1000:1000:52C1:D5FF:FE0E:2B01, a task to which socket s is assigned can accept connection requests which are addressed to 128.32.0.4 and 3FFE:1:1000:1000:52C1:D5FF:FE0E:2B01.

The following examples show how a task can bind a local name to a socket without an Internet address being specified. The task 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);
sin6.sin6_family = AF_INET6;
memcpy(sin6.sin6_addr.s6_addr, in6addr_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 task 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);
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);

Automatic address assignment by the system

You can still call a function for a socket which actually requires a bound socket (e.g. connect(), sendto(), etc.) even if the socket has no address assigned to it. In this case, the system executes an implicit bind() call with wildcards for the Internet address and port number, i.e. the socket is bound with INADDR_ANY to all IPv4 addresses and with IN6ADDR_ANY to all IPv6 addresses and IPv4 addresses of the host and receives a port number from a free range.