diff options
Diffstat (limited to 'testing/DHT_cryptosendfiletest.c')
-rw-r--r-- | testing/DHT_cryptosendfiletest.c | 138 |
1 files changed, 64 insertions, 74 deletions
diff --git a/testing/DHT_cryptosendfiletest.c b/testing/DHT_cryptosendfiletest.c index 39a928d2..b06ddea5 100644 --- a/testing/DHT_cryptosendfiletest.c +++ b/testing/DHT_cryptosendfiletest.c | |||
@@ -15,14 +15,33 @@ | |||
15 | * Saves all received data to: received.txt | 15 | * Saves all received data to: received.txt |
16 | * | 16 | * |
17 | * EX: ./test 127.0.0.1 33445 filename.txt | 17 | * EX: ./test 127.0.0.1 33445 filename.txt |
18 | * | ||
19 | * Copyright (C) 2013 Tox project All Rights Reserved. | ||
20 | * | ||
21 | * This file is part of Tox. | ||
22 | * | ||
23 | * Tox is free software: you can redistribute it and/or modify | ||
24 | * it under the terms of the GNU General Public License as published by | ||
25 | * the Free Software Foundation, either version 3 of the License, or | ||
26 | * (at your option) any later version. | ||
27 | * | ||
28 | * Tox is distributed in the hope that it will be useful, | ||
29 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
30 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
31 | * GNU General Public License for more details. | ||
32 | * | ||
33 | * You should have received a copy of the GNU General Public License | ||
34 | * along with Tox. If not, see <http://www.gnu.org/licenses/>. | ||
35 | * | ||
18 | */ | 36 | */ |
37 | |||
19 | #include "../core/network.h" | 38 | #include "../core/network.h" |
20 | #include "../core/DHT.h" | 39 | #include "../core/DHT.h" |
21 | #include "../core/net_crypto.h" | 40 | #include "../core/net_crypto.h" |
22 | 41 | ||
23 | #include <string.h> | 42 | #include <string.h> |
24 | 43 | ||
25 | //Sleep function (x = milliseconds) | 44 | /* Sleep function (x = milliseconds) */ |
26 | #ifdef WIN32 | 45 | #ifdef WIN32 |
27 | 46 | ||
28 | #define c_sleep(x) Sleep(1*x) | 47 | #define c_sleep(x) Sleep(1*x) |
@@ -41,16 +60,14 @@ void printip(IP_Port ip_port) | |||
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)); | 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)); |
42 | } | 61 | } |
43 | 62 | ||
44 | 63 | /* horrible function from one of my first C programs. | |
45 | //horrible function from one of my first C programs. | 64 | *only here because I was too lazy to write a proper one. */ |
46 | //only here because I was too lazy to write a proper one. | ||
47 | unsigned char * hex_string_to_bin(char hex_string[]) | 65 | unsigned char * hex_string_to_bin(char hex_string[]) |
48 | { | 66 | { |
49 | unsigned char * val = malloc(strlen(hex_string)); | 67 | unsigned char * val = malloc(strlen(hex_string)); |
50 | char * pos = hex_string; | 68 | char * pos = hex_string; |
51 | int i=0; | 69 | int i=0; |
52 | while(i < strlen(hex_string)) | 70 | while(i < strlen(hex_string)) { |
53 | { | ||
54 | sscanf(pos,"%2hhx",&val[i]); | 71 | sscanf(pos,"%2hhx",&val[i]); |
55 | pos+=2; | 72 | pos+=2; |
56 | i++; | 73 | i++; |
@@ -69,8 +86,7 @@ int main(int argc, char *argv[]) | |||
69 | new_keys(); | 86 | new_keys(); |
70 | printf("OUR ID: "); | 87 | printf("OUR ID: "); |
71 | uint32_t i; | 88 | uint32_t i; |
72 | for(i = 0; i < 32; i++) | 89 | for(i = 0; i < 32; i++) { |
73 | { | ||
74 | if(self_public_key[i] < 16) | 90 | if(self_public_key[i] < 16) |
75 | printf("0"); | 91 | printf("0"); |
76 | printf("%hhX",self_public_key[i]); | 92 | printf("%hhX",self_public_key[i]); |
@@ -86,8 +102,7 @@ int main(int argc, char *argv[]) | |||
86 | uint8_t friend_id[32]; | 102 | uint8_t friend_id[32]; |
87 | memcpy(friend_id, hex_string_to_bin(temp_id), 32); | 103 | memcpy(friend_id, hex_string_to_bin(temp_id), 32); |
88 | 104 | ||
89 | 105 | /* memcpy(self_client_id, "qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq", 32); */ | |
90 | //memcpy(self_client_id, "qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq", 32); | ||
91 | 106 | ||
92 | 107 | ||
93 | DHT_addfriend(friend_id); | 108 | DHT_addfriend(friend_id); |
@@ -99,15 +114,13 @@ int main(int argc, char *argv[]) | |||
99 | int friendrequest = -1; | 114 | int friendrequest = -1; |
100 | uint8_t request_data[512]; | 115 | uint8_t request_data[512]; |
101 | 116 | ||
102 | //initialize networking | 117 | /* initialize networking |
103 | //bind to ip 0.0.0.0:PORT | 118 | * bind to ip 0.0.0.0:PORT */ |
104 | IP ip; | 119 | IP ip; |
105 | ip.i = 0; | 120 | ip.i = 0; |
106 | init_networking(ip, PORT); | 121 | init_networking(ip, PORT); |
107 | initNetCrypto(); | 122 | initNetCrypto(); |
108 | 123 | ||
109 | |||
110 | |||
111 | perror("Initialization"); | 124 | perror("Initialization"); |
112 | IP_Port bootstrap_ip_port; | 125 | IP_Port bootstrap_ip_port; |
113 | bootstrap_ip_port.port = htons(atoi(argv[2])); | 126 | bootstrap_ip_port.port = htons(atoi(argv[2])); |
@@ -128,122 +141,99 @@ int main(int argc, char *argv[]) | |||
128 | if ( file2==NULL ){return 1;} | 141 | if ( file2==NULL ){return 1;} |
129 | read1 = fread(buffer1, 1, 128, file1); | 142 | read1 = fread(buffer1, 1, 128, file1); |
130 | 143 | ||
131 | while(1) | 144 | while(1) { |
132 | { | 145 | while(receivepacket(&ip_port, data, &length) != -1) { |
133 | 146 | if(rand() % 3 != 1) { /* simulate packet loss */ | |
134 | while(receivepacket(&ip_port, data, &length) != -1) | 147 | if(DHT_handlepacket(data, length, ip_port) && LosslessUDP_handlepacket(data, length, ip_port)) { |
135 | { | 148 | /* if packet is not recognized */ |
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); | 149 | printf("Received unhandled packet with length: %u\n", length); |
142 | } | 150 | } else { |
143 | else | ||
144 | { | ||
145 | printf("Received handled packet with length: %u\n", length); | 151 | printf("Received handled packet with length: %u\n", length); |
146 | } | 152 | } |
147 | } | 153 | } |
148 | } | 154 | } |
149 | friend_ip = DHT_getfriendip(friend_id); | 155 | friend_ip = DHT_getfriendip(friend_id); |
150 | if(friend_ip.ip.i != 0) | 156 | if(friend_ip.ip.i != 0) { |
151 | { | 157 | if(connection == -1 && friendrequest == -1) { |
152 | if(connection == -1 && friendrequest == -1) | ||
153 | { | ||
154 | printf("Sending friend request to peer:"); | 158 | printf("Sending friend request to peer:"); |
155 | printip(friend_ip); | 159 | printip(friend_ip); |
156 | 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); |
157 | //connection = crypto_connect((uint8_t *)friend_id, friend_ip); | 161 | /* connection = crypto_connect((uint8_t *)friend_id, friend_ip); */ |
158 | //connection = new_connection(friend_ip); | 162 | /* connection = new_connection(friend_ip); */ |
159 | } | 163 | } |
160 | if(check_friendrequest(friendrequest) == 1) | 164 | if(check_friendrequest(friendrequest) == 1) { |
161 | { | ||
162 | printf("Started connecting to friend:"); | 165 | printf("Started connecting to friend:"); |
163 | connection = crypto_connect(friend_id, friend_ip); | 166 | connection = crypto_connect(friend_id, friend_ip); |
164 | } | 167 | } |
165 | } | 168 | } |
166 | if(inconnection == -1) | 169 | if(inconnection == -1) { |
167 | { | ||
168 | uint8_t secret_nonce[crypto_box_NONCEBYTES]; | 170 | uint8_t secret_nonce[crypto_box_NONCEBYTES]; |
169 | uint8_t public_key[crypto_box_PUBLICKEYBYTES]; | 171 | uint8_t public_key[crypto_box_PUBLICKEYBYTES]; |
170 | uint8_t session_key[crypto_box_PUBLICKEYBYTES]; | 172 | uint8_t session_key[crypto_box_PUBLICKEYBYTES]; |
171 | inconnection = crypto_inbound(public_key, secret_nonce, session_key); | 173 | inconnection = crypto_inbound(public_key, secret_nonce, session_key); |
172 | 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); |
173 | //inconnection = incoming_connection(); | 175 | /* inconnection = incoming_connection(); */ |
174 | if(inconnection != -1) | 176 | if(inconnection != -1) { |
175 | { | ||
176 | printf("Someone connected to us:\n"); | 177 | printf("Someone connected to us:\n"); |
177 | // printip(connection_ip(inconnection)); | 178 | /* printip(connection_ip(inconnection)); */ |
178 | } | 179 | } |
179 | } | 180 | } |
180 | if(handle_friendrequest(acceptedfriend_public_key, request_data) > 1) | 181 | if(handle_friendrequest(acceptedfriend_public_key, request_data) > 1) { |
181 | { | ||
182 | printf("RECIEVED FRIEND REQUEST: %s\n", request_data); | 182 | printf("RECIEVED FRIEND REQUEST: %s\n", request_data); |
183 | } | 183 | } |
184 | 184 | ||
185 | //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 |
186 | //also send him our file. | 186 | * also send him our file. */ |
187 | if(inconnection != -1) | 187 | if(inconnection != -1) { |
188 | { | 188 | if(write_cryptpacket(inconnection, buffer1, read1)) { |
189 | if(write_cryptpacket(inconnection, buffer1, read1)) | ||
190 | { | ||
191 | printf("Wrote data1.\n"); | 189 | printf("Wrote data1.\n"); |
192 | read1 = fread(buffer1, 1, 128, file1); | 190 | read1 = fread(buffer1, 1, 128, file1); |
193 | } | 191 | } |
194 | read2 = read_cryptpacket(inconnection, buffer2); | 192 | read2 = read_cryptpacket(inconnection, buffer2); |
195 | if(read2 != 0) | 193 | if(read2 != 0) { |
196 | { | ||
197 | printf("Received data1.\n"); | 194 | printf("Received data1.\n"); |
198 | if(!fwrite(buffer2, read2, 1, file2)) | 195 | if(!fwrite(buffer2, read2, 1, file2)) { |
199 | { | ||
200 | printf("file write error1\n"); | 196 | printf("file write error1\n"); |
201 | } | 197 | } |
202 | if(read2 < 128) | 198 | if(read2 < 128) { |
203 | { | ||
204 | printf("Closed file1 %u\n", read2); | 199 | printf("Closed file1 %u\n", read2); |
205 | fclose(file2); | 200 | fclose(file2); |
206 | } | 201 | } |
207 | } | 202 | } |
208 | 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. */ |
209 | { | 204 | else if(is_cryptoconnected(inconnection) == 4) { |
210 | crypto_kill(inconnection); | 205 | crypto_kill(inconnection); |
211 | } | 206 | } |
212 | } | 207 | } |
213 | //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. |
214 | //also put what he sends us in a file. | 209 | * also put what he sends us in a file. */ |
215 | if(is_cryptoconnected(connection) >= 3) | 210 | if(is_cryptoconnected(connection) >= 3) { |
216 | { | 211 | if(write_cryptpacket(0, buffer1, read1)) { |
217 | if(write_cryptpacket(0, buffer1, read1)) | ||
218 | { | ||
219 | printf("Wrote data2.\n"); | 212 | printf("Wrote data2.\n"); |
220 | read1 = fread(buffer1, 1, 128, file1); | 213 | read1 = fread(buffer1, 1, 128, file1); |
221 | } | 214 | } |
222 | read2 = read_cryptpacket(0, buffer2); | 215 | read2 = read_cryptpacket(0, buffer2); |
223 | if(read2 != 0) | 216 | if(read2 != 0) { |
224 | { | ||
225 | printf("Received data2.\n"); | 217 | printf("Received data2.\n"); |
226 | if(!fwrite(buffer2, read2, 1, file2)) | 218 | if(!fwrite(buffer2, read2, 1, file2)) { |
227 | { | ||
228 | printf("file write error2\n"); | 219 | printf("file write error2\n"); |
229 | } | 220 | } |
230 | if(read2 < 128) | 221 | if(read2 < 128) { |
231 | { | ||
232 | printf("Closed file2 %u\n", read2); | 222 | printf("Closed file2 %u\n", read2); |
233 | fclose(file2); | 223 | fclose(file2); |
234 | } | 224 | } |
235 | } | 225 | } |
236 | 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. */ |
237 | { | 227 | else if(is_cryptoconnected(connection) == 4) { |
238 | crypto_kill(connection); | 228 | crypto_kill(connection); |
239 | } | 229 | } |
240 | } | 230 | } |
241 | doDHT(); | 231 | doDHT(); |
242 | doLossless_UDP(); | 232 | doLossless_UDP(); |
243 | doNetCrypto(); | 233 | doNetCrypto(); |
244 | //print_clientlist(); | 234 | /*print_clientlist(); |
245 | //print_friendlist(); | 235 | *print_friendlist(); |
246 | //c_sleep(300); | 236 | *c_sleep(300); */ |
247 | c_sleep(1); | 237 | c_sleep(1); |
248 | } | 238 | } |
249 | 239 | ||