mirror of
https://github.com/irungentoo/toxcore.git
synced 2024-03-22 13:30:51 +08:00
Make Friend_Requests a module-private type.
This commit is contained in:
parent
4b54d14d9c
commit
d8fcac5b4f
|
@ -141,7 +141,7 @@ static uint16_t address_checksum(const uint8_t *address, uint32_t len)
|
||||||
void getaddress(const Messenger *m, uint8_t *address)
|
void getaddress(const Messenger *m, uint8_t *address)
|
||||||
{
|
{
|
||||||
id_copy(address, m->net_crypto->self_public_key);
|
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));
|
memcpy(address + CRYPTO_PUBLIC_KEY_SIZE, &nospam, sizeof(nospam));
|
||||||
uint16_t checksum = address_checksum(address, FRIEND_ADDRESS_SIZE - sizeof(checksum));
|
uint16_t checksum = address_checksum(address, FRIEND_ADDRESS_SIZE - sizeof(checksum));
|
||||||
memcpy(address + CRYPTO_PUBLIC_KEY_SIZE + sizeof(nospam), &checksum, 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);
|
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);
|
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) {
|
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 m_callback_friendrequest(Messenger *m, void (*function)(Messenger *m, const uint8_t *, const uint8_t *, size_t,
|
||||||
void *))
|
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. */
|
/* 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;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
Messenger *m = (Messenger *)calloc(1, sizeof(Messenger));
|
|
||||||
|
|
||||||
if (error) {
|
if (error) {
|
||||||
*error = MESSENGER_ERROR_OTHER;
|
*error = MESSENGER_ERROR_OTHER;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Messenger *m = (Messenger *)calloc(1, sizeof(Messenger));
|
||||||
|
|
||||||
if (!m) {
|
if (!m) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
m->fr = friendreq_new();
|
||||||
|
|
||||||
|
if (!m->fr) {
|
||||||
|
free(m);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
Logger *log = NULL;
|
Logger *log = NULL;
|
||||||
|
|
||||||
if (options->log_callback) {
|
if (options->log_callback) {
|
||||||
|
@ -1954,6 +1961,7 @@ Messenger *new_messenger(Messenger_Options *options, unsigned int *error)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (m->net == NULL) {
|
if (m->net == NULL) {
|
||||||
|
friendreq_kill(m->fr);
|
||||||
free(m);
|
free(m);
|
||||||
|
|
||||||
if (error && net_err == 1) {
|
if (error && net_err == 1) {
|
||||||
|
@ -1967,6 +1975,7 @@ Messenger *new_messenger(Messenger_Options *options, unsigned int *error)
|
||||||
|
|
||||||
if (m->dht == NULL) {
|
if (m->dht == NULL) {
|
||||||
kill_networking(m->net);
|
kill_networking(m->net);
|
||||||
|
friendreq_kill(m->fr);
|
||||||
free(m);
|
free(m);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
@ -1976,6 +1985,7 @@ Messenger *new_messenger(Messenger_Options *options, unsigned int *error)
|
||||||
if (m->net_crypto == NULL) {
|
if (m->net_crypto == NULL) {
|
||||||
kill_networking(m->net);
|
kill_networking(m->net);
|
||||||
kill_DHT(m->dht);
|
kill_DHT(m->dht);
|
||||||
|
friendreq_kill(m->fr);
|
||||||
free(m);
|
free(m);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
@ -1993,6 +2003,7 @@ Messenger *new_messenger(Messenger_Options *options, unsigned int *error)
|
||||||
kill_net_crypto(m->net_crypto);
|
kill_net_crypto(m->net_crypto);
|
||||||
kill_DHT(m->dht);
|
kill_DHT(m->dht);
|
||||||
kill_networking(m->net);
|
kill_networking(m->net);
|
||||||
|
friendreq_kill(m->fr);
|
||||||
free(m);
|
free(m);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
@ -2008,6 +2019,7 @@ Messenger *new_messenger(Messenger_Options *options, unsigned int *error)
|
||||||
kill_net_crypto(m->net_crypto);
|
kill_net_crypto(m->net_crypto);
|
||||||
kill_DHT(m->dht);
|
kill_DHT(m->dht);
|
||||||
kill_networking(m->net);
|
kill_networking(m->net);
|
||||||
|
friendreq_kill(m->fr);
|
||||||
free(m);
|
free(m);
|
||||||
|
|
||||||
if (error) {
|
if (error) {
|
||||||
|
@ -2019,9 +2031,9 @@ Messenger *new_messenger(Messenger_Options *options, unsigned int *error)
|
||||||
}
|
}
|
||||||
|
|
||||||
m->options = *options;
|
m->options = *options;
|
||||||
friendreq_init(&m->fr, m->fr_c);
|
friendreq_init(m->fr, m->fr_c);
|
||||||
set_nospam(&m->fr, random_int());
|
set_nospam(m->fr, random_int());
|
||||||
set_filter_function(&m->fr, &friend_already_added, m);
|
set_filter_function(m->fr, &friend_already_added, m);
|
||||||
|
|
||||||
m->lastdump = 0;
|
m->lastdump = 0;
|
||||||
|
|
||||||
|
@ -2059,6 +2071,7 @@ void kill_messenger(Messenger *m)
|
||||||
|
|
||||||
logger_kill(m->log);
|
logger_kill(m->log);
|
||||||
free(m->friendlist);
|
free(m->friendlist);
|
||||||
|
friendreq_kill(m->fr);
|
||||||
free(m);
|
free(m);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2925,11 +2938,11 @@ void messenger_save(const Messenger *m, uint8_t *data)
|
||||||
host_to_lendian32(data, MESSENGER_STATE_COOKIE_GLOBAL);
|
host_to_lendian32(data, MESSENGER_STATE_COOKIE_GLOBAL);
|
||||||
data += size32;
|
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;
|
len = size32 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_SECRET_KEY_SIZE;
|
||||||
type = MESSENGER_STATE_TYPE_NOSPAMKEYS;
|
type = MESSENGER_STATE_TYPE_NOSPAMKEYS;
|
||||||
data = messenger_save_subheader(data, len, type);
|
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);
|
save_keys(m->net_crypto, data + size32);
|
||||||
data += len;
|
data += len;
|
||||||
|
|
||||||
|
@ -3000,7 +3013,7 @@ static int messenger_load_state_callback(void *outer, const uint8_t *data, uint3
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case MESSENGER_STATE_TYPE_NOSPAMKEYS:
|
case MESSENGER_STATE_TYPE_NOSPAMKEYS:
|
||||||
if (length == CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_SECRET_KEY_SIZE + sizeof(uint32_t)) {
|
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);
|
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) {
|
if (public_key_cmp((&data[sizeof(uint32_t)]), m->net_crypto->self_public_key) != 0) {
|
||||||
|
|
|
@ -228,7 +228,7 @@ struct Messenger {
|
||||||
Friend_Connections *fr_c;
|
Friend_Connections *fr_c;
|
||||||
|
|
||||||
TCP_Server *tcp_server;
|
TCP_Server *tcp_server;
|
||||||
Friend_Requests fr;
|
Friend_Requests *fr;
|
||||||
uint8_t name[MAX_NAME_LENGTH];
|
uint8_t name[MAX_NAME_LENGTH];
|
||||||
uint16_t name_length;
|
uint16_t name_length;
|
||||||
|
|
||||||
|
|
|
@ -29,6 +29,24 @@
|
||||||
|
|
||||||
#include "util.h"
|
#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. */
|
/* Set and get the nospam variable used to prevent one type of friend request spam. */
|
||||||
void set_nospam(Friend_Requests *fr, uint32_t num)
|
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);
|
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);
|
||||||
|
}
|
||||||
|
|
|
@ -28,23 +28,7 @@
|
||||||
|
|
||||||
#define MAX_FRIEND_REQUEST_DATA_SIZE (ONION_CLIENT_MAX_DATA_SIZE - (1 + sizeof(uint32_t)))
|
#define MAX_FRIEND_REQUEST_DATA_SIZE (ONION_CLIENT_MAX_DATA_SIZE - (1 + sizeof(uint32_t)))
|
||||||
|
|
||||||
typedef struct {
|
typedef struct Friend_Requests 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;
|
|
||||||
} Friend_Requests;
|
|
||||||
|
|
||||||
/* Set and get the nospam variable used to prevent one type of friend request spam. */
|
/* Set and get the nospam variable used to prevent one type of friend request spam. */
|
||||||
void set_nospam(Friend_Requests *fr, uint32_t num);
|
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. */
|
/* Sets up friendreq packet handlers. */
|
||||||
void friendreq_init(Friend_Requests *fr, Friend_Connections *fr_c);
|
void friendreq_init(Friend_Requests *fr, Friend_Connections *fr_c);
|
||||||
|
|
||||||
|
Friend_Requests *friendreq_new(void);
|
||||||
|
void friendreq_kill(Friend_Requests *fr);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -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)
|
void tox_self_set_nospam(Tox *tox, uint32_t nospam)
|
||||||
{
|
{
|
||||||
Messenger *m = tox;
|
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)
|
uint32_t tox_self_get_nospam(const Tox *tox)
|
||||||
{
|
{
|
||||||
const Messenger *m = 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)
|
void tox_self_get_public_key(const Tox *tox, uint8_t *public_key)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user