From c8409a2d18f59127a92d90d877478570f6a2dbd8 Mon Sep 17 00:00:00 2001 From: Dag Haavi Finstad Date: Fri, 23 Oct 2020 16:59:44 +0200 Subject: [PATCH] Add a --dbg-listen argument for use in test cases --- src/configuration.c | 6 ++++++ src/configuration.h | 1 + src/hitch.c | 44 +++++++++++++++++++++++++++++++++++++++++ src/tests/hitch_test.sh | 41 ++++---------------------------------- 4 files changed, 55 insertions(+), 37 deletions(-) diff --git a/src/configuration.c b/src/configuration.c index 0a33e7a..4c6bdcb 100644 --- a/src/configuration.c +++ b/src/configuration.c @@ -79,6 +79,8 @@ #define CFG_OCSP_DIR "ocsp-dir" #define CFG_TLS_PROTOS "tls-protos" #define CFG_PARAM_TLS_PROTOS 11018 +#define CFG_DBG_LISTEN "dbg-listen" +#define CFG_PARAM_DBG_LISTEN 11019 #ifdef TCP_FASTOPEN_WORKS #define CFG_TFO "enable-tcp-fastopen" #endif @@ -1045,6 +1047,8 @@ config_param_validate(const char *k, char *v, hitch_config *cfg, config_error_set("Invalid 'tls-protos' option '%s'", v); return (1); } + } else if (strcmp(k, CFG_DBG_LISTEN) == 0) { + config_assign_str(&cfg->DEBUG_LISTEN_ADDR, v); } else { fprintf( stderr, @@ -1528,6 +1532,7 @@ config_parse_cli(int argc, char **argv, hitch_config *cfg) { CFG_SNI_NOMATCH_ABORT, 2, NULL, 1 }, { CFG_OCSP_DIR, 1, NULL, 'o' }, { CFG_TLS_PROTOS, 1, NULL, CFG_PARAM_TLS_PROTOS }, + { CFG_DBG_LISTEN, 1, NULL, CFG_PARAM_DBG_LISTEN }, { "test", 0, NULL, 't' }, { "version", 0, NULL, 'V' }, { "help", 0, NULL, 'h' }, @@ -1597,6 +1602,7 @@ CFG_ARG(CFG_PARAM_SEND_BUFSIZE, CFG_SEND_BUFSIZE); CFG_ARG(CFG_PARAM_RECV_BUFSIZE, CFG_RECV_BUFSIZE); CFG_ARG(CFG_PARAM_ALPN_PROTOS, CFG_ALPN_PROTOS); CFG_ARG(CFG_PARAM_TLS_PROTOS, CFG_TLS_PROTOS); +CFG_ARG(CFG_PARAM_DBG_LISTEN, CFG_DBG_LISTEN); CFG_ARG('c', CFG_CIPHERS); CFG_ARG('e', CFG_SSL_ENGINE); CFG_ARG('b', CFG_BACKEND); diff --git a/src/configuration.h b/src/configuration.h index 7425617..2e84a4e 100644 --- a/src/configuration.h +++ b/src/configuration.h @@ -150,6 +150,7 @@ struct __hitch_config { double OCSP_RESP_TMO; double OCSP_CONN_TMO; int OCSP_REFRESH_INTERVAL; + char *DEBUG_LISTEN_ADDR; #ifdef TCP_FASTOPEN_WORKS int TFO; #endif diff --git a/src/hitch.c b/src/hitch.c index a99b26b..c5b90d4 100644 --- a/src/hitch.c +++ b/src/hitch.c @@ -3991,6 +3991,42 @@ notify_workers(struct worker_update *wu) } } +/* + * Print Hitch's listen enpoints to a file. + * Used for testing purposes. + */ +int +listen_endpoint_print(const char *fn) +{ + FILE *fp; + int n; + struct frontend *fr; + struct addrinfo *it; + char hostbuf[NI_MAXHOST]; + char servbuf[NI_MAXSERV]; + + fp = fopen(fn, "w"); + if (fp == NULL) { + perror("fopen"); + return (1); + } + + VTAILQ_FOREACH(fr, &frontends, list) { + for (it = fr->addrs; it != NULL; it = it->ai_next) { + if (it->ai_addr->sa_family != AF_INET) + continue; + n = getnameinfo(it->ai_addr, it->ai_addrlen, + hostbuf, sizeof(hostbuf), servbuf, sizeof(servbuf), + NI_NUMERICHOST|NI_NUMERICSERV); + AZ(n); + fprintf(fp, "%s:%s\n", hostbuf, servbuf); + } + } + + fclose(fp); + return (0); +} + static void reconfigure(int argc, char **argv) { @@ -4077,6 +4113,9 @@ reconfigure(int argc, char **argv) worker_gen++; start_workers(0, CONFIG->NCORES); + if (CONFIG->DEBUG_LISTEN_ADDR) + listen_endpoint_print(CONFIG->DEBUG_LISTEN_ADDR); + wu.type = WORKER_GEN; wu.payload.gen = worker_gen; notify_workers(&wu); @@ -4119,6 +4158,7 @@ sleep_and_refresh(hitch_config *CONFIG) } } + /* Process command line args, create the bound socket, * spawn child (worker) processes, and respawn if any die */ int @@ -4240,6 +4280,10 @@ main(int argc, char **argv) start_workers(0, CONFIG->NCORES); + if (CONFIG->DEBUG_LISTEN_ADDR) { + listen_endpoint_print(CONFIG->DEBUG_LISTEN_ADDR); + } + if (CONFIG->OCSP_DIR != NULL) start_ocsp_proc(); diff --git a/src/tests/hitch_test.sh b/src/tests/hitch_test.sh index 1f1ad02..904a52f 100755 --- a/src/tests/hitch_test.sh +++ b/src/tests/hitch_test.sh @@ -163,6 +163,7 @@ start_hitch() { --pidfile="$TEST_TMPDIR/hitch.pid" \ --log-filename=hitch.log \ --daemon \ + --dbg-listen="$TEST_TMPDIR/hitch_hosts" \ $HITCH_USER \ "$@" } @@ -197,16 +198,6 @@ hitch_pid() { cat "$TEST_TMPDIR/hitch.pid" } -# -# Usage: hitch_cld_pid -# -# Print the PID of a hitch child process -# - -hitch_cld_pid() { - pgrep -P "$(hitch_pid)" | head -1 -} - #- # Usage: hitch_hosts # @@ -214,37 +205,13 @@ hitch_cld_pid() { # a loop. Only IPv4 listen addresses are listed. hitch_hosts() { - if cmd lsof - then - lsof -F -P -n -a -p "$(hitch_cld_pid)" -i 4 -i TCP -s TCP:LISTEN | - awk '/^n/ { - sub("\\*", "127.0.0.1", $1) - print substr($1,2) - }' - return - fi - - if cmd sockstat && test "$(uname)" = FreeBSD - then - sockstat -P tcp -4 | - awk '$3 == '"$(hitch_cld_pid)"' { - sub("\\*", "127.0.0.1", $6) - print $6 - }' - return - fi - - if cmd fstat && test "$(uname)" = OpenBSD + if [ -f "$TEST_TMPDIR/hitch_hosts" ] then - fstat -p "$(hitch_cld_pid)" | - awk '$5 == "internet" && $7 == "tcp" && NF == 9 { - sub("\\*", "127.0.0.1", $9) - print $9 - }' + cat "$TEST_TMPDIR/hitch_hosts" return fi - fail "none of supported lsof, sockstat or fstat available" + fail "$TEST_TMPDIR/hitch_hosts not found. Hitch started without --dbg-listen?" } #-