From 860631f910be9e2a19d6f520ec57bbe4e7c923e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yves=20R=C5=B1tschl=C3=A9?= Date: Mon, 11 Aug 2025 09:37:57 +0200 Subject: [PATCH] refactor: move out forking code --- tcp-listener.c | 59 +++++++++++++++++++++++++------------------------- 1 file changed, 29 insertions(+), 30 deletions(-) diff --git a/tcp-listener.c b/tcp-listener.c index a057d5d..5fb100a 100644 --- a/tcp-listener.c +++ b/tcp-listener.c @@ -294,6 +294,32 @@ static void connect_proxy(struct connection *cnx) exit(0); } + +/* Forks a shoveler process for one protocol */ +void fork_shoveling_process(struct loop_info* fd_info, struct connection* cnx) { + pid_t pid; + switch (pid = fork()) { + case 0: /* child */ + /* TODO: close all file descriptors except 2 */ + /* free(cnx); */ + connect_proxy(cnx); + exit(0); + case -1: print_message(msg_system_error, "fork failed: err %d: %s\n", errno, strerror(errno)); + break; + default: /* parent */ + struct pid2proto* pid2proto = malloc(sizeof(*pid2proto)); + pid2proto->pid = pid; + pid2proto->proto = cnx->proto; + if (hash_insert(fd_info->pid2proto, pid2proto)) { + /* TODO something if it fails */ + } + break; + } + /* Free file descriptor, but do not reduce protocol count */ + cnx->proto = NULL; + tidy_connection(cnx, fd_info); +} + /* Process read activity on a socket in probe state * IN/OUT cnx: connection data, updated if connected * IN/OUT info: updated if connected @@ -301,17 +327,13 @@ static void connect_proxy(struct connection *cnx) void probing_read_process(struct connection* cnx, struct loop_info* fd_info) { - int res; - /* If timed out it's SSH, otherwise the client sent * data so probe the protocol */ if ((cnx->probe_timeout < time(NULL))) { cnx->proto = timeout_protocol(); print_message(msg_fd, "timed out, connect to %s\n", cnx->proto->name); } else { - res = probe_client_protocol(cnx); - if (res == PROBE_AGAIN) - return; + probe_client_protocol(cnx); } remove_probing_cnx(fd_info, cnx); @@ -326,33 +348,10 @@ void probing_read_process(struct connection* cnx, if (cnx->proto->service && check_access_rights(cnx->q[0].fd, cnx->proto->service)) { tidy_connection(cnx, fd_info); - res = -1; } else if (cnx->proto->fork) { - pid_t pid; - switch (pid = fork()) { - case 0: /* child */ - /* TODO: close all file descriptors except 2 */ - /* free(cnx); */ - connect_proxy(cnx); - exit(0); - case -1: print_message(msg_system_error, "fork failed: err %d: %s\n", errno, strerror(errno)); - break; - default: /* parent */ - struct pid2proto* pid2proto = malloc(sizeof(*pid2proto)); - pid2proto->pid = pid; - pid2proto->proto = cnx->proto; - if (hash_insert(fd_info->pid2proto, pid2proto)) { - /* TODO something if it fails */ - } - break; - } - /* Free file descriptor, but do not reduce protocol count */ - cnx->proto = NULL; - tidy_connection(cnx, fd_info); - - res = -1; + fork_shoveling_process(fd_info, cnx); } else { - res = connect_queue(cnx, fd_info); + connect_queue(cnx, fd_info); } }