Skip to Content
IPv6 Network Administration
book

IPv6 Network Administration

by Niall Richard Murphy, David Malone
March 2005
Intermediate to advanced
308 pages
10h 32m
English
O'Reilly Media, Inc.
Content preview from IPv6 Network Administration

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 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Deploying IPv6 Networks

Deploying IPv6 Networks

Ciprian Popoviciu, Eric Levy-Abegnoli, Patrick Grossetete
IPv6 Core Protocols Implementation

IPv6 Core Protocols Implementation

Qing Li, Tatuya Jinmei, Keiichi Shima

Publisher Resources

ISBN: 0596009348Errata Page