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