Why is h_addr_list in struct hostent a char ** instead of struct in_addr **?

I am new to network programming. The following structure definitions are quite confusing to me. Here h_addr_list defined as a string array, but is used to store an array of in_addr structures. Why was it not defined as struct in_addr **h_addr_list and not char **h_addr_list ?

 struct hostent { char *h_name; /* Official domain name of host */ char **h_aliases; /* Null-terminated array of domain names */ int h_addrtype; /* Host address type (AF_INET) */ int h_length; /* Length of an address, in bytes */ char **h_addr_list; /* Null-terminated array of in_addr structs */ }; struct in_addr { unsigned int s_addr; /* Network byte order (big-endian) */ }; 
+8
c pointers struct network-programming
source share
2 answers

The definition of structure dates back to the era before C supports void * (or void in general or prototypes). In those days, char * was a "universal pointer". This explains some of the oddities of network function interfaces.

This also refers to the era when there were many different network systems (IPX / SPX, SNA, TCP / IP, ...). TCP / IP is dominant these days, but even now you may have an IPv4 array or an array of returned IPv6 addresses, so specifying a struct in_addr or struct in6_addr can cause problems.

It was assumed that you would have an array of pointers to the corresponding structure types. Currently, void **h_addr_list would be written - an array of void * . But this parameter was not available when the structures were first defined, and the rest is history (you do not change the interface after standardization, if you can avoid it).

+11
source share

When the structure was created, the creator was not sure whether AF_INET would be the type of address to receive.

What if h_addrtype is something other than AF_INET? Then h_addr_list will contain addresses that are not struct in_addr .

Now, after a couple of decades, we find that IPV4 addresses are ending. Soon, the struct inaddr will increasingly be replaced by IPV6 addresses.

+2
source share

All Articles