Skip to content
This repository has been archived by the owner on Jun 11, 2020. It is now read-only.

Commit

Permalink
Updated Core
Browse files Browse the repository at this point in the history
  • Loading branch information
nurupo committed Jul 14, 2013
1 parent 3f9575c commit 1bd91a5
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 19 deletions.
45 changes: 29 additions & 16 deletions src/core/DHT.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ 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.

}Friend;

Expand Down Expand Up @@ -69,11 +70,8 @@ uint8_t self_secret_key[crypto_box_SECRETKEYBYTES];
static Client_data close_clientlist[LCLIENT_LIST];


//Hard maximum number of friends
#define MAX_FRIENDS 256

//Let's start with a static array for testing.
static Friend friends_list[MAX_FRIENDS];
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
Expand Down Expand Up @@ -370,7 +368,7 @@ int is_gettingnodes(IP_Port ip_port, uint64_t ping_id)
uint8_t pinging;
uint32_t temp_time = unix_time();

for(i = 0; i < LPING_ARRAY; i++ )
for(i = 0; i < LSEND_NODES_ARRAY; i++ )
{
if((send_nodes[i].timestamp + PING_TIMEOUT) > temp_time)
{
Expand Down Expand Up @@ -765,14 +763,26 @@ int handle_sendnodes(uint8_t * packet, uint32_t length, IP_Port source)

int DHT_addfriend(uint8_t * client_id)
{
//TODO:Maybe make the array of friends dynamic instead of a static array with MAX_FRIENDS
if(MAX_FRIENDS > num_friends)
Friend * temp;
if(num_friends == 0)
{
memcpy(friends_list[num_friends].client_id, client_id, CLIENT_ID_SIZE);
num_friends++;
return 0;
temp = malloc(sizeof(Friend));
}
if(num_friends > 0)
{
temp = realloc(friends_list, sizeof(Friend) * (num_friends + 1));
}
if(temp == NULL)
{
return 1;
}
return 1;

friends_list = temp;
memset(&friends_list[num_friends], 0, sizeof(Friend));
memcpy(friends_list[num_friends].client_id, client_id, CLIENT_ID_SIZE);
num_friends++;
return 0;


}

Expand All @@ -783,17 +793,22 @@ int DHT_addfriend(uint8_t * client_id)
int DHT_delfriend(uint8_t * client_id)
{
uint32_t i;
Friend * temp;
for(i = 0; i < num_friends; i++)
{
if(memcmp(friends_list[i].client_id, client_id, CLIENT_ID_SIZE) == 0)//Equal
{
memcpy(friends_list[num_friends].client_id, friends_list[i].client_id, CLIENT_ID_SIZE);
num_friends--;
temp = realloc(friends_list, sizeof(friends_list) * (num_friends));
if(temp != NULL)
{
friends_list = temp;
}
return 0;
}
}
return 1;

}


Expand Down Expand Up @@ -866,8 +881,6 @@ int DHT_handlepacket(uint8_t * packet, uint32_t length, IP_Port source)
//Ping each client in the "friends" list every 60 seconds.
//Send a get nodes request every 20 seconds to a random good node for each "friend" in our "friends" list.

static uint32_t friend_lastgetnode[MAX_FRIENDS];


void doDHTFriends()
{
Expand Down Expand Up @@ -895,13 +908,13 @@ void doDHTFriends()
}
}
}
if(friend_lastgetnode[i] + 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,
friends_list[i].client_id);
friend_lastgetnode[i] = temp_time;
friends_list[i].lastgetnode = temp_time;
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/core/Lossless_UDP.c
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,8 @@ int new_inconnection(IP_Port ip_port)
connections[i].SYNC_rate = SYNC_RATE;
connections[i].data_rate = DATA_SYNC_RATE;
connections[i].last_recv = current_time();
connections[i].killat = ~0;
//if this connection isn't handled within 5 seconds, kill it
connections[i].killat = current_time() + 1000000UL*CONNEXION_TIMEOUT;
connections[i].send_counter = 127;
return i;
}
Expand Down
11 changes: 9 additions & 2 deletions src/core/Messenger.c
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ int m_delfriend(int friendnumber)
}

DHT_delfriend(friendlist[friendnumber].client_id);
crypto_kill(friendlist[friendnumber].crypt_connection_id);
memset(&friendlist[friendnumber], 0, sizeof(Friend));
uint32_t i;
for(i = numfriends; i != 0; i--)
Expand Down Expand Up @@ -341,18 +342,24 @@ void doMessenger()
uint32_t length;
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 packet is discarded
//printf("Received unhandled packet with length: %u\n", length);
printf("Received unhandled packet with length: %u\n", length);
}
else
{
//printf("Received handled packet with length: %u\n", length);
printf("Received handled packet with length: %u\n", length);
}
//}
#else
DHT_handlepacket(data, length, ip_port);
LosslessUDP_handlepacket(data, length, ip_port);
#endif

}
doDHT();
doLossless_UDP();
Expand Down
3 changes: 3 additions & 0 deletions src/core/net_crypto.c
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,9 @@ static void receive_crypto()
{
increment_nonce(crypto_connections[i].recv_nonce);
crypto_connections[i].status = 3;

//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);
}
else
{
Expand Down

0 comments on commit 1bd91a5

Please sign in to comment.