Avoid false positives for address 000000 in the icao filter hashtable

This commit is contained in:
Oliver Jowett 2020-10-08 19:30:06 +08:00
parent 66f26846b8
commit f0055c978d
1 changed files with 16 additions and 14 deletions

View File

@ -35,6 +35,8 @@ static uint32_t icao_filter_a[ICAO_FILTER_SIZE];
static uint32_t icao_filter_b[ICAO_FILTER_SIZE];
static uint32_t *icao_filter_active;
#define EMPTY 0xFFFFFFFF
static uint32_t icaoHash(uint32_t a)
{
// Jenkins one-at-a-time hash, unrolled for 3 bytes
@ -61,8 +63,8 @@ static uint32_t icaoHash(uint32_t a)
void icaoFilterInit()
{
memset(icao_filter_a, 0, sizeof(icao_filter_a));
memset(icao_filter_b, 0, sizeof(icao_filter_b));
memset(icao_filter_a, 0xFF, sizeof(icao_filter_a));
memset(icao_filter_b, 0xFF, sizeof(icao_filter_b));
icao_filter_active = icao_filter_a;
}
@ -70,26 +72,26 @@ void icaoFilterAdd(uint32_t addr)
{
uint32_t h, h0;
h0 = h = icaoHash(addr);
while (icao_filter_active[h] && icao_filter_active[h] != addr) {
while (icao_filter_active[h] != EMPTY && icao_filter_active[h] != addr) {
h = (h+1) & (ICAO_FILTER_SIZE-1);
if (h == h0) {
fprintf(stderr, "ICAO hash table full, increase ICAO_FILTER_SIZE\n");
return;
}
}
if (!icao_filter_active[h])
if (icao_filter_active[h] == EMPTY)
icao_filter_active[h] = addr;
// also add with a zeroed top byte, for handling DF20/21 with Data Parity
h0 = h = icaoHash(addr & 0x00ffff);
while (icao_filter_active[h] && (icao_filter_active[h] & 0x00ffff) != (addr & 0x00ffff)) {
while (icao_filter_active[h] != EMPTY && (icao_filter_active[h] & 0x00ffff) != (addr & 0x00ffff)) {
h = (h+1) & (ICAO_FILTER_SIZE-1);
if (h == h0) {
fprintf(stderr, "ICAO hash table full, increase ICAO_FILTER_SIZE\n");
return;
}
}
if (!icao_filter_active[h])
if (icao_filter_active[h] == EMPTY)
icao_filter_active[h] = addr;
}
@ -98,7 +100,7 @@ int icaoFilterTest(uint32_t addr)
uint32_t h, h0;
h0 = h = icaoHash(addr);
while (icao_filter_a[h] && icao_filter_a[h] != addr) {
while (icao_filter_a[h] != EMPTY && icao_filter_a[h] != addr) {
h = (h+1) & (ICAO_FILTER_SIZE-1);
if (h == h0)
break;
@ -107,7 +109,7 @@ int icaoFilterTest(uint32_t addr)
return 1;
h = h0;
while (icao_filter_b[h] && icao_filter_b[h] != addr) {
while (icao_filter_b[h] != EMPTY && icao_filter_b[h] != addr) {
h = (h+1) & (ICAO_FILTER_SIZE-1);
if (h == h0)
break;
@ -124,21 +126,21 @@ uint32_t icaoFilterTestFuzzy(uint32_t partial)
partial &= 0x00ffff;
h0 = h = icaoHash(partial);
while (icao_filter_a[h] && (icao_filter_a[h] & 0x00ffff) != partial) {
while (icao_filter_a[h] != EMPTY && (icao_filter_a[h] & 0x00ffff) != partial) {
h = (h+1) & (ICAO_FILTER_SIZE-1);
if (h == h0)
break;
}
if ((icao_filter_a[h] & 0x00ffff) == partial)
if (icao_filter_a[h] != EMPTY && (icao_filter_a[h] & 0x00ffff) == partial)
return icao_filter_a[h];
h = h0;
while (icao_filter_b[h] && (icao_filter_b[h] & 0x00ffff) != partial) {
while (icao_filter_b[h] != EMPTY && (icao_filter_b[h] & 0x00ffff) != partial) {
h = (h+1) & (ICAO_FILTER_SIZE-1);
if (h == h0)
break;
}
if ((icao_filter_b[h] & 0x00ffff) == partial)
if (icao_filter_b[h] != EMPTY && (icao_filter_b[h] & 0x00ffff) == partial)
return icao_filter_b[h];
return 0;
@ -152,10 +154,10 @@ void icaoFilterExpire()
if (now >= next_flip) {
if (icao_filter_active == icao_filter_a) {
memset(icao_filter_b, 0, sizeof(icao_filter_b));
memset(icao_filter_b, 0xFF, sizeof(icao_filter_b));
icao_filter_active = icao_filter_b;
} else {
memset(icao_filter_a, 0, sizeof(icao_filter_a));
memset(icao_filter_a, 0xFF, sizeof(icao_filter_a));
icao_filter_active = icao_filter_a;
}
next_flip = now + MODES_ICAO_FILTER_TTL;