diff options
-rw-r--r-- | core/Messenger.c | 37 | ||||
-rw-r--r-- | core/Messenger.h | 9 | ||||
-rw-r--r-- | 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 | |||
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 | ||
35 | uint16_t info_size; //length of the info | ||
34 | 36 | ||
35 | }Friend; | 37 | }Friend; |
36 | 38 | ||
37 | 39 | ||
38 | uint8_t info[MAX_DATA_SIZE]; //the data that is sent during the friend requests we do | ||
39 | 40 | ||
40 | uint16_t info_size; //length of the info | 41 | |
41 | 42 | ||
42 | #define MAX_NUM_FRIENDS 256 | 43 | #define MAX_NUM_FRIENDS 256 |
43 | 44 | ||
@@ -68,11 +69,19 @@ int getfriend_id(uint8_t * client_id) | |||
68 | 69 | ||
69 | 70 | ||
70 | //add a friend | 71 | //add a friend |
71 | //client_id is the client i of the friend | 72 | //set the data that will be sent along with friend request |
73 | //client_id is the client id of the friend | ||
74 | //data is the data and length is the length | ||
72 | //returns the friend number if success | 75 | //returns the friend number if success |
73 | //return -1 if failure. | 76 | //return -1 if failure. |
74 | int m_addfriend(uint8_t * client_id) | 77 | int m_addfriend(uint8_t * client_id, uint8_t * data, uint16_t length) |
75 | { | 78 | { |
79 | if(length == 0 || length > MAX_DATA_SIZE - 1 - crypto_box_PUBLICKEYBYTES - crypto_box_NONCEBYTES | ||
80 | - crypto_box_BOXZEROBYTES + crypto_box_ZEROBYTES) | ||
81 | { | ||
82 | return -1; | ||
83 | } | ||
84 | |||
76 | if(getfriend_id(client_id) != -1) | 85 | if(getfriend_id(client_id) != -1) |
77 | { | 86 | { |
78 | return -1; | 87 | return -1; |
@@ -86,6 +95,10 @@ int m_addfriend(uint8_t * client_id) | |||
86 | friendlist[i].status = 1; | 95 | friendlist[i].status = 1; |
87 | friendlist[i].friend_request_id = -1; | 96 | friendlist[i].friend_request_id = -1; |
88 | memcpy(friendlist[i].client_id, client_id, CLIENT_ID_SIZE); | 97 | memcpy(friendlist[i].client_id, client_id, CLIENT_ID_SIZE); |
98 | |||
99 | memcpy(friendlist[i].info, data, length); | ||
100 | friendlist[i].info_size = length; | ||
101 | |||
89 | numfriends++; | 102 | numfriends++; |
90 | return i; | 103 | return i; |
91 | } | 104 | } |
@@ -176,19 +189,6 @@ int m_sendmessage(int friendnumber, uint8_t * message, uint32_t length) | |||
176 | 189 | ||
177 | } | 190 | } |
178 | 191 | ||
179 | //set the data that will be sent along with friend requests | ||
180 | //return -1 if failure | ||
181 | //return 0 if success | ||
182 | int m_setinfo(uint8_t * data, uint16_t length) | ||
183 | { | ||
184 | if(length == 0 || length > MAX_DATA_SIZE - 1 - crypto_box_PUBLICKEYBYTES - crypto_box_NONCEBYTES) | ||
185 | { | ||
186 | return -1; | ||
187 | } | ||
188 | memcpy(info, data, length); | ||
189 | info_size = length; | ||
190 | return 0; | ||
191 | } | ||
192 | 192 | ||
193 | void (*friend_request)(uint8_t *, uint8_t *, uint16_t); | 193 | void (*friend_request)(uint8_t *, uint8_t *, uint16_t); |
194 | 194 | ||
@@ -235,7 +235,8 @@ void doFriends() | |||
235 | //printf("\n%u %u %u\n", friendip.ip.i, request, friendlist[i].friend_request_id); | 235 | //printf("\n%u %u %u\n", friendip.ip.i, request, friendlist[i].friend_request_id); |
236 | if(friendip.ip.i > 1 && request == -1) | 236 | if(friendip.ip.i > 1 && request == -1) |
237 | { | 237 | { |
238 | friendlist[i].friend_request_id = send_friendrequest(friendlist[i].client_id, friendip, info, info_size); | 238 | friendlist[i].friend_request_id = send_friendrequest(friendlist[i].client_id, |
239 | friendip, friendlist[i].info, friendlist[i].info_size); | ||
239 | friendlist[i].status = 2; | 240 | friendlist[i].status = 2; |
240 | } | 241 | } |
241 | } | 242 | } |
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 @@ | |||
31 | 31 | ||
32 | 32 | ||
33 | //add a friend | 33 | //add a friend |
34 | //set the data that will be sent along with friend request | ||
35 | //client_id is the client id of the friend | ||
36 | //data is the data and length is the length | ||
34 | //returns the friend number if success | 37 | //returns the friend number if success |
35 | //return -1 if failure. | 38 | //return -1 if failure. |
36 | int m_addfriend(uint8_t * client_id); | 39 | int m_addfriend(uint8_t * client_id, uint8_t * data, uint16_t length); |
37 | 40 | ||
38 | 41 | ||
39 | //add a friend without sending a friendrequest. | 42 | //add a friend without sending a friendrequest. |
@@ -61,10 +64,6 @@ int m_friendstatus(int friendnumber); | |||
61 | //return 0 if it was not. | 64 | //return 0 if it was not. |
62 | int m_sendmessage(int friendnumber, uint8_t * message, uint32_t length); | 65 | int m_sendmessage(int friendnumber, uint8_t * message, uint32_t length); |
63 | 66 | ||
64 | //set the data that will be sent along with friend requests | ||
65 | //return -1 if failure | ||
66 | //return 0 if success | ||
67 | int m_setinfo(uint8_t * data, uint16_t length); | ||
68 | 67 | ||
69 | //set the function that will be executed when a friend request is received. | 68 | //set the function that will be executed when a friend request is received. |
70 | //function format is function(uint8_t * public_key, uint8_t * data, uint16_t length) | 69 | //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) | |||
57 | } | 57 | } |
58 | printf("\nOf length: %u with data: %s \n", length, data); | 58 | printf("\nOf length: %u with data: %s \n", length, data); |
59 | 59 | ||
60 | if(length != sizeof((uint8_t*)"Install Gentoo")) | 60 | if(length != sizeof("Install Gentoo")) |
61 | { | 61 | { |
62 | return; | 62 | return; |
63 | } | 63 | } |
@@ -85,8 +85,6 @@ int main(int argc, char *argv[]) | |||
85 | m_callback_friendrequest(print_request); | 85 | m_callback_friendrequest(print_request); |
86 | m_callback_friendmessage(print_message); | 86 | m_callback_friendmessage(print_message); |
87 | 87 | ||
88 | m_setinfo((uint8_t*)"Install Gentoo", sizeof((uint8_t*)"Install Gentoo"));//The message we send is a message of peace | ||
89 | |||
90 | printf("OUR ID: "); | 88 | printf("OUR ID: "); |
91 | uint32_t i; | 89 | uint32_t i; |
92 | for(i = 0; i < 32; i++) | 90 | for(i = 0; i < 32; i++) |
@@ -99,7 +97,7 @@ int main(int argc, char *argv[]) | |||
99 | char temp_id[128]; | 97 | char temp_id[128]; |
100 | printf("\nEnter the client_id of the friend you wish to add (32 bytes HEX format):\n"); | 98 | printf("\nEnter the client_id of the friend you wish to add (32 bytes HEX format):\n"); |
101 | scanf("%s", temp_id); | 99 | scanf("%s", temp_id); |
102 | int num = m_addfriend(hex_string_to_bin(temp_id)); | 100 | int num = m_addfriend(hex_string_to_bin(temp_id), (uint8_t*)"Install Gentoo", sizeof("Install Gentoo")); |
103 | 101 | ||
104 | perror("Initialization"); | 102 | perror("Initialization"); |
105 | IP_Port bootstrap_ip_port; | 103 | IP_Port bootstrap_ip_port; |