diff options
-rw-r--r-- | core/Messenger.c | 110 | ||||
-rw-r--r-- | core/Messenger.h | 3 | ||||
-rw-r--r-- | testing/DHT_cryptosendfiletest.c | 2 | ||||
-rw-r--r-- | testing/Messenger_test.c | 2 |
4 files changed, 80 insertions, 37 deletions
diff --git a/core/Messenger.c b/core/Messenger.c index 57baa84c..7603a04f 100644 --- a/core/Messenger.c +++ b/core/Messenger.c | |||
@@ -29,39 +29,96 @@ Friend friendlist[MAX_NUM_FRIENDS]; | |||
29 | 29 | ||
30 | uint32_t numfriends; | 30 | uint32_t numfriends; |
31 | 31 | ||
32 | |||
33 | //return the friend id associated to that public key. | ||
34 | //return -1 if no such friend | ||
35 | int getfriend_id(uint8_t * client_id) | ||
36 | { | ||
37 | uint32_t i; | ||
38 | for(i = 0; i < numfriends; i++) | ||
39 | { | ||
40 | if(friendlist[i].status > 0) | ||
41 | { | ||
42 | if(memcmp(client_id, friendlist[i].client_id, crypto_box_PUBLICKEYBYTES) == 0) | ||
43 | { | ||
44 | return i; | ||
45 | } | ||
46 | } | ||
47 | } | ||
48 | return -1; | ||
49 | } | ||
50 | |||
51 | |||
32 | //add a friend | 52 | //add a friend |
33 | //client_id is the client i of the friend | 53 | //client_id is the client i of the friend |
34 | //returns the friend number if success | 54 | //returns the friend number if success |
35 | //return -1 if failure. | 55 | //return -1 if failure. |
36 | int m_addfriend(uint8_t * client_id) | 56 | int m_addfriend(uint8_t * client_id) |
37 | { | 57 | { |
38 | 58 | if(getfriend_id(client_id) != -1) | |
39 | DHT_addfriend(client_id); | 59 | { |
40 | friendlist[numfriends].status = 1; | 60 | return -1; |
41 | friendlist[numfriends].friend_request_id = -1; | 61 | } |
42 | memcpy(friendlist[numfriends].client_id, client_id, CLIENT_ID_SIZE); | 62 | uint32_t i; |
43 | numfriends++; | 63 | for(i = 0; i < (numfriends + 1); i++) |
44 | 64 | { | |
45 | return numfriends - 1; | 65 | if(friendlist[i].status == 0) |
66 | { | ||
67 | DHT_addfriend(client_id); | ||
68 | friendlist[i].status = 1; | ||
69 | friendlist[i].friend_request_id = -1; | ||
70 | memcpy(friendlist[i].client_id, client_id, CLIENT_ID_SIZE); | ||
71 | numfriends++; | ||
72 | return i; | ||
73 | } | ||
74 | } | ||
75 | return -1; | ||
46 | } | 76 | } |
47 | 77 | ||
48 | int m_addfriend_norequest(uint8_t * client_id) | 78 | int m_addfriend_norequest(uint8_t * client_id) |
49 | { | 79 | { |
50 | DHT_addfriend(client_id); | 80 | if(getfriend_id(client_id) != -1) |
51 | friendlist[numfriends].status = 2; | 81 | { |
52 | friendlist[numfriends].friend_request_id = -1; | 82 | return -1; |
53 | memcpy(friendlist[numfriends].client_id, client_id, CLIENT_ID_SIZE); | 83 | } |
54 | numfriends++; | 84 | uint32_t i; |
55 | 85 | for(i = 0; i < (numfriends + 1); i++) | |
56 | return numfriends - 1; | 86 | { |
87 | if(friendlist[i].status == 0) | ||
88 | { | ||
89 | DHT_addfriend(client_id); | ||
90 | friendlist[i].status = 2; | ||
91 | friendlist[i].friend_request_id = -1; | ||
92 | memcpy(friendlist[i].client_id, client_id, CLIENT_ID_SIZE); | ||
93 | numfriends++; | ||
94 | return i; | ||
95 | } | ||
96 | } | ||
97 | return -1; | ||
57 | } | 98 | } |
58 | 99 | ||
59 | //remove a friend | 100 | //remove a friend |
101 | //returns 0 if success | ||
102 | //return -1 if failure. | ||
60 | int m_delfriend(int friendnumber) | 103 | int m_delfriend(int friendnumber) |
61 | {/* | 104 | { |
62 | TODO | 105 | if(friendnumber >= numfriends || friendnumber < 0) |
106 | { | ||
107 | return -1; | ||
108 | } | ||
109 | |||
63 | DHT_delfriend(friendlist[friendnumber].client_id); | 110 | DHT_delfriend(friendlist[friendnumber].client_id); |
64 | */ | 111 | memset(&friendlist[friendnumber], 0, sizeof(Friend)); |
112 | uint32_t i; | ||
113 | for(i = numfriends; i != 0; i--) | ||
114 | { | ||
115 | if(friendlist[i].status != 0) | ||
116 | { | ||
117 | break; | ||
118 | } | ||
119 | } | ||
120 | numfriends = i; | ||
121 | return 0; | ||
65 | } | 122 | } |
66 | 123 | ||
67 | 124 | ||
@@ -220,23 +277,6 @@ void doFriendRequest() | |||
220 | } | 277 | } |
221 | 278 | ||
222 | 279 | ||
223 | //return the friend id associated to that public key. | ||
224 | //return -1 if no such friend | ||
225 | int 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 | 280 | ||
241 | void doInbound() | 281 | void doInbound() |
242 | { | 282 | { |
diff --git a/core/Messenger.h b/core/Messenger.h index 4e56b4fc..42ce20ca 100644 --- a/core/Messenger.h +++ b/core/Messenger.h | |||
@@ -23,6 +23,9 @@ int m_addfriend(uint8_t * client_id); | |||
23 | //return -1 if failure. | 23 | //return -1 if failure. |
24 | int m_addfriend_norequest(uint8_t * client_id); | 24 | int m_addfriend_norequest(uint8_t * client_id); |
25 | 25 | ||
26 | //return the friend id associated to that client id. | ||
27 | //return -1 if no such friend | ||
28 | int getfriend_id(uint8_t * client_id); | ||
26 | 29 | ||
27 | //remove a friend | 30 | //remove a friend |
28 | int m_delfriend(int friendnumber); | 31 | int m_delfriend(int friendnumber); |
diff --git a/testing/DHT_cryptosendfiletest.c b/testing/DHT_cryptosendfiletest.c index ad77a17d..39a928d2 100644 --- a/testing/DHT_cryptosendfiletest.c +++ b/testing/DHT_cryptosendfiletest.c | |||
@@ -7,7 +7,7 @@ | |||
7 | * | 7 | * |
8 | * NOTE: this program simulates 33% packet loss. | 8 | * NOTE: this program simulates 33% packet loss. |
9 | * | 9 | * |
10 | * This is how I compile it: gcc -O2 -Wall -o test ../core/Lossless_UDP.c ../core/network.c ../core/net_crypto.c ../core/DHT.c ../nacl/build/Linux/lib/amd64/* DHT_cryptosendfiletest.c | 10 | * This is how I compile it: gcc -O2 -Wall -o test ../core/Lossless_UDP.c ../core/network.c ../core/net_crypto.c ../core/DHT.c ../nacl/build/$HOSTNAME/lib/amd64/* DHT_cryptosendfiletest.c |
11 | * | 11 | * |
12 | * | 12 | * |
13 | * Command line arguments are the ip and port of a node (for bootstrapping). | 13 | * Command line arguments are the ip and port of a node (for bootstrapping). |
diff --git a/testing/Messenger_test.c b/testing/Messenger_test.c index 8b19ffa5..ea32673d 100644 --- a/testing/Messenger_test.c +++ b/testing/Messenger_test.c | |||
@@ -7,7 +7,7 @@ | |||
7 | * If it recieves a message from a friend it replies back. | 7 | * If it recieves a message from a friend it replies back. |
8 | * | 8 | * |
9 | * | 9 | * |
10 | * This is how I compile it: gcc -O2 -Wall -o test ../core/Lossless_UDP.c ../core/network.c ../core/net_crypto.c ../core/DHT.c ../core/Messenger.c ../nacl/build/Linux/lib/amd64/* Messenger_test.c | 10 | * This is how I compile it: gcc -O2 -Wall -o test ../core/Lossless_UDP.c ../core/network.c ../core/net_crypto.c ../core/DHT.c ../core/Messenger.c ../nacl/build/$HOSTNAME/lib/amd64/* Messenger_test.c |
11 | * | 11 | * |
12 | * | 12 | * |
13 | * Command line arguments are the ip and port of a node (for bootstrapping). | 13 | * Command line arguments are the ip and port of a node (for bootstrapping). |