]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - contrib/bind9/doc/rfc/rfc2553.txt
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / contrib / bind9 / doc / rfc / rfc2553.txt
1
2
3
4
5
6
7 Network Working Group                                        R. Gilligan
8 Request for Comments: 2553                                      FreeGate
9 Obsoletes: 2133                                               S. Thomson
10 Category: Informational                                         Bellcore
11                                                                 J. Bound
12                                                                   Compaq
13                                                               W. Stevens
14                                                               Consultant
15                                                               March 1999
16
17
18                Basic Socket Interface Extensions for IPv6
19
20 Status of this Memo
21
22    This memo provides information for the Internet community.  It does
23    not specify an Internet standard of any kind.  Distribution of this
24    memo is unlimited.
25
26 Copyright Notice
27
28    Copyright (C) The Internet Society (1999).  All Rights Reserved.
29
30 Abstract
31
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 [4].
48
49
50
51
52
53
54
55
56
57
58 Gilligan, et. al.            Informational                      [Page 1]
59 \f
60 RFC 2553       Basic Socket Interface Extensions for IPv6     March 1999
61
62
63 Table of Contents
64
65    1. Introduction.................................................3
66    2. Design Considerations........................................3
67    2.1 What Needs to be Changed....................................4
68    2.2 Data Types..................................................5
69    2.3 Headers.....................................................5
70    2.4 Structures..................................................5
71    3. Socket Interface.............................................6
72    3.1 IPv6 Address Family and Protocol Family.....................6
73    3.2 IPv6 Address Structure......................................6
74    3.3 Socket Address Structure for 4.3BSD-Based Systems...........7
75    3.4 Socket Address Structure for 4.4BSD-Based Systems...........8
76    3.5 The Socket Functions........................................9
77    3.6 Compatibility with IPv4 Applications.......................10
78    3.7 Compatibility with IPv4 Nodes..............................10
79    3.8 IPv6 Wildcard Address......................................11
80    3.9 IPv6 Loopback Address......................................12
81    3.10 Portability Additions.....................................13
82    4. Interface Identification....................................16
83    4.1 Name-to-Index..............................................16
84    4.2 Index-to-Name..............................................17
85    4.3 Return All Interface Names and Indexes.....................17
86    4.4 Free Memory................................................18
87    5. Socket Options..............................................18
88    5.1 Unicast Hop Limit..........................................18
89    5.2 Sending and Receiving Multicast Packets....................19
90    6. Library Functions...........................................21
91    6.1 Nodename-to-Address Translation............................21
92    6.2 Address-To-Nodename Translation............................24
93    6.3 Freeing memory for getipnodebyname and getipnodebyaddr.....26
94    6.4 Protocol-Independent Nodename and Service Name Translation.26
95    6.5 Socket Address Structure to Nodename and Service Name......29
96    6.6 Address Conversion Functions...............................31
97    6.7 Address Testing Macros.....................................32
98    7. Summary of New Definitions..................................33
99    8. Security Considerations.....................................35
100    9. Year 2000 Considerations....................................35
101    Changes From RFC 2133..........................................35
102    Acknowledgments................................................38
103    References.....................................................39
104    Authors' Addresses.............................................40
105    Full Copyright Statement.......................................41
106
107
108
109
110
111
112
113
114 Gilligan, et. al.            Informational                      [Page 2]
115 \f
116 RFC 2553       Basic Socket Interface Extensions for IPv6     March 1999
117
118
119 1. Introduction
120
121    While IPv4 addresses are 32 bits long, IPv6 interfaces are identified
122    by 128-bit addresses.  The socket interface makes the size of an IP
123    address quite visible to an application; virtually all TCP/IP
124    applications for BSD-based systems have knowledge of the size of an
125    IP address.  Those parts of the API that expose the addresses must be
126    changed to accommodate the larger IPv6 address size.  IPv6 also
127    introduces new features (e.g., traffic class and flowlabel), some of
128    which must be made visible to applications via the API.  This memo
129    defines a set of extensions to the socket interface to support the
130    larger address size and new features of IPv6.
131
132 2. Design Considerations
133
134    There are a number of important considerations in designing changes
135    to this well-worn API:
136
137       - The API changes should provide both source and binary
138         compatibility for programs written to the original API.  That
139         is, existing program binaries should continue to operate when
140         run on a system supporting the new API.  In addition, existing
141         applications that are re-compiled and run on a system supporting
142         the new API should continue to operate.  Simply put, the API
143         changes for IPv6 should not break existing programs.  An
144         additonal mechanism for implementations to verify this is to
145         verify the new symbols are protected by Feature Test Macros as
146         described in IEEE Std 1003.1.  (Such Feature Test Macros are not
147         defined by this RFC.)
148
149       - The changes to the API should be as small as possible in order
150         to simplify the task of converting existing IPv4 applications to
151         IPv6.
152
153       - Where possible, applications should be able to use this API to
154         interoperate with both IPv6 and IPv4 hosts.  Applications should
155         not need to know which type of host they are communicating with.
156
157       - IPv6 addresses carried in data structures should be 64-bit
158         aligned.  This is necessary in order to obtain optimum
159         performance on 64-bit machine architectures.
160
161    Because of the importance of providing IPv4 compatibility in the API,
162    these extensions are explicitly designed to operate on machines that
163    provide complete support for both IPv4 and IPv6.  A subset of this
164    API could probably be designed for operation on systems that support
165    only IPv6.  However, this is not addressed in this memo.
166
167
168
169
170 Gilligan, et. al.            Informational                      [Page 3]
171 \f
172 RFC 2553       Basic Socket Interface Extensions for IPv6     March 1999
173
174
175 2.1 What Needs to be Changed
176
177    The socket interface API consists of a few distinct components:
178
179       -  Core socket functions.
180
181       -  Address data structures.
182
183       -  Name-to-address translation functions.
184
185       -  Address conversion functions.
186
187    The core socket functions -- those functions that deal with such
188    things as setting up and tearing down TCP connections, and sending
189    and receiving UDP packets -- were designed to be transport
190    independent.  Where protocol addresses are passed as function
191    arguments, they are carried via opaque pointers.  A protocol-specific
192    address data structure is defined for each protocol that the socket
193    functions support.  Applications must cast pointers to these
194    protocol-specific address structures into pointers to the generic
195    "sockaddr" address structure when using the socket functions.  These
196    functions need not change for IPv6, but a new IPv6-specific address
197    data structure is needed.
198
199    The "sockaddr_in" structure is the protocol-specific data structure
200    for IPv4.  This data structure actually includes 8-octets of unused
201    space, and it is tempting to try to use this space to adapt the
202    sockaddr_in structure to IPv6.  Unfortunately, the sockaddr_in
203    structure is not large enough to hold the 16-octet IPv6 address as
204    well as the other information (address family and port number) that
205    is needed.  So a new address data structure must be defined for IPv6.
206
207    IPv6 addresses are scoped [2] so they could be link-local, site,
208    organization, global, or other scopes at this time undefined.  To
209    support applications that want to be able to identify a set of
210    interfaces for a specific scope, the IPv6 sockaddr_in structure must
211    support a field that can be used by an implementation to identify a
212    set of interfaces identifying the scope for an IPv6 address.
213
214    The name-to-address translation functions in the socket interface are
215    gethostbyname() and gethostbyaddr().  These are left as is and new
216    functions are defined to support IPv4 and IPv6.  Additionally, the
217    POSIX 1003.g draft [3] specifies a new nodename-to-address
218    translation function which is protocol independent.  This function
219    can also be used with IPv4 and IPv6.
220
221
222
223
224
225
226 Gilligan, et. al.            Informational                      [Page 4]
227 \f
228 RFC 2553       Basic Socket Interface Extensions for IPv6     March 1999
229
230
231    The address conversion functions -- inet_ntoa() and inet_addr() --
232    convert IPv4 addresses between binary and printable form.  These
233    functions are quite specific to 32-bit IPv4 addresses.  We have
234    designed two analogous functions that convert both IPv4 and IPv6
235    addresses, and carry an address type parameter so that they can be
236    extended to other protocol families as well.
237
238    Finally, a few miscellaneous features are needed to support IPv6.
239    New interfaces are needed to support the IPv6 traffic class, flow
240    label, and hop limit header fields.  New socket options are needed to
241    control the sending and receiving of IPv6 multicast packets.
242
243    The socket interface will be enhanced in the future to provide access
244    to other IPv6 features.  These extensions are described in [4].
245
246 2.2 Data Types
247
248    The data types of the structure elements given in this memo are
249    intended to be examples, not absolute requirements.  Whenever
250    possible, data types from Draft 6.6 (March 1997) of POSIX 1003.1g are
251    used: uintN_t means an unsigned integer of exactly N bits (e.g.,
252    uint16_t).  We also assume the argument data types from 1003.1g when
253    possible (e.g., the final argument to setsockopt() is a size_t
254    value).  Whenever buffer sizes are specified, the POSIX 1003.1 size_t
255    data type is used (e.g., the two length arguments to getnameinfo()).
256
257 2.3 Headers
258
259    When function prototypes and structures are shown we show the headers
260    that must be #included to cause that item to be defined.
261
262 2.4 Structures
263
264    When structures are described the members shown are the ones that
265    must appear in an implementation.  Additional, nonstandard members
266    may also be defined by an implementation.  As an additional
267    precaution nonstandard members could be verified by Feature Test
268    Macros as described in IEEE Std 1003.1.  (Such Feature Test Macros
269    are not defined by this RFC.)
270
271    The ordering shown for the members of a structure is the recommended
272    ordering, given alignment considerations of multibyte members, but an
273    implementation may order the members differently.
274
275
276
277
278
279
280
281
282 Gilligan, et. al.            Informational                      [Page 5]
283 \f
284 RFC 2553       Basic Socket Interface Extensions for IPv6     March 1999
285
286
287 3. Socket Interface
288
289    This section specifies the socket interface changes for IPv6.
290
291 3.1 IPv6 Address Family and Protocol Family
292
293    A new address family name, AF_INET6, is defined in <sys/socket.h>.
294    The AF_INET6 definition distinguishes between the original
295    sockaddr_in address data structure, and the new sockaddr_in6 data
296    structure.
297
298    A new protocol family name, PF_INET6, is defined in <sys/socket.h>.
299    Like most of the other protocol family names, this will usually be
300    defined to have the same value as the corresponding address family
301    name:
302
303       #define PF_INET6        AF_INET6
304
305    The PF_INET6 is used in the first argument to the socket() function
306    to indicate that an IPv6 socket is being created.
307
308 3.2 IPv6 Address Structure
309
310    A new in6_addr structure holds a single IPv6 address and is defined
311    as a result of including <netinet/in.h>:
312
313       struct in6_addr {
314           uint8_t  s6_addr[16];      /* IPv6 address */
315       };
316
317    This data structure contains an array of sixteen 8-bit elements,
318    which make up one 128-bit IPv6 address.  The IPv6 address is stored
319    in network byte order.
320
321    The structure in6_addr above is usually implemented with an embedded
322    union with extra fields that force the desired alignment level in a
323    manner similar to BSD implementations of "struct in_addr". Those
324    additional implementation details are omitted here for simplicity.
325
326    An example is as follows:
327
328
329
330
331
332
333
334
335
336
337
338 Gilligan, et. al.            Informational                      [Page 6]
339 \f
340 RFC 2553       Basic Socket Interface Extensions for IPv6     March 1999
341
342
343    struct in6_addr {
344         union {
345             uint8_t  _S6_u8[16];
346             uint32_t _S6_u32[4];
347             uint64_t _S6_u64[2];
348         } _S6_un;
349    };
350    #define s6_addr _S6_un._S6_u8
351
352 3.3 Socket Address Structure for 4.3BSD-Based Systems
353
354    In the socket interface, a different protocol-specific data structure
355    is defined to carry the addresses for each protocol suite.  Each
356    protocol- specific data structure is designed so it can be cast into a
357    protocol- independent data structure -- the "sockaddr" structure.
358    Each has a "family" field that overlays the "sa_family" of the
359    sockaddr data structure.  This field identifies the type of the data
360    structure.
361
362    The sockaddr_in structure is the protocol-specific address data
363    structure for IPv4.  It is used to pass addresses between applications
364    and the system in the socket functions.  The following sockaddr_in6
365    structure holds IPv6 addresses and is defined as a result of including
366    the <netinet/in.h> header:
367
368 struct sockaddr_in6 {
369     sa_family_t     sin6_family;    /* AF_INET6 */
370     in_port_t       sin6_port;      /* transport layer port # */
371     uint32_t        sin6_flowinfo;  /* IPv6 traffic class & flow info */
372     struct in6_addr sin6_addr;      /* IPv6 address */
373     uint32_t        sin6_scope_id;  /* set of interfaces for a scope */
374 };
375
376    This structure is designed to be compatible with the sockaddr data
377    structure used in the 4.3BSD release.
378
379    The sin6_family field identifies this as a sockaddr_in6 structure.
380    This field overlays the sa_family field when the buffer is cast to a
381    sockaddr data structure.  The value of this field must be AF_INET6.
382
383    The sin6_port field contains the 16-bit UDP or TCP port number.  This
384    field is used in the same way as the sin_port field of the
385    sockaddr_in structure.  The port number is stored in network byte
386    order.
387
388
389
390
391
392
393
394 Gilligan, et. al.            Informational                      [Page 7]
395 \f
396 RFC 2553       Basic Socket Interface Extensions for IPv6     March 1999
397
398
399    The sin6_flowinfo field is a 32-bit field that contains two pieces of
400    information: the traffic class and the flow label.  The contents and
401    interpretation of this member is specified in [1].  The sin6_flowinfo
402    field SHOULD be set to zero by an implementation prior to using the
403    sockaddr_in6 structure by an application on receive operations.
404
405    The sin6_addr field is a single in6_addr structure (defined in the
406    previous section).  This field holds one 128-bit IPv6 address.  The
407    address is stored in network byte order.
408
409    The ordering of elements in this structure is specifically designed
410    so that when sin6_addr field is aligned on a 64-bit boundary, the
411    start of the structure will also be aligned on a 64-bit boundary.
412    This is done for optimum performance on 64-bit architectures.
413
414    The sin6_scope_id field is a 32-bit integer that identifies a set of
415    interfaces as appropriate for the scope of the address carried in the
416    sin6_addr field.  For a link scope sin6_addr sin6_scope_id would be
417    an interface index.  For a site scope sin6_addr, sin6_scope_id would
418    be a site identifier.  The mapping of sin6_scope_id to an interface
419    or set of interfaces is left to implementation and future
420    specifications on the subject of site identifiers.
421
422    Notice that the sockaddr_in6 structure will normally be larger than
423    the generic sockaddr structure.  On many existing implementations the
424    sizeof(struct sockaddr_in) equals sizeof(struct sockaddr), with both
425    being 16 bytes.  Any existing code that makes this assumption needs
426    to be examined carefully when converting to IPv6.
427
428 3.4 Socket Address Structure for 4.4BSD-Based Systems
429
430    The 4.4BSD release includes a small, but incompatible change to the
431    socket interface.  The "sa_family" field of the sockaddr data
432    structure was changed from a 16-bit value to an 8-bit value, and the
433    space saved used to hold a length field, named "sa_len".  The
434    sockaddr_in6 data structure given in the previous section cannot be
435    correctly cast into the newer sockaddr data structure.  For this
436    reason, the following alternative IPv6 address data structure is
437    provided to be used on systems based on 4.4BSD.  It is defined as a
438    result of including the <netinet/in.h> header.
439
440
441
442
443
444
445
446
447
448
449
450 Gilligan, et. al.            Informational                      [Page 8]
451 \f
452 RFC 2553       Basic Socket Interface Extensions for IPv6     March 1999
453
454
455 struct sockaddr_in6 {
456     uint8_t         sin6_len;       /* length of this struct */
457     sa_family_t     sin6_family;    /* AF_INET6 */
458     in_port_t       sin6_port;      /* transport layer port # */
459     uint32_t        sin6_flowinfo;  /* IPv6 flow information */
460     struct in6_addr sin6_addr;      /* IPv6 address */
461     uint32_t        sin6_scope_id;  /* set of interfaces for a scope */
462 };
463
464    The only differences between this data structure and the 4.3BSD
465    variant are the inclusion of the length field, and the change of the
466    family field to a 8-bit data type.  The definitions of all the other
467    fields are identical to the structure defined in the previous
468    section.
469
470    Systems that provide this version of the sockaddr_in6 data structure
471    must also declare SIN6_LEN as a result of including the
472    <netinet/in.h> header.  This macro allows applications to determine
473    whether they are being built on a system that supports the 4.3BSD or
474    4.4BSD variants of the data structure.
475
476 3.5 The Socket Functions
477
478    Applications call the socket() function to create a socket descriptor
479    that represents a communication endpoint.  The arguments to the
480    socket() function tell the system which protocol to use, and what
481    format address structure will be used in subsequent functions.  For
482    example, to create an IPv4/TCP socket, applications make the call:
483
484       s = socket(PF_INET, SOCK_STREAM, 0);
485
486    To create an IPv4/UDP socket, applications make the call:
487
488       s = socket(PF_INET, SOCK_DGRAM, 0);
489
490    Applications may create IPv6/TCP and IPv6/UDP sockets by simply using
491    the constant PF_INET6 instead of PF_INET in the first argument.  For
492    example, to create an IPv6/TCP socket, applications make the call:
493
494       s = socket(PF_INET6, SOCK_STREAM, 0);
495
496    To create an IPv6/UDP socket, applications make the call:
497
498       s = socket(PF_INET6, SOCK_DGRAM, 0);
499
500
501
502
503
504
505
506 Gilligan, et. al.            Informational                      [Page 9]
507 \f
508 RFC 2553       Basic Socket Interface Extensions for IPv6     March 1999
509
510
511    Once the application has created a PF_INET6 socket, it must use the
512    sockaddr_in6 address structure when passing addresses in to the
513    system.  The functions that the application uses to pass addresses
514    into the system are:
515
516       bind()
517       connect()
518       sendmsg()
519       sendto()
520
521    The system will use the sockaddr_in6 address structure to return
522    addresses to applications that are using PF_INET6 sockets.  The
523    functions that return an address from the system to an application
524    are:
525
526       accept()
527       recvfrom()
528       recvmsg()
529       getpeername()
530       getsockname()
531
532    No changes to the syntax of the socket functions are needed to
533    support IPv6, since all of the "address carrying" functions use an
534    opaque address pointer, and carry an address length as a function
535    argument.
536
537 3.6 Compatibility with IPv4 Applications
538
539    In order to support the large base of applications using the original
540    API, system implementations must provide complete source and binary
541    compatibility with the original API.  This means that systems must
542    continue to support PF_INET sockets and the sockaddr_in address
543    structure.  Applications must be able to create IPv4/TCP and IPv4/UDP
544    sockets using the PF_INET constant in the socket() function, as
545    described in the previous section.  Applications should be able to
546    hold a combination of IPv4/TCP, IPv4/UDP, IPv6/TCP and IPv6/UDP
547    sockets simultaneously within the same process.
548
549    Applications using the original API should continue to operate as
550    they did on systems supporting only IPv4.  That is, they should
551    continue to interoperate with IPv4 nodes.
552
553 3.7 Compatibility with IPv4 Nodes
554
555    The API also provides a different type of compatibility: the ability
556    for IPv6 applications to interoperate with IPv4 applications.  This
557    feature uses the IPv4-mapped IPv6 address format defined in the IPv6
558    addressing architecture specification [2].  This address format
559
560
561
562 Gilligan, et. al.            Informational                     [Page 10]
563 \f
564 RFC 2553       Basic Socket Interface Extensions for IPv6     March 1999
565
566
567    allows the IPv4 address of an IPv4 node to be represented as an IPv6
568    address.  The IPv4 address is encoded into the low-order 32 bits of
569    the IPv6 address, and the high-order 96 bits hold the fixed prefix
570    0:0:0:0:0:FFFF.  IPv4- mapped addresses are written as follows:
571
572       ::FFFF:<IPv4-address>
573
574    These addresses can be generated automatically by the
575    getipnodebyname() function when the specified host has only IPv4
576    addresses (as described in Section 6.1).
577
578    Applications may use PF_INET6 sockets to open TCP connections to IPv4
579    nodes, or send UDP packets to IPv4 nodes, by simply encoding the
580    destination's IPv4 address as an IPv4-mapped IPv6 address, and
581    passing that address, within a sockaddr_in6 structure, in the
582    connect() or sendto() call.  When applications use PF_INET6 sockets
583    to accept TCP connections from IPv4 nodes, or receive UDP packets
584    from IPv4 nodes, the system returns the peer's address to the
585    application in the accept(), recvfrom(), or getpeername() call using
586    a sockaddr_in6 structure encoded this way.
587
588    Few applications will likely need to know which type of node they are
589    interoperating with.  However, for those applications that do need to
590    know, the IN6_IS_ADDR_V4MAPPED() macro, defined in Section 6.7, is
591    provided.
592
593 3.8 IPv6 Wildcard Address
594
595    While the bind() function allows applications to select the source IP
596    address of UDP packets and TCP connections, applications often want
597    the system to select the source address for them.  With IPv4, one
598    specifies the address as the symbolic constant INADDR_ANY (called the
599    "wildcard" address) in the bind() call, or simply omits the bind()
600    entirely.
601
602    Since the IPv6 address type is a structure (struct in6_addr), a
603    symbolic constant can be used to initialize an IPv6 address variable,
604    but cannot be used in an assignment.  Therefore systems provide the
605    IPv6 wildcard address in two forms.
606
607    The first version is a global variable named "in6addr_any" that is an
608    in6_addr structure.  The extern declaration for this variable is
609    defined in <netinet/in.h>:
610
611       extern const struct in6_addr in6addr_any;
612
613
614
615
616
617
618 Gilligan, et. al.            Informational                     [Page 11]
619 \f
620 RFC 2553       Basic Socket Interface Extensions for IPv6     March 1999
621
622
623    Applications use in6addr_any similarly to the way they use INADDR_ANY
624    in IPv4.  For example, to bind a socket to port number 23, but let
625    the system select the source address, an application could use the
626    following code:
627
628       struct sockaddr_in6 sin6;
629        . . .
630       sin6.sin6_family = AF_INET6;
631       sin6.sin6_flowinfo = 0;
632       sin6.sin6_port = htons(23);
633       sin6.sin6_addr = in6addr_any;  /* structure assignment */
634        . . .
635       if (bind(s, (struct sockaddr *) &sin6, sizeof(sin6)) == -1)
636               . . .
637
638    The other version is a symbolic constant named IN6ADDR_ANY_INIT and
639    is defined in <netinet/in.h>.  This constant can be used to
640    initialize an in6_addr structure:
641
642       struct in6_addr anyaddr = IN6ADDR_ANY_INIT;
643
644    Note that this constant can be used ONLY at declaration time.  It can
645    not be used to assign a previously declared in6_addr structure.  For
646    example, the following code will not work:
647
648       /* This is the WRONG way to assign an unspecified address */
649       struct sockaddr_in6 sin6;
650        . . .
651       sin6.sin6_addr = IN6ADDR_ANY_INIT; /* will NOT compile */
652
653    Be aware that the IPv4 INADDR_xxx constants are all defined in host
654    byte order but the IPv6 IN6ADDR_xxx constants and the IPv6
655    in6addr_xxx externals are defined in network byte order.
656
657 3.9 IPv6 Loopback Address
658
659    Applications may need to send UDP packets to, or originate TCP
660    connections to, services residing on the local node.  In IPv4, they
661    can do this by using the constant IPv4 address INADDR_LOOPBACK in
662    their connect(), sendto(), or sendmsg() call.
663
664    IPv6 also provides a loopback address to contact local TCP and UDP
665    services.  Like the unspecified address, the IPv6 loopback address is
666    provided in two forms -- a global variable and a symbolic constant.
667
668
669
670
671
672
673
674 Gilligan, et. al.            Informational                     [Page 12]
675 \f
676 RFC 2553       Basic Socket Interface Extensions for IPv6     March 1999
677
678
679    The global variable is an in6_addr structure named
680    "in6addr_loopback."  The extern declaration for this variable is
681    defined in <netinet/in.h>:
682
683       extern const struct in6_addr in6addr_loopback;
684
685    Applications use in6addr_loopback as they would use INADDR_LOOPBACK
686    in IPv4 applications (but beware of the byte ordering difference
687    mentioned at the end of the previous section).  For example, to open
688    a TCP connection to the local telnet server, an application could use
689    the following code:
690
691       struct sockaddr_in6 sin6;
692        . . .
693       sin6.sin6_family = AF_INET6;
694       sin6.sin6_flowinfo = 0;
695       sin6.sin6_port = htons(23);
696       sin6.sin6_addr = in6addr_loopback;  /* structure assignment */
697        . . .
698       if (connect(s, (struct sockaddr *) &sin6, sizeof(sin6)) == -1)
699               . . .
700
701    The symbolic constant is named IN6ADDR_LOOPBACK_INIT and is defined
702    in <netinet/in.h>.  It can be used at declaration time ONLY; for
703    example:
704
705       struct in6_addr loopbackaddr = IN6ADDR_LOOPBACK_INIT;
706
707    Like IN6ADDR_ANY_INIT, this constant cannot be used in an assignment
708    to a previously declared IPv6 address variable.
709
710 3.10 Portability Additions
711
712    One simple addition to the sockets API that can help application
713    writers is the "struct sockaddr_storage". This data structure can
714    simplify writing code portable across multiple address families and
715    platforms.  This data structure is designed with the following goals.
716
717       - It has a large enough implementation specific maximum size to
718         store the desired set of protocol specific socket address data
719         structures. Specifically, it is at least large enough to
720         accommodate sockaddr_in and sockaddr_in6 and possibly other
721         protocol specific socket addresses too.
722       - It is aligned at an appropriate boundary so protocol specific
723         socket address data structure pointers can be cast to it and
724         access their fields without alignment problems. (e.g. pointers
725         to sockaddr_in6 and/or sockaddr_in can be cast to it and access
726         fields without alignment problems).
727
728
729
730 Gilligan, et. al.            Informational                     [Page 13]
731 \f
732 RFC 2553       Basic Socket Interface Extensions for IPv6     March 1999
733
734
735       - It has the initial field(s) isomorphic to the fields of the
736         "struct sockaddr" data structure on that implementation which
737         can be used as a discriminants for deriving the protocol in use.
738         These initial field(s) would on most implementations either be a
739         single field of type "sa_family_t" (isomorphic to sa_family
740         field, 16 bits) or two fields of type uint8_t and sa_family_t
741         respectively, (isomorphic to sa_len and sa_family_t, 8 bits
742         each).
743
744    An example implementation design of such a data structure would be as
745    follows.
746
747 /*
748  * Desired design of maximum size and alignment
749  */
750 #define _SS_MAXSIZE    128  /* Implementation specific max size */
751 #define _SS_ALIGNSIZE  (sizeof (int64_t))
752                          /* Implementation specific desired alignment */
753 /*
754  * Definitions used for sockaddr_storage structure paddings design.
755  */
756 #define _SS_PAD1SIZE   (_SS_ALIGNSIZE - sizeof (sa_family_t))
757 #define _SS_PAD2SIZE   (_SS_MAXSIZE - (sizeof (sa_family_t)+
758                               _SS_PAD1SIZE + _SS_ALIGNSIZE))
759 struct sockaddr_storage {
760     sa_family_t  __ss_family;     /* address family */
761     /* Following fields are implementation specific */
762     char      __ss_pad1[_SS_PAD1SIZE];
763               /* 6 byte pad, this is to make implementation
764               /* specific pad up to alignment field that */
765               /* follows explicit in the data structure */
766     int64_t   __ss_align;     /* field to force desired structure */
767                /* storage alignment */
768     char      __ss_pad2[_SS_PAD2SIZE];
769               /* 112 byte pad to achieve desired size, */
770               /* _SS_MAXSIZE value minus size of ss_family */
771               /* __ss_pad1, __ss_align fields is 112 */
772 };
773
774    On implementations where sockaddr data structure includes a "sa_len",
775    field this data structure would look like this:
776
777 /*
778  * Definitions used for sockaddr_storage structure paddings design.
779  */
780 #define _SS_PAD1SIZE (_SS_ALIGNSIZE -
781                             (sizeof (uint8_t) + sizeof (sa_family_t))
782 #define _SS_PAD2SIZE (_SS_MAXSIZE - (sizeof (sa_family_t)+
783
784
785
786 Gilligan, et. al.            Informational                     [Page 14]
787 \f
788 RFC 2553       Basic Socket Interface Extensions for IPv6     March 1999
789
790
791                               _SS_PAD1SIZE + _SS_ALIGNSIZE))
792 struct sockaddr_storage {
793     uint8_t      __ss_len;        /* address length */
794     sa_family_t  __ss_family;     /* address family */
795     /* Following fields are implementation specific */
796     char         __ss_pad1[_SS_PAD1SIZE];
797                   /* 6 byte pad, this is to make implementation
798                   /* specific pad up to alignment field that */
799                   /* follows explicit in the data structure */
800     int64_t      __ss_align;  /* field to force desired structure */
801                   /* storage alignment */
802     char         __ss_pad2[_SS_PAD2SIZE];
803                   /* 112 byte pad to achieve desired size, */
804                   /* _SS_MAXSIZE value minus size of ss_len, */
805                   /* __ss_family, __ss_pad1, __ss_align fields is 112 */
806 };
807
808    The above example implementation illustrates a data structure which
809    will align on a 64 bit boundary. An implementation specific field
810    "__ss_align" along "__ss_pad1" is used to force a 64-bit alignment
811    which covers proper alignment good enough for needs of sockaddr_in6
812    (IPv6), sockaddr_in (IPv4) address data structures.  The size of
813    padding fields __ss_pad1 depends on the chosen alignment boundary.
814    The size of padding field __ss_pad2 depends on the value of overall
815    size chosen for the total size of the structure. This size and
816    alignment are represented in the above example by implementation
817    specific (not required) constants _SS_MAXSIZE (chosen value 128) and
818    _SS_ALIGNMENT (with chosen value 8).  Constants _SS_PAD1SIZE (derived
819    value 6) and _SS_PAD2SIZE (derived value 112) are also for
820    illustration and not required.  The implementation specific
821    definitions and structure field names above start with an underscore
822    to denote implementation private namespace.  Portable code is not
823    expected to access or reference those fields or constants.
824
825    The sockaddr_storage structure solves the problem of declaring
826    storage for automatic variables which is large enough and aligned
827    enough for storing socket address data structure of any family. For
828    example, code with a file descriptor and without the context of the
829    address family can pass a pointer to a variable of this type where a
830    pointer to a socket address structure is expected in calls such as
831    getpeername() and determine the address family by accessing the
832    received content after the call.
833
834    The sockaddr_storage structure may also be useful and applied to
835    certain other interfaces where a generic socket address large enough
836    and aligned for use with multiple address families may be needed. A
837    discussion of those interfaces is outside the scope of this document.
838
839
840
841
842 Gilligan, et. al.            Informational                     [Page 15]
843 \f
844 RFC 2553       Basic Socket Interface Extensions for IPv6     March 1999
845
846
847    Also, much existing code assumes that any socket address structure
848    can fit in a generic sockaddr structure.  While this has been true
849    for IPv4 socket address structures, it has always been false for Unix
850    domain socket address structures (but in practice this has not been a
851    problem) and it is also false for IPv6 socket address structures
852    (which can be a problem).
853
854    So now an application can do the following:
855
856       struct sockaddr_storage __ss;
857       struct sockaddr_in6 *sin6;
858       sin6 = (struct sockaddr_in6 *) &__ss;
859
860 4. Interface Identification
861
862    This API uses an interface index (a small positive integer) to
863    identify the local interface on which a multicast group is joined
864    (Section 5.3).  Additionally, the advanced API [4] uses these same
865    interface indexes to identify the interface on which a datagram is
866    received, or to specify the interface on which a datagram is to be
867    sent.
868
869    Interfaces are normally known by names such as "le0", "sl1", "ppp2",
870    and the like.  On Berkeley-derived implementations, when an interface
871    is made known to the system, the kernel assigns a unique positive
872    integer value (called the interface index) to that interface.  These
873    are small positive integers that start at 1.  (Note that 0 is never
874    used for an interface index.) There may be gaps so that there is no
875    current interface for a particular positive interface index.
876
877    This API defines two functions that map between an interface name and
878    index, a third function that returns all the interface names and
879    indexes, and a fourth function to return the dynamic memory allocated
880    by the previous function.  How these functions are implemented is
881    left up to the implementation.  4.4BSD implementations can implement
882    these functions using the existing sysctl() function with the
883    NET_RT_IFLIST command.  Other implementations may wish to use ioctl()
884    for this purpose.
885
886 4.1 Name-to-Index
887
888    The first function maps an interface name into its corresponding
889    index.
890
891       #include <net/if.h>
892
893       unsigned int  if_nametoindex(const char *ifname);
894
895
896
897
898 Gilligan, et. al.            Informational                     [Page 16]
899 \f
900 RFC 2553       Basic Socket Interface Extensions for IPv6     March 1999
901
902
903    If the specified interface name does not exist, the return value is
904    0, and errno is set to ENXIO.  If there was a system error (such as
905    running out of memory), the return value is 0 and errno is set to the
906    proper value (e.g., ENOMEM).
907
908 4.2 Index-to-Name
909
910    The second function maps an interface index into its corresponding
911    name.
912
913       #include <net/if.h>
914
915       char  *if_indextoname(unsigned int ifindex, char *ifname);
916
917    The ifname argument must point to a buffer of at least IF_NAMESIZE
918    bytes into which the interface name corresponding to the specified
919    index is returned.  (IF_NAMESIZE is also defined in <net/if.h> and
920    its value includes a terminating null byte at the end of the
921    interface name.) This pointer is also the return value of the
922    function.  If there is no interface corresponding to the specified
923    index, NULL is returned, and errno is set to ENXIO, if there was a
924    system error (such as running out of memory), if_indextoname returns
925    NULL and errno would be set to the proper value (e.g., ENOMEM).
926
927 4.3 Return All Interface Names and Indexes
928
929    The if_nameindex structure holds the information about a single
930    interface and is defined as a result of including the <net/if.h>
931    header.
932
933       struct if_nameindex {
934         unsigned int   if_index;  /* 1, 2, ... */
935         char          *if_name;   /* null terminated name: "le0", ... */
936       };
937
938    The final function returns an array of if_nameindex structures, one
939    structure per interface.
940
941       struct if_nameindex  *if_nameindex(void);
942
943    The end of the array of structures is indicated by a structure with
944    an if_index of 0 and an if_name of NULL.  The function returns a NULL
945    pointer upon an error, and would set errno to the appropriate value.
946
947    The memory used for this array of structures along with the interface
948    names pointed to by the if_name members is obtained dynamically.
949    This memory is freed by the next function.
950
951
952
953
954 Gilligan, et. al.            Informational                     [Page 17]
955 \f
956 RFC 2553       Basic Socket Interface Extensions for IPv6     March 1999
957
958
959 4.4 Free Memory
960
961    The following function frees the dynamic memory that was allocated by
962    if_nameindex().
963
964       #include <net/if.h>
965
966       void  if_freenameindex(struct if_nameindex *ptr);
967
968    The argument to this function must be a pointer that was returned by
969    if_nameindex().
970
971    Currently net/if.h doesn't have prototype definitions for functions
972    and it is recommended that these definitions be defined in net/if.h
973    as well and the struct if_nameindex{}.
974
975 5. Socket Options
976
977    A number of new socket options are defined for IPv6.  All of these
978    new options are at the IPPROTO_IPV6 level.  That is, the "level"
979    parameter in the getsockopt() and setsockopt() calls is IPPROTO_IPV6
980    when using these options.  The constant name prefix IPV6_ is used in
981    all of the new socket options.  This serves to clearly identify these
982    options as applying to IPv6.
983
984    The declaration for IPPROTO_IPV6, the new IPv6 socket options, and
985    related constants defined in this section are obtained by including
986    the header <netinet/in.h>.
987
988 5.1 Unicast Hop Limit
989
990    A new setsockopt() option controls the hop limit used in outgoing
991    unicast IPv6 packets.  The name of this option is IPV6_UNICAST_HOPS,
992    and it is used at the IPPROTO_IPV6 layer.  The following example
993    illustrates how it is used:
994
995       int  hoplimit = 10;
996
997       if (setsockopt(s, IPPROTO_IPV6, IPV6_UNICAST_HOPS,
998                      (char *) &hoplimit, sizeof(hoplimit)) == -1)
999           perror("setsockopt IPV6_UNICAST_HOPS");
1000
1001    When the IPV6_UNICAST_HOPS option is set with setsockopt(), the
1002    option value given is used as the hop limit for all subsequent
1003    unicast packets sent via that socket.  If the option is not set, the
1004    system selects a default value.  The integer hop limit value (called
1005    x) is interpreted as follows:
1006
1007
1008
1009
1010 Gilligan, et. al.            Informational                     [Page 18]
1011 \f
1012 RFC 2553       Basic Socket Interface Extensions for IPv6     March 1999
1013
1014
1015       x < -1:        return an error of EINVAL
1016       x == -1:       use kernel default
1017       0 <= x <= 255: use x
1018       x >= 256:      return an error of EINVAL
1019
1020    The IPV6_UNICAST_HOPS option may be used with getsockopt() to
1021    determine the hop limit value that the system will use for subsequent
1022    unicast packets sent via that socket.  For example:
1023
1024       int  hoplimit;
1025       size_t  len = sizeof(hoplimit);
1026
1027       if (getsockopt(s, IPPROTO_IPV6, IPV6_UNICAST_HOPS,
1028                      (char *) &hoplimit, &len) == -1)
1029           perror("getsockopt IPV6_UNICAST_HOPS");
1030       else
1031           printf("Using %d for hop limit.\n", hoplimit);
1032
1033 5.2 Sending and Receiving Multicast Packets
1034
1035    IPv6 applications may send UDP multicast packets by simply specifying
1036    an IPv6 multicast address in the address argument of the sendto()
1037    function.
1038
1039    Three socket options at the IPPROTO_IPV6 layer control some of the
1040    parameters for sending multicast packets.  Setting these options is
1041    not required: applications may send multicast packets without using
1042    these options.  The setsockopt() options for controlling the sending
1043    of multicast packets are summarized below.  These three options can
1044    also be used with getsockopt().
1045
1046       IPV6_MULTICAST_IF
1047
1048          Set the interface to use for outgoing multicast packets.  The
1049          argument is the index of the interface to use.
1050
1051          Argument type: unsigned int
1052
1053       IPV6_MULTICAST_HOPS
1054
1055          Set the hop limit to use for outgoing multicast packets.  (Note
1056          a separate option - IPV6_UNICAST_HOPS - is provided to set the
1057          hop limit to use for outgoing unicast packets.)
1058
1059          The interpretation of the argument is the same as for the
1060          IPV6_UNICAST_HOPS option:
1061
1062
1063
1064
1065
1066 Gilligan, et. al.            Informational                     [Page 19]
1067 \f
1068 RFC 2553       Basic Socket Interface Extensions for IPv6     March 1999
1069
1070
1071            x < -1:        return an error of EINVAL
1072            x == -1:       use kernel default
1073            0 <= x <= 255: use x
1074            x >= 256:      return an error of EINVAL
1075
1076            If IPV6_MULTICAST_HOPS is not set, the default is 1
1077            (same as IPv4 today)
1078
1079          Argument type: int
1080
1081       IPV6_MULTICAST_LOOP
1082
1083          If a multicast datagram is sent to a group to which the sending
1084          host itself belongs (on the outgoing interface), a copy of the
1085          datagram is looped back by the IP layer for local delivery if
1086          this option is set to 1.  If this option is set to 0 a copy
1087          is not looped back.  Other option values return an error of
1088          EINVAL.
1089
1090          If IPV6_MULTICAST_LOOP is not set, the default is 1 (loopback;
1091          same as IPv4 today).
1092
1093          Argument type: unsigned int
1094
1095    The reception of multicast packets is controlled by the two
1096    setsockopt() options summarized below.  An error of EOPNOTSUPP is
1097    returned if these two options are used with getsockopt().
1098
1099       IPV6_JOIN_GROUP
1100
1101          Join a multicast group on a specified local interface.  If the
1102          interface index is specified as 0, the kernel chooses the local
1103          interface.  For example, some kernels look up the multicast
1104          group in the normal IPv6 routing table and using the resulting
1105          interface.
1106
1107          Argument type: struct ipv6_mreq
1108
1109       IPV6_LEAVE_GROUP
1110
1111          Leave a multicast group on a specified interface.
1112
1113          Argument type: struct ipv6_mreq
1114
1115    The argument type of both of these options is the ipv6_mreq structure,
1116    defined as a result of including the <netinet/in.h> header;
1117
1118
1119
1120
1121
1122 Gilligan, et. al.            Informational                     [Page 20]
1123 \f
1124 RFC 2553       Basic Socket Interface Extensions for IPv6     March 1999
1125
1126
1127    struct ipv6_mreq {
1128        struct in6_addr ipv6mr_multiaddr; /* IPv6 multicast addr */
1129        unsigned int    ipv6mr_interface; /* interface index */
1130    };
1131
1132    Note that to receive multicast datagrams a process must join the
1133    multicast group and bind the UDP port to which datagrams will be
1134    sent.  Some processes also bind the multicast group address to the
1135    socket, in addition to the port, to prevent other datagrams destined
1136    to that same port from being delivered to the socket.
1137
1138 6. Library Functions
1139
1140    New library functions are needed to perform a variety of operations
1141    with IPv6 addresses.  Functions are needed to lookup IPv6 addresses
1142    in the Domain Name System (DNS).  Both forward lookup (nodename-to-
1143    address translation) and reverse lookup (address-to-nodename
1144    translation) need to be supported.  Functions are also needed to
1145    convert IPv6 addresses between their binary and textual form.
1146
1147    We note that the two existing functions, gethostbyname() and
1148    gethostbyaddr(), are left as-is.  New functions are defined to handle
1149    both IPv4 and IPv6 addresses.
1150
1151 6.1 Nodename-to-Address Translation
1152
1153    The commonly used function gethostbyname() is inadequate for many
1154    applications, first because it provides no way for the caller to
1155    specify anything about the types of addresses desired (IPv4 only,
1156    IPv6 only, IPv4-mapped IPv6 are OK, etc.), and second because many
1157    implementations of this function are not thread safe.  RFC 2133
1158    defined a function named gethostbyname2() but this function was also
1159    inadequate, first because its use required setting a global option
1160    (RES_USE_INET6) when IPv6 addresses were required, and second because
1161    a flag argument is needed to provide the caller with additional
1162    control over the types of addresses required.
1163
1164    The following function is new and must be thread safe:
1165
1166    #include <sys/socket.h>
1167    #include <netdb.h>
1168
1169    struct hostent *getipnodebyname(const char *name, int af, int flags
1170                                        int *error_num);
1171
1172    The name argument can be either a node name or a numeric address
1173    string (i.e., a dotted-decimal IPv4 address or an IPv6 hex address).
1174    The af argument specifies the address family, either AF_INET or
1175
1176
1177
1178 Gilligan, et. al.            Informational                     [Page 21]
1179 \f
1180 RFC 2553       Basic Socket Interface Extensions for IPv6     March 1999
1181
1182
1183    AF_INET6. The error_num value is returned to the caller, via a
1184    pointer, with the appropriate error code in error_num, to support
1185    thread safe error code returns.  error_num will be set to one of the
1186    following values:
1187
1188       HOST_NOT_FOUND
1189
1190          No such host is known.
1191
1192       NO_ADDRESS
1193
1194          The server recognised the request and the name but no address is
1195          available.  Another type of request to the name server for the
1196          domain might return an answer.
1197
1198       NO_RECOVERY
1199
1200          An unexpected server failure occurred which cannot be recovered.
1201
1202       TRY_AGAIN
1203
1204          A temporary and possibly transient error occurred, such as a
1205          failure of a server to respond.
1206
1207    The flags argument specifies the types of addresses that are searched
1208    for, and the types of addresses that are returned.  We note that a
1209    special flags value of AI_DEFAULT (defined below) should handle most
1210    applications.
1211
1212    That is, porting simple applications to use IPv6 replaces the call
1213
1214       hptr = gethostbyname(name);
1215
1216    with
1217
1218       hptr = getipnodebyname(name, AF_INET6, AI_DEFAULT, &error_num);
1219
1220    and changes any subsequent error diagnosis code to use error_num
1221    instead of externally declared variables, such as h_errno.
1222
1223    Applications desiring finer control over the types of addresses
1224    searched for and returned, can specify other combinations of the
1225    flags argument.
1226
1227
1228
1229
1230
1231
1232
1233
1234 Gilligan, et. al.            Informational                     [Page 22]
1235 \f
1236 RFC 2553       Basic Socket Interface Extensions for IPv6     March 1999
1237
1238
1239    A flags of 0 implies a strict interpretation of the af argument:
1240
1241       - If flags is 0 and af is AF_INET, then the caller wants only
1242         IPv4 addresses.  A query is made for A records.  If successful,
1243         the IPv4 addresses are returned and the h_length member of the
1244         hostent structure will be 4, else the function returns a NULL
1245         pointer.
1246
1247       -  If flags is 0 and if af is AF_INET6, then the caller wants only
1248         IPv6 addresses.  A query is made for AAAA records.  If
1249         successful, the IPv6 addresses are returned and the h_length
1250         member of the hostent structure will be 16, else the function
1251         returns a NULL pointer.
1252
1253    Other constants can be logically-ORed into the flags argument, to
1254    modify the behavior of the function.
1255
1256       - If the AI_V4MAPPED flag is specified along with an af of
1257         AF_INET6, then the caller will accept IPv4-mapped IPv6
1258         addresses.  That is, if no AAAA records are found then a query
1259         is made for A records and any found are returned as IPv4-mapped
1260         IPv6 addresses (h_length will be 16).  The AI_V4MAPPED flag is
1261         ignored unless af equals AF_INET6.
1262
1263       - The AI_ALL flag is used in conjunction with the AI_V4MAPPED
1264         flag, and is only used with the IPv6 address family.  When AI_ALL
1265         is logically or'd with AI_V4MAPPED flag then the caller wants
1266         all addresses: IPv6 and IPv4-mapped IPv6.  A query is first made
1267         for AAAA records and if successful, the IPv6 addresses are
1268         returned. Another query is then made for A records and any found
1269         are returned as IPv4-mapped IPv6 addresses. h_length will be 16.
1270         Only if both queries fail does the function return a NULL pointer.
1271         This flag is ignored unless af equals AF_INET6.
1272
1273       - The AI_ADDRCONFIG flag specifies that a query for AAAA records
1274         should occur only if the node has at least one IPv6 source
1275         address configured and a query for A records should occur only
1276         if the node has at least one IPv4 source address configured.
1277
1278         For example, if the node has no IPv6 source addresses
1279         configured, and af equals AF_INET6, and the node name being
1280         looked up has both AAAA and A records, then:
1281
1282             (a) if only AI_ADDRCONFIG is specified, the function
1283                 returns a NULL pointer;
1284             (b) if AI_ADDRCONFIG | AI_V4MAPPED is specified, the A
1285                 records are returned as IPv4-mapped IPv6 addresses;
1286
1287
1288
1289
1290 Gilligan, et. al.            Informational                     [Page 23]
1291 \f
1292 RFC 2553       Basic Socket Interface Extensions for IPv6     March 1999
1293
1294
1295    The special flags value of AI_DEFAULT is defined as
1296
1297       #define  AI_DEFAULT  (AI_V4MAPPED | AI_ADDRCONFIG)
1298
1299    We noted that the getipnodebyname() function must allow the name
1300    argument to be either a node name or a literal address string (i.e.,
1301    a dotted-decimal IPv4 address or an IPv6 hex address).  This saves
1302    applications from having to call inet_pton() to handle literal
1303    address strings.
1304
1305    There are four scenarios based on the type of literal address string
1306    and the value of the af argument.
1307
1308    The two simple cases are:
1309
1310    When name is a dotted-decimal IPv4 address and af equals AF_INET, or
1311    when name is an IPv6 hex address and af equals AF_INET6.  The members
1312    of the returned hostent structure are: h_name points to a copy of the
1313    name argument, h_aliases is a NULL pointer, h_addrtype is a copy of
1314    the af argument, h_length is either 4 (for AF_INET) or 16 (for
1315    AF_INET6), h_addr_list[0] is a pointer to the 4-byte or 16-byte
1316    binary address, and h_addr_list[1] is a NULL pointer.
1317
1318    When name is a dotted-decimal IPv4 address and af equals AF_INET6,
1319    and flags equals AI_V4MAPPED, an IPv4-mapped IPv6 address is
1320    returned:  h_name points to an IPv6 hex address containing the IPv4-
1321    mapped IPv6 address, h_aliases is a NULL pointer, h_addrtype is
1322    AF_INET6, h_length is 16, h_addr_list[0] is a pointer to the 16-byte
1323    binary address, and h_addr_list[1] is a NULL pointer.  If AI_V4MAPPED
1324    is set (with or without AI_ALL) return IPv4-mapped otherwise return
1325    NULL.
1326
1327    It is an error when name is an IPv6 hex address and af equals
1328    AF_INET.  The function's return value is a NULL pointer and error_num
1329    equals HOST_NOT_FOUND.
1330
1331 6.2 Address-To-Nodename Translation
1332
1333    The following function has the same arguments as the existing
1334    gethostbyaddr() function, but adds an error number.
1335
1336       #include <sys/socket.h> #include <netdb.h>
1337
1338       struct hostent *getipnodebyaddr(const void *src, size_t len,
1339                                           int af, int *error_num);
1340
1341
1342
1343
1344
1345
1346 Gilligan, et. al.            Informational                     [Page 24]
1347 \f
1348 RFC 2553       Basic Socket Interface Extensions for IPv6     March 1999
1349
1350
1351    As with getipnodebyname(), getipnodebyaddr() must be thread safe.
1352    The error_num value is returned to the caller with the appropriate
1353    error code, to support thread safe error code returns.  The following
1354    error conditions may be returned for error_num:
1355
1356       HOST_NOT_FOUND
1357
1358          No such host is known.
1359
1360       NO_ADDRESS
1361
1362          The server recognized the request and the name but no address
1363          is available.  Another type of request to the name server for
1364          the domain might return an answer.
1365
1366       NO_RECOVERY
1367
1368          An unexpected server failure occurred which cannot be
1369          recovered.
1370
1371       TRY_AGAIN
1372
1373          A temporary and possibly transient error occurred, such as a
1374          failure of a server to respond.
1375
1376    One possible source of confusion is the handling of IPv4-mapped IPv6
1377    addresses and IPv4-compatible IPv6 addresses, but the following logic
1378    should apply.
1379
1380       1.  If af is AF_INET6, and if len equals 16, and if the IPv6
1381           address is an IPv4-mapped IPv6 address or an IPv4-compatible
1382           IPv6 address, then skip over the first 12 bytes of the IPv6
1383           address, set af to AF_INET, and set len to 4.
1384
1385       2.  If af is AF_INET, lookup the name for the given IPv4 address
1386           (e.g., query for a PTR record in the in-addr.arpa domain).
1387
1388       3.  If af is AF_INET6, lookup the name for the given IPv6 address
1389           (e.g., query for a PTR record in the ip6.int domain).
1390
1391       4.  If the function is returning success, then the single address
1392           that is returned in the hostent structure is a copy of the
1393           first argument to the function with the same address family
1394           that was passed as an argument to this function.
1395
1396
1397
1398
1399
1400
1401
1402 Gilligan, et. al.            Informational                     [Page 25]
1403 \f
1404 RFC 2553       Basic Socket Interface Extensions for IPv6     March 1999
1405
1406
1407    All four steps listed are performed, in order.  Also note that the
1408    IPv6 hex addresses "::" and "::1" MUST NOT be treated as IPv4-
1409    compatible addresses, and if the address is "::", HOST_NOT_FOUND MUST
1410    be returned and a query of the address not performed.
1411
1412    Also for the macro in section 6.7 IN6_IS_ADDR_V4COMPAT MUST return
1413    false for "::" and "::1".
1414
1415 6.3 Freeing memory for getipnodebyname and getipnodebyaddr
1416
1417    The hostent structure does not change from its existing definition.
1418    This structure, and the information pointed to by this structure, are
1419    dynamically allocated by getipnodebyname and getipnodebyaddr.  The
1420    following function frees this memory:
1421
1422       #include <netdb.h>
1423
1424       void freehostent(struct hostent *ptr);
1425
1426 6.4 Protocol-Independent Nodename and Service Name Translation
1427
1428    Nodename-to-address translation is done in a protocol-independent
1429    fashion using the getaddrinfo() function that is taken from the
1430    Institute of Electrical and Electronic Engineers (IEEE) POSIX 1003.1g
1431    (Protocol Independent Interfaces) draft specification [3].
1432
1433    The official specification for this function will be the final POSIX
1434    standard, with the following additional requirements:
1435
1436       -  getaddrinfo() (along with the getnameinfo() function described
1437          in the next section) must be thread safe.
1438
1439       -  The AI_NUMERICHOST is new with this document.
1440
1441       -  All fields in socket address structures returned by
1442          getaddrinfo() that are not filled in through an explicit
1443          argument (e.g., sin6_flowinfo and sin_zero) must be set to 0.
1444          (This makes it easier to compare socket address structures.)
1445
1446       -  getaddrinfo() must fill in the length field of a socket address
1447          structure (e.g., sin6_len) on systems that support this field.
1448
1449    We are providing this independent description of the function because
1450    POSIX standards are not freely available (as are IETF documents).
1451
1452       #include <sys/socket.h>
1453       #include <netdb.h>
1454
1455
1456
1457
1458 Gilligan, et. al.            Informational                     [Page 26]
1459 \f
1460 RFC 2553       Basic Socket Interface Extensions for IPv6     March 1999
1461
1462
1463       int getaddrinfo(const char *nodename, const char *servname,
1464                       const struct addrinfo *hints,
1465                       struct addrinfo **res);
1466
1467    The addrinfo structure is defined as a result of including the
1468    <netdb.h> header.
1469
1470   struct addrinfo {
1471     int     ai_flags;     /* AI_PASSIVE, AI_CANONNAME, AI_NUMERICHOST */
1472     int     ai_family;    /* PF_xxx */
1473     int     ai_socktype;  /* SOCK_xxx */
1474     int     ai_protocol;  /* 0 or IPPROTO_xxx for IPv4 and IPv6 */
1475     size_t  ai_addrlen;   /* length of ai_addr */
1476     char   *ai_canonname; /* canonical name for nodename */
1477     struct sockaddr  *ai_addr; /* binary address */
1478     struct addrinfo  *ai_next; /* next structure in linked list */
1479   };
1480
1481    The return value from the function is 0 upon success or a nonzero
1482    error code.  The following names are the nonzero error codes from
1483    getaddrinfo(), and are defined in <netdb.h>:
1484
1485       EAI_ADDRFAMILY  address family for nodename not supported
1486       EAI_AGAIN       temporary failure in name resolution
1487       EAI_BADFLAGS    invalid value for ai_flags
1488       EAI_FAIL        non-recoverable failure in name resolution
1489       EAI_FAMILY      ai_family not supported
1490       EAI_MEMORY      memory allocation failure
1491       EAI_NODATA      no address associated with nodename
1492       EAI_NONAME      nodename nor servname provided, or not known
1493       EAI_SERVICE     servname not supported for ai_socktype
1494       EAI_SOCKTYPE    ai_socktype not supported
1495       EAI_SYSTEM      system error returned in errno
1496
1497    The nodename and servname arguments are pointers to null-terminated
1498    strings or NULL.  One or both of these two arguments must be a non-
1499    NULL pointer.  In the normal client scenario, both the nodename and
1500    servname are specified.  In the normal server scenario, only the
1501    servname is specified.  A non-NULL nodename string can be either a
1502    node name or a numeric host address string (i.e., a dotted-decimal
1503    IPv4 address or an IPv6 hex address).  A non-NULL servname string can
1504    be either a service name or a decimal port number.
1505
1506    The caller can optionally pass an addrinfo structure, pointed to by
1507    the third argument, to provide hints concerning the type of socket
1508    that the caller supports.  In this hints structure all members other
1509    than ai_flags, ai_family, ai_socktype, and ai_protocol must be zero
1510    or a NULL pointer.  A value of PF_UNSPEC for ai_family means the
1511
1512
1513
1514 Gilligan, et. al.            Informational                     [Page 27]
1515 \f
1516 RFC 2553       Basic Socket Interface Extensions for IPv6     March 1999
1517
1518
1519    caller will accept any protocol family.  A value of 0 for ai_socktype
1520    means the caller will accept any socket type.  A value of 0 for
1521    ai_protocol means the caller will accept any protocol.  For example,
1522    if the caller handles only TCP and not UDP, then the ai_socktype
1523    member of the hints structure should be set to SOCK_STREAM when
1524    getaddrinfo() is called.  If the caller handles only IPv4 and not
1525    IPv6, then the ai_family member of the hints structure should be set
1526    to PF_INET when getaddrinfo() is called.  If the third argument to
1527    getaddrinfo() is a NULL pointer, this is the same as if the caller
1528    had filled in an addrinfo structure initialized to zero with
1529    ai_family set to PF_UNSPEC.
1530
1531    Upon successful return a pointer to a linked list of one or more
1532    addrinfo structures is returned through the final argument.  The
1533    caller can process each addrinfo structure in this list by following
1534    the ai_next pointer, until a NULL pointer is encountered.  In each
1535    returned addrinfo structure the three members ai_family, ai_socktype,
1536    and ai_protocol are the corresponding arguments for a call to the
1537    socket() function.  In each addrinfo structure the ai_addr member
1538    points to a filled-in socket address structure whose length is
1539    specified by the ai_addrlen member.
1540
1541    If the AI_PASSIVE bit is set in the ai_flags member of the hints
1542    structure, then the caller plans to use the returned socket address
1543    structure in a call to bind().  In this case, if the nodename
1544    argument is a NULL pointer, then the IP address portion of the socket
1545    address structure will be set to INADDR_ANY for an IPv4 address or
1546    IN6ADDR_ANY_INIT for an IPv6 address.
1547
1548    If the AI_PASSIVE bit is not set in the ai_flags member of the hints
1549    structure, then the returned socket address structure will be ready
1550    for a call to connect() (for a connection-oriented protocol) or
1551    either connect(), sendto(), or sendmsg() (for a connectionless
1552    protocol).  In this case, if the nodename argument is a NULL pointer,
1553    then the IP address portion of the socket address structure will be
1554    set to the loopback address.
1555
1556    If the AI_CANONNAME bit is set in the ai_flags member of the hints
1557    structure, then upon successful return the ai_canonname member of the
1558    first addrinfo structure in the linked list will point to a null-
1559    terminated string containing the canonical name of the specified
1560    nodename.
1561
1562    If the AI_NUMERICHOST bit is set in the ai_flags member of the hints
1563    structure, then a non-NULL nodename string must be a numeric host
1564    address string.  Otherwise an error of EAI_NONAME is returned.  This
1565    flag prevents any type of name resolution service (e.g., the DNS)
1566    from being called.
1567
1568
1569
1570 Gilligan, et. al.            Informational                     [Page 28]
1571 \f
1572 RFC 2553       Basic Socket Interface Extensions for IPv6     March 1999
1573
1574
1575    All of the information returned by getaddrinfo() is dynamically
1576    allocated: the addrinfo structures, and the socket address structures
1577    and canonical node name strings pointed to by the addrinfo
1578    structures.  To return this information to the system the function
1579    freeaddrinfo() is called:
1580
1581       #include <sys/socket.h> #include <netdb.h>
1582
1583       void freeaddrinfo(struct addrinfo *ai);
1584
1585    The addrinfo structure pointed to by the ai argument is freed, along
1586    with any dynamic storage pointed to by the structure.  This operation
1587    is repeated until a NULL ai_next pointer is encountered.
1588
1589    To aid applications in printing error messages based on the EAI_xxx
1590    codes returned by getaddrinfo(), the following function is defined.
1591
1592       #include <sys/socket.h> #include <netdb.h>
1593
1594       char *gai_strerror(int ecode);
1595
1596    The argument is one of the EAI_xxx values defined earlier and the
1597    return value points to a string describing the error.  If the
1598    argument is not one of the EAI_xxx values, the function still returns
1599    a pointer to a string whose contents indicate an unknown error.
1600
1601 6.5 Socket Address Structure to Nodename and Service Name
1602
1603    The POSIX 1003.1g specification includes no function to perform the
1604    reverse conversion from getaddrinfo(): to look up a nodename and
1605    service name, given the binary address and port.  Therefore, we
1606    define the following function:
1607
1608       #include <sys/socket.h>
1609       #include <netdb.h>
1610
1611       int getnameinfo(const struct sockaddr *sa, socklen_t salen,
1612                       char *host, size_t hostlen,
1613                       char *serv, size_t servlen,
1614                       int flags);
1615
1616    This function looks up an IP address and port number provided by the
1617    caller in the DNS and system-specific database, and returns text
1618    strings for both in buffers provided by the caller.  The function
1619    indicates successful completion by a zero return value; a non-zero
1620    return value indicates failure.
1621
1622
1623
1624
1625
1626 Gilligan, et. al.            Informational                     [Page 29]
1627 \f
1628 RFC 2553       Basic Socket Interface Extensions for IPv6     March 1999
1629
1630
1631    The first argument, sa, points to either a sockaddr_in structure (for
1632    IPv4) or a sockaddr_in6 structure (for IPv6) that holds the IP
1633    address and port number.  The salen argument gives the length of the
1634    sockaddr_in or sockaddr_in6 structure.
1635
1636    The function returns the nodename associated with the IP address in
1637    the buffer pointed to by the host argument.  The caller provides the
1638    size of this buffer via the hostlen argument.  The service name
1639    associated with the port number is returned in the buffer pointed to
1640    by serv, and the servlen argument gives the length of this buffer.
1641    The caller specifies not to return either string by providing a zero
1642    value for the hostlen or servlen arguments.  Otherwise, the caller
1643    must provide buffers large enough to hold the nodename and the
1644    service name, including the terminating null characters.
1645
1646    Unfortunately most systems do not provide constants that specify the
1647    maximum size of either a fully-qualified domain name or a service
1648    name.  Therefore to aid the application in allocating buffers for
1649    these two returned strings the following constants are defined in
1650    <netdb.h>:
1651
1652       #define NI_MAXHOST  1025
1653       #define NI_MAXSERV    32
1654
1655    The first value is actually defined as the constant MAXDNAME in recent
1656    versions of BIND's <arpa/nameser.h> header (older versions of BIND
1657    define this constant to be 256) and the second is a guess based on the
1658    services listed in the current Assigned Numbers RFC.
1659
1660    The final argument is a flag that changes the default actions of this
1661    function.  By default the fully-qualified domain name (FQDN) for the
1662    host is looked up in the DNS and returned.  If the flag bit NI_NOFQDN
1663    is set, only the nodename portion of the FQDN is returned for local
1664    hosts.
1665
1666    If the flag bit NI_NUMERICHOST is set, or if the host's name cannot be
1667    located in the DNS, the numeric form of the host's address is returned
1668    instead of its name (e.g., by calling inet_ntop() instead of
1669    getipnodebyaddr()).  If the flag bit NI_NAMEREQD is set, an error is
1670    returned if the host's name cannot be located in the DNS.
1671
1672    If the flag bit NI_NUMERICSERV is set, the numeric form of the service
1673    address is returned (e.g., its port number) instead of its name.  The
1674    two NI_NUMERICxxx flags are required to support the "-n" flag that
1675    many commands provide.
1676
1677
1678
1679
1680
1681
1682 Gilligan, et. al.            Informational                     [Page 30]
1683 \f
1684 RFC 2553       Basic Socket Interface Extensions for IPv6     March 1999
1685
1686
1687    A fifth flag bit, NI_DGRAM, specifies that the service is a datagram
1688    service, and causes getservbyport() to be called with a second
1689    argument of "udp" instead of its default of "tcp".  This is required
1690    for the few ports (e.g. 512-514) that have different services for UDP
1691    and TCP.
1692
1693    These NI_xxx flags are defined in <netdb.h> along with the AI_xxx
1694    flags already defined for getaddrinfo().
1695
1696 6.6 Address Conversion Functions
1697
1698    The two functions inet_addr() and inet_ntoa() convert an IPv4 address
1699    between binary and text form.  IPv6 applications need similar
1700    functions.  The following two functions convert both IPv6 and IPv4
1701    addresses:
1702
1703       #include <sys/socket.h>
1704       #include <arpa/inet.h>
1705
1706       int inet_pton(int af, const char *src, void *dst);
1707
1708       const char *inet_ntop(int af, const void *src,
1709                             char *dst, size_t size);
1710
1711    The inet_pton() function converts an address in its standard text
1712    presentation form into its numeric binary form.  The af argument
1713    specifies the family of the address.  Currently the AF_INET and
1714    AF_INET6 address families are supported.  The src argument points to
1715    the string being passed in.  The dst argument points to a buffer into
1716    which the function stores the numeric address.  The address is
1717    returned in network byte order.  Inet_pton() returns 1 if the
1718    conversion succeeds, 0 if the input is not a valid IPv4 dotted-
1719    decimal string or a valid IPv6 address string, or -1 with errno set
1720    to EAFNOSUPPORT if the af argument is unknown.  The calling
1721    application must ensure that the buffer referred to by dst is large
1722    enough to hold the numeric address (e.g., 4 bytes for AF_INET or 16
1723    bytes for AF_INET6).
1724
1725    If the af argument is AF_INET, the function accepts a string in the
1726    standard IPv4 dotted-decimal form:
1727
1728       ddd.ddd.ddd.ddd
1729
1730    where ddd is a one to three digit decimal number between 0 and 255.
1731    Note that many implementations of the existing inet_addr() and
1732    inet_aton() functions accept nonstandard input: octal numbers,
1733    hexadecimal numbers, and fewer than four numbers.  inet_pton() does
1734    not accept these formats.
1735
1736
1737
1738 Gilligan, et. al.            Informational                     [Page 31]
1739 \f
1740 RFC 2553       Basic Socket Interface Extensions for IPv6     March 1999
1741
1742
1743    If the af argument is AF_INET6, then the function accepts a string in
1744    one of the standard IPv6 text forms defined in Section 2.2 of the
1745    addressing architecture specification [2].
1746
1747    The inet_ntop() function converts a numeric address into a text
1748    string suitable for presentation.  The af argument specifies the
1749    family of the address.  This can be AF_INET or AF_INET6.  The src
1750    argument points to a buffer holding an IPv4 address if the af
1751    argument is AF_INET, or an IPv6 address if the af argument is
1752    AF_INET6, the address must be in network byte order.  The dst
1753    argument points to a buffer where the function will store the
1754    resulting text string.  The size argument specifies the size of this
1755    buffer.  The application must specify a non-NULL dst argument.  For
1756    IPv6 addresses, the buffer must be at least 46-octets.  For IPv4
1757    addresses, the buffer must be at least 16-octets.  In order to allow
1758    applications to easily declare buffers of the proper size to store
1759    IPv4 and IPv6 addresses in string form, the following two constants
1760    are defined in <netinet/in.h>:
1761
1762       #define INET_ADDRSTRLEN    16
1763       #define INET6_ADDRSTRLEN   46
1764
1765    The inet_ntop() function returns a pointer to the buffer containing
1766    the text string if the conversion succeeds, and NULL otherwise.  Upon
1767    failure, errno is set to EAFNOSUPPORT if the af argument is invalid or
1768    ENOSPC if the size of the result buffer is inadequate.
1769
1770 6.7 Address Testing Macros
1771
1772    The following macros can be used to test for special IPv6 addresses.
1773
1774       #include <netinet/in.h>
1775
1776       int  IN6_IS_ADDR_UNSPECIFIED (const struct in6_addr *);
1777       int  IN6_IS_ADDR_LOOPBACK    (const struct in6_addr *);
1778       int  IN6_IS_ADDR_MULTICAST   (const struct in6_addr *);
1779       int  IN6_IS_ADDR_LINKLOCAL   (const struct in6_addr *);
1780       int  IN6_IS_ADDR_SITELOCAL   (const struct in6_addr *);
1781       int  IN6_IS_ADDR_V4MAPPED    (const struct in6_addr *);
1782       int  IN6_IS_ADDR_V4COMPAT    (const struct in6_addr *);
1783
1784       int  IN6_IS_ADDR_MC_NODELOCAL(const struct in6_addr *);
1785       int  IN6_IS_ADDR_MC_LINKLOCAL(const struct in6_addr *);
1786       int  IN6_IS_ADDR_MC_SITELOCAL(const struct in6_addr *);
1787       int  IN6_IS_ADDR_MC_ORGLOCAL (const struct in6_addr *);
1788       int  IN6_IS_ADDR_MC_GLOBAL   (const struct in6_addr *);
1789
1790
1791
1792
1793
1794 Gilligan, et. al.            Informational                     [Page 32]
1795 \f
1796 RFC 2553       Basic Socket Interface Extensions for IPv6     March 1999
1797
1798
1799    The first seven macros return true if the address is of the specified
1800    type, or false otherwise.  The last five test the scope of a
1801    multicast address and return true if the address is a multicast
1802    address of the specified scope or false if the address is either not
1803    a multicast address or not of the specified scope.  Note that
1804    IN6_IS_ADDR_LINKLOCAL and IN6_IS_ADDR_SITELOCAL return true only for
1805    the two local-use IPv6 unicast addresses.  These two macros do not
1806    return true for IPv6 multicast addresses of either link-local scope
1807    or site-local scope.
1808
1809 7. Summary of New Definitions
1810
1811    The following list summarizes the constants, structure, and extern
1812    definitions discussed in this memo, sorted by header.
1813
1814       <net/if.h>      IF_NAMESIZE
1815       <net/if.h>      struct if_nameindex{};
1816
1817       <netdb.h>       AI_ADDRCONFIG
1818       <netdb.h>       AI_DEFAULT
1819       <netdb.h>       AI_ALL
1820       <netdb.h>       AI_CANONNAME
1821       <netdb.h>       AI_NUMERICHOST
1822       <netdb.h>       AI_PASSIVE
1823       <netdb.h>       AI_V4MAPPED
1824       <netdb.h>       EAI_ADDRFAMILY
1825       <netdb.h>       EAI_AGAIN
1826       <netdb.h>       EAI_BADFLAGS
1827       <netdb.h>       EAI_FAIL
1828       <netdb.h>       EAI_FAMILY
1829       <netdb.h>       EAI_MEMORY
1830       <netdb.h>       EAI_NODATA
1831       <netdb.h>       EAI_NONAME
1832       <netdb.h>       EAI_SERVICE
1833       <netdb.h>       EAI_SOCKTYPE
1834       <netdb.h>       EAI_SYSTEM
1835       <netdb.h>       NI_DGRAM
1836       <netdb.h>       NI_MAXHOST
1837       <netdb.h>       NI_MAXSERV
1838       <netdb.h>       NI_NAMEREQD
1839       <netdb.h>       NI_NOFQDN
1840       <netdb.h>       NI_NUMERICHOST
1841       <netdb.h>       NI_NUMERICSERV
1842       <netdb.h>       struct addrinfo{};
1843
1844       <netinet/in.h>  IN6ADDR_ANY_INIT
1845       <netinet/in.h>  IN6ADDR_LOOPBACK_INIT
1846       <netinet/in.h>  INET6_ADDRSTRLEN
1847
1848
1849
1850 Gilligan, et. al.            Informational                     [Page 33]
1851 \f
1852 RFC 2553       Basic Socket Interface Extensions for IPv6     March 1999
1853
1854
1855       <netinet/in.h>  INET_ADDRSTRLEN
1856       <netinet/in.h>  IPPROTO_IPV6
1857       <netinet/in.h>  IPV6_JOIN_GROUP
1858       <netinet/in.h>  IPV6_LEAVE_GROUP
1859       <netinet/in.h>  IPV6_MULTICAST_HOPS
1860       <netinet/in.h>  IPV6_MULTICAST_IF
1861       <netinet/in.h>  IPV6_MULTICAST_LOOP
1862       <netinet/in.h>  IPV6_UNICAST_HOPS
1863       <netinet/in.h>  SIN6_LEN
1864       <netinet/in.h>  extern const struct in6_addr in6addr_any;
1865       <netinet/in.h>  extern const struct in6_addr in6addr_loopback;
1866       <netinet/in.h>  struct in6_addr{};
1867       <netinet/in.h>  struct ipv6_mreq{};
1868       <netinet/in.h>  struct sockaddr_in6{};
1869
1870       <sys/socket.h>  AF_INET6
1871       <sys/socket.h>  PF_INET6
1872       <sys/socket.h>  struct sockaddr_storage;
1873
1874    The following list summarizes the function and macro prototypes
1875    discussed in this memo, sorted by header.
1876
1877 <arpa/inet.h>   int inet_pton(int, const char *, void *);
1878 <arpa/inet.h>   const char *inet_ntop(int, const void *,
1879                                       char *, size_t);
1880
1881 <net/if.h>      char *if_indextoname(unsigned int, char *);
1882 <net/if.h>      unsigned int if_nametoindex(const char *);
1883 <net/if.h>      void if_freenameindex(struct if_nameindex *);
1884 <net/if.h>      struct if_nameindex *if_nameindex(void);
1885
1886 <netdb.h>       int getaddrinfo(const char *, const char *,
1887                                 const struct addrinfo *,
1888                                 struct addrinfo **);
1889 <netdb.h>       int getnameinfo(const struct sockaddr *, socklen_t,
1890                                 char *, size_t, char *, size_t, int);
1891 <netdb.h>       void freeaddrinfo(struct addrinfo *);
1892 <netdb.h>       char *gai_strerror(int);
1893 <netdb.h>       struct hostent *getipnodebyname(const char *, int, int,
1894                                        int *);
1895 <netdb.h>       struct hostent *getipnodebyaddr(const void *, size_t,
1896                                        int, int *);
1897 <netdb.h>       void freehostent(struct hostent *);
1898
1899 <netinet/in.h>  int IN6_IS_ADDR_LINKLOCAL(const struct in6_addr *);
1900 <netinet/in.h>  int IN6_IS_ADDR_LOOPBACK(const struct in6_addr *);
1901 <netinet/in.h>  int IN6_IS_ADDR_MC_GLOBAL(const struct in6_addr *);
1902 <netinet/in.h>  int IN6_IS_ADDR_MC_LINKLOCAL(const struct in6_addr *);
1903
1904
1905
1906 Gilligan, et. al.            Informational                     [Page 34]
1907 \f
1908 RFC 2553       Basic Socket Interface Extensions for IPv6     March 1999
1909
1910
1911 <netinet/in.h>  int IN6_IS_ADDR_MC_NODELOCAL(const struct in6_addr *);
1912 <netinet/in.h>  int IN6_IS_ADDR_MC_ORGLOCAL(const struct in6_addr *);
1913 <netinet/in.h>  int IN6_IS_ADDR_MC_SITELOCAL(const struct in6_addr *);
1914 <netinet/in.h>  int IN6_IS_ADDR_MULTICAST(const struct in6_addr *);
1915 <netinet/in.h>  int IN6_IS_ADDR_SITELOCAL(const struct in6_addr *);
1916 <netinet/in.h>  int IN6_IS_ADDR_UNSPECIFIED(const struct in6_addr *);
1917 <netinet/in.h>  int IN6_IS_ADDR_V4COMPAT(const struct in6_addr *);
1918 <netinet/in.h>  int IN6_IS_ADDR_V4MAPPED(const struct in6_addr *);
1919
1920 8. Security Considerations
1921
1922    IPv6 provides a number of new security mechanisms, many of which need
1923    to be accessible to applications.  Companion memos detailing the
1924    extensions to the socket interfaces to support IPv6 security are
1925    being written.
1926
1927 9. Year 2000 Considerations
1928
1929    There are no issues for this memo concerning the Year 2000 issue
1930    regarding the use of dates.
1931
1932 Changes From RFC 2133
1933
1934    Changes made in the March 1998 Edition (-01 draft):
1935
1936       Changed all "hostname" to "nodename" for consistency with other
1937       IPv6 documents.
1938
1939       Section 3.3: changed comment for sin6_flowinfo to be "traffic
1940       class & flow info" and updated corresponding text description to
1941       current definition of these two fields.
1942
1943       Section 3.10 ("Portability Additions") is new.
1944
1945       Section 6: a new paragraph was added reiterating that the existing
1946       gethostbyname() and gethostbyaddr() are not changed.
1947
1948       Section 6.1: change gethostbyname3() to getnodebyname().  Add
1949       AI_DEFAULT to handle majority of applications.  Renamed
1950       AI_V6ADDRCONFIG to AI_ADDRCONFIG and define it for A records and
1951       IPv4 addresses too.  Defined exactly what getnodebyname() must
1952       return if the name argument is a numeric address string.
1953
1954       Section 6.2: change gethostbyaddr() to getnodebyaddr().  Reword
1955       items 2 and 3 in the description of how to handle IPv4-mapped and
1956       IPv4- compatible addresses to "lookup a name" for a given address,
1957       instead of specifying what type of DNS query to issue.
1958
1959
1960
1961
1962 Gilligan, et. al.            Informational                     [Page 35]
1963 \f
1964 RFC 2553       Basic Socket Interface Extensions for IPv6     March 1999
1965
1966
1967       Section 6.3: added two more requirements to getaddrinfo().
1968
1969       Section 7: added the following constants to the list for
1970       <netdb.h>:  AI_ADDRCONFIG, AI_ALL, and AI_V4MAPPED.  Add union
1971       sockaddr_union and SA_LEN to the lists for <sys/socket.h>.
1972
1973       Updated references.
1974
1975    Changes made in the November 1997 Edition (-00 draft):
1976
1977       The data types have been changed to conform with Draft 6.6 of the
1978       Posix 1003.1g standard.
1979
1980       Section 3.2: data type of s6_addr changed to "uint8_t".
1981
1982       Section 3.3: data type of sin6_family changed to "sa_family_t".
1983       data type of sin6_port changed to "in_port_t", data type of
1984       sin6_flowinfo changed to "uint32_t".
1985
1986       Section 3.4: same as Section 3.3, plus data type of sin6_len
1987       changed to "uint8_t".
1988
1989       Section 6.2: first argument of gethostbyaddr() changed from "const
1990       char *" to "const void *" and second argument changed from "int"
1991       to "size_t".
1992
1993       Section 6.4: second argument of getnameinfo() changed from
1994       "size_t" to "socklen_t".
1995
1996       The wording was changed when new structures were defined, to be
1997       more explicit as to which header must be included to define the
1998       structure:
1999
2000       Section 3.2 (in6_addr{}), Section 3.3 (sockaddr_in6{}), Section
2001       3.4 (sockaddr_in6{}), Section 4.3 (if_nameindex{}), Section 5.3
2002       (ipv6_mreq{}), and Section 6.3 (addrinfo{}).
2003
2004       Section 4: NET_RT_LIST changed to NET_RT_IFLIST.
2005
2006       Section 5.1: The IPV6_ADDRFORM socket option was removed.
2007
2008       Section 5.3: Added a note that an option value other than 0 or 1
2009       for IPV6_MULTICAST_LOOP returns an error.  Added a note that
2010       IPV6_MULTICAST_IF, IPV6_MULTICAST_HOPS, and IPV6_MULTICAST_LOOP
2011       can also be used with getsockopt(), but IPV6_ADD_MEMBERSHIP and
2012       IPV6_DROP_MEMBERSHIP cannot be used with getsockopt().
2013
2014
2015
2016
2017
2018 Gilligan, et. al.            Informational                     [Page 36]
2019 \f
2020 RFC 2553       Basic Socket Interface Extensions for IPv6     March 1999
2021
2022
2023       Section 6.1: Removed the description of gethostbyname2() and its
2024       associated RES_USE_INET6 option, replacing it with
2025       gethostbyname3().
2026
2027       Section 6.2: Added requirement that gethostbyaddr() be thread
2028       safe.  Reworded step 4 to avoid using the RES_USE_INET6 option.
2029
2030       Section 6.3: Added the requirement that getaddrinfo() and
2031       getnameinfo() be thread safe.  Added the AI_NUMERICHOST flag.
2032
2033       Section 6.6: Added clarification about IN6_IS_ADDR_LINKLOCAL and
2034       IN6_IS_ADDR_SITELOCAL macros.
2035
2036    Changes made to the draft -01 specification Sept 98
2037
2038       Changed priority to traffic class in the spec.
2039
2040       Added the need for scope identification in section 2.1.
2041
2042       Added sin6_scope_id to struct sockaddr_in6 in sections 3.3 and
2043       3.4.
2044
2045       Changed 3.10 to use generic storage structure to support holding
2046       IPv6 addresses and removed the SA_LEN macro.
2047
2048       Distinguished between invalid input parameters and system failures
2049       for Interface Identification in Section 4.1 and 4.2.
2050
2051       Added defaults for multicast operations in section 5.2 and changed
2052       the names from ADD to JOIN and DROP to LEAVE to be consistent with
2053       IPv6 multicast terminology.
2054
2055       Changed getnodebyname to getipnodebyname, getnodebyaddr to
2056       getipnodebyaddr, and added MT safe error code to function
2057       parameters in section 6.
2058
2059       Moved freehostent to its own sub-section after getipnodebyaddr now
2060       6.3 (so this bumps all remaining sections in section 6.
2061
2062       Clarified the use of AI_ALL and AI_V4MAPPED that these are
2063       dependent on the AF parameter and must be used as a conjunction in
2064       section 6.1.
2065
2066       Removed the restriction that literal addresses cannot be used with
2067       a flags argument in section 6.1.
2068
2069       Added Year 2000 Section to the draft
2070
2071
2072
2073
2074 Gilligan, et. al.            Informational                     [Page 37]
2075 \f
2076 RFC 2553       Basic Socket Interface Extensions for IPv6     March 1999
2077
2078
2079       Deleted Reference to the following because the attached is deleted
2080       from the ID directory and has expired.  But the logic from the
2081       aforementioned draft still applies, so that was kept in Section
2082       6.2 bullets after 3rd paragraph.
2083
2084       [7]  P. Vixie, "Reverse Name Lookups of Encapsulated IPv4
2085            Addresses in IPv6", Internet-Draft, <draft-vixie-ipng-
2086            ipv4ptr-00.txt>, May 1996.
2087
2088       Deleted the following reference as it is no longer referenced.
2089       And the draft has expired.
2090
2091       [3]  D. McDonald, "A Simple IP Security API Extension to BSD
2092            Sockets", Internet-Draft, <draft-mcdonald-simple-ipsec-api-
2093            01.txt>, March 1997.
2094
2095       Deleted the following reference as it is no longer referenced.
2096
2097       [4]  C. Metz, "Network Security API for Sockets",
2098            Internet-Draft, <draft-metz-net-security-api-01.txt>, January
2099            1998.
2100
2101       Update current references to current status.
2102
2103       Added alignment notes for in6_addr and sin6_addr.
2104
2105       Clarified further that AI_V4MAPPED must be used with a dotted IPv4
2106       literal address for getipnodebyname(), when address family is
2107       AF_INET6.
2108
2109       Added text to clarify "::" and "::1" when used by
2110       getipnodebyaddr().
2111
2112 Acknowledgments
2113
2114    Thanks to the many people who made suggestions and provided feedback
2115    to this document, including: Werner Almesberger, Ran Atkinson, Fred
2116    Baker, Dave Borman, Andrew Cherenson, Alex Conta, Alan Cox, Steve
2117    Deering, Richard Draves, Francis Dupont, Robert Elz, Marc Hasson, Tom
2118    Herbert, Bob Hinden, Wan-Yen Hsu, Christian Huitema, Koji Imada,
2119    Markus Jork, Ron Lee, Alan Lloyd, Charles Lynn, Dan McDonald, Dave
2120    Mitton, Thomas Narten, Josh Osborne, Craig Partridge, Jean-Luc
2121    Richier, Erik Scoredos, Keith Sklower, Matt Thomas, Harvey Thompson,
2122    Dean D. Throop, Karen Tracey, Glenn Trewitt, Paul Vixie, David
2123    Waitzman, Carl Williams, and Kazu Yamamoto,
2124
2125
2126
2127
2128
2129
2130 Gilligan, et. al.            Informational                     [Page 38]
2131 \f
2132 RFC 2553       Basic Socket Interface Extensions for IPv6     March 1999
2133
2134
2135    The getaddrinfo() and getnameinfo() functions are taken from an
2136    earlier Internet Draft by Keith Sklower.  As noted in that draft,
2137    William Durst, Steven Wise, Michael Karels, and Eric Allman provided
2138    many useful discussions on the subject of protocol-independent name-
2139    to-address translation, and reviewed early versions of Keith
2140    Sklower's original proposal.  Eric Allman implemented the first
2141    prototype of getaddrinfo().  The observation that specifying the pair
2142    of name and service would suffice for connecting to a service
2143    independent of protocol details was made by Marshall Rose in a
2144    proposal to X/Open for a "Uniform Network Interface".
2145
2146    Craig Metz, Jack McCann, Erik Nordmark, Tim Hartrick, and Mukesh
2147    Kacker made many contributions to this document.  Ramesh Govindan
2148    made a number of contributions and co-authored an earlier version of
2149    this memo.
2150
2151 References
2152
2153    [1]  Deering, S. and R. Hinden, "Internet Protocol, Version 6 (IPv6)
2154         Specification", RFC 2460, December 1998.
2155
2156    [2]  Hinden, R. and S. Deering, "IP Version 6 Addressing
2157         Architecture", RFC 2373, July 1998.
2158
2159    [3]  IEEE, "Protocol Independent Interfaces", IEEE Std 1003.1g, DRAFT
2160         6.6, March 1997.
2161
2162    [4]  Stevens, W. and M. Thomas, "Advanced Sockets API for IPv6", RFC
2163         2292, February 1998.
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186 Gilligan, et. al.            Informational                     [Page 39]
2187 \f
2188 RFC 2553       Basic Socket Interface Extensions for IPv6     March 1999
2189
2190
2191 Authors' Addresses
2192
2193    Robert E. Gilligan
2194    FreeGate Corporation
2195    1208 E. Arques Ave.
2196    Sunnyvale, CA 94086
2197
2198    Phone: +1 408 617 1004
2199    EMail: gilligan@freegate.com
2200
2201
2202    Susan Thomson
2203    Bell Communications Research
2204    MRE 2P-343, 445 South Street
2205    Morristown, NJ 07960
2206
2207    Phone: +1 201 829 4514
2208    EMail: set@thumper.bellcore.com
2209
2210
2211    Jim Bound
2212    Compaq Computer Corporation
2213    110 Spitbrook Road ZK3-3/U14
2214    Nashua, NH 03062-2698
2215
2216    Phone: +1 603 884 0400
2217    EMail: bound@zk3.dec.com
2218
2219
2220    W. Richard Stevens
2221    1202 E. Paseo del Zorro
2222    Tucson, AZ 85718-2826
2223
2224    Phone: +1 520 297 9416
2225    EMail: rstevens@kohala.com
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242 Gilligan, et. al.            Informational                     [Page 40]
2243 \f
2244 RFC 2553       Basic Socket Interface Extensions for IPv6     March 1999
2245
2246
2247 Full Copyright Statement
2248
2249    Copyright (C) The Internet Society (1999).  All Rights Reserved.
2250
2251    This document and translations of it may be copied and furnished to
2252    others, and derivative works that comment on or otherwise explain it
2253    or assist in its implementation may be prepared, copied, published
2254    and distributed, in whole or in part, without restriction of any
2255    kind, provided that the above copyright notice and this paragraph are
2256    included on all such copies and derivative works.  However, this
2257    document itself may not be modified in any way, such as by removing
2258    the copyright notice or references to the Internet Society or other
2259    Internet organizations, except as needed for the purpose of
2260    developing Internet standards in which case the procedures for
2261    copyrights defined in the Internet Standards process must be
2262    followed, or as required to translate it into languages other than
2263    English.
2264
2265    The limited permissions granted above are perpetual and will not be
2266    revoked by the Internet Society or its successors or assigns.
2267
2268    This document and the information contained herein is provided on an
2269    "AS IS" basis and THE INTERNET SOCIETY AND THE INTERNET ENGINEERING
2270    TASK FORCE DISCLAIMS ALL WARRANTIES, EXPRESS OR IMPLIED, INCLUDING
2271    BUT NOT LIMITED TO ANY WARRANTY THAT THE USE OF THE INFORMATION
2272    HEREIN WILL NOT INFRINGE ANY RIGHTS OR ANY IMPLIED WARRANTIES OF
2273    MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298 Gilligan, et. al.            Informational                     [Page 41]
2299 \f