O'Reilly logo

IPv6 Network Administration by David Malone, Niall Richard Murphy

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Other Considerations for Developers

Here we will look at a few tips regarding informal standards that have emerged among those producing IPv6-enabled code.

Switching Between IPv4 and IPv6

In many cases, it may be desirable to give the end user the choice of IPv4 or IPv6 operation. The way this has been implemented in most command-line utilities is to use either type of address by default, and to add flags -4 and -6 to restrict the program to using IPv4 and IPv6 addresses respectively. This is easy to implement by setting the appropriate family in the hints passed to getaddrinfo.

The following code shows how you can set a variable family based on command-line arguments and then use that when constructing the hints argument for getaddrinfo.

        if (argc > 1 && strcmp(argv[1], "-4") =  = 0) {
                family = PF_INET; argc--; argv++;
        } else if (argc > 1 && strcmp(argv[1], "-6") =  = 0) {
                family = PF_INET6; argc--; argv++;
        } else
                family = PF_UNSPEC;

        memset(&hints, 0, sizeof(hints));
        hints.ai_family = family;
        hints.ai_socktype = SOCK_STREAM;
        error = getaddrinfo(host, service, &hints, &res);

How Long Is a sockaddr?

One inconsistency in the sockets API is the presence or absence of a length field as a common member of all sockaddr structures. While we were busily engaged in writing IPv4-only code, this wasn't really an issue; we knew that functions like getpeername would always return sizeof(struct sockaddr_in) bytes. When writing address-family agnostic code, this suddenly becomes a problem. So, how do ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required