diff options
author | Toni Uhlig <matzeton@googlemail.com> | 2020-10-26 17:16:51 +0100 |
---|---|---|
committer | Toni Uhlig <matzeton@googlemail.com> | 2020-10-26 17:16:51 +0100 |
commit | b2540ad3304f13306cff98c29992b52a6f6d03b6 (patch) | |
tree | 98ee5974826eede7259138af2b5a735cd42a2844 | |
parent | b85fe6e68f7e46dbea7fedff47da06d05966ebfb (diff) |
Fixed latency calculation.
- replaced more __attribute__((warn_unused_result)) with WARN_UNUSED_RESULT
- do not use remote last_p(i|o)ng_recv for latency calculation is that make no sense at all
- static' some functions
Signed-off-by: Toni Uhlig <matzeton@googlemail.com>
-rw-r--r-- | client.c | 7 | ||||
-rw-r--r-- | common-event2.c | 24 | ||||
-rw-r--r-- | common-event2.h | 33 | ||||
-rw-r--r-- | protocol.c | 8 | ||||
-rw-r--r-- | protocol.h | 7 | ||||
-rw-r--r-- | server.c | 7 |
6 files changed, 46 insertions, 40 deletions
@@ -110,9 +110,6 @@ enum recv_return protocol_request_ping(struct connection * const state, strftime_local(ts, ts_str, sizeof(ts_str)); LOG(NOTICE, "Received PING with timestamp %.09lfs: %s / %lluns", ts, ts_str, extract_nsecs(ts)); - if (state->latency > 0.0f) { - LOG(NOTICE, "PING-PONG latency: %.09lfs", state->latency); - } if (ev_protocol_pong(state) != 0) { return RECV_FATAL_CALLBACK_ERROR; @@ -134,6 +131,10 @@ enum recv_return protocol_request_pong(struct connection * const state, LOG(NOTICE, "Received PONG with timestamp %.09lfs: %s / %lluns / %zu outstanding PONG's", ts, ts_str, extract_nsecs(ts), state->awaiting_pong); + if (state->latency > 0.0) { + LOG(NOTICE, "PING-PONG latency: %.09lfs", state->latency); + } + if (state->awaiting_pong > 3) { LOG(ERROR, "Waiting for more than 3 PONG's, disconnecting.."); return RECV_FATAL_CALLBACK_ERROR; diff --git a/common-event2.c b/common-event2.c index 28e28ad..22f3fe6 100644 --- a/common-event2.c +++ b/common-event2.c @@ -6,17 +6,15 @@ #include "common-event2.h" #include "logging.h" -#include "protocol.h" #include "utils.h" -int ev_auth_timeout(struct ev_user_data * const user_data) +static void ev_auth_timeout(struct ev_user_data * const user_data) { LOG(NOTICE, "Authentication timeout"); ev_disconnect(user_data->state); - return 0; } -int ev_add_timer(struct ev_user_data * const user_data, time_t trigger_after) +static int ev_add_timer(struct ev_user_data * const user_data, time_t trigger_after) { struct timeval tv; @@ -26,9 +24,9 @@ int ev_add_timer(struct ev_user_data * const user_data, time_t trigger_after) return event_add(user_data->generic_timer, &tv); } -int ev_del_timer(struct ev_user_data * const user_data) +static void ev_del_timer(struct ev_user_data * const user_data) { - return event_del(user_data->generic_timer); + event_del(user_data->generic_timer); } static double time_passed(double last_time) @@ -212,8 +210,11 @@ int ev_protocol_ping(struct connection * const state) protocol_response_ping(ping_pkt_crypted, state); strftime_local(state->last_ping_send, timestamp, sizeof(timestamp)); - LOG(LP_DEBUG, "Sending PING with ts %.09lf: %s / %uns", - state->last_ping_send, timestamp, extract_nsecs(state->last_ping_send)); + LOG(LP_DEBUG, + "Sending PING with ts %.09lf: %s / %uns", + state->last_ping_send, + timestamp, + extract_nsecs(state->last_ping_send)); result = evbuffer_add(bufferevent_get_output(user_data->bev), ping_pkt_crypted, sizeof(ping_pkt_crypted)); return result; } @@ -227,8 +228,11 @@ int ev_protocol_pong(struct connection * const state) protocol_response_pong(pong_pkt_crypted, state); strftime_local(state->last_pong_send, timestamp, sizeof(timestamp)); - LOG(LP_DEBUG, "Sending PONG with ts %.09lf: %s / %uns", - state->last_pong_send, timestamp, extract_nsecs(state->last_pong_send)); + LOG(LP_DEBUG, + "Sending PONG with ts %.09lf: %s / %uns", + state->last_pong_send, + timestamp, + extract_nsecs(state->last_pong_send)); result = evbuffer_add(bufferevent_get_output(user_data->bev), pong_pkt_crypted, sizeof(pong_pkt_crypted)); return result; } diff --git a/common-event2.h b/common-event2.h index a402e07..05cc1bf 100644 --- a/common-event2.h +++ b/common-event2.h @@ -4,6 +4,8 @@ #include <event2/event.h> #include <stdint.h> +#include "protocol.h" + struct bufferevent; #define AUTHENTICATION_TIMEOUT 10 @@ -19,38 +21,29 @@ struct ev_user_data { extern void on_disconnect(struct connection * const state); -int ev_auth_timeout(struct ev_user_data * const user_data); - -int ev_add_timer(struct ev_user_data * const user_data, time_t trigger_after); - -int ev_del_timer(struct ev_user_data * const user_data); - -__attribute__((warn_unused_result)) int ev_setup_generic_timer(struct ev_user_data * const user_data, - time_t trigger_after); +int WARN_UNUSED_RESULT ev_setup_generic_timer(struct ev_user_data * const user_data, time_t trigger_after); void ev_cleanup_user_data(struct connection * const state); -__attribute__((warn_unused_result)) int ev_setup_user_data(struct bufferevent * const bev, - struct connection * const state); +int WARN_UNUSED_RESULT ev_setup_user_data(struct bufferevent * const bev, struct connection * const state); void ev_set_io_timeouts(struct bufferevent * const bev); void ev_sighandler(evutil_socket_t fd, short events, void * arg); -__attribute__((warn_unused_result)) int ev_protocol_client_auth(struct connection * const state, - const char * const user, - const char * const pass); +int WARN_UNUSED_RESULT ev_protocol_client_auth(struct connection * const state, + const char * const user, + const char * const pass); -__attribute__((warn_unused_result)) int ev_protocol_server_helo(struct connection * const state, - const char * const server_message); +int WARN_UNUSED_RESULT ev_protocol_server_helo(struct connection * const state, const char * const server_message); -__attribute__((warn_unused_result)) int ev_protocol_data(struct connection * const state, - uint8_t const * const payload, - uint32_t payload_size); +int WARN_UNUSED_RESULT ev_protocol_data(struct connection * const state, + uint8_t const * const payload, + uint32_t payload_size); -__attribute__((warn_unused_result)) int ev_protocol_ping(struct connection * const state); +int WARN_UNUSED_RESULT ev_protocol_ping(struct connection * const state); -__attribute__((warn_unused_result)) int ev_protocol_pong(struct connection * const state); +int WARN_UNUSED_RESULT ev_protocol_pong(struct connection * const state); void ev_disconnect(struct connection * const state); @@ -135,14 +135,16 @@ static enum recv_return process_body(struct connection * const state, case TYPE_PING: { struct protocol_ping const * const ping_pkt = (struct protocol_ping *)hdr; - state->last_ping_recv = to_timestamp(be64toh(ping_pkt->timestamp.sec), ntohl(ping_pkt->timestamp.nsec)); + state->last_ping_recv = create_timestamp(); + state->last_ping_recv_remote = to_timestamp(be64toh(ping_pkt->timestamp.sec), ntohl(ping_pkt->timestamp.nsec)); *processed += CRYPT_PACKET_SIZE_PING; break; } case TYPE_PONG: { struct protocol_pong const * const pong_pkt = (struct protocol_pong *)hdr; - state->last_pong_recv = to_timestamp(be64toh(pong_pkt->timestamp.sec), ntohl(pong_pkt->timestamp.nsec)); + state->last_pong_recv = create_timestamp(); + state->last_pong_recv_remote = to_timestamp(be64toh(pong_pkt->timestamp.sec), ntohl(pong_pkt->timestamp.nsec)); if (state->awaiting_pong == 0) { return RECV_FATAL; } @@ -561,6 +563,8 @@ static struct connection * new_connection(struct longterm_keypair const * const c->my_keypair = my_keypair; c->user_data = NULL; + c->last_ping_recv_remote = 0.0; + c->last_pong_recv_remote = 0.0; c->last_ping_recv = c->last_ping_send = c->last_pong_recv = c->last_pong_send = create_timestamp(); c->latency = 0.0; c->total_bytes_recv = 0; @@ -135,10 +135,13 @@ struct connection { /* nonce must be incremented before sending or comparing a remote received one */ uint8_t last_nonce[crypto_box_NONCEBYTES]; - double last_ping_recv; + double last_ping_recv_remote; + double last_pong_recv_remote; + double last_ping_send; - double last_pong_recv; + double last_ping_recv; double last_pong_send; + double last_pong_recv; double latency; uint64_t total_bytes_recv; @@ -128,9 +128,6 @@ enum recv_return protocol_request_ping(struct connection * const state, strftime_local(ts, ts_str, sizeof(ts_str)); LOG(NOTICE, "Received PING with timestamp %.09lfs: %s / %uns", ts, ts_str, extract_nsecs(ts)); - if (state->latency > 0.0f) { - LOG(NOTICE, "PING-PONG latency: %.09lfs", state->latency); - } if (ev_protocol_pong(state) != 0) { return RECV_FATAL_CALLBACK_ERROR; @@ -152,6 +149,10 @@ enum recv_return protocol_request_pong(struct connection * const state, LOG(NOTICE, "Received PONG with timestamp %.09lfs: %s / %uns / %zu outstanding PONG's", ts, ts_str, extract_nsecs(ts), state->awaiting_pong); + if (state->latency > 0.0) { + LOG(NOTICE, "PING-PONG latency: %.09lfs", state->latency); + } + if (state->awaiting_pong > 3) { LOG(ERROR, "Waiting for more than 3 PONG's, disconnecting.."); return RECV_FATAL_CALLBACK_ERROR; |