diff options
Diffstat (limited to 'core/Messenger.c')
-rw-r--r-- | core/Messenger.c | 168 |
1 files changed, 74 insertions, 94 deletions
diff --git a/core/Messenger.c b/core/Messenger.c index 69d33172..fd95869f 100644 --- a/core/Messenger.c +++ b/core/Messenger.c | |||
@@ -24,8 +24,7 @@ | |||
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 | { | ||
29 | uint8_t client_id[CLIENT_ID_SIZE]; | 28 | uint8_t client_id[CLIENT_ID_SIZE]; |
30 | int crypt_connection_id; | 29 | int crypt_connection_id; |
31 | int friend_request_id; /* id of the friend request corresponding to the current friend request to the current friend. */ | 30 | int friend_request_id; /* id of the friend request corresponding to the current friend request to the current friend. */ |
@@ -37,7 +36,7 @@ typedef struct | |||
37 | uint16_t userstatus_length; | 36 | uint16_t userstatus_length; |
38 | uint8_t userstatus_sent; | 37 | uint8_t userstatus_sent; |
39 | uint16_t info_size; /* length of the info */ | 38 | uint16_t info_size; /* length of the info */ |
40 | }Friend; | 39 | } Friend; |
41 | 40 | ||
42 | uint8_t self_public_key[crypto_box_PUBLICKEYBYTES]; | 41 | uint8_t self_public_key[crypto_box_PUBLICKEYBYTES]; |
43 | 42 | ||
@@ -57,14 +56,15 @@ static uint32_t numfriends; | |||
57 | 56 | ||
58 | /* return the friend id associated to that public key. | 57 | /* return the friend id associated to that public key. |
59 | return -1 if no such friend */ | 58 | return -1 if no such friend */ |
60 | int getfriend_id(uint8_t * client_id) | 59 | int getfriend_id(uint8_t *client_id) |
61 | { | 60 | { |
62 | uint32_t i; | 61 | uint32_t i; |
63 | 62 | ||
64 | for(i = 0; i < numfriends; ++i) | 63 | for (i = 0; i < numfriends; ++i) { |
65 | if(friendlist[i].status > 0) | 64 | if (friendlist[i].status > 0) |
66 | if(memcmp(client_id, friendlist[i].client_id, crypto_box_PUBLICKEYBYTES) == 0) | 65 | if (memcmp(client_id, friendlist[i].client_id, crypto_box_PUBLICKEYBYTES) == 0) |
67 | return i; | 66 | return i; |
67 | } | ||
68 | 68 | ||
69 | return -1; | 69 | return -1; |
70 | } | 70 | } |
@@ -73,12 +73,12 @@ int getfriend_id(uint8_t * client_id) | |||
73 | make sure that client_id is of size CLIENT_ID_SIZE. | 73 | make sure that client_id is of size CLIENT_ID_SIZE. |
74 | return 0 if success | 74 | return 0 if success |
75 | return -1 if failure. */ | 75 | return -1 if failure. */ |
76 | int getclient_id(int friend_id, uint8_t * client_id) | 76 | int getclient_id(int friend_id, uint8_t *client_id) |
77 | { | 77 | { |
78 | if(friend_id >= numfriends || friend_id < 0) | 78 | if (friend_id >= numfriends || friend_id < 0) |
79 | return -1; | 79 | return -1; |
80 | 80 | ||
81 | if(friendlist[friend_id].status > 0) { | 81 | if (friendlist[friend_id].status > 0) { |
82 | memcpy(client_id, friendlist[friend_id].client_id, CLIENT_ID_SIZE); | 82 | memcpy(client_id, friendlist[friend_id].client_id, CLIENT_ID_SIZE); |
83 | return 0; | 83 | return 0; |
84 | } | 84 | } |
@@ -92,20 +92,18 @@ int getclient_id(int friend_id, uint8_t * client_id) | |||
92 | data is the data and length is the length | 92 | data is the data and length is the length |
93 | returns the friend number if success | 93 | returns the friend number if success |
94 | return -1 if failure. */ | 94 | return -1 if failure. */ |
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; |
102 | if(getfriend_id(client_id) != -1) | 102 | if (getfriend_id(client_id) != -1) |
103 | return -1; | 103 | return -1; |
104 | uint32_t i; | 104 | uint32_t i; |
105 | for(i = 0; i <= numfriends; ++i) | 105 | for (i = 0; i <= numfriends; ++i) { |
106 | { | 106 | if(friendlist[i].status == 0) { |
107 | if(friendlist[i].status == 0) | ||
108 | { | ||
109 | DHT_addfriend(client_id); | 107 | DHT_addfriend(client_id); |
110 | friendlist[i].status = 1; | 108 | friendlist[i].status = 1; |
111 | friendlist[i].crypt_connection_id = -1; | 109 | friendlist[i].crypt_connection_id = -1; |
@@ -125,13 +123,11 @@ int m_addfriend(uint8_t * client_id, uint8_t * data, uint16_t length) | |||
125 | 123 | ||
126 | int m_addfriend_norequest(uint8_t * client_id) | 124 | int m_addfriend_norequest(uint8_t * client_id) |
127 | { | 125 | { |
128 | if(getfriend_id(client_id) != -1) | 126 | if (getfriend_id(client_id) != -1) |
129 | return -1; | 127 | return -1; |
130 | uint32_t i; | 128 | uint32_t i; |
131 | for(i = 0; i <= numfriends; ++i) | 129 | for (i = 0; i <= numfriends; ++i) { |
132 | { | 130 | if(friendlist[i].status == 0) { |
133 | if(friendlist[i].status == 0) | ||
134 | { | ||
135 | DHT_addfriend(client_id); | 131 | DHT_addfriend(client_id); |
136 | friendlist[i].status = 2; | 132 | friendlist[i].status = 2; |
137 | friendlist[i].crypt_connection_id = -1; | 133 | friendlist[i].crypt_connection_id = -1; |
@@ -151,7 +147,7 @@ int m_addfriend_norequest(uint8_t * client_id) | |||
151 | return -1 if failure */ | 147 | return -1 if failure */ |
152 | int m_delfriend(int friendnumber) | 148 | int m_delfriend(int friendnumber) |
153 | { | 149 | { |
154 | if(friendnumber >= numfriends || friendnumber < 0) | 150 | if (friendnumber >= numfriends || friendnumber < 0) |
155 | return -1; | 151 | return -1; |
156 | 152 | ||
157 | DHT_delfriend(friendlist[friendnumber].client_id); | 153 | DHT_delfriend(friendlist[friendnumber].client_id); |
@@ -159,9 +155,10 @@ int m_delfriend(int friendnumber) | |||
159 | free(friendlist[friendnumber].userstatus); | 155 | free(friendlist[friendnumber].userstatus); |
160 | memset(&friendlist[friendnumber], 0, sizeof(Friend)); | 156 | memset(&friendlist[friendnumber], 0, sizeof(Friend)); |
161 | uint32_t i; | 157 | uint32_t i; |
162 | for(i = numfriends; i != 0; --i) | 158 | for (i = numfriends; i != 0; --i) { |
163 | if(friendlist[i].status != 0) | 159 | if (friendlist[i].status != 0) |
164 | break; | 160 | break; |
161 | } | ||
165 | numfriends = i; | 162 | numfriends = i; |
166 | return 0; | 163 | return 0; |
167 | } | 164 | } |
@@ -173,7 +170,7 @@ int m_delfriend(int friendnumber) | |||
173 | return 0 if there is no friend with that number */ | 170 | return 0 if there is no friend with that number */ |
174 | int m_friendstatus(int friendnumber) | 171 | int m_friendstatus(int friendnumber) |
175 | { | 172 | { |
176 | if(friendnumber < 0 || friendnumber >= MAX_NUM_FRIENDS) | 173 | if (friendnumber < 0 || friendnumber >= MAX_NUM_FRIENDS) |
177 | return 0; | 174 | return 0; |
178 | return friendlist[friendnumber].status; | 175 | return friendlist[friendnumber].status; |
179 | } | 176 | } |
@@ -181,11 +178,11 @@ int m_friendstatus(int friendnumber) | |||
181 | /* send a text chat message to an online friend | 178 | /* send a text chat message to an online friend |
182 | return 1 if packet was successfully put into the send queue | 179 | return 1 if packet was successfully put into the send queue |
183 | return 0 if it was not */ | 180 | return 0 if it was not */ |
184 | int m_sendmessage(int friendnumber, uint8_t * message, uint32_t length) | 181 | int m_sendmessage(int friendnumber, uint8_t *message, uint32_t length) |
185 | { | 182 | { |
186 | if(friendnumber < 0 || friendnumber >= MAX_NUM_FRIENDS) | 183 | if (friendnumber < 0 || friendnumber >= MAX_NUM_FRIENDS) |
187 | return 0; | 184 | return 0; |
188 | if(length >= MAX_DATA_SIZE || friendlist[friendnumber].status != 4) | 185 | if (length >= MAX_DATA_SIZE || friendlist[friendnumber].status != 4) |
189 | /* 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. */ |
190 | return 0; | 187 | return 0; |
191 | uint8_t temp[MAX_DATA_SIZE]; | 188 | uint8_t temp[MAX_DATA_SIZE]; |
@@ -208,7 +205,7 @@ static int m_sendname(int friendnumber, uint8_t * name) | |||
208 | return -1 if failure */ | 205 | return -1 if failure */ |
209 | static int setfriendname(int friendnumber, uint8_t * name) | 206 | static int setfriendname(int friendnumber, uint8_t * name) |
210 | { | 207 | { |
211 | if(friendnumber >= numfriends || friendnumber < 0) | 208 | if (friendnumber >= numfriends || friendnumber < 0) |
212 | return -1; | 209 | return -1; |
213 | memcpy(friendlist[friendnumber].name, name, MAX_NAME_LENGTH); | 210 | memcpy(friendlist[friendnumber].name, name, MAX_NAME_LENGTH); |
214 | return 0; | 211 | return 0; |
@@ -220,11 +217,11 @@ static int setfriendname(int friendnumber, uint8_t * name) | |||
220 | return -1 if failure */ | 217 | return -1 if failure */ |
221 | int setname(uint8_t * name, uint16_t length) | 218 | int setname(uint8_t * name, uint16_t length) |
222 | { | 219 | { |
223 | if(length > MAX_NAME_LENGTH) | 220 | if (length > MAX_NAME_LENGTH) |
224 | return -1; | 221 | return -1; |
225 | memcpy(self_name, name, length); | 222 | memcpy(self_name, name, length); |
226 | uint32_t i; | 223 | uint32_t i; |
227 | for(i = 0; i < numfriends; ++i) | 224 | for (i = 0; i < numfriends; ++i) |
228 | friendlist[i].name_sent = 0; | 225 | friendlist[i].name_sent = 0; |
229 | return 0; | 226 | return 0; |
230 | } | 227 | } |
@@ -236,7 +233,7 @@ int setname(uint8_t * name, uint16_t length) | |||
236 | return -1 if failure */ | 233 | return -1 if failure */ |
237 | int getname(int friendnumber, uint8_t * name) | 234 | int getname(int friendnumber, uint8_t * name) |
238 | { | 235 | { |
239 | if(friendnumber >= numfriends || friendnumber < 0) | 236 | if (friendnumber >= numfriends || friendnumber < 0) |
240 | return -1; | 237 | return -1; |
241 | memcpy(name, friendlist[friendnumber].name, MAX_NAME_LENGTH); | 238 | memcpy(name, friendlist[friendnumber].name, MAX_NAME_LENGTH); |
242 | return 0; | 239 | return 0; |
@@ -244,7 +241,7 @@ int getname(int friendnumber, uint8_t * name) | |||
244 | 241 | ||
245 | int m_set_userstatus(uint8_t *status, uint16_t length) | 242 | int m_set_userstatus(uint8_t *status, uint16_t length) |
246 | { | 243 | { |
247 | if(length > MAX_USERSTATUS_LENGTH) | 244 | if (length > MAX_USERSTATUS_LENGTH) |
248 | return -1; | 245 | return -1; |
249 | uint8_t *newstatus = calloc(length, 1); | 246 | uint8_t *newstatus = calloc(length, 1); |
250 | memcpy(newstatus, status, length); | 247 | memcpy(newstatus, status, length); |
@@ -253,7 +250,7 @@ int m_set_userstatus(uint8_t *status, uint16_t length) | |||
253 | self_userstatus_len = length; | 250 | self_userstatus_len = length; |
254 | 251 | ||
255 | uint32_t i; | 252 | uint32_t i; |
256 | for(i = 0; i < numfriends; ++i) | 253 | for (i = 0; i < numfriends; ++i) |
257 | friendlist[i].userstatus_sent = 0; | 254 | friendlist[i].userstatus_sent = 0; |
258 | return 0; | 255 | return 0; |
259 | } | 256 | } |
@@ -262,7 +259,7 @@ int m_set_userstatus(uint8_t *status, uint16_t length) | |||
262 | guaranteed to be at most MAX_USERSTATUS_LENGTH */ | 259 | guaranteed to be at most MAX_USERSTATUS_LENGTH */ |
263 | int m_get_userstatus_size(int friendnumber) | 260 | int m_get_userstatus_size(int friendnumber) |
264 | { | 261 | { |
265 | if(friendnumber >= numfriends || friendnumber < 0) | 262 | if (friendnumber >= numfriends || friendnumber < 0) |
266 | return -1; | 263 | return -1; |
267 | return friendlist[friendnumber].userstatus_length; | 264 | return friendlist[friendnumber].userstatus_length; |
268 | } | 265 | } |
@@ -271,7 +268,7 @@ int m_get_userstatus_size(int friendnumber) | |||
271 | bytes, use m_get_userstatus_size to find out how much you need to allocate */ | 268 | bytes, use m_get_userstatus_size to find out how much you need to allocate */ |
272 | int m_copy_userstatus(int friendnumber, uint8_t * buf, uint32_t maxlen) | 269 | int m_copy_userstatus(int friendnumber, uint8_t * buf, uint32_t maxlen) |
273 | { | 270 | { |
274 | if(friendnumber >= numfriends || friendnumber < 0) | 271 | if (friendnumber >= numfriends || friendnumber < 0) |
275 | return -1; | 272 | return -1; |
276 | memset(buf, 0, maxlen); | 273 | memset(buf, 0, maxlen); |
277 | memcpy(buf, friendlist[friendnumber].userstatus, MIN(maxlen, MAX_USERSTATUS_LENGTH) - 1); | 274 | memcpy(buf, friendlist[friendnumber].userstatus, MIN(maxlen, MAX_USERSTATUS_LENGTH) - 1); |
@@ -290,7 +287,7 @@ static int send_userstatus(int friendnumber, uint8_t * status, uint16_t length) | |||
290 | 287 | ||
291 | static int set_friend_userstatus(int friendnumber, uint8_t * status, uint16_t length) | 288 | static int set_friend_userstatus(int friendnumber, uint8_t * status, uint16_t length) |
292 | { | 289 | { |
293 | if(friendnumber >= numfriends || friendnumber < 0) | 290 | if (friendnumber >= numfriends || friendnumber < 0) |
294 | return -1; | 291 | return -1; |
295 | uint8_t *newstatus = calloc(length, 1); | 292 | uint8_t *newstatus = calloc(length, 1); |
296 | memcpy(newstatus, status, length); | 293 | memcpy(newstatus, status, length); |
@@ -350,35 +347,29 @@ int initMessenger() | |||
350 | return 0; | 347 | return 0; |
351 | } | 348 | } |
352 | 349 | ||
350 | //TODO: make this function not suck. | ||
353 | static void doFriends() | 351 | static void doFriends() |
354 | {/* TODO: add incoming connections and some other stuff. */ | 352 | {/* TODO: add incoming connections and some other stuff. */ |
355 | uint32_t i; | 353 | uint32_t i; |
356 | int len; | 354 | int len; |
357 | uint8_t temp[MAX_DATA_SIZE]; | 355 | uint8_t temp[MAX_DATA_SIZE]; |
358 | for(i = 0; i < numfriends; ++i) | 356 | for (i = 0; i < numfriends; ++i) { |
359 | { | 357 | if (friendlist[i].status == 1) { |
360 | if(friendlist[i].status == 1) | ||
361 | { | ||
362 | int fr = send_friendrequest(friendlist[i].client_id, friendlist[i].info, friendlist[i].info_size); | 358 | int fr = send_friendrequest(friendlist[i].client_id, friendlist[i].info, friendlist[i].info_size); |
363 | if(fr == 0) /* TODO: This needs to be fixed so that it sends the friend requests a couple of times in case | 359 | if (fr == 0) /* TODO: This needs to be fixed so that it sends the friend requests a couple of times in case of packet loss */ |
364 | of packet loss */ | ||
365 | friendlist[i].status = 2; | 360 | friendlist[i].status = 2; |
366 | else | 361 | else if (fr > 0) |
367 | if(fr > 0) | ||
368 | friendlist[i].status = 2; | 362 | friendlist[i].status = 2; |
369 | } | 363 | } |
370 | if(friendlist[i].status == 2 || friendlist[i].status == 3) /* friend is not online */ | 364 | if (friendlist[i].status == 2 || friendlist[i].status == 3) { /* friend is not online */ |
371 | { | 365 | if (friendlist[i].status == 2) { |
372 | if(friendlist[i].status == 2) | 366 | if (friendlist[i].friend_request_id + 10 < unix_time()) { /*I know this is hackish but it should work.*/ |
373 | { | ||
374 | if(friendlist[i].friend_request_id + 10 < unix_time()) /*I know this is hackish but it should work.*/ | ||
375 | { | ||
376 | send_friendrequest(friendlist[i].client_id, friendlist[i].info, friendlist[i].info_size); | 367 | send_friendrequest(friendlist[i].client_id, friendlist[i].info, friendlist[i].info_size); |
377 | friendlist[i].friend_request_id = unix_time(); | 368 | friendlist[i].friend_request_id = unix_time(); |
378 | } | 369 | } |
379 | } | 370 | } |
380 | IP_Port friendip = DHT_getfriendip(friendlist[i].client_id); | 371 | IP_Port friendip = DHT_getfriendip(friendlist[i].client_id); |
381 | switch(is_cryptoconnected(friendlist[i].crypt_connection_id)) { | 372 | switch (is_cryptoconnected(friendlist[i].crypt_connection_id)) { |
382 | case 0: | 373 | case 0: |
383 | if (friendip.ip.i > 1) | 374 | if (friendip.ip.i > 1) |
384 | friendlist[i].crypt_connection_id = crypto_connect(friendlist[i].client_id, friendip); | 375 | friendlist[i].crypt_connection_id = crypto_connect(friendlist[i].client_id, friendip); |
@@ -394,24 +385,23 @@ static void doFriends() | |||
394 | break; | 385 | break; |
395 | } | 386 | } |
396 | } | 387 | } |
397 | while(friendlist[i].status == 4) /* friend is online */ | 388 | while (friendlist[i].status == 4) { /* friend is online */ |
398 | { | 389 | if (friendlist[i].name_sent == 0) { |
399 | if(friendlist[i].name_sent == 0) | 390 | if (m_sendname(i, self_name)) |
400 | if(m_sendname(i, self_name)) | ||
401 | friendlist[i].name_sent = 1; | 391 | friendlist[i].name_sent = 1; |
402 | if(friendlist[i].userstatus_sent == 0) | 392 | } |
403 | if(send_userstatus(i, self_userstatus, self_userstatus_len)) | 393 | if (friendlist[i].userstatus_sent == 0) { |
394 | if (send_userstatus(i, self_userstatus, self_userstatus_len)) | ||
404 | friendlist[i].userstatus_sent = 1; | 395 | friendlist[i].userstatus_sent = 1; |
396 | } | ||
405 | len = read_cryptpacket(friendlist[i].crypt_connection_id, temp); | 397 | len = read_cryptpacket(friendlist[i].crypt_connection_id, temp); |
406 | if(len > 0) | 398 | if (len > 0) { |
407 | { | 399 | switch (temp[0]) { |
408 | switch(temp[0]) { | ||
409 | case PACKET_ID_NICKNAME: { | 400 | case PACKET_ID_NICKNAME: { |
410 | if (len != MAX_NAME_LENGTH + 1) break; | 401 | if (len != MAX_NAME_LENGTH + 1) |
402 | break; | ||
411 | if(friend_namechange_isset) | 403 | if(friend_namechange_isset) |
412 | { | ||
413 | friend_namechange(i, temp + 1, MAX_NAME_LENGTH); /* TODO: use the actual length */ | 404 | friend_namechange(i, temp + 1, MAX_NAME_LENGTH); /* TODO: use the actual length */ |
414 | } | ||
415 | memcpy(friendlist[i].name, temp + 1, MAX_NAME_LENGTH); | 405 | memcpy(friendlist[i].name, temp + 1, MAX_NAME_LENGTH); |
416 | friendlist[i].name[MAX_NAME_LENGTH - 1] = 0; /* make sure the NULL terminator is present. */ | 406 | friendlist[i].name[MAX_NAME_LENGTH - 1] = 0; /* make sure the NULL terminator is present. */ |
417 | break; | 407 | break; |
@@ -419,25 +409,21 @@ static void doFriends() | |||
419 | case PACKET_ID_USERSTATUS: { | 409 | case PACKET_ID_USERSTATUS: { |
420 | uint8_t *status = calloc(MIN(len - 1, MAX_USERSTATUS_LENGTH), 1); | 410 | uint8_t *status = calloc(MIN(len - 1, MAX_USERSTATUS_LENGTH), 1); |
421 | memcpy(status, temp + 1, MIN(len - 1, MAX_USERSTATUS_LENGTH)); | 411 | memcpy(status, temp + 1, MIN(len - 1, MAX_USERSTATUS_LENGTH)); |
422 | if(friend_statuschange_isset) | 412 | if (friend_statuschange_isset) |
423 | { | 413 | friend_statuschange(i, status, MIN(len - 1, MAX_USERSTATUS_LENGTH)); |
424 | friend_statuschange(i, status, MIN(len - 1, MAX_USERSTATUS_LENGTH)); | ||
425 | } | ||
426 | set_friend_userstatus(i, status, MIN(len - 1, MAX_USERSTATUS_LENGTH)); | 414 | set_friend_userstatus(i, status, MIN(len - 1, MAX_USERSTATUS_LENGTH)); |
427 | free(status); | 415 | free(status); |
428 | break; | 416 | break; |
429 | } | 417 | } |
430 | case PACKET_ID_MESSAGE: { | 418 | case PACKET_ID_MESSAGE: { |
431 | if(friend_message_isset) | 419 | if (friend_message_isset) |
432 | (*friend_message)(i, temp + 1, len - 1); | 420 | (*friend_message)(i, temp + 1, len - 1); |
433 | break; | 421 | break; |
434 | } | 422 | } |
435 | } | 423 | } |
436 | } | 424 | } |
437 | else | 425 | else { |
438 | { | 426 | if (is_cryptoconnected(friendlist[i].crypt_connection_id) == 4) { /* if the connection timed out, kill it */ |
439 | if(is_cryptoconnected(friendlist[i].crypt_connection_id) == 4) /* if the connection timed out, kill it */ | ||
440 | { | ||
441 | crypto_kill(friendlist[i].crypt_connection_id); | 427 | crypto_kill(friendlist[i].crypt_connection_id); |
442 | friendlist[i].crypt_connection_id = -1; | 428 | friendlist[i].crypt_connection_id = -1; |
443 | friendlist[i].status = 3; | 429 | friendlist[i].status = 3; |
@@ -454,11 +440,9 @@ static void doInbound() | |||
454 | uint8_t public_key[crypto_box_PUBLICKEYBYTES]; | 440 | uint8_t public_key[crypto_box_PUBLICKEYBYTES]; |
455 | uint8_t session_key[crypto_box_PUBLICKEYBYTES]; | 441 | uint8_t session_key[crypto_box_PUBLICKEYBYTES]; |
456 | int inconnection = crypto_inbound(public_key, secret_nonce, session_key); | 442 | int inconnection = crypto_inbound(public_key, secret_nonce, session_key); |
457 | if(inconnection != -1) | 443 | if (inconnection != -1) { |
458 | { | ||
459 | int friend_id = getfriend_id(public_key); | 444 | int friend_id = getfriend_id(public_key); |
460 | if(friend_id != -1) | 445 | if (friend_id != -1) { |
461 | { | ||
462 | crypto_kill(friendlist[friend_id].crypt_connection_id); | 446 | crypto_kill(friendlist[friend_id].crypt_connection_id); |
463 | friendlist[friend_id].crypt_connection_id = | 447 | friendlist[friend_id].crypt_connection_id = |
464 | accept_crypto_inbound(inconnection, public_key, secret_nonce, session_key); | 448 | accept_crypto_inbound(inconnection, public_key, secret_nonce, session_key); |
@@ -476,8 +460,7 @@ static uint32_t last_LANdiscovery; | |||
476 | /*Send a LAN discovery packet every LAN_DISCOVERY_INTERVAL seconds*/ | 460 | /*Send a LAN discovery packet every LAN_DISCOVERY_INTERVAL seconds*/ |
477 | static void LANdiscovery() | 461 | static void LANdiscovery() |
478 | { | 462 | { |
479 | if(last_LANdiscovery + LAN_DISCOVERY_INTERVAL < unix_time()) | 463 | if (last_LANdiscovery + LAN_DISCOVERY_INTERVAL < unix_time()) { |
480 | { | ||
481 | send_LANdiscovery(htons(PORT)); | 464 | send_LANdiscovery(htons(PORT)); |
482 | last_LANdiscovery = unix_time(); | 465 | last_LANdiscovery = unix_time(); |
483 | } | 466 | } |
@@ -490,12 +473,11 @@ void doMessenger() | |||
490 | IP_Port ip_port; | 473 | IP_Port ip_port; |
491 | uint8_t data[MAX_UDP_PACKET_SIZE]; | 474 | uint8_t data[MAX_UDP_PACKET_SIZE]; |
492 | uint32_t length; | 475 | uint32_t length; |
493 | while(receivepacket(&ip_port, data, &length) != -1) | 476 | while (receivepacket(&ip_port, data, &length) != -1) { |
494 | { | ||
495 | #ifdef DEBUG | 477 | #ifdef DEBUG |
496 | /* if(rand() % 3 != 1) //simulate packet loss */ | 478 | /* if(rand() % 3 != 1) //simulate packet loss */ |
497 | /* { */ | 479 | /* { */ |
498 | 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) && |
499 | 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)) |
500 | /* if packet is discarded */ | 482 | /* if packet is discarded */ |
501 | printf("Received unhandled packet with length: %u\n", length); | 483 | printf("Received unhandled packet with length: %u\n", length); |
@@ -527,7 +509,7 @@ uint32_t Messenger_size() | |||
527 | } | 509 | } |
528 | 510 | ||
529 | /* save the messenger in data of size Messenger_size() */ | 511 | /* save the messenger in data of size Messenger_size() */ |
530 | void Messenger_save(uint8_t * data) | 512 | void Messenger_save(uint8_t *data) |
531 | { | 513 | { |
532 | save_keys(data); | 514 | save_keys(data); |
533 | data += crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES; | 515 | data += crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES; |
@@ -545,9 +527,9 @@ void Messenger_save(uint8_t * data) | |||
545 | /* load the messenger from data of size length. */ | 527 | /* load the messenger from data of size length. */ |
546 | int Messenger_load(uint8_t * data, uint32_t length) | 528 | int Messenger_load(uint8_t * data, uint32_t length) |
547 | { | 529 | { |
548 | if(length == ~0) | 530 | if (length == ~0) |
549 | return -1; | 531 | return -1; |
550 | if(length < crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES + sizeof(uint32_t) * 2) | 532 | if (length < crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES + sizeof(uint32_t) * 2) |
551 | return -1; | 533 | return -1; |
552 | length -= crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES + sizeof(uint32_t) * 2; | 534 | length -= crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES + sizeof(uint32_t) * 2; |
553 | load_keys(data); | 535 | load_keys(data); |
@@ -556,15 +538,15 @@ int Messenger_load(uint8_t * data, uint32_t length) | |||
556 | memcpy(&size, data, sizeof(size)); | 538 | memcpy(&size, data, sizeof(size)); |
557 | data += sizeof(size); | 539 | data += sizeof(size); |
558 | 540 | ||
559 | if(length < size) | 541 | if (length < size) |
560 | return -1; | 542 | return -1; |
561 | length -= size; | 543 | length -= size; |
562 | if(DHT_load(data, size) == -1) | 544 | if (DHT_load(data, size) == -1) |
563 | return -1; | 545 | return -1; |
564 | data += size; | 546 | data += size; |
565 | memcpy(&size, data, sizeof(size)); | 547 | memcpy(&size, data, sizeof(size)); |
566 | data += sizeof(size); | 548 | data += sizeof(size); |
567 | if(length != size || length % sizeof(Friend) != 0) | 549 | if (length != size || length % sizeof(Friend) != 0) |
568 | return -1; | 550 | return -1; |
569 | 551 | ||
570 | Friend * temp = malloc(size); | 552 | Friend * temp = malloc(size); |
@@ -573,10 +555,8 @@ int Messenger_load(uint8_t * data, uint32_t length) | |||
573 | uint16_t num = size / sizeof(Friend); | 555 | uint16_t num = size / sizeof(Friend); |
574 | 556 | ||
575 | uint32_t i; | 557 | uint32_t i; |
576 | for(i = 0; i < num; ++i) | 558 | for (i = 0; i < num; ++i) { |
577 | { | 559 | if(temp[i].status != 0) { |
578 | if(temp[i].status != 0) | ||
579 | { | ||
580 | int fnum = m_addfriend_norequest(temp[i].client_id); | 560 | int fnum = m_addfriend_norequest(temp[i].client_id); |
581 | setfriendname(fnum, temp[i].name); | 561 | setfriendname(fnum, temp[i].name); |
582 | /* set_friend_userstatus(fnum, temp[i].userstatus, temp[i].userstatus_length); */ | 562 | /* set_friend_userstatus(fnum, temp[i].userstatus, temp[i].userstatus_length); */ |