-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbridge.c
45 lines (43 loc) · 1.08 KB
/
bridge.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
#include<stdlib.h>
#include<stdio.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <errno.h>
#include <linux/if_tun.h>
#include <sys/ioctl.h>
#define max(a,b) ((a)>(b) ? (a):(b))
void bridge(int fd1, int fd2)
{
ioctl(fd1, TUNSETNOCSUM, 1);
ioctl(fd2, TUNSETNOCSUM, 1);
char buf[1500];
fd_set fds;
int fm = max(fd1, fd2) + 1;
int l = 0;
while(1){
FD_ZERO(&fds);
FD_SET(fd1, &fds);
FD_SET(fd2, &fds);
select(fm, &fds, NULL, NULL, NULL);
if(FD_ISSET(fd1, &fds)) {
l = read(fd1,buf,sizeof(buf));
if (l == 0) {
printf("read fd1 failed.");
break;
}
printf("recieved %d bytes.\n", l);
write(fd2,buf,l);
}
if(FD_ISSET(fd2, &fds)) {
l = read(fd2, buf, sizeof(buf));
if (l == 0) {
printf("read fd2 failed.");
break;
}
printf("sending %d bytes.\n", l);
write(fd1, buf, l);
}
}
}