-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy patheth_transport_linux.cpp
More file actions
167 lines (137 loc) · 3.32 KB
/
eth_transport_linux.cpp
File metadata and controls
167 lines (137 loc) · 3.32 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
#include "gen.h"
#if defined(linux)
#include <cstring>
#include <errno.h>
#include <fcntl.h>
#include <poll.h>
#include <unistd.h>
#include <linux/if.h>
#include <linux/if_arp.h>
#include <linux/if_tun.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include "eth_transport_linux.h"
#include "log.h"
#include "utils.h"
static void set_ifr_name(ifreq *ifr, const std::string & dev_name)
{
memset(ifr->ifr_name, 0x00, IFNAMSIZ);
size_t copy_name_n = std::min(size_t(IFNAMSIZ), dev_name.size());
memcpy(ifr->ifr_name, dev_name.c_str(), copy_name_n);
}
static bool invoke_if_ioctl(const std::string & dev_name, const int ioctl_nr, ifreq *const p)
{
int temp_fd = socket(AF_INET, SOCK_DGRAM, 0);
if (temp_fd == -1)
DOLOG(log_ss::LS_ETH, "create socket failed");
else {
set_ifr_name(p, dev_name);
bool ok = true;
if (ioctl(temp_fd, ioctl_nr, p) == -1) {
DOLOG(log_ss::LS_ETH, "deqna: ioctl %d failed: %s", ioctl_nr, strerror(errno));
ok = false;
}
close(temp_fd);
return ok;
}
return false;
}
static int open_tun(const std::string & dev_name)
{
int fd = -1;
int temp_fd = -1;
do {
fd = open("/dev/net/tun", O_RDWR);
if (fd == -1) {
DOLOG(log_ss::LS_ETH, "cannot open /dev/net/tun");
break;
}
if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1) {
DOLOG(log_ss::LS_ETH, "FD_CLOEXEC on fd failed");
break;
}
// set if name
ifreq ifr_tap { };
ifr_tap.ifr_flags = IFF_TAP | IFF_NO_PI;
set_ifr_name(&ifr_tap, dev_name);
if (ioctl(fd, TUNSETIFF, &ifr_tap) == -1) {
DOLOG(log_ss::LS_ETH, "ioctl TUNSETIFF failed");
break;
}
// set running
ifr_tap.ifr_flags = IFF_UP | IFF_RUNNING | IFF_BROADCAST;
if (invoke_if_ioctl(dev_name, SIOCSIFFLAGS, &ifr_tap) == false)
break;
close(temp_fd);
return fd;
}
while(0);
if (temp_fd != -1)
close(temp_fd);
if (fd != -1)
close(fd);
return -1;
}
eth_transport_linux::eth_transport_linux(const std::string & dev_name) :
dev_name(dev_name)
{
}
eth_transport_linux::~eth_transport_linux()
{
if (fd != -1)
close(fd);
}
bool eth_transport_linux::begin()
{
const std::string script = "./kek-if-up.sh";
fd = open_tun(dev_name);
if (fd != -1) {
if (file_exists(script)) {
int rc = fork();
if (rc == 0) {
execl("/bin/bash", "-c", script.c_str(), dev_name.c_str(), nullptr);
exit(1);
}
else if (rc == -1) {
close(fd);
DOLOG(log_ss::LS_ETH, "cannot fork (%s)", strerror(errno));
return false;
}
wait(nullptr);
}
return true;
}
return false;
}
std::string eth_transport_linux::identifier() const
{
return "linux:" + dev_name;
}
bool eth_transport_linux::transmit(const uint8_t *const data, const size_t n_bytes)
{
ssize_t rc = WRITE(fd, reinterpret_cast<const char *>(data), n_bytes);
pkt_cnt_tx++;
return rc == ssize_t(n_bytes);
}
std::pair<uint8_t *, size_t> eth_transport_linux::get(const int timeout)
{
pollfd fds[] { { fd, POLLIN, 0 } };
int rc = poll(fds, 1, timeout);
if (rc <= 0)
return { nullptr, 0 };
pkt_cnt_rx++;
constexpr const int max_pkt_size = 1512;
uint8_t *pkt = new uint8_t[max_pkt_size]();
int rc2 = read(fd, pkt, max_pkt_size);
if (rc2 == -1) {
delete [] pkt;
pkt = nullptr;
rc2 = 0;
}
if (trace)
printf("Pkt to %02x:%02x:%02x:%02x:%02x:%02x processed\n",
pkt[0], pkt[1], pkt[2], pkt[3], pkt[4], pkt[5]);
return { pkt, rc2 };
}
#endif