-
-
Notifications
You must be signed in to change notification settings - Fork 84
/
tcpthread.cpp
277 lines (254 loc) · 8.69 KB
/
tcpthread.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
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*
* This file is part of cannelloni, a SocketCAN over Ethernet tunnel.
*
* Copyright (C) 2014-2023 Maximilian Güntner <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License, version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*/
#include <algorithm>
#include <chrono>
#include <cstddef>
#include <cstdint>
#include <cstdio>
#ifdef __GLIBCXX__
#include <bits/stdc++.h>
#endif
#include <linux/can.h>
#include <string.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <sys/select.h>
#include <sys/timerfd.h>
#include <net/if.h>
#include <arpa/inet.h>
#include <netinet/tcp.h>
#include "cannelloni.h"
#include "connection.h"
#include "logging.h"
#include "parser.h"
#include "tcpthread.h"
TCPThread::TCPThread(const struct debugOptions_t &debugOptions,
const struct TCPThreadParams ¶ms)
: ConnectionThread()
, m_debugOptions(debugOptions)
, m_serverSocket(0)
, m_socket(0)
, m_connect_state(DISCONNECTED)
, m_rxCount(0)
, m_txCount(0)
, m_addressFamily(params.addressFamily)
{
memcpy(&m_remoteAddr, ¶ms.remoteAddr, sizeof(struct sockaddr_storage));
memcpy(&m_localAddr, ¶ms.localAddr, sizeof(struct sockaddr_storage));
}
int TCPThread::start() {
return Thread::start();
}
void TCPThread::run() {
fd_set readfds;
uint8_t buffer[MAX_TRANSMIT_BUFFER_SIZE_BYTES];
uint8_t protocolVersionBuffer[] = CANNELLONI_CONNECT_V1_STRING;
/* Set interval to m_timeout */
m_blockTimer.adjust(SELECT_TIMEOUT, SELECT_TIMEOUT);
while (m_started) {
if (m_connect_state == DISCONNECTED) {
bool connect_successful = attempt_connect();
if (connect_successful) {
m_connect_state = CONNECTED;
ssize_t res = write(m_socket, protocolVersionBuffer, sizeof(protocolVersionBuffer)-1);
if (res != sizeof(protocolVersionBuffer)-1) {
lerror << "write error could not announce protocol" << std::endl;
disconnect();
continue;
}
} else {
/* Wait here for some time until the next attempt */
std::this_thread::sleep_for(std::chrono::seconds(2));
}
} else {
/* Prepare readfds */
FD_ZERO(&readfds);
FD_SET(m_socket, &readfds);
FD_SET(m_blockTimer.getFd(), &readfds);
FD_SET(m_framebufferHasDataPipe[SIGNAL_PIPE_READ], &readfds);
int ret = select(std::max({m_socket, m_blockTimer.getFd(), m_framebufferHasDataPipe[SIGNAL_PIPE_READ]})+1, &readfds, NULL, NULL, NULL);
if (ret < 0) {
if (errno == EOF) {
disconnect();
}
/* Check whether the remote has terminated the connection */
lerror << "select error" << std::endl;
continue;
}
if (FD_ISSET(m_blockTimer.getFd(), &readfds)) {
m_blockTimer.read();
/*
Let's flush out the frame buffer as well as there might be single frames left
that have not been signaled through the pipe due to blocking
*/
flushFrameBuffer();
}
if (FD_ISSET(m_framebufferHasDataPipe[SIGNAL_PIPE_READ], &readfds)) {
int signal;
ssize_t res = read(m_framebufferHasDataPipe[SIGNAL_PIPE_READ], &signal, sizeof(signal));
if (res == sizeof(signal)) {
flushFrameBuffer();
}
}
if (FD_ISSET(m_socket, &readfds)) {
ssize_t receivedBytes = 0;
ssize_t expectedBytes = 0;
if (m_connect_state == CONNECTED) {
expectedBytes = sizeof(CANNELLONI_CONNECT_V1_STRING)-1;
} else {
expectedBytes = m_decoder.expectedBytes;
}
if (expectedBytes != 0) {
/* check whether we can read enough bytes */
int available;
if (ioctl(m_socket, FIONREAD, &available) == -1) {
lerror << "ioctl failed" << std::endl;
disconnect();
continue;
} else if (available > 0 && available < static_cast<int>(expectedBytes)) {
/* not enough bytes are available, let's wait a bit */
std::this_thread::sleep_for(std::chrono::milliseconds(20));
continue;
}
receivedBytes = read(m_socket, buffer, expectedBytes);
if (receivedBytes < 0) {
lerror << "recvfrom error." << std::endl;
/* close connection */
disconnect();
continue;
} else if (receivedBytes == 0){
disconnect();
continue;
}
}
if (m_connect_state == CONNECTED) {
if (memcmp(buffer, protocolVersionBuffer, sizeof(CANNELLONI_CONNECT_V1_STRING)-1) == 0) {
m_connect_state = NEGOTIATED;
continue;
} else {
lwarn << "Invalid protocol detected" << std::endl;
disconnect();
continue;
}
} else {
m_decoder.expectedBytes = decodeFrame(buffer, receivedBytes, &m_decoder.tempFrame, &m_decoder.state);
if (m_decoder.expectedBytes == 0) {
canfd_frame *frameBufferFrame = m_peerThread->getFrameBuffer()->requestFrame(true, m_debugOptions.buffer);
if (frameBufferFrame != NULL) {
memcpy(frameBufferFrame, &m_decoder.tempFrame, sizeof(m_decoder.tempFrame));
m_peerThread->transmitFrame(frameBufferFrame);
} else {
lerror << "Dropping frame due to framebuffer issue." << std::endl;
}
m_rxCount++;
continue;
} else if (m_decoder.expectedBytes == -1) {
lerror << "Decoder Error" << std::endl;
disconnect();
continue;
}
}
}
}
}
if (m_debugOptions.buffer) {
m_frameBuffer->debug();
}
linfo << "Shutting down. TCP Transmission Summary: TX: " << m_txCount << " RX: " << m_rxCount << std::endl;
m_connect_state = DISCONNECTED;
close(m_socket);
cleanup();
}
void TCPThread::disconnect() {
m_connect_state = DISCONNECTED;
close(m_socket);
close(m_framebufferHasDataPipe[SIGNAL_PIPE_READ]);
close(m_framebufferHasDataPipe[SIGNAL_PIPE_WRITE]);
}
void TCPThread::transmitFrame(canfd_frame *frame) {
if (m_connect_state != NEGOTIATED) {
m_frameBuffer->insertFramePool(frame);
return;
}
m_frameBuffer->insertFrame(frame);
int signal = 1;
ssize_t res = write(m_framebufferHasDataPipe[SIGNAL_PIPE_WRITE], &signal, sizeof(signal));
if (res != sizeof(signal)) {
/*
when writing a lot of frames, the main loop might be too slow to consume the signals
from the pipe which is not an error
*/
if (errno != EWOULDBLOCK) {
lwarn << "could not write to pipe " << res << std::endl;
}
}
return;
}
void TCPThread::flushFrameBuffer() {
if (m_connect_state != NEGOTIATED) {
return;
}
uint8_t transmitBuffer[MAX_TRANSMIT_BUFFER_SIZE_BYTES];
m_frameBuffer->swapBuffers();
std::list<canfd_frame*> *frames = m_frameBuffer->getIntermediateBuffer();
for (auto it = frames->begin(); it != frames->end(); it++) {
canfd_frame* frame = *it;
ssize_t encodedBytes = encodeFrame(transmitBuffer, frame);
ssize_t bytesWritten = send(m_socket, transmitBuffer, encodedBytes, 0);
if (encodedBytes != bytesWritten) {
disconnect();
break;
}
m_txCount++;
}
m_frameBuffer->unlockIntermediateBuffer();
m_frameBuffer->mergeIntermediateBuffer();
}
bool TCPThread::setupSocket() {
const int nagle = 0;
const int min_window_size = 1;
if (setsockopt(m_socket, IPPROTO_TCP, TCP_WINDOW_CLAMP, &min_window_size, sizeof(min_window_size))) {
lerror << "Could not set window size to " << min_window_size << std::endl;
return false;
}
/* Disable Nagle for this connection */
if (setsockopt(m_socket, IPPROTO_TCP, TCP_NODELAY, &nagle, sizeof(nagle))) {
lerror << "Could not disable Nagle." << std::endl;
return false;
}
return true;
}
bool TCPThread::setupPipe() {
if (pipe(m_framebufferHasDataPipe) == -1) {
lerror << "could not inititalize signal pipe" << std::endl;
return false;
}
if (fcntl(m_framebufferHasDataPipe[SIGNAL_PIPE_WRITE], F_SETFL, O_NONBLOCK) <
0) {
lerror << "could not inititalize signal pipe" << std::endl;
return false;
}
return true;
}