diff options
author | Micael Karlberg <[email protected]> | 2018-07-20 15:14:06 +0200 |
---|---|---|
committer | Micael Karlberg <[email protected]> | 2018-09-18 14:50:18 +0200 |
commit | 1c26ae984a79224ce64b40dbc7239bf9721bb096 (patch) | |
tree | bc01665931d21620dc152eeb402ad47f21bd0aac | |
parent | 84f62ae80dd08874d0d5fbedc532605394e897c1 (diff) | |
download | otp-1c26ae984a79224ce64b40dbc7239bf9721bb096.tar.gz otp-1c26ae984a79224ce64b40dbc7239bf9721bb096.tar.bz2 otp-1c26ae984a79224ce64b40dbc7239bf9721bb096.zip |
[socket-nif] Add support for socket (level ip) option freebind
Added support for ip level socket option FREEBIND.
Note that there is an option available on FreeBSD called
IP_BINDANY, which seems to have similar properties (FREEBIND
is *not* available on FreeBSD). There are some restrictions
for this option though (which is not mentioned in the Linux
man page).
OTP-14831
-rw-r--r-- | erts/doc/src/socket_usage.xml | 7 | ||||
-rw-r--r-- | erts/emulator/nifs/common/socket_nif.c | 60 | ||||
-rw-r--r-- | erts/preloaded/ebin/socket.beam | bin | 56432 -> 56564 bytes | |||
-rw-r--r-- | erts/preloaded/src/socket.erl | 17 | ||||
-rw-r--r-- | lib/kernel/test/socket_server.erl | 4 |
5 files changed, 83 insertions, 5 deletions
diff --git a/erts/doc/src/socket_usage.xml b/erts/doc/src/socket_usage.xml index c875ecd6f0..a286171a1b 100644 --- a/erts/doc/src/socket_usage.xml +++ b/erts/doc/src/socket_usage.xml @@ -289,6 +289,13 @@ <cell>none</cell> </row> <row> + <cell>freebind</cell> + <cell>boolean()</cell> + <cell>yes</cell> + <cell>yes</cell> + <cell>none</cell> + </row> + <row> <cell>minttl</cell> <cell>integer()</cell> <cell>yes</cell> diff --git a/erts/emulator/nifs/common/socket_nif.c b/erts/emulator/nifs/common/socket_nif.c index 6df2020e5e..f319f171ad 100644 --- a/erts/emulator/nifs/common/socket_nif.c +++ b/erts/emulator/nifs/common/socket_nif.c @@ -517,6 +517,7 @@ typedef union { #define SOCKET_OPT_IP_BLOCK_SOURCE 3 #define SOCKET_OPT_IP_DROP_MEMBERSHIP 5 #define SOCKET_OPT_IP_DROP_SOURCE_MEMBERSHIP 6 +#define SOCKET_OPT_IP_FREEBIND 7 #define SOCKET_OPT_IP_MINTTL 9 #define SOCKET_OPT_IP_MTU 11 #define SOCKET_OPT_IP_MTU_DISCOVER 12 @@ -1086,6 +1087,11 @@ static ERL_NIF_TERM nsetopt_lvl_ip_drop_source_membership(ErlNifEnv* env, SocketDescriptor* descP, ERL_NIF_TERM eVal); #endif +#if defined(IP_FREEBIND) +static ERL_NIF_TERM nsetopt_lvl_ip_freebind(ErlNifEnv* env, + SocketDescriptor* descP, + ERL_NIF_TERM eVal); +#endif #if defined(IP_MINTTL) static ERL_NIF_TERM nsetopt_lvl_ip_minttl(ErlNifEnv* env, SocketDescriptor* descP, @@ -1408,6 +1414,10 @@ static ERL_NIF_TERM ngetopt_lvl_sock_type(ErlNifEnv* env, static ERL_NIF_TERM ngetopt_lvl_ip(ErlNifEnv* env, SocketDescriptor* descP, int eOpt); +#if defined(IP_FREEBIND) +static ERL_NIF_TERM ngetopt_lvl_ip_freebind(ErlNifEnv* env, + SocketDescriptor* descP); +#endif #if defined(IP_MINTTL) static ERL_NIF_TERM ngetopt_lvl_ip_minttl(ErlNifEnv* env, SocketDescriptor* descP); @@ -4738,6 +4748,12 @@ ERL_NIF_TERM nsetopt_lvl_ip(ErlNifEnv* env, break; #endif +#if defined(IP_FREEBIND) + case SOCKET_OPT_IP_FREEBIND: + result = nsetopt_lvl_ip_freebind(env, descP, eVal); + break; +#endif + #if defined(IP_MINTTL) case SOCKET_OPT_IP_MINTTL: result = nsetopt_lvl_ip_minttl(env, descP, eVal); @@ -4936,6 +4952,26 @@ ERL_NIF_TERM nsetopt_lvl_ip_drop_source_membership(ErlNifEnv* env, +/* nsetopt_lvl_ip_freebind - Level IP FREEBIND option + */ +#if defined(IP_FREEBIND) +static +ERL_NIF_TERM nsetopt_lvl_ip_freebind(ErlNifEnv* env, + SocketDescriptor* descP, + ERL_NIF_TERM eVal) +{ +#if defined(SOL_IP) + int level = SOL_IP; +#else + int level = IPPROTO_IP; +#endif + + return nsetopt_bool_opt(env, descP, level, IP_FREEBIND, eVal); +} +#endif + + + /* nsetopt_lvl_ip_minttl - Level IP MINTTL option */ #if defined(IP_MINTTL) @@ -7374,6 +7410,12 @@ ERL_NIF_TERM ngetopt_lvl_ip(ErlNifEnv* env, ERL_NIF_TERM result; switch (eOpt) { +#if defined(IP_FREEBIND) + case SOCKET_OPT_IP_FREEBIND: + result = ngetopt_lvl_ip_freebind(env, descP); + break; +#endif + #if defined(IP_MINTTL) case SOCKET_OPT_IP_MINTTL: result = ngetopt_lvl_ip_minttl(env, descP); @@ -7485,6 +7527,24 @@ ERL_NIF_TERM ngetopt_lvl_ip_minttl(ErlNifEnv* env, #endif +/* ngetopt_lvl_ip_freebind - Level IP FREEBIND option + */ +#if defined(IP_FREEBIND) +static +ERL_NIF_TERM ngetopt_lvl_ip_freebind(ErlNifEnv* env, + SocketDescriptor* descP) +{ +#if defined(SOL_IP) + int level = SOL_IP; +#else + int level = IPPROTO_IP; +#endif + + return ngetopt_bool_opt(env, descP, level, IP_FREEBIND); +} +#endif + + /* ngetopt_lvl_ip_mtu - Level IP MTU option */ #if defined(IP_MTU) diff --git a/erts/preloaded/ebin/socket.beam b/erts/preloaded/ebin/socket.beam Binary files differindex 8799f07bc0..3657212f81 100644 --- a/erts/preloaded/ebin/socket.beam +++ b/erts/preloaded/ebin/socket.beam diff --git a/erts/preloaded/src/socket.erl b/erts/preloaded/src/socket.erl index c231647f8c..329223c443 100644 --- a/erts/preloaded/src/socket.erl +++ b/erts/preloaded/src/socket.erl @@ -87,6 +87,7 @@ tcp_socket_option/0, udp_socket_option/0, sctp_socket_option/0, + raw_socket_option/0, timeval/0, ip_tos_flag/0, @@ -258,7 +259,7 @@ %% Int - Raw level, sent down and used "as is". -type sockopt_level() :: otp | socket | - ip | ipv6 | tcp | udp | sctp | + ip | ipv6 | tcp | udp | sctp | raw | non_neg_integer(). %% There are some options that are 'read-only'. @@ -434,6 +435,8 @@ status | use_ext_recvinfo. +-type raw_socket_option() :: filter. + %% -type plain_socket_option() :: integer(). %% -type sockopt() :: otp_socket_option() | %% socket_option() | @@ -442,6 +445,7 @@ %% tcp_socket_option() | %% udp_socket_option() | %% sctp_socket_option() | +%% raw_socket_option() | %% plain_socket_option(). -type socket_info() :: #{domain => domain(), @@ -594,7 +598,7 @@ %% -define(SOCKET_OPT_IP_DONTFRAG, 4). -define(SOCKET_OPT_IP_DROP_MEMBERSHIP, 5). -define(SOCKET_OPT_IP_DROP_SOURCE_MEMBERSHIP, 6). -%% -define(SOCKET_OPT_IP_FREEBIND, 7). +-define(SOCKET_OPT_IP_FREEBIND, 7). %% -define(SOCKET_OPT_IP_HDRINCL, 8). -define(SOCKET_OPT_IP_MINTTL, 9). %% -define(SOCKET_OPT_IP_MSFILTER, 10). @@ -2177,6 +2181,8 @@ enc_setopt_value(ip, drop_source_membership, #{multiaddr := MA, (is_tuple(IF) andalso (size(IF) =:= 4)) andalso (is_tuple(SA) andalso (size(SA) =:= 4)) -> V; +enc_setopt_value(ip, freebind, V, _D, _T, _P) when is_boolean(V) -> + V; enc_setopt_value(ip, minttl, V, _D, _T, _P) when is_integer(V) -> V; enc_setopt_value(ip, mtu_discover, V, _D, _T, _P) @@ -2358,6 +2364,9 @@ enc_setopt_value(sctp, rtoinfo, #{assoc_id := AssocId, enc_setopt_value(sctp = L, Opt, V, _D, _T, _P) -> not_supported({L, Opt, V}); +enc_setopt_value(raw = L, Opt, _V, _D, _T, _P) -> + not_supported({L, Opt}); + %% Is this correct? What about getopt? enc_setopt_value(L, Opt, V, _, _, _) when is_integer(L) andalso is_integer(Opt) andalso is_binary(V) -> @@ -2574,8 +2583,8 @@ enc_sockopt_key(ip = _L, drop_membership = _Opt, set = _Dir, _D, _T, _P) -> enc_sockopt_key(ip = _L, drop_source_membership = _Opt, set = _Dir, _D, _T, _P) -> ?SOCKET_OPT_IP_DROP_SOURCE_MEMBERSHIP; %% Linux only? -enc_sockopt_key(ip = L, free_bind = Opt, _Dir, _D, _T, _P) -> - not_supported({L, Opt}); +enc_sockopt_key(ip = _L, freebind = _Opt, _Dir, _D, _T, _P) -> + ?SOCKET_OPT_IP_FREEBIND; enc_sockopt_key(ip = L, hdrincl = Opt, _Dir, _D, raw = _T, _P) -> not_supported({L, Opt}); %% FreeBSD only? diff --git a/lib/kernel/test/socket_server.erl b/lib/kernel/test/socket_server.erl index 6aa26494f7..971ceb2093 100644 --- a/lib/kernel/test/socket_server.erl +++ b/lib/kernel/test/socket_server.erl @@ -604,6 +604,7 @@ handler_init(Manager, ID, Peek, Sock) -> SndTO = GSO(sndtimeo), Linger = GSO(linger), Timestamp = GSO(timestamp), + FreeBind = GIP(freebind), MTU = GIP(mtu), MTUDisc = GIP(mtu_discover), MALL = GIP(multicast_all), @@ -629,6 +630,7 @@ handler_init(Manager, ID, Peek, Sock) -> "~n (socket) SndTO: ~s" "~n (socket) Linger: ~s" "~n (socket) Timestamp: ~s" + "~n (ip) FreeBind: ~s" "~n (ip) MTU: ~s" "~n (ip) MTU Discovery: ~s" "~n (ip) Multicast ALL: ~s" @@ -642,7 +644,7 @@ handler_init(Manager, ID, Peek, Sock) -> RA, RP, B2D, OOBI, RcvBuf, RcvLW, RcvTO, SndBuf, SndLW, SndTO, Linger, Timestamp, - MTU, MTUDisc, MALL, MIF, MLoop, MTTL, + FreeBind, MTU, MTUDisc, MALL, MIF, MLoop, MTTL, NF, RecvTOS, RecvTTL]), handler_loop(#handler{peek = Peek, |