idnits 2.17.1 draft-ietf-ipngwg-bsd-api-03.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-25) 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 12 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 (November 21, 1995) is 10383 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 931, but not defined -- Looks like a reference, but probably isn't: 'N-1' on line 939 -- Looks like a reference, but probably isn't: 'N' on line 943 -- 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 November 21, 1995 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 May 21, 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 to select the source address for them. In IPv4, this 487 is 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 A symbolic constant can not be used for IPv6 because the address is 492 not a scalar type. Instead, the system provides a global variable 493 holding the distinguished IPv6 address that can be used in the bind() 494 call to instruct the system to select the source IPv6 address. The 495 global variable is an in6_addr type structure named "ipv6addr_any." 496 The extern declaration for this is: 498 extern struct in6_addr ipv6addr_any; 500 Applications use ipv6addr_any similarly to the way they use INADDR_ANY 501 in IPv4. For example, to bind a socket to port number 23, but let the 502 system select the source address, an application could use the 503 following code: 505 struct sockaddr_in6 sin6; 506 . . . 507 sin6.sin6_family = AF_INET6; 508 sin6.sin6_flowinfo = 0; 509 sin6.sin6_port = htons(23); 510 sin6.sin6_addr = ipv6addr_any; 511 . . . 512 if (bind(s, (struct sockaddr *) &sin6, sizeof(sin6)) == -1) 513 . . . 515 Note that the value of ipv6addr_any can not be the all-zeros IPv6 516 address, since that can be used as a valid IPv6 source address in some 517 cases. 519 3.10. Communicating with Local Services 521 Applications may need to send UDP packets to, or originate TCP 522 connections to, services residing on the local node. In IPv4, they can 523 do this by using the constant IPv4 address INADDR_LOOPBACK in their 524 connect(), sendto(), or sendmsg() call. 526 For IPv6, the system provides a global variable holding a distinguished 527 IPv6 address that can be used to contact local TCP and UDP services. 528 This variable is an in6_addr type structure named "ipv6addr_loopback." 529 The extern declaration for this variable is: 531 extern struct in6_addr ipv6addr_loopback; 533 Applications use ipv6addr_loopback as they would use INADDR_LOOPBACK 534 in IPv4 applications. For example, to open a TCP connection to the 535 local telnet server, an application could use the following code: 537 struct sockaddr_in6 sin6; 538 . . . 539 sin6.sin6_family = AF_INET6; 540 sin6.sin6_flowinfo = 0; 541 sin6.sin6_port = htons(23); 542 sin6.sin6_addr = ipv6addr_loopback; 543 . . . 544 if (connect(s, (struct sockaddr *) &sin6, sizeof(sin6)) == -1) 545 . . . 547 4. Socket Options 549 A number of new socket options are defined for IPv6. All of these new 550 options are at the IPPROTO_IPV6 level. That is, the "level" parameter 551 in the getsockopt() and setsockopt() call is IPPROTO_IPV6 when using 552 these options. The constant name prefix IPV6_ is used in all of the new 553 socket options. This serves to clearly identify these options as 554 applying to IPv6. 556 The macro declaration for IPPROTO_IPV6, the new IPv6 socket options, and 557 related constants defined in this section are obtained by including the 558 header file 560 4.1 Changing Socket Type 562 Unix allows open sockets to be passed between processes via the exec() 563 call and other means. It is a relatively common application practice to 564 pass open sockets across exec() calls. Thus it is possible for an 565 application using the original API to pass an open PF_INET socket to an 566 application that is expecting to receive a PF_INET6 socket. Similarly, 567 it is possible for an application using the extended API to pass an open 568 PF_INET6 socket to an application using the original API, which would be 569 equipped only to deal with PF_INET sockets. Either of these cases could 570 cause problems, because the application which is passed the open socket 571 might not know how to decode the address structures returned in 572 subsequent socket functions. 574 To remedy this problem, a new setsockopt() option is defined that allows 575 an application to "transform" a PF_INET6 socket into a PF_INET socket 576 and vice-versa. 578 An IPv6 application that is passed an open socket from an unknown 579 process may use the IPV6_ADDRFORM setsockopt() option to "convert" the 580 socket to PF_INET6. Once that has been done, the system will return 581 sockaddr_in6 address structures in subsequent socket functions. 582 Similarly, an IPv6 application that is about to pass an open PF_INET6 583 socket to a program that may not be IPv6 capable may "downgrade" the 584 socket to PF_INET before calling exec(). After that, the system will 585 return sockaddr_in address structures to the application that was 586 exec()'ed. 588 The IPV6_ADDRFORM option is at the IPPROTO_IP level. The only valid 589 option values are PF_INET6 and PF_INET. For example, to convert a 590 PF_INET6 socket to PF_INET, a program would call: 592 int addrform = PF_INET; 594 if (setsockopt(s, IPPROTO_IPV6, IPV6_ADDRFORM, (char *) &addrform, 595 sizeof(addrform)) == -1) 596 perror("setsockopt IPV6_ADDRFORM"); 598 An application may use IPV6_ADDRFORM in the getsockopt() function to 599 learn whether an open socket is a PF_INET of PF_INET6 socket. For 600 example: 602 int addrform; 603 int len = sizeof(int); 605 if (getsockopt(s, IPPROTO_IPV6, IPV6_ADDRFORM, (char *) &addrform, 606 &len) == -1) 607 perror("getsockopt IPV6_ADDRFORM"); 608 if (addrform == PF_INET) 609 printf("This is an IPv4 socket.\n"); 610 else if (addrform == PF_INET6) 611 printf("This is an IPv6 socket.\n"); 612 else 613 printf("This system is broken.\n"); 615 4.2. Handling IPv6 Source Routes 616 IPv6 makes more use of the source routing mechanism than IPv4. In order 617 for source routing to operate properly, the node receiving a request 618 packet that bears a source route must reverse that source route when 619 sending the reply. In the case of TCP, the reversal can be done in the 620 transport protocol implementation transparently to the application. But 621 in the case of UDP, the application must perform the reversal itself. 622 The transport protocol code can not perform the reversal for UDP packets 623 because a UDP application may receive a number of requests and generate 624 replies asynchronously. A "reply" sent by an application may not match 625 the "request" most recently passed up to the application. 627 The API for source routing has two components: providing a source route 628 to be used with originated traffic -- actively opened TCP connections 629 and UDP packets being sent; and retrieving the source route of received 630 traffic -- passively accepted TCP connections and received UDP packets. 631 An application may always provide a source route with TCP connections 632 being originated and UDP packets being sent. But to receive source 633 routes, the application must enable an option. 635 To provide a source route, an application simply provides an array of 636 sockaddr_in6 data structures in the msg_name field of the msghdr 637 structure of a sendmsg() function, or the address argument of the 638 sendto() function (when sending a UDP packet), or the address argument 639 of the connect() function (when actively opening a TCP connection). For 640 sendto() and connect(), the length argument of the function is the total 641 length, in octets, of the array. For sendmsg(), the msg_namelen field 642 of the msghdr structure specifies the total length of the array. The 643 elements of the array represent the full source route, including both 644 source and destination endpoint address. The elements of the array are 645 ordered from destination to source. That is, the first element of the 646 array represents the destination endpoint address, and the last element 647 of the array represents the source endpoint address. If the application 648 provides a source route, the source endpoint address can not be omitted. 649 The sin6_addr field of the source endpoint address may be set to zero, 650 however, in which case the system will select an appropriate source 651 address. The sin6_port field of the destination endpoint address must 652 be assigned. The sin6_port field of the source endpoint address may be 653 set to zero, in which case the system will select an appropriate source 654 port number. The sin6_port fields of the intermediate addresses must be 655 set to zero. 657 The application also has control over the loose/strict source routing 658 flag that is defined in the IPv6 specification [1]. It does this by 659 setting or clearing the loose/strict flag contained in the sin6_flowinfo 660 field of the destination and intermediate addresses. On the receive 661 side, the implementation uses the loose/strict flag in the address array 662 returned to the application to indicate the loose/strict status of each 663 hop. 665 The implementation provides a set of constant definitions to simplify 666 getting and setting the loose/strict flag for each of the hops of a 667 source route. The following constant is used to select the loose/strict 668 flag from the sin6_flowinfo field: 670 IPV6_FLOWINFO_SRFLAG 672 In addition, two constants are provided which represent the two states 673 of this flag: 675 IPV6_SRFLAG_STRICT 676 IPV6_SRFLAG_LOOSE 678 These constants can be used to inspect the source route flags of 679 received addresses, for example: 681 struct sockaddr_in6 sin6[3]; 682 . . . 683 if ((sin6[0].sin6_flowinfo & IPV6_FLOWINFO_SRFLAG) == 684 IPV6_SRFLAG_STRICT) 685 . . . 687 And they can also be used to set the source route flags: 689 struct sockaddr_in6 sin6[3]; 690 . . . 691 sin6[0].sin6_flowinfo = 692 (sin6[0].sin6_flowinfo & ~IPV6_FLOWINFO_SRFLAG) | 693 IPV6_SRFLAG_STRICT; 695 The flow label and priority sub-fields of the sin6_flowinfo field of the 696 destination endpoint address may be set, but the these fields must be 697 set to zero in the intermediate and source endpoint address. 699 The arrangement of the address structures in the address buffer passed 700 to sendmsg(), connect() or sendto() is shown in the figure below: 702 +--------------------+ 703 | | 704 | sockaddr_in6[0] | Destination Endpoint Address 705 | | 706 +--------------------+ 707 | | 708 | sockaddr_in6[1] | Last Source-Route Hop Address 709 | | 710 +--------------------+ 711 . . 712 . . 713 . . 714 +--------------------+ 715 | | 716 | sockaddr_in6[N-1] | First Source-Route Hop Address 717 | | 718 +--------------------+ 719 | | 720 | sockaddr_in6[N] | Source Endpoint Address 721 | | 722 +--------------------+ 724 Address buffer when sending a source route 726 The IPV6_RECVSRCRT setsockopt() option controls the reception of source 727 routes. The option is disabled by default. Applications must 728 explicitly enable the option using the setsockopt() function in order to 729 receive source routes. 731 The IPV6_RECVSRCRT option is at the IPPROTO_IPV6 level. An example of 732 how an application might use this option is: 734 int on = 1; /* value == 1 means enable the option */ 736 if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVSRCRT, (char *) &on, 737 sizeof(on)) == -1) 738 perror("setsockopt IPV6_RECVSRCRT"); 740 When the IPV6_RECVSRCRT option is disabled, only a single sockaddr_in6 741 address structure is returned to applications in the address argument of 742 the recvfrom() and accept() functions. This address represents the 743 source endpoint address of the UDP packet received or the TCP connection 744 accepted. 746 When the IPV6_RECVSRCRT option is enabled, the msg_name field of the 747 msghdr of the recvmsg() function, or the address argument of the 748 recvfrom() function (when receiving UDP packets) and the accept() 749 functions (when passively accepting TCP connections) points to an array 750 of sockaddr_in6 structures. When the function returns, the array will 751 hold two elements -- source and destination address -- when the received 752 UDP packet or TCP SYN packet does not carry a source route. The array 753 will hold more than two elements when the received packet carries a 754 source route. 756 The addresses in the array are ordered from source to destination. That 757 is, the first element of the array holds source endpoint address of the 758 received packet. Following this in the array are the intermediate hops 759 in the order in which they were visited. The last element of the array 760 holds the destination endpoint address. Note that this is the opposite 761 of the order specified for sending. This ordering was chosen so that 762 the address array returned in a recvmsg() or recvfrom() call can be used 763 in a subsequent sendmsg() or sendto() call without requiring the 764 application to re-order the addresses in the array. Similarly, the 765 address array received in an accept() call can be used unchanged in a 766 subsequent connect() call. 768 The address length argument of the recvfrom() and accept() functions, 769 and the msg_namelen field of the msghdr field in the recvmsg() function, 770 indicate the length, in octets, of the full address array. This 771 argument is a value-result parameter. The application sets the maximum 772 size of the address buffer when it makes the call, and the system 773 modifies the value to return the actual size of the buffer to the 774 application. 776 The sin6_port field of the first and last array elements (source and 777 destination endpoint address) will hold the source and destination UDP 778 or TCP port number of the received packet. The sin6_port field of the 779 intermediate elements of the array will be zero. 781 The flow label and priority sub-fields of the sin6_flowinfo field of the 782 source endpoint address will hold the flow label and priority values of 783 the received packet. The flow label and priority sub-fields of the 784 intermediate addresses and the destination endpoint address will be 785 zero. The loose/strict flag of the sin6_flowinfo field of the source 786 endpoint address and the intermediate addresses will be set according to 787 the flags in the received packet. The macros defined above can be used 788 to inspect the loose/strict flag of each hop. 790 The address buffer returned to the application in the recvfrom() or 791 accept() functions when the IPV6_RECVSRCRT option is enabled is shown 792 below: 794 +--------------------+ 795 | | 796 | sockaddr_in6[0] | Source Endpoint Address 797 | | 798 +--------------------+ 799 | | 800 | sockaddr_in6[1] | First Source-Route Hop Address 801 | | 802 +--------------------+ 803 . . 804 . . 805 . . 806 +--------------------+ 807 | | 808 | sockaddr_in6[N-1] | Last Source-Route Hop Address 809 | | 810 +--------------------+ 811 | | 812 | sockaddr_in6[N] | Destination Endpoint Address 813 | | 814 +--------------------+ 816 Address buffer when receiving a source route 818 IPv6 allows a source route with up to 23 intermediate hops. Since the 819 it must also receive the source and destination endpoint addresses, the 820 application must provide a buffer capable of holding 25 addresses to 821 receive such a source route. Implementations provide the following 822 constant declaration in order to allow applications to simply declare 823 storage for the largest possible source route: 825 IPV6_SR_MAXADDR 827 Applications can use this constant like this: 829 struct sockaddr_in6 sin6[IPV6_SR_MAXADDR]; 831 It may be impractical for some applications to allocate space to hold 832 the largest possible source route. Thus a received source route may be 833 too large to fit into the buffer provided by the application. In this 834 circumstance, the system should return only a single address element -- 835 the source endpoint address -- to the application. This case is clearly 836 distinguishable to the application because in all other cases, the 837 system returns at least two address elements -- the source and 838 destination endpoint addresses. 840 4.3. Receiving Interface Determination 841 Some applications run on multi-homed hosts need to determine which 842 interface UDP packets were received on or TCP connections are bound to. 843 While the source routing interface described in the previous section 844 returns the destination address of the packet, this does not necessarily 845 identify the receiving interface. Some cases where it does not are: 847 - When the received packet is multicast. The destination address 848 in this case is an IPv6 multicast address, not the address of an 849 interface. 851 - When the node is operating as an IPv6 router. The node may 852 receive packets on interfaces other than the one they are 853 addressed to. 855 - When the received packet is sent to the node's link-local 856 address which is being used on multiple interfaces. 858 The address of the receiving interface is returned to the application 859 similarly to the way that source routes are returned. A new 860 setsockopt() option named IPV6_RECVIF is provided at the IPPROTO_IPV6 861 level. If this option is enabled, the system returns an additional 862 sockaddr_in6 structure to the application, holding the IPv6 address of 863 the receiving interface, in the recvfrom(), recvmsg(), or accept() 864 functions. 866 The option is enabled like this: 868 int on = 1; /* value == 1 means enable the option */ 870 if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVIF, (char *) &on, 871 sizeof(on)) == -1) 872 perror("setsockopt IPV6_RECVIF"); 874 This option can be used in conjunction with the IP_RECVSRCRT option. 875 When the IPV6_RECVIF option is enabled, the buffer returned to the 876 application is structured like this: 878 +--------------------+ - - - - - - - - - - - - - - - 879 | | 880 | sockaddr_in6[0] | 881 | | 882 +--------------------+ 883 . . Source Address, or 884 . . Full Source Route 885 . . 886 +--------------------+ 887 | | 888 | sockaddr_in6[N-1] | 889 | | 890 +--------------------+ - - - - - - - - - - - - - - - 891 | | 892 | sockaddr_in6[N] | Receiving Interface Address 893 | | 894 +--------------------+ - - - - - - - - - - - - - - - 896 Address buffer with receiving interface address 898 The last address in the array is an IPv6 address of the receiving 899 interface. Since interfaces in IPv6 may have more than one address, and 900 some addresses (e.g. link-local addresses) may be used on more than one 901 interface, the system should select an address that uniquely identifies 902 the interface. 904 4.4. Sending Interface Specification 906 Applications may also need to specify the outgoing interface that 907 originated UDP or TCP packets should use. This is accomplished like 908 source route selection. The application may provide an additional 909 sockaddr_in6 structure in its sendto(), sendmsg() or connect() call 910 specifying the address of the outgoing interface. Unlike source route 911 selection, the outgoing interface address can only be included if a new 912 option is enabled. The new option is needed so that the system can 913 differentiate between the application's specification of an outgoing 914 interface address and a source route. 916 The new option option is named IPV6_SENDIF and is at the IPPROTO_IPV6 917 level. It can be enabled like this: 919 int on = 1; /* value == 1 means enable the option */ 921 if (setsockopt(s, IPPROTO_IPV6, IPV6_SENDIF, (char *) &on, 922 sizeof(on)) == -1) 923 perror("setsockopt IPV6_SENDIF"); 925 This option can be used in conjunction with source route specification. 926 If this option is enabled, the application passes in an address array 927 structured as follows: 929 +--------------------+ - - - - - - - - - - - - - - - 930 | | 931 | sockaddr_in6[0] | 932 | | 933 +--------------------+ 934 . . Destination Address, or 935 . . Full Source Route 936 . . 937 +--------------------+ 938 | | 939 | sockaddr_in6[N-1] | 940 | | 941 +--------------------+ - - - - - - - - - - - - - - - 942 | | 943 | sockaddr_in6[N] | Sending Interface Address 944 | | 945 +--------------------+ - - - - - - - - - - - - - - - 947 Address buffer with sending interface address 949 The last address in the array is an IPv6 address of the sending 950 interface. Applications should use an address that uniquely identifies 951 the interface to use. 953 4.5. Unicast Hop Limit 955 A new setsockopt() option is used to control the hop limit used in 956 outgoing unicast IPv6 packets. The name of this option is 957 IPV6_UNICAST_HOPS, and it is used at the IPPROTO_IPV6 layer. The 958 following example illustrates how it is used: 960 int hoplimit = 10; 962 if (setsockopt(s, IPPROTO_IPV6, IPV6_UNICAST_HOPS, (char *) &hoplimit, 963 sizeof(hoplimit)) == -1) 964 perror("setsockopt IPV6_UNICAST_HOPS"); 966 When the IPV6_UNICAST_HOPS option is set with setsockopt(), the option 967 value given is used as the hop limit for all subsequent unicast packets 968 sent via that socket. If the option is not set, the system selects a 969 default value. 971 The IPV6_UNICAST_HOPS option may be used in the getsockopt() function to 972 determine the hop limit value that the system will use for subsequent 973 unicast packets sent via that socket. For example: 975 int hoplimit; 976 int len = sizeof(hoplimit); 978 if (getsockopt(s, IPPROTO_IPV6, IPV6_UNICAST_HOPS, (char *) &hoplimit, 979 &len) == -1) 980 perror("getsockopt IPV6_UNICAST_HOPS"); 981 else 982 printf("Using %d for hop limit.\n", hoplimit); 984 4.6. Sending and Receiving Multicast Packets 986 IPv6 applications may send UDP multicast packets by simply specifying an 987 IPv6 multicast address in the address argument of the sendto() function. 989 A few setsockopt options at the IPPROTO_IPV6 layer are used to control 990 some of the parameters of sending multicast packets. These options are 991 optional: applications may send multicast packets without using these 992 options. The setsockopt() options for controlling the sending of 993 multicast packets are summarized below: 995 IPV6_MULTICAST_IF 997 Set the interface to use for outgoing multicast packets. 998 The argument is an IPv6 address of the interface to use. 1000 Argument type: struct in6_addr 1002 IPV6_MULTICAST_HOPS 1004 Set the hop limit to use for outgoing multicast packets. 1005 (Note a separate option - IPV6_UNICAST_HOPS - is 1006 provided to set the hop limit to use for outgoing 1007 unicast packets.) 1009 Argument type: unsigned int 1011 IPV6_MULTICAST_LOOP 1013 Controls whether outgoing multicast packets sent should 1014 be delivered back to the local application. A toggle. 1015 If the option is set to 1, multicast packets are looped 1016 back. If it is set to 0, they are not. 1018 Argument type: unsigned int 1020 The reception of multicast packets is controlled by the two setsockopt() 1021 options summarized below: 1023 IPV6_ADD_MEMBERSHIP 1025 Join a multicast group. Requests that multicast packets 1026 sent to a particular multicast address be delivered to 1027 this socket. The argument is the IPv6 multicast address 1028 of the group to join. 1030 Argument type: struct in6_addr 1032 IPV6_DROP_MEMBERSHIP 1034 Leave a multicast group. Requests that multicast 1035 packets sent to a particular multicast address no longer 1036 be delivered to this socket. The argument is the IPv6 1037 multicast address of the group to join. 1039 Argument type: struct in6_addr 1041 5. Library Functions 1042 New library functions are needed to lookup IPv6 addresses in the name 1043 service, and to manipulate IPv6 addresses. 1045 5.1. Name-to-Address Translation Functions 1047 Two new functions analogous to gethostbyname() and gethostbyaddr() have 1048 been defined which support both IPv4 and IPv6 addresses. The names of 1049 the new functions are hostname2addr() and addr2hostname(). These 1050 functions were designed to have semantics similar to gethostbyname() and 1051 gethostbyaddr(), so that existing IPv4 applications can be easily ported 1052 to IPv6. 1054 The new functions differ from the old in one important way that is not 1055 related to IPv6: The old functions could not safely be used by 1056 multi-threaded applications, while the new ones can. There are two 1057 multi-threading problems with the old functions. First, the return 1058 value of the old functions is a pointer to a single static buffer 1059 belonging to the library. The new functions return a dynamically 1060 allocated buffer, and a third new function, named freehostent(), is 1061 provided to free that storage. Second, the old functions returned their 1062 error code in a global variable (h_errno). The new functions carry a 1063 pointer that allows the library to return the error code into storage 1064 provided by the caller. 1066 The hostname2addr() function is similar to gethostbyname(), but enables 1067 applications to specify the type of address to be looked up: 1069 struct hostent *hostname2addr( 1070 const char *name, 1071 int af, 1072 int *error); 1074 This function looks up the hostname argument name in the name service 1075 and, if the lookup succeeds, returns a completed hostent structure. If 1076 the lookup fails, the function returns NULL and an error code is 1077 returned in the buffer pointed to by the argument error. The af argument 1078 specifies the type of the address -- IPv4 (AF_INET) or IPv6 (AF_INET6) 1079 -- to return to the caller in the h_addr_list field of the hostent 1080 structure. 1082 If the af argument is AF_INET, hostname2addr() behaves much like 1083 gethostbyname. It queries the name service for IPv4 addresses and, if 1084 any are found, returns a hostent structure that includes an array of 1085 IPv4 addresses. Each IPv4 address is encoded in network byte order. 1087 If the af argument is AF_INET6, hostname2addr() queries the name service 1088 for IPv6 addresses. The function may also query the name service for 1089 IPv4 records. If this is done, any IPv4 addresses found are returned to 1090 the application encoded as IPv4-compatible IPv6 addresses. The 1091 determination of whether to query for IPv4 addresses is system specific. 1092 Systems that support querying for IPv4 addresses should provide a 1093 system-wide configuration switch allowing the system administrator to 1094 enable or disable that feature. 1096 IPv6 addresses returned by the hostname2addr() function are encoded in 1097 network byte order. 1099 The second new function, called addr2hostname(), is like the 1100 gethostbyaddr() function, but supports both the IPv4 and IPv6 address 1101 families: 1103 struct hostent *addr2hostname( 1104 const void *addr, 1105 int addrlen, 1106 int af, 1107 int *error); 1109 The addr2hostname() function performs an address-to-name lookup on the 1110 address specified by the addr argument, returning a completed hostent 1111 structure if the lookup succeeds. If the lookup fails, the function 1112 returns NULL and an error code is returned in the buffer pointed to by 1113 the argument error. 1115 The addrlen argument specifies the length of the address (in octets) 1116 pointed to by the addr argument. 1118 The af argument specifies the address family of the addr argument. This 1119 function supports both the AF_INET and AF_INET6 address families. If 1120 the af argument is AF_INET, then addr refers to an IPv4 address and 1121 addrlen must have the value 4. If af is AF_INET6, addr represents an 1122 IPv6 address and addrlen must have the value 16. In the latter case, 1123 the caller may present an IPv4-mapped IPv6 address in the addr argument. 1124 If this is done, an IPv4 address-to-name lookup is performed on the 1125 embedded IPv4 address. 1127 The hostent structure returned by both of these functions is allocated 1128 by the library. Applications use the freehostent() function to return 1129 the hostent structure to the library after they are done using it: 1131 void freehostent( 1132 struct hostent *hp); 1134 Applications may not access the hostent structure after they have 1135 returned it to the library. 1137 Another new name-to-address translation library function is now under 1138 development at Berkeley. This new function, named getconninfo(), will 1139 subsume the functionality of gethostbyname(), hostname2addr(), as well 1140 as the getservbyname() and getservbyport() functions. The new function 1141 is specifically designed to be "transport independent", so it should be 1142 directly usable by IPv6 applications. 1144 System implementations should provide the addr2hostname() and 1145 hostname2addr() functions in order to simplify the porting of existing 1146 IPv4 applications to IPv6. System implementations may also provide the 1147 getconninfo() function, once it is defined, so that newly written 1148 applications can be transport independent. 1150 The specification of the getconninfo() function is published as a 1151 separate document [2], not included in this spec. 1153 Implementations must retain the BSD gethostbyname() and gethostbyaddr() 1154 functions in order to provide source and binary compatibility for 1155 existing applications. 1157 Applications obtain the function prototype declarations for 1158 hostname2addr() and addr2hostname() by including the header file 1159 . 1161 5.3. Address Conversion Functions 1163 BSD Unix provides two functions, inet_addr() and inet_ntoa(), to convert 1164 an IPv4 address between binary and printable form. IPv6 applications 1165 need similar functions. The following two functions convert both IPv6 1166 and IPv4 addresses: 1168 int ascii2addr( 1169 int af, 1170 const char *cp, 1171 void *ap); 1173 and 1175 char *addr2ascii( 1176 int af, 1177 const void *ap, 1178 int len, 1179 char *cp); 1181 The first function converts an ascii string to an address in the address 1182 family specified by the af argument. Currently AF_INET and AF_INET6 1183 address families are supported. The cp argument points to the ascii 1184 string being passed in. The ap argument points to a buffer into which 1185 the function stores the address. Ascii2addr() returns the length of the 1186 address in octets if the conversion succeeds, and -1 otherwise. The 1187 function does not modify the storage pointed to by ap if the conversion 1188 fails. The application must ensure that the buffer referred to by ap is 1189 large enough to hold the converted address. 1191 If the af argument is AF_INET, the function accepts a string in the 1192 standard IPv4 dotted decimal form: 1194 ddd.ddd.ddd.ddd 1196 where ddd is a one to three digit decimal number between 0 and 255. 1198 If the af argument is AF_INET6, then the function accepts a string in 1199 one of the standard IPv6 printing forms defined in the addressing 1200 architecture specification [3]. 1202 The second function converts an address into a printable string. The af 1203 argument specifies the form of the address. This can be AF_INET or 1204 AF_INET6. The ap argument points to a buffer holding an IPv4 address if 1205 the af argument is AF_INET, and an IPv6 address if the af argument is 1206 AF_INET6. The len field specifies the length in octets of the address 1207 pointed to by ap, and must be 4 if af is AF_INET, or 16 if af is 1208 AF_INET6. The cp argument points to a buffer that the function can use 1209 to store the ascii string. If the cp argument is NULL, the function 1210 uses its own private static buffer. If the application specifies a cp 1211 argument, it must be large enough to hold the ascii conversion of the 1212 address specified as an argument, including the terminating null octet. 1213 For IPv6 addresses, the buffer must be at least 46-octets. For IPv4 1214 addresses, the buffer must be at least 16-octets. In order to allow 1215 applications to easily declare buffers of the proper size to store IPv4 1216 and IPv6 addresses in string form, implementations should provide the 1217 following constants, made available to applications that include 1218 : 1220 #define INET_ADDRSTRLEN 16 1221 #define INET6_ADDRSTRLEN 46 1223 The addr2ascii() function returns a pointer to the buffer containing the 1224 ascii string if the conversion succeeds, and NULL otherwise. The 1225 function does not modify the storage pointed to by cp if the conversion 1226 fails. 1228 Applications obtain the prototype declarations for addr2ascii() and 1229 ascii2addr() by including the header file . 1231 5.3. Embedded IPv4 Addresses 1232 The IPv4-mapped IPv6 address format is used to represent IPv4 addresses 1233 as IPv6 addresses. Most applications should be able to to manipulate 1234 IPv6 addresses as opaque 16-bit quantities, without needing to know 1235 whether they represent IPv4 addresses. However, a few applications may 1236 need to determine whether an IPv6 address is an IPv4-mapped address or 1237 not. The following function is provided for those applications: 1239 int inet6_isipv4addr (const struct in6_addr *addr); 1241 The "addr" argument to this function points to a buffer holding an IPv6 1242 address in network byte order. The function returns true (non-zero) if 1243 that address is an IPv4-mapped address, and returns 0 otherwise. 1245 This function could be used by server applications to determine whether 1246 the peer is an IPv4 node or an IPv6 node. After accepting a TCP 1247 connection via accept(), or receiving a UDP packet via recvfrom(), the 1248 application can apply the inet6_isipv4addr() function to the returned 1249 address. 1251 Applications obtain the prototype for this function by including the 1252 header file . 1254 6. Security Considerations 1256 IPv6 provides a number of new security mechanisms, many of which need to 1257 be accessible to applications. A companion document detailing the 1258 extensions to the socket interfaces to support IPv6 security is being 1259 written [4]. At some point in the future, that document and this one 1260 may be merged into a single API specification. 1262 7. Change History 1264 Changes from the July 1995 Edition 1266 - Changed u_long and u_short types in structures to u_int32_t and 1267 u_int16_t for consistency and clarity. 1269 - Added implementation-provided constants for IPv4 and IPv6 text 1270 address buffer length. 1272 - Defined a set of constants for subfields of sin6_flowid and for 1273 priority values. 1275 - Defined constants for getting and setting the source route flag. 1277 - Define where ansi prototypes for hostname2addr(), 1278 addr2hostname(), addr2ascii(), ascii2addr(), and 1279 ipv6_isipv4addr() reside. 1281 - Clarified the include file requirements. Say that the structure 1282 definitions are defined as a result of including the header file 1283 , not that the structures are necessarily defined 1284 there. 1286 - Removed underscore chars from is_ipv4_addr() function name for 1287 BSD compatibility. 1289 - Added inet6_ prefix to is_ipv4_addr() function name to avoid 1290 name space conflicts. 1292 - Changes setsockopt option naming convention to use IPV6_ prefix 1293 instead of IP_ so that there is clearly no ambiguity with IPv4 1294 options. Also, use level IPPROTO_IPV6 for these options. 1296 - Made hostname2addr() and addr2hostname() functions thread-safe. 1298 - Added support for sendmsg() and recvmsg() in source routing 1299 section. 1301 - Changed in_addr6 to in6_addr for consistency. 1303 - Re-structured document into sub-sections. 1305 - Deleted the implementation experience section. It was too 1306 wordy. 1308 - Added argument types to multicast socket options. 1310 - Added constant for largest source route array buffer. 1312 - Added the freehostent() function. 1314 - Added receving interface determination and sending interface 1315 selection options. 1317 - Added definitions of ipv6addr_any and ipv6addr_loopback. 1319 - Added text making the lookup of IPv4 addresses by 1320 hostname2addr() optional. 1322 Changes from the June 1995 Edition 1324 - Added capability for application to select loose or strict 1325 source routing. 1327 Changes from the March 1995 Edition 1329 - Changed the definition of the ipv6_addr structure to be an array 1330 of sixteen chars instead of four longs. This change is 1331 necessary to support machines which implement the socket 1332 interface, but do not have a 32-bit addressable word. Virtually 1333 all machines which provide the socket interface do support an 1334 8-bit addressable data type. 1336 - Added a more detailed explanation that the data types defined in 1337 this documented are not intended to be hard and fast 1338 requirements. Systems may use other data types if they wish. 1340 - Added a note flagging the fact that the sockaddr_in6 structure 1341 is not the same size as the sockaddr structure. 1343 - Changed the sin6_flowlabel field to sin6_flowinfo to accommodate 1344 the addition of the priority field to the IPv6 header. 1346 Changes from the October 1994 Edition 1348 - Added variant of sockaddr_in6 for 4.4 BSD-based systems (sa_len 1349 compatibility). 1351 - Removed references to SIT transition specification, and added 1352 reference to addressing architecture document, for definition of 1353 IPv4-mapped addresses. 1355 - Added a solution to the problem of the application not providing 1356 enough buffer space to hold a received source route. 1358 - Moved discussion of IPv4 applications interoperating with IPv6 1359 nodes to open issues section. 1361 - Added length parameter to addr2ascii() function to be consistent 1362 with addr2hostname(). 1364 - Changed IP_MULTICAST_TTL to IP_MULTICAST_HOPS to match IPv6 1365 terminology, and added IP_UNICAST_HOPS option to match 1366 IP_MULTICAST_HOPS. 1368 - Removed specification of numeric values for AF_INET6, 1369 IP_ADDRFORM, and IP_RCVSRCRT, since they need not be the same on 1370 different implementations. 1372 - Added a definition for the in_addr6 IPv6 address data 1373 structure. Added this so that applications could use 1374 sizeof(struct in_addr6) to get the size of an IPv6 address, 1375 and so that a structured type could be used in the 1376 is_ipv4_addr(). 1378 8. Open Issues 1380 A few open issues for IPv6 socket interface API specification remain, 1381 including: 1383 - Should we add a timeout parameter to hostname2addr() and 1384 addr2hostname()? DNS lookups need to be given some finite 1385 timeout interval, so it might be nice to let the application 1386 specify that interval. 1388 - Can the IPV6_ADDRFORM option really be implemented? 1390 - An interface is needed to allocate flow labels. Should one be 1391 defined in this spec, or left for another document? 1393 - Can existing IPv4 applications interoperate with IPv6 nodes? 1394 This issue is discussed in more detail in the following section. 1396 8.1. IPv4 Applications Interoperating with IPv6 Nodes 1398 This problem primarily has to do with the how IPv4 applications 1399 represent addresses of IPv6 nodes. What address should be returned to 1400 the application when an IPv6/UDP packet is received, or an IPv6/TCP 1401 connection is accepted? The peer's address could be any arbitrary 1402 128-bit IPv6 address. But the application is only equipped to deal with 1403 32-bit IPv4 addresses encoded in sockaddr_in data structures. 1405 We have not discovered any solution that provides complete transparent 1406 interoperability with IPv6 nodes for applications using the original 1407 IPv4 API. However, two techniques that partially solve the problem are: 1409 1) Prohibit communication between IPv4 applications and IPv6 nodes. 1410 Only UDP packets received from IPv4 nodes would be passed up to 1411 the application, and only TCP connections received from IPv4 1412 nodes would be accepted. UDP packets from IPv6 nodes would be 1413 dropped, and TCP connections from IPv6 nodes would be refused. 1415 2) The system could generate a local 32-bit cookie to represent the 1416 full 128-bit IPv6 address, and pass this value to the 1417 application. The system would maintain a mapping from cookie 1418 value into the 128-bit IPv6 address that it represents. When 1419 the application passed a cookie back into the system (for 1420 example, in a sendto() or connect() call) the system would use 1421 the 128-bit IPv6 address that the cookie represents. 1423 The cookie would have to be chosen so as to be an invalid IPv4 1424 address (e.g. an address on net 127.0.0.0), and the system would 1425 have to make sure that these cookie values did not escape into 1426 the Internet as the source or destination addresses of IPv4 1427 packets. 1429 Both of these techniques have drawbacks. This is an area for further 1430 study. System implementors may use one of these techniques or implement 1431 another solution. 1433 Acknowledgments 1435 Thanks to the many people who made suggestions and provided feedback to 1436 to the numerous revisions of this document, including: Ran Atkinson, 1437 Fred Baker, Dave Borman, Andrew Cherenson, Alex Conta, Alan Cox, Steve 1438 Deering, Francis Dupont, Robert Elz, Marc Hasson, Tom Herbert, Christian 1439 Huitema, Wan-Yen Hsu, Alan Lloyd, Charles Lynn, Dan McDonald, Erik 1440 Nordmark, Josh Osborne, Richard Stevens, Dean D. Throop, Glenn Trewitt, 1441 and Carl Williams. Craig Partridge suggested the addr2ascii() and 1442 ascii2addr() functions. 1444 Ramesh Govindan made a number of contributions and co-authored an 1445 earlier version of this paper. 1447 References 1449 [1] R. Hinden. "Internet Protocol, Version 6 (IPv6) Specification". 1450 Internet Draft. June 1995. 1452 [2] Keith Sklower. "Getconninfo(): An alternative to Gethostbyname()" 1453 Internet Draft. June 1995. 1455 [3] R. Hinden., S. Deering. "IP Version 6 Addressing Architecture". 1456 Internet Draft. June 1995. 1458 [4] D. McDonald. "IPv6 Security API for BSD Sockets". Internet 1459 Draft. January 1995. 1461 Authors' Address 1463 Jim Bound 1464 Digital Equipment Corporation 1465 110 Spitbrook Road ZK3-3/U14 1466 Nashua, NH 03062-2698 1467 Phone: +1 603 881 0400 1468 Email: bound@zk3.dec.com 1469 Susan Thomson 1470 Bell Communications Research 1471 MRE 2P-343, 445 South Street 1472 Morristown, NJ 07960 1473 Telephone: +1 201 829 4514 1474 Email: set@thumper.bellcore.com 1476 Robert E. Gilligan 1477 Mailstop MPK 17-202 1478 Sun Microsystems, Inc. 1479 2550 Garcia Avenue 1480 Mountain View, CA 94043-1100 1481 Phone: +1 415 786 5151 1482 Email: gilligan@eng.sun.com