-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstatus_udp.cc
More file actions
527 lines (444 loc) · 16.3 KB
/
status_udp.cc
File metadata and controls
527 lines (444 loc) · 16.3 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
// Trunk-Recorder Status Over UDP Plugin
// ********************************
// Requires trunk-recorder 5.0 or later.
// ********************************
#include <time.h>
#include <vector>
#include <iostream>
#include <cstdlib>
#include <string>
#include <cstring>
#include "../../trunk-recorder/source.h"
#include <json.hpp>
#include "../../trunk-recorder/plugin_manager/plugin_api.h"
#include <boost/archive/iterators/base64_from_binary.hpp>
#include <boost/archive/iterators/transform_width.hpp>
#include <boost/date_time/posix_time/posix_time.hpp> //time_formatters.hpp>
#include <boost/dll/alias.hpp> // for BOOST_DLL_ALIAS
#include <boost/property_tree/json_parser.hpp>
#include <boost/log/trivial.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/sinks/sync_frontend.hpp>
#include <boost/log/sinks/text_ostream_backend.hpp>
#include <boost/crc.hpp>
// UDP Socket Includes.
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h> // getaddrinfo, freeaddrinfo
#include <arpa/inet.h>
#include <unistd.h> // close
#include <errno.h>
#define INVALID_SOCKET -1
#define SOCKET_ERROR -1
typedef int SOCKET;
struct UdpTarget {
SOCKET sock;
sockaddr_storage addr;
socklen_t addrlen;
};
// Helper Consts
const int PLUGIN_SUCCESS = 0;
const int PLUGIN_FAILURE = 1;
using namespace std;
namespace logging = boost::log;
// Alais C++ types to Rust types.
using u8 = std::uint8_t;
using u16 = std::uint16_t;
using u32 = std::uint32_t;
enum Type : u8 {
Type_Invalid = 0,
Unit_On = 1,
Unit_Off = 2,
Unit_AckResp = 3,
Unit_Join = 4,
Unit_Data = 5,
Unit_AnsReq = 6,
Unit_Location = 7,
Unit_PTTP = 8, // Push to Talk Pressed
};
static_assert(std::is_same_v<std::underlying_type_t<Type>, u8>, "Type must be u8");
// Decalared Before Defined.
bool alias_eq(const char* a, const char* b);
// Packets
#pragma pack(push, 1)
struct Packet {
// Header: 4 Bytes (32 bits - 4 Bytes)
char hdr[2] = {'M', 'C'}; // Prefix: 'M','C'
Type typ = Type::Type_Invalid; // Type: 1 byte
u8 len = 8; // Size: Whole packet size, including Header, System, Radio, Payload. Size = Len * 4;
// System: 4 Bytes (32 bits - 4 Bytes)
u32 p25Id = 0; // [31:20] = SystemID (12b), [19:0] = WACN (20b)
// Radio: 20 Bytes (160 bits - 20 Bytes)
u16 nac = 0; // NAC
u16 tgId = 0; // Talk Group ID
u32 radioId = 0; // Radio's Src ID
char alias[12] = {0}; // Radio's Alias
// Payload: 4 Bytes (32 Bits - 4 Bytes)
u32 ts = 0; // Time Stamp (UNIX Epoch Seconds)
bool operator==(const Packet& o) const {
return
typ == o.typ &&
len == o.len &&
p25Id == o.p25Id &&
nac == o.nac &&
tgId == o.tgId &&
radioId == o.radioId &&
alias_eq(alias, o.alias) &&
ts == o.ts;
}
};
#pragma pack(pop)
static_assert(sizeof(Packet) == 32, "Packet must be 32 bytes");
static_assert(alignof(Packet) == 1, "Packet must be packed");
// Packet Helpers
inline constexpr u16 p25_system_id(u32 p) {
return static_cast<u16>(p >> 20);
}
inline constexpr u32 p25_wacn(u32 p) {
return p & 0xFFFFFu;
}
inline constexpr u16 p25_nac (u32 p) {
return p & 0x0FFFu;
}
inline constexpr std::size_t payload_bytes(const Packet& p) {
return static_cast<std::size_t>(p.len) * 4;
}
inline constexpr bool valid_hdr(const Packet& p) {
return p.hdr[0] == 'M' && p.hdr[1] == 'C';
}
constexpr u32 make_p25id(u16 sysId, u32 wacn) {
return (u32(sysId & 0x0FFF) << 20) | (wacn & 0xFFFFF);
}
inline void stringToChar12(const std::string& input, char out[12]) {
// Since strncpy already null-pads when the source is shorter
std::strncpy(out, input.c_str(), 11);
// Ensure the last element is always null
out[11] = '\0';
}
bool alias_eq(const char* a, const char* b) {
return std::strcmp(a, b) == 0;
}
class Status_Udp : public Plugin_Api
{
// Trunk-Recorder
Config *tr_config;
std::vector<Source *> tr_sources;
std::vector<System *> tr_systems;
// Plugin Settings
std::string log_prefix = "\t[Status UDP]\t";
std::string udp_dest;
bool unit_enabled = true;
// Plugin Socket
UdpTarget udp_socket = UdpTarget {};
// Make sure we don't send the same packet muliple times.
Packet last_packet = Packet{};
public:
Status_Udp(){};
// ********************************
// trunk-recorder messages
// ********************************
// call_start()
// Send information about a new call or the unit initiating it.
// TRUNK-RECORDER PLUGIN API: Called when a call starts
int call_start(Call *call) override
{
if (unit_enabled)
{
System* sys = call->get_system();
boost::property_tree::ptree stat_node = call->get_stats();
u32 source_id = stat_node.get<u32>("srcId");
Packet pkt{};
pkt.typ = Type::Unit_PTTP;
pkt.p25Id = make_p25id(sys->get_sys_site_id(), sys->get_wacn());
pkt.nac = p25_nac(sys->get_nac());
pkt.tgId = call->get_talkgroup();
pkt.radioId = source_id;
stringToChar12(sys->find_unit_tag(source_id), pkt.alias);
pkt.ts = time(NULL);
return send_packet(pkt);
};
return PLUGIN_SUCCESS;
}
// unit_registration()
// Unit registration on a system (on)
// TRUNK-RECORDER PLUGIN API: Called each REGISTRATION message
int unit_registration(System *sys, long source_id) override
{
if (unit_enabled)
{
Packet pkt{};
pkt.typ = Type::Unit_On;
pkt.p25Id = make_p25id(sys->get_sys_site_id(), sys->get_wacn());
pkt.nac = p25_nac(sys->get_nac());
pkt.radioId = source_id;
stringToChar12(sys->find_unit_tag(source_id), pkt.alias);
pkt.ts = time(NULL);
return send_packet(pkt);
}
return PLUGIN_SUCCESS;
}
// unit_deregistration()
// Unit de-registration on a system (off)
// TRUNK-RECORDER PLUGIN API: Called each DEREGISTRATION message
int unit_deregistration(System *sys, long source_id) override
{
if (unit_enabled)
{
Packet pkt{};
pkt.typ = Type::Unit_Off;
pkt.p25Id = make_p25id(sys->get_sys_site_id(), sys->get_wacn());
pkt.nac = p25_nac(sys->get_nac());
pkt.radioId = source_id;
stringToChar12(sys->find_unit_tag(source_id), pkt.alias);
pkt.ts = time(NULL);
return send_packet(pkt);
}
return PLUGIN_SUCCESS;
}
// unit_acknowledge_response()
// Unit acknowledge response (ackresp)
// TRUNK-RECORDER PLUGIN API: Called each ACKNOWLEDGE message
int unit_acknowledge_response(System *sys, long source_id) override
{
if (unit_enabled)
{
Packet pkt{};
pkt.typ = Type::Unit_AckResp;
pkt.p25Id = make_p25id(sys->get_sys_site_id(), sys->get_wacn());
pkt.nac = p25_nac(sys->get_nac());
pkt.radioId = source_id;
stringToChar12(sys->find_unit_tag(source_id), pkt.alias);
pkt.ts = time(NULL);
return send_packet(pkt);
}
return PLUGIN_SUCCESS;
}
// unit_group_affiliation()
// Unit talkgroup affiliation (join)
// TRUNK-RECORDER PLUGIN API: Called each AFFILIATION message
int unit_group_affiliation(System *sys, long source_id, long talkgroup_num) override
{
if (unit_enabled)
{
Packet pkt{};
pkt.typ = Type::Unit_Join;
pkt.p25Id = make_p25id(sys->get_sys_site_id(), sys->get_wacn());
pkt.nac = p25_nac(sys->get_nac());
pkt.tgId = talkgroup_num;
pkt.radioId = source_id;
stringToChar12(sys->find_unit_tag(source_id), pkt.alias);
pkt.ts = time(NULL);
return send_packet(pkt);
}
return PLUGIN_SUCCESS;
}
// unit_data_grant()
// Unit data grant (data)
// TRUNK-RECORDER PLUGIN API: Called each DATA_GRANT message
int unit_data_grant(System *sys, long source_id) override
{
if (unit_enabled)
{
Packet pkt{};
pkt.typ = Type::Unit_Data;
pkt.p25Id = make_p25id(sys->get_sys_site_id(), sys->get_wacn());
pkt.nac = p25_nac(sys->get_nac());
pkt.radioId = source_id;
stringToChar12(sys->find_unit_tag(source_id), pkt.alias);
pkt.ts = time(NULL);
return send_packet(pkt);
}
return PLUGIN_SUCCESS;
}
// unit_answer_request()
// TRUNK-RECORDER PLUGIN API: Called each UU_ANS_REQ message
int unit_answer_request(System *sys, long source_id, long talkgroup_num) override
{
if (unit_enabled)
{
Packet pkt{};
pkt.typ = Type::Unit_AnsReq;
pkt.p25Id = make_p25id(sys->get_sys_site_id(), sys->get_wacn());
pkt.nac = p25_nac(sys->get_nac());
pkt.tgId = talkgroup_num;
pkt.radioId = source_id;
stringToChar12(sys->find_unit_tag(source_id), pkt.alias);
pkt.ts = time(NULL);
return send_packet(pkt);
}
return PLUGIN_SUCCESS;
}
// unit_location()
// Unit location/roaming update (location)
// TRUNK-RECORDER PLUGIN API: Called each LOCATION message
int unit_location(System *sys, long source_id, long talkgroup_num) override
{
if (unit_enabled)
{
Packet pkt{};
pkt.typ = Type::Unit_Location;
pkt.p25Id = make_p25id(sys->get_sys_site_id(), sys->get_wacn());
pkt.nac = p25_nac(sys->get_nac());
pkt.tgId = talkgroup_num;
pkt.radioId = source_id;
stringToChar12(sys->find_unit_tag(source_id), pkt.alias);
pkt.ts = time(NULL);
return send_packet(pkt);
}
return PLUGIN_SUCCESS;
}
// ********************************
// trunk-recorder plugin API & startup
// ********************************
// parse_config()
// TRUNK-RECORDER PLUGIN API: Called before init(); parses the config information for this plugin.
int parse_config(json config_data) override
{
// Get values from this plugin's config.json section and load into class variables.
udp_dest = config_data.value("destination", "udp://127.0.0.1:7767");
// Print plugin startup info
BOOST_LOG_TRIVIAL(info) << log_prefix << "destination: " << udp_dest << endl;
return PLUGIN_SUCCESS;
}
// init()
// TRUNK-RECORDER PLUGIN API: Plugin initialization; called after parse_config().
int init(Config *config, std::vector<Source *> sources, std::vector<System *> systems) override
{
// Establish pointers to systems, sources, and configs if needed later.
tr_sources = sources;
tr_systems = systems;
tr_config = config;
return PLUGIN_SUCCESS;
}
// start()
// TRUNK-RECORDER PLUGIN API: Called after trunk-recorder finishes setup and the plugin is initialized
int start() override
{
// Start the UDP connection
open_udp_connection();
return PLUGIN_SUCCESS;
}
// stop()
int stop() override
{
// In the event that we choose to "connect" to a UDP socket,
// We would then remove that connection in the stop function.
return PLUGIN_SUCCESS;
}
// ********************************
// Service Functions
// ********************************
// send_packet()
// Send a UDP packet to the desingated host.
int send_packet(Packet packet)
{
if (udp_socket.sock == INVALID_SOCKET || udp_socket.addrlen == 0) {
BOOST_LOG_TRIVIAL(error) << log_prefix << "UDP socket not initialized";
return PLUGIN_FAILED;
}
// Don't send duplicate packets.
if (last_packet == packet) {
return PLUGIN_SUCCESS;
}
std::vector<u8> data;
data.resize(sizeof(Packet));
std::memcpy(data.data(), &packet, sizeof(packet));
ssize_t bytesSent = ::sendto(
udp_socket.sock,
data.data(),
data.size(),
0,
reinterpret_cast<const sockaddr*>(&udp_socket.addr),
udp_socket.addrlen
);
// Update the last packet, to this packet.
last_packet = packet;
if (bytesSent == -1) {
int err = errno;
BOOST_LOG_TRIVIAL(error) << log_prefix << "sendto failed (" << err << "): " << std::strerror(err);
return PLUGIN_FAILURE;
}
return PLUGIN_SUCCESS;
}
void open_udp_connection()
{
this->udp_socket = make_udp_target(udp_dest);
if (this->udp_socket.sock == INVALID_SOCKET) {
BOOST_LOG_TRIVIAL(error) << log_prefix << "Failed to open UDP target for " << udp_dest;
}
}
// Parse udp://host[:port], with default port 7727
bool parse_udp_uri(const std::string& uri, std::string& host, std::string& port) {
const std::string prefix = "udp://";
if (uri.rfind(prefix, 0) != 0) {
BOOST_LOG_TRIVIAL(error) << log_prefix << "Destination URI must start with udp://" << endl;
return false;
}
auto without_scheme = uri.substr(prefix.size());
auto colon = without_scheme.find_last_of(':');
if (colon == std::string::npos) {
host = without_scheme;
port = "7767"; // default
} else {
host = without_scheme.substr(0, colon);
port = without_scheme.substr(colon + 1);
if (port.empty()) port = "7767"; // handle udp://host:
}
BOOST_LOG_TRIVIAL(info) << log_prefix << "parse_udp_uri: host: '" << host << "' port: '" << port << "'" << endl;
return !host.empty();
}
UdpTarget make_udp_target(const std::string& uri) {
UdpTarget target{};
target.sock = INVALID_SOCKET;
std::string host, port;
if (!parse_udp_uri(uri, host, port)) {
BOOST_LOG_TRIVIAL(error) << "Invalid URI format";
return target;
}
if (host == "0.0.0.0" || host == "::") {
BOOST_LOG_TRIVIAL(error) << log_prefix << "Refusing to use unspecified address (" << host << ") as a destination";
return target;
}
addrinfo hints{};
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_DGRAM;
hints.ai_flags = AI_NUMERICSERV;
addrinfo* res = nullptr;
int rc = ::getaddrinfo(host.c_str(), port.c_str(), &hints, &res);
if (rc != 0) {
BOOST_LOG_TRIVIAL(error) << log_prefix << "getaddrinfo failed for " << host << ":" << port
<< " (" << gai_strerror(rc) << ")";
return target;
}
target.sock = ::socket(res->ai_family, res->ai_socktype, res->ai_protocol);
if (target.sock == INVALID_SOCKET) {
BOOST_LOG_TRIVIAL(error) << log_prefix << "socket() failed";
::freeaddrinfo(res);
return target;
}
std::memcpy(&target.addr, res->ai_addr, res->ai_addrlen);
target.addrlen = static_cast<socklen_t>(res->ai_addrlen);
// Optional: enable broadcast if you're targeting a broadcast address
if (res->ai_family == AF_INET) {
auto sin = reinterpret_cast<const sockaddr_in*>(res->ai_addr);
if (sin->sin_addr.s_addr == INADDR_BROADCAST) {
int yes = 1;
::setsockopt(target.sock, SOL_SOCKET, SO_BROADCAST, &yes, sizeof(yes));
}
}
::freeaddrinfo(res);
return target;
}
// ********************************
// Create the plugin
// ********************************
// Factory method
static boost::shared_ptr<Status_Udp> create()
{
return boost::shared_ptr<Status_Udp>(new Status_Udp());
}
};
BOOST_DLL_ALIAS(
Status_Udp::create, // <-- this function is exported with...
create_plugin // <-- ... this alias name
)