diff options
Diffstat (limited to 'core/Messenger.c')
-rw-r--r-- | core/Messenger.c | 124 |
1 files changed, 62 insertions, 62 deletions
diff --git a/core/Messenger.c b/core/Messenger.c index fd95869f..86056df8 100644 --- a/core/Messenger.c +++ b/core/Messenger.c | |||
@@ -18,12 +18,12 @@ | |||
18 | * | 18 | * |
19 | * You should have received a copy of the GNU General Public License | 19 | * You should have received a copy of the GNU General Public License |
20 | * along with Tox. If not, see <http://www.gnu.org/licenses/>. | 20 | * along with Tox. If not, see <http://www.gnu.org/licenses/>. |
21 | * | 21 | * |
22 | */ | 22 | */ |
23 | 23 | ||
24 | #include "Messenger.h" | 24 | #include "Messenger.h" |
25 | #define MIN(a,b) (((a)<(b))?(a):(b)) | 25 | #define MIN(a,b) (((a)<(b))?(a):(b)) |
26 | 26 | ||
27 | typedef struct { | 27 | typedef struct { |
28 | uint8_t client_id[CLIENT_ID_SIZE]; | 28 | uint8_t client_id[CLIENT_ID_SIZE]; |
29 | int crypt_connection_id; | 29 | int crypt_connection_id; |
@@ -49,7 +49,7 @@ static uint16_t self_userstatus_len; | |||
49 | static Friend friendlist[MAX_NUM_FRIENDS]; | 49 | static Friend friendlist[MAX_NUM_FRIENDS]; |
50 | 50 | ||
51 | static uint32_t numfriends; | 51 | static uint32_t numfriends; |
52 | 52 | ||
53 | /* 1 if we are online | 53 | /* 1 if we are online |
54 | 0 if we are offline | 54 | 0 if we are offline |
55 | static uint8_t online; */ | 55 | static uint8_t online; */ |
@@ -95,7 +95,7 @@ int getclient_id(int friend_id, uint8_t *client_id) | |||
95 | int m_addfriend(uint8_t *client_id, uint8_t *data, uint16_t length) | 95 | int m_addfriend(uint8_t *client_id, uint8_t *data, uint16_t length) |
96 | { | 96 | { |
97 | if (length == 0 || length >= | 97 | if (length == 0 || length >= |
98 | (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)) |
99 | return -1; | 99 | return -1; |
100 | if (memcmp(client_id, self_public_key, crypto_box_PUBLICKEYBYTES) == 0) | 100 | if (memcmp(client_id, self_public_key, crypto_box_PUBLICKEYBYTES) == 0) |
101 | return -1; | 101 | return -1; |
@@ -113,7 +113,7 @@ int m_addfriend(uint8_t *client_id, uint8_t *data, uint16_t length) | |||
113 | friendlist[i].userstatus_length = 1; | 113 | friendlist[i].userstatus_length = 1; |
114 | memcpy(friendlist[i].info, data, length); | 114 | memcpy(friendlist[i].info, data, length); |
115 | friendlist[i].info_size = length; | 115 | friendlist[i].info_size = length; |
116 | 116 | ||
117 | ++numfriends; | 117 | ++numfriends; |
118 | return i; | 118 | return i; |
119 | } | 119 | } |
@@ -183,7 +183,7 @@ int m_sendmessage(int friendnumber, uint8_t *message, uint32_t length) | |||
183 | if (friendnumber < 0 || friendnumber >= MAX_NUM_FRIENDS) | 183 | if (friendnumber < 0 || friendnumber >= MAX_NUM_FRIENDS) |
184 | return 0; | 184 | return 0; |
185 | if (length >= MAX_DATA_SIZE || friendlist[friendnumber].status != 4) | 185 | if (length >= MAX_DATA_SIZE || friendlist[friendnumber].status != 4) |
186 | /* this does not mean the maximum message length is MAX_DATA_SIZE - 1, it is actually 17 bytes less. */ | 186 | /* this does not mean the maximum message length is MAX_DATA_SIZE - 1, it is actually 17 bytes less. */ |
187 | return 0; | 187 | return 0; |
188 | uint8_t temp[MAX_DATA_SIZE]; | 188 | uint8_t temp[MAX_DATA_SIZE]; |
189 | temp[0] = PACKET_ID_MESSAGE; | 189 | temp[0] = PACKET_ID_MESSAGE; |
@@ -349,7 +349,8 @@ int initMessenger() | |||
349 | 349 | ||
350 | //TODO: make this function not suck. | 350 | //TODO: make this function not suck. |
351 | static void doFriends() | 351 | static void doFriends() |
352 | {/* TODO: add incoming connections and some other stuff. */ | 352 | { |
353 | /* TODO: add incoming connections and some other stuff. */ | ||
353 | uint32_t i; | 354 | uint32_t i; |
354 | int len; | 355 | int len; |
355 | uint8_t temp[MAX_DATA_SIZE]; | 356 | uint8_t temp[MAX_DATA_SIZE]; |
@@ -370,19 +371,19 @@ static void doFriends() | |||
370 | } | 371 | } |
371 | IP_Port friendip = DHT_getfriendip(friendlist[i].client_id); | 372 | IP_Port friendip = DHT_getfriendip(friendlist[i].client_id); |
372 | switch (is_cryptoconnected(friendlist[i].crypt_connection_id)) { | 373 | switch (is_cryptoconnected(friendlist[i].crypt_connection_id)) { |
373 | case 0: | 374 | case 0: |
374 | if (friendip.ip.i > 1) | 375 | if (friendip.ip.i > 1) |
375 | friendlist[i].crypt_connection_id = crypto_connect(friendlist[i].client_id, friendip); | 376 | friendlist[i].crypt_connection_id = crypto_connect(friendlist[i].client_id, friendip); |
376 | break; | 377 | break; |
377 | case 3: /* Connection is established */ | 378 | case 3: /* Connection is established */ |
378 | friendlist[i].status = 4; | 379 | friendlist[i].status = 4; |
379 | break; | 380 | break; |
380 | case 4: | 381 | case 4: |
381 | crypto_kill(friendlist[i].crypt_connection_id); | 382 | crypto_kill(friendlist[i].crypt_connection_id); |
382 | friendlist[i].crypt_connection_id = -1; | 383 | friendlist[i].crypt_connection_id = -1; |
383 | break; | 384 | break; |
384 | default: | 385 | default: |
385 | break; | 386 | break; |
386 | } | 387 | } |
387 | } | 388 | } |
388 | while (friendlist[i].status == 4) { /* friend is online */ | 389 | while (friendlist[i].status == 4) { /* friend is online */ |
@@ -397,38 +398,37 @@ static void doFriends() | |||
397 | len = read_cryptpacket(friendlist[i].crypt_connection_id, temp); | 398 | len = read_cryptpacket(friendlist[i].crypt_connection_id, temp); |
398 | if (len > 0) { | 399 | if (len > 0) { |
399 | switch (temp[0]) { | 400 | switch (temp[0]) { |
400 | case PACKET_ID_NICKNAME: { | 401 | case PACKET_ID_NICKNAME: { |
401 | if (len != MAX_NAME_LENGTH + 1) | 402 | if (len != MAX_NAME_LENGTH + 1) |
402 | break; | ||
403 | if(friend_namechange_isset) | ||
404 | friend_namechange(i, temp + 1, MAX_NAME_LENGTH); /* TODO: use the actual length */ | ||
405 | memcpy(friendlist[i].name, temp + 1, MAX_NAME_LENGTH); | ||
406 | friendlist[i].name[MAX_NAME_LENGTH - 1] = 0; /* make sure the NULL terminator is present. */ | ||
407 | break; | 403 | break; |
408 | } | 404 | if(friend_namechange_isset) |
409 | case PACKET_ID_USERSTATUS: { | 405 | friend_namechange(i, temp + 1, MAX_NAME_LENGTH); /* TODO: use the actual length */ |
410 | uint8_t *status = calloc(MIN(len - 1, MAX_USERSTATUS_LENGTH), 1); | 406 | memcpy(friendlist[i].name, temp + 1, MAX_NAME_LENGTH); |
411 | memcpy(status, temp + 1, MIN(len - 1, MAX_USERSTATUS_LENGTH)); | 407 | friendlist[i].name[MAX_NAME_LENGTH - 1] = 0; /* make sure the NULL terminator is present. */ |
412 | if (friend_statuschange_isset) | 408 | break; |
413 | friend_statuschange(i, status, MIN(len - 1, MAX_USERSTATUS_LENGTH)); | ||
414 | set_friend_userstatus(i, status, MIN(len - 1, MAX_USERSTATUS_LENGTH)); | ||
415 | free(status); | ||
416 | break; | ||
417 | } | ||
418 | case PACKET_ID_MESSAGE: { | ||
419 | if (friend_message_isset) | ||
420 | (*friend_message)(i, temp + 1, len - 1); | ||
421 | break; | ||
422 | } | ||
423 | } | 409 | } |
424 | } | 410 | case PACKET_ID_USERSTATUS: { |
425 | else { | 411 | uint8_t *status = calloc(MIN(len - 1, MAX_USERSTATUS_LENGTH), 1); |
426 | if (is_cryptoconnected(friendlist[i].crypt_connection_id) == 4) { /* if the connection timed out, kill it */ | 412 | memcpy(status, temp + 1, MIN(len - 1, MAX_USERSTATUS_LENGTH)); |
427 | crypto_kill(friendlist[i].crypt_connection_id); | 413 | if (friend_statuschange_isset) |
428 | friendlist[i].crypt_connection_id = -1; | 414 | friend_statuschange(i, status, MIN(len - 1, MAX_USERSTATUS_LENGTH)); |
429 | friendlist[i].status = 3; | 415 | set_friend_userstatus(i, status, MIN(len - 1, MAX_USERSTATUS_LENGTH)); |
430 | } | 416 | free(status); |
431 | break; | 417 | break; |
418 | } | ||
419 | case PACKET_ID_MESSAGE: { | ||
420 | if (friend_message_isset) | ||
421 | (*friend_message)(i, temp + 1, len - 1); | ||
422 | break; | ||
423 | } | ||
424 | } | ||
425 | } else { | ||
426 | if (is_cryptoconnected(friendlist[i].crypt_connection_id) == 4) { /* if the connection timed out, kill it */ | ||
427 | crypto_kill(friendlist[i].crypt_connection_id); | ||
428 | friendlist[i].crypt_connection_id = -1; | ||
429 | friendlist[i].status = 3; | ||
430 | } | ||
431 | break; | ||
432 | } | 432 | } |
433 | } | 433 | } |
434 | } | 434 | } |
@@ -443,11 +443,11 @@ static void doInbound() | |||
443 | if (inconnection != -1) { | 443 | if (inconnection != -1) { |
444 | int friend_id = getfriend_id(public_key); | 444 | int friend_id = getfriend_id(public_key); |
445 | if (friend_id != -1) { | 445 | if (friend_id != -1) { |
446 | crypto_kill(friendlist[friend_id].crypt_connection_id); | 446 | crypto_kill(friendlist[friend_id].crypt_connection_id); |
447 | friendlist[friend_id].crypt_connection_id = | 447 | friendlist[friend_id].crypt_connection_id = |
448 | accept_crypto_inbound(inconnection, public_key, secret_nonce, session_key); | 448 | accept_crypto_inbound(inconnection, public_key, secret_nonce, session_key); |
449 | 449 | ||
450 | friendlist[friend_id].status = 3; | 450 | friendlist[friend_id].status = 3; |
451 | } | 451 | } |
452 | } | 452 | } |
453 | } | 453 | } |
@@ -477,8 +477,8 @@ void doMessenger() | |||
477 | #ifdef DEBUG | 477 | #ifdef DEBUG |
478 | /* if(rand() % 3 != 1) //simulate packet loss */ | 478 | /* if(rand() % 3 != 1) //simulate packet loss */ |
479 | /* { */ | 479 | /* { */ |
480 | if (DHT_handlepacket(data, length, ip_port) && LosslessUDP_handlepacket(data, length, ip_port) && | 480 | if (DHT_handlepacket(data, length, ip_port) && LosslessUDP_handlepacket(data, length, ip_port) && |
481 | friendreq_handlepacket(data, length, ip_port) && LANdiscovery_handlepacket(data, length, ip_port)) | 481 | friendreq_handlepacket(data, length, ip_port) && LANdiscovery_handlepacket(data, length, ip_port)) |
482 | /* if packet is discarded */ | 482 | /* if packet is discarded */ |
483 | printf("Received unhandled packet with length: %u\n", length); | 483 | printf("Received unhandled packet with length: %u\n", length); |
484 | else | 484 | else |
@@ -505,7 +505,7 @@ void doMessenger() | |||
505 | uint32_t Messenger_size() | 505 | uint32_t Messenger_size() |
506 | { | 506 | { |
507 | return crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES | 507 | return crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES |
508 | + sizeof(uint32_t) + DHT_size() + sizeof(uint32_t) + sizeof(Friend) * numfriends; | 508 | + sizeof(uint32_t) + DHT_size() + sizeof(uint32_t) + sizeof(Friend) * numfriends; |
509 | } | 509 | } |
510 | 510 | ||
511 | /* save the messenger in data of size Messenger_size() */ | 511 | /* save the messenger in data of size Messenger_size() */ |
@@ -537,7 +537,7 @@ int Messenger_load(uint8_t * data, uint32_t length) | |||
537 | uint32_t size; | 537 | uint32_t size; |
538 | memcpy(&size, data, sizeof(size)); | 538 | memcpy(&size, data, sizeof(size)); |
539 | data += sizeof(size); | 539 | data += sizeof(size); |
540 | 540 | ||
541 | if (length < size) | 541 | if (length < size) |
542 | return -1; | 542 | return -1; |
543 | length -= size; | 543 | length -= size; |
@@ -548,12 +548,12 @@ int Messenger_load(uint8_t * data, uint32_t length) | |||
548 | data += sizeof(size); | 548 | data += sizeof(size); |
549 | if (length != size || length % sizeof(Friend) != 0) | 549 | if (length != size || length % sizeof(Friend) != 0) |
550 | return -1; | 550 | return -1; |
551 | 551 | ||
552 | Friend * temp = malloc(size); | 552 | Friend * temp = malloc(size); |
553 | memcpy(temp, data, size); | 553 | memcpy(temp, data, size); |
554 | 554 | ||
555 | uint16_t num = size / sizeof(Friend); | 555 | uint16_t num = size / sizeof(Friend); |
556 | 556 | ||
557 | uint32_t i; | 557 | uint32_t i; |
558 | for (i = 0; i < num; ++i) { | 558 | for (i = 0; i < num; ++i) { |
559 | if(temp[i].status != 0) { | 559 | if(temp[i].status != 0) { |