[wslproxy]: survive closed socket, close socket correctly.

1.When remote side socket is closed, `SIGPIPE` might be sent. We must ignore and exit correctly (we can't die as we might accept more connections)

2. shutdown and close sockets will inform remote side that socket is closed. Without it, remote side may not notice anything until the next write.

GitOrigin-RevId: 51a6204651e49f63ac8b52d9e3e1e4928007c3ff
This commit is contained in:
Ilya.Kazakevich
2024-05-01 15:36:23 +02:00
committed by intellij-monorepo-bot
parent 25f08fe587
commit ae0ed41348
3 changed files with 13 additions and 1 deletions

Binary file not shown.

Binary file not shown.

View File

@@ -8,6 +8,7 @@
#include <strings.h>
#include <errno.h>
#include <netinet/tcp.h>
#include <signal.h>
// See svg file and wslproxy_test_client.py
@@ -129,13 +130,20 @@ static void *jb_connect_pair(jb_sockpair *sockpair) {
while (sent < bytes) {
if ((write_result = write(dest, buf + sent, bytes - sent)) < 0) {
if (errno != EINTR || errno != EAGAIN) {
break; //socket closed
goto end; //socket closed
}
}
sent += write_result;
}
}
end:
shutdown(source, SHUT_WR);
close(source);
shutdown(dest, SHUT_WR);
close(dest);
return NULL;
}
@@ -189,6 +197,10 @@ _Noreturn static void *jb_listen_ingress(const int *p_ingress_srv_sock_fd) {
static int g_ingress_srv_sock_fd;
int main(int argc, char **argv) {
// We expect write failures to occur but we want to handle them where
// the error occurs rather than in a SIGPIPE handler.
signal(SIGPIPE, SIG_IGN);
// '--loopback' means use 127.0.0.1 as egress IP
g_egress_ip = (argc > 1 && strcmp(&argv[1][0], "--loopback") == 0) ? htonl(INADDR_LOOPBACK)
: jb_get_wsl_public_ip();