The inet_aton and inet_addr functions have traditionally been liberal in what th

The inet_aton and inet_addr functions have traditionally been liberal in what theyaccept as a dotted-decimal IPv4 address string: allowing from one to four numbersseparated by decimal points, and allowing a leading 0x to specify a hexadecimalnumber, or a leading 0 to specify an octal number. (Try telnet 0xe to see thisbehavior.) inet_pton is much stricter with IPv4 address and requires exactly fournumbers separated by three decimal points, with each number being a decimalnumber between 0 and 255. inet_pton does not allow a dotted-decimal number tobe specified when the address family is AF_INET6, although one could argue thatthese should be allowed and the return value should be the IPv4-mapped IPv6address for the dotted-decimal string (Figure A.10).Write a new function named inet_pton_loose that handles these scenarios: If theaddress family is AF_INET and inet_pton returns 0, call inet_aton and see if itsucceeds. Similarly, if the address family is AF_INET6 and inet_pton returns 0, callinet_aton and if it succeeds, return the IPv4-mapped IPv6 address.

You may also like