-
Notifications
You must be signed in to change notification settings - Fork 8
/
tun.cpp
executable file
·73 lines (60 loc) · 1.58 KB
/
tun.cpp
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
#include "tun.h"
Tun::Tun() {
}
void Tun::conn(string arg_name) {
init(arg_name);
attach();
}
void Tun::init(string arg_name) {
name = arg_name;
}
void Tun::attach() {
struct ifreq ifr;
const char *DEV = "/dev/net/tun";
int flags;
int status;
flags = (IFF_TUN | IFF_NO_PI);
conn_fd = open(DEV , O_RDWR);
g_utils.handle_type1_error(conn_fd, "tunopen error: tun_attach");
memset(&ifr, 0, sizeof(ifr));
ifr.ifr_flags = flags;
if (name.size() != 0) {
strncpy(ifr.ifr_name, name.c_str(), IFNAMSIZ);
}
status = ioctl(conn_fd, TUNSETIFF, (void*)&ifr);
g_utils.handle_type1_error(status, "ioctl_tunsetiff error: network_attachtotun");
name.assign(ifr.ifr_name);
}
void Tun::snd(Packet pkt) {
int status;
status = write(conn_fd, pkt.data, pkt.len);
g_utils.handle_type1_error(status, "tun write error: tun_snd");
}
void Tun::rcv(Packet &pkt) {
int status;
pkt.clear_pkt();
status = read(conn_fd, pkt.data, BUF_SIZE);
g_utils.handle_type1_error(status, "tun read error: tun_rcv");
pkt.data_ptr = 0;
pkt.len = status;
}
void Tun::set_itf(string name, string ip_addr_sp) {
string rmtun;
string mktun;
string itf_up;
string add_addr;
string set_mtu;
rmtun = "sudo openvpn --rmtun --dev " + name;
mktun = "sudo openvpn --mktun --dev " + name;
itf_up = "sudo ip link set " + name + " up";
add_addr = "sudo ip addr add " + ip_addr_sp + " dev " + name;
set_mtu = "sudo ifconfig " + name + " mtu " + to_string(8000);
system(rmtun.c_str());
system(mktun.c_str());
system(itf_up.c_str());
system(add_addr.c_str());
// system(set_mtu.c_str());
}
Tun::~Tun() {
close(conn_fd);
}