diff --git a/other/DHT_bootstrap.c b/other/DHT_bootstrap.c index 4a5d937d..af5fae01 100644 --- a/other/DHT_bootstrap.c +++ b/other/DHT_bootstrap.c @@ -1,3 +1,4 @@ + /* DHT boostrap * * A simple DHT boostrap server for tox. @@ -20,6 +21,7 @@ * along with Tox. If not, see . * */ + #ifdef HAVE_CONFIG_H #include "config.h" #endif @@ -27,6 +29,8 @@ #include "../toxcore/DHT.h" #include "../toxcore/LAN_discovery.h" #include "../toxcore/friend_requests.h" +#include "../toxcore/util.h" + #include "../testing/misc_tools.c" /* Sleep function (x = milliseconds) */ @@ -147,7 +151,7 @@ int main(int argc, char *argv[]) do_DHT(dht); - if (last_LANdiscovery + (is_waiting_for_dht_connection ? 5 : LAN_DISCOVERY_INTERVAL) < unix_time()) { + if (is_timeout(last_LANdiscovery, is_waiting_for_dht_connection ? 5 : LAN_DISCOVERY_INTERVAL)) { send_LANdiscovery(htons(PORT), dht->c); last_LANdiscovery = unix_time(); } diff --git a/toxcore/DHT.c b/toxcore/DHT.c index 2f468632..5232deed 100644 --- a/toxcore/DHT.c +++ b/toxcore/DHT.c @@ -115,26 +115,15 @@ static int client_id_cmp(ClientPair p1, ClientPair p2) return c; } -static int id_equal(uint8_t *a, uint8_t *b) -{ - return memcmp(a, b, CLIENT_ID_SIZE) == 0; -} - -static int is_timeout(uint64_t time_now, uint64_t timestamp, uint64_t timeout) -{ - return timestamp + timeout <= time_now; -} - static int client_in_list(Client_data *list, uint32_t length, uint8_t *client_id) { uint32_t i; - uint64_t temp_time = unix_time(); for (i = 0; i < length; i++) /* Dead nodes are considered dead (not in the list)*/ - if (!is_timeout(temp_time, list[i].assoc4.timestamp, KILL_NODE_TIMEOUT) || - !is_timeout(temp_time, list[i].assoc6.timestamp, KILL_NODE_TIMEOUT)) + if (!is_timeout(list[i].assoc4.timestamp, KILL_NODE_TIMEOUT) || + !is_timeout(list[i].assoc6.timestamp, KILL_NODE_TIMEOUT)) if (id_equal(list[i].client_id, client_id)) return 1; @@ -273,7 +262,7 @@ static int friend_number(DHT *dht, uint8_t *client_id) */ static void get_close_nodes_inner(DHT *dht, uint8_t *client_id, Node_format *nodes_list, sa_family_t sa_family, Client_data *client_list, uint32_t client_list_length, - time_t timestamp, int *num_nodes_ptr, uint8_t is_LAN) + int *num_nodes_ptr, uint8_t is_LAN) { if ((sa_family != AF_INET) && (sa_family != AF_INET6)) return; @@ -297,7 +286,7 @@ static void get_close_nodes_inner(DHT *dht, uint8_t *client_id, Node_format *nod ipptp = &client->assoc6; /* node not in a good condition? */ - if (is_timeout(timestamp, ipptp->timestamp, BAD_NODE_TIMEOUT)) + if (is_timeout(ipptp->timestamp, BAD_NODE_TIMEOUT)) continue; IP *client_ip = &ipptp->ip_port.ip; @@ -369,15 +358,14 @@ static void get_close_nodes_inner(DHT *dht, uint8_t *client_id, Node_format *nod */ static int get_close_nodes(DHT *dht, uint8_t *client_id, Node_format *nodes_list, sa_family_t sa_family, uint8_t is_LAN) { - time_t timestamp = unix_time(); int num_nodes = 0, i; get_close_nodes_inner(dht, client_id, nodes_list, sa_family, - dht->close_clientlist, LCLIENT_LIST, timestamp, &num_nodes, is_LAN); + dht->close_clientlist, LCLIENT_LIST, &num_nodes, is_LAN); for (i = 0; i < dht->num_friends; ++i) get_close_nodes_inner(dht, client_id, nodes_list, sa_family, dht->friends_list[i].client_list, MAX_FRIEND_CLIENTS, - timestamp, &num_nodes, is_LAN); + &num_nodes, is_LAN); return num_nodes; } @@ -396,7 +384,6 @@ static int replace_bad( Client_data *list, return 1; uint32_t i; - uint64_t temp_time = unix_time(); for (i = 0; i < length; ++i) { /* If node is bad */ @@ -408,10 +395,10 @@ static int replace_bad( Client_data *list, else ipptp = &client->assoc6; - if (is_timeout(temp_time, ipptp->timestamp, BAD_NODE_TIMEOUT)) { + if (is_timeout(ipptp->timestamp, BAD_NODE_TIMEOUT)) { memcpy(client->client_id, client_id, CLIENT_ID_SIZE); ipptp->ip_port = ip_port; - ipptp->timestamp = temp_time; + ipptp->timestamp = unix_time(); ip_reset(&ipptp->ret_ip_port.ip); ipptp->ret_ip_port.port = 0; @@ -943,7 +930,6 @@ static int handle_sendnodes_ipv6(void *object, IP_Port source, uint8_t *packet, */ static void get_bunchnodes(DHT *dht, Client_data *list, uint16_t length, uint16_t max_num, uint8_t *client_id) { - uint64_t temp_time = unix_time(); uint32_t i, num = 0; for (i = 0; i < length; ++i) { @@ -952,7 +938,7 @@ static void get_bunchnodes(DHT *dht, Client_data *list, uint16_t length, uint16_ for (a = 0, assoc = &list[i].assoc6; a < 2; a++, assoc = &list[i].assoc4) if (ipport_isset(&(assoc->ip_port)) && - !is_timeout(temp_time, assoc->ret_timestamp, BAD_NODE_TIMEOUT)) { + !is_timeout(assoc->ret_timestamp, BAD_NODE_TIMEOUT)) { getnodes(dht, assoc->ip_port, list[i].client_id, client_id, NULL); ++num; @@ -1022,7 +1008,6 @@ int DHT_delfriend(DHT *dht, uint8_t *client_id) int DHT_getfriendip(DHT *dht, uint8_t *client_id, IP_Port *ip_port) { uint32_t i, j; - uint64_t temp_time = unix_time(); ip_reset(&ip_port->ip); ip_port->port = 0; @@ -1038,7 +1023,7 @@ int DHT_getfriendip(DHT *dht, uint8_t *client_id, IP_Port *ip_port) uint32_t a; for (a = 0, assoc = &client->assoc6; a < 2; a++, assoc = &client->assoc4) - if (!is_timeout(temp_time, assoc->timestamp, BAD_NODE_TIMEOUT)) { + if (!is_timeout(assoc->timestamp, BAD_NODE_TIMEOUT)) { *ip_port = assoc->ip_port; return 1; } @@ -1069,14 +1054,14 @@ static void do_ping_and_sendnode_requests(DHT *dht, uint64_t *lastgetnode, uint8 uint32_t a; for (a = 0, assoc = &client->assoc6; a < 2; a++, assoc = &client->assoc4) - if (!is_timeout(temp_time, assoc->timestamp, KILL_NODE_TIMEOUT)) { - if (is_timeout(temp_time, assoc->last_pinged, PING_INTERVAL)) { + if (!is_timeout(assoc->timestamp, KILL_NODE_TIMEOUT)) { + if (is_timeout(assoc->last_pinged, PING_INTERVAL)) { send_ping_request(dht->ping, assoc->ip_port, client->client_id ); assoc->last_pinged = temp_time; } /* If node is good. */ - if (!is_timeout(temp_time, assoc->timestamp, BAD_NODE_TIMEOUT)) { + if (!is_timeout(assoc->timestamp, BAD_NODE_TIMEOUT)) { client_list[num_nodes] = client; assoc_list[num_nodes] = assoc; ++num_nodes; @@ -1084,8 +1069,7 @@ static void do_ping_and_sendnode_requests(DHT *dht, uint64_t *lastgetnode, uint8 } } - if ((num_nodes != 0) && - is_timeout(temp_time, *lastgetnode, GET_NODE_INTERVAL)) { + if ((num_nodes != 0) && is_timeout(*lastgetnode, GET_NODE_INTERVAL)) { uint32_t rand_node = rand() % num_nodes; getnodes(dht, assoc_list[rand_node]->ip_port, client_list[rand_node]->client_id, client_id, NULL); @@ -1180,9 +1164,6 @@ int route_packet(DHT *dht, uint8_t *client_id, uint8_t *packet, uint32_t length) */ static int friend_iplist(DHT *dht, IP_Port *ip_portlist, uint16_t friend_num) { - int i; - uint64_t temp_time = unix_time(); - if (friend_num >= dht->num_friends) return -1; @@ -1193,22 +1174,21 @@ static int friend_iplist(DHT *dht, IP_Port *ip_portlist, uint16_t friend_num) IP_Port ipv6s[MAX_FRIEND_CLIENTS]; int num_ipv6s = 0; uint8_t connected; + int i; for (i = 0; i < MAX_FRIEND_CLIENTS; ++i) { client = &(friend->client_list[i]); connected = 0; /* If ip is not zero and node is good. */ - if (ip_isset(&client->assoc4.ret_ip_port.ip) - && !is_timeout(temp_time, client->assoc4.ret_timestamp, BAD_NODE_TIMEOUT)) { + if (ip_isset(&client->assoc4.ret_ip_port.ip) && !is_timeout(client->assoc4.ret_timestamp, BAD_NODE_TIMEOUT)) { ipv4s[num_ipv4s] = client->assoc4.ret_ip_port; ++num_ipv4s; connected = 1; } - if (ip_isset(&client->assoc6.ret_ip_port.ip) - && !is_timeout(temp_time, client->assoc6.ret_timestamp, BAD_NODE_TIMEOUT)) { + if (ip_isset(&client->assoc6.ret_ip_port.ip) && !is_timeout(client->assoc6.ret_timestamp, BAD_NODE_TIMEOUT)) { ipv6s[num_ipv6s] = client->assoc6.ret_ip_port; ++num_ipv6s; @@ -1271,7 +1251,6 @@ int route_tofriend(DHT *dht, uint8_t *friend_id, uint8_t *packet, uint32_t lengt if (ip_num < (MAX_FRIEND_CLIENTS / 2)) return 0; /* Reason for that? */ - uint64_t temp_time = unix_time(); DHT_Friend *friend = &dht->friends_list[num]; Client_data *client; @@ -1294,7 +1273,7 @@ int route_tofriend(DHT *dht, uint8_t *friend_id, uint8_t *packet, uint32_t lengt /* If ip is not zero and node is good. */ if (ip_isset(&assoc->ret_ip_port.ip) && - !is_timeout(temp_time, assoc->ret_timestamp, BAD_NODE_TIMEOUT)) { + !is_timeout(assoc->ret_timestamp, BAD_NODE_TIMEOUT)) { int retval = sendpacket(dht->c->lossless_udp->net, assoc->ip_port, packet, length); if ((unsigned int)retval == length) { @@ -1324,7 +1303,6 @@ static int routeone_tofriend(DHT *dht, uint8_t *friend_id, uint8_t *packet, uint IP_Port ip_list[MAX_FRIEND_CLIENTS * 2]; int n = 0; uint32_t i; - uint64_t temp_time = unix_time(); /* extra legwork, because having the outside allocating the space for us * is *usually* good(tm) (bites us in the behind in this case though) */ @@ -1341,7 +1319,7 @@ static int routeone_tofriend(DHT *dht, uint8_t *friend_id, uint8_t *packet, uint assoc = &client->assoc6; /* If ip is not zero and node is good. */ - if (ip_isset(&assoc->ret_ip_port.ip) && !is_timeout(temp_time, assoc->ret_timestamp, BAD_NODE_TIMEOUT)) { + if (ip_isset(&assoc->ret_ip_port.ip) && !is_timeout(assoc->ret_timestamp, BAD_NODE_TIMEOUT)) { ip_list[n] = assoc->ip_port; ++n; } @@ -1665,6 +1643,9 @@ static int handle_hardening(void *object, IP_Port source, uint8_t *source_pubkey DHT *new_DHT(Net_Crypto *c) { + /* init time */ + unix_time_update(); + if (c == NULL) return NULL; @@ -1694,6 +1675,8 @@ DHT *new_DHT(Net_Crypto *c) void do_DHT(DHT *dht) { + unix_time_update(); + do_Close(dht); do_DHT_friends(dht); do_NAT(dht); @@ -1711,8 +1694,6 @@ void kill_DHT(DHT *dht) #define DHT_STATE_COOKIE_GLOBAL 0x159000d #define DHT_STATE_COOKIE_TYPE 0x11ce -#define DHT_STATE_TYPE_FRIENDS 1 -#define DHT_STATE_TYPE_CLIENTS 2 #define DHT_STATE_TYPE_FRIENDS_ASSOC46 3 #define DHT_STATE_TYPE_CLIENTS_ASSOC46 4 @@ -1784,43 +1765,6 @@ static int dht_load_state_callback(void *outer, uint8_t *data, uint32_t length, uint32_t num, i, j; switch (type) { - case DHT_STATE_TYPE_FRIENDS: - if (length % sizeof(DHT_Friend_old) != 0) - break; - - { /* localize declarations */ - DHT_Friend_old *friend_list = (DHT_Friend_old *)data; - num = length / sizeof(DHT_Friend_old); - - for (i = 0; i < num; ++i) { - DHT_addfriend(dht, friend_list[i].client_id); - - for (j = 0; j < MAX_FRIEND_CLIENTS; ++j) { - Client_data_old *client = &friend_list[i].client_list[j]; - - if (client->assoc.timestamp != 0) - getnodes(dht, client->assoc.ip_port, client->client_id, friend_list[i].client_id, NULL); - } - } - } /* localize declarations */ - - break; - - case DHT_STATE_TYPE_CLIENTS: - if ((length % sizeof(Client_data_old)) != 0) - break; - - { /* localize declarations */ - num = length / sizeof(Client_data_old); - Client_data_old *client_list = (Client_data_old *)data; - - for (i = 0; i < num; ++i) - if (client_list[i].assoc.timestamp != 0) - DHT_bootstrap(dht, client_list[i].assoc.ip_port, client_list[i].client_id); - } /* localize declarations */ - - break; - case DHT_STATE_TYPE_FRIENDS_ASSOC46: if (length % sizeof(DHT_Friend) != 0) break; @@ -1894,7 +1838,7 @@ int DHT_load_new(DHT *dht, uint8_t *data, uint32_t length) length - cookie_len, DHT_STATE_COOKIE_TYPE); } - return DHT_load_old(dht, data, length); + return -1; } /* return 0 if we are not connected to the DHT. * return 1 if we are. @@ -1902,13 +1846,13 @@ int DHT_load_new(DHT *dht, uint8_t *data, uint32_t length) int DHT_isconnected(DHT *dht) { uint32_t i; - uint64_t temp_time = unix_time(); + unix_time_update(); for (i = 0; i < LCLIENT_LIST; ++i) { Client_data *client = &dht->close_clientlist[i]; - if (!is_timeout(temp_time, client->assoc4.timestamp, BAD_NODE_TIMEOUT) || - !is_timeout(temp_time, client->assoc6.timestamp, BAD_NODE_TIMEOUT)) + if (!is_timeout(client->assoc4.timestamp, BAD_NODE_TIMEOUT) || + !is_timeout(client->assoc6.timestamp, BAD_NODE_TIMEOUT)) return 1; } diff --git a/toxcore/LAN_discovery.c b/toxcore/LAN_discovery.c index a4f1ccc2..eb0b95a1 100644 --- a/toxcore/LAN_discovery.c +++ b/toxcore/LAN_discovery.c @@ -26,6 +26,7 @@ #endif #include "LAN_discovery.h" +#include "util.h" #define MAX_INTERFACES 16 @@ -214,7 +215,7 @@ int send_LANdiscovery(uint16_t port, Net_Crypto *c) { uint8_t data[crypto_box_PUBLICKEYBYTES + 1]; data[0] = NET_PACKET_LAN_DISCOVERY; - memcpy(data + 1, c->self_public_key, crypto_box_PUBLICKEYBYTES); + id_copy(data + 1, c->self_public_key); #ifdef __linux send_broadcasts(c->lossless_udp->net, port, data, 1 + crypto_box_PUBLICKEYBYTES); diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index ff64a072..be3d82e0 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c @@ -92,7 +92,7 @@ int getfriend_id(Messenger *m, uint8_t *client_id) for (i = 0; i < m->numfriends; ++i) { if (m->friendlist[i].status > 0) - if (memcmp(client_id, m->friendlist[i].client_id, crypto_box_PUBLICKEYBYTES) == 0) + if (id_equal(client_id, m->friendlist[i].client_id)) return i; } @@ -140,7 +140,7 @@ static uint16_t address_checksum(uint8_t *address, uint32_t len) */ void getaddress(Messenger *m, uint8_t *address) { - memcpy(address, m->net_crypto->self_public_key, crypto_box_PUBLICKEYBYTES); + id_copy(address, m->net_crypto->self_public_key); uint32_t nospam = get_nospam(&(m->fr)); memcpy(address + crypto_box_PUBLICKEYBYTES, &nospam, sizeof(nospam)); uint16_t checksum = address_checksum(address, FRIEND_ADDRESS_SIZE - sizeof(checksum)); @@ -173,7 +173,7 @@ int m_addfriend(Messenger *m, uint8_t *address, uint8_t *data, uint16_t length) return FAERR_TOOLONG; uint8_t client_id[crypto_box_PUBLICKEYBYTES]; - memcpy(client_id, address, crypto_box_PUBLICKEYBYTES); + id_copy(client_id, address); uint16_t check, checksum = address_checksum(address, FRIEND_ADDRESS_SIZE - sizeof(checksum)); memcpy(&check, address + crypto_box_PUBLICKEYBYTES + sizeof(uint32_t), sizeof(check)); @@ -183,7 +183,7 @@ int m_addfriend(Messenger *m, uint8_t *address, uint8_t *data, uint16_t length) if (length < 1) return FAERR_NOMESSAGE; - if (memcmp(client_id, m->net_crypto->self_public_key, crypto_box_PUBLICKEYBYTES) == 0) + if (id_equal(client_id, m->net_crypto->self_public_key)) return FAERR_OWNKEY; int friend_id = getfriend_id(m, client_id); @@ -214,7 +214,7 @@ int m_addfriend(Messenger *m, uint8_t *address, uint8_t *data, uint16_t length) m->friendlist[i].crypt_connection_id = -1; m->friendlist[i].friendrequest_lastsent = 0; m->friendlist[i].friendrequest_timeout = FRIENDREQUEST_TIMEOUT; - memcpy(m->friendlist[i].client_id, client_id, CLIENT_ID_SIZE); + id_copy(m->friendlist[i].client_id, client_id); m->friendlist[i].statusmessage = calloc(1, 1); m->friendlist[i].statusmessage_length = 1; m->friendlist[i].userstatus = USERSTATUS_NONE; @@ -243,7 +243,7 @@ int m_addfriend_norequest(Messenger *m, uint8_t *client_id) if (realloc_friendlist(m, m->numfriends + 1) != 0) return FAERR_NOMEM; - if (memcmp(client_id, m->net_crypto->self_public_key, crypto_box_PUBLICKEYBYTES) == 0) + if (id_equal(client_id, m->net_crypto->self_public_key)) return FAERR_OWNKEY; memset(&(m->friendlist[m->numfriends]), 0, sizeof(Friend)); @@ -256,7 +256,7 @@ int m_addfriend_norequest(Messenger *m, uint8_t *client_id) m->friendlist[i].status = FRIEND_CONFIRMED; m->friendlist[i].crypt_connection_id = -1; m->friendlist[i].friendrequest_lastsent = 0; - memcpy(m->friendlist[i].client_id, client_id, CLIENT_ID_SIZE); + id_copy(m->friendlist[i].client_id, client_id); m->friendlist[i].statusmessage = calloc(1, 1); m->friendlist[i].statusmessage_length = 1; m->friendlist[i].userstatus = USERSTATUS_NONE; @@ -728,7 +728,7 @@ static int group_num(Messenger *m, uint8_t *group_public_key) uint32_t i; for (i = 0; i < m->numchats; ++i) { - if (memcmp(m->chats[i]->self_public_key, group_public_key, crypto_box_PUBLICKEYBYTES) == 0) + if (id_equal(m->chats[i]->self_public_key, group_public_key)) return i; } @@ -917,8 +917,8 @@ int join_groupchat(Messenger *m, int friendnumber, uint8_t *friend_group_public_ if (groupnum == -1) return -1; - memcpy(data, friend_group_public_key, crypto_box_PUBLICKEYBYTES); - memcpy(data + crypto_box_PUBLICKEYBYTES, m->chats[groupnum]->self_public_key, crypto_box_PUBLICKEYBYTES); + id_copy(data, friend_group_public_key); + id_copy(data + crypto_box_PUBLICKEYBYTES, m->chats[groupnum]->self_public_key); if (write_cryptpacket_id(m, friendnumber, PACKET_ID_JOIN_GROUPCHAT, data, sizeof(data))) { chat_bootstrap_nonlazy(m->chats[groupnum], get_friend_ipport(m, friendnumber), @@ -965,7 +965,7 @@ static int handle_group(void *object, IP_Port source, uint8_t *packet, uint32_t if (m->chats[i] == NULL) continue; - if (memcmp(packet + 1, m->chats[i]->self_public_key, crypto_box_PUBLICKEYBYTES) == 0) + if (id_equal(packet + 1, m->chats[i]->self_public_key)) return handle_groupchatpacket(m->chats[i], source, packet, length); } @@ -1769,6 +1769,8 @@ static char *ID2String(uint8_t *client_id) /* The main loop that needs to be run at least 20 times per second. */ void do_messenger(Messenger *m) { + unix_time_update(); + networking_poll(m->net); do_DHT(m->dht); @@ -1888,144 +1890,6 @@ void wait_cleanup_messenger(Messenger *m, uint8_t *data, uint16_t len) networking_wait_cleanup(m->net, data, len); } -/* return size of the messenger data (for saving) */ -uint32_t Messenger_size_old(Messenger *m) -{ - return crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES - + sizeof(uint32_t) // nospam. - + sizeof(uint32_t) // DHT size. - + DHT_size(m->dht) // DHT itself. - + sizeof(uint32_t) // Friendlist size. - + sizeof(Friend) * m->numfriends // Friendlist itself. - + sizeof(uint16_t) // Own nickname length. - + m->name_length // Own nickname. - ; -} - -/* Save the messenger in data of size Messenger_size(). Old version without cookies. */ -static void Messenger_save_old(Messenger *m, uint8_t *data) -{ - save_keys(m->net_crypto, data); - data += crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES; - - uint32_t nospam = get_nospam(&(m->fr)); - memcpy(data, &nospam, sizeof(nospam)); - data += sizeof(nospam); - - uint32_t size = DHT_size(m->dht); - memcpy(data, &size, sizeof(size)); - data += sizeof(size); - DHT_save(m->dht, data); - data += size; - - size = sizeof(Friend) * m->numfriends; - memcpy(data, &size, sizeof(size)); - data += sizeof(size); - memcpy(data, m->friendlist, sizeof(Friend) * m->numfriends); - data += size; - - uint16_t small_size = m->name_length; - memcpy(data, &small_size, sizeof(small_size)); - data += sizeof(small_size); - memcpy(data, m->name, small_size); -} - -/* Load the messenger from data of size length. Old version without cookies. */ -static int Messenger_load_old(Messenger *m, uint8_t *data, uint32_t length) -{ - if (length == ~((uint32_t)0)) - return -1; - - /* BLOCK1: PUBKEY, SECKEY, NOSPAM, SIZE */ - if (length < crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES + sizeof(uint32_t) * 2) - return -1; - - load_keys(m->net_crypto, data); - data += crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES; - length -= crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES; - - uint32_t nospam; - memcpy(&nospam, data, sizeof(nospam)); - set_nospam(&(m->fr), nospam); - data += sizeof(nospam); - length -= sizeof(nospam); - - uint32_t size; - - if (length < sizeof(size)) - return -1; - - memcpy(&size, data, sizeof(size)); - data += sizeof(size); - length -= sizeof(size); - - if (length < size) - return -1; - - if (DHT_load_old(m->dht, data, size) == -1) { -#ifdef DEBUG - fprintf(stderr, "Data file: Something wicked happened to the stored connections...\n"); - /* DO go on, friends/name still might be intact */ -#endif - } - - data += size; - length -= size; - - if (length < sizeof(size)) - return -1; - - memcpy(&size, data, sizeof(size)); - data += sizeof(size); - length -= sizeof(size); - - if (length < size) - return -1; - - if (!(size % sizeof(Friend))) { - uint16_t num = size / sizeof(Friend); - Friend *friend_list = (Friend *)data; - - uint32_t i; - - for (i = 0; i < num; ++i) { - if (friend_list[i].status >= 3) { - int fnum = m_addfriend_norequest(m, friend_list[i].client_id); - setfriendname(m, fnum, friend_list[i].name, friend_list[i].name_length); - /* set_friend_statusmessage(fnum, temp[i].statusmessage, temp[i].statusmessage_length); */ - } else if (friend_list[i].status != 0) { - /* TODO: This is not a good way to do this. */ - uint8_t address[FRIEND_ADDRESS_SIZE]; - memcpy(address, friend_list[i].client_id, crypto_box_PUBLICKEYBYTES); - memcpy(address + crypto_box_PUBLICKEYBYTES, &(friend_list[i].friendrequest_nospam), sizeof(uint32_t)); - uint16_t checksum = address_checksum(address, FRIEND_ADDRESS_SIZE - sizeof(checksum)); - memcpy(address + crypto_box_PUBLICKEYBYTES + sizeof(uint32_t), &checksum, sizeof(checksum)); - m_addfriend(m, address, friend_list[i].info, friend_list[i].info_size); - } - } - } - - data += size; - length -= size; - - uint16_t small_size; - - if (length < sizeof(small_size)) - return -1; - - memcpy(&small_size, data, sizeof(small_size)); - data += sizeof(small_size); - length -= sizeof(small_size); - - if (length < small_size) - return -1; - - setname(m, data, small_size); - - return 0; -} - - /* new messenger format for load/save, more robust and forward compatible */ #define MESSENGER_STATE_COOKIE_GLOBAL 0x15ed1b1e @@ -2130,7 +1994,7 @@ static int messenger_load_state_callback(void *outer, uint8_t *data, uint32_t le } else if (friends[i].status != 0) { /* TODO: This is not a good way to do this. */ uint8_t address[FRIEND_ADDRESS_SIZE]; - memcpy(address, friends[i].client_id, crypto_box_PUBLICKEYBYTES); + id_copy(address, friends[i].client_id); memcpy(address + crypto_box_PUBLICKEYBYTES, &(friends[i].friendrequest_nospam), sizeof(uint32_t)); uint16_t checksum = address_checksum(address, FRIEND_ADDRESS_SIZE - sizeof(checksum)); memcpy(address + crypto_box_PUBLICKEYBYTES + sizeof(uint32_t), &checksum, sizeof(checksum)); @@ -2174,7 +2038,7 @@ int messenger_load(Messenger *m, uint8_t *data, uint32_t length) return load_state(messenger_load_state_callback, m, data + cookie_len, length - cookie_len, MESSENGER_STATE_COOKIE_TYPE); else /* old state file */ - return Messenger_load_old(m, data, length); + return -1; } /* Return the number of friends in the instance m. diff --git a/toxcore/friend_requests.c b/toxcore/friend_requests.c index c821d998..67977c23 100644 --- a/toxcore/friend_requests.c +++ b/toxcore/friend_requests.c @@ -26,6 +26,7 @@ #endif #include "friend_requests.h" +#include "util.h" /* Try to send a friend request to peer with public_key. * data is the data in the request and length is the length. @@ -102,7 +103,7 @@ static void addto_receivedlist(Friend_Requests *fr, uint8_t *client_id) if (fr->received_requests_index >= MAX_RECEIVED_STORED) fr->received_requests_index = 0; - memcpy(fr->received_requests[fr->received_requests_index], client_id, crypto_box_PUBLICKEYBYTES); + id_copy(fr->received_requests[fr->received_requests_index], client_id); ++fr->received_requests_index; } @@ -115,10 +116,9 @@ static int request_received(Friend_Requests *fr, uint8_t *client_id) { uint32_t i; - for (i = 0; i < MAX_RECEIVED_STORED; ++i) { - if (memcmp(fr->received_requests[i], client_id, crypto_box_PUBLICKEYBYTES) == 0) + for (i = 0; i < MAX_RECEIVED_STORED; ++i) + if (id_equal(fr->received_requests[i], client_id)) return 1; - } return 0; } diff --git a/toxcore/group_chats.c b/toxcore/group_chats.c index 0d6d6972..fdf6e834 100644 --- a/toxcore/group_chats.c +++ b/toxcore/group_chats.c @@ -27,7 +27,7 @@ #endif #include "group_chats.h" - +#include "util.h" #define GROUPCHAT_MAXDATA_LENGTH (MAX_DATA_SIZE - (1 + crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES)) #define GROUPCHAT_MAXPLAINDATA_LENGTH (GROUPCHAT_MAXDATA_LENGTH - crypto_box_MACBYTES) @@ -67,16 +67,14 @@ static int peer_in_chat(Group_Chat *chat, uint8_t *client_id) { uint32_t i; - for (i = 0; i < chat->numpeers; ++i) { - /* Equal */ - if (memcmp(chat->group[i].client_id, client_id, crypto_box_PUBLICKEYBYTES) == 0) + for (i = 0; i < chat->numpeers; ++i) + if (id_equal(chat->group[i].client_id, client_id)) return i; - } return -1; } -#define BAD_NODE_TIMEOUT 30 +#define BAD_GROUPNODE_TIMEOUT 30 /* * Check if peer is closer to us that the other peers in the list and if the peer is in the list. @@ -87,19 +85,18 @@ static int peer_in_chat(Group_Chat *chat, uint8_t *client_id) static int peer_okping(Group_Chat *chat, uint8_t *client_id) { uint32_t i, j = 0; - uint64_t temp_time = unix_time(); - if (memcmp(chat->self_public_key, client_id, crypto_box_PUBLICKEYBYTES) == 0) + if (id_equal(chat->self_public_key, client_id)) return -1; for (i = 0; i < GROUP_CLOSE_CONNECTIONS; ++i) { - if (chat->close[i].last_recv + BAD_NODE_TIMEOUT < temp_time) { + if (is_timeout(chat->close[i].last_recv, BAD_GROUPNODE_TIMEOUT)) { ++j; continue; } /* Equal */ - if (memcmp(chat->close[i].client_id, client_id, crypto_box_PUBLICKEYBYTES) == 0) + if (id_equal(chat->close[i].client_id, client_id)) return -1; if (id_closest(chat->self_public_key, chat->close[i].client_id, client_id) == 2) @@ -121,29 +118,28 @@ static int peer_okping(Group_Chat *chat, uint8_t *client_id) static int add_closepeer(Group_Chat *chat, uint8_t *client_id, IP_Port ip_port) { uint32_t i; - uint64_t temp_time = unix_time(); for (i = 0; i < GROUP_CLOSE_CONNECTIONS; ++i) { /* Check if node is already in list, if it is update its last_recv */ - if (memcmp(chat->close[i].client_id, client_id, crypto_box_PUBLICKEYBYTES) == 0) { - chat->close[i].last_recv = temp_time; + if (id_equal(chat->close[i].client_id, client_id)) { + chat->close[i].last_recv = unix_time(); return 0; } } for (i = 0; i < GROUP_CLOSE_CONNECTIONS; ++i) { /* Try replacing bad nodes first */ - if (chat->close[i].last_recv + BAD_NODE_TIMEOUT < temp_time) { - memcpy(chat->close[i].client_id, client_id, crypto_box_PUBLICKEYBYTES); + if (is_timeout(chat->close[i].last_recv, BAD_GROUPNODE_TIMEOUT)) { + id_copy(chat->close[i].client_id, client_id); chat->close[i].ip_port = ip_port; - chat->close[i].last_recv = temp_time; + chat->close[i].last_recv = unix_time(); return 0; } } for (i = 0; i < GROUP_CLOSE_CONNECTIONS; ++i) { /* Replace nodes if given one is closer. */ if (id_closest(chat->self_public_key, chat->close[i].client_id, client_id) == 2) { - memcpy(chat->close[i].client_id, client_id, crypto_box_PUBLICKEYBYTES); + id_copy(chat->close[i].client_id, client_id); chat->close[i].ip_port = ip_port; - chat->close[i].last_recv = temp_time; + chat->close[i].last_recv = unix_time(); return 0; } } @@ -154,7 +150,7 @@ static int add_closepeer(Group_Chat *chat, uint8_t *client_id, IP_Port ip_port) static int send_groupchatpacket(Group_Chat *chat, IP_Port ip_port, uint8_t *public_key, uint8_t *data, uint32_t length, uint8_t request_id) { - if (memcmp(chat->self_public_key, public_key, crypto_box_PUBLICKEYBYTES) == 0) + if (id_equal(chat->self_public_key, public_key)) return -1; uint8_t packet[MAX_DATA_SIZE]; @@ -180,11 +176,12 @@ static uint8_t sendto_allpeers(Group_Chat *chat, uint8_t *data, uint16_t length, { uint16_t sent = 0; uint32_t i; - uint64_t temp_time = unix_time(); for (i = 0; i < GROUP_CLOSE_CONNECTIONS; ++i) { - if (ip_isset(&chat->close[i].ip_port.ip) && chat->close[i].last_recv + BAD_NODE_TIMEOUT > temp_time) { - if (send_groupchatpacket(chat, chat->close[i].ip_port, chat->close[i].client_id, data, length, request_id) == 0) + if (ip_isset(&chat->close[i].ip_port.ip) && + !is_timeout(chat->close[i].last_recv, BAD_GROUPNODE_TIMEOUT)) { + if (send_groupchatpacket(chat, chat->close[i].ip_port, chat->close[i].client_id, + data, length, request_id) == 0) ++sent; } } @@ -214,7 +211,7 @@ static int addpeer(Group_Chat *chat, uint8_t *client_id) return -1; chat->group = temp; - memcpy(chat->group[chat->numpeers].client_id, client_id, crypto_box_PUBLICKEYBYTES); + id_copy(chat->group[chat->numpeers].client_id, client_id); ++chat->numpeers; return (chat->numpeers - 1); } @@ -232,14 +229,11 @@ static int delpeer(Group_Chat *chat, uint8_t *client_id) for (i = 0; i < chat->numpeers; ++i) { /* Equal */ - if (memcmp(chat->group[i].client_id, client_id, crypto_box_PUBLICKEYBYTES) == 0) { + if (id_equal(chat->group[i].client_id, client_id)) { --chat->numpeers; - if (chat->numpeers != i) { - memcpy( chat->group[i].client_id, - chat->group[chat->numpeers].client_id, - crypto_box_PUBLICKEYBYTES ); - } + if (chat->numpeers != i) + id_copy(chat->group[i].client_id, chat->group[chat->numpeers].client_id); temp = realloc(chat->group, sizeof(Group_Peer) * (chat->numpeers)); @@ -276,22 +270,23 @@ int group_peername(Group_Chat *chat, int peernum, uint8_t *name) /* min time between pings sent to one peer in seconds */ +/* TODO: move this to global section */ #define PING_TIMEOUT 5 + static int send_getnodes(Group_Chat *chat, IP_Port ip_port, int peernum) { if ((uint32_t)peernum >= chat->numpeers) return -1; - uint64_t temp_time = unix_time(); - - getnodes_data contents; - - if (chat->group[peernum].last_pinged + PING_TIMEOUT > temp_time) + if (!is_timeout(chat->group[peernum].last_pinged, PING_TIMEOUT)) return -1; + getnodes_data contents; contents.pingid = ((uint64_t)random_int() << 32) + random_int(); - chat->group[peernum].last_pinged = temp_time; + + chat->group[peernum].last_pinged = unix_time(); chat->group[peernum].pingid = contents.pingid; + return send_groupchatpacket(chat, ip_port, chat->group[peernum].client_id, (uint8_t *)&contents, sizeof(contents), 48); } @@ -303,11 +298,10 @@ static int send_sendnodes(Group_Chat *chat, IP_Port ip_port, int peernum, uint64 sendnodes_data contents; contents.pingid = pingid; uint32_t i, j = 0; - uint64_t temp_time = unix_time(); for (i = 0; i < GROUP_CLOSE_CONNECTIONS; ++i) { - if (chat->close[i].last_recv + BAD_NODE_TIMEOUT > temp_time) { - memcpy(contents.nodes[j].client_id, chat->close[i].client_id, crypto_box_PUBLICKEYBYTES); + if (!is_timeout(chat->close[i].last_recv, BAD_GROUPNODE_TIMEOUT)) { + id_copy(contents.nodes[j].client_id, chat->close[i].client_id); contents.nodes[j].ip_port = chat->close[i].ip_port; ++j; } @@ -346,7 +340,7 @@ static int handle_sendnodes(Group_Chat *chat, IP_Port source, int peernum, uint8 if ((len - sizeof(uint64_t)) % sizeof(groupchat_nodes) != 0) return 1; - if (chat->group[peernum].last_pinged + PING_TIMEOUT < unix_time()) + if (is_timeout(chat->group[peernum].last_pinged, PING_TIMEOUT)) return 1; sendnodes_data contents; @@ -376,7 +370,9 @@ static int handle_sendnodes(Group_Chat *chat, IP_Port source, int peernum, uint8 add_closepeer(chat, chat->group[peernum].client_id, source); return 0; } + #define GROUP_DATA_MIN_SIZE (crypto_box_PUBLICKEYBYTES + sizeof(uint32_t) + 1) + static int handle_data(Group_Chat *chat, uint8_t *data, uint32_t len) { if (len < GROUP_DATA_MIN_SIZE) @@ -392,7 +388,6 @@ static int handle_data(Group_Chat *chat, uint8_t *data, uint32_t len) if (peernum == -1) return 1; - uint64_t temp_time = unix_time(); /* Spam prevention (1 message per peer per second limit.) if (chat->group[peernum].last_recv == temp_time) @@ -421,7 +416,8 @@ static int handle_data(Group_Chat *chat, uint8_t *data, uint32_t len) if (contents_len != 0) return 1; - chat->group[peernum].last_recv_msgping = temp_time; + chat->group[peernum].last_recv_msgping = unix_time(); + break; case 16: /* If message is new peer */ if (contents_len != crypto_box_PUBLICKEYBYTES) @@ -463,7 +459,7 @@ static uint8_t send_data(Group_Chat *chat, uint8_t *data, uint32_t len, uint8_t uint32_t message_num = htonl(chat->message_number); //TODO - memcpy(packet, chat->self_public_key, crypto_box_PUBLICKEYBYTES); + id_copy(packet, chat->self_public_key); memcpy(packet + crypto_box_PUBLICKEYBYTES, &message_num, sizeof(message_num)); memcpy(packet + GROUP_DATA_MIN_SIZE, data, len); packet[crypto_box_PUBLICKEYBYTES + sizeof(message_num)] = message_id; @@ -489,7 +485,7 @@ int handle_groupchatpacket(Group_Chat *chat, IP_Port source, uint8_t *packet, ui if (len <= 0) return 1; - if (memcmp(chat->self_public_key, public_key, crypto_box_PUBLICKEYBYTES) == 0) + if (id_equal(chat->self_public_key, public_key)) return 1; int peernum = peer_in_chat(chat, public_key); @@ -534,6 +530,8 @@ void callback_groupmessage(Group_Chat *chat, void (*function)(Group_Chat *chat, Group_Chat *new_groupchat(Networking_Core *net) { + unix_time_update(); + if (net == 0) return 0; @@ -548,16 +546,16 @@ Group_Chat *new_groupchat(Networking_Core *net) static void ping_close(Group_Chat *chat) { uint32_t i; - uint64_t temp_time = unix_time(); for (i = 0; i < GROUP_CLOSE_CONNECTIONS; ++i) { - if (chat->close[i].last_recv < temp_time + BAD_NODE_TIMEOUT) { + /* previous condition was always true, assuming this is the wanted one: */ + if (!is_timeout(chat->close[i].last_recv, BAD_GROUPNODE_TIMEOUT)) { int peernum = peer_in_chat(chat, chat->close[i].client_id); if (peernum == -1) continue; - if (chat->group[peernum].last_pinged + NODE_PING_INTERVAL < temp_time) + if (is_timeout(chat->group[peernum].last_pinged, NODE_PING_INTERVAL)) send_getnodes(chat, chat->close[i].ip_port, peernum); } } @@ -565,6 +563,7 @@ static void ping_close(Group_Chat *chat) void do_groupchat(Group_Chat *chat) { + unix_time_update(); ping_close(chat); } @@ -584,4 +583,3 @@ void chat_bootstrap_nonlazy(Group_Chat *chat, IP_Port ip_port, uint8_t *client_i send_getnodes(chat, ip_port, addpeer(chat, client_id)); add_closepeer(chat, client_id, ip_port); } - diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c index 14cee200..d58f4c27 100644 --- a/toxcore/net_crypto.c +++ b/toxcore/net_crypto.c @@ -29,7 +29,7 @@ #endif #include "net_crypto.h" - +#include "util.h" static uint8_t crypt_connection_id_not_valid(Net_Crypto *c, int crypt_connection_id) { return (uint32_t)crypt_connection_id >= c->crypto_connections_length; @@ -799,6 +799,8 @@ static void receive_crypto(Net_Crypto *c) */ Net_Crypto *new_net_crypto(Networking_Core *net) { + unix_time_update(); + if (net == NULL) return NULL; @@ -837,6 +839,7 @@ static void kill_timedout(Net_Crypto *c) /* Main loop. */ void do_net_crypto(Net_Crypto *c) { + unix_time_update(); do_lossless_udp(c->lossless_udp); kill_timedout(c); receive_crypto(c); diff --git a/toxcore/network.c b/toxcore/network.c index 640311b3..d163c0eb 100644 --- a/toxcore/network.c +++ b/toxcore/network.c @@ -277,6 +277,8 @@ void networking_registerhandler(Networking_Core *net, uint8_t byte, packet_handl void networking_poll(Networking_Core *net) { + unix_time_update(); + IP_Port ip_port; uint8_t data[MAX_UDP_PACKET_SIZE]; uint32_t length; diff --git a/toxcore/network.h b/toxcore/network.h index c1418c4c..bb851dcb 100644 --- a/toxcore/network.h +++ b/toxcore/network.h @@ -99,12 +99,9 @@ typedef int sock_t; #define NET_PACKET_GROUP_CHATS 48 /* Group chats packet ID. */ #define TOX_PORTRANGE_FROM 33445 -#define TOX_PORTRANGE_TO 33455 +#define TOX_PORTRANGE_TO 33545 #define TOX_PORT_DEFAULT TOX_PORTRANGE_FROM -/* Current time, unix format */ -#define unix_time() ((uint64_t)time(NULL)) - typedef union { uint8_t uint8[4]; uint16_t uint16[2]; diff --git a/toxcore/ping.c b/toxcore/ping.c index 240bd713..3f237836 100644 --- a/toxcore/ping.c +++ b/toxcore/ping.c @@ -64,7 +64,7 @@ typedef struct { static bool is_ping_timeout(uint64_t time) { - return (time + PING_TIMEOUT) < now(); + return is_timeout(time, PING_TIMEOUT); } static void remove_timeouts(PING *ping) // O(n) @@ -107,7 +107,7 @@ static uint64_t add_ping(PING *ping, IP_Port ipp) // O(n) p = (ping->pos_pings + ping->num_pings) % PING_NUM_MAX; ping->pings[p].ip_port = ipp; - ping->pings[p].timestamp = now(); + ping->pings[p].timestamp = unix_time(); ping->pings[p].id = random_64b(); ping->num_pings++; @@ -146,14 +146,14 @@ int send_ping_request(PING *ping, IP_Port ipp, uint8_t *client_id) int rc; uint64_t ping_id; - if (is_pinging(ping, ipp, 0) || id_eq(client_id, ping->c->self_public_key)) + if (is_pinging(ping, ipp, 0) || id_equal(client_id, ping->c->self_public_key)) return 1; // Generate random ping_id. ping_id = add_ping(ping, ipp); pk[0] = NET_PACKET_PING_REQUEST; - id_cpy(pk + 1, ping->c->self_public_key); // Our pubkey + id_copy(pk + 1, ping->c->self_public_key); // Our pubkey new_nonce(pk + 1 + CLIENT_ID_SIZE); // Generate new nonce // Encrypt ping_id using recipient privkey @@ -174,11 +174,11 @@ static int send_ping_response(PING *ping, IP_Port ipp, uint8_t *client_id, uint6 uint8_t pk[DHT_PING_SIZE]; int rc; - if (id_eq(client_id, ping->c->self_public_key)) + if (id_equal(client_id, ping->c->self_public_key)) return 1; pk[0] = NET_PACKET_PING_RESPONSE; - id_cpy(pk + 1, ping->c->self_public_key); // Our pubkey + id_copy(pk + 1, ping->c->self_public_key); // Our pubkey new_nonce(pk + 1 + CLIENT_ID_SIZE); // Generate new nonce // Encrypt ping_id using recipient privkey @@ -205,7 +205,7 @@ static int handle_ping_request(void *_dht, IP_Port source, uint8_t *packet, uint PING *ping = dht->ping; - if (id_eq(packet + 1, ping->c->self_public_key)) + if (id_equal(packet + 1, ping->c->self_public_key)) return 1; // Decrypt ping_id @@ -237,7 +237,7 @@ static int handle_ping_response(void *_dht, IP_Port source, uint8_t *packet, uin PING *ping = dht->ping; - if (id_eq(packet + 1, ping->c->self_public_key)) + if (id_equal(packet + 1, ping->c->self_public_key)) return 1; // Decrypt ping_id @@ -301,19 +301,12 @@ int add_toping(PING *ping, uint8_t *client_id, IP_Port ip_port) /* Ping all the valid nodes in the toping list every TIME_TOPING seconds. * This function must be run at least once every TIME_TOPING seconds. */ -static int is_timeout(uint64_t time_now, uint64_t timestamp, uint64_t timeout) -{ - return timestamp + timeout <= time_now; -} - void do_toping(PING *ping) { - uint64_t temp_time = unix_time(); - - if (!is_timeout(temp_time, ping->last_toping, TIME_TOPING)) + if (!is_timeout(ping->last_toping, TIME_TOPING)) return; - ping->last_toping = temp_time; + ping->last_toping = unix_time(); uint32_t i; for (i = 0; i < MAX_TOPING; ++i) { diff --git a/toxcore/tox.h b/toxcore/tox.h index 2a26e7a6..5d3916b4 100644 --- a/toxcore/tox.h +++ b/toxcore/tox.h @@ -62,7 +62,7 @@ extern "C" { #define TOX_FRIEND_ADDRESS_SIZE (TOX_CLIENT_ID_SIZE + sizeof(uint32_t) + sizeof(uint16_t)) #define TOX_PORTRANGE_FROM 33445 -#define TOX_PORTRANGE_TO 33455 +#define TOX_PORTRANGE_TO 33545 #define TOX_PORT_DEFAULT TOX_PORTRANGE_FROM typedef union { @@ -521,7 +521,8 @@ uint64_t tox_file_dataremaining(Tox *tox, int friendnumber, uint8_t filenumber, void tox_bootstrap_from_ip(Tox *tox, tox_IP_Port ip_port, uint8_t *public_key); /* Resolves address into an IP address. If successful, sends a "get nodes" - * request to the given node with ip, port and public_key to setup connections + * request to the given node with ip, port (in network byte order, HINT: use htons()) + * and public_key to setup connections * * address can be a hostname or an IP address (IPv4 or IPv6). * if ipv6enabled is 0 (zero), the resolving sticks STRICTLY to IPv4 addresses diff --git a/toxcore/util.c b/toxcore/util.c index 620d1f4f..fab9f660 100644 --- a/toxcore/util.c +++ b/toxcore/util.c @@ -33,11 +33,6 @@ #include "util.h" -uint64_t now() -{ - return time(NULL); -} - uint64_t random_64b() { uint64_t r; @@ -50,16 +45,39 @@ uint64_t random_64b() return r; } -bool id_eq(uint8_t *dest, uint8_t *src) +/* don't call into system billions of times for no reason */ +static uint64_t unix_time_value; + +void unix_time_update() +{ + unix_time_value = (uint64_t)time(NULL); +} + +uint64_t unix_time() +{ + return unix_time_value; +} + +int is_timeout(uint64_t timestamp, uint64_t timeout) +{ + return timestamp + timeout <= unix_time_value; +} + + +/* id functions */ +bool id_equal(uint8_t *dest, uint8_t *src) { return memcmp(dest, src, CLIENT_ID_SIZE) == 0; } -void id_cpy(uint8_t *dest, uint8_t *src) +uint32_t id_copy(uint8_t *dest, uint8_t *src) { memcpy(dest, src, CLIENT_ID_SIZE); + return CLIENT_ID_SIZE; } + +/* state load/save */ int load_state(load_state_callback_func load_state_callback, void *outer, uint8_t *data, uint32_t length, uint16_t cookie_inner) { diff --git a/toxcore/util.h b/toxcore/util.h index 97f82f60..20dcb2de 100644 --- a/toxcore/util.h +++ b/toxcore/util.h @@ -28,11 +28,19 @@ #include #include -uint64_t now(); uint64_t random_64b(); -bool id_eq(uint8_t *dest, uint8_t *src); -void id_cpy(uint8_t *dest, uint8_t *src); +void unix_time_update(); +uint64_t unix_time(); +int is_timeout(uint64_t timestamp, uint64_t timeout); + + +/* id functions */ +bool id_equal(uint8_t *dest, uint8_t *src); +uint32_t id_copy(uint8_t *dest, uint8_t *src); /* return value is CLIENT_ID_SIZE */ + + +/* state load/save */ typedef int (*load_state_callback_func)(void *outer, uint8_t *data, uint32_t len, uint16_t type); int load_state(load_state_callback_func load_state_callback, void *outer, uint8_t *data, uint32_t length, uint16_t cookie_inner); diff --git a/toxmsi/Makefile.inc b/toxmsi/Makefile.inc index 27678bc0..7d620e70 100644 --- a/toxmsi/Makefile.inc +++ b/toxmsi/Makefile.inc @@ -3,7 +3,8 @@ if BUILD_AV lib_LTLIBRARIES += libtoxmsi.la libtoxmsi_la_include_HEADERS = \ - ../toxmsi/toxmsi.h + ../toxmsi/toxmsi.h \ + ../toxmsi/toxmedia.h libtoxmsi_la_includedir = $(includedir)/tox @@ -35,8 +36,7 @@ libtoxmsi_la_LIBS = $(NACL_LIBS) noinst_PROGRAMS += phone phone_SOURCES = ../toxmsi/phone.c \ - ../toxmsi/AV_codec.h \ - ../toxmsi/AV_codec.c + ../toxmsi/toxmedia.c phone_CFLAGS = -I../toxcore \ -I../toxrtp \ diff --git a/toxmsi/phone.c b/toxmsi/phone.c index f14d0323..432be94c 100644 --- a/toxmsi/phone.c +++ b/toxmsi/phone.c @@ -16,7 +16,7 @@ #include /* #include Can this be removed? */ #include -#include "AV_codec.h" +#include "toxmedia.h" diff --git a/toxmsi/phone.h b/toxmsi/phone.h index d661dcfd..f96aac73 100644 --- a/toxmsi/phone.h +++ b/toxmsi/phone.h @@ -8,7 +8,7 @@ #include "../toxrtp/tests/test_helper.h" #include #include -#include "AV_codec.h" +#include "toxmedia.h" /* Define client version */ #define _USERAGENT "tox_phone-v.0.2.1" diff --git a/toxmsi/AV_codec.c b/toxmsi/toxmedia.c similarity index 99% rename from toxmsi/AV_codec.c rename to toxmsi/toxmedia.c index 82e6a320..4c9f5261 100644 --- a/toxmsi/AV_codec.c +++ b/toxmsi/toxmedia.c @@ -36,8 +36,8 @@ #include #include #include -#include -#include +#include +#include #include #include @@ -46,7 +46,9 @@ #include "../toxrtp/toxrtp_message.h" #include "../toxrtp/tests/test_helper.h" #include "phone.h" -#include "AV_codec.h" +#include "toxmedia.h" + +SDL_Surface *screen; int display_received_frame(codec_state *cs, AVFrame *r_video_frame) { diff --git a/toxmsi/AV_codec.h b/toxmsi/toxmedia.h similarity index 97% rename from toxmsi/AV_codec.h rename to toxmsi/toxmedia.h index 641cf486..7eea39ae 100644 --- a/toxmsi/AV_codec.h +++ b/toxmsi/toxmedia.h @@ -35,10 +35,10 @@ #include #include #include -#include "../toxrtp/toxrtp.h" -#include "../toxcore/tox.h" +#include "toxrtp.h" +#include "tox.h" -#include +#include #include /* ffmpeg VP8 codec ID */ @@ -75,7 +75,7 @@ #define DEFAULT_WEBCAM "0" #endif -SDL_Surface *screen; +extern SDL_Surface *screen; typedef struct { SDL_Overlay *bmp; diff --git a/toxmsi/toxmsi.h b/toxmsi/toxmsi.h index 69d5144a..d8985c64 100644 --- a/toxmsi/toxmsi.h +++ b/toxmsi/toxmsi.h @@ -28,7 +28,7 @@ #define _MSI_IMPL_H_ #include -#include "../toxcore/tox.h" +#include "tox.h" #include #define MCBTYPE void diff --git a/toxrtp/toxrtp.h b/toxrtp/toxrtp.h index 44717835..0aa89993 100644 --- a/toxrtp/toxrtp.h +++ b/toxrtp/toxrtp.h @@ -30,7 +30,7 @@ #define RTP_VERSION 2 #include -#include "../toxcore/tox.h" +#include "tox.h" #include /* Extension header flags */ #define RTP_EXT_TYPE_RESOLUTION 0x01