-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmain.c
313 lines (296 loc) · 8.42 KB
/
main.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
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
/*
This program 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.
This program 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, see <http://www.gnu.org/licenses/>.
*/
#include "def.h"
pcap_t* descr;
pthread_t tid[2];
int sock, connected, bytes_recv = 0;
struct sockaddr_in local_addr, remote_addr;
struct sockaddr_in6 local_addr6, remote_addr6;
socklen_t sin_size;
unsigned short server_mode = 0;
char hostname[256] = {0};
char udpremote[256] = {0};
int port = 0;
unsigned short int tunorif = 0; /* [tun==1, if==2] */
char *tap_ip, *tap_mac, *tap_mask = NULL;
char *bpf = NULL;
short int udpmode = 0;
short int ipv6 = 0;
short int tap6 = 0;
short int cksum = 1;
short int compmode = 0;
void usage()
{
fprintf(stderr, "Option -%c error.\n", optopt);
fprintf(stderr, "*** tundeep v%s by Adam Palmer <[email protected]> ***\n", VER);
#ifdef _LINUX
fprintf(stderr, "Usage: tundeep <-i iface|[-t|-T] tapiface> <-h ip> <-p port> [-6] [-C] <-c|-s> ");
fprintf(stderr, "[-x tapip] [-y tapmask] [-u tapmac] [-b bpf] [-d udp mode] [-e udp remote] [-K]\n\n");
#else
fprintf(stderr, "Usage: tundeep [-a] <-i iface> <-h ip> <-p port> [-6] [-C] <-c|-s> ");
fprintf(stderr, "[-b bpf] [-d udp mode] [-e udp remote] [-K]\n\n");
#endif
fprintf(stderr, "-6 IPv6 mode\n");
fprintf(stderr, "-C compress mode\n");
fprintf(stderr, "-K disable checksum\n");
fprintf(stderr, "-a print all pcap devs\n");
fprintf(stderr, "-b \"bpf\"\n");
fprintf(stderr, "-i interface to bind to\n");
fprintf(stderr, "-h IP to bind to/connect to\n");
fprintf(stderr, "-p port to bind to/connect to\n");
fprintf(stderr, "-c client mode\n");
fprintf(stderr, "-s server mode\n");
fprintf(stderr, "-d udp mode\n");
fprintf(stderr, "-e udp peer\n");
#ifdef _LINUX
fprintf(stderr, "-t tap interface \n");
fprintf(stderr, "-T ipv6 tap interface \n");
fprintf(stderr, "-u tap mac \n");
fprintf(stderr, "-x if -t mode, set iface ip, if -T mode, set iface ipv6 ip\n");
fprintf(stderr, "-y if -t mode, set iface mask, if -T mode, set iface ipv6 prefixlen\n");
#endif
fprintf(stderr, "--------------------\n\n");
}
int main(int argc,char **argv)
{
int c = 0;
char iface[128] = {0};
int actr = 0; char a[34];
int i = 0, inum = 0;
pcap_if_t *alldevsp, *device;
char errbuf[PCAP_ERRBUF_SIZE];
#ifdef _LINUX
while ( ((c = getopt(argc, argv, "6CKe:dai:T:t:u:h:p:csb:x:y:")) != -1) && (actr < 32) )
#else
while ( ((c = getopt(argc, argv, "6CKe:dab:i:h:p:cs")) != -1) && (actr < 32) )
#endif
{
a[actr] = c; actr++;
switch(c)
{
case 'C':
compmode = 1;
break;
case 'K':
cksum = 0;
break;
case '6':
ipv6 = 1;
break;
case 'b':
bpf = malloc(strlen(optarg)+1);
strncpy(bpf, optarg, strlen(optarg));
break;
case 'a':
//interface
fprintf(stderr, "Printing device list:\n");
fprintf(stderr, "---------------------\n");
if (pcap_findalldevs(&alldevsp, errbuf))
{
debug(1, 1, "Error finding devices (%s)", errbuf);
}
for (device = alldevsp; device != NULL; device = device->next)
{
fprintf(stderr, "%d. %s", ++i, device->name);
if (device->description)
{
fprintf(stderr, " (%s)\n", device->description);
} else {
fprintf(stderr, " (No description available)\n");
}
}
fprintf(stderr, "---------------------\n");
debug(1, 0, "Device list finished printing");
printf("Enter the interface number (1-%d): ", i);
if (scanf("%d", &inum)) { /* ssshh compiler */ }
if (inum < 1 || inum > i)
{
fprintf(stderr, "\nInterface out of range\n");
pcap_freealldevs(alldevsp);
exit(0);
}
for (device=alldevsp, i=0; i < inum-1; device = device->next, i++);
strncpy(iface, device->name, 127);
tunorif = IFACE;
break;
case 'i':
strncpy(iface, optarg, 127);
tunorif = IFACE;
break;
case 'e':
//host to listen on/connect to
strncpy(udpremote, optarg, 255);
break;
case 'h':
//host to listen on/connect to
strncpy(hostname, optarg, 255);
break;
case 'p':
//port
port = atoi(optarg);
if (port < 0 || port > 65535)
{
debug(1, 1, "Invalid port number");
}
break;
case 'c':
//client mode
server_mode = 0;
break;
case 's':
//server mode
server_mode = 1;
break;
case 'd':
udpmode = 1;
break;
#ifdef _LINUX
case 't':
//tap
strncpy(iface, optarg, 127);
tunorif = TUN;
break;
case 'T':
//tap6
strncpy(iface, optarg, 127);
tap6 = 1;
tunorif = TUN;
break;
case 'u':
//tap mac
tap_mac = malloc(18);
strncpy(tap_mac,optarg,17);
break;
case 'x':
//tap ip
tap_ip = malloc(128);
strncpy(tap_ip, optarg, 127);
break;
case 'y':
//tap mask
tap_mask = malloc(128);
strncpy(tap_mask, optarg, 127);
break;
#endif
case '?':
usage();
debug(2, 1, "Usage error");
break;
}
}
a[actr]='\0';
if ( ((strchr(a, 's') == NULL) && (strchr(a, 'c') == NULL)) && (strchr(a, 'd') == NULL) )
{
usage();
debug(2, 1, "Usage: Either -s or -c must be specified");
}
if ( (strchr(a, 's') != NULL) && (strchr(a, 'c') != NULL) )
{
usage();
debug(2, 1, "Usage: Option -s and -c can not be specified together");
}
if ( (strchr(a, 'h') == NULL) || (strchr(a, 'p') == NULL) )
{
usage();
debug(2, 1, "Usage: Options -h and -p are mandatory");
}
if ( (strchr(a, 'd') != NULL) && (strchr(a, 'e') == NULL) )
{
usage();
debug(2, 1, "Usage: -e endpoint must be specified in UDP mode");
}
if ( (strchr(a, 'd') != NULL) && ( (strchr(a, 'c') != NULL) || (strchr(a, 's') != NULL) ) )
{
usage();
debug(2, 1, "Usage: -c/-s not required in UDP mode");
}
if ( (strchr(a, 'a') == NULL) && (strchr(a, 'i') == NULL) && (strchr(a, 't') == NULL) && (strchr(a, 'T') == NULL) )
{
usage();
debug(2, 1, "Usage: Option -a, -i, -t or -T must be specified");
}
if ( (strchr(a, 'a') != NULL) && (strchr(a, 'i') != NULL) && (strchr(a, 't') != NULL) && (strchr(a, 'T') != NULL) )
{
usage();
debug(2, 1, "Usage: Option -a, -i and -t can not be specified together");
}
if ( (strchr(a, 't') != NULL) && (strchr(a, 'T') != NULL) )
{
usage();
debug(2, 1, "Options -t and -T can not be specified together");
}
if ( ( (strchr(a, 'a') != NULL) || (strchr(a, 'i') != NULL) ) && ( (strchr(a, 'u') != NULL) || (strchr(a, 'x') != NULL) || (strchr(a, 'y') != NULL) ))
{
usage();
debug(2, 1, "Usage: Options -u, -x and -y only work with -t or -T, not -i or -a");
}
#ifdef _LINUX
if (tunorif == TUN)
{
//set up the tap device
if ((tap_fd = tun_alloc(iface, IFF_TAP | IFF_NO_PI)) < 0)
{
perror("tun/tap failed");
}
if (tap6)
{
confif6(iface, tap_ip, tap_mask);
} else {
confif(iface, tap_ip, tap_mask);
}
if (tap_ip != NULL) { free(tap_ip); }
if (tap_mask != NULL) { free(tap_mask); }
}
#endif
/* First we set up PCAP */
struct bpf_program fp;/* hold compiled program */
bpf_u_int32 netp = 0; /* ip */
/* open device for reading in promiscuous mode */
descr = pcap_open_live(iface, MAX_PCAP_SIZ, 1,PCAP_TIMEOUT, errbuf);
if(descr == NULL) {
printf("pcap_open_live(): %s\n", errbuf);
debug(2, 1, "pcap_open_live");
}
if (bpf == NULL)
{
bpf = malloc(1);
memcpy(bpf, "\0", 1);
}
/* Now we'll compile the filter expression*/
if(pcap_compile(descr, &fp,bpf, 0, netp) == -1) { //no search
fprintf(stderr, "Error calling pcap_compile\n");
debug(2, 1, "pcap_compile");
}
free(bpf);
/* set the filter */
if(pcap_setfilter(descr, &fp) == -1) {
fprintf(stderr, "Error setting filter\n");
debug(2, 1, "pcap filter");
}
/* Now we set up the socket */
if (!tun_connect(hostname, port))
{
debug (1, 1, "tun_connect failed");
}
/* Now launch the threads */
if (pthread_create(&(tid[0]), NULL, &thread_func, "") != 0) //read from br0 and write to socket
{
debug (1, 1, "Thread creation failed");
}
if (pthread_create(&(tid[1]), NULL, &thread_func, "") != 0) //read from socket and write to br0
{
debug (1, 1, "Thread creation failed");
}
for (;;) sleep(10); //don't terminate
return 0;
}