diff options
author | Raimo Niskanen <raimo@erlang.org> | 2021-07-27 21:39:10 +0200 |
---|---|---|
committer | Raimo Niskanen <raimo@erlang.org> | 2021-07-28 11:35:25 +0200 |
commit | 0a58d875f0d59e6d1ebfa4090073eeb8f06dc11b (patch) | |
tree | 780ed17568d00aaa33f2d336f0bc164ccf0fc04b | |
parent | 47a7651a0725fbd1f456e32350c427bc23eb45f5 (diff) | |
download | erlang-0a58d875f0d59e6d1ebfa4090073eeb8f06dc11b.tar.gz |
Bind if port has been specified
-rw-r--r-- | lib/kernel/src/inet.erl | 31 |
1 files changed, 27 insertions, 4 deletions
diff --git a/lib/kernel/src/inet.erl b/lib/kernel/src/inet.erl index b5b7b0a3eb..b638fd8a30 100644 --- a/lib/kernel/src/inet.erl +++ b/lib/kernel/src/inet.erl @@ -1563,12 +1563,33 @@ open(Fd, BAddr, BPort, Opts, Protocol, Family, Type, Module) open_fd(Fd, BAddr, BPort, Opts, Protocol, Family, Type, Module); open(Fd_or_OpenOpts, BAddr, BPort, Opts, Protocol, Family, Type, Module) -> open_opts( - Fd_or_OpenOpts, BAddr, BPort, Opts, Protocol, Family, Type, Module). + Fd_or_OpenOpts, + if + BAddr =:= undefined, BPort =/= 0 -> + translate_ip(any, Family); + true -> + BAddr + end, BPort, Opts, Protocol, Family, Type, Module). %% The only difference between open/8 and open_bind/8 is that -%% if Fd_O is not a (file descriptor :: non_neg_integer()) -%% the latter translates Addr =:= 'undefined' to 'any', -%% causing open_setopts/5 to bind to the wildcard address +%% if Fd_or_OpenOpts is not a FileDescriptor :: non_neg_integer() +%% i.e option {fd,Fd} has not been used hence we are not handling +%% an already open socket handle, and also if no bind address +%% has been specified (BAddr =:= undefined). +%% +%% Then open_bind/8 will bind to the wildcard address and the +%% specified port (BPort, 0 = wildcard port per default), +%% which is the legacy behaviour by this module when opening a socket. +%% +%% In the same situation, open/8 will bind to the wildcard address +%% and the specified port, only if BPort is not 0, i.e a bind port +%% has been specified to not be the wildcard port. +%% +%% So open/8 per default does not bind to an address, which +%% is used by TCP connect to let the OS automatically bind to +%% an address later, during TCP connect operation. This +%% gives the OS more freedom in choosing the originating port and +%% therefore makes far more effective use of the port range. -spec open_bind(Fd_or_OpenOpts :: integer() | list(), BAddr :: @@ -1637,6 +1658,8 @@ open_opts(Fd_or_OpenOpts, BAddr, BPort, Opts, Protocol, Family, Type, Module) -> Error end. +%% If BAddr is undefined - do not bind to an address +%% open_setopts(S, BAddr, BPort, Opts, Module) -> case prim_inet:setopts(S, Opts) of ok when BAddr =:= undefined -> |