Next: , Previous: , Up: Host Addresses   [Contents][Index]


16.6.2.2 Host Address Data Type

IPv4 Internet host addresses are represented in some contexts as integers (type uint32_t). In other contexts, the integer is packaged inside a structure of type struct in_addr. It would be better if the usage were made consistent, but it is not hard to extract the integer from the structure or put the integer into a structure.

You will find older code that uses unsigned long int for IPv4 Internet host addresses instead of uint32_t or struct in_addr. Historically unsigned long int was a 32-bit number but with 64-bit machines this has changed. Using unsigned long int might break the code if it is used on machines where this type doesn’t have 32 bits. uint32_t is specified by Unix98 and guaranteed to have 32 bits.

IPv6 Internet host addresses have 128 bits and are packaged inside a structure of type struct in6_addr.

The following basic definitions for Internet addresses are declared in the header file netinet/in.h:

Data Type: struct in_addr

This data type is used in certain contexts to contain an IPv4 Internet host address. It has just one field, named s_addr, which records the host address number as an uint32_t.

Macro: uint32_t INADDR_LOOPBACK

You can use this constant to stand for “the address of this machine,” instead of finding its actual address. It is the IPv4 Internet address ‘127.0.0.1’, which is usually called ‘localhost’. This special constant saves you the trouble of looking up the address of your own machine. Also, the system usually implements INADDR_LOOPBACK specially, avoiding any network traffic for the case of one machine talking to itself.

Macro: uint32_t INADDR_ANY

You can use this constant to stand for “any incoming address” when binding to an address. See Setting Address. This is the usual address to give in the sin_addr member of struct sockaddr_in when you want to accept Internet connections.

Macro: uint32_t INADDR_BROADCAST

This constant is the address you use to send a broadcast message.

Macro: uint32_t INADDR_NONE

This constant is returned by some functions to indicate an error.

Data Type: struct in6_addr

This data type is used to store an IPv6 address. It stores 128 bits of data, which can be accessed (via a union) in a variety of ways.

Constant: struct in6_addr in6addr_loopback

This constant is the IPv6 address ‘::1’, the loopback address. See above for a description of what this means. The macro IN6ADDR_LOOPBACK_INIT is provided to allow you to initialize your own variables to this value.

Constant: struct in6_addr in6addr_any

This constant is the IPv6 address ‘::’, the unspecified address. See above for a description of what this means. The macro IN6ADDR_ANY_INIT is provided to allow you to initialize your own variables to this value.


Next: , Previous: , Up: Host Addresses   [Contents][Index]