-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathEvents.c
More file actions
1350 lines (997 loc) · 37.4 KB
/
Copy pathEvents.c
File metadata and controls
1350 lines (997 loc) · 37.4 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
/*
Copyright 2001-2022 John Wiseman G8BPQ
This file is part of LinBPQ/BPQ32.
LinBPQ/BPQ32 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.
LinBPQ/BPQ32 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 LinBPQ/BPQ32. If not, see http://www.gnu.org/licenses
*/
#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
#define _CRT_SECURE_NO_DEPRECATE
#include "compatbits.h"
#include <string.h>
#include "asmstrucs.h"
#include "tncinfo.h"
#include "cheaders.h"
#include "kiss.h"
VOID __cdecl Debugprintf(const char * format, ...);
#ifndef WIN32
#define APIENTRY
#define DllExport
#define VOID void
#else
#include <windows.h>
#endif
extern BOOL EventsEnabled;
void MQTTReportSession(char * Msg);
extern int MQTT;
extern time_t TimeLoaded;
uint16_t UDPSeq = 1;
int linkSeq = 1;
int cctSeq = 1;
extern SOCKET NodeAPISocket;
extern SOCKADDR_IN UDPreportdest;
extern char Modenames[19][10];
extern char NODECALLLOPPED[10];
extern char MYALIASLOPPED[10];
extern char LOC[7];
extern char VersionString[50];
extern double LatFromLOC;
extern double LonFromLOC;
extern int NUMBEROFNODES, MAXDESTS, L4CONNECTSOUT, L4CONNECTSIN, L4FRAMESTX, L4FRAMESRX, L4FRAMESRETRIED, OLDFRAMES;
extern int L2CONNECTSOUT, L2CONNECTSIN;
void hookL2SessionClosed(struct _LINKTABLE * LINK, char * Reason, char * Direction);
int ConvFromAX25(unsigned char * incall, unsigned char * outcall);
int COUNT_AT_L2(struct _LINKTABLE * LINK);
int CountFramesQueuedOnSession(TRANSPORTENTRY * Session);
int decodeNETROMUIMsg(unsigned char * Msg, int iLen, char * Buffer, int BufferLen);
int decodeNETROMIFrame(unsigned char * Msg, int iLen, char * Buffer, int BufferLen);
int decodeINP3RIF(unsigned char * Msg, int iLen, char * Buffer, int BufferLen);
int decodeRecordRoute(L3MESSAGE * L3, int iLen, char * Buffer, int BufferLen);
char * byte_base64_encode(char *str, int len);
// Runs use specified routine on certain event
#ifndef WIN32
void RunEventProgram(char * Program, char * Param)
{
char * arg_list[] = {Program, NULL, NULL};
pid_t child_pid;
if (EventsEnabled == 0)
return;
signal(SIGCHLD, SIG_IGN); // Silently (and portably) reap children.
if (Param && Param[0])
arg_list[1] = Param;
// Fork and Exec Specified program
// Duplicate this process.
child_pid = fork ();
if (child_pid == -1)
{
printf ("Event fork() Failed\n");
return;
}
if (child_pid == 0)
{
execvp (arg_list[0], arg_list);
// The execvp function returns only if an error occurs.
printf ("Failed to run %s\n", arg_list[0]);
exit(0); // Kill the new process
}
#else
DllExport void APIENTRY RunEventProgram(char * Program, char * Param)
{
int n = 0;
char cmdLine[256];
STARTUPINFO SInfo; // pointer to STARTUPINFO
PROCESS_INFORMATION PInfo; // pointer to PROCESS_INFORMATION
if (EventsEnabled == 0)
return;
SInfo.cb=sizeof(SInfo);
SInfo.lpReserved=NULL;
SInfo.lpDesktop=NULL;
SInfo.lpTitle=NULL;
SInfo.dwFlags=0;
SInfo.cbReserved2=0;
SInfo.lpReserved2=NULL;
sprintf(cmdLine, "%s %s", Program, Param);
if (!CreateProcess(NULL, cmdLine, NULL, NULL, FALSE,0 ,NULL ,NULL, &SInfo, &PInfo))
Debugprintf("Failed to Start %s Error %d ", Program, GetLastError());
#endif
return;
}
void hookL2SessionAccepted(int Port, char * remotecall, char * ourcall, struct _LINKTABLE * LINK)
{
// Incoming SABM accepted
char UDPMsg[1024];
int udplen;
L2CONNECTSIN++;
LINK->apiSeq = linkSeq++;
LINK->lastStatusSentTime = LINK->ConnectTime = NOW;
LINK->bytesTXed = LINK->bytesRXed = LINK->framesResent = LINK->framesRXed = LINK->framesTXed = 0;
LINK->LastStatusbytesTXed = LINK->LastStatusbytesRXed = 0;
strcpy(LINK->callingCall, remotecall);
strcpy(LINK->receivingCall, ourcall);
strcpy(LINK->Direction, "In");
if (NodeAPISocket)
{
LINK->lastStatusSentTime = NOW;
udplen = sprintf(UDPMsg, "{\"@type\":\"LinkUpEvent\", \"node\": \"%s\", \"id\": %d, \"direction\": \"incoming\", \"port\": \"%d\", \"remote\": \"%s\", \"local\": \"%s\", \"isRF\": %s}",
NODECALLLOPPED, LINK->apiSeq, LINK->LINKPORT->PORTNUMBER, LINK->callingCall, LINK->receivingCall, (LINK->LINKPORT->isRF)?"true":"false");
// Debugprintf(UDPMsg);
sendto(NodeAPISocket, UDPMsg, udplen, 0, (struct sockaddr *)&UDPreportdest, sizeof(UDPreportdest));
}
}
void hookL2SessionDeleted(struct _LINKTABLE * LINK)
{
// calculate session time and av bytes/min in and out
if (LINK->ConnectTime)
{
if (LINK->bytesTXed == 0 && LINK->bytesRXed == 0)
{
// assume failed connect and ignore for now - maybe log later
}
else
{
char Msg[256];
char timestamp[64];
time_t sessionTime = NOW - LINK->ConnectTime;
double avBytesSent = LINK->bytesTXed / (sessionTime / 60.0);
double avBytesRXed = LINK->bytesRXed / (sessionTime / 60.0);
struct tm * TM = localtime(&NOW);
sprintf(timestamp, "%02d:%02d:%02d", TM->tm_hour, TM->tm_min, TM->tm_sec);
if (sessionTime == 0)
sessionTime = 1; // Or will get divide by zero error
Debugprintf("KISS Session Stats Port %d %s %s %d secs Bytes Sent %d BPM %4.2f Bytes Received %d %4.2f BPM ",
LINK->LINKPORT->PORTNUMBER, LINK->callingCall, LINK->receivingCall, sessionTime, LINK->bytesTXed, avBytesSent, LINK->bytesRXed, avBytesRXed, timestamp);
sprintf(Msg, "{\"mode\": \"%s\", \"direction\": \"%s\", \"port\": %d, \"callfrom\": \"%s\", \"callto\": \"%s\", \"time\": %d, \"bytesSent\": %d,"
"\"BPMSent\": %4.2f, \"BytesReceived\": %d, \"BPMReceived\": %4.2f, \"timestamp\": \"%s\"}",
"KISS", LINK->Direction, LINK->LINKPORT->PORTNUMBER, LINK->callingCall, LINK->receivingCall, sessionTime,
LINK->bytesTXed, avBytesSent, LINK->bytesRXed, avBytesRXed, timestamp);
if (MQTT)
MQTTReportSession(Msg);
LINK->ConnectTime = 0;
}
if (LINK->Sent && LINK->Received && (LINK->SentAfterCompression || LINK->ReceivedAfterExpansion))
Debugprintf("L2 Compression Stats %s %s TX %d %d %d%% RX %d %d %d%%", LINK->callingCall, LINK->receivingCall,
LINK->Sent, LINK->SentAfterCompression, ((LINK->Sent - LINK->SentAfterCompression) * 100) / LINK->Sent,
LINK->Received, LINK->ReceivedAfterExpansion, ((LINK->ReceivedAfterExpansion - LINK->Received) * 100) / LINK->Received);
}
}
void hookL2SessionAttempt(int Port, char * ourcall, char * remotecall, struct _LINKTABLE * LINK)
{
LINK->lastStatusSentTime = LINK->ConnectTime = NOW;
LINK->bytesTXed = LINK->bytesRXed = LINK->framesResent = LINK->framesRXed = LINK->framesTXed = 0;
LINK->LastStatusbytesTXed = LINK->LastStatusbytesRXed = 0;
strcpy(LINK->callingCall, ourcall);
strcpy(LINK->receivingCall, remotecall);
strcpy(LINK->Direction, "Out");
}
void hookL2SessionConnected(struct _LINKTABLE * LINK)
{
// UA received in reponse to SABM
char UDPMsg[1024];
int udplen;
L2CONNECTSOUT++;
LINK->apiSeq = linkSeq++;
if (NodeAPISocket)
{
LINK->lastStatusSentTime = NOW;
udplen = sprintf(UDPMsg, "{\"@type\":\"LinkUpEvent\", \"node\": \"%s\", \"id\": %d, \"direction\": \"outgoing\", \"port\": \"%d\", \"remote\": \"%s\", \"local\": \"%s\", \"isRF\": %s}",
NODECALLLOPPED, LINK->apiSeq, LINK->LINKPORT->PORTNUMBER, LINK->callingCall, LINK->receivingCall, (LINK->LINKPORT->isRF)?"true":"false");
// Debugprintf(UDPMsg);
sendto(NodeAPISocket, UDPMsg, udplen, 0, (struct sockaddr *)&UDPreportdest, sizeof(UDPreportdest));
}
}
void hookL2SessionClosed(struct _LINKTABLE * LINK, char * Reason, char * Direction)
{
// Link closed. Could be normal, ie disc send/received or restried out etc
char UDPMsg[1024];
int udplen;
if (NodeAPISocket)
{
if (LINK->receivingCall[0] == 0 || LINK->callingCall[0] == 0)
return;
if (strcmp(Direction, "Out") == 0)
udplen = sprintf(UDPMsg, "{\"@type\":\"LinkDownEvent\", \"node\": \"%s\", \"id\": %d, \"direction\": \"outgoing\", \"port\": \"%d\", \"remote\": \"%s\", \"local\": \"%s\","
"\"bytesSent\": %d, \"bytesRcvd\": %d, \"frmsSent\": %d, \"frmsRcvd\": %d, \"frmsQueued\": %d, \"frmsResent\": %d, \"reason\": \"%s\","
" \"time\": %d, \"upForSecs\": %d, \"frmsQdPeak\": %d, \"isRF\": %s}",
NODECALLLOPPED, LINK->apiSeq, LINK->LINKPORT->PORTNUMBER, LINK->receivingCall, LINK->callingCall,
LINK->bytesTXed , LINK->bytesRXed, LINK->framesTXed, LINK->framesRXed, COUNT_AT_L2(LINK), LINK->framesResent, Reason,
(int)NOW, (int)NOW - LINK->ConnectTime, LINK->maxQueued, (LINK->LINKPORT->isRF)?"true":"false");
else
udplen = sprintf(UDPMsg, "{\"@type\":\"LinkDownEvent\", \"node\": \"%s\", \"id\": %d, \"direction\": \"incoming\", \"port\": \"%d\", \"remote\": \"%s\", \"local\": \"%s\","
"\"bytesSent\": %d, \"bytesRcvd\": %d, \"frmsSent\": %d, \"frmsRcvd\": %d, \"frmsQueued\": %d, \"frmsResent\": %d, \"reason\": \"%s\","
" \"time\": %d, \"upForSecs\": %d, \"frmsQdPeak\": %d, \"isRF\": %s}",
NODECALLLOPPED, LINK->apiSeq, LINK->LINKPORT->PORTNUMBER, LINK->callingCall, LINK->receivingCall,
LINK->bytesTXed , LINK->bytesRXed, LINK->framesTXed, LINK->framesRXed, COUNT_AT_L2(LINK), LINK->framesResent, Reason,
(int)NOW, (int)NOW - LINK->ConnectTime, LINK->maxQueued, (LINK->LINKPORT->isRF)?"true":"false");
// Debugprintf(UDPMsg);
sendto(NodeAPISocket, UDPMsg, udplen, 0, (struct sockaddr *)&UDPreportdest, sizeof(UDPreportdest));
}
}
void hookL2SessionStatus(struct _LINKTABLE * LINK)
{
// Send at regular intervals on open links
char UDPMsg[1024];
int udplen;
int bpsTx, bpsRx, interval;
if (NodeAPISocket)
{
interval = NOW - (int)LINK->lastStatusSentTime;
bpsTx = (LINK->bytesTXed - LINK->LastStatusbytesTXed) / interval;
bpsRx = (LINK->bytesRXed - LINK->LastStatusbytesRXed) / interval;
LINK->lastStatusSentTime = NOW;
LINK->LastStatusbytesTXed = LINK->bytesTXed;
LINK->LastStatusbytesRXed = LINK->bytesRXed;
if (strcmp(LINK->Direction, "Out") == 0)
udplen = sprintf(UDPMsg, "{\"@type\":\"LinkStatus\", \"node\": \"%s\", \"id\": %d, \"direction\": \"outgoing\", \"port\": \"%d\", \"remote\": \"%s\", \"local\": \"%s\","
"\"bytesSent\": %d, \"bytesRcvd\": %d, \"frmsSent\": %d, \"frmsRcvd\": %d, \"frmsQueued\": %d, \"frmsResent\": %d,"
"\"upForSecs\": %d, \"frmsQdPeak\": %d, \"bpsTxMean\": %d, \"bpsRxMean\": %d, \"frmQMax\": %d, \"l2rttMs\": %d, \"isRF\": %s}",
NODECALLLOPPED, LINK->apiSeq, LINK->LINKPORT->PORTNUMBER, LINK->receivingCall, LINK->callingCall,
LINK->bytesTXed, LINK->bytesRXed, LINK->framesTXed, LINK->framesRXed, 0, LINK->framesResent,
(int)NOW - LINK->ConnectTime, LINK->maxQueued, bpsTx, bpsRx, LINK->intervalMaxQueued, LINK->RTT, (LINK->LINKPORT->isRF)?"true":"false");
else
udplen = sprintf(UDPMsg, "{\"@type\":\"LinkStatus\", \"node\": \"%s\", \"id\": %d, \"direction\": \"incoming\", \"port\": \"%d\", \"remote\": \"%s\", \"local\": \"%s\","
"\"bytesSent\": %d, \"bytesRcvd\": %d, \"frmsSent\": %d, \"frmsRcvd\": %d, \"frmsQueued\": %d, \"frmsResent\": %d,"
"\"upForSecs\": %d, \"frmsQdPeak\": %d, \"bpsTxMean\": %d, \"bpsRxMean\": %d, \"frmQMax\": %d, \"l2rttMs\": %d, \"isRF\": %s}",
NODECALLLOPPED, LINK->apiSeq, LINK->LINKPORT->PORTNUMBER, LINK->callingCall, LINK->receivingCall,
LINK->bytesTXed, LINK->bytesRXed, LINK->framesTXed, LINK->framesRXed, 0, LINK->framesResent,
(int)NOW - LINK->ConnectTime, LINK->maxQueued, bpsTx, bpsRx, LINK->intervalMaxQueued, LINK->RTT, (LINK->LINKPORT->isRF)?"true":"false");
LINK->intervalMaxQueued = 0;
// Debugprintf(UDPMsg);
sendto(NodeAPISocket, UDPMsg, udplen, 0, (struct sockaddr *)&UDPreportdest, sizeof(UDPreportdest));
}
}
void hookL4SessionAttempt(struct STREAMINFO * STREAM, char * remotecall, char * ourcall)
{
// Outgoing Connect
STREAM->ConnectTime = NOW;
STREAM->bytesTXed = STREAM->bytesRXed = 0;
strcpy(STREAM->callingCall, ourcall);
strcpy(STREAM->receivingCall, remotecall);
strcpy(STREAM->Direction, "Out");
}
void hookL4SessionAccepted(struct STREAMINFO * STREAM, char * remotecall, char * ourcall)
{
// Incoming Connect
STREAM->ConnectTime = NOW;
STREAM->bytesTXed = STREAM->bytesRXed = 0;
strcpy(STREAM->callingCall, remotecall);
strcpy(STREAM->receivingCall, ourcall);
strcpy(STREAM->Direction, "In");
}
/*
{
"eventSource": "circuit",
"time": "2025-10-08T14:05:54+00:00",
"id": 23,
"direction": "incoming",
"port": "0",
"remote": "G8PZT@G8PZT:15aa",
"local": "GE8PZT:0017",
"event": "disconnect",
"@type": "event"
}
*/
void hookL4SessionDeleted(struct TNCINFO * TNC, struct STREAMINFO * STREAM)
{
char Msg[256];
char timestamp[16];
if (STREAM->ConnectTime)
{
time_t sessionTime = NOW - STREAM->ConnectTime;
double avBytesRXed = STREAM->bytesRXed / (sessionTime / 60.0);
double avBytesSent = STREAM->bytesTXed / (sessionTime / 60.0);
struct tm * TM = localtime(&NOW);
sprintf(timestamp, "%02d:%02d:%02d", TM->tm_hour, TM->tm_min, TM->tm_sec);
if (sessionTime == 0)
sessionTime = 1; // Or will get divide by zero error
sprintf(Msg, "{\"mode\": \"%s\", \"direction\": \"%s\", \"port\": %d, \"callfrom\": \"%s\", \"callto\": \"%s\", \"time\": %d, \"bytesSent\": %d,"
"\"BPMSent\": %4.2f, \"BytesReceived\": %d, \"BPMReceived\": %4.2f, \"timestamp\": \"%s\"}",
Modenames[TNC->Hardware - 1], STREAM->Direction, TNC->Port, STREAM->callingCall, STREAM->receivingCall, sessionTime,
STREAM->bytesTXed, avBytesSent, STREAM->bytesRXed, avBytesRXed, timestamp);
if (MQTT)
MQTTReportSession(Msg);
STREAM->ConnectTime = 0;
}
}
void hookNodeStarted()
{
char UDPMsg[1024];
int udplen;
#ifdef LINBPQ
char Software[80] = "LinBPQ";
if (sizeof(void *) == 4)
strcat(Software, "(32 bit)");
#else
char Software[80] = "BPQ32";
#endif
if (NodeAPISocket)
{
int ret;
udplen = sprintf(UDPMsg, "{\"@type\": \"NodeUpEvent\", \"nodeCall\": \"%s\", \"nodeAlias\": \"%s\", \"locator\": \"%s\","
"\"latitude\": %f, \"longitude\": %f, \"software\": \"%s\", \"version\": \"%s\"}",
NODECALLLOPPED, MYALIASLOPPED, LOC, LatFromLOC, LonFromLOC, Software, VersionString);
ret = sendto(NodeAPISocket, UDPMsg, udplen, 0, (struct sockaddr *)&UDPreportdest, sizeof(UDPreportdest));
if (ret != udplen)
Debugprintf("%d %d %s", ret, WSAGetLastError(), UDPMsg);
}
}
void hookNodeClosing(char * Reason)
{
char UDPMsg[1024];
int udplen;
if (NodeAPISocket)
{
udplen = sprintf(UDPMsg, "{\"@type\": \"NodeDownEvent\", \"nodeCall\": \"%s\", \"nodeAlias\": \"%s\", \"reason\": \"%s\", \"uptimeSecs\": %d,"
"\"linksIn\": %d, \"linksOut\": %d, \"cctsIn\": %d, \"cctsOut\": %d, \"l3Relayed\": %d}",
NODECALLLOPPED, MYALIASLOPPED, Reason, NOW - TimeLoaded, L2CONNECTSIN, L2CONNECTSOUT, L4CONNECTSIN, L4CONNECTSOUT, L3FRAMES);
// Debugprintf(UDPMsg);
sendto(NodeAPISocket, UDPMsg, udplen, 0, (struct sockaddr *)&UDPreportdest, sizeof(UDPreportdest));
}
}
void hookNodeRunning()
{
char UDPMsg[1024];
int udplen;
#ifdef LINBPQ
char Software[80] = "LinBPQ";
if (sizeof(void *) == 4)
strcat(Software, "(32 bit)");
#else
char Software[80] = "BPQ32";
#endif
if (NodeAPISocket)
{
udplen = sprintf(UDPMsg, "{\"@type\": \"NodeStatus\", \"nodeCall\": \"%s\", \"nodeAlias\": \"%s\", \"locator\": \"%s\","
"\"latitude\": %f, \"longitude\": %f, \"software\": \"%s\", \"version\": \"%s\", \"uptimeSecs\": %d,"
"\"linksIn\": %d, \"linksOut\": %d, \"cctsIn\": %d, \"cctsOut\": %d, \"l3Relayed\": %d}",
NODECALLLOPPED, MYALIASLOPPED, LOC, LatFromLOC, LonFromLOC, Software, VersionString, NOW - TimeLoaded,
L2CONNECTSIN, L2CONNECTSOUT, L4CONNECTSIN, L4CONNECTSOUT, L3FRAMES);
// Debugprintf(UDPMsg);
sendto(NodeAPISocket, UDPMsg, udplen, 0, (struct sockaddr *)&UDPreportdest, sizeof(UDPreportdest));
}
}
void IncomingL4ConnectionEvent(TRANSPORTENTRY * L4)
{
char UDPMsg[1024];
int udplen;
char remotecall[64];
char ourcall[64];
char circuitinfo[32];
int Service = L4->Service;
// CACK sent to CREQ
L4->apiSeq = cctSeq++;
strcpy(L4->Direction, "incoming");
if (NodeAPISocket)
{
L4->lastStatusSentTime = NOW;
remotecall[ConvFromAX25(L4->L4TARGET.DEST->DEST_CALL, remotecall)] = 0;
// remotecall[ConvFromAX25(L4->L4USER, remotecall)] = 0;
ourcall[ConvFromAX25(L4->L4MYCALL, ourcall)] = 0;
sprintf(circuitinfo, ":%02x%02x", L4->FARINDEX, L4->FARID);
strcat(remotecall, circuitinfo);
sprintf(circuitinfo, ":%02x%02x", L4->CIRCUITINDEX, L4->CIRCUITID);
strcat(ourcall, circuitinfo);
if (Service == -1)
udplen = sprintf(UDPMsg, "{\"@type\": \"CircuitUpEvent\", \"node\": \"%s\", \"id\": %d, \"direction\": \"incoming\", "
"\"remote\": \"%s\", \"local\": \"%s\"}",
NODECALLLOPPED, L4->apiSeq, remotecall, ourcall);
else
udplen = sprintf(UDPMsg, "{\"@type\": \"CircuitUpEvent\", \"node\": \"%s\", \"id\": %d, \"direction\": \"incoming\", "
"\"service\": %d, \"remote\": \"%s\", \"local\": \"%s\"}",
NODECALLLOPPED, L4->apiSeq, Service, remotecall, ourcall);
// Debugprintf(UDPMsg);
sendto(NodeAPISocket, UDPMsg, udplen, 0, (struct sockaddr *)&UDPreportdest, sizeof(UDPreportdest));
}
}
void OutgoingL4ConnectionEvent(TRANSPORTENTRY * L4)
{
char UDPMsg[1024];
int udplen;
char remotecall[64];
char ourcall[64];
char circuitinfo[32];
int Service = L4->Service;
// CACK received
strcpy(L4->Direction, "outgoing");
L4->apiSeq = cctSeq++;
if (NodeAPISocket)
{
L4->lastStatusSentTime = NOW;
remotecall[ConvFromAX25(L4->L4TARGET.DEST->DEST_CALL, remotecall)] = 0;
// remotecall[ConvFromAX25(L4->L4USER, remotecall)] = 0;
ourcall[ConvFromAX25(L4->L4MYCALL, ourcall)] = 0;
sprintf(circuitinfo, ":%02x%02x", L4->FARID, L4->FARINDEX);
strcat(remotecall, circuitinfo);
sprintf(circuitinfo, ":%02x%02x", L4->CIRCUITID, L4->CIRCUITINDEX);
strcat(ourcall, circuitinfo);
if (Service == -1)
udplen = sprintf(UDPMsg, "{\"@type\": \"CircuitUpEvent\", \"node\": \"%s\", \"id\": %d, \"direction\": \"outgoing\", "
"\"remote\": \"%s\", \"local\": \"%s\"}",
NODECALLLOPPED, L4->apiSeq, remotecall, ourcall);
else
udplen = sprintf(UDPMsg, "{\"@type\": \"CircuitUpEvent\", \"node\": \"%s\", \"id\": %d, \"direction\": \"outgoing\", "
"\"service\": %d, \"remote\": \"%s\", \"local\": \"%s\"}",
NODECALLLOPPED, L4->apiSeq, Service, remotecall, ourcall);
// Debugprintf(UDPMsg);
sendto(NodeAPISocket, UDPMsg, udplen, 0, (struct sockaddr *)&UDPreportdest, sizeof(UDPreportdest));
}
}
/*
{
"@type": "CircuitUpEvent",
"node": "G8PZT"
"id": 1,
"direction": "incoming",
"service": 0,
"remote": "G8PZT@G8PZT:14c0",
"local": "G8PZT-4:0001"
}
"segsSent": 5,
"segsRcvd": 27,
"segsResent": 0,
"segsQueued": 0,
"reason": "rcvd DREQ"
*/
void L4DisconnectEvent(TRANSPORTENTRY * L4, char * Direction, char * Reason)
{
char UDPMsg[1024];
int udplen;
char remotecall[64];
char ourcall[64];
char circuitinfo[32];
int Count;
// CACK received
if (NodeAPISocket)
{
remotecall[ConvFromAX25(L4->L4TARGET.DEST->DEST_CALL, remotecall)] = 0;
// remotecall[ConvFromAX25(L4->L4USER, remotecall)] = 0;
ourcall[ConvFromAX25(L4->L4MYCALL, ourcall)] = 0;
sprintf(circuitinfo, ":%02x%02x", L4->FARINDEX, L4->FARID);
strcat(remotecall, circuitinfo);
sprintf(circuitinfo, ":%02x%02x", L4->CIRCUITINDEX, L4->CIRCUITID);
strcat(ourcall, circuitinfo);
if (L4->L4CROSSLINK) // CONNECTED?
Count = CountFramesQueuedOnSession(L4->L4CROSSLINK);
else
Count = CountFramesQueuedOnSession(L4);
udplen = sprintf(UDPMsg, "{\"@type\": \"CircuitDownEvent\", \"node\": \"%s\", \"id\": %d, \"direction\": \"%s\","
"\"service\": %d, \"remote\": \"%s\", \"local\": \"%s\", \"segsSent\": %d, \"segsRcvd\": %d, \"segsResent\": %d, \"segsQueued\": %d, \"reason\": \"%s\"}",
NODECALLLOPPED, L4->apiSeq, Direction, 0, remotecall, ourcall,L4->segsSent, L4->segsRcvd, L4->segsResent, Count, Reason);
// Debugprintf(UDPMsg);
sendto(NodeAPISocket, UDPMsg, udplen, 0, (struct sockaddr *)&UDPreportdest, sizeof(UDPreportdest));
}
}
void L4StatusSeport(TRANSPORTENTRY * L4)
{
char UDPMsg[1024];
int udplen;
char remotecall[64];
char ourcall[64];
char nodecall[16];
char circuitinfo[32];
int Count;
int Service = L4->Service;
// Regular Status reports
if (NodeAPISocket)
{
L4->lastStatusSentTime = NOW;
nodecall[ConvFromAX25(L4->L4TARGET.DEST->DEST_CALL, nodecall)] = 0;
remotecall[ConvFromAX25(L4->L4USER, remotecall)] = 0;
ourcall[ConvFromAX25(L4->L4MYCALL, ourcall)] = 0;
sprintf(circuitinfo, ":%02x%02x", L4->FARINDEX, L4->FARID);
strcat(remotecall, circuitinfo);
sprintf(circuitinfo, ":%02x%02x", L4->CIRCUITINDEX, L4->CIRCUITID);
strcat(ourcall, circuitinfo);
if (L4->L4CROSSLINK) // CONNECTED?
Count = CountFramesQueuedOnSession(L4->L4CROSSLINK);
else
Count = CountFramesQueuedOnSession(L4);
if (Service == -1)
udplen = sprintf(UDPMsg, "{\"@type\": \"CircuitStatus\", \"node\": \"%s\", \"id\": %d, \"direction\": \"%s\","
"\"upForSecs\": %d,\"remote\": \"%s\", \"local\": \"%s\", \"segsSent\": %d, \"segsRcvd\": %d, \"segsResent\": %d, \"segsQueued\": %d}",
NODECALLLOPPED, L4->apiSeq, L4->Direction, NOW - L4->ConnectTime, remotecall, ourcall,L4->segsSent, L4->segsRcvd, L4->segsResent, Count);
else
udplen = sprintf(UDPMsg, "{\"@type\": \"CircuitStatus\", \"node\": \"%s\", \"id\": %d, \"direction\": \"%s\","
"\"upForSecs\": %d, \"service\": %d, \"remote\": \"%s\", \"local\": \"%s\", \"segsSent\": %d, \"segsRcvd\": %d, \"segsResent\": %d, \"segsQueued\": %d}",
NODECALLLOPPED, L4->apiSeq, L4->Direction, NOW - L4->ConnectTime, Service, remotecall, ourcall,L4->segsSent, L4->segsRcvd, L4->segsResent, Count);
// Debugprintf(UDPMsg);
sendto(NodeAPISocket, UDPMsg, udplen, 0, (struct sockaddr *)&UDPreportdest, sizeof(UDPreportdest));
}
}
// L2/3/4 Tracing
#define PFBIT 0x10 // POLL/FINAL BIT IN CONTROL BYTE
#define NETROM_PID 0xCF
#define IP_PID 0xCC
#define ARP_PID 0xCD
char * PIDtoText(int PID)
{
switch (PID)
{
case 240:
return "DATA";
case NETROM_PID:
return "NET/ROM";
case IP_PID:
return "IP";
case ARP_PID:
return "ARP";
}
return "?";
}
void dumpDuffPacket(char * call, char * Buffer, int Len)
{
// log to syslog in base64
char * Base64;
Base64 = byte_base64_encode(Buffer, Len);
Debugprintf("Trace Error %s %s", call, Base64);
free(Base64);
}
int checkCall(char * call, unsigned char * Buffer, int Len)
{
char c;
int i;
// Validate source and dest calls - if duff, dump packet
for (i = 0; i < strlen(call); i++)
{
c = call[i];
if (isalnum(c) || c == '-' || c == '@')
continue;
dumpDuffPacket(call, Buffer, Len);
return 0;
}
return 1;
}
void APIL2Trace(struct _MESSAGE * Message, char * Dirn)
{
char UDPMsg[2048];
int udplen;
char srcecall[64];
char destcall[16];
char CR[3] = "";
char PF[2] = "";
int iLen = 0;
int CTL = Message->CTL;
char Type[16] = "Unknown";
int UIFlag = 0;
int IFlag = 0;
int UFlag = 0;
int NS;
int NR;
struct PORTCONTROL * PORT = GetPortTableEntryFromPortNum(Message->PORT);
#ifndef WIN32
struct timespec ts;
#else
FILETIME SystemTimeAsFileTime;
ULARGE_INTEGER FT64;
#endif
double NowMs;
if (PORT == 0)
return;
if ((Message->ORIGIN[6] & 1) == 0) // Digis
return;
destcall[ConvFromAX25(Message->DEST, destcall)] = 0;
srcecall[ConvFromAX25(Message->ORIGIN, srcecall)] = 0;
// Validate source and dest calls - if duff, dump packet
if (!checkCall(destcall,(char *) Message, Message->LENGTH))
return;
if (!checkCall(srcecall, (char *) Message, Message->LENGTH))
return;
// see if any Digis
if ((Message->ORIGIN[6] & 1) == 0) // Digis - ignore for now
return;
#ifndef WIN32
if (clock_gettime(CLOCK_REALTIME, &ts) == -1)
NowMs = NOW;
else
NowMs = ts.tv_sec + ts.tv_nsec / 1000000000.0;
#else
GetSystemTimeAsFileTime(&SystemTimeAsFileTime);
// FILETIME is 100 nS interval since 1601 - subtract 116444736000000000 to get 100 nS intervels since 1970 (Unix Time)
FT64.HighPart = SystemTimeAsFileTime.dwHighDateTime;
FT64.LowPart = SystemTimeAsFileTime.dwLowDateTime;
NowMs = (FT64.QuadPart - 116444736000000000) / 10000000.0;
#endif
if ((Message->DEST[6] & 0x80) == 0 && (Message->ORIGIN[6] & 0x80) == 0)
strcpy(CR, "V1");
else if ((Message->DEST[6] & 0x80))
strcpy(CR, "C");
else if (Message->ORIGIN[6] & 0x80)
strcpy(CR, "R");
else
strcpy(CR, "V1");
if (CTL & PFBIT)
{
if (CR[0] == 'C')
PF[0] = 'P';
else if (CR[0] == 'R')
PF[0] = 'F';
}
CTL &= ~PFBIT;
if ((CTL & 1) == 0) // I frame
{
NS = (CTL >> 1) & 7; // ISOLATE RECEIVED N(S)
NR = (CTL >> 5) & 7;
IFlag = 1;
iLen = Message->LENGTH - (MSGHDDRLEN + 16); // Dest origin ctl pid
strcpy(Type, "I");
}
else if (CTL == 3)
{
// Un-numbered Information Frame
strcpy(Type, "UI");
UIFlag = 1;
iLen = Message->LENGTH - (MSGHDDRLEN + 16); // Dest origin ctl pid
}
if (CTL & 2)
{
// UnNumbered
UFlag = 1;
switch (CTL)
{
case SABM:
strcpy(Type, "C");
break;
case SABME:
strcpy(Type, "SABME");
break;
case XID:
strcpy(Type, "XID");
break;
case TEST:
strcpy(Type, "TEST");
break;
case DISC:
strcpy(Type, "D");
break;
case DM:
strcpy(Type, "DM");
break;
case UA:
strcpy(Type, "UA");
break;
case FRMR:
strcpy(Type, "FRMR");
break;
}
}
else
{
// Super
NR = (CTL >> 5) & 7;
NS = (CTL >> 1) & 7; // ISOLATE RECEIVED N(S)
switch (CTL & 0x0F)
{
case RR:
strcpy(Type, "RR");
break;
case RNR:
strcpy(Type, "RNR");
break;
case REJ:
strcpy(Type, "REJ");
break;
case SREJ:
strcpy(Type, "SREJ");
break;
}
}
// Common to all frame types
udplen = snprintf(UDPMsg, 2048,
"{\"@type\": \"L2Trace\", \"serial\": %d, \"time\": %.3f, \"dirn\": \"%s\", \"isRF\": %s, \"reportFrom\": \"%s\", \"port\": \"%d\", \"srce\": \"%s\", \"dest\": \"%s\", \"ctrl\": %d,"
"\"l2Type\": \"%s\", \"modulo\": 8, \"cr\": \"%s\"",
UDPSeq++, NowMs, Dirn, (PORT->isRF)?"true":"false", NODECALLLOPPED, Message->PORT, srcecall, destcall, Message->CTL, Type, CR);
if (UIFlag)
{
udplen += snprintf(&UDPMsg[udplen], 2048 - udplen,
", \"ilen\": %d, \"pid\": %d, \"ptcl\": \"%s\"", iLen, Message->PID, PIDtoText(Message->PID));
if (Message->PID == NETROM_PID)
{
udplen += decodeNETROMUIMsg(Message->L2DATA, iLen, &UDPMsg[udplen], 2048 - udplen);
}
}
else if (IFlag)
{
if (PF[0])
udplen += snprintf(&UDPMsg[udplen], 2048 - udplen,
", \"ilen\": %d, \"pid\": %d, \"ptcl\": \"%s\", \"pf\": \"%s\", \"rseq\": %d, \"tseq\": %d",
iLen, Message->PID, PIDtoText(Message->PID), PF, NR, NS);
else
udplen += snprintf(&UDPMsg[udplen], 2048 - udplen,
", \"ilen\": %d, \"pid\": %d, \"ptcl\": \"%s\", \"rseq\": %d, \"tseq\": %d",
iLen, Message->PID, PIDtoText(Message->PID), NR, NS);
if (Message->PID == NETROM_PID)
{
int n = decodeNETROMIFrame(Message->L2DATA, iLen, &UDPMsg[udplen], 2048 - udplen);
if (n == 0)
return; // Can't decode so don't trace anything;
udplen += n;
}
}
else if (UFlag)
{
if (PF[0])
udplen += snprintf(&UDPMsg[udplen], 2048 - udplen, ", \"pf\": \"%s\"", PF);
}
else
{
// supervisory
if (PF[0])
udplen += snprintf(&UDPMsg[udplen], 2048 - udplen, ", \"pf\": \"%s\", \"rseq\": %d", PF, NR);
else
udplen += snprintf(&UDPMsg[udplen], 2048 - udplen, ", \"rseq\": %d", NR);
}
UDPMsg[udplen++] = '}';
UDPMsg[udplen] = 0;
// Debugprintf(UDPMsg);
sendto(NodeAPISocket, UDPMsg, udplen, 0, (struct sockaddr *)&UDPreportdest, sizeof(UDPreportdest));
}
//"@type" = @L3Trace, reportFrom, time, dirn,
void NetromTCPTrace(struct _MESSAGE * Message, char * Dirn)
{
char UDPMsg[2048];
int udplen;
int iLen = Message->LENGTH - (15 + MSGHDDRLEN);
int isRF = 0;
udplen = snprintf(UDPMsg, 2048,
"{\"@type\": \"L3Trace\", \"serial\": %d, \"time\": %d, \"dirn\": \"%s\", \"isRF\": %s, \"reportFrom\": \"%s\", \"port\": %d",
UDPSeq++, (int)NOW, Dirn, (isRF)?"true":"false", NODECALLLOPPED, Message->PORT);
udplen += snprintf(&UDPMsg[udplen], 2048 - udplen,
", \"ilen\": %d, \"pid\": %d, \"ptcl\": \"%s\"",
iLen, Message->PID, PIDtoText(Message->PID));
if (Message->PID == NETROM_PID)
{
int n = decodeNETROMIFrame(Message->L2DATA, iLen, &UDPMsg[udplen], 2048 - udplen);
if (n == 0)
return; // Can't decode so don't trace anything;
udplen += n;
}