forked from n7tae/mrefd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprotocol.cpp
328 lines (286 loc) · 6.98 KB
/
protocol.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
//
// cprotocol.cpp
// mrefd
//
// Created by Jean-Luc Deltombe (LX3JL) on 01/11/2015.
// Copyright © 2015 Jean-Luc Deltombe (LX3JL). All rights reserved.
// Copyright © 2020 Thomas A. Early, N7TAE
//
// ----------------------------------------------------------------------------
// This file is part of mrefd.
//
// mrefd is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// mrefd 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 Foobar. If not, see <http://www.gnu.org/licenses/>.
// ----------------------------------------------------------------------------
#include "main.h"
#include "protocol.h"
#include "clients.h"
#include "reflector.h"
////////////////////////////////////////////////////////////////////////////////////////
// constructor
CProtocol::CProtocol() : keep_running(true) {}
////////////////////////////////////////////////////////////////////////////////////////
// destructor
CProtocol::~CProtocol()
{
// kill threads
Close();
// empty queue
m_Queue.Lock();
while ( !m_Queue.empty() )
{
m_Queue.pop();
}
m_Queue.Unlock();
}
////////////////////////////////////////////////////////////////////////////////////////
// initialization
bool CProtocol::Initialize(int ptype, const uint16_t port, const bool has_ipv4, const bool has_ipv6)
{
// init reflector apparent callsign
m_ReflectorCallsign = g_Reflector.GetCallsign();
// reset stop flag
keep_running = true;
// create our sockets
#ifdef LISTEN_IPV4
if (has_ipv4)
{
CIp ip4(AF_INET, port, LISTEN_IPV4);
if ( ip4.IsSet() )
{
if (! m_Socket4.Open(ip4))
return false;
}
std::cout << "Listening on " << ip4 << std::endl;
}
#endif
#ifdef LISTEN_IPV6
if (has_ipv6)
{
CIp ip6(AF_INET6, port, LISTEN_IPV6);
if ( ip6.IsSet() )
{
if (! m_Socket6.Open(ip6))
{
m_Socket4.Close();
return false;
}
std::cout << "Listening on " << ip6 << std::endl;
}
}
#endif
try {
m_Future = std::async(std::launch::async, &CProtocol::Thread, this);
}
catch (const std::exception &e)
{
std::cerr << "Could not start protocol on port " << port << ": " << e.what() << std::endl;
m_Socket4.Close();
m_Socket6.Close();
return false;
}
return true;
}
void CProtocol::Thread()
{
while (keep_running)
{
Task();
}
}
void CProtocol::Close(void)
{
keep_running = false;
if ( m_Future.valid() )
{
m_Future.get();
}
m_Socket4.Close();
m_Socket6.Close();
}
////////////////////////////////////////////////////////////////////////////////////////
// streams helpers
void CProtocol::OnPacketIn(std::unique_ptr<CPacket> &packet, const CIp &ip)
{
// find the stream
CPacketStream *stream = GetStream(packet->GetStreamId(), ip);
if ( stream )
{
auto islast = packet->IsLastPacket(); // we'll need this after the std::move()!
// and push the packet
stream->Lock();
stream->Push(std::move(packet));
stream->Unlock();
if (islast)
g_Reflector.CloseStream(stream);
}
}
////////////////////////////////////////////////////////////////////////////////////////
// stream handle helpers
CPacketStream *CProtocol::GetStream(uint16_t uiStreamId, const CIp &Ip)
{
for ( auto it=m_Streams.begin(); it!=m_Streams.end(); it++ )
{
if ( (*it)->GetPacketStreamId() == uiStreamId )
{
// if Ip not nullptr, also check if IP match
if ( (*it)->GetOwnerIp() != nullptr )
{
if ( Ip == *((*it)->GetOwnerIp()) )
{
return *it;
}
}
}
}
// done
return nullptr;
}
void CProtocol::CheckStreamsTimeout(void)
{
for ( auto it=m_Streams.begin(); it!=m_Streams.end(); )
{
// time out ?
(*it)->Lock();
if ( (*it)->IsExpired() )
{
// yes, close it
(*it)->Unlock();
g_Reflector.CloseStream(*it);
// and remove it
it = m_Streams.erase(it);
}
else
{
(*it++)->Unlock();
}
}
}
////////////////////////////////////////////////////////////////////////////////////////
// syntax helper
bool CProtocol::IsNumber(char c) const
{
return ((c >= '0') && (c <= '9'));
}
bool CProtocol::IsLetter(char c) const
{
return ((c >= 'A') && (c <= 'Z'));
}
bool CProtocol::IsSpace(char c) const
{
return (c == ' ');
}
////////////////////////////////////////////////////////////////////////////////////////
// Receivers
ssize_t CProtocol::Receive6(uint8_t *buf, CIp &ip, int time_ms)
{
return m_Socket6.Receive(buf, ip, time_ms);
}
ssize_t CProtocol::Receive4(uint8_t *buf, CIp &ip, int time_ms)
{
return m_Socket4.Receive(buf, ip, time_ms);
}
ssize_t CProtocol::ReceiveDS(uint8_t *buf, CIp &ip, int time_ms)
{
auto fd4 = m_Socket4.GetSocket();
auto fd6 = m_Socket6.GetSocket();
if (fd4 < 0)
{
if (fd6 < 0)
return false;
return m_Socket6.Receive(buf, ip, time_ms);
}
else if (fd6 < 0)
return m_Socket4.Receive(buf, ip, time_ms);
fd_set fset;
FD_ZERO(&fset);
FD_SET(fd4, &fset);
FD_SET(fd6, &fset);
int max = (fd4 > fd6) ? fd4 : fd6;
struct timeval tv;
tv.tv_sec = time_ms / 1000;
tv.tv_usec = (time_ms % 1000) * 1000;
auto rval = select(max+1, &fset, 0, 0, &tv);
if (rval <= 0)
{
if (rval < 0)
std::cerr << "ReceiveDS select error: " << strerror(errno) << std::endl;
return false;
}
if (FD_ISSET(fd4, &fset))
return m_Socket4.ReceiveFrom(buf, ip);
else
return m_Socket6.ReceiveFrom(buf, ip);
}
////////////////////////////////////////////////////////////////////////////////////////
// dual stack senders
void CProtocol::Send(const char *buf, const CIp &Ip) const
{
switch (Ip.GetFamily())
{
case AF_INET:
m_Socket4.Send(buf, Ip);
break;
case AF_INET6:
m_Socket6.Send(buf, Ip);
break;
default:
std::cerr << "ERROR: wrong family: " << Ip.GetFamily() << std::endl;
break;
}
}
void CProtocol::Send(const uint8_t *buf, size_t size, const CIp &Ip) const
{
switch (Ip.GetFamily())
{
case AF_INET:
m_Socket4.Send(buf, size, Ip);
break;
case AF_INET6:
m_Socket6.Send(buf, size, Ip);
break;
default:
std::cerr << "ERROR: wrong family: " << Ip.GetFamily() << std::endl;
break;
}
}
void CProtocol::Send(const char *buf, const CIp &Ip, uint16_t port) const
{
switch (Ip.GetFamily())
{
case AF_INET:
m_Socket4.Send(buf, Ip, port);
break;
case AF_INET6:
m_Socket6.Send(buf, Ip, port);
break;
default:
std::cerr << "ERROR: wrong family: " << Ip.GetFamily() << " on port " << port << std::endl;
break;
}
}
void CProtocol::Send(const uint8_t *buf, size_t size, const CIp &Ip, uint16_t port) const
{
switch (Ip.GetFamily())
{
case AF_INET:
m_Socket4.Send(buf, size, Ip, port);
break;
case AF_INET6:
m_Socket6.Send(buf, size, Ip, port);
break;
default:
std::cerr << "ERROR: wrong family: " << Ip.GetFamily() << " on port " << port << std::endl;
break;
}
}