diff options
Diffstat (limited to 'testing/Lossless_UDP_testserver.c')
-rw-r--r-- | testing/Lossless_UDP_testserver.c | 237 |
1 files changed, 0 insertions, 237 deletions
diff --git a/testing/Lossless_UDP_testserver.c b/testing/Lossless_UDP_testserver.c deleted file mode 100644 index dd4612f5..00000000 --- a/testing/Lossless_UDP_testserver.c +++ /dev/null | |||
@@ -1,237 +0,0 @@ | |||
1 | /* Lossless_UDP testserver | ||
2 | * A program that waits for a lossless UDP connection and then saves all the data received to a file. | ||
3 | * NOTE: this program simulates a 33% packet loss. | ||
4 | * | ||
5 | * Best used in combination with Lossless_UDP_testclient | ||
6 | * | ||
7 | * Compile with: gcc -O2 -Wall -lsodium -o testserver ../toxcore/network.c ../toxcore/Lossless_UDP.c ../toxcore/util.c Lossless_UDP_testserver.c | ||
8 | * | ||
9 | * Command line argument is the name of the file to save what we receive to. | ||
10 | * EX: ./testserver filename1.txt | ||
11 | * | ||
12 | * Copyright (C) 2013 Tox project All Rights Reserved. | ||
13 | * | ||
14 | * This file is part of Tox. | ||
15 | * | ||
16 | * Tox is free software: you can redistribute it and/or modify | ||
17 | * it under the terms of the GNU General Public License as published by | ||
18 | * the Free Software Foundation, either version 3 of the License, or | ||
19 | * (at your option) any later version. | ||
20 | * | ||
21 | * Tox is distributed in the hope that it will be useful, | ||
22 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
23 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
24 | * GNU General Public License for more details. | ||
25 | * | ||
26 | * You should have received a copy of the GNU General Public License | ||
27 | * along with Tox. If not, see <http://www.gnu.org/licenses/>. | ||
28 | * | ||
29 | */ | ||
30 | |||
31 | #ifdef HAVE_CONFIG_H | ||
32 | #include "config.h" | ||
33 | #endif | ||
34 | |||
35 | #include "../toxcore/network.h" | ||
36 | #include "../toxcore/Lossless_UDP.h" | ||
37 | #include "../toxcore/util.h" | ||
38 | #include "misc_tools.c" | ||
39 | |||
40 | //Sleep function (x = milliseconds) | ||
41 | #if defined(_WIN32) || defined(__WIN32__) || defined (WIN32) | ||
42 | |||
43 | #define c_sleep(x) Sleep(1*x) | ||
44 | |||
45 | #else | ||
46 | #include <unistd.h> | ||
47 | #include <arpa/inet.h> | ||
48 | #define c_sleep(x) usleep(1000*x) | ||
49 | |||
50 | #endif | ||
51 | |||
52 | #define PORT 33445 | ||
53 | |||
54 | void printpacket(uint8_t *data, uint32_t length, IP_Port ip_port) | ||
55 | { | ||
56 | uint32_t i; | ||
57 | printf("UNHANDLED PACKET RECEIVED\nLENGTH:%u\nCONTENTS:\n", length); | ||
58 | printf("--------------------BEGIN-----------------------------\n"); | ||
59 | |||
60 | for (i = 0; i < length; i++) { | ||
61 | if (data[i] < 16) | ||
62 | printf("0"); | ||
63 | |||
64 | printf("%hhX", data[i]); | ||
65 | } | ||
66 | |||
67 | printf("\n--------------------END-----------------------------\n\n\n"); | ||
68 | } | ||
69 | |||
70 | /* | ||
71 | void printpackets(Data test) | ||
72 | { | ||
73 | int i; | ||
74 | if(test.size == 0) | ||
75 | return; | ||
76 | printf("SIZE: %u\n", test.size); | ||
77 | for(i =0; i < test.size; i++) | ||
78 | { | ||
79 | printf("%hhX", test.data[i]); | ||
80 | } | ||
81 | printf("\n"); | ||
82 | } | ||
83 | |||
84 | void printconnection(int connection_id) | ||
85 | { | ||
86 | printf("--------------------BEGIN---------------------\n"); | ||
87 | IP_Port ip_port = connections[connection_id].ip_port; | ||
88 | printf("IP: %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)); | ||
89 | printf("status: %u, inbound: %u, SYNC_rate: %u\n", connections[connection_id].status, | ||
90 | connections[connection_id].inbound, connections[connection_id].SYNC_rate); | ||
91 | printf("data rate: %u, last sync: %llu, last sent: %llu, last recv: %llu \n", connections[connection_id].data_rate, | ||
92 | connections[connection_id].last_SYNC, connections[connection_id].last_sent, connections[connection_id].last_recv); | ||
93 | int i; | ||
94 | for(i =0; i < MAX_QUEUE_NUM; i++) | ||
95 | { | ||
96 | printf(" %u ",i); | ||
97 | printpackets(connections[connection_id].sendbuffer[i]); | ||
98 | } | ||
99 | for(i =0; i < MAX_QUEUE_NUM; i++) | ||
100 | { | ||
101 | printf(" %u ",i); | ||
102 | printpackets(connections[connection_id].recvbuffer[i]); | ||
103 | } | ||
104 | Data sendbuffer[MAX_QUEUE_NUM]; | ||
105 | Data recvbuffer[MAX_QUEUE_NUM]; | ||
106 | printf("recv_num: %u, orecv_num: %u, sent_packetnum %u, osent_packetnum: %u, successful_sent: %u, successful_read: %u\n", | ||
107 | connections[connection_id].recv_packetnum, | ||
108 | connections[connection_id].orecv_packetnum, connections[connection_id].sent_packetnum, connections[connection_id].osent_packetnum, | ||
109 | connections[connection_id].successful_sent, | ||
110 | connections[connection_id].successful_read); | ||
111 | |||
112 | printf("req packets: \n"); | ||
113 | for(i = 0; i < BUFFER_PACKET_NUM; i++) | ||
114 | { | ||
115 | printf(" %u ", connections[connection_id].req_packets[i]); | ||
116 | } | ||
117 | printf("\nNumber: %u recv_counter: %u, send_counter: %u\n", connections[connection_id].num_req_paquets, | ||
118 | connections[connection_id].recv_counter, connections[connection_id].send_counter); | ||
119 | |||
120 | printf("--------------------END---------------------\n"); | ||
121 | |||
122 | } | ||
123 | */ | ||
124 | |||
125 | /* receive packets and send them to the packethandler | ||
126 | * run doLossless_UDP(); */ | ||
127 | //void Lossless_UDP() | ||
128 | //{ | ||
129 | // IP_Port ip_port; | ||
130 | // uint8_t data[MAX_UDP_PACKET_SIZE]; | ||
131 | // uint32_t length; | ||
132 | // while (receivepacket(&ip_port, data, &length) != -1) { | ||
133 | //if(rand() % 3 != 1)//add packet loss | ||
134 | //{ | ||
135 | // if (LosslessUDP_handlepacket(data, length, ip_port)) { | ||
136 | // printpacket(data, length, ip_port); | ||
137 | // } else { | ||
138 | //printconnection(0); | ||
139 | // printf("Received handled packet with length: %u\n", length); | ||
140 | // } | ||
141 | //} | ||
142 | // } | ||
143 | |||
144 | // networking_poll(); | ||
145 | |||
146 | //doLossless_UDP(); | ||
147 | //} | ||
148 | |||
149 | |||
150 | int main(int argc, char *argv[]) | ||
151 | { | ||
152 | /* let user override default by cmdline */ | ||
153 | uint8_t ipv6enabled = TOX_ENABLE_IPV6_DEFAULT; /* x */ | ||
154 | int argvoffset = cmdline_parsefor_ipv46(argc, argv, &ipv6enabled); | ||
155 | |||
156 | if (argvoffset < 0) | ||
157 | exit(1); | ||
158 | |||
159 | if (argc < argvoffset + 2) { | ||
160 | printf("Usage: %s [--ipv4|--ipv6] filename\n", argv[0]); | ||
161 | exit(0); | ||
162 | } | ||
163 | |||
164 | uint8_t buffer[MAX_DATA_SIZE]; | ||
165 | int read; | ||
166 | |||
167 | FILE *file = fopen(argv[argvoffset + 1], "wb"); | ||
168 | |||
169 | if (file == NULL) { | ||
170 | printf("Failed to open file \"%s\".\n", argv[argvoffset + 1]); | ||
171 | return 1; | ||
172 | } | ||
173 | |||
174 | |||
175 | //initialize networking | ||
176 | //bind to ip 0.0.0.0:PORT | ||
177 | IP ip; | ||
178 | ip_init(&ip, ipv6enabled); | ||
179 | |||
180 | Lossless_UDP *ludp = new_lossless_udp(new_networking(ip, PORT)); | ||
181 | perror("Initialization"); | ||
182 | |||
183 | int connection; | ||
184 | uint64_t timer = current_time(); | ||
185 | |||
186 | while (1) { | ||
187 | networking_poll(ludp->net); | ||
188 | do_lossless_udp(ludp); | ||
189 | connection = incoming_connection(ludp, 0); | ||
190 | |||
191 | if (connection != -1) { | ||
192 | if (is_connected(ludp, connection) == LUDP_NOT_CONFIRMED) { | ||
193 | printf("Received the connection.\n"); | ||
194 | |||
195 | } | ||
196 | |||
197 | break; | ||
198 | } | ||
199 | |||
200 | c_sleep(1); | ||
201 | } | ||
202 | |||
203 | timer = current_time(); | ||
204 | |||
205 | while (1) { | ||
206 | //printconnection(0); | ||
207 | networking_poll(ludp->net); | ||
208 | |||
209 | if (is_connected(ludp, connection) >= LUDP_NOT_CONFIRMED) { | ||
210 | confirm_connection(ludp, connection); | ||
211 | |||
212 | while (1) { | ||
213 | read = read_packet(ludp, connection, buffer); | ||
214 | |||
215 | if (read != 0) { | ||
216 | // printf("Received data.\n"); | ||
217 | if (!fwrite(buffer, read, 1, file)) | ||
218 | printf("file write error\n"); | ||
219 | } else { | ||
220 | break; | ||
221 | } | ||
222 | } | ||
223 | } | ||
224 | |||
225 | do_lossless_udp(ludp); | ||
226 | |||
227 | if (is_connected(ludp, connection) == LUDP_TIMED_OUT) { | ||
228 | printf("Server Connecting Lost after: %llu us\n", (unsigned long long)(current_time() - timer)); | ||
229 | fclose(file); | ||
230 | return 1; | ||
231 | } | ||
232 | |||
233 | c_sleep(25); | ||
234 | } | ||
235 | |||
236 | return 0; | ||
237 | } | ||