Name to address resolutionTopThe IPv6 ProtocolIPv6 API

IPv6 API

This chapter will describe the IPv6 API into the standard libraries. If you have written programs for IPv4 before, you'll see most of it has not changed that much, unless you need to access the more advanced features of IPv6.

API Structures

For IPv4 the structures used include sockaddr, sockaddr_in, in_addr , hostent, servent and protoent. We won't mention things like u32, unsigned longs and other evils here, I'm sure noone uses those!

In a way, things have become a lot simpler, there is now sockaddr_storage and addrinfo. These two structures should do the majority of what you need.

Sockaddr Storage

We've all used sockaddr and sockaddr_in for holding IP or other socket type addresses. As the size of the socket address has grown, there now needs to be a new type of structure. It is sockaddr_storage and looks like the following:
struct sockaddr_storage {
  u_char ss_len;
  u_char ss_family;
  u_char padding[128-2];
};

The main difference between this structure and the old one is that it much larger so it can hold the larger addresses.

addrinfo

The address information structure is used by getaddrinfo and getnameinfo functions (describe later ) is called addrinfo. It replaces a lot of those *ent structs.

struct addrinfo {
  int              ai_flags;
  int              ai_family;
  int              ai_socktype;
  int              ai_protocol;
  size_t           ai_addrlen;
  struct sockaddr  *ai_addr;
  char             *ai_canonname;
  struct addrinfo  *ai_next;
};

Notice that the structure looks a little like the old struct hostent structure but with the additional protocol line and a pointer to the same time.

Most functions return a singularly linked list of these structures. You traverse the list by moving through the ai_next items until you hit null.

API Functions

s There has been a big clean-up of the functions, or system calls, used for IPv6. Most of what you need to do can be covered by two functions. For advanced IPv6 specific features, look at the next section for information.

This first set of functions are similar to the ones used in IPv4. In fact these functions can be used for IPv4 instead of the regular ones. The advantage is that most code can use IPv4 or IPv6 and it will work fine, which was a major goal of the API designers.

The first function is getaddrinfo which is used to do the work that getipnodebyname and getservbyname can do. In other words the function does the name to address translation. The good thing is that you can do hostname and service name translation at the same time, it returns a list and it is thread safe unlike most of the old functions.

Advanced API Functions

IPv6 can do a lot more than 4


Name to address resolutionTopThe IPv6 ProtocolIPv6 API