summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorirungentoo <irungentoo@gmail.com>2013-07-18 11:47:27 -0400
committerirungentoo <irungentoo@gmail.com>2013-07-18 11:47:27 -0400
commitad44110fd54d5e0400f6f6749922e1c788d04f7d (patch)
treeadea517a8a16e113e5f381f7ca6ed3028d76cb39
parente02620c7bed8764421b9b4c258e594369615da39 (diff)
Added nicknames and nickname syncing.
-rw-r--r--core/Messenger.c82
-rw-r--r--core/Messenger.h13
-rw-r--r--docs/Messenger_Protocol.txt17
-rw-r--r--testing/Messenger_test.c6
4 files changed, 107 insertions, 11 deletions
diff --git a/core/Messenger.c b/core/Messenger.c
index 2a846ba3..85b2ac5a 100644
--- a/core/Messenger.c
+++ b/core/Messenger.c
@@ -24,6 +24,7 @@
24 24
25#include "Messenger.h" 25#include "Messenger.h"
26 26
27#define MAX_NAME_LENGTH 128
27 28
28typedef struct 29typedef struct
29{ 30{
@@ -32,6 +33,8 @@ typedef struct
32 int friend_request_id; //id of the friend request corresponding to the current friend request to the current friend. 33 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. 34 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 uint8_t info[MAX_DATA_SIZE]; //the data that is sent during the friend requests we do
36 uint8_t name[MAX_NAME_LENGTH];
37 uint8_t name_sent;//0 if we didn't send our name to this friend 1 if we have.
35 uint16_t info_size; //length of the info 38 uint16_t info_size; //length of the info
36}Friend; 39}Friend;
37 40
@@ -39,6 +42,7 @@ typedef struct
39 42
40uint8_t self_public_key[crypto_box_PUBLICKEYBYTES]; 43uint8_t self_public_key[crypto_box_PUBLICKEYBYTES];
41 44
45static uint8_t self_name[MAX_NAME_LENGTH];
42 46
43#define MAX_NUM_FRIENDS 256 47#define MAX_NUM_FRIENDS 256
44 48
@@ -216,6 +220,63 @@ int m_sendmessage(int friendnumber, uint8_t * message, uint32_t length)
216 return write_cryptpacket(friendlist[friendnumber].crypt_connection_id, temp, length + 1); 220 return write_cryptpacket(friendlist[friendnumber].crypt_connection_id, temp, length + 1);
217} 221}
218 222
223//send a name packet to friendnumber
224static int m_sendname(int friendnumber, uint8_t * name)
225{
226 uint8_t temp[MAX_NAME_LENGTH + 1];
227 memcpy(temp + 1, name, MAX_NAME_LENGTH);
228 temp[0] = 48;
229 return write_cryptpacket(friendlist[friendnumber].crypt_connection_id, temp, MAX_NAME_LENGTH + 1);
230}
231
232//set the name of a friend
233//return 0 if success
234//return -1 if failure
235
236static int setfriendname(int friendnumber, uint8_t * name)
237{
238 if(friendnumber >= numfriends || friendnumber < 0)
239 {
240 return -1;
241 }
242 memcpy(friendlist[friendnumber].name, name, MAX_NAME_LENGTH);
243 return 0;
244}
245
246
247//Set our nickname
248//name must be a string of maximum MAX_NAME_LENGTH length.
249//return 0 if success
250//return -1 if failure
251int setname(uint8_t * name, uint16_t length)
252{
253 if(length > MAX_NAME_LENGTH)
254 {
255 return -1;
256 }
257 memcpy(self_name, name, length);
258 uint32_t i;
259 for(i = 0; i < numfriends; i++)
260 {
261 friendlist[i].name_sent = 0;
262 }
263 return 0;
264}
265
266//get name of friendnumber
267//put it in name
268//name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH bytes.
269//return 0 if success
270//return -1 if failure
271int getname(int friendnumber, uint8_t * name)
272{
273 if(friendnumber >= numfriends || friendnumber < 0)
274 {
275 return -1;
276 }
277 memcpy(name, friendlist[friendnumber].name, MAX_NAME_LENGTH);
278 return 0;
279}
219 280
220static void (*friend_request)(uint8_t *, uint8_t *, uint16_t); 281static void (*friend_request)(uint8_t *, uint8_t *, uint16_t);
221 282
@@ -265,7 +326,7 @@ static void doFriends()
265 friendlist[i].status = 2; 326 friendlist[i].status = 2;
266 } 327 }
267 } 328 }
268 if(friendlist[i].status == 2 || friendlist[i].status == 3) 329 if(friendlist[i].status == 2 || friendlist[i].status == 3) //friend is not online
269 { 330 {
270 check_friendrequest(friendlist[i].friend_request_id);//for now this is used to kill the friend request 331 check_friendrequest(friendlist[i].friend_request_id);//for now this is used to kill the friend request
271 IP_Port friendip = DHT_getfriendip(friendlist[i].client_id); 332 IP_Port friendip = DHT_getfriendip(friendlist[i].client_id);
@@ -286,12 +347,25 @@ static void doFriends()
286 break; 347 break;
287 } 348 }
288 } 349 }
289 while(friendlist[i].status == 4) 350 while(friendlist[i].status == 4) //friend is online
290 { 351 {
352 if(friendlist[i].name_sent == 0)
353 {
354 if(m_sendname(i, self_name))
355 {
356 friendlist[i].name_sent = 1;
357 }
358 }
291 len = read_cryptpacket(friendlist[i].crypt_connection_id, temp); 359 len = read_cryptpacket(friendlist[i].crypt_connection_id, temp);
292 if(len > 0) 360 if(len > 0)
293 { 361 {
294 if(temp[0] == 64) 362 if(temp[0] == 48 && len == MAX_NAME_LENGTH + 1)//Username
363 {
364 memcpy(friendlist[i].name, temp + 1, MAX_NAME_LENGTH);
365 friendlist[i].name[MAX_NAME_LENGTH - 1] = 0;//make sure the NULL terminator is present.
366 }
367 else
368 if(temp[0] == 64)//Chat message
295 { 369 {
296 (*friend_message)(i, temp + 1, len - 1); 370 (*friend_message)(i, temp + 1, len - 1);
297 } 371 }
@@ -446,7 +520,7 @@ int Messenger_load(uint8_t * data, uint32_t length)
446 uint32_t i; 520 uint32_t i;
447 for(i = 0; i < num; i++) 521 for(i = 0; i < num; i++)
448 { 522 {
449 m_addfriend_norequest(temp[i].client_id); 523 setfriendname(m_addfriend_norequest(temp[i].client_id), temp[i].name);
450 } 524 }
451 free(temp); 525 free(temp);
452 return 0; 526 return 0;
diff --git a/core/Messenger.h b/core/Messenger.h
index 0a3ae309..0b8aa7aa 100644
--- a/core/Messenger.h
+++ b/core/Messenger.h
@@ -71,6 +71,19 @@ int m_friendstatus(int friendnumber);
71//return 0 if it was not. 71//return 0 if it was not.
72int m_sendmessage(int friendnumber, uint8_t * message, uint32_t length); 72int m_sendmessage(int friendnumber, uint8_t * message, uint32_t length);
73 73
74//Set our nickname
75//name must be a string of maximum MAX_NAME_LENGTH length.
76//return 0 if success
77//return -1 if failure
78int setname(uint8_t * name, uint16_t length);
79
80
81//get name of friendnumber
82//put it in name
83//name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH (128) bytes.
84//return 0 if success
85//return -1 if failure
86int getname(int friendnumber, uint8_t * name);
74 87
75//set the function that will be executed when a friend request is received. 88//set the function that will be executed when a friend request is received.
76//function format is function(uint8_t * public_key, uint8_t * data, uint16_t length) 89//function format is function(uint8_t * public_key, uint8_t * data, uint16_t length)
diff --git a/docs/Messenger_Protocol.txt b/docs/Messenger_Protocol.txt
index 9362fdd2..377a6016 100644
--- a/docs/Messenger_Protocol.txt
+++ b/docs/Messenger_Protocol.txt
@@ -1,4 +1,6 @@
1Protocol for messages, data, video, etc.. 1Protocol for messages, data, etc..
2
3Streaming audio/video will not use this protocol as they can absorb some data loss.
2 4
3The protocol itself will run on top of the encryption which means it should be 5The protocol itself will run on top of the encryption which means it should be
4impossible for someone to know what type of data is being transmitted.(Well they 6impossible for someone to know what type of data is being transmitted.(Well they
@@ -12,6 +14,8 @@ Basic packet format:
12 14
13data_id represents the type of data. 15data_id represents the type of data.
14 16
17All strings must be UTF-8.
18
15EX: data_id 64 designates a chat message. so the packet would look like: @Hello WorldNULL 19EX: data_id 64 designates a chat message. so the packet would look like: @Hello WorldNULL
16Where @ is the ASCII character for 64, "Hello World" is the message and NULL is the null string terminator. 20Where @ is the ASCII character for 64, "Hello World" is the message and NULL is the null string terminator.
17 21
@@ -19,11 +23,10 @@ Proposed data_ids and what they mean (in decimal)
19 23
20ids 0 to 16 are reserved. 24ids 0 to 16 are reserved.
21 25
2648 Username (Send this packet as soon as you connect to a friend or to each friend everytime you change names.
27 Username is maximum 128 bytes long with the null terminator included)
28
22 29
2364 Chat message 3064 Chat message
2465 Audio Data(Voip) 316? File transmission.
2566 Video? 32
2667 File transmission.
2768
2869
29...
diff --git a/testing/Messenger_test.c b/testing/Messenger_test.c
index 484098b9..f9215b44 100644
--- a/testing/Messenger_test.c
+++ b/testing/Messenger_test.c
@@ -117,6 +117,8 @@ int main(int argc, char *argv[])
117 printf("%hhX",self_public_key[i]); 117 printf("%hhX",self_public_key[i]);
118 } 118 }
119 119
120 setname((uint8_t *)"Anon", 5);
121
120 char temp_id[128]; 122 char temp_id[128];
121 printf("\nEnter the client_id of the friend you wish to add (32 bytes HEX format):\n"); 123 printf("\nEnter the client_id of the friend you wish to add (32 bytes HEX format):\n");
122 if(scanf("%s", temp_id) != 1) 124 if(scanf("%s", temp_id) != 1)
@@ -129,6 +131,10 @@ int main(int argc, char *argv[])
129 131
130 while(1) 132 while(1)
131 { 133 {
134 uint8_t name[128];
135 getname(num, name);
136 printf("%s\n", name);
137
132 m_sendmessage(num, (uint8_t*)"Test", 5); 138 m_sendmessage(num, (uint8_t*)"Test", 5);
133 doMessenger(); 139 doMessenger();
134 c_sleep(30); 140 c_sleep(30);