summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--core/DHT.c8
-rw-r--r--core/DHT.h2
-rw-r--r--core/network.c18
-rw-r--r--core/network.h6
-rw-r--r--docs/Lossless_UDP.txt2
-rw-r--r--testing/DHT_test.c6
6 files changed, 30 insertions, 12 deletions
diff --git a/core/DHT.c b/core/DHT.c
index d7299100..05d9d20c 100644
--- a/core/DHT.c
+++ b/core/DHT.c
@@ -713,10 +713,10 @@ void doFriends()
713 { 713 {
714 if(friends_list[i].client_list[j].timestamp + Kill_NODE_TIMEOUT > temp_time)//if node is not dead. 714 if(friends_list[i].client_list[j].timestamp + Kill_NODE_TIMEOUT > temp_time)//if node is not dead.
715 { 715 {
716 //TODO: Make this better, it only works if the function is called more than once per second. 716 if((friends_list[i].client_list[j].last_pinged + PING_INTERVAL) <= temp_time)
717 if((temp_time - friends_list[i].client_list[j].timestamp) % PING_INTERVAL == 0)
718 { 717 {
719 pingreq(friends_list[i].client_list[j].ip_port); 718 pingreq(friends_list[i].client_list[j].ip_port);
719 friends_list[i].client_list[j].last_pinged = temp_time;
720 } 720 }
721 if(friends_list[i].client_list[j].timestamp + BAD_NODE_TIMEOUT > temp_time)//if node is good. 721 if(friends_list[i].client_list[j].timestamp + BAD_NODE_TIMEOUT > temp_time)//if node is good.
722 { 722 {
@@ -751,10 +751,10 @@ void doClose()//tested
751 { 751 {
752 if(close_clientlist[i].timestamp + Kill_NODE_TIMEOUT > temp_time)//if node is not dead. 752 if(close_clientlist[i].timestamp + Kill_NODE_TIMEOUT > temp_time)//if node is not dead.
753 { 753 {
754 //TODO: Make this better, it only works if the function is called more than once per second. 754 if((close_clientlist[i].last_pinged + PING_INTERVAL) <= temp_time)
755 if((temp_time - close_clientlist[i].timestamp) % PING_INTERVAL == 0)
756 { 755 {
757 pingreq(close_clientlist[i].ip_port); 756 pingreq(close_clientlist[i].ip_port);
757 close_clientlist[i].last_pinged = temp_time;
758 } 758 }
759 if(close_clientlist[i].timestamp + BAD_NODE_TIMEOUT > temp_time)//if node is good. 759 if(close_clientlist[i].timestamp + BAD_NODE_TIMEOUT > temp_time)//if node is good.
760 { 760 {
diff --git a/core/DHT.h b/core/DHT.h
index a66e7bad..3238191c 100644
--- a/core/DHT.h
+++ b/core/DHT.h
@@ -39,7 +39,7 @@ typedef struct
39 char client_id[CLIENT_ID_SIZE]; 39 char client_id[CLIENT_ID_SIZE];
40 IP_Port ip_port; 40 IP_Port ip_port;
41 uint32_t timestamp; 41 uint32_t timestamp;
42 42 uint32_t last_pinged;
43}Client_data; 43}Client_data;
44//maximum number of clients stored per friend. 44//maximum number of clients stored per friend.
45#define MAX_FRIEND_CLIENTS 8 45#define MAX_FRIEND_CLIENTS 8
diff --git a/core/network.c b/core/network.c
index d5389bfa..70536abf 100644
--- a/core/network.c
+++ b/core/network.c
@@ -24,12 +24,26 @@
24 24
25#include "network.h" 25#include "network.h"
26 26
27
28//returns current time in milleseconds since the epoch.
29uint64_t current_time()
30{
31 uint64_t time;
32 #ifdef WIN32
33 //TODO: windows version
34 #else
35 struct timeval a;
36 gettimeofday(&a, NULL);
37 time = 1000000UL*a.tv_sec + a.tv_usec;
38 #endif
39 return time;
40
41}
42
27//our UDP socket, a global variable. 43//our UDP socket, a global variable.
28static int sock; 44static int sock;
29 45
30//Basic network functions: 46//Basic network functions:
31//TODO: put them somewhere else than here
32
33//Function to send packet(data) of length length to ip_port 47//Function to send packet(data) of length length to ip_port
34int sendpacket(IP_Port ip_port, char * data, uint32_t length) 48int sendpacket(IP_Port ip_port, char * data, uint32_t length)
35{ 49{
diff --git a/core/network.h b/core/network.h
index b4f374d8..a35ca214 100644
--- a/core/network.h
+++ b/core/network.h
@@ -32,6 +32,7 @@
32#include <string.h> 32#include <string.h>
33#include <time.h> 33#include <time.h>
34 34
35
35#ifdef WIN32 //Put win32 includes here 36#ifdef WIN32 //Put win32 includes here
36 37
37#include <winsock2.h> 38#include <winsock2.h>
@@ -43,7 +44,7 @@
43#include <sys/socket.h> 44#include <sys/socket.h>
44#include <netinet/in.h> 45#include <netinet/in.h>
45#include <errno.h> 46#include <errno.h>
46 47#include <sys/time.h>
47#endif 48#endif
48 49
49#define MAX_UDP_PACKET_SIZE 65507 50#define MAX_UDP_PACKET_SIZE 65507
@@ -75,7 +76,8 @@ typedef struct
75}ADDR; 76}ADDR;
76 77
77 78
78 79//returns current time in milleseconds since the epoch.
80uint64_t current_time();
79 81
80//Basic network functions: 82//Basic network functions:
81 83
diff --git a/docs/Lossless_UDP.txt b/docs/Lossless_UDP.txt
index 8454cfe2..54eede16 100644
--- a/docs/Lossless_UDP.txt
+++ b/docs/Lossless_UDP.txt
@@ -15,7 +15,7 @@ Lossless UDP:
15 Alice puts it in the handshake packet (handshake_id1). 15 Alice puts it in the handshake packet (handshake_id1).
16 Alice starts sending handshake packets to Bob (send 10 packets over 5 seconds if no response connection fails.) 16 Alice starts sending handshake packets to Bob (send 10 packets over 5 seconds if no response connection fails.)
17 Bob receives the packet. 17 Bob receives the packet.
18 Bob copies the handshake packet he got from alice but adds a random 4 byte number to it (handshake_id2) 18 Bob copies the handshake packet he got from alice but caternates a random 4 byte number to it (handshake_id2)
19 Alice receives the packet, checks if handshake_id1 matches the one she sent. 19 Alice receives the packet, checks if handshake_id1 matches the one she sent.
20 If it does she starts sending SYNC packets with sent_packetnum = handshake_id2 . 20 If it does she starts sending SYNC packets with sent_packetnum = handshake_id2 .
21 Bob receives the packet, 21 Bob receives the packet,
diff --git a/testing/DHT_test.c b/testing/DHT_test.c
index be2f0168..1eae74a0 100644
--- a/testing/DHT_test.c
+++ b/testing/DHT_test.c
@@ -40,7 +40,8 @@ void print_clientlist()
40 } 40 }
41 p_ip = close_clientlist[i].ip_port; 41 p_ip = close_clientlist[i].ip_port;
42 printf("\nIP: %u.%u.%u.%u Port: %u",p_ip.ip.c[0],p_ip.ip.c[1],p_ip.ip.c[2],p_ip.ip.c[3],ntohs(p_ip.port)); 42 printf("\nIP: %u.%u.%u.%u Port: %u",p_ip.ip.c[0],p_ip.ip.c[1],p_ip.ip.c[2],p_ip.ip.c[3],ntohs(p_ip.port));
43 printf("\nTimestamp: %u\n", close_clientlist[i].timestamp); 43 printf("\nTimestamp: %u", close_clientlist[i].timestamp);
44 printf("\nLast pinged: %u\n", close_clientlist[i].last_pinged);
44 } 45 }
45} 46}
46 47
@@ -73,7 +74,8 @@ void print_friendlist()
73 } 74 }
74 p_ip = friends_list[k].client_list[i].ip_port; 75 p_ip = friends_list[k].client_list[i].ip_port;
75 printf("\nIP: %u.%u.%u.%u:%u",p_ip.ip.c[0],p_ip.ip.c[1],p_ip.ip.c[2],p_ip.ip.c[3],ntohs(p_ip.port)); 76 printf("\nIP: %u.%u.%u.%u:%u",p_ip.ip.c[0],p_ip.ip.c[1],p_ip.ip.c[2],p_ip.ip.c[3],ntohs(p_ip.port));
76 printf("\nTimestamp: %u\n", friends_list[k].client_list[i].timestamp); 77 printf("\nTimestamp: %u", friends_list[k].client_list[i].timestamp);
78 printf("\nLast pinged: %u\n", friends_list[k].client_list[i].last_pinged);
77 } 79 }
78 } 80 }
79} 81}