Skip to content

Commit

Permalink
[SQUASH ME] fixed prefix search
Browse files Browse the repository at this point in the history
  • Loading branch information
BytesGalore authored and BytesGalore committed Dec 9, 2015
1 parent dc04291 commit fb45f8c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 30 deletions.
59 changes: 29 additions & 30 deletions sys/universal_address/universal_address.c
Original file line number Diff line number Diff line change
Expand Up @@ -196,48 +196,47 @@ int universal_address_compare(universal_address_container_t *entry,
return ret;
}

/* Get the index of the first trailing `0` (indicates a prefix) */
int i = 0;
for( i = entry->address_size-1; i > 0; --i) {
if( entry->address[i] != 0 ) {
/* compare up to fist distinct byte, pretty clumsy method for now */
int idx = -1;
bool test_all_zeros = true;
for( size_t i = 0; i < entry->address_size; i++) {
if ( (idx == -1) && (entry->address[i] != addr[i]) ) {
idx = i;
}
if ( test_all_zeros ) {
test_all_zeros = (entry->address[i] == 0);
}
if ( (idx != -1) && !test_all_zeros ) {
break;
}
}

/* if the address is all 0 its most probably the default gateway address */
if ((i == 0) && (entry->address[i] == 0)) {
/* if the address is all 0 its a default route address */
if ( test_all_zeros ) {
*addr_size_in_bits = 0;
mutex_unlock(&mtx_access);
return 2;
}

if( memcmp(entry->address, addr, i) == 0 ) {
/* if the bytes-1 equals we check the bits of the lowest byte */
uint8_t bitmask = 0x00;
uint8_t j = 0;
/* get a bitmask for the trailing 0b */
for( ; j < 8; ++j ) {
if ( (entry->address[i] >> j) & 0x01 ) {
bitmask = 0xff << j;
break;
}
}
if( (entry->address[i] & bitmask) == (addr[i] & bitmask) ) {
ret = entry->address[i] != addr[i];
*addr_size_in_bits = (i<<3) + (8-j);
if( ret == 0 ) {
/* check if the remaining bits from addr are significant */
i++;
for(; i < entry->address_size; ++i) {
if( addr[i] != 0 ) {
ret = 1;
break;
}
}
}
/* if we have no distinct bytes the addresses are equal */
if (idx == -1) {
mutex_unlock(&mtx_access);
return 0;
}

/* count equal bits */
uint8_t xor = entry->address[idx]^addr[idx];
int8_t j = 7;
for( ; j > 0; --j ) {
if ( (xor >> j) & 0x01 ) {
break;
}
}

/* get the total number of matching bits */
*addr_size_in_bits = (idx << 3) + j;
ret = 1;

mutex_unlock(&mtx_access);
return ret;
}
Expand Down
3 changes: 3 additions & 0 deletions tests/unittests/tests-fib/tests-fib.c
Original file line number Diff line number Diff line change
Expand Up @@ -664,8 +664,10 @@ static void test_fib_16_prefix_match(void)
/* test fail */
addr_dst[14] = (char)0x3c; /* 0011 1100 */
addr_lookup[14] = (char)0x34; /* 0011 0100 */
addr_lookup[13] += 1;
add_buf_size = 16;
prefix_len = _get_prefix_bits_num(addr_dst, strlen(addr_dst));

fib_add_entry(&test_fib_table, 42, (uint8_t *)addr_dst,
add_buf_size - 1, ((prefix_len << FIB_FLAG_NET_PREFIX_SHIFT) | 0x123),
(uint8_t *)addr_nxt, add_buf_size -
Expand All @@ -681,6 +683,7 @@ static void test_fib_16_prefix_match(void)

/* test success (again) by adjusting the lsb */
addr_lookup[14] = (char)0x3e; /* 0011 1110 */
addr_lookup[13] -= 1;
add_buf_size = 16;

memset(addr_nxt, 0, add_buf_size);
Expand Down

0 comments on commit fb45f8c

Please sign in to comment.