diff options
Diffstat (limited to 'testing/DHT_cryptosendfiletest.c')
-rw-r--r-- | testing/DHT_cryptosendfiletest.c | 258 |
1 files changed, 0 insertions, 258 deletions
diff --git a/testing/DHT_cryptosendfiletest.c b/testing/DHT_cryptosendfiletest.c deleted file mode 100644 index 42635b35..00000000 --- a/testing/DHT_cryptosendfiletest.c +++ /dev/null | |||
@@ -1,258 +0,0 @@ | |||
1 | /* DHT cryptosendfiletest | ||
2 | * | ||
3 | * This program sends or receives 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/$HOSTNAME/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 | * 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 | * | ||
36 | */ | ||
37 | |||
38 | #include "../core/network.h" | ||
39 | #include "../core/DHT.h" | ||
40 | #include "../core/net_crypto.h" | ||
41 | #include "misc_tools.h" | ||
42 | |||
43 | #include <string.h> | ||
44 | |||
45 | /* Sleep function (x = milliseconds) */ | ||
46 | #ifdef WIN32 | ||
47 | |||
48 | #define c_sleep(x) Sleep(1*x) | ||
49 | |||
50 | #else | ||
51 | #include <unistd.h> | ||
52 | #include <arpa/inet.h> | ||
53 | #define c_sleep(x) usleep(1000*x) | ||
54 | |||
55 | #endif | ||
56 | |||
57 | #define PORT 33445 | ||
58 | |||
59 | void printip(IP_Port ip_port) | ||
60 | { | ||
61 | 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], | ||
62 | ntohs(ip_port.port)); | ||
63 | } | ||
64 | |||
65 | uint8_t self_public_key[crypto_box_PUBLICKEYBYTES]; | ||
66 | |||
67 | int main(int argc, char *argv[]) | ||
68 | { | ||
69 | if (argc < 4) { | ||
70 | printf("usage %s ip port filename(of file to send)\n", argv[0]); | ||
71 | exit(0); | ||
72 | } | ||
73 | |||
74 | new_keys(); | ||
75 | printf("OUR ID: "); | ||
76 | uint32_t i; | ||
77 | |||
78 | for (i = 0; i < 32; i++) { | ||
79 | if (self_public_key[i] < 16) | ||
80 | printf("0"); | ||
81 | |||
82 | printf("%hhX", self_public_key[i]); | ||
83 | } | ||
84 | |||
85 | printf("\n"); | ||
86 | |||
87 | memcpy(self_client_id, self_public_key, 32); | ||
88 | |||
89 | char temp_id[128]; | ||
90 | printf("Enter the client_id of the friend to connect to (32 bytes HEX format):\n"); | ||
91 | scanf("%s", temp_id); | ||
92 | |||
93 | uint8_t friend_id[32]; | ||
94 | memcpy(friend_id, hex_string_to_bin(temp_id), 32); | ||
95 | |||
96 | /* memcpy(self_client_id, "qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq", 32); */ | ||
97 | |||
98 | |||
99 | DHT_addfriend(friend_id); | ||
100 | IP_Port friend_ip; | ||
101 | int connection = -1; | ||
102 | int inconnection = -1; | ||
103 | |||
104 | uint8_t acceptedfriend_public_key[crypto_box_PUBLICKEYBYTES]; | ||
105 | int friendrequest = -1; | ||
106 | uint8_t request_data[512]; | ||
107 | |||
108 | /* initialize networking | ||
109 | * bind to ip 0.0.0.0:PORT */ | ||
110 | IP ip; | ||
111 | ip.i = 0; | ||
112 | init_networking(ip, PORT); | ||
113 | initNetCrypto(); | ||
114 | |||
115 | perror("Initialization"); | ||
116 | IP_Port bootstrap_ip_port; | ||
117 | bootstrap_ip_port.port = htons(atoi(argv[2])); | ||
118 | bootstrap_ip_port.ip.i = inet_addr(argv[1]); | ||
119 | DHT_bootstrap(bootstrap_ip_port); | ||
120 | |||
121 | IP_Port ip_port; | ||
122 | uint8_t data[MAX_UDP_PACKET_SIZE]; | ||
123 | uint32_t length; | ||
124 | |||
125 | uint8_t buffer1[128]; | ||
126 | int read1 = 0; | ||
127 | uint8_t buffer2[128]; | ||
128 | int read2 = 0; | ||
129 | FILE *file1 = fopen(argv[3], "rb"); | ||
130 | |||
131 | if ( file1 == NULL ) { | ||
132 | printf("Error opening file.\n"); | ||
133 | return 1; | ||
134 | } | ||
135 | |||
136 | FILE *file2 = fopen("received.txt", "wb"); | ||
137 | |||
138 | if ( file2 == NULL ) { | ||
139 | return 1; | ||
140 | } | ||
141 | |||
142 | read1 = fread(buffer1, 1, 128, file1); | ||
143 | |||
144 | while (1) { | ||
145 | while (receivepacket(&ip_port, data, &length) != -1) { | ||
146 | if (rand() % 3 != 1) { /* simulate packet loss */ | ||
147 | if (DHT_handlepacket(data, length, ip_port) && LosslessUDP_handlepacket(data, length, ip_port)) { | ||
148 | /* if packet is not recognized */ | ||
149 | printf("Received unhandled packet with length: %u\n", length); | ||
150 | } else { | ||
151 | printf("Received handled packet with length: %u\n", length); | ||
152 | } | ||
153 | } | ||
154 | } | ||
155 | |||
156 | friend_ip = DHT_getfriendip(friend_id); | ||
157 | |||
158 | if (friend_ip.ip.i != 0) { | ||
159 | if (connection == -1 && friendrequest == -1) { | ||
160 | printf("Sending friend request to peer:"); | ||
161 | printip(friend_ip); | ||
162 | friendrequest = send_friendrequest(friend_id, friend_ip, (uint8_t *) "Hello World", 12); | ||
163 | /* connection = crypto_connect((uint8_t *)friend_id, friend_ip); */ | ||
164 | /* connection = new_connection(friend_ip); */ | ||
165 | } | ||
166 | |||
167 | if (check_friendrequest(friendrequest) == 1) { | ||
168 | printf("Started connecting to friend:"); | ||
169 | connection = crypto_connect(friend_id, friend_ip); | ||
170 | } | ||
171 | } | ||
172 | |||
173 | if (inconnection == -1) { | ||
174 | uint8_t secret_nonce[crypto_box_NONCEBYTES]; | ||
175 | uint8_t public_key[crypto_box_PUBLICKEYBYTES]; | ||
176 | uint8_t session_key[crypto_box_PUBLICKEYBYTES]; | ||
177 | inconnection = crypto_inbound(public_key, secret_nonce, session_key); | ||
178 | inconnection = accept_crypto_inbound(inconnection, acceptedfriend_public_key, secret_nonce, session_key); | ||
179 | |||
180 | /* inconnection = incoming_connection(); */ | ||
181 | if (inconnection != -1) { | ||
182 | printf("Someone connected to us:\n"); | ||
183 | /* printip(connection_ip(inconnection)); */ | ||
184 | } | ||
185 | } | ||
186 | |||
187 | if (handle_friendrequest(acceptedfriend_public_key, request_data) > 1) { | ||
188 | printf("RECEIVED FRIEND REQUEST: %s\n", request_data); | ||
189 | } | ||
190 | |||
191 | /* if someone connected to us write what he sends to a file | ||
192 | * also send him our file. */ | ||
193 | if (inconnection != -1) { | ||
194 | if (write_cryptpacket(inconnection, buffer1, read1)) { | ||
195 | printf("Wrote data1.\n"); | ||
196 | read1 = fread(buffer1, 1, 128, file1); | ||
197 | } | ||
198 | |||
199 | read2 = read_cryptpacket(inconnection, buffer2); | ||
200 | |||
201 | if (read2 != 0) { | ||
202 | printf("Received data1.\n"); | ||
203 | |||
204 | if (!fwrite(buffer2, read2, 1, file2)) { | ||
205 | printf("file write error1\n"); | ||
206 | } | ||
207 | |||
208 | if (read2 < 128) { | ||
209 | printf("Closed file1 %u\n", read2); | ||
210 | fclose(file2); | ||
211 | } | ||
212 | } | ||
213 | /* if buffer is empty and the connection timed out. */ | ||
214 | else if (is_cryptoconnected(inconnection) == 4) { | ||
215 | crypto_kill(inconnection); | ||
216 | } | ||
217 | } | ||
218 | |||
219 | /* if we are connected to a friend send him data from the file. | ||
220 | * also put what he sends us in a file. */ | ||
221 | if (is_cryptoconnected(connection) >= 3) { | ||
222 | if (write_cryptpacket(0, buffer1, read1)) { | ||
223 | printf("Wrote data2.\n"); | ||
224 | read1 = fread(buffer1, 1, 128, file1); | ||
225 | } | ||
226 | |||
227 | read2 = read_cryptpacket(0, buffer2); | ||
228 | |||
229 | if (read2 != 0) { | ||
230 | printf("Received data2.\n"); | ||
231 | |||
232 | if (!fwrite(buffer2, read2, 1, file2)) { | ||
233 | printf("file write error2\n"); | ||
234 | } | ||
235 | |||
236 | if (read2 < 128) { | ||
237 | printf("Closed file2 %u\n", read2); | ||
238 | fclose(file2); | ||
239 | } | ||
240 | } | ||
241 | /* if buffer is empty and the connection timed out. */ | ||
242 | else if (is_cryptoconnected(connection) == 4) { | ||
243 | crypto_kill(connection); | ||
244 | } | ||
245 | } | ||
246 | |||
247 | doDHT(); | ||
248 | doLossless_UDP(); | ||
249 | doNetCrypto(); | ||
250 | /*print_clientlist(); | ||
251 | *print_friendlist(); | ||
252 | *c_sleep(300); */ | ||
253 | c_sleep(1); | ||
254 | } | ||
255 | |||
256 | shutdown_networking(); | ||
257 | return 0; | ||
258 | } | ||