Pushpendra
Pushpendra

Reputation: 459

close call on a SCTP socket is blocking or non blocking?

I am facing an issue, not sure if i can call it an issue or its just understanding gap.

I am calling close() on a SCTP socket FD (something like this: close(sctp_sock_fd);). I am expecting this close call will return when SCTP SHUTDOWN flow will complete i.e. it will do below then return:

SHUTDWON (Source -> Peer)
SHUTDWON_ACK (Source <- Peer)
SHUTDOWN_COMPLETE (Source -> Peer)

But what i saw that it looks like close(sctp_sock_fd); returned soon after the call, and SCTP shutdown sequence is in progress.

If it is true that its a non-blocking call then is there any way i can ensure that graceful shutdown at SCTP Kernel level has completed ??

Upvotes: 3

Views: 1289

Answers (1)

nos
nos

Reputation: 229184

Yes by default close() will be non-blocking on an SCTP socket, it will just initiate the shutdown procedure, not wait for it to complete.

You can change this by setting the SO_LINGER socket option:

struct linger lin;
unsigned int len =sizeof(lin);
lin.l_onoff=1;
lin.l_linger=10;
setsockopt(socketfd,SOL_SOCKET, SO_LINGER,&lin, len);

Whith this setting, close() will block up to 10 seconds From the SCTP Socket API RFC:

8.1.4. SO_LINGER

An application can use this option to perform the SCTP ABORT
primitive. This option affects all associations related to the
socket.

The linger option structure is

struct linger { int l_onoff; /* option on/off / int l_linger; / linger time */ };

To enable the option, set l_onoff to 1. If the l_linger value is set to 0, calling close() is the same as the ABORT primitive. If the value is set to a negative value, the setsockopt() call will return an error. If the value is set to a positive value linger_time, the close() can be blocked for at most linger_time. Please note that the time unit is in seconds, according to POSIX, but might be different on specific platforms. If the graceful shutdown phase does not finish during this period, close() will return, but the graceful shutdown phase will continue in the system.

Note that this is a socket-level option, not an SCTP-level option. When using this option, an application must specify a level of
SOL_SOCKET in the call.

If you're using Linux, there's also these notes in the source code.

Upvotes: 2

Related Questions