From ac621d96d3139ca09eeebd509d6f6b92380f4f41 Mon Sep 17 00:00:00 2001 From: irungentoo Date: Tue, 20 Aug 2013 14:47:32 -0400 Subject: [PATCH] Refactor of core done. --- core/DHT.c | 24 ++++----- core/DHT.h | 2 +- core/Messenger.c | 18 +++---- core/Messenger.h | 20 ++++--- core/net_crypto.c | 1 - core/net_crypto.h | 2 - core/ping.c | 97 ++++++++++++++++++---------------- core/ping.h | 11 ++-- {core => other/unused}/timer.c | 0 {core => other/unused}/timer.h | 0 testing/toxic/chat.c | 2 +- testing/toxic/dhtstatus.c | 4 +- testing/toxic/friendlist.c | 2 +- testing/toxic/prompt.c | 2 +- testing/toxic/windows.c | 2 +- testing/toxic/windows.h | 2 +- 16 files changed, 93 insertions(+), 96 deletions(-) rename {core => other/unused}/timer.c (100%) rename {core => other/unused}/timer.h (100%) diff --git a/core/DHT.c b/core/DHT.c index d58c6d2c..a139f2c5 100644 --- a/core/DHT.c +++ b/core/DHT.c @@ -603,7 +603,7 @@ static int handle_sendnodes(void * object, IP_Port source, uint8_t *packet, uint uint32_t i; for (i = 0; i < num_nodes; ++i) { - send_ping_request(nodes_list[i].ip_port, (clientid_t *) &nodes_list[i].client_id); + send_ping_request(dht->ping, dht->c, nodes_list[i].ip_port, (clientid_t *) &nodes_list[i].client_id); returnedip_ports(dht, nodes_list[i].ip_port, nodes_list[i].client_id, packet + 1); } @@ -709,7 +709,7 @@ static void do_DHT_friends(DHT * dht) /* if node is not dead. */ if (!is_timeout(temp_time, dht->friends_list[i].client_list[j].timestamp, Kill_NODE_TIMEOUT)) { if ((dht->friends_list[i].client_list[j].last_pinged + PING_INTERVAL) <= temp_time) { - send_ping_request( dht->friends_list[i].client_list[j].ip_port, + send_ping_request(dht->ping, dht->c, dht->friends_list[i].client_list[j].ip_port, (clientid_t *) &dht->friends_list[i].client_list[j].client_id ); dht->friends_list[i].client_list[j].last_pinged = temp_time; } @@ -747,7 +747,7 @@ static void do_Close(DHT * dht) /* if node is not dead. */ if (!is_timeout(temp_time, dht->close_clientlist[i].timestamp, Kill_NODE_TIMEOUT)) { if ((dht->close_clientlist[i].last_pinged + PING_INTERVAL) <= temp_time) { - send_ping_request( dht->close_clientlist[i].ip_port, + send_ping_request(dht->ping, dht->c, dht->close_clientlist[i].ip_port, (clientid_t *) &dht->close_clientlist[i].client_id ); dht->close_clientlist[i].last_pinged = temp_time; } @@ -772,8 +772,7 @@ static void do_Close(DHT * dht) void DHT_bootstrap(DHT * dht, IP_Port ip_port, uint8_t *public_key) { getnodes(dht, ip_port, public_key, dht->c->self_public_key); - //send_ping_request(dht, ip_port, (clientid_t *) public_key); - send_ping_request(ip_port, (clientid_t *) public_key); + send_ping_request(dht->ping, dht->c, ip_port, (clientid_t *) public_key); } /* send the given packet to node with client_id @@ -1039,8 +1038,7 @@ static void punch_holes(DHT * dht, IP ip, uint16_t *port_list, uint16_t numports /*TODO: improve port guessing algorithm*/ uint16_t port = port_list[(i / 2) % numports] + (i / (2 * numports)) * ((i % 2) ? -1 : 1); IP_Port pinging = {ip, htons(port)}; - //send_ping_request(dht, pinging, (clientid_t *) &dht->friends_list[friend_num].client_id); - send_ping_request(pinging, (clientid_t *) &dht->friends_list[friend_num].client_id); + send_ping_request(dht->ping, dht->c, pinging, (clientid_t *) &dht->friends_list[friend_num].client_id); } dht->friends_list[friend_num].punching_index = i; @@ -1139,8 +1137,7 @@ static void do_toping(DHT * dht) if (dht->toping[i].ip_port.ip.i == 0) return; - //send_ping_request(dht, dht->toping[i].ip_port, (clientid_t *) dht->toping[i].client_id); - send_ping_request(dht->toping[i].ip_port, (clientid_t *) dht->toping[i].client_id); + send_ping_request(dht->ping, dht->c, dht->toping[i].ip_port, (clientid_t *) dht->toping[i].client_id); dht->toping[i].ip_port.ip.i = 0; } } @@ -1151,13 +1148,17 @@ DHT * new_DHT(Net_Crypto *c) DHT * temp = calloc(1, sizeof(DHT)); if (temp == NULL) return NULL; + temp->ping = new_ping(); + if (temp->ping == NULL){ + kill_DHT(temp); + return NULL; + } temp->c = c; networking_registerhandler(c->lossless_udp->net, 0, &handle_ping_request, temp); networking_registerhandler(c->lossless_udp->net, 1, &handle_ping_response, temp); networking_registerhandler(c->lossless_udp->net, 2, &handle_getnodes, temp); networking_registerhandler(c->lossless_udp->net, 3, &handle_sendnodes, temp); cryptopacket_registerhandler(c, 254, &handle_NATping, temp); - temp_DHT = temp; return temp; } @@ -1170,6 +1171,7 @@ void do_DHT(DHT * dht) } void kill_DHT(DHT * dht) { + kill_ping(dht->ping); free(dht->friends_list); free(dht); } @@ -1193,8 +1195,6 @@ void DHT_save(DHT * dht, uint8_t *data) */ int DHT_load(DHT * dht, uint8_t *data, uint32_t size) { - init_ping(); - if (size < sizeof(dht->close_clientlist)) return -1; diff --git a/core/DHT.h b/core/DHT.h index 0ecc0b50..7d926b13 100644 --- a/core/DHT.h +++ b/core/DHT.h @@ -100,9 +100,9 @@ typedef struct { Node_format toping[MAX_TOPING]; uint64_t last_toping; uint64_t close_lastgetnodes; + void * ping; } DHT; /*----------------------------------------------------------------------------------*/ -DHT * temp_DHT; //TODO: remove Client_data *DHT_get_close_list(DHT * dht); diff --git a/core/Messenger.c b/core/Messenger.c index 25a4ff98..a6cbb3ac 100644 --- a/core/Messenger.c +++ b/core/Messenger.c @@ -22,7 +22,6 @@ */ #include "Messenger.h" -#include "timer.h" #define MIN(a,b) (((a)<(b))?(a):(b)) @@ -608,17 +607,17 @@ int write_cryptpacket_id(Messenger *m, int friendnumber, uint8_t packet_id, uint return write_cryptpacket(m->net_crypto, m->friendlist[friendnumber].crypt_connection_id, packet, length + 1); } - /*Interval in seconds between LAN discovery packet sending*/ #define LAN_DISCOVERY_INTERVAL 60 #define PORT 33445 /*Send a LAN discovery packet every LAN_DISCOVERY_INTERVAL seconds*/ -int LANdiscovery(timer *t, void *arg) +static void LANdiscovery(Messenger *m) { - send_LANdiscovery(htons(PORT), temp_net_crypto); - timer_start(t, LAN_DISCOVERY_INTERVAL); - return 0; + if (m->last_LANdiscovery + LAN_DISCOVERY_INTERVAL < unix_time()) { + send_LANdiscovery(htons(PORT), m->net_crypto); + m->last_LANdiscovery = unix_time(); + } } /* run this at startup */ @@ -655,9 +654,6 @@ Messenger *initMessenger(void) set_nospam(&(m->fr), random_int()); init_cryptopackets(m->dht); - send_LANdiscovery(htons(PORT), m->net_crypto); - timer_single(&LANdiscovery, 0, LAN_DISCOVERY_INTERVAL); - return m; } @@ -885,7 +881,6 @@ void doInbound(Messenger *m) } } - /* the main loop that needs to be run at least 20 times per second. */ void doMessenger(Messenger *m) { @@ -895,8 +890,7 @@ void doMessenger(Messenger *m) do_net_crypto(m->net_crypto); doInbound(m); doFriends(m); - - timer_poll(); + LANdiscovery(m); } /* returns the size of the messenger data (for saving) */ diff --git a/core/Messenger.h b/core/Messenger.h index c4e7cc1e..518becc9 100644 --- a/core/Messenger.h +++ b/core/Messenger.h @@ -128,6 +128,8 @@ typedef struct Messenger { Friend *friendlist; uint32_t numfriends; + uint64_t last_LANdiscovery; + void (*friend_message)(struct Messenger *m, int, uint8_t *, uint16_t, void *); void *friend_message_userdata; void (*friend_action)(struct Messenger *m, int, uint8_t *, uint16_t, void *); @@ -222,17 +224,13 @@ int m_sendaction(Messenger *m, int friendnumber, uint8_t *action, uint32_t lengt return -1 if failure */ int setname(Messenger *m, uint8_t *name, uint16_t length); -/** - * @brief Get your nickname. - * - * @param[in] m The messanger context to use. - * - * @param[inout] name Pointer to a string for the name. - * - * @param[in] nlen The length of the string buffer. - * - * @return Return the length of the name, 0 on error. - */ +/* + Get your nickname. + m The messanger context to use. + name Pointer to a string for the name. + nlen The length of the string buffer. + returns Return the length of the name, 0 on error. +*/ uint16_t getself_name(Messenger *m, uint8_t *name, uint16_t nlen); /* get name of friendnumber diff --git a/core/net_crypto.c b/core/net_crypto.c index 2e63d2f1..e36a1666 100644 --- a/core/net_crypto.c +++ b/core/net_crypto.c @@ -706,7 +706,6 @@ Net_Crypto * new_net_crypto(Networking_Core * net) if (temp->lossless_udp == NULL) return NULL; memset(temp->incoming_connections, -1 , sizeof(int) * MAX_INCOMING); - temp_net_crypto = temp; //TODO remove return temp; } diff --git a/core/net_crypto.h b/core/net_crypto.h index 887b432b..7b9e9b97 100644 --- a/core/net_crypto.h +++ b/core/net_crypto.h @@ -73,8 +73,6 @@ typedef struct { #include "DHT.h" -Net_Crypto * temp_net_crypto; //TODO: remove this - #define ENCRYPTION_PADDING (crypto_box_ZEROBYTES - crypto_box_BOXZEROBYTES) /* returns zero if the buffer contains only zeros */ diff --git a/core/ping.c b/core/ping.c index 10b9b19f..5da3c0ca 100644 --- a/core/ping.c +++ b/core/ping.c @@ -23,16 +23,20 @@ typedef struct { uint64_t timestamp; } pinged_t; -static pinged_t pings[PING_NUM_MAX]; -static size_t num_pings; -static size_t pos_pings; +typedef struct { + pinged_t pings[PING_NUM_MAX]; + size_t num_pings; + size_t pos_pings; +} PING; -extern uint8_t self_secret_key[crypto_box_SECRETKEYBYTES]; // DHT.c - -void init_ping() +void * new_ping(void) { - num_pings = 0; - pos_pings = 0; + return calloc(1, sizeof(PING)); +} + +void kill_ping(void * ping) +{ + free(ping); } static bool is_timeout(uint64_t time) @@ -40,17 +44,18 @@ static bool is_timeout(uint64_t time) return (time + PING_TIMEOUT) < now(); } -static void remove_timeouts() // O(n) +static void remove_timeouts(void * ping) // O(n) { + PING * png = ping; size_t i, id; - size_t new_pos = pos_pings; - size_t new_num = num_pings; + size_t new_pos = png->pos_pings; + size_t new_num = png->num_pings; // Loop through buffer, oldest first - for (i = 0; i < num_pings; i++) { - id = (pos_pings + i) % PING_NUM_MAX; + for (i = 0; i < png->num_pings; i++) { + id = (png->pos_pings + i) % PING_NUM_MAX; - if (is_timeout(pings[id].timestamp)) { + if (is_timeout(png->pings[id].timestamp)) { new_pos++; new_num--; } @@ -60,47 +65,49 @@ static void remove_timeouts() // O(n) } } - num_pings = new_num; - pos_pings = new_pos % PING_NUM_MAX; + png->num_pings = new_num; + png->pos_pings = new_pos % PING_NUM_MAX; } -uint64_t add_ping(IP_Port ipp) // O(n) +uint64_t add_ping(void * ping, IP_Port ipp) // O(n) { + PING * png = ping; size_t p; - remove_timeouts(); + remove_timeouts(ping); // Remove oldest ping if full buffer - if (num_pings == PING_NUM_MAX) { - num_pings--; - pos_pings = (pos_pings + 1) % PING_NUM_MAX; + if (png->num_pings == PING_NUM_MAX) { + png->num_pings--; + png->pos_pings = (png->pos_pings + 1) % PING_NUM_MAX; } // Insert new ping at end of list - p = (pos_pings + num_pings) % PING_NUM_MAX; + p = (png->pos_pings + png->num_pings) % PING_NUM_MAX; - pings[p].ipp = ipp; - pings[p].timestamp = now(); - pings[p].id = random_64b(); + png->pings[p].ipp = ipp; + png->pings[p].timestamp = now(); + png->pings[p].id = random_64b(); - num_pings++; - return pings[p].id; + png->num_pings++; + return png->pings[p].id; } -bool is_pinging(IP_Port ipp, uint64_t ping_id) // O(n) TODO: replace this with something else. +bool is_pinging(void * ping, IP_Port ipp, uint64_t ping_id) // O(n) TODO: replace this with something else. { + PING * png = ping; if (ipp.ip.i == 0 && ping_id == 0) return false; size_t i, id; - remove_timeouts(); + remove_timeouts(ping); - for (i = 0; i < num_pings; i++) { - id = (pos_pings + i) % PING_NUM_MAX; + for (i = 0; i < png->num_pings; i++) { + id = (png->pos_pings + i) % PING_NUM_MAX; // ping_id = 0 means match any id - if ((ipp_eq(pings[id].ipp, ipp) || ipp.ip.i == 0) && (pings[id].id == ping_id || ping_id == 0)) { + if ((ipp_eq(png->pings[id].ipp, ipp) || ipp.ip.i == 0) && (png->pings[id].id == ping_id || ping_id == 0)) { return true; } } @@ -108,25 +115,25 @@ bool is_pinging(IP_Port ipp, uint64_t ping_id) // O(n) TODO: replace this with return false; } -int send_ping_request(IP_Port ipp, clientid_t *client_id) +int send_ping_request(void * ping, Net_Crypto *c, IP_Port ipp, clientid_t *client_id) { pingreq_t pk; int rc; uint64_t ping_id; - if (is_pinging(ipp, 0) || id_eq(client_id, (clientid_t *)temp_net_crypto->self_public_key)) + if (is_pinging(ping, ipp, 0) || id_eq(client_id, (clientid_t *)c->self_public_key)) return 1; // Generate random ping_id - ping_id = add_ping(ipp); + ping_id = add_ping(ping, ipp); pk.magic = PACKET_PING_REQ; - id_cpy(&pk.client_id, (clientid_t *)temp_net_crypto->self_public_key); // Our pubkey + id_cpy(&pk.client_id, (clientid_t *)c->self_public_key); // Our pubkey random_nonce((uint8_t *) &pk.nonce); // Generate random nonce // Encrypt ping_id using recipient privkey rc = encrypt_data((uint8_t *) client_id, - temp_net_crypto->self_secret_key, + c->self_secret_key, (uint8_t *) &pk.nonce, (uint8_t *) &ping_id, sizeof(ping_id), (uint8_t *) &pk.ping_id); @@ -134,24 +141,24 @@ int send_ping_request(IP_Port ipp, clientid_t *client_id) if (rc != sizeof(ping_id) + ENCRYPTION_PADDING) return 1; - return sendpacket(temp_net_crypto->lossless_udp->net->sock, ipp, (uint8_t *) &pk, sizeof(pk)); + return sendpacket(c->lossless_udp->net->sock, ipp, (uint8_t *) &pk, sizeof(pk)); } -int send_ping_response(IP_Port ipp, clientid_t *client_id, uint64_t ping_id) +int send_ping_response(Net_Crypto *c, IP_Port ipp, clientid_t *client_id, uint64_t ping_id) { pingres_t pk; int rc; - if (id_eq(client_id, (clientid_t *)temp_net_crypto->self_public_key)) + if (id_eq(client_id, (clientid_t *)c->self_public_key)) return 1; pk.magic = PACKET_PING_RES; - id_cpy(&pk.client_id, (clientid_t *)temp_net_crypto->self_public_key); // Our pubkey + id_cpy(&pk.client_id, (clientid_t *)c->self_public_key); // Our pubkey random_nonce((uint8_t *) &pk.nonce); // Generate random nonce // Encrypt ping_id using recipient privkey rc = encrypt_data((uint8_t *) client_id, - temp_net_crypto->self_secret_key, + c->self_secret_key, (uint8_t *) &pk.nonce, (uint8_t *) &ping_id, sizeof(ping_id), (uint8_t *) &pk.ping_id); @@ -159,7 +166,7 @@ int send_ping_response(IP_Port ipp, clientid_t *client_id, uint64_t ping_id) if (rc != sizeof(ping_id) + ENCRYPTION_PADDING) return 1; - return sendpacket(temp_net_crypto->lossless_udp->net->sock, ipp, (uint8_t *) &pk, sizeof(pk)); + return sendpacket(c->lossless_udp->net->sock, ipp, (uint8_t *) &pk, sizeof(pk)); } int handle_ping_request(void * object, IP_Port source, uint8_t *packet, uint32_t length) @@ -184,7 +191,7 @@ int handle_ping_request(void * object, IP_Port source, uint8_t *packet, uint32_t return 1; // Send response - send_ping_response(source, &p->client_id, ping_id); + send_ping_response(dht->c, source, &p->client_id, ping_id); add_toping(dht, (uint8_t *) &p->client_id, source); return 0; @@ -212,7 +219,7 @@ int handle_ping_response(void * object, IP_Port source, uint8_t *packet, uint32_ return 1; // Make sure ping_id is correct - if (!is_pinging(source, ping_id)) + if (!is_pinging(dht->ping, source, ping_id)) return 1; // Associate source ip with client_id diff --git a/core/ping.h b/core/ping.h index 6d5a0ea6..1f30392c 100644 --- a/core/ping.h +++ b/core/ping.h @@ -7,10 +7,11 @@ #include -void init_ping(); -uint64_t add_ping(IP_Port ipp); -bool is_pinging(IP_Port ipp, uint64_t ping_id); -int send_ping_request(IP_Port ipp, clientid_t *client_id); -int send_ping_response(IP_Port ipp, clientid_t *client_id, uint64_t ping_id); +void * new_ping(void); +void kill_ping(void * ping); +uint64_t add_ping(void * ping, IP_Port ipp); +bool is_pinging(void * ping, IP_Port ipp, uint64_t ping_id); +int send_ping_request(void * ping, Net_Crypto *c, IP_Port ipp, clientid_t *client_id); +int send_ping_response(Net_Crypto *c, IP_Port ipp, clientid_t *client_id, uint64_t ping_id); int handle_ping_request(void * object, IP_Port source, uint8_t *packet, uint32_t length); int handle_ping_response(void * object, IP_Port source, uint8_t *packet, uint32_t length); diff --git a/core/timer.c b/other/unused/timer.c similarity index 100% rename from core/timer.c rename to other/unused/timer.c diff --git a/core/timer.h b/other/unused/timer.h similarity index 100% rename from core/timer.h rename to other/unused/timer.h diff --git a/testing/toxic/chat.c b/testing/toxic/chat.c index 57404a59..d3e68ec6 100644 --- a/testing/toxic/chat.c +++ b/testing/toxic/chat.c @@ -342,7 +342,7 @@ void execute(ToxWindow *self, ChatContext *ctx, Messenger *m, char *cmd) wprintw(ctx->history, "Invalid command.\n"); } -static void chat_onDraw(ToxWindow *self) +static void chat_onDraw(ToxWindow *self, Messenger *m) { curs_set(1); int x, y; diff --git a/testing/toxic/dhtstatus.c b/testing/toxic/dhtstatus.c index c7a57c7d..6c9f2a80 100644 --- a/testing/toxic/dhtstatus.c +++ b/testing/toxic/dhtstatus.c @@ -34,9 +34,9 @@ static void dhtstatus_onKey(ToxWindow *self, Messenger *m, int key) } } -static void dhtstatus_onDraw(ToxWindow *self) +static void dhtstatus_onDraw(ToxWindow *self, Messenger *m) { - Client_data *close_clientlist = DHT_get_close_list(temp_DHT); + Client_data *close_clientlist = DHT_get_close_list(m->dht); curs_set(0); werase(self->window); diff --git a/testing/toxic/friendlist.c b/testing/toxic/friendlist.c index 0a58bc54..2e46f124 100644 --- a/testing/toxic/friendlist.c +++ b/testing/toxic/friendlist.c @@ -102,7 +102,7 @@ static void friendlist_onKey(ToxWindow *self, Messenger *m, int key) } } -static void friendlist_onDraw(ToxWindow *self) +static void friendlist_onDraw(ToxWindow *self, Messenger *m) { curs_set(0); werase(self->window); diff --git a/testing/toxic/prompt.c b/testing/toxic/prompt.c index 204cd7e6..12f8a201 100644 --- a/testing/toxic/prompt.c +++ b/testing/toxic/prompt.c @@ -460,7 +460,7 @@ static void prompt_onKey(ToxWindow *self, Messenger *m, int key) } } -static void prompt_onDraw(ToxWindow *self) +static void prompt_onDraw(ToxWindow *self, Messenger *m) { curs_set(1); int x, y; diff --git a/testing/toxic/windows.c b/testing/toxic/windows.c index c0ff3026..de924329 100644 --- a/testing/toxic/windows.c +++ b/testing/toxic/windows.c @@ -225,7 +225,7 @@ void draw_active_window(Messenger *m) prepare_window(a->window); a->blink = false; draw_bar(); - a->onDraw(a); + a->onDraw(a, m); /* Handle input */ int ch = getch(); diff --git a/testing/toxic/windows.h b/testing/toxic/windows.h index be5557e9..86917dbe 100644 --- a/testing/toxic/windows.h +++ b/testing/toxic/windows.h @@ -24,7 +24,7 @@ typedef struct ToxWindow_ ToxWindow; struct ToxWindow_ { void(*onKey)(ToxWindow *, Messenger *, int); - void(*onDraw)(ToxWindow *); + void(*onDraw)(ToxWindow *, Messenger *); void(*onInit)(ToxWindow *, Messenger *); void(*onFriendRequest)(ToxWindow *, uint8_t *, uint8_t *, uint16_t); void(*onMessage)(ToxWindow *, Messenger *, int, uint8_t *, uint16_t);