File: digipeater.c

package info (click to toggle)
aprx 2.9.0+dfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 2,352 kB
  • sloc: ansic: 15,809; sh: 598; makefile: 160
file content (1876 lines) | stat: -rw-r--r-- 55,169 bytes parent folder | download | duplicates (2)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
/* **************************************************************** *
 *                                                                  *
 *  APRX -- 2nd generation APRS iGate and digi with                 *
 *          minimal requirement of esoteric facilities or           *
 *          libraries of any kind beyond UNIX system libc.          *
 *                                                                  *
 * (c) Matti Aarnio - OH2MQK,  2007-2014                            *
 *                                                                  *
 * **************************************************************** */

#include "aprx.h"

static int digi_count;
static struct digipeater **digis;

#define TOKENBUCKET_INTERVAL 5  // 5 seconds per refill.
                                // 60/5 part of "ratelimit" to be max
                                // that token bucket can be filled to.

static struct timeval tokenbucket_timer;

struct viastate {
	int hopsreq;
	int hopsdone;
	int tracereq;
	int tracedone;
	int digireq;
	int digidone;

	int fixthis;
	int fixall;
	int probably_heard_direct;
};

struct digistate {
	struct viastate v;

	int     ax25addrlen;
	uint8_t ax25addr[90]; // 70 for address, a bit more for "body"
};


#define AX25ADDRMAXLEN 70  // SRC + DEST + 8*VIA (7 bytes each)
#define AX25ADDRLEN  7
#define AX25HBIT  0x80
#define AX25ATERM 0x01

static char * tracewords[] = { "WIDE","TRACE","RELAY" };
static int tracewordlens[] = { 4, 5, 5 };
static const struct tracewide default_trace_param = {
	4, 4, 1, // maxreq, maxdone, is_trace
	3, // Count of tracewords defined above
	tracewords,
	tracewordlens
};
static char * widewords[] = { "WIDE","RELAY" };
static int widewordlens[] = { 4,5 };
static const struct tracewide default_wide_param = {
	4, 4, 0,
	2,
	widewords,
	widewordlens
};

static int  run_tokenbucket_timers(void);


float ratelimitmax     = 9999999.9;
float rateincrementmax = 9999999.9;


/*
 * regex_filter_add() -- adds configured regular expressions
 *                       into forbidden patterns list.
 * 
 * These are actually processed on TNC2 format text line, and not
 * AX.25 datastream per se.
 */
static int regex_filter_add(struct configfile *cf,
		struct digipeater_source *src,
		char *param1,
		char *str)
{
	int rc;
	int groupcode = -1;
	regex_t re, *rep;
	char errbuf[2000];

	if (strcmp(param1, "source") == 0) {
		groupcode = 0;
	} else if (strcmp(param1, "destination") == 0) {
		groupcode = 1;
	} else if (strcmp(param1, "via") == 0) {
		groupcode = 2;
	} else if (strcmp(param1, "data") == 0) {
		groupcode = 3;
	} else {
		printf("%s:%d ERROR: Bad RE target: '%s'  must be one of: source, destination, via\n",
				cf->name, cf->linenum, param1);
		return 1;
	}

	if (!*str) {
		printf("%s:%d ERROR: Expected RE pattern missing or a NUL string.\n",
				cf->name, cf->linenum);
		return 1;               /* Bad input.. */
	}

	param1 = str;
	str = config_SKIPTEXT(str, NULL); // Handle quoted string
	str = config_SKIPSPACE(str);

	memset(&re, 0, sizeof(re));
	rc = regcomp(&re, param1, REG_EXTENDED | REG_NOSUB);

	if (rc != 0) {          /* Something is bad.. */
		*errbuf = 0;
		regerror(rc, &re, errbuf, sizeof(errbuf));
		printf("%s:%d ERROR: Bad POSIX RE input, error: %s\n",
				cf->name, cf->linenum, errbuf);
		return 1;
	}

	/* param1 and str were processed successfully ... */

	rep = calloc(1,sizeof(*rep));
	*rep = re;

	switch (groupcode) {
		case 0:
			src->sourceregscount += 1;
			src->sourceregs = realloc(src->sourceregs,
						src->sourceregscount * sizeof(void *));
			src->sourceregs[src->sourceregscount - 1] = rep;
			break;
		case 1:
			src->destinationregscount += 1;
			src->destinationregs = realloc(src->destinationregs,
						src->destinationregscount * sizeof(void *));
			src->destinationregs[src->destinationregscount - 1] = rep;
			break;
		case 2:
			src->viaregscount += 1;
			src->viaregs = realloc(src->viaregs,
					src->viaregscount * sizeof(void *));
			src->viaregs[src->viaregscount - 1] = rep;
			break;
		case 3:
			src->dataregscount += 1;
			src->dataregs = realloc(src->dataregs,
						src->dataregscount * sizeof(void *));
			src->dataregs[src->dataregscount - 1] = rep;
			break;
	}
	return 0; // OK state
}


static int match_tracewide(const char *via, const struct tracewide *twp)
{
	int i;
	if (twp == NULL) return 0;

	for (i = 0; i < twp->nkeys; ++i) {
		// if (debug>2) printf(" match:'%s'",twp->keys[i]);
		if (memcmp(via, twp->keys[i], twp->keylens[i]) == 0) {
			return twp->keylens[i];
		}
	}
	return 0;
}

static int match_aliases(const char *via, struct aprx_interface *txif)
{
	int i;
	for (i = 0; i < txif->aliascount; ++i) {
		if (strcmp(via, txif->aliases[i]) == 0)
			return 1;
	}
	return 0;
}

// Counts the number of requested and consumed hops in an alias
// and adds those to the viastate->{digireq,digidone,tracereq,tracedone}.
// returns 1 on horrific failure
static int count_single_tnc2_tracewide(struct viastate *state,
		const char *viafield, const int istrace,
		const int matchlen, const int viaindex)
{
	const char *p = viafield + matchlen;
	const char reqc = p[0];
	const char c    = p[1];
	const char remc = p[2];
	int req, done;

	int hasHflag = (strchr(viafield,'*') != NULL);

	// Non-matched case, may have H-bit flag
	if (matchlen == 0) {
		req  = 1;
		done = hasHflag;
		if (viaindex == 2 && !hasHflag)
			state->probably_heard_direct = 1;
		// if (debug>1) printf(" a[req=%d,done=%d,trace=%d]",0,0,hasHflag);
		goto addtostate;
	}

	// Is the character following matched part not [1-7]
	if (!('1' <= reqc && reqc <= '7')) {
		// Not a digit, this is single matcher..
		req = 1;
		done = hasHflag;

		if (viaindex == 2 && !hasHflag)
			state->probably_heard_direct = 1;
		// if (debug>1) printf(" d[req=%d,done=%d]",1,hasHflag);
		goto addtostate;
	}

	req = reqc - '0';

	if (c == '*' && remc == 0) { // WIDE1*
		done = req;
		// if (debug>1) printf(" e[req=%d,done=%d]",req,req);
		goto addtostate;
	}
	if (c == 0) { // Bogus WIDE1 - uidigi puts these out.
		state->fixthis = 1;
		done = req;
		// if (debug>1) printf(" E[req=%d,done=%d]",req,req);
		goto addtostate;
	}
	// Not WIDE1-
	if (c != '-') {
		req = 1;
		done = hasHflag;
		// if (debug>1) printf(" f[req=%d,done=%d]",1,hasHflag);
		goto addtostate;
	}

	// OK, it is "WIDEn-" plus "N"
	if ('0' <= remc  && remc <= '7' && p[3] == 0) {
		done = req - (remc - '0');
		if (done < 0) {
			// Something like "WIDE3-7", which is definitely bogus!
			done = 0;
			state->fixall = 1;
			if (viaindex == 2 && !hasHflag)
				state->probably_heard_direct = 1;
			goto addtostate;
		}
		if (viaindex == 2) {
			if (istrace) // A real "TRACE" in first slot?
				state->probably_heard_direct = 1;

			else if (!hasHflag && done == 0) // WIDE1-1/2-2/3-3/etc on first slot
				state->probably_heard_direct = 1;
		}
		// if (debug>1) printf(" g[req=%d,done=%d%s]",req,done,hasHflag ? ",Hflag!":"");
		goto addtostate;

	} else if (('8' <= remc && remc <= '9' && p[3] == 0) ||
			(remc == '1' && '0' <= p[3] && p[3] <= '5' && p[4] == 0)) {
		// The request has SSID value in range of 8 to 15
		state->fixall = 1;
		if (viaindex == 2 && !hasHflag)
			state->probably_heard_direct = 1;
		return 0;

	} else {
		// Yuck, impossible/syntactically invalid
		state->hopsreq  += 1;
		state->hopsdone += hasHflag;
		if (istrace) {
			state->tracereq  += 1;
			state->tracedone += hasHflag;
		}
		if (viaindex == 2 && !hasHflag)
			state->probably_heard_direct = 1;
		// if (debug>1) printf(" h[req=%d,done=%d]",1,hasHflag);
		return 1;
	}

addtostate:;
		 // We've successfully parsed the field. Update the viastate and return 0
		 state->hopsreq  += req;
		 state->hopsdone += done;
		 if (istrace) {
			 state->tracereq  += req;
			 state->tracedone += done;
		 }
		 return 0;
}

static int match_transmitter(const char *viafield,
		const struct digipeater_source *src,
		const int lastviachar)
{
	struct aprx_interface *aif = src->parent->transmitter;
	int tlen = strlen(aif->callsign);

	if (memcmp(viafield, aif->callsign, tlen) == 0) {
		if (viafield[tlen] == lastviachar)
			return 1;
	}

	return 0;
}

static int try_reject_filters(const int  fieldtype,
		const char *field,
		struct digipeater_source *src)
{
	int i;
	int stat = 0;
	switch (fieldtype) {
		case 0: // Source
			for (i = 0; i < src->sourceregscount; ++i) {
				stat = regexec(src->sourceregs[i],
						field, 0, NULL, 0);
				if (stat == 0)
					return 1;       /* MATCH! */
			}
			if (memcmp("MYCALL",field,6)==0) return 1;
			if (memcmp("N0CALL",field,6)==0) return 1;
			if (memcmp("NOCALL",field,6)==0) return 1;
			break;
		case 1: // Destination

			for (i = 0; i < src->destinationregscount; ++i) {
				int stat = regexec(src->destinationregs[i],
						field, 0, NULL, 0);
				if (stat == 0)
					return 1;       /* MATCH! */
			}
			if (memcmp("MYCALL",field,6)==0) return 1;
			if (memcmp("N0CALL",field,6)==0) return 1;
			if (memcmp("NOCALL",field,6)==0) return 1;
			break;
		case 2: // Via

			for (i = 0; i < src->viaregscount; ++i) {
				int stat = regexec(src->viaregs[i],
						field, 0, NULL, 0);
				if (stat == 0)
					return 1;       /* MATCH! */
			}
			if (memcmp("MYCALL",field,6)==0) return 1;
			if (memcmp("N0CALL",field,6)==0) return 1;
			if (memcmp("NOCALL",field,6)==0) return 1;
			break;
		case 3: // Data

			for (i = 0; i < src->dataregscount; ++i) {
				int stat = regexec(src->dataregs[i],
						field, 0, NULL, 0);
				if (stat == 0)
					return 1;       /* MATCH! */
			}
			break;
		default:
			if (debug)
				printf("try_reject_filters(fieldtype=%d) - CODE BUG\n",
						fieldtype);
			return 1;
	}
	if (stat != 0 && stat != REG_NOMATCH) {
		// Some odd reason for an error?

	}
	return 0;
}

/* Parse executed and requested WIDEn-N/TRACEn-N info */
static int parse_tnc2_hops(struct digistate *state,
		struct digipeater_source *src,
		struct pbuf_t *pb)
{
	const char *p = pb->dstcall_end+1;
	const char *s;
	const struct digipeater *digi = src->parent;
	const char *lastviastar;
	char viafield[15]; // temp buffer for many uses
	int have_fault = 0;
	int viaindex = 1; // First via index will be 2..
	int activeviacount = 0;
	int len;
	int digiok;

	if (debug>1) printf(" hops count of buffer: %s\n",p);

	if (src->src_relaytype == DIGIRELAY_THIRDPARTY) {
		state->v.hopsreq = 1; // Bonus for tx-igated 3rd-party frames
		state->v.tracereq = 1; // Bonus for tx-igated 3rd-party frames
		state->v.hopsdone = 0;
		state->v.tracedone = 0;
		state->v.probably_heard_direct = 1;
		return 0;
	}

	// Copy the SRCCALL part of  SRCALL>DSTCALL  to viafield[] buffer
	len = pb->srccall_end - pb->data;
	if (len >= sizeof(viafield)) len = sizeof(viafield)-1;
	memcpy(viafield, pb->data, len);
	viafield[len] = 0;
	// if (debug>2)printf(" srccall='%s'",viafield);
	if (try_reject_filters(0, viafield, src)) {
		if (debug>1) printf(" - Src filters reject\n");
		return 1; // Src reject filters
	}

	// Copy the DSTCALL part of  SRCALL>DSTCALL  to viafield[] buffer
	len = pb->dstcall_end - pb->srccall_end -1;
	if (len >= sizeof(viafield)) len = sizeof(viafield)-1;
	memcpy(viafield, pb->srccall_end+1, len);
	viafield[len] = 0;
	// if (debug>2)printf(" destcall='%s'",viafield);
	if (try_reject_filters(1, viafield, src)) {
		if (debug>1) printf(" - Dest filters reject\n");
		return 1; // Dest reject filters
	}

	// Where is the last via-field with a star on it?
	len = pb->info_start - p; if (len < 0) len=0;
	lastviastar = memrchr(p, len, '*');

	// Loop over VIA fields to see if we need to digipeat anything.
	while (p < pb->info_start && !have_fault) {
		len = 0;

		if (*p == ':') {
			// A round may stop at ':' found at the end of the processed field,
			// then next round finds it at the start of the field.
			break;
		}

		// Scan for a VIA field ...
		for (s = p; s < pb->info_start; ++s) {
			if (*s == ',' || *s == ':') {
				// ... until comma or double-colon.
				break;
			}
		}
		// [p..s) is now one VIA field.
		if (s == p && *p != ':') {  // BAD!
			have_fault = 1;
			if (debug>1) printf(" S==P ");
			break;
		}
		if (*p == 'q') break; // APRSIS q-constructs..
		++viaindex;

		// Pick-up a viafield to separate buffer for processing
		len = s-p;
		if (len >= sizeof(viafield)) len = sizeof(viafield)-2;
		memcpy(viafield, p, len);
		viafield[len] = 0;
		if (*s == ',') ++s;
		p = s;

		// Only last via field with H-bit is indicated at TNC2 format,
		// but this digi code logic needs it at every VIA field where
		// it is set.  Therefore this crooked way to add it to picked
		// up fields.
		if (strchr(viafield,'*') == NULL) {
			// If it exists somewhere, and we are not yet at it..
			if (lastviastar != NULL && p < lastviastar)
				strcat(viafield,"*"); // we do know that there is space for this.
		}

		if (debug>1) printf(" - ViaField[%d]: '%s'\n", viaindex, viafield);

		// VIA-field picked up, now analyze it..

		if (try_reject_filters(2, viafield, src)) {
			if (debug>1) printf(" - Via filters reject\n");
			return 1; // via reject filters
		}

		// Transmitter callsign match with H-flag set.
		if (match_transmitter(viafield, src, '*')) {
			if (debug>1) printf(" - Tx match reject\n");
			// Oops, LOOP!  I have transmit this in past
			// (according to my transmitter callsign present
			// in a VIA field!)
			return 1;
		}

		// If there is no '*' meaning this has not been
		// processed, then this is active field..
		if (strchr(viafield, '*') == NULL)
			++activeviacount;

		digiok = 0;

		// If first active field (without '*') matches
		// transmitter or alias, then this digi is accepted
		// regardless if it is APRS or some other protocol.
		if (activeviacount == 1 &&
				(match_transmitter(viafield, src, 0) ||
				 match_aliases(viafield, digi->transmitter))) {
			if (debug>1) printf(" - Tx match accept!\n");
			state->v.hopsreq  += 1;
			state->v.tracereq += 1;
			digiok = 1;
		}

		// .. otherwise following rules are applied only to APRS packets.
		if (pb->is_aprs) {

			if ((len = match_tracewide(viafield, src->src_trace))) {
				// Match source specific list of trace aliases
				if (debug>1) printf("Trace (src specific)\n");
				have_fault = count_single_tnc2_tracewide(&state->v, viafield, 1, len, viaindex);
				if (!have_fault)
					digiok = 1;
			} else if ((len = match_tracewide(viafield, digi->trace))) {
				// Match digipeater-wide list of trace aliases
				if (debug>1) printf("Trace (global)\n");
				have_fault = count_single_tnc2_tracewide(&state->v, viafield, 1, len, viaindex);
				if (!have_fault)
					digiok = 1;
			} else if ((len = match_tracewide(viafield, src->src_wide))) {
				// Match source specific list of non-trace aliases
				if (debug>1) printf("Trace (src-specific, non-trace)\n");
				have_fault = count_single_tnc2_tracewide(&state->v, viafield, 0, len, viaindex);
				if (!have_fault)
					digiok = 1;
			} else if ((len = match_tracewide(viafield, digi->wide))) {
				// Match digipeater-wide list of non-trace aliases
				if (debug>1) printf("Trace (global, non-trace)\n");
				have_fault = count_single_tnc2_tracewide(&state->v, viafield, 0, len, viaindex);
				if (!have_fault)
					digiok = 1;
			} else {
				// No match on trace or wide, but if there was earlier
				// match on interface or alias, then it set "digiok" for us.
				state->v.digidone += (int) (strchr(viafield,'*') != NULL);
				if (debug>1) printf("Trace (non-alias) digi=%d\n",state->v.digidone);
			}
		}
		if (state->v.fixthis || state->v.fixall) {
			// Argh..  bogus WIDEn seen, which is what UIDIGIs put out..
			// Also some other broken requests are "fixed": like WIDE3-7
			// Fixing it: We set the missing H-bit, and continue processing.
			// (That fixing is done in incoming AX25 address field, which
			//  we generally do not touch - with this exception.)
			pb->ax25addr[ AX25ADDRLEN*viaindex + AX25ADDRLEN-1 ] |= AX25HBIT;
			state->v.fixthis = 0;
		}

		if (digiok) {
			if (debug>1) printf(" via field match %s\n", viafield);
			if(state->v.hopsreq>state->v.hopsdone) break;
		}
	}
	return have_fault;
}


static void free_tracewide(struct tracewide *twp)
{
	int i;

	if (twp == NULL) return;
	if (twp->keys) {
		for (i = 0; i < twp->nkeys; ++i)
			free((void*)(twp->keys[i]));
		free(twp->keys);
	}
	if (twp->keylens)
		free((void*)(twp->keylens));

	free(twp);
}
static void free_source(struct digipeater_source *src)
{
	if (src == NULL) return;
	free(src);
}

static struct tracewide *digipeater_config_tracewide(struct configfile *cf, int is_trace)
{
	char  *name, *param1;
	char  *str       = cf->buf;
	int    has_fault = 0;
	int    nkeys     = 0;
	char **keywords  = NULL;
	int   *keylens   = NULL;
	int    maxreq    = 4;
	int    maxdone   = 4;
	struct tracewide *tw;

	while (readconfigline(cf) != NULL) {
		if (configline_is_comment(cf))
			continue;       /* Comment line, or empty line */

		// It can be severely indented...
		str = config_SKIPSPACE(cf->buf);

		name = str;
		str = config_SKIPTEXT(str, NULL);
		str = config_SKIPSPACE(str);
		config_STRLOWER(name);

		param1 = str;
		str = config_SKIPTEXT(str, NULL);
		str = config_SKIPSPACE(str);

		if (is_trace) {
			if (strcmp(name, "</trace>") == 0) {
				break;
			}
		} else {
			if (strcmp(name, "</wide>") == 0) {
				break;
			}
		}

		// ... actual parameters
		if (strcmp(name,"maxreq") == 0) {
			maxreq = atoi(param1);
			// if (debug) printf(" maxreq %d\n",maxreq);

		} else if (strcmp(name,"maxdone") == 0) {
			maxdone = atoi(param1);
			// if (debug) printf(" maxdone %d\n",maxdone);

		} else if (strcmp(name,"keys") == 0) {
			char *k = strtok(param1, ",");
			for (; k ; k = strtok(NULL,",")) {
				++nkeys;
				// if (debug) printf(" n=%d key='%s'\n",nkeys,k);
				keywords = realloc(keywords, sizeof(char*) * nkeys);
				keywords[nkeys-1] = strdup(k);

				keylens  = realloc(keylens,  sizeof(int) * nkeys);
				keylens[nkeys-1] = strlen(k);
			}

		} else {
			has_fault = 1;
			printf("%s:%d ERROR: Unknown keyword inside %s subblock: '%s'\n",
					cf->name, cf->linenum, is_trace ? "<trace>":"<wide>", name);
		}
	}

	if (has_fault) {
		int i;
		for (i = 0; i < nkeys; ++i)
			free(keywords[i]);
		if (keywords != NULL)
			free(keywords);
		if (keylens != NULL)
			free(keylens);
		return NULL;
	}

	tw = calloc(1,sizeof(*tw));

	tw->maxreq   = maxreq;
	tw->maxdone  = maxdone;
	tw->is_trace = is_trace;
	tw->nkeys    = nkeys;
	tw->keys     = keywords;
	tw->keylens  = keylens;

	return tw;
}

static struct digipeater_source *digipeater_config_source(struct configfile *cf)
{
	char *name, *param1;
	char *str = cf->buf;
	int has_fault = 0;
	int viscous_delay = 0;
	float ratelimit = 120;
	float rateincrement = 60;

	struct aprx_interface *source_aif = NULL;
	struct digipeater_source  *source = NULL;
	digi_relaytype          relaytype = DIGIRELAY_DIGIPEAT;
	struct filter_t          *filters = NULL;
	struct tracewide    *source_trace = NULL;
	struct tracewide     *source_wide = NULL;
	struct digipeater_source regexsrc;
#ifndef DISABLE_IGATE
	char                    *via_path = NULL;
	char                    *msg_path = NULL;
	uint8_t               ax25viapath[AX25ADDRLEN];
	uint8_t                msgviapath[AX25ADDRLEN];
#endif

	memset(&regexsrc, 0, sizeof(regexsrc));
#ifndef DISABLE_IGATE
	memset(ax25viapath, 0, sizeof(ax25viapath));
	memset(msgviapath,  0, sizeof(msgviapath));
#endif

	while (readconfigline(cf) != NULL) {
		if (configline_is_comment(cf))
			continue;       /* Comment line, or empty line */

		// It can be severely indented...
		str = config_SKIPSPACE(cf->buf);

		name = str;
		str = config_SKIPTEXT(str, NULL);
		str = config_SKIPSPACE(str);
		config_STRLOWER(name);

		param1 = str;
		str = config_SKIPTEXT(str, NULL);
		str = config_SKIPSPACE(str);

		if (strcmp(name, "</source>") == 0) {
			break;

			// ... actual parameters
		} else if (strcmp(name,"source") == 0) {
			if (debug)
				printf("%s:%d <source> source = '%s'\n",
						cf->name, cf->linenum, param1);

			if (strcasecmp(param1,"$mycall") == 0)
				param1 = (char*)mycall;

			source_aif = find_interface_by_callsign(param1);
			if (source_aif == NULL) {
				has_fault = 1;
				printf("%s:%d ERROR: Digipeater source '%s' not found\n",
						cf->name, cf->linenum, param1);
			}
			if (debug>1)
				printf(" .. source_aif = %p\n", source_aif);

		} else if (strcmp(name, "viscous-delay") == 0) {
			viscous_delay = atoi(param1);
			if (debug) printf(" viscous-delay = %d\n",viscous_delay);
			if (viscous_delay < 0) {
				printf("%s:%d ERROR: Bad value for viscous-delay: '%s'\n",
						cf->name, cf->linenum, param1);
				viscous_delay = 0;
				has_fault = 1;
			}
			if (viscous_delay > 9) {
				printf("%s:%d ERROR: Too large value for viscous-delay: '%s'\n",
						cf->name, cf->linenum, param1);
				viscous_delay = 9;
				has_fault = 1;
			}

		} else if (strcmp(name, "ratelimit") == 0) {
			char *param2 = str;
			str = config_SKIPTEXT(str, NULL);
			str = config_SKIPSPACE(str);

			rateincrement = (float)atof(param1);
			ratelimit     = (float)atof(param2);
			if (rateincrement < 0.01 || rateincrement > rateincrementmax)
				rateincrement = 60;
			if (ratelimit < 0.01 || ratelimit > ratelimitmax)
				ratelimit = 120;
			if (ratelimit < rateincrement)
				rateincrement = ratelimit;
			if (debug)
				printf("  .. ratelimit %f %f\n",
						rateincrement, ratelimit);

		} else if (strcmp(name,"regex-filter") == 0) {
			if (regex_filter_add(cf, &regexsrc, param1, str)) {
				// prints errors internally
				has_fault = 1;
			}

#ifndef DISABLE_IGATE
		} else if (strcmp(name, "via-path") == 0) {

			// Validate that source callsign is "APRSIS"
			// or "DPRS" for this parameter

			if (source_aif == NULL ||
					(strcmp(source_aif->callsign,"APRSIS") != 0 &&
					 strcmp(source_aif->callsign,"DPRS") != 0)) {
				printf("%s:%d ERROR: via-path parameter is available only on 'source APRSIS' and 'source DPRS' cases.\n",
						cf->name, cf->linenum);
				has_fault = 1;
				continue;
			}

			via_path  = strdup(param1);
			config_STRUPPER(via_path);

			if (parse_ax25addr(ax25viapath, via_path, 0x00)) {
				has_fault = 1;
				printf("%s:%d ERROR: via-path parameter is not valid AX.25 callsign: '%s'\n",
						cf->name, cf->linenum, via_path);
				free(via_path);
				via_path = NULL;
				continue;
			}

			if (debug)
				printf("via-path '%s'\n", via_path);

		} else if (strcmp(name, "msg-path") == 0) {

			// Validate that source callsign is "APRSIS"
			// or "DPRS" for this parameter

			if (source_aif == NULL ||
					(strcmp(source_aif->callsign,"APRSIS") != 0 &&
					 strcmp(source_aif->callsign,"DPRS") != 0)) {
				printf("%s:%d ERROR: msg-path parameter is available only on 'source APRSIS' and 'source DPRS' cases.\n",
						cf->name, cf->linenum);
				has_fault = 1;
				continue;
			}

			msg_path  = strdup(param1);
			config_STRUPPER(msg_path);

			if (parse_ax25addr(msgviapath, msg_path, 0x00)) {
				has_fault = 1;
				printf("%s:%d ERROR: msg-path parameter is not valid AX.25 callsign: '%s'\n",
						cf->name, cf->linenum, msg_path);
				free(msg_path);
				msg_path = NULL;
				continue;
			}

			if (debug)
				printf("msg-path '%s'\n", msg_path);
#endif
		} else if (strcmp(name,"<trace>") == 0) {
			if (source_trace == NULL) {
				source_trace = digipeater_config_tracewide(cf, 1);
				// prints errors internally
			} else {
				has_fault = 1;
				printf("%s:%d ERROR: double definition of <trace> block.\n",
						cf->name, cf->linenum);
			}

		} else if (strcmp(name,"<wide>") == 0) {
			if (source_wide == NULL) {
				source_wide  = digipeater_config_tracewide(cf, 0);
				// prints errors internally
			} else {
				has_fault = 1;
				printf("%s:%d ERROR: double definition of <wide> block.\n",
						cf->name, cf->linenum);
			}

		} else if (strcmp(name,"filter") == 0) {
			if (filter_parse(&filters, param1)) {
				// prints errors internally

				has_fault = 1;
				printf("%s:%d ERROR: Error at filter parser.\n",
						cf->name, cf->linenum);
			} else {
				if (debug)
					printf(" .. OK filter %s\n", param1);
			}

		} else if (strcmp(name,"relay-type") == 0 ||   // documented name
				strcmp(name,"relay-format") == 0 || // an alias
				strcmp(name,"digi-mode") == 0) {    // very old alias
			config_STRLOWER(param1);
			if (strcmp(param1,"digipeat") == 0) {
				relaytype = DIGIRELAY_DIGIPEAT;
			} else if (strcmp(param1,"digipeated") == 0) {
				relaytype = DIGIRELAY_DIGIPEAT;
			} else if (strcmp(param1,"digipeater") == 0) {
				relaytype = DIGIRELAY_DIGIPEAT;
			} else if (strcmp(param1,"directonly") == 0) {
				relaytype = DIGIRELAY_DIGIPEAT_DIRECTONLY;
			} else if (strcmp(param1,"third-party") == 0) {
				relaytype = DIGIRELAY_THIRDPARTY;
			} else if (strcmp(param1,"3rd-party") == 0) {
				relaytype = DIGIRELAY_THIRDPARTY;
			} else {
				printf("%s:%d ERROR: Digipeater <source>'s %s did not recognize: '%s' \n", cf->name, cf->linenum, name, param1);
				has_fault = 1;
			}
		} else {
			printf("%s:%d ERROR: Digipeater <source>'s %s did not recognize: '%s' \n", cf->name, cf->linenum, name, param1);
			has_fault = 1;
		}
	}

	if (source_aif == NULL) {
		has_fault = 1;
		printf("%s:%d ERROR: Missing or bad 'source =' definition at this <source> group.\n",
				cf->name, cf->linenum);
	}

	if (!has_fault && (source_aif != NULL)) {
		source = calloc(1,sizeof(*source));

		source->src_if        = source_aif;
		source->src_relaytype = relaytype;
		source->src_filters   = filters;
		source->src_trace     = source_trace;
		source->src_wide      = source_wide;
#ifndef DISABLE_IGATE
		source->via_path      = via_path;
		source->msg_path      = msg_path;
		memcpy(source->ax25viapath, ax25viapath, sizeof(ax25viapath));
		memcpy(source->msgviapath,  msgviapath,  sizeof(msgviapath));
		if (msg_path == NULL) { // default value of via-path !
			source->msg_path    = via_path;
			memcpy(source->msgviapath,  ax25viapath,  sizeof(ax25viapath));
		}
#endif

		source->viscous_delay = viscous_delay;

		source->tbf_limit     = (ratelimit * TOKENBUCKET_INTERVAL)/60;
		source->tbf_increment = (rateincrement * TOKENBUCKET_INTERVAL)/60;
		source->tokenbucket   = source->tbf_limit;

		// RE pattern reject filters
		source->sourceregscount      = regexsrc.sourceregscount;
		source->sourceregs           = regexsrc.sourceregs;
		source->destinationregscount = regexsrc.destinationregscount;
		source->destinationregs      = regexsrc.destinationregs;
		source->viaregscount         = regexsrc.viaregscount;
		source->viaregs              = regexsrc.viaregs;
		source->dataregscount        = regexsrc.dataregscount;
		source->dataregs             = regexsrc.dataregs;

	} else {
		// Errors detected
		free_tracewide(source_trace);
		free_tracewide(source_wide);
		// filters_free(filters);
		// free regexsrc's allocations
		if (debug)
			printf("Seen errors at <digipeater><source> definition.\n");
	}

	if (debug>1)printf(" .. <source> definition returning %p\n",source);
	return source;
}

int digipeater_config(struct configfile *cf)
{
	char *name, *param1;
	char *str = cf->buf;
	int has_fault = 0;
	int i;
	const int line0 = cf->linenum;

	struct aprx_interface *aif = NULL;
	float ratelimit = 60;
	float rateincrement = 60;
	float srcratelimit = 60;
	float srcrateincrement = 60;
	int sourcecount = 0;
	int dupestoretime = 30; // FIXME: parametrize! 30 is minimum..
	struct digipeater_source **sources = NULL;
	struct digipeater *digi = NULL;
	struct tracewide *traceparam = NULL;
	struct tracewide *wideparam  = NULL;

	while (readconfigline(cf) != NULL) {
		if (configline_is_comment(cf))
			continue;       /* Comment line, or empty line */

		// It can be severely indented...
		str = config_SKIPSPACE(cf->buf);

		name = str;
		str = config_SKIPTEXT(str, NULL);
		str = config_SKIPSPACE(str);
		config_STRLOWER(name);

		param1 = str;
		str = config_SKIPTEXT(str, NULL);
		str = config_SKIPSPACE(str);

		if (strcmp(name, "</digipeater>") == 0) {
			break;
		}
		if (strcmp(name, "transmit") == 0 ||
				strcmp(name, "transmitter") == 0) {
			if (strcasecmp(param1,"$mycall") == 0)
				param1 = (char*)mycall;

			aif = find_interface_by_callsign(param1);
			if (aif != NULL && (!aif->tx_ok)) {
				aif = NULL; // Not 
				printf("%s:%d ERROR: This transmit interface has no TX-OK TRUE setting: '%s'\n",
						cf->name, cf->linenum, param1);
				has_fault = 1;
			} else if (aif != NULL && aif->txrefcount > 0) {
				aif = NULL;
				printf("%s:%d ERROR: This transmit interface is being used on multiple <digipeater>s as transmitter: '%s'\n",
						cf->name, cf->linenum, param1);
				has_fault = 1;
			} else if (aif == NULL) {
				printf("%s:%d ERROR: Unknown interface: '%s'\n",
						cf->name, cf->linenum, param1);
				has_fault = 1;
			}

		} else if (strcmp(name, "ratelimit") == 0) {
			char *param2 = str;
			str = config_SKIPTEXT(str, NULL);
			str = config_SKIPSPACE(str);

			rateincrement = (float)atof(param1);
			ratelimit     = (float)atof(param2);
			if (rateincrement < 0.01 || rateincrement > rateincrementmax)
				rateincrement = 60;
			if (ratelimit < 0.01 || ratelimit > ratelimitmax)
				ratelimit = 60;
			if (ratelimit < rateincrement)
				rateincrement = ratelimit;
			if (debug)
				printf("  .. ratelimit %f %f\n",
						rateincrement, ratelimit);

		} else if (strcmp(name, "srcratelimit") == 0) {
			char *param2 = str;
			str = config_SKIPTEXT(str, NULL);
			str = config_SKIPSPACE(str);

			srcrateincrement = (float)atof(param1);
			srcratelimit     = (float)atof(param2);
			if (srcrateincrement < 0.01 || srcrateincrement > rateincrementmax)
				srcrateincrement = 60;
			if (srcratelimit < 0.01 || srcratelimit > ratelimitmax)
				srcratelimit = 60;
			if (srcratelimit < srcrateincrement)
				srcrateincrement = srcratelimit;
			if (debug)
				printf("  .. srcratelimit %f %f\n",
						srcrateincrement, srcratelimit);

		} else if (strcmp(name, "<trace>") == 0) {
			if (traceparam == NULL) {
				traceparam = digipeater_config_tracewide(cf, 1);
				if (traceparam == NULL) {
					printf("%s:%d ERROR: <trace> definition failed!\n",
							cf->name, cf->linenum);
					has_fault = 1;
				}
			} else {
				printf("%s:%d ERROR: Double definition of <trace> !\n",
						cf->name, cf->linenum);
				has_fault = 1;
			}

		} else if (strcmp(name, "<wide>") == 0) {
			if (wideparam == NULL) {
				wideparam = digipeater_config_tracewide(cf, 0);
				if (wideparam == NULL) {
					printf("%s:%d ERROR: <wide> definition failed!\n",
							cf->name, cf->linenum);
					has_fault = 1;
				}
			} else {
				printf("%s:%d ERROR: Double definition of <wide> !\n",
						cf->name, cf->linenum);
				has_fault = 1;
			}

		} else if (strcmp(name, "<source>") == 0) {
			struct digipeater_source *src =
				digipeater_config_source(cf);
			if (src != NULL) {
				// Found a source, link it!
				sources = realloc(sources, sizeof(void*) * (sourcecount+1));
				sources[sourcecount] = src;
				++sourcecount;
			} else {
				has_fault = 1;
				printf("%s:%d ERROR: <source> definition failed\n",
						cf->name, cf->linenum);
			}

		} else {
			printf("%s:%d ERROR: Unknown <digipeater> config keyword: '%s'\n",
					cf->name, cf->linenum, name);
			has_fault = 1;
			continue;
		}
	}

	if (aif == NULL && !has_fault) {
		printf("%s:%d ERROR: Digipeater defined without transmit interface.\n",
				cf->name, cf->linenum);
		has_fault = 1;
	}
	if (sourcecount == 0 && !has_fault) {
		printf("%s:%d ERROR: Digipeater defined without <source>:s.\n",
				cf->name, cf->linenum);
		has_fault = 1;
	}
	// Check that source definitions are unique
	for ( i = 0; i < sourcecount; ++i ) {
		int j;
		for (j = i+1; j < sourcecount; ++j) {
			if (sources[i]->src_if == sources[j]->src_if) {
				has_fault = 1;
				printf("%s:%d Two <source>s on this <digipeater> definition use same <interface>: '%s'\n",
						cf->name, line0, sources[i]->src_if->callsign);
			}
		}
	}

	if (has_fault) {
		// Free allocated resources and link pointers, if any
		for ( i = 0; i < sourcecount; ++i ) {
			free_source(sources[i]);
		}
		if (sources != NULL)
			free(sources);

		free_tracewide(traceparam);
		free_tracewide(wideparam);

		printf("ERROR: Config fault observed on <digipeater> definitions! \n");
	} else {
		// Construct the digipeater

		digi = calloc(1,sizeof(*digi));

		if (debug>1)printf("<digipeater> sourcecount=%d\n",sourcecount);

		// up-link all interfaces used as sources
		for ( i = 0; i < sourcecount; ++i ) {
			struct digipeater_source *src = sources[i];
			src->parent = digi; // Set parent link

			src->src_if->digisources = realloc( src->src_if->digisources,
					(src->src_if->digisourcecount +1) * (sizeof(void*)));
			src->src_if->digisources[src->src_if->digisourcecount] = src;
			src->src_if->digisourcecount += 1;
		}

		aif->txrefcount += 1; // Increment Tx usage Reference count.
		// We permit only one <digipeater> to
		// use any given Tx-interface. (Rx:es
		// permit multiple uses.)
		digi->transmitter   = aif;
		digi->tbf_limit     = (ratelimit * TOKENBUCKET_INTERVAL)/60;
		digi->tbf_increment = (rateincrement * TOKENBUCKET_INTERVAL)/60;
		digi->src_tbf_limit = (srcratelimit * TOKENBUCKET_INTERVAL)/60;
		digi->src_tbf_increment = (srcrateincrement * TOKENBUCKET_INTERVAL)/60;
		digi->tokenbucket   = digi->tbf_limit;

		digi->dupechecker   = dupecheck_new(dupestoretime);  // Dupecheck is per transmitter
#ifndef DISABLE_IGATE
		digi->historydb     = historydb_new();  // HistoryDB is per transmitter
#endif

		digi->trace         = (traceparam != NULL) ? traceparam : & default_trace_param;
		digi->wide          = (wideparam  != NULL) ? wideparam  : & default_wide_param;

		digi->sourcecount   = sourcecount;
		digi->sources       = sources;

		digis = realloc( digis, sizeof(void*) * (digi_count+1));
		digis[digi_count] = digi;
		++digi_count;
	}
	return has_fault;
}


static int decrement_ssid(uint8_t *ax25addr)
{
	// bit-field manipulation
	int ssid = (ax25addr[AX25ADDRLEN-1] >> 1) & 0x0F;
	if (ssid > 0)
		--ssid;
	ax25addr[AX25ADDRLEN-1] = (ax25addr[AX25ADDRLEN-1] & 0xE1) | (ssid << 1);
	return ssid;
}


/* 0 == accept, otherwise reject */
/*
   int digipeater_receive_filter(struct digipeater_source *src, struct pbuf_t *pb)
   {

   if (src->src_filters == NULL) {
   if (debug>1)
   printf("No source filters, accepted the packet from %s.\n", src->src_if->callsign);
   return 0;
   }
   int rc = filter_process(pb, src->src_filters, src->parent->historydb);
   if (rc != 1) {
   if (debug>1)
   printf("Source filtering rejected the packet from %s.\n", src->src_if->callsign);
   return 1;
   }
   if (debug>1)
   printf("Source filtering accepted the packet from %s.\n", src->src_if->callsign);
   return 0;
   }
   */

static void digipeater_receive_backend(struct digipeater_source *src, struct pbuf_t *pb)
{
	int len, viaindex;
	struct digistate state;
	struct viastate  viastate;
	struct digipeater *digi = src->parent;
	char viafield[14]; // room for text format
	uint8_t *axaddr, *e;

	memset(&state,    0, sizeof(state));
	memset(&viastate, 0, sizeof(viastate));

	//  2) Verify that none of our interface callsigns does match any
	//     of already DIGIPEATED via fields! (fields that have H-bit set)
	//   ( present implementation: this digi's transmitter callsign is
	//     verified)

	// Parse executed and requested WIDEn-N/TRACEn-N info
	if (parse_tnc2_hops(&state, src, pb)) {
		// A fault was observed! -- tests include "not this transmitter"
		if (debug>1)
			printf("Parse_tnc2_hops rejected this.");
		return;
	}

	if (pb->is_aprs) {

		if (state.v.probably_heard_direct) {
			// Collect a decaying average of distances to stations?
			//  .. could auto-beacon an aloha-circle - maybe
			//  .. note: this does not get packets that have no VIA fields.
			// Score of direct DX:es?
			//  .. note: this does not get packets that have no VIA fields.
		} else {
			if (src->src_relaytype == DIGIRELAY_DIGIPEAT_DIRECTONLY) {
				// Source relaytype is DIRECTONLY, and this was not
				// likely directly heard...
				if (debug>1) printf("DIRECTONLY -mode, and packet is probably not direct heard.");
				return;
			}
		}
		// Keep score of all DX packets?

		if (try_reject_filters(3, pb->info_start, src)) {
			if (debug>1)
				printf(" - Data body regexp filters reject\n");
			return; // data body regexp reject filters
		}

		// FIXME: 3) aprsc style filters checking in service area of the packet..

	}

	// 4) Hop-count filtering:

	// APRSIS sourced packets have different rules than DIGIPEAT
	// packets...
	if (state.v.hopsreq <= state.v.hopsdone) {
		if (debug>1) printf(" No remaining hops to execute.\n");
		return;
	}
	if (state.v.hopsreq   > digi->trace->maxreq  ||
			state.v.hopsreq   > digi->wide->maxreq   ||
			state.v.tracereq  > digi->trace->maxreq  ||
			state.v.digidone  > digi->trace->maxdone ||
			state.v.digidone  > digi->wide->maxdone  ||
			state.v.hopsdone  > digi->trace->maxdone ||
			state.v.hopsdone  > digi->wide->maxdone  ||
			state.v.tracedone > digi->trace->maxdone) {
		if (debug) printf(" Packet exceeds digipeat limits\n");
		if (!state.v.probably_heard_direct) {
			if (debug) printf(".. discard.\n");
			return;
		} else {
			state.v.fixall = 1;
		}
	}

	// if (debug) printf(" Packet accepted to digipeat!\n");

	state.ax25addrlen = pb->ax25addrlen;
	memcpy(state.ax25addr, pb->ax25addr, pb->ax25addrlen);
	axaddr = state.ax25addr + 2*AX25ADDRLEN;
	e      = state.ax25addr + state.ax25addrlen;

	if (state.v.fixall) {
		// Okay, insert my transmitter callsign on the first
		// VIA field, and mark the rest with H-bit
		// (in search loop below)
		int taillen = e - axaddr;
		if (state.ax25addrlen >= AX25ADDRMAXLEN) {
			if (debug) printf(" FIXALL TRACE overgrows the VIA fields! Dropping last of incoming ones.\n");
			// Drop the last via field to make room for insert below.
			state.ax25addrlen -= AX25ADDRLEN;
			taillen           -= AX25ADDRLEN;
		}
		// If we have a tail, move it up (there is always room for it)
		if (taillen > 0)
			memmove(axaddr+AX25ADDRLEN, axaddr, taillen);
		state.ax25addrlen += AX25ADDRLEN;
		e = state.ax25addr + state.ax25addrlen; // recalculate!

		// Put the transmitter callsign in
		memcpy(axaddr, digi->transmitter->ax25call, AX25ADDRLEN);

		// Set Address Termination bit at the last VIA field
		// (possibly ours, or maybe the previous one was truncated..)
		axaddr[state.ax25addrlen-1] |= AX25ATERM;
	}

	// Search for first AX.25 VIA field that does not have H-bit set:
	viaindex = 1; // First via field is number 2
	*viafield = 0; // clear that buffer for starters
	for (; axaddr < e; axaddr += AX25ADDRLEN, ++viaindex) {
		ax25_to_tnc2_fmtaddress(viafield, axaddr, 0);
		// if (debug>1) {
		//   printf(" via: %s", viafield);
		// }

		// Initial parsing said that things are seriously wrong..
		// .. and we will digipeat the packet with all H-bits set.
		if (state.v.fixall) axaddr[AX25ADDRLEN-1] |= AX25HBIT;

		if (!(axaddr[AX25ADDRLEN-1] & AX25HBIT)) // No "Has Been Digipeated" bit set
			break; // this doesn't happen in "fixall" mode
	}

	switch (src->src_relaytype) {
		case DIGIRELAY_THIRDPARTY:
			// Effectively disable the digipeat modifying of address
			axaddr = e;
			break;
		case DIGIRELAY_DIGIPEAT:
			// Normal functionality
			break;
		default: ;
	}

	// Unprocessed VIA field found (not in FIXALL mode)
	if (axaddr < e) {       // VIA-field of interest has been found

		// FIXME: 5) / 6) Cross-frequency/cross-band digipeat may add a special
		//                label telling that the message originated on other band

		// 7) WIDEn-N treatment (as well as transmitter matching digi)
		if (pb->digi_like_aprs) {
			if (strcmp(viafield,digi->transmitter->callsign) == 0 ||
					// Match on the transmitter callsign without the star...
					match_aliases(viafield, digi->transmitter)) {
				// .. or match transmitter interface alias.

				// Treat it as a TRACE request.

				int aterm = axaddr[AX25ADDRLEN-1] & AX25ATERM; // save old address termination bit
				// Put the transmitter callsign in, and set the H-bit.
				memcpy(axaddr, digi->transmitter->ax25call, AX25ADDRLEN);
				axaddr[AX25ADDRLEN-1] |= (AX25HBIT | aterm); // Set H-bit

			} else if ((len = match_tracewide(viafield, src->src_trace))) {
				count_single_tnc2_tracewide(&viastate, viafield, 1, len, viaindex);
			} else if ((len = match_tracewide(viafield, digi->trace))) {
				count_single_tnc2_tracewide(&viastate, viafield, 1, len, viaindex);
			} else if ((len = match_tracewide(viafield, src->src_wide))) {
				count_single_tnc2_tracewide(&viastate, viafield, 0, len, viaindex);
			} else if ((len = match_tracewide(viafield, digi->wide))) {
				count_single_tnc2_tracewide(&viastate, viafield, 0, len, viaindex);
			}

		} else { // Not "digi_as_aprs" rules

			if (strcmp(viafield,digi->transmitter->callsign) == 0) {
				// Match on the transmitter callsign without the star.
				// Treat it as a TRACE request.
				int aterm = axaddr[AX25ADDRLEN-1] & AX25ATERM; // save old address termination bit
				// Put the transmitter callsign in, and set the H-bit.
				memcpy(axaddr, digi->transmitter->ax25call, AX25ADDRLEN);
				axaddr[AX25ADDRLEN-1] |= (AX25HBIT | aterm); // Set H-bit

			} else if (match_aliases(viafield, digi->transmitter)) {
				// Match on the aliases.
				// Treat it as a TRACE request.
				int aterm = axaddr[AX25ADDRLEN-1] & AX25ATERM; // save old address termination bit
				// Put the transmitter callsign in, and set the H-bit.
				memcpy(axaddr, digi->transmitter->ax25call, AX25ADDRLEN);
				axaddr[AX25ADDRLEN-1] |= (AX25HBIT | aterm); // Set H-bit
			}
		}

		if (viastate.tracereq > viastate.tracedone) {
			// if (debug) printf(" TRACE on %s!\n",viafield);
			// Must move it up in memory to be able to put
			// transmitter callsign in
			int taillen = e - axaddr;
			int newssid;
			if (state.ax25addrlen >= AX25ADDRMAXLEN) {
				if (debug) printf(" TRACE overgrows the VIA fields! Discard.\n");
				return;
			}
			memmove(axaddr+AX25ADDRLEN, axaddr, taillen);
			state.ax25addrlen += AX25ADDRLEN;

			newssid = decrement_ssid(axaddr+AX25ADDRLEN);
			if (newssid <= 0)
				axaddr[2*AX25ADDRLEN-1] |= AX25HBIT; // Set H-bit
			// Put the transmitter callsign in, and set the H-bit.
			memcpy(axaddr, digi->transmitter->ax25call, AX25ADDRLEN);
			axaddr[AX25ADDRLEN-1] |= AX25HBIT; // Set H-bit

		} else if (viastate.hopsreq > viastate.hopsdone) {
			// If configuration didn't process "WIDE" et.al. as
			// a TRACE, then here we process them without trace..
			int newssid;
			if (debug) printf(" VIA on %s!\n",viafield);
			newssid = decrement_ssid(axaddr);
			if (newssid <= 0)
				axaddr[AX25ADDRLEN-1] |= AX25HBIT; // Set H-bit
		}
	}
	{
		history_cell_t *hcell;
		char tbuf[2800];
		int is_ui = 0, ui_pid = -1, frameaddrlen = 0, tnc2addrlen = 0, t2l;
		// uint8_t *u = state.ax25addr + state.ax25addrlen;
		// *u++ = 0;
		// *u++ = 0;
		// *u++ = 0;
		t2l = ax25_format_to_tnc( state.ax25addr,
				state.ax25addrlen+AX25ADDRLEN-1,
				tbuf, sizeof(tbuf),
				& frameaddrlen, &tnc2addrlen,
				& is_ui, &ui_pid );
		tbuf[t2l] = 0;
		if (debug) {
			printf(" out-hdr: '%s' data='",tbuf);
			(void)fwrite(pb->ax25data+2, pb->ax25datalen-2,  // without Control+PID
					1, stdout);
			printf("'\n");
		}

#ifndef DISABLE_IGATE
		// Insert into history database - track every packet
		hcell = historydb_insert_( digi->historydb, pb, 1 );

		if (hcell != NULL) {
			if (hcell->tokenbucket < 1.0) {
				if (debug) printf("TRANSMITTER SOURCE CALLSIGN RATELIMIT DISCARD.\n");
				return;
			}
			hcell->tokenbucket -= 1.0;
		}
#endif

		// Now we do token bucket filtering -- rate limiting
		if (digi->tokenbucket < 1.0) {
			if (debug) printf("TRANSMITTER RATELIMIT DISCARD.\n");
			return;
		}
		digi->tokenbucket -= 1.0;

		if (pb->is_aprs && rflogfile) {
			int t2l2;
			// Essentially Debug logging.. to file

			if (sizeof(tbuf) - pb->ax25datalen > t2l && t2l > 0) {
				// Have space for body too, skip leading Ctrl+PID bytes
				memcpy(tbuf+t2l, pb->ax25data+2, pb->ax25datalen-2); // Ctrl+PID skiped
				t2l2 = t2l + pb->ax25datalen-2; // tbuf size sans Ctrl+PID

				rflog( digi->transmitter->callsign, 'T', 0, tbuf, t2l2 );
				tbuf[t2l]=0;
			}
		}

		// Feed to dupe-filter (transmitter specific)
		// this means we have already seen it, and when 
		// it comes back from somewhere, we do not digipeat
		// it ourselves.

		// This recording is needed at output side of digipeater
		// for APRSIS and DPRS transmit gates.

		if (t2l>0) {
			dupecheck_aprs( digi->dupechecker,
					(const char *)tbuf,
					t2l,
					(const char *)pb->ax25data+2,
					pb->ax25datalen-2 );  // ignore Ctrl+PID
		} else {
			dupecheck_aprs( digi->dupechecker,
					(const char *)state.ax25addr,
					state.ax25addrlen,
					(const char *)pb->ax25data+2,
					pb->ax25datalen-2 );  // ignore Ctrl+PID
		}
	}

	// Feed to interface_transmit_ax25() with new header and body
	interface_transmit_ax25( digi->transmitter,
			state.ax25addr, state.ax25addrlen,
			(const char*)pb->ax25data, pb->ax25datalen );
	if (debug>1) printf("Done.\n");
}


void digipeater_receive( struct digipeater_source *src,
		struct pbuf_t *pb )
{
	// Below numbers like "4)" refer to Requirement Specification
	// paper chapter 2.6: Digipeater Rules

	//  The dupe-filter exists for APRS frames, possibly for some
	// selected UI frame types, and definitely not for CONS frames.

	if (debug)
		printf("digipeater_receive() from %s, is_aprs=%d viscous_delay=%d\n",
				src->src_if->callsign, pb->is_aprs, src->viscous_delay);

	if (src->tokenbucket < 1.0) {
		if (debug) printf("SOURCE RATELIMIT DISCARD\n");
		return;
	}
	src->tokenbucket -= 1.0;


	if (pb->is_aprs) {

		const int source_is_transmitter = (src->src_if ==
				src->parent->transmitter);

		// 1) Feed to dupe-filter (transmitter specific)
		//    If the dupe detector on this packet has reached
		//    count > 1, drop it.

		int jittery = src->viscous_delay > 0 ? random() % 3 + src->viscous_delay : 0;
		dupe_record_t *dupe = dupecheck_pbuf( src->parent->dupechecker,
				pb, jittery);
		if (dupe == NULL) {  // Oops.. allocation error!
			if (debug)
				printf("digipeater_receive() - dupecheck_pbuf() allocation error, packet discarded\n");
			return;
		}

		// 1.1) optional viscous delay!

		if (src->viscous_delay == 0) { // No delay, direct cases

			// First packet on direct source arrives here
			// with  seen = 1

			// 1.x) Analyze dupe checking

			if (debug>1)
				printf("Seen this packet %d times (delayed=%d)\n",
						dupe->delayed_seen + dupe->seen,
						dupe->delayed_seen);

			if (dupe->seen > 1) {
				// N:th direct packet, duplicate.
				// Drop this direct packet.
				if (debug>1) printf(".. discarded\n");
				return;
			}

			if (dupe->seen == 1 && dupe->delayed_seen > 0 &&
					dupe->pbuf == NULL) {
				// First direct, but dupe record does not have
				// pbuf anymore indicating that a delayed 
				// handling did process it sometime in past.
				// Drop this direct packet.
				if (debug>1) printf(".. discarded\n");
				return;
			}

			if (dupe->seen == 1 && dupe->delayed_seen >= 0 &&
					dupe->pbuf != NULL) {

				// First direct, and pbuf exists in dupe record.
				// It was added first to viscous queue, and
				// a bit latter came this direct one.
				// Remove one from viscous queue, and proceed
				// with direct processing.

				if (debug>1) printf(" .. discard dupe record, process immediately");

				pbuf_put(dupe->pbuf);
				dupe->pbuf = NULL;
				dupe = NULL; // Do not do  dupecheck_put() here!
			}

		} else {  // src->viscous_delay > 0

			// First packet on viscous source arrives here
			// with   dupe->delayed_seen = 1

			// Has this been seen on direct channel?
			if (dupe->seen > 0) {
				// Already processed thru direct processing,
				// no point in adding this to viscous delay queue
				if (debug>1)
					printf("Seen this packet %d times. Discarding it.\n",
							dupe->delayed_seen + dupe->seen);
				return;
			}

			// Depending on source definition, the transmitter is
			// either non-viscous or viscous.  We care about it
			// only when the source is viscous:
			if (source_is_transmitter)
				dupe->seen_on_transmitter += 1;

			if (dupe->delayed_seen > 1) {
				// 2nd or more of same packet from delayed source
				if (debug>1)
					printf("Seen this packet %d times.\n",
							dupe->delayed_seen + dupe->seen);

				// If any of them is transmitter interface, then
				// drop the queued packet, and drop current one.
				if (dupe->seen_on_transmitter > 0) {

					// If pbuf is on delayed queue, drop it.
					if (dupe->pbuf != NULL) {
						pbuf_put(dupe->pbuf);
						dupe->pbuf = NULL;
						dupe = NULL; // Do not do  dupecheck_put() here!
					}

				}
				if (debug>1) printf(".. discarded\n");
				return;
			}

			// First time that we have seen this packet at all.
			// Put the pbuf_t on viscous delay queue.. (Put
			// this dupe_record_t there, and the pbuf_t pointer
			// is already in that dupe_record_t.)
			src->viscous_queue_size += 1;
			if (src->viscous_queue_size > src->viscous_queue_space) {
				src->viscous_queue_space += 16;
				src->viscous_queue = realloc( src->viscous_queue,
						sizeof(void*) *
						src->viscous_queue_space );
			}
			src->viscous_queue[ src->viscous_queue_size -1 ]
				= dupecheck_get(dupe);

			if (debug) printf("%ld ENTER VISCOUS QUEUE: len=%d pbuf=%p\n",
					tick.tv_sec, src->viscous_queue_size, pb);
			return; // Put on viscous queue

		} 
	}
	// Send directly to backend
	if (debug>1) printf(".. direct to processing\n");
	digipeater_receive_backend(src, pb);
}

dupecheck_t *digipeater_find_dupecheck(const struct aprx_interface *aif)
{
	int i;
	for (i = 0; i < digi_count; ++i) {
		if (aif == digis[i]->transmitter)
			return digis[i]->dupechecker;
	}
	return NULL;
}

struct digipeater* digipeater_find_by_iface(const struct aprx_interface *aif) {
	int i;
	for (i = 0; i < digi_count; i++) {
		if (aif == digis[i]->transmitter)
			return digis[i];
	}

	if (debug > 1) printf(" Failed to find digipeater by aprx_interface\n");
	return NULL;
}


static void digipeater_resettime(void *arg)
{
	struct timeval *tv = (struct timeval *)arg;
	*tv = tick;
}


// Viscous queue processing needs poll digis <source>s for delayed actions
int  digipeater_prepoll(struct aprxpolls *app)
{
	int d, s;

	if (tokenbucket_timer.tv_sec == 0) {
		tokenbucket_timer = tick; // init this..
	}

	// If the time(2) has jumped around a lot,
	// and we didn't get around to do our work, reset the timer.

	if (time_reset) {
		digipeater_resettime(&tokenbucket_timer);
	}

	if (tv_timercmp( &tokenbucket_timer, &tick ) <= 0) {
		// Run the digipeater timer handling now
		// Will also advance the timer!
		if (debug>2) printf("digipeater_prepoll() run tokenbucket_timers\n");
		tv_timeradd_seconds( &tokenbucket_timer, &tokenbucket_timer, TOKENBUCKET_INTERVAL);
		run_tokenbucket_timers();
	}

	if (tv_timercmp( &tokenbucket_timer, &app->next_timeout ) <= 0) {
		app->next_timeout = tokenbucket_timer;
	}

	// if (debug>2) printf("digipeater_prepoll - 1 - timeout millis=%d\n",aprxpolls_millis(app));

	// Over all digipeaters..
	for (d = 0; d < digi_count; ++d) {
		struct digipeater *digi = digis[d];
		// Over all sources in those digipeaters
		for (s = 0; s < digi->sourcecount; ++s) {
			struct timeval tv;
			struct digipeater_source * src = digi->sources[s];
			// If viscous delay is zero, there is no work...
			// if (src->viscous_delay == 0)
			//   continue;
			// Delay is non-zero, perhaps there is work?
			if (src->viscous_queue_size == 0) // Empty queue
				continue;
			// First entry expires first
			tv.tv_sec = src->viscous_queue[0]->t + src->viscous_delay;
			tv.tv_usec = 0;
			if (tv_timercmp(&app->next_timeout, &tv) > 0) {
				app->next_timeout = tv;
				// if (debug>2) printf("digipeater_prepoll - 2 - timeout millis=%d\n",aprxpolls_millis(app));
			}
		}
	}

	return 0;
}

static void sourcecalltick(struct digipeater *digi);

int  digipeater_postpoll(struct aprxpolls *app)
{
	int d, s, i, donecount;

	if (tv_timercmp(&tokenbucket_timer, &tick) < 0) {
		tv_timeradd_seconds( &tokenbucket_timer, &tokenbucket_timer, TOKENBUCKET_INTERVAL);
		run_tokenbucket_timers();
	}

	// Over all digipeaters..
	for (d = 0; d < digi_count; ++d) {
		struct digipeater *digi = digis[d];

		// Over all sources in those digipeaters
		for (s = 0; s < digi->sourcecount; ++s) {
			struct digipeater_source * src = digi->sources[s];

			// If viscous delay is zero, there is no work...
			// if (src->viscous_delay == 0)
			//   continue;
			// Delay is non-zero, perhaps there is work?
			if (src->viscous_queue_size == 0) // Empty queue
				continue;
			// Feed backend from viscous queue
			donecount = 0;
			for (i = 0; i < src->viscous_queue_size; ++i) {
				struct dupe_record_t *dupe = src->viscous_queue[i];
				time_t t = dupe->t + src->viscous_delay;
				if ((t - tick.tv_sec) <= 0) {
					if (debug)printf("%ld LEAVE VISCOUS QUEUE: dupe=%p pbuf=%p\n",
							tick.tv_sec, dupe, dupe->pbuf);
					if (dupe->pbuf != NULL) {
						// We send the pbuf from viscous queue, if it still is
						// present in the dupe record.  (For example direct sourced
						// packets remove a packet from queued dupe record.)
						digipeater_receive_backend(src, dupe->pbuf);

						// Remove the delayed pbuf from this dupe record.
						pbuf_put(dupe->pbuf);
						dupe->pbuf = NULL;
					}
					dupecheck_put(dupe);
					++donecount;
				} else {
					break; // found a case we are not yet interested in.
				}
			}
			if (donecount > 0) {
				if (donecount >= src->viscous_queue_size) {
					// All cleared
					src->viscous_queue_size = 0;
				} else {
					// Compact the queue left after this processing round
					i = src->viscous_queue_size - donecount;
					memcpy(&src->viscous_queue[0],
							&src->viscous_queue[donecount],
							sizeof(void*) * i);
					src->viscous_queue_size = i;
				}
			}
		}
	}

	return 0;
}

static int  run_tokenbucket_timers()
{
	int d, s;
	// Over all digipeaters..
	for (d = 0; d < digi_count; ++d) {
		struct digipeater *digi = digis[d];

		digi->tokenbucket += digi->tbf_increment;
		if (digi->tokenbucket > digi->tbf_limit)
			digi->tokenbucket = digi->tbf_limit;

#ifndef DISABLE_IGATE
		sourcecalltick(digi);
#endif

		// Over all sources in those digipeaters
		for (s = 0; s < digi->sourcecount; ++s) {
			struct digipeater_source * src = digi->sources[s];

			src->tokenbucket += src->tbf_increment;
			if (src->tokenbucket > src->tbf_limit)
				src->tokenbucket = src->tbf_limit;

		}
	}

	return 0;
}

#ifndef DISABLE_IGATE
static void sourcecalltick(struct digipeater *digi)
{
	int i;
	historydb_t *db = digi->historydb;
	if (db == NULL) return; // Should never happen..

	for (i = 0; i < HISTORYDB_HASH_MODULO; ++i) {
		history_cell_t *c = db->hash[i];
		for ( ; c != NULL; c = c->next ) {
			c->tokenbucket += digi->src_tbf_increment;
			if (c->tokenbucket > digi->src_tbf_limit)
				c->tokenbucket = digi->src_tbf_limit;
		}
	}
}
#endif

// An utility function that exists at GNU Libc..

#if !defined(HAVE_MEMRCHR) && !defined(_FOR_VALGRIND_)
void   *memrchr(const void *s, int c, size_t n) {
	const unsigned char *p = s;
	c &= 0xFF;
	for (p = s+n; n > 0; --n, --p) {
		if (*p == c) return (void*)p;
	}
	return NULL;
}
#endif