| United States-English |
|
|
|
![]() |
SCTP Programmer's Guide: HP-UX 11i v2, HP-UX 11i v3 > Chapter 2 SCTP Socket APIsDifferent Socket API Styles |
|
This section discusses the different socket API styles and the basic call flow sequence of each socket API style. Following are the different socket API styles:
The one-to-one style API is similar to the existing socket APIs for a connection-oriented protocol, such as TCP. The one-to-many style API facilitates simultaneous associations with multiple peers using one end point (that is, it associates with multiple peers using one socket file descriptor simultaneously). These socket API styles share common data structures and operations. However, each socket API style requires a different application programming style. You can use these socket APIs to implement all the SCTP features. You can also select the API style depending on the type of association you need in the application. This section addresses the following topics: The one-to-one style socket APIs are designed to enable the existing TCP applications to migrate to SCTP with minimal changes. The sequence of socket calls made by the client and server of a one-to-one style SCTP application is similar to the sequence of socket calls made by a TCP application. A one-to-one style SCTP application can control only one association using one file descriptor. A one-to-one style SCTP application uses the following system call sequence to prepare an SCTP endpoint for servicing requests:
An SCTP client uses the following system call sequence to set up an association with a server to request services:
For more information about the one-to-one style socket calls, see“Common Socket Calls”. Applications call socket()() to create a socket descriptor, to represent an SCTP endpoint. Following is the syntax for the socket()() socket API:
or
where: The first syntax of the socket()() socket API creates an endpoint that can use only IPv4 addresses, while the second syntax creates an endpoint, which can use both IPv6 and IPv4 addresses. Applications use bind()() to specify the local address with which an SCTP endpoint must associate. These addresses, associated with a socket, are eligible transport addresses for the endpoint to send and receive data. The endpoint also presents these addresses to its peers during the association initialization process. To accept new associations on the socket, the endpoint must call listen()(), after calling bind()(). For information on listen()(), see “The listen() Socket API”. Following is the syntax for the bind()() API:
where: If sd is an IPv4 socket, the address passed must be an IPv4 address. If sd is an IPv6 socket, the address passed can either be an IPv4 or an IPv6 address. Applications cannot call bind()() multiple times to associate multiple addresses to an endpoint. After the first call to bind()(), all the subsequent calls will return an error. If addr is specified as a wildcard (INADDR_ANY for an IPv4 address, or IN6ADDR_ANY_INIT or in6addr_any for an IPv6 address), the operating system associates the endpoint with an optimal address set of the available interfaces. If bind()() is not called before a sendmsg()() call that initiates a new association, the endpoint picks a transient port and chooses an address set that is equivalent to binding with a wildcard address. One of the addresses in the address set serves as the primary address for the association. Thus, when an application calls bind()() with the INADDR_ANY or the IN6ADDR_ANY_INIT wildcard address, the multihoming feature is enabled in SCTP. The completion of the bind()() process alone does not prepare the SCTP endpoint to accept inbound SCTP association requests. When a listen()() system call is performed on the socket, the SCTP endpoint promptly rejects an inbound INIT request using an ABORT flag. Applications use listen()() to prepare the SCTP endpoint for accepting inbound associations. Following is the syntax for the listen()() socket API:
where:
Applications use the accept()() call to remove an established SCTP association from the accept queue. The accept()() API returns a new socket descriptor, to represent the newly formed association. Following is the syntax for the accept()() socket API:
where: Applications use connect()() to initiate an association with a peer. Following is the syntax for the connect()() socket API:
where: By default, the newly created association has only one outbound stream. Applications must use the SCTP_INITMSG option before connecting to the server, to change the number of outbound streams. The SCTP_INITMSG option enables you to set a socket option and get a socket option, using the setsockopt()() and getsockopt()() APIs. If SCTP does not call the bind()() API before calling connect()() , the application picks a transient port and chooses an address set that is equivalent to binding with INADDR_ANY and IN6ADDR_ANY for IPv4 and IPv6 sockets, respectively. One of these addresses serves as the primary address for the association. When an application calls bind()() with the INADDR_ANY or the IN6ADDR_ANY_INIT wildcard address, the multihoming feature is enabled in SCTP. Applications use close()() to gracefully close down an association. Following is the syntax for the close()() socket API:
where:
After an application calls close()() on a socket descriptor, no further socket operations succeed on that descriptor. Applications use the shutdown()() socket API to disable send or receive operations at an endpoint. The effect of the shutdown()() call is different in SCTP and TCP. In TCP, a connection is in half-closed state even after an application calls shutdown()(). In the half-close state, an application at the sending endpoint continues to send data even if an application at the receiving endpoint has stopped receiving data. In SCTP, shutdown()() completely disables applications at both the endpoints from sending or receiving data.
Following is the syntax for the shutdown()() socket call:
In SCTP, SHUT_WR initiates an immediate and full protocol shutdown. In TCP, SHUT_WR causes TCP to enter a half-closed state. The SHUT_RD value behaves in the same way for SCTP and TCP. SCTP_WR closes the SCTP association while leaving the socket descriptor open, so that the receiving endpoint can receive data that SCTP was unable to deliver. Applications use the sendmsg()() and recvmsg()() socket APIs to transmit data to and receive data from its peer. Following is the syntax for the sendmsg()() and recvmsg()() socket APIs:
where:
Applications use the getpeername()() socket API to retrieve the primary socket address of the peer. Following is the syntax for the getpeername()() socket API:
where:
If the actual length of the address is greater than the length of the supplied sockaddr structure, SCTP truncates the stored address.
The one-to-many style APIs are designed to enable applications to control many associations from a single endpoint, using a single file descriptor. Similar to the APIs in UDP, one-to-many style APIs in SCTP enable a single socket file descriptor to connect to multiple remote endpoints. A one-to-many style socket can send and receive data without connecting to an endpoint. Unlike UDP, however, SCTP always has a valid association with the specified endpoints, because SCTP is a connection-oriented protocol. A server in the one-to-many style uses the following socket call sequence to prepare an endpoint for servicing requests:
A client in the one-to-many style API uses the following call sequence to set up an association with a server, and to request services from that server:
By default, all the associations connected to an endpoint are represented with a single socket. Each association is assigned an association ID (of type uint32_t sctp_assoc_t), so that an application can use it to differentiate between different associations. Applications can also use the sendto()() call to set up an association. However, an application cannot use the sendto()() call to send data to an established association, because the association ID cannot be specified in the sendto()() call. When an association ID is assigned to an SCTP association, SCTP must not reuse the ID until the application explicitly terminates the association. The resources belonging to that association are not freed until the association terminates. This is similar to the close()() call on a normal socket. The only difference between a normal socket and a one-to-many socket is the availability of the SCTP_AUTOCLOSE option in the one-to-many socket. When the SCTP_AUTOCLOSE option is set in a one-to-many socket, an application can reuse the association ID assigned to the association once the association terminates automatically. Applications that use this option must ensure that they do not send data to an incorrect peer endpoint, because the association ID is being reused. A server or client can branch off an existing one-to-many association into a separate socket. When an association is branched off to a separate socket, the branched off socket is completely separated from the original socket. All subsequent control and data operations on that association must be done through the new socket. For example, the close()() operation on the original socket does not terminate associations that have been branched off to a different socket. For more information on the one-to-many style socket APIs, see the subsequent subsections and “SCTP-Specific Socket APIs”. The socket()() socket API is used to create a socket descriptor that represents an SCTP endpoint. Following is the syntax for the socket()() socket API:
or
where: The first syntax of socket()() creates an endpoint that can use only IPv4 addresses, while the second syntax creates an endpoint that can use both IPv6 and IPv4 addresses. Applications use bind()() to specify the local address to which an SCTP endpoint must associate. An SCTP endpoint can be associated with multiple addresses. The sctp_bindx()() API enables applications to associate with multiple addresses. For information on sctp_bindx()(), see “The sctp_bindx() SCTP Socket API”. These addresses, associated with a socket, are eligible transport addresses for the endpoint to send and receive data. The endpoint also presents these addresses to its peers during the association initialization process. To accept new associations on the socket, applications must call listen()(), after calling bind()(). For information on listen()(), see “The listen() Socket API”. Following is the syntax for the bind()() socket API:
where: If sd is an IPv4 socket, the address passed must be an IPv4 address. If sd is an IPv6 socket, the address passed can be either an IPv4 or an IPv6 address. Applications cannot call bind()() multiple times to associate multiple addresses to an endpoint. After the first call to bind()(), all the subsequent calls will return an error. If addr is specified as a wildcard (INADDR_ANY for an IPv4 address, or IN6ADDR_ANY_INIT or in6addr_any for an IPv6 address), the operating system associates the endpoint with an optimal address set of the available interfaces. If a bind()() call is not called before a sendmsg()() call that initiates a new association, the endpoint picks a transient port and chooses an address set that is equivalent to binding with a wildcard address. One of the addresses in the address set serves as the primary address for the association. This automatically enables the multi-homing feature of SCTP. An application uses listen()() to indicate that a socket is ready to accept new associations. Following is the syntax for the listen()() socket API:
where: By default, one-to-many style sockets do not accept new associations. If applications that use one-to-many style socket APIs call the accept()() API to accept new associations, SCTP returns the EOPNOTSUPP error to the application. In the one-to-many style socket APIs, new associations are accepted automatically and notifications of the new associations are delivered using the recvmsg()() API with the SCTP_ASSOC_CHANGE event (if these notifications are enabled). A client in a one-to-many association does not call listen()(). Therefore, clients can be assured that the only associations on the socket are the associations that are actively initiated. On the contrary, a server or a peer-to-peer socket in a one-to-many connection always accepts new associations. Therefore, an application running on a server using a one-to-many style API must be prepared to handle new associations from unwanted peers. The SCTP_ASSOC_CHANGE event provides the association ID for a new association. If applications want to use the association ID as input for other socket calls, they must ensure that the SCTP_ASSOC_CHANGE event is enabled (it is enabled by default). An application uses sendmsg()() and recvmsg()() calls to transmit data to and receive data from its peer.
where:
Applications use close()() to gracefully shutdown all the associations represented by a one-to-many style socket. Following is the syntax for the close()() socket API:
where:
To gracefully shutdown a specific association that is represented by the one-to-many style socket, an application must use the sendmsg()() call and include the MSG_EOF flag. A user can optionally terminate an association non-gracefully by sending the MSG_ABORT flag, and possibly by passing a user specified abort code in the data field. Both the flags, MSG_EOF and MSG_ABORT, are passed with ancillary data in the sendmsg()() call. If sd in the close()() call is a branched-off socket that represents only one association, the shutdown is performed only on that association. An application can use the connect()() call in the one-to-many style to initiate an association without sending data. Following is the syntax for the connect()() socket API:
where:
Multiple connect()() calls can be made on the same socket to create multiple associations. In UDP, a connect()() call cannot make multiple associations on the same socket. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||