idnits 2.17.1 draft-ietf-tsvwg-sctpsocket-09.txt: Checking boilerplate required by RFC 5378 and the IETF Trust (see https://trustee.ietf.org/license-info): ---------------------------------------------------------------------------- ** It looks like you're using RFC 3978 boilerplate. You should update this to the boilerplate described in the IETF Trust License Policy document (see https://trustee.ietf.org/license-info), which is required now. -- Found old boilerplate from RFC 3978, Section 5.1.a on line 27. -- Found old boilerplate from RFC 3978, Section 5.5 on line 3298. -- Found old boilerplate from RFC 3979, Section 5, paragraph 1 on line 3275. -- Found old boilerplate from RFC 3979, Section 5, paragraph 2 on line 3282. -- Found old boilerplate from RFC 3979, Section 5, paragraph 3 on line 3288. ** The document seems to lack an RFC 3978 Section 5.1 IPR Disclosure Acknowledgement. ** This document has an original RFC 3978 Section 5.4 Copyright Line, instead of the newer IETF Trust Copyright according to RFC 4748. ** This document has an original RFC 3978 Section 5.5 Disclaimer, instead of the newer disclaimer which includes the IETF Trust according to RFC 4748. ** The document uses RFC 3667 boilerplate or RFC 3978-like boilerplate instead of verbatim RFC 3978 boilerplate. After 6 May 2005, submission of drafts without verbatim RFC 3978 boilerplate is not accepted. The following non-3978 patterns matched text found in the document. That text should be removed or replaced: This document is an Internet-Draft and is subject to all provisions of Section 3 of RFC 3667. By submitting this Internet-Draft, each author represents that any applicable patent or other IPR claims of which he or she is aware have been or will be disclosed, and any of which he or she becomes aware will be disclosed, in accordance with Section 6 of BCP 79. Checking nits according to https://www.ietf.org/id-info/1id-guidelines.txt: ---------------------------------------------------------------------------- == No 'Intended status' indicated for this document; assuming Proposed Standard Checking nits according to https://www.ietf.org/id-info/checklist : ---------------------------------------------------------------------------- ** The document seems to lack an IANA Considerations section. (See Section 2.2 of https://www.ietf.org/id-info/checklist for how to handle the case when there are no actions for IANA.) ** The document seems to lack separate sections for Informative/Normative References. All references will be assumed normative when checking for downward references. ** There are 40 instances of too long lines in the document, the longest one being 14 characters in excess of 72. ** There is 1 instance of lines with control characters in the document. ** The abstract seems to contain references ([8]), which it shouldn't. Please replace those with straight textual mentions of the documents in question. == There is 1 instance of lines with non-RFC6890-compliant IPv4 addresses in the document. If these are example addresses, they should be changed. Miscellaneous warnings: ---------------------------------------------------------------------------- == The copyright year in the RFC 3978 Section 5.4 Copyright Line does not match the current year == Line 868 has weird spacing: '...n. The value...' == Line 880 has weird spacing: '...er send and ...' == Line 1048 has weird spacing: '...g_level cms...' == Line 1102 has weird spacing: '...g_level cms...' == Line 1261 has weird spacing: '..._change sn_a...' == (8 more instances...) == The document seems to lack the recommended RFC 2119 boilerplate, even if it appears to use RFC 2119 keywords. (The document does seem to have the reference to RFC 2119 which the ID-Checklist requires). -- The document seems to lack a disclaimer for pre-RFC5378 work, but may have content which was first submitted before 10 November 2008. If you have contacted all the original authors and they are all willing to grant the BCP78 rights to the IETF Trust, then this is fine, and you can ignore this comment. If not, you may need to add the pre-RFC5378 disclaimer. (See the Legal Provisions document at https://trustee.ietf.org/license-info for more information.) -- The document date (September 27, 2004) is 7151 days in the past. Is this intentional? -- Found something which looks like a code comment -- if you have code sections in the document, please surround them with '' and '' lines. Checking references for intended status: Proposed Standard ---------------------------------------------------------------------------- (See RFCs 3967 and 4897 for information about using normative references to lower-maturity documents in RFCs) == Missing Reference: '0' is mentioned on line 1525, but not defined == Unused Reference: '4' is defined on line 2879, but no explicit reference was found in the text == Unused Reference: '5' is defined on line 2882, but no explicit reference was found in the text ** Obsolete normative reference: RFC 793 (ref. '1') (Obsoleted by RFC 9293) ** Obsolete normative reference: RFC 1644 (ref. '3') (Obsoleted by RFC 6247) ** Obsolete normative reference: RFC 2292 (ref. '6') (Obsoleted by RFC 3542) ** Obsolete normative reference: RFC 2553 (ref. '7') (Obsoleted by RFC 3493) ** Obsolete normative reference: RFC 2960 (ref. '8') (Obsoleted by RFC 4960) Summary: 15 errors (**), 0 flaws (~~), 13 warnings (==), 8 comments (--). Run idnits with the --verbose option for more detailed information about the items above. -------------------------------------------------------------------------------- 2 Network Working Group R. Stewart 3 Internet-Draft Cisco Systems, Inc. 4 Expires: March 28, 2005 Q. Xie 5 Motorola, Inc. 6 L. Yarroll 7 TimeSys Corp 8 J. Wood 9 DoCoMo USA Labs 10 K. Poon 11 Sun Microsystems, Inc. 12 M. Tuexen 13 Univ. of Applied Sciences Muenster 14 September 27, 2004 16 Sockets API Extensions for Stream Control Transmission Protocol 17 (SCTP) 18 draft-ietf-tsvwg-sctpsocket-09.txt 20 Status of this Memo 22 This document is an Internet-Draft and is subject to all provisions 23 of section 3 of RFC 3667. By submitting this Internet-Draft, each 24 author represents that any applicable patent or other IPR claims of 25 which he or she is aware have been or will be disclosed, and any of 26 which he or she become aware will be disclosed, in accordance with 27 RFC 3668. 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 32 Internet-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 March 28, 2005. 47 Copyright Notice 48 Copyright (C) The Internet Society (2004). 50 Abstract 52 This document describes a mapping of the Stream Control Transmission 53 Protocol SCTP RFC2960 [8] into a sockets API. The benefits of this 54 mapping include compatibility for TCP applications, access to new 55 SCTP features and a consolidated error and event notification scheme. 57 Table of Contents 59 1. Introduction . . . . . . . . . . . . . . . . . . . . . . . . 5 60 2. Conventions . . . . . . . . . . . . . . . . . . . . . . . . 7 61 2.1 Data Types . . . . . . . . . . . . . . . . . . . . . . . . 7 62 3. one-to-many style Interface . . . . . . . . . . . . . . . . 8 63 3.1 Basic Operation . . . . . . . . . . . . . . . . . . . . . 8 64 3.1.1 socket() - one-to-many style socket . . . . . . . . . 9 65 3.1.2 bind() - one-to-many style socket . . . . . . . . . . 9 66 3.1.3 listen() - One-to-many style socket . . . . . . . . . 10 67 3.1.4 sendmsg() and recvmsg() - one-to-many style socket . . 11 68 3.1.5 close() - one-to-many style socket . . . . . . . . . . 12 69 3.1.6 connect() - one-to-many style socket . . . . . . . . . 13 70 3.2 Implicit Association Setup . . . . . . . . . . . . . . . . 13 71 3.3 Non-blocking mode . . . . . . . . . . . . . . . . . . . . 14 72 3.4 Special considerations . . . . . . . . . . . . . . . . . . 15 73 4. one-to-one style Interface . . . . . . . . . . . . . . . . . 17 74 4.1 Basic Operation . . . . . . . . . . . . . . . . . . . . . 17 75 4.1.1 socket() - one-to-one style socket . . . . . . . . . . 18 76 4.1.2 bind() - one-to-one style socket . . . . . . . . . . . 18 77 4.1.3 listen() - one-to-one style socket . . . . . . . . . . 19 78 4.1.4 accept() - one-to-one style socket . . . . . . . . . . 20 79 4.1.5 connect() - one-to-one style socket . . . . . . . . . 20 80 4.1.6 close() - one-to-one style socket . . . . . . . . . . 21 81 4.1.7 shutdown() - one-to-one style socket . . . . . . . . . 21 82 4.1.8 sendmsg() and recvmsg() - one-to-one style socket . . 22 83 4.1.9 getpeername() . . . . . . . . . . . . . . . . . . . . 23 84 5. Data Structures . . . . . . . . . . . . . . . . . . . . . . 24 85 5.1 The msghdr and cmsghdr Structures . . . . . . . . . . . . 24 86 5.2 SCTP msg_control Structures . . . . . . . . . . . . . . . 25 87 5.2.1 SCTP Initiation Structure (SCTP_INIT) . . . . . . . . 26 88 5.2.2 SCTP Header Information Structure (SCTP_SNDRCV) . . . 27 89 5.3 SCTP Events and Notifications . . . . . . . . . . . . . . 30 90 5.3.1 SCTP Notification Structure . . . . . . . . . . . . . 30 91 5.4 Ancillary Data Considerations and Semantics . . . . . . . 40 92 5.4.1 Multiple Items and Ordering . . . . . . . . . . . . . 40 93 5.4.2 Accessing and Manipulating Ancillary Data . . . . . . 40 94 5.4.3 Control Message Buffer Sizing . . . . . . . . . . . . 41 95 6. Common Operations for Both Styles . . . . . . . . . . . . . 43 96 6.1 send(), recv(), sendto(), recvfrom() . . . . . . . . . . . 43 97 6.2 setsockopt(), getsockopt() . . . . . . . . . . . . . . . . 44 98 6.3 read() and write() . . . . . . . . . . . . . . . . . . . . 44 99 6.4 getsockname() . . . . . . . . . . . . . . . . . . . . . . 44 100 7. Socket Options . . . . . . . . . . . . . . . . . . . . . . . 46 101 7.1 Read / Write Options . . . . . . . . . . . . . . . . . . . 47 102 7.1.1 Retransmission Timeout Parameters (SCTP_RTOINFO) . . . 47 103 7.1.2 Association Parameters (SCTP_ASSOCINFO) . . . . . . . 48 104 7.1.3 Initialization Parameters (SCTP_INITMSG) . . . . . . . 50 105 7.1.4 SO_LINGER . . . . . . . . . . . . . . . . . . . . . . 50 106 7.1.5 SCTP_NODELAY . . . . . . . . . . . . . . . . . . . . . 50 107 7.1.6 SO_RCVBUF . . . . . . . . . . . . . . . . . . . . . . 51 108 7.1.7 SO_SNDBUF . . . . . . . . . . . . . . . . . . . . . . 51 109 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE) . . . 51 110 7.1.9 Set Peer Primary Address 111 (SCTP_SET_PEER_PRIMARY_ADDR) . . . . . . . . . . . . . 51 112 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR) . . . . . . 52 113 7.1.11 Set Adaption Layer Indicator (SCTP_ADAPTION_LAYER) . 52 114 7.1.12 Enable/Disable message fragmentation 115 (SCTP_DISABLE_FRAGMENTS) . . . . . . . . . . . . . . 53 116 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS) . . 53 117 7.1.14 Set default send parameters 118 (SCTP_DEFAULT_SEND_PARAM) . . . . . . . . . . . . . 53 119 7.1.15 Set notification and ancillary events 120 (SCTP_EVENTS) . . . . . . . . . . . . . . . . . . . 54 121 7.1.16 Set/clear IPv4 mapped addresses 122 (SCTP_I_WANT_MAPPED_V4_ADDR) . . . . . . . . . . . . 54 123 7.1.17 Set the maximum fragmentation size (SCTP_MAXSEG) . . 54 124 7.2 Read-Only Options . . . . . . . . . . . . . . . . . . . . 54 125 7.2.1 Association Status (SCTP_STATUS) . . . . . . . . . . . 54 126 7.2.2 Peer Address Information (SCTP_GET_PEER_ADDR_INFO) . . 56 127 7.3 Ancillary Data and Notification Interest Options . . . . . 57 128 8. New Interfaces . . . . . . . . . . . . . . . . . . . . . . . 60 129 8.1 sctp_bindx() . . . . . . . . . . . . . . . . . . . . . . . 60 130 8.2 Branched-off Association . . . . . . . . . . . . . . . . . 61 131 8.3 sctp_getpaddrs() . . . . . . . . . . . . . . . . . . . . . 61 132 8.4 sctp_freepaddrs() . . . . . . . . . . . . . . . . . . . . 62 133 8.5 sctp_getladdrs() . . . . . . . . . . . . . . . . . . . . . 62 134 8.6 sctp_freeladdrs() . . . . . . . . . . . . . . . . . . . . 63 135 8.7 sctp_sendmsg() . . . . . . . . . . . . . . . . . . . . . . 63 136 8.8 sctp_recvmsg() . . . . . . . . . . . . . . . . . . . . . . 64 137 8.9 sctp_connectx() . . . . . . . . . . . . . . . . . . . . . 65 138 8.10 sctp_send() . . . . . . . . . . . . . . . . . . . . . . 66 139 8.11 sctp_sendx() . . . . . . . . . . . . . . . . . . . . . . 66 140 9. Preprocessor Constants . . . . . . . . . . . . . . . . . . . 68 141 10. Security Considerations . . . . . . . . . . . . . . . . . . 69 142 11. Acknowledgments . . . . . . . . . . . . . . . . . . . . . . 70 143 12. References . . . . . . . . . . . . . . . . . . . . . . . . . 70 144 Authors' Addresses . . . . . . . . . . . . . . . . . . . . . 71 145 A. one-to-one style Code Example . . . . . . . . . . . . . . . 73 146 B. one-to-many style Code Example . . . . . . . . . . . . . . . 79 147 Intellectual Property and Copyright Statements . . . . . . . 81 149 1. Introduction 151 The sockets API has provided a standard mapping of the Internet 152 Protocol suite to many operating systems. Both TCP RFC793 [1] and 153 UDP RFC768 [2] have benefited from this standard representation and 154 access method across many diverse platforms. SCTP is a new protocol 155 that provides many of the characteristics of TCP but also 156 incorporates semantics more akin to UDP. This document defines a 157 method to map the existing sockets API for use with SCTP, providing 158 both a base for access to new features and compatibility so that most 159 existing TCP applications can be migrated to SCTP with few (if any) 160 changes. 162 There are three basic design objectives: 164 1) Maintain consistency with existing sockets APIs: 165 We define a sockets mapping for SCTP that is consistent with other 166 sockets API protocol mappings (for instance, UDP, TCP, IPv4, and 167 IPv6). 168 2) Support a one-to-many style interface 169 This set of semantics is similar to that defined for 170 connection-less protocols, such as UDP. A one-to-many style SCTP 171 socket should be able to control multiple SCTP associations. This 172 is similar to an UDP socket, which can communicate with many peer 173 end points. Each of these associations is assigned an association 174 ID so that an applications can use the ID to differentiate them. 175 Note that SCTP is connection-oriented in nature, and it does not 176 support broadcast or multicast communications, as UDP does. 177 3) Support a one-to-one style interface 178 This interface supports a similar semantics as sockets for 179 connection-oriented protocols, such as TCP. A one-to-one style 180 SCTP socket should only control one SCTP association. 181 One purpose of defining this interface is to allow existing 182 applications built on other connection-oriented protocols be 183 ported to use SCTP with very little effort. And developers 184 familiar with those semantics can easily adapt to SCTP. Another 185 purpose is to make sure that existing mechanisms in most OSes to 186 deal with socket, such as select(), should continue to work with 187 this style of socket. 188 Extensions are added to this mapping to provide mechanisms to 189 exploit new features of SCTP. 191 Goals 2 and 3 are not compatible, so in this document we define two 192 modes of mapping, namely the one-to-many style mapping and the 193 one-to-one style mapping. These two modes share some common data 194 structures and operations, but will require the use of two different 195 application programming styles. Note that all new SCTP features can 196 be used with both styles of socket. The decision on which one to use 197 depends mainly on the nature of applications. 199 A mechanism is defined to extract a one-to-many style SCTP 200 association into a one-to-one style socket. 202 Some of the SCTP mechanisms cannot be adequately mapped to existing 203 socket interface. In some cases, it is more desirable to have new 204 interface instead of using existing socket calls. Section 8 of this 205 document describes those new interface. 207 2. Conventions 209 2.1 Data Types 211 Whenever possible, data types from Draft 6.6 (March 1997) of POSIX 212 1003.1g are used: uintN_t means an unsigned integer of exactly N bits 213 (e.g., uint16_t). We also assume the argument data types from 214 1003.1g when possible (e.g., the final argument to setsockopt() is a 215 size_t value). Whenever buffer sizes are specified, the POSIX 1003.1 216 size_t data type is used. 218 3. one-to-many style Interface 220 The one-to-many style interface has the following characteristics: 222 A) Outbound association setup is implicit. 224 B) Messages are delivered in complete messages (with one notable 225 exception). 227 C) There is a 1 to MANY relationship between socket and association. 229 3.1 Basic Operation 231 A typical server in this style uses the following socket calls in 232 sequence to prepare an endpoint for servicing requests: 234 1. socket() 235 2. bind() 236 3. listen() 237 4. recvmsg() 238 5. sendmsg() 239 6. close() 241 A typical client uses the following calls in sequence to setup an 242 association with a server to request services: 244 1. socket() 245 2. sendmsg() 246 3. recvmsg() 247 4. close() 249 In this style, by default, all the associations connected to the 250 endpoint are represented with a single socket. Each associations is 251 assigned an association ID (type is sctp_assoc_t) so that an 252 application can use it to differentiate between them. In some 253 implementations, the peer end point's addresses can also be used for 254 this purpose. But this is not required for performance reasons. If 255 an implementation does not support using addresses to differentiate 256 between different associations, the sendto() call can only be used to 257 setup an association implicitly. It cannot be used to send data to 258 an established association as the association ID cannot be specified. 260 Once as association ID is assigned to an SCTP association, that ID 261 will not be reused until the application explicitly terminates the 262 association. The resources belonged to that association will not be 263 freed until that happens. This is similar to the close() operation 264 on a normal socket. The only exception is when the SCTP_AUTOCLOSE 265 option (section 7.1.8) is set. In this case, after the association 266 is terminated automatically, the association ID assigned to it can be 267 reused. All applications using this option should be aware of this 268 to avoid the possible problem of sending data to an incorrect peer 269 end point. 271 If the server or client wishes to branch an existing association off 272 to a separate socket, it is required to call sctp_peeloff() and in 273 the parameter specifies the association identification. The 274 sctp_peeloff() call will return a new socket which can then be used 275 with recv() and send() functions for message passing. See Section 276 8.2 for more on branched-off associations. 278 Once an association is branched off to a separate socket, it becomes 279 completely separated from the original socket. All subsequent 280 control and data operations to that association must be done through 281 the new socket. For example, the close operation on the original 282 socket will not terminate any associations that have been branched 283 off to a different socket. 285 We will discuss the one-to-many style socket calls in more details in 286 the following subsections. 288 3.1.1 socket() - one-to-many style socket 290 Applications use socket() to create a socket descriptor to represent 291 an SCTP endpoint. 293 The syntax is, 295 sd = socket(PF_INET, SOCK_SEQPACKET, IPPROTO_SCTP); 297 or, 299 sd = socket(PF_INET6, SOCK_SEQPACKET, IPPROTO_SCTP); 301 Here, SOCK_SEQPACKET indicates the creation of a one-to-many style 302 socket. 304 The first form creates an endpoint which can use only IPv4 addresses, 305 while, the second form creates an endpoint which can use both IPv6 306 and IPv4 addresses. 308 3.1.2 bind() - one-to-many style socket 310 Applications use bind() to specify which local address the SCTP 311 endpoint should associate itself with. 313 An SCTP endpoint can be associated with multiple addresses. To do 314 this, sctp_bindx() is introduced in section Section 8.1 to help 315 applications do the job of associating multiple addresses. 317 These addresses associated with a socket are the eligible transport 318 addresses for the endpoint to send and receive data. The endpoint 319 will also present these addresses to its peers during the association 320 initialization process, see RFC2960 [8]. 322 After calling bind(), if the endpoint wishes to accept new 323 associations on the socket, it must call listen() (see section 324 Section 3.1.3). 326 The syntax of bind() is, 328 ret = bind(int sd, struct sockaddr *addr, socklen_t addrlen); 330 sd: the socket descriptor returned by socket(). 331 addr: the address structure (struct sockaddr_in or struct 332 sockaddr_in6 RFC2553 [7]). 333 addrlen: the size of the address structure. 335 If sd is an IPv4 socket, the address passed must be an IPv4 address. 336 If the sd is an IPv6 socket, the address passed can either be an IPv4 337 or an IPv6 address. 339 Applications cannot call bind() multiple times to associate multiple 340 addresses to an endpoint. After the first call to bind(), all 341 subsequent calls will return an error. 343 If addr is specified as a wildcard (INADDR_ANY for an IPv4 address, 344 or as IN6ADDR_ANY_INIT or in6addr_any for an IPv6 address), the 345 operating system will associate the endpoint with an optimal address 346 set of the available interfaces. 348 If a bind() is not called prior to a sendmsg() call that initiates a 349 new association, the system picks an ephemeral port and will choose 350 an address set equivalent to binding with a wildcard address. One of 351 those addresses will be the primary address for the association. 352 This automatically enables the multi-homing capability of SCTP. 354 3.1.3 listen() - One-to-many style socket 356 By default, new associations are not accepted for one-to-many style 357 sockets. An application uses listen() to mark a socket as being able 358 to accept new associations. The syntax is, 360 int listen(int sd, int backlog); 361 sd - the socket descriptor of the endpoint. 363 backlog - if backlog is non-zero, enable listening else 364 disable listening. 366 Note that one-to-many style socket consumers do not need to call 367 accept to retrieve new associations. Calling accept() on a 368 one-to-many style socket should return EOPNOTSUPP. Rather, new 369 associations are accepted automatically, and notifications of the new 370 associations are delivered via recvmsg() with the SCTP_ASSOC_CHANGE 371 event (if these notifications are enabled). Clients will typically 372 not call listen(), so that they can be assured that the only 373 associations on the socket will be ones they actively initiated. 374 Server or peer-to-peer sockets, on the other hand, will always accept 375 new associations, so a well-written application using server 376 one-to-many style sockets must be prepared to handle new associations 377 from unwanted peers. 379 Also note that the SCTP_ASSOC_CHANGE event provides the association 380 ID for a new association, so if applications wish to use the 381 association ID as input to other socket calls, they should ensure 382 that the SCTP_ASSOC_CHANGE event is enabled (it is enabled by 383 default). 385 3.1.4 sendmsg() and recvmsg() - one-to-many style socket 387 An application uses sendmsg() and recvmsg() call to transmit data to 388 and receive data from its peer. 390 ssize_t sendmsg(int sd, const struct msghdr *message, int flags); 392 ssize_t recvmsg(int sd, struct msghdr *message, int flags); 394 sd: the socket descriptor of the endpoint. 395 message: pointer to the msghdr structure which contains a single user 396 message and possibly some ancillary data. See Section 5 for 397 complete description of the data structures. 398 flags: No new flags are defined for SCTP at this level. See Section 399 5 for SCTP-specific flags used in the msghdr structure. 401 As we will see in Section 5, along with the user data, the ancillary 402 data field is used to carry the sctp_sndrcvinfo and/or the 403 sctp_initmsg structures to perform various SCTP functions including 404 specifying options for sending each user message. Those options, 405 depending on whether sending or receiving, include stream number, 406 stream sequence number, various flags, context and payload protocol 407 Id, etc. 409 When sending user data with sendmsg(), the msg_name field in msghdr 410 structure will be filled with one of the transport addresses of the 411 intended receiver. If there is no association existing between the 412 sender and the intended receiver, the sender's SCTP stack will set up 413 a new association and then send the user data (see Section 3.2 for 414 more on implicit association setup). 416 If a peer sends a SHUTDOWN, a SCTP_SHUTDOWN_EVENT notification will 417 be delivered if that notification has been enabled, and no more data 418 can be sent to that association. Any attempt to send more data will 419 cause sendmsg() to return with an ESHUTDOWN error. Note that the 420 socket is still open for reading at this point so it is possible to 421 retrieve notifications. 423 When receiving a user message with recvmsg(), the msg_name field in 424 msghdr structure will be populated with the source transport address 425 of the user data. The caller of recvmsg() can use this address 426 information to determine to which association the received user 427 message belongs. Note that if SCTP_ASSOC_CHANGE events are disabled, 428 applications must use the peer transport address provided in the 429 msg_name field by recvmsg() to perform correlation to an association, 430 since they will not have the association ID. 432 If all data in a single message has been delivered, MSG_EOR will be 433 set in the msg_flags field of the msghdr structure (see section 434 Section 5.1). 436 If the application does not provide enough buffer space to completely 437 receive a data message, MSG_EOR will not be set in msg_flags. 438 Successive reads will consume more of the same message until the 439 entire message has been delivered, and MSG_EOR will be set. 441 If the SCTP stack is running low on buffers, it may partially deliver 442 a message. In this case, MSG_EOR will not be set, and more calls to 443 recvmsg() will be necessary to completely consume the message. Only 444 one message at a time can be partially delivered. 446 Note, if the socket is a branched-off socket that only represents one 447 association (see Section 3.1), the msg_name field can be used to 448 override the primary address when sending data. 450 3.1.5 close() - one-to-many style socket 452 Applications use close() to perform graceful shutdown (as described 453 in Section 10.1 of RFC2960 [8]) on ALL the associations currently 454 represented by a one-to-many style socket. 456 The syntax is: 458 ret = close(int sd); 459 sd - the socket descriptor of the associations to be closed. 461 To gracefully shutdown a specific association represented by the 462 one-to-many style socket, an application should use the sendmsg() 463 call, and including the MSG_EOF flag. A user may optionally 464 terminate an association non-gracefully by sending with the MSG_ABORT 465 flag and possibly passing a user specified abort code in the data 466 field. Both flags MSG_EOF and MSG_ABORT are passwd with ancillary 467 data (see Section 5.2.2) in the sendmsg call. 469 If sd in the close() call is a branched-off socket representing only 470 one association, the shutdown is performed on that association only. 472 3.1.6 connect() - one-to-many style socket 474 An application may use the connect() call in the one-to-many style to 475 initiate an association without sending data. 477 The syntax is: 479 ret = connect(int sd, const struct sockaddr *nam, socklen_t len); 481 sd: the socket descriptor to have a new association added to. 482 nam: the address structure (either struct sockaddr_in or struct 483 sockaddr_in6 defined in RFC2553 [7]). 484 len: the size of the address. 485 Multiple connect() calls can be made on the same socket to create 486 multiple associations. This is different from the semantics of 487 connect() on a UDP socket. 489 3.2 Implicit Association Setup 491 Once the bind() call is complete on a one-to-many style socket, the 492 application can begin sending and receiving data using the sendmsg()/ 493 recvmsg() or sendto()/recvfrom() calls, without going through any 494 explicit association setup procedures (i.e., no connect() calls 495 required). 497 Whenever sendmsg() or sendto() is called and the SCTP stack at the 498 sender finds that there is no association existing between the sender 499 and the intended receiver (identified by the address passed either in 500 the msg_name field of msghdr structure in the sendmsg() call or the 501 dest_addr field in the sendto() call), the SCTP stack will 502 automatically setup an association to the intended receiver. 504 Upon the successful association setup a SCTP_COMM_UP notification 505 will be dispatched to the socket at both the sender and receiver 506 side. This notification can be read by the recvmsg() system call 507 (see Section 3.1.3). 509 Note, if the SCTP stack at the sender side supports bundling, the 510 first user message may be bundled with the COOKIE ECHO message 511 RFC2960 [8]. 513 When the SCTP stack sets up a new association implicitly, it first 514 consults the sctp_initmsg structure, which is passed along within the 515 ancillary data in the sendmsg() call (see Section 5.2.1 for details 516 of the data structures), for any special options to be used on the 517 new association. 519 If this information is not present in the sendmsg() call, or if the 520 implicit association setup is triggered by a sendto() call, the 521 default association initialization parameters will be used. These 522 default association parameters may be set with respective 523 setsockopt() calls or be left to the system defaults. 525 Implicit association setup cannot be initiated by send()/recv() 526 calls. 528 3.3 Non-blocking mode 530 Some SCTP users might want to avoid blocking when they call socket 531 interface function. 533 Once all bind() calls are complete on a one-to-many style socket, the 534 application must set the non-blocking option by a fcntl() (such as 535 O_NONBLOCK). After which the sendmsg() function returns immediately, 536 and the success or failure of the data message (and possible 537 SCTP_INITMSG parameters) will be signaled by the SCTP_ASSOC_CHANGE 538 event with SCTP_COMM_UP or CANT_START_ASSOC. If user data could not 539 be sent (due to a CANT_START_ASSOC), the sender will also receive a 540 SCTP_SEND_FAILED event. Those event(s) can be received by the user 541 calling of recvmsg(). A server (having called listen()) is also 542 notified of an association up event by the reception of a 543 SCTP_ASSOC_CHANGE with SCTP_COMM_UP via the calling of recvmsg() and 544 possibly the reception of the first data message. 546 In order to shutdown the association gracefully, the user must call 547 sendmsg() with no data and with the MSG_EOF flag set. The function 548 returns immediately, and completion of the graceful shutdown is 549 indicated by an SCTP_ASSOC_CHANGE notification of type 550 SHUTDOWN_COMPLETE (see Section 5.3.1.1). Note that this can also be 551 done using the sctp_send() call described in Section 8.10. 553 An application is recommended to use caution when using select() (or 554 poll()) for writing on a one-to-many style socket. The reason being 555 that interpretation of select on write is implementation specific. 556 Generally a positive return on a select on write would only indicate 557 that one of the associations represented by the one-to-many socket is 558 writable. An application that writes after the select return may 559 still block since the association that was writeable is not the 560 destination association of the write call. Likewise select (or 561 poll()) for reading from a one-to-many socket will only return an 562 indication that one of the associations represented by the socket has 563 data to be read. 565 An application that wishes to know that a particular association is 566 ready for reading or writing should either use the one-to-one style 567 or use the sctp_peelloff() (see Section 8.2) function to seperate the 568 association of interest from the one-to-many socket. 570 3.4 Special considerations 572 The fact that a one-to-many style socket can provide access to many 573 SCTP associations through a single socket descriptor has important 574 implications for both application programmers and system programmers 575 implementing this API. A key issue is how buffer space inside the 576 sockets layer is managed. Because this implementation detail 577 directly affects how application programmers must write their code to 578 ensure correct operation and portability, this section provides some 579 guidance to both implementors and application programmers. 581 An important feature that SCTP shares with TCP is flow control: 582 specifically, a sender may not send data faster than the receiver can 583 consume it. 585 For TCP, flow control is typically provided for in the sockets API as 586 follows. If the reader stops reading, the sender queues messages in 587 the socket layer until it uses all of its socket buffer space 588 allocation creating a "stalled connection". Further attempts to 589 write to the socket will block or return the error EAGAIN or 590 EWOULDBLOCK for a non-blocking socket. At some point, either the 591 connection is closed, or the receiver begins to read again freeing 592 space in the output queue. 594 For one-to-one style SCTP sockets (this includes sockets descriptors 595 that were separated from a one-to-many style socket with 596 sctp_peeloff()) the behavior is identical. For one-to-many style 597 SCTP sockets, the fact that we have multiple associations on a single 598 socket makes the situation more complicated. If the implementation 599 uses a single buffer space allocation shared by all associations, a 600 single stalled association can prevent the further sending of data on 601 all associations active on a particular one-to-many style socket. 603 For a blocking socket, it should be clear that a single stalled 604 association can block the entire socket. For this reason, 605 application programmers may want to use non-blocking one-to-many 606 style sockets. The application should at least be able to send 607 messages to the non-stalled associations. 609 But a non-blocking socket is not sufficient if the API implementor 610 has chosen a single shared buffer allocation for the socket. A 611 single stalled association would eventually cause the shared 612 allocation to fill, and it would become impossible to send even to 613 non-stalled associations. 615 The API implementor can solve this problem by providing each 616 association with its own allocation of outbound buffer space. Each 617 association should conceptually have as much buffer space as it would 618 have if it had its own socket. As a bonus, this simplifies the 619 implementation of sctp_peeloff(). 621 To ensure that a given stalled association will not prevent other 622 non-stalled associations from being writable, application programmers 623 should either: 624 (a) demand that the underlying implementation dedicates independent 625 buffer space allotments to each association (as suggested above), 626 or 627 (b) verify that their application layer protocol does not permit 628 large amounts of unread data at the receiver (this is true of some 629 request-response protocols, for example), or 630 (c) use one-to-one style sockets for association which may 631 potentially stall (either from the beginning, or by using 632 sctp_peeloff before sending large amounts of data that may cause a 633 stalled condition). 634 An implemenation which dedicates independent buffer space for each 635 association should define HAVE_SCTP_MULTIBUF to 1. 637 4. one-to-one style Interface 639 The goal of this style is to follow as closely as possible the 640 current practice of using the sockets interface for a connection 641 oriented protocol, such as TCP. This style enables existing 642 applications using connection oriented protocols to be ported to SCTP 643 with very little effort. 645 Note that some new SCTP features and some new SCTP socket options can 646 only be utilized through the use of sendmsg() and recvmsg() calls, 647 see Section 4.1.8. Also note that some socket interfaces may not be 648 able to provide data on the third leg of the association set up with 649 this interface style. 651 4.1 Basic Operation 653 A typical server in one-to-one style uses the following system call 654 sequence to prepare an SCTP endpoint for servicing requests: 656 1. socket() 658 2. bind() 660 3. listen() 662 4. accept() 664 The accept() call blocks until a new association is set up. It 665 returns with a new socket descriptor. The server then uses the new 666 socket descriptor to communicate with the client, using recv() and 667 send() calls to get requests and send back responses. 669 Then it calls 671 5. close() 673 to terminate the association. 675 A typical client uses the following system call sequence to setup an 676 association with a server to request services: 678 1. socket() 680 2. connect() 682 After returning from connect(), the client uses send() and recv() 683 calls to send out requests and receive responses from the server. 685 The client calls 687 3. close() 689 to terminate this association when done. 691 4.1.1 socket() - one-to-one style socket 693 Applications calls socket() to create a socket descriptor to 694 represent an SCTP endpoint. 696 The syntax is: 698 int socket(PF_INET, SOCK_STREAM, IPPROTO_SCTP); 700 or, 702 int socket(PF_INET6, SOCK_STREAM, IPPROTO_SCTP); 704 Here, SOCK_STREAM indicates the creation of a one-to-one style 705 socket. 707 The first form creates an endpoint which can use only IPv4 addresses, 708 while the second form creates an endpoint which can use both IPv6 and 709 IPv4 addresses. 711 4.1.2 bind() - one-to-one style socket 713 Applications use bind() to pass an address to be associated with an 714 SCTP endpoint to the system. bind() allows only either a single 715 address or a IPv4 or IPv6 wildcard address to be bound. An SCTP 716 endpoint can be associated with multiple addresses. To do this, 717 sctp_bindx() is introduced in Section 8.1 to help applications do 718 the job of associating multiple addresses. 720 These addresses associated with a socket are the eligible transport 721 addresses for the endpoint to send and receive data. The endpoint 722 will also present these addresses to its peers during the association 723 initialization process, see RFC2960 [8]. 725 The syntax is: 727 int bind(int sd, struct sockaddr *addr, socklen_t addrlen); 729 sd: the socket descriptor returned by socket() call. 731 addr: the address structure (either struct sockaddr_in or struct 732 sockaddr_in6 defined in RFC2553 [7]). 733 addrlen: the size of the address structure. 735 If sd is an IPv4 socket, the address passed must be an IPv4 address. 736 Otherwise, i.e., the sd is an IPv6 socket, the address passed can 737 either be an IPv4 or an IPv6 address. 739 Applications cannot call bind() multiple times to associate multiple 740 addresses to the endpoint. After the first call to bind(), all 741 subsequent calls will return an error. 743 If addr is specified as a wildcard (INADDR_ANY for an IPv4 address, 744 or as IN6ADDR_ANY_INIT or in6addr_any for an IPv6 address), the 745 operating system will associate the endpoint with an optimal address 746 set of the available interfaces. 748 If a bind() is not called prior to the connect() call, the system 749 picks an ephemeral port and will choose an address set equivalent to 750 binding with a wildcard address. One of those addresses will be the 751 primary address for the association. This automatically enables the 752 multi-homing capability of SCTP. 754 The completion of this bind() process does not ready the SCTP 755 endpoint to accept inbound SCTP association requests. Until a 756 listen() system call, described below, is performed on the socket, 757 the SCTP endpoint will promptly reject an inbound SCTP INIT request 758 with an SCTP ABORT. 760 4.1.3 listen() - one-to-one style socket 762 Applications use listen() to ready the SCTP endpoint for accepting 763 inbound associations. 765 The syntax is: 767 int listen(int sd, int backlog); 769 sd: the socket descriptor of the SCTP endpoint. 770 backlog: this specifies the max number of outstanding associations 771 allowed in the socket's accept queue. These are the associations 772 that have finished the four-way initiation handshake (see Section 773 5 of RFC2960 [8]) and are in the ESTABLISHED state. Note, a 774 backlog of '0' indicates that the caller no longer wishes to 775 receive new associations. 777 4.1.4 accept() - one-to-one style socket 779 Applications use accept() call to remove an established SCTP 780 association from the accept queue of the endpoint. A new socket 781 descriptor will be returned from accept() to represent the newly 782 formed association. 784 The syntax is: 786 new_sd = accept(int sd, struct sockaddr *addr, socklen_t *addrlen); 788 new_sd: the socket descriptor for the newly formed association. 789 sd the listening socket descriptor. 790 addr on return, will contain the primary address of the peer 791 endpoint. 792 addrlen on return, will contain the size of addr. 794 4.1.5 connect() - one-to-one style socket 796 Applications use connect() to initiate an association to a peer. 798 The syntax is: 800 int connect(int sd, const struct sockaddr *addr, socklen_t addrlen); 802 sd: the socket descriptor of the endpoint. 803 addr the peer's address. 804 addrlen the size of the address. 806 This operation corresponds to the ASSOCIATE primitive described in 807 section 10.1 of RFC2960 [8]. 809 By default, the new association created has only one outbound stream. 810 The SCTP_INITMSG option described in Section 7.1.3 should be used 811 before connecting to change the number of outbound streams. 813 If a bind() is not called prior to the connect() call, the system 814 picks an ephemeral port and will choose an address set equivalent to 815 binding with INADDR_ANY and IN6ADDR_ANY for IPv4 and IPv6 socket 816 respectively. One of those addresses will be the primary address for 817 the association. This automatically enables the multi-homing 818 capability of SCTP. 820 Note that SCTP allows data exchange, similar to T/TCP RFC1644 [3], 821 during the association set up phase. If an application wants to do 822 this, it cannot use connect() call. Instead, it should use sendto() 823 or sendmsg() to initiate an association. If it uses sendto() and it 824 wants to change initialization behavior, it needs to use the 825 SCTP_INITMSG socket option before calling sendto(). Or it can use 826 SCTP_INIT type sendmsg() to initiate an association without doing the 827 setsockopt(). Note that some sockets implementations may not support 828 the sending of data to initiate an assocation with the one-to-one 829 style (implementations that do not support T/TCP normally have this 830 restriction). Implementations which allow sending of data to 831 initiate an association without calling connect() define the 832 preprocessor constant HAVE_SCTP_NOCONNECT to 1. 834 SCTP does not support half close semantics. This means that unlike 835 T/TCP, MSG_EOF should not be set in the flags parameter when calling 836 sendto() or sendmsg() when the call is used to initiate a connection. 837 MSG_EOF is not an acceptable flag with SCTP socket. 839 4.1.6 close() - one-to-one style socket 841 Applications use close() to gracefully close down an association. 843 The syntax is: 845 int close(int sd); 847 sd - the socket descriptor of the association to be closed. 849 After an application calls close() on a socket descriptor, no further 850 socket operations will succeed on that descriptor. 852 4.1.7 shutdown() - one-to-one style socket 854 SCTP differs from TCP in that it does not have half closed semantics. 855 Hence the shutdown() call for SCTP is an approximation of the TCP 856 shutdown() call, and solves some different problems. Full 857 TCP-compatibility is not provided, so developers porting TCP 858 applications to SCTP may need to recode sections that use shutdown(). 859 (Note that it is possible to achieve the same results as half close 860 in SCTP using SCTP streams.) 862 The syntax is: 864 int shutdown(int sd, int how); 866 sd - the socket descriptor of the association to be closed. 868 how - Specifies the type of shutdown. The values are 869 as follows: 871 SHUT_RD 872 Disables further receive operations. No SCTP 873 protocol action is taken. 875 SHUT_WR 876 Disables further send operations, and initiates 877 the SCTP shutdown sequence. 879 SHUT_RDWR 880 Disables further send and receive operations 881 and initiates the SCTP shutdown sequence. 883 The major difference between SCTP and TCP shutdown() is that SCTP 884 SHUT_WR initiates immediate and full protocol shutdown, whereas TCP 885 SHUT_WR causes TCP to go into the half closed state. SHUT_RD behaves 886 the same for SCTP as TCP. The purpose of SCTP SHUT_WR is to close 887 the SCTP association while still leaving the socket descriptor open, 888 so that the caller can receive back any data SCTP was unable to 889 deliver (see Section 5.3.1.4 for more information). 891 To perform the ABORT operation described in RFC2960 [8] section 10.1, 892 an application can use the socket option SO_LINGER. It is described 893 in Section 7.1.4. 895 4.1.8 sendmsg() and recvmsg() - one-to-one style socket 897 With a one-to-one style socket, the application can also use 898 sendmsg() and recvmsg() to transmit data to and receive data from its 899 peer. The semantics is similar to those used in the one-to-many 900 style (section Section 3.1.3), with the following differences: 902 1) When sending, the msg_name field in the msghdr is not used to 903 specify the intended receiver, rather it is used to indicate a 904 preferred peer address if the sender wishes to discourage the stack 905 from sending the message to the primary address of the receiver. If 906 the transport address given is not part of the current association, 907 the data will not be sent and a SCTP_SEND_FAILED event will be 908 delivered to the application if send failure events are enabled. 910 2) An application must use close() to gracefully shutdown an 911 association, or use SO_LINGER option with close() to abort an 912 association. It must not use the MSG_ABORT or MSG_EOF flag in 913 sendmsg(). The system returns an error if an application tries to do 914 so. 916 4.1.9 getpeername() 918 Applications use getpeername() to retrieve the primary socket address 919 of the peer. This call is for TCP compatibility, and is not 920 multi-homed. It does not work with one-to-many style sockets. See 921 Section 8.3 for a multi-homed/one-to-many style version of the call. 923 The syntax is: 925 int getpeername(int sd, struct sockaddr *address, 926 socklen_t *len); 928 sd - the socket descriptor to be queried. 930 address - On return, the peer primary address is stored in 931 this buffer. If the socket is an IPv4 socket, the 932 address will be IPv4. If the socket is an IPv6 socket, 933 the address will be either an IPv6 or IPv4 934 address. 936 len - The caller should set the length of address here. 937 On return, this is set to the length of the returned 938 address. 940 If the actual length of the address is greater than the length of the 941 supplied sockaddr structure, the stored address will be truncated. 943 5. Data Structures 945 We discuss in this section important data structures which are 946 specific to SCTP and are used with sendmsg() and recvmsg() calls to 947 control SCTP endpoint operations and to access ancillary information 948 and notifications. 950 5.1 The msghdr and cmsghdr Structures 952 The msghdr structure used in the sendmsg() and recvmsg() calls, as 953 well as the ancillary data carried in the structure, is the key for 954 the application to set and get various control information from the 955 SCTP endpoint. 957 The msghdr and the related cmsghdr structures are defined and 958 discussed in details in RFC2292 [6]. Here we will cite their 959 definitions from RFC2292 [6]. 961 The msghdr structure: 963 struct msghdr { 964 void *msg_name; /* ptr to socket address structure */ 965 socklen_t msg_namelen; /* size of socket address structure */ 966 struct iovec *msg_iov; /* scatter/gather array */ 967 size_t msg_iovlen; /* # elements in msg_iov */ 968 void *msg_control; /* ancillary data */ 969 socklen_t msg_controllen; /* ancillary data buffer length */ 970 int msg_flags; /* flags on received message */ 971 }; 973 The cmsghdr structure: 975 struct cmsghdr { 976 socklen_t cmsg_len; /* #bytes, including this header */ 977 int cmsg_level; /* originating protocol */ 978 int cmsg_type; /* protocol-specific type */ 979 /* followed by unsigned char cmsg_data[]; */ 980 }; 982 In the msghdr structure, the usage of msg_name has been discussed in 983 previous sections (see Section 3.1.3 and Section 4.1.8). 985 The scatter/gather buffers, or I/O vectors (pointed to by the msg_iov 986 field) are treated as a single SCTP data chunk, rather than multiple 987 chunks, for both sendmsg() and recvmsg(). 989 The msg_flags are not used when sending a message with sendmsg(). 991 If a notification has arrived, recvmsg() will return the notification 992 with the MSG_NOTIFICATION flag set in msg_flags. If the 993 MSG_NOTIFICATION flag is not set, recvmsg() will return data. See 994 Section 5.3 for more information about notifications. 996 If all portions of a data frame or notification have been read, 997 recvmsg() will return with MSG_EOR set in msg_flags. 999 5.2 SCTP msg_control Structures 1001 A key element of all SCTP-specific socket extensions is the use of 1002 ancillary data to specify and access SCTP-specific data via the 1003 struct msghdr's msg_control member used in sendmsg() and recvmsg(). 1004 Fine-grained control over initialization and sending parameters are 1005 handled with ancillary data. 1007 Each ancillary data item is proceeded by a struct cmsghdr (see 1008 Section 5.1), which defines the function and purpose of the data 1009 contained in in the cmsg_data[] member. 1011 There are two kinds of ancillary data used by SCTP: initialization 1012 data, and, header information (SNDRCV). Initialization data 1013 (one-to-many style only) sets protocol parameters for new 1014 associations. Section 5.2.1 provides more details. Header 1015 information can set or report parameters on individual messages in a 1016 stream. See Section 5.2.2 for how to use SNDRCV ancillary data. 1018 By default on a one-to-one style socket, SCTP will pass no ancillary 1019 data; on a one-to-many style socket, SCTP will only pass SCTP_SNDRCV 1020 and SCTP_ASSOC_CHANGE information. Specific ancillary data items can 1021 be enabled with socket options defined for SCTP; see Section 7.3. 1023 Note that all ancillary types are fixed length; see Section 5.4 for 1024 further discussion on this. These data structures use struct 1025 sockaddr_storage (defined in RFC2553 [7]) as a portable, fixed length 1026 address format. 1028 Other protocols may also provide ancillary data to the socket layer 1029 consumer. These ancillary data items from other protocols may 1030 intermingle with SCTP data. For example, the IPv6 socket API 1031 definitions (RFC2292 [6] and RFC2553 [7]) define a number of 1032 ancillary data items. If a socket API consumer enables delivery of 1033 both SCTP and IPv6 ancillary data, they both may appear in the same 1034 msg_control buffer in any order. An application may thus need to 1035 handle other types of ancillary data besides that passed by SCTP. 1037 The sockets application must provide a buffer large enough to 1038 accommodate all ancillary data provided via recvmsg(). If the buffer 1039 is not large enough, the ancillary data will be truncated and the 1040 msghdr's msg_flags will include MSG_CTRUNC. 1042 5.2.1 SCTP Initiation Structure (SCTP_INIT) 1044 This cmsghdr structure provides information for initializing new SCTP 1045 associations with sendmsg(). The SCTP_INITMSG socket option uses 1046 this same data structure. This structure is not used for recvmsg(). 1048 cmsg_level cmsg_type cmsg_data[] 1049 ------------ ------------ ---------------------- 1050 IPPROTO_SCTP SCTP_INIT struct sctp_initmsg 1052 Here is the definition of the sctp_initmsg structure: 1054 struct sctp_initmsg { 1055 uint16_t sinit_num_ostreams; 1056 uint16_t sinit_max_instreams; 1057 uint16_t sinit_max_attempts; 1058 uint16_t sinit_max_init_timeo; 1059 }; 1061 sinit_num_ostreams: 16 bits (unsigned integer) 1063 This is an integer number representing the number of streams that the 1064 application wishes to be able to send to. This number is confirmed 1065 in the SCTP_COMM_UP notification and must be verified since it is a 1066 negotiated number with the remote endpoint. The default value of 0 1067 indicates to use the endpoint default value. 1069 sinit_max_instreams: 16 bits (unsigned integer) 1071 This value represents the maximum number of inbound streams the 1072 application is prepared to support. This value is bounded by the 1073 actual implementation. In other words the user MAY be able to 1074 support more streams than the Operating System. In such a case, the 1075 Operating System limit overrides the value requested by the user. 1076 The default value of 0 indicates to use the endpoint's default value. 1078 sinit_max_attempts: 16 bits (unsigned integer) 1080 This integer specifies how many attempts the SCTP endpoint should 1081 make at resending the INIT. This value overrides the system SCTP 1082 'Max.Init.Retransmits' value. The default value of 0 indicates to 1083 use the endpoint's default value. This is normally set to the 1084 system's default 'Max.Init.Retransmit' value. 1086 sinit_max_init_timeo: 16 bits (unsigned integer) 1087 This value represents the largest Time-Out or RTO value (in 1088 milliseconds) to use inattempting a INIT. Normally the 'RTO.Max' is 1089 used to limit the doubling of the RTO upon timeout. For the INIT 1090 message this value MAY override 'RTO.Max'. This value MUST NOT 1091 influence 'RTO.Max' during data transmission and is only used to 1092 bound the initial setup time. A default value of 0 indicates to use 1093 the endpoint's default value. This is normally set to the system's 1094 'RTO.Max' value (60 seconds). 1096 5.2.2 SCTP Header Information Structure (SCTP_SNDRCV) 1098 This cmsghdr structure specifies SCTP options for sendmsg() and 1099 describes SCTP header information about a received message through 1100 recvmsg(). 1102 cmsg_level cmsg_type cmsg_data[] 1103 ------------ ------------ ---------------------- 1104 IPPROTO_SCTP SCTP_SNDRCV struct sctp_sndrcvinfo 1106 Here is the definition of sctp_sndrcvinfo: 1108 struct sctp_sndrcvinfo { 1109 uint16_t sinfo_stream; 1110 uint16_t sinfo_ssn; 1111 uint16_t sinfo_flags; 1112 uint32_t sinfo_ppid; 1113 uint32_t sinfo_context; 1114 uint32_t sinfo_timetolive; 1115 uint32_t sinfo_tsn; 1116 uint32_t sinfo_cumtsn; 1117 sctp_assoc_t sinfo_assoc_id; 1118 }; 1120 sinfo_stream: 16 bits (unsigned integer) 1122 For recvmsg() the SCTP stack places the message's stream number in 1123 this value. For sendmsg() this value holds the stream number that 1124 the application wishes to send this message to. If a sender 1125 specifies an invalid stream number an error indication is returned 1126 and the call fails. 1128 sinfo_ssn: 16 bits (unsigned integer) 1130 For recvmsg() this value contains the stream sequence number that the 1131 remote endpoint placed in the DATA chunk. For fragmented messages 1132 this is the same number for all deliveries of the message (if more 1133 than one recvmsg() is needed to read the message). The sendmsg() 1134 call will ignore this parameter. 1136 sinfo_ppid: 32 bits (unsigned integer) 1138 This value in sendmsg() is an opaque unsigned value that is passed to 1139 the remote end in each user message. In recvmsg() this value is the 1140 same information that was passed by the upper layer in the peer 1141 application. Please note that byte order issues are NOT accounted 1142 for and this information is passed opaquely by the SCTP stack from 1143 one end to the other. 1145 sinfo_context: 32 bits (unsigned integer) 1147 This value is an opaque 32 bit context datum that is used in the 1148 sendmsg() function. This value is passed back to the upper layer if 1149 a error occurs on the send of a message and is retrieved with each 1150 undelivered message (Note: if a endpoint has done multiple sends, all 1151 of which fail, multiple different sinfo_context values will be 1152 returned. One with each user data message). 1154 sinfo_flags: 16 bits (unsigned integer) 1156 This field may contain any of the following flags and is composed of 1157 a bitwise OR of these values. 1159 recvmsg() flags: 1161 MSG_UNORDERED - This flag is present when the message was sent 1162 non-ordered. 1164 sendmsg() flags: 1166 MSG_UNORDERED - This flag requests the un-ordered delivery of the 1167 message. If this flag is clear the datagram is 1168 considered an ordered send. 1170 MSG_ADDR_OVER - This flag, in the one-to-many style, requests the SCTP 1171 stack to override the primary destination address 1172 with the address found with the sendto/sendmsg 1173 call. 1175 MSG_ABORT - Setting this flag causes the specified association 1176 to abort by sending an ABORT message to the peer 1177 (one-to-many style only). The ABORT chunk will contain an 1178 error cause 'User Initiated Abort' with cause code 12. 1179 The cause specific information of this error cause is 1180 provided in msg_iov. 1182 MSG_EOF - Setting this flag invokes the SCTP graceful shutdown 1183 procedures on the specified association. Graceful 1184 shutdown assures that all data enqueued by both 1185 endpoints is successfully transmitted before closing 1186 the association (one-to-many style only). 1188 MSG_SENDALL - This flag, if set, will cause a one-to-many model 1189 socket to send the message to all associations 1190 that are currently established on this socket. For 1191 the one-to-one socket, this flag has no effect. 1193 sinfo_timetolive: 32 bit (unsigned integer) 1195 For the sending side, this field contains the message time to live in 1196 milliseconds. The sending side will expire the message within the 1197 specified time period if the message as not been sent to the peer 1198 within this time period. This value will override any default value 1199 set using any socket option. Also note that the value of 0 is 1200 special in that it indicates no timeout should occur on this message. 1202 sinfo_tsn: 32 bit (unsigned integer) 1204 For the receiving side, this field holds a TSN that was assigned to 1205 one of the SCTP Data Chunks. 1207 sinfo_cumtsn: 32 bit (unsigned integer) 1209 This field will hold the current cumulative TSN as known by the 1210 underlying SCTP layer. Note this field is ignored when sending and 1211 only valid for a receive operation when sinfo_flags are set to 1212 MSG_UNORDERED. 1214 sinfo_assoc_id: sizeof (sctp_assoc_t) 1216 The association handle field, sinfo_assoc_id, holds the identifier 1217 for the association announced in the SCTP_COMM_UP notification. All 1218 notifications for a given association have the same identifier. 1219 Ignored for one-to-one style sockets. 1221 A sctp_sndrcvinfo item always corresponds to the data in msg_iov. 1223 5.3 SCTP Events and Notifications 1225 An SCTP application may need to understand and process events and 1226 errors that happen on the SCTP stack. These events include network 1227 status changes, association startups, remote operational errors and 1228 undeliverable messages. All of these can be essential for the 1229 application. 1231 When an SCTP application layer does a recvmsg() the message read is 1232 normally a data message from a peer endpoint. If the application 1233 wishes to have the SCTP stack deliver notifications of non-data 1234 events, it sets the appropriate socket option for the notifications 1235 it wants. See Section 7.3 for these socket options. When a 1236 notification arrives, recvmsg() returns the notification in the 1237 application-supplied data buffer via msg_iov, and sets 1238 MSG_NOTIFICATION in msg_flags. 1240 This section details the notification structures. Every notification 1241 structure carries some common fields which provides general 1242 information. 1244 A recvmsg() call will return only one notification at a time. Just 1245 as when reading normal data, it may return part of a notification if 1246 the msg_iov buffer is not large enough. If a single read is not 1247 sufficient, msg_flags will have MSG_EOR clear. The user MUST finish 1248 reading the notification before subsequent data can arrive. 1250 5.3.1 SCTP Notification Structure 1252 The notification structure is defined as the union of all 1253 notification types. 1255 union sctp_notification { 1256 struct { 1257 uint16_t sn_type; /* Notification type. */ 1258 uint16_t sn_flags; 1259 uint32_t sn_length; 1260 } sn_header; 1261 struct sctp_assoc_change sn_assoc_change; 1262 struct sctp_paddr_change sn_paddr_change; 1263 struct sctp_remote_error sn_remote_error; 1264 struct sctp_send_failed sn_send_failed; 1265 struct sctp_shutdown_event sn_shutdown_event; 1266 struct sctp_adaption_event sn_adaption_event; 1267 struct sctp_pdapi_event sn_pdapi_event; 1268 }; 1270 sn_type: 16 bits (unsigned integer) 1272 The following list describes the SCTP notification and event types 1273 for the field sn_type. 1275 SCTP_ASSOC_CHANGE: This tag indicates that an association has either 1276 been opened or closed. Refer to Section 5.3.1.1 for details. 1277 SCTP_PEER_ADDR_CHANGE: This tag indicates that an address that is 1278 part of an existing association has experienced a change of state 1279 (e.g. a failure or return to service of the reachability of a 1280 endpoint via a specific transport address). Please see Section 1281 5.3.1.2 for data structure details. 1282 SCTP_REMOTE_ERROR: The attached error message is an Operational Error 1283 received from the remote peer. It includes the complete TLV sent 1284 by the remote endpoint. See Section 5.3.1.3 for the detailed 1285 format. 1286 SCTP_SEND_FAILED: The attached datagram could not be sent to the 1287 remote endpoint. This structure includes the original 1288 SCTP_SNDRCVINFO that was used in sending this message i.e. this 1289 structure uses the sctp_sndrecvinfo per Section 5.3.1.4. 1290 SCTP_SHUTDOWN_EVENT: The peer has sent a SHUTDOWN. No further data 1291 should be sent on this socket. 1292 SCTP_ADAPTION_INDICATION: This notification holds the peers indicated 1293 adaption layer. Please see Section 5.3.1.6. 1294 SCTP_PARTIAL_DELIVERY_EVENT: This notification is used to tell a 1295 receiver that the partial delivery has been aborted. This may 1296 indicate the association is about to be aborted. Please see 1297 Section 5.3.1.7 1299 All standard values for sn_type are greater than 2^15. Values from 1300 2^15 and down are reserved. 1302 sn_flags: 16 bits (unsigned integer) 1304 These are notification-specific flags. 1306 sn_length: 32 bits (unsigned integer) 1308 This is the length of the whole sctp_notification structure including 1309 the sn_type, sn_flags, and sn_length fields. 1311 5.3.1.1 SCTP_ASSOC_CHANGE 1313 Communication notifications inform the ULP that an SCTP association 1314 has either begun or ended. The identifier for a new association is 1315 provided by this notification. The notification information has the 1316 following format: 1318 struct sctp_assoc_change { 1319 uint16_t sac_type; 1320 uint16_t sac_flags; 1321 uint32_t sac_length; 1322 uint16_t sac_state; 1323 uint16_t sac_error; 1324 uint16_t sac_outbound_streams; 1325 uint16_t sac_inbound_streams; 1326 sctp_assoc_t sac_assoc_id; 1327 uint8_t sac_info[0]; 1328 }; 1330 sac_type: 1332 It should be SCTP_ASSOC_CHANGE. 1334 sac_flags: 16 bits (unsigned integer) 1336 Currently unused. 1338 sac_length: 32 bits (unsigned integer) 1340 This field is the total length of the notification data, including 1341 the notification header. 1343 sac_state: 16 bits (signed integer) 1345 This field holds one of a number of values that communicate the event 1346 that happened to the association. They include: 1348 Event Name Description 1349 ---------------- --------------- 1350 SCTP_COMM_UP A new association is now ready 1351 and data may be exchanged with this 1352 peer. 1354 SCTP_COMM_LOST The association has failed. The association 1355 is now in the closed state. If SEND FAILED 1356 notifications are turned on, a SCTP_COMM_LOST 1357 is followed by a series of SCTP_SEND_FAILED 1358 events, one for each outstanding message. 1360 SCTP_RESTART SCTP has detected that the peer has restarted. 1362 SCTP_SHUTDOWN_COMP The association has gracefully closed. 1364 SCTP_CANT_STR_ASSOC The association failed to setup. If non blocking 1365 mode is set and data was sent (in the udp mode), 1366 a SCTP_CANT_STR_ASSOC is followed by a series of 1367 SCTP_SEND_FAILED events, one for each outstanding 1368 message. 1370 sac_error: 16 bits (signed integer) 1372 If the state was reached due to a error condition (e.g. 1373 SCTP_COMM_LOST) any relevant error information is available in this 1374 field. This corresponds to the protocol error codes defined in 1375 RFC2960 [8]. 1377 sac_outbound_streams: 16 bits (unsigned integer) 1379 sac_inbound_streams: 16 bits (unsigned integer) 1381 The maximum number of streams allowed in each direction are available 1382 in sac_outbound_streams and sac_inbound streams. 1384 sac_assoc_id: sizeof (sctp_assoc_t) 1386 The association id field, holds the identifier for the association. 1387 All notifications for a given association have the same association 1388 identifier. For one-to-one style socket, this field is ignored. 1390 sac_info: variable 1392 If the sac_state is SCTP_COMM_LOST and an ABORT chunk was received 1393 for this association, sac_info[] contains the complete ABORT chunk as 1394 defined in the SCTP specification RFC2960 [8] section 3.3.7. 1396 5.3.1.2 SCTP_PEER_ADDR_CHANGE 1398 When a destination address on a multi-homed peer encounters a change 1399 an interface details event is sent. The information has the 1400 following structure: 1402 struct sctp_paddr_change { 1403 uint16_t spc_type; 1404 uint16_t spc_flags; 1405 uint32_t spc_length; 1406 struct sockaddr_storage spc_aaddr; 1407 int spc_state; 1408 int spc_error; 1409 sctp_assoc_t spc_assoc_id; 1410 } 1412 spc_type: 1414 It should be SCTP_PEER_ADDR_CHANGE. 1416 spc_flags: 16 bits (unsigned integer) 1418 Currently unused. 1420 spc_length: 32 bits (unsigned integer) 1422 This field is the total length of the notification data, including 1423 the notification header. 1425 spc_aaddr: sizeof (struct sockaddr_storage) 1427 The affected address field, holds the remote peer's address that is 1428 encountering the change of state. 1430 spc_state: 32 bits (signed integer) 1432 This field holds one of a number of values that communicate the event 1433 that happened to the address. They include: 1435 Event Name Description 1436 ---------------- --------------- 1437 SCTP_ADDR_AVAILABLE This address is now reachable. 1439 SCTP_ADDR_UNREACHABLE The address specified can no 1440 longer be reached. Any data sent 1441 to this address is rerouted to an 1442 alternate until this address becomes 1443 reachable. 1445 SCTP_ADDR_REMOVED The address is no longer part of 1446 the association. 1448 SCTP_ADDR_ADDED The address is now part of the 1449 association. 1451 SCTP_ADDR_MADE_PRIM This address has now been made 1452 to be the primary destination address. 1454 spc_error: 32 bits (signed integer) 1456 If the state was reached due to any error condition (e.g. 1457 SCTP_ADDR_UNREACHABLE) any relevant error information is available in 1458 this field. 1460 spc_assoc_id: sizeof (sctp_assoc_t) 1462 The association id field, holds the identifier for the association. 1463 All notifications for a given association have the same association 1464 identifier. For one-to-one style socket, this field is ignored. 1466 5.3.1.3 SCTP_REMOTE_ERROR 1468 A remote peer may send an Operational Error message to its peer. 1469 This message indicates a variety of error conditions on an 1470 association. The entire ERROR chunk as it appears on the wire is 1471 included in a SCTP_REMOTE_ERROR event. Please refer to the SCTP 1472 specification RFC2960 [8] and any extensions for a list of possible 1473 error formats. SCTP error notifications have the format: 1475 struct sctp_remote_error { 1476 uint16_t sre_type; 1477 uint16_t sre_flags; 1478 uint32_t sre_length; 1479 uint16_t sre_error; 1480 sctp_assoc_t sre_assoc_id; 1481 uint8_t sre_data[0]; 1482 }; 1484 sre_type: 1486 It should be SCTP_REMOTE_ERROR. 1488 sre_flags: 16 bits (unsigned integer) 1490 Currently unused. 1492 sre_length: 32 bits (unsigned integer) 1494 This field is the total length of the notification data, including 1495 the notification header and the contents of sre_data. 1497 sre_error: 16 bits (unsigned integer) 1499 This value represents one of the Operational Error causes defined in 1500 the SCTP specification, in network byte order. 1502 sre_assoc_id: sizeof (sctp_assoc_t) 1504 The association id field, holds the identifier for the association. 1505 All notifications for a given association have the same association 1506 identifier. For one-to-one style socket, this field is ignored. 1508 sre_data: variable 1510 This contains the ERROR chunk as defined in the SCTP specification 1511 RFC2960 [8] section 3.3.10. 1513 5.3.1.4 SCTP_SEND_FAILED 1515 If SCTP cannot deliver a message it may return the message as a 1516 notification. 1518 struct sctp_send_failed { 1519 uint16_t ssf_type; 1520 uint16_t ssf_flags; 1521 uint32_t ssf_length; 1522 uint32_t ssf_error; 1523 struct sctp_sndrcvinfo ssf_info; 1524 sctp_assoc_t ssf_assoc_id; 1525 uint8_t ssf_data[0]; 1526 }; 1527 ssf_type: 1529 It should be SCTP_SEND_FAILED. 1531 The flag value will take one of the following values 1533 SCTP_DATA_UNSENT - Indicates that the data was never put on 1534 the wire. 1536 SCTP_DATA_SENT - Indicates that the data was put on the wire. 1537 Note that this does not necessarily mean that the 1538 data was (or was not) successfully delivered. 1540 ssf_length: 32 bits (unsigned integer) 1542 This field is the total length of the notification data, including 1543 the notification header and the payload in ssf_data. 1545 ssf_error: 16 bits (unsigned integer) 1547 This value represents the reason why the send failed, and if set, 1548 will be a SCTP protocol error code as defined in RFC2960 [8] section 1549 3.3.10. 1551 ssf_info: sizeof (struct sctp_sndrcvinfo) 1553 The original send information associated with the undelivered 1554 message. 1556 ssf_assoc_id: sizeof (sctp_assoc_t) 1558 The association id field, sf_assoc_id, holds the identifier for the 1559 association. All notifications for a given association have the same 1560 association identifier. For one-to-one style socket, this field is 1561 ignored. 1563 ssf_data: variable length 1565 The undelivered message, exactly as delivered by the caller to the 1566 original send*() call. 1568 5.3.1.5 SCTP_SHUTDOWN_EVENT 1570 When a peer sends a SHUTDOWN, SCTP delivers this notification to 1571 inform the application that it should cease sending data. 1573 struct sctp_shutdown_event { 1574 uint16_t sse_type; 1575 uint16_t sse_flags; 1576 uint32_t sse_length; 1577 sctp_assoc_t sse_assoc_id; 1578 }; 1580 sse_type 1582 It should be SCTP_SHUTDOWN_EVENT 1584 sse_flags: 16 bits (unsigned integer) 1586 Currently unused. 1588 sse_length: 32 bits (unsigned integer) 1590 This field is the total length of the notification data, including 1591 the notification header. It will generally be sizeof (struct 1592 sctp_shutdown_event). 1594 sse_flags: 16 bits (unsigned integer) 1596 Currently unused. 1598 sse_assoc_id: sizeof (sctp_assoc_t) 1600 The association id field, holds the identifier for the association. 1601 All notifications for a given association have the same association 1602 identifier. For one-to-one style socket, this field is ignored. 1604 5.3.1.6 SCTP_ADAPTION_INDICATION 1606 When a peer sends a Adaption Layer Indication parameter , SCTP 1607 delivers this notification to inform the application that of the 1608 peers requested adaption layer. 1610 struct sctp_adaption_event { 1611 uint16_t sai_type; 1612 uint16_t sai_flags; 1613 uint32_t sai_length; 1614 uint32_t sai_adaption_ind; 1615 sctp_assoc_t sai_assoc_id; 1616 }; 1618 sai_type 1620 It should be SCTP_ADAPTION_INDICATION 1621 sai_flags: 16 bits (unsigned integer) 1623 Currently unused. 1625 sai_length: 32 bits (unsigned integer) 1627 This field is the total length of the notification data, including 1628 the notification header. It will generally be sizeof (struct 1629 sctp_adaption_event). 1631 sai_adaption_ind: 32 bits (unsigned integer) 1633 This field holds the bit array sent by the peer in the adaption layer 1634 indication parameter. The bits are in network byte order. 1636 sai_assoc_id: sizeof (sctp_assoc_t) 1638 The association id field, holds the identifier for the association. 1639 All notifications for a given association have the same association 1640 identifier. For one-to-one style socket, this field is ignored. 1642 5.3.1.7 SCTP_PARTIAL_DELIVERY_EVENT 1644 When a receiver is engaged in a partial delivery of a message this 1645 notification will be used to indicate various events. 1647 struct sctp_pdapi_event { 1648 uint16_t pdapi_type; 1649 uint16_t pdapi_flags; 1650 uint32_t pdapi_length; 1651 uint32_t pdapi_indication; 1652 sctp_assoc_t pdapi_assoc_id; 1653 }; 1655 pdapi_type 1657 It should be SCTP_PARTIAL_DELIVERY_EVENT 1659 pdapi_flags: 16 bits (unsigned integer) 1661 Currently unused. 1663 pdapi_length: 32 bits (unsigned integer) 1665 This field is the total length of the notification data, including 1666 the notification header. It will generally be sizeof (struct 1667 sctp_pdapi_event). 1669 pdapi_indication: 32 bits (unsigned integer) 1671 This field holds the indication being sent to the application 1672 possible values include: 1674 SCTP_PARTIAL_DELIVERY_ABORTED 1676 pdapi_assoc_id: sizeof (sctp_assoc_t) 1678 The association id field, holds the identifier for the association. 1679 All notifications for a given association have the same association 1680 identifier. For one-to-one style socket, this field is ignored. 1682 5.4 Ancillary Data Considerations and Semantics 1684 Programming with ancillary socket data contains some subtleties and 1685 pitfalls, which are discussed below. 1687 5.4.1 Multiple Items and Ordering 1689 Multiple ancillary data items may be included in any call to 1690 sendmsg() or recvmsg(); these may include multiple SCTP or non-SCTP 1691 items, or both. 1693 The ordering of ancillary data items (either by SCTP or another 1694 protocol) is not significant and is implementation-dependent, so 1695 applications must not depend on any ordering. 1697 SCTP_SNDRCV items must always correspond to the data in the msghdr's 1698 msg_iov member. There can be only a single SCTP_SNDRCV info for each 1699 sendmsg() or recvmsg() call. 1701 5.4.2 Accessing and Manipulating Ancillary Data 1703 Applications can infer the presence of data or ancillary data by 1704 examining the msg_iovlen and msg_controllen msghdr members, 1705 respectively. 1707 Implementations may have different padding requirements for ancillary 1708 data, so portable applications should make use of the macros 1709 CMSG_FIRSTHDR, CMSG_NXTHDR, CMSG_DATA, CMSG_SPACE, and CMSG_LEN. See 1710 RFC2292 [6] and your SCTP implementation's documentation for more 1711 information. Following is an example, from RFC2292 [6], 1712 demonstrating the use of these macros to access ancillary data: 1714 struct msghdr msg; 1715 struct cmsghdr *cmsgptr; 1717 /* fill in msg */ 1719 /* call recvmsg() */ 1721 for (cmsgptr = CMSG_FIRSTHDR(&msg); cmsgptr != NULL; 1722 cmsgptr = CMSG_NXTHDR(&msg, cmsgptr)) { 1723 if (cmsgptr->cmsg_level == ... && cmsgptr->cmsg_type == ... ) { 1724 u_char *ptr; 1726 ptr = CMSG_DATA(cmsgptr); 1727 /* process data pointed to by ptr */ 1728 } 1729 } 1731 5.4.3 Control Message Buffer Sizing 1733 The information conveyed via SCTP_SNDRCV events will often be 1734 fundamental to the correct and sane operation of the sockets 1735 application. This is particularly true of the one-to-many semantics, 1736 but also of the one-ton-one semantics. For example, if an 1737 application needs to send and receive data on different SCTP streams, 1738 SCTP_SNDRCV events are indispensable. 1740 Given that some ancillary data is critical, and that multiple 1741 ancillary data items may appear in any order, applications should be 1742 carefully written to always provide a large enough buffer to contain 1743 all possible ancillary data that can be presented by recvmsg(). If 1744 the buffer is too small, and crucial data is truncated, it may pose a 1745 fatal error condition. 1747 Thus it is essential that applications be able to deterministically 1748 calculate the maximum required buffer size to pass to recvmsg(). One 1749 constraint imposed on this specification that makes this possible is 1750 that all ancillary data definitions are of a fixed length. One way 1751 to calculate the maximum required buffer size might be to take the 1752 sum the sizes of all enabled ancillary data item structures, as 1753 calculated by CMSG_SPACE. For example, if we enabled 1754 SCTP_SNDRCV_INFO and IPV6_RECVPKTINFO RFC2292 [6], we would calculate 1755 and allocate the buffer size as follows: 1757 size_t total; 1758 void *buf; 1760 total = CMSG_SPACE(sizeof (struct sctp_sndrcvinfo)) + 1761 CMSG_SPACE(sizeof (struct in6_pktinfo)); 1763 buf = malloc(total); 1765 We could then use this buffer for msg_control on each call to 1766 recvmsg() and be assured that we would not lose any ancillary data to 1767 truncation. 1769 6. Common Operations for Both Styles 1771 6.1 send(), recv(), sendto(), recvfrom() 1773 Applications can use send() and sendto() to transmit data to the peer 1774 of an SCTP endpoint. recv() and recvfrom() can be used to receive 1775 data from the peer. 1777 The syntax is: 1779 ssize_t send(int sd, const void *msg, size_t len, int flags); 1780 ssize_t sendto(int sd, const void *msg, size_t len, int flags, 1781 const struct sockaddr *to, socklen_t tolen); 1782 ssize_t recv(int sd, void *buf, size_t len, int flags); 1783 ssize_t recvfrom(int sd, void *buf, size_t len, int flags, 1784 struct sockaddr *from, socklen_t *fromlen); 1786 sd - the socket descriptor of an SCTP endpoint. 1787 msg - the message to be sent. 1788 len - the size of the message or the size of buffer. 1789 to - one of the peer addresses of the association to be 1790 used to send the message. 1791 tolen - the size of the address. 1792 buf - the buffer to store a received message. 1793 from - the buffer to store the peer address used to send the 1794 received message. 1795 fromlen - the size of the from address 1796 flags - (described below). 1798 These calls give access to only basic SCTP protocol features. If 1799 either peer in the association uses multiple streams, or sends 1800 unordered data these calls will usually be inadequate, and may 1801 deliver the data in unpredictable ways. 1803 SCTP has the concept of multiple streams in one association. The 1804 above calls do not allow the caller to specify on which stream a 1805 message should be sent. The system uses stream 0 as the default 1806 stream for send() and sendto(). recv() and recvfrom() return data 1807 from any stream, but the caller can not distinguish the different 1808 streams. This may result in data seeming to arrive out of order. 1809 Similarly, if a data chunk is sent unordered, recv() and recvfrom() 1810 provide no indication. 1812 SCTP is message based. The msg buffer above in send() and sendto() 1813 is considered to be a single message. This means that if the caller 1814 wants to send a message which is composed by several buffers, the 1815 caller needs to combine them before calling send() or sendto(). 1816 Alternately, the caller can use sendmsg() to do that without 1817 combining them. recv() and recvfrom() cannot distinguish message 1818 boundaries. 1820 In receiving, if the buffer supplied is not large enough to hold a 1821 complete message, the receive call acts like a stream socket and 1822 returns as much data as will fit in the buffer. 1824 Note, the send() and recv() calls may not be used for a one-to-many 1825 style socket. 1827 Note, if an application calls a send function with no user data and 1828 no ancillary data the SCTP implementation should reject the request 1829 with an appropriate error message. An implementation is NOT allowed 1830 to send a Data chunk with no user data RFC2960 [8]. 1832 6.2 setsockopt(), getsockopt() 1834 Applications use setsockopt() and getsockopt() to set or retrieve 1835 socket options. Socket options are used to change the default 1836 behavior of sockets calls. They are described in Section 7 1838 The syntax is: 1840 ret = getsockopt(int sd, int level, int optname, void *optval, 1841 socklen_t *optlen); 1842 ret = setsockopt(int sd, int level, int optname, const void *optval, 1843 socklen_t optlen); 1845 sd - the socket descript. 1846 level - set to IPPROTO_SCTP for all SCTP options. 1847 optname - the option name. 1848 optval - the buffer to store the value of the option. 1849 optlen - the size of the buffer (or the length of the option 1850 returned). 1852 6.3 read() and write() 1854 Applications can use read() and write() to send and receive data to 1855 and from peer. They have the same semantics as send() and recv() 1856 except that the flags parameter cannot be used. 1858 Note, these calls, when used in the one-to-many style, may only be 1859 used with branched off socket descriptors (see Section 8.2). 1861 6.4 getsockname() 1863 Applications use getsockname() to retrieve the locally-bound socket 1864 address of the specified socket. This is especially useful if the 1865 caller let SCTP chose a local port. This call is for where the 1866 endpoint is not multi-homed. It does not work well with multi-homed 1867 sockets. See Section 8.5 for a multi-homed version of the call. 1869 The syntax is: 1871 int getsockname(int sd, struct sockaddr *address, 1872 socklen_t *len); 1874 sd - the socket descriptor to be queried. 1876 address - On return, one locally bound address (chosen by 1877 the SCTP stack) is stored in this buffer. If the 1878 socket is an IPv4 socket, the address will be IPv4. 1879 If the socket is an IPv6 socket, the address will 1880 be either an IPv6 or IPv4 address. 1882 len - The caller should set the length of address here. 1883 On return, this is set to the length of the returned 1884 address. 1886 If the actual length of the address is greater than the length of the 1887 supplied sockaddr structure, the stored address will be truncated. 1889 If the socket has not been bound to a local name, the value stored in 1890 the object pointed to by address is unspecified. 1892 7. Socket Options 1894 The following sub-section describes various SCTP level socket options 1895 that are common to both styles. SCTP associations can be 1896 multi-homed. Therefore, certain option parameters include a 1897 sockaddr_storage structure to select which peer address the option 1898 should be applied to. 1900 For the one-to-many style sockets, an sctp_assoc_t structure 1901 (association ID) is used to identify the the association instance 1902 that the operation affects. So it must be set when using this style. 1904 For the one-to-one style sockets and branched off one-to-many style 1905 sockets (see Section 8.2) this association ID parameter is ignored. 1907 Note that socket or IP level options are set or retrieved per socket. 1908 This means that for one-to-many style sockets, those options will be 1909 applied to all associations belonging to the socket. And for 1910 one-to-one style, those options will be applied to all peer addresses 1911 of the association controlled by the socket. Applications should be 1912 very careful in setting those options. 1914 For some IP stacks getsockopt() is read-only, so a new interface will 1915 be needed when information must be passed both in to and out of the 1916 SCTP stack. The syntax for scpt_opt_info() is, 1918 int sctp_opt_info(int sd, 1919 sctp_assoc_t id, 1920 int opt, 1921 void *arg, 1922 socklen_t *size); 1924 For one-to-many style sockets, id specifies the association to query. 1925 For one-to-one style sockets, id is ignored. 1927 opt specifies which SCTP socket option to get. It can any socket 1928 option currently supported that requests information (either read/ 1929 write options or read only) such as: 1931 SCTP_RTOINFO 1932 SCTP_ASSOCINFO 1933 SCTP_DEFAULT_SEND_PARAM 1934 SCTP_GET_PEER_ADDR_INFO 1935 SCTP_PRIMARY_ADDR 1936 SCTP_PEER_ADDR_PARAMS 1937 SCTP_STATUS 1939 arg is an option-specific structure buffer provided by the caller. 1941 See Section 8.5) subsections for more information on these options 1942 and option-specific structures. 1944 sctp_opt_info() returns 0 on success, or on failure returns -1 and 1945 sets errno to the appropriate error code. 1947 All options that support specific settings on an association by 1948 filling in either an association id variable or a sockaddr_storage 1949 SHOULD also support setting of the same value for the entire endpoint 1950 (i.e. future associations). To accomplish this the following logic 1951 is used when setting one of these options: 1953 a) If an address is specified via a sockaddr_storage that is included 1954 in the structure the address is used to lookup the association and 1955 the settings are applied to the specific address (if appropriate) 1956 or to the entire association. 1957 b) If an association identification is filled in but not a 1958 sockaddr_storage (if present) the association is found using the 1959 association identification and the settings should be applied to 1960 the entire association (since a specific address is not 1961 specified). Note this also applies to options that hold an 1962 association identification in their structure but do not have a 1963 sockaddr_storage field. 1964 c) If neither the sockaddr_storage or association identification is 1965 set i.e. the sockaddr_storage is set to all 0's (INADDR_ANY) and 1966 the association identification is 0, the settings are a default 1967 and to be applied to the endpoint (all future associations). 1969 7.1 Read / Write Options 1971 7.1.1 Retransmission Timeout Parameters (SCTP_RTOINFO) 1973 The protocol parameters used to initialize and bound retransmission 1974 timeout (RTO) are tunable. See RFC2960 [8] for more information on 1975 how these parameters are used in RTO calculation. 1977 The following structure is used to access and modify these 1978 parameters: 1980 struct sctp_rtoinfo { 1981 sctp_assoc_t srto_assoc_id; 1982 uint32_t srto_initial; 1983 uint32_t srto_max; 1984 uint32_t srto_min; 1985 }; 1986 srto_initial - This contains the initial RTO value. 1987 srto_max and srto_min - These contain the maximum and minimum bounds 1988 for all RTOs. 1989 srto_assoc_id - (one-to-many style socket) This is filled in the application, 1990 and identifies the association for this query. If 1991 this parameter is '0' (on a one-to-many style socket), 1992 then the change effects the entire endpoint. 1994 All parameters are time values, in milliseconds. A value of 0, when 1995 modifying the parameters, indicates that the current value should not 1996 be changed. 1998 To access or modify these parameters, the application should call 1999 getsockopt or setsockopt() respectively with the option name 2000 SCTP_RTOINFO. 2002 7.1.2 Association Parameters (SCTP_ASSOCINFO) 2004 This option is used to both examine and set various association and 2005 endpoint parameters. 2007 See RFC2960 [8] for more information on how this parameter is used. 2008 The peer address parameter is ignored for one-to-one style socket. 2010 The following structure is used to access and modify this parameters: 2012 struct sctp_assocparams { 2013 sctp_assoc_t sasoc_assoc_id; 2014 uint16_t sasoc_asocmaxrxt; 2015 uint16_t sasoc_number_peer_destinations; 2016 uint32_t sasoc_peer_rwnd; 2017 uint32_t sasoc_local_rwnd; 2018 uint32_t sasoc_cookie_life; 2019 }; 2020 sasoc_asocmaxrxt - This contains the maximum retransmission attempts 2021 to make for the association. 2023 sasoc_number_peer_destinations - This is the number of destination 2024 addresses that the peer has. 2025 sasoc_peer_rwnd - This holds the current value of the peers 2026 rwnd (reported in the last SACK) minus any 2027 outstanding data (i.e. data inflight). 2028 sasoc_local_rwnd - This holds the last reported rwnd that was 2029 sent to the peer. 2030 sasoc_cookie_life - This is the associations cookie life value 2031 used when issuing cookies. 2032 sasoc_assoc_id - (one-to-many style socket) This is filled in the application, 2033 and identifies the association for this query. 2035 This information may be examined for either the endpoint or a 2036 specific association. To examine a endpoints default parameters the 2037 association id (sasoc_assoc_id) should must be set to the value '0'. 2038 The values of the sasoc_peer_rwnd is meaningless when examining 2039 endpoint information. 2041 All parameters are time values, in milliseconds. A value of 0, when 2042 modifying the parameters, indicates that the current value should not 2043 be changed. 2045 The values of the sasoc_asocmaxrxt and sasoc_cookie_life may be set 2046 on either an endpoint or association basis. The rwnd and destination 2047 counts (sasoc_number_peer_destinations, 2048 sasoc_peer_rwnd,sasoc_local_rwnd) are NOT settable and any value 2049 placed in these is ignored. 2051 To access or modify these parameters, the application should call 2052 getsockopt or setsockopt() respectively with the option name 2053 SCTP_ASSOCINFO. 2055 The maximum number of retransmissions before an address is considered 2056 unreachable is also tunable, but is address-specific, so it is 2057 covered in a separate option. If an application attempts to set the 2058 value of the association maximum retransmission parameter to more 2059 than the sum of all maximum retransmission parameters, setsockopt() 2060 shall return an error. The reason for this, from RFC2960 [8] section 2061 8.2: 2063 Note: When configuring the SCTP endpoint, the user should avoid 2064 having the value of 'Association.Max.Retrans' larger than the 2065 summation of the 'Path.Max.Retrans' of all the destination addresses 2066 for the remote endpoint. Otherwise, all the destination addresses 2067 may become inactive while the endpoint still considers the peer 2068 endpoint reachable. 2070 7.1.3 Initialization Parameters (SCTP_INITMSG) 2072 Applications can specify protocol parameters for the default 2073 association initialization. The structure used to access and modify 2074 these parameters is defined in Section 5.2.1). The option name 2075 argument to setsockopt() and getsockopt() is SCTP_INITMSG. 2077 Setting initialization parameters is effective only on an unconnected 2078 socket (for one-to-many style sockets only future associations are 2079 effected by the change). With one-to-one style sockets, this option 2080 is inherited by sockets derived from a listener socket. 2082 7.1.4 SO_LINGER 2084 An application using the one-to-one style socket can use this option 2085 to perform the SCTP ABORT primitive. The linger option structure is: 2087 struct linger { 2088 int l_onoff; /* option on/off */ 2089 int l_linger; /* linger time */ 2090 }; 2092 To enable the option, set l_onoff to 1. If the l_linger value is set 2093 to 0, calling close() is the same as the ABORT primitive. If the 2094 value is set to a negative value, the setsockopt() call will return 2095 an error. If the value is set to a positive value linger_time, the 2096 close() can be blocked for at most linger_time ms. If the graceful 2097 shutdown phase does not finish during this period, close() will 2098 return but the graceful shutdown phase continues in the system. 2100 Note, this is a socket level option NOT an SCTP level option. So 2101 when setting SO_LINGER you must specify a level of SOL_SOCKET in the 2102 setsockopt() call. 2104 7.1.5 SCTP_NODELAY 2106 Turn on/off any Nagle-like algorithm. This means that packets are 2107 generally sent as soon as possible and no unnecessary delays are 2108 introduced, at the cost of more packets in the network. Expects an 2109 integer boolean flag. 2111 7.1.6 SO_RCVBUF 2113 Sets receive buffer size in octets. For SCTP one-to-one style 2114 sockets, this controls the receiver window size. For one-to-many 2115 style sockets the meaning depends on the constant HAVE_SCTP_MULTIBUF 2116 (see Section 3.4). If the implementation defines HAVE_SCTP_MULTIBUF 2117 as 1, this controls the receiver window size for each association 2118 bound to the socket descriptor. If the implementation defines 2119 HAVE_SCTP_MULTIBUF as 0, this controls the size of the single receive 2120 buffer for the whole socket. The call expects an integer. 2122 7.1.7 SO_SNDBUF 2124 Sets send buffer size. For SCTP one-to-one style sockets, this 2125 controls the amount of data SCTP may have waiting in internal buffers 2126 to be sent. This option therefore bounds the maximum size of data 2127 that can be sent in a single send call. For one-to-many style 2128 sockets, the effect is the same, except that it applies to one or all 2129 associations (see Section 3.4) bound to the socket descriptor 2130 used in the setsockopt() or getsockopt() call. The option applies to 2131 each association's window size separately. The call expects an 2132 integer. 2134 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE) 2136 This socket option is applicable to the one-to-many style socket 2137 only. When set it will cause associations that are idle for more 2138 than the specified number of seconds to automatically close. An 2139 association being idle is defined as an association that has NOT sent 2140 or received user data. The special value of '0' indicates that no 2141 automatic close of any associations should be performed, this is the 2142 default value. The option expects an integer defining the number of 2143 seconds of idle time before an association is closed. 2145 An application using this option should enable receiving the 2146 association change notification. This is the only mechanism an 2147 application is informed about the closing of an association. After 2148 an association is closed, the association ID assigned to it can be 2149 reused. An application should be aware of this to avoid the possible 2150 problem of sending data to an incorrect peer end point. 2152 7.1.9 Set Peer Primary Address (SCTP_SET_PEER_PRIMARY_ADDR) 2154 Requests that the peer mark the enclosed address as the association 2155 primary. The enclosed address must be one of the association's 2156 locally bound addresses. The following structure is used to make a 2157 set primary request: 2159 struct sctp_setpeerprim { 2160 sctp_assoc_t sspp_assoc_id; 2161 struct sockaddr_storage sspp_addr; 2162 }; 2164 sspp_addr The address to set as primary 2165 sspp_assoc_id (one-to-many style socket) This is filled in by the 2166 application, and identifies the association 2167 for this request. 2169 This functionality is optional. Implementations that do not support 2170 this functionality should return EOPNOTSUPP. 2172 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR) 2174 Requests that the local SCTP stack use the enclosed peer address as 2175 the association primary. The enclosed address must be one of the 2176 association peer's addresses. The following structure is used to 2177 make a set peer primary request: 2179 struct sctp_setprim { 2180 sctp_assoc_t ssp_assoc_id; 2181 struct sockaddr_storage ssp_addr; 2182 }; 2183 ssp_addr The address to set as primary 2184 ssp_assoc_id (one-to-many style socket) This is filled in by the 2185 application, and identifies the association 2186 for this request. 2188 7.1.11 Set Adaption Layer Indicator (SCTP_ADAPTION_LAYER) 2190 Requests that the local endpoint set the specified Adaption Layer 2191 Indication parameter for all future INIT and INIT-ACK exchanges. 2193 struct sctp_setadaption { 2194 uint32_t ssb_adaption_ind; 2195 }; 2197 ssb_adaption_ind The adaption layer indicator that will be included 2198 in any outgoing Adaption Layer Indication 2199 parameter. 2201 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS) 2203 This option is a on/off flag and is passed an integer where a 2204 non-zero is on and a zero is off. If enabled no SCTP message 2205 fragmentation will be performed. Instead if a message being sent 2206 exceeds the current PMTU size, the message will NOT be sent and 2207 instead a error will be indicated to the user. 2209 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS) 2211 Applications can enable or disable heartbeats for any peer address of 2212 an association, modify an address's heartbeat interval, force a 2213 heartbeat to be sent immediately, and adjust the address's maximum 2214 number of retransmissions sent before an address is considered 2215 unreachable. The following structure is used to access and modify an 2216 address's parameters: 2218 struct sctp_paddrparams { 2219 sctp_assoc_t spp_assoc_id; 2220 struct sockaddr_storage spp_address; 2221 uint32_t spp_hbinterval; 2222 uint16_t spp_pathmaxrxt; 2223 }; 2225 spp_assoc_id - (one-to-many style socket) This is filled in the application, 2226 and identifies the association for this query. 2227 spp_address - This specifies which address is of interest. 2228 spp_hbinterval - This contains the value of the heartbeat interval, 2229 in milliseconds. A value of 0, when modifying the 2230 parameter, specifies that the heartbeat on this 2231 address should be disabled. A value of UINT32_MAX 2232 (4294967295), when modifying the parameter, 2233 specifies that a heartbeat should be sent 2234 immediately to the peer address, and the current 2235 interval should remain unchanged. 2236 spp_pathmaxrxt - This contains the maximum number of 2237 retransmissions before this address shall be 2238 considered unreachable. If a value of zero 2239 is present in this field then no changes are to 2240 be made to this parameter. 2242 To read or modify these parameters, the application should call 2243 sctp_opt_info() with the SCTP_PEER_ADDR_PARAMS option. 2245 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM) 2247 Applications that wish to use the sendto() system call may wish to 2248 specify a default set of parameters that would normally be supplied 2249 through the inclusion of ancillary data. This socket option allows 2250 such an application to set the default sctp_sndrcvinfo structure. 2251 The application that wishes to use this socket option simply passes 2252 in to this call the sctp_sndrcvinfo structure defined in Section 2253 5.2.2) The input parameters accepted by this call include 2254 sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context, 2255 sinfo_timetolive. The user must set the sinfo_assoc_id field to 2256 identify the association to affect if the caller is using the 2257 one-to-many style. 2259 7.1.15 Set notification and ancillary events (SCTP_EVENTS) 2261 This socket option is used to specify various notifications and 2262 ancillary data the user wishes to receive. Please see Section 7.3) 2263 for a full description of this option and its usage. 2265 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR) 2267 This socket option is a boolean flag which turns on or off mapped V4 2268 addresses. If this option is turned on and the socket is type 2269 PF_INET6, then IPv4 addresses will be mapped to V6 representation. 2270 If this option is turned off, then no mapping will be done of V4 2271 addresses and a user will receive both PF_INET6 and PF_INET type 2272 addresses on the socket. 2274 By default this option is turned on and expects an integer to be 2275 passed where non-zero turns on the option and zero turns off the 2276 option. 2278 7.1.17 Set the maximum fragmentation size (SCTP_MAXSEG) 2280 This socket option specifies the maximum size to put in any outgoing 2281 SCTP DATA chunk. If a message is larger than this size it will be 2282 fragmented by SCTP into the specified size. Note that the underlying 2283 SCTP implementation may fragment into smaller sized chunks when the 2284 PMTU of the underlying association is smaller than the value set by 2285 the user. The option expects an integer. 2287 The default value for this option is '0' which indicates the user is 2288 NOT limiting fragmentation and only the PMTU will effect SCTP's 2289 choice of DATA chunk size. 2291 7.2 Read-Only Options 2293 7.2.1 Association Status (SCTP_STATUS) 2295 Applications can retrieve current status information about an 2296 association, including association state, peer receiver window size, 2297 number of unacked data chunks, and number of data chunks pending 2298 receipt. This information is read-only. The following structure is 2299 used to access this information: 2301 struct sctp_status { 2302 sctp_assoc_t sstat_assoc_id; 2303 int32_t sstat_state; 2304 uint32_t sstat_rwnd; 2305 uint16_t sstat_unackdata; 2306 uint16_t sstat_penddata; 2307 uint16_t sstat_instrms; 2308 uint16_t sstat_outstrms; 2309 uint32_t sstat_fragmentation_point; 2310 struct sctp_paddrinfo sstat_primary; 2311 }; 2313 sstat_state - This contains the association's current state one 2314 of the following values: 2316 SCTP_CLOSED 2317 SCTP_BOUND 2318 SCTP_LISTEN 2319 SCTP_COOKIE_WAIT 2320 SCTP_COOKIE_ECHOED 2321 SCTP_ESTABLISHED 2322 SCTP_SHUTDOWN_PENDING 2323 SCTP_SHUTDOWN_SENT 2324 SCTP_SHUTDOWN_RECEIVED 2325 SCTP_SHUTDOWN_ACK_SENT 2327 sstat_rwnd - This contains the association peer's current 2328 receiver window size. 2329 sstat_unackdata - This is the number of unacked data chunks. 2330 sstat_penddata - This is the number of data chunks pending receipt. 2331 sstat_primary - This is information on the current primary peer 2332 address. 2333 sstat_assoc_id - (one-to-many style socket) This holds the an identifier for the 2334 association. All notifications for a given association 2335 have the same association identifier. 2337 sstat_instrms - The number of streams that the peer will 2338 be using inbound. 2340 sstat_outstrms - The number of streams that the endpoint is 2341 allowed to use outbound. 2343 sstat_fragmentation_point - The size at which SCTP fragmentation 2344 will occur. 2346 To access these status values, the application calls getsockopt() 2347 with the option name SCTP_STATUS. The sstat_assoc_id parameter is 2348 ignored for one-to-one style socket. 2350 7.2.2 Peer Address Information (SCTP_GET_PEER_ADDR_INFO) 2352 Applications can retrieve information about a specific peer address 2353 of an association, including its reachability state, congestion 2354 window, and retransmission timer values. This information is 2355 read-only. The following structure is used to access this 2356 information: 2358 struct sctp_paddrinfo { 2359 sctp_assoc_t spinfo_assoc_id; 2360 struct sockaddr_storage spinfo_address; 2361 int32_t spinfo_state; 2362 uint32_t spinfo_cwnd; 2363 uint32_t spinfo_srtt; 2364 uint32_t spinfo_rto; 2365 uint32_t spinfo_mtu; 2366 }; 2368 spinfo_address - This is filled in the application, and contains 2369 the peer address of interest. 2371 On return from getsockopt(): 2373 spinfo_state - This contains the peer addresses's state (either 2374 SCTP_ACTIVE or SCTP_INACTIVE and possibly the modifer 2375 SCTP_UNCONFIRMED) 2377 spinfo_cwnd - This contains the peer addresses's current congestion 2378 window. 2379 spinfo_srtt - This contains the peer addresses's current smoothed 2380 round-trip time calculation in milliseconds. 2381 spinfo_rto - This contains the peer addresses's current 2382 retransmission timeout value in milliseconds. 2383 spinfo_mtu - The current P-MTU of this address. 2384 spinfo_assoc_id - (one-to-many style socket) This is filled in the application, 2385 and identifies the association for this query. 2387 To retrieve this information, use sctp_opt_info() with the 2388 SCTP_GET_PEER_ADDR_INFO options. 2390 7.3 Ancillary Data and Notification Interest Options 2392 Applications can receive per-message ancillary information and 2393 notifications of certain SCTP events with recvmsg(). 2395 The following optional information is available to the application: 2397 1. SCTP_SNDRCV (sctp_data_io_event): Per-message information (i.e. 2398 stream number, TSN, SSN, etc. described in Section 5.2.2) 2399 2. SCTP_ASSOC_CHANGE (sctp_association_event): (described in Section 2400 5.3.1.1) 2401 3. SCTP_PEER_ADDR_CHANGE (sctp_address_event): (described in Section 2402 5.3.1.2) 2403 4. SCTP_SEND_FAILED (sctp_send_failure_event): (described in Section 2404 5.3.1.4) 2405 5. SCTP_REMOTE_ERROR (sctp_peer_error_event): (described in Section 2406 5.3.1.3) 2407 6. SCTP_SHUTDOWN_EVENT (sctp_shtudown_event): (described in Section 2408 5.3.1.5) 2409 7. SCTP_PARTIAL_DELIVERY_EVENT (sctp_partial_delivery_event): 2410 (described in Section 5.3.1.7) 2411 8. SCTP_ADAPTION_INDICATION (sctp_adaption_layer_event): (described 2412 in Section 5.3.1.6) 2414 To receive any ancillary data or notifications, first the application 2415 registers it's interest by calling the SCTP_EVENTS setsockopt() with 2416 the following structure. 2418 struct sctp_event_subscribe{ 2419 uint8_t sctp_data_io_event; 2420 uint8_t sctp_association_event; 2421 uint8_t sctp_address_event; 2422 uint8_t sctp_send_failure_event; 2423 uint8_t sctp_peer_error_event; 2424 uint8_t sctp_shutdown_event; 2425 uint8_t sctp_partial_delivery_event; 2426 uint8_t sctp_adaption_layer_event; 2427 }; 2429 sctp_data_io_event - Setting this flag to 1 will cause the reception 2430 of SCTP_SNDRCV information on a per message basis. The application 2431 will need to use the recvmsg() interface so that it can receive the 2432 event information contained in the msg_control field. Please see 2433 Section 5.2 for further details. Setting the flag to 0 will disable 2434 reception of the message control information. 2436 sctp_association_event - Setting this flag to 1 will enable the 2437 reception of association event notifications. Setting the flag to 0 2438 will disable association event notifications. For more information 2439 on event notifications please see Section 5.3. 2441 sctp_address_event - Setting this flag to 1 will enable the reception 2442 of address event notifications. Setting the flag to 0 will disable 2443 address event notifications. For more information on event 2444 notifications please see Section 5.3. 2446 sctp_send_failure_event - Setting this flag to 1 will enable the 2447 reception of send failure event notifications. Setting the flag to 0 2448 will disable send failure event notifications. For more information 2449 on event notifications please see Section 5.3. 2451 sctp_peer_error_event - Setting this flag to 1 will enable the 2452 reception of peer error event notifications. Setting the flag to 0 2453 will disable peer error event notifications. For more information on 2454 event notifications please see Section 5.3. 2456 sctp_shutdown_event - Setting this flag to 1 will enable the 2457 reception of shutdown event notifications. Setting the flag to 0 2458 will disable shutdown event notifications. For more information on 2459 event notifications please see Section 5.3. 2461 sctp_partial_delivery_event - Setting this flag to 1 will enable the 2462 reception of partial delivery notifications. Setting the flag to 0 2463 will disable partial delivery event notifications. For more 2464 information on event notifications please see Section 5.3. 2466 sctp_adaption_layer_event - Setting this flag to 1 will enable the 2467 reception of adaption layer notifications. Setting the flag to 0 2468 will disable adaption layer event notifications. For more 2469 information on event notifications please see Section 5.3. 2471 An example where an application would like to receive data io events 2472 and association events but no others would be as follows: 2474 { 2475 struct sctp_event_subscribe event; 2477 memset(&event,0,sizeof(event)); 2479 event.sctp_data_io_event = 1; 2480 event.sctp_association_event = 1; 2482 setsockopt(fd, IPPROTO_SCTP, SCTP_EVENT, &event, sizeof(event)); 2483 } 2485 Note that for one-to-many style SCTP sockets, the caller of recvmsg() 2486 receives ancillary data and notifications for ALL associations bound 2487 to the file descriptor. For one-to-one style SCTP sockets, the 2488 caller receives ancillary data and notifications for only the single 2489 association bound to the file descriptor. 2491 By default both the one-to-one style and one-to-many style socket has 2492 all options off. 2494 8. New Interfaces 2496 Depending on the system, the following interface can be implemented 2497 as a system call or library function. 2499 8.1 sctp_bindx() 2501 The syntax of sctp_bindx() is, 2503 int sctp_bindx(int sd, struct sockaddr *addrs, int addrcnt, 2504 int flags); 2506 If sd is an IPv4 socket, the addresses passed must be IPv4 addresses. 2507 If the sd is an IPv6 socket, the addresses passed can either be IPv4 2508 or IPv6 addresses. 2510 A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see 2511 Section 3.1.2 for this usage. 2513 addrs is a pointer to an array of one or more socket addresses. Each 2514 address is contained in its appropriate structure. For an IPv6 2515 socket, an array of sockaddr_in6 would be returned. For a IPv4 2516 socket, an array of sockaddr_in would be returned. The caller 2517 specifies the number of addresses in the array with addrcnt. Note 2518 that the wildcard addresses cannot be used with this function, doing 2519 so will result in an error. 2521 On success, sctp_bindx() returns 0. On failure, sctp_bindx() returns 2522 -1, and sets errno to the appropriate error code. 2524 For SCTP, the port given in each socket address must be the same, or 2525 sctp_bindx() will fail, setting errno to EINVAL. 2527 The flags parameter is formed from the bitwise OR of zero or more of 2528 the following currently defined flags: 2530 SCTP_BINDX_ADD_ADDR 2532 SCTP_BINDX_REM_ADDR 2534 SCTP_BINDX_ADD_ADDR directs SCTP to add the given addresses to the 2535 association, and SCTP_BINDX_REM_ADDR directs SCTP to remove the given 2536 addresses from the association. The two flags are mutually 2537 exclusive; if both are given, sctp_bindx() will fail with EINVAL. A 2538 caller may not remove all addresses from an association; sctp_bindx() 2539 will reject such an attempt with EINVAL. 2541 An application can use sctp_bindx(SCTP_BINDX_ADD_ADDR) to associate 2542 additional addresses with an endpoint after calling bind(). Or use 2543 sctp_bindx(SCTP_BINDX_REM_ADDR) to remove some addresses a listening 2544 socket is associated with so that no new association accepted will be 2545 associated with those addresses. If the endpoint supports dynamic 2546 address a SCTP_BINDX_REM_ADDR or SCTP_BINDX_ADD_ADDR may cause a 2547 endpoint to send the appropriate message to the peer to change the 2548 peers address lists. 2550 Adding and removing addresses from a connected association is 2551 optional functionality. Implementations that do not support this 2552 functionality should return EOPNOTSUPP. 2554 8.2 Branched-off Association 2556 After an association is established on a one-to-many style socket, 2557 the application may wish to branch off the association into a 2558 separate socket/file descriptor. 2560 This is particularly desirable when, for instance, the application 2561 wishes to have a number of sporadic message senders/receivers remain 2562 under the original one-to-many style socket but branch off those 2563 associations carrying high volume data traffic into their own 2564 separate socket descriptors. 2566 The application uses sctp_peeloff() call to branch off an association 2567 into a separate socket (Note the semantics are somewhat changed from 2568 the traditional one-to-one style accept() call). Note that the new 2569 socket is a one-to-one style socket. Thus it will be confined to 2570 operations allowed for a one-to-one style socket. 2572 The syntax is: 2574 new_sd = sctp_peeloff(int sd, sctp_assoc_t assoc_id); 2576 the new socket descriptor representing the branched-off 2577 association. 2578 the original one-to-many style socket descriptor returned from the 2579 socket() system call (see Section 3.1.1). 2580 the specified identifier of the association that is to be branched 2581 off to a separate file descriptor (Note, in a traditional 2582 one-to-one style accept() call, this would be an out parameter, 2583 but for the one-to-many style call, this is an in parameter). 2585 8.3 sctp_getpaddrs() 2587 sctp_getpaddrs() returns all peer addresses in an association. The 2588 syntax is, 2589 int sctp_getpaddrs(int sd, sctp_assoc_t id, 2590 struct sockaddr **addrs); 2592 On return, addrs will point to an array dynamically allocated 2593 sockaddr structures of the appropriate type for the socket type. The 2594 caller should use sctp_freepaddrs() to free the memory. Note that 2595 the in/out parameter addrs must not be NULL. 2597 If sd is an IPv4 socket, the addresses returned will be all IPv4 2598 addresses. If sd is an IPv6 socket, the addresses returned can be a 2599 mix of IPv4 or IPv6 addresses. 2601 For one-to-many style sockets, id specifies the association to query. 2602 For one-to-one style sockets, id is ignored. 2604 On success, sctp_getpaddrs() returns the number of peer addresses in 2605 the association. If there is no association on this socket, 2606 sctp_getpaddrs() returns 0, and the value of *addrs is undefined. If 2607 an error occurs, sctp_getpaddrs() returns -1, and the value of *addrs 2608 is undefined. 2610 8.4 sctp_freepaddrs() 2612 sctp_freepaddrs() frees all resources allocated by 2613 sctp_getpaddrs(). Its syntax is, 2615 void sctp_freepaddrs(struct sockaddr *addrs); 2617 addrs is the array of peer addresses returned by sctp_getpaddrs(). 2619 8.5 sctp_getladdrs() 2621 sctp_getladdrs() returns all locally bound address(es) on a socket. 2622 The syntax is, 2624 int sctp_getladdrs(int sd, sctp_assoc_t id, 2625 struct sockaddr **ss); 2627 On return, addrs will point to a dynamically allocated array of 2628 sockaddr structures of the appropriate type for the socket type. The 2629 caller should use sctp_freeladdrs() to free the memory. Note that 2630 the in/out parameter addrs must not be NULL. 2632 If sd is an IPv4 socket, the addresses returned will be all IPv4 2633 addresses. If sd is an IPv6 socket, the addresses returned can be a 2634 mix of IPv4 or IPv6 addresses. 2636 For one-to-many style sockets, id specifies the association to query. 2637 For one-to-one style sockets, id is ignored. 2639 If the id field is set to the value '0' then the locally bound 2640 addresses are returned without regard to any particular association. 2642 On success, sctp_getladdrs() returns the number of local addresses 2643 bound to the socket. If the socket is unbound, sctp_getladdrs() 2644 returns 0, and the value of *addrs is undefined. If an error occurs, 2645 sctp_getladdrs() returns -1, and the value of *addrs is undefined. 2647 8.6 sctp_freeladdrs() 2649 sctp_freeladdrs() frees all resources allocated by 2650 sctp_getladdrs(). Its syntax is, 2652 void sctp_freeladdrs(struct sockaddr *addrs); 2654 addrs is the array of peer addresses returned by sctp_getladdrs(). 2656 8.7 sctp_sendmsg() 2658 An implementation may provide a library function (or possibly system 2659 call) to assist the user with the advanced features of SCTP. 2661 sctp_sendmsg(). Its syntax is, 2663 ssize_t sctp_sendmsg(int sd, 2664 const void *msg, 2665 size_t len, 2666 const struct sockaddr *to, 2667 socklen_t tolen, 2668 uint32_t ppid, 2669 uint32_t flags, 2670 uint16_t stream_no, 2671 uint32_t timetolive, 2672 uint32_t context) 2674 sd - is the socket descriptor 2675 msg - is the message to be sent. 2676 len - is the length of the message. 2677 to - is the destination address of the message. 2678 tolen - is the length of the destination address. 2679 ppid - is the same as sinfo_ppid (see section 5.2.2) 2680 flags - is the same as sinfo_flags (see section 5.2.2) 2681 stream_no - is the same as sinfo_stream (see section 5.2.2) 2682 timetolive - is the same as sinfo_timetolive (see section 5.2.2) 2683 context - is the same as sinfo_context (see section 5.2.2) 2685 8.8 sctp_recvmsg() 2687 An implementation may provide a library function (or possibly system 2688 call) to assist the user with the advanced features of SCTP. Note 2689 that in order for the sctp_sndrcvinfo structure to be filled in by 2690 sctp_recvmsg() the caller must enable the sctp_data_io_events with 2691 the SCTP_EVENTS option. 2693 sctp_recvmsg(). Its syntax is, 2695 ssize_t sctp_recvmsg(int sd, 2696 void *msg, 2697 size_t len, 2698 struct sockaddr *from, 2699 socklen_t *fromlen 2700 struct sctp_sndrcvinfo *sinfo 2701 int *msg_flags) 2703 sd - is the socket descriptor 2704 msg - is a message buffer to be filled. 2705 len - is the length of the message buffer. 2706 from - is a pointer to a address to be filled with 2707 the sender of this messages address. 2708 fromlen - is the from length. 2709 sinfo - A pointer to a sctp_sndrcvinfo structure 2710 to be filled upon receipt of the message. 2711 msg_flags - A pointer to a integer to be filled with 2712 any message flags (e.g. MSG_NOTIFICATION). 2714 8.9 sctp_connectx() 2716 An implementation may provide a library function (or possibly system 2717 call) to assist the user with associating to an endpoint that is 2718 multi-homed. Much like sctp_bindx() this call allows a caller to 2719 specify multiple addresses at which a peer can be reached. The way 2720 the SCTP stack uses the list of addresses to set up the association 2721 is implementation dependant. This function only specifies that the 2722 stack will try to make use of all the addresses in the list when 2723 needed. 2725 Note that the list of addresses passed in is only used for setting up 2726 the association. It does not necessarily equal the set of addresses 2727 the peer uses for the resulting association. If the caller wants to 2728 find out the set of peer addresses, it must use sctp_getpaddrs() to 2729 retrieve them after the association has been set up. 2731 sctp_connectx(). Its syntax is, 2733 int sctp_connectx(int sd, 2734 struct sockaddr *addrs, 2735 int addrcnt) 2737 sd - is the socket descriptor 2738 addrs - is an array of addresses. 2740 addrcnt - is the number of addresses in the array. 2742 8.10 sctp_send() 2744 An implementation may provide another alternative function or system 2745 call to assist an application with the sending of data without the 2746 use of the CMSG header structures. The function takes the following 2747 form: 2749 sctp_send(). Its syntax is, 2751 int sctp_send(int sd, 2752 const void *msg, 2753 size_t len, 2754 const struct sctp_sndrcvinfo *sinfo, 2755 int flags); 2757 sd - is the socket descriptor 2758 msg - The message to be sent 2759 len - The length of the message 2760 sinfo - A pointer to a sctp_sndrcvinfo struture used 2761 as described in 5.2.2 for a sendmsg call. 2762 flags - is used in the same format as the sendmsg call 2763 flags (e.g. MSG_DONTROUTE). 2765 This function call may also be used to terminate an association using 2766 an association identification by setting the sinfo.sinfo_flags to 2767 MSG_EOF and the sinfo.sinf_associd to the association that needs to 2768 be terminated. In such a case the len of the message would be zero. 2770 8.11 sctp_sendx() 2772 An implementation may provide another alternative function or system 2773 call to assist an application with the sending of data without the 2774 use of the CMSG header structures that also gives a list of 2775 addresses. The list of addresses is provided for implicit 2776 association setup. In such a case the list of addresses serves the 2777 same purpose as the addresses given in sctp_connectx (see Section 2778 8.9). 2780 sctp_sendx(). Its syntax is, 2782 int sctp_sendx(int sd, 2783 const void *msg, 2784 size_t len, 2785 struct sockaddr *addrs, 2786 int addrcnt, 2787 const struct sctp_sndrcvinfo *sinfo, 2788 int flags); 2790 sd - is the socket descriptor 2791 msg - The message to be sent 2792 len - The length of the message 2793 addrs - is an array of addresses. 2794 addrcnt - is the number of addresses in the array. 2795 sinfo - A pointer to a sctp_sndrcvinfo struture used 2796 as described in 5.2.2 for a sendmsg call. 2797 flags - is used in the same format as the sendmsg call 2798 flags (e.g. MSG_DONTROUTE). 2800 This function call may also be used to terminate an association using 2801 an association identification by setting the sinfo.sinfo_flags to 2802 MSG_EOF and the sinfo.sinf_associd to the association that needs to 2803 be terminated. In such a case the len of the message would be zero. 2805 9. Preprocessor Constants 2807 For application portability it is desireable to define pre-processor 2808 constants for determination if sctp is present and supports various 2809 features. The following pre-processor constants should be defined in 2810 a include file, sctp.h. 2811 HAVE_SCTP - If this constant is defined to 1, then an implementation 2812 of SCTP is available. 2813 HAVE_KERNEL_SCTP - If this constant is defined to 1, then a kernel 2814 SCTP implementation is available through the sockets interface. 2815 HAVE_SCTP_PRSCTP - If this constant is defined to 1, then the SCTP 2816 implementation supports the partial reliablility extension to 2817 SCTP. 2818 HAVE_SCTP_ADDIP - If this constant is defined to 1, then the SCTP 2819 implementation supports the dynamic address extension to SCTP. 2820 HAVE_SCTP_CANSET_PRIMARY - If this constant is defined to 1, then the 2821 SCTP implementation supports the ability to request setting of the 2822 remote primary address. 2823 HAVE_SCTP_SAT_NETWORK_CAPABILITY - If this constant is defined to 1, 2824 then the SCTP implementation supports the satellite network 2825 extension to SCTP. 2826 HAVE_SCTP_MULTIBUF - If this constant is defined to 1, then the SCTP 2827 implementation dedicates separate buffer space to each association 2828 on a one-to-many socket. If this constant is defined to 0, then 2829 the implementation provides a single block of shared buffer space 2830 for a one-to-many socket. 2831 HAVE_SCTP_NOCONNECT - If this constant is defined to 1, then the SCTP 2832 implementation supports initiating an association on a one-to-one 2833 style socket without the use of connect(), as outlined in Section 2834 4.1.5. 2836 10. Security Considerations 2838 Many TCP and UDP implementations reserve port numbers below 1024 for 2839 privileged users. If the target platform supports privileged users, 2840 the SCTP implementation SHOULD restrict the ability to call bind() or 2841 sctp_bindx() on these port numbers to privileged users. 2843 Similarly unpriviledged users should not be able to set protocol 2844 parameters which could result in the congestion control algorithm 2845 being more aggressive than permitted on the public Internet. These 2846 parameters are: 2848 struct sctp_rtoinfo 2850 If an unprivileged user inherits a one-to-many style socket with open 2851 associations on a privileged port, it MAY be permitted to accept new 2852 associations, but it SHOULD NOT be permitted to open new 2853 associations. This could be relevant for the r* family of protocols. 2855 11. Acknowledgments 2857 Special acknowledgment is givne to Ken Fujita who helped extensively 2858 in the early formation of this document. 2860 The authors also wish to thank Kavitha Baratakke, Mike Bartlett, Jon 2861 Berger, Scott Kimble, Renee Revis, and many others on the TSVWG 2862 mailing list for contributing valuable comments. 2864 A special thanks to Phillip Conrad, for his suggested text, quick and 2865 constructive insights, and most of all his persistent fighting to 2866 keep the interface to SCTP usable for the application programmer. 2868 12 References 2870 [1] Postel, J., "Transmission Control Protocol", STD 7, RFC 793, 2871 September 1981. 2873 [2] Postel, J., "User Datagram Protocol", STD 6, RFC 768, August 2874 1980. 2876 [3] Braden, B., "T/TCP -- TCP Extensions for Transactions Functional 2877 Specification", RFC 1644, July 1994. 2879 [4] Bradner, S., "The Internet Standards Process -- Revision 3", BCP 2880 9, RFC 2026, October 1996. 2882 [5] Bradner, S., "Key words for use in RFCs to Indicate Requirement 2883 Levels", BCP 14, RFC 2119, March 1997. 2885 [6] Stevens, W. and M. Thomas, "Advanced Sockets API for IPv6", RFC 2886 2292, February 1998. 2888 [7] Gilligan, R., Thomson, S., Bound, J. and W. Stevens, "Basic 2889 Socket Interface Extensions for IPv6", RFC 2553, March 1999. 2891 [8] Stewart, R., Xie, Q., Morneault, K., Sharp, C., Schwarzbauer, 2892 H., Taylor, T., Rytina, I., Kalla, M., Zhang, L. and V. Paxson, 2893 "Stream Control Transmission Protocol", RFC 2960, October 2000. 2895 Authors' Addresses 2897 Randall R. Stewart 2898 Cisco Systems, Inc. 2899 4875 Forest Drive 2900 Suite 200 2901 Columbia, SC 29206 2902 USA 2904 Phone: 2905 EMail: rrs@cisco.com 2907 Qiaobing Xie 2908 Motorola, Inc. 2909 1501 W. Shure Drive, #2309 2910 Arlington Heights, IL 60004 2911 USA 2913 Phone: 2914 EMail: qxie1@email.mot.com 2916 La Monte H.P. Yarroll 2917 TimeSys Corp 2918 925 Liberty Ave. 2919 Pittsburgh, PA 15222 2920 USA 2922 Phone: 2923 EMail: piggy@acm.org 2925 Jonathan Wood 2926 DoCoMo USA Labs 2927 181 Metro Drive, Suite 300 2928 San Jose, CA 95110 2929 USA 2931 Phone: 2932 EMail: jonwood@speakeasy.net 2933 Kacheong Poon 2934 Sun Microsystems, Inc. 2935 4150 Network Circle 2936 Santa Clara, CA 95054 2937 USA 2939 Phone: 2940 EMail: kacheong.poon@sun.com 2942 Michael Tuexen 2943 Univ. of Applied Sciences Muenster 2944 Stegerwaldstr. 39 2945 48565 Steinfurt 2946 Germany 2948 EMail: tuexen@fh-muenster.de 2950 Appendix A. one-to-one style Code Example 2952 The following code is a simple implementation of an echo server over 2953 SCTP. The example shows how to use some features of one-to-one style 2954 IPv4 SCTP sockets, including: 2956 o Opening, binding, and listening for new associations on a socket; 2957 o Enabling ancillary data 2958 o Enabling notifications 2959 o Using ancillary data with sendmsg() and recvmsg() 2960 o Using MSG_EOR to determine if an entire message has been read 2961 o Handling notifications 2963 #include 2964 #include 2965 #include 2966 #include 2967 #include 2968 #include 2969 #include 2970 #include 2971 #include 2973 #define BUFLEN 100 2975 static void 2976 handle_event(void *buf) 2977 { 2978 struct sctp_assoc_change *sac; 2979 struct sctp_send_failed *ssf; 2980 struct sctp_paddr_change *spc; 2981 struct sctp_remote_error *sre; 2982 union sctp_notification *snp; 2983 char addrbuf[INET6_ADDRSTRLEN]; 2984 const char *ap; 2985 struct sockaddr_in *sin; 2986 struct sockaddr_in6 *sin6; 2988 snp = buf; 2990 switch (snp->sn_header.sn_type) { 2991 case SCTP_ASSOC_CHANGE: 2992 sac = &snp->sn_assoc_change; 2993 printf("^^^ assoc_change: state=%hu, error=%hu, instr=%hu " 2994 "outstr=%hu\n", sac->sac_state, sac->sac_error, 2995 sac->sac_inbound_streams, sac->sac_outbound_streams); 2996 break; 2997 case SCTP_SEND_FAILED: 2999 ssf = &snp->sn_send_failed; 3000 printf("^^^ sendfailed: len=%hu err=%d\n", ssf->ssf_length, 3001 ssf->ssf_error); 3002 break; 3004 case SCTP_PEER_ADDR_CHANGE: 3005 spc = &snp->sn_paddr_change; 3006 if (spc->spc_aaddr.ss_family == AF_INET) { 3007 sin = (struct sockaddr_in *)&spc->spc_aaddr; 3008 ap = inet_ntop(AF_INET, &sin->sin_addr, 3009 addrbuf, INET6_ADDRSTRLEN); 3010 } else { 3011 sin6 = (struct sockaddr_in6 *)&spc->spc_aaddr; 3012 ap = inet_ntop(AF_INET6, &sin6->sin6_addr, 3013 addrbuf, INET6_ADDRSTRLEN); 3014 } 3015 printf("^^^ intf_change: %s state=%d, error=%d\n", ap, 3016 spc->spc_state, spc->spc_error); 3017 break; 3018 case SCTP_REMOTE_ERROR: 3019 sre = &snp->sn_remote_error; 3020 printf("^^^ remote_error: err=%hu len=%hu\n", 3021 ntohs(sre->sre_error), ntohs(sre->sre_length)); 3022 break; 3023 case SCTP_SHUTDOWN_EVENT: 3024 printf("^^^ shutdown event\n"); 3025 break; 3026 default: 3027 printf("unknown type: %hu\n", snp->sn_header.sn_type); 3028 break; 3029 } 3030 } 3032 static void * 3033 mysctp_recvmsg(int fd, struct msghdr *msg, void *buf, size_t *buflen, 3034 ssize_t *nrp, size_t cmsglen) 3035 { 3036 ssize_t nr = 0, nnr = 0; 3037 struct iovec iov[1]; 3039 *nrp = 0; 3040 iov->iov_base = buf; 3041 iov->iov_len = *buflen; 3042 msg->msg_iov = iov; 3043 msg->msg_iovlen = 1; 3045 for (;;) { 3047 #ifndef MSG_XPG4_2 3048 #define MSG_XPG4_2 0 3049 #endif 3050 msg->msg_flags = MSG_XPG4_2; 3051 msg->msg_controllen = cmsglen; 3053 nnr = recvmsg(fd, msg, 0); 3054 if (nnr <= 0) { 3055 /* EOF or error */ 3056 *nrp = nr; 3057 return (NULL); 3058 } 3059 nr += nnr; 3061 if ((msg->msg_flags & MSG_EOR) != 0) { 3062 *nrp = nr; 3063 return (buf); 3064 } 3066 /* Realloc the buffer? */ 3067 if (*buflen == (size_t)nr) { 3068 buf = realloc(buf, *buflen * 2); 3069 if (buf == 0) { 3070 fprintf(stderr, "out of memory\n"); 3071 exit(1); 3072 } 3073 *buflen *= 2; 3074 } 3076 /* Set the next read offset */ 3077 iov->iov_base = (char *)buf + nr; 3078 iov->iov_len = *buflen - nr; 3080 } 3081 } 3083 static void 3084 echo(int fd, int socketModeone_to_many) 3085 { 3086 ssize_t nr; 3087 struct sctp_sndrcvinfo *sri; 3088 struct msghdr msg[1]; 3089 struct cmsghdr *cmsg; 3090 char cbuf[sizeof (*cmsg) + sizeof (*sri)]; 3091 char *buf; 3092 size_t buflen; 3093 struct iovec iov[1]; 3094 size_t cmsglen = sizeof (*cmsg) + sizeof (*sri); 3095 /* Allocate the initial data buffer */ 3096 buflen = BUFLEN; 3097 if (!(buf = malloc(BUFLEN))) { 3098 fprintf(stderr, "out of memory\n"); 3099 exit(1); 3100 } 3102 /* Set up the msghdr structure for receiving */ 3103 memset(msg, 0, sizeof (*msg)); 3104 msg->msg_control = cbuf; 3105 msg->msg_controllen = cmsglen; 3106 msg->msg_flags = 0; 3107 cmsg = (struct cmsghdr *)cbuf; 3108 sri = (struct sctp_sndrcvinfo *)(cmsg + 1); 3110 /* Wait for something to echo */ 3111 while (buf = mysctp_recvmsg(fd, msg, buf, &buflen, &nr, cmsglen)) { 3113 /* Intercept notifications here */ 3114 if (msg->msg_flags & MSG_NOTIFICATION) { 3115 handle_event(buf); 3116 continue; 3117 } 3119 iov->iov_base = buf; 3120 iov->iov_len = nr; 3121 msg->msg_iov = iov; 3122 msg->msg_iovlen = 1; 3124 printf("got %u bytes on stream %hu:\n", nr, 3125 sri->sinfo_stream); 3126 write(0, buf, nr); 3128 /* Echo it back */ 3129 msg->msg_flags = MSG_XPG4_2; 3130 if (sendmsg(fd, msg, 0) < 0) { 3131 perror("sendmsg"); 3132 exit(1); 3133 } 3134 } 3136 if (nr < 0) { 3137 perror("recvmsg"); 3138 } 3139 if(socketModeone_to_many == 0) 3140 close(fd); 3141 } 3142 int main() 3143 { 3144 struct sctp_event_subscribe event; 3145 int lfd, cfd; 3146 int onoff = 1; 3147 struct sockaddr_in sin[1]; 3149 if ((lfd = socket(AF_INET, SOCK_STREAM, IPPROTO_SCTP)) == -1) { 3150 perror("socket"); 3151 exit(1); 3152 } 3154 sin->sin_family = AF_INET; 3155 sin->sin_port = htons(7); 3156 sin->sin_addr.s_addr = INADDR_ANY; 3157 if (bind(lfd, (struct sockaddr *)sin, sizeof (*sin)) == -1) { 3158 perror("bind"); 3159 exit(1); 3160 } 3162 if (listen(lfd, 1) == -1) { 3163 perror("listen"); 3164 exit(1); 3165 } 3167 /* Wait for new associations */ 3168 for (;;) { 3169 if ((cfd = accept(lfd, NULL, 0)) == -1) { 3170 perror("accept"); 3171 exit(1); 3172 } 3174 /* Enable all events */ 3175 event.sctp_data_io_event = 1; 3176 event.sctp_association_event = 1; 3177 event.sctp_address_event = 1; 3178 event.sctp_send_failure_event = 1; 3179 event.sctp_peer_error_event = 1; 3180 event.sctp_shutdown_event = 1; 3181 event.sctp_partial_delivery_event = 1; 3182 event.sctp_adaption_layer_event = 1; 3183 if (setsockopt(cfd, IPPROTO_SCTP, 3184 SCTP_EVENTS, &event, 3185 sizeof(event)) != 0) { 3186 perror("setevent failed"); 3187 exit(1); 3188 } 3189 /* Echo back any and all data */ 3190 echo(cfd,0); 3191 } 3192 } 3194 Appendix B. one-to-many style Code Example 3196 The following code is a simple implementation of an echo server over 3197 SCTP. The example shows how to use some features of one-to-many 3198 style IPv4 SCTP sockets, including: 3200 o Opening and binding of a socket; 3201 o Enabling ancillary data 3202 o Enabling notifications 3203 o Using ancillary data with sendmsg() and recvmsg() 3204 o Using MSG_EOR to determine if an entire message has been read 3205 o Handling notifications 3207 Note most functions defined in Appendix A are reused in this example. 3209 int main() 3210 { 3211 int fd; 3212 int idleTime = 2; 3213 struct sockaddr_in sin[1]; 3214 struct sctp_event_subscribe event; 3216 if ((fd = socket(AF_INET, SOCK_SEQPACKET, IPPROTO_SCTP)) == -1) { 3217 perror("socket"); 3218 exit(1); 3219 } 3221 sin->sin_family = AF_INET; 3222 sin->sin_port = htons(7); 3223 sin->sin_addr.s_addr = INADDR_ANY; 3224 if (bind(fd, (struct sockaddr *)sin, sizeof (*sin)) == -1) { 3225 perror("bind"); 3226 exit(1); 3227 } 3229 /* Enable all notifications and events */ 3230 event.sctp_data_io_event = 1; 3231 event.sctp_association_event = 1; 3232 event.sctp_address_event = 1; 3233 event.sctp_send_failure_event = 1; 3234 event.sctp_peer_error_event = 1; 3235 event.sctp_shutdown_event = 1; 3236 event.sctp_partial_delivery_event = 1; 3237 event.sctp_adaption_layer_event = 1; 3238 if (setsockopt(fd, IPPROTO_SCTP, 3239 SCTP_EVENTS, &event, 3240 sizeof(event)) != 0) { 3241 perror("setevent failed"); 3242 exit(1); 3243 } 3244 /* Set associations to auto-close in 2 seconds of 3245 * inactivity 3246 */ 3247 if (setsockopt(fd, IPPROTO_SCTP, SCTP_AUTOCLOSE, 3248 &idleTime, 4) < 0) { 3249 perror("setsockopt SCTP_AUTOCLOSE"); 3250 exit(1); 3251 } 3253 /* Allow new associations to be accepted */ 3254 if (listen(fd, 1) < 0) { 3255 perror("listen"); 3256 exit(1); 3257 } 3259 /* Wait for new associations */ 3260 while(1){ 3261 /* Echo back any and all data */ 3262 echo(fd,1); 3263 } 3264 } 3266 Intellectual Property Statement 3268 The IETF takes no position regarding the validity or scope of any 3269 Intellectual Property Rights or other rights that might be claimed to 3270 pertain to the implementation or use of the technology described in 3271 this document or the extent to which any license under such rights 3272 might or might not be available; nor does it represent that it has 3273 made any independent effort to identify any such rights. Information 3274 on the procedures with respect to rights in RFC documents can be 3275 found in BCP 78 and BCP 79. 3277 Copies of IPR disclosures made to the IETF Secretariat and any 3278 assurances of licenses to be made available, or the result of an 3279 attempt made to obtain a general license or permission for the use of 3280 such proprietary rights by implementers or users of this 3281 specification can be obtained from the IETF on-line IPR repository at 3282 http://www.ietf.org/ipr. 3284 The IETF invites any interested party to bring to its attention any 3285 copyrights, patents or patent applications, or other proprietary 3286 rights that may cover technology that may be required to implement 3287 this standard. Please address the information to the IETF at 3288 ietf-ipr@ietf.org. 3290 Disclaimer of Validity 3292 This document and the information contained herein are provided on an 3293 "AS IS" basis and THE CONTRIBUTOR, THE ORGANIZATION HE/SHE REPRESENTS 3294 OR IS SPONSORED BY (IF ANY), THE INTERNET SOCIETY AND THE INTERNET 3295 ENGINEERING TASK FORCE DISCLAIM ALL WARRANTIES, EXPRESS OR IMPLIED, 3296 INCLUDING BUT NOT LIMITED TO ANY WARRANTY THAT THE USE OF THE 3297 INFORMATION HEREIN WILL NOT INFRINGE ANY RIGHTS OR ANY IMPLIED 3298 WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. 3300 Copyright Statement 3302 Copyright (C) The Internet Society (2004). This document is subject 3303 to the rights, licenses and restrictions contained in BCP 78, and 3304 except as set forth therein, the authors retain all their rights. 3306 Acknowledgment 3308 Funding for the RFC Editor function is currently provided by the 3309 Internet Society.