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 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334
|
/********************************************************************
* 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, <oh2mqk@sral.fi>
*
* 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 == '-') {
f0.h.negation = 1;
++filt;
}
f0.h.type = *filt;
if (!strchr("abdefmopqrstuABDEFMOPQRSTU",*filt)) {
// Not valid filter code
// hlog(LOG_DEBUG, "Bad filter code: %s", filt0);
if (debug)
printf("Bad filter code: %s\n", filt0);
return -1;
}
switch (f0.h.type) {
case 'a':
case 'A':
/* a/latN/lonW/latS/lonE Area filter -- OPTIMIZE! */
f0.h.type = 'a'; // inside area
i = sscanf(filt+1, "/%f/%f/%f/%f%c%c",
&f0.h.f_latN, &f0.h.u2.f_lonW,
&f0.h.u1.f_latS, &f0.h.f_lonE, &dummyc, &dummy2);
if (i == 6 && dummyc == '/' && dummy2 == '-') {
i = 4;
f0.h.type = 'A'; // outside area!
}
if (i == 5 && dummyc == '-') {
i = 4;
f0.h.type = 'A'; // outside area!
}
if (i == 5 && dummyc == '/') {
i = 4;
}
if (i != 4) {
// hlog(LOG_DEBUG, "Bad filter parse: %s", filt0);
if (debug)
printf("Bad filter parse: %s", filt0);
return -1;
}
if (!( -90.01 < f0.h.f_latN && f0.h.f_latN < 90.01)) {
// hlog(LOG_DEBUG, "Bad filter latN value: %s", filt0);
if (debug)
printf("Bad filter latN value: %s", filt0);
return -2;
}
if (!(-180.01 < f0.h.u2.f_lonW && f0.h.u2.f_lonW < 180.01)) {
// hlog(LOG_DEBUG, "Bad filter lonW value: %s", filt0);
if (debug)
printf("Bad filter lonW value: %s", filt0);
return -2;
}
if (!( -90.01 < f0.h.u1.f_latS && f0.h.u1.f_latS < 90.01)) {
// hlog(LOG_DEBUG, "Bad filter latS value: %s", filt0);
if (debug)
printf("Bad filter latS value: %s", filt0);
return -2;
}
if (!(-180.01 < f0.h.f_lonE && f0.h.f_lonE < 180.01)) {
// hlog(LOG_DEBUG, "Bad filter lonE value: %s", filt0);
if (debug)
printf("Bad filter lonE value: %s", filt0);
return -2;
}
if (f0.h.u2.f_lonW > f0.h.f_lonE) {
// wrong way, swap longitudes
float t = f0.h.u2.f_lonW;
f0.h.u2.f_lonW = f0.h.f_lonE;
f0.h.f_lonE = t;
}
if (f0.h.u1.f_latS > f0.h.f_latN) {
// wrong way, swap latitudes
float t = f0.h.u1.f_latS;
f0.h.u1.f_latS = f0.h.f_latN;
f0.h.f_latN = t;
}
// hlog(LOG_DEBUG, "Filter: %s -> A %.3f %.3f %.3f %.3f", filt0, f0.h.f_latN, f0.h.f_lonW, f0.h.f_latS, f0.h.f_lonE);
f0.h.f_latN = filter_lat2rad(f0.h.f_latN);
f0.h.u2.f_lonW = filter_lon2rad(f0.h.u2.f_lonW);
f0.h.u1.f_latS = filter_lat2rad(f0.h.u1.f_latS);
f0.h.f_lonE = filter_lon2rad(f0.h.f_lonE);
break;
case 'b':
case 'B':
/* b/call1/call2... Budlist filter (*) */
i = filter_parse_one_callsignset(ffp, &f0, filt0, MatchWild );
if (i < 0)
return i;
if (i > 0) /* extended previous */
return 0;
break;
case 'd':
case 'D':
/* d/digi1/digi2... Digipeater filter (*) */
i = filter_parse_one_callsignset(ffp, &f0, filt0, MatchWild );
if (i < 0)
return i;
if (i > 0) /* extended previous */
return 0;
break;
#if 0
case 'e':
case 'E':
/* e/call1/call1/... Entry station filter (*) */
i = filter_parse_one_callsignset(ffp, &f0, filt0, MatchWild );
if (i < 0)
return i;
if (i > 0) /* extended previous */
return 0;
break;
#endif
case 'f':
case 'F':
/* f/call/dist Friend's range filter */
i = sscanf(filt+1, "/%9[^/]/%f", f0.h.u5.refcallsign.callsign, &f0.h.u2.f_dist);
// negative distance means "outside this range."
// and makes most sense with overall negative filter!
if (i != 2 || (-0.1 < f0.h.u2.f_dist && f0.h.u2.f_dist < 0.1)) {
// hlog(LOG_DEBUG, "Bad filter parse: %s", filt0);
if (debug)
printf("Bad filter parse: %s", filt0);
return -1;
}
f0.h.u5.refcallsign.callsign[CALLSIGNLEN_MAX] = 0;
f0.h.u5.refcallsign.reflen = strlen(f0.h.u5.refcallsign.callsign);
f0.h.u3.numnames = 0; /* reusing this as "position-cache valid" flag */
// hlog(LOG_DEBUG, "Filter: %s -> F xxx %.3f", filt0, f0.h.u2.f_dist);
/* NOTE: Could do static location resolving at connect time,
** and then use the same way as 'r' range does. The friends
** are rarely moving...
*/
break;
case 'g':
case 'G':
// g/call1/call2/ Group Messaging filter
i = filter_parse_one_callsignset(ffp, &f0, filt0, MatchWild );
if (i < 0)
return i;
if (i > 0) /* extended previous */
return 0;
break;
case 'm':
case 'M':
/* m/dist My range filter */
if (myloc_latstr == NULL) {
printf("The M/radius_km filter requires top-level myloc definition. It doesn't exist.\n");
return -1;
}
f0.h.type = 'r'; // internal implementation at Aprx is a RANGE filter.
f0.h.f_latN = myloc_lat; // radians
f0.h.f_lonE = myloc_lon; // radians
f0.h.u1.f_coslat = myloc_coslat;
i = sscanf(filt+1, "/%f", &f0.h.u2.f_dist);
if (i != 1 || f0.h.u2.f_dist < 0.1) {
// hlog(LOG_DEBUG, "Bad filter parse: %s", filt0);
if (debug)
printf("Bad filter parse: %s", filt0);
return -1;
}
f0.h.u3.numnames = 0; /* reusing this as "position-cache valid" flag */
// hlog(LOG_DEBUG, "Filter: %s -> M %.3f", filt0, f0.h.u2.f_dist);
break;
case 'o':
case 'O':
/* o/obje1/obj2... Object filter (*) */
i = filter_parse_one_callsignset(ffp, &f0, filt0, MatchWild );
if (i < 0)
return i;
if (i > 0) /* extended previous */
return 0;
break;
case 'p':
case 'P':
/* p/aa/bb/cc... Prefix filter
Pass traffic with fromCall that start with aa or bb or cc...
*/
i = filter_parse_one_callsignset(ffp, &f0, filt0, MatchWild );
if (i < 0)
return i;
if (i > 0) /* extended previous */
return 0;
break;
#if 0
case 'q':
case 'Q':
/* q/con/ana q Contruct filter */
s = filt+1;
f0.h.type = 'q';
f0.h.u4.bitflags = 0; /* For QC_* flags */
if (*s++ != '/') {
// hlog(LOG_DEBUG, "Bad q-filter parse: %s", filt0);
if (debug)
printf("Bad q-filter parse: %s", filt0);
return -1;
}
for ( ; *s && *s != '/'; ++s ) {
switch (*s) {
case 'C':
f0.h.u4.bitflags |= QC_C;
break;
case 'X':
f0.h.u4.bitflags |= QC_X;
break;
case 'U':
f0.h.u4.bitflags |= QC_U;
break;
case 'o':
f0.h.u4.bitflags |= QC_o;
break;
case 'O':
f0.h.u4.bitflags |= QC_O;
break;
case 'S':
f0.h.u4.bitflags |= QC_S;
break;
case 'r':
f0.h.u4.bitflags |= QC_r;
break;
case 'R':
f0.h.u4.bitflags |= QC_R;
break;
case 'Z':
f0.h.u4.bitflags |= QC_Z;
break;
case 'I':
f0.h.u4.bitflags |= QC_I;
break;
default:
// hlog(LOG_DEBUG, "Bad q-filter parse: %s", filt0);
if (debug)
printf("Bad q-filter parse: %s", filt0);
return -1;
}
}
if (*s == '/') { /* second format */
++s;
if (*s == 'i' || *s == 'I') {
f0.h.u4.bitflags |= QC_AnalyticsI;
++s;
}
if (*s) {
// hlog(LOG_DEBUG, "Bad q-filter parse: %s", filt0);
if (debug)
printf("Bad q-filter parse: %s", filt0);
return -1;
}
}
break;
#endif
case 'r':
case 'R':
/* r/lat/lon/dist Range filter */
i = sscanf(filt+1, "/%f/%f/%f",
&f0.h.f_latN, &f0.h.f_lonE, &f0.h.u2.f_dist);
// negative distance means "outside this range."
// and makes most sense with overall negative filter!
if (i != 3 || (-0.1 < f0.h.u2.f_dist && f0.h.u2.f_dist < 0.1)) {
// hlog(LOG_DEBUG, "Bad filter parse: %s", filt0);
if (debug)
printf("Bad filter parse: %s", filt0);
return -1;
}
if (!( -90.01 < f0.h.f_latN && f0.h.f_latN < 90.01)) {
// hlog(LOG_DEBUG, "Bad filter lat value: %s", filt0);
if (debug)
printf("Bad filter lat value: %s", filt0);
return -2;
}
if (!(-180.01 < f0.h.f_lonE && f0.h.f_lonE < 180.01)) {
// hlog(LOG_DEBUG, "Bad filter lon value: %s", filt0);
if (debug)
printf("Bad filter lon value: %s", filt0);
return -2;
}
// hlog(LOG_DEBUG, "Filter: %s -> R %.3f %.3f %.3f", filt0, f0.h.f_latN, f0.h.f_lonE, f0.h.u2.f_dist);
f0.h.f_latN = filter_lat2rad(f0.h.f_latN);
f0.h.f_lonE = filter_lon2rad(f0.h.f_lonE);
f0.h.u1.f_coslat = cosf( f0.h.f_latN ); /* Store pre-calculated COS of LAT */
break;
case 's':
case 'S':
/* s/pri/alt/over Symbol filter */
i = filter_parse_one_s( &f0, ffp, filt0 );
if (i < 0) {
// hlog(LOG_DEBUG, "Bad s-filter syntax: %s", filt0);
if (debug)
printf("Bad s-filter syntax: %s", filt0);
return i;
}
if (i > 0) /* extended previous */
return 0;
break;
case 't':
case 'T':
/* t/..............
t/............../call/km
*/
s = filt+1;
f0.h.type = 't';
f0.h.u4.bitflags = 0;
f0.h.u3.numnames = 0; /* reusing this as "position-cache valid" flag */
if (*s++ != '/') {
// hlog(LOG_DEBUG, "Bad filter parse: %s", filt0);
if (debug)
printf("Bad t-filter syntax: %s", filt0);
return -1;
}
for ( ; *s && *s != '/'; ++s ) {
switch (*s) {
case '*':
f0.h.u4.bitflags |= ~T_CWOP; /* "ALL" -- excluding CWOP */
break;
case '3':
f0.h.u4.bitflags |= T_THIRDPARTY;
break;
case 'c': case 'C':
f0.h.u4.bitflags |= T_CWOP;
break;
case 'i': case 'I':
f0.h.u4.bitflags |= T_ITEM;
break;
case 'm': case 'M':
f0.h.u4.bitflags |= T_MESSAGE;
break;
case 'n': case 'N':
f0.h.u4.bitflags |= T_NWS;
break;
case 'o': case 'O':
f0.h.u4.bitflags |= T_OBJECT;
break;
case 'p': case 'P':
f0.h.u4.bitflags |= T_POSITION;
break;
case 'q': case 'Q':
f0.h.u4.bitflags |= T_QUERY;
break;
case 's': case 'S':
f0.h.u4.bitflags |= T_STATUS;
break;
case 't': case 'T':
f0.h.u4.bitflags |= T_TELEMETRY;
break;
case 'u': case 'U':
f0.h.u4.bitflags |= T_USERDEF;
break;
case 'w': case 'W':
f0.h.u4.bitflags |= T_WX;
break;
default:
// hlog(LOG_DEBUG, "Bad filter parse: %s", filt0);
if (debug)
printf("Bad t-filter syntax: %s", filt0);
return -1;
}
}
if (*s == '/' && s[1] != 0) { /* second format */
i = sscanf(s, "/%9[^/]/%f%c", f0.h.u5.refcallsign.callsign, &f0.h.u2.f_dist, &dummyc);
// negative distance means "outside this range."
// and makes most sense with overall negative filter!
if ( i != 2 || (-0.1 < f0.h.u2.f_dist && f0.h.u2.f_dist < 0.1) || /* 0.1 km minimum radius */
strlen(f0.h.u5.refcallsign.callsign) < CALLSIGNLEN_MIN ) {
// hlog(LOG_DEBUG, "Bad filter parse: %s", filt0);
if (debug)
printf("Bad t-filter parse: %s", filt0);
return -1;
}
f0.h.u5.refcallsign.callsign[CALLSIGNLEN_MAX] = 0;
f0.h.u5.refcallsign.reflen = strlen(f0.h.u5.refcallsign.callsign);
f0.h.type = 'T'; /* two variants... */
}
break;
case 'u':
case 'U':
/* u/unproto1/unproto2... Unproto filter (*) */
i = filter_parse_one_callsignset(ffp, &f0, filt0, MatchWild );
if (i < 0)
return i;
if (i > 0) /* extended previous */
return 0;
break;
default:;
/* No pre-parsers for other types */
// hlog(LOG_DEBUG, "Filter: %s", filt0);
if (debug)
printf("Bad filter code: %s\n", filt0);
return -1;
break;
}
// if (!c) return 0; /* Just a verification scan, not actual fill in parse */
/* OK, pre-parsing produced accepted result */
#ifndef _FOR_VALGRIND_
f = cellmalloc(filter_cells);
if (!f) return -1;
*f = f0; /* store pre-parsed values */
if (strlen(filt0) < FILT_TEXTBUFSIZE) {
strcpy(f->textbuf, filt0);
f->h.text = f->textbuf;
} else
f->h.text = strdup(filt0); /* and copy of filter text */
#else
f = calloc(1, sizeof(*f) + strlen(filt0));
*f = f0; /* store pre-parsed values */
f->h.text = f->textbuf;
strcpy(f->textbuf, filt); /* and copy of filter text */
#endif
/* hlog(LOG_DEBUG, "parsed filter: t=%c n=%d '%s'", f->h.type, f->h.negation, f->h.text); */
/* link to the tail.. */
if (ff)
ffp = &ff->h.next;
*ffp = f;
return 0;
}
/* Discard the defined filter chain */
void filter_free(struct filter_t *f)
{
struct filter_t *fnext;
for ( ; f ; f = fnext ) {
fnext = f->h.next;
/* If not pointer to internal string, free it.. */
#ifndef _FOR_VALGRIND_
if (f->h.text != f->textbuf)
free((void*)(f->h.text));
cellfree(filter_cells, f);
#else
free(f);
#endif
}
}
/*
#
# Input: This[La] Source Latitude, in radians
# This[Lo] Source Longitude, in radians
# That[La] Destination Latitude, in radians
# That[Lo] Destination Longitude, in radians
# Output: R[s] Distance, in kilometers
#
function maidenhead_km_distance($This, $That) {
#Haversine Formula (from R.W. Sinnott, "Virtues of the Haversine",
#Sky and Telescope, vol. 68, no. 2, 1984, p. 159):
$dlon = $That[Lo] - $This[Lo];
$dlat = $That[La] - $This[La];
$sinDlat2 = sin($dlat/2);
$sinDlon2 = sin($dlon/2);
$a = ($sinDlat2 * $sinDlat2 +
cos($This[La]) * cos($That[La]) * $sinDlon2 * $sinDlon2);
# The Haversine Formula can be expressed in terms of a two-argument
# inverse tangent function, atan2(y,x), instead of an inverse sine
# as follows (no bulletproofing is needed for an inverse tangent):
$c = 2.0 * atan2( sqrt($a), sqrt(1.0-$a) );
# $d = R * $c ; # Radius of ball times angle [radians] ...
$R[s] = rad2deg($c) * 111.2;
return($R);
}
*/
static float maidenhead_km_distance(float lat1, float coslat1, float lon1, float lat2, float coslat2, float lon2)
{
float sindlat2 = sinf((lat1 - lat2) * 0.5);
float sindlon2 = sinf((lon1 - lon2) * 0.5);
float a = (sindlat2 * sindlat2 +
coslat1 * coslat2 * sindlon2 * sindlon2);
float c = 2.0 * atan2f( sqrtf(a), sqrtf(1.0 - a));
return ((111.2 * 180.0 / M_PI) * c);
}
/*
*
* http://www.aprs-is.net/javaprssrvr/javaprsfilter.htm
*
*/
static int filter_process_one_a(struct pbuf_t *pb, struct filter_t *f)
{
/* a/latN/lonW/latS/lonE Area filter
The area filter works the same as range filter but the filter
is defined as a box of coordinates. The coordinates can also
been seen as upper left coordinate and lower right. Lat/lon
are decimal degrees. South and west are negative.
Multiple area filters can be defined at the same time.
Messages addressed to stations within the area are also passed.
(by means of aprs packet parse finding out the location..)
50-70 instances in APRS-IS core at any given time.
Up to 2500 invocations per second.
*/
;
if (!(pb->flags & F_HASPOS)) /* packet with a position.. (msgs with RECEIVER's position) */
return 0;
if ((pb->lat <= f->h.f_latN) &&
(pb->lat >= f->h.u1.f_latS) &&
(pb->lng <= f->h.f_lonE) && /* East POSITIVE ! */
(pb->lng >= f->h.u2.f_lonW)) {
/* Inside the box */
return f->h.negation ? 2 : 1;
} else if (f->h.type == 'A') {
/* Outside the box */
return f->h.negation ? 2 : 1;
}
return 0;
}
static int filter_process_one_b(struct pbuf_t *pb, struct filter_t *f)
{
/* b/call1/call2... Budlist filter
Pass all traffic FROM exact call: call1, call2, ...
(* wild card allowed)
50/70 instances in APRS-IS core at any given time.
Up to 2500 invocations per second.
*/
struct filter_refcallsign_t ref;
int i = pb->srccall_end - pb->data;
if (i > CALLSIGNLEN_MAX) i = CALLSIGNLEN_MAX;
/* source address "addr">... */
memset( &ref, 0, sizeof(ref) ); // clear it all
memcpy( ref.callsign, pb->data, i);
return filter_match_on_callsignset(&ref, i, f, MatchWild);
}
static int filter_process_one_d(struct pbuf_t *pb, struct filter_t *f)
{
/* d/digi1/digi2... Digipeater filter
The digipeater filter will match all packets that have been
digipeated by a particular station(s) (the station's call
is in the path). This filter allows the * wildcard.
25-35 instances in APRS-IS core at any given time.
Up to 1300 invocations per second.
*/
struct filter_refcallsign_t ref;
const char *d = pb->srccall_end + 1 + pb->dstcall_len + 1; /* viacall start */
const char *q = pb->qconst_start-1;
int rc, i, j = 0;
// hlog( LOG_INFO, "digifilter: '%.*s' -> '%.*s' q-d=%d",
// (int)(pb->packet_len < 50 ? pb->packet_len : 50),
// pb->data, (int)i, d, (int)(q-d) );
for (i = 0; d < q; ) {
++j;
if (j > 10) break; // way too many callsigns... (code bug?)
if (*d == ':') break; //end of via fields
if (*d == ',') ++d; // second round and onwards..
for (i = 0; i+d <= q && i <= CALLSIGNLEN_MAX; ++i) {
if ((d[i] == ',') || (d[i] == ':'))
break;
}
// hlog(LOG_INFO, "d: -> (%d,%d) '%.*s'", (int)(d-pb->data), i, i, d);
// digipeater address ",addr,"
memset( &ref, 0, sizeof(ref) ); // clear it all
memcpy( ref.callsign, d, i);
if (i > CALLSIGNLEN_MAX) i = CALLSIGNLEN_MAX;
rc = filter_match_on_callsignset(&ref, i, f, MatchWild);
if (rc) {
return (rc);
}
d += i;
}
return 0;
}
#if 0
static int filter_process_one_e(struct pbuf_t *pb, struct filter_t *f)
{
/* e/call1/call1/... Entry station filter
This filter matches all packets with the specified
callsign-SSID(s) immediately following the q construct.
This allows filtering based on receiving IGate, etc.
Supports * wildcard.
2-6 instances in APRS-IS core at any given time.
Up to 200 invocations per second.
*/
struct filter_refcallsign_t ref;
const char *e = pb->qconst_start+4;
int i = pb->entrycall_len;
if (i < 1) /* should not happen.. */
return 0; /* Bad Entry-station callsign */
/* entry station address "qA*,addr," */
memset( &ref, 0, sizeof(ref) ); // clear it all
memcpy( ref.callsign, e, i);
return filter_match_on_callsignset(&ref, i, f, MatchWild);
}
#endif
#ifndef DISABLE_IGATE
static int filter_process_one_f(struct pbuf_t *pb, struct filter_t *f, historydb_t *historydb)
{
/* f/call/dist Friend Range filter
This is the same as the range filter except that the center is
defined as the last known position of call.
Multiple friend filters can be defined at the same time.
Messages addressed to stations within the range are also passed.
(by means of aprs packet parse finding out the location..)
NOTE: Could do static location resolving at connect time,
and then use the same way as 'r' range does. The friends
are rarely moving...
15-25 instances in APRS-IS core at any given time.
Up to 900 invocations per second.
Caching the historydb_lookup() result will lower CPU power
spent on the historydb.
*/
history_cell_t *history;
float r;
float lat1, lon1, coslat1;
float lat2, lon2, coslat2;
const char *callsign = f->h.u5.refcallsign.callsign;
int i = f->h.u5.refcallsign.reflen;
if (!(pb->flags & F_HASPOS)) { /* packet with a position.. (msgs with RECEIVER's position) */
if (debug) printf("f-filter: no position -> return 0\n");
return 0; /* No position data... */
}
/* find friend's last location packet */
if (f->h.hist_age < tick.tv_sec) {
history = historydb_lookup( historydb, callsign, i );
f->h.hist_age = tick.tv_sec + hist_lookup_interval;
if (!history) {
if (debug) printf("f-filter: no history lookup result (%.*s) -> return 0\n", i, callsign );
return 0; /* no lookup result.. */
}
f->h.u3.numnames = 1;
f->h.f_latN = history->lat;
f->h.f_lonE = history->lon;
f->h.u1.f_coslat = history->coslat;
}
if (!f->h.u3.numnames) {
if (debug) printf("f-filter: no history lookup result (numnames == 0) -> return 0\n");
return 0; /* histdb lookup cache invalid */
}
lat1 = f->h.f_latN;
lon1 = f->h.f_lonE;
coslat1 = f->h.u1.f_coslat;
lat2 = pb->lat;
lon2 = pb->lng;
coslat2 = pb->cos_lat;
r = maidenhead_km_distance(lat1, coslat1, lon1, lat2, coslat2, lon2);
if (debug) printf("f-filter: r=%.1f km\n", r);
if (f->h.u2.f_dist < 0.0) {
// Test for _outside_ the range
if (r > -f->h.u2.f_dist) /* Range is more than given limit */
return (f->h.negation) ? 2 : 1;
} else {
// Test for _inside_ the range
if (r < f->h.u2.f_dist) /* Range is less than given limit */
return (f->h.negation) ? 2 : 1;
}
return 0;
}
#endif
static int filter_process_one_g(struct pbuf_t *pb, struct filter_t *f)
{
/* g/call1/call2... Group Messaging filter
Pass all message traffic TO calls call1/call2/...
(* wild card allowed)
*/
struct filter_refcallsign_t ref;
int i = pb->dstname_len;
if (i > CALLSIGNLEN_MAX) i = CALLSIGNLEN_MAX;
/* source address "addr">... */
memset( &ref, 0, sizeof(ref) ); // clear it all
memcpy( ref.callsign, pb->dstname, i);
return filter_match_on_callsignset(&ref, i, f, MatchWild);
}
#if 0 // No M filter implementation, but there is M filter parse producing R filter..
static int filter_process_one_m(struct pbuf_t *pb, struct filter_t *f)
{
/* m/dist My Range filter
This is the same as the range filter except that the center is
defined as the last known position of the logged in client.
Messages addressed to stations within the range are also passed.
(by means of aprs packet parse finding out the location..)
NOTE: MY RANGE is rarely moving, once there is a positional
fix, it could stay fixed...
80-120 instances in APRS-IS core at any given time.
Up to 4200 invocations per second.
Caching the historydb_lookup() result will lower CPU power
spent on the historydb.
At Aprx: Implemented using Range filter, and prepared at parse time..
*/
float lat2, lon2, coslat2;
float r;
if (!(pb->flags & F_HASPOS)) /* packet with a position.. (msgs with RECEIVER's position) */
return 0;
lat2 = pb->lat;
lon2 = pb->lng;
coslat2 = pb->cos_lat;
r = maidenhead_km_distance(myloc_lat, myloc_coslat, myloc_lon, lat2, coslat2, lon2);
if (f->h.u2.f_dist < 0.0) {
// Test for _outside_ the range
if (r > -f->h.u2.f_dist) /* Range is more than given limit */
return (f->h.negation) ? 2 : 1;
} else {
// Test for _inside_ the range
if (r < f->h.u2.f_dist) /* Range is less than given limit */
return (f->h.negation) ? 2 : 1;
}
return 0;
}
#endif
static int filter_process_one_o(struct pbuf_t *pb, struct filter_t *f)
{
/* o/obj1/obj2... Object filter
Pass all objects with the exact name of obj1, obj2, ...
(* wild card allowed)
PROBABLY ALSO ITEMs
Usage frequency: 0.2%
.. 2 cases in entire APRS-IS core at any time.
About 50-70 invocations per second at peak.
*/
struct filter_refcallsign_t ref;
int i;
// const char *s;
if ( (pb->packettype & (T_OBJECT|T_ITEM)) == 0 ) { /* not an Object NOR Item */
if (debug) printf("o-filter: packet type not OBJECT nor ITEM\n");
return 0;
}
/* parse_aprs() has picked item/object name pointer and length.. */
// s = pb->srcname;
i = pb->srcname_len;
if (i < 1) {
if (debug) printf("o-filter: object/item name length < 1 at the packet\n");
return 0; /* Bad object/item name */
}
/* object name */
memset( &ref, 0, sizeof(ref) ); // clear it all
memcpy( ref.callsign, pb->srcname, i); // copy the interesting part
return filter_match_on_callsignset(&ref, i, f, MatchWild);
}
static int filter_process_one_p(struct pbuf_t *pb, struct filter_t *f)
{
/* p/aa/bb/cc... Prefix filter
Pass traffic with fromCall that start with aa or bb or cc...
Usage frequency: 14.4%
.. 80-100 cases in entire APRS-IS core at any time.
Up to 3500 invocations per second at peak.
*/
struct filter_refcallsign_t ref;
int i = pb->srccall_end - pb->data;
if (i > CALLSIGNLEN_MAX) i = CALLSIGNLEN_MAX;
/* source address "addr">... */
memset( &ref, 0, sizeof(ref) ); // clear it all
memcpy( ref.callsign, pb->data, i);
return filter_match_on_callsignset(&ref, i, f, MatchPrefix);
}
#if 0
static int filter_process_one_q(struct pbuf_t *pb, struct filter_t *f)
{
/* q/con/ana q Contruct filter
q = q Construct command
con = list of q Construct to pass (case sensitive)
ana = analysis based on q Construct.
I = Pass positions from IGATES identified by qAr or qAR.
For example:
q/C Pass all traffic with qAC
q/rR Pass all traffic with qAr or qAR
q//I Pass all position packets from IGATES identified
in other packets by qAr or qAR
Usage frequency: 0.4%
.. 2-6 cases in entire APRS-IS core at any time.
Up to 200 invocations per second at peak.
*/
const char *e = pb->qconst_start+2;
int mask;
switch (*e) {
case 'C':
mask = QC_C;
break;
case 'X':
mask = QC_X;
break;
case 'U':
mask = QC_U;
break;
case 'o':
mask = QC_o;
break;
case 'O':
mask = QC_O;
break;
case 'S':
mask = QC_S;
break;
case 'r':
mask = QC_r;
break;
case 'R':
mask = QC_R;
break;
case 'Z':
mask = QC_Z;
break;
case 'I':
mask = QC_I;
break;
default:
return 0; /* Should not happen... */
break;
}
if (f->h.u4.bitflags & mask) {
/* Something matched! */
return 1;
}
if (f->h.u4.bitflags & QC_AnalyticsI) {
/* Oh ? Analytical!
Has it ever been accepted into entry-igate database ? */
if (filter_entrycall_lookup(pb))
return 1; /* Found on entry-igate database! */
}
return 0; /* No match */
}
#endif
static int filter_process_one_r(struct pbuf_t *pb, struct filter_t *f)
{
/* r/lat/lon/dist Range filter
Pass posits and objects within dist km from lat/lon.
lat and lon are signed degrees, i.e. negative for West/South
and positive for East/North.
Multiple range filters can be defined at the same time.
Messages addressed to stations within the range are also passed.
(by means of aprs packet parse finding out the location..)
About 120-150 r-filters in entire APRS-IS core at any given time.
Up to 5200 invocations per second at peak.
*/
float lat1 = f->h.f_latN;
float lon1 = f->h.f_lonE;
float coslat1 = f->h.u1.f_coslat;
float r;
float lat2, lon2, coslat2;
if (!(pb->flags & F_HASPOS)) {
/* packet with a position..
(msgs with RECEIVER's position) */
return 0;
}
lat2 = pb->lat;
lon2 = pb->lng;
coslat2 = pb->cos_lat;
r = maidenhead_km_distance(lat1, coslat1, lon1, lat2, coslat2, lon2);
if (f->h.u2.f_dist < 0.0) {
// Test for _outside_ the range
if (r > -f->h.u2.f_dist) /* Range is more than given limit */
return (f->h.negation) ? 2 : 1;
} else {
// Test for _inside_ the range
if (r < f->h.u2.f_dist) /* Range is less than given limit */
return (f->h.negation) ? 2 : 1;
}
return 0;
}
static int filter_process_one_s(struct pbuf_t *pb, struct filter_t *f)
{
/* 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 symtable = (pb->symbol[0] == '/') ? '/' : '\\';
const char symcode = pb->symbol[1];
const char symolay = (pb->symbol[0] != symtable) ? pb->symbol[0] : 0;
// hlog( LOG_DEBUG, "s-filt %c|%c|%c %s", symtable, symcode, symolay ? symolay : '-', f->h.text );
if (f->h.u4.len1 != 0) {
/* Primary table symbols */
if ( symtable == '/' &&
memchr(f->h.text+f->h.u3.len1s, symcode, f->h.u4.len1) != NULL )
return f->h.negation ? 2 : 1;
// return 0;
}
if (f->h.u5.lens.len3 != 0) {
/* Secondary table with overlay */
if ( memchr(f->h.text+f->h.u5.lens.len3s, symolay, f->h.u5.lens.len3) == NULL )
return 0; // No match on overlay
if ( memchr(f->h.text+f->h.u5.lens.len2s, symcode, f->h.u5.lens.len2) == NULL )
return 0; // No match on overlay
return f->h.negation ? 2 : 1;
}
/* OK, no overlay... */
if (f->h.u5.lens.len2 != 0) {
/* Secondary table symbols */
if ( symtable != '/' &&
memchr(f->h.text+f->h.u5.lens.len2s, symcode, f->h.u5.lens.len2) != NULL )
return f->h.negation ? 2 : 1;
}
/* No match */
return 0;
}
static int filter_process_one_t(struct pbuf_t *pb, struct filter_t *f, historydb_t *historydb)
{
/* [-]t/poimntqsu3*c
[-]t/poimntqsu3*c/call/km
Type filter Pass all traffic based on packet type.
One or more types can be defined at the same time, t/otq
is a valid definition.
c = CWOP (local extension)
* = ALL (local extension)
i = Items
m = Message
n = NWS Weather & Weather Objects
o = Objects
p = Position packets
q = Query
s = Status
t = Telemetry
u = User-defined
w = Weather
3 = 3rd party frame
Note: The weather type filter also passes positions packets
for positionless weather packets.
The second format allows putting a radius limit around "call"
(station callsign-SSID or object name) for the requested station
types.
About 40-60 s-filters in entire APRS-IS core at any given time.
Up to 2100 invocations per second at peak.
For the second format perhaps 2-3 in APRS-IS at any time.
(mapping to 60-100 invocations per second)
Usage examples:
-t/c Everything except CWOP
t/.*./OH2RDY/50 Everything within 50 km of OH2RDY's last known position
("." is dummy addition for C comments..)
*/
int rc = 0;
if (pb->packettype & f->h.u4.bitflags) /* u4.bitflags as comparison bitmask */
rc = 1;
#if 0
if (!rc && (f->h.u4.bitflags & T_WX) && (pb->flags & F_HASPOS)) {
/* "Note: The weather type filter also passes positions packets
// for positionless weather packets."
//
// 1) recognize positionless weather packets
// 2) register their source callsigns, do this in input_parse
// 3) when filtering for weather data, check non-weather
// recognized packets against the database in point 2
// 4) pass on packets matching point 3
*/
rc = filter_wx_lookup(pb);
}
#endif
/* Either it stops here, or it continues... */
if (rc && f->h.type == 'T') { /* Within a range of callsign ?
* Rather rare.. perhaps 2-3 in APRS-IS.
*/
float range, r;
float lat1, lon1, coslat1;
float lat2, lon2, coslat2;
#ifndef DISABLE_IGATE
const char *callsign = f->h.u5.refcallsign.callsign;
const int callsignlen = f->h.u5.refcallsign.reflen;
history_cell_t *history;
#endif
/* hlog(LOG_DEBUG, "Type filter with callsign range used! '%s'", f->h.text); */
if (!(pb->flags & F_HASPOS)) /* packet with a position.. (msgs with RECEIVER's position) */
return 0; /* No positional data.. */
range = f->h.u2.f_dist;
/* So.. Now we have a callsign, and we have range.
Lets find callsign's location, and range to that item..
.. 60-100 lookups per second. */
#ifndef DISABLE_IGATE
if (f->h.hist_age < tick.tv_sec) {
history = historydb_lookup( historydb, callsign, callsignlen );
/* hlog( LOG_DEBUG, "Type filter with callsign range used! call='%s', range=%.1f position %sfound",
// callsign, range, i ? "" : "not ");
*/
if (!history) return 0; /* no lookup result.. */
f->h.u3.numnames = 1;
f->h.hist_age = tick.tv_sec + hist_lookup_interval;
f->h.f_latN = history->lat;
f->h.f_lonE = history->lon;
f->h.u1.f_coslat = history->coslat;
}
#endif
if (!f->h.u3.numnames) return 0; /* No valid data at range center position cache */
lat1 = f->h.f_latN;
lon1 = f->h.f_lonE;
coslat1 = f->h.u1.f_coslat;
lat2 = pb->lat;
lon2 = pb->lng;
coslat2 = pb->cos_lat;
r = maidenhead_km_distance(lat1, coslat1, lon1, lat2, coslat2, lon2);
if (range < 0.0) {
// Test for _outside_ the range
if (r > -range) /* Range is more than given limit */
return (f->h.negation) ? 2 : 1;
} else {
// Test for _inside_ the range
if (r < range) /* Range is less than given limit */
return (f->h.negation) ? 2 : 1;
}
return 0; /* unimplemented! */
}
return (f->h.negation ? (rc+rc) : rc);
}
static int filter_process_one_u(struct pbuf_t *pb, struct filter_t *f)
{
/* u/unproto1/unproto2/... Unproto filter
This filter passes all packets with the specified destination
callsign-SSID(s) (also known as the To call or unproto call).
Supports * wild card.
Seen hardly ever in APRS-IS core, some rare instances in Tier-2.
*/
struct filter_refcallsign_t ref;
const char *d = pb->srccall_end+1;
int i;
i = pb->dstcall_len;
if (i > CALLSIGNLEN_MAX) i = CALLSIGNLEN_MAX;
/* hlog( LOG_INFO, "unproto: '%.*s' -> '%.*s'",
// (int)(pb->packet_len < 30 ? pb->packet_len : 30), pb->data, (int)i, d);
*/
/* destination address ">addr," */
memset( &ref, 0, sizeof(ref) ); // clear it all
memcpy( ref.callsign, d, i);
return filter_match_on_callsignset(&ref, i, f, MatchWild);
}
static int filter_process_one(struct pbuf_t *pb, struct filter_t *f, historydb_t *historydb)
{
int rc = 0;
if (debug>1) printf("filter_process_one() type=%c '%s'\n",f->h.type, f->h.text);
switch (f->h.type) {
case 'a':
case 'A':
rc = filter_process_one_a(pb, f);
break;
case 'b':
case 'B':
rc = filter_process_one_b(pb, f);
break;
case 'd':
case 'D':
rc = filter_process_one_d(pb, f);
break;
#if 0
case 'e':
case 'E':
rc = filter_process_one_e(pb, f);
break;
#endif
#ifndef DISABLE_IGATE
case 'f':
case 'F':
rc = filter_process_one_f(pb, f, historydb);
break;
#endif
case 'g':
case 'G':
rc = filter_process_one_g(pb, f);
break;
#if 0 // these are compiled as R filters, no M filters exist internally
case 'm':
case 'M':
rc = filter_process_one_m(pb, f);
break;
#endif
case 'o':
case 'O':
rc = filter_process_one_o(pb, f);
break;
case 'p':
case 'P':
rc = filter_process_one_p(pb, f);
break;
#if 0
case 'q':
case 'Q':
rc = filter_process_one_q(pb, f);
break;
#endif
case 'r':
case 'R':
rc = filter_process_one_r(pb, f);
break;
case 's':
case 'S':
rc = filter_process_one_s(pb, f);
break;
case 't':
case 'T':
rc = filter_process_one_t(pb, f, historydb);
break;
case 'u':
case 'U':
rc = filter_process_one_u(pb, f);
break;
default:
rc = -1;
break;
}
// hlog(LOG_DEBUG, "filter '%s' rc=%d", f->h.text, rc);
return rc;
}
int filter_process(struct pbuf_t *pb, struct filter_t *f, historydb_t *historydb)
{
int seen_accept = 0;
for ( ; f; f = f->h.next ) {
int rc = filter_process_one(pb, f, historydb);
/* no reports to user about bad filters.. */
if (rc == 1)
seen_accept = 1;
else if (rc == 2)
return -1;
/* "2" reply means: "match, but don't pass.." */
}
return seen_accept;
}
|