/** * @brief removes the given entry * * @param[in] entry the entry to be removed * * @return 0 on success */ static int fib_remove(fib_entry_t *entry) { if (entry->global != NULL) { universal_address_rem(entry->global); } if (entry->next_hop) { universal_address_rem(entry->next_hop); } entry->global = NULL; entry->global_flags = 0; entry->next_hop = NULL; entry->next_hop_flags = 0; entry->iface_id = KERNEL_PID_UNDEF; entry->lifetime = 0; return 0; }
/** * @brief updates the next hop the lifetime and the interface id for a given entry * * @param[in] entry the entry to be updated * @param[in] next_hop the next hop address to be updated * @param[in] next_hop_size the next hop address size * @param[in] next_hop_flags the next-hop address flags * @param[in] lifetime the lifetime in ms * * @return 0 if the entry has been updated * -ENOMEM if the entry cannot be updated due to insufficient RAM */ static int fib_upd_entry(fib_entry_t *entry, uint8_t *next_hop, size_t next_hop_size, uint32_t next_hop_flags, uint32_t lifetime) { universal_address_container_t *container = universal_address_add(next_hop, next_hop_size); if (container == NULL) { return -ENOMEM; } universal_address_rem(entry->next_hop); entry->next_hop = container; entry->next_hop_flags = next_hop_flags; if (lifetime != (uint32_t)FIB_LIFETIME_NO_EXPIRE) { fib_lifetime_to_absolute(lifetime, &entry->lifetime); } else { entry->lifetime = FIB_LIFETIME_NO_EXPIRE; } return 0; }
/** * @brief returns pointer to the entry for the given destination address * * @param[in] dst the destination address * @param[in] dst_size the destination address size * @param[out] entry_arr the array to scribe the found match * @param[in, out] entry_arr_size the number of entries provided by entry_arr (should be always 1) * this value is overwritten with the actual found number * * @return 0 if we found a next-hop prefix * 1 if we found the exact address next-hop * -EHOSTUNREACH if no fitting next-hop is available */ static int fib_find_entry(uint8_t *dst, size_t dst_size, fib_entry_t **entry_arr, size_t *entry_arr_size) { timex_t now; vtimer_now(&now); size_t count = 0; size_t prefix_size = 0; size_t match_size = dst_size; int ret = -EHOSTUNREACH; for (size_t i = 0; i < FIB_MAX_FIB_TABLE_ENTRIES; ++i) { /* autoinvalidate if the entry lifetime is not set to not expire */ if ((fib_table[i].lifetime.seconds != FIB_LIFETIME_NO_EXPIRE) || (fib_table[i].lifetime.microseconds != FIB_LIFETIME_NO_EXPIRE)) { /* check if the lifetime expired */ if (timex_cmp(now, fib_table[i].lifetime) > -1) { /* remove this entry if its lifetime expired */ fib_table[i].lifetime.seconds = 0; fib_table[i].lifetime.microseconds = 0; if (fib_table[i].global != NULL) { universal_address_rem(fib_table[i].global); fib_table[i].global = NULL; } if (fib_table[i].next_hop != NULL) { universal_address_rem(fib_table[i].next_hop); fib_table[i].next_hop = NULL; } } } if ((prefix_size < dst_size) && (fib_table[i].global != NULL) && (universal_address_compare(fib_table[i].global, dst, &match_size) == 0)) { /* If we found an exact match */ if (match_size == dst_size) { entry_arr[0] = &(fib_table[i]); *entry_arr_size = 1; /* we will not find a better one so we return */ return 1; } else { /* we try to find the most fitting prefix */ if (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; count = 1; } } *entry_arr_size = count; return ret; }
/** * @brief returns pointer to the entry for the given destination address * * @param[in] table the FIB table to search in * @param[in] dst the destination address * @param[in] dst_size the destination address size * @param[out] entry_arr the array to scribe the found match * @param[in, out] entry_arr_size the number of entries provided by entry_arr (should be always 1) * this value is overwritten with the actual found number * * @return 0 if we found a next-hop prefix * 1 if we found the exact address next-hop * -EHOSTUNREACH if no fitting next-hop is available */ static int fib_find_entry(fib_table_t *table, uint8_t *dst, size_t dst_size, fib_entry_t **entry_arr, size_t *entry_arr_size) { uint64_t now = xtimer_now64(); size_t count = 0; size_t prefix_size = 0; size_t match_size = dst_size<<3; int ret = -EHOSTUNREACH; bool is_all_zeros_addr = true; for(size_t i = 0; i < dst_size; ++i) { if (dst[i] != 0) { is_all_zeros_addr = false; break; } } for (size_t i = 0; i < table->size; ++i) { /* autoinvalidate if the entry lifetime is not set to not expire */ if (table->entries[i].lifetime != FIB_LIFETIME_NO_EXPIRE) { /* check if the lifetime expired */ if (table->entries[i].lifetime < now) { /* remove this entry if its lifetime expired */ table->entries[i].lifetime = 0; table->entries[i].global_flags = 0; table->entries[i].next_hop_flags = 0; table->entries[i].iface_id = KERNEL_PID_UNDEF; if (table->entries[i].global != NULL) { universal_address_rem(table->entries[i].global); table->entries[i].global = NULL; } if (table->entries[i].next_hop != NULL) { universal_address_rem(table->entries[i].next_hop); table->entries[i].next_hop = NULL; } } } if ((prefix_size < (dst_size<<3)) && (table->entries[i].global != NULL)) { int ret_comp = universal_address_compare(table->entries[i].global, dst, &match_size); /* If we found an exact match */ if (ret_comp == 0 || (is_all_zeros_addr && match_size == 0)) { entry_arr[0] = &(table->entries[i]); *entry_arr_size = 1; /* we will not find a better one so we return */ return 1; } else { /* we try to find the most fitting prefix */ if (ret_comp == 1) { entry_arr[0] = &(table->entries[i]); /* we could find a better one so we move on */ ret = 0; prefix_size = match_size; match_size = dst_size<<3; count = 1; } } } } *entry_arr_size = count; return ret; }