forked from wb2osz/direwolf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ax25_pad.c
2822 lines (2305 loc) · 78.5 KB
/
ax25_pad.c
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
//
// This file is part of Dire Wolf, an amateur radio packet TNC.
//
// Copyright (C) 2011 , 2013, 2014, 2015 John Langner, WB2OSZ
//
// 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 2 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 this program. If not, see <http://www.gnu.org/licenses/>.
//
/*------------------------------------------------------------------
*
* Name: ax25_pad
*
* Purpose: Packet assembler and disasembler.
*
* This was written when I was only concerned about APRS which
* uses only UI frames. ax25_pad2.c, added years later, has
* functions for dealing with other types of frames.
*
* We can obtain AX.25 packets from different sources:
*
* (a) from an HDLC frame.
* (b) from text representation.
* (c) built up piece by piece.
*
* We also want to use a packet in different ways:
*
* (a) transmit as an HDLC frame.
* (b) print in human-readable text.
* (c) take it apart piece by piece.
*
* Looking at the more general case, we also want to modify
* an existing packet. For instance an APRS repeater might
* want to change "WIDE2-2" to "WIDE2-1" and retransmit it.
*
*
* Description:
*
*
* APRS uses only UI frames.
* Each starts with 2-10 addressses (14-70 octets):
*
* * Destination Address (note: opposite order in printed format)
*
* * Source Address
*
* * 0-8 Digipeater Addresses (Could there ever be more as a result of
* digipeaters inserting their own call for
* the tracing feature?
* NO. The limit is 8 when transmitting AX.25 over the
* radio.
* Communication with an IGate server could
* have a longer VIA path but that is only in text form,
* not as an AX.25 frame.)
*
* Each address is composed of:
*
* * 6 upper case letters or digits, blank padded.
* These are shifted left one bit, leaving the LSB always 0.
*
* * a 7th octet containing the SSID and flags.
* The LSB is always 0 except for the last octet of the address field.
*
* The final octet of the Destination has the form:
*
* C R R SSID 0, where,
*
* C = command/response = 1
* R R = Reserved = 1 1
* SSID = substation ID
* 0 = zero
*
* The AX.25 spec states that the RR bits should be 11 if not used.
* There are a couple documents talking about possible uses for APRS.
* I'm ignoring them for now.
* http://www.aprs.org/aprs12/preemptive-digipeating.txt
* http://www.aprs.org/aprs12/RR-bits.txt
*
* I don't recall why I originally intended to set the source/destination C bits both to 1.
* Reviewing this 5 years later, after spending more time delving into the
* AX.25 spec, I think it should be 1 for destination and 0 for source.
* In practice you see all four combinations being used by APRS stations
* and no one really cares about these two bits.
*
* The final octet of the Source has the form:
*
* C R R SSID 0, where,
*
* C = command/response = 1 (originally, now I think it should be 0 for source.)
* (Haven't gone back to check to see what code actually does.)
* R R = Reserved = 1 1
* SSID = substation ID
* 0 = zero (or 1 if no repeaters)
*
* The final octet of each repeater has the form:
*
* H R R SSID 0, where,
*
* H = has-been-repeated = 0 initially.
* Set to 1 after this address has been used.
* R R = Reserved = 1 1
* SSID = substation ID
* 0 = zero (or 1 if last repeater in list)
*
* A digipeater would repeat this frame if it finds its address
* with the "H" bit set to 0 and all earlier repeater addresses
* have the "H" bit set to 1.
* The "H" bit would be set to 1 in the repeated frame.
*
* In standard monitoring format, an asterisk is displayed after the last
* digipeater with the "H" bit set. That indicates who you are hearing
* over the radio.
* (That is if digipeaters update the via path properly. Some don't so
* we don't know who we are hearing. This is discussed in the User Guide.)
* No asterisk means the source is being heard directly.
*
* Example, if we can hear all stations involved,
*
* SRC>DST,RPT1,RPT2,RPT3: -- we heard SRC
* SRC>DST,RPT1*,RPT2,RPT3: -- we heard RPT1
* SRC>DST,RPT1,RPT2*,RPT3: -- we heard RPT2
* SRC>DST,RPT1,RPT2,RPT3*: -- we heard RPT3
*
*
* Next we have:
*
* * One byte Control Field - APRS uses 3 for UI frame
* The more general AX.25 frame can have two.
*
* * One byte Protocol ID - APRS uses 0xf0 for no layer 3
*
* Finally the Information Field of 1-256 bytes.
*
* And, of course, the 2 byte CRC.
*
* The descriptions above, for the C, H, and RR bits, are for APRS usage.
* When operating as a KISS TNC we just pass everything along and don't
* interpret or change them.
*
*
* Constructors: ax25_init - Clear everything.
* ax25_from_text - Tear apart a text string
* ax25_from_frame - Tear apart an AX.25 frame.
* Must be called before any other function.
*
* Get methods: .... - Extract destination, source, or digipeater
* address from frame.
*
* Assumptions: CRC has already been verified to be correct.
*
*------------------------------------------------------------------*/
#define AX25_PAD_C /* this will affect behavior of ax25_pad.h */
#include "direwolf.h"
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <stdio.h>
#include <ctype.h>
#include "regex.h"
#if __WIN32__
char *strtok_r(char *str, const char *delim, char **saveptr);
#endif
#include "textcolor.h"
#include "ax25_pad.h"
#include "fcs_calc.h"
/*
* Accumulate statistics.
* If new_count gets much larger than delete_count plus the size of
* the transmit queue we have a memory leak.
*/
static volatile int new_count = 0;
static volatile int delete_count = 0;
static volatile int last_seq_num = 0;
#if AX25MEMDEBUG
int ax25memdebug = 0;
void ax25memdebug_set(void)
{
ax25memdebug = 1;
}
int ax25memdebug_get (void)
{
return (ax25memdebug);
}
int ax25memdebug_seq (packet_t this_p)
{
return (this_p->seq);
}
#endif
#define CLEAR_LAST_ADDR_FLAG this_p->frame_data[this_p->num_addr*7-1] &= ~ SSID_LAST_MASK
#define SET_LAST_ADDR_FLAG this_p->frame_data[this_p->num_addr*7-1] |= SSID_LAST_MASK
/*------------------------------------------------------------------------------
*
* Name: ax25_new
*
* Purpose: Allocate memory for a new packet object.
*
* Returns: Identifier for a new packet object.
* In the current implementation this happens to be a pointer.
*
*------------------------------------------------------------------------------*/
packet_t ax25_new (void)
{
struct packet_s *this_p;
#if DEBUG
text_color_set(DW_COLOR_DEBUG);
dw_printf ("ax25_new(): before alloc, new=%d, delete=%d\n", new_count, delete_count);
#endif
last_seq_num++;
new_count++;
/*
* check for memory leak.
*/
// version 1.4 push up the threshold. We could have considerably more with connected mode.
//if (new_count > delete_count + 100) {
if (new_count > delete_count + 256) {
text_color_set(DW_COLOR_ERROR);
dw_printf ("Report to WB2OSZ - Memory leak for packet objects. new=%d, delete=%d\n", new_count, delete_count);
#if AX25MEMDEBUG
// Force on debug option to gather evidence.
ax25memdebug_set();
#endif
}
this_p = calloc(sizeof (struct packet_s), (size_t)1);
if (this_p == NULL) {
text_color_set(DW_COLOR_ERROR);
dw_printf ("ERROR - can't allocate memory in ax25_new.\n");
}
assert (this_p != NULL);
this_p->magic1 = MAGIC;
this_p->seq = last_seq_num;
this_p->magic2 = MAGIC;
this_p->num_addr = (-1);
return (this_p);
}
/*------------------------------------------------------------------------------
*
* Name: ax25_delete
*
* Purpose: Destroy a packet object, freeing up memory it was using.
*
*------------------------------------------------------------------------------*/
#if AX25MEMDEBUG
void ax25_delete_debug (packet_t this_p, char *src_file, int src_line)
#else
void ax25_delete (packet_t this_p)
#endif
{
#if DEBUG
text_color_set(DW_COLOR_DEBUG);
dw_printf ("ax25_delete(): before free, new=%d, delete=%d\n", new_count, delete_count);
#endif
if (this_p == NULL) {
text_color_set(DW_COLOR_ERROR);
dw_printf ("ERROR - NULL pointer passed to ax25_delete.\n");
return;
}
delete_count++;
#if AX25MEMDEBUG
if (ax25memdebug) {
text_color_set(DW_COLOR_DEBUG);
dw_printf ("ax25_delete, seq=%d, called from %s %d, new_count=%d, delete_count=%d\n", this_p->seq, src_file, src_line, new_count, delete_count);
}
#endif
assert (this_p->magic1 == MAGIC);
assert (this_p->magic2 == MAGIC);
this_p->magic1 = 0;
this_p->magic1 = 0;
//memset (this_p, 0, sizeof (struct packet_s));
free (this_p);
}
/*------------------------------------------------------------------------------
*
* Name: ax25_from_text
*
* Purpose: Parse a frame in human-readable monitoring format and change
* to internal representation.
*
* Input: monitor - "TNC-2" monitor format for packet. i.e.
* source>dest[,repeater1,repeater2,...]:information
*
* The information part can have non-printable characters
* in the form of <0xff>. This will be converted to single
* bytes. e.g. <0x0d> is carriage return.
* In version 1.4H we will allow nul characters which means
* we have to maintain a length rather than using strlen().
* I maintain that it violates the spec but want to handle it
* because it does happen and we want to preserve it when
* acting as an IGate rather than corrupting it.
*
* strict - True to enforce rules for packets sent over the air.
* False to be more lenient for packets from IGate server.
*
* Messages from an IGate server can have longer
* addresses after qAC. Up to 9 observed so far.
*
* We can just truncate the name because we will only
* end up discarding it. TODO: check on this.
*
* Returns: Pointer to new packet object in the current implementation.
*
* Outputs: Use the "get" functions to retrieve information in different ways.
*
*------------------------------------------------------------------------------*/
#if AX25MEMDEBUG
packet_t ax25_from_text_debug (char *monitor, int strict, char *src_file, int src_line)
#else
packet_t ax25_from_text (char *monitor, int strict)
#endif
{
/*
* Tearing it apart is destructive so make our own copy first.
*/
char stuff[512];
char *pinfo;
char *pa;
char *saveptr; /* Used with strtok_r because strtok is not thread safe. */
int ssid_temp, heard_temp;
char atemp[AX25_MAX_ADDR_LEN];
char info_part[AX25_MAX_INFO_LEN+1];
int info_len;
packet_t this_p = ax25_new ();
#if AX25MEMDEBUG
if (ax25memdebug) {
text_color_set(DW_COLOR_DEBUG);
dw_printf ("ax25_from_text, seq=%d, called from %s %d\n", this_p->seq, src_file, src_line);
}
#endif
/* Is it possible to have a nul character (zero byte) in the */
/* information field of an AX.25 frame? */
/* At this point, we have a normal C string. */
/* It is possible that will convert <0x00> to a nul character later. */
/* There we need to maintain a separate length and not use normal C string functions. */
strlcpy (stuff, monitor, sizeof(stuff));
/*
* Initialize the packet structure with two addresses and control/pid
* for APRS.
*/
memset (this_p->frame_data + AX25_DESTINATION*7, ' ' << 1, 6);
this_p->frame_data[AX25_DESTINATION*7+6] = SSID_H_MASK | SSID_RR_MASK;
memset (this_p->frame_data + AX25_SOURCE*7, ' ' << 1, 6);
this_p->frame_data[AX25_SOURCE*7+6] = SSID_H_MASK | SSID_RR_MASK | SSID_LAST_MASK;
this_p->frame_data[14] = AX25_UI_FRAME;
this_p->frame_data[15] = AX25_PID_NO_LAYER_3;
this_p->frame_len = 7 + 7 + 1 + 1;
this_p->num_addr = (-1);
assert (ax25_get_num_addr(this_p) == 2);
/*
* Separate the addresses from the rest.
*/
pinfo = strchr (stuff, ':');
if (pinfo == NULL) {
ax25_delete (this_p);
return (NULL);
}
*pinfo = '\0';
pinfo++;
/*
* Separate the addresses.
* Note that source and destination order is swappped.
*/
/*
* Source address.
* Don't use traditional strtok because it is not thread safe.
*/
pa = strtok_r (stuff, ">", &saveptr);
if (pa == NULL) {
text_color_set(DW_COLOR_ERROR);
dw_printf ("Failed to create packet from text. No source address\n");
ax25_delete (this_p);
return (NULL);
}
if ( ! ax25_parse_addr (AX25_SOURCE, pa, strict, atemp, &ssid_temp, &heard_temp)) {
text_color_set(DW_COLOR_ERROR);
dw_printf ("Failed to create packet from text. Bad source address\n");
ax25_delete (this_p);
return (NULL);
}
ax25_set_addr (this_p, AX25_SOURCE, atemp);
ax25_set_h (this_p, AX25_SOURCE); // c/r in this position
ax25_set_ssid (this_p, AX25_SOURCE, ssid_temp);
/*
* Destination address.
*/
pa = strtok_r (NULL, ",", &saveptr);
if (pa == NULL) {
text_color_set(DW_COLOR_ERROR);
dw_printf ("Failed to create packet from text. No destination address\n");
ax25_delete (this_p);
return (NULL);
}
if ( ! ax25_parse_addr (AX25_DESTINATION, pa, strict, atemp, &ssid_temp, &heard_temp)) {
text_color_set(DW_COLOR_ERROR);
dw_printf ("Failed to create packet from text. Bad destination address\n");
ax25_delete (this_p);
return (NULL);
}
ax25_set_addr (this_p, AX25_DESTINATION, atemp);
ax25_set_h (this_p, AX25_DESTINATION); // c/r in this position
ax25_set_ssid (this_p, AX25_DESTINATION, ssid_temp);
/*
* VIA path.
*/
while (( pa = strtok_r (NULL, ",", &saveptr)) != NULL && this_p->num_addr < AX25_MAX_ADDRS ) {
int k;
k = this_p->num_addr;
if ( ! ax25_parse_addr (k, pa, strict, atemp, &ssid_temp, &heard_temp)) {
text_color_set(DW_COLOR_ERROR);
dw_printf ("Failed to create packet from text. Bad digipeater address\n");
ax25_delete (this_p);
return (NULL);
}
ax25_set_addr (this_p, k, atemp);
ax25_set_ssid (this_p, k, ssid_temp);
// Does it have an "*" at the end?
// TODO: Complain if more than one "*".
// Could also check for all has been repeated bits are adjacent.
if (heard_temp) {
for ( ; k >= AX25_REPEATER_1; k--) {
ax25_set_h (this_p, k);
}
}
}
/*
* Finally, process the information part.
*
* Translate hexadecimal values like <0xff> to single bytes.
* MIC-E format uses 5 different non-printing characters.
* We might want to manually generate UTF-8 characters such as degree.
*/
//#define DEBUG14H 1
#if DEBUG14H
text_color_set(DW_COLOR_DEBUG);
dw_printf ("BEFORE: %s\nSAFE: ", pinfo);
ax25_safe_print (pinfo, -1, 0);
dw_printf ("\n");
#endif
info_len = 0;
while (*pinfo != '\0' && info_len < AX25_MAX_INFO_LEN) {
if (strlen(pinfo) >= 6 &&
pinfo[0] == '<' &&
pinfo[1] == '0' &&
pinfo[2] == 'x' &&
isxdigit(pinfo[3]) &&
isxdigit(pinfo[4]) &&
pinfo[5] == '>') {
char *p;
info_part[info_len] = strtol (pinfo + 3, &p, 16);
info_len++;
pinfo += 6;
}
else {
info_part[info_len] = *pinfo;
info_len++;
pinfo++;
}
}
info_part[info_len] = '\0';
#if DEBUG14H
text_color_set(DW_COLOR_DEBUG);
dw_printf ("AFTER: %s\nSAFE: ", info_part);
ax25_safe_print (info_part, info_len, 0);
dw_printf ("\n");
#endif
/*
* Append the info part.
*/
memcpy ((char*)(this_p->frame_data+this_p->frame_len), info_part, info_len);
this_p->frame_len += info_len;
return (this_p);
}
/*------------------------------------------------------------------------------
*
* Name: ax25_from_frame
*
* Purpose: Split apart an HDLC frame to components.
*
* Inputs: fbuf - Pointer to beginning of frame.
*
* flen - Length excluding the two FCS bytes.
*
* alevel - Audio level of received signal.
* Maximum range 0 - 100.
* -1 might be used when not applicable.
*
* Returns: Pointer to new packet object or NULL if error.
*
* Outputs: Use the "get" functions to retrieve information in different ways.
*
*------------------------------------------------------------------------------*/
#if AX25MEMDEBUG
packet_t ax25_from_frame_debug (unsigned char *fbuf, int flen, alevel_t alevel, char *src_file, int src_line)
#else
packet_t ax25_from_frame (unsigned char *fbuf, int flen, alevel_t alevel)
#endif
{
packet_t this_p;
/*
* First make sure we have an acceptable length:
*
* We are not concerned with the FCS (CRC) because someone else checked it.
*
* Is is possible to have zero length for info?
*
* In the original version, assuming APRS, the answer was no.
* We always had at least 3 octets after the address part:
* control, protocol, and first byte of info part for data type.
*
* In later versions, this restriction was relaxed so other
* variations of AX.25 could be used. Now the minimum length
* is 7+7 for addresses plus 1 for control.
*
*/
if (flen < AX25_MIN_PACKET_LEN || flen > AX25_MAX_PACKET_LEN)
{
text_color_set(DW_COLOR_ERROR);
dw_printf ("Frame length %d not in allowable range of %d to %d.\n", flen, AX25_MIN_PACKET_LEN, AX25_MAX_PACKET_LEN);
return (NULL);
}
this_p = ax25_new ();
#if AX25MEMDEBUG
if (ax25memdebug) {
text_color_set(DW_COLOR_DEBUG);
dw_printf ("ax25_from_frame, seq=%d, called from %s %d\n", this_p->seq, src_file, src_line);
}
#endif
/* Copy the whole thing intact. */
memcpy (this_p->frame_data, fbuf, flen);
this_p->frame_data[flen] = 0;
this_p->frame_len = flen;
/* Find number of addresses. */
this_p->num_addr = (-1);
(void) ax25_get_num_addr (this_p);
return (this_p);
}
/*------------------------------------------------------------------------------
*
* Name: ax25_dup
*
* Purpose: Make a copy of given packet object.
*
* Inputs: copy_from - Existing packet object.
*
* Returns: Pointer to new packet object or NULL if error.
*
*
*------------------------------------------------------------------------------*/
#if AX25MEMDEBUG
packet_t ax25_dup_debug (packet_t copy_from, char *src_file, int src_line)
#else
packet_t ax25_dup (packet_t copy_from)
#endif
{
int save_seq;
packet_t this_p;
this_p = ax25_new ();
assert (this_p != NULL);
save_seq = this_p->seq;
memcpy (this_p, copy_from, sizeof (struct packet_s));
this_p->seq = save_seq;
#if AX25MEMDEBUG
if (ax25memdebug) {
text_color_set(DW_COLOR_DEBUG);
dw_printf ("ax25_dup, seq=%d, called from %s %d, clone of seq %d\n", this_p->seq, src_file, src_line, copy_from->seq);
}
#endif
return (this_p);
}
/*------------------------------------------------------------------------------
*
* Name: ax25_parse_addr
*
* Purpose: Parse address with optional ssid.
*
* Inputs: position - AX25_DESTINATION, AX25_SOURCE, AX25_REPEATER_1...
* Used for more specific error message. -1 if not used.
*
* in_addr - Input such as "WB2OSZ-15*"
*
* strict - 1 (true) for strict checking (6 characters, no lower case,
* SSID must be in range of 0 to 15).
* Strict is appropriate for packets sent
* over the radio. Communication with IGate
* allows lower case (e.g. "qAR") and two
* alphanumeric characters for the SSID.
* We also get messages like this from a server.
* KB1POR>APU25N,TCPIP*,qAC,T2NUENGLD:...
*
* 2 (extra true) will complain if * is found at end.
*
* Outputs: out_addr - Address without any SSID.
* Must be at least AX25_MAX_ADDR_LEN bytes.
*
* out_ssid - Numeric value of SSID.
*
* out_heard - True if "*" found.
*
* Returns: True (1) if OK, false (0) if any error.
* When 0, out_addr, out_ssid, and out_heard are unpredictable.
*
*
*------------------------------------------------------------------------------*/
static const char *position_name[1 + AX25_MAX_ADDRS] = {
"", "Destination ", "Source ",
"Digi1 ", "Digi2 ", "Digi3 ", "Digi4 ",
"Digi5 ", "Digi6 ", "Digi7 ", "Digi8 " };
int ax25_parse_addr (int position, char *in_addr, int strict, char *out_addr, int *out_ssid, int *out_heard)
{
char *p;
char sstr[8]; /* Should be 1 or 2 digits for SSID. */
int i, j, k;
int maxlen;
*out_addr = '\0';
*out_ssid = 0;
*out_heard = 0;
if (position < -1) position = -1;
if (position > AX25_REPEATER_8) position = AX25_REPEATER_8;
position++; /* Adjust for position_name above. */
if (strict && strlen(in_addr) >= 2 && strncmp(in_addr, "qA", 2) == 0) {
text_color_set(DW_COLOR_ERROR);
dw_printf ("%sAddress \"%s\" is a \"q-construct\" used for communicating with\n", position_name[position], in_addr);
dw_printf ("APRS Internet Servers. It should never appear when going over the radio.\n");
}
//dw_printf ("ax25_parse_addr in: %s\n", in_addr);
maxlen = strict ? 6 : (AX25_MAX_ADDR_LEN-1);
p = in_addr;
i = 0;
for (p = in_addr; *p != '\0' && *p != '-' && *p != '*'; p++) {
if (i >= maxlen) {
text_color_set(DW_COLOR_ERROR);
dw_printf ("%sAddress is too long. \"%s\" has more than %d characters.\n", position_name[position], in_addr, maxlen);
return 0;
}
if ( ! isalnum(*p)) {
text_color_set(DW_COLOR_ERROR);
dw_printf ("%sAddress, \"%s\" contains character other than letter or digit in character position %d.\n", position_name[position], in_addr, (int)(long)(p-in_addr)+1);
return 0;
}
out_addr[i++] = *p;
out_addr[i] = '\0';
#if DECAMAIN // Hack when running in decode_aprs utility.
// Exempt the "qA..." case because it was already mentioned.
if (strict && islower(*p) && strncmp(in_addr, "qA", 2) != 0) {
text_color_set(DW_COLOR_ERROR);
dw_printf ("%sAddress has lower case letters. \"%s\" must be all upper case.\n", position_name[position], in_addr);
}
#else
if (strict && islower(*p)) {
text_color_set(DW_COLOR_ERROR);
dw_printf ("%sAddress has lower case letters. \"%s\" must be all upper case.\n", position_name[position], in_addr);
return 0;
}
#endif
}
j = 0;
sstr[j] = '\0';
if (*p == '-') {
for (p++; isalnum(*p); p++) {
if (j >= 2) {
text_color_set(DW_COLOR_ERROR);
dw_printf ("%sSSID is too long. SSID part of \"%s\" has more than 2 characters.\n", position_name[position], in_addr);
return 0;
}
sstr[j++] = *p;
sstr[j] = '\0';
if (strict && ! isdigit(*p)) {
text_color_set(DW_COLOR_ERROR);
dw_printf ("%sSSID must be digits. \"%s\" has letters in SSID.\n", position_name[position], in_addr);
return 0;
}
}
k = atoi(sstr);
if (k < 0 || k > 15) {
text_color_set(DW_COLOR_ERROR);
dw_printf ("%sSSID out of range. SSID of \"%s\" not in range of 0 to 15.\n", position_name[position], in_addr);
return 0;
}
*out_ssid = k;
}
if (*p == '*') {
*out_heard = 1;
p++;
if (strict == 2) {
text_color_set(DW_COLOR_ERROR);
dw_printf ("\"*\" is not allowed at end of address \"%s\" here.\n", in_addr);
return 0;
}
}
if (*p != '\0') {
text_color_set(DW_COLOR_ERROR);
dw_printf ("Invalid character \"%c\" found in %saddress \"%s\".\n", *p, position_name[position], in_addr);
return 0;
}
//dw_printf ("ax25_parse_addr out: %s %d %d\n", out_addr, *out_ssid, *out_heard);
return (1);
} /* end ax25_parse_addr */
/*-------------------------------------------------------------------
*
* Name: ax25_check_addresses
*
* Purpose: Check addresses of given packet and print message if any issues.
* We call this when receiving and transmitting.
*
* Inputs: pp - packet object pointer.
*
* Errors: Print error message.
*
* Returns: 1 for all valid. 0 if not.
*
* Examples: I was surprised to get this from an APRS-IS server with
* a lower case source address.
*
* n1otx>APRS,TCPIP*,qAC,THIRD:@141335z4227.48N/07111.73W_348/005g014t044r000p000h60b10075.wview_5_20_2
*
* I haven't gotten to the bottom of this yet but it sounds
* like "q constructs" are somehow getting on to the air when
* they should only appear in conversations with IGate servers.
*
* https://groups.yahoo.com/neo/groups/direwolf_packet/conversations/topics/678
*
* WB0VGI-7>APDW12,W0YC-5*,qAR,AE0RF-10:}N0DZQ-10>APWW10,TCPIP,WB0VGI-7*:;145.230MN*080306z4607.62N/09230.58WrKE0ACL/R 145.230- T146.2 (Pine County ARES)
*
* Typical result:
*
* Digipeater WIDE2 (probably N3LEE-4) audio level = 28(10/6) [NONE] __|||||||
* [0.5] VE2DJE-9>P_0_P?,VE2PCQ-3,K1DF-7,N3LEE-4,WIDE2*:'{S+l <0x1c>>/
* Invalid character "_" in MIC-E destination/latitude.
* Invalid character "_" in MIC-E destination/latitude.
* Invalid character "?" in MIC-E destination/latitude.
* Invalid MIC-E N/S encoding in 4th character of destination.
* Invalid MIC-E E/W encoding in 6th character of destination.
* MIC-E, normal car (side view), Unknown manufacturer, Returning
* N 00 00.0000, E 005 55.1500, 0 MPH
* Invalid character "_" found in Destination address "P_0_P?".
*
* *** The origin and journey of this packet should receive some scrutiny. ***
*
*--------------------------------------------------------------------*/
int ax25_check_addresses (packet_t pp)
{
int n;
char addr[AX25_MAX_ADDR_LEN];
char ignore1[AX25_MAX_ADDR_LEN];
int ignore2, ignore3;
int all_ok = 1;
for (n = 0; n < ax25_get_num_addr(pp); n++) {
ax25_get_addr_with_ssid (pp, n, addr);
all_ok &= ax25_parse_addr (n, addr, 1, ignore1, &ignore2, &ignore3);
}
if (! all_ok) {
text_color_set(DW_COLOR_ERROR);
dw_printf ("\n");
dw_printf ("*** The origin and journey of this packet should receive some scrutiny. ***\n");
dw_printf ("\n");
}
return (all_ok);
} /* end ax25_check_addresses */
/*------------------------------------------------------------------------------
*
* Name: ax25_unwrap_third_party
*
* Purpose: Unwrap a third party messge from the header.
*
* Inputs: copy_from - Existing packet object.
*
* Returns: Pointer to new packet object or NULL if error.
*
* Example: Input: A>B,C:}D>E,F:info
* Output: D>E,F:info
*
*------------------------------------------------------------------------------*/
packet_t ax25_unwrap_third_party (packet_t from_pp)
{
unsigned char *info_p;
packet_t result_pp;
if (ax25_get_dti(from_pp) != '}') {
text_color_set(DW_COLOR_ERROR);
dw_printf ("Internal error: ax25_unwrap_third_party: wrong data type.\n");
return (NULL);
}
(void) ax25_get_info (from_pp, &info_p);
// Want strict because addresses should conform to AX.25 here.
// That's not the case for something from an Internet Server.
result_pp = ax25_from_text((char *)info_p + 1, 1);
return (result_pp);
}
/*------------------------------------------------------------------------------
*
* Name: ax25_set_addr
*
* Purpose: Add or change an address.
*
* Inputs: n - Index of address. Use the symbols
* AX25_DESTINATION, AX25_SOURCE, AX25_REPEATER1, etc.
*
* Must be either an existing address or one greater
* than the final which causes a new one to be added.
*
* ad - Address with optional dash and substation id.
*
* Assumption: ax25_from_text or ax25_from_frame was called first.
*
* TODO: ax25_from_text could use this.
*
* Returns: None.
*
*------------------------------------------------------------------------------*/
void ax25_set_addr (packet_t this_p, int n, char *ad)
{
int ssid_temp, heard_temp;
char atemp[AX25_MAX_ADDR_LEN];
int i;
assert (this_p->magic1 == MAGIC);
assert (this_p->magic2 == MAGIC);
assert (n >= 0 && n < AX25_MAX_ADDRS);
//dw_printf ("ax25_set_addr (%d, %s) num_addr=%d\n", n, ad, this_p->num_addr);
if (n >= 0 && n < this_p->num_addr) {
//dw_printf ("ax25_set_addr , existing case\n");
/*
* Set existing address position.
*/
// Why aren't we setting 'strict' here?
// Messages from IGate have q-constructs.
// We use this to parse it and later remove unwanted parts.
ax25_parse_addr (n, ad, 0, atemp, &ssid_temp, &heard_temp);
memset (this_p->frame_data + n*7, ' ' << 1, 6);