Stream Control Transmission Protocol (SCTP) Associations

 in
This second in a series of articles on the SCTP network protocol examines associations and connections.

where the field sinfo_stream of sinfo has been set.

The call to read is, conversely:

ssize_t sctp_recvmsg(int sd, 
                     void *msg, 
                     size_t len, 
                     struct sockaddr *from, 
                     socklen_t *fromlen 
                     struct sctp_sndrcvinfo *sinfo 
                     int *msg_flags)

The stream number is then available in sinfo.sinfo_stream.

The SCTP stack keeps a lot of information about each message that passes between peers. It also keeps information about the state of each association. To avoid overloading applications, most of this information is suppressed and is not passed to the application. In particular, by default, the structure sctp_sndrcvinfo is not filled in, so a reader cannot tell on which stream a read occurred! To enable this to be filled, a socket option must be called first as:


struct sctp_event_subscribe events; 
bzero(&events, sizeof(events)); 
events.sctp_data_io_event = 1; 
setsockopt(sockfd, IPPROTO_SCTP, 
           SCTP_EVENTS, &events, sizeof(events));

(More details on SCTP events will be given in the next article.) See Listings 4 (streamsend_echo_client.c) and 5 (streamsend_echo_server.c) for an example of a client and server using a specific stream for communication. .

There is no way to specify from which stream to read. This is deliberate; the intention is that when data is ready on any stream, then you read it. Otherwise, data could be blocked on a stream with no one to read it, which eventually could fill up system buffers. So, you can't restrict reading to any particular stream. But, once a read is done, you can tell which stream it has come from by using the mechanism above.

Typically, a server that reads and handles a message will have (pseudocode) that looks like this:


while (true) { 
    nread = sctp_recvmsg(..., msg, ..., &sinfo, ...) 
    if (nread <= 0) break; 
    assoc_id = sinfo.sinfo_assoc_id; 
    stream = sinfo.sinfo_stream; 
    handle_mesg(assoc_id, stream, msg, nread); 
}

This is a single-threaded read loop. It ensures that information is read, no matter what association or stream it is sent on. The application function handle_mesg() can, of course, dispatch the message to different threads if it wants. Writes, on the other hand can be sent from multiple threads if desired.

______________________

Webinar
One Click, Universal Protection: Implementing Centralized Security Policies on Linux Systems

As Linux continues to play an ever increasing role in corporate data centers and institutions, ensuring the integrity and protection of these systems must be a priority. With 60% of the world's websites and an increasing share of organization's mission-critical workloads running on Linux, failing to stop malware and other advanced threats on Linux can increasingly impact an organization's reputation and bottom line.

Learn More

Sponsored by Bit9

Webinar
Linux Backup and Recovery Webinar

Most companies incorporate backup procedures for critical data, which can be restored quickly if a loss occurs. However, fewer companies are prepared for catastrophic system failures, in which they lose all data, the entire operating system, applications, settings, patches and more, reducing their system(s) to “bare metal.” After all, before data can be restored to a system, there must be a system to restore it to.

In this one hour webinar, learn how to enhance your existing backup strategies for better disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible bare-metal recovery solution for UNIX and Linux systems.

Learn More

Sponsored by Storix