-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathtest_eventloop_tun.c
More file actions
575 lines (544 loc) · 18.4 KB
/
test_eventloop_tun.c
File metadata and controls
575 lines (544 loc) · 18.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
/* test_eventloop_tun.c
*
* Copyright (C) 2024 wolfSSL Inc.
*
* This file is part of wolfIP TCP/IP stack.
*
* wolfIP is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* wolfIP is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
*/
#include <stdio.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <pthread.h>
#include <stdlib.h>
#include <sys/time.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <sys/types.h>
#include "config.h"
#include "wolfip.h"
#define TEST_SIZE (4 * 1024)
#define BUFFER_SIZE TEST_SIZE
static int listen_fd = -1, client_fd = -1;
static int exit_ok = 0, exit_count = 0;
static uint8_t buf[TEST_SIZE];
static int tot_sent = 0;
static int tot_recv = 0;
static int wolfIP_closing = 0;
static int closed = 0;
static int conn_fd = -1;
static int client_connected = 0;
/* "Test pattern - -" 16 chars without trailing null. */
static const uint8_t test_pattern[16] = {0x54, 0x65, 0x73, 0x74, 0x20, 0x70,
0x61, 0x74, 0x74, 0x65, 0x72, 0x6e,
0x20, 0x2d, 0x20, 0x2d};
/* wolfIP: server side callback. */
static void server_cb(int fd, uint16_t event, void *arg)
{
int ret = 0;
if ((fd == listen_fd) && (event & CB_EVENT_READABLE) && (client_fd == -1)) {
client_fd = wolfIP_sock_accept((struct wolfIP *)arg, listen_fd, NULL, NULL);
if (client_fd > 0) {
printf("accept: %04x\n", client_fd);
}
} else if ((fd == client_fd) && (event & CB_EVENT_READABLE )) {
ret = wolfIP_sock_recvfrom((struct wolfIP *)arg, client_fd, buf, sizeof(buf), 0, NULL, NULL);
if (ret != -EAGAIN) {
if (ret < 0) {
printf("Recv error: %d\n", ret);
wolfIP_sock_close((struct wolfIP *)arg, client_fd);
} else if (ret == 0) {
printf("Client side closed the connection.\n");
wolfIP_sock_close((struct wolfIP *)arg, client_fd);
printf("Server: Exiting.\n");
exit_ok = 1;
} else if (ret > 0) {
printf("recv: %d, echoing back\n", ret);
tot_recv += ret;
}
}
}
if ((event & CB_EVENT_WRITABLE) || ((ret > 0) && !closed)) {
int snd_ret;
if ((tot_sent >= 4096) && wolfIP_closing) {
wolfIP_sock_close((struct wolfIP *)arg, client_fd);
printf("Server: I closed the connection.\n");
closed = 1;
exit_ok = 1;
}
if ((!closed) && (tot_sent < tot_recv)) {
snd_ret = wolfIP_sock_sendto((struct wolfIP *)arg, client_fd, buf + tot_sent, tot_recv - tot_sent, 0, NULL, 0);
if (snd_ret != -EAGAIN) {
if (snd_ret < 0) {
printf("Send error: %d\n", snd_ret);
wolfIP_sock_close((struct wolfIP *)arg, client_fd);
} else {
tot_sent += snd_ret;
printf("sent %d bytes\n", snd_ret);
if (tot_recv == tot_sent) {
tot_sent = 0;
tot_recv = 0;
}
}
}
}
}
if (event & CB_EVENT_CLOSED) {
printf("Closing %d, client fd: %d\n", fd, client_fd);
}
if ((fd == client_fd) && (event & CB_EVENT_CLOSED)) {
printf("Client side closed the connection (EVENT_CLOSED)\n");
wolfIP_sock_close((struct wolfIP *)arg, client_fd);
client_fd = -1;
printf("Server: Exiting.\n");
exit_ok = 1;
}
(void)arg;
}
/* Client-side callback. */
static void client_cb(int fd, uint16_t event, void *arg)
{
struct wolfIP *s = (struct wolfIP *)arg;
uint32_t i;
int ret;
static unsigned int total_r = 0, total_w = 0;
if (fd == conn_fd) {
if ((event & CB_EVENT_WRITABLE) && (client_connected == 0)) {
printf("Client: connected\n");
client_connected = 1;
}
}
if (total_w == 0) {
for (i = 0; i < sizeof(buf); i += sizeof(test_pattern)) {
memcpy(buf + i, test_pattern, sizeof(test_pattern));
}
}
if (client_connected && (event & CB_EVENT_WRITABLE) && (total_w < sizeof(buf))) {
ret = wolfIP_sock_sendto(s, fd, buf + total_w, sizeof(buf) - total_w, 0, NULL, 0);
if (ret <= 0) {
printf("Test client write: %d\n", ret);
return;
}
total_w += ret;
}
while ((total_r < total_w) && (event & CB_EVENT_READABLE)) {
ret = wolfIP_sock_recvfrom(s, fd, buf + total_r, sizeof(buf) - total_r, 0, NULL, NULL);
if (ret < 0){
if (ret != -EAGAIN) {
printf("Client read: %d\n", ret);
}
return;
}
if (ret == 0) {
printf("Client read: server has closed the connection.\n");
return;
}
total_r += ret;
printf("Client RX total: %u\n", total_r);
}
if (total_r == sizeof(buf)) {
exit_ok = 1;
for (i = 0; i < sizeof(buf); i += sizeof(test_pattern)) {
if (memcmp(buf + i, test_pattern, sizeof(test_pattern))) {
char slice[sizeof(test_pattern) + 1];
printf("test client: pattern mismatch\n");
printf("at position %u\n", i);
memcpy(slice, buf + i, sizeof(test_pattern));
slice[sizeof(test_pattern)] = '\0';
printf("%s\n", slice);
return;
}
}
if (wolfIP_closing) {
wolfIP_sock_close(s, fd);
conn_fd = -1;
}
printf("Test client: success\n");
}
}
/* wolfIP side: main loop of the stack under test. */
static int test_loop(struct wolfIP *s, int active_close)
{
exit_ok = 0;
exit_count = 0;
tot_sent = 0;
wolfIP_closing = active_close;
closed = 0;
while(1) {
uint32_t ms_next;
struct timeval tv;
gettimeofday(&tv, NULL);
ms_next = wolfIP_poll(s, tv.tv_sec * 1000 + tv.tv_usec / 1000);
usleep(ms_next * 1000);
if (exit_ok > 0) {
if (exit_count++ < 10)
continue;
else break;
}
}
return 0;
}
/* Test code (host side).
* Thread with client to test the echoserver.
*/
void *pt_echoclient(void *arg)
{
int fd, ret;
unsigned total_r = 0;
unsigned i;
uint8_t local_buf[BUFFER_SIZE];
uint32_t *srv_addr = (uint32_t *)arg;
int old_flags = -1;
fd_set wfds, rfds;
struct timeval tv;
socklen_t errlen;
int err;
struct sockaddr_in remote_sock = {
.sin_family = AF_INET,
.sin_port = htons(8), /* Echo */
};
remote_sock.sin_addr.s_addr = *srv_addr;
fd = socket(AF_INET, IPSTACK_SOCK_STREAM, 0);
if (fd < 0) {
printf("test client socket: %d\n", fd);
return (void *)-1;
}
sleep(1);
(void)setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &(int){1}, sizeof(int));
printf("Connecting to echo server\n");
old_flags = fcntl(fd, F_GETFL, 0);
if (old_flags < 0) {
perror("fcntl(F_GETFL)");
close(fd);
return (void *)-1;
}
if (fcntl(fd, F_SETFL, old_flags | O_NONBLOCK) < 0) {
perror("fcntl(F_SETFL)");
close(fd);
return (void *)-1;
}
ret = connect(fd, (struct sockaddr *)&remote_sock, sizeof(remote_sock));
if (ret < 0) {
err = errno;
printf("test client connect returned %d, errno=%d (%s)\n", ret, err, strerror(err));
if (err != EINPROGRESS) {
perror("connect");
close(fd);
return (void *)-1;
}
printf("Waiting for connect to complete...\n");
while (1) {
tv.tv_sec = 5;
tv.tv_usec = 0;
FD_ZERO(&rfds);
FD_ZERO(&wfds);
FD_SET(fd, &rfds);
FD_SET(fd, &wfds);
ret = select(fd + 1, &rfds, &wfds, NULL, &tv);
if (ret <= 0) {
printf("select returned %d (timeout or error)\n", ret);
if (ret < 0) {
perror("select");
close(fd);
return (void *)-1;
}
}
errlen = sizeof(err);
if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &errlen) < 0) {
perror("getsockopt(SO_ERROR)");
close(fd);
return (void *)-1;
}
if (err == 0) {
printf("connect completed after select()\n");
break;
}
if (ret == 0) {
printf("connect still in progress after timeout\n");
continue;
}
if (err != EINPROGRESS && err != EALREADY && err != EWOULDBLOCK && err != EAGAIN) {
printf("connect completed with error: %d (%s)\n", err, strerror(err));
close(fd);
return (void *)-1;
}
}
} else {
printf("connect returned immediately\n");
}
if (fcntl(fd, F_SETFL, old_flags) < 0)
perror("fcntl(restore)");
printf("test client: connect succeeded\n");
for (i = 0; i < sizeof(local_buf); i += sizeof(test_pattern)) {
memcpy(local_buf + i, test_pattern, sizeof(test_pattern));
}
ret = write(fd, local_buf, sizeof(local_buf));
if (ret < 0) {
int werr = errno;
printf("test client write: %d (errno=%d: %s)\n", ret, werr, strerror(werr));
perror("write");
return (void *)-1;
}
printf("test client: wrote %d bytes\n", ret);
while (total_r < sizeof(local_buf)) {
ret = read(fd, local_buf + total_r, sizeof(local_buf) - total_r);
if (ret < 0) {
printf("failed test client read: %d\n", ret);
return (void *)-1;
}
if (ret == 0) {
printf("test client read: server has closed the connection.\n");
if (wolfIP_closing)
return (void *)0;
else
return (void *)-1;
}
total_r += ret;
printf("test client: read %d bytes (total %u)\n", ret, total_r);
}
for (i = 0; i < sizeof(local_buf); i += sizeof(test_pattern)) {
if (memcmp(local_buf + i, test_pattern, sizeof(test_pattern))) {
char slice[sizeof(test_pattern) + 1];
printf("test client: pattern mismatch\n");
printf("at position %u\n", i);
memcpy(slice, local_buf + i, sizeof(test_pattern));
slice[sizeof(test_pattern)] = '\0';
printf("%s\n", slice);
return (void *)-1;
}
}
close(fd);
printf("Test client: success\n");
return (void *)0;
}
/* Test code (host side).
* Thread with echo server to test the client.
*/
static void *pt_echoserver(void *arg)
{
int fd, listen_fd, ret;
unsigned total_r = 0;
uint8_t local_buf[BUFFER_SIZE];
struct sockaddr_in local_sock = {
.sin_family = AF_INET,
.sin_port = htons(8), /* Echo */
.sin_addr.s_addr = 0
};
wolfIP_closing = (uintptr_t)arg;
fd = socket(AF_INET, IPSTACK_SOCK_STREAM, 0);
if (fd < 0) {
printf("test server socket: %d\n", fd);
return (void *)-1;
}
local_sock.sin_addr.s_addr = inet_addr(HOST_STACK_IP);
(void)setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &(int){1}, sizeof(int));
ret = bind(fd, (struct sockaddr *)&local_sock, sizeof(local_sock));
if (ret < 0) {
printf("test server bind: %d (%s)\n", ret, strerror(errno));
close(fd);
return (void *)-1;
}
ret = listen(fd, 1);
if (ret < 0) {
printf("test server listen: %d\n", ret);
close(fd);
return (void *)-1;
}
listen_fd = fd;
printf("Waiting for client\n");
ret = accept(fd, NULL, NULL);
if (ret < 0) {
printf("test server accept: %d\n", ret);
close(listen_fd);
return (void *)-1;
}
printf("test server: client %d connected\n", ret);
fd = ret;
while (1) {
ret = read(fd, local_buf + total_r, sizeof(local_buf) - total_r);
if (ret < 0) {
printf("failed test server read: %d (%s) \n", ret, strerror(errno));
close(fd);
close(listen_fd);
return (void *)-1;
}
if (ret == 0) {
printf("test server read: client has closed the connection.\n");
close(fd);
close(listen_fd);
if (wolfIP_closing)
return (void *)0;
else
return (void *)-1;
}
total_r += ret;
write(fd, local_buf + total_r - ret, ret);
}
}
/* Network device initialization: TUN (L3) */
#if defined(__linux__)
extern int tun_init(struct wolfIP_ll_dev *dev, const char *name,
uint32_t host_ip, uint32_t peer_ip);
#endif
/* Test cases */
void test_wolfip_echoserver(struct wolfIP *s, uint32_t srv_ip)
{
int ret, test_ret = 0;
pthread_t pt;
struct wolfIP_sockaddr_in local_sock = {
.sin_family = AF_INET,
.sin_port = ee16(8), /* Echo */
.sin_addr.s_addr = 0
};
printf("TCP server tests\n");
listen_fd = wolfIP_sock_socket(s, AF_INET, IPSTACK_SOCK_STREAM, 0);
printf("socket: %04x\n", listen_fd);
wolfIP_register_callback(s, listen_fd, server_cb, s);
pthread_create(&pt, NULL, pt_echoclient, &srv_ip);
printf("Starting test: echo server close-wait\n");
ret = wolfIP_sock_bind(s, listen_fd, (struct wolfIP_sockaddr *)&local_sock, sizeof(local_sock));
printf("bind: %d\n", ret);
ret = wolfIP_sock_listen(s, listen_fd, 1);
printf("listen: %d\n", ret);
ret = test_loop(s, 0);
pthread_join(pt, (void **)&test_ret);
printf("Test echo server close-wait: %d\n", ret);
printf("Test host client: %d\n", test_ret);
sleep(1);
pthread_create(&pt, NULL, pt_echoclient, &srv_ip);
printf("Starting test: echo server active close\n");
ret = test_loop(s, 1);
printf("Test echo server close-wait: %d\n", ret);
pthread_join(pt, (void **)&test_ret);
printf("Test host client: %d\n", test_ret);
sleep(1);
wolfIP_sock_close(s, listen_fd);
}
void test_wolfip_echoclient(struct wolfIP *s)
{
int ret, test_ret = 0;
pthread_t pt;
struct wolfIP_sockaddr_in remote_sock;
/* Client side test: client is closing the connection */
remote_sock.sin_family = AF_INET;
remote_sock.sin_port = ee16(8);
remote_sock.sin_addr.s_addr = inet_addr(HOST_STACK_IP);
printf("TCP client tests\n");
conn_fd = wolfIP_sock_socket(s, AF_INET, IPSTACK_SOCK_STREAM, 0);
printf("client socket: %04x\n", conn_fd);
wolfIP_register_callback(s, conn_fd, client_cb, s);
printf("Connecting to %s:8\n", HOST_STACK_IP);
wolfIP_sock_connect(s, conn_fd, (struct wolfIP_sockaddr *)&remote_sock, sizeof(remote_sock));
pthread_create(&pt, NULL, pt_echoserver, (void*)1);
printf("Starting test: echo client active close\n");
ret = test_loop(s, 1);
printf("Test echo client active close: %d\n", ret);
pthread_join(pt, (void **)&test_ret);
printf("Test host server: %d\n", test_ret);
if (conn_fd >= 0) {
wolfIP_sock_close(s, conn_fd);
conn_fd = -1;
}
/* Client side test: server is closing the connection */
/* Excluded for now because binding twice on port 8 is not supported */
#if 0
conn_fd = wolfIP_sock_socket(s, AF_INET, IPSTACK_SOCK_STREAM, 0);
if (conn_fd < 0) {
printf("cannot create socket: %d\n", conn_fd);
}
printf("client socket: %04x\n", conn_fd);
wolfIP_register_callback(s, conn_fd, client_cb, s);
printf("Connecting to %s:8\n", HOST_STACK_IP);
wolfIP_sock_connect(s, conn_fd, (struct wolfIP_sockaddr *)&remote_sock, sizeof(remote_sock));
pthread_create(&pt, NULL, pt_echoserver, (void*)0);
printf("Starting test: echo client passive close\n");
ret = test_loop(s, 0);
printf("Test echo client, server closing: %d\n", ret);
pthread_join(pt, (void **)&test_ret);
printf("Test host server: %d\n", test_ret);
#endif
}
/* Main test function. */
int main(int argc, char **argv)
{
struct wolfIP *s;
struct wolfIP_ll_dev *tapdev;
struct timeval tv = {0, 0};
struct in_addr host_stack_ip;
struct in_addr peer_stack_ip;
uint32_t srv_ip;
ip4 ip = 0, nm = 0, gw = 0;
(void)argc;
(void)argv;
(void)ip;
(void)nm;
(void)gw;
(void)tv;
wolfIP_init_static(&s);
tapdev = wolfIP_getdev(s);
if (!tapdev)
return 1;
inet_aton(HOST_STACK_IP, &host_stack_ip);
inet_aton(WOLFIP_IP, &peer_stack_ip);
#if defined(__linux__)
if (tun_init(tapdev, "wtun0", host_stack_ip.s_addr, peer_stack_ip.s_addr) < 0) {
perror("tun init");
return 2;
}
#else
fprintf(stderr, "test_eventloop_tun is supported only on Linux.\n");
return 2;
#endif
{
#if !defined(__FreeBSD__) && !defined(__APPLE__)
char cmd[128];
snprintf(cmd, sizeof(cmd), "tcpdump -i %s -w test.pcap &", tapdev->ifname);
system(cmd);
#else
(void)tapdev;
#endif
}
#ifdef DHCP
gettimeofday(&tv, NULL);
wolfIP_poll(s, tv.tv_sec * 1000 + tv.tv_usec / 1000);
dhcp_client_init(s);
do {
gettimeofday(&tv, NULL);
wolfIP_poll(s, tv.tv_sec * 1000 + tv.tv_usec / 1000);
usleep(1000);
wolfIP_ipconfig_get(s, &ip, &nm, &gw);
} while (!dhcp_bound(s));
printf("DHCP: obtained IP address.\n");
wolfIP_ipconfig_get(s, &ip, &nm, &gw);
srv_ip = htonl(ip);
#else
wolfIP_ipconfig_set(s, atoip4(WOLFIP_IP), atoip4("255.255.255.255"),
atoip4(HOST_STACK_IP));
printf("IP: manually configured\n");
inet_pton(AF_INET, WOLFIP_IP, &srv_ip);
#endif
/* Server side test */
test_wolfip_echoserver(s, srv_ip);
/* Client side test */
test_wolfip_echoclient(s);
#if !defined(__FreeBSD__) && !defined(__APPLE__)
system("killall tcpdump");
#endif
return 0;
}