idnits 2.17.1 draft-ietf-ipngwg-bsd-api-04.txt: Checking boilerplate required by RFC 5378 and the IETF Trust (see https://trustee.ietf.org/license-info): ---------------------------------------------------------------------------- ** Cannot find the required boilerplate sections (Copyright, IPR, etc.) in this document. Expected boilerplate is as follows today (2024-04-18) according to https://trustee.ietf.org/license-info : IETF Trust Legal Provisions of 28-dec-2009, Section 6.a: This Internet-Draft is submitted in full conformance with the provisions of BCP 78 and BCP 79. IETF Trust Legal Provisions of 28-dec-2009, Section 6.b(i), paragraph 2: Copyright (c) 2024 IETF Trust and the persons identified as the document authors. All rights reserved. IETF Trust Legal Provisions of 28-dec-2009, Section 6.b(i), paragraph 3: This document is subject to BCP 78 and the IETF Trust's Legal Provisions Relating to IETF Documents (https://trustee.ietf.org/license-info) in effect on the date of publication of this document. Please review these documents carefully, as they describe your rights and restrictions with respect to this document. Code Components extracted from this document must include Simplified BSD License text as described in Section 4.e of the Trust Legal Provisions and are provided without warranty as described in the Simplified BSD License. Checking nits according to https://www.ietf.org/id-info/1id-guidelines.txt: ---------------------------------------------------------------------------- ** Missing expiration date. The document expiration date should appear on the first and last page. ** The document seems to lack a 1id_guidelines paragraph about Internet-Drafts being working documents. ** The document seems to lack a 1id_guidelines paragraph about 6 months document validity. ** The document seems to lack a 1id_guidelines paragraph about the list of current Internet-Drafts. ** The document seems to lack a 1id_guidelines paragraph about the list of Shadow Directories. ** The document is more than 15 pages and seems to lack a Table of Contents. == No 'Intended status' indicated for this document; assuming Proposed Standard Checking nits according to https://www.ietf.org/id-info/checklist : ---------------------------------------------------------------------------- ** The document seems to lack an IANA Considerations section. (See Section 2.2 of https://www.ietf.org/id-info/checklist for how to handle the case when there are no actions for IANA.) ** The document seems to lack separate sections for Informative/Normative References. All references will be assumed normative when checking for downward references. ** There are 13 instances of too long lines in the document, the longest one being 6 characters in excess of 72. ** The abstract seems to contain references ([1]), which it shouldn't. Please replace those with straight textual mentions of the documents in question. == There are 1 instance of lines with non-RFC2606-compliant FQDNs in the document. == There are 1 instance of lines with non-RFC6890-compliant IPv4 addresses in the document. If these are example addresses, they should be changed. Miscellaneous warnings: ---------------------------------------------------------------------------- == Line 190 has weird spacing: '... u_char s6_ad...' -- The document seems to lack a disclaimer for pre-RFC5378 work, but may have content which was first submitted before 10 November 2008. If you have contacted all the original authors and they are all willing to grant the BCP78 rights to the IETF Trust, then this is fine, and you can ignore this comment. If not, you may need to add the pre-RFC5378 disclaimer. (See the Legal Provisions document at https://trustee.ietf.org/license-info for more information.) -- The document date (January 9, 1996) is 10327 days in the past. Is this intentional? -- Found something which looks like a code comment -- if you have code sections in the document, please surround them with '' and '' lines. Checking references for intended status: Proposed Standard ---------------------------------------------------------------------------- (See RFCs 3967 and 4897 for information about using normative references to lower-maturity documents in RFCs) == Missing Reference: '16' is mentioned on line 190, but not defined == Missing Reference: '0' is mentioned on line 961, but not defined -- Looks like a reference, but probably isn't: 'N-1' on line 969 -- Looks like a reference, but probably isn't: 'N' on line 973 -- Possible downref: Non-RFC (?) normative reference: ref. '1' -- Possible downref: Non-RFC (?) normative reference: ref. '2' -- Possible downref: Non-RFC (?) normative reference: ref. '3' -- Possible downref: Non-RFC (?) normative reference: ref. '4' Summary: 11 errors (**), 0 flaws (~~), 6 warnings (==), 9 comments (--). Run idnits with the --verbose option for more detailed information about the items above. -------------------------------------------------------------------------------- 1 Internet Engineering Task Force R. E. Gilligan (Sun) 2 INTERNET-DRAFT S. Thomson (Bellcore) 3 J. Bound (Digital) 5 January 9, 1996 7 IPv6 Program Interfaces for BSD Systems 8 10 Abstract 12 In order to implement the version 6 Internet Protocol (IPv6) [1] in an 13 operating system based on Berkeley Unix (4.x BSD), changes must be made 14 to the application program interface (API). TCP/IP applications written 15 for BSD-based operating systems have in the past enjoyed a high degree 16 of portability because most of the systems derived from BSD provide the 17 same API, known informally as "the socket interface". We would like the 18 same portability with IPv6. This memo presents a set of extensions to 19 the BSD socket API to support IPv6. The changes include a new data 20 structure to carry IPv6 addresses, new name-to-address translation 21 library functions, new address conversion functions, and some new 22 setsockopt() options. The extensions are designed to provide access to 23 IPv6 features, while introducing a minimum of change into the system and 24 providing complete compatibility for existing IPv4 applications. 26 Status of this Memo 28 This document is an Internet Draft. Internet Drafts are working 29 documents of the Internet Engineering Task Force (IETF), its Areas, 30 and its Working Groups. Note that other groups may also distribute 31 working documents as Internet Drafts. 33 Internet Drafts are draft documents valid for a maximum of six months. 34 This Internet Draft expires on July 6, 1996. Internet Drafts may be 35 updated, replaced, or obsoleted by other documents at any time. It is 36 not appropriate to use Internet Drafts as reference material or to cite 37 them other than as a "working draft" or "work in progress." 39 To learn the current status of any Internet-Draft, please check the 40 1id-abstracts.txt listing contained in the Internet-Drafts Shadow 41 Directories on ds.internic.net, nic.nordu.net, ftp.isi.edu, or 42 munnari.oz.au. 44 Distribution of this memo is unlimited. 46 1. Introduction. 48 While IPv4 addresses are 32-bits long, IPv6 nodes are identified by 49 128-bit addresses. The socket interface make the size of an IP address 50 quite visible to an application; virtually all TCP/IP applications for 51 BSD-based systems have knowledge of the size of an IP address. Those 52 parts of the API that expose the addresses need to be extended to 53 accommodate the larger IPv6 address size. IPv6 also introduces new 54 features, some of which must be made visible to applications via the 55 API. This paper defines a set of extensions to the socket interface to 56 support the larger address size and new features of IPv6. 58 This specification is preliminary. These API extensions are expected to 59 evolve as we gain more implementation experience. 61 2. Design Considerations 63 There are a number of important considerations in designing changes to 64 this well-worn API: 66 - The extended API should provide both source and binary 67 compatibility for programs written to the original API. That 68 is, existing program binaries should continue to operate when 69 run on a system supporting the new API. In addition, existing 70 applications that are re-compiled and run on a system supporting 71 the new API should continue to operate. Simply put, the API 72 changes for IPv6 should not break existing programs. 74 - The changes to the API should be as small as possible in order 75 to simplify the task of converting existing IPv4 applications to 76 IPv6. 78 - Where possible, applications should be able to use the extended 79 API to interoperate with both IPv6 and IPv4 hosts. Applications 80 should not need to know which type of host they are 81 communicating with. 83 - IPv6 addresses carried in data structures should be 64-bit 84 aligned. This is necessary in order to obtain optimum 85 performance on 64-bit machine architectures. 87 Because of the importance of providing IPv4 compatibility in the API, 88 these extensions are explicitly designed to operate on machines that 89 provide complete support for both IPv4 and IPv6. A subset of this API 90 could probably be designed for operation on systems that support only 91 IPv6. However, this is not addressed in this document. 93 2.1. What Needs to be Changed 95 The socket interface API consists of a few distinct components: 97 - Core socket functions. 99 - Address data structures. 101 - Name-to-address translation functions. 103 - Address conversion functions. 105 The core socket functions -- those functions that deal with such things 106 as setting up and tearing down TCP connections, and sending and 107 receiving UDP packets -- were designed to be transport independent. 108 Where protocol addresses are passed as function arguments, they are 109 carried via opaque pointers. A protocol specific address data structure 110 is defined for each protocol that the socket functions support. 111 Applications must cast these protocol specific address structures into 112 the generic "sockaddr" data type when using the socket functions. These 113 functions need not change for IPv6, but a new IPv6 specific address data 114 structure is needed. 116 The "sockaddr_in" structure is the protocol specific data structure for 117 IPv4. This data structure actually includes 8-octets of unused space, 118 and it is tempting to try to use this space to adapt the sockaddr_in 119 structure to IPv6. Unfortunately, the sockaddr_in structure is not 120 large enough to hold the 16-octet IPv6 address as well as the other 121 information (2-octet address family and 2-octet port number) that is 122 needed. So a new address data structure must be defined for IPv6. 124 The name-to-address translation functions in the socket interface are 125 gethostbyname() and gethostbyaddr(). Gethostbyname() does not provide 126 enough flexibility to accommodate more than one protocol family. To 127 solve this problem, we introduced a new name-to-address translation 128 function which is analogous to gethostbyname(), but supports addresses 129 in both the IPv4 and IPv6 address families. Gethostbyaddr() does not, 130 strictly speaking, need to be replaced since it carries an address 131 family argument and can be extended to support both address families 132 without introducing compatibility problems. However, we have chosen to 133 introduce a new function to maintain symmetry with the replacement to 134 gethostbyname(). The new functions both carry an address family 135 parameter, so they can be extended to operate with other protocol 136 families in addition to IPv4 and IPv6. 138 The address conversion functions -- inet_ntoa() and inet_addr() -- 139 convert IPv4 addresses between binary and printable form. These 140 functions are quite specific to 32-bit IPv4 addresses. We have designed 141 two analogous functions which convert both IPv4 and IPv6 addresses, and 142 carry an address type parameter so that they can be extended to other 143 protocol families as well. 145 Finally, a few miscellaneous features are needed to support IPv6. A new 146 interface is needed in order to support the IPv6 flow label and priority 147 header fields. New interfaces are needed in order to receive IPv6 148 multicast packets and control the sending of multicast packets. And an 149 interface is necessary in order to pass IPv6 source route information 150 between the application and the system. 152 3. Socket Interface 154 This section specifies the socket interface changes for IPv6. 156 The data types of the structure elements given in the following section 157 are intended to be examples, not absolute requirements. System 158 implementations may use other types if they are appropriate. In some 159 cases, such as when a field of a data structure holds a protocol value, 160 the structure field must be of some minimum size. These size 161 requirements are noted in the text. For example, since the UDP and TCP 162 port values are 16-bit quantities, the sin6_port field must be at least 163 a 16-bit data types. The sin6_port field is specified as a u_int16_t 164 type, but an implementation may use any data type that is at least 165 16-bits long. 167 3.1. New Address Family 169 A new address family macro, named AF_INET6, is defined in 170 . The AF_INET6 definition is used to distinguish between 171 the original sockaddr_in address data structure, and the new 172 sockaddr_in6 data structure. 174 A new protocol family macro, named PF_INET6, is defined in 175 . Like most of the other protocol family macros, this 176 will usually be defined to have the same value as the corresponding 177 address family macro: 179 #define PF_INET6 AF_INET6 181 The PF_INET6 is used in the first argument to the socket() function to 182 indicate that an IPv6 socket is being created. 184 3.2. IPv6 Address Data Structure 186 A new data structure to hold a single IPv6 address is defined as 187 follows: 189 struct in6_addr { 190 u_char s6_addr[16]; /* IPv6 address */ 191 } 193 This data structure contains an array of sixteen 8-bit elements, which 194 make up one 128-bit IPv6 address. The IPv6 address is stored in network 195 byte order. 197 Applications obtain the declaration for this structure by including 198 the system header file . 200 3.3. Socket Address Structure for 4.3 BSD-Based Systems 202 In the socket interface, a different protocol-specific data structure is 203 defined to carry the addresses for each of the protocol suite. Each 204 protocol-specific data structure is designed so it can be cast into a 205 protocol-independent data structure -- the "sockaddr" structure. Each 206 has a "family" field which overlays the "sa_family" of the sockaddr data 207 structure. This field can be used to identify the type of the data 208 structure. 210 The sockaddr_in structure is the protocol-specific address data 211 structure for IPv4. It is used to pass addresses between applications 212 and the system in the socket functions. The following structure is 213 defined to carry IPv6 addresses: 215 struct sockaddr_in6 { 216 u_int16_t sin6_family; /* AF_INET6 */ 217 u_int16_t sin6_port; /* Transport layer port # */ 218 u_int32_t sin6_flowinfo; /* IPv6 flow information */ 219 struct in6_addr sin6_addr; /* IPv6 address */ 220 }; 222 This structure is designed to be compatible with the sockaddr data 223 structure used in the 4.3 BSD release. 225 The sin6_family field is used to identify this as a sockaddr_in6 226 structure. This field is designed to overlay the sa_family field when 227 the buffer is cast to a sockaddr data structure. The value of this 228 field must be AF_INET6. 230 The sin6_port field is used to store the 16-bit UDP or TCP port 231 number. This field is used in the same way as the sin_port field of 232 the sockaddr_in structure. The port number is stored in network byte 233 order. 235 The sin6_flowinfo field is a 32-bit field that is used to store three 236 pieces of information: the 24-bit IPv6 flow label, the 4-bit priority 237 field, and a 1-bit loose/strict source routing flag. The IPv6 flow 238 label is represented as the low-order 24-bits of the 32-bit field. The 239 priority is represented in the next 4-bits above this, and the 240 loose/strict flag is the 1 bit above this. The high-order 3 bits of 241 this field are reserved. The sin6_flowinfo field is stored in network 242 byte order. The use of the flow label and priority fields are explained 243 in sec 4.9. The use of the loose/strict flag is discussed in section 244 4.10. 246 The sin6_addr field is a single in6_addr structure (defined in the 247 previous section). This field holds one 128-bit IPv6 address. The 248 address is stored in network byte order. 250 The ordering of elements in this structure is specifically designed so 251 that the sin6_addr field will be aligned on a 64-bit boundary. This is 252 done for optimum performance on 64-bit architectures. 254 Applications obtain the declaration of the sockaddr_in6 structure by 255 including the system header file . 257 3.4. Socket Address Structure for 4.4 BSD-Based Systems 259 The 4.4 BSD release includes a small, but incompatible change to the 260 socket interface. The "sa_family" field of the sockaddr data structure 261 was changed from a 16-bit value to an 8-bit value, and the space saved 262 used to hold a length field, named "sa_len". The sockaddr_in6 data 263 structure given in the previous section can not be correctly cast into 264 the newer sockaddr data structure. For this reason, following 265 alternative IPv6 address data structure is provided to be used on 266 systems based on 4.4 BSD: 268 #define SIN6_LEN 270 struct sockaddr_in6 { 271 u_char sin6_len; /* length of this struct */ 272 u_char sin6_family; /* AF_INET6 */ 273 u_int16_t sin6_port; /* Transport layer port # */ 274 u_int32_t sin6_flowinfo; /* IPv6 flow information */ 275 struct in6_addr sin6_addr; /* IPv6 address */ 276 }; 278 The only differences between this data structure and the 4.3 BSD variant 279 are the inclusion of the length field, and the change of the family 280 field to a 8-bit data type. The definitions of all the other fields are 281 identical to the 4.3 BSD variant defined in the previous section. 283 Systems that provide this version of the sockaddr_in6 data structure 284 must also declare the SIN6_LEN as a result of including the 285 header file. This macro allows applications to determine 286 whether they are being built on a system that supports the 4.3 BSD or 287 4.4 BSD variants of the data structure. Applications can be written to 288 run on both systems by simply making their assignments and use of the 289 sin6_len field conditional on the SIN6_LEN field. For example, to fill 290 in an IPv6 address structure in an application, one might write: 292 struct sockaddr_in6 sin6; 294 bzero((char *) &sin6, sizeof(struct sockaddr_in6)); 295 #ifdef SIN6_LEN 296 sin6.sin6_len = sizeof(struct sockaddr_in6); 297 #endif 298 sin6.sin6_family = AF_INET6; 299 sin6.sin6_port = htons(23); 301 Note that the size of the sockaddr_in6 structure is larger than the size 302 of the sockaddr structure. Applications that use the sockaddr_in6 303 structure need to be aware that they can not use sizeof(sockaddr) to 304 allocate a buffer to hold a sockaddr_in6 structure. They should use 305 sizeof(sockaddr_in6) instead. 307 3.5. The Socket Functions 309 Applications use the socket() function to create a socket descriptor 310 that represents a communication endpoint. The arguments to the socket() 311 function tell the system which protocol to use, and what format address 312 structure will be used in subsequent functions. For example, to create 313 an IPv4/TCP socket, applications make the call: 315 s = socket (PF_INET, SOCK_STREAM, 0); 317 To create an IPv4/UDP socket, applications make the call: 319 s = socket (PF_INET, SOCK_DGRAM, 0); 321 Applications may create IPv6/TCP and IPv6/UDP sockets by simply using 322 the constant PF_INET6 instead of PF_INET in the first argument. For 323 example, to create an IPv6/TCP socket, applications make the call: 325 s = socket (PF_INET6, SOCK_STREAM, 0); 327 To create an IPv6/UDP socket, applications make the call: 329 s = socket (PF_INET6, SOCK_DGRAM, 0); 331 Once the application has created a PF_INET6 socket, it must use the 332 sockaddr_in6 address structure when passing addresses in to the system. 333 The functions which the application uses to pass addresses into the 334 system are: 336 bind() 337 connect() 338 sendmsg() 339 sendto() 341 The system will use the sockaddr_in6 address structure to return 342 addresses to applications that are using PF_INET6 sockets. The 343 functions that return an address from the system to an application 344 are: 346 accept() 347 recvfrom() 348 recvmsg() 349 getpeername() 350 getsockname() 352 No changes to the syntax of the socket functions are needed to support 353 IPv6, since the all of the "address carrying" functions use an opaque 354 address pointer, and carry an address length as a function argument. 356 3.6. Compatibility with IPv4 Applications 358 In order to support the large base of applications using the original 359 API, system implementations must provide complete source and binary 360 compatibility with the original API. This means that systems must 361 continue to support PF_INET sockets and the sockaddr_in addresses 362 structure. Applications must be able to create IPv4/TCP and IPv4/UDP 363 sockets using the PF_INET constant in the socket() function, as 364 described in the previous section. Applications should be able to hold 365 a combination of IPv4/TCP, IPv4/UDP, IPv6/TCP and IPv6/UDP sockets 366 simultaneously within the same process. 368 Applications using the original API should continue to operate as they 369 did on systems supporting only IPv4. That is, they should continue to 370 interoperate with IPv4 nodes. It is not clear, though, how, or even if, 371 those IPv4 applications should interoperate with IPv6 nodes. The open 372 issues section (section 9) discusses some of the alternatives. 374 3.7. Compatibility with IPv4 Nodes 376 The API also provides a different type of compatibility: the ability for 377 applications using the extended API to interoperate with IPv4 nodes. 379 This feature uses the IPv4-mapped IPv6 address format defined in the 380 IPv6 addressing architecture specification [3]. This address format 381 allows the IPv4 address of an IPv4 node to be represented as an IPv6 382 address. The IPv4 address is encoded into the low-order 32-bits of the 383 IPv6 address, and the high-order 96-bits hold the fixed prefix 384 0:0:0:0:0:FFFF. IPv4-mapped addresses are written as follows: 386 ::FFFF: 388 Applications may use PF_INET6 sockets to open TCP connections to IPv4 389 nodes, or send UDP packets to IPv4 nodes, by simply encoding the 390 destination's IPv4 address as an IPv4-mapped IPv6 address, and passing 391 that address, within a sockaddr_in6 structure, in the connect() or 392 sendto() call. When applications use PF_INET6 sockets to accept TCP 393 connections from IPv4 nodes, or receive UDP packets from IPv4 nodes, the 394 system returns the peer's address to the application in the accept(), 395 recvfrom(), or getpeername() call using a sockaddr_in6 structure encoded 396 this way. 398 Few applications will likely need to know which type of node they are 399 interoperating with. However, for those applications that do need to 400 know, the inet6_isipv4addr() function, defined in section 6.3, is 401 provided. 403 3.8. Flow Information 405 The IPv6 header has a 24-bit field to hold a "flow label", and a 4-bit 406 field to hold a "priority" value. Applications have control over what 407 values for these fields are used in packets that they originate, and 408 have access to the field values of packets that they receive. 410 The sin6_flowinfo field of the sockaddr_in6 structure encodes three 411 pieces of information: IPv6 flow label, IPv6 priority, and a 412 strict/loose source routing flag which is discussed in section 4.2. 413 Applications use this field to set the flow label and priority in IPv6 414 headers of packets they generate, and to retrieve the flow label and 415 priority from the packets they receive. The header fields of an 416 actively opened TCP connection are set by assigning in the sin6_flowinfo 417 field of the destination address sockaddr_in6 structure passed in the 418 connect() function. The same technique can be used with the 419 sockaddr_in6 structure passed in to the sendto() or sendmsg() function 420 to set the flow label and priority fields of UDP packets. Similarly, 421 the flow label and priority values of received UDP packets and accepted 422 TCP connections are reflected in the sin6_flowinfo field of the 423 sockaddr_in6 structure returned to the application by the recvfrom(), 424 recvmsg(), and accept() functions. And an application may specify the 425 flow label and priority to use in transmitted packets of a passively 426 accepted TCP connection, by setting the sin6_flowinfo field of the 427 address passed in the bind() function. 429 Implementations provide two bitmask constant declarations to help 430 applications select out the flow label and priority fields. These 431 constants are: 433 IPV6_FLOWINFO_FLOWLABEL 434 IPV6_FLOWINFO_PRIORITY 436 These constants can be applied to the sin6_flowinfo field of addresses 437 returned to the application, for example: 439 struct sockaddr_in6 sin6; 440 . . . 441 recvfrom(s, buf, buflen, flags, (struct sockaddr *) &sin6, &fromlen); 442 . . . 443 received_flowlabel = sin6.sin6_flowinfo & IPV6_FLOWINFO_FLOWLABEL; 444 received_priority = sin6.sin6_flowinfo & IPV6_FLOWINFO_PRIORITY; 446 On the sending side, applications are responsible for selecting the flow 447 label value. The system provides constant declarations for the IPv6 448 priority values defined in the IPv6 specification [1]. These constants 449 are: 451 IPV6_PRIORITY_UNCHARACTERIZED 452 IPV6_PRIORITY_FILLER 453 IPV6_PRIORITY_UNATTENDED 454 IPV6_PRIORITY_RESERVED1 455 IPV6_PRIORITY_BULK 456 IPV6_PRIORITY_RESERVED2 457 IPV6_PRIORITY_INTERACTIVE 458 IPV6_PRIORITY_CONTROL 459 IPV6_PRIORITY_8 460 IPV6_PRIORITY_9 461 IPV6_PRIORITY_10 462 IPV6_PRIORITY_11 463 IPV6_PRIORITY_12 464 IPV6_PRIORITY_13 465 IPV6_PRIORITY_14 466 IPV6_PRIORITY_15 468 Applications can use these constants along with the flow label they 469 selected to assign the sin6_flowinfo field, for example: 471 struct sockaddr_in6 sin6; 472 . . . 473 send_flowlabel = . . . ; 474 . . . 476 sin6.sin6_flowinfo = IPV6_PRIORITY_UNATTENDED | 477 (IPV6_FLOWINFO_FLOWLABEL & send_flowlabel); 479 The macro declarations for these constants are obtained by including 480 the header file . 482 3.9. Binding to System-Selected Address 484 While the bind() function allows applications to select the source IP 485 address of UDP packets and TCP connections, applications often wish to 486 let the system select the source address for them. In IPv4, this is 487 done by specifying the IPv4 address represented by the symbolic 488 constant INADDR_ANY in the bind() call, or by simply by skipping the 489 bind() entirely. 491 Since the IPv6 address type is a structure (struct in6_addr), a 492 symbolic constant can be used to initialize an IPv6 address variable, 493 but can not be used in an assignment. Therefore systems provide the 494 IPv6 address value that can be used to instruct the system to select 495 the source IPv6 address in two forms. 497 The first version is a global variable named "ipv6addr_any" which is 498 an in6_addr type structure. The extern declaration for this variable 499 is: 501 extern struct in6_addr ipv6addr_any; 503 Applications use ipv6addr_any similarly to the way they use INADDR_ANY 504 in IPv4. For example, to bind a socket to port number 23, but let the 505 system select the source address, an application could use the 506 following code: 508 struct sockaddr_in6 sin6; 509 . . . 510 sin6.sin6_family = AF_INET6; 511 sin6.sin6_flowinfo = 0; 512 sin6.sin6_port = htons(23); 513 sin6.sin6_addr = ipv6addr_any; 514 . . . 515 if (bind(s, (struct sockaddr *) &sin6, sizeof(sin6)) == -1) 516 . . . 518 The other version is a symbolic constant named IPV6ADDR_ANY_INIT. 519 This constant can be used to initialize an in6_addr structure: 521 struct in6_addr anyaddr = IPV6ADDR_ANY_INIT; 523 Note that this constant can be used ONLY at declaration type. It can 524 not be used assign a previously declared in6_addr structure. For 525 example, the following code will not work: 527 /* This is the WRONG way to assign an unspecified address */ 528 struct sockaddr_in6 sin6; 529 . . . 530 sin6.sin6_addr = IPV6ADDR_ANY_INIT; /* Will NOT compile */ 532 3.10. Communicating with Local Services 534 Applications may need to send UDP packets to, or originate TCP 535 connections to, services residing on the local node. In IPv4, they 536 can do this by using the constant IPv4 address INADDR_LOOPBACK in 537 their connect(), sendto(), or sendmsg() call. 539 IPv6 also provides a loopback address which can be used to contact 540 local TCP and UDP services. Like the unspecified address, the IPv6 541 loopback address is provided in two forms -- a global variable and a 542 symbolic constant. 544 The global variable is an in6_addr type structure named 545 "ipv6addr_loopback." The extern declaration for this variable is: 547 extern struct in6_addr ipv6addr_loopback; 549 Applications use ipv6addr_loopback as they would use INADDR_LOOPBACK 550 in IPv4 applications. For example, to open a TCP connection to the 551 local telnet server, an application could use the following code: 553 struct sockaddr_in6 sin6; 554 . . . 555 sin6.sin6_family = AF_INET6; 556 sin6.sin6_flowinfo = 0; 557 sin6.sin6_port = htons(23); 558 sin6.sin6_addr = ipv6addr_loopback; 559 . . . 560 if (connect(s, (struct sockaddr *) &sin6, sizeof(sin6)) == -1) 561 . . . 563 The symbolic constant is named IPV6ADDR_LOOPBACK_INIT. It can be used 564 at declaration time ONLY; for example: 566 struct in6_addr loopbackaddr = IPV6ADDR_LOOPBACK_INIT; 568 Like IPV6ADDR_ANY_INIT, this constant can not be used in an assignment 569 to a previously declared IPv6 address variable. 571 4. Socket Options 573 A number of new socket options are defined for IPv6. All of these new 574 options are at the IPPROTO_IPV6 level. That is, the "level" parameter 575 in the getsockopt() and setsockopt() call is IPPROTO_IPV6 when using 576 these options. The constant name prefix IPV6_ is used in all of the new 577 socket options. This serves to clearly identify these options as 578 applying to IPv6. 580 The macro declaration for IPPROTO_IPV6, the new IPv6 socket options, and 581 related constants defined in this section are obtained by including the 582 header file 584 4.1 Changing Socket Type 586 Unix allows open sockets to be passed between processes via the exec() 587 call and other means. It is a relatively common application practice to 588 pass open sockets across exec() calls. Thus it is possible for an 589 application using the original API to pass an open PF_INET socket to an 590 application that is expecting to receive a PF_INET6 socket. Similarly, 591 it is possible for an application using the extended API to pass an open 592 PF_INET6 socket to an application using the original API, which would be 593 equipped only to deal with PF_INET sockets. Either of these cases could 594 cause problems, because the application which is passed the open socket 595 might not know how to decode the address structures returned in 596 subsequent socket functions. 598 To remedy this problem, a new setsockopt() option is defined that allows 599 an application to "transform" a PF_INET6 socket into a PF_INET socket 600 and vice-versa. 602 An IPv6 application that is passed an open socket from an unknown 603 process may use the IPV6_ADDRFORM setsockopt() option to "convert" the 604 socket to PF_INET6. Once that has been done, the system will return 605 sockaddr_in6 address structures in subsequent socket functions. 606 Similarly, an IPv6 application that is about to pass an open PF_INET6 607 socket to a program that may not be IPv6 capable may "downgrade" the 608 socket to PF_INET before calling exec(). After that, the system will 609 return sockaddr_in address structures to the application that was 610 exec()'ed. 612 The IPV6_ADDRFORM option is at the IPPROTO_IP level. The only valid 613 option values are PF_INET6 and PF_INET. For example, to convert a 614 PF_INET6 socket to PF_INET, a program would call: 616 int addrform = PF_INET; 618 if (setsockopt(s, IPPROTO_IPV6, IPV6_ADDRFORM, (char *) &addrform, 619 sizeof(addrform)) == -1) 620 perror("setsockopt IPV6_ADDRFORM"); 622 An application may use IPV6_ADDRFORM in the getsockopt() function to 623 learn whether an open socket is a PF_INET of PF_INET6 socket. For 624 example: 626 int addrform; 627 int len = sizeof(int); 629 if (getsockopt(s, IPPROTO_IPV6, IPV6_ADDRFORM, (char *) &addrform, 630 &len) == -1) 631 perror("getsockopt IPV6_ADDRFORM"); 632 if (addrform == PF_INET) 633 printf("This is an IPv4 socket.\n"); 634 else if (addrform == PF_INET6) 635 printf("This is an IPv6 socket.\n"); 636 else 637 printf("This system is broken.\n"); 639 4.2. Handling IPv6 Source Routes 641 IPv6 makes more use of the source routing mechanism than IPv4. In order 642 for source routing to operate properly, the node receiving a request 643 packet that bears a source route must reverse that source route when 644 sending the reply. In the case of TCP, the reversal can be done in the 645 transport protocol implementation transparently to the application. But 646 in the case of UDP, the application must perform the reversal itself. 647 The transport protocol code can not perform the reversal for UDP packets 648 because a UDP application may receive a number of requests and generate 649 replies asynchronously. A "reply" sent by an application may not match 650 the "request" most recently passed up to the application. 652 The API for source routing has two components: providing a source route 653 to be used with originated traffic -- actively opened TCP connections 654 and UDP packets being sent; and retrieving the source route of received 655 traffic -- passively accepted TCP connections and received UDP packets. 656 An application may always provide a source route with TCP connections 657 being originated and UDP packets being sent. But to receive source 658 routes, the application must enable an option. 660 To provide a source route, an application simply provides an array of 661 sockaddr_in6 data structures in the msg_name field of the msghdr 662 structure of a sendmsg() function, or the address argument of the 663 sendto() function (when sending a UDP packet), or the address argument 664 of the connect() function (when actively opening a TCP connection). For 665 sendto() and connect(), the length argument of the function is the total 666 length, in octets, of the array. For sendmsg(), the msg_namelen field 667 of the msghdr structure specifies the total length of the array. The 668 elements of the array represent the full source route, including both 669 source and destination endpoint address. The elements of the array are 670 ordered from destination to source. That is, the first element of the 671 array represents the destination endpoint address, and the last element 672 of the array represents the source endpoint address. If the application 673 provides a source route, the source endpoint address can not be omitted. 674 The sin6_addr field of the source endpoint address may be set to 675 ipv6addr_any, however, in which case the system will select an 676 appropriate source address. The sin6_port field of the destination 677 endpoint address must be assigned. The sin6_port field of the source 678 endpoint address may be set to zero, in which case the system will 679 select an appropriate source port number. The sin6_port fields of the 680 intermediate addresses must be set to zero. 682 The application also has control over the loose/strict source routing 683 flag that is defined in the IPv6 specification [1]. It does this by 684 setting or clearing the loose/strict flag contained in the sin6_flowinfo 685 field of the destination and intermediate addresses. On the receive 686 side, the implementation uses the loose/strict flag in the address array 687 returned to the application to indicate the loose/strict status of each 688 hop. 690 The implementation provides a set of constant definitions to simplify 691 getting and setting the loose/strict flag for each of the hops of a 692 source route. The following constant is used to select the loose/strict 693 flag from the sin6_flowinfo field: 695 IPV6_FLOWINFO_SRFLAG 697 In addition, two constants are provided which represent the two states 698 of this flag: 700 IPV6_SRFLAG_STRICT 701 IPV6_SRFLAG_LOOSE 703 These constants can be used to inspect the source route flags of 704 received addresses, for example: 706 struct sockaddr_in6 sin6[3]; 707 . . . 708 if ((sin6[0].sin6_flowinfo & IPV6_FLOWINFO_SRFLAG) == 709 IPV6_SRFLAG_STRICT) 710 . . . 712 And they can also be used to set the source route flags: 714 struct sockaddr_in6 sin6[3]; 715 . . . 716 sin6[0].sin6_flowinfo = 717 (sin6[0].sin6_flowinfo & ~IPV6_FLOWINFO_SRFLAG) | 718 IPV6_SRFLAG_STRICT; 720 The flow label and priority sub-fields of the sin6_flowinfo field of the 721 destination endpoint address may be set, but the these fields must be 722 set to zero in the intermediate and source endpoint addresses. 724 The arrangement of the address structures in the address buffer passed 725 to sendmsg(), connect() or sendto() is shown in the figure below: 727 +--------------------+ 728 | | 729 | sockaddr_in6[0] | Destination Endpoint Address 730 | | 731 +--------------------+ 732 | | 733 | sockaddr_in6[1] | Last Source-Route Hop Address 734 | | 735 +--------------------+ 736 . . 737 . . 738 . . 739 +--------------------+ 740 | | 741 | sockaddr_in6[N-1] | First Source-Route Hop Address 742 | | 743 +--------------------+ 744 | | 745 | sockaddr_in6[N] | Source Endpoint Address 746 | | 747 +--------------------+ 749 Address buffer when sending a source route 751 The IPV6_RECVSRCRT setsockopt() option controls the reception of source 752 routes. The option is disabled by default. Applications must 753 explicitly enable the option using the setsockopt() function in order to 754 receive source routes. 756 The IPV6_RECVSRCRT option is at the IPPROTO_IPV6 level. An example of 757 how an application might use this option is: 759 int on = 1; /* value == 1 means enable the option */ 761 if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVSRCRT, (char *) &on, 762 sizeof(on)) == -1) 763 perror("setsockopt IPV6_RECVSRCRT"); 765 When the IPV6_RECVSRCRT option is disabled, only a single sockaddr_in6 766 address structure is returned to applications in the address argument of 767 the recvfrom() and accept() functions. This address represents the 768 source endpoint address of the UDP packet received or the TCP connection 769 accepted. 771 When the IPV6_RECVSRCRT option is enabled, the msg_name field of the 772 msghdr of the recvmsg() function, or the address argument of the 773 recvfrom() function (when receiving UDP packets) and the accept() 774 functions (when passively accepting TCP connections) points to an array 775 of sockaddr_in6 structures. When the function returns, the array will 776 hold two elements -- source and destination address -- when the received 777 UDP packet or TCP SYN packet does not carry a source route. The array 778 will hold more than two elements when the received packet carries a 779 source route. 781 The addresses in the array are ordered from source to destination. That 782 is, the first element of the array holds source endpoint address of the 783 received packet. Following this in the array are the intermediate hops 784 in the order in which they were visited. The last element of the array 785 holds the destination endpoint address. Note that this is the opposite 786 of the order specified for sending. This ordering was chosen so that 787 the address array returned in a recvmsg() or recvfrom() call can be used 788 in a subsequent sendmsg() or sendto() call without requiring the 789 application to re-order the addresses in the array. Similarly, the 790 address array received in an accept() call can be used unchanged in a 791 subsequent connect() call. 793 The address length argument of the recvfrom() and accept() functions, 794 and the msg_namelen field of the msghdr field in the recvmsg() function, 795 indicate the length, in octets, of the full address array. This 796 argument is a value-result parameter. The application sets the maximum 797 size of the address buffer when it makes the call, and the system 798 modifies the value to return the actual size of the buffer to the 799 application. 801 The sin6_port field of the first and last array elements (source and 802 destination endpoint address) will hold the source and destination UDP 803 or TCP port number of the received packet. The sin6_port field of the 804 intermediate elements of the array will be zero. 806 The flow label and priority sub-fields of the sin6_flowinfo field of the 807 source endpoint address will hold the flow label and priority values of 808 the received packet. The flow label and priority sub-fields of the 809 intermediate addresses and the destination endpoint address will be 810 zero. The loose/strict flag of the sin6_flowinfo field of the source 811 endpoint address and the intermediate addresses will be set according to 812 the flags in the received packet. The macros defined above can be used 813 to inspect the loose/strict flag of each hop. 815 The address buffer returned to the application in the recvfrom() or 816 accept() functions when the IPV6_RECVSRCRT option is enabled is shown 817 below: 819 +--------------------+ 820 | | 821 | sockaddr_in6[0] | Source Endpoint Address 822 | | 823 +--------------------+ 824 | | 825 | sockaddr_in6[1] | First Source-Route Hop Address 826 | | 827 +--------------------+ 828 . . 829 . . 830 . . 831 +--------------------+ 832 | | 833 | sockaddr_in6[N-1] | Last Source-Route Hop Address 834 | | 835 +--------------------+ 836 | | 837 | sockaddr_in6[N] | Destination Endpoint Address 838 | | 839 +--------------------+ 841 Address buffer when receiving a source route 843 IPv6 allows a source route with up to 23 intermediate hops. Since the 844 it must also receive the source and destination endpoint addresses, the 845 application must provide a buffer capable of holding 25 addresses to 846 receive such a source route. Implementations provide the following 847 constant declaration in order to allow applications to simply declare 848 storage for the largest possible source route: 850 IPV6_SR_MAXADDR 852 Applications can use this constant like this: 854 struct sockaddr_in6 sin6[IPV6_SR_MAXADDR]; 856 It may be impractical for some applications to allocate space to hold 857 the largest possible source route. Thus a received source route may be 858 too large to fit into the buffer provided by the application. In this 859 circumstance, the system should return only a single address element -- 860 the source endpoint address -- to the application. This case is clearly 861 distinguishable to the application because in all other cases, the 862 system returns at least two address elements -- the source and 863 destination endpoint addresses. 865 4.3. Receiving Interface Determination 867 Some applications run on multi-homed hosts need to determine which 868 interface UDP packets were received on or TCP connections are bound to. 869 While the source routing interface described in the previous section 870 returns the destination address of the packet, this does not necessarily 871 identify the receiving interface. Some cases where it does not are: 873 - When the received packet is multicast. The destination address 874 in this case is an IPv6 multicast address, not the address of an 875 interface. 877 - When the node is operating as an IPv6 router. The node may 878 receive packets on interfaces other than the one they are 879 addressed to. 881 - When the received packet is sent to the node's link-local 882 address which is being used on multiple interfaces. 884 The address of the receiving interface is returned to the application 885 similarly to the way that source routes are returned. A new 886 setsockopt() option named IPV6_RECVIF is provided at the IPPROTO_IPV6 887 level. If this option is enabled, the system returns an additional 888 sockaddr_in6 structure to the application, holding the IPv6 address of 889 the receiving interface, in the recvfrom(), recvmsg(), or accept() 890 functions. 892 The option is enabled like this: 894 int on = 1; /* value == 1 means enable the option */ 896 if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVIF, (char *) &on, 897 sizeof(on)) == -1) 898 perror("setsockopt IPV6_RECVIF"); 900 This option can be used in conjunction with the IP_RECVSRCRT option. 901 When the IPV6_RECVIF option is enabled, the buffer returned to the 902 application is structured like this: 904 +--------------------+ - - - - - - - - - - - - - - - 905 | | 906 | sockaddr_in6[0] | 907 | | 908 +--------------------+ 909 . . Source Address, or 910 . . Full Source Route 911 . . 912 +--------------------+ 913 | | 914 | sockaddr_in6[N-1] | 915 | | 916 +--------------------+ - - - - - - - - - - - - - - - 917 | | 918 | sockaddr_in6[N] | Receiving Interface Address 919 | | 920 +--------------------+ - - - - - - - - - - - - - - - 922 Address buffer with receiving interface address 924 The last address in the array is an IPv6 address of the receiving 925 interface. Since interfaces in IPv6 may have more than one address, and 926 some addresses (e.g. link-local addresses) may be used on more than one 927 interface, the system should select an address that uniquely identifies 928 the interface. 930 As when receiving source routes, the system returns a single 931 sockaddr_in6 structure holding the source endpoint address if the buffer 932 supplied by the application is too small to hold the receiving interface 933 address. 935 4.4. Sending Interface Specification 937 Applications may also need to specify the outgoing interface that 938 originated UDP or TCP packets should use. This is accomplished like 939 source route selection. The application may provide an additional 940 sockaddr_in6 structure in its sendto(), sendmsg() or connect() call 941 specifying the address of the outgoing interface. Unlike source route 942 selection, the outgoing interface address can only be included if a new 943 option is enabled. The new option is needed so that the system can 944 differentiate between the application's specification of an outgoing 945 interface address and a source route. 947 The new option option is named IPV6_SENDIF and is at the IPPROTO_IPV6 948 level. It can be enabled like this: 950 int on = 1; /* value == 1 means enable the option */ 951 if (setsockopt(s, IPPROTO_IPV6, IPV6_SENDIF, (char *) &on, 952 sizeof(on)) == -1) 953 perror("setsockopt IPV6_SENDIF"); 955 This option can be used in conjunction with source route specification. 956 If this option is enabled, the application passes in an address array 957 structured as follows: 959 +--------------------+ - - - - - - - - - - - - - - - 960 | | 961 | sockaddr_in6[0] | 962 | | 963 +--------------------+ 964 . . Destination Address, or 965 . . Full Source Route 966 . . 967 +--------------------+ 968 | | 969 | sockaddr_in6[N-1] | 970 | | 971 +--------------------+ - - - - - - - - - - - - - - - 972 | | 973 | sockaddr_in6[N] | Sending Interface Address 974 | | 975 +--------------------+ - - - - - - - - - - - - - - - 977 Address buffer with sending interface address 979 The last address in the array is an IPv6 address of the sending 980 interface. Applications should use an address that uniquely identifies 981 the interface to use. 983 4.5. Unicast Hop Limit 985 A new setsockopt() option is used to control the hop limit used in 986 outgoing unicast IPv6 packets. The name of this option is 987 IPV6_UNICAST_HOPS, and it is used at the IPPROTO_IPV6 layer. The 988 following example illustrates how it is used: 990 int hoplimit = 10; 992 if (setsockopt(s, IPPROTO_IPV6, IPV6_UNICAST_HOPS, (char *) &hoplimit, 993 sizeof(hoplimit)) == -1) 994 perror("setsockopt IPV6_UNICAST_HOPS"); 996 When the IPV6_UNICAST_HOPS option is set with setsockopt(), the option 997 value given is used as the hop limit for all subsequent unicast packets 998 sent via that socket. If the option is not set, the system selects a 999 default value. 1001 The IPV6_UNICAST_HOPS option may be used in the getsockopt() function to 1002 determine the hop limit value that the system will use for subsequent 1003 unicast packets sent via that socket. For example: 1005 int hoplimit; 1006 int len = sizeof(hoplimit); 1008 if (getsockopt(s, IPPROTO_IPV6, IPV6_UNICAST_HOPS, (char *) &hoplimit, 1009 &len) == -1) 1010 perror("getsockopt IPV6_UNICAST_HOPS"); 1011 else 1012 printf("Using %d for hop limit.\n", hoplimit); 1014 4.6. Sending and Receiving Multicast Packets 1016 IPv6 applications may send UDP multicast packets by simply specifying an 1017 IPv6 multicast address in the address argument of the sendto() function. 1019 A few setsockopt options at the IPPROTO_IPV6 layer are used to control 1020 some of the parameters of sending multicast packets. These options are 1021 optional: applications may send multicast packets without using these 1022 options. The setsockopt() options for controlling the sending of 1023 multicast packets are summarized below: 1025 IPV6_MULTICAST_IF 1027 Set the interface to use for outgoing multicast packets. 1028 The argument is an IPv6 address of the interface to use. 1030 Argument type: struct in6_addr 1032 IPV6_MULTICAST_HOPS 1034 Set the hop limit to use for outgoing multicast packets. 1035 (Note a separate option - IPV6_UNICAST_HOPS - is 1036 provided to set the hop limit to use for outgoing 1037 unicast packets.) 1039 Argument type: unsigned int 1041 IPV6_MULTICAST_LOOP 1043 Controls whether outgoing multicast packets sent should 1044 be delivered back to the local application. A toggle. 1045 If the option is set to 1, multicast packets are looped 1046 back. If it is set to 0, they are not. 1048 Argument type: unsigned int 1050 The reception of multicast packets is controlled by the two setsockopt() 1051 options summarized below: 1053 IPV6_ADD_MEMBERSHIP 1055 Join a multicast group. Requests that multicast packets 1056 sent to a particular multicast address be delivered to 1057 this socket. The argument is the IPv6 multicast address 1058 of the group to join. 1060 Argument type: struct ipv6_mreq 1062 IPV6_DROP_MEMBERSHIP 1064 Leave a multicast group. Requests that multicast 1065 packets sent to a particular multicast address no longer 1066 be delivered to this socket. The argument is the IPv6 1067 multicast address of the group to join. 1069 Argument type: struct ipv6_mreq 1071 The argument type of both of these options is the ipv6_mreq structure, 1072 which is defined as follows: 1074 struct ipv6_mreq { 1075 /* IPv6 multicast address of group */ 1076 struct in6_addr ipv6mr_multiaddr; 1078 /* local IPv6 address of interface */ 1079 struct in6_addr ipv6mr_interface; 1080 }; 1082 5. Library Functions 1084 New library functions are needed to lookup IPv6 addresses in the name 1085 service, and to manipulate IPv6 addresses. 1087 5.1. Name-to-Address Translation Functions 1089 Two new functions analogous to gethostbyname() and gethostbyaddr() have 1090 been defined which support both IPv4 and IPv6 addresses. The names of 1091 the new functions are hostname2addr() and addr2hostname(). These 1092 functions were designed to have semantics similar to gethostbyname() and 1093 gethostbyaddr(), so that existing IPv4 applications can be easily ported 1094 to IPv6. 1096 The new functions differ from the old in one important way that is not 1097 related to IPv6: The old functions could not safely be used by 1098 multi-threaded applications, while the new ones can. There are two 1099 multi-threading problems with the old functions. First, the return 1100 value of the old functions is a pointer to a single static buffer 1101 belonging to the library. The new functions return a dynamically 1102 allocated buffer, and a third new function, named freehostent(), is 1103 provided to free that storage. Second, the old functions returned their 1104 error code in a global variable (h_errno). The new functions carry a 1105 pointer that allows the library to return the error code into storage 1106 provided by the caller. 1108 The hostname2addr() function is similar to gethostbyname(), but enables 1109 applications to specify the type of address to be looked up: 1111 struct hostent *hostname2addr( 1112 const char *name, 1113 int af, 1114 int *error); 1116 This function looks up the hostname argument name in the name service 1117 and, if the lookup succeeds, returns a completed hostent structure. If 1118 the lookup fails, the function returns NULL and an error code is 1119 returned in the buffer pointed to by the argument error. The af argument 1120 specifies the type of the address -- IPv4 (AF_INET) or IPv6 (AF_INET6) 1121 -- to return to the caller in the h_addr_list field of the hostent 1122 structure. 1124 If the af argument is AF_INET, hostname2addr() behaves much like 1125 gethostbyname. It queries the name service for IPv4 addresses and, if 1126 any are found, returns a hostent structure that includes an array of 1127 IPv4 addresses. Each IPv4 address is encoded in network byte order. 1129 If the af argument is AF_INET6, hostname2addr() queries the name service 1130 for IPv6 addresses. The function may also query the name service for 1131 IPv4 records. If this is done, any IPv4 addresses found are returned to 1132 the application encoded as IPv4-compatible IPv6 addresses. The 1133 determination of whether to query for IPv4 addresses is system specific. 1134 Systems that support querying for IPv4 addresses should provide a 1135 system-wide configuration switch allowing the system administrator to 1136 enable or disable that feature. 1138 IPv6 addresses returned by the hostname2addr() function are encoded in 1139 network byte order. 1141 The second new function, called addr2hostname(), is like the 1142 gethostbyaddr() function, but supports both the IPv4 and IPv6 address 1143 families: 1145 struct hostent *addr2hostname( 1146 const void *addr, 1147 int addrlen, 1148 int af, 1149 int *error); 1151 The addr2hostname() function performs an address-to-name lookup on the 1152 address specified by the addr argument, returning a completed hostent 1153 structure if the lookup succeeds. If the lookup fails, the function 1154 returns NULL and an error code is returned in the buffer pointed to by 1155 the argument error. 1157 The addrlen argument specifies the length of the address (in octets) 1158 pointed to by the addr argument. 1160 The af argument specifies the address family of the addr argument. This 1161 function supports both the AF_INET and AF_INET6 address families. If 1162 the af argument is AF_INET, then addr refers to an IPv4 address and 1163 addrlen must have the value 4. If af is AF_INET6, addr represents an 1164 IPv6 address and addrlen must have the value 16. In the latter case, 1165 the caller may present an IPv4-mapped IPv6 address in the addr argument. 1166 If this is done, an IPv4 address-to-name lookup is performed on the 1167 embedded IPv4 address. 1169 IPv6 addresses are returned by hostname2addr() and addr2hostname() via 1170 the hostent structure. This h_addr_list element of this structure 1171 points to an array of ipv6_hostent_addr type structures. This 1172 structure is declared as follows: 1174 struct ipv6_hostent_addr { 1175 /* amount of time in seconds that address is valid for */ 1176 u_int32_t iha_lifetime; 1178 /* the IPv6 address being returned */ 1179 struct in6_addr iha_addr; 1180 }; 1182 The hostent and address structures returned by both of these functions 1183 is allocated by the library. Applications use the freehostent() 1184 function to return this storage to the library after they are done using 1185 it: 1187 void freehostent( 1188 struct hostent *hp); 1190 Applications may not access the hostent structure after they have 1191 returned it to the library. 1193 Another new name-to-address translation library function is now under 1194 development at Berkeley. This new function, named getconninfo(), will 1195 subsume the functionality of gethostbyname(), hostname2addr(), as well 1196 as the getservbyname() and getservbyport() functions. The new function 1197 is specifically designed to be "transport independent", so it should be 1198 directly usable by IPv6 applications. 1200 System implementations should provide the addr2hostname() and 1201 hostname2addr() functions in order to simplify the porting of existing 1202 IPv4 applications to IPv6. System implementations may also provide the 1203 getconninfo() function, once it is defined, so that newly written 1204 applications can be transport independent. 1206 The specification of the getconninfo() function is published as a 1207 separate document [2], not included in this spec. 1209 Implementations must retain the BSD gethostbyname() and gethostbyaddr() 1210 functions in order to provide source and binary compatibility for 1211 existing applications. 1213 Applications obtain the function prototype declarations for 1214 hostname2addr() and addr2hostname() by including the header file 1215 . 1217 5.3. Address Conversion Functions 1219 BSD Unix provides two functions, inet_addr() and inet_ntoa(), to convert 1220 an IPv4 address between binary and printable form. IPv6 applications 1221 need similar functions. The following two functions convert both IPv6 1222 and IPv4 addresses: 1224 int ascii2addr( 1225 int af, 1226 const char *cp, 1227 void *ap); 1229 and 1231 char *addr2ascii( 1232 int af, 1233 const void *ap, 1234 int len, 1235 char *cp); 1237 The first function converts an ascii string to an address in the address 1238 family specified by the af argument. Currently AF_INET and AF_INET6 1239 address families are supported. The cp argument points to the ascii 1240 string being passed in. The ap argument points to a buffer into which 1241 the function stores the address. Ascii2addr() returns the length of the 1242 address in octets if the conversion succeeds, and -1 otherwise. The 1243 function does not modify the storage pointed to by ap if the conversion 1244 fails. The application must ensure that the buffer referred to by ap is 1245 large enough to hold the converted address. 1247 If the af argument is AF_INET, the function accepts a string in the 1248 standard IPv4 dotted decimal form: 1250 ddd.ddd.ddd.ddd 1252 where ddd is a one to three digit decimal number between 0 and 255. 1254 If the af argument is AF_INET6, then the function accepts a string in 1255 one of the standard IPv6 printing forms defined in the addressing 1256 architecture specification [3]. 1258 The second function converts an address into a printable string. The af 1259 argument specifies the form of the address. This can be AF_INET or 1260 AF_INET6. The ap argument points to a buffer holding an IPv4 address if 1261 the af argument is AF_INET, and an IPv6 address if the af argument is 1262 AF_INET6. The len field specifies the length in octets of the address 1263 pointed to by ap, and must be 4 if af is AF_INET, or 16 if af is 1264 AF_INET6. The cp argument points to a buffer that the function can use 1265 to store the ascii string. If the cp argument is NULL, the function 1266 uses its own private static buffer. If the application specifies a cp 1267 argument, it must be large enough to hold the ascii conversion of the 1268 address specified as an argument, including the terminating null octet. 1269 For IPv6 addresses, the buffer must be at least 46-octets. For IPv4 1270 addresses, the buffer must be at least 16-octets. In order to allow 1271 applications to easily declare buffers of the proper size to store IPv4 1272 and IPv6 addresses in string form, implementations should provide the 1273 following constants, made available to applications that include 1274 : 1276 #define INET_ADDRSTRLEN 16 1277 #define INET6_ADDRSTRLEN 46 1279 The addr2ascii() function returns a pointer to the buffer containing the 1280 ascii string if the conversion succeeds, and NULL otherwise. The 1281 function does not modify the storage pointed to by cp if the conversion 1282 fails. 1284 Applications obtain the prototype declarations for addr2ascii() and 1285 ascii2addr() by including the header file . 1287 5.3. Embedded IPv4 Addresses 1289 The IPv4-mapped IPv6 address format is used to represent IPv4 addresses 1290 as IPv6 addresses. Most applications should be able to to manipulate 1291 IPv6 addresses as opaque 16-octet quantities, without needing to know 1292 whether they represent IPv4 addresses. However, a few applications may 1293 need to determine whether an IPv6 address is an IPv4-mapped address or 1294 not. The following function is provided for those applications: 1296 int inet6_isipv4addr (const struct in6_addr *addr); 1298 The "addr" argument to this function points to a buffer holding an IPv6 1299 address in network byte order. The function returns true (non-zero) if 1300 that address is an IPv4-mapped address, and returns 0 otherwise. 1302 This function could be used by server applications to determine whether 1303 the peer is an IPv4 node or an IPv6 node. After accepting a TCP 1304 connection via accept(), or receiving a UDP packet via recvfrom(), the 1305 application can apply the inet6_isipv4addr() function to the returned 1306 address. 1308 Applications obtain the prototype for this function by including the 1309 header file . 1311 6. Security Considerations 1312 IPv6 provides a number of new security mechanisms, many of which need to 1313 be accessible to applications. A companion document detailing the 1314 extensions to the socket interfaces to support IPv6 security is being 1315 written [4]. At some point in the future, that document and this one 1316 may be merged into a single API specification. 1318 7. Change History 1320 Changes from the November 1995 Edition 1322 - Added the symbolic constants IPV6ADDR_ANY_INIT and 1323 IPV6ADDR_LOOPBACK_INIT for applications to use for 1324 initializations. 1326 - Eliminated restrictions on the value of ipv6addr_any. Systems 1327 may now choose any value, including all-zeros. 1329 - Added a mechanism for returning time to live with the address in 1330 the name-to-address translation functions. 1332 - Added a mechanism for applications to specify the interface in 1333 the setsockopt() options to join and leave a multicast group. 1335 Changes from the July 1995 Edition 1337 - Changed u_long and u_short types in structures to u_int32_t and 1338 u_int16_t for consistency and clarity. 1340 - Added implementation-provided constants for IPv4 and IPv6 text 1341 address buffer length. 1343 - Defined a set of constants for subfields of sin6_flowid and for 1344 priority values. 1346 - Defined constants for getting and setting the source route flag. 1348 - Define where ansi prototypes for hostname2addr(), 1349 addr2hostname(), addr2ascii(), ascii2addr(), and 1350 ipv6_isipv4addr() reside. 1352 - Clarified the include file requirements. Say that the structure 1353 definitions are defined as a result of including the header file 1354 , not that the structures are necessarily defined 1355 there. 1357 - Removed underscore chars from is_ipv4_addr() function name for 1358 BSD compatibility. 1360 - Added inet6_ prefix to is_ipv4_addr() function name to avoid 1361 name space conflicts. 1363 - Changes setsockopt option naming convention to use IPV6_ prefix 1364 instead of IP_ so that there is clearly no ambiguity with IPv4 1365 options. Also, use level IPPROTO_IPV6 for these options. 1367 - Made hostname2addr() and addr2hostname() functions thread-safe. 1369 - Added support for sendmsg() and recvmsg() in source routing 1370 section. 1372 - Changed in_addr6 to in6_addr for consistency. 1374 - Re-structured document into sub-sections. 1376 - Deleted the implementation experience section. It was too 1377 wordy. 1379 - Added argument types to multicast socket options. 1381 - Added constant for largest source route array buffer. 1383 - Added the freehostent() function. 1385 - Added receiving interface determination and sending interface 1386 selection options. 1388 - Added definitions of ipv6addr_any and ipv6addr_loopback. 1390 - Added text making the lookup of IPv4 addresses by 1391 hostname2addr() optional. 1393 Changes from the June 1995 Edition 1395 - Added capability for application to select loose or strict 1396 source routing. 1398 Changes from the March 1995 Edition 1400 - Changed the definition of the ipv6_addr structure to be an array 1401 of sixteen chars instead of four longs. This change is 1402 necessary to support machines which implement the socket 1403 interface, but do not have a 32-bit addressable word. Virtually 1404 all machines which provide the socket interface do support an 1405 8-bit addressable data type. 1407 - Added a more detailed explanation that the data types defined in 1408 this documented are not intended to be hard and fast 1409 requirements. Systems may use other data types if they wish. 1411 - Added a note flagging the fact that the sockaddr_in6 structure 1412 is not the same size as the sockaddr structure. 1414 - Changed the sin6_flowlabel field to sin6_flowinfo to accommodate 1415 the addition of the priority field to the IPv6 header. 1417 Changes from the October 1994 Edition 1419 - Added variant of sockaddr_in6 for 4.4 BSD-based systems (sa_len 1420 compatibility). 1422 - Removed references to SIT transition specification, and added 1423 reference to addressing architecture document, for definition of 1424 IPv4-mapped addresses. 1426 - Added a solution to the problem of the application not providing 1427 enough buffer space to hold a received source route. 1429 - Moved discussion of IPv4 applications interoperating with IPv6 1430 nodes to open issues section. 1432 - Added length parameter to addr2ascii() function to be consistent 1433 with addr2hostname(). 1435 - Changed IP_MULTICAST_TTL to IP_MULTICAST_HOPS to match IPv6 1436 terminology, and added IP_UNICAST_HOPS option to match 1437 IP_MULTICAST_HOPS. 1439 - Removed specification of numeric values for AF_INET6, 1440 IP_ADDRFORM, and IP_RCVSRCRT, since they need not be the same on 1441 different implementations. 1443 - Added a definition for the in_addr6 IPv6 address data 1444 structure. Added this so that applications could use 1445 sizeof(struct in_addr6) to get the size of an IPv6 address, 1446 and so that a structured type could be used in the 1447 is_ipv4_addr(). 1449 8. Open Issues 1451 A few open issues for IPv6 socket interface API specification remain, 1452 including: 1454 - Should we add a timeout parameter to hostname2addr() and 1455 addr2hostname()? DNS lookups need to be given some finite 1456 timeout interval, so it might be nice to let the application 1457 specify that interval. 1459 - Can the IPV6_ADDRFORM option really be implemented? 1461 - An interface is needed to allocate flow labels. Should one be 1462 defined in this spec, or left for another document? 1464 - Can existing IPv4 applications interoperate with IPv6 nodes? 1465 This issue is discussed in more detail in the following section. 1467 8.1. IPv4 Applications Interoperating with IPv6 Nodes 1469 This problem primarily has to do with the how IPv4 applications 1470 represent addresses of IPv6 nodes. What address should be returned to 1471 the application when an IPv6/UDP packet is received, or an IPv6/TCP 1472 connection is accepted? The peer's address could be any arbitrary 1473 128-bit IPv6 address. But the application is only equipped to deal with 1474 32-bit IPv4 addresses encoded in sockaddr_in data structures. 1476 We have not discovered any solution that provides complete transparent 1477 interoperability with IPv6 nodes for applications using the original 1478 IPv4 API. However, two techniques that partially solve the problem are: 1480 1) Prohibit communication between IPv4 applications and IPv6 nodes. 1481 Only UDP packets received from IPv4 nodes would be passed up to 1482 the application, and only TCP connections received from IPv4 1483 nodes would be accepted. UDP packets from IPv6 nodes would be 1484 dropped, and TCP connections from IPv6 nodes would be refused. 1486 2) The system could generate a local 32-bit cookie to represent the 1487 full 128-bit IPv6 address, and pass this value to the 1488 application. The system would maintain a mapping from cookie 1489 value into the 128-bit IPv6 address that it represents. When 1490 the application passed a cookie back into the system (for 1491 example, in a sendto() or connect() call) the system would use 1492 the 128-bit IPv6 address that the cookie represents. 1494 The cookie would have to be chosen so as to be an invalid IPv4 1495 address (e.g. an address on net 127.0.0.0), and the system would 1496 have to make sure that these cookie values did not escape into 1497 the Internet as the source or destination addresses of IPv4 1498 packets. 1500 Both of these techniques have drawbacks. This is an area for further 1501 study. System implementors may use one of these techniques or implement 1502 another solution. 1504 Acknowledgments 1506 Thanks to the many people who made suggestions and provided feedback to 1507 to the numerous revisions of this document, including: Ran Atkinson, 1508 Fred Baker, Dave Borman, Andrew Cherenson, Alex Conta, Alan Cox, Steve 1509 Deering, Francis Dupont, Robert Elz, Marc Hasson, Tom Herbert, Christian 1510 Huitema, Wan-Yen Hsu, Alan Lloyd, Charles Lynn, Dan McDonald, Craig 1511 Metz, Erik Nordmark, Josh Osborne, Richard Stevens, Matt Thomas, Dean D. 1512 Throop, Glenn Trewitt, David Waitzman, and Carl Williams. Craig 1513 Partridge suggested the addr2ascii() and ascii2addr() functions. 1515 Ramesh Govindan made a number of contributions and co-authored an 1516 earlier version of this paper. 1518 References 1520 [1] R. Hinden. "Internet Protocol, Version 6 (IPv6) Specification". 1521 Internet Draft. June 1995. 1523 [2] Keith Sklower. "Getconninfo(): An alternative to Gethostbyname()" 1524 Internet Draft. June 1995. 1526 [3] R. Hinden., S. Deering. "IP Version 6 Addressing Architecture". 1527 Internet Draft. June 1995. 1529 [4] D. McDonald. "IPv6 Security API for BSD Sockets". Internet 1530 Draft. January 1995. 1532 Authors' Address 1534 Jim Bound 1535 Digital Equipment Corporation 1536 110 Spitbrook Road ZK3-3/U14 1537 Nashua, NH 03062-2698 1538 Phone: +1 603 881 0400 1539 Email: bound@zk3.dec.com 1541 Susan Thomson 1542 Bell Communications Research 1543 MRE 2P-343, 445 South Street 1544 Morristown, NJ 07960 1545 Telephone: +1 201 829 4514 1546 Email: set@thumper.bellcore.com 1548 Robert E. Gilligan 1549 Mailstop MPK 17-202 1550 Sun Microsystems, Inc. 1551 2550 Garcia Avenue 1552 Mountain View, CA 94043-1100 1553 Phone: +1 415 786 5151 1554 Email: gilligan@eng.sun.com