Ran the code through: astyle --style=linux

This commit is contained in:
irungentoo 2013-07-27 08:43:36 -04:00
parent 1a6446266c
commit 14b43651c1
14 changed files with 737 additions and 749 deletions

View File

@ -23,8 +23,7 @@
#include "DHT.h" #include "DHT.h"
typedef struct typedef struct {
{
uint8_t client_id[CLIENT_ID_SIZE]; uint8_t client_id[CLIENT_ID_SIZE];
IP_Port ip_port; IP_Port ip_port;
uint32_t timestamp; uint32_t timestamp;
@ -37,8 +36,7 @@ typedef struct
/* maximum number of clients stored per friend. */ /* maximum number of clients stored per friend. */
#define MAX_FRIEND_CLIENTS 8 #define MAX_FRIEND_CLIENTS 8
typedef struct typedef struct {
{
uint8_t client_id[CLIENT_ID_SIZE]; uint8_t client_id[CLIENT_ID_SIZE];
Client_data client_list[MAX_FRIEND_CLIENTS]; Client_data client_list[MAX_FRIEND_CLIENTS];
uint32_t lastgetnode; /* time at which the last get_nodes request was sent. */ uint32_t lastgetnode; /* time at which the last get_nodes request was sent. */
@ -51,14 +49,12 @@ typedef struct
uint32_t NATping_timestamp; uint32_t NATping_timestamp;
} Friend; } Friend;
typedef struct typedef struct {
{
uint8_t client_id[CLIENT_ID_SIZE]; uint8_t client_id[CLIENT_ID_SIZE];
IP_Port ip_port; IP_Port ip_port;
} Node_format; } Node_format;
typedef struct typedef struct {
{
IP_Port ip_port; IP_Port ip_port;
uint64_t ping_id; uint64_t ping_id;
uint32_t timestamp; uint32_t timestamp;
@ -174,8 +170,7 @@ int get_close_nodes(uint8_t * client_id, Node_format * nodes_list)
memcpy(nodes_list[num_nodes].client_id, close_clientlist[i].client_id, CLIENT_ID_SIZE); memcpy(nodes_list[num_nodes].client_id, close_clientlist[i].client_id, CLIENT_ID_SIZE);
nodes_list[num_nodes].ip_port = close_clientlist[i].ip_port; nodes_list[num_nodes].ip_port = close_clientlist[i].ip_port;
num_nodes++; num_nodes++;
} } else for(j = 0; j < MAX_SENT_NODES; ++j)
else for(j = 0; j < MAX_SENT_NODES; ++j)
if(id_closest(client_id, nodes_list[j].client_id, close_clientlist[i].client_id) == 2) { if(id_closest(client_id, nodes_list[j].client_id, close_clientlist[i].client_id) == 2) {
memcpy(nodes_list[j].client_id, close_clientlist[i].client_id, CLIENT_ID_SIZE); memcpy(nodes_list[j].client_id, close_clientlist[i].client_id, CLIENT_ID_SIZE);
nodes_list[j].ip_port = close_clientlist[i].ip_port; nodes_list[j].ip_port = close_clientlist[i].ip_port;
@ -210,7 +205,7 @@ int replace_bad(Client_data * list, uint32_t length, uint8_t * client_id, IP_Por
uint32_t i; uint32_t i;
uint32_t temp_time = unix_time(); uint32_t temp_time = unix_time();
for(i = 0; i < length; ++i) for(i = 0; i < length; ++i)
if(list[i].timestamp + BAD_NODE_TIMEOUT < temp_time) /* if node is bad. */ { if(list[i].timestamp + BAD_NODE_TIMEOUT < temp_time) { /* if node is bad. */
memcpy(list[i].client_id, client_id, CLIENT_ID_SIZE); memcpy(list[i].client_id, client_id, CLIENT_ID_SIZE);
list[i].ip_port = ip_port; list[i].ip_port = ip_port;
list[i].timestamp = temp_time; list[i].timestamp = temp_time;
@ -704,12 +699,12 @@ void doDHTFriends()
for(i = 0; i < num_friends; ++i) { for(i = 0; i < num_friends; ++i) {
uint32_t num_nodes = 0; uint32_t num_nodes = 0;
for(j = 0; j < MAX_FRIEND_CLIENTS; ++j) 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].timestamp + Kill_NODE_TIMEOUT > temp_time) { /* if node is not dead. */
if((friends_list[i].client_list[j].last_pinged + PING_INTERVAL) <= temp_time) { 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); 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; 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; index[num_nodes] = j;
++num_nodes; ++num_nodes;
} }
@ -918,8 +913,7 @@ int handle_NATping(uint8_t * packet, uint32_t length, IP_Port source)
return 1; return 1;
} }
/* if request is not for us, try routing it. */ /* if request is not for us, try routing it. */
else else if(route_packet(packet + 1, packet, length) == length)
if(route_packet(packet + 1, packet, length) == length)
return 0; return 0;
return 0; return 0;
} }
@ -998,8 +992,7 @@ static void doNAT()
send_NATping(friends_list[i].client_id, friends_list[i].NATping_id, 0); /*0 is request*/ send_NATping(friends_list[i].client_id, friends_list[i].NATping_id, 0); /*0 is request*/
friends_list[i].NATping_timestamp = temp_time; friends_list[i].NATping_timestamp = temp_time;
} }
} } else if(friends_list[i].punching_timestamp + PUNCH_INTERVAL < temp_time) {
else if(friends_list[i].punching_timestamp + PUNCH_INTERVAL < temp_time) {
IP ip = NAT_commonip(ip_list, num, MAX_FRIEND_CLIENTS/2); IP ip = NAT_commonip(ip_list, num, MAX_FRIEND_CLIENTS/2);
if(ip.i == 0) if(ip.i == 0)
continue; continue;

View File

@ -95,7 +95,8 @@ static Connection connections[MAX_CONNECTIONS];
/* get connection id from IP_Port /* get connection id from IP_Port
return -1 if there are no connections like we are looking for return -1 if there are no connections like we are looking for
return id if it found it */ return id if it found it */
int getconnection_id(IP_Port ip_port) { int getconnection_id(IP_Port ip_port)
{
uint32_t i; 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 && if (connections[i].ip_port.ip.i == ip_port.ip.i &&
@ -684,8 +685,7 @@ void adjustRates()
if (sendqueue(i) != 0) { if (sendqueue(i) != 0) {
connections[i].data_rate = (BUFFER_PACKET_NUM - connections[i].num_req_paquets) * MAX_SYNC_RATE; connections[i].data_rate = (BUFFER_PACKET_NUM - connections[i].num_req_paquets) * MAX_SYNC_RATE;
connections[i].SYNC_rate = 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; connections[i].SYNC_rate = MAX_SYNC_RATE;
else else
connections[i].SYNC_rate = SYNC_RATE; connections[i].SYNC_rate = SYNC_RATE;

View File

@ -349,7 +349,8 @@ int initMessenger()
//TODO: make this function not suck. //TODO: make this function not suck.
static void doFriends() static void doFriends()
{/* TODO: add incoming connections and some other stuff. */ {
/* TODO: add incoming connections and some other stuff. */
uint32_t i; uint32_t i;
int len; int len;
uint8_t temp[MAX_DATA_SIZE]; uint8_t temp[MAX_DATA_SIZE];
@ -421,8 +422,7 @@ static void doFriends()
break; break;
} }
} }
} } else {
else {
if (is_cryptoconnected(friendlist[i].crypt_connection_id) == 4) { /* if the connection timed out, kill it */ if (is_cryptoconnected(friendlist[i].crypt_connection_id) == 4) { /* if the connection timed out, kill it */
crypto_kill(friendlist[i].crypt_connection_id); crypto_kill(friendlist[i].crypt_connection_id);
friendlist[i].crypt_connection_id = -1; friendlist[i].crypt_connection_id = -1;

View File

@ -61,7 +61,8 @@ static void (*handle_friendrequest)(uint8_t *, uint8_t *, uint16_t);
static uint8_t handle_friendrequest_isset = 0; static uint8_t handle_friendrequest_isset = 0;
/* set the function that will be executed when a friend request is received. */ /* 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 = function;
handle_friendrequest_isset = 1; handle_friendrequest_isset = 1;
} }
@ -76,7 +77,8 @@ static uint8_t recieved_requests[MAX_RECIEVED_STORED][crypto_box_PUBLICKEYBYTES]
static uint16_t recieved_requests_index; static uint16_t recieved_requests_index;
/*Add to list of recieved friend requests*/ /*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) if (recieved_requests_index >= MAX_RECIEVED_STORED)
recieved_requests_index = 0; recieved_requests_index = 0;
@ -86,7 +88,8 @@ static void addto_recievedlist(uint8_t * client_id) {
/* Check if a friend request was already recieved /* Check if a friend request was already recieved
return 0 if not, 1 if we did */ 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; uint32_t i;
for (i = 0; i < MAX_RECIEVED_STORED; ++i) { for (i = 0; i < MAX_RECIEVED_STORED; ++i) {
@ -98,7 +101,8 @@ static int request_recieved(uint8_t * client_id) {
} }
int friendreq_handlepacket(uint8_t * packet, uint32_t length, IP_Port source) { int friendreq_handlepacket(uint8_t * packet, uint32_t length, IP_Port source)
{
if (packet[0] == 32) { if (packet[0] == 32) {
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) length > MAX_DATA_SIZE + ENCRYPTION_PADDING)
@ -118,8 +122,7 @@ int friendreq_handlepacket(uint8_t * packet, uint32_t length, IP_Port source) {
addto_recievedlist(public_key); addto_recievedlist(public_key);
(*handle_friendrequest)(public_key, data, len); (*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) if(route_packet(packet + 1, packet, length) == length)
return 0; return 0;
} }

View File

@ -211,8 +211,7 @@ int handle_request(uint8_t *public_key, uint8_t *data, uint8_t *packet, uint16_t
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 && length <= MAX_DATA_SIZE + ENCRYPTION_PADDING &&
memcmp(packet + 1, self_public_key, crypto_box_PUBLICKEYBYTES) == 0) memcmp(packet + 1, self_public_key, crypto_box_PUBLICKEYBYTES) == 0) {
{
memcpy(public_key, packet + 1 + crypto_box_PUBLICKEYBYTES, crypto_box_PUBLICKEYBYTES); memcpy(public_key, packet + 1 + crypto_box_PUBLICKEYBYTES, crypto_box_PUBLICKEYBYTES);
uint8_t nonce[crypto_box_NONCEBYTES]; uint8_t nonce[crypto_box_NONCEBYTES];
memcpy(nonce, packet + 1 + crypto_box_PUBLICKEYBYTES * 2, crypto_box_NONCEBYTES); memcpy(nonce, packet + 1 + crypto_box_PUBLICKEYBYTES * 2, crypto_box_NONCEBYTES);
@ -221,8 +220,7 @@ int handle_request(uint8_t *public_key, uint8_t *data, uint8_t *packet, uint16_t
if(len1 == -1) if(len1 == -1)
return -1; return -1;
return len1; return len1;
} } else
else
return -1; return -1;
} }
@ -257,8 +255,7 @@ int handle_cryptohandshake(uint8_t *public_key, uint8_t *secret_nonce,
{ {
int pad = (- crypto_box_BOXZEROBYTES + crypto_box_ZEROBYTES); 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) + crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES + pad) {
{
return 0; return 0;
} }
if (data[0] != 2) if (data[0] != 2)
@ -317,8 +314,7 @@ int crypto_connect(uint8_t *public_key, IP_Port ip_port)
crypto_box_keypair(crypto_connections[i].sessionpublic_key, crypto_connections[i].sessionsecret_key); crypto_box_keypair(crypto_connections[i].sessionpublic_key, crypto_connections[i].sessionsecret_key);
if (send_cryptohandshake(id, public_key, crypto_connections[i].recv_nonce, if (send_cryptohandshake(id, public_key, crypto_connections[i].recv_nonce,
crypto_connections[i].sessionpublic_key) == 1) crypto_connections[i].sessionpublic_key) == 1) {
{
increment_nonce(crypto_connections[i].recv_nonce); increment_nonce(crypto_connections[i].recv_nonce);
return i; return i;
} }
@ -401,8 +397,7 @@ int accept_crypto_inbound(int connection_id, uint8_t *public_key, uint8_t *secre
crypto_box_keypair(crypto_connections[i].sessionpublic_key, crypto_connections[i].sessionsecret_key); 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) crypto_connections[i].sessionpublic_key) == 1) {
{
increment_nonce(crypto_connections[i].recv_nonce); increment_nonce(crypto_connections[i].recv_nonce);
uint32_t zero = 0; uint32_t zero = 0;
crypto_connections[i].status = 3; /* connection status needs to be 3 for write_cryptpacket() to work */ crypto_connections[i].status = 3; /* connection status needs to be 3 for write_cryptpacket() to work */
@ -504,8 +499,7 @@ static void receive_crypto()
crypto_connections[i].status = 2; /* set it to its proper value right after. */ crypto_connections[i].status = 2; /* set it to its proper value right after. */
} }
} }
} } 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); crypto_kill(crypto_connections[i].number);
} }
@ -524,11 +518,9 @@ static void receive_crypto()
/* connection is accepted so we disable the auto kill by setting it to about 1 month from now. */ /* connection is accepted so we disable the auto kill by setting it to about 1 month from now. */
kill_connection_in(crypto_connections[i].number, 3000000); kill_connection_in(crypto_connections[i].number, 3000000);
} } else
else
crypto_kill(crypto_connections[i].number); // This should not happen kill the connection if it does 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)
else if(id_packet(crypto_connections[i].number) != -1)
/* This should not happen /* This should not happen
kill the connection if it does */ kill the connection if it does */
crypto_kill(crypto_connections[i].number); crypto_kill(crypto_connections[i].number);