Skip to content

Commit

Permalink
Add a --dbg-listen argument for use in test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
daghf committed Oct 23, 2020
1 parent 0d4ab8e commit c8409a2
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 37 deletions.
6 changes: 6 additions & 0 deletions src/configuration.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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' },
Expand Down Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions src/configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
44 changes: 44 additions & 0 deletions src/hitch.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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();

Expand Down
41 changes: 4 additions & 37 deletions src/tests/hitch_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ start_hitch() {
--pidfile="$TEST_TMPDIR/hitch.pid" \
--log-filename=hitch.log \
--daemon \
--dbg-listen="$TEST_TMPDIR/hitch_hosts" \
$HITCH_USER \
"$@"
}
Expand Down Expand Up @@ -197,54 +198,20 @@ 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
#
# Print a list of hosts for the daemon started with `start_hitch`, usually in
# 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?"
}

#-
Expand Down

0 comments on commit c8409a2

Please sign in to comment.