-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathprotocol.cpp
More file actions
1281 lines (1167 loc) · 34.1 KB
/
protocol.cpp
File metadata and controls
1281 lines (1167 loc) · 34.1 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
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// mrefd
//
// Created by Jean-Luc Deltombe (LX3JL) on 01/11/2015.
// Copyright © 2015 Jean-Luc Deltombe (LX3JL). All rights reserved.
// Copyright © 2022-2025 Thomas A. Early, N7TAE
//
// ----------------------------------------------------------------------------
// This file is part of mrefd.
//
// This program 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.
//
// 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 Foobar. If not, see <http://www.gnu.org/licenses/>.
// ----------------------------------------------------------------------------
#include "defines.h"
#include "protocol.h"
#include "clients.h"
#include "reflector.h"
#include "gatekeeper.h"
#include "configure.h"
#include "interlinks.h"
#include "frametype.h"
#include "position.h"
extern CConfigure g_CFG;
extern CGateKeeper g_GateKeeper;
extern CReflector g_Reflector;
extern CInterlinks g_Interlinks;
////////////////////////////////////////////////////////////////////////////////////////
// constructor
CProtocol::CProtocol() : keep_running(true), publish(true)
{
peerRegEx = std::regex("^M17-[A-Z0-9]{3,3}( [A-Z])?$", std::regex::extended);
clientRegEx = std::regex("^([0-9]?[A-Z]{1,2}[0-9]{0,2}/)?[0-9]?[A-Z]{1,2}[0-9]{1,2}[A-Z]{1,4}([ -/\\.][A-Z0-9 -/\\.]*)?$", std::regex::extended);
lstnRegEx = std::regex("^[0-9]?[A-Z][A-Z0-9]{2,8}$", std::regex::extended);
}
////////////////////////////////////////////////////////////////////////////////////////
// destructor
CProtocol::~CProtocol()
{
Close();
}
////////////////////////////////////////////////////////////////////////////////////////
// initialization
// returns true on error
bool CProtocol::Initialize(const uint16_t port, const std::string &strIPv4, const std::string &strIPv6)
{
// init reflector apparent callsign
m_ReflectorCallsign = g_CFG.GetCallsign();
// reset stop flag
keep_running = true;
// create our sockets
if (not strIPv4.empty())
{
CIp ip4(AF_INET, port, strIPv4.c_str());
if (ip4.IsSet())
{
if (m_Socket4.Open(ip4))
return true;
}
std::cout << "Listening on " << ip4 << std::endl;
}
if (not strIPv6.empty())
{
CIp ip6(AF_INET6, port, strIPv6.c_str());
if (ip6.IsSet())
{
if (m_Socket6.Open(ip6))
{
m_Socket4.Close();
return true;
}
std::cout << "Listening on " << ip6 << std::endl;
}
}
// set up the Receive function pointer
if (strIPv4.empty())
{
Receive = &CProtocol::Receive6;
}
else
{
if (strIPv6.empty())
{
Receive = &CProtocol::Receive4;
}
else
{
Receive = &CProtocol::ReceiveDS;
}
}
for (const auto &mod : g_CFG.GetRefMods().GetModules())
{
m_streamMap[mod] = std::make_unique<CPacketStream>();
}
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 true;
}
// update time
m_LastKeepaliveTime.Start();
m_LastPeersLinkTime.Start();
// done
return false;
}
void CProtocol::Thread()
{
while (keep_running)
{
Task();
}
}
////////////////////////////////////////////////////////////////////////////////////////
// task
void CProtocol::Task(void)
{
CIp ip;
char mod = 0;
char mods[27];
CCallsign cs;
CPacket pack;
// any incoming packet ?
auto len = (*this.*Receive)(pack.GetData(), ip, 20);
switch (len)
{
case 0:
break;
case 10:
if (IsValidKeepAlive(pack.GetCData(), cs))
{
auto client = g_Reflector.GetClients()->FindClient(ip);
g_Reflector.ReleaseClients();
if (client)
{
const auto ct = client->GetClientType();
if (EClientType::simple == ct or EClientType::listenonly == ct)
client->Alive();
else
{
auto peer = g_Reflector.GetPeers().FindPeer(ip);
if (peer)
peer->Alive();
}
}
}
else if (IsValidDisconnect(pack.GetCData(), cs))
{
auto client = g_Reflector.GetClients()->FindClient(ip);
g_Reflector.ReleaseClients();
if (client)
{
const auto ct = client->GetClientType();
if (EClientType::simple == ct or EClientType::listenonly == ct)
{
std::cout << "Disconnect packet from " << cs << " at " << ip << std::endl;
uint8_t disc[4];
EncodeDisconnectedPacket(disc);
Send(disc, 4, ip);
g_Reflector.GetClients()->RemoveClient(client);
g_Reflector.ReleaseClients();
}
else
{
auto peer = g_Reflector.GetPeers().FindPeer(ip);
if (peer)
{
g_Reflector.GetPeers().RemovePeer(peer);
}
}
}
}
else if (IsValidNAcknowledge(pack.GetCData(), cs))
{
std::cout << "NACK packet received from " << cs << " at " << ip << std::endl;
}
break;
case 11:
if (IsValidConnect(pack.GetCData(), ip, cs, mod))
{
bool isLstn = (0 == memcmp(pack.GetCData(), "LSTN", 4));
std::cout << "Connect packet for module " << mod << " from " << cs << " at " << ip << (isLstn ? " as listen-only" : "") << std::endl;
// callsign authorized?
if (g_GateKeeper.ClientMayLink(cs, ip))
{
// valid module ?
if (g_CFG.IsValidModule(mod))
{
// acknowledge a normal request from a repeater/hot-spot/mvoice
EncodeConnectAckPacket(pack.GetData());
Send(pack.GetCData(), 4, ip);
// create the client and append
if (isLstn)
{
if (g_CFG.GetRefMods().GetEModules().find(mod) != std::string::npos && not g_CFG.GetSWLEncryptedMods())
{
std::cout << "SWL Node " << cs << " is not allowed to connect to encrypted Module '" << mod << "'" << std::endl;
// deny the request
EncodeConnectNackPacket(pack.GetData());
Send(pack.GetCData(), 4, ip);
}
else
{
if (AF_INET6 == ip.GetFamily())
g_Reflector.GetClients()->AddClient(std::make_shared<CClient>(cs, ip, EClientType::listenonly, mod, m_Socket6));
else
g_Reflector.GetClients()->AddClient(std::make_shared<CClient>(cs, ip, EClientType::listenonly, mod, m_Socket4));
g_Reflector.ReleaseClients();
}
}
else
{
if (AF_INET6 == ip.GetFamily())
g_Reflector.GetClients()->AddClient(std::make_shared<CClient>(cs, ip, EClientType::simple, mod, m_Socket6));
else
g_Reflector.GetClients()->AddClient(std::make_shared<CClient>(cs, ip, EClientType::simple, mod, m_Socket4));
g_Reflector.ReleaseClients();
}
}
else
{
std::cout << "Node " << cs << " connect attempt on non-existing module '" << mod << "'" << std::endl;
// deny the request
EncodeConnectNackPacket(pack.GetData());
Send(pack.GetCData(), 4, ip);
}
}
else
{
// deny the request
EncodeConnectNackPacket(pack.GetData());
Send(pack.GetCData(), 4, ip);
}
}
break;
case sizeof(SInterConnect):
if (IsValidInterlinkConnect(pack.GetCData(), ip, cs, mods))
{
std::cout << "CONN packet from " << cs << " at " << ip << " to module(s) " << mods << std::endl;
// callsign authorized?
if (g_GateKeeper.PeerMayLink(cs))
{
SInterConnect ackn;
// acknowledge the request
EncodeInterlinkAckPacket(ackn, mods);
Send(ackn.magic, sizeof(SInterConnect), ip);
}
else
{
// deny the request
EncodeInterlinkNackPacket(pack.GetData());
Send(pack.GetCData(), 10, ip);
}
}
else if (IsValidInterlinkAcknowledge(pack.GetCData(), cs, mods))
{
std::cout << "ACQN packet from " << cs << " at " << ip << " on module(s) " << mods << std::endl;
// callsign authorized?
if (g_GateKeeper.PeerMayLink(cs))
{
// already connected ?
if (nullptr == g_Reflector.GetPeers().FindPeer(cs))
{
auto item = g_Interlinks.Find(cs.GetCS());
if (item)
{
auto type = item->IsNotLegacy() ? EClientType::reflector : EClientType::legacy;
// create the new peer
// this also create one client per module
if (AF_INET6 == ip.GetFamily())
g_Reflector.GetPeers().AddPeer(std::make_shared<CPeer>(cs, ip, type, mods, m_Socket6));
else
g_Reflector.GetPeers().AddPeer(std::make_shared<CPeer>(cs, ip, type, mods, m_Socket4));
publish = true;
}
else
{
std::cerr << "ERROR: got an ACKN packet from " << cs.GetCS() << " but could not find the interlink item!" << std::endl;
}
}
}
}
break;
default:
if (len > sizeof(SInterConnect))
{
CCallsign dst, src;
auto client = GetClient(ip, len, pack, dst, src);
if (client)
{
// std::cout << "Data:" << (pack.IsStreamData()?"Stream":"Packet") << " Module:" << mod << " Client:" << client->GetCallsign() << " SRC:" << src << " IP:" << ip << std::endl;
// make sure the SRC callsign is not blacklisted
if (g_GateKeeper.MayTransmit(src, ip))
{
// might open a new stream if it's the first packet
// if there is a problem, return false
if (OnPacketIn(pack, client))
{
if (0 == ((0x7fffu & pack.GetFrameNumber()) % 6))
UpdateDashData(src, dst, client, pack);
SendToClients(pack, client, dst);
if (pack.IsStreamData() and pack.IsLastPacket())
{
CloseStream(client->GetReflectorModule()); // so this only closes streams
}
}
}
else if (pack.IsStreamData())
{
// this voicestream is blocked, so
if (pack.GetFrameNumber() & 0x8000u)
{
// when the stream closes, log it.
std::cout << "Blocked voice stream from " << src << " at " << ip << std::endl;
}
}
else
{
// here is a blocked PM packet
std::cout << "Blocked Packet from " << src << " at " << ip << std::endl;
}
}
}
break;
}
// Now we do all the other maintenance
// handle end of streaming timeout
CheckStreamsTimeout();
// keep alive
if (m_LastKeepaliveTime.Time() > M17_KEEPALIVE_PERIOD)
{
// handle keep alives
HandleKeepalives();
// update time
m_LastKeepaliveTime.Start();
}
// peer connections
if (m_LastPeersLinkTime.Time() > M17_RECONNECT_PERIOD)
{
// handle remote peers connections
HandlePeerLinks();
// update time
m_LastPeersLinkTime.Start();
}
}
void CProtocol::Close(void)
{
keep_running = false;
if (m_Future.valid())
{
m_Future.get();
}
m_streamMap.clear();
m_Socket4.Close();
m_Socket6.Close();
}
////////////////////////////////////////////////////////////////////////////////////////
// dashboard data handler
void CProtocol::UpdateDashData(const CCallsign &src, const CCallsign &dst, SPClient client, const CPacket &pack)
{
const CFrameType t(pack.GetFrameType());
auto users = g_Reflector.GetUsers();
users->Hearing(src, dst, client->GetCallsign(), client->GetReflectorModule(), (pack.IsStreamData() ? EMode::sm : EMode::pm));
if (EMetaDatType::gnss == t.GetMetaDataType())
{
CPosition p(pack.GetCMetaData());
std::string lat, lon;
const std::string maid(p.GetPosition(lat, lon));
if (not maid.empty())
users->Location(src, maid, lat, lon);
}
g_Reflector.ReleaseUsers();
client->Heard();
}
////////////////////////////////////////////////////////////////////////////////////////
// stream handle helpers
CPacketStream *CProtocol::GetStream(CPacket &packet, const SPClient client)
{
// this is only for stream mode packet
if (packet.IsPacketData())
return nullptr;
// get the stream based on the DST module
const auto mod = client->GetReflectorModule();
const auto pit = m_streamMap.find(mod);
// does the dst module exist?
if (m_streamMap.end() == pit)
{
#ifdef DEBUG
const CCallsign dst(packet.GetCDstAddress());
const CCallsign src(packet.GetCSrcAddress());
std::cout << "Bad incoming packet on module '" << mod << "' to " << dst.GetCS() << " from " << src.GetCS() << std::endl;
#endif
return nullptr;
}
if (pit->second->IsOpen())
{
// the stream is opened. do the SIDs match?
if (pit->second->GetPacketStreamId() == packet.GetStreamId())
{
return pit->second.get();
}
}
// done
return nullptr;
}
void CProtocol::CheckStreamsTimeout(void)
{
// check the packetstreams for each module
for (auto &it : m_streamMap)
{
// time out ?
if (it.second->IsExpired())
CloseStream(it.first);
}
// check each item in the parrot map
for (auto pit = parrotMap.begin(); pit != parrotMap.end();)
{
switch (pit->second->GetState())
{
case EParrotState::record:
if (pit->second->IsExpired())
{
std::cout << "Parrot stream from " << pit->second->GetSRC() << " timed out! Playing..." << std::endl;
pit->second->Play();
}
pit++;
break;
case EParrotState::done:
if (pit->second->IsStream())
{
auto psp = static_cast<CStreamParrot *>(pit->second.get());
std::cout << psp->GetSize() << " packet parrot stream from " << psp->GetSRC() << " played back to " << pit->first->GetCallsign() << " at " << pit->first->GetIp() << std::endl;
}
else
{
std::cout << "Parrot packet from " << pit->second->GetSRC() << " played back to " << pit->first->GetCallsign() << " at " << pit->first->GetIp() << std::endl;
}
pit->second->Quit(); // get() the future
pit->second.reset(); // destroy the parrot object
pit = parrotMap.erase(pit); // remove the map std::pair, incrementing the pointer
break;
default:
break;
}
}
}
////////////////////////////////////////////////////////////////////////////////////////
// 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
unsigned CProtocol::Receive6(uint8_t *buf, CIp &ip, int time_ms)
{
return m_Socket6.Receive(buf, ip, time_ms);
}
unsigned CProtocol::Receive4(uint8_t *buf, CIp &ip, int time_ms)
{
return m_Socket4.Receive(buf, ip, time_ms);
}
unsigned 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 0;
}
if (FD_ISSET(fd4, &fset))
return m_Socket4.ReceiveFrom(buf, ip);
else
return m_Socket6.ReceiveFrom(buf, ip);
}
////////////////////////////////////////////////////////////////////////////////////////
// dual stack sender
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;
}
}
////////////////////////////////////////////////////////////////////////////////////////
// queue helper
void CProtocol::SendToClients(CPacket &packet, const SPClient &txclient, const CCallsign &dst)
{
// std::cout << "SendToAll DST=" << CCallsign(packet.GetCDstAddress()) << " SRC=" << CCallsign(packet.GetCSrcAddress()) << std::endl;
// Dump(packet.GetCData(), packet.GetSize());
// push it to all our clients linked to the module
SPClient client = nullptr;
auto clients = g_Reflector.GetClients();
auto it = clients->begin();
const auto mod = txclient->GetReflectorModule();
while (nullptr != (client = clients->FindNextClient(mod, it)))
{
if (txclient == client)
continue; // don't send data back to the originator
if (packet.IsStreamData())
{
// the client is not parroting
if (parrotMap.end() != parrotMap.find(client))
continue;
const auto fromtype = packet.GetFromType();
switch (client->GetClientType())
{
case EClientType::legacy:
// reflectors will only get streaming data from simple clients
if (EClientType::simple == fromtype)
{
// legacy reflectors have to be properly addressed
// set the address and calculate the CRC
client->GetCallsign().CodeOut(packet.GetDstAddress());
packet.CalcCRC();
packet.SetSize(55u);
// one last thing, set the 55th bit to true
const bool b = true;
packet.GetData()[54] = uint8_t(b);
// send this data
client->SendPacket(packet);
// restore the DST address and CRC
dst.CodeOut(packet.GetDstAddress());
packet.CalcCRC();
}
break;
case EClientType::reflector:
// reflectors will only get data from streaming client
if (EClientType::simple == fromtype)
{
packet.SetSize(55u);
packet.GetData()[54] = uint8_t(mod);
client->SendPacket(packet);
}
break;
default:
// all local clients, simple and listen-only, get data from anywhere
// bogus listen-only input is blocked in OnPacketIn
packet.SetSize(54u);
client->SendPacket(packet);
break;
}
}
else // this is packet data
{
const auto ct = client->GetClientType();
switch (packet.GetFromType())
{
case EClientType::reflector:
if (EClientType::simple == ct or EClientType::listenonly == ct)
{
// the packet has already been trimmed in GetClient()
client->SendPacket(packet);
}
break;
case EClientType::simple:
if (EClientType::legacy == ct)
break; // legacy reflectors don't get packet data
if (EClientType::reflector == ct)
{
const auto size = packet.GetSize();
packet.SetSize(size + 1);
packet.GetData()[size] = uint8_t(mod);
client->SendPacket(packet);
packet.SetSize(size);
}
else
client->SendPacket(packet);
break;
default:
break;
}
}
}
g_Reflector.ReleaseClients();
}
////////////////////////////////////////////////////////////////////////////////////////
// keepalive helpers
void CProtocol::HandleKeepalives(void)
{
uint8_t keepalive[10];
EncodeKeepAlivePacket(keepalive);
// iterate on clients
auto clients = g_Reflector.GetClients();
auto it = clients->begin();
SPClient client;
while (nullptr != (client = clients->FindNextClient(it)))
{
// don't ping reflector modules, we'll do each interlinked refectors after this while loop
if (0 == client->GetCallsign().GetCS(4).compare("M17-"))
continue;
// send keepalive
Send(keepalive, 10, client->GetIp());
// client busy ?
if (client->IsTransmitting())
{
// yes, just tickle it
client->Alive();
}
// otherwise check if still with us
else if (not client->IsAlive())
{
const auto type = client->GetClientType();
if (EClientType::simple == type or EClientType::listenonly == type)
{
// no, disconnect
uint8_t disconnect[10];
EncodeDisconnectPacket(disconnect, client->GetReflectorModule());
Send(disconnect, 10, client->GetIp());
// remove it
std::cout << "Client " << client->GetCallsign() << " keepalive timeout" << std::endl;
clients->RemoveClient(client);
}
}
}
g_Reflector.ReleaseClients();
// iterate on peers
auto pit = g_Reflector.GetPeers().Begin();
SPPeer peer;
while ((peer = g_Reflector.GetPeers().FindNextPeer(pit)))
{
// send keepalive
Send(keepalive, 10, peer->GetIp());
// client busy ?
if (peer->IsTransmitting())
{
// yes, just tickle it
peer->Alive();
}
// otherwise check if still with us
else if (not peer->IsAlive())
{
// no, disconnect
uint8_t disconnect[10];
EncodeDisconnectPacket(disconnect, 0);
Send(disconnect, 10, peer->GetIp());
// remove it
std::cout << "Peer " << peer->GetCallsign() << " keepalive timeout" << std::endl;
g_Reflector.GetPeers().RemovePeer(peer);
}
}
}
////////////////////////////////////////////////////////////////////////////////////////
// Peers helpers
void CProtocol::HandlePeerLinks(void)
{
// check if all our connected peers are still listed in mrefd.interlink
// if not, disconnect
auto pit = g_Reflector.GetPeers().Begin();
SPPeer peer = nullptr;
while ((peer = g_Reflector.GetPeers().FindNextPeer(pit)))
{
const auto cs = peer->GetCallsign().GetCS();
if (nullptr == g_Interlinks.Find(cs))
{
uint8_t buf[10];
// send disconnect packet
EncodeDisconnectPacket(buf, 0);
Send(buf, 10, peer->GetIp());
std::cout << "Sent disconnect packet to M17 peer " << cs << " at " << peer->GetIp() << std::endl;
// remove client
g_Reflector.GetPeers().RemovePeer(peer);
publish = true;
}
}
// check if all ours peers listed in mrefd.interlink are connected
// if not, connect or reconnect
for (auto it = g_Interlinks.begin(); it != g_Interlinks.end(); it++)
{
auto &item = it->second;
const auto cs = item->GetCallsign().GetCS();
if (item->GetIp().IsSet())
{
if (nullptr == g_Reflector.GetPeers().FindPeer(item->GetCallsign()))
{
// send connect packet to re-initiate peer link
SInterConnect connect;
const auto mods = item->GetReqMods();
EncodeInterlinkConnectPacket(connect, mods);
Send(connect.magic, sizeof(SInterConnect), item->GetIp());
std::cout << "Sent connect packet to M17 peer " << item->GetCallsign() << " @ " << item->GetIp() << " for module(s) " << mods << std::endl;
}
}
else
{
#ifdef NO_DHT
std::cerr << "ERROR: " << cs << " doesn't have a vaild IP address!" << std::endl;
#else
if (item->IsUsingDHT())
g_Reflector.GetDHTConfig(cs);
else
std::cerr << "ERROR: IP adress for " << item->GetCallsign() << " has not been initialized!" << std::endl;
#endif
}
}
#ifndef NO_DHT
if (publish)
{
g_Reflector.PutDHTPeers();
publish = false;
}
#endif
}
////////////////////////////////////////////////////////////////////////////////////////
// streams helpers
// returns true if the packet is ready for distribution
bool CProtocol::OnPacketIn(CPacket &packet, const SPClient client)
{
// if the packet dst looks like an M17 reflector, change the dst to @ALL
CCallsign dst(packet.GetCDstAddress());
const auto cs = dst.GetCS();
if (0 == cs.compare(0, 4, "M17-"))
{
dst.CSIn("@ALL");
dst.CodeOut(packet.GetDstAddress());
packet.CalcCRC();
}
else if (std::string::npos != cs.find("PARROT"))
{
auto item = parrotMap.find(client);
if (parrotMap.end() == item)
{
CFrameType t(packet.GetFrameType());
if (packet.IsStreamData())
{
if (not packet.IsLastPacket())
{
const CCallsign src(packet.GetCSrcAddress());
if (EEncryptType::none == t.GetEncryptType())
{
// it is not the last packet and it is a stream packet and it is not enrypted
std::cout << "Parrot stream from " << src << " on " << client->GetCallsign() << " with SID 0x" << std::hex << packet.GetStreamId() << std::dec << " at " << client->GetIp() << std::endl;
parrotMap[client] = std::make_unique<CStreamParrot>(packet.GetCSrcAddress(), client, packet.GetFrameType());
parrotMap[client]->Add(packet);
}
else
{
std::cout << "Parrot stream from " << src << " on " << client->GetCallsign() << " was rejected because it was entrypted" << std::endl;
}
}
}
else
{
const CCallsign src(packet.GetCSrcAddress());
std::cout << "Parrot Packet from " << src << " on " << client->GetCallsign() << " at " << client->GetIp() << std::endl;
parrotMap[client] = std::make_unique<CPacketParrot>(packet.GetCSrcAddress(), client, packet.GetFrameType());
parrotMap[client]->Add(packet);
parrotMap[client]->Play();
}
}
else
{
if (EParrotState::record == item->second->GetState())
{
item->second->Add(packet);
if (packet.IsLastPacket())
{
std::cout << "Parrot stream 0x" << std::hex << packet.GetStreamId() << std::dec << " closed, playing..." << std::endl;
item->second->Play();
}
}
}
return false;
}
if (client->IsListenOnly())
{
if (packet.IsLastPacket())
std::cerr << "Listen-only client " << client->GetCallsign() << " is sending data!" << std::endl;
return false;
}
if (packet.IsStreamData())
{
auto stream = GetStream(packet, client);
if (stream)
{
// stream already open
// skip packet, but tickle the stream
stream->Tickle();
}
else
{
// try to open the stream
stream = OpenStream(packet, client);
if (nullptr == stream)
{
return false;
}
}
}
return true;
}
////////////////////////////////////////////////////////////////////////////////////////
// packet decoding helpers
bool CProtocol::IsValidConnect(const uint8_t *buf, const CIp &ip, CCallsign &cs, char &mod)
{
if (0 == memcmp(buf, "CONN", 4))
{
cs.CodeIn(buf + 4);
if (std::regex_match(cs.GetCS(), clientRegEx))
{
mod = buf[10];
if (IsLetter(mod))
{
return true;
}
std::cout << "Bad CONN from '" << cs.GetCS() << "'. at " << ip << std::endl;
Dump("The requested module is not a letter:", buf, 11);
}
else
{
if (cs.GetCS(4).compare("WPSD"))
std::cout << "CONN packet from " << ip << " rejected because '" << cs.GetCS() << "' didn't pass the regex!" << std::endl;
}
}
else if (0 == memcmp(buf, "LSTN", 4))
{
cs.CodeIn(buf + 4);
if (std::regex_match(cs.GetCS(), lstnRegEx))
{
mod = buf[10];
if (IsLetter(mod))
{
return true;
}
std::cout << "Bad LSTN from '" << cs.GetCS() << "'. at " << ip << std::endl;
Dump("The requested module is not a letter:", buf, 11);
}
else
{
std::cout << "LSTN packet from " << ip << " rejected because '" << cs.GetCS() << "' didn't pass the regex!" << std::endl;
}
}
return false;
}
bool CProtocol::IsValidDisconnect(const uint8_t *buf, CCallsign &cs)
{
if (0 == memcmp(buf, "DISC", 4))
{
cs.CodeIn(buf + 4);
auto call = cs.GetCS();
if (std::regex_match(call, clientRegEx) || std::regex_match(call, peerRegEx) || std::regex_match(call, lstnRegEx))
{
return true;
}
}
return false;
}
bool CProtocol::IsValidKeepAlive(const uint8_t *buf, CCallsign &cs)
{
if ('P' == buf[0] && ('I' == buf[1] || 'O' == buf[1]) && 'N' == buf[2] && 'G' == buf[3])
{
cs.CodeIn(buf + 4);
auto call = cs.GetCS();
if (std::regex_match(call, clientRegEx) || std::regex_match(call, peerRegEx) || std::regex_match(call, lstnRegEx))
{
return true;
}
}
return false;
}
SPClient CProtocol::GetClient(const CIp &ip, const unsigned size, CPacket &packet, CCallsign &dst, CCallsign &src)
{
auto buf = packet.GetCData();
// could this be stream or packet data?
if (memcmp(buf, "M17", 3))
return nullptr;
// is there a client with this address?
auto client = g_Reflector.GetClients()->FindClient(ip);
g_Reflector.ReleaseClients();