diff options
Diffstat (limited to 'core/Messenger.c')
-rw-r--r-- | core/Messenger.c | 70 |
1 files changed, 64 insertions, 6 deletions
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 | ||
48 | int 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 |
49 | int m_delfriend(int friendnumber) | 60 | int 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. |
75 | int m_sendmessage(int friendnumber, uint8_t * message, uint32_t length) | 86 | int 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) | |||
103 | void (*friend_request)(uint8_t *, uint8_t *, uint16_t); | 118 | void (*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. |
106 | int m_callback_friendrequest(void (*function)(uint8_t *, uint8_t *, uint16_t)) | 121 | void 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)) | |||
112 | void (*friend_message)(int, uint8_t *, uint16_t); | 127 | void (*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. |
115 | int m_callback_friendmessage(void (*function)(int, uint8_t *, uint16_t)) | 130 | void 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 | ||
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 | |||
241 | void 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. |
204 | void doMessenger() | 261 | void 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 | } |