mirror of
https://github.com/irungentoo/toxcore.git
synced 2024-03-22 13:30:51 +08:00
A *lot* of style changes.
This commit is contained in:
parent
1b4ea2e1ae
commit
241aca98bd
371
core/DHT.c
371
core/DHT.c
|
@ -23,8 +23,7 @@
|
|||
|
||||
#include "DHT.h"
|
||||
|
||||
typedef struct
|
||||
{
|
||||
typedef struct {
|
||||
uint8_t client_id[CLIENT_ID_SIZE];
|
||||
IP_Port ip_port;
|
||||
uint32_t timestamp;
|
||||
|
@ -32,13 +31,12 @@ typedef struct
|
|||
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) */
|
||||
uint32_t ret_timestamp;
|
||||
}Client_data;
|
||||
} Client_data;
|
||||
|
||||
/* maximum number of clients stored per friend. */
|
||||
#define MAX_FRIEND_CLIENTS 8
|
||||
|
||||
typedef struct
|
||||
{
|
||||
typedef struct {
|
||||
uint8_t client_id[CLIENT_ID_SIZE];
|
||||
Client_data client_list[MAX_FRIEND_CLIENTS];
|
||||
uint32_t lastgetnode; /* time at which the last get_nodes request was sent. */
|
||||
|
@ -49,21 +47,19 @@ typedef struct
|
|||
uint32_t punching_timestamp;
|
||||
uint64_t NATping_id;
|
||||
uint32_t NATping_timestamp;
|
||||
}Friend;
|
||||
} Friend;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
typedef struct {
|
||||
uint8_t client_id[CLIENT_ID_SIZE];
|
||||
IP_Port ip_port;
|
||||
}Node_format;
|
||||
} Node_format;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
typedef struct {
|
||||
IP_Port ip_port;
|
||||
uint64_t ping_id;
|
||||
uint32_t timestamp;
|
||||
|
||||
}Pinged;
|
||||
} Pinged;
|
||||
|
||||
/* Our client id/public key */
|
||||
uint8_t self_public_key[CLIENT_ID_SIZE];
|
||||
|
@ -74,7 +70,7 @@ uint8_t self_secret_key[crypto_box_SECRETKEYBYTES];
|
|||
#define LCLIENT_LIST 32
|
||||
static Client_data close_clientlist[LCLIENT_LIST];
|
||||
|
||||
static Friend * friends_list;
|
||||
static Friend *friends_list;
|
||||
static uint16_t num_friends;
|
||||
|
||||
/* The list of ip ports along with the ping_id of what we sent them and a timestamp */
|
||||
|
@ -90,19 +86,16 @@ static Pinged send_nodes[LSEND_NODES_ARRAY];
|
|||
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 */
|
||||
int id_closest(uint8_t *client_id, uint8_t *client_id1, uint8_t *client_id2) /* tested */
|
||||
{
|
||||
uint32_t i;
|
||||
for(i = 0; i < CLIENT_ID_SIZE; ++i) {
|
||||
if(abs(client_id[i] ^ client_id1[i]) < abs(client_id[i] ^ client_id2[i])) {
|
||||
for (i = 0; i < CLIENT_ID_SIZE; ++i) {
|
||||
if(abs(client_id[i] ^ client_id1[i]) < abs(client_id[i] ^ client_id2[i]))
|
||||
return 1;
|
||||
} else if(abs(client_id[i] ^ client_id1[i]) > abs(client_id[i] ^ client_id2[i])) {
|
||||
else if(abs(client_id[i] ^ client_id1[i]) > abs(client_id[i] ^ client_id2[i]))
|
||||
return 2;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
}
|
||||
|
||||
/* check if client with client_id is already in list of length length.
|
||||
|
@ -110,15 +103,16 @@ int id_closest(uint8_t * client_id, uint8_t * client_id1, uint8_t * client_id2)
|
|||
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. */
|
||||
int client_in_list(Client_data * list, uint32_t length, uint8_t * client_id, IP_Port ip_port)
|
||||
int client_in_list(Client_data *list, uint32_t length, uint8_t *client_id, IP_Port ip_port)
|
||||
{
|
||||
uint32_t i;
|
||||
uint32_t temp_time = unix_time();
|
||||
|
||||
for(i = 0; i < length; ++i) {
|
||||
for (i = 0; i < length; ++i) {
|
||||
/*If ip_port is assigned to a different client_id replace it*/
|
||||
if(list[i].ip_port.ip.i == ip_port.ip.i &&
|
||||
list[i].ip_port.port == ip_port.port) {
|
||||
list[i].ip_port.port == ip_port.port)
|
||||
{
|
||||
memcpy(list[i].client_id, client_id, CLIENT_ID_SIZE);
|
||||
}
|
||||
|
||||
|
@ -136,14 +130,12 @@ int client_in_list(Client_data * list, uint32_t length, uint8_t * client_id, IP_
|
|||
|
||||
/* check if client with client_id is already in node format list of length length.
|
||||
return True(1) or False(0) */
|
||||
int client_in_nodelist(Node_format * list, uint32_t length, uint8_t * client_id)
|
||||
int client_in_nodelist(Node_format *list, uint32_t length, uint8_t *client_id)
|
||||
{
|
||||
uint32_t i;
|
||||
for(i = 0; i < length; ++i) {
|
||||
if(memcmp(list[i].client_id, client_id, CLIENT_ID_SIZE) == 0) {
|
||||
|
||||
for (i = 0; i < length; ++i) {
|
||||
if (memcmp(list[i].client_id, client_id, CLIENT_ID_SIZE) == 0)
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
|
||||
|
@ -151,14 +143,12 @@ int client_in_nodelist(Node_format * list, uint32_t length, uint8_t * client_id)
|
|||
|
||||
/*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)
|
||||
static int friend_number(uint8_t *client_id)
|
||||
{
|
||||
uint32_t i;
|
||||
for(i = 0; i < num_friends; ++i) {
|
||||
if(memcmp(friends_list[i].client_id, client_id, CLIENT_ID_SIZE) == 0) /* Equal */
|
||||
{
|
||||
for (i = 0; i < num_friends; ++i) {
|
||||
if (memcmp(friends_list[i].client_id, client_id, CLIENT_ID_SIZE) == 0) /* Equal */
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
@ -171,14 +161,15 @@ static int friend_number(uint8_t * client_id)
|
|||
/* 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. */
|
||||
int get_close_nodes(uint8_t * client_id, Node_format * nodes_list)
|
||||
int get_close_nodes(uint8_t *client_id, Node_format * nodes_list)
|
||||
{
|
||||
uint32_t i, j, k;
|
||||
int num_nodes=0;
|
||||
uint32_t temp_time = unix_time();
|
||||
for(i = 0; i < LCLIENT_LIST; ++i) {
|
||||
if(close_clientlist[i].timestamp + BAD_NODE_TIMEOUT > temp_time &&
|
||||
!client_in_nodelist(nodes_list, MAX_SENT_NODES,close_clientlist[i].client_id)) {
|
||||
for (i = 0; i < LCLIENT_LIST; ++i) {
|
||||
if (close_clientlist[i].timestamp + BAD_NODE_TIMEOUT > temp_time &&
|
||||
!client_in_nodelist(nodes_list, MAX_SENT_NODES,close_clientlist[i].client_id))
|
||||
{
|
||||
/* if node is good and not already in list. */
|
||||
if(num_nodes < MAX_SENT_NODES) {
|
||||
memcpy(nodes_list[num_nodes].client_id, close_clientlist[i].client_id, CLIENT_ID_SIZE);
|
||||
|
@ -194,12 +185,12 @@ int get_close_nodes(uint8_t * client_id, Node_format * nodes_list)
|
|||
}
|
||||
}
|
||||
}
|
||||
for(i = 0; i < num_friends; ++i) {
|
||||
for(j = 0; j < MAX_FRIEND_CLIENTS; ++j) {
|
||||
if(friends_list[i].client_list[j].timestamp + BAD_NODE_TIMEOUT > temp_time &&
|
||||
for (i = 0; i < num_friends; ++i) {
|
||||
for (j = 0; j < MAX_FRIEND_CLIENTS; ++j) {
|
||||
if (friends_list[i].client_list[j].timestamp + BAD_NODE_TIMEOUT > temp_time &&
|
||||
!client_in_nodelist(nodes_list, MAX_SENT_NODES,friends_list[i].client_list[j].client_id)) {
|
||||
/* if node is good and not already in list. */
|
||||
if(num_nodes < MAX_SENT_NODES) {
|
||||
if (num_nodes < MAX_SENT_NODES) {
|
||||
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++;
|
||||
|
@ -220,12 +211,12 @@ int get_close_nodes(uint8_t * client_id, Node_format * nodes_list)
|
|||
/* 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 */
|
||||
int replace_bad(Client_data *list, uint32_t length, uint8_t *client_id, IP_Port ip_port) /* tested */
|
||||
{
|
||||
uint32_t i;
|
||||
uint32_t temp_time = unix_time();
|
||||
for(i = 0; i < length; ++i) {
|
||||
if(list[i].timestamp + BAD_NODE_TIMEOUT < temp_time) /* if node is bad. */ {
|
||||
for (i = 0; i < length; ++i) {
|
||||
if (list[i].timestamp + BAD_NODE_TIMEOUT < temp_time) /* if node is bad. */ {
|
||||
memcpy(list[i].client_id, client_id, CLIENT_ID_SIZE);
|
||||
list[i].ip_port = ip_port;
|
||||
list[i].timestamp = temp_time;
|
||||
|
@ -240,13 +231,13 @@ int replace_bad(Client_data * list, uint32_t length, uint8_t * client_id, IP_Por
|
|||
}
|
||||
|
||||
/* replace the first good node that is further to the comp_client_id than that of the client_id in the list */
|
||||
int replace_good(Client_data * list, uint32_t length, uint8_t * client_id, IP_Port ip_port, uint8_t * comp_client_id)
|
||||
int replace_good(Client_data *list, uint32_t length, uint8_t *client_id, IP_Port ip_port, uint8_t *comp_client_id)
|
||||
{
|
||||
uint32_t i;
|
||||
uint32_t temp_time = unix_time();
|
||||
|
||||
for(i = 0; i < length; ++i) {
|
||||
if(id_closest(comp_client_id, list[i].client_id, client_id) == 2) {
|
||||
for (i = 0; i < length; ++i) {
|
||||
if (id_closest(comp_client_id, list[i].client_id, client_id) == 2) {
|
||||
memcpy(list[i].client_id, client_id, CLIENT_ID_SIZE);
|
||||
list[i].ip_port = ip_port;
|
||||
list[i].timestamp = temp_time;
|
||||
|
@ -261,17 +252,14 @@ int replace_good(Client_data * list, uint32_t length, uint8_t * client_id, IP_Po
|
|||
}
|
||||
|
||||
/* Attempt to add client with ip_port and client_id to the friends client list and close_clientlist */
|
||||
void addto_lists(IP_Port ip_port, uint8_t * client_id)
|
||||
void addto_lists(IP_Port ip_port, uint8_t *client_id)
|
||||
{
|
||||
uint32_t i;
|
||||
|
||||
/* NOTE: current behavior if there are two clients with the same id is to replace the first ip by the second. */
|
||||
if(!client_in_list(close_clientlist, LCLIENT_LIST, client_id, ip_port)) {
|
||||
|
||||
if(replace_bad(close_clientlist, LCLIENT_LIST, client_id, ip_port)) {
|
||||
/* if we can't replace bad nodes we try replacing good ones */
|
||||
replace_good(close_clientlist, LCLIENT_LIST, client_id, ip_port, self_public_key);
|
||||
}
|
||||
if (!client_in_list(close_clientlist, LCLIENT_LIST, client_id, ip_port)) {
|
||||
if (replace_bad(close_clientlist, LCLIENT_LIST, client_id, ip_port))
|
||||
replace_good(close_clientlist, LCLIENT_LIST, client_id, ip_port, self_public_key); /* if we can't replace bad nodes we try replacing good ones */
|
||||
}
|
||||
for(i = 0; i < num_friends; ++i) {
|
||||
if(!client_in_list(friends_list[i].client_list, MAX_FRIEND_CLIENTS, client_id, ip_port)) {
|
||||
|
@ -285,30 +273,31 @@ void addto_lists(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 */
|
||||
void returnedip_ports(IP_Port ip_port, uint8_t * client_id, uint8_t * nodeclient_id)
|
||||
void returnedip_ports(IP_Port ip_port, uint8_t *client_id, uint8_t *nodeclient_id)
|
||||
{
|
||||
uint32_t i, j;
|
||||
uint32_t temp_time = unix_time();
|
||||
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) {
|
||||
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) {
|
||||
close_clientlist[i].ret_ip_port = ip_port;
|
||||
close_clientlist[i].ret_timestamp = temp_time;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
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) {
|
||||
friends_list[i].client_list[j].ret_ip_port = ip_port;
|
||||
friends_list[i].client_list[j].ret_timestamp = temp_time;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
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) {
|
||||
friends_list[i].client_list[j].ret_ip_port = ip_port;
|
||||
friends_list[i].client_list[j].ret_timestamp = temp_time;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -326,25 +315,23 @@ int is_pinging(IP_Port ip_port, uint64_t ping_id)
|
|||
uint8_t pinging;
|
||||
uint32_t temp_time = unix_time();
|
||||
|
||||
for(i = 0; i < LPING_ARRAY; ++i ) {
|
||||
if((pings[i].timestamp + PING_TIMEOUT) > temp_time) {
|
||||
for (i = 0; i < LPING_ARRAY; ++i ) {
|
||||
if ((pings[i].timestamp + PING_TIMEOUT) > temp_time) {
|
||||
pinging = 0;
|
||||
if(ip_port.ip.i != 0) {
|
||||
if (ip_port.ip.i != 0) {
|
||||
if(pings[i].ip_port.ip.i == ip_port.ip.i &&
|
||||
pings[i].ip_port.port == ip_port.port) {
|
||||
++pinging;
|
||||
}
|
||||
}
|
||||
if(ping_id != 0) {
|
||||
if(pings[i].ping_id == ping_id)
|
||||
pings[i].ip_port.port == ip_port.port)
|
||||
{
|
||||
++pinging;
|
||||
}
|
||||
}
|
||||
if(pinging == (ping_id != 0) + (ip_port.ip.i != 0)) {
|
||||
return 1;
|
||||
if (ping_id != 0) {
|
||||
if(pings[i].ping_id == ping_id)
|
||||
++pinging;
|
||||
|
||||
}
|
||||
|
||||
if(pinging == (ping_id != 0) + (ip_port.ip.i != 0))
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -359,24 +346,21 @@ int is_gettingnodes(IP_Port ip_port, uint64_t ping_id)
|
|||
uint8_t pinging;
|
||||
uint32_t temp_time = unix_time();
|
||||
|
||||
for(i = 0; i < LSEND_NODES_ARRAY; ++i ) {
|
||||
if((send_nodes[i].timestamp + PING_TIMEOUT) > temp_time) {
|
||||
for(i = 0; i < LSEND_NODES_ARRAY; ++i) {
|
||||
if ((send_nodes[i].timestamp + PING_TIMEOUT) > temp_time) {
|
||||
pinging = 0;
|
||||
if(ip_port.ip.i != 0) {
|
||||
if(send_nodes[i].ip_port.ip.i == ip_port.ip.i &&
|
||||
if (ip_port.ip.i != 0) {
|
||||
if (send_nodes[i].ip_port.ip.i == ip_port.ip.i &&
|
||||
send_nodes[i].ip_port.port == ip_port.port) {
|
||||
++pinging;
|
||||
}
|
||||
}
|
||||
if(ping_id != 0) {
|
||||
if(send_nodes[i].ping_id == ping_id) {
|
||||
if (ping_id != 0) {
|
||||
if (send_nodes[i].ping_id == ping_id)
|
||||
++pinging;
|
||||
}
|
||||
}
|
||||
if(pinging == (ping_id != 0) + (ip_port.ip.i != 0)) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
if(pinging == (ping_id != 0) + (ip_port.ip.i != 0))
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -394,9 +378,9 @@ uint64_t add_pinging(IP_Port ip_port)
|
|||
uint64_t ping_id = ((uint64_t)random_int() << 32) + random_int();
|
||||
uint32_t temp_time = unix_time();
|
||||
|
||||
for(i = 0; i < PING_TIMEOUT; ++i ) {
|
||||
for(j = 0; j < LPING_ARRAY; ++j ) {
|
||||
if((pings[j].timestamp + PING_TIMEOUT - i) < temp_time) {
|
||||
for (i = 0; i < PING_TIMEOUT; ++i ) {
|
||||
for (j = 0; j < LPING_ARRAY; ++j ) {
|
||||
if ((pings[j].timestamp + PING_TIMEOUT - i) < temp_time) {
|
||||
pings[j].timestamp = temp_time;
|
||||
pings[j].ip_port = ip_port;
|
||||
pings[j].ping_id = ping_id;
|
||||
|
@ -415,9 +399,9 @@ uint64_t add_gettingnodes(IP_Port ip_port)
|
|||
uint64_t ping_id = ((uint64_t)random_int() << 32) + random_int();
|
||||
uint32_t temp_time = unix_time();
|
||||
|
||||
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) {
|
||||
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) {
|
||||
send_nodes[j].timestamp = temp_time;
|
||||
send_nodes[j].ip_port = ip_port;
|
||||
send_nodes[j].ping_id = ping_id;
|
||||
|
@ -431,20 +415,17 @@ uint64_t add_gettingnodes(IP_Port ip_port)
|
|||
|
||||
/* send a ping request
|
||||
Ping request only works if none has been sent to that ip/port in the last 5 seconds. */
|
||||
static int pingreq(IP_Port ip_port, uint8_t * public_key)
|
||||
static int pingreq(IP_Port ip_port, uint8_t *public_key)
|
||||
{
|
||||
if(memcmp(public_key, self_public_key, CLIENT_ID_SIZE) == 0) /* check if packet is gonna be sent to ourself */ {
|
||||
if (memcmp(public_key, self_public_key, CLIENT_ID_SIZE) == 0) /* check if packet is gonna be sent to ourself */
|
||||
return 1;
|
||||
}
|
||||
|
||||
if(is_pinging(ip_port, 0)) {
|
||||
if (is_pinging(ip_port, 0))
|
||||
return 1;
|
||||
}
|
||||
|
||||
uint64_t ping_id = add_pinging(ip_port);
|
||||
if(ping_id == 0) {
|
||||
if (ping_id == 0)
|
||||
return 1;
|
||||
}
|
||||
|
||||
uint8_t data[1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) + ENCRYPTION_PADDING];
|
||||
uint8_t encrypt[sizeof(ping_id) + ENCRYPTION_PADDING];
|
||||
|
@ -452,9 +433,8 @@ static int pingreq(IP_Port ip_port, uint8_t * public_key)
|
|||
random_nonce(nonce);
|
||||
|
||||
int len = encrypt_data(public_key, self_secret_key, nonce, (uint8_t *)&ping_id, sizeof(ping_id), encrypt);
|
||||
if(len != sizeof(ping_id) + ENCRYPTION_PADDING) {
|
||||
if(len != sizeof(ping_id) + ENCRYPTION_PADDING)
|
||||
return -1;
|
||||
}
|
||||
data[0] = 0;
|
||||
memcpy(data + 1, self_public_key, CLIENT_ID_SIZE);
|
||||
memcpy(data + 1 + CLIENT_ID_SIZE, nonce, crypto_box_NONCEBYTES);
|
||||
|
@ -465,12 +445,11 @@ static int pingreq(IP_Port ip_port, uint8_t * public_key)
|
|||
}
|
||||
|
||||
/* send a ping response */
|
||||
static int pingres(IP_Port ip_port, uint8_t * public_key, uint64_t ping_id)
|
||||
static int pingres(IP_Port ip_port, uint8_t *public_key, uint64_t ping_id)
|
||||
{
|
||||
/* check if packet is gonna be sent to ourself */
|
||||
if(memcmp(public_key, self_public_key, CLIENT_ID_SIZE) == 0) {
|
||||
if (memcmp(public_key, self_public_key, CLIENT_ID_SIZE) == 0)
|
||||
return 1;
|
||||
}
|
||||
|
||||
uint8_t data[1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) + ENCRYPTION_PADDING];
|
||||
uint8_t encrypt[sizeof(ping_id) + ENCRYPTION_PADDING];
|
||||
|
@ -478,9 +457,8 @@ static int pingres(IP_Port ip_port, uint8_t * public_key, uint64_t ping_id)
|
|||
random_nonce(nonce);
|
||||
|
||||
int len = encrypt_data(public_key, self_secret_key, nonce, (uint8_t *)&ping_id, sizeof(ping_id), encrypt);
|
||||
if(len != sizeof(ping_id) + ENCRYPTION_PADDING) {
|
||||
if (len != sizeof(ping_id) + ENCRYPTION_PADDING)
|
||||
return -1;
|
||||
}
|
||||
data[0] = 1;
|
||||
memcpy(data + 1, self_public_key, CLIENT_ID_SIZE);
|
||||
memcpy(data + 1 + CLIENT_ID_SIZE, nonce, crypto_box_NONCEBYTES);
|
||||
|
@ -491,22 +469,18 @@ static int pingres(IP_Port ip_port, uint8_t * public_key, uint64_t ping_id)
|
|||
}
|
||||
|
||||
/* send a getnodes request */
|
||||
static int getnodes(IP_Port ip_port, uint8_t * public_key, uint8_t * client_id)
|
||||
static int getnodes(IP_Port ip_port, uint8_t *public_key, uint8_t *client_id)
|
||||
{
|
||||
/* check if packet is gonna be sent to ourself */
|
||||
if(memcmp(public_key, self_public_key, CLIENT_ID_SIZE) == 0) {
|
||||
if (memcmp(public_key, self_public_key, CLIENT_ID_SIZE) == 0)
|
||||
return 1;
|
||||
}
|
||||
|
||||
if(is_gettingnodes(ip_port, 0)) {
|
||||
if (is_gettingnodes(ip_port, 0))
|
||||
return 1;
|
||||
}
|
||||
|
||||
uint64_t ping_id = add_gettingnodes(ip_port);
|
||||
|
||||
if(ping_id == 0) {
|
||||
if (ping_id == 0)
|
||||
return 1;
|
||||
}
|
||||
|
||||
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];
|
||||
|
@ -519,9 +493,8 @@ static int getnodes(IP_Port ip_port, uint8_t * public_key, uint8_t * client_id)
|
|||
|
||||
int len = encrypt_data(public_key, self_secret_key, nonce, plain, sizeof(ping_id) + CLIENT_ID_SIZE, encrypt);
|
||||
|
||||
if(len != sizeof(ping_id) + CLIENT_ID_SIZE + ENCRYPTION_PADDING) {
|
||||
if (len != sizeof(ping_id) + CLIENT_ID_SIZE + ENCRYPTION_PADDING)
|
||||
return -1;
|
||||
}
|
||||
data[0] = 2;
|
||||
memcpy(data + 1, self_public_key, CLIENT_ID_SIZE);
|
||||
memcpy(data + 1 + CLIENT_ID_SIZE, nonce, crypto_box_NONCEBYTES);
|
||||
|
@ -531,11 +504,10 @@ static int getnodes(IP_Port ip_port, uint8_t * public_key, uint8_t * client_id)
|
|||
}
|
||||
|
||||
/* send a send nodes response */
|
||||
static int sendnodes(IP_Port ip_port, uint8_t * public_key, uint8_t * client_id, uint64_t ping_id)
|
||||
static int sendnodes(IP_Port ip_port, uint8_t *public_key, uint8_t *client_id, uint64_t ping_id)
|
||||
{
|
||||
if(memcmp(public_key, self_public_key, CLIENT_ID_SIZE) == 0) /* check if packet is gonna be sent to ourself */ {
|
||||
if (memcmp(public_key, self_public_key, CLIENT_ID_SIZE) == 0) /* check if packet is gonna be sent to ourself */
|
||||
return 1;
|
||||
}
|
||||
|
||||
uint8_t data[1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id)
|
||||
+ sizeof(Node_format) * MAX_SENT_NODES + ENCRYPTION_PADDING];
|
||||
|
@ -543,9 +515,8 @@ static int sendnodes(IP_Port ip_port, uint8_t * public_key, uint8_t * client_id,
|
|||
Node_format nodes_list[MAX_SENT_NODES];
|
||||
int num_nodes = get_close_nodes(client_id, nodes_list);
|
||||
|
||||
if(num_nodes == 0) {
|
||||
if (num_nodes == 0)
|
||||
return 0;
|
||||
}
|
||||
|
||||
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];
|
||||
|
@ -558,9 +529,8 @@ static int sendnodes(IP_Port ip_port, uint8_t * public_key, uint8_t * client_id,
|
|||
int len = encrypt_data(public_key, self_secret_key, nonce, plain,
|
||||
sizeof(ping_id) + num_nodes * sizeof(Node_format), encrypt);
|
||||
|
||||
if(len != sizeof(ping_id) + num_nodes * sizeof(Node_format) + ENCRYPTION_PADDING) {
|
||||
if (len != sizeof(ping_id) + num_nodes * sizeof(Node_format) + ENCRYPTION_PADDING)
|
||||
return -1;
|
||||
}
|
||||
|
||||
data[0] = 3;
|
||||
memcpy(data + 1, self_public_key, CLIENT_ID_SIZE);
|
||||
|
@ -574,27 +544,20 @@ static int sendnodes(IP_Port ip_port, uint8_t * public_key, uint8_t * client_id,
|
|||
/* Packet handling functions
|
||||
One to handle each types of packets we receive
|
||||
return 0 if handled correctly, 1 if packet is bad. */
|
||||
int handle_pingreq(uint8_t * packet, uint32_t length, IP_Port source)
|
||||
int handle_pingreq(uint8_t *packet, uint32_t length, IP_Port source)
|
||||
{
|
||||
uint64_t ping_id;
|
||||
if(length != 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) + ENCRYPTION_PADDING) {
|
||||
if (length != 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) + ENCRYPTION_PADDING)
|
||||
return 1;
|
||||
}
|
||||
/* check if packet is from ourself. */
|
||||
if(memcmp(packet + 1, self_public_key, CLIENT_ID_SIZE) == 0) {
|
||||
if (memcmp(packet + 1, self_public_key, CLIENT_ID_SIZE) == 0)
|
||||
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);
|
||||
if(len != sizeof(ping_id)) {
|
||||
if (len != sizeof(ping_id))
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
pingres(source, packet + 1, ping_id);
|
||||
|
||||
pingreq(source, packet + 1); /* TODO: make this smarter? */
|
||||
|
@ -603,26 +566,21 @@ int handle_pingreq(uint8_t * packet, uint32_t length, IP_Port source)
|
|||
|
||||
}
|
||||
|
||||
int handle_pingres(uint8_t * packet, uint32_t length, IP_Port source)
|
||||
int handle_pingres(uint8_t *packet, uint32_t length, IP_Port source)
|
||||
{
|
||||
uint64_t ping_id;
|
||||
if(length != 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) + ENCRYPTION_PADDING) {
|
||||
if (length != 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) + ENCRYPTION_PADDING)
|
||||
return 1;
|
||||
}
|
||||
if(memcmp(packet + 1, self_public_key, CLIENT_ID_SIZE) == 0) /* check if packet is from ourself. */ {
|
||||
if (memcmp(packet + 1, self_public_key, CLIENT_ID_SIZE) == 0) /* check if packet is from ourself. */
|
||||
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);
|
||||
if(len != sizeof(ping_id)) {
|
||||
if (len != sizeof(ping_id))
|
||||
return 1;
|
||||
}
|
||||
|
||||
if(is_pinging(source, ping_id)) {
|
||||
if (is_pinging(source, ping_id)) {
|
||||
addto_lists(source, packet + 1);
|
||||
return 0;
|
||||
}
|
||||
|
@ -630,16 +588,14 @@ int handle_pingres(uint8_t * packet, uint32_t length, IP_Port source)
|
|||
|
||||
}
|
||||
|
||||
int handle_getnodes(uint8_t * packet, uint32_t length, IP_Port source)
|
||||
int handle_getnodes(uint8_t *packet, uint32_t length, IP_Port source)
|
||||
{
|
||||
uint64_t ping_id;
|
||||
if(length != 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) + CLIENT_ID_SIZE + ENCRYPTION_PADDING) {
|
||||
if (length != 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) + CLIENT_ID_SIZE + ENCRYPTION_PADDING)
|
||||
return 1;
|
||||
}
|
||||
/* check if packet is from ourself. */
|
||||
if(memcmp(packet + 1, self_public_key, CLIENT_ID_SIZE) == 0) {
|
||||
if (memcmp(packet + 1, self_public_key, CLIENT_ID_SIZE) == 0)
|
||||
return 1;
|
||||
}
|
||||
|
||||
uint8_t plain[sizeof(ping_id) + CLIENT_ID_SIZE];
|
||||
|
||||
|
@ -647,10 +603,8 @@ int handle_getnodes(uint8_t * packet, uint32_t length, IP_Port source)
|
|||
packet + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES,
|
||||
sizeof(ping_id) + CLIENT_ID_SIZE + ENCRYPTION_PADDING, plain);
|
||||
|
||||
if(len != sizeof(ping_id) + CLIENT_ID_SIZE) {
|
||||
if (len != sizeof(ping_id) + CLIENT_ID_SIZE)
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
memcpy(&ping_id, plain, sizeof(ping_id));
|
||||
sendnodes(source, packet + 1, plain + sizeof(ping_id), ping_id);
|
||||
|
@ -664,7 +618,7 @@ int handle_getnodes(uint8_t * packet, uint32_t length, IP_Port source)
|
|||
int handle_sendnodes(uint8_t * packet, uint32_t length, IP_Port source)
|
||||
{
|
||||
uint64_t ping_id;
|
||||
if(length > (1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id)
|
||||
if (length > (1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) //TODO: rewrite this monstrosity
|
||||
+ 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 ||
|
||||
|
@ -682,42 +636,36 @@ int handle_sendnodes(uint8_t * packet, uint32_t length, IP_Port source)
|
|||
sizeof(ping_id) + num_nodes * sizeof(Node_format) + ENCRYPTION_PADDING, plain);
|
||||
|
||||
|
||||
if(len != sizeof(ping_id) + num_nodes * sizeof(Node_format)) {
|
||||
if (len != sizeof(ping_id) + num_nodes * sizeof(Node_format))
|
||||
return 1;
|
||||
}
|
||||
|
||||
memcpy(&ping_id, plain, sizeof(ping_id));
|
||||
if(!is_gettingnodes(source, ping_id)) {
|
||||
if(!is_gettingnodes(source, ping_id))
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
Node_format nodes_list[MAX_SENT_NODES];
|
||||
memcpy(nodes_list, plain + sizeof(ping_id), num_nodes * sizeof(Node_format));
|
||||
|
||||
addto_lists(source, packet + 1);
|
||||
|
||||
uint32_t i;
|
||||
for(i = 0; i < num_nodes; ++i) {
|
||||
for (i = 0; i < num_nodes; ++i)
|
||||
pingreq(nodes_list[i].ip_port, nodes_list[i].client_id);
|
||||
returnedip_ports(nodes_list[i].ip_port, nodes_list[i].client_id, packet + 1);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* END of packet handling functions */
|
||||
|
||||
int DHT_addfriend(uint8_t * client_id)
|
||||
int DHT_addfriend(uint8_t *client_id)
|
||||
{
|
||||
Friend * temp;
|
||||
if(num_friends == 0) {
|
||||
if (num_friends == 0)
|
||||
temp = malloc(sizeof(Friend));
|
||||
} else {
|
||||
else
|
||||
temp = realloc(friends_list, sizeof(Friend) * (num_friends + 1));
|
||||
}
|
||||
if(temp == NULL) {
|
||||
if (temp == NULL)
|
||||
return 1;
|
||||
}
|
||||
|
||||
friends_list = temp;
|
||||
memset(&friends_list[num_friends], 0, sizeof(Friend));
|
||||
|
@ -727,21 +675,19 @@ int DHT_addfriend(uint8_t * client_id)
|
|||
return 0;
|
||||
}
|
||||
|
||||
int DHT_delfriend(uint8_t * client_id)
|
||||
int DHT_delfriend(uint8_t *client_id)
|
||||
{
|
||||
uint32_t i;
|
||||
Friend * temp;
|
||||
for(i = 0; i < num_friends; ++i) {
|
||||
for (i = 0; i < num_friends; ++i) {
|
||||
/* Equal */
|
||||
if(memcmp(friends_list[i].client_id, client_id, CLIENT_ID_SIZE) == 0){
|
||||
if (memcmp(friends_list[i].client_id, client_id, CLIENT_ID_SIZE) == 0){
|
||||
--num_friends;
|
||||
if(num_friends != i) {
|
||||
if (num_friends != i)
|
||||
memcpy(friends_list[i].client_id, friends_list[num_friends].client_id, CLIENT_ID_SIZE);
|
||||
}
|
||||
temp = realloc(friends_list, sizeof(Friend) * (num_friends));
|
||||
if(temp != NULL) {
|
||||
if (temp != NULL)
|
||||
friends_list = temp;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
@ -749,12 +695,12 @@ int DHT_delfriend(uint8_t * client_id)
|
|||
}
|
||||
|
||||
/* TODO: Optimize this. */
|
||||
IP_Port DHT_getfriendip(uint8_t * client_id)
|
||||
IP_Port DHT_getfriendip(uint8_t *client_id)
|
||||
{
|
||||
uint32_t i, j;
|
||||
IP_Port empty = {{{0}}, 0};
|
||||
uint32_t temp_time = unix_time();
|
||||
for(i = 0; i < num_friends; ++i) {
|
||||
for (i = 0; i < num_friends; ++i) {
|
||||
/* Equal */
|
||||
if(memcmp(friends_list[i].client_id, client_id, CLIENT_ID_SIZE) == 0) {
|
||||
for(j = 0; j < MAX_FRIEND_CLIENTS; ++j) {
|
||||
|
@ -789,21 +735,21 @@ void doDHTFriends()
|
|||
uint32_t rand_node;
|
||||
uint32_t index[MAX_FRIEND_CLIENTS];
|
||||
|
||||
for(i = 0; i < num_friends; ++i) {
|
||||
for (i = 0; i < num_friends; ++i) {
|
||||
uint32_t num_nodes = 0;
|
||||
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) {
|
||||
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) {
|
||||
pingreq(friends_list[i].client_list[j].ip_port, friends_list[i].client_list[j].client_id);
|
||||
friends_list[i].client_list[j].last_pinged = temp_time;
|
||||
}
|
||||
if(friends_list[i].client_list[j].timestamp + BAD_NODE_TIMEOUT > temp_time) /* if node is good. */ {
|
||||
if (friends_list[i].client_list[j].timestamp + BAD_NODE_TIMEOUT > temp_time) /* if node is good. */ {
|
||||
index[num_nodes] = j;
|
||||
++num_nodes;
|
||||
}
|
||||
}
|
||||
}
|
||||
if(friends_list[i].lastgetnode + GET_NODE_INTERVAL <= temp_time && num_nodes != 0) {
|
||||
if (friends_list[i].lastgetnode + GET_NODE_INTERVAL <= temp_time && num_nodes != 0) {
|
||||
rand_node = rand() % num_nodes;
|
||||
getnodes(friends_list[i].client_list[index[rand_node]].ip_port,
|
||||
friends_list[i].client_list[index[rand_node]].client_id,
|
||||
|
@ -825,23 +771,23 @@ void doClose() /* tested */
|
|||
uint32_t rand_node;
|
||||
uint32_t index[LCLIENT_LIST];
|
||||
|
||||
for(i = 0; i < LCLIENT_LIST; ++i) {
|
||||
for (i = 0; i < LCLIENT_LIST; ++i) {
|
||||
/* if node is not dead. */
|
||||
if(close_clientlist[i].timestamp + Kill_NODE_TIMEOUT > temp_time) {
|
||||
if((close_clientlist[i].last_pinged + PING_INTERVAL) <= temp_time)
|
||||
if (close_clientlist[i].timestamp + Kill_NODE_TIMEOUT > temp_time) {
|
||||
if ((close_clientlist[i].last_pinged + PING_INTERVAL) <= temp_time)
|
||||
{
|
||||
pingreq(close_clientlist[i].ip_port, close_clientlist[i].client_id);
|
||||
close_clientlist[i].last_pinged = temp_time;
|
||||
}
|
||||
/* if node is good. */
|
||||
if(close_clientlist[i].timestamp + BAD_NODE_TIMEOUT > temp_time) {
|
||||
if (close_clientlist[i].timestamp + BAD_NODE_TIMEOUT > temp_time) {
|
||||
index[num_nodes] = i;
|
||||
++num_nodes;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(close_lastgetnodes + GET_NODE_INTERVAL <= temp_time && num_nodes != 0) {
|
||||
if (close_lastgetnodes + GET_NODE_INTERVAL <= temp_time && num_nodes != 0) {
|
||||
rand_node = rand() % num_nodes;
|
||||
getnodes(close_clientlist[index[rand_node]].ip_port,
|
||||
close_clientlist[index[rand_node]].client_id,
|
||||
|
@ -850,18 +796,18 @@ void doClose() /* tested */
|
|||
}
|
||||
}
|
||||
|
||||
void DHT_bootstrap(IP_Port ip_port, uint8_t * public_key)
|
||||
void DHT_bootstrap(IP_Port ip_port, uint8_t *public_key)
|
||||
{
|
||||
getnodes(ip_port, public_key, self_public_key);
|
||||
}
|
||||
|
||||
/* 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)
|
||||
int route_packet(uint8_t *client_id, uint8_t *packet, uint32_t length)
|
||||
{
|
||||
uint32_t i;
|
||||
for(i = 0; i < LCLIENT_LIST; ++i) {
|
||||
if(memcmp(client_id, close_clientlist[i].client_id, CLIENT_ID_SIZE) == 0) {
|
||||
for (i = 0; i < LCLIENT_LIST; ++i) {
|
||||
if (memcmp(client_id, close_clientlist[i].client_id, CLIENT_ID_SIZE) == 0) {
|
||||
return sendpacket(close_clientlist[i].ip_port, packet, length);
|
||||
}
|
||||
}
|
||||
|
@ -873,22 +819,20 @@ int route_packet(uint8_t * client_id, uint8_t * packet, uint32_t length)
|
|||
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)
|
||||
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();
|
||||
if(friend_num >= num_friends) {
|
||||
if (friend_num >= num_friends)
|
||||
return -1;
|
||||
}
|
||||
for(i = 0; i < MAX_FRIEND_CLIENTS; ++i)
|
||||
{
|
||||
for (i = 0; i < MAX_FRIEND_CLIENTS; ++i) {
|
||||
/*If ip is not zero and node is good */
|
||||
if(friends_list[friend_num].client_list[i].ret_ip_port.ip.i != 0 &&
|
||||
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 ) {
|
||||
if (friends_list[friend_num].client_list[i].ret_ip_port.ip.i != 0 &&
|
||||
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)
|
||||
return 0;
|
||||
}
|
||||
ip_portlist[num_ips] = friends_list[friend_num].client_list[i].ret_ip_port;
|
||||
++num_ips;
|
||||
}
|
||||
|
@ -898,21 +842,20 @@ static int friend_iplist(IP_Port * ip_portlist, uint16_t friend_num)
|
|||
|
||||
/* 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)
|
||||
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();
|
||||
for(i = 0; i < num_friends; ++i) {
|
||||
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 (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 */
|
||||
if(friends_list[i].client_list[j].ret_ip_port.ip.i != 0 &&
|
||||
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) {
|
||||
if(sendpacket(friends_list[i].client_list[j].ip_port, packet, length) == length)
|
||||
++sent;
|
||||
}
|
||||
}
|
||||
}
|
||||
return sent;
|
||||
|
|
20
core/DHT.h
20
core/DHT.h
|
@ -40,13 +40,13 @@ extern "C" {
|
|||
client_id must be CLIENT_ID_SIZE bytes long.
|
||||
returns 0 if success
|
||||
returns 1 if failure (friends list is full) */
|
||||
int DHT_addfriend(uint8_t * client_id);
|
||||
int DHT_addfriend(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(uint8_t * client_id);
|
||||
int DHT_delfriend(uint8_t *client_id);
|
||||
|
||||
/* Get ip of friend
|
||||
client_id must be CLIENT_ID_SIZE bytes long.
|
||||
|
@ -55,7 +55,7 @@ int DHT_delfriend(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(uint8_t * client_id);
|
||||
IP_Port DHT_getfriendip(uint8_t *client_id);
|
||||
|
||||
/* Run this function at least a couple times per second (It's the main loop) */
|
||||
void doDHT();
|
||||
|
@ -63,21 +63,21 @@ void doDHT();
|
|||
/* if we receive a DHT packet we call this function so it can be handled.
|
||||
return 0 if packet is handled correctly.
|
||||
return 1 if it didn't handle the packet or if the packet was shit. */
|
||||
int DHT_handlepacket(uint8_t * packet, uint32_t length, IP_Port source);
|
||||
int DHT_handlepacket(uint8_t *packet, uint32_t length, IP_Port source);
|
||||
|
||||
/* 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(IP_Port ip_port, uint8_t * public_key);
|
||||
void DHT_bootstrap(IP_Port ip_port, uint8_t *public_key);
|
||||
|
||||
/* ROUTING FUNCTIONS */
|
||||
|
||||
/* 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);
|
||||
int route_packet(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(uint8_t * friend_id, uint8_t * packet, uint32_t length);
|
||||
int route_tofriend(uint8_t *friend_id, uint8_t *packet, uint32_t length);
|
||||
|
||||
/* NAT PUNCHING FUNCTIONS */
|
||||
|
||||
|
@ -85,7 +85,7 @@ int route_tofriend(uint8_t * friend_id, uint8_t * packet, uint32_t length);
|
|||
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(IP_Port * ip_portlist, uint8_t * friend_id);
|
||||
int friend_ips(IP_Port *ip_portlist, uint8_t *friend_id);
|
||||
|
||||
/* SAVE/LOAD functions */
|
||||
|
||||
|
@ -93,12 +93,12 @@ int friend_ips(IP_Port * ip_portlist, uint8_t * friend_id);
|
|||
uint32_t DHT_size();
|
||||
|
||||
/* save the DHT in data where data is an array of size DHT_size() */
|
||||
void DHT_save(uint8_t * data);
|
||||
void DHT_save(uint8_t *data);
|
||||
|
||||
/* load the DHT from data of size size;
|
||||
return -1 if failure
|
||||
return 0 if success */
|
||||
int DHT_load(uint8_t * data, uint32_t size);
|
||||
int DHT_load(uint8_t *data, uint32_t size);
|
||||
|
||||
/* returns 0 if we are not connected to the DHT
|
||||
returns 1 if we are */
|
||||
|
|
|
@ -37,24 +37,24 @@ IP broadcast_ip()
|
|||
return -1 if it is not */
|
||||
int LAN_ip(IP ip)
|
||||
{
|
||||
if(ip.c[0] == 127)/* Loopback */
|
||||
if (ip.c[0] == 127)/* Loopback */
|
||||
return 0;
|
||||
if(ip.c[0] == 10)/* 10.0.0.0 to 10.255.255.255 range */
|
||||
if (ip.c[0] == 10)/* 10.0.0.0 to 10.255.255.255 range */
|
||||
return 0;
|
||||
if(ip.c[0] == 172 && ip.c[1] >= 16 && ip.c[1] <= 31)/* 172.16.0.0 to 172.31.255.255 range */
|
||||
if (ip.c[0] == 172 && ip.c[1] >= 16 && ip.c[1] <= 31)/* 172.16.0.0 to 172.31.255.255 range */
|
||||
return 0;
|
||||
if(ip.c[0] == 192 && ip.c[1] == 168) /* 192.168.0.0 to 192.168.255.255 range */
|
||||
if (ip.c[0] == 192 && ip.c[1] == 168) /* 192.168.0.0 to 192.168.255.255 range */
|
||||
return 0;
|
||||
if(ip.c[0] == 169 && ip.c[1] == 254 && ip.c[2] != 0 && ip.c[2] != 255)/* 169.254.1.0 to 169.254.254.255 range */
|
||||
if (ip.c[0] == 169 && ip.c[1] == 254 && ip.c[2] != 0 && ip.c[2] != 255)/* 169.254.1.0 to 169.254.254.255 range */
|
||||
return 0;
|
||||
return -1;
|
||||
}
|
||||
|
||||
int handle_LANdiscovery(uint8_t * packet, uint32_t length, IP_Port source)
|
||||
int handle_LANdiscovery(uint8_t *packet, uint32_t length, IP_Port source)
|
||||
{
|
||||
if(LAN_ip(source.ip) == -1)
|
||||
if (LAN_ip(source.ip) == -1)
|
||||
return 1;
|
||||
if(length != crypto_box_PUBLICKEYBYTES + 1)
|
||||
if (length != crypto_box_PUBLICKEYBYTES + 1)
|
||||
return 1;
|
||||
DHT_bootstrap(source, packet + 1);
|
||||
return 0;
|
||||
|
@ -71,9 +71,9 @@ int send_LANdiscovery(uint16_t port)
|
|||
}
|
||||
|
||||
|
||||
int LANdiscovery_handlepacket(uint8_t * packet, uint32_t length, IP_Port source)
|
||||
int LANdiscovery_handlepacket(uint8_t *packet, uint32_t length, IP_Port source)
|
||||
{
|
||||
if(packet[0] == 32)
|
||||
if (packet[0] == 32)
|
||||
return handle_LANdiscovery(packet, length, source);
|
||||
return 1;
|
||||
}
|
||||
|
|
|
@ -39,7 +39,7 @@ int send_LANdiscovery(uint16_t port);
|
|||
/* if we receive a packet we call this function so it can be handled.
|
||||
return 0 if packet is handled correctly.
|
||||
return 1 if it didn't handle the packet or if the packet was shit. */
|
||||
int LANdiscovery_handlepacket(uint8_t * packet, uint32_t length, IP_Port source);
|
||||
int LANdiscovery_handlepacket(uint8_t *packet, uint32_t length, IP_Port source);
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -44,14 +44,12 @@ timeout per connection is randomly set between CONNEXION_TIMEOUT and 2*CONNEXION
|
|||
/* initial send rate of data. */
|
||||
#define DATA_SYNC_RATE 30
|
||||
|
||||
typedef struct
|
||||
{
|
||||
typedef struct {
|
||||
uint8_t data[MAX_DATA_SIZE];
|
||||
uint16_t size;
|
||||
}Data;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
typedef struct {
|
||||
IP_Port ip_port;
|
||||
uint8_t status; /* 0 if connection is dead, 1 if attempting handshake,
|
||||
2 if handshake is done (we start sending SYNC packets)
|
||||
|
@ -97,13 +95,13 @@ static Connection connections[MAX_CONNECTIONS];
|
|||
/* 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(IP_Port ip_port)
|
||||
{
|
||||
int getconnection_id(IP_Port ip_port) {
|
||||
uint32_t i;
|
||||
for(i = 0; i < MAX_CONNECTIONS; ++i)
|
||||
if(connections[i].ip_port.ip.i == ip_port.ip.i &&
|
||||
for (i = 0; i < MAX_CONNECTIONS; ++i) {
|
||||
if (connections[i].ip_port.ip.i == ip_port.ip.i &&
|
||||
connections[i].ip_port.port == ip_port.port && connections[i].status > 0)
|
||||
return i;
|
||||
return i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
@ -116,12 +114,12 @@ static uint32_t randtable[6][256];
|
|||
uint32_t handshake_id(IP_Port source)
|
||||
{
|
||||
uint32_t id = 0, i;
|
||||
for(i = 0; i < 6; ++i) {
|
||||
for (i = 0; i < 6; ++i) {
|
||||
if(randtable[i][((uint8_t *)&source)[i]] == 0)
|
||||
randtable[i][((uint8_t *)&source)[i]] = random_int();
|
||||
id ^= randtable[i][((uint8_t *)&source)[i]];
|
||||
}
|
||||
if(id == 0) /* id can't be zero */
|
||||
if (id == 0) /* id can't be zero */
|
||||
id = 1;
|
||||
return id;
|
||||
}
|
||||
|
@ -141,10 +139,10 @@ void change_handshake(IP_Port source)
|
|||
int new_connection(IP_Port ip_port)
|
||||
{
|
||||
int connect = getconnection_id(ip_port);
|
||||
if(connect != -1)
|
||||
if (connect != -1)
|
||||
return connect;
|
||||
uint32_t i;
|
||||
for(i = 0; i < MAX_CONNECTIONS; ++i) {
|
||||
for (i = 0; i < MAX_CONNECTIONS; ++i) {
|
||||
if(connections[i].status == 0) {
|
||||
memset(&connections[i], 0, sizeof(Connection));
|
||||
connections[i].ip_port = ip_port;
|
||||
|
@ -173,11 +171,11 @@ int new_connection(IP_Port ip_port)
|
|||
return -1 if it could not initialize the connection. */
|
||||
int new_inconnection(IP_Port ip_port)
|
||||
{
|
||||
if(getconnection_id(ip_port) != -1)
|
||||
if (getconnection_id(ip_port) != -1)
|
||||
return -1;
|
||||
uint32_t i;
|
||||
for(i = 0; i < MAX_CONNECTIONS; ++i) {
|
||||
if(connections[i].status == 0) {
|
||||
for (i = 0; i < MAX_CONNECTIONS; ++i) {
|
||||
if (connections[i].status == 0) {
|
||||
memset(&connections[i], 0, sizeof(Connection));
|
||||
connections[i].ip_port = ip_port;
|
||||
connections[i].status = 2;
|
||||
|
@ -202,11 +200,12 @@ int new_inconnection(IP_Port ip_port)
|
|||
int incoming_connection()
|
||||
{
|
||||
uint32_t i;
|
||||
for(i = 0; i < MAX_CONNECTIONS; ++i)
|
||||
if(connections[i].inbound == 2) {
|
||||
for (i = 0; i < MAX_CONNECTIONS; ++i) {
|
||||
if (connections[i].inbound == 2) {
|
||||
connections[i].inbound = 1;
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
@ -214,12 +213,13 @@ int incoming_connection()
|
|||
return 0 if killed successfully */
|
||||
int kill_connection(int connection_id)
|
||||
{
|
||||
if(connection_id >= 0 && connection_id < MAX_CONNECTIONS)
|
||||
if(connections[connection_id].status > 0) {
|
||||
connections[connection_id].status = 0;
|
||||
change_handshake(connections[connection_id].ip_port);
|
||||
return 0;
|
||||
if (connection_id >= 0 && connection_id < MAX_CONNECTIONS) {
|
||||
if (connections[connection_id].status > 0) {
|
||||
connections[connection_id].status = 0;
|
||||
change_handshake(connections[connection_id].ip_port);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
@ -228,11 +228,12 @@ int kill_connection(int connection_id)
|
|||
return 0 if it will kill it */
|
||||
int kill_connection_in(int connection_id, uint32_t seconds)
|
||||
{
|
||||
if(connection_id >= 0 && connection_id < MAX_CONNECTIONS)
|
||||
if(connections[connection_id].status > 0) {
|
||||
connections[connection_id].killat = current_time() + 1000000UL*seconds;
|
||||
return 0;
|
||||
if (connection_id >= 0 && connection_id < MAX_CONNECTIONS) {
|
||||
if (connections[connection_id].status > 0) {
|
||||
connections[connection_id].killat = current_time() + 1000000UL*seconds;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
@ -244,7 +245,7 @@ int kill_connection_in(int connection_id, uint32_t seconds)
|
|||
return 4 if timed out and waiting to be killed */
|
||||
int is_connected(int connection_id)
|
||||
{
|
||||
if(connection_id >= 0 && connection_id < MAX_CONNECTIONS)
|
||||
if (connection_id >= 0 && connection_id < MAX_CONNECTIONS)
|
||||
return connections[connection_id].status;
|
||||
return 0;
|
||||
}
|
||||
|
@ -252,7 +253,7 @@ int is_connected(int connection_id)
|
|||
/* returns the ip_port of the corresponding connection. */
|
||||
IP_Port connection_ip(int connection_id)
|
||||
{
|
||||
if(connection_id >= 0 && connection_id < MAX_CONNECTIONS)
|
||||
if (connection_id >= 0 && connection_id < MAX_CONNECTIONS)
|
||||
return connections[connection_id].ip_port;
|
||||
IP_Port zero = {{{0}}, 0};
|
||||
return zero;
|
||||
|
@ -274,7 +275,7 @@ uint32_t recvqueue(int connection_id)
|
|||
return -1 if no packet in queue */
|
||||
char id_packet(int connection_id)
|
||||
{
|
||||
if(recvqueue(connection_id) != 0 && connections[connection_id].status != 0)
|
||||
if (recvqueue(connection_id) != 0 && connections[connection_id].status != 0)
|
||||
return connections[connection_id].recvbuffer[connections[connection_id].successful_read % MAX_QUEUE_NUM].data[0];
|
||||
return -1;
|
||||
}
|
||||
|
@ -283,7 +284,7 @@ char id_packet(int connection_id)
|
|||
return length of received packet if successful */
|
||||
int read_packet(int connection_id, uint8_t * data)
|
||||
{
|
||||
if(recvqueue(connection_id) != 0) {
|
||||
if (recvqueue(connection_id) != 0) {
|
||||
uint16_t index = connections[connection_id].successful_read % MAX_QUEUE_NUM;
|
||||
uint16_t size = connections[connection_id].recvbuffer[index].size;
|
||||
memcpy(data, connections[connection_id].recvbuffer[index].data, size);
|
||||
|
@ -298,11 +299,11 @@ int read_packet(int connection_id, uint8_t * data)
|
|||
return 1 if data was put into the queue */
|
||||
int write_packet(int connection_id, uint8_t * data, uint32_t length)
|
||||
{
|
||||
if(length > MAX_DATA_SIZE)
|
||||
if (length > MAX_DATA_SIZE)
|
||||
return 0;
|
||||
if(length == 0)
|
||||
if (length == 0)
|
||||
return 0;
|
||||
if(sendqueue(connection_id) < BUFFER_PACKET_NUM) {
|
||||
if (sendqueue(connection_id) < BUFFER_PACKET_NUM) {
|
||||
uint32_t index = connections[connection_id].sendbuff_packetnum % MAX_QUEUE_NUM;
|
||||
memcpy(connections[connection_id].sendbuffer[index].data, data, length);
|
||||
connections[connection_id].sendbuffer[index].size = length;
|
||||
|
@ -318,14 +319,15 @@ uint32_t missing_packets(int connection_id, uint32_t * requested)
|
|||
uint32_t number = 0;
|
||||
uint32_t i;
|
||||
uint32_t temp;
|
||||
if(recvqueue(connection_id) >= (BUFFER_PACKET_NUM - 1)) /* don't request packets if the buffer is full. */
|
||||
if (recvqueue(connection_id) >= (BUFFER_PACKET_NUM - 1)) /* don't request packets if the buffer is full. */
|
||||
return 0;
|
||||
for(i = connections[connection_id].recv_packetnum; i != connections[connection_id].osent_packetnum; i++ )
|
||||
for (i = connections[connection_id].recv_packetnum; i != connections[connection_id].osent_packetnum; i++) {
|
||||
if(connections[connection_id].recvbuffer[i % MAX_QUEUE_NUM].size == 0) {
|
||||
temp = htonl(i);
|
||||
memcpy(requested + number, &temp, 4);
|
||||
++number;
|
||||
}
|
||||
}
|
||||
if(number == 0)
|
||||
connections[connection_id].recv_packetnum = connections[connection_id].osent_packetnum;
|
||||
return number;
|
||||
|
@ -393,14 +395,14 @@ int send_DATA(uint32_t connection_id)
|
|||
{
|
||||
int ret;
|
||||
uint32_t buffer[BUFFER_PACKET_NUM];
|
||||
if(connections[connection_id].num_req_paquets > 0) {
|
||||
if (connections[connection_id].num_req_paquets > 0) {
|
||||
ret = send_data_packet(connection_id, connections[connection_id].req_packets[0]);
|
||||
connections[connection_id].num_req_paquets--;
|
||||
memcpy(buffer, connections[connection_id].req_packets + 1, connections[connection_id].num_req_paquets * 4);
|
||||
memcpy(connections[connection_id].req_packets, buffer, connections[connection_id].num_req_paquets * 4);
|
||||
return ret;
|
||||
}
|
||||
if(connections[connection_id].sendbuff_packetnum != connections[connection_id].sent_packetnum) {
|
||||
if (connections[connection_id].sendbuff_packetnum != connections[connection_id].sent_packetnum) {
|
||||
ret = send_data_packet(connection_id, connections[connection_id].sent_packetnum);
|
||||
connections[connection_id].sent_packetnum++;
|
||||
return ret;
|
||||
|
@ -415,7 +417,7 @@ int send_DATA(uint32_t connection_id)
|
|||
return 0 if handled correctly, 1 if packet is bad. */
|
||||
int handle_handshake(uint8_t * packet, uint32_t length, IP_Port source)
|
||||
{
|
||||
if(length != (1 + 4 + 4))
|
||||
if (length != (1 + 4 + 4))
|
||||
return 1;
|
||||
uint32_t temp;
|
||||
uint32_t handshake_id1, handshake_id2;
|
||||
|
@ -425,13 +427,13 @@ int handle_handshake(uint8_t * packet, uint32_t length, IP_Port source)
|
|||
memcpy(&temp, packet + 5, 4);
|
||||
handshake_id2 = ntohl(temp);
|
||||
|
||||
if(handshake_id2 == 0) {
|
||||
if (handshake_id2 == 0) {
|
||||
send_handshake(source, handshake_id(source), handshake_id1);
|
||||
return 0;
|
||||
}
|
||||
if(is_connected(connection) != 1)
|
||||
if (is_connected(connection) != 1)
|
||||
return 1;
|
||||
if(handshake_id2 == connections[connection].handshake_id1) { /* if handshake_id2 is what we sent previously as handshake_id1 */
|
||||
if (handshake_id2 == connections[connection].handshake_id1) { /* if handshake_id2 is what we sent previously as handshake_id1 */
|
||||
connections[connection].status = 2;
|
||||
/* NOTE: is this necessary?
|
||||
connections[connection].handshake_id2 = handshake_id1; */
|
||||
|
@ -448,10 +450,10 @@ int handle_handshake(uint8_t * packet, uint32_t length, IP_Port source)
|
|||
0 if not. */
|
||||
int SYNC_valid(uint32_t length)
|
||||
{
|
||||
if(length < 4 + 4 + 2)
|
||||
if (length < 4 + 4 + 2)
|
||||
return 0;
|
||||
if(length > (BUFFER_PACKET_NUM*4 + 4 + 4 + 2) ||
|
||||
((length - 4 - 4 - 2) % 4) != 0)
|
||||
if (length > (BUFFER_PACKET_NUM*4 + 4 + 4 + 2) ||
|
||||
((length - 4 - 4 - 2) % 4) != 0)
|
||||
return 0;
|
||||
return 1;
|
||||
}
|
||||
|
@ -459,9 +461,9 @@ int SYNC_valid(uint32_t length)
|
|||
/* case 1: */
|
||||
int handle_SYNC1(IP_Port source, uint32_t recv_packetnum, uint32_t sent_packetnum)
|
||||
{
|
||||
if(handshake_id(source) == recv_packetnum) {
|
||||
if (handshake_id(source) == recv_packetnum) {
|
||||
int x = new_inconnection(source);
|
||||
if(x != -1) {
|
||||
if (x != -1) {
|
||||
connections[x].orecv_packetnum = recv_packetnum;
|
||||
connections[x].sent_packetnum = recv_packetnum;
|
||||
connections[x].sendbuff_packetnum = recv_packetnum;
|
||||
|
@ -479,7 +481,7 @@ int handle_SYNC1(IP_Port source, uint32_t recv_packetnum, uint32_t sent_packetnu
|
|||
/* case 2: */
|
||||
int handle_SYNC2(int connection_id, uint8_t counter, uint32_t recv_packetnum, uint32_t sent_packetnum)
|
||||
{
|
||||
if(recv_packetnum == connections[connection_id].orecv_packetnum) {
|
||||
if (recv_packetnum == connections[connection_id].orecv_packetnum) {
|
||||
/* && sent_packetnum == connections[connection_id].osent_packetnum) */
|
||||
connections[connection_id].status = 3;
|
||||
connections[connection_id].recv_counter = counter;
|
||||
|
@ -499,14 +501,14 @@ int handle_SYNC3(int connection_id, uint8_t counter, uint32_t recv_packetnum, ui
|
|||
uint32_t comp_2 = (sent_packetnum - connections[connection_id].successful_read); */
|
||||
uint32_t comp_1 = (recv_packetnum - connections[connection_id].orecv_packetnum);
|
||||
uint32_t comp_2 = (sent_packetnum - connections[connection_id].osent_packetnum);
|
||||
if(comp_1 <= BUFFER_PACKET_NUM && comp_2 <= BUFFER_PACKET_NUM && comp_counter < 10 && comp_counter != 0) { /* packet valid */
|
||||
if (comp_1 <= BUFFER_PACKET_NUM && comp_2 <= BUFFER_PACKET_NUM && comp_counter < 10 && comp_counter != 0) { /* packet valid */
|
||||
connections[connection_id].orecv_packetnum = recv_packetnum;
|
||||
connections[connection_id].osent_packetnum = sent_packetnum;
|
||||
connections[connection_id].successful_sent = recv_packetnum;
|
||||
connections[connection_id].last_recvSYNC = current_time();
|
||||
connections[connection_id].recv_counter = counter;
|
||||
++connections[connection_id].send_counter;
|
||||
for(i = 0; i < number; ++i) {
|
||||
for (i = 0; i < number; ++i) {
|
||||
temp = ntohl(req_packets[i]);
|
||||
memcpy(connections[connection_id].req_packets + i, &temp, 4 * number);
|
||||
}
|
||||
|
@ -516,10 +518,10 @@ int handle_SYNC3(int connection_id, uint8_t counter, uint32_t recv_packetnum, ui
|
|||
return 1;
|
||||
}
|
||||
|
||||
int handle_SYNC(uint8_t * packet, uint32_t length, IP_Port source)
|
||||
int handle_SYNC(uint8_t *packet, uint32_t length, IP_Port source)
|
||||
{
|
||||
|
||||
if(!SYNC_valid(length))
|
||||
if (!SYNC_valid(length))
|
||||
return 1;
|
||||
int connection = getconnection_id(source);
|
||||
uint8_t counter;
|
||||
|
@ -533,39 +535,39 @@ int handle_SYNC(uint8_t * packet, uint32_t length, IP_Port source)
|
|||
recv_packetnum = ntohl(temp);
|
||||
memcpy(&temp,packet + 6, 4);
|
||||
sent_packetnum = ntohl(temp);
|
||||
if(number != 0)
|
||||
if (number != 0)
|
||||
memcpy(req_packets, packet + 10, 4 * number);
|
||||
if(connection == -1)
|
||||
if (connection == -1)
|
||||
return handle_SYNC1(source, recv_packetnum, sent_packetnum);
|
||||
if(connections[connection].status == 2)
|
||||
if (connections[connection].status == 2)
|
||||
return handle_SYNC2(connection, counter, recv_packetnum, sent_packetnum);
|
||||
if(connections[connection].status == 3)
|
||||
if (connections[connection].status == 3)
|
||||
return handle_SYNC3(connection, counter, recv_packetnum, sent_packetnum, req_packets, number);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* 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. */
|
||||
int add_recv(int connection_id, uint32_t data_num, uint8_t * data, uint16_t size)
|
||||
int add_recv(int connection_id, uint32_t data_num, uint8_t *data, uint16_t size)
|
||||
{
|
||||
if(size > MAX_DATA_SIZE)
|
||||
if (size > MAX_DATA_SIZE)
|
||||
return 1;
|
||||
|
||||
uint32_t i;
|
||||
uint32_t maxnum = connections[connection_id].successful_read + BUFFER_PACKET_NUM;
|
||||
uint32_t sent_packet = data_num - connections[connection_id].osent_packetnum;
|
||||
for(i = connections[connection_id].recv_packetnum; i != maxnum; ++i) {
|
||||
if(i == data_num) {
|
||||
for (i = connections[connection_id].recv_packetnum; i != maxnum; ++i) {
|
||||
if (i == data_num) {
|
||||
memcpy(connections[connection_id].recvbuffer[i % MAX_QUEUE_NUM].data, data, size);
|
||||
connections[connection_id].recvbuffer[i % MAX_QUEUE_NUM].size = size;
|
||||
connections[connection_id].last_recvdata = current_time();
|
||||
if(sent_packet < BUFFER_PACKET_NUM)
|
||||
if (sent_packet < BUFFER_PACKET_NUM)
|
||||
connections[connection_id].osent_packetnum = data_num;
|
||||
break;
|
||||
}
|
||||
}
|
||||
for(i = connections[connection_id].recv_packetnum; i != maxnum; ++i) {
|
||||
if(connections[connection_id].recvbuffer[i % MAX_QUEUE_NUM].size != 0)
|
||||
for (i = connections[connection_id].recv_packetnum; i != maxnum; ++i) {
|
||||
if (connections[connection_id].recvbuffer[i % MAX_QUEUE_NUM].size != 0)
|
||||
connections[connection_id].recv_packetnum = i;
|
||||
else
|
||||
break;
|
||||
|
@ -574,17 +576,17 @@ int add_recv(int connection_id, uint32_t data_num, uint8_t * data, uint16_t size
|
|||
return 0;
|
||||
}
|
||||
|
||||
int handle_data(uint8_t * packet, uint32_t length, IP_Port source)
|
||||
int handle_data(uint8_t *packet, uint32_t length, IP_Port source)
|
||||
{
|
||||
int connection = getconnection_id(source);
|
||||
|
||||
if(connection == -1)
|
||||
if (connection == -1)
|
||||
return 1;
|
||||
|
||||
if(connections[connection].status != 3) /* Drop the data packet if connection is not connected. */
|
||||
if (connections[connection].status != 3) /* Drop the data packet if connection is not connected. */
|
||||
return 1;
|
||||
|
||||
if(length > 1 + 4 + MAX_DATA_SIZE || length < 1 + 4 + 1)
|
||||
if (length > 1 + 4 + MAX_DATA_SIZE || length < 1 + 4 + 1)
|
||||
return 1;
|
||||
uint32_t temp;
|
||||
uint32_t number;
|
||||
|
@ -597,9 +599,9 @@ int handle_data(uint8_t * packet, uint32_t length, IP_Port source)
|
|||
|
||||
/* END of packet handling functions */
|
||||
|
||||
int LosslessUDP_handlepacket(uint8_t * packet, uint32_t length, IP_Port source)
|
||||
int LosslessUDP_handlepacket(uint8_t *packet, uint32_t length, IP_Port source)
|
||||
{
|
||||
switch (packet[0]) {
|
||||
switch (packet[0]) { //TODO: check if no break statement is correct???
|
||||
case 16:
|
||||
return handle_handshake(packet, length, source);
|
||||
|
||||
|
@ -622,19 +624,19 @@ void doNew()
|
|||
{
|
||||
uint32_t i;
|
||||
uint64_t temp_time = current_time();
|
||||
for(i = 0; i < MAX_CONNECTIONS; ++i) {
|
||||
if(connections[i].status == 1)
|
||||
if((connections[i].last_sent + (1000000UL/connections[i].SYNC_rate)) <= temp_time) {
|
||||
for (i = 0; i < MAX_CONNECTIONS; ++i) {
|
||||
if (connections[i].status == 1)
|
||||
if ((connections[i].last_sent + (1000000UL/connections[i].SYNC_rate)) <= temp_time) {
|
||||
send_handshake(connections[i].ip_port, connections[i].handshake_id1, 0);
|
||||
connections[i].last_sent = temp_time;
|
||||
}
|
||||
|
||||
/* kill all timed out connections */
|
||||
if( connections[i].status > 0 && (connections[i].last_recvSYNC + connections[i].timeout * 1000000UL) < temp_time &&
|
||||
if ( connections[i].status > 0 && (connections[i].last_recvSYNC + connections[i].timeout * 1000000UL) < temp_time &&
|
||||
connections[i].status != 4)
|
||||
/* kill_connection(i); */
|
||||
connections[i].status = 4;
|
||||
if(connections[i].status > 0 && connections[i].killat < temp_time)
|
||||
if (connections[i].status > 0 && connections[i].killat < temp_time)
|
||||
kill_connection(i);
|
||||
}
|
||||
}
|
||||
|
@ -643,9 +645,9 @@ void doSYNC()
|
|||
{
|
||||
uint32_t i;
|
||||
uint64_t temp_time = current_time();
|
||||
for(i = 0; i < MAX_CONNECTIONS; ++i) {
|
||||
if(connections[i].status == 2 || connections[i].status == 3)
|
||||
if((connections[i].last_SYNC + (1000000UL/connections[i].SYNC_rate)) <= temp_time) {
|
||||
for (i = 0; i < MAX_CONNECTIONS; ++i) {
|
||||
if (connections[i].status == 2 || connections[i].status == 3)
|
||||
if ((connections[i].last_SYNC + (1000000UL/connections[i].SYNC_rate)) <= temp_time) {
|
||||
send_SYNC(i);
|
||||
connections[i].last_SYNC = temp_time;
|
||||
}
|
||||
|
@ -657,10 +659,10 @@ void doData()
|
|||
uint32_t i;
|
||||
uint64_t j;
|
||||
uint64_t temp_time = current_time();
|
||||
for(i = 0; i < MAX_CONNECTIONS; ++i)
|
||||
if(connections[i].status == 3 && sendqueue(i) != 0)
|
||||
if((connections[i].last_sent + (1000000UL/connections[i].data_rate)) <= temp_time) {
|
||||
for(j = connections[i].last_sent; j < temp_time; j += (1000000UL/connections[i].data_rate))
|
||||
for (i = 0; i < MAX_CONNECTIONS; ++i)
|
||||
if (connections[i].status == 3 && sendqueue(i) != 0)
|
||||
if ((connections[i].last_sent + (1000000UL/connections[i].data_rate)) <= temp_time) {
|
||||
for (j = connections[i].last_sent; j < temp_time; j += (1000000UL/connections[i].data_rate))
|
||||
send_DATA(i);
|
||||
connections[i].last_sent = temp_time;
|
||||
}
|
||||
|
@ -675,15 +677,15 @@ void adjustRates()
|
|||
{
|
||||
uint32_t i;
|
||||
uint64_t temp_time = current_time();
|
||||
for(i = 0; i < MAX_CONNECTIONS; ++i) {
|
||||
if(connections[i].status == 1 || connections[i].status == 2)
|
||||
for (i = 0; i < MAX_CONNECTIONS; ++i) {
|
||||
if (connections[i].status == 1 || connections[i].status == 2)
|
||||
connections[i].SYNC_rate = MAX_SYNC_RATE;
|
||||
if(connections[i].status == 3) {
|
||||
if(sendqueue(i) != 0) {
|
||||
if (connections[i].status == 3) {
|
||||
if (sendqueue(i) != 0) {
|
||||
connections[i].data_rate = (BUFFER_PACKET_NUM - connections[i].num_req_paquets) * MAX_SYNC_RATE;
|
||||
connections[i].SYNC_rate = MAX_SYNC_RATE;
|
||||
}
|
||||
else if(connections[i].last_recvdata + 1000000UL > temp_time)
|
||||
else if (connections[i].last_recvdata + 1000000UL > temp_time)
|
||||
connections[i].SYNC_rate = MAX_SYNC_RATE;
|
||||
else
|
||||
connections[i].SYNC_rate = SYNC_RATE;
|
||||
|
|
|
@ -69,11 +69,11 @@ char id_packet(int connection_id);
|
|||
|
||||
/* return 0 if there is no received data in the buffer.
|
||||
return length of received packet if successful */
|
||||
int read_packet(int connection_id, uint8_t * data);
|
||||
int read_packet(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(int connection_id, uint8_t * data, uint32_t length);
|
||||
int write_packet(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(int connection_id);
|
||||
|
@ -97,7 +97,7 @@ void doLossless_UDP();
|
|||
/* if we receive a Lossless_UDP packet we call this function so it can be handled.
|
||||
return 0 if packet is handled correctly.
|
||||
return 1 if it didn't handle the packet or if the packet was shit. */
|
||||
int LosslessUDP_handlepacket(uint8_t * packet, uint32_t length, IP_Port source);
|
||||
int LosslessUDP_handlepacket(uint8_t *packet, uint32_t length, IP_Port source);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
|
168
core/Messenger.c
168
core/Messenger.c
|
@ -24,8 +24,7 @@
|
|||
#include "Messenger.h"
|
||||
#define MIN(a,b) (((a)<(b))?(a):(b))
|
||||
|
||||
typedef struct
|
||||
{
|
||||
typedef struct {
|
||||
uint8_t client_id[CLIENT_ID_SIZE];
|
||||
int crypt_connection_id;
|
||||
int friend_request_id; /* id of the friend request corresponding to the current friend request to the current friend. */
|
||||
|
@ -37,7 +36,7 @@ typedef struct
|
|||
uint16_t userstatus_length;
|
||||
uint8_t userstatus_sent;
|
||||
uint16_t info_size; /* length of the info */
|
||||
}Friend;
|
||||
} Friend;
|
||||
|
||||
uint8_t self_public_key[crypto_box_PUBLICKEYBYTES];
|
||||
|
||||
|
@ -57,14 +56,15 @@ static uint32_t numfriends;
|
|||
|
||||
/* return the friend id associated to that public key.
|
||||
return -1 if no such friend */
|
||||
int getfriend_id(uint8_t * client_id)
|
||||
int getfriend_id(uint8_t *client_id)
|
||||
{
|
||||
uint32_t i;
|
||||
|
||||
for(i = 0; i < numfriends; ++i)
|
||||
if(friendlist[i].status > 0)
|
||||
if(memcmp(client_id, friendlist[i].client_id, crypto_box_PUBLICKEYBYTES) == 0)
|
||||
for (i = 0; i < numfriends; ++i) {
|
||||
if (friendlist[i].status > 0)
|
||||
if (memcmp(client_id, friendlist[i].client_id, crypto_box_PUBLICKEYBYTES) == 0)
|
||||
return i;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
@ -73,12 +73,12 @@ int getfriend_id(uint8_t * client_id)
|
|||
make sure that client_id is of size CLIENT_ID_SIZE.
|
||||
return 0 if success
|
||||
return -1 if failure. */
|
||||
int getclient_id(int friend_id, uint8_t * client_id)
|
||||
int getclient_id(int friend_id, uint8_t *client_id)
|
||||
{
|
||||
if(friend_id >= numfriends || friend_id < 0)
|
||||
if (friend_id >= numfriends || friend_id < 0)
|
||||
return -1;
|
||||
|
||||
if(friendlist[friend_id].status > 0) {
|
||||
if (friendlist[friend_id].status > 0) {
|
||||
memcpy(client_id, friendlist[friend_id].client_id, CLIENT_ID_SIZE);
|
||||
return 0;
|
||||
}
|
||||
|
@ -92,20 +92,18 @@ int getclient_id(int friend_id, uint8_t * client_id)
|
|||
data is the data and length is the length
|
||||
returns the friend number if success
|
||||
return -1 if failure. */
|
||||
int m_addfriend(uint8_t * client_id, uint8_t * data, uint16_t length)
|
||||
int m_addfriend(uint8_t *client_id, uint8_t *data, uint16_t length)
|
||||
{
|
||||
if(length == 0 || length >=
|
||||
if (length == 0 || length >=
|
||||
(MAX_DATA_SIZE - crypto_box_PUBLICKEYBYTES - crypto_box_NONCEBYTES - crypto_box_BOXZEROBYTES + crypto_box_ZEROBYTES))
|
||||
return -1;
|
||||
if(memcmp(client_id, self_public_key, crypto_box_PUBLICKEYBYTES) == 0)
|
||||
if (memcmp(client_id, self_public_key, crypto_box_PUBLICKEYBYTES) == 0)
|
||||
return -1;
|
||||
if(getfriend_id(client_id) != -1)
|
||||
if (getfriend_id(client_id) != -1)
|
||||
return -1;
|
||||
uint32_t i;
|
||||
for(i = 0; i <= numfriends; ++i)
|
||||
{
|
||||
if(friendlist[i].status == 0)
|
||||
{
|
||||
for (i = 0; i <= numfriends; ++i) {
|
||||
if(friendlist[i].status == 0) {
|
||||
DHT_addfriend(client_id);
|
||||
friendlist[i].status = 1;
|
||||
friendlist[i].crypt_connection_id = -1;
|
||||
|
@ -125,13 +123,11 @@ int m_addfriend(uint8_t * client_id, uint8_t * data, uint16_t length)
|
|||
|
||||
int m_addfriend_norequest(uint8_t * client_id)
|
||||
{
|
||||
if(getfriend_id(client_id) != -1)
|
||||
if (getfriend_id(client_id) != -1)
|
||||
return -1;
|
||||
uint32_t i;
|
||||
for(i = 0; i <= numfriends; ++i)
|
||||
{
|
||||
if(friendlist[i].status == 0)
|
||||
{
|
||||
for (i = 0; i <= numfriends; ++i) {
|
||||
if(friendlist[i].status == 0) {
|
||||
DHT_addfriend(client_id);
|
||||
friendlist[i].status = 2;
|
||||
friendlist[i].crypt_connection_id = -1;
|
||||
|
@ -151,7 +147,7 @@ int m_addfriend_norequest(uint8_t * client_id)
|
|||
return -1 if failure */
|
||||
int m_delfriend(int friendnumber)
|
||||
{
|
||||
if(friendnumber >= numfriends || friendnumber < 0)
|
||||
if (friendnumber >= numfriends || friendnumber < 0)
|
||||
return -1;
|
||||
|
||||
DHT_delfriend(friendlist[friendnumber].client_id);
|
||||
|
@ -159,9 +155,10 @@ int m_delfriend(int friendnumber)
|
|||
free(friendlist[friendnumber].userstatus);
|
||||
memset(&friendlist[friendnumber], 0, sizeof(Friend));
|
||||
uint32_t i;
|
||||
for(i = numfriends; i != 0; --i)
|
||||
if(friendlist[i].status != 0)
|
||||
for (i = numfriends; i != 0; --i) {
|
||||
if (friendlist[i].status != 0)
|
||||
break;
|
||||
}
|
||||
numfriends = i;
|
||||
return 0;
|
||||
}
|
||||
|
@ -173,7 +170,7 @@ int m_delfriend(int friendnumber)
|
|||
return 0 if there is no friend with that number */
|
||||
int m_friendstatus(int friendnumber)
|
||||
{
|
||||
if(friendnumber < 0 || friendnumber >= MAX_NUM_FRIENDS)
|
||||
if (friendnumber < 0 || friendnumber >= MAX_NUM_FRIENDS)
|
||||
return 0;
|
||||
return friendlist[friendnumber].status;
|
||||
}
|
||||
|
@ -181,11 +178,11 @@ int m_friendstatus(int friendnumber)
|
|||
/* send a text chat message to an online friend
|
||||
return 1 if packet was successfully put into the send queue
|
||||
return 0 if it was not */
|
||||
int m_sendmessage(int friendnumber, uint8_t * message, uint32_t length)
|
||||
int m_sendmessage(int friendnumber, uint8_t *message, uint32_t length)
|
||||
{
|
||||
if(friendnumber < 0 || friendnumber >= MAX_NUM_FRIENDS)
|
||||
if (friendnumber < 0 || friendnumber >= MAX_NUM_FRIENDS)
|
||||
return 0;
|
||||
if(length >= MAX_DATA_SIZE || friendlist[friendnumber].status != 4)
|
||||
if (length >= MAX_DATA_SIZE || friendlist[friendnumber].status != 4)
|
||||
/* this does not mean the maximum message length is MAX_DATA_SIZE - 1, it is actually 17 bytes less. */
|
||||
return 0;
|
||||
uint8_t temp[MAX_DATA_SIZE];
|
||||
|
@ -208,7 +205,7 @@ static int m_sendname(int friendnumber, uint8_t * name)
|
|||
return -1 if failure */
|
||||
static int setfriendname(int friendnumber, uint8_t * name)
|
||||
{
|
||||
if(friendnumber >= numfriends || friendnumber < 0)
|
||||
if (friendnumber >= numfriends || friendnumber < 0)
|
||||
return -1;
|
||||
memcpy(friendlist[friendnumber].name, name, MAX_NAME_LENGTH);
|
||||
return 0;
|
||||
|
@ -220,11 +217,11 @@ static int setfriendname(int friendnumber, uint8_t * name)
|
|||
return -1 if failure */
|
||||
int setname(uint8_t * name, uint16_t length)
|
||||
{
|
||||
if(length > MAX_NAME_LENGTH)
|
||||
if (length > MAX_NAME_LENGTH)
|
||||
return -1;
|
||||
memcpy(self_name, name, length);
|
||||
uint32_t i;
|
||||
for(i = 0; i < numfriends; ++i)
|
||||
for (i = 0; i < numfriends; ++i)
|
||||
friendlist[i].name_sent = 0;
|
||||
return 0;
|
||||
}
|
||||
|
@ -236,7 +233,7 @@ int setname(uint8_t * name, uint16_t length)
|
|||
return -1 if failure */
|
||||
int getname(int friendnumber, uint8_t * name)
|
||||
{
|
||||
if(friendnumber >= numfriends || friendnumber < 0)
|
||||
if (friendnumber >= numfriends || friendnumber < 0)
|
||||
return -1;
|
||||
memcpy(name, friendlist[friendnumber].name, MAX_NAME_LENGTH);
|
||||
return 0;
|
||||
|
@ -244,7 +241,7 @@ int getname(int friendnumber, uint8_t * name)
|
|||
|
||||
int m_set_userstatus(uint8_t *status, uint16_t length)
|
||||
{
|
||||
if(length > MAX_USERSTATUS_LENGTH)
|
||||
if (length > MAX_USERSTATUS_LENGTH)
|
||||
return -1;
|
||||
uint8_t *newstatus = calloc(length, 1);
|
||||
memcpy(newstatus, status, length);
|
||||
|
@ -253,7 +250,7 @@ int m_set_userstatus(uint8_t *status, uint16_t length)
|
|||
self_userstatus_len = length;
|
||||
|
||||
uint32_t i;
|
||||
for(i = 0; i < numfriends; ++i)
|
||||
for (i = 0; i < numfriends; ++i)
|
||||
friendlist[i].userstatus_sent = 0;
|
||||
return 0;
|
||||
}
|
||||
|
@ -262,7 +259,7 @@ int m_set_userstatus(uint8_t *status, uint16_t length)
|
|||
guaranteed to be at most MAX_USERSTATUS_LENGTH */
|
||||
int m_get_userstatus_size(int friendnumber)
|
||||
{
|
||||
if(friendnumber >= numfriends || friendnumber < 0)
|
||||
if (friendnumber >= numfriends || friendnumber < 0)
|
||||
return -1;
|
||||
return friendlist[friendnumber].userstatus_length;
|
||||
}
|
||||
|
@ -271,7 +268,7 @@ int m_get_userstatus_size(int friendnumber)
|
|||
bytes, use m_get_userstatus_size to find out how much you need to allocate */
|
||||
int m_copy_userstatus(int friendnumber, uint8_t * buf, uint32_t maxlen)
|
||||
{
|
||||
if(friendnumber >= numfriends || friendnumber < 0)
|
||||
if (friendnumber >= numfriends || friendnumber < 0)
|
||||
return -1;
|
||||
memset(buf, 0, maxlen);
|
||||
memcpy(buf, friendlist[friendnumber].userstatus, MIN(maxlen, MAX_USERSTATUS_LENGTH) - 1);
|
||||
|
@ -290,7 +287,7 @@ static int send_userstatus(int friendnumber, uint8_t * status, uint16_t length)
|
|||
|
||||
static int set_friend_userstatus(int friendnumber, uint8_t * status, uint16_t length)
|
||||
{
|
||||
if(friendnumber >= numfriends || friendnumber < 0)
|
||||
if (friendnumber >= numfriends || friendnumber < 0)
|
||||
return -1;
|
||||
uint8_t *newstatus = calloc(length, 1);
|
||||
memcpy(newstatus, status, length);
|
||||
|
@ -350,35 +347,29 @@ int initMessenger()
|
|||
return 0;
|
||||
}
|
||||
|
||||
//TODO: make this function not suck.
|
||||
static void doFriends()
|
||||
{/* TODO: add incoming connections and some other stuff. */
|
||||
uint32_t i;
|
||||
int len;
|
||||
uint8_t temp[MAX_DATA_SIZE];
|
||||
for(i = 0; i < numfriends; ++i)
|
||||
{
|
||||
if(friendlist[i].status == 1)
|
||||
{
|
||||
for (i = 0; i < numfriends; ++i) {
|
||||
if (friendlist[i].status == 1) {
|
||||
int fr = send_friendrequest(friendlist[i].client_id, friendlist[i].info, friendlist[i].info_size);
|
||||
if(fr == 0) /* TODO: This needs to be fixed so that it sends the friend requests a couple of times in case
|
||||
of packet loss */
|
||||
if (fr == 0) /* TODO: This needs to be fixed so that it sends the friend requests a couple of times in case of packet loss */
|
||||
friendlist[i].status = 2;
|
||||
else
|
||||
if(fr > 0)
|
||||
else if (fr > 0)
|
||||
friendlist[i].status = 2;
|
||||
}
|
||||
if(friendlist[i].status == 2 || friendlist[i].status == 3) /* friend is not online */
|
||||
{
|
||||
if(friendlist[i].status == 2)
|
||||
{
|
||||
if(friendlist[i].friend_request_id + 10 < unix_time()) /*I know this is hackish but it should work.*/
|
||||
{
|
||||
if (friendlist[i].status == 2 || friendlist[i].status == 3) { /* friend is not online */
|
||||
if (friendlist[i].status == 2) {
|
||||
if (friendlist[i].friend_request_id + 10 < unix_time()) { /*I know this is hackish but it should work.*/
|
||||
send_friendrequest(friendlist[i].client_id, friendlist[i].info, friendlist[i].info_size);
|
||||
friendlist[i].friend_request_id = unix_time();
|
||||
}
|
||||
}
|
||||
IP_Port friendip = DHT_getfriendip(friendlist[i].client_id);
|
||||
switch(is_cryptoconnected(friendlist[i].crypt_connection_id)) {
|
||||
switch (is_cryptoconnected(friendlist[i].crypt_connection_id)) {
|
||||
case 0:
|
||||
if (friendip.ip.i > 1)
|
||||
friendlist[i].crypt_connection_id = crypto_connect(friendlist[i].client_id, friendip);
|
||||
|
@ -394,24 +385,23 @@ static void doFriends()
|
|||
break;
|
||||
}
|
||||
}
|
||||
while(friendlist[i].status == 4) /* friend is online */
|
||||
{
|
||||
if(friendlist[i].name_sent == 0)
|
||||
if(m_sendname(i, self_name))
|
||||
while (friendlist[i].status == 4) { /* friend is online */
|
||||
if (friendlist[i].name_sent == 0) {
|
||||
if (m_sendname(i, self_name))
|
||||
friendlist[i].name_sent = 1;
|
||||
if(friendlist[i].userstatus_sent == 0)
|
||||
if(send_userstatus(i, self_userstatus, self_userstatus_len))
|
||||
}
|
||||
if (friendlist[i].userstatus_sent == 0) {
|
||||
if (send_userstatus(i, self_userstatus, self_userstatus_len))
|
||||
friendlist[i].userstatus_sent = 1;
|
||||
}
|
||||
len = read_cryptpacket(friendlist[i].crypt_connection_id, temp);
|
||||
if(len > 0)
|
||||
{
|
||||
switch(temp[0]) {
|
||||
if (len > 0) {
|
||||
switch (temp[0]) {
|
||||
case PACKET_ID_NICKNAME: {
|
||||
if (len != MAX_NAME_LENGTH + 1) break;
|
||||
if (len != MAX_NAME_LENGTH + 1)
|
||||
break;
|
||||
if(friend_namechange_isset)
|
||||
{
|
||||
friend_namechange(i, temp + 1, MAX_NAME_LENGTH); /* TODO: use the actual length */
|
||||
}
|
||||
memcpy(friendlist[i].name, temp + 1, MAX_NAME_LENGTH);
|
||||
friendlist[i].name[MAX_NAME_LENGTH - 1] = 0; /* make sure the NULL terminator is present. */
|
||||
break;
|
||||
|
@ -419,25 +409,21 @@ static void doFriends()
|
|||
case PACKET_ID_USERSTATUS: {
|
||||
uint8_t *status = calloc(MIN(len - 1, MAX_USERSTATUS_LENGTH), 1);
|
||||
memcpy(status, temp + 1, MIN(len - 1, MAX_USERSTATUS_LENGTH));
|
||||
if(friend_statuschange_isset)
|
||||
{
|
||||
friend_statuschange(i, status, MIN(len - 1, MAX_USERSTATUS_LENGTH));
|
||||
}
|
||||
if (friend_statuschange_isset)
|
||||
friend_statuschange(i, status, MIN(len - 1, MAX_USERSTATUS_LENGTH));
|
||||
set_friend_userstatus(i, status, MIN(len - 1, MAX_USERSTATUS_LENGTH));
|
||||
free(status);
|
||||
break;
|
||||
}
|
||||
case PACKET_ID_MESSAGE: {
|
||||
if(friend_message_isset)
|
||||
if (friend_message_isset)
|
||||
(*friend_message)(i, temp + 1, len - 1);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if(is_cryptoconnected(friendlist[i].crypt_connection_id) == 4) /* if the connection timed out, kill it */
|
||||
{
|
||||
else {
|
||||
if (is_cryptoconnected(friendlist[i].crypt_connection_id) == 4) { /* if the connection timed out, kill it */
|
||||
crypto_kill(friendlist[i].crypt_connection_id);
|
||||
friendlist[i].crypt_connection_id = -1;
|
||||
friendlist[i].status = 3;
|
||||
|
@ -454,11 +440,9 @@ static void doInbound()
|
|||
uint8_t public_key[crypto_box_PUBLICKEYBYTES];
|
||||
uint8_t session_key[crypto_box_PUBLICKEYBYTES];
|
||||
int inconnection = crypto_inbound(public_key, secret_nonce, session_key);
|
||||
if(inconnection != -1)
|
||||
{
|
||||
if (inconnection != -1) {
|
||||
int friend_id = getfriend_id(public_key);
|
||||
if(friend_id != -1)
|
||||
{
|
||||
if (friend_id != -1) {
|
||||
crypto_kill(friendlist[friend_id].crypt_connection_id);
|
||||
friendlist[friend_id].crypt_connection_id =
|
||||
accept_crypto_inbound(inconnection, public_key, secret_nonce, session_key);
|
||||
|
@ -476,8 +460,7 @@ static uint32_t last_LANdiscovery;
|
|||
/*Send a LAN discovery packet every LAN_DISCOVERY_INTERVAL seconds*/
|
||||
static void LANdiscovery()
|
||||
{
|
||||
if(last_LANdiscovery + LAN_DISCOVERY_INTERVAL < unix_time())
|
||||
{
|
||||
if (last_LANdiscovery + LAN_DISCOVERY_INTERVAL < unix_time()) {
|
||||
send_LANdiscovery(htons(PORT));
|
||||
last_LANdiscovery = unix_time();
|
||||
}
|
||||
|
@ -490,12 +473,11 @@ void doMessenger()
|
|||
IP_Port ip_port;
|
||||
uint8_t data[MAX_UDP_PACKET_SIZE];
|
||||
uint32_t length;
|
||||
while(receivepacket(&ip_port, data, &length) != -1)
|
||||
{
|
||||
while (receivepacket(&ip_port, data, &length) != -1) {
|
||||
#ifdef DEBUG
|
||||
/* if(rand() % 3 != 1) //simulate packet loss */
|
||||
/* { */
|
||||
if(DHT_handlepacket(data, length, ip_port) && LosslessUDP_handlepacket(data, length, ip_port) &&
|
||||
if (DHT_handlepacket(data, length, ip_port) && LosslessUDP_handlepacket(data, length, ip_port) &&
|
||||
friendreq_handlepacket(data, length, ip_port) && LANdiscovery_handlepacket(data, length, ip_port))
|
||||
/* if packet is discarded */
|
||||
printf("Received unhandled packet with length: %u\n", length);
|
||||
|
@ -527,7 +509,7 @@ uint32_t Messenger_size()
|
|||
}
|
||||
|
||||
/* save the messenger in data of size Messenger_size() */
|
||||
void Messenger_save(uint8_t * data)
|
||||
void Messenger_save(uint8_t *data)
|
||||
{
|
||||
save_keys(data);
|
||||
data += crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES;
|
||||
|
@ -545,9 +527,9 @@ void Messenger_save(uint8_t * data)
|
|||
/* load the messenger from data of size length. */
|
||||
int Messenger_load(uint8_t * data, uint32_t length)
|
||||
{
|
||||
if(length == ~0)
|
||||
if (length == ~0)
|
||||
return -1;
|
||||
if(length < crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES + sizeof(uint32_t) * 2)
|
||||
if (length < crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES + sizeof(uint32_t) * 2)
|
||||
return -1;
|
||||
length -= crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES + sizeof(uint32_t) * 2;
|
||||
load_keys(data);
|
||||
|
@ -556,15 +538,15 @@ int Messenger_load(uint8_t * data, uint32_t length)
|
|||
memcpy(&size, data, sizeof(size));
|
||||
data += sizeof(size);
|
||||
|
||||
if(length < size)
|
||||
if (length < size)
|
||||
return -1;
|
||||
length -= size;
|
||||
if(DHT_load(data, size) == -1)
|
||||
if (DHT_load(data, size) == -1)
|
||||
return -1;
|
||||
data += size;
|
||||
memcpy(&size, data, sizeof(size));
|
||||
data += sizeof(size);
|
||||
if(length != size || length % sizeof(Friend) != 0)
|
||||
if (length != size || length % sizeof(Friend) != 0)
|
||||
return -1;
|
||||
|
||||
Friend * temp = malloc(size);
|
||||
|
@ -573,10 +555,8 @@ int Messenger_load(uint8_t * data, uint32_t length)
|
|||
uint16_t num = size / sizeof(Friend);
|
||||
|
||||
uint32_t i;
|
||||
for(i = 0; i < num; ++i)
|
||||
{
|
||||
if(temp[i].status != 0)
|
||||
{
|
||||
for (i = 0; i < num; ++i) {
|
||||
if(temp[i].status != 0) {
|
||||
int fnum = m_addfriend_norequest(temp[i].client_id);
|
||||
setfriendname(fnum, temp[i].name);
|
||||
/* set_friend_userstatus(fnum, temp[i].userstatus, temp[i].userstatus_length); */
|
||||
|
|
|
@ -51,23 +51,23 @@ extern "C" {
|
|||
data is the data and length is the length
|
||||
returns the friend number if success
|
||||
return -1 if failure. */
|
||||
int m_addfriend(uint8_t * client_id, uint8_t * data, uint16_t length);
|
||||
int m_addfriend(uint8_t *client_id, uint8_t *data, uint16_t length);
|
||||
|
||||
|
||||
/* add a friend without sending a friendrequest.
|
||||
returns the friend number if success
|
||||
return -1 if failure. */
|
||||
int m_addfriend_norequest(uint8_t * client_id);
|
||||
int m_addfriend_norequest(uint8_t *client_id);
|
||||
|
||||
/* return the friend id associated to that client id.
|
||||
return -1 if no such friend */
|
||||
int getfriend_id(uint8_t * client_id);
|
||||
int getfriend_id(uint8_t *client_id);
|
||||
|
||||
/* copies the public key associated to that friend id into client_id buffer.
|
||||
make sure that client_id is of size CLIENT_ID_SIZE.
|
||||
return 0 if success
|
||||
return -1 if failure */
|
||||
int getclient_id(int friend_id, uint8_t * client_id);
|
||||
int getclient_id(int friend_id, uint8_t *client_id);
|
||||
|
||||
/* remove a friend */
|
||||
int m_delfriend(int friendnumber);
|
||||
|
@ -82,20 +82,20 @@ int m_friendstatus(int friendnumber);
|
|||
/* send a text chat message to an online friend
|
||||
returns 1 if packet was successfully put into the send queue
|
||||
return 0 if it was not */
|
||||
int m_sendmessage(int friendnumber, uint8_t * message, uint32_t length);
|
||||
int m_sendmessage(int friendnumber, uint8_t *message, uint32_t length);
|
||||
|
||||
/* Set our nickname
|
||||
name must be a string of maximum MAX_NAME_LENGTH length.
|
||||
return 0 if success
|
||||
return -1 if failure */
|
||||
int setname(uint8_t * name, uint16_t length);
|
||||
int setname(uint8_t *name, uint16_t length);
|
||||
|
||||
/* get name of friendnumber
|
||||
put it in name
|
||||
name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH (128) bytes.
|
||||
return 0 if success
|
||||
return -1 if failure */
|
||||
int getname(int friendnumber, uint8_t * name);
|
||||
int getname(int friendnumber, uint8_t *name);
|
||||
|
||||
/* set our user status
|
||||
you are responsible for freeing status after
|
||||
|
@ -109,7 +109,7 @@ int m_get_userstatus_size(int friendnumber);
|
|||
|
||||
/* copy friendnumber's userstatus into buf, truncating if size is over maxlen
|
||||
get the size you need to allocate from m_get_userstatus_size */
|
||||
int m_copy_userstatus(int friendnumber, uint8_t * buf, uint32_t maxlen);
|
||||
int m_copy_userstatus(int friendnumber, uint8_t *buf, uint32_t maxlen);
|
||||
|
||||
/* set the function that will be executed when a friend request is received.
|
||||
function format is function(uint8_t * public_key, uint8_t * data, uint16_t length) */
|
||||
|
@ -143,10 +143,10 @@ void doMessenger();
|
|||
uint32_t Messenger_size();
|
||||
|
||||
/* save the messenger in data (must be allocated memory of size Messenger_size()) */
|
||||
void Messenger_save(uint8_t * data);
|
||||
void Messenger_save(uint8_t *data);
|
||||
|
||||
/* load the messenger from data of size length */
|
||||
int Messenger_load(uint8_t * data, uint32_t length);
|
||||
int Messenger_load(uint8_t *data, uint32_t length);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
|
|
@ -35,24 +35,23 @@ int send_friendrequest(uint8_t * public_key, uint8_t * data, uint32_t length)
|
|||
uint8_t packet[MAX_DATA_SIZE];
|
||||
int len = create_request(packet, public_key, data, length, 32); /* 32 is friend request packet id */
|
||||
|
||||
if(len == -1)
|
||||
if (len == -1)
|
||||
return -1;
|
||||
|
||||
IP_Port ip_port = DHT_getfriendip(public_key);
|
||||
|
||||
if(ip_port.ip.i == 1)
|
||||
if (ip_port.ip.i == 1)
|
||||
return -1;
|
||||
|
||||
if(ip_port.ip.i != 0)
|
||||
{
|
||||
if(sendpacket(ip_port, packet, len) != -1)
|
||||
if (ip_port.ip.i != 0) {
|
||||
if (sendpacket(ip_port, packet, len) != -1)
|
||||
return 0;
|
||||
return -1;
|
||||
}
|
||||
|
||||
int num = route_tofriend(public_key, packet, len);
|
||||
|
||||
if(num == 0)
|
||||
if (num == 0)
|
||||
return -1;
|
||||
|
||||
return num;
|
||||
|
@ -62,8 +61,7 @@ static void (*handle_friendrequest)(uint8_t *, uint8_t *, uint16_t);
|
|||
static uint8_t handle_friendrequest_isset = 0;
|
||||
|
||||
/* set the function that will be executed when a friend request is received. */
|
||||
void callback_friendrequest(void (*function)(uint8_t *, uint8_t *, uint16_t))
|
||||
{
|
||||
void callback_friendrequest(void (*function)(uint8_t *, uint8_t *, uint16_t)) {
|
||||
handle_friendrequest = function;
|
||||
handle_friendrequest_isset = 1;
|
||||
}
|
||||
|
@ -78,9 +76,8 @@ static uint8_t recieved_requests[MAX_RECIEVED_STORED][crypto_box_PUBLICKEYBYTES]
|
|||
static uint16_t recieved_requests_index;
|
||||
|
||||
/*Add to list of recieved friend requests*/
|
||||
static void addto_recievedlist(uint8_t * client_id)
|
||||
{
|
||||
if(recieved_requests_index >= MAX_RECIEVED_STORED)
|
||||
static void addto_recievedlist(uint8_t * client_id) {
|
||||
if (recieved_requests_index >= MAX_RECIEVED_STORED)
|
||||
recieved_requests_index = 0;
|
||||
|
||||
memcpy(recieved_requests[recieved_requests_index], client_id, crypto_box_PUBLICKEYBYTES);
|
||||
|
@ -89,46 +86,43 @@ static void addto_recievedlist(uint8_t * client_id)
|
|||
|
||||
/* Check if a friend request was already recieved
|
||||
return 0 if not, 1 if we did */
|
||||
static int request_recieved(uint8_t * client_id)
|
||||
{
|
||||
static int request_recieved(uint8_t * client_id) {
|
||||
uint32_t i;
|
||||
|
||||
for(i = 0; i < MAX_RECIEVED_STORED; ++i)
|
||||
if(memcmp(recieved_requests[i], client_id, crypto_box_PUBLICKEYBYTES) == 0)
|
||||
for (i = 0; i < MAX_RECIEVED_STORED; ++i) {
|
||||
if (memcmp(recieved_requests[i], client_id, crypto_box_PUBLICKEYBYTES) == 0)
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int friendreq_handlepacket(uint8_t * packet, uint32_t length, IP_Port source)
|
||||
{
|
||||
|
||||
if(packet[0] == 32)
|
||||
{
|
||||
if(length <= crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1 + ENCRYPTION_PADDING &&
|
||||
length > MAX_DATA_SIZE + ENCRYPTION_PADDING)
|
||||
int friendreq_handlepacket(uint8_t * packet, uint32_t length, IP_Port source) {
|
||||
if (packet[0] == 32) {
|
||||
if (length <= crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1 + ENCRYPTION_PADDING &&
|
||||
length > MAX_DATA_SIZE + ENCRYPTION_PADDING)
|
||||
return 1;
|
||||
if(memcmp(packet + 1, self_public_key, crypto_box_PUBLICKEYBYTES) == 0) /* check if request is for us. */
|
||||
{
|
||||
if(handle_friendrequest_isset == 0)
|
||||
if (memcmp(packet + 1, self_public_key, crypto_box_PUBLICKEYBYTES) == 0) {// check if request is for us.
|
||||
if (handle_friendrequest_isset == 0)
|
||||
return 1;
|
||||
|
||||
uint8_t public_key[crypto_box_PUBLICKEYBYTES];
|
||||
uint8_t data[MAX_DATA_SIZE];
|
||||
int len = handle_request(public_key, data, packet, length);
|
||||
|
||||
if(len == -1)
|
||||
if (len == -1)
|
||||
return 1;
|
||||
if(request_recieved(public_key))
|
||||
if (request_recieved(public_key))
|
||||
return 1;
|
||||
|
||||
addto_recievedlist(public_key);
|
||||
(*handle_friendrequest)(public_key, data, len);
|
||||
}
|
||||
else /* if request is not for us, try routing it. */
|
||||
else {/* if request is not for us, try routing it. */
|
||||
if(route_packet(packet + 1, packet, length) == length)
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
|
|
@ -33,7 +33,7 @@ extern "C" {
|
|||
|
||||
/* Try to send a friendrequest to peer with public_key
|
||||
data is the data in the request and length is the length. */
|
||||
int send_friendrequest(uint8_t * public_key, uint8_t * data, uint32_t length);
|
||||
int send_friendrequest(uint8_t *public_key, uint8_t *data, uint32_t length);
|
||||
|
||||
/* 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) */
|
||||
|
@ -42,7 +42,7 @@ void callback_friendrequest(void (*function)(uint8_t *, uint8_t *, uint16_t));
|
|||
/* if we receive a packet we call this function so it can be handled.
|
||||
return 0 if packet is handled correctly.
|
||||
return 1 if it didn't handle the packet or if the packet was shit. */
|
||||
int friendreq_handlepacket(uint8_t * packet, uint32_t length, IP_Port source);
|
||||
int friendreq_handlepacket(uint8_t *packet, uint32_t length, IP_Port source);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
|
|
@ -30,8 +30,7 @@
|
|||
uint8_t self_public_key[crypto_box_PUBLICKEYBYTES];
|
||||
uint8_t self_secret_key[crypto_box_SECRETKEYBYTES];
|
||||
|
||||
typedef struct
|
||||
{
|
||||
typedef struct {
|
||||
uint8_t public_key[crypto_box_PUBLICKEYBYTES]; /* the real public key of the peer. */
|
||||
uint8_t recv_nonce[crypto_box_NONCEBYTES]; /* nonce of received packets */
|
||||
uint8_t sent_nonce[crypto_box_NONCEBYTES]; /* nonce of sent packets. */
|
||||
|
@ -43,7 +42,7 @@ typedef struct
|
|||
4 if the connection is timed out. */
|
||||
uint16_t number; /* Lossless_UDP connection number corresponding to this connection. */
|
||||
|
||||
}Crypto_Connection;
|
||||
} Crypto_Connection;
|
||||
|
||||
#define MAX_CRYPTO_CONNECTIONS 256
|
||||
|
||||
|
@ -58,10 +57,10 @@ static int incoming_connections[MAX_INCOMING];
|
|||
public key(32 bytes) of the receiver and the secret key of the sender and a 24 byte nonce
|
||||
return -1 if there was a problem.
|
||||
return length of encrypted data if everything was fine. */
|
||||
int encrypt_data(uint8_t * public_key, uint8_t * secret_key, uint8_t * nonce,
|
||||
uint8_t * plain, uint32_t length, uint8_t * encrypted)
|
||||
int encrypt_data(uint8_t *public_key, uint8_t *secret_key, uint8_t *nonce,
|
||||
uint8_t *plain, uint32_t length, uint8_t *encrypted)
|
||||
{
|
||||
if(length - crypto_box_BOXZEROBYTES + crypto_box_ZEROBYTES > MAX_DATA_SIZE || length == 0)
|
||||
if (length - crypto_box_BOXZEROBYTES + crypto_box_ZEROBYTES > MAX_DATA_SIZE || length == 0)
|
||||
return -1;
|
||||
|
||||
uint8_t temp_plain[MAX_DATA_SIZE + crypto_box_ZEROBYTES - crypto_box_BOXZEROBYTES] = {0};
|
||||
|
@ -73,7 +72,7 @@ int encrypt_data(uint8_t * public_key, uint8_t * secret_key, uint8_t * nonce,
|
|||
crypto_box(temp_encrypted, temp_plain, length + crypto_box_ZEROBYTES, nonce, public_key, secret_key);
|
||||
|
||||
/* if encryption is successful the first crypto_box_BOXZEROBYTES of the message will be zero */
|
||||
if(memcmp(temp_encrypted, zeroes, crypto_box_BOXZEROBYTES) != 0)
|
||||
if (memcmp(temp_encrypted, zeroes, crypto_box_BOXZEROBYTES) != 0)
|
||||
return -1;
|
||||
|
||||
/* unpad the encrypted message */
|
||||
|
@ -85,10 +84,10 @@ int encrypt_data(uint8_t * public_key, uint8_t * secret_key, uint8_t * nonce,
|
|||
public key(32 bytes) of the sender, the secret key of the receiver and a 24 byte nonce
|
||||
return -1 if there was a problem(decryption failed)
|
||||
return length of plain data if everything was fine. */
|
||||
int decrypt_data(uint8_t * public_key, uint8_t * secret_key, uint8_t * nonce,
|
||||
uint8_t * encrypted, uint32_t length, uint8_t * plain)
|
||||
int decrypt_data(uint8_t *public_key, uint8_t *secret_key, uint8_t *nonce,
|
||||
uint8_t *encrypted, uint32_t length, uint8_t *plain)
|
||||
{
|
||||
if(length > MAX_DATA_SIZE || length <= crypto_box_BOXZEROBYTES)
|
||||
if (length > MAX_DATA_SIZE || length <= crypto_box_BOXZEROBYTES)
|
||||
return -1;
|
||||
|
||||
uint8_t temp_plain[MAX_DATA_SIZE - crypto_box_ZEROBYTES + crypto_box_BOXZEROBYTES];
|
||||
|
@ -97,12 +96,12 @@ int decrypt_data(uint8_t * public_key, uint8_t * secret_key, uint8_t * nonce,
|
|||
|
||||
memcpy(temp_encrypted + crypto_box_BOXZEROBYTES, encrypted, length); /* pad the message with 16 0 bytes. */
|
||||
|
||||
if(crypto_box_open(temp_plain, temp_encrypted, length + crypto_box_BOXZEROBYTES,
|
||||
if (crypto_box_open(temp_plain, temp_encrypted, length + crypto_box_BOXZEROBYTES,
|
||||
nonce, public_key, secret_key) == -1)
|
||||
return -1;
|
||||
|
||||
/* if decryption is successful the first crypto_box_ZEROBYTES of the message will be zero */
|
||||
if(memcmp(temp_plain, zeroes, crypto_box_ZEROBYTES) != 0)
|
||||
if (memcmp(temp_plain, zeroes, crypto_box_ZEROBYTES) != 0)
|
||||
return -1;
|
||||
|
||||
/* unpad the plain message */
|
||||
|
@ -111,10 +110,10 @@ int decrypt_data(uint8_t * public_key, uint8_t * secret_key, uint8_t * nonce,
|
|||
}
|
||||
|
||||
/* increment the given nonce by 1 */
|
||||
void increment_nonce(uint8_t * nonce)
|
||||
void increment_nonce(uint8_t *nonce)
|
||||
{
|
||||
uint32_t i;
|
||||
for(i = 0; i < crypto_box_NONCEBYTES; ++i) {
|
||||
for (i = 0; i < crypto_box_NONCEBYTES; ++i) {
|
||||
++nonce[i];
|
||||
if(nonce[i] != 0)
|
||||
break;
|
||||
|
@ -122,7 +121,7 @@ void increment_nonce(uint8_t * nonce)
|
|||
}
|
||||
|
||||
/* fill the given nonce with random bytes. */
|
||||
void random_nonce(uint8_t * nonce)
|
||||
void random_nonce(uint8_t *nonce)
|
||||
{
|
||||
uint32_t i, temp;
|
||||
for (i = 0; i < crypto_box_NONCEBYTES / 4; ++i) {
|
||||
|
@ -134,22 +133,22 @@ void random_nonce(uint8_t * nonce)
|
|||
/* return 0 if there is no received data in the buffer
|
||||
return -1 if the packet was discarded.
|
||||
return length of received data if successful */
|
||||
int read_cryptpacket(int crypt_connection_id, uint8_t * data)
|
||||
int read_cryptpacket(int crypt_connection_id, uint8_t *data)
|
||||
{
|
||||
if(crypt_connection_id < 0 || crypt_connection_id >= MAX_CRYPTO_CONNECTIONS)
|
||||
if (crypt_connection_id < 0 || crypt_connection_id >= MAX_CRYPTO_CONNECTIONS)
|
||||
return 0;
|
||||
if(crypto_connections[crypt_connection_id].status != 3)
|
||||
if (crypto_connections[crypt_connection_id].status != 3)
|
||||
return 0;
|
||||
uint8_t temp_data[MAX_DATA_SIZE];
|
||||
int length = read_packet(crypto_connections[crypt_connection_id].number, temp_data);
|
||||
if(length == 0)
|
||||
if (length == 0)
|
||||
return 0;
|
||||
if(temp_data[0] != 3)
|
||||
if (temp_data[0] != 3)
|
||||
return -1;
|
||||
int len = decrypt_data(crypto_connections[crypt_connection_id].peersessionpublic_key,
|
||||
crypto_connections[crypt_connection_id].sessionsecret_key,
|
||||
crypto_connections[crypt_connection_id].recv_nonce, temp_data + 1, length - 1, data);
|
||||
if(len != -1) {
|
||||
if (len != -1) {
|
||||
increment_nonce(crypto_connections[crypt_connection_id].recv_nonce);
|
||||
return len;
|
||||
}
|
||||
|
@ -158,22 +157,22 @@ int read_cryptpacket(int crypt_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_cryptpacket(int crypt_connection_id, uint8_t * data, uint32_t length)
|
||||
int write_cryptpacket(int crypt_connection_id, uint8_t *data, uint32_t length)
|
||||
{
|
||||
if(crypt_connection_id < 0 || crypt_connection_id >= MAX_CRYPTO_CONNECTIONS)
|
||||
if (crypt_connection_id < 0 || crypt_connection_id >= MAX_CRYPTO_CONNECTIONS)
|
||||
return 0;
|
||||
if(length - crypto_box_BOXZEROBYTES + crypto_box_ZEROBYTES > MAX_DATA_SIZE - 1)
|
||||
if (length - crypto_box_BOXZEROBYTES + crypto_box_ZEROBYTES > MAX_DATA_SIZE - 1)
|
||||
return 0;
|
||||
if(crypto_connections[crypt_connection_id].status != 3)
|
||||
if (crypto_connections[crypt_connection_id].status != 3)
|
||||
return 0;
|
||||
uint8_t temp_data[MAX_DATA_SIZE];
|
||||
int len = encrypt_data(crypto_connections[crypt_connection_id].peersessionpublic_key,
|
||||
crypto_connections[crypt_connection_id].sessionsecret_key,
|
||||
crypto_connections[crypt_connection_id].sent_nonce, data, length, temp_data + 1);
|
||||
if(len == -1)
|
||||
if (len == -1)
|
||||
return 0;
|
||||
temp_data[0] = 3;
|
||||
if(write_packet(crypto_connections[crypt_connection_id].number, temp_data, len + 1) == 0)
|
||||
if (write_packet(crypto_connections[crypt_connection_id].number, temp_data, len + 1) == 0)
|
||||
return 0;
|
||||
increment_nonce(crypto_connections[crypt_connection_id].sent_nonce);
|
||||
return 1;
|
||||
|
@ -185,15 +184,15 @@ int write_cryptpacket(int crypt_connection_id, uint8_t * data, uint32_t length)
|
|||
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 * packet, uint8_t * public_key, uint8_t * data, uint32_t length, uint8_t request_id)
|
||||
int create_request(uint8_t *packet, uint8_t *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 + ENCRYPTION_PADDING)
|
||||
if (MAX_DATA_SIZE < length + 1 + crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + ENCRYPTION_PADDING)
|
||||
return -1;
|
||||
uint8_t nonce[crypto_box_NONCEBYTES];
|
||||
random_nonce(nonce);
|
||||
int len = encrypt_data(public_key, self_secret_key, nonce, data, length,
|
||||
1 + crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + packet);
|
||||
if(len == -1)
|
||||
if (len == -1)
|
||||
return -1;
|
||||
packet[0] = request_id;
|
||||
memcpy(packet + 1, public_key, crypto_box_PUBLICKEYBYTES);
|
||||
|
@ -207,10 +206,10 @@ int create_request(uint8_t * packet, uint8_t * public_key, uint8_t * data, uint3
|
|||
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. */
|
||||
int handle_request(uint8_t * public_key, uint8_t * data, uint8_t * packet, uint16_t length)
|
||||
int handle_request(uint8_t *public_key, uint8_t *data, uint8_t *packet, uint16_t length)
|
||||
{
|
||||
|
||||
if(length > crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1 + ENCRYPTION_PADDING &&
|
||||
if (length > crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1 + ENCRYPTION_PADDING &&
|
||||
length <= MAX_DATA_SIZE + ENCRYPTION_PADDING &&
|
||||
memcmp(packet + 1, self_public_key, crypto_box_PUBLICKEYBYTES) == 0)
|
||||
{
|
||||
|
@ -230,7 +229,7 @@ int handle_request(uint8_t * public_key, uint8_t * data, uint8_t * packet, uint1
|
|||
/* 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 */
|
||||
int send_cryptohandshake(int connection_id, uint8_t * public_key, uint8_t * secret_nonce, uint8_t * session_key)
|
||||
int send_cryptohandshake(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];
|
||||
|
@ -242,7 +241,7 @@ int send_cryptohandshake(int connection_id, uint8_t * public_key, uint8_t * secr
|
|||
|
||||
int len = encrypt_data(public_key, self_secret_key, nonce, temp, crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES,
|
||||
1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES + temp_data);
|
||||
if(len == -1)
|
||||
if (len == -1)
|
||||
return 0;
|
||||
temp_data[0] = 2;
|
||||
memcpy(temp_data + 1, self_public_key, crypto_box_PUBLICKEYBYTES);
|
||||
|
@ -253,16 +252,16 @@ int send_cryptohandshake(int connection_id, uint8_t * public_key, uint8_t * secr
|
|||
/* Extract secret nonce, session public key and public_key from a packet(data) with length length
|
||||
return 1 if successful
|
||||
return 0 if failure */
|
||||
int handle_cryptohandshake(uint8_t * public_key, uint8_t * secret_nonce,
|
||||
uint8_t * session_key, uint8_t * data, uint16_t length)
|
||||
int handle_cryptohandshake(uint8_t *public_key, uint8_t *secret_nonce,
|
||||
uint8_t *session_key, uint8_t *data, uint16_t length)
|
||||
{
|
||||
int pad = (- crypto_box_BOXZEROBYTES + crypto_box_ZEROBYTES);
|
||||
if(length != 1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES
|
||||
if (length != 1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES
|
||||
+ crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES + pad)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
if(data[0] != 2)
|
||||
if (data[0] != 2)
|
||||
return 0;
|
||||
uint8_t temp[crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES];
|
||||
|
||||
|
@ -272,7 +271,7 @@ int handle_cryptohandshake(uint8_t * public_key, uint8_t * secret_nonce,
|
|||
data + 1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES,
|
||||
crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES + pad, temp);
|
||||
|
||||
if(len != crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES)
|
||||
if (len != crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES)
|
||||
return 0;
|
||||
|
||||
memcpy(secret_nonce, temp, crypto_box_NONCEBYTES);
|
||||
|
@ -283,33 +282,33 @@ int handle_cryptohandshake(uint8_t * public_key, uint8_t * secret_nonce,
|
|||
/* get crypto connection id from public key of peer
|
||||
return -1 if there are no connections like we are looking for
|
||||
return id if it found it */
|
||||
int getcryptconnection_id(uint8_t * public_key)
|
||||
int getcryptconnection_id(uint8_t *public_key)
|
||||
{
|
||||
uint32_t i;
|
||||
for(i = 0; i < MAX_CRYPTO_CONNECTIONS; ++i)
|
||||
if(crypto_connections[i].status > 0)
|
||||
if(memcmp(public_key, crypto_connections[i].public_key, crypto_box_PUBLICKEYBYTES) == 0)
|
||||
for (i = 0; i < MAX_CRYPTO_CONNECTIONS; ++i) {
|
||||
if (crypto_connections[i].status > 0)
|
||||
if (memcmp(public_key, crypto_connections[i].public_key, crypto_box_PUBLICKEYBYTES) == 0)
|
||||
return i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Start a secure connection with other peer who has public_key and ip_port
|
||||
returns -1 if failure
|
||||
returns crypt_connection_id of the initialized connection if everything went well. */
|
||||
int crypto_connect(uint8_t * public_key, IP_Port ip_port)
|
||||
int crypto_connect(uint8_t *public_key, IP_Port ip_port)
|
||||
{
|
||||
uint32_t i;
|
||||
int id = getcryptconnection_id(public_key);
|
||||
if(id != -1) {
|
||||
if (id != -1) {
|
||||
IP_Port c_ip = connection_ip(crypto_connections[id].number);
|
||||
if(c_ip.ip.i == ip_port.ip.i && c_ip.port == ip_port.port)
|
||||
return -1;
|
||||
}
|
||||
for(i = 0; i < MAX_CRYPTO_CONNECTIONS; ++i)
|
||||
{
|
||||
if(crypto_connections[i].status == 0) {
|
||||
for (i = 0; i < MAX_CRYPTO_CONNECTIONS; ++i) {
|
||||
if (crypto_connections[i].status == 0) {
|
||||
int id = new_connection(ip_port);
|
||||
if(id == -1)
|
||||
if (id == -1)
|
||||
return -1;
|
||||
crypto_connections[i].number = id;
|
||||
crypto_connections[i].status = 1;
|
||||
|
@ -317,8 +316,8 @@ int crypto_connect(uint8_t * public_key, IP_Port ip_port)
|
|||
memcpy(crypto_connections[i].public_key, public_key, crypto_box_PUBLICKEYBYTES);
|
||||
crypto_box_keypair(crypto_connections[i].sessionpublic_key, crypto_connections[i].sessionsecret_key);
|
||||
|
||||
if(send_cryptohandshake(id, public_key, crypto_connections[i].recv_nonce,
|
||||
crypto_connections[i].sessionpublic_key) == 1)
|
||||
if (send_cryptohandshake(id, public_key, crypto_connections[i].recv_nonce,
|
||||
crypto_connections[i].sessionpublic_key) == 1)
|
||||
{
|
||||
increment_nonce(crypto_connections[i].recv_nonce);
|
||||
return i;
|
||||
|
@ -336,21 +335,20 @@ int crypto_connect(uint8_t * public_key, IP_Port ip_port)
|
|||
and the session public key for the connection in session_key
|
||||
to accept it see: accept_crypto_inbound(...)
|
||||
to refuse it just call kill_connection(...) on the connection id */
|
||||
int crypto_inbound(uint8_t * public_key, uint8_t * secret_nonce, uint8_t * session_key)
|
||||
int crypto_inbound(uint8_t *public_key, uint8_t *secret_nonce, uint8_t *session_key)
|
||||
{
|
||||
uint32_t i;
|
||||
for(i = 0; i < MAX_INCOMING; ++i)
|
||||
{
|
||||
if(incoming_connections[i] != -1) {
|
||||
if(is_connected(incoming_connections[i]) == 4 || is_connected(incoming_connections[i]) == 0) {
|
||||
for (i = 0; i < MAX_INCOMING; ++i) {
|
||||
if (incoming_connections[i] != -1) {
|
||||
if (is_connected(incoming_connections[i]) == 4 || is_connected(incoming_connections[i]) == 0) {
|
||||
kill_connection(incoming_connections[i]);
|
||||
incoming_connections[i] = -1;
|
||||
continue;
|
||||
}
|
||||
if(id_packet(incoming_connections[i]) == 2) {
|
||||
if (id_packet(incoming_connections[i]) == 2) {
|
||||
uint8_t temp_data[MAX_DATA_SIZE];
|
||||
uint16_t len = read_packet(incoming_connections[i], temp_data);
|
||||
if(handle_cryptohandshake(public_key, secret_nonce, session_key, temp_data, len)) {
|
||||
if (handle_cryptohandshake(public_key, secret_nonce, session_key, temp_data, len)) {
|
||||
int connection_id = incoming_connections[i];
|
||||
incoming_connections[i] = -1; /* remove this connection from the incoming connection list. */
|
||||
return connection_id;
|
||||
|
@ -366,9 +364,9 @@ int crypto_inbound(uint8_t * public_key, uint8_t * secret_nonce, uint8_t * sessi
|
|||
return 1 if there was a problem. */
|
||||
int crypto_kill(int crypt_connection_id)
|
||||
{
|
||||
if(crypt_connection_id < 0 || crypt_connection_id >= MAX_CRYPTO_CONNECTIONS)
|
||||
if (crypt_connection_id < 0 || crypt_connection_id >= MAX_CRYPTO_CONNECTIONS)
|
||||
return 1;
|
||||
if(crypto_connections[crypt_connection_id].status != 0) {
|
||||
if (crypto_connections[crypt_connection_id].status != 0) {
|
||||
crypto_connections[crypt_connection_id].status = 0;
|
||||
kill_connection(crypto_connections[crypt_connection_id].number);
|
||||
crypto_connections[crypt_connection_id].number = ~0;
|
||||
|
@ -380,18 +378,17 @@ int crypto_kill(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(int connection_id, uint8_t * public_key, uint8_t * secret_nonce, uint8_t * session_key)
|
||||
int accept_crypto_inbound(int connection_id, uint8_t *public_key, uint8_t *secret_nonce, uint8_t *session_key)
|
||||
{
|
||||
uint32_t i;
|
||||
if(connection_id == -1)
|
||||
if (connection_id == -1)
|
||||
return -1;
|
||||
/*
|
||||
if(getcryptconnection_id(public_key) != -1)
|
||||
{
|
||||
return -1;
|
||||
}*/
|
||||
for(i = 0; i < MAX_CRYPTO_CONNECTIONS; ++i)
|
||||
{
|
||||
for (i = 0; i < MAX_CRYPTO_CONNECTIONS; ++i) {
|
||||
if(crypto_connections[i].status == 0) {
|
||||
crypto_connections[i].number = connection_id;
|
||||
crypto_connections[i].status = 2;
|
||||
|
@ -403,7 +400,7 @@ int accept_crypto_inbound(int connection_id, uint8_t * public_key, uint8_t * sec
|
|||
|
||||
crypto_box_keypair(crypto_connections[i].sessionpublic_key, crypto_connections[i].sessionsecret_key);
|
||||
|
||||
if(send_cryptohandshake(connection_id, public_key, crypto_connections[i].recv_nonce,
|
||||
if (send_cryptohandshake(connection_id, public_key, crypto_connections[i].recv_nonce,
|
||||
crypto_connections[i].sessionpublic_key) == 1)
|
||||
{
|
||||
increment_nonce(crypto_connections[i].recv_nonce);
|
||||
|
@ -424,7 +421,7 @@ int accept_crypto_inbound(int connection_id, uint8_t * public_key, uint8_t * sec
|
|||
4 if the connection is timed out and waiting to be killed */
|
||||
int is_cryptoconnected(int crypt_connection_id)
|
||||
{
|
||||
if(crypt_connection_id >= 0 && crypt_connection_id < MAX_CRYPTO_CONNECTIONS)
|
||||
if (crypt_connection_id >= 0 && crypt_connection_id < MAX_CRYPTO_CONNECTIONS)
|
||||
return crypto_connections[crypt_connection_id].status;
|
||||
return 0;
|
||||
}
|
||||
|
@ -438,7 +435,7 @@ void new_keys()
|
|||
|
||||
/* save the public and private keys to the keys array
|
||||
Length must be crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES */
|
||||
void save_keys(uint8_t * keys)
|
||||
void save_keys(uint8_t *keys)
|
||||
{
|
||||
memcpy(keys, self_public_key, crypto_box_PUBLICKEYBYTES);
|
||||
memcpy(keys + crypto_box_PUBLICKEYBYTES, self_secret_key, crypto_box_SECRETKEYBYTES);
|
||||
|
@ -446,7 +443,7 @@ void save_keys(uint8_t * keys)
|
|||
|
||||
/* load the public and private keys from the keys array
|
||||
Length must be crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES */
|
||||
void load_keys(uint8_t * keys)
|
||||
void load_keys(uint8_t *keys)
|
||||
{
|
||||
memcpy(self_public_key, keys, crypto_box_PUBLICKEYBYTES);
|
||||
memcpy(self_secret_key, keys + crypto_box_PUBLICKEYBYTES, crypto_box_SECRETKEYBYTES);
|
||||
|
@ -459,8 +456,8 @@ void load_keys(uint8_t * keys)
|
|||
int new_incoming(int id)
|
||||
{
|
||||
uint32_t i;
|
||||
for(i = 0; i < MAX_INCOMING; ++i) {
|
||||
if(incoming_connections[i] == -1) {
|
||||
for (i = 0; i < MAX_INCOMING; ++i) {
|
||||
if (incoming_connections[i] == -1) {
|
||||
incoming_connections[i] = id;
|
||||
return 0;
|
||||
}
|
||||
|
@ -473,7 +470,7 @@ int new_incoming(int id)
|
|||
static void handle_incomings()
|
||||
{
|
||||
int income;
|
||||
while(1) {
|
||||
while (1) {
|
||||
income = incoming_connection();
|
||||
if(income == -1 || new_incoming(income) )
|
||||
break;
|
||||
|
@ -484,22 +481,20 @@ static void handle_incomings()
|
|||
static void receive_crypto()
|
||||
{
|
||||
uint32_t i;
|
||||
for(i = 0; i < MAX_CRYPTO_CONNECTIONS; ++i)
|
||||
{
|
||||
if(crypto_connections[i].status == 1)
|
||||
{
|
||||
for (i = 0; i < MAX_CRYPTO_CONNECTIONS; ++i) {
|
||||
if (crypto_connections[i].status == 1) {
|
||||
uint8_t temp_data[MAX_DATA_SIZE];
|
||||
uint8_t secret_nonce[crypto_box_NONCEBYTES];
|
||||
uint8_t public_key[crypto_box_PUBLICKEYBYTES];
|
||||
uint8_t session_key[crypto_box_PUBLICKEYBYTES];
|
||||
uint16_t len;
|
||||
if(id_packet(crypto_connections[i].number) == 1)
|
||||
if (id_packet(crypto_connections[i].number) == 1)
|
||||
/* if the packet is a friend request drop it (because we are already friends) */
|
||||
len = read_packet(crypto_connections[i].number, temp_data);
|
||||
if(id_packet(crypto_connections[i].number) == 2) { /* handle handshake packet. */
|
||||
if (id_packet(crypto_connections[i].number) == 2) { /* handle handshake packet. */
|
||||
len = read_packet(crypto_connections[i].number, temp_data);
|
||||
if(handle_cryptohandshake(public_key, secret_nonce, session_key, temp_data, len)) {
|
||||
if(memcmp(public_key, crypto_connections[i].public_key, crypto_box_PUBLICKEYBYTES) == 0) {
|
||||
if (handle_cryptohandshake(public_key, secret_nonce, session_key, temp_data, len)) {
|
||||
if (memcmp(public_key, crypto_connections[i].public_key, crypto_box_PUBLICKEYBYTES) == 0) {
|
||||
memcpy(crypto_connections[i].sent_nonce, secret_nonce, crypto_box_NONCEBYTES);
|
||||
memcpy(crypto_connections[i].peersessionpublic_key, session_key, crypto_box_PUBLICKEYBYTES);
|
||||
increment_nonce(crypto_connections[i].sent_nonce);
|
||||
|
@ -510,15 +505,12 @@ static void receive_crypto()
|
|||
}
|
||||
}
|
||||
}
|
||||
else if(id_packet(crypto_connections[i].number) != -1)
|
||||
/* This should not happen
|
||||
kill the connection if it does */
|
||||
else if (id_packet(crypto_connections[i].number) != -1) // This should not happen kill the connection if it does
|
||||
crypto_kill(crypto_connections[i].number);
|
||||
|
||||
}
|
||||
if(crypto_connections[i].status == 2)
|
||||
{
|
||||
if(id_packet(crypto_connections[i].number) == 3) {
|
||||
if (crypto_connections[i].status == 2) {
|
||||
if (id_packet(crypto_connections[i].number) == 3) {
|
||||
uint8_t temp_data[MAX_DATA_SIZE];
|
||||
uint8_t data[MAX_DATA_SIZE];
|
||||
int length = read_packet(crypto_connections[i].number, temp_data);
|
||||
|
@ -526,7 +518,7 @@ static void receive_crypto()
|
|||
crypto_connections[i].sessionsecret_key,
|
||||
crypto_connections[i].recv_nonce, temp_data + 1, length - 1, data);
|
||||
uint32_t zero = 0;
|
||||
if(len == sizeof(uint32_t) && memcmp(((uint8_t *)&zero), data, sizeof(uint32_t)) == 0) {
|
||||
if (len == sizeof(uint32_t) && memcmp(((uint8_t *)&zero), data, sizeof(uint32_t)) == 0) {
|
||||
increment_nonce(crypto_connections[i].recv_nonce);
|
||||
crypto_connections[i].status = 3;
|
||||
|
||||
|
@ -534,9 +526,7 @@ static void receive_crypto()
|
|||
kill_connection_in(crypto_connections[i].number, 3000000);
|
||||
}
|
||||
else
|
||||
/* This should not happen
|
||||
kill the connection if it does */
|
||||
crypto_kill(crypto_connections[i].number);
|
||||
crypto_kill(crypto_connections[i].number); // This should not happen kill the connection if it does
|
||||
}
|
||||
else if(id_packet(crypto_connections[i].number) != -1)
|
||||
/* This should not happen
|
||||
|
@ -553,17 +543,17 @@ void initNetCrypto()
|
|||
memset(crypto_connections, 0 ,sizeof(crypto_connections));
|
||||
memset(incoming_connections, -1 ,sizeof(incoming_connections));
|
||||
uint32_t i;
|
||||
for(i = 0; i < MAX_CRYPTO_CONNECTIONS; ++i)
|
||||
for (i = 0; i < MAX_CRYPTO_CONNECTIONS; ++i)
|
||||
crypto_connections[i].number = ~0;
|
||||
}
|
||||
|
||||
static void killTimedout()
|
||||
{
|
||||
uint32_t i;
|
||||
for(i = 0; i < MAX_CRYPTO_CONNECTIONS; ++i) {
|
||||
if(crypto_connections[i].status != 0 && is_connected(crypto_connections[i].number) == 4)
|
||||
for (i = 0; i < MAX_CRYPTO_CONNECTIONS; ++i) {
|
||||
if (crypto_connections[i].status != 0 && is_connected(crypto_connections[i].number) == 4)
|
||||
crypto_connections[i].status = 4;
|
||||
else if(is_connected(crypto_connections[i].number) == 4) {
|
||||
else if (is_connected(crypto_connections[i].number) == 4) {
|
||||
kill_connection(crypto_connections[i].number);
|
||||
crypto_connections[i].number = ~0;
|
||||
}
|
||||
|
|
|
@ -40,29 +40,29 @@ extern uint8_t self_secret_key[crypto_box_SECRETKEYBYTES];
|
|||
public key(32 bytes) of the receiver and the secret key of the sender and a 24 byte nonce
|
||||
return -1 if there was a problem.
|
||||
return length of encrypted data if everything was fine. */
|
||||
int encrypt_data(uint8_t * public_key, uint8_t * secret_key, uint8_t * nonce,
|
||||
uint8_t * plain, uint32_t length, uint8_t * encrypted);
|
||||
int encrypt_data(uint8_t *public_key, uint8_t *secret_key, uint8_t *nonce,
|
||||
uint8_t *plain, uint32_t length, uint8_t *encrypted);
|
||||
|
||||
|
||||
/* decrypts encrypted of length length to plain of length length - 16 using the
|
||||
public key(32 bytes) of the sender, the secret key of the receiver and a 24 byte nonce
|
||||
return -1 if there was a problem(decryption failed)
|
||||
return length of plain data if everything was fine. */
|
||||
int decrypt_data(uint8_t * public_key, uint8_t * secret_key, uint8_t * nonce,
|
||||
uint8_t * encrypted, uint32_t length, uint8_t * plain);
|
||||
int decrypt_data(uint8_t *public_key, uint8_t *secret_key, uint8_t *nonce,
|
||||
uint8_t *encrypted, uint32_t length, uint8_t *plain);
|
||||
|
||||
|
||||
/* fill the given nonce with random bytes. */
|
||||
void random_nonce(uint8_t * nonce);
|
||||
void random_nonce(uint8_t *nonce);
|
||||
|
||||
/* return 0 if there is no received data in the buffer
|
||||
return -1 if the packet was discarded.
|
||||
return length of received data if successful */
|
||||
int read_cryptpacket(int crypt_connection_id, uint8_t * data);
|
||||
int read_cryptpacket(int crypt_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_cryptpacket(int crypt_connection_id, uint8_t * data, uint32_t length);
|
||||
int write_cryptpacket(int crypt_connection_id, uint8_t *data, uint32_t length);
|
||||
|
||||
/* create a request to peer with public_key.
|
||||
packet must be an array of MAX_DATA_SIZE big.
|
||||
|
@ -70,18 +70,18 @@ int write_cryptpacket(int crypt_connection_id, uint8_t * data, uint32_t length);
|
|||
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 * packet, uint8_t * public_key, uint8_t * data, uint32_t length, uint8_t request_id);
|
||||
int create_request(uint8_t *packet, uint8_t * public_key, uint8_t *data, uint32_t length, uint8_t request_id);
|
||||
|
||||
/* puts the senders public key in the request in public_key, the data from the request
|
||||
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. */
|
||||
int handle_request(uint8_t * public_key, uint8_t * data, uint8_t * packet, uint16_t length);
|
||||
int handle_request(uint8_t *public_key, uint8_t *data, uint8_t *packet, uint16_t length);
|
||||
|
||||
/* Start a secure connection with other peer who has public_key and ip_port
|
||||
returns -1 if failure
|
||||
returns crypt_connection_id of the initialized connection if everything went well. */
|
||||
int crypto_connect(uint8_t * public_key, IP_Port ip_port);
|
||||
int crypto_connect(uint8_t *public_key, IP_Port ip_port);
|
||||
|
||||
/* kill a crypto connection
|
||||
return 0 if killed successfully
|
||||
|
@ -95,12 +95,12 @@ int crypto_kill(int crypt_connection_id);
|
|||
and the session public key for the connection in session_key
|
||||
to accept it see: accept_crypto_inbound(...)
|
||||
to refuse it just call kill_connection(...) on the connection id */
|
||||
int crypto_inbound(uint8_t * public_key, uint8_t * secret_nonce, uint8_t * session_key);
|
||||
int crypto_inbound(uint8_t *public_key, uint8_t * secret_nonce, uint8_t *session_key);
|
||||
|
||||
/* 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(int connection_id, uint8_t * public_key, uint8_t * secret_nonce, uint8_t * session_key);
|
||||
int accept_crypto_inbound(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.
|
||||
|
|
|
@ -79,13 +79,10 @@ int receivepacket(IP_Port * ip_port, uint8_t * data, uint32_t * length)
|
|||
#else
|
||||
uint32_t addrlen = sizeof(addr);
|
||||
#endif
|
||||
(*(int32_t *)length) = recvfrom(sock,(char *) data, MAX_UDP_PACKET_SIZE, 0, (struct sockaddr *)&addr, &addrlen);
|
||||
if(*(int32_t *)length <= 0)
|
||||
{
|
||||
/* nothing received
|
||||
or empty packet */
|
||||
return -1;
|
||||
}
|
||||
(*(int32_t*)length) = recvfrom(sock,(char*) data, MAX_UDP_PACKET_SIZE, 0, (struct sockaddr*)&addr, &addrlen);
|
||||
if (*(int32_t*)length <= 0)
|
||||
return -1; /* nothing received or empty packet */
|
||||
|
||||
ip_port->ip = addr.ip;
|
||||
ip_port->port = addr.port;
|
||||
return 0;
|
||||
|
@ -101,7 +98,7 @@ int init_networking(IP ip, uint16_t port)
|
|||
{
|
||||
#ifdef WIN32
|
||||
WSADATA wsaData;
|
||||
if(WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)
|
||||
if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)
|
||||
return -1;
|
||||
#else
|
||||
srandom((uint32_t)current_time());
|
||||
|
|
|
@ -68,23 +68,20 @@ extern "C" {
|
|||
|
||||
#define MAX_UDP_PACKET_SIZE 65507
|
||||
|
||||
typedef union
|
||||
{
|
||||
typedef union {
|
||||
uint8_t c[4];
|
||||
uint16_t s[2];
|
||||
uint32_t i;
|
||||
}IP;
|
||||
} IP;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
typedef struct {
|
||||
IP ip;
|
||||
uint16_t port;
|
||||
/* not used for anything right now */
|
||||
uint16_t padding;
|
||||
}IP_Port;
|
||||
} IP_Port;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
typedef struct {
|
||||
int16_t family;
|
||||
uint16_t port;
|
||||
IP ip;
|
||||
|
@ -92,7 +89,7 @@ typedef struct
|
|||
#ifdef ENABLE_IPV6
|
||||
uint8_t zeroes2[12];
|
||||
#endif
|
||||
}ADDR;
|
||||
} ADDR;
|
||||
|
||||
/* returns current time in milleseconds since the epoch. */
|
||||
uint64_t current_time();
|
||||
|
@ -104,12 +101,12 @@ uint32_t random_int();
|
|||
/* Basic network functions: */
|
||||
|
||||
/* Function to send packet(data) of length length to ip_port */
|
||||
int sendpacket(IP_Port ip_port, uint8_t * data, uint32_t length);
|
||||
int sendpacket(IP_Port ip_port, uint8_t *data, uint32_t length);
|
||||
|
||||
/* Function to receive data, ip and port of sender is put into ip_port
|
||||
the packet data into data
|
||||
the packet length into length. */
|
||||
int receivepacket(IP_Port * ip_port, uint8_t * data, uint32_t * length);
|
||||
int receivepacket(IP_Port *ip_port, uint8_t *data, uint32_t *length);
|
||||
|
||||
/* initialize networking
|
||||
bind to ip and port
|
||||
|
@ -117,7 +114,7 @@ int receivepacket(IP_Port * ip_port, uint8_t * data, uint32_t * length);
|
|||
port is in host byte order (this means don't worry about it)
|
||||
returns 0 if no problems
|
||||
returns -1 if there were problems */
|
||||
int init_networking(IP ip ,uint16_t port);
|
||||
int init_networking(IP ip, uint16_t port);
|
||||
|
||||
/* function to cleanup networking stuff(doesn't do much right now) */
|
||||
void shutdown_networking();
|
||||
|
|
|
@ -96,78 +96,72 @@ int main(int argc, char *argv[])
|
|||
uint8_t buffer2[128];
|
||||
int read2 = 0;
|
||||
FILE *file1 = fopen(argv[4], "rb");
|
||||
if ( file1==NULL ){printf("Error opening file.\n");return 1;}
|
||||
if (file1 == NULL) {
|
||||
printf("Error opening file.\n");
|
||||
return 1;
|
||||
}
|
||||
FILE *file2 = fopen("received.txt", "wb");
|
||||
if ( file2==NULL ){return 1;}
|
||||
if (file2 == NULL)
|
||||
return 1;
|
||||
|
||||
read1 = fread(buffer1, 1, 128, file1);
|
||||
|
||||
while(1) {
|
||||
|
||||
while(receivepacket(&ip_port, data, &length) != -1) {
|
||||
if(rand() % 3 != 1) { /* simulate packet loss */
|
||||
if(DHT_handlepacket(data, length, ip_port) && LosslessUDP_handlepacket(data, length, ip_port)) {
|
||||
/* if packet is not recognized */
|
||||
printf("Received unhandled packet with length: %u\n", length);
|
||||
} else {
|
||||
while (1) {
|
||||
while (receivepacket(&ip_port, data, &length) != -1) {
|
||||
if (rand() % 3 != 1) { /* simulate packet loss */
|
||||
if (DHT_handlepacket(data, length, ip_port) && LosslessUDP_handlepacket(data, length, ip_port))
|
||||
printf("Received unhandled packet with length: %u\n", length); /* if packet is not recognized */
|
||||
else
|
||||
printf("Received handled packet with length: %u\n", length);
|
||||
}
|
||||
}
|
||||
}
|
||||
friend_ip = DHT_getfriendip((uint8_t *)argv[3]);
|
||||
if(friend_ip.ip.i != 0) {
|
||||
if(connection == -1) {
|
||||
if (friend_ip.ip.i != 0) {
|
||||
if (connection == -1) {
|
||||
printf("Started connecting to friend:");
|
||||
printip(friend_ip);
|
||||
connection = new_connection(friend_ip);
|
||||
}
|
||||
}
|
||||
if(inconnection == -1) {
|
||||
if (inconnection == -1) {
|
||||
inconnection = incoming_connection();
|
||||
if(inconnection != -1) {
|
||||
if (inconnection != -1) {
|
||||
printf("Someone connected to us:");
|
||||
printip(connection_ip(inconnection));
|
||||
}
|
||||
}
|
||||
/* if someone connected to us write what he sends to a file */
|
||||
/* also send him our file. */
|
||||
if(inconnection != -1) {
|
||||
if(write_packet(inconnection, buffer1, read1)) {
|
||||
if (inconnection != -1) {
|
||||
if (write_packet(inconnection, buffer1, read1)) {
|
||||
printf("Wrote data.\n");
|
||||
read1 = fread(buffer1, 1, 128, file1);
|
||||
}
|
||||
read2 = read_packet(inconnection, buffer2);
|
||||
if(read2 != 0) {
|
||||
if (read2 != 0) {
|
||||
printf("Received data.\n");
|
||||
if(!fwrite(buffer2, read2, 1, file2)) {
|
||||
if (!fwrite(buffer2, read2, 1, file2))
|
||||
printf("file write error\n");
|
||||
}
|
||||
if(read2 < 128) {
|
||||
if (read2 < 128) {
|
||||
fclose(file2);
|
||||
}
|
||||
}
|
||||
}
|
||||
/* if we are connected to a friend send him data from the file.
|
||||
* also put what he sends us in a file. */
|
||||
if(is_connected(connection) == 3)
|
||||
{
|
||||
if(write_packet(0, buffer1, read1))
|
||||
{
|
||||
if (is_connected(connection) == 3) {
|
||||
if (write_packet(0, buffer1, read1)) {
|
||||
printf("Wrote data.\n");
|
||||
read1 = fread(buffer1, 1, 128, file1);
|
||||
}
|
||||
read2 = read_packet(0, buffer2);
|
||||
if(read2 != 0)
|
||||
{
|
||||
if (read2 != 0) {
|
||||
printf("Received data.\n");
|
||||
if(!fwrite(buffer2, read2, 1, file2))
|
||||
{
|
||||
printf("file write error\n");
|
||||
}
|
||||
printf("file write error\n");
|
||||
if(read2 < 128)
|
||||
{
|
||||
fclose(file2);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
doDHT();
|
||||
doLossless_UDP();
|
||||
|
|
|
@ -44,13 +44,13 @@
|
|||
|
||||
#define PORT 33446
|
||||
|
||||
void printpacket(uint8_t * data, uint32_t length, IP_Port ip_port)
|
||||
void printpacket(uint8_t *data, uint32_t length, IP_Port ip_port)
|
||||
{
|
||||
uint32_t i;
|
||||
printf("UNHANDLED PACKET RECEIVED\nLENGTH:%u\nCONTENTS:\n", length);
|
||||
printf("--------------------BEGIN-----------------------------\n");
|
||||
for(i = 0; i < length; i++) {
|
||||
if(data[i] < 16)
|
||||
for (i = 0; i < length; i++) {
|
||||
if (data[i] < 16)
|
||||
printf("0");
|
||||
printf("%hhX",data[i]);
|
||||
}
|
||||
|
@ -59,7 +59,7 @@ void printpacket(uint8_t * data, uint32_t length, IP_Port ip_port)
|
|||
|
||||
void printip(IP_Port ip_port)
|
||||
{
|
||||
printf("\nIP: %u.%u.%u.%u Port: %u",ip_port.ip.c[0],ip_port.ip.c[1],ip_port.ip.c[2],ip_port.ip.c[3],ntohs(ip_port.port));
|
||||
printf("\nIP: %u.%u.%u.%u Port: %u", ip_port.ip.c[0], ip_port.ip.c[1], ip_port.ip.c[2], ip_port.ip.c[3], ntohs(ip_port.port));
|
||||
}
|
||||
/*
|
||||
void printpackets(Data test)
|
||||
|
@ -123,16 +123,15 @@ void Lossless_UDP()
|
|||
IP_Port ip_port;
|
||||
uint8_t data[MAX_UDP_PACKET_SIZE];
|
||||
uint32_t length;
|
||||
while(receivepacket(&ip_port, data, &length) != -1) {
|
||||
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 {
|
||||
//printconnection(0);
|
||||
printf("Received handled packet with length: %u\n", length);
|
||||
}
|
||||
if (LosslessUDP_handlepacket(data, length, ip_port))
|
||||
printpacket(data, length, ip_port);
|
||||
else
|
||||
printf("Received handled packet with length: %u\n", length); //printconnection(0);
|
||||
|
||||
/* } */
|
||||
}
|
||||
|
||||
|
@ -151,7 +150,8 @@ int main(int argc, char *argv[])
|
|||
int read;
|
||||
|
||||
FILE *file = fopen(argv[3], "rb");
|
||||
if ( file==NULL ){return 1;}
|
||||
if (file == NULL)
|
||||
return 1;
|
||||
|
||||
|
||||
/* initialize networking */
|
||||
|
@ -166,14 +166,14 @@ int main(int argc, char *argv[])
|
|||
printip(serverip);
|
||||
int connection = new_connection(serverip);
|
||||
uint64_t timer = current_time();
|
||||
while(1) {
|
||||
while (1) {
|
||||
/* printconnection(connection); */
|
||||
Lossless_UDP();
|
||||
if(is_connected(connection) == 3) {
|
||||
if (is_connected(connection) == 3) {
|
||||
printf("Connecting took: %llu us\n", (unsigned long long)(current_time() - timer));
|
||||
break;
|
||||
}
|
||||
if(is_connected(connection) == 0) {
|
||||
if (is_connected(connection) == 0) {
|
||||
printf("Connection timeout after: %llu us\n", (unsigned long long)(current_time() - timer));
|
||||
return 1;
|
||||
}
|
||||
|
@ -185,19 +185,19 @@ int main(int argc, char *argv[])
|
|||
/*read first part of file */
|
||||
read = fread(buffer, 1, 512, file);
|
||||
|
||||
while(1) {
|
||||
while (1) {
|
||||
/* printconnection(connection); */
|
||||
Lossless_UDP();
|
||||
if(is_connected(connection) == 3) {
|
||||
if (is_connected(connection) == 3) {
|
||||
|
||||
if(write_packet(connection, buffer, read)) {
|
||||
if (write_packet(connection, buffer, read)) {
|
||||
/* printf("Wrote data.\n"); */
|
||||
read = fread(buffer, 1, 512, file);
|
||||
|
||||
}
|
||||
/* printf("%u\n", sendqueue(connection)); */
|
||||
if(sendqueue(connection) == 0) {
|
||||
if(read == 0) {
|
||||
if (sendqueue(connection) == 0) {
|
||||
if (read == 0) {
|
||||
printf("Sent file successfully in: %llu us\n", (unsigned long long)(current_time() - timer));
|
||||
break;
|
||||
}
|
||||
|
|
|
@ -45,12 +45,12 @@
|
|||
|
||||
#define PORT 33445
|
||||
|
||||
void printpacket(uint8_t * data, uint32_t length, IP_Port ip_port)
|
||||
void printpacket(uint8_t *data, uint32_t length, IP_Port ip_port)
|
||||
{
|
||||
uint32_t i;
|
||||
printf("UNHANDLED PACKET RECEIVED\nLENGTH:%u\nCONTENTS:\n", length);
|
||||
printf("--------------------BEGIN-----------------------------\n");
|
||||
for(i = 0; i < length; i++) {
|
||||
for (i = 0; i < length; i++) {
|
||||
if(data[i] < 16)
|
||||
printf("0");
|
||||
printf("%hhX",data[i]);
|
||||
|
@ -120,10 +120,10 @@ void Lossless_UDP()
|
|||
IP_Port ip_port;
|
||||
uint8_t data[MAX_UDP_PACKET_SIZE];
|
||||
uint32_t length;
|
||||
while(receivepacket(&ip_port, data, &length) != -1) {
|
||||
while (receivepacket(&ip_port, data, &length) != -1) {
|
||||
//if(rand() % 3 != 1)//add packet loss
|
||||
//{
|
||||
if(LosslessUDP_handlepacket(data, length, ip_port)) {
|
||||
if (LosslessUDP_handlepacket(data, length, ip_port)) {
|
||||
printpacket(data, length, ip_port);
|
||||
} else {
|
||||
//printconnection(0);
|
||||
|
@ -147,7 +147,8 @@ int main(int argc, char *argv[])
|
|||
int read;
|
||||
|
||||
FILE *file = fopen(argv[1], "wb");
|
||||
if ( file==NULL ){return 1;}
|
||||
if (file == NULL)
|
||||
return 1;
|
||||
|
||||
|
||||
//initialize networking
|
||||
|
@ -161,7 +162,7 @@ int main(int argc, char *argv[])
|
|||
uint64_t timer = current_time();
|
||||
|
||||
|
||||
while(1) {
|
||||
while (1) {
|
||||
Lossless_UDP();
|
||||
connection = incoming_connection();
|
||||
if(connection != -1) {
|
||||
|
@ -176,17 +177,16 @@ int main(int argc, char *argv[])
|
|||
|
||||
timer = current_time();
|
||||
|
||||
while(1) {
|
||||
while (1) {
|
||||
//printconnection(0);
|
||||
Lossless_UDP();
|
||||
if(is_connected(connection) >= 2) {
|
||||
if (is_connected(connection) >= 2) {
|
||||
kill_connection_in(connection, 3000000);
|
||||
read = read_packet(connection, buffer);
|
||||
if(read != 0) {
|
||||
if (read != 0) {
|
||||
// printf("Recieved data.\n");
|
||||
if(!fwrite(buffer, read, 1, file)) {
|
||||
if (!fwrite(buffer, read, 1, file))
|
||||
printf("file write error\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
if(is_connected(connection) == 4) {
|
||||
|
|
|
@ -39,9 +39,9 @@ int nick_before;
|
|||
void new_lines(char *line)
|
||||
{
|
||||
int i;
|
||||
for (i = HISTORY-1; i > 0; i--) {
|
||||
for (i = HISTORY-1; i > 0; i--)
|
||||
strcpy(lines[i],lines[i-1]);
|
||||
}
|
||||
|
||||
strcpy(lines[0],line);
|
||||
do_refresh();
|
||||
}
|
||||
|
@ -53,8 +53,7 @@ unsigned char * hex_string_to_bin(char hex_string[])
|
|||
unsigned char * val = malloc(len);
|
||||
char * pos = hex_string;
|
||||
int i=0;
|
||||
while(i < len)
|
||||
{
|
||||
while(i < len) {
|
||||
sscanf(pos,"%2hhx",&val[i]);
|
||||
pos+=2;
|
||||
i++;
|
||||
|
@ -71,17 +70,18 @@ void line_eval(char lines[HISTORY][STRING_LENGTH], char *line)
|
|||
if (line[1] == 'f') { // add friend command: /f ID
|
||||
int i;
|
||||
char temp_id[128];
|
||||
for (i=0; i<128; i++) {
|
||||
for (i=0; i<128; i++)
|
||||
temp_id[i] = line[i+3];
|
||||
}
|
||||
int num = m_addfriend(hex_string_to_bin(temp_id), (uint8_t*)"Install Gentoo", sizeof("Install Gentoo"));
|
||||
char numstring[100];
|
||||
sprintf(numstring, "[i] added friend %d", num);
|
||||
new_lines(numstring);
|
||||
do_refresh();
|
||||
} else if (line[1] == 'd') {
|
||||
}
|
||||
else if (line[1] == 'd') {
|
||||
doMessenger();
|
||||
} else if (line[1] == 'm') { //message command: /m friendnumber messsage
|
||||
}
|
||||
else if (line[1] == 'm') { //message command: /m friendnumber messsage
|
||||
int i;
|
||||
size_t len = strlen(line);
|
||||
char numstring[len-3];
|
||||
|
@ -91,15 +91,15 @@ void line_eval(char lines[HISTORY][STRING_LENGTH], char *line)
|
|||
numstring[i] = line[i+3];
|
||||
} else {
|
||||
int j;
|
||||
for (j=i+1; j<len; j++) {
|
||||
for (j=i+1; j<len; j++)
|
||||
message[j-i-1] = line[j+3];
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
int num = atoi(numstring);
|
||||
m_sendmessage(num, (uint8_t*) message, sizeof(message));
|
||||
} else if (line[1] == 'n') {
|
||||
}
|
||||
else if (line[1] == 'n') {
|
||||
uint8_t name[MAX_NAME_LENGTH];
|
||||
int i = 0;
|
||||
size_t len = strlen(line);
|
||||
|
@ -112,7 +112,8 @@ void line_eval(char lines[HISTORY][STRING_LENGTH], char *line)
|
|||
char numstring[100];
|
||||
sprintf(numstring, "[i] changed nick to %s", (char*)name);
|
||||
new_lines(numstring);
|
||||
} else if (line[1] == 's') {
|
||||
}
|
||||
else if (line[1] == 's') {
|
||||
uint8_t status[MAX_USERSTATUS_LENGTH];
|
||||
int i = 0;
|
||||
size_t len = strlen(line);
|
||||
|
@ -125,7 +126,8 @@ void line_eval(char lines[HISTORY][STRING_LENGTH], char *line)
|
|||
char numstring[100];
|
||||
sprintf(numstring, "[i] changed status to %s", (char*)status);
|
||||
new_lines(numstring);
|
||||
} else if (line[1] == 'q') { //exit
|
||||
}
|
||||
else if (line[1] == 'q') { //exit
|
||||
endwin();
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
|
@ -139,7 +141,7 @@ void wrap(char output[STRING_LENGTH], char input[STRING_LENGTH], int line_width)
|
|||
int i = 0;
|
||||
strcpy(output,input);
|
||||
size_t len = strlen(output);
|
||||
for (i=line_width; i < len; i = i + line_width) {
|
||||
for (i = line_width; i < len; i = i + line_width) {
|
||||
while (output[i] != ' ' && i != 0) {
|
||||
i--;
|
||||
}
|
||||
|
@ -155,9 +157,8 @@ int count_lines(char *string)
|
|||
int i;
|
||||
int count = 1;
|
||||
for (i=0; i < len; i++) {
|
||||
if (string[i] == '\n') {
|
||||
if (string[i] == '\n')
|
||||
count++;
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
@ -196,11 +197,11 @@ void do_refresh()
|
|||
refresh();
|
||||
}
|
||||
|
||||
void print_request(uint8_t * public_key, uint8_t * data, uint16_t length)
|
||||
void print_request(uint8_t *public_key, uint8_t *data, uint16_t length)
|
||||
{
|
||||
new_lines("[i] received friend request");
|
||||
do_refresh();
|
||||
if(memcmp(data , "Install Gentoo", sizeof("Install Gentoo")) == 0 )
|
||||
if (memcmp(data , "Install Gentoo", sizeof("Install Gentoo")) == 0)
|
||||
//if the request contained the message of peace the person is obviously a friend so we add him.
|
||||
{
|
||||
new_lines("[i] friend request accepted");
|
||||
|
@ -298,15 +299,14 @@ int main(int argc, char *argv[])
|
|||
uint32_t i;
|
||||
for(i = 0; i < 32; i++)
|
||||
{
|
||||
if(self_public_key[i] < 16) {
|
||||
if(self_public_key[i] < 16)
|
||||
strcpy(idstring1[i],"0");
|
||||
} else {
|
||||
else
|
||||
strcpy(idstring1[i], "");
|
||||
}
|
||||
sprintf(idstring2[i], "%hhX",self_public_key[i]);
|
||||
}
|
||||
strcpy(idstring0,"[i] your ID: ");
|
||||
for(i=0; i<32; i++) {
|
||||
for (i=0; i<32; i++) {
|
||||
strcat(idstring0,idstring1[i]);
|
||||
strcat(idstring0,idstring2[i]);
|
||||
}
|
||||
|
@ -320,11 +320,11 @@ int main(int argc, char *argv[])
|
|||
IP_Port bootstrap_ip_port;
|
||||
bootstrap_ip_port.port = htons(atoi(argv[2]));
|
||||
int resolved_address = resolve_addr(argv[1]);
|
||||
if (resolved_address != -1) {
|
||||
if (resolved_address != -1)
|
||||
bootstrap_ip_port.ip.i = resolved_address;
|
||||
} else {
|
||||
else
|
||||
exit(1);
|
||||
}
|
||||
|
||||
DHT_bootstrap(bootstrap_ip_port, hex_string_to_bin(argv[3]));
|
||||
nodelay(stdscr, TRUE);
|
||||
while(true) {
|
||||
|
|
Loading…
Reference in New Issue
Block a user