2 * The olsr.org Optimized Link-State Routing daemon(olsrd)
3 * Copyright (c) 2004, Andreas Tønnesen(andreto@olsr.org)
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
16 * * Neither the name of olsr.org, olsrd nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
33 * Visit http://www.olsr.org for more information.
35 * If you find this software useful feel free to make a donation
36 * to the project. For more information see the website or contact
37 * the copyright holders.
39 * $Id: neighbor_table.c,v 1.37 2007/11/29 18:10:14 bernd67 Exp $
44 #include "two_hop_neighbor_table.h"
47 #include "neighbor_table.h"
49 #include "scheduler.h"
51 #include "mpr_selector_set.h"
55 struct neighbor_entry neighbortable[HASHSIZE];
59 olsr_init_neighbor_table(void)
63 olsr_register_timeout_function(&olsr_time_out_neighborhood_tables, OLSR_TRUE);
64 for(i = 0; i < HASHSIZE; i++)
66 neighbortable[i].next = &neighbortable[i];
67 neighbortable[i].prev = &neighbortable[i];
73 *Delete a two hop neighbor from a neighbors two
76 *@param neighbor the neighbor to delete the two hop
78 *@param address the IP address of the two hop neighbor
81 *@return positive if entry deleted
84 olsr_delete_neighbor_2_pointer(struct neighbor_entry *neighbor, union olsr_ip_addr *address)
87 struct neighbor_2_list_entry *entry;
89 entry = neighbor->neighbor_2_list.next;
91 while(entry != &neighbor->neighbor_2_list)
93 if(ipequal(&entry->neighbor_2->neighbor_2_addr, address))
108 *Check if a two hop neighbor is reachable via a given
111 *@param neighbor neighbor-entry to check via
112 *@param neighbor_main_address the addres of the two hop neighbor
115 *@return a pointer to the neighbor_2_list_entry struct
116 *representing the two hop neighbor if found. NULL if not found.
118 struct neighbor_2_list_entry *
119 olsr_lookup_my_neighbors(const struct neighbor_entry *neighbor, const union olsr_ip_addr *neighbor_main_address)
121 struct neighbor_2_list_entry *entry;
123 for(entry = neighbor->neighbor_2_list.next;
124 entry != &neighbor->neighbor_2_list;
128 if(ipequal(&entry->neighbor_2->neighbor_2_addr, neighbor_main_address))
138 *Delete a neighbr table entry.
140 *Remember: Deleting a neighbor entry results
141 *the deletion of its 2 hop neighbors list!!!
142 *@param neighbor the neighbor entry to delete
148 olsr_delete_neighbor_table(const union olsr_ip_addr *neighbor_addr)
150 struct neighbor_2_list_entry *two_hop_list, *two_hop_to_delete;
152 struct neighbor_entry *entry;
154 //printf("inserting neighbor\n");
156 hash = olsr_hashing(neighbor_addr);
158 entry = neighbortable[hash].next;
161 * Find neighbor entry
163 while(entry != &neighbortable[hash])
165 if(ipequal(&entry->neighbor_main_addr, neighbor_addr))
171 if(entry == &neighbortable[hash])
175 two_hop_list = entry->neighbor_2_list.next;
177 while(two_hop_list != &entry->neighbor_2_list)
179 struct neighbor_2_entry *two_hop_entry = two_hop_list->neighbor_2;
181 two_hop_entry->neighbor_2_pointer--;
183 olsr_delete_neighbor_pointer(two_hop_entry, &entry->neighbor_main_addr);
185 /* Delete entry if it has no more one hop neighbors pointing to it */
186 if(two_hop_entry->neighbor_2_pointer < 1)
188 DEQUEUE_ELEM(two_hop_entry);
194 two_hop_to_delete = two_hop_list;
195 two_hop_list = two_hop_list->next;
197 free(two_hop_to_delete);
207 changes_neighborhood = OLSR_TRUE;
215 *Insert a neighbor entry in the neighbor table
217 *@param main_addr the main address of the new node
219 *@return 0 if neighbor already exists 1 if inserted
221 struct neighbor_entry *
222 olsr_insert_neighbor_table(const union olsr_ip_addr *main_addr)
225 struct neighbor_entry *new_neigh;
227 hash = olsr_hashing(main_addr);
229 /* Check if entry exists */
231 for(new_neigh = neighbortable[hash].next;
232 new_neigh != &neighbortable[hash];
233 new_neigh = new_neigh->next)
235 if(ipequal(&new_neigh->neighbor_main_addr, main_addr))
239 //printf("inserting neighbor\n");
241 new_neigh = olsr_malloc(sizeof(struct neighbor_entry), "New neighbor entry");
243 /* Set address, willingness and status */
244 new_neigh->neighbor_main_addr = *main_addr;
245 new_neigh->willingness = WILL_NEVER;
246 new_neigh->status = NOT_SYM;
248 new_neigh->neighbor_2_list.next = &new_neigh->neighbor_2_list;
249 new_neigh->neighbor_2_list.prev = &new_neigh->neighbor_2_list;
251 new_neigh->linkcount = 0;
252 new_neigh->is_mpr = OLSR_FALSE;
253 new_neigh->was_mpr = OLSR_FALSE;
256 QUEUE_ELEM(neighbortable[hash], new_neigh);
264 *Lookup a neighbor entry in the neighbortable based on an address.
266 *@param dst the IP address of the neighbor to look up
268 *@return a pointer to the neighbor struct registered on the given
269 *address. NULL if not found.
271 struct neighbor_entry *
272 olsr_lookup_neighbor_table(const union olsr_ip_addr *dst)
275 *Find main address of node
277 union olsr_ip_addr *tmp_ip = mid_lookup_main_addr(dst);
280 return olsr_lookup_neighbor_table_alias(dst);
285 *Lookup a neighbor entry in the neighbortable based on an address.
287 *@param dst the IP address of the neighbor to look up
289 *@return a pointer to the neighbor struct registered on the given
290 *address. NULL if not found.
292 struct neighbor_entry *
293 olsr_lookup_neighbor_table_alias(const union olsr_ip_addr *dst)
295 struct neighbor_entry *entry;
296 olsr_u32_t hash = olsr_hashing(dst);
298 //printf("\nLookup %s\n", olsr_ip_to_string(&buf, dst));
299 for(entry = neighbortable[hash].next;
300 entry != &neighbortable[hash];
303 //printf("Checking %s\n", olsr_ip_to_string(&buf, &entry->neighbor_main_addr));
304 if(ipequal(&entry->neighbor_main_addr, dst))
308 //printf("NOPE\n\n");
317 update_neighbor_status(struct neighbor_entry *entry, int lnk)
320 * Update neighbor entry
325 /* N_status is set to SYM */
326 if(entry->status == NOT_SYM)
328 struct neighbor_2_entry *two_hop_neighbor;
330 /* Delete posible 2 hop entry on this neighbor */
331 if((two_hop_neighbor = olsr_lookup_two_hop_neighbor_table(&entry->neighbor_main_addr))!=NULL)
333 olsr_delete_two_hop_neighbor_table(two_hop_neighbor);
336 changes_neighborhood = OLSR_TRUE;
337 changes_topology = OLSR_TRUE;
338 if(olsr_cnf->tc_redundancy > 1)
339 signal_link_changes(OLSR_TRUE);
345 if(entry->status == SYM)
347 changes_neighborhood = OLSR_TRUE;
348 changes_topology = OLSR_TRUE;
349 if(olsr_cnf->tc_redundancy > 1)
350 signal_link_changes(OLSR_TRUE);
352 /* else N_status is set to NOT_SYM */
353 entry->status = NOT_SYM;
354 /* remove neighbor from routing list */
357 return entry->status;
363 *Times out the entries in the two hop neighbor table and
364 *deletes those who have exceeded their time to live since
371 olsr_time_out_two_hop_neighbors(struct neighbor_entry *neighbor)
373 struct neighbor_2_list_entry *two_hop_list = neighbor->neighbor_2_list.next;
375 while(two_hop_list != &neighbor->neighbor_2_list)
377 if(TIMED_OUT(two_hop_list->neighbor_2_timer))
379 struct neighbor_2_list_entry *two_hop_to_delete;
380 struct neighbor_2_entry *two_hop_entry = two_hop_list->neighbor_2;
382 two_hop_entry->neighbor_2_pointer--;
383 olsr_delete_neighbor_pointer(two_hop_entry, &neighbor->neighbor_main_addr);
385 if(two_hop_entry->neighbor_2_pointer < 1)
387 DEQUEUE_ELEM(two_hop_entry);
391 two_hop_to_delete = two_hop_list;
392 two_hop_list = two_hop_list->next;
395 DEQUEUE_ELEM(two_hop_to_delete);
397 free(two_hop_to_delete);
399 /* This flag is set to OLSR_TRUE to recalculate the MPR set and the routing table*/
400 changes_neighborhood = OLSR_TRUE;
401 changes_topology = OLSR_TRUE;
405 two_hop_list = two_hop_list->next;
411 olsr_time_out_neighborhood_tables(void)
415 for(idx=0;idx<HASHSIZE;idx++)
417 struct neighbor_entry *entry;
418 for(entry = neighbortable[idx].next; entry != &neighbortable[idx]; entry = entry->next)
420 olsr_time_out_two_hop_neighbors(entry);
426 *Prints the registered neighbors and two hop neighbors
432 olsr_print_neighbor_table(void)
435 /* The whole function doesn't do anything else. */
437 const int iplen = olsr_cnf->ip_version == AF_INET ? 15 : 39;
440 OLSR_PRINTF(1, "\n--- %02d:%02d:%02d.%02d ------------------------------------------------ NEIGHBORS\n\n"
441 "%*s LQ NLQ SYM MPR MPRS will\n",
445 (int)now.tv_usec/10000,
449 for (idx = 0; idx < HASHSIZE; idx++) {
450 struct neighbor_entry *neigh;
451 for(neigh = neighbortable[idx].next; neigh != &neighbortable[idx]; neigh = neigh->next) {
452 struct link_entry *lnk = get_best_link_to_neighbor(&neigh->neighbor_main_addr);
455 struct ipaddr_str buf;
457 OLSR_PRINTF(1, "%-*s %5.3f %5.3f %s %s %s %d\n",
459 olsr_ip_to_string(&buf, &neigh->neighbor_main_addr),
460 lnk->loss_link_quality,
461 lnk->neigh_link_quality,
462 neigh->status == SYM ? "YES " : "NO ",
463 neigh->is_mpr ? "YES " : "NO ",
464 olsr_lookup_mprs_set(&neigh->neighbor_main_addr) == NULL ? "NO " : "YES ",