summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorirungentoo <irungentoo@gmail.com>2013-07-09 13:20:48 -0400
committerirungentoo <irungentoo@gmail.com>2013-07-09 13:20:48 -0400
commit4d5063852859d44547497f1d3c0b5af9c676b2ba (patch)
treee467cc6828f98fe40d8bfbbe8d3c9dc537d41122
parentdb37eca44bca126c08dff4353c9d1dab824f8030 (diff)
Basic IM messenger backend pretty much done (You can start the GUI)
And a couple of fixes to the other parts.
-rw-r--r--core/Lossless_UDP.c13
-rw-r--r--core/Messenger.c70
-rw-r--r--core/Messenger.h10
-rw-r--r--core/net_crypto.c49
-rw-r--r--testing/Messenger_test.c14
5 files changed, 136 insertions, 20 deletions
diff --git a/core/Lossless_UDP.c b/core/Lossless_UDP.c
index 080dc821..9d31a53b 100644
--- a/core/Lossless_UDP.c
+++ b/core/Lossless_UDP.c
@@ -37,7 +37,7 @@
37#define BUFFER_PACKET_NUM (16-1) 37#define BUFFER_PACKET_NUM (16-1)
38 38
39//Lossless UDP connection timeout. 39//Lossless UDP connection timeout.
40#define CONNEXION_TIMEOUT 10 40#define CONNEXION_TIMEOUT 5
41 41
42//initial amount of sync/hanshake packets to send per second. 42//initial amount of sync/hanshake packets to send per second.
43#define SYNC_RATE 10 43#define SYNC_RATE 10
@@ -133,7 +133,13 @@ uint32_t handshake_id(IP_Port source)
133 } 133 }
134 return id; 134 return id;
135} 135}
136 136//change the hnshake id associated with that ip_port
137//TODO: make this better
138void change_handshake(IP_Port source)
139{
140 uint8_t rand = random_int() % 4;
141 randtable[rand][((uint8_t *)&source)[rand]] = random_int();
142}
137 143
138 144
139//initialize a new connection to ip_port 145//initialize a new connection to ip_port
@@ -152,6 +158,7 @@ int new_connection(IP_Port ip_port)
152 { 158 {
153 if(connections[i].status == 0) 159 if(connections[i].status == 0)
154 { 160 {
161 memset(&connections[i], 0, sizeof(Connection));
155 connections[i].ip_port = ip_port; 162 connections[i].ip_port = ip_port;
156 connections[i].status = 1; 163 connections[i].status = 1;
157 connections[i].inbound = 0; 164 connections[i].inbound = 0;
@@ -184,6 +191,7 @@ int new_inconnection(IP_Port ip_port)
184 { 191 {
185 if(connections[i].status == 0) 192 if(connections[i].status == 0)
186 { 193 {
194 memset(&connections[i], 0, sizeof(Connection));
187 connections[i].ip_port = ip_port; 195 connections[i].ip_port = ip_port;
188 connections[i].status = 2; 196 connections[i].status = 2;
189 connections[i].inbound = 2; 197 connections[i].inbound = 2;
@@ -223,6 +231,7 @@ int kill_connection(int connection_id)
223 if(connections[connection_id].status > 0) 231 if(connections[connection_id].status > 0)
224 { 232 {
225 connections[connection_id].status = 0; 233 connections[connection_id].status = 0;
234 change_handshake(connections[connection_id].ip_port);
226 return 0; 235 return 0;
227 } 236 }
228 } 237 }
diff --git a/core/Messenger.c b/core/Messenger.c
index 1107b60f..4d484e54 100644
--- a/core/Messenger.c
+++ b/core/Messenger.c
@@ -45,6 +45,17 @@ int m_addfriend(uint8_t * client_id)
45 return numfriends - 1; 45 return numfriends - 1;
46} 46}
47 47
48int m_addfriend_norequest(uint8_t * client_id)
49{
50 DHT_addfriend(client_id);
51 friendlist[numfriends].status = 2;
52 friendlist[numfriends].friend_request_id = -1;
53 memcpy(friendlist[numfriends].client_id, client_id, CLIENT_ID_SIZE);
54 numfriends++;
55
56 return numfriends - 1;
57}
58
48//remove a friend 59//remove a friend
49int m_delfriend(int friendnumber) 60int m_delfriend(int friendnumber)
50{/* 61{/*
@@ -74,7 +85,11 @@ int m_friendstatus(int friendnumber)
74//return 0 if it was not. 85//return 0 if it was not.
75int m_sendmessage(int friendnumber, uint8_t * message, uint32_t length) 86int m_sendmessage(int friendnumber, uint8_t * message, uint32_t length)
76{ 87{
77 if(length >= MAX_DATA_SIZE) 88 if(friendnumber < 0 || friendnumber >= MAX_NUM_FRIENDS)
89 {
90 return 0;
91 }
92 if(length >= MAX_DATA_SIZE || friendlist[friendnumber].status != 4)
78 //this does not mean the maximum message length is MAX_DATA_SIZE - 1, it is actually 17 bytes less. 93 //this does not mean the maximum message length is MAX_DATA_SIZE - 1, it is actually 17 bytes less.
79 { 94 {
80 return 0; 95 return 0;
@@ -103,7 +118,7 @@ int m_setinfo(uint8_t * data, uint16_t length)
103void (*friend_request)(uint8_t *, uint8_t *, uint16_t); 118void (*friend_request)(uint8_t *, uint8_t *, uint16_t);
104 119
105//set the function that will be executed when a friend request is received. 120//set the function that will be executed when a friend request is received.
106int m_callback_friendrequest(void (*function)(uint8_t *, uint8_t *, uint16_t)) 121void m_callback_friendrequest(void (*function)(uint8_t *, uint8_t *, uint16_t))
107{ 122{
108 friend_request = function; 123 friend_request = function;
109} 124}
@@ -112,7 +127,7 @@ int m_callback_friendrequest(void (*function)(uint8_t *, uint8_t *, uint16_t))
112void (*friend_message)(int, uint8_t *, uint16_t); 127void (*friend_message)(int, uint8_t *, uint16_t);
113 128
114//set the function that will be executed when a message from a friend is received. 129//set the function that will be executed when a message from a friend is received.
115int m_callback_friendmessage(void (*function)(int, uint8_t *, uint16_t)) 130void m_callback_friendmessage(void (*function)(int, uint8_t *, uint16_t))
116{ 131{
117 friend_message = function; 132 friend_message = function;
118} 133}
@@ -146,14 +161,13 @@ void doFriends()
146 if(friendip.ip.i > 1 && request == -1) 161 if(friendip.ip.i > 1 && request == -1)
147 { 162 {
148 friendlist[i].friend_request_id = send_friendrequest(friendlist[i].client_id, friendip, info, info_size); 163 friendlist[i].friend_request_id = send_friendrequest(friendlist[i].client_id, friendip, info, info_size);
149 }
150 if(request == 1)
151 {
152 friendlist[i].status = 2; 164 friendlist[i].status = 2;
153 } 165 }
154 } 166 }
155 if(friendlist[i].status == 2 || friendlist[i].status == 3) 167 if(friendlist[i].status == 2 || friendlist[i].status == 3)
156 { 168 {
169 check_friendrequest(friendlist[i].friend_request_id);//for now this is used to kill the friend request
170
157 IP_Port friendip = DHT_getfriendip(friendlist[i].client_id); 171 IP_Port friendip = DHT_getfriendip(friendlist[i].client_id);
158 if(is_cryptoconnected(friendlist[i].crypt_connection_id) == 0 && friendip.ip.i > 1) 172 if(is_cryptoconnected(friendlist[i].crypt_connection_id) == 0 && friendip.ip.i > 1)
159 { 173 {
@@ -163,6 +177,10 @@ void doFriends()
163 { 177 {
164 friendlist[i].status = 4; 178 friendlist[i].status = 4;
165 } 179 }
180 if(is_cryptoconnected(friendlist[i].crypt_connection_id) == 4)
181 {
182 crypto_kill(friendlist[i].crypt_connection_id);
183 }
166 } 184 }
167 while(friendlist[i].status == 4) 185 while(friendlist[i].status == 4)
168 { 186 {
@@ -200,6 +218,45 @@ void doFriendRequest()
200 } 218 }
201 219
202} 220}
221
222
223//return the friend id associated to that public key.
224//return -1 if no such friend
225int getfriend_id(uint8_t * public_key)
226{
227 uint32_t i;
228 for(i = 0; i < numfriends; i++)
229 {
230 if(friendlist[i].status > 0)
231 {
232 if(memcmp(public_key, friendlist[i].client_id, crypto_box_PUBLICKEYBYTES) == 0)
233 {
234 return i;
235 }
236 }
237 }
238 return -1;
239}
240
241void doInbound()
242{
243 uint8_t secret_nonce[crypto_box_NONCEBYTES];
244 uint8_t public_key[crypto_box_PUBLICKEYBYTES];
245 uint8_t session_key[crypto_box_PUBLICKEYBYTES];
246 int inconnection = crypto_inbound(public_key, secret_nonce, session_key);
247 if(inconnection != -1)
248 {
249 int friend_id = getfriend_id(public_key);
250 if(friend_id != -1)
251 {
252 friendlist[friend_id].crypt_connection_id =
253 accept_crypto_inbound(inconnection, public_key, secret_nonce, session_key);
254
255 friendlist[friend_id].status = 3;
256 }
257 }
258}
259
203//the main loop that needs to be run at least 200 times per second. 260//the main loop that needs to be run at least 200 times per second.
204void doMessenger() 261void doMessenger()
205{ 262{
@@ -224,6 +281,7 @@ void doMessenger()
224 doDHT(); 281 doDHT();
225 doLossless_UDP(); 282 doLossless_UDP();
226 doNetCrypto(); 283 doNetCrypto();
284 doInbound();
227 doFriendRequest(); 285 doFriendRequest();
228 doFriends(); 286 doFriends();
229} 287}
diff --git a/core/Messenger.h b/core/Messenger.h
index 1032d8d8..7e6a0ba0 100644
--- a/core/Messenger.h
+++ b/core/Messenger.h
@@ -18,6 +18,12 @@
18int m_addfriend(uint8_t * client_id); 18int m_addfriend(uint8_t * client_id);
19 19
20 20
21//add a friend without sending a friendrequest.
22//returns the friend number if success
23//return -1 if failure.
24int m_addfriend_norequest(uint8_t * client_id);
25
26
21//remove a friend 27//remove a friend
22int m_delfriend(int friendnumber); 28int m_delfriend(int friendnumber);
23 29
@@ -36,12 +42,12 @@ int m_setinfo(uint8_t * data, uint16_t length);
36 42
37//set the function that will be executed when a friend request is received. 43//set the function that will be executed when a friend request is received.
38//function format is function(uint8_t * public_key, uint8_t * data, uint16_t length) 44//function format is function(uint8_t * public_key, uint8_t * data, uint16_t length)
39int m_callback_friendrequest(void (*function)(uint8_t *, uint8_t *, uint16_t)); 45void m_callback_friendrequest(void (*function)(uint8_t *, uint8_t *, uint16_t));
40 46
41 47
42//set the function that will be executed when a message from a friend is received. 48//set the function that will be executed when a message from a friend is received.
43//function format is: function(int friendnumber, uint8_t * message, uint32_t length) 49//function format is: function(int friendnumber, uint8_t * message, uint32_t length)
44int m_callback_friendmessage(void (*function)(int, uint8_t *, uint16_t)); 50void m_callback_friendmessage(void (*function)(int, uint8_t *, uint16_t));
45 51
46 52
47//run this at startup 53//run this at startup
diff --git a/core/net_crypto.c b/core/net_crypto.c
index bcdb2030..9f9378f6 100644
--- a/core/net_crypto.c
+++ b/core/net_crypto.c
@@ -383,7 +383,8 @@ int handle_friendrequest(uint8_t * public_key, uint8_t * data)
383 len - (crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES + 1), data); 383 len - (crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES + 1), data);
384 if(len1 != -1) 384 if(len1 != -1)
385 { 385 {
386 kill_connection_in(incoming_connections[i], 1); //conection is useless now, kill it in 1 seconds 386 kill_connection(incoming_connections[i]);
387 //kill_connection_in(incoming_connections[i], 1); //conection is useless now, kill it in 1 seconds
387 incoming_connections[i] = -1; 388 incoming_connections[i] = -1;
388 return len1; 389 return len1;
389 } 390 }
@@ -396,6 +397,25 @@ int handle_friendrequest(uint8_t * public_key, uint8_t * data)
396 return -1; 397 return -1;
397} 398}
398 399
400//get crypto connection id from public key of peer
401//return -1 if there are no connections like we are looking for
402//return id if it found it
403int getcryptconnection_id(uint8_t * public_key)
404{
405 uint32_t i;
406 for(i = 0; i < MAX_CRYPTO_CONNECTIONS; i++)
407 {
408 if(crypto_connections[i].status > 0)
409 {
410 if(memcmp(public_key, crypto_connections[i].public_key, crypto_box_PUBLICKEYBYTES) == 0)
411 {
412 return i;
413 }
414 }
415 }
416 return -1;
417}
418
399 419
400//Start a secure connection with other peer who has public_key and ip_port 420//Start a secure connection with other peer who has public_key and ip_port
401//returns -1 if failure 421//returns -1 if failure
@@ -403,7 +423,7 @@ int handle_friendrequest(uint8_t * public_key, uint8_t * data)
403int crypto_connect(uint8_t * public_key, IP_Port ip_port) 423int crypto_connect(uint8_t * public_key, IP_Port ip_port)
404{ 424{
405 uint32_t i; 425 uint32_t i;
406 if(getconnection_id(ip_port) != -1) 426 if(getcryptconnection_id(public_key) != -1)
407 { 427 {
408 return -1; 428 return -1;
409 } 429 }
@@ -493,6 +513,10 @@ int accept_crypto_inbound(int connection_id, uint8_t * public_key, uint8_t * sec
493 { 513 {
494 return -1; 514 return -1;
495 } 515 }
516 if(getcryptconnection_id(public_key) != -1)
517 {
518 return -1;
519 }
496 for(i = 0; i < MAX_CRYPTO_CONNECTIONS; i++) 520 for(i = 0; i < MAX_CRYPTO_CONNECTIONS; i++)
497 { 521 {
498 if(crypto_connections[i].status == 0) 522 if(crypto_connections[i].status == 0)
@@ -584,13 +608,22 @@ void receive_crypto()
584 { 608 {
585 if(crypto_connections[i].status == 1) 609 if(crypto_connections[i].status == 1)
586 { 610 {
611 uint8_t temp_data[MAX_DATA_SIZE];
612 uint8_t secret_nonce[crypto_box_NONCEBYTES];
613 uint8_t public_key[crypto_box_PUBLICKEYBYTES];
614 uint8_t session_key[crypto_box_PUBLICKEYBYTES];
615 uint16_t len;
616 if(id_packet(crypto_connections[i].number) == 1)
617 //if the packet is a friend request drop it (because we are already friends)
618 {
619 len = read_packet(crypto_connections[i].number, temp_data);
620 printf("REQUEST DROPPED\n");
621
622 }
587 if(id_packet(crypto_connections[i].number) == 2)//handle handshake packet. 623 if(id_packet(crypto_connections[i].number) == 2)//handle handshake packet.
588 { 624 {
589 uint8_t temp_data[MAX_DATA_SIZE]; 625
590 uint8_t secret_nonce[crypto_box_NONCEBYTES]; 626 len = read_packet(crypto_connections[i].number, temp_data);
591 uint8_t public_key[crypto_box_PUBLICKEYBYTES];
592 uint8_t session_key[crypto_box_PUBLICKEYBYTES];
593 uint16_t len = read_packet(crypto_connections[i].number, temp_data);
594 if(handle_cryptohandshake(public_key, secret_nonce, session_key, temp_data, len)) 627 if(handle_cryptohandshake(public_key, secret_nonce, session_key, temp_data, len))
595 { 628 {
596 if(memcmp(public_key, crypto_connections[i].public_key, crypto_box_PUBLICKEYBYTES) == 0) 629 if(memcmp(public_key, crypto_connections[i].public_key, crypto_box_PUBLICKEYBYTES) == 0)
@@ -660,7 +693,7 @@ void killTimedout()
660 uint32_t i; 693 uint32_t i;
661 for(i = 0; i < MAX_CRYPTO_CONNECTIONS; i++) 694 for(i = 0; i < MAX_CRYPTO_CONNECTIONS; i++)
662 { 695 {
663 if(is_connected(crypto_connections[i].number) == 4) 696 if(crypto_connections[i].status != 0 && is_connected(crypto_connections[i].number) == 4)
664 { 697 {
665 crypto_connections[i].status = 4; 698 crypto_connections[i].status = 4;
666 } 699 }
diff --git a/testing/Messenger_test.c b/testing/Messenger_test.c
index 4290d0fa..3c7c08e9 100644
--- a/testing/Messenger_test.c
+++ b/testing/Messenger_test.c
@@ -42,12 +42,22 @@ void print_request(uint8_t * public_key, uint8_t * data, uint16_t length)
42 } 42 }
43 printf("\nOf length: %u with data: %s \n", length, data); 43 printf("\nOf length: %u with data: %s \n", length, data);
44 44
45 if(length != sizeof("Install Gentoo"))
46 {
47 return;
48 }
49 if(memcmp(data ,"Install Gentoo", sizeof("Install Gentoo")) == 0 )
50 //if the request contained the message of peace the person is obviously a friend so we add him.
51 {
52 printf("Friend request accepted.\n");
53 m_addfriend_norequest(public_key);
54 }
45} 55}
46 56
47void print_message(int friendnumber, uint8_t * string, uint16_t length) 57void print_message(int friendnumber, uint8_t * string, uint16_t length)
48{ 58{
49 printf("Message with length %u recieved from %u: %s \n", length, friendnumber, string); 59 printf("Message with length %u recieved from %u: %s \n", length, friendnumber, string);
50 60 m_sendmessage(friendnumber, "Test1", 6);
51} 61}
52 62
53int main(int argc, char *argv[]) 63int main(int argc, char *argv[])
@@ -86,7 +96,7 @@ int main(int argc, char *argv[])
86 { 96 {
87 m_sendmessage(num, "Test", 5); 97 m_sendmessage(num, "Test", 5);
88 doMessenger(); 98 doMessenger();
89 c_sleep(1); 99 c_sleep(30);
90 } 100 }
91 101
92} 102}