summaryrefslogtreecommitdiff
path: root/toxcore
diff options
context:
space:
mode:
authorJamoBox <petewicken@gmail.com>2013-09-02 17:12:02 +0100
committerJamoBox <petewicken@gmail.com>2013-09-02 17:12:02 +0100
commit7b944e9c656f5bc9e0165db666db0a89963247c8 (patch)
treec06738ffff9ee3d89e0e93fe8a2d523cf0cc907a /toxcore
parent433cc9c8b255f62b912ae2b81d99838577ecf261 (diff)
comment updates
Diffstat (limited to 'toxcore')
-rw-r--r--toxcore/Messenger.c95
-rw-r--r--toxcore/Messenger.h85
-rw-r--r--toxcore/misc_tools.h2
-rw-r--r--toxcore/net_crypto.c65
-rw-r--r--toxcore/net_crypto.h36
5 files changed, 156 insertions, 127 deletions
diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c
index 70623611..e28ff9db 100644
--- a/toxcore/Messenger.c
+++ b/toxcore/Messenger.c
@@ -28,13 +28,15 @@
28static void set_friend_status(Messenger *m, int friendnumber, uint8_t status); 28static void set_friend_status(Messenger *m, int friendnumber, uint8_t status);
29static int write_cryptpacket_id(Messenger *m, int friendnumber, uint8_t packet_id, uint8_t *data, uint32_t length); 29static int write_cryptpacket_id(Messenger *m, int friendnumber, uint8_t packet_id, uint8_t *data, uint32_t length);
30 30
31/* return 1 if we are online. 31/* static uint8_t online;
32 * return 0 if we are offline. 32 *
33 * static uint8_t online; 33 * return 1 if we are online.
34 * return 0 if we are offline.
34 */ 35 */
35 36
36/* Set the size of the friend list to numfriends. 37/* Set the size of the friend list to numfriends.
37 * return -1 if realloc fails. 38 *
39 * return -1 if realloc fails.
38 */ 40 */
39int realloc_friendlist(Messenger *m, uint32_t num) 41int realloc_friendlist(Messenger *m, uint32_t num)
40{ 42{
@@ -53,8 +55,8 @@ int realloc_friendlist(Messenger *m, uint32_t num)
53 return 0; 55 return 0;
54} 56}
55 57
56/* return the friend id associated to that public key. 58/* return the friend id associated to that public key.
57 * return -1 if no such friend. 59 * return -1 if no such friend.
58 */ 60 */
59int getfriend_id(Messenger *m, uint8_t *client_id) 61int getfriend_id(Messenger *m, uint8_t *client_id)
60{ 62{
@@ -71,8 +73,9 @@ int getfriend_id(Messenger *m, uint8_t *client_id)
71 73
72/* Copies the public key associated to that friend id into client_id buffer. 74/* Copies the public key associated to that friend id into client_id buffer.
73 * Make sure that client_id is of size CLIENT_ID_SIZE. 75 * Make sure that client_id is of size CLIENT_ID_SIZE.
74 * return 0 if success. 76 *
75 * return -1 if failure. 77 * return 0 if success.
78 * return -1 if failure.
76 */ 79 */
77int getclient_id(Messenger *m, int friend_id, uint8_t *client_id) 80int getclient_id(Messenger *m, int friend_id, uint8_t *client_id)
78{ 81{
@@ -86,10 +89,9 @@ int getclient_id(Messenger *m, int friend_id, uint8_t *client_id)
86 89
87 return -1; 90 return -1;
88} 91}
89/* 92/* TODO: Another checksum algorithm might be better.
90 * return a uint16_t that represents the checksum of address of length len.
91 * 93 *
92 * TODO: Another checksum algorithm might be better. 94 * return a uint16_t that represents the checksum of address of length len.
93 */ 95 */
94static uint16_t address_checksum(uint8_t *address, uint32_t len) 96static uint16_t address_checksum(uint8_t *address, uint32_t len)
95{ 97{
@@ -104,10 +106,9 @@ static uint16_t address_checksum(uint8_t *address, uint32_t len)
104 return check; 106 return check;
105} 107}
106 108
107/* 109/* Format: [client_id (32 bytes)][nospam number (4 bytes)][checksum (2 bytes)]
108 * return FRIEND_ADDRESS_SIZE byte address to give to others.
109 * Format: [client_id (32 bytes)][nospam number (4 bytes)][checksum (2 bytes)]
110 * 110 *
111 * return FRIEND_ADDRESS_SIZE byte address to give to others.
111 */ 112 */
112void getaddress(Messenger *m, uint8_t *address) 113void getaddress(Messenger *m, uint8_t *address)
113{ 114{
@@ -124,16 +125,17 @@ void getaddress(Messenger *m, uint8_t *address)
124 * Address is the address of the friend (returned by getaddress of the friend you wish to add) it must be FRIEND_ADDRESS_SIZE bytes. 125 * Address is the address of the friend (returned by getaddress of the friend you wish to add) it must be FRIEND_ADDRESS_SIZE bytes.
125 * TODO: add checksum. 126 * TODO: add checksum.
126 * data is the data and length is the length. 127 * data is the data and length is the length.
127 * return the friend number if success. 128 *
128 * return FA_TOOLONG if message length is too long. 129 * return the friend number if success.
129 * return FAERR_NOMESSAGE if no message (message length must be >= 1 byte). 130 * return FA_TOOLONG if message length is too long.
130 * return FAERR_OWNKEY if user's own key. 131 * return FAERR_NOMESSAGE if no message (message length must be >= 1 byte).
131 * return FAERR_ALREADYSENT if friend request already sent or already a friend. 132 * return FAERR_OWNKEY if user's own key.
132 * return FAERR_UNKNOWN for unknown error. 133 * return FAERR_ALREADYSENT if friend request already sent or already a friend.
133 * return FAERR_BADCHECKSUM if bad checksum in address. 134 * return FAERR_UNKNOWN for unknown error.
134 * return FAERR_SETNEWNOSPAM if the friend was already there but the nospam was different. 135 * return FAERR_BADCHECKSUM if bad checksum in address.
135 * (the nospam for that friend was set to the new one). 136 * return FAERR_SETNEWNOSPAM if the friend was already there but the nospam was different.
136 * return FAERR_NOMEM if increasing the friend list size fails. 137 * (the nospam for that friend was set to the new one).
138 * return FAERR_NOMEM if increasing the friend list size fails.
137 */ 139 */
138int m_addfriend(Messenger *m, uint8_t *address, uint8_t *data, uint16_t length) 140int m_addfriend(Messenger *m, uint8_t *address, uint8_t *data, uint16_t length)
139{ 141{
@@ -241,6 +243,7 @@ int m_addfriend_norequest(Messenger *m, uint8_t *client_id)
241} 243}
242 244
243/* Remove a friend. 245/* Remove a friend.
246 *
244 * return 0 if success. 247 * return 0 if success.
245 * return -1 if failure. 248 * return -1 if failure.
246 */ 249 */
@@ -268,11 +271,11 @@ int m_delfriend(Messenger *m, int friendnumber)
268 return 0; 271 return 0;
269} 272}
270 273
271/* return FRIEND_ONLINE if friend is online. 274/* return FRIEND_ONLINE if friend is online.
272 * return FRIEND_CONFIRMED if friend is confirmed. 275 * return FRIEND_CONFIRMED if friend is confirmed.
273 * return FRIEND_REQUESTED if the friend request was sent. 276 * return FRIEND_REQUESTED if the friend request was sent.
274 * return FRIEND_ADDED if the friend was added. 277 * return FRIEND_ADDED if the friend was added.
275 * return NOFRIEND if there is no friend with that number. 278 * return NOFRIEND if there is no friend with that number.
276 */ 279 */
277int m_friendstatus(Messenger *m, int friendnumber) 280int m_friendstatus(Messenger *m, int friendnumber)
278{ 281{
@@ -283,8 +286,9 @@ int m_friendstatus(Messenger *m, int friendnumber)
283} 286}
284 287
285/* Send a text chat message to an online friend. 288/* Send a text chat message to an online friend.
286 * return the message id if packet was successfully put into the send queue. 289 *
287 * return 0 if it was not. 290 * return the message id if packet was successfully put into the send queue.
291 * return 0 if it was not.
288 */ 292 */
289uint32_t m_sendmessage(Messenger *m, int friendnumber, uint8_t *message, uint32_t length) 293uint32_t m_sendmessage(Messenger *m, int friendnumber, uint8_t *message, uint32_t length)
290{ 294{
@@ -316,8 +320,9 @@ uint32_t m_sendmessage_withid(Messenger *m, int friendnumber, uint32_t theid, ui
316} 320}
317 321
318/* Send an action to an online friend. 322/* Send an action to an online friend.
319 * return 1 if packet was successfully put into the send queue. 323 *
320 * return 0 if it was not. 324 * return 1 if packet was successfully put into the send queue.
325 * return 0 if it was not.
321 */ 326 */
322int m_sendaction(Messenger *m, int friendnumber, uint8_t *action, uint32_t length) 327int m_sendaction(Messenger *m, int friendnumber, uint8_t *action, uint32_t length)
323{ 328{
@@ -336,8 +341,9 @@ static int m_sendname(Messenger *m, int friendnumber, uint8_t *name, uint16_t le
336} 341}
337 342
338/* Set the name of a friend. 343/* Set the name of a friend.
339 * return 0 if success. 344 *
340 * return -1 if failure. 345 * return 0 if success.
346 * return -1 if failure.
341 */ 347 */
342static int setfriendname(Messenger *m, int friendnumber, uint8_t *name) 348static int setfriendname(Messenger *m, int friendnumber, uint8_t *name)
343{ 349{
@@ -352,8 +358,9 @@ static int setfriendname(Messenger *m, int friendnumber, uint8_t *name)
352 * name must be a string of maximum MAX_NAME_LENGTH length. 358 * name must be a string of maximum MAX_NAME_LENGTH length.
353 * length must be at least 1 byte. 359 * length must be at least 1 byte.
354 * length is the length of name with the NULL terminator. 360 * length is the length of name with the NULL terminator.
355 * return 0 if success. 361 *
356 * return -1 if failure. 362 * return 0 if success.
363 * return -1 if failure.
357 */ 364 */
358int setname(Messenger *m, uint8_t *name, uint16_t length) 365int setname(Messenger *m, uint8_t *name, uint16_t length)
359{ 366{
@@ -371,8 +378,9 @@ int setname(Messenger *m, uint8_t *name, uint16_t length)
371} 378}
372 379
373/* Get our nickname and put it in name. 380/* Get our nickname and put it in name.
374 * Name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH bytes. 381 * name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH bytes.
375 * return the length of the name. 382 *
383 * return the length of the name.
376 */ 384 */
377uint16_t getself_name(Messenger *m, uint8_t *name, uint16_t nlen) 385uint16_t getself_name(Messenger *m, uint8_t *name, uint16_t nlen)
378{ 386{
@@ -389,9 +397,10 @@ uint16_t getself_name(Messenger *m, uint8_t *name, uint16_t nlen)
389} 397}
390 398
391/* Get name of friendnumber and put it in name. 399/* Get name of friendnumber and put it in name.
392 * Name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH bytes. 400 * name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH bytes.
393 * return 0 if success. 401 *
394 * return -1 if failure. 402 * return 0 if success.
403 * return -1 if failure.
395 */ 404 */
396int getname(Messenger *m, int friendnumber, uint8_t *name) 405int getname(Messenger *m, int friendnumber, uint8_t *name)
397{ 406{
@@ -918,7 +927,7 @@ void doMessenger(Messenger *m)
918 LANdiscovery(m); 927 LANdiscovery(m);
919} 928}
920 929
921/* return the size of the messenger data (for saving) */ 930/* return size of the messenger data (for saving) */
922uint32_t Messenger_size(Messenger *m) 931uint32_t Messenger_size(Messenger *m)
923{ 932{
924 return crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES 933 return crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES
diff --git a/toxcore/Messenger.h b/toxcore/Messenger.h
index 05b049ae..fedf2879 100644
--- a/toxcore/Messenger.h
+++ b/toxcore/Messenger.h
@@ -154,28 +154,27 @@ typedef struct Messenger {
154 154
155} Messenger; 155} Messenger;
156 156
157/* 157/* Format: [client_id (32 bytes)][nospam number (4 bytes)][checksum (2 bytes)]
158 * return FRIEND_ADDRESS_SIZE byte address to give to others.
159 * Format: [client_id (32 bytes)][nospam number (4 bytes)][checksum (2 bytes)]
160 * 158 *
159 * return FRIEND_ADDRESS_SIZE byte address to give to others.
161 */ 160 */
162void getaddress(Messenger *m, uint8_t *address); 161void getaddress(Messenger *m, uint8_t *address);
163 162
164/* 163/* Add a friend.
165 * Add a friend.
166 * Set the data that will be sent along with friend request. 164 * Set the data that will be sent along with friend request.
167 * address is the address of the friend (returned by getaddress of the friend you wish to add) it must be FRIEND_ADDRESS_SIZE bytes. TODO: add checksum. 165 * address is the address of the friend (returned by getaddress of the friend you wish to add) it must be FRIEND_ADDRESS_SIZE bytes. TODO: add checksum.
168 * data is the data and length is the length. 166 * data is the data and length is the length.
169 * returns the friend number if success. 167 *
170 * return -1 if message length is too long. 168 * return the friend number if success.
171 * return -2 if no message (message length must be >= 1 byte). 169 * return -1 if message length is too long.
172 * return -3 if user's own key. 170 * return -2 if no message (message length must be >= 1 byte).
173 * return -4 if friend request already sent or already a friend. 171 * return -3 if user's own key.
174 * return -5 for unknown error. 172 * return -4 if friend request already sent or already a friend.
175 * return -6 if bad checksum in address. 173 * return -5 for unknown error.
176 * return -7 if the friend was already there but the nospam was different. 174 * return -6 if bad checksum in address.
177 * (the nospam for that friend was set to the new one). 175 * return -7 if the friend was already there but the nospam was different.
178 * return -8 if increasing the friend list size fails. 176 * (the nospam for that friend was set to the new one).
177 * return -8 if increasing the friend list size fails.
179 */ 178 */
180int m_addfriend(Messenger *m, uint8_t *address, uint8_t *data, uint16_t length); 179int m_addfriend(Messenger *m, uint8_t *address, uint8_t *data, uint16_t length);
181 180
@@ -186,13 +185,14 @@ int m_addfriend(Messenger *m, uint8_t *address, uint8_t *data, uint16_t length);
186 */ 185 */
187int m_addfriend_norequest(Messenger *m, uint8_t *client_id); 186int m_addfriend_norequest(Messenger *m, uint8_t *client_id);
188 187
189/* return the friend id associated to that client id. 188/* return the friend id associated to that client id.
190 * return -1 if no such friend. 189 * return -1 if no such friend.
191 */ 190 */
192int getfriend_id(Messenger *m, uint8_t *client_id); 191int getfriend_id(Messenger *m, uint8_t *client_id);
193 192
194/* Copies the public key associated to that friend id into client_id buffer. 193/* Copies the public key associated to that friend id into client_id buffer.
195 * Make sure that client_id is of size CLIENT_ID_SIZE. 194 * Make sure that client_id is of size CLIENT_ID_SIZE.
195 *
196 * return 0 if success 196 * return 0 if success
197 * return -1 if failure 197 * return -1 if failure
198 */ 198 */
@@ -201,15 +201,16 @@ int getclient_id(Messenger *m, int friend_id, uint8_t *client_id);
201/* Remove a friend. */ 201/* Remove a friend. */
202int m_delfriend(Messenger *m, int friendnumber); 202int m_delfriend(Messenger *m, int friendnumber);
203 203
204/* return 4 if friend is online. 204/* return 4 if friend is online.
205 * return 3 if friend is confirmed. 205 * return 3 if friend is confirmed.
206 * return 2 if the friend request was sent. 206 * return 2 if the friend request was sent.
207 * return 1 if the friend was added. 207 * return 1 if the friend was added.
208 * return 0 if there is no friend with that number. 208 * return 0 if there is no friend with that number.
209 */ 209 */
210int m_friendstatus(Messenger *m, int friendnumber); 210int m_friendstatus(Messenger *m, int friendnumber);
211 211
212/* Send a text chat message to an online friend. 212/* Send a text chat message to an online friend.
213 *
213 * return the message id if packet was successfully put into the send queue. 214 * return the message id if packet was successfully put into the send queue.
214 * return 0 if it was not. 215 * return 0 if it was not.
215 * 216 *
@@ -222,6 +223,7 @@ uint32_t m_sendmessage(Messenger *m, int friendnumber, uint8_t *message, uint32_
222uint32_t m_sendmessage_withid(Messenger *m, int friendnumber, uint32_t theid, uint8_t *message, uint32_t length); 223uint32_t m_sendmessage_withid(Messenger *m, int friendnumber, uint32_t theid, uint8_t *message, uint32_t length);
223 224
224/* Send an action to an online friend. 225/* Send an action to an online friend.
226 *
225 * return 1 if packet was successfully put into the send queue. 227 * return 1 if packet was successfully put into the send queue.
226 * return 0 if it was not. 228 * return 0 if it was not.
227 */ 229 */
@@ -231,8 +233,9 @@ int m_sendaction(Messenger *m, int friendnumber, uint8_t *action, uint32_t lengt
231 * name must be a string of maximum MAX_NAME_LENGTH length. 233 * name must be a string of maximum MAX_NAME_LENGTH length.
232 * length must be at least 1 byte. 234 * length must be at least 1 byte.
233 * length is the length of name with the NULL terminator. 235 * length is the length of name with the NULL terminator.
234 * return 0 if success. 236 *
235 * return -1 if failure. 237 * return 0 if success.
238 * return -1 if failure.
236 */ 239 */
237int setname(Messenger *m, uint8_t *name, uint16_t length); 240int setname(Messenger *m, uint8_t *name, uint16_t length);
238 241
@@ -241,28 +244,30 @@ int setname(Messenger *m, uint8_t *name, uint16_t length);
241 * m - The messanger context to use. 244 * m - The messanger context to use.
242 * name - Pointer to a string for the name. 245 * name - Pointer to a string for the name.
243 * nlen - The length of the string buffer. 246 * nlen - The length of the string buffer.
244 * return length of the name. 247 *
245 * return 0 on error. 248 * return length of the name.
249 * return 0 on error.
246 */ 250 */
247uint16_t getself_name(Messenger *m, uint8_t *name, uint16_t nlen); 251uint16_t getself_name(Messenger *m, uint8_t *name, uint16_t nlen);
248 252
249/* Get name of friendnumber and put it in name. 253/* Get name of friendnumber and put it in name.
250 * name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH (128) bytes. 254 * name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH (128) bytes.
255 *
251 * return 0 if success. 256 * return 0 if success.
252 * return -1 if failure. 257 * return -1 if failure.
253 */ 258 */
254int getname(Messenger *m, int friendnumber, uint8_t *name); 259int getname(Messenger *m, int friendnumber, uint8_t *name);
255 260
256/* Set our user status. 261/* Set our user status.
257 * You are responsible for freeing status after. 262 * You are responsible for freeing status after.
263 *
258 * returns 0 on success. 264 * returns 0 on success.
259 * returns -1 on failure. 265 * returns -1 on failure.
260 */ 266 */
261int m_set_statusmessage(Messenger *m, uint8_t *status, uint16_t length); 267int m_set_statusmessage(Messenger *m, uint8_t *status, uint16_t length);
262int m_set_userstatus(Messenger *m, USERSTATUS status); 268int m_set_userstatus(Messenger *m, USERSTATUS status);
263 269
264/* return the length of friendnumber's status message, 270/* return the length of friendnumber's status message, including null.
265 * including null.
266 * Pass it into malloc. 271 * Pass it into malloc.
267 */ 272 */
268int m_get_statusmessage_size(Messenger *m, int friendnumber); 273int m_get_statusmessage_size(Messenger *m, int friendnumber);
@@ -274,10 +279,10 @@ int m_get_statusmessage_size(Messenger *m, int friendnumber);
274int m_copy_statusmessage(Messenger *m, int friendnumber, uint8_t *buf, uint32_t maxlen); 279int m_copy_statusmessage(Messenger *m, int friendnumber, uint8_t *buf, uint32_t maxlen);
275int m_copy_self_statusmessage(Messenger *m, uint8_t *buf, uint32_t maxlen); 280int m_copy_self_statusmessage(Messenger *m, uint8_t *buf, uint32_t maxlen);
276 281
277/* return one of USERSTATUS values. 282/* return one of USERSTATUS values.
278 * Values unknown to your application should be represented as USERSTATUS_NONE. 283 * Values unknown to your application should be represented as USERSTATUS_NONE.
279 * As above, the self variant will return our own USERSTATUS. 284 * As above, the self variant will return our own USERSTATUS.
280 * If friendnumber is invalid, this shall return USERSTATUS_INVALID. 285 * If friendnumber is invalid, this shall return USERSTATUS_INVALID.
281 */ 286 */
282USERSTATUS m_get_userstatus(Messenger *m, int friendnumber); 287USERSTATUS m_get_userstatus(Messenger *m, int friendnumber);
283USERSTATUS m_get_self_userstatus(Messenger *m); 288USERSTATUS m_get_self_userstatus(Messenger *m);
@@ -299,7 +304,7 @@ void m_callback_friendmessage(Messenger *m, void (*function)(Messenger *m, int,
299 void *userdata); 304 void *userdata);
300 305
301/* Set the function that will be executed when an action from a friend is received. 306/* Set the function that will be executed when an action from a friend is received.
302 * function format is: function(int friendnumber, uint8_t * action, uint32_t length) 307 * Function format is: function(int friendnumber, uint8_t * action, uint32_t length)
303 */ 308 */
304void m_callback_action(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t, void *), void *userdata); 309void m_callback_action(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t, void *), void *userdata);
305 310
@@ -311,7 +316,7 @@ void m_callback_namechange(Messenger *m, void (*function)(Messenger *m, int, uin
311 void *userdata); 316 void *userdata);
312 317
313/* Set the callback for status message changes. 318/* Set the callback for status message changes.
314 * function(int friendnumber, uint8_t *newstatus, uint16_t length) 319 * Function(int friendnumber, uint8_t *newstatus, uint16_t length)
315 * 320 *
316 * You are not responsible for freeing newstatus 321 * You are not responsible for freeing newstatus
317 */ 322 */
@@ -319,12 +324,12 @@ void m_callback_statusmessage(Messenger *m, void (*function)(Messenger *m, int,
319 void *userdata); 324 void *userdata);
320 325
321/* Set the callback for status type changes. 326/* Set the callback for status type changes.
322 * function(int friendnumber, USERSTATUS kind) 327 * Function(int friendnumber, USERSTATUS kind)
323 */ 328 */
324void m_callback_userstatus(Messenger *m, void (*function)(Messenger *m, int, USERSTATUS, void *), void *userdata); 329void m_callback_userstatus(Messenger *m, void (*function)(Messenger *m, int, USERSTATUS, void *), void *userdata);
325 330
326/* Set the callback for read receipts. 331/* Set the callback for read receipts.
327 * function(int friendnumber, uint32_t receipt) 332 * Function(int friendnumber, uint32_t receipt)
328 * 333 *
329 * If you are keeping a record of returns from m_sendmessage, 334 * If you are keeping a record of returns from m_sendmessage,
330 * receipt might be one of those values, meaning the message 335 * receipt might be one of those values, meaning the message
@@ -348,8 +353,8 @@ void m_callback_read_receipt(Messenger *m, void (*function)(Messenger *m, int, u
348void m_callback_connectionstatus(Messenger *m, void (*function)(Messenger *m, int, uint8_t, void *), void *userdata); 353void m_callback_connectionstatus(Messenger *m, void (*function)(Messenger *m, int, uint8_t, void *), void *userdata);
349 354
350/* Run this at startup. 355/* Run this at startup.
351 * returns allocated instance of Messenger on success. 356 * return allocated instance of Messenger on success.
352 * returns 0 if there are problems. 357 * return 0 if there are problems.
353 */ 358 */
354Messenger *initMessenger(void); 359Messenger *initMessenger(void);
355 360
diff --git a/toxcore/misc_tools.h b/toxcore/misc_tools.h
index 1889eba4..af31ff54 100644
--- a/toxcore/misc_tools.h
+++ b/toxcore/misc_tools.h
@@ -84,7 +84,7 @@ typedef struct tox_list {
84 struct tox_list *prev, *next; 84 struct tox_list *prev, *next;
85} tox_list; 85} tox_list;
86 86
87/* Returns a new tox_list_t. */ 87/* return new tox_list_t. */
88static inline void tox_list_new(tox_list *lst) 88static inline void tox_list_new(tox_list *lst)
89{ 89{
90 lst->prev = lst->next = lst; 90 lst->prev = lst->next = lst;
diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c
index 87c98089..739003a4 100644
--- a/toxcore/net_crypto.c
+++ b/toxcore/net_crypto.c
@@ -144,9 +144,9 @@ void random_nonce(uint8_t *nonce)
144 } 144 }
145} 145}
146 146
147/* return 0 if there is no received data in the buffer. 147/* return 0 if there is no received data in the buffer.
148 * return -1 if the packet was discarded. 148 * return -1 if the packet was discarded.
149 * return length of received data if successful. 149 * return length of received data if successful.
150 */ 150 */
151int read_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data) 151int read_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data)
152{ 152{
@@ -177,8 +177,8 @@ int read_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data)
177 return -1; 177 return -1;
178} 178}
179 179
180/* return 0 if data could not be put in packet queue. 180/* return 0 if data could not be put in packet queue.
181 * return 1 if data was put into the queue. 181 * return 1 if data was put into the queue.
182 */ 182 */
183int write_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data, uint32_t length) 183int write_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data, uint32_t length)
184{ 184{
@@ -215,8 +215,8 @@ int write_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data, uin
215 * Data represents the data we send with the request with length being the length of the data. 215 * Data represents the data we send with the request with length being the length of the data.
216 * request_id is the id of the request (32 = friend request, 254 = ping request). 216 * request_id is the id of the request (32 = friend request, 254 = ping request).
217 * 217 *
218 * returns -1 on failure. 218 * return -1 on failure.
219 * returns the length of the created packet on success. 219 * return the length of the created packet on success.
220 */ 220 */
221int create_request(uint8_t *send_public_key, uint8_t *send_secret_key, uint8_t *packet, uint8_t *recv_public_key, 221int create_request(uint8_t *send_public_key, uint8_t *send_secret_key, uint8_t *packet, uint8_t *recv_public_key,
222 uint8_t *data, uint32_t length, uint8_t request_id) 222 uint8_t *data, uint32_t length, uint8_t request_id)
@@ -246,7 +246,8 @@ int create_request(uint8_t *send_public_key, uint8_t *send_secret_key, uint8_t *
246/* Puts the senders public key in the request in public_key, the data from the request 246/* Puts the senders public key in the request in public_key, the data from the request
247 * in data if a friend or ping request was sent to us and returns the length of the data. 247 * in data if a friend or ping request was sent to us and returns the length of the data.
248 * packet is the request packet and length is its length. 248 * packet is the request packet and length is its length.
249 * return -1 if not valid request. 249 *
250 * return -1 if not valid request.
250 */ 251 */
251static int handle_request(Net_Crypto *c, uint8_t *public_key, uint8_t *data, uint8_t *request_id, uint8_t *packet, 252static int handle_request(Net_Crypto *c, uint8_t *public_key, uint8_t *data, uint8_t *request_id, uint8_t *packet,
252 uint16_t length) 253 uint16_t length)
@@ -341,8 +342,9 @@ static int send_cryptohandshake(Net_Crypto *c, int connection_id, uint8_t *publi
341} 342}
342 343
343/* Extract secret nonce, session public key and public_key from a packet(data) with length length. 344/* Extract secret nonce, session public key and public_key from a packet(data) with length length.
344 * return 1 if successful. 345 *
345 * return 0 if failure. 346 * return 1 if successful.
347 * return 0 if failure.
346 */ 348 */
347static int handle_cryptohandshake(Net_Crypto *c, uint8_t *public_key, uint8_t *secret_nonce, 349static int handle_cryptohandshake(Net_Crypto *c, uint8_t *public_key, uint8_t *secret_nonce,
348 uint8_t *session_key, uint8_t *data, uint16_t length) 350 uint8_t *session_key, uint8_t *data, uint16_t length)
@@ -374,8 +376,9 @@ static int handle_cryptohandshake(Net_Crypto *c, uint8_t *public_key, uint8_t *s
374} 376}
375 377
376/* Get crypto connection id from public key of peer. 378/* Get crypto connection id from public key of peer.
377 * return -1 if there are no connections like we are looking for. 379 *
378 * return id if it found it. 380 * return -1 if there are no connections like we are looking for.
381 * return id if it found it.
379 */ 382 */
380static int getcryptconnection_id(Net_Crypto *c, uint8_t *public_key) 383static int getcryptconnection_id(Net_Crypto *c, uint8_t *public_key)
381{ 384{
@@ -391,7 +394,8 @@ static int getcryptconnection_id(Net_Crypto *c, uint8_t *public_key)
391} 394}
392 395
393/* Set the size of the friend list to numfriends. 396/* Set the size of the friend list to numfriends.
394 * return -1 if realloc fails. 397 *
398 * return -1 if realloc fails.
395 */ 399 */
396int realloc_cryptoconnection(Net_Crypto *c, uint32_t num) 400int realloc_cryptoconnection(Net_Crypto *c, uint32_t num)
397{ 401{
@@ -411,8 +415,9 @@ int realloc_cryptoconnection(Net_Crypto *c, uint32_t num)
411} 415}
412 416
413/* Start a secure connection with other peer who has public_key and ip_port. 417/* Start a secure connection with other peer who has public_key and ip_port.
414 * returns -1 if failure. 418 *
415 * returns crypt_connection_id of the initialized connection if everything went well. 419 * return -1 if failure.
420 * return crypt_connection_id of the initialized connection if everything went well.
416 */ 421 */
417int crypto_connect(Net_Crypto *c, uint8_t *public_key, IP_Port ip_port) 422int crypto_connect(Net_Crypto *c, uint8_t *public_key, IP_Port ip_port)
418{ 423{
@@ -462,8 +467,9 @@ int crypto_connect(Net_Crypto *c, uint8_t *public_key, IP_Port ip_port)
462} 467}
463 468
464/* Handle an incoming connection. 469/* Handle an incoming connection.
465 * return -1 if no crypto inbound connection. 470 *
466 * return incoming connection id (Lossless_UDP one) if there is an incoming crypto connection. 471 * return -1 if no crypto inbound connection.
472 * return incoming connection id (Lossless_UDP one) if there is an incoming crypto connection.
467 * 473 *
468 * Put the public key of the peer in public_key, the secret_nonce from the handshake into secret_nonce 474 * Put the public key of the peer in public_key, the secret_nonce from the handshake into secret_nonce
469 * and the session public key for the connection in session_key. 475 * and the session public key for the connection in session_key.
@@ -500,8 +506,9 @@ int crypto_inbound(Net_Crypto *c, uint8_t *public_key, uint8_t *secret_nonce, ui
500} 506}
501 507
502/* Kill a crypto connection. 508/* Kill a crypto connection.
503 * return 0 if killed successfully. 509 *
504 * return 1 if there was a problem. 510 * return 0 if killed successfully.
511 * return 1 if there was a problem.
505 */ 512 */
506int crypto_kill(Net_Crypto *c, int crypt_connection_id) 513int crypto_kill(Net_Crypto *c, int crypt_connection_id)
507{ 514{
@@ -529,8 +536,9 @@ int crypto_kill(Net_Crypto *c, int crypt_connection_id)
529} 536}
530 537
531/* Accept an incoming connection using the parameters provided by crypto_inbound. 538/* Accept an incoming connection using the parameters provided by crypto_inbound.
532 * return -1 if not successful. 539 *
533 * returns the crypt_connection_id if successful. 540 * return -1 if not successful.
541 * return the crypt_connection_id if successful.
534 */ 542 */
535int accept_crypto_inbound(Net_Crypto *c, int connection_id, uint8_t *public_key, uint8_t *secret_nonce, 543int accept_crypto_inbound(Net_Crypto *c, int connection_id, uint8_t *public_key, uint8_t *secret_nonce,
536 uint8_t *session_key) 544 uint8_t *session_key)
@@ -588,11 +596,11 @@ int accept_crypto_inbound(Net_Crypto *c, int connection_id, uint8_t *public_key,
588 return -1; 596 return -1;
589} 597}
590 598
591/* return 0 if no connection. 599/* return 0 if no connection.
592 * return 1 we have sent a handshake. 600 * return 1 we have sent a handshake.
593 * return 2 if connection is not confirmed yet (we have received a handshake but no empty data packet). 601 * return 2 if connection is not confirmed yet (we have received a handshake but no empty data packet).
594 * return 3 if the connection is established. 602 * return 3 if the connection is established.
595 * return 4 if the connection is timed out and waiting to be killed. 603 * return 4 if the connection is timed out and waiting to be killed.
596 */ 604 */
597int is_cryptoconnected(Net_Crypto *c, int crypt_connection_id) 605int is_cryptoconnected(Net_Crypto *c, int crypt_connection_id)
598{ 606{
@@ -626,9 +634,10 @@ void load_keys(Net_Crypto *c, uint8_t *keys)
626} 634}
627 635
628/* Adds an incoming connection to the incoming_connection list. 636/* Adds an incoming connection to the incoming_connection list.
629 * returns 0 if successful
630 * returns 1 if failure.
631 * TODO: Optimize this. 637 * TODO: Optimize this.
638 *
639 * returns 0 if successful
640 * returns 1 if failure.
632 */ 641 */
633static int new_incoming(Net_Crypto *c, int id) 642static int new_incoming(Net_Crypto *c, int id)
634{ 643{
diff --git a/toxcore/net_crypto.h b/toxcore/net_crypto.h
index 778837f4..030cc678 100644
--- a/toxcore/net_crypto.h
+++ b/toxcore/net_crypto.h
@@ -80,7 +80,8 @@ typedef struct {
80uint8_t crypto_iszero(uint8_t *buffer, uint32_t blen); 80uint8_t crypto_iszero(uint8_t *buffer, uint32_t blen);
81 81
82/* Encrypts plain of length length to encrypted of length + 16 using the 82/* Encrypts plain of length length to encrypted of length + 16 using the
83 * public key(32 bytes) of the receiver and the secret key of the sender and a 24 byte nonce. 83 * public key(32 bytes) of the receiver and the secret key of the sender and a 24 byte nonce.
84 *
84 * return -1 if there was a problem. 85 * return -1 if there was a problem.
85 * return length of encrypted data if everything was fine. 86 * return length of encrypted data if everything was fine.
86 */ 87 */
@@ -89,7 +90,8 @@ int encrypt_data(uint8_t *public_key, uint8_t *secret_key, uint8_t *nonce,
89 90
90 91
91/* Decrypts encrypted of length length to plain of length length - 16 using the 92/* Decrypts encrypted of length length to plain of length length - 16 using the
92 * public key(32 bytes) of the sender, the secret key of the receiver and a 24 byte nonce. 93 * public key(32 bytes) of the sender, the secret key of the receiver and a 24 byte nonce.
94 *
93 * return -1 if there was a problem (decryption failed). 95 * return -1 if there was a problem (decryption failed).
94 * return length of plain data if everything was fine. 96 * return length of plain data if everything was fine.
95 */ 97 */
@@ -113,14 +115,14 @@ int decrypt_data_fast(uint8_t *enc_key, uint8_t *nonce,
113/* Fill the given nonce with random bytes. */ 115/* Fill the given nonce with random bytes. */
114void random_nonce(uint8_t *nonce); 116void random_nonce(uint8_t *nonce);
115 117
116/* return 0 if there is no received data in the buffer. 118/* return 0 if there is no received data in the buffer.
117 * return -1 if the packet was discarded. 119 * return -1 if the packet was discarded.
118 * return length of received data if successful. 120 * return length of received data if successful.
119 */ 121 */
120int read_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data); 122int read_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data);
121 123
122/* return 0 if data could not be put in packet queue 124/* return 0 if data could not be put in packet queue.
123 * return 1 if data was put into the queue 125 * return 1 if data was put into the queue.
124 */ 126 */
125int write_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data, uint32_t length); 127int write_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data, uint32_t length);
126 128
@@ -131,8 +133,8 @@ int write_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data, uin
131 * Data represents the data we send with the request with length being the length of the data. 133 * Data represents the data we send with the request with length being the length of the data.
132 * request_id is the id of the request (32 = friend request, 254 = ping request). 134 * request_id is the id of the request (32 = friend request, 254 = ping request).
133 * 135 *
134 * returns -1 on failure. 136 * return -1 on failure.
135 * returns the length of the created packet on success. 137 * return the length of the created packet on success.
136 */ 138 */
137int create_request(uint8_t *send_public_key, uint8_t *send_secret_key, uint8_t *packet, uint8_t *recv_public_key, 139int create_request(uint8_t *send_public_key, uint8_t *send_secret_key, uint8_t *packet, uint8_t *recv_public_key,
138 uint8_t *data, uint32_t length, uint8_t request_id); 140 uint8_t *data, uint32_t length, uint8_t request_id);
@@ -142,18 +144,21 @@ int create_request(uint8_t *send_public_key, uint8_t *send_secret_key, uint8_t *
142void cryptopacket_registerhandler(Net_Crypto *c, uint8_t byte, cryptopacket_handler_callback cb, void *object); 144void cryptopacket_registerhandler(Net_Crypto *c, uint8_t byte, cryptopacket_handler_callback cb, void *object);
143 145
144/* Start a secure connection with other peer who has public_key and ip_port. 146/* Start a secure connection with other peer who has public_key and ip_port.
145 * returns -1 if failure. 147 *
146 * returns crypt_connection_id of the initialized connection if everything went well. 148 * return -1 if failure.
149 * return crypt_connection_id of the initialized connection if everything went well.
147 */ 150 */
148int crypto_connect(Net_Crypto *c, uint8_t *public_key, IP_Port ip_port); 151int crypto_connect(Net_Crypto *c, uint8_t *public_key, IP_Port ip_port);
149 152
150/* Kill a crypto connection. 153/* Kill a crypto connection.
154 *
151 * return 0 if killed successfully. 155 * return 0 if killed successfully.
152 * return 1 if there was a problem. 156 * return 1 if there was a problem.
153 */ 157 */
154int crypto_kill(Net_Crypto *c, int crypt_connection_id); 158int crypto_kill(Net_Crypto *c, int crypt_connection_id);
155 159
156/* Handle an incoming connection. 160/* Handle an incoming connection.
161 *
157 * return -1 if no crypto inbound connection. 162 * return -1 if no crypto inbound connection.
158 * return incoming connection id (Lossless_UDP one) if there is an incoming crypto connection. 163 * return incoming connection id (Lossless_UDP one) if there is an incoming crypto connection.
159 * 164 *
@@ -165,17 +170,18 @@ int crypto_kill(Net_Crypto *c, int crypt_connection_id);
165int crypto_inbound(Net_Crypto *c, uint8_t *public_key, uint8_t *secret_nonce, uint8_t *session_key); 170int crypto_inbound(Net_Crypto *c, uint8_t *public_key, uint8_t *secret_nonce, uint8_t *session_key);
166 171
167/* Accept an incoming connection using the parameters provided by crypto_inbound. 172/* Accept an incoming connection using the parameters provided by crypto_inbound.
173 *
168 * return -1 if not successful. 174 * return -1 if not successful.
169 * returns the crypt_connection_id if successful. 175 * return crypt_connection_id if successful.
170 */ 176 */
171int accept_crypto_inbound(Net_Crypto *c, int connection_id, uint8_t *public_key, uint8_t *secret_nonce, 177int accept_crypto_inbound(Net_Crypto *c, int connection_id, uint8_t *public_key, uint8_t *secret_nonce,
172 uint8_t *session_key); 178 uint8_t *session_key);
173 179
174/* return 0 if no connection. 180/* return 0 if no connection.
175 * return 1 we have sent a handshake 181 * return 1 we have sent a handshake
176 * return 2 if connexion is not confirmed yet (we have received a handshake but no empty data packet). 182 * return 2 if connexion is not confirmed yet (we have received a handshake but no empty data packet).
177 * return 3 if the connection is established. 183 * return 3 if the connection is established.
178 * return 4 if the connection is timed out and waiting to be killed. 184 * return 4 if the connection is timed out and waiting to be killed.
179 */ 185 */
180int is_cryptoconnected(Net_Crypto *c, int crypt_connection_id); 186int is_cryptoconnected(Net_Crypto *c, int crypt_connection_id);
181 187