-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_ipk_rdt.c
More file actions
2010 lines (1834 loc) · 64.3 KB
/
Copy pathtest_ipk_rdt.c
File metadata and controls
2010 lines (1834 loc) · 64.3 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
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*******************************************************************************
* *
* Brno University of Technology *
* Faculty of Information Technology *
* *
* Počítačové komunikace a sítě *
* *
* Author: Hugo Bohacsek [xbohach00 AT stud.fit.vutbr.cz] *
* Brno 2026 *
* *
* Implementation of the 2nd project impairment proxy for testing *
* *
*******************************************************************************/
#define _DEFAULT_SOURCE
#define _POSIX_C_SOURCE 200809L
#include <arpa/inet.h>
#include <ctype.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <netinet/in.h>
#include <pthread.h>
#include <signal.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <time.h>
#include <unistd.h>
#define PROXY_BUF_SZ 65535
#define MAX_MSG 512
#define POLL_US 200000
// Terminal colors
static int g_color;
#define CB_N 16
#define CB_SZ 512
static char g_cb[CB_N][CB_SZ];
static int g_cbi;
static char *cb(void) {
char *b = g_cb[g_cbi]; g_cbi = (g_cbi + 1) % CB_N;
return b;
}
static char *_a(const char *code, const char *t) {
char *b = cb();
if (g_color) {
snprintf(b, CB_SZ, "\033[%sm%s\033[0m", code, t);
} else {
snprintf(b, CB_SZ, "%s", t);
}
return b;
}
#define DIM(t) _a("2", (t))
#define BOLD(t) _a("1", (t))
#define RED(t) _a("1;31", (t))
#define GREEN(t) _a("1;32", (t))
#define YELLOW(t) _a("1;33", (t))
#define BLUE(t) _a("1;34", (t))
#define CYAN(t) _a("36", (t))
// sha256
#define ROR(x,n) (((x)>>(n))|((x)<<(32-(n))))
#define CH(e,f,g) (((e)&(f))^(~(e)&(g)))
#define MAJ(a,b,c) (((a)&(b))^((a)&(c))^((b)&(c)))
#define S0(a) (ROR(a,2)^ROR(a,13)^ROR(a,22))
#define S1(e) (ROR(e,6)^ROR(e,11)^ROR(e,25))
#define G0(w) (ROR(w,7)^ROR(w,18)^((w)>>3))
#define G1(w) (ROR(w,17)^ROR(w,19)^((w)>>10))
static const uint32_t K64[64] = {
0x428a2f98,0x71374491,0xb5c0fbcf,0xe9b5dba5,0x3956c25b,0x59f111f1,0x923f82a4,0xab1c5ed5,
0xd807aa98,0x12835b01,0x243185be,0x550c7dc3,0x72be5d74,0x80deb1fe,0x9bdc06a7,0xc19bf174,
0xe49b69c1,0xefbe4786,0x0fc19dc6,0x240ca1cc,0x2de92c6f,0x4a7484aa,0x5cb0a9dc,0x76f988da,
0x983e5152,0xa831c66d,0xb00327c8,0xbf597fc7,0xc6e00bf3,0xd5a79147,0x06ca6351,0x14292967,
0x27b70a85,0x2e1b2138,0x4d2c6dfc,0x53380d13,0x650a7354,0x766a0abb,0x81c2c92e,0x92722c85,
0xa2bfe8a1,0xa81a664b,0xc24b8b70,0xc76c51a3,0xd192e819,0xd6990624,0xf40e3585,0x106aa070,
0x19a4c116,0x1e376c08,0x2748774c,0x34b0bcb5,0x391c0cb3,0x4ed8aa4a,0x5b9cca4f,0x682e6ff3,
0x748f82ee,0x78a5636f,0x84c87814,0x8cc70208,0x90befffa,0xa4506ceb,0xbef9a3f7,0xc67178f2
};
typedef struct {
uint32_t h[8];
uint8_t b[64];
uint64_t tot;
uint32_t bl;
uint32_t pad;
} S256;
static void s_init(S256 *c) {
c->h[0]=0x6a09e667; c->h[1]=0xbb67ae85; c->h[2]=0x3c6ef372; c->h[3]=0xa54ff53a;
c->h[4]=0x510e527f; c->h[5]=0x9b05688c; c->h[6]=0x1f83d9ab; c->h[7]=0x5be0cd19;
c->tot = c->bl = 0;
c->pad = 0;
}
static void s_blk(S256 *c, const uint8_t *p) {
uint32_t w[64], s[8];
for (int i = 0; i < 16; i++) {
w[i] = (uint32_t)p[i*4]<<24 | (uint32_t)p[i*4+1]<<16 | (uint32_t)p[i*4+2]<<8 | p[i*4+3];
}
for (int i = 16; i < 64; i++) {
w[i] = G1(w[i-2]) + w[i-7] + G0(w[i-15]) + w[i-16];
}
for (int i = 0; i < 8; i++) {
s[i] = c->h[i];
}
for (int i = 0; i < 64; i++) {
uint32_t t1 = s[7] + S1(s[4]) + CH(s[4],s[5],s[6]) + K64[i] + w[i];
uint32_t t2 = S0(s[0]) + MAJ(s[0],s[1],s[2]);
s[7]=s[6]; s[6]=s[5]; s[5]=s[4]; s[4]=s[3]+t1;
s[3]=s[2]; s[2]=s[1]; s[1]=s[0]; s[0]=t1+t2;
}
for (int i = 0; i < 8; i++) {
c->h[i] += s[i];
}
}
static void s_feed(S256 *c, const uint8_t *data, size_t n) {
c->tot += n;
while (n > 0) {
size_t r = 64 - c->bl, take = n < r ? n : r;
memcpy(c->b + c->bl, data, take);
c->bl += (uint32_t)take; data += take; n -= take;
if (c->bl == 64) {
s_blk(c, c->b); c->bl = 0;
}
}
}
static void s_done(S256 *c, uint8_t *dig) {
uint64_t bits = c->tot * 8;
uint8_t z = 0x80; s_feed(c, &z, 1); z = 0;
while (c->bl != 56) s_feed(c, &z, 1);
for (int i = 7; i >= 0; i--) {
uint8_t byt = (uint8_t)(bits >> (i*8)); s_feed(c, &byt, 1);
}
for (int i = 0; i < 8; i++) {
dig[i*4] = (uint8_t)(c->h[i] >> 24); dig[i*4+1] = (uint8_t)(c->h[i] >> 16);
dig[i*4+2] = (uint8_t)(c->h[i] >> 8); dig[i*4+3] = (uint8_t) c->h[i];
}
}
static void sha256hex(const uint8_t *data, size_t len, char *out) {
S256 c; s_init(&c); s_feed(&c, data, len);
uint8_t d[32]; s_done(&c, d);
for (int i = 0; i < 32; i++) {
sprintf(out + i*2, "%02x", d[i]);
}
out[64] = '\0';
}
// time
static double now(void) {
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return (double)ts.tv_sec + (double)ts.tv_nsec * 1e-9;
}
// impairment, TestDef and test table
typedef struct {
float loss_pct, dup_pct, reorder_pct;
int reorder_delay_ms, jitter_ms;
float corrupt_pct;
int delay_ms;
float trunc_pct;
int hostile_n;
int oversized_n;
} Impairment;
typedef struct {
const char *name;
const char *desc;
long input_size;
Impairment imp;
int timeout_w;
int session_timeout;
int repeat;
// cppcheck-suppress unusedStructMember
int _pad;
} TestDef;
// fancy utf chars
#define UTF_ARROW "\xe2\x86\x92"
#define UTF_PM "\xc2\xb1"
#define UTF_DASH "\xe2\x80\x94"
static void appendf(char *dst, size_t cap, size_t *used, const char *fmt, ...) {
if (!dst || !used || cap == 0 || *used >= cap - 1) {
return;
}
va_list ap;
va_start(ap, fmt);
int wr = vsnprintf(dst + *used, cap - *used, fmt, ap);
va_end(ap);
if (wr <= 0) {
return;
}
size_t wrote = (size_t)wr;
size_t rem = cap - *used;
if (wrote >= rem) {
*used = cap - 1;
} else {
*used += wrote;
}
}
static const char *imp_desc(const Impairment *m, char *buf, size_t sz) {
char t[256];
size_t n = 0;
t[0] = '\0';
if (m->loss_pct > 0) appendf(t, sizeof t, &n, "%sloss=%.0f%%", n ? ", " : "", (double)m->loss_pct);
if (m->dup_pct > 0) appendf(t, sizeof t, &n, "%sdup=%.0f%%", n ? ", " : "", (double)m->dup_pct);
if (m->reorder_pct > 0) appendf(t, sizeof t, &n, "%sreorder=%.0f%%/%dms", n ? ", " : "", (double)m->reorder_pct, m->reorder_delay_ms);
if (m->jitter_ms > 0) appendf(t, sizeof t, &n, "%sjitter=" UTF_PM "%dms", n ? ", " : "", m->jitter_ms);
if (m->corrupt_pct > 0) appendf(t, sizeof t, &n, "%scorrupt=%.0f%%", n ? ", " : "", (double)m->corrupt_pct);
if (m->trunc_pct > 0) appendf(t, sizeof t, &n, "%struncate=%.0f%%", n ? ", " : "", (double)m->trunc_pct);
if (m->delay_ms > 0) appendf(t, sizeof t, &n, "%sdelay=%dms", n ? ", " : "", m->delay_ms);
if (m->hostile_n > 0) appendf(t, sizeof t, &n, "%shostile=%d", n ? ", " : "", m->hostile_n);
if (m->oversized_n > 0) appendf(t, sizeof t, &n, "%soversized=%d", n ? ", " : "", m->oversized_n);
snprintf(buf, sz, "%s", n ? t : "clean");
return buf;
}
// initialiser - loss, dup, reorder, reorder_ms, jitter, corrupt, delay, trunc, hostile, oversized
#define IMPX(l,d,r,rm,j,c,dl,tr,ho,ov) {(float)(l),(float)(d),(float)(r),(rm),(j),(float)(c),(dl),(float)(tr),(ho),(ov)}
#define IMP(l,d,r,rm,j,c,dl) IMPX(l,d,r,rm,j,c,dl,0,0,0)
#define CLEAN IMP(0,0,0,50,0,0,0)
#define TDEF(name,desc,imp,size,tw,to,rep) {(name),(desc),(size),imp,(tw),(to),(rep),0}
static const TestDef TESTS[] = {
TDEF("normal", "Clean channel, small file", CLEAN, 50000, 10, 60, 1),
TDEF("normal_large", "Clean channel, 200 KB", CLEAN, 200000, 10, 60, 1),
TDEF("empty", "Empty file (0 bytes)", CLEAN, 0, 10, 60, 1),
TDEF("tiny", "Single byte", CLEAN, 1, 10, 60, 1),
TDEF("binary", "Binary data (all byte values)", CLEAN, 0, 10, 30, 1),
TDEF("loss_5", "5% packet loss", IMP(5,0,0,50,0,0,0), 80000, 10, 60, 1),
TDEF("loss_15", "15% packet loss", IMP(15,0,0,50,0,0,0), 80000, 10, 60, 1),
TDEF("loss_30", "30% packet loss (stress)", IMP(30,0,0,50,0,0,0), 50000, 15, 90, 1),
TDEF("reorder", "20% reorder, 80 ms delay", IMP(0,0,20,80,0,0,0), 80000, 10, 60, 1),
TDEF("dup", "15% duplication", IMP(0,15,0,50,0,0,0), 80000, 10, 60, 1),
TDEF("corrupt", "10% corruption", IMP(0,0,0,50,0,10,0), 80000, 10, 60, 1),
TDEF("jitter", UTF_PM "50 ms jitter, 20 ms base delay", IMP(0,0,0,50,50,0,20), 80000, 10, 60, 1),
TDEF("delay", "Fixed 100 ms delay each way", IMP(0,0,0,50,0,0,100), 50000, 10, 60, 1),
TDEF("combined", "loss=10% dup=8% reorder=10% corrupt=5% jitter=" UTF_PM "30ms", IMP(10,8,10,50,30,5,10), 60000, 15, 90, 1),
TDEF("timeout_test", "40% loss + 200 ms delay (timeout stress)", IMP(40,0,0,50,0,0,200), 30000, 15, 90, 1),
TDEF("large_1mb", "1 MB, 5% loss", IMP(5,0,0,50,0,0,0), 1000000, 15, 120, 1),
TDEF("large_5mb", "5 MB, 3% loss", IMP(3,0,0,50,0,0,0), 5000000, 20, 180, 1),
TDEF("stdin_stdout", "stdin" UTF_ARROW "stdout pipe transfer", CLEAN, 30000, 10, 60, 1),
TDEF("ipv6", "IPv6 loopback transfer", CLEAN, 30000, 10, 60, 1),
TDEF("signal", "SIGTERM during idle " UTF_DASH " clean exit", CLEAN, 0, 10, 10, 1),
TDEF("bad_args", "Invalid CLI arguments " UTF_ARROW " non-zero exit", CLEAN, 0, 10, 60, 1),
TDEF("corrupt_only", "A-CORRUPT: 3% corruption, 96 KiB", IMP(0,0,0,50,0,3,0), 98304, 12, 90, 1),
TDEF("truncate_only", "A-CORRUPT: 2% truncation, 96 KiB", IMPX(0,0,0,50,0,0,0,2,0,0), 98304, 12, 90, 1),
TDEF("corrupt_trunc", "A-CORRUPT: 3% corrupt + 2% truncate, 96 KiB", IMPX(0,0,0,50,0,3,0,2,0,0), 98304, 12, 90, 1),
TDEF("pipeline", "A-PIPELINE: RTT 100 ms, must pipeline within 5 s", IMP(0,0,0,50,0,0,50), 262144, 8, 5, 1),
TDEF("hostile", "A-HOSTILE: garbage datagrams injected, must ignore", IMPX(0,0,0,50,0,0,0,0,40,0), 80000, 12, 90, 1),
TDEF("oversized", "A-OVERSIZED-IGNORE: ~60 KB datagrams, must ignore", IMPX(0,0,0,50,0,0,0,0,0,4), 80000, 12, 90, 1),
TDEF("lifecycle", "A-LIFECYCLE: -w idle self-exit timing", CLEAN, 0, 10, 20, 1),
TDEF("bulk_32m", "A-BULK: 32 MiB clean transfer", CLEAN, 33554432, 30, 120, 1),
TDEF("help", "--help " UTF_ARROW " exit 0 with non-empty stdout", CLEAN, 0, 10, 10, 1),
TDEF(NULL, NULL, CLEAN, 0, 0, 0, 0)
};
#define N_TESTS ((int)(sizeof(TESTS)/sizeof(TESTS[0]) - 1))
static const TestDef *find_test(const char *name) {
for (int i = 0; i < N_TESTS; i++) {
if (strcmp(TESTS[i].name, name) == 0) {
return &TESTS[i];
}
}
return NULL;
}
static int is_slow(const char *name) {
static const char *slow[] = {"large_1mb","large_5mb","timeout_test","loss_30","bulk_32m","corrupt_only","truncate_only","corrupt_trunc",NULL};
for (int i = 0; slow[i]; i++) {
if (strcmp(name, slow[i]) == 0) {
return 1;
}
}
return 0;
}
// udp impairment Proxy
typedef struct {
uint8_t *data;
size_t len;
int sock_fd;
struct sockaddr_in dest;
int delay_us;
} DSSendArg;
static void *delay_send_fn(void *vp) {
DSSendArg *a = vp;
if (a->delay_us > 0) {
usleep((useconds_t)a->delay_us);
}
sendto(a->sock_fd, a->data, a->len, 0, (struct sockaddr *)&a->dest, sizeof a->dest);
free(a->data); free(a);
return NULL;
}
static void fire_delayed(int sock_fd, const uint8_t *data, size_t len, const struct sockaddr_in *dest, int delay_us) {
DSSendArg *a = malloc(sizeof *a);
if (!a) {
sendto(sock_fd, data, len, 0, (const struct sockaddr *)dest, sizeof *dest);
return;
}
a->data = malloc(len + 1);
if (!a->data) {
free(a);
sendto(sock_fd, data, len, 0, (const struct sockaddr *)dest, sizeof *dest);
return;
}
memcpy(a->data, data, len);
a->len = len; a->sock_fd = sock_fd; a->dest = *dest; a->delay_us = delay_us;
pthread_t tid; pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
pthread_create(&tid, &attr, delay_send_fn, a);
pthread_attr_destroy(&attr);
}
typedef struct {
Impairment imp;
struct sockaddr_in server_addr;
struct sockaddr_in client_addr;
int proxy_port, server_port;
volatile int halt;
int sock_fd;
int has_client;
int inj_hostile, inj_oversized;
// cppcheck-suppress unusedStructMember
int _ipad;
pthread_t tid;
// IOS HW; why isn't a mutex used here?
long s_fwd, s_rev, s_fwd_b, s_rev_b;
long s_drop, s_dup, s_reorder, s_corrupt, s_trunc, s_inject;
} UDPProxy;
// inject a datagram of random bytes toward dest (garbage you must ignore)
static void inject_garbage(int sock_fd, const struct sockaddr_in *dest, size_t size) {
if (size == 0) {
size = 1;
}
uint8_t *g = malloc(size);
if (!g) {
return;
}
for (size_t i = 0; i < size; i++) {
g[i] = (uint8_t)(rand() & 0xff);
}
sendto(sock_fd, g, size, 0, (const struct sockaddr *)dest, sizeof *dest);
free(g);
}
static void proxy_relay(UDPProxy *px, const uint8_t *data, size_t len, const struct sockaddr_in *dest, int is_fwd) {
/*
| ||
|| |_
*/
if (px->imp.loss_pct > 0.0f &&
(double)rand() / RAND_MAX * 100.0 < (double)px->imp.loss_pct) {
px->s_drop++; return;
}
// corruption
uint8_t *pkt = malloc(len + 1);
if (len) {
memcpy(pkt, data, len);
}
if (px->imp.corrupt_pct > 0.0f && len > 0 && (double)rand() / RAND_MAX * 100.0 < (double)px->imp.corrupt_pct) {
pkt[rand() % (int)len] ^= (uint8_t)((rand() % 255) + 1);
px->s_corrupt++;
}
// truncation
size_t send_len = len;
if (px->imp.trunc_pct > 0.0f && len > 4 &&
(double)rand() / RAND_MAX * 100.0 < (double)px->imp.trunc_pct) {
size_t maxchop = len / 2;
size_t chop = 1u + (size_t)(rand() % (int)maxchop);
send_len = len - chop;
px->s_trunc++;
}
// delay, jitter, reorder
double delay = px->imp.delay_ms / 1000.0;
if (px->imp.jitter_ms > 0) {
delay += ((double)rand() / RAND_MAX * 2.0 - 1.0) * px->imp.jitter_ms / 1000.0;
if (delay < 0.0) {
delay = 0.0;
}
}
if (px->imp.reorder_pct > 0.0f && (double)rand() / RAND_MAX * 100.0 < (double)px->imp.reorder_pct) {
delay += px->imp.reorder_delay_ms / 1000.0;
px->s_reorder++;
}
int delay_us = (int)(delay * 1e6);
if (delay_us > 1000) {
fire_delayed(px->sock_fd, pkt, send_len, dest, delay_us);
} else {
sendto(px->sock_fd, pkt, send_len, 0, (const struct sockaddr *)dest, sizeof *dest);
}
// duplication
if (px->imp.dup_pct > 0.0f && (double)rand() / RAND_MAX * 100.0 < (double)px->imp.dup_pct) {
double d2 = delay + 0.003 + (double)rand() / RAND_MAX * 0.022;
fire_delayed(px->sock_fd, pkt, send_len, dest, (int)(d2 * 1e6));
px->s_dup++;
}
free(pkt);
if (is_fwd) {
// garbage injection toward server
if (px->inj_oversized > 0) {
inject_garbage(px->sock_fd, dest, 60000);
px->inj_oversized--;
px->s_inject++;
}
if (px->inj_hostile > 0 && (rand() % 4 == 0)) {
inject_garbage(px->sock_fd, dest, 20u + (size_t)(rand() % 40));
px->inj_hostile--;
px->s_inject++;
}
px->s_fwd++;
px->s_fwd_b += (long)len;
} else {
px->s_rev++;
px->s_rev_b += (long)len;
}
}
static void *proxy_thread_fn(void *vp) {
UDPProxy *px = vp;
uint8_t *buf = malloc(PROXY_BUF_SZ);
struct sockaddr_in from;
socklen_t flen;
while (!px->halt) {
int fd = px->sock_fd;
if (fd < 0) {
break;
}
fd_set rfds; FD_ZERO(&rfds); FD_SET(fd, &rfds);
struct timeval tv = {0, 300000};
int r = select(fd + 1, &rfds, NULL, NULL, &tv);
if (r < 0) {
if (errno == EINTR) {
continue;
}
break;
}
if (r == 0) {
continue;
}
flen = sizeof from;
ssize_t n = recvfrom(fd, buf, PROXY_BUF_SZ, 0, (struct sockaddr *)&from, &flen);
if (n < 0) {
if (!px->halt) {
continue;
}
break;
}
// routing s->c / c->s
if (from.sin_addr.s_addr == px->server_addr.sin_addr.s_addr && from.sin_port == px->server_addr.sin_port) {
if (px->has_client) {
proxy_relay(px, buf, (size_t)n, &px->client_addr, 0);
}
} else {
if (!px->has_client) {
px->client_addr = from; px->has_client = 1;
}
proxy_relay(px, buf, (size_t)n, &px->server_addr, 1);
}
}
free(buf);
return NULL;
}
static int proxy_start(UDPProxy *px, int proxy_port, int server_port, const Impairment *imp) {
memset(px, 0, sizeof *px);
px->proxy_port = proxy_port;
px->server_port = server_port;
px->imp = *imp;
px->sock_fd = -1;
px->inj_hostile = imp->hostile_n;
px->inj_oversized = imp->oversized_n;
px->server_addr.sin_family = AF_INET;
px->server_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
px->server_addr.sin_port = htons((uint16_t)server_port);
px->sock_fd = socket(AF_INET, SOCK_DGRAM, 0);
if (px->sock_fd < 0) return -1;
int one = 1;
setsockopt(px->sock_fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof one);
struct sockaddr_in ba = {0};
ba.sin_family = AF_INET;
ba.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
ba.sin_port = htons((uint16_t)proxy_port);
if (bind(px->sock_fd, (struct sockaddr *)&ba, sizeof ba) < 0) {
close(px->sock_fd); px->sock_fd = -1; return -1;
}
pthread_create(&px->tid, NULL, proxy_thread_fn, px);
return 0;
}
static void proxy_stop(UDPProxy *px) {
px->halt = 1;
int fd = px->sock_fd; px->sock_fd = -1;
if (fd >= 0) {
close(fd);
}
pthread_join(px->tid, NULL);
}
// port allocation
static int find_free_ports(int n, int start, int *out) {
int found = 0;
for (int p = start; p < start + 500 && found < n; p++) {
int s = socket(AF_INET, SOCK_DGRAM, 0);
if (s < 0) {
continue;
}
struct sockaddr_in a = {0};
a.sin_family = AF_INET;
a.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
a.sin_port = htons((uint16_t)p);
if (bind(s, (struct sockaddr *)&a, sizeof a) == 0) {
out[found++] = p;
}
close(s);
}
return (found == n) ? 0 : -1;
}
// process managment
#define STDIO_DEVNULL 0
#define STDIO_PIPE 1
#define STDIO_INHERIT 2
typedef struct {
pid_t pid;
int stdin_fd;
int stdout_fd;
int stderr_fd;
int returncode;
int exited;
} Proc;
static int proc_spawn(Proc *p, const char *const argv[], int in_m, int out_m, int err_m) {
memset(p, 0, sizeof *p);
p->stdin_fd = p->stdout_fd = p->stderr_fd = -1;
int dn = open("/dev/null", O_RDWR);
int pi[2]={-1,-1}, po[2]={-1,-1}, pe[2]={-1,-1};
if (in_m == STDIO_PIPE && pipe(pi) < 0) {
goto fail;
}
if (out_m == STDIO_PIPE && pipe(po) < 0) {
goto fail;
}
if (err_m == STDIO_PIPE && pipe(pe) < 0) {
goto fail;
}
p->pid = fork();
if (p->pid < 0) {
goto fail;
}
if (p->pid == 0) {
// child
int ci = (in_m == STDIO_DEVNULL) ? dn : (in_m == STDIO_PIPE) ? pi[0] : STDIN_FILENO;
int co = (out_m == STDIO_DEVNULL) ? dn : (out_m == STDIO_PIPE) ? po[1] : STDOUT_FILENO;
int ce = (err_m == STDIO_DEVNULL) ? dn : (err_m == STDIO_PIPE) ? pe[1] : STDERR_FILENO;
if (ci != STDIN_FILENO) {
dup2(ci, STDIN_FILENO);
}
if (co != STDOUT_FILENO) {
dup2(co, STDOUT_FILENO);
}
if (ce != STDERR_FILENO) {
dup2(ce, STDERR_FILENO);
}
long maxfd = sysconf(_SC_OPEN_MAX);
if (maxfd < 64) {
maxfd = 64;
}
for (long fd = 3; fd < maxfd; fd++) {
close((int)fd);
}
execvp(argv[0], (char *const *)argv);
_exit(127);
}
// parent
if (dn >= 0) {
close(dn);
}
if (in_m == STDIO_PIPE) {
close(pi[0]); p->stdin_fd = pi[1];
}
if (out_m == STDIO_PIPE) {
close(po[1]); p->stdout_fd = po[0];
}
if (err_m == STDIO_PIPE) {
close(pe[1]); p->stderr_fd = pe[0];
}
return 0;
fail:
if (dn >= 0) {
close(dn);
}
if (pi[0]>=0){
close(pi[0]);close(pi[1]);
}
if (po[0]>=0){
close(po[0]);close(po[1]);
}
if (pe[0]>=0){
close(pe[0]);close(pe[1]);
}
return -1;
}
static int proc_poll(Proc *p) {
if (p->exited) {
return 1;
}
int st; pid_t r = waitpid(p->pid, &st, WNOHANG);
if (r == p->pid) {
p->exited = 1;
p->returncode = WIFEXITED(st) ? WEXITSTATUS(st) : -(int)WTERMSIG(st);
return 1;
}
return 0;
}
static void proc_reap(Proc *p) {
int st; waitpid(p->pid, &st, 0);
p->exited = 1;
p->returncode = WIFEXITED(st) ? WEXITSTATUS(st) : -(int)WTERMSIG(st);
}
// 1 = on time, 0 = timeout
static int proc_wait_until(Proc *p, double deadline) {
while (now() < deadline) {
if (proc_poll(p)) {
return 1;
}
usleep(POLL_US);
}
return proc_poll(p);
}
static void proc_terminate(Proc *p) {
if (p->exited) {
return;
}
kill(p->pid, SIGTERM);
for (int i = 0; i < 30 && !p->exited; i++) {
usleep(100000);
proc_poll(p);
}
if (!p->exited) {
kill(p->pid, SIGKILL); proc_reap(p);
}
}
// stderr drain (pipe-buffer dEaDlOcK)
typedef struct {
const char *label;
int fd;
int verbose;
} SErr;
static void *serr_fn(void *vp) {
SErr *s = vp;
char line[512];
FILE *f = fdopen(s->fd, "r");
if (!f) {
free(s); return NULL;
}
while (fgets(line, sizeof line, f)) {
if (s->verbose) {
size_t n = strlen(line);
while (n && (line[n-1]=='\n'||line[n-1]=='\r')) {
line[--n]='\0';
}
if (n) {
printf(" [%s] %s\n", s->label, line);
}
}
}
fclose(f);
return NULL;
}
static void drain_stderr(int stderr_fd, const char *label, int verbose) {
if (stderr_fd < 0) {
return;
}
SErr *s = malloc(sizeof *s);
if (!s) {
close(stderr_fd);
return;
}
s->fd = stderr_fd; s->label = label; s->verbose = verbose;
pthread_t tid; pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
pthread_create(&tid, &attr, serr_fn, s);
pthread_attr_destroy(&attr);
}
// input generation + fmt_bytes
static uint8_t *gen_input(const TestDef *td, size_t *len) {
if (strcmp(td->name, "tiny") == 0) {
*len = 1;
uint8_t *b = malloc(1);
if (!b) {
return NULL;
}
b[0] = 0x42;
return b;
}
if (strcmp(td->name, "binary") == 0) {
*len = 256 * 200;
uint8_t *b = malloc(*len);
if (!b) {
return NULL;
}
for (int i = 0; i < 200; i++){
for (int j = 0; j < 256; j++) {
b[i*256+j] = (uint8_t)j;
}
}
return b;
}
if (td->input_size == 0) {
*len = 0;
return malloc(1);
}
*len = (size_t)td->input_size;
uint8_t *b = malloc(*len);
if (!b) {
return NULL;
}
int fd = open("/dev/urandom", O_RDONLY);
if (fd < 0) {
for (size_t i = 0; i < *len; i++) {
b[i] = (uint8_t)(rand() & 0xff);
}
return b;
}
size_t done = 0;
while (done < *len) {
ssize_t r = read(fd, b + done, *len - done);
if (r > 0) {
done += (size_t)r;
} else {
break;
}
}
close(fd);
if (done < *len) {
for (size_t i = done; i < *len; i++) {
b[i] = (uint8_t)(rand() & 0xff);
}
}
return b;
}
static const char *fmt_bytes(long n, char *buf, size_t sz) {
if (n == 0) {
snprintf(buf, sz, "0 B");
return buf;
}
double v = (double)n;
int i = 0;
while (v >= 1024.0 && i < 4) {
v /= 1024.0; i++;
}
if (i == 0) {
snprintf(buf, sz, "%ld B", n);
}
else {
static const char *u[] = {"B","KB","MB","GB","TB"};
snprintf(buf, sz, "%.1f %s", v, u[i]);
}
return buf;
}
// results
typedef struct {
long input_size, output_size, first_diff;
long p_fwd, p_rev, p_drop, p_dup, p_reorder, p_corrupt;
long p_trunc, p_inject;
int client_exit, server_exit;
int has_diff, skipped;
int exit_code;
int pad;
char input_sha[65], output_sha[65];
char sha_pad[6];
} Det;
typedef struct {
Det d;
char msg[MAX_MSG];
int ok;
int pad;
} Res;
static Res res_ok(const char *msg, const Det *d) {
Res r = {0};
r.ok = 1;
snprintf(r.msg, MAX_MSG, "%s", msg);
if (d) {
r.d = *d;
} else {
memset(&r.d, 0, sizeof r.d);
}
r.pad = 0;
r.d.pad = 0;
memset(r.d.sha_pad, 0, sizeof r.d.sha_pad);
return r;
}
static Res res_fail(const char *msg, const Det *d) {
Res r = {0};
r.ok = 0;
snprintf(r.msg, MAX_MSG, "%s", msg);
if (d) {
r.d = *d;
} else {
memset(&r.d, 0, sizeof r.d);
}
r.pad = 0;
r.d.pad = 0;
memset(r.d.sha_pad, 0, sizeof r.d.sha_pad);
return r;
}
static Res res_failf(const Det *d, const char *fmt, ...) {
Res r = {0};
r.ok = 0;
if (d) {
r.d = *d;
} else {
memset(&r.d, 0, sizeof r.d);
}
va_list ap;
va_start(ap, fmt);
vsnprintf(r.msg, MAX_MSG, fmt, ap);
va_end(ap);
r.pad = 0;
r.d.pad = 0;
memset(r.d.sha_pad, 0, sizeof r.d.sha_pad);
return r;
}
static Res res_okf(const Det *d, const char *fmt, ...) {
Res r = {0};
r.ok = 1;
if (d) {
r.d = *d;
} else {
memset(&r.d, 0, sizeof r.d);
}
va_list ap;
va_start(ap, fmt);
vsnprintf(r.msg, MAX_MSG, fmt, ap);
va_end(ap);
r.pad = 0;
r.d.pad = 0;
memset(r.d.sha_pad, 0, sizeof r.d.sha_pad);
return r;
}
static Res res_skip(const char *msg) {
Res r = {0};
r.ok = 1;
r.d.skipped = 1;
snprintf(r.msg, MAX_MSG, "%s", msg);
r.pad = 0;
r.d.pad = 0;
memset(r.d.sha_pad, 0, sizeof r.d.sha_pad);
return r;
}
// file - file test
static Res run_standard(const char *binary, const TestDef *td, const char *tmpdir, int verbose, int port_base) {
static Det d;
memset(&d, 0, sizeof d);
(void)td->_pad;
int ports[2];
if (find_free_ports(2, port_base, ports) < 0) {
return res_fail("No free ports", &d);
}
int proxy_port = ports[0], server_port = ports[1];
d.input_size = 0;
// input generation
size_t in_len; uint8_t *in_data = gen_input(td, &in_len);
if (!in_data) {
return res_fail("Input allocation failed", &d);
}
char in_file[128], out_file[128];
snprintf(in_file, sizeof in_file, "%s/input.bin", tmpdir);
snprintf(out_file, sizeof out_file, "%s/output.bin", tmpdir);
FILE *f = fopen(in_file, "wb");
if (!f) {
free(in_data);
return res_fail("Failed to open input file", &d);
}
if (in_len) {
fwrite(in_data, 1, in_len, f);
}
fclose(f);
sha256hex(in_data, in_len, d.input_sha);
d.input_size = (long)in_len;
// start proxy
UDPProxy *px = malloc(sizeof *px);
if (!px) {
free(in_data);
return res_fail("OOM allocating proxy", &d);
}
if (proxy_start(px, proxy_port, server_port, &td->imp) < 0) {
free(px);
free(in_data);
return res_fail("Proxy bind failed", &d);
}
(void)px->_ipad;
// start server
char pw[16], sp[16]; snprintf(pw,sizeof pw,"%d",td->timeout_w);
snprintf(sp, sizeof sp, "%d", server_port);
const char *srv_argv[] = {
binary, "-s", "-p", sp, "-a", "127.0.0.1",
"-o", out_file, "-w", pw, NULL
};
Proc srv;
if (proc_spawn(&srv, srv_argv, STDIO_DEVNULL, STDIO_DEVNULL, STDIO_PIPE) < 0) {
proxy_stop(px);
free(px);
free(in_data);
return res_fail("Failed to start server", &d);
}
drain_stderr(srv.stderr_fd, "srv", verbose);
usleep(150000); // server is binding
// start client
char pp[16]; snprintf(pp, sizeof pp, "%d", proxy_port);
const char *cli_argv[] = {
binary, "-c", "-a", "127.0.0.1", "-p", pp,
"-i", in_file, "-w", pw, NULL
};
Proc cli;
if (proc_spawn(&cli, cli_argv, STDIO_DEVNULL, STDIO_DEVNULL, STDIO_PIPE) < 0) {
proc_terminate(&srv);
proxy_stop(px);
free(px);
free(in_data);
return res_fail("Failed to start client", &d);
}
drain_stderr(cli.stderr_fd, "cli", verbose);