summaryrefslogtreecommitdiff
path: root/testing
diff options
context:
space:
mode:
Diffstat (limited to 'testing')
-rw-r--r--testing/DHT_cryptosendfiletest.c251
-rw-r--r--testing/DHT_test.c6
2 files changed, 254 insertions, 3 deletions
diff --git a/testing/DHT_cryptosendfiletest.c b/testing/DHT_cryptosendfiletest.c
new file mode 100644
index 00000000..787e279e
--- /dev/null
+++ b/testing/DHT_cryptosendfiletest.c
@@ -0,0 +1,251 @@
1/* DHT cryptosendfiletest
2 *
3 * This program sends or recieves a friend request.
4 *
5 * it also sends the encrypted data from a file to another client.
6 * Receives the file data that that client sends us.
7 *
8 * NOTE: this program simulates 33% packet loss.
9 *
10 * This is how I compile it: gcc -O2 -Wall -o test ../core/Lossless_UDP.c ../core/network.c ../core/net_crypto.c ../core/DHT.c ../nacl/build/Linux/lib/amd64/* DHT_cryptosendfiletest.c
11*
12 *
13 * Command line arguments are the ip and port of a node (for bootstrapping).
14 *
15 * Saves all received data to: received.txt
16 *
17 * EX: ./test 127.0.0.1 33445 filename.txt
18 */
19#include "../core/network.h"
20#include "../core/DHT.h"
21#include "../core/net_crypto.h"
22
23#include <string.h>
24
25//Sleep function (x = milliseconds)
26#ifdef WIN32
27
28#define c_sleep(x) Sleep(1*x)
29
30#else
31#include <unistd.h>
32#include <arpa/inet.h>
33#define c_sleep(x) usleep(1000*x)
34
35#endif
36
37#define PORT 33445
38
39void printip(IP_Port ip_port)
40{
41 printf("\nIP: %u.%u.%u.%u Port: %u\n",ip_port.ip.c[0],ip_port.ip.c[1],ip_port.ip.c[2],ip_port.ip.c[3],ntohs(ip_port.port));
42}
43
44
45//horrible function from one of my first C programs.
46//only here because I was too lazy to write a proper one.
47unsigned char * hex_string_to_bin(char hex_string[])
48{
49 unsigned char * val = malloc(strlen(hex_string));
50 char * pos = hex_string;
51 int i=0;
52 while(i < strlen(hex_string))
53 {
54 sscanf(pos,"%2hhx",&val[i]);
55 pos+=2;
56 i++;
57 }
58 return val;
59}
60
61uint8_t self_public_key[crypto_box_PUBLICKEYBYTES];
62
63int main(int argc, char *argv[])
64{
65 if (argc < 4) {
66 printf("usage %s ip port filename(of file to send)\n", argv[0]);
67 exit(0);
68 }
69 new_keys();
70 printf("OUR ID: ");
71 uint32_t i;
72 for(i = 0; i < 32; i++)
73 {
74 if(self_public_key[i] < 16)
75 printf("0");
76 printf("%hhX",self_public_key[i]);
77 }
78 printf("\n");
79
80 memcpy(self_client_id, self_public_key, 32);
81
82 char temp_id[128];
83 printf("Enter the client_id of the friend to connect to (32 bytes HEX format):\n");
84 scanf("%s", temp_id);
85
86 uint8_t friend_id[32];
87 memcpy(friend_id, hex_string_to_bin(temp_id), 32);
88
89
90 //memcpy(self_client_id, "qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq", 32);
91
92
93 addfriend(friend_id);
94 IP_Port friend_ip;
95 int connection = -1;
96 int inconnection = -1;
97
98 uint8_t acceptedfriend_public_key[crypto_box_PUBLICKEYBYTES];
99 int friendrequest = -1;
100 uint8_t request_data[512];
101
102 //initialize networking
103 //bind to ip 0.0.0.0:PORT
104 IP ip;
105 ip.i = 0;
106 init_networking(ip, PORT);
107 initNetCrypto();
108
109
110
111 perror("Initialization");
112 IP_Port bootstrap_ip_port;
113 bootstrap_ip_port.port = htons(atoi(argv[2]));
114 bootstrap_ip_port.ip.i = inet_addr(argv[1]);
115 bootstrap(bootstrap_ip_port);
116
117 IP_Port ip_port;
118 uint8_t data[MAX_UDP_PACKET_SIZE];
119 uint32_t length;
120
121 uint8_t buffer1[128];
122 int read1 = 0;
123 uint8_t buffer2[128];
124 int read2 = 0;
125 FILE *file1 = fopen(argv[3], "rb");
126 if ( file1==NULL ){printf("Error opening file.\n");return 1;}
127 FILE *file2 = fopen("received.txt", "wb");
128 if ( file2==NULL ){return 1;}
129 read1 = fread(buffer1, 1, 128, file1);
130
131 while(1)
132 {
133
134 while(recievepacket(&ip_port, data, &length) != -1)
135 {
136 if(rand() % 3 != 1)//simulate packet loss
137 {
138 if(DHT_handlepacket(data, length, ip_port) && LosslessUDP_handlepacket(data, length, ip_port))
139 {
140 //if packet is not recognized
141 printf("Received unhandled packet with length: %u\n", length);
142 }
143 else
144 {
145 printf("Received handled packet with length: %u\n", length);
146 }
147 }
148 }
149 friend_ip = getfriendip((uint8_t *)friend_id);
150 if(friend_ip.ip.i != 0)
151 {
152 if(connection == -1 && friendrequest == -1)
153 {
154 printf("Sending friend request to peer:");
155 printip(friend_ip);
156 friendrequest = send_friendrequest(friend_id, friend_ip, "Hello World", 12);
157 //connection = crypto_connect((uint8_t *)friend_id, friend_ip);
158 //connection = new_connection(friend_ip);
159 }
160 if(check_friendrequest(friendrequest) == 1)
161 {
162 printf("Started connecting to friend:");
163 connection = crypto_connect((uint8_t *)friend_id, friend_ip);
164 }
165 }
166 if(inconnection == -1)
167 {
168 uint8_t secret_nonce[crypto_box_NONCEBYTES];
169 uint8_t public_key[crypto_box_PUBLICKEYBYTES];
170 inconnection = crypto_inbound(public_key, secret_nonce);
171 inconnection = accept_crypto_inbound(inconnection, acceptedfriend_public_key, secret_nonce);
172 //inconnection = incoming_connection();
173 if(inconnection != -1)
174 {
175 printf("Someone connected to us:\n");
176 // printip(connection_ip(inconnection));
177 }
178 }
179 if(handle_friendrequest(acceptedfriend_public_key, request_data) > 1)
180 {
181 printf("RECIEVED FRIEND REQUEST: %s\n", request_data);
182 }
183
184 //if someone connected to us write what he sends to a file
185 //also send him our file.
186 if(inconnection != -1)
187 {
188 if(write_cryptpacket(inconnection, buffer1, read1))
189 {
190 printf("Wrote data1.\n");
191 read1 = fread(buffer1, 1, 128, file1);
192 }
193 read2 = read_cryptpacket(inconnection, buffer2);
194 if(read2 != 0)
195 {
196 printf("Received data1.\n");
197 if(!fwrite(buffer2, read2, 1, file2))
198 {
199 printf("file write error1\n");
200 }
201 if(read2 < 128)
202 {
203 printf("Closed file1 %u\n", read2);
204 fclose(file2);
205 }
206 }
207 else if(is_cryptoconnected(inconnection) == 4)//if buffer is empty and the connection timed out.
208 {
209 crypto_kill(inconnection);
210 }
211 }
212 //if we are connected to a friend send him data from the file.
213 //also put what he sends us in a file.
214 if(is_cryptoconnected(connection) >= 3)
215 {
216 if(write_cryptpacket(0, buffer1, read1))
217 {
218 printf("Wrote data2.\n");
219 read1 = fread(buffer1, 1, 128, file1);
220 }
221 read2 = read_cryptpacket(0, buffer2);
222 if(read2 != 0)
223 {
224 printf("Received data2.\n");
225 if(!fwrite(buffer2, read2, 1, file2))
226 {
227 printf("file write error2\n");
228 }
229 if(read2 < 128)
230 {
231 printf("Closed file2 %u\n", read2);
232 fclose(file2);
233 }
234 }
235 else if(is_cryptoconnected(connection) == 4)//if buffer is empty and the connection timed out.
236 {
237 crypto_kill(connection);
238 }
239 }
240 doDHT();
241 doLossless_UDP();
242 doNetCrypto();
243 //print_clientlist();
244 //print_friendlist();
245 //c_sleep(300);
246 c_sleep(1);
247 }
248
249 shutdown_networking();
250 return 0;
251} \ No newline at end of file
diff --git a/testing/DHT_test.c b/testing/DHT_test.c
index 4c1a9cd8..9100c01a 100644
--- a/testing/DHT_test.c
+++ b/testing/DHT_test.c
@@ -80,7 +80,7 @@ void print_friendlist()
80 } 80 }
81} 81}
82 82
83void printpacket(char * data, uint32_t length, IP_Port ip_port) 83void printpacket(uint8_t * data, uint32_t length, IP_Port ip_port)
84{ 84{
85 uint32_t i; 85 uint32_t i;
86 printf("UNHANDLED PACKET RECEIVED\nLENGTH:%u\nCONTENTS:\n", length); 86 printf("UNHANDLED PACKET RECEIVED\nLENGTH:%u\nCONTENTS:\n", length);
@@ -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(argv[3]); 105 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
@@ -125,7 +125,7 @@ int main(int argc, char *argv[])
125 bootstrap(bootstrap_ip_port); 125 bootstrap(bootstrap_ip_port);
126 126
127 IP_Port ip_port; 127 IP_Port ip_port;
128 char data[MAX_UDP_PACKET_SIZE]; 128 uint8_t data[MAX_UDP_PACKET_SIZE];
129 uint32_t length; 129 uint32_t length;
130 130
131 while(1) 131 while(1)