From 8e00294b3cb9808ce55160240454359638178275 Mon Sep 17 00:00:00 2001 From: iphydf Date: Sat, 23 Jun 2018 17:16:28 +0000 Subject: Add Logger to various net_crypto functions. In preparation for adding log statements. Also, fix an uninitialised variable warning in cppcheck. --- toxav/audio.c | 19 ++++++++-------- toxav/audio.h | 5 +++-- toxav/groupav.c | 20 ++++++++--------- toxav/groupav.h | 4 ++-- toxav/msi.c | 6 ++--- toxav/msi.h | 2 +- toxav/rtp.c | 15 +++++++------ toxav/rtp.h | 2 +- toxav/video.c | 4 ++-- toxav/video.h | 5 +++-- toxcore/DHT.c | 8 +++---- toxcore/DHT.h | 2 +- toxcore/net_crypto.c | 63 ++++++++++++++++++++++++++-------------------------- toxcore/net_crypto.h | 2 +- toxcore/network.c | 12 +++++----- toxcore/network.h | 6 ++--- toxcore/util.c | 2 +- toxcore/util.h | 2 +- 18 files changed, 91 insertions(+), 88 deletions(-) diff --git a/toxav/audio.c b/toxav/audio.c index 85639f16..3b46d1d5 100644 --- a/toxav/audio.c +++ b/toxav/audio.c @@ -33,16 +33,17 @@ static struct JitterBuffer *jbuf_new(uint32_t capacity); static void jbuf_clear(struct JitterBuffer *q); static void jbuf_free(struct JitterBuffer *q); -static int jbuf_write(Logger *log, struct JitterBuffer *q, struct RTPMessage *m); +static int jbuf_write(const Logger *log, struct JitterBuffer *q, struct RTPMessage *m); static struct RTPMessage *jbuf_read(struct JitterBuffer *q, int32_t *success); -OpusEncoder *create_audio_encoder(Logger *log, int32_t bit_rate, int32_t sampling_rate, int32_t channel_count); -bool reconfigure_audio_encoder(Logger *log, OpusEncoder **e, int32_t new_br, int32_t new_sr, uint8_t new_ch, - int32_t *old_br, int32_t *old_sr, int32_t *old_ch); -bool reconfigure_audio_decoder(ACSession *ac, int32_t sampling_rate, int8_t channels); +static OpusEncoder *create_audio_encoder(const Logger *log, int32_t bit_rate, int32_t sampling_rate, + int32_t channel_count); +static bool reconfigure_audio_encoder(const Logger *log, OpusEncoder **e, int32_t new_br, int32_t new_sr, + uint8_t new_ch, int32_t *old_br, int32_t *old_sr, int32_t *old_ch); +static bool reconfigure_audio_decoder(ACSession *ac, int32_t sampling_rate, int8_t channels); -ACSession *ac_new(Logger *log, ToxAV *av, uint32_t friend_number, toxav_audio_receive_frame_cb *cb, void *cb_data) +ACSession *ac_new(const Logger *log, ToxAV *av, uint32_t friend_number, toxav_audio_receive_frame_cb *cb, void *cb_data) { ACSession *ac = (ACSession *)calloc(sizeof(ACSession), 1); @@ -309,7 +310,7 @@ static void jbuf_free(struct JitterBuffer *q) free(q->queue); free(q); } -static int jbuf_write(Logger *log, struct JitterBuffer *q, struct RTPMessage *m) +static int jbuf_write(const Logger *log, struct JitterBuffer *q, struct RTPMessage *m) { uint16_t sequnum = m->header.sequnum; @@ -363,7 +364,7 @@ static struct RTPMessage *jbuf_read(struct JitterBuffer *q, int32_t *success) *success = 0; return nullptr; } -OpusEncoder *create_audio_encoder(Logger *log, int32_t bit_rate, int32_t sampling_rate, int32_t channel_count) +OpusEncoder *create_audio_encoder(const Logger *log, int32_t bit_rate, int32_t sampling_rate, int32_t channel_count) { int status = OPUS_OK; /* @@ -456,7 +457,7 @@ FAILURE: return nullptr; } -bool reconfigure_audio_encoder(Logger *log, OpusEncoder **e, int32_t new_br, int32_t new_sr, uint8_t new_ch, +bool reconfigure_audio_encoder(const Logger *log, OpusEncoder **e, int32_t new_br, int32_t new_sr, uint8_t new_ch, int32_t *old_br, int32_t *old_sr, int32_t *old_ch) { /* Values are checked in toxav.c */ diff --git a/toxav/audio.h b/toxav/audio.h index 73f1b19e..64aa2f12 100644 --- a/toxav/audio.h +++ b/toxav/audio.h @@ -51,7 +51,7 @@ struct RTPMessage; typedef struct ACSession_s { - Logger *log; + const Logger *log; /* encoding */ OpusEncoder *encoder; @@ -76,7 +76,8 @@ typedef struct ACSession_s { PAIR(toxav_audio_receive_frame_cb *, void *) acb; /* Audio frame receive callback */ } ACSession; -ACSession *ac_new(Logger *log, ToxAV *av, uint32_t friend_number, toxav_audio_receive_frame_cb *cb, void *cb_data); +ACSession *ac_new(const Logger *log, ToxAV *av, uint32_t friend_number, toxav_audio_receive_frame_cb *cb, + void *cb_data); void ac_kill(ACSession *ac); void ac_iterate(ACSession *ac); int ac_queue_message(void *acp, struct RTPMessage *msg); diff --git a/toxav/groupav.c b/toxav/groupav.c index f0cb3a5b..97baac39 100644 --- a/toxav/groupav.c +++ b/toxav/groupav.c @@ -159,7 +159,7 @@ static Group_Audio_Packet *dequeue(Group_JitterBuffer *q, int *success) } typedef struct { - Logger *log; + const Logger *log; Group_Chats *g_c; OpusEncoder *audio_encoder; @@ -227,8 +227,8 @@ static int recreate_encoder(Group_AV *group_av) return 0; } -static Group_AV *new_group_av(Logger *log, Group_Chats *g_c, void (*audio_callback)(Messenger *, uint32_t, uint32_t, - const int16_t *, unsigned int, uint8_t, uint32_t, void *), void *userdata) +static Group_AV *new_group_av(const Logger *log, Group_Chats *g_c, void (*audio_callback)(Messenger *, uint32_t, + uint32_t, const int16_t *, unsigned int, uint8_t, uint32_t, void *), void *userdata) { if (!g_c) { return nullptr; @@ -429,9 +429,9 @@ static int handle_group_audio_packet(void *object, uint32_t groupnumber, uint32_ * return 0 on success. * return -1 on failure. */ -static int groupchat_enable_av(Logger *log, Group_Chats *g_c, uint32_t groupnumber, void (*audio_callback)(Messenger *, - uint32_t, - uint32_t, const int16_t *, unsigned int, uint8_t, uint32_t, void *), void *userdata) +static int groupchat_enable_av(const Logger *log, Group_Chats *g_c, uint32_t groupnumber, + void (*audio_callback)(Messenger *, + uint32_t, uint32_t, const int16_t *, unsigned int, uint8_t, uint32_t, void *), void *userdata) { Group_AV *group_av = new_group_av(log, g_c, audio_callback, userdata); @@ -456,10 +456,8 @@ static int groupchat_enable_av(Logger *log, Group_Chats *g_c, uint32_t groupnumb * return group number on success. * return -1 on failure. */ -int add_av_groupchat(Logger *log, Group_Chats *g_c, void (*audio_callback)(Messenger *, uint32_t, uint32_t, - const int16_t *, - unsigned int, - uint8_t, uint32_t, void *), void *userdata) +int add_av_groupchat(const Logger *log, Group_Chats *g_c, void (*audio_callback)(Messenger *, uint32_t, uint32_t, + const int16_t *, unsigned int, uint8_t, uint32_t, void *), void *userdata) { int groupnumber = add_groupchat(g_c, GROUPCHAT_TYPE_AV); @@ -480,7 +478,7 @@ int add_av_groupchat(Logger *log, Group_Chats *g_c, void (*audio_callback)(Messe * returns group number on success * returns -1 on failure. */ -int join_av_groupchat(Logger *log, Group_Chats *g_c, uint32_t friendnumber, const uint8_t *data, uint16_t length, +int join_av_groupchat(const Logger *log, Group_Chats *g_c, uint32_t friendnumber, const uint8_t *data, uint16_t length, void (*audio_callback)(Messenger *, uint32_t, uint32_t, const int16_t *, unsigned int, uint8_t, uint32_t, void *), void *userdata) { diff --git a/toxav/groupav.h b/toxav/groupav.h index 142edb0e..ca30a51b 100644 --- a/toxav/groupav.h +++ b/toxav/groupav.h @@ -29,7 +29,7 @@ * return group number on success. * return -1 on failure. */ -int add_av_groupchat(Logger *log, Group_Chats *g_c, void (*audio_callback)(Messenger *, uint32_t, uint32_t, +int add_av_groupchat(const Logger *log, Group_Chats *g_c, void (*audio_callback)(Messenger *, uint32_t, uint32_t, const int16_t *, unsigned int, uint8_t, uint32_t, void *), void *userdata); /* Join a AV group (you need to have been invited first.) @@ -37,7 +37,7 @@ int add_av_groupchat(Logger *log, Group_Chats *g_c, void (*audio_callback)(Messe * returns group number on success * returns -1 on failure. */ -int join_av_groupchat(Logger *log, Group_Chats *g_c, uint32_t friendnumber, const uint8_t *data, uint16_t length, +int join_av_groupchat(const Logger *log, Group_Chats *g_c, uint32_t friendnumber, const uint8_t *data, uint16_t length, void (*audio_callback)(Messenger *, uint32_t, uint32_t, const int16_t *, unsigned int, uint8_t, uint32_t, void *), void *userdata); diff --git a/toxav/msi.c b/toxav/msi.c index 47f76fb7..f00ccfdf 100644 --- a/toxav/msi.c +++ b/toxav/msi.c @@ -74,7 +74,7 @@ typedef struct { void msg_init(MSIMessage *dest, MSIRequest request); -int msg_parse_in(Logger *log, MSIMessage *dest, const uint8_t *data, uint16_t length); +int msg_parse_in(const Logger *log, MSIMessage *dest, const uint8_t *data, uint16_t length); uint8_t *msg_parse_header_out(MSIHeaderID id, uint8_t *dest, const void *value, uint8_t value_len, uint16_t *length); static int send_message(Messenger *m, uint32_t friend_number, const MSIMessage *msg); int send_error(Messenger *m, uint32_t friend_number, MSIError error); @@ -131,7 +131,7 @@ MSISession *msi_new(Messenger *m) LOGGER_DEBUG(m->log, "New msi session: %p ", (void *)retu); return retu; } -int msi_kill(MSISession *session, Logger *log) +int msi_kill(MSISession *session, const Logger *log) { if (session == nullptr) { LOGGER_ERROR(log, "Tried to terminate non-existing session"); @@ -324,7 +324,7 @@ void msg_init(MSIMessage *dest, MSIRequest request) dest->request.exists = true; dest->request.value = request; } -int msg_parse_in(Logger *log, MSIMessage *dest, const uint8_t *data, uint16_t length) +int msg_parse_in(const Logger *log, MSIMessage *dest, const uint8_t *data, uint16_t length) { /* Parse raw data received from socket into MSIMessage struct */ diff --git a/toxav/msi.h b/toxav/msi.h index 43ff46d5..b2c15567 100644 --- a/toxav/msi.h +++ b/toxav/msi.h @@ -126,7 +126,7 @@ MSISession *msi_new(Messenger *m); /** * Terminate control session. NOTE: all calls will be freed */ -int msi_kill(MSISession *session, Logger *log); +int msi_kill(MSISession *session, const Logger *log); /** * Callback setter. */ diff --git a/toxav/rtp.c b/toxav/rtp.c index bc968de4..1e178720 100644 --- a/toxav/rtp.c +++ b/toxav/rtp.c @@ -82,7 +82,7 @@ enum { * do not kick it out right away if all slots are full instead kick out the new * incoming interframe. */ -static int8_t get_slot(Logger *log, struct RTPWorkBufferList *wkbl, bool is_keyframe, +static int8_t get_slot(const Logger *log, struct RTPWorkBufferList *wkbl, bool is_keyframe, const struct RTPHeader *header, bool is_multipart) { if (is_multipart) { @@ -198,7 +198,7 @@ static int8_t get_slot(Logger *log, struct RTPWorkBufferList *wkbl, bool is_keyf * non-NULL, it transfers ownership of the message to the caller, i.e. the * caller is responsible for storing it elsewhere or calling free(). */ -static struct RTPMessage *process_frame(Logger *log, struct RTPWorkBufferList *wkbl, uint8_t slot_id) +static struct RTPMessage *process_frame(const Logger *log, struct RTPWorkBufferList *wkbl, uint8_t slot_id) { assert(wkbl->next_free_entry >= 0); @@ -253,8 +253,9 @@ static struct RTPMessage *process_frame(Logger *log, struct RTPWorkBufferList *w * @param incoming_data The pure payload without header. * @param incoming_data_length The length in bytes of the incoming data payload. */ -static bool fill_data_into_slot(Logger *log, struct RTPWorkBufferList *wkbl, const uint8_t slot_id, bool is_keyframe, - const struct RTPHeader *header, const uint8_t *incoming_data, uint16_t incoming_data_length) +static bool fill_data_into_slot(const Logger *log, struct RTPWorkBufferList *wkbl, const uint8_t slot_id, + bool is_keyframe, const struct RTPHeader *header, + const uint8_t *incoming_data, uint16_t incoming_data_length) { // We're either filling the data into an existing slot, or in a new one that // is the next free entry. @@ -312,7 +313,7 @@ static bool fill_data_into_slot(Logger *log, struct RTPWorkBufferList *wkbl, con return slot->received_len == header->data_length_full; } -static void update_bwc_values(Logger *log, RTPSession *session, const struct RTPMessage *msg) +static void update_bwc_values(const Logger *log, RTPSession *session, const struct RTPMessage *msg) { if (session->first_packets_counter < DISMISS_FIRST_LOST_VIDEO_PACKET_COUNT) { session->first_packets_counter++; @@ -348,7 +349,7 @@ static void update_bwc_values(Logger *log, RTPSession *session, const struct RTP * @return -1 on error, 0 on success. */ static int handle_video_packet(RTPSession *session, const struct RTPHeader *header, - const uint8_t *incoming_data, uint16_t incoming_data_length, Logger *log) + const uint8_t *incoming_data, uint16_t incoming_data_length, const Logger *log) { // Full frame length in bytes. The frame may be split into multiple packets, // but this value is the complete assembled frame size. @@ -738,7 +739,7 @@ int rtp_stop_receiving(RTPSession *session) * @param length is the length of the raw data. */ int rtp_send_data(RTPSession *session, const uint8_t *data, uint32_t length, - bool is_keyframe, Logger *log) + bool is_keyframe, const Logger *log) { if (!session) { LOGGER_ERROR(log, "No session!"); diff --git a/toxav/rtp.h b/toxav/rtp.h index a310d58a..d7fc5560 100644 --- a/toxav/rtp.h +++ b/toxav/rtp.h @@ -213,7 +213,7 @@ int rtp_stop_receiving(RTPSession *session); * audio frame, this parameter is ignored. */ int rtp_send_data(RTPSession *session, const uint8_t *data, uint32_t length, - bool is_keyframe, Logger *log); + bool is_keyframe, const Logger *log); #ifdef __cplusplus } // extern "C" diff --git a/toxav/video.c b/toxav/video.c index 8c31cf75..1126b8e6 100644 --- a/toxav/video.c +++ b/toxav/video.c @@ -83,7 +83,7 @@ #define VPX_MAX_DECODER_THREADS 4 #define VIDEO__VP8_DECODER_POST_PROCESSING_ENABLED 0 -static void vc_init_encoder_cfg(Logger *log, vpx_codec_enc_cfg_t *cfg, int16_t kf_max_dist) +static void vc_init_encoder_cfg(const Logger *log, vpx_codec_enc_cfg_t *cfg, int16_t kf_max_dist) { vpx_codec_err_t rc = vpx_codec_enc_config_default(VIDEO_CODEC_ENCODER_INTERFACE, cfg, 0); @@ -152,7 +152,7 @@ static void vc_init_encoder_cfg(Logger *log, vpx_codec_enc_cfg_t *cfg, int16_t k #endif } -VCSession *vc_new(Logger *log, ToxAV *av, uint32_t friend_number, toxav_video_receive_frame_cb *cb, void *cb_data) +VCSession *vc_new(const Logger *log, ToxAV *av, uint32_t friend_number, toxav_video_receive_frame_cb *cb, void *cb_data) { VCSession *vc = (VCSession *)calloc(sizeof(VCSession), 1); vpx_codec_err_t rc; diff --git a/toxav/video.h b/toxav/video.h index 85ad3129..616a866a 100644 --- a/toxav/video.h +++ b/toxav/video.h @@ -50,7 +50,7 @@ typedef struct VCSession_s { uint64_t linfts; /* Last received frame time stamp */ uint32_t lcfd; /* Last calculated frame duration for incoming video payload */ - Logger *log; + const Logger *log; ToxAV *av; uint32_t friend_number; @@ -59,7 +59,8 @@ typedef struct VCSession_s { pthread_mutex_t queue_mutex[1]; } VCSession; -VCSession *vc_new(Logger *log, ToxAV *av, uint32_t friend_number, toxav_video_receive_frame_cb *cb, void *cb_data); +VCSession *vc_new(const Logger *log, ToxAV *av, uint32_t friend_number, toxav_video_receive_frame_cb *cb, + void *cb_data); void vc_kill(VCSession *vc); void vc_iterate(VCSession *vc); int vc_queue_message(void *vcp, struct RTPMessage *msg); diff --git a/toxcore/DHT.c b/toxcore/DHT.c index 6134f59b..bb996e2d 100644 --- a/toxcore/DHT.c +++ b/toxcore/DHT.c @@ -85,7 +85,7 @@ struct DHT_Friend { }; struct DHT { - Logger *log; + const Logger *log; Networking_Core *net; bool hole_punching_enabled; @@ -658,7 +658,7 @@ static uint32_t index_of_client_ip_port(const Client_data *array, uint32_t size, /* Update ip_port of client if it's needed. */ -static void update_client(Logger *log, int index, Client_data *client, IP_Port ip_port) +static void update_client(const Logger *log, int index, Client_data *client, IP_Port ip_port) { IPPTsPng *assoc; int ip_version; @@ -698,7 +698,7 @@ static void update_client(Logger *log, int index, Client_data *client, IP_Port i * * return True(1) or False(0) */ -static int client_or_ip_port_in_list(Logger *log, Client_data *list, uint16_t length, const uint8_t *public_key, +static int client_or_ip_port_in_list(const Logger *log, Client_data *list, uint16_t length, const uint8_t *public_key, IP_Port ip_port) { const uint64_t temp_time = unix_time(); @@ -2673,7 +2673,7 @@ static int cryptopacket_handle(void *object, IP_Port source, const uint8_t *pack /*----------------------------------------------------------------------------------*/ -DHT *new_DHT(Logger *log, Networking_Core *net, bool holepunching_enabled) +DHT *new_DHT(const Logger *log, Networking_Core *net, bool holepunching_enabled) { /* init time */ unix_time_update(); diff --git a/toxcore/DHT.h b/toxcore/DHT.h index 3ad28d8c..bbfeb307 100644 --- a/toxcore/DHT.h +++ b/toxcore/DHT.h @@ -405,7 +405,7 @@ void DHT_save(const DHT *dht, uint8_t *data); int DHT_load(DHT *dht, const uint8_t *data, uint32_t length); /* Initialize DHT. */ -DHT *new_DHT(Logger *log, Networking_Core *net, bool holepunching_enabled); +DHT *new_DHT(const Logger *log, Networking_Core *net, bool holepunching_enabled); void kill_DHT(DHT *dht); diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c index fbf41552..f82cef69 100644 --- a/toxcore/net_crypto.c +++ b/toxcore/net_crypto.c @@ -130,7 +130,7 @@ typedef struct { } Crypto_Connection; struct Net_Crypto { - Logger *log; + const Logger *log; DHT *dht; TCP_Connections *tcp_c; @@ -245,7 +245,7 @@ static int create_cookie_request(const Net_Crypto *c, uint8_t *packet, uint8_t * * return -1 on failure. * return 0 on success. */ -static int create_cookie(uint8_t *cookie, const uint8_t *bytes, const uint8_t *encryption_key) +static int create_cookie(const Logger *log, uint8_t *cookie, const uint8_t *bytes, const uint8_t *encryption_key) { uint8_t contents[COOKIE_CONTENTS_LENGTH]; uint64_t temp_time = unix_time(); @@ -266,7 +266,7 @@ static int create_cookie(uint8_t *cookie, const uint8_t *bytes, const uint8_t *e * return -1 on failure. * return 0 on success. */ -static int open_cookie(uint8_t *bytes, const uint8_t *cookie, const uint8_t *encryption_key) +static int open_cookie(const Logger *log, uint8_t *bytes, const uint8_t *cookie, const uint8_t *encryption_key) { uint8_t contents[COOKIE_CONTENTS_LENGTH]; int len = decrypt_data_symmetric(encryption_key, cookie, cookie + CRYPTO_NONCE_SIZE, @@ -304,7 +304,7 @@ static int create_cookie_response(const Net_Crypto *c, uint8_t *packet, const ui memcpy(cookie_plain + CRYPTO_PUBLIC_KEY_SIZE, dht_public_key, CRYPTO_PUBLIC_KEY_SIZE); uint8_t plain[COOKIE_LENGTH + sizeof(uint64_t)]; - if (create_cookie(plain, cookie_plain, c->secret_symmetric_key) != 0) { + if (create_cookie(c->log, plain, cookie_plain, c->secret_symmetric_key) != 0) { return -1; } @@ -431,7 +431,8 @@ static int tcp_oob_handle_cookie_request(const Net_Crypto *c, unsigned int tcp_c * return -1 on failure. * return COOKIE_LENGTH on success. */ -static int handle_cookie_response(uint8_t *cookie, uint64_t *number, const uint8_t *packet, uint16_t length, +static int handle_cookie_response(const Logger *log, uint8_t *cookie, uint64_t *number, + const uint8_t *packet, uint16_t length, const uint8_t *shared_key) { if (length != COOKIE_RESPONSE_LENGTH) { @@ -471,7 +472,7 @@ static int create_crypto_handshake(const Net_Crypto *c, uint8_t *packet, const u memcpy(cookie_plain, peer_real_pk, CRYPTO_PUBLIC_KEY_SIZE); memcpy(cookie_plain + CRYPTO_PUBLIC_KEY_SIZE, peer_dht_pubkey, CRYPTO_PUBLIC_KEY_SIZE); - if (create_cookie(plain + CRYPTO_NONCE_SIZE + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_SHA512_SIZE, cookie_plain, + if (create_cookie(c->log, plain + CRYPTO_NONCE_SIZE + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_SHA512_SIZE, cookie_plain, c->secret_symmetric_key) != 0) { return -1; } @@ -517,7 +518,7 @@ static int handle_crypto_handshake(const Net_Crypto *c, uint8_t *nonce, uint8_t uint8_t cookie_plain[COOKIE_DATA_LENGTH]; - if (open_cookie(cookie_plain, packet + 1, c->secret_symmetric_key) != 0) { + if (open_cookie(c->log, cookie_plain, packet + 1, c->secret_symmetric_key) != 0) { return -1; } @@ -724,7 +725,7 @@ static uint32_t num_packets_array(const Packets_Array *array) * return -1 on failure. * return 0 on success. */ -static int add_data_to_buffer(Packets_Array *array, uint32_t number, const Packet_Data *data) +static int add_data_to_buffer(const Logger *log, Packets_Array *array, uint32_t number, const Packet_Data *data) { if (number - array->buffer_start > CRYPTO_PACKET_BUFFER_SIZE) { return -1; @@ -758,7 +759,7 @@ static int add_data_to_buffer(Packets_Array *array, uint32_t number, const Packe * return 0 if data at number is empty. * return 1 if data pointer was put in data. */ -static int get_data_pointer(const Packets_Array *array, Packet_Data **data, uint32_t number) +static int get_data_pointer(const Logger *log, const Packets_Array *array, Packet_Data **data, uint32_t number) { uint32_t num_spots = array->buffer_end - array->buffer_start; @@ -781,7 +782,7 @@ static int get_data_pointer(const Packets_Array *array, Packet_Data **data, uint * return -1 on failure. * return packet number on success. */ -static int64_t add_data_end_of_buffer(Packets_Array *array, const Packet_Data *data) +static int64_t add_data_end_of_buffer(const Logger *log, Packets_Array *array, const Packet_Data *data) { if (num_packets_array(array) >= CRYPTO_PACKET_BUFFER_SIZE) { return -1; @@ -800,12 +801,12 @@ static int64_t add_data_end_of_buffer(Packets_Array *array, const Packet_Data *d return id; } -/* Read data from begginning of array. +/* Read data from beginning of array. * * return -1 on failure. * return packet number on success. */ -static int64_t read_data_beg_buffer(Packets_Array *array, Packet_Data *data) +static int64_t read_data_beg_buffer(const Logger *log, Packets_Array *array, Packet_Data *data) { if (array->buffer_end == array->buffer_start) { return -1; @@ -830,7 +831,7 @@ static int64_t read_data_beg_buffer(Packets_Array *array, Packet_Data *data) * return -1 on failure. * return 0 on success */ -static int clear_buffer_until(Packets_Array *array, uint32_t number) +static int clear_buffer_until(const Logger *log, Packets_Array *array, uint32_t number) { uint32_t num_spots = array->buffer_end - array->buffer_start; @@ -875,7 +876,7 @@ static int clear_buffer(Packets_Array *array) * return -1 on failure. * return 0 on success. */ -static int set_buffer_end(Packets_Array *array, uint32_t number) +static int set_buffer_end(const Logger *log, Packets_Array *array, uint32_t number) { if ((number - array->buffer_start) > CRYPTO_PACKET_BUFFER_SIZE) { return -1; @@ -895,7 +896,7 @@ static int set_buffer_end(Packets_Array *array, uint32_t number) * return -1 on failure. * return length of packet on success. */ -static int generate_request_packet(uint8_t *data, uint16_t length, const Packets_Array *recv_array) +static int generate_request_packet(const Logger *log, uint8_t *data, uint16_t length, const Packets_Array *recv_array) { if (length == 0) { return -1; @@ -948,7 +949,7 @@ static int generate_request_packet(uint8_t *data, uint16_t length, const Packets * return -1 on failure. * return number of requested packets on success. */ -static int handle_request_packet(Packets_Array *send_array, const uint8_t *data, uint16_t length, +static int handle_request_packet(const Logger *log, Packets_Array *send_array, const uint8_t *data, uint16_t length, uint64_t *latest_send_time, uint64_t rtt_time) { if (length < 1) { @@ -1101,7 +1102,7 @@ static int reset_max_speed_reached(Net_Crypto *c, int crypt_connection_id) if (conn->maximum_speed_reached) { Packet_Data *dt = nullptr; uint32_t packet_num = conn->send_array.buffer_end - 1; - int ret = get_data_pointer(&conn->send_array, &dt, packet_num); + int ret = get_data_pointer(c->log, &conn->send_array, &dt, packet_num); uint8_t send_failed = 0; @@ -1155,7 +1156,7 @@ static int64_t send_lossless_packet(Net_Crypto *c, int crypt_connection_id, cons dt.length = length; memcpy(dt.data, data, length); pthread_mutex_lock(&conn->mutex); - int64_t packet_num = add_data_end_of_buffer(&conn->send_array, &dt); + int64_t packet_num = add_data_end_of_buffer(c->log, &conn->send_array, &dt); pthread_mutex_unlock(&conn->mutex); if (packet_num == -1) { @@ -1169,7 +1170,7 @@ static int64_t send_lossless_packet(Net_Crypto *c, int crypt_connection_id, cons if (send_data_packet_helper(c, crypt_connection_id, conn->recv_array.buffer_start, packet_num, data, length) == 0) { Packet_Data *dt1 = nullptr; - if (get_data_pointer(&conn->send_array, &dt1, packet_num) == 1) { + if (get_data_pointer(c->log, &conn->send_array, &dt1, packet_num) == 1) { dt1->sent_time = current_time_monotonic(); } } else { @@ -1248,7 +1249,7 @@ static int send_request_packet(Net_Crypto *c, int crypt_connection_id) } uint8_t data[MAX_CRYPTO_DATA_SIZE]; - int len = generate_request_packet(data, sizeof(data), &conn->recv_array); + int len = generate_request_packet(c->log, data, sizeof(data), &conn->recv_array); if (len == -1) { return -1; @@ -1281,7 +1282,7 @@ static int send_requested_packets(Net_Crypto *c, int crypt_connection_id, uint32 for (i = 0; i < array_size; ++i) { Packet_Data *dt; uint32_t packet_num = (i + conn->send_array.buffer_start); - int ret = get_data_pointer(&conn->send_array, &dt, packet_num); + int ret = get_data_pointer(c->log, &conn->send_array, &dt, packet_num); if (ret == -1) { return -1; @@ -1496,11 +1497,11 @@ static int handle_data_packet_core(Net_Crypto *c, int crypt_connection_id, const if (buffer_start != conn->send_array.buffer_start) { Packet_Data *packet_time; - if (get_data_pointer(&conn->send_array, &packet_time, conn->send_array.buffer_start) == 1) { + if (get_data_pointer(c->log, &conn->send_array, &packet_time, conn->send_array.buffer_start) == 1) { rtt_calc_time = packet_time->sent_time; } - if (clear_buffer_until(&conn->send_array, buffer_start) != 0) { + if (clear_buffer_until(c->log, &conn->send_array, buffer_start) != 0) { return -1; } } @@ -1541,25 +1542,25 @@ static int handle_data_packet_core(Net_Crypto *c, int crypt_connection_id, const rtt_time = DEFAULT_TCP_PING_CONNECTION; } - int requested = handle_request_packet(&conn->send_array, real_data, real_length, &rtt_calc_time, rtt_time); + int requested = handle_request_packet(c->log, &conn->send_array, real_data, real_length, &rtt_calc_time, rtt_time); if (requested == -1) { return -1; } - set_buffer_end(&conn->recv_array, num); + set_buffer_end(c->log, &conn->recv_array, num); } else if (real_data[0] >= CRYPTO_RESERVED_PACKETS && real_data[0] < PACKET_ID_LOSSY_RANGE_START) { - Packet_Data dt; + Packet_Data dt = {0}; dt.length = real_length; memcpy(dt.data, real_data, real_length); - if (add_data_to_buffer(&conn->recv_array, num, &dt) != 0) { + if (add_data_to_buffer(c->log, &conn->recv_array, num, &dt) != 0) { return -1; } while (1) { pthread_mutex_lock(&conn->mutex); - int ret = read_data_beg_buffer(&conn->recv_array, &dt); + int ret = read_data_beg_buffer(c->log, &conn->recv_array, &dt); pthread_mutex_unlock(&conn->mutex); if (ret == -1) { @@ -1584,7 +1585,7 @@ static int handle_data_packet_core(Net_Crypto *c, int crypt_connection_id, const } else if (real_data[0] >= PACKET_ID_LOSSY_RANGE_START && real_data[0] < (PACKET_ID_LOSSY_RANGE_START + PACKET_ID_LOSSY_RANGE_SIZE)) { - set_buffer_end(&conn->recv_array, num); + set_buffer_end(c->log, &conn->recv_array, num); if (conn->connection_lossy_data_callback) { conn->connection_lossy_data_callback(conn->connection_lossy_data_callback_object, @@ -1632,7 +1633,7 @@ static int handle_packet_connection(Net_Crypto *c, int crypt_connection_id, cons uint8_t cookie[COOKIE_LENGTH]; uint64_t number; - if (handle_cookie_response(cookie, &number, packet, length, conn->shared_key) != sizeof(cookie)) { + if (handle_cookie_response(c->log, cookie, &number, packet, length, conn->shared_key) != sizeof(cookie)) { return -1; } @@ -2928,7 +2929,7 @@ void load_secret_key(Net_Crypto *c, const uint8_t *sk) /* Run this to (re)initialize net_crypto. * Sets all the global connection variables to their default values. */ -Net_Crypto *new_net_crypto(Logger *log, DHT *dht, TCP_Proxy_Info *proxy_info) +Net_Crypto *new_net_crypto(const Logger *log, DHT *dht, TCP_Proxy_Info *proxy_info) { unix_time_update(); diff --git a/toxcore/net_crypto.h b/toxcore/net_crypto.h index f6b2648e..2a76013e 100644 --- a/toxcore/net_crypto.h +++ b/toxcore/net_crypto.h @@ -299,7 +299,7 @@ void load_secret_key(Net_Crypto *c, const uint8_t *sk); /* Create new instance of Net_Crypto. * Sets all the global connection variables to their default values. */ -Net_Crypto *new_net_crypto(Logger *log, DHT *dht, TCP_Proxy_Info *proxy_info); +Net_Crypto *new_net_crypto(const Logger *log, DHT *dht, TCP_Proxy_Info *proxy_info); /* return the optimal interval in ms for running do_net_crypto. */ diff --git a/toxcore/network.c b/toxcore/network.c index 44ca2dee..30c3bb6c 100644 --- a/toxcore/network.c +++ b/toxcore/network.c @@ -495,7 +495,7 @@ static uint32_t data_1(uint16_t buflen, const uint8_t *buffer) return buflen > 7 ? net_ntohl(*(const uint32_t *)&buffer[5]) : 0; } -static void loglogdata(Logger *log, const char *message, const uint8_t *buffer, +static void loglogdata(const Logger *log, const char *message, const uint8_t *buffer, uint16_t buflen, IP_Port ip_port, int res) { char ip_str[IP_NTOA_LEN]; @@ -527,7 +527,7 @@ typedef struct { } Packet_Handler; struct Networking_Core { - Logger *log; + const Logger *log; Packet_Handler packethandlers[256]; Family family; @@ -616,7 +616,7 @@ int sendpacket(Networking_Core *net, IP_Port ip_port, const uint8_t *data, uint1 * Packet data is put into data. * Packet length is put into length. */ -static int receivepacket(Logger *log, Socket sock, IP_Port *ip_port, uint8_t *data, uint32_t *length) +static int receivepacket(const Logger *log, Socket sock, IP_Port *ip_port, uint8_t *data, uint32_t *length) { memset(ip_port, 0, sizeof(IP_Port)); struct sockaddr_storage addr; @@ -766,7 +766,7 @@ static void at_shutdown(void) /* Initialize networking. * Added for reverse compatibility with old new_networking calls. */ -Networking_Core *new_networking(Logger *log, IP ip, uint16_t port) +Networking_Core *new_networking(const Logger *log, IP ip, uint16_t port) { return new_networking_ex(log, ip, port, port + (TOX_PORTRANGE_TO - TOX_PORTRANGE_FROM), nullptr); } @@ -781,7 +781,7 @@ Networking_Core *new_networking(Logger *log, IP ip, uint16_t port) * * If error is non NULL it is set to 0 if no issues, 1 if socket related error, 2 if other. */ -Networking_Core *new_networking_ex(Logger *log, IP ip, uint16_t port_from, uint16_t port_to, unsigned int *error) +Networking_Core *new_networking_ex(const Logger *log, IP ip, uint16_t port_from, uint16_t port_to, unsigned int *error) { /* If both from and to are 0, use default port range * If one is 0 and the other is non-0, use the non-0 value as only port @@ -996,7 +996,7 @@ Networking_Core *new_networking_ex(Logger *log, IP ip, uint16_t port_from, uint1 return nullptr; } -Networking_Core *new_networking_no_udp(Logger *log) +Networking_Core *new_networking_no_udp(const Logger *log) { /* this is the easiest way to completely disable UDP without changing too much code. */ Networking_Core *net = (Networking_Core *)calloc(1, sizeof(Networking_Core)); diff --git a/toxcore/network.h b/toxcore/network.h index 8071375b..fbd12bb7 100644 --- a/toxcore/network.h +++ b/toxcore/network.h @@ -458,9 +458,9 @@ void net_kill_strerror(const char *strerror); * * If error is non NULL it is set to 0 if no issues, 1 if socket related error, 2 if other. */ -Networking_Core *new_networking(Logger *log, IP ip, uint16_t port); -Networking_Core *new_networking_ex(Logger *log, IP ip, uint16_t port_from, uint16_t port_to, unsigned int *error); -Networking_Core *new_networking_no_udp(Logger *log); +Networking_Core *new_networking(const Logger *log, IP ip, uint16_t port); +Networking_Core *new_networking_ex(const Logger *log, IP ip, uint16_t port_from, uint16_t port_to, unsigned int *error); +Networking_Core *new_networking_no_udp(const Logger *log); /* Function to cleanup networking stuff (doesn't do much right now). */ void kill_networking(Networking_Core *net); diff --git a/toxcore/util.c b/toxcore/util.c index 082e453f..57d1762e 100644 --- a/toxcore/util.c +++ b/toxcore/util.c @@ -122,7 +122,7 @@ void lendian_to_host32(uint32_t *dest, const uint8_t *lendian) } /* state load/save */ -int load_state(load_state_callback_func load_state_callback, Logger *log, void *outer, +int load_state(load_state_callback_func load_state_callback, const Logger *log, void *outer, const uint8_t *data, uint32_t length, uint16_t cookie_inner) { if (!load_state_callback || !data) { diff --git a/toxcore/util.h b/toxcore/util.h index a9faa863..3979cefe 100644 --- a/toxcore/util.h +++ b/toxcore/util.h @@ -59,7 +59,7 @@ void lendian_to_host32(uint32_t *dest, const uint8_t *lendian); /* state load/save */ typedef int (*load_state_callback_func)(void *outer, const uint8_t *data, uint32_t len, uint16_t type); -int load_state(load_state_callback_func load_state_callback, Logger *log, void *outer, +int load_state(load_state_callback_func load_state_callback, const Logger *log, void *outer, const uint8_t *data, uint32_t length, uint16_t cookie_inner); /* Returns -1 if failed or 0 if success */ -- cgit v1.2.3