idnits 2.17.1 draft-ietf-tsvwg-sctpsocket-21.txt: Checking boilerplate required by RFC 5378 and the IETF Trust (see https://trustee.ietf.org/license-info): ---------------------------------------------------------------------------- ** You're using the IETF Trust Provisions' Section 6.b License Notice from 12 Sep 2009 rather than the newer Notice from 28 Dec 2009. (See https://trustee.ietf.org/license-info/) Checking nits according to https://www.ietf.org/id-info/1id-guidelines.txt: ---------------------------------------------------------------------------- No issues found here. Checking nits according to https://www.ietf.org/id-info/checklist : ---------------------------------------------------------------------------- No issues found here. Miscellaneous warnings: ---------------------------------------------------------------------------- == The copyright year in the IETF Trust and authors Copyright Line does not match the current year -- The document seems to contain a disclaimer for pre-RFC5378 work, and may have content which was first submitted before 10 November 2008. The disclaimer is necessary when there are original authors that you have been unable to contact, or if some do not wish to grant the BCP78 rights to the IETF Trust. If you are able to get all authors (current and original) to grant those rights, you can and should remove the disclaimer; otherwise, the disclaimer is needed and you can ignore this comment. (See the Legal Provisions document at https://trustee.ietf.org/license-info for more information.) -- The document date (February 1, 2010) is 5170 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: Informational ---------------------------------------------------------------------------- ** Obsolete normative reference: RFC 793 (Obsoleted by RFC 9293) ** Obsolete normative reference: RFC 1644 (Obsoleted by RFC 6247) ** Obsolete normative reference: RFC 4960 (Obsoleted by RFC 9260) Summary: 4 errors (**), 0 flaws (~~), 1 warning (==), 3 comments (--). Run idnits with the --verbose option for more detailed information about the items above. -------------------------------------------------------------------------------- 2 Network Working Group R. Stewart 3 Internet-Draft Huawei 4 Intended status: Informational K. Poon 5 Expires: August 5, 2010 Sun Microsystems, Inc. 6 M. Tuexen 7 Muenster Univ. of Applied Sciences 8 V. Yasevich 9 HP 10 P. Lei 11 Cisco Systems, Inc. 12 February 1, 2010 14 Sockets API Extensions for Stream Control Transmission Protocol (SCTP) 15 draft-ietf-tsvwg-sctpsocket-21.txt 17 Abstract 19 This document describes a mapping of the Stream Control Transmission 20 Protocol SCTP into a sockets API. The benefits of this mapping 21 include compatibility for TCP applications, access to new SCTP 22 features and a consolidated error and event notification scheme. 24 Status of this Memo 26 This Internet-Draft is submitted to IETF in full conformance with the 27 provisions of BCP 78 and BCP 79. 29 Internet-Drafts are working documents of the Internet Engineering 30 Task Force (IETF), its areas, and its working groups. Note that 31 other groups may also distribute working documents as Internet- 32 Drafts. 34 Internet-Drafts are draft documents valid for a maximum of six months 35 and may be updated, replaced, or obsoleted by other documents at any 36 time. It is inappropriate to use Internet-Drafts as reference 37 material or to cite them other than as "work in progress." 39 The list of current Internet-Drafts can be accessed at 40 http://www.ietf.org/ietf/1id-abstracts.txt. 42 The list of Internet-Draft Shadow Directories can be accessed at 43 http://www.ietf.org/shadow.html. 45 This Internet-Draft will expire on August 5, 2010. 47 Copyright Notice 48 Copyright (c) 2010 IETF Trust and the persons identified as the 49 document authors. All rights reserved. 51 This document is subject to BCP 78 and the IETF Trust's Legal 52 Provisions Relating to IETF Documents 53 (http://trustee.ietf.org/license-info) in effect on the date of 54 publication of this document. Please review these documents 55 carefully, as they describe your rights and restrictions with respect 56 to this document. Code Components extracted from this document must 57 include Simplified BSD License text as described in Section 4.e of 58 the Trust Legal Provisions and are provided without warranty as 59 described in the BSD License. 61 This document may contain material from IETF Documents or IETF 62 Contributions published or made publicly available before November 63 10, 2008. The person(s) controlling the copyright in some of this 64 material may not have granted the IETF Trust the right to allow 65 modifications of such material outside the IETF Standards Process. 66 Without obtaining an adequate license from the person(s) controlling 67 the copyright in such materials, this document may not be modified 68 outside the IETF Standards Process, and derivative works of it may 69 not be created outside the IETF Standards Process, except to format 70 it for publication as an RFC or to translate it into languages other 71 than English. 73 Table of Contents 75 1. Introduction . . . . . . . . . . . . . . . . . . . . . . . . . 6 76 2. Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . 7 77 3. One-to-Many Style Interface . . . . . . . . . . . . . . . . . 7 78 3.1. Basic Operation . . . . . . . . . . . . . . . . . . . . . 7 79 3.1.1. socket() . . . . . . . . . . . . . . . . . . . . . . . 8 80 3.1.2. bind() . . . . . . . . . . . . . . . . . . . . . . . . 9 81 3.1.3. listen() . . . . . . . . . . . . . . . . . . . . . . . 10 82 3.1.4. sendmsg() and recvmsg() . . . . . . . . . . . . . . . 10 83 3.1.5. close() . . . . . . . . . . . . . . . . . . . . . . . 12 84 3.1.6. connect() . . . . . . . . . . . . . . . . . . . . . . 13 85 3.2. Implicit Association Setup . . . . . . . . . . . . . . . . 13 86 3.3. Non-blocking mode . . . . . . . . . . . . . . . . . . . . 14 87 3.4. Special considerations . . . . . . . . . . . . . . . . . . 15 88 4. One-to-One Style Interface . . . . . . . . . . . . . . . . . . 16 89 4.1. Basic Operation . . . . . . . . . . . . . . . . . . . . . 17 90 4.1.1. socket() . . . . . . . . . . . . . . . . . . . . . . . 17 91 4.1.2. bind() . . . . . . . . . . . . . . . . . . . . . . . . 18 92 4.1.3. listen() . . . . . . . . . . . . . . . . . . . . . . . 19 93 4.1.4. accept() . . . . . . . . . . . . . . . . . . . . . . . 19 94 4.1.5. connect() . . . . . . . . . . . . . . . . . . . . . . 20 95 4.1.6. close() . . . . . . . . . . . . . . . . . . . . . . . 21 96 4.1.7. shutdown() . . . . . . . . . . . . . . . . . . . . . . 21 97 4.1.8. sendmsg() and recvmsg() . . . . . . . . . . . . . . . 22 98 4.1.9. getpeername() . . . . . . . . . . . . . . . . . . . . 22 99 5. Data Structures . . . . . . . . . . . . . . . . . . . . . . . 23 100 5.1. The msghdr and cmsghdr Structures . . . . . . . . . . . . 23 101 5.2. SCTP msg_control Structures . . . . . . . . . . . . . . . 24 102 5.2.1. SCTP Initiation Structure (SCTP_INIT) . . . . . . . . 24 103 5.2.2. SCTP Header Information Structure (SCTP_SNDRCV) . . . 25 104 5.2.3. Extended SCTP Header Information Structure 105 (SCTP_EXTRCV) . . . . . . . . . . . . . . . . . . . . 28 106 5.2.4. SCTP Send Information Structure (SCTP_SNDINFO) . . . . 29 107 5.2.5. SCTP Receive Information Structure (SCTP_RCVINFO) . . 31 108 5.2.6. SCTP Next Receive Information Structure 109 (SCTP_NXTINFO) . . . . . . . . . . . . . . . . . . . . 32 110 5.2.7. SCTP PR-SCTP Information Structure (SCTP_PRINFO) . . . 32 111 5.2.8. SCTP AUTH Information Structure (SCTP_AUTHINFO) . . . 33 112 5.3. SCTP Events and Notifications . . . . . . . . . . . . . . 33 113 5.3.1. SCTP Notification Structure . . . . . . . . . . . . . 34 114 5.3.2. SCTP_ASSOC_CHANGE . . . . . . . . . . . . . . . . . . 35 115 5.3.3. SCTP_PEER_ADDR_CHANGE . . . . . . . . . . . . . . . . 36 116 5.3.4. SCTP_REMOTE_ERROR . . . . . . . . . . . . . . . . . . 37 117 5.3.5. SCTP_SEND_FAILED . . . . . . . . . . . . . . . . . . . 38 118 5.3.6. SCTP_SHUTDOWN_EVENT . . . . . . . . . . . . . . . . . 39 119 5.3.7. SCTP_ADAPTATION_INDICATION . . . . . . . . . . . . . . 40 120 5.3.8. SCTP_PARTIAL_DELIVERY_EVENT . . . . . . . . . . . . . 40 121 5.3.9. SCTP_AUTHENTICATION_EVENT . . . . . . . . . . . . . . 41 122 5.3.10. SCTP_SENDER_DRY_EVENT . . . . . . . . . . . . . . . . 42 123 5.3.11. SCTP_NOTIFICATIONS_STOPPED_EVENT . . . . . . . . . . . 43 124 5.4. Ancillary Data Considerations and Semantics . . . . . . . 43 125 5.4.1. Multiple Items and Ordering . . . . . . . . . . . . . 43 126 5.4.2. Accessing and Manipulating Ancillary Data . . . . . . 43 127 5.4.3. Control Message Buffer Sizing . . . . . . . . . . . . 44 128 6. Common Operations for Both Styles . . . . . . . . . . . . . . 45 129 6.1. send(), recv(), sendto(), and recvfrom() . . . . . . . . . 45 130 6.2. setsockopt() and getsockopt() . . . . . . . . . . . . . . 47 131 6.3. read() and write() . . . . . . . . . . . . . . . . . . . . 48 132 6.4. getsockname() . . . . . . . . . . . . . . . . . . . . . . 48 133 7. Socket Options . . . . . . . . . . . . . . . . . . . . . . . . 48 134 7.1. Read / Write Options . . . . . . . . . . . . . . . . . . . 50 135 7.1.1. Retransmission Timeout Parameters (SCTP_RTOINFO) . . . 50 136 7.1.2. Association Parameters (SCTP_ASSOCINFO) . . . . . . . 51 137 7.1.3. Initialization Parameters (SCTP_INITMSG) . . . . . . . 52 138 7.1.4. SO_LINGER . . . . . . . . . . . . . . . . . . . . . . 53 139 7.1.5. SCTP_NODELAY . . . . . . . . . . . . . . . . . . . . . 53 140 7.1.6. SO_RCVBUF . . . . . . . . . . . . . . . . . . . . . . 53 141 7.1.7. SO_SNDBUF . . . . . . . . . . . . . . . . . . . . . . 54 142 7.1.8. Automatic Close of Associations (SCTP_AUTOCLOSE) . . . 54 143 7.1.9. Set Primary Address (SCTP_PRIMARY_ADDR) . . . . . . . 54 144 7.1.10. Set Adaptation Layer Indicator 145 (SCTP_ADAPTATION_LAYER) . . . . . . . . . . . . . . . 55 146 7.1.11. Enable/Disable Message Fragmentation 147 (SCTP_DISABLE_FRAGMENTS) . . . . . . . . . . . . . . . 55 148 7.1.12. Peer Address Parameters (SCTP_PEER_ADDR_PARAMS) . . . 55 149 7.1.13. Set Default Send Parameters 150 (SCTP_DEFAULT_SEND_PARAM) . . . . . . . . . . . . . . 58 151 7.1.14. Set Notification and Ancillary Events (SCTP_EVENTS) . 58 152 7.1.15. Set/Clear IPv4 Mapped Addresses 153 (SCTP_I_WANT_MAPPED_V4_ADDR) . . . . . . . . . . . . . 58 154 7.1.16. Get or Set the Maximum Fragmentation Size 155 (SCTP_MAXSEG) . . . . . . . . . . . . . . . . . . . . 58 156 7.1.17. Get or Set the List of Supported HMAC Identifiers 157 (SCTP_HMAC_IDENT) . . . . . . . . . . . . . . . . . . 59 158 7.1.18. Get or Set the Active Shared Key 159 (SCTP_AUTH_ACTIVE_KEY) . . . . . . . . . . . . . . . . 60 160 7.1.19. Get or Set Delayed SACK Timer (SCTP_DELAYED_SACK) . . 60 161 7.1.20. Get or Set Fragmented Interleave 162 (SCTP_FRAGMENT_INTERLEAVE) . . . . . . . . . . . . . . 61 163 7.1.21. Set or Get the SCTP Partial Delivery Point 164 (SCTP_PARTIAL_DELIVERY_POINT) . . . . . . . . . . . . 62 165 7.1.22. Set or Get the Use of Extended Receive Info 166 (SCTP_USE_EXT_RCVINFO) . . . . . . . . . . . . . . . . 63 167 7.1.23. Set or Get the Auto ASCONF Flag (SCTP_AUTO_ASCONF) . . 63 168 7.1.24. Set or Get the Maximum Burst (SCTP_MAX_BURST) . . . . 63 169 7.1.25. Set or Get the Default Context (SCTP_CONTEXT) . . . . 64 170 7.1.26. Enable or Disable Explicit EOR Marking 171 (SCTP_EXPLICIT_EOR) . . . . . . . . . . . . . . . . . 64 172 7.1.27. Enable SCTP Port Reusage (SCTP_REUSE_PORT) . . . . . . 64 173 7.1.28. Set Notification Event (SCTP_EVENT) . . . . . . . . . 65 174 7.2. Read-Only Options . . . . . . . . . . . . . . . . . . . . 65 175 7.2.1. Association Status (SCTP_STATUS) . . . . . . . . . . . 65 176 7.2.2. Peer Address Information (SCTP_GET_PEER_ADDR_INFO) . . 66 177 7.2.3. Get the List of Chunks the Peer Requires to be 178 Authenticated (SCTP_PEER_AUTH_CHUNKS) . . . . . . . . 67 179 7.2.4. Get the List of Chunks the Local Endpoint Requires 180 to be Authenticated (SCTP_LOCAL_AUTH_CHUNKS) . . . . . 68 181 7.2.5. Get the Current Number of Associations 182 (SCTP_GET_ASSOC_NUMBER) . . . . . . . . . . . . . . . 68 183 7.2.6. Get the Current Identifiers of Associations 184 (SCTP_GET_ASSOC_ID_LIST) . . . . . . . . . . . . . . . 68 185 7.3. Write-Only Options . . . . . . . . . . . . . . . . . . . . 69 186 7.3.1. Set Peer Primary Address 187 (SCTP_SET_PEER_PRIMARY_ADDR) . . . . . . . . . . . . . 69 188 7.3.2. Add a Chunk That Must Be Authenticated 189 (SCTP_AUTH_CHUNK) . . . . . . . . . . . . . . . . . . 69 190 7.3.3. Set a Shared Key (SCTP_AUTH_KEY) . . . . . . . . . . . 70 191 7.3.4. Deactivate a Shared Key (SCTP_AUTH_DEACTIVATE_KEY) . . 70 192 7.3.5. Delete a Shared Key (SCTP_AUTH_DELETE_KEY) . . . . . . 71 193 7.4. Ancillary Data and Notification Interest Options . . . . . 72 194 8. New Functions . . . . . . . . . . . . . . . . . . . . . . . . 75 195 8.1. sctp_bindx() . . . . . . . . . . . . . . . . . . . . . . . 75 196 8.2. sctp_peeloff() . . . . . . . . . . . . . . . . . . . . . . 76 197 8.3. sctp_getpaddrs() . . . . . . . . . . . . . . . . . . . . . 77 198 8.4. sctp_freepaddrs() . . . . . . . . . . . . . . . . . . . . 77 199 8.5. sctp_getladdrs() . . . . . . . . . . . . . . . . . . . . . 78 200 8.6. sctp_freeladdrs() . . . . . . . . . . . . . . . . . . . . 78 201 8.7. sctp_sendmsg() . . . . . . . . . . . . . . . . . . . . . . 79 202 8.8. sctp_recvmsg() . . . . . . . . . . . . . . . . . . . . . . 79 203 8.9. sctp_connectx() . . . . . . . . . . . . . . . . . . . . . 80 204 8.10. sctp_send() . . . . . . . . . . . . . . . . . . . . . . . 81 205 8.11. sctp_sendx() . . . . . . . . . . . . . . . . . . . . . . . 82 206 8.12. sctp_getaddrlen() . . . . . . . . . . . . . . . . . . . . 83 207 9. IANA Considerations . . . . . . . . . . . . . . . . . . . . . 83 208 10. Security Considerations . . . . . . . . . . . . . . . . . . . 83 209 11. Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . 84 210 12. Normative References . . . . . . . . . . . . . . . . . . . . . 84 211 Appendix A. One-to-One Style Code Example . . . . . . . . . . . . 85 212 Appendix B. One-to-Many Style Code Example . . . . . . . . . . . 90 213 Authors' Addresses . . . . . . . . . . . . . . . . . . . . . . . . 91 215 1. Introduction 217 The sockets API has provided a standard mapping of the Internet 218 Protocol suite to many operating systems. Both TCP [RFC0793] and UDP 219 [RFC0768] have benefited from this standard representation and access 220 method across many diverse platforms. SCTP is a new protocol that 221 provides many of the characteristics of TCP but also incorporates 222 semantics more akin to UDP. This document defines a method to map 223 the existing sockets API for use with SCTP, providing both a base for 224 access to new features and compatibility so that most existing TCP 225 applications can be migrated to SCTP with few (if any) changes. 227 There are three basic design objectives: 228 1. Maintain consistency with existing sockets APIs: We define a 229 sockets mapping for SCTP that is consistent with other sockets 230 API protocol mappings (for instance UDP, TCP, IPv4, and IPv6). 231 2. Support a one-to-many style interface: This set of semantics is 232 similar to that defined for connection-less protocols, such as 233 UDP. A one-to-many style SCTP socket should be able to control 234 multiple SCTP associations. This is similar to a UDP socket, 235 which can communicate with many peer endpoints. Each of these 236 associations is assigned an association ID so that an application 237 can use the ID to differentiate them. Note that SCTP is 238 connection-oriented in nature, and it does not support broadcast 239 or multicast communications, as UDP does. 240 3. Support a one-to-one style interface: This interface supports a 241 similar semantics as sockets for connection-oriented protocols, 242 such as TCP. A one-to-one style SCTP socket should only control 243 one SCTP association. One purpose of defining this interface is 244 to allow existing applications built on other connection-oriented 245 protocols be ported to use SCTP with very little effort. And 246 developers familiar with those semantics can easily adapt to 247 SCTP. Another purpose is to make sure that existing mechanisms 248 in most operating systems to deal with socket, such as select(), 249 should continue to work with this style of socket. Extensions 250 are added to this mapping to provide mechanisms to exploit new 251 features of SCTP. 253 Goals 2 and 3 are not compatible, so in this document we define two 254 modes of mapping, namely the one-to-many style mapping and the one- 255 to-one style mapping. These two modes share some common data 256 structures and operations, but will require the use of two different 257 application programming styles. Note that all new SCTP features can 258 be used with both styles of socket. The decision on which one to use 259 depends mainly on the nature of applications. 261 A mechanism is defined to extract a one-to-many style SCTP 262 association into a one-to-one style socket. 264 Some of the SCTP mechanisms cannot be adequately mapped to an 265 existing socket interface. In some cases, it is more desirable to 266 have a new interface instead of using existing socket calls. 267 Section 8 of this document describes those new interfaces. 269 2. Data Types 271 Whenever possible, data types from Draft 6.6 (March 1997) of POSIX 272 1003.1g are used: uintN_t means an unsigned integer of exactly N bits 273 (e.g. uint16_t). We also assume the argument data types from 1003.1g 274 when possible (e.g. the final argument to setsockopt() is a size_t 275 value). Whenever buffer sizes are specified, the POSIX 1003.1 size_t 276 data type is used. 278 3. One-to-Many Style Interface 280 The one-to-many style interface has the following characteristics: 281 o Outbound association setup is implicit. 282 o Messages are delivered in complete messages (with one notable 283 exception). 284 o There is a 1 to MANY relationship between socket and association. 286 3.1. Basic Operation 288 A typical server in this style uses the following socket calls in 289 sequence to prepare an endpoint for servicing requests: 290 o socket() 291 o bind() 292 o listen() 293 o recvmsg() 294 o sendmsg() 295 o close() 297 A typical client uses the following calls in sequence to setup an 298 association with a server to request services: 299 o socket() 300 o sendmsg() 301 o recvmsg() 302 o close() 304 In this style, by default, all the associations connected to the 305 endpoint are represented with a single socket. Each association is 306 assigned an association ID (type is sctp_assoc_t) so that an 307 application can use it to differentiate between them. In some 308 implementations, the peer endpoints' addresses can also be used for 309 this purpose. But this is not required for performance reasons. If 310 an implementation does not support using addresses to differentiate 311 between different associations, the sendto() call can only be used to 312 setup an association implicitly. It cannot be used to send data to 313 an established association as the association ID cannot be specified. 315 Once as association ID is assigned to an SCTP association, that ID 316 will not be reused until the application explicitly terminates the 317 association. The resources belonging to that association will not be 318 freed until that happens. This is similar to the close() operation 319 on a normal socket. The only exception is when the SCTP_AUTOCLOSE 320 option (section 7.1.8) is set. In this case, after the association 321 is terminated gracefully and automatically, the association ID 322 assigned to it can be reused. All applications using this option 323 should be aware of this to avoid the possible problem of sending data 324 to an incorrect peer endpoint. 326 If the server or client wishes to branch an existing association off 327 to a separate socket, it is required to call sctp_peeloff() and to 328 specify the association identifier. The sctp_peeloff() call will 329 return a new socket which can then be used with recv() and send() 330 functions for message passing. See Section 8.2 for more on branched- 331 off associations. The returned socket is a one-to-one style socket. 333 Once an association is branched off to a separate socket, it becomes 334 completely separated from the original socket. All subsequent 335 control and data operations to that association must be done through 336 the new socket. For example, the close operation on the original 337 socket will not terminate any associations that have been branched 338 off to a different socket. 340 We will discuss the one-to-many style socket calls in more detail in 341 the following subsections. 343 3.1.1. socket() 345 Applications use socket() to create a socket descriptor to represent 346 an SCTP endpoint. 348 The function prototype is 350 int socket(int domain, 351 int type, 352 int protocol); 354 and one uses PF_INET or PF_INET6 as the domain, SOCK_SEQPACKET as the 355 type and IPPROTO_SCTP as the protocol. 357 Here, SOCK_SEQPACKET indicates the creation of a one-to-many style 358 socket. 360 Using the PF_INET domain indicates the creation of an endpoint which 361 can use only IPv4 addresses, while PF_INET6 creates an endpoint which 362 can use both IPv6 and IPv4 addresses. 364 3.1.2. bind() 366 Applications use bind() to specify which local address the SCTP 367 endpoint should associate itself with. 369 An SCTP endpoint can be associated with multiple addresses. To do 370 this, sctp_bindx() is introduced in Section 8.1 to help applications 371 do the job of associating multiple addresses. 373 These addresses associated with a socket are the eligible transport 374 addresses for the endpoint to send and receive data. The endpoint 375 will also present these addresses to its peers during the association 376 initialization process, see [RFC4960]. 378 After calling bind(), if the endpoint wishes to accept new 379 associations on the socket, it must call listen() (see 380 Section 3.1.3). 382 The function prototype of bind() is 384 int bind(int sd, 385 struct sockaddr *addr, 386 socklen_t addrlen); 388 and the arguments are 389 sd: The socket descriptor returned by socket(). 390 addr: The address structure (struct sockaddr_in or struct 391 sockaddr_in6, see [RFC3493]). 392 addrlen: The size of the address structure. 394 If sd is an IPv4 socket, the address passed must be an IPv4 address. 395 If the sd is an IPv6 socket, the address passed can either be an IPv4 396 or an IPv6 address. 398 Applications cannot call bind() multiple times to associate multiple 399 addresses to an endpoint. After the first call to bind(), all 400 subsequent calls will return an error. 402 If addr is specified as a wildcard (INADDR_ANY for an IPv4 address, 403 or as IN6ADDR_ANY_INIT or in6addr_any for an IPv6 address), the 404 operating system will associate the endpoint with an optimal address 405 set of the available interfaces. 407 If a bind() is not called prior to a sendmsg() call that initiates a 408 new association, the system picks an ephemeral port and will choose 409 an address set equivalent to binding with a wildcard address. One of 410 those addresses will be the primary address for the association. 411 This automatically enables the multi-homing capability of SCTP. 413 3.1.3. listen() 415 By default, new associations are not accepted for one-to-many style 416 sockets. An application uses listen() to mark a socket as being able 417 to accept new associations. 419 The function prototype is 421 int listen(int sd, 422 int backlog); 424 and the arguments are 425 sd: The socket descriptor of the endpoint. 426 backlog: If backlog is non-zero, enable listening else disable 427 listening. 429 Note that one-to-many style socket consumers do not need to call 430 accept to retrieve new associations. Calling accept() on a one-to- 431 many style socket should return EOPNOTSUPP. Rather, new associations 432 are accepted automatically, and notifications of the new associations 433 are delivered via recvmsg() with the SCTP_ASSOC_CHANGE event (if 434 these notifications are enabled). Clients will typically not call 435 listen(), so that they can be assured that the only associations on 436 the socket will be the ones those actively initiated. Server or 437 peer-to-peer sockets, on the other hand, will always accept new 438 associations, so a well-written application using server one-to-many 439 style sockets must be prepared to handle new associations from 440 unwanted peers. 442 Also note that the SCTP_ASSOC_CHANGE event provides the association 443 ID for a new association, so if applications wish to use the 444 association ID as input to other socket calls, they should ensure 445 that the SCTP_ASSOC_CHANGE event is enabled. 447 3.1.4. sendmsg() and recvmsg() 449 An application uses the sendmsg() and recvmsg() call to transmit data 450 to and receive data from its peer. 452 The function prototypes are 453 ssize_t sendmsg(int sd, 454 const struct msghdr *message, 455 int flags); 457 and 459 ssize_t recvmsg(int sd, 460 struct msghdr *message, 461 int flags); 463 using the arguments: 464 sd: The socket descriptor of the endpoint. 465 message: Pointer to the msghdr structure which contains a single 466 user message and possibly some ancillary data. See Section 5 for 467 complete description of the data structures. 468 flags: No new flags are defined for SCTP at this level. See 469 Section 5 for SCTP-specific flags used in the msghdr structure. 471 As we will see in Section 5, along with the user data, the ancillary 472 data field is used to carry the sctp_sndrcvinfo and/or the 473 sctp_initmsg structures to perform various SCTP functions including 474 specifying options for sending each user message. Those options, 475 depending on whether sending or receiving, include stream number, 476 stream sequence number, various flags, context and payload protocol 477 Id, etc. 479 When sending user data with sendmsg(), the msg_name field in the 480 msghdr structure will be filled with one of the transport addresses 481 of the intended receiver. If there is no association existing 482 between the sender and the intended receiver, the sender's SCTP stack 483 will set up a new association and then send the user data (see 484 Section 3.2 for more on implicit association setup). If an SCTP_INIT 485 cmsg structure is used with NULL data, an association will be 486 established using the parameters from the struct sctp_initmsg 487 structure. If no SCTP_INIT cmsg structure is used in combination 488 with NULL data, an association is established using the default 489 parameters. If NULL data is used, no association exists and the 490 SCTP_ABORT or SCTP_EOF flags are present, then -1 must be returned 491 and an errno should be set to something like EDONOTBESTUPID. Sending 492 a message using sendmsg() is atomic unless explicit EOR marking is 493 enabled on the socket specified by sd. 495 If a peer sends a SHUTDOWN, an SCTP_SHUTDOWN_EVENT notification will 496 be delivered if that notification has been enabled, and no more data 497 can be sent to that association. Any attempt to send more data will 498 cause sendmsg() to return with an ESHUTDOWN error. Note that the 499 socket is still open for reading at this point so it is possible to 500 retrieve notifications. 502 When receiving a user message with recvmsg(), the msg_name field in 503 the msghdr structure will be populated with the source transport 504 address of the user data. The caller of recvmsg() can use this 505 address information to determine to which association the received 506 user message belongs. Note that if SCTP_ASSOC_CHANGE events are 507 disabled, applications must use the peer transport address provided 508 in the msg_name field by recvmsg() to perform correlation to an 509 association, since they will not have the association ID. 511 If all data in a single message has been delivered, MSG_EOR will be 512 set in the msg_flags field of the msghdr structure (see section 513 Section 5.1). 515 If the application does not provide enough buffer space to completely 516 receive a data message, MSG_EOR will not be set in msg_flags. 517 Successive reads will consume more of the same message until the 518 entire message has been delivered, and MSG_EOR will be set. 520 If the SCTP stack is running low on buffers, it may partially deliver 521 a message. In this case, MSG_EOR will not be set, and more calls to 522 recvmsg() will be necessary to completely consume the message. Only 523 one message at a time can be partially delivered in any stream. The 524 socket option SCTP_FRAGMENT_INTERLEAVE controls various aspects of 525 what interlacing of messages occurs for both the one-to-one and the 526 one-to-many model sockets. Please consult Section 7.1.20 for further 527 details on message delivery options. 529 Note, if the socket is a branched-off socket that only represents one 530 association (see Section 3.1), the msg_name field can be used to 531 override the primary address when sending data. 533 3.1.5. close() 535 Applications use close() to perform graceful shutdown (as described 536 in Section 10.1 of [RFC4960]) on ALL the associations currently 537 represented by a one-to-many style socket. 539 The function prototype is 541 int close(int sd); 543 and the argument is 544 sd: The socket descriptor of the associations to be closed. 546 To gracefully shutdown a specific association represented by the one- 547 to-many style socket, an application should use the sendmsg() call, 548 and include the SCTP_EOF flag. A user may optionally terminate an 549 association non-gracefully by sending with the SCTP_ABORT flag and 550 possibly passing a user specified abort code in the data field. Both 551 flags SCTP_EOF and SCTP_ABORT are passed with ancillary data (see 552 Section 5.2.2) in the sendmsg() call. 554 If sd in the close() call is a branched-off socket representing only 555 one association, the shutdown is performed on that association only. 557 3.1.6. connect() 559 An application may use the connect() call in the one-to-many style to 560 initiate an association without sending data. 562 The function prototype is 564 int connect(int sd, 565 const struct sockaddr *nam, 566 socklen_t len); 568 and the arguments are 569 sd: The socket descriptor to have a new association added to. 570 nam: The address structure (either struct sockaddr_in or struct 571 sockaddr_in6 defined in [RFC3493]). 572 len: The size of the address. 574 Multiple connect() calls can be made on the same socket to create 575 multiple associations. This is different from the semantics of 576 connect() on a UDP socket. 578 3.2. Implicit Association Setup 580 Implicit association setup applies only to one-to-many style sockets. 581 For one-to-one style sockets implicit association setup must not be 582 used. 584 Once the bind() call is complete on a one-to-many style socket, the 585 application can begin sending and receiving data using the sendmsg()/ 586 recvmsg() or sendto()/recvfrom() calls, without going through any 587 explicit association setup procedures (i.e., no connect() calls 588 required). 590 Whenever sendmsg() or sendto() is called and the SCTP stack at the 591 sender finds that there is no association existing between the sender 592 and the intended receiver (identified by the address passed either in 593 the msg_name field of msghdr structure in the sendmsg() call or the 594 dest_addr field in the sendto() call), the SCTP stack will 595 automatically setup an association to the intended receiver. 597 Upon the successful association setup an SCTP_COMM_UP notification 598 will be dispatched to the socket at both the sender and receiver 599 side. This notification can be read by the recvmsg() system call 600 (see Section 3.1.3). 602 Note, if the SCTP stack at the sender side supports bundling, the 603 first user message may be bundled with the COOKIE ECHO message 604 [RFC4960]. 606 When the SCTP stack sets up a new association implicitly, it first 607 consults the sctp_initmsg structure, which is passed along within the 608 ancillary data in the sendmsg() call (see Section 5.2.1 for details 609 of the data structures), for any special options to be used on the 610 new association. 612 If this information is not present in the sendmsg() call, or if the 613 implicit association setup is triggered by a sendto() call, the 614 default association initialization parameters will be used. These 615 default association parameters may be set with respective 616 setsockopt() calls or be left to the system defaults. 618 Implicit association setup cannot be initiated by send()/recv() 619 calls. 621 3.3. Non-blocking mode 623 Some SCTP users might want to avoid blocking when they call socket 624 interface function. 626 Once all bind() calls are complete on a one-to-many style socket, the 627 application must set the non-blocking option by a fcntl() (such as 628 O_NONBLOCK), after which the sendmsg() function returns immediately, 629 and the success or failure of the data message (and possible 630 SCTP_INITMSG parameters) will be signaled by the SCTP_ASSOC_CHANGE 631 event with SCTP_COMM_UP or CANT_START_ASSOC. If user data could not 632 be sent (due to a CANT_START_ASSOC), the sender will also receive an 633 SCTP_SEND_FAILED event. Events can be received by the user calling 634 recvmsg(). A server (having called listen()) is also notified of an 635 association up event by the reception of an SCTP_ASSOC_CHANGE with 636 SCTP_COMM_UP via the calling of recvmsg() and possibly the reception 637 of the first data message. 639 In order to shutdown the association gracefully, the user must call 640 sendmsg() with no data and with the SCTP_EOF flag set. The function 641 returns immediately, and completion of the graceful shutdown is 642 indicated by an SCTP_ASSOC_CHANGE notification of type 643 SHUTDOWN_COMPLETE (see Section 5.3.2). Note that this can also be 644 done using the sctp_send() call described in Section 8.10. 646 An application is recommended to use caution when using select() (or 647 poll()) for writing on a one-to-many style socket. The reason being 648 that the interpretation of select on write is implementation 649 specific. Generally a positive return on a select on write would 650 only indicate that one of the associations represented by the one-to- 651 many socket is writable. An application that writes after the 652 select() returns may still block since the association that was 653 writeable is not the destination association of the write call. 654 Likewise select() (or poll()) for reading from a one-to-many socket 655 will only return an indication that one of the associations 656 represented by the socket has data to be read. 658 An application that wishes to know that a particular association is 659 ready for reading or writing should either use the one-to-one style 660 or use the sctp_peeloff() (see Section 8.2) function to separate the 661 association of interest from the one-to-many socket. 663 3.4. Special considerations 665 The fact that a one-to-many style socket can provide access to many 666 SCTP associations through a single socket descriptor has important 667 implications for both application programmers and system programmers 668 implementing this API. A key issue is how buffer space inside the 669 sockets layer is managed. Because this implementation detail 670 directly affects how application programmers must write their code to 671 ensure correct operation and portability, this section provides some 672 guidance to both implementers and application programmers. 674 An important feature that SCTP shares with TCP is flow control: 675 specifically, a sender may not send data faster than the receiver can 676 consume it. 678 For TCP, flow control is typically provided for in the sockets API as 679 follows. If the reader stops reading, the sender queues messages in 680 the socket layer until it uses all of its socket buffer space 681 allocation creating a "stalled connection". Further attempts to 682 write to the socket will block or return the error EAGAIN or 683 EWOULDBLOCK for a non-blocking socket. At some point, either the 684 connection is closed, or the receiver begins to read again freeing 685 space in the output queue. 687 For one-to-one style SCTP sockets (this includes sockets descriptors 688 that were separated from a one-to-many style socket with 689 sctp_peeloff()) the behavior is identical. For one-to-many style 690 SCTP sockets, the fact that we have multiple associations on a single 691 socket makes the situation more complicated. If the implementation 692 uses a single buffer space allocation shared by all associations, a 693 single stalled association can prevent the further sending of data on 694 all associations active on a particular one-to-many style socket. 696 For a blocking socket, it should be clear that a single stalled 697 association can block the entire socket. For this reason, 698 application programmers may want to use non-blocking one-to-many 699 style sockets. The application should at least be able to send 700 messages to the non-stalled associations. 702 But a non-blocking socket is not sufficient if the API implementer 703 has chosen a single shared buffer allocation for the socket. A 704 single stalled association would eventually cause the shared 705 allocation to fill, and it would become impossible to send even to 706 non-stalled associations. 708 The API implementer can solve this problem by providing each 709 association with its own allocation of outbound buffer space. Each 710 association should conceptually have as much buffer space as it would 711 have if it had its own socket. As a bonus, this simplifies the 712 implementation of sctp_peeloff(). 714 To ensure that a given stalled association will not prevent other 715 non-stalled associations from being writable, application programmers 716 should either: 717 o demand that the underlying implementation dedicates independent 718 buffer space allotments to each association (as suggested above), 719 or 720 o verify that their application layer protocol does not permit large 721 amounts of unread data at the receiver (this is true of some 722 request-response protocols, for example), or 723 o use one-to-one style sockets for association which may potentially 724 stall (either from the beginning, or by using sctp_peeloff before 725 sending large amounts of data that may cause a stalled condition). 727 4. One-to-One Style Interface 729 The goal of this style is to follow as closely as possible the 730 current practice of using the sockets interface for a connection 731 oriented protocol, such as TCP. This style enables existing 732 applications using connection oriented protocols to be ported to SCTP 733 with very little effort. 735 Note that some new SCTP features and some new SCTP socket options can 736 only be utilized through the use of sendmsg() and recvmsg() calls, 737 see Section 4.1.8. Also note that some socket interfaces may not be 738 able to bundle DATA chunks with the COOKIE chunk when using this 739 interface style. 741 4.1. Basic Operation 743 A typical server in one-to-one style uses the following system call 744 sequence to prepare an SCTP endpoint for servicing requests: 745 o socket() 746 o bind() 747 o listen() 748 o accept() 750 The accept() call blocks until a new association is set up. It 751 returns with a new socket descriptor. The server then uses the new 752 socket descriptor to communicate with the client, using recv() and 753 send() calls to get requests and send back responses. 755 Then it calls 756 o close() 757 to terminate the association. 759 A typical client uses the following system call sequence to setup an 760 association with a server to request services: 761 o socket() 762 o connect() 764 After returning from connect(), the client uses send() and recv() 765 calls to send out requests and receive responses from the server. 767 The client calls 768 o close() 769 to terminate this association when done. 771 4.1.1. socket() 773 Applications call socket() to create a socket descriptor to represent 774 an SCTP endpoint. 776 The function prototype is 778 int socket(int domain, 779 int type, 780 int protocol); 782 and one uses PF_INET or PF_INET6 as the domain, SOCK_STREAM as the 783 type and IPPROTO_SCTP as the protocol. 785 Here, SOCK_STREAM indicates the creation of a one-to-one style 786 socket. 788 Using the PF_INET domain indicates the creation of an endpoint which 789 can use only IPv4 addresses, while PF_INET6 creates an endpoint which 790 can use both IPv6 and IPv4 addresses. 792 4.1.2. bind() 794 Applications use bind() to pass an address to be associated with an 795 SCTP endpoint to the system. bind() allows only either a single 796 address or a IPv4 or IPv6 wildcard address to be bound. An SCTP 797 endpoint can be associated with multiple addresses. To do this, 798 sctp_bindx() is introduced in Section 8.1 to help applications do the 799 job of associating multiple addresses. 801 These addresses associated with a socket are the eligible transport 802 addresses for the endpoint to send and receive data. The endpoint 803 will also present these addresses to its peers during the association 804 initialization process, see [RFC4960]. 806 The function prototype of bind() is 808 int bind(int sd, 809 struct sockaddr *addr, 810 socklen_t addrlen); 812 and the arguments are 813 sd: The socket descriptor returned by socket(). 814 addr: The address structure (struct sockaddr_in or struct 815 sockaddr_in6, see [RFC3493]). 816 addrlen: The size of the address structure. 818 If sd is an IPv4 socket, the address passed must be an IPv4 address. 819 Otherwise, i.e., the sd is an IPv6 socket, the address passed can 820 either be an IPv4 or an IPv6 address. 822 Applications cannot call bind() multiple times to associate multiple 823 addresses to the endpoint. After the first call to bind(), all 824 subsequent calls will return an error. 826 If addr is specified as a wildcard (INADDR_ANY for an IPv4 address, 827 or as IN6ADDR_ANY_INIT or in6addr_any for an IPv6 address), the 828 operating system will associate the endpoint with an optimal address 829 set of the available interfaces. 831 If a bind() is not called prior to the connect() call, the system 832 picks an ephemeral port and will choose an address set equivalent to 833 binding with a wildcard address. One of those addresses will be the 834 primary address for the association. This automatically enables the 835 multi-homing capability of SCTP. 837 The completion of this bind() process does not ready the SCTP 838 endpoint to accept inbound SCTP association requests. Until a 839 listen() system call, described below, is performed on the socket, 840 the SCTP endpoint will promptly reject an inbound SCTP INIT request 841 with an SCTP ABORT. 843 4.1.3. listen() 845 Applications use listen() to ready the SCTP endpoint for accepting 846 inbound associations. 848 The function prototype is 850 int listen(int sd, 851 int backlog); 853 and the arguments are 854 sd: the socket descriptor of the SCTP endpoint. 855 backlog: this specifies the max number of outstanding associations 856 allowed in the socket's accept queue. These are the associations 857 that have finished the four-way initiation handshake (see Section 858 5 of [RFC4960]) and are in the ESTABLISHED state. Note, a backlog 859 of '0' indicates that the caller no longer wishes to receive new 860 associations. 862 4.1.4. accept() 864 Applications use the accept() call to remove an established SCTP 865 association from the accept queue of the endpoint. A new socket 866 descriptor will be returned from accept() to represent the newly 867 formed association. 869 The function prototype is 871 int accept(int sd, 872 struct sockaddr *addr, 873 socklen_t *addrlen); 875 and the arguments are 876 sd: The listening socket descriptor. 877 addr: On return, will contain the primary address of the peer 878 endpoint. 879 addrlen: On return, will contain the size of addr. 880 The functions returns the socket descriptor for the newly formed 881 association. 883 4.1.5. connect() 885 Applications use connect() to initiate an association to a peer. 887 The function prototype is 889 int connect(int sd, 890 const struct sockaddr *addr, 891 socklen_t addrlen); 893 and the arguments are 894 sd: The socket descriptor of the endpoint. 895 addr: The peer's address. 896 addrlen: The size of the address. 898 This operation corresponds to the ASSOCIATE primitive described in 899 section 10.1 of [RFC4960]. 901 By default, the new association created has only one outbound stream. 902 The SCTP_INITMSG option described in Section 7.1.3 should be used 903 before connecting to change the number of outbound streams. 905 If a bind() is not called prior to the connect() call, the system 906 picks an ephemeral port and will choose an address set equivalent to 907 binding with INADDR_ANY and IN6ADDR_ANY_INIT for IPv4 and IPv6 socket 908 respectively. One of those addresses will be the primary address for 909 the association. This automatically enables the multi-homing 910 capability of SCTP. 912 Note that SCTP allows data exchange, similar to T/TCP [RFC1644], 913 during the association set up phase. If an application wants to do 914 this, it cannot use the connect() call. Instead, it should use 915 sendto() or sendmsg() to initiate an association. If it uses 916 sendto() and it wants to change the initialization behavior, it needs 917 to use the SCTP_INITMSG socket option before calling sendto(). Or it 918 can use SCTP_INIT type sendmsg() to initiate an association without 919 doing the setsockopt(). Note that some sockets implementations may 920 not support the sending of data to initiate an association with the 921 one-to-one style (implementations that do not support T/TCP normally 922 have this restriction). 924 SCTP does not support half close semantics. This means that unlike 925 T/TCP, MSG_EOF should not be set in the flags parameter when calling 926 sendto() or sendmsg() when the call is used to initiate a connection. 927 MSG_EOF is not an acceptable flag with an SCTP socket. 929 4.1.6. close() 931 Applications use close() to gracefully close down an association. 933 The function prototype is 935 int close(int sd); 937 and the argument is 938 sd: The socket descriptor of the association to be closed. 940 After an application calls close() on a socket descriptor, no further 941 socket operations will succeed on that descriptor. 943 4.1.7. shutdown() 945 SCTP differs from TCP in that it does not have half closed semantics. 946 Hence the shutdown() call for SCTP is an approximation of the TCP 947 shutdown() call, and solves some different problems. Full TCP- 948 compatibility is not provided, so developers porting TCP applications 949 to SCTP may need to recode sections that use shutdown(). (Note that 950 it is possible to achieve the same results as half close in SCTP 951 using SCTP streams.) 953 The function prototype is 955 int shutdown(int sd, 956 int how); 958 and the arguments are 959 sd: The socket descriptor of the association to be closed. 960 how: Specifies the type of shutdown. The values are as follows: 961 SHUT_RD: Disables further receive operations. No SCTP protocol 962 action is taken. 963 SHUT_WR: Disables further send operations, and initiates the SCTP 964 shutdown sequence. 965 SHUT_RDWR: Disables further send and receive operations and 966 initiates the SCTP shutdown sequence. 968 The major difference between SCTP and TCP shutdown() is that SCTP 969 SHUT_WR initiates immediate and full protocol shutdown, whereas TCP 970 SHUT_WR causes TCP to go into the half closed state. SHUT_RD behaves 971 the same for SCTP as TCP. The purpose of SCTP SHUT_WR is to close 972 the SCTP association while still leaving the socket descriptor open, 973 so that the caller can receive back any data SCTP was unable to 974 deliver (see Section 5.3.5 for more information). 976 To perform the ABORT operation described in [RFC4960] section 10.1, 977 an application can use the socket option SO_LINGER. It is described 978 in Section 7.1.4. 980 4.1.8. sendmsg() and recvmsg() 982 With a one-to-one style socket, the application can also use 983 sendmsg() and recvmsg() to transmit data to and receive data from its 984 peer. The semantics is similar to those used in the one-to-many 985 style (section Section 3.1.3), with the following differences: 986 1. When sending, the msg_name field in the msghdr is not used to 987 specify the intended receiver, rather it is used to indicate a 988 preferred peer address if the sender wishes to discourage the 989 stack from sending the message to the primary address of the 990 receiver. If the socket is connected and the transport address 991 given is not part of the current association, the data will not 992 be sent and an SCTP_SEND_FAILED event will be delivered to the 993 application if send failure events are enabled. 994 2. Using sendmsg() on a non-connected one-to-one style socket for 995 implicit connection setup may or may not work depending on the 996 SCTP implementation. 998 4.1.9. getpeername() 1000 Applications use getpeername() to retrieve the primary socket address 1001 of the peer. This call is for TCP compatibility, and is not multi- 1002 homed. It does not work with one-to-many style sockets. See 1003 Section 8.3 for a multi-homed/one-to-many style version of the call. 1005 The function prototype is 1007 int getpeername(int sd, 1008 struct sockaddr *address, 1009 socklen_t *len); 1011 and the arguments are: 1012 sd: The socket descriptor to be queried. 1013 address: On return, the peer primary address is stored in this 1014 buffer. If the socket is an IPv4 socket, the address will be 1015 IPv4. If the socket is an IPv6 socket, the address will be either 1016 an IPv6 or IPv4 address. 1017 len: The caller should set the length of address here. On return, 1018 this is set to the length of the returned address. 1020 If the actual length of the address is greater than the length of the 1021 supplied sockaddr structure, the stored address will be truncated. 1023 5. Data Structures 1025 In this section we discuss important data structures which are 1026 specific to SCTP and are used with sendmsg() and recvmsg() calls to 1027 control SCTP endpoint operations and to access ancillary information 1028 and notifications. 1030 5.1. The msghdr and cmsghdr Structures 1032 The msghdr structure used in the sendmsg() and recvmsg() calls, as 1033 well as the ancillary data carried in the structure, is the key for 1034 the application to set and get various control information from the 1035 SCTP endpoint. 1037 The msghdr and the related cmsghdr structures are defined and 1038 discussed in detail in [RFC3542]. Here we will cite their 1039 definitions from [RFC3542]. 1041 The msghdr structure: 1043 struct msghdr { 1044 void *msg_name; /* ptr to socket address structure */ 1045 socklen_t msg_namelen; /* size of socket address structure */ 1046 struct iovec *msg_iov; /* scatter/gather array */ 1047 size_t msg_iovlen; /* # elements in msg_iov */ 1048 void *msg_control; /* ancillary data */ 1049 socklen_t msg_controllen; /* ancillary data buffer length */ 1050 int msg_flags; /* flags on received message */ 1051 }; 1053 and the cmsghdr structure: 1055 struct cmsghdr { 1056 socklen_t cmsg_len; /* #bytes, including this header */ 1057 int cmsg_level; /* originating protocol */ 1058 int cmsg_type; /* protocol-specific type */ 1059 /* followed by unsigned char cmsg_data[]; */ 1060 }; 1062 In the msghdr structure, the usage of msg_name has been discussed in 1063 previous sections (see Section 3.1.3 and Section 4.1.8). 1065 The scatter/gather buffers, or I/O vectors (pointed to by the msg_iov 1066 field) are treated as a single SCTP data chunk, rather than multiple 1067 chunks, for both sendmsg() and recvmsg(). 1069 The msg_flags are not used when sending a message with sendmsg(). 1071 If a notification has arrived, recvmsg() will return the notification 1072 with the MSG_NOTIFICATION flag set in msg_flags. If the 1073 MSG_NOTIFICATION flag is not set, recvmsg() will return data. See 1074 Section 5.3 for more information about notifications. 1076 If all portions of a data frame or notification have been read, 1077 recvmsg() will return with MSG_EOR set in msg_flags. 1079 5.2. SCTP msg_control Structures 1081 A key element of all SCTP-specific socket extensions is the use of 1082 ancillary data to specify and access SCTP-specific data via the 1083 struct msghdr's msg_control member used in sendmsg() and recvmsg(). 1084 Fine-grained control over initialization and sending parameters are 1085 handled with ancillary data. 1087 Each ancillary data item is proceeded by a struct cmsghdr (see 1088 Section 5.1), which defines the function and purpose of the data 1089 contained in the cmsg_data[] member. 1091 By default on either style socket, SCTP will pass no ancillary data; 1092 Specific ancillary data items can be enabled with socket options 1093 defined for SCTP; see Section 7.4. 1095 Note that all ancillary types are fixed length; see Section 5.4 for 1096 further discussion on this. These data structures use struct 1097 sockaddr_storage (defined in [RFC3493]) as a portable, fixed length 1098 address format. 1100 Other protocols may also provide ancillary data to the socket layer 1101 consumer. These ancillary data items from other protocols may 1102 intermingle with SCTP data. For example, the IPv6 socket API 1103 definitions ([RFC3542] and [RFC3493]) define a number of ancillary 1104 data items. If a socket API consumer enables delivery of both SCTP 1105 and IPv6 ancillary data, they both may appear in the same msg_control 1106 buffer in any order. An application may thus need to handle other 1107 types of ancillary data besides those passed by SCTP. 1109 The sockets application must provide a buffer large enough to 1110 accommodate all ancillary data provided via recvmsg(). If the buffer 1111 is not large enough, the ancillary data will be truncated and the 1112 msghdr's msg_flags will include MSG_CTRUNC. 1114 5.2.1. SCTP Initiation Structure (SCTP_INIT) 1116 This cmsghdr structure provides information for initializing new SCTP 1117 associations with sendmsg(). The SCTP_INITMSG socket option uses 1118 this same data structure. This structure is not used for recvmsg(). 1120 +--------------+-----------+---------------------+ 1121 | cmsg_level | cmsg_type | cmsg_data[] | 1122 +--------------+-----------+---------------------+ 1123 | IPPROTO_SCTP | SCTP_INIT | struct sctp_initmsg | 1124 +--------------+-----------+---------------------+ 1126 Here is the definition of the sctp_initmsg structure: 1128 struct sctp_initmsg { 1129 uint16_t sinit_num_ostreams; 1130 uint16_t sinit_max_instreams; 1131 uint16_t sinit_max_attempts; 1132 uint16_t sinit_max_init_timeo; 1133 }; 1135 sinit_num_ostreams: This is an integer number representing the 1136 number of streams that the application wishes to be able to send 1137 to. This number is confirmed in the SCTP_COMM_UP notification and 1138 must be verified since it is a negotiated number with the remote 1139 endpoint. The default value of 0 indicates to use the endpoint 1140 default value. 1141 sinit_max_instreams: This value represents the maximum number of 1142 inbound streams the application is prepared to support. This 1143 value is bounded by the actual implementation. In other words the 1144 user may be able to support more streams than the Operating 1145 System. In such a case, the Operating System limit overrides the 1146 value requested by the user. The default value of 0 indicates to 1147 use the endpoints default value. 1148 sinit_max_attempts: This integer specifies how many attempts the 1149 SCTP endpoint should make at resending the INIT. This value 1150 overrides the system SCTP 'Max.Init.Retransmits' value. The 1151 default value of 0 indicates to use the endpoints default value. 1152 This is normally set to the system's default 'Max.Init.Retransmit' 1153 value. 1154 sinit_max_init_timeo: This value represents the largest Time-Out or 1155 RTO value (in milliseconds) to use in attempting an INIT. 1156 Normally the 'RTO.Max' is used to limit the doubling of the RTO 1157 upon timeout. For the INIT message this value may override 1158 'RTO.Max'. This value must not influence 'RTO.Max' during data 1159 transmission and is only used to bound the initial setup time. A 1160 default value of 0 indicates to use the endpoints default value. 1161 This is normally set to the system's 'RTO.Max' value (60 seconds). 1163 5.2.2. SCTP Header Information Structure (SCTP_SNDRCV) 1165 This cmsghdr structure specifies SCTP options for sendmsg() and 1166 describes SCTP header information about a received message through 1167 recvmsg(). This structure mixes the send and receive path. 1169 SCTP_SNDINFO described in Section 5.2.4 and SCTP_RCVINFO described in 1170 Section 5.2.5 split this information. These structures should be 1171 used, when possible, since SCTP_SNDRCV might be deprecated in the 1172 future. 1174 +--------------+-------------+------------------------+ 1175 | cmsg_level | cmsg_type | cmsg_data[] | 1176 +--------------+-------------+------------------------+ 1177 | IPPROTO_SCTP | SCTP_SNDRCV | struct sctp_sndrcvinfo | 1178 +--------------+-------------+------------------------+ 1180 Here is the definition of sctp_sndrcvinfo: 1182 struct sctp_sndrcvinfo { 1183 uint16_t sinfo_stream; 1184 uint16_t sinfo_ssn; 1185 uint16_t sinfo_flags; 1186 uint32_t sinfo_ppid; 1187 uint32_t sinfo_context; 1188 uint32_t sinfo_pr_value; 1189 uint32_t sinfo_tsn; 1190 uint32_t sinfo_cumtsn; 1191 sctp_assoc_t sinfo_assoc_id; 1192 }; 1194 sinfo_stream: For recvmsg() the SCTP stack places the message's 1195 stream number in this value. For sendmsg() this value holds the 1196 stream number that the application wishes to send this message to. 1197 If a sender specifies an invalid stream number an error indication 1198 is returned and the call fails. 1199 sinfo_ssn: For recvmsg() this value contains the stream sequence 1200 number that the remote endpoint placed in the DATA chunk. For 1201 fragmented messages this is the same number for all deliveries of 1202 the message (if more than one recvmsg() is needed to read the 1203 message). The sendmsg() call will ignore this parameter. 1204 sinfo_flags: This field may contain any of the following flags and 1205 is composed of a bitwise OR of these values. 1206 recvmsg() flags: 1207 SCTP_UNORDERED: This flag is present when the message was sent 1208 non-ordered. 1209 sendmsg() flags: 1210 SCTP_UNORDERED: This flag requests the un-ordered delivery of 1211 the message. If this flag is clear the datagram is 1212 considered an ordered send. 1214 SCTP_ADDR_OVER: This flag, in the one-to-many style, requests 1215 the SCTP stack to override the primary destination address 1216 with the address found with the sendto/sendmsg call. 1217 SCTP_ABORT: Setting this flag causes the specified association 1218 to abort by sending an ABORT message to the peer (one-to- 1219 many style only). The ABORT chunk will contain an error 1220 cause 'User Initiated Abort' with cause code 12. The cause 1221 specific information of this error cause is provided in 1222 msg_iov. 1223 SCTP_EOF: Setting this flag invokes the SCTP graceful shutdown 1224 procedure on the specified association. Graceful shutdown 1225 assures that all data queued by both endpoints is 1226 successfully transmitted before closing the association 1227 (one-to-many style only). 1228 SCTP_SENDALL: This flag, if set, will cause a one-to-many 1229 model socket to send the message to all associations that 1230 are currently established on this socket. For the one-to- 1231 one socket, this flag has no effect. 1232 sinfo_ppid: This value in sendmsg() is an unsigned integer that is 1233 passed to the remote end in each user message. In recvmsg() this 1234 value is the same information that was passed by the upper layer 1235 in the peer application. Please note that the SCTP stack performs 1236 no byte order modification of this field. For example, if the 1237 DATA chunk has to contain a given value in network byte order, the 1238 SCTP user has to perform the htonl() computation. 1239 sinfo_context: This value is an opaque 32 bit context datum that is 1240 used in the sendmsg() function. This value is passed back to the 1241 upper layer if an error occurs on the send of a message and is 1242 retrieved with each undelivered message (Note: if an endpoint has 1243 done multiple sends, all of which fail, multiple different 1244 sinfo_context values will be returned. One with each user data 1245 message). 1246 sinfo_pr_value: The meaning of this field depends on the PR-SCTP 1247 policy specified by the sinfo_pr_policy field. It is ignored when 1248 SCTP_PR_SCTP_NONE is specified. In case of SCTP_PR_SCTP_TTL the 1249 lifetime is specified. 1250 sinfo_tsn: For the receiving side, this field holds a TSN that was 1251 assigned to one of the SCTP Data Chunks. 1252 sinfo_cumtsn: This field will hold the current cumulative TSN as 1253 known by the underlying SCTP layer. Note this field is ignored 1254 when sending. 1255 sinfo_assoc_id: The association handle field, sinfo_assoc_id, holds 1256 the identifier for the association announced in the SCTP_COMM_UP 1257 notification. All notifications for a given association have the 1258 same identifier. Ignored for one-to-one style sockets. 1260 An sctp_sndrcvinfo item always corresponds to the data in msg_iov. 1262 5.2.3. Extended SCTP Header Information Structure (SCTP_EXTRCV) 1264 This cmsghdr structure specifies SCTP options for SCTP header 1265 information about a received message via recvmsg(). Note that this 1266 structure is an extended version of SCTP_SNDRCV (see Section 5.2.2) 1267 and will only be received if the user has set the socket option 1268 SCTP_USE_EXT_RCVINFO to true in addition to any event subscription 1269 needed to receive ancillary data. Note that next message data is not 1270 valid unless the current message is completely read, i.e. the MSG_EOR 1271 is set, in other words if you have more data to read from the current 1272 message then no next message information will be available. 1274 SCTP_NXTINFO described in Section 5.2.6 should be used when possible, 1275 since SCTP_EXTRCV is considered deprecated. 1277 +--------------+-------------+------------------------+ 1278 | cmsg_level | cmsg_type | cmsg_data[] | 1279 +--------------+-------------+------------------------+ 1280 | IPPROTO_SCTP | SCTP_EXTRCV | struct sctp_extrcvinfo | 1281 +--------------+-------------+------------------------+ 1283 Here is the definition of sctp_extrcvinfo structure: 1285 struct sctp_extrcvinfo { 1286 uint16_t sinfo_stream; 1287 uint16_t sinfo_ssn; 1288 uint16_t sinfo_flags; 1289 uint32_t sinfo_ppid; 1290 uint32_t sinfo_context; 1291 uint32_t sinfo_pr_value; 1292 uint32_t sinfo_tsn; 1293 uint32_t sinfo_cumtsn; 1294 uint16_t serinfo_next_flags; 1295 uint16_t serinfo_next_stream; 1296 uint32_t serinfo_next_aid; 1297 uint32_t serinfo_next_length; 1298 uint32_t serinfo_next_ppid; 1299 sctp_assoc_t sinfo_assoc_id; 1300 }; 1302 sinfo_*: Please see Section 5.2.2 for the details for these fields. 1303 serinfo_next_flags: This bitmask will hold one or more of the 1304 following values: 1305 SCTP_NEXT_MSG_AVAIL: This bit, when set to 1, indicates that next 1306 message information is available i.e.: next_stream, 1307 next_asocid, next_length and next_ppid fields all have valid 1308 values. If this bit is set to 0, then these fields are not 1309 valid and should be ignored. 1311 SCTP_NEXT_MSG_ISCOMPLETE: This bit, when set, indicates that the 1312 next message is completely in the receive buffer. The 1313 next_length field thus contains the entire message size. If 1314 this flag is set to 0, then the next_length field only contains 1315 part of the message size since the message is still being 1316 received (it is being partially delivered). 1317 SCTP_NEXT_MSG_IS_UNORDERED: This bit, when set, indicates that 1318 the next message to be received was sent by the peer as 1319 unordered. If this bit is not set (i.e the bit is 0) the next 1320 message to be read is an ordered message in the stream 1321 specified. 1322 SCTP_NEXT_MSG_IS_NOTIFICATION: This bit, when set, indicates that 1323 the next message to be received is not a message from the peer, 1324 but instead is a MSG_NOTIFICATION from the local SCTP stack. 1325 serinfo_next_stream: This value, when valid (see 1326 serinfo_next_flags), contains the next stream number that will be 1327 received on a subsequent call to one of the receive message 1328 functions. 1329 serinfo_next_aid: This value, when valid (see serinfo_next_flags), 1330 contains the next association identification that will be received 1331 on a subsequent call to one of the receive message functions. 1332 serinfo_next_length: This value, when valid (see 1333 serinfo_next_flags), contains the length of the next message that 1334 will be received on a subsequent call to one of the receive 1335 message functions. Note that this length may be a partial length 1336 depending on the settings of next_flags. 1337 serinfo_next_ppid: This value, when valid (see serinfo_next_flags), 1338 contains the ppid of the next message that will be received on a 1339 subsequent call to one of the receive message functions. 1341 5.2.4. SCTP Send Information Structure (SCTP_SNDINFO) 1343 This cmsghdr structure specifies SCTP options for sendmsg(). 1345 +--------------+--------------+---------------------+ 1346 | cmsg_level | cmsg_type | cmsg_data[] | 1347 +--------------+--------------+---------------------+ 1348 | IPPROTO_SCTP | SCTP_SNDINFO | struct sctp_sndinfo | 1349 +--------------+--------------+---------------------+ 1351 Here is the definition of the sctp_sndinfo structure: 1353 struct sctp_sndinfo { 1354 uint16_t snd_sid; 1355 uint16_t snd_flags; 1356 uint32_t snd_ppid; 1357 uint32_t snd_context; 1358 sctp_assoc_t snd_assoc_id; 1360 }; 1362 snd_sid: This value holds the stream number that the application 1363 wishes to send this message to. If a sender specifies an invalid 1364 stream number an error indication is returned and the call fails. 1365 snd_flags: This field may contain any of the following flags and is 1366 composed of a bitwise OR of these values. 1368 SCTP_UNORDERED: This flag requests the un-ordered delivery of the 1369 message. If this flag is clear the datagram is considered an 1370 ordered send. 1371 SCTP_ADDR_OVER: This flag, in the one-to-many style, requests the 1372 SCTP stack to override the primary destination address with the 1373 address found with the sendto/sendmsg call. 1374 SCTP_ABORT: Setting this flag causes the specified association to 1375 abort by sending an ABORT message to the peer (one-to-many 1376 style only). The ABORT chunk will contain an error cause 'User 1377 Initiated Abort' with cause code 12. The cause specific 1378 information of this error cause is provided in msg_iov. 1379 SCTP_EOF: Setting this flag invokes the SCTP graceful shutdown 1380 procedures on the specified association. Graceful shutdown 1381 assures that all data queued by both endpoints is successfully 1382 transmitted before closing the association (one-to-many style 1383 only). 1384 SCTP_SENDALL: This flag, if set, will cause a one-to-many model 1385 socket to send the message to all associations that are 1386 currently established on this socket. For the one-to-one 1387 socket, this flag has no effect. 1388 snd_ppid: This value in sendmsg() is an unsigned integer that is 1389 passed to the remote end in each user message. Please note that 1390 the SCTP stack performs no byte order modification of this field. 1391 For example, if the DATA chunk has to contain a given value in 1392 network byte order, the SCTP user has to perform the htonl() 1393 computation. 1394 snd_context: This value is an opaque 32 bit context datum that is 1395 used in the sendmsg() function. This value is passed back to the 1396 upper layer if an error occurs on the send of a message and is 1397 retrieved with each undelivered message (Note: if an endpoint has 1398 done multiple sends, all of which fail, multiple different 1399 sinfo_context values will be returned. One with each user data 1400 message). 1401 snd_assoc_id: The association handle field, sinfo_assoc_id, holds 1402 the identifier for the association announced in the SCTP_COMM_UP 1403 notification. All notifications for a given association have the 1404 same identifier. Ignored for one-to-one style sockets. 1406 An sctp_sndinfo item always corresponds to the data in msg_iov. 1408 5.2.5. SCTP Receive Information Structure (SCTP_RCVINFO) 1410 This cmsghdr structure describes SCTP header information about a 1411 received message through recvmsg(). 1413 To receive this information you must subscribe to the SCTP_RCV_EVENT 1414 using the SCTP_EVENT option. 1416 +--------------+--------------+---------------------+ 1417 | cmsg_level | cmsg_type | cmsg_data[] | 1418 +--------------+--------------+---------------------+ 1419 | IPPROTO_SCTP | SCTP_RCVINFO | struct sctp_rcvinfo | 1420 +--------------+--------------+---------------------+ 1422 Here is the definition of the sctp_rcvinfo structure: 1424 struct sctp_rcvinfo { 1425 uint16_t rcv_sid; 1426 uint16_t rcv_ssn; 1427 uint16_t rcv_flags; 1428 uint32_t rcv_ppid; 1429 uint32_t rcv_tsn; 1430 uint32_t rcv_cumtsn; 1431 sctp_assoc_t rcv_assoc_id; 1432 }; 1434 rcv_sid: The SCTP stack places the message's stream number in this 1435 value. 1436 rcv_ssn: This value contains the stream sequence number that the 1437 remote endpoint placed in the DATA chunk. For fragmented messages 1438 this is the same number for all deliveries of the message (if more 1439 than one recvmsg() is needed to read the message). 1440 rcv_flags: This field may contain any of the following flags and is 1441 composed of a bitwise OR of these values. 1443 SCTP_UNORDERED: This flag is present when the message was sent 1444 non-ordered. 1445 rcv_ppid: This value is the same information that was passed by the 1446 upper layer in the peer application. Please note that the SCTP 1447 stack performs no byte order modification of this field. For 1448 example, if the DATA chunk has to contain a given value in network 1449 byte order, the SCTP user has to perform the htonl() computation. 1450 rcv_tsn: This field holds a TSN that was assigned to one of the SCTP 1451 Data Chunks. 1453 rcv_cumtsn: This field will hold the current cumulative TSN as known 1454 by the underlying SCTP layer. 1455 rcv_assoc_id: The association handle field, sinfo_assoc_id, holds 1456 the identifier for the association announced in the SCTP_COMM_UP 1457 notification. All notifications for a given association have the 1458 same identifier. Ignored for one-to-one style sockets. 1460 A sctp_rcvinfo item always corresponds to the data in msg_iov. 1462 5.2.6. SCTP Next Receive Information Structure (SCTP_NXTINFO) 1464 This cmsghdr structure describes SCTP receive information of the next 1465 message which will be delivered through recvmsg() if this information 1466 is available. It uses the same structure as the SCTP Receive 1467 Information Structure. 1469 To receive this information you must subscribe to the SCTP_NXT_EVENT 1470 using the SCTP_EVENT option. 1472 +--------------+--------------+---------------------+ 1473 | cmsg_level | cmsg_type | cmsg_data[] | 1474 +--------------+--------------+---------------------+ 1475 | IPPROTO_SCTP | SCTP_NXTINFO | struct sctp_rcvinfo | 1476 +--------------+--------------+---------------------+ 1478 5.2.7. SCTP PR-SCTP Information Structure (SCTP_PRINFO) 1480 This cmsghdr structure specifies SCTP options for sendmsg(). 1482 +--------------+-------------+--------------------+ 1483 | cmsg_level | cmsg_type | cmsg_data[] | 1484 +--------------+-------------+--------------------+ 1485 | IPPROTO_SCTP | SCTP_PRINFO | struct sctp_prinfo | 1486 +--------------+-------------+--------------------+ 1488 Here is the definition of the sctp_prinfo structure: 1490 struct sctp_prinfo { 1491 uint16_t pr_policy; 1492 uint32_t pr_value; 1493 }; 1495 pr_policy: This specifies which PR-SCTP policy is used. Using 1496 SCTP_PR_SCTP_NONE results in a reliable transmission. When 1497 SCTP_PR_SCTP_TTL is used, the PR-SCTP policy "timed reliability" 1498 defined in [RFC3758] is used. In this case, the lifetime is 1499 provided in pr_value. 1501 pr_value: The meaning of this field depends on the PR-SCTP policy 1502 specified by the sinfo_pr_policy field. It is ignored when 1503 SCTP_PR_SCTP_NONE is specified. In case of SCTP_PR_SCTP_TTL the 1504 lifetime in milliseconds is specified. 1506 An sctp_prinfo item always corresponds to the data in msg_iov. 1508 5.2.8. SCTP AUTH Information Structure (SCTP_AUTHINFO) 1510 This cmsghdr structure specifies SCTP options for sendmsg(). 1512 +--------------+---------------+----------------------+ 1513 | cmsg_level | cmsg_type | cmsg_data[] | 1514 +--------------+---------------+----------------------+ 1515 | IPPROTO_SCTP | SCTP_AUTHINFO | struct sctp_authinfo | 1516 +--------------+---------------+----------------------+ 1518 Here is the definition of the sctp_authinfo structure: 1520 struct sctp_authinfo { 1521 uint16_t auth_keyid; 1522 }; 1524 auth_keyid: This specifies the shared key identifier used for 1525 sending the user message. 1527 An sctp_authinfo item always corresponds to the data in msg_iov. 1529 5.3. SCTP Events and Notifications 1531 An SCTP application may need to understand and process events and 1532 errors that happen on the SCTP stack. These events include network 1533 status changes, association startups, remote operational errors and 1534 undeliverable messages. All of these can be essential for the 1535 application. 1537 When an SCTP application layer does a recvmsg() the message read is 1538 normally a data message from a peer endpoint. If the application 1539 wishes to have the SCTP stack deliver notifications of non-data 1540 events, it sets the appropriate socket option for the notifications 1541 it wants. See Section 7.4 for these socket options. When a 1542 notification arrives, recvmsg() returns the notification in the 1543 application-supplied data buffer via msg_iov, and sets 1544 MSG_NOTIFICATION in msg_flags. 1546 This section details the notification structures. Every notification 1547 structure carries some common fields which provide general 1548 information. 1550 A recvmsg() call will return only one notification at a time. Just 1551 as when reading normal data, it may return part of a notification if 1552 the msg_iov buffer is not large enough. If a single read is not 1553 sufficient, msg_flags will have MSG_EOR clear. The user must finish 1554 reading the notification before subsequent data can arrive. 1556 5.3.1. SCTP Notification Structure 1558 The notification structure is defined as the union of all 1559 notification types. 1561 union sctp_notification { 1562 struct sctp_tlv { 1563 uint16_t sn_type; /* Notification type. */ 1564 uint16_t sn_flags; 1565 uint32_t sn_length; 1566 } sn_header; 1567 struct sctp_assoc_change sn_assoc_change; 1568 struct sctp_paddr_change sn_paddr_change; 1569 struct sctp_remote_error sn_remote_error; 1570 struct sctp_send_failed sn_send_failed; 1571 struct sctp_shutdown_event sn_shutdown_event; 1572 struct sctp_adaptation_event sn_adaptation_event; 1573 struct sctp_pdapi_event sn_pdapi_event; 1574 struct sctp_authkey_event sn_auth_event; 1575 struct sctp_sender_dry_event sn_sender_dry_event; 1576 }; 1578 sn_type: The following list describes the SCTP notification and 1579 event types for the field sn_type. 1580 SCTP_ASSOC_CHANGE: This tag indicates that an association has 1581 either been opened or closed. Refer to Section 5.3.2 for 1582 details. 1583 SCTP_PEER_ADDR_CHANGE: This tag indicates that an address that is 1584 part of an existing association has experienced a change of 1585 state (e.g. a failure or return to service of the reachability 1586 of an endpoint via a specific transport address). Please see 1587 Section 5.3.3 for data structure details. 1588 SCTP_REMOTE_ERROR: The attached error message is an Operational 1589 Error received from the remote peer. It includes the complete 1590 TLV sent by the remote endpoint. See Section 5.3.4 for the 1591 detailed format. 1592 SCTP_SEND_FAILED: The attached datagram could not be sent to the 1593 remote endpoint. This structure includes the original 1594 SCTP_SNDRCVINFO that was used in sending this message i.e. this 1595 structure uses the sctp_sndrcvinfo per Section 5.3.5. 1597 SCTP_SHUTDOWN_EVENT: The peer has sent a SHUTDOWN. No further 1598 data should be sent on this socket. 1599 SCTP_ADAPTATION_INDICATION: This notification holds the peer's 1600 indicated adaptation layer. Please see Section 5.3.7. 1601 SCTP_PARTIAL_DELIVERY_EVENT: This notification is used to tell a 1602 receiver that the partial delivery has been aborted. This may 1603 indicate the association is about to be aborted. Please see 1604 Section 5.3.8. 1605 SCTP_AUTHENTICATION_EVENT: This notification is used to tell a 1606 receiver that either an error occurred on authentication, or a 1607 new key was made active. See Section 5.3.9. 1608 SCTP_SENDER_DRY_EVENT: This notification is used to inform the 1609 application that the sender has no user data queued anymore, 1610 neither for transmission nor retransmission. See 1611 Section 5.3.10. 1612 sn_flags: These are notification-specific flags. 1613 sn_length: This is the length of the whole sctp_notification 1614 structure including the sn_type, sn_flags, and sn_length fields. 1616 5.3.2. SCTP_ASSOC_CHANGE 1618 Communication notifications inform the ULP that an SCTP association 1619 has either begun or ended. The identifier for a new association is 1620 provided by this notification. The notification information has the 1621 following format: 1623 struct sctp_assoc_change { 1624 uint16_t sac_type; 1625 uint16_t sac_flags; 1626 uint32_t sac_length; 1627 uint16_t sac_state; 1628 uint16_t sac_error; 1629 uint16_t sac_outbound_streams; 1630 uint16_t sac_inbound_streams; 1631 sctp_assoc_t sac_assoc_id; 1632 uint8_t sac_info[]; 1633 }; 1635 sac_type: It should be SCTP_ASSOC_CHANGE. 1636 sac_flags: Currently unused. 1637 sac_length: This field is the total length of the notification data, 1638 including the notification header. 1639 sac_state: This field holds one of a number of values that 1640 communicate the event that happened to the association. They 1641 include: 1643 SCTP_COMM_UP: A new association is now ready and data may be 1644 exchanged with this peer. When an association has been 1645 established successfully, this notification should be the first 1646 one. 1647 SCTP_COMM_LOST: The association has failed. The association is 1648 now in the closed state. If SEND FAILED notifications are 1649 turned on, a SCTP_COMM_LOST is accompanied by a series of 1650 SCTP_SEND_FAILED events, one for each outstanding message. 1651 SCTP_RESTART: SCTP has detected that the peer has restarted. 1652 SCTP_SHUTDOWN_COMP: The association has gracefully closed. 1653 SCTP_CANT_STR_ASSOC: The association failed to setup. If non 1654 blocking mode is set and data was sent (on a one-to-many style 1655 socket), a SCTP_CANT_STR_ASSOC is accompanied by a series of 1656 SCTP_SEND_FAILED events, one for each outstanding message. 1657 sac_error: If the state was reached due to an error condition (e.g. 1658 SCTP_COMM_LOST) any relevant error information is available in 1659 this field. This corresponds to the protocol error codes defined 1660 in [RFC4960]. 1661 sac_outbound_streams: 1662 sac_inbound_streams: The maximum number of streams allowed in each 1663 direction are available in sac_outbound_streams and sac_inbound 1664 streams. 1665 sac_assoc_id: The association id field holds the identifier for the 1666 association. All notifications for a given association have the 1667 same association identifier. For a one-to-one style socket, this 1668 field is ignored. 1669 sac_info: If the sac_state is SCTP_COMM_LOST and an ABORT chunk was 1670 received for this association, sac_info[] contains the complete 1671 ABORT chunk as defined in the SCTP specification [RFC4960] section 1672 3.3.7. If the sac_state is SCTP_COMM_UP or SCTP_RESTART, sac_info 1673 may contain an array of features that the current association 1674 supports. Features may include 1675 SCTP_PR: Both endpoints support the protocol extension described 1676 in [RFC3758]. 1677 SCTP_AUTH: Both endpoints support the protocol extension 1678 described in [RFC4895]. 1679 SCTP_ASCONF: Both endpoints support the protocol extension 1680 described in [RFC5061]. 1681 SCTP_MULTIBUF: For a one-to-many style socket, the local 1682 endpoints use separate send and/or receive buffers for each 1683 SCTP association. 1685 5.3.3. SCTP_PEER_ADDR_CHANGE 1687 When a destination address of a multi-homed peer encounters a change 1688 a peer address change event is sent. The information has the 1689 following structure: 1691 struct sctp_paddr_change { 1692 uint16_t spc_type; 1693 uint16_t spc_flags; 1694 uint32_t spc_length; 1695 struct sockaddr_storage spc_aaddr; 1696 uint32_t spc_state; 1697 uint32_t spc_error; 1698 sctp_assoc_t spc_assoc_id; 1699 } 1701 spc_type: It should be SCTP_PEER_ADDR_CHANGE. 1702 spc_flags: Currently unused. 1703 spc_length: This field is the total length of the notification data, 1704 including the notification header. 1705 spc_aaddr: The affected address field holds the remote peer's 1706 address that is encountering the change of state. 1707 spc_state: This field holds one of a number of values that 1708 communicate the event that happened to the address. They include: 1709 SCTP_ADDR_AVAILABLE: This address is now reachable. 1710 SCTP_ADDR_UNREACHABLE: The address specified can no longer be 1711 reached. Any data sent to this address is rerouted to an 1712 alternate until this address becomes reachable. 1713 SCTP_ADDR_REMOVED: The address is no longer part of the 1714 association. 1715 SCTP_ADDR_ADDED: The address is now part of the association. 1716 SCTP_ADDR_MADE_PRIM: This address has now been made to be the 1717 primary destination address. 1718 SCTP_ADDR_CONFIRMED: This address has now been confirmed as a 1719 valid address. 1720 spc_error: If the state was reached due to any error condition (e.g. 1721 SCTP_ADDR_UNREACHABLE) any relevant error information is available 1722 in this field. 1723 spc_assoc_id: The association id field holds the identifier for the 1724 association. All notifications for a given association have the 1725 same association identifier. For a one-to-one style socket, this 1726 field is ignored. 1728 5.3.4. SCTP_REMOTE_ERROR 1730 A remote peer may send an Operational Error message to its peer. 1731 This message indicates a variety of error conditions on an 1732 association. The entire ERROR chunk as it appears on the wire is 1733 included in an SCTP_REMOTE_ERROR event. Please refer to the SCTP 1734 specification [RFC4960] and any extensions for a list of possible 1735 error formats. SCTP error notifications have the format: 1737 struct sctp_remote_error { 1738 uint16_t sre_type; 1739 uint16_t sre_flags; 1740 uint32_t sre_length; 1741 uint16_t sre_error; 1742 sctp_assoc_t sre_assoc_id; 1743 uint8_t sre_data[]; 1744 }; 1746 sre_type: It should be SCTP_REMOTE_ERROR. 1747 sre_flags: Currently unused. 1748 sre_length: This field is the total length of the notification data, 1749 including the notification header and the contents of sre_data. 1750 sre_error: This value represents one of the Operational Error causes 1751 defined in the SCTP specification, in network byte order. 1752 sre_assoc_id: The association id field holds the identifier for the 1753 association. All notifications for a given association have the 1754 same association identifier. For a one-to-one style socket, this 1755 field is ignored. 1756 sre_data: This contains the ERROR chunk as defined in the SCTP 1757 specification [RFC4960] section 3.3.10. 1759 5.3.5. SCTP_SEND_FAILED 1761 If SCTP cannot deliver a message it may return the message as a 1762 notification. 1764 struct sctp_send_failed { 1765 uint16_t ssf_type; 1766 uint16_t ssf_flags; 1767 uint32_t ssf_length; 1768 uint32_t ssf_error; 1769 struct sctp_sndrcvinfo ssf_info; 1770 sctp_assoc_t ssf_assoc_id; 1771 uint8_t ssf_data[]; 1772 }; 1774 ssf_type: It should be SCTP_SEND_FAILED. 1775 ssf_flags: The flag value will take one of the following values: 1776 SCTP_DATA_UNSENT: Indicates that the data was never put on the 1777 wire. 1778 SCTP_DATA_SENT: Indicates that the data was put on the wire. 1779 Note that this does not necessarily mean that the data was (or 1780 was not) successfully delivered. 1782 ssf_length: This field is the total length of the notification data, 1783 including the notification header and the payload in ssf_data. 1784 ssf_error: This value represents the reason why the send failed, and 1785 if set, will be an SCTP protocol error code as defined in 1786 [RFC4960] section 3.3.10. 1787 ssf_info: The send information associated with the undelivered 1788 message. The ssf_info.sinfo_flags field will also contain an 1789 indication if the beginning of the message and/or end of the 1790 message is present. In cases where no data has been sent on the 1791 wire, this field will have or'ed in the value SCTP_DATA_NOT_FRAG, 1792 which is a composition of both a "BEGIN" and "END" fragmentation 1793 bit. In cases where only part of the data has been sent, this 1794 field will have or'ed in the value SCTP_DATA_LAST_FRAG, which 1795 corresponds to the "END" bit. Note that the message itself may be 1796 more than one chunk. If the ssf_info.sinfo_flags field holds 1797 neither of these two values then a piece that has been fragmented 1798 and sent but not acknowledged is present. This piece is from an 1799 unspecified position in the message and the application can make 1800 no assumptions about the data itself. Applications wanting to 1801 examine a recovered message should look for the 1802 SCTP_DATA_NOT_FRAG. Without this flag the application should 1803 assume part of the message arrived and take appropriate steps to 1804 audit and recover any lost or missing data. 1805 ssf_assoc_id: The association id field, ssf_assoc_id, holds the 1806 identifier for the association. All notifications for a given 1807 association have the same association identifier. For a one-to- 1808 one style socket, this field is ignored. 1809 ssf_data: The undelivered message or part of the undelivered message 1810 will be present in the ssf_data field. Note that the 1811 ssf_info.sinfo_flags field as noted above should be used to 1812 determine if a complete message is present or just a piece of the 1813 message. Note that only user data is present in this field, any 1814 chunk headers or SCTP common headers must be removed by the SCTP 1815 stack. 1817 5.3.6. SCTP_SHUTDOWN_EVENT 1819 When a peer sends a SHUTDOWN, SCTP delivers this notification to 1820 inform the application that it should cease sending data. 1822 struct sctp_shutdown_event { 1823 uint16_t sse_type; 1824 uint16_t sse_flags; 1825 uint32_t sse_length; 1826 sctp_assoc_t sse_assoc_id; 1827 }; 1829 sse_type: It should be SCTP_SHUTDOWN_EVENT. 1830 sse_flags: Currently unused. 1831 sse_length: This field is the total length of the notification data, 1832 including the notification header. It will generally be sizeof 1833 (struct sctp_shutdown_event). 1834 sse_flags: Currently unused. 1835 sse_assoc_id: The association id field holds the identifier for the 1836 association. All notifications for a given association have the 1837 same association identifier. For a one-to-one style socket, this 1838 field is ignored. 1840 5.3.7. SCTP_ADAPTATION_INDICATION 1842 When a peer sends an Adaptation Layer Indication parameter as 1843 described in [RFC5061], SCTP delivers this notification to inform the 1844 application about the peer's adaptation layer indication. 1846 struct sctp_adaptation_event { 1847 uint16_t sai_type; 1848 uint16_t sai_flags; 1849 uint32_t sai_length; 1850 uint32_t sai_adaptation_ind; 1851 sctp_assoc_t sai_assoc_id; 1852 }; 1854 sai_type: It should be SCTP_ADAPTATION_INDICATION. 1855 sai_flags: Currently unused. 1856 sai_length: This field is the total length of the notification data, 1857 including the notification header. It will generally be sizeof 1858 (struct sctp_adaptation_event). 1859 sai_adaptation_ind: This field holds the bit array sent by the peer 1860 in the adaptation layer indication parameter. The bits are in 1861 network byte order. 1862 sai_assoc_id: The association id field holds the identifier for the 1863 association. All notifications for a given association have the 1864 same association identifier. For a one-to-one style socket, this 1865 field is ignored. 1867 5.3.8. SCTP_PARTIAL_DELIVERY_EVENT 1869 When a receiver is engaged in a partial delivery of a message this 1870 notification will be used to indicate various events. 1872 struct sctp_pdapi_event { 1873 uint16_t pdapi_type; 1874 uint16_t pdapi_flags; 1875 uint32_t pdapi_length; 1876 uint32_t pdapi_indication; 1877 uint32_t pdapi_stream; 1878 uint32_t pdapi_seq; 1879 sctp_assoc_t pdapi_assoc_id; 1880 }; 1882 pdapi_type: It should be SCTP_PARTIAL_DELIVERY_EVENT. 1883 pdapi_flags: Currently unused. 1884 pdapi_length: This field is the total length of the notification 1885 data, including the notification header. It will generally be 1886 sizeof(struct sctp_pdapi_event). 1887 pdapi_indication: This field holds the indication being sent to the 1888 application. Possible values include: 1889 SCTP_PARTIAL_DELIVERY_ABORTED: This notification indicates that 1890 the partial delivery of a user message has been aborted. 1891 pdapi_stream: This field holds the stream on which the partial 1892 delivery event happened. 1893 pdapi_seq: This field holds the stream sequence number which was 1894 partially delivered. 1895 pdapi_assoc_id: The association id field holds the identifier for 1896 the association. All notifications for a given association have 1897 the same association identifier. For a one-to-one style socket 1898 this field is ignored. 1900 5.3.9. SCTP_AUTHENTICATION_EVENT 1902 When a receiver is using authentication this message will provide 1903 notifications regarding new keys being made active as well as errors. 1905 struct sctp_authkey_event { 1906 uint16_t auth_type; 1907 uint16_t auth_flags; 1908 uint32_t auth_length; 1909 uint16_t auth_keynumber; 1910 uint16_t auth_altkeynumber; 1911 uint32_t auth_indication; 1912 sctp_assoc_t auth_assoc_id; 1913 }; 1915 auth_type: It should be SCTP_AUTHENTICATION_EVENT. 1917 auth_flags: Currently unused. 1918 auth_length: This field is the total length of the notification 1919 data, including the notification header. It will generally be 1920 sizeof (struct sctp_authkey_event). 1921 auth_keynumber: This field holds the keynumber set by the user for 1922 the effected key. If more than one key is involved, this will 1923 contain one of the keys involved in the notification. 1924 auth_altkeynumber: This field holds an alternate keynumber which is 1925 used by some notifications. 1926 auth_indication: This field holds the error or indication being 1927 reported. The following values are currently defined: 1928 SCTP_AUTH_NEWKEY: This report indicates that a new key has been 1929 made active (used for the first time by the peer) and is now 1930 the active key. The auth_keynumber field holds the user 1931 specified key number. 1932 SCTP_AUTH_NO_AUTH: This report indicates that the peer does not 1933 support SCTP-AUTH. 1934 SCTP_AUTH_FREE_KEY: This report indicates that the SCTP 1935 implementation will not use the key identifier specified in 1936 auth_keynumber anymore. 1937 auth_assoc_id: The association id field holds the identifier for the 1938 association. All notifications for a given association have the 1939 same association identifier. For a one-to-one style socket this 1940 field is ignored. 1942 5.3.10. SCTP_SENDER_DRY_EVENT 1944 When the SCTP implementation has no user data anymore to send or 1945 retransmit, this notification is given to the user. If the user 1946 subscribes to this event and SCTP has at this point of time no user 1947 data to send or retransmit, this notification is also given to the 1948 user. 1950 struct sctp_sender_dry_event { 1951 uint16_t sender_dry_type; 1952 uint16_t sender_dry_flags; 1953 uint32_t sender_dry_length; 1954 sctp_assoc_t sender_dry_assoc_id; 1955 }; 1957 sender_dry_type: It should be SCTP_SENDER_DRY_EVENT. 1958 sender_dry_flags: Currently unused. 1959 sender_dry_length: This field is the total length of the 1960 notification data, including the notification header. It will 1961 generally be sizeof(struct sctp_sender_dry_event). 1963 5.3.11. SCTP_NOTIFICATIONS_STOPPED_EVENT 1965 Notifications, when subscribed to, are reliable. They are always 1966 delivered as long as there is space in the socket receive buffer. 1967 However, if an implementation experiences a notification storm, it 1968 may run out of socket buffer space. When this occurs it may wish to 1969 disable notifications. If the implementation chooses to do this, it 1970 will append a final notification SCTP_NOTIFICATIONS_STOPPED_EVENT. 1971 This notification is an empty sctp_tlv (see the union above), that 1972 merely has this type in the sn_type field, the sn_length field set to 1973 the sizeof an sctp_tlv structure and the sn_flags set to 0. If an 1974 application receives this notification, it will need to resubscribe 1975 to any notifications of interest to it. 1977 5.4. Ancillary Data Considerations and Semantics 1979 Programming with ancillary socket data contains some subtleties and 1980 pitfalls, which are discussed below. 1982 5.4.1. Multiple Items and Ordering 1984 Multiple ancillary data items may be included in any call to 1985 sendmsg() or recvmsg(); these may include multiple SCTP or non-SCTP 1986 items, or both. 1988 The ordering of ancillary data items (either by SCTP or another 1989 protocol) is not significant and is implementation-dependent, so 1990 applications must not depend on any ordering. 1992 SCTP_SNDRCV items must always correspond to the data in the msghdr's 1993 msg_iov member. There can be only a single SCTP_SNDRCV info for each 1994 sendmsg() or recvmsg() call. 1996 5.4.2. Accessing and Manipulating Ancillary Data 1998 Applications can infer the presence of data or ancillary data by 1999 examining the msg_iovlen and msg_controllen msghdr members, 2000 respectively. 2002 Implementations may have different padding requirements for ancillary 2003 data, so portable applications should make use of the macros 2004 CMSG_FIRSTHDR, CMSG_NXTHDR, CMSG_DATA, CMSG_SPACE, and CMSG_LEN. See 2005 [RFC3542] and your SCTP implementation's documentation for more 2006 information. The following is an example, from [RFC3542], 2007 demonstrating the use of these macros to access ancillary data: 2009 struct msghdr msg; 2010 struct cmsghdr *cmsgptr; 2012 /* fill in msg */ 2014 /* call recvmsg() */ 2016 for (cmsgptr = CMSG_FIRSTHDR(&msg); cmsgptr != NULL; 2017 cmsgptr = CMSG_NXTHDR(&msg, cmsgptr)) { 2018 if (cmsgptr->cmsg_level == ... && cmsgptr->cmsg_type == ... ) { 2019 u_char *ptr; 2021 ptr = CMSG_DATA(cmsgptr); 2022 /* process data pointed to by ptr */ 2023 } 2024 } 2026 5.4.3. Control Message Buffer Sizing 2028 The information conveyed via SCTP_SNDRCV events will often be 2029 fundamental to the correct and sane operation of the sockets 2030 application. This is particularly true of the one-to-many semantics, 2031 but also of the one-to-one semantics. For example, if an application 2032 needs to send and receive data on different SCTP streams, SCTP_SNDRCV 2033 events are indispensable. 2035 Given that some ancillary data is critical, and that multiple 2036 ancillary data items may appear in any order, applications should be 2037 carefully written to always provide a large enough buffer to contain 2038 all possible ancillary data that can be presented by recvmsg(). If 2039 the buffer is too small, and crucial data is truncated, it may pose a 2040 fatal error condition. 2042 Thus, it is essential that applications be able to deterministically 2043 calculate the maximum required buffer size to pass to recvmsg(). One 2044 constraint imposed on this specification that makes this possible is 2045 that all ancillary data definitions are of a fixed length. One way 2046 to calculate the maximum required buffer size might be to take the 2047 sum the sizes of all enabled ancillary data item structures, as 2048 calculated by CMSG_SPACE. For example, if we enabled 2049 SCTP_SNDRCV_INFO and IPV6_RECVPKTINFO [RFC3542], we would calculate 2050 and allocate the buffer size as follows: 2052 size_t total; 2053 void *buf; 2055 total = CMSG_SPACE(sizeof (struct sctp_sndrcvinfo)) + 2056 CMSG_SPACE(sizeof (struct in6_pktinfo)); 2058 buf = malloc(total); 2060 We could then use this buffer (buf) for msg_control on each call to 2061 recvmsg() and be assured that we would not lose any ancillary data to 2062 truncation. 2064 6. Common Operations for Both Styles 2066 6.1. send(), recv(), sendto(), and recvfrom() 2068 Applications can use send() and sendto() to transmit data to the peer 2069 of an SCTP endpoint. recv() and recvfrom() can be used to receive 2070 data from the peer. 2072 The function prototypes are 2074 ssize_t send(int sd, 2075 const void *msg, 2076 size_t len, 2077 int flags); 2079 ssize_t sendto(int sd, 2080 const void *msg, 2081 size_t len, 2082 int flags, 2083 const struct sockaddr *to, 2084 socklen_t tolen); 2086 ssize_t recv(int sd, 2087 void *buf, 2088 size_t len, 2089 int flags); 2091 ssize_t recvfrom(int sd, 2092 void *buf, 2093 size_t len, 2094 int flags, 2095 struct sockaddr *from, 2096 socklen_t *fromlen); 2098 and the arguments are 2099 sd: The socket descriptor of an SCTP endpoint. 2100 msg: The message to be sent. 2101 len: the size of the message or the size of the buffer. 2102 to: one of the peer addresses of the association to be used to send 2103 the message. 2104 tolen: The size of the address. 2105 buf: The buffer to store a received message. 2106 from: The buffer to store the peer address used to send the received 2107 message. 2108 fromlen: The size of the from address. 2109 flags: (described below). 2111 These calls give access to only basic SCTP protocol features. If 2112 either peer in the association uses multiple streams, or sends 2113 unordered data, these calls will usually be inadequate, and may 2114 deliver the data in unpredictable ways. 2116 SCTP has the concept of multiple streams in one association. The 2117 above calls do not allow the caller to specify on which stream a 2118 message should be sent. The system uses stream 0 as the default 2119 stream for send() and sendto(). recv() and recvfrom() return data 2120 from any stream, but the caller can not distinguish the different 2121 streams. This may result in data seeming to arrive out of order. 2122 Similarly, if a data chunk is sent unordered, recv() and recvfrom() 2123 provide no indication. 2125 SCTP is message based. The msg buffer above in send() and sendto() 2126 is considered to be a single message. This means that if the caller 2127 wants to send a message which is composed by several buffers, the 2128 caller needs to combine them before calling send() or sendto(). 2129 Alternately, the caller can use sendmsg() to do that without 2130 combining them. Sending a message using send() or sendto() is atomic 2131 unless explicit EOR marking is enabled on the socket specified by sd. 2132 Using sendto() on a non-connected one-to-one style socket for 2133 implicit connection setup may or may not work depending on the SCTP 2134 implementation. recv() and recvfrom() cannot distinguish message 2135 boundaries. 2137 In receiving, if the buffer supplied is not large enough to hold a 2138 complete message, the receive call acts like a stream socket and 2139 returns as much data as will fit in the buffer. 2141 Note, the send() and recv() calls may not be used for a one-to-many 2142 style socket. 2144 Note, if an application calls a send function with no user data and 2145 no ancillary data the SCTP implementation should reject the request 2146 with an appropriate error message. An implementation is NOT allowed 2147 to send a DATA chunk with no user data [RFC4960]. 2149 6.2. setsockopt() and getsockopt() 2151 Applications use setsockopt() and getsockopt() to set or retrieve 2152 socket options. Socket options are used to change the default 2153 behavior of socket calls. They are described in Section 7. 2155 The function prototypes are 2157 int getsockopt(int sd, 2158 int level, 2159 int optname, 2160 void *optval, 2161 socklen_t *optlen); 2163 and 2165 int setsockopt(int sd, 2166 int level, 2167 int optname, 2168 const void *optval, 2169 socklen_t optlen); 2171 and the arguments are 2172 sd: The socket descriptor. 2173 level: Set to IPPROTO_SCTP for all SCTP options. 2174 optname: The option name. 2175 optval: The buffer to store the value of the option. 2176 optlen: The size of the buffer (or the length of the option 2177 returned). 2179 All socket options set on a one-to-one style listening socket also 2180 apply to all accepted sockets. For one-to-many style sockets often a 2181 socket option will pass a structure that includes an assoc_id field. 2182 This field can be filled with the association id of a particular 2183 association and unless otherwise specified can be filled with one of 2184 the following constants: 2185 SCTP_FUTURE_ASSOC: Specifies that only future associations created 2186 after this socket option will be effected by this call. 2187 SCTP_CURRENT_ASSOC: Specifies that only currently existing 2188 associations will be effected by this call, future associations 2189 will still receive the previous default value. 2191 SCTP_ALL_ASSOC: Specifies that all current and future associations 2192 will be effected by this call. 2194 6.3. read() and write() 2196 Applications can use read() and write() to send and receive data to 2197 and from a peer. They have the same semantics as send() and recv() 2198 except that the flags parameter cannot be used. 2200 Note, these calls, when used in the one-to-many style, should only be 2201 used with branched off socket descriptors (see Section 8.2). 2203 6.4. getsockname() 2205 Applications use getsockname() to retrieve the locally-bound socket 2206 address of the specified socket. This is especially useful if the 2207 caller let SCTP chose a local port. This call is for single homed 2208 endpoints. It does not work well with multi-homed endpoints. See 2209 Section 8.5 for a multi-homed version of the call. 2211 The function prototype is 2213 int getsockname(int sd, 2214 struct sockaddr *address, 2215 socklen_t *len); 2217 and the arguments are 2218 sd: The socket descriptor to be queried. 2219 address: On return, one locally bound address (chosen by the SCTP 2220 stack) is stored in this buffer. If the socket is an IPv4 socket, 2221 the address will be IPv4. If the socket is an IPv6 socket, the 2222 address will be either an IPv6 or IPv4 address. 2223 len: The caller should set the length of the address here. On 2224 return, this is set to the length of the returned address. 2226 If the actual length of the address is greater than the length of the 2227 supplied sockaddr structure, the stored address will be truncated. 2229 If the socket has not been bound to a local name, the value stored in 2230 the object pointed to by address is unspecified. 2232 7. Socket Options 2234 The following sub-section describes various SCTP level socket options 2235 that are common to both styles. SCTP associations can be multi- 2236 homed. Therefore, certain option parameters include a 2237 sockaddr_storage structure to select which peer address the option 2238 should be applied to. 2240 For the one-to-many style sockets, an sctp_assoc_t structure 2241 (association ID) is used to identify the association instance that 2242 the operation affects. So it must be set when using this style. 2244 For the one-to-one style sockets and branched off one-to-many style 2245 sockets (see Section 8.2) this association ID parameter is ignored. 2247 Note that socket or IP level options are set or retrieved per socket. 2248 This means that for one-to-many style sockets, those options will be 2249 applied to all associations belonging to the socket. And for one-to- 2250 one style, those options will be applied to all peer addresses of the 2251 association controlled by the socket. Applications should be very 2252 careful in setting those options. 2254 For some IP stacks getsockopt() is read-only; so a new interface will 2255 be needed when information must be passed both into and out of the 2256 SCTP stack. The syntax for sctp_opt_info() is 2258 int sctp_opt_info(int sd, 2259 sctp_assoc_t id, 2260 int opt, 2261 void *arg, 2262 socklen_t *size); 2264 The sctp_opt_info() call is a replacement for getsockopt() only and 2265 will not set any options associated with the specified socket. A 2266 setsockopt() must be used to set any writeable option. 2268 For one-to-many style sockets, id specifies the association to query. 2269 For one-to-one style sockets, id is ignored. 2271 The field opt specifies which SCTP socket option to get. It can get 2272 any socket option currently supported that requests information 2273 (either read/write options or read only) such as: 2274 SCTP_RTOINFO 2275 SCTP_ASSOCINFO 2276 SCTP_DEFAULT_SEND_PARAM 2277 SCTP_GET_PEER_ADDR_INFO 2278 SCTP_PRIMARY_ADDR 2279 SCTP_PEER_ADDR_PARAMS 2280 SCTP_STATUS 2281 SCTP_CONTEXT 2282 SCTP_AUTH_ACTIVE_KEY 2283 SCTP_PEER_AUTH_CHUNKS 2284 SCTP_LOCAL_AUTH_CHUNKS 2286 The arg field is an option-specific structure buffer provided by the 2287 caller. See Section 8.5 subsections for more information on these 2288 options and option-specific structures. 2290 sctp_opt_info() returns 0 on success, or on failure returns -1 and 2291 sets errno to the appropriate error code. 2293 All options that support specific settings on an association by 2294 filling in either an association id variable or a sockaddr_storage 2295 should also support the setting of the same value for the entire 2296 endpoint (i.e. future associations). To accomplish this the 2297 following logic is used when setting one of these options: 2298 o If an address is specified via a sockaddr_storage that is included 2299 in the structure, the address is used to lookup the association 2300 and the settings are applied to the specific address (if 2301 appropriate) or to the entire association. 2302 o If an association identification is filled in but not a 2303 sockaddr_storage (if present), the association is found using the 2304 association identification and the settings should be applied to 2305 the entire association (since a specific address is not 2306 specified). Note this also applies to options that hold an 2307 association identification in their structure but do not have a 2308 sockaddr_storage field. 2309 o If neither the sockaddr_storage nor association identification is 2310 set, i.e. the sockaddr_storage is set to all 0 (INADDR_ANY) and 2311 the association identification is SCTP_FUTURE_ASSOC, the settings 2312 are a default and to be applied to the endpoint. 2314 7.1. Read / Write Options 2316 7.1.1. Retransmission Timeout Parameters (SCTP_RTOINFO) 2318 The protocol parameters used to initialize and limit the 2319 retransmission timeout (RTO) are tunable. See [RFC4960] for more 2320 information on how these parameters are used in RTO calculation. 2322 The following structure is used to access and modify these 2323 parameters: 2325 struct sctp_rtoinfo { 2326 sctp_assoc_t srto_assoc_id; 2327 uint32_t srto_initial; 2328 uint32_t srto_max; 2329 uint32_t srto_min; 2331 }; 2333 srto_initial: This contains the initial RTO value. 2334 srto_max and srto_min: These contain the maximum and minimum bounds 2335 for all RTOs. 2336 srto_assoc_id: This parameter is ignored for one-to-one style 2337 sockets. For one-to-many style sockets the application may fill 2338 in an association identification or one of the predefined 2339 constants. 2341 All times are given in milliseconds. A value of 0, when modifying 2342 the parameters, indicates that the current value should not be 2343 changed. 2345 To access or modify these parameters, the application should call 2346 getsockopt() or setsockopt() respectively with the option name 2347 SCTP_RTOINFO. 2349 7.1.2. Association Parameters (SCTP_ASSOCINFO) 2351 This option is used to both examine and set various association and 2352 endpoint parameters. See [RFC4960] for more information on how this 2353 parameter is used. 2355 The following structure is used to access and modify these 2356 parameters: 2358 struct sctp_assocparams { 2359 sctp_assoc_t sasoc_assoc_id; 2360 uint16_t sasoc_asocmaxrxt; 2361 uint16_t sasoc_number_peer_destinations; 2362 uint32_t sasoc_peer_rwnd; 2363 uint32_t sasoc_local_rwnd; 2364 uint32_t sasoc_cookie_life; 2365 }; 2367 sasoc_assoc_id: This parameter is ignored for one-to-one style 2368 sockets. For one-to-many style sockets the application may fill 2369 in an association identification or one of the predefined 2370 constants. 2371 sasoc_asocmaxrxt: This contains the maximum retransmission attempts 2372 to make for the association. 2373 sasoc_number_peer_destinations: This is the number of destination 2374 addresses that the peer has. 2376 sasoc_peer_rwnd: This holds the current value of the peers rwnd 2377 (reported in the last SACK) minus any outstanding data (i.e. data 2378 in flight). 2379 sasoc_local_rwnd: This holds the last reported rwnd that was sent to 2380 the peer. 2381 sasoc_cookie_life: This is the association's cookie life value used 2382 when issuing cookies. 2384 The values of the sasoc_peer_rwnd is meaningless when examining 2385 endpoint information. 2387 All time values are given in milliseconds. A value of 0, when 2388 modifying the parameters, indicates that the current value should not 2389 be changed. 2391 The values of the sasoc_asocmaxrxt and sasoc_cookie_life may be set 2392 on either an endpoint or association basis. The rwnd and destination 2393 counts (sasoc_number_peer_destinations, sasoc_peer_rwnd, 2394 sasoc_local_rwnd) are NOT settable and any value placed in these is 2395 ignored. 2397 To access or modify these parameters, the application should call 2398 getsockopt() or setsockopt() respectively with the option name 2399 SCTP_ASSOCINFO. 2401 The maximum number of retransmissions before an address is considered 2402 unreachable is also tunable, but is address-specific, so it is 2403 covered in a separate option. If an application attempts to set the 2404 value of the association maximum retransmission parameter to more 2405 than the sum of all maximum retransmission parameters, setsockopt() 2406 may return an error. The reason for this, from [RFC4960] section 2407 8.2: 2409 Note: When configuring the SCTP endpoint, the user should avoid 2410 having the value of 'Association.Max.Retrans' larger than the 2411 summation of the 'Path.Max.Retrans' of all the destination addresses 2412 for the remote endpoint. Otherwise, all the destination addresses 2413 may become inactive while the endpoint still considers the peer 2414 endpoint reachable. 2416 7.1.3. Initialization Parameters (SCTP_INITMSG) 2418 Applications can specify protocol parameters for the default 2419 association initialization. The structure used to access and modify 2420 these parameters is defined in Section 5.2.1. The option name 2421 argument to setsockopt() and getsockopt() is SCTP_INITMSG. 2423 Setting initialization parameters is effective only on an unconnected 2424 socket (for one-to-many style sockets only future associations are 2425 effected by the change). With one-to-one style sockets, this option 2426 is inherited by sockets derived from a listening socket. 2428 7.1.4. SO_LINGER 2430 An application can use this option to perform the SCTP ABORT 2431 primitive. This option affects all associations related to the 2432 socket. 2434 The linger option structure is: 2436 struct linger { 2437 int l_onoff; /* option on/off */ 2438 int l_linger; /* linger time */ 2439 }; 2441 To enable the option, set l_onoff to 1. If the l_linger value is set 2442 to 0, calling close() is the same as the ABORT primitive. If the 2443 value is set to a negative value, the setsockopt() call will return 2444 an error. If the value is set to a positive value linger_time, the 2445 close() can be blocked for at most linger_time ms. If the graceful 2446 shutdown phase does not finish during this period, close() will 2447 return but the graceful shutdown phase will continue in the system. 2449 Note, this is a socket level option NOT an SCTP level option. So 2450 when setting SO_LINGER you must specify a level of SOL_SOCKET in the 2451 setsockopt() call. 2453 7.1.5. SCTP_NODELAY 2455 Turn on/off any Nagle-like algorithm. This means that packets are 2456 generally sent as soon as possible and no unnecessary delays are 2457 introduced, at the cost of more packets in the network. Expects an 2458 integer boolean flag. Turning this option on disables any Nagle-like 2459 algorithm. 2461 7.1.6. SO_RCVBUF 2463 Sets the receive buffer size in octets. For SCTP one-to-one style 2464 sockets, this controls the receiver window size. For one-to-many 2465 style sockets the meaning is implementation dependent. It might 2466 control the receive buffer for each association bound to the socket 2467 descriptor or it might control the receive buffer for the whole 2468 socket. The call expects an integer. 2470 7.1.7. SO_SNDBUF 2472 Sets the send buffer size. For SCTP one-to-one style sockets, this 2473 controls the amount of data SCTP may have waiting in internal buffers 2474 to be sent. This option therefore bounds the maximum size of data 2475 that can be sent in a single send call. For one-to-many style 2476 sockets, the effect is the same, except that it applies to one or all 2477 associations (see Section 3.4) bound to the socket descriptor used in 2478 the setsockopt() or getsockopt() call. The option applies to each 2479 association's window size separately. The call expects an integer. 2481 7.1.8. Automatic Close of Associations (SCTP_AUTOCLOSE) 2483 This socket option is applicable to the one-to-many style socket 2484 only. When set it will cause associations that are idle for more 2485 than the specified number of seconds to automatically close using the 2486 graceful shutdown procedure. An association being idle is defined as 2487 an association that has NOT sent or received user data. The special 2488 value of '0' indicates that no automatic close of any association 2489 should be performed, this is the default value. The option expects 2490 an integer defining the number of seconds of idle time before an 2491 association is closed. 2493 An application using this option should enable receiving the 2494 association change notification. This is the only mechanism an 2495 application is informed about the closing of an association. After 2496 an association is closed, the association ID assigned to it can be 2497 reused. An application should be aware of this to avoid the possible 2498 problem of sending data to an incorrect peer endpoint. 2500 7.1.9. Set Primary Address (SCTP_PRIMARY_ADDR) 2502 Requests that the local SCTP stack uses the enclosed peer address as 2503 the association's primary. The enclosed address must be one of the 2504 association peer's addresses. 2506 The following structure is used to make a set peer primary request: 2508 struct sctp_setprim { 2509 sctp_assoc_t ssp_assoc_id; 2510 struct sockaddr_storage ssp_addr; 2511 }; 2513 ssp_addr: The address to set as primary. 2515 ssp_assoc_id: This parameter is ignored for one-to-one style 2516 sockets. For one-to-many style sockets it identifies the 2517 association for this request. Note that the predefined constants 2518 are NOT allowed. 2520 7.1.10. Set Adaptation Layer Indicator (SCTP_ADAPTATION_LAYER) 2522 Requests that the local endpoint set the specified Adaptation Layer 2523 Indication parameter for all future INIT and INIT-ACK exchanges. 2525 The following structure is used to access and modify this parameter: 2527 struct sctp_setadaptation { 2528 uint32_t ssb_adaptation_ind; 2529 }; 2531 ssb_adaptation_ind: The adaptation layer indicator that will be 2532 included in any outgoing Adaptation Layer Indication parameter. 2534 7.1.11. Enable/Disable Message Fragmentation (SCTP_DISABLE_FRAGMENTS) 2536 This option is a on/off flag and is passed as an integer where a non- 2537 zero is on and a zero is off. If enabled no SCTP message 2538 fragmentation will be performed. Instead, if a message being sent 2539 exceeds the current PMTU size, the message will NOT be sent and 2540 instead an error will be indicated to the user. 2542 7.1.12. Peer Address Parameters (SCTP_PEER_ADDR_PARAMS) 2544 Applications can enable or disable heartbeats for any peer address of 2545 an association, modify an address's heartbeat interval, force a 2546 heartbeat to be sent immediately, and adjust the address's maximum 2547 number of retransmissions sent before an address is considered 2548 unreachable. 2550 The following structure is used to access and modify an address's 2551 parameters: 2553 struct sctp_paddrparams { 2554 sctp_assoc_t spp_assoc_id; 2555 struct sockaddr_storage spp_address; 2556 uint32_t spp_hbinterval; 2557 uint16_t spp_pathmaxrxt; 2558 uint32_t spp_pathmtu; 2559 uint32_t spp_flags; 2560 uint32_t spp_ipv6_flowlabel; 2561 uint8_t spp_ipv4_tos; 2562 }; 2563 spp_assoc_id: This parameter is ignored for one-to-one style 2564 sockets. For one-to-many style sockets it identifies the 2565 association for this query. Note that the predefined constants 2566 are NOT allowed. 2567 spp_address: This specifies which address is of interest. 2568 spp_hbinterval: This contains the value of the heartbeat interval, 2569 in milliseconds. Note that unless the spp_flag is set to 2570 SPP_HB_ENABLE the value of this field is ignored. Note also that 2571 a value of zero indicates the current setting should be left 2572 unchanged. To set an actual value of zero the use of the flag 2573 SPP_HB_TIME_IS_ZERO should be used. 2574 spp_pathmaxrxt: This contains the maximum number of retransmissions 2575 before this address shall be considered unreachable. Note that a 2576 value of zero indicates the current setting should be left 2577 unchanged. 2578 spp_pathmtu: When Path MTU discovery is disabled the value specified 2579 here will be the "fixed" path MTU (i.e. the value of the spp_flags 2580 field must include the flag SPP_PMTUD_DISABLE). Note that if the 2581 spp_address field is empty then all destinations for this 2582 association will have this fixed path MTU set upon them. If an 2583 address is specified, then only that address will be effected. 2584 Note also that this option cannot be set on the endpoint, but must 2585 be set on each individual association. Also, when disabling PMTU 2586 discovery, the implementation may disallow this behavior if the 2587 "fixed" path MTU is below the constant value SCTP_SMALLEST_PMTU. 2588 spp_ipv6_flowlabel: This field is used in conjunction with the 2589 SPP_IPV6_FLOWLABEL flag. 2590 spp_ipv4_tos: This field is used in conjunction with the 2591 SPP_IPV4_TOS flag. 2592 spp_flags: These flags are used to control various features on an 2593 association. The flag field is a bit mask which may contain zero 2594 or more of the following options: 2595 SPP_HB_ENABLE: Enable heartbeats on the specified address. Note 2596 that if the address field is empty all addresses for the 2597 association have heartbeats enabled upon them. 2598 SPP_HB_DISABLE: Disable heartbeats on the specified address. 2599 Note that if the address field is empty all addresses for the 2600 association will have their heartbeats disabled. Note also 2601 that SPP_HB_ENABLE and SPP_HB_DISABLE are mutually exclusive, 2602 only one of these two should be specified. Enabling both 2603 fields will have undetermined results. 2604 SPP_HB_DEMAND: Request a user initiated heartbeat to be made 2605 immediately. 2606 SPP_HB_TIME_IS_ZERO: Specifies that the time for heartbeat delay 2607 is to be set to the value of 0 milliseconds. 2609 SPP_PMTUD_ENABLE: This field will enable PMTU discovery upon the 2610 specified address. Note that if the address field is empty 2611 then all addresses on the association are effected. 2612 SPP_PMTUD_DISABLE: This field will disable PMTU discovery upon 2613 the specified address. Note that if the address field is empty 2614 then all addresses on the association are effected. Note also 2615 that SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually 2616 exclusive. Enabling both will have undetermined results. 2617 SPP_IPV6_FLOWLABEL: Setting this flag enables the setting of the 2618 IPV6 flowlabel value associated with either the association or 2619 the specific address. If the address field is filled in, then 2620 the specific destination address has this value set upon it. 2621 If the association is specified, but not the address, then the 2622 flowlabel value is set for any future destination addresses 2623 that may be added. The value is obtained in the 2624 spp_ipv6_flowlabel field. 2626 Upon retrieval, this flag will be set to indicate that the 2627 spp_ipv6_flowlabel field has a valid value returned. If a 2628 specific destination address is set (in the spp_address field) 2629 when called then the value returned is that of the address. If 2630 just an association is specified (and no address) then the 2631 association's default flowlabel is returned. If neither an 2632 association nor a destination is specified, then the socket's 2633 default flowlabel is returned. For non IPv6 sockets, this flag 2634 will be left cleared. 2635 SPP_IPV4_TOS: Setting this flag enables the setting of the IPV4 2636 TOS value associated with either the association or a specific 2637 address. If the address field is filled in, then the specific 2638 destination address has this value set upon it. If the 2639 association is specified, but not the address, then the TOS 2640 value is set for any future destination addresses that may be 2641 added. The value is obtained in the spp_ipv4_tos field. 2643 Upon retrieval, this flag will be set to indicate that the 2644 spp_ipv4_tos field has a valid value returned. If a specific 2645 destination address is set when called (in the spp_address 2646 field) then that specific destination address' TOS value is 2647 returned. If just an association is specified then the 2648 association default TOS is returned. If neither an association 2649 nor an destination is specified, then the sockets default TOS 2650 is returned. For non IPv4 sockets, this flag will be left 2651 cleared. 2653 To read or modify these parameters, the application should call 2654 sctp_opt_info() with the SCTP_PEER_ADDR_PARAMS option. 2656 7.1.13. Set Default Send Parameters (SCTP_DEFAULT_SEND_PARAM) 2658 Applications that wish to use the sendto() system call may wish to 2659 specify a default set of parameters that would normally be supplied 2660 through the inclusion of ancillary data. This socket option allows 2661 such an application to set the default sctp_sndrcvinfo structure. 2662 The application that wishes to use this socket option simply passes 2663 the sctp_sndrcvinfo structure defined in Section 5.2.2 to this call. 2664 The input parameters accepted by this call include sinfo_stream, 2665 sinfo_flags, sinfo_ppid, sinfo_context, sinfo_pr_policy and 2666 sinfo_pr_value. The sinfo_flags is composed of a bitwise OR of 2667 SCTP_UNORDERED, SCTP_EOF, and SCTP_SENDALL. The sinfo_assoc_id field 2668 specifies the association to apply the parameters to. In a one-to- 2669 many style sockets any of the predefined constants are also allowed 2670 in this field. The field is ignored on the one-to-one style. 2672 7.1.14. Set Notification and Ancillary Events (SCTP_EVENTS) 2674 This socket option is used to specify various notifications and 2675 ancillary data the user wishes to receive. Please see Section 7.4 2676 for a full description of this option and its usage. Note that this 2677 option is considered deprecated and present for backward 2678 compatibility. New applications should use the SCTP_SET_EVENT 2679 option. See Section 7.4 for a full description of that option as 2680 well. 2682 7.1.15. Set/Clear IPv4 Mapped Addresses (SCTP_I_WANT_MAPPED_V4_ADDR) 2684 This socket option is a boolean flag which turns on or off the 2685 mapping of IPv4 addresses. If this option is turned on and the 2686 socket is type PF_INET6, then IPv4 addresses will be mapped to V6 2687 representation. If this option is turned off, then no mapping will 2688 be done of V4 addresses and a user will receive both PF_INET6 and 2689 PF_INET type addresses on the socket. 2691 By default this option is turned off and expects an integer to be 2692 passed where non-zero turns on the option and zero turns off the 2693 option. 2695 7.1.16. Get or Set the Maximum Fragmentation Size (SCTP_MAXSEG) 2697 This option will get or set the maximum size to put in any outgoing 2698 SCTP DATA chunk. If a message is larger than this size it will be 2699 fragmented by SCTP into the specified size. Note that the underlying 2700 SCTP implementation may fragment into smaller sized chunks when the 2701 PMTU of the underlying association is smaller than the value set by 2702 the user. The default value for this option is '0' which indicates 2703 the user is NOT limiting fragmentation and only the PMTU will effect 2704 SCTP's choice of DATA chunk size. Note also that values set larger 2705 than the maximum size of an IP datagram will effectively let SCTP 2706 control fragmentation (i.e. the same as setting this option to 0). 2708 The following structure is used to access and modify this parameter: 2710 struct sctp_assoc_value { 2711 sctp_assoc_t assoc_id; 2712 uint32_t assoc_value; 2713 }; 2715 assoc_id: This parameter is ignored for one-to-one style sockets. 2716 For one-to-many style sockets this parameter indicates which 2717 association the user is performing an action upon. Note that any 2718 of the predefined constants are also allowed in this field. 2719 assoc_value: This parameter specifies the maximum size in bytes. 2721 7.1.17. Get or Set the List of Supported HMAC Identifiers 2722 (SCTP_HMAC_IDENT) 2724 This option gets or sets the list of HMAC algorithms that the local 2725 endpoint requires the peer to use. 2727 The following structure is used to get or set these identifiers: 2729 struct sctp_hmacalgo { 2730 uint32_t shmac_number_of_idents; 2731 uint16_t shmac_idents[]; 2732 }; 2734 shmac_number_of_idents: This field gives the number of elements 2735 present in the array shmac_idents. 2736 shmac_idents: This parameter contains an array of HMAC Identifiers 2737 that the local endpoint is requesting the peer to use, in priority 2738 order. The following identifiers are valid: 2739 * SCTP_AUTH_HMAC_ID_SHA1 2740 * SCTP_AUTH_HMAC_ID_SHA256 2742 Note that the list supplied must include SCTP_AUTH_HMAC_ID_SHA1 and 2743 may include any of the other values in its preferred order (lowest 2744 list position has the highest preference in algorithm selection). 2745 Note also that the lack of SCTP_AUTH_HMAC_ID_SHA1, or the inclusion 2746 of an unknown HMAC identifier (including optional identifiers unknown 2747 to the implementation) will cause the set option to fail and return 2748 an error. 2750 7.1.18. Get or Set the Active Shared Key (SCTP_AUTH_ACTIVE_KEY) 2752 This option will get or set the active shared key to be used to build 2753 the association shared key. 2755 The following structure is used to access and modify these 2756 parameters: 2758 struct sctp_authkeyid { 2759 sctp_assoc_t scact_assoc_id; 2760 uint16_t scact_keynumber; 2761 }; 2763 scact_assoc_id: This parameter, if non-zero, indicates the 2764 association that the shared key identifier is set active upon. 2765 Note that if this element contains zero, then the activation 2766 applies to the endpoint and all future associations will use the 2767 specified shared key identifier. For one-to-one sockets, this 2768 parameter is ignored. Note, however, that this option will set 2769 the active key on the association if the socket is connected, 2770 otherwise this will set the default active key for the endpoint. 2771 scact_keynumber: This parameter is the shared key identifier which 2772 the application is requesting to become the active shared key to 2773 be used for sending authenticated chunks. The key identifier must 2774 correspond to an existing shared key. Note that shared key 2775 identifier '0' defaults to a null key. 2777 When used with setsockopt() the SCTP implementation must use the 2778 indicated shared key identifier for all messages being given to an 2779 SCTP implementation via a send call after the setsockopt() call until 2780 changed again. Therefore, the SCTP implementation must not bundle 2781 user messages which should be authenticated using different shared 2782 key identifiers. 2784 Initially the key with key identifier 0 is the active key. 2786 7.1.19. Get or Set Delayed SACK Timer (SCTP_DELAYED_SACK) 2788 This option will effect the way delayed acks are performed. This 2789 option allows you to get or set the delayed ack time, in 2790 milliseconds. It also allows changing the delayed ack frequency. 2791 Changing the frequency to 1 disables the delayed sack algorithm. If 2792 the sack_assoc_id is 0, then this sets or gets the endpoints default 2793 values. If the sack_assoc_id field is non-zero, then the set or get 2794 effects the specified association for the one-to-many model (the 2795 assoc_id field is ignored by the one-to-one model). Note that if 2796 sack_delay or sack_freq are 0 when setting this option, the current 2797 values will remain unchanged. 2799 The following structure is used to access and modify these 2800 parameters: 2802 struct sctp_sack_info { 2803 sctp_assoc_t sack_assoc_id; 2804 uint32_t sack_delay; 2805 uint32_t sack_freq; 2806 }; 2808 sack_assoc_id: This parameter is ignored for one-to-one style 2809 sockets. For one-to-many style sockets this parameter indicates 2810 which association the user is performing an action upon. Note 2811 that any of the predefined constants may also be used for one-to- 2812 many style sockets. 2813 sack_delay: This parameter contains the number of milliseconds that 2814 the user is requesting the delayed ACK timer to be set to. Note 2815 that this value is defined in the standard to be between 200 and 2816 500 milliseconds. 2817 sack_freq: This parameter contains the number of packets that must 2818 be received before a sack is sent without waiting for the delay 2819 timer to expire. The default value is 2, setting this value to 1 2820 will disable the delayed sack algorithm. 2822 7.1.20. Get or Set Fragmented Interleave (SCTP_FRAGMENT_INTERLEAVE) 2824 Fragmented interleave controls how the presentation of messages 2825 occurs for the message receiver. There are three levels of fragment 2826 interleave defined. Two of the levels effect the one-to-one model, 2827 while the one-to-many model is effected by all three levels. 2829 This option takes an integer value. It can be set to a value of 0, 1 2830 or 2. Attempting to set this level to other values will return an 2831 error. 2833 Setting the three levels provides the following receiver 2834 interactions: 2836 level 0: Prevents the interleaving of any messages. This means that 2837 when a partial delivery begins, no other messages will be received 2838 except the message being partially delivered. If another message 2839 arrives on a different stream (or association) that could be 2840 delivered, it will be blocked waiting for the user to read all of 2841 the partially delivered message. 2842 level 1: Allows interleaving of messages that are from different 2843 associations. For the one-to-one model, level 0 and level 1 thus 2844 have the same meaning since a one-to-one socket always receives 2845 messages from the same association. Note that setting the one-to- 2846 many model to this level may cause multiple partial deliveries 2847 from different associations but for any given association, only 2848 one message will be delivered until all parts of a message have 2849 been delivered. This means that one large message, being read 2850 with an association identification of "X", will block other 2851 messages from association "X" from being delivered. 2852 level 2: Allows complete interleaving of messages. This level 2853 requires that the sender carefully observes not only the peer 2854 association identification (or address) but must also pay careful 2855 attention to the stream number. With this option enabled a 2856 partially delivered message may begin being delivered for 2857 association "X" stream "Y" and the next subsequent receive may 2858 return a message from association "X" stream "Z". Note that no 2859 other messages would be delivered for association "X" stream "Y" 2860 until all of stream "Y"'s partially delivered message was read. 2861 Note that this option also effects the one-to-one model. Also 2862 note that for the one-to-many model not only may another streams 2863 message from the same association be delivered from the next 2864 receive, some other associations message may be delivered upon the 2865 next receive. 2867 An implementation should default the one-to-many model to level 1. 2868 The reason for this is that otherwise it is possible that a peer 2869 could begin sending a partial message and thus block all other peers 2870 from sending data. However a setting of level 2 requires the 2871 application to not only be aware of the association (via the 2872 association id or peer's address) but also the stream number. The 2873 stream number is NOT present unless the user has subscribed to the 2874 sctp_data_io_events (see Section 7.4). This is also why we recommend 2875 that the one-to-one model be defaulted to level 0 (level 1 for the 2876 one-to-one model has no effect). Note that an implementation should 2877 return an error if an application attempts to set the level to 2 and 2878 has NOT subscribed to the sctp_data_io_events. 2880 For applications that have subscribed to events those events appear 2881 in the normal socket buffer data stream. This means that unless the 2882 user has set the fragmentation interleave level to 0, notifications 2883 may also be interleaved with partially delivered messages. 2885 7.1.21. Set or Get the SCTP Partial Delivery Point 2886 (SCTP_PARTIAL_DELIVERY_POINT) 2888 This option will set or get the SCTP partial delivery point. This 2889 point is the size of a message where the partial delivery API will be 2890 invoked to help free up rwnd space for the peer. Setting this to a 2891 lower value will cause partial deliveries to happen more often. The 2892 call's argument is an integer that sets or gets the partial delivery 2893 point. Note also that the call will fail if the user attempts to set 2894 this value larger than the socket receive buffer size. 2896 Note that any single message having a length smaller than or equal to 2897 the SCTP partial delivery point will be delivered in one single read 2898 call as long as the user provided buffer is large enough to hold the 2899 message. 2901 7.1.22. Set or Get the Use of Extended Receive Info 2902 (SCTP_USE_EXT_RCVINFO) 2904 This option will enable or disable the use of the extended version of 2905 the sctp_sndrcvinfo structure. If this option is disabled, then the 2906 normal sctp_sndrcvinfo structure is returned in all receive message 2907 calls. If this option is enabled then the sctp_extrcvinfo structure 2908 is returned in all receive message calls. This option is present for 2909 compatibility with older applications and is deprecated. Future 2910 applications should use SCTP_NXTINFO to retrieve this same 2911 information via ancillary data. 2913 Note that the sctp_extrcvinfo structure is never used in any send 2914 call. 2916 7.1.23. Set or Get the Auto ASCONF Flag (SCTP_AUTO_ASCONF) 2918 This option will enable or disable the use of the automatic 2919 generation of ASCONF chunks to add and delete addresses to an 2920 existing association. Note that this option has two caveats namely: 2921 a) it only effects sockets that are bound to all addresses on the 2922 machine, and b) the system administrator may have an overriding 2923 control that turns the ASCONF feature off no matter what setting the 2924 socket option may have. 2926 7.1.24. Set or Get the Maximum Burst (SCTP_MAX_BURST) 2928 This option will allow a user to change the maximum burst of packets 2929 that can be emitted by this association. Note that the default value 2930 is 4, and some implementations may restrict this setting so that it 2931 can only be lowered. 2933 To set or get this option the user fills in the following structure: 2935 struct sctp_assoc_value { 2936 sctp_assoc_t assoc_id; 2937 uint32_t assoc_value; 2938 }; 2939 assoc_id: This parameter is ignored for one-to-one style sockets. 2940 For one-to-many style sockets this parameter indicates which 2941 association the user is performing an action upon. Note that any 2942 of the predefined constants may be used for one-to-many style 2943 sockets. 2944 assoc_value: This parameter contains the maximum burst. 2946 7.1.25. Set or Get the Default Context (SCTP_CONTEXT) 2948 The context field in the sctp_sndrcvinfo structure is normally only 2949 used when a failed message is retrieved holding the value that was 2950 sent down on the actual send call. This option allows the setting of 2951 a default context on an association basis that will be received on 2952 reading messages from the peer. This is especially helpful in the 2953 one-to-many model for an application to keep some reference to an 2954 internal state machine that is processing messages on the 2955 association. Note that the setting of this value only effects 2956 received messages from the peer and does not effect the value that is 2957 saved with outbound messages. 2959 To set or get this option the user fills in the following structure: 2961 struct sctp_assoc_value { 2962 sctp_assoc_t assoc_id; 2963 uint32_t assoc_value; 2964 }; 2966 assoc_id: This parameter is ignored for one-to-one style sockets. 2967 For one-to-many style sockets this parameter indicates which 2968 association the user is performing an action upon. Note that any 2969 of the predefined constants may be used for one-to-many style 2970 sockets. 2971 assoc_value: This parameter contains the context. 2973 7.1.26. Enable or Disable Explicit EOR Marking (SCTP_EXPLICIT_EOR) 2975 This boolean flag is used to enable or disable explicit end of record 2976 (EOR) marking. When this option is enabled, a user may make multiple 2977 send system calls to send a record and must indicate that they are 2978 finished sending a particular record by including the SCTP_EOR flag. 2979 If this boolean flag is disabled then each individual send system 2980 call is considered to have an SCTP_EOR indicator set on it implicitly 2981 without the user having to explicitly add this flag. 2983 7.1.27. Enable SCTP Port Reusage (SCTP_REUSE_PORT) 2985 This option only supports one-to-one style SCTP sockets. If used on 2986 a one-to-many style SCTP socket an error is indicated. 2988 This setsockopt() call must not be used after calling bind() or 2989 sctp_bindx() for a one-to-one style SCTP socket. If using bind() or 2990 sctp_bindx() on a socket with the SCTP_REUSE_PORT option, all other 2991 SCTP sockets bound to the same port must have set the 2992 SCTP_REUSE_PORT. Calling bind() or sctp_bindx() for a socket without 2993 having set the SCTP_REUSE_PORT option will fail if there are other 2994 sockets bound to the same port. At most one socket being bound to 2995 the same port may be listening. 2997 It should be noted that the behavior of the socket level socket 2998 option to reuse ports and/or addresses for SCTP sockets is 2999 unspecified. 3001 7.1.28. Set Notification Event (SCTP_EVENT) 3003 This socket option is used to set a specific notification or 3004 ancillary data option. Please see Section 7.4 for a full description 3005 of this option and its usage. 3007 7.2. Read-Only Options 3009 The options defined in this subsection are read-only. Using this 3010 option in a setsockopt() call will result in an error indicating 3011 EOPNOTSUPP. 3013 7.2.1. Association Status (SCTP_STATUS) 3015 Applications can retrieve current status information about an 3016 association, including association state, peer receiver window size, 3017 number of unacked data chunks, and number of data chunks pending 3018 receipt. This information is read-only. 3020 The following structure is used to access this information: 3022 struct sctp_status { 3023 sctp_assoc_t sstat_assoc_id; 3024 int32_t sstat_state; 3025 uint32_t sstat_rwnd; 3026 uint16_t sstat_unackdata; 3027 uint16_t sstat_penddata; 3028 uint16_t sstat_instrms; 3029 uint16_t sstat_outstrms; 3030 uint32_t sstat_fragmentation_point; 3031 struct sctp_paddrinfo sstat_primary; 3032 }; 3033 sstat_assoc_id: This parameter is ignored for one-to-one style 3034 sockets. For one-to-many style sockets it holds the identifier 3035 for the association. All notifications for a given association 3036 have the same association identifier. Note that the one-to-many 3037 predefined constants may not be used with this option. 3038 sstat_state: This contains the association's current state one of 3039 the following values: 3040 * SCTP_CLOSED 3041 * SCTP_BOUND 3042 * SCTP_LISTEN 3043 * SCTP_COOKIE_WAIT 3044 * SCTP_COOKIE_ECHOED 3045 * SCTP_ESTABLISHED 3046 * SCTP_SHUTDOWN_PENDING 3047 * SCTP_SHUTDOWN_SENT 3048 * SCTP_SHUTDOWN_RECEIVED 3049 * SCTP_SHUTDOWN_ACK_SENT 3050 sstat_rwnd: This contains the association peer's current receiver 3051 window size. 3052 sstat_unackdata: This is the number of unacked data chunks. 3053 sstat_penddata: This is the number of data chunks pending receipt. 3054 sstat_primary: This is information on the current primary peer 3055 address. 3056 sstat_instrms: The number of streams that the peer will be using 3057 inbound. 3058 sstat_outstrms: The number of streams that the endpoint is allowed 3059 to use outbound. 3060 sstat_fragmentation_point: The size at which SCTP fragmentation will 3061 occur. 3063 To access these status values, the application calls getsockopt() 3064 with the option name SCTP_STATUS. 3066 7.2.2. Peer Address Information (SCTP_GET_PEER_ADDR_INFO) 3068 Applications can retrieve information about a specific peer address 3069 of an association, including its reachability state, congestion 3070 window, and retransmission timer values. This information is read- 3071 only. 3073 The following structure is used to access this information: 3075 struct sctp_paddrinfo { 3076 sctp_assoc_t spinfo_assoc_id; 3077 struct sockaddr_storage spinfo_address; 3078 int32_t spinfo_state; 3079 uint32_t spinfo_cwnd; 3080 uint32_t spinfo_srtt; 3081 uint32_t spinfo_rto; 3082 uint32_t spinfo_mtu; 3083 }; 3085 spinfo_assoc_id: This parameter is ignored for one-to-one style 3086 sockets. For one-to-many style sockets the following applies: 3087 This field may be filled by the application, if so, this field 3088 will have priority in looking up the association using the address 3089 specified in spinfo_address. Note that if the address does not 3090 belong to the association specified then this call will fail. If 3091 the application does NOT fill in the spinfo_assoc_id, then the 3092 address will be used to lookup the association and on return this 3093 field will have the valid association id. In other words, this 3094 call can be used to translate an address into an association id. 3095 Note that the predefined constants are not allowed on this option. 3096 spinfo_address: This is filled by the application, and contains the 3097 peer address of interest. 3098 spinfo_state: This contains the peer address' state (either 3099 SCTP_ACTIVE or SCTP_INACTIVE and possibly the modifier 3100 SCTP_UNCONFIRMED). 3101 spinfo_cwnd: This contains the peer address' current congestion 3102 window. 3103 spinfo_srtt: This contains the peer address' current smoothed round- 3104 trip time calculation in milliseconds. 3105 spinfo_rto: This contains the peer address' current retransmission 3106 timeout value in milliseconds. 3107 spinfo_mtu: The current P-MTU of this address. 3109 7.2.3. Get the List of Chunks the Peer Requires to be Authenticated 3110 (SCTP_PEER_AUTH_CHUNKS) 3112 This option gets a list of chunks for a specified association that 3113 the peer requires to be received authenticated only. 3115 The following structure is used to access these parameters: 3117 struct sctp_authchunks { 3118 sctp_assoc_t gauth_assoc_id; 3119 guint32_t gauth_number_of_chunks 3120 uint8_t gauth_chunks[]; 3121 }; 3122 gauth_assoc_id: This parameter indicates for which association the 3123 user is requesting the list of peer authenticated chunks. For 3124 one-to-one sockets, this parameter is ignored. Note that the 3125 predefined constants are not allowed with this option. 3126 gauth_number_of_chunks: This parameter gives the number of elements 3127 in the array gauth_chunks. 3128 gauth_chunks: This parameter contains an array of chunks that the 3129 peer is requesting to be authenticated. 3131 7.2.4. Get the List of Chunks the Local Endpoint Requires to be 3132 Authenticated (SCTP_LOCAL_AUTH_CHUNKS) 3134 This option gets a list of chunks for a specified association that 3135 the local endpoint requires to be received authenticated only. 3137 The following structure is used to access these parameters: 3139 struct sctp_authchunks { 3140 sctp_assoc_t gauth_assoc_id; 3141 uint32_t gauth_number_of_chunks; 3142 uint8_t gauth_chunks[]; 3143 }; 3145 gauth_assoc_id: This parameter indicates for which association the 3146 user is requesting the list of local authenticated chunks. For 3147 one-to-one sockets, this parameter is ignored. 3148 gauth_number_of_chunks: This parameter gives the number of elements 3149 in the array gauth_chunks. 3150 gauth_chunks: This parameter contains an array of chunks that the 3151 local endpoint is requesting to be authenticated. 3153 7.2.5. Get the Current Number of Associations (SCTP_GET_ASSOC_NUMBER) 3155 This option gets the current number of associations that are attached 3156 to a one-to-many style socket. The option value is an uint32_t. 3158 7.2.6. Get the Current Identifiers of Associations 3159 (SCTP_GET_ASSOC_ID_LIST) 3161 This option gets the current list of SCTP association identifiers of 3162 the SCTP associations handled by a one-to-many style socket. 3164 The option value has the structure 3166 struct sctp_assoc_ids { 3167 uint32_t gaids_number_of_ids; 3168 sctp_assoc_t gaids_assoc_id[]; 3169 }; 3170 The caller must provide a large enough buffer to hold all association 3171 identifiers. If the buffer is too small, an error must be returned. 3172 The user can use the SCTP_GET_ASSOC_NUMBER socket option to get an 3173 idea how large the buffer has to be. gaids_number_of_ids gives the 3174 number of elements in the array gaids_assoc_id. 3176 7.3. Write-Only Options 3178 The options defined in this subsection are write-only. Using this 3179 option in a getsockopt() or sctp_opt_info() call will result in an 3180 error indicating EOPNOTSUPP. 3182 7.3.1. Set Peer Primary Address (SCTP_SET_PEER_PRIMARY_ADDR) 3184 Requests that the peer marks the enclosed address as the association 3185 primary. The enclosed address must be one of the association's 3186 locally bound addresses. 3188 The following structure is used to make a set peer primary request: 3190 struct sctp_setpeerprim { 3191 sctp_assoc_t sspp_assoc_id; 3192 struct sockaddr_storage sspp_addr; 3193 }; 3195 sspp_addr: The address to set as primary. 3196 sspp_assoc_id: This parameter is ignored for one-to-one style 3197 sockets. For one-to-many style sockets it identifies the 3198 association for this request. Note that the predefined constants 3199 are not allowed on this option. 3201 7.3.2. Add a Chunk That Must Be Authenticated (SCTP_AUTH_CHUNK) 3203 This set option adds a chunk type that the user is requesting to be 3204 received only in an authenticated way. Changes to the list of chunks 3205 will only effect future associations on the socket. 3207 The following structure is used to add a chunk: 3209 struct sctp_authchunk { 3210 uint8_t sauth_chunk; 3211 }; 3213 sauth_chunk: This parameter contains a chunk type that the user is 3214 requesting to be authenticated. 3216 The chunk types for INIT, INIT-ACK, SHUTDOWN-COMPLETE, and AUTH 3217 chunks must not be used. If they are used, an error must be 3218 returned. The usage of this option enables SCTP-AUTH in cases where 3219 it is not required by other means (for example the use of dynamic 3220 address reconfiguration). 3222 7.3.3. Set a Shared Key (SCTP_AUTH_KEY) 3224 This option will set a shared secret key which is used to build an 3225 association shared key. 3227 The following structure is used to access and modify these 3228 parameters: 3230 struct sctp_authkey { 3231 sctp_assoc_t sca_assoc_id; 3232 uint16_t sca_keynumber; 3233 uint16_t sca_keylength; 3234 uint8_t sca_key[]; 3235 }; 3237 sca_assoc_id: This parameter, if non-zero, indicates what 3238 association the shared key is being set upon. Note that any of 3239 the predefined constants can be used. For one-to-one sockets, 3240 this parameter is ignored. Note, however, that this option will 3241 set a key on the association if the socket is connected, otherwise 3242 this will set a key on the endpoint. 3243 sca_keynumber: This parameter is the shared key identifier by which 3244 the application will refer to this shared key. If a key of the 3245 specified index already exists, then this new key will replace the 3246 old existing key. Note that shared key identifier '0' defaults to 3247 a null key. 3248 sca_keylength: This parameter is the length of the array sca_key. 3249 sca_key: This parameter contains an array of bytes that is to be 3250 used by the endpoint (or association) as the shared secret key. 3251 Note, if the length of this field is zero, a null key is set. 3253 7.3.4. Deactivate a Shared Key (SCTP_AUTH_DEACTIVATE_KEY) 3255 This set option indicates that the application will not send user 3256 messages anymore requiring the usage of the indicated key identifier. 3258 struct sctp_authkeyid { 3259 sctp_assoc_t scact_assoc_id; 3260 uint16_t scact_keynumber; 3261 }; 3262 scact_assoc_id: This parameter, if non-zero, indicates what 3263 association the shared key identifier is being deactivated for. 3264 Note that the predefined constants may be used with this option. 3265 For one-to-one sockets, this parameter is ignored. Note, however, 3266 that this option will deactivate the key from the association if 3267 the socket is connected, otherwise this will deactivate the key 3268 from the endpoint. 3269 scact_keynumber: This parameter is the shared key identifier which 3270 the application is requesting to be deactivated. The key 3271 identifier must correspond to an existing shared key. Note if 3272 this parameter is zero, use of the null key identifier '0' is 3273 deactivated on the endpoint and/or association. 3275 The currently active key cannot be deactivated. 3277 7.3.5. Delete a Shared Key (SCTP_AUTH_DELETE_KEY) 3279 This set option will delete a shared secret key in the SCTP 3280 implementation. 3282 struct sctp_authkeyid { 3283 sctp_assoc_t scact_assoc_id; 3284 uint16_t scact_keynumber; 3285 }; 3287 scact_assoc_id: This parameter, if non-zero, indicates which 3288 association the shared key identifier is being deleted from. Note 3289 that if this element contains zero, then the shared key is deleted 3290 from the endpoint and all associations will no longer use the 3291 specified shared key identifier (unless otherwise set on the 3292 association using SCTP_AUTH_KEY). For one-to-one sockets, this 3293 parameter is ignored. Note, however, that this option will delete 3294 the key from the association if the socket is connected, otherwise 3295 this will delete the key from the endpoint. 3296 scact_keynumber: This parameter is the shared key identifier which 3297 the application is requesting to be deleted. The key identifier 3298 must correspond to an existing shared key and must not be in use 3299 for any packet being sent by the SCTP implementation. This means 3300 in particular, that it must be deactivated first. Note if this 3301 parameter is zero, use of the null key identifier '0' is deleted 3302 from the endpoint and/or association. 3304 Only deactivated keys which are no longer used by the kernel can be 3305 deleted. 3307 7.4. Ancillary Data and Notification Interest Options 3309 Applications can receive per-message ancillary information and 3310 notifications of certain SCTP events with recvmsg(). 3312 The following optional information is available to the application: 3313 SCTP_SNDRCV (sctp_data_io_event): Per-message information (i.e. 3314 stream number, TSN, SSN, etc. described in Section 5.2.2) 3315 SCTP_ASSOC_CHANGE (sctp_association_event): described in 3316 Section 5.3.2 3317 SCTP_PEER_ADDR_CHANGE (sctp_address_event): described in 3318 Section 5.3.3 3319 SCTP_SEND_FAILED (sctp_send_failure_event): described in 3320 Section 5.3.5 3321 SCTP_REMOTE_ERROR (sctp_peer_error_event): described in 3322 Section 5.3.4 3323 SCTP_SHUTDOWN_EVENT (sctp_shutdown_event): described in 3324 Section 5.3.6 3325 SCTP_PARTIAL_DELIVERY_EVENT (sctp_partial_delivery_event): described 3326 in Section 5.3.8 3327 SCTP_ADAPTATION_INDICATION (sctp_adaptation_layer_event): described 3328 in Section 5.3.7 3329 SCTP_AUTHENTICATION_EVENT (sctp_authentication_event): described in 3330 Section 5.3.9) 3331 SCTP_SENDER_DRY_EVENT (sctp_sender_dry_event): described in 3332 Section 5.3.10 3333 SCTP_NOTIFICATIONS_STOPPED_EVENT (sctp_tlv): described in 3334 Section 5.3.11 3336 To receive any ancillary data or notifications, first the application 3337 registers its interest by calling the SCTP_EVENTS setsockopt() with 3338 the following structure: 3340 struct sctp_event_subscribe{ 3341 uint8_t sctp_data_io_event; 3342 uint8_t sctp_association_event; 3343 uint8_t sctp_address_event; 3344 uint8_t sctp_send_failure_event; 3345 uint8_t sctp_peer_error_event; 3346 uint8_t sctp_shutdown_event; 3347 uint8_t sctp_partial_delivery_event; 3348 uint8_t sctp_adaptation_layer_event; 3349 uint8_t sctp_authentication_event; 3350 uint8_t sctp_sender_dry_event; 3351 }; 3352 sctp_data_io_event: Setting this flag to 1 will cause the reception 3353 of SCTP_SNDRCV information on a per message basis. The 3354 application will need to use the recvmsg() interface so that it 3355 can receive the event information contained in the msg_control 3356 field. Setting the flag to 0 will disable the reception of the 3357 message control information. 3358 sctp_association_event: Setting this flag to 1 will enable the 3359 reception of association event notifications. Setting the flag to 3360 0 will disable association event notifications. 3361 sctp_address_event: Setting this flag to 1 will enable the reception 3362 of address event notifications. Setting the flag to 0 will 3363 disable address event notifications. 3364 sctp_send_failure_event: Setting this flag to 1 will enable the 3365 reception of send failure event notifications. Setting the flag 3366 to 0 will disable send failure event notifications. 3367 sctp_peer_error_event: Setting this flag to 1 will enable the 3368 reception of peer error event notifications. Setting the flag to 3369 0 will disable peer error event notifications. 3370 sctp_shutdown_event: Setting this flag to 1 will enable the 3371 reception of shutdown event notifications. Setting the flag to 0 3372 will disable shutdown event notifications. 3373 sctp_partial_delivery_event: Setting this flag to 1 will enable the 3374 reception of partial delivery notifications. Setting the flag to 3375 0 will disable partial delivery event notifications. 3376 sctp_adaptation_layer_event: Setting this flag to 1 will enable the 3377 reception of adaptation layer notifications. Setting the flag to 3378 0 will disable adaptation layer event notifications. 3379 sctp_authentication_event: Setting this flag to 1 will enable the 3380 reception of authentication layer notifications. Setting the flag 3381 to 0 will disable authentication layer event notifications. 3382 sctp_sender_dry_event: Setting this flag to 1 will enable the 3383 reception of sender dry notifications. Setting the flag to 0 will 3384 disable sender dry event notifications. 3386 An example where an application would like to receive data io events 3387 and association events but no others would be as follows: 3389 { 3390 struct sctp_event_subscribe events; 3392 memset(&events,0,sizeof(events)); 3394 events.sctp_data_io_event = 1; 3395 events.sctp_association_event = 1; 3397 setsockopt(fd, IPPROTO_SCTP, SCTP_EVENTS, &events, sizeof(events)); 3398 } 3399 Note that for one-to-many style SCTP sockets, the caller of recvmsg() 3400 receives ancillary data and notifications for ALL associations bound 3401 to the file descriptor. For one-to-one style SCTP sockets, the 3402 caller receives ancillary data and notifications only for the single 3403 association bound to the file descriptor. 3405 The SCTP_EVENTS socket option has one issue for future compatibility. 3406 As new features are added the structure (sctp_event_subscribe) must 3407 be expanded. This can cause an ABI issue unless an implementation 3408 has added padding at the end of the structure. To avoid this 3409 problem, SCTP_EVENTS has been deprecated and a new option SCTP_EVENT 3410 socket option has taken its place. The option is used with the 3411 following structure: 3413 struct sctp_event { 3414 sctp_assoc_t se_assoc_id; 3415 uint16_t se_type; 3416 uint8_t se_on; 3417 }; 3419 se_assoc_id: The se_assoc_id field is ignored for one-to-one style 3420 sockets. For one-to-many style sockets any this field can be a 3421 particular association id or one of the defined constants. 3422 se_type: The se_type field can be filled with any value that would 3423 show up in the respective sn_type field (in the sctp_tlv structure 3424 of the notification). In addition SCTP_SNDRCV_EVENT, 3425 SCTP_RCV_EVENT, and SCTP_NXT_EVENT can be used. 3426 se_on: The se_on field is set to 1 to turn on an event and set to 0 3427 to turn off an event. 3429 To use this option the user fills in this structure and then calls 3430 the setsockopt to turn on or off an individual event. The following 3431 is an example use of this option: 3433 { 3434 struct sctp_event event; 3436 memset(&event, 0, sizeof(event)); 3438 event.se_assoc_id = SCTP_FUTURE_ASSOC; 3439 event.se_type = SCTP_SENDER_DRY_EVENT; 3440 event.se_on = 1; 3441 setsockopt(fd, IPPROTO_SCTP, SCTP_EVENT, &event, sizeof(event)); 3442 } 3444 By default both the one-to-one style and the one-to-many style socket 3445 has all options off. 3447 8. New Functions 3449 Depending on the system, the following interface can be implemented 3450 as a system call or library function. 3452 8.1. sctp_bindx() 3454 This function allows the user to bind a specific subset of addresses 3455 or, if the SCTP extension described in [RFC5061] is supported, add or 3456 delete specific addresses. 3458 The function prototype is 3460 int sctp_bindx(int sd, 3461 struct sockaddr *addrs, 3462 int addrcnt, 3463 int flags); 3465 If sd is an IPv4 socket, the addresses passed must be IPv4 addresses. 3466 If the sd is an IPv6 socket, the addresses passed can either be IPv4 3467 or IPv6 addresses. 3469 A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see 3470 Section 3.1.2 for this usage. 3472 addrs is a pointer to an array of one or more socket addresses. Each 3473 address is contained in its appropriate structure. For an IPv6 3474 socket, an array of sockaddr_in6 would be returned. For a IPv4 3475 socket, an array of sockaddr_in would be returned. The caller 3476 specifies the number of addresses in the array with addrcnt. Note 3477 that the wildcard addresses cannot be used in combination with non 3478 wildcard addresses on a socket with this function, doing so will 3479 result in an error. 3481 On success, sctp_bindx() returns 0. On failure, sctp_bindx() returns 3482 -1 and sets errno to the appropriate error code. 3484 For SCTP, the port given in each socket address must be the same, or 3485 sctp_bindx() will fail, setting errno to EINVAL. 3487 The flags parameter is formed from the bitwise OR of zero or more of 3488 the following currently defined flags: 3489 o SCTP_BINDX_ADD_ADDR 3490 o SCTP_BINDX_REM_ADDR 3491 SCTP_BINDX_ADD_ADDR directs SCTP to add the given addresses to the 3492 association, and SCTP_BINDX_REM_ADDR directs SCTP to remove the given 3493 addresses from the association. The two flags are mutually 3494 exclusive; if both are given, sctp_bindx() will fail with EINVAL. A 3495 caller may not remove all addresses from an association; sctp_bindx() 3496 will reject such an attempt with EINVAL. 3498 An application can use sctp_bindx(SCTP_BINDX_ADD_ADDR) to associate 3499 additional addresses with an endpoint after calling bind(). Or use 3500 sctp_bindx(SCTP_BINDX_REM_ADDR) to remove some addresses a listening 3501 socket is associated with, so that no new association accepted will 3502 be associated with those addresses. If the endpoint supports dynamic 3503 address reconfiguration an SCTP_BINDX_REM_ADDR or SCTP_BINDX_ADD_ADDR 3504 may cause an endpoint to send the appropriate message to the peer to 3505 change the peer's address lists. 3507 Adding and removing addresses from a connected association is an 3508 optional functionality. Implementations that do not support this 3509 functionality should return EOPNOTSUPP. 3511 sctp_bindx() can be called on an already bound socket or on an 3512 unbound socket. If the socket is unbound and the first port number 3513 in the addrs is zero, the kernel will choose a port number. All port 3514 numbers after the first one being 0 must also be zero. If the first 3515 port number is not zero, the following port numbers must be zero or 3516 have the same value as the first one. For an already bound socket, 3517 all port numbers provided must be the bound one or 0. 3519 sctp_bindx() is an atomic operation. Therefore, the binding will be 3520 either successful on all addresses or fail on all addresses. If 3521 multiple addresses are provided and the sctp_bindx() call fails there 3522 is no indication which address is responsible for the failure. The 3523 only way to get a specific error indication is to call sctp_bindx() 3524 with only one address sequentially. 3526 8.2. sctp_peeloff() 3528 After an association is established on a one-to-many style socket, 3529 the application may wish to branch off the association into a 3530 separate socket/file descriptor. 3532 This is particularly desirable when, for instance, the application 3533 wishes to have a number of sporadic message senders/receivers remain 3534 under the original one-to-many style socket but branch off those 3535 associations carrying high volume data traffic into their own 3536 separate socket descriptors. 3538 The application uses the sctp_peeloff() call to branch off an 3539 association into a separate socket (Note the semantics are somewhat 3540 changed from the traditional one-to-one style accept() call). Note 3541 that the new socket is a one-to-one style socket. Thus it will be 3542 confined to operations allowed for a one-to-one style socket. 3544 The function prototype is 3546 int sctp_peeloff(int sd, 3547 sctp_assoc_t assoc_id); 3549 and the arguments are 3550 sd: The original one-to-many style socket descriptor returned from 3551 the socket() system call (see Section 3.1.1). 3552 assoc_id: the specified identifier of the association that is to be 3553 branched off to a separate file descriptor (Note, in a traditional 3554 one-to-one style accept() call, this would be an out parameter, 3555 but for the one-to-many style call, this is an in parameter). 3556 The function returns a non-negative file descriptor representing the 3557 branched-off association, or -1 if an error occurred. The variable 3558 errno is then set appropriately. 3560 8.3. sctp_getpaddrs() 3562 sctp_getpaddrs() returns all peer addresses in an association. 3564 The function protoype is: 3566 int sctp_getpaddrs(int sd, 3567 sctp_assoc_t id, 3568 struct sockaddr **addrs); 3570 On return, addrs will point to an array dynamically allocated 3571 sockaddr structures of the appropriate type for the socket type. The 3572 caller should use sctp_freepaddrs() to free the memory. Note that 3573 the in/out parameter addrs must not be NULL. 3575 If sd is an IPv4 socket, the addresses returned will be all IPv4 3576 addresses. If sd is an IPv6 socket, the addresses returned can be a 3577 mix of IPv4 or IPv6 addresses. 3579 For one-to-many style sockets, id specifies the association to query. 3580 For one-to-one style sockets, id is ignored. 3582 On success, sctp_getpaddrs() returns the number of peer addresses in 3583 the association. If there is no association on this socket, 3584 sctp_getpaddrs() returns 0, and the value of *addrs is undefined. If 3585 an error occurs, sctp_getpaddrs() returns -1, and the value of *addrs 3586 is undefined. 3588 8.4. sctp_freepaddrs() 3590 sctp_freepaddrs() frees all resources allocated by sctp_getpaddrs(). 3592 The function prototype is 3594 void sctp_freepaddrs(struct sockaddr *addrs); 3596 and addrs is the array of peer addresses returned by 3597 sctp_getpaddrs(). 3599 8.5. sctp_getladdrs() 3601 sctp_getladdrs() returns all locally bound address(es) on a socket. 3603 The function prototype is 3605 int sctp_getladdrs(int sd, 3606 sctp_assoc_t id, 3607 struct sockaddr **ss); 3609 On return, addrs will point to a dynamically allocated array of 3610 sockaddr structures of the appropriate type for the socket type. The 3611 caller should use sctp_freeladdrs() to free the memory. Note that 3612 the in/out parameter addrs must not be NULL. 3614 If sd is an IPv4 socket, the addresses returned will be all IPv4 3615 addresses. If sd is an IPv6 socket, the addresses returned can be a 3616 mix of IPv4 or IPv6 addresses. 3618 For one-to-many style sockets, id specifies the association to query. 3619 For one-to-one style sockets, id is ignored. 3621 If the id field is set to the value '0' then the locally bound 3622 addresses are returned without regard to any particular association. 3624 On success, sctp_getladdrs() returns the number of local addresses 3625 bound to the socket. If the socket is unbound, sctp_getladdrs() 3626 returns 0, and the value of *addrs is undefined. If an error occurs, 3627 sctp_getladdrs() returns -1, and the value of *addrs is undefined. 3629 8.6. sctp_freeladdrs() 3631 sctp_freeladdrs() frees all resources allocated by sctp_getladdrs(). 3633 The function prototype is 3635 void sctp_freeladdrs(struct sockaddr *addrs); 3637 and addrs is the array of peer addresses returned by 3638 sctp_getladdrs(). 3640 8.7. sctp_sendmsg() 3642 An implementation may provide a library function (or possibly system 3643 call) to assist the user with the advanced features of SCTP. 3645 The function prototype is 3647 ssize_t sctp_sendmsg(int sd, 3648 const void *msg, 3649 size_t len, 3650 const struct sockaddr *to, 3651 socklen_t tolen, 3652 uint32_t ppid, 3653 uint32_t flags, 3654 uint16_t stream_no, 3655 uint32_t pr_value, 3656 uint32_t context); 3658 and the arguments are: 3659 sd: The socket descriptor 3660 msg: The message to be sent. 3661 len: The length of the message. 3662 to: The destination address of the message. 3663 tolen: The length of the destination address. 3664 ppid: The same as sinfo_ppid (see Section 5.2.2) 3665 flags: The same as sinfo_flags (see Section 5.2.2) 3666 stream_no: The same as sinfo_stream (see Section 5.2.2) 3667 pr_value: The same as sinfo_pr_value (see Section 5.2.2). 3668 context: The same as sinfo_context (see Section 5.2.2) 3669 The call returns the number of characters sent, or -1 if an error 3670 occurred. The variable errno is then set appropriately. 3672 Sending a message using sctp_sendmsg() is atomic (unless explicit EOR 3673 marking is enabled on the socket specified by sd). 3675 Using sctp_sendmsg() on a non-connected one-to-one style socket for 3676 implicit connection setup may or may not work depending on the SCTP 3677 implementation. 3679 8.8. sctp_recvmsg() 3681 An implementation may provide a library function (or possibly system 3682 call) to assist the user with the advanced features of SCTP. Note 3683 that in order for the sctp_sndrcvinfo structure to be filled in by 3684 sctp_recvmsg() the caller must enable the sctp_data_io_events with 3685 the SCTP_EVENTS option. Note that the setting of the 3686 SCTP_USE_EXT_RCVINFO will effect this function as well, causing the 3687 sctp_sndrcvinfo information to be extended. 3689 The function prototype is 3691 ssize_t sctp_recvmsg(int sd, 3692 void *msg, 3693 size_t len, 3694 struct sockaddr *from, 3695 socklen_t *fromlen 3696 struct sctp_sndrcvinfo *sinfo 3697 int *msg_flags); 3699 and the arguments are 3700 sd: The socket descriptor. 3701 msg: The message buffer to be filled. 3702 len: The length of the message buffer. 3703 from: A pointer to an address to be filled with the sender of this 3704 messages address. 3705 fromlen: An in/out parameter describing the from length. 3706 sinfo: A pointer to an sctp_sndrcvinfo structure to be filled upon 3707 receipt of the message. 3708 msg_flags: A pointer to an integer to be filled with any message 3709 flags (e.g. MSG_NOTIFICATION). Note that this field is an in-out 3710 field. Options for the receive may also be passed into the value 3711 (e.g. MSG_PEEK). On return from the call, the msg_flags value 3712 will be different than what was sent in to the call. If 3713 implemented via a recvmsg() call, the msg_flags should only 3714 contain the value of the flags from the recvmsg() call. 3715 The call returns the number of bytes received, or -1 if an error 3716 occurred. The variable errno is then set appropriately. 3718 8.9. sctp_connectx() 3720 An implementation may provide a library function (or possibly system 3721 call) to assist the user with associating to an endpoint that is 3722 multi-homed. Much like sctp_bindx() this call allows a caller to 3723 specify multiple addresses at which a peer can be reached. The way 3724 the SCTP stack uses the list of addresses to set up the association 3725 is implementation dependent. This function only specifies that the 3726 stack will try to make use of all the addresses in the list when 3727 needed. 3729 Note that the list of addresses passed in is only used for setting up 3730 the association. It does not necessarily equal the set of addresses 3731 the peer uses for the resulting association. If the caller wants to 3732 find out the set of peer addresses, it must use sctp_getpaddrs() to 3733 retrieve them after the association has been set up. 3735 The function prototype is 3736 int sctp_connectx(int sd, 3737 struct sockaddr *addrs, 3738 int addrcnt, 3739 sctp_assoc_t *id); 3741 and the arguments are: 3742 sd: The socket descriptor. 3743 addrs: An (packed) array of addresses. 3744 addrcnt: The number of addresses in the array. 3745 id: An output parameter that if passed in as a non-NULL will return 3746 the association identification for the newly created association 3747 (if successful). 3749 The call returns 0 on success or -1 if an error occurred. The 3750 variable errno is then set appropriately. 3752 8.10. sctp_send() 3754 An implementation may provide another alternative function or system 3755 call to assist an application with the sending of data without the 3756 use of the CMSG header structures. 3758 The function prototype is 3760 ssize_t sctp_send(int sd, 3761 const void *msg, 3762 size_t len, 3763 const struct sctp_sndrcvinfo *sinfo, 3764 int flags); 3766 and the arguments are 3767 sd: The socket descriptor. 3768 msg: The message to be sent. 3769 len: The length of the message. 3770 sinfo: A pointer to an sctp_sndrcvinfo structure used as described 3771 in Section 5.2.2 for a sendmsg call. 3772 flags: The same flags as used by the sendmsg call flags (e.g. 3773 MSG_DONTROUTE). 3774 The call returns the number of bytes sent, or -1 if an error 3775 occurred. The variable errno is then set appropriately. 3777 This function call may also be used to terminate an association using 3778 an association identification by setting the sinfo.sinfo_flags to 3779 SCTP_EOF and the sinfo.sinfo_assoc_id to the association that needs 3780 to be terminated. In such a case the len of the message would be 3781 zero. 3783 Using sctp_send() on a non-connected one-to-one style socket for 3784 implicit connection setup may or may not work depending on the SCTP 3785 implementation. 3787 Sending a message using sctp_send() is atomic unless explicit EOR 3788 marking is enabled on the socket specified by sd. 3790 8.11. sctp_sendx() 3792 An implementation may provide another alternative function or system 3793 call to assist an application with the sending of data without the 3794 use of the CMSG header structures that also gives a list of 3795 addresses. The list of addresses is provided for implicit 3796 association setup. In such a case the list of addresses serves the 3797 same purpose as the addresses given in sctp_connectx() (see 3798 Section 8.9). 3800 The function prototype is 3802 ssize_t sctp_sendx(int sd, 3803 const void *msg, 3804 size_t len, 3805 struct sockaddr *addrs, 3806 int addrcnt, 3807 struct sctp_sndrcvinfo *sinfo, 3808 int flags); 3810 and the arguments are: 3811 sd: The socket descriptor. 3812 msg: The message to be sent. 3813 len: The length of the message. 3814 addrs: is an array of addresses. 3815 addrcnt: The number of addresses in the array. 3816 sinfo: A pointer to a sctp_sndrcvinfo structure used as described in 3817 Section 5.2.2 for a sendmsg call. 3818 flags: The same flags as used by the sendmsg call flags (e.g. 3819 MSG_DONTROUTE). 3820 The call returns the number of bytes sent, or -1 if an error 3821 occurred. The variable errno is then set appropriately. 3823 Note that on return from this call the sinfo structure will have 3824 changed in that the sinfo_assoc_id will be filled in with the new 3825 association id. 3827 This function call may also be used to terminate an association using 3828 an association identification by setting the sinfo.sinfo_flags to 3829 SCTP_EOF and the sinfo.sinfo_assoc_id to the association that needs 3830 to be terminated. In such a case the len of the message would be 3831 zero. 3833 Sending a message using sctp_send() is atomic unless explicit EOR 3834 marking is enabled on the socket specified by sd. 3836 Using sctp_sendx() on a non-connected one-to-one style socket for 3837 implicit connection setup may or may not work depending on the SCTP 3838 implementation. 3840 8.12. sctp_getaddrlen() 3842 For application binary portability it is sometimes desirable to know 3843 what the kernel thinks is the length of a socket address family. 3845 The function prototype is: 3847 int sctp_getaddrlen(sa_family_t family); 3849 This function, when called with a valid family type returns the 3850 length that the operating system uses in the specified family's 3851 socket address structure. In case of an error, -1 is returned and 3852 the variable errno is then set appropriately. 3854 9. IANA Considerations 3856 This document requires no actions from IANA. 3858 10. Security Considerations 3860 Many TCP and UDP implementations reserve port numbers below 1024 for 3861 privileged users. If the target platform supports privileged users, 3862 the SCTP implementation should restrict the ability to call bind() or 3863 sctp_bindx() on these port numbers to privileged users. 3865 Similarly unprivileged users should not be able to set protocol 3866 parameters which could result in the congestion control algorithm 3867 being more aggressive than permitted on the public Internet. These 3868 parameters are: 3869 o struct sctp_rtoinfo 3871 If an unprivileged user inherits a one-to-many style socket with open 3872 associations on a privileged port, it may be permitted to accept new 3873 associations, but it should not be permitted to open new 3874 associations. This could be relevant for the r* family of protocols. 3876 Applications using the one-to-many style sockets and using the 3877 interleave level if 0 are subject to denial of service attacks as 3878 described in Section 7.1.20. 3880 11. Acknowledgments 3882 Special acknowledgment is given to Ken Fujita, Jonathan Woods, 3883 Qiaobing Xie, and La Monte Yarroll, who helped extensively in the 3884 early formation of this document. 3886 The authors also wish to thank Kavitha Baratakke, Mike Bartlett, Jon 3887 Berger, Mark Butler, Scott Kimble, Renee Revis, Andreas Fink, 3888 Jonathan Leighton, Irene Ruengeler, and many others on the TSVWG 3889 mailing list for contributing valuable comments. 3891 A special thanks to Phillip Conrad, for his suggested text, quick and 3892 constructive insights, and most of all his persistent fighting to 3893 keep the interface to SCTP usable for the application programmer. 3895 12. Normative References 3897 [RFC0793] Postel, J., "Transmission Control Protocol", STD 7, 3898 RFC 793, September 1981. 3900 [RFC0768] Postel, J., "User Datagram Protocol", STD 6, RFC 768, 3901 August 1980. 3903 [RFC1644] Braden, B., "T/TCP -- TCP Extensions for Transactions 3904 Functional Specification", RFC 1644, July 1994. 3906 [RFC3493] Gilligan, R., Thomson, S., Bound, J., McCann, J., and W. 3907 Stevens, "Basic Socket Interface Extensions for IPv6", 3908 RFC 3493, February 2003. 3910 [RFC3542] Stevens, W., Thomas, M., Nordmark, E., and T. Jinmei, 3911 "Advanced Sockets Application Program Interface (API) for 3912 IPv6", RFC 3542, May 2003. 3914 [RFC3758] Stewart, R., Ramalho, M., Xie, Q., Tuexen, M., and P. 3915 Conrad, "Stream Control Transmission Protocol (SCTP) 3916 Partial Reliability Extension", RFC 3758, May 2004. 3918 [RFC4895] Tuexen, M., Stewart, R., Lei, P., and E. Rescorla, 3919 "Authenticated Chunks for the Stream Control Transmission 3920 Protocol (SCTP)", RFC 4895, August 2007. 3922 [RFC4960] Stewart, R., "Stream Control Transmission Protocol", 3923 RFC 4960, September 2007. 3925 [RFC5061] Stewart, R., Xie, Q., Tuexen, M., Maruyama, S., and M. 3926 Kozuka, "Stream Control Transmission Protocol (SCTP) 3927 Dynamic Address Reconfiguration", RFC 5061, 3928 September 2007. 3930 Appendix A. One-to-One Style Code Example 3932 The following code is a simple implementation of an echo server over 3933 SCTP. The example shows how to use some features of one-to-one style 3934 IPv4 SCTP sockets, including: 3935 o Opening, binding, and listening for new associations on a socket 3936 o Enabling ancillary data 3937 o Enabling notifications 3938 o Using ancillary data with sendmsg() and recvmsg() 3939 o Using MSG_EOR to determine if an entire message has been read 3940 o Handling notifications 3942 #include 3943 #include 3944 #include 3945 #include 3946 #include 3947 #include 3948 #include 3949 #include 3950 #include 3952 #define BUFLEN 100 3954 static void 3955 handle_event(void *buf) 3956 { 3957 struct sctp_assoc_change *sac; 3958 struct sctp_send_failed *ssf; 3959 struct sctp_paddr_change *spc; 3960 struct sctp_remote_error *sre; 3961 union sctp_notification *snp; 3962 char addrbuf[INET6_ADDRSTRLEN]; 3963 const char *ap; 3964 struct sockaddr_in *sin; 3965 struct sockaddr_in6 *sin6; 3967 snp = buf; 3969 switch (snp->sn_header.sn_type) { 3970 case SCTP_ASSOC_CHANGE: 3971 sac = &snp->sn_assoc_change; 3972 printf("^^^ assoc_change: state=%hu, error=%hu, instr=%hu " 3973 "outstr=%hu\n", sac->sac_state, sac->sac_error, 3974 sac->sac_inbound_streams, sac->sac_outbound_streams); 3975 break; 3976 case SCTP_SEND_FAILED: 3977 ssf = &snp->sn_send_failed; 3978 printf("^^^ sendfailed: len=%hu err=%d\n", ssf->ssf_length, 3979 ssf->ssf_error); 3980 break; 3982 case SCTP_PEER_ADDR_CHANGE: 3983 spc = &snp->sn_paddr_change; 3984 if (spc->spc_aaddr.ss_family == AF_INET) { 3985 sin = (struct sockaddr_in *)&spc->spc_aaddr; 3986 ap = inet_ntop(AF_INET, &sin->sin_addr, 3987 addrbuf, INET6_ADDRSTRLEN); 3988 } else { 3989 sin6 = (struct sockaddr_in6 *)&spc->spc_aaddr; 3990 ap = inet_ntop(AF_INET6, &sin6->sin6_addr, 3991 addrbuf, INET6_ADDRSTRLEN); 3992 } 3993 printf("^^^ intf_change: %s state=%d, error=%d\n", ap, 3994 spc->spc_state, spc->spc_error); 3995 break; 3996 case SCTP_REMOTE_ERROR: 3997 sre = &snp->sn_remote_error; 3998 printf("^^^ remote_error: err=%hu len=%hu\n", 3999 ntohs(sre->sre_error), ntohs(sre->sre_length)); 4000 break; 4001 case SCTP_SHUTDOWN_EVENT: 4002 printf("^^^ shutdown event\n"); 4003 break; 4004 default: 4005 printf("unknown type: %hu\n", snp->sn_header.sn_type); 4006 break; 4007 }; 4008 } 4010 static void * 4011 mysctp_recvmsg(int fd, struct msghdr *msg, void *buf, size_t *buflen, 4012 ssize_t *nrp, size_t cmsglen) 4013 { 4014 ssize_t nr = 0, nnr = 0; 4015 struct iovec iov; 4017 *nrp = 0; 4018 iov.iov_base = buf; 4019 iov.iov_len = *buflen; 4020 msg->msg_iov = � 4021 msg->msg_iovlen = 1; 4022 for (;;) { 4023 #ifndef MSG_XPG4_2 4024 #define MSG_XPG4_2 0 4025 #endif 4026 msg->msg_flags = MSG_XPG4_2; 4027 msg->msg_controllen = cmsglen; 4029 nnr = recvmsg(fd, msg, 0); 4030 if (nnr <= 0) { 4031 /* EOF or error */ 4032 *nrp = nr; 4033 return (NULL); 4034 } 4035 nr += nnr; 4037 if ((msg->msg_flags & MSG_EOR) != 0) { 4038 *nrp = nr; 4039 return (buf); 4040 } 4042 /* Realloc the buffer? */ 4043 if (*buflen == (size_t)nr) { 4044 buf = realloc(buf, *buflen * 2); 4045 if (buf == 0) { 4046 fprintf(stderr, "out of memory\n"); 4047 exit(1); 4048 } 4049 *buflen *= 2; 4050 } 4051 /* Set the next read offset */ 4052 iov.iov_base = (char *)buf + nr; 4053 iov.iov_len = *buflen - nr; 4054 } 4055 } 4057 static void 4058 echo(int fd, int socketModeone_to_many) 4059 { 4060 ssize_t nr; 4061 struct sctp_sndrcvinfo *sri; 4062 struct msghdr msg; 4063 struct cmsghdr *cmsg; 4064 char cbuf[sizeof (*cmsg) + sizeof (*sri)]; 4065 char *buf; 4066 size_t buflen; 4067 struct iovec iov; 4068 size_t cmsglen = sizeof (*cmsg) + sizeof (*sri); 4069 /* Allocate the initial data buffer */ 4070 buflen = BUFLEN; 4071 if (!(buf = malloc(BUFLEN))) { 4072 fprintf(stderr, "out of memory\n"); 4073 exit(1); 4074 } 4076 /* Set up the msghdr structure for receiving */ 4077 memset(&msg, 0, sizeof (msg)); 4078 msg.msg_control = cbuf; 4079 msg.msg_controllen = cmsglen; 4080 msg.msg_flags = 0; 4081 cmsg = (struct cmsghdr *)cbuf; 4082 sri = (struct sctp_sndrcvinfo *)(cmsg + 1); 4084 /* Wait for something to echo */ 4085 while (buf = mysctp_recvmsg(fd, &msg, 4086 buf, &buflen, &nr, cmsglen)) { 4088 /* Intercept notifications here */ 4089 if (msg.msg_flags & MSG_NOTIFICATION) { 4090 handle_event(buf); 4091 continue; 4092 } 4094 iov.iov_base = buf; 4095 iov.iov_len = nr; 4096 msg.msg_iov = � 4097 msg.msg_iovlen = 1; 4099 printf("got %u bytes on stream %hu:\n", nr, 4100 sri->sinfo_stream); 4101 write(0, buf, nr); 4103 /* Echo it back */ 4104 msg.msg_flags = MSG_XPG4_2; 4105 if (sendmsg(fd, &msg, 0) < 0) { 4106 perror("sendmsg"); 4107 exit(1); 4108 } 4109 } 4111 if (nr < 0) { 4112 perror("recvmsg"); 4113 } 4114 if(socketModeone_to_many == 0) 4115 close(fd); 4116 } 4117 int main() 4118 { 4119 struct sctp_event_subscribe event; 4120 int lfd, cfd; 4121 int onoff = 1; 4122 struct sockaddr_in sin; 4123 if ((lfd = socket(AF_INET, SOCK_STREAM, IPPROTO_SCTP)) == -1) { 4124 perror("socket"); 4125 exit(1); 4126 } 4128 sin.sin_family = AF_INET; 4129 sin.sin_port = htons(7); 4130 sin.sin_addr.s_addr = INADDR_ANY; 4131 if (bind(lfd, (struct sockaddr *)&sin, sizeof (sin)) == -1) { 4132 perror("bind"); 4133 exit(1); 4134 } 4136 if (listen(lfd, 1) == -1) { 4137 perror("listen"); 4138 exit(1); 4139 } 4141 /* Wait for new associations */ 4142 for (;;) { 4143 if ((cfd = accept(lfd, NULL, 0)) == -1) { 4144 perror("accept"); 4145 exit(1); 4146 } 4148 /* Enable all events */ 4149 event.sctp_data_io_event = 1; 4150 event.sctp_association_event = 1; 4151 event.sctp_address_event = 1; 4152 event.sctp_send_failure_event = 1; 4153 event.sctp_peer_error_event = 1; 4154 event.sctp_shutdown_event = 1; 4155 event.sctp_partial_delivery_event = 1; 4156 event.sctp_adaptation_layer_event = 1; 4157 if (setsockopt(cfd, IPPROTO_SCTP, 4158 SCTP_EVENTS, &event, 4159 sizeof(event)) != 0) { 4160 perror("setevent failed"); 4161 exit(1); 4162 } 4163 /* Echo back any and all data */ 4164 echo(cfd,0); 4166 } 4167 } 4169 Appendix B. One-to-Many Style Code Example 4171 The following code is a simple implementation of an echo server over 4172 SCTP. The example shows how to use some features of one-to-many 4173 style IPv4 SCTP sockets, including: 4174 o Opening and binding of a socket 4175 o Enabling ancillary data 4176 o Enabling notifications 4177 o Using ancillary data with sendmsg() and recvmsg() 4178 o Using MSG_EOR to determine if an entire message has been read 4179 o Handling notifications 4181 Note most functions defined in Appendix A are reused in this example. 4183 int main() 4184 { 4185 int fd; 4186 int idleTime = 2; 4187 struct sockaddr_in sin; 4188 struct sctp_event_subscribe event; 4190 if ((fd = socket(AF_INET, SOCK_SEQPACKET, IPPROTO_SCTP)) == -1) { 4191 perror("socket"); 4192 exit(1); 4193 } 4195 sin.sin_family = AF_INET; 4196 sin.sin_port = htons(7); 4197 sin.sin_addr.s_addr = INADDR_ANY; 4198 if (bind(fd, (struct sockaddr *)&sin, sizeof (sin)) == -1) { 4199 perror("bind"); 4200 exit(1); 4201 } 4203 /* Enable all notifications and events */ 4204 event.sctp_data_io_event = 1; 4205 event.sctp_association_event = 1; 4206 event.sctp_address_event = 1; 4207 event.sctp_send_failure_event = 1; 4208 event.sctp_peer_error_event = 1; 4209 event.sctp_shutdown_event = 1; 4210 event.sctp_partial_delivery_event = 1; 4211 event.sctp_adaptation_layer_event = 1; 4212 if (setsockopt(fd, IPPROTO_SCTP, 4213 SCTP_EVENTS, &event, 4214 sizeof(event)) != 0) { 4215 perror("setevent failed"); 4216 exit(1); 4217 } 4218 /* Set associations to auto-close in 2 seconds of 4219 * inactivity 4220 */ 4221 if (setsockopt(fd, IPPROTO_SCTP, SCTP_AUTOCLOSE, 4222 &idleTime, 4) < 0) { 4223 perror("setsockopt SCTP_AUTOCLOSE"); 4224 exit(1); 4225 } 4227 /* Allow new associations to be accepted */ 4228 if (listen(fd, 1) < 0) { 4229 perror("listen"); 4230 exit(1); 4231 } 4233 /* Wait for new associations */ 4234 while(1){ 4235 /* Echo back any and all data */ 4236 echo(fd,1); /* from appendix a */ 4237 } 4238 } 4240 Authors' Addresses 4242 Randall R. Stewart 4243 Huawei 4244 Chapin, SC 29036 4245 USA 4247 Email: rstewart@huawei.com 4249 Kacheong Poon 4250 Sun Microsystems, Inc. 4251 4150 Network Circle 4252 Santa Clara, CA 95054 4253 USA 4255 Email: kacheong.poon@sun.com 4256 Michael Tuexen 4257 Muenster Univ. of Applied Sciences 4258 Stegerwaldstr. 39 4259 48565 Steinfurt 4260 Germany 4262 Email: tuexen@fh-muenster.de 4264 Vladislav Yasevich 4265 HP 4266 110 Spitrook Rd 4267 Nashua, NH, 03062 4268 USA 4270 Email: vladislav.yasevich@hp.com 4272 Peter Lei 4273 Cisco Systems, Inc. 4274 8735 West Higgins Road 4275 Suite 300 4276 Chicago, IL 60631 4277 USA 4279 Email: peterlei@cisco.com