diff options
Diffstat (limited to 'testing/DHT_cryptosendfiletest.c')
-rw-r--r-- | testing/DHT_cryptosendfiletest.c | 119 |
1 files changed, 45 insertions, 74 deletions
diff --git a/testing/DHT_cryptosendfiletest.c b/testing/DHT_cryptosendfiletest.c index 6aaac677..b06ddea5 100644 --- a/testing/DHT_cryptosendfiletest.c +++ b/testing/DHT_cryptosendfiletest.c | |||
@@ -41,7 +41,7 @@ | |||
41 | 41 | ||
42 | #include <string.h> | 42 | #include <string.h> |
43 | 43 | ||
44 | //Sleep function (x = milliseconds) | 44 | /* Sleep function (x = milliseconds) */ |
45 | #ifdef WIN32 | 45 | #ifdef WIN32 |
46 | 46 | ||
47 | #define c_sleep(x) Sleep(1*x) | 47 | #define c_sleep(x) Sleep(1*x) |
@@ -60,16 +60,14 @@ void printip(IP_Port ip_port) | |||
60 | 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)); | 60 | 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)); |
61 | } | 61 | } |
62 | 62 | ||
63 | 63 | /* horrible function from one of my first C programs. | |
64 | //horrible function from one of my first C programs. | 64 | *only here because I was too lazy to write a proper one. */ |
65 | //only here because I was too lazy to write a proper one. | ||
66 | unsigned char * hex_string_to_bin(char hex_string[]) | 65 | unsigned char * hex_string_to_bin(char hex_string[]) |
67 | { | 66 | { |
68 | unsigned char * val = malloc(strlen(hex_string)); | 67 | unsigned char * val = malloc(strlen(hex_string)); |
69 | char * pos = hex_string; | 68 | char * pos = hex_string; |
70 | int i=0; | 69 | int i=0; |
71 | while(i < strlen(hex_string)) | 70 | while(i < strlen(hex_string)) { |
72 | { | ||
73 | sscanf(pos,"%2hhx",&val[i]); | 71 | sscanf(pos,"%2hhx",&val[i]); |
74 | pos+=2; | 72 | pos+=2; |
75 | i++; | 73 | i++; |
@@ -88,8 +86,7 @@ int main(int argc, char *argv[]) | |||
88 | new_keys(); | 86 | new_keys(); |
89 | printf("OUR ID: "); | 87 | printf("OUR ID: "); |
90 | uint32_t i; | 88 | uint32_t i; |
91 | for(i = 0; i < 32; i++) | 89 | for(i = 0; i < 32; i++) { |
92 | { | ||
93 | if(self_public_key[i] < 16) | 90 | if(self_public_key[i] < 16) |
94 | printf("0"); | 91 | printf("0"); |
95 | printf("%hhX",self_public_key[i]); | 92 | printf("%hhX",self_public_key[i]); |
@@ -105,8 +102,7 @@ int main(int argc, char *argv[]) | |||
105 | uint8_t friend_id[32]; | 102 | uint8_t friend_id[32]; |
106 | memcpy(friend_id, hex_string_to_bin(temp_id), 32); | 103 | memcpy(friend_id, hex_string_to_bin(temp_id), 32); |
107 | 104 | ||
108 | 105 | /* memcpy(self_client_id, "qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq", 32); */ | |
109 | //memcpy(self_client_id, "qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq", 32); | ||
110 | 106 | ||
111 | 107 | ||
112 | DHT_addfriend(friend_id); | 108 | DHT_addfriend(friend_id); |
@@ -118,15 +114,13 @@ int main(int argc, char *argv[]) | |||
118 | int friendrequest = -1; | 114 | int friendrequest = -1; |
119 | uint8_t request_data[512]; | 115 | uint8_t request_data[512]; |
120 | 116 | ||
121 | //initialize networking | 117 | /* initialize networking |
122 | //bind to ip 0.0.0.0:PORT | 118 | * bind to ip 0.0.0.0:PORT */ |
123 | IP ip; | 119 | IP ip; |
124 | ip.i = 0; | 120 | ip.i = 0; |
125 | init_networking(ip, PORT); | 121 | init_networking(ip, PORT); |
126 | initNetCrypto(); | 122 | initNetCrypto(); |
127 | 123 | ||
128 | |||
129 | |||
130 | perror("Initialization"); | 124 | perror("Initialization"); |
131 | IP_Port bootstrap_ip_port; | 125 | IP_Port bootstrap_ip_port; |
132 | bootstrap_ip_port.port = htons(atoi(argv[2])); | 126 | bootstrap_ip_port.port = htons(atoi(argv[2])); |
@@ -147,122 +141,99 @@ int main(int argc, char *argv[]) | |||
147 | if ( file2==NULL ){return 1;} | 141 | if ( file2==NULL ){return 1;} |
148 | read1 = fread(buffer1, 1, 128, file1); | 142 | read1 = fread(buffer1, 1, 128, file1); |
149 | 143 | ||
150 | while(1) | 144 | while(1) { |
151 | { | 145 | while(receivepacket(&ip_port, data, &length) != -1) { |
152 | 146 | if(rand() % 3 != 1) { /* simulate packet loss */ | |
153 | while(receivepacket(&ip_port, data, &length) != -1) | 147 | if(DHT_handlepacket(data, length, ip_port) && LosslessUDP_handlepacket(data, length, ip_port)) { |
154 | { | 148 | /* if packet is not recognized */ |
155 | if(rand() % 3 != 1)//simulate packet loss | ||
156 | { | ||
157 | if(DHT_handlepacket(data, length, ip_port) && LosslessUDP_handlepacket(data, length, ip_port)) | ||
158 | { | ||
159 | //if packet is not recognized | ||
160 | printf("Received unhandled packet with length: %u\n", length); | 149 | printf("Received unhandled packet with length: %u\n", length); |
161 | } | 150 | } else { |
162 | else | ||
163 | { | ||
164 | printf("Received handled packet with length: %u\n", length); | 151 | printf("Received handled packet with length: %u\n", length); |
165 | } | 152 | } |
166 | } | 153 | } |
167 | } | 154 | } |
168 | friend_ip = DHT_getfriendip(friend_id); | 155 | friend_ip = DHT_getfriendip(friend_id); |
169 | if(friend_ip.ip.i != 0) | 156 | if(friend_ip.ip.i != 0) { |
170 | { | 157 | if(connection == -1 && friendrequest == -1) { |
171 | if(connection == -1 && friendrequest == -1) | ||
172 | { | ||
173 | printf("Sending friend request to peer:"); | 158 | printf("Sending friend request to peer:"); |
174 | printip(friend_ip); | 159 | printip(friend_ip); |
175 | friendrequest = send_friendrequest(friend_id, friend_ip,(uint8_t *) "Hello World", 12); | 160 | friendrequest = send_friendrequest(friend_id, friend_ip,(uint8_t *) "Hello World", 12); |
176 | //connection = crypto_connect((uint8_t *)friend_id, friend_ip); | 161 | /* connection = crypto_connect((uint8_t *)friend_id, friend_ip); */ |
177 | //connection = new_connection(friend_ip); | 162 | /* connection = new_connection(friend_ip); */ |
178 | } | 163 | } |
179 | if(check_friendrequest(friendrequest) == 1) | 164 | if(check_friendrequest(friendrequest) == 1) { |
180 | { | ||
181 | printf("Started connecting to friend:"); | 165 | printf("Started connecting to friend:"); |
182 | connection = crypto_connect(friend_id, friend_ip); | 166 | connection = crypto_connect(friend_id, friend_ip); |
183 | } | 167 | } |
184 | } | 168 | } |
185 | if(inconnection == -1) | 169 | if(inconnection == -1) { |
186 | { | ||
187 | uint8_t secret_nonce[crypto_box_NONCEBYTES]; | 170 | uint8_t secret_nonce[crypto_box_NONCEBYTES]; |
188 | uint8_t public_key[crypto_box_PUBLICKEYBYTES]; | 171 | uint8_t public_key[crypto_box_PUBLICKEYBYTES]; |
189 | uint8_t session_key[crypto_box_PUBLICKEYBYTES]; | 172 | uint8_t session_key[crypto_box_PUBLICKEYBYTES]; |
190 | inconnection = crypto_inbound(public_key, secret_nonce, session_key); | 173 | inconnection = crypto_inbound(public_key, secret_nonce, session_key); |
191 | inconnection = accept_crypto_inbound(inconnection, acceptedfriend_public_key, secret_nonce, session_key); | 174 | inconnection = accept_crypto_inbound(inconnection, acceptedfriend_public_key, secret_nonce, session_key); |
192 | //inconnection = incoming_connection(); | 175 | /* inconnection = incoming_connection(); */ |
193 | if(inconnection != -1) | 176 | if(inconnection != -1) { |
194 | { | ||
195 | printf("Someone connected to us:\n"); | 177 | printf("Someone connected to us:\n"); |
196 | // printip(connection_ip(inconnection)); | 178 | /* printip(connection_ip(inconnection)); */ |
197 | } | 179 | } |
198 | } | 180 | } |
199 | if(handle_friendrequest(acceptedfriend_public_key, request_data) > 1) | 181 | if(handle_friendrequest(acceptedfriend_public_key, request_data) > 1) { |
200 | { | ||
201 | printf("RECIEVED FRIEND REQUEST: %s\n", request_data); | 182 | printf("RECIEVED FRIEND REQUEST: %s\n", request_data); |
202 | } | 183 | } |
203 | 184 | ||
204 | //if someone connected to us write what he sends to a file | 185 | /* if someone connected to us write what he sends to a file |
205 | //also send him our file. | 186 | * also send him our file. */ |
206 | if(inconnection != -1) | 187 | if(inconnection != -1) { |
207 | { | 188 | if(write_cryptpacket(inconnection, buffer1, read1)) { |
208 | if(write_cryptpacket(inconnection, buffer1, read1)) | ||
209 | { | ||
210 | printf("Wrote data1.\n"); | 189 | printf("Wrote data1.\n"); |
211 | read1 = fread(buffer1, 1, 128, file1); | 190 | read1 = fread(buffer1, 1, 128, file1); |
212 | } | 191 | } |
213 | read2 = read_cryptpacket(inconnection, buffer2); | 192 | read2 = read_cryptpacket(inconnection, buffer2); |
214 | if(read2 != 0) | 193 | if(read2 != 0) { |
215 | { | ||
216 | printf("Received data1.\n"); | 194 | printf("Received data1.\n"); |
217 | if(!fwrite(buffer2, read2, 1, file2)) | 195 | if(!fwrite(buffer2, read2, 1, file2)) { |
218 | { | ||
219 | printf("file write error1\n"); | 196 | printf("file write error1\n"); |
220 | } | 197 | } |
221 | if(read2 < 128) | 198 | if(read2 < 128) { |
222 | { | ||
223 | printf("Closed file1 %u\n", read2); | 199 | printf("Closed file1 %u\n", read2); |
224 | fclose(file2); | 200 | fclose(file2); |
225 | } | 201 | } |
226 | } | 202 | } |
227 | else if(is_cryptoconnected(inconnection) == 4)//if buffer is empty and the connection timed out. | 203 | /* if buffer is empty and the connection timed out. */ |
228 | { | 204 | else if(is_cryptoconnected(inconnection) == 4) { |
229 | crypto_kill(inconnection); | 205 | crypto_kill(inconnection); |
230 | } | 206 | } |
231 | } | 207 | } |
232 | //if we are connected to a friend send him data from the file. | 208 | /* if we are connected to a friend send him data from the file. |
233 | //also put what he sends us in a file. | 209 | * also put what he sends us in a file. */ |
234 | if(is_cryptoconnected(connection) >= 3) | 210 | if(is_cryptoconnected(connection) >= 3) { |
235 | { | 211 | if(write_cryptpacket(0, buffer1, read1)) { |
236 | if(write_cryptpacket(0, buffer1, read1)) | ||
237 | { | ||
238 | printf("Wrote data2.\n"); | 212 | printf("Wrote data2.\n"); |
239 | read1 = fread(buffer1, 1, 128, file1); | 213 | read1 = fread(buffer1, 1, 128, file1); |
240 | } | 214 | } |
241 | read2 = read_cryptpacket(0, buffer2); | 215 | read2 = read_cryptpacket(0, buffer2); |
242 | if(read2 != 0) | 216 | if(read2 != 0) { |
243 | { | ||
244 | printf("Received data2.\n"); | 217 | printf("Received data2.\n"); |
245 | if(!fwrite(buffer2, read2, 1, file2)) | 218 | if(!fwrite(buffer2, read2, 1, file2)) { |
246 | { | ||
247 | printf("file write error2\n"); | 219 | printf("file write error2\n"); |
248 | } | 220 | } |
249 | if(read2 < 128) | 221 | if(read2 < 128) { |
250 | { | ||
251 | printf("Closed file2 %u\n", read2); | 222 | printf("Closed file2 %u\n", read2); |
252 | fclose(file2); | 223 | fclose(file2); |
253 | } | 224 | } |
254 | } | 225 | } |
255 | else if(is_cryptoconnected(connection) == 4)//if buffer is empty and the connection timed out. | 226 | /* if buffer is empty and the connection timed out. */ |
256 | { | 227 | else if(is_cryptoconnected(connection) == 4) { |
257 | crypto_kill(connection); | 228 | crypto_kill(connection); |
258 | } | 229 | } |
259 | } | 230 | } |
260 | doDHT(); | 231 | doDHT(); |
261 | doLossless_UDP(); | 232 | doLossless_UDP(); |
262 | doNetCrypto(); | 233 | doNetCrypto(); |
263 | //print_clientlist(); | 234 | /*print_clientlist(); |
264 | //print_friendlist(); | 235 | *print_friendlist(); |
265 | //c_sleep(300); | 236 | *c_sleep(300); */ |
266 | c_sleep(1); | 237 | c_sleep(1); |
267 | } | 238 | } |
268 | 239 | ||