summaryrefslogtreecommitdiff
path: root/core/Messenger.c
diff options
context:
space:
mode:
authorjvrv <unknown@unknown.unknown>2013-07-20 00:00:10 -0300
committerjvrv <unknown@unknown.unknown>2013-07-20 00:00:10 -0300
commita72777ed70ed6bfcda8a532c52e0d64241a8bb71 (patch)
treeeb1e70bace7fbaf39a5e80eb4d953dff28998068 /core/Messenger.c
parentac6003e932219bd15cbe50808c7ad13ea01aad8f (diff)
fix comments of some files
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 5a10c1da..e8de3b88 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)
@@ -356,7 +356,7 @@ static int set_friend_userstatus(int friendnumber, uint8_t * status, uint16_t le
356 356
357static void (*friend_request)(uint8_t *, uint8_t *, uint16_t); 357static void (*friend_request)(uint8_t *, uint8_t *, uint16_t);
358 358
359//set the function that will be executed when a friend request is received. 359/* set the function that will be executed when a friend request is received. */
360void m_callback_friendrequest(void (*function)(uint8_t *, uint8_t *, uint16_t)) 360void m_callback_friendrequest(void (*function)(uint8_t *, uint8_t *, uint16_t))
361{ 361{
362 friend_request = function; 362 friend_request = function;
@@ -365,7 +365,7 @@ void m_callback_friendrequest(void (*function)(uint8_t *, uint8_t *, uint16_t))
365 365
366static void (*friend_message)(int, uint8_t *, uint16_t); 366static void (*friend_message)(int, uint8_t *, uint16_t);
367 367
368//set the function that will be executed when a message from a friend is received. 368/* set the function that will be executed when a message from a friend is received. */
369void m_callback_friendmessage(void (*function)(int, uint8_t *, uint16_t)) 369void m_callback_friendmessage(void (*function)(int, uint8_t *, uint16_t))
370{ 370{
371 friend_message = function; 371 friend_message = function;
@@ -385,7 +385,7 @@ void m_callback_userstatus(void (*function)(int, uint8_t *, uint16_t))
385} 385}
386 386
387#define PORT 33445 387#define PORT 33445
388//run this at startup 388/* run this at startup */
389void initMessenger() 389void initMessenger()
390{ 390{
391 new_keys(); 391 new_keys();
@@ -397,7 +397,7 @@ void initMessenger()
397} 397}
398 398
399static void doFriends() 399static void doFriends()
400{//TODO: add incoming connections and some other stuff. 400{/* TODO: add incoming connections and some other stuff. */
401 uint32_t i; 401 uint32_t i;
402 int len; 402 int len;
403 uint8_t temp[MAX_DATA_SIZE]; 403 uint8_t temp[MAX_DATA_SIZE];
@@ -407,7 +407,7 @@ static void doFriends()
407 { 407 {
408 IP_Port friendip = DHT_getfriendip(friendlist[i].client_id); 408 IP_Port friendip = DHT_getfriendip(friendlist[i].client_id);
409 int request = check_friendrequest(friendlist[i].friend_request_id); 409 int request = check_friendrequest(friendlist[i].friend_request_id);
410 //printf("\n%u %u %u\n", friendip.ip.i, request, friendlist[i].friend_request_id); 410 /* printf("\n%u %u %u\n", friendip.ip.i, request, friendlist[i].friend_request_id); */
411 if(friendip.ip.i > 1 && request == -1) 411 if(friendip.ip.i > 1 && request == -1)
412 { 412 {
413 friendlist[i].friend_request_id = send_friendrequest(friendlist[i].client_id, 413 friendlist[i].friend_request_id = send_friendrequest(friendlist[i].client_id,
@@ -415,9 +415,9 @@ static void doFriends()
415 friendlist[i].status = 2; 415 friendlist[i].status = 2;
416 } 416 }
417 } 417 }
418 if(friendlist[i].status == 2 || friendlist[i].status == 3) //friend is not online 418 if(friendlist[i].status == 2 || friendlist[i].status == 3) /* friend is not online */
419 { 419 {
420 check_friendrequest(friendlist[i].friend_request_id);//for now this is used to kill the friend request 420 check_friendrequest(friendlist[i].friend_request_id); /* for now this is used to kill the friend request */
421 IP_Port friendip = DHT_getfriendip(friendlist[i].client_id); 421 IP_Port friendip = DHT_getfriendip(friendlist[i].client_id);
422 switch(is_cryptoconnected(friendlist[i].crypt_connection_id)) 422 switch(is_cryptoconnected(friendlist[i].crypt_connection_id))
423 { 423 {
@@ -425,7 +425,7 @@ static void doFriends()
425 if (friendip.ip.i > 1) 425 if (friendip.ip.i > 1)
426 friendlist[i].crypt_connection_id = crypto_connect(friendlist[i].client_id, friendip); 426 friendlist[i].crypt_connection_id = crypto_connect(friendlist[i].client_id, friendip);
427 break; 427 break;
428 case 3: // Connection is established 428 case 3: /* Connection is established */
429 friendlist[i].status = 4; 429 friendlist[i].status = 4;
430 break; 430 break;
431 case 4: 431 case 4:
@@ -436,7 +436,7 @@ static void doFriends()
436 break; 436 break;
437 } 437 }
438 } 438 }
439 while(friendlist[i].status == 4) //friend is online 439 while(friendlist[i].status == 4) /* friend is online */
440 { 440 {
441 if(friendlist[i].name_sent == 0) 441 if(friendlist[i].name_sent == 0)
442 { 442 {
@@ -458,9 +458,9 @@ static void doFriends()
458 switch(temp[0]) { 458 switch(temp[0]) {
459 case PACKET_ID_NICKNAME: { 459 case PACKET_ID_NICKNAME: {
460 if (len != MAX_NAME_LENGTH + 1) break; 460 if (len != MAX_NAME_LENGTH + 1) break;
461 friend_namechange(i, temp + 1, MAX_NAME_LENGTH); // todo: use the actual length 461 friend_namechange(i, temp + 1, MAX_NAME_LENGTH); /* TODO: use the actual length */
462 memcpy(friendlist[i].name, temp + 1, MAX_NAME_LENGTH); 462 memcpy(friendlist[i].name, temp + 1, MAX_NAME_LENGTH);
463 friendlist[i].name[MAX_NAME_LENGTH - 1] = 0;//make sure the NULL terminator is present. 463 friendlist[i].name[MAX_NAME_LENGTH - 1] = 0; /* make sure the NULL terminator is present. */
464 break; 464 break;
465 } 465 }
466 case PACKET_ID_USERSTATUS: { 466 case PACKET_ID_USERSTATUS: {
@@ -479,7 +479,7 @@ static void doFriends()
479 } 479 }
480 else 480 else
481 { 481 {
482 if(is_cryptoconnected(friendlist[i].crypt_connection_id) == 4)//if the connection timed out, kill it 482 if(is_cryptoconnected(friendlist[i].crypt_connection_id) == 4) /* if the connection timed out, kill it */
483 { 483 {
484 crypto_kill(friendlist[i].crypt_connection_id); 484 crypto_kill(friendlist[i].crypt_connection_id);
485 friendlist[i].crypt_connection_id = -1; 485 friendlist[i].crypt_connection_id = -1;
@@ -525,7 +525,7 @@ static void doInbound()
525 } 525 }
526} 526}
527 527
528//the main loop that needs to be run at least 200 times per second. 528/* the main loop that needs to be run at least 200 times per second. */
529void doMessenger() 529void doMessenger()
530{ 530{
531 IP_Port ip_port; 531 IP_Port ip_port;
@@ -534,18 +534,18 @@ void doMessenger()
534 while(receivepacket(&ip_port, data, &length) != -1) 534 while(receivepacket(&ip_port, data, &length) != -1)
535 { 535 {
536#ifdef DEBUG 536#ifdef DEBUG
537 //if(rand() % 3 != 1)//simulate packet loss 537 /* if(rand() % 3 != 1) //simulate packet loss */
538 //{ 538 /* { */
539 if(DHT_handlepacket(data, length, ip_port) && LosslessUDP_handlepacket(data, length, ip_port)) 539 if(DHT_handlepacket(data, length, ip_port) && LosslessUDP_handlepacket(data, length, ip_port))
540 { 540 {
541 //if packet is discarded 541 /* if packet is discarded */
542 printf("Received unhandled packet with length: %u\n", length); 542 printf("Received unhandled packet with length: %u\n", length);
543 } 543 }
544 else 544 else
545 { 545 {
546 printf("Received handled packet with length: %u\n", length); 546 printf("Received handled packet with length: %u\n", length);
547 } 547 }
548 //} 548 /* } */
549 printf("Status: %u %u %u\n",friendlist[0].status ,is_cryptoconnected(friendlist[0].crypt_connection_id), friendlist[0].crypt_connection_id); 549 printf("Status: %u %u %u\n",friendlist[0].status ,is_cryptoconnected(friendlist[0].crypt_connection_id), friendlist[0].crypt_connection_id);
550#else 550#else
551 DHT_handlepacket(data, length, ip_port); 551 DHT_handlepacket(data, length, ip_port);
@@ -561,14 +561,14 @@ void doMessenger()
561 doFriends(); 561 doFriends();
562} 562}
563 563
564//returns the size of the messenger data (for saving) 564/* returns the size of the messenger data (for saving) */
565uint32_t Messenger_size() 565uint32_t Messenger_size()
566{ 566{
567 return crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES 567 return crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES
568 + sizeof(uint32_t) + DHT_size() + sizeof(uint32_t) + sizeof(Friend) * numfriends; 568 + sizeof(uint32_t) + DHT_size() + sizeof(uint32_t) + sizeof(Friend) * numfriends;
569} 569}
570 570
571//save the messenger in data of size Messenger_size() 571/* save the messenger in data of size Messenger_size() */
572void Messenger_save(uint8_t * data) 572void Messenger_save(uint8_t * data)
573{ 573{
574 save_keys(data); 574 save_keys(data);
@@ -584,7 +584,7 @@ void Messenger_save(uint8_t * data)
584 memcpy(data, friendlist, sizeof(Friend) * numfriends); 584 memcpy(data, friendlist, sizeof(Friend) * numfriends);
585} 585}
586 586
587//load the messenger from data of size length. 587/* load the messenger from data of size length. */
588int Messenger_load(uint8_t * data, uint32_t length) 588int Messenger_load(uint8_t * data, uint32_t length)
589{ 589{
590 if(length == ~0) 590 if(length == ~0)
@@ -631,7 +631,7 @@ int Messenger_load(uint8_t * data, uint32_t length)
631 { 631 {
632 int fnum = m_addfriend_norequest(temp[i].client_id); 632 int fnum = m_addfriend_norequest(temp[i].client_id);
633 setfriendname(fnum, temp[i].name); 633 setfriendname(fnum, temp[i].name);
634 //set_friend_userstatus(fnum, temp[i].userstatus, temp[i].userstatus_length); 634 /* set_friend_userstatus(fnum, temp[i].userstatus, temp[i].userstatus_length); */
635 } 635 }
636 } 636 }
637 free(temp); 637 free(temp);