-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtapio.c
252 lines (220 loc) · 6.99 KB
/
tapio.c
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
#define _GNU_SOURCE
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/select.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netinet/tcp.h>
#include <netdb.h>
#include <sys/ioctl.h>
#include <linux/if.h>
#include <linux/if_tun.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <stdlib.h>
#include <signal.h>
#include "strannex.h"
#ifndef POLLRDHUP
#define POLLRDHUP 0x2000
#endif
#define BUF_SIZE 4096
int tap_alloc(char *dev, char *name, int tun)
{
struct ifreq ifr;
int fd, err;
/* Arguments taken by the function:
*
* char *dev: the name of an interface (or '\0'). MUST have enough
* space to hold the interface name if '\0' is passed
* int flags: interface flags (eg, IFF_TUN etc.)
*/
/* open the clone device */
if( (fd = open(dev, O_RDWR | O_EXCL)) < 0 ) {
return fd;
}
/* preparation of the struct ifr, of type "struct ifreq" */
memset(&ifr, 0, sizeof(ifr));
ifr.ifr_flags = tun | IFF_NO_PI; /* IFF_TUN or IFF_TAP, plus maybe IFF_NO_PI */
if (*dev) {
/* if a device name was specified, put it in the structure; otherwise,
* the kernel will try to allocate the "next" device of the
* specified type */
strncpy(ifr.ifr_name, name, IFNAMSIZ);
}
/* try to create the device */
if ((err = ioctl(fd, TUNSETIFF, (void *) &ifr)) < 0) {
close(fd);
return err;
}
if ((err = ioctl(fd, TUNSETPERSIST, 1)) < 0) {
close(fd);
return err;
}
/* if the operation was successful, write back the name of the
* interface to the variable "dev", so the caller can know
* it. Note that the caller MUST reserve space in *dev (see calling
* code below) */
strcpy(dev, ifr.ifr_name);
/* this is the special file descriptor that the caller will use to talk
* with the virtual interface */
return fd;
}
void signalHandler(int signal)
{
exit (0);
}
int main(int argc, char **argv)
{
char buf[BUF_SIZE], dev[IFNAMSIZ]="/dev/net/tun", tapdev[IFNAMSIZ], *progname;
int tap, r, tun = IFF_TUN;
int c, verbose = 0, interval = 0, keep = 0, keep_count = 0;
time_t last_keep;
struct timeval tv;
struct rlimit rlim;
fd_set rfd;
while ( (c=getopt(argc,argv,"vi:k:")) != -1 ) {
switch (c) {
case 'v':
verbose++;
break;
case 'i':
interval = atoi(optarg);
break;
case 'k':
keep = atoi(optarg);
break;
}
}
if ((progname = strrchr(argv[0], '/')) == NULL) {
progname = argv[0];
} else {
progname++;
}
if (keep && !interval) {
write_str(STDERR_FILENO, progname);
write_cstr(STDERR_FILENO, ": keep alive need interval value.\n");
return 1;
}
signal(SIGCHLD,signalHandler);
signal(SIGPIPE,signalHandler);
if (strcmp("tunio", progname)) tun = IFF_TAP;
if (argc>optind) {
if (!strncmp(argv[optind],"/dev/",5)) argv[optind] += 5;
if (verbose) {
write_str(STDERR_FILENO, progname);
write_cstr(STDERR_FILENO, ": Forced to ");
write_str(STDERR_FILENO,argv[optind]);
write_cstr(STDERR_FILENO, "\n");
}
strstart(tapdev);
strarray(tapdev);
strannex(tapdev,argv[optind]);
tap = tap_alloc (dev, tapdev, tun);
} else {
strstart(tapdev);
for (r=0 ; r<1000 ; r++) {
strarray(tapdev);
if (tun == IFF_TUN) {
strannex(tapdev, "tun");
} else {
strannex(tapdev, "tap");
}
strannex_uint(tapdev, r);
tap = tap_alloc (dev, tapdev, tun);
if (tap > 0) break; // success
}
}
if (tap <= 0) {
write_str(STDERR_FILENO, progname);
if (tun == IFF_TUN) {
write_cstr(STDERR_FILENO, ": Cannot open TUN\n");
} else {
write_cstr(STDERR_FILENO, ": Cannot open TAP\n");
}
return 1;
}
gettimeofday(&tv, NULL);
last_keep = tv.tv_sec;
rlim.rlim_cur = 0;
setrlimit(RLIMIT_NOFILE, &rlim);
/* ------------ main loop without length ---------- */
for (;;) {
FD_ZERO(&rfd);
FD_SET(STDIN_FILENO, &rfd);
FD_SET(tap, &rfd);
tv.tv_sec = 1;
tv.tv_usec = 0;
r = select(tap+1,&rfd,NULL,NULL,&tv);
if(r == -1) {
write_str(STDERR_FILENO, progname);
write_cstr(STDERR_FILENO, ": select() error.\n");
return errno;
}
if (interval) {
gettimeofday(&tv, NULL);
if (verbose && (tv.tv_sec % 10)==0) {
write_str(STDERR_FILENO, progname);
write_str_uint(STDERR_FILENO, ": keep count: ", keep_count);
writeln_str_uint(STDERR_FILENO, ", keep: ", keep);
}
if (tv.tv_sec >= (last_keep+interval)) {
write(STDOUT_FILENO, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 20);
if (verbose) {
write_str(STDERR_FILENO, progname);
write_cstr(STDERR_FILENO, ": Sent a keep alive.\n");
}
last_keep = tv.tv_sec;
}
}
if (keep) {
keep_count++;
if (verbose) {
write_str(STDERR_FILENO, progname);
writeln_str_uint(STDERR_FILENO, ": keep count increased: ", keep_count);
}
}
if (FD_ISSET(STDIN_FILENO,&rfd)) {
r = read(STDIN_FILENO, buf, BUF_SIZE);
if (r > 0 && keep) { // reset count on read.
keep_count = 0;
if (verbose) {
write_str(STDERR_FILENO, progname);
write_cstr(STDERR_FILENO, ": data received, keep count reseted.\n");
}
}
if (r > 20) { // ignore packet inferior to 21 bytes, keep alive
write(tap, buf, r);
} else if (r <= 0) {
if (verbose) {
write_str(STDERR_FILENO, progname);
write_cstr(STDERR_FILENO, ": stdin error.\n");
}
return 1;
}
}
if (FD_ISSET(tap,&rfd)) {
r = read(tap, buf, BUF_SIZE);
if (r > 0) {
write(STDOUT_FILENO, buf, r);
} else if (r <= 0) {
if (verbose) {
write_str(STDERR_FILENO, progname);
write_cstr(STDERR_FILENO, ": stdout error.\n");
}
return 2;
}
}
if (keep && keep_count >= keep) {
write_str(STDERR_FILENO, progname);
writeln_str_uint(STDERR_FILENO, ": Keep count exceeded ", keep_count);
return 0;
}
}
return 0;
}