-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy patheth_transport_vxlan.cpp
More file actions
192 lines (170 loc) · 4.41 KB
/
Copy patheth_transport_vxlan.cpp
File metadata and controls
192 lines (170 loc) · 4.41 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
182
183
184
185
186
187
188
189
190
191
192
#include "gen.h"
#include <fcntl.h>
#include <unistd.h>
#if defined(BUILD_FOR_PICO2W)
#include <WiFiUdp.h>
#elif defined(_WIN32)
#include "win32.h"
#elif defined(ESP32)
#include <arpa/inet.h>
#include <lwip/netdb.h>
#include <lwip/sockets.h>
#include <sys/socket.h>
#elif defined(TEENSY4_1)
#else
#include <poll.h>
#include <arpa/inet.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#endif
#if defined(__FreeBSD__)
#include <netinet/in.h>
#endif
#include "eth_transport_vxlan.h"
#include "log.h"
#include "utils.h"
// https://www.rfc-editor.org/rfc/rfc7348
constexpr const int max_pkt_size = 1512;
eth_transport_vxlan::eth_transport_vxlan(const std::string & peer, const int port, const uint32_t id):
peer(peer),
port(port),
id(id)
{
}
eth_transport_vxlan::~eth_transport_vxlan()
{
#if !defined(BUILD_FOR_PICO2W) && !defined(TEENSY4_1)
if (fd != -1)
close(fd);
#endif
}
bool eth_transport_vxlan::begin()
{
#if !defined(BUILD_FOR_PICO2W) && !defined(TEENSY4_1)
fd = socket(AF_INET, SOCK_DGRAM, 0);
if (fd == -1) {
DOLOG(log_ss::LS_ETH, "Cannot create socket: %s", strerror(errno));
return false;
}
sockaddr_in listen_addr { };
listen_addr.sin_family = AF_INET;
listen_addr.sin_addr.s_addr = htonl(INADDR_ANY);
listen_addr.sin_port = htons(port);
if (bind(fd, reinterpret_cast<struct sockaddr *>(&listen_addr), sizeof(listen_addr)) == -1) {
DOLOG(log_ss::LS_ETH, "Cannot bind to port %d: %s", port, strerror(errno));
close(fd);
fd = -1;
return false;
}
#endif
return true;
}
std::string eth_transport_vxlan::identifier() const
{
return format("vxlan:%s:%d/%d", peer.c_str(), port, id);
}
bool eth_transport_vxlan::transmit(const uint8_t *const data, const size_t n_bytes)
{
bool rc = true;
size_t wrapped_n = n_bytes + 8;
uint8_t *wrapped = new uint8_t[wrapped_n]();
wrapped[0] = 0x08;
wrapped[4] = id >> 16;
wrapped[5] = id >> 8;
wrapped[6] = id;
memcpy(&wrapped[8], data, n_bytes);
#if defined(BUILD_FOR_PICO2W) || defined(TEENSY4_1)
udp.begin(port);
udp.beginPacket(peer.c_str(), port);
udp.write(data, n_bytes);
if (udp.endPacket() == 0)
rc = false;
#else
sockaddr_in serveraddr { };
serveraddr.sin_family = AF_INET;
serveraddr.sin_port = htons(port);
#if defined(_WIN32)
#ifdef _WIN32_WINNT
serveraddr.sin_addr.s_addr = inet_addr(peer.c_str());
#else
if (inet_pton(AF_INET, peer.c_str(), &serveraddr.sin_addr) == 0) {
delete [] wrapped;
DOLOG(log_ss::LS_ETH, "inet_pton(%s) failed", peer.c_str());
return false;
}
#endif
#else
if (inet_aton(peer.c_str(), &serveraddr.sin_addr) == 0) {
delete [] wrapped;
DOLOG(log_ss::LS_ETH, "inet_aton(%s) failed", peer.c_str());
return false;
}
#endif
if (sendto(fd, reinterpret_cast<const char *>(wrapped), wrapped_n, 0, reinterpret_cast<const sockaddr *>(&serveraddr), sizeof serveraddr) == -1) {
DOLOG(log_ss::LS_ETH, "sendto failed: %s", strerror(errno));
rc = false;
}
#endif
delete [] wrapped;
pkt_cnt_tx++;
return rc;
}
std::pair<uint8_t *, size_t> eth_transport_vxlan::get(const int timeout)
{
uint8_t *pkt = nullptr;
size_t packet_size = 0;
#if defined(BUILD_FOR_PICO2W) || defined(TEENSY4_1)
auto start = millis();
while(millis() - start < timeout) {
int rc = udp.parsePacket();
if (rc > 0) {
pkt = new uint8_t[rc];
if (!pkt) {
DOLOG(log_ss::LS_ETH, "malloc issue");
return { nullptr, 0 };
}
udp.read(pkt, rc);
packet_size = rc;
break;
}
}
if (!pkt)
return { nullptr, 0 };
#else
#if defined(_WIN32)
WSAPOLLFD fds[] { { fd, POLLIN, 0 } };
int rc = WSAPoll(fds, 1, timeout);
#else
pollfd fds[] { { fd, POLLIN, 0 } };
int rc = poll(fds, 1, timeout);
#endif
if (rc <= 0)
return { nullptr, 0 };
pkt = new uint8_t[max_pkt_size]();
int rc2 = recv(fd, reinterpret_cast<char *>(pkt), max_pkt_size, 0);
if (rc2 == -1) {
delete [] pkt;
return { nullptr, 0 };
}
packet_size = rc2;
#endif
pkt_cnt_rx++;
if (packet_size < 14 + 8) {
delete [] pkt;
return { nullptr, 0 };
}
uint32_t their_id = (pkt[4] << 16) | (pkt[5] << 8) | pkt[6];
if (pkt[0] != 0x08 || their_id != id) {
DOLOG(log_ss::LS_ETH, "vxlan id mismatch: %02x != %02x", their_id, id);
delete [] pkt;
return { nullptr, 0 };
}
packet_size -= 8;
memmove(&pkt[0], &pkt[8], packet_size);
#if !IS_POSIX && !defined(_WIN32)
if (trace)
Serial.println(format("Pkt to %02x:%02x:%02x:%02x:%02x:%02x processed",
pkt[0], pkt[1], pkt[2], pkt[3], pkt[4], pkt[5]).c_str());
#endif
return { pkt, packet_size };
}