diff options
Diffstat (limited to 'core/Messenger.c')
-rw-r--r-- | core/Messenger.c | 94 |
1 files changed, 9 insertions, 85 deletions
diff --git a/core/Messenger.c b/core/Messenger.c index faa3cefc..69d33172 100644 --- a/core/Messenger.c +++ b/core/Messenger.c | |||
@@ -60,16 +60,12 @@ static uint32_t numfriends; | |||
60 | int getfriend_id(uint8_t * client_id) | 60 | int getfriend_id(uint8_t * client_id) |
61 | { | 61 | { |
62 | uint32_t i; | 62 | uint32_t i; |
63 | |||
63 | for(i = 0; i < numfriends; ++i) | 64 | for(i = 0; i < numfriends; ++i) |
64 | { | ||
65 | if(friendlist[i].status > 0) | 65 | if(friendlist[i].status > 0) |
66 | { | ||
67 | if(memcmp(client_id, friendlist[i].client_id, crypto_box_PUBLICKEYBYTES) == 0) | 66 | if(memcmp(client_id, friendlist[i].client_id, crypto_box_PUBLICKEYBYTES) == 0) |
68 | { | ||
69 | return i; | 67 | return i; |
70 | } | 68 | |
71 | } | ||
72 | } | ||
73 | return -1; | 69 | return -1; |
74 | } | 70 | } |
75 | 71 | ||
@@ -80,15 +76,13 @@ int getfriend_id(uint8_t * client_id) | |||
80 | int getclient_id(int friend_id, uint8_t * client_id) | 76 | int getclient_id(int friend_id, uint8_t * client_id) |
81 | { | 77 | { |
82 | if(friend_id >= numfriends || friend_id < 0) | 78 | if(friend_id >= numfriends || friend_id < 0) |
83 | { | ||
84 | return -1; | 79 | return -1; |
85 | } | ||
86 | 80 | ||
87 | if(friendlist[friend_id].status > 0) | 81 | if(friendlist[friend_id].status > 0) { |
88 | { | ||
89 | memcpy(client_id, friendlist[friend_id].client_id, CLIENT_ID_SIZE); | 82 | memcpy(client_id, friendlist[friend_id].client_id, CLIENT_ID_SIZE); |
90 | return 0; | 83 | return 0; |
91 | } | 84 | } |
85 | |||
92 | return -1; | 86 | return -1; |
93 | } | 87 | } |
94 | 88 | ||
@@ -102,17 +96,11 @@ int m_addfriend(uint8_t * client_id, uint8_t * data, uint16_t length) | |||
102 | { | 96 | { |
103 | if(length == 0 || length >= | 97 | if(length == 0 || length >= |
104 | (MAX_DATA_SIZE - crypto_box_PUBLICKEYBYTES - crypto_box_NONCEBYTES - crypto_box_BOXZEROBYTES + crypto_box_ZEROBYTES)) | 98 | (MAX_DATA_SIZE - crypto_box_PUBLICKEYBYTES - crypto_box_NONCEBYTES - crypto_box_BOXZEROBYTES + crypto_box_ZEROBYTES)) |
105 | { | ||
106 | return -1; | 99 | return -1; |
107 | } | ||
108 | if(memcmp(client_id, self_public_key, crypto_box_PUBLICKEYBYTES) == 0) | 100 | if(memcmp(client_id, self_public_key, crypto_box_PUBLICKEYBYTES) == 0) |
109 | { | ||
110 | return -1; | 101 | return -1; |
111 | } | ||
112 | if(getfriend_id(client_id) != -1) | 102 | if(getfriend_id(client_id) != -1) |
113 | { | ||
114 | return -1; | 103 | return -1; |
115 | } | ||
116 | uint32_t i; | 104 | uint32_t i; |
117 | for(i = 0; i <= numfriends; ++i) | 105 | for(i = 0; i <= numfriends; ++i) |
118 | { | 106 | { |
@@ -138,9 +126,7 @@ int m_addfriend(uint8_t * client_id, uint8_t * data, uint16_t length) | |||
138 | int m_addfriend_norequest(uint8_t * client_id) | 126 | int m_addfriend_norequest(uint8_t * client_id) |
139 | { | 127 | { |
140 | if(getfriend_id(client_id) != -1) | 128 | if(getfriend_id(client_id) != -1) |
141 | { | ||
142 | return -1; | 129 | return -1; |
143 | } | ||
144 | uint32_t i; | 130 | uint32_t i; |
145 | for(i = 0; i <= numfriends; ++i) | 131 | for(i = 0; i <= numfriends; ++i) |
146 | { | 132 | { |
@@ -166,9 +152,7 @@ int m_addfriend_norequest(uint8_t * client_id) | |||
166 | int m_delfriend(int friendnumber) | 152 | int m_delfriend(int friendnumber) |
167 | { | 153 | { |
168 | if(friendnumber >= numfriends || friendnumber < 0) | 154 | if(friendnumber >= numfriends || friendnumber < 0) |
169 | { | ||
170 | return -1; | 155 | return -1; |
171 | } | ||
172 | 156 | ||
173 | DHT_delfriend(friendlist[friendnumber].client_id); | 157 | DHT_delfriend(friendlist[friendnumber].client_id); |
174 | crypto_kill(friendlist[friendnumber].crypt_connection_id); | 158 | crypto_kill(friendlist[friendnumber].crypt_connection_id); |
@@ -176,12 +160,8 @@ int m_delfriend(int friendnumber) | |||
176 | memset(&friendlist[friendnumber], 0, sizeof(Friend)); | 160 | memset(&friendlist[friendnumber], 0, sizeof(Friend)); |
177 | uint32_t i; | 161 | uint32_t i; |
178 | for(i = numfriends; i != 0; --i) | 162 | for(i = numfriends; i != 0; --i) |
179 | { | ||
180 | if(friendlist[i].status != 0) | 163 | if(friendlist[i].status != 0) |
181 | { | ||
182 | break; | 164 | break; |
183 | } | ||
184 | } | ||
185 | numfriends = i; | 165 | numfriends = i; |
186 | return 0; | 166 | return 0; |
187 | } | 167 | } |
@@ -194,9 +174,7 @@ int m_delfriend(int friendnumber) | |||
194 | int m_friendstatus(int friendnumber) | 174 | int m_friendstatus(int friendnumber) |
195 | { | 175 | { |
196 | if(friendnumber < 0 || friendnumber >= MAX_NUM_FRIENDS) | 176 | if(friendnumber < 0 || friendnumber >= MAX_NUM_FRIENDS) |
197 | { | ||
198 | return 0; | 177 | return 0; |
199 | } | ||
200 | return friendlist[friendnumber].status; | 178 | return friendlist[friendnumber].status; |
201 | } | 179 | } |
202 | 180 | ||
@@ -206,14 +184,10 @@ int m_friendstatus(int friendnumber) | |||
206 | int m_sendmessage(int friendnumber, uint8_t * message, uint32_t length) | 184 | int m_sendmessage(int friendnumber, uint8_t * message, uint32_t length) |
207 | { | 185 | { |
208 | if(friendnumber < 0 || friendnumber >= MAX_NUM_FRIENDS) | 186 | if(friendnumber < 0 || friendnumber >= MAX_NUM_FRIENDS) |
209 | { | ||
210 | return 0; | 187 | return 0; |
211 | } | ||
212 | if(length >= MAX_DATA_SIZE || friendlist[friendnumber].status != 4) | 188 | if(length >= MAX_DATA_SIZE || friendlist[friendnumber].status != 4) |
213 | /* this does not mean the maximum message length is MAX_DATA_SIZE - 1, it is actually 17 bytes less. */ | 189 | /* this does not mean the maximum message length is MAX_DATA_SIZE - 1, it is actually 17 bytes less. */ |
214 | { | ||
215 | return 0; | 190 | return 0; |
216 | } | ||
217 | uint8_t temp[MAX_DATA_SIZE]; | 191 | uint8_t temp[MAX_DATA_SIZE]; |
218 | temp[0] = PACKET_ID_MESSAGE; | 192 | temp[0] = PACKET_ID_MESSAGE; |
219 | memcpy(temp + 1, message, length); | 193 | memcpy(temp + 1, message, length); |
@@ -235,9 +209,7 @@ static int m_sendname(int friendnumber, uint8_t * name) | |||
235 | static int setfriendname(int friendnumber, uint8_t * name) | 209 | static int setfriendname(int friendnumber, uint8_t * name) |
236 | { | 210 | { |
237 | if(friendnumber >= numfriends || friendnumber < 0) | 211 | if(friendnumber >= numfriends || friendnumber < 0) |
238 | { | ||
239 | return -1; | 212 | return -1; |
240 | } | ||
241 | memcpy(friendlist[friendnumber].name, name, MAX_NAME_LENGTH); | 213 | memcpy(friendlist[friendnumber].name, name, MAX_NAME_LENGTH); |
242 | return 0; | 214 | return 0; |
243 | } | 215 | } |
@@ -249,15 +221,11 @@ static int setfriendname(int friendnumber, uint8_t * name) | |||
249 | int setname(uint8_t * name, uint16_t length) | 221 | int setname(uint8_t * name, uint16_t length) |
250 | { | 222 | { |
251 | if(length > MAX_NAME_LENGTH) | 223 | if(length > MAX_NAME_LENGTH) |
252 | { | ||
253 | return -1; | 224 | return -1; |
254 | } | ||
255 | memcpy(self_name, name, length); | 225 | memcpy(self_name, name, length); |
256 | uint32_t i; | 226 | uint32_t i; |
257 | for(i = 0; i < numfriends; ++i) | 227 | for(i = 0; i < numfriends; ++i) |
258 | { | ||
259 | friendlist[i].name_sent = 0; | 228 | friendlist[i].name_sent = 0; |
260 | } | ||
261 | return 0; | 229 | return 0; |
262 | } | 230 | } |
263 | 231 | ||
@@ -269,9 +237,7 @@ int setname(uint8_t * name, uint16_t length) | |||
269 | int getname(int friendnumber, uint8_t * name) | 237 | int getname(int friendnumber, uint8_t * name) |
270 | { | 238 | { |
271 | if(friendnumber >= numfriends || friendnumber < 0) | 239 | if(friendnumber >= numfriends || friendnumber < 0) |
272 | { | ||
273 | return -1; | 240 | return -1; |
274 | } | ||
275 | memcpy(name, friendlist[friendnumber].name, MAX_NAME_LENGTH); | 241 | memcpy(name, friendlist[friendnumber].name, MAX_NAME_LENGTH); |
276 | return 0; | 242 | return 0; |
277 | } | 243 | } |
@@ -279,9 +245,7 @@ int getname(int friendnumber, uint8_t * name) | |||
279 | int m_set_userstatus(uint8_t *status, uint16_t length) | 245 | int m_set_userstatus(uint8_t *status, uint16_t length) |
280 | { | 246 | { |
281 | if(length > MAX_USERSTATUS_LENGTH) | 247 | if(length > MAX_USERSTATUS_LENGTH) |
282 | { | ||
283 | return -1; | 248 | return -1; |
284 | } | ||
285 | uint8_t *newstatus = calloc(length, 1); | 249 | uint8_t *newstatus = calloc(length, 1); |
286 | memcpy(newstatus, status, length); | 250 | memcpy(newstatus, status, length); |
287 | free(self_userstatus); | 251 | free(self_userstatus); |
@@ -290,9 +254,7 @@ int m_set_userstatus(uint8_t *status, uint16_t length) | |||
290 | 254 | ||
291 | uint32_t i; | 255 | uint32_t i; |
292 | for(i = 0; i < numfriends; ++i) | 256 | for(i = 0; i < numfriends; ++i) |
293 | { | ||
294 | friendlist[i].userstatus_sent = 0; | 257 | friendlist[i].userstatus_sent = 0; |
295 | } | ||
296 | return 0; | 258 | return 0; |
297 | } | 259 | } |
298 | 260 | ||
@@ -301,9 +263,7 @@ int m_set_userstatus(uint8_t *status, uint16_t length) | |||
301 | int m_get_userstatus_size(int friendnumber) | 263 | int m_get_userstatus_size(int friendnumber) |
302 | { | 264 | { |
303 | if(friendnumber >= numfriends || friendnumber < 0) | 265 | if(friendnumber >= numfriends || friendnumber < 0) |
304 | { | ||
305 | return -1; | 266 | return -1; |
306 | } | ||
307 | return friendlist[friendnumber].userstatus_length; | 267 | return friendlist[friendnumber].userstatus_length; |
308 | } | 268 | } |
309 | 269 | ||
@@ -312,9 +272,7 @@ int m_get_userstatus_size(int friendnumber) | |||
312 | int m_copy_userstatus(int friendnumber, uint8_t * buf, uint32_t maxlen) | 272 | int m_copy_userstatus(int friendnumber, uint8_t * buf, uint32_t maxlen) |
313 | { | 273 | { |
314 | if(friendnumber >= numfriends || friendnumber < 0) | 274 | if(friendnumber >= numfriends || friendnumber < 0) |
315 | { | ||
316 | return -1; | 275 | return -1; |
317 | } | ||
318 | memset(buf, 0, maxlen); | 276 | memset(buf, 0, maxlen); |
319 | memcpy(buf, friendlist[friendnumber].userstatus, MIN(maxlen, MAX_USERSTATUS_LENGTH) - 1); | 277 | memcpy(buf, friendlist[friendnumber].userstatus, MIN(maxlen, MAX_USERSTATUS_LENGTH) - 1); |
320 | return 0; | 278 | return 0; |
@@ -333,9 +291,7 @@ static int send_userstatus(int friendnumber, uint8_t * status, uint16_t length) | |||
333 | static int set_friend_userstatus(int friendnumber, uint8_t * status, uint16_t length) | 291 | static int set_friend_userstatus(int friendnumber, uint8_t * status, uint16_t length) |
334 | { | 292 | { |
335 | if(friendnumber >= numfriends || friendnumber < 0) | 293 | if(friendnumber >= numfriends || friendnumber < 0) |
336 | { | ||
337 | return -1; | 294 | return -1; |
338 | } | ||
339 | uint8_t *newstatus = calloc(length, 1); | 295 | uint8_t *newstatus = calloc(length, 1); |
340 | memcpy(newstatus, status, length); | 296 | memcpy(newstatus, status, length); |
341 | free(friendlist[friendnumber].userstatus); | 297 | free(friendlist[friendnumber].userstatus); |
@@ -344,10 +300,8 @@ static int set_friend_userstatus(int friendnumber, uint8_t * status, uint16_t le | |||
344 | return 0; | 300 | return 0; |
345 | } | 301 | } |
346 | 302 | ||
347 | /* | 303 | /* static void (*friend_request)(uint8_t *, uint8_t *, uint16_t); |
348 | static void (*friend_request)(uint8_t *, uint8_t *, uint16_t); | 304 | static uint8_t friend_request_isset = 0; */ |
349 | static uint8_t friend_request_isset = 0; | ||
350 | */ | ||
351 | /* set the function that will be executed when a friend request is received. */ | 305 | /* set the function that will be executed when a friend request is received. */ |
352 | void m_callback_friendrequest(void (*function)(uint8_t *, uint8_t *, uint16_t)) | 306 | void m_callback_friendrequest(void (*function)(uint8_t *, uint8_t *, uint16_t)) |
353 | { | 307 | { |
@@ -406,16 +360,12 @@ static void doFriends() | |||
406 | if(friendlist[i].status == 1) | 360 | if(friendlist[i].status == 1) |
407 | { | 361 | { |
408 | int fr = send_friendrequest(friendlist[i].client_id, friendlist[i].info, friendlist[i].info_size); | 362 | int fr = send_friendrequest(friendlist[i].client_id, friendlist[i].info, friendlist[i].info_size); |
409 | if(fr == 0)/*TODO: This needs to be fixed so that it sends the friend requests a couple of times in case | 363 | if(fr == 0) /* TODO: This needs to be fixed so that it sends the friend requests a couple of times in case |
410 | of packet loss*/ | 364 | of packet loss */ |
411 | { | ||
412 | friendlist[i].status = 2; | 365 | friendlist[i].status = 2; |
413 | } | ||
414 | else | 366 | else |
415 | if(fr > 0) | 367 | if(fr > 0) |
416 | { | ||
417 | friendlist[i].status = 2; | 368 | friendlist[i].status = 2; |
418 | } | ||
419 | } | 369 | } |
420 | if(friendlist[i].status == 2 || friendlist[i].status == 3) /* friend is not online */ | 370 | if(friendlist[i].status == 2 || friendlist[i].status == 3) /* friend is not online */ |
421 | { | 371 | { |
@@ -425,12 +375,10 @@ static void doFriends() | |||
425 | { | 375 | { |
426 | send_friendrequest(friendlist[i].client_id, friendlist[i].info, friendlist[i].info_size); | 376 | send_friendrequest(friendlist[i].client_id, friendlist[i].info, friendlist[i].info_size); |
427 | friendlist[i].friend_request_id = unix_time(); | 377 | friendlist[i].friend_request_id = unix_time(); |
428 | |||
429 | } | 378 | } |
430 | } | 379 | } |
431 | IP_Port friendip = DHT_getfriendip(friendlist[i].client_id); | 380 | IP_Port friendip = DHT_getfriendip(friendlist[i].client_id); |
432 | switch(is_cryptoconnected(friendlist[i].crypt_connection_id)) | 381 | switch(is_cryptoconnected(friendlist[i].crypt_connection_id)) { |
433 | { | ||
434 | case 0: | 382 | case 0: |
435 | if (friendip.ip.i > 1) | 383 | if (friendip.ip.i > 1) |
436 | friendlist[i].crypt_connection_id = crypto_connect(friendlist[i].client_id, friendip); | 384 | friendlist[i].crypt_connection_id = crypto_connect(friendlist[i].client_id, friendip); |
@@ -449,19 +397,11 @@ static void doFriends() | |||
449 | while(friendlist[i].status == 4) /* friend is online */ | 397 | while(friendlist[i].status == 4) /* friend is online */ |
450 | { | 398 | { |
451 | if(friendlist[i].name_sent == 0) | 399 | if(friendlist[i].name_sent == 0) |
452 | { | ||
453 | if(m_sendname(i, self_name)) | 400 | if(m_sendname(i, self_name)) |
454 | { | ||
455 | friendlist[i].name_sent = 1; | 401 | friendlist[i].name_sent = 1; |
456 | } | ||
457 | } | ||
458 | if(friendlist[i].userstatus_sent == 0) | 402 | if(friendlist[i].userstatus_sent == 0) |
459 | { | ||
460 | if(send_userstatus(i, self_userstatus, self_userstatus_len)) | 403 | if(send_userstatus(i, self_userstatus, self_userstatus_len)) |
461 | { | ||
462 | friendlist[i].userstatus_sent = 1; | 404 | friendlist[i].userstatus_sent = 1; |
463 | } | ||
464 | } | ||
465 | len = read_cryptpacket(friendlist[i].crypt_connection_id, temp); | 405 | len = read_cryptpacket(friendlist[i].crypt_connection_id, temp); |
466 | if(len > 0) | 406 | if(len > 0) |
467 | { | 407 | { |
@@ -489,9 +429,7 @@ static void doFriends() | |||
489 | } | 429 | } |
490 | case PACKET_ID_MESSAGE: { | 430 | case PACKET_ID_MESSAGE: { |
491 | if(friend_message_isset) | 431 | if(friend_message_isset) |
492 | { | ||
493 | (*friend_message)(i, temp + 1, len - 1); | 432 | (*friend_message)(i, temp + 1, len - 1); |
494 | } | ||
495 | break; | 433 | break; |
496 | } | 434 | } |
497 | } | 435 | } |
@@ -559,14 +497,10 @@ void doMessenger() | |||
559 | /* { */ | 497 | /* { */ |
560 | if(DHT_handlepacket(data, length, ip_port) && LosslessUDP_handlepacket(data, length, ip_port) && | 498 | if(DHT_handlepacket(data, length, ip_port) && LosslessUDP_handlepacket(data, length, ip_port) && |
561 | friendreq_handlepacket(data, length, ip_port) && LANdiscovery_handlepacket(data, length, ip_port)) | 499 | friendreq_handlepacket(data, length, ip_port) && LANdiscovery_handlepacket(data, length, ip_port)) |
562 | { | ||
563 | /* if packet is discarded */ | 500 | /* if packet is discarded */ |
564 | printf("Received unhandled packet with length: %u\n", length); | 501 | printf("Received unhandled packet with length: %u\n", length); |
565 | } | ||
566 | else | 502 | else |
567 | { | ||
568 | printf("Received handled packet with length: %u\n", length); | 503 | printf("Received handled packet with length: %u\n", length); |
569 | } | ||
570 | /* } */ | 504 | /* } */ |
571 | printf("Status: %u %u %u\n",friendlist[0].status ,is_cryptoconnected(friendlist[0].crypt_connection_id), friendlist[0].crypt_connection_id); | 505 | printf("Status: %u %u %u\n",friendlist[0].status ,is_cryptoconnected(friendlist[0].crypt_connection_id), friendlist[0].crypt_connection_id); |
572 | #else | 506 | #else |
@@ -612,13 +546,9 @@ void Messenger_save(uint8_t * data) | |||
612 | int Messenger_load(uint8_t * data, uint32_t length) | 546 | int Messenger_load(uint8_t * data, uint32_t length) |
613 | { | 547 | { |
614 | if(length == ~0) | 548 | if(length == ~0) |
615 | { | ||
616 | return -1; | 549 | return -1; |
617 | } | ||
618 | if(length < crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES + sizeof(uint32_t) * 2) | 550 | if(length < crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES + sizeof(uint32_t) * 2) |
619 | { | ||
620 | return -1; | 551 | return -1; |
621 | } | ||
622 | length -= crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES + sizeof(uint32_t) * 2; | 552 | length -= crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES + sizeof(uint32_t) * 2; |
623 | load_keys(data); | 553 | load_keys(data); |
624 | data += crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES; | 554 | data += crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES; |
@@ -627,21 +557,15 @@ int Messenger_load(uint8_t * data, uint32_t length) | |||
627 | data += sizeof(size); | 557 | data += sizeof(size); |
628 | 558 | ||
629 | if(length < size) | 559 | if(length < size) |
630 | { | ||
631 | return -1; | 560 | return -1; |
632 | } | ||
633 | length -= size; | 561 | length -= size; |
634 | if(DHT_load(data, size) == -1) | 562 | if(DHT_load(data, size) == -1) |
635 | { | ||
636 | return -1; | 563 | return -1; |
637 | } | ||
638 | data += size; | 564 | data += size; |
639 | memcpy(&size, data, sizeof(size)); | 565 | memcpy(&size, data, sizeof(size)); |
640 | data += sizeof(size); | 566 | data += sizeof(size); |
641 | if(length != size || length % sizeof(Friend) != 0) | 567 | if(length != size || length % sizeof(Friend) != 0) |
642 | { | ||
643 | return -1; | 568 | return -1; |
644 | } | ||
645 | 569 | ||
646 | Friend * temp = malloc(size); | 570 | Friend * temp = malloc(size); |
647 | memcpy(temp, data, size); | 571 | memcpy(temp, data, size); |