-
Notifications
You must be signed in to change notification settings - Fork 8
/
aprsdigi.c
2797 lines (2586 loc) · 83.7 KB
/
aprsdigi.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
/* aprsdigi: APRS-style UI frame digipeater and node.
*
* APRS has some peculiar digipeating ideas, so let's test them here:
*
* 1. Several digipeater aliases such as RELAY, WIDE, GATE.
* 2. WIDEn-n flooding algorithm.
* 3. SSID-based shorthand for digi path.
*
* See Bob Bruninga's (WB4APR) README/MIC-E.TXT for a description of
* methods 2 and 3. #1 is conventional TNC digipeating with MYAlias, etc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License, version 2 as
* published by the Free Software Foundation.
*
* 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, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* (See the file "COPYING" that is included with this source distribution.)
*
* portions derived from ax25-utils/listen.c
*
*/
static char copyr[] = "Copyright (c) 1996,1997,1999,2001,2002,2003,2004,2009,2012 Alan Crosswell, [email protected]";
/*
* Alan Crosswell, N2YGK
* 144 Washburn Road
* Briarcliff Manor, NY 10510, USA
*
* TODO:
* see ./TODO
*/
#include <sys/types.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <net/if.h>
#include <netinet/if_ether.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#include <signal.h>
#include <errno.h>
#include <netax25/ax25.h>
#include <netax25/axlib.h>
#ifndef AX25_MTU
#define AX25_MTU 256
#endif
#include <netdb.h>
#include "mic_e.h"
#include "libax25ext.h"
#include "netax25/axconfig.h"
#ifndef PACKAGE
#define PACKAGE "aprsdigi"
#endif
#ifndef VERSION
#define VERSION "$Revision$";
#endif
/* some defines that really belong in a header file! */
#define ALEN 6 /* index to where the SSID and flags go */
#define AXLEN 7 /* length of a callsign */
/* SSID mask and various flags that are stuffed into the SSID byte */
#define SSID 0x1E
#define HDLCAEB 0x01
#define REPEATED 0x80
#define UI 0x03 /* unnumbered information (unproto) */
#define PID_NO_L3 0xF0 /* no level-3 (text) */
#define C 0x80
#define SSSID_SPARE 0x40 /* must be set if not used */
#define ESSID_SPARE 0x20
struct statistics { /* counters */
int rx; /* packets received */
int rx_ign; /* rx packets ignored */
int rx_dup; /* dupe packets killed */
int rx_loop; /* looping packets killed */
int rx_mic; /* mic_e packets received */
int rx_ssid; /* dest SSID packets received */
int tx; /* packets transmitted, sum of: */
int digi; /* regular digipeats */
int flood; /* flood-N digipeats */
int ssid; /* dest SSID digipeats */
int ids; /* id packets */
};
/* the per-interface information */
#define MAXALIASES 5
#define MAXINTF 10
struct budlist_entry { /* budlist entry */
struct budlist_entry *be_next;
int be_permit; /* permit/deny */
struct sockaddr_storage be_addr, be_mask;
int be_maskbits;
};
struct budlist { /* a given budlist */
struct budlist *bl_next;
int bl_list_no; /* list number */
struct budlist_entry *bl_ent_head, *bl_ent_last;
};
static struct budlist *Bl_head = NULL, *Bl_last = NULL;
struct dupelist_entry { /* dupelist entry */
struct dupelist_entry *de_next;
char *de_dev; /* interface device name to dupe to */
};
struct dupelist { /* a given dupelist */
struct dupelist *dl_next;
int dl_list_no; /* list number */
struct interface_list *dl_il; /* list of interfaces to dupe to */
struct dupelist_entry *dl_ent_head, *dl_ent_last;
};
static struct dupelist *Dl_head = NULL, *Dl_last=NULL;
static struct interface {
char *port; /* axports port name */
char *dev; /* kernel device name */
char *devname; /* user-friendly device name */
char *tag; /* optional tag text */
int taglen;
int idinterval; /* seconds between IDs; 0 for none. */
int i_flags; /* status flags */
enum {
P_UDP=1, /* interface is UDP socket */
P_AX25=2, /* interface is AX25 socket */
P_UNIX=3, /* interface is a unix file */
} i_proto;
ax25_address aliases[MAXALIASES]; /* intf call & aliases */
int n_aliases;
int rsock,tsock; /* socket fds */
#ifdef IPV6
struct sockaddr_storage rsa,tsa,rsafrom; /* and their sockaddrs */
#else
struct sockaddr rsa,tsa,rsafrom; /* and their sockaddrs */
#endif /*IPV6*/
int rsa_size,tsa_size; /* sendto() bug w/sizeof(sockaddr_storage)? */
time_t next_id; /* next time we ID */
u_char idbuf[AX25_MTU]; /* An ID packet for this intf */
int idlen;
struct statistics stats; /* statistics */
struct budlist *bud; /* budlist */
struct dupelist *dupe; /* list of interfaces to dupe to */
} Intf[MAXINTF];
#define MYCALL(n) Intf[n].aliases[0]
#define I_MYCALL(i) i->aliases[0]
static int N_intf = 0;
struct interface_list { /* a list of interfaces (duh) */
struct interface_list *next;
struct interface *i;
};
struct stuff { /* maybe I should learn C++... */
u_char *cp; /* pointer into the packet */
int len; /* length of it */
struct ax_calls in; /* the received packet's calls, flags, etc. */
struct ax_calls out; /* the transmitted packet's calls, etc. */
struct interface *i; /* the interface received on */
};
/* some global stuff */
/* General options: */
static int Verbose = 0;
static int Testing = 0;
static int Maxhops = 0; /* limit direct input packets to this many digipeats */
static int Digi_SSID = 0;
static int Kill_dupes = 0; /* kill dupes even in conventional mode */
static int Kill_loops = 0; /* kill loops */
static int Doing_dupes = 0; /* dupelist was set on some interface */
static int Max_wide_n = 0; /* The limit to wideN-N SSID value */
static char *Logfile = NULL;
static ax25_address Aprscall; /* replace mic-e encoded to-call with this */
static ax25_address Trace_dummy; /* dummy call for tracen-n substitution */
static ax25_address Widecall; /* dummy call for WIDEn-n substitution */
static ax25_address Udpipcall; /* dummy call for UDPIP 3rd party */
static int Have_digipath = 0;
#define DIGISIZE 7 /* one more than usual! */
static ax25_address Digipath[DIGISIZE]; /* for SSID-[1:7] w/o flooding */
static struct full_sockaddr_ax25 Path[4]; /* for SSID-[8:15] N S E W */
static char Dirs[5] = "NSEW";
/* per-interface default options: */
static char *Tag = NULL; /* tag onto end of rx'd posit */
static int Taglen = 0;
static int Idinterval = (9*60)+30; /* default to 9:30 */
static int Keep = 28; /* seconds to remember for dupe test */
static int I_flags = 0; /* interface default flags */
#define I_NEED_ID 0x01 /* need to ID */
#define SUBST_MYCALL 0x02 /* replace digi alias w/mycall */
#define MICE_XLATE 0x04 /* translate mic-e to tracker format */
#define X1J4_XLATE 0x08 /* translate x1j4 to plain format */
#define I_COOKED 0x20 /* interface is cooked */
#define I_RX 0x40 /* interface is enabled to receive */
#define I_TX 0x80 /* interface is enabled to transmit */
#define I_TXSAME 0x0100 /* enable re-TX on received interface */
#define I_3RDP 0x0200 /* enable 3rd party tunneling */
/*
* A table of recognized flooding alias prefixes. In current practice,
* the only recognized flood aliases are WIDEn and TRACEn(where n is 0-7)
* where the latter has truly odd behavior. The flavors of WIDE and TRACE
* are:
* 1. WIDE: A conventional alias, eligible for MYCALL substitution.
* Roughly equivalent to WIDE0-0.
* 2. WIDEn-n: Flooding algorithm. Decrement SSID and repeat the packet
* without setting the DIGIPEATED flag until it reaches zero at which
* point do set the DIGIPEATED flag and move on to the next callsign
* in the digipeat list. Does not get MYCALL substituted (even WIDEn-0)
* so the user can see that the packet came in via n (anonymous) WIDE
* digis.
* 3. TRACE: Like WIDE but MYCALL substitution is not required.
* 4. TRACEn-n: Do the SSID substitution like WIDE, but also insert MYCALL
* into the digi list:
* RELAY*,TRACE3-3
* RELAY,N2YGK-7*,TRACE3-2
* RELAY,N2YGK-7,WB2ZII*,TRACE3-1
* RELAY,N2YGK-7,WB2ZII,N2MH-15*,TRACE3*
* (What happens when the digi list gets too long for the AX.25 protocol
* appears not to have been considered in this design. I guess we'll
* just insert as many as we can.)
*/
/*
* These flags are shared by the calltab and floods tables and by
* the floodtype of a specific received call. That is, while there is
* only one WIDE calltab entry, a received callsign can be WIDE-n or
* WIDEn-n.
*/
#define C_IS_NOFLOOD 0 /* not a flood */
#define C_IS_FLOOD 1 /* this callsign is a flood (WIDE) */
#define C_IS_FLOODN 2 /* this is an N-N type flood (WIDEn-n) */
#define C_IS_TRACE 4 /* this is a trace-type flood (TRACE*) */
struct flood {
ax25_address call; /* the flooding callsign prefix */
int len; /* length */
int flags;
} Floods[MAXALIASES];
static int N_floods = 0;
#define FLOODCALL Floods[0].call
#define FLOODLEN Floods[0].len
/* forward declarations */
static int unproto(struct full_sockaddr_ax25 *, char *);
static int parsecalls(ax25_address*, int, char *);
static void print_it(FILE *,struct ax_calls *,unsigned char *,int len);
static void add_text(u_char **,int *,u_char *,int,char *,int);
static int dupe_packet(struct ax_calls *,u_char *,int);
static int loop_packet(struct ax_calls *,u_char *,int);
static int xmit(struct stuff *s, struct interface_list *list);
static void set_id(void);
static void rx_packet(struct interface *i, u_char *buffer, int len);
static void fix_recvfrom(struct stuff *s);
static int floodcmp(ax25_address *a, int len, ax25_address *b);
static int floodtype(ax25_address *c, int i);
static void sked_id(struct interface *iface);
static void budlist_add(char *, int);
static int budlist_permit(struct stuff *);
static void budlist_print_all(FILE *f);
static void budlist_print(FILE *f,struct budlist *);
static void dupelist_add(char *);
static void dupelist_print_all(FILE *f);
static void dupelist_print(FILE *f,struct dupelist *);
static void dupelist_init(void);
static void printaddr(struct sockaddr_storage *s, char *buf, int buflen);
static void setmask(int, u_char *);
static int cmpmask(int, u_char *, u_char *, u_char *);
static void die(char *);
static void usage(void);
static void usage_udp(void);
static void usage_unix(void);
static void usage_ax25(void);
static void usage_budlist(void);
static void check_config();
static void config_print(FILE *f);
static void print_dupes(void);
static struct interface_list *intf_of(ax25_address *callsign);
static void to_3rdparty(struct ax_calls *calls);
static void drop_unused_digis(struct ax_calls *calls);
static void from_3rdparty(struct stuff *s,
int npkts, /* dimension of the next two vectors */
u_char *pkt[], /* output buffers for source path header */
int pktl[]); /* output length of sph (AX25_MTU max) */
/* signal handlers */
static void cleanup(int),identify(int),identify_final(int),
print_stats(int),reset_stats(int);
static void do_opts(int argc, char **argv);
static void do_ports(void);
static void do_port_ax25(struct interface *);
static void do_port_udp(struct interface *);
static void do_port_unix(struct interface *);
static void set_sig(void);
static int rx_loop(void);
int
main(int argc, char **argv)
{
int r;
bzero(Intf,sizeof(Intf));
do_opts(argc,argv);
do_ports();
set_id();
set_sig();
check_config();
r = rx_loop();
exit(r);
}
/* Listen for received packets and farm out the work */
static int
rx_loop()
{
unsigned char buffer[AX25_MTU];
int n,selfds = 0;
struct interface *i;
fd_set select_mask;
/* set up the initial select mask */
FD_ZERO(&select_mask);
for (n = 0, i = Intf; n < N_intf; n++, i++) {
if (i->rsock >= 0 && (i->i_flags & I_RX)) {
FD_SET(i->rsock, &select_mask);
selfds = (i->rsock>selfds)?i->rsock:selfds;
}
}
++selfds;
for (;;) {
int len;
fd_set rmask = select_mask;
if (select(selfds,&rmask,NULL,NULL,NULL) < 0) {
if (errno == EINTR)
continue;
perror("select");
return 1;
}
/* find which sockets have data */
for (n = 0, i = Intf; n < N_intf; n++, i++) {
if (i->rsock >= 0 && FD_ISSET(i->rsock,&rmask)) {
int size = sizeof(i->rsafrom);
if ((len = recvfrom(i->rsock,buffer,sizeof(buffer),0,(struct sockaddr *)&i->rsafrom,&size)) < 0) {
if (errno == EINTR)
continue;
perror(i->port);
return 1;
}
rx_packet(i,buffer,len); /* process the received packet */
}
}
} /* end of for(;;) */
return 0; /* NOTREACHED */
}
/* Parse a received packet, convert and digipeat if necessary. */
static void rx_dupeit(struct stuff *s);
static int rx_nodigi(struct stuff *s,int r);
static int rx_dupe(struct stuff *s);
static int rx_to_me(struct stuff *s);
static int rx_bud_deny(struct stuff *s);
static int rx_from_me(struct stuff *s);
static int rx_ssid(struct stuff *s);
static int rx_limit(struct stuff *s);
static int rx_flood(struct stuff *s);
static int rx_digi(struct stuff *s);
static void
rx_packet(struct interface *i, /* which interface received on */
u_char *buffer, /* what was received */
int len) /* length received */
{
int r;
struct stuff s;
bzero(&s,sizeof(s));
s.i = i;
++s.i->stats.rx;
s.cp = buffer;
s.len = len;
/* parse the cooked or raw kiss frame */
if (i->i_flags&I_COOKED)
r = parse_cooked_ax25(&s.cp,&s.len,&s.in);
else
r = parse_raw_ax25(&s.cp,&s.len,&s.in);
fix_recvfrom(&s); /* workaround bug in AF_AX25 */
if (Verbose) {
char buf[20];
time_t tick = time(0);
strftime(buf,sizeof(buf),"%T",gmtime(&tick));
fprintf(stderr,"%s %s: RX: ",buf,s.i->port);
print_it(stderr,&s.in,s.cp,s.len);
}
/* go through a bunch of choices until we eat the packet or die:-) */
if (Doing_dupes)
rx_dupeit(&s); /* blindly dupe it */
if (rx_bud_deny(&s)) /* budlisted */
return;
else if (rx_from_me(&s)) /* don't digi my own repeated beacons! */
return;
else if (rx_ssid(&s)) /* dest SSID handling */
return;
else if (rx_nodigi(&s,r)) /* Nothing to digi? */
return;
else if (rx_dupe(&s)) /* Is it a killed dupe or loop? */
return;
else if (rx_to_me(&s)) /* Addressed to me? */
return;
else if (rx_limit(&s)) /* Will this packet exceed the digipeat limit */
return;
else if (rx_flood(&s)) /* flood special handling */
return;
else if (rx_digi(&s)) /* conventional digi */
return;
/* How'd we get here? */
if (Testing) {
fprintf(stderr,"%s: Not repeatable\n",s.i->port);
}
}
/* XXX bug(?) in AF_AX25 for raw sockets puts the interface name in
sax25_call instead of the sender's callsign:
(gdb) p (struct sockaddr_ax25)i->rsafrom
$3 = {sax25_family = 3, sax25_call = {ax25_call = "ax0\0\0\0"},
sax25_ndigis = 0}
*/
static void
fix_recvfrom(struct stuff *s)
{
struct sockaddr_ax25 *sax25 = (struct sockaddr_ax25 *)&s->i->rsafrom;
if (sax25->sax25_family == AF_AX25) {
bcopy(&s->in.ax_from_call,
sax25->sax25_call.ax25_call,sizeof(sax25->sax25_call.ax25_call));
}
}
static void
rx_dupeit(struct stuff *s)
{
int n;
if (s->i->dupe && s->i->dupe->dl_il) {
if (Verbose) {
fprintf(stderr,"%s: duping to dupelist %d\n",s->i->devname,
s->i->dupe->dl_list_no);
}
s->out = s->in;
if (xmit(s,s->i->dupe->dl_il) < 0)
perror("xmit");
}
}
static int
rx_nodigi(struct stuff *s,int r)
{
int result = 0;
/* bail if invalid or not doing SSID & no unrepeated digis */
if (r == PK_INVALID || r == PK_VALID || (r != PK_VALDIGI && !Digi_SSID)) {
++s->i->stats.rx_ign;
result = 1;
}
if (Verbose) {
if (result)
fprintf(stderr,"packet is %s. (r=%d)\n",
(r == PK_INVALID)?"invalid":"not repeatable",r);
else
fprintf(stderr,"packet is repeatable. (r=%d)\n",r);
}
return result;
}
static int
rx_dupe(struct stuff *s)
{
int result = 0;
static char *lupedupe[] = {"dupe or loop","dupe","loop"};
/* If packet was last transmitted by me, then don't digipeat it! */
if (Kill_dupes && dupe_packet(&s->in,s->cp,s->len)) {
++s->i->stats.rx_dup;
result = 1;
} else if (Kill_loops && loop_packet(&s->in,s->cp,s->len)) {
++s->i->stats.rx_loop;
result = 2;
}
if (Verbose) {
fprintf(stderr,"packet is%sa %s\n",result?" ":" not ",lupedupe[result]);
}
return result;
}
/*
* find interfaces belonging to a callsign or alias. Returns a pointer to
* a list of interfaces or NULL if not found.
*/
static struct callsign_list {
ax25_address *callsign;
int flags;
int floodlen; /* length of flood call */
struct interface_list *l; /* list of interfaces having this callsign */
struct interface_list *illast;
struct callsign_list *next;
} *calltab = NULL, *ctlast = NULL;
static void calltab_init(void);
static struct callsign_list *
calltab_entry(ax25_address *callsign,int *flags)
{
struct callsign_list *c;
int flagdummy;
if (flags == NULL)
flags = &flagdummy; /* dummy the flags if not needed */
*flags = 0;
for (c = calltab; c; c = c->next) {
if ((c->flags&C_IS_FLOOD
&& (*flags = floodcmp(c->callsign,c->floodlen,callsign)))
|| (ax25_cmp(c->callsign,callsign) == 0)) {
*flags |= c->flags; /* add stuff floodcmp doesn't know about */
return c;
}
}
return NULL;
}
static void
calltab_init(void)
{
struct interface *i;
int n,m;
/* iterate over all interfaces' callsigns */
for (i = Intf, n = 0; n < N_intf; n++,i++) {
for (m = 0; m < i->n_aliases; m++) {
struct interface_list *new_il =
(struct interface_list *)calloc(1,sizeof(struct interface_list));
struct callsign_list *c = (calltab)?calltab_entry(&i->aliases[m],0):NULL;
if (!c) { /* first time seeing this call */
int f;
c = (struct callsign_list *)calloc(1,sizeof(struct callsign_list));
c->callsign = &i->aliases[m];
/* see if this is a flood call */
for (f = 0; f < N_floods; f++) {
if (floodcmp(&Floods[f].call,Floods[f].len,c->callsign)) {
c->flags = Floods[f].flags;
c->floodlen = Floods[f].len;
break;
}
}
/* add the new callsign_list to calltab */
if (ctlast) {
ctlast->next = c;
ctlast = c;
} else {
calltab = ctlast = c; /* very first entry */
}
}
new_il->i = i; /* initialize the interface list */
if (c->illast) { /* and link it in to the callsign's list */
c->illast->next = new_il;
c->illast = new_il;
} else {
c->l = c->illast = new_il;
}
}
}
}
static struct interface_list *
intf_of(ax25_address *callsign)
{
struct callsign_list *c;
if (c = calltab_entry(callsign,0))
return c->l;
return NULL;
}
/* Does a packet appear to come directly to us from the rf originator */
static int
is_direct(struct stuff *s)
{
struct callsign_list *c;
int flag=0, digit, n1, n2;
if (s->in.ax_next_digi != 0) return 0;
c = calltab_entry(&s->in.ax_digi_call[0],&flag);
if (!c) /* Nothing we recognize or will deal with, but prob. direct */
return 1;
if ((!flag&C_IS_FLOOD) || (!flag&C_IS_FLOODN)) /* alias, yes we are direct */
return 1;
digit = ((s->in.ax_digi_call[0].ax25_call[c->floodlen]) & 0xFE)>>1;
if ((digit<'0') || digit > '9') return 0; /* Something is very wrong */
n1 = digit - 0x30;
n2 = (s->in.ax_digi_call[0].ax25_call[ALEN]&SSID)>>1;
if (n2<n1) /* There's probably been a hop already */
return 0;
return 1;
}
static int
rx_to_me(struct stuff *s)
{
int result = 0;
struct interface_list *l = intf_of(&s->in.ax_to_call);
if (l) {
result = 1;
++s->i->stats.rx_ign;
}
if (Verbose) {
fprintf(stderr,"packet is%saddressed to me.\n",result?" ":" not ");
}
return result;
}
static int
rx_bud_deny(struct stuff *s)
{
int permit = 0;
permit = budlist_permit(s);
if (Verbose) {
fprintf(stderr,"packet is %s by budlist.\n",permit?"permitted":"denied");
}
return !permit; /* true if denied */
}
static int
rx_from_me(struct stuff *s)
{
int n,result = 0;
struct interface *iface;
for (n = 0, iface = Intf; n < N_intf; n++, iface++) {
if (ax25_cmp(&MYCALL(n),&s->in.ax_from_call) == 0) {
result=1;
++s->i->stats.rx_ign;
}
}
if (Verbose) {
fprintf(stderr,"packet is%sfrom me.\n",result?" ":" not ");
}
return result;
}
/* Does a direct inbound packet request more hops than the limit we set */
static int
rx_limit(struct stuff *s)
{
int sum = 0, result = 0, flag=0;
struct callsign_list *c;
if (!Maxhops) return result;
if (!(is_direct(s))) return result;
c = calltab_entry(&s->in.ax_digi_call[0],&flag);
if (!c) return result;
if (Verbose) {
fprintf(stderr, "Direct input packet with known via call\n");
}
for (int n=0 ; n < s->in.ax_n_digis; n++) {
flag = 0;
c = calltab_entry(&s->in.ax_digi_call[n],&flag);
if (!c) {
sum++;
if (Verbose) {
fprintf(stderr, "Call, non flood running total %i\n", ax25_ntoa_pretty(&s->in.ax_digi_call[n]), sum);
}
continue;
}
if ((flag&C_IS_FLOOD) && (flag&C_IS_FLOODN)) {
sum += (s->in.ax_digi_call[n].ax25_call[ALEN]&SSID)>>1;
if (Verbose) {
fprintf(stderr, "Call %s, flag %i, flood running total %i\n", ax25_ntoa_pretty(&s->in.ax_digi_call[n]), flag, sum);
}
} else {
sum++;
if (Verbose) {
fprintf(stderr, "Call %s, flag %i, non floodn running total %i\n", ax25_ntoa_pretty(&s->in.ax_digi_call[n]), flag, sum);
}
}
}
if (Verbose) {
fprintf(stderr,"Total hops expected for pkt being received directly %i limit %i\n",
sum, Maxhops);
}
if (sum > Maxhops) {
++s->i->stats.rx_ign;
result = 1;
}
if (Verbose) {
fprintf(stderr,"Direct input packet%sdropped for too many digipeats requested\n",
result?" ":" not ");
}
return result;
}
/*
* SSID path selection only applies if:
* 1. SSID digipeating is enabled.
* 2. To-call's SSID must be non zero: this is the route.
* 3. Digipeater path must be empty.
* Count up mic_e packets while we are here.
*/
static int
rx_ssid(struct stuff *s)
{
int ssid, mic_e, result = 0;
mic_e = (*(s->cp) == 0x60 || *(s->cp) == 0x27
|| *(s->cp) == 0x1c || *(s->cp) == 0x1d);
if (mic_e)
++s->i->stats.rx_mic;
if (Verbose) {
fprintf(stderr,"%s mic_e...\n",mic_e?"is":"is not");
}
if (Digi_SSID && s->in.ax_n_digis == 0
&& (ssid = (s->in.ax_to_call.ax25_call[ALEN]&SSID)>>1)
&& s->len > 0) {
if (Verbose) {
fprintf(stderr,"Got an SSID route for path %d.\n",ssid);
}
++s->i->stats.rx_ssid;
s->out.ax_from_call = s->in.ax_from_call;
s->out.ax_to_call = s->in.ax_to_call;
s->out.ax_to_call.ax25_call[ALEN]&=~SSID; /* zero the SSID */
s->out.ax_type = s->in.ax_type;
s->out.ax_pid = s->in.ax_pid;
if (ssid <= 7) { /* omnidirectional */
if (N_floods || ssid == 0) { /* in a flooding network? */
s->out.ax_n_digis = 1;
s->out.ax_digi_call[0] = FLOODCALL;
s->out.ax_digi_call[0].ax25_call[FLOODLEN] = (ssid+'0') << 1;
s->out.ax_digi_call[0].ax25_call[ALEN] |= SSID&(ssid<<1);
if (Verbose) {
fprintf(stderr,"Flooding: setting path to %s\n",
ax25_ntoa_pretty(&s->out.ax_digi_call[0]));
}
} else { /* not in a flooding network */
int startat,i;
if (ssid < 4) { /* RTFM for why this is */
startat = 0; /* starting point in digipath */
s->out.ax_n_digis = ssid; /* number of digis from there. */
} else {
startat = 3;
s->out.ax_n_digis = ssid-3;
}
if (Verbose) {
fprintf(stderr,"Non-flooding: converting SSID WIDE-%d path to DIGI[%d:%d]\n",
ssid,startat,startat+s->out.ax_n_digis-1);
}
/* fill in the digipeater list */
for (i = 0; i < s->out.ax_n_digis; i++,startat++) {
s->out.ax_digi_call[i] = Digipath[startat];
}
} /* flooding/non-flooding network */
} else { /* SSID > 7 is directional */
int j;
if (Verbose) {
fprintf(stderr,"setting path to %c UNPROTO%s\n",
Dirs[ssid&3],(ssid&4)?" + WIDE":"");
}
s->out.ax_n_digis = Path[ssid&3].fsa_ax25.sax25_ndigis;
for (j = 0; j <= s->out.ax_n_digis; j++)
s->out.ax_digi_call[j] = Path[ssid&3].fsa_digipeater[j];
if (ssid&4) { /* directional + wide call */
s->out.ax_digi_call[s->out.ax_n_digis++] = Widecall;
}
}
++s->i->stats.ssid;
if (xmit(s,NULL) < 0)
perror("xmit");
result = 1;
}
if (Verbose) {
fprintf(stderr,"did%srequire destination SSID handling.\n",
result?" ":" not ");
}
return result;
}
/* see if special WIDEn-n & TRACEn-n handling applies. */
static int
rx_flood(struct stuff *s)
{
int i,wide,thisflags,result=0;
struct callsign_list *c;
/* FLOODn-n algorithm: next digi with non-zero ssid callsign */
c = calltab_entry(&s->in.ax_digi_call[s->in.ax_next_digi],&thisflags);
if (c && thisflags&C_IS_FLOOD
&& (wide = (s->in.ax_digi_call[s->in.ax_next_digi].ax25_call[ALEN]&SSID)>>1)) {
if (Verbose) {
fprintf(stderr,"Got a flooding %s route.\n",
ax25_ntoa_pretty(&s->in.ax_digi_call[s->in.ax_next_digi]));
}
if ((Max_wide_n) && (wide > Max_wide_n)) {
wide = Max_wide_n;
if (Verbose) fprintf(stderr, "Flooding hopcount limited to %d\n", wide);
}
/* flooding algorithm always kills dupes. If kill_dupes
option was not selected, then do the dupe checking here */
if (!Kill_dupes && dupe_packet(&s->in,s->cp,s->len)) {
if (Verbose) {
fprintf(stderr,"flood packet is a dupe\n");
}
return 1;
}
/* decrement the flood counter (SSID) */
s->out = s->in; /* copy the input header */
s->out.ax_digi_call[s->out.ax_next_digi].ax25_call[ALEN] &= ~SSID;
s->out.ax_digi_call[s->out.ax_next_digi].ax25_call[ALEN] |= ((--wide) << 1)&SSID;
/* Handle FLOOD-1 -> FLOOD-0 case */
if (wide == 0)
s->out.ax_digi_call[s->out.ax_next_digi].ax25_call[ALEN] |= REPEATED;
/* TRACEn-n: insert dummy mycall in front: xmit will put the real one in */
if ((thisflags&(C_IS_FLOODN|C_IS_TRACE)) == (C_IS_FLOODN|C_IS_TRACE)) {
int n;
if (s->out.ax_n_digis >= AX25_MAX_DIGIS) {
fprintf(stderr,"%s: TRACEn-n list overflow. Last digi dropped.\n",
s->i->port);
s->out.ax_n_digis--;
}
/* shift remaining digis right one slot to make room */
for (n = s->out.ax_n_digis; n >= s->out.ax_next_digi; n--) {
s->out.ax_digi_call[n] = s->out.ax_digi_call[n-1];
}
/* then stuff in a dummy "TRACE" and mark it repeated */
s->out.ax_digi_call[s->out.ax_next_digi] = Trace_dummy;
s->out.ax_digi_call[s->out.ax_next_digi].ax25_call[ALEN] |= REPEATED;
s->out.ax_n_digis++;
}
if (Verbose) {
fprintf(stderr,"Rewriting it as %s:\n",
ax25_ntoa_pretty(&s->out.ax_digi_call[s->out.ax_next_digi]));
}
++s->i->stats.flood;
if (xmit(s,NULL) < 0)
perror("xmit");
result = 1;
}
if (Verbose) {
fprintf(stderr,"Did %s require special flooding handling.\n",
result?" ":" not ");
}
return result;
}
/* see if conventional digipeat handling applies. */
static int
rx_digi(struct stuff *s)
{
int j, result = 0;
struct interface_list *l;
/* conventional: see if the next digipeater matches one of my calls */
if (l = intf_of(&s->in.ax_digi_call[s->in.ax_next_digi])) {
/* a packet for me to digipeat */
if (Verbose) {
fprintf(stderr,"Got a conventional digipeat.\n");
}
s->out = s->in; /* copy input list to output unmodified */
s->out.ax_digi_call[s->out.ax_next_digi].ax25_call[ALEN] |= REPEATED;
++s->i->stats.digi;
if (xmit(s,NULL) < 0)
perror("xmit");
result = 1;
}
if (Verbose) {
fprintf(stderr,"Did%srequire conventional digipeat handling.\n",
result?" ":" not ");
}
return result;
}
static void
add_text(op,oleft,text,len,tag,taglen)
unsigned char **op;
int *oleft;
u_char *text;
int len;
char *tag;
int taglen;
{
if ((*oleft -= len) > 0) {
bcopy(text,*op,len); /* copy the text */
*op += len;
}
if (taglen && tag && (*oleft -= taglen) > 0) {
bcopy(tag,*op,taglen); /* and tack on the tag */
*op += taglen;
}
}
/* watch out for overflow when adding mycall and/or wide */
static int
unproto(calls,str) /* parse a via path into a calls struct */
struct full_sockaddr_ax25 *calls;
char *str;
{
char buf[200];
bzero(calls, sizeof(*calls));
sprintf(buf,"dummy via %s",str);
return ax25_aton(buf,calls);
}
static int
parsecalls(calls,ncalls,str) /* parse a via path into a calls struct */
ax25_address *calls;
int ncalls; /* max number */
char *str;
{
char *cp;
int i;
bzero(calls,ncalls*sizeof(*calls));
cp = strtok(str," \t\n,");
for (i = 0; cp && i < ncalls; i++) {
if (ax25_aton_entry(cp,calls[i].ax25_call) < 0)
return -1;
cp = strtok(NULL," \t\n,");
}
return i;
}
static void
print_it(FILE *f,
struct ax_calls *calls,
u_char *data,
int len)
{
int j;
char asc_from[12],asc_to[12];
if (f == NULL)
return;
strncpy(asc_to,ax25_ntoa_pretty(&calls->ax_to_call),sizeof(asc_to));
strncpy(asc_from,ax25_ntoa_pretty(&calls->ax_from_call),sizeof(asc_from));
fprintf(f,"%s>%s",asc_from,asc_to);
for (j = 0; j < calls->ax_n_digis; j++) {
fprintf(f,",%s%s",ax25_ntoa_pretty(&calls->ax_digi_call[j]),
(calls->ax_digi_call[j].ax25_call[ALEN]&REPEATED
&& (j == calls->ax_next_digi-1))?"*":"");
}
fprintf(f,":%.*s\n",len,data);
}
/*
* packet reformatter
* depending on options flags on transmit interface, perform reformatting
* of the payload. Current methods include mic_E expansion, X1J4
* stripping of the the "TheNet X1J4 (alias)" prefix. Future methods
* to include GPS/APRS position compression, expansion, etc.