summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--toxcore/DHT.c99
-rw-r--r--toxcore/DHT.h43
-rw-r--r--toxcore/LAN_discovery.c9
-rw-r--r--toxcore/LAN_discovery.h2
-rw-r--r--toxcore/Lossless_UDP.c76
-rw-r--r--toxcore/Lossless_UDP.h80
-rw-r--r--toxcore/Messenger.c93
-rw-r--r--toxcore/Messenger.h85
-rw-r--r--toxcore/friend_requests.c12
-rw-r--r--toxcore/misc_tools.h6
-rw-r--r--toxcore/net_crypto.c65
-rw-r--r--toxcore/net_crypto.h36
-rw-r--r--toxcore/network.c8
-rw-r--r--toxcore/network.h14
-rw-r--r--toxcore/tox.c88
-rw-r--r--toxcore/tox.h79
16 files changed, 432 insertions, 363 deletions
diff --git a/toxcore/DHT.c b/toxcore/DHT.c
index 19f8a30c..749eb78c 100644
--- a/toxcore/DHT.c
+++ b/toxcore/DHT.c
@@ -72,10 +72,11 @@ Client_data *DHT_get_close_list(DHT *dht)
72 return dht->close_clientlist; 72 return dht->close_clientlist;
73} 73}
74 74
75/* Compares client_id1 and client_id2 with client_id 75/* Compares client_id1 and client_id2 with client_id.
76 * return 0 if both are same distance 76 *
77 * return 1 if client_id1 is closer 77 * return 0 if both are same distance.
78 * return 2 if client_id2 is closer 78 * return 1 if client_id1 is closer.
79 * return 2 if client_id2 is closer.
79 */ 80 */
80static int id_closest(uint8_t *id, uint8_t *id1, uint8_t *id2) 81static int id_closest(uint8_t *id, uint8_t *id1, uint8_t *id2)
81{ 82{
@@ -128,9 +129,9 @@ static int is_timeout(uint64_t time_now, uint64_t timestamp, uint64_t timeout)
128/* Check if client with client_id is already in list of length length. 129/* Check if client with client_id is already in list of length length.
129 * If it is then set its corresponding timestamp to current time. 130 * If it is then set its corresponding timestamp to current time.
130 * If the id is already in the list with a different ip_port, update it. 131 * If the id is already in the list with a different ip_port, update it.
131 * return True(1) or False(0) 132 * TODO: Maybe optimize this.
132 * 133 *
133 * TODO: maybe optimize this. 134 * return True(1) or False(0)
134 */ 135 */
135static int client_in_list(Client_data *list, uint32_t length, uint8_t *client_id, IP_Port ip_port) 136static int client_in_list(Client_data *list, uint32_t length, uint8_t *client_id, IP_Port ip_port)
136{ 137{
@@ -156,7 +157,9 @@ static int client_in_list(Client_data *list, uint32_t length, uint8_t *client_id
156} 157}
157 158
158/* Check if client with client_id is already in node format list of length length. 159/* Check if client with client_id is already in node format list of length length.
159 * return True(1) or False(0) 160 *
161 * return 1 if true.
162 * return 2 if false.
160 */ 163 */
161static int client_in_nodelist(Node_format *list, uint32_t length, uint8_t *client_id) 164static int client_in_nodelist(Node_format *list, uint32_t length, uint8_t *client_id)
162{ 165{
@@ -170,7 +173,8 @@ static int client_in_nodelist(Node_format *list, uint32_t length, uint8_t *clien
170 return 0; 173 return 0;
171} 174}
172 175
173/* Returns the friend number from the client_id, or -1 if a failure occurs 176/* return friend number from the client_id.
177 * return -1 if a failure occurs.
174 */ 178 */
175static int friend_number(DHT *dht, uint8_t *client_id) 179static int friend_number(DHT *dht, uint8_t *client_id)
176{ 180{
@@ -274,9 +278,10 @@ static int get_close_nodes(DHT *dht, uint8_t *client_id, Node_format *nodes_list
274 return num_nodes; 278 return num_nodes;
275} 279}
276 280
277/* Replace first bad (or empty) node with this one 281/* Replace first bad (or empty) node with this one.
278 * return 0 if successful 282 *
279 * return 1 if not (list contains no bad nodes) 283 * return 0 if successful.
284 * return 1 if not (list contains no bad nodes).
280 */ 285 */
281static int replace_bad( Client_data *list, 286static int replace_bad( Client_data *list,
282 uint32_t length, 287 uint32_t length,
@@ -302,8 +307,8 @@ static int replace_bad( Client_data *list,
302 return 1; 307 return 1;
303} 308}
304 309
305/*Sort the list. It will be sorted from furthest to closest. 310/* Sort the list. It will be sorted from furthest to closest.
306 * Turns list into data that quick sort can use and reverts it back. 311 * Turns list into data that quick sort can use and reverts it back.
307 */ 312 */
308static void sort_list(Client_data *list, uint32_t length, uint8_t *comp_client_id) 313static void sort_list(Client_data *list, uint32_t length, uint8_t *comp_client_id)
309{ 314{
@@ -350,7 +355,7 @@ static int replace_good( Client_data *list,
350} 355}
351 356
352/* Attempt to add client with ip_port and client_id to the friends client list 357/* Attempt to add client with ip_port and client_id to the friends client list
353 * and close_clientlist 358 * and close_clientlist.
354 */ 359 */
355void addto_lists(DHT *dht, IP_Port ip_port, uint8_t *client_id) 360void addto_lists(DHT *dht, IP_Port ip_port, uint8_t *client_id)
356{ 361{
@@ -361,7 +366,7 @@ void addto_lists(DHT *dht, IP_Port ip_port, uint8_t *client_id)
361 */ 366 */
362 if (!client_in_list(dht->close_clientlist, LCLIENT_LIST, client_id, ip_port)) { 367 if (!client_in_list(dht->close_clientlist, LCLIENT_LIST, client_id, ip_port)) {
363 if (replace_bad(dht->close_clientlist, LCLIENT_LIST, client_id, ip_port)) { 368 if (replace_bad(dht->close_clientlist, LCLIENT_LIST, client_id, ip_port)) {
364 /* If we can't replace bad nodes we try replacing good ones */ 369 /* If we can't replace bad nodes we try replacing good ones. */
365 replace_good( dht->close_clientlist, 370 replace_good( dht->close_clientlist,
366 LCLIENT_LIST, 371 LCLIENT_LIST,
367 client_id, 372 client_id,
@@ -801,7 +806,8 @@ void DHT_bootstrap(DHT *dht, IP_Port ip_port, uint8_t *public_key)
801} 806}
802 807
803/* Send the given packet to node with client_id 808/* Send the given packet to node with client_id
804 * returns -1 if failure. 809 *
810 * return -1 if failure.
805 */ 811 */
806int route_packet(DHT *dht, uint8_t *client_id, uint8_t *packet, uint32_t length) 812int route_packet(DHT *dht, uint8_t *client_id, uint8_t *packet, uint32_t length)
807{ 813{
@@ -815,11 +821,12 @@ int route_packet(DHT *dht, uint8_t *client_id, uint8_t *packet, uint32_t length)
815 return -1; 821 return -1;
816} 822}
817 823
818/* Puts all the different ips returned by the nodes for a friend_num into array ip_portlist 824/* Puts all the different ips returned by the nodes for a friend_num into array ip_portlist.
819 * ip_portlist must be at least MAX_FRIEND_CLIENTS big 825 * ip_portlist must be at least MAX_FRIEND_CLIENTS big.
820 * returns the number of ips returned 826 *
821 * return 0 if we are connected to friend or if no ips were found. 827 * return the number of ips returned.
822 * returns -1 if no such friend 828 * return 0 if we are connected to friend or if no ips were found.
829 * return -1 if no such friend.
823 */ 830 */
824static int friend_iplist(DHT *dht, IP_Port *ip_portlist, uint16_t friend_num) 831static int friend_iplist(DHT *dht, IP_Port *ip_portlist, uint16_t friend_num)
825{ 832{
@@ -836,7 +843,7 @@ static int friend_iplist(DHT *dht, IP_Port *ip_portlist, uint16_t friend_num)
836 for (i = 0; i < MAX_FRIEND_CLIENTS; ++i) { 843 for (i = 0; i < MAX_FRIEND_CLIENTS; ++i) {
837 client = &friend->client_list[i]; 844 client = &friend->client_list[i];
838 845
839 /* If ip is not zero and node is good */ 846 /* If ip is not zero and node is good. */
840 if (client->ret_ip_port.ip.uint32 != 0 && !is_timeout(temp_time, client->ret_timestamp, BAD_NODE_TIMEOUT)) { 847 if (client->ret_ip_port.ip.uint32 != 0 && !is_timeout(temp_time, client->ret_timestamp, BAD_NODE_TIMEOUT)) {
841 848
842 if (id_equal(client->client_id, friend->client_id)) 849 if (id_equal(client->client_id, friend->client_id))
@@ -852,9 +859,9 @@ static int friend_iplist(DHT *dht, IP_Port *ip_portlist, uint16_t friend_num)
852 859
853 860
854/* Send the following packet to everyone who tells us they are connected to friend_id. 861/* Send the following packet to everyone who tells us they are connected to friend_id.
855 * returns the number of nodes it sent the packet to.
856 * 862 *
857 * Only works if more than (MAX_FRIEND_CLIENTS / 2) return an ip for friend. 863 * return ip for friend.
864 * return number of nodes the packet was sent to. (Only works if more than (MAX_FRIEND_CLIENTS / 2).
858 */ 865 */
859int route_tofriend(DHT *dht, uint8_t *friend_id, uint8_t *packet, uint32_t length) 866int route_tofriend(DHT *dht, uint8_t *friend_id, uint8_t *packet, uint32_t length)
860{ 867{
@@ -878,7 +885,7 @@ int route_tofriend(DHT *dht, uint8_t *friend_id, uint8_t *packet, uint32_t lengt
878 for (i = 0; i < MAX_FRIEND_CLIENTS; ++i) { 885 for (i = 0; i < MAX_FRIEND_CLIENTS; ++i) {
879 client = &friend->client_list[i]; 886 client = &friend->client_list[i];
880 887
881 /* If ip is not zero and node is good */ 888 /* If ip is not zero and node is good. */
882 if (client->ret_ip_port.ip.uint32 != 0 && !is_timeout(temp_time, client->ret_timestamp, BAD_NODE_TIMEOUT)) { 889 if (client->ret_ip_port.ip.uint32 != 0 && !is_timeout(temp_time, client->ret_timestamp, BAD_NODE_TIMEOUT)) {
883 int retval = sendpacket(dht->c->lossless_udp->net->sock, client->ip_port, packet, length); 890 int retval = sendpacket(dht->c->lossless_udp->net->sock, client->ip_port, packet, length);
884 891
@@ -891,8 +898,9 @@ int route_tofriend(DHT *dht, uint8_t *friend_id, uint8_t *packet, uint32_t lengt
891} 898}
892 899
893/* Send the following packet to one random person who tells us they are connected to friend_id. 900/* Send the following packet to one random person who tells us they are connected to friend_id.
894* returns the number of nodes it sent the packet to 901 *
895*/ 902 * return number of nodes the packet was sent to.
903 */
896static int routeone_tofriend(DHT *dht, uint8_t *friend_id, uint8_t *packet, uint32_t length) 904static int routeone_tofriend(DHT *dht, uint8_t *friend_id, uint8_t *packet, uint32_t length)
897{ 905{
898 int num = friend_number(dht, friend_id); 906 int num = friend_number(dht, friend_id);
@@ -931,9 +939,10 @@ static int routeone_tofriend(DHT *dht, uint8_t *friend_id, uint8_t *packet, uint
931 939
932/* Puts all the different ips returned by the nodes for a friend_id into array ip_portlist. 940/* Puts all the different ips returned by the nodes for a friend_id into array ip_portlist.
933 * ip_portlist must be at least MAX_FRIEND_CLIENTS big. 941 * ip_portlist must be at least MAX_FRIEND_CLIENTS big.
934 * returns the number of ips returned 942 *
935 * return 0 if we are connected to friend or if no ips were found. 943 * return number of ips returned.
936 * returns -1 if no such friend 944 * return 0 if we are connected to friend or if no ips were found.
945 * return -1 if no such friend.
937 */ 946 */
938int friend_ips(DHT *dht, IP_Port *ip_portlist, uint8_t *friend_id) 947int friend_ips(DHT *dht, IP_Port *ip_portlist, uint8_t *friend_id)
939{ 948{
@@ -1014,7 +1023,8 @@ static int handle_NATping(void *object, IP_Port source, uint8_t *source_pubkey,
1014/* Get the most common ip in the ip_portlist. 1023/* Get the most common ip in the ip_portlist.
1015 * Only return ip if it appears in list min_num or more. 1024 * Only return ip if it appears in list min_num or more.
1016 * len must not be bigger than MAX_FRIEND_CLIENTS. 1025 * len must not be bigger than MAX_FRIEND_CLIENTS.
1017 * return ip of 0 if failure. 1026 *
1027 * return ip of 0 if failure.
1018 */ 1028 */
1019static IP NAT_commonip(IP_Port *ip_portlist, uint16_t len, uint16_t min_num) 1029static IP NAT_commonip(IP_Port *ip_portlist, uint16_t len, uint16_t min_num)
1020{ 1030{
@@ -1040,9 +1050,10 @@ static IP NAT_commonip(IP_Port *ip_portlist, uint16_t len, uint16_t min_num)
1040} 1050}
1041 1051
1042/* Return all the ports for one ip in a list. 1052/* Return all the ports for one ip in a list.
1043 * portlist must be at least len long 1053 * portlist must be at least len long,
1044 * where len is the length of ip_portlist 1054 * where len is the length of ip_portlist.
1045 * returns the number of ports and puts the list of ports in portlist. 1055 *
1056 * return number of ports and puts the list of ports in portlist.
1046 */ 1057 */
1047static uint16_t NAT_getports(uint16_t *portlist, IP_Port *ip_portlist, uint16_t len, IP ip) 1058static uint16_t NAT_getports(uint16_t *portlist, IP_Port *ip_portlist, uint16_t len, IP ip)
1048{ 1059{
@@ -1068,7 +1079,7 @@ static void punch_holes(DHT *dht, IP ip, uint16_t *port_list, uint16_t numports,
1068 uint32_t top = dht->friends_list[friend_num].punching_index + MAX_PUNCHING_PORTS; 1079 uint32_t top = dht->friends_list[friend_num].punching_index + MAX_PUNCHING_PORTS;
1069 1080
1070 for (i = dht->friends_list[friend_num].punching_index; i != top; i++) { 1081 for (i = dht->friends_list[friend_num].punching_index; i != top; i++) {
1071 /* TODO: improve port guessing algorithm */ 1082 /* TODO: Improve port guessing algorithm. */
1072 uint16_t port = port_list[(i / 2) % numports] + (i / (2 * numports)) * ((i % 2) ? -1 : 1); 1083 uint16_t port = port_list[(i / 2) % numports] + (i / (2 * numports)) * ((i % 2) ? -1 : 1);
1073 IP_Port pinging = {{ip, htons(port), 0}}; 1084 IP_Port pinging = {{ip, htons(port), 0}};
1074 send_ping_request(dht->ping, dht->c, pinging, dht->friends_list[friend_num].client_id); 1085 send_ping_request(dht->ping, dht->c, pinging, dht->friends_list[friend_num].client_id);
@@ -1086,7 +1097,7 @@ static void do_NAT(DHT *dht)
1086 IP_Port ip_list[MAX_FRIEND_CLIENTS]; 1097 IP_Port ip_list[MAX_FRIEND_CLIENTS];
1087 int num = friend_iplist(dht, ip_list, i); 1098 int num = friend_iplist(dht, ip_list, i);
1088 1099
1089 /* If already connected or friend is not online don't try to hole punch */ 1100 /* If already connected or friend is not online don't try to hole punch. */
1090 if (num < MAX_FRIEND_CLIENTS / 2) 1101 if (num < MAX_FRIEND_CLIENTS / 2)
1091 continue; 1102 continue;
1092 1103
@@ -1124,8 +1135,9 @@ static void do_NAT(DHT *dht)
1124 * If the list is full the nodes farthest from our client_id are replaced. 1135 * If the list is full the nodes farthest from our client_id are replaced.
1125 * The purpose of this list is to enable quick integration of new nodes into the 1136 * The purpose of this list is to enable quick integration of new nodes into the
1126 * network while preventing amplification attacks. 1137 * network while preventing amplification attacks.
1127 * return 0 if node was added. 1138 *
1128 * return -1 if node was not added. 1139 * return 0 if node was added.
1140 * return -1 if node was not added.
1129 */ 1141 */
1130int add_toping(DHT *dht, uint8_t *client_id, IP_Port ip_port) 1142int add_toping(DHT *dht, uint8_t *client_id, IP_Port ip_port)
1131{ 1143{
@@ -1156,7 +1168,7 @@ int add_toping(DHT *dht, uint8_t *client_id, IP_Port ip_port)
1156} 1168}
1157 1169
1158/* Ping all the valid nodes in the toping list every TIME_TOPING seconds. 1170/* Ping all the valid nodes in the toping list every TIME_TOPING seconds.
1159 * This function must be run at least once every TIME_TOPING seconds 1171 * This function must be run at least once every TIME_TOPING seconds.
1160 */ 1172 */
1161static void do_toping(DHT *dht) 1173static void do_toping(DHT *dht)
1162{ 1174{
@@ -1233,8 +1245,9 @@ void DHT_save(DHT *dht, uint8_t *data)
1233} 1245}
1234 1246
1235/* Load the DHT from data of size size. 1247/* Load the DHT from data of size size.
1236 * return -1 if failure. 1248 *
1237 * return 0 if success. 1249 * return -1 if failure.
1250 * return 0 if success.
1238 */ 1251 */
1239int DHT_load(DHT *dht, uint8_t *data, uint32_t size) 1252int DHT_load(DHT *dht, uint8_t *data, uint32_t size)
1240{ 1253{
@@ -1278,8 +1291,8 @@ int DHT_load(DHT *dht, uint8_t *data, uint32_t size)
1278 return 0; 1291 return 0;
1279} 1292}
1280 1293
1281/* returns 0 if we are not connected to the DHT. 1294/* return 0 if we are not connected to the DHT.
1282 * returns 1 if we are. 1295 * return 1 if we are.
1283 */ 1296 */
1284int DHT_isconnected(DHT *dht) 1297int DHT_isconnected(DHT *dht)
1285{ 1298{
diff --git a/toxcore/DHT.h b/toxcore/DHT.h
index 23a664bb..e5122b5e 100644
--- a/toxcore/DHT.h
+++ b/toxcore/DHT.h
@@ -104,16 +104,18 @@ typedef struct {
104Client_data *DHT_get_close_list(DHT *dht); 104Client_data *DHT_get_close_list(DHT *dht);
105 105
106/* Add a new friend to the friends list. 106/* Add a new friend to the friends list.
107 * client_id must be CLIENT_ID_SIZE bytes long. 107 * client_id must be CLIENT_ID_SIZE bytes long.
108 * returns 0 if success. 108 *
109 * returns 1 if failure (friends list is full). 109 * return 0 if success.
110 * return 1 if failure (friends list is full).
110 */ 111 */
111int DHT_addfriend(DHT *dht, uint8_t *client_id); 112int DHT_addfriend(DHT *dht, uint8_t *client_id);
112 113
113/* Delete a friend from the friends list. 114/* Delete a friend from the friends list.
114 * client_id must be CLIENT_ID_SIZE bytes long. 115 * client_id must be CLIENT_ID_SIZE bytes long.
115 * returns 0 if success. 116 *
116 * returns 1 if failure (client_id not in friends list). 117 * return 0 if success.
118 * return 1 if failure (client_id not in friends list).
117 */ 119 */
118int DHT_delfriend(DHT *dht, uint8_t *client_id); 120int DHT_delfriend(DHT *dht, uint8_t *client_id);
119 121
@@ -121,9 +123,10 @@ int DHT_delfriend(DHT *dht, uint8_t *client_id);
121 * client_id must be CLIENT_ID_SIZE bytes long. 123 * client_id must be CLIENT_ID_SIZE bytes long.
122 * ip must be 4 bytes long. 124 * ip must be 4 bytes long.
123 * port must be 2 bytes long. 125 * port must be 2 bytes long.
124 * returns ip if success. 126 *
125 * returns ip of 0 if failure (This means the friend is either offline or we have not found him yet). 127 * return ip if success.
126 * returns ip of 1 if friend is not in list. 128 * return ip of 0 if failure (This means the friend is either offline or we have not found him yet).
129 * return ip of 1 if friend is not in list.
127 */ 130 */
128IP_Port DHT_getfriendip(DHT *dht, uint8_t *client_id); 131IP_Port DHT_getfriendip(DHT *dht, uint8_t *client_id);
129 132
@@ -141,27 +144,32 @@ void DHT_bootstrap(DHT *dht, IP_Port ip_port, uint8_t *public_key);
141 * If the list is full the nodes farthest from our client_id are replaced. 144 * If the list is full the nodes farthest from our client_id are replaced.
142 * The purpose of this list is to enable quick integration of new nodes into the 145 * The purpose of this list is to enable quick integration of new nodes into the
143 * network while preventing amplification attacks. 146 * network while preventing amplification attacks.
144 * return 0 if node was added. 147 *
145 * return -1 if node was not added. 148 * return 0 if node was added.
149 * return -1 if node was not added.
146 */ 150 */
147int add_toping(DHT *dht, uint8_t *client_id, IP_Port ip_port); 151int add_toping(DHT *dht, uint8_t *client_id, IP_Port ip_port);
148 152
149/* ROUTING FUNCTIONS */ 153/* ROUTING FUNCTIONS */
150 154
151/* Send the given packet to node with client_id. 155/* Send the given packet to node with client_id.
152 returns -1 if failure. */ 156 *
157 * return -1 if failure.
158 */
153int route_packet(DHT *dht, uint8_t *client_id, uint8_t *packet, uint32_t length); 159int route_packet(DHT *dht, uint8_t *client_id, uint8_t *packet, uint32_t length);
154 160
155/* Send the following packet to everyone who tells us they are connected to friend_id. 161/* Send the following packet to everyone who tells us they are connected to friend_id.
156 * returns the number of nodes it sent the packet to. 162 *
163 * return number of nodes it sent the packet to.
157 */ 164 */
158int route_tofriend(DHT *dht, uint8_t *friend_id, uint8_t *packet, uint32_t length); 165int route_tofriend(DHT *dht, uint8_t *friend_id, uint8_t *packet, uint32_t length);
159 166
160/* NAT PUNCHING FUNCTIONS */ 167/* NAT PUNCHING FUNCTIONS */
161 168
162/* Puts all the different ips returned by the nodes for a friend_id into array ip_portlist. 169/* Puts all the different ips returned by the nodes for a friend_id into array ip_portlist.
163 * ip_portlist must be at least MAX_FRIEND_CLIENTS big. 170 * ip_portlist must be at least MAX_FRIEND_CLIENTS big.
164 * returns the number of ips returned. 171 *
172 * returns number of ips returned.
165 * returns -1 if no such friend. 173 * returns -1 if no such friend.
166 */ 174 */
167int friend_ips(DHT *dht, IP_Port *ip_portlist, uint8_t *friend_id); 175int friend_ips(DHT *dht, IP_Port *ip_portlist, uint8_t *friend_id);
@@ -180,13 +188,14 @@ DHT *new_DHT(Net_Crypto *c);
180void kill_DHT(DHT *dht); 188void kill_DHT(DHT *dht);
181 189
182/* Load the DHT from data of size size. 190/* Load the DHT from data of size size.
191 *
183 * return -1 if failure. 192 * return -1 if failure.
184 * return 0 if success. 193 * return 0 if success.
185 */ 194 */
186int DHT_load(DHT *dht, uint8_t *data, uint32_t size); 195int DHT_load(DHT *dht, uint8_t *data, uint32_t size);
187 196
188/* returns 0 if we are not connected to the DHT 197/* return 0 if we are not connected to the DHT.
189 * returns 1 if we are 198 * return 1 if we are.
190 */ 199 */
191int DHT_isconnected(DHT *dht); 200int DHT_isconnected(DHT *dht);
192 201
diff --git a/toxcore/LAN_discovery.c b/toxcore/LAN_discovery.c
index 4662bc8e..8998e0a9 100644
--- a/toxcore/LAN_discovery.c
+++ b/toxcore/LAN_discovery.c
@@ -27,8 +27,9 @@
27 27
28#ifdef __linux 28#ifdef __linux
29/* Get the first working broadcast address that's not from "lo". 29/* Get the first working broadcast address that's not from "lo".
30 * returns higher than 0 on success. 30 *
31 * returns 0 on error. 31 * return higher than 0 on success.
32 * return 0 on error.
32 */ 33 */
33static uint32_t get_broadcast(void) 34static uint32_t get_broadcast(void)
34{ 35{
@@ -100,8 +101,8 @@ static IP broadcast_ip(void)
100 return ip; 101 return ip;
101} 102}
102 103
103/* return 0 if ip is a LAN ip. 104/* return 0 if ip is a LAN ip.
104 * return -1 if it is not. 105 * return -1 if it is not.
105 */ 106 */
106static int LAN_ip(IP ip) 107static int LAN_ip(IP ip)
107{ 108{
diff --git a/toxcore/LAN_discovery.h b/toxcore/LAN_discovery.h
index 7bbe775e..78990936 100644
--- a/toxcore/LAN_discovery.h
+++ b/toxcore/LAN_discovery.h
@@ -28,7 +28,7 @@
28 28
29#include "DHT.h" 29#include "DHT.h"
30 30
31/* used for get_broadcast(). */ 31/* Used for get_broadcast(). */
32#ifdef __linux 32#ifdef __linux
33#include <sys/ioctl.h> 33#include <sys/ioctl.h>
34#include <arpa/inet.h> 34#include <arpa/inet.h>
diff --git a/toxcore/Lossless_UDP.c b/toxcore/Lossless_UDP.c
index 28a50ed9..0d6d8694 100644
--- a/toxcore/Lossless_UDP.c
+++ b/toxcore/Lossless_UDP.c
@@ -33,6 +33,7 @@
33 33
34/* 34/*
35 * Get connection id from IP_Port. 35 * Get connection id from IP_Port.
36 *
36 * return -1 if there are no connections like we are looking for. 37 * return -1 if there are no connections like we are looking for.
37 * return id if it found it. 38 * return id if it found it.
38 */ 39 */
@@ -87,8 +88,9 @@ static void change_handshake(Lossless_UDP *ludp, IP_Port source)
87 88
88/* 89/*
89 * Initialize a new connection to ip_port 90 * Initialize a new connection to ip_port
90 * Returns an integer corresponding to the connection id. 91 *
91 * Return -1 if it could not initialize the connectiont 92 * return an integer corresponding to the connection id.
93 * return -1 if it could not initialize the connectiont
92 * If there already was an existing connection to that ip_port return its number. 94 * If there already was an existing connection to that ip_port return its number.
93 */ 95 */
94int new_connection(Lossless_UDP *ludp, IP_Port ip_port) 96int new_connection(Lossless_UDP *ludp, IP_Port ip_port)
@@ -141,8 +143,9 @@ int new_connection(Lossless_UDP *ludp, IP_Port ip_port)
141 143
142/* 144/*
143 * Initialize a new inbound connection from ip_port. 145 * Initialize a new inbound connection from ip_port.
144 * return an integer corresponding to the connection id. 146 *
145 * return -1 if it could not initialize the connection. 147 * return an integer corresponding to the connection id.
148 * return -1 if it could not initialize the connection.
146 */ 149 */
147static int new_inconnection(Lossless_UDP *ludp, IP_Port ip_port) 150static int new_inconnection(Lossless_UDP *ludp, IP_Port ip_port)
148{ 151{
@@ -179,10 +182,10 @@ static int new_inconnection(Lossless_UDP *ludp, IP_Port ip_port)
179 .last_sent = current_time(), 182 .last_sent = current_time(),
180 .send_counter = 127, 183 .send_counter = 127,
181 184
182 /* add randomness to timeout to prevent connections getting stuck in a loop. */ 185 /* Add randomness to timeout to prevent connections getting stuck in a loop. */
183 .timeout = timeout, 186 .timeout = timeout,
184 187
185 /* if this connection isn't handled within the timeout kill it. */ 188 /* If this connection isn't handled within the timeout kill it. */
186 .killat = current_time() + 1000000UL * timeout 189 .killat = current_time() + 1000000UL * timeout
187 }; 190 };
188 191
@@ -190,8 +193,8 @@ static int new_inconnection(Lossless_UDP *ludp, IP_Port ip_port)
190} 193}
191 194
192/* 195/*
193 * return an integer corresponding to the next connection in our incoming connection list. 196 * return an integer corresponding to the next connection in our incoming connection list.
194 * return -1 if there are no new incoming connections in the list. 197 * return -1 if there are no new incoming connections in the list.
195 */ 198 */
196int incoming_connection(Lossless_UDP *ludp) 199int incoming_connection(Lossless_UDP *ludp)
197{ 200{
@@ -220,9 +223,8 @@ static void free_connections(Lossless_UDP *ludp)
220 223
221 return tox_array_pop(&ludp->connections, ludp->connections.len - i); 224 return tox_array_pop(&ludp->connections, ludp->connections.len - i);
222} 225}
223/* 226/* return -1 if it could not kill the connection.
224 * return -1 if it could not kill the connection. 227 * return 0 if killed successfully.
225 * return 0 if killed successfully.
226 */ 228 */
227int kill_connection(Lossless_UDP *ludp, int connection_id) 229int kill_connection(Lossless_UDP *ludp, int connection_id)
228{ 230{
@@ -243,8 +245,9 @@ int kill_connection(Lossless_UDP *ludp, int connection_id)
243 245
244/* 246/*
245 * Kill connection in seconds. 247 * Kill connection in seconds.
246 * return -1 if it can not kill the connection. 248 *
247 * return 0 if it will kill it. 249 * return -1 if it can not kill the connection.
250 * return 0 if it will kill it.
248 */ 251 */
249int kill_connection_in(Lossless_UDP *ludp, int connection_id, uint32_t seconds) 252int kill_connection_in(Lossless_UDP *ludp, int connection_id, uint32_t seconds)
250{ 253{
@@ -262,11 +265,12 @@ int kill_connection_in(Lossless_UDP *ludp, int connection_id, uint32_t seconds)
262 265
263/* 266/*
264 * Check if connection is connected: 267 * Check if connection is connected:
265 * return 0 no. 268 *
266 * return 1 if attempting handshake. 269 * return 0 if not.
267 * return 2 if handshake is done. 270 * return 1 if attempting handshake.
268 * return 3 if fully connected. 271 * return 2 if handshake is done.
269 * return 4 if timed out and waiting to be killed. 272 * return 3 if fully connected.
273 * return 4 if timed out and waiting to be killed.
270 */ 274 */
271int is_connected(Lossless_UDP *ludp, int connection_id) 275int is_connected(Lossless_UDP *ludp, int connection_id)
272{ 276{
@@ -276,7 +280,7 @@ int is_connected(Lossless_UDP *ludp, int connection_id)
276 return 0; 280 return 0;
277} 281}
278 282
279/* return the ip_port of the corresponding connection. */ 283/* return the ip_port of the corresponding connection. */
280IP_Port connection_ip(Lossless_UDP *ludp, int connection_id) 284IP_Port connection_ip(Lossless_UDP *ludp, int connection_id)
281{ 285{
282 if ((unsigned int)connection_id < ludp->connections.len) 286 if ((unsigned int)connection_id < ludp->connections.len)
@@ -286,7 +290,7 @@ IP_Port connection_ip(Lossless_UDP *ludp, int connection_id)
286 return zero; 290 return zero;
287} 291}
288 292
289/* returns the number of packets in the queue waiting to be successfully sent. */ 293/* return the number of packets in the queue waiting to be successfully sent. */
290uint32_t sendqueue(Lossless_UDP *ludp, int connection_id) 294uint32_t sendqueue(Lossless_UDP *ludp, int connection_id)
291{ 295{
292 if ((unsigned int)connection_id >= ludp->connections.len) 296 if ((unsigned int)connection_id >= ludp->connections.len)
@@ -296,7 +300,7 @@ uint32_t sendqueue(Lossless_UDP *ludp, int connection_id)
296 return connection->sendbuff_packetnum - connection->successful_sent; 300 return connection->sendbuff_packetnum - connection->successful_sent;
297} 301}
298 302
299/* returns the number of packets in the queue waiting to be successfully read with read_packet(...). */ 303/* return the number of packets in the queue waiting to be successfully read with read_packet(...). */
300uint32_t recvqueue(Lossless_UDP *ludp, int connection_id) 304uint32_t recvqueue(Lossless_UDP *ludp, int connection_id)
301{ 305{
302 if ((unsigned int)connection_id >= ludp->connections.len) 306 if ((unsigned int)connection_id >= ludp->connections.len)
@@ -306,8 +310,8 @@ uint32_t recvqueue(Lossless_UDP *ludp, int connection_id)
306 return connection->recv_packetnum - connection->successful_read; 310 return connection->recv_packetnum - connection->successful_read;
307} 311}
308 312
309/* returns the id of the next packet in the queue. 313/* return the id of the next packet in the queue.
310 * return -1 if no packet in queue. 314 * return -1 if no packet in queue.
311 */ 315 */
312char id_packet(Lossless_UDP *ludp, int connection_id) 316char id_packet(Lossless_UDP *ludp, int connection_id)
313{ 317{
@@ -322,8 +326,8 @@ char id_packet(Lossless_UDP *ludp, int connection_id)
322 return -1; 326 return -1;
323} 327}
324 328
325/* return 0 if there is no received data in the buffer. 329/* return 0 if there is no received data in the buffer.
326 * return length of received packet if successful. 330 * return length of received packet if successful.
327 */ 331 */
328int read_packet(Lossless_UDP *ludp, int connection_id, uint8_t *data) 332int read_packet(Lossless_UDP *ludp, int connection_id, uint8_t *data)
329{ 333{
@@ -340,9 +344,8 @@ int read_packet(Lossless_UDP *ludp, int connection_id, uint8_t *data)
340 344
341} 345}
342 346
343/* 347/* return 0 if data could not be put in packet queue.
344 * return 0 if data could not be put in packet queue. 348 * return 1 if data was put into the queue.
345 * return 1 if data was put into the queue.
346 */ 349 */
347int write_packet(Lossless_UDP *ludp, int connection_id, uint8_t *data, uint32_t length) 350int write_packet(Lossless_UDP *ludp, int connection_id, uint8_t *data, uint32_t length)
348{ 351{
@@ -360,7 +363,7 @@ int write_packet(Lossless_UDP *ludp, int connection_id, uint8_t *data, uint32_t
360/* Put the packet numbers the we are missing in requested and return the number. */ 363/* Put the packet numbers the we are missing in requested and return the number. */
361uint32_t missing_packets(Lossless_UDP *ludp, int connection_id, uint32_t *requested) 364uint32_t missing_packets(Lossless_UDP *ludp, int connection_id, uint32_t *requested)
362{ 365{
363 /* don't request packets if the buffer is full. */ 366 /* Don't request packets if the buffer is full. */
364 if (recvqueue(ludp, connection_id) >= (BUFFER_PACKET_NUM - 1)) 367 if (recvqueue(ludp, connection_id) >= (BUFFER_PACKET_NUM - 1))
365 return 0; 368 return 0;
366 369
@@ -448,7 +451,7 @@ static int send_data_packet(Lossless_UDP *ludp, int connection_id, uint32_t pack
448 return sendpacket(ludp->net->sock, connection->ip_port, packet, 1 + 4 + connection->sendbuffer[index].size); 451 return sendpacket(ludp->net->sock, connection->ip_port, packet, 1 + 4 + connection->sendbuffer[index].size);
449} 452}
450 453
451/* sends 1 data packet */ 454/* Sends 1 data packet. */
452static int send_DATA(Lossless_UDP *ludp, int connection_id) 455static int send_DATA(Lossless_UDP *ludp, int connection_id)
453{ 456{
454 Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection); 457 Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection);
@@ -481,8 +484,8 @@ static int send_DATA(Lossless_UDP *ludp, int connection_id)
481 */ 484 */
482 485
483 486
484/* return 0 if handled correctly. 487/* return 0 if handled correctly.
485 * return 1 if packet is bad. 488 * return 1 if packet is bad.
486 */ 489 */
487static int handle_handshake(void *object, IP_Port source, uint8_t *packet, uint32_t length) 490static int handle_handshake(void *object, IP_Port source, uint8_t *packet, uint32_t length)
488{ 491{
@@ -525,7 +528,9 @@ static int handle_handshake(void *object, IP_Port source, uint8_t *packet, uint3
525 return 0; 528 return 0;
526} 529}
527 530
528/* returns 1 if sync packet is valid 0 if not. */ 531/* return 1 if sync packet is valid.
532 * return 0 if not.
533 */
529static int SYNC_valid(uint32_t length) 534static int SYNC_valid(uint32_t length)
530{ 535{
531 if (length < 4 + 4 + 2) 536 if (length < 4 + 4 + 2)
@@ -660,8 +665,9 @@ static int handle_SYNC(void *object, IP_Port source, uint8_t *packet, uint32_t l
660/* 665/*
661 * Add a packet to the received buffer and set the recv_packetnum of the 666 * Add a packet to the received buffer and set the recv_packetnum of the
662 * connection to its proper value. 667 * connection to its proper value.
663 * return 1 if data was too big. 668 *
664 * return 0 if not. 669 * return 1 if data was too big.
670 * return 0 if not.
665 */ 671 */
666static int add_recv(Lossless_UDP *ludp, int connection_id, uint32_t data_num, uint8_t *data, uint16_t size) 672static int add_recv(Lossless_UDP *ludp, int connection_id, uint32_t data_num, uint8_t *data, uint16_t size)
667{ 673{
diff --git a/toxcore/Lossless_UDP.h b/toxcore/Lossless_UDP.h
index c1db9b6f..20471b0a 100644
--- a/toxcore/Lossless_UDP.h
+++ b/toxcore/Lossless_UDP.h
@@ -55,18 +55,18 @@ typedef struct {
55 IP_Port ip_port; 55 IP_Port ip_port;
56 56
57 /* 57 /*
58 * return 0 if connection is dead. 58 * return 0 if connection is dead.
59 * return 1 if attempting handshake. 59 * return 1 if attempting handshake.
60 * return 2 if handshake is done (we start sending SYNC packets). 60 * return 2 if handshake is done (we start sending SYNC packets).
61 * return 3 if we are sending SYNC packets and can send data. 61 * return 3 if we are sending SYNC packets and can send data.
62 * return 4 if the connection has timed out. 62 * return 4 if the connection has timed out.
63 */ 63 */
64 uint8_t status; 64 uint8_t status;
65 65
66 /* 66 /*
67 * return 0 if connection was not initiated by someone else. 67 * return 0 if connection was not initiated by someone else.
68 * return 1 if incoming_connection() has returned. 68 * return 1 if incoming_connection() has returned.
69 * return 2 if it has not. 69 * return 2 if it has not.
70 */ 70 */
71 uint8_t inbound; 71 uint8_t inbound;
72 72
@@ -129,77 +129,75 @@ typedef struct {
129 129
130/* 130/*
131 * Initialize a new connection to ip_port. 131 * Initialize a new connection to ip_port.
132 * return an integer corresponding to the connection id. 132 *
133 * return -1 if it could not initialize the connection. 133 * return an integer corresponding to the connection id.
134 * return number if there already was an existing connection to that ip_port. 134 * return -1 if it could not initialize the connection.
135 * return number if there already was an existing connection to that ip_port.
135 */ 136 */
136int new_connection(Lossless_UDP *ludp, IP_Port ip_port); 137int new_connection(Lossless_UDP *ludp, IP_Port ip_port);
137 138
138/* 139/*
139 * Get connection id from IP_Port. 140 * Get connection id from IP_Port.
140 * return -1 if there are no connections like we are looking for. 141 *
141 * return id if it found it . 142 * return -1 if there are no connections like we are looking for.
143 * return id if it found it .
142 */ 144 */
143int getconnection_id(Lossless_UDP *ludp, IP_Port ip_port); 145int getconnection_id(Lossless_UDP *ludp, IP_Port ip_port);
144 146
145/* 147/* return an integer corresponding to the next connection in our imcoming connection list.
146 * return an integer corresponding to the next connection in our imcoming connection list. 148 * return -1 if there are no new incoming connections in the list.
147 * return -1 if there are no new incoming connections in the list.
148 */ 149 */
149int incoming_connection(Lossless_UDP *ludp); 150int incoming_connection(Lossless_UDP *ludp);
150 151
151/* 152/* return -1 if it could not kill the connection.
152 * return -1 if it could not kill the connection. 153 * return 0 if killed successfully.
153 * return 0 if killed successfully.
154 */ 154 */
155int kill_connection(Lossless_UDP *ludp, int connection_id); 155int kill_connection(Lossless_UDP *ludp, int connection_id);
156 156
157/* 157/*
158 * Kill connection in seconds seconds. 158 * Kill connection in seconds seconds.
159 * return -1 if it can not kill the connection. 159 *
160 * return 0 if it will kill it. 160 * return -1 if it can not kill the connection.
161 * return 0 if it will kill it.
161 */ 162 */
162int kill_connection_in(Lossless_UDP *ludp, int connection_id, uint32_t seconds); 163int kill_connection_in(Lossless_UDP *ludp, int connection_id, uint32_t seconds);
163 164
164/* 165/* returns the ip_port of the corresponding connection.
165 * returns the ip_port of the corresponding connection. 166 * return 0 if there is no such connection.
166 * return 0 if there is no such connection.
167 */ 167 */
168IP_Port connection_ip(Lossless_UDP *ludp, int connection_id); 168IP_Port connection_ip(Lossless_UDP *ludp, int connection_id);
169 169
170/* 170/* returns the id of the next packet in the queue.
171 * returns the id of the next packet in the queue. 171 * return -1 if no packet in queue.
172 * return -1 if no packet in queue.
173 */ 172 */
174char id_packet(Lossless_UDP *ludp, int connection_id); 173char id_packet(Lossless_UDP *ludp, int connection_id);
175 174
176/* 175/* return 0 if there is no received data in the buffer.
177 * return 0 if there is no received data in the buffer. 176 * return length of received packet if successful.
178 * return length of received packet if successful.
179 */ 177 */
180int read_packet(Lossless_UDP *ludp, int connection_id, uint8_t *data); 178int read_packet(Lossless_UDP *ludp, int connection_id, uint8_t *data);
181 179
182/* 180/* return 0 if data could not be put in packet queue.
183 * return 0 if data could not be put in packet queue. 181 * return 1 if data was put into the queue.
184 * return 1 if data was put into the queue.
185 */ 182 */
186int write_packet(Lossless_UDP *ludp, int connection_id, uint8_t *data, uint32_t length); 183int write_packet(Lossless_UDP *ludp, int connection_id, uint8_t *data, uint32_t length);
187 184
188/* returns the number of packets in the queue waiting to be successfully sent. */ 185/* return number of packets in the queue waiting to be successfully sent. */
189uint32_t sendqueue(Lossless_UDP *ludp, int connection_id); 186uint32_t sendqueue(Lossless_UDP *ludp, int connection_id);
190 187
191/* 188/*
192 * return the number of packets in the queue waiting to be successfully 189 * return number of packets in the queue waiting to be successfully
193 * read with read_packet(...). 190 * read with read_packet(...).
194 */ 191 */
195uint32_t recvqueue(Lossless_UDP *ludp, int connection_id); 192uint32_t recvqueue(Lossless_UDP *ludp, int connection_id);
196 193
197/* Check if connection is connected: 194/* Check if connection is connected:
198 * return 0 not. 195 *
199 * return 1 if attempting handshake. 196 * return 0 not.
200 * return 2 if handshake is done. 197 * return 1 if attempting handshake.
201 * return 3 if fully connected. 198 * return 2 if handshake is done.
202 * return 4 if timed out and wating to be killed. 199 * return 3 if fully connected.
200 * return 4 if timed out and wating to be killed.
203 */ 201 */
204int is_connected(Lossless_UDP *ludp, int connection_id); 202int is_connected(Lossless_UDP *ludp, int connection_id);
205 203
diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c
index 2bbeed41..621f7130 100644
--- a/toxcore/Messenger.c
+++ b/toxcore/Messenger.c
@@ -34,13 +34,9 @@ static uint8_t friend_not_valid(Messenger *m, int friendnumber)
34 return (unsigned int)friendnumber >= m->numfriends; 34 return (unsigned int)friendnumber >= m->numfriends;
35} 35}
36 36
37/* return 1 if we are online.
38 * return 0 if we are offline.
39 * static uint8_t online;
40 */
41
42/* Set the size of the friend list to numfriends. 37/* Set the size of the friend list to numfriends.
43 * return -1 if realloc fails. 38 *
39 * return -1 if realloc fails.
44 */ 40 */
45int realloc_friendlist(Messenger *m, uint32_t num) 41int realloc_friendlist(Messenger *m, uint32_t num)
46{ 42{
@@ -59,8 +55,8 @@ int realloc_friendlist(Messenger *m, uint32_t num)
59 return 0; 55 return 0;
60} 56}
61 57
62/* return the friend id associated to that public key. 58/* return the friend id associated to that public key.
63 * return -1 if no such friend. 59 * return -1 if no such friend.
64 */ 60 */
65int getfriend_id(Messenger *m, uint8_t *client_id) 61int getfriend_id(Messenger *m, uint8_t *client_id)
66{ 62{
@@ -77,8 +73,9 @@ int getfriend_id(Messenger *m, uint8_t *client_id)
77 73
78/* 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.
79 * Make sure that client_id is of size CLIENT_ID_SIZE. 75 * Make sure that client_id is of size CLIENT_ID_SIZE.
80 * return 0 if success. 76 *
81 * return -1 if failure. 77 * return 0 if success.
78 * return -1 if failure.
82 */ 79 */
83int getclient_id(Messenger *m, int friend_id, uint8_t *client_id) 80int getclient_id(Messenger *m, int friend_id, uint8_t *client_id)
84{ 81{
@@ -92,10 +89,9 @@ int getclient_id(Messenger *m, int friend_id, uint8_t *client_id)
92 89
93 return -1; 90 return -1;
94} 91}
95/* 92/* TODO: Another checksum algorithm might be better.
96 * return a uint16_t that represents the checksum of address of length len.
97 * 93 *
98 * TODO: Another checksum algorithm might be better. 94 * return a uint16_t that represents the checksum of address of length len.
99 */ 95 */
100static uint16_t address_checksum(uint8_t *address, uint32_t len) 96static uint16_t address_checksum(uint8_t *address, uint32_t len)
101{ 97{
@@ -110,10 +106,9 @@ static uint16_t address_checksum(uint8_t *address, uint32_t len)
110 return check; 106 return check;
111} 107}
112 108
113/* 109/* Format: [client_id (32 bytes)][nospam number (4 bytes)][checksum (2 bytes)]
114 * return FRIEND_ADDRESS_SIZE byte address to give to others.
115 * Format: [client_id (32 bytes)][nospam number (4 bytes)][checksum (2 bytes)]
116 * 110 *
111 * return FRIEND_ADDRESS_SIZE byte address to give to others.
117 */ 112 */
118void getaddress(Messenger *m, uint8_t *address) 113void getaddress(Messenger *m, uint8_t *address)
119{ 114{
@@ -130,16 +125,17 @@ void getaddress(Messenger *m, uint8_t *address)
130 * 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.
131 * TODO: add checksum. 126 * TODO: add checksum.
132 * data is the data and length is the length. 127 * data is the data and length is the length.
133 * return the friend number if success. 128 *
134 * return FA_TOOLONG if message length is too long. 129 * return the friend number if success.
135 * return FAERR_NOMESSAGE if no message (message length must be >= 1 byte). 130 * return FA_TOOLONG if message length is too long.
136 * return FAERR_OWNKEY if user's own key. 131 * return FAERR_NOMESSAGE if no message (message length must be >= 1 byte).
137 * return FAERR_ALREADYSENT if friend request already sent or already a friend. 132 * return FAERR_OWNKEY if user's own key.
138 * return FAERR_UNKNOWN for unknown error. 133 * return FAERR_ALREADYSENT if friend request already sent or already a friend.
139 * return FAERR_BADCHECKSUM if bad checksum in address. 134 * return FAERR_UNKNOWN for unknown error.
140 * return FAERR_SETNEWNOSPAM if the friend was already there but the nospam was different. 135 * return FAERR_BADCHECKSUM if bad checksum in address.
141 * (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.
142 * 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.
143 */ 139 */
144int 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)
145{ 141{
@@ -247,6 +243,7 @@ int m_addfriend_norequest(Messenger *m, uint8_t *client_id)
247} 243}
248 244
249/* Remove a friend. 245/* Remove a friend.
246 *
250 * return 0 if success. 247 * return 0 if success.
251 * return -1 if failure. 248 * return -1 if failure.
252 */ 249 */
@@ -274,11 +271,11 @@ int m_delfriend(Messenger *m, int friendnumber)
274 return 0; 271 return 0;
275} 272}
276 273
277/* return FRIEND_ONLINE if friend is online. 274/* return FRIEND_ONLINE if friend is online.
278 * return FRIEND_CONFIRMED if friend is confirmed. 275 * return FRIEND_CONFIRMED if friend is confirmed.
279 * return FRIEND_REQUESTED if the friend request was sent. 276 * return FRIEND_REQUESTED if the friend request was sent.
280 * return FRIEND_ADDED if the friend was added. 277 * return FRIEND_ADDED if the friend was added.
281 * return NOFRIEND if there is no friend with that number. 278 * return NOFRIEND if there is no friend with that number.
282 */ 279 */
283int m_friendstatus(Messenger *m, int friendnumber) 280int m_friendstatus(Messenger *m, int friendnumber)
284{ 281{
@@ -289,8 +286,9 @@ int m_friendstatus(Messenger *m, int friendnumber)
289} 286}
290 287
291/* Send a text chat message to an online friend. 288/* Send a text chat message to an online friend.
292 * return the message id if packet was successfully put into the send queue. 289 *
293 * 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.
294 */ 292 */
295uint32_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)
296{ 294{
@@ -322,8 +320,9 @@ uint32_t m_sendmessage_withid(Messenger *m, int friendnumber, uint32_t theid, ui
322} 320}
323 321
324/* Send an action to an online friend. 322/* Send an action to an online friend.
325 * return 1 if packet was successfully put into the send queue. 323 *
326 * 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.
327 */ 326 */
328int 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)
329{ 328{
@@ -342,8 +341,9 @@ static int m_sendname(Messenger *m, int friendnumber, uint8_t *name, uint16_t le
342} 341}
343 342
344/* Set the name of a friend. 343/* Set the name of a friend.
345 * return 0 if success. 344 *
346 * return -1 if failure. 345 * return 0 if success.
346 * return -1 if failure.
347 */ 347 */
348static int setfriendname(Messenger *m, int friendnumber, uint8_t *name) 348static int setfriendname(Messenger *m, int friendnumber, uint8_t *name)
349{ 349{
@@ -358,8 +358,9 @@ static int setfriendname(Messenger *m, int friendnumber, uint8_t *name)
358 * name must be a string of maximum MAX_NAME_LENGTH length. 358 * name must be a string of maximum MAX_NAME_LENGTH length.
359 * length must be at least 1 byte. 359 * length must be at least 1 byte.
360 * length is the length of name with the NULL terminator. 360 * length is the length of name with the NULL terminator.
361 * return 0 if success. 361 *
362 * return -1 if failure. 362 * return 0 if success.
363 * return -1 if failure.
363 */ 364 */
364int setname(Messenger *m, uint8_t *name, uint16_t length) 365int setname(Messenger *m, uint8_t *name, uint16_t length)
365{ 366{
@@ -377,8 +378,9 @@ int setname(Messenger *m, uint8_t *name, uint16_t length)
377} 378}
378 379
379/* Get our nickname and put it in name. 380/* Get our nickname and put it in name.
380 * 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.
381 * return the length of the name. 382 *
383 * return the length of the name.
382 */ 384 */
383uint16_t getself_name(Messenger *m, uint8_t *name, uint16_t nlen) 385uint16_t getself_name(Messenger *m, uint8_t *name, uint16_t nlen)
384{ 386{
@@ -395,9 +397,10 @@ uint16_t getself_name(Messenger *m, uint8_t *name, uint16_t nlen)
395} 397}
396 398
397/* Get name of friendnumber and put it in name. 399/* Get name of friendnumber and put it in name.
398 * 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.
399 * return 0 if success. 401 *
400 * return -1 if failure. 402 * return 0 if success.
403 * return -1 if failure.
401 */ 404 */
402int getname(Messenger *m, int friendnumber, uint8_t *name) 405int getname(Messenger *m, int friendnumber, uint8_t *name)
403{ 406{
@@ -924,7 +927,7 @@ void doMessenger(Messenger *m)
924 LANdiscovery(m); 927 LANdiscovery(m);
925} 928}
926 929
927/* return the size of the messenger data (for saving) */ 930/* return size of the messenger data (for saving) */
928uint32_t Messenger_size(Messenger *m) 931uint32_t Messenger_size(Messenger *m)
929{ 932{
930 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/friend_requests.c b/toxcore/friend_requests.c
index 152cb188..6e98f18b 100644
--- a/toxcore/friend_requests.c
+++ b/toxcore/friend_requests.c
@@ -25,9 +25,10 @@
25 25
26/* Try to send a friend request to peer with public_key. 26/* Try to send a friend request to peer with public_key.
27 * data is the data in the request and length is the length. 27 * data is the data in the request and length is the length.
28 * return -1 if failure. 28 *
29 * return 0 if it sent the friend request directly to the friend. 29 * return -1 if failure.
30 * return the number of peers it was routed through if it did not send it directly. 30 * return 0 if it sent the friend request directly to the friend.
31 * return the number of peers it was routed through if it did not send it directly.
31 */ 32 */
32int send_friendrequest(DHT *dht, uint8_t *public_key, uint32_t nospam_num, uint8_t *data, uint32_t length) 33int send_friendrequest(DHT *dht, uint8_t *public_key, uint32_t nospam_num, uint8_t *data, uint32_t length)
33{ 34{
@@ -98,8 +99,9 @@ static void addto_receivedlist(Friend_Requests *fr, uint8_t *client_id)
98} 99}
99 100
100/* Check if a friend request was already received. 101/* Check if a friend request was already received.
101 * return 0 if it did not. 102 *
102 * return 1 if it did. 103 * return 0 if it did not.
104 * return 1 if it did.
103 */ 105 */
104static int request_received(Friend_Requests *fr, uint8_t *client_id) 106static int request_received(Friend_Requests *fr, uint8_t *client_id)
105{ 107{
diff --git a/toxcore/misc_tools.h b/toxcore/misc_tools.h
index 1889eba4..448c1cbd 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;
@@ -196,10 +196,10 @@ static inline void tox_array_pop(tox_array *arr, uint32_t num)
196 * Quick Sort: Complexity O(nlogn) 196 * Quick Sort: Complexity O(nlogn)
197 * arr - the array to sort 197 * arr - the array to sort
198 * n - the sort index (should be called with n = length(arr)) 198 * n - the sort index (should be called with n = length(arr))
199 * cmpfn - a function that compares two values of type type. 199 * cmpfn - a function that compares two values of type type.
200 * Must return -1, 0, 1 for a < b, a == b, and a > b respectively. 200 * Must return -1, 0, 1 for a < b, a == b, and a > b respectively.
201 */ 201 */
202/* Must be called in the header file. */ 202/* Must be called in the header file. */
203#define declare_quick_sort(type) \ 203#define declare_quick_sort(type) \
204void type##_quick_sort(type *arr, int n, int (*cmpfn)(type, type)); 204void type##_quick_sort(type *arr, int n, int (*cmpfn)(type, type));
205 205
diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c
index b168fc76..59a068c8 100644
--- a/toxcore/net_crypto.c
+++ b/toxcore/net_crypto.c
@@ -149,9 +149,9 @@ void random_nonce(uint8_t *nonce)
149 } 149 }
150} 150}
151 151
152/* return 0 if there is no received data in the buffer. 152/* return 0 if there is no received data in the buffer.
153 * return -1 if the packet was discarded. 153 * return -1 if the packet was discarded.
154 * return length of received data if successful. 154 * return length of received data if successful.
155 */ 155 */
156int read_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data) 156int read_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data)
157{ 157{
@@ -182,8 +182,8 @@ int read_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data)
182 return -1; 182 return -1;
183} 183}
184 184
185/* return 0 if data could not be put in packet queue. 185/* return 0 if data could not be put in packet queue.
186 * return 1 if data was put into the queue. 186 * return 1 if data was put into the queue.
187 */ 187 */
188int write_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data, uint32_t length) 188int write_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data, uint32_t length)
189{ 189{
@@ -220,8 +220,8 @@ int write_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data, uin
220 * Data represents the data we send with the request with length being the length of the data. 220 * Data represents the data we send with the request with length being the length of the data.
221 * request_id is the id of the request (32 = friend request, 254 = ping request). 221 * request_id is the id of the request (32 = friend request, 254 = ping request).
222 * 222 *
223 * returns -1 on failure. 223 * return -1 on failure.
224 * returns the length of the created packet on success. 224 * return the length of the created packet on success.
225 */ 225 */
226int create_request(uint8_t *send_public_key, uint8_t *send_secret_key, uint8_t *packet, uint8_t *recv_public_key, 226int create_request(uint8_t *send_public_key, uint8_t *send_secret_key, uint8_t *packet, uint8_t *recv_public_key,
227 uint8_t *data, uint32_t length, uint8_t request_id) 227 uint8_t *data, uint32_t length, uint8_t request_id)
@@ -251,7 +251,8 @@ int create_request(uint8_t *send_public_key, uint8_t *send_secret_key, uint8_t *
251/* Puts the senders public key in the request in public_key, the data from the request 251/* Puts the senders public key in the request in public_key, the data from the request
252 * in data if a friend or ping request was sent to us and returns the length of the data. 252 * in data if a friend or ping request was sent to us and returns the length of the data.
253 * packet is the request packet and length is its length. 253 * packet is the request packet and length is its length.
254 * return -1 if not valid request. 254 *
255 * return -1 if not valid request.
255 */ 256 */
256static int handle_request(Net_Crypto *c, uint8_t *public_key, uint8_t *data, uint8_t *request_id, uint8_t *packet, 257static int handle_request(Net_Crypto *c, uint8_t *public_key, uint8_t *data, uint8_t *request_id, uint8_t *packet,
257 uint16_t length) 258 uint16_t length)
@@ -348,8 +349,9 @@ static int send_cryptohandshake(Net_Crypto *c, int connection_id, uint8_t *publi
348} 349}
349 350
350/* Extract secret nonce, session public key and public_key from a packet(data) with length length. 351/* Extract secret nonce, session public key and public_key from a packet(data) with length length.
351 * return 1 if successful. 352 *
352 * return 0 if failure. 353 * return 1 if successful.
354 * return 0 if failure.
353 */ 355 */
354static int handle_cryptohandshake(Net_Crypto *c, uint8_t *public_key, uint8_t *secret_nonce, 356static int handle_cryptohandshake(Net_Crypto *c, uint8_t *public_key, uint8_t *secret_nonce,
355 uint8_t *session_key, uint8_t *data, uint16_t length) 357 uint8_t *session_key, uint8_t *data, uint16_t length)
@@ -381,8 +383,9 @@ static int handle_cryptohandshake(Net_Crypto *c, uint8_t *public_key, uint8_t *s
381} 383}
382 384
383/* Get crypto connection id from public key of peer. 385/* Get crypto connection id from public key of peer.
384 * return -1 if there are no connections like we are looking for. 386 *
385 * return id if it found it. 387 * return -1 if there are no connections like we are looking for.
388 * return id if it found it.
386 */ 389 */
387static int getcryptconnection_id(Net_Crypto *c, uint8_t *public_key) 390static int getcryptconnection_id(Net_Crypto *c, uint8_t *public_key)
388{ 391{
@@ -398,7 +401,8 @@ static int getcryptconnection_id(Net_Crypto *c, uint8_t *public_key)
398} 401}
399 402
400/* Set the size of the friend list to numfriends. 403/* Set the size of the friend list to numfriends.
401 * return -1 if realloc fails. 404 *
405 * return -1 if realloc fails.
402 */ 406 */
403int realloc_cryptoconnection(Net_Crypto *c, uint32_t num) 407int realloc_cryptoconnection(Net_Crypto *c, uint32_t num)
404{ 408{
@@ -418,8 +422,9 @@ int realloc_cryptoconnection(Net_Crypto *c, uint32_t num)
418} 422}
419 423
420/* Start a secure connection with other peer who has public_key and ip_port. 424/* Start a secure connection with other peer who has public_key and ip_port.
421 * returns -1 if failure. 425 *
422 * returns crypt_connection_id of the initialized connection if everything went well. 426 * return -1 if failure.
427 * return crypt_connection_id of the initialized connection if everything went well.
423 */ 428 */
424int crypto_connect(Net_Crypto *c, uint8_t *public_key, IP_Port ip_port) 429int crypto_connect(Net_Crypto *c, uint8_t *public_key, IP_Port ip_port)
425{ 430{
@@ -469,8 +474,9 @@ int crypto_connect(Net_Crypto *c, uint8_t *public_key, IP_Port ip_port)
469} 474}
470 475
471/* Handle an incoming connection. 476/* Handle an incoming connection.
472 * return -1 if no crypto inbound connection. 477 *
473 * return incoming connection id (Lossless_UDP one) if there is an incoming crypto connection. 478 * return -1 if no crypto inbound connection.
479 * return incoming connection id (Lossless_UDP one) if there is an incoming crypto connection.
474 * 480 *
475 * Put the public key of the peer in public_key, the secret_nonce from the handshake into secret_nonce 481 * Put the public key of the peer in public_key, the secret_nonce from the handshake into secret_nonce
476 * and the session public key for the connection in session_key. 482 * and the session public key for the connection in session_key.
@@ -507,8 +513,9 @@ int crypto_inbound(Net_Crypto *c, uint8_t *public_key, uint8_t *secret_nonce, ui
507} 513}
508 514
509/* Kill a crypto connection. 515/* Kill a crypto connection.
510 * return 0 if killed successfully. 516 *
511 * return 1 if there was a problem. 517 * return 0 if killed successfully.
518 * return 1 if there was a problem.
512 */ 519 */
513int crypto_kill(Net_Crypto *c, int crypt_connection_id) 520int crypto_kill(Net_Crypto *c, int crypt_connection_id)
514{ 521{
@@ -536,8 +543,9 @@ int crypto_kill(Net_Crypto *c, int crypt_connection_id)
536} 543}
537 544
538/* Accept an incoming connection using the parameters provided by crypto_inbound. 545/* Accept an incoming connection using the parameters provided by crypto_inbound.
539 * return -1 if not successful. 546 *
540 * returns the crypt_connection_id if successful. 547 * return -1 if not successful.
548 * return the crypt_connection_id if successful.
541 */ 549 */
542int accept_crypto_inbound(Net_Crypto *c, int connection_id, uint8_t *public_key, uint8_t *secret_nonce, 550int accept_crypto_inbound(Net_Crypto *c, int connection_id, uint8_t *public_key, uint8_t *secret_nonce,
543 uint8_t *session_key) 551 uint8_t *session_key)
@@ -595,11 +603,11 @@ int accept_crypto_inbound(Net_Crypto *c, int connection_id, uint8_t *public_key,
595 return -1; 603 return -1;
596} 604}
597 605
598/* return 0 if no connection. 606/* return 0 if no connection.
599 * return 1 we have sent a handshake. 607 * return 1 we have sent a handshake.
600 * return 2 if connection is not confirmed yet (we have received a handshake but no empty data packet). 608 * return 2 if connection is not confirmed yet (we have received a handshake but no empty data packet).
601 * return 3 if the connection is established. 609 * return 3 if the connection is established.
602 * return 4 if the connection is timed out and waiting to be killed. 610 * return 4 if the connection is timed out and waiting to be killed.
603 */ 611 */
604int is_cryptoconnected(Net_Crypto *c, int crypt_connection_id) 612int is_cryptoconnected(Net_Crypto *c, int crypt_connection_id)
605{ 613{
@@ -633,9 +641,10 @@ void load_keys(Net_Crypto *c, uint8_t *keys)
633} 641}
634 642
635/* Adds an incoming connection to the incoming_connection list. 643/* Adds an incoming connection to the incoming_connection list.
636 * returns 0 if successful
637 * returns 1 if failure.
638 * TODO: Optimize this. 644 * TODO: Optimize this.
645 *
646 * returns 0 if successful
647 * returns 1 if failure.
639 */ 648 */
640static int new_incoming(Net_Crypto *c, int id) 649static int new_incoming(Net_Crypto *c, int id)
641{ 650{
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
diff --git a/toxcore/network.c b/toxcore/network.c
index 592f8e45..9e329949 100644
--- a/toxcore/network.c
+++ b/toxcore/network.c
@@ -23,7 +23,7 @@
23 23
24#include "network.h" 24#include "network.h"
25 25
26/* return current UNIX time in microseconds (us). */ 26/* return current UNIX time in microseconds (us). */
27uint64_t current_time(void) 27uint64_t current_time(void)
28{ 28{
29 uint64_t time; 29 uint64_t time;
@@ -44,7 +44,7 @@ uint64_t current_time(void)
44#endif 44#endif
45} 45}
46 46
47/* return a random number. 47/* return a random number.
48 * NOTE: This function should probably not be used where cryptographic randomness is absolutely necessary. 48 * NOTE: This function should probably not be used where cryptographic randomness is absolutely necessary.
49 */ 49 */
50uint32_t random_int(void) 50uint32_t random_int(void)
@@ -153,8 +153,8 @@ static void at_shutdown(void)
153 * ip must be in network order EX: 127.0.0.1 = (7F000001). 153 * ip must be in network order EX: 127.0.0.1 = (7F000001).
154 * port is in host byte order (this means don't worry about it). 154 * port is in host byte order (this means don't worry about it).
155 * 155 *
156 * returns Networking_Core object if no problems 156 * return Networking_Core object if no problems
157 * returns NULL if there are problems. 157 * return NULL if there are problems.
158 */ 158 */
159Networking_Core *new_networking(IP ip, uint16_t port) 159Networking_Core *new_networking(IP ip, uint16_t port)
160{ 160{
diff --git a/toxcore/network.h b/toxcore/network.h
index 3a1c75f8..ca79a81b 100644
--- a/toxcore/network.h
+++ b/toxcore/network.h
@@ -128,10 +128,10 @@ typedef struct {
128 128
129} Networking_Core; 129} Networking_Core;
130 130
131/* return current time in milleseconds since the epoch. */ 131/* return current time in milleseconds since the epoch. */
132uint64_t current_time(void); 132uint64_t current_time(void);
133 133
134/* return a random number. 134/* return a random number.
135 * NOTE: this function should probably not be used where cryptographic randomness is absolutely necessary. 135 * NOTE: this function should probably not be used where cryptographic randomness is absolutely necessary.
136 */ 136 */
137uint32_t random_int(void); 137uint32_t random_int(void);
@@ -153,12 +153,12 @@ void networking_registerhandler(Networking_Core *net, uint8_t byte, packet_handl
153void networking_poll(Networking_Core *net); 153void networking_poll(Networking_Core *net);
154 154
155/* Initialize networking. 155/* Initialize networking.
156 * bind to ip and port. 156 * bind to ip and port.
157 * ip must be in network order EX: 127.0.0.1 = (7F000001). 157 * ip must be in network order EX: 127.0.0.1 = (7F000001).
158 * port is in host byte order (this means don't worry about it). 158 * port is in host byte order (this means don't worry about it).
159 * 159 *
160 * returns 0 if no problems. 160 * return 0 if no problems.
161 * returns -1 if there were problems. 161 * return -1 if there were problems.
162 */ 162 */
163Networking_Core *new_networking(IP ip, uint16_t port); 163Networking_Core *new_networking(IP ip, uint16_t port);
164 164
diff --git a/toxcore/tox.c b/toxcore/tox.c
index 494de478..23ca6572 100644
--- a/toxcore/tox.c
+++ b/toxcore/tox.c
@@ -38,16 +38,17 @@ void tox_getaddress(void *tox, uint8_t *address)
38 * Set the data that will be sent along with friend request. 38 * Set the data that will be sent along with friend request.
39 * 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. 39 * 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.
40 * data is the data and length is the length. 40 * data is the data and length is the length.
41 * returns the friend number if success. 41 *
42 * return FA_TOOLONG if message length is too long. 42 * return the friend number if success.
43 * return FAERR_NOMESSAGE if no message (message length must be >= 1 byte). 43 * return FA_TOOLONG if message length is too long.
44 * return FAERR_OWNKEY if user's own key. 44 * return FAERR_NOMESSAGE if no message (message length must be >= 1 byte).
45 * return FAERR_ALREADYSENT if friend request already sent or already a friend. 45 * return FAERR_OWNKEY if user's own key.
46 * return FAERR_UNKNOWN for unknown error. 46 * return FAERR_ALREADYSENT if friend request already sent or already a friend.
47 * return FAERR_BADCHECKSUM if bad checksum in address. 47 * return FAERR_UNKNOWN for unknown error.
48 * return FAERR_SETNEWNOSPAM if the friend was already there but the nospam was different. 48 * return FAERR_BADCHECKSUM if bad checksum in address.
49 * (the nospam for that friend was set to the new one). 49 * return FAERR_SETNEWNOSPAM if the friend was already there but the nospam was different.
50 * return FAERR_NOMEM if increasing the friend list size fails. 50 * (the nospam for that friend was set to the new one).
51 * return FAERR_NOMEM if increasing the friend list size fails.
51 */ 52 */
52int tox_addfriend(void *tox, uint8_t *address, uint8_t *data, uint16_t length) 53int tox_addfriend(void *tox, uint8_t *address, uint8_t *data, uint16_t length)
53{ 54{
@@ -56,7 +57,8 @@ int tox_addfriend(void *tox, uint8_t *address, uint8_t *data, uint16_t length)
56} 57}
57 58
58/* Add a friend without sending a friendrequest. 59/* Add a friend without sending a friendrequest.
59 * returns the friend number if success. 60 *
61 * return the friend number if success.
60 * return -1 if failure. 62 * return -1 if failure.
61 */ 63 */
62int tox_addfriend_norequest(void *tox, uint8_t *client_id) 64int tox_addfriend_norequest(void *tox, uint8_t *client_id)
@@ -65,8 +67,8 @@ int tox_addfriend_norequest(void *tox, uint8_t *client_id)
65 return m_addfriend_norequest(m, client_id); 67 return m_addfriend_norequest(m, client_id);
66} 68}
67 69
68/* return the friend id associated to that client id. 70/* return the friend id associated to that client id.
69 * return -1 if no such friend. 71 * return -1 if no such friend.
70 */ 72 */
71int tox_getfriend_id(void *tox, uint8_t *client_id) 73int tox_getfriend_id(void *tox, uint8_t *client_id)
72{ 74{
@@ -75,7 +77,8 @@ int tox_getfriend_id(void *tox, uint8_t *client_id)
75} 77}
76 78
77/* Copies the public key associated to that friend id into client_id buffer. 79/* Copies the public key associated to that friend id into client_id buffer.
78 * Make sure that client_id is of size CLIENT_ID_SIZE. 80 * Make sure that client_id is of size CLIENT_ID_SIZE.
81 *
79 * return 0 if success. 82 * return 0 if success.
80 * return -1 if failure. 83 * return -1 if failure.
81 */ 84 */
@@ -92,11 +95,11 @@ int tox_delfriend(void *tox, int friendnumber)
92 return m_delfriend(m, friendnumber); 95 return m_delfriend(m, friendnumber);
93} 96}
94 97
95/* return 4 if friend is online. 98/* return 4 if friend is online.
96 * return 3 if friend is confirmed. 99 * return 3 if friend is confirmed.
97 * return 2 if the friend request was sent. 100 * return 2 if the friend request was sent.
98 * return 1 if the friend was added. 101 * return 1 if the friend was added.
99 * return 0 if there is no friend with that number. 102 * return 0 if there is no friend with that number.
100 */ 103 */
101int tox_friendstatus(void *tox, int friendnumber) 104int tox_friendstatus(void *tox, int friendnumber)
102{ 105{
@@ -105,7 +108,7 @@ int tox_friendstatus(void *tox, int friendnumber)
105} 108}
106 109
107/* Send a text chat message to an online friend. 110/* Send a text chat message to an online friend.
108 * returns the message id if packet was successfully put into the send queue. 111 * return the message id if packet was successfully put into the send queue.
109 * return 0 if it was not. 112 * return 0 if it was not.
110 * 113 *
111 * You will want to retain the return value, it will be passed to your read receipt callback 114 * You will want to retain the return value, it will be passed to your read receipt callback
@@ -126,7 +129,7 @@ uint32_t tox_sendmessage_withid(void *tox, int friendnumber, uint32_t theid, uin
126} 129}
127 130
128/* Send an action to an online friend. 131/* Send an action to an online friend.
129 * returns 1 if packet was successfully put into the send queue. 132 * return 1 if packet was successfully put into the send queue.
130 * return 0 if it was not. 133 * return 0 if it was not.
131 */ 134 */
132int tox_sendaction(void *tox, int friendnumber, uint8_t *action, uint32_t length) 135int tox_sendaction(void *tox, int friendnumber, uint8_t *action, uint32_t length)
@@ -139,8 +142,9 @@ int tox_sendaction(void *tox, int friendnumber, uint8_t *action, uint32_t length
139 * name must be a string of maximum MAX_NAME_LENGTH length. 142 * name must be a string of maximum MAX_NAME_LENGTH length.
140 * length must be at least 1 byte. 143 * length must be at least 1 byte.
141 * length is the length of name with the NULL terminator. 144 * length is the length of name with the NULL terminator.
142 * return 0 if success. 145 *
143 * return -1 if failure. 146 * return 0 if success.
147 * return -1 if failure.
144 */ 148 */
145int tox_setname(void *tox, uint8_t *name, uint16_t length) 149int tox_setname(void *tox, uint8_t *name, uint16_t length)
146{ 150{
@@ -152,8 +156,9 @@ int tox_setname(void *tox, uint8_t *name, uint16_t length)
152 * m - The messanger context to use. 156 * m - The messanger context to use.
153 * name - Pointer to a string for the name. 157 * name - Pointer to a string for the name.
154 * nlen - The length of the string buffer. 158 * nlen - The length of the string buffer.
155 * return length of the name. 159 *
156 * return 0 on error. 160 * return length of the name.
161 * return 0 on error.
157 */ 162 */
158uint16_t tox_getselfname(void *tox, uint8_t *name, uint16_t nlen) 163uint16_t tox_getselfname(void *tox, uint8_t *name, uint16_t nlen)
159{ 164{
@@ -162,7 +167,8 @@ uint16_t tox_getselfname(void *tox, uint8_t *name, uint16_t nlen)
162} 167}
163 168
164/* Get name of friendnumber and put it in name. 169/* Get name of friendnumber and put it in name.
165 * name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH (128) bytes. 170 * name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH (128) bytes.
171 *
166 * return 0 if success. 172 * return 0 if success.
167 * return -1 if failure. 173 * return -1 if failure.
168 */ 174 */
@@ -172,9 +178,11 @@ int tox_getname(void *tox, int friendnumber, uint8_t *name)
172 return getname(m, friendnumber, name); 178 return getname(m, friendnumber, name);
173} 179}
174 180
175/* set our user status 181/* Set our user status;
176 you are responsible for freeing status after 182 * you are responsible for freeing status after.
177 returns 0 on success, -1 on failure */ 183 *
184 * return 0 on success, -1 on failure.
185 */
178int tox_set_statusmessage(void *tox, uint8_t *status, uint16_t length) 186int tox_set_statusmessage(void *tox, uint8_t *status, uint16_t length)
179{ 187{
180 Messenger *m = tox; 188 Messenger *m = tox;
@@ -187,8 +195,8 @@ int tox_set_userstatus(void *tox, USERSTATUS status)
187 return m_set_userstatus(m, status); 195 return m_set_userstatus(m, status);
188} 196}
189 197
190/* return the length of friendnumber's status message, including null. 198/* return the length of friendnumber's status message, including null.
191 * Pass it into malloc. 199 * Pass it into malloc.
192 */ 200 */
193int tox_get_statusmessage_size(void *tox, int friendnumber) 201int tox_get_statusmessage_size(void *tox, int friendnumber)
194{ 202{
@@ -197,8 +205,8 @@ int tox_get_statusmessage_size(void *tox, int friendnumber)
197} 205}
198 206
199/* Copy friendnumber's status message into buf, truncating if size is over maxlen. 207/* Copy friendnumber's status message into buf, truncating if size is over maxlen.
200 * Get the size you need to allocate from m_get_statusmessage_size. 208 * Get the size you need to allocate from m_get_statusmessage_size.
201 * The self variant will copy our own status message. 209 * The self variant will copy our own status message.
202 */ 210 */
203int tox_copy_statusmessage(void *tox, int friendnumber, uint8_t *buf, uint32_t maxlen) 211int tox_copy_statusmessage(void *tox, int friendnumber, uint8_t *buf, uint32_t maxlen)
204{ 212{
@@ -317,6 +325,7 @@ void tox_callback_read_receipt(void *tox, void (*function)(Messenger *tox, int,
317 325
318/* Set the callback for connection status changes. 326/* Set the callback for connection status changes.
319 * function(int friendnumber, uint8_t status) 327 * function(int friendnumber, uint8_t status)
328 *
320 * Status: 329 * Status:
321 * 0 -- friend went offline after being previously online 330 * 0 -- friend went offline after being previously online
322 * 1 -- friend went online 331 * 1 -- friend went online
@@ -332,7 +341,7 @@ void tox_callback_connectionstatus(void *tox, void (*function)(Messenger *tox, i
332} 341}
333 342
334/* Use this function to bootstrap the client. 343/* Use this function to bootstrap the client.
335 * Sends a get nodes request to the given node with ip port and public_key. 344 * Sends a get nodes request to the given node with ip port and public_key.
336 */ 345 */
337void tox_bootstrap(void *tox, IP_Port ip_port, uint8_t *public_key) 346void tox_bootstrap(void *tox, IP_Port ip_port, uint8_t *public_key)
338{ 347{
@@ -340,8 +349,8 @@ void tox_bootstrap(void *tox, IP_Port ip_port, uint8_t *public_key)
340 DHT_bootstrap(m->dht, ip_port, public_key); 349 DHT_bootstrap(m->dht, ip_port, public_key);
341} 350}
342 351
343/* returns 0 if we are not connected to the DHT. 352/* return 0 if we are not connected to the DHT.
344 * returns 1 if we are. 353 * return 1 if we are.
345 */ 354 */
346int tox_isconnected(void *tox) 355int tox_isconnected(void *tox)
347{ 356{
@@ -350,8 +359,9 @@ int tox_isconnected(void *tox)
350} 359}
351 360
352/* Run this at startup. 361/* Run this at startup.
353 * returns allocated instance of tox on success. 362 *
354 * returns 0 if there are problems. 363 * return allocated instance of tox on success.
364 * return 0 if there are problems.
355 */ 365 */
356void *tox_new(void) 366void *tox_new(void)
357{ 367{
@@ -376,7 +386,7 @@ void tox_do(void *tox)
376 386
377/* SAVING AND LOADING FUNCTIONS: */ 387/* SAVING AND LOADING FUNCTIONS: */
378 388
379/* returns the size of the messenger data (for saving). */ 389/* return size of the messenger data (for saving). */
380uint32_t tox_size(void *tox) 390uint32_t tox_size(void *tox)
381{ 391{
382 Messenger *m = tox; 392 Messenger *m = tox;
diff --git a/toxcore/tox.h b/toxcore/tox.h
index eabdf8ff..b17ca36f 100644
--- a/toxcore/tox.h
+++ b/toxcore/tox.h
@@ -86,7 +86,7 @@ TOX_USERSTATUS;
86 86
87typedef void Tox; 87typedef void Tox;
88 88
89/* returns FRIEND_ADDRESS_SIZE byte address to give to others. 89/* return FRIEND_ADDRESS_SIZE byte address to give to others.
90 * format: [client_id (32 bytes)][nospam number (4 bytes)][checksum (2 bytes)] 90 * format: [client_id (32 bytes)][nospam number (4 bytes)][checksum (2 bytes)]
91 */ 91 */
92void tox_getaddress(Tox *tox, uint8_t *address); 92void tox_getaddress(Tox *tox, uint8_t *address);
@@ -95,32 +95,33 @@ void tox_getaddress(Tox *tox, uint8_t *address);
95 * Set the data that will be sent along with friend request. 95 * Set the data that will be sent along with friend request.
96 * 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. 96 * 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.
97 * data is the data and length is the length. 97 * data is the data and length is the length.
98 * returns the friend number if success. 98 *
99 * return TOX_FA_TOOLONG if message length is too long. 99 * return the friend number if success.
100 * return TOX_FAERR_NOMESSAGE if no message (message length must be >= 1 byte). 100 * return TOX_FA_TOOLONG if message length is too long.
101 * return TOX_FAERR_OWNKEY if user's own key. 101 * return TOX_FAERR_NOMESSAGE if no message (message length must be >= 1 byte).
102 * return TOX_FAERR_ALREADYSENT if friend request already sent or already a friend. 102 * return TOX_FAERR_OWNKEY if user's own key.
103 * return TOX_FAERR_UNKNOWN for unknown error. 103 * return TOX_FAERR_ALREADYSENT if friend request already sent or already a friend.
104 * return TOX_FAERR_BADCHECKSUM if bad checksum in address. 104 * return TOX_FAERR_UNKNOWN for unknown error.
105 * return TOX_FAERR_SETNEWNOSPAM if the friend was already there but the nospam was different. 105 * return TOX_FAERR_BADCHECKSUM if bad checksum in address.
106 * (the nospam for that friend was set to the new one). 106 * return TOX_FAERR_SETNEWNOSPAM if the friend was already there but the nospam was different.
107 * return TOX_FAERR_NOMEM if increasing the friend list size fails. 107 * (the nospam for that friend was set to the new one).
108 * return TOX_FAERR_NOMEM if increasing the friend list size fails.
108 */ 109 */
109int tox_addfriend(Tox *tox, uint8_t *address, uint8_t *data, uint16_t length); 110int tox_addfriend(Tox *tox, uint8_t *address, uint8_t *data, uint16_t length);
110 111
111 112
112/* Add a friend without sending a friendrequest. 113/* Add a friend without sending a friendrequest.
113 * returns the friend number if success. 114 * return the friend number if success.
114 * return -1 if failure. 115 * return -1 if failure.
115 */ 116 */
116int tox_addfriend_norequest(Tox *tox, uint8_t *client_id); 117int tox_addfriend_norequest(Tox *tox, uint8_t *client_id);
117 118
118/* return the friend id associated to that client id. 119/* return the friend id associated to that client id.
119 return -1 if no such friend */ 120 return -1 if no such friend */
120int tox_getfriend_id(Tox *tox, uint8_t *client_id); 121int tox_getfriend_id(Tox *tox, uint8_t *client_id);
121 122
122/* Copies the public key associated to that friend id into client_id buffer. 123/* Copies the public key associated to that friend id into client_id buffer.
123 * Make sure that client_id is of size CLIENT_ID_SIZE. 124 * Make sure that client_id is of size CLIENT_ID_SIZE.
124 * return 0 if success. 125 * return 0 if success.
125 * return -1 if failure. 126 * return -1 if failure.
126 */ 127 */
@@ -138,8 +139,9 @@ int tox_delfriend(Tox *tox, int friendnumber);
138int tox_friendstatus(Tox *tox, int friendnumber); 139int tox_friendstatus(Tox *tox, int friendnumber);
139 140
140/* Send a text chat message to an online friend. 141/* Send a text chat message to an online friend.
141 * returns the message id if packet was successfully put into the send queue. 142 *
142 * return 0 if it was not. 143 * return the message id if packet was successfully put into the send queue.
144 * return 0 if it was not.
143 * 145 *
144 * You will want to retain the return value, it will be passed to your read receipt callback 146 * You will want to retain the return value, it will be passed to your read receipt callback
145 * if one is received. 147 * if one is received.
@@ -150,7 +152,8 @@ uint32_t tox_sendmessage(Tox *tox, int friendnumber, uint8_t *message, uint32_t
150uint32_t tox_sendmessage_withid(Tox *tox, int friendnumber, uint32_t theid, uint8_t *message, uint32_t length); 152uint32_t tox_sendmessage_withid(Tox *tox, int friendnumber, uint32_t theid, uint8_t *message, uint32_t length);
151 153
152/* Send an action to an online friend. 154/* Send an action to an online friend.
153 * returns 1 if packet was successfully put into the send queue. 155 *
156 * return 1 if packet was successfully put into the send queue.
154 * return 0 if it was not. 157 * return 0 if it was not.
155 */ 158 */
156int tox_sendaction(Tox *tox, int friendnumber, uint8_t *action, uint32_t length); 159int tox_sendaction(Tox *tox, int friendnumber, uint8_t *action, uint32_t length);
@@ -160,8 +163,8 @@ int tox_sendaction(Tox *tox, int friendnumber, uint8_t *action, uint32_t length)
160 * length must be at least 1 byte. 163 * length must be at least 1 byte.
161 * length is the length of name with the NULL terminator. 164 * length is the length of name with the NULL terminator.
162 * 165 *
163 * return 0 if success. 166 * return 0 if success.
164 * return -1 if failure. 167 * return -1 if failure.
165 */ 168 */
166int tox_setname(Tox *tox, uint8_t *name, uint16_t length); 169int tox_setname(Tox *tox, uint8_t *name, uint16_t length);
167 170
@@ -171,12 +174,13 @@ int tox_setname(Tox *tox, uint8_t *name, uint16_t length);
171 * name - Pointer to a string for the name. 174 * name - Pointer to a string for the name.
172 * nlen - The length of the string buffer. 175 * nlen - The length of the string buffer.
173 * 176 *
174 * returns Return the length of the name, 0 on error. 177 * return length of name.
178 * return 0 on error.
175 */ 179 */
176uint16_t tox_getselfname(Tox *tox, uint8_t *name, uint16_t nlen); 180uint16_t tox_getselfname(Tox *tox, uint8_t *name, uint16_t nlen);
177 181
178/* Get name of friendnumber and put it in name. 182/* Get name of friendnumber and put it in name.
179 * name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH (128) bytes. 183 * name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH (128) bytes.
180 * 184 *
181 * return 0 if success. 185 * return 0 if success.
182 * return -1 if failure. 186 * return -1 if failure.
@@ -184,29 +188,30 @@ uint16_t tox_getselfname(Tox *tox, uint8_t *name, uint16_t nlen);
184int tox_getname(Tox *tox, int friendnumber, uint8_t *name); 188int tox_getname(Tox *tox, int friendnumber, uint8_t *name);
185 189
186/* Set our user status. 190/* Set our user status.
187 * You are responsible for freeing status after. 191 * You are responsible for freeing status after.
192 *
188 * returns 0 on success. 193 * returns 0 on success.
189 * returns -1 on failure. 194 * returns -1 on failure.
190 */ 195 */
191int tox_set_statusmessage(Tox *tox, uint8_t *status, uint16_t length); 196int tox_set_statusmessage(Tox *tox, uint8_t *status, uint16_t length);
192int tox_set_userstatus(Tox *tox, TOX_USERSTATUS status); 197int tox_set_userstatus(Tox *tox, TOX_USERSTATUS status);
193 198
194/* return the length of friendnumber's status message, including null. 199/* return the length of friendnumber's status message, including null.
195 * Pass it into malloc 200 * Pass it into malloc
196 */ 201 */
197int tox_get_statusmessage_size(Tox *tox, int friendnumber); 202int tox_get_statusmessage_size(Tox *tox, int friendnumber);
198 203
199/* Copy friendnumber's status message into buf, truncating if size is over maxlen. 204/* Copy friendnumber's status message into buf, truncating if size is over maxlen.
200 * Get the size you need to allocate from m_get_statusmessage_size. 205 * Get the size you need to allocate from m_get_statusmessage_size.
201 * The self variant will copy our own status message. 206 * The self variant will copy our own status message.
202 */ 207 */
203int tox_copy_statusmessage(Tox *tox, int friendnumber, uint8_t *buf, uint32_t maxlen); 208int tox_copy_statusmessage(Tox *tox, int friendnumber, uint8_t *buf, uint32_t maxlen);
204int tox_copy_self_statusmessage(Tox *tox, uint8_t *buf, uint32_t maxlen); 209int tox_copy_self_statusmessage(Tox *tox, uint8_t *buf, uint32_t maxlen);
205 210
206/* return one of USERSTATUS values. 211/* return one of USERSTATUS values.
207 * Values unknown to your application should be represented as USERSTATUS_NONE. 212 * Values unknown to your application should be represented as USERSTATUS_NONE.
208 * As above, the self variant will return our own USERSTATUS. 213 * As above, the self variant will return our own USERSTATUS.
209 * If friendnumber is invalid, this shall return USERSTATUS_INVALID. 214 * If friendnumber is invalid, this shall return USERSTATUS_INVALID.
210 */ 215 */
211TOX_USERSTATUS tox_get_userstatus(Tox *tox, int friendnumber); 216TOX_USERSTATUS tox_get_userstatus(Tox *tox, int friendnumber);
212TOX_USERSTATUS tox_get_selfuserstatus(Tox *tox); 217TOX_USERSTATUS tox_get_selfuserstatus(Tox *tox);
@@ -264,6 +269,7 @@ void tox_callback_read_receipt(Tox *tox, void (*function)(Tox *tox, int, uint32_
264 269
265/* Set the callback for connection status changes. 270/* Set the callback for connection status changes.
266 * function(int friendnumber, uint8_t status) 271 * function(int friendnumber, uint8_t status)
272 *
267 * Status: 273 * Status:
268 * 0 -- friend went offline after being previously online 274 * 0 -- friend went offline after being previously online
269 * 1 -- friend went online 275 * 1 -- friend went online
@@ -275,18 +281,19 @@ void tox_callback_read_receipt(Tox *tox, void (*function)(Tox *tox, int, uint32_
275void tox_callback_connectionstatus(Tox *tox, void (*function)(Tox *tox, int, uint8_t, void *), void *userdata); 281void tox_callback_connectionstatus(Tox *tox, void (*function)(Tox *tox, int, uint8_t, void *), void *userdata);
276 282
277/* Use this function to bootstrap the client. 283/* Use this function to bootstrap the client.
278 * Sends a get nodes request to the given node with ip port and public_key. 284 * Sends a get nodes request to the given node with ip port and public_key.
279 */ 285 */
280void tox_bootstrap(Tox *tox, tox_IP_Port ip_port, uint8_t *public_key); 286void tox_bootstrap(Tox *tox, tox_IP_Port ip_port, uint8_t *public_key);
281 287
282/* returns 0 if we are not connected to the DHT. 288/* return 0 if we are not connected to the DHT.
283 * returns 1 if we are. 289 * return 1 if we are.
284 */ 290 */
285int tox_isconnected(Tox *tox); 291int tox_isconnected(Tox *tox);
286 292
287/* Run this at startup. 293/* Run this at startup.
288 * returns allocated instance of tox on success. 294 *
289 * returns 0 if there are problems. 295 * return allocated instance of tox on success.
296 * return 0 if there are problems.
290 */ 297 */
291Tox *tox_new(void); 298Tox *tox_new(void);
292 299
@@ -299,7 +306,7 @@ void tox_do(Tox *tox);
299 306
300/* SAVING AND LOADING FUNCTIONS: */ 307/* SAVING AND LOADING FUNCTIONS: */
301 308
302/* returns the size of the messenger data (for saving). */ 309/* return size of messenger data (for saving). */
303uint32_t tox_size(Tox *tox); 310uint32_t tox_size(Tox *tox);
304 311
305/* Save the messenger in data (must be allocated memory of size Messenger_size()). */ 312/* Save the messenger in data (must be allocated memory of size Messenger_size()). */