7 Network Working Group R. Gilligan
8 Request for Comments: 2133 Freegate
9 Category: Informational S. Thomson
17 Basic Socket Interface Extensions for IPv6
21 This memo provides information for the Internet community. This memo
22 does not specify an Internet standard of any kind. Distribution of
23 this memo is unlimited.
27 The de facto standard application program interface (API) for TCP/IP
28 applications is the "sockets" interface. Although this API was
29 developed for Unix in the early 1980s it has also been implemented on
30 a wide variety of non-Unix systems. TCP/IP applications written
31 using the sockets API have in the past enjoyed a high degree of
32 portability and we would like the same portability with IPv6
33 applications. But changes are required to the sockets API to support
34 IPv6 and this memo describes these changes. These include a new
35 socket address structure to carry IPv6 addresses, new address
36 conversion functions, and some new socket options. These extensions
37 are designed to provide access to the basic IPv6 features required by
38 TCP and UDP applications, including multicasting, while introducing a
39 minimum of change into the system and providing complete
40 compatibility for existing IPv4 applications. Additional extensions
41 for advanced IPv6 features (raw sockets and access to the IPv6
42 extension headers) are defined in another document [5].
46 1. Introduction ................................................ 2
47 2. Design Considerations ....................................... 3
48 2.1. What Needs to be Changed .................................. 3
49 2.2. Data Types ................................................ 5
50 2.3. Headers ................................................... 5
51 2.4. Structures ................................................ 5
52 3. Socket Interface ............................................ 5
53 3.1. IPv6 Address Family and Protocol Family ................... 5
54 3.2. IPv6 Address Structure .................................... 6
58 Gilligan, et. al. Informational [Page 1]
60 RFC 2133 IPv6 Socket Interface Extensions April 1997
63 3.3. Socket Address Structure for 4.3BSD-Based Systems ......... 6
64 3.4. Socket Address Structure for 4.4BSD-Based Systems ......... 7
65 3.5. The Socket Functions ...................................... 8
66 3.6. Compatibility with IPv4 Applications ...................... 9
67 3.7. Compatibility with IPv4 Nodes ............................. 9
68 3.8. IPv6 Wildcard Address ..................................... 10
69 3.9. IPv6 Loopback Address ..................................... 11
70 4. Interface Identification .................................... 12
71 4.1. Name-to-Index ............................................. 13
72 4.2. Index-to-Name ............................................. 13
73 4.3. Return All Interface Names and Indexes .................... 14
74 4.4. Free Memory ............................................... 14
75 5. Socket Options .............................................. 14
76 5.1. Changing Socket Type ...................................... 15
77 5.2. Unicast Hop Limit ......................................... 16
78 5.3. Sending and Receiving Multicast Packets ................... 17
79 6. Library Functions ........................................... 19
80 6.1. Hostname-to-Address Translation ........................... 19
81 6.2. Address To Hostname Translation ........................... 22
82 6.3. Protocol-Independent Hostname and Service Name Translation 22
83 6.4. Socket Address Structure to Hostname and Service Name ..... 25
84 6.5. Address Conversion Functions .............................. 27
85 6.6. Address Testing Macros .................................... 28
86 7. Summary of New Definitions .................................. 29
87 8. Security Considerations ..................................... 31
88 9. Acknowledgments ............................................. 31
89 10. References ................................................. 31
90 11. Authors' Addresses ......................................... 32
94 While IPv4 addresses are 32 bits long, IPv6 interfaces are identified
95 by 128-bit addresses. The socket interface make the size of an IP
96 address quite visible to an application; virtually all TCP/IP
97 applications for BSD-based systems have knowledge of the size of an
98 IP address. Those parts of the API that expose the addresses must be
99 changed to accommodate the larger IPv6 address size. IPv6 also
100 introduces new features (e.g., flow label and priority), some of
101 which must be made visible to applications via the API. This memo
102 defines a set of extensions to the socket interface to support the
103 larger address size and new features of IPv6.
114 Gilligan, et. al. Informational [Page 2]
116 RFC 2133 IPv6 Socket Interface Extensions April 1997
119 2. Design Considerations
121 There are a number of important considerations in designing changes
122 to this well-worn API:
124 - The API changes should provide both source and binary
125 compatibility for programs written to the original API. That is,
126 existing program binaries should continue to operate when run on
127 a system supporting the new API. In addition, existing
128 applications that are re-compiled and run on a system supporting
129 the new API should continue to operate. Simply put, the API
130 changes for IPv6 should not break existing programs.
132 - The changes to the API should be as small as possible in order to
133 simplify the task of converting existing IPv4 applications to
136 - Where possible, applications should be able to use this API to
137 interoperate with both IPv6 and IPv4 hosts. Applications should
138 not need to know which type of host they are communicating with.
140 - IPv6 addresses carried in data structures should be 64-bit
141 aligned. This is necessary in order to obtain optimum
142 performance on 64-bit machine architectures.
144 Because of the importance of providing IPv4 compatibility in the API,
145 these extensions are explicitly designed to operate on machines that
146 provide complete support for both IPv4 and IPv6. A subset of this
147 API could probably be designed for operation on systems that support
148 only IPv6. However, this is not addressed in this memo.
150 2.1. What Needs to be Changed
152 The socket interface API consists of a few distinct components:
154 - Core socket functions.
156 - Address data structures.
158 - Name-to-address translation functions.
160 - Address conversion functions.
162 The core socket functions -- those functions that deal with such
163 things as setting up and tearing down TCP connections, and sending
164 and receiving UDP packets -- were designed to be transport
165 independent. Where protocol addresses are passed as function
166 arguments, they are carried via opaque pointers. A protocol-specific
170 Gilligan, et. al. Informational [Page 3]
172 RFC 2133 IPv6 Socket Interface Extensions April 1997
175 address data structure is defined for each protocol that the socket
176 functions support. Applications must cast pointers to these
177 protocol-specific address structures into pointers to the generic
178 "sockaddr" address structure when using the socket functions. These
179 functions need not change for IPv6, but a new IPv6-specific address
180 data structure is needed.
182 The "sockaddr_in" structure is the protocol-specific data structure
183 for IPv4. This data structure actually includes 8-octets of unused
184 space, and it is tempting to try to use this space to adapt the
185 sockaddr_in structure to IPv6. Unfortunately, the sockaddr_in
186 structure is not large enough to hold the 16-octet IPv6 address as
187 well as the other information (address family and port number) that
188 is needed. So a new address data structure must be defined for IPv6.
190 The name-to-address translation functions in the socket interface are
191 gethostbyname() and gethostbyaddr(). These must be modified to
192 support IPv6 and the semantics defined must provide 100% backward
193 compatibility for all existing IPv4 applications, along with IPv6
194 support for new applications. Additionally, the POSIX 1003.g work in
195 progress [4] specifies a new hostname-to-address translation function
196 which is protocol independent. This function can also be used with
199 The address conversion functions -- inet_ntoa() and inet_addr() --
200 convert IPv4 addresses between binary and printable form. These
201 functions are quite specific to 32-bit IPv4 addresses. We have
202 designed two analogous functions that convert both IPv4 and IPv6
203 addresses, and carry an address type parameter so that they can be
204 extended to other protocol families as well.
206 Finally, a few miscellaneous features are needed to support IPv6.
207 New interfaces are needed to support the IPv6 flow label, priority,
208 and hop limit header fields. New socket options are needed to
209 control the sending and receiving of IPv6 multicast packets.
211 The socket interface will be enhanced in the future to provide access
212 to other IPv6 features. These extensions are described in [5].
226 Gilligan, et. al. Informational [Page 4]
228 RFC 2133 IPv6 Socket Interface Extensions April 1997
233 The data types of the structure elements given in this memo are
234 intended to be examples, not absolute requirements. Whenever
235 possible, POSIX 1003.1g data types are used: u_intN_t means an
236 unsigned integer of exactly N bits (e.g., u_int16_t) and u_intNm_t
237 means an unsigned integer of at least N bits (e.g., u_int32m_t). We
238 also assume the argument data types from 1003.1g when possible (e.g.,
239 the final argument to setsockopt() is a size_t value). Whenever
240 buffer sizes are specified, the POSIX 1003.1 size_t data type is used
241 (e.g., the two length arguments to getnameinfo()).
245 When function prototypes and structures are shown we show the headers
246 that must be #included to cause that item to be defined.
250 When structures are described the members shown are the ones that
251 must appear in an implementation. Additional, nonstandard members
252 may also be defined by an implementation.
254 The ordering shown for the members of a structure is the recommended
255 ordering, given alignment considerations of multibyte members, but an
256 implementation may order the members differently.
260 This section specifies the socket interface changes for IPv6.
262 3.1. IPv6 Address Family and Protocol Family
264 A new address family name, AF_INET6, is defined in <sys/socket.h>.
265 The AF_INET6 definition distinguishes between the original
266 sockaddr_in address data structure, and the new sockaddr_in6 data
269 A new protocol family name, PF_INET6, is defined in <sys/socket.h>.
270 Like most of the other protocol family names, this will usually be
271 defined to have the same value as the corresponding address family
274 #define PF_INET6 AF_INET6
276 The PF_INET6 is used in the first argument to the socket() function
277 to indicate that an IPv6 socket is being created.
282 Gilligan, et. al. Informational [Page 5]
284 RFC 2133 IPv6 Socket Interface Extensions April 1997
287 3.2. IPv6 Address Structure
289 A new data structure to hold a single IPv6 address is defined as
292 #include <netinet/in.h>
295 u_int8_t s6_addr[16]; /* IPv6 address */
298 This data structure contains an array of sixteen 8-bit elements,
299 which make up one 128-bit IPv6 address. The IPv6 address is stored
300 in network byte order.
302 3.3. Socket Address Structure for 4.3BSD-Based Systems
304 In the socket interface, a different protocol-specific data structure
305 is defined to carry the addresses for each protocol suite. Each
306 protocol-specific data structure is designed so it can be cast into a
307 protocol-independent data structure -- the "sockaddr" structure.
308 Each has a "family" field that overlays the "sa_family" of the
309 sockaddr data structure. This field identifies the type of the data
312 The sockaddr_in structure is the protocol-specific address data
313 structure for IPv4. It is used to pass addresses between
314 applications and the system in the socket functions. The following
315 structure is defined to carry IPv6 addresses:
317 #include <netinet/in.h>
319 struct sockaddr_in6 {
320 u_int16m_t sin6_family; /* AF_INET6 */
321 u_int16m_t sin6_port; /* transport layer port # */
322 u_int32m_t sin6_flowinfo; /* IPv6 flow information */
323 struct in6_addr sin6_addr; /* IPv6 address */
326 This structure is designed to be compatible with the sockaddr data
327 structure used in the 4.3BSD release.
329 The sin6_family field identifies this as a sockaddr_in6 structure.
330 This field overlays the sa_family field when the buffer is cast to a
331 sockaddr data structure. The value of this field must be AF_INET6.
338 Gilligan, et. al. Informational [Page 6]
340 RFC 2133 IPv6 Socket Interface Extensions April 1997
343 The sin6_port field contains the 16-bit UDP or TCP port number. This
344 field is used in the same way as the sin_port field of the
345 sockaddr_in structure. The port number is stored in network byte
348 The sin6_flowinfo field is a 32-bit field that contains two pieces of
349 information: the 24-bit IPv6 flow label and the 4-bit priority field.
350 The contents and interpretation of this member is unspecified at this
353 The sin6_addr field is a single in6_addr structure (defined in the
354 previous section). This field holds one 128-bit IPv6 address. The
355 address is stored in network byte order.
357 The ordering of elements in this structure is specifically designed
358 so that the sin6_addr field will be aligned on a 64-bit boundary.
359 This is done for optimum performance on 64-bit architectures.
361 Notice that the sockaddr_in6 structure will normally be larger than
362 the generic sockaddr structure. On many existing implementations the
363 sizeof(struct sockaddr_in) equals sizeof(struct sockaddr), with both
364 being 16 bytes. Any existing code that makes this assumption needs
365 to be examined carefully when converting to IPv6.
367 3.4. Socket Address Structure for 4.4BSD-Based Systems
369 The 4.4BSD release includes a small, but incompatible change to the
370 socket interface. The "sa_family" field of the sockaddr data
371 structure was changed from a 16-bit value to an 8-bit value, and the
372 space saved used to hold a length field, named "sa_len". The
373 sockaddr_in6 data structure given in the previous section cannot be
374 correctly cast into the newer sockaddr data structure. For this
375 reason, the following alternative IPv6 address data structure is
376 provided to be used on systems based on 4.4BSD:
378 #include <netinet/in.h>
382 struct sockaddr_in6 {
383 u_char sin6_len; /* length of this struct */
384 u_char sin6_family; /* AF_INET6 */
385 u_int16m_t sin6_port; /* transport layer port # */
386 u_int32m_t sin6_flowinfo; /* IPv6 flow information */
387 struct in6_addr sin6_addr; /* IPv6 address */
394 Gilligan, et. al. Informational [Page 7]
396 RFC 2133 IPv6 Socket Interface Extensions April 1997
399 The only differences between this data structure and the 4.3BSD
400 variant are the inclusion of the length field, and the change of the
401 family field to a 8-bit data type. The definitions of all the other
402 fields are identical to the structure defined in the previous
405 Systems that provide this version of the sockaddr_in6 data structure
406 must also declare SIN6_LEN as a result of including the
407 <netinet/in.h> header. This macro allows applications to determine
408 whether they are being built on a system that supports the 4.3BSD or
409 4.4BSD variants of the data structure.
411 3.5. The Socket Functions
413 Applications call the socket() function to create a socket descriptor
414 that represents a communication endpoint. The arguments to the
415 socket() function tell the system which protocol to use, and what
416 format address structure will be used in subsequent functions. For
417 example, to create an IPv4/TCP socket, applications make the call:
419 s = socket(PF_INET, SOCK_STREAM, 0);
421 To create an IPv4/UDP socket, applications make the call:
423 s = socket(PF_INET, SOCK_DGRAM, 0);
425 Applications may create IPv6/TCP and IPv6/UDP sockets by simply using
426 the constant PF_INET6 instead of PF_INET in the first argument. For
427 example, to create an IPv6/TCP socket, applications make the call:
429 s = socket(PF_INET6, SOCK_STREAM, 0);
431 To create an IPv6/UDP socket, applications make the call:
433 s = socket(PF_INET6, SOCK_DGRAM, 0);
435 Once the application has created a PF_INET6 socket, it must use the
436 sockaddr_in6 address structure when passing addresses in to the
437 system. The functions that the application uses to pass addresses
450 Gilligan, et. al. Informational [Page 8]
452 RFC 2133 IPv6 Socket Interface Extensions April 1997
455 The system will use the sockaddr_in6 address structure to return
456 addresses to applications that are using PF_INET6 sockets. The
457 functions that return an address from the system to an application
466 No changes to the syntax of the socket functions are needed to
467 support IPv6, since all of the "address carrying" functions use an
468 opaque address pointer, and carry an address length as a function
471 3.6. Compatibility with IPv4 Applications
473 In order to support the large base of applications using the original
474 API, system implementations must provide complete source and binary
475 compatibility with the original API. This means that systems must
476 continue to support PF_INET sockets and the sockaddr_in address
477 structure. Applications must be able to create IPv4/TCP and IPv4/UDP
478 sockets using the PF_INET constant in the socket() function, as
479 described in the previous section. Applications should be able to
480 hold a combination of IPv4/TCP, IPv4/UDP, IPv6/TCP and IPv6/UDP
481 sockets simultaneously within the same process.
483 Applications using the original API should continue to operate as
484 they did on systems supporting only IPv4. That is, they should
485 continue to interoperate with IPv4 nodes.
487 3.7. Compatibility with IPv4 Nodes
489 The API also provides a different type of compatibility: the ability
490 for IPv6 applications to interoperate with IPv4 applications. This
491 feature uses the IPv4-mapped IPv6 address format defined in the IPv6
492 addressing architecture specification [2]. This address format
493 allows the IPv4 address of an IPv4 node to be represented as an IPv6
494 address. The IPv4 address is encoded into the low-order 32 bits of
495 the IPv6 address, and the high-order 96 bits hold the fixed prefix
496 0:0:0:0:0:FFFF. IPv4-mapped addresses are written as follows:
498 ::FFFF:<IPv4-address>
500 These addresses are often generated automatically by the
501 gethostbyname() function when the specified host has only IPv4
502 addresses (as described in Section 6.1).
506 Gilligan, et. al. Informational [Page 9]
508 RFC 2133 IPv6 Socket Interface Extensions April 1997
511 Applications may use PF_INET6 sockets to open TCP connections to IPv4
512 nodes, or send UDP packets to IPv4 nodes, by simply encoding the
513 destination's IPv4 address as an IPv4-mapped IPv6 address, and
514 passing that address, within a sockaddr_in6 structure, in the
515 connect() or sendto() call. When applications use PF_INET6 sockets
516 to accept TCP connections from IPv4 nodes, or receive UDP packets
517 from IPv4 nodes, the system returns the peer's address to the
518 application in the accept(), recvfrom(), or getpeername() call using
519 a sockaddr_in6 structure encoded this way.
521 Few applications will likely need to know which type of node they are
522 interoperating with. However, for those applications that do need to
523 know, the IN6_IS_ADDR_V4MAPPED() macro, defined in Section 6.6, is
526 3.8. IPv6 Wildcard Address
528 While the bind() function allows applications to select the source IP
529 address of UDP packets and TCP connections, applications often want
530 the system to select the source address for them. With IPv4, one
531 specifies the address as the symbolic constant INADDR_ANY (called the
532 "wildcard" address) in the bind() call, or simply omits the bind()
535 Since the IPv6 address type is a structure (struct in6_addr), a
536 symbolic constant can be used to initialize an IPv6 address variable,
537 but cannot be used in an assignment. Therefore systems provide the
538 IPv6 wildcard address in two forms.
540 The first version is a global variable named "in6addr_any" that is an
541 in6_addr structure. The extern declaration for this variable is
542 defined in <netinet/in.h>:
544 extern const struct in6_addr in6addr_any;
562 Gilligan, et. al. Informational [Page 10]
564 RFC 2133 IPv6 Socket Interface Extensions April 1997
567 Applications use in6addr_any similarly to the way they use INADDR_ANY
568 in IPv4. For example, to bind a socket to port number 23, but let
569 the system select the source address, an application could use the
572 struct sockaddr_in6 sin6;
574 sin6.sin6_family = AF_INET6;
575 sin6.sin6_flowinfo = 0;
576 sin6.sin6_port = htons(23);
577 sin6.sin6_addr = in6addr_any; /* structure assignment */
579 if (bind(s, (struct sockaddr *) &sin6, sizeof(sin6)) == -1)
582 The other version is a symbolic constant named IN6ADDR_ANY_INIT and
583 is defined in <netinet/in.h>. This constant can be used to
584 initialize an in6_addr structure:
586 struct in6_addr anyaddr = IN6ADDR_ANY_INIT;
588 Note that this constant can be used ONLY at declaration time. It can
589 not be used to assign a previously declared in6_addr structure. For
590 example, the following code will not work:
592 /* This is the WRONG way to assign an unspecified address */
593 struct sockaddr_in6 sin6;
595 sin6.sin6_addr = IN6ADDR_ANY_INIT; /* will NOT compile */
597 Be aware that the IPv4 INADDR_xxx constants are all defined in host
598 byte order but the IPv6 IN6ADDR_xxx constants and the IPv6
599 in6addr_xxx externals are defined in network byte order.
601 3.9. IPv6 Loopback Address
603 Applications may need to send UDP packets to, or originate TCP
604 connections to, services residing on the local node. In IPv4, they
605 can do this by using the constant IPv4 address INADDR_LOOPBACK in
606 their connect(), sendto(), or sendmsg() call.
608 IPv6 also provides a loopback address to contact local TCP and UDP
609 services. Like the unspecified address, the IPv6 loopback address is
610 provided in two forms -- a global variable and a symbolic constant.
618 Gilligan, et. al. Informational [Page 11]
620 RFC 2133 IPv6 Socket Interface Extensions April 1997
623 The global variable is an in6_addr structure named
624 "in6addr_loopback." The extern declaration for this variable is
625 defined in <netinet/in.h>:
627 extern const struct in6_addr in6addr_loopback;
629 Applications use in6addr_loopback as they would use INADDR_LOOPBACK
630 in IPv4 applications (but beware of the byte ordering difference
631 mentioned at the end of the previous section). For example, to open
632 a TCP connection to the local telnet server, an application could use
635 struct sockaddr_in6 sin6;
637 sin6.sin6_family = AF_INET6;
638 sin6.sin6_flowinfo = 0;
639 sin6.sin6_port = htons(23);
640 sin6.sin6_addr = in6addr_loopback; /* structure assignment */
642 if (connect(s, (struct sockaddr *) &sin6, sizeof(sin6)) == -1)
645 The symbolic constant is named IN6ADDR_LOOPBACK_INIT and is defined
646 in <netinet/in.h>. It can be used at declaration time ONLY; for
649 struct in6_addr loopbackaddr = IN6ADDR_LOOPBACK_INIT;
651 Like IN6ADDR_ANY_INIT, this constant cannot be used in an assignment
652 to a previously declared IPv6 address variable.
654 4. Interface Identification
656 This API uses an interface index (a small positive integer) to
657 identify the local interface on which a multicast group is joined
658 (Section 5.3). Additionally, the advanced API [5] uses these same
659 interface indexes to identify the interface on which a datagram is
660 received, or to specify the interface on which a datagram is to be
663 Interfaces are normally known by names such as "le0", "sl1", "ppp2",
664 and the like. On Berkeley-derived implementations, when an interface
665 is made known to the system, the kernel assigns a unique positive
666 integer value (called the interface index) to that interface. These
667 are small positive integers that start at 1. (Note that 0 is never
668 used for an interface index.) There may be gaps so that there is no
669 current interface for a particular positive interface index.
674 Gilligan, et. al. Informational [Page 12]
676 RFC 2133 IPv6 Socket Interface Extensions April 1997
679 This API defines two functions that map between an interface name and
680 index, a third function that returns all the interface names and
681 indexes, and a fourth function to return the dynamic memory allocated
682 by the previous function. How these functions are implemented is
683 left up to the implementation. 4.4BSD implementations can implement
684 these functions using the existing sysctl() function with the
685 NET_RT_LIST command. Other implementations may wish to use ioctl()
690 The first function maps an interface name into its corresponding
695 unsigned int if_nametoindex(const char *ifname);
697 If the specified interface does not exist, the return value is 0.
701 The second function maps an interface index into its corresponding
706 char *if_indextoname(unsigned int ifindex, char *ifname);
708 The ifname argument must point to a buffer of at least IFNAMSIZ bytes
709 into which the interface name corresponding to the specified index is
710 returned. (IFNAMSIZ is also defined in <net/if.h> and its value
711 includes a terminating null byte at the end of the interface name.)
712 This pointer is also the return value of the function. If there is
713 no interface corresponding to the specified index, NULL is returned.
730 Gilligan, et. al. Informational [Page 13]
732 RFC 2133 IPv6 Socket Interface Extensions April 1997
735 4.3. Return All Interface Names and Indexes
737 The final function returns an array of if_nameindex structures, one
738 structure per interface.
742 struct if_nameindex {
743 unsigned int if_index; /* 1, 2, ... */
744 char *if_name; /* null terminated name: "le0", ... */
747 struct if_nameindex *if_nameindex(void);
749 The end of the array of structures is indicated by a structure with
750 an if_index of 0 and an if_name of NULL. The function returns a NULL
751 pointer upon an error.
753 The memory used for this array of structures along with the interface
754 names pointed to by the if_name members is obtained dynamically.
755 This memory is freed by the next function.
759 The following function frees the dynamic memory that was allocated by
764 void if_freenameindex(struct if_nameindex *ptr);
766 The argument to this function must be a pointer that was returned by
771 A number of new socket options are defined for IPv6. All of these
772 new options are at the IPPROTO_IPV6 level. That is, the "level"
773 parameter in the getsockopt() and setsockopt() calls is IPPROTO_IPV6
774 when using these options. The constant name prefix IPV6_ is used in
775 all of the new socket options. This serves to clearly identify these
776 options as applying to IPv6.
778 The declaration for IPPROTO_IPV6, the new IPv6 socket options, and
779 related constants defined in this section are obtained by including
780 the header <netinet/in.h>.
786 Gilligan, et. al. Informational [Page 14]
788 RFC 2133 IPv6 Socket Interface Extensions April 1997
791 5.1. Changing Socket Type
793 Unix allows open sockets to be passed between processes via the
794 exec() call and other means. It is a relatively common application
795 practice to pass open sockets across exec() calls. Thus it is
796 possible for an application using the original API to pass an open
797 PF_INET socket to an application that is expecting to receive a
798 PF_INET6 socket. Similarly, it is possible for an application using
799 the extended API to pass an open PF_INET6 socket to an application
800 using the original API, which would be equipped only to deal with
801 PF_INET sockets. Either of these cases could cause problems, because
802 the application that is passed the open socket might not know how to
803 decode the address structures returned in subsequent socket
806 To remedy this problem, a new setsockopt() option is defined that
807 allows an application to "convert" a PF_INET6 socket into a PF_INET
808 socket and vice versa.
810 An IPv6 application that is passed an open socket from an unknown
811 process may use the IPV6_ADDRFORM setsockopt() option to "convert"
812 the socket to PF_INET6. Once that has been done, the system will
813 return sockaddr_in6 address structures in subsequent socket
816 An IPv6 application that is about to pass an open PF_INET6 socket to
817 a program that is not be IPv6 capable can "downgrade" the socket to
818 PF_INET before calling exec(). After that, the system will return
819 sockaddr_in address structures to the application that was exec()'ed.
820 Be aware that you cannot downgrade an IPv6 socket to an IPv4 socket
821 unless all nonwildcard addresses already associated with the IPv6
822 socket are IPv4-mapped IPv6 addresses.
824 The IPV6_ADDRFORM option is valid at both the IPPROTO_IP and
825 IPPROTO_IPV6 levels. The only valid option values are PF_INET6 and
826 PF_INET. For example, to convert a PF_INET6 socket to PF_INET, a
829 int addrform = PF_INET;
831 if (setsockopt(s, IPPROTO_IPV6, IPV6_ADDRFORM,
832 (char *) &addrform, sizeof(addrform)) == -1)
833 perror("setsockopt IPV6_ADDRFORM");
842 Gilligan, et. al. Informational [Page 15]
844 RFC 2133 IPv6 Socket Interface Extensions April 1997
847 An application may use IPV6_ADDRFORM with getsockopt() to learn
848 whether an open socket is a PF_INET of PF_INET6 socket. For example:
851 size_t len = sizeof(addrform);
853 if (getsockopt(s, IPPROTO_IPV6, IPV6_ADDRFORM,
854 (char *) &addrform, &len) == -1)
855 perror("getsockopt IPV6_ADDRFORM");
856 else if (addrform == PF_INET)
857 printf("This is an IPv4 socket.\n");
858 else if (addrform == PF_INET6)
859 printf("This is an IPv6 socket.\n");
861 printf("This system is broken.\n");
863 5.2. Unicast Hop Limit
865 A new setsockopt() option controls the hop limit used in outgoing
866 unicast IPv6 packets. The name of this option is IPV6_UNICAST_HOPS,
867 and it is used at the IPPROTO_IPV6 layer. The following example
868 illustrates how it is used:
872 if (setsockopt(s, IPPROTO_IPV6, IPV6_UNICAST_HOPS,
873 (char *) &hoplimit, sizeof(hoplimit)) == -1)
874 perror("setsockopt IPV6_UNICAST_HOPS");
876 When the IPV6_UNICAST_HOPS option is set with setsockopt(), the
877 option value given is used as the hop limit for all subsequent
878 unicast packets sent via that socket. If the option is not set, the
879 system selects a default value. The integer hop limit value (called
880 x) is interpreted as follows:
882 x < -1: return an error of EINVAL
883 x == -1: use kernel default
885 x >= 256: return an error of EINVAL
898 Gilligan, et. al. Informational [Page 16]
900 RFC 2133 IPv6 Socket Interface Extensions April 1997
903 The IPV6_UNICAST_HOPS option may be used with getsockopt() to
904 determine the hop limit value that the system will use for subsequent
905 unicast packets sent via that socket. For example:
908 size_t len = sizeof(hoplimit);
910 if (getsockopt(s, IPPROTO_IPV6, IPV6_UNICAST_HOPS,
911 (char *) &hoplimit, &len) == -1)
912 perror("getsockopt IPV6_UNICAST_HOPS");
914 printf("Using %d for hop limit.\n", hoplimit);
916 5.3. Sending and Receiving Multicast Packets
918 IPv6 applications may send UDP multicast packets by simply specifying
919 an IPv6 multicast address in the address argument of the sendto()
922 Three socket options at the IPPROTO_IPV6 layer control some of the
923 parameters for sending multicast packets. Setting these options is
924 not required: applications may send multicast packets without using
925 these options. The setsockopt() options for controlling the sending
926 of multicast packets are summarized below:
930 Set the interface to use for outgoing multicast packets. The
931 argument is the index of the interface to use.
933 Argument type: unsigned int
937 Set the hop limit to use for outgoing multicast packets.
938 (Note a separate option - IPV6_UNICAST_HOPS - is provided to
939 set the hop limit to use for outgoing unicast packets.) The
940 interpretation of the argument is the same as for the
941 IPV6_UNICAST_HOPS option:
943 x < -1: return an error of EINVAL
944 x == -1: use kernel default
946 x >= 256: return an error of EINVAL
954 Gilligan, et. al. Informational [Page 17]
956 RFC 2133 IPv6 Socket Interface Extensions April 1997
961 Controls whether outgoing multicast packets sent should be
962 delivered back to the local application. A toggle. If the
963 option is set to 1, multicast packets are looped back. If it
964 is set to 0, they are not.
966 Argument type: unsigned int
968 The reception of multicast packets is controlled by the two
969 setsockopt() options summarized below:
973 Join a multicast group on a specified local interface. If
974 the interface index is specified as 0, the kernel chooses the
975 local interface. For example, some kernels look up the
976 multicast group in the normal IPv6 routing table and using
977 the resulting interface.
979 Argument type: struct ipv6_mreq
983 Leave a multicast group on a specified interface.
985 Argument type: struct ipv6_mreq
987 The argument type of both of these options is the ipv6_mreq
988 structure, defined as:
990 #include <netinet/in.h>
993 struct in6_addr ipv6mr_multiaddr; /* IPv6 multicast addr */
994 unsigned int ipv6mr_interface; /* interface index */
997 Note that to receive multicast datagrams a process must join the
998 multicast group and bind the UDP port to which datagrams will be
999 sent. Some processes also bind the multicast group address to the
1000 socket, in addition to the port, to prevent other datagrams destined
1001 to that same port from being delivered to the socket.
1010 Gilligan, et. al. Informational [Page 18]
1012 RFC 2133 IPv6 Socket Interface Extensions April 1997
1015 6. Library Functions
1017 New library functions are needed to perform a variety of operations
1018 with IPv6 addresses. Functions are needed to lookup IPv6 addresses
1019 in the Domain Name System (DNS). Both forward lookup (hostname-to-
1020 address translation) and reverse lookup (address-to-hostname
1021 translation) need to be supported. Functions are also needed to
1022 convert IPv6 addresses between their binary and textual form.
1024 6.1. Hostname-to-Address Translation
1026 The commonly used function gethostbyname() remains unchanged as does
1027 the hostent structure to which it returns a pointer. Existing
1028 applications that call this function continue to receive only IPv4
1029 addresses that are the result of a query in the DNS for A records.
1030 (We assume the DNS is being used; some environments may be using a
1031 hosts file or some other name resolution system, either of which may
1032 impede renumbering. We also assume that the RES_USE_INET6 resolver
1033 option is not set, which we describe in more detail shortly.)
1035 Two new changes are made to support IPv6 addresses. First, the
1036 following function is new:
1038 #include <sys/socket.h>
1041 struct hostent *gethostbyname2(const char *name, int af);
1043 The af argument specifies the address family. The default operation
1044 of this function is simple:
1046 - If the af argument is AF_INET, then a query is made for A
1047 records. If successful, IPv4 addresses are returned and the
1048 h_length member of the hostent structure will be 4, else the
1049 function returns a NULL pointer.
1051 - If the af argument is AF_INET6, then a query is made for AAAA
1052 records. If successful, IPv6 addresses are returned and the
1053 h_length member of the hostent structure will be 16, else the
1054 function returns a NULL pointer.
1066 Gilligan, et. al. Informational [Page 19]
1068 RFC 2133 IPv6 Socket Interface Extensions April 1997
1071 The second change, that provides additional functionality, is a new
1072 resolver option RES_USE_INET6, which is defined as a result of
1073 including the <resolv.h> header. (This option is provided starting
1074 with the BIND 4.9.4 release.) There are three ways to set this
1080 _res.options |= RES_USE_INET6;
1082 and then call either gethostbyname() or gethostbyname2(). This
1083 option then affects only the process that is calling the
1086 - The second way to set this option is to set the environment
1087 variable RES_OPTIONS, as in RES_OPTIONS=inet6. (This example is
1088 for the Bourne and Korn shells.) This method affects any
1089 processes that see this environment variable.
1091 - The third way is to set this option in the resolver configuration
1092 file (normally /etc/resolv.conf) and the option then affects all
1093 applications on the host. This final method should not be done
1094 until all applications on the host are capable of dealing with
1097 There is no priority among these three methods. When the
1098 RES_USE_INET6 option is set, two changes occur:
1100 - gethostbyname(host) first calls gethostbyname2(host, AF_INET6)
1101 looking for AAAA records, and if this fails it then calls
1102 gethostbyname2(host, AF_INET) looking for A records.
1104 - gethostbyname2(host, AF_INET) always returns IPv4-mapped IPv6
1105 addresses with the h_length member of the hostent structure set
1108 An application must not enable the RES_USE_INET6 option until it is
1109 prepared to deal with 16-byte addresses in the returned hostent
1122 Gilligan, et. al. Informational [Page 20]
1124 RFC 2133 IPv6 Socket Interface Extensions April 1997
1127 The following table summarizes the operation of the existing
1128 gethostbyname() function, the new function gethostbyname2(), along
1129 with the new resolver option RES_USE_INET6.
1131 +------------------+---------------------------------------------------+
1132 | | RES_USE_INET6 option |
1133 | +-------------------------+-------------------------+
1135 +------------------+-------------------------+-------------------------+
1136 | |Search for A records. |Search for AAAA records. |
1137 | gethostbyname | If found, return IPv4 | If found, return IPv6 |
1138 | (host) | addresses (h_length=4). | addresses (h_length=16).|
1139 | | Else error. | Else search for A |
1140 | | | records. If found, |
1141 | |Provides backward | return IPv4-mapped IPv6 |
1142 | | compatibility with all | addresses (h_length=16).|
1143 | | existing IPv4 appls. | Else error. |
1144 +------------------+-------------------------+-------------------------+
1145 | |Search for A records. |Search for A records. |
1146 | gethostbyname2 | If found, return IPv4 | If found, return |
1147 | (host, AF_INET) | addresses (h_length=4). | IPv4-mapped IPv6 |
1148 | | Else error. | addresses (h_length=16).|
1150 +------------------+-------------------------+-------------------------+
1151 | |Search for AAAA records. |Search for AAAA records. |
1152 | gethostbyname2 | If found, return IPv6 | If found, return IPv6 |
1153 | (host, AF_INET6) | addresses (h_length=16).| addresses (h_length=16).|
1154 | | Else error. | Else error. |
1155 +------------------+-------------------------+-------------------------+
1157 It is expected that when a typical naive application that calls
1158 gethostbyname() today is modified to use IPv6, it simply changes the
1159 program to use IPv6 sockets and then enables the RES_USE_INET6
1160 resolver option before calling gethostbyname(). This application
1161 will then work with either IPv4 or IPv6 peers.
1163 Note that gethostbyname() and gethostbyname2() are not thread-safe,
1164 since both return a pointer to a static hostent structure. But
1165 several vendors have defined a thread-safe gethostbyname_r() function
1166 that requires four additional arguments. We expect these vendors to
1167 also define a gethostbyname2_r() function.
1178 Gilligan, et. al. Informational [Page 21]
1180 RFC 2133 IPv6 Socket Interface Extensions April 1997
1183 6.2. Address To Hostname Translation
1185 The existing gethostbyaddr() function already requires an address
1186 family argument and can therefore work with IPv6 addresses:
1188 #include <sys/socket.h>
1191 struct hostent *gethostbyaddr(const char *src, int len, int af);
1193 One possible source of confusion is the handling of IPv4-mapped IPv6
1194 addresses and IPv4-compatible IPv6 addresses. This is addressed in
1195 [6] and involves the following logic:
1197 1. If af is AF_INET6, and if len equals 16, and if the IPv6 address
1198 is an IPv4-mapped IPv6 address or an IPv4-compatible IPv6
1199 address, then skip over the first 12 bytes of the IPv6 address,
1200 set af to AF_INET, and set len to 4.
1202 2. If af is AF_INET, then query for a PTR record in the in-
1205 3. If af is AF_INET6, then query for a PTR record in the ip6.int
1208 4. If the function is returning success, and if af equals AF_INET,
1209 and if the RES_USE_INET6 option was set, then the single address
1210 that is returned in the hostent structure (a copy of the first
1211 argument to the function) is returned as an IPv4-mapped IPv6
1212 address and the h_length member is set to 16.
1214 All four steps listed are performed, in order. The same caveats
1215 regarding a thread-safe version of gethostbyname() that were made at
1216 the end of the previous section apply here as well.
1218 6.3. Protocol-Independent Hostname and Service Name Translation
1220 Hostname-to-address translation is done in a protocol-independent
1221 fashion using the getaddrinfo() function that is taken from the
1222 Institute of Electrical and Electronic Engineers (IEEE) POSIX 1003.1g
1223 (Protocol Independent Interfaces) work in progress specification [4].
1225 The official specification for this function will be the final POSIX
1226 standard. We are providing this independent description of the
1227 function because POSIX standards are not freely available (as are
1228 IETF documents). Should there be any discrepancies between this
1229 description and the POSIX description, the POSIX description takes
1234 Gilligan, et. al. Informational [Page 22]
1236 RFC 2133 IPv6 Socket Interface Extensions April 1997
1239 #include <sys/socket.h>
1242 int getaddrinfo(const char *hostname, const char *servname,
1243 const struct addrinfo *hints,
1244 struct addrinfo **res);
1246 The addrinfo structure is defined as:
1248 #include <sys/socket.h>
1252 int ai_flags; /* AI_PASSIVE, AI_CANONNAME */
1253 int ai_family; /* PF_xxx */
1254 int ai_socktype; /* SOCK_xxx */
1255 int ai_protocol; /* 0 or IPPROTO_xxx for IPv4 and IPv6 */
1256 size_t ai_addrlen; /* length of ai_addr */
1257 char *ai_canonname; /* canonical name for hostname */
1258 struct sockaddr *ai_addr; /* binary address */
1259 struct addrinfo *ai_next; /* next structure in linked list */
1262 The return value from the function is 0 upon success or a nonzero
1263 error code. The following names are the nonzero error codes from
1264 getaddrinfo(), and are defined in <netdb.h>:
1266 EAI_ADDRFAMILY address family for hostname not supported
1267 EAI_AGAIN temporary failure in name resolution
1268 EAI_BADFLAGS invalid value for ai_flags
1269 EAI_FAIL non-recoverable failure in name resolution
1270 EAI_FAMILY ai_family not supported
1271 EAI_MEMORY memory allocation failure
1272 EAI_NODATA no address associated with hostname
1273 EAI_NONAME hostname nor servname provided, or not known
1274 EAI_SERVICE servname not supported for ai_socktype
1275 EAI_SOCKTYPE ai_socktype not supported
1276 EAI_SYSTEM system error returned in errno
1278 The hostname and servname arguments are pointers to null-terminated
1279 strings or NULL. One or both of these two arguments must be a non-
1280 NULL pointer. In the normal client scenario, both the hostname and
1281 servname are specified. In the normal server scenario, only the
1282 servname is specified. A non-NULL hostname string can be either a
1283 host name or a numeric host address string (i.e., a dotted-decimal
1284 IPv4 address or an IPv6 hex address). A non-NULL servname string can
1285 be either a service name or a decimal port number.
1290 Gilligan, et. al. Informational [Page 23]
1292 RFC 2133 IPv6 Socket Interface Extensions April 1997
1295 The caller can optionally pass an addrinfo structure, pointed to by
1296 the third argument, to provide hints concerning the type of socket
1297 that the caller supports. In this hints structure all members other
1298 than ai_flags, ai_family, ai_socktype, and ai_protocol must be zero
1299 or a NULL pointer. A value of PF_UNSPEC for ai_family means the
1300 caller will accept any protocol family. A value of 0 for ai_socktype
1301 means the caller will accept any socket type. A value of 0 for
1302 ai_protocol means the caller will accept any protocol. For example,
1303 if the caller handles only TCP and not UDP, then the ai_socktype
1304 member of the hints structure should be set to SOCK_STREAM when
1305 getaddrinfo() is called. If the caller handles only IPv4 and not
1306 IPv6, then the ai_family member of the hints structure should be set
1307 to PF_INET when getaddrinfo() is called. If the third argument to
1308 getaddrinfo() is a NULL pointer, this is the same as if the caller
1309 had filled in an addrinfo structure initialized to zero with
1310 ai_family set to PF_UNSPEC.
1312 Upon successful return a pointer to a linked list of one or more
1313 addrinfo structures is returned through the final argument. The
1314 caller can process each addrinfo structure in this list by following
1315 the ai_next pointer, until a NULL pointer is encountered. In each
1316 returned addrinfo structure the three members ai_family, ai_socktype,
1317 and ai_protocol are the corresponding arguments for a call to the
1318 socket() function. In each addrinfo structure the ai_addr member
1319 points to a filled-in socket address structure whose length is
1320 specified by the ai_addrlen member.
1322 If the AI_PASSIVE bit is set in the ai_flags member of the hints
1323 structure, then the caller plans to use the returned socket address
1324 structure in a call to bind(). In this case, if the hostname
1325 argument is a NULL pointer, then the IP address portion of the socket
1326 address structure will be set to INADDR_ANY for an IPv4 address or
1327 IN6ADDR_ANY_INIT for an IPv6 address.
1329 If the AI_PASSIVE bit is not set in the ai_flags member of the hints
1330 structure, then the returned socket address structure will be ready
1331 for a call to connect() (for a connection-oriented protocol) or
1332 either connect(), sendto(), or sendmsg() (for a connectionless
1333 protocol). In this case, if the hostname argument is a NULL pointer,
1334 then the IP address portion of the socket address structure will be
1335 set to the loopback address.
1337 If the AI_CANONNAME bit is set in the ai_flags member of the hints
1338 structure, then upon successful return the ai_canonname member of the
1339 first addrinfo structure in the linked list will point to a null-
1340 terminated string containing the canonical name of the specified
1346 Gilligan, et. al. Informational [Page 24]
1348 RFC 2133 IPv6 Socket Interface Extensions April 1997
1351 All of the information returned by getaddrinfo() is dynamically
1352 allocated: the addrinfo structures, and the socket address structures
1353 and canonical host name strings pointed to by the addrinfo
1354 structures. To return this information to the system the function
1355 freeaddrinfo() is called:
1357 #include <sys/socket.h>
1360 void freeaddrinfo(struct addrinfo *ai);
1362 The addrinfo structure pointed to by the ai argument is freed, along
1363 with any dynamic storage pointed to by the structure. This operation
1364 is repeated until a NULL ai_next pointer is encountered.
1366 To aid applications in printing error messages based on the EAI_xxx
1367 codes returned by getaddrinfo(), the following function is defined.
1369 #include <sys/socket.h>
1372 char *gai_strerror(int ecode);
1374 The argument is one of the EAI_xxx values defined earlier and the
1375 eturn value points to a string describing the error. If the argument
1376 is not one of the EAI_xxx values, the function still returns a
1377 pointer to a string whose contents indicate an unknown error.
1379 6.4. Socket Address Structure to Hostname and Service Name
1381 The POSIX 1003.1g specification includes no function to perform the
1382 reverse conversion from getaddrinfo(): to look up a hostname and
1383 service name, given the binary address and port. Therefore, we
1384 define the following function:
1386 #include <sys/socket.h>
1389 int getnameinfo(const struct sockaddr *sa, size_t salen,
1390 char *host, size_t hostlen,
1391 char *serv, size_t servlen,
1394 This function looks up an IP address and port number provided by the
1395 caller in the DNS and system-specific database, and returns text
1396 strings for both in buffers provided by the caller. The function
1397 indicates successful completion by a zero return value; a non-zero
1398 return value indicates failure.
1402 Gilligan, et. al. Informational [Page 25]
1404 RFC 2133 IPv6 Socket Interface Extensions April 1997
1407 The first argument, sa, points to either a sockaddr_in structure (for
1408 IPv4) or a sockaddr_in6 structure (for IPv6) that holds the IP
1409 address and port number. The salen argument gives the length of the
1410 sockaddr_in or sockaddr_in6 structure.
1412 The function returns the hostname associated with the IP address in
1413 the buffer pointed to by the host argument. The caller provides the
1414 size of this buffer via the hostlen argument. The service name
1415 associated with the port number is returned in the buffer pointed to
1416 by serv, and the servlen argument gives the length of this buffer.
1417 The caller specifies not to return either string by providing a zero
1418 value for the hostlen or servlen arguments. Otherwise, the caller
1419 must provide buffers large enough to hold the hostname and the
1420 service name, including the terminating null characters.
1422 Unfortunately most systems do not provide constants that specify the
1423 maximum size of either a fully-qualified domain name or a service
1424 name. Therefore to aid the application in allocating buffers for
1425 these two returned strings the following constants are defined in
1428 #define NI_MAXHOST 1025
1429 #define NI_MAXSERV 32
1431 The first value is actually defined as the constant MAXDNAME in
1432 recent versions of BIND's <arpa/nameser.h> header (older versions of
1433 BIND define this constant to be 256) and the second is a guess based
1434 on the services listed in the current Assigned Numbers RFC.
1436 The final argument is a flag that changes the default actions of this
1437 function. By default the fully-qualified domain name (FQDN) for the
1438 host is looked up in the DNS and returned. If the flag bit NI_NOFQDN
1439 is set, only the hostname portion of the FQDN is returned for local
1442 If the flag bit NI_NUMERICHOST is set, or if the host's name cannot
1443 be located in the DNS, the numeric form of the host's address is
1444 returned instead of its name (e.g., by calling inet_ntop() instead of
1445 gethostbyaddr()). If the flag bit NI_NAMEREQD is set, an error is
1446 returned if the host's name cannot be located in the DNS.
1448 If the flag bit NI_NUMERICSERV is set, the numeric form of the
1449 service address is returned (e.g., its port number) instead of its
1450 name. The two NI_NUMERICxxx flags are required to support the "-n"
1451 flag that many commands provide.
1458 Gilligan, et. al. Informational [Page 26]
1460 RFC 2133 IPv6 Socket Interface Extensions April 1997
1463 A fifth flag bit, NI_DGRAM, specifies that the service is a datagram
1464 service, and causes getservbyport() to be called with a second
1465 argument of "udp" instead of its default of "tcp". This is required
1466 for the few ports (512-514) that have different services for UDP and
1469 These NI_xxx flags are defined in <netdb.h> along with the AI_xxx
1470 flags already defined for getaddrinfo().
1472 6.5. Address Conversion Functions
1474 The two functions inet_addr() and inet_ntoa() convert an IPv4 address
1475 between binary and text form. IPv6 applications need similar
1476 functions. The following two functions convert both IPv6 and IPv4
1479 #include <sys/socket.h>
1480 #include <arpa/inet.h>
1482 int inet_pton(int af, const char *src, void *dst);
1484 const char *inet_ntop(int af, const void *src,
1485 char *dst, size_t size);
1487 The inet_pton() function converts an address in its standard text
1488 presentation form into its numeric binary form. The af argument
1489 specifies the family of the address. Currently the AF_INET and
1490 AF_INET6 address families are supported. The src argument points to
1491 the string being passed in. The dst argument points to a buffer into
1492 which the function stores the numeric address. The address is
1493 returned in network byte order. Inet_pton() returns 1 if the
1494 conversion succeeds, 0 if the input is not a valid IPv4 dotted-
1495 decimal string or a valid IPv6 address string, or -1 with errno set
1496 to EAFNOSUPPORT if the af argument is unknown. The calling
1497 application must ensure that the buffer referred to by dst is large
1498 enough to hold the numeric address (e.g., 4 bytes for AF_INET or 16
1499 bytes for AF_INET6).
1501 If the af argument is AF_INET, the function accepts a string in the
1502 standard IPv4 dotted-decimal form:
1506 where ddd is a one to three digit decimal number between 0 and 255.
1507 Note that many implementations of the existing inet_addr() and
1508 inet_aton() functions accept nonstandard input: octal numbers,
1509 hexadecimal numbers, and fewer than four numbers. inet_pton() does
1510 not accept these formats.
1514 Gilligan, et. al. Informational [Page 27]
1516 RFC 2133 IPv6 Socket Interface Extensions April 1997
1519 If the af argument is AF_INET6, then the function accepts a string in
1520 one of the standard IPv6 text forms defined in Section 2.2 of the
1521 addressing architecture specification [2].
1523 The inet_ntop() function converts a numeric address into a text
1524 string suitable for presentation. The af argument specifies the
1525 family of the address. This can be AF_INET or AF_INET6. The src
1526 argument points to a buffer holding an IPv4 address if the af
1527 argument is AF_INET, or an IPv6 address if the af argument is
1528 AF_INET6. The dst argument points to a buffer where the function
1529 will store the resulting text string. The size argument specifies
1530 the size of this buffer. The application must specify a non-NULL dst
1531 argument. For IPv6 addresses, the buffer must be at least 46-octets.
1532 For IPv4 addresses, the buffer must be at least 16-octets. In order
1533 to allow applications to easily declare buffers of the proper size to
1534 store IPv4 and IPv6 addresses in string form, the following two
1535 constants are defined in <netinet/in.h>:
1537 #define INET_ADDRSTRLEN 16
1538 #define INET6_ADDRSTRLEN 46
1540 The inet_ntop() function returns a pointer to the buffer containing
1541 the text string if the conversion succeeds, and NULL otherwise. Upon
1542 failure, errno is set to EAFNOSUPPORT if the af argument is invalid
1543 or ENOSPC if the size of the result buffer is inadequate.
1545 6.6. Address Testing Macros
1547 The following macros can be used to test for special IPv6 addresses.
1549 #include <netinet/in.h>
1551 int IN6_IS_ADDR_UNSPECIFIED (const struct in6_addr *);
1552 int IN6_IS_ADDR_LOOPBACK (const struct in6_addr *);
1553 int IN6_IS_ADDR_MULTICAST (const struct in6_addr *);
1554 int IN6_IS_ADDR_LINKLOCAL (const struct in6_addr *);
1555 int IN6_IS_ADDR_SITELOCAL (const struct in6_addr *);
1556 int IN6_IS_ADDR_V4MAPPED (const struct in6_addr *);
1557 int IN6_IS_ADDR_V4COMPAT (const struct in6_addr *);
1559 int IN6_IS_ADDR_MC_NODELOCAL(const struct in6_addr *);
1560 int IN6_IS_ADDR_MC_LINKLOCAL(const struct in6_addr *);
1561 int IN6_IS_ADDR_MC_SITELOCAL(const struct in6_addr *);
1562 int IN6_IS_ADDR_MC_ORGLOCAL (const struct in6_addr *);
1563 int IN6_IS_ADDR_MC_GLOBAL (const struct in6_addr *);
1570 Gilligan, et. al. Informational [Page 28]
1572 RFC 2133 IPv6 Socket Interface Extensions April 1997
1575 The first seven macros return true if the address is of the specified
1576 type, or false otherwise. The last five test the scope of a
1577 multicast address and return true if the address is a multicast
1578 address of the specified scope or false if the address is either not
1579 a multicast address or not of the specified scope.
1581 7. Summary of New Definitions
1583 The following list summarizes the constants, structure, and extern
1584 definitions discussed in this memo, sorted by header.
1587 <net/if.h> struct if_nameindex{};
1589 <netdb.h> AI_CANONNAME
1590 <netdb.h> AI_PASSIVE
1591 <netdb.h> EAI_ADDRFAMILY
1593 <netdb.h> EAI_BADFLAGS
1595 <netdb.h> EAI_FAMILY
1596 <netdb.h> EAI_MEMORY
1597 <netdb.h> EAI_NODATA
1598 <netdb.h> EAI_NONAME
1599 <netdb.h> EAI_SERVICE
1600 <netdb.h> EAI_SOCKTYPE
1601 <netdb.h> EAI_SYSTEM
1603 <netdb.h> NI_MAXHOST
1604 <netdb.h> NI_MAXSERV
1605 <netdb.h> NI_NAMEREQD
1607 <netdb.h> NI_NUMERICHOST
1608 <netdb.h> NI_NUMERICSERV
1609 <netdb.h> struct addrinfo{};
1611 <netinet/in.h> IN6ADDR_ANY_INIT
1612 <netinet/in.h> IN6ADDR_LOOPBACK_INIT
1613 <netinet/in.h> INET6_ADDRSTRLEN
1614 <netinet/in.h> INET_ADDRSTRLEN
1615 <netinet/in.h> IPPROTO_IPV6
1616 <netinet/in.h> IPV6_ADDRFORM
1617 <netinet/in.h> IPV6_ADD_MEMBERSHIP
1618 <netinet/in.h> IPV6_DROP_MEMBERSHIP
1619 <netinet/in.h> IPV6_MULTICAST_HOPS
1620 <netinet/in.h> IPV6_MULTICAST_IF
1621 <netinet/in.h> IPV6_MULTICAST_LOOP
1622 <netinet/in.h> IPV6_UNICAST_HOPS
1626 Gilligan, et. al. Informational [Page 29]
1628 RFC 2133 IPv6 Socket Interface Extensions April 1997
1631 <netinet/in.h> SIN6_LEN
1632 <netinet/in.h> extern const struct in6_addr in6addr_any;
1633 <netinet/in.h> extern const struct in6_addr in6addr_loopback;
1634 <netinet/in.h> struct in6_addr{};
1635 <netinet/in.h> struct ipv6_mreq{};
1636 <netinet/in.h> struct sockaddr_in6{};
1638 <resolv.h> RES_USE_INET6
1640 <sys/socket.h> AF_INET6
1641 <sys/socket.h> PF_INET6
1644 The following list summarizes the function and macro prototypes
1645 discussed in this memo, sorted by header.
1647 <arpa/inet.h> int inet_pton(int, const char *, void *);
1648 <arpa/inet.h> const char *inet_ntop(int, const void *,
1651 <net/if.h> char *if_indextoname(unsigned int, char *);
1652 <net/if.h> unsigned int if_nametoindex(const char *);
1653 <net/if.h> void if_freenameindex(struct if_nameindex *);
1654 <net/if.h> struct if_nameindex *if_nameindex(void);
1656 <netdb.h> int getaddrinfo(const char *, const char *,
1657 const struct addrinfo *,
1658 struct addrinfo **);
1659 <netdb.h> int getnameinfo(const struct sockaddr *, size_t,
1660 char *, size_t, char *, size_t, int);
1661 <netdb.h> void freeaddrinfo(struct addrinfo *);
1662 <netdb.h> char *gai_strerror(int);
1663 <netdb.h> struct hostent *gethostbyname(const char *);
1664 <netdb.h> struct hostent *gethostbyaddr(const char *, int, int);
1665 <netdb.h> struct hostent *gethostbyname2(const char *, int);
1667 <netinet/in.h> int IN6_IS_ADDR_LINKLOCAL(const struct in6_addr *);
1668 <netinet/in.h> int IN6_IS_ADDR_LOOPBACK(const struct in6_addr *);
1669 <netinet/in.h> int IN6_IS_ADDR_MC_GLOBAL(const struct in6_addr *);
1670 <netinet/in.h> int IN6_IS_ADDR_MC_LINKLOCAL(const struct in6_addr *);
1671 <netinet/in.h> int IN6_IS_ADDR_MC_NODELOCAL(const struct in6_addr *);
1672 <netinet/in.h> int IN6_IS_ADDR_MC_ORGLOCAL(const struct in6_addr *);
1673 <netinet/in.h> int IN6_IS_ADDR_MC_SITELOCAL(const struct in6_addr *);
1674 <netinet/in.h> int IN6_IS_ADDR_MULTICAST(const struct in6_addr *);
1675 <netinet/in.h> int IN6_IS_ADDR_SITELOCAL(const struct in6_addr *);
1676 <netinet/in.h> int IN6_IS_ADDR_UNSPECIFIED(const struct in6_addr *);
1677 <netinet/in.h> int IN6_IS_ADDR_V4COMPAT(const struct in6_addr *);
1678 <netinet/in.h> int IN6_IS_ADDR_V4MAPPED(const struct in6_addr *);
1682 Gilligan, et. al. Informational [Page 30]
1684 RFC 2133 IPv6 Socket Interface Extensions April 1997
1687 8. Security Considerations
1689 IPv6 provides a number of new security mechanisms, many of which need
1690 to be accessible to applications. A companion memo detailing the
1691 extensions to the socket interfaces to support IPv6 security is being
1696 Thanks to the many people who made suggestions and provided feedback
1697 to to the numerous revisions of this document, including: Werner
1698 Almesberger, Ran Atkinson, Fred Baker, Dave Borman, Andrew Cherenson,
1699 Alex Conta, Alan Cox, Steve Deering, Richard Draves, Francis Dupont,
1700 Robert Elz, Marc Hasson, Tim Hartrick, Tom Herbert, Bob Hinden, Wan-
1701 Yen Hsu, Christian Huitema, Koji Imada, Markus Jork, Ron Lee, Alan
1702 Lloyd, Charles Lynn, Jack McCann, Dan McDonald, Dave Mitton, Thomas
1703 Narten, Erik Nordmark, Josh Osborne, Craig Partridge, Jean-Luc
1704 Richier, Erik Scoredos, Keith Sklower, Matt Thomas, Harvey Thompson,
1705 Dean D. Throop, Karen Tracey, Glenn Trewitt, Paul Vixie, David
1706 Waitzman, Carl Williams, and Kazuhiko Yamamoto,
1708 The getaddrinfo() and getnameinfo() functions are taken from an
1709 earlier Work in Progress by Keith Sklower. As noted in that
1710 document, William Durst, Steven Wise, Michael Karels, and Eric Allman
1711 provided many useful discussions on the subject of protocol-
1712 independent name-to-address translation, and reviewed early versions
1713 of Keith Sklower's original proposal. Eric Allman implemented the
1714 first prototype of getaddrinfo(). The observation that specifying
1715 the pair of name and service would suffice for connecting to a
1716 service independent of protocol details was made by Marshall Rose in
1717 a proposal to X/Open for a "Uniform Network Interface".
1719 Craig Metz made many contributions to this document. Ramesh Govindan
1720 made a number of contributions and co-authored an earlier version of
1725 [1] Deering, S., and R. Hinden, "Internet Protocol, Version 6 (IPv6)
1726 Specification", RFC 1883, December 1995.
1728 [2] Hinden, R., and S. Deering, "IP Version 6 Addressing Architecture",
1729 RFC 1884, December 1995.
1731 [3] McDonald, D., "A Simple IP Security API Extension to BSD Sockets",
1738 Gilligan, et. al. Informational [Page 31]
1740 RFC 2133 IPv6 Socket Interface Extensions April 1997
1743 [4] IEEE, "Protocol Independent Interfaces", IEEE Std 1003.1g, DRAFT
1746 [5] Stevens, W., and M. Thomas, "Advanced Sockets API for IPv6",
1749 [6] Vixie, P., "Reverse Name Lookups of Encapsulated IPv4 Addresses in
1750 IPv6", Work in Progress.
1752 11. Authors' Addresses
1755 Freegate Corporation
1756 710 Lakeway Dr. STE 230
1759 Phone: +1 408 524 4804
1760 EMail: gilligan@freegate.net
1764 Bell Communications Research
1765 MRE 2P-343, 445 South Street
1766 Morristown, NJ 07960
1768 Phone: +1 201 829 4514
1769 EMail: set@thumper.bellcore.com
1773 Digital Equipment Corporation
1774 110 Spitbrook Road ZK3-3/U14
1775 Nashua, NH 03062-2698
1777 Phone: +1 603 881 0400
1778 Email: bound@zk3.dec.com
1782 1202 E. Paseo del Zorro
1783 Tucson, AZ 85718-2826
1785 Phone: +1 520 297 9416
1786 EMail: rstevens@kohala.com
1794 Gilligan, et. al. Informational [Page 32]