summaryrefslogtreecommitdiff
path: root/core/Messenger.c
diff options
context:
space:
mode:
authorirungentoo <irungentoo@gmail.com>2013-07-20 07:55:21 -0400
committerirungentoo <irungentoo@gmail.com>2013-07-20 07:55:21 -0400
commit11e94066f710a602a9a071b028036a2b35de2741 (patch)
tree0a6c037e40c0d8744516715c7b4e126d94e75841 /core/Messenger.c
parent41cc5b19caae05fb751dc9b066b1ea98c263f585 (diff)
parenta72777ed70ed6bfcda8a532c52e0d64241a8bb71 (diff)
Merge branch 'master' of https://github.com/sometwo/ProjectTox-Core into sometwo-master
Conflicts: core/Messenger.c
Diffstat (limited to 'core/Messenger.c')
-rw-r--r--core/Messenger.c140
1 files changed, 70 insertions, 70 deletions
diff --git a/core/Messenger.c b/core/Messenger.c
index bb581a45..25ce067d 100644
--- a/core/Messenger.c
+++ b/core/Messenger.c
@@ -29,15 +29,15 @@ typedef struct
29{ 29{
30 uint8_t client_id[CLIENT_ID_SIZE]; 30 uint8_t client_id[CLIENT_ID_SIZE];
31 int crypt_connection_id; 31 int crypt_connection_id;
32 int friend_request_id; //id of the friend request corresponding to the current friend request to the current friend. 32 int friend_request_id; /* id of the friend request corresponding to the current friend request to the current friend. */
33 uint8_t status;//0 if no friend, 1 if added, 2 if friend request sent, 3 if confirmed friend, 4 if online. 33 uint8_t status; /* 0 if no friend, 1 if added, 2 if friend request sent, 3 if confirmed friend, 4 if online. */
34 uint8_t info[MAX_DATA_SIZE]; //the data that is sent during the friend requests we do 34 uint8_t info[MAX_DATA_SIZE]; /* the data that is sent during the friend requests we do */
35 uint8_t name[MAX_NAME_LENGTH]; 35 uint8_t name[MAX_NAME_LENGTH];
36 uint8_t name_sent;//0 if we didn't send our name to this friend 1 if we have. 36 uint8_t name_sent; /* 0 if we didn't send our name to this friend 1 if we have. */
37 uint8_t *userstatus; 37 uint8_t *userstatus;
38 uint16_t userstatus_length; 38 uint16_t userstatus_length;
39 uint8_t userstatus_sent; 39 uint8_t userstatus_sent;
40 uint16_t info_size; //length of the info 40 uint16_t info_size; /* length of the info */
41}Friend; 41}Friend;
42 42
43 43
@@ -54,13 +54,13 @@ static Friend friendlist[MAX_NUM_FRIENDS];
54 54
55static uint32_t numfriends; 55static uint32_t numfriends;
56 56
57//1 if we are online 57/* 1 if we are online
58//0 if we are offline 58 0 if we are offline
59//static uint8_t online; 59 static uint8_t online; */
60 60
61 61
62//return the friend id associated to that public key. 62/* return the friend id associated to that public key.
63//return -1 if no such friend 63 return -1 if no such friend */
64int getfriend_id(uint8_t * client_id) 64int getfriend_id(uint8_t * client_id)
65{ 65{
66 uint32_t i; 66 uint32_t i;
@@ -78,10 +78,10 @@ int getfriend_id(uint8_t * client_id)
78} 78}
79 79
80 80
81//copies the public key associated to that friend id into client_id buffer. 81/* copies the public key associated to that friend id into client_id buffer.
82//make sure that client_id is of size CLIENT_ID_SIZE. 82 make sure that client_id is of size CLIENT_ID_SIZE.
83//returns 0 if success 83 return 0 if success
84//return -1 if failure. 84 return -1 if failure. */
85int getclient_id(int friend_id, uint8_t * client_id) 85int getclient_id(int friend_id, uint8_t * client_id)
86{ 86{
87 if(friend_id >= numfriends || friend_id < 0) 87 if(friend_id >= numfriends || friend_id < 0)
@@ -98,12 +98,12 @@ int getclient_id(int friend_id, uint8_t * client_id)
98} 98}
99 99
100 100
101//add a friend 101/* add a friend
102//set the data that will be sent along with friend request 102 set the data that will be sent along with friend request
103//client_id is the client id of the friend 103 client_id is the client id of the friend
104//data is the data and length is the length 104 data is the data and length is the length
105//returns the friend number if success 105 returns the friend number if success
106//return -1 if failure. 106 return -1 if failure. */
107int m_addfriend(uint8_t * client_id, uint8_t * data, uint16_t length) 107int m_addfriend(uint8_t * client_id, uint8_t * data, uint16_t length)
108{ 108{
109 if(length == 0 || length >= 109 if(length == 0 || length >=
@@ -166,9 +166,9 @@ int m_addfriend_norequest(uint8_t * client_id)
166 return -1; 166 return -1;
167} 167}
168 168
169//remove a friend 169/* remove a friend
170//returns 0 if success 170 return 0 if success
171//return -1 if failure. 171 return -1 if failure */
172int m_delfriend(int friendnumber) 172int m_delfriend(int friendnumber)
173{ 173{
174 if(friendnumber >= numfriends || friendnumber < 0) 174 if(friendnumber >= numfriends || friendnumber < 0)
@@ -193,11 +193,11 @@ int m_delfriend(int friendnumber)
193} 193}
194 194
195 195
196//return 4 if friend is online 196/* return 4 if friend is online
197//return 3 if friend is confirmed 197 return 3 if friend is confirmed
198//return 2 if the friend request was sent 198 return 2 if the friend request was sent
199//return 1 if the friend was added 199 return 1 if the friend was added
200//return 0 if there is no friend with that number. 200 return 0 if there is no friend with that number */
201int m_friendstatus(int friendnumber) 201int m_friendstatus(int friendnumber)
202{ 202{
203 if(friendnumber < 0 || friendnumber >= MAX_NUM_FRIENDS) 203 if(friendnumber < 0 || friendnumber >= MAX_NUM_FRIENDS)
@@ -208,9 +208,9 @@ int m_friendstatus(int friendnumber)
208} 208}
209 209
210 210
211//send a text chat message to an online friend. 211/* send a text chat message to an online friend
212//returns 1 if packet was successfully put into the send queue 212 return 1 if packet was successfully put into the send queue
213//return 0 if it was not. 213 return 0 if it was not */
214int m_sendmessage(int friendnumber, uint8_t * message, uint32_t length) 214int m_sendmessage(int friendnumber, uint8_t * message, uint32_t length)
215{ 215{
216 if(friendnumber < 0 || friendnumber >= MAX_NUM_FRIENDS) 216 if(friendnumber < 0 || friendnumber >= MAX_NUM_FRIENDS)
@@ -218,7 +218,7 @@ int m_sendmessage(int friendnumber, uint8_t * message, uint32_t length)
218 return 0; 218 return 0;
219 } 219 }
220 if(length >= MAX_DATA_SIZE || friendlist[friendnumber].status != 4) 220 if(length >= MAX_DATA_SIZE || friendlist[friendnumber].status != 4)
221 //this does not mean the maximum message length is MAX_DATA_SIZE - 1, it is actually 17 bytes less. 221 /* this does not mean the maximum message length is MAX_DATA_SIZE - 1, it is actually 17 bytes less. */
222 { 222 {
223 return 0; 223 return 0;
224 } 224 }
@@ -228,7 +228,7 @@ int m_sendmessage(int friendnumber, uint8_t * message, uint32_t length)
228 return write_cryptpacket(friendlist[friendnumber].crypt_connection_id, temp, length + 1); 228 return write_cryptpacket(friendlist[friendnumber].crypt_connection_id, temp, length + 1);
229} 229}
230 230
231//send a name packet to friendnumber 231/* send a name packet to friendnumber */
232static int m_sendname(int friendnumber, uint8_t * name) 232static int m_sendname(int friendnumber, uint8_t * name)
233{ 233{
234 uint8_t temp[MAX_NAME_LENGTH + 1]; 234 uint8_t temp[MAX_NAME_LENGTH + 1];
@@ -237,9 +237,9 @@ static int m_sendname(int friendnumber, uint8_t * name)
237 return write_cryptpacket(friendlist[friendnumber].crypt_connection_id, temp, MAX_NAME_LENGTH + 1); 237 return write_cryptpacket(friendlist[friendnumber].crypt_connection_id, temp, MAX_NAME_LENGTH + 1);
238} 238}
239 239
240//set the name of a friend 240/* set the name of a friend
241//return 0 if success 241 return 0 if success
242//return -1 if failure 242 return -1 if failure */
243 243
244static int setfriendname(int friendnumber, uint8_t * name) 244static int setfriendname(int friendnumber, uint8_t * name)
245{ 245{
@@ -252,10 +252,10 @@ static int setfriendname(int friendnumber, uint8_t * name)
252} 252}
253 253
254 254
255//Set our nickname 255/* Set our nickname
256//name must be a string of maximum MAX_NAME_LENGTH length. 256 name must be a string of maximum MAX_NAME_LENGTH length.
257//return 0 if success 257 return 0 if success
258//return -1 if failure 258 return -1 if failure */
259int setname(uint8_t * name, uint16_t length) 259int setname(uint8_t * name, uint16_t length)
260{ 260{
261 if(length > MAX_NAME_LENGTH) 261 if(length > MAX_NAME_LENGTH)
@@ -271,11 +271,11 @@ int setname(uint8_t * name, uint16_t length)
271 return 0; 271 return 0;
272} 272}
273 273
274//get name of friendnumber 274/* get name of friendnumber
275//put it in name 275 put it in name
276//name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH bytes. 276 name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH bytes.
277//return 0 if success 277 return 0 if success
278//return -1 if failure 278 return -1 if failure */
279int getname(int friendnumber, uint8_t * name) 279int getname(int friendnumber, uint8_t * name)
280{ 280{
281 if(friendnumber >= numfriends || friendnumber < 0) 281 if(friendnumber >= numfriends || friendnumber < 0)
@@ -306,8 +306,8 @@ int m_set_userstatus(uint8_t *status, uint16_t length)
306 return 0; 306 return 0;
307} 307}
308 308
309// return the size of friendnumber's user status 309/* return the size of friendnumber's user status
310// guaranteed to be at most MAX_USERSTATUS_LENGTH 310 guaranteed to be at most MAX_USERSTATUS_LENGTH */
311int m_get_userstatus_size(int friendnumber) 311int m_get_userstatus_size(int friendnumber)
312{ 312{
313 if(friendnumber >= numfriends || friendnumber < 0) 313 if(friendnumber >= numfriends || friendnumber < 0)
@@ -317,8 +317,8 @@ int m_get_userstatus_size(int friendnumber)
317 return friendlist[friendnumber].userstatus_length; 317 return friendlist[friendnumber].userstatus_length;
318} 318}
319 319
320// copy the user status of friendnumber into buf, truncating if needed to maxlen 320/* copy the user status of friendnumber into buf, truncating if needed to maxlen
321// bytes, use m_get_userstatus_size to find out how much you need to allocate 321 bytes, use m_get_userstatus_size to find out how much you need to allocate */
322int m_copy_userstatus(int friendnumber, uint8_t * buf, uint32_t maxlen) 322int m_copy_userstatus(int friendnumber, uint8_t * buf, uint32_t maxlen)
323{ 323{
324 if(friendnumber >= numfriends || friendnumber < 0) 324 if(friendnumber >= numfriends || friendnumber < 0)
@@ -357,7 +357,7 @@ static int set_friend_userstatus(int friendnumber, uint8_t * status, uint16_t le
357static void (*friend_request)(uint8_t *, uint8_t *, uint16_t); 357static void (*friend_request)(uint8_t *, uint8_t *, uint16_t);
358static uint8_t friend_request_isset = 0; 358static uint8_t friend_request_isset = 0;
359 359
360//set the function that will be executed when a friend request is received. 360/* set the function that will be executed when a friend request is received. */
361void m_callback_friendrequest(void (*function)(uint8_t *, uint8_t *, uint16_t)) 361void m_callback_friendrequest(void (*function)(uint8_t *, uint8_t *, uint16_t))
362{ 362{
363 friend_request = function; 363 friend_request = function;
@@ -368,7 +368,7 @@ void m_callback_friendrequest(void (*function)(uint8_t *, uint8_t *, uint16_t))
368static void (*friend_message)(int, uint8_t *, uint16_t); 368static void (*friend_message)(int, uint8_t *, uint16_t);
369static uint8_t friend_message_isset = 0; 369static uint8_t friend_message_isset = 0;
370 370
371//set the function that will be executed when a message from a friend is received. 371/* set the function that will be executed when a message from a friend is received. */
372void m_callback_friendmessage(void (*function)(int, uint8_t *, uint16_t)) 372void m_callback_friendmessage(void (*function)(int, uint8_t *, uint16_t))
373{ 373{
374 friend_message = function; 374 friend_message = function;
@@ -393,7 +393,7 @@ void m_callback_userstatus(void (*function)(int, uint8_t *, uint16_t))
393} 393}
394 394
395#define PORT 33445 395#define PORT 33445
396//run this at startup 396/* run this at startup */
397void initMessenger() 397void initMessenger()
398{ 398{
399 new_keys(); 399 new_keys();
@@ -405,7 +405,7 @@ void initMessenger()
405} 405}
406 406
407static void doFriends() 407static void doFriends()
408{//TODO: add incoming connections and some other stuff. 408{/* TODO: add incoming connections and some other stuff. */
409 uint32_t i; 409 uint32_t i;
410 int len; 410 int len;
411 uint8_t temp[MAX_DATA_SIZE]; 411 uint8_t temp[MAX_DATA_SIZE];
@@ -415,7 +415,7 @@ static void doFriends()
415 { 415 {
416 IP_Port friendip = DHT_getfriendip(friendlist[i].client_id); 416 IP_Port friendip = DHT_getfriendip(friendlist[i].client_id);
417 int request = check_friendrequest(friendlist[i].friend_request_id); 417 int request = check_friendrequest(friendlist[i].friend_request_id);
418 //printf("\n%u %u %u\n", friendip.ip.i, request, friendlist[i].friend_request_id); 418 /* printf("\n%u %u %u\n", friendip.ip.i, request, friendlist[i].friend_request_id); */
419 if(friendip.ip.i > 1 && request == -1) 419 if(friendip.ip.i > 1 && request == -1)
420 { 420 {
421 friendlist[i].friend_request_id = send_friendrequest(friendlist[i].client_id, 421 friendlist[i].friend_request_id = send_friendrequest(friendlist[i].client_id,
@@ -423,9 +423,9 @@ static void doFriends()
423 friendlist[i].status = 2; 423 friendlist[i].status = 2;
424 } 424 }
425 } 425 }
426 if(friendlist[i].status == 2 || friendlist[i].status == 3) //friend is not online 426 if(friendlist[i].status == 2 || friendlist[i].status == 3) /* friend is not online */
427 { 427 {
428 check_friendrequest(friendlist[i].friend_request_id);//for now this is used to kill the friend request 428 check_friendrequest(friendlist[i].friend_request_id); /* for now this is used to kill the friend request */
429 IP_Port friendip = DHT_getfriendip(friendlist[i].client_id); 429 IP_Port friendip = DHT_getfriendip(friendlist[i].client_id);
430 switch(is_cryptoconnected(friendlist[i].crypt_connection_id)) 430 switch(is_cryptoconnected(friendlist[i].crypt_connection_id))
431 { 431 {
@@ -433,7 +433,7 @@ static void doFriends()
433 if (friendip.ip.i > 1) 433 if (friendip.ip.i > 1)
434 friendlist[i].crypt_connection_id = crypto_connect(friendlist[i].client_id, friendip); 434 friendlist[i].crypt_connection_id = crypto_connect(friendlist[i].client_id, friendip);
435 break; 435 break;
436 case 3: // Connection is established 436 case 3: /* Connection is established */
437 friendlist[i].status = 4; 437 friendlist[i].status = 4;
438 break; 438 break;
439 case 4: 439 case 4:
@@ -444,7 +444,7 @@ static void doFriends()
444 break; 444 break;
445 } 445 }
446 } 446 }
447 while(friendlist[i].status == 4) //friend is online 447 while(friendlist[i].status == 4) /* friend is online */
448 { 448 {
449 if(friendlist[i].name_sent == 0) 449 if(friendlist[i].name_sent == 0)
450 { 450 {
@@ -468,10 +468,10 @@ static void doFriends()
468 if (len != MAX_NAME_LENGTH + 1) break; 468 if (len != MAX_NAME_LENGTH + 1) break;
469 if(friend_namechange_isset) 469 if(friend_namechange_isset)
470 { 470 {
471 friend_namechange(i, temp + 1, MAX_NAME_LENGTH); // todo: use the actual length 471 friend_namechange(i, temp + 1, MAX_NAME_LENGTH); /* TODO: use the actual length */
472 } 472 }
473 memcpy(friendlist[i].name, temp + 1, MAX_NAME_LENGTH); 473 memcpy(friendlist[i].name, temp + 1, MAX_NAME_LENGTH);
474 friendlist[i].name[MAX_NAME_LENGTH - 1] = 0;//make sure the NULL terminator is present. 474 friendlist[i].name[MAX_NAME_LENGTH - 1] = 0; /* make sure the NULL terminator is present. */
475 break; 475 break;
476 } 476 }
477 case PACKET_ID_USERSTATUS: { 477 case PACKET_ID_USERSTATUS: {
@@ -496,7 +496,7 @@ static void doFriends()
496 } 496 }
497 else 497 else
498 { 498 {
499 if(is_cryptoconnected(friendlist[i].crypt_connection_id) == 4)//if the connection timed out, kill it 499 if(is_cryptoconnected(friendlist[i].crypt_connection_id) == 4) /* if the connection timed out, kill it */
500 { 500 {
501 crypto_kill(friendlist[i].crypt_connection_id); 501 crypto_kill(friendlist[i].crypt_connection_id);
502 friendlist[i].crypt_connection_id = -1; 502 friendlist[i].crypt_connection_id = -1;
@@ -545,7 +545,7 @@ static void doInbound()
545 } 545 }
546} 546}
547 547
548//the main loop that needs to be run at least 200 times per second. 548/* the main loop that needs to be run at least 200 times per second. */
549void doMessenger() 549void doMessenger()
550{ 550{
551 IP_Port ip_port; 551 IP_Port ip_port;
@@ -554,18 +554,18 @@ void doMessenger()
554 while(receivepacket(&ip_port, data, &length) != -1) 554 while(receivepacket(&ip_port, data, &length) != -1)
555 { 555 {
556#ifdef DEBUG 556#ifdef DEBUG
557 //if(rand() % 3 != 1)//simulate packet loss 557 /* if(rand() % 3 != 1) //simulate packet loss */
558 //{ 558 /* { */
559 if(DHT_handlepacket(data, length, ip_port) && LosslessUDP_handlepacket(data, length, ip_port)) 559 if(DHT_handlepacket(data, length, ip_port) && LosslessUDP_handlepacket(data, length, ip_port))
560 { 560 {
561 //if packet is discarded 561 /* if packet is discarded */
562 printf("Received unhandled packet with length: %u\n", length); 562 printf("Received unhandled packet with length: %u\n", length);
563 } 563 }
564 else 564 else
565 { 565 {
566 printf("Received handled packet with length: %u\n", length); 566 printf("Received handled packet with length: %u\n", length);
567 } 567 }
568 //} 568 /* } */
569 printf("Status: %u %u %u\n",friendlist[0].status ,is_cryptoconnected(friendlist[0].crypt_connection_id), friendlist[0].crypt_connection_id); 569 printf("Status: %u %u %u\n",friendlist[0].status ,is_cryptoconnected(friendlist[0].crypt_connection_id), friendlist[0].crypt_connection_id);
570#else 570#else
571 DHT_handlepacket(data, length, ip_port); 571 DHT_handlepacket(data, length, ip_port);
@@ -581,14 +581,14 @@ void doMessenger()
581 doFriends(); 581 doFriends();
582} 582}
583 583
584//returns the size of the messenger data (for saving) 584/* returns the size of the messenger data (for saving) */
585uint32_t Messenger_size() 585uint32_t Messenger_size()
586{ 586{
587 return crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES 587 return crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES
588 + sizeof(uint32_t) + DHT_size() + sizeof(uint32_t) + sizeof(Friend) * numfriends; 588 + sizeof(uint32_t) + DHT_size() + sizeof(uint32_t) + sizeof(Friend) * numfriends;
589} 589}
590 590
591//save the messenger in data of size Messenger_size() 591/* save the messenger in data of size Messenger_size() */
592void Messenger_save(uint8_t * data) 592void Messenger_save(uint8_t * data)
593{ 593{
594 save_keys(data); 594 save_keys(data);
@@ -604,7 +604,7 @@ void Messenger_save(uint8_t * data)
604 memcpy(data, friendlist, sizeof(Friend) * numfriends); 604 memcpy(data, friendlist, sizeof(Friend) * numfriends);
605} 605}
606 606
607//load the messenger from data of size length. 607/* load the messenger from data of size length. */
608int Messenger_load(uint8_t * data, uint32_t length) 608int Messenger_load(uint8_t * data, uint32_t length)
609{ 609{
610 if(length == ~0) 610 if(length == ~0)
@@ -651,7 +651,7 @@ int Messenger_load(uint8_t * data, uint32_t length)
651 { 651 {
652 int fnum = m_addfriend_norequest(temp[i].client_id); 652 int fnum = m_addfriend_norequest(temp[i].client_id);
653 setfriendname(fnum, temp[i].name); 653 setfriendname(fnum, temp[i].name);
654 //set_friend_userstatus(fnum, temp[i].userstatus, temp[i].userstatus_length); 654 /* set_friend_userstatus(fnum, temp[i].userstatus, temp[i].userstatus_length); */
655 } 655 }
656 } 656 }
657 free(temp); 657 free(temp);