diff options
-rw-r--r-- | CMakeLists.txt | 2 | ||||
-rw-r--r-- | core/DHT.c | 65 | ||||
-rw-r--r-- | core/DHT.h | 62 | ||||
-rw-r--r-- | testing/DHT_cryptosendfiletest.c | 10 | ||||
-rw-r--r-- | testing/DHT_sendfiletest.c | 12 | ||||
-rw-r--r-- | testing/DHT_test.c | 10 |
6 files changed, 83 insertions, 78 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 388e6d91..47fca42b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt | |||
@@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 2.6.0) | |||
2 | project(TOX C) | 2 | project(TOX C) |
3 | 3 | ||
4 | set(core_sources | 4 | set(core_sources |
5 | core/DHT.c | 5 | #core/DHT.c |
6 | core/network.c) | 6 | core/network.c) |
7 | 7 | ||
8 | set(test_sources | 8 | set(test_sources |
@@ -27,8 +27,65 @@ | |||
27 | 27 | ||
28 | #include "DHT.h" | 28 | #include "DHT.h" |
29 | 29 | ||
30 | |||
31 | typedef struct | ||
32 | { | ||
33 | uint8_t client_id[CLIENT_ID_SIZE]; | ||
34 | IP_Port ip_port; | ||
35 | uint32_t timestamp; | ||
36 | uint32_t last_pinged; | ||
37 | }Client_data; | ||
38 | //maximum number of clients stored per friend. | ||
39 | #define MAX_FRIEND_CLIENTS 8 | ||
40 | typedef struct | ||
41 | { | ||
42 | uint8_t client_id[CLIENT_ID_SIZE]; | ||
43 | Client_data client_list[MAX_FRIEND_CLIENTS]; | ||
44 | |||
45 | }Friend; | ||
46 | |||
47 | |||
48 | typedef struct | ||
49 | { | ||
50 | uint8_t client_id[CLIENT_ID_SIZE]; | ||
51 | IP_Port ip_port; | ||
52 | }Node_format; | ||
53 | |||
54 | typedef struct | ||
55 | { | ||
56 | IP_Port ip_port; | ||
57 | uint32_t ping_id; | ||
58 | uint32_t timestamp; | ||
59 | |||
60 | }Pinged; | ||
61 | |||
62 | |||
30 | uint8_t self_client_id[CLIENT_ID_SIZE]; | 63 | uint8_t self_client_id[CLIENT_ID_SIZE]; |
31 | 64 | ||
65 | |||
66 | //TODO: Move these out of here and put them into the .c file. | ||
67 | //A list of the clients mathematically closest to ours. | ||
68 | #define LCLIENT_LIST 32 | ||
69 | Client_data close_clientlist[LCLIENT_LIST]; | ||
70 | |||
71 | |||
72 | //Hard maximum number of friends | ||
73 | #define MAX_FRIENDS 256 | ||
74 | |||
75 | //Let's start with a static array for testing. | ||
76 | Friend friends_list[MAX_FRIENDS]; | ||
77 | uint16_t num_friends; | ||
78 | |||
79 | //The list of ip ports along with the ping_id of what we sent them and a timestamp | ||
80 | #define LPING_ARRAY 128 | ||
81 | |||
82 | Pinged pings[LPING_ARRAY]; | ||
83 | |||
84 | #define LSEND_NODES_ARRAY LPING_ARRAY/2 | ||
85 | |||
86 | Pinged send_nodes[LSEND_NODES_ARRAY]; | ||
87 | |||
88 | |||
32 | //Compares client_id1 and client_id2 with client_id | 89 | //Compares client_id1 and client_id2 with client_id |
33 | //return 0 if both are same distance | 90 | //return 0 if both are same distance |
34 | //return 1 if client_id1 is closer. | 91 | //return 1 if client_id1 is closer. |
@@ -593,7 +650,7 @@ int handle_sendnodes(uint8_t * packet, uint32_t length, IP_Port source)//tested | |||
593 | 650 | ||
594 | 651 | ||
595 | 652 | ||
596 | int addfriend(uint8_t * client_id) | 653 | int DHT_addfriend(uint8_t * client_id) |
597 | { | 654 | { |
598 | //TODO:Maybe make the array of friends dynamic instead of a static array with MAX_FRIENDS | 655 | //TODO:Maybe make the array of friends dynamic instead of a static array with MAX_FRIENDS |
599 | if(MAX_FRIENDS > num_friends) | 656 | if(MAX_FRIENDS > num_friends) |
@@ -610,7 +667,7 @@ int addfriend(uint8_t * client_id) | |||
610 | 667 | ||
611 | 668 | ||
612 | 669 | ||
613 | int delfriend(uint8_t * client_id) | 670 | int DHT_delfriend(uint8_t * client_id) |
614 | { | 671 | { |
615 | uint32_t i; | 672 | uint32_t i; |
616 | for(i = 0; i < num_friends; i++) | 673 | for(i = 0; i < num_friends; i++) |
@@ -630,7 +687,7 @@ int delfriend(uint8_t * client_id) | |||
630 | 687 | ||
631 | 688 | ||
632 | //TODO: Optimize this. | 689 | //TODO: Optimize this. |
633 | IP_Port getfriendip(uint8_t * client_id) | 690 | IP_Port DHT_getfriendip(uint8_t * client_id) |
634 | { | 691 | { |
635 | uint32_t i, j; | 692 | uint32_t i, j; |
636 | IP_Port empty = {{{0}}, 0}; | 693 | IP_Port empty = {{{0}}, 0}; |
@@ -785,7 +842,7 @@ void doDHT() | |||
785 | 842 | ||
786 | 843 | ||
787 | 844 | ||
788 | void bootstrap(IP_Port ip_port) | 845 | void DHT_bootstrap(IP_Port ip_port) |
789 | { | 846 | { |
790 | 847 | ||
791 | getnodes(ip_port, self_client_id); | 848 | getnodes(ip_port, self_client_id); |
@@ -34,49 +34,19 @@ | |||
34 | //size of the client_id in bytes | 34 | //size of the client_id in bytes |
35 | #define CLIENT_ID_SIZE 32 | 35 | #define CLIENT_ID_SIZE 32 |
36 | 36 | ||
37 | typedef struct | ||
38 | { | ||
39 | uint8_t client_id[CLIENT_ID_SIZE]; | ||
40 | IP_Port ip_port; | ||
41 | uint32_t timestamp; | ||
42 | uint32_t last_pinged; | ||
43 | }Client_data; | ||
44 | //maximum number of clients stored per friend. | ||
45 | #define MAX_FRIEND_CLIENTS 8 | ||
46 | typedef struct | ||
47 | { | ||
48 | uint8_t client_id[CLIENT_ID_SIZE]; | ||
49 | Client_data client_list[MAX_FRIEND_CLIENTS]; | ||
50 | |||
51 | }Friend; | ||
52 | |||
53 | |||
54 | typedef struct | ||
55 | { | ||
56 | uint8_t client_id[CLIENT_ID_SIZE]; | ||
57 | IP_Port ip_port; | ||
58 | }Node_format; | ||
59 | |||
60 | typedef struct | ||
61 | { | ||
62 | IP_Port ip_port; | ||
63 | uint32_t ping_id; | ||
64 | uint32_t timestamp; | ||
65 | |||
66 | }Pinged; | ||
67 | 37 | ||
68 | 38 | ||
69 | //Add a new friend to the friends list | 39 | //Add a new friend to the friends list |
70 | //client_id must be CLIENT_ID_SIZE bytes long. | 40 | //client_id must be CLIENT_ID_SIZE bytes long. |
71 | //returns 0 if success | 41 | //returns 0 if success |
72 | //returns 1 if failure (friends list is full) | 42 | //returns 1 if failure (friends list is full) |
73 | int addfriend(uint8_t * client_id); | 43 | int DHT_addfriend(uint8_t * client_id); |
74 | 44 | ||
75 | //Delete a friend from the friends list | 45 | //Delete a friend from the friends list |
76 | //client_id must be CLIENT_ID_SIZE bytes long. | 46 | //client_id must be CLIENT_ID_SIZE bytes long. |
77 | //returns 0 if success | 47 | //returns 0 if success |
78 | //returns 1 if failure (client_id not in friends list) | 48 | //returns 1 if failure (client_id not in friends list) |
79 | int delfriend(uint8_t * client_id); | 49 | int DHT_delfriend(uint8_t * client_id); |
80 | 50 | ||
81 | 51 | ||
82 | //Get ip of friend | 52 | //Get ip of friend |
@@ -86,7 +56,7 @@ int delfriend(uint8_t * client_id); | |||
86 | //returns ip if success | 56 | //returns ip if success |
87 | //returns ip of 0 if failure (This means the friend is either offline or we have not found him yet.) | 57 | //returns ip of 0 if failure (This means the friend is either offline or we have not found him yet.) |
88 | //returns ip of 1 if friend is not in list. | 58 | //returns ip of 1 if friend is not in list. |
89 | IP_Port getfriendip(uint8_t * client_id); | 59 | IP_Port DHT_getfriendip(uint8_t * client_id); |
90 | 60 | ||
91 | 61 | ||
92 | //Run this function at least a couple times per second (It's the main loop) | 62 | //Run this function at least a couple times per second (It's the main loop) |
@@ -99,7 +69,7 @@ int DHT_handlepacket(uint8_t * packet, uint32_t length, IP_Port source); | |||
99 | 69 | ||
100 | //Use this function to bootstrap the client | 70 | //Use this function to bootstrap the client |
101 | //Sends a get nodes request to the given ip port | 71 | //Sends a get nodes request to the given ip port |
102 | void bootstrap(IP_Port ip_port); | 72 | void DHT_bootstrap(IP_Port ip_port); |
103 | 73 | ||
104 | 74 | ||
105 | //TODO: | 75 | //TODO: |
@@ -112,27 +82,5 @@ void bootstrap(IP_Port ip_port); | |||
112 | extern uint8_t self_client_id[CLIENT_ID_SIZE]; | 82 | extern uint8_t self_client_id[CLIENT_ID_SIZE]; |
113 | 83 | ||
114 | 84 | ||
115 | //TODO: Move these out of here and put them into the .c file. | ||
116 | //A list of the clients mathematically closest to ours. | ||
117 | #define LCLIENT_LIST 32 | ||
118 | Client_data close_clientlist[LCLIENT_LIST]; | ||
119 | |||
120 | |||
121 | //Hard maximum number of friends | ||
122 | #define MAX_FRIENDS 256 | ||
123 | |||
124 | //Let's start with a static array for testing. | ||
125 | Friend friends_list[MAX_FRIENDS]; | ||
126 | uint16_t num_friends; | ||
127 | |||
128 | //The list of ip ports along with the ping_id of what we sent them and a timestamp | ||
129 | #define LPING_ARRAY 128 | ||
130 | |||
131 | Pinged pings[LPING_ARRAY]; | ||
132 | |||
133 | #define LSEND_NODES_ARRAY LPING_ARRAY/2 | ||
134 | |||
135 | Pinged send_nodes[LSEND_NODES_ARRAY]; | ||
136 | |||
137 | 85 | ||
138 | #endif \ No newline at end of file | 86 | #endif |
diff --git a/testing/DHT_cryptosendfiletest.c b/testing/DHT_cryptosendfiletest.c index 84d777cc..09683a45 100644 --- a/testing/DHT_cryptosendfiletest.c +++ b/testing/DHT_cryptosendfiletest.c | |||
@@ -90,7 +90,7 @@ int main(int argc, char *argv[]) | |||
90 | //memcpy(self_client_id, "qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq", 32); | 90 | //memcpy(self_client_id, "qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq", 32); |
91 | 91 | ||
92 | 92 | ||
93 | addfriend(friend_id); | 93 | DHT_addfriend(friend_id); |
94 | IP_Port friend_ip; | 94 | IP_Port friend_ip; |
95 | int connection = -1; | 95 | int connection = -1; |
96 | int inconnection = -1; | 96 | int inconnection = -1; |
@@ -112,7 +112,7 @@ int main(int argc, char *argv[]) | |||
112 | IP_Port bootstrap_ip_port; | 112 | IP_Port bootstrap_ip_port; |
113 | bootstrap_ip_port.port = htons(atoi(argv[2])); | 113 | bootstrap_ip_port.port = htons(atoi(argv[2])); |
114 | bootstrap_ip_port.ip.i = inet_addr(argv[1]); | 114 | bootstrap_ip_port.ip.i = inet_addr(argv[1]); |
115 | bootstrap(bootstrap_ip_port); | 115 | DHT_bootstrap(bootstrap_ip_port); |
116 | 116 | ||
117 | IP_Port ip_port; | 117 | IP_Port ip_port; |
118 | uint8_t data[MAX_UDP_PACKET_SIZE]; | 118 | uint8_t data[MAX_UDP_PACKET_SIZE]; |
@@ -146,21 +146,21 @@ int main(int argc, char *argv[]) | |||
146 | } | 146 | } |
147 | } | 147 | } |
148 | } | 148 | } |
149 | friend_ip = getfriendip((uint8_t *)friend_id); | 149 | friend_ip = DHT_getfriendip(friend_id); |
150 | if(friend_ip.ip.i != 0) | 150 | if(friend_ip.ip.i != 0) |
151 | { | 151 | { |
152 | if(connection == -1 && friendrequest == -1) | 152 | if(connection == -1 && friendrequest == -1) |
153 | { | 153 | { |
154 | printf("Sending friend request to peer:"); | 154 | printf("Sending friend request to peer:"); |
155 | printip(friend_ip); | 155 | printip(friend_ip); |
156 | friendrequest = send_friendrequest(friend_id, friend_ip, "Hello World", 12); | 156 | friendrequest = send_friendrequest(friend_id, friend_ip,(uint8_t *) "Hello World", 12); |
157 | //connection = crypto_connect((uint8_t *)friend_id, friend_ip); | 157 | //connection = crypto_connect((uint8_t *)friend_id, friend_ip); |
158 | //connection = new_connection(friend_ip); | 158 | //connection = new_connection(friend_ip); |
159 | } | 159 | } |
160 | if(check_friendrequest(friendrequest) == 1) | 160 | if(check_friendrequest(friendrequest) == 1) |
161 | { | 161 | { |
162 | printf("Started connecting to friend:"); | 162 | printf("Started connecting to friend:"); |
163 | connection = crypto_connect((uint8_t *)friend_id, friend_ip); | 163 | connection = crypto_connect(friend_id, friend_ip); |
164 | } | 164 | } |
165 | } | 165 | } |
166 | if(inconnection == -1) | 166 | if(inconnection == -1) |
diff --git a/testing/DHT_sendfiletest.c b/testing/DHT_sendfiletest.c index fd4fd29f..9a455056 100644 --- a/testing/DHT_sendfiletest.c +++ b/testing/DHT_sendfiletest.c | |||
@@ -48,7 +48,7 @@ int main(int argc, char *argv[]) | |||
48 | printf("usage %s ip port client_id(of friend to find ip_port of) filename(of file to send) client_id(ours)\n", argv[0]); | 48 | printf("usage %s ip port client_id(of friend to find ip_port of) filename(of file to send) client_id(ours)\n", argv[0]); |
49 | exit(0); | 49 | exit(0); |
50 | } | 50 | } |
51 | addfriend(argv[3]); | 51 | DHT_addfriend((uint8_t *)argv[3]); |
52 | IP_Port friend_ip; | 52 | IP_Port friend_ip; |
53 | int connection = -1; | 53 | int connection = -1; |
54 | int inconnection = -1; | 54 | int inconnection = -1; |
@@ -66,15 +66,15 @@ int main(int argc, char *argv[]) | |||
66 | IP_Port bootstrap_ip_port; | 66 | IP_Port bootstrap_ip_port; |
67 | bootstrap_ip_port.port = htons(atoi(argv[2])); | 67 | bootstrap_ip_port.port = htons(atoi(argv[2])); |
68 | bootstrap_ip_port.ip.i = inet_addr(argv[1]); | 68 | bootstrap_ip_port.ip.i = inet_addr(argv[1]); |
69 | bootstrap(bootstrap_ip_port); | 69 | DHT_bootstrap(bootstrap_ip_port); |
70 | 70 | ||
71 | IP_Port ip_port; | 71 | IP_Port ip_port; |
72 | char data[MAX_UDP_PACKET_SIZE]; | 72 | uint8_t data[MAX_UDP_PACKET_SIZE]; |
73 | uint32_t length; | 73 | uint32_t length; |
74 | 74 | ||
75 | char buffer1[128]; | 75 | uint8_t buffer1[128]; |
76 | int read1 = 0; | 76 | int read1 = 0; |
77 | char buffer2[128]; | 77 | uint8_t buffer2[128]; |
78 | int read2 = 0; | 78 | int read2 = 0; |
79 | FILE *file1 = fopen(argv[4], "rb"); | 79 | FILE *file1 = fopen(argv[4], "rb"); |
80 | if ( file1==NULL ){printf("Error opening file.\n");return 1;} | 80 | if ( file1==NULL ){printf("Error opening file.\n");return 1;} |
@@ -100,7 +100,7 @@ int main(int argc, char *argv[]) | |||
100 | } | 100 | } |
101 | } | 101 | } |
102 | } | 102 | } |
103 | friend_ip = getfriendip(argv[3]); | 103 | friend_ip = DHT_getfriendip((uint8_t *)argv[3]); |
104 | if(friend_ip.ip.i != 0) | 104 | if(friend_ip.ip.i != 0) |
105 | { | 105 | { |
106 | if(connection == -1) | 106 | if(connection == -1) |
diff --git a/testing/DHT_test.c b/testing/DHT_test.c index 9100c01a..25bf869e 100644 --- a/testing/DHT_test.c +++ b/testing/DHT_test.c | |||
@@ -1,13 +1,13 @@ | |||
1 | /* DHT test | 1 | /* DHT test |
2 | * A file with a main that runs our DHT for testing. | 2 | * A file with a main that runs our DHT for testing. |
3 | * | 3 | * |
4 | * Compile with: gcc -O2 -Wall -o test ../core/DHT.c ../core/network.c DHT_test.c | 4 | * Compile with: gcc -O2 -Wall -o test ../core/network.c DHT_test.c |
5 | * | 5 | * |
6 | * Command line arguments are the ip and port of a node and the client_id (32 bytes) of the friend you want to find the ip_port of | 6 | * Command line arguments are the ip and port of a node and the client_id (32 bytes) of the friend you want to find the ip_port of |
7 | * EX: ./test 127.0.0.1 33445 ABCDEFGHIJKLMNOPQRSTUVWXYZabcdef | 7 | * EX: ./test 127.0.0.1 33445 ABCDEFGHIJKLMNOPQRSTUVWXYZabcdef |
8 | */ | 8 | */ |
9 | #include "../core/network.h" | 9 | #include "../core/network.h" |
10 | #include "../core/DHT.h" | 10 | #include "../core/DHT.c" |
11 | 11 | ||
12 | #include <string.h> | 12 | #include <string.h> |
13 | 13 | ||
@@ -58,7 +58,7 @@ void print_friendlist() | |||
58 | { | 58 | { |
59 | printf("%c", friends_list[k].client_id[j]); | 59 | printf("%c", friends_list[k].client_id[j]); |
60 | } | 60 | } |
61 | p_ip = getfriendip(friends_list[k].client_id); | 61 | p_ip = DHT_getfriendip(friends_list[k].client_id); |
62 | 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)); | 62 | 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)); |
63 | 63 | ||
64 | printf("\nCLIENTS IN LIST:\n\n"); | 64 | printf("\nCLIENTS IN LIST:\n\n"); |
@@ -102,7 +102,7 @@ int main(int argc, char *argv[]) | |||
102 | printf("usage %s ip port client_id(of friend to find ip_port of)\n", argv[0]); | 102 | printf("usage %s ip port client_id(of friend to find ip_port of)\n", argv[0]); |
103 | exit(0); | 103 | exit(0); |
104 | } | 104 | } |
105 | addfriend((uint8_t *)argv[3]); | 105 | DHT_addfriend((uint8_t *)argv[3]); |
106 | 106 | ||
107 | //initialize networking | 107 | //initialize networking |
108 | //bind to ip 0.0.0.0:PORT | 108 | //bind to ip 0.0.0.0:PORT |
@@ -122,7 +122,7 @@ int main(int argc, char *argv[]) | |||
122 | //bootstrap_ip_port.ip.c[2] = 0; | 122 | //bootstrap_ip_port.ip.c[2] = 0; |
123 | //bootstrap_ip_port.ip.c[3] = 1; | 123 | //bootstrap_ip_port.ip.c[3] = 1; |
124 | bootstrap_ip_port.ip.i = inet_addr(argv[1]); | 124 | bootstrap_ip_port.ip.i = inet_addr(argv[1]); |
125 | bootstrap(bootstrap_ip_port); | 125 | DHT_bootstrap(bootstrap_ip_port); |
126 | 126 | ||
127 | IP_Port ip_port; | 127 | IP_Port ip_port; |
128 | uint8_t data[MAX_UDP_PACKET_SIZE]; | 128 | uint8_t data[MAX_UDP_PACKET_SIZE]; |