From a4b8ff911b4abfa1c2247c91d1507687459bab68 Mon Sep 17 00:00:00 2001 From: Simon Tatham Date: Mon, 13 Sep 2021 14:34:46 +0100 Subject: [PATCH] FdSocket, HandleSocket: store a notional peer address. In the case where these socket types are constructed because of a local proxy command, we do actually have a SockAddr representing the logical host we were trying to make a connection to. So we might as well store it in the socket implementation, and then we can include it in the PLUGLOG_CONNECT_SUCCESS call to make the log message more informative. --- unix/fd-socket.c | 13 +++++++++++-- unix/local-proxy.c | 5 +---- unix/platform.h | 3 ++- unix/psusan.c | 2 +- unix/uppity.c | 2 +- windows/handle-socket.c | 12 ++++++++++-- windows/local-proxy.c | 5 +---- windows/named-pipe-client.c | 3 ++- windows/named-pipe-server.c | 2 +- windows/platform.h | 3 ++- 10 files changed, 32 insertions(+), 18 deletions(-) diff --git a/unix/fd-socket.c b/unix/fd-socket.c index afd37957..d352d8e4 100644 --- a/unix/fd-socket.c +++ b/unix/fd-socket.c @@ -23,6 +23,8 @@ typedef struct FdSocket { int pending_error; + SockAddr *addr; + int port; Plug *plug; Socket sock; @@ -134,6 +136,9 @@ static void fdsocket_close(Socket *s) bufchain_clear(&fds->pending_input_data); bufchain_clear(&fds->pending_output_data); + if (fds->addr) + sk_addr_free(fds->addr); + delete_callbacks_for_context(fds); sfree(fds); @@ -318,15 +323,19 @@ static const SocketVtable FdSocket_sockvt = { static void fdsocket_connect_success_callback(void *ctx) { FdSocket *fds = (FdSocket *)ctx; - plug_log(fds->plug, PLUGLOG_CONNECT_SUCCESS, NULL, 0, NULL, 0); + plug_log(fds->plug, PLUGLOG_CONNECT_SUCCESS, fds->addr, fds->port, + NULL, 0); } -Socket *make_fd_socket(int infd, int outfd, int inerrfd, Plug *plug) +Socket *make_fd_socket(int infd, int outfd, int inerrfd, + SockAddr *addr, int port, Plug *plug) { FdSocket *fds; fds = snew(FdSocket); fds->sock.vt = &FdSocket_sockvt; + fds->addr = addr; + fds->port = port; fds->plug = plug; fds->outgoingeof = EOF_NO; fds->pending_error = 0; diff --git a/unix/local-proxy.c b/unix/local-proxy.c index 0a637bd9..c8663928 100644 --- a/unix/local-proxy.c +++ b/unix/local-proxy.c @@ -98,8 +98,5 @@ Socket *platform_new_connection(SockAddr *addr, const char *hostname, inerrfd = -1; } - /* We are responsible for this and don't need it any more */ - sk_addr_free(addr); - - return make_fd_socket(infd, outfd, inerrfd, plug); + return make_fd_socket(infd, outfd, inerrfd, addr, port, plug); } diff --git a/unix/platform.h b/unix/platform.h index 6d88d84b..97762f3f 100644 --- a/unix/platform.h +++ b/unix/platform.h @@ -380,7 +380,8 @@ bool so_peercred(int fd, int *pid, int *uid, int *gid); /* * uxfdsock.c. */ -Socket *make_fd_socket(int infd, int outfd, int inerrfd, Plug *plug); +Socket *make_fd_socket(int infd, int outfd, int inerrfd, + SockAddr *addr, int port, Plug *plug); /* * Default font setting, which can vary depending on NOT_X_WINDOWS. diff --git a/unix/psusan.c b/unix/psusan.c index a9312c40..2017685b 100644 --- a/unix/psusan.c +++ b/unix/psusan.c @@ -411,7 +411,7 @@ int main(int argc, char **argv) } else { struct server_instance *inst; Plug *plug = server_conn_plug(&scfg, &inst); - ssh_server_start(plug, make_fd_socket(0, 1, -1, plug)); + ssh_server_start(plug, make_fd_socket(0, 1, -1, NULL, 0, plug)); log_to_stderr(inst->id, "running directly on stdio"); } diff --git a/unix/uppity.c b/unix/uppity.c index 4189c7e0..8d731492 100644 --- a/unix/uppity.c +++ b/unix/uppity.c @@ -869,7 +869,7 @@ int main(int argc, char **argv) } else { struct server_instance *inst; Plug *plug = server_conn_plug(&scfg, &inst); - ssh_server_start(plug, make_fd_socket(0, 1, -1, plug)); + ssh_server_start(plug, make_fd_socket(0, 1, -1, NULL, 0, plug)); log_to_stderr(inst->id, "speaking SSH on stdio"); } diff --git a/windows/handle-socket.c b/windows/handle-socket.c index 77b9067c..dbc4e0de 100644 --- a/windows/handle-socket.c +++ b/windows/handle-socket.c @@ -40,6 +40,8 @@ typedef struct HandleSocket { char *error; + SockAddr *addr; + int port; Plug *plug; Socket sock; @@ -128,6 +130,9 @@ static void sk_handle_close(Socket *s) CloseHandle(hs->recv_H); bufchain_clear(&hs->inputdata); + if (hs->addr) + sk_addr_free(hs->addr); + delete_callbacks_for_context(hs); sfree(hs); @@ -317,17 +322,20 @@ static const SocketVtable HandleSocket_sockvt = { static void sk_handle_connect_success_callback(void *ctx) { HandleSocket *hs = (HandleSocket *)ctx; - plug_log(hs->plug, PLUGLOG_CONNECT_SUCCESS, NULL, 0, NULL, 0); + plug_log(hs->plug, PLUGLOG_CONNECT_SUCCESS, hs->addr, hs->port, NULL, 0); } Socket *make_handle_socket(HANDLE send_H, HANDLE recv_H, HANDLE stderr_H, - Plug *plug, bool overlapped) + SockAddr *addr, int port, Plug *plug, + bool overlapped) { HandleSocket *hs; int flags = (overlapped ? HANDLE_FLAG_OVERLAPPED : 0); hs = snew(HandleSocket); hs->sock.vt = &HandleSocket_sockvt; + hs->addr = addr; + hs->port = port; hs->plug = plug; hs->error = NULL; hs->frozen = UNFROZEN; diff --git a/windows/local-proxy.c b/windows/local-proxy.c index 94e31fcb..ddbdb97c 100644 --- a/windows/local-proxy.c +++ b/windows/local-proxy.c @@ -30,9 +30,6 @@ Socket *platform_new_connection(SockAddr *addr, const char *hostname, cmd = format_telnet_command(addr, port, conf); - /* We are responsible for this and don't need it any more */ - sk_addr_free(addr); - { char *msg = dupprintf("Starting local proxy command: %s", cmd); plug_log(plug, PLUGLOG_PROXY_MSG, NULL, 0, msg, 0); @@ -103,5 +100,5 @@ Socket *platform_new_connection(SockAddr *addr, const char *hostname, CloseHandle(cmd_err_to_us); return make_handle_socket(us_to_cmd, us_from_cmd, us_from_cmd_err, - plug, false); + addr, port, plug, false); } diff --git a/windows/named-pipe-client.c b/windows/named-pipe-client.c index bd43998a..b827dd0d 100644 --- a/windows/named-pipe-client.c +++ b/windows/named-pipe-client.c @@ -90,5 +90,6 @@ Socket *new_named_pipe_client(const char *pipename, Plug *plug) if (pipehandle == INVALID_HANDLE_VALUE) return new_error_socket_consume_string(plug, err); else - return make_handle_socket(pipehandle, pipehandle, NULL, plug, true); + return make_handle_socket(pipehandle, pipehandle, NULL, NULL, 0, + plug, true); } diff --git a/windows/named-pipe-server.c b/windows/named-pipe-server.c index a272cdad..a9271c16 100644 --- a/windows/named-pipe-server.c +++ b/windows/named-pipe-server.c @@ -112,7 +112,7 @@ static Socket *named_pipe_accept(accept_ctx_t ctx, Plug *plug) { HANDLE conn = (HANDLE)ctx.p; - return make_handle_socket(conn, conn, NULL, plug, true); + return make_handle_socket(conn, conn, NULL, NULL, 0, plug, true); } static void named_pipe_accept_loop(NamedPipeServerSocket *ps, diff --git a/windows/platform.h b/windows/platform.h index f549896d..ba07ea1a 100644 --- a/windows/platform.h +++ b/windows/platform.h @@ -341,7 +341,8 @@ extern HANDLE winselcli_event; * Network-subsystem-related functions provided in other Windows modules. */ Socket *make_handle_socket(HANDLE send_H, HANDLE recv_H, HANDLE stderr_H, - Plug *plug, bool overlapped); /* winhsock */ + SockAddr *addr, int port, Plug *plug, + bool overlapped); /* winhsock */ Socket *new_named_pipe_client(const char *pipename, Plug *plug); /* winnpc */ Socket *new_named_pipe_listener(const char *pipename, Plug *plug); /* winnps */