summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--core/DHT.c24
-rw-r--r--core/DHT.h2
-rw-r--r--core/Messenger.c18
-rw-r--r--core/Messenger.h20
-rw-r--r--core/net_crypto.c1
-rw-r--r--core/net_crypto.h2
-rw-r--r--core/ping.c95
-rw-r--r--core/ping.h11
-rw-r--r--other/unused/timer.c (renamed from core/timer.c)0
-rw-r--r--other/unused/timer.h (renamed from core/timer.h)0
-rw-r--r--testing/toxic/chat.c2
-rw-r--r--testing/toxic/dhtstatus.c4
-rw-r--r--testing/toxic/friendlist.c2
-rw-r--r--testing/toxic/prompt.c2
-rw-r--r--testing/toxic/windows.c2
-rw-r--r--testing/toxic/windows.h2
16 files changed, 92 insertions, 95 deletions
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
603 uint32_t i; 603 uint32_t i;
604 604
605 for (i = 0; i < num_nodes; ++i) { 605 for (i = 0; i < num_nodes; ++i) {
606 send_ping_request(nodes_list[i].ip_port, (clientid_t *) &nodes_list[i].client_id); 606 send_ping_request(dht->ping, dht->c, nodes_list[i].ip_port, (clientid_t *) &nodes_list[i].client_id);
607 returnedip_ports(dht, nodes_list[i].ip_port, nodes_list[i].client_id, packet + 1); 607 returnedip_ports(dht, nodes_list[i].ip_port, nodes_list[i].client_id, packet + 1);
608 } 608 }
609 609
@@ -709,7 +709,7 @@ static void do_DHT_friends(DHT * dht)
709 /* if node is not dead. */ 709 /* if node is not dead. */
710 if (!is_timeout(temp_time, dht->friends_list[i].client_list[j].timestamp, Kill_NODE_TIMEOUT)) { 710 if (!is_timeout(temp_time, dht->friends_list[i].client_list[j].timestamp, Kill_NODE_TIMEOUT)) {
711 if ((dht->friends_list[i].client_list[j].last_pinged + PING_INTERVAL) <= temp_time) { 711 if ((dht->friends_list[i].client_list[j].last_pinged + PING_INTERVAL) <= temp_time) {
712 send_ping_request( dht->friends_list[i].client_list[j].ip_port, 712 send_ping_request(dht->ping, dht->c, dht->friends_list[i].client_list[j].ip_port,
713 (clientid_t *) &dht->friends_list[i].client_list[j].client_id ); 713 (clientid_t *) &dht->friends_list[i].client_list[j].client_id );
714 dht->friends_list[i].client_list[j].last_pinged = temp_time; 714 dht->friends_list[i].client_list[j].last_pinged = temp_time;
715 } 715 }
@@ -747,7 +747,7 @@ static void do_Close(DHT * dht)
747 /* if node is not dead. */ 747 /* if node is not dead. */
748 if (!is_timeout(temp_time, dht->close_clientlist[i].timestamp, Kill_NODE_TIMEOUT)) { 748 if (!is_timeout(temp_time, dht->close_clientlist[i].timestamp, Kill_NODE_TIMEOUT)) {
749 if ((dht->close_clientlist[i].last_pinged + PING_INTERVAL) <= temp_time) { 749 if ((dht->close_clientlist[i].last_pinged + PING_INTERVAL) <= temp_time) {
750 send_ping_request( dht->close_clientlist[i].ip_port, 750 send_ping_request(dht->ping, dht->c, dht->close_clientlist[i].ip_port,
751 (clientid_t *) &dht->close_clientlist[i].client_id ); 751 (clientid_t *) &dht->close_clientlist[i].client_id );
752 dht->close_clientlist[i].last_pinged = temp_time; 752 dht->close_clientlist[i].last_pinged = temp_time;
753 } 753 }
@@ -772,8 +772,7 @@ static void do_Close(DHT * dht)
772void DHT_bootstrap(DHT * dht, IP_Port ip_port, uint8_t *public_key) 772void DHT_bootstrap(DHT * dht, IP_Port ip_port, uint8_t *public_key)
773{ 773{
774 getnodes(dht, ip_port, public_key, dht->c->self_public_key); 774 getnodes(dht, ip_port, public_key, dht->c->self_public_key);
775 //send_ping_request(dht, ip_port, (clientid_t *) public_key); 775 send_ping_request(dht->ping, dht->c, ip_port, (clientid_t *) public_key);
776 send_ping_request(ip_port, (clientid_t *) public_key);
777} 776}
778 777
779/* send the given packet to node with client_id 778/* 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
1039 /*TODO: improve port guessing algorithm*/ 1038 /*TODO: improve port guessing algorithm*/
1040 uint16_t port = port_list[(i / 2) % numports] + (i / (2 * numports)) * ((i % 2) ? -1 : 1); 1039 uint16_t port = port_list[(i / 2) % numports] + (i / (2 * numports)) * ((i % 2) ? -1 : 1);
1041 IP_Port pinging = {ip, htons(port)}; 1040 IP_Port pinging = {ip, htons(port)};
1042 //send_ping_request(dht, pinging, (clientid_t *) &dht->friends_list[friend_num].client_id); 1041 send_ping_request(dht->ping, dht->c, pinging, (clientid_t *) &dht->friends_list[friend_num].client_id);
1043 send_ping_request(pinging, (clientid_t *) &dht->friends_list[friend_num].client_id);
1044 } 1042 }
1045 1043
1046 dht->friends_list[friend_num].punching_index = i; 1044 dht->friends_list[friend_num].punching_index = i;
@@ -1139,8 +1137,7 @@ static void do_toping(DHT * dht)
1139 if (dht->toping[i].ip_port.ip.i == 0) 1137 if (dht->toping[i].ip_port.ip.i == 0)
1140 return; 1138 return;
1141 1139
1142 //send_ping_request(dht, dht->toping[i].ip_port, (clientid_t *) dht->toping[i].client_id); 1140 send_ping_request(dht->ping, dht->c, dht->toping[i].ip_port, (clientid_t *) dht->toping[i].client_id);
1143 send_ping_request(dht->toping[i].ip_port, (clientid_t *) dht->toping[i].client_id);
1144 dht->toping[i].ip_port.ip.i = 0; 1141 dht->toping[i].ip_port.ip.i = 0;
1145 } 1142 }
1146} 1143}
@@ -1151,13 +1148,17 @@ DHT * new_DHT(Net_Crypto *c)
1151 DHT * temp = calloc(1, sizeof(DHT)); 1148 DHT * temp = calloc(1, sizeof(DHT));
1152 if (temp == NULL) 1149 if (temp == NULL)
1153 return NULL; 1150 return NULL;
1151 temp->ping = new_ping();
1152 if (temp->ping == NULL){
1153 kill_DHT(temp);
1154 return NULL;
1155 }
1154 temp->c = c; 1156 temp->c = c;
1155 networking_registerhandler(c->lossless_udp->net, 0, &handle_ping_request, temp); 1157 networking_registerhandler(c->lossless_udp->net, 0, &handle_ping_request, temp);
1156 networking_registerhandler(c->lossless_udp->net, 1, &handle_ping_response, temp); 1158 networking_registerhandler(c->lossless_udp->net, 1, &handle_ping_response, temp);
1157 networking_registerhandler(c->lossless_udp->net, 2, &handle_getnodes, temp); 1159 networking_registerhandler(c->lossless_udp->net, 2, &handle_getnodes, temp);
1158 networking_registerhandler(c->lossless_udp->net, 3, &handle_sendnodes, temp); 1160 networking_registerhandler(c->lossless_udp->net, 3, &handle_sendnodes, temp);
1159 cryptopacket_registerhandler(c, 254, &handle_NATping, temp); 1161 cryptopacket_registerhandler(c, 254, &handle_NATping, temp);
1160 temp_DHT = temp;
1161 return temp; 1162 return temp;
1162} 1163}
1163 1164
@@ -1170,6 +1171,7 @@ void do_DHT(DHT * dht)
1170} 1171}
1171void kill_DHT(DHT * dht) 1172void kill_DHT(DHT * dht)
1172{ 1173{
1174 kill_ping(dht->ping);
1173 free(dht->friends_list); 1175 free(dht->friends_list);
1174 free(dht); 1176 free(dht);
1175} 1177}
@@ -1193,8 +1195,6 @@ void DHT_save(DHT * dht, uint8_t *data)
1193 */ 1195 */
1194int DHT_load(DHT * dht, uint8_t *data, uint32_t size) 1196int DHT_load(DHT * dht, uint8_t *data, uint32_t size)
1195{ 1197{
1196 init_ping();
1197
1198 if (size < sizeof(dht->close_clientlist)) 1198 if (size < sizeof(dht->close_clientlist))
1199 return -1; 1199 return -1;
1200 1200
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 {
100 Node_format toping[MAX_TOPING]; 100 Node_format toping[MAX_TOPING];
101 uint64_t last_toping; 101 uint64_t last_toping;
102 uint64_t close_lastgetnodes; 102 uint64_t close_lastgetnodes;
103 void * ping;
103} DHT; 104} DHT;
104/*----------------------------------------------------------------------------------*/ 105/*----------------------------------------------------------------------------------*/
105DHT * temp_DHT; //TODO: remove
106 106
107Client_data *DHT_get_close_list(DHT * dht); 107Client_data *DHT_get_close_list(DHT * dht);
108 108
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 @@
22 */ 22 */
23 23
24#include "Messenger.h" 24#include "Messenger.h"
25#include "timer.h"
26 25
27#define MIN(a,b) (((a)<(b))?(a):(b)) 26#define MIN(a,b) (((a)<(b))?(a):(b))
28 27
@@ -608,17 +607,17 @@ int write_cryptpacket_id(Messenger *m, int friendnumber, uint8_t packet_id, uint
608 return write_cryptpacket(m->net_crypto, m->friendlist[friendnumber].crypt_connection_id, packet, length + 1); 607 return write_cryptpacket(m->net_crypto, m->friendlist[friendnumber].crypt_connection_id, packet, length + 1);
609} 608}
610 609
611
612/*Interval in seconds between LAN discovery packet sending*/ 610/*Interval in seconds between LAN discovery packet sending*/
613#define LAN_DISCOVERY_INTERVAL 60 611#define LAN_DISCOVERY_INTERVAL 60
614#define PORT 33445 612#define PORT 33445
615 613
616/*Send a LAN discovery packet every LAN_DISCOVERY_INTERVAL seconds*/ 614/*Send a LAN discovery packet every LAN_DISCOVERY_INTERVAL seconds*/
617int LANdiscovery(timer *t, void *arg) 615static void LANdiscovery(Messenger *m)
618{ 616{
619 send_LANdiscovery(htons(PORT), temp_net_crypto); 617 if (m->last_LANdiscovery + LAN_DISCOVERY_INTERVAL < unix_time()) {
620 timer_start(t, LAN_DISCOVERY_INTERVAL); 618 send_LANdiscovery(htons(PORT), m->net_crypto);
621 return 0; 619 m->last_LANdiscovery = unix_time();
620 }
622} 621}
623 622
624/* run this at startup */ 623/* run this at startup */
@@ -655,9 +654,6 @@ Messenger *initMessenger(void)
655 set_nospam(&(m->fr), random_int()); 654 set_nospam(&(m->fr), random_int());
656 init_cryptopackets(m->dht); 655 init_cryptopackets(m->dht);
657 656
658 send_LANdiscovery(htons(PORT), m->net_crypto);
659 timer_single(&LANdiscovery, 0, LAN_DISCOVERY_INTERVAL);
660
661 return m; 657 return m;
662} 658}
663 659
@@ -885,7 +881,6 @@ void doInbound(Messenger *m)
885 } 881 }
886} 882}
887 883
888
889/* the main loop that needs to be run at least 20 times per second. */ 884/* the main loop that needs to be run at least 20 times per second. */
890void doMessenger(Messenger *m) 885void doMessenger(Messenger *m)
891{ 886{
@@ -895,8 +890,7 @@ void doMessenger(Messenger *m)
895 do_net_crypto(m->net_crypto); 890 do_net_crypto(m->net_crypto);
896 doInbound(m); 891 doInbound(m);
897 doFriends(m); 892 doFriends(m);
898 893 LANdiscovery(m);
899 timer_poll();
900} 894}
901 895
902/* returns the size of the messenger data (for saving) */ 896/* 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 {
128 Friend *friendlist; 128 Friend *friendlist;
129 uint32_t numfriends; 129 uint32_t numfriends;
130 130
131 uint64_t last_LANdiscovery;
132
131 void (*friend_message)(struct Messenger *m, int, uint8_t *, uint16_t, void *); 133 void (*friend_message)(struct Messenger *m, int, uint8_t *, uint16_t, void *);
132 void *friend_message_userdata; 134 void *friend_message_userdata;
133 void (*friend_action)(struct Messenger *m, int, uint8_t *, uint16_t, void *); 135 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
222 return -1 if failure */ 224 return -1 if failure */
223int setname(Messenger *m, uint8_t *name, uint16_t length); 225int setname(Messenger *m, uint8_t *name, uint16_t length);
224 226
225/** 227/*
226 * @brief Get your nickname. 228 Get your nickname.
227 * 229 m The messanger context to use.
228 * @param[in] m The messanger context to use. 230 name Pointer to a string for the name.
229 * 231 nlen The length of the string buffer.
230 * @param[inout] name Pointer to a string for the name. 232 returns Return the length of the name, 0 on error.
231 * 233*/
232 * @param[in] nlen The length of the string buffer.
233 *
234 * @return Return the length of the name, 0 on error.
235 */
236uint16_t getself_name(Messenger *m, uint8_t *name, uint16_t nlen); 234uint16_t getself_name(Messenger *m, uint8_t *name, uint16_t nlen);
237 235
238/* get name of friendnumber 236/* 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)
706 if (temp->lossless_udp == NULL) 706 if (temp->lossless_udp == NULL)
707 return NULL; 707 return NULL;
708 memset(temp->incoming_connections, -1 , sizeof(int) * MAX_INCOMING); 708 memset(temp->incoming_connections, -1 , sizeof(int) * MAX_INCOMING);
709 temp_net_crypto = temp; //TODO remove
710 return temp; 709 return temp;
711} 710}
712 711
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 {
73 73
74#include "DHT.h" 74#include "DHT.h"
75 75
76Net_Crypto * temp_net_crypto; //TODO: remove this
77
78#define ENCRYPTION_PADDING (crypto_box_ZEROBYTES - crypto_box_BOXZEROBYTES) 76#define ENCRYPTION_PADDING (crypto_box_ZEROBYTES - crypto_box_BOXZEROBYTES)
79 77
80/* returns zero if the buffer contains only zeros */ 78/* 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 {
23 uint64_t timestamp; 23 uint64_t timestamp;
24} pinged_t; 24} pinged_t;
25 25
26static pinged_t pings[PING_NUM_MAX]; 26typedef struct {
27static size_t num_pings; 27 pinged_t pings[PING_NUM_MAX];
28static size_t pos_pings; 28 size_t num_pings;
29 size_t pos_pings;
30} PING;
29 31
30extern uint8_t self_secret_key[crypto_box_SECRETKEYBYTES]; // DHT.c 32void * new_ping(void)
33{
34 return calloc(1, sizeof(PING));
35}
31 36
32void init_ping() 37void kill_ping(void * ping)
33{ 38{
34 num_pings = 0; 39 free(ping);
35 pos_pings = 0;
36} 40}
37 41
38static bool is_timeout(uint64_t time) 42static bool is_timeout(uint64_t time)
@@ -40,17 +44,18 @@ static bool is_timeout(uint64_t time)
40 return (time + PING_TIMEOUT) < now(); 44 return (time + PING_TIMEOUT) < now();
41} 45}
42 46
43static void remove_timeouts() // O(n) 47static void remove_timeouts(void * ping) // O(n)
44{ 48{
49 PING * png = ping;
45 size_t i, id; 50 size_t i, id;
46 size_t new_pos = pos_pings; 51 size_t new_pos = png->pos_pings;
47 size_t new_num = num_pings; 52 size_t new_num = png->num_pings;
48 53
49 // Loop through buffer, oldest first 54 // Loop through buffer, oldest first
50 for (i = 0; i < num_pings; i++) { 55 for (i = 0; i < png->num_pings; i++) {
51 id = (pos_pings + i) % PING_NUM_MAX; 56 id = (png->pos_pings + i) % PING_NUM_MAX;
52 57
53 if (is_timeout(pings[id].timestamp)) { 58 if (is_timeout(png->pings[id].timestamp)) {
54 new_pos++; 59 new_pos++;
55 new_num--; 60 new_num--;
56 } 61 }
@@ -60,47 +65,49 @@ static void remove_timeouts() // O(n)
60 } 65 }
61 } 66 }
62 67
63 num_pings = new_num; 68 png->num_pings = new_num;
64 pos_pings = new_pos % PING_NUM_MAX; 69 png->pos_pings = new_pos % PING_NUM_MAX;
65} 70}
66 71
67uint64_t add_ping(IP_Port ipp) // O(n) 72uint64_t add_ping(void * ping, IP_Port ipp) // O(n)
68{ 73{
74 PING * png = ping;
69 size_t p; 75 size_t p;
70 76
71 remove_timeouts(); 77 remove_timeouts(ping);
72 78
73 // Remove oldest ping if full buffer 79 // Remove oldest ping if full buffer
74 if (num_pings == PING_NUM_MAX) { 80 if (png->num_pings == PING_NUM_MAX) {
75 num_pings--; 81 png->num_pings--;
76 pos_pings = (pos_pings + 1) % PING_NUM_MAX; 82 png->pos_pings = (png->pos_pings + 1) % PING_NUM_MAX;
77 } 83 }
78 84
79 // Insert new ping at end of list 85 // Insert new ping at end of list
80 p = (pos_pings + num_pings) % PING_NUM_MAX; 86 p = (png->pos_pings + png->num_pings) % PING_NUM_MAX;
81 87
82 pings[p].ipp = ipp; 88 png->pings[p].ipp = ipp;
83 pings[p].timestamp = now(); 89 png->pings[p].timestamp = now();
84 pings[p].id = random_64b(); 90 png->pings[p].id = random_64b();
85 91
86 num_pings++; 92 png->num_pings++;
87 return pings[p].id; 93 return png->pings[p].id;
88} 94}
89 95
90bool is_pinging(IP_Port ipp, uint64_t ping_id) // O(n) TODO: replace this with something else. 96bool is_pinging(void * ping, IP_Port ipp, uint64_t ping_id) // O(n) TODO: replace this with something else.
91{ 97{
98 PING * png = ping;
92 if (ipp.ip.i == 0 && ping_id == 0) 99 if (ipp.ip.i == 0 && ping_id == 0)
93 return false; 100 return false;
94 101
95 size_t i, id; 102 size_t i, id;
96 103
97 remove_timeouts(); 104 remove_timeouts(ping);
98 105
99 for (i = 0; i < num_pings; i++) { 106 for (i = 0; i < png->num_pings; i++) {
100 id = (pos_pings + i) % PING_NUM_MAX; 107 id = (png->pos_pings + i) % PING_NUM_MAX;
101 108
102 // ping_id = 0 means match any id 109 // ping_id = 0 means match any id
103 if ((ipp_eq(pings[id].ipp, ipp) || ipp.ip.i == 0) && (pings[id].id == ping_id || ping_id == 0)) { 110 if ((ipp_eq(png->pings[id].ipp, ipp) || ipp.ip.i == 0) && (png->pings[id].id == ping_id || ping_id == 0)) {
104 return true; 111 return true;
105 } 112 }
106 } 113 }
@@ -108,25 +115,25 @@ bool is_pinging(IP_Port ipp, uint64_t ping_id) // O(n) TODO: replace this with
108 return false; 115 return false;
109} 116}
110 117
111int send_ping_request(IP_Port ipp, clientid_t *client_id) 118int send_ping_request(void * ping, Net_Crypto *c, IP_Port ipp, clientid_t *client_id)
112{ 119{
113 pingreq_t pk; 120 pingreq_t pk;
114 int rc; 121 int rc;
115 uint64_t ping_id; 122 uint64_t ping_id;
116 123
117 if (is_pinging(ipp, 0) || id_eq(client_id, (clientid_t *)temp_net_crypto->self_public_key)) 124 if (is_pinging(ping, ipp, 0) || id_eq(client_id, (clientid_t *)c->self_public_key))
118 return 1; 125 return 1;
119 126
120 // Generate random ping_id 127 // Generate random ping_id
121 ping_id = add_ping(ipp); 128 ping_id = add_ping(ping, ipp);
122 129
123 pk.magic = PACKET_PING_REQ; 130 pk.magic = PACKET_PING_REQ;
124 id_cpy(&pk.client_id, (clientid_t *)temp_net_crypto->self_public_key); // Our pubkey 131 id_cpy(&pk.client_id, (clientid_t *)c->self_public_key); // Our pubkey
125 random_nonce((uint8_t *) &pk.nonce); // Generate random nonce 132 random_nonce((uint8_t *) &pk.nonce); // Generate random nonce
126 133
127 // Encrypt ping_id using recipient privkey 134 // Encrypt ping_id using recipient privkey
128 rc = encrypt_data((uint8_t *) client_id, 135 rc = encrypt_data((uint8_t *) client_id,
129 temp_net_crypto->self_secret_key, 136 c->self_secret_key,
130 (uint8_t *) &pk.nonce, 137 (uint8_t *) &pk.nonce,
131 (uint8_t *) &ping_id, sizeof(ping_id), 138 (uint8_t *) &ping_id, sizeof(ping_id),
132 (uint8_t *) &pk.ping_id); 139 (uint8_t *) &pk.ping_id);
@@ -134,24 +141,24 @@ int send_ping_request(IP_Port ipp, clientid_t *client_id)
134 if (rc != sizeof(ping_id) + ENCRYPTION_PADDING) 141 if (rc != sizeof(ping_id) + ENCRYPTION_PADDING)
135 return 1; 142 return 1;
136 143
137 return sendpacket(temp_net_crypto->lossless_udp->net->sock, ipp, (uint8_t *) &pk, sizeof(pk)); 144 return sendpacket(c->lossless_udp->net->sock, ipp, (uint8_t *) &pk, sizeof(pk));
138} 145}
139 146
140int send_ping_response(IP_Port ipp, clientid_t *client_id, uint64_t ping_id) 147int send_ping_response(Net_Crypto *c, IP_Port ipp, clientid_t *client_id, uint64_t ping_id)
141{ 148{
142 pingres_t pk; 149 pingres_t pk;
143 int rc; 150 int rc;
144 151
145 if (id_eq(client_id, (clientid_t *)temp_net_crypto->self_public_key)) 152 if (id_eq(client_id, (clientid_t *)c->self_public_key))
146 return 1; 153 return 1;
147 154
148 pk.magic = PACKET_PING_RES; 155 pk.magic = PACKET_PING_RES;
149 id_cpy(&pk.client_id, (clientid_t *)temp_net_crypto->self_public_key); // Our pubkey 156 id_cpy(&pk.client_id, (clientid_t *)c->self_public_key); // Our pubkey
150 random_nonce((uint8_t *) &pk.nonce); // Generate random nonce 157 random_nonce((uint8_t *) &pk.nonce); // Generate random nonce
151 158
152 // Encrypt ping_id using recipient privkey 159 // Encrypt ping_id using recipient privkey
153 rc = encrypt_data((uint8_t *) client_id, 160 rc = encrypt_data((uint8_t *) client_id,
154 temp_net_crypto->self_secret_key, 161 c->self_secret_key,
155 (uint8_t *) &pk.nonce, 162 (uint8_t *) &pk.nonce,
156 (uint8_t *) &ping_id, sizeof(ping_id), 163 (uint8_t *) &ping_id, sizeof(ping_id),
157 (uint8_t *) &pk.ping_id); 164 (uint8_t *) &pk.ping_id);
@@ -159,7 +166,7 @@ int send_ping_response(IP_Port ipp, clientid_t *client_id, uint64_t ping_id)
159 if (rc != sizeof(ping_id) + ENCRYPTION_PADDING) 166 if (rc != sizeof(ping_id) + ENCRYPTION_PADDING)
160 return 1; 167 return 1;
161 168
162 return sendpacket(temp_net_crypto->lossless_udp->net->sock, ipp, (uint8_t *) &pk, sizeof(pk)); 169 return sendpacket(c->lossless_udp->net->sock, ipp, (uint8_t *) &pk, sizeof(pk));
163} 170}
164 171
165int handle_ping_request(void * object, IP_Port source, uint8_t *packet, uint32_t length) 172int 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
184 return 1; 191 return 1;
185 192
186 // Send response 193 // Send response
187 send_ping_response(source, &p->client_id, ping_id); 194 send_ping_response(dht->c, source, &p->client_id, ping_id);
188 add_toping(dht, (uint8_t *) &p->client_id, source); 195 add_toping(dht, (uint8_t *) &p->client_id, source);
189 196
190 return 0; 197 return 0;
@@ -212,7 +219,7 @@ int handle_ping_response(void * object, IP_Port source, uint8_t *packet, uint32_
212 return 1; 219 return 1;
213 220
214 // Make sure ping_id is correct 221 // Make sure ping_id is correct
215 if (!is_pinging(source, ping_id)) 222 if (!is_pinging(dht->ping, source, ping_id))
216 return 1; 223 return 1;
217 224
218 // Associate source ip with client_id 225 // 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 @@
7 7
8#include <stdbool.h> 8#include <stdbool.h>
9 9
10void init_ping(); 10void * new_ping(void);
11uint64_t add_ping(IP_Port ipp); 11void kill_ping(void * ping);
12bool is_pinging(IP_Port ipp, uint64_t ping_id); 12uint64_t add_ping(void * ping, IP_Port ipp);
13int send_ping_request(IP_Port ipp, clientid_t *client_id); 13bool is_pinging(void * ping, IP_Port ipp, uint64_t ping_id);
14int send_ping_response(IP_Port ipp, clientid_t *client_id, uint64_t ping_id); 14int send_ping_request(void * ping, Net_Crypto *c, IP_Port ipp, clientid_t *client_id);
15int send_ping_response(Net_Crypto *c, IP_Port ipp, clientid_t *client_id, uint64_t ping_id);
15int handle_ping_request(void * object, IP_Port source, uint8_t *packet, uint32_t length); 16int handle_ping_request(void * object, IP_Port source, uint8_t *packet, uint32_t length);
16int handle_ping_response(void * object, IP_Port source, uint8_t *packet, uint32_t length); 17int 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
index 29190921..29190921 100644
--- a/core/timer.c
+++ b/other/unused/timer.c
diff --git a/core/timer.h b/other/unused/timer.h
index 15491326..15491326 100644
--- a/core/timer.h
+++ b/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)
342 wprintw(ctx->history, "Invalid command.\n"); 342 wprintw(ctx->history, "Invalid command.\n");
343} 343}
344 344
345static void chat_onDraw(ToxWindow *self) 345static void chat_onDraw(ToxWindow *self, Messenger *m)
346{ 346{
347 curs_set(1); 347 curs_set(1);
348 int x, y; 348 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)
34 } 34 }
35} 35}
36 36
37static void dhtstatus_onDraw(ToxWindow *self) 37static void dhtstatus_onDraw(ToxWindow *self, Messenger *m)
38{ 38{
39 Client_data *close_clientlist = DHT_get_close_list(temp_DHT); 39 Client_data *close_clientlist = DHT_get_close_list(m->dht);
40 curs_set(0); 40 curs_set(0);
41 werase(self->window); 41 werase(self->window);
42 42
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)
102 } 102 }
103} 103}
104 104
105static void friendlist_onDraw(ToxWindow *self) 105static void friendlist_onDraw(ToxWindow *self, Messenger *m)
106{ 106{
107 curs_set(0); 107 curs_set(0);
108 werase(self->window); 108 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)
460 } 460 }
461} 461}
462 462
463static void prompt_onDraw(ToxWindow *self) 463static void prompt_onDraw(ToxWindow *self, Messenger *m)
464{ 464{
465 curs_set(1); 465 curs_set(1);
466 int x, y; 466 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)
225 prepare_window(a->window); 225 prepare_window(a->window);
226 a->blink = false; 226 a->blink = false;
227 draw_bar(); 227 draw_bar();
228 a->onDraw(a); 228 a->onDraw(a, m);
229 229
230 /* Handle input */ 230 /* Handle input */
231 int ch = getch(); 231 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;
24 24
25struct ToxWindow_ { 25struct ToxWindow_ {
26 void(*onKey)(ToxWindow *, Messenger *, int); 26 void(*onKey)(ToxWindow *, Messenger *, int);
27 void(*onDraw)(ToxWindow *); 27 void(*onDraw)(ToxWindow *, Messenger *);
28 void(*onInit)(ToxWindow *, Messenger *); 28 void(*onInit)(ToxWindow *, Messenger *);
29 void(*onFriendRequest)(ToxWindow *, uint8_t *, uint8_t *, uint16_t); 29 void(*onFriendRequest)(ToxWindow *, uint8_t *, uint8_t *, uint16_t);
30 void(*onMessage)(ToxWindow *, Messenger *, int, uint8_t *, uint16_t); 30 void(*onMessage)(ToxWindow *, Messenger *, int, uint8_t *, uint16_t);