mirror of
https://github.com/irungentoo/toxcore.git
synced 2024-03-22 13:30:51 +08:00
Refactor of core done.
This commit is contained in:
parent
a1c40d753e
commit
ac621d96d3
24
core/DHT.c
24
core/DHT.c
|
@ -603,7 +603,7 @@ static int handle_sendnodes(void * object, IP_Port source, uint8_t *packet, uint
|
||||||
uint32_t i;
|
uint32_t i;
|
||||||
|
|
||||||
for (i = 0; i < num_nodes; ++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);
|
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 node is not dead. */
|
||||||
if (!is_timeout(temp_time, dht->friends_list[i].client_list[j].timestamp, Kill_NODE_TIMEOUT)) {
|
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) {
|
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 );
|
(clientid_t *) &dht->friends_list[i].client_list[j].client_id );
|
||||||
dht->friends_list[i].client_list[j].last_pinged = temp_time;
|
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 node is not dead. */
|
||||||
if (!is_timeout(temp_time, dht->close_clientlist[i].timestamp, Kill_NODE_TIMEOUT)) {
|
if (!is_timeout(temp_time, dht->close_clientlist[i].timestamp, Kill_NODE_TIMEOUT)) {
|
||||||
if ((dht->close_clientlist[i].last_pinged + PING_INTERVAL) <= temp_time) {
|
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 );
|
(clientid_t *) &dht->close_clientlist[i].client_id );
|
||||||
dht->close_clientlist[i].last_pinged = temp_time;
|
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)
|
void DHT_bootstrap(DHT * dht, IP_Port ip_port, uint8_t *public_key)
|
||||||
{
|
{
|
||||||
getnodes(dht, ip_port, public_key, dht->c->self_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(dht->ping, dht->c, ip_port, (clientid_t *) public_key);
|
||||||
send_ping_request(ip_port, (clientid_t *) public_key);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* send the given packet to node with client_id
|
/* 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*/
|
/*TODO: improve port guessing algorithm*/
|
||||||
uint16_t port = port_list[(i / 2) % numports] + (i / (2 * numports)) * ((i % 2) ? -1 : 1);
|
uint16_t port = port_list[(i / 2) % numports] + (i / (2 * numports)) * ((i % 2) ? -1 : 1);
|
||||||
IP_Port pinging = {ip, htons(port)};
|
IP_Port pinging = {ip, htons(port)};
|
||||||
//send_ping_request(dht, 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);
|
||||||
send_ping_request(pinging, (clientid_t *) &dht->friends_list[friend_num].client_id);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
dht->friends_list[friend_num].punching_index = i;
|
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)
|
if (dht->toping[i].ip_port.ip.i == 0)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
//send_ping_request(dht, 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);
|
||||||
send_ping_request(dht->toping[i].ip_port, (clientid_t *) dht->toping[i].client_id);
|
|
||||||
dht->toping[i].ip_port.ip.i = 0;
|
dht->toping[i].ip_port.ip.i = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1151,13 +1148,17 @@ DHT * new_DHT(Net_Crypto *c)
|
||||||
DHT * temp = calloc(1, sizeof(DHT));
|
DHT * temp = calloc(1, sizeof(DHT));
|
||||||
if (temp == NULL)
|
if (temp == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
temp->ping = new_ping();
|
||||||
|
if (temp->ping == NULL){
|
||||||
|
kill_DHT(temp);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
temp->c = c;
|
temp->c = c;
|
||||||
networking_registerhandler(c->lossless_udp->net, 0, &handle_ping_request, temp);
|
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, 1, &handle_ping_response, temp);
|
||||||
networking_registerhandler(c->lossless_udp->net, 2, &handle_getnodes, temp);
|
networking_registerhandler(c->lossless_udp->net, 2, &handle_getnodes, temp);
|
||||||
networking_registerhandler(c->lossless_udp->net, 3, &handle_sendnodes, temp);
|
networking_registerhandler(c->lossless_udp->net, 3, &handle_sendnodes, temp);
|
||||||
cryptopacket_registerhandler(c, 254, &handle_NATping, temp);
|
cryptopacket_registerhandler(c, 254, &handle_NATping, temp);
|
||||||
temp_DHT = temp;
|
|
||||||
return temp;
|
return temp;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1170,6 +1171,7 @@ void do_DHT(DHT * dht)
|
||||||
}
|
}
|
||||||
void kill_DHT(DHT * dht)
|
void kill_DHT(DHT * dht)
|
||||||
{
|
{
|
||||||
|
kill_ping(dht->ping);
|
||||||
free(dht->friends_list);
|
free(dht->friends_list);
|
||||||
free(dht);
|
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)
|
int DHT_load(DHT * dht, uint8_t *data, uint32_t size)
|
||||||
{
|
{
|
||||||
init_ping();
|
|
||||||
|
|
||||||
if (size < sizeof(dht->close_clientlist))
|
if (size < sizeof(dht->close_clientlist))
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
|
|
|
@ -100,9 +100,9 @@ typedef struct {
|
||||||
Node_format toping[MAX_TOPING];
|
Node_format toping[MAX_TOPING];
|
||||||
uint64_t last_toping;
|
uint64_t last_toping;
|
||||||
uint64_t close_lastgetnodes;
|
uint64_t close_lastgetnodes;
|
||||||
|
void * ping;
|
||||||
} DHT;
|
} DHT;
|
||||||
/*----------------------------------------------------------------------------------*/
|
/*----------------------------------------------------------------------------------*/
|
||||||
DHT * temp_DHT; //TODO: remove
|
|
||||||
|
|
||||||
Client_data *DHT_get_close_list(DHT * dht);
|
Client_data *DHT_get_close_list(DHT * dht);
|
||||||
|
|
||||||
|
|
|
@ -22,7 +22,6 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "Messenger.h"
|
#include "Messenger.h"
|
||||||
#include "timer.h"
|
|
||||||
|
|
||||||
#define MIN(a,b) (((a)<(b))?(a):(b))
|
#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);
|
return write_cryptpacket(m->net_crypto, m->friendlist[friendnumber].crypt_connection_id, packet, length + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*Interval in seconds between LAN discovery packet sending*/
|
/*Interval in seconds between LAN discovery packet sending*/
|
||||||
#define LAN_DISCOVERY_INTERVAL 60
|
#define LAN_DISCOVERY_INTERVAL 60
|
||||||
#define PORT 33445
|
#define PORT 33445
|
||||||
|
|
||||||
/*Send a LAN discovery packet every LAN_DISCOVERY_INTERVAL seconds*/
|
/*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);
|
if (m->last_LANdiscovery + LAN_DISCOVERY_INTERVAL < unix_time()) {
|
||||||
timer_start(t, LAN_DISCOVERY_INTERVAL);
|
send_LANdiscovery(htons(PORT), m->net_crypto);
|
||||||
return 0;
|
m->last_LANdiscovery = unix_time();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* run this at startup */
|
/* run this at startup */
|
||||||
|
@ -655,9 +654,6 @@ Messenger *initMessenger(void)
|
||||||
set_nospam(&(m->fr), random_int());
|
set_nospam(&(m->fr), random_int());
|
||||||
init_cryptopackets(m->dht);
|
init_cryptopackets(m->dht);
|
||||||
|
|
||||||
send_LANdiscovery(htons(PORT), m->net_crypto);
|
|
||||||
timer_single(&LANdiscovery, 0, LAN_DISCOVERY_INTERVAL);
|
|
||||||
|
|
||||||
return m;
|
return m;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -885,7 +881,6 @@ void doInbound(Messenger *m)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* the main loop that needs to be run at least 20 times per second. */
|
/* the main loop that needs to be run at least 20 times per second. */
|
||||||
void doMessenger(Messenger *m)
|
void doMessenger(Messenger *m)
|
||||||
{
|
{
|
||||||
|
@ -895,8 +890,7 @@ void doMessenger(Messenger *m)
|
||||||
do_net_crypto(m->net_crypto);
|
do_net_crypto(m->net_crypto);
|
||||||
doInbound(m);
|
doInbound(m);
|
||||||
doFriends(m);
|
doFriends(m);
|
||||||
|
LANdiscovery(m);
|
||||||
timer_poll();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* returns the size of the messenger data (for saving) */
|
/* returns the size of the messenger data (for saving) */
|
||||||
|
|
|
@ -128,6 +128,8 @@ typedef struct Messenger {
|
||||||
Friend *friendlist;
|
Friend *friendlist;
|
||||||
uint32_t numfriends;
|
uint32_t numfriends;
|
||||||
|
|
||||||
|
uint64_t last_LANdiscovery;
|
||||||
|
|
||||||
void (*friend_message)(struct Messenger *m, int, uint8_t *, uint16_t, void *);
|
void (*friend_message)(struct Messenger *m, int, uint8_t *, uint16_t, void *);
|
||||||
void *friend_message_userdata;
|
void *friend_message_userdata;
|
||||||
void (*friend_action)(struct Messenger *m, int, uint8_t *, uint16_t, void *);
|
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 */
|
return -1 if failure */
|
||||||
int setname(Messenger *m, uint8_t *name, uint16_t length);
|
int setname(Messenger *m, uint8_t *name, uint16_t length);
|
||||||
|
|
||||||
/**
|
/*
|
||||||
* @brief Get your nickname.
|
Get your nickname.
|
||||||
*
|
m The messanger context to use.
|
||||||
* @param[in] m The messanger context to use.
|
name Pointer to a string for the name.
|
||||||
*
|
nlen The length of the string buffer.
|
||||||
* @param[inout] name Pointer to a string for the name.
|
returns Return the length of the name, 0 on error.
|
||||||
*
|
*/
|
||||||
* @param[in] nlen The length of the string buffer.
|
|
||||||
*
|
|
||||||
* @return Return the length of the name, 0 on error.
|
|
||||||
*/
|
|
||||||
uint16_t getself_name(Messenger *m, uint8_t *name, uint16_t nlen);
|
uint16_t getself_name(Messenger *m, uint8_t *name, uint16_t nlen);
|
||||||
|
|
||||||
/* get name of friendnumber
|
/* get name of friendnumber
|
||||||
|
|
|
@ -706,7 +706,6 @@ Net_Crypto * new_net_crypto(Networking_Core * net)
|
||||||
if (temp->lossless_udp == NULL)
|
if (temp->lossless_udp == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
memset(temp->incoming_connections, -1 , sizeof(int) * MAX_INCOMING);
|
memset(temp->incoming_connections, -1 , sizeof(int) * MAX_INCOMING);
|
||||||
temp_net_crypto = temp; //TODO remove
|
|
||||||
return temp;
|
return temp;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -73,8 +73,6 @@ typedef struct {
|
||||||
|
|
||||||
#include "DHT.h"
|
#include "DHT.h"
|
||||||
|
|
||||||
Net_Crypto * temp_net_crypto; //TODO: remove this
|
|
||||||
|
|
||||||
#define ENCRYPTION_PADDING (crypto_box_ZEROBYTES - crypto_box_BOXZEROBYTES)
|
#define ENCRYPTION_PADDING (crypto_box_ZEROBYTES - crypto_box_BOXZEROBYTES)
|
||||||
|
|
||||||
/* returns zero if the buffer contains only zeros */
|
/* returns zero if the buffer contains only zeros */
|
||||||
|
|
97
core/ping.c
97
core/ping.c
|
@ -23,16 +23,20 @@ typedef struct {
|
||||||
uint64_t timestamp;
|
uint64_t timestamp;
|
||||||
} pinged_t;
|
} pinged_t;
|
||||||
|
|
||||||
static pinged_t pings[PING_NUM_MAX];
|
typedef struct {
|
||||||
static size_t num_pings;
|
pinged_t pings[PING_NUM_MAX];
|
||||||
static size_t pos_pings;
|
size_t num_pings;
|
||||||
|
size_t pos_pings;
|
||||||
|
} PING;
|
||||||
|
|
||||||
extern uint8_t self_secret_key[crypto_box_SECRETKEYBYTES]; // DHT.c
|
void * new_ping(void)
|
||||||
|
|
||||||
void init_ping()
|
|
||||||
{
|
{
|
||||||
num_pings = 0;
|
return calloc(1, sizeof(PING));
|
||||||
pos_pings = 0;
|
}
|
||||||
|
|
||||||
|
void kill_ping(void * ping)
|
||||||
|
{
|
||||||
|
free(ping);
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool is_timeout(uint64_t time)
|
static bool is_timeout(uint64_t time)
|
||||||
|
@ -40,17 +44,18 @@ static bool is_timeout(uint64_t time)
|
||||||
return (time + PING_TIMEOUT) < now();
|
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 i, id;
|
||||||
size_t new_pos = pos_pings;
|
size_t new_pos = png->pos_pings;
|
||||||
size_t new_num = num_pings;
|
size_t new_num = png->num_pings;
|
||||||
|
|
||||||
// Loop through buffer, oldest first
|
// Loop through buffer, oldest first
|
||||||
for (i = 0; i < num_pings; i++) {
|
for (i = 0; i < png->num_pings; i++) {
|
||||||
id = (pos_pings + i) % PING_NUM_MAX;
|
id = (png->pos_pings + i) % PING_NUM_MAX;
|
||||||
|
|
||||||
if (is_timeout(pings[id].timestamp)) {
|
if (is_timeout(png->pings[id].timestamp)) {
|
||||||
new_pos++;
|
new_pos++;
|
||||||
new_num--;
|
new_num--;
|
||||||
}
|
}
|
||||||
|
@ -60,47 +65,49 @@ static void remove_timeouts() // O(n)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
num_pings = new_num;
|
png->num_pings = new_num;
|
||||||
pos_pings = new_pos % PING_NUM_MAX;
|
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;
|
size_t p;
|
||||||
|
|
||||||
remove_timeouts();
|
remove_timeouts(ping);
|
||||||
|
|
||||||
// Remove oldest ping if full buffer
|
// Remove oldest ping if full buffer
|
||||||
if (num_pings == PING_NUM_MAX) {
|
if (png->num_pings == PING_NUM_MAX) {
|
||||||
num_pings--;
|
png->num_pings--;
|
||||||
pos_pings = (pos_pings + 1) % PING_NUM_MAX;
|
png->pos_pings = (png->pos_pings + 1) % PING_NUM_MAX;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Insert new ping at end of list
|
// 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;
|
png->pings[p].ipp = ipp;
|
||||||
pings[p].timestamp = now();
|
png->pings[p].timestamp = now();
|
||||||
pings[p].id = random_64b();
|
png->pings[p].id = random_64b();
|
||||||
|
|
||||||
num_pings++;
|
png->num_pings++;
|
||||||
return pings[p].id;
|
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)
|
if (ipp.ip.i == 0 && ping_id == 0)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
size_t i, id;
|
size_t i, id;
|
||||||
|
|
||||||
remove_timeouts();
|
remove_timeouts(ping);
|
||||||
|
|
||||||
for (i = 0; i < num_pings; i++) {
|
for (i = 0; i < png->num_pings; i++) {
|
||||||
id = (pos_pings + i) % PING_NUM_MAX;
|
id = (png->pos_pings + i) % PING_NUM_MAX;
|
||||||
|
|
||||||
// ping_id = 0 means match any id
|
// 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;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -108,25 +115,25 @@ bool is_pinging(IP_Port ipp, uint64_t ping_id) // O(n) TODO: replace this with
|
||||||
return false;
|
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;
|
pingreq_t pk;
|
||||||
int rc;
|
int rc;
|
||||||
uint64_t ping_id;
|
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;
|
return 1;
|
||||||
|
|
||||||
// Generate random ping_id
|
// Generate random ping_id
|
||||||
ping_id = add_ping(ipp);
|
ping_id = add_ping(ping, ipp);
|
||||||
|
|
||||||
pk.magic = PACKET_PING_REQ;
|
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
|
random_nonce((uint8_t *) &pk.nonce); // Generate random nonce
|
||||||
|
|
||||||
// Encrypt ping_id using recipient privkey
|
// Encrypt ping_id using recipient privkey
|
||||||
rc = encrypt_data((uint8_t *) client_id,
|
rc = encrypt_data((uint8_t *) client_id,
|
||||||
temp_net_crypto->self_secret_key,
|
c->self_secret_key,
|
||||||
(uint8_t *) &pk.nonce,
|
(uint8_t *) &pk.nonce,
|
||||||
(uint8_t *) &ping_id, sizeof(ping_id),
|
(uint8_t *) &ping_id, sizeof(ping_id),
|
||||||
(uint8_t *) &pk.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)
|
if (rc != sizeof(ping_id) + ENCRYPTION_PADDING)
|
||||||
return 1;
|
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;
|
pingres_t pk;
|
||||||
int rc;
|
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;
|
return 1;
|
||||||
|
|
||||||
pk.magic = PACKET_PING_RES;
|
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
|
random_nonce((uint8_t *) &pk.nonce); // Generate random nonce
|
||||||
|
|
||||||
// Encrypt ping_id using recipient privkey
|
// Encrypt ping_id using recipient privkey
|
||||||
rc = encrypt_data((uint8_t *) client_id,
|
rc = encrypt_data((uint8_t *) client_id,
|
||||||
temp_net_crypto->self_secret_key,
|
c->self_secret_key,
|
||||||
(uint8_t *) &pk.nonce,
|
(uint8_t *) &pk.nonce,
|
||||||
(uint8_t *) &ping_id, sizeof(ping_id),
|
(uint8_t *) &ping_id, sizeof(ping_id),
|
||||||
(uint8_t *) &pk.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)
|
if (rc != sizeof(ping_id) + ENCRYPTION_PADDING)
|
||||||
return 1;
|
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)
|
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;
|
return 1;
|
||||||
|
|
||||||
// Send response
|
// 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);
|
add_toping(dht, (uint8_t *) &p->client_id, source);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -212,7 +219,7 @@ int handle_ping_response(void * object, IP_Port source, uint8_t *packet, uint32_
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
// Make sure ping_id is correct
|
// Make sure ping_id is correct
|
||||||
if (!is_pinging(source, ping_id))
|
if (!is_pinging(dht->ping, source, ping_id))
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
// Associate source ip with client_id
|
// Associate source ip with client_id
|
||||||
|
|
11
core/ping.h
11
core/ping.h
|
@ -7,10 +7,11 @@
|
||||||
|
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
|
||||||
void init_ping();
|
void * new_ping(void);
|
||||||
uint64_t add_ping(IP_Port ipp);
|
void kill_ping(void * ping);
|
||||||
bool is_pinging(IP_Port ipp, uint64_t ping_id);
|
uint64_t add_ping(void * ping, IP_Port ipp);
|
||||||
int send_ping_request(IP_Port ipp, clientid_t *client_id);
|
bool is_pinging(void * ping, IP_Port ipp, uint64_t ping_id);
|
||||||
int send_ping_response(IP_Port ipp, clientid_t *client_id, 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_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);
|
int handle_ping_response(void * object, IP_Port source, uint8_t *packet, uint32_t length);
|
||||||
|
|
|
@ -342,7 +342,7 @@ void execute(ToxWindow *self, ChatContext *ctx, Messenger *m, char *cmd)
|
||||||
wprintw(ctx->history, "Invalid command.\n");
|
wprintw(ctx->history, "Invalid command.\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
static void chat_onDraw(ToxWindow *self)
|
static void chat_onDraw(ToxWindow *self, Messenger *m)
|
||||||
{
|
{
|
||||||
curs_set(1);
|
curs_set(1);
|
||||||
int x, y;
|
int x, y;
|
||||||
|
|
|
@ -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);
|
curs_set(0);
|
||||||
werase(self->window);
|
werase(self->window);
|
||||||
|
|
||||||
|
|
|
@ -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);
|
curs_set(0);
|
||||||
werase(self->window);
|
werase(self->window);
|
||||||
|
|
|
@ -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);
|
curs_set(1);
|
||||||
int x, y;
|
int x, y;
|
||||||
|
|
|
@ -225,7 +225,7 @@ void draw_active_window(Messenger *m)
|
||||||
prepare_window(a->window);
|
prepare_window(a->window);
|
||||||
a->blink = false;
|
a->blink = false;
|
||||||
draw_bar();
|
draw_bar();
|
||||||
a->onDraw(a);
|
a->onDraw(a, m);
|
||||||
|
|
||||||
/* Handle input */
|
/* Handle input */
|
||||||
int ch = getch();
|
int ch = getch();
|
||||||
|
|
|
@ -24,7 +24,7 @@ typedef struct ToxWindow_ ToxWindow;
|
||||||
|
|
||||||
struct ToxWindow_ {
|
struct ToxWindow_ {
|
||||||
void(*onKey)(ToxWindow *, Messenger *, int);
|
void(*onKey)(ToxWindow *, Messenger *, int);
|
||||||
void(*onDraw)(ToxWindow *);
|
void(*onDraw)(ToxWindow *, Messenger *);
|
||||||
void(*onInit)(ToxWindow *, Messenger *);
|
void(*onInit)(ToxWindow *, Messenger *);
|
||||||
void(*onFriendRequest)(ToxWindow *, uint8_t *, uint8_t *, uint16_t);
|
void(*onFriendRequest)(ToxWindow *, uint8_t *, uint8_t *, uint16_t);
|
||||||
void(*onMessage)(ToxWindow *, Messenger *, int, uint8_t *, uint16_t);
|
void(*onMessage)(ToxWindow *, Messenger *, int, uint8_t *, uint16_t);
|
||||||
|
|
Loading…
Reference in New Issue
Block a user