forked from PhirePhly/aprx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
filter.c
2334 lines (1937 loc) · 58.9 KB
/
filter.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
/********************************************************************
* APRX -- 2nd generation APRS-i-gate with *
* minimal requirement of esoteric facilities or *
* libraries of any kind beyond UNIX system libc. *
* *
* (c) Matti Aarnio - OH2MQK, 2007-2014 *
* *
********************************************************************/
/*
* Some parts of this code are copied from:
*
* aprsc
*
* (c) Matti Aarnio, OH2MQK, <[email protected]>
*
* This program is licensed under the BSD license, which can be found
* in the file LICENSE.
*
*/
#include "aprx.h"
#include "cellmalloc.h"
#include "historydb.h"
#include "keyhash.h"
/*
See: http://www.aprs-is.net/javaprssrvr/javaprsfilter.htm
a/latN/lonW/latS/lonE Area filter
b/call1/call2... Budlist filter (*)
d/digi1/digi2... Digipeater filter (*)
e/call1/call1/... Entry station filter (*)
f/call/dist Friend Range filter
g/call1/call2.. Group Messaging filter (*)
m/dist My Range filter
o/obj1/obj2... Object filter (*)
p/aa/bb/cc... Prefix filter
q/con/ana q Contruct filter
r/lat/lon/dist Range filter
s/pri/alt/over Symbol filter
t/poimntqsu3*c Type filter
t/poimntqsu3*c/call/km Type filter
u/unproto1/unproto2/.. Unproto filter (*)
Sample usage frequencies (out of entire APRS-IS):
23.7 a/ <-- Optimize!
9.2 b/ <-- Optimize?
1.4 d/
0.2 e/
2.2 f/
20.9 m/ <-- Optimize!
0.2 o/
14.4 p/ <-- Optimize!
0.0 pk
0.0 pm
0.4 q/
19.0 r/ <-- Optimize!
0.1 s_
1.6 s/
6.6 t/
0.1 u/
(*) = wild-card supported
Undocumented at above web-page, but apparent behaviour is:
- Everything not explicitely stated to be case sensitive is
case INSENSITIVE
- Minus-prefixes on filters behave as is there are two sets of
filters:
- filters without minus-prefixes add on approved set, and all
those without are evaluated at first
- filters with minus-prefixes are evaluated afterwards to drop
selections after the additive filter has been evaluated
- Our current behaviour is: "evaluate everything in entry order,
stop at first match", which enables filters like:
p/OH2R -p/OH2 p/OH
while javAPRSSrvr filter adjunct behaves like the request is:
-p/OH2 p/OH
that is, OH2R** stations are not passed thru.
*/
/* FIXME: What exactly is the meaning of negation on the pattern ?
** Match as a failure to match, and stop searching ?
** Something filter dependent ?
**
** javAPRSSrvr Filter Adjunct manual tells:
#14 Exclusion filter
All the above filters also support exclusion. Be prefixing the above filters with a
dash the result will be the opposite. Any packet that match the exclusion filter will
NOT pass. The exclusion filters will be processed first so if there is a match for an
exclusion then the packet is not passed no matter any other filter definitions.
*/
#define WildCard 0x80 /* it is wild-carded prefix string */
#define NegationFlag 0x40 /* */
#define LengthMask 0x0F /* only low 4 bits encode length */
/* values above are chosen for 4 byte alignment.. */
struct filter_refcallsign_t {
char callsign[CALLSIGNLEN_MAX+1]; /* size: 10.. */
int8_t reflen; /* length and flags */
};
struct filter_head_t {
struct filter_t *next;
const char *text; /* filter text as is */
float f_latN, f_lonE;
union {
float f_latS; /* for A filter */
float f_coslat; /* for R filter */
} u1;
union {
float f_lonW; /* for A filter */
float f_dist; /* for R filter */
} u2;
time_t hist_age;
char type; /* 1 char */
int16_t negation; /* boolean flag */
union {
int16_t numnames; /* used as named, and as cache validity flag */
int16_t len1s; /* or len1 of s-filter */
} u3;
union {
int16_t bitflags; /* used as bit-set on T_*** enumerations */
int16_t len1; /* or as len2 of s-filter */
} u4;
union {
struct { int16_t len2s, len2, len3s, len3; } lens; /* of s-filter */
/* for cases where there is only one.. */
struct filter_refcallsign_t refcallsign;
/* malloc()ed array, alignment important! */
struct filter_refcallsign_t *refcallsigns;
} u5;
};
struct filter_t {
struct filter_head_t h;
#define FILT_TEXTBUFSIZE (508-sizeof(struct filter_head_t))
char textbuf[FILT_TEXTBUFSIZE];
};
#define QC_C 0x001 /* Q-filter flag bits */
#define QC_X 0x002
#define QC_U 0x004
#define QC_o 0x008
#define QC_O 0x010
#define QC_S 0x020
#define QC_r 0x040
#define QC_R 0x080
#define QC_Z 0x100
#define QC_I 0x200
#define QC_AnalyticsI 0x800
/*
// For q-filter analytics: entrycall igate filter database
struct filter_entrycall_t {
struct filter_entrycall_t *next;
time_t expirytime;
uint32_t hash;
int len;
char callsign[CALLSIGNLEN_MAX+1];
};
*/
/*
struct filter_wx_t {
struct filter_wx_t *next;
time_t expirytime;
uint32_t hash;
int len;
char callsign[CALLSIGNLEN_MAX+1];
};
*/
typedef enum {
MatchExact,
MatchPrefix,
MatchWild
} MatchEnum;
/*
#define FILTER_ENTRYCALL_HASHSIZE 2048 // Around 500-600 in db, this looks
// for collision free result..
int filter_entrycall_maxage = 60*60; // 1 hour, default. Validity on
// lookups: 5 minutes less..
int filter_entrycall_cellgauge;
struct filter_entrycall_t *filter_entrycall_hash[FILTER_ENTRYCALL_HASHSIZE];
*/
/*
#define FILTER_WX_HASHSIZE 1024 // Around 300-400 in db, this looks
// for collision free result..
int filter_wx_maxage = 60*60; // 1 hour, default. Validity on
// lookups: 5 minutes less..
int filter_wx_cellgauge;
struct filter_wx_t *filter_wx_hash[FILTER_WX_HASHSIZE];
*/
#ifndef _FOR_VALGRIND_
cellarena_t *filter_cells;
//cellarena_t *filter_entrycall_cells;
//cellarena_t *filter_wx_cells;
#endif
int hist_lookup_interval = 20; /* FIXME: Configurable: Cache historydb
position lookups this much seconds on
each filter entry referring to some
fixed callsign (f,m,t) */
float filter_lat2rad(float lat)
{
return (lat * (M_PI / 180.0));
}
float filter_lon2rad(float lon)
{
return (lon * (M_PI / 180.0));
}
const int filter_cellsize = sizeof(struct filter_t);
const int filter_cellalign = __alignof__(struct filter_t);
void filter_init(void)
{
#ifndef _FOR_VALGRIND_
/* A _few_... */
filter_cells = cellinit( "filter",
filter_cellsize,
filter_cellalign,
CELLMALLOC_POLICY_LIFO,
4 /* 4 kB at the time,
should be enough in all cases.. */,
0 /* minfree */ );
/* printf("filter: sizeof=%d alignof=%d\n",
sizeof(struct filter_t),__alignof__(struct filter_t)); */
/*
// Couple thousand
filter_entrycall_cells = cellinit( "entrycall",
sizeof(struct filter_entrycall_t),
__alignof__(struct filter_entrycall_t),
CELLMALLOC_POLICY_FIFO,
32, // 32 kB at the time,
0 // minfree
);
*/
/*
// Under 1 thousand..
filter_wx_cells = cellinit( "wxcalls",
sizeof(struct filter_wx_t),
__alignof__(struct filter_wx_t),
CELLMALLOC_POLICY_FIFO,
32, // 32 kB at the time
0 // minfree
);
*/
#endif
}
#if 0
static void filter_entrycall_free(struct filter_entrycall_t *f)
{
#ifndef _FOR_VALGRIND_
cellfree( filter_entrycall_cells, f );
#else
free(f);
#endif
-- filter_entrycall_cellgauge;
}
/*
* filter_entrycall_insert() is for support of q//i filters.
* That is, "pass on any message that has traversed thru entry
* igate which has identified itself with qAr or qAR. Not all
* messages traversed thru such gate will have those same q-cons
* values, thus this database keeps info about entry igate that
* have shown such capability in recent past.
*
* This must be called by the incoming_parse() in every case
* (or at least when qcons is either 'r' or 'R'.)
*
* The key has no guaranteed alignment, no way to play tricks
* with gcc builtin optimizers.
*/
int filter_entrycall_insert(struct pbuf_t *pb)
{
struct filter_entrycall_t *f, **fp, *f2;
/* OK, pre-parsing produced accepted result */
uint32_t hash;
int idx, keylen;
const char qcons = pb->qconst_start[2];
const char *key = pb->qconst_start+4;
char uckey[CALLSIGNLEN_MAX+1];
for (keylen = 0; keylen < CALLSIGNLEN_MAX; ++keylen) {
int c = key[keylen];
if (c == ',' || c == ':')
break;
if ('a' <= c && c <= 'z')
c -= ('a' - 'A');
uckey[keylen] = c;
uckey[keylen+1] = 0;
}
if ((key[keylen] != ',' && key[keylen] != ':') || keylen < CALLSIGNLEN_MIN)
return 0; /* Bad entry-station callsign */
pb->entrycall_len = keylen; // FIXME: should be in incoming parser...
/* We insert only those that have Q-Constructs of qAR or qAr */
if (qcons != 'r' && qcons != 'R') return 0;
hash = keyhash(uckey, keylen, 0);
idx = (hash ^ (hash >> 11) ^ (hash >> 22) ) % FILTER_ENTRYCALL_HASHSIZE; /* Fold the hashbits.. */
fp = &filter_entrycall_hash[idx];
f2 = NULL;
while (( f = *fp )) {
if ( f->hash == hash ) {
if (f->len == keylen) {
int cmp = strncasecmp(f->callsign, uckey, keylen);
if (cmp == 0) { /* Have key match */
f->expirytime = tick.tv_sec + filter_entrycall_maxage;
f2 = f;
break;
}
}
}
/* No match at all, advance the pointer.. */
fp = &(f -> next);
}
if (!f2) {
/* Allocate and insert into hash table */
fp = &filter_entrycall_hash[idx];
#ifndef _FOR_VALGRIND_
f = cellmalloc(filter_entrycall_cells);
#else
f = calloc(1, sizeof(*f));
#endif
if (f) {
f->next = *fp;
f->expirytime = tick.tv_sec + filter_entrycall_maxage;
f->hash = hash;
f->len = keylen;
memcpy(f->callsign, uckey, keylen);
memset(f->callsign+keylen, 0, sizeof(f->callsign)-keylen);
*fp = f2 = f;
++ filter_entrycall_cellgauge;
}
}
return (f2 != NULL);
}
/*
* filter_entrycall_lookup() is for support of q//i filters.
* That is, "pass on any message that has traversed thru entry
* igate which has identified itself with qAr or qAR. Not all
* messages traversed thru such gate will have those same q-cons
* values, thus this keeps database about entry servers that have
* shown such capability in recent past.
*
* The key has no guaranteed alignment, no way to play tricks
* with gcc builtin optimizers.
*/
static int filter_entrycall_lookup(const struct pbuf_t *pb)
{
struct filter_entrycall_t *f, **fp, *f2;
const char *key = pb->qconst_start+4;
const int keylen = pb->entrycall_len;
uint32_t hash = keyhashuc(key, keylen, 0);
int idx = ( hash ^ (hash >> 11) ^ (hash >> 22) ) % FILTER_ENTRYCALL_HASHSIZE; /* fold the hashbits.. */
f2 = NULL;
fp = &filter_entrycall_hash[idx];
while (( f = *fp )) {
if ( f->hash == hash ) {
if (f->len == keylen) {
int rc = strncasecmp(f->callsign, key, keylen);
if (rc == 0) { /* Have key match, see if it is
still valid entry ? */
if ((f->expirytime - (tick.tv_sec - 60)) < 0) {
f2 = f;
break;
}
}
}
}
/* No match at all, advance the pointer.. */
fp = &(f -> next);
}
return (f2 != NULL);
}
/*
* The filter_entrycall_cleanup() does purge old entries
* out of the database. Run about once a minute.
*/
void filter_entrycall_cleanup(void)
{
int k, cleancount = 0;
struct filter_entrycall_t *f, **fp;
for (k = 0; k < FILTER_ENTRYCALL_HASHSIZE; ++k) {
fp = & filter_entrycall_hash[k];
while (( f = *fp )) {
/* Did it expire ? */
if ((f->expirytime - tick.tv_sec) <= 0) {
*fp = f->next;
f->next = NULL;
filter_entrycall_free(f);
++cleancount;
continue;
}
/* No purge, advance the pointer.. */
fp = &(f -> next);
}
}
// hlog( LOG_DEBUG, "filter_entrycall_cleanup() removed %d entries, count now: %ld",
// cleancount, filter_entrycall_cellgauge );
}
/*
* The filter_entrycall_atend() does purge all entries
* out of the database. Run at the exit of the program.
* This exists primarily to make valgrind happy...
*/
void filter_entrycall_atend(void)
{
int k;
struct filter_entrycall_t *f, **fp;
for (k = 0; k < FILTER_ENTRYCALL_HASHSIZE; ++k) {
fp = & filter_entrycall_hash[k];
while (( f = *fp )) {
*fp = f->next;
f->next = NULL;
filter_entrycall_free(f);
}
}
}
void filter_entrycall_dump(FILE *fp)
{
int k;
struct filter_entrycall_t *f;
for (k = 0; k < FILTER_ENTRYCALL_HASHSIZE; ++k) {
f = filter_entrycall_hash[k];
for ( ; f; f = f->next ) {
fprintf( fp, "%ld\t%s\n",
(long)f->expirytime, f->callsign );
}
}
}
#endif
/* ================================================================ */
#if 0
static void filter_wx_free(struct filter_wx_t *f)
{
#ifndef _FOR_VALGRIND_
cellfree( filter_wx_cells, f );
#else
free(f);
#endif
--filter_wx_cellgauge;
}
/*
* The filter_wx_insert() does lookup key storage for problem of:
*
* Positionless T_WX packets want also position packets on output filters.
*/
int filter_wx_insert(struct pbuf_t *pb)
{
struct filter_wx_t *f, **fp, *f2;
/* OK, pre-parsing produced accepted result */
const char *key = pb->data;
const int keylen = pb->srccall_end - key;
uint32_t hash;
int idx;
char uckey[CALLSIGNLEN_MAX+1];
/* If it is not a WX packet without position, we are not intrerested */
if (!((pb->packettype & T_WX) && !(pb->flags & F_HASPOS)))
return 0;
for (idx = 0; idx <= keylen && idx < CALLSIGNLEN_MAX; ++idx) {
int c = key[idx];
if (c == ',' || c == ':')
break;
if ('a' <= c && c <= 'z')
c -= ('a' - 'A');
uckey[idx] = c;
uckey[idx+1] = 0;
}
hash = keyhash(uckey, keylen, 0);
idx = ( hash ^ (hash >> 10) ^ (hash >> 20) ) % FILTER_WX_HASHSIZE; /* fold the hashbits.. */
fp = &filter_wx_hash[idx];
f2 = NULL;
while (( f = *fp )) {
if ( f->hash == hash ) {
if (f->len == keylen) {
int cmp = memcmp(f->callsign, uckey, keylen);
if (cmp == 0) { /* Have key match */
f->expirytime = tick.tv_sec + filter_wx_maxage;
f2 = f;
break;
}
}
}
/* No match at all, advance the pointer.. */
fp = &(f -> next);
}
if (!f2) {
/* Allocate and insert into hash table */
fp = &filter_wx_hash[idx];
#ifndef _FOR_VALGRIND_
f = cellmalloc(filter_wx_cells);
#else
f = calloc(1, sizeof(*f));
#endif
++filter_wx_cellgauge;
if (f) {
f->next = *fp;
f->expirytime = tick.tv_sec + filter_wx_maxage;
f->hash = hash;
f->len = keylen;
memcpy(f->callsign, key, keylen);
memset(f->callsign+keylen, 0, sizeof(f->callsign)-keylen);
*fp = f2 = f;
}
}
return 0;
}
static int filter_wx_lookup(const struct pbuf_t *pb)
{
struct filter_wx_t *f, **fp, *f2;
const char *key = pb->data;
const int keylen = pb->srccall_end - key;
uint32_t hash = keyhashuc(key, keylen, 0);
int idx = ( hash ^ (hash >> 10) ^ (hash >> 20) ) % FILTER_WX_HASHSIZE; /* fold the hashbits.. */
f2 = NULL;
fp = &filter_wx_hash[idx];
while (( f = *fp )) {
if ( f->hash == hash ) {
if (f->len == keylen) {
int rc = strncasecmp(f->callsign, key, keylen);
if (rc == 0) { /* Have key match, see if it is
still valid entry ? */
if ((f->expirytime - (tick.tv_sec - 60)) < 0) {
f2 = f;
break;
}
}
}
}
/* No match at all, advance the pointer.. */
fp = &(f -> next);
}
return (f2 != NULL);
}
/*
* The filter_wx_cleanup() does purge old entries
* out of the database. Run about once a minute.
*/
void filter_wx_cleanup(void)
{
int k, cleancount = 0;
struct filter_wx_t *f, **fp;
for (k = 0; k < FILTER_WX_HASHSIZE; ++k) {
fp = & filter_wx_hash[k];
while (( f = *fp )) {
/* Did it expire ? */
if ((f->expirytime - tick.tv_sec) <= 0) {
*fp = f->next;
f->next = NULL;
filter_wx_free(f);
++cleancount;
continue;
}
/* No purge, advance the pointer.. */
fp = &(f -> next);
}
}
// hlog( LOG_DEBUG, "filter_wx_cleanup() removed %d entries, count now: %ld",
// cleancount, filter_wx_cellgauge );
}
/*
* The filter_wx_atend() does purge all entries
* out of the database. Run at the exit of the program.
* This exists primarily to make valgrind happy...
*/
void filter_wx_atend(void)
{
int k;
struct filter_wx_t *f, **fp;
for (k = 0; k < FILTER_WX_HASHSIZE; ++k) {
fp = & filter_wx_hash[k];
while (( f = *fp )) {
*fp = f->next;
f->next = NULL;
filter_wx_free(f);
}
}
}
void filter_wx_dump(FILE *fp)
{
int k;
struct filter_wx_t *f;
for (k = 0; k < FILTER_WX_HASHSIZE; ++k) {
f = filter_wx_hash[k];
for ( ; f; f = f->next ) {
fprintf( fp, "%ld\t%s\n",
(long)f->expirytime, f->callsign );
}
}
}
#endif
/* ================================================================ */
void filter_preprocess_dupefilter(struct pbuf_t *pbuf)
{
#if 0
filter_entrycall_insert(pbuf);
filter_wx_insert(pbuf);
#endif
}
void filter_postprocess_dupefilter(struct pbuf_t *pbuf, historydb_t *historydb)
{
/*
* If there is no position at this packet from earlier
* processing, try now to find one by the callsign of
* the packet sender.
*
*/
#ifndef DISABLE_IGATE
if (!(pbuf->flags & F_HASPOS)) {
history_cell_t *hist;
hist = historydb_lookup(historydb, pbuf->srcname, pbuf->srcname_len);
// hlog( LOG_DEBUG, "postprocess_dupefilter: no pos, looking up '%.*s', rc=%d",
// pbuf->srcname_len, pbuf->srcname, rc );
if (hist != NULL) {
pbuf->lat = hist->lat;
pbuf->lng = hist->lon;
pbuf->cos_lat = hist->coslat;
pbuf->flags |= F_HASPOS;
}
}
#endif
}
/* ================================================================ */
/*
* filter_match_on_callsignset() matches prefixes, or exact keys
* on filters of types: b, d, e, o, p, u
* ('p' and 'b' need OPTIMIZATION - others get it for free)
*
*/
static int filter_match_on_callsignset(struct filter_refcallsign_t *ref, int keylen, struct filter_t *f, const MatchEnum wildok)
{
int i;
struct filter_refcallsign_t *r = f->h.u5.refcallsigns;
const char *r1 = (const void*)ref->callsign;
if (debug) printf(" filter_match_on_callsignset(ref='%s', keylen=%d, filter='%s')\n", ref->callsign, keylen, f->h.text);
for (i = 0; i < f->h.u3.numnames; ++i) {
const int reflen = r[i].reflen;
const int len = reflen & LengthMask;
const char *r2 = (const void*)r[i].callsign;
if (debug)printf(" .. reflen=0x%02x r2='%s'\n", reflen & 0xFF, r2);
switch (wildok) {
case MatchExact:
if (len != keylen)
continue; /* no match */
/* length OK, compare content */
if (strncasecmp( r1, r2, len ) != 0) continue;
/* So it was an exact match
** Precisely speaking.. we should check that there is
** no WildCard flag, or such. But then this match
** method should not be used if parser finds any such.
*/
return ( reflen & NegationFlag ? 2 : 1 );
break;
case MatchPrefix:
if (len > keylen || !len) {
/* reference string length is longer than our key */
continue;
}
if (strncasecmp( r1, r2, len ) != 0) continue;
return ( reflen & NegationFlag ? 2 : 1 );
break;
case MatchWild:
if (len > keylen || !len) {
/* reference string length is longer than our key */
continue;
}
if (strncasecmp( r1, r2, len ) != 0) continue;
if (reflen & WildCard)
return ( reflen & NegationFlag ? 2 : 1 );
if (len == keylen)
return ( reflen & NegationFlag ? 2 : 1 );
break;
default:
break;
}
}
return 0; /* no match */
}
/*
* filter_parse_one_callsignset() collects multiple callsigns
* on filters of types: b, d, e, o, p, u
*
* If previous filter was of same type as this one, that one's refbuf is extended.
*/
static int filter_parse_one_callsignset(struct filter_t **ffp, struct filter_t *f0, const char *filt0, MatchEnum wildok)
{
char prefixbuf[CALLSIGNLEN_MAX+1];
char *k;
const char *p;
int i, refcount, wildcard;
int refmax = 0, extend = 0;
struct filter_refcallsign_t *refbuf;
struct filter_t *ff = *ffp;
p = filt0;
if (*p == '-') ++p;
// Skip the first '/'
while (*p && *p != '/') ++p;
if (*p == '/') ++p;
/* count the number of prefixes in there.. */
while (*p) {
if (*p) ++refmax;
while (*p && *p != '/') ++p;
if (*p == '/') ++p;
}
if (refmax == 0) {
printf("Filter definition of '%s' has no prefixes defined.\n", filt0);
return -1; /* No prefixes ?? */
}
if (ff && ff->h.type == f0->h.type) { /* SAME TYPE,
extend previous record! */
extend = 1;
refcount = ff->h.u3.numnames + refmax;
refbuf = realloc(ff->h.u5.refcallsigns, sizeof(*refbuf) * refcount);
ff->h.u5.refcallsigns = refbuf;
refcount = ff->h.u3.numnames;
} else {
refbuf = calloc(1, sizeof(*refbuf)*refmax);
refcount = 0;
f0->h.u5.refcallsigns = refbuf;
f0->h.u3.numnames = 0;
}
p = filt0;
if (*p == '-') ++p;
// Skip the first '/'
while (*p && *p != '/') ++p;
if (*p == '/') ++p;
/* hlog(LOG_DEBUG, "p-filter: '%s' vs. '%s'", p, keybuf); */
while (*p) {
k = prefixbuf;
memset(prefixbuf, 0, sizeof(prefixbuf));
i = 0;
wildcard = 0;
while (*p != 0 && *p != '/') {
int c = *p++;
if (c == '*') {
wildcard = 1;
if (wildok != MatchWild) {
printf("Wild-card matching not permitted, yet filter definition says: '%s'\n", filt0);
return -1;
}
continue;
}
if (i < CALLSIGNLEN_MAX) {
*k++ = c;
++i;
} else {
printf("Too long callsign string: '%s' input: '%s'\n", prefixbuf, filt0);
return -1; // invalid input
}
}
*k = 0;
/* OK, we have one prefix part collected, scan source until next '/' */
if (*p != 0 && *p != '/') ++p;
if (*p == '/') ++p;
/* If there is more of patterns, the loop continues.. */
/* Store the refprefix */
memset(&refbuf[refcount], 0, sizeof(refbuf[refcount]));
memcpy(refbuf[refcount].callsign, prefixbuf, sizeof(refbuf[refcount].callsign));
refbuf[refcount].reflen = strlen(prefixbuf);
if (wildcard)
refbuf[refcount].reflen |= WildCard;
if (f0->h.negation)
refbuf[refcount].reflen |= NegationFlag;
++refcount;
}
f0->h.u3.numnames = refcount;
if (extend) {
char *s;
ff->h.u3.numnames = refcount;
i = strlen(ff->h.text) + strlen(filt0)+2;
if (i <= FILT_TEXTBUFSIZE) {
/* Fits in our built-in buffer block - like previous..
** Append on existing buffer
*/
s = ff->textbuf + strlen(ff->textbuf);
sprintf(s, " %s", filt0);
} else {
/* It does not fit anymore.. */
s = malloc(i); /* alloc a new one */
sprintf(s, "%s %s", p, filt0); /* .. and catenate. */
p = ff->h.text;
if (ff->h.text != ff->textbuf) /* possibly free old */
free((void*)p);
ff->h.text = s; /* store new */
}
}
/* If not extending existing filter item, let main parser do the finalizations */
return extend;
}
int filter_parse_one_s(struct filter_t *f0, struct filter_t **ffp, const char *filt0)
{
/* s/pri/alt/over Symbol filter
pri = symbols in primary table
alt = symbols in alternate table
over = overlay character (case sensitive)
For example:
s/-> This will pass all House and Car symbols (primary table)
s//# This will pass all Digi with or without overlay
s//#/T This will pass all Digi with overlay of capital T
About 10-15 s-filters in entire APRS-IS core at any given time.
Up to 520 invocations per second at peak.
*/
const char *s = filt0;
// struct filter_t *ff = *ffp;
int len1, len2, len3, len4, len5, len6;
if (*s == '-')
++s;
if (*s == 's' || *s == 'S')
++s;
if (*s != '/')
return -1;
++s;
len1 = len2 = len3 = len4 = len5 = len6 = 0;
while (1) {
len1 = s - filt0;
while (*s && *s != '/') ++s;
len2 = s - filt0;
f0->h.u3.len1s = len1;
f0->h.u4.len1 = len2 - len1;
f0->h.u5.lens.len2s = f0->h.u5.lens.len2 = f0->h.u5.lens.len3s = f0->h.u5.lens.len3 = 0;
if (!*s) break;
if (*s == '/') ++s;
len3 = s - filt0;
while (*s && *s != '/') ++s;
len4 = s - filt0;
f0->h.u5.lens.len2s = len3;
f0->h.u5.lens.len2 = len4 - len3;
if (!*s) break;
if (*s == '/') ++s;
len5 = s - filt0;
while (*s) ++s;
len6 = s - filt0;
f0->h.u5.lens.len3s = len5;
f0->h.u5.lens.len3 = len6 - len5;
break;
}
if ((len6-len5 > 0) && (len4-len3 == 0)) {
/* overlay but no secondary table.. */
return -1; /* bad parse */
}
#if 0
{
const char *s1 = filt0+len1, *s2 = filt0+len3, *s3 = filt0+len5;
int l1 = len2-len1, l2 = len4-len3, l3 = len6-len5;
// hlog( LOG_DEBUG, "parse s-filter: '%.*s' '%.*s' '%.*s'", l1, s1, l2, s2, l3, s3 );
}
#endif
return 0;
}
int filter_parse(struct filter_t **ffp, const char *filt)
{
struct filter_t f0;
int i;
const char *filt0 = filt;
const char *s;
char dummyc, dummy2;
struct filter_t *ff, *f;
ff = *ffp;
for ( ; ff && ff->h.next; ff = ff->h.next)
;
/* ff points to last so far accumulated filter,
if none were previously received, it is NULL.. */
memset(&f0, 0, sizeof(f0));
if (*filt == '-') {