-
Notifications
You must be signed in to change notification settings - Fork 0
/
fakelink.cpp
171 lines (133 loc) · 3.72 KB
/
fakelink.cpp
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
#include <unistd.h>
#include <sys/select.h>
#include <stdint.h>
#include <cstdlib>
#include <string>
#include <stdio.h>
#include <iostream>
void subexec(char * cmdexec[],int * childpid, int * childin,int * childout) {
int child_stdin[2];
int child_stdout[2];
int pid;
if (pipe(child_stdin) == -1 || pipe(child_stdout) == -1) {
perror("Can't create child pipes");
exit(1);
}
if ( (pid = fork()) < 0) {
perror("fork error");
exit(1);
} else if ( pid == 0 ) {
close(child_stdin[1]);
close(child_stdout[0]);
if(dup2(child_stdin[0], STDIN_FILENO) < 0) {
perror("dup2 failed");
exit(1);
}
if(dup2(child_stdout[1], STDOUT_FILENO) < 0) {
perror("dup2 failed");
exit(1);
}
execvp(cmdexec[0],cmdexec);
perror(cmdexec[0]);
exit(1);
/* do child stuff */
} else {
close(child_stdin[0]);
close(child_stdout[1]);
/* do parent stuff */
*childin = child_stdin[1];
*childout = child_stdout[0];
}
}
static int write_loop(int fd, uint8_t *buf, uint32_t size)
{
uint8_t *p;
int n;
p = buf;
while (p - buf < size) {
n = write(fd, p, size - (p - buf));
if (n == -1) {
break;
}
p += n;
}
if((p - buf) > size) {
return -1;
}
return 0;
}
void doCorruption(uint8_t * data,uint32_t size,double errRate) {
for(uint32_t i = 0; i < size; i++) {
double r = (double)rand();
if(r < (errRate * RAND_MAX)) {
data[i] = rand();
}
}
}
int main(int argc, char * argv[]) {
int opt;
bool corrupt = false;
double err_rate;
uint32_t bps;
bool speedlimit = false;
while ((opt = getopt(argc, argv, "e:s:")) != -1) {
switch (opt) {
case 'e':
corrupt = true;
err_rate = atof(optarg);
break;
case 's':
speedlimit = true;
bps = atoi(optarg);
break;
default: /* '?' */
std::cerr << "Bad arguments." << std::endl;
exit(EXIT_FAILURE);
}
}
int childpid,childin,childout;
subexec(&argv[optind],&childpid,&childin,&childout);
uint8_t buff[64]; //deliberately small so we can control speeds finely
int maxfd = (STDIN_FILENO > childout) ? STDIN_FILENO : childout;
for (;;) {
fd_set fds;
int r, n_r;
FD_ZERO(&fds);
FD_SET(childout, &fds);
FD_SET(STDIN_FILENO, &fds);
r = select(maxfd + 1, &fds, NULL, NULL, NULL);
if(r < 0) {
break;
}
if (FD_ISSET(STDIN_FILENO, &fds)) {
n_r = read(STDIN_FILENO, buff, sizeof(buff));
if (n_r <= 0) {
break;
}
if(corrupt) {
doCorruption(buff,n_r,err_rate);
}
if(speedlimit) {
usleep((n_r * 1000000) / bps);
}
if(write_loop(childin, buff, n_r) != 0) {
break;
}
}
if (FD_ISSET(childout, &fds)) {
n_r = read(childout, buff, sizeof(buff));
if (n_r <= 0) {
break;
}
if(corrupt) {
doCorruption(buff,n_r,err_rate);
}
if(speedlimit) {
usleep((n_r * 1000000) / bps);
}
if(write_loop(STDOUT_FILENO, buff, n_r) != 0) {
break;
}
}
}
}