Skip to content

Commit

Permalink
Merge branch 'bmk/esock/20240926/esock_on_win_with_only_ipv6/OTP-1925…
Browse files Browse the repository at this point in the history
…4' into maint-27

* bmk/esock/20240926/esock_on_win_with_only_ipv6/OTP-19254:
  [esock] Renamed utility socket
  [esock] Try IPv6 socket also
  • Loading branch information
Erlang/OTP committed Sep 30, 2024
2 parents 4d2ea7c + 43fbfad commit ecfaf57
Showing 1 changed file with 64 additions and 22 deletions.
86 changes: 64 additions & 22 deletions erts/emulator/nifs/win32/win_socket_asyncio.c
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,10 @@ typedef struct {
WSADATA wsaData;
HANDLE cport;

SOCKET dummy; // Used for extracting AcceptEx and ConnectEx
/* Used during initiation
* for extracting AcceptEx and ConnectEx
*/
SOCKET srvInit;

/* Extension functions */
LPFN_ACCEPTEX accept;
Expand Down Expand Up @@ -474,6 +477,9 @@ typedef struct __ESAIOOperation {
* *
* =================================================================== */

static
BOOLEAN_T init_srv_init_socket(int* savedErrno);

static ERL_NIF_TERM esaio_connect_stream(ErlNifEnv* env,
ESockDescriptor* descP,
ERL_NIF_TERM sockRef,
Expand Down Expand Up @@ -1198,15 +1204,13 @@ int esaio_init(unsigned int numThreads,
}


/* Create the "dummy" socket and then
/* Create the "service init" socket and then
* extract the AcceptEx and ConnectEx functions.
*/
SGDBG( ("WIN-ESAIO", "esaio_init -> try create 'dummy' socket\r\n") );
ctrl.dummy = sock_open(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (ctrl.dummy == INVALID_SOCKET) {
save_errno = sock_errno();
SGDBG( ("WIN-ESAIO", "esaio_init -> try create 'service init' socket\r\n") );
if ( !init_srv_init_socket(&save_errno) ) {

esock_error_msg("Failed create 'dummy' socket: "
esock_error_msg("Failed create 'service init' socket: "
"\r\n %s (%d)"
"\r\n",
erl_errno_id(save_errno), save_errno);
Expand All @@ -1226,7 +1230,7 @@ int esaio_init(unsigned int numThreads,
* rather than refer to the Mswsock.lib library.
*/
SGDBG( ("WIN-ESAIO", "esaio_init -> try extract 'accept' function\r\n") );
ires = WSAIoctl(ctrl.dummy, SIO_GET_EXTENSION_FUNCTION_POINTER,
ires = WSAIoctl(ctrl.srvInit, SIO_GET_EXTENSION_FUNCTION_POINTER,
&guidAcceptEx, sizeof (guidAcceptEx),
&ctrl.accept, sizeof (ctrl.accept),
&dummy, NULL, NULL);
Expand All @@ -1238,8 +1242,8 @@ int esaio_init(unsigned int numThreads,
"\r\n",
ires, erl_errno_id(save_errno), save_errno);

(void) sock_close(ctrl.dummy);
ctrl.dummy = INVALID_SOCKET;
(void) sock_close(ctrl.srvInit);
ctrl.srvInit = INVALID_SOCKET;
ctrl.accept = NULL;

WSACleanup();
Expand All @@ -1250,7 +1254,7 @@ int esaio_init(unsigned int numThreads,

/* Basically the same as for AcceptEx above */
SGDBG( ("WIN-ESAIO", "esaio_init -> try extract 'connect' function\r\n") );
ires = WSAIoctl(ctrl.dummy, SIO_GET_EXTENSION_FUNCTION_POINTER,
ires = WSAIoctl(ctrl.srvInit, SIO_GET_EXTENSION_FUNCTION_POINTER,
&guidConnectEx, sizeof (guidConnectEx),
&ctrl.connect, sizeof (ctrl.connect),
&dummy, NULL, NULL);
Expand All @@ -1262,8 +1266,8 @@ int esaio_init(unsigned int numThreads,
"\r\n",
ires, erl_errno_id(save_errno), save_errno);

(void) sock_close(ctrl.dummy);
ctrl.dummy = INVALID_SOCKET;
(void) sock_close(ctrl.srvInit);
ctrl.srvInit = INVALID_SOCKET;
ctrl.accept = NULL;

WSACleanup();
Expand All @@ -1274,7 +1278,7 @@ int esaio_init(unsigned int numThreads,

/* Basically the same as for AcceptEx above */
SGDBG( ("WIN-ESAIO", "esaio_init -> try extract 'sendmsg' function\r\n") );
ires = WSAIoctl(ctrl.dummy, SIO_GET_EXTENSION_FUNCTION_POINTER,
ires = WSAIoctl(ctrl.srvInit, SIO_GET_EXTENSION_FUNCTION_POINTER,
&guidSendMsg, sizeof (guidSendMsg),
&ctrl.sendmsg, sizeof (ctrl.sendmsg),
&dummy, NULL, NULL);
Expand All @@ -1286,8 +1290,8 @@ int esaio_init(unsigned int numThreads,
"\r\n",
ires, erl_errno_id(save_errno), save_errno);

(void) sock_close(ctrl.dummy);
ctrl.dummy = INVALID_SOCKET;
(void) sock_close(ctrl.srvInit);
ctrl.srvInit = INVALID_SOCKET;
ctrl.accept = NULL;
ctrl.connect = NULL;

Expand All @@ -1299,7 +1303,7 @@ int esaio_init(unsigned int numThreads,

/* Basically the same as for AcceptEx above */
SGDBG( ("WIN-ESAIO", "esaio_init -> try extract 'recvmsg' function\r\n") );
ires = WSAIoctl(ctrl.dummy, SIO_GET_EXTENSION_FUNCTION_POINTER,
ires = WSAIoctl(ctrl.srvInit, SIO_GET_EXTENSION_FUNCTION_POINTER,
&guidRecvMsg, sizeof (guidRecvMsg),
&ctrl.recvmsg, sizeof (ctrl.recvmsg),
&dummy, NULL, NULL);
Expand All @@ -1311,8 +1315,8 @@ int esaio_init(unsigned int numThreads,
"\r\n",
ires, erl_errno_id(save_errno), save_errno);

(void) sock_close(ctrl.dummy);
ctrl.dummy = INVALID_SOCKET;
(void) sock_close(ctrl.srvInit);
ctrl.srvInit = INVALID_SOCKET;
ctrl.accept = NULL;
ctrl.connect = NULL;
ctrl.sendmsg = NULL;
Expand Down Expand Up @@ -1404,6 +1408,44 @@ int esaio_init(unsigned int numThreads,
}


static
BOOLEAN_T init_srv_init_socket(int* savedErrno)
{
SOCKET sock;
int save_errno = 0;

sock = sock_open(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sock == INVALID_SOCKET) {
save_errno = sock_errno();

/* This *could* be because we are on a 'IPv6 only' machine
* => So try with that (AF_INET6) domain also.
*/

if (save_errno == WSAEAFNOSUPPORT) {
sock = sock_open(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
if (sock == INVALID_SOCKET) {
/* Ouch, still failing, so we will keep the original error */
ctrl.srvInit = INVALID_SOCKET;
*savedErrno = save_errno;
return FALSE;
} else {
ctrl.srvInit = sock;
*savedErrno = 0;
return TRUE;
}
} else {
ctrl.srvInit = INVALID_SOCKET;
*savedErrno = save_errno;
return FALSE;
}
} else {
ctrl.srvInit = sock;
*savedErrno = 0;
return TRUE;
}
}


/* *******************************************************************
* Finish, terminate, the ESock Async I/O backend.
Expand All @@ -1418,10 +1460,10 @@ void esaio_finish()

SGDBG( ("WIN-ESAIO", "esaio_finish -> entry\r\n") );

if (ctrl.dummy != INVALID_SOCKET) {
if (ctrl.srvInit != INVALID_SOCKET) {
SGDBG( ("WIN-ESAIO", "esaio_finish -> close 'dummy' socket\r\n") );
(void) sock_close(ctrl.dummy);
ctrl.dummy = INVALID_SOCKET;
(void) sock_close(ctrl.srvInit);
ctrl.srvInit = INVALID_SOCKET;
}

SGDBG( ("WIN-ESAIO",
Expand Down

0 comments on commit ecfaf57

Please sign in to comment.