From d8fcac5b4f66efcec961cf30cb461ab9231c6a67 Mon Sep 17 00:00:00 2001 From: iphydf Date: Sat, 13 Jan 2018 18:31:51 +0000 Subject: [PATCH] Make Friend_Requests a module-private type. --- toxcore/Messenger.c | 35 ++++++++++++++++++++++++----------- toxcore/Messenger.h | 2 +- toxcore/friend_requests.c | 28 ++++++++++++++++++++++++++++ toxcore/friend_requests.h | 20 +++----------------- toxcore/tox.c | 4 ++-- 5 files changed, 58 insertions(+), 31 deletions(-) diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index bcd298c6..2686b7b8 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c @@ -141,7 +141,7 @@ static uint16_t address_checksum(const uint8_t *address, uint32_t len) void getaddress(const Messenger *m, uint8_t *address) { id_copy(address, m->net_crypto->self_public_key); - uint32_t nospam = get_nospam(&m->fr); + uint32_t nospam = get_nospam(m->fr); memcpy(address + CRYPTO_PUBLIC_KEY_SIZE, &nospam, sizeof(nospam)); uint16_t checksum = address_checksum(address, FRIEND_ADDRESS_SIZE - sizeof(checksum)); memcpy(address + CRYPTO_PUBLIC_KEY_SIZE + sizeof(nospam), &checksum, sizeof(checksum)); @@ -415,7 +415,7 @@ int m_delfriend(Messenger *m, int32_t friendnumber) } clear_receipts(m, friendnumber); - remove_request_received(&m->fr, m->friendlist[friendnumber].real_pk); + remove_request_received(m->fr, m->friendlist[friendnumber].real_pk); friend_connection_callbacks(m->fr_c, m->friendlist[friendnumber].friendcon_id, MESSENGER_CALLBACK_INDEX, 0, 0, 0, 0, 0); if (friend_con_connected(m->fr_c, m->friendlist[friendnumber].friendcon_id) == FRIENDCONN_STATUS_CONNECTED) { @@ -837,7 +837,7 @@ void m_callback_log(Messenger *m, logger_cb *function, void *context, void *user void m_callback_friendrequest(Messenger *m, void (*function)(Messenger *m, const uint8_t *, const uint8_t *, size_t, void *)) { - callback_friendrequest(&m->fr, (void (*)(void *, const uint8_t *, const uint8_t *, size_t, void *))function, m); + callback_friendrequest(m->fr, (void (*)(void *, const uint8_t *, const uint8_t *, size_t, void *))function, m); } /* Set the function that will be executed when a message from a friend is received. */ @@ -1921,16 +1921,23 @@ Messenger *new_messenger(Messenger_Options *options, unsigned int *error) return NULL; } - Messenger *m = (Messenger *)calloc(1, sizeof(Messenger)); - if (error) { *error = MESSENGER_ERROR_OTHER; } + Messenger *m = (Messenger *)calloc(1, sizeof(Messenger)); + if (!m) { return NULL; } + m->fr = friendreq_new(); + + if (!m->fr) { + free(m); + return NULL; + } + Logger *log = NULL; if (options->log_callback) { @@ -1954,6 +1961,7 @@ Messenger *new_messenger(Messenger_Options *options, unsigned int *error) } if (m->net == NULL) { + friendreq_kill(m->fr); free(m); if (error && net_err == 1) { @@ -1967,6 +1975,7 @@ Messenger *new_messenger(Messenger_Options *options, unsigned int *error) if (m->dht == NULL) { kill_networking(m->net); + friendreq_kill(m->fr); free(m); return NULL; } @@ -1976,6 +1985,7 @@ Messenger *new_messenger(Messenger_Options *options, unsigned int *error) if (m->net_crypto == NULL) { kill_networking(m->net); kill_DHT(m->dht); + friendreq_kill(m->fr); free(m); return NULL; } @@ -1993,6 +2003,7 @@ Messenger *new_messenger(Messenger_Options *options, unsigned int *error) kill_net_crypto(m->net_crypto); kill_DHT(m->dht); kill_networking(m->net); + friendreq_kill(m->fr); free(m); return NULL; } @@ -2008,6 +2019,7 @@ Messenger *new_messenger(Messenger_Options *options, unsigned int *error) kill_net_crypto(m->net_crypto); kill_DHT(m->dht); kill_networking(m->net); + friendreq_kill(m->fr); free(m); if (error) { @@ -2019,9 +2031,9 @@ Messenger *new_messenger(Messenger_Options *options, unsigned int *error) } m->options = *options; - friendreq_init(&m->fr, m->fr_c); - set_nospam(&m->fr, random_int()); - set_filter_function(&m->fr, &friend_already_added, m); + friendreq_init(m->fr, m->fr_c); + set_nospam(m->fr, random_int()); + set_filter_function(m->fr, &friend_already_added, m); m->lastdump = 0; @@ -2059,6 +2071,7 @@ void kill_messenger(Messenger *m) logger_kill(m->log); free(m->friendlist); + friendreq_kill(m->fr); free(m); } @@ -2925,11 +2938,11 @@ void messenger_save(const Messenger *m, uint8_t *data) host_to_lendian32(data, MESSENGER_STATE_COOKIE_GLOBAL); data += size32; - assert(sizeof(get_nospam(&m->fr)) == sizeof(uint32_t)); + assert(sizeof(get_nospam(m->fr)) == sizeof(uint32_t)); len = size32 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_SECRET_KEY_SIZE; type = MESSENGER_STATE_TYPE_NOSPAMKEYS; data = messenger_save_subheader(data, len, type); - *(uint32_t *)data = get_nospam(&m->fr); + *(uint32_t *)data = get_nospam(m->fr); save_keys(m->net_crypto, data + size32); data += len; @@ -3000,7 +3013,7 @@ static int messenger_load_state_callback(void *outer, const uint8_t *data, uint3 switch (type) { case MESSENGER_STATE_TYPE_NOSPAMKEYS: if (length == CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_SECRET_KEY_SIZE + sizeof(uint32_t)) { - set_nospam(&m->fr, *(const uint32_t *)data); + set_nospam(m->fr, *(const uint32_t *)data); load_secret_key(m->net_crypto, (&data[sizeof(uint32_t)]) + CRYPTO_PUBLIC_KEY_SIZE); if (public_key_cmp((&data[sizeof(uint32_t)]), m->net_crypto->self_public_key) != 0) { diff --git a/toxcore/Messenger.h b/toxcore/Messenger.h index e1dba698..350fe564 100644 --- a/toxcore/Messenger.h +++ b/toxcore/Messenger.h @@ -228,7 +228,7 @@ struct Messenger { Friend_Connections *fr_c; TCP_Server *tcp_server; - Friend_Requests fr; + Friend_Requests *fr; uint8_t name[MAX_NAME_LENGTH]; uint16_t name_length; diff --git a/toxcore/friend_requests.c b/toxcore/friend_requests.c index ba782e2b..4a06654d 100644 --- a/toxcore/friend_requests.c +++ b/toxcore/friend_requests.c @@ -29,6 +29,24 @@ #include "util.h" +struct Friend_Requests { + uint32_t nospam; + void (*handle_friendrequest)(void *, const uint8_t *, const uint8_t *, size_t, void *); + uint8_t handle_friendrequest_isset; + void *handle_friendrequest_object; + + int (*filter_function)(const uint8_t *, void *); + void *filter_function_userdata; + /* NOTE: The following is just a temporary fix for the multiple friend requests received at the same time problem. + * TODO(irungentoo): Make this better (This will most likely tie in with the way we will handle spam.) + */ + +#define MAX_RECEIVED_STORED 32 + + uint8_t received_requests[MAX_RECEIVED_STORED][CRYPTO_PUBLIC_KEY_SIZE]; + uint16_t received_requests_index; +}; + /* Set and get the nospam variable used to prevent one type of friend request spam. */ void set_nospam(Friend_Requests *fr, uint32_t num) { @@ -150,3 +168,13 @@ void friendreq_init(Friend_Requests *fr, Friend_Connections *fr_c) { set_friend_request_callback(fr_c, &friendreq_handlepacket, fr); } + +Friend_Requests *friendreq_new(void) +{ + return (Friend_Requests *)calloc(1, sizeof(Friend_Requests)); +} + +void friendreq_kill(Friend_Requests *fr) +{ + free(fr); +} diff --git a/toxcore/friend_requests.h b/toxcore/friend_requests.h index 316e1c67..e6948b6c 100644 --- a/toxcore/friend_requests.h +++ b/toxcore/friend_requests.h @@ -28,23 +28,7 @@ #define MAX_FRIEND_REQUEST_DATA_SIZE (ONION_CLIENT_MAX_DATA_SIZE - (1 + sizeof(uint32_t))) -typedef struct { - uint32_t nospam; - void (*handle_friendrequest)(void *, const uint8_t *, const uint8_t *, size_t, void *); - uint8_t handle_friendrequest_isset; - void *handle_friendrequest_object; - - int (*filter_function)(const uint8_t *, void *); - void *filter_function_userdata; - /* NOTE: The following is just a temporary fix for the multiple friend requests received at the same time problem. - * TODO(irungentoo): Make this better (This will most likely tie in with the way we will handle spam.) - */ - -#define MAX_RECEIVED_STORED 32 - - uint8_t received_requests[MAX_RECEIVED_STORED][CRYPTO_PUBLIC_KEY_SIZE]; - uint16_t received_requests_index; -} Friend_Requests; +typedef struct Friend_Requests Friend_Requests; /* Set and get the nospam variable used to prevent one type of friend request spam. */ void set_nospam(Friend_Requests *fr, uint32_t num); @@ -72,5 +56,7 @@ void set_filter_function(Friend_Requests *fr, int (*function)(const uint8_t *, v /* Sets up friendreq packet handlers. */ void friendreq_init(Friend_Requests *fr, Friend_Connections *fr_c); +Friend_Requests *friendreq_new(void); +void friendreq_kill(Friend_Requests *fr); #endif diff --git a/toxcore/tox.c b/toxcore/tox.c index ca81cbbc..3201f74e 100644 --- a/toxcore/tox.c +++ b/toxcore/tox.c @@ -355,13 +355,13 @@ void tox_self_get_address(const Tox *tox, uint8_t *address) void tox_self_set_nospam(Tox *tox, uint32_t nospam) { Messenger *m = tox; - set_nospam(&m->fr, net_htonl(nospam)); + set_nospam(m->fr, net_htonl(nospam)); } uint32_t tox_self_get_nospam(const Tox *tox) { const Messenger *m = tox; - return net_ntohl(get_nospam(&m->fr)); + return net_ntohl(get_nospam(m->fr)); } void tox_self_get_public_key(const Tox *tox, uint8_t *public_key)