A *lot* of style changes.

This commit is contained in:
Konstantin Kowalski 2013-07-26 23:07:25 -04:00
parent 1b4ea2e1ae
commit 241aca98bd
18 changed files with 569 additions and 672 deletions

View File

@ -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;
@ -37,8 +36,7 @@ typedef struct
/* 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. */
@ -51,14 +49,12 @@ typedef struct
uint32_t NATping_timestamp;
} Friend;
typedef struct
{
typedef struct {
uint8_t client_id[CLIENT_ID_SIZE];
IP_Port ip_port;
} Node_format;
typedef struct
{
typedef struct {
IP_Port ip_port;
uint64_t ping_id;
uint32_t timestamp;
@ -94,15 +90,12 @@ int id_closest(uint8_t * client_id, uint8_t * client_id1, uint8_t * client_id2)
{
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])) {
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.
@ -118,7 +111,8 @@ int client_in_list(Client_data * list, uint32_t length, uint8_t * client_id, IP_
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);
}
@ -140,11 +134,9 @@ 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) {
if (memcmp(list[i].client_id, client_id, CLIENT_ID_SIZE) == 0)
return 1;
}
}
return 0;
}
@ -156,10 +148,8 @@ 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 */
{
return i;
}
}
return -1;
}
@ -178,7 +168,8 @@ int get_close_nodes(uint8_t * client_id, Node_format * nodes_list)
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)) {
!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);
@ -267,11 +258,8 @@ void addto_lists(IP_Port ip_port, uint8_t * client_id)
/* 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 (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)) {
@ -298,7 +286,7 @@ void returnedip_ports(IP_Port ip_port, uint8_t * client_id, uint8_t * nodeclient
}
}
}
else
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) {
@ -311,6 +299,7 @@ void returnedip_ports(IP_Port ip_port, uint8_t * client_id, uint8_t * nodeclient
}
}
}
}
/* ping timeout in seconds */
#define PING_TIMEOUT 5
@ -331,21 +320,19 @@ int is_pinging(IP_Port ip_port, uint64_t ping_id)
pinging = 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) {
pings[i].ip_port.port == ip_port.port)
{
++pinging;
}
}
if (ping_id != 0) {
if(pings[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;
}
}
return 0;
@ -369,15 +356,12 @@ int is_gettingnodes(IP_Port ip_port, uint64_t ping_id)
}
}
if (ping_id != 0) {
if(send_nodes[i].ping_id == ping_id) {
if (send_nodes[i].ping_id == ping_id)
++pinging;
}
}
if(pinging == (ping_id != 0) + (ip_port.ip.i != 0)) {
if(pinging == (ping_id != 0) + (ip_port.ip.i != 0))
return 1;
}
}
}
return 0;
@ -433,18 +417,15 @@ uint64_t add_gettingnodes(IP_Port ip_port)
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)
{
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);
@ -468,9 +448,8 @@ static int pingreq(IP_Port ip_port, uint8_t * public_key)
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);
@ -494,19 +472,15 @@ static int pingres(IP_Port ip_port, uint8_t * public_key, uint64_t ping_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);
@ -533,9 +506,8 @@ 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)
{
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);
@ -577,23 +547,16 @@ static int sendnodes(IP_Port ip_port, uint8_t * public_key, uint8_t * client_id,
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);
@ -606,21 +569,16 @@ int handle_pingreq(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)) {
addto_lists(source, packet + 1);
@ -633,13 +591,11 @@ int handle_pingres(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,14 +636,11 @@ 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));
@ -697,10 +648,9 @@ int handle_sendnodes(uint8_t * packet, uint32_t length, IP_Port source)
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;
}
@ -710,14 +660,12 @@ int handle_sendnodes(uint8_t * packet, uint32_t length, IP_Port source)
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));
@ -735,13 +683,11 @@ int DHT_delfriend(uint8_t * client_id)
/* Equal */
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;
}
}
@ -878,17 +824,15 @@ 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 ) {
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;
}
@ -910,11 +854,10 @@ int route_tofriend(uint8_t * friend_id, uint8_t * packet, uint32_t length)
/*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;
}
}

View File

@ -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)
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 -1;
}
@ -202,11 +200,12 @@ int new_inconnection(IP_Port ip_port)
int incoming_connection()
{
uint32_t i;
for(i = 0; i < MAX_CONNECTIONS; ++i)
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 (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 (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;
}
@ -320,12 +321,13 @@ uint32_t missing_packets(int connection_id, uint32_t * requested)
uint32_t temp;
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;
@ -599,7 +601,7 @@ int handle_data(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);

View File

@ -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. */
@ -61,10 +60,11 @@ int getfriend_id(uint8_t * client_id)
{
uint32_t i;
for(i = 0; i < numfriends; ++i)
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;
}
@ -102,10 +102,8 @@ int m_addfriend(uint8_t * client_id, uint8_t * data, uint16_t length)
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;
@ -128,10 +126,8 @@ int m_addfriend_norequest(uint8_t * client_id)
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;
@ -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)
for (i = numfriends; i != 0; --i) {
if (friendlist[i].status != 0)
break;
}
numfriends = i;
return 0;
}
@ -350,29 +347,23 @@ 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();
}
@ -394,24 +385,23 @@ static void doFriends()
break;
}
}
while(friendlist[i].status == 4) /* friend is online */
{
if(friendlist[i].name_sent == 0)
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 (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)
{
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;
@ -420,9 +410,7 @@ static void doFriends()
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));
}
set_friend_userstatus(i, status, MIN(len - 1, MAX_USERSTATUS_LENGTH));
free(status);
break;
@ -434,10 +422,8 @@ static void doFriends()
}
}
}
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,8 +473,7 @@ 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 */
/* { */
@ -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); */

View File

@ -43,8 +43,7 @@ int send_friendrequest(uint8_t * public_key, uint8_t * data, uint32_t length)
if (ip_port.ip.i == 1)
return -1;
if(ip_port.ip.i != 0)
{
if (ip_port.ip.i != 0) {
if (sendpacket(ip_port, packet, len) != -1)
return 0;
return -1;
@ -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,8 +76,7 @@ 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)
{
static void addto_recievedlist(uint8_t * client_id) {
if (recieved_requests_index >= MAX_RECIEVED_STORED)
recieved_requests_index = 0;
@ -89,28 +86,24 @@ 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)
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)
{
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 (memcmp(packet + 1, self_public_key, crypto_box_PUBLICKEYBYTES) == 0) {// check if request is for us.
if (handle_friendrequest_isset == 0)
return 1;
@ -126,9 +119,10 @@ int friendreq_handlepacket(uint8_t * packet, uint32_t length, IP_Port source)
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;
}

View File

@ -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. */
@ -286,10 +285,11 @@ int handle_cryptohandshake(uint8_t * public_key, uint8_t * secret_nonce,
int getcryptconnection_id(uint8_t *public_key)
{
uint32_t i;
for(i = 0; i < MAX_CRYPTO_CONNECTIONS; ++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)
return i;
}
return -1;
}
@ -305,8 +305,7 @@ int crypto_connect(uint8_t * public_key, IP_Port ip_port)
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)
{
for (i = 0; i < MAX_CRYPTO_CONNECTIONS; ++i) {
if (crypto_connections[i].status == 0) {
int id = new_connection(ip_port);
if (id == -1)
@ -339,8 +338,7 @@ int crypto_connect(uint8_t * public_key, IP_Port ip_port)
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)
{
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]);
@ -390,8 +388,7 @@ int accept_crypto_inbound(int connection_id, uint8_t * public_key, uint8_t * sec
{
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;
@ -484,10 +481,8 @@ 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];
@ -510,14 +505,11 @@ 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 (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];
@ -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

View File

@ -81,11 +81,8 @@ int receivepacket(IP_Port * ip_port, uint8_t * data, uint32_t * length)
#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;
}
return -1; /* nothing received or empty packet */
ip_port->ip = addr.ip;
ip_port->port = addr.port;
return 0;

View File

@ -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;
typedef struct
{
typedef struct {
IP ip;
uint16_t port;
/* not used for anything right now */
uint16_t padding;
} IP_Port;
typedef struct
{
typedef struct {
int16_t family;
uint16_t port;
IP ip;

View File

@ -96,23 +96,25 @@ 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 {
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) {
@ -138,9 +140,8 @@ int main(int argc, char *argv[])
read2 = read_packet(inconnection, buffer2);
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) {
fclose(file2);
}
@ -148,27 +149,20 @@ int main(int argc, char *argv[])
}
/* 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");
}
if(read2 < 128)
{
fclose(file2);
}
}
}
doDHT();
doLossless_UDP();
/* print_clientlist();

View File

@ -127,12 +127,11 @@ void Lossless_UDP()
printf("packet with length: %u\n", length);
/* 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);
printf("Received handled packet with length: %u\n", length);
}
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 */

View File

@ -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
@ -184,11 +185,10 @@ int main(int argc, char *argv[])
read = read_packet(connection, buffer);
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) {
printf("Connecting Lost after: %llu us\n", (unsigned long long)(current_time() - timer));
fclose(file);

View File

@ -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);
}
@ -155,10 +157,9 @@ 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;
}
@ -298,11 +299,10 @@ 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: ");
@ -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) {