Refactor of core done.

This commit is contained in:
irungentoo 2013-08-20 14:47:32 -04:00
parent a1c40d753e
commit ac621d96d3
16 changed files with 93 additions and 96 deletions

View File

@ -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;

View File

@ -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);

View File

@ -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) */

View File

@ -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

View File

@ -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;
}

View File

@ -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 */

View File

@ -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

View File

@ -7,10 +7,11 @@
#include <stdbool.h>
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);

View File

@ -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;

View File

@ -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);

View File

@ -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);

View File

@ -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;

View File

@ -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();

View File

@ -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);