7 Network Working Group R. Gilligan
8 Request for Comments: 3493 Intransa, Inc.
9 Obsoletes: 2553 S. Thomson
10 Category: Informational Cisco
18 Basic Socket Interface Extensions for IPv6
22 This memo provides information for the Internet community. It does
23 not specify an Internet standard of any kind. Distribution of this
28 Copyright (C) The Internet Society (2003). All Rights Reserved.
32 The de facto standard Application Program Interface (API) for TCP/IP
33 applications is the "sockets" interface. Although this API was
34 developed for Unix in the early 1980s it has also been implemented on
35 a wide variety of non-Unix systems. TCP/IP applications written
36 using the sockets API have in the past enjoyed a high degree of
37 portability and we would like the same portability with IPv6
38 applications. But changes are required to the sockets API to support
39 IPv6 and this memo describes these changes. These include a new
40 socket address structure to carry IPv6 addresses, new address
41 conversion functions, and some new socket options. These extensions
42 are designed to provide access to the basic IPv6 features required by
43 TCP and UDP applications, including multicasting, while introducing a
44 minimum of change into the system and providing complete
45 compatibility for existing IPv4 applications. Additional extensions
46 for advanced IPv6 features (raw sockets and access to the IPv6
47 extension headers) are defined in another document.
58 Gilligan, et al. Informational [Page 1]
60 RFC 3493 Basic Socket Interface Extensions for IPv6 February 2003
65 1. Introduction................................................3
66 2. Design Considerations.......................................4
67 2.1 What Needs to be Changed...............................4
68 2.2 Data Types.............................................6
69 2.3 Headers................................................6
70 2.4 Structures.............................................6
71 3. Socket Interface............................................6
72 3.1 IPv6 Address Family and Protocol Family................6
73 3.2 IPv6 Address Structure.................................7
74 3.3 Socket Address Structure for 4.3BSD-Based Systems......7
75 3.4 Socket Address Structure for 4.4BSD-Based Systems......9
76 3.5 The Socket Functions...................................9
77 3.6 Compatibility with IPv4 Applications..................10
78 3.7 Compatibility with IPv4 Nodes.........................11
79 3.8 IPv6 Wildcard Address.................................11
80 3.9 IPv6 Loopback Address.................................13
81 3.10 Portability Additions.................................14
82 4. Interface Identification...................................16
83 4.1 Name-to-Index.........................................17
84 4.2 Index-to-Name.........................................17
85 4.3 Return All Interface Names and Indexes................18
86 4.4 Free Memory...........................................18
87 5. Socket Options.............................................18
88 5.1 Unicast Hop Limit.....................................19
89 5.2 Sending and Receiving Multicast Packets...............19
90 5.3 IPV6_V6ONLY option for AF_INET6 Sockets...............22
91 6. Library Functions..........................................22
92 6.1 Protocol-Independent Nodename and
93 Service Name Translation..............................23
94 6.2 Socket Address Structure to Node Name
95 and Service Name......................................28
96 6.3 Address Conversion Functions..........................31
97 6.4 Address Testing Macros................................33
98 7. Summary of New Definitions.................................33
99 8. Security Considerations....................................35
100 9. Changes from RFC 2553......................................35
101 10. Acknowledgments............................................36
102 11. References.................................................37
103 12. Authors' Addresses.........................................38
104 13. Full Copyright Statement...................................39
114 Gilligan, et al. Informational [Page 2]
116 RFC 3493 Basic Socket Interface Extensions for IPv6 February 2003
121 While IPv4 addresses are 32 bits long, IPv6 addresses are 128 bits
122 long. The socket interface makes the size of an IP address quite
123 visible to an application; virtually all TCP/IP applications for
124 BSD-based systems have knowledge of the size of an IP address. Those
125 parts of the API that expose the addresses must be changed to
126 accommodate the larger IPv6 address size. IPv6 also introduces new
127 features, some of which must be made visible to applications via the
128 API. This memo defines a set of extensions to the socket interface
129 to support the larger address size and new features of IPv6. It
130 defines "basic" extensions that are of use to a broad range of
131 applications. A companion document, the "advanced" API [4], covers
132 extensions that are of use to more specialized applications, examples
133 of which include routing daemons, and the "ping" and "traceroute"
136 The development of this API was started in 1994 in the IETF IPng
137 working group. The API has evolved over the years, published first
138 in RFC 2133, then again in RFC 2553, and reaching its final form in
141 As the API matured and stabilized, it was incorporated into the Open
142 Group's Networking Services (XNS) specification, issue 5.2, which was
143 subsequently incorporated into a joint Open Group/IEEE/ISO standard
146 Effort has been made to ensure that this document and [3] contain the
147 same information with regard to the API definitions. However, the
148 reader should note that this document is for informational purposes
149 only, and that the official standard specification of the sockets API
152 It is expected that any future standardization work on this API would
153 be done by the Open Group Base Working Group [6].
155 It should also be noted that this document describes only those
156 portions of the API needed for IPv4 and IPv6 communications. Other
157 potential uses of the API, for example the use of getaddrinfo() and
158 getnameinfo() with the AF_UNIX address family, are beyond the scope
170 Gilligan, et al. Informational [Page 3]
172 RFC 3493 Basic Socket Interface Extensions for IPv6 February 2003
175 2. Design Considerations
177 There are a number of important considerations in designing changes
178 to this well-worn API:
180 - The API changes should provide both source and binary
181 compatibility for programs written to the original API. That is,
182 existing program binaries should continue to operate when run on a
183 system supporting the new API. In addition, existing applications
184 that are re-compiled and run on a system supporting the new API
185 should continue to operate. Simply put, the API changes for IPv6
186 should not break existing programs. An additional mechanism for
187 implementations to verify this is to verify the new symbols are
188 protected by Feature Test Macros as described in [3]. (Such
189 Feature Test Macros are not defined by this RFC.)
191 - The changes to the API should be as small as possible in order to
192 simplify the task of converting existing IPv4 applications to
195 - Where possible, applications should be able to use this API to
196 interoperate with both IPv6 and IPv4 hosts. Applications should
197 not need to know which type of host they are communicating with.
199 - IPv6 addresses carried in data structures should be 64-bit
200 aligned. This is necessary in order to obtain optimum performance
201 on 64-bit machine architectures.
203 Because of the importance of providing IPv4 compatibility in the API,
204 these extensions are explicitly designed to operate on machines that
205 provide complete support for both IPv4 and IPv6. A subset of this
206 API could probably be designed for operation on systems that support
207 only IPv6. However, this is not addressed in this memo.
209 2.1 What Needs to be Changed
211 The socket interface API consists of a few distinct components:
213 - Core socket functions.
215 - Address data structures.
217 - Name-to-address translation functions.
219 - Address conversion functions.
226 Gilligan, et al. Informational [Page 4]
228 RFC 3493 Basic Socket Interface Extensions for IPv6 February 2003
231 The core socket functions -- those functions that deal with such
232 things as setting up and tearing down TCP connections, and sending
233 and receiving UDP packets -- were designed to be transport
234 independent. Where protocol addresses are passed as function
235 arguments, they are carried via opaque pointers. A protocol-specific
236 address data structure is defined for each protocol that the socket
237 functions support. Applications must cast pointers to these
238 protocol-specific address structures into pointers to the generic
239 "sockaddr" address structure when using the socket functions. These
240 functions need not change for IPv6, but a new IPv6-specific address
241 data structure is needed.
243 The "sockaddr_in" structure is the protocol-specific data structure
244 for IPv4. This data structure actually includes 8-octets of unused
245 space, and it is tempting to try to use this space to adapt the
246 sockaddr_in structure to IPv6. Unfortunately, the sockaddr_in
247 structure is not large enough to hold the 16-octet IPv6 address as
248 well as the other information (address family and port number) that
249 is needed. So a new address data structure must be defined for IPv6.
251 IPv6 addresses are scoped [2] so they could be link-local, site,
252 organization, global, or other scopes at this time undefined. To
253 support applications that want to be able to identify a set of
254 interfaces for a specific scope, the IPv6 sockaddr_in structure must
255 support a field that can be used by an implementation to identify a
256 set of interfaces identifying the scope for an IPv6 address.
258 The IPv4 name-to-address translation functions in the socket
259 interface are gethostbyname() and gethostbyaddr(). These are left as
260 is, and new functions are defined which support both IPv4 and IPv6.
262 The IPv4 address conversion functions -- inet_ntoa() and inet_addr()
263 -- convert IPv4 addresses between binary and printable form. These
264 functions are quite specific to 32-bit IPv4 addresses. We have
265 designed two analogous functions that convert both IPv4 and IPv6
266 addresses, and carry an address type parameter so that they can be
267 extended to other protocol families as well.
269 Finally, a few miscellaneous features are needed to support IPv6. A
270 new interface is needed to support the IPv6 hop limit header field.
271 New socket options are needed to control the sending and receiving of
272 IPv6 multicast packets.
274 The socket interface will be enhanced in the future to provide access
275 to other IPv6 features. Some of these extensions are described in
282 Gilligan, et al. Informational [Page 5]
284 RFC 3493 Basic Socket Interface Extensions for IPv6 February 2003
289 The data types of the structure elements given in this memo are
290 intended to track the relevant standards. uintN_t means an unsigned
291 integer of exactly N bits (e.g., uint16_t). The sa_family_t and
292 in_port_t types are defined in [3].
296 When function prototypes and structures are shown we show the headers
297 that must be #included to cause that item to be defined.
301 When structures are described the members shown are the ones that
302 must appear in an implementation. Additional, nonstandard members
303 may also be defined by an implementation. As an additional
304 precaution nonstandard members could be verified by Feature Test
305 Macros as described in [3]. (Such Feature Test Macros are not
306 defined by this RFC.)
308 The ordering shown for the members of a structure is the recommended
309 ordering, given alignment considerations of multibyte members, but an
310 implementation may order the members differently.
314 This section specifies the socket interface changes for IPv6.
316 3.1 IPv6 Address Family and Protocol Family
318 A new address family name, AF_INET6, is defined in <sys/socket.h>.
319 The AF_INET6 definition distinguishes between the original
320 sockaddr_in address data structure, and the new sockaddr_in6 data
323 A new protocol family name, PF_INET6, is defined in <sys/socket.h>.
324 Like most of the other protocol family names, this will usually be
325 defined to have the same value as the corresponding address family
328 #define PF_INET6 AF_INET6
330 The AF_INET6 is used in the first argument to the socket() function
331 to indicate that an IPv6 socket is being created.
338 Gilligan, et al. Informational [Page 6]
340 RFC 3493 Basic Socket Interface Extensions for IPv6 February 2003
343 3.2 IPv6 Address Structure
345 A new in6_addr structure holds a single IPv6 address and is defined
346 as a result of including <netinet/in.h>:
349 uint8_t s6_addr[16]; /* IPv6 address */
352 This data structure contains an array of sixteen 8-bit elements,
353 which make up one 128-bit IPv6 address. The IPv6 address is stored
354 in network byte order.
356 The structure in6_addr above is usually implemented with an embedded
357 union with extra fields that force the desired alignment level in a
358 manner similar to BSD implementations of "struct in_addr". Those
359 additional implementation details are omitted here for simplicity.
361 An example is as follows:
370 #define s6_addr _S6_un._S6_u8
372 3.3 Socket Address Structure for 4.3BSD-Based Systems
374 In the socket interface, a different protocol-specific data structure
375 is defined to carry the addresses for each protocol suite. Each
376 protocol-specific data structure is designed so it can be cast into a
377 protocol-independent data structure -- the "sockaddr" structure.
378 Each has a "family" field that overlays the "sa_family" of the
379 sockaddr data structure. This field identifies the type of the data
382 The sockaddr_in structure is the protocol-specific address data
383 structure for IPv4. It is used to pass addresses between
384 applications and the system in the socket functions. The following
385 sockaddr_in6 structure holds IPv6 addresses and is defined as a
386 result of including the <netinet/in.h> header:
394 Gilligan, et al. Informational [Page 7]
396 RFC 3493 Basic Socket Interface Extensions for IPv6 February 2003
399 struct sockaddr_in6 {
400 sa_family_t sin6_family; /* AF_INET6 */
401 in_port_t sin6_port; /* transport layer port # */
402 uint32_t sin6_flowinfo; /* IPv6 flow information */
403 struct in6_addr sin6_addr; /* IPv6 address */
404 uint32_t sin6_scope_id; /* set of interfaces for a scope */
407 This structure is designed to be compatible with the sockaddr data
408 structure used in the 4.3BSD release.
410 The sin6_family field identifies this as a sockaddr_in6 structure.
411 This field overlays the sa_family field when the buffer is cast to a
412 sockaddr data structure. The value of this field must be AF_INET6.
414 The sin6_port field contains the 16-bit UDP or TCP port number. This
415 field is used in the same way as the sin_port field of the
416 sockaddr_in structure. The port number is stored in network byte
419 The sin6_flowinfo field is a 32-bit field intended to contain flow-
420 related information. The exact way this field is mapped to or from a
421 packet is not currently specified. Until such time as its use is
422 specified, applications should set this field to zero when
423 constructing a sockaddr_in6, and ignore this field in a sockaddr_in6
424 structure constructed by the system.
426 The sin6_addr field is a single in6_addr structure (defined in the
427 previous section). This field holds one 128-bit IPv6 address. The
428 address is stored in network byte order.
430 The ordering of elements in this structure is specifically designed
431 so that when sin6_addr field is aligned on a 64-bit boundary, the
432 start of the structure will also be aligned on a 64-bit boundary.
433 This is done for optimum performance on 64-bit architectures.
435 The sin6_scope_id field is a 32-bit integer that identifies a set of
436 interfaces as appropriate for the scope [2] of the address carried in
437 the sin6_addr field. The mapping of sin6_scope_id to an interface or
438 set of interfaces is left to implementation and future specifications
439 on the subject of scoped addresses.
441 Notice that the sockaddr_in6 structure will normally be larger than
442 the generic sockaddr structure. On many existing implementations the
443 sizeof(struct sockaddr_in) equals sizeof(struct sockaddr), with both
444 being 16 bytes. Any existing code that makes this assumption needs
445 to be examined carefully when converting to IPv6.
450 Gilligan, et al. Informational [Page 8]
452 RFC 3493 Basic Socket Interface Extensions for IPv6 February 2003
455 3.4 Socket Address Structure for 4.4BSD-Based Systems
457 The 4.4BSD release includes a small, but incompatible change to the
458 socket interface. The "sa_family" field of the sockaddr data
459 structure was changed from a 16-bit value to an 8-bit value, and the
460 space saved used to hold a length field, named "sa_len". The
461 sockaddr_in6 data structure given in the previous section cannot be
462 correctly cast into the newer sockaddr data structure. For this
463 reason, the following alternative IPv6 address data structure is
464 provided to be used on systems based on 4.4BSD. It is defined as a
465 result of including the <netinet/in.h> header.
467 struct sockaddr_in6 {
468 uint8_t sin6_len; /* length of this struct */
469 sa_family_t sin6_family; /* AF_INET6 */
470 in_port_t sin6_port; /* transport layer port # */
471 uint32_t sin6_flowinfo; /* IPv6 flow information */
472 struct in6_addr sin6_addr; /* IPv6 address */
473 uint32_t sin6_scope_id; /* set of interfaces for a scope */
476 The only differences between this data structure and the 4.3BSD
477 variant are the inclusion of the length field, and the change of the
478 family field to a 8-bit data type. The definitions of all the other
479 fields are identical to the structure defined in the previous
482 Systems that provide this version of the sockaddr_in6 data structure
483 must also declare SIN6_LEN as a result of including the
484 <netinet/in.h> header. This macro allows applications to determine
485 whether they are being built on a system that supports the 4.3BSD or
486 4.4BSD variants of the data structure.
488 3.5 The Socket Functions
490 Applications call the socket() function to create a socket descriptor
491 that represents a communication endpoint. The arguments to the
492 socket() function tell the system which protocol to use, and what
493 format address structure will be used in subsequent functions. For
494 example, to create an IPv4/TCP socket, applications make the call:
496 s = socket(AF_INET, SOCK_STREAM, 0);
498 To create an IPv4/UDP socket, applications make the call:
500 s = socket(AF_INET, SOCK_DGRAM, 0);
506 Gilligan, et al. Informational [Page 9]
508 RFC 3493 Basic Socket Interface Extensions for IPv6 February 2003
511 Applications may create IPv6/TCP and IPv6/UDP sockets (which may also
512 handle IPv4 communication as described in section 3.7) by simply
513 using the constant AF_INET6 instead of AF_INET in the first argument.
514 For example, to create an IPv6/TCP socket, applications make the
517 s = socket(AF_INET6, SOCK_STREAM, 0);
519 To create an IPv6/UDP socket, applications make the call:
521 s = socket(AF_INET6, SOCK_DGRAM, 0);
523 Once the application has created a AF_INET6 socket, it must use the
524 sockaddr_in6 address structure when passing addresses in to the
525 system. The functions that the application uses to pass addresses
533 The system will use the sockaddr_in6 address structure to return
534 addresses to applications that are using AF_INET6 sockets. The
535 functions that return an address from the system to an application
544 No changes to the syntax of the socket functions are needed to
545 support IPv6, since all of the "address carrying" functions use an
546 opaque address pointer, and carry an address length as a function
549 3.6 Compatibility with IPv4 Applications
551 In order to support the large base of applications using the original
552 API, system implementations must provide complete source and binary
553 compatibility with the original API. This means that systems must
554 continue to support AF_INET sockets and the sockaddr_in address
555 structure. Applications must be able to create IPv4/TCP and IPv4/UDP
556 sockets using the AF_INET constant in the socket() function, as
562 Gilligan, et al. Informational [Page 10]
564 RFC 3493 Basic Socket Interface Extensions for IPv6 February 2003
567 described in the previous section. Applications should be able to
568 hold a combination of IPv4/TCP, IPv4/UDP, IPv6/TCP and IPv6/UDP
569 sockets simultaneously within the same process.
571 Applications using the original API should continue to operate as
572 they did on systems supporting only IPv4. That is, they should
573 continue to interoperate with IPv4 nodes.
575 3.7 Compatibility with IPv4 Nodes
577 The API also provides a different type of compatibility: the ability
578 for IPv6 applications to interoperate with IPv4 applications. This
579 feature uses the IPv4-mapped IPv6 address format defined in the IPv6
580 addressing architecture specification [2]. This address format
581 allows the IPv4 address of an IPv4 node to be represented as an IPv6
582 address. The IPv4 address is encoded into the low-order 32 bits of
583 the IPv6 address, and the high-order 96 bits hold the fixed prefix
584 0:0:0:0:0:FFFF. IPv4-mapped addresses are written as follows:
586 ::FFFF:<IPv4-address>
588 These addresses can be generated automatically by the getaddrinfo()
589 function, as described in Section 6.1.
591 Applications may use AF_INET6 sockets to open TCP connections to IPv4
592 nodes, or send UDP packets to IPv4 nodes, by simply encoding the
593 destination's IPv4 address as an IPv4-mapped IPv6 address, and
594 passing that address, within a sockaddr_in6 structure, in the
595 connect() or sendto() call. When applications use AF_INET6 sockets
596 to accept TCP connections from IPv4 nodes, or receive UDP packets
597 from IPv4 nodes, the system returns the peer's address to the
598 application in the accept(), recvfrom(), or getpeername() call using
599 a sockaddr_in6 structure encoded this way.
601 Few applications will likely need to know which type of node they are
602 interoperating with. However, for those applications that do need to
603 know, the IN6_IS_ADDR_V4MAPPED() macro, defined in Section 6.4, is
606 3.8 IPv6 Wildcard Address
608 While the bind() function allows applications to select the source IP
609 address of UDP packets and TCP connections, applications often want
610 the system to select the source address for them. With IPv4, one
611 specifies the address as the symbolic constant INADDR_ANY (called the
612 "wildcard" address) in the bind() call, or simply omits the bind()
618 Gilligan, et al. Informational [Page 11]
620 RFC 3493 Basic Socket Interface Extensions for IPv6 February 2003
623 Since the IPv6 address type is a structure (struct in6_addr), a
624 symbolic constant can be used to initialize an IPv6 address variable,
625 but cannot be used in an assignment. Therefore systems provide the
626 IPv6 wildcard address in two forms.
628 The first version is a global variable named "in6addr_any" that is an
629 in6_addr structure. The extern declaration for this variable is
630 defined in <netinet/in.h>:
632 extern const struct in6_addr in6addr_any;
634 Applications use in6addr_any similarly to the way they use INADDR_ANY
635 in IPv4. For example, to bind a socket to port number 23, but let
636 the system select the source address, an application could use the
639 struct sockaddr_in6 sin6;
641 sin6.sin6_family = AF_INET6;
642 sin6.sin6_flowinfo = 0;
643 sin6.sin6_port = htons(23);
644 sin6.sin6_addr = in6addr_any; /* structure assignment */
646 if (bind(s, (struct sockaddr *) &sin6, sizeof(sin6)) == -1)
649 The other version is a symbolic constant named IN6ADDR_ANY_INIT and
650 is defined in <netinet/in.h>. This constant can be used to
651 initialize an in6_addr structure:
653 struct in6_addr anyaddr = IN6ADDR_ANY_INIT;
655 Note that this constant can be used ONLY at declaration time. It can
656 not be used to assign a previously declared in6_addr structure. For
657 example, the following code will not work:
659 /* This is the WRONG way to assign an unspecified address */
660 struct sockaddr_in6 sin6;
662 sin6.sin6_addr = IN6ADDR_ANY_INIT; /* will NOT compile */
664 Be aware that the IPv4 INADDR_xxx constants are all defined in host
665 byte order but the IPv6 IN6ADDR_xxx constants and the IPv6
666 in6addr_xxx externals are defined in network byte order.
674 Gilligan, et al. Informational [Page 12]
676 RFC 3493 Basic Socket Interface Extensions for IPv6 February 2003
679 3.9 IPv6 Loopback Address
681 Applications may need to send UDP packets to, or originate TCP
682 connections to, services residing on the local node. In IPv4, they
683 can do this by using the constant IPv4 address INADDR_LOOPBACK in
684 their connect(), sendto(), or sendmsg() call.
686 IPv6 also provides a loopback address to contact local TCP and UDP
687 services. Like the unspecified address, the IPv6 loopback address is
688 provided in two forms -- a global variable and a symbolic constant.
690 The global variable is an in6_addr structure named
691 "in6addr_loopback." The extern declaration for this variable is
692 defined in <netinet/in.h>:
694 extern const struct in6_addr in6addr_loopback;
696 Applications use in6addr_loopback as they would use INADDR_LOOPBACK
697 in IPv4 applications (but beware of the byte ordering difference
698 mentioned at the end of the previous section). For example, to open
699 a TCP connection to the local telnet server, an application could use
702 struct sockaddr_in6 sin6;
704 sin6.sin6_family = AF_INET6;
705 sin6.sin6_flowinfo = 0;
706 sin6.sin6_port = htons(23);
707 sin6.sin6_addr = in6addr_loopback; /* structure assignment */
709 if (connect(s, (struct sockaddr *) &sin6, sizeof(sin6)) == -1)
712 The symbolic constant is named IN6ADDR_LOOPBACK_INIT and is defined
713 in <netinet/in.h>. It can be used at declaration time ONLY; for
716 struct in6_addr loopbackaddr = IN6ADDR_LOOPBACK_INIT;
718 Like IN6ADDR_ANY_INIT, this constant cannot be used in an assignment
719 to a previously declared IPv6 address variable.
730 Gilligan, et al. Informational [Page 13]
732 RFC 3493 Basic Socket Interface Extensions for IPv6 February 2003
735 3.10 Portability Additions
737 One simple addition to the sockets API that can help application
738 writers is the "struct sockaddr_storage". This data structure can
739 simplify writing code that is portable across multiple address
740 families and platforms. This data structure is designed with the
743 - Large enough to accommodate all supported protocol-specific address
746 - Aligned at an appropriate boundary so that pointers to it can be
747 cast as pointers to protocol specific address structures and used
748 to access the fields of those structures without alignment
751 The sockaddr_storage structure contains field ss_family which is of
752 type sa_family_t. When a sockaddr_storage structure is cast to a
753 sockaddr structure, the ss_family field of the sockaddr_storage
754 structure maps onto the sa_family field of the sockaddr structure.
755 When a sockaddr_storage structure is cast as a protocol specific
756 address structure, the ss_family field maps onto a field of that
757 structure that is of type sa_family_t and that identifies the
758 protocol's address family.
786 Gilligan, et al. Informational [Page 14]
788 RFC 3493 Basic Socket Interface Extensions for IPv6 February 2003
791 An example implementation design of such a data structure would be as
795 * Desired design of maximum size and alignment
797 #define _SS_MAXSIZE 128 /* Implementation specific max size */
798 #define _SS_ALIGNSIZE (sizeof (int64_t))
799 /* Implementation specific desired alignment */
801 * Definitions used for sockaddr_storage structure paddings design.
803 #define _SS_PAD1SIZE (_SS_ALIGNSIZE - sizeof (sa_family_t))
804 #define _SS_PAD2SIZE (_SS_MAXSIZE - (sizeof (sa_family_t) +
805 _SS_PAD1SIZE + _SS_ALIGNSIZE))
806 struct sockaddr_storage {
807 sa_family_t ss_family; /* address family */
808 /* Following fields are implementation specific */
809 char __ss_pad1[_SS_PAD1SIZE];
810 /* 6 byte pad, this is to make implementation
811 /* specific pad up to alignment field that */
812 /* follows explicit in the data structure */
813 int64_t __ss_align; /* field to force desired structure */
814 /* storage alignment */
815 char __ss_pad2[_SS_PAD2SIZE];
816 /* 112 byte pad to achieve desired size, */
817 /* _SS_MAXSIZE value minus size of ss_family */
818 /* __ss_pad1, __ss_align fields is 112 */
821 The above example implementation illustrates a data structure which
822 will align on a 64-bit boundary. An implementation-specific field
823 "__ss_align" along with "__ss_pad1" is used to force a 64-bit
824 alignment which covers proper alignment good enough for the needs of
825 sockaddr_in6 (IPv6), sockaddr_in (IPv4) address data structures. The
826 size of padding field __ss_pad1 depends on the chosen alignment
827 boundary. The size of padding field __ss_pad2 depends on the value
828 of overall size chosen for the total size of the structure. This
829 size and alignment are represented in the above example by
830 implementation specific (not required) constants _SS_MAXSIZE (chosen
831 value 128) and _SS_ALIGNSIZE (with chosen value 8). Constants
832 _SS_PAD1SIZE (derived value 6) and _SS_PAD2SIZE (derived value 112)
833 are also for illustration and not required. The derived values
834 assume sa_family_t is 2 bytes. The implementation specific
835 definitions and structure field names above start with an underscore
836 to denote implementation private namespace. Portable code is not
837 expected to access or reference those fields or constants.
842 Gilligan, et al. Informational [Page 15]
844 RFC 3493 Basic Socket Interface Extensions for IPv6 February 2003
847 On implementations where the sockaddr data structure includes a
848 "sa_len" field this data structure would look like this:
851 * Definitions used for sockaddr_storage structure paddings design.
853 #define _SS_PAD1SIZE (_SS_ALIGNSIZE -
854 (sizeof (uint8_t) + sizeof (sa_family_t))
855 #define _SS_PAD2SIZE (_SS_MAXSIZE -
856 (sizeof (uint8_t) + sizeof (sa_family_t) +
857 _SS_PAD1SIZE + _SS_ALIGNSIZE))
858 struct sockaddr_storage {
859 uint8_t ss_len; /* address length */
860 sa_family_t ss_family; /* address family */
861 /* Following fields are implementation specific */
862 char __ss_pad1[_SS_PAD1SIZE];
863 /* 6 byte pad, this is to make implementation
864 /* specific pad up to alignment field that */
865 /* follows explicit in the data structure */
866 int64_t __ss_align; /* field to force desired structure */
867 /* storage alignment */
868 char __ss_pad2[_SS_PAD2SIZE];
869 /* 112 byte pad to achieve desired size, */
870 /* _SS_MAXSIZE value minus size of ss_len, */
871 /* __ss_family, __ss_pad1, __ss_align fields is 112 */
874 4. Interface Identification
876 This API uses an interface index (a small positive integer) to
877 identify the local interface on which a multicast group is joined
878 (Section 5.2). Additionally, the advanced API [4] uses these same
879 interface indexes to identify the interface on which a datagram is
880 received, or to specify the interface on which a datagram is to be
883 Interfaces are normally known by names such as "le0", "sl1", "ppp2",
884 and the like. On Berkeley-derived implementations, when an interface
885 is made known to the system, the kernel assigns a unique positive
886 integer value (called the interface index) to that interface. These
887 are small positive integers that start at 1. (Note that 0 is never
888 used for an interface index.) There may be gaps so that there is no
889 current interface for a particular positive interface index.
891 This API defines two functions that map between an interface name and
892 index, a third function that returns all the interface names and
893 indexes, and a fourth function to return the dynamic memory allocated
894 by the previous function. How these functions are implemented is
898 Gilligan, et al. Informational [Page 16]
900 RFC 3493 Basic Socket Interface Extensions for IPv6 February 2003
903 left up to the implementation. 4.4BSD implementations can implement
904 these functions using the existing sysctl() function with the
905 NET_RT_IFLIST command. Other implementations may wish to use ioctl()
910 The first function maps an interface name into its corresponding
915 unsigned int if_nametoindex(const char *ifname);
917 If ifname is the name of an interface, the if_nametoindex() function
918 shall return the interface index corresponding to name ifname;
919 otherwise, it shall return zero. No errors are defined.
923 The second function maps an interface index into its corresponding
928 char *if_indextoname(unsigned int ifindex, char *ifname);
930 When this function is called, the ifname argument shall point to a
931 buffer of at least IF_NAMESIZE bytes. The function shall place in
932 this buffer the name of the interface with index ifindex.
933 (IF_NAMESIZE is also defined in <net/if.h> and its value includes a
934 terminating null byte at the end of the interface name.) If ifindex
935 is an interface index, then the function shall return the value
936 supplied in ifname, which points to a buffer now containing the
937 interface name. Otherwise, the function shall return a NULL pointer
938 and set errno to indicate the error. If there is no interface
939 corresponding to the specified index, errno is set to ENXIO. If
940 there was a system error (such as running out of memory), errno would
941 be set to the proper value (e.g., ENOMEM).
954 Gilligan, et al. Informational [Page 17]
956 RFC 3493 Basic Socket Interface Extensions for IPv6 February 2003
959 4.3 Return All Interface Names and Indexes
961 The if_nameindex structure holds the information about a single
962 interface and is defined as a result of including the <net/if.h>
965 struct if_nameindex {
966 unsigned int if_index; /* 1, 2, ... */
967 char *if_name; /* null terminated name: "le0", ... */
970 The final function returns an array of if_nameindex structures, one
971 structure per interface.
975 struct if_nameindex *if_nameindex(void);
977 The end of the array of structures is indicated by a structure with
978 an if_index of 0 and an if_name of NULL. The function returns a NULL
979 pointer upon an error, and would set errno to the appropriate value.
981 The memory used for this array of structures along with the interface
982 names pointed to by the if_name members is obtained dynamically.
983 This memory is freed by the next function.
987 The following function frees the dynamic memory that was allocated by
992 void if_freenameindex(struct if_nameindex *ptr);
994 The ptr argument shall be a pointer that was returned by
995 if_nameindex(). After if_freenameindex() has been called, the
996 application shall not use the array of which ptr is the address.
1000 A number of new socket options are defined for IPv6. All of these
1001 new options are at the IPPROTO_IPV6 level. That is, the "level"
1002 parameter in the getsockopt() and setsockopt() calls is IPPROTO_IPV6
1003 when using these options. The constant name prefix IPV6_ is used in
1004 all of the new socket options. This serves to clearly identify these
1005 options as applying to IPv6.
1010 Gilligan, et al. Informational [Page 18]
1012 RFC 3493 Basic Socket Interface Extensions for IPv6 February 2003
1015 The declaration for IPPROTO_IPV6, the new IPv6 socket options, and
1016 related constants defined in this section are obtained by including
1017 the header <netinet/in.h>.
1019 5.1 Unicast Hop Limit
1021 A new setsockopt() option controls the hop limit used in outgoing
1022 unicast IPv6 packets. The name of this option is IPV6_UNICAST_HOPS,
1023 and it is used at the IPPROTO_IPV6 layer. The following example
1024 illustrates how it is used:
1028 if (setsockopt(s, IPPROTO_IPV6, IPV6_UNICAST_HOPS,
1029 (char *) &hoplimit, sizeof(hoplimit)) == -1)
1030 perror("setsockopt IPV6_UNICAST_HOPS");
1032 When the IPV6_UNICAST_HOPS option is set with setsockopt(), the
1033 option value given is used as the hop limit for all subsequent
1034 unicast packets sent via that socket. If the option is not set, the
1035 system selects a default value. The integer hop limit value (called
1036 x) is interpreted as follows:
1038 x < -1: return an error of EINVAL
1039 x == -1: use kernel default
1040 0 <= x <= 255: use x
1041 x >= 256: return an error of EINVAL
1043 The IPV6_UNICAST_HOPS option may be used with getsockopt() to
1044 determine the hop limit value that the system will use for subsequent
1045 unicast packets sent via that socket. For example:
1048 socklen_t len = sizeof(hoplimit);
1050 if (getsockopt(s, IPPROTO_IPV6, IPV6_UNICAST_HOPS,
1051 (char *) &hoplimit, &len) == -1)
1052 perror("getsockopt IPV6_UNICAST_HOPS");
1054 printf("Using %d for hop limit.\n", hoplimit);
1056 5.2 Sending and Receiving Multicast Packets
1058 IPv6 applications may send multicast packets by simply specifying an
1059 IPv6 multicast address as the destination address, for example in the
1060 destination address argument of the sendto() function.
1066 Gilligan, et al. Informational [Page 19]
1068 RFC 3493 Basic Socket Interface Extensions for IPv6 February 2003
1071 Three socket options at the IPPROTO_IPV6 layer control some of the
1072 parameters for sending multicast packets. Setting these options is
1073 not required: applications may send multicast packets without using
1074 these options. The setsockopt() options for controlling the sending
1075 of multicast packets are summarized below. These three options can
1076 also be used with getsockopt().
1080 Set the interface to use for outgoing multicast packets. The
1081 argument is the index of the interface to use. If the
1082 interface index is specified as zero, the system selects the
1083 interface (for example, by looking up the address in a routing
1084 table and using the resulting interface).
1086 Argument type: unsigned int
1090 Set the hop limit to use for outgoing multicast packets. (Note
1091 a separate option - IPV6_UNICAST_HOPS - is provided to set the
1092 hop limit to use for outgoing unicast packets.)
1094 The interpretation of the argument is the same as for the
1095 IPV6_UNICAST_HOPS option:
1097 x < -1: return an error of EINVAL
1098 x == -1: use kernel default
1099 0 <= x <= 255: use x
1100 x >= 256: return an error of EINVAL
1102 If IPV6_MULTICAST_HOPS is not set, the default is 1
1103 (same as IPv4 today)
1109 If a multicast datagram is sent to a group to which the sending
1110 host itself belongs (on the outgoing interface), a copy of the
1111 datagram is looped back by the IP layer for local delivery if
1112 this option is set to 1. If this option is set to 0 a copy is
1113 not looped back. Other option values return an error of
1122 Gilligan, et al. Informational [Page 20]
1124 RFC 3493 Basic Socket Interface Extensions for IPv6 February 2003
1127 If IPV6_MULTICAST_LOOP is not set, the default is 1 (loopback;
1128 same as IPv4 today).
1130 Argument type: unsigned int
1132 The reception of multicast packets is controlled by the two
1133 setsockopt() options summarized below. An error of EOPNOTSUPP is
1134 returned if these two options are used with getsockopt().
1138 Join a multicast group on a specified local interface.
1139 If the interface index is specified as 0,
1140 the kernel chooses the local interface.
1141 For example, some kernels look up the multicast group
1142 in the normal IPv6 routing table and use the resulting
1145 Argument type: struct ipv6_mreq
1149 Leave a multicast group on a specified interface.
1150 If the interface index is specified as 0, the system
1151 may choose a multicast group membership to drop by
1152 matching the multicast address only.
1154 Argument type: struct ipv6_mreq
1156 The argument type of both of these options is the ipv6_mreq
1157 structure, defined as a result of including the <netinet/in.h>
1161 struct in6_addr ipv6mr_multiaddr; /* IPv6 multicast addr */
1162 unsigned int ipv6mr_interface; /* interface index */
1165 Note that to receive multicast datagrams a process must join the
1166 multicast group to which datagrams will be sent. UDP applications
1167 must also bind the UDP port to which datagrams will be sent. Some
1168 processes also bind the multicast group address to the socket, in
1169 addition to the port, to prevent other datagrams destined to that
1170 same port from being delivered to the socket.
1178 Gilligan, et al. Informational [Page 21]
1180 RFC 3493 Basic Socket Interface Extensions for IPv6 February 2003
1183 5.3 IPV6_V6ONLY option for AF_INET6 Sockets
1185 This socket option restricts AF_INET6 sockets to IPv6 communications
1186 only. As stated in section <3.7 Compatibility with IPv4 Nodes>,
1187 AF_INET6 sockets may be used for both IPv4 and IPv6 communications.
1188 Some applications may want to restrict their use of an AF_INET6
1189 socket to IPv6 communications only. For these applications the
1190 IPV6_V6ONLY socket option is defined. When this option is turned on,
1191 the socket can be used to send and receive IPv6 packets only. This
1192 is an IPPROTO_IPV6 level option. This option takes an int value.
1193 This is a boolean option. By default this option is turned off.
1195 Here is an example of setting this option:
1199 if (setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY,
1200 (char *)&on, sizeof(on)) == -1)
1201 perror("setsockopt IPV6_V6ONLY");
1203 printf("IPV6_V6ONLY set\n");
1205 Note - This option has no effect on the use of IPv4 Mapped addresses
1206 which enter a node as a valid IPv6 addresses for IPv6 communications
1207 as defined by Stateless IP/ICMP Translation Algorithm (SIIT) [5].
1209 An example use of this option is to allow two versions of the same
1210 server process to run on the same port, one providing service over
1211 IPv6, the other providing the same service over IPv4.
1213 6. Library Functions
1215 New library functions are needed to perform a variety of operations
1216 with IPv6 addresses. Functions are needed to lookup IPv6 addresses
1217 in the Domain Name System (DNS). Both forward lookup (nodename-to-
1218 address translation) and reverse lookup (address-to-nodename
1219 translation) need to be supported. Functions are also needed to
1220 convert IPv6 addresses between their binary and textual form.
1222 We note that the two existing functions, gethostbyname() and
1223 gethostbyaddr(), are left as-is. New functions are defined to handle
1224 both IPv4 and IPv6 addresses.
1226 The commonly used function gethostbyname() is inadequate for many
1227 applications, first because it provides no way for the caller to
1228 specify anything about the types of addresses desired (IPv4 only,
1229 IPv6 only, IPv4-mapped IPv6 are OK, etc.), and second because many
1230 implementations of this function are not thread safe. RFC 2133
1234 Gilligan, et al. Informational [Page 22]
1236 RFC 3493 Basic Socket Interface Extensions for IPv6 February 2003
1239 defined a function named gethostbyname2() but this function was also
1240 inadequate, first because its use required setting a global option
1241 (RES_USE_INET6) when IPv6 addresses were required, and second because
1242 a flag argument is needed to provide the caller with additional
1243 control over the types of addresses required. The gethostbyname2()
1244 function was deprecated in RFC 2553 and is no longer part of the
1247 6.1 Protocol-Independent Nodename and Service Name Translation
1249 Nodename-to-address translation is done in a protocol-independent
1250 fashion using the getaddrinfo() function.
1252 #include <sys/socket.h>
1256 int getaddrinfo(const char *nodename, const char *servname,
1257 const struct addrinfo *hints, struct addrinfo **res);
1259 void freeaddrinfo(struct addrinfo *ai);
1262 int ai_flags; /* AI_PASSIVE, AI_CANONNAME,
1263 AI_NUMERICHOST, .. */
1264 int ai_family; /* AF_xxx */
1265 int ai_socktype; /* SOCK_xxx */
1266 int ai_protocol; /* 0 or IPPROTO_xxx for IPv4 and IPv6 */
1267 socklen_t ai_addrlen; /* length of ai_addr */
1268 char *ai_canonname; /* canonical name for nodename */
1269 struct sockaddr *ai_addr; /* binary address */
1270 struct addrinfo *ai_next; /* next structure in linked list */
1273 The getaddrinfo() function translates the name of a service location
1274 (for example, a host name) and/or a service name and returns a set of
1275 socket addresses and associated information to be used in creating a
1276 socket with which to address the specified service.
1278 The nodename and servname arguments are either null pointers or
1279 pointers to null-terminated strings. One or both of these two
1280 arguments must be a non-null pointer.
1282 The format of a valid name depends on the address family or families.
1283 If a specific family is not given and the name could be interpreted
1284 as valid within multiple supported families, the implementation will
1285 attempt to resolve the name in all supported families and, in absence
1286 of errors, one or more results shall be returned.
1290 Gilligan, et al. Informational [Page 23]
1292 RFC 3493 Basic Socket Interface Extensions for IPv6 February 2003
1295 If the nodename argument is not null, it can be a descriptive name or
1296 can be an address string. If the specified address family is
1297 AF_INET, AF_INET6, or AF_UNSPEC, valid descriptive names include host
1298 names. If the specified address family is AF_INET or AF_UNSPEC,
1299 address strings using Internet standard dot notation as specified in
1300 inet_addr() are valid. If the specified address family is AF_INET6
1301 or AF_UNSPEC, standard IPv6 text forms described in inet_pton() are
1304 If nodename is not null, the requested service location is named by
1305 nodename; otherwise, the requested service location is local to the
1308 If servname is null, the call shall return network-level addresses
1309 for the specified nodename. If servname is not null, it is a null-
1310 terminated character string identifying the requested service. This
1311 can be either a descriptive name or a numeric representation suitable
1312 for use with the address family or families. If the specified
1313 address family is AF_INET, AF_INET6 or AF_UNSPEC, the service can be
1314 specified as a string specifying a decimal port number.
1316 If the argument hints is not null, it refers to a structure
1317 containing input values that may direct the operation by providing
1318 options and by limiting the returned information to a specific socket
1319 type, address family and/or protocol. In this hints structure every
1320 member other than ai_flags, ai_family, ai_socktype and ai_protocol
1321 shall be set to zero or a null pointer. A value of AF_UNSPEC for
1322 ai_family means that the caller shall accept any address family. A
1323 value of zero for ai_socktype means that the caller shall accept any
1324 socket type. A value of zero for ai_protocol means that the caller
1325 shall accept any protocol. If hints is a null pointer, the behavior
1326 shall be as if it referred to a structure containing the value zero
1327 for the ai_flags, ai_socktype and ai_protocol fields, and AF_UNSPEC
1328 for the ai_family field.
1332 1. If the caller handles only TCP and not UDP, for example, then the
1333 ai_protocol member of the hints structure should be set to
1334 IPPROTO_TCP when getaddrinfo() is called.
1336 2. If the caller handles only IPv4 and not IPv6, then the ai_family
1337 member of the hints structure should be set to AF_INET when
1338 getaddrinfo() is called.
1346 Gilligan, et al. Informational [Page 24]
1348 RFC 3493 Basic Socket Interface Extensions for IPv6 February 2003
1351 The ai_flags field to which hints parameter points shall be set to
1352 zero or be the bitwise-inclusive OR of one or more of the values
1353 AI_PASSIVE, AI_CANONNAME, AI_NUMERICHOST, AI_NUMERICSERV,
1354 AI_V4MAPPED, AI_ALL, and AI_ADDRCONFIG.
1356 If the AI_PASSIVE flag is specified, the returned address information
1357 shall be suitable for use in binding a socket for accepting incoming
1358 connections for the specified service (i.e., a call to bind()). In
1359 this case, if the nodename argument is null, then the IP address
1360 portion of the socket address structure shall be set to INADDR_ANY
1361 for an IPv4 address or IN6ADDR_ANY_INIT for an IPv6 address. If the
1362 AI_PASSIVE flag is not specified, the returned address information
1363 shall be suitable for a call to connect() (for a connection-mode
1364 protocol) or for a call to connect(), sendto() or sendmsg() (for a
1365 connectionless protocol). In this case, if the nodename argument is
1366 null, then the IP address portion of the socket address structure
1367 shall be set to the loopback address. This flag is ignored if the
1368 nodename argument is not null.
1370 If the AI_CANONNAME flag is specified and the nodename argument is
1371 not null, the function shall attempt to determine the canonical name
1372 corresponding to nodename (for example, if nodename is an alias or
1373 shorthand notation for a complete name).
1375 If the AI_NUMERICHOST flag is specified, then a non-null nodename
1376 string supplied shall be a numeric host address string. Otherwise,
1377 an [EAI_NONAME] error is returned. This flag shall prevent any type
1378 of name resolution service (for example, the DNS) from being invoked.
1380 If the AI_NUMERICSERV flag is specified, then a non-null servname
1381 string supplied shall be a numeric port string. Otherwise, an
1382 [EAI_NONAME] error shall be returned. This flag shall prevent any
1383 type of name resolution service (for example, NIS+) from being
1386 If the AI_V4MAPPED flag is specified along with an ai_family of
1387 AF_INET6, then getaddrinfo() shall return IPv4-mapped IPv6 addresses
1388 on finding no matching IPv6 addresses (ai_addrlen shall be 16).
1390 For example, when using the DNS, if no AAAA records are found then
1391 a query is made for A records and any found are returned as IPv4-
1392 mapped IPv6 addresses.
1394 The AI_V4MAPPED flag shall be ignored unless ai_family equals
1397 If the AI_ALL flag is used with the AI_V4MAPPED flag, then
1398 getaddrinfo() shall return all matching IPv6 and IPv4 addresses.
1402 Gilligan, et al. Informational [Page 25]
1404 RFC 3493 Basic Socket Interface Extensions for IPv6 February 2003
1407 For example, when using the DNS, queries are made for both AAAA
1408 records and A records, and getaddrinfo() returns the combined
1409 results of both queries. Any IPv4 addresses found are returned as
1410 IPv4-mapped IPv6 addresses.
1412 The AI_ALL flag without the AI_V4MAPPED flag is ignored.
1416 When ai_family is not specified (AF_UNSPEC), AI_V4MAPPED and
1417 AI_ALL flags will only be used if AF_INET6 is supported.
1419 If the AI_ADDRCONFIG flag is specified, IPv4 addresses shall be
1420 returned only if an IPv4 address is configured on the local system,
1421 and IPv6 addresses shall be returned only if an IPv6 address is
1422 configured on the local system. The loopback address is not
1423 considered for this case as valid as a configured address.
1425 For example, when using the DNS, a query for AAAA records should
1426 occur only if the node has at least one IPv6 address configured
1427 (other than IPv6 loopback) and a query for A records should occur
1428 only if the node has at least one IPv4 address configured (other
1429 than the IPv4 loopback).
1431 The ai_socktype field to which argument hints points specifies the
1432 socket type for the service, as defined for socket(). If a specific
1433 socket type is not given (for example, a value of zero) and the
1434 service name could be interpreted as valid with multiple supported
1435 socket types, the implementation shall attempt to resolve the service
1436 name for all supported socket types and, in the absence of errors,
1437 all possible results shall be returned. A non-zero socket type value
1438 shall limit the returned information to values with the specified
1441 If the ai_family field to which hints points has the value AF_UNSPEC,
1442 addresses shall be returned for use with any address family that can
1443 be used with the specified nodename and/or servname. Otherwise,
1444 addresses shall be returned for use only with the specified address
1445 family. If ai_family is not AF_UNSPEC and ai_protocol is not zero,
1446 then addresses are returned for use only with the specified address
1447 family and protocol; the value of ai_protocol shall be interpreted as
1448 in a call to the socket() function with the corresponding values of
1449 ai_family and ai_protocol.
1451 The freeaddrinfo() function frees one or more addrinfo structures
1452 returned by getaddrinfo(), along with any additional storage
1453 associated with those structures (for example, storage pointed to by
1454 the ai_canonname and ai_addr fields; an application must not
1458 Gilligan, et al. Informational [Page 26]
1460 RFC 3493 Basic Socket Interface Extensions for IPv6 February 2003
1463 reference this storage after the associated addrinfo structure has
1464 been freed). If the ai_next field of the structure is not null, the
1465 entire list of structures is freed. The freeaddrinfo() function must
1466 support the freeing of arbitrary sublists of an addrinfo list
1467 originally returned by getaddrinfo().
1469 Functions getaddrinfo() and freeaddrinfo() must be thread-safe.
1471 A zero return value for getaddrinfo() indicates successful
1472 completion; a non-zero return value indicates failure. The possible
1473 values for the failures are listed below under Error Return Values.
1475 Upon successful return of getaddrinfo(), the location to which res
1476 points shall refer to a linked list of addrinfo structures, each of
1477 which shall specify a socket address and information for use in
1478 creating a socket with which to use that socket address. The list
1479 shall include at least one addrinfo structure. The ai_next field of
1480 each structure contains a pointer to the next structure on the list,
1481 or a null pointer if it is the last structure on the list. Each
1482 structure on the list shall include values for use with a call to the
1483 socket() function, and a socket address for use with the connect()
1484 function or, if the AI_PASSIVE flag was specified, for use with the
1485 bind() function. The fields ai_family, ai_socktype, and ai_protocol
1486 shall be usable as the arguments to the socket() function to create a
1487 socket suitable for use with the returned address. The fields
1488 ai_addr and ai_addrlen are usable as the arguments to the connect()
1489 or bind() functions with such a socket, according to the AI_PASSIVE
1492 If nodename is not null, and if requested by the AI_CANONNAME flag,
1493 the ai_canonname field of the first returned addrinfo structure shall
1494 point to a null-terminated string containing the canonical name
1495 corresponding to the input nodename; if the canonical name is not
1496 available, then ai_canonname shall refer to the nodename argument or
1497 a string with the same contents. The contents of the ai_flags field
1498 of the returned structures are undefined.
1500 All fields in socket address structures returned by getaddrinfo()
1501 that are not filled in through an explicit argument (for example,
1502 sin6_flowinfo) shall be set to zero.
1504 Note: This makes it easier to compare socket address structures.
1514 Gilligan, et al. Informational [Page 27]
1516 RFC 3493 Basic Socket Interface Extensions for IPv6 February 2003
1519 Error Return Values:
1521 The getaddrinfo() function shall fail and return the corresponding
1524 [EAI_AGAIN] The name could not be resolved at this time. Future
1525 attempts may succeed.
1527 [EAI_BADFLAGS] The flags parameter had an invalid value.
1529 [EAI_FAIL] A non-recoverable error occurred when attempting to
1532 [EAI_FAMILY] The address family was not recognized.
1534 [EAI_MEMORY] There was a memory allocation failure when trying to
1535 allocate storage for the return value.
1537 [EAI_NONAME] The name does not resolve for the supplied
1538 parameters. Neither nodename nor servname were
1539 supplied. At least one of these must be supplied.
1541 [EAI_SERVICE] The service passed was not recognized for the
1542 specified socket type.
1544 [EAI_SOCKTYPE] The intended socket type was not recognized.
1546 [EAI_SYSTEM] A system error occurred; the error code can be found
1549 The gai_strerror() function provides a descriptive text string
1550 corresponding to an EAI_xxx error value.
1554 const char *gai_strerror(int ecode);
1556 The argument is one of the EAI_xxx values defined for the
1557 getaddrinfo() and getnameinfo() functions. The return value points
1558 to a string describing the error. If the argument is not one of the
1559 EAI_xxx values, the function still returns a pointer to a string
1560 whose contents indicate an unknown error.
1562 6.2 Socket Address Structure to Node Name and Service Name
1564 The getnameinfo() function is used to translate the contents of a
1565 socket address structure to a node name and/or service name.
1570 Gilligan, et al. Informational [Page 28]
1572 RFC 3493 Basic Socket Interface Extensions for IPv6 February 2003
1575 #include <sys/socket.h>
1578 int getnameinfo(const struct sockaddr *sa, socklen_t salen,
1579 char *node, socklen_t nodelen,
1580 char *service, socklen_t servicelen,
1583 The getnameinfo() function shall translate a socket address to a node
1584 name and service location, all of which are defined as in
1587 The sa argument points to a socket address structure to be
1590 The salen argument holds the size of the socket address structure
1593 If the socket address structure contains an IPv4-mapped IPv6 address
1594 or an IPv4-compatible IPv6 address, the implementation shall extract
1595 the embedded IPv4 address and lookup the node name for that IPv4
1598 Note: The IPv6 unspecified address ("::") and the IPv6 loopback
1599 address ("::1") are not IPv4-compatible addresses. If the address
1600 is the IPv6 unspecified address ("::"), a lookup is not performed,
1601 and the [EAI_NONAME] error is returned.
1603 If the node argument is non-NULL and the nodelen argument is nonzero,
1604 then the node argument points to a buffer able to contain up to
1605 nodelen characters that receives the node name as a null-terminated
1606 string. If the node argument is NULL or the nodelen argument is
1607 zero, the node name shall not be returned. If the node's name cannot
1608 be located, the numeric form of the node's address is returned
1609 instead of its name.
1611 If the service argument is non-NULL and the servicelen argument is
1612 non-zero, then the service argument points to a buffer able to
1613 contain up to servicelen bytes that receives the service name as a
1614 null-terminated string. If the service argument is NULL or the
1615 servicelen argument is zero, the service name shall not be returned.
1616 If the service's name cannot be located, the numeric form of the
1617 service address (for example, its port number) shall be returned
1618 instead of its name.
1620 The arguments node and service cannot both be NULL.
1626 Gilligan, et al. Informational [Page 29]
1628 RFC 3493 Basic Socket Interface Extensions for IPv6 February 2003
1631 The flags argument is a flag that changes the default actions of the
1632 function. By default the fully-qualified domain name (FQDN) for the
1633 host shall be returned, but:
1635 - If the flag bit NI_NOFQDN is set, only the node name portion of
1636 the FQDN shall be returned for local hosts.
1638 - If the flag bit NI_NUMERICHOST is set, the numeric form of the
1639 host's address shall be returned instead of its name, under all
1642 - If the flag bit NI_NAMEREQD is set, an error shall be returned if
1643 the host's name cannot be located.
1645 - If the flag bit NI_NUMERICSERV is set, the numeric form of the
1646 service address shall be returned (for example, its port number)
1647 instead of its name, under all circumstances.
1649 - If the flag bit NI_DGRAM is set, this indicates that the service
1650 is a datagram service (SOCK_DGRAM). The default behavior shall
1651 assume that the service is a stream service (SOCK_STREAM).
1655 1. The NI_NUMERICxxx flags are required to support the "-n" flags
1656 that many commands provide.
1658 2. The NI_DGRAM flag is required for the few AF_INET and AF_INET6
1659 port numbers (for example, [512,514]) that represent different
1660 services for UDP and TCP.
1662 The getnameinfo() function shall be thread safe.
1664 A zero return value for getnameinfo() indicates successful
1665 completion; a non-zero return value indicates failure.
1667 Upon successful completion, getnameinfo() shall return the node and
1668 service names, if requested, in the buffers provided. The returned
1669 names are always null-terminated strings.
1682 Gilligan, et al. Informational [Page 30]
1684 RFC 3493 Basic Socket Interface Extensions for IPv6 February 2003
1687 Error Return Values:
1689 The getnameinfo() function shall fail and return the corresponding
1692 [EAI_AGAIN] The name could not be resolved at this time.
1693 Future attempts may succeed.
1695 [EAI_BADFLAGS] The flags had an invalid value.
1697 [EAI_FAIL] A non-recoverable error occurred.
1699 [EAI_FAMILY] The address family was not recognized or the address
1700 length was invalid for the specified family.
1702 [EAI_MEMORY] There was a memory allocation failure.
1704 [EAI_NONAME] The name does not resolve for the supplied parameters.
1705 NI_NAMEREQD is set and the host's name cannot be
1706 located, or both nodename and servname were null.
1708 [EAI_OVERFLOW] An argument buffer overflowed.
1710 [EAI_SYSTEM] A system error occurred. The error code can be found
1713 6.3 Address Conversion Functions
1715 The two IPv4 functions inet_addr() and inet_ntoa() convert an IPv4
1716 address between binary and text form. IPv6 applications need similar
1717 functions. The following two functions convert both IPv6 and IPv4
1720 #include <arpa/inet.h>
1722 int inet_pton(int af, const char *src, void *dst);
1724 const char *inet_ntop(int af, const void *src,
1725 char *dst, socklen_t size);
1727 The inet_pton() function shall convert an address in its standard
1728 text presentation form into its numeric binary form. The af argument
1729 shall specify the family of the address. The AF_INET and AF_INET6
1730 address families shall be supported. The src argument points to the
1731 string being passed in. The dst argument points to a buffer into
1732 which the function stores the numeric address; this shall be large
1733 enough to hold the numeric address (32 bits for AF_INET, 128 bits for
1734 AF_INET6). The inet_pton() function shall return 1 if the conversion
1738 Gilligan, et al. Informational [Page 31]
1740 RFC 3493 Basic Socket Interface Extensions for IPv6 February 2003
1743 succeeds, with the address pointed to by dst in network byte order.
1744 It shall return 0 if the input is not a valid IPv4 dotted-decimal
1745 string or a valid IPv6 address string, or -1 with errno set to
1746 EAFNOSUPPORT if the af argument is unknown.
1748 If the af argument of inet_pton() is AF_INET, the src string shall be
1749 in the standard IPv4 dotted-decimal form:
1753 where "ddd" is a one to three digit decimal number between 0 and 255.
1754 The inet_pton() function does not accept other formats (such as the
1755 octal numbers, hexadecimal numbers, and fewer than four numbers that
1756 inet_addr() accepts).
1758 If the af argument of inet_pton() is AF_INET6, the src string shall
1759 be in one of the standard IPv6 text forms defined in Section 2.2 of
1760 the addressing architecture specification [2].
1762 The inet_ntop() function shall convert a numeric address into a text
1763 string suitable for presentation. The af argument shall specify the
1764 family of the address. This can be AF_INET or AF_INET6. The src
1765 argument points to a buffer holding an IPv4 address if the af
1766 argument is AF_INET, or an IPv6 address if the af argument is
1767 AF_INET6; the address must be in network byte order. The dst
1768 argument points to a buffer where the function stores the resulting
1769 text string; it shall not be NULL. The size argument specifies the
1770 size of this buffer, which shall be large enough to hold the text
1771 string (INET_ADDRSTRLEN characters for IPv4, INET6_ADDRSTRLEN
1772 characters for IPv6).
1774 In order to allow applications to easily declare buffers of the
1775 proper size to store IPv4 and IPv6 addresses in string form, the
1776 following two constants are defined in <netinet/in.h>:
1778 #define INET_ADDRSTRLEN 16
1779 #define INET6_ADDRSTRLEN 46
1781 The inet_ntop() function shall return a pointer to the buffer
1782 containing the text string if the conversion succeeds, and NULL
1783 otherwise. Upon failure, errno is set to EAFNOSUPPORT if the af
1784 argument is invalid or ENOSPC if the size of the result buffer is
1794 Gilligan, et al. Informational [Page 32]
1796 RFC 3493 Basic Socket Interface Extensions for IPv6 February 2003
1799 6.4 Address Testing Macros
1801 The following macros can be used to test for special IPv6 addresses.
1803 #include <netinet/in.h>
1805 int IN6_IS_ADDR_UNSPECIFIED (const struct in6_addr *);
1806 int IN6_IS_ADDR_LOOPBACK (const struct in6_addr *);
1807 int IN6_IS_ADDR_MULTICAST (const struct in6_addr *);
1808 int IN6_IS_ADDR_LINKLOCAL (const struct in6_addr *);
1809 int IN6_IS_ADDR_SITELOCAL (const struct in6_addr *);
1810 int IN6_IS_ADDR_V4MAPPED (const struct in6_addr *);
1811 int IN6_IS_ADDR_V4COMPAT (const struct in6_addr *);
1813 int IN6_IS_ADDR_MC_NODELOCAL(const struct in6_addr *);
1814 int IN6_IS_ADDR_MC_LINKLOCAL(const struct in6_addr *);
1815 int IN6_IS_ADDR_MC_SITELOCAL(const struct in6_addr *);
1816 int IN6_IS_ADDR_MC_ORGLOCAL (const struct in6_addr *);
1817 int IN6_IS_ADDR_MC_GLOBAL (const struct in6_addr *);
1819 The first seven macros return true if the address is of the specified
1820 type, or false otherwise. The last five test the scope of a
1821 multicast address and return true if the address is a multicast
1822 address of the specified scope or false if the address is either not
1823 a multicast address or not of the specified scope.
1825 Note that IN6_IS_ADDR_LINKLOCAL and IN6_IS_ADDR_SITELOCAL return true
1826 only for the two types of local-use IPv6 unicast addresses (Link-
1827 Local and Site-Local) defined in [2], and that by this definition,
1828 the IN6_IS_ADDR_LINKLOCAL macro returns false for the IPv6 loopback
1829 address (::1). These two macros do not return true for IPv6
1830 multicast addresses of either link-local scope or site-local scope.
1832 7. Summary of New Definitions
1834 The following list summarizes the constants, structure, and extern
1835 definitions discussed in this memo, sorted by header.
1837 <net/if.h> IF_NAMESIZE
1838 <net/if.h> struct if_nameindex{};
1840 <netdb.h> AI_ADDRCONFIG
1842 <netdb.h> AI_CANONNAME
1843 <netdb.h> AI_NUMERICHOST
1844 <netdb.h> AI_NUMERICSERV
1845 <netdb.h> AI_PASSIVE
1846 <netdb.h> AI_V4MAPPED
1850 Gilligan, et al. Informational [Page 33]
1852 RFC 3493 Basic Socket Interface Extensions for IPv6 February 2003
1856 <netdb.h> EAI_BADFLAGS
1858 <netdb.h> EAI_FAMILY
1859 <netdb.h> EAI_MEMORY
1860 <netdb.h> EAI_NONAME
1861 <netdb.h> EAI_OVERFLOW
1862 <netdb.h> EAI_SERVICE
1863 <netdb.h> EAI_SOCKTYPE
1864 <netdb.h> EAI_SYSTEM
1866 <netdb.h> NI_NAMEREQD
1868 <netdb.h> NI_NUMERICHOST
1869 <netdb.h> NI_NUMERICSERV
1870 <netdb.h> struct addrinfo{};
1872 <netinet/in.h> IN6ADDR_ANY_INIT
1873 <netinet/in.h> IN6ADDR_LOOPBACK_INIT
1874 <netinet/in.h> INET6_ADDRSTRLEN
1875 <netinet/in.h> INET_ADDRSTRLEN
1876 <netinet/in.h> IPPROTO_IPV6
1877 <netinet/in.h> IPV6_JOIN_GROUP
1878 <netinet/in.h> IPV6_LEAVE_GROUP
1879 <netinet/in.h> IPV6_MULTICAST_HOPS
1880 <netinet/in.h> IPV6_MULTICAST_IF
1881 <netinet/in.h> IPV6_MULTICAST_LOOP
1882 <netinet/in.h> IPV6_UNICAST_HOPS
1883 <netinet/in.h> IPV6_V6ONLY
1884 <netinet/in.h> SIN6_LEN
1885 <netinet/in.h> extern const struct in6_addr in6addr_any;
1886 <netinet/in.h> extern const struct in6_addr in6addr_loopback;
1887 <netinet/in.h> struct in6_addr{};
1888 <netinet/in.h> struct ipv6_mreq{};
1889 <netinet/in.h> struct sockaddr_in6{};
1891 <sys/socket.h> AF_INET6
1892 <sys/socket.h> PF_INET6
1893 <sys/socket.h> struct sockaddr_storage;
1895 The following list summarizes the function and macro prototypes
1896 discussed in this memo, sorted by header.
1898 <arpa/inet.h> int inet_pton(int, const char *, void *);
1899 <arpa/inet.h> const char *inet_ntop(int, const void *,
1906 Gilligan, et al. Informational [Page 34]
1908 RFC 3493 Basic Socket Interface Extensions for IPv6 February 2003
1911 <net/if.h> char *if_indextoname(unsigned int, char *);
1912 <net/if.h> unsigned int if_nametoindex(const char *);
1913 <net/if.h> void if_freenameindex(struct if_nameindex *);
1914 <net/if.h> struct if_nameindex *if_nameindex(void);
1916 <netdb.h> int getaddrinfo(const char *, const char *,
1917 const struct addrinfo *,
1918 struct addrinfo **);
1919 <netdb.h> int getnameinfo(const struct sockaddr *, socklen_t,
1920 char *, socklen_t, char *, socklen_t, int);
1921 <netdb.h> void freeaddrinfo(struct addrinfo *);
1922 <netdb.h> const char *gai_strerror(int);
1924 <netinet/in.h> int IN6_IS_ADDR_LINKLOCAL(const struct in6_addr *);
1925 <netinet/in.h> int IN6_IS_ADDR_LOOPBACK(const struct in6_addr *);
1926 <netinet/in.h> int IN6_IS_ADDR_MC_GLOBAL(const struct in6_addr *);
1927 <netinet/in.h> int IN6_IS_ADDR_MC_LINKLOCAL(const struct in6_addr *);
1928 <netinet/in.h> int IN6_IS_ADDR_MC_NODELOCAL(const struct in6_addr *);
1929 <netinet/in.h> int IN6_IS_ADDR_MC_ORGLOCAL(const struct in6_addr *);
1930 <netinet/in.h> int IN6_IS_ADDR_MC_SITELOCAL(const struct in6_addr *);
1931 <netinet/in.h> int IN6_IS_ADDR_MULTICAST(const struct in6_addr *);
1932 <netinet/in.h> int IN6_IS_ADDR_SITELOCAL(const struct in6_addr *);
1933 <netinet/in.h> int IN6_IS_ADDR_UNSPECIFIED(const struct in6_addr *);
1934 <netinet/in.h> int IN6_IS_ADDR_V4COMPAT(const struct in6_addr *);
1935 <netinet/in.h> int IN6_IS_ADDR_V4MAPPED(const struct in6_addr *);
1937 8. Security Considerations
1939 IPv6 provides a number of new security mechanisms, many of which need
1940 to be accessible to applications. Companion memos detailing the
1941 extensions to the socket interfaces to support IPv6 security are
1944 9. Changes from RFC 2553
1946 1. Add brief description of the history of this API and its relation
1947 to the Open Group/IEEE/ISO standards.
1949 2. Alignments with [3].
1951 3. Removed all references to getipnodebyname() and getipnodebyaddr(),
1952 which are deprecated in favor of getaddrinfo() and getnameinfo().
1954 4. Added IPV6_V6ONLY IP level socket option to permit nodes to not
1955 process IPv4 packets as IPv4 Mapped addresses in implementations.
1957 5. Added SIIT to references and added new contributors.
1962 Gilligan, et al. Informational [Page 35]
1964 RFC 3493 Basic Socket Interface Extensions for IPv6 February 2003
1967 6. In previous versions of this specification, the sin6_flowinfo
1968 field was associated with the IPv6 traffic class and flow label,
1969 but its usage was not completely specified. The complete
1970 definition of the sin6_flowinfo field, including its association
1971 with the traffic class or flow label, is now deferred to a future
1976 This specification's evolution and completeness were significantly
1977 influenced by the efforts of Richard Stevens, who has passed on.
1978 Richard's wisdom and talent made the specification what it is today.
1979 The co-authors will long think of Richard with great respect.
1981 Thanks to the many people who made suggestions and provided feedback
1982 to this document, including:
1984 Werner Almesberger, Ran Atkinson, Fred Baker, Dave Borman, Andrew
1985 Cherenson, Alex Conta, Alan Cox, Steve Deering, Richard Draves,
1986 Francis Dupont, Robert Elz, Brian Haberman, Jun-ichiro itojun Hagino,
1987 Marc Hasson, Tom Herbert, Bob Hinden, Wan-Yen Hsu, Christian Huitema,
1988 Koji Imada, Markus Jork, Ron Lee, Alan Lloyd, Charles Lynn, Dan
1989 McDonald, Dave Mitton, Finnbarr Murphy, Thomas Narten, Josh Osborne,
1990 Craig Partridge, Jean-Luc Richier, Bill Sommerfield, Erik Scoredos,
1991 Keith Sklower, JINMEI Tatuya, Dave Thaler, Matt Thomas, Harvey
1992 Thompson, Dean D. Throop, Karen Tracey, Glenn Trewitt, Paul Vixie,
1993 David Waitzman, Carl Williams, Kazu Yamamoto, Vlad Yasevich, Stig
1994 Venaas, and Brian Zill.
1996 The getaddrinfo() and getnameinfo() functions are taken from an
1997 earlier document by Keith Sklower. As noted in that document,
1998 William Durst, Steven Wise, Michael Karels, and Eric Allman provided
1999 many useful discussions on the subject of protocol-independent name-
2000 to-address translation, and reviewed early versions of Keith
2001 Sklower's original proposal. Eric Allman implemented the first
2002 prototype of getaddrinfo(). The observation that specifying the pair
2003 of name and service would suffice for connecting to a service
2004 independent of protocol details was made by Marshall Rose in a
2005 proposal to X/Open for a "Uniform Network Interface".
2007 Craig Metz, Jack McCann, Erik Nordmark, Tim Hartrick, and Mukesh
2008 Kacker made many contributions to this document. Ramesh Govindan
2009 made a number of contributions and co-authored an earlier version of
2018 Gilligan, et al. Informational [Page 36]
2020 RFC 3493 Basic Socket Interface Extensions for IPv6 February 2003
2025 [1] Deering, S. and R. Hinden, "Internet Protocol, Version 6 (IPv6)
2026 Specification", RFC 2460, December 1998.
2028 [2] Hinden, R. and S. Deering, "IP Version 6 Addressing
2029 Architecture", RFC 2373, July 1998.
2031 [3] IEEE Std. 1003.1-2001 Standard for Information Technology --
2032 Portable Operating System Interface (POSIX). Open Group
2033 Technical Standard: Base Specifications, Issue 6, December 2001.
2034 ISO/IEC 9945:2002. http://www.opengroup.org/austin
2036 [4] Stevens, W. and M. Thomas, "Advanced Sockets API for IPv6", RFC
2037 2292, February 1998.
2039 [5] Nordmark, E., "Stateless IP/ICMP Translation Algorithm (SIIT)",
2040 RFC 2765, February 2000.
2042 [6] The Open Group Base Working Group
2043 http://www.opengroup.org/platform/base.html
2074 Gilligan, et al. Informational [Page 37]
2076 RFC 3493 Basic Socket Interface Extensions for IPv6 February 2003
2079 12. Authors' Addresses
2087 EMail: gilligan@intransa.com
2092 499 Thornall Street, 8th floor
2096 EMail: sethomso@cisco.com
2100 Hewlett-Packard Company
2101 110 Spitbrook Road ZKO3-3/W20
2105 EMail: Jim.Bound@hp.com
2109 Hewlett-Packard Company
2110 110 Spitbrook Road ZKO3-3/W20
2114 EMail: Jack.McCann@hp.com
2130 Gilligan, et al. Informational [Page 38]
2132 RFC 3493 Basic Socket Interface Extensions for IPv6 February 2003
2135 13. Full Copyright Statement
2137 Copyright (C) The Internet Society (2003). All Rights Reserved.
2139 This document and translations of it may be copied and furnished to
2140 others, and derivative works that comment on or otherwise explain it
2141 or assist in its implementation may be prepared, copied, published
2142 and distributed, in whole or in part, without restriction of any
2143 kind, provided that the above copyright notice and this paragraph are
2144 included on all such copies and derivative works. However, this
2145 document itself may not be modified in any way, such as by removing
2146 the copyright notice or references to the Internet Society or other
2147 Internet organizations, except as needed for the purpose of
2148 developing Internet standards in which case the procedures for
2149 copyrights defined in the Internet Standards process must be
2150 followed, or as required to translate it into languages other than
2153 The limited permissions granted above are perpetual and will not be
2154 revoked by the Internet Society or its successors or assigns.
2156 This document and the information contained herein is provided on an
2157 "AS IS" basis and THE INTERNET SOCIETY AND THE INTERNET ENGINEERING
2158 TASK FORCE DISCLAIMS ALL WARRANTIES, EXPRESS OR IMPLIED, INCLUDING
2159 BUT NOT LIMITED TO ANY WARRANTY THAT THE USE OF THE INFORMATION
2160 HEREIN WILL NOT INFRINGE ANY RIGHTS OR ANY IMPLIED WARRANTIES OF
2161 MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
2165 Funding for the RFC Editor function is currently provided by the
2186 Gilligan, et al. Informational [Page 39]