23.2 An Autoclosing
One-to-Many-Style Server
Recall the server program we wrote in Chapter 10. That
program does not keep track of any associations. The server depends
on the client to close the association, thereby removing the
association state. But depending on the client to close the
association leaves a weakness: What happens if a client opens an
association and never sends any data? Resources would be allocated
to a client that never uses them. This dependency could introduce
an accidental denial-of-service attack to our SCTP implementation
from lazy clients. To avoid this problem, an autoclose feature was added to SCTP.
Autoclose lets an SCTP endpoint specify a
maximum number of seconds an association may remain idle. An
association is considered idle when it is not transmitting user
data in either direction. If an association is idle for more than
this maximum time, the association is automatically closed by the
SCTP implementation.
When using this option, care should be taken in
choosing a value for autoclose. The server should not pick too
small a value, otherwise it may find itself needing to send data on
an association that has been closed. There would be extra overhead
in re-opening the association to send back the data to the client,
and it is unlikely that the client would have performed a
listen to enable inbound associations. Figure 23.1 revisits our server code and
inserts the necessary calls to make our server resistant to stale
idle associations. As described in Section 7.10,
autoclose defaults to disabled and must be explicitly enabled with
the SCTP_AUTOCLOSE socket option.
Figure 23.1 A
server enabling autoclose.
sctp/sctpserv04.c
14 if (argc == 2)
15 stream_increment = atoi(argv[1]);
16 sock_fd = Socket(AF_INET, SOCK_SEQPACKET, IPPROTO_SCTP);
17 close_time = 120;
18 Setsockopt(sock_fd, IPPROTO_SCTP, SCTP_AUTOCLOSE,
19 &close_time, sizeof(close_time));
20 bzero(&servaddr, sizeof(servaddr));
21 servaddr.sin_family = AF_INET;
22 servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
23 servaddr.sin_port = htons(SERV_PORT);
Set autoclose option
17鈥?9
The server selects a value of 120 seconds to shut down idle
associations and places this value in the variable
close_time. Next, the server calls the socket option that
configures the autoclose time. All the remaining code in the server
stays unchanged.
Now, SCTP will automatically close associations
that remain idle for more than two minutes. By forcing the
association to close automatically, we reduce the amount of server
resources consumed by lazy clients.
|