diff options
Diffstat (limited to 'toxcore/Messenger.c')
-rw-r--r-- | toxcore/Messenger.c | 70 |
1 files changed, 33 insertions, 37 deletions
diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index 308cf14e..1a5c93aa 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c | |||
@@ -406,10 +406,10 @@ uint32_t m_sendmessage(Messenger *m, int32_t friendnumber, uint8_t *message, uin | |||
406 | 406 | ||
407 | uint32_t m_sendmessage_withid(Messenger *m, int32_t friendnumber, uint32_t theid, uint8_t *message, uint32_t length) | 407 | uint32_t m_sendmessage_withid(Messenger *m, int32_t friendnumber, uint32_t theid, uint8_t *message, uint32_t length) |
408 | { | 408 | { |
409 | if (length >= (MAX_DATA_SIZE - sizeof(theid))) | 409 | if (length >= (MAX_CRYPTO_DATA_SIZE - sizeof(theid))) |
410 | return 0; | 410 | return 0; |
411 | 411 | ||
412 | uint8_t temp[MAX_DATA_SIZE]; | 412 | uint8_t temp[MAX_CRYPTO_DATA_SIZE]; |
413 | theid = htonl(theid); | 413 | theid = htonl(theid); |
414 | memcpy(temp, &theid, sizeof(theid)); | 414 | memcpy(temp, &theid, sizeof(theid)); |
415 | memcpy(temp + sizeof(theid), message, length); | 415 | memcpy(temp + sizeof(theid), message, length); |
@@ -440,10 +440,10 @@ uint32_t m_sendaction(Messenger *m, int32_t friendnumber, uint8_t *action, uint3 | |||
440 | 440 | ||
441 | uint32_t m_sendaction_withid(Messenger *m, int32_t friendnumber, uint32_t theid, uint8_t *action, uint32_t length) | 441 | uint32_t m_sendaction_withid(Messenger *m, int32_t friendnumber, uint32_t theid, uint8_t *action, uint32_t length) |
442 | { | 442 | { |
443 | if (length >= (MAX_DATA_SIZE - sizeof(theid))) | 443 | if (length >= (MAX_CRYPTO_DATA_SIZE - sizeof(theid))) |
444 | return 0; | 444 | return 0; |
445 | 445 | ||
446 | uint8_t temp[MAX_DATA_SIZE]; | 446 | uint8_t temp[MAX_CRYPTO_DATA_SIZE]; |
447 | theid = htonl(theid); | 447 | theid = htonl(theid); |
448 | memcpy(temp, &theid, sizeof(theid)); | 448 | memcpy(temp, &theid, sizeof(theid)); |
449 | memcpy(temp + sizeof(theid), action, length); | 449 | memcpy(temp + sizeof(theid), action, length); |
@@ -840,7 +840,7 @@ int write_cryptpacket_id(Messenger *m, int32_t friendnumber, uint8_t packet_id, | |||
840 | if (friend_not_valid(m, friendnumber)) | 840 | if (friend_not_valid(m, friendnumber)) |
841 | return 0; | 841 | return 0; |
842 | 842 | ||
843 | if (length >= MAX_DATA_SIZE || m->friendlist[friendnumber].status != FRIEND_ONLINE) | 843 | if (length >= MAX_CRYPTO_DATA_SIZE || m->friendlist[friendnumber].status != FRIEND_ONLINE) |
844 | return 0; | 844 | return 0; |
845 | 845 | ||
846 | uint8_t packet[length + 1]; | 846 | uint8_t packet[length + 1]; |
@@ -888,7 +888,7 @@ static IP_Port get_friend_ipport(Messenger *m, int32_t friendnumber) | |||
888 | if (is_cryptoconnected(m->net_crypto, crypt_id) != CRYPTO_CONN_ESTABLISHED) | 888 | if (is_cryptoconnected(m->net_crypto, crypt_id) != CRYPTO_CONN_ESTABLISHED) |
889 | return zero; | 889 | return zero; |
890 | 890 | ||
891 | return connection_ip(m->net_crypto->lossless_udp, m->net_crypto->crypto_connections[crypt_id].number); | 891 | return m->net_crypto->crypto_connections[crypt_id].ip_port; |
892 | } | 892 | } |
893 | 893 | ||
894 | /* returns the group number of the chat with public key group_public_key. | 894 | /* returns the group number of the chat with public key group_public_key. |
@@ -1395,7 +1395,7 @@ int new_filesender(Messenger *m, int32_t friendnumber, uint64_t filesize, uint8_ | |||
1395 | int file_control(Messenger *m, int32_t friendnumber, uint8_t send_receive, uint8_t filenumber, uint8_t message_id, | 1395 | int file_control(Messenger *m, int32_t friendnumber, uint8_t send_receive, uint8_t filenumber, uint8_t message_id, |
1396 | uint8_t *data, uint16_t length) | 1396 | uint8_t *data, uint16_t length) |
1397 | { | 1397 | { |
1398 | if (length > MAX_DATA_SIZE - 3) | 1398 | if (length > MAX_CRYPTO_DATA_SIZE - 3) |
1399 | return -1; | 1399 | return -1; |
1400 | 1400 | ||
1401 | if (friend_not_valid(m, friendnumber)) | 1401 | if (friend_not_valid(m, friendnumber)) |
@@ -1412,7 +1412,7 @@ int file_control(Messenger *m, int32_t friendnumber, uint8_t send_receive, uint8 | |||
1412 | if (send_receive > 1) | 1412 | if (send_receive > 1) |
1413 | return -1; | 1413 | return -1; |
1414 | 1414 | ||
1415 | uint8_t packet[MAX_DATA_SIZE]; | 1415 | uint8_t packet[MAX_CRYPTO_DATA_SIZE]; |
1416 | packet[0] = send_receive; | 1416 | packet[0] = send_receive; |
1417 | packet[1] = filenumber; | 1417 | packet[1] = filenumber; |
1418 | packet[2] = message_id; | 1418 | packet[2] = message_id; |
@@ -1482,7 +1482,7 @@ int file_control(Messenger *m, int32_t friendnumber, uint8_t send_receive, uint8 | |||
1482 | */ | 1482 | */ |
1483 | int file_data(Messenger *m, int32_t friendnumber, uint8_t filenumber, uint8_t *data, uint16_t length) | 1483 | int file_data(Messenger *m, int32_t friendnumber, uint8_t filenumber, uint8_t *data, uint16_t length) |
1484 | { | 1484 | { |
1485 | if (length > MAX_DATA_SIZE - 1) | 1485 | if (length > MAX_CRYPTO_DATA_SIZE - 1) |
1486 | return -1; | 1486 | return -1; |
1487 | 1487 | ||
1488 | if (friend_not_valid(m, friendnumber)) | 1488 | if (friend_not_valid(m, friendnumber)) |
@@ -1495,7 +1495,7 @@ int file_data(Messenger *m, int32_t friendnumber, uint8_t filenumber, uint8_t *d | |||
1495 | if (crypto_num_free_sendqueue_slots(m->net_crypto, m->friendlist[friendnumber].crypt_connection_id) < MIN_SLOTS_FREE) | 1495 | if (crypto_num_free_sendqueue_slots(m->net_crypto, m->friendlist[friendnumber].crypt_connection_id) < MIN_SLOTS_FREE) |
1496 | return -1; | 1496 | return -1; |
1497 | 1497 | ||
1498 | uint8_t packet[MAX_DATA_SIZE]; | 1498 | uint8_t packet[MAX_CRYPTO_DATA_SIZE]; |
1499 | packet[0] = filenumber; | 1499 | packet[0] = filenumber; |
1500 | memcpy(packet + 1, data, length); | 1500 | memcpy(packet + 1, data, length); |
1501 | 1501 | ||
@@ -1738,6 +1738,23 @@ static void LANdiscovery(Messenger *m) | |||
1738 | } | 1738 | } |
1739 | } | 1739 | } |
1740 | 1740 | ||
1741 | int handle_new_connections(void *object, New_Connection *n_c) | ||
1742 | { | ||
1743 | Messenger *m = object; | ||
1744 | int friend_id = getfriend_id(m, n_c->public_key); | ||
1745 | |||
1746 | if (friend_id != -1) { | ||
1747 | if (m->friendlist[friend_id].crypt_connection_id != -1) | ||
1748 | return -1; | ||
1749 | |||
1750 | m->friendlist[friend_id].crypt_connection_id = accept_crypto_connection(m->net_crypto, n_c); | ||
1751 | set_friend_status(m, friend_id, FRIEND_CONFIRMED); | ||
1752 | } | ||
1753 | |||
1754 | return -1; | ||
1755 | } | ||
1756 | |||
1757 | |||
1741 | /* Run this at startup. */ | 1758 | /* Run this at startup. */ |
1742 | Messenger *new_messenger(uint8_t ipv6enabled) | 1759 | Messenger *new_messenger(uint8_t ipv6enabled) |
1743 | { | 1760 | { |
@@ -1772,6 +1789,8 @@ Messenger *new_messenger(uint8_t ipv6enabled) | |||
1772 | return NULL; | 1789 | return NULL; |
1773 | } | 1790 | } |
1774 | 1791 | ||
1792 | new_connection_handler(m->net_crypto, &handle_new_connections, m); | ||
1793 | |||
1775 | m->onion = new_onion(m->dht); | 1794 | m->onion = new_onion(m->dht); |
1776 | m->onion_a = new_onion_announce(m->dht); | 1795 | m->onion_a = new_onion_announce(m->dht); |
1777 | m->onion_c = new_onion_client(m->net_crypto); | 1796 | m->onion_c = new_onion_client(m->net_crypto); |
@@ -1843,7 +1862,7 @@ void do_friends(Messenger *m) | |||
1843 | { | 1862 | { |
1844 | uint32_t i; | 1863 | uint32_t i; |
1845 | int len; | 1864 | int len; |
1846 | uint8_t temp[MAX_DATA_SIZE]; | 1865 | uint8_t temp[MAX_CRYPTO_DATA_SIZE]; |
1847 | uint64_t temp_time = unix_time(); | 1866 | uint64_t temp_time = unix_time(); |
1848 | 1867 | ||
1849 | for (i = 0; i < m->numfriends; ++i) { | 1868 | for (i = 0; i < m->numfriends; ++i) { |
@@ -2184,32 +2203,8 @@ void do_friends(Messenger *m) | |||
2184 | } | 2203 | } |
2185 | } | 2204 | } |
2186 | 2205 | ||
2187 | void do_inbound(Messenger *m) | ||
2188 | { | ||
2189 | uint8_t secret_nonce[crypto_box_NONCEBYTES]; | ||
2190 | uint8_t public_key[crypto_box_PUBLICKEYBYTES]; | ||
2191 | uint8_t session_key[crypto_box_PUBLICKEYBYTES]; | ||
2192 | int inconnection = crypto_inbound(m->net_crypto, public_key, secret_nonce, session_key); | ||
2193 | |||
2194 | if (inconnection != -1) { | ||
2195 | int friend_id = getfriend_id(m, public_key); | ||
2196 | |||
2197 | if (friend_id != -1) { | ||
2198 | if (m_get_friend_connectionstatus(m, friend_id) == 1) { | ||
2199 | kill_connection(m->net_crypto->lossless_udp, inconnection); | ||
2200 | return; | ||
2201 | } | ||
2202 | 2206 | ||
2203 | crypto_kill(m->net_crypto, m->friendlist[friend_id].crypt_connection_id); | ||
2204 | m->friendlist[friend_id].crypt_connection_id = | ||
2205 | accept_crypto_inbound(m->net_crypto, inconnection, public_key, secret_nonce, session_key); | ||
2206 | 2207 | ||
2207 | set_friend_status(m, friend_id, FRIEND_CONFIRMED); | ||
2208 | } else { | ||
2209 | kill_connection(m->net_crypto->lossless_udp, inconnection); | ||
2210 | } | ||
2211 | } | ||
2212 | } | ||
2213 | 2208 | ||
2214 | #ifdef LOGGING | 2209 | #ifdef LOGGING |
2215 | #define DUMPING_CLIENTS_FRIENDS_EVERY_N_SECONDS 60UL | 2210 | #define DUMPING_CLIENTS_FRIENDS_EVERY_N_SECONDS 60UL |
@@ -2238,7 +2233,6 @@ void do_messenger(Messenger *m) | |||
2238 | do_net_crypto(m->net_crypto); | 2233 | do_net_crypto(m->net_crypto); |
2239 | do_onion_client(m->onion_c); | 2234 | do_onion_client(m->onion_c); |
2240 | do_friends(m); | 2235 | do_friends(m); |
2241 | do_inbound(m); | ||
2242 | do_allgroupchats(m); | 2236 | do_allgroupchats(m); |
2243 | LANdiscovery(m); | 2237 | LANdiscovery(m); |
2244 | 2238 | ||
@@ -2381,7 +2375,9 @@ size_t wait_data_size() | |||
2381 | 2375 | ||
2382 | int wait_prepare_messenger(Messenger *m, uint8_t *data) | 2376 | int wait_prepare_messenger(Messenger *m, uint8_t *data) |
2383 | { | 2377 | { |
2384 | return networking_wait_prepare(m->net, sendqueue_total(m->net_crypto->lossless_udp), data); | 2378 | //TODO |
2379 | //return networking_wait_prepare(m->net, sendqueue_total(m->net_crypto->lossless_udp), data); | ||
2380 | return networking_wait_prepare(m->net, 1024, data); | ||
2385 | } | 2381 | } |
2386 | 2382 | ||
2387 | int wait_execute_messenger(uint8_t *data, long seconds, long microseconds) | 2383 | int wait_execute_messenger(uint8_t *data, long seconds, long microseconds) |