From 827bb85bafd96ec3037c849ea42878e4f581d22f Mon Sep 17 00:00:00 2001 From: Simon Cornish Date: Mon, 21 Dec 2009 22:48:42 -0800 Subject: Implement a non-blocking SCTP connect This patch adds a new set of functions - gen_sctp:connect_init/* that initiate an SCTP connection without blocking for the result. The result is delivered asynchronously as an sctp_assoc_change event. The new functions have the same API as documented for gen_sctp:connect/* with the following exceptions: * Timeout is only used to supervise resolving Addr (the peer address) * The possible return values are ok | {error, posix()} The caller application is responsible for receiving the #sctp_assoc_change{} event and correctly determining the connect it originated from (for example, by examining the remote host and/or port). The application should have at least {active, once} or use gen_sctp:recv to retrieve the connect result. The implementation of gen_sctp:connect suffers from a number of shortcomings which the user may avoid by using gen_sctp:connect_init and adding code to receive the connect result. First, irrespective of the Timeout value given to gen_sctp:connect, the OS attempts and retries the SCTP INIT according to various kernel parameters. If the Timeout value is shorter than the entire attempt then the application will still receive an sctp_assoc_change event after the {error, timeout} is returned from the initial call. This could be somewhat confusing (either to the application or the designer!) especially if the status is comm_up. Subsequent calls to connect before the OS has finished this process return {error, ealready} which may also be counter-intuitive. Second, there is a race-condition (documented in comments in inet_sctp.erl) that can cause the wrong sctp_assoc_change record to be returned to an application calling gen_sctp:connect. The race seriously affects connection attempts when using one-to-many sockets. --- lib/kernel/src/inet_sctp.erl | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) (limited to 'lib/kernel/src/inet_sctp.erl') diff --git a/lib/kernel/src/inet_sctp.erl b/lib/kernel/src/inet_sctp.erl index 30c0e85dd9..6fdd4bf789 100644 --- a/lib/kernel/src/inet_sctp.erl +++ b/lib/kernel/src/inet_sctp.erl @@ -64,16 +64,22 @@ close(S) -> listen(S, Flag) -> prim_inet:listen(S, Flag). +%% A non-blocking connect is implemented when the initial call is to +%% gen_sctp:connect_init which passes the value nowait as the Timer connect(S, Addr, Port, Opts, Timer) -> case prim_inet:chgopts(S, Opts) of ok -> case prim_inet:getopt(S, active) of {ok,Active} -> - Timeout = inet:timeout(Timer), + Timeout = if Timer =:= nowait -> + infinity; %% don't start driver timer in inet_drv + true -> + inet:timeout(Timer) + end, case prim_inet:connect(S, Addr, Port, Timeout) of - ok -> + ok when Timer =/= nowait -> connect_get_assoc(S, Addr, Port, Active, Timer); - Err1 -> Err1 + OkOrErr1 -> OkOrErr1 end; Err2 -> Err2 end; @@ -89,10 +95,10 @@ connect(S, Addr, Port, Opts, Timer) -> %% connect_get_assoc/5 below mistakes it for an invalid response %% for a socket in {active,false} or {active,once} modes. %% -%% In {active,true} mode it probably gets right, but it is -%% a blocking connect that is implemented even for {active,true}, -%% and that may be a shortcoming. A non-blocking connect -%% would be nice to have. +%% In {active,true} mode the window of time for the race is smaller, +%% but it is possible and also it is a blocking connect that is +%% implemented even for {active,true}, and that may be a +%% shortcoming. connect_get_assoc(S, Addr, Port, false, Timer) -> case recv(S, inet:timeout(Timer)) of -- cgit v1.2.3