2 * OLSR ad-hoc routing table management protocol GUI front-end
3 * Copyright (C) 2003 Andreas Tønnesen (andreto@ifi.uio.no)
5 * This file is part of olsrd-unik.
7 * uolsrGUI is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * uolsrGUI is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with olsrd-unik; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
37 *Insert a new node in the list
38 *NB! The list is NOT checked for duplicates!!
41 insert_node(struct node *n, olsr_u8_t vtime)
43 struct node *new_node;
45 printf("Inserting node %s\n", ip_to_string((union olsr_ip_addr *)&n->addr));
47 if((new_node = malloc(sizeof(struct node))) == 0)
49 fprintf(stderr, "OUT OF MEMORY!\n");
53 memcpy(new_node, n, sizeof(struct node));
56 nodes.next->prev = new_node;
57 new_node->next = nodes.next;
58 nodes.next = new_node;
59 new_node->prev = &nodes;
61 new_node->hna.next = &new_node->hna;
62 new_node->hna.prev = &new_node->hna;
63 new_node->mid.next = &new_node->mid;
64 new_node->mid.prev = &new_node->mid;
65 new_node->mpr.next = &new_node->mpr;
66 new_node->mpr.prev = &new_node->mpr;
69 update_timer_node(&n->addr, vtime);
76 *Add a new node to the set
79 add_node(union olsr_ip_addr *node, olsr_u8_t vtime)
82 struct node *tmp_nodes;
83 struct timeval tmp_timer;
85 olsr_u32_t time_value;
88 dbl_time = me_to_double(vtime);
89 time_value = (olsr_u32_t) dbl_time*1000;
91 tmp_timer.tv_sec = time_value/1000;
92 tmp_timer.tv_usec = (time_value-(tmp_timer.tv_sec*1000)) * 1000;
94 /* Check if node exists */
95 for(tmp_nodes = nodes.next;
97 tmp_nodes = tmp_nodes->next)
99 if(memcmp(&tmp_nodes->addr, node, ipsize) == 0)
101 //printf("updating timer for node %s\n", ip_to_string(node));
102 //printf("Updatimng timer for: %s\n", ip_to_string(node));
103 //printf("Secs: %d, usecs: %d\n", (int)tmp_timer.tv_sec, (int)tmp_timer.tv_usec);
104 gettimeofday(&now, (struct timezone *)NULL);
105 timeradd(&now, &tmp_timer, &tmp_nodes->timer);
109 for(tmp_mid = tmp_nodes->mid.next;
110 tmp_mid != &tmp_nodes->mid;
111 tmp_mid = tmp_mid->next)
113 if(memcmp(&tmp_mid->alias, node, ipsize) == 0)
115 //printf("updating timer for node %s\n", ip_to_string(node));
116 //printf("Updatimng timer for (MID): %s\n", ip_to_string(&tmp_nodes->addr));
117 //printf("Secs: %d, usecs: %d\n", (int)tmp_timer.tv_sec, (int)tmp_timer.tv_usec);
118 gettimeofday(&now, (struct timezone *)NULL);
119 timeradd(&now, &tmp_timer, &tmp_nodes->timer);
126 memset(&new, 0, sizeof(struct node));
127 memcpy(&new.addr, node, ipsize);
130 insert_node(&new, vtime);
131 update_nodes_list(&new);
138 update_timer_node(union olsr_ip_addr *node, olsr_u8_t vtime)
140 struct node *tmp_nodes;
141 struct timeval tmp_timer;
143 olsr_u32_t time_value;
145 dbl_time = me_to_double(vtime);
146 time_value = (olsr_u32_t) dbl_time*1000;
148 tmp_timer.tv_sec = time_value/1000;
149 tmp_timer.tv_usec = (time_value-(tmp_timer.tv_sec*1000)) * 1000;
151 //printf("Updatimng timer for: %s\n", ip_to_string(node));
152 //printf("Secs: %d, usecs: %d\n", (int)tmp_timer.tv_sec, (int)tmp_timer.tv_usec);
154 for(tmp_nodes = nodes.next;
156 tmp_nodes = tmp_nodes->next)
158 if(memcmp(&tmp_nodes->addr, node, ipsize) == 0)
160 //printf("updating timer for node %s\n", ip_to_string(node));
161 gettimeofday(&now, (struct timezone *)NULL);
162 timeradd(&now, &tmp_timer, &tmp_nodes->timer);
163 if(tmp_nodes->display)
164 update_nodes_list(tmp_nodes);
174 *Updates the hold time for the mpr 'mpr' registered on
175 *the node 'node'. Adds the mpr to the node if not already
177 *@param node the node that has chosen the MPR
178 *@param mpr the MPR chosen by the node
179 *@return 0 if node was added, 1 if not
182 update_timer_mpr(union olsr_ip_addr *node, union olsr_ip_addr *mpr, olsr_u8_t vtime)
184 struct node *tmp_nodes;
186 struct timeval tmp_timer;
188 olsr_u32_t time_value;
190 dbl_time = me_to_double(vtime);
191 time_value = (olsr_u32_t) dbl_time*1000;
193 tmp_timer.tv_sec = time_value/1000;
194 tmp_timer.tv_usec = (time_value-(tmp_timer.tv_sec*1000)) * 1000;
196 //printf("Updatimng MPR timer for: %s\n", ip_to_string(node));
197 //printf("Secs: %d, usecs: %d\n", (int)tmp_timer.tv_sec, (int)tmp_timer.tv_usec);
199 //printf("Updatimng timer for: %s\n", ip_to_string(node));
200 for(tmp_nodes = nodes.next;
202 tmp_nodes = tmp_nodes->next)
204 if(memcmp(&tmp_nodes->addr, node, ipsize) == 0)
206 for(tmp_mpr = tmp_nodes->mpr.next;
207 tmp_mpr != &tmp_nodes->mpr;
208 tmp_mpr = tmp_mpr->next)
210 if(memcmp(&tmp_mpr->addr, mpr, ipsize) == 0)
212 //printf("updating timer for MPR %s ", ip_to_string(mpr));
213 //printf("node %s\n", ip_to_string(node));
214 gettimeofday(&now, (struct timezone *)NULL);
215 timeradd(&now, &tmp_timer, &tmp_mpr->timer);
219 /* Only add if parent is added */
220 add_mpr(node, mpr, &tmp_timer);
232 add_mid_node(union olsr_ip_addr *node, union olsr_ip_addr *alias, olsr_u8_t vtime)
235 struct node *tmp_nodes;
237 struct node new, *inserted;
239 //printf("MID_add: %s\n", ip_to_string(alias));
241 //update_timer_node(node, vtime);
243 for(tmp_nodes = nodes.next;
245 tmp_nodes = tmp_nodes->next)
247 if(memcmp(&tmp_nodes->addr, node, ipsize) == 0)
249 for(tmp_mid = tmp_nodes->mid.next;
250 tmp_mid != &tmp_nodes->mid;
251 tmp_mid = tmp_mid->next)
253 if(memcmp(&tmp_mid->alias, alias, ipsize) == 0)
257 /* we didn't find the address */
258 printf("(1)NEW MID %s ", ip_to_string(alias));
259 printf("ADDED FOR %s\n", ip_to_string(node));
260 if((tmp_mid = malloc(sizeof(struct mid))) == 0)
262 fprintf(stderr, "OUT OF MEMORY\n");
266 memcpy(&tmp_mid->alias, alias, ipsize);
268 tmp_nodes->mid.next->prev = tmp_mid;
269 tmp_mid->next = tmp_nodes->mid.next;
270 tmp_nodes->mid.next = tmp_mid;
271 tmp_mid->prev = &tmp_nodes->mid;
273 remove_node_addr(alias); // Remove if already registered as a node
275 update_nodes_list(tmp_nodes);
285 printf("ADDING NEW NODE %s FROM MID...\n", ip_to_string(node));
286 /* We don't know wery much... */
287 memset(&new, 0, sizeof(struct node));
288 memcpy(&new.addr, node, ipsize);
289 inserted = insert_node(&new, vtime);
291 if((tmp_mid = malloc(sizeof(struct mid))) == 0)
293 fprintf(stderr, "OUT OF MEMORY!\n");
297 memcpy(&tmp_mid->alias, alias, ipsize);
299 tmp_mid->next = &inserted->mid;
300 tmp_mid->prev = &inserted->mid;
301 inserted->mid.next = tmp_mid;
302 inserted->mid.prev = tmp_mid;
304 update_nodes_list(inserted);
311 add_hna_node(union olsr_ip_addr *node, union olsr_ip_addr *net, union olsr_ip_addr *mask, olsr_u8_t vtime)
314 struct node *tmp_nodes;
316 struct node new, *inserted;
318 //printf("HNA: %s\n", ip_to_string(&net));
320 update_timer_node(node, vtime);
322 for(tmp_nodes = nodes.next;
324 tmp_nodes = tmp_nodes->next)
326 if(memcmp(&tmp_nodes->addr, node, ipsize) == 0)
328 for(tmp_hna = tmp_nodes->hna.next;
329 tmp_hna != &tmp_nodes->hna;
330 tmp_hna = tmp_hna->next)
332 if((memcmp(&tmp_hna->net, net, ipsize) == 0) && (memcmp(&tmp_hna->mask, mask, ipsize) == 0))
336 //printf("NEW HNA ADDED FOR %s ", ip_to_string(node));
337 //printf("net: %s \n", ip_to_string(&net));
338 /* we didn't find the address */
339 if((tmp_hna = malloc(sizeof(struct hna))) == 0)
341 fprintf(stderr, "OUT OF MEMORY\n");
345 memcpy(&tmp_hna->net, net, ipsize);
346 memcpy(&tmp_hna->mask, mask, ipsize);
349 tmp_nodes->hna.next->prev = tmp_hna;
350 tmp_hna->next = tmp_nodes->hna.next;
351 tmp_nodes->hna.next = tmp_hna;
352 tmp_hna->prev = &tmp_nodes->hna;
354 update_nodes_list(tmp_nodes);
361 printf("ADDING NEW NODE %s FROM HNA...\n", ip_to_string(node));
362 /* We don't know wery much... */
363 memset(&new, 0, sizeof(struct node));
364 memcpy(&new.addr, node, ipsize);
365 inserted = insert_node(&new, vtime);
367 if((tmp_hna = malloc(sizeof(struct hna))) == 0)
369 fprintf(stderr, "OUT OF MEMORY!\n");
373 memcpy(&tmp_hna->net, net, ipsize);
374 memcpy(&tmp_hna->mask, mask, ipsize);
376 tmp_hna->next = &inserted->hna;
377 tmp_hna->prev = &inserted->hna;
378 inserted->hna.next = tmp_hna;
379 inserted->hna.prev = tmp_hna;
381 update_nodes_list(inserted);
388 *Add the MPR mpr to the node nodes selected MPRs.
389 *Nodes are NOT added if they are not yet registered!
391 *@param node the node that has chosen an MPR
392 *@param mpr the MPR choosen by node
393 *@return negative if node already registered or node not found
396 add_mpr(union olsr_ip_addr *node, union olsr_ip_addr *mpr, struct timeval *tmp_timer)
399 struct node *tmp_nodes;
403 for(tmp_nodes = nodes.next;
405 tmp_nodes = tmp_nodes->next)
407 if(memcmp(&tmp_nodes->addr, node, ipsize) == 0)
409 for(mprs = tmp_nodes->mpr.next;
410 mprs != &tmp_nodes->mpr;
413 if(memcmp(&mprs->addr, mpr, ipsize) == 0)
417 //printf("Adding MPR %s to ", ip_to_string(mpr));
418 //printf("%s\n", ip_to_string(node));
421 if((tmp_mpr = malloc(sizeof(struct mpr))) == 0)
423 fprintf(stderr, "OUT OF MEMORY\n");
427 memcpy(&tmp_mpr->addr, mpr, ipsize);
429 gettimeofday(&now, (struct timezone *)NULL);
430 timeradd(&now, tmp_timer, &tmp_mpr->timer);
433 tmp_nodes->mpr.next->prev = tmp_mpr;
434 tmp_mpr->next = tmp_nodes->mpr.next;
435 tmp_nodes->mpr.next = tmp_mpr;
436 tmp_mpr->prev = &tmp_nodes->mpr;
438 update_nodes_list(tmp_nodes);
451 remove_node(struct node *node)
453 struct hna *tmp_hna, *tmp_hna2;
454 struct mid *tmp_mid, *tmp_mid2;
455 struct mpr *tmp_mpr, *tmp_mpr2;
457 printf("Remove node %s\n", ip_to_string(&node->addr));
460 tmp_hna = node->hna.next;
461 while(tmp_hna != &node->hna)
464 tmp_hna = tmp_hna->next;
467 tmp_mpr = node->mpr.next;
468 while(tmp_mpr != &node->mpr)
471 tmp_mpr = tmp_mpr->next;
474 tmp_mid = node->mid.next;
475 while(tmp_mid != &node->mid)
478 tmp_mid = tmp_mid->next;
482 /* Gemove form GUI */
483 remove_nodes_list(&node->addr);
486 node->prev->next = node->next;
487 node->next->prev = node->prev;
500 * Remove based on address
504 remove_node_addr(union olsr_ip_addr *node)
506 struct node *tmp_nodes;
507 struct hna *tmp_hna, *tmp_hna2;
508 struct mid *tmp_mid, *tmp_mid2;
509 struct mpr *tmp_mpr, *tmp_mpr2;
511 printf("Remove node %s\n", ip_to_string(node));
514 tmp_nodes = nodes.next;
516 while(tmp_nodes != &nodes)
518 if(memcmp(&tmp_nodes->addr, node, ipsize) == 0)
520 printf("(2)Deleting node %s\n", ip_to_string((union olsr_ip_addr *)&tmp_nodes->addr));
522 tmp_hna = tmp_nodes->hna.next;
523 while(tmp_hna != &tmp_nodes->hna)
526 tmp_hna = tmp_hna->next;
529 tmp_mpr = tmp_nodes->mpr.next;
530 while(tmp_mpr != &tmp_nodes->mpr)
533 tmp_mpr = tmp_mpr->next;
536 tmp_mid = tmp_nodes->mid.next;
537 while(tmp_mid != &tmp_nodes->mid)
540 tmp_mid = tmp_mid->next;
544 /* Gemove form GUI */
545 remove_nodes_list(&tmp_nodes->addr);
548 tmp_nodes->prev->next = tmp_nodes->next;
549 tmp_nodes->next->prev = tmp_nodes->prev;
556 tmp_nodes = tmp_nodes->next;
568 struct node *tmp_nodes;
570 for(tmp_nodes = nodes.next;
572 tmp_nodes = tmp_nodes->next)
574 if(strcmp(ip_to_string((union olsr_ip_addr *)&tmp_nodes->addr), ip) == 0)
583 find_node_t(union olsr_ip_addr *ip)
585 struct node *tmp_nodes;
587 for(tmp_nodes = nodes.next;
589 tmp_nodes = tmp_nodes->next)
591 if(memcmp(&tmp_nodes->addr, ip, ipsize) == 0)
601 *Remove timed out nodes
604 time_out_nodes(gpointer data)
606 struct node *tmp_nodes;
607 struct node *node_to_delete;
609 /* Wait before starting timing out */
613 //printf("Waiting...\n");
617 //printf("Timing out nodes...\n");
618 gettimeofday(&now, (struct timezone *)NULL);
620 tmp_nodes = nodes.next;
622 while(tmp_nodes != &nodes)
624 //printf("%s: %6d < %6d\n", ip_to_string(&tmp_nodes->addr), tmp_nodes->timer.tv_sec, now.tv_sec);
625 if(timercmp(&tmp_nodes->timer,&now,<))
627 printf("Node %s timed out...\n", ip_to_string((union olsr_ip_addr *)&tmp_nodes->addr));
628 node_to_delete = tmp_nodes;
630 tmp_nodes = tmp_nodes->next;
632 remove_nodes_list(&node_to_delete->addr);
633 remove_node(node_to_delete);
636 tmp_nodes = tmp_nodes->next;
645 *Timeout MPRs for a given node. Only called when user
646 *is to see the registered MPRs of the node.
647 *@param node the node whom MPRs should be timed out
648 *@return negative if node not found
651 time_out_mprs(union olsr_ip_addr *node)
654 struct node *tmp_nodes;
655 struct mpr *mpr_to_delete;
658 gettimeofday(&now, (struct timezone *)NULL);
663 * THIS ALGORITHM HAS NOT BEEN TESTED PROPERLY!!!!!!
667 for(tmp_nodes = nodes.next;
669 tmp_nodes = tmp_nodes->next)
671 if(memcmp(&tmp_nodes->addr, node, ipsize) == 0)
673 tmp_mpr = tmp_nodes->mpr.next;
675 while(tmp_mpr != &tmp_nodes->mpr)
677 if(timercmp(&tmp_mpr->timer,&now,<))
679 printf("MPR %s OF NODE ", ip_to_string((union olsr_ip_addr *)&tmp_mpr->addr));
680 printf("%s TIMIED OUT ", ip_to_string((union olsr_ip_addr *)&tmp_nodes->addr));fflush(stdout);
682 mpr_to_delete = tmp_mpr;
683 tmp_mpr = tmp_mpr->next;
686 mpr_to_delete->next->prev = mpr_to_delete->prev;
687 mpr_to_delete->prev->next = mpr_to_delete->next;
692 tmp_mpr = tmp_mpr->next;
705 init_timer(olsr_u32_t time_value, struct timeval *hold_timer)
707 olsr_u16_t time_value_sec=0;
708 olsr_u16_t time_value_msec=0;
710 time_value_sec=time_value/1000;
711 time_value_msec=time_value-(time_value_sec*1000);
713 hold_timer->tv_sec=time_value_sec;
714 hold_timer->tv_usec=time_value_msec*1000;
720 *Function that converts a mantissa/exponent 8bit value back
721 *to double as described in RFC3626:
723 * value = C*(1+a/16)*2^b [in seconds]
725 * where a is the integer represented by the four highest bits of the
726 * field and b the integer represented by the four lowest bits of the
729 *@param me the 8 bit mantissa/exponen value
731 *@return a double value
734 me_to_double(olsr_u8_t me)
738 return (double)(VTIME_SCALE_FACTOR*(1+(double)a/16)*(double)pow(2,b));