diff --git a/sys/include/net/ng_fib.h b/sys/include/net/ng_fib.h index 751e2ac5cf011..17cced91807a0 100644 --- a/sys/include/net/ng_fib.h +++ b/sys/include/net/ng_fib.h @@ -57,6 +57,11 @@ typedef struct fib_destination_set_entry_t { */ #define FIB_LIFETIME_NO_EXPIRE (0xFFFFFFFF) +/** + * @brief flag to identify if the FIB-Entry is a net prefix (MSB == 1) + */ +#define FIB_FLAG_NET_PREFIX (1<<31) + /** * @brief initializes all FIB entries with 0 */ diff --git a/sys/net/network_layer/fib/fib.c b/sys/net/network_layer/fib/fib.c index 9b1eeded7b64f..9ba9d3d0791c9 100644 --- a/sys/net/network_layer/fib/fib.c +++ b/sys/net/network_layer/fib/fib.c @@ -149,15 +149,16 @@ static int fib_find_entry(uint8_t *dst, size_t dst_size, } else { /* we try to find the most fitting prefix */ - if (ret_comp == 1) { + if ((ret_comp == 1) + && (fib_table[i].global_flags & FIB_FLAG_NET_PREFIX) + && match_size > prefix_size) { entry_arr[0] = &(fib_table[i]); /* we could find a better one so we move on */ ret = 0; - prefix_size = match_size; - match_size = dst_size<<3; count = 1; } + match_size = dst_size<<3; } } } @@ -639,8 +640,8 @@ static void fib_print_adress(universal_address_container_t *entry) void fib_print_routes(void) { mutex_lock(&mtx_access); - printf("%-32s %-6s %-32s %-6s %-16s Interface\n" - , "Destination", "Flags", "Next Hop", "Flags", "Expires"); + printf("%-32s %-12s %-32s %-12s %-12s %-2s\n" + , "Destination", "Flags", "Next Hop", "Flags", "Expires", "Interface"); timex_t now; vtimer_now(&now); @@ -648,9 +649,14 @@ void fib_print_routes(void) for (size_t i = 0; i < FIB_MAX_FIB_TABLE_ENTRIES; ++i) { if (fib_table[i].lifetime.seconds != 0 || fib_table[i].lifetime.microseconds != 0) { fib_print_adress(fib_table[i].global); - printf(" 0x%04"PRIx32" ", fib_table[i].global_flags); + printf(" 0x%"PRIx32" ", fib_table[i].global_flags); + if(fib_table[i].global_flags & FIB_FLAG_NET_PREFIX) { + printf("N "); + } else { + printf("H "); + } fib_print_adress(fib_table[i].next_hop); - printf(" 0x%04"PRIx32" ", fib_table[i].next_hop_flags); + printf(" 0x%010"PRIx32" ", fib_table[i].next_hop_flags); if ((fib_table[i].lifetime.seconds != FIB_LIFETIME_NO_EXPIRE) || (fib_table[i].lifetime.microseconds != FIB_LIFETIME_NO_EXPIRE)) { @@ -663,14 +669,14 @@ void fib_print_routes(void) printf("%-16s ", "EXPIRED"); } else { - printf("%"PRIu32".%05"PRIu32, tm.seconds, tm.microseconds); + printf("%"PRIu32".%"PRIu32, tm.seconds, tm.microseconds); } } else { printf("%-16s ", "NEVER"); } - printf("%d\n", (int)fib_table[i].iface_id); + printf("\t %d\n", (int)fib_table[i].iface_id); } } diff --git a/tests/unittests/tests-fib/tests-fib.c b/tests/unittests/tests-fib/tests-fib.c index e1f0b0674d74a..9cc9663262a86 100644 --- a/tests/unittests/tests-fib/tests-fib.c +++ b/tests/unittests/tests-fib/tests-fib.c @@ -475,17 +475,20 @@ static void test_fib_14_exact_and_prefix_match(void) snprintf(addr_dst, add_buf_size, "Test addr12"); snprintf(addr_nxt, add_buf_size, "Test address %02d", 12); - fib_add_entry(42, (uint8_t *)addr_dst, add_buf_size - 1, 0x12, + fib_add_entry(42, (uint8_t *)addr_dst, add_buf_size - 1, + (FIB_FLAG_NET_PREFIX | 0x12), (uint8_t *)addr_nxt, add_buf_size - 1, 0x12, 100000); snprintf(addr_dst, add_buf_size, "Test addr123"); snprintf(addr_nxt, add_buf_size, "Test address %02d", 23); - fib_add_entry(42, (uint8_t *)addr_dst, add_buf_size - 1, 0x123, + fib_add_entry(42, (uint8_t *)addr_dst, add_buf_size - 1, + (FIB_FLAG_NET_PREFIX | 0x123), (uint8_t *)addr_nxt, add_buf_size - 1, 0x23, 100000); snprintf(addr_dst, add_buf_size, "Test addr1234"); snprintf(addr_nxt, add_buf_size, "Test address %02d", 34); - fib_add_entry(42, (uint8_t *)addr_dst, add_buf_size - 1, 0x1234, + fib_add_entry(42, (uint8_t *)addr_dst, add_buf_size - 1, + (FIB_FLAG_NET_PREFIX | 0x1234), (uint8_t *)addr_nxt, add_buf_size - 1, 0x34, 100000); memset(addr_lookup, 0, add_buf_size); @@ -584,11 +587,13 @@ static void test_fib_16_prefix_match(void) addr_dst[14] = (char)0x80; /* 1000 0000 */ addr_lookup[14] = (char)0x87; /* 1000 0111 */ - fib_add_entry(42, (uint8_t *)addr_dst, add_buf_size - 1, 0x123, + fib_add_entry(42, (uint8_t *)addr_dst, add_buf_size - 1, + (FIB_FLAG_NET_PREFIX | 0x123), (uint8_t *)addr_nxt, add_buf_size - 1, 0x23, 100000); addr_dst[14] = (char)0x3c; /* 0011 1100 */ - fib_add_entry(42, (uint8_t *)addr_dst, add_buf_size - 1, 0x123, + fib_add_entry(42, (uint8_t *)addr_dst, add_buf_size - 1, + (FIB_FLAG_NET_PREFIX | 0x123), (uint8_t *)addr_nxt, add_buf_size - 1, 0x23, 100000); memset(addr_nxt, 0, add_buf_size); @@ -604,14 +609,15 @@ static void test_fib_16_prefix_match(void) addr_lookup[14] = (char)0x34; /* 0011 0100 */ add_buf_size = 16; - fib_add_entry(42, (uint8_t *)addr_dst, add_buf_size - 1, 0x123, - (uint8_t *)addr_nxt, add_buf_size - 1, 0x23, 100000); + fib_add_entry(42, (uint8_t *)addr_dst, add_buf_size - 1, + (FIB_FLAG_NET_PREFIX | 0x123), + (uint8_t *)addr_nxt, add_buf_size - 1, 0x23, 100000); memset(addr_nxt, 0, add_buf_size); ret = fib_get_next_hop(&iface_id, - (uint8_t *)addr_nxt, &add_buf_size, &next_hop_flags, - (uint8_t *)addr_lookup, add_buf_size - 1, 0x123); + (uint8_t *)addr_nxt, &add_buf_size, &next_hop_flags, + (uint8_t *)addr_lookup, add_buf_size - 1, 0x123); TEST_ASSERT_EQUAL_INT(-EHOSTUNREACH, ret); @@ -659,6 +665,9 @@ static void test_fib_17_get_entry_set(void) fib_destination_set_entry_t arr_dst[arr_size]; char prefix[addr_buf_size]; memset(prefix,0, addr_buf_size); + /* cppcheck: prefix is set to all 0 before adding an address + */ + /* cppcheck-suppress redundantCopy */ snprintf(prefix, addr_buf_size, "Test address 1"); int ret = fib_get_destination_set((uint8_t *)prefix, addr_buf_size-1, &arr_dst[0], &arr_size); @@ -669,6 +678,9 @@ static void test_fib_17_get_entry_set(void) arr_size = 20; memset(prefix,0, addr_buf_size); + /* cppcheck: prefix is set to all 0 before adding an address + */ + /* cppcheck-suppress redundantCopy */ snprintf(prefix, addr_buf_size, "Test address 0"); ret = fib_get_destination_set((uint8_t *)prefix, addr_buf_size-1, &arr_dst[0], &arr_size);