summaryrefslogtreecommitdiff
path: root/testing/DHT_sendfiletest.c
diff options
context:
space:
mode:
Diffstat (limited to 'testing/DHT_sendfiletest.c')
-rw-r--r--testing/DHT_sendfiletest.c196
1 files changed, 0 insertions, 196 deletions
diff --git a/testing/DHT_sendfiletest.c b/testing/DHT_sendfiletest.c
deleted file mode 100644
index f839be57..00000000
--- a/testing/DHT_sendfiletest.c
+++ /dev/null
@@ -1,196 +0,0 @@
1/* DHT sendfiletest
2 *
3 * Sends the data from a file to another client.
4 * Receives the file data that that client sends us.
5 *
6 * NOTE: this program simulates 33% packet loss.
7 *
8 * Compile with: gcc -O2 -Wall -o test ../core/DHT.c ../core/network.c ../core/Lossless_UDP.c DHT_sendfiletest.c
9 *
10 * Command line arguments are the ip and port of a node (for bootstrapping), the
11 * client_id (32 bytes) of the friend you want to send the data in filename to and
12 * the client_id this node will take.
13 *
14 * Saves all received data to: received.txt
15 *
16 * EX: ./test 127.0.0.1 33445 ABCDEFGHIJKLMNOPQRSTUVWXYZabcdef filename.txt ABCDEFGHIJKLMNOPQRSTUVWXYZabcdeg
17 *
18 * Copyright (C) 2013 Tox project All Rights Reserved.
19 *
20 * This file is part of Tox.
21 *
22 * Tox is free software: you can redistribute it and/or modify
23 * it under the terms of the GNU General Public License as published by
24 * the Free Software Foundation, either version 3 of the License, or
25 * (at your option) any later version.
26 *
27 * Tox is distributed in the hope that it will be useful,
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 * GNU General Public License for more details.
31 *
32 * You should have received a copy of the GNU General Public License
33 * along with Tox. If not, see <http://www.gnu.org/licenses/>.
34 *
35 */
36
37#include "../core/network.h"
38#include "../core/DHT.h"
39#include "../core/Lossless_UDP.h"
40
41#include <string.h>
42
43//Sleep function (x = milliseconds)
44#ifdef WIN32
45
46#define c_sleep(x) Sleep(1*x)
47
48#else
49#include <unistd.h>
50#include <arpa/inet.h>
51#define c_sleep(x) usleep(1000*x)
52
53#endif
54
55#define PORT 33445
56
57void printip(IP_Port ip_port)
58{
59 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],
60 ntohs(ip_port.port));
61}
62
63int main(int argc, char *argv[])
64{
65 //memcpy(self_client_id, "qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq", 32);
66
67 if (argc < 6) {
68 printf("usage %s ip port client_id(of friend to find ip_port of) filename(of file to send) client_id(ours)\n", argv[0]);
69 exit(0);
70 }
71
72 DHT_addfriend((uint8_t *)argv[3]);
73 IP_Port friend_ip;
74 int connection = -1;
75 int inconnection = -1;
76
77 //initialize networking
78 //bind to ip 0.0.0.0:PORT
79 IP ip;
80 ip.i = 0;
81 init_networking(ip, PORT);
82
83 memcpy(self_client_id, argv[5], 32);
84
85
86 perror("Initialization");
87 IP_Port bootstrap_ip_port;
88 bootstrap_ip_port.port = htons(atoi(argv[2]));
89 bootstrap_ip_port.ip.i = inet_addr(argv[1]);
90 DHT_bootstrap(bootstrap_ip_port);
91
92 IP_Port ip_port;
93 uint8_t data[MAX_UDP_PACKET_SIZE];
94 uint32_t length;
95
96 uint8_t buffer1[128];
97 int read1 = 0;
98 uint8_t buffer2[128];
99 int read2 = 0;
100 FILE *file1 = fopen(argv[4], "rb");
101
102 if (file1 == NULL) {
103 printf("Error opening file.\n");
104 return 1;
105 }
106
107 FILE *file2 = fopen("received.txt", "wb");
108
109 if (file2 == NULL)
110 return 1;
111
112 read1 = fread(buffer1, 1, 128, file1);
113
114 while (1) {
115 while (receivepacket(&ip_port, data, &length) != -1) {
116 if (rand() % 3 != 1) { /* simulate packet loss */
117 if (DHT_handlepacket(data, length, ip_port) && LosslessUDP_handlepacket(data, length, ip_port))
118 printf("Received unhandled packet with length: %u\n", length); /* if packet is not recognized */
119 else
120 printf("Received handled packet with length: %u\n", length);
121 }
122 }
123
124 friend_ip = DHT_getfriendip((uint8_t *)argv[3]);
125
126 if (friend_ip.ip.i != 0) {
127 if (connection == -1) {
128 printf("Started connecting to friend:");
129 printip(friend_ip);
130 connection = new_connection(friend_ip);
131 }
132 }
133
134 if (inconnection == -1) {
135 inconnection = incoming_connection();
136
137 if (inconnection != -1) {
138 printf("Someone connected to us:");
139 printip(connection_ip(inconnection));
140 }
141 }
142
143 /* if someone connected to us write what he sends to a file */
144 /* also send him our file. */
145 if (inconnection != -1) {
146 if (write_packet(inconnection, buffer1, read1)) {
147 printf("Wrote data.\n");
148 read1 = fread(buffer1, 1, 128, file1);
149 }
150
151 read2 = read_packet(inconnection, buffer2);
152
153 if (read2 != 0) {
154 printf("Received data.\n");
155
156 if (!fwrite(buffer2, read2, 1, file2))
157 printf("file write error\n");
158
159 if (read2 < 128) {
160 fclose(file2);
161 }
162 }
163 }
164
165 /* if we are connected to a friend send him data from the file.
166 * also put what he sends us in a file. */
167 if (is_connected(connection) == 3) {
168 if (write_packet(0, buffer1, read1)) {
169 printf("Wrote data.\n");
170 read1 = fread(buffer1, 1, 128, file1);
171 }
172
173 read2 = read_packet(0, buffer2);
174
175 if (read2 != 0) {
176 printf("Received data.\n");
177
178 if (!fwrite(buffer2, read2, 1, file2))
179 printf("file write error\n");
180
181 if (read2 < 128)
182 fclose(file2);
183 }
184 }
185
186 doDHT();
187 doLossless_UDP();
188 /* print_clientlist();
189 * print_friendlist();
190 * c_sleep(300); */
191 c_sleep(1);
192 }
193
194 shutdown_networking();
195 return 0;
196} \ No newline at end of file