mirror of
https://github.com/irungentoo/toxcore.git
synced 2024-03-22 13:30:51 +08:00
astyle --options=tools/astylerc -r ./*.{c,h}
This commit is contained in:
parent
c7aafcfb4c
commit
128223d9d1
108
core/DHT.c
108
core/DHT.c
@ -54,7 +54,7 @@
|
||||
#define TIME_TOPING 5
|
||||
|
||||
|
||||
Client_data *DHT_get_close_list(DHT * dht)
|
||||
Client_data *DHT_get_close_list(DHT *dht)
|
||||
{
|
||||
return dht->close_clientlist;
|
||||
}
|
||||
@ -146,7 +146,7 @@ static int client_in_nodelist(Node_format *list, uint32_t length, uint8_t *clien
|
||||
|
||||
/* Returns the friend number from the client_id, or -1 if a failure occurs
|
||||
*/
|
||||
static int friend_number(DHT * dht, uint8_t *client_id)
|
||||
static int friend_number(DHT *dht, uint8_t *client_id)
|
||||
{
|
||||
uint32_t i;
|
||||
|
||||
@ -163,7 +163,7 @@ static int friend_number(DHT * dht, uint8_t *client_id)
|
||||
*
|
||||
* TODO: For the love of based Allah make this function cleaner and much more efficient.
|
||||
*/
|
||||
static int get_close_nodes(DHT * dht, uint8_t *client_id, Node_format *nodes_list)
|
||||
static int get_close_nodes(DHT *dht, uint8_t *client_id, Node_format *nodes_list)
|
||||
{
|
||||
uint32_t i, j, k;
|
||||
uint64_t temp_time = unix_time();
|
||||
@ -330,7 +330,7 @@ static int replace_good( Client_data *list,
|
||||
/* Attempt to add client with ip_port and client_id to the friends client list
|
||||
* and close_clientlist
|
||||
*/
|
||||
void addto_lists(DHT * dht, IP_Port ip_port, uint8_t *client_id)
|
||||
void addto_lists(DHT *dht, IP_Port ip_port, uint8_t *client_id)
|
||||
{
|
||||
uint32_t i;
|
||||
|
||||
@ -372,7 +372,7 @@ void addto_lists(DHT * dht, IP_Port ip_port, uint8_t *client_id)
|
||||
/* If client_id is a friend or us, update ret_ip_port
|
||||
* nodeclient_id is the id of the node that sent us this info
|
||||
*/
|
||||
static void returnedip_ports(DHT * dht, IP_Port ip_port, uint8_t *client_id, uint8_t *nodeclient_id)
|
||||
static void returnedip_ports(DHT *dht, IP_Port ip_port, uint8_t *client_id, uint8_t *nodeclient_id)
|
||||
{
|
||||
uint32_t i, j;
|
||||
uint64_t temp_time = unix_time();
|
||||
@ -406,7 +406,7 @@ static void returnedip_ports(DHT * dht, IP_Port ip_port, uint8_t *client_id, uin
|
||||
}
|
||||
|
||||
/* Same as last function but for get_node requests. */
|
||||
static int is_gettingnodes(DHT * dht, IP_Port ip_port, uint64_t ping_id)
|
||||
static int is_gettingnodes(DHT *dht, IP_Port ip_port, uint64_t ping_id)
|
||||
{
|
||||
uint32_t i;
|
||||
uint8_t pinging;
|
||||
@ -431,7 +431,7 @@ static int is_gettingnodes(DHT * dht, IP_Port ip_port, uint64_t ping_id)
|
||||
}
|
||||
|
||||
/* Same but for get node requests */
|
||||
static uint64_t add_gettingnodes(DHT * dht, IP_Port ip_port)
|
||||
static uint64_t add_gettingnodes(DHT *dht, IP_Port ip_port)
|
||||
{
|
||||
uint32_t i, j;
|
||||
uint64_t ping_id = ((uint64_t)random_int() << 32) + random_int();
|
||||
@ -452,7 +452,7 @@ static uint64_t add_gettingnodes(DHT * dht, IP_Port ip_port)
|
||||
}
|
||||
|
||||
/* send a getnodes request */
|
||||
static int getnodes(DHT * dht, IP_Port ip_port, uint8_t *public_key, uint8_t *client_id)
|
||||
static int getnodes(DHT *dht, IP_Port ip_port, uint8_t *public_key, uint8_t *client_id)
|
||||
{
|
||||
/* check if packet is gonna be sent to ourself */
|
||||
if (id_equal(public_key, dht->c->self_public_key) || is_gettingnodes(dht, ip_port, 0))
|
||||
@ -491,7 +491,7 @@ static int getnodes(DHT * dht, IP_Port ip_port, uint8_t *public_key, uint8_t *cl
|
||||
}
|
||||
|
||||
/* send a send nodes response */
|
||||
static int sendnodes(DHT * dht, IP_Port ip_port, uint8_t *public_key, uint8_t *client_id, uint64_t ping_id)
|
||||
static int sendnodes(DHT *dht, IP_Port ip_port, uint8_t *public_key, uint8_t *client_id, uint64_t ping_id)
|
||||
{
|
||||
/* check if packet is gonna be sent to ourself */
|
||||
if (id_equal(public_key, dht->c->self_public_key))
|
||||
@ -532,9 +532,9 @@ static int sendnodes(DHT * dht, IP_Port ip_port, uint8_t *public_key, uint8_t *c
|
||||
return sendpacket(dht->c->lossless_udp->net->sock, ip_port, data, 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + len);
|
||||
}
|
||||
|
||||
static int handle_getnodes(void * object, IP_Port source, uint8_t *packet, uint32_t length)
|
||||
static int handle_getnodes(void *object, IP_Port source, uint8_t *packet, uint32_t length)
|
||||
{
|
||||
DHT * dht = object;
|
||||
DHT *dht = object;
|
||||
uint64_t ping_id;
|
||||
|
||||
if (length != ( 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES
|
||||
@ -565,9 +565,9 @@ static int handle_getnodes(void * object, IP_Port source, uint8_t *packet, uint3
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int handle_sendnodes(void * object, IP_Port source, uint8_t *packet, uint32_t length)
|
||||
static int handle_sendnodes(void *object, IP_Port source, uint8_t *packet, uint32_t length)
|
||||
{
|
||||
DHT * dht = object;
|
||||
DHT *dht = object;
|
||||
uint64_t ping_id;
|
||||
uint32_t cid_size = 1 + CLIENT_ID_SIZE;
|
||||
cid_size += crypto_box_NONCEBYTES + sizeof(ping_id) + ENCRYPTION_PADDING;
|
||||
@ -613,7 +613,7 @@ static int handle_sendnodes(void * object, IP_Port source, uint8_t *packet, uint
|
||||
/*----------------------------------------------------------------------------------*/
|
||||
/*------------------------END of packet handling functions--------------------------*/
|
||||
|
||||
int DHT_addfriend(DHT * dht, uint8_t *client_id)
|
||||
int DHT_addfriend(DHT *dht, uint8_t *client_id)
|
||||
{
|
||||
if (friend_number(dht, client_id) != -1) /*Is friend already in DHT?*/
|
||||
return 1;
|
||||
@ -633,7 +633,7 @@ int DHT_addfriend(DHT * dht, uint8_t *client_id)
|
||||
return 0;
|
||||
}
|
||||
|
||||
int DHT_delfriend(DHT * dht, uint8_t *client_id)
|
||||
int DHT_delfriend(DHT *dht, uint8_t *client_id)
|
||||
{
|
||||
uint32_t i;
|
||||
DHT_Friend *temp;
|
||||
@ -669,7 +669,7 @@ int DHT_delfriend(DHT * dht, uint8_t *client_id)
|
||||
}
|
||||
|
||||
/* TODO: Optimize this. */
|
||||
IP_Port DHT_getfriendip(DHT * dht, uint8_t *client_id)
|
||||
IP_Port DHT_getfriendip(DHT *dht, uint8_t *client_id)
|
||||
{
|
||||
uint32_t i, j;
|
||||
uint64_t temp_time = unix_time();
|
||||
@ -695,7 +695,7 @@ IP_Port DHT_getfriendip(DHT * dht, uint8_t *client_id)
|
||||
/* Ping each client in the "friends" list every PING_INTERVAL seconds. Send a get nodes request
|
||||
* every GET_NODE_INTERVAL seconds to a random good node for each "friend" in our "friends" list.
|
||||
*/
|
||||
static void do_DHT_friends(DHT * dht)
|
||||
static void do_DHT_friends(DHT *dht)
|
||||
{
|
||||
uint32_t i, j;
|
||||
uint64_t temp_time = unix_time();
|
||||
@ -710,7 +710,7 @@ static void do_DHT_friends(DHT * dht)
|
||||
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->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;
|
||||
}
|
||||
|
||||
@ -725,8 +725,8 @@ static void do_DHT_friends(DHT * dht)
|
||||
if (dht->friends_list[i].lastgetnode + GET_NODE_INTERVAL <= temp_time && num_nodes != 0) {
|
||||
rand_node = rand() % num_nodes;
|
||||
getnodes(dht, dht->friends_list[i].client_list[index[rand_node]].ip_port,
|
||||
dht->friends_list[i].client_list[index[rand_node]].client_id,
|
||||
dht->friends_list[i].client_id );
|
||||
dht->friends_list[i].client_list[index[rand_node]].client_id,
|
||||
dht->friends_list[i].client_id );
|
||||
dht->friends_list[i].lastgetnode = temp_time;
|
||||
}
|
||||
}
|
||||
@ -735,7 +735,7 @@ static void do_DHT_friends(DHT * dht)
|
||||
/* Ping each client in the close nodes list every PING_INTERVAL seconds.
|
||||
* Send a get nodes request every GET_NODE_INTERVAL seconds to a random good node in the list.
|
||||
*/
|
||||
static void do_Close(DHT * dht)
|
||||
static void do_Close(DHT *dht)
|
||||
{
|
||||
uint32_t i;
|
||||
uint64_t temp_time = unix_time();
|
||||
@ -748,7 +748,7 @@ static void do_Close(DHT * dht)
|
||||
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->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;
|
||||
}
|
||||
|
||||
@ -763,13 +763,13 @@ static void do_Close(DHT * dht)
|
||||
if (dht->close_lastgetnodes + GET_NODE_INTERVAL <= temp_time && num_nodes != 0) {
|
||||
rand_node = rand() % num_nodes;
|
||||
getnodes(dht, dht->close_clientlist[index[rand_node]].ip_port,
|
||||
dht->close_clientlist[index[rand_node]].client_id,
|
||||
dht->c->self_public_key );
|
||||
dht->close_clientlist[index[rand_node]].client_id,
|
||||
dht->c->self_public_key );
|
||||
dht->close_lastgetnodes = temp_time;
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
send_ping_request(dht->ping, dht->c, ip_port, (clientid_t *) public_key);
|
||||
@ -778,7 +778,7 @@ void DHT_bootstrap(DHT * dht, IP_Port ip_port, uint8_t *public_key)
|
||||
/* send the given packet to node with client_id
|
||||
* returns -1 if failure
|
||||
*/
|
||||
int route_packet(DHT * dht, uint8_t *client_id, uint8_t *packet, uint32_t length)
|
||||
int route_packet(DHT *dht, uint8_t *client_id, uint8_t *packet, uint32_t length)
|
||||
{
|
||||
uint32_t i;
|
||||
|
||||
@ -796,7 +796,7 @@ int route_packet(DHT * dht, uint8_t *client_id, uint8_t *packet, uint32_t length
|
||||
* return 0 if we are connected to friend or if no ips were found.
|
||||
* returns -1 if no such friend
|
||||
*/
|
||||
static int friend_iplist(DHT * dht, IP_Port *ip_portlist, uint16_t friend_num)
|
||||
static int friend_iplist(DHT *dht, IP_Port *ip_portlist, uint16_t friend_num)
|
||||
{
|
||||
int num_ips = 0;
|
||||
uint32_t i;
|
||||
@ -831,7 +831,7 @@ static int friend_iplist(DHT * dht, IP_Port *ip_portlist, uint16_t friend_num)
|
||||
*
|
||||
* Only works if more than (MAX_FRIEND_CLIENTS / 2) return an ip for friend.
|
||||
*/
|
||||
int route_tofriend(DHT * dht, uint8_t *friend_id, uint8_t *packet, uint32_t length)
|
||||
int route_tofriend(DHT *dht, uint8_t *friend_id, uint8_t *packet, uint32_t length)
|
||||
{
|
||||
int num = friend_number(dht, friend_id);
|
||||
|
||||
@ -866,7 +866,7 @@ int route_tofriend(DHT * dht, uint8_t *friend_id, uint8_t *packet, uint32_t leng
|
||||
/* Send the following packet to one random person who tells us they are connected to friend_id
|
||||
* returns the number of nodes it sent the packet to
|
||||
*/
|
||||
static int routeone_tofriend(DHT * dht, uint8_t *friend_id, uint8_t *packet, uint32_t length)
|
||||
static int routeone_tofriend(DHT *dht, uint8_t *friend_id, uint8_t *packet, uint32_t length)
|
||||
{
|
||||
int num = friend_number(dht, friend_id);
|
||||
|
||||
@ -906,7 +906,7 @@ static int routeone_tofriend(DHT * dht, uint8_t *friend_id, uint8_t *packet, uin
|
||||
* return 0 if we are connected to friend or if no ips were found.
|
||||
* returns -1 if no such friend
|
||||
*/
|
||||
int friend_ips(DHT * dht, IP_Port *ip_portlist, uint8_t *friend_id)
|
||||
int friend_ips(DHT *dht, IP_Port *ip_portlist, uint8_t *friend_id)
|
||||
{
|
||||
uint32_t i;
|
||||
|
||||
@ -922,7 +922,7 @@ int friend_ips(DHT * dht, IP_Port *ip_portlist, uint8_t *friend_id)
|
||||
/*----------------------------------------------------------------------------------*/
|
||||
/*---------------------BEGINNING OF NAT PUNCHING FUNCTIONS--------------------------*/
|
||||
|
||||
static int send_NATping(DHT * dht, uint8_t *public_key, uint64_t ping_id, uint8_t type)
|
||||
static int send_NATping(DHT *dht, uint8_t *public_key, uint64_t ping_id, uint8_t type)
|
||||
{
|
||||
uint8_t data[sizeof(uint64_t) + 1];
|
||||
uint8_t packet[MAX_DATA_SIZE];
|
||||
@ -932,7 +932,8 @@ static int send_NATping(DHT * dht, uint8_t *public_key, uint64_t ping_id, uint8_
|
||||
data[0] = type;
|
||||
memcpy(data + 1, &ping_id, sizeof(uint64_t));
|
||||
/* 254 is NAT ping request packet id */
|
||||
int len = create_request(dht->c->self_public_key, dht->c->self_secret_key, packet, public_key, data, sizeof(uint64_t) + 1, 254);
|
||||
int len = create_request(dht->c->self_public_key, dht->c->self_secret_key, packet, public_key, data,
|
||||
sizeof(uint64_t) + 1, 254);
|
||||
|
||||
if (len == -1)
|
||||
return -1;
|
||||
@ -949,9 +950,9 @@ static int send_NATping(DHT * dht, uint8_t *public_key, uint64_t ping_id, uint8_
|
||||
}
|
||||
|
||||
/* Handle a received ping request for */
|
||||
static int handle_NATping(void * object, IP_Port source, uint8_t *source_pubkey, uint8_t *packet, uint32_t length)
|
||||
static int handle_NATping(void *object, IP_Port source, uint8_t *source_pubkey, uint8_t *packet, uint32_t length)
|
||||
{
|
||||
DHT * dht = object;
|
||||
DHT *dht = object;
|
||||
uint64_t ping_id;
|
||||
memcpy(&ping_id, packet + 1, sizeof(uint64_t));
|
||||
|
||||
@ -1026,7 +1027,7 @@ static uint16_t NAT_getports(uint16_t *portlist, IP_Port *ip_portlist, uint16_t
|
||||
return num;
|
||||
}
|
||||
|
||||
static void punch_holes(DHT * dht, IP ip, uint16_t *port_list, uint16_t numports, uint16_t friend_num)
|
||||
static void punch_holes(DHT *dht, IP ip, uint16_t *port_list, uint16_t numports, uint16_t friend_num)
|
||||
{
|
||||
if (numports > MAX_FRIEND_CLIENTS || numports == 0)
|
||||
return;
|
||||
@ -1044,7 +1045,7 @@ static void punch_holes(DHT * dht, IP ip, uint16_t *port_list, uint16_t numports
|
||||
dht->friends_list[friend_num].punching_index = i;
|
||||
}
|
||||
|
||||
static void do_NAT(DHT * dht)
|
||||
static void do_NAT(DHT *dht)
|
||||
{
|
||||
uint32_t i;
|
||||
uint64_t temp_time = unix_time();
|
||||
@ -1093,7 +1094,7 @@ static void do_NAT(DHT * dht)
|
||||
network while preventing amplification attacks.
|
||||
return 0 if node was added
|
||||
return -1 if node was not added */
|
||||
int add_toping(DHT * dht, uint8_t *client_id, IP_Port ip_port)
|
||||
int add_toping(DHT *dht, uint8_t *client_id, IP_Port ip_port)
|
||||
{
|
||||
if (ip_port.ip.i == 0)
|
||||
return -1;
|
||||
@ -1123,7 +1124,7 @@ int add_toping(DHT * dht, 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 void do_toping(DHT * dht)
|
||||
static void do_toping(DHT *dht)
|
||||
{
|
||||
uint64_t temp_time = unix_time();
|
||||
|
||||
@ -1143,18 +1144,23 @@ static void do_toping(DHT * dht)
|
||||
}
|
||||
|
||||
|
||||
DHT * new_DHT(Net_Crypto *c)
|
||||
DHT *new_DHT(Net_Crypto *c)
|
||||
{
|
||||
if (c == NULL)
|
||||
return NULL;
|
||||
DHT * temp = calloc(1, sizeof(DHT));
|
||||
|
||||
DHT *temp = calloc(1, sizeof(DHT));
|
||||
|
||||
if (temp == NULL)
|
||||
return NULL;
|
||||
return NULL;
|
||||
|
||||
temp->ping = new_ping();
|
||||
if (temp->ping == NULL){
|
||||
kill_DHT(temp);
|
||||
return NULL;
|
||||
|
||||
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);
|
||||
@ -1164,14 +1170,14 @@ DHT * new_DHT(Net_Crypto *c)
|
||||
return temp;
|
||||
}
|
||||
|
||||
void do_DHT(DHT * dht)
|
||||
void do_DHT(DHT *dht)
|
||||
{
|
||||
do_Close(dht);
|
||||
do_DHT_friends(dht);
|
||||
do_NAT(dht);
|
||||
do_toping(dht);
|
||||
}
|
||||
void kill_DHT(DHT * dht)
|
||||
void kill_DHT(DHT *dht)
|
||||
{
|
||||
kill_ping(dht->ping);
|
||||
free(dht->friends_list);
|
||||
@ -1179,13 +1185,13 @@ void kill_DHT(DHT * dht)
|
||||
}
|
||||
|
||||
/* get the size of the DHT (for saving) */
|
||||
uint32_t DHT_size(DHT * dht)
|
||||
uint32_t DHT_size(DHT *dht)
|
||||
{
|
||||
return sizeof(dht->close_clientlist) + sizeof(DHT_Friend) * dht->num_friends;
|
||||
}
|
||||
|
||||
/* save the DHT in data where data is an array of size DHT_size() */
|
||||
void DHT_save(DHT * dht, uint8_t *data)
|
||||
void DHT_save(DHT *dht, uint8_t *data)
|
||||
{
|
||||
memcpy(data, dht->close_clientlist, sizeof(dht->close_clientlist));
|
||||
memcpy(data + sizeof(dht->close_clientlist), dht->friends_list, sizeof(DHT_Friend) * dht->num_friends);
|
||||
@ -1195,7 +1201,7 @@ void DHT_save(DHT * dht, uint8_t *data)
|
||||
* return -1 if failure
|
||||
* return 0 if success
|
||||
*/
|
||||
int DHT_load(DHT * dht, uint8_t *data, uint32_t size)
|
||||
int DHT_load(DHT *dht, uint8_t *data, uint32_t size)
|
||||
{
|
||||
if (size < sizeof(dht->close_clientlist))
|
||||
return -1;
|
||||
@ -1231,7 +1237,7 @@ int DHT_load(DHT * dht, uint8_t *data, uint32_t size)
|
||||
for (i = 0; i < LCLIENT_LIST; ++i) {
|
||||
if (tempclose_clientlist[i].timestamp != 0)
|
||||
DHT_bootstrap(dht, tempclose_clientlist[i].ip_port,
|
||||
tempclose_clientlist[i].client_id );
|
||||
tempclose_clientlist[i].client_id );
|
||||
}
|
||||
|
||||
return 0;
|
||||
@ -1240,7 +1246,7 @@ int DHT_load(DHT * dht, uint8_t *data, uint32_t size)
|
||||
/* returns 0 if we are not connected to the DHT
|
||||
* returns 1 if we are
|
||||
*/
|
||||
int DHT_isconnected(DHT * dht)
|
||||
int DHT_isconnected(DHT *dht)
|
||||
{
|
||||
uint32_t i;
|
||||
uint64_t temp_time = unix_time();
|
||||
|
36
core/DHT.h
36
core/DHT.h
@ -100,23 +100,23 @@ typedef struct {
|
||||
Node_format toping[MAX_TOPING];
|
||||
uint64_t last_toping;
|
||||
uint64_t close_lastgetnodes;
|
||||
void * ping;
|
||||
void *ping;
|
||||
} DHT;
|
||||
/*----------------------------------------------------------------------------------*/
|
||||
|
||||
Client_data *DHT_get_close_list(DHT * dht);
|
||||
Client_data *DHT_get_close_list(DHT *dht);
|
||||
|
||||
/* Add a new friend to the friends list
|
||||
client_id must be CLIENT_ID_SIZE bytes long.
|
||||
returns 0 if success
|
||||
returns 1 if failure (friends list is full) */
|
||||
int DHT_addfriend(DHT * dht, uint8_t *client_id);
|
||||
int DHT_addfriend(DHT *dht, uint8_t *client_id);
|
||||
|
||||
/* Delete a friend from the friends list
|
||||
client_id must be CLIENT_ID_SIZE bytes long.
|
||||
returns 0 if success
|
||||
returns 1 if failure (client_id not in friends list) */
|
||||
int DHT_delfriend(DHT * dht, uint8_t *client_id);
|
||||
int DHT_delfriend(DHT *dht, uint8_t *client_id);
|
||||
|
||||
/* Get ip of friend
|
||||
client_id must be CLIENT_ID_SIZE bytes long.
|
||||
@ -125,14 +125,14 @@ int DHT_delfriend(DHT * dht, uint8_t *client_id);
|
||||
returns ip if success
|
||||
returns ip of 0 if failure (This means the friend is either offline or we have not found him yet.)
|
||||
returns ip of 1 if friend is not in list. */
|
||||
IP_Port DHT_getfriendip(DHT * dht, uint8_t *client_id);
|
||||
IP_Port DHT_getfriendip(DHT *dht, uint8_t *client_id);
|
||||
|
||||
/* Run this function at least a couple times per second (It's the main loop) */
|
||||
void do_DHT(DHT * dht);
|
||||
void do_DHT(DHT *dht);
|
||||
|
||||
/* Use this function to bootstrap the client
|
||||
Sends a get nodes request to the given node with ip port and public_key */
|
||||
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);
|
||||
|
||||
/* Add nodes to the toping list
|
||||
all nodes in this list are pinged every TIME_TOPING seconds
|
||||
@ -142,17 +142,17 @@ void DHT_bootstrap(DHT * dht, IP_Port ip_port, uint8_t *public_key);
|
||||
network while preventing amplification attacks.
|
||||
return 0 if node was added
|
||||
return -1 if node was not added */
|
||||
int add_toping(DHT * dht, uint8_t *client_id, IP_Port ip_port);
|
||||
int add_toping(DHT *dht, uint8_t *client_id, IP_Port ip_port);
|
||||
|
||||
/* ROUTING FUNCTIONS */
|
||||
|
||||
/* send the given packet to node with client_id
|
||||
returns -1 if failure */
|
||||
int route_packet(DHT * dht, uint8_t *client_id, uint8_t *packet, uint32_t length);
|
||||
int route_packet(DHT *dht, uint8_t *client_id, uint8_t *packet, uint32_t length);
|
||||
|
||||
/* Send the following packet to everyone who tells us they are connected to friend_id
|
||||
returns the number of nodes it sent the packet to */
|
||||
int route_tofriend(DHT * dht, uint8_t *friend_id, uint8_t *packet, uint32_t length);
|
||||
int route_tofriend(DHT *dht, uint8_t *friend_id, uint8_t *packet, uint32_t length);
|
||||
|
||||
/* NAT PUNCHING FUNCTIONS */
|
||||
|
||||
@ -160,31 +160,31 @@ int route_tofriend(DHT * dht, uint8_t *friend_id, uint8_t *packet, uint32_t leng
|
||||
ip_portlist must be at least MAX_FRIEND_CLIENTS big
|
||||
returns the number of ips returned
|
||||
returns -1 if no such friend*/
|
||||
int friend_ips(DHT * dht, IP_Port *ip_portlist, uint8_t *friend_id);
|
||||
int friend_ips(DHT *dht, IP_Port *ip_portlist, uint8_t *friend_id);
|
||||
|
||||
/* SAVE/LOAD functions */
|
||||
|
||||
/* get the size of the DHT (for saving) */
|
||||
uint32_t DHT_size(DHT * dht);
|
||||
uint32_t DHT_size(DHT *dht);
|
||||
|
||||
/* save the DHT in data where data is an array of size DHT_size() */
|
||||
void DHT_save(DHT * dht, uint8_t *data);
|
||||
void DHT_save(DHT *dht, uint8_t *data);
|
||||
|
||||
/* init DHT */
|
||||
DHT * new_DHT(Net_Crypto *c);
|
||||
DHT *new_DHT(Net_Crypto *c);
|
||||
|
||||
void kill_DHT(DHT * dht);
|
||||
void kill_DHT(DHT *dht);
|
||||
|
||||
/* load the DHT from data of size size;
|
||||
return -1 if failure
|
||||
return 0 if success */
|
||||
int DHT_load(DHT * dht, uint8_t *data, uint32_t size);
|
||||
int DHT_load(DHT *dht, uint8_t *data, uint32_t size);
|
||||
|
||||
/* returns 0 if we are not connected to the DHT
|
||||
returns 1 if we are */
|
||||
int DHT_isconnected(DHT * dht);
|
||||
int DHT_isconnected(DHT *dht);
|
||||
|
||||
void addto_lists(DHT * dht, IP_Port ip_port, uint8_t *client_id);
|
||||
void addto_lists(DHT *dht, IP_Port ip_port, uint8_t *client_id);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
@ -121,9 +121,10 @@ static int LAN_ip(IP ip)
|
||||
return -1;
|
||||
}
|
||||
|
||||
static int handle_LANdiscovery(void * object, IP_Port source, uint8_t *packet, uint32_t length)
|
||||
static int handle_LANdiscovery(void *object, IP_Port source, uint8_t *packet, uint32_t length)
|
||||
{
|
||||
DHT *dht = object;
|
||||
|
||||
if (LAN_ip(source.ip) == -1)
|
||||
return 1;
|
||||
|
||||
|
@ -36,7 +36,7 @@
|
||||
* Return -1 if there are no connections like we are looking for
|
||||
* Return id if it found it
|
||||
*/
|
||||
int getconnection_id(Lossless_UDP * ludp, IP_Port ip_port)
|
||||
int getconnection_id(Lossless_UDP *ludp, IP_Port ip_port)
|
||||
{
|
||||
uint32_t i;
|
||||
|
||||
@ -57,7 +57,7 @@ int getconnection_id(Lossless_UDP * ludp, IP_Port ip_port)
|
||||
*
|
||||
* TODO: make this better
|
||||
*/
|
||||
static uint32_t handshake_id(Lossless_UDP * ludp, IP_Port source)
|
||||
static uint32_t handshake_id(Lossless_UDP *ludp, IP_Port source)
|
||||
{
|
||||
uint32_t id = 0, i;
|
||||
|
||||
@ -79,7 +79,7 @@ static uint32_t handshake_id(Lossless_UDP * ludp, IP_Port source)
|
||||
*
|
||||
* TODO: make this better
|
||||
*/
|
||||
static void change_handshake(Lossless_UDP * ludp, IP_Port source)
|
||||
static void change_handshake(Lossless_UDP *ludp, IP_Port source)
|
||||
{
|
||||
uint8_t rand = random_int() % 4;
|
||||
ludp->randtable[rand][((uint8_t *)&source)[rand]] = random_int();
|
||||
@ -91,7 +91,7 @@ static void change_handshake(Lossless_UDP * ludp, IP_Port source)
|
||||
* Return -1 if it could not initialize the connectiont
|
||||
* If there already was an existing connection to that ip_port return its number.
|
||||
*/
|
||||
int new_connection(Lossless_UDP * ludp, IP_Port ip_port)
|
||||
int new_connection(Lossless_UDP *ludp, IP_Port ip_port)
|
||||
{
|
||||
int connect = getconnection_id(ludp, ip_port);
|
||||
|
||||
@ -148,7 +148,7 @@ int new_connection(Lossless_UDP * ludp, IP_Port ip_port)
|
||||
* Returns an integer corresponding to the connection id.
|
||||
* Return -1 if it could not initialize the connection.
|
||||
*/
|
||||
static int new_inconnection(Lossless_UDP * ludp, IP_Port ip_port)
|
||||
static int new_inconnection(Lossless_UDP *ludp, IP_Port ip_port)
|
||||
{
|
||||
if (getconnection_id(ludp, ip_port) != -1)
|
||||
return -1;
|
||||
@ -200,7 +200,7 @@ static int new_inconnection(Lossless_UDP * ludp, IP_Port ip_port)
|
||||
* Returns an integer corresponding to the next connection in our incoming connection list.
|
||||
* Return -1 if there are no new incoming connections in the list.
|
||||
*/
|
||||
int incoming_connection(Lossless_UDP * ludp)
|
||||
int incoming_connection(Lossless_UDP *ludp)
|
||||
{
|
||||
uint32_t i;
|
||||
|
||||
@ -215,7 +215,7 @@ int incoming_connection(Lossless_UDP * ludp)
|
||||
}
|
||||
|
||||
/* Try to free some memory from the connections array. */
|
||||
static void free_connections(Lossless_UDP * ludp)
|
||||
static void free_connections(Lossless_UDP *ludp)
|
||||
{
|
||||
uint32_t i;
|
||||
|
||||
@ -247,7 +247,7 @@ static void free_connections(Lossless_UDP * ludp)
|
||||
* Return -1 if it could not kill the connection.
|
||||
* Return 0 if killed successfully
|
||||
*/
|
||||
int kill_connection(Lossless_UDP * ludp, int connection_id)
|
||||
int kill_connection(Lossless_UDP *ludp, int connection_id)
|
||||
{
|
||||
if (connection_id >= 0 && connection_id < ludp->connections_length) {
|
||||
if (ludp->connections[connection_id].status > 0) {
|
||||
@ -267,7 +267,7 @@ int kill_connection(Lossless_UDP * ludp, int connection_id)
|
||||
* Return -1 if it can not kill the connection.
|
||||
* Return 0 if it will kill it.
|
||||
*/
|
||||
int kill_connection_in(Lossless_UDP * ludp, int connection_id, uint32_t seconds)
|
||||
int kill_connection_in(Lossless_UDP *ludp, int connection_id, uint32_t seconds)
|
||||
{
|
||||
if (connection_id >= 0 && connection_id < ludp->connections_length) {
|
||||
if (ludp->connections[connection_id].status > 0) {
|
||||
@ -287,7 +287,7 @@ int kill_connection_in(Lossless_UDP * ludp, int connection_id, uint32_t seconds)
|
||||
* Return 3 if fully connected.
|
||||
* Return 4 if timed out and waiting to be killed.
|
||||
*/
|
||||
int is_connected(Lossless_UDP * ludp, int connection_id)
|
||||
int is_connected(Lossless_UDP *ludp, int connection_id)
|
||||
{
|
||||
if (connection_id >= 0 && connection_id < ludp->connections_length)
|
||||
return ludp->connections[connection_id].status;
|
||||
@ -296,7 +296,7 @@ int is_connected(Lossless_UDP * ludp, int connection_id)
|
||||
}
|
||||
|
||||
/* returns the ip_port of the corresponding connection. */
|
||||
IP_Port connection_ip(Lossless_UDP * ludp, int connection_id)
|
||||
IP_Port connection_ip(Lossless_UDP *ludp, int connection_id)
|
||||
{
|
||||
if (connection_id >= 0 && connection_id < ludp->connections_length)
|
||||
return ludp->connections[connection_id].ip_port;
|
||||
@ -306,7 +306,7 @@ IP_Port connection_ip(Lossless_UDP * ludp, int connection_id)
|
||||
}
|
||||
|
||||
/* returns the number of packets in the queue waiting to be successfully sent. */
|
||||
uint32_t sendqueue(Lossless_UDP * ludp, int connection_id)
|
||||
uint32_t sendqueue(Lossless_UDP *ludp, int connection_id)
|
||||
{
|
||||
if (connection_id < 0 || connection_id >= ludp->connections_length)
|
||||
return 0;
|
||||
@ -315,7 +315,7 @@ uint32_t sendqueue(Lossless_UDP * ludp, int connection_id)
|
||||
}
|
||||
|
||||
/* returns the number of packets in the queue waiting to be successfully read with read_packet(...) */
|
||||
uint32_t recvqueue(Lossless_UDP * ludp, int connection_id)
|
||||
uint32_t recvqueue(Lossless_UDP *ludp, int connection_id)
|
||||
{
|
||||
if (connection_id < 0 || connection_id >= ludp->connections_length)
|
||||
return 0;
|
||||
@ -325,20 +325,21 @@ uint32_t recvqueue(Lossless_UDP * ludp, int connection_id)
|
||||
|
||||
/* returns the id of the next packet in the queue
|
||||
return -1 if no packet in queue */
|
||||
char id_packet(Lossless_UDP * ludp, int connection_id)
|
||||
char id_packet(Lossless_UDP *ludp, int connection_id)
|
||||
{
|
||||
if (connection_id < 0 || connection_id >= ludp->connections_length)
|
||||
return -1;
|
||||
|
||||
if (recvqueue(ludp, connection_id) != 0 && ludp->connections[connection_id].status != 0)
|
||||
return ludp->connections[connection_id].recvbuffer[ludp->connections[connection_id].successful_read % MAX_QUEUE_NUM].data[0];
|
||||
return ludp->connections[connection_id].recvbuffer[ludp->connections[connection_id].successful_read %
|
||||
MAX_QUEUE_NUM].data[0];
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* return 0 if there is no received data in the buffer.
|
||||
return length of received packet if successful */
|
||||
int read_packet(Lossless_UDP * ludp, int connection_id, uint8_t *data)
|
||||
int read_packet(Lossless_UDP *ludp, int connection_id, uint8_t *data)
|
||||
{
|
||||
if (recvqueue(ludp, connection_id) != 0) {
|
||||
uint16_t index = ludp->connections[connection_id].successful_read % MAX_QUEUE_NUM;
|
||||
@ -356,7 +357,7 @@ int read_packet(Lossless_UDP * ludp, int connection_id, uint8_t *data)
|
||||
* Return 0 if data could not be put in packet queue
|
||||
* Return 1 if data was put into the queue
|
||||
*/
|
||||
int write_packet(Lossless_UDP * ludp, int connection_id, uint8_t *data, uint32_t length)
|
||||
int write_packet(Lossless_UDP *ludp, int connection_id, uint8_t *data, uint32_t length)
|
||||
{
|
||||
if (length > MAX_DATA_SIZE || length == 0)
|
||||
return 0;
|
||||
@ -373,7 +374,7 @@ int write_packet(Lossless_UDP * ludp, int connection_id, uint8_t *data, uint32_t
|
||||
}
|
||||
|
||||
/* put the packet numbers the we are missing in requested and return the number */
|
||||
uint32_t missing_packets(Lossless_UDP * ludp, int connection_id, uint32_t *requested)
|
||||
uint32_t missing_packets(Lossless_UDP *ludp, int connection_id, uint32_t *requested)
|
||||
{
|
||||
uint32_t number = 0;
|
||||
uint32_t i;
|
||||
@ -403,7 +404,7 @@ uint32_t missing_packets(Lossless_UDP * ludp, int connection_id, uint32_t *reque
|
||||
* see http://wiki.tox.im/index.php/Lossless_UDP for more information.
|
||||
*/
|
||||
|
||||
static int send_handshake(Lossless_UDP * ludp, IP_Port ip_port, uint32_t handshake_id1, uint32_t handshake_id2)
|
||||
static int send_handshake(Lossless_UDP *ludp, IP_Port ip_port, uint32_t handshake_id1, uint32_t handshake_id2)
|
||||
{
|
||||
uint8_t packet[1 + 4 + 4];
|
||||
uint32_t temp;
|
||||
@ -417,7 +418,7 @@ static int send_handshake(Lossless_UDP * ludp, IP_Port ip_port, uint32_t handsha
|
||||
return sendpacket(ludp->net->sock, ip_port, packet, sizeof(packet));
|
||||
}
|
||||
|
||||
static int send_SYNC(Lossless_UDP * ludp, uint32_t connection_id)
|
||||
static int send_SYNC(Lossless_UDP *ludp, uint32_t connection_id)
|
||||
{
|
||||
uint8_t packet[(BUFFER_PACKET_NUM * 4 + 4 + 4 + 2)];
|
||||
uint16_t index = 0;
|
||||
@ -444,7 +445,7 @@ static int send_SYNC(Lossless_UDP * ludp, uint32_t connection_id)
|
||||
|
||||
}
|
||||
|
||||
static int send_data_packet(Lossless_UDP * ludp, uint32_t connection_id, uint32_t packet_num)
|
||||
static int send_data_packet(Lossless_UDP *ludp, uint32_t connection_id, uint32_t packet_num)
|
||||
{
|
||||
uint32_t index = packet_num % MAX_QUEUE_NUM;
|
||||
uint32_t temp;
|
||||
@ -459,7 +460,7 @@ static int send_data_packet(Lossless_UDP * ludp, uint32_t connection_id, uint32_
|
||||
}
|
||||
|
||||
/* sends 1 data packet */
|
||||
static int send_DATA(Lossless_UDP * ludp, uint32_t connection_id)
|
||||
static int send_DATA(Lossless_UDP *ludp, uint32_t connection_id)
|
||||
{
|
||||
int ret;
|
||||
uint32_t buffer[BUFFER_PACKET_NUM];
|
||||
@ -491,9 +492,10 @@ static int send_DATA(Lossless_UDP * ludp, uint32_t connection_id)
|
||||
|
||||
|
||||
/* Return 0 if handled correctly, 1 if packet is bad. */
|
||||
static int handle_handshake(void * object, IP_Port source, uint8_t *packet, uint32_t length)
|
||||
static int handle_handshake(void *object, IP_Port source, uint8_t *packet, uint32_t length)
|
||||
{
|
||||
Lossless_UDP * ludp = object;
|
||||
Lossless_UDP *ludp = object;
|
||||
|
||||
if (length != (1 + 4 + 4))
|
||||
return 1;
|
||||
|
||||
@ -542,7 +544,7 @@ static int SYNC_valid(uint32_t length)
|
||||
}
|
||||
|
||||
/* case 1 in handle_SYNC: */
|
||||
static int handle_SYNC1(Lossless_UDP * ludp, IP_Port source, uint32_t recv_packetnum, uint32_t sent_packetnum)
|
||||
static int handle_SYNC1(Lossless_UDP *ludp, IP_Port source, uint32_t recv_packetnum, uint32_t sent_packetnum)
|
||||
{
|
||||
if (handshake_id(ludp, source) == recv_packetnum) {
|
||||
int x = new_inconnection(ludp, source);
|
||||
@ -564,7 +566,8 @@ static int handle_SYNC1(Lossless_UDP * ludp, IP_Port source, uint32_t recv_packe
|
||||
}
|
||||
|
||||
/* case 2 in handle_SYNC: */
|
||||
static int handle_SYNC2(Lossless_UDP * ludp, int connection_id, uint8_t counter, uint32_t recv_packetnum, uint32_t sent_packetnum)
|
||||
static int handle_SYNC2(Lossless_UDP *ludp, int connection_id, uint8_t counter, uint32_t recv_packetnum,
|
||||
uint32_t sent_packetnum)
|
||||
{
|
||||
if (recv_packetnum == ludp->connections[connection_id].orecv_packetnum) {
|
||||
/* && sent_packetnum == ludp->connections[connection_id].osent_packetnum) */
|
||||
@ -578,7 +581,8 @@ static int handle_SYNC2(Lossless_UDP * ludp, int connection_id, uint8_t counter,
|
||||
return 1;
|
||||
}
|
||||
/* case 3 in handle_SYNC: */
|
||||
static int handle_SYNC3(Lossless_UDP * ludp, int connection_id, uint8_t counter, uint32_t recv_packetnum, uint32_t sent_packetnum,
|
||||
static int handle_SYNC3(Lossless_UDP *ludp, int connection_id, uint8_t counter, uint32_t recv_packetnum,
|
||||
uint32_t sent_packetnum,
|
||||
uint32_t *req_packets,
|
||||
uint16_t number)
|
||||
{
|
||||
@ -614,9 +618,10 @@ static int handle_SYNC3(Lossless_UDP * ludp, int connection_id, uint8_t counter,
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int handle_SYNC(void * object, IP_Port source, uint8_t *packet, uint32_t length)
|
||||
static int handle_SYNC(void *object, IP_Port source, uint8_t *packet, uint32_t length)
|
||||
{
|
||||
Lossless_UDP * ludp = object;
|
||||
Lossless_UDP *ludp = object;
|
||||
|
||||
if (!SYNC_valid(length))
|
||||
return 1;
|
||||
|
||||
@ -654,7 +659,7 @@ static int handle_SYNC(void * object, IP_Port source, uint8_t *packet, uint32_t
|
||||
* Add a packet to the received buffer and set the recv_packetnum of the
|
||||
* connection to its proper value. Return 1 if data was too big, 0 if not.
|
||||
*/
|
||||
static int add_recv(Lossless_UDP * ludp, int connection_id, uint32_t data_num, uint8_t *data, uint16_t size)
|
||||
static int add_recv(Lossless_UDP *ludp, int connection_id, uint32_t data_num, uint8_t *data, uint16_t size)
|
||||
{
|
||||
if (size > MAX_DATA_SIZE)
|
||||
return 1;
|
||||
@ -688,9 +693,9 @@ static int add_recv(Lossless_UDP * ludp, int connection_id, uint32_t data_num, u
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int handle_data(void * object, IP_Port source, uint8_t *packet, uint32_t length)
|
||||
static int handle_data(void *object, IP_Port source, uint8_t *packet, uint32_t length)
|
||||
{
|
||||
Lossless_UDP * ludp = object;
|
||||
Lossless_UDP *ludp = object;
|
||||
int connection = getconnection_id(ludp, source);
|
||||
|
||||
if (connection == -1)
|
||||
@ -717,13 +722,16 @@ static int handle_data(void * object, IP_Port source, uint8_t *packet, uint32_t
|
||||
* END of packet handling functions
|
||||
*/
|
||||
|
||||
Lossless_UDP * new_lossless_udp(Networking_Core * net)
|
||||
Lossless_UDP *new_lossless_udp(Networking_Core *net)
|
||||
{
|
||||
if(net == NULL)
|
||||
if (net == NULL)
|
||||
return NULL;
|
||||
Lossless_UDP * temp = calloc(1, sizeof(Lossless_UDP));
|
||||
|
||||
Lossless_UDP *temp = calloc(1, sizeof(Lossless_UDP));
|
||||
|
||||
if (temp == NULL)
|
||||
return NULL;
|
||||
return NULL;
|
||||
|
||||
temp->net = net;
|
||||
networking_registerhandler(net, 16, &handle_handshake, temp);
|
||||
networking_registerhandler(net, 17, &handle_SYNC, temp);
|
||||
@ -735,7 +743,7 @@ Lossless_UDP * new_lossless_udp(Networking_Core * net)
|
||||
* Send handshake requests
|
||||
* handshake packets are sent at the same rate as SYNC packets
|
||||
*/
|
||||
static void do_new(Lossless_UDP * ludp)
|
||||
static void do_new(Lossless_UDP *ludp)
|
||||
{
|
||||
uint32_t i;
|
||||
uint64_t temp_time = current_time();
|
||||
@ -760,7 +768,7 @@ static void do_new(Lossless_UDP * ludp)
|
||||
}
|
||||
}
|
||||
|
||||
static void do_SYNC(Lossless_UDP * ludp)
|
||||
static void do_SYNC(Lossless_UDP *ludp)
|
||||
{
|
||||
uint32_t i;
|
||||
uint64_t temp_time = current_time();
|
||||
@ -774,7 +782,7 @@ static void do_SYNC(Lossless_UDP * ludp)
|
||||
}
|
||||
}
|
||||
|
||||
static void do_data(Lossless_UDP * ludp)
|
||||
static void do_data(Lossless_UDP *ludp)
|
||||
{
|
||||
uint32_t i;
|
||||
uint64_t j;
|
||||
@ -797,7 +805,7 @@ static void do_data(Lossless_UDP * ludp)
|
||||
*
|
||||
* TODO: flow control.
|
||||
*/
|
||||
static void adjust_rates(Lossless_UDP * ludp)
|
||||
static void adjust_rates(Lossless_UDP *ludp)
|
||||
{
|
||||
uint32_t i;
|
||||
uint64_t temp_time = current_time();
|
||||
@ -819,7 +827,7 @@ static void adjust_rates(Lossless_UDP * ludp)
|
||||
}
|
||||
|
||||
/* Call this function a couple times per second It's the main loop. */
|
||||
void do_lossless_udp(Lossless_UDP * ludp)
|
||||
void do_lossless_udp(Lossless_UDP *ludp)
|
||||
{
|
||||
do_new(ludp);
|
||||
do_SYNC(ludp);
|
||||
@ -827,7 +835,7 @@ void do_lossless_udp(Lossless_UDP * ludp)
|
||||
adjust_rates(ludp);
|
||||
}
|
||||
|
||||
void kill_lossless_udp(Lossless_UDP * ludp)
|
||||
void kill_lossless_udp(Lossless_UDP *ludp)
|
||||
{
|
||||
free(ludp->connections);
|
||||
free(ludp);
|
||||
|
@ -118,14 +118,14 @@ typedef struct {
|
||||
} Connection;
|
||||
|
||||
typedef struct {
|
||||
Networking_Core *net;
|
||||
Connection *connections;
|
||||
Networking_Core *net;
|
||||
Connection *connections;
|
||||
|
||||
uint32_t connections_length; /* Length of connections array */
|
||||
uint32_t connections_number; /* Number of connections in connections array */
|
||||
uint32_t connections_length; /* Length of connections array */
|
||||
uint32_t connections_number; /* Number of connections in connections array */
|
||||
|
||||
/* table of random numbers used in handshake_id. */
|
||||
uint32_t randtable[6][256];
|
||||
/* table of random numbers used in handshake_id. */
|
||||
uint32_t randtable[6][256];
|
||||
|
||||
} Lossless_UDP;
|
||||
|
||||
@ -135,66 +135,66 @@ uint32_t randtable[6][256];
|
||||
* Return -1 if it could not initialize the connection.
|
||||
* Return number if there already was an existing connection to that ip_port.
|
||||
*/
|
||||
int new_connection(Lossless_UDP * ludp, IP_Port ip_port);
|
||||
int new_connection(Lossless_UDP *ludp, IP_Port ip_port);
|
||||
|
||||
/*
|
||||
* Get connection id from IP_Port.
|
||||
* Return -1 if there are no connections like we are looking for.
|
||||
* Return id if it found it .
|
||||
*/
|
||||
int getconnection_id(Lossless_UDP * ludp, IP_Port ip_port);
|
||||
int getconnection_id(Lossless_UDP *ludp, IP_Port ip_port);
|
||||
|
||||
/*
|
||||
* Returns an int corresponding to the next connection in our imcoming connection list
|
||||
* Return -1 if there are no new incoming connections in the list.
|
||||
*/
|
||||
int incoming_connection(Lossless_UDP * ludp);
|
||||
int incoming_connection(Lossless_UDP *ludp);
|
||||
|
||||
/*
|
||||
* Return -1 if it could not kill the connection.
|
||||
* Return 0 if killed successfully
|
||||
*/
|
||||
int kill_connection(Lossless_UDP * ludp, int connection_id);
|
||||
int kill_connection(Lossless_UDP *ludp, int connection_id);
|
||||
|
||||
/*
|
||||
* Kill connection in seconds seconds.
|
||||
* Return -1 if it can not kill the connection.
|
||||
* Return 0 if it will kill it
|
||||
*/
|
||||
int kill_connection_in(Lossless_UDP * ludp, int connection_id, uint32_t seconds);
|
||||
int kill_connection_in(Lossless_UDP *ludp, int connection_id, uint32_t seconds);
|
||||
|
||||
/*
|
||||
* Returns the ip_port of the corresponding connection.
|
||||
* Return 0 if there is no such connection.
|
||||
*/
|
||||
IP_Port connection_ip(Lossless_UDP * ludp, int connection_id);
|
||||
IP_Port connection_ip(Lossless_UDP *ludp, int connection_id);
|
||||
|
||||
/*
|
||||
* Returns the id of the next packet in the queue
|
||||
* Return -1 if no packet in queue
|
||||
*/
|
||||
char id_packet(Lossless_UDP * ludp, int connection_id);
|
||||
char id_packet(Lossless_UDP *ludp, int connection_id);
|
||||
|
||||
/*
|
||||
* Return 0 if there is no received data in the buffer.
|
||||
* Return length of received packet if successful
|
||||
*/
|
||||
int read_packet(Lossless_UDP * ludp, int connection_id, uint8_t *data);
|
||||
int read_packet(Lossless_UDP *ludp, int connection_id, uint8_t *data);
|
||||
|
||||
/*
|
||||
* Return 0 if data could not be put in packet queue
|
||||
* Return 1 if data was put into the queue
|
||||
*/
|
||||
int write_packet(Lossless_UDP * ludp, int connection_id, uint8_t *data, uint32_t length);
|
||||
int write_packet(Lossless_UDP *ludp, int connection_id, uint8_t *data, uint32_t length);
|
||||
|
||||
/* Returns the number of packets in the queue waiting to be successfully sent. */
|
||||
uint32_t sendqueue(Lossless_UDP * ludp, int connection_id);
|
||||
uint32_t sendqueue(Lossless_UDP *ludp, int connection_id);
|
||||
|
||||
/*
|
||||
* returns the number of packets in the queue waiting to be successfully
|
||||
* read with read_packet(...)
|
||||
*/
|
||||
uint32_t recvqueue(Lossless_UDP * ludp, int connection_id);
|
||||
uint32_t recvqueue(Lossless_UDP *ludp, int connection_id);
|
||||
|
||||
/* Check if connection is connected:
|
||||
* Return 0 no.
|
||||
@ -203,17 +203,17 @@ uint32_t recvqueue(Lossless_UDP * ludp, int connection_id);
|
||||
* Return 3 if fully connected.
|
||||
* Return 4 if timed out and wating to be killed.
|
||||
*/
|
||||
int is_connected(Lossless_UDP * ludp, int connection_id);
|
||||
int is_connected(Lossless_UDP *ludp, int connection_id);
|
||||
|
||||
/* Call this function a couple times per second It's the main loop. */
|
||||
void do_lossless_udp(Lossless_UDP * ludp);
|
||||
void do_lossless_udp(Lossless_UDP *ludp);
|
||||
|
||||
/*
|
||||
* This function sets up LosslessUDP packet handling.
|
||||
*/
|
||||
Lossless_UDP * new_lossless_udp(Networking_Core * net);
|
||||
Lossless_UDP *new_lossless_udp(Networking_Core *net);
|
||||
|
||||
void kill_lossless_udp(Lossless_UDP * ludp);
|
||||
void kill_lossless_udp(Lossless_UDP *ludp);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
@ -624,28 +624,36 @@ static void LANdiscovery(Messenger *m)
|
||||
Messenger *initMessenger(void)
|
||||
{
|
||||
Messenger *m = calloc(1, sizeof(Messenger));
|
||||
|
||||
if ( ! m )
|
||||
return NULL;
|
||||
|
||||
IP ip;
|
||||
ip.i = 0;
|
||||
m->net = new_networking(ip, PORT);
|
||||
|
||||
if (m->net == NULL) {
|
||||
free(m);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
m->net_crypto = new_net_crypto(m->net);
|
||||
|
||||
if (m->net_crypto == NULL) {
|
||||
kill_networking(m->net);
|
||||
free(m);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
m->dht = new_DHT(m->net_crypto);
|
||||
|
||||
if (m->dht == NULL) {
|
||||
kill_net_crypto(m->net_crypto);
|
||||
kill_networking(m->net);
|
||||
free(m);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
new_keys(m->net_crypto);
|
||||
m_set_statusmessage(m, (uint8_t *)"Online", sizeof("Online"));
|
||||
|
||||
@ -681,7 +689,8 @@ void doFriends(Messenger *m)
|
||||
|
||||
for (i = 0; i < m->numfriends; ++i) {
|
||||
if (m->friendlist[i].status == FRIEND_ADDED) {
|
||||
int fr = send_friendrequest(m->dht, m->friendlist[i].client_id, m->friendlist[i].friendrequest_nospam, m->friendlist[i].info,
|
||||
int fr = send_friendrequest(m->dht, m->friendlist[i].client_id, m->friendlist[i].friendrequest_nospam,
|
||||
m->friendlist[i].info,
|
||||
m->friendlist[i].info_size);
|
||||
|
||||
if (fr >= 0) {
|
||||
@ -842,7 +851,8 @@ void doFriends(Messenger *m)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (is_cryptoconnected(m->net_crypto, m->friendlist[i].crypt_connection_id) == 4) { /* if the connection timed out, kill it */
|
||||
if (is_cryptoconnected(m->net_crypto,
|
||||
m->friendlist[i].crypt_connection_id) == 4) { /* if the connection timed out, kill it */
|
||||
crypto_kill(m->net_crypto, m->friendlist[i].crypt_connection_id);
|
||||
m->friendlist[i].crypt_connection_id = -1;
|
||||
set_friend_status(m, i, FRIEND_CONFIRMED);
|
||||
|
@ -112,10 +112,10 @@ typedef struct {
|
||||
} Friend;
|
||||
|
||||
typedef struct Messenger {
|
||||
|
||||
|
||||
Networking_Core *net;
|
||||
Net_Crypto * net_crypto;
|
||||
DHT * dht;
|
||||
Net_Crypto *net_crypto;
|
||||
DHT *dht;
|
||||
Friend_Requests fr;
|
||||
uint8_t name[MAX_NAME_LENGTH];
|
||||
uint16_t name_length;
|
||||
|
@ -37,7 +37,8 @@ int send_friendrequest(DHT *dht, uint8_t *public_key, uint32_t nospam_num, uint8
|
||||
memcpy(temp, &nospam_num, sizeof(nospam_num));
|
||||
memcpy(temp + sizeof(nospam_num), data, length);
|
||||
uint8_t packet[MAX_DATA_SIZE];
|
||||
int len = create_request(dht->c->self_public_key, dht->c->self_secret_key, packet, public_key, temp, length + sizeof(nospam_num),
|
||||
int len = create_request(dht->c->self_public_key, dht->c->self_secret_key, packet, public_key, temp,
|
||||
length + sizeof(nospam_num),
|
||||
32); /* 32 is friend request packet id */
|
||||
|
||||
if (len == -1)
|
||||
@ -79,7 +80,8 @@ uint32_t get_nospam(Friend_Requests *fr)
|
||||
|
||||
|
||||
/* set the function that will be executed when a friend request is received. */
|
||||
void callback_friendrequest(Friend_Requests *fr, void (*function)(uint8_t *, uint8_t *, uint16_t, void *), void *userdata)
|
||||
void callback_friendrequest(Friend_Requests *fr, void (*function)(uint8_t *, uint8_t *, uint16_t, void *),
|
||||
void *userdata)
|
||||
{
|
||||
fr->handle_friendrequest = function;
|
||||
fr->handle_friendrequest_isset = 1;
|
||||
@ -111,9 +113,11 @@ static int request_received(Friend_Requests *fr, uint8_t *client_id)
|
||||
}
|
||||
|
||||
|
||||
static int friendreq_handlepacket(void *object, IP_Port source, uint8_t *source_pubkey, uint8_t *packet, uint32_t length)
|
||||
static int friendreq_handlepacket(void *object, IP_Port source, uint8_t *source_pubkey, uint8_t *packet,
|
||||
uint32_t length)
|
||||
{
|
||||
Friend_Requests *fr = object;
|
||||
|
||||
if (fr->handle_friendrequest_isset == 0)
|
||||
return 1;
|
||||
|
||||
|
@ -40,7 +40,7 @@ typedef struct {
|
||||
/*NOTE: the following is just a temporary fix for the multiple friend requests received at the same time problem
|
||||
TODO: Make this better (This will most likely tie in with the way we will handle spam.)*/
|
||||
|
||||
#define MAX_RECEIVED_STORED 32
|
||||
#define MAX_RECEIVED_STORED 32
|
||||
|
||||
uint8_t received_requests[MAX_RECEIVED_STORED][crypto_box_PUBLICKEYBYTES];
|
||||
uint16_t received_requests_index;
|
||||
@ -57,7 +57,8 @@ uint32_t get_nospam(Friend_Requests *fr);
|
||||
|
||||
/* set the function that will be executed when a friend request for us is received.
|
||||
function format is function(uint8_t * public_key, uint8_t * data, uint16_t length) */
|
||||
void callback_friendrequest(Friend_Requests *fr, void (*function)(uint8_t *, uint8_t *, uint16_t, void *), void *userdata);
|
||||
void callback_friendrequest(Friend_Requests *fr, void (*function)(uint8_t *, uint8_t *, uint16_t, void *),
|
||||
void *userdata);
|
||||
|
||||
/* sets up friendreq packet handlers */
|
||||
void friendreq_init(Friend_Requests *fr, Net_Crypto *c);
|
||||
|
@ -212,7 +212,8 @@ int write_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data, uin
|
||||
request_id is the id of the request (32 = friend request, 254 = ping request)
|
||||
returns -1 on failure
|
||||
returns the length of the created packet on success */
|
||||
int create_request(uint8_t *send_public_key, uint8_t *send_secret_key, uint8_t *packet, uint8_t *recv_public_key, uint8_t *data, uint32_t length, uint8_t request_id)
|
||||
int create_request(uint8_t *send_public_key, uint8_t *send_secret_key, uint8_t *packet, uint8_t *recv_public_key,
|
||||
uint8_t *data, uint32_t length, uint8_t request_id)
|
||||
{
|
||||
if (MAX_DATA_SIZE < length + 1 + crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1 + ENCRYPTION_PADDING)
|
||||
return -1;
|
||||
@ -240,7 +241,8 @@ int create_request(uint8_t *send_public_key, uint8_t *send_secret_key, uint8_t *
|
||||
in data if a friend or ping request was sent to us and returns the length of the data.
|
||||
packet is the request packet and length is its length
|
||||
return -1 if not valid request. */
|
||||
static int handle_request(Net_Crypto *c, uint8_t *public_key, uint8_t *data, uint8_t *request_id, uint8_t *packet, uint16_t length)
|
||||
static int handle_request(Net_Crypto *c, uint8_t *public_key, uint8_t *data, uint8_t *request_id, uint8_t *packet,
|
||||
uint16_t length)
|
||||
{
|
||||
|
||||
if (length > crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1 + ENCRYPTION_PADDING &&
|
||||
@ -274,6 +276,7 @@ void cryptopacket_registerhandler(Net_Crypto *c, uint8_t byte, cryptopacket_hand
|
||||
static int cryptopacket_handle(void *object, IP_Port source, uint8_t *packet, uint32_t length)
|
||||
{
|
||||
DHT *dht = object;
|
||||
|
||||
if (packet[0] == 32) {
|
||||
if (length <= crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1 + ENCRYPTION_PADDING ||
|
||||
length > MAX_DATA_SIZE + ENCRYPTION_PADDING)
|
||||
@ -290,7 +293,8 @@ static int cryptopacket_handle(void *object, IP_Port source, uint8_t *packet, ui
|
||||
|
||||
if (!dht->c->cryptopackethandlers[number].function) return 1;
|
||||
|
||||
dht->c->cryptopackethandlers[number].function(dht->c->cryptopackethandlers[number].object, source, public_key, data, len);
|
||||
dht->c->cryptopackethandlers[number].function(dht->c->cryptopackethandlers[number].object, source, public_key, data,
|
||||
len);
|
||||
|
||||
} else { /* if request is not for us, try routing it. */
|
||||
if (route_packet(dht, packet + 1, packet, length) == length) //NOTE
|
||||
@ -304,7 +308,8 @@ static int cryptopacket_handle(void *object, IP_Port source, uint8_t *packet, ui
|
||||
/* Send a crypto handshake packet containing an encrypted secret nonce and session public key
|
||||
to peer with connection_id and public_key
|
||||
the packet is encrypted with a random nonce which is sent in plain text with the packet */
|
||||
static int send_cryptohandshake(Net_Crypto *c, int connection_id, uint8_t *public_key, uint8_t *secret_nonce, uint8_t *session_key)
|
||||
static int send_cryptohandshake(Net_Crypto *c, int connection_id, uint8_t *public_key, uint8_t *secret_nonce,
|
||||
uint8_t *session_key)
|
||||
{
|
||||
uint8_t temp_data[MAX_DATA_SIZE];
|
||||
uint8_t temp[crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES];
|
||||
@ -323,7 +328,8 @@ static int send_cryptohandshake(Net_Crypto *c, int connection_id, uint8_t *publi
|
||||
temp_data[0] = 2;
|
||||
memcpy(temp_data + 1, c->self_public_key, crypto_box_PUBLICKEYBYTES);
|
||||
memcpy(temp_data + 1 + crypto_box_PUBLICKEYBYTES, nonce, crypto_box_NONCEBYTES);
|
||||
return write_packet(c->lossless_udp, connection_id, temp_data, len + 1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES);
|
||||
return write_packet(c->lossless_udp, connection_id, temp_data,
|
||||
len + 1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES);
|
||||
}
|
||||
|
||||
/* Extract secret nonce, session public key and public_key from a packet(data) with length length
|
||||
@ -431,7 +437,7 @@ int crypto_connect(Net_Crypto *c, uint8_t *public_key, IP_Port ip_port)
|
||||
++c->crypto_connections_length;
|
||||
|
||||
if (send_cryptohandshake(c, id, public_key, c->crypto_connections[i].recv_nonce,
|
||||
c->crypto_connections[i].sessionpublic_key) == 1) {
|
||||
c->crypto_connections[i].sessionpublic_key) == 1) {
|
||||
increment_nonce(c->crypto_connections[i].recv_nonce);
|
||||
return i;
|
||||
}
|
||||
@ -456,7 +462,8 @@ int crypto_inbound(Net_Crypto *c, uint8_t *public_key, uint8_t *secret_nonce, ui
|
||||
|
||||
for (i = 0; i < MAX_INCOMING; ++i) {
|
||||
if (c->incoming_connections[i] != -1) {
|
||||
if (is_connected(c->lossless_udp, c->incoming_connections[i]) == 4 || is_connected(c->lossless_udp, c->incoming_connections[i]) == 0) {
|
||||
if (is_connected(c->lossless_udp, c->incoming_connections[i]) == 4
|
||||
|| is_connected(c->lossless_udp, c->incoming_connections[i]) == 0) {
|
||||
kill_connection(c->lossless_udp, c->incoming_connections[i]);
|
||||
c->incoming_connections[i] = -1;
|
||||
continue;
|
||||
@ -509,7 +516,8 @@ int crypto_kill(Net_Crypto *c, int crypt_connection_id)
|
||||
/* accept an incoming connection using the parameters provided by crypto_inbound
|
||||
return -1 if not successful
|
||||
returns the crypt_connection_id if successful */
|
||||
int accept_crypto_inbound(Net_Crypto *c, int connection_id, uint8_t *public_key, uint8_t *secret_nonce, uint8_t *session_key)
|
||||
int accept_crypto_inbound(Net_Crypto *c, int connection_id, uint8_t *public_key, uint8_t *secret_nonce,
|
||||
uint8_t *session_key)
|
||||
{
|
||||
uint32_t i;
|
||||
|
||||
@ -549,7 +557,8 @@ int accept_crypto_inbound(Net_Crypto *c, int connection_id, uint8_t *public_key,
|
||||
encrypt_precompute(c->crypto_connections[i].peersessionpublic_key,
|
||||
c->crypto_connections[i].sessionsecret_key,
|
||||
c->crypto_connections[i].shared_key);
|
||||
c->crypto_connections[i].status = CONN_ESTABLISHED; /* connection status needs to be 3 for write_cryptpacket() to work */
|
||||
c->crypto_connections[i].status =
|
||||
CONN_ESTABLISHED; /* connection status needs to be 3 for write_cryptpacket() to work */
|
||||
write_cryptpacket(c, i, ((uint8_t *)&zero), sizeof(zero));
|
||||
c->crypto_connections[i].status = CONN_NOT_CONFIRMED; /* set it to its proper value right after. */
|
||||
return i;
|
||||
@ -651,12 +660,14 @@ static void receive_crypto(Net_Crypto *c)
|
||||
encrypt_precompute(c->crypto_connections[i].peersessionpublic_key,
|
||||
c->crypto_connections[i].sessionsecret_key,
|
||||
c->crypto_connections[i].shared_key);
|
||||
c->crypto_connections[i].status = CONN_ESTABLISHED; /* connection status needs to be 3 for write_cryptpacket() to work */
|
||||
c->crypto_connections[i].status =
|
||||
CONN_ESTABLISHED; /* connection status needs to be 3 for write_cryptpacket() to work */
|
||||
write_cryptpacket(c, i, ((uint8_t *)&zero), sizeof(zero));
|
||||
c->crypto_connections[i].status = CONN_NOT_CONFIRMED; /* set it to its proper value right after. */
|
||||
}
|
||||
}
|
||||
} else if (id_packet(c->lossless_udp, c->crypto_connections[i].number) != -1) { // This should not happen kill the connection if it does
|
||||
} else if (id_packet(c->lossless_udp,
|
||||
c->crypto_connections[i].number) != -1) { // This should not happen kill the connection if it does
|
||||
crypto_kill(c, i);
|
||||
return;
|
||||
}
|
||||
@ -697,16 +708,21 @@ static void receive_crypto(Net_Crypto *c)
|
||||
|
||||
/* run this to (re)initialize net_crypto
|
||||
sets all the global connection variables to their default values. */
|
||||
Net_Crypto * new_net_crypto(Networking_Core * net)
|
||||
Net_Crypto *new_net_crypto(Networking_Core *net)
|
||||
{
|
||||
if (net == NULL)
|
||||
return NULL;
|
||||
Net_Crypto * temp = calloc(1, sizeof(Net_Crypto));
|
||||
|
||||
Net_Crypto *temp = calloc(1, sizeof(Net_Crypto));
|
||||
|
||||
if (temp == NULL)
|
||||
return NULL;
|
||||
return NULL;
|
||||
|
||||
temp->lossless_udp = new_lossless_udp(net);
|
||||
|
||||
if (temp->lossless_udp == NULL)
|
||||
return NULL;
|
||||
return NULL;
|
||||
|
||||
memset(temp->incoming_connections, -1 , sizeof(int) * MAX_INCOMING);
|
||||
return temp;
|
||||
}
|
||||
@ -722,7 +738,8 @@ static void kill_timedout(Net_Crypto *c)
|
||||
uint32_t i;
|
||||
|
||||
for (i = 0; i < c->crypto_connections_length; ++i) {
|
||||
if (c->crypto_connections[i].status != CONN_NO_CONNECTION && is_connected(c->lossless_udp, c->crypto_connections[i].number) == 4)
|
||||
if (c->crypto_connections[i].status != CONN_NO_CONNECTION
|
||||
&& is_connected(c->lossless_udp, c->crypto_connections[i].number) == 4)
|
||||
c->crypto_connections[i].status = CONN_TIMED_OUT;
|
||||
else if (is_connected(c->lossless_udp, c->crypto_connections[i].number) == 4) {
|
||||
kill_connection(c->lossless_udp, c->crypto_connections[i].number);
|
||||
@ -745,9 +762,10 @@ void kill_net_crypto(Net_Crypto *c)
|
||||
uint32_t i;
|
||||
|
||||
for (i = 0; i < c->crypto_connections_length; ++i) {
|
||||
crypto_kill(c, i);
|
||||
crypto_kill(c, i);
|
||||
}
|
||||
|
||||
kill_lossless_udp(c->lossless_udp);
|
||||
memset(c, 0, sizeof(Net_Crypto));
|
||||
free(c);
|
||||
free(c);
|
||||
}
|
||||
|
@ -47,15 +47,16 @@ typedef struct {
|
||||
|
||||
} Crypto_Connection;
|
||||
|
||||
typedef int (*cryptopacket_handler_callback)(void * object, IP_Port ip_port, uint8_t *source_pubkey, uint8_t *data, uint32_t len);
|
||||
typedef int (*cryptopacket_handler_callback)(void *object, IP_Port ip_port, uint8_t *source_pubkey, uint8_t *data,
|
||||
uint32_t len);
|
||||
|
||||
typedef struct {
|
||||
cryptopacket_handler_callback function;
|
||||
void * object;
|
||||
}Cryptopacket_Handles;
|
||||
void *object;
|
||||
} Cryptopacket_Handles;
|
||||
|
||||
typedef struct {
|
||||
Lossless_UDP * lossless_udp;
|
||||
Lossless_UDP *lossless_udp;
|
||||
|
||||
Crypto_Connection *crypto_connections;
|
||||
|
||||
@ -127,11 +128,12 @@ int write_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data, uin
|
||||
request_id is the id of the request (32 = friend request, 254 = ping request)
|
||||
returns -1 on failure
|
||||
returns the length of the created packet on success */
|
||||
int create_request(uint8_t *send_public_key, uint8_t *send_secret_key, uint8_t *packet, uint8_t *recv_public_key, uint8_t *data, uint32_t length, uint8_t request_id);
|
||||
int create_request(uint8_t *send_public_key, uint8_t *send_secret_key, uint8_t *packet, uint8_t *recv_public_key,
|
||||
uint8_t *data, uint32_t length, uint8_t request_id);
|
||||
|
||||
|
||||
/* Function to call when request beginning with byte is received */
|
||||
void cryptopacket_registerhandler(Net_Crypto *c, uint8_t byte, cryptopacket_handler_callback cb, void * object);
|
||||
void cryptopacket_registerhandler(Net_Crypto *c, uint8_t byte, cryptopacket_handler_callback cb, void *object);
|
||||
|
||||
/* Start a secure connection with other peer who has public_key and ip_port
|
||||
returns -1 if failure
|
||||
@ -155,7 +157,8 @@ int crypto_inbound(Net_Crypto *c, uint8_t *public_key, uint8_t *secret_nonce, ui
|
||||
/* accept an incoming connection using the parameters provided by crypto_inbound
|
||||
return -1 if not successful
|
||||
returns the crypt_connection_id if successful */
|
||||
int accept_crypto_inbound(Net_Crypto *c, int connection_id, uint8_t *public_key, uint8_t *secret_nonce, uint8_t *session_key);
|
||||
int accept_crypto_inbound(Net_Crypto *c, int connection_id, uint8_t *public_key, uint8_t *secret_nonce,
|
||||
uint8_t *session_key);
|
||||
|
||||
/* return 0 if no connection, 1 we have sent a handshake, 2 if connexion is not confirmed yet
|
||||
(we have received a handshake but no empty data packet), 3 if the connection is established.
|
||||
@ -177,7 +180,7 @@ void load_keys(Net_Crypto *c, uint8_t *keys);
|
||||
|
||||
/* create new instance of Net_Crypto
|
||||
sets all the global connection variables to their default values. */
|
||||
Net_Crypto * new_net_crypto(Networking_Core * net);
|
||||
Net_Crypto *new_net_crypto(Networking_Core *net);
|
||||
|
||||
/* main loop */
|
||||
void do_net_crypto(Net_Crypto *c);
|
||||
|
@ -86,13 +86,13 @@ static int receivepacket(int sock, IP_Port *ip_port, uint8_t *data, uint32_t *le
|
||||
return 0;
|
||||
}
|
||||
|
||||
void networking_registerhandler(Networking_Core * net, uint8_t byte, packet_handler_callback cb, void * object)
|
||||
void networking_registerhandler(Networking_Core *net, uint8_t byte, packet_handler_callback cb, void *object)
|
||||
{
|
||||
net->packethandlers[byte].function = cb;
|
||||
net->packethandlers[byte].object = object;
|
||||
}
|
||||
|
||||
void networking_poll(Networking_Core * net)
|
||||
void networking_poll(Networking_Core *net)
|
||||
{
|
||||
IP_Port ip_port;
|
||||
uint8_t data[MAX_UDP_PACKET_SIZE];
|
||||
@ -112,6 +112,7 @@ static void at_startup(void)
|
||||
{
|
||||
if (at_startup_ran != 0)
|
||||
return;
|
||||
|
||||
#ifdef WIN32
|
||||
WSADATA wsaData;
|
||||
|
||||
@ -121,7 +122,7 @@ static void at_startup(void)
|
||||
#else
|
||||
srandom((uint32_t)current_time());
|
||||
#endif
|
||||
srand((uint32_t)current_time());
|
||||
srand((uint32_t)current_time());
|
||||
at_startup_ran = 1;
|
||||
}
|
||||
|
||||
@ -130,7 +131,7 @@ static void at_shutdown(void)
|
||||
{
|
||||
#ifdef WIN32
|
||||
WSACleanup();
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
*/
|
||||
|
||||
@ -140,13 +141,15 @@ static void at_shutdown(void)
|
||||
port is in host byte order (this means don't worry about it)
|
||||
returns Networking_Core object if no problems
|
||||
returns NULL if there are problems */
|
||||
Networking_Core * new_networking(IP ip, uint16_t port)
|
||||
Networking_Core *new_networking(IP ip, uint16_t port)
|
||||
{
|
||||
at_startup();
|
||||
/* initialize our socket */
|
||||
Networking_Core * temp = calloc(1, sizeof(Networking_Core));
|
||||
Networking_Core *temp = calloc(1, sizeof(Networking_Core));
|
||||
|
||||
if (temp == NULL)
|
||||
return NULL;
|
||||
return NULL;
|
||||
|
||||
temp->sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
|
||||
|
||||
/* Check for socket error */
|
||||
@ -200,7 +203,7 @@ Networking_Core * new_networking(IP ip, uint16_t port)
|
||||
}
|
||||
|
||||
/* function to cleanup networking stuff */
|
||||
void kill_networking(Networking_Core * net)
|
||||
void kill_networking(Networking_Core *net)
|
||||
{
|
||||
#ifdef WIN32
|
||||
closesocket(net->sock);
|
||||
|
@ -99,18 +99,18 @@ typedef struct {
|
||||
/* Function to receive data, ip and port of sender is put into ip_port
|
||||
the packet data into data
|
||||
the packet length into length. */
|
||||
typedef int (*packet_handler_callback)(void * object, IP_Port ip_port, uint8_t *data, uint32_t len);
|
||||
typedef int (*packet_handler_callback)(void *object, IP_Port ip_port, uint8_t *data, uint32_t len);
|
||||
|
||||
typedef struct {
|
||||
packet_handler_callback function;
|
||||
void * object;
|
||||
}Packet_Handles;
|
||||
void *object;
|
||||
} Packet_Handles;
|
||||
|
||||
typedef struct {
|
||||
Packet_Handles packethandlers[256];
|
||||
/* our UDP socket */
|
||||
int sock;
|
||||
}Networking_Core;
|
||||
} Networking_Core;
|
||||
|
||||
/* returns current time in milleseconds since the epoch. */
|
||||
uint64_t current_time(void);
|
||||
@ -125,10 +125,10 @@ uint32_t random_int(void);
|
||||
int sendpacket(int sock, IP_Port ip_port, uint8_t *data, uint32_t length);
|
||||
|
||||
/* Function to call when packet beginning with byte is received */
|
||||
void networking_registerhandler(Networking_Core * net, uint8_t byte, packet_handler_callback cb, void * object);
|
||||
void networking_registerhandler(Networking_Core *net, uint8_t byte, packet_handler_callback cb, void *object);
|
||||
|
||||
/* call this several times a second */
|
||||
void networking_poll(Networking_Core * net);
|
||||
void networking_poll(Networking_Core *net);
|
||||
|
||||
/* initialize networking
|
||||
bind to ip and port
|
||||
@ -136,10 +136,10 @@ void networking_poll(Networking_Core * net);
|
||||
port is in host byte order (this means don't worry about it)
|
||||
returns 0 if no problems
|
||||
returns -1 if there were problems */
|
||||
Networking_Core * new_networking(IP ip, uint16_t port);
|
||||
Networking_Core *new_networking(IP ip, uint16_t port);
|
||||
|
||||
/* function to cleanup networking stuff(doesn't do much right now) */
|
||||
void kill_networking(Networking_Core * net);
|
||||
void kill_networking(Networking_Core *net);
|
||||
|
||||
/*
|
||||
resolve_addr():
|
||||
|
27
core/ping.c
27
core/ping.c
@ -29,12 +29,12 @@ typedef struct {
|
||||
size_t pos_pings;
|
||||
} PING;
|
||||
|
||||
void * new_ping(void)
|
||||
void *new_ping(void)
|
||||
{
|
||||
return calloc(1, sizeof(PING));
|
||||
}
|
||||
|
||||
void kill_ping(void * ping)
|
||||
void kill_ping(void *ping)
|
||||
{
|
||||
free(ping);
|
||||
}
|
||||
@ -44,9 +44,9 @@ static bool is_timeout(uint64_t time)
|
||||
return (time + PING_TIMEOUT) < now();
|
||||
}
|
||||
|
||||
static void remove_timeouts(void * ping) // O(n)
|
||||
static void remove_timeouts(void *ping) // O(n)
|
||||
{
|
||||
PING * png = ping;
|
||||
PING *png = ping;
|
||||
size_t i, id;
|
||||
size_t new_pos = png->pos_pings;
|
||||
size_t new_num = png->num_pings;
|
||||
@ -69,9 +69,9 @@ static void remove_timeouts(void * ping) // O(n)
|
||||
png->pos_pings = new_pos % PING_NUM_MAX;
|
||||
}
|
||||
|
||||
uint64_t add_ping(void * ping, IP_Port ipp) // O(n)
|
||||
uint64_t add_ping(void *ping, IP_Port ipp) // O(n)
|
||||
{
|
||||
PING * png = ping;
|
||||
PING *png = ping;
|
||||
size_t p;
|
||||
|
||||
remove_timeouts(ping);
|
||||
@ -93,9 +93,10 @@ uint64_t add_ping(void * ping, IP_Port ipp) // O(n)
|
||||
return png->pings[p].id;
|
||||
}
|
||||
|
||||
bool is_pinging(void * ping, 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;
|
||||
PING *png = ping;
|
||||
|
||||
if (ipp.ip.i == 0 && ping_id == 0)
|
||||
return false;
|
||||
|
||||
@ -115,7 +116,7 @@ bool is_pinging(void * ping, IP_Port ipp, uint64_t ping_id) // O(n) TODO: repl
|
||||
return false;
|
||||
}
|
||||
|
||||
int send_ping_request(void * ping, Net_Crypto *c, 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;
|
||||
@ -169,9 +170,9 @@ int send_ping_response(Net_Crypto *c, IP_Port ipp, clientid_t *client_id, uint64
|
||||
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)
|
||||
{
|
||||
DHT * dht = object;
|
||||
DHT *dht = object;
|
||||
pingreq_t *p = (pingreq_t *) packet;
|
||||
int rc;
|
||||
uint64_t ping_id;
|
||||
@ -197,9 +198,9 @@ int handle_ping_request(void * object, IP_Port source, uint8_t *packet, uint32_t
|
||||
return 0;
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
DHT * dht = object;
|
||||
DHT *dht = object;
|
||||
pingres_t *p = (pingres_t *) packet;
|
||||
int rc;
|
||||
uint64_t ping_id;
|
||||
|
14
core/ping.h
14
core/ping.h
@ -7,11 +7,11 @@
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
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);
|
||||
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);
|
||||
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);
|
||||
|
@ -44,7 +44,7 @@
|
||||
|
||||
|
||||
|
||||
void manage_keys(DHT * dht)
|
||||
void manage_keys(DHT *dht)
|
||||
{
|
||||
const uint32_t KEYS_SIZE = crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES;
|
||||
uint8_t keys[KEYS_SIZE];
|
||||
@ -85,7 +85,7 @@ int main(int argc, char *argv[])
|
||||
//bind to ip 0.0.0.0:PORT
|
||||
IP ip;
|
||||
ip.i = 0;
|
||||
DHT * dht = new_DHT(new_net_crypto(new_networking(ip, PORT)));
|
||||
DHT *dht = new_DHT(new_net_crypto(new_networking(ip, PORT)));
|
||||
init_cryptopackets(dht);
|
||||
manage_keys(dht);
|
||||
printf("Public key: ");
|
||||
|
@ -77,7 +77,7 @@ and connect to them.
|
||||
returns 1 if the connection to the DHT is up
|
||||
returns -1 if all attempts failed
|
||||
*/
|
||||
int connect_to_servers(DHT * dht, struct server_info_s *info)
|
||||
int connect_to_servers(DHT *dht, struct server_info_s *info)
|
||||
{
|
||||
int i;
|
||||
int c;
|
||||
@ -115,7 +115,7 @@ int connect_to_servers(DHT * dht, struct server_info_s *info)
|
||||
return 0;
|
||||
}
|
||||
|
||||
void manage_keys(DHT * dht, char *keys_file)
|
||||
void manage_keys(DHT *dht, char *keys_file)
|
||||
{
|
||||
const uint32_t KEYS_SIZE = crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES;
|
||||
uint8_t keys[KEYS_SIZE];
|
||||
@ -293,13 +293,14 @@ int main(int argc, char *argv[])
|
||||
printf("Please specify a configuration file.\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
server_conf = configure_server(argv[1]);
|
||||
|
||||
|
||||
/* initialize networking
|
||||
bind to ip 0.0.0.0:PORT */
|
||||
IP ip;
|
||||
ip.i = 0;
|
||||
DHT * dht = new_DHT(new_net_crypto(new_networking(ip, server_conf.port)));
|
||||
DHT *dht = new_DHT(new_net_crypto(new_networking(ip, server_conf.port)));
|
||||
/* Read the config file */
|
||||
printf("PID file: %s\n", server_conf.pid_file);
|
||||
printf("Key file: %s\n", server_conf.keys_file);
|
||||
|
@ -48,7 +48,7 @@
|
||||
|
||||
#define PORT 33445
|
||||
|
||||
void print_clientlist(DHT * dht)
|
||||
void print_clientlist(DHT *dht)
|
||||
{
|
||||
uint32_t i, j;
|
||||
IP_Port p_ip;
|
||||
@ -71,7 +71,7 @@ void print_clientlist(DHT * dht)
|
||||
}
|
||||
}
|
||||
|
||||
void print_friendlist(DHT * dht)
|
||||
void print_friendlist(DHT *dht)
|
||||
{
|
||||
uint32_t i, j, k;
|
||||
IP_Port p_ip;
|
||||
@ -104,7 +104,7 @@ void print_friendlist(DHT * dht)
|
||||
printf("\nIP: %u.%u.%u.%u:%u", p_ip.ip.c[0], p_ip.ip.c[1], p_ip.ip.c[2], p_ip.ip.c[3], ntohs(p_ip.port));
|
||||
printf("\nTimestamp: %llu", (long long unsigned int) dht->friends_list[k].client_list[i].timestamp);
|
||||
printf("\nLast pinged: %llu\n", (long long unsigned int) dht->friends_list[k].client_list[i].last_pinged);
|
||||
p_ip =dht->friends_list[k].client_list[i].ret_ip_port;
|
||||
p_ip = dht->friends_list[k].client_list[i].ret_ip_port;
|
||||
printf("ret IP: %u.%u.%u.%u:%u\n", p_ip.ip.c[0], p_ip.ip.c[1], p_ip.ip.c[2], p_ip.ip.c[3], ntohs(p_ip.port));
|
||||
printf("Timestamp: %llu\n", (long long unsigned int)dht->friends_list[k].client_list[i].ret_timestamp);
|
||||
}
|
||||
@ -134,9 +134,10 @@ int main(int argc, char *argv[])
|
||||
/* bind to ip 0.0.0.0:PORT */
|
||||
IP ip;
|
||||
ip.i = 0;
|
||||
|
||||
DHT * dht = new_DHT(new_net_crypto(new_networking(ip, PORT)));
|
||||
|
||||
DHT *dht = new_DHT(new_net_crypto(new_networking(ip, PORT)));
|
||||
init_cryptopackets(dht);
|
||||
|
||||
if (argc < 4) {
|
||||
printf("usage %s ip port public_key\n", argv[0]);
|
||||
exit(0);
|
||||
@ -198,5 +199,6 @@ int main(int argc, char *argv[])
|
||||
print_friendlist(dht);
|
||||
c_sleep(300);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -124,25 +124,25 @@ void printconnection(int connection_id)
|
||||
/*run doLossless_UDP(); */
|
||||
//void Lossless_UDP()
|
||||
//{
|
||||
/* IP_Port ip_port;
|
||||
uint8_t data[MAX_UDP_PACKET_SIZE];
|
||||
uint32_t length;
|
||||
while (receivepacket(&ip_port, data, &length) != -1) {
|
||||
printf("packet with length: %u\n", length); */
|
||||
/* if(rand() % 3 != 1)//add packet loss
|
||||
{ */
|
||||
/*
|
||||
if (LosslessUDP_handlepacket(data, length, ip_port))
|
||||
printpacket(data, length, ip_port);
|
||||
else
|
||||
printf("Received handled packet with length: %u\n", length); //printconnection(0); */
|
||||
/* IP_Port ip_port;
|
||||
uint8_t data[MAX_UDP_PACKET_SIZE];
|
||||
uint32_t length;
|
||||
while (receivepacket(&ip_port, data, &length) != -1) {
|
||||
printf("packet with length: %u\n", length); */
|
||||
/* if(rand() % 3 != 1)//add packet loss
|
||||
{ */
|
||||
/*
|
||||
if (LosslessUDP_handlepacket(data, length, ip_port))
|
||||
printpacket(data, length, ip_port);
|
||||
else
|
||||
printf("Received handled packet with length: %u\n", length); //printconnection(0); */
|
||||
|
||||
/* } */
|
||||
/* }*/
|
||||
/* } */
|
||||
/* }*/
|
||||
|
||||
//networking_poll();
|
||||
//networking_poll();
|
||||
|
||||
//doLossless_UDP();
|
||||
//doLossless_UDP();
|
||||
|
||||
//}
|
||||
|
||||
@ -166,7 +166,7 @@ int main(int argc, char *argv[])
|
||||
/* bind to ip 0.0.0.0:PORT */
|
||||
IP ip;
|
||||
ip.i = 0;
|
||||
Lossless_UDP * ludp = new_lossless_udp(new_networking(ip, PORT));
|
||||
Lossless_UDP *ludp = new_lossless_udp(new_networking(ip, PORT));
|
||||
perror("Initialization");
|
||||
IP_Port serverip;
|
||||
serverip.ip.i = inet_addr(argv[1]);
|
||||
@ -203,6 +203,7 @@ int main(int argc, char *argv[])
|
||||
/* printconnection(connection); */
|
||||
networking_poll(ludp->net);
|
||||
do_lossless_udp(ludp);
|
||||
|
||||
if (is_connected(ludp, connection) == 3) {
|
||||
|
||||
if (write_packet(ludp, connection, buffer, read)) {
|
||||
|
@ -124,20 +124,20 @@ void printconnection(int connection_id)
|
||||
// uint8_t data[MAX_UDP_PACKET_SIZE];
|
||||
// uint32_t length;
|
||||
// while (receivepacket(&ip_port, data, &length) != -1) {
|
||||
//if(rand() % 3 != 1)//add packet loss
|
||||
//{
|
||||
//if(rand() % 3 != 1)//add packet loss
|
||||
//{
|
||||
// if (LosslessUDP_handlepacket(data, length, ip_port)) {
|
||||
// printpacket(data, length, ip_port);
|
||||
// } else {
|
||||
//printconnection(0);
|
||||
//printconnection(0);
|
||||
// printf("Received handled packet with length: %u\n", length);
|
||||
// }
|
||||
//}
|
||||
//}
|
||||
// }
|
||||
|
||||
// networking_poll();
|
||||
// networking_poll();
|
||||
|
||||
//doLossless_UDP();
|
||||
//doLossless_UDP();
|
||||
//}
|
||||
|
||||
|
||||
@ -161,7 +161,7 @@ int main(int argc, char *argv[])
|
||||
//bind to ip 0.0.0.0:PORT
|
||||
IP ip;
|
||||
ip.i = 0;
|
||||
Lossless_UDP * ludp = new_lossless_udp(new_networking(ip, PORT));
|
||||
Lossless_UDP *ludp = new_lossless_udp(new_networking(ip, PORT));
|
||||
perror("Initialization");
|
||||
|
||||
int connection;
|
||||
|
@ -123,7 +123,7 @@ static void chat_onStatusChange(ToxWindow *self, int num, uint8_t *status, uint1
|
||||
|
||||
status[len - 1] = '\0';
|
||||
fix_name(status);
|
||||
|
||||
|
||||
wattron(ctx->history, COLOR_PAIR(3));
|
||||
wprintw(ctx->history, "* Your partner changed status to '%s'\n", status);
|
||||
wattroff(ctx->history, COLOR_PAIR(3));
|
||||
|
@ -93,7 +93,7 @@ static Messenger *init_tox()
|
||||
int init_connection(Messenger *m)
|
||||
{
|
||||
FILE *fp = NULL;
|
||||
|
||||
|
||||
if (DHT_isconnected(m->dht))
|
||||
return 0;
|
||||
|
||||
@ -297,7 +297,7 @@ int main(int argc, char *argv[])
|
||||
strcpy(DATA_FILE, user_config_dir);
|
||||
strcat(DATA_FILE, CONFIGDIR);
|
||||
strcat(DATA_FILE, "data");
|
||||
|
||||
|
||||
SRVLIST_FILE = malloc(strlen(user_config_dir) + strlen(CONFIGDIR) + strlen("DHTservers") + 1);
|
||||
strcpy(SRVLIST_FILE, user_config_dir);
|
||||
strcat(SRVLIST_FILE, CONFIGDIR);
|
||||
|
@ -365,7 +365,9 @@ static void execute(ToxWindow *self, Messenger *m, char *u_cmd)
|
||||
cmd[i] = '\0';
|
||||
|
||||
int j = i;
|
||||
|
||||
while (++j < MAX_STR_SIZE && isspace(cmd[j]));
|
||||
|
||||
i = j - 1;
|
||||
|
||||
numargs++;
|
||||
|
@ -88,23 +88,25 @@ int add_window(Messenger *m, ToxWindow w)
|
||||
{
|
||||
if (LINES < 2)
|
||||
return -1;
|
||||
|
||||
|
||||
int i;
|
||||
for(i = 0; i < MAX_WINDOWS_NUM; i++) {
|
||||
if (windows[i].window)
|
||||
|
||||
for (i = 0; i < MAX_WINDOWS_NUM; i++) {
|
||||
if (windows[i].window)
|
||||
continue;
|
||||
|
||||
|
||||
w.window = newwin(LINES - 2, COLS, 0, 0);
|
||||
|
||||
if (w.window == NULL)
|
||||
return -1;
|
||||
|
||||
windows[i] = w;
|
||||
w.onInit(&w, m);
|
||||
|
||||
active_window = windows+i;
|
||||
|
||||
active_window = windows + i;
|
||||
return i;
|
||||
}
|
||||
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
@ -113,8 +115,10 @@ void del_window(ToxWindow *w)
|
||||
{
|
||||
active_window = windows; // Go to prompt screen
|
||||
delwin(w->window);
|
||||
|
||||
if (w->x)
|
||||
free(w->x);
|
||||
|
||||
w->window = NULL;
|
||||
memset(w, 0, sizeof(ToxWindow));
|
||||
clear();
|
||||
@ -124,19 +128,19 @@ void del_window(ToxWindow *w)
|
||||
/* Shows next window when tab or back-tab is pressed */
|
||||
void set_next_window(int ch)
|
||||
{
|
||||
ToxWindow *end = windows+MAX_WINDOWS_NUM-1;
|
||||
ToxWindow *end = windows + MAX_WINDOWS_NUM - 1;
|
||||
ToxWindow *inf = active_window;
|
||||
while(true) {
|
||||
|
||||
while (true) {
|
||||
if (ch == '\t') {
|
||||
if (++active_window > end)
|
||||
active_window = windows;
|
||||
} else
|
||||
if (--active_window < windows)
|
||||
active_window = end;
|
||||
|
||||
} else if (--active_window < windows)
|
||||
active_window = end;
|
||||
|
||||
if (active_window->window)
|
||||
return;
|
||||
|
||||
|
||||
if (active_window == inf) { // infinite loop check
|
||||
endwin();
|
||||
exit(2);
|
||||
@ -148,14 +152,14 @@ void set_active_window(int index)
|
||||
{
|
||||
if (index < 0 || index >= MAX_WINDOWS_NUM)
|
||||
return;
|
||||
|
||||
active_window = windows+index;
|
||||
|
||||
active_window = windows + index;
|
||||
}
|
||||
|
||||
ToxWindow *init_windows()
|
||||
{
|
||||
int n_prompt = add_window(m, new_prompt());
|
||||
|
||||
|
||||
if (n_prompt == -1
|
||||
|| add_window(m, new_friendlist()) == -1
|
||||
|| add_window(m, new_dhtstatus()) == -1) {
|
||||
@ -166,7 +170,7 @@ ToxWindow *init_windows()
|
||||
|
||||
prompt = &windows[n_prompt];
|
||||
active_window = prompt;
|
||||
|
||||
|
||||
return prompt;
|
||||
}
|
||||
|
||||
@ -189,7 +193,7 @@ static void draw_bar()
|
||||
|
||||
for (i = 0; i < (MAX_WINDOWS_NUM); ++i) {
|
||||
if (windows[i].window) {
|
||||
if (windows+i == active_window)
|
||||
if (windows + i == active_window)
|
||||
attron(A_BOLD);
|
||||
|
||||
odd = (odd + 1) % blinkrate;
|
||||
@ -197,13 +201,13 @@ static void draw_bar()
|
||||
if (windows[i].blink && (odd < (blinkrate / 2)))
|
||||
attron(COLOR_PAIR(3));
|
||||
|
||||
clrtoeol();
|
||||
clrtoeol();
|
||||
printw(" %s", windows[i].title);
|
||||
|
||||
if (windows[i].blink && (odd < (blinkrate / 2)))
|
||||
attroff(COLOR_PAIR(3));
|
||||
|
||||
if (windows+i == active_window) {
|
||||
if (windows + i == active_window) {
|
||||
attroff(A_BOLD);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user