📄 routing_table.c
字号:
#ifdef _IPV6 memcpy(&rt_entry->next_hop, &next, sizeof(struct in6_addr));#else rt_entry->next_hop = next;#endif /* _IPV6 */ rt_entry->last_hcnt = rt_entry->hcnt; rt_entry->hcnt = hops; if (hops > 1) { rt_entry->last_hello_time.tv_sec = 0; rt_entry->last_hello_time.tv_usec = 0; rt_entry->hello_cnt = 0; } if (newlife != 0) rt_table_update_timeout(rt_entry, newlife); return rt_entry;}inline rt_table_t *NS_CLASS rt_table_update_timeout(rt_table_t * rt_entry, long life){ if (!rt_entry) return NULL; timer_add_msec(&rt_entry->rt_timer, life); return rt_entry;}//PL:#ifdef _IPV6rt_table_t *NS_CLASS rt_table_find_active(struct in6_addr dest_addr)#elsert_table_t *NS_CLASS rt_table_find_active(u_int32_t dest_addr)#endif /* _IPV6 */{ rt_table_t *rt_entry; rt_entry = rt_table_find(dest_addr); if (rt_entry == NULL || rt_entry->hcnt == INFTY) return NULL; return rt_entry;}//PL:#ifdef _IPV6rt_table_t *NS_CLASS rt_table_find(struct in6_addr dest_addr)#elsert_table_t *NS_CLASS rt_table_find(u_int32_t dest_addr)#endif /* _IPV6 */{ rt_table_t *rt_entry; hash_value hash; unsigned int index; /* Calculate index */ index = hashing(&dest_addr, &hash); rt_entry = routing_table[index]; //PL: debug /* printf("rt_table_find: hash = %d, index = %d\n", hash, index); if(rt_entry != NULL) printf("rt_entry != NULL\n"); else printf("rt_entry == NULL\n"); */ /* Handle collisions: */ while (rt_entry != NULL) { //PL:#ifdef _IPV6 if (memcmp(&rt_entry->hash, &hash, sizeof(hash)) != 0) #else if (rt_entry->hash != hash) #endif /* _IPV6 */ { rt_entry = rt_entry->next; continue; } //PL:#ifdef _IPV6 if (memcmp(&dest_addr, &rt_entry->dest_addr, sizeof(struct in6_addr)) == 0)#else if (memcmp(&dest_addr, &rt_entry->dest_addr, sizeof(u_int32_t)) == 0)#endif /* _IPV6 */ return rt_entry; rt_entry = rt_entry->next; } return NULL;}/* Route expiry and Deletion. */int NS_CLASS rt_table_invalidate(rt_table_t * rt_entry){ struct timeval now; gettimeofday(&now, NULL); if (rt_entry == NULL) return -1; if (rt_entry->hello_timer.used) DEBUG(LOG_DEBUG, 0, "route_table_invalidate: last HELLO: %ld", timeval_diff(&now, &rt_entry->last_hello_time) / 1000); timer_remove(&rt_entry->rt_timer); timer_remove(&rt_entry->hello_timer); timer_remove(&rt_entry->ack_timer); /* If the route is already invalidated, do nothing... */ if (rt_entry->hcnt == INFTY) { //PL:#ifdef _IPV6 DEBUG(LOG_DEBUG, 0, "route_table_invalidate: Route %s already invalidated!!!", ip6_to_str(rt_entry->dest_addr));#else DEBUG(LOG_DEBUG, 0, "route_table_invalidate: Route %s already invalidated!!!", ip_to_str(rt_entry->dest_addr));#endif /* _IPV6 */ return -1; } /* Save last hopcount */ rt_entry->last_hcnt = rt_entry->hcnt; /* Set infinity hop count */ rt_entry->hcnt = INFTY; rt_entry->hello_cnt = 0; /* When the lifetime of a route entry expires, increase the sequence number for that entry. (AODV draft v.10, section 6.1.) */ rt_entry->dest_seqno++; rt_entry->flags = 0; rt_entry->last_life = 0; rt_entry->last_hello_time.tv_sec = 0; rt_entry->last_hello_time.tv_usec = 0;#ifndef NS_PORT /* Delete kernel routing table entry. */ //PL: plen is 128 IPv6 and 0 is the netmask for IPv4#ifdef _IPV6 //printf("3. rt_entry->last_hcnt = %d\n", rt_entry->last_hcnt); //PL: should use last hop count as metric + 1? if (k_del_rte(rt_entry->dest_addr, rt_entry->next_hop, 128, rt_entry->ifindex, rt_entry->last_hcnt + 1) < 0)#else if (k_del_rte(rt_entry->dest_addr, 0, 0) < 0)#endif /* _IPV6 */ log(LOG_WARNING, errno, "rt_table_invalidate: Could not delete kernel route!");#endif /* Schedule a deletion timer */ rt_entry->rt_timer.handler = &NS_CLASS route_delete_timeout; timer_add_msec(&rt_entry->rt_timer, DELETE_PERIOD); //PL:#ifdef _IPV6 DEBUG(LOG_DEBUG, 0, "rt_table_invalidate: %s removed in %u msecs", ip6_to_str(rt_entry->dest_addr), DELETE_PERIOD);#else DEBUG(LOG_DEBUG, 0, "rt_table_invalidate: %s removed in %u msecs", ip_to_str(rt_entry->dest_addr), DELETE_PERIOD);#endif /* _IPV6 */ return 0;}//PL:#ifdef _IPV6void NS_CLASS rt_table_delete(struct in6_addr dest_addr)#elsevoid NS_CLASS rt_table_delete(u_int32_t dest_addr)#endif /* _IPV6 */{ rt_table_t *rt_entry, *prev; hash_value hash; unsigned int index; /* Calculate index */ index = hashing(&dest_addr, &hash); for (prev = NULL, rt_entry = routing_table[index]; rt_entry != NULL; prev = rt_entry, rt_entry = rt_entry->next) { //PL:#ifdef _IPV6 if (memcmp(&rt_entry->hash, &hash, sizeof(hash)) != 0) continue; if (memcmp(&dest_addr, &rt_entry->dest_addr, sizeof(struct in6_addr)) == 0)#else if (rt_entry->hash != hash) continue; if (memcmp(&dest_addr, &rt_entry->dest_addr, sizeof(u_int32_t)) == 0)#endif /* _IPV6 */ { if (prev == NULL) routing_table[index] = rt_entry->next; else prev->next = rt_entry->next; precursor_list_destroy(rt_entry);#ifndef NS_PORT //PL: plen is 128 IPv6 and 0 is the netmask for IPv4#ifdef _IPV6 //PL: need to varify the gw is correct!! //printf("4. rt_entry->last_hcnt = %d\n", rt_entry->last_hcnt); if (rt_entry->hcnt != INFTY && k_del_rte(dest_addr, rt_entry->next_hop, 128, rt_entry->ifindex, rt_entry->hcnt + 1) < 0) log(LOG_WARNING, errno, "rt_table_delete: Could not delete kernel route!");#else if (rt_entry->hcnt != INFTY && k_del_rte(dest_addr, 0, 0) < 0) log(LOG_WARNING, errno, "rt_table_delete: Could not delete kernel route!");#endif /* _IPV6 */#endif /* NS_PORT */ /* Make sure any timers are removed... */ timer_remove(&rt_entry->rt_timer); timer_remove(&rt_entry->hello_timer); timer_remove(&rt_entry->ack_timer); free(rt_entry); return; } }}//PL:#ifdef _IPV6NS_STATIC void NS_CLASS rt_table_remove_precursor(struct in6_addr dest_addr)#elseNS_STATIC void NS_CLASS rt_table_remove_precursor(u_int32_t dest_addr)#endif /* _IPV6 */{ rt_table_t *rt_entry; int i; /* Loop through the whole table and remove destination from any precursor lists */ for (i = 0; i < RT_TABLESIZE; i++) { for (rt_entry = routing_table[i]; rt_entry != NULL; rt_entry = rt_entry->next) precursor_remove(rt_entry, dest_addr); }}/****************************************************************//* Add an neighbor to the active neighbor list. *///PL:#ifdef _IPV6void NS_CLASS precursor_add(rt_table_t * rt_entry, struct in6_addr addr)#elsevoid NS_CLASS precursor_add(rt_table_t * rt_entry, u_int32_t addr)#endif /* _IPV6 */{ precursor_t *pr_entry; /* Sanity check */ if (rt_entry == NULL) return; /* Check if the node is already in the precursors list. */ for (pr_entry = rt_entry->precursors; pr_entry != NULL; pr_entry = pr_entry->next) { //PL:#ifdef _IPV6 if (memcmp(&pr_entry->neighbor, &addr, sizeof(struct in6_addr)) == 0) return;#else if (pr_entry->neighbor == addr) return;#endif /* _IPV6 */ } if ((pr_entry = (precursor_t *) malloc(sizeof(precursor_t))) == NULL) { perror("Could not allocate memory for precursor node!!\n"); exit(-1); } //PL:#ifdef _IPV6 DEBUG(LOG_INFO, 0, "precursor_add: Adding precursor %s to rte %s", ip6_to_str(addr), ip6_to_str(rt_entry->dest_addr));#else DEBUG(LOG_INFO, 0, "precursor_add: Adding precursor %s to rte %s", ip_to_str(addr), ip_to_str(rt_entry->dest_addr));#endif /* _IPV6 */ /* Insert first in precursors list */ //PL:#ifdef _IPV6 memcpy(&pr_entry->neighbor, &addr, sizeof(struct in6_addr));#else pr_entry->neighbor = addr;#endif /* _IPV6 */ pr_entry->next = rt_entry->precursors; rt_entry->precursors = pr_entry;}/****************************************************************//* Remove a neighbor from the active neighbor list. *///PL:#ifdef _IPV6void NS_CLASS precursor_remove(rt_table_t * rt_entry, struct in6_addr addr)#elsevoid NS_CLASS precursor_remove(rt_table_t * rt_entry, u_int32_t addr)#endif /* _IPV6 */{ precursor_t *curr, *prev; /* Sanity check */ if (rt_entry == NULL) return; for (prev = NULL, curr = rt_entry->precursors; curr != NULL; prev = curr, curr = curr->next) { //PL:#ifdef _IPV6 if (memcmp(&curr->neighbor, &addr, sizeof(struct in6_addr)) == 0); { DEBUG(LOG_INFO, 0, "precursor_remove: Removing precursor %s from rte %s", ip6_to_str(addr), ip6_to_str(rt_entry->dest_addr));#else if (curr->neighbor == addr) { DEBUG(LOG_INFO, 0, "precursor_remove: Removing precursor %s from rte %s", ip_to_str(addr), ip_to_str(rt_entry->dest_addr));#endif /* _IPV6 */ if (prev == NULL) /* We are about to remove the first entry.. */ rt_entry->precursors = curr->next; else prev->next = curr->next; free(curr); return; } }}/****************************************************************//* Delete all entries from the active neighbor list. */void precursor_list_destroy(rt_table_t * rt_entry){ precursor_t *tmp; /* Sanity check */ if (rt_entry == NULL) return; while (rt_entry->precursors != NULL) { tmp = rt_entry->precursors; rt_entry->precursors = rt_entry->precursors->next; free(tmp); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -