aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--erts/emulator/nifs/common/socket_int.h1
-rw-r--r--erts/emulator/nifs/common/socket_nif.c59
-rw-r--r--erts/emulator/test/socket_SUITE.erl46
-rw-r--r--erts/preloaded/ebin/socket.beambin70248 -> 70640 bytes
-rw-r--r--erts/preloaded/src/socket.erl82
5 files changed, 152 insertions, 36 deletions
diff --git a/erts/emulator/nifs/common/socket_int.h b/erts/emulator/nifs/common/socket_int.h
index 38c28a6de5..d6977be5aa 100644
--- a/erts/emulator/nifs/common/socket_int.h
+++ b/erts/emulator/nifs/common/socket_int.h
@@ -139,6 +139,7 @@ typedef unsigned int BOOLEAN_T;
GLOBAL_ATOM_DEF(ctrunc); \
GLOBAL_ATOM_DEF(data); \
GLOBAL_ATOM_DEF(debug); \
+ GLOBAL_ATOM_DEF(default); \
GLOBAL_ATOM_DEF(default_send_params); \
GLOBAL_ATOM_DEF(delayed_ack_time); \
GLOBAL_ATOM_DEF(dgram); \
diff --git a/erts/emulator/nifs/common/socket_nif.c b/erts/emulator/nifs/common/socket_nif.c
index e71c786d3e..7484694cdb 100644
--- a/erts/emulator/nifs/common/socket_nif.c
+++ b/erts/emulator/nifs/common/socket_nif.c
@@ -528,6 +528,7 @@ typedef union {
#define SOCKET_TYPE_SEQPACKET 5
/* protocol */
+#define SOCKET_PROTOCOL_DEFAULT 0
#define SOCKET_PROTOCOL_IP 1
#define SOCKET_PROTOCOL_TCP 2
#define SOCKET_PROTOCOL_UDP 3
@@ -1000,6 +1001,8 @@ static ERL_NIF_TERM nopen(ErlNifEnv* env,
int type,
int protocol,
char* netns);
+static BOOLEAN_T nopen_which_protocol(SOCKET sock, int* proto);
+
static ERL_NIF_TERM nbind(ErlNifEnv* env,
ESockDescriptor* descP,
ESockAddress* sockAddrP,
@@ -2644,6 +2647,7 @@ static char str_exsend[] = "exsend"; // failed send
GLOBAL_ATOM_DECL(ctrunc); \
GLOBAL_ATOM_DECL(data); \
GLOBAL_ATOM_DECL(debug); \
+ GLOBAL_ATOM_DECL(default); \
GLOBAL_ATOM_DECL(default_send_params); \
GLOBAL_ATOM_DECL(delayed_ack_time); \
GLOBAL_ATOM_DECL(dgram); \
@@ -4421,6 +4425,7 @@ ERL_NIF_TERM nif_open(ErlNifEnv* env,
* yet, we must use the global debug flag.
*/
#if !defined(__WIN32__)
+
static
ERL_NIF_TERM nopen(ErlNifEnv* env,
int domain, int type, int protocol,
@@ -4428,7 +4433,7 @@ ERL_NIF_TERM nopen(ErlNifEnv* env,
{
ESockDescriptor* descP;
ERL_NIF_TERM res;
- int save_errno = 0;
+ int proto = protocol, save_errno = 0;
SOCKET sock;
HANDLE event;
#ifdef HAVE_SETNS
@@ -4448,11 +4453,24 @@ ERL_NIF_TERM nopen(ErlNifEnv* env,
return esock_make_error_errno(env, save_errno);
#endif
- if ((sock = sock_open(domain, type, protocol)) == INVALID_SOCKET)
+ if ((sock = sock_open(domain, type, proto)) == INVALID_SOCKET)
return esock_make_error_errno(env, sock_errno());
SGDBG( ("SOCKET", "nopen -> open success: %d\r\n", sock) );
+
+ /* NOTE that if the protocol = 0 (default) and the domain is not
+ * local (AF_LOCAL) we need to explicitly get the protocol here!
+ */
+
+ if ((proto != 0) && (domain != AF_LOCAL))
+ if (!nopen_which_protocol(sock, &proto)) {
+ save_errno = sock_errno();
+ while ((sock_close(sock) == INVALID_SOCKET) && (sock_errno() == EINTR));
+ return esock_make_error_errno(env, save_errno);
+ }
+
+
#ifdef HAVE_SETNS
if ((netns != NULL) &&
!restore_network_namespace(current_ns, sock, &save_errno))
@@ -4484,7 +4502,7 @@ ERL_NIF_TERM nopen(ErlNifEnv* env,
descP->state = SOCKET_STATE_OPEN;
descP->domain = domain;
descP->type = type;
- descP->protocol = protocol;
+ descP->protocol = proto;
/* Does this apply to other types? Such as RAW?
* Also, is this really correct? Should we not wait for bind?
@@ -4521,6 +4539,26 @@ ERL_NIF_TERM nopen(ErlNifEnv* env,
return esock_make_ok2(env, res);
}
+
+
+static
+BOOLEAN_T nopen_which_protocol(SOCKET sock, int* proto)
+{
+ int val;
+ SOCKOPTLEN_T valSz = sizeof(val);
+ int res;
+
+ res = sock_getopt(sock, SOL_SOCKET, SO_PROTOCOL, &val, &valSz);
+
+ if (res != 0) {
+ return FALSE;
+ } else {
+ *proto = val;
+ return TRUE;
+ }
+}
+
+
#endif // if !defined(__WIN32__)
@@ -10895,7 +10933,11 @@ ERL_NIF_TERM ngetopt_otp_protocol(ErlNifEnv* env,
switch (val) {
case IPPROTO_IP:
- result = esock_make_ok2(env, esock_atom_ip);
+ if (descP->domain == AF_LOCAL) {
+ result = esock_make_ok2(env, esock_atom_default);
+ } else {
+ result = esock_make_ok2(env, esock_atom_ip);
+ }
break;
case IPPROTO_TCP:
@@ -11467,7 +11509,10 @@ ERL_NIF_TERM ngetopt_lvl_sock_protocol(ErlNifEnv* env,
} else {
switch (val) {
case IPPROTO_IP:
- result = esock_make_ok2(env, esock_atom_ip);
+ if (descP->domain == AF_LOCAL)
+ result = esock_make_ok2(env, esock_atom_default);
+ else
+ result = esock_make_ok2(env, esock_atom_ip);
break;
case IPPROTO_TCP:
@@ -17039,6 +17084,10 @@ BOOLEAN_T eproto2proto(ErlNifEnv* env,
}
switch (ep) {
+ case SOCKET_PROTOCOL_DEFAULT:
+ *proto = 0; // default - note that _IP also has the value 0...
+ break;
+
case SOCKET_PROTOCOL_IP:
*proto = IPPROTO_IP;
break;
diff --git a/erts/emulator/test/socket_SUITE.erl b/erts/emulator/test/socket_SUITE.erl
index e3545ccbf9..74a6e2ecde 100644
--- a/erts/emulator/test/socket_SUITE.erl
+++ b/erts/emulator/test/socket_SUITE.erl
@@ -70,6 +70,8 @@
%% *** API Basic ***
api_b_open_and_close_udp4/1,
api_b_open_and_close_tcp4/1,
+ api_b_open_and_close_udpL/1,
+ api_b_open_and_close_tcpL/1,
api_b_sendto_and_recvfrom_udp4/1,
api_b_sendmsg_and_recvmsg_udp4/1,
api_b_send_and_recv_tcp4/1,
@@ -588,6 +590,8 @@ api_basic_cases() ->
[
api_b_open_and_close_udp4,
api_b_open_and_close_tcp4,
+ api_b_open_and_close_udpL,
+ api_b_open_and_close_tcpL,
api_b_sendto_and_recvfrom_udp4,
api_b_sendmsg_and_recvmsg_udp4,
api_b_send_and_recv_tcp4,
@@ -1510,6 +1514,44 @@ api_b_open_and_close_tcp4(_Config) when is_list(_Config) ->
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%% Basically open (create) and close an Unix Domain dgram (UDP) socket.
+%% With some extra checks...
+api_b_open_and_close_udpL(suite) ->
+ [];
+api_b_open_and_close_udpL(doc) ->
+ [];
+api_b_open_and_close_udpL(_Config) when is_list(_Config) ->
+ ?TT(?SECS(5)),
+ tc_try(api_b_open_and_close_udpL,
+ fun() ->
+ InitState = #{domain => local,
+ type => dgram,
+ protocol => default},
+ ok = api_b_open_and_close(InitState)
+ end).
+
+
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+
+%% Basically open (create) and close an Unix Domain stream (TCP) socket.
+%% With some extra checks...
+api_b_open_and_close_tcpL(suite) ->
+ [];
+api_b_open_and_close_tcpL(doc) ->
+ [];
+api_b_open_and_close_tcpL(_Config) when is_list(_Config) ->
+ ?TT(?SECS(5)),
+ tc_try(api_b_open_and_close_tcpL,
+ fun() ->
+ InitState = #{domain => local,
+ type => stream,
+ protocol => default},
+ ok = api_b_open_and_close(InitState)
+ end).
+
+
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+
api_b_open_and_close(InitState) ->
Seq =
[
@@ -1665,11 +1707,11 @@ api_b_sendmsg_and_recvmsg_udp4(_Config) when is_list(_Config) ->
end,
Recv = fun(Sock) ->
%% We have some issues on old darwing...
- socket:setopt(Sock, otp, debug, true),
+ %% socket:setopt(Sock, otp, debug, true),
case socket:recvmsg(Sock) of
{ok, #{addr := Source,
iov := [Data]}} ->
- socket:setopt(Sock, otp, debug, false),
+ %% socket:setopt(Sock, otp, debug, false),
{ok, {Source, Data}};
{error, _} = ERROR ->
ERROR
diff --git a/erts/preloaded/ebin/socket.beam b/erts/preloaded/ebin/socket.beam
index 27efe10f38..6aa043972c 100644
--- a/erts/preloaded/ebin/socket.beam
+++ b/erts/preloaded/ebin/socket.beam
Binary files differ
diff --git a/erts/preloaded/src/socket.erl b/erts/preloaded/src/socket.erl
index 126db66cdd..cdbc79204e 100644
--- a/erts/preloaded/src/socket.erl
+++ b/erts/preloaded/src/socket.erl
@@ -602,12 +602,13 @@
%% -define(SOCKET_TYPE_RDM, 4).
-define(SOCKET_TYPE_SEQPACKET, 5).
--define(SOCKET_PROTOCOL_IP, 1).
--define(SOCKET_PROTOCOL_TCP, 2).
--define(SOCKET_PROTOCOL_UDP, 3).
--define(SOCKET_PROTOCOL_SCTP, 4).
--define(SOCKET_PROTOCOL_ICMP, 5).
--define(SOCKET_PROTOCOL_IGMP, 6).
+-define(SOCKET_PROTOCOL_DEFAULT, 0).
+-define(SOCKET_PROTOCOL_IP, 1).
+-define(SOCKET_PROTOCOL_TCP, 2).
+-define(SOCKET_PROTOCOL_UDP, 3).
+-define(SOCKET_PROTOCOL_SCTP, 4).
+-define(SOCKET_PROTOCOL_ICMP, 5).
+-define(SOCKET_PROTOCOL_IGMP, 6).
-define(SOCKET_LISTEN_BACKLOG_DEFAULT, 5).
@@ -1006,12 +1007,18 @@ supports(_Key1, _Key2, _Key3) ->
Reason :: term().
open(Domain, Type) ->
- open(Domain, Type, null).
+ open(Domain, Type, default).
-spec open(Domain, Type, Protocol) -> {ok, Socket} | {error, Reason} when
+ Domain :: local,
+ Type :: stream | dgram,
+ Protocol :: default,
+ Socket :: socket(),
+ Reason :: term();
+ (Domain, Type, Protocol) -> {ok, Socket} | {error, Reason} when
Domain :: domain(),
Type :: type(),
- Protocol :: null | protocol(),
+ Protocol :: default | protocol(),
Socket :: socket(),
Reason :: term().
@@ -1019,17 +1026,23 @@ open(Domain, Type, Protocol) ->
open(Domain, Type, Protocol, #{}).
-spec open(Domain, Type, Protocol, Extra) -> {ok, Socket} | {error, Reason} when
+ Domain :: local,
+ Type :: stream | dgram,
+ Protocol :: default,
+ Extra :: map(),
+ Socket :: socket(),
+ Reason :: term();
+ (Domain, Type, Protocol, Extra) -> {ok, Socket} | {error, Reason} when
Domain :: domain(),
Type :: type(),
- Protocol :: null | protocol(),
+ Protocol :: default | protocol(),
Extra :: map(),
Socket :: socket(),
Reason :: term().
-open(Domain, Type, Protocol0, Extra) when is_map(Extra) ->
+open(Domain, Type, Protocol, Extra) when is_map(Extra) ->
try
begin
- Protocol = default_protocol(Protocol0, Type),
EDomain = enc_domain(Domain),
EType = enc_type(Domain, Type),
EProtocol = enc_protocol(Type, Protocol),
@@ -1052,15 +1065,6 @@ open(Domain, Type, Protocol0, Extra) when is_map(Extra) ->
{error, Reason}
end.
-%% Note that this is just a convenience function for when the protocol was
-%% not specified. If its actually specified, then that will be selected.
-%% Also, this only works for the some of the type's (stream, dgram and
-%% seqpacket).
-default_protocol(null, stream) -> tcp;
-default_protocol(null, dgram) -> udp;
-default_protocol(null, seqpacket) -> sctp;
-default_protocol(null, Type) -> throw({error, {no_default_protocol, Type}});
-default_protocol(Protocol, _) -> Protocol.
%% ===========================================================================
@@ -2355,7 +2359,7 @@ peername(#socket{ref = SockRef}) ->
enc_domain(local) -> ?SOCKET_DOMAIN_LOCAL;
enc_domain(inet) -> ?SOCKET_DOMAIN_INET;
enc_domain(inet6) -> ?SOCKET_DOMAIN_INET6;
-enc_domain(Domain) -> throw({error, {invalid_domain, Domain}}).
+enc_domain(Domain) -> invalid_domain(Domain).
-spec enc_type(Domain, Type) -> non_neg_integer() when
Domain :: domain(),
@@ -2366,22 +2370,23 @@ enc_type(_, stream) -> ?SOCKET_TYPE_STREAM;
enc_type(_, dgram) -> ?SOCKET_TYPE_DGRAM;
enc_type(_, raw) -> ?SOCKET_TYPE_RAW;
enc_type(_, seqpacket) -> ?SOCKET_TYPE_SEQPACKET;
-enc_type(_, Type) -> throw({error, {invalid_type, Type}}).
+enc_type(_, Type) -> invalid_type(Type).
-spec enc_protocol(Type, Protocol) -> non_neg_integer() |
{raw, non_neg_integer()} when
Type :: type(),
Protocol :: protocol().
-enc_protocol(dgram, ip) -> ?SOCKET_PROTOCOL_IP;
-enc_protocol(stream, tcp) -> ?SOCKET_PROTOCOL_TCP;
-enc_protocol(dgram, udp) -> ?SOCKET_PROTOCOL_UDP;
-enc_protocol(seqpacket, sctp) -> ?SOCKET_PROTOCOL_SCTP;
-enc_protocol(raw, icmp) -> ?SOCKET_PROTOCOL_ICMP;
-enc_protocol(raw, igmp) -> ?SOCKET_PROTOCOL_IGMP;
+enc_protocol(_, default) -> ?SOCKET_PROTOCOL_DEFAULT;
+enc_protocol(dgram, ip) -> ?SOCKET_PROTOCOL_IP;
+enc_protocol(stream, tcp) -> ?SOCKET_PROTOCOL_TCP;
+enc_protocol(dgram, udp) -> ?SOCKET_PROTOCOL_UDP;
+enc_protocol(seqpacket, sctp) -> ?SOCKET_PROTOCOL_SCTP;
+enc_protocol(raw, icmp) -> ?SOCKET_PROTOCOL_ICMP;
+enc_protocol(raw, igmp) -> ?SOCKET_PROTOCOL_IGMP;
enc_protocol(raw, {raw, P} = RAW) when is_integer(P) -> RAW;
enc_protocol(Type, Proto) ->
- throw({error, {invalid_protocol, {Type, Proto}}}).
+ invalid_protocol(Type, Proto).
-spec enc_send_flags(Flags) -> non_neg_integer() when
@@ -3510,6 +3515,25 @@ tdiff(T1, T2) ->
%%
%% ===========================================================================
+-spec invalid_domain(Domain) -> no_return() when
+ Domain :: term().
+
+invalid_domain(Domain) ->
+ error({invalid_domain, Domain}).
+
+-spec invalid_type(Type) -> no_return() when
+ Type :: term().
+
+invalid_type(Type) ->
+ error({invalid_type, Type}).
+
+-spec invalid_protocol(Type, Proto) -> no_return() when
+ Type :: term(),
+ Proto :: term().
+
+invalid_protocol(Type, Proto) ->
+ error({invalid_protocol, {Type, Proto}}).
+
-spec not_supported(What) -> no_return() when
What :: term().