From 1bd91a520c49df16b391f0d6d68f6bc1c03c2225 Mon Sep 17 00:00:00 2001
From: Maxim Biro <nurupo.contributions@gmail.com>
Date: Sun, 14 Jul 2013 19:09:43 -0400
Subject: [PATCH] Updated Core

---
 src/core/DHT.c          | 45 ++++++++++++++++++++++++++---------------
 src/core/Lossless_UDP.c |  3 ++-
 src/core/Messenger.c    | 11 ++++++++--
 src/core/net_crypto.c   |  3 +++
 4 files changed, 43 insertions(+), 19 deletions(-)

diff --git a/src/core/DHT.c b/src/core/DHT.c
index edcc9a4..42869c4 100644
--- a/src/core/DHT.c
+++ b/src/core/DHT.c
@@ -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;
 
@@ -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
@@ -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)
         {
@@ -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;
+
     
 }
 
@@ -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;
-    
 }
 
 
@@ -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()
 {
@@ -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;
         }
     }
 }
diff --git a/src/core/Lossless_UDP.c b/src/core/Lossless_UDP.c
index 9d31a53..1f123ed 100644
--- a/src/core/Lossless_UDP.c
+++ b/src/core/Lossless_UDP.c
@@ -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;
         }
diff --git a/src/core/Messenger.c b/src/core/Messenger.c
index 2e381f8..94b4c74 100644
--- a/src/core/Messenger.c
+++ b/src/core/Messenger.c
@@ -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--)
@@ -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();
diff --git a/src/core/net_crypto.c b/src/core/net_crypto.c
index 2af5239..faeb6be 100644
--- a/src/core/net_crypto.c
+++ b/src/core/net_crypto.c
@@ -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
                 {