diff options
-rw-r--r-- | configure.ac | 4 | ||||
-rw-r--r-- | docs/start_guide.rst | 2 | ||||
-rw-r--r-- | other/DHT_bootstrap.c | 16 | ||||
-rw-r--r-- | other/bootstrap_serverdaemon/DHT_bootstrap_daemon.c | 20 | ||||
-rw-r--r-- | other/fun/sign.c | 10 | ||||
-rw-r--r-- | toxcore/DHT.c | 148 | ||||
-rw-r--r-- | toxcore/DHT.h | 107 | ||||
-rw-r--r-- | toxcore/LAN_discovery.c | 37 | ||||
-rw-r--r-- | toxcore/LAN_discovery.h | 6 | ||||
-rw-r--r-- | toxcore/Lossless_UDP.c | 110 | ||||
-rw-r--r-- | toxcore/Lossless_UDP.h | 124 | ||||
-rw-r--r-- | toxcore/Messenger.c | 214 | ||||
-rw-r--r-- | toxcore/Messenger.h | 280 | ||||
-rw-r--r-- | toxcore/friend_requests.c | 25 | ||||
-rw-r--r-- | toxcore/friend_requests.h | 21 | ||||
-rw-r--r-- | toxcore/net_crypto.c | 209 | ||||
-rw-r--r-- | toxcore/net_crypto.h | 147 | ||||
-rw-r--r-- | toxcore/network.c | 59 | ||||
-rw-r--r-- | toxcore/network.h | 64 | ||||
-rw-r--r-- | toxcore/packets.h | 4 | ||||
-rw-r--r-- | toxcore/ping.c | 34 | ||||
-rw-r--r-- | toxcore/tox.c | 237 | ||||
-rw-r--r-- | toxcore/tox.h | 261 |
23 files changed, 1159 insertions, 980 deletions
diff --git a/configure.ac b/configure.ac index 185fecb4..381b99c9 100644 --- a/configure.ac +++ b/configure.ac | |||
@@ -80,7 +80,7 @@ AC_ARG_WITH(libsodium-headers, | |||
80 | [search for libsodium header files in DIR]), | 80 | [search for libsodium header files in DIR]), |
81 | [ | 81 | [ |
82 | LIBSODIUM_SEARCH_HEADERS="$withval" | 82 | LIBSODIUM_SEARCH_HEADERS="$withval" |
83 | AC_MSG_NOTICE([Will search for libsodium header files in $withval]) | 83 | AC_MSG_NOTICE([will search for libsodium header files in $withval]) |
84 | ] | 84 | ] |
85 | ) | 85 | ) |
86 | 86 | ||
@@ -89,7 +89,7 @@ AC_ARG_WITH(libsodium-libs, | |||
89 | [search for libsodium libraries in DIR]), | 89 | [search for libsodium libraries in DIR]), |
90 | [ | 90 | [ |
91 | LIBSODIUM_SEARCH_LIBS="$withval" | 91 | LIBSODIUM_SEARCH_LIBS="$withval" |
92 | AC_MSG_NOTICE([Will search for libsodium libraries in $withval]) | 92 | AC_MSG_NOTICE([will search for libsodium libraries in $withval]) |
93 | ] | 93 | ] |
94 | ) | 94 | ) |
95 | 95 | ||
diff --git a/docs/start_guide.rst b/docs/start_guide.rst index 19ef982e..604908bc 100644 --- a/docs/start_guide.rst +++ b/docs/start_guide.rst | |||
@@ -19,7 +19,7 @@ guide for how you *use* Tox. Here's a user-friendly attempt at it. | |||
19 | - You need to connect to a bootstrapping server, to give you a | 19 | - You need to connect to a bootstrapping server, to give you a |
20 | public key. | 20 | public key. |
21 | - Where can I find a public server? Right here, as of now: (the help | 21 | - Where can I find a public server? Right here, as of now: (the help |
22 | message from running ``nTox`` with no args will help) | 22 | message from running ``nTox`` with no args will help). |
23 | 23 | ||
24 | - ``198.46.136.167 33445 728925473812C7AAC482BE7250BCCAD0B8CB9F737BF3D42ABD34459C1768F854`` | 24 | - ``198.46.136.167 33445 728925473812C7AAC482BE7250BCCAD0B8CB9F737BF3D42ABD34459C1768F854`` |
25 | - ``192.81.133.111 33445 8CD5A9BF0A6CE358BA36F7A653F99FA6B258FF756E490F52C1F98CC420F78858`` | 25 | - ``192.81.133.111 33445 8CD5A9BF0A6CE358BA36F7A653F99FA6B258FF756E490F52C1F98CC420F78858`` |
diff --git a/other/DHT_bootstrap.c b/other/DHT_bootstrap.c index 0bf71312..229e6593 100644 --- a/other/DHT_bootstrap.c +++ b/other/DHT_bootstrap.c | |||
@@ -31,7 +31,7 @@ | |||
31 | #include "../toxcore/friend_requests.h" | 31 | #include "../toxcore/friend_requests.h" |
32 | #include "../testing/misc_tools.h" | 32 | #include "../testing/misc_tools.h" |
33 | 33 | ||
34 | //Sleep function (x = milliseconds) | 34 | /* Sleep function (x = milliseconds) */ |
35 | #ifdef WIN32 | 35 | #ifdef WIN32 |
36 | #define c_sleep(x) Sleep(1*x) | 36 | #define c_sleep(x) Sleep(1*x) |
37 | #else | 37 | #else |
@@ -52,7 +52,8 @@ void manage_keys(DHT *dht) | |||
52 | FILE *keys_file = fopen("key", "r"); | 52 | FILE *keys_file = fopen("key", "r"); |
53 | 53 | ||
54 | if (keys_file != NULL) { | 54 | if (keys_file != NULL) { |
55 | //if file was opened successfully -- load keys | 55 | /* If file was opened successfully -- load keys, |
56 | otherwise save new keys */ | ||
56 | size_t read_size = fread(keys, sizeof(uint8_t), KEYS_SIZE, keys_file); | 57 | size_t read_size = fread(keys, sizeof(uint8_t), KEYS_SIZE, keys_file); |
57 | 58 | ||
58 | if (read_size != KEYS_SIZE) { | 59 | if (read_size != KEYS_SIZE) { |
@@ -61,9 +62,8 @@ void manage_keys(DHT *dht) | |||
61 | } | 62 | } |
62 | 63 | ||
63 | load_keys(dht->c, keys); | 64 | load_keys(dht->c, keys); |
64 | printf("Keys loaded successfully\n"); | 65 | printf("Keys loaded successfully.\n"); |
65 | } else { | 66 | } else { |
66 | //otherwise save new keys | ||
67 | new_keys(dht->c); | 67 | new_keys(dht->c); |
68 | save_keys(dht->c, keys); | 68 | save_keys(dht->c, keys); |
69 | keys_file = fopen("key", "w"); | 69 | keys_file = fopen("key", "w"); |
@@ -73,7 +73,7 @@ void manage_keys(DHT *dht) | |||
73 | exit(1); | 73 | exit(1); |
74 | } | 74 | } |
75 | 75 | ||
76 | printf("Keys saved successfully\n"); | 76 | printf("Keys saved successfully.\n"); |
77 | } | 77 | } |
78 | 78 | ||
79 | fclose(keys_file); | 79 | fclose(keys_file); |
@@ -81,8 +81,8 @@ void manage_keys(DHT *dht) | |||
81 | 81 | ||
82 | int main(int argc, char *argv[]) | 82 | int main(int argc, char *argv[]) |
83 | { | 83 | { |
84 | //initialize networking | 84 | /* Initialize networking - |
85 | //bind to ip 0.0.0.0:PORT | 85 | Bind to ip 0.0.0.0:PORT */ |
86 | IP ip; | 86 | IP ip; |
87 | ip.i = 0; | 87 | ip.i = 0; |
88 | DHT *dht = new_DHT(new_net_crypto(new_networking(ip, PORT))); | 88 | DHT *dht = new_DHT(new_net_crypto(new_networking(ip, PORT))); |
@@ -106,7 +106,7 @@ int main(int argc, char *argv[]) | |||
106 | printf("\n"); | 106 | printf("\n"); |
107 | printf("Port: %u\n", PORT); | 107 | printf("Port: %u\n", PORT); |
108 | 108 | ||
109 | perror("Initialization"); | 109 | perror("Initialization."); |
110 | 110 | ||
111 | if (argc > 3) { | 111 | if (argc > 3) { |
112 | printf("Trying to bootstrap into the network...\n"); | 112 | printf("Trying to bootstrap into the network...\n"); |
diff --git a/other/bootstrap_serverdaemon/DHT_bootstrap_daemon.c b/other/bootstrap_serverdaemon/DHT_bootstrap_daemon.c index eec4e39f..0f39285b 100644 --- a/other/bootstrap_serverdaemon/DHT_bootstrap_daemon.c +++ b/other/bootstrap_serverdaemon/DHT_bootstrap_daemon.c | |||
@@ -74,7 +74,7 @@ int b16_to_key(char b16_string[], uint8_t *bs_pubkey) | |||
74 | 74 | ||
75 | /* | 75 | /* |
76 | resolve_addr(): | 76 | resolve_addr(): |
77 | address should represent IPv4 or a hostname with A record | 77 | address should represent IPv4 or a hostname with a record |
78 | 78 | ||
79 | returns a data in network byte order that can be used to set IP.i or IP_Port.ip.i | 79 | returns a data in network byte order that can be used to set IP.i or IP_Port.ip.i |
80 | returns 0 on failure | 80 | returns 0 on failure |
@@ -91,7 +91,7 @@ uint32_t resolve_addr(const char *address) | |||
91 | 91 | ||
92 | memset(&hints, 0, sizeof(hints)); | 92 | memset(&hints, 0, sizeof(hints)); |
93 | hints.ai_family = AF_INET; // IPv4 only right now. | 93 | hints.ai_family = AF_INET; // IPv4 only right now. |
94 | hints.ai_socktype = SOCK_DGRAM; // type of socket Tox uses. | 94 | hints.ai_socktype = SOCK_DGRAM; // Type of socket Tox uses. |
95 | 95 | ||
96 | rc = getaddrinfo(address, "echo", &hints, &server); | 96 | rc = getaddrinfo(address, "echo", &hints, &server); |
97 | 97 | ||
@@ -113,7 +113,7 @@ uint32_t resolve_addr(const char *address) | |||
113 | return addr; | 113 | return addr; |
114 | } | 114 | } |
115 | 115 | ||
116 | /* This unction connects to all specified servers | 116 | /* This function connects to all specified servers |
117 | and connect to them. | 117 | and connect to them. |
118 | returns 1 if the connection to the DHT is up | 118 | returns 1 if the connection to the DHT is up |
119 | returns -1 if all attempts failed | 119 | returns -1 if all attempts failed |
@@ -178,7 +178,7 @@ void manage_keys(DHT *dht, char *keys_file) | |||
178 | load_keys(dht->c, keys); | 178 | load_keys(dht->c, keys); |
179 | 179 | ||
180 | } else { | 180 | } else { |
181 | /* otherwise save new keys */ | 181 | /* Otherwise save new keys */ |
182 | /* Silly work-around to ignore any errors coming from new_keys() */ | 182 | /* Silly work-around to ignore any errors coming from new_keys() */ |
183 | new_keys(dht->c); | 183 | new_keys(dht->c); |
184 | save_keys(dht->c, keys); | 184 | save_keys(dht->c, keys); |
@@ -273,7 +273,7 @@ struct server_conf_s configure_server(char *cfg_file) | |||
273 | 273 | ||
274 | for (i = 0; i < count; ++i) { | 274 | for (i = 0; i < count; ++i) { |
275 | config_setting_t *server = config_setting_get_elem(server_list, i); | 275 | config_setting_t *server = config_setting_get_elem(server_list, i); |
276 | /* Get a pointer on the key aray */ | 276 | /* Get a pointer on the key array */ |
277 | uint8_t *bs_pk_p = server_conf.info[i].bs_pk; | 277 | uint8_t *bs_pk_p = server_conf.info[i].bs_pk; |
278 | 278 | ||
279 | /* Only output the record if all of the expected fields are present. */ | 279 | /* Only output the record if all of the expected fields are present. */ |
@@ -288,17 +288,17 @@ struct server_conf_s configure_server(char *cfg_file) | |||
288 | 288 | ||
289 | if (resolve_addr(strcpy(tmp_ip, bs_ip)) == 0) { | 289 | if (resolve_addr(strcpy(tmp_ip, bs_ip)) == 0) { |
290 | server_conf.info[i].valid = 0; | 290 | server_conf.info[i].valid = 0; |
291 | printf("bootstrap_server %d: Invalid IP\n", i); | 291 | printf("bootstrap_server %d: Invalid IP.\n", i); |
292 | } | 292 | } |
293 | 293 | ||
294 | if (strlen(bs_pk) != 64) { | 294 | if (strlen(bs_pk) != 64) { |
295 | server_conf.info[i].valid = 0; | 295 | server_conf.info[i].valid = 0; |
296 | printf("bootstrap_server %d: Invalid public key\n", i); | 296 | printf("bootstrap_server %d: Invalid public key.\n", i); |
297 | } | 297 | } |
298 | 298 | ||
299 | if (!bs_port) { | 299 | if (!bs_port) { |
300 | server_conf.info[i].valid = 0; | 300 | server_conf.info[i].valid = 0; |
301 | printf("bootstrap_server %d: Invalid port\n", i); | 301 | printf("bootstrap_server %d: Invalid port.\n", i); |
302 | } | 302 | } |
303 | 303 | ||
304 | server_conf.info[i].conn.ip.i = resolve_addr(strcpy(tmp_ip, bs_ip)); | 304 | server_conf.info[i].conn.ip.i = resolve_addr(strcpy(tmp_ip, bs_ip)); |
@@ -337,7 +337,7 @@ int main(int argc, char *argv[]) | |||
337 | 337 | ||
338 | server_conf = configure_server(argv[1]); | 338 | server_conf = configure_server(argv[1]); |
339 | 339 | ||
340 | /* initialize networking | 340 | /* Initialize networking |
341 | bind to ip 0.0.0.0:PORT */ | 341 | bind to ip 0.0.0.0:PORT */ |
342 | IP ip; | 342 | IP ip; |
343 | ip.i = 0; | 343 | ip.i = 0; |
@@ -412,7 +412,7 @@ int main(int argc, char *argv[]) | |||
412 | /* If we got a good PID, then | 412 | /* If we got a good PID, then |
413 | we can exit the parent process. */ | 413 | we can exit the parent process. */ |
414 | if (pid > 0) { | 414 | if (pid > 0) { |
415 | printf("Forked successfully: %d\n", pid); | 415 | printf("Forked successfully: %d.\n", pid); |
416 | 416 | ||
417 | /* Write the PID file */ | 417 | /* Write the PID file */ |
418 | fprintf(pidf, "%d\n", pid); | 418 | fprintf(pidf, "%d\n", pid); |
diff --git a/other/fun/sign.c b/other/fun/sign.c index bcf54c3a..44485bb5 100644 --- a/other/fun/sign.c +++ b/other/fun/sign.c | |||
@@ -95,13 +95,13 @@ int main(int argc, char *argv[]) | |||
95 | 95 | ||
96 | if (f == NULL) | 96 | if (f == NULL) |
97 | goto fail; | 97 | goto fail; |
98 | memcpy(sm + smlen, sm, crypto_sign_ed25519_BYTES); //Move signature from beginning to end of file. | 98 | memcpy(sm + smlen, sm, crypto_sign_ed25519_BYTES); // Move signature from beginning to end of file. |
99 | 99 | ||
100 | if (fwrite(sm + (smlen - size), 1, smlen, f) != smlen) | 100 | if (fwrite(sm + (smlen - size), 1, smlen, f) != smlen) |
101 | goto fail; | 101 | goto fail; |
102 | 102 | ||
103 | fclose(f); | 103 | fclose(f); |
104 | printf("Signed successfully\n"); | 104 | printf("Signed successfully.\n"); |
105 | } | 105 | } |
106 | 106 | ||
107 | if (argc == 4 && argv[1][0] == 'c') { | 107 | if (argc == 4 && argv[1][0] == 'c') { |
@@ -113,17 +113,17 @@ int main(int argc, char *argv[]) | |||
113 | goto fail; | 113 | goto fail; |
114 | 114 | ||
115 | char *signe = malloc(size + crypto_sign_ed25519_BYTES); | 115 | char *signe = malloc(size + crypto_sign_ed25519_BYTES); |
116 | memcpy(signe, data + size - crypto_sign_ed25519_BYTES, crypto_sign_ed25519_BYTES);//Move signature from end to beginning of file. | 116 | memcpy(signe, data + size - crypto_sign_ed25519_BYTES, crypto_sign_ed25519_BYTES); // Move signature from end to beginning of file. |
117 | memcpy(signe + crypto_sign_ed25519_BYTES, data, size - crypto_sign_ed25519_BYTES); | 117 | memcpy(signe + crypto_sign_ed25519_BYTES, data, size - crypto_sign_ed25519_BYTES); |
118 | unsigned long long smlen; | 118 | unsigned long long smlen; |
119 | char *m = malloc(size); | 119 | char *m = malloc(size); |
120 | unsigned long long mlen; | 120 | unsigned long long mlen; |
121 | 121 | ||
122 | if (crypto_sign_ed25519_open(m, &mlen, signe, size, public_key) == -1) { | 122 | if (crypto_sign_ed25519_open(m, &mlen, signe, size, public_key) == -1) { |
123 | printf("Failed checking sig\n"); | 123 | printf("Failed checking sig.\n"); |
124 | goto fail; | 124 | goto fail; |
125 | } | 125 | } |
126 | printf("Checked successfully\n"); | 126 | printf("Checked successfully.\n"); |
127 | } | 127 | } |
128 | 128 | ||
129 | return 0; | 129 | return 0; |
diff --git a/toxcore/DHT.c b/toxcore/DHT.c index 533425c4..56e8b052 100644 --- a/toxcore/DHT.c +++ b/toxcore/DHT.c | |||
@@ -27,30 +27,30 @@ | |||
27 | #include "packets.h" | 27 | #include "packets.h" |
28 | #include "ping.h" | 28 | #include "ping.h" |
29 | 29 | ||
30 | /* the number of seconds for a non responsive node to become bad. */ | 30 | /* The number of seconds for a non responsive node to become bad. */ |
31 | #define BAD_NODE_TIMEOUT 70 | 31 | #define BAD_NODE_TIMEOUT 70 |
32 | 32 | ||
33 | /* the max number of nodes to send with send nodes. */ | 33 | /* The max number of nodes to send with send nodes. */ |
34 | #define MAX_SENT_NODES 8 | 34 | #define MAX_SENT_NODES 8 |
35 | 35 | ||
36 | /* ping timeout in seconds */ | 36 | /* Ping timeout in seconds */ |
37 | #define PING_TIMEOUT 5 | 37 | #define PING_TIMEOUT 5 |
38 | 38 | ||
39 | /* The timeout after which a node is discarded completely. */ | 39 | /* The timeout after which a node is discarded completely. */ |
40 | #define Kill_NODE_TIMEOUT 300 | 40 | #define Kill_NODE_TIMEOUT 300 |
41 | 41 | ||
42 | /* ping interval in seconds for each node in our lists. */ | 42 | /* Ping interval in seconds for each node in our lists. */ |
43 | #define PING_INTERVAL 60 | 43 | #define PING_INTERVAL 60 |
44 | 44 | ||
45 | /* ping interval in seconds for each random sending of a get nodes request. */ | 45 | /* Ping interval in seconds for each random sending of a get nodes request. */ |
46 | #define GET_NODE_INTERVAL 10 | 46 | #define GET_NODE_INTERVAL 10 |
47 | 47 | ||
48 | #define MAX_PUNCHING_PORTS 32 | 48 | #define MAX_PUNCHING_PORTS 32 |
49 | 49 | ||
50 | /*Interval in seconds between punching attempts*/ | 50 | /* Interval in seconds between punching attempts*/ |
51 | #define PUNCH_INTERVAL 10 | 51 | #define PUNCH_INTERVAL 10 |
52 | 52 | ||
53 | /*Ping newly announced nodes to ping per TIME_TOPING seconds*/ | 53 | /* Ping newly announced nodes to ping per TIME_TOPING seconds*/ |
54 | #define TIME_TOPING 5 | 54 | #define TIME_TOPING 5 |
55 | 55 | ||
56 | #define NAT_PING_REQUEST 0 | 56 | #define NAT_PING_REQUEST 0 |
@@ -102,9 +102,9 @@ static int is_timeout(uint64_t time_now, uint64_t timestamp, uint64_t timeout) | |||
102 | return timestamp + timeout <= time_now; | 102 | return timestamp + timeout <= time_now; |
103 | } | 103 | } |
104 | 104 | ||
105 | /* check if client with client_id is already in list of length length. | 105 | /* Check if client with client_id is already in list of length length. |
106 | * if it is then set its corresponding timestamp to current time. | 106 | * If it is then set its corresponding timestamp to current time. |
107 | * if the id is already in the list with a different ip_port, update it. | 107 | * If the id is already in the list with a different ip_port, update it. |
108 | * return True(1) or False(0) | 108 | * return True(1) or False(0) |
109 | * | 109 | * |
110 | * TODO: maybe optimize this. | 110 | * TODO: maybe optimize this. |
@@ -115,7 +115,7 @@ static int client_in_list(Client_data *list, uint32_t length, uint8_t *client_id | |||
115 | uint64_t temp_time = unix_time(); | 115 | uint64_t temp_time = unix_time(); |
116 | 116 | ||
117 | for (i = 0; i < length; ++i) { | 117 | for (i = 0; i < length; ++i) { |
118 | /*If ip_port is assigned to a different client_id replace it*/ | 118 | /* If ip_port is assigned to a different client_id replace it */ |
119 | if (ipport_equal(list[i].ip_port, ip_port)) { | 119 | if (ipport_equal(list[i].ip_port, ip_port)) { |
120 | memcpy(list[i].client_id, client_id, CLIENT_ID_SIZE); | 120 | memcpy(list[i].client_id, client_id, CLIENT_ID_SIZE); |
121 | } | 121 | } |
@@ -132,7 +132,7 @@ static int client_in_list(Client_data *list, uint32_t length, uint8_t *client_id | |||
132 | return 0; | 132 | return 0; |
133 | } | 133 | } |
134 | 134 | ||
135 | /* check if client with client_id is already in node format list of length length. | 135 | /* Check if client with client_id is already in node format list of length length. |
136 | * return True(1) or False(0) | 136 | * return True(1) or False(0) |
137 | */ | 137 | */ |
138 | static int client_in_nodelist(Node_format *list, uint32_t length, uint8_t *client_id) | 138 | static int client_in_nodelist(Node_format *list, uint32_t length, uint8_t *client_id) |
@@ -176,7 +176,7 @@ static int get_close_nodes(DHT *dht, uint8_t *client_id, Node_format *nodes_list | |||
176 | tout = is_timeout(temp_time, dht->close_clientlist[i].timestamp, BAD_NODE_TIMEOUT); | 176 | tout = is_timeout(temp_time, dht->close_clientlist[i].timestamp, BAD_NODE_TIMEOUT); |
177 | inlist = client_in_nodelist(nodes_list, MAX_SENT_NODES, dht->close_clientlist[i].client_id); | 177 | inlist = client_in_nodelist(nodes_list, MAX_SENT_NODES, dht->close_clientlist[i].client_id); |
178 | 178 | ||
179 | /* if node isn't good or is already in list. */ | 179 | /* If node isn't good or is already in list. */ |
180 | if (tout || inlist) | 180 | if (tout || inlist) |
181 | continue; | 181 | continue; |
182 | 182 | ||
@@ -216,7 +216,7 @@ static int get_close_nodes(DHT *dht, uint8_t *client_id, Node_format *nodes_list | |||
216 | MAX_SENT_NODES, | 216 | MAX_SENT_NODES, |
217 | dht->friends_list[i].client_list[j].client_id); | 217 | dht->friends_list[i].client_list[j].client_id); |
218 | 218 | ||
219 | /* if node isn't good or is already in list. */ | 219 | /* If node isn't good or is already in list. */ |
220 | if (tout || inlist) | 220 | if (tout || inlist) |
221 | continue; | 221 | continue; |
222 | 222 | ||
@@ -251,7 +251,7 @@ static int get_close_nodes(DHT *dht, uint8_t *client_id, Node_format *nodes_list | |||
251 | return num_nodes; | 251 | return num_nodes; |
252 | } | 252 | } |
253 | 253 | ||
254 | /* replace first bad (or empty) node with this one | 254 | /* Replace first bad (or empty) node with this one |
255 | * return 0 if successful | 255 | * return 0 if successful |
256 | * return 1 if not (list contains no bad nodes) | 256 | * return 1 if not (list contains no bad nodes) |
257 | */ | 257 | */ |
@@ -264,7 +264,7 @@ static int replace_bad( Client_data *list, | |||
264 | uint64_t temp_time = unix_time(); | 264 | uint64_t temp_time = unix_time(); |
265 | 265 | ||
266 | for (i = 0; i < length; ++i) { | 266 | for (i = 0; i < length; ++i) { |
267 | /* if node is bad */ | 267 | /* If node is bad */ |
268 | if (is_timeout(temp_time, list[i].timestamp, BAD_NODE_TIMEOUT)) { | 268 | if (is_timeout(temp_time, list[i].timestamp, BAD_NODE_TIMEOUT)) { |
269 | memcpy(list[i].client_id, client_id, CLIENT_ID_SIZE); | 269 | memcpy(list[i].client_id, client_id, CLIENT_ID_SIZE); |
270 | list[i].ip_port = ip_port; | 270 | list[i].ip_port = ip_port; |
@@ -278,8 +278,8 @@ static int replace_bad( Client_data *list, | |||
278 | 278 | ||
279 | return 1; | 279 | return 1; |
280 | } | 280 | } |
281 | /*Sort the list. It will be sorted from furthest to closest. | 281 | /* Sort the list. It will be sorted from furthest to closest. |
282 | TODO: this is innefficient and needs to be optimized.*/ | 282 | TODO: this is innefficient and needs to be optimized. */ |
283 | static void sort_list(Client_data *list, uint32_t length, uint8_t *comp_client_id) | 283 | static void sort_list(Client_data *list, uint32_t length, uint8_t *comp_client_id) |
284 | { | 284 | { |
285 | if (length == 0) | 285 | if (length == 0) |
@@ -305,7 +305,7 @@ static void sort_list(Client_data *list, uint32_t length, uint8_t *comp_client_i | |||
305 | } | 305 | } |
306 | 306 | ||
307 | 307 | ||
308 | /* replace the first good node that is further to the comp_client_id than that of the client_id in the list */ | 308 | /* Replace the first good node that is further to the comp_client_id than that of the client_id in the list */ |
309 | static int replace_good( Client_data *list, | 309 | static int replace_good( Client_data *list, |
310 | uint32_t length, | 310 | uint32_t length, |
311 | uint8_t *client_id, | 311 | uint8_t *client_id, |
@@ -337,12 +337,12 @@ void addto_lists(DHT *dht, IP_Port ip_port, uint8_t *client_id) | |||
337 | { | 337 | { |
338 | uint32_t i; | 338 | uint32_t i; |
339 | 339 | ||
340 | /* NOTE: current behavior if there are two clients with the same id is | 340 | /* NOTE: Current behavior if there are two clients with the same id is |
341 | * to replace the first ip by the second. | 341 | * to replace the first ip by the second. |
342 | */ | 342 | */ |
343 | if (!client_in_list(dht->close_clientlist, LCLIENT_LIST, client_id, ip_port)) { | 343 | if (!client_in_list(dht->close_clientlist, LCLIENT_LIST, client_id, ip_port)) { |
344 | if (replace_bad(dht->close_clientlist, LCLIENT_LIST, client_id, ip_port)) { | 344 | if (replace_bad(dht->close_clientlist, LCLIENT_LIST, client_id, ip_port)) { |
345 | /* if we can't replace bad nodes we try replacing good ones */ | 345 | /* If we can't replace bad nodes we try replacing good ones */ |
346 | replace_good( dht->close_clientlist, | 346 | replace_good( dht->close_clientlist, |
347 | LCLIENT_LIST, | 347 | LCLIENT_LIST, |
348 | client_id, | 348 | client_id, |
@@ -361,7 +361,7 @@ void addto_lists(DHT *dht, IP_Port ip_port, uint8_t *client_id) | |||
361 | MAX_FRIEND_CLIENTS, | 361 | MAX_FRIEND_CLIENTS, |
362 | client_id, | 362 | client_id, |
363 | ip_port )) { | 363 | ip_port )) { |
364 | /* if we can't replace bad nodes we try replacing good ones. */ | 364 | /* If we can't replace bad nodes we try replacing good ones. */ |
365 | replace_good( dht->friends_list[i].client_list, | 365 | replace_good( dht->friends_list[i].client_list, |
366 | MAX_FRIEND_CLIENTS, | 366 | MAX_FRIEND_CLIENTS, |
367 | client_id, | 367 | client_id, |
@@ -373,7 +373,7 @@ void addto_lists(DHT *dht, IP_Port ip_port, uint8_t *client_id) | |||
373 | } | 373 | } |
374 | 374 | ||
375 | /* If client_id is a friend or us, update ret_ip_port | 375 | /* If client_id is a friend or us, update ret_ip_port |
376 | * nodeclient_id is the id of the node that sent us this info | 376 | * nodeclient_id is the id of the node that sent us this info. |
377 | */ | 377 | */ |
378 | static void returnedip_ports(DHT *dht, IP_Port ip_port, uint8_t *client_id, uint8_t *nodeclient_id) | 378 | static void returnedip_ports(DHT *dht, IP_Port ip_port, uint8_t *client_id, uint8_t *nodeclient_id) |
379 | { | 379 | { |
@@ -433,7 +433,7 @@ static int is_gettingnodes(DHT *dht, IP_Port ip_port, uint64_t ping_id) | |||
433 | return 0; | 433 | return 0; |
434 | } | 434 | } |
435 | 435 | ||
436 | /* Same but for get node requests */ | 436 | /* Same but for get node requests. */ |
437 | static uint64_t add_gettingnodes(DHT *dht, IP_Port ip_port) | 437 | static uint64_t add_gettingnodes(DHT *dht, IP_Port ip_port) |
438 | { | 438 | { |
439 | uint32_t i, j; | 439 | uint32_t i, j; |
@@ -454,10 +454,10 @@ static uint64_t add_gettingnodes(DHT *dht, IP_Port ip_port) | |||
454 | return 0; | 454 | return 0; |
455 | } | 455 | } |
456 | 456 | ||
457 | /* send a getnodes request */ | 457 | /* Send a getnodes request. */ |
458 | static int getnodes(DHT *dht, IP_Port ip_port, uint8_t *public_key, uint8_t *client_id) | 458 | static int getnodes(DHT *dht, IP_Port ip_port, uint8_t *public_key, uint8_t *client_id) |
459 | { | 459 | { |
460 | /* check if packet is gonna be sent to ourself */ | 460 | /* Check if packet is going to be sent to ourself. */ |
461 | if (id_equal(public_key, dht->c->self_public_key) || is_gettingnodes(dht, ip_port, 0)) | 461 | if (id_equal(public_key, dht->c->self_public_key) || is_gettingnodes(dht, ip_port, 0)) |
462 | return 1; | 462 | return 1; |
463 | 463 | ||
@@ -493,10 +493,10 @@ static int getnodes(DHT *dht, IP_Port ip_port, uint8_t *public_key, uint8_t *cli | |||
493 | return sendpacket(dht->c->lossless_udp->net->sock, ip_port, data, sizeof(data)); | 493 | return sendpacket(dht->c->lossless_udp->net->sock, ip_port, data, sizeof(data)); |
494 | } | 494 | } |
495 | 495 | ||
496 | /* send a send nodes response */ | 496 | /* Send a send nodes response. */ |
497 | static int sendnodes(DHT *dht, IP_Port ip_port, uint8_t *public_key, uint8_t *client_id, uint64_t ping_id) | 497 | static int sendnodes(DHT *dht, IP_Port ip_port, uint8_t *public_key, uint8_t *client_id, uint64_t ping_id) |
498 | { | 498 | { |
499 | /* check if packet is gonna be sent to ourself */ | 499 | /* Check if packet is going to be sent to ourself. */ |
500 | if (id_equal(public_key, dht->c->self_public_key)) | 500 | if (id_equal(public_key, dht->c->self_public_key)) |
501 | return 1; | 501 | return 1; |
502 | 502 | ||
@@ -544,7 +544,7 @@ static int handle_getnodes(void *object, IP_Port source, uint8_t *packet, uint32 | |||
544 | + sizeof(ping_id) + CLIENT_ID_SIZE + ENCRYPTION_PADDING )) | 544 | + sizeof(ping_id) + CLIENT_ID_SIZE + ENCRYPTION_PADDING )) |
545 | return 1; | 545 | return 1; |
546 | 546 | ||
547 | /* check if packet is from ourself. */ | 547 | /* Check if packet is from ourself. */ |
548 | if (id_equal(packet + 1, dht->c->self_public_key)) | 548 | if (id_equal(packet + 1, dht->c->self_public_key)) |
549 | return 1; | 549 | return 1; |
550 | 550 | ||
@@ -563,7 +563,7 @@ static int handle_getnodes(void *object, IP_Port source, uint8_t *packet, uint32 | |||
563 | memcpy(&ping_id, plain, sizeof(ping_id)); | 563 | memcpy(&ping_id, plain, sizeof(ping_id)); |
564 | sendnodes(dht, source, packet + 1, plain + sizeof(ping_id), ping_id); | 564 | sendnodes(dht, source, packet + 1, plain + sizeof(ping_id), ping_id); |
565 | 565 | ||
566 | //send_ping_request(dht, source, (clientid_t*) (packet + 1)); /* TODO: make this smarter? */ | 566 | // send_ping_request(dht, source, (clientid_t*) (packet + 1)); /* TODO: make this smarter? */ |
567 | 567 | ||
568 | return 0; | 568 | return 0; |
569 | } | 569 | } |
@@ -618,7 +618,7 @@ static int handle_sendnodes(void *object, IP_Port source, uint8_t *packet, uint3 | |||
618 | 618 | ||
619 | int DHT_addfriend(DHT *dht, uint8_t *client_id) | 619 | int DHT_addfriend(DHT *dht, uint8_t *client_id) |
620 | { | 620 | { |
621 | if (friend_number(dht, client_id) != -1) /*Is friend already in DHT?*/ | 621 | if (friend_number(dht, client_id) != -1) /* Is friend already in DHT? */ |
622 | return 1; | 622 | return 1; |
623 | 623 | ||
624 | DHT_Friend *temp; | 624 | DHT_Friend *temp; |
@@ -709,7 +709,7 @@ static void do_DHT_friends(DHT *dht) | |||
709 | uint32_t num_nodes = 0; | 709 | uint32_t num_nodes = 0; |
710 | 710 | ||
711 | for (j = 0; j < MAX_FRIEND_CLIENTS; ++j) { | 711 | for (j = 0; j < MAX_FRIEND_CLIENTS; ++j) { |
712 | /* if node is not dead. */ | 712 | /* If node is not dead. */ |
713 | if (!is_timeout(temp_time, dht->friends_list[i].client_list[j].timestamp, Kill_NODE_TIMEOUT)) { | 713 | if (!is_timeout(temp_time, dht->friends_list[i].client_list[j].timestamp, Kill_NODE_TIMEOUT)) { |
714 | if ((dht->friends_list[i].client_list[j].last_pinged + PING_INTERVAL) <= temp_time) { | 714 | if ((dht->friends_list[i].client_list[j].last_pinged + PING_INTERVAL) <= temp_time) { |
715 | send_ping_request(dht->ping, dht->c, dht->friends_list[i].client_list[j].ip_port, | 715 | send_ping_request(dht->ping, dht->c, dht->friends_list[i].client_list[j].ip_port, |
@@ -717,7 +717,7 @@ static void do_DHT_friends(DHT *dht) | |||
717 | dht->friends_list[i].client_list[j].last_pinged = temp_time; | 717 | dht->friends_list[i].client_list[j].last_pinged = temp_time; |
718 | } | 718 | } |
719 | 719 | ||
720 | /* if node is good. */ | 720 | /* If node is good. */ |
721 | if (!is_timeout(temp_time, dht->friends_list[i].client_list[j].timestamp, BAD_NODE_TIMEOUT)) { | 721 | if (!is_timeout(temp_time, dht->friends_list[i].client_list[j].timestamp, BAD_NODE_TIMEOUT)) { |
722 | index[num_nodes] = j; | 722 | index[num_nodes] = j; |
723 | ++num_nodes; | 723 | ++num_nodes; |
@@ -747,7 +747,7 @@ static void do_Close(DHT *dht) | |||
747 | uint32_t index[LCLIENT_LIST]; | 747 | uint32_t index[LCLIENT_LIST]; |
748 | 748 | ||
749 | for (i = 0; i < LCLIENT_LIST; ++i) { | 749 | for (i = 0; i < LCLIENT_LIST; ++i) { |
750 | /* if node is not dead. */ | 750 | /* If node is not dead. */ |
751 | if (!is_timeout(temp_time, dht->close_clientlist[i].timestamp, Kill_NODE_TIMEOUT)) { | 751 | if (!is_timeout(temp_time, dht->close_clientlist[i].timestamp, Kill_NODE_TIMEOUT)) { |
752 | if ((dht->close_clientlist[i].last_pinged + PING_INTERVAL) <= temp_time) { | 752 | if ((dht->close_clientlist[i].last_pinged + PING_INTERVAL) <= temp_time) { |
753 | send_ping_request(dht->ping, dht->c, dht->close_clientlist[i].ip_port, | 753 | send_ping_request(dht->ping, dht->c, dht->close_clientlist[i].ip_port, |
@@ -755,7 +755,7 @@ static void do_Close(DHT *dht) | |||
755 | dht->close_clientlist[i].last_pinged = temp_time; | 755 | dht->close_clientlist[i].last_pinged = temp_time; |
756 | } | 756 | } |
757 | 757 | ||
758 | /* if node is good. */ | 758 | /* If node is good. */ |
759 | if (!is_timeout(temp_time, dht->close_clientlist[i].timestamp, BAD_NODE_TIMEOUT)) { | 759 | if (!is_timeout(temp_time, dht->close_clientlist[i].timestamp, BAD_NODE_TIMEOUT)) { |
760 | index[num_nodes] = i; | 760 | index[num_nodes] = i; |
761 | ++num_nodes; | 761 | ++num_nodes; |
@@ -778,8 +778,8 @@ void DHT_bootstrap(DHT *dht, IP_Port ip_port, uint8_t *public_key) | |||
778 | send_ping_request(dht->ping, dht->c, ip_port, (clientid_t *) public_key); | 778 | send_ping_request(dht->ping, dht->c, ip_port, (clientid_t *) public_key); |
779 | } | 779 | } |
780 | 780 | ||
781 | /* send the given packet to node with client_id | 781 | /* Send the given packet to node with client_id |
782 | * returns -1 if failure | 782 | * returns -1 if failure. |
783 | */ | 783 | */ |
784 | int route_packet(DHT *dht, uint8_t *client_id, uint8_t *packet, uint32_t length) | 784 | int route_packet(DHT *dht, uint8_t *client_id, uint8_t *packet, uint32_t length) |
785 | { | 785 | { |
@@ -814,7 +814,7 @@ static int friend_iplist(DHT *dht, IP_Port *ip_portlist, uint16_t friend_num) | |||
814 | for (i = 0; i < MAX_FRIEND_CLIENTS; ++i) { | 814 | for (i = 0; i < MAX_FRIEND_CLIENTS; ++i) { |
815 | client = &friend->client_list[i]; | 815 | client = &friend->client_list[i]; |
816 | 816 | ||
817 | /*If ip is not zero and node is good */ | 817 | /* If ip is not zero and node is good */ |
818 | if (client->ret_ip_port.ip.i != 0 && !is_timeout(temp_time, client->ret_timestamp, BAD_NODE_TIMEOUT)) { | 818 | if (client->ret_ip_port.ip.i != 0 && !is_timeout(temp_time, client->ret_timestamp, BAD_NODE_TIMEOUT)) { |
819 | 819 | ||
820 | if (id_equal(client->client_id, friend->client_id)) | 820 | if (id_equal(client->client_id, friend->client_id)) |
@@ -829,8 +829,8 @@ static int friend_iplist(DHT *dht, IP_Port *ip_portlist, uint16_t friend_num) | |||
829 | } | 829 | } |
830 | 830 | ||
831 | 831 | ||
832 | /* Send the following packet to everyone who tells us they are connected to friend_id | 832 | /* Send the following packet to everyone who tells us they are connected to friend_id. |
833 | * returns the number of nodes it sent the packet to | 833 | * returns the number of nodes it sent the packet to. |
834 | * | 834 | * |
835 | * Only works if more than (MAX_FRIEND_CLIENTS / 2) return an ip for friend. | 835 | * Only works if more than (MAX_FRIEND_CLIENTS / 2) return an ip for friend. |
836 | */ | 836 | */ |
@@ -856,7 +856,7 @@ int route_tofriend(DHT *dht, uint8_t *friend_id, uint8_t *packet, uint32_t lengt | |||
856 | for (i = 0; i < MAX_FRIEND_CLIENTS; ++i) { | 856 | for (i = 0; i < MAX_FRIEND_CLIENTS; ++i) { |
857 | client = &friend->client_list[i]; | 857 | client = &friend->client_list[i]; |
858 | 858 | ||
859 | /*If ip is not zero and node is good */ | 859 | /* If ip is not zero and node is good */ |
860 | if (client->ret_ip_port.ip.i != 0 && !is_timeout(temp_time, client->ret_timestamp, BAD_NODE_TIMEOUT)) { | 860 | if (client->ret_ip_port.ip.i != 0 && !is_timeout(temp_time, client->ret_timestamp, BAD_NODE_TIMEOUT)) { |
861 | if (sendpacket(dht->c->lossless_udp->net->sock, client->ip_port, packet, length) == length) | 861 | if (sendpacket(dht->c->lossless_udp->net->sock, client->ip_port, packet, length) == length) |
862 | ++sent; | 862 | ++sent; |
@@ -866,7 +866,7 @@ int route_tofriend(DHT *dht, uint8_t *friend_id, uint8_t *packet, uint32_t lengt | |||
866 | return sent; | 866 | return sent; |
867 | } | 867 | } |
868 | 868 | ||
869 | /* Send the following packet to one random person who tells us they are connected to friend_id | 869 | /* Send the following packet to one random person who tells us they are connected to friend_id. |
870 | * returns the number of nodes it sent the packet to | 870 | * returns the number of nodes it sent the packet to |
871 | */ | 871 | */ |
872 | static int routeone_tofriend(DHT *dht, uint8_t *friend_id, uint8_t *packet, uint32_t length) | 872 | static int routeone_tofriend(DHT *dht, uint8_t *friend_id, uint8_t *packet, uint32_t length) |
@@ -887,7 +887,7 @@ static int routeone_tofriend(DHT *dht, uint8_t *friend_id, uint8_t *packet, uint | |||
887 | for (i = 0; i < MAX_FRIEND_CLIENTS; ++i) { | 887 | for (i = 0; i < MAX_FRIEND_CLIENTS; ++i) { |
888 | client = &friend->client_list[i]; | 888 | client = &friend->client_list[i]; |
889 | 889 | ||
890 | /*If ip is not zero and node is good */ | 890 | /* If ip is not zero and node is good. */ |
891 | if (client->ret_ip_port.ip.i != 0 && !is_timeout(temp_time, client->ret_timestamp, BAD_NODE_TIMEOUT)) { | 891 | if (client->ret_ip_port.ip.i != 0 && !is_timeout(temp_time, client->ret_timestamp, BAD_NODE_TIMEOUT)) { |
892 | ip_list[n] = client->ip_port; | 892 | ip_list[n] = client->ip_port; |
893 | ++n; | 893 | ++n; |
@@ -903,8 +903,8 @@ static int routeone_tofriend(DHT *dht, uint8_t *friend_id, uint8_t *packet, uint | |||
903 | return 0; | 903 | return 0; |
904 | } | 904 | } |
905 | 905 | ||
906 | /* Puts all the different ips returned by the nodes for a friend_id into array ip_portlist | 906 | /* Puts all the different ips returned by the nodes for a friend_id into array ip_portlist. |
907 | * ip_portlist must be at least MAX_FRIEND_CLIENTS big | 907 | * ip_portlist must be at least MAX_FRIEND_CLIENTS big. |
908 | * returns the number of ips returned | 908 | * returns the number of ips returned |
909 | * return 0 if we are connected to friend or if no ips were found. | 909 | * return 0 if we are connected to friend or if no ips were found. |
910 | * returns -1 if no such friend | 910 | * returns -1 if no such friend |
@@ -941,9 +941,9 @@ static int send_NATping(DHT *dht, uint8_t *public_key, uint64_t ping_id, uint8_t | |||
941 | if (len == -1) | 941 | if (len == -1) |
942 | return -1; | 942 | return -1; |
943 | 943 | ||
944 | if (type == 0) /*If packet is request use many people to route it*/ | 944 | if (type == 0) /* If packet is request use many people to route it. */ |
945 | num = route_tofriend(dht, public_key, packet, len); | 945 | num = route_tofriend(dht, public_key, packet, len); |
946 | else if (type == 1) /*If packet is response use only one person to route it*/ | 946 | else if (type == 1) /* If packet is response use only one person to route it */ |
947 | num = routeone_tofriend(dht, public_key, packet, len); | 947 | num = routeone_tofriend(dht, public_key, packet, len); |
948 | 948 | ||
949 | if (num == 0) | 949 | if (num == 0) |
@@ -952,7 +952,7 @@ static int send_NATping(DHT *dht, uint8_t *public_key, uint64_t ping_id, uint8_t | |||
952 | return num; | 952 | return num; |
953 | } | 953 | } |
954 | 954 | ||
955 | /* Handle a received ping request for */ | 955 | /* Handle a received ping request for. */ |
956 | static int handle_NATping(void *object, IP_Port source, uint8_t *source_pubkey, uint8_t *packet, uint32_t length) | 956 | static int handle_NATping(void *object, IP_Port source, uint8_t *source_pubkey, uint8_t *packet, uint32_t length) |
957 | { | 957 | { |
958 | DHT *dht = object; | 958 | DHT *dht = object; |
@@ -982,10 +982,10 @@ static int handle_NATping(void *object, IP_Port source, uint8_t *source_pubkey, | |||
982 | return 1; | 982 | return 1; |
983 | } | 983 | } |
984 | 984 | ||
985 | /* Get the most common ip in the ip_portlist | 985 | /* Get the most common ip in the ip_portlist. |
986 | * Only return ip if it appears in list min_num or more | 986 | * Only return ip if it appears in list min_num or more. |
987 | * len must not be bigger than MAX_FRIEND_CLIENTS | 987 | * len must not be bigger than MAX_FRIEND_CLIENTS. |
988 | * return ip of 0 if failure | 988 | * return ip of 0 if failure. |
989 | */ | 989 | */ |
990 | static IP NAT_commonip(IP_Port *ip_portlist, uint16_t len, uint16_t min_num) | 990 | static IP NAT_commonip(IP_Port *ip_portlist, uint16_t len, uint16_t min_num) |
991 | { | 991 | { |
@@ -1010,10 +1010,10 @@ static IP NAT_commonip(IP_Port *ip_portlist, uint16_t len, uint16_t min_num) | |||
1010 | return zero; | 1010 | return zero; |
1011 | } | 1011 | } |
1012 | 1012 | ||
1013 | /* Return all the ports for one ip in a list | 1013 | /* Return all the ports for one ip in a list. |
1014 | * portlist must be at least len long | 1014 | * portlist must be at least len long |
1015 | * where len is the length of ip_portlist | 1015 | * where len is the length of ip_portlist |
1016 | * returns the number of ports and puts the list of ports in portlist | 1016 | * returns the number of ports and puts the list of ports in portlist. |
1017 | */ | 1017 | */ |
1018 | static uint16_t NAT_getports(uint16_t *portlist, IP_Port *ip_portlist, uint16_t len, IP ip) | 1018 | static uint16_t NAT_getports(uint16_t *portlist, IP_Port *ip_portlist, uint16_t len, IP ip) |
1019 | { | 1019 | { |
@@ -1039,7 +1039,7 @@ static void punch_holes(DHT *dht, IP ip, uint16_t *port_list, uint16_t numports, | |||
1039 | uint32_t top = dht->friends_list[friend_num].punching_index + MAX_PUNCHING_PORTS; | 1039 | uint32_t top = dht->friends_list[friend_num].punching_index + MAX_PUNCHING_PORTS; |
1040 | 1040 | ||
1041 | for (i = dht->friends_list[friend_num].punching_index; i != top; i++) { | 1041 | for (i = dht->friends_list[friend_num].punching_index; i != top; i++) { |
1042 | /*TODO: improve port guessing algorithm*/ | 1042 | /* TODO: improve port guessing algorithm */ |
1043 | uint16_t port = port_list[(i / 2) % numports] + (i / (2 * numports)) * ((i % 2) ? -1 : 1); | 1043 | uint16_t port = port_list[(i / 2) % numports] + (i / (2 * numports)) * ((i % 2) ? -1 : 1); |
1044 | IP_Port pinging = {ip, htons(port)}; | 1044 | IP_Port pinging = {ip, htons(port)}; |
1045 | send_ping_request(dht->ping, dht->c, pinging, (clientid_t *) &dht->friends_list[friend_num].client_id); | 1045 | send_ping_request(dht->ping, dht->c, pinging, (clientid_t *) &dht->friends_list[friend_num].client_id); |
@@ -1057,7 +1057,7 @@ static void do_NAT(DHT *dht) | |||
1057 | IP_Port ip_list[MAX_FRIEND_CLIENTS]; | 1057 | IP_Port ip_list[MAX_FRIEND_CLIENTS]; |
1058 | int num = friend_iplist(dht, ip_list, i); | 1058 | int num = friend_iplist(dht, ip_list, i); |
1059 | 1059 | ||
1060 | /*If already connected or friend is not online don't try to hole punch*/ | 1060 | /* If already connected or friend is not online don't try to hole punch */ |
1061 | if (num < MAX_FRIEND_CLIENTS / 2) | 1061 | if (num < MAX_FRIEND_CLIENTS / 2) |
1062 | continue; | 1062 | continue; |
1063 | 1063 | ||
@@ -1089,14 +1089,15 @@ static void do_NAT(DHT *dht) | |||
1089 | /*-----------------------END OF NAT PUNCHING FUNCTIONS------------------------------*/ | 1089 | /*-----------------------END OF NAT PUNCHING FUNCTIONS------------------------------*/ |
1090 | 1090 | ||
1091 | 1091 | ||
1092 | /* Add nodes to the toping list | 1092 | /* Add nodes to the toping list. |
1093 | all nodes in this list are pinged every TIME_TOPING seconds | 1093 | * All nodes in this list are pinged every TIME_TOPING seconds |
1094 | and are then removed from the list. | 1094 | * and are then removed from the list. |
1095 | if the list is full the nodes farthest from our client_id are replaced | 1095 | * If the list is full the nodes farthest from our client_id are replaced. |
1096 | the purpose of this list is to enable quick integration of new nodes into the | 1096 | * The purpose of this list is to enable quick integration of new nodes into the |
1097 | network while preventing amplification attacks. | 1097 | * network while preventing amplification attacks. |
1098 | return 0 if node was added | 1098 | * return 0 if node was added. |
1099 | return -1 if node was not added */ | 1099 | * return -1 if node was not added. |
1100 | */ | ||
1100 | int add_toping(DHT *dht, uint8_t *client_id, IP_Port ip_port) | 1101 | int add_toping(DHT *dht, uint8_t *client_id, IP_Port ip_port) |
1101 | { | 1102 | { |
1102 | if (ip_port.ip.i == 0) | 1103 | if (ip_port.ip.i == 0) |
@@ -1125,8 +1126,9 @@ int add_toping(DHT *dht, uint8_t *client_id, IP_Port ip_port) | |||
1125 | return -1; | 1126 | return -1; |
1126 | } | 1127 | } |
1127 | 1128 | ||
1128 | /*Ping all the valid nodes in the toping list every TIME_TOPING seconds | 1129 | /* Ping all the valid nodes in the toping list every TIME_TOPING seconds. |
1129 | this function must be run at least once every TIME_TOPING seconds*/ | 1130 | * This function must be run at least once every TIME_TOPING seconds |
1131 | */ | ||
1130 | static void do_toping(DHT *dht) | 1132 | static void do_toping(DHT *dht) |
1131 | { | 1133 | { |
1132 | uint64_t temp_time = unix_time(); | 1134 | uint64_t temp_time = unix_time(); |
@@ -1188,22 +1190,22 @@ void kill_DHT(DHT *dht) | |||
1188 | free(dht); | 1190 | free(dht); |
1189 | } | 1191 | } |
1190 | 1192 | ||
1191 | /* get the size of the DHT (for saving) */ | 1193 | /* Get the size of the DHT (for saving). */ |
1192 | uint32_t DHT_size(DHT *dht) | 1194 | uint32_t DHT_size(DHT *dht) |
1193 | { | 1195 | { |
1194 | return sizeof(dht->close_clientlist) + sizeof(DHT_Friend) * dht->num_friends; | 1196 | return sizeof(dht->close_clientlist) + sizeof(DHT_Friend) * dht->num_friends; |
1195 | } | 1197 | } |
1196 | 1198 | ||
1197 | /* save the DHT in data where data is an array of size DHT_size() */ | 1199 | /* Save the DHT in data where data is an array of size DHT_size(). */ |
1198 | void DHT_save(DHT *dht, uint8_t *data) | 1200 | void DHT_save(DHT *dht, uint8_t *data) |
1199 | { | 1201 | { |
1200 | memcpy(data, dht->close_clientlist, sizeof(dht->close_clientlist)); | 1202 | memcpy(data, dht->close_clientlist, sizeof(dht->close_clientlist)); |
1201 | memcpy(data + sizeof(dht->close_clientlist), dht->friends_list, sizeof(DHT_Friend) * dht->num_friends); | 1203 | memcpy(data + sizeof(dht->close_clientlist), dht->friends_list, sizeof(DHT_Friend) * dht->num_friends); |
1202 | } | 1204 | } |
1203 | 1205 | ||
1204 | /* load the DHT from data of size size; | 1206 | /* Load the DHT from data of size size. |
1205 | * return -1 if failure | 1207 | * return -1 if failure. |
1206 | * return 0 if success | 1208 | * return 0 if success. |
1207 | */ | 1209 | */ |
1208 | int DHT_load(DHT *dht, uint8_t *data, uint32_t size) | 1210 | int DHT_load(DHT *dht, uint8_t *data, uint32_t size) |
1209 | { | 1211 | { |
@@ -1247,8 +1249,8 @@ int DHT_load(DHT *dht, uint8_t *data, uint32_t size) | |||
1247 | return 0; | 1249 | return 0; |
1248 | } | 1250 | } |
1249 | 1251 | ||
1250 | /* returns 0 if we are not connected to the DHT | 1252 | /* returns 0 if we are not connected to the DHT. |
1251 | * returns 1 if we are | 1253 | * returns 1 if we are. |
1252 | */ | 1254 | */ |
1253 | int DHT_isconnected(DHT *dht) | 1255 | int DHT_isconnected(DHT *dht) |
1254 | { | 1256 | { |
diff --git a/toxcore/DHT.h b/toxcore/DHT.h index 6295581b..e4ba2154 100644 --- a/toxcore/DHT.h +++ b/toxcore/DHT.h | |||
@@ -31,21 +31,21 @@ extern "C" { | |||
31 | #endif | 31 | #endif |
32 | 32 | ||
33 | 33 | ||
34 | /* size of the client_id in bytes */ | 34 | /* Size of the client_id in bytes. */ |
35 | #define CLIENT_ID_SIZE crypto_box_PUBLICKEYBYTES | 35 | #define CLIENT_ID_SIZE crypto_box_PUBLICKEYBYTES |
36 | 36 | ||
37 | /* maximum number of clients stored per friend. */ | 37 | /* Maximum number of clients stored per friend. */ |
38 | #define MAX_FRIEND_CLIENTS 8 | 38 | #define MAX_FRIEND_CLIENTS 8 |
39 | 39 | ||
40 | /* A list of the clients mathematically closest to ours. */ | 40 | /* A list of the clients mathematically closest to ours. */ |
41 | #define LCLIENT_LIST 32 | 41 | #define LCLIENT_LIST 32 |
42 | 42 | ||
43 | /* The list of ip ports along with the ping_id of what we sent them and a timestamp */ | 43 | /* The list of ip ports along with the ping_id of what we sent them and a timestamp. */ |
44 | #define LPING_ARRAY 256 //NOTE Deprecated (doesn't do anything) | 44 | #define LPING_ARRAY 256 // NOTE: Deprecated (doesn't do anything). |
45 | 45 | ||
46 | #define LSEND_NODES_ARRAY LPING_ARRAY/2 | 46 | #define LSEND_NODES_ARRAY LPING_ARRAY/2 |
47 | 47 | ||
48 | /*Maximum newly announced nodes to ping per TIME_TOPING seconds*/ | 48 | /* Maximum newly announced nodes to ping per TIME_TOPING seconds. */ |
49 | #define MAX_TOPING 16 | 49 | #define MAX_TOPING 16 |
50 | 50 | ||
51 | typedef struct { | 51 | typedef struct { |
@@ -54,7 +54,7 @@ typedef struct { | |||
54 | uint64_t timestamp; | 54 | uint64_t timestamp; |
55 | uint64_t last_pinged; | 55 | uint64_t last_pinged; |
56 | 56 | ||
57 | /* Returned by this node. Either our friend or us */ | 57 | /* Returned by this node. Either our friend or us. */ |
58 | IP_Port ret_ip_port; | 58 | IP_Port ret_ip_port; |
59 | uint64_t ret_timestamp; | 59 | uint64_t ret_timestamp; |
60 | } Client_data; | 60 | } Client_data; |
@@ -65,10 +65,10 @@ typedef struct { | |||
65 | uint8_t client_id[CLIENT_ID_SIZE]; | 65 | uint8_t client_id[CLIENT_ID_SIZE]; |
66 | Client_data client_list[MAX_FRIEND_CLIENTS]; | 66 | Client_data client_list[MAX_FRIEND_CLIENTS]; |
67 | 67 | ||
68 | /* time at which the last get_nodes request was sent. */ | 68 | /* Time at which the last get_nodes request was sent. */ |
69 | uint64_t lastgetnode; | 69 | uint64_t lastgetnode; |
70 | 70 | ||
71 | /* Symetric NAT hole punching stuff */ | 71 | /* Symetric NAT hole punching stuff. */ |
72 | 72 | ||
73 | /* 1 if currently hole punching, otherwise 0 */ | 73 | /* 1 if currently hole punching, otherwise 0 */ |
74 | uint8_t hole_punching; | 74 | uint8_t hole_punching; |
@@ -106,82 +106,91 @@ typedef struct { | |||
106 | 106 | ||
107 | Client_data *DHT_get_close_list(DHT *dht); | 107 | Client_data *DHT_get_close_list(DHT *dht); |
108 | 108 | ||
109 | /* Add a new friend to the friends list | 109 | /* Add a new friend to the friends list. |
110 | client_id must be CLIENT_ID_SIZE bytes long. | 110 | * client_id must be CLIENT_ID_SIZE bytes long. |
111 | returns 0 if success | 111 | * returns 0 if success. |
112 | returns 1 if failure (friends list is full) */ | 112 | * returns 1 if failure (friends list is full). |
113 | */ | ||
113 | int DHT_addfriend(DHT *dht, uint8_t *client_id); | 114 | int DHT_addfriend(DHT *dht, uint8_t *client_id); |
114 | 115 | ||
115 | /* Delete a friend from the friends list | 116 | /* Delete a friend from the friends list. |
116 | client_id must be CLIENT_ID_SIZE bytes long. | 117 | * client_id must be CLIENT_ID_SIZE bytes long. |
117 | returns 0 if success | 118 | * returns 0 if success. |
118 | returns 1 if failure (client_id not in friends list) */ | 119 | * returns 1 if failure (client_id not in friends list). |
120 | */ | ||
119 | int DHT_delfriend(DHT *dht, uint8_t *client_id); | 121 | int DHT_delfriend(DHT *dht, uint8_t *client_id); |
120 | 122 | ||
121 | /* Get ip of friend | 123 | /* Get ip of friend. |
122 | client_id must be CLIENT_ID_SIZE bytes long. | 124 | * client_id must be CLIENT_ID_SIZE bytes long. |
123 | ip must be 4 bytes long. | 125 | * ip must be 4 bytes long. |
124 | port must be 2 bytes long. | 126 | * port must be 2 bytes long. |
125 | returns ip if success | 127 | * returns ip if success. |
126 | returns ip of 0 if failure (This means the friend is either offline or we have not found him yet.) | 128 | * returns ip of 0 if failure (This means the friend is either offline or we have not found him yet). |
127 | returns ip of 1 if friend is not in list. */ | 129 | * returns ip of 1 if friend is not in list. |
130 | */ | ||
128 | IP_Port DHT_getfriendip(DHT *dht, uint8_t *client_id); | 131 | IP_Port DHT_getfriendip(DHT *dht, uint8_t *client_id); |
129 | 132 | ||
130 | /* Run this function at least a couple times per second (It's the main loop) */ | 133 | /* Run this function at least a couple times per second (It's the main loop). */ |
131 | void do_DHT(DHT *dht); | 134 | void do_DHT(DHT *dht); |
132 | 135 | ||
133 | /* Use this function to bootstrap the client | 136 | /* Use this function to bootstrap the client. |
134 | Sends a get nodes request to the given node with ip port and public_key */ | 137 | * Sends a get nodes request to the given node with ip port and public_key. |
138 | */ | ||
135 | void DHT_bootstrap(DHT *dht, IP_Port ip_port, uint8_t *public_key); | 139 | void DHT_bootstrap(DHT *dht, IP_Port ip_port, uint8_t *public_key); |
136 | 140 | ||
137 | /* Add nodes to the toping list | 141 | /* Add nodes to the toping list. |
138 | all nodes in this list are pinged every TIME_TOPING seconds | 142 | * All nodes in this list are pinged every TIME_TOPING seconds |
139 | and are then removed from the list. | 143 | * and are then removed from the list. |
140 | 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. |
141 | 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 |
142 | network while preventing amplification attacks. | 146 | * network while preventing amplification attacks. |
143 | return 0 if node was added | 147 | * return 0 if node was added. |
144 | return -1 if node was not added */ | 148 | * return -1 if node was not added. |
149 | */ | ||
145 | int add_toping(DHT *dht, uint8_t *client_id, IP_Port ip_port); | 150 | int add_toping(DHT *dht, uint8_t *client_id, IP_Port ip_port); |
146 | 151 | ||
147 | /* ROUTING FUNCTIONS */ | 152 | /* ROUTING FUNCTIONS */ |
148 | 153 | ||
149 | /* send the given packet to node with client_id | 154 | /* Send the given packet to node with client_id. |
150 | returns -1 if failure */ | 155 | returns -1 if failure. */ |
151 | int route_packet(DHT *dht, uint8_t *client_id, uint8_t *packet, uint32_t length); | 156 | int route_packet(DHT *dht, uint8_t *client_id, uint8_t *packet, uint32_t length); |
152 | 157 | ||
153 | /* Send the following packet to everyone who tells us they are connected to friend_id | 158 | /* Send the following packet to everyone who tells us they are connected to friend_id. |
154 | returns the number of nodes it sent the packet to */ | 159 | * returns the number of nodes it sent the packet to. |
160 | */ | ||
155 | int route_tofriend(DHT *dht, uint8_t *friend_id, uint8_t *packet, uint32_t length); | 161 | int route_tofriend(DHT *dht, uint8_t *friend_id, uint8_t *packet, uint32_t length); |
156 | 162 | ||
157 | /* NAT PUNCHING FUNCTIONS */ | 163 | /* NAT PUNCHING FUNCTIONS */ |
158 | 164 | ||
159 | /* Puts all the different ips returned by the nodes for a friend_id into array ip_portlist | 165 | /* Puts all the different ips returned by the nodes for a friend_id into array ip_portlist. |
160 | ip_portlist must be at least MAX_FRIEND_CLIENTS big | 166 | * ip_portlist must be at least MAX_FRIEND_CLIENTS big. |
161 | returns the number of ips returned | 167 | * returns the number of ips returned. |
162 | returns -1 if no such friend*/ | 168 | * returns -1 if no such friend. |
169 | */ | ||
163 | int friend_ips(DHT *dht, IP_Port *ip_portlist, uint8_t *friend_id); | 170 | int friend_ips(DHT *dht, IP_Port *ip_portlist, uint8_t *friend_id); |
164 | 171 | ||
165 | /* SAVE/LOAD functions */ | 172 | /* SAVE/LOAD functions */ |
166 | 173 | ||
167 | /* get the size of the DHT (for saving) */ | 174 | /* Get the size of the DHT (for saving). */ |
168 | uint32_t DHT_size(DHT *dht); | 175 | uint32_t DHT_size(DHT *dht); |
169 | 176 | ||
170 | /* save the DHT in data where data is an array of size DHT_size() */ | 177 | /* Save the DHT in data where data is an array of size DHT_size(). */ |
171 | void DHT_save(DHT *dht, uint8_t *data); | 178 | void DHT_save(DHT *dht, uint8_t *data); |
172 | 179 | ||
173 | /* init DHT */ | 180 | /* Initialize DHT. */ |
174 | DHT *new_DHT(Net_Crypto *c); | 181 | DHT *new_DHT(Net_Crypto *c); |
175 | 182 | ||
176 | void kill_DHT(DHT *dht); | 183 | void kill_DHT(DHT *dht); |
177 | 184 | ||
178 | /* load the DHT from data of size size; | 185 | /* Load the DHT from data of size size. |
179 | return -1 if failure | 186 | * return -1 if failure. |
180 | return 0 if success */ | 187 | * return 0 if success. |
188 | */ | ||
181 | int DHT_load(DHT *dht, uint8_t *data, uint32_t size); | 189 | int DHT_load(DHT *dht, uint8_t *data, uint32_t size); |
182 | 190 | ||
183 | /* returns 0 if we are not connected to the DHT | 191 | /* returns 0 if we are not connected to the DHT |
184 | returns 1 if we are */ | 192 | * returns 1 if we are |
193 | */ | ||
185 | int DHT_isconnected(DHT *dht); | 194 | int DHT_isconnected(DHT *dht); |
186 | 195 | ||
187 | void addto_lists(DHT *dht, IP_Port ip_port, uint8_t *client_id); | 196 | void addto_lists(DHT *dht, IP_Port ip_port, uint8_t *client_id); |
diff --git a/toxcore/LAN_discovery.c b/toxcore/LAN_discovery.c index 49f52ce7..736959c5 100644 --- a/toxcore/LAN_discovery.c +++ b/toxcore/LAN_discovery.c | |||
@@ -26,13 +26,15 @@ | |||
26 | #define MAX_INTERFACES 16 | 26 | #define MAX_INTERFACES 16 |
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 | * returns higher than 0 on success. |
31 | * returns 0 on error */ | 31 | * returns 0 on error. |
32 | */ | ||
32 | static uint32_t get_broadcast(void) | 33 | static uint32_t get_broadcast(void) |
33 | { | 34 | { |
34 | /* not sure how many platforms this will | 35 | /* Not sure how many platforms this will run on, |
35 | * run on, so it's wrapped in __linux for now */ | 36 | * so it's wrapped in __linux for now. |
37 | */ | ||
36 | struct sockaddr_in *sock_holder = NULL; | 38 | struct sockaddr_in *sock_holder = NULL; |
37 | struct ifreq i_faces[MAX_INTERFACES]; | 39 | struct ifreq i_faces[MAX_INTERFACES]; |
38 | struct ifconf ifconf; | 40 | struct ifconf ifconf; |
@@ -40,7 +42,7 @@ static uint32_t get_broadcast(void) | |||
40 | int sock = 0; | 42 | int sock = 0; |
41 | int i = 0; | 43 | int i = 0; |
42 | 44 | ||
43 | /* configure ifconf for the ioctl call */ | 45 | /* Configure ifconf for the ioctl call. */ |
44 | if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) { | 46 | if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) { |
45 | perror("[!] get_broadcast: socket() error"); | 47 | perror("[!] get_broadcast: socket() error"); |
46 | return 0; | 48 | return 0; |
@@ -58,14 +60,14 @@ static uint32_t get_broadcast(void) | |||
58 | } | 60 | } |
59 | 61 | ||
60 | for (i = 0; i < count; i++) { | 62 | for (i = 0; i < count; i++) { |
61 | /* skip the loopback interface, as it's useless */ | 63 | /* Skip the loopback interface, as it's useless. */ |
62 | if (strcmp(i_faces[i].ifr_name, "lo") != 0) { | 64 | if (strcmp(i_faces[i].ifr_name, "lo") != 0) { |
63 | if (ioctl(sock, SIOCGIFBRDADDR, &i_faces[i]) < 0) { | 65 | if (ioctl(sock, SIOCGIFBRDADDR, &i_faces[i]) < 0) { |
64 | perror("[!] get_broadcast: ioctl error"); | 66 | perror("[!] get_broadcast: ioctl error"); |
65 | return 0; | 67 | return 0; |
66 | } | 68 | } |
67 | 69 | ||
68 | /* just to clarify where we're getting the values from */ | 70 | /* Just to clarify where we're getting the values from. */ |
69 | sock_holder = (struct sockaddr_in *)&i_faces[i].ifr_broadaddr; | 71 | sock_holder = (struct sockaddr_in *)&i_faces[i].ifr_broadaddr; |
70 | break; | 72 | break; |
71 | } | 73 | } |
@@ -82,7 +84,7 @@ static uint32_t get_broadcast(void) | |||
82 | } | 84 | } |
83 | #endif | 85 | #endif |
84 | 86 | ||
85 | /* Return the broadcast ip */ | 87 | /* Return the broadcast ip. */ |
86 | static IP broadcast_ip(void) | 88 | static IP broadcast_ip(void) |
87 | { | 89 | { |
88 | IP ip; | 90 | IP ip; |
@@ -90,7 +92,7 @@ static IP broadcast_ip(void) | |||
90 | ip.i = get_broadcast(); | 92 | ip.i = get_broadcast(); |
91 | 93 | ||
92 | if (ip.i == 0) | 94 | if (ip.i == 0) |
93 | /* error errored, but try anyway? */ | 95 | /* Error occured, but try anyway? */ |
94 | ip.i = ~0; | 96 | ip.i = ~0; |
95 | 97 | ||
96 | #else | 98 | #else |
@@ -99,23 +101,24 @@ static IP broadcast_ip(void) | |||
99 | return ip; | 101 | return ip; |
100 | } | 102 | } |
101 | 103 | ||
102 | /*return 0 if ip is a LAN ip | 104 | /* return 0 if ip is a LAN ip. |
103 | return -1 if it is not */ | 105 | * return -1 if it is not. |
106 | */ | ||
104 | static int LAN_ip(IP ip) | 107 | static int LAN_ip(IP ip) |
105 | { | 108 | { |
106 | if (ip.c[0] == 127)/* Loopback */ | 109 | if (ip.c[0] == 127) /* Loopback. */ |
107 | return 0; | 110 | return 0; |
108 | 111 | ||
109 | if (ip.c[0] == 10)/* 10.0.0.0 to 10.255.255.255 range */ | 112 | if (ip.c[0] == 10) /* 10.0.0.0 to 10.255.255.255 range. */ |
110 | return 0; | 113 | return 0; |
111 | 114 | ||
112 | if (ip.c[0] == 172 && ip.c[1] >= 16 && ip.c[1] <= 31)/* 172.16.0.0 to 172.31.255.255 range */ | 115 | if (ip.c[0] == 172 && ip.c[1] >= 16 && ip.c[1] <= 31) /* 172.16.0.0 to 172.31.255.255 range. */ |
113 | return 0; | 116 | return 0; |
114 | 117 | ||
115 | if (ip.c[0] == 192 && ip.c[1] == 168) /* 192.168.0.0 to 192.168.255.255 range */ | 118 | if (ip.c[0] == 192 && ip.c[1] == 168) /* 192.168.0.0 to 192.168.255.255 range. */ |
116 | return 0; | 119 | return 0; |
117 | 120 | ||
118 | if (ip.c[0] == 169 && ip.c[1] == 254 && ip.c[2] != 0 && ip.c[2] != 255)/* 169.254.1.0 to 169.254.254.255 range */ | 121 | if (ip.c[0] == 169 && ip.c[1] == 254 && ip.c[2] != 0 && ip.c[2] != 255)/* 169.254.1.0 to 169.254.254.255 range. */ |
119 | return 0; | 122 | return 0; |
120 | 123 | ||
121 | return -1; | 124 | return -1; |
diff --git a/toxcore/LAN_discovery.h b/toxcore/LAN_discovery.h index 5a790331..e570908e 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> |
@@ -39,11 +39,11 @@ | |||
39 | extern "C" { | 39 | extern "C" { |
40 | #endif | 40 | #endif |
41 | 41 | ||
42 | /*Send a LAN discovery pcaket to the broadcast address with port port*/ | 42 | /* Send a LAN discovery pcaket to the broadcast address with port port. */ |
43 | int send_LANdiscovery(uint16_t port, Net_Crypto *c); | 43 | int send_LANdiscovery(uint16_t port, Net_Crypto *c); |
44 | 44 | ||
45 | 45 | ||
46 | /* sets up packet handlers */ | 46 | /* Sets up packet handlers. */ |
47 | void LANdiscovery_init(DHT *dht); | 47 | void LANdiscovery_init(DHT *dht); |
48 | 48 | ||
49 | 49 | ||
diff --git a/toxcore/Lossless_UDP.c b/toxcore/Lossless_UDP.c index c30eb903..6046c09b 100644 --- a/toxcore/Lossless_UDP.c +++ b/toxcore/Lossless_UDP.c | |||
@@ -32,9 +32,9 @@ | |||
32 | /* Functions */ | 32 | /* Functions */ |
33 | 33 | ||
34 | /* | 34 | /* |
35 | * Get connection id from IP_Port | 35 | * Get connection id from IP_Port. |
36 | * Return -1 if there are no connections like we are looking for | 36 | * return -1 if there are no connections like we are looking for. |
37 | * Return id if it found it | 37 | * return id if it found it. |
38 | */ | 38 | */ |
39 | int getconnection_id(Lossless_UDP *ludp, IP_Port ip_port) | 39 | int getconnection_id(Lossless_UDP *ludp, IP_Port ip_port) |
40 | { | 40 | { |
@@ -68,16 +68,17 @@ static uint32_t handshake_id(Lossless_UDP *ludp, IP_Port source) | |||
68 | id ^= ludp->randtable[i][((uint8_t *)&source)[i]]; | 68 | id ^= ludp->randtable[i][((uint8_t *)&source)[i]]; |
69 | } | 69 | } |
70 | 70 | ||
71 | if (id == 0) /* id can't be zero */ | 71 | /* id can't be zero. */ |
72 | if (id == 0) | ||
72 | id = 1; | 73 | id = 1; |
73 | 74 | ||
74 | return id; | 75 | return id; |
75 | } | 76 | } |
76 | 77 | ||
77 | /* | 78 | /* |
78 | * Change the hanshake id associated with that ip_port | 79 | * Change the handshake id associated with that ip_port. |
79 | * | 80 | * |
80 | * TODO: make this better | 81 | * TODO: Make this better |
81 | */ | 82 | */ |
82 | static void change_handshake(Lossless_UDP *ludp, IP_Port source) | 83 | static void change_handshake(Lossless_UDP *ludp, IP_Port source) |
83 | { | 84 | { |
@@ -86,9 +87,9 @@ static void change_handshake(Lossless_UDP *ludp, IP_Port source) | |||
86 | } | 87 | } |
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 idt | 91 | * return an integer corresponding to the connection idt. |
91 | * Return -1 if it could not initialize the connectiont | 92 | * return -1 if it could not initialize the connectiont |
92 | * If there already was an existing connection to that ip_port return its number. | 93 | * If there already was an existing connection to that ip_port return its number. |
93 | */ | 94 | */ |
94 | int new_connection(Lossless_UDP *ludp, IP_Port ip_port) | 95 | int new_connection(Lossless_UDP *ludp, IP_Port ip_port) |
@@ -131,7 +132,7 @@ int new_connection(Lossless_UDP *ludp, IP_Port ip_port) | |||
131 | .last_sent = current_time(), | 132 | .last_sent = current_time(), |
132 | .killat = ~0, | 133 | .killat = ~0, |
133 | .send_counter = 0, | 134 | .send_counter = 0, |
134 | /* add randomness to timeout to prevent connections getting stuck in a loop. */ | 135 | /* Add randomness to timeout to prevent connections getting stuck in a loop. */ |
135 | .timeout = CONNEXION_TIMEOUT + rand() % CONNEXION_TIMEOUT | 136 | .timeout = CONNEXION_TIMEOUT + rand() % CONNEXION_TIMEOUT |
136 | }; | 137 | }; |
137 | ++ludp->connections_number; | 138 | ++ludp->connections_number; |
@@ -144,9 +145,9 @@ int new_connection(Lossless_UDP *ludp, IP_Port ip_port) | |||
144 | } | 145 | } |
145 | 146 | ||
146 | /* | 147 | /* |
147 | * Initialize a new inbound connection from ip_port | 148 | * Initialize a new inbound connection from ip_port. |
148 | * Returns an integer corresponding to the connection id. | 149 | * return an integer corresponding to the connection id. |
149 | * Return -1 if it could not initialize the connection. | 150 | * return -1 if it could not initialize the connection. |
150 | */ | 151 | */ |
151 | static int new_inconnection(Lossless_UDP *ludp, IP_Port ip_port) | 152 | static int new_inconnection(Lossless_UDP *ludp, IP_Port ip_port) |
152 | { | 153 | { |
@@ -182,10 +183,10 @@ static int new_inconnection(Lossless_UDP *ludp, IP_Port ip_port) | |||
182 | .last_sent = current_time(), | 183 | .last_sent = current_time(), |
183 | .send_counter = 127, | 184 | .send_counter = 127, |
184 | 185 | ||
185 | /* add randomness to timeout to prevent connections getting stuck in a loop. */ | 186 | /* Add randomness to timeout to prevent connections getting stuck in a loop. */ |
186 | .timeout = timeout, | 187 | .timeout = timeout, |
187 | 188 | ||
188 | /* if this connection isn't handled within the timeout kill it. */ | 189 | /* If this connection isn't handled within the timeout kill it. */ |
189 | .killat = current_time() + 1000000UL * timeout | 190 | .killat = current_time() + 1000000UL * timeout |
190 | }; | 191 | }; |
191 | ++ludp->connections_number; | 192 | ++ludp->connections_number; |
@@ -197,8 +198,8 @@ static int new_inconnection(Lossless_UDP *ludp, IP_Port ip_port) | |||
197 | } | 198 | } |
198 | 199 | ||
199 | /* | 200 | /* |
200 | * Returns an integer corresponding to the next connection in our incoming connection list. | 201 | * return an integer corresponding to the next connection in our incoming connection list. |
201 | * Return -1 if there are no new incoming connections in the list. | 202 | * return -1 if there are no new incoming connections in the list. |
202 | */ | 203 | */ |
203 | int incoming_connection(Lossless_UDP *ludp) | 204 | int incoming_connection(Lossless_UDP *ludp) |
204 | { | 205 | { |
@@ -244,8 +245,8 @@ static void free_connections(Lossless_UDP *ludp) | |||
244 | } | 245 | } |
245 | 246 | ||
246 | /* | 247 | /* |
247 | * Return -1 if it could not kill the connection. | 248 | * return -1 if it could not kill the connection. |
248 | * Return 0 if killed successfully | 249 | * return 0 if killed successfully. |
249 | */ | 250 | */ |
250 | int kill_connection(Lossless_UDP *ludp, int connection_id) | 251 | int kill_connection(Lossless_UDP *ludp, int connection_id) |
251 | { | 252 | { |
@@ -264,8 +265,8 @@ int kill_connection(Lossless_UDP *ludp, int connection_id) | |||
264 | 265 | ||
265 | /* | 266 | /* |
266 | * Kill connection in seconds. | 267 | * Kill connection in seconds. |
267 | * Return -1 if it can not kill the connection. | 268 | * return -1 if it can not kill the connection. |
268 | * Return 0 if it will kill it. | 269 | * return 0 if it will kill it. |
269 | */ | 270 | */ |
270 | int kill_connection_in(Lossless_UDP *ludp, int connection_id, uint32_t seconds) | 271 | int kill_connection_in(Lossless_UDP *ludp, int connection_id, uint32_t seconds) |
271 | { | 272 | { |
@@ -281,11 +282,11 @@ int kill_connection_in(Lossless_UDP *ludp, int connection_id, uint32_t seconds) | |||
281 | 282 | ||
282 | /* | 283 | /* |
283 | * Check if connection is connected: | 284 | * Check if connection is connected: |
284 | * Return 0 no. | 285 | * return 0 no. |
285 | * Return 1 if attempting handshake. | 286 | * return 1 if attempting handshake. |
286 | * Return 2 if handshake is done. | 287 | * return 2 if handshake is done. |
287 | * Return 3 if fully connected. | 288 | * return 3 if fully connected. |
288 | * Return 4 if timed out and waiting to be killed. | 289 | * return 4 if timed out and waiting to be killed. |
289 | */ | 290 | */ |
290 | int is_connected(Lossless_UDP *ludp, int connection_id) | 291 | int is_connected(Lossless_UDP *ludp, int connection_id) |
291 | { | 292 | { |
@@ -295,7 +296,7 @@ int is_connected(Lossless_UDP *ludp, int connection_id) | |||
295 | return 0; | 296 | return 0; |
296 | } | 297 | } |
297 | 298 | ||
298 | /* returns the ip_port of the corresponding connection. */ | 299 | /* return the ip_port of the corresponding connection. */ |
299 | IP_Port connection_ip(Lossless_UDP *ludp, int connection_id) | 300 | IP_Port connection_ip(Lossless_UDP *ludp, int connection_id) |
300 | { | 301 | { |
301 | if (connection_id >= 0 && connection_id < ludp->connections_length) | 302 | if (connection_id >= 0 && connection_id < ludp->connections_length) |
@@ -314,7 +315,7 @@ uint32_t sendqueue(Lossless_UDP *ludp, int connection_id) | |||
314 | return ludp->connections[connection_id].sendbuff_packetnum - ludp->connections[connection_id].successful_sent; | 315 | return ludp->connections[connection_id].sendbuff_packetnum - ludp->connections[connection_id].successful_sent; |
315 | } | 316 | } |
316 | 317 | ||
317 | /* returns the number of packets in the queue waiting to be successfully read with read_packet(...) */ | 318 | /* returns the number of packets in the queue waiting to be successfully read with read_packet(...). */ |
318 | uint32_t recvqueue(Lossless_UDP *ludp, int connection_id) | 319 | uint32_t recvqueue(Lossless_UDP *ludp, int connection_id) |
319 | { | 320 | { |
320 | if (connection_id < 0 || connection_id >= ludp->connections_length) | 321 | if (connection_id < 0 || connection_id >= ludp->connections_length) |
@@ -323,8 +324,9 @@ uint32_t recvqueue(Lossless_UDP *ludp, int connection_id) | |||
323 | return ludp->connections[connection_id].recv_packetnum - ludp->connections[connection_id].successful_read; | 324 | return ludp->connections[connection_id].recv_packetnum - ludp->connections[connection_id].successful_read; |
324 | } | 325 | } |
325 | 326 | ||
326 | /* returns the id of the next packet in the queue | 327 | /* returns the id of the next packet in the queue. |
327 | return -1 if no packet in queue */ | 328 | * return -1 if no packet in queue. |
329 | */ | ||
328 | char id_packet(Lossless_UDP *ludp, int connection_id) | 330 | char id_packet(Lossless_UDP *ludp, int connection_id) |
329 | { | 331 | { |
330 | if (connection_id < 0 || connection_id >= ludp->connections_length) | 332 | if (connection_id < 0 || connection_id >= ludp->connections_length) |
@@ -338,7 +340,8 @@ char id_packet(Lossless_UDP *ludp, int connection_id) | |||
338 | } | 340 | } |
339 | 341 | ||
340 | /* return 0 if there is no received data in the buffer. | 342 | /* return 0 if there is no received data in the buffer. |
341 | return length of received packet if successful */ | 343 | * return length of received packet if successful. |
344 | */ | ||
342 | int read_packet(Lossless_UDP *ludp, int connection_id, uint8_t *data) | 345 | int read_packet(Lossless_UDP *ludp, int connection_id, uint8_t *data) |
343 | { | 346 | { |
344 | if (recvqueue(ludp, connection_id) != 0) { | 347 | if (recvqueue(ludp, connection_id) != 0) { |
@@ -354,8 +357,8 @@ int read_packet(Lossless_UDP *ludp, int connection_id, uint8_t *data) | |||
354 | } | 357 | } |
355 | 358 | ||
356 | /* | 359 | /* |
357 | * Return 0 if data could not be put in packet queue | 360 | * return 0 if data could not be put in packet queue. |
358 | * Return 1 if data was put into the queue | 361 | * return 1 if data was put into the queue. |
359 | */ | 362 | */ |
360 | int write_packet(Lossless_UDP *ludp, int connection_id, uint8_t *data, uint32_t length) | 363 | int write_packet(Lossless_UDP *ludp, int connection_id, uint8_t *data, uint32_t length) |
361 | { | 364 | { |
@@ -373,14 +376,14 @@ int write_packet(Lossless_UDP *ludp, int connection_id, uint8_t *data, uint32_t | |||
373 | return 0; | 376 | return 0; |
374 | } | 377 | } |
375 | 378 | ||
376 | /* put the packet numbers the we are missing in requested and return the number */ | 379 | /* Put the packet numbers the we are missing in requested and return the number. */ |
377 | uint32_t missing_packets(Lossless_UDP *ludp, int connection_id, uint32_t *requested) | 380 | uint32_t missing_packets(Lossless_UDP *ludp, int connection_id, uint32_t *requested) |
378 | { | 381 | { |
379 | uint32_t number = 0; | 382 | uint32_t number = 0; |
380 | uint32_t i; | 383 | uint32_t i; |
381 | uint32_t temp; | 384 | uint32_t temp; |
382 | 385 | ||
383 | /* don't request packets if the buffer is full. */ | 386 | /* Don't request packets if the buffer is full. */ |
384 | if (recvqueue(ludp, connection_id) >= (BUFFER_PACKET_NUM - 1)) | 387 | if (recvqueue(ludp, connection_id) >= (BUFFER_PACKET_NUM - 1)) |
385 | return 0; | 388 | return 0; |
386 | 389 | ||
@@ -399,9 +402,9 @@ uint32_t missing_packets(Lossless_UDP *ludp, int connection_id, uint32_t *reques | |||
399 | } | 402 | } |
400 | 403 | ||
401 | /* | 404 | /* |
402 | * BEGIN Packet sending functions | 405 | * BEGIN Packet sending functions. |
403 | * One per packet type. | 406 | * One per packet type. |
404 | * see http://wiki.tox.im/index.php/Lossless_UDP for more information. | 407 | * See http://wiki.tox.im/index.php/Lossless_UDP for more information. |
405 | */ | 408 | */ |
406 | 409 | ||
407 | static int send_handshake(Lossless_UDP *ludp, IP_Port ip_port, uint32_t handshake_id1, uint32_t handshake_id2) | 410 | static int send_handshake(Lossless_UDP *ludp, IP_Port ip_port, uint32_t handshake_id1, uint32_t handshake_id2) |
@@ -459,7 +462,7 @@ static int send_data_packet(Lossless_UDP *ludp, uint32_t connection_id, uint32_t | |||
459 | 1 + 4 + ludp->connections[connection_id].sendbuffer[index].size); | 462 | 1 + 4 + ludp->connections[connection_id].sendbuffer[index].size); |
460 | } | 463 | } |
461 | 464 | ||
462 | /* sends 1 data packet */ | 465 | /* Sends 1 data packet. */ |
463 | static int send_DATA(Lossless_UDP *ludp, uint32_t connection_id) | 466 | static int send_DATA(Lossless_UDP *ludp, uint32_t connection_id) |
464 | { | 467 | { |
465 | int ret; | 468 | int ret; |
@@ -483,15 +486,17 @@ static int send_DATA(Lossless_UDP *ludp, uint32_t connection_id) | |||
483 | } | 486 | } |
484 | 487 | ||
485 | /* | 488 | /* |
486 | * END of packet sending functions | 489 | * END of packet sending functions. |
487 | * | 490 | * |
488 | * | 491 | * |
489 | * BEGIN Packet handling functions | 492 | * BEGIN Packet handling functions. |
490 | * One to handle each type of packets we receive | 493 | * One to handle each type of packets we receive. |
491 | */ | 494 | */ |
492 | 495 | ||
493 | 496 | ||
494 | /* Return 0 if handled correctly, 1 if packet is bad. */ | 497 | /* return 0 if handled correctly. |
498 | * return 1 if packet is bad. | ||
499 | */ | ||
495 | static int handle_handshake(void *object, IP_Port source, uint8_t *packet, uint32_t length) | 500 | static int handle_handshake(void *object, IP_Port source, uint8_t *packet, uint32_t length) |
496 | { | 501 | { |
497 | Lossless_UDP *ludp = object; | 502 | Lossless_UDP *ludp = object; |
@@ -516,11 +521,12 @@ static int handle_handshake(void *object, IP_Port source, uint8_t *packet, uint3 | |||
516 | if (is_connected(ludp, connection) != 1) | 521 | if (is_connected(ludp, connection) != 1) |
517 | return 1; | 522 | return 1; |
518 | 523 | ||
519 | /* if handshake_id2 is what we sent previously as handshake_id1 */ | 524 | /* If handshake_id2 is what we sent previously as handshake_id1. */ |
520 | if (handshake_id2 == ludp->connections[connection].handshake_id1) { | 525 | if (handshake_id2 == ludp->connections[connection].handshake_id1) { |
521 | ludp->connections[connection].status = 2; | 526 | ludp->connections[connection].status = 2; |
522 | /* NOTE: is this necessary? | 527 | /* NOTE: is this necessary? |
523 | ludp->connections[connection].handshake_id2 = handshake_id1; */ | 528 | * ludp->connections[connection].handshake_id2 = handshake_id1; |
529 | */ | ||
524 | ludp->connections[connection].orecv_packetnum = handshake_id2; | 530 | ludp->connections[connection].orecv_packetnum = handshake_id2; |
525 | ludp->connections[connection].osent_packetnum = handshake_id1; | 531 | ludp->connections[connection].osent_packetnum = handshake_id1; |
526 | ludp->connections[connection].recv_packetnum = handshake_id1; | 532 | ludp->connections[connection].recv_packetnum = handshake_id1; |
@@ -593,7 +599,7 @@ static int handle_SYNC3(Lossless_UDP *ludp, int connection_id, uint8_t counter, | |||
593 | uint32_t comp_1 = (recv_packetnum - ludp->connections[connection_id].orecv_packetnum); | 599 | uint32_t comp_1 = (recv_packetnum - ludp->connections[connection_id].orecv_packetnum); |
594 | uint32_t comp_2 = (sent_packetnum - ludp->connections[connection_id].osent_packetnum); | 600 | uint32_t comp_2 = (sent_packetnum - ludp->connections[connection_id].osent_packetnum); |
595 | 601 | ||
596 | /* packet valid */ | 602 | /* Packet valid. */ |
597 | if (comp_1 <= BUFFER_PACKET_NUM && | 603 | if (comp_1 <= BUFFER_PACKET_NUM && |
598 | comp_2 <= BUFFER_PACKET_NUM && | 604 | comp_2 <= BUFFER_PACKET_NUM && |
599 | comp_counter < 10 && comp_counter != 0) { | 605 | comp_counter < 10 && comp_counter != 0) { |
@@ -657,7 +663,9 @@ static int handle_SYNC(void *object, IP_Port source, uint8_t *packet, uint32_t l | |||
657 | 663 | ||
658 | /* | 664 | /* |
659 | * Add a packet to the received buffer and set the recv_packetnum of the | 665 | * Add a packet to the received buffer and set the recv_packetnum of the |
660 | * connection to its proper value. Return 1 if data was too big, 0 if not. | 666 | * connection to its proper value. |
667 | * return 1 if data was too big. | ||
668 | * return 0 if not. | ||
661 | */ | 669 | */ |
662 | static int add_recv(Lossless_UDP *ludp, int connection_id, uint32_t data_num, uint8_t *data, uint16_t size) | 670 | static int add_recv(Lossless_UDP *ludp, int connection_id, uint32_t data_num, uint8_t *data, uint16_t size) |
663 | { | 671 | { |
@@ -719,7 +727,7 @@ static int handle_data(void *object, IP_Port source, uint8_t *packet, uint32_t l | |||
719 | } | 727 | } |
720 | 728 | ||
721 | /* | 729 | /* |
722 | * END of packet handling functions | 730 | * END of packet handling functions. |
723 | */ | 731 | */ |
724 | 732 | ||
725 | Lossless_UDP *new_lossless_udp(Networking_Core *net) | 733 | Lossless_UDP *new_lossless_udp(Networking_Core *net) |
@@ -740,8 +748,8 @@ Lossless_UDP *new_lossless_udp(Networking_Core *net) | |||
740 | } | 748 | } |
741 | 749 | ||
742 | /* | 750 | /* |
743 | * Send handshake requests | 751 | * Send handshake requests. |
744 | * handshake packets are sent at the same rate as SYNC packets | 752 | * Handshake packets are sent at the same rate as SYNC packets. |
745 | */ | 753 | */ |
746 | static void do_new(Lossless_UDP *ludp) | 754 | static void do_new(Lossless_UDP *ludp) |
747 | { | 755 | { |
@@ -755,7 +763,7 @@ static void do_new(Lossless_UDP *ludp) | |||
755 | ludp->connections[i].last_sent = temp_time; | 763 | ludp->connections[i].last_sent = temp_time; |
756 | } | 764 | } |
757 | 765 | ||
758 | /* kill all timed out connections */ | 766 | /* Kill all timed out connections. */ |
759 | if (ludp->connections[i].status > 0 && | 767 | if (ludp->connections[i].status > 0 && |
760 | (ludp->connections[i].last_recvSYNC + ludp->connections[i].timeout * 1000000UL) < temp_time && | 768 | (ludp->connections[i].last_recvSYNC + ludp->connections[i].timeout * 1000000UL) < temp_time && |
761 | ludp->connections[i].status != 4) { | 769 | ludp->connections[i].status != 4) { |
@@ -803,7 +811,7 @@ static void do_data(Lossless_UDP *ludp) | |||
803 | /* | 811 | /* |
804 | * Automatically adjusts send rates of packets for optimal transmission. | 812 | * Automatically adjusts send rates of packets for optimal transmission. |
805 | * | 813 | * |
806 | * TODO: flow control. | 814 | * TODO: Flow control. |
807 | */ | 815 | */ |
808 | static void adjust_rates(Lossless_UDP *ludp) | 816 | static void adjust_rates(Lossless_UDP *ludp) |
809 | { | 817 | { |
diff --git a/toxcore/Lossless_UDP.h b/toxcore/Lossless_UDP.h index 176e86ce..216e95d0 100644 --- a/toxcore/Lossless_UDP.h +++ b/toxcore/Lossless_UDP.h | |||
@@ -30,22 +30,22 @@ | |||
30 | extern "C" { | 30 | extern "C" { |
31 | #endif | 31 | #endif |
32 | 32 | ||
33 | /* maximum length of the data in the data packets */ | 33 | /* Maximum length of the data in the data packets. */ |
34 | #define MAX_DATA_SIZE 1024 | 34 | #define MAX_DATA_SIZE 1024 |
35 | 35 | ||
36 | /* maximum data packets in sent and receive queues. */ | 36 | /* Maximum data packets in sent and receive queues. */ |
37 | #define MAX_QUEUE_NUM 16 | 37 | #define MAX_QUEUE_NUM 16 |
38 | 38 | ||
39 | /* maximum number of data packets in the buffer */ | 39 | /* Maximum number of data packets in the buffer. */ |
40 | #define BUFFER_PACKET_NUM (16-1) | 40 | #define BUFFER_PACKET_NUM (16-1) |
41 | 41 | ||
42 | /* timeout per connection is randomly set between CONNEXION_TIMEOUT and 2*CONNEXION_TIMEOUT */ | 42 | /* Timeout per connection is randomly set between CONNEXION_TIMEOUT and 2*CONNEXION_TIMEOUT. */ |
43 | #define CONNEXION_TIMEOUT 5 | 43 | #define CONNEXION_TIMEOUT 5 |
44 | 44 | ||
45 | /* initial amount of sync/hanshake packets to send per second. */ | 45 | /* Initial amount of sync/hanshake packets to send per second. */ |
46 | #define SYNC_RATE 2 | 46 | #define SYNC_RATE 2 |
47 | 47 | ||
48 | /* initial send rate of data. */ | 48 | /* Initial send rate of data. */ |
49 | #define DATA_SYNC_RATE 30 | 49 | #define DATA_SYNC_RATE 30 |
50 | 50 | ||
51 | typedef struct { | 51 | typedef struct { |
@@ -57,27 +57,29 @@ typedef struct { | |||
57 | IP_Port ip_port; | 57 | IP_Port ip_port; |
58 | 58 | ||
59 | /* | 59 | /* |
60 | * 0 if connection is dead, 1 if attempting handshake, | 60 | * return 0 if connection is dead. |
61 | * 2 if handshake is done (we start sending SYNC packets) | 61 | * return 1 if attempting handshake. |
62 | * 3 if we are sending SYNC packets and can send data | 62 | * return 2 if handshake is done (we start sending SYNC packets). |
63 | * 4 if the connection has timed out. | 63 | * return 3 if we are sending SYNC packets and can send data. |
64 | * return 4 if the connection has timed out. | ||
64 | */ | 65 | */ |
65 | uint8_t status; | 66 | uint8_t status; |
66 | 67 | ||
67 | /* | 68 | /* |
68 | * 1 or 2 if connection was initiated by someone else, 0 if not. | 69 | * return 0 if connection was not initiated by someone else. |
69 | * 2 if incoming_connection() has not returned it yet, 1 if it has. | 70 | * return 1 if incoming_connection() has returned. |
71 | * return 2 if it has not. | ||
70 | */ | 72 | */ |
71 | uint8_t inbound; | 73 | uint8_t inbound; |
72 | 74 | ||
73 | uint16_t SYNC_rate; /* current SYNC packet send rate packets per second. */ | 75 | uint16_t SYNC_rate; /* Current SYNC packet send rate packets per second. */ |
74 | uint16_t data_rate; /* current data packet send rate packets per second. */ | 76 | uint16_t data_rate; /* Current data packet send rate packets per second. */ |
75 | 77 | ||
76 | uint64_t last_SYNC; /* time our last SYNC packet was sent. */ | 78 | uint64_t last_SYNC; /* Time our last SYNC packet was sent. */ |
77 | uint64_t last_sent; /* time our last data or handshake packet was sent. */ | 79 | uint64_t last_sent; /* Time our last data or handshake packet was sent. */ |
78 | uint64_t last_recvSYNC; /* time we last received a SYNC packet from the other */ | 80 | uint64_t last_recvSYNC; /* Time we last received a SYNC packet from the other. */ |
79 | uint64_t last_recvdata; /* time we last received a DATA packet from the other */ | 81 | uint64_t last_recvdata; /* Time we last received a DATA packet from the other. */ |
80 | uint64_t killat; /* time to kill the connection */ | 82 | uint64_t killat; /* Time to kill the connection. */ |
81 | 83 | ||
82 | Data sendbuffer[MAX_QUEUE_NUM]; /* packet send buffer. */ | 84 | Data sendbuffer[MAX_QUEUE_NUM]; /* packet send buffer. */ |
83 | Data recvbuffer[MAX_QUEUE_NUM]; /* packet receive buffer. */ | 85 | Data recvbuffer[MAX_QUEUE_NUM]; /* packet receive buffer. */ |
@@ -85,31 +87,31 @@ typedef struct { | |||
85 | uint32_t handshake_id1; | 87 | uint32_t handshake_id1; |
86 | uint32_t handshake_id2; | 88 | uint32_t handshake_id2; |
87 | 89 | ||
88 | /* number of data packets received (also used as handshake_id1) */ | 90 | /* Number of data packets received (also used as handshake_id1). */ |
89 | uint32_t recv_packetnum; | 91 | uint32_t recv_packetnum; |
90 | 92 | ||
91 | /* number of packets received by the other peer */ | 93 | /* Number of packets received by the other peer. */ |
92 | uint32_t orecv_packetnum; | 94 | uint32_t orecv_packetnum; |
93 | 95 | ||
94 | /* number of data packets sent */ | 96 | /* Number of data packets sent. */ |
95 | uint32_t sent_packetnum; | 97 | uint32_t sent_packetnum; |
96 | 98 | ||
97 | /* number of packets sent by the other peer. */ | 99 | /* Number of packets sent by the other peer. */ |
98 | uint32_t osent_packetnum; | 100 | uint32_t osent_packetnum; |
99 | 101 | ||
100 | /* number of latest packet written onto the sendbuffer */ | 102 | /* Number of latest packet written onto the sendbuffer. */ |
101 | uint32_t sendbuff_packetnum; | 103 | uint32_t sendbuff_packetnum; |
102 | 104 | ||
103 | /* we know all packets before that number were successfully sent */ | 105 | /* We know all packets before that number were successfully sent. */ |
104 | uint32_t successful_sent; | 106 | uint32_t successful_sent; |
105 | 107 | ||
106 | /* packet number of last packet read with the read_packet function */ | 108 | /* Packet number of last packet read with the read_packet function. */ |
107 | uint32_t successful_read; | 109 | uint32_t successful_read; |
108 | 110 | ||
109 | /* list of currently requested packet numbers(by the other person) */ | 111 | /* List of currently requested packet numbers(by the other person). */ |
110 | uint32_t req_packets[BUFFER_PACKET_NUM]; | 112 | uint32_t req_packets[BUFFER_PACKET_NUM]; |
111 | 113 | ||
112 | /* total number of currently requested packets(by the other person) */ | 114 | /* Total number of currently requested packets(by the other person). */ |
113 | uint16_t num_req_paquets; | 115 | uint16_t num_req_paquets; |
114 | 116 | ||
115 | uint8_t recv_counter; | 117 | uint8_t recv_counter; |
@@ -121,96 +123,94 @@ typedef struct { | |||
121 | Networking_Core *net; | 123 | Networking_Core *net; |
122 | Connection *connections; | 124 | Connection *connections; |
123 | 125 | ||
124 | uint32_t connections_length; /* Length of connections array */ | 126 | uint32_t connections_length; /* Length of connections array. */ |
125 | uint32_t connections_number; /* Number of connections in connections array */ | 127 | uint32_t connections_number; /* Number of connections in connections array. */ |
126 | 128 | ||
127 | /* table of random numbers used in handshake_id. */ | 129 | /* Table of random numbers used in handshake_id. */ |
128 | uint32_t randtable[6][256]; | 130 | uint32_t randtable[6][256]; |
129 | 131 | ||
130 | } Lossless_UDP; | 132 | } Lossless_UDP; |
131 | 133 | ||
132 | /* | 134 | /* |
133 | * Initialize a new connection to ip_port | 135 | * Initialize a new connection to ip_port. |
134 | * Returns an integer corresponding to the connection id. | 136 | * return an integer corresponding to the connection id. |
135 | * Return -1 if it could not initialize the connection. | 137 | * return -1 if it could not initialize the connection. |
136 | * Return number if there already was an existing connection to that ip_port. | 138 | * return number if there already was an existing connection to that ip_port. |
137 | */ | 139 | */ |
138 | int new_connection(Lossless_UDP *ludp, IP_Port ip_port); | 140 | int new_connection(Lossless_UDP *ludp, IP_Port ip_port); |
139 | 141 | ||
140 | /* | 142 | /* |
141 | * Get connection id from IP_Port. | 143 | * Get connection id from IP_Port. |
142 | * Return -1 if there are no connections like we are looking for. | 144 | * return -1 if there are no connections like we are looking for. |
143 | * Return id if it found it . | 145 | * return id if it found it . |
144 | */ | 146 | */ |
145 | int getconnection_id(Lossless_UDP *ludp, IP_Port ip_port); | 147 | int getconnection_id(Lossless_UDP *ludp, IP_Port ip_port); |
146 | 148 | ||
147 | /* | 149 | /* |
148 | * Returns an int corresponding to the next connection in our imcoming connection list | 150 | * return an integer corresponding to the next connection in our imcoming connection list. |
149 | * Return -1 if there are no new incoming connections in the list. | 151 | * return -1 if there are no new incoming connections in the list. |
150 | */ | 152 | */ |
151 | int incoming_connection(Lossless_UDP *ludp); | 153 | int incoming_connection(Lossless_UDP *ludp); |
152 | 154 | ||
153 | /* | 155 | /* |
154 | * Return -1 if it could not kill the connection. | 156 | * return -1 if it could not kill the connection. |
155 | * Return 0 if killed successfully | 157 | * return 0 if killed successfully. |
156 | */ | 158 | */ |
157 | int kill_connection(Lossless_UDP *ludp, int connection_id); | 159 | int kill_connection(Lossless_UDP *ludp, int connection_id); |
158 | 160 | ||
159 | /* | 161 | /* |
160 | * Kill connection in seconds seconds. | 162 | * Kill connection in seconds seconds. |
161 | * Return -1 if it can not kill the connection. | 163 | * return -1 if it can not kill the connection. |
162 | * Return 0 if it will kill it | 164 | * return 0 if it will kill it. |
163 | */ | 165 | */ |
164 | int kill_connection_in(Lossless_UDP *ludp, int connection_id, uint32_t seconds); | 166 | int kill_connection_in(Lossless_UDP *ludp, int connection_id, uint32_t seconds); |
165 | 167 | ||
166 | /* | 168 | /* |
167 | * Returns the ip_port of the corresponding connection. | 169 | * returns the ip_port of the corresponding connection. |
168 | * Return 0 if there is no such connection. | 170 | * return 0 if there is no such connection. |
169 | */ | 171 | */ |
170 | IP_Port connection_ip(Lossless_UDP *ludp, int connection_id); | 172 | IP_Port connection_ip(Lossless_UDP *ludp, int connection_id); |
171 | 173 | ||
172 | /* | 174 | /* |
173 | * Returns the id of the next packet in the queue | 175 | * returns the id of the next packet in the queue. |
174 | * Return -1 if no packet in queue | 176 | * return -1 if no packet in queue. |
175 | */ | 177 | */ |
176 | char id_packet(Lossless_UDP *ludp, int connection_id); | 178 | char id_packet(Lossless_UDP *ludp, int connection_id); |
177 | 179 | ||
178 | /* | 180 | /* |
179 | * Return 0 if there is no received data in the buffer. | 181 | * return 0 if there is no received data in the buffer. |
180 | * Return length of received packet if successful | 182 | * return length of received packet if successful. |
181 | */ | 183 | */ |
182 | int read_packet(Lossless_UDP *ludp, int connection_id, uint8_t *data); | 184 | int read_packet(Lossless_UDP *ludp, int connection_id, uint8_t *data); |
183 | 185 | ||
184 | /* | 186 | /* |
185 | * Return 0 if data could not be put in packet queue | 187 | * return 0 if data could not be put in packet queue. |
186 | * Return 1 if data was put into the queue | 188 | * return 1 if data was put into the queue. |
187 | */ | 189 | */ |
188 | int write_packet(Lossless_UDP *ludp, int connection_id, uint8_t *data, uint32_t length); | 190 | int write_packet(Lossless_UDP *ludp, int connection_id, uint8_t *data, uint32_t length); |
189 | 191 | ||
190 | /* Returns the number of packets in the queue waiting to be successfully sent. */ | 192 | /* returns the number of packets in the queue waiting to be successfully sent. */ |
191 | uint32_t sendqueue(Lossless_UDP *ludp, int connection_id); | 193 | uint32_t sendqueue(Lossless_UDP *ludp, int connection_id); |
192 | 194 | ||
193 | /* | 195 | /* |
194 | * returns the number of packets in the queue waiting to be successfully | 196 | * return the number of packets in the queue waiting to be successfully |
195 | * read with read_packet(...) | 197 | * read with read_packet(...). |
196 | */ | 198 | */ |
197 | uint32_t recvqueue(Lossless_UDP *ludp, int connection_id); | 199 | uint32_t recvqueue(Lossless_UDP *ludp, int connection_id); |
198 | 200 | ||
199 | /* Check if connection is connected: | 201 | /* Check if connection is connected: |
200 | * Return 0 no. | 202 | * return 0 not. |
201 | * Return 1 if attempting handshake. | 203 | * return 1 if attempting handshake. |
202 | * Return 2 if handshake is done. | 204 | * return 2 if handshake is done. |
203 | * Return 3 if fully connected. | 205 | * return 3 if fully connected. |
204 | * Return 4 if timed out and wating to be killed. | 206 | * return 4 if timed out and wating to be killed. |
205 | */ | 207 | */ |
206 | int is_connected(Lossless_UDP *ludp, int connection_id); | 208 | int is_connected(Lossless_UDP *ludp, int connection_id); |
207 | 209 | ||
208 | /* Call this function a couple times per second It's the main loop. */ | 210 | /* Call this function a couple times per second It's the main loop. */ |
209 | void do_lossless_udp(Lossless_UDP *ludp); | 211 | void do_lossless_udp(Lossless_UDP *ludp); |
210 | 212 | ||
211 | /* | 213 | /* This function sets up LosslessUDP packet handling. */ |
212 | * This function sets up LosslessUDP packet handling. | ||
213 | */ | ||
214 | Lossless_UDP *new_lossless_udp(Networking_Core *net); | 214 | Lossless_UDP *new_lossless_udp(Networking_Core *net); |
215 | 215 | ||
216 | void kill_lossless_udp(Lossless_UDP *ludp); | 216 | void kill_lossless_udp(Lossless_UDP *ludp); |
diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index c5dd8a40..01b6b090 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c | |||
@@ -28,12 +28,14 @@ | |||
28 | static void set_friend_status(Messenger *m, int friendnumber, uint8_t status); | 28 | static void set_friend_status(Messenger *m, int friendnumber, uint8_t status); |
29 | static int write_cryptpacket_id(Messenger *m, int friendnumber, uint8_t packet_id, uint8_t *data, uint32_t length); | 29 | static int write_cryptpacket_id(Messenger *m, int friendnumber, uint8_t packet_id, uint8_t *data, uint32_t length); |
30 | 30 | ||
31 | /* 1 if we are online | 31 | /* return 1 if we are online. |
32 | 0 if we are offline | 32 | * return 0 if we are offline. |
33 | static uint8_t online; */ | 33 | * static uint8_t online; |
34 | */ | ||
34 | 35 | ||
35 | /* set the size of the friend list to numfriends | 36 | /* Set the size of the friend list to numfriends. |
36 | return -1 if realloc fails */ | 37 | * return -1 if realloc fails. |
38 | */ | ||
37 | int realloc_friendlist(Messenger *m, uint32_t num) | 39 | int realloc_friendlist(Messenger *m, uint32_t num) |
38 | { | 40 | { |
39 | if (num == 0) { | 41 | if (num == 0) { |
@@ -52,7 +54,8 @@ int realloc_friendlist(Messenger *m, uint32_t num) | |||
52 | } | 54 | } |
53 | 55 | ||
54 | /* return the friend id associated to that public key. | 56 | /* return the friend id associated to that public key. |
55 | return -1 if no such friend */ | 57 | * return -1 if no such friend. |
58 | */ | ||
56 | int getfriend_id(Messenger *m, uint8_t *client_id) | 59 | int getfriend_id(Messenger *m, uint8_t *client_id) |
57 | { | 60 | { |
58 | uint32_t i; | 61 | uint32_t i; |
@@ -66,10 +69,11 @@ int getfriend_id(Messenger *m, uint8_t *client_id) | |||
66 | return -1; | 69 | return -1; |
67 | } | 70 | } |
68 | 71 | ||
69 | /* copies the public key associated to that friend id into client_id buffer. | 72 | /* Copies the public key associated to that friend id into client_id buffer. |
70 | make sure that client_id is of size CLIENT_ID_SIZE. | 73 | * Make sure that client_id is of size CLIENT_ID_SIZE. |
71 | return 0 if success | 74 | * return 0 if success. |
72 | return -1 if failure. */ | 75 | * return -1 if failure. |
76 | */ | ||
73 | int getclient_id(Messenger *m, int friend_id, uint8_t *client_id) | 77 | int getclient_id(Messenger *m, int friend_id, uint8_t *client_id) |
74 | { | 78 | { |
75 | if (friend_id >= m->numfriends || friend_id < 0) | 79 | if (friend_id >= m->numfriends || friend_id < 0) |
@@ -83,7 +87,7 @@ int getclient_id(Messenger *m, int friend_id, uint8_t *client_id) | |||
83 | return -1; | 87 | return -1; |
84 | } | 88 | } |
85 | /* | 89 | /* |
86 | * returns a uint16_t that represents the checksum of address of length len | 90 | * return a uint16_t that represents the checksum of address of length len. |
87 | * | 91 | * |
88 | * TODO: Another checksum algorithm might be better. | 92 | * TODO: Another checksum algorithm might be better. |
89 | */ | 93 | */ |
@@ -101,8 +105,8 @@ static uint16_t address_checksum(uint8_t *address, uint32_t len) | |||
101 | } | 105 | } |
102 | 106 | ||
103 | /* | 107 | /* |
104 | * returns a FRIEND_ADDRESS_SIZE byte address to give to others. | 108 | * return FRIEND_ADDRESS_SIZE byte address to give to others. |
105 | * format: [client_id (32 bytes)][nospam number (4 bytes)][checksum (2 bytes)] | 109 | * Format: [client_id (32 bytes)][nospam number (4 bytes)][checksum (2 bytes)] |
106 | * | 110 | * |
107 | */ | 111 | */ |
108 | void getaddress(Messenger *m, uint8_t *address) | 112 | void getaddress(Messenger *m, uint8_t *address) |
@@ -115,20 +119,21 @@ void getaddress(Messenger *m, uint8_t *address) | |||
115 | } | 119 | } |
116 | 120 | ||
117 | /* | 121 | /* |
118 | * add a friend | 122 | * Add a friend. |
119 | * set the data that will be sent along with friend request | 123 | * Set the data that will be sent along with friend request. |
120 | * 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. | 124 | * Address is the address of the friend (returned by getaddress of the friend you wish to add) it must be FRIEND_ADDRESS_SIZE bytes. |
121 | * data is the data and length is the length | 125 | * TODO: add checksum. |
122 | * returns the friend number if success | 126 | * data is the data and length is the length. |
123 | * return FA_TOOLONG if message length is too long | 127 | * return the friend number if success. |
124 | * return FAERR_NOMESSAGE if no message (message length must be >= 1 byte) | 128 | * return FA_TOOLONG if message length is too long. |
125 | * return FAERR_OWNKEY if user's own key | 129 | * return FAERR_NOMESSAGE if no message (message length must be >= 1 byte). |
126 | * return FAERR_ALREADYSENT if friend request already sent or already a friend | 130 | * return FAERR_OWNKEY if user's own key. |
127 | * return FAERR_UNKNOWN for unknown error | 131 | * return FAERR_ALREADYSENT if friend request already sent or already a friend. |
128 | * return FAERR_BADCHECKSUM if bad checksum in address | 132 | * return FAERR_UNKNOWN for unknown error. |
129 | * return FAERR_SETNEWNOSPAM if the friend was already there but the nospam was different | 133 | * return FAERR_BADCHECKSUM if bad checksum in address. |
130 | * (the nospam for that friend was set to the new one) | 134 | * return FAERR_SETNEWNOSPAM if the friend was already there but the nospam was different. |
131 | * return FAERR_NOMEM if increasing the friend list size fails | 135 | * (the nospam for that friend was set to the new one). |
136 | * return FAERR_NOMEM if increasing the friend list size fails. | ||
132 | */ | 137 | */ |
133 | int m_addfriend(Messenger *m, uint8_t *address, uint8_t *data, uint16_t length) | 138 | int m_addfriend(Messenger *m, uint8_t *address, uint8_t *data, uint16_t length) |
134 | { | 139 | { |
@@ -164,7 +169,7 @@ int m_addfriend(Messenger *m, uint8_t *address, uint8_t *data, uint16_t length) | |||
164 | return FAERR_SETNEWNOSPAM; | 169 | return FAERR_SETNEWNOSPAM; |
165 | } | 170 | } |
166 | 171 | ||
167 | /* resize the friend list if necessary */ | 172 | /* Resize the friend list if necessary. */ |
168 | if (realloc_friendlist(m, m->numfriends + 1) != 0) | 173 | if (realloc_friendlist(m, m->numfriends + 1) != 0) |
169 | return FAERR_NOMEM; | 174 | return FAERR_NOMEM; |
170 | 175 | ||
@@ -186,7 +191,7 @@ int m_addfriend(Messenger *m, uint8_t *address, uint8_t *data, uint16_t length) | |||
186 | memcpy(m->friendlist[i].info, data, length); | 191 | memcpy(m->friendlist[i].info, data, length); |
187 | m->friendlist[i].info_size = length; | 192 | m->friendlist[i].info_size = length; |
188 | m->friendlist[i].message_id = 0; | 193 | m->friendlist[i].message_id = 0; |
189 | m->friendlist[i].receives_read_receipts = 1; /* default: YES */ | 194 | m->friendlist[i].receives_read_receipts = 1; /* Default: YES. */ |
190 | memcpy(&(m->friendlist[i].friendrequest_nospam), address + crypto_box_PUBLICKEYBYTES, sizeof(uint32_t)); | 195 | memcpy(&(m->friendlist[i].friendrequest_nospam), address + crypto_box_PUBLICKEYBYTES, sizeof(uint32_t)); |
191 | 196 | ||
192 | if (m->numfriends == i) | 197 | if (m->numfriends == i) |
@@ -204,7 +209,7 @@ int m_addfriend_norequest(Messenger *m, uint8_t *client_id) | |||
204 | if (getfriend_id(m, client_id) != -1) | 209 | if (getfriend_id(m, client_id) != -1) |
205 | return -1; | 210 | return -1; |
206 | 211 | ||
207 | /* resize the friend list if necessary */ | 212 | /* Resize the friend list if necessary. */ |
208 | if (realloc_friendlist(m, m->numfriends + 1) != 0) | 213 | if (realloc_friendlist(m, m->numfriends + 1) != 0) |
209 | return FAERR_NOMEM; | 214 | return FAERR_NOMEM; |
210 | 215 | ||
@@ -223,7 +228,7 @@ int m_addfriend_norequest(Messenger *m, uint8_t *client_id) | |||
223 | m->friendlist[i].statusmessage_length = 1; | 228 | m->friendlist[i].statusmessage_length = 1; |
224 | m->friendlist[i].userstatus = USERSTATUS_NONE; | 229 | m->friendlist[i].userstatus = USERSTATUS_NONE; |
225 | m->friendlist[i].message_id = 0; | 230 | m->friendlist[i].message_id = 0; |
226 | m->friendlist[i].receives_read_receipts = 1; /* default: YES */ | 231 | m->friendlist[i].receives_read_receipts = 1; /* Default: YES. */ |
227 | 232 | ||
228 | if (m->numfriends == i) | 233 | if (m->numfriends == i) |
229 | ++ m->numfriends; | 234 | ++ m->numfriends; |
@@ -235,9 +240,10 @@ int m_addfriend_norequest(Messenger *m, uint8_t *client_id) | |||
235 | return -1; | 240 | return -1; |
236 | } | 241 | } |
237 | 242 | ||
238 | /* remove a friend | 243 | /* Remove a friend. |
239 | return 0 if success | 244 | * return 0 if success. |
240 | return -1 if failure */ | 245 | * return -1 if failure. |
246 | */ | ||
241 | int m_delfriend(Messenger *m, int friendnumber) | 247 | int m_delfriend(Messenger *m, int friendnumber) |
242 | { | 248 | { |
243 | if (friendnumber >= m->numfriends || friendnumber < 0) | 249 | if (friendnumber >= m->numfriends || friendnumber < 0) |
@@ -262,11 +268,12 @@ int m_delfriend(Messenger *m, int friendnumber) | |||
262 | return 0; | 268 | return 0; |
263 | } | 269 | } |
264 | 270 | ||
265 | /* return FRIEND_ONLINE if friend is online | 271 | /* return FRIEND_ONLINE if friend is online. |
266 | return FRIEND_CONFIRMED if friend is confirmed | 272 | * return FRIEND_CONFIRMED if friend is confirmed. |
267 | return FRIEND_REQUESTED if the friend request was sent | 273 | * return FRIEND_REQUESTED if the friend request was sent. |
268 | return FRIEND_ADDED if the friend was added | 274 | * return FRIEND_ADDED if the friend was added. |
269 | return NOFRIEND if there is no friend with that number */ | 275 | * return NOFRIEND if there is no friend with that number. |
276 | */ | ||
270 | int m_friendstatus(Messenger *m, int friendnumber) | 277 | int m_friendstatus(Messenger *m, int friendnumber) |
271 | { | 278 | { |
272 | if (friendnumber < 0 || friendnumber >= m->numfriends) | 279 | if (friendnumber < 0 || friendnumber >= m->numfriends) |
@@ -275,9 +282,10 @@ int m_friendstatus(Messenger *m, int friendnumber) | |||
275 | return m->friendlist[friendnumber].status; | 282 | return m->friendlist[friendnumber].status; |
276 | } | 283 | } |
277 | 284 | ||
278 | /* send a text chat message to an online friend | 285 | /* Send a text chat message to an online friend. |
279 | return the message id if packet was successfully put into the send queue | 286 | * return the message id if packet was successfully put into the send queue. |
280 | return 0 if it was not */ | 287 | * return 0 if it was not. |
288 | */ | ||
281 | uint32_t m_sendmessage(Messenger *m, int friendnumber, uint8_t *message, uint32_t length) | 289 | uint32_t m_sendmessage(Messenger *m, int friendnumber, uint8_t *message, uint32_t length) |
282 | { | 290 | { |
283 | if (friendnumber < 0 || friendnumber >= m->numfriends) | 291 | if (friendnumber < 0 || friendnumber >= m->numfriends) |
@@ -286,7 +294,7 @@ uint32_t m_sendmessage(Messenger *m, int friendnumber, uint8_t *message, uint32_ | |||
286 | uint32_t msgid = ++m->friendlist[friendnumber].message_id; | 294 | uint32_t msgid = ++m->friendlist[friendnumber].message_id; |
287 | 295 | ||
288 | if (msgid == 0) | 296 | if (msgid == 0) |
289 | msgid = 1; /* otherwise, false error */ | 297 | msgid = 1; // Otherwise, false error |
290 | 298 | ||
291 | if (m_sendmessage_withid(m, friendnumber, msgid, message, length)) { | 299 | if (m_sendmessage_withid(m, friendnumber, msgid, message, length)) { |
292 | return msgid; | 300 | return msgid; |
@@ -307,16 +315,18 @@ uint32_t m_sendmessage_withid(Messenger *m, int friendnumber, uint32_t theid, ui | |||
307 | return write_cryptpacket_id(m, friendnumber, PACKET_ID_MESSAGE, temp, length + sizeof(theid)); | 315 | return write_cryptpacket_id(m, friendnumber, PACKET_ID_MESSAGE, temp, length + sizeof(theid)); |
308 | } | 316 | } |
309 | 317 | ||
310 | /* send an action to an online friend | 318 | /* Send an action to an online friend. |
311 | return 1 if packet was successfully put into the send queue | 319 | * return 1 if packet was successfully put into the send queue. |
312 | return 0 if it was not */ | 320 | * return 0 if it was not. |
321 | */ | ||
313 | int m_sendaction(Messenger *m, int friendnumber, uint8_t *action, uint32_t length) | 322 | int m_sendaction(Messenger *m, int friendnumber, uint8_t *action, uint32_t length) |
314 | { | 323 | { |
315 | return write_cryptpacket_id(m, friendnumber, PACKET_ID_ACTION, action, length); | 324 | return write_cryptpacket_id(m, friendnumber, PACKET_ID_ACTION, action, length); |
316 | } | 325 | } |
317 | 326 | ||
318 | /* send a name packet to friendnumber | 327 | /* Send a name packet to friendnumber. |
319 | length is the length with the NULL terminator*/ | 328 | * length is the length with the NULL terminator. |
329 | */ | ||
320 | static int m_sendname(Messenger *m, int friendnumber, uint8_t *name, uint16_t length) | 330 | static int m_sendname(Messenger *m, int friendnumber, uint8_t *name, uint16_t length) |
321 | { | 331 | { |
322 | if (length > MAX_NAME_LENGTH || length == 0) | 332 | if (length > MAX_NAME_LENGTH || length == 0) |
@@ -325,9 +335,10 @@ static int m_sendname(Messenger *m, int friendnumber, uint8_t *name, uint16_t le | |||
325 | return write_cryptpacket_id(m, friendnumber, PACKET_ID_NICKNAME, name, length); | 335 | return write_cryptpacket_id(m, friendnumber, PACKET_ID_NICKNAME, name, length); |
326 | } | 336 | } |
327 | 337 | ||
328 | /* set the name of a friend | 338 | /* Set the name of a friend. |
329 | return 0 if success | 339 | * return 0 if success. |
330 | return -1 if failure */ | 340 | * return -1 if failure. |
341 | */ | ||
331 | static int setfriendname(Messenger *m, int friendnumber, uint8_t *name) | 342 | static int setfriendname(Messenger *m, int friendnumber, uint8_t *name) |
332 | { | 343 | { |
333 | if (friendnumber >= m->numfriends || friendnumber < 0) | 344 | if (friendnumber >= m->numfriends || friendnumber < 0) |
@@ -338,11 +349,12 @@ static int setfriendname(Messenger *m, int friendnumber, uint8_t *name) | |||
338 | } | 349 | } |
339 | 350 | ||
340 | /* Set our nickname | 351 | /* Set our nickname |
341 | name must be a string of maximum MAX_NAME_LENGTH length. | 352 | * name must be a string of maximum MAX_NAME_LENGTH length. |
342 | length must be at least 1 byte | 353 | * length must be at least 1 byte. |
343 | length is the length of name with the NULL terminator | 354 | * length is the length of name with the NULL terminator. |
344 | return 0 if success | 355 | * return 0 if success. |
345 | return -1 if failure */ | 356 | * return -1 if failure. |
357 | */ | ||
346 | int setname(Messenger *m, uint8_t *name, uint16_t length) | 358 | int setname(Messenger *m, uint8_t *name, uint16_t length) |
347 | { | 359 | { |
348 | if (length > MAX_NAME_LENGTH || length == 0) | 360 | if (length > MAX_NAME_LENGTH || length == 0) |
@@ -358,10 +370,10 @@ int setname(Messenger *m, uint8_t *name, uint16_t length) | |||
358 | return 0; | 370 | return 0; |
359 | } | 371 | } |
360 | 372 | ||
361 | /* get our nickname | 373 | /* Get our nickname and put it in name. |
362 | put it in name | 374 | * Name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH bytes. |
363 | name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH bytes. | 375 | * return the length of the name. |
364 | return the length of the name */ | 376 | */ |
365 | uint16_t getself_name(Messenger *m, uint8_t *name, uint16_t nlen) | 377 | uint16_t getself_name(Messenger *m, uint8_t *name, uint16_t nlen) |
366 | { | 378 | { |
367 | uint16_t len; | 379 | uint16_t len; |
@@ -376,11 +388,11 @@ uint16_t getself_name(Messenger *m, uint8_t *name, uint16_t nlen) | |||
376 | return len; | 388 | return len; |
377 | } | 389 | } |
378 | 390 | ||
379 | /* get name of friendnumber | 391 | /* Get name of friendnumber and put it in name. |
380 | put it in name | 392 | * 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. | 393 | * return 0 if success. |
382 | return 0 if success | 394 | * return -1 if failure. |
383 | return -1 if failure */ | 395 | */ |
384 | int getname(Messenger *m, int friendnumber, uint8_t *name) | 396 | int getname(Messenger *m, int friendnumber, uint8_t *name) |
385 | { | 397 | { |
386 | if (friendnumber >= m->numfriends || friendnumber < 0) | 398 | if (friendnumber >= m->numfriends || friendnumber < 0) |
@@ -421,8 +433,9 @@ int m_set_userstatus(Messenger *m, USERSTATUS status) | |||
421 | return 0; | 433 | return 0; |
422 | } | 434 | } |
423 | 435 | ||
424 | /* return the size of friendnumber's user status | 436 | /* return the size of friendnumber's user status. |
425 | guaranteed to be at most MAX_STATUSMESSAGE_LENGTH */ | 437 | * Guaranteed to be at most MAX_STATUSMESSAGE_LENGTH. |
438 | */ | ||
426 | int m_get_statusmessage_size(Messenger *m, int friendnumber) | 439 | int m_get_statusmessage_size(Messenger *m, int friendnumber) |
427 | { | 440 | { |
428 | if (friendnumber >= m->numfriends || friendnumber < 0) | 441 | if (friendnumber >= m->numfriends || friendnumber < 0) |
@@ -431,8 +444,9 @@ int m_get_statusmessage_size(Messenger *m, int friendnumber) | |||
431 | return m->friendlist[friendnumber].statusmessage_length; | 444 | return m->friendlist[friendnumber].statusmessage_length; |
432 | } | 445 | } |
433 | 446 | ||
434 | /* copy the user status of friendnumber into buf, truncating if needed to maxlen | 447 | /* Copy the user status of friendnumber into buf, truncating if needed to maxlen |
435 | bytes, use m_get_statusmessage_size to find out how much you need to allocate */ | 448 | * bytes, use m_get_statusmessage_size to find out how much you need to allocate. |
449 | */ | ||
436 | int m_copy_statusmessage(Messenger *m, int friendnumber, uint8_t *buf, uint32_t maxlen) | 450 | int m_copy_statusmessage(Messenger *m, int friendnumber, uint8_t *buf, uint32_t maxlen) |
437 | { | 451 | { |
438 | if (friendnumber >= m->numfriends || friendnumber < 0) | 452 | if (friendnumber >= m->numfriends || friendnumber < 0) |
@@ -517,13 +531,13 @@ void m_set_sends_receipts(Messenger *m, int friendnumber, int yesno) | |||
517 | } | 531 | } |
518 | 532 | ||
519 | /* static void (*friend_request)(uint8_t *, uint8_t *, uint16_t); */ | 533 | /* static void (*friend_request)(uint8_t *, uint8_t *, uint16_t); */ |
520 | /* set the function that will be executed when a friend request is received. */ | 534 | /* Set the function that will be executed when a friend request is received. */ |
521 | void m_callback_friendrequest(Messenger *m, void (*function)(uint8_t *, uint8_t *, uint16_t, void *), void *userdata) | 535 | void m_callback_friendrequest(Messenger *m, void (*function)(uint8_t *, uint8_t *, uint16_t, void *), void *userdata) |
522 | { | 536 | { |
523 | callback_friendrequest(&(m->fr), function, userdata); | 537 | callback_friendrequest(&(m->fr), function, userdata); |
524 | } | 538 | } |
525 | 539 | ||
526 | /* set the function that will be executed when a message from a friend is received. */ | 540 | /* Set the function that will be executed when a message from a friend is received. */ |
527 | void m_callback_friendmessage(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t, void *), | 541 | void m_callback_friendmessage(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t, void *), |
528 | void *userdata) | 542 | void *userdata) |
529 | { | 543 | { |
@@ -607,11 +621,11 @@ int write_cryptpacket_id(Messenger *m, int friendnumber, uint8_t packet_id, uint | |||
607 | return write_cryptpacket(m->net_crypto, m->friendlist[friendnumber].crypt_connection_id, packet, length + 1); | 621 | return write_cryptpacket(m->net_crypto, m->friendlist[friendnumber].crypt_connection_id, packet, length + 1); |
608 | } | 622 | } |
609 | 623 | ||
610 | /*Interval in seconds between LAN discovery packet sending*/ | 624 | /* Interval in seconds between LAN discovery packet sending. */ |
611 | #define LAN_DISCOVERY_INTERVAL 60 | 625 | #define LAN_DISCOVERY_INTERVAL 60 |
612 | #define PORT 33445 | 626 | #define PORT 33445 |
613 | 627 | ||
614 | /*Send a LAN discovery packet every LAN_DISCOVERY_INTERVAL seconds*/ | 628 | /* Send a LAN discovery packet every LAN_DISCOVERY_INTERVAL seconds. */ |
615 | static void LANdiscovery(Messenger *m) | 629 | static void LANdiscovery(Messenger *m) |
616 | { | 630 | { |
617 | if (m->last_LANdiscovery + LAN_DISCOVERY_INTERVAL < unix_time()) { | 631 | if (m->last_LANdiscovery + LAN_DISCOVERY_INTERVAL < unix_time()) { |
@@ -620,7 +634,7 @@ static void LANdiscovery(Messenger *m) | |||
620 | } | 634 | } |
621 | } | 635 | } |
622 | 636 | ||
623 | /* run this at startup */ | 637 | /* Run this at startup. */ |
624 | Messenger *initMessenger(void) | 638 | Messenger *initMessenger(void) |
625 | { | 639 | { |
626 | Messenger *m = calloc(1, sizeof(Messenger)); | 640 | Messenger *m = calloc(1, sizeof(Messenger)); |
@@ -664,11 +678,11 @@ Messenger *initMessenger(void) | |||
664 | return m; | 678 | return m; |
665 | } | 679 | } |
666 | 680 | ||
667 | /* run this before closing shop */ | 681 | /* Run this before closing shop. */ |
668 | void cleanupMessenger(Messenger *m) | 682 | void cleanupMessenger(Messenger *m) |
669 | { | 683 | { |
670 | /* FIXME TODO ideally cleanupMessenger will mirror initMessenger | 684 | /* FIXME TODO: ideally cleanupMessenger will mirror initMessenger. |
671 | * this requires the other modules to expose cleanup functions | 685 | * This requires the other modules to expose cleanup functions. |
672 | */ | 686 | */ |
673 | kill_DHT(m->dht); | 687 | kill_DHT(m->dht); |
674 | kill_net_crypto(m->net_crypto); | 688 | kill_net_crypto(m->net_crypto); |
@@ -677,10 +691,10 @@ void cleanupMessenger(Messenger *m) | |||
677 | free(m); | 691 | free(m); |
678 | } | 692 | } |
679 | 693 | ||
680 | //TODO: make this function not suck. | 694 | /* TODO: Make this function not suck. */ |
681 | void doFriends(Messenger *m) | 695 | void doFriends(Messenger *m) |
682 | { | 696 | { |
683 | /* TODO: add incoming connections and some other stuff. */ | 697 | /* TODO: Add incoming connections and some other stuff. */ |
684 | uint32_t i; | 698 | uint32_t i; |
685 | int len; | 699 | int len; |
686 | uint8_t temp[MAX_DATA_SIZE]; | 700 | uint8_t temp[MAX_DATA_SIZE]; |
@@ -699,14 +713,16 @@ void doFriends(Messenger *m) | |||
699 | } | 713 | } |
700 | 714 | ||
701 | if (m->friendlist[i].status == FRIEND_REQUESTED | 715 | if (m->friendlist[i].status == FRIEND_REQUESTED |
702 | || m->friendlist[i].status == FRIEND_CONFIRMED) { /* friend is not online */ | 716 | || m->friendlist[i].status == FRIEND_CONFIRMED) { /* friend is not online. */ |
703 | if (m->friendlist[i].status == FRIEND_REQUESTED) { | 717 | if (m->friendlist[i].status == FRIEND_REQUESTED) { |
704 | /* If we didn't connect to friend after successfully sending him a friend request the request is deemed | 718 | /* If we didn't connect to friend after successfully sending him a friend request the request is deemed |
705 | unsuccessful so we set the status back to FRIEND_ADDED and try again.*/ | 719 | * unsuccessful so we set the status back to FRIEND_ADDED and try again. |
720 | */ | ||
706 | if (m->friendlist[i].friendrequest_lastsent + m->friendlist[i].friendrequest_timeout < temp_time) { | 721 | if (m->friendlist[i].friendrequest_lastsent + m->friendlist[i].friendrequest_timeout < temp_time) { |
707 | set_friend_status(m, i, FRIEND_ADDED); | 722 | set_friend_status(m, i, FRIEND_ADDED); |
708 | /* Double the default timeout everytime if friendrequest is assumed to have been | 723 | /* Double the default timeout everytime if friendrequest is assumed to have been |
709 | sent unsuccessfully. */ | 724 | * sent unsuccessfully. |
725 | */ | ||
710 | m->friendlist[i].friendrequest_timeout *= 2; | 726 | m->friendlist[i].friendrequest_timeout *= 2; |
711 | } | 727 | } |
712 | } | 728 | } |
@@ -720,7 +736,7 @@ void doFriends(Messenger *m) | |||
720 | 736 | ||
721 | break; | 737 | break; |
722 | 738 | ||
723 | case 3: /* Connection is established */ | 739 | case 3: /* Connection is established. */ |
724 | set_friend_status(m, i, FRIEND_ONLINE); | 740 | set_friend_status(m, i, FRIEND_ONLINE); |
725 | m->friendlist[i].name_sent = 0; | 741 | m->friendlist[i].name_sent = 0; |
726 | m->friendlist[i].userstatus_sent = 0; | 742 | m->friendlist[i].userstatus_sent = 0; |
@@ -738,7 +754,7 @@ void doFriends(Messenger *m) | |||
738 | } | 754 | } |
739 | } | 755 | } |
740 | 756 | ||
741 | while (m->friendlist[i].status == FRIEND_ONLINE) { /* friend is online */ | 757 | while (m->friendlist[i].status == FRIEND_ONLINE) { /* friend is online. */ |
742 | if (m->friendlist[i].name_sent == 0) { | 758 | if (m->friendlist[i].name_sent == 0) { |
743 | if (m_sendname(m, i, m->name, m->name_length)) | 759 | if (m_sendname(m, i, m->name, m->name_length)) |
744 | m->friendlist[i].name_sent = 1; | 760 | m->friendlist[i].name_sent = 1; |
@@ -778,7 +794,7 @@ void doFriends(Messenger *m) | |||
778 | m->friend_namechange(m, i, data, data_length, m->friend_namechange_userdata); | 794 | m->friend_namechange(m, i, data, data_length, m->friend_namechange_userdata); |
779 | 795 | ||
780 | memcpy(m->friendlist[i].name, data, data_length); | 796 | memcpy(m->friendlist[i].name, data, data_length); |
781 | m->friendlist[i].name[data_length - 1] = 0; /* make sure the NULL terminator is present. */ | 797 | m->friendlist[i].name[data_length - 1] = 0; /* Make sure the NULL terminator is present. */ |
782 | break; | 798 | break; |
783 | } | 799 | } |
784 | 800 | ||
@@ -851,7 +867,7 @@ void doFriends(Messenger *m) | |||
851 | } | 867 | } |
852 | } else { | 868 | } else { |
853 | if (is_cryptoconnected(m->net_crypto, | 869 | if (is_cryptoconnected(m->net_crypto, |
854 | m->friendlist[i].crypt_connection_id) == 4) { /* if the connection timed out, kill it */ | 870 | m->friendlist[i].crypt_connection_id) == 4) { /* If the connection timed out, kill it. */ |
855 | crypto_kill(m->net_crypto, m->friendlist[i].crypt_connection_id); | 871 | crypto_kill(m->net_crypto, m->friendlist[i].crypt_connection_id); |
856 | m->friendlist[i].crypt_connection_id = -1; | 872 | m->friendlist[i].crypt_connection_id = -1; |
857 | set_friend_status(m, i, FRIEND_CONFIRMED); | 873 | set_friend_status(m, i, FRIEND_CONFIRMED); |
@@ -861,7 +877,7 @@ void doFriends(Messenger *m) | |||
861 | } | 877 | } |
862 | 878 | ||
863 | if (m->friendlist[i].ping_lastrecv + FRIEND_CONNECTION_TIMEOUT < temp_time) { | 879 | if (m->friendlist[i].ping_lastrecv + FRIEND_CONNECTION_TIMEOUT < temp_time) { |
864 | /* if we stopped recieving ping packets kill it */ | 880 | /* If we stopped recieving ping packets, kill it. */ |
865 | crypto_kill(m->net_crypto, m->friendlist[i].crypt_connection_id); | 881 | crypto_kill(m->net_crypto, m->friendlist[i].crypt_connection_id); |
866 | m->friendlist[i].crypt_connection_id = -1; | 882 | m->friendlist[i].crypt_connection_id = -1; |
867 | set_friend_status(m, i, FRIEND_CONFIRMED); | 883 | set_friend_status(m, i, FRIEND_CONFIRMED); |
@@ -890,7 +906,7 @@ void doInbound(Messenger *m) | |||
890 | } | 906 | } |
891 | } | 907 | } |
892 | 908 | ||
893 | /* the main loop that needs to be run at least 20 times per second. */ | 909 | /* The main loop that needs to be run at least 20 times per second. */ |
894 | void doMessenger(Messenger *m) | 910 | void doMessenger(Messenger *m) |
895 | { | 911 | { |
896 | networking_poll(m->net); | 912 | networking_poll(m->net); |
@@ -902,21 +918,21 @@ void doMessenger(Messenger *m) | |||
902 | LANdiscovery(m); | 918 | LANdiscovery(m); |
903 | } | 919 | } |
904 | 920 | ||
905 | /* returns the size of the messenger data (for saving) */ | 921 | /* return the size of the messenger data (for saving) */ |
906 | uint32_t Messenger_size(Messenger *m) | 922 | uint32_t Messenger_size(Messenger *m) |
907 | { | 923 | { |
908 | return crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES | 924 | return crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES |
909 | + sizeof(uint32_t) // nospam | 925 | + sizeof(uint32_t) // nospam. |
910 | + sizeof(uint32_t) // DHT size | 926 | + sizeof(uint32_t) // DHT size. |
911 | + DHT_size(m->dht) // DHT itself | 927 | + DHT_size(m->dht) // DHT itself. |
912 | + sizeof(uint32_t) // Friendlist size | 928 | + sizeof(uint32_t) // Friendlist size. |
913 | + sizeof(Friend) * m->numfriends // Friendlist itself | 929 | + sizeof(Friend) * m->numfriends // Friendlist itself. |
914 | + sizeof(uint16_t) // Own nickname length | 930 | + sizeof(uint16_t) // Own nickname length. |
915 | + m->name_length // Own nickname | 931 | + m->name_length // Own nickname. |
916 | ; | 932 | ; |
917 | } | 933 | } |
918 | 934 | ||
919 | /* save the messenger in data of size Messenger_size() */ | 935 | /* Save the messenger in data of size Messenger_size(). */ |
920 | void Messenger_save(Messenger *m, uint8_t *data) | 936 | void Messenger_save(Messenger *m, uint8_t *data) |
921 | { | 937 | { |
922 | save_keys(m->net_crypto, data); | 938 | save_keys(m->net_crypto, data); |
@@ -940,7 +956,7 @@ void Messenger_save(Messenger *m, uint8_t *data) | |||
940 | memcpy(data, m->name, small_size); | 956 | memcpy(data, m->name, small_size); |
941 | } | 957 | } |
942 | 958 | ||
943 | /* load the messenger from data of size length. */ | 959 | /* Load the messenger from data of size length. */ |
944 | int Messenger_load(Messenger *m, uint8_t *data, uint32_t length) | 960 | int Messenger_load(Messenger *m, uint8_t *data, uint32_t length) |
945 | { | 961 | { |
946 | if (length == ~0) | 962 | if (length == ~0) |
@@ -988,7 +1004,7 @@ int Messenger_load(Messenger *m, uint8_t *data, uint32_t length) | |||
988 | setfriendname(m, fnum, temp[i].name); | 1004 | setfriendname(m, fnum, temp[i].name); |
989 | /* set_friend_statusmessage(fnum, temp[i].statusmessage, temp[i].statusmessage_length); */ | 1005 | /* set_friend_statusmessage(fnum, temp[i].statusmessage, temp[i].statusmessage_length); */ |
990 | } else if (temp[i].status != 0) { | 1006 | } else if (temp[i].status != 0) { |
991 | /* TODO: this is not a good way to do this. */ | 1007 | /* TODO: This is not a good way to do this. */ |
992 | uint8_t address[FRIEND_ADDRESS_SIZE]; | 1008 | uint8_t address[FRIEND_ADDRESS_SIZE]; |
993 | memcpy(address, temp[i].client_id, crypto_box_PUBLICKEYBYTES); | 1009 | memcpy(address, temp[i].client_id, crypto_box_PUBLICKEYBYTES); |
994 | memcpy(address + crypto_box_PUBLICKEYBYTES, &(temp[i].friendrequest_nospam), sizeof(uint32_t)); | 1010 | memcpy(address + crypto_box_PUBLICKEYBYTES, &(temp[i].friendrequest_nospam), sizeof(uint32_t)); |
diff --git a/toxcore/Messenger.h b/toxcore/Messenger.h index e808529f..b588fb4b 100644 --- a/toxcore/Messenger.h +++ b/toxcore/Messenger.h | |||
@@ -49,7 +49,7 @@ extern "C" { | |||
49 | #define PACKET_ID_ACTION 63 | 49 | #define PACKET_ID_ACTION 63 |
50 | 50 | ||
51 | 51 | ||
52 | /* status definitions */ | 52 | /* Status definitions. */ |
53 | enum { | 53 | enum { |
54 | NOFRIEND, | 54 | NOFRIEND, |
55 | FRIEND_ADDED, | 55 | FRIEND_ADDED, |
@@ -58,8 +58,9 @@ enum { | |||
58 | FRIEND_ONLINE, | 58 | FRIEND_ONLINE, |
59 | }; | 59 | }; |
60 | 60 | ||
61 | /* errors for m_addfriend | 61 | /* Errors for m_addfriend |
62 | * FAERR - Friend Add Error */ | 62 | * FAERR - Friend Add Error |
63 | */ | ||
63 | enum { | 64 | enum { |
64 | FAERR_TOOLONG = -1, | 65 | FAERR_TOOLONG = -1, |
65 | FAERR_NOMESSAGE = -2, | 66 | FAERR_NOMESSAGE = -2, |
@@ -71,20 +72,22 @@ enum { | |||
71 | FAERR_NOMEM = -8 | 72 | FAERR_NOMEM = -8 |
72 | }; | 73 | }; |
73 | 74 | ||
74 | /* don't assume MAX_STATUSMESSAGE_LENGTH will stay at 128, it may be increased | 75 | /* Don't assume MAX_STATUSMESSAGE_LENGTH will stay at 128, it may be increased |
75 | to an absurdly large number later */ | 76 | * to an absurdly large number later. |
77 | */ | ||
76 | 78 | ||
77 | /* Default start timeout in seconds between friend requests */ | 79 | /* Default start timeout in seconds between friend requests. */ |
78 | #define FRIENDREQUEST_TIMEOUT 5; | 80 | #define FRIENDREQUEST_TIMEOUT 5; |
79 | 81 | ||
80 | /* interval between the sending of ping packets.*/ | 82 | /* Interval between the sending of ping packets. */ |
81 | #define FRIEND_PING_INTERVAL 5 | 83 | #define FRIEND_PING_INTERVAL 5 |
82 | 84 | ||
83 | /* If no packets are recieved from friend in this time interval, kill the connection.*/ | 85 | /* If no packets are recieved from friend in this time interval, kill the connection. */ |
84 | #define FRIEND_CONNECTION_TIMEOUT (FRIEND_PING_INTERVAL * 2) | 86 | #define FRIEND_CONNECTION_TIMEOUT (FRIEND_PING_INTERVAL * 2) |
85 | 87 | ||
86 | /* USERSTATUS | 88 | /* USERSTATUS - |
87 | * Represents userstatuses someone can have. */ | 89 | * Represents userstatuses someone can have. |
90 | */ | ||
88 | 91 | ||
89 | typedef enum { | 92 | typedef enum { |
90 | USERSTATUS_NONE, | 93 | USERSTATUS_NONE, |
@@ -97,21 +100,21 @@ USERSTATUS; | |||
97 | typedef struct { | 100 | typedef struct { |
98 | uint8_t client_id[CLIENT_ID_SIZE]; | 101 | uint8_t client_id[CLIENT_ID_SIZE]; |
99 | int crypt_connection_id; | 102 | int crypt_connection_id; |
100 | uint64_t friendrequest_lastsent; /* time at which the last friend request was sent. */ | 103 | uint64_t friendrequest_lastsent; // Time at which the last friend request was sent. |
101 | uint32_t friendrequest_timeout; /* The timeout between successful friendrequest sending attempts */ | 104 | uint32_t friendrequest_timeout; // The timeout between successful friendrequest sending attempts. |
102 | uint8_t status; /* 0 if no friend, 1 if added, 2 if friend request sent, 3 if confirmed friend, 4 if online. */ | 105 | uint8_t status; // 0 if no friend, 1 if added, 2 if friend request sent, 3 if confirmed friend, 4 if online. |
103 | uint8_t info[MAX_DATA_SIZE]; /* the data that is sent during the friend requests we do */ | 106 | uint8_t info[MAX_DATA_SIZE]; // the data that is sent during the friend requests we do. |
104 | uint8_t name[MAX_NAME_LENGTH]; | 107 | uint8_t name[MAX_NAME_LENGTH]; |
105 | uint8_t name_sent; /* 0 if we didn't send our name to this friend 1 if we have. */ | 108 | uint8_t name_sent; // 0 if we didn't send our name to this friend 1 if we have. |
106 | uint8_t *statusmessage; | 109 | uint8_t *statusmessage; |
107 | uint16_t statusmessage_length; | 110 | uint16_t statusmessage_length; |
108 | uint8_t statusmessage_sent; | 111 | uint8_t statusmessage_sent; |
109 | USERSTATUS userstatus; | 112 | USERSTATUS userstatus; |
110 | uint8_t userstatus_sent; | 113 | uint8_t userstatus_sent; |
111 | uint16_t info_size; /* length of the info */ | 114 | uint16_t info_size; // Length of the info. |
112 | uint32_t message_id; /* a semi-unique id used in read receipts */ | 115 | uint32_t message_id; // a semi-unique id used in read receipts. |
113 | uint8_t receives_read_receipts; /* shall we send read receipts to this person? */ | 116 | uint8_t receives_read_receipts; // shall we send read receipts to this person? |
114 | uint32_t friendrequest_nospam; /*The nospam number used in the friend request*/ | 117 | uint32_t friendrequest_nospam; // The nospam number used in the friend request. |
115 | uint64_t ping_lastrecv; | 118 | uint64_t ping_lastrecv; |
116 | uint64_t ping_lastsent; | 119 | uint64_t ping_lastsent; |
117 | } Friend; | 120 | } Friend; |
@@ -156,192 +159,221 @@ typedef struct Messenger { | |||
156 | } Messenger; | 159 | } Messenger; |
157 | 160 | ||
158 | /* | 161 | /* |
159 | * returns a FRIEND_ADDRESS_SIZE byte address to give to others. | 162 | * return FRIEND_ADDRESS_SIZE byte address to give to others. |
160 | * format: [client_id (32 bytes)][nospam number (4 bytes)][checksum (2 bytes)] | 163 | * Format: [client_id (32 bytes)][nospam number (4 bytes)][checksum (2 bytes)] |
161 | * | 164 | * |
162 | */ | 165 | */ |
163 | void getaddress(Messenger *m, uint8_t *address); | 166 | void getaddress(Messenger *m, uint8_t *address); |
164 | 167 | ||
165 | /* | 168 | /* |
166 | * add a friend | 169 | * Add a friend. |
167 | * set the data that will be sent along with friend request | 170 | * Set the data that will be sent along with friend request. |
168 | * 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. | 171 | * 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. |
169 | * data is the data and length is the length | 172 | * data is the data and length is the length. |
170 | * returns the friend number if success | 173 | * returns the friend number if success. |
171 | * return -1 if message length is too long | 174 | * return -1 if message length is too long. |
172 | * return -2 if no message (message length must be >= 1 byte) | 175 | * return -2 if no message (message length must be >= 1 byte). |
173 | * return -3 if user's own key | 176 | * return -3 if user's own key. |
174 | * return -4 if friend request already sent or already a friend | 177 | * return -4 if friend request already sent or already a friend. |
175 | * return -5 for unknown error | 178 | * return -5 for unknown error. |
176 | * return -6 if bad checksum in address | 179 | * return -6 if bad checksum in address. |
177 | * return -7 if the friend was already there but the nospam was different | 180 | * return -7 if the friend was already there but the nospam was different. |
178 | * (the nospam for that friend was set to the new one) | 181 | * (the nospam for that friend was set to the new one). |
179 | * return -8 if increasing the friend list size fails | 182 | * return -8 if increasing the friend list size fails. |
180 | */ | 183 | */ |
181 | int m_addfriend(Messenger *m, uint8_t *address, uint8_t *data, uint16_t length); | 184 | int m_addfriend(Messenger *m, uint8_t *address, uint8_t *data, uint16_t length); |
182 | 185 | ||
183 | 186 | ||
184 | /* add a friend without sending a friendrequest. | 187 | /* Add a friend without sending a friendrequest. |
185 | returns the friend number if success | 188 | * return the friend number if success. |
186 | return -1 if failure. */ | 189 | * return -1 if failure. |
190 | */ | ||
187 | int m_addfriend_norequest(Messenger *m, uint8_t *client_id); | 191 | int m_addfriend_norequest(Messenger *m, uint8_t *client_id); |
188 | 192 | ||
189 | /* return the friend id associated to that client id. | 193 | /* return the friend id associated to that client id. |
190 | return -1 if no such friend */ | 194 | * return -1 if no such friend. |
195 | */ | ||
191 | int getfriend_id(Messenger *m, uint8_t *client_id); | 196 | int getfriend_id(Messenger *m, uint8_t *client_id); |
192 | 197 | ||
193 | /* copies the public key associated to that friend id into client_id buffer. | 198 | /* Copies the public key associated to that friend id into client_id buffer. |
194 | make sure that client_id is of size CLIENT_ID_SIZE. | 199 | * Make sure that client_id is of size CLIENT_ID_SIZE. |
195 | return 0 if success | 200 | * return 0 if success |
196 | return -1 if failure */ | 201 | * return -1 if failure |
202 | */ | ||
197 | int getclient_id(Messenger *m, int friend_id, uint8_t *client_id); | 203 | int getclient_id(Messenger *m, int friend_id, uint8_t *client_id); |
198 | 204 | ||
199 | /* remove a friend */ | 205 | /* Remove a friend. */ |
200 | int m_delfriend(Messenger *m, int friendnumber); | 206 | int m_delfriend(Messenger *m, int friendnumber); |
201 | 207 | ||
202 | /* return 4 if friend is online | 208 | /* return 4 if friend is online. |
203 | return 3 if friend is confirmed | 209 | * return 3 if friend is confirmed. |
204 | return 2 if the friend request was sent | 210 | * return 2 if the friend request was sent. |
205 | return 1 if the friend was added | 211 | * return 1 if the friend was added. |
206 | return 0 if there is no friend with that number */ | 212 | * return 0 if there is no friend with that number. |
213 | */ | ||
207 | int m_friendstatus(Messenger *m, int friendnumber); | 214 | int m_friendstatus(Messenger *m, int friendnumber); |
208 | 215 | ||
209 | /* send a text chat message to an online friend | 216 | /* Send a text chat message to an online friend. |
210 | returns the message id if packet was successfully put into the send queue | 217 | * return the message id if packet was successfully put into the send queue. |
211 | return 0 if it was not | 218 | * return 0 if it was not. |
212 | you will want to retain the return value, it will be passed to your read receipt callback | 219 | * |
213 | if one is received. | 220 | * You will want to retain the return value, it will be passed to your read receipt callback |
214 | m_sendmessage_withid will send a message with the id of your choosing, | 221 | * if one is received. |
215 | however we can generate an id for you by calling plain m_sendmessage. */ | 222 | * m_sendmessage_withid will send a message with the id of your choosing, |
223 | * however we can generate an id for you by calling plain m_sendmessage. | ||
224 | */ | ||
216 | uint32_t m_sendmessage(Messenger *m, int friendnumber, uint8_t *message, uint32_t length); | 225 | uint32_t m_sendmessage(Messenger *m, int friendnumber, uint8_t *message, uint32_t length); |
217 | uint32_t m_sendmessage_withid(Messenger *m, int friendnumber, uint32_t theid, uint8_t *message, uint32_t length); | 226 | uint32_t m_sendmessage_withid(Messenger *m, int friendnumber, uint32_t theid, uint8_t *message, uint32_t length); |
218 | 227 | ||
219 | /* send an action to an online friend | 228 | /* Send an action to an online friend. |
220 | returns 1 if packet was successfully put into the send queue | 229 | * return 1 if packet was successfully put into the send queue. |
221 | return 0 if it was not */ | 230 | * return 0 if it was not. |
231 | */ | ||
222 | int m_sendaction(Messenger *m, int friendnumber, uint8_t *action, uint32_t length); | 232 | int m_sendaction(Messenger *m, int friendnumber, uint8_t *action, uint32_t length); |
223 | 233 | ||
224 | /* Set our nickname | 234 | /* Set our nickname. |
225 | name must be a string of maximum MAX_NAME_LENGTH length. | 235 | * name must be a string of maximum MAX_NAME_LENGTH length. |
226 | length must be at least 1 byte | 236 | * length must be at least 1 byte. |
227 | length is the length of name with the NULL terminator | 237 | * length is the length of name with the NULL terminator. |
228 | return 0 if success | 238 | * return 0 if success. |
229 | return -1 if failure */ | 239 | * return -1 if failure. |
240 | */ | ||
230 | int setname(Messenger *m, uint8_t *name, uint16_t length); | 241 | int setname(Messenger *m, uint8_t *name, uint16_t length); |
231 | 242 | ||
232 | /* | 243 | /* |
233 | Get your nickname. | 244 | * Get your nickname. |
234 | m The messanger context to use. | 245 | * m - The messanger context to use. |
235 | name Pointer to a string for the name. | 246 | * name - Pointer to a string for the name. |
236 | nlen The length of the string buffer. | 247 | * nlen - The length of the string buffer. |
237 | returns Return the length of the name, 0 on error. | 248 | * return length of the name. |
238 | */ | 249 | * return 0 on error. |
250 | */ | ||
239 | uint16_t getself_name(Messenger *m, uint8_t *name, uint16_t nlen); | 251 | uint16_t getself_name(Messenger *m, uint8_t *name, uint16_t nlen); |
240 | 252 | ||
241 | /* get name of friendnumber | 253 | /* Get name of friendnumber and put it in name. |
242 | put it in name | 254 | * name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH (128) bytes. |
243 | name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH (128) bytes. | 255 | * return 0 if success. |
244 | return 0 if success | 256 | * return -1 if failure. |
245 | return -1 if failure */ | 257 | */ |
246 | int getname(Messenger *m, int friendnumber, uint8_t *name); | 258 | int getname(Messenger *m, int friendnumber, uint8_t *name); |
247 | 259 | ||
248 | /* set our user status | 260 | /* Set our user status. |
249 | you are responsible for freeing status after | 261 | * You are responsible for freeing status after. |
250 | returns 0 on success, -1 on failure */ | 262 | * returns 0 on success. |
263 | * returns -1 on failure. | ||
264 | */ | ||
251 | int m_set_statusmessage(Messenger *m, uint8_t *status, uint16_t length); | 265 | int m_set_statusmessage(Messenger *m, uint8_t *status, uint16_t length); |
252 | int m_set_userstatus(Messenger *m, USERSTATUS status); | 266 | int m_set_userstatus(Messenger *m, USERSTATUS status); |
253 | 267 | ||
254 | /* return the length of friendnumber's status message, | 268 | /* return the length of friendnumber's status message, |
255 | including null | 269 | * including null. |
256 | pass it into malloc */ | 270 | * Pass it into malloc. |
271 | */ | ||
257 | int m_get_statusmessage_size(Messenger *m, int friendnumber); | 272 | int m_get_statusmessage_size(Messenger *m, int friendnumber); |
258 | 273 | ||
259 | /* copy friendnumber's status message into buf, truncating if size is over maxlen | 274 | /* Copy friendnumber's status message into buf, truncating if size is over maxlen. |
260 | get the size you need to allocate from m_get_statusmessage_size | 275 | * Get the size you need to allocate from m_get_statusmessage_size. |
261 | The self variant will copy our own status message. */ | 276 | * The self variant will copy our own status message. |
277 | */ | ||
262 | int m_copy_statusmessage(Messenger *m, int friendnumber, uint8_t *buf, uint32_t maxlen); | 278 | int m_copy_statusmessage(Messenger *m, int friendnumber, uint8_t *buf, uint32_t maxlen); |
263 | int m_copy_self_statusmessage(Messenger *m, uint8_t *buf, uint32_t maxlen); | 279 | int m_copy_self_statusmessage(Messenger *m, uint8_t *buf, uint32_t maxlen); |
264 | 280 | ||
265 | /* Return one of USERSTATUS values. | 281 | /* return one of USERSTATUS values. |
266 | * Values unknown to your application should be represented as USERSTATUS_NONE. | 282 | * Values unknown to your application should be represented as USERSTATUS_NONE. |
267 | * As above, the self variant will return our own USERSTATUS. | 283 | * As above, the self variant will return our own USERSTATUS. |
268 | * If friendnumber is invalid, this shall return USERSTATUS_INVALID. */ | 284 | * If friendnumber is invalid, this shall return USERSTATUS_INVALID. |
285 | */ | ||
269 | USERSTATUS m_get_userstatus(Messenger *m, int friendnumber); | 286 | USERSTATUS m_get_userstatus(Messenger *m, int friendnumber); |
270 | USERSTATUS m_get_self_userstatus(Messenger *m); | 287 | USERSTATUS m_get_self_userstatus(Messenger *m); |
271 | 288 | ||
272 | /* Sets whether we send read receipts for friendnumber. | 289 | /* Sets whether we send read receipts for friendnumber. |
273 | * This function is not lazy, and it will fail if yesno is not (0 or 1).*/ | 290 | * This function is not lazy, and it will fail if yesno is not (0 or 1). |
291 | */ | ||
274 | void m_set_sends_receipts(Messenger *m, int friendnumber, int yesno); | 292 | void m_set_sends_receipts(Messenger *m, int friendnumber, int yesno); |
275 | 293 | ||
276 | /* set the function that will be executed when a friend request is received. | 294 | /* Set the function that will be executed when a friend request is received. |
277 | function format is function(uint8_t * public_key, uint8_t * data, uint16_t length) */ | 295 | * Function format is function(uint8_t * public_key, uint8_t * data, uint16_t length) |
296 | */ | ||
278 | void m_callback_friendrequest(Messenger *m, void (*function)(uint8_t *, uint8_t *, uint16_t, void *), void *userdata); | 297 | void m_callback_friendrequest(Messenger *m, void (*function)(uint8_t *, uint8_t *, uint16_t, void *), void *userdata); |
279 | 298 | ||
280 | /* set the function that will be executed when a message from a friend is received. | 299 | /* Set the function that will be executed when a message from a friend is received. |
281 | function format is: function(int friendnumber, uint8_t * message, uint32_t length) */ | 300 | * Function format is: function(int friendnumber, uint8_t * message, uint32_t length) |
301 | */ | ||
282 | void m_callback_friendmessage(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t, void *), | 302 | void m_callback_friendmessage(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t, void *), |
283 | void *userdata); | 303 | void *userdata); |
284 | 304 | ||
285 | /* set the function that will be executed when an action from a friend is received. | 305 | /* Set the function that will be executed when an action from a friend is received. |
286 | function format is: function(int friendnumber, uint8_t * action, uint32_t length) */ | 306 | * function format is: function(int friendnumber, uint8_t * action, uint32_t length) |
307 | */ | ||
287 | void m_callback_action(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t, void *), void *userdata); | 308 | void m_callback_action(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t, void *), void *userdata); |
288 | 309 | ||
289 | /* set the callback for name changes | 310 | /* Set the callback for name changes. |
290 | function(int friendnumber, uint8_t *newname, uint16_t length) | 311 | * Function(int friendnumber, uint8_t *newname, uint16_t length) |
291 | you are not responsible for freeing newname */ | 312 | * You are not responsible for freeing newname. |
313 | */ | ||
292 | void m_callback_namechange(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t, void *), | 314 | void m_callback_namechange(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t, void *), |
293 | void *userdata); | 315 | void *userdata); |
294 | 316 | ||
295 | /* set the callback for status message changes | 317 | /* Set the callback for status message changes. |
296 | function(int friendnumber, uint8_t *newstatus, uint16_t length) | 318 | * function(int friendnumber, uint8_t *newstatus, uint16_t length) |
297 | you are not responsible for freeing newstatus */ | 319 | * |
320 | * You are not responsible for freeing newstatus | ||
321 | */ | ||
298 | void m_callback_statusmessage(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t, void *), | 322 | void m_callback_statusmessage(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t, void *), |
299 | void *userdata); | 323 | void *userdata); |
300 | 324 | ||
301 | /* set the callback for status type changes | 325 | /* Set the callback for status type changes. |
302 | function(int friendnumber, USERSTATUS kind) */ | 326 | * function(int friendnumber, USERSTATUS kind) |
327 | */ | ||
303 | void m_callback_userstatus(Messenger *m, void (*function)(Messenger *m, int, USERSTATUS, void *), void *userdata); | 328 | void m_callback_userstatus(Messenger *m, void (*function)(Messenger *m, int, USERSTATUS, void *), void *userdata); |
304 | 329 | ||
305 | /* set the callback for read receipts | 330 | /* Set the callback for read receipts. |
306 | function(int friendnumber, uint32_t receipt) | 331 | * function(int friendnumber, uint32_t receipt) |
307 | if you are keeping a record of returns from m_sendmessage, | 332 | * |
308 | receipt might be one of those values, and that means the message | 333 | * If you are keeping a record of returns from m_sendmessage, |
309 | has been received on the other side. since core doesn't | 334 | * receipt might be one of those values, meaning the message |
310 | track ids for you, receipt may not correspond to any message | 335 | * has been received on the other side. |
311 | in that case, you should discard it. */ | 336 | * Since core doesn't track ids for you, receipt may not correspond to any message. |
337 | * In that case, you should discard it. | ||
338 | */ | ||
312 | void m_callback_read_receipt(Messenger *m, void (*function)(Messenger *m, int, uint32_t, void *), void *userdata); | 339 | void m_callback_read_receipt(Messenger *m, void (*function)(Messenger *m, int, uint32_t, void *), void *userdata); |
313 | 340 | ||
314 | /* set the callback for connection status changes | 341 | /* Set the callback for connection status changes. |
315 | function(int friendnumber, uint8_t status) | 342 | * function(int friendnumber, uint8_t status) |
316 | status: | 343 | * |
317 | 0 -- friend went offline after being previously online | 344 | * Status: |
318 | 1 -- friend went online | 345 | * 0 -- friend went offline after being previously online. |
319 | note that this callback is not called when adding friends, thus the "after | 346 | * 1 -- friend went online. |
320 | being previously online" part. it's assumed that when adding friends, | 347 | * |
321 | their connection status is offline. */ | 348 | * Note that this callback is not called when adding friends, thus the "after |
349 | * being previously online" part. | ||
350 | * It's assumed that when adding friends, their connection status is offline. | ||
351 | */ | ||
322 | void m_callback_connectionstatus(Messenger *m, void (*function)(Messenger *m, int, uint8_t, void *), void *userdata); | 352 | void m_callback_connectionstatus(Messenger *m, void (*function)(Messenger *m, int, uint8_t, void *), void *userdata); |
323 | 353 | ||
324 | /* run this at startup | 354 | /* Run this at startup. |
325 | * returns allocated instance of Messenger on success | 355 | * returns allocated instance of Messenger on success. |
326 | * returns 0 if there are problems */ | 356 | * returns 0 if there are problems. |
357 | */ | ||
327 | Messenger *initMessenger(void); | 358 | Messenger *initMessenger(void); |
328 | 359 | ||
329 | /* run this before closing shop | 360 | /* Run this before closing shop |
330 | * free all datastructures */ | 361 | * Free all datastructures. |
362 | */ | ||
331 | void cleanupMessenger(Messenger *M); | 363 | void cleanupMessenger(Messenger *M); |
332 | 364 | ||
333 | /* the main loop that needs to be run at least 200 times per second */ | 365 | /* The main loop that needs to be run at least 20 times per second. */ |
334 | void doMessenger(Messenger *m); | 366 | void doMessenger(Messenger *m); |
335 | 367 | ||
336 | /* SAVING AND LOADING FUNCTIONS: */ | 368 | /* SAVING AND LOADING FUNCTIONS: */ |
337 | 369 | ||
338 | /* returns the size of the messenger data (for saving) */ | 370 | /* return size of the messenger data (for saving). */ |
339 | uint32_t Messenger_size(Messenger *m); | 371 | uint32_t Messenger_size(Messenger *m); |
340 | 372 | ||
341 | /* save the messenger in data (must be allocated memory of size Messenger_size()) */ | 373 | /* Save the messenger in data (must be allocated memory of size Messenger_size()) */ |
342 | void Messenger_save(Messenger *m, uint8_t *data); | 374 | void Messenger_save(Messenger *m, uint8_t *data); |
343 | 375 | ||
344 | /* load the messenger from data of size length */ | 376 | /* Load the messenger from data of size length. */ |
345 | int Messenger_load(Messenger *m, uint8_t *data, uint32_t length); | 377 | int Messenger_load(Messenger *m, uint8_t *data, uint32_t length); |
346 | 378 | ||
347 | #ifdef __cplusplus | 379 | #ifdef __cplusplus |
diff --git a/toxcore/friend_requests.c b/toxcore/friend_requests.c index d8c6858b..a2827280 100644 --- a/toxcore/friend_requests.c +++ b/toxcore/friend_requests.c | |||
@@ -23,11 +23,12 @@ | |||
23 | 23 | ||
24 | #include "friend_requests.h" | 24 | #include "friend_requests.h" |
25 | 25 | ||
26 | /* Try to send a friendrequest 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 | * return -1 if failure. |
29 | return 0 if it sent the friend request directly to the friend. | 29 | * return 0 if it sent the friend request directly to the friend. |
30 | return the number of peers it was routed through if it did not send it directly.*/ | 30 | * return the number of peers it was routed through if it did not send it directly. |
31 | */ | ||
31 | int send_friendrequest(DHT *dht, uint8_t *public_key, uint32_t nospam_num, uint8_t *data, uint32_t length) | 32 | int send_friendrequest(DHT *dht, uint8_t *public_key, uint32_t nospam_num, uint8_t *data, uint32_t length) |
32 | { | 33 | { |
33 | if (length + sizeof(nospam_num) > MAX_DATA_SIZE) | 34 | if (length + sizeof(nospam_num) > MAX_DATA_SIZE) |
@@ -65,9 +66,7 @@ int send_friendrequest(DHT *dht, uint8_t *public_key, uint32_t nospam_num, uint8 | |||
65 | } | 66 | } |
66 | 67 | ||
67 | 68 | ||
68 | /* | 69 | /* Set and get the nospam variable used to prevent one type of friend request spam. */ |
69 | * Set and get the nospam variable used to prevent one type of friend request spam | ||
70 | */ | ||
71 | void set_nospam(Friend_Requests *fr, uint32_t num) | 70 | void set_nospam(Friend_Requests *fr, uint32_t num) |
72 | { | 71 | { |
73 | fr->nospam = num; | 72 | fr->nospam = num; |
@@ -79,7 +78,7 @@ uint32_t get_nospam(Friend_Requests *fr) | |||
79 | } | 78 | } |
80 | 79 | ||
81 | 80 | ||
82 | /* set the function that will be executed when a friend request is received. */ | 81 | /* Set the function that will be executed when a friend request is received. */ |
83 | void callback_friendrequest(Friend_Requests *fr, void (*function)(uint8_t *, uint8_t *, uint16_t, void *), | 82 | void callback_friendrequest(Friend_Requests *fr, void (*function)(uint8_t *, uint8_t *, uint16_t, void *), |
84 | void *userdata) | 83 | void *userdata) |
85 | { | 84 | { |
@@ -88,7 +87,7 @@ void callback_friendrequest(Friend_Requests *fr, void (*function)(uint8_t *, uin | |||
88 | fr->handle_friendrequest_userdata = userdata; | 87 | fr->handle_friendrequest_userdata = userdata; |
89 | } | 88 | } |
90 | 89 | ||
91 | /*Add to list of received friend requests*/ | 90 | /* Add to list of received friend requests. */ |
92 | static void addto_receivedlist(Friend_Requests *fr, uint8_t *client_id) | 91 | static void addto_receivedlist(Friend_Requests *fr, uint8_t *client_id) |
93 | { | 92 | { |
94 | if (fr->received_requests_index >= MAX_RECEIVED_STORED) | 93 | if (fr->received_requests_index >= MAX_RECEIVED_STORED) |
@@ -98,8 +97,10 @@ static void addto_receivedlist(Friend_Requests *fr, uint8_t *client_id) | |||
98 | ++fr->received_requests_index; | 97 | ++fr->received_requests_index; |
99 | } | 98 | } |
100 | 99 | ||
101 | /* Check if a friend request was already received | 100 | /* Check if a friend request was already received. |
102 | return 0 if not, 1 if we did */ | 101 | * return 0 if it did not. |
102 | * return 1 if it did. | ||
103 | */ | ||
103 | static int request_received(Friend_Requests *fr, uint8_t *client_id) | 104 | static int request_received(Friend_Requests *fr, uint8_t *client_id) |
104 | { | 105 | { |
105 | uint32_t i; | 106 | uint32_t i; |
diff --git a/toxcore/friend_requests.h b/toxcore/friend_requests.h index 2ebd557b..58572488 100644 --- a/toxcore/friend_requests.h +++ b/toxcore/friend_requests.h | |||
@@ -37,8 +37,9 @@ typedef struct { | |||
37 | uint8_t handle_friendrequest_isset; | 37 | uint8_t handle_friendrequest_isset; |
38 | void *handle_friendrequest_userdata; | 38 | void *handle_friendrequest_userdata; |
39 | 39 | ||
40 | /*NOTE: the following is just a temporary fix for the multiple friend requests received at the same time problem | 40 | /* NOTE: The following is just a temporary fix for the multiple friend requests received at the same time problem. |
41 | TODO: Make this better (This will most likely tie in with the way we will handle spam.)*/ | 41 | * TODO: Make this better (This will most likely tie in with the way we will handle spam.) |
42 | */ | ||
42 | 43 | ||
43 | #define MAX_RECEIVED_STORED 32 | 44 | #define MAX_RECEIVED_STORED 32 |
44 | 45 | ||
@@ -46,21 +47,21 @@ typedef struct { | |||
46 | uint16_t received_requests_index; | 47 | uint16_t received_requests_index; |
47 | } Friend_Requests; | 48 | } Friend_Requests; |
48 | 49 | ||
49 | /* Try to send a friendrequest to peer with public_key | 50 | /* Try to send a friendrequest to peer with public_key. |
50 | data is the data in the request and length is the length. */ | 51 | * data is the data in the request and length is the length. |
51 | int send_friendrequest(DHT *dht, uint8_t *public_key, uint32_t nospam_num, uint8_t *data, uint32_t length); | ||
52 | /* | ||
53 | * Set and get the nospam variable used to prevent one type of friend request spam | ||
54 | */ | 52 | */ |
53 | int send_friendrequest(DHT *dht, uint8_t *public_key, uint32_t nospam_num, uint8_t *data, uint32_t length); | ||
54 | /* Set and get the nospam variable used to prevent one type of friend request spam. */ | ||
55 | void set_nospam(Friend_Requests *fr, uint32_t num); | 55 | void set_nospam(Friend_Requests *fr, uint32_t num); |
56 | uint32_t get_nospam(Friend_Requests *fr); | 56 | uint32_t get_nospam(Friend_Requests *fr); |
57 | 57 | ||
58 | /* set the function that will be executed when a friend request for us is received. | 58 | /* Set the function that will be executed when a friend request for us is received. |
59 | function format is function(uint8_t * public_key, uint8_t * data, uint16_t length) */ | 59 | * Function format is function(uint8_t * public_key, uint8_t * data, uint16_t length) |
60 | */ | ||
60 | void callback_friendrequest(Friend_Requests *fr, void (*function)(uint8_t *, uint8_t *, uint16_t, void *), | 61 | void callback_friendrequest(Friend_Requests *fr, void (*function)(uint8_t *, uint8_t *, uint16_t, void *), |
61 | void *userdata); | 62 | void *userdata); |
62 | 63 | ||
63 | /* sets up friendreq packet handlers */ | 64 | /* Sets up friendreq packet handlers. */ |
64 | void friendreq_init(Friend_Requests *fr, Net_Crypto *c); | 65 | void friendreq_init(Friend_Requests *fr, Net_Crypto *c); |
65 | 66 | ||
66 | #ifdef __cplusplus | 67 | #ifdef __cplusplus |
diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c index f421c37d..ccf61700 100644 --- a/toxcore/net_crypto.c +++ b/toxcore/net_crypto.c | |||
@@ -46,9 +46,10 @@ uint8_t crypto_iszero(uint8_t *mem, uint32_t length) | |||
46 | } | 46 | } |
47 | 47 | ||
48 | /* Precomputes the shared key from their public_key and our secret_key. | 48 | /* Precomputes the shared key from their public_key and our secret_key. |
49 | This way we can avoid an expensive elliptic curve scalar multiply for each | 49 | * This way we can avoid an expensive elliptic curve scalar multiply for each |
50 | encrypt/decrypt operation. | 50 | * encrypt/decrypt operation. |
51 | enc_key has to be crypto_box_BEFORENMBYTES bytes long. */ | 51 | * enc_key has to be crypto_box_BEFORENMBYTES bytes long. |
52 | */ | ||
52 | void encrypt_precompute(uint8_t *public_key, uint8_t *secret_key, uint8_t *enc_key) | 53 | void encrypt_precompute(uint8_t *public_key, uint8_t *secret_key, uint8_t *enc_key) |
53 | { | 54 | { |
54 | crypto_box_beforenm(enc_key, public_key, secret_key); | 55 | crypto_box_beforenm(enc_key, public_key, secret_key); |
@@ -64,14 +65,14 @@ int encrypt_data_fast(uint8_t *enc_key, uint8_t *nonce, | |||
64 | uint8_t temp_plain[MAX_DATA_SIZE + crypto_box_ZEROBYTES] = {0}; | 65 | uint8_t temp_plain[MAX_DATA_SIZE + crypto_box_ZEROBYTES] = {0}; |
65 | uint8_t temp_encrypted[MAX_DATA_SIZE + crypto_box_BOXZEROBYTES]; | 66 | uint8_t temp_encrypted[MAX_DATA_SIZE + crypto_box_BOXZEROBYTES]; |
66 | 67 | ||
67 | memcpy(temp_plain + crypto_box_ZEROBYTES, plain, length); /* pad the message with 32 0 bytes. */ | 68 | memcpy(temp_plain + crypto_box_ZEROBYTES, plain, length); // Pad the message with 32 0 bytes. |
68 | 69 | ||
69 | crypto_box_afternm(temp_encrypted, temp_plain, length + crypto_box_ZEROBYTES, nonce, enc_key); | 70 | crypto_box_afternm(temp_encrypted, temp_plain, length + crypto_box_ZEROBYTES, nonce, enc_key); |
70 | 71 | ||
71 | if (crypto_iszero(temp_encrypted, crypto_box_BOXZEROBYTES) != 0) | 72 | if (crypto_iszero(temp_encrypted, crypto_box_BOXZEROBYTES) != 0) |
72 | return -1; | 73 | return -1; |
73 | 74 | ||
74 | /* unpad the encrypted message */ | 75 | /* Unpad the encrypted message. */ |
75 | memcpy(encrypted, temp_encrypted + crypto_box_BOXZEROBYTES, length + crypto_box_MACBYTES); | 76 | memcpy(encrypted, temp_encrypted + crypto_box_BOXZEROBYTES, length + crypto_box_MACBYTES); |
76 | return length - crypto_box_BOXZEROBYTES + crypto_box_ZEROBYTES; | 77 | return length - crypto_box_BOXZEROBYTES + crypto_box_ZEROBYTES; |
77 | } | 78 | } |
@@ -86,18 +87,19 @@ int decrypt_data_fast(uint8_t *enc_key, uint8_t *nonce, | |||
86 | uint8_t temp_plain[MAX_DATA_SIZE + crypto_box_ZEROBYTES]; | 87 | uint8_t temp_plain[MAX_DATA_SIZE + crypto_box_ZEROBYTES]; |
87 | uint8_t temp_encrypted[MAX_DATA_SIZE + crypto_box_BOXZEROBYTES] = {0}; | 88 | uint8_t temp_encrypted[MAX_DATA_SIZE + crypto_box_BOXZEROBYTES] = {0}; |
88 | 89 | ||
89 | memcpy(temp_encrypted + crypto_box_BOXZEROBYTES, encrypted, length); /* pad the message with 16 0 bytes. */ | 90 | memcpy(temp_encrypted + crypto_box_BOXZEROBYTES, encrypted, length); // Pad the message with 16 0 bytes. |
90 | 91 | ||
91 | if (crypto_box_open_afternm(temp_plain, temp_encrypted, length + crypto_box_BOXZEROBYTES, | 92 | if (crypto_box_open_afternm(temp_plain, temp_encrypted, length + crypto_box_BOXZEROBYTES, |
92 | nonce, enc_key) == -1) | 93 | nonce, enc_key) == -1) |
93 | return -1; | 94 | return -1; |
94 | 95 | ||
95 | /* if decryption is successful the first crypto_box_ZEROBYTES of the message will be zero | 96 | /* If decryption is successful the first crypto_box_ZEROBYTES of the message will be zero. |
96 | apparently memcmp should not be used so we do this instead:*/ | 97 | * Apparently memcmp should not be used so we do this instead: |
98 | */ | ||
97 | if (crypto_iszero(temp_plain, crypto_box_ZEROBYTES) != 0) | 99 | if (crypto_iszero(temp_plain, crypto_box_ZEROBYTES) != 0) |
98 | return -1; | 100 | return -1; |
99 | 101 | ||
100 | /* unpad the plain message */ | 102 | /* Unpad the plain message. */ |
101 | memcpy(plain, temp_plain + crypto_box_ZEROBYTES, length - crypto_box_MACBYTES); | 103 | memcpy(plain, temp_plain + crypto_box_ZEROBYTES, length - crypto_box_MACBYTES); |
102 | return length - crypto_box_ZEROBYTES + crypto_box_BOXZEROBYTES; | 104 | return length - crypto_box_ZEROBYTES + crypto_box_BOXZEROBYTES; |
103 | } | 105 | } |
@@ -118,7 +120,7 @@ int decrypt_data(uint8_t *public_key, uint8_t *secret_key, uint8_t *nonce, | |||
118 | return decrypt_data_fast(k, nonce, encrypted, length, plain); | 120 | return decrypt_data_fast(k, nonce, encrypted, length, plain); |
119 | } | 121 | } |
120 | 122 | ||
121 | /* increment the given nonce by 1 */ | 123 | /* Increment the given nonce by 1. */ |
122 | static void increment_nonce(uint8_t *nonce) | 124 | static void increment_nonce(uint8_t *nonce) |
123 | { | 125 | { |
124 | uint32_t i; | 126 | uint32_t i; |
@@ -131,7 +133,7 @@ static void increment_nonce(uint8_t *nonce) | |||
131 | } | 133 | } |
132 | } | 134 | } |
133 | 135 | ||
134 | /* fill the given nonce with random bytes. */ | 136 | /* Fill the given nonce with random bytes. */ |
135 | void random_nonce(uint8_t *nonce) | 137 | void random_nonce(uint8_t *nonce) |
136 | { | 138 | { |
137 | uint32_t i, temp; | 139 | uint32_t i, temp; |
@@ -142,9 +144,10 @@ void random_nonce(uint8_t *nonce) | |||
142 | } | 144 | } |
143 | } | 145 | } |
144 | 146 | ||
145 | /* return 0 if there is no received data in the buffer | 147 | /* return 0 if there is no received data in the buffer. |
146 | return -1 if the packet was discarded. | 148 | * return -1 if the packet was discarded. |
147 | return length of received data if successful */ | 149 | * return length of received data if successful. |
150 | */ | ||
148 | int read_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data) | 151 | int read_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data) |
149 | { | 152 | { |
150 | if (crypt_connection_id < 0 || crypt_connection_id >= c->crypto_connections_length) | 153 | if (crypt_connection_id < 0 || crypt_connection_id >= c->crypto_connections_length) |
@@ -174,8 +177,9 @@ int read_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data) | |||
174 | return -1; | 177 | return -1; |
175 | } | 178 | } |
176 | 179 | ||
177 | /* return 0 if data could not be put in packet queue | 180 | /* return 0 if data could not be put in packet queue. |
178 | return 1 if data was put into the queue */ | 181 | * return 1 if data was put into the queue. |
182 | */ | ||
179 | int write_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data, uint32_t length) | 183 | int write_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data, uint32_t length) |
180 | { | 184 | { |
181 | if (crypt_connection_id < 0 || crypt_connection_id >= c->crypto_connections_length) | 185 | if (crypt_connection_id < 0 || crypt_connection_id >= c->crypto_connections_length) |
@@ -204,14 +208,16 @@ int write_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data, uin | |||
204 | return 1; | 208 | return 1; |
205 | } | 209 | } |
206 | 210 | ||
207 | /* create a request to peer. | 211 | /* Ceate a request to peer. |
208 | send_public_key and send_secret_key are the pub/secret keys of the sender | 212 | * send_public_key and send_secret_key are the pub/secret keys of the sender. |
209 | recv_public_key is public key of reciever | 213 | * recv_public_key is public key of reciever. |
210 | packet must be an array of MAX_DATA_SIZE big. | 214 | * packet must be an array of MAX_DATA_SIZE big. |
211 | Data represents the data we send with the request with length being the length of the data. | 215 | * Data represents the data we send with the request with length being the length of the data. |
212 | request_id is the id of the request (32 = friend request, 254 = ping request) | 216 | * request_id is the id of the request (32 = friend request, 254 = ping request). |
213 | returns -1 on failure | 217 | * |
214 | returns the length of the created packet on success */ | 218 | * returns -1 on failure. |
219 | * returns the length of the created packet on success. | ||
220 | */ | ||
215 | int create_request(uint8_t *send_public_key, uint8_t *send_secret_key, uint8_t *packet, uint8_t *recv_public_key, | 221 | int create_request(uint8_t *send_public_key, uint8_t *send_secret_key, uint8_t *packet, uint8_t *recv_public_key, |
216 | uint8_t *data, uint32_t length, uint8_t request_id) | 222 | uint8_t *data, uint32_t length, uint8_t request_id) |
217 | { | 223 | { |
@@ -237,10 +243,11 @@ int create_request(uint8_t *send_public_key, uint8_t *send_secret_key, uint8_t * | |||
237 | return len + 1 + crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES; | 243 | return len + 1 + crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES; |
238 | } | 244 | } |
239 | 245 | ||
240 | /* puts the senders public key in the request in public_key, the data from the request | 246 | /* Puts the senders public key in the request in public_key, the data from the request |
241 | in data if a friend or ping request was sent to us and returns the length of the data. | 247 | * in data if a friend or ping request was sent to us and returns the length of the data. |
242 | packet is the request packet and length is its length | 248 | * packet is the request packet and length is its length. |
243 | return -1 if not valid request. */ | 249 | * return -1 if not valid request. |
250 | */ | ||
244 | static int handle_request(Net_Crypto *c, uint8_t *public_key, uint8_t *data, uint8_t *request_id, uint8_t *packet, | 251 | static int handle_request(Net_Crypto *c, uint8_t *public_key, uint8_t *data, uint8_t *request_id, uint8_t *packet, |
245 | uint16_t length) | 252 | uint16_t length) |
246 | { | 253 | { |
@@ -282,7 +289,7 @@ static int cryptopacket_handle(void *object, IP_Port source, uint8_t *packet, ui | |||
282 | length > MAX_DATA_SIZE + ENCRYPTION_PADDING) | 289 | length > MAX_DATA_SIZE + ENCRYPTION_PADDING) |
283 | return 1; | 290 | return 1; |
284 | 291 | ||
285 | if (memcmp(packet + 1, dht->c->self_public_key, crypto_box_PUBLICKEYBYTES) == 0) {// check if request is for us. | 292 | if (memcmp(packet + 1, dht->c->self_public_key, crypto_box_PUBLICKEYBYTES) == 0) { // Check if request is for us. |
286 | uint8_t public_key[crypto_box_PUBLICKEYBYTES]; | 293 | uint8_t public_key[crypto_box_PUBLICKEYBYTES]; |
287 | uint8_t data[MAX_DATA_SIZE]; | 294 | uint8_t data[MAX_DATA_SIZE]; |
288 | uint8_t number; | 295 | uint8_t number; |
@@ -296,8 +303,8 @@ static int cryptopacket_handle(void *object, IP_Port source, uint8_t *packet, ui | |||
296 | dht->c->cryptopackethandlers[number].function(dht->c->cryptopackethandlers[number].object, source, public_key, data, | 303 | dht->c->cryptopackethandlers[number].function(dht->c->cryptopackethandlers[number].object, source, public_key, data, |
297 | len); | 304 | len); |
298 | 305 | ||
299 | } else { /* if request is not for us, try routing it. */ | 306 | } else { /* If request is not for us, try routing it. */ |
300 | if (route_packet(dht, packet + 1, packet, length) == length) //NOTE | 307 | if (route_packet(dht, packet + 1, packet, length) == length) |
301 | return 0; | 308 | return 0; |
302 | } | 309 | } |
303 | } | 310 | } |
@@ -306,8 +313,9 @@ static int cryptopacket_handle(void *object, IP_Port source, uint8_t *packet, ui | |||
306 | } | 313 | } |
307 | 314 | ||
308 | /* Send a crypto handshake packet containing an encrypted secret nonce and session public key | 315 | /* Send a crypto handshake packet containing an encrypted secret nonce and session public key |
309 | to peer with connection_id and public_key | 316 | * to peer with connection_id and public_key. |
310 | the packet is encrypted with a random nonce which is sent in plain text with the packet */ | 317 | * The packet is encrypted with a random nonce which is sent in plain text with the packet. |
318 | */ | ||
311 | static int send_cryptohandshake(Net_Crypto *c, int connection_id, uint8_t *public_key, uint8_t *secret_nonce, | 319 | static int send_cryptohandshake(Net_Crypto *c, int connection_id, uint8_t *public_key, uint8_t *secret_nonce, |
312 | uint8_t *session_key) | 320 | uint8_t *session_key) |
313 | { | 321 | { |
@@ -332,9 +340,10 @@ static int send_cryptohandshake(Net_Crypto *c, int connection_id, uint8_t *publi | |||
332 | len + 1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES); | 340 | len + 1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES); |
333 | } | 341 | } |
334 | 342 | ||
335 | /* Extract secret nonce, session public key and public_key from a packet(data) with length length | 343 | /* Extract secret nonce, session public key and public_key from a packet(data) with length length. |
336 | return 1 if successful | 344 | * return 1 if successful. |
337 | return 0 if failure */ | 345 | * return 0 if failure. |
346 | */ | ||
338 | static int handle_cryptohandshake(Net_Crypto *c, uint8_t *public_key, uint8_t *secret_nonce, | 347 | static int handle_cryptohandshake(Net_Crypto *c, uint8_t *public_key, uint8_t *secret_nonce, |
339 | uint8_t *session_key, uint8_t *data, uint16_t length) | 348 | uint8_t *session_key, uint8_t *data, uint16_t length) |
340 | { | 349 | { |
@@ -364,9 +373,10 @@ static int handle_cryptohandshake(Net_Crypto *c, uint8_t *public_key, uint8_t *s | |||
364 | return 1; | 373 | return 1; |
365 | } | 374 | } |
366 | 375 | ||
367 | /* get crypto connection id from public key of peer | 376 | /* Get crypto connection id from public key of peer. |
368 | return -1 if there are no connections like we are looking for | 377 | * return -1 if there are no connections like we are looking for. |
369 | return id if it found it */ | 378 | * return id if it found it. |
379 | */ | ||
370 | static int getcryptconnection_id(Net_Crypto *c, uint8_t *public_key) | 380 | static int getcryptconnection_id(Net_Crypto *c, uint8_t *public_key) |
371 | { | 381 | { |
372 | uint32_t i; | 382 | uint32_t i; |
@@ -380,8 +390,9 @@ static int getcryptconnection_id(Net_Crypto *c, uint8_t *public_key) | |||
380 | return -1; | 390 | return -1; |
381 | } | 391 | } |
382 | 392 | ||
383 | /* set the size of the friend list to numfriends | 393 | /* Set the size of the friend list to numfriends. |
384 | return -1 if realloc fails */ | 394 | * return -1 if realloc fails. |
395 | */ | ||
385 | int realloc_cryptoconnection(Net_Crypto *c, uint32_t num) | 396 | int realloc_cryptoconnection(Net_Crypto *c, uint32_t num) |
386 | { | 397 | { |
387 | if (num == 0) { | 398 | if (num == 0) { |
@@ -399,9 +410,10 @@ int realloc_cryptoconnection(Net_Crypto *c, uint32_t num) | |||
399 | return 0; | 410 | return 0; |
400 | } | 411 | } |
401 | 412 | ||
402 | /* Start a secure connection with other peer who has public_key and ip_port | 413 | /* Start a secure connection with other peer who has public_key and ip_port. |
403 | returns -1 if failure | 414 | * returns -1 if failure. |
404 | returns crypt_connection_id of the initialized connection if everything went well. */ | 415 | * returns crypt_connection_id of the initialized connection if everything went well. |
416 | */ | ||
405 | int crypto_connect(Net_Crypto *c, uint8_t *public_key, IP_Port ip_port) | 417 | int crypto_connect(Net_Crypto *c, uint8_t *public_key, IP_Port ip_port) |
406 | { | 418 | { |
407 | uint32_t i; | 419 | uint32_t i; |
@@ -442,20 +454,22 @@ int crypto_connect(Net_Crypto *c, uint8_t *public_key, IP_Port ip_port) | |||
442 | return i; | 454 | return i; |
443 | } | 455 | } |
444 | 456 | ||
445 | return -1; /* this should never happen. */ | 457 | return -1; /* This should never happen. */ |
446 | } | 458 | } |
447 | } | 459 | } |
448 | 460 | ||
449 | return -1; | 461 | return -1; |
450 | } | 462 | } |
451 | 463 | ||
452 | /* handle an incoming connection | 464 | /* Handle an incoming connection. |
453 | return -1 if no crypto inbound connection | 465 | * return -1 if no crypto inbound connection. |
454 | return incoming connection id (Lossless_UDP one) if there is an incoming crypto connection | 466 | * return incoming connection id (Lossless_UDP one) if there is an incoming crypto connection. |
455 | Put the public key of the peer in public_key, the secret_nonce from the handshake into secret_nonce | 467 | * |
456 | and the session public key for the connection in session_key | 468 | * Put the public key of the peer in public_key, the secret_nonce from the handshake into secret_nonce |
457 | to accept it see: accept_crypto_inbound(...) | 469 | * and the session public key for the connection in session_key. |
458 | to refuse it just call kill_connection(...) on the connection id */ | 470 | * to accept it see: accept_crypto_inbound(...). |
471 | * to refuse it just call kill_connection(...) on the connection id. | ||
472 | */ | ||
459 | int crypto_inbound(Net_Crypto *c, uint8_t *public_key, uint8_t *secret_nonce, uint8_t *session_key) | 473 | int crypto_inbound(Net_Crypto *c, uint8_t *public_key, uint8_t *secret_nonce, uint8_t *session_key) |
460 | { | 474 | { |
461 | uint32_t i; | 475 | uint32_t i; |
@@ -475,7 +489,7 @@ int crypto_inbound(Net_Crypto *c, uint8_t *public_key, uint8_t *secret_nonce, ui | |||
475 | 489 | ||
476 | if (handle_cryptohandshake(c, public_key, secret_nonce, session_key, temp_data, len)) { | 490 | if (handle_cryptohandshake(c, public_key, secret_nonce, session_key, temp_data, len)) { |
477 | int connection_id = c->incoming_connections[i]; | 491 | int connection_id = c->incoming_connections[i]; |
478 | c->incoming_connections[i] = -1; /* remove this connection from the incoming connection list. */ | 492 | c->incoming_connections[i] = -1; /* Remove this connection from the incoming connection list. */ |
479 | return connection_id; | 493 | return connection_id; |
480 | } | 494 | } |
481 | } | 495 | } |
@@ -485,9 +499,10 @@ int crypto_inbound(Net_Crypto *c, uint8_t *public_key, uint8_t *secret_nonce, ui | |||
485 | return -1; | 499 | return -1; |
486 | } | 500 | } |
487 | 501 | ||
488 | /* kill a crypto connection | 502 | /* Kill a crypto connection. |
489 | return 0 if killed successfully | 503 | * return 0 if killed successfully. |
490 | return 1 if there was a problem. */ | 504 | * return 1 if there was a problem. |
505 | */ | ||
491 | int crypto_kill(Net_Crypto *c, int crypt_connection_id) | 506 | int crypto_kill(Net_Crypto *c, int crypt_connection_id) |
492 | { | 507 | { |
493 | if (crypt_connection_id < 0 || crypt_connection_id >= c->crypto_connections_length) | 508 | if (crypt_connection_id < 0 || crypt_connection_id >= c->crypto_connections_length) |
@@ -513,9 +528,10 @@ int crypto_kill(Net_Crypto *c, int crypt_connection_id) | |||
513 | return 1; | 528 | return 1; |
514 | } | 529 | } |
515 | 530 | ||
516 | /* accept an incoming connection using the parameters provided by crypto_inbound | 531 | /* Accept an incoming connection using the parameters provided by crypto_inbound. |
517 | return -1 if not successful | 532 | * return -1 if not successful. |
518 | returns the crypt_connection_id if successful */ | 533 | * returns the crypt_connection_id if successful. |
534 | */ | ||
519 | int accept_crypto_inbound(Net_Crypto *c, int connection_id, uint8_t *public_key, uint8_t *secret_nonce, | 535 | int accept_crypto_inbound(Net_Crypto *c, int connection_id, uint8_t *public_key, uint8_t *secret_nonce, |
520 | uint8_t *session_key) | 536 | uint8_t *session_key) |
521 | { | 537 | { |
@@ -524,11 +540,12 @@ int accept_crypto_inbound(Net_Crypto *c, int connection_id, uint8_t *public_key, | |||
524 | if (connection_id == -1) | 540 | if (connection_id == -1) |
525 | return -1; | 541 | return -1; |
526 | 542 | ||
527 | /* | 543 | /* |
528 | if(getcryptconnection_id(public_key) != -1) | 544 | * if(getcryptconnection_id(public_key) != -1) |
529 | { | 545 | * { |
530 | return -1; | 546 | * return -1; |
531 | }*/ | 547 | * } |
548 | */ | ||
532 | if (realloc_cryptoconnection(c, c->crypto_connections_length + 1) == -1) | 549 | if (realloc_cryptoconnection(c, c->crypto_connections_length + 1) == -1) |
533 | return -1; | 550 | return -1; |
534 | 551 | ||
@@ -558,22 +575,25 @@ int accept_crypto_inbound(Net_Crypto *c, int connection_id, uint8_t *public_key, | |||
558 | c->crypto_connections[i].sessionsecret_key, | 575 | c->crypto_connections[i].sessionsecret_key, |
559 | c->crypto_connections[i].shared_key); | 576 | c->crypto_connections[i].shared_key); |
560 | c->crypto_connections[i].status = | 577 | c->crypto_connections[i].status = |
561 | CONN_ESTABLISHED; /* connection status needs to be 3 for write_cryptpacket() to work */ | 578 | CONN_ESTABLISHED; /* Connection status needs to be 3 for write_cryptpacket() to work. */ |
562 | write_cryptpacket(c, i, ((uint8_t *)&zero), sizeof(zero)); | 579 | write_cryptpacket(c, i, ((uint8_t *)&zero), sizeof(zero)); |
563 | c->crypto_connections[i].status = CONN_NOT_CONFIRMED; /* set it to its proper value right after. */ | 580 | c->crypto_connections[i].status = CONN_NOT_CONFIRMED; /* Set it to its proper value right after. */ |
564 | return i; | 581 | return i; |
565 | } | 582 | } |
566 | 583 | ||
567 | return -1; /* this should never happen. */ | 584 | return -1; /* This should never happen. */ |
568 | } | 585 | } |
569 | } | 586 | } |
570 | 587 | ||
571 | return -1; | 588 | return -1; |
572 | } | 589 | } |
573 | 590 | ||
574 | /* return 0 if no connection, 1 we have sent a handshake, 2 if connection is not confirmed yet | 591 | /* return 0 if no connection. |
575 | (we have received a handshake but no empty data packet), 3 if the connection is established. | 592 | * return 1 we have sent a handshake. |
576 | 4 if the connection is timed out and waiting to be killed */ | 593 | * return 2 if connection is not confirmed yet (we have received a handshake but no empty data packet). |
594 | * return 3 if the connection is established. | ||
595 | * return 4 if the connection is timed out and waiting to be killed. | ||
596 | */ | ||
577 | int is_cryptoconnected(Net_Crypto *c, int crypt_connection_id) | 597 | int is_cryptoconnected(Net_Crypto *c, int crypt_connection_id) |
578 | { | 598 | { |
579 | if (crypt_connection_id >= 0 && crypt_connection_id < c->crypto_connections_length) | 599 | if (crypt_connection_id >= 0 && crypt_connection_id < c->crypto_connections_length) |
@@ -587,26 +607,29 @@ void new_keys(Net_Crypto *c) | |||
587 | crypto_box_keypair(c->self_public_key, c->self_secret_key); | 607 | crypto_box_keypair(c->self_public_key, c->self_secret_key); |
588 | } | 608 | } |
589 | 609 | ||
590 | /* save the public and private keys to the keys array | 610 | /* Save the public and private keys to the keys array. |
591 | Length must be crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES */ | 611 | * Length must be crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES. |
612 | */ | ||
592 | void save_keys(Net_Crypto *c, uint8_t *keys) | 613 | void save_keys(Net_Crypto *c, uint8_t *keys) |
593 | { | 614 | { |
594 | memcpy(keys, c->self_public_key, crypto_box_PUBLICKEYBYTES); | 615 | memcpy(keys, c->self_public_key, crypto_box_PUBLICKEYBYTES); |
595 | memcpy(keys + crypto_box_PUBLICKEYBYTES, c->self_secret_key, crypto_box_SECRETKEYBYTES); | 616 | memcpy(keys + crypto_box_PUBLICKEYBYTES, c->self_secret_key, crypto_box_SECRETKEYBYTES); |
596 | } | 617 | } |
597 | 618 | ||
598 | /* load the public and private keys from the keys array | 619 | /* Load the public and private keys from the keys array. |
599 | Length must be crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES */ | 620 | * Length must be crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES. |
621 | */ | ||
600 | void load_keys(Net_Crypto *c, uint8_t *keys) | 622 | void load_keys(Net_Crypto *c, uint8_t *keys) |
601 | { | 623 | { |
602 | memcpy(c->self_public_key, keys, crypto_box_PUBLICKEYBYTES); | 624 | memcpy(c->self_public_key, keys, crypto_box_PUBLICKEYBYTES); |
603 | memcpy(c->self_secret_key, keys + crypto_box_PUBLICKEYBYTES, crypto_box_SECRETKEYBYTES); | 625 | memcpy(c->self_secret_key, keys + crypto_box_PUBLICKEYBYTES, crypto_box_SECRETKEYBYTES); |
604 | } | 626 | } |
605 | 627 | ||
606 | /* TODO: optimize this | 628 | /* Adds an incoming connection to the incoming_connection list. |
607 | adds an incoming connection to the incoming_connection list. | 629 | * returns 0 if successful |
608 | returns 0 if successful | 630 | * returns 1 if failure. |
609 | returns 1 if failure */ | 631 | * TODO: Optimize this. |
632 | */ | ||
610 | static int new_incoming(Net_Crypto *c, int id) | 633 | static int new_incoming(Net_Crypto *c, int id) |
611 | { | 634 | { |
612 | uint32_t i; | 635 | uint32_t i; |
@@ -621,8 +644,9 @@ static int new_incoming(Net_Crypto *c, int id) | |||
621 | return 1; | 644 | return 1; |
622 | } | 645 | } |
623 | 646 | ||
624 | /* TODO: optimize this | 647 | /* Handle all new incoming connections. |
625 | handle all new incoming connections. */ | 648 | * TODO: Optimize this. |
649 | */ | ||
626 | static void handle_incomings(Net_Crypto *c) | 650 | static void handle_incomings(Net_Crypto *c) |
627 | { | 651 | { |
628 | int income; | 652 | int income; |
@@ -635,7 +659,7 @@ static void handle_incomings(Net_Crypto *c) | |||
635 | } | 659 | } |
636 | } | 660 | } |
637 | 661 | ||
638 | /* handle received packets for not yet established crypto connections. */ | 662 | /* Handle received packets for not yet established crypto connections. */ |
639 | static void receive_crypto(Net_Crypto *c) | 663 | static void receive_crypto(Net_Crypto *c) |
640 | { | 664 | { |
641 | uint32_t i; | 665 | uint32_t i; |
@@ -648,7 +672,7 @@ static void receive_crypto(Net_Crypto *c) | |||
648 | uint8_t session_key[crypto_box_PUBLICKEYBYTES]; | 672 | uint8_t session_key[crypto_box_PUBLICKEYBYTES]; |
649 | uint16_t len; | 673 | uint16_t len; |
650 | 674 | ||
651 | if (id_packet(c->lossless_udp, c->crypto_connections[i].number) == 2) { /* handle handshake packet. */ | 675 | if (id_packet(c->lossless_udp, c->crypto_connections[i].number) == 2) { /* Handle handshake packet. */ |
652 | len = read_packet(c->lossless_udp, c->crypto_connections[i].number, temp_data); | 676 | len = read_packet(c->lossless_udp, c->crypto_connections[i].number, temp_data); |
653 | 677 | ||
654 | if (handle_cryptohandshake(c, public_key, secret_nonce, session_key, temp_data, len)) { | 678 | if (handle_cryptohandshake(c, public_key, secret_nonce, session_key, temp_data, len)) { |
@@ -661,13 +685,13 @@ static void receive_crypto(Net_Crypto *c) | |||
661 | c->crypto_connections[i].sessionsecret_key, | 685 | c->crypto_connections[i].sessionsecret_key, |
662 | c->crypto_connections[i].shared_key); | 686 | c->crypto_connections[i].shared_key); |
663 | c->crypto_connections[i].status = | 687 | c->crypto_connections[i].status = |
664 | CONN_ESTABLISHED; /* connection status needs to be 3 for write_cryptpacket() to work */ | 688 | CONN_ESTABLISHED; /* Connection status needs to be 3 for write_cryptpacket() to work. */ |
665 | write_cryptpacket(c, i, ((uint8_t *)&zero), sizeof(zero)); | 689 | write_cryptpacket(c, i, ((uint8_t *)&zero), sizeof(zero)); |
666 | c->crypto_connections[i].status = CONN_NOT_CONFIRMED; /* set it to its proper value right after. */ | 690 | c->crypto_connections[i].status = CONN_NOT_CONFIRMED; /* Set it to its proper value right after. */ |
667 | } | 691 | } |
668 | } | 692 | } |
669 | } else if (id_packet(c->lossless_udp, | 693 | } else if (id_packet(c->lossless_udp, |
670 | c->crypto_connections[i].number) != -1) { // This should not happen kill the connection if it does | 694 | c->crypto_connections[i].number) != -1) { // This should not happen, kill the connection if it does. |
671 | crypto_kill(c, i); | 695 | crypto_kill(c, i); |
672 | return; | 696 | return; |
673 | } | 697 | } |
@@ -690,15 +714,15 @@ static void receive_crypto(Net_Crypto *c) | |||
690 | c->crypto_connections[i].shared_key); | 714 | c->crypto_connections[i].shared_key); |
691 | c->crypto_connections[i].status = CONN_ESTABLISHED; | 715 | c->crypto_connections[i].status = CONN_ESTABLISHED; |
692 | 716 | ||
693 | /* connection is accepted so we disable the auto kill by setting it to about 1 month from now. */ | 717 | /* Connection is accepted so we disable the auto kill by setting it to about 1 month from now. */ |
694 | kill_connection_in(c->lossless_udp, c->crypto_connections[i].number, 3000000); | 718 | kill_connection_in(c->lossless_udp, c->crypto_connections[i].number, 3000000); |
695 | } else { | 719 | } else { |
696 | crypto_kill(c, i); // This should not happen kill the connection if it does | 720 | /* This should not happen, kill the connection if it does. */ |
721 | crypto_kill(c, i); | ||
697 | return; | 722 | return; |
698 | } | 723 | } |
699 | } else if (id_packet(c->lossless_udp, c->crypto_connections[i].number) != -1) | 724 | } else if (id_packet(c->lossless_udp, c->crypto_connections[i].number) != -1) |
700 | /* This should not happen | 725 | /* This should not happen, kill the connection if it does. */ |
701 | kill the connection if it does */ | ||
702 | crypto_kill(c, i); | 726 | crypto_kill(c, i); |
703 | 727 | ||
704 | return; | 728 | return; |
@@ -706,8 +730,9 @@ static void receive_crypto(Net_Crypto *c) | |||
706 | } | 730 | } |
707 | } | 731 | } |
708 | 732 | ||
709 | /* run this to (re)initialize net_crypto | 733 | /* Run this to (re)initialize net_crypto. |
710 | sets all the global connection variables to their default values. */ | 734 | * Sets all the global connection variables to their default values. |
735 | */ | ||
711 | Net_Crypto *new_net_crypto(Networking_Core *net) | 736 | Net_Crypto *new_net_crypto(Networking_Core *net) |
712 | { | 737 | { |
713 | if (net == NULL) | 738 | if (net == NULL) |
@@ -748,7 +773,7 @@ static void kill_timedout(Net_Crypto *c) | |||
748 | } | 773 | } |
749 | } | 774 | } |
750 | 775 | ||
751 | /* main loop */ | 776 | /* Main loop. */ |
752 | void do_net_crypto(Net_Crypto *c) | 777 | void do_net_crypto(Net_Crypto *c) |
753 | { | 778 | { |
754 | do_lossless_udp(c->lossless_udp); | 779 | do_lossless_udp(c->lossless_udp); |
diff --git a/toxcore/net_crypto.h b/toxcore/net_crypto.h index 81670993..1fde0297 100644 --- a/toxcore/net_crypto.h +++ b/toxcore/net_crypto.h | |||
@@ -32,20 +32,21 @@ extern "C" { | |||
32 | 32 | ||
33 | #define MAX_INCOMING 64 | 33 | #define MAX_INCOMING 64 |
34 | 34 | ||
35 | #define CRYPTO_PACKET_FRIEND_REQ 32 /* Friend request crypto packet ID */ | 35 | #define CRYPTO_PACKET_FRIEND_REQ 32 /* Friend request crypto packet ID. */ |
36 | #define CRYPTO_PACKET_NAT_PING 254 /* NAT ping crypto packet ID */ | 36 | #define CRYPTO_PACKET_NAT_PING 254 /* NAT ping crypto packet ID. */ |
37 | 37 | ||
38 | typedef struct { | 38 | typedef struct { |
39 | uint8_t public_key[crypto_box_PUBLICKEYBYTES]; /* the real public key of the peer. */ | 39 | uint8_t public_key[crypto_box_PUBLICKEYBYTES]; /* The real public key of the peer. */ |
40 | uint8_t recv_nonce[crypto_box_NONCEBYTES]; /* nonce of received packets */ | 40 | uint8_t recv_nonce[crypto_box_NONCEBYTES]; /* Nonce of received packets. */ |
41 | uint8_t sent_nonce[crypto_box_NONCEBYTES]; /* nonce of sent packets. */ | 41 | uint8_t sent_nonce[crypto_box_NONCEBYTES]; /* Nonce of sent packets. */ |
42 | uint8_t sessionpublic_key[crypto_box_PUBLICKEYBYTES]; /* our public key for this session. */ | 42 | uint8_t sessionpublic_key[crypto_box_PUBLICKEYBYTES]; /* Our public key for this session. */ |
43 | uint8_t sessionsecret_key[crypto_box_SECRETKEYBYTES]; /* our private key for this session. */ | 43 | uint8_t sessionsecret_key[crypto_box_SECRETKEYBYTES]; /* Our private key for this session. */ |
44 | uint8_t peersessionpublic_key[crypto_box_PUBLICKEYBYTES]; /* The public key of the peer. */ | 44 | uint8_t peersessionpublic_key[crypto_box_PUBLICKEYBYTES]; /* The public key of the peer. */ |
45 | uint8_t shared_key[crypto_box_BEFORENMBYTES]; /* the precomputed shared key from encrypt_precompute */ | 45 | uint8_t shared_key[crypto_box_BEFORENMBYTES]; /* The precomputed shared key from encrypt_precompute. */ |
46 | uint8_t status; /* 0 if no connection, 1 we have sent a handshake, 2 if connexion is not confirmed yet | 46 | uint8_t status; /* 0 if no connection, 1 we have sent a handshake, 2 if connexion is not confirmed yet |
47 | (we have received a handshake but no empty data packet), 3 if the connection is established. | 47 | * (we have received a handshake but no empty data packet), 3 if the connection is established. |
48 | 4 if the connection is timed out. */ | 48 | * 4 if the connection is timed out. |
49 | */ | ||
49 | uint16_t number; /* Lossless_UDP connection number corresponding to this connection. */ | 50 | uint16_t number; /* Lossless_UDP connection number corresponding to this connection. */ |
50 | 51 | ||
51 | } Crypto_Connection; | 52 | } Crypto_Connection; |
@@ -63,13 +64,13 @@ typedef struct { | |||
63 | 64 | ||
64 | Crypto_Connection *crypto_connections; | 65 | Crypto_Connection *crypto_connections; |
65 | 66 | ||
66 | uint32_t crypto_connections_length; /* Length of connections array */ | 67 | uint32_t crypto_connections_length; /* Length of connections array. */ |
67 | 68 | ||
68 | /* Our public and secret keys. */ | 69 | /* Our public and secret keys. */ |
69 | uint8_t self_public_key[crypto_box_PUBLICKEYBYTES]; | 70 | uint8_t self_public_key[crypto_box_PUBLICKEYBYTES]; |
70 | uint8_t self_secret_key[crypto_box_SECRETKEYBYTES]; | 71 | uint8_t self_secret_key[crypto_box_SECRETKEYBYTES]; |
71 | 72 | ||
72 | /* keeps track of the connection numbers for friends request so we can check later if they were sent */ | 73 | /* keeps track of the connection numbers for friends request so we can check later if they were sent. */ |
73 | int incoming_connections[MAX_INCOMING]; | 74 | int incoming_connections[MAX_INCOMING]; |
74 | 75 | ||
75 | Cryptopacket_Handles cryptopackethandlers[256]; | 76 | Cryptopacket_Handles cryptopackethandlers[256]; |
@@ -79,21 +80,23 @@ typedef struct { | |||
79 | 80 | ||
80 | #define ENCRYPTION_PADDING (crypto_box_ZEROBYTES - crypto_box_BOXZEROBYTES) | 81 | #define ENCRYPTION_PADDING (crypto_box_ZEROBYTES - crypto_box_BOXZEROBYTES) |
81 | 82 | ||
82 | /* returns zero if the buffer contains only zeros */ | 83 | /* return zero if the buffer contains only zeros. */ |
83 | uint8_t crypto_iszero(uint8_t *buffer, uint32_t blen); | 84 | uint8_t crypto_iszero(uint8_t *buffer, uint32_t blen); |
84 | 85 | ||
85 | /* encrypts plain of length length to encrypted of length + 16 using the | 86 | /* Encrypts plain of length length to encrypted of length + 16 using the |
86 | public key(32 bytes) of the receiver and the secret key of the sender and a 24 byte nonce | 87 | * public key(32 bytes) of the receiver and the secret key of the sender and a 24 byte nonce. |
87 | return -1 if there was a problem. | 88 | * return -1 if there was a problem. |
88 | return length of encrypted data if everything was fine. */ | 89 | * return length of encrypted data if everything was fine. |
90 | */ | ||
89 | int encrypt_data(uint8_t *public_key, uint8_t *secret_key, uint8_t *nonce, | 91 | int encrypt_data(uint8_t *public_key, uint8_t *secret_key, uint8_t *nonce, |
90 | uint8_t *plain, uint32_t length, uint8_t *encrypted); | 92 | uint8_t *plain, uint32_t length, uint8_t *encrypted); |
91 | 93 | ||
92 | 94 | ||
93 | /* decrypts encrypted of length length to plain of length length - 16 using the | 95 | /* Decrypts encrypted of length length to plain of length length - 16 using the |
94 | public key(32 bytes) of the sender, the secret key of the receiver and a 24 byte nonce | 96 | * public key(32 bytes) of the sender, the secret key of the receiver and a 24 byte nonce. |
95 | return -1 if there was a problem(decryption failed) | 97 | * return -1 if there was a problem (decryption failed). |
96 | return length of plain data if everything was fine. */ | 98 | * return length of plain data if everything was fine. |
99 | */ | ||
97 | int decrypt_data(uint8_t *public_key, uint8_t *secret_key, uint8_t *nonce, | 100 | int decrypt_data(uint8_t *public_key, uint8_t *secret_key, uint8_t *nonce, |
98 | uint8_t *encrypted, uint32_t length, uint8_t *plain); | 101 | uint8_t *encrypted, uint32_t length, uint8_t *plain); |
99 | 102 | ||
@@ -111,86 +114,102 @@ int decrypt_data_fast(uint8_t *enc_key, uint8_t *nonce, | |||
111 | uint8_t *encrypted, uint32_t length, uint8_t *plain); | 114 | uint8_t *encrypted, uint32_t length, uint8_t *plain); |
112 | 115 | ||
113 | 116 | ||
114 | /* fill the given nonce with random bytes. */ | 117 | /* Fill the given nonce with random bytes. */ |
115 | void random_nonce(uint8_t *nonce); | 118 | void random_nonce(uint8_t *nonce); |
116 | 119 | ||
117 | /* return 0 if there is no received data in the buffer | 120 | /* return 0 if there is no received data in the buffer. |
118 | return -1 if the packet was discarded. | 121 | * return -1 if the packet was discarded. |
119 | return length of received data if successful */ | 122 | * return length of received data if successful. |
123 | */ | ||
120 | int read_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data); | 124 | int read_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data); |
121 | 125 | ||
122 | /* return 0 if data could not be put in packet queue | 126 | /* return 0 if data could not be put in packet queue |
123 | return 1 if data was put into the queue */ | 127 | * return 1 if data was put into the queue |
128 | */ | ||
124 | int write_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data, uint32_t length); | 129 | int write_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data, uint32_t length); |
125 | 130 | ||
126 | /* create a request to peer. | 131 | /* Create a request to peer. |
127 | send_public_key and send_secret_key are the pub/secret keys of the sender | 132 | * send_public_key and send_secret_key are the pub/secret keys of the sender. |
128 | recv_public_key is public key of reciever | 133 | * recv_public_key is public key of reciever. |
129 | packet must be an array of MAX_DATA_SIZE big. | 134 | * packet must be an array of MAX_DATA_SIZE big. |
130 | Data represents the data we send with the request with length being the length of the data. | 135 | * Data represents the data we send with the request with length being the length of the data. |
131 | request_id is the id of the request (32 = friend request, 254 = ping request) | 136 | * request_id is the id of the request (32 = friend request, 254 = ping request). |
132 | returns -1 on failure | 137 | * |
133 | returns the length of the created packet on success */ | 138 | * returns -1 on failure. |
139 | * returns the length of the created packet on success. | ||
140 | */ | ||
134 | int create_request(uint8_t *send_public_key, uint8_t *send_secret_key, uint8_t *packet, uint8_t *recv_public_key, | 141 | int create_request(uint8_t *send_public_key, uint8_t *send_secret_key, uint8_t *packet, uint8_t *recv_public_key, |
135 | uint8_t *data, uint32_t length, uint8_t request_id); | 142 | uint8_t *data, uint32_t length, uint8_t request_id); |
136 | 143 | ||
137 | 144 | ||
138 | /* Function to call when request beginning with byte is received */ | 145 | /* Function to call when request beginning with byte is received. */ |
139 | void cryptopacket_registerhandler(Net_Crypto *c, uint8_t byte, cryptopacket_handler_callback cb, void *object); | 146 | void cryptopacket_registerhandler(Net_Crypto *c, uint8_t byte, cryptopacket_handler_callback cb, void *object); |
140 | 147 | ||
141 | /* Start a secure connection with other peer who has public_key and ip_port | 148 | /* Start a secure connection with other peer who has public_key and ip_port. |
142 | returns -1 if failure | 149 | * returns -1 if failure. |
143 | returns crypt_connection_id of the initialized connection if everything went well. */ | 150 | * returns crypt_connection_id of the initialized connection if everything went well. |
151 | */ | ||
144 | int crypto_connect(Net_Crypto *c, uint8_t *public_key, IP_Port ip_port); | 152 | int crypto_connect(Net_Crypto *c, uint8_t *public_key, IP_Port ip_port); |
145 | 153 | ||
146 | /* kill a crypto connection | 154 | /* Kill a crypto connection. |
147 | return 0 if killed successfully | 155 | * return 0 if killed successfully. |
148 | return 1 if there was a problem. */ | 156 | * return 1 if there was a problem. |
157 | */ | ||
149 | int crypto_kill(Net_Crypto *c, int crypt_connection_id); | 158 | int crypto_kill(Net_Crypto *c, int crypt_connection_id); |
150 | 159 | ||
151 | /* handle an incoming connection | 160 | /* Handle an incoming connection. |
152 | return -1 if no crypto inbound connection | 161 | * return -1 if no crypto inbound connection. |
153 | return incoming connection id (Lossless_UDP one) if there is an incoming crypto connection | 162 | * return incoming connection id (Lossless_UDP one) if there is an incoming crypto connection. |
154 | Put the public key of the peer in public_key, the secret_nonce from the handshake into secret_nonce | 163 | * |
155 | and the session public key for the connection in session_key | 164 | * Put the public key of the peer in public_key, the secret_nonce from the handshake into secret_nonce |
156 | to accept it see: accept_crypto_inbound(...) | 165 | * and the session public key for the connection in session_key. |
157 | to refuse it just call kill_connection(...) on the connection id */ | 166 | * to accept it see: accept_crypto_inbound(...). |
167 | * to refuse it just call kill_connection(...) on the connection id. | ||
168 | */ | ||
158 | int crypto_inbound(Net_Crypto *c, uint8_t *public_key, uint8_t *secret_nonce, uint8_t *session_key); | 169 | int crypto_inbound(Net_Crypto *c, uint8_t *public_key, uint8_t *secret_nonce, uint8_t *session_key); |
159 | 170 | ||
160 | /* accept an incoming connection using the parameters provided by crypto_inbound | 171 | /* Accept an incoming connection using the parameters provided by crypto_inbound. |
161 | return -1 if not successful | 172 | * return -1 if not successful. |
162 | returns the crypt_connection_id if successful */ | 173 | * returns the crypt_connection_id if successful. |
174 | */ | ||
163 | int accept_crypto_inbound(Net_Crypto *c, int connection_id, uint8_t *public_key, uint8_t *secret_nonce, | 175 | int accept_crypto_inbound(Net_Crypto *c, int connection_id, uint8_t *public_key, uint8_t *secret_nonce, |
164 | uint8_t *session_key); | 176 | uint8_t *session_key); |
165 | 177 | ||
166 | /* return 0 if no connection, 1 we have sent a handshake, 2 if connexion is not confirmed yet | 178 | /* return 0 if no connection. |
167 | (we have received a handshake but no empty data packet), 3 if the connection is established. | 179 | * return 1 we have sent a handshake |
168 | 4 if the connection is timed out and waiting to be killed */ | 180 | * return 2 if connexion is not confirmed yet (we have received a handshake but no empty data packet). |
181 | * return 3 if the connection is established. | ||
182 | * return 4 if the connection is timed out and waiting to be killed. | ||
183 | */ | ||
169 | int is_cryptoconnected(Net_Crypto *c, int crypt_connection_id); | 184 | int is_cryptoconnected(Net_Crypto *c, int crypt_connection_id); |
170 | 185 | ||
171 | 186 | ||
172 | /* Generate our public and private keys | 187 | /* Generate our public and private keys. |
173 | Only call this function the first time the program starts. */ | 188 | * Only call this function the first time the program starts. |
189 | */ | ||
174 | void new_keys(Net_Crypto *c); | 190 | void new_keys(Net_Crypto *c); |
175 | 191 | ||
176 | /* save the public and private keys to the keys array | 192 | /* Save the public and private keys to the keys array. |
177 | Length must be crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES */ | 193 | * Length must be crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES. |
194 | */ | ||
178 | void save_keys(Net_Crypto *c, uint8_t *keys); | 195 | void save_keys(Net_Crypto *c, uint8_t *keys); |
179 | 196 | ||
180 | /* load the public and private keys from the keys array | 197 | /* Load the public and private keys from the keys array. |
181 | Length must be crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES */ | 198 | * Length must be crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES. |
199 | */ | ||
182 | void load_keys(Net_Crypto *c, uint8_t *keys); | 200 | void load_keys(Net_Crypto *c, uint8_t *keys); |
183 | 201 | ||
184 | /* create new instance of Net_Crypto | 202 | /* Create new instance of Net_Crypto. |
185 | sets all the global connection variables to their default values. */ | 203 | * Sets all the global connection variables to their default values. |
204 | */ | ||
186 | Net_Crypto *new_net_crypto(Networking_Core *net); | 205 | Net_Crypto *new_net_crypto(Networking_Core *net); |
187 | 206 | ||
188 | /* main loop */ | 207 | /* Main loop. */ |
189 | void do_net_crypto(Net_Crypto *c); | 208 | void do_net_crypto(Net_Crypto *c); |
190 | 209 | ||
191 | void kill_net_crypto(Net_Crypto *c); | 210 | void kill_net_crypto(Net_Crypto *c); |
192 | 211 | ||
193 | /* Init the cryptopacket handling */ | 212 | /* Initialize the cryptopacket handling. */ |
194 | void init_cryptopackets(void *dht); | 213 | void init_cryptopackets(void *dht); |
195 | 214 | ||
196 | #ifdef __cplusplus | 215 | #ifdef __cplusplus |
diff --git a/toxcore/network.c b/toxcore/network.c index 2bcf7d61..34775570 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 | /* returns current UNIX time in microseconds (us). */ | 26 | /* return current UNIX time in microseconds (us). */ |
27 | uint64_t current_time(void) | 27 | uint64_t current_time(void) |
28 | { | 28 | { |
29 | uint64_t time; | 29 | uint64_t time; |
@@ -44,12 +44,13 @@ 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 | uint32_t random_int(void) | 50 | uint32_t random_int(void) |
50 | { | 51 | { |
51 | #ifndef VANILLA_NACL | 52 | #ifndef VANILLA_NACL |
52 | //NOTE: this function comes from libsodium | 53 | /* NOTE: this function comes from libsodium. */ |
53 | return randombytes_random(); | 54 | return randombytes_random(); |
54 | #else | 55 | #else |
55 | return random(); | 56 | return random(); |
@@ -57,17 +58,20 @@ uint32_t random_int(void) | |||
57 | } | 58 | } |
58 | 59 | ||
59 | /* Basic network functions: | 60 | /* Basic network functions: |
60 | Function to send packet(data) of length length to ip_port */ | 61 | * Function to send packet(data) of length length to ip_port. |
62 | */ | ||
61 | int sendpacket(int sock, IP_Port ip_port, uint8_t *data, uint32_t length) | 63 | int sendpacket(int sock, IP_Port ip_port, uint8_t *data, uint32_t length) |
62 | { | 64 | { |
63 | ADDR addr = {AF_INET, ip_port.port, ip_port.ip}; | 65 | ADDR addr = {AF_INET, ip_port.port, ip_port.ip}; |
64 | return sendto(sock, (char *) data, length, 0, (struct sockaddr *)&addr, sizeof(addr)); | 66 | return sendto(sock, (char *) data, length, 0, (struct sockaddr *)&addr, sizeof(addr)); |
65 | } | 67 | } |
66 | 68 | ||
67 | /* Function to receive data, ip and port of sender is put into ip_port | 69 | /* Function to receive data |
68 | the packet data into data | 70 | * ip and port of sender is put into ip_port. |
69 | the packet length into length. | 71 | * Packet data is put into data. |
70 | dump all empty packets. */ | 72 | * Packet length is put into length. |
73 | * Dump all empty packets. | ||
74 | */ | ||
71 | static int receivepacket(int sock, IP_Port *ip_port, uint8_t *data, uint32_t *length) | 75 | static int receivepacket(int sock, IP_Port *ip_port, uint8_t *data, uint32_t *length) |
72 | { | 76 | { |
73 | ADDR addr; | 77 | ADDR addr; |
@@ -79,7 +83,7 @@ static int receivepacket(int sock, IP_Port *ip_port, uint8_t *data, uint32_t *le | |||
79 | (*(int32_t *)length) = recvfrom(sock, (char *) data, MAX_UDP_PACKET_SIZE, 0, (struct sockaddr *)&addr, &addrlen); | 83 | (*(int32_t *)length) = recvfrom(sock, (char *) data, MAX_UDP_PACKET_SIZE, 0, (struct sockaddr *)&addr, &addrlen); |
80 | 84 | ||
81 | if (*(int32_t *)length <= 0) | 85 | if (*(int32_t *)length <= 0) |
82 | return -1; /* nothing received or empty packet */ | 86 | return -1; /* Nothing received or empty packet. */ |
83 | 87 | ||
84 | ip_port->ip = addr.ip; | 88 | ip_port->ip = addr.ip; |
85 | ip_port->port = addr.port; | 89 | ip_port->port = addr.port; |
@@ -127,7 +131,7 @@ static int at_startup(void) | |||
127 | return 0; | 131 | return 0; |
128 | } | 132 | } |
129 | 133 | ||
130 | /* TODO: put this somewhere | 134 | /* TODO: Put this somewhere |
131 | static void at_shutdown(void) | 135 | static void at_shutdown(void) |
132 | { | 136 | { |
133 | #ifdef WIN32 | 137 | #ifdef WIN32 |
@@ -136,18 +140,20 @@ static void at_shutdown(void) | |||
136 | } | 140 | } |
137 | */ | 141 | */ |
138 | 142 | ||
139 | /* initialize networking | 143 | /* Initialize networking. |
140 | bind to ip and port | 144 | * Bind to ip and port. |
141 | ip must be in network order EX: 127.0.0.1 = (7F000001) | 145 | * ip must be in network order EX: 127.0.0.1 = (7F000001). |
142 | port is in host byte order (this means don't worry about it) | 146 | * port is in host byte order (this means don't worry about it). |
143 | returns Networking_Core object if no problems | 147 | * |
144 | returns NULL if there are problems */ | 148 | * returns Networking_Core object if no problems |
149 | * returns NULL if there are problems. | ||
150 | */ | ||
145 | Networking_Core *new_networking(IP ip, uint16_t port) | 151 | Networking_Core *new_networking(IP ip, uint16_t port) |
146 | { | 152 | { |
147 | if (at_startup() != 0) | 153 | if (at_startup() != 0) |
148 | return NULL; | 154 | return NULL; |
149 | 155 | ||
150 | /* initialize our socket */ | 156 | /* Initialize our socket. */ |
151 | Networking_Core *temp = calloc(1, sizeof(Networking_Core)); | 157 | Networking_Core *temp = calloc(1, sizeof(Networking_Core)); |
152 | 158 | ||
153 | if (temp == NULL) | 159 | if (temp == NULL) |
@@ -155,10 +161,10 @@ Networking_Core *new_networking(IP ip, uint16_t port) | |||
155 | 161 | ||
156 | temp->sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); | 162 | temp->sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); |
157 | 163 | ||
158 | /* Check for socket error */ | 164 | /* Check for socket error. */ |
159 | #ifdef WIN32 | 165 | #ifdef WIN32 |
160 | 166 | ||
161 | if (temp->sock == INVALID_SOCKET) { /* MSDN recommends this */ | 167 | if (temp->sock == INVALID_SOCKET) { /* MSDN recommends this. */ |
162 | free(temp); | 168 | free(temp); |
163 | return NULL; | 169 | return NULL; |
164 | } | 170 | } |
@@ -172,8 +178,9 @@ Networking_Core *new_networking(IP ip, uint16_t port) | |||
172 | 178 | ||
173 | #endif | 179 | #endif |
174 | 180 | ||
175 | /* Functions to increase the size of the send and receive UDP buffers | 181 | /* Functions to increase the size of the send and receive UDP buffers. |
176 | NOTE: uncomment if necessary */ | 182 | * NOTE: Uncomment if necessary. |
183 | */ | ||
177 | /* | 184 | /* |
178 | int n = 1024 * 1024 * 2; | 185 | int n = 1024 * 1024 * 2; |
179 | if(setsockopt(sock, SOL_SOCKET, SO_RCVBUF, (char*)&n, sizeof(n)) == -1) | 186 | if(setsockopt(sock, SOL_SOCKET, SO_RCVBUF, (char*)&n, sizeof(n)) == -1) |
@@ -185,13 +192,13 @@ Networking_Core *new_networking(IP ip, uint16_t port) | |||
185 | return -1; | 192 | return -1; |
186 | */ | 193 | */ |
187 | 194 | ||
188 | /* Enable broadcast on socket */ | 195 | /* Enable broadcast on socket. */ |
189 | int broadcast = 1; | 196 | int broadcast = 1; |
190 | setsockopt(temp->sock, SOL_SOCKET, SO_BROADCAST, (char *)&broadcast, sizeof(broadcast)); | 197 | setsockopt(temp->sock, SOL_SOCKET, SO_BROADCAST, (char *)&broadcast, sizeof(broadcast)); |
191 | 198 | ||
192 | /* Set socket nonblocking */ | 199 | /* Set socket nonblocking. */ |
193 | #ifdef WIN32 | 200 | #ifdef WIN32 |
194 | /* I think this works for windows */ | 201 | /* I think this works for Windows. */ |
195 | u_long mode = 1; | 202 | u_long mode = 1; |
196 | /* ioctl(sock, FIONBIO, &mode); */ | 203 | /* ioctl(sock, FIONBIO, &mode); */ |
197 | ioctlsocket(temp->sock, FIONBIO, &mode); | 204 | ioctlsocket(temp->sock, FIONBIO, &mode); |
@@ -205,7 +212,7 @@ Networking_Core *new_networking(IP ip, uint16_t port) | |||
205 | return temp; | 212 | return temp; |
206 | } | 213 | } |
207 | 214 | ||
208 | /* function to cleanup networking stuff */ | 215 | /* Function to cleanup networking stuff. */ |
209 | void kill_networking(Networking_Core *net) | 216 | void kill_networking(Networking_Core *net) |
210 | { | 217 | { |
211 | #ifdef WIN32 | 218 | #ifdef WIN32 |
diff --git a/toxcore/network.h b/toxcore/network.h index 3547f79b..7d699762 100644 --- a/toxcore/network.h +++ b/toxcore/network.h | |||
@@ -39,9 +39,9 @@ | |||
39 | #include <windows.h> | 39 | #include <windows.h> |
40 | #include <ws2tcpip.h> | 40 | #include <ws2tcpip.h> |
41 | 41 | ||
42 | #undef VANILLA_NACL /* make sure on windows we use libsodium */ | 42 | #undef VANILLA_NACL /* Make sure on Windows we use libsodium. */ |
43 | 43 | ||
44 | #else //Linux includes | 44 | #else // Linux includes |
45 | 45 | ||
46 | #include <fcntl.h> | 46 | #include <fcntl.h> |
47 | #include <sys/socket.h> | 47 | #include <sys/socket.h> |
@@ -55,7 +55,7 @@ | |||
55 | #endif | 55 | #endif |
56 | 56 | ||
57 | #ifndef VANILLA_NACL | 57 | #ifndef VANILLA_NACL |
58 | /* we use libsodium by default */ | 58 | /* We use libsodium by default. */ |
59 | #include <sodium.h> | 59 | #include <sodium.h> |
60 | #else | 60 | #else |
61 | #include <crypto_box.h> | 61 | #include <crypto_box.h> |
@@ -68,15 +68,15 @@ extern "C" { | |||
68 | 68 | ||
69 | #define MAX_UDP_PACKET_SIZE 65507 | 69 | #define MAX_UDP_PACKET_SIZE 65507 |
70 | 70 | ||
71 | #define NET_PACKET_PING_REQUEST 0 /* Ping request packet ID */ | 71 | #define NET_PACKET_PING_REQUEST 0 /* Ping request packet ID. */ |
72 | #define NET_PACKET_PING_RESPONSE 1 /* Ping response packet ID */ | 72 | #define NET_PACKET_PING_RESPONSE 1 /* Ping response packet ID. */ |
73 | #define NET_PACKET_GET_NODES 2 /* Get nodes request packet ID */ | 73 | #define NET_PACKET_GET_NODES 2 /* Get nodes request packet ID. */ |
74 | #define NET_PACKET_SEND_NODES 3 /* Send nodes response packet ID */ | 74 | #define NET_PACKET_SEND_NODES 3 /* Send nodes response packet ID. */ |
75 | #define NET_PACKET_HANDSHAKE 16 /* Handshake packet ID */ | 75 | #define NET_PACKET_HANDSHAKE 16 /* Handshake packet ID. */ |
76 | #define NET_PACKET_SYNC 17 /* SYNC packet ID */ | 76 | #define NET_PACKET_SYNC 17 /* SYNC packet ID. */ |
77 | #define NET_PACKET_DATA 18 /* Data packet ID */ | 77 | #define NET_PACKET_DATA 18 /* Data packet ID. */ |
78 | #define NET_PACKET_CRYPTO 32 /* Encrypted data packet ID */ | 78 | #define NET_PACKET_CRYPTO 32 /* Encrypted data packet ID. */ |
79 | #define NET_PACKET_LAN_DISCOVERY 33 /* LAN discovery packet ID */ | 79 | #define NET_PACKET_LAN_DISCOVERY 33 /* LAN discovery packet ID. */ |
80 | 80 | ||
81 | 81 | ||
82 | /* Current time, unix format */ | 82 | /* Current time, unix format */ |
@@ -92,7 +92,7 @@ typedef union { | |||
92 | typedef struct { | 92 | typedef struct { |
93 | IP ip; | 93 | IP ip; |
94 | uint16_t port; | 94 | uint16_t port; |
95 | /* not used for anything right now */ | 95 | /* Not used for anything right now. */ |
96 | uint16_t padding; | 96 | uint16_t padding; |
97 | } IP_Port; | 97 | } IP_Port; |
98 | 98 | ||
@@ -106,9 +106,10 @@ typedef struct { | |||
106 | #endif | 106 | #endif |
107 | } ADDR; | 107 | } ADDR; |
108 | 108 | ||
109 | /* Function to receive data, ip and port of sender is put into ip_port | 109 | /* Function to receive data, ip and port of sender is put into ip_port. |
110 | the packet data into data | 110 | * Packet data is put into data. |
111 | the packet length into length. */ | 111 | * Packet length is put into length. |
112 | */ | ||
112 | typedef int (*packet_handler_callback)(void *object, IP_Port ip_port, uint8_t *data, uint32_t len); | 113 | typedef int (*packet_handler_callback)(void *object, IP_Port ip_port, uint8_t *data, uint32_t len); |
113 | 114 | ||
114 | typedef struct { | 115 | typedef struct { |
@@ -118,37 +119,40 @@ typedef struct { | |||
118 | 119 | ||
119 | typedef struct { | 120 | typedef struct { |
120 | Packet_Handles packethandlers[256]; | 121 | Packet_Handles packethandlers[256]; |
121 | /* our UDP socket */ | 122 | /* Our UDP socket. */ |
122 | int sock; | 123 | int sock; |
123 | } Networking_Core; | 124 | } Networking_Core; |
124 | 125 | ||
125 | /* returns current time in milleseconds since the epoch. */ | 126 | /* return current time in milleseconds since the epoch. */ |
126 | uint64_t current_time(void); | 127 | uint64_t current_time(void); |
127 | 128 | ||
128 | /* return a random number | 129 | /* return a random number. |
129 | NOTE: this function should probably not be used where cryptographic randomness is absolutely necessary */ | 130 | * NOTE: this function should probably not be used where cryptographic randomness is absolutely necessary. |
131 | */ | ||
130 | uint32_t random_int(void); | 132 | uint32_t random_int(void); |
131 | 133 | ||
132 | /* Basic network functions: */ | 134 | /* Basic network functions: */ |
133 | 135 | ||
134 | /* Function to send packet(data) of length length to ip_port */ | 136 | /* Function to send packet(data) of length length to ip_port. */ |
135 | int sendpacket(int sock, IP_Port ip_port, uint8_t *data, uint32_t length); | 137 | int sendpacket(int sock, IP_Port ip_port, uint8_t *data, uint32_t length); |
136 | 138 | ||
137 | /* Function to call when packet beginning with byte is received */ | 139 | /* Function to call when packet beginning with byte is received. */ |
138 | void networking_registerhandler(Networking_Core *net, uint8_t byte, packet_handler_callback cb, void *object); | 140 | void networking_registerhandler(Networking_Core *net, uint8_t byte, packet_handler_callback cb, void *object); |
139 | 141 | ||
140 | /* call this several times a second */ | 142 | /* Call this several times a second. */ |
141 | void networking_poll(Networking_Core *net); | 143 | void networking_poll(Networking_Core *net); |
142 | 144 | ||
143 | /* initialize networking | 145 | /* Initialize networking. |
144 | bind to ip and port | 146 | * bind to ip and port. |
145 | ip must be in network order EX: 127.0.0.1 = (7F000001) | 147 | * ip must be in network order EX: 127.0.0.1 = (7F000001). |
146 | port is in host byte order (this means don't worry about it) | 148 | * port is in host byte order (this means don't worry about it). |
147 | returns 0 if no problems | 149 | * |
148 | returns -1 if there were problems */ | 150 | * returns 0 if no problems. |
151 | * returns -1 if there were problems. | ||
152 | */ | ||
149 | Networking_Core *new_networking(IP ip, uint16_t port); | 153 | Networking_Core *new_networking(IP ip, uint16_t port); |
150 | 154 | ||
151 | /* function to cleanup networking stuff(doesn't do much right now) */ | 155 | /* Function to cleanup networking stuff (doesn't do much right now). */ |
152 | void kill_networking(Networking_Core *net); | 156 | void kill_networking(Networking_Core *net); |
153 | 157 | ||
154 | 158 | ||
diff --git a/toxcore/packets.h b/toxcore/packets.h index 4f410fb3..e619f864 100644 --- a/toxcore/packets.h +++ b/toxcore/packets.h | |||
@@ -10,7 +10,7 @@ typedef struct { | |||
10 | 10 | ||
11 | } __attribute__((packed)) clientid_t; | 11 | } __attribute__((packed)) clientid_t; |
12 | 12 | ||
13 | // Ping packet | 13 | // Ping packet. |
14 | typedef struct { | 14 | typedef struct { |
15 | uint8_t packet_id; | 15 | uint8_t packet_id; |
16 | clientid_t client_id; | 16 | clientid_t client_id; |
@@ -20,7 +20,7 @@ typedef struct { | |||
20 | 20 | ||
21 | } __attribute__((packed)) pingreq_t; | 21 | } __attribute__((packed)) pingreq_t; |
22 | 22 | ||
23 | // Pong packet | 23 | // Pong packet. |
24 | typedef struct { | 24 | typedef struct { |
25 | uint8_t packet_id; | 25 | uint8_t packet_id; |
26 | clientid_t client_id; | 26 | clientid_t client_id; |
diff --git a/toxcore/ping.c b/toxcore/ping.c index 55d4d261..1c2de777 100644 --- a/toxcore/ping.c +++ b/toxcore/ping.c | |||
@@ -51,7 +51,7 @@ static void remove_timeouts(void *ping) // O(n) | |||
51 | size_t new_pos = png->pos_pings; | 51 | size_t new_pos = png->pos_pings; |
52 | size_t new_num = png->num_pings; | 52 | size_t new_num = png->num_pings; |
53 | 53 | ||
54 | // Loop through buffer, oldest first | 54 | // Loop through buffer, oldest first. |
55 | for (i = 0; i < png->num_pings; i++) { | 55 | for (i = 0; i < png->num_pings; i++) { |
56 | id = (png->pos_pings + i) % PING_NUM_MAX; | 56 | id = (png->pos_pings + i) % PING_NUM_MAX; |
57 | 57 | ||
@@ -76,13 +76,13 @@ uint64_t add_ping(void *ping, IP_Port ipp) // O(n) | |||
76 | 76 | ||
77 | remove_timeouts(ping); | 77 | remove_timeouts(ping); |
78 | 78 | ||
79 | // Remove oldest ping if full buffer | 79 | /* Remove oldest ping if full buffer. */ |
80 | if (png->num_pings == PING_NUM_MAX) { | 80 | if (png->num_pings == PING_NUM_MAX) { |
81 | png->num_pings--; | 81 | png->num_pings--; |
82 | png->pos_pings = (png->pos_pings + 1) % PING_NUM_MAX; | 82 | png->pos_pings = (png->pos_pings + 1) % PING_NUM_MAX; |
83 | } | 83 | } |
84 | 84 | ||
85 | // Insert new ping at end of list | 85 | /* Insert new ping at end of list. */ |
86 | p = (png->pos_pings + png->num_pings) % PING_NUM_MAX; | 86 | p = (png->pos_pings + png->num_pings) % PING_NUM_MAX; |
87 | 87 | ||
88 | png->pings[p].ipp = ipp; | 88 | png->pings[p].ipp = ipp; |
@@ -93,7 +93,7 @@ uint64_t add_ping(void *ping, IP_Port ipp) // O(n) | |||
93 | return png->pings[p].id; | 93 | return png->pings[p].id; |
94 | } | 94 | } |
95 | 95 | ||
96 | bool is_pinging(void *ping, IP_Port ipp, uint64_t ping_id) // O(n) TODO: replace this with something else. | 96 | bool is_pinging(void *ping, IP_Port ipp, uint64_t ping_id) // O(n) TODO: Replace this with something else. |
97 | { | 97 | { |
98 | PING *png = ping; | 98 | PING *png = ping; |
99 | 99 | ||
@@ -107,7 +107,7 @@ bool is_pinging(void *ping, IP_Port ipp, uint64_t ping_id) // O(n) TODO: repl | |||
107 | for (i = 0; i < png->num_pings; i++) { | 107 | for (i = 0; i < png->num_pings; i++) { |
108 | id = (png->pos_pings + i) % PING_NUM_MAX; | 108 | id = (png->pos_pings + i) % PING_NUM_MAX; |
109 | 109 | ||
110 | // ping_id = 0 means match any id | 110 | /* ping_id = 0 means match any id. */ |
111 | if ((ipp_eq(png->pings[id].ipp, ipp) || ipp.ip.i == 0) && (png->pings[id].id == ping_id || ping_id == 0)) { | 111 | if ((ipp_eq(png->pings[id].ipp, ipp) || ipp.ip.i == 0) && (png->pings[id].id == ping_id || ping_id == 0)) { |
112 | return true; | 112 | return true; |
113 | } | 113 | } |
@@ -125,14 +125,14 @@ int send_ping_request(void *ping, Net_Crypto *c, IP_Port ipp, clientid_t *client | |||
125 | if (is_pinging(ping, ipp, 0) || id_eq(client_id, (clientid_t *)c->self_public_key)) | 125 | if (is_pinging(ping, ipp, 0) || id_eq(client_id, (clientid_t *)c->self_public_key)) |
126 | return 1; | 126 | return 1; |
127 | 127 | ||
128 | // Generate random ping_id | 128 | // Generate random ping_id. |
129 | ping_id = add_ping(ping, ipp); | 129 | ping_id = add_ping(ping, ipp); |
130 | 130 | ||
131 | pk.packet_id = NET_PACKET_PING_REQUEST; | 131 | pk.packet_id = NET_PACKET_PING_REQUEST; |
132 | id_cpy(&pk.client_id, (clientid_t *)c->self_public_key); // Our pubkey | 132 | id_cpy(&pk.client_id, (clientid_t *)c->self_public_key); // Our pubkey. |
133 | random_nonce((uint8_t *) &pk.nonce); // Generate random nonce | 133 | random_nonce((uint8_t *) &pk.nonce); // Generate random nonce. |
134 | 134 | ||
135 | // Encrypt ping_id using recipient privkey | 135 | /* Encrypt ping_id using recipient privkey. */ |
136 | rc = encrypt_data((uint8_t *) client_id, | 136 | rc = encrypt_data((uint8_t *) client_id, |
137 | c->self_secret_key, | 137 | c->self_secret_key, |
138 | (uint8_t *) &pk.nonce, | 138 | (uint8_t *) &pk.nonce, |
@@ -154,10 +154,10 @@ int send_ping_response(Net_Crypto *c, IP_Port ipp, clientid_t *client_id, uint64 | |||
154 | return 1; | 154 | return 1; |
155 | 155 | ||
156 | pk.packet_id = NET_PACKET_PING_RESPONSE; | 156 | pk.packet_id = NET_PACKET_PING_RESPONSE; |
157 | id_cpy(&pk.client_id, (clientid_t *)c->self_public_key); // Our pubkey | 157 | id_cpy(&pk.client_id, (clientid_t *)c->self_public_key); // Our pubkey. |
158 | random_nonce((uint8_t *) &pk.nonce); // Generate random nonce | 158 | random_nonce((uint8_t *) &pk.nonce); // Generate random nonce. |
159 | 159 | ||
160 | // Encrypt ping_id using recipient privkey | 160 | /* Encrypt ping_id using recipient privkey */ |
161 | rc = encrypt_data((uint8_t *) client_id, | 161 | rc = encrypt_data((uint8_t *) client_id, |
162 | c->self_secret_key, | 162 | c->self_secret_key, |
163 | (uint8_t *) &pk.nonce, | 163 | (uint8_t *) &pk.nonce, |
@@ -180,7 +180,7 @@ int handle_ping_request(void *object, IP_Port source, uint8_t *packet, uint32_t | |||
180 | if (length != sizeof(pingreq_t) || id_eq(&p->client_id, (clientid_t *)dht->c->self_public_key)) | 180 | if (length != sizeof(pingreq_t) || id_eq(&p->client_id, (clientid_t *)dht->c->self_public_key)) |
181 | return 1; | 181 | return 1; |
182 | 182 | ||
183 | // Decrypt ping_id | 183 | /* Decrypt ping_id. */ |
184 | rc = decrypt_data((uint8_t *) &p->client_id, | 184 | rc = decrypt_data((uint8_t *) &p->client_id, |
185 | dht->c->self_secret_key, | 185 | dht->c->self_secret_key, |
186 | (uint8_t *) &p->nonce, | 186 | (uint8_t *) &p->nonce, |
@@ -191,7 +191,7 @@ int handle_ping_request(void *object, IP_Port source, uint8_t *packet, uint32_t | |||
191 | if (rc != sizeof(ping_id)) | 191 | if (rc != sizeof(ping_id)) |
192 | return 1; | 192 | return 1; |
193 | 193 | ||
194 | // Send response | 194 | /* Send response. */ |
195 | send_ping_response(dht->c, source, &p->client_id, ping_id); | 195 | send_ping_response(dht->c, source, &p->client_id, ping_id); |
196 | add_toping(dht, (uint8_t *) &p->client_id, source); | 196 | add_toping(dht, (uint8_t *) &p->client_id, source); |
197 | 197 | ||
@@ -208,7 +208,7 @@ int handle_ping_response(void *object, IP_Port source, uint8_t *packet, uint32_t | |||
208 | if (length != sizeof(pingres_t) || id_eq(&p->client_id, (clientid_t *)dht->c->self_public_key)) | 208 | if (length != sizeof(pingres_t) || id_eq(&p->client_id, (clientid_t *)dht->c->self_public_key)) |
209 | return 1; | 209 | return 1; |
210 | 210 | ||
211 | // Decrypt ping_id | 211 | /* Decrypt ping_id. */ |
212 | rc = decrypt_data((uint8_t *) &p->client_id, | 212 | rc = decrypt_data((uint8_t *) &p->client_id, |
213 | dht->c->self_secret_key, | 213 | dht->c->self_secret_key, |
214 | (uint8_t *) &p->nonce, | 214 | (uint8_t *) &p->nonce, |
@@ -219,11 +219,11 @@ int handle_ping_response(void *object, IP_Port source, uint8_t *packet, uint32_t | |||
219 | if (rc != sizeof(ping_id)) | 219 | if (rc != sizeof(ping_id)) |
220 | return 1; | 220 | return 1; |
221 | 221 | ||
222 | // Make sure ping_id is correct | 222 | /* Make sure ping_id is correct. */ |
223 | if (!is_pinging(dht->ping, source, ping_id)) | 223 | if (!is_pinging(dht->ping, source, ping_id)) |
224 | return 1; | 224 | return 1; |
225 | 225 | ||
226 | // Associate source ip with client_id | 226 | /* Associate source ip with client_id. */ |
227 | addto_lists(dht, source, (uint8_t *) &p->client_id); | 227 | addto_lists(dht, source, (uint8_t *) &p->client_id); |
228 | return 0; | 228 | return 0; |
229 | } | 229 | } |
diff --git a/toxcore/tox.c b/toxcore/tox.c index a97e52bc..494de478 100644 --- a/toxcore/tox.c +++ b/toxcore/tox.c | |||
@@ -24,7 +24,7 @@ | |||
24 | #include "Messenger.h" | 24 | #include "Messenger.h" |
25 | /* | 25 | /* |
26 | * returns a FRIEND_ADDRESS_SIZE byte address to give to others. | 26 | * returns a FRIEND_ADDRESS_SIZE byte address to give to others. |
27 | * format: [client_id (32 bytes)][nospam number (4 bytes)][checksum (2 bytes)] | 27 | * Format: [client_id (32 bytes)][nospam number (4 bytes)][checksum (2 bytes)] |
28 | * | 28 | * |
29 | */ | 29 | */ |
30 | void tox_getaddress(void *tox, uint8_t *address) | 30 | void tox_getaddress(void *tox, uint8_t *address) |
@@ -34,20 +34,20 @@ void tox_getaddress(void *tox, uint8_t *address) | |||
34 | } | 34 | } |
35 | 35 | ||
36 | /* | 36 | /* |
37 | * add a friend | 37 | * Add a friend. |
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 | * returns the friend number if success. |
42 | * return FA_TOOLONG if message length is too long | 42 | * return FA_TOOLONG if message length is too long. |
43 | * return FAERR_NOMESSAGE if no message (message length must be >= 1 byte) | 43 | * return FAERR_NOMESSAGE if no message (message length must be >= 1 byte). |
44 | * return FAERR_OWNKEY if user's own key | 44 | * return FAERR_OWNKEY if user's own key. |
45 | * return FAERR_ALREADYSENT if friend request already sent or already a friend | 45 | * return FAERR_ALREADYSENT if friend request already sent or already a friend. |
46 | * return FAERR_UNKNOWN for unknown error | 46 | * return FAERR_UNKNOWN for unknown error. |
47 | * return FAERR_BADCHECKSUM if bad checksum in address | 47 | * return FAERR_BADCHECKSUM if bad checksum in address. |
48 | * return FAERR_SETNEWNOSPAM if the friend was already there but the nospam was different | 48 | * return FAERR_SETNEWNOSPAM if the friend was already there but the nospam was different. |
49 | * (the nospam for that friend was set to the new one) | 49 | * (the nospam for that friend was set to the new one). |
50 | * return FAERR_NOMEM if increasing the friend list size fails | 50 | * return FAERR_NOMEM if increasing the friend list size fails. |
51 | */ | 51 | */ |
52 | int tox_addfriend(void *tox, uint8_t *address, uint8_t *data, uint16_t length) | 52 | int tox_addfriend(void *tox, uint8_t *address, uint8_t *data, uint16_t length) |
53 | { | 53 | { |
@@ -55,9 +55,10 @@ int tox_addfriend(void *tox, uint8_t *address, uint8_t *data, uint16_t length) | |||
55 | return m_addfriend(m, address, data, length); | 55 | return m_addfriend(m, address, data, length); |
56 | } | 56 | } |
57 | 57 | ||
58 | /* add a friend without sending a friendrequest. | 58 | /* Add a friend without sending a friendrequest. |
59 | returns the friend number if success | 59 | * returns the friend number if success. |
60 | return -1 if failure. */ | 60 | * return -1 if failure. |
61 | */ | ||
61 | int tox_addfriend_norequest(void *tox, uint8_t *client_id) | 62 | int tox_addfriend_norequest(void *tox, uint8_t *client_id) |
62 | { | 63 | { |
63 | Messenger *m = tox; | 64 | Messenger *m = tox; |
@@ -65,48 +66,53 @@ int tox_addfriend_norequest(void *tox, uint8_t *client_id) | |||
65 | } | 66 | } |
66 | 67 | ||
67 | /* return the friend id associated to that client id. | 68 | /* return the friend id associated to that client id. |
68 | return -1 if no such friend */ | 69 | * return -1 if no such friend. |
70 | */ | ||
69 | int tox_getfriend_id(void *tox, uint8_t *client_id) | 71 | int tox_getfriend_id(void *tox, uint8_t *client_id) |
70 | { | 72 | { |
71 | Messenger *m = tox; | 73 | Messenger *m = tox; |
72 | return getfriend_id(m, client_id); | 74 | return getfriend_id(m, client_id); |
73 | } | 75 | } |
74 | 76 | ||
75 | /* copies the public key associated to that friend id into client_id buffer. | 77 | /* Copies the public key associated to that friend id into client_id buffer. |
76 | make sure that client_id is of size CLIENT_ID_SIZE. | 78 | * Make sure that client_id is of size CLIENT_ID_SIZE. |
77 | return 0 if success | 79 | * return 0 if success. |
78 | return -1 if failure */ | 80 | * return -1 if failure. |
81 | */ | ||
79 | int tox_getclient_id(void *tox, int friend_id, uint8_t *client_id) | 82 | int tox_getclient_id(void *tox, int friend_id, uint8_t *client_id) |
80 | { | 83 | { |
81 | Messenger *m = tox; | 84 | Messenger *m = tox; |
82 | return getclient_id(m, friend_id, client_id); | 85 | return getclient_id(m, friend_id, client_id); |
83 | } | 86 | } |
84 | 87 | ||
85 | /* remove a friend */ | 88 | /* Remove a friend. */ |
86 | int tox_delfriend(void *tox, int friendnumber) | 89 | int tox_delfriend(void *tox, int friendnumber) |
87 | { | 90 | { |
88 | Messenger *m = tox; | 91 | Messenger *m = tox; |
89 | return m_delfriend(m, friendnumber); | 92 | return m_delfriend(m, friendnumber); |
90 | } | 93 | } |
91 | 94 | ||
92 | /* return 4 if friend is online | 95 | /* return 4 if friend is online. |
93 | return 3 if friend is confirmed | 96 | * return 3 if friend is confirmed. |
94 | return 2 if the friend request was sent | 97 | * return 2 if the friend request was sent. |
95 | return 1 if the friend was added | 98 | * return 1 if the friend was added. |
96 | return 0 if there is no friend with that number */ | 99 | * return 0 if there is no friend with that number. |
100 | */ | ||
97 | int tox_friendstatus(void *tox, int friendnumber) | 101 | int tox_friendstatus(void *tox, int friendnumber) |
98 | { | 102 | { |
99 | Messenger *m = tox; | 103 | Messenger *m = tox; |
100 | return m_friendstatus(m, friendnumber); | 104 | return m_friendstatus(m, friendnumber); |
101 | } | 105 | } |
102 | 106 | ||
103 | /* send a text chat message to an online friend | 107 | /* Send a text chat message to an online friend. |
104 | returns the message id if packet was successfully put into the send queue | 108 | * returns the message id if packet was successfully put into the send queue. |
105 | return 0 if it was not | 109 | * return 0 if it was not. |
106 | you will want to retain the return value, it will be passed to your read receipt callback | 110 | * |
107 | if one is received. | 111 | * You will want to retain the return value, it will be passed to your read receipt callback |
108 | m_sendmessage_withid will send a message with the id of your choosing, | 112 | * if one is received. |
109 | however we can generate an id for you by calling plain m_sendmessage. */ | 113 | * m_sendmessage_withid will send a message with the id of your choosing, |
114 | * however we can generate an id for you by calling plain m_sendmessage. | ||
115 | */ | ||
110 | uint32_t tox_sendmessage(void *tox, int friendnumber, uint8_t *message, uint32_t length) | 116 | uint32_t tox_sendmessage(void *tox, int friendnumber, uint8_t *message, uint32_t length) |
111 | { | 117 | { |
112 | Messenger *m = tox; | 118 | Messenger *m = tox; |
@@ -119,45 +125,47 @@ uint32_t tox_sendmessage_withid(void *tox, int friendnumber, uint32_t theid, uin | |||
119 | return m_sendmessage_withid(m, friendnumber, theid, message, length); | 125 | return m_sendmessage_withid(m, friendnumber, theid, message, length); |
120 | } | 126 | } |
121 | 127 | ||
122 | /* send an action to an online friend | 128 | /* Send an action to an online friend. |
123 | returns 1 if packet was successfully put into the send queue | 129 | * returns 1 if packet was successfully put into the send queue. |
124 | return 0 if it was not */ | 130 | * return 0 if it was not. |
131 | */ | ||
125 | int tox_sendaction(void *tox, int friendnumber, uint8_t *action, uint32_t length) | 132 | int tox_sendaction(void *tox, int friendnumber, uint8_t *action, uint32_t length) |
126 | { | 133 | { |
127 | Messenger *m = tox; | 134 | Messenger *m = tox; |
128 | return m_sendaction(m, friendnumber, action, length); | 135 | return m_sendaction(m, friendnumber, action, length); |
129 | } | 136 | } |
130 | 137 | ||
131 | /* Set our nickname | 138 | /* Set our nickname. |
132 | name must be a string of maximum MAX_NAME_LENGTH length. | 139 | * name must be a string of maximum MAX_NAME_LENGTH length. |
133 | length must be at least 1 byte | 140 | * length must be at least 1 byte. |
134 | length is the length of name with the NULL terminator | 141 | * length is the length of name with the NULL terminator. |
135 | return 0 if success | 142 | * return 0 if success. |
136 | return -1 if failure */ | 143 | * return -1 if failure. |
144 | */ | ||
137 | int tox_setname(void *tox, uint8_t *name, uint16_t length) | 145 | int tox_setname(void *tox, uint8_t *name, uint16_t length) |
138 | { | 146 | { |
139 | Messenger *m = tox; | 147 | Messenger *m = tox; |
140 | return setname(m, name, length); | 148 | return setname(m, name, length); |
141 | } | 149 | } |
142 | 150 | ||
143 | /* | 151 | /* Get your nickname. |
144 | Get your nickname. | 152 | * m - The messanger context to use. |
145 | m The messanger context to use. | 153 | * name - Pointer to a string for the name. |
146 | name Pointer to a string for the name. | 154 | * nlen - The length of the string buffer. |
147 | nlen The length of the string buffer. | 155 | * return length of the name. |
148 | returns Return the length of the name, 0 on error. | 156 | * return 0 on error. |
149 | */ | 157 | */ |
150 | uint16_t tox_getselfname(void *tox, uint8_t *name, uint16_t nlen) | 158 | uint16_t tox_getselfname(void *tox, uint8_t *name, uint16_t nlen) |
151 | { | 159 | { |
152 | Messenger *m = tox; | 160 | Messenger *m = tox; |
153 | return getself_name(m, name, nlen); | 161 | return getself_name(m, name, nlen); |
154 | } | 162 | } |
155 | 163 | ||
156 | /* get name of friendnumber | 164 | /* Get name of friendnumber and put it in name. |
157 | put it in name | 165 | * name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH (128) bytes. |
158 | name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH (128) bytes. | 166 | * return 0 if success. |
159 | return 0 if success | 167 | * return -1 if failure. |
160 | return -1 if failure */ | 168 | */ |
161 | int tox_getname(void *tox, int friendnumber, uint8_t *name) | 169 | int tox_getname(void *tox, int friendnumber, uint8_t *name) |
162 | { | 170 | { |
163 | Messenger *m = tox; | 171 | Messenger *m = tox; |
@@ -179,18 +187,19 @@ int tox_set_userstatus(void *tox, USERSTATUS status) | |||
179 | return m_set_userstatus(m, status); | 187 | return m_set_userstatus(m, status); |
180 | } | 188 | } |
181 | 189 | ||
182 | /* return the length of friendnumber's status message, | 190 | /* return the length of friendnumber's status message, including null. |
183 | including null | 191 | * Pass it into malloc. |
184 | pass it into malloc */ | 192 | */ |
185 | int tox_get_statusmessage_size(void *tox, int friendnumber) | 193 | int tox_get_statusmessage_size(void *tox, int friendnumber) |
186 | { | 194 | { |
187 | Messenger *m = tox; | 195 | Messenger *m = tox; |
188 | return m_get_statusmessage_size(m, friendnumber); | 196 | return m_get_statusmessage_size(m, friendnumber); |
189 | } | 197 | } |
190 | 198 | ||
191 | /* copy friendnumber's status message into buf, truncating if size is over maxlen | 199 | /* Copy friendnumber's status message into buf, truncating if size is over maxlen. |
192 | get the size you need to allocate from m_get_statusmessage_size | 200 | * Get the size you need to allocate from m_get_statusmessage_size. |
193 | The self variant will copy our own status message. */ | 201 | * The self variant will copy our own status message. |
202 | */ | ||
194 | int tox_copy_statusmessage(void *tox, int friendnumber, uint8_t *buf, uint32_t maxlen) | 203 | int tox_copy_statusmessage(void *tox, int friendnumber, uint8_t *buf, uint32_t maxlen) |
195 | { | 204 | { |
196 | Messenger *m = tox; | 205 | Messenger *m = tox; |
@@ -206,7 +215,8 @@ int tox_copy_self_statusmessage(void *tox, uint8_t *buf, uint32_t maxlen) | |||
206 | /* Return one of USERSTATUS values. | 215 | /* Return one of USERSTATUS values. |
207 | * Values unknown to your application should be represented as USERSTATUS_NONE. | 216 | * Values unknown to your application should be represented as USERSTATUS_NONE. |
208 | * As above, the self variant will return our own USERSTATUS. | 217 | * As above, the self variant will return our own USERSTATUS. |
209 | * If friendnumber is invalid, this shall return USERSTATUS_INVALID. */ | 218 | * If friendnumber is invalid, this shall return USERSTATUS_INVALID. |
219 | */ | ||
210 | USERSTATUS tox_get_userstatus(void *tox, int friendnumber) | 220 | USERSTATUS tox_get_userstatus(void *tox, int friendnumber) |
211 | { | 221 | { |
212 | Messenger *m = tox; | 222 | Messenger *m = tox; |
@@ -221,7 +231,8 @@ USERSTATUS tox_get_selfuserstatus(void *tox) | |||
221 | 231 | ||
222 | 232 | ||
223 | /* Sets whether we send read receipts for friendnumber. | 233 | /* Sets whether we send read receipts for friendnumber. |
224 | * This function is not lazy, and it will fail if yesno is not (0 or 1).*/ | 234 | * This function is not lazy, and it will fail if yesno is not (0 or 1). |
235 | */ | ||
225 | void tox_set_sends_receipts(void *tox, int friendnumber, int yesno) | 236 | void tox_set_sends_receipts(void *tox, int friendnumber, int yesno) |
226 | { | 237 | { |
227 | Messenger *m = tox; | 238 | Messenger *m = tox; |
@@ -229,8 +240,9 @@ void tox_set_sends_receipts(void *tox, int friendnumber, int yesno) | |||
229 | } | 240 | } |
230 | 241 | ||
231 | 242 | ||
232 | /* set the function that will be executed when a friend request is received. | 243 | /* Set the function that will be executed when a friend request is received. |
233 | function format is function(uint8_t * public_key, uint8_t * data, uint16_t length) */ | 244 | * Function format is function(uint8_t * public_key, uint8_t * data, uint16_t length) |
245 | */ | ||
234 | void tox_callback_friendrequest(void *tox, void (*function)(uint8_t *, uint8_t *, uint16_t, void *), void *userdata) | 246 | void tox_callback_friendrequest(void *tox, void (*function)(uint8_t *, uint8_t *, uint16_t, void *), void *userdata) |
235 | { | 247 | { |
236 | Messenger *m = tox; | 248 | Messenger *m = tox; |
@@ -238,8 +250,9 @@ void tox_callback_friendrequest(void *tox, void (*function)(uint8_t *, uint8_t * | |||
238 | } | 250 | } |
239 | 251 | ||
240 | 252 | ||
241 | /* set the function that will be executed when a message from a friend is received. | 253 | /* Set the function that will be executed when a message from a friend is received. |
242 | function format is: function(int friendnumber, uint8_t * message, uint32_t length) */ | 254 | * Function format is: function(int friendnumber, uint8_t * message, uint32_t length) |
255 | */ | ||
243 | void tox_callback_friendmessage(void *tox, void (*function)(Messenger *tox, int, uint8_t *, uint16_t, void *), | 256 | void tox_callback_friendmessage(void *tox, void (*function)(Messenger *tox, int, uint8_t *, uint16_t, void *), |
244 | void *userdata) | 257 | void *userdata) |
245 | { | 258 | { |
@@ -247,17 +260,19 @@ void tox_callback_friendmessage(void *tox, void (*function)(Messenger *tox, int, | |||
247 | m_callback_friendmessage(m, function, userdata); | 260 | m_callback_friendmessage(m, function, userdata); |
248 | } | 261 | } |
249 | 262 | ||
250 | /* set the function that will be executed when an action from a friend is received. | 263 | /* Set the function that will be executed when an action from a friend is received. |
251 | function format is: function(int friendnumber, uint8_t * action, uint32_t length) */ | 264 | * function format is: function(int friendnumber, uint8_t * action, uint32_t length) |
265 | */ | ||
252 | void tox_callback_action(void *tox, void (*function)(Messenger *tox, int, uint8_t *, uint16_t, void *), void *userdata) | 266 | void tox_callback_action(void *tox, void (*function)(Messenger *tox, int, uint8_t *, uint16_t, void *), void *userdata) |
253 | { | 267 | { |
254 | Messenger *m = tox; | 268 | Messenger *m = tox; |
255 | m_callback_action(m, function, userdata); | 269 | m_callback_action(m, function, userdata); |
256 | } | 270 | } |
257 | 271 | ||
258 | /* set the callback for name changes | 272 | /* Set the callback for name changes. |
259 | function(int friendnumber, uint8_t *newname, uint16_t length) | 273 | * function(int friendnumber, uint8_t *newname, uint16_t length) |
260 | you are not responsible for freeing newname */ | 274 | * You are not responsible for freeing newname. |
275 | */ | ||
261 | void tox_callback_namechange(void *tox, void (*function)(Messenger *tox, int, uint8_t *, uint16_t, void *), | 276 | void tox_callback_namechange(void *tox, void (*function)(Messenger *tox, int, uint8_t *, uint16_t, void *), |
262 | void *userdata) | 277 | void *userdata) |
263 | { | 278 | { |
@@ -265,9 +280,10 @@ void tox_callback_namechange(void *tox, void (*function)(Messenger *tox, int, ui | |||
265 | m_callback_namechange(m, function, userdata); | 280 | m_callback_namechange(m, function, userdata); |
266 | } | 281 | } |
267 | 282 | ||
268 | /* set the callback for status message changes | 283 | /* Set the callback for status message changes. |
269 | function(int friendnumber, uint8_t *newstatus, uint16_t length) | 284 | * function(int friendnumber, uint8_t *newstatus, uint16_t length) |
270 | you are not responsible for freeing newstatus */ | 285 | * You are not responsible for freeing newstatus. |
286 | */ | ||
271 | void tox_callback_statusmessage(void *tox, void (*function)(Messenger *tox, int, uint8_t *, uint16_t, void *), | 287 | void tox_callback_statusmessage(void *tox, void (*function)(Messenger *tox, int, uint8_t *, uint16_t, void *), |
272 | void *userdata) | 288 | void *userdata) |
273 | { | 289 | { |
@@ -275,74 +291,83 @@ void tox_callback_statusmessage(void *tox, void (*function)(Messenger *tox, int, | |||
275 | m_callback_statusmessage(m, function, userdata); | 291 | m_callback_statusmessage(m, function, userdata); |
276 | } | 292 | } |
277 | 293 | ||
278 | /* set the callback for status type changes | 294 | /* Set the callback for status type changes. |
279 | function(int friendnumber, USERSTATUS kind) */ | 295 | * function(int friendnumber, USERSTATUS kind) |
296 | */ | ||
280 | void tox_callback_userstatus(void *tox, void (*function)(Messenger *tox, int, USERSTATUS, void *), void *userdata) | 297 | void tox_callback_userstatus(void *tox, void (*function)(Messenger *tox, int, USERSTATUS, void *), void *userdata) |
281 | { | 298 | { |
282 | Messenger *m = tox; | 299 | Messenger *m = tox; |
283 | m_callback_userstatus(m, function, userdata); | 300 | m_callback_userstatus(m, function, userdata); |
284 | } | 301 | } |
285 | 302 | ||
286 | /* set the callback for read receipts | 303 | /* Set the callback for read receipts. |
287 | function(int friendnumber, uint32_t receipt) | 304 | * function(int friendnumber, uint32_t receipt) |
288 | if you are keeping a record of returns from m_sendmessage, | 305 | * |
289 | receipt might be one of those values, and that means the message | 306 | * If you are keeping a record of returns from m_sendmessage; |
290 | has been received on the other side. since core doesn't | 307 | * receipt might be one of those values, meaning the message |
291 | track ids for you, receipt may not correspond to any message | 308 | * has been received on the other side. |
292 | in that case, you should discard it. */ | 309 | * Since core doesn't track ids for you, receipt may not correspond to any message. |
310 | * in that case, you should discard it. | ||
311 | */ | ||
293 | void tox_callback_read_receipt(void *tox, void (*function)(Messenger *tox, int, uint32_t, void *), void *userdata) | 312 | void tox_callback_read_receipt(void *tox, void (*function)(Messenger *tox, int, uint32_t, void *), void *userdata) |
294 | { | 313 | { |
295 | Messenger *m = tox; | 314 | Messenger *m = tox; |
296 | m_callback_read_receipt(m, function, userdata); | 315 | m_callback_read_receipt(m, function, userdata); |
297 | } | 316 | } |
298 | 317 | ||
299 | /* set the callback for connection status changes | 318 | /* Set the callback for connection status changes. |
300 | function(int friendnumber, uint8_t status) | 319 | * function(int friendnumber, uint8_t status) |
301 | status: | 320 | * Status: |
302 | 0 -- friend went offline after being previously online | 321 | * 0 -- friend went offline after being previously online |
303 | 1 -- friend went online | 322 | * 1 -- friend went online |
304 | note that this callback is not called when adding friends, thus the "after | 323 | * |
305 | being previously online" part. it's assumed that when adding friends, | 324 | * NOTE: this callback is not called when adding friends, thus the "after |
306 | their connection status is offline. */ | 325 | * being previously online" part. It's assumed that when adding friends, |
326 | * their connection status is offline. | ||
327 | */ | ||
307 | void tox_callback_connectionstatus(void *tox, void (*function)(Messenger *tox, int, uint8_t, void *), void *userdata) | 328 | void tox_callback_connectionstatus(void *tox, void (*function)(Messenger *tox, int, uint8_t, void *), void *userdata) |
308 | { | 329 | { |
309 | Messenger *m = tox; | 330 | Messenger *m = tox; |
310 | m_callback_connectionstatus(m, function, userdata); | 331 | m_callback_connectionstatus(m, function, userdata); |
311 | } | 332 | } |
312 | 333 | ||
313 | /* Use this function to bootstrap the client | 334 | /* Use this function to bootstrap the client. |
314 | Sends a get nodes request to the given node with ip port and public_key */ | 335 | * Sends a get nodes request to the given node with ip port and public_key. |
336 | */ | ||
315 | void tox_bootstrap(void *tox, IP_Port ip_port, uint8_t *public_key) | 337 | void tox_bootstrap(void *tox, IP_Port ip_port, uint8_t *public_key) |
316 | { | 338 | { |
317 | Messenger *m = tox; | 339 | Messenger *m = tox; |
318 | DHT_bootstrap(m->dht, ip_port, public_key); | 340 | DHT_bootstrap(m->dht, ip_port, public_key); |
319 | } | 341 | } |
320 | 342 | ||
321 | /* returns 0 if we are not connected to the DHT | 343 | /* returns 0 if we are not connected to the DHT. |
322 | returns 1 if we are */ | 344 | * returns 1 if we are. |
345 | */ | ||
323 | int tox_isconnected(void *tox) | 346 | int tox_isconnected(void *tox) |
324 | { | 347 | { |
325 | Messenger *m = tox; | 348 | Messenger *m = tox; |
326 | return DHT_isconnected(m->dht); | 349 | return DHT_isconnected(m->dht); |
327 | } | 350 | } |
328 | 351 | ||
329 | /* run this at startup | 352 | /* Run this at startup. |
330 | * returns allocated instance of tox on success | 353 | * returns allocated instance of tox on success. |
331 | * returns 0 if there are problems */ | 354 | * returns 0 if there are problems. |
355 | */ | ||
332 | void *tox_new(void) | 356 | void *tox_new(void) |
333 | { | 357 | { |
334 | return initMessenger(); | 358 | return initMessenger(); |
335 | } | 359 | } |
336 | 360 | ||
337 | /* run this before closing shop | 361 | /* Run this before closing shop. |
338 | * free all datastructures */ | 362 | * Free all datastructures. |
363 | */ | ||
339 | void tox_kill(void *tox) | 364 | void tox_kill(void *tox) |
340 | { | 365 | { |
341 | Messenger *m = tox; | 366 | Messenger *m = tox; |
342 | cleanupMessenger(m); | 367 | cleanupMessenger(m); |
343 | } | 368 | } |
344 | 369 | ||
345 | /* the main loop that needs to be run at least 20 times per second */ | 370 | /* The main loop that needs to be run at least 20 times per second. */ |
346 | void tox_do(void *tox) | 371 | void tox_do(void *tox) |
347 | { | 372 | { |
348 | Messenger *m = tox; | 373 | Messenger *m = tox; |
@@ -351,21 +376,21 @@ void tox_do(void *tox) | |||
351 | 376 | ||
352 | /* SAVING AND LOADING FUNCTIONS: */ | 377 | /* SAVING AND LOADING FUNCTIONS: */ |
353 | 378 | ||
354 | /* returns the size of the messenger data (for saving) */ | 379 | /* returns the size of the messenger data (for saving). */ |
355 | uint32_t tox_size(void *tox) | 380 | uint32_t tox_size(void *tox) |
356 | { | 381 | { |
357 | Messenger *m = tox; | 382 | Messenger *m = tox; |
358 | return Messenger_size(m); | 383 | return Messenger_size(m); |
359 | } | 384 | } |
360 | 385 | ||
361 | /* save the messenger in data (must be allocated memory of size Messenger_size()) */ | 386 | /* Save the messenger in data (must be allocated memory of size Messenger_size()). */ |
362 | void tox_save(void *tox, uint8_t *data) | 387 | void tox_save(void *tox, uint8_t *data) |
363 | { | 388 | { |
364 | Messenger *m = tox; | 389 | Messenger *m = tox; |
365 | Messenger_save(m, data); | 390 | Messenger_save(m, data); |
366 | } | 391 | } |
367 | 392 | ||
368 | /* load the messenger from data of size length */ | 393 | /* Load the messenger from data of size length. */ |
369 | int tox_load(void *tox, uint8_t *data, uint32_t length) | 394 | int tox_load(void *tox, uint8_t *data, uint32_t length) |
370 | { | 395 | { |
371 | Messenger *m = tox; | 396 | Messenger *m = tox; |
diff --git a/toxcore/tox.h b/toxcore/tox.h index bdfac1d6..5a4dcf49 100644 --- a/toxcore/tox.h +++ b/toxcore/tox.h | |||
@@ -46,11 +46,11 @@ typedef union { | |||
46 | typedef struct { | 46 | typedef struct { |
47 | tox_IP ip; | 47 | tox_IP ip; |
48 | uint16_t port; | 48 | uint16_t port; |
49 | /* not used for anything right now */ | 49 | /* Not used for anything right now. */ |
50 | uint16_t padding; | 50 | uint16_t padding; |
51 | } tox_IP_Port; | 51 | } tox_IP_Port; |
52 | 52 | ||
53 | /* status definitions */ | 53 | /* Status definitions. */ |
54 | enum { | 54 | enum { |
55 | TOX_NOFRIEND, | 55 | TOX_NOFRIEND, |
56 | TOX_FRIEND_ADDED, | 56 | TOX_FRIEND_ADDED, |
@@ -59,8 +59,9 @@ enum { | |||
59 | TOX_FRIEND_ONLINE, | 59 | TOX_FRIEND_ONLINE, |
60 | }; | 60 | }; |
61 | 61 | ||
62 | /* errors for m_addfriend | 62 | /* Errors for m_addfriend |
63 | * FAERR - Friend Add Error */ | 63 | * FAERR - Friend Add Error |
64 | */ | ||
64 | enum { | 65 | enum { |
65 | TOX_FAERR_TOOLONG = -1, | 66 | TOX_FAERR_TOOLONG = -1, |
66 | TOX_FAERR_NOMESSAGE = -2, | 67 | TOX_FAERR_NOMESSAGE = -2, |
@@ -71,9 +72,10 @@ enum { | |||
71 | TOX_FAERR_SETNEWNOSPAM = -7, | 72 | TOX_FAERR_SETNEWNOSPAM = -7, |
72 | TOX_FAERR_NOMEM = -8 | 73 | TOX_FAERR_NOMEM = -8 |
73 | }; | 74 | }; |
74 | /* USERSTATUS | ||
75 | * Represents userstatuses someone can have. */ | ||
76 | 75 | ||
76 | /* USERSTATUS - | ||
77 | * Represents userstatuses someone can have. | ||
78 | */ | ||
77 | typedef enum { | 79 | typedef enum { |
78 | TOX_USERSTATUS_NONE, | 80 | TOX_USERSTATUS_NONE, |
79 | TOX_USERSTATUS_AWAY, | 81 | TOX_USERSTATUS_AWAY, |
@@ -84,201 +86,226 @@ TOX_USERSTATUS; | |||
84 | 86 | ||
85 | typedef void Tox; | 87 | typedef void Tox; |
86 | 88 | ||
87 | /* | 89 | /* returns FRIEND_ADDRESS_SIZE byte address to give to others. |
88 | * returns a FRIEND_ADDRESS_SIZE byte address to give to others. | ||
89 | * 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)] |
90 | * | ||
91 | */ | 91 | */ |
92 | void tox_getaddress(Tox *tox, uint8_t *address); | 92 | void tox_getaddress(Tox *tox, uint8_t *address); |
93 | 93 | ||
94 | /* | 94 | /* Add a friend. |
95 | * add a friend | 95 | * Set the data that will be sent along with friend request. |
96 | * set the data that will be sent along with friend request | ||
97 | * 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. |
98 | * data is the data and length is the length | 97 | * data is the data and length is the length. |
99 | * returns the friend number if success | 98 | * returns the friend number if success. |
100 | * return TOX_FA_TOOLONG if message length is too long | 99 | * return TOX_FA_TOOLONG if message length is too long. |
101 | * return TOX_FAERR_NOMESSAGE if no message (message length must be >= 1 byte) | 100 | * return TOX_FAERR_NOMESSAGE if no message (message length must be >= 1 byte). |
102 | * return TOX_FAERR_OWNKEY if user's own key | 101 | * return TOX_FAERR_OWNKEY if user's own key. |
103 | * return TOX_FAERR_ALREADYSENT if friend request already sent or already a friend | 102 | * return TOX_FAERR_ALREADYSENT if friend request already sent or already a friend. |
104 | * return TOX_FAERR_UNKNOWN for unknown error | 103 | * return TOX_FAERR_UNKNOWN for unknown error. |
105 | * return TOX_FAERR_BADCHECKSUM if bad checksum in address | 104 | * return TOX_FAERR_BADCHECKSUM if bad checksum in address. |
106 | * return TOX_FAERR_SETNEWNOSPAM if the friend was already there but the nospam was different | 105 | * return TOX_FAERR_SETNEWNOSPAM if the friend was already there but the nospam was different. |
107 | * (the nospam for that friend was set to the new one) | 106 | * (the nospam for that friend was set to the new one). |
108 | * return TOX_FAERR_NOMEM if increasing the friend list size fails | 107 | * return TOX_FAERR_NOMEM if increasing the friend list size fails. |
109 | */ | 108 | */ |
110 | int tox_addfriend(Tox *tox, uint8_t *address, uint8_t *data, uint16_t length); | 109 | int tox_addfriend(Tox *tox, uint8_t *address, uint8_t *data, uint16_t length); |
111 | 110 | ||
112 | 111 | ||
113 | /* add a friend without sending a friendrequest. | 112 | /* Add a friend without sending a friendrequest. |
114 | returns the friend number if success | 113 | * returns the friend number if success. |
115 | return -1 if failure. */ | 114 | * return -1 if failure. |
115 | */ | ||
116 | int tox_addfriend_norequest(Tox *tox, uint8_t *client_id); | 116 | int tox_addfriend_norequest(Tox *tox, uint8_t *client_id); |
117 | 117 | ||
118 | /* return the friend id associated to that client id. | 118 | /* return the friend id associated to that client id. |
119 | return -1 if no such friend */ | 119 | return -1 if no such friend */ |
120 | int tox_getfriend_id(Tox *tox, uint8_t *client_id); | 120 | int tox_getfriend_id(Tox *tox, uint8_t *client_id); |
121 | 121 | ||
122 | /* copies the public key associated to that friend id into client_id buffer. | 122 | /* 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. | 123 | * Make sure that client_id is of size CLIENT_ID_SIZE. |
124 | return 0 if success | 124 | * return 0 if success. |
125 | return -1 if failure */ | 125 | * return -1 if failure. |
126 | */ | ||
126 | int tox_getclient_id(Tox *tox, int friend_id, uint8_t *client_id); | 127 | int tox_getclient_id(Tox *tox, int friend_id, uint8_t *client_id); |
127 | 128 | ||
128 | /* remove a friend */ | 129 | /* Remove a friend. */ |
129 | int tox_delfriend(Tox *tox, int friendnumber); | 130 | int tox_delfriend(Tox *tox, int friendnumber); |
130 | 131 | ||
131 | /* return TOX_FRIEND_ONLINE if friend is online | 132 | /* return TOX_FRIEND_ONLINE if friend is online. |
132 | return TOX_FRIEND_CONFIRMED if friend is confirmed | 133 | * return TOX_FRIEND_CONFIRMED if friend is confirmed. |
133 | return TOX_FRIEND_REQUESTED if the friend request was sent | 134 | * return TOX_FRIEND_REQUESTED if the friend request was sent. |
134 | return TOX_FRIEND_ADDED if the friend was added | 135 | * return TOX_FRIEND_ADDED if the friend was added. |
135 | return TOX_NOFRIEND if there is no friend with that number */ | 136 | * return TOX_NOFRIEND if there is no friend with that number. |
137 | */ | ||
136 | int tox_friendstatus(Tox *tox, int friendnumber); | 138 | int tox_friendstatus(Tox *tox, int friendnumber); |
137 | 139 | ||
138 | /* send a text chat message to an online friend | 140 | /* Send a text chat message to an online friend. |
139 | returns the message id if packet was successfully put into the send queue | 141 | * returns the message id if packet was successfully put into the send queue. |
140 | return 0 if it was not | 142 | * return 0 if it was not. |
141 | you will want to retain the return value, it will be passed to your read receipt callback | 143 | * |
142 | if one is received. | 144 | * You will want to retain the return value, it will be passed to your read receipt callback |
143 | m_sendmessage_withid will send a message with the id of your choosing, | 145 | * if one is received. |
144 | however we can generate an id for you by calling plain m_sendmessage. */ | 146 | * m_sendmessage_withid will send a message with the id of your choosing, |
147 | * however we can generate an id for you by calling plain m_sendmessage. | ||
148 | */ | ||
145 | uint32_t tox_sendmessage(Tox *tox, int friendnumber, uint8_t *message, uint32_t length); | 149 | uint32_t tox_sendmessage(Tox *tox, int friendnumber, uint8_t *message, uint32_t length); |
146 | uint32_t tox_sendmessage_withid(Tox *tox, int friendnumber, uint32_t theid, uint8_t *message, uint32_t length); | 150 | uint32_t tox_sendmessage_withid(Tox *tox, int friendnumber, uint32_t theid, uint8_t *message, uint32_t length); |
147 | 151 | ||
148 | /* send an action to an online friend | 152 | /* Send an action to an online friend. |
149 | returns 1 if packet was successfully put into the send queue | 153 | * returns 1 if packet was successfully put into the send queue. |
150 | return 0 if it was not */ | 154 | * return 0 if it was not. |
155 | */ | ||
151 | int tox_sendaction(Tox *tox, int friendnumber, uint8_t *action, uint32_t length); | 156 | int tox_sendaction(Tox *tox, int friendnumber, uint8_t *action, uint32_t length); |
152 | 157 | ||
153 | /* Set our nickname | 158 | /* Set our nickname. |
154 | name must be a string of maximum MAX_NAME_LENGTH length. | 159 | * name must be a string of maximum MAX_NAME_LENGTH length. |
155 | length must be at least 1 byte | 160 | * length must be at least 1 byte. |
156 | length is the length of name with the NULL terminator | 161 | * length is the length of name with the NULL terminator. |
157 | return 0 if success | 162 | * |
158 | return -1 if failure */ | 163 | * return 0 if success. |
164 | * return -1 if failure. | ||
165 | */ | ||
159 | int tox_setname(Tox *tox, uint8_t *name, uint16_t length); | 166 | int tox_setname(Tox *tox, uint8_t *name, uint16_t length); |
160 | 167 | ||
161 | /* | 168 | /* |
162 | Get your nickname. | 169 | * Get your nickname. |
163 | m The messanger context to use. | 170 | * m - The messanger context to use. |
164 | name Pointer to a string for the name. | 171 | * name - Pointer to a string for the name. |
165 | nlen The length of the string buffer. | 172 | * nlen - The length of the string buffer. |
166 | returns Return the length of the name, 0 on error. | 173 | * |
167 | */ | 174 | * returns Return the length of the name, 0 on error. |
175 | */ | ||
168 | uint16_t tox_getselfname(Tox *tox, uint8_t *name, uint16_t nlen); | 176 | uint16_t tox_getselfname(Tox *tox, uint8_t *name, uint16_t nlen); |
169 | 177 | ||
170 | /* get name of friendnumber | 178 | /* Get name of friendnumber and put it in name. |
171 | put it in name | 179 | * name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH (128) bytes. |
172 | name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH (128) bytes. | 180 | * |
173 | return 0 if success | 181 | * return 0 if success. |
174 | return -1 if failure */ | 182 | * return -1 if failure. |
183 | */ | ||
175 | int tox_getname(Tox *tox, int friendnumber, uint8_t *name); | 184 | int tox_getname(Tox *tox, int friendnumber, uint8_t *name); |
176 | 185 | ||
177 | /* set our user status | 186 | /* Set our user status. |
178 | you are responsible for freeing status after | 187 | * You are responsible for freeing status after. |
179 | returns 0 on success, -1 on failure */ | 188 | * returns 0 on success. |
189 | * returns -1 on failure. | ||
190 | */ | ||
180 | int tox_set_statusmessage(Tox *tox, uint8_t *status, uint16_t length); | 191 | int tox_set_statusmessage(Tox *tox, uint8_t *status, uint16_t length); |
181 | int tox_set_userstatus(Tox *tox, TOX_USERSTATUS status); | 192 | int tox_set_userstatus(Tox *tox, TOX_USERSTATUS status); |
182 | 193 | ||
183 | /* return the length of friendnumber's status message, | 194 | /* return the length of friendnumber's status message, including null. |
184 | including null | 195 | * Pass it into malloc |
185 | pass it into malloc */ | 196 | */ |
186 | int tox_get_statusmessage_size(Tox *tox, int friendnumber); | 197 | int tox_get_statusmessage_size(Tox *tox, int friendnumber); |
187 | 198 | ||
188 | /* copy friendnumber's status message into buf, truncating if size is over maxlen | 199 | /* Copy friendnumber's status message into buf, truncating if size is over maxlen. |
189 | get the size you need to allocate from m_get_statusmessage_size | 200 | * Get the size you need to allocate from m_get_statusmessage_size. |
190 | The self variant will copy our own status message. */ | 201 | * The self variant will copy our own status message. |
202 | */ | ||
191 | int tox_copy_statusmessage(Tox *tox, int friendnumber, uint8_t *buf, uint32_t maxlen); | 203 | int tox_copy_statusmessage(Tox *tox, int friendnumber, uint8_t *buf, uint32_t maxlen); |
192 | int tox_copy_self_statusmessage(Tox *tox, uint8_t *buf, uint32_t maxlen); | 204 | int tox_copy_self_statusmessage(Tox *tox, uint8_t *buf, uint32_t maxlen); |
193 | 205 | ||
194 | /* Return one of USERSTATUS values. | 206 | /* return one of USERSTATUS values. |
195 | * Values unknown to your application should be represented as USERSTATUS_NONE. | 207 | * Values unknown to your application should be represented as USERSTATUS_NONE. |
196 | * As above, the self variant will return our own USERSTATUS. | 208 | * As above, the self variant will return our own USERSTATUS. |
197 | * If friendnumber is invalid, this shall return USERSTATUS_INVALID. */ | 209 | * If friendnumber is invalid, this shall return USERSTATUS_INVALID. |
210 | */ | ||
198 | TOX_USERSTATUS tox_get_userstatus(Tox *tox, int friendnumber); | 211 | TOX_USERSTATUS tox_get_userstatus(Tox *tox, int friendnumber); |
199 | TOX_USERSTATUS tox_get_selfuserstatus(Tox *tox); | 212 | TOX_USERSTATUS tox_get_selfuserstatus(Tox *tox); |
200 | 213 | ||
201 | /* Sets whether we send read receipts for friendnumber. | 214 | /* Sets whether we send read receipts for friendnumber. |
202 | * This function is not lazy, and it will fail if yesno is not (0 or 1).*/ | 215 | * This function is not lazy, and it will fail if yesno is not (0 or 1). |
216 | */ | ||
203 | void tox_set_sends_receipts(Tox *tox, int friendnumber, int yesno); | 217 | void tox_set_sends_receipts(Tox *tox, int friendnumber, int yesno); |
204 | 218 | ||
205 | /* set the function that will be executed when a friend request is received. | 219 | /* Set the function that will be executed when a friend request is received. |
206 | function format is function(uint8_t * public_key, uint8_t * data, uint16_t length) */ | 220 | * Function format is function(uint8_t * public_key, uint8_t * data, uint16_t length) |
221 | */ | ||
207 | void tox_callback_friendrequest(Tox *tox, void (*function)(uint8_t *, uint8_t *, uint16_t, void *), void *userdata); | 222 | void tox_callback_friendrequest(Tox *tox, void (*function)(uint8_t *, uint8_t *, uint16_t, void *), void *userdata); |
208 | 223 | ||
209 | /* set the function that will be executed when a message from a friend is received. | 224 | /* Set the function that will be executed when a message from a friend is received. |
210 | function format is: function(int friendnumber, uint8_t * message, uint32_t length) */ | 225 | * Function format is: function(int friendnumber, uint8_t * message, uint32_t length) |
226 | */ | ||
211 | void tox_callback_friendmessage(Tox *tox, void (*function)(Tox *tox, int, uint8_t *, uint16_t, void *), | 227 | void tox_callback_friendmessage(Tox *tox, void (*function)(Tox *tox, int, uint8_t *, uint16_t, void *), |
212 | void *userdata); | 228 | void *userdata); |
213 | 229 | ||
214 | /* set the function that will be executed when an action from a friend is received. | 230 | /* Set the function that will be executed when an action from a friend is received. |
215 | function format is: function(int friendnumber, uint8_t * action, uint32_t length) */ | 231 | * Function format is: function(int friendnumber, uint8_t * action, uint32_t length) |
232 | */ | ||
216 | void tox_callback_action(Tox *tox, void (*function)(Tox *tox, int, uint8_t *, uint16_t, void *), void *userdata); | 233 | void tox_callback_action(Tox *tox, void (*function)(Tox *tox, int, uint8_t *, uint16_t, void *), void *userdata); |
217 | 234 | ||
218 | /* set the callback for name changes | 235 | /* Set the callback for name changes. |
219 | function(int friendnumber, uint8_t *newname, uint16_t length) | 236 | * function(int friendnumber, uint8_t *newname, uint16_t length) |
220 | you are not responsible for freeing newname */ | 237 | * You are not responsible for freeing newname |
238 | */ | ||
221 | void tox_callback_namechange(Tox *tox, void (*function)(Tox *tox, int, uint8_t *, uint16_t, void *), | 239 | void tox_callback_namechange(Tox *tox, void (*function)(Tox *tox, int, uint8_t *, uint16_t, void *), |
222 | void *userdata); | 240 | void *userdata); |
223 | 241 | ||
224 | /* set the callback for status message changes | 242 | /* Set the callback for status message changes. |
225 | function(int friendnumber, uint8_t *newstatus, uint16_t length) | 243 | * function(int friendnumber, uint8_t *newstatus, uint16_t length) |
226 | you are not responsible for freeing newstatus */ | 244 | * You are not responsible for freeing newstatus. |
245 | */ | ||
227 | void tox_callback_statusmessage(Tox *tox, void (*function)(Tox *tox, int, uint8_t *, uint16_t, void *), | 246 | void tox_callback_statusmessage(Tox *tox, void (*function)(Tox *tox, int, uint8_t *, uint16_t, void *), |
228 | void *userdata); | 247 | void *userdata); |
229 | 248 | ||
230 | /* set the callback for status type changes | 249 | /* Set the callback for status type changes. |
231 | function(int friendnumber, USERSTATUS kind) */ | 250 | * function(int friendnumber, USERSTATUS kind) |
251 | */ | ||
232 | void tox_callback_userstatus(Tox *tox, void (*function)(Tox *tox, int, TOX_USERSTATUS, void *), void *userdata); | 252 | void tox_callback_userstatus(Tox *tox, void (*function)(Tox *tox, int, TOX_USERSTATUS, void *), void *userdata); |
233 | 253 | ||
234 | /* set the callback for read receipts | 254 | /* Set the callback for read receipts. |
235 | function(int friendnumber, uint32_t receipt) | 255 | * function(int friendnumber, uint32_t receipt) |
236 | if you are keeping a record of returns from m_sendmessage, | 256 | * |
237 | receipt might be one of those values, and that means the message | 257 | * If you are keeping a record of returns from m_sendmessage; |
238 | has been received on the other side. since core doesn't | 258 | * receipt might be one of those values, meaning the message |
239 | track ids for you, receipt may not correspond to any message | 259 | * has been received on the other side. |
240 | in that case, you should discard it. */ | 260 | * Since core doesn't track ids for you, receipt may not correspond to any message. |
261 | * In that case, you should discard it. | ||
262 | */ | ||
241 | void tox_callback_read_receipt(Tox *tox, void (*function)(Tox *tox, int, uint32_t, void *), void *userdata); | 263 | void tox_callback_read_receipt(Tox *tox, void (*function)(Tox *tox, int, uint32_t, void *), void *userdata); |
242 | 264 | ||
243 | /* set the callback for connection status changes | 265 | /* Set the callback for connection status changes. |
244 | function(int friendnumber, uint8_t status) | 266 | * function(int friendnumber, uint8_t status) |
245 | status: | 267 | * Status: |
246 | 0 -- friend went offline after being previously online | 268 | * 0 -- friend went offline after being previously online |
247 | 1 -- friend went online | 269 | * 1 -- friend went online |
248 | note that this callback is not called when adding friends, thus the "after | 270 | * |
249 | being previously online" part. it's assumed that when adding friends, | 271 | * NOTE: This callback is not called when adding friends, thus the "after |
250 | their connection status is offline. */ | 272 | * being previously online" part. it's assumed that when adding friends, |
273 | * their connection status is offline. | ||
274 | */ | ||
251 | void tox_callback_connectionstatus(Tox *tox, void (*function)(Tox *tox, int, uint8_t, void *), void *userdata); | 275 | void tox_callback_connectionstatus(Tox *tox, void (*function)(Tox *tox, int, uint8_t, void *), void *userdata); |
252 | 276 | ||
253 | /* Use this function to bootstrap the client | 277 | /* Use this function to bootstrap the client. |
254 | Sends a get nodes request to the given node with ip port and public_key */ | 278 | * Sends a get nodes request to the given node with ip port and public_key. |
279 | */ | ||
255 | void tox_bootstrap(Tox *tox, tox_IP_Port ip_port, uint8_t *public_key); | 280 | void tox_bootstrap(Tox *tox, tox_IP_Port ip_port, uint8_t *public_key); |
256 | 281 | ||
257 | /* returns 0 if we are not connected to the DHT | 282 | /* returns 0 if we are not connected to the DHT. |
258 | returns 1 if we are */ | 283 | * returns 1 if we are. |
284 | */ | ||
259 | int tox_isconnected(Tox *tox); | 285 | int tox_isconnected(Tox *tox); |
260 | 286 | ||
261 | /* run this at startup | 287 | /* Run this at startup. |
262 | * returns allocated instance of tox on success | 288 | * returns allocated instance of tox on success. |
263 | * returns 0 if there are problems */ | 289 | * returns 0 if there are problems. |
290 | */ | ||
264 | Tox *tox_new(void); | 291 | Tox *tox_new(void); |
265 | 292 | ||
266 | /* run this before closing shop | 293 | /* Run this before closing shop. |
267 | * free all datastructures */ | 294 | * Free all datastructures. */ |
268 | void tox_kill(Tox *tox); | 295 | void tox_kill(Tox *tox); |
269 | 296 | ||
270 | /* the main loop that needs to be run at least 20 times per second */ | 297 | /* The main loop that needs to be run at least 20 times per second. */ |
271 | void tox_do(Tox *tox); | 298 | void tox_do(Tox *tox); |
272 | 299 | ||
273 | /* SAVING AND LOADING FUNCTIONS: */ | 300 | /* SAVING AND LOADING FUNCTIONS: */ |
274 | 301 | ||
275 | /* returns the size of the messenger data (for saving) */ | 302 | /* returns the size of the messenger data (for saving). */ |
276 | uint32_t tox_size(Tox *tox); | 303 | uint32_t tox_size(Tox *tox); |
277 | 304 | ||
278 | /* save the messenger in data (must be allocated memory of size Messenger_size()) */ | 305 | /* Save the messenger in data (must be allocated memory of size Messenger_size()). */ |
279 | void tox_save(Tox *tox, uint8_t *data); | 306 | void tox_save(Tox *tox, uint8_t *data); |
280 | 307 | ||
281 | /* load the messenger from data of size length */ | 308 | /* Load the messenger from data of size length. */ |
282 | int tox_load(Tox *tox, uint8_t *data, uint32_t length); | 309 | int tox_load(Tox *tox, uint8_t *data, uint32_t length); |
283 | 310 | ||
284 | 311 | ||