diff options
Diffstat (limited to 'erts/preloaded/src/socket.erl')
-rw-r--r-- | erts/preloaded/src/socket.erl | 156 |
1 files changed, 116 insertions, 40 deletions
diff --git a/erts/preloaded/src/socket.erl b/erts/preloaded/src/socket.erl index ae1ffdb4ac..07e720c44d 100644 --- a/erts/preloaded/src/socket.erl +++ b/erts/preloaded/src/socket.erl @@ -26,9 +26,13 @@ %% Administrative and "global" utility functions -export([ on_load/0, on_load/1, - info/0, - supports/0, supports/1, supports/2, supports/3, - ensure_sockaddr/1 + + ensure_sockaddr/1, + + debug/1, + %% command/1, + info/0, info/1, + supports/0, supports/1, supports/2, supports/3 ]). -export([ @@ -63,6 +67,12 @@ select_ref/0, select_info/0, + socket_counters/0, + socket_counter/0, + socket_info/0, + + %% command/0, + domain/0, type/0, protocol/0, @@ -135,6 +145,27 @@ ]). +%% The command type has the general form: +%% #{ +%% command := atom(), +%% data := term() +%% } +%% But only certain values are actually valid, so the type gets the form: +-type debug_command() :: #{ + command := debug, + data := boolean() + }. +%% -type command() :: debug_command(). + +-type socket_counters() :: [{socket_counter(), non_neg_integer()}]. +-type socket_counter() :: read_byte | read_fails | read_pkg | read_tries | + read_waits | write_byte | write_fails | write_pkg | + write_tries | write_waits. +-type socket_info() :: #{counters := socket_counters(), + num_readers := non_neg_integer(), + num_writers := non_neg_integer(), + num_acceptors := non_neg_integer()}. + -type uint8() :: 0..16#FF. -type uint16() :: 0..16#FFFF. -type uint20() :: 0..16#FFFFF. @@ -282,7 +313,8 @@ path := binary() | string()}. -type sockaddr_in4() :: #{family := inet, port := port_number(), - addr := any | loopback | ip4_address()}. + %% The 'broadcast' here is the "limited broadcast" + addr := any | broadcast | loopback | ip4_address()}. -type sockaddr_in6() :: #{family := inet6, port := port_number(), addr := any | loopback | ip6_address(), @@ -303,7 +335,7 @@ -define(SOCKADDR_IN6_DEFAULTS, ?SOCKADDR_IN6_DEFAULTS(any)). -define(SOCKADDR_IN6_DEFAULT(A), (?SOCKADDR_IN6_DEFAULTS(A))#{family => inet6}). -%% otp - The option is internal to our (OTP) imeplementation. +%% otp - This option is internal to our (OTP) implementation. %% socket - The socket layer (SOL_SOCKET). %% ip - The IP layer (SOL_IP or is it IPPROTO_IP?). %% ipv6 - The IPv6 layer (SOL_IPV6). @@ -311,6 +343,7 @@ %% udp - The UDP (User Datagram Protocol) layer (IPPROTO_UDP). %% sctp - The SCTP (Stream Control Transmission Protocol) layer (IPPROTO_SCTP). %% Int - Raw level, sent down and used "as is". +%% Its up to the caller to make sure this is correct! -type sockopt_level() :: otp | socket | ip | ipv6 | tcp | udp | sctp | @@ -594,11 +627,11 @@ -opaque select_tag() :: atom(). -opaque select_ref() :: reference(). --record(select_info, {tag :: select_tag(), ref :: select_ref()}). +%% -record(select_info, {tag :: select_tag(), ref :: select_ref()}). --type select_info() :: #select_info{}. +-type select_info() :: {select_info, select_tag(), select_ref()}. --define(SELECT_INFO(T, R), #select_info{tag = T, ref = R}). +-define(SELECT_INFO(T, R), {select_info, T, R}). -define(SELECT(T, R), {select, ?SELECT_INFO(T, R)}). @@ -861,12 +894,46 @@ on_load(Extra) -> --spec info() -> list(). +-spec info() -> map(). info() -> nif_info(). +-spec debug(D) -> ok when + D :: boolean(). + +debug(D) when is_boolean(D) -> + command(#{command => debug, + data => D}). + + +-spec command(Command) -> ok when + Command :: debug_command(). + +command(#{command := debug, + data := Dbg} = Command) when is_boolean(Dbg) -> + nif_command(Command). + + + +%% =========================================================================== +%% +%% info - Get miscellaneous information about a socket. +%% +%% Generates a list of various info about the socket, such as counter values. +%% +%% Do *not* call this function often. +%% +%% =========================================================================== + +-spec info(Socket) -> socket_info() when + Socket :: socket(). + +info(#socket{ref = SockRef}) -> + nif_info(SockRef). + + %% =========================================================================== %% @@ -1081,19 +1148,25 @@ open(Domain, Type, Protocol, Extra) when is_map(Extra) -> %% %% bind - bind a name to a socket %% +%% Note that Addr can only have the value of broadcast *if* Domain =:= inet! +%% -spec bind(Socket, Addr) -> ok | {error, Reason} when Socket :: socket(), - Addr :: any | loopback | sockaddr(), + Addr :: any | broadcast | loopback | sockaddr(), Reason :: term(). bind(#socket{ref = SockRef}, Addr) - when ((Addr =:= any) orelse (Addr =:= loopback)) -> + when ((Addr =:= any) orelse + (Addr =:= broadcast) orelse + (Addr =:= loopback)) -> try which_domain(SockRef) of inet -> nif_bind(SockRef, ?SOCKADDR_IN4_DEFAULT(Addr)); - inet6 -> - nif_bind(SockRef, ?SOCKADDR_IN6_DEFAULT(Addr)) + inet6 when (Addr =:= any) orelse (Addr =:= loopback) -> + nif_bind(SockRef, ?SOCKADDR_IN6_DEFAULT(Addr)); + _ -> + einval() catch %% <WIN32-TEMPORARY> error:notsup:S -> @@ -1236,7 +1309,7 @@ connect(#socket{ref = SockRef}, #{family := Fam} = SockAddr, Timeout) ((Timeout =:= nowait) orelse (Timeout =:= infinity) orelse is_integer(Timeout)) -> TS = timestamp(Timeout), - case nif_connect(SockRef, SockAddr) of + case nif_connect(SockRef, ensure_sockaddr(SockAddr)) of ok -> %% Connected! ok; @@ -1494,7 +1567,7 @@ do_send(SockRef, Data, EFlags, Timeout) -> ok | {error, Reason} when Socket :: socket(), Data :: binary(), - Dest :: null | sockaddr(), + Dest :: sockaddr(), Reason :: term(). sendto(Socket, Data, Dest) -> @@ -1503,7 +1576,7 @@ sendto(Socket, Data, Dest) -> -spec sendto(Socket, Data, Dest, Flags) -> ok | {error, Reason} when Socket :: socket(), Data :: binary(), - Dest :: null | sockaddr(), + Dest :: sockaddr(), Flags :: send_flags(), Reason :: term() ; (Socket, Data, Dest, Timeout :: nowait) -> ok | @@ -1511,13 +1584,13 @@ sendto(Socket, Data, Dest) -> {error, Reason} when Socket :: socket(), Data :: iodata(), - Dest :: null | sockaddr(), + Dest :: sockaddr(), SelectInfo :: select_info(), Reason :: term() ; (Socket, Data, Dest, Timeout) -> ok | {error, Reason} when Socket :: socket(), Data :: iodata(), - Dest :: null | sockaddr(), + Dest :: sockaddr(), Timeout :: timeout(), Reason :: term(). @@ -1532,14 +1605,14 @@ sendto(Socket, Data, Dest, Timeout) -> {error, Reason} when Socket :: socket(), Data :: binary(), - Dest :: null | sockaddr(), + Dest :: sockaddr(), Flags :: send_flags(), SelectInfo :: select_info(), Reason :: term() ; (Socket, Data, Dest, Flags, Timeout) -> ok | {error, Reason} when Socket :: socket(), Data :: binary(), - Dest :: null | sockaddr(), + Dest :: sockaddr(), Flags :: send_flags(), Timeout :: timeout(), Reason :: term(). @@ -1547,15 +1620,6 @@ sendto(Socket, Data, Dest, Timeout) -> sendto(Socket, Data, Dest, Flags, Timeout) when is_list(Data) -> Bin = erlang:list_to_binary(Data), sendto(Socket, Bin, Dest, Flags, Timeout); -sendto(#socket{ref = SockRef}, Data, Dest, Flags, Timeout) - when is_binary(Data) andalso - (Dest =:= null) andalso - is_list(Flags) andalso - ((Timeout =:= nowait) orelse - (Timeout =:= infinity) orelse - (is_integer(Timeout) andalso (Timeout > 0))) -> - EFlags = enc_send_flags(Flags), - do_sendto(SockRef, Data, Dest, EFlags, Timeout); sendto(#socket{ref = SockRef}, Data, #{family := Fam} = Dest, Flags, Timeout) when is_binary(Data) andalso ((Fam =:= inet) orelse (Fam =:= inet6) orelse (Fam =:= local)) andalso @@ -1564,7 +1628,7 @@ sendto(#socket{ref = SockRef}, Data, #{family := Fam} = Dest, Flags, Timeout) (Timeout =:= infinity) orelse (is_integer(Timeout) andalso (Timeout > 0))) -> EFlags = enc_send_flags(Flags), - do_sendto(SockRef, Data, Dest, EFlags, Timeout). + do_sendto(SockRef, Data, ensure_sockaddr(Dest), EFlags, Timeout). do_sendto(SockRef, Data, Dest, EFlags, Timeout) -> TS = timestamp(Timeout), @@ -1755,8 +1819,12 @@ do_sendmsg_rest([B|IOVec], Written) -> ensure_msghdr(#{ctrl := []} = M) -> ensure_msghdr(maps:remove(ctrl, M)); -ensure_msghdr(#{iov := IOV} = M) when is_list(IOV) andalso (IOV =/= []) -> - M#{iov := erlang:iolist_to_iovec(IOV)}; +ensure_msghdr(#{iov := IOV, addr := Addr} = M) + when is_list(IOV) andalso (IOV =/= []) -> + M#{iov => erlang:iolist_to_iovec(IOV), addr => ensure_sockaddr(Addr)}; +ensure_msghdr(#{iov := IOV} = M) + when is_list(IOV) andalso (IOV =/= []) -> + M#{iov => erlang:iolist_to_iovec(IOV)}; ensure_msghdr(_) -> einval(). @@ -2617,7 +2685,7 @@ peername(#socket{ref = SockRef}) -> SelectInfo :: select_info(), Reason :: term(). -cancel(#socket{ref = SockRef}, #select_info{tag = Tag, ref = Ref}) -> +cancel(#socket{ref = SockRef}, ?SELECT_INFO(Tag, Ref)) -> cancel(SockRef, Tag, Ref). @@ -2628,17 +2696,17 @@ cancel(#socket{ref = SockRef}, #select_info{tag = Tag, ref = Ref}) -> %% %% =========================================================================== --spec enc_domain(Domain) -> non_neg_integer() when - Domain :: domain(). +%% -spec enc_domain(Domain) -> non_neg_integer() when +%% Domain :: domain(). enc_domain(local) -> ?SOCKET_DOMAIN_LOCAL; enc_domain(inet) -> ?SOCKET_DOMAIN_INET; enc_domain(inet6) -> ?SOCKET_DOMAIN_INET6; enc_domain(Domain) -> invalid_domain(Domain). --spec enc_type(Domain, Type) -> non_neg_integer() when - Domain :: domain(), - Type :: type(). +%% -spec enc_type(Domain, Type) -> non_neg_integer() when +%% Domain :: domain(), +%% Type :: type(). %% What combos are valid? enc_type(_, stream) -> ?SOCKET_TYPE_STREAM; @@ -3308,10 +3376,12 @@ enc_sockopt_key(otp = L, Opt, _, _, _, _) -> %% +++ SOCKET socket options +++ enc_sockopt_key(socket = _L, acceptconn = _Opt, get = _Dir, _D, _T, _P) -> ?SOCKET_OPT_SOCK_ACCEPTCONN; +enc_sockopt_key(socket = L, acceptconn = Opt, Dir, _D, _T, _P) -> + not_supported({L, Opt, Dir}); enc_sockopt_key(socket = L, acceptfilter = Opt, _Dir, _D, _T, _P) -> not_supported({L, Opt}); -%% Before linux 3.8, this socket option could be set. -%% Maximum size of buffer for name: IFNAMSZIZ +%% Before linux 3.8, this socket option could be set but not get. +%% Maximum size of buffer for name: IFNAMSIZ %% So, we let the implementation decide. enc_sockopt_key(socket = _L, bindtodevice = _Opt, _Dir, _D, _T, _P) -> ?SOCKET_OPT_SOCK_BINDTODEVICE; @@ -3845,6 +3915,12 @@ error(Reason) -> nif_info() -> erlang:nif_error(undef). +nif_info(_SRef) -> + erlang:nif_error(undef). + +nif_command(_Command) -> + erlang:nif_error(undef). + nif_supports(_Key) -> erlang:nif_error(undef). |