Unix Network Programming, Volume 1: The Sockets Networking API (3rd Edition)
In our examples, we have depended on the client closing the socket to shut down the association. But the client application may not always wish to close the socket. For that matter, our server may not want to keep the association open after sending the reply message. In these cases, we need to look at two alternative mechanisms for shutting down an association. For the one-to-many-style interface, two possible methods are available to the application: one is graceful , while the other is disruptive. If a server wishes to shut down an association after sending a message, we apply the MSG_EOF flag to the reply message in the sinfo_flags field of the sctp_sndrcvinfo structure. This flag forces an association to shut down after the message being sent is acknowledged . The other alternative is to apply the MSG_ABORT flag to the sinfo_flags field. This flag will force an immediate termination of the association with an ABORT chunk. An ABORT chunk is similar to a TCP RST segment, terminating any association without delay. Note that any data not yet transfered will be discarded. However, closing an SCTP session with an ABORT chunk does not have any negative side effects like preventing TCP's TIME_WAIT state; the ABORT chunk causes a "graceful" abortive close. Figure 10.11 shows the modifications needed to our echo server to initiate a graceful shutdown when the response message is sent to the peer. Figure 10.12 shows a modified client that sends an ABORT chunk before closing the socket. Figure 10.11 The server terminates an association on reply.
sctp/sctpserv03.c 25 for ( ; ; ) { 26 len = sizeof(struct sockaddr_in); 27 rd_sz = Sctp_recvmsg(sock_fd, readbuf, sizeof (readbuf), 28 (SA *) &cliaddr, &len, &sri, &msg_flags); 29 if (stream_increment) { 30 sri.sinfo_stream++; 31 if (sri.sinfo_stream >= 32 sctp_get_no_strms (sock_fd, (SA *) &cliaddr, len)) 33 sri.sinfo_stream = 0; 34 } 35 Sctp_sendmsg (sock_fd, readbuf, rd_sz, 36 (SA *) &cliaddr, len, 37 sri.sinfo_ppid, 38 (sri.sinfo_flags MSG_EOF), sri.sinfo_stream, 0, 0); Send back response, but shut down association
38 We can see that the change in this line is simply OR'ing the MSG_EOF flag to the sctp_sendmsg function. This flag value causes our server to shut down the association after the reply message is successfully acknowledged. Figure 10.12 The client aborts the association before closing.
sctp/sctpclient02.c 25 if (echo_to_all == 0) 26 sctpstr_cli(stdin, sock_fd, (SA *) &servaddr, sizeof (servaddr)); 27 else 28 sctpstr_cli_echoall(stdin, sock_fd, (SA *) &servaddr, 29 sizeof (servaddr)); 30 strcpy(byemsg, "goodbye"); 31 Sctp_sendmsg(sock_fd, byemsg, strlen (byemsg), 32 (SA *) &servaddr, sizeof (servaddr), 0, MSG_ABORT, 0, 0, 0); 33 Close(sock_fd); Abort association before close
30 “32 In these lines, the client prepares a message that is included with the abort as a user error cause. The client then calls the sctp_sendmsg function with the MSG_ABORT flag. This flag sends an ABORT chunk, which immediately terminates the association. The ABORT chunk includes the user-initiated error cause with the message ("goodbye") in the upper layer reason field. Close socket descriptor
33 Even though the association has been aborted, we still need to close the socket descriptor to free the system resources associated with it. |