2 * The olsr.org Optimized Link-State Routing daemon(olsrd)
3 * Copyright (c) 2004-2009, the olsr.org team - see HISTORY file
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.
42 * All these functions are global
48 #include "two_hop_neighbor_table.h"
50 #include "duplicate_set.h"
51 #include "mpr_selector_set.h"
56 #include "scheduler.h"
59 #include "neighbor_table.h"
61 #include "lq_packet.h"
62 #include "common/avl.h"
64 #include "lq_plugin.h"
71 static void olsr_update_willingness(void *);
72 static void olsr_trigger_forced_update(void *);
74 bool changes_topology;
75 bool changes_neighborhood;
80 * Process changes functions
85 int (*function)(int, int, int);
89 static struct pcf *pcf_list;
91 static uint16_t message_seqno;
94 *Initialize the message sequence number as a random value
99 message_seqno = random() & 0xFFFF;
103 * Get and increment the message sequence number
110 return message_seqno++;
115 register_pcf(int (*f)(int, int, int))
119 OLSR_PRINTF(1, "Registering pcf function\n");
121 new_pcf = olsr_malloc(sizeof(struct pcf), "New PCF");
123 new_pcf->function = f;
124 new_pcf->next = pcf_list;
131 *Process changes in neighborhood or/and topology.
132 *Re-calculates the neighborhood/topology if there
133 *are any updates - then calls the right functions to
134 *update the routing table.
138 olsr_process_changes(void)
140 struct pcf *tmp_pc_list;
143 if(changes_neighborhood)
144 OLSR_PRINTF(3, "CHANGES IN NEIGHBORHOOD\n");
146 OLSR_PRINTF(3, "CHANGES IN TOPOLOGY\n");
148 OLSR_PRINTF(3, "CHANGES IN HNA\n");
152 0 >= olsr_cnf->lq_dlimit)
155 if(!changes_neighborhood &&
160 if (olsr_cnf->debug_level > 0 && olsr_cnf->clear_screen && isatty(1))
163 printf(" *** %s (%s on %s) ***\n", olsrd_version, build_date, build_host);
166 if (changes_neighborhood) {
167 olsr_calculate_lq_mpr();
170 /* calculate the routing table */
171 if (changes_neighborhood || changes_topology || changes_hna) {
172 olsr_calculate_routing_table();
175 if (olsr_cnf->debug_level > 0)
177 if (olsr_cnf->debug_level > 2)
179 olsr_print_mid_set();
181 if (olsr_cnf->debug_level > 3)
183 if (olsr_cnf->debug_level > 8)
185 olsr_print_duplicate_table();
187 olsr_print_hna_set();
192 olsr_print_link_set();
193 olsr_print_neighbor_table();
194 olsr_print_two_hop_neighbor_table();
195 olsr_print_tc_table();
199 for(tmp_pc_list = pcf_list;
201 tmp_pc_list = tmp_pc_list->next)
203 tmp_pc_list->function(changes_neighborhood,
208 changes_neighborhood = false;
209 changes_topology = false;
211 changes_force = false;
215 * Callback for the periodic route calculation.
218 olsr_trigger_forced_update(void *unused __attribute__((unused))) {
220 changes_force = true;
221 changes_neighborhood = true;
222 changes_topology = true;
225 olsr_process_changes();
229 *Initialize all the tables used(neighbor,
230 *topology, MID, HNA, MPR, dup).
231 *Also initalizes other variables
234 olsr_init_tables(void)
236 /* Some cookies for stats keeping */
237 static struct olsr_cookie_info *periodic_spf_timer_cookie = NULL;
239 changes_topology = false;
240 changes_neighborhood = false;
243 /* Initialize link set */
244 olsr_init_link_set();
246 /* Initialize duplicate table */
247 olsr_init_duplicate_set();
249 /* Initialize neighbor table */
250 olsr_init_neighbor_table();
252 /* Initialize routing table */
253 olsr_init_routing_table();
255 /* Initialize two hop table */
256 olsr_init_two_hop_table();
258 /* Initialize topology */
261 /* Initialize MID set */
264 /* Initialize HNA set */
267 /* Initialize MPRS */
271 /* Initialize Layer 1/2 database */
272 olsr_initialize_layer12();
275 /* Start periodic SPF and RIB recalculation */
276 if (olsr_cnf->lq_dinter > 0.0) {
277 periodic_spf_timer_cookie = olsr_alloc_cookie("Periodic SPF",
278 OLSR_COOKIE_TYPE_TIMER);
279 olsr_start_timer((unsigned int)(olsr_cnf->lq_dinter * MSEC_PER_SEC), 5,
280 OLSR_TIMER_PERIODIC, &olsr_trigger_forced_update, NULL,
281 periodic_spf_timer_cookie->ci_id);
286 *Check if a message is to be forwarded and forward
289 *@param m the OLSR message recieved
291 *@returns positive if forwarded
294 olsr_forward_message(union olsr_message *m, struct interface *in_if,
295 union olsr_ip_addr *from_addr)
297 union olsr_ip_addr *src;
298 struct neighbor_entry *neighbor;
300 struct interface *ifn;
303 * Sven-Ola: We should not flood the mesh with overdue messages. Because
304 * of a bug in parser.c:parse_packet, we have a lot of messages because
305 * all older olsrd's have lq_fish enabled.
307 if (AF_INET == olsr_cnf->ip_version)
309 if (m->v4.ttl < 2 || 255 < (int)m->v4.hopcnt + (int)m->v4.ttl) return 0;
313 if (m->v6.ttl < 2|| 255 < (int)m->v6.hopcnt + (int)m->v6.ttl) return 0;
316 /* Lookup sender address */
317 src = olsr_lookup_main_addr_by_alias(from_addr);
321 neighbor=olsr_lookup_neighbor_table(src);
325 if(neighbor->status != SYM)
329 if(olsr_lookup_mprs_set(src) == NULL)
332 struct ipaddr_str buf;
333 OLSR_PRINTF(5, "Forward - sender %s not MPR selector\n", olsr_ip_to_string(&buf, src));
338 /* check if we already forwarded this message */
339 if (olsr_message_is_duplicate(m)) {
340 return 0; /* it's a duplicate, forget about it */
343 /* Treat TTL hopcnt */
344 if(olsr_cnf->ip_version == AF_INET)
357 /* Update packet data */
358 msgsize = ntohs(m->v4.olsr_msgsize);
360 /* looping trough interfaces */
361 OLSR_FOR_ALL_INTERFACES(ifn) {
362 if(net_output_pending(ifn))
364 /* dont forward to incoming interface if interface is mode ether */
365 if (in_if->mode == IF_MODE_ETHER && ifn == in_if)
369 * Check if message is to big to be piggybacked
371 if(net_outbuffer_push(ifn, m, msgsize) != msgsize)
376 set_buffer_timer(ifn);
378 if(net_outbuffer_push(ifn, m, msgsize) != msgsize)
380 OLSR_WARN(LOG_NETWORKING, "Received message to big to be forwarded in %s(%d bytes)!", ifn->int_name, msgsize);
386 /* No forwarding pending */
387 set_buffer_timer(ifn);
389 if(net_outbuffer_push(ifn, m, msgsize) != msgsize)
391 OLSR_WARN(LOG_NETWORKING, "Received message to big to be forwarded in %s(%d bytes)!", ifn->int_name, msgsize);
394 } OLSR_FOR_ALL_INTERFACES_END(ifn);
400 * Wrapper for the timer callback.
403 olsr_expire_buffer_timer(void *context)
405 struct interface *ifn;
407 ifn = (struct interface *)context;
410 * Clear the pointer to indicate that this timer has
411 * been expired and needs to be restarted in case there
412 * will be another message queued in the future.
414 ifn->buffer_hold_timer = NULL;
417 OLSR_PRINTF(1, "Buffer Holdtimer for %s timed out\n", ifn->int_name);
421 * Do we have something to emit ?
423 if (!net_output_pending(ifn)) {
433 * Kick a hold-down timer which defers building of a message.
434 * This has the desired effect that olsr messages get bigger.
437 set_buffer_timer(struct interface *ifn)
441 * Bail if there is already a timer running.
443 if (ifn->buffer_hold_timer) {
448 * This is the first message since the last time this interface has
449 * been drained. Flush the buffer in second or so.
451 ifn->buffer_hold_timer =
452 olsr_start_timer(OLSR_BUFFER_HOLD_TIME, OLSR_BUFFER_HOLD_JITTER,
453 OLSR_TIMER_ONESHOT, &olsr_expire_buffer_timer, ifn,
454 buffer_hold_timer_cookie->ci_id);
458 olsr_init_willingness(void)
460 /* Some cookies for stats keeping */
461 static struct olsr_cookie_info *willingness_timer_cookie = NULL;
463 if (olsr_cnf->willingness_auto) {
465 /* Run it first and then periodic. */
466 olsr_update_willingness(NULL);
468 willingness_timer_cookie = olsr_alloc_cookie("Update Willingness",
469 OLSR_COOKIE_TYPE_TIMER);
470 olsr_start_timer((unsigned int)olsr_cnf->will_int * MSEC_PER_SEC, 5,
471 OLSR_TIMER_PERIODIC, &olsr_update_willingness, NULL,
472 willingness_timer_cookie->ci_id);
477 olsr_update_willingness(void *foo __attribute__((unused)))
479 int tmp_will = olsr_cnf->willingness;
481 /* Re-calculate willingness */
482 olsr_cnf->willingness = olsr_calculate_willingness();
484 if(tmp_will != olsr_cnf->willingness)
486 OLSR_PRINTF(1, "Local willingness updated: old %d new %d\n", tmp_will, olsr_cnf->willingness);
492 *Calculate this nodes willingness to act as a MPR
493 *based on either a fixed value or the power status
494 *of the node using APM
496 *@return a 8bit value from 0-7 representing the willingness
500 olsr_calculate_willingness(void)
502 struct olsr_apm_info ainfo;
504 /* If fixed willingness */
505 if(!olsr_cnf->willingness_auto)
506 return olsr_cnf->willingness;
508 if(apm_read(&ainfo) < 1)
511 apm_printinfo(&ainfo);
514 if(ainfo.ac_line_status == OLSR_AC_POWERED)
517 /* If battery powered
519 * juice > 78% will: 3
520 * 78% > juice > 26% will: 2
521 * 26% > juice will: 1
523 return (ainfo.battery_percentage / 26);
527 olsr_msgtype_to_string(uint8_t msgtype)
529 static char type[20];
541 case(LQ_HELLO_MESSAGE):
549 snprintf(type, sizeof(type), "UNKNOWN(%d)", msgtype);
555 olsr_link_to_string(uint8_t linktype)
557 static char type[20];
575 snprintf(type, sizeof(type), "UNKNOWN(%d)", linktype);
581 olsr_status_to_string(uint8_t status)
583 static char type[20];
597 snprintf(type, sizeof(type), "UNKNOWN(%d)", status);
603 *Termination function to be called whenever a error occures
604 *that requires the daemon to terminate
606 *@param val the exit code for OLSR
613 olsr_cnf->exit_value = val;
614 if (app_state == STATE_INIT) {
617 app_state = STATE_SHUTDOWN;
622 * Wrapper for malloc(3) that does error-checking
624 * @param size the number of bytes to allocalte
625 * @param caller a string identifying the caller for
626 * use in error messaging
628 * @return a void pointer to the memory allocated
631 olsr_malloc(size_t size, const char *id __attribute__((unused)))
636 * Not all the callers do a proper cleaning of memory.
637 * Clean it on behalf of those.
639 ptr = calloc(1, size);
642 OLSR_ERROR(LOG_MAIN, "Out of memory for id '%s': %s\n", id, strerror(errno));
643 olsr_exit(EXIT_FAILURE);
647 /* useful for debugging */
648 OLSR_PRINTF(1, "MEMORY: alloc %s %p, %u bytes\n",
656 * Same as strdup but works with olsr_malloc
659 olsr_strdup(const char *s)
661 char *ret = olsr_malloc(1 + strlen(s), "olsr_strdup");
667 * Same as strndup but works with olsr_malloc
670 olsr_strndup(const char *s, size_t n)
672 size_t len = n < strlen(s) ? n : strlen(s);
673 char *ret = olsr_malloc(1 + len, "olsr_strndup");
674 strncpy(ret, s, len);
682 * indent-tabs-mode: nil