Repository navigation
Expand file tree
/
Copy pathmain.c
More file actions
182 lines (148 loc) · 4.05 KB
/
Copy pathmain.c
File metadata and controls
182 lines (148 loc) · 4.05 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
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
/********************************************************
* Sorry, no garbage infomations here.
* Lisence: read the LISENCE file,
* Author(s): read the AUTHOR file and the git commit log,
* Code history: read the git commit log.
* That's all.
********************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <sys/ioctl.h>
#include <errno.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <linux/if.h>
#include <linux/if_tun.h>
#include "protocol.h"
#include "relayer.h"
#include "json_conf.h"
static char *config_file;
static void parse_args(int argc, char **argv)
{
int c;
do {
c=getopt(argc, argv, "c:");
switch (c) {
case 'c':
config_file = optarg;
break;
default:
break;
}
} while (c!=-1);
if (config_file==NULL) {
fprintf(stderr, "Usage:!!\n");
abort();
}
}
static int tun_alloc(char *dev, int flags) {
struct ifreq ifr;
int fd, err;
char *clonedev = "/dev/net/tun";
/* 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(clonedev, O_RDWR)) < 0 ) {
return fd;
}
/* preparation of the struct ifr, of type "struct ifreq" */
memset(&ifr, 0, sizeof(ifr));
ifr.ifr_flags = flags; /* 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, dev, IFNAMSIZ);
}
/* try to create the device */
if ( (err = ioctl(fd, TUNSETIFF, (void *) &ifr)) < 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;
}
static int shell(const char *cmd)
{
int ret;
printf("run: %s ... ", cmd);
ret = system(cmd);
if (ret==-1) {
printf("failed: %m.\n");
} else {
printf("status=%d.\n", ret);
}
return ret;
}
#define BUFSIZE 1024
int
main(int argc, char **argv)
{
int sd, tun_fd;
char tun_name[IFNAMSIZ];
struct sockaddr_in local_addr;
char cmdline[BUFSIZE];
cJSON *conf;
char *tun_local_addr, *tun_peer_addr;
char *rate;
int loss, delay;
parse_args(argc, argv);
fprintf(stderr, "Load config: %s\n", config_file);
conf = conf_load(config_file);
if (conf==NULL) {
fprintf(stderr, "Load config failed.\n");
exit(1);
}
tun_local_addr = conf_get_str("TunnelLocalAddr", conf);
tun_peer_addr = conf_get_str("TunnelPeerAddr", conf);
if (tun_local_addr==NULL || tun_peer_addr==NULL) {
fprintf(stderr, "Must define TunnelLocalAddr and TunnelPeerAddr in config file!\n");
exit(1);
}
sd = socket(PF_INET, SOCK_DGRAM, 0);
if (sd<0) {
perror("socket()");
exit(1);
}
local_addr.sin_family = PF_INET;
local_addr.sin_addr.s_addr = 0;
local_addr.sin_port = htons(*conf_get_int("LocalPort", conf));
if (bind(sd, (void*)&local_addr, sizeof(local_addr))<0) {
perror("bind()");
exit(1);
}
tun_name[0]='\0';
tun_fd = tun_alloc(tun_name, IFF_TUN | IFF_NO_PI);
if (tun_fd<0) {
perror("tun_alloc()");
exit(1);
}
snprintf(cmdline, BUFSIZE, "ip addr add dev %s %s peer %s", tun_name, tun_local_addr, tun_peer_addr);
shell(cmdline);
snprintf(cmdline, BUFSIZE, "ip link set dev %s up", tun_name);
shell(cmdline);
rate = conf_get_str("Rate", conf);
loss = *conf_get_int("Loss", conf);
delay = *conf_get_int("Delay", conf);
snprintf(cmdline, BUFSIZE, "tc qdisc add dev %s root netem delay %dms %dms rate %sbit loss random %d", tun_name, delay, delay/4, rate, loss);
shell(cmdline);
relay(sd, tun_fd, conf);
close(sd);
close(tun_fd);
return 0;
}