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: olsr.c,v 1.44 2005/05/26 09:55:11 kattemat Exp $
43 * All these functions are global
49 #include "two_hop_neighbor_table.h"
51 #include "duplicate_set.h"
52 #include "mpr_selector_set.h"
57 #include "scheduler.h"
60 #include "neighbor_table.h"
62 #include "lq_packet.h"
69 * Process changes functions
74 int (*function)(int, int, int);
78 static struct pcf *pcf_list;
80 static olsr_u16_t message_seqno;
83 *Initialize the message sequence number as a random value
88 message_seqno = random() & 0xFFFF;
92 * Get and increment the message sequence number
99 return message_seqno++;
104 register_pcf(int (*f)(int, int, int))
108 OLSR_PRINTF(1, "Registering pcf function\n")
110 new_pcf = olsr_malloc(sizeof(struct pcf), "New PCF");
112 new_pcf->function = f;
113 new_pcf->next = pcf_list;
120 *Process changes in neighborhood or/and topology.
121 *Re-calculates the neighborhooh/topology if there
122 *are any updates - then calls the right functions to
123 *update the routing table.
127 olsr_process_changes()
130 struct pcf *tmp_pc_list;
133 if(changes_neighborhood)
134 OLSR_PRINTF(3, "CHANGES IN NEIGHBORHOOD\n")
136 OLSR_PRINTF(3, "CHANGES IN TOPOLOGY\n")
138 OLSR_PRINTF(3, "CHANGES IN HNA\n")
141 if(!changes_neighborhood &&
146 if (olsr_cnf->debug_level > 0 && olsr_cnf->clear_screen && isatty(1))
149 printf("%s", OLSRD_VERSION_DATE);
152 if (changes_neighborhood)
154 /* Calculate new mprs, HNA and routing table */
155 if (olsr_cnf->lq_level < 1)
157 olsr_calculate_mpr();
162 olsr_calculate_lq_mpr();
165 if (olsr_cnf->lq_level < 2)
167 olsr_calculate_routing_table();
168 olsr_calculate_hna_routes();
173 olsr_calculate_lq_routing_table();
177 else if (changes_topology)
179 /* calculate the routing table and HNA */
181 if (olsr_cnf->lq_level < 2)
183 olsr_calculate_routing_table();
184 olsr_calculate_hna_routes();
189 olsr_calculate_lq_routing_table();
193 else if (changes_hna)
195 /* update HNA routes */
197 if (olsr_cnf->lq_level < 2)
199 olsr_calculate_hna_routes();
204 olsr_calculate_lq_routing_table();
208 if (olsr_cnf->debug_level > 0)
210 if (olsr_cnf->debug_level > 2)
212 olsr_print_mid_set();
214 if (olsr_cnf->debug_level > 3)
216 olsr_print_duplicate_table();
217 olsr_print_hna_set();
221 olsr_print_link_set();
222 olsr_print_neighbor_table();
223 olsr_print_tc_table();
226 for(tmp_pc_list = pcf_list;
228 tmp_pc_list = tmp_pc_list->next)
230 tmp_pc_list->function(changes_neighborhood,
235 changes_neighborhood = OLSR_FALSE;
236 changes_topology = OLSR_FALSE;
237 changes_hna = OLSR_FALSE;
248 *Initialize all the tables used(neighbor,
249 *topology, MID, HNA, MPR, dup).
250 *Also initalizes other variables
256 changes_topology = OLSR_FALSE;
257 changes_neighborhood = OLSR_FALSE;
258 changes_hna = OLSR_FALSE;
260 /* Initialize link set */
261 olsr_init_link_set();
263 /* Initialize duplicate table */
264 olsr_init_duplicate_table();
266 /* Initialize neighbor table */
267 olsr_init_neighbor_table();
269 /* Initialize routing table */
270 olsr_init_routing_table();
272 /* Initialize two hop table */
273 olsr_init_two_hop_table();
275 /* Initialize old route table */
276 olsr_init_old_table();
278 /* Initialize topology */
281 /* Initialize mpr selector table */
282 olsr_init_mprs_set();
284 /* Initialize MID set */
287 /* Initialize HNA set */
298 *Check if a message is to be forwarded and forward
301 *@param m the OLSR message recieved
302 *@param originator the originator of this message
303 *@param seqno the seqno of the message
305 *@returns positive if forwarded
308 olsr_forward_message(union olsr_message *m,
309 union olsr_ip_addr *originator,
311 struct interface *in_if,
312 union olsr_ip_addr *from_addr)
314 union olsr_ip_addr *src;
315 struct neighbor_entry *neighbor;
317 struct interface *ifn;
320 if(!olsr_check_dup_table_fwd(originator, seqno, &in_if->ip_addr))
323 OLSR_PRINTF(3, "Message already forwarded!\n")
328 /* Lookup sender address */
329 if(!(src = mid_lookup_main_addr(from_addr)))
333 if(NULL == (neighbor=olsr_lookup_neighbor_table(src)))
336 if(neighbor->status != SYM)
339 /* Update duplicate table interface */
340 olsr_update_dup_entry(originator, seqno, &in_if->ip_addr);
344 if(olsr_lookup_mprs_set(src) == NULL)
347 OLSR_PRINTF(5, "Forward - sender %s not MPR selector\n", olsr_ip_to_string(src))
353 /* Treat TTL hopcnt */
354 if(olsr_cnf->ip_version == AF_INET)
369 /* Update dup forwarded */
370 olsr_set_dup_forward(originator, seqno);
372 /* Update packet data */
375 msgsize = ntohs(m->v4.olsr_msgsize);
377 /* looping trough interfaces */
378 for (ifn = ifnet; ifn ; ifn = ifn->int_next)
380 if(net_output_pending(ifn))
383 * Check if message is to big to be piggybacked
385 if(net_outbuffer_push(ifn, (olsr_u8_t *)m, msgsize) != msgsize)
390 set_buffer_timer(ifn);
392 if(net_outbuffer_push(ifn, (olsr_u8_t *)m, msgsize) != msgsize)
394 OLSR_PRINTF(1, "Received message to big to be forwarded in %s(%d bytes)!", ifn->int_name, msgsize)
395 olsr_syslog(OLSR_LOG_ERR, "Received message to big to be forwarded on %s(%d bytes)!", ifn->int_name, msgsize);
403 /* No forwarding pending */
404 set_buffer_timer(ifn);
406 if(net_outbuffer_push(ifn, (olsr_u8_t *)m, msgsize) != msgsize)
408 OLSR_PRINTF(1, "Received message to big to be forwarded in %s(%d bytes)!", ifn->int_name, msgsize)
409 olsr_syslog(OLSR_LOG_ERR, "Received message to big to be forwarded on %s(%d bytes)!", ifn->int_name, msgsize);
420 set_buffer_timer(struct interface *ifn)
425 jitter = (float) random()/RAND_MAX;
426 jitter *= max_jitter;
428 fwdtimer[ifn->if_nr] = GET_TIMESTAMP(jitter*1000);
435 olsr_init_willingness()
437 if(olsr_cnf->willingness_auto)
438 olsr_register_scheduler_event(&olsr_update_willingness,
439 NULL, will_int, will_int, NULL);
443 olsr_update_willingness(void *foo)
447 tmp_will = olsr_cnf->willingness;
449 /* Re-calculate willingness */
450 olsr_cnf->willingness = olsr_calculate_willingness();
452 if(tmp_will != olsr_cnf->willingness)
454 OLSR_PRINTF(1, "Local willingness updated: old %d new %d\n", tmp_will, olsr_cnf->willingness)
460 *Calculate this nodes willingness to act as a MPR
461 *based on either a fixed value or the power status
462 *of the node using APM
464 *@return a 8bit value from 0-7 representing the willingness
468 olsr_calculate_willingness()
470 struct olsr_apm_info ainfo;
472 /* If fixed willingness */
473 if(!olsr_cnf->willingness_auto)
474 return olsr_cnf->willingness;
476 if(apm_read(&ainfo) < 1)
479 apm_printinfo(&ainfo);
482 if(ainfo.ac_line_status == OLSR_AC_POWERED)
485 /* If battery powered
487 * juice > 78% will: 3
488 * 78% > juice > 26% will: 2
489 * 26% > juice will: 1
491 return (ainfo.battery_percentage / 26);
495 olsr_msgtype_to_string(olsr_u8_t msgtype)
497 static char type[20];
509 case(LQ_HELLO_MESSAGE):
517 snprintf(type, 20, "UNKNOWN(%d)", msgtype);
523 *Termination function to be called whenever a error occures
524 *that requires the daemon to terminate
526 *@param msg the message to write to the syslog and possibly stdout
530 olsr_exit(const char *msg, int val)
532 OLSR_PRINTF(1, "OLSR EXIT: %s\n", msg)
533 olsr_syslog(OLSR_LOG_ERR, "olsrd exit: %s\n", msg);
542 *Wrapper for malloc(3) that does error-checking
544 *@param size the number of bytes to allocalte
545 *@param caller a string identifying the caller for
546 *use in error messaging
548 *@return a void pointer to the memory allocated
551 olsr_malloc(size_t size, const char *id)
555 if((ptr = malloc(size)) == 0)
557 OLSR_PRINTF(1, "OUT OF MEMORY: %s\n", strerror(errno))
558 olsr_syslog(OLSR_LOG_ERR, "olsrd: out of memory!: %m\n");
559 olsr_exit((char *)id, EXIT_FAILURE);
566 *Wrapper for printf that prints to a specific
567 *debuglevel upper limit
572 olsr_printf(int loglevel, char *format, ...)
576 if((loglevel <= olsr_cnf->debug_level) && debug_handle)
578 va_start(arglist, format);
580 vfprintf(debug_handle, format, arglist);