diff --git a/testing/nTox.c b/testing/nTox.c index fb4efce3..7df35386 100644 --- a/testing/nTox.c +++ b/testing/nTox.c @@ -1257,8 +1257,6 @@ int main(int argc, char *argv[]) time_t timestamp0 = time(NULL); uint8_t pollok = 0; - uint16_t len = tox_wait_data_size(); - uint8_t data[len]; while (1) { if (on == 0) { diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index d9895e83..38cf2040 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c @@ -2427,31 +2427,6 @@ void do_messenger(Messenger *m) #endif /* LOGGING */ } -/* - * functions to avoid excessive polling - */ -size_t wait_data_size() -{ - return networking_wait_data_size(); -} - -int wait_prepare_messenger(Messenger *m, uint8_t *data) -{ - //TODO - //return networking_wait_prepare(m->net, sendqueue_total(m->net_crypto->lossless_udp), data); - return networking_wait_prepare(m->net, 1024, data); -} - -int wait_execute_messenger(uint8_t *data, long seconds, long microseconds) -{ - return networking_wait_execute(data, seconds, microseconds); -} - -int wait_cleanup_messenger(Messenger *m, uint8_t *data) -{ - return networking_wait_cleanup(m->net, data); -} - /* new messenger format for load/save, more robust and forward compatible */ #define MESSENGER_STATE_COOKIE_GLOBAL_OLD 0x15ed1b1e diff --git a/toxcore/Messenger.h b/toxcore/Messenger.h index 94dbccef..222bf3e4 100644 --- a/toxcore/Messenger.h +++ b/toxcore/Messenger.h @@ -727,14 +727,6 @@ void do_messenger(Messenger *m); */ uint32_t messenger_run_interval(Messenger *m); -/* - * functions to avoid excessive polling - */ -size_t wait_data_size(); -int wait_prepare_messenger(Messenger *m, uint8_t *data); -int wait_execute_messenger(uint8_t *data, long seconds, long microseconds); -int wait_cleanup_messenger(Messenger *m, uint8_t *data); - /* SAVING AND LOADING FUNCTIONS: */ /* return size of the messenger data (for saving). */ diff --git a/toxcore/assoc.c b/toxcore/assoc.c index 44128688..736f246e 100644 --- a/toxcore/assoc.c +++ b/toxcore/assoc.c @@ -1005,8 +1005,8 @@ void Assoc_status(Assoc *assoc) entry->used_at ? (int)(unix_time() - entry->used_at) : 0, entry->seen_at ? (int)(unix_time() - entry->seen_at) : 0, entry->seen_at ? (entry->seen_family == AF_INET ? '4' : (entry->seen_family == AF_INET6 ? '6' : '?')) : '?', - entry->heard_at ? (int)(unix_time() - entry->heard_at) : 0, - entry->heard_at ? (entry->heard_family == AF_INET ? '4' : (entry->heard_family == AF_INET6 ? '6' : '?')) : '?'); + entry->heard_at ? (int)(unix_time() - entry->heard_at) : 0, + entry->heard_at ? (entry->heard_family == AF_INET ? '4' : (entry->heard_family == AF_INET6 ? '6' : '?')) : '?'); } } } diff --git a/toxcore/network.c b/toxcore/network.c index bd5f8f56..b6bc8e18 100644 --- a/toxcore/network.c +++ b/toxcore/network.c @@ -424,133 +424,6 @@ void networking_poll(Networking_Core *net) } } -/* - * function to avoid excessive polling - */ -typedef struct { - sock_t sock; - uint32_t sendqueue_length; - uint16_t send_fail_reset; - uint64_t send_fail_eagain; -} select_info; - -size_t networking_wait_data_size() -{ - return sizeof(select_info); -} - -int networking_wait_prepare(Networking_Core *net, uint32_t sendqueue_length, uint8_t *data) -{ - if (data == NULL) { - return 0; - } - - select_info *s = (select_info *)data; - s->sock = net->sock; - s->sendqueue_length = sendqueue_length; - s->send_fail_reset = 0; - s->send_fail_eagain = net->send_fail_eagain; - - return 1; -} - -/* *** Function MUSTN'T poll. *** -* The function mustn't modify anything at all, so it can be called completely -* asynchronously without any worry. -*/ -int networking_wait_execute(uint8_t *data, long seconds, long microseconds) -{ - /* WIN32: supported since Win2K, but might need some adjustements */ - /* UNIX: this should work for any remotely Unix'ish system */ - - if (data == NULL) { - return 0; - } - - select_info *s = (select_info *)data; - - /* add only if we had a failed write */ - int writefds_add = 0; - - /* if send_fail_eagain is set, that means that socket's buffer was full and couldn't fit data we tried to send, - * so this is the only case when we need to know when the socket becomes write-ready, i.e. socket's buffer gets - * some free space for us to put data to be sent in, but select will tell us that the socket is writable even - * if we can fit a small part of our data (say 1 byte), so we wait some time, in hope that large enough chunk - * of socket's buffer will be available (at least that's how I understand intentions of the previous author of - * that code) - */ - if (s->send_fail_eagain != 0) { - // current_time(): milliseconds - uint64_t now = current_time_monotonic(); - - /* s->sendqueue_length: might be used to guess how long we keep checking */ - /* for now, threshold is hardcoded to 250ms, too long for a really really - * fast link, but too short for a sloooooow link... */ - if (now - s->send_fail_eagain < 250) { - writefds_add = 1; - } - } - - int nfds = 1 + s->sock; - - /* the FD_ZERO calls might be superfluous */ - fd_set readfds; - FD_ZERO(&readfds); - FD_SET(s->sock, &readfds); - - fd_set writefds; - FD_ZERO(&writefds); - - if (writefds_add) { - FD_SET(s->sock, &writefds); - } - - fd_set exceptfds; - FD_ZERO(&exceptfds); - FD_SET(s->sock, &exceptfds); - - struct timeval timeout; - struct timeval *timeout_ptr = &timeout; - - if (seconds < 0 || microseconds < 0) { - timeout_ptr = NULL; - } else { - timeout.tv_sec = seconds; - timeout.tv_usec = microseconds; - } - - /* returns -1 on error, 0 on timeout, the socket on activity */ - int res = select(nfds, &readfds, &writefds, &exceptfds, timeout_ptr); - - LOGGER_SCOPE( - - if (res) LOGGER_INFO("select(%d, %d): %d (%d, %s) - %d %d %d\n", microseconds, seconds, res, errno, - strerror(errno), FD_ISSET(s->sock, &readfds), FD_ISSET(s->sock, &writefds), - FD_ISSET(s->sock, &exceptfds)); - ); - - if (FD_ISSET(s->sock, &writefds)) { - s->send_fail_reset = 1; - } - - return res > 0 ? 2 : 1; -} - -int networking_wait_cleanup(Networking_Core *net, uint8_t *data) -{ - if (data == NULL) { - return 0; - } - - select_info *s = (select_info *)data; - - if (s->send_fail_reset) { - net->send_fail_eagain = 0; - } - - return 1; -} - #ifndef VANILLA_NACL /* Used for sodium_init() */ #include diff --git a/toxcore/network.h b/toxcore/network.h index 633f8010..9058a341 100644 --- a/toxcore/network.h +++ b/toxcore/network.h @@ -350,14 +350,6 @@ void networking_registerhandler(Networking_Core *net, uint8_t byte, packet_handl /* Call this several times a second. */ void networking_poll(Networking_Core *net); -/* - * functions to avoid excessive polling - */ -size_t networking_wait_data_size(); -int networking_wait_prepare(Networking_Core *net, uint32_t sendqueue_length, uint8_t *data); -int networking_wait_execute(uint8_t *data, long seconds, long microseconds); -int networking_wait_cleanup(Networking_Core *net, uint8_t *data); - /* Initialize networking. * bind to ip and port. * ip must be in network order EX: 127.0.0.1 = (7F000001). diff --git a/toxcore/onion_announce.c b/toxcore/onion_announce.c index e6489a67..a9891b9c 100644 --- a/toxcore/onion_announce.c +++ b/toxcore/onion_announce.c @@ -52,7 +52,8 @@ int send_announce_request(Networking_Core *net, Onion_Path *path, Node_format dest, uint8_t *public_key, uint8_t *secret_key, uint8_t *ping_id, uint8_t *client_id, uint8_t *data_public_key, uint64_t sendback_data) { - uint8_t plain[ONION_PING_ID_SIZE + crypto_box_PUBLICKEYBYTES + crypto_box_PUBLICKEYBYTES + ONION_ANNOUNCE_SENDBACK_DATA_LENGTH]; + uint8_t plain[ONION_PING_ID_SIZE + crypto_box_PUBLICKEYBYTES + crypto_box_PUBLICKEYBYTES + + ONION_ANNOUNCE_SENDBACK_DATA_LENGTH]; memcpy(plain, ping_id, ONION_PING_ID_SIZE); memcpy(plain + ONION_PING_ID_SIZE, client_id, crypto_box_PUBLICKEYBYTES); memcpy(plain + ONION_PING_ID_SIZE + crypto_box_PUBLICKEYBYTES, data_public_key, crypto_box_PUBLICKEYBYTES); @@ -223,7 +224,8 @@ static int handle_announce_request(void *object, IP_Port source, uint8_t *packet uint8_t shared_key[crypto_box_BEFORENMBYTES]; get_shared_key(&onion_a->shared_keys_recv, shared_key, onion_a->dht->self_secret_key, packet_public_key); - uint8_t plain[ONION_PING_ID_SIZE + crypto_box_PUBLICKEYBYTES + crypto_box_PUBLICKEYBYTES + ONION_ANNOUNCE_SENDBACK_DATA_LENGTH]; + uint8_t plain[ONION_PING_ID_SIZE + crypto_box_PUBLICKEYBYTES + crypto_box_PUBLICKEYBYTES + + ONION_ANNOUNCE_SENDBACK_DATA_LENGTH]; int len = decrypt_data_symmetric(shared_key, packet + 1, packet + 1 + crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES, ONION_PING_ID_SIZE + crypto_box_PUBLICKEYBYTES + crypto_box_PUBLICKEYBYTES + ONION_ANNOUNCE_SENDBACK_DATA_LENGTH + crypto_box_MACBYTES, plain); diff --git a/toxcore/tox.c b/toxcore/tox.c index b8c15a39..12bee911 100644 --- a/toxcore/tox.c +++ b/toxcore/tox.c @@ -820,32 +820,6 @@ void tox_do(Tox *tox) do_messenger(m); } -/* - * functions to avoid excessive polling - */ - -size_t tox_wait_data_size() -{ - return wait_data_size(); -} - -int tox_wait_prepare(Tox *tox, uint8_t *data) -{ - Messenger *m = tox; - return wait_prepare_messenger(m, data); -} - -int tox_wait_execute(uint8_t *data, long seconds, long microseconds) -{ - return wait_execute_messenger(data, seconds, microseconds); -} - -int tox_wait_cleanup(Tox *tox, uint8_t *data) -{ - Messenger *m = tox; - return wait_cleanup_messenger(m, data); -} - /* SAVING AND LOADING FUNCTIONS: */ /* return size of the messenger data (for saving). */ diff --git a/toxcore/tox.h b/toxcore/tox.h index 151f5715..52976c17 100644 --- a/toxcore/tox.h +++ b/toxcore/tox.h @@ -26,50 +26,20 @@ #include -#if defined(_WIN32) || defined(__WIN32__) || defined (WIN32) -#ifndef WINVER -//Windows XP -#define WINVER 0x0501 -#endif - -#include -#include -#include - - -#ifndef true -#define true 1 -#endif -#ifndef false -#define false 0 -#endif -#else - -#include -#ifdef __OpenBSD__ -#include -#endif -#include -#include - -#endif #ifdef __cplusplus extern "C" { #endif #define TOX_MAX_NAME_LENGTH 128 -/* Maximum message and action length that can be sent. */ -#define TOX_MAX_MESSAGE_LENGTH 1003 + +/* Maximum length of single messages after which they should be split. */ +#define TOX_MAX_MESSAGE_LENGTH 1368 #define TOX_MAX_STATUSMESSAGE_LENGTH 1007 #define TOX_CLIENT_ID_SIZE 32 #define TOX_FRIEND_ADDRESS_SIZE (TOX_CLIENT_ID_SIZE + sizeof(uint32_t) + sizeof(uint16_t)) -#define TOX_PORTRANGE_FROM 33445 -#define TOX_PORTRANGE_TO 33545 -#define TOX_PORT_DEFAULT TOX_PORTRANGE_FROM - #define TOX_ENABLE_IPV6_DEFAULT 1 /* Errors for m_addfriend @@ -177,6 +147,10 @@ int tox_friend_exists(Tox *tox, int32_t friendnumber); * return the message id if packet was successfully put into the send queue. * return 0 if it was not. * + * maximum length of messages is TOX_MAX_MESSAGE_LENGTH, your client must split larger messages + * or else sending them will not work. No the core will not split messages for you because that + * requires me to parse UTF-8. + * * You will want to retain the return value, it will be passed to your read_receipt callback * if one is received. * m_sendmessage_withid will send a message with the id of your choosing, @@ -190,6 +164,10 @@ uint32_t tox_send_message_withid(Tox *tox, int32_t friendnumber, uint32_t theid, * return the message id if packet was successfully put into the send queue. * return 0 if it was not. * + * maximum length of actions is TOX_MAX_MESSAGE_LENGTH, your client must split larger actions + * or else sending them will not work. No the core will not split actions for you because that + * requires me to parse UTF-8. + * * You will want to retain the return value, it will be passed to your read_receipt callback * if one is received. * m_sendaction_withid will send an action message with the id of your choosing, @@ -235,6 +213,7 @@ int tox_get_self_name_size(Tox *tox); /* Set our user status. * * userstatus must be one of TOX_USERSTATUS values. + * max length of the status is TOX_MAX_STATUSMESSAGE_LENGTH. * * returns 0 on success. * returns -1 on failure. diff --git a/toxcore/util.c b/toxcore/util.c index f0bb4130..46b33400 100644 --- a/toxcore/util.c +++ b/toxcore/util.c @@ -138,56 +138,56 @@ int load_state(load_state_callback_func load_state_callback, void *outer, inline__ void bytes_to_U32(uint32_t *dest, const uint8_t *bytes) { *dest = - #ifdef WORDS_BIGENDIAN - ( ( uint32_t ) * bytes ) | - ( ( uint32_t ) * ( bytes + 1 ) << 8 ) | - ( ( uint32_t ) * ( bytes + 2 ) << 16 ) | - ( ( uint32_t ) * ( bytes + 3 ) << 24 ) ; - #else - ( ( uint32_t ) * bytes << 24 ) | - ( ( uint32_t ) * ( bytes + 1 ) << 16 ) | - ( ( uint32_t ) * ( bytes + 2 ) << 8 ) | - ( ( uint32_t ) * ( bytes + 3 ) ) ; - #endif +#ifdef WORDS_BIGENDIAN + ( ( uint32_t ) * bytes ) | + ( ( uint32_t ) * ( bytes + 1 ) << 8 ) | + ( ( uint32_t ) * ( bytes + 2 ) << 16 ) | + ( ( uint32_t ) * ( bytes + 3 ) << 24 ) ; +#else + ( ( uint32_t ) * bytes << 24 ) | + ( ( uint32_t ) * ( bytes + 1 ) << 16 ) | + ( ( uint32_t ) * ( bytes + 2 ) << 8 ) | + ( ( uint32_t ) * ( bytes + 3 ) ) ; +#endif } /* Converts 2 bytes to uint16_t */ inline__ void bytes_to_U16(uint16_t *dest, const uint8_t *bytes) { *dest = - #ifdef WORDS_BIGENDIAN - ( ( uint16_t ) * bytes ) | - ( ( uint16_t ) * ( bytes + 1 ) << 8 ); - #else - ( ( uint16_t ) * bytes << 8 ) | - ( ( uint16_t ) * ( bytes + 1 ) ); - #endif +#ifdef WORDS_BIGENDIAN + ( ( uint16_t ) * bytes ) | + ( ( uint16_t ) * ( bytes + 1 ) << 8 ); +#else + ( ( uint16_t ) * bytes << 8 ) | + ( ( uint16_t ) * ( bytes + 1 ) ); +#endif } /* Convert uint32_t to byte string of size 4 */ inline__ void U32_to_bytes(uint8_t *dest, uint32_t value) { - #ifdef WORDS_BIGENDIAN +#ifdef WORDS_BIGENDIAN *(dest) = ( value ); *(dest + 1) = ( value >> 8 ); *(dest + 2) = ( value >> 16 ); *(dest + 3) = ( value >> 24 ); - #else +#else *(dest) = ( value >> 24 ); *(dest + 1) = ( value >> 16 ); *(dest + 2) = ( value >> 8 ); *(dest + 3) = ( value ); - #endif +#endif } /* Convert uint16_t to byte string of size 2 */ inline__ void U16_to_bytes(uint8_t *dest, uint16_t value) { - #ifdef WORDS_BIGENDIAN +#ifdef WORDS_BIGENDIAN *(dest) = ( value ); *(dest + 1) = ( value >> 8 ); - #else +#else *(dest) = ( value >> 8 ); *(dest + 1) = ( value ); - #endif +#endif } \ No newline at end of file