From 0b18dcda6f6bd181a164755a82decc7ae08c603e Mon Sep 17 00:00:00 2001 From: irungentoo Date: Wed, 10 Jul 2013 17:31:58 -0400 Subject: [PATCH] Per friend request data added. --- core/Messenger.c | 37 +++++++++++++++++++------------------ core/Messenger.h | 9 ++++----- testing/Messenger_test.c | 6 ++---- 3 files changed, 25 insertions(+), 27 deletions(-) diff --git a/core/Messenger.c b/core/Messenger.c index 0660ba94..da845560 100644 --- a/core/Messenger.c +++ b/core/Messenger.c @@ -31,13 +31,14 @@ typedef struct int crypt_connection_id; int friend_request_id; //id of the friend request corresponding to the current friend request to the current friend. uint8_t status;//0 if no friend, 1 if added, 2 if friend request sent, 3 if confirmed friend, 4 if online. + uint8_t info[MAX_DATA_SIZE]; //the data that is sent during the friend requests we do + uint16_t info_size; //length of the info }Friend; -uint8_t info[MAX_DATA_SIZE]; //the data that is sent during the friend requests we do -uint16_t info_size; //length of the info + #define MAX_NUM_FRIENDS 256 @@ -68,11 +69,19 @@ int getfriend_id(uint8_t * client_id) //add a friend -//client_id is the client i of the friend +//set the data that will be sent along with friend request +//client_id is the client id of the friend +//data is the data and length is the length //returns the friend number if success //return -1 if failure. -int m_addfriend(uint8_t * client_id) +int m_addfriend(uint8_t * client_id, uint8_t * data, uint16_t length) { + if(length == 0 || length > MAX_DATA_SIZE - 1 - crypto_box_PUBLICKEYBYTES - crypto_box_NONCEBYTES + - crypto_box_BOXZEROBYTES + crypto_box_ZEROBYTES) + { + return -1; + } + if(getfriend_id(client_id) != -1) { return -1; @@ -86,6 +95,10 @@ int m_addfriend(uint8_t * client_id) friendlist[i].status = 1; friendlist[i].friend_request_id = -1; memcpy(friendlist[i].client_id, client_id, CLIENT_ID_SIZE); + + memcpy(friendlist[i].info, data, length); + friendlist[i].info_size = length; + numfriends++; return i; } @@ -176,19 +189,6 @@ int m_sendmessage(int friendnumber, uint8_t * message, uint32_t length) } -//set the data that will be sent along with friend requests -//return -1 if failure -//return 0 if success -int m_setinfo(uint8_t * data, uint16_t length) -{ - if(length == 0 || length > MAX_DATA_SIZE - 1 - crypto_box_PUBLICKEYBYTES - crypto_box_NONCEBYTES) - { - return -1; - } - memcpy(info, data, length); - info_size = length; - return 0; -} void (*friend_request)(uint8_t *, uint8_t *, uint16_t); @@ -235,7 +235,8 @@ void doFriends() //printf("\n%u %u %u\n", friendip.ip.i, request, friendlist[i].friend_request_id); if(friendip.ip.i > 1 && request == -1) { - friendlist[i].friend_request_id = send_friendrequest(friendlist[i].client_id, friendip, info, info_size); + friendlist[i].friend_request_id = send_friendrequest(friendlist[i].client_id, + friendip, friendlist[i].info, friendlist[i].info_size); friendlist[i].status = 2; } } diff --git a/core/Messenger.h b/core/Messenger.h index 4bdf5859..128da67a 100644 --- a/core/Messenger.h +++ b/core/Messenger.h @@ -31,9 +31,12 @@ //add a friend +//set the data that will be sent along with friend request +//client_id is the client id of the friend +//data is the data and length is the length //returns the friend number if success //return -1 if failure. -int m_addfriend(uint8_t * client_id); +int m_addfriend(uint8_t * client_id, uint8_t * data, uint16_t length); //add a friend without sending a friendrequest. @@ -61,10 +64,6 @@ int m_friendstatus(int friendnumber); //return 0 if it was not. int m_sendmessage(int friendnumber, uint8_t * message, uint32_t length); -//set the data that will be sent along with friend requests -//return -1 if failure -//return 0 if success -int m_setinfo(uint8_t * data, uint16_t length); //set the function that will be executed when a friend request is received. //function format is function(uint8_t * public_key, uint8_t * data, uint16_t length) diff --git a/testing/Messenger_test.c b/testing/Messenger_test.c index 0fd9c557..6b0e56fc 100644 --- a/testing/Messenger_test.c +++ b/testing/Messenger_test.c @@ -57,7 +57,7 @@ void print_request(uint8_t * public_key, uint8_t * data, uint16_t length) } printf("\nOf length: %u with data: %s \n", length, data); - if(length != sizeof((uint8_t*)"Install Gentoo")) + if(length != sizeof("Install Gentoo")) { return; } @@ -85,8 +85,6 @@ int main(int argc, char *argv[]) m_callback_friendrequest(print_request); m_callback_friendmessage(print_message); - m_setinfo((uint8_t*)"Install Gentoo", sizeof((uint8_t*)"Install Gentoo"));//The message we send is a message of peace - printf("OUR ID: "); uint32_t i; for(i = 0; i < 32; i++) @@ -99,7 +97,7 @@ int main(int argc, char *argv[]) char temp_id[128]; printf("\nEnter the client_id of the friend you wish to add (32 bytes HEX format):\n"); scanf("%s", temp_id); - int num = m_addfriend(hex_string_to_bin(temp_id)); + int num = m_addfriend(hex_string_to_bin(temp_id), (uint8_t*)"Install Gentoo", sizeof("Install Gentoo")); perror("Initialization"); IP_Port bootstrap_ip_port;