diff options
author | Toni Uhlig <matzeton@googlemail.com> | 2020-10-26 09:39:35 +0100 |
---|---|---|
committer | Toni Uhlig <matzeton@googlemail.com> | 2020-10-26 09:39:35 +0100 |
commit | b85fe6e68f7e46dbea7fedff47da06d05966ebfb (patch) | |
tree | 6023533c5a37d27a00b63f02567fc11a6026470f | |
parent | 4ba484be6ef6e1f759fdb6dbde056592143cbd14 (diff) |
Finished timestamp refactoring.
- adjusted -W* CFLAGS
- replaced suseconds_t with uint32_t for nanoseconds extraction
- fixed (now) invalid format specifiers
- added WARN_UNUSED_RESULT macro usable to warn if a fn return value is unused
Signed-off-by: Toni Uhlig <matzeton@googlemail.com>
-rw-r--r-- | Makefile | 4 | ||||
-rw-r--r-- | client.c | 6 | ||||
-rw-r--r-- | common-event2.c | 4 | ||||
-rw-r--r-- | protocol.c | 4 | ||||
-rw-r--r-- | protocol.h | 48 | ||||
-rw-r--r-- | server.c | 8 |
6 files changed, 39 insertions, 35 deletions
@@ -19,9 +19,7 @@ DEBUG_CFLAGS = EXTRA_CFLAGS = -Werror -Os endif -CFLAGS += -Wall -Wextra -Wsign-conversion -Wstrict-prototypes -std=gnu11 $(EXTRA_CFLAGS) -D_GNU_SOURCE $(DEBUG_CFLAGS) $(SANITIZER_CFLAGS) -fstrict-aliasing \ - $(shell $(PKG_CONFIG_BIN) --cflags libsodium) \ - $(shell $(PKG_CONFIG_BIN) --cflags libevent) +CFLAGS += -Wall -Wextra -Wsign-conversion -Wstrict-prototypes -std=gnu11 $(EXTRA_CFLAGS) -D_GNU_SOURCE $(DEBUG_CFLAGS) $(SANITIZER_CFLAGS) -fstrict-aliasing HEADER_TARGETS = utils.h common-event2.h common-sodium.h logging.h protocol.h BUILD_TARGETS = utils.o common-event2.o common-sodium.o logging.o protocol.o @@ -32,7 +32,7 @@ static void send_data(struct connection * const state) if (data_fd >= 0) { bytes_read = read(data_fd, buf, sizeof(buf)); - if (bytes_read <= 0 || ev_protocol_data(state, buf, bytes_read) != 0) { + if (bytes_read <= 0 || ev_protocol_data(state, buf, (uint32_t)bytes_read) != 0) { if (bytes_read == 0) { LOG(WARNING, "EoF: Closing file descriptor %d aka %s", data_fd, opts.filepath); } else { @@ -273,7 +273,9 @@ int main(int argc, char ** argv) return 2; } - srandom(time(NULL)); + double ts = create_timestamp(); + uint64_t ts_seed = (uint64_t)ts + extract_nsecs(ts); + srandom(ts_seed); if (sodium_init() != 0) { LOG(ERROR, "Sodium init failed"); diff --git a/common-event2.c b/common-event2.c index 9c5ac93..28e28ad 100644 --- a/common-event2.c +++ b/common-event2.c @@ -212,7 +212,7 @@ 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 / %lluns", + 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,7 +227,7 @@ 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 / %lluns", + 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; @@ -88,11 +88,11 @@ double to_timestamp(uint64_t time_in_secs, uint32_t nano_secs) return r + ((double)nano_secs * 1e-9); } -suseconds_t extract_nsecs(double time_in_secs) +uint32_t extract_nsecs(double time_in_secs) { double s = (double)((time_t)time_in_secs); - return (suseconds_t)((time_in_secs - s) / 1e-9); + return (uint32_t)((time_in_secs - s) / 1e-9); } static enum recv_return process_body(struct connection * const state, @@ -15,6 +15,8 @@ #error "Window size is limited by sizeof(header.body_size)" #endif +#define WARN_UNUSED_RESULT __attribute__((warn_unused_result)) + #define CRYPTO_BYTES_PREAUTH crypto_box_SEALBYTES #define CRYPTO_BYTES_POSTAUTH crypto_secretstream_xchacha20poly1305_ABYTES #define PLAIN_PACKET_HEADER_SIZE ((size_t)sizeof(struct protocol_header)) @@ -169,27 +171,27 @@ enum recv_return { * PDU receive functionality * *****************************/ -enum recv_return process_received(struct connection * const state, - uint8_t const * const buffer, - size_t * const buffer_size); +enum recv_return WARN_UNUSED_RESULT process_received(struct connection * const state, + uint8_t const * const buffer, + size_t * const buffer_size); /* The following functions have to be implemented in your application e.g. client/server. */ -extern enum recv_return protocol_request_client_auth(struct connection * const state, - struct protocol_header const * const buffer, - size_t * const processed); -extern enum recv_return protocol_request_server_helo(struct connection * const, - struct protocol_header const * const buffer, - size_t * const processed); -extern enum recv_return protocol_request_data(struct connection * const state, - struct protocol_header const * const buffer, - size_t * const processed); -extern enum recv_return protocol_request_ping(struct connection * const state, - struct protocol_header const * const buffer, - size_t * const processed); -extern enum recv_return protocol_request_pong(struct connection * const state, - struct protocol_header const * const buffer, - size_t * const processed); +extern enum recv_return WARN_UNUSED_RESULT protocol_request_client_auth(struct connection * const state, + struct protocol_header const * const buffer, + size_t * const processed); +extern enum recv_return WARN_UNUSED_RESULT protocol_request_server_helo(struct connection * const, + struct protocol_header const * const buffer, + size_t * const processed); +extern enum recv_return WARN_UNUSED_RESULT protocol_request_data(struct connection * const state, + struct protocol_header const * const buffer, + size_t * const processed); +extern enum recv_return WARN_UNUSED_RESULT protocol_request_ping(struct connection * const state, + struct protocol_header const * const buffer, + size_t * const processed); +extern enum recv_return WARN_UNUSED_RESULT protocol_request_pong(struct connection * const state, + struct protocol_header const * const buffer, + size_t * const processed); /************************** * PDU send functionality * @@ -214,14 +216,14 @@ void protocol_response_pong(unsigned char out[CRYPT_PACKET_SIZE_PONG], struct co * connection state functionality * **********************************/ -struct connection * new_connection_from_client(struct longterm_keypair const * const my_keypair); -struct connection * new_connection_to_server(struct longterm_keypair const * const my_keypair); +struct connection * WARN_UNUSED_RESULT new_connection_from_client(struct longterm_keypair const * const my_keypair); +struct connection * WARN_UNUSED_RESULT new_connection_to_server(struct longterm_keypair const * const my_keypair); /*********************** * timestamp functions * ***********************/ -double create_timestamp(void); -double to_timestamp(uint64_t time_in_secs, uint32_t nano_secs); -suseconds_t extract_nsecs(double time_in_secs); +double WARN_UNUSED_RESULT create_timestamp(void); +double WARN_UNUSED_RESULT to_timestamp(uint64_t time_in_secs, uint32_t nano_secs); +uint32_t WARN_UNUSED_RESULT extract_nsecs(double time_in_secs); #endif @@ -126,7 +126,7 @@ enum recv_return protocol_request_ping(struct connection * const state, (void)processed; strftime_local(ts, ts_str, sizeof(ts_str)); - LOG(NOTICE, "Received PING with timestamp %.09lfs: %s / %lluns", + 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); @@ -149,7 +149,7 @@ enum recv_return protocol_request_pong(struct connection * const state, (void)processed; strftime_local(ts, ts_str, sizeof(ts_str)); - LOG(NOTICE, "Received PONG with timestamp %.09lfs: %s / %lluns / %zu outstanding PONG's", + LOG(NOTICE, "Received PONG with timestamp %.09lfs: %s / %uns / %zu outstanding PONG's", ts, ts_str, extract_nsecs(ts), state->awaiting_pong); if (state->awaiting_pong > 3) { @@ -325,7 +325,9 @@ int main(int argc, char ** argv) return 2; } - srandom(time(NULL)); + double ts = create_timestamp(); + uint64_t ts_seed = (uint64_t)ts + extract_nsecs(ts); + srandom(ts_seed); if (sodium_init() != 0) { LOG(ERROR, "Sodium init failed"); |