-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmtun.c
More file actions
executable file
·156 lines (132 loc) · 3.63 KB
/
mtun.c
File metadata and controls
executable file
·156 lines (132 loc) · 3.63 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
# ---------------------------------------------------------------
# | This piece of code is released under the |
# | artistic license 2.0 |
# | http://www.opensource.org/licenses/artistic-license-2.0.php |
# | Athanasios Douitsis, aduitsis@netmode.ntua.gr, NTUA, 2011 |
# ---------------------------------------------------------------
#include <stdio.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <netinet/if_ether.h>
#include <linux/if.h>
#include <linux/if_tun.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <time.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
struct sockaddr_in addr;
int sock,ttl=64;
struct ip_mreq mreq;
struct ifreq ifr;
int fd, err, n;
void *buf;
fprintf(stderr,"%s %s \n",argv[1],argv[2]);
/*create a udp socket*/
if ((sock=socket(AF_INET,SOCK_DGRAM,0)) < 0) {
perror("socket");
exit(1);
}
/*make a dest address*/
memset(&addr,0,sizeof(addr));
addr.sin_family=AF_INET;
addr.sin_addr.s_addr=inet_addr(argv[1]);
addr.sin_port=htons(atoi(argv[2]));
/*open the tun device*/
buf = malloc(10000);
if( (fd = open("/dev/net/tun", O_RDWR)) < 0 ) {
fprintf(stderr,"Could not open device\n");
exit(1);
}
/* setup an ifr options struct*/
memset(&ifr, 0, sizeof(ifr));
ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
/* set the options to the device*/
if( (err = ioctl(fd, TUNSETIFF, (void *) &ifr)) < 0 ) {
fprintf(stderr,"Cannot allocate TAP device\n");
close(fd);
exit(1);
}
fprintf(stderr,"Allocated device %s\n", ifr.ifr_name);
if(fork()) {
if (setsockopt(sock, IPPROTO_IP, IP_MULTICAST_TTL,
(char *) &ttl, 1) < 0 ) {
perror("setsockopt error");
exit(EXIT_FAILURE);
}
close(0);
fprintf(stderr,"Parent is up\n");
while(1) {
if ((n = read(fd, buf, 10000))<0) {
fprintf(stderr,"Could not read from device");
close(fd);
exit(1);
}
/*
if(write(1,buf,n)<0) {
fprintf(stderr,"write to stdout failed\n");
close(fd);
exit(1);
}
*/
if(sendto(sock,buf,n,0,(struct sockaddr *) &addr,sizeof(addr)) < 0) {
perror("sendto");
exit(1);
}
fprintf(stderr,"Read %d from device\n",n);
}
} else {
fprintf(stderr,"Child is up\n");
u_int yes=1;
u_int no=0;
/*other sockets get get packets with the same port number*/
if (setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(yes)) < 0) {
perror("Reusing failed");
exit(1);
}
/*this socket will get packets from anywhere*/
addr.sin_addr.s_addr=htonl(INADDR_ANY);
if (bind(sock,(struct sockaddr *) &addr,sizeof(addr)) < 0) {
perror("bind");
exit(1);
}
/*and now, join the multicast group*/
mreq.imr_multiaddr.s_addr=inet_addr(argv[1]);
mreq.imr_interface.s_addr=htonl(INADDR_ANY);
if (setsockopt(sock,IPPROTO_IP,IP_ADD_MEMBERSHIP,&mreq,sizeof(mreq)) < 0) {
perror("setsockopt");
exit(1);
}
/*also make sure we won't have loops, eg. out packets will not get echoed back to us*/
if (setsockopt(sock,IPPROTO_IP,IP_MULTICAST_LOOP,&no,sizeof(no)) < 0) {
perror("IP_MULTICAST_LOOP = false failed");
exit(1);
}
while(1) {
/*
if ((n = read(0, buf, 10000))<0) {
fprintf(stderr,"Could not read from stdin");
close(fd);
exit(1);
}
*/
socklen_t addrlen=sizeof(addr);
if ((n=recvfrom(sock,buf,10000,0,(struct sockaddr *) &addr,&addrlen)) < 0) {
perror("recvfrom");
exit(1);
}
if(write(fd,buf,n)<0) {
fprintf(stderr,"write to device failed\n");
close(fd);
exit(1);
}
fprintf(stderr,"Write %d to device\n",n);
}
}
return 0;
}