summaryrefslogtreecommitdiff
path: root/toxcore/Messenger.c
diff options
context:
space:
mode:
authorCoren[m] <Break@Ocean>2013-10-24 22:32:28 +0200
committerCoren[m] <Break@Ocean>2013-10-24 22:32:28 +0200
commit0a4c3d7e2e080dafd66d25f7a5806b89f7be1bcf (patch)
tree61409d905b8132bddc9142525cced3368aba0ad8 /toxcore/Messenger.c
parente9d92606d94837ac24e2583cdfd8d313a0fd9338 (diff)
Move unix_time(), id_cpy()/id_eq(), is_timeout() to util.*
unix_time(): - returns local value for current epoch - value is updated explicitly with unix_time_update() called at new_DHT()/doMessenger()/do_DHT() is_timeout(): - uses the local value for current epoch id_cpy()/id_eq() => id_copy()/id_equal(): - centralized duplicate definitions - replaced (most) memcpy()/memcmp() of (*, *, CLIENT_ID_SIZE) with id_copy()/id_equal()
Diffstat (limited to 'toxcore/Messenger.c')
-rw-r--r--toxcore/Messenger.c28
1 files changed, 15 insertions, 13 deletions
diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c
index 2e8efeac..7710a33c 100644
--- a/toxcore/Messenger.c
+++ b/toxcore/Messenger.c
@@ -92,7 +92,7 @@ int getfriend_id(Messenger *m, uint8_t *client_id)
92 92
93 for (i = 0; i < m->numfriends; ++i) { 93 for (i = 0; i < m->numfriends; ++i) {
94 if (m->friendlist[i].status > 0) 94 if (m->friendlist[i].status > 0)
95 if (memcmp(client_id, m->friendlist[i].client_id, crypto_box_PUBLICKEYBYTES) == 0) 95 if (id_equal(client_id, m->friendlist[i].client_id))
96 return i; 96 return i;
97 } 97 }
98 98
@@ -140,7 +140,7 @@ static uint16_t address_checksum(uint8_t *address, uint32_t len)
140 */ 140 */
141void getaddress(Messenger *m, uint8_t *address) 141void getaddress(Messenger *m, uint8_t *address)
142{ 142{
143 memcpy(address, m->net_crypto->self_public_key, crypto_box_PUBLICKEYBYTES); 143 id_copy(address, m->net_crypto->self_public_key);
144 uint32_t nospam = get_nospam(&(m->fr)); 144 uint32_t nospam = get_nospam(&(m->fr));
145 memcpy(address + crypto_box_PUBLICKEYBYTES, &nospam, sizeof(nospam)); 145 memcpy(address + crypto_box_PUBLICKEYBYTES, &nospam, sizeof(nospam));
146 uint16_t checksum = address_checksum(address, FRIEND_ADDRESS_SIZE - sizeof(checksum)); 146 uint16_t checksum = address_checksum(address, FRIEND_ADDRESS_SIZE - sizeof(checksum));
@@ -173,7 +173,7 @@ int m_addfriend(Messenger *m, uint8_t *address, uint8_t *data, uint16_t length)
173 return FAERR_TOOLONG; 173 return FAERR_TOOLONG;
174 174
175 uint8_t client_id[crypto_box_PUBLICKEYBYTES]; 175 uint8_t client_id[crypto_box_PUBLICKEYBYTES];
176 memcpy(client_id, address, crypto_box_PUBLICKEYBYTES); 176 id_copy(client_id, address);
177 uint16_t check, checksum = address_checksum(address, FRIEND_ADDRESS_SIZE - sizeof(checksum)); 177 uint16_t check, checksum = address_checksum(address, FRIEND_ADDRESS_SIZE - sizeof(checksum));
178 memcpy(&check, address + crypto_box_PUBLICKEYBYTES + sizeof(uint32_t), sizeof(check)); 178 memcpy(&check, address + crypto_box_PUBLICKEYBYTES + sizeof(uint32_t), sizeof(check));
179 179
@@ -183,7 +183,7 @@ int m_addfriend(Messenger *m, uint8_t *address, uint8_t *data, uint16_t length)
183 if (length < 1) 183 if (length < 1)
184 return FAERR_NOMESSAGE; 184 return FAERR_NOMESSAGE;
185 185
186 if (memcmp(client_id, m->net_crypto->self_public_key, crypto_box_PUBLICKEYBYTES) == 0) 186 if (id_equal(client_id, m->net_crypto->self_public_key))
187 return FAERR_OWNKEY; 187 return FAERR_OWNKEY;
188 188
189 int friend_id = getfriend_id(m, client_id); 189 int friend_id = getfriend_id(m, client_id);
@@ -214,7 +214,7 @@ int m_addfriend(Messenger *m, uint8_t *address, uint8_t *data, uint16_t length)
214 m->friendlist[i].crypt_connection_id = -1; 214 m->friendlist[i].crypt_connection_id = -1;
215 m->friendlist[i].friendrequest_lastsent = 0; 215 m->friendlist[i].friendrequest_lastsent = 0;
216 m->friendlist[i].friendrequest_timeout = FRIENDREQUEST_TIMEOUT; 216 m->friendlist[i].friendrequest_timeout = FRIENDREQUEST_TIMEOUT;
217 memcpy(m->friendlist[i].client_id, client_id, CLIENT_ID_SIZE); 217 id_copy(m->friendlist[i].client_id, client_id);
218 m->friendlist[i].statusmessage = calloc(1, 1); 218 m->friendlist[i].statusmessage = calloc(1, 1);
219 m->friendlist[i].statusmessage_length = 1; 219 m->friendlist[i].statusmessage_length = 1;
220 m->friendlist[i].userstatus = USERSTATUS_NONE; 220 m->friendlist[i].userstatus = USERSTATUS_NONE;
@@ -243,7 +243,7 @@ int m_addfriend_norequest(Messenger *m, uint8_t *client_id)
243 if (realloc_friendlist(m, m->numfriends + 1) != 0) 243 if (realloc_friendlist(m, m->numfriends + 1) != 0)
244 return FAERR_NOMEM; 244 return FAERR_NOMEM;
245 245
246 if (memcmp(client_id, m->net_crypto->self_public_key, crypto_box_PUBLICKEYBYTES) == 0) 246 if (id_equal(client_id, m->net_crypto->self_public_key))
247 return FAERR_OWNKEY; 247 return FAERR_OWNKEY;
248 248
249 memset(&(m->friendlist[m->numfriends]), 0, sizeof(Friend)); 249 memset(&(m->friendlist[m->numfriends]), 0, sizeof(Friend));
@@ -256,7 +256,7 @@ int m_addfriend_norequest(Messenger *m, uint8_t *client_id)
256 m->friendlist[i].status = FRIEND_CONFIRMED; 256 m->friendlist[i].status = FRIEND_CONFIRMED;
257 m->friendlist[i].crypt_connection_id = -1; 257 m->friendlist[i].crypt_connection_id = -1;
258 m->friendlist[i].friendrequest_lastsent = 0; 258 m->friendlist[i].friendrequest_lastsent = 0;
259 memcpy(m->friendlist[i].client_id, client_id, CLIENT_ID_SIZE); 259 id_copy(m->friendlist[i].client_id, client_id);
260 m->friendlist[i].statusmessage = calloc(1, 1); 260 m->friendlist[i].statusmessage = calloc(1, 1);
261 m->friendlist[i].statusmessage_length = 1; 261 m->friendlist[i].statusmessage_length = 1;
262 m->friendlist[i].userstatus = USERSTATUS_NONE; 262 m->friendlist[i].userstatus = USERSTATUS_NONE;
@@ -728,7 +728,7 @@ static int group_num(Messenger *m, uint8_t *group_public_key)
728 uint32_t i; 728 uint32_t i;
729 729
730 for (i = 0; i < m->numchats; ++i) { 730 for (i = 0; i < m->numchats; ++i) {
731 if (memcmp(m->chats[i]->self_public_key, group_public_key, crypto_box_PUBLICKEYBYTES) == 0) 731 if (id_equal(m->chats[i]->self_public_key, group_public_key))
732 return i; 732 return i;
733 } 733 }
734 734
@@ -917,8 +917,8 @@ int join_groupchat(Messenger *m, int friendnumber, uint8_t *friend_group_public_
917 if (groupnum == -1) 917 if (groupnum == -1)
918 return -1; 918 return -1;
919 919
920 memcpy(data, friend_group_public_key, crypto_box_PUBLICKEYBYTES); 920 id_copy(data, friend_group_public_key);
921 memcpy(data + crypto_box_PUBLICKEYBYTES, m->chats[groupnum]->self_public_key, crypto_box_PUBLICKEYBYTES); 921 id_copy(data + crypto_box_PUBLICKEYBYTES, m->chats[groupnum]->self_public_key);
922 922
923 if (write_cryptpacket_id(m, friendnumber, PACKET_ID_JOIN_GROUPCHAT, data, sizeof(data))) { 923 if (write_cryptpacket_id(m, friendnumber, PACKET_ID_JOIN_GROUPCHAT, data, sizeof(data))) {
924 chat_bootstrap_nonlazy(m->chats[groupnum], get_friend_ipport(m, friendnumber), 924 chat_bootstrap_nonlazy(m->chats[groupnum], get_friend_ipport(m, friendnumber),
@@ -965,7 +965,7 @@ static int handle_group(void *object, IP_Port source, uint8_t *packet, uint32_t
965 if (m->chats[i] == NULL) 965 if (m->chats[i] == NULL)
966 continue; 966 continue;
967 967
968 if (memcmp(packet + 1, m->chats[i]->self_public_key, crypto_box_PUBLICKEYBYTES) == 0) 968 if (id_equal(packet + 1, m->chats[i]->self_public_key))
969 return handle_groupchatpacket(m->chats[i], source, packet, length); 969 return handle_groupchatpacket(m->chats[i], source, packet, length);
970 } 970 }
971 971
@@ -1769,6 +1769,8 @@ static char *ID2String(uint8_t *client_id)
1769/* The main loop that needs to be run at least 20 times per second. */ 1769/* The main loop that needs to be run at least 20 times per second. */
1770void doMessenger(Messenger *m) 1770void doMessenger(Messenger *m)
1771{ 1771{
1772 unix_time_update();
1773
1772 networking_poll(m->net); 1774 networking_poll(m->net);
1773 1775
1774 do_DHT(m->dht); 1776 do_DHT(m->dht);
@@ -1996,7 +1998,7 @@ static int Messenger_load_old(Messenger *m, uint8_t *data, uint32_t length)
1996 } else if (friend_list[i].status != 0) { 1998 } else if (friend_list[i].status != 0) {
1997 /* TODO: This is not a good way to do this. */ 1999 /* TODO: This is not a good way to do this. */
1998 uint8_t address[FRIEND_ADDRESS_SIZE]; 2000 uint8_t address[FRIEND_ADDRESS_SIZE];
1999 memcpy(address, friend_list[i].client_id, crypto_box_PUBLICKEYBYTES); 2001 id_copy(address, friend_list[i].client_id);
2000 memcpy(address + crypto_box_PUBLICKEYBYTES, &(friend_list[i].friendrequest_nospam), sizeof(uint32_t)); 2002 memcpy(address + crypto_box_PUBLICKEYBYTES, &(friend_list[i].friendrequest_nospam), sizeof(uint32_t));
2001 uint16_t checksum = address_checksum(address, FRIEND_ADDRESS_SIZE - sizeof(checksum)); 2003 uint16_t checksum = address_checksum(address, FRIEND_ADDRESS_SIZE - sizeof(checksum));
2002 memcpy(address + crypto_box_PUBLICKEYBYTES + sizeof(uint32_t), &checksum, sizeof(checksum)); 2004 memcpy(address + crypto_box_PUBLICKEYBYTES + sizeof(uint32_t), &checksum, sizeof(checksum));
@@ -2130,7 +2132,7 @@ static int messenger_load_state_callback(void *outer, uint8_t *data, uint32_t le
2130 } else if (friends[i].status != 0) { 2132 } else if (friends[i].status != 0) {
2131 /* TODO: This is not a good way to do this. */ 2133 /* TODO: This is not a good way to do this. */
2132 uint8_t address[FRIEND_ADDRESS_SIZE]; 2134 uint8_t address[FRIEND_ADDRESS_SIZE];
2133 memcpy(address, friends[i].client_id, crypto_box_PUBLICKEYBYTES); 2135 id_copy(address, friends[i].client_id);
2134 memcpy(address + crypto_box_PUBLICKEYBYTES, &(friends[i].friendrequest_nospam), sizeof(uint32_t)); 2136 memcpy(address + crypto_box_PUBLICKEYBYTES, &(friends[i].friendrequest_nospam), sizeof(uint32_t));
2135 uint16_t checksum = address_checksum(address, FRIEND_ADDRESS_SIZE - sizeof(checksum)); 2137 uint16_t checksum = address_checksum(address, FRIEND_ADDRESS_SIZE - sizeof(checksum));
2136 memcpy(address + crypto_box_PUBLICKEYBYTES + sizeof(uint32_t), &checksum, sizeof(checksum)); 2138 memcpy(address + crypto_box_PUBLICKEYBYTES + sizeof(uint32_t), &checksum, sizeof(checksum));