2013-06-26 21:56:15 +08:00
|
|
|
/* DHT.c
|
2013-07-26 09:45:56 +08:00
|
|
|
*
|
|
|
|
* An implementation of the DHT as seen in docs/DHT.txt
|
|
|
|
*
|
|
|
|
* Copyright (C) 2013 Tox project All Rights Reserved.
|
|
|
|
*
|
|
|
|
* This file is part of Tox.
|
|
|
|
*
|
|
|
|
* Tox is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Tox is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with Tox. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
2013-06-27 03:02:42 +08:00
|
|
|
|
2013-06-26 21:56:15 +08:00
|
|
|
#include "DHT.h"
|
2013-06-25 04:50:43 +08:00
|
|
|
|
2013-07-07 23:54:25 +08:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
uint8_t client_id[CLIENT_ID_SIZE];
|
|
|
|
IP_Port ip_port;
|
|
|
|
uint32_t timestamp;
|
|
|
|
uint32_t last_pinged;
|
2013-07-22 01:14:36 +08:00
|
|
|
IP_Port ret_ip_port;/* The ip_port returned by this node for the friend
|
|
|
|
(for nodes in friends_list) or us (for nodes in close_clientlist) */
|
2013-07-23 04:26:10 +08:00
|
|
|
uint32_t ret_timestamp;
|
2013-07-07 23:54:25 +08:00
|
|
|
}Client_data;
|
2013-07-26 16:02:17 +08:00
|
|
|
|
2013-07-20 11:00:10 +08:00
|
|
|
/* maximum number of clients stored per friend. */
|
2013-07-07 23:54:25 +08:00
|
|
|
#define MAX_FRIEND_CLIENTS 8
|
2013-07-26 16:02:17 +08:00
|
|
|
|
2013-07-07 23:54:25 +08:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
uint8_t client_id[CLIENT_ID_SIZE];
|
|
|
|
Client_data client_list[MAX_FRIEND_CLIENTS];
|
2013-07-20 11:00:10 +08:00
|
|
|
uint32_t lastgetnode; /* time at which the last get_nodes request was sent. */
|
2013-07-07 23:54:25 +08:00
|
|
|
|
2013-07-25 07:39:34 +08:00
|
|
|
/*Symetric NAT hole punching stuff*/
|
|
|
|
uint8_t hole_punching; /*0 if not hole punching, 1 if currently hole punching */
|
|
|
|
uint32_t punching_index;
|
|
|
|
uint32_t punching_timestamp;
|
|
|
|
uint64_t NATping_id;
|
|
|
|
uint32_t NATping_timestamp;
|
2013-07-07 23:54:25 +08:00
|
|
|
}Friend;
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
uint8_t client_id[CLIENT_ID_SIZE];
|
|
|
|
IP_Port ip_port;
|
|
|
|
}Node_format;
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
IP_Port ip_port;
|
2013-07-13 04:27:19 +08:00
|
|
|
uint64_t ping_id;
|
2013-07-07 23:54:25 +08:00
|
|
|
uint32_t timestamp;
|
|
|
|
|
|
|
|
}Pinged;
|
|
|
|
|
2013-07-20 11:00:10 +08:00
|
|
|
/* Our client id/public key */
|
2013-07-13 04:27:19 +08:00
|
|
|
uint8_t self_public_key[CLIENT_ID_SIZE];
|
|
|
|
uint8_t self_secret_key[crypto_box_SECRETKEYBYTES];
|
2013-07-07 23:54:25 +08:00
|
|
|
|
2013-07-20 11:00:10 +08:00
|
|
|
/* TODO: Move these out of here and put them into the .c file.
|
|
|
|
A list of the clients mathematically closest to ours. */
|
2013-07-07 23:54:25 +08:00
|
|
|
#define LCLIENT_LIST 32
|
2013-07-13 04:27:19 +08:00
|
|
|
static Client_data close_clientlist[LCLIENT_LIST];
|
2013-07-07 23:54:25 +08:00
|
|
|
|
2013-07-14 23:50:34 +08:00
|
|
|
static Friend * friends_list;
|
2013-07-13 04:27:19 +08:00
|
|
|
static uint16_t num_friends;
|
2013-07-07 23:54:25 +08:00
|
|
|
|
2013-07-20 11:00:10 +08:00
|
|
|
/* The list of ip ports along with the ping_id of what we sent them and a timestamp */
|
2013-07-25 07:39:34 +08:00
|
|
|
#define LPING_ARRAY 256
|
2013-07-07 23:54:25 +08:00
|
|
|
|
2013-07-13 04:27:19 +08:00
|
|
|
static Pinged pings[LPING_ARRAY];
|
2013-07-07 23:54:25 +08:00
|
|
|
|
|
|
|
#define LSEND_NODES_ARRAY LPING_ARRAY/2
|
|
|
|
|
2013-07-13 04:27:19 +08:00
|
|
|
static Pinged send_nodes[LSEND_NODES_ARRAY];
|
2013-07-07 23:54:25 +08:00
|
|
|
|
2013-07-20 11:00:10 +08:00
|
|
|
/* Compares client_id1 and client_id2 with client_id
|
|
|
|
return 0 if both are same distance
|
|
|
|
return 1 if client_id1 is closer
|
|
|
|
return 2 if client_id2 is closer */
|
|
|
|
int id_closest(uint8_t * client_id, uint8_t * client_id1, uint8_t * client_id2) /* tested */
|
2013-06-24 22:42:55 +08:00
|
|
|
{
|
2013-06-25 00:23:46 +08:00
|
|
|
uint32_t i;
|
2013-07-26 16:02:17 +08:00
|
|
|
for(i = 0; i < CLIENT_ID_SIZE; ++i) {
|
|
|
|
if(abs(client_id[i] ^ client_id1[i]) < abs(client_id[i] ^ client_id2[i])) {
|
2013-06-24 22:42:55 +08:00
|
|
|
return 1;
|
2013-07-26 16:02:17 +08:00
|
|
|
} else if(abs(client_id[i] ^ client_id1[i]) > abs(client_id[i] ^ client_id2[i])) {
|
2013-06-24 22:42:55 +08:00
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2013-06-26 00:11:11 +08:00
|
|
|
|
2013-06-24 22:42:55 +08:00
|
|
|
}
|
|
|
|
|
2013-07-20 11:00:10 +08:00
|
|
|
/* check if client with client_id is already in list of length length.
|
|
|
|
if it is set it's corresponding timestamp to current time.
|
|
|
|
if the id is already in the list with a different ip_port, update it.
|
|
|
|
return True(1) or False(0)
|
|
|
|
TODO: maybe optimize this. */
|
2013-07-06 05:00:39 +08:00
|
|
|
int client_in_list(Client_data * list, uint32_t length, uint8_t * client_id, IP_Port ip_port)
|
2013-06-25 00:23:46 +08:00
|
|
|
{
|
2013-07-17 01:20:45 +08:00
|
|
|
uint32_t i;
|
2013-06-26 03:24:31 +08:00
|
|
|
uint32_t temp_time = unix_time();
|
|
|
|
|
2013-07-26 16:02:17 +08:00
|
|
|
for(i = 0; i < length; ++i) {
|
2013-07-23 05:21:58 +08:00
|
|
|
/*If ip_port is assigned to a different client_id replace it*/
|
|
|
|
if(list[i].ip_port.ip.i == ip_port.ip.i &&
|
2013-07-26 16:02:17 +08:00
|
|
|
list[i].ip_port.port == ip_port.port) {
|
2013-07-23 05:21:58 +08:00
|
|
|
memcpy(list[i].client_id, client_id, CLIENT_ID_SIZE);
|
|
|
|
}
|
|
|
|
|
2013-07-26 16:02:17 +08:00
|
|
|
if(memcmp(list[i].client_id, client_id, CLIENT_ID_SIZE) == 0) {
|
2013-07-20 11:00:10 +08:00
|
|
|
/* Refresh the client timestamp. */
|
2013-06-26 03:24:31 +08:00
|
|
|
list[i].timestamp = temp_time;
|
2013-07-17 01:20:45 +08:00
|
|
|
list[i].ip_port.ip.i = ip_port.ip.i;
|
|
|
|
list[i].ip_port.port = ip_port.port;
|
2013-06-25 00:23:46 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
2013-06-26 00:11:11 +08:00
|
|
|
|
2013-06-25 00:23:46 +08:00
|
|
|
}
|
|
|
|
|
2013-07-20 11:00:10 +08:00
|
|
|
/* check if client with client_id is already in node format list of length length.
|
|
|
|
return True(1) or False(0) */
|
2013-07-06 05:00:39 +08:00
|
|
|
int client_in_nodelist(Node_format * list, uint32_t length, uint8_t * client_id)
|
2013-06-25 08:51:07 +08:00
|
|
|
{
|
2013-07-17 01:20:45 +08:00
|
|
|
uint32_t i;
|
2013-07-26 16:02:17 +08:00
|
|
|
for(i = 0; i < length; ++i) {
|
|
|
|
if(memcmp(list[i].client_id, client_id, CLIENT_ID_SIZE) == 0) {
|
2013-06-25 08:51:07 +08:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
2013-06-26 00:11:11 +08:00
|
|
|
|
2013-06-25 08:51:07 +08:00
|
|
|
}
|
|
|
|
|
2013-07-25 07:39:34 +08:00
|
|
|
/*Return the friend number from the client_id
|
|
|
|
Return -1 if failure, number of friend if success*/
|
|
|
|
static int friend_number(uint8_t * client_id)
|
|
|
|
{
|
|
|
|
uint32_t i;
|
2013-07-26 16:02:17 +08:00
|
|
|
for(i = 0; i < num_friends; ++i) {
|
2013-07-25 07:39:34 +08:00
|
|
|
if(memcmp(friends_list[i].client_id, client_id, CLIENT_ID_SIZE) == 0) /* Equal */
|
|
|
|
{
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
2013-06-25 08:51:07 +08:00
|
|
|
|
2013-07-20 11:00:10 +08:00
|
|
|
/* the number of seconds for a non responsive node to become bad. */
|
2013-07-25 07:39:34 +08:00
|
|
|
#define BAD_NODE_TIMEOUT 70
|
2013-07-20 11:00:10 +08:00
|
|
|
/* the max number of nodes to send with send nodes. */
|
2013-06-25 08:51:07 +08:00
|
|
|
#define MAX_SENT_NODES 8
|
|
|
|
|
2013-07-20 11:00:10 +08:00
|
|
|
/* Find MAX_SENT_NODES nodes closest to the client_id for the send nodes request:
|
|
|
|
put them in the nodes_list and return how many were found.
|
|
|
|
TODO: Make this function much more efficient. */
|
2013-07-06 05:00:39 +08:00
|
|
|
int get_close_nodes(uint8_t * client_id, Node_format * nodes_list)
|
2013-06-25 08:51:07 +08:00
|
|
|
{
|
|
|
|
uint32_t i, j, k;
|
|
|
|
int num_nodes=0;
|
|
|
|
uint32_t temp_time = unix_time();
|
2013-07-26 16:02:17 +08:00
|
|
|
for(i = 0; i < LCLIENT_LIST; ++i) {
|
2013-06-25 08:51:07 +08:00
|
|
|
if(close_clientlist[i].timestamp + BAD_NODE_TIMEOUT > temp_time &&
|
2013-07-26 16:02:17 +08:00
|
|
|
!client_in_nodelist(nodes_list, MAX_SENT_NODES,close_clientlist[i].client_id)) {
|
2013-07-20 11:00:10 +08:00
|
|
|
/* if node is good and not already in list. */
|
2013-07-26 16:02:17 +08:00
|
|
|
if(num_nodes < MAX_SENT_NODES) {
|
2013-06-25 08:51:07 +08:00
|
|
|
memcpy(nodes_list[num_nodes].client_id, close_clientlist[i].client_id, CLIENT_ID_SIZE);
|
|
|
|
nodes_list[num_nodes].ip_port = close_clientlist[i].ip_port;
|
|
|
|
num_nodes++;
|
|
|
|
}
|
2013-07-26 16:02:17 +08:00
|
|
|
else for(j = 0; j < MAX_SENT_NODES; ++j) {
|
|
|
|
if(id_closest(client_id, nodes_list[j].client_id, close_clientlist[i].client_id) == 2) {
|
2013-06-25 08:51:07 +08:00
|
|
|
memcpy(nodes_list[j].client_id, close_clientlist[i].client_id, CLIENT_ID_SIZE);
|
|
|
|
nodes_list[j].ip_port = close_clientlist[i].ip_port;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2013-07-26 16:02:17 +08:00
|
|
|
}
|
2013-06-25 08:51:07 +08:00
|
|
|
}
|
2013-07-26 16:02:17 +08:00
|
|
|
for(i = 0; i < num_friends; ++i) {
|
|
|
|
for(j = 0; j < MAX_FRIEND_CLIENTS; ++j) {
|
2013-06-25 08:51:07 +08:00
|
|
|
if(friends_list[i].client_list[j].timestamp + BAD_NODE_TIMEOUT > temp_time &&
|
2013-07-26 16:02:17 +08:00
|
|
|
!client_in_nodelist(nodes_list, MAX_SENT_NODES,friends_list[i].client_list[j].client_id)) {
|
2013-07-20 11:00:10 +08:00
|
|
|
/* if node is good and not already in list. */
|
2013-07-26 16:02:17 +08:00
|
|
|
if(num_nodes < MAX_SENT_NODES) {
|
2013-06-25 08:51:07 +08:00
|
|
|
memcpy(nodes_list[num_nodes].client_id, friends_list[i].client_list[j].client_id, CLIENT_ID_SIZE);
|
|
|
|
nodes_list[num_nodes].ip_port = friends_list[i].client_list[j].ip_port;
|
|
|
|
num_nodes++;
|
2013-07-26 16:02:17 +08:00
|
|
|
} else for(k = 0; k < MAX_SENT_NODES; ++k) {
|
|
|
|
if(id_closest(client_id, nodes_list[k].client_id, friends_list[i].client_list[j].client_id) == 2) {
|
2013-06-25 21:19:01 +08:00
|
|
|
memcpy(nodes_list[k].client_id, friends_list[i].client_list[j].client_id, CLIENT_ID_SIZE);
|
|
|
|
nodes_list[k].ip_port = friends_list[i].client_list[j].ip_port;
|
2013-06-25 08:51:07 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-26 16:02:17 +08:00
|
|
|
return num_nodes;
|
2013-06-25 08:51:07 +08:00
|
|
|
}
|
|
|
|
|
2013-07-20 11:00:10 +08:00
|
|
|
/* replace first bad (or empty) node with this one
|
|
|
|
return 0 if successful
|
|
|
|
return 1 if not (list contains no bad nodes) */
|
|
|
|
int replace_bad(Client_data * list, uint32_t length, uint8_t * client_id, IP_Port ip_port) /* tested */
|
2013-06-25 00:23:46 +08:00
|
|
|
{
|
|
|
|
uint32_t i;
|
2013-06-25 08:51:07 +08:00
|
|
|
uint32_t temp_time = unix_time();
|
2013-07-26 16:02:17 +08:00
|
|
|
for(i = 0; i < length; ++i) {
|
|
|
|
if(list[i].timestamp + BAD_NODE_TIMEOUT < temp_time) /* if node is bad. */ {
|
2013-06-25 00:23:46 +08:00
|
|
|
memcpy(list[i].client_id, client_id, CLIENT_ID_SIZE);
|
|
|
|
list[i].ip_port = ip_port;
|
2013-06-25 08:51:07 +08:00
|
|
|
list[i].timestamp = temp_time;
|
2013-07-22 01:14:36 +08:00
|
|
|
list[i].ret_ip_port.ip.i = 0;
|
|
|
|
list[i].ret_ip_port.port = 0;
|
2013-07-23 04:26:10 +08:00
|
|
|
list[i].ret_timestamp = 0;
|
2013-06-25 00:23:46 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1;
|
2013-06-26 00:11:11 +08:00
|
|
|
|
2013-06-25 00:23:46 +08:00
|
|
|
}
|
|
|
|
|
2013-07-20 11:00:10 +08:00
|
|
|
/* replace the first good node that is further to the comp_client_id than that of the client_id in the list */
|
2013-07-06 05:00:39 +08:00
|
|
|
int replace_good(Client_data * list, uint32_t length, uint8_t * client_id, IP_Port ip_port, uint8_t * comp_client_id)
|
2013-06-25 00:23:46 +08:00
|
|
|
{
|
|
|
|
uint32_t i;
|
2013-06-26 03:24:31 +08:00
|
|
|
uint32_t temp_time = unix_time();
|
|
|
|
|
2013-07-26 16:02:17 +08:00
|
|
|
for(i = 0; i < length; ++i) {
|
|
|
|
if(id_closest(comp_client_id, list[i].client_id, client_id) == 2) {
|
2013-06-25 00:23:46 +08:00
|
|
|
memcpy(list[i].client_id, client_id, CLIENT_ID_SIZE);
|
|
|
|
list[i].ip_port = ip_port;
|
2013-06-26 03:24:31 +08:00
|
|
|
list[i].timestamp = temp_time;
|
2013-07-22 01:14:36 +08:00
|
|
|
list[i].ret_ip_port.ip.i = 0;
|
|
|
|
list[i].ret_ip_port.port = 0;
|
2013-07-23 04:26:10 +08:00
|
|
|
list[i].ret_timestamp = 0;
|
2013-06-25 00:23:46 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1;
|
2013-06-26 00:11:11 +08:00
|
|
|
|
2013-06-25 00:23:46 +08:00
|
|
|
}
|
2013-06-24 22:42:55 +08:00
|
|
|
|
2013-07-20 11:00:10 +08:00
|
|
|
/* Attempt to add client with ip_port and client_id to the friends client list and close_clientlist */
|
2013-07-06 05:00:39 +08:00
|
|
|
void addto_lists(IP_Port ip_port, uint8_t * client_id)
|
2013-06-24 20:59:42 +08:00
|
|
|
{
|
2013-06-25 02:31:50 +08:00
|
|
|
uint32_t i;
|
2013-06-24 20:59:42 +08:00
|
|
|
|
2013-07-20 11:00:10 +08:00
|
|
|
/* NOTE: current behavior if there are two clients with the same id is to replace the first ip by the second. */
|
2013-07-26 16:02:17 +08:00
|
|
|
if(!client_in_list(close_clientlist, LCLIENT_LIST, client_id, ip_port)) {
|
2013-06-25 00:23:46 +08:00
|
|
|
|
2013-07-26 16:02:17 +08:00
|
|
|
if(replace_bad(close_clientlist, LCLIENT_LIST, client_id, ip_port)) {
|
2013-07-20 11:00:10 +08:00
|
|
|
/* if we can't replace bad nodes we try replacing good ones */
|
2013-07-13 04:27:19 +08:00
|
|
|
replace_good(close_clientlist, LCLIENT_LIST, client_id, ip_port, self_public_key);
|
2013-07-26 16:02:17 +08:00
|
|
|
}
|
2013-06-25 00:23:46 +08:00
|
|
|
}
|
2013-07-26 16:02:17 +08:00
|
|
|
for(i = 0; i < num_friends; ++i) {
|
|
|
|
if(!client_in_list(friends_list[i].client_list, MAX_FRIEND_CLIENTS, client_id, ip_port)) {
|
|
|
|
if(replace_bad(friends_list[i].client_list, MAX_FRIEND_CLIENTS, client_id, ip_port)) {
|
2013-07-20 11:00:10 +08:00
|
|
|
/* if we can't replace bad nodes we try replacing good ones. */
|
2013-07-20 00:35:52 +08:00
|
|
|
replace_good(friends_list[i].client_list, MAX_FRIEND_CLIENTS, client_id, ip_port, friends_list[i].client_id);
|
2013-06-25 00:23:46 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-06-24 20:59:42 +08:00
|
|
|
}
|
2013-06-24 20:28:19 +08:00
|
|
|
|
2013-07-22 01:14:36 +08:00
|
|
|
/* 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 */
|
|
|
|
void returnedip_ports(IP_Port ip_port, uint8_t * client_id, uint8_t * nodeclient_id)
|
|
|
|
{
|
|
|
|
uint32_t i, j;
|
2013-07-23 04:26:10 +08:00
|
|
|
uint32_t temp_time = unix_time();
|
2013-07-26 16:02:17 +08:00
|
|
|
if(memcmp(client_id, self_public_key, CLIENT_ID_SIZE) == 0) {
|
|
|
|
for(i = 0; i < LCLIENT_LIST; ++i) {
|
|
|
|
if(memcmp(nodeclient_id, close_clientlist[i].client_id, CLIENT_ID_SIZE) == 0) {
|
2013-07-22 01:14:36 +08:00
|
|
|
close_clientlist[i].ret_ip_port = ip_port;
|
2013-07-23 04:26:10 +08:00
|
|
|
close_clientlist[i].ret_timestamp = temp_time;
|
2013-07-22 01:14:36 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2013-07-26 16:02:17 +08:00
|
|
|
for(i = 0; i < num_friends; ++i) {
|
|
|
|
if(memcmp(client_id, friends_list[i].client_id, CLIENT_ID_SIZE) == 0) {
|
|
|
|
for(j = 0; j < MAX_FRIEND_CLIENTS; ++j) {
|
|
|
|
if(memcmp(nodeclient_id, friends_list[i].client_list[j].client_id, CLIENT_ID_SIZE) == 0) {
|
2013-07-22 01:14:36 +08:00
|
|
|
friends_list[i].client_list[j].ret_ip_port = ip_port;
|
2013-07-23 04:26:10 +08:00
|
|
|
friends_list[i].client_list[j].ret_timestamp = temp_time;
|
2013-07-22 01:14:36 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-06-24 20:28:19 +08:00
|
|
|
|
2013-07-20 11:00:10 +08:00
|
|
|
/* ping timeout in seconds */
|
2013-06-26 19:40:07 +08:00
|
|
|
#define PING_TIMEOUT 5
|
2013-07-26 16:02:17 +08:00
|
|
|
|
2013-07-20 11:00:10 +08:00
|
|
|
/* check if we are currently pinging an ip_port and/or a ping_id
|
|
|
|
variables with values of zero will not be checked.
|
|
|
|
if we are already, return 1
|
|
|
|
else return 0
|
|
|
|
TODO: optimize this */
|
2013-07-13 04:27:19 +08:00
|
|
|
int is_pinging(IP_Port ip_port, uint64_t ping_id)
|
2013-06-25 02:26:29 +08:00
|
|
|
{
|
|
|
|
uint32_t i;
|
2013-06-26 03:24:31 +08:00
|
|
|
uint8_t pinging;
|
|
|
|
uint32_t temp_time = unix_time();
|
|
|
|
|
2013-07-26 16:02:17 +08:00
|
|
|
for(i = 0; i < LPING_ARRAY; ++i ) {
|
|
|
|
if((pings[i].timestamp + PING_TIMEOUT) > temp_time) {
|
2013-06-26 19:40:07 +08:00
|
|
|
pinging = 0;
|
2013-07-26 16:02:17 +08:00
|
|
|
if(ip_port.ip.i != 0) {
|
2013-06-26 03:24:31 +08:00
|
|
|
if(pings[i].ip_port.ip.i == ip_port.ip.i &&
|
2013-07-26 16:02:17 +08:00
|
|
|
pings[i].ip_port.port == ip_port.port) {
|
2013-07-19 23:55:34 +08:00
|
|
|
++pinging;
|
2013-06-26 03:24:31 +08:00
|
|
|
}
|
|
|
|
}
|
2013-07-26 16:02:17 +08:00
|
|
|
if(ping_id != 0) {
|
2013-06-26 03:24:31 +08:00
|
|
|
if(pings[i].ping_id == ping_id)
|
|
|
|
{
|
2013-07-19 23:55:34 +08:00
|
|
|
++pinging;
|
2013-06-26 03:24:31 +08:00
|
|
|
}
|
|
|
|
}
|
2013-07-26 16:02:17 +08:00
|
|
|
if(pinging == (ping_id != 0) + (ip_port.ip.i != 0)) {
|
2013-06-26 03:24:31 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-06-25 02:26:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-07-20 11:00:10 +08:00
|
|
|
/* Same as last function but for get_node requests. */
|
2013-07-13 04:27:19 +08:00
|
|
|
int is_gettingnodes(IP_Port ip_port, uint64_t ping_id)
|
2013-06-25 02:26:29 +08:00
|
|
|
{
|
|
|
|
uint32_t i;
|
2013-06-26 03:24:31 +08:00
|
|
|
uint8_t pinging;
|
|
|
|
uint32_t temp_time = unix_time();
|
|
|
|
|
2013-07-26 16:02:17 +08:00
|
|
|
for(i = 0; i < LSEND_NODES_ARRAY; ++i ) {
|
|
|
|
if((send_nodes[i].timestamp + PING_TIMEOUT) > temp_time) {
|
2013-06-26 19:40:07 +08:00
|
|
|
pinging = 0;
|
2013-07-26 16:02:17 +08:00
|
|
|
if(ip_port.ip.i != 0) {
|
2013-06-26 03:24:31 +08:00
|
|
|
if(send_nodes[i].ip_port.ip.i == ip_port.ip.i &&
|
2013-07-26 16:02:17 +08:00
|
|
|
send_nodes[i].ip_port.port == ip_port.port) {
|
2013-07-19 23:55:34 +08:00
|
|
|
++pinging;
|
2013-06-26 03:24:31 +08:00
|
|
|
}
|
|
|
|
}
|
2013-07-26 16:02:17 +08:00
|
|
|
if(ping_id != 0) {
|
|
|
|
if(send_nodes[i].ping_id == ping_id) {
|
2013-07-19 23:55:34 +08:00
|
|
|
++pinging;
|
2013-06-26 03:24:31 +08:00
|
|
|
}
|
|
|
|
}
|
2013-07-26 16:02:17 +08:00
|
|
|
if(pinging == (ping_id != 0) + (ip_port.ip.i != 0)) {
|
2013-06-26 03:24:31 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-06-25 02:26:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-07-20 11:00:10 +08:00
|
|
|
/* Add a new ping request to the list of ping requests
|
|
|
|
returns the ping_id to put in the ping request
|
|
|
|
returns 0 if problem.
|
|
|
|
TODO: optimize this */
|
2013-07-13 04:27:19 +08:00
|
|
|
uint64_t add_pinging(IP_Port ip_port)
|
2013-06-25 02:26:29 +08:00
|
|
|
{
|
|
|
|
uint32_t i, j;
|
2013-07-13 04:27:19 +08:00
|
|
|
uint64_t ping_id = ((uint64_t)random_int() << 32) + random_int();
|
2013-06-26 03:24:31 +08:00
|
|
|
uint32_t temp_time = unix_time();
|
|
|
|
|
2013-07-26 16:02:17 +08:00
|
|
|
for(i = 0; i < PING_TIMEOUT; ++i ) {
|
|
|
|
for(j = 0; j < LPING_ARRAY; ++j ) {
|
|
|
|
if((pings[j].timestamp + PING_TIMEOUT - i) < temp_time) {
|
2013-06-26 03:24:31 +08:00
|
|
|
pings[j].timestamp = temp_time;
|
2013-06-25 02:26:29 +08:00
|
|
|
pings[j].ip_port = ip_port;
|
|
|
|
pings[j].ping_id = ping_id;
|
|
|
|
return ping_id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-06-25 21:19:01 +08:00
|
|
|
return 0;
|
2013-06-26 00:11:11 +08:00
|
|
|
|
2013-06-25 02:26:29 +08:00
|
|
|
}
|
|
|
|
|
2013-07-20 11:00:10 +08:00
|
|
|
/* Same but for get node requests */
|
2013-07-13 04:27:19 +08:00
|
|
|
uint64_t add_gettingnodes(IP_Port ip_port)
|
2013-06-25 02:26:29 +08:00
|
|
|
{
|
|
|
|
uint32_t i, j;
|
2013-07-13 04:27:19 +08:00
|
|
|
uint64_t ping_id = ((uint64_t)random_int() << 32) + random_int();
|
2013-06-26 03:24:31 +08:00
|
|
|
uint32_t temp_time = unix_time();
|
|
|
|
|
2013-07-26 16:02:17 +08:00
|
|
|
for(i = 0; i < PING_TIMEOUT; ++i ) {
|
|
|
|
for(j = 0; j < LSEND_NODES_ARRAY; ++j ) {
|
|
|
|
if((send_nodes[j].timestamp + PING_TIMEOUT - i) < temp_time) {
|
2013-06-26 03:24:31 +08:00
|
|
|
send_nodes[j].timestamp = temp_time;
|
2013-06-25 02:26:29 +08:00
|
|
|
send_nodes[j].ip_port = ip_port;
|
|
|
|
send_nodes[j].ping_id = ping_id;
|
|
|
|
return ping_id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-06-25 21:19:01 +08:00
|
|
|
return 0;
|
2013-06-26 00:11:11 +08:00
|
|
|
|
2013-06-25 02:26:29 +08:00
|
|
|
}
|
|
|
|
|
2013-07-20 11:00:10 +08:00
|
|
|
/* send a ping request
|
|
|
|
Ping request only works if none has been sent to that ip/port in the last 5 seconds. */
|
2013-07-13 04:27:19 +08:00
|
|
|
static int pingreq(IP_Port ip_port, uint8_t * public_key)
|
2013-06-24 20:59:42 +08:00
|
|
|
{
|
2013-07-26 16:02:17 +08:00
|
|
|
if(memcmp(public_key, self_public_key, CLIENT_ID_SIZE) == 0) /* check if packet is gonna be sent to ourself */ {
|
2013-07-13 04:27:19 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-07-26 16:02:17 +08:00
|
|
|
if(is_pinging(ip_port, 0)) {
|
2013-06-25 02:26:29 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-07-13 04:27:19 +08:00
|
|
|
uint64_t ping_id = add_pinging(ip_port);
|
2013-07-26 16:02:17 +08:00
|
|
|
if(ping_id == 0) {
|
2013-06-25 21:19:01 +08:00
|
|
|
return 1;
|
|
|
|
}
|
2013-06-25 02:26:29 +08:00
|
|
|
|
2013-07-13 04:27:19 +08:00
|
|
|
uint8_t data[1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) + ENCRYPTION_PADDING];
|
|
|
|
uint8_t encrypt[sizeof(ping_id) + ENCRYPTION_PADDING];
|
|
|
|
uint8_t nonce[crypto_box_NONCEBYTES];
|
|
|
|
random_nonce(nonce);
|
|
|
|
|
|
|
|
int len = encrypt_data(public_key, self_secret_key, nonce, (uint8_t *)&ping_id, sizeof(ping_id), encrypt);
|
2013-07-26 16:02:17 +08:00
|
|
|
if(len != sizeof(ping_id) + ENCRYPTION_PADDING) {
|
2013-07-13 04:27:19 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2013-06-24 20:59:42 +08:00
|
|
|
data[0] = 0;
|
2013-07-13 04:27:19 +08:00
|
|
|
memcpy(data + 1, self_public_key, CLIENT_ID_SIZE);
|
|
|
|
memcpy(data + 1 + CLIENT_ID_SIZE, nonce, crypto_box_NONCEBYTES);
|
|
|
|
memcpy(data + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, encrypt, len);
|
2013-06-24 20:59:42 +08:00
|
|
|
|
2013-06-25 02:26:29 +08:00
|
|
|
return sendpacket(ip_port, data, sizeof(data));
|
2013-06-24 20:59:42 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-07-20 11:00:10 +08:00
|
|
|
/* send a ping response */
|
2013-07-13 04:27:19 +08:00
|
|
|
static int pingres(IP_Port ip_port, uint8_t * public_key, uint64_t ping_id)
|
2013-06-24 20:59:42 +08:00
|
|
|
{
|
2013-07-26 16:02:17 +08:00
|
|
|
/* check if packet is gonna be sent to ourself */
|
|
|
|
if(memcmp(public_key, self_public_key, CLIENT_ID_SIZE) == 0) {
|
2013-07-13 04:27:19 +08:00
|
|
|
return 1;
|
|
|
|
}
|
2013-06-24 20:59:42 +08:00
|
|
|
|
2013-07-13 04:27:19 +08:00
|
|
|
uint8_t data[1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) + ENCRYPTION_PADDING];
|
|
|
|
uint8_t encrypt[sizeof(ping_id) + ENCRYPTION_PADDING];
|
|
|
|
uint8_t nonce[crypto_box_NONCEBYTES];
|
|
|
|
random_nonce(nonce);
|
|
|
|
|
|
|
|
int len = encrypt_data(public_key, self_secret_key, nonce, (uint8_t *)&ping_id, sizeof(ping_id), encrypt);
|
2013-07-26 16:02:17 +08:00
|
|
|
if(len != sizeof(ping_id) + ENCRYPTION_PADDING) {
|
2013-07-13 04:27:19 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
data[0] = 1;
|
|
|
|
memcpy(data + 1, self_public_key, CLIENT_ID_SIZE);
|
|
|
|
memcpy(data + 1 + CLIENT_ID_SIZE, nonce, crypto_box_NONCEBYTES);
|
|
|
|
memcpy(data + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, encrypt, len);
|
2013-06-24 20:59:42 +08:00
|
|
|
|
2013-06-25 02:26:29 +08:00
|
|
|
return sendpacket(ip_port, data, sizeof(data));
|
2013-06-24 20:59:42 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-07-20 11:00:10 +08:00
|
|
|
/* send a getnodes request */
|
2013-07-13 04:27:19 +08:00
|
|
|
static int getnodes(IP_Port ip_port, uint8_t * public_key, uint8_t * client_id)
|
2013-06-24 20:28:19 +08:00
|
|
|
{
|
2013-07-26 16:02:17 +08:00
|
|
|
/* check if packet is gonna be sent to ourself */
|
|
|
|
if(memcmp(public_key, self_public_key, CLIENT_ID_SIZE) == 0) {
|
2013-07-13 04:27:19 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-07-26 16:02:17 +08:00
|
|
|
if(is_gettingnodes(ip_port, 0)) {
|
2013-06-25 02:26:29 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-07-13 04:27:19 +08:00
|
|
|
uint64_t ping_id = add_gettingnodes(ip_port);
|
2013-06-25 21:19:01 +08:00
|
|
|
|
2013-07-26 16:02:17 +08:00
|
|
|
if(ping_id == 0) {
|
2013-06-25 21:19:01 +08:00
|
|
|
return 1;
|
|
|
|
}
|
2013-06-25 02:26:29 +08:00
|
|
|
|
2013-07-13 04:27:19 +08:00
|
|
|
uint8_t data[1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) + CLIENT_ID_SIZE + ENCRYPTION_PADDING];
|
|
|
|
uint8_t plain[sizeof(ping_id) + CLIENT_ID_SIZE];
|
|
|
|
uint8_t encrypt[sizeof(ping_id) + CLIENT_ID_SIZE + ENCRYPTION_PADDING];
|
|
|
|
uint8_t nonce[crypto_box_NONCEBYTES];
|
|
|
|
random_nonce(nonce);
|
2013-06-25 02:26:29 +08:00
|
|
|
|
2013-07-13 04:27:19 +08:00
|
|
|
memcpy(plain, &ping_id, sizeof(ping_id));
|
|
|
|
memcpy(plain + sizeof(ping_id), client_id, CLIENT_ID_SIZE);
|
|
|
|
|
|
|
|
int len = encrypt_data(public_key, self_secret_key, nonce, plain, sizeof(ping_id) + CLIENT_ID_SIZE, encrypt);
|
|
|
|
|
2013-07-26 16:02:17 +08:00
|
|
|
if(len != sizeof(ping_id) + CLIENT_ID_SIZE + ENCRYPTION_PADDING) {
|
2013-07-13 04:27:19 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
data[0] = 2;
|
|
|
|
memcpy(data + 1, self_public_key, CLIENT_ID_SIZE);
|
|
|
|
memcpy(data + 1 + CLIENT_ID_SIZE, nonce, crypto_box_NONCEBYTES);
|
|
|
|
memcpy(data + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, encrypt, len);
|
2013-06-25 02:26:29 +08:00
|
|
|
return sendpacket(ip_port, data, sizeof(data));
|
2013-06-26 00:11:11 +08:00
|
|
|
|
2013-06-24 13:05:28 +08:00
|
|
|
}
|
|
|
|
|
2013-07-20 11:00:10 +08:00
|
|
|
/* send a send nodes response */
|
2013-07-13 04:27:19 +08:00
|
|
|
static int sendnodes(IP_Port ip_port, uint8_t * public_key, uint8_t * client_id, uint64_t ping_id)
|
2013-06-24 13:05:28 +08:00
|
|
|
{
|
2013-07-26 16:02:17 +08:00
|
|
|
if(memcmp(public_key, self_public_key, CLIENT_ID_SIZE) == 0) /* check if packet is gonna be sent to ourself */ {
|
2013-07-13 04:27:19 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t data[1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id)
|
|
|
|
+ sizeof(Node_format) * MAX_SENT_NODES + ENCRYPTION_PADDING];
|
|
|
|
|
2013-06-26 00:11:11 +08:00
|
|
|
Node_format nodes_list[MAX_SENT_NODES];
|
|
|
|
int num_nodes = get_close_nodes(client_id, nodes_list);
|
2013-07-13 04:27:19 +08:00
|
|
|
|
2013-07-26 16:02:17 +08:00
|
|
|
if(num_nodes == 0) {
|
2013-06-26 00:11:11 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2013-07-13 04:27:19 +08:00
|
|
|
|
|
|
|
uint8_t plain[sizeof(ping_id) + sizeof(Node_format) * MAX_SENT_NODES];
|
|
|
|
uint8_t encrypt[sizeof(ping_id) + sizeof(Node_format) * MAX_SENT_NODES + ENCRYPTION_PADDING];
|
|
|
|
uint8_t nonce[crypto_box_NONCEBYTES];
|
|
|
|
random_nonce(nonce);
|
|
|
|
|
|
|
|
memcpy(plain, &ping_id, sizeof(ping_id));
|
|
|
|
memcpy(plain + sizeof(ping_id), nodes_list, num_nodes * sizeof(Node_format));
|
|
|
|
|
|
|
|
int len = encrypt_data(public_key, self_secret_key, nonce, plain,
|
|
|
|
sizeof(ping_id) + num_nodes * sizeof(Node_format), encrypt);
|
|
|
|
|
2013-07-26 16:02:17 +08:00
|
|
|
if(len != sizeof(ping_id) + num_nodes * sizeof(Node_format) + ENCRYPTION_PADDING) {
|
2013-07-13 04:27:19 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-06-26 00:11:11 +08:00
|
|
|
data[0] = 3;
|
2013-07-13 04:27:19 +08:00
|
|
|
memcpy(data + 1, self_public_key, CLIENT_ID_SIZE);
|
|
|
|
memcpy(data + 1 + CLIENT_ID_SIZE, nonce, crypto_box_NONCEBYTES);
|
|
|
|
memcpy(data + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, encrypt, len);
|
|
|
|
|
|
|
|
return sendpacket(ip_port, data, 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + len);
|
2013-06-24 20:59:42 +08:00
|
|
|
|
2013-06-24 13:05:28 +08:00
|
|
|
}
|
|
|
|
|
2013-07-20 11:00:10 +08:00
|
|
|
/* Packet handling functions
|
|
|
|
One to handle each types of packets we receive
|
|
|
|
return 0 if handled correctly, 1 if packet is bad. */
|
2013-07-13 04:27:19 +08:00
|
|
|
int handle_pingreq(uint8_t * packet, uint32_t length, IP_Port source)
|
2013-06-24 20:28:19 +08:00
|
|
|
{
|
2013-07-13 04:27:19 +08:00
|
|
|
uint64_t ping_id;
|
2013-07-26 16:02:17 +08:00
|
|
|
if(length != 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) + ENCRYPTION_PADDING) {
|
2013-07-13 04:27:19 +08:00
|
|
|
return 1;
|
|
|
|
}
|
2013-07-26 16:02:17 +08:00
|
|
|
/* check if packet is from ourself. */
|
|
|
|
if(memcmp(packet + 1, self_public_key, CLIENT_ID_SIZE) == 0) {
|
2013-06-24 22:42:55 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-06-24 20:28:19 +08:00
|
|
|
|
2013-06-26 03:24:31 +08:00
|
|
|
|
2013-07-13 04:27:19 +08:00
|
|
|
int len = decrypt_data(packet + 1, self_secret_key, packet + 1 + CLIENT_ID_SIZE,
|
|
|
|
packet + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES,
|
|
|
|
sizeof(ping_id) + ENCRYPTION_PADDING, (uint8_t *)&ping_id);
|
2013-07-26 16:02:17 +08:00
|
|
|
if(len != sizeof(ping_id)) {
|
2013-06-26 03:24:31 +08:00
|
|
|
return 1;
|
|
|
|
}
|
2013-07-13 04:27:19 +08:00
|
|
|
|
2013-06-26 03:24:31 +08:00
|
|
|
|
2013-07-13 04:27:19 +08:00
|
|
|
pingres(source, packet + 1, ping_id);
|
2013-06-25 21:19:01 +08:00
|
|
|
|
2013-07-20 11:00:10 +08:00
|
|
|
pingreq(source, packet + 1); /* TODO: make this smarter? */
|
2013-06-25 08:51:07 +08:00
|
|
|
|
2013-06-24 22:42:55 +08:00
|
|
|
return 0;
|
2013-06-26 00:11:11 +08:00
|
|
|
|
2013-06-24 20:28:19 +08:00
|
|
|
}
|
|
|
|
|
2013-07-06 05:00:39 +08:00
|
|
|
int handle_pingres(uint8_t * packet, uint32_t length, IP_Port source)
|
2013-06-24 20:28:19 +08:00
|
|
|
{
|
2013-07-13 04:27:19 +08:00
|
|
|
uint64_t ping_id;
|
2013-07-26 16:02:17 +08:00
|
|
|
if(length != 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) + ENCRYPTION_PADDING) {
|
2013-07-13 04:27:19 +08:00
|
|
|
return 1;
|
|
|
|
}
|
2013-07-26 16:02:17 +08:00
|
|
|
if(memcmp(packet + 1, self_public_key, CLIENT_ID_SIZE) == 0) /* check if packet is from ourself. */ {
|
2013-07-13 04:27:19 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int len = decrypt_data(packet + 1, self_secret_key, packet + 1 + CLIENT_ID_SIZE,
|
|
|
|
packet + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES,
|
|
|
|
sizeof(ping_id) + ENCRYPTION_PADDING, (uint8_t *)&ping_id);
|
2013-07-26 16:02:17 +08:00
|
|
|
if(len != sizeof(ping_id)) {
|
2013-06-24 22:42:55 +08:00
|
|
|
return 1;
|
|
|
|
}
|
2013-06-24 20:28:19 +08:00
|
|
|
|
2013-07-26 16:02:17 +08:00
|
|
|
if(is_pinging(source, ping_id)) {
|
2013-07-13 04:27:19 +08:00
|
|
|
addto_lists(source, packet + 1);
|
2013-06-26 03:24:31 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
2013-06-26 00:11:11 +08:00
|
|
|
|
2013-06-24 20:28:19 +08:00
|
|
|
}
|
|
|
|
|
2013-07-06 05:00:39 +08:00
|
|
|
int handle_getnodes(uint8_t * packet, uint32_t length, IP_Port source)
|
2013-06-24 20:28:19 +08:00
|
|
|
{
|
2013-07-13 04:27:19 +08:00
|
|
|
uint64_t ping_id;
|
2013-07-26 16:02:17 +08:00
|
|
|
if(length != 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) + CLIENT_ID_SIZE + ENCRYPTION_PADDING) {
|
2013-06-24 22:42:55 +08:00
|
|
|
return 1;
|
|
|
|
}
|
2013-07-26 16:02:17 +08:00
|
|
|
/* check if packet is from ourself. */
|
|
|
|
if(memcmp(packet + 1, self_public_key, CLIENT_ID_SIZE) == 0) {
|
2013-07-13 04:27:19 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t plain[sizeof(ping_id) + CLIENT_ID_SIZE];
|
2013-06-25 00:23:46 +08:00
|
|
|
|
2013-07-13 04:27:19 +08:00
|
|
|
int len = decrypt_data(packet + 1, self_secret_key, packet + 1 + CLIENT_ID_SIZE,
|
|
|
|
packet + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES,
|
|
|
|
sizeof(ping_id) + CLIENT_ID_SIZE + ENCRYPTION_PADDING, plain);
|
2013-06-26 03:24:31 +08:00
|
|
|
|
2013-07-26 16:02:17 +08:00
|
|
|
if(len != sizeof(ping_id) + CLIENT_ID_SIZE) {
|
2013-06-26 03:24:31 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-07-13 04:27:19 +08:00
|
|
|
|
|
|
|
memcpy(&ping_id, plain, sizeof(ping_id));
|
|
|
|
sendnodes(source, packet + 1, plain + sizeof(ping_id), ping_id);
|
|
|
|
|
2013-07-20 11:00:10 +08:00
|
|
|
pingreq(source, packet + 1); /* TODO: make this smarter? */
|
2013-06-25 21:59:54 +08:00
|
|
|
|
2013-06-25 08:51:07 +08:00
|
|
|
return 0;
|
2013-06-26 00:11:11 +08:00
|
|
|
|
2013-06-24 20:28:19 +08:00
|
|
|
}
|
|
|
|
|
2013-07-13 04:27:19 +08:00
|
|
|
int handle_sendnodes(uint8_t * packet, uint32_t length, IP_Port source)
|
2013-06-24 20:28:19 +08:00
|
|
|
{
|
2013-07-13 04:27:19 +08:00
|
|
|
uint64_t ping_id;
|
|
|
|
if(length > (1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id)
|
|
|
|
+ sizeof(Node_format) * MAX_SENT_NODES + ENCRYPTION_PADDING) ||
|
|
|
|
(length - (1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id)
|
|
|
|
+ ENCRYPTION_PADDING)) % (sizeof(Node_format)) != 0 ||
|
|
|
|
length < 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id)
|
2013-07-26 16:02:17 +08:00
|
|
|
+ sizeof(Node_format) + ENCRYPTION_PADDING) {
|
2013-06-24 22:42:55 +08:00
|
|
|
return 1;
|
|
|
|
}
|
2013-07-13 04:27:19 +08:00
|
|
|
uint32_t num_nodes = (length - (1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES
|
|
|
|
+ sizeof(ping_id) + ENCRYPTION_PADDING)) / sizeof(Node_format);
|
|
|
|
|
|
|
|
uint8_t plain[sizeof(ping_id) + sizeof(Node_format) * MAX_SENT_NODES];
|
|
|
|
|
|
|
|
int len = decrypt_data(packet + 1, self_secret_key, packet + 1 + CLIENT_ID_SIZE,
|
|
|
|
packet + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES,
|
|
|
|
sizeof(ping_id) + num_nodes * sizeof(Node_format) + ENCRYPTION_PADDING, plain);
|
2013-06-26 03:24:31 +08:00
|
|
|
|
2013-07-13 04:27:19 +08:00
|
|
|
|
2013-07-26 16:02:17 +08:00
|
|
|
if(len != sizeof(ping_id) + num_nodes * sizeof(Node_format)) {
|
2013-07-13 04:27:19 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(&ping_id, plain, sizeof(ping_id));
|
2013-07-26 16:02:17 +08:00
|
|
|
if(!is_gettingnodes(source, ping_id)) {
|
2013-06-26 03:24:31 +08:00
|
|
|
return 1;
|
|
|
|
}
|
2013-06-25 21:19:01 +08:00
|
|
|
|
|
|
|
Node_format nodes_list[MAX_SENT_NODES];
|
2013-07-13 04:27:19 +08:00
|
|
|
memcpy(nodes_list, plain + sizeof(ping_id), num_nodes * sizeof(Node_format));
|
2013-06-24 20:28:19 +08:00
|
|
|
|
2013-07-22 01:14:36 +08:00
|
|
|
addto_lists(source, packet + 1);
|
|
|
|
|
2013-07-13 04:27:19 +08:00
|
|
|
uint32_t i;
|
2013-07-26 16:02:17 +08:00
|
|
|
for(i = 0; i < num_nodes; ++i) {
|
2013-07-13 04:27:19 +08:00
|
|
|
pingreq(nodes_list[i].ip_port, nodes_list[i].client_id);
|
2013-07-22 01:14:36 +08:00
|
|
|
returnedip_ports(nodes_list[i].ip_port, nodes_list[i].client_id, packet + 1);
|
2013-06-25 21:19:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2013-06-24 20:28:19 +08:00
|
|
|
}
|
|
|
|
|
2013-07-20 11:00:10 +08:00
|
|
|
/* END of packet handling functions */
|
2013-06-24 13:05:28 +08:00
|
|
|
|
2013-07-07 23:54:25 +08:00
|
|
|
int DHT_addfriend(uint8_t * client_id)
|
2013-06-24 13:05:28 +08:00
|
|
|
{
|
2013-07-14 23:50:34 +08:00
|
|
|
Friend * temp;
|
2013-07-26 16:02:17 +08:00
|
|
|
if(num_friends == 0) {
|
2013-07-14 23:50:34 +08:00
|
|
|
temp = malloc(sizeof(Friend));
|
2013-07-26 16:02:17 +08:00
|
|
|
} else {
|
2013-07-14 23:50:34 +08:00
|
|
|
temp = realloc(friends_list, sizeof(Friend) * (num_friends + 1));
|
|
|
|
}
|
2013-07-26 16:02:17 +08:00
|
|
|
if(temp == NULL) {
|
2013-07-14 23:50:34 +08:00
|
|
|
return 1;
|
2013-06-26 00:11:11 +08:00
|
|
|
}
|
2013-07-14 23:50:34 +08:00
|
|
|
|
|
|
|
friends_list = temp;
|
|
|
|
memset(&friends_list[num_friends], 0, sizeof(Friend));
|
|
|
|
memcpy(friends_list[num_friends].client_id, client_id, CLIENT_ID_SIZE);
|
2013-07-25 07:39:34 +08:00
|
|
|
friends_list[num_friends].NATping_id = ((uint64_t)random_int() << 32) + random_int();
|
2013-07-19 23:55:34 +08:00
|
|
|
++num_friends;
|
2013-07-14 23:50:34 +08:00
|
|
|
return 0;
|
2013-06-24 13:05:28 +08:00
|
|
|
}
|
|
|
|
|
2013-07-07 23:54:25 +08:00
|
|
|
int DHT_delfriend(uint8_t * client_id)
|
2013-06-24 13:05:28 +08:00
|
|
|
{
|
2013-06-25 04:50:43 +08:00
|
|
|
uint32_t i;
|
2013-07-14 23:50:34 +08:00
|
|
|
Friend * temp;
|
2013-07-26 16:02:17 +08:00
|
|
|
for(i = 0; i < num_friends; ++i) {
|
|
|
|
/* Equal */
|
|
|
|
if(memcmp(friends_list[i].client_id, client_id, CLIENT_ID_SIZE) == 0){
|
2013-07-19 23:55:34 +08:00
|
|
|
--num_friends;
|
2013-07-26 16:02:17 +08:00
|
|
|
if(num_friends != i) {
|
2013-07-18 01:54:19 +08:00
|
|
|
memcpy(friends_list[i].client_id, friends_list[num_friends].client_id, CLIENT_ID_SIZE);
|
|
|
|
}
|
2013-07-18 03:10:52 +08:00
|
|
|
temp = realloc(friends_list, sizeof(Friend) * (num_friends));
|
2013-07-26 16:02:17 +08:00
|
|
|
if(temp != NULL) {
|
2013-07-14 23:50:34 +08:00
|
|
|
friends_list = temp;
|
|
|
|
}
|
2013-06-25 04:50:43 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1;
|
2013-06-24 13:05:28 +08:00
|
|
|
}
|
|
|
|
|
2013-07-20 11:00:10 +08:00
|
|
|
/* TODO: Optimize this. */
|
2013-07-07 23:54:25 +08:00
|
|
|
IP_Port DHT_getfriendip(uint8_t * client_id)
|
2013-06-24 13:05:28 +08:00
|
|
|
{
|
2013-06-25 21:19:01 +08:00
|
|
|
uint32_t i, j;
|
|
|
|
IP_Port empty = {{{0}}, 0};
|
2013-07-17 01:20:45 +08:00
|
|
|
uint32_t temp_time = unix_time();
|
2013-07-26 16:02:17 +08:00
|
|
|
for(i = 0; i < num_friends; ++i) {
|
|
|
|
/* Equal */
|
2013-07-26 16:08:39 +08:00
|
|
|
if(memcmp(friends_list[i].client_id, client_id, CLIENT_ID_SIZE) == 0) {
|
2013-07-26 16:02:17 +08:00
|
|
|
for(j = 0; j < MAX_FRIEND_CLIENTS; ++j) {
|
2013-07-17 01:20:45 +08:00
|
|
|
if(memcmp(friends_list[i].client_list[j].client_id, client_id, CLIENT_ID_SIZE) == 0 &&
|
2013-07-26 16:02:17 +08:00
|
|
|
friends_list[i].client_list[j].timestamp + BAD_NODE_TIMEOUT > temp_time) {
|
2013-06-25 21:19:01 +08:00
|
|
|
return friends_list[i].client_list[j].ip_port;
|
2013-07-26 16:02:17 +08:00
|
|
|
}
|
|
|
|
}
|
2013-06-25 21:19:01 +08:00
|
|
|
return empty;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
empty.ip.i = 1;
|
|
|
|
return empty;
|
2013-06-26 00:11:11 +08:00
|
|
|
|
2013-07-26 16:02:17 +08:00
|
|
|
}
|
2013-06-24 13:05:28 +08:00
|
|
|
|
2013-07-20 11:00:10 +08:00
|
|
|
/* The timeout after which a node is discarded completely. */
|
2013-06-25 21:19:01 +08:00
|
|
|
#define Kill_NODE_TIMEOUT 300
|
|
|
|
|
2013-07-20 11:00:10 +08:00
|
|
|
/* ping interval in seconds for each node in our lists. */
|
2013-06-25 21:19:01 +08:00
|
|
|
#define PING_INTERVAL 60
|
|
|
|
|
2013-07-20 11:00:10 +08:00
|
|
|
/* ping interval in seconds for each random sending of a get nodes request. */
|
2013-06-26 03:24:31 +08:00
|
|
|
#define GET_NODE_INTERVAL 10
|
2013-06-26 00:11:11 +08:00
|
|
|
|
2013-07-20 11:00:10 +08:00
|
|
|
/* Ping each client in the "friends" list every 60 seconds.
|
|
|
|
Send a get nodes request every 20 seconds to a random good node for each "friend" in our "friends" list. */
|
2013-07-09 08:50:25 +08:00
|
|
|
void doDHTFriends()
|
2013-06-25 00:23:46 +08:00
|
|
|
{
|
2013-06-25 21:19:01 +08:00
|
|
|
uint32_t i, j;
|
|
|
|
uint32_t temp_time = unix_time();
|
2013-06-26 00:11:11 +08:00
|
|
|
uint32_t rand_node;
|
|
|
|
uint32_t index[MAX_FRIEND_CLIENTS];
|
|
|
|
|
2013-07-26 16:02:17 +08:00
|
|
|
for(i = 0; i < num_friends; ++i) {
|
2013-07-21 07:59:01 +08:00
|
|
|
uint32_t num_nodes = 0;
|
2013-07-26 16:02:17 +08:00
|
|
|
for(j = 0; j < MAX_FRIEND_CLIENTS; ++j) {
|
|
|
|
if(friends_list[i].client_list[j].timestamp + Kill_NODE_TIMEOUT > temp_time) /* if node is not dead. */ {
|
|
|
|
if((friends_list[i].client_list[j].last_pinged + PING_INTERVAL) <= temp_time) {
|
2013-07-13 04:27:19 +08:00
|
|
|
pingreq(friends_list[i].client_list[j].ip_port, friends_list[i].client_list[j].client_id);
|
2013-06-27 19:37:06 +08:00
|
|
|
friends_list[i].client_list[j].last_pinged = temp_time;
|
2013-06-25 21:19:01 +08:00
|
|
|
}
|
2013-07-26 16:02:17 +08:00
|
|
|
if(friends_list[i].client_list[j].timestamp + BAD_NODE_TIMEOUT > temp_time) /* if node is good. */ {
|
2013-06-26 00:11:11 +08:00
|
|
|
index[num_nodes] = j;
|
2013-07-19 23:55:34 +08:00
|
|
|
++num_nodes;
|
2013-06-26 00:11:11 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-07-26 16:02:17 +08:00
|
|
|
if(friends_list[i].lastgetnode + GET_NODE_INTERVAL <= temp_time && num_nodes != 0) {
|
2013-06-26 00:11:11 +08:00
|
|
|
rand_node = rand() % num_nodes;
|
|
|
|
getnodes(friends_list[i].client_list[index[rand_node]].ip_port,
|
2013-07-13 04:27:19 +08:00
|
|
|
friends_list[i].client_list[index[rand_node]].client_id,
|
|
|
|
friends_list[i].client_id);
|
2013-07-14 23:50:34 +08:00
|
|
|
friends_list[i].lastgetnode = temp_time;
|
2013-06-25 21:19:01 +08:00
|
|
|
}
|
|
|
|
}
|
2013-06-25 00:23:46 +08:00
|
|
|
}
|
2013-06-24 13:05:28 +08:00
|
|
|
|
2013-06-26 19:40:07 +08:00
|
|
|
static uint32_t close_lastgetnodes;
|
2013-06-24 13:05:28 +08:00
|
|
|
|
2013-07-20 11:00:10 +08:00
|
|
|
/* Ping each client in the close nodes list every 60 seconds.
|
|
|
|
Send a get nodes request every 20 seconds to a random good node in the list. */
|
|
|
|
void doClose() /* tested */
|
2013-06-25 00:23:46 +08:00
|
|
|
{
|
2013-06-25 21:19:01 +08:00
|
|
|
uint32_t i;
|
|
|
|
uint32_t temp_time = unix_time();
|
2013-06-26 00:11:11 +08:00
|
|
|
uint32_t num_nodes = 0;
|
|
|
|
uint32_t rand_node;
|
|
|
|
uint32_t index[LCLIENT_LIST];
|
|
|
|
|
2013-07-26 16:02:17 +08:00
|
|
|
for(i = 0; i < LCLIENT_LIST; ++i) {
|
|
|
|
/* if node is not dead. */
|
|
|
|
if(close_clientlist[i].timestamp + Kill_NODE_TIMEOUT > temp_time) {
|
2013-06-27 19:37:06 +08:00
|
|
|
if((close_clientlist[i].last_pinged + PING_INTERVAL) <= temp_time)
|
2013-06-25 21:19:01 +08:00
|
|
|
{
|
2013-07-13 04:27:19 +08:00
|
|
|
pingreq(close_clientlist[i].ip_port, close_clientlist[i].client_id);
|
2013-06-27 19:37:06 +08:00
|
|
|
close_clientlist[i].last_pinged = temp_time;
|
2013-06-25 21:19:01 +08:00
|
|
|
}
|
2013-07-26 16:02:17 +08:00
|
|
|
/* if node is good. */
|
|
|
|
if(close_clientlist[i].timestamp + BAD_NODE_TIMEOUT > temp_time) {
|
2013-06-26 00:11:11 +08:00
|
|
|
index[num_nodes] = i;
|
2013-07-19 23:55:34 +08:00
|
|
|
++num_nodes;
|
2013-06-26 00:11:11 +08:00
|
|
|
}
|
2013-06-25 21:19:01 +08:00
|
|
|
}
|
2013-06-26 00:11:11 +08:00
|
|
|
}
|
|
|
|
|
2013-07-26 16:02:17 +08:00
|
|
|
if(close_lastgetnodes + GET_NODE_INTERVAL <= temp_time && num_nodes != 0) {
|
2013-06-26 00:11:11 +08:00
|
|
|
rand_node = rand() % num_nodes;
|
|
|
|
getnodes(close_clientlist[index[rand_node]].ip_port,
|
2013-07-13 04:27:19 +08:00
|
|
|
close_clientlist[index[rand_node]].client_id,
|
|
|
|
self_public_key);
|
2013-06-26 00:11:11 +08:00
|
|
|
close_lastgetnodes = temp_time;
|
|
|
|
}
|
2013-06-25 00:23:46 +08:00
|
|
|
}
|
|
|
|
|
2013-07-13 04:27:19 +08:00
|
|
|
void DHT_bootstrap(IP_Port ip_port, uint8_t * public_key)
|
2013-06-24 13:05:28 +08:00
|
|
|
{
|
2013-07-20 11:00:10 +08:00
|
|
|
getnodes(ip_port, public_key, self_public_key);
|
2013-06-24 19:26:30 +08:00
|
|
|
}
|
|
|
|
|
2013-07-22 01:14:36 +08:00
|
|
|
/* send the given packet to node with client_id
|
|
|
|
returns -1 if failure */
|
|
|
|
int route_packet(uint8_t * client_id, uint8_t * packet, uint32_t length)
|
|
|
|
{
|
|
|
|
uint32_t i;
|
2013-07-26 16:02:17 +08:00
|
|
|
for(i = 0; i < LCLIENT_LIST; ++i) {
|
|
|
|
if(memcmp(client_id, close_clientlist[i].client_id, CLIENT_ID_SIZE) == 0) {
|
2013-07-22 01:14:36 +08:00
|
|
|
return sendpacket(close_clientlist[i].ip_port, packet, length);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-07-25 07:39:34 +08:00
|
|
|
/* Puts all the different ips returned by the nodes for a friend_num into array ip_portlist
|
|
|
|
ip_portlist must be at least MAX_FRIEND_CLIENTS big
|
|
|
|
returns the number of ips returned
|
|
|
|
return 0 if we are connected to friend or if no ips were found.
|
|
|
|
returns -1 if no such friend*/
|
|
|
|
static int friend_iplist(IP_Port * ip_portlist, uint16_t friend_num)
|
|
|
|
{
|
|
|
|
int num_ips = 0;
|
|
|
|
uint32_t i;
|
|
|
|
uint32_t temp_time = unix_time();
|
2013-07-26 16:02:17 +08:00
|
|
|
if(friend_num >= num_friends) {
|
2013-07-25 07:39:34 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
for(i = 0; i < MAX_FRIEND_CLIENTS; ++i)
|
|
|
|
{
|
2013-07-26 16:02:17 +08:00
|
|
|
/*If ip is not zero and node is good */
|
2013-07-25 07:39:34 +08:00
|
|
|
if(friends_list[friend_num].client_list[i].ret_ip_port.ip.i != 0 &&
|
2013-07-26 16:02:17 +08:00
|
|
|
friends_list[friend_num].client_list[i].ret_timestamp + BAD_NODE_TIMEOUT > temp_time) {
|
|
|
|
if(memcmp(friends_list[friend_num].client_list[i].client_id, friends_list[friend_num].client_id, CLIENT_ID_SIZE) == 0 ) {
|
2013-07-25 07:39:34 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
ip_portlist[num_ips] = friends_list[friend_num].client_list[i].ret_ip_port;
|
|
|
|
++num_ips;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return num_ips;
|
|
|
|
}
|
|
|
|
|
2013-07-22 01:14:36 +08:00
|
|
|
/* 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(uint8_t * friend_id, uint8_t * packet, uint32_t length)
|
|
|
|
{
|
|
|
|
uint32_t i, j;
|
|
|
|
uint32_t sent = 0;
|
|
|
|
uint32_t temp_time = unix_time();
|
2013-07-26 16:02:17 +08:00
|
|
|
for(i = 0; i < num_friends; ++i) {
|
|
|
|
/* Equal */
|
|
|
|
if(memcmp(friends_list[i].client_id, friend_id, CLIENT_ID_SIZE) == 0) {
|
|
|
|
for(j = 0; j < MAX_FRIEND_CLIENTS; ++j) {
|
|
|
|
/*If ip is not zero and node is good */
|
2013-07-22 01:14:36 +08:00
|
|
|
if(friends_list[i].client_list[j].ret_ip_port.ip.i != 0 &&
|
2013-07-26 16:02:17 +08:00
|
|
|
friends_list[i].client_list[j].ret_timestamp + BAD_NODE_TIMEOUT > temp_time) {
|
|
|
|
if(sendpacket(friends_list[i].client_list[j].ip_port, packet, length) == length) {
|
2013-07-22 01:14:36 +08:00
|
|
|
++sent;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return sent;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-07-25 07:39:34 +08:00
|
|
|
/* 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 */
|
|
|
|
int routeone_tofriend(uint8_t * friend_id, uint8_t * packet, uint32_t length)
|
|
|
|
{
|
|
|
|
int num = friend_number(friend_id);
|
2013-07-26 16:02:17 +08:00
|
|
|
if(num == -1) {
|
2013-07-25 07:39:34 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
IP_Port ip_list[MAX_FRIEND_CLIENTS];
|
|
|
|
int n = 0;
|
|
|
|
uint32_t i;
|
|
|
|
uint32_t temp_time = unix_time();
|
2013-07-26 16:02:17 +08:00
|
|
|
for(i = 0; i < MAX_FRIEND_CLIENTS; ++i) {
|
|
|
|
/*If ip is not zero and node is good */
|
2013-07-25 07:39:34 +08:00
|
|
|
if(friends_list[num].client_list[i].ret_ip_port.ip.i != 0 &&
|
2013-07-26 16:02:17 +08:00
|
|
|
friends_list[num].client_list[i].ret_timestamp + BAD_NODE_TIMEOUT > temp_time) {
|
2013-07-25 07:39:34 +08:00
|
|
|
ip_list[n] = friends_list[num].client_list[i].ip_port;
|
|
|
|
++n;
|
|
|
|
}
|
|
|
|
}
|
2013-07-26 16:02:17 +08:00
|
|
|
if(n < 1) {
|
2013-07-25 07:39:34 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2013-07-26 16:02:17 +08:00
|
|
|
if(sendpacket(ip_list[rand() % n], packet, length) == length) {
|
2013-07-25 07:39:34 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-07-22 01:14:36 +08:00
|
|
|
/* Puts all the different ips returned by the nodes for a friend_id into array ip_portlist
|
|
|
|
ip_portlist must be at least MAX_FRIEND_CLIENTS big
|
|
|
|
returns the number of ips returned
|
2013-07-25 07:39:34 +08:00
|
|
|
return 0 if we are connected to friend or if no ips were found.
|
2013-07-22 01:14:36 +08:00
|
|
|
returns -1 if no such friend*/
|
|
|
|
int friend_ips(IP_Port * ip_portlist, uint8_t * friend_id)
|
|
|
|
{
|
2013-07-25 07:39:34 +08:00
|
|
|
|
|
|
|
uint32_t i;
|
2013-07-26 16:02:17 +08:00
|
|
|
for(i = 0; i < num_friends; ++i) {
|
|
|
|
/* Equal */
|
|
|
|
if(memcmp(friends_list[i].client_id, friend_id, CLIENT_ID_SIZE) == 0) {
|
2013-07-25 07:39:34 +08:00
|
|
|
return friend_iplist(ip_portlist, i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*BEGINNING OF NAT PUNCHING FUNCTIONS*/
|
|
|
|
|
|
|
|
int send_NATping(uint8_t * public_key, uint64_t ping_id, uint8_t type)
|
|
|
|
{
|
|
|
|
uint8_t data[sizeof(uint64_t) + 1];
|
|
|
|
data[0] = type;
|
|
|
|
memcpy(data + 1, &ping_id, sizeof(uint64_t));
|
|
|
|
|
|
|
|
uint8_t packet[MAX_DATA_SIZE];
|
|
|
|
int len = create_request(packet, public_key, data, sizeof(uint64_t) + 1, 254); /* 254 is NAT ping request packet id */
|
2013-07-26 16:02:17 +08:00
|
|
|
if(len == -1) {
|
2013-07-25 07:39:34 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
int num = 0;
|
2013-07-26 16:02:17 +08:00
|
|
|
if(type == 0) {
|
2013-07-25 07:39:34 +08:00
|
|
|
num = route_tofriend(public_key, packet, len);/*If packet is request use many people to route it*/
|
|
|
|
}
|
2013-07-26 16:02:17 +08:00
|
|
|
else if(type == 1) {
|
2013-07-25 07:39:34 +08:00
|
|
|
num = routeone_tofriend(public_key, packet, len);/*If packet is response use only one person to route it*/
|
|
|
|
}
|
2013-07-26 16:02:17 +08:00
|
|
|
if(num == 0) {
|
2013-07-25 07:39:34 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return num;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Handle a recieved ping request for */
|
|
|
|
int handle_NATping(uint8_t * packet, uint32_t length, IP_Port source)
|
|
|
|
{
|
|
|
|
if(length <= crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1 + ENCRYPTION_PADDING &&
|
2013-07-26 16:02:17 +08:00
|
|
|
length > MAX_DATA_SIZE + ENCRYPTION_PADDING) {
|
2013-07-25 07:39:34 +08:00
|
|
|
return 1;
|
|
|
|
}
|
2013-07-26 16:02:17 +08:00
|
|
|
/* check if request is for us. */
|
|
|
|
if(memcmp(packet + 1, self_public_key, crypto_box_PUBLICKEYBYTES) == 0) {
|
2013-07-25 07:39:34 +08:00
|
|
|
uint8_t public_key[crypto_box_PUBLICKEYBYTES];
|
|
|
|
uint8_t data[MAX_DATA_SIZE];
|
|
|
|
int len = handle_request(public_key, data, packet, length);
|
2013-07-26 16:02:17 +08:00
|
|
|
if(len != sizeof(uint64_t) + 1) {
|
2013-07-25 07:39:34 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
uint64_t ping_id;
|
|
|
|
memcpy(&ping_id, data + 1, sizeof(uint64_t));
|
|
|
|
|
|
|
|
int friendnumber = friend_number(public_key);
|
2013-07-26 16:02:17 +08:00
|
|
|
if(friendnumber == -1) {
|
2013-07-25 07:39:34 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-07-26 16:02:17 +08:00
|
|
|
if(data[0] == 0) {
|
2013-07-25 07:39:34 +08:00
|
|
|
send_NATping(public_key, ping_id, 1);/*1 is reply*/
|
|
|
|
return 0;
|
2013-07-26 16:02:17 +08:00
|
|
|
} else if (data[0] == 1) {
|
2013-07-25 07:39:34 +08:00
|
|
|
if(friends_list[friendnumber].NATping_id == ping_id)
|
|
|
|
{
|
|
|
|
friends_list[friendnumber].NATping_id = ((uint64_t)random_int() << 32) + random_int();
|
|
|
|
friends_list[friendnumber].hole_punching = 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
2013-07-26 16:02:17 +08:00
|
|
|
/* if request is not for us, try routing it. */
|
|
|
|
else {
|
|
|
|
if(route_packet(packet + 1, packet, length) == length) {
|
2013-07-25 07:39:34 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*Get the most common ip in the ip_portlist
|
|
|
|
Only return ip if it appears in list min_num or more
|
|
|
|
len must not be bigger than MAX_FRIEND_CLIENTS
|
|
|
|
return ip of 0 if failure */
|
|
|
|
static IP NAT_commonip(IP_Port * ip_portlist, uint16_t len, uint16_t min_num)
|
|
|
|
{
|
|
|
|
IP zero = {{0}};
|
2013-07-26 16:02:17 +08:00
|
|
|
if(len > MAX_FRIEND_CLIENTS) {
|
2013-07-25 07:39:34 +08:00
|
|
|
return zero;
|
|
|
|
}
|
|
|
|
|
2013-07-22 01:14:36 +08:00
|
|
|
uint32_t i, j;
|
2013-07-25 07:39:34 +08:00
|
|
|
uint16_t numbers[MAX_FRIEND_CLIENTS] = {0};
|
2013-07-26 16:02:17 +08:00
|
|
|
for(i = 0; i < len; ++i) {
|
|
|
|
for(j = 0; j < len; ++j) {
|
|
|
|
if(ip_portlist[i].ip.i == ip_portlist[j].ip.i) {
|
2013-07-25 07:39:34 +08:00
|
|
|
++numbers[i];
|
|
|
|
}
|
|
|
|
}
|
2013-07-26 16:02:17 +08:00
|
|
|
if(numbers[i] >= min_num) {
|
2013-07-25 07:39:34 +08:00
|
|
|
return ip_portlist[i].ip;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return zero;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*Return all the ports for one ip in a list
|
|
|
|
portlist must be at least len long
|
|
|
|
where len is the length of ip_portlist
|
|
|
|
returns the number of ports and puts the list of ports in portlist*/
|
|
|
|
static uint16_t NAT_getports(uint16_t * portlist, IP_Port * ip_portlist, uint16_t len, IP ip)
|
|
|
|
{
|
|
|
|
uint32_t i;
|
|
|
|
uint16_t num = 0;
|
2013-07-26 16:02:17 +08:00
|
|
|
for(i = 0; i < len; ++i) {
|
|
|
|
if(ip_portlist[i].ip.i == ip.i) {
|
2013-07-25 07:39:34 +08:00
|
|
|
portlist[num] = ntohs(ip_portlist[i].port);
|
|
|
|
++num;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return num;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define MAX_PUNCHING_PORTS 32
|
|
|
|
|
|
|
|
static void punch_holes(IP ip, uint16_t * port_list, uint16_t numports, uint16_t friend_num)
|
|
|
|
{
|
2013-07-26 16:02:17 +08:00
|
|
|
if(numports > MAX_FRIEND_CLIENTS || numports == 0) {
|
2013-07-25 07:39:34 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
uint32_t i;
|
|
|
|
uint32_t top = friends_list[friend_num].punching_index + MAX_PUNCHING_PORTS;
|
2013-07-26 16:02:17 +08:00
|
|
|
for(i = friends_list[friend_num].punching_index; i != top; i++) {
|
2013-07-25 07:39:34 +08:00
|
|
|
/*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)};
|
|
|
|
pingreq(pinging, friends_list[friend_num].client_id);
|
|
|
|
}
|
|
|
|
friends_list[friend_num].punching_index = i;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*Interval in seconds between punching attempts*/
|
|
|
|
#define PUNCH_INTERVAL 10
|
|
|
|
|
|
|
|
static void doNAT()
|
|
|
|
{
|
|
|
|
uint32_t i;
|
2013-07-22 01:14:36 +08:00
|
|
|
uint32_t temp_time = unix_time();
|
2013-07-26 16:02:17 +08:00
|
|
|
for(i = 0; i < num_friends; ++i) {
|
2013-07-25 07:39:34 +08:00
|
|
|
IP_Port ip_list[MAX_FRIEND_CLIENTS];
|
|
|
|
int num = friend_iplist(ip_list, i);
|
|
|
|
/*If we are connected to friend or if friend is not online don't try to hole punch with him*/
|
2013-07-26 16:02:17 +08:00
|
|
|
if(num < MAX_FRIEND_CLIENTS/2) {
|
2013-07-25 07:39:34 +08:00
|
|
|
continue;
|
|
|
|
}
|
2013-07-26 16:02:17 +08:00
|
|
|
if(friends_list[i].hole_punching != 1) {
|
|
|
|
if(friends_list[i].NATping_timestamp + PUNCH_INTERVAL < temp_time) {
|
2013-07-25 07:39:34 +08:00
|
|
|
send_NATping(friends_list[i].client_id, friends_list[i].NATping_id, 0); /*0 is request*/
|
|
|
|
friends_list[i].NATping_timestamp = temp_time;
|
2013-07-22 01:14:36 +08:00
|
|
|
}
|
|
|
|
}
|
2013-07-26 16:02:17 +08:00
|
|
|
else if(friends_list[i].punching_timestamp + PUNCH_INTERVAL < temp_time) {
|
2013-07-25 07:39:34 +08:00
|
|
|
IP ip = NAT_commonip(ip_list, num, MAX_FRIEND_CLIENTS/2);
|
2013-07-26 16:02:17 +08:00
|
|
|
if(ip.i == 0) {
|
2013-07-25 07:39:34 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
uint16_t port_list[MAX_FRIEND_CLIENTS];
|
|
|
|
uint16_t numports = NAT_getports(port_list, ip_list, num, ip);
|
|
|
|
punch_holes(ip, port_list, numports, i);
|
|
|
|
|
|
|
|
friends_list[i].punching_timestamp = temp_time;
|
|
|
|
friends_list[i].hole_punching = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*END OF NAT PUNCHING FUNCTIONS*/
|
|
|
|
|
|
|
|
int DHT_handlepacket(uint8_t * packet, uint32_t length, IP_Port source)
|
|
|
|
{
|
|
|
|
switch (packet[0]) {
|
|
|
|
case 0:
|
|
|
|
return handle_pingreq(packet, length, source);
|
|
|
|
|
|
|
|
case 1:
|
|
|
|
return handle_pingres(packet, length, source);
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
return handle_getnodes(packet, length, source);
|
|
|
|
|
|
|
|
case 3:
|
|
|
|
return handle_sendnodes(packet, length, source);
|
|
|
|
|
|
|
|
case 254:
|
|
|
|
return handle_NATping(packet, length, source);
|
|
|
|
|
|
|
|
default:
|
|
|
|
return 1;
|
2013-07-22 01:14:36 +08:00
|
|
|
|
|
|
|
}
|
2013-07-25 07:39:34 +08:00
|
|
|
|
2013-07-22 01:14:36 +08:00
|
|
|
return 0;
|
2013-07-25 07:39:34 +08:00
|
|
|
|
2013-07-22 01:14:36 +08:00
|
|
|
}
|
|
|
|
|
2013-07-25 07:39:34 +08:00
|
|
|
void doDHT()
|
|
|
|
{
|
|
|
|
doClose();
|
|
|
|
doDHTFriends();
|
|
|
|
doNAT();
|
|
|
|
}
|
|
|
|
|
2013-07-20 11:00:10 +08:00
|
|
|
/* get the size of the DHT (for saving) */
|
2013-07-17 01:20:45 +08:00
|
|
|
uint32_t DHT_size()
|
|
|
|
{
|
|
|
|
return sizeof(close_clientlist) + sizeof(Friend) * num_friends;
|
|
|
|
}
|
|
|
|
|
2013-07-20 11:00:10 +08:00
|
|
|
/* save the DHT in data where data is an array of size DHT_size() */
|
2013-07-17 01:20:45 +08:00
|
|
|
void DHT_save(uint8_t * data)
|
|
|
|
{
|
|
|
|
memcpy(data, close_clientlist, sizeof(close_clientlist));
|
|
|
|
memcpy(data + sizeof(close_clientlist), friends_list, sizeof(Friend) * num_friends);
|
|
|
|
}
|
|
|
|
|
2013-07-20 11:00:10 +08:00
|
|
|
/* load the DHT from data of size size;
|
|
|
|
return -1 if failure
|
|
|
|
return 0 if success */
|
2013-07-17 01:20:45 +08:00
|
|
|
int DHT_load(uint8_t * data, uint32_t size)
|
|
|
|
{
|
2013-07-26 16:02:17 +08:00
|
|
|
if(size < sizeof(close_clientlist)) {
|
2013-07-17 01:20:45 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2013-07-26 16:02:17 +08:00
|
|
|
if((size - sizeof(close_clientlist)) % sizeof(Friend) != 0) {
|
2013-07-17 01:20:45 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
uint32_t i, j;
|
2013-07-20 11:00:10 +08:00
|
|
|
/* uint32_t temp_time = unix_time(); */
|
2013-07-17 01:20:45 +08:00
|
|
|
uint16_t temp;
|
|
|
|
|
|
|
|
temp = (size - sizeof(close_clientlist))/sizeof(Friend);
|
|
|
|
|
2013-07-26 16:02:17 +08:00
|
|
|
if(temp != 0) {
|
2013-07-17 01:20:45 +08:00
|
|
|
Friend * tempfriends_list = (Friend *)(data + sizeof(close_clientlist));
|
|
|
|
|
2013-07-26 16:02:17 +08:00
|
|
|
for(i = 0; i < temp; ++i) {
|
2013-07-17 01:20:45 +08:00
|
|
|
DHT_addfriend(tempfriends_list[i].client_id);
|
2013-07-26 16:02:17 +08:00
|
|
|
for(j = 0; j < MAX_FRIEND_CLIENTS; ++j) {
|
|
|
|
if(tempfriends_list[i].client_list[j].timestamp != 0) {
|
2013-07-17 01:20:45 +08:00
|
|
|
getnodes(tempfriends_list[i].client_list[j].ip_port,
|
|
|
|
tempfriends_list[i].client_list[j].client_id, tempfriends_list[i].client_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Client_data * tempclose_clientlist = (Client_data *)data;
|
|
|
|
|
2013-07-26 16:02:17 +08:00
|
|
|
for(i = 0; i < LCLIENT_LIST; ++i) {
|
|
|
|
if(tempclose_clientlist[i].timestamp != 0) {
|
2013-07-17 01:20:45 +08:00
|
|
|
DHT_bootstrap(tempclose_clientlist[i].ip_port, tempclose_clientlist[i].client_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2013-07-17 07:45:20 +08:00
|
|
|
|
2013-07-20 11:00:10 +08:00
|
|
|
/* returns 0 if we are not connected to the DHT
|
|
|
|
returns 1 if we are */
|
2013-07-17 07:45:20 +08:00
|
|
|
int DHT_isconnected()
|
|
|
|
{
|
|
|
|
uint32_t i;
|
|
|
|
uint32_t temp_time = unix_time();
|
2013-07-26 16:02:17 +08:00
|
|
|
for(i = 0; i < LCLIENT_LIST; ++i) {
|
|
|
|
if(close_clientlist[i].timestamp + BAD_NODE_TIMEOUT > temp_time) {
|
2013-07-17 07:45:20 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
2013-07-26 16:02:17 +08:00
|
|
|
}
|