diff options
author | Astonex <softukitu@gmail.com> | 2013-07-27 02:52:24 +0100 |
---|---|---|
committer | Astonex <softukitu@gmail.com> | 2013-07-27 02:52:24 +0100 |
commit | 3e1b96f333b7e51c8a714c2e701e7310239f1364 (patch) | |
tree | 838d8d55a7f2f69830ff59161820f21e86ddcca0 /testing/DHT_cryptosendfiletest.c | |
parent | 37a300f9021cbf8c5e6e1134ae8eee9c33307eb7 (diff) | |
parent | 1b4ea2e1aeb874e872a2c767326633450de12d20 (diff) |
Merge remote-tracking branch 'ProjectTox/master'
Diffstat (limited to 'testing/DHT_cryptosendfiletest.c')
-rw-r--r-- | testing/DHT_cryptosendfiletest.c | 141 |
1 files changed, 66 insertions, 75 deletions
diff --git a/testing/DHT_cryptosendfiletest.c b/testing/DHT_cryptosendfiletest.c index 39a928d2..5c3a0256 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,15 +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 | //TODO: rewrite | |
45 | //horrible function from one of my first C programs. | ||
46 | //only here because I was too lazy to write a proper one. | ||
47 | unsigned char * hex_string_to_bin(char hex_string[]) | 64 | unsigned char * hex_string_to_bin(char hex_string[]) |
48 | { | 65 | { |
49 | unsigned char * val = malloc(strlen(hex_string)); | 66 | size_t len = strlen(hex_string); |
67 | unsigned char * val = malloc(len); | ||
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 < len) |
53 | { | 71 | { |
54 | sscanf(pos,"%2hhx",&val[i]); | 72 | sscanf(pos,"%2hhx",&val[i]); |
55 | pos+=2; | 73 | pos+=2; |
@@ -69,8 +87,7 @@ int main(int argc, char *argv[]) | |||
69 | new_keys(); | 87 | new_keys(); |
70 | printf("OUR ID: "); | 88 | printf("OUR ID: "); |
71 | uint32_t i; | 89 | uint32_t i; |
72 | for(i = 0; i < 32; i++) | 90 | for(i = 0; i < 32; i++) { |
73 | { | ||
74 | if(self_public_key[i] < 16) | 91 | if(self_public_key[i] < 16) |
75 | printf("0"); | 92 | printf("0"); |
76 | printf("%hhX",self_public_key[i]); | 93 | printf("%hhX",self_public_key[i]); |
@@ -86,8 +103,7 @@ int main(int argc, char *argv[]) | |||
86 | uint8_t friend_id[32]; | 103 | uint8_t friend_id[32]; |
87 | memcpy(friend_id, hex_string_to_bin(temp_id), 32); | 104 | memcpy(friend_id, hex_string_to_bin(temp_id), 32); |
88 | 105 | ||
89 | 106 | /* memcpy(self_client_id, "qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq", 32); */ | |
90 | //memcpy(self_client_id, "qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq", 32); | ||
91 | 107 | ||
92 | 108 | ||
93 | DHT_addfriend(friend_id); | 109 | DHT_addfriend(friend_id); |
@@ -99,15 +115,13 @@ int main(int argc, char *argv[]) | |||
99 | int friendrequest = -1; | 115 | int friendrequest = -1; |
100 | uint8_t request_data[512]; | 116 | uint8_t request_data[512]; |
101 | 117 | ||
102 | //initialize networking | 118 | /* initialize networking |
103 | //bind to ip 0.0.0.0:PORT | 119 | * bind to ip 0.0.0.0:PORT */ |
104 | IP ip; | 120 | IP ip; |
105 | ip.i = 0; | 121 | ip.i = 0; |
106 | init_networking(ip, PORT); | 122 | init_networking(ip, PORT); |
107 | initNetCrypto(); | 123 | initNetCrypto(); |
108 | 124 | ||
109 | |||
110 | |||
111 | perror("Initialization"); | 125 | perror("Initialization"); |
112 | IP_Port bootstrap_ip_port; | 126 | IP_Port bootstrap_ip_port; |
113 | bootstrap_ip_port.port = htons(atoi(argv[2])); | 127 | bootstrap_ip_port.port = htons(atoi(argv[2])); |
@@ -128,125 +142,102 @@ int main(int argc, char *argv[]) | |||
128 | if ( file2==NULL ){return 1;} | 142 | if ( file2==NULL ){return 1;} |
129 | read1 = fread(buffer1, 1, 128, file1); | 143 | read1 = fread(buffer1, 1, 128, file1); |
130 | 144 | ||
131 | while(1) | 145 | while(1) { |
132 | { | 146 | while(receivepacket(&ip_port, data, &length) != -1) { |
133 | 147 | if(rand() % 3 != 1) { /* simulate packet loss */ | |
134 | while(receivepacket(&ip_port, data, &length) != -1) | 148 | if(DHT_handlepacket(data, length, ip_port) && LosslessUDP_handlepacket(data, length, ip_port)) { |
135 | { | 149 | /* 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); | 150 | printf("Received unhandled packet with length: %u\n", length); |
142 | } | 151 | } else { |
143 | else | ||
144 | { | ||
145 | printf("Received handled packet with length: %u\n", length); | 152 | printf("Received handled packet with length: %u\n", length); |
146 | } | 153 | } |
147 | } | 154 | } |
148 | } | 155 | } |
149 | friend_ip = DHT_getfriendip(friend_id); | 156 | friend_ip = DHT_getfriendip(friend_id); |
150 | if(friend_ip.ip.i != 0) | 157 | if(friend_ip.ip.i != 0) { |
151 | { | 158 | if(connection == -1 && friendrequest == -1) { |
152 | if(connection == -1 && friendrequest == -1) | ||
153 | { | ||
154 | printf("Sending friend request to peer:"); | 159 | printf("Sending friend request to peer:"); |
155 | printip(friend_ip); | 160 | printip(friend_ip); |
156 | friendrequest = send_friendrequest(friend_id, friend_ip,(uint8_t *) "Hello World", 12); | 161 | friendrequest = send_friendrequest(friend_id, friend_ip,(uint8_t *) "Hello World", 12); |
157 | //connection = crypto_connect((uint8_t *)friend_id, friend_ip); | 162 | /* connection = crypto_connect((uint8_t *)friend_id, friend_ip); */ |
158 | //connection = new_connection(friend_ip); | 163 | /* connection = new_connection(friend_ip); */ |
159 | } | 164 | } |
160 | if(check_friendrequest(friendrequest) == 1) | 165 | if(check_friendrequest(friendrequest) == 1) { |
161 | { | ||
162 | printf("Started connecting to friend:"); | 166 | printf("Started connecting to friend:"); |
163 | connection = crypto_connect(friend_id, friend_ip); | 167 | connection = crypto_connect(friend_id, friend_ip); |
164 | } | 168 | } |
165 | } | 169 | } |
166 | if(inconnection == -1) | 170 | if(inconnection == -1) { |
167 | { | ||
168 | uint8_t secret_nonce[crypto_box_NONCEBYTES]; | 171 | uint8_t secret_nonce[crypto_box_NONCEBYTES]; |
169 | uint8_t public_key[crypto_box_PUBLICKEYBYTES]; | 172 | uint8_t public_key[crypto_box_PUBLICKEYBYTES]; |
170 | uint8_t session_key[crypto_box_PUBLICKEYBYTES]; | 173 | uint8_t session_key[crypto_box_PUBLICKEYBYTES]; |
171 | inconnection = crypto_inbound(public_key, secret_nonce, session_key); | 174 | inconnection = crypto_inbound(public_key, secret_nonce, session_key); |
172 | inconnection = accept_crypto_inbound(inconnection, acceptedfriend_public_key, secret_nonce, session_key); | 175 | inconnection = accept_crypto_inbound(inconnection, acceptedfriend_public_key, secret_nonce, session_key); |
173 | //inconnection = incoming_connection(); | 176 | /* inconnection = incoming_connection(); */ |
174 | if(inconnection != -1) | 177 | if(inconnection != -1) { |
175 | { | ||
176 | printf("Someone connected to us:\n"); | 178 | printf("Someone connected to us:\n"); |
177 | // printip(connection_ip(inconnection)); | 179 | /* printip(connection_ip(inconnection)); */ |
178 | } | 180 | } |
179 | } | 181 | } |
180 | if(handle_friendrequest(acceptedfriend_public_key, request_data) > 1) | 182 | if(handle_friendrequest(acceptedfriend_public_key, request_data) > 1) { |
181 | { | ||
182 | printf("RECIEVED FRIEND REQUEST: %s\n", request_data); | 183 | printf("RECIEVED FRIEND REQUEST: %s\n", request_data); |
183 | } | 184 | } |
184 | 185 | ||
185 | //if someone connected to us write what he sends to a file | 186 | /* if someone connected to us write what he sends to a file |
186 | //also send him our file. | 187 | * also send him our file. */ |
187 | if(inconnection != -1) | 188 | if(inconnection != -1) { |
188 | { | 189 | if(write_cryptpacket(inconnection, buffer1, read1)) { |
189 | if(write_cryptpacket(inconnection, buffer1, read1)) | ||
190 | { | ||
191 | printf("Wrote data1.\n"); | 190 | printf("Wrote data1.\n"); |
192 | read1 = fread(buffer1, 1, 128, file1); | 191 | read1 = fread(buffer1, 1, 128, file1); |
193 | } | 192 | } |
194 | read2 = read_cryptpacket(inconnection, buffer2); | 193 | read2 = read_cryptpacket(inconnection, buffer2); |
195 | if(read2 != 0) | 194 | if(read2 != 0) { |
196 | { | ||
197 | printf("Received data1.\n"); | 195 | printf("Received data1.\n"); |
198 | if(!fwrite(buffer2, read2, 1, file2)) | 196 | if(!fwrite(buffer2, read2, 1, file2)) { |
199 | { | ||
200 | printf("file write error1\n"); | 197 | printf("file write error1\n"); |
201 | } | 198 | } |
202 | if(read2 < 128) | 199 | if(read2 < 128) { |
203 | { | ||
204 | printf("Closed file1 %u\n", read2); | 200 | printf("Closed file1 %u\n", read2); |
205 | fclose(file2); | 201 | fclose(file2); |
206 | } | 202 | } |
207 | } | 203 | } |
208 | else if(is_cryptoconnected(inconnection) == 4)//if buffer is empty and the connection timed out. | 204 | /* if buffer is empty and the connection timed out. */ |
209 | { | 205 | else if(is_cryptoconnected(inconnection) == 4) { |
210 | crypto_kill(inconnection); | 206 | crypto_kill(inconnection); |
211 | } | 207 | } |
212 | } | 208 | } |
213 | //if we are connected to a friend send him data from the file. | 209 | /* if we are connected to a friend send him data from the file. |
214 | //also put what he sends us in a file. | 210 | * also put what he sends us in a file. */ |
215 | if(is_cryptoconnected(connection) >= 3) | 211 | if(is_cryptoconnected(connection) >= 3) { |
216 | { | 212 | if(write_cryptpacket(0, buffer1, read1)) { |
217 | if(write_cryptpacket(0, buffer1, read1)) | ||
218 | { | ||
219 | printf("Wrote data2.\n"); | 213 | printf("Wrote data2.\n"); |
220 | read1 = fread(buffer1, 1, 128, file1); | 214 | read1 = fread(buffer1, 1, 128, file1); |
221 | } | 215 | } |
222 | read2 = read_cryptpacket(0, buffer2); | 216 | read2 = read_cryptpacket(0, buffer2); |
223 | if(read2 != 0) | 217 | if(read2 != 0) { |
224 | { | ||
225 | printf("Received data2.\n"); | 218 | printf("Received data2.\n"); |
226 | if(!fwrite(buffer2, read2, 1, file2)) | 219 | if(!fwrite(buffer2, read2, 1, file2)) { |
227 | { | ||
228 | printf("file write error2\n"); | 220 | printf("file write error2\n"); |
229 | } | 221 | } |
230 | if(read2 < 128) | 222 | if(read2 < 128) { |
231 | { | ||
232 | printf("Closed file2 %u\n", read2); | 223 | printf("Closed file2 %u\n", read2); |
233 | fclose(file2); | 224 | fclose(file2); |
234 | } | 225 | } |
235 | } | 226 | } |
236 | else if(is_cryptoconnected(connection) == 4)//if buffer is empty and the connection timed out. | 227 | /* if buffer is empty and the connection timed out. */ |
237 | { | 228 | else if(is_cryptoconnected(connection) == 4) { |
238 | crypto_kill(connection); | 229 | crypto_kill(connection); |
239 | } | 230 | } |
240 | } | 231 | } |
241 | doDHT(); | 232 | doDHT(); |
242 | doLossless_UDP(); | 233 | doLossless_UDP(); |
243 | doNetCrypto(); | 234 | doNetCrypto(); |
244 | //print_clientlist(); | 235 | /*print_clientlist(); |
245 | //print_friendlist(); | 236 | *print_friendlist(); |
246 | //c_sleep(300); | 237 | *c_sleep(300); */ |
247 | c_sleep(1); | 238 | c_sleep(1); |
248 | } | 239 | } |
249 | 240 | ||
250 | shutdown_networking(); | 241 | shutdown_networking(); |
251 | return 0; | 242 | return 0; |
252 | } \ No newline at end of file | 243 | } |