From 8f21a5da81d6553a314b7b52981edaac4ca517ff Mon Sep 17 00:00:00 2001 From: lemoer Date: Wed, 18 May 2016 21:20:47 +0200 Subject: [PATCH 1/4] pong handler: add sanity check for rtt --- src/protocol_misc.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/protocol_misc.c b/src/protocol_misc.c index b2695436..154f2c85 100644 --- a/src/protocol_misc.c +++ b/src/protocol_misc.c @@ -137,6 +137,12 @@ bool pong_h(connection_t *c, const char *request) { tv_usec = c->last_ping_time.tv_usec; } + if (_now.tv_sec - tv_sec > 2*pingtimeout) { + /* timeout_handler should close the meta connection after pingtimeout. + * So if we still receive such pong, something is fishy. */ + logger(DEBUG_ALWAYS, LOG_ERR, "bogus pong received from %s (%s)", c->name, c->hostname); + return false; + } /* RTT should be in ms */ current_rtt = (_now.tv_sec - tv_sec)*1000; /* Compute diff between usec */ From 25317983c72e1fb34666d12ccc5ccbada35ab281 Mon Sep 17 00:00:00 2001 From: lemoer Date: Wed, 18 May 2016 21:22:03 +0200 Subject: [PATCH 2/4] pong handler: add warning if we receive pong without ping --- src/protocol_misc.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/protocol_misc.c b/src/protocol_misc.c index 154f2c85..8e6b1de7 100644 --- a/src/protocol_misc.c +++ b/src/protocol_misc.c @@ -126,6 +126,12 @@ bool pong_h(connection_t *c, const char *request) { int current_rtt = 0; int tv_sec, tv_usec, ret; struct timeval _now; + + if (!c->status.pinged) { + logger(DEBUG_ALWAYS, LOG_WARNING, "received pong without ping"); + return false; + } + c->status.pinged = false; ret = sscanf(request, "%*d %d %d", &tv_sec, &tv_usec); From 762dc69a59900ee0ebacce07a32a04ab22d760b0 Mon Sep 17 00:00:00 2001 From: lemoer Date: Wed, 18 May 2016 21:25:31 +0200 Subject: [PATCH 3/4] pong handler: rtt and timestamps are always positive --- src/protocol_misc.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/protocol_misc.c b/src/protocol_misc.c index 8e6b1de7..7480f4d8 100644 --- a/src/protocol_misc.c +++ b/src/protocol_misc.c @@ -123,8 +123,9 @@ bool send_pong(connection_t *c) { } bool pong_h(connection_t *c, const char *request) { - int current_rtt = 0; - int tv_sec, tv_usec, ret; + int ret; + long current_rtt = 0; + long tv_sec, tv_usec; struct timeval _now; if (!c->status.pinged) { @@ -134,7 +135,7 @@ bool pong_h(connection_t *c, const char *request) { c->status.pinged = false; - ret = sscanf(request, "%*d %d %d", &tv_sec, &tv_usec); + ret = sscanf(request, "%*d %ld %ld", &tv_sec, &tv_usec); gettimeofday(&_now, NULL); if (ret != 2) { From 532b9ca451311639ca70276fac52c3776c1b2e5b Mon Sep 17 00:00:00 2001 From: lemoer Date: Wed, 18 May 2016 21:32:24 +0200 Subject: [PATCH 4/4] pong handler: fix incorrect calculation for rtt Before this fix seconds were treated as milliseconds. Example: A ping of 1.001s resulted in current_rtt = 2 (ms) where a ping of 0.999s resulted in current_rtt = 999 (ms). --- src/protocol_misc.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/protocol_misc.c b/src/protocol_misc.c index 7480f4d8..91bb28c0 100644 --- a/src/protocol_misc.c +++ b/src/protocol_misc.c @@ -150,12 +150,11 @@ bool pong_h(connection_t *c, const char *request) { logger(DEBUG_ALWAYS, LOG_ERR, "bogus pong received from %s (%s)", c->name, c->hostname); return false; } - /* RTT should be in ms */ - current_rtt = (_now.tv_sec - tv_sec)*1000; - /* Compute diff between usec */ - current_rtt += _now.tv_usec >= tv_usec ? _now.tv_usec - tv_usec : tv_usec - _now.tv_usec; - current_rtt = current_rtt/1000; + /* current_rtt should be in ms */ + current_rtt += (_now.tv_sec - tv_sec) * 1000; + /* Compute diff between usec */ + current_rtt += (_now.tv_usec - tv_usec) / 1000; if (c->edge->avg_rtt == 0) c->edge->avg_rtt = current_rtt;