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.
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"
69 olsr_bool changes_topology;
70 olsr_bool changes_neighborhood;
71 olsr_bool changes_hna;
72 olsr_bool changes_force;
75 * Process changes functions
80 int (*function)(int, int, int);
84 static struct pcf *pcf_list;
86 static olsr_u16_t message_seqno;
89 *Initialize the message sequence number as a random value
94 message_seqno = random() & 0xFFFF;
98 * Get and increment the message sequence number
105 return message_seqno++;
110 register_pcf(int (*f)(int, int, int))
114 OLSR_PRINTF(1, "Registering pcf function\n");
116 new_pcf = olsr_malloc(sizeof(struct pcf), "New PCF");
118 new_pcf->function = f;
119 new_pcf->next = pcf_list;
126 *Process changes in neighborhood or/and topology.
127 *Re-calculates the neighborhooh/topology if there
128 *are any updates - then calls the right functions to
129 *update the routing table.
133 olsr_process_changes(void)
135 struct pcf *tmp_pc_list;
138 if(changes_neighborhood)
139 OLSR_PRINTF(3, "CHANGES IN NEIGHBORHOOD\n");
141 OLSR_PRINTF(3, "CHANGES IN TOPOLOGY\n");
143 OLSR_PRINTF(3, "CHANGES IN HNA\n");
147 2 <= olsr_cnf->lq_level &&
148 0 >= olsr_cnf->lq_dlimit)
151 if(!changes_neighborhood &&
156 if (olsr_cnf->debug_level > 0 && olsr_cnf->clear_screen && isatty(1))
159 printf(" *** %s (%s on %s) ***\n", olsrd_version, build_date, build_host);
162 if (changes_neighborhood) {
163 if (olsr_cnf->lq_level < 1) {
164 olsr_calculate_mpr();
166 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 = OLSR_FALSE;
209 changes_topology = OLSR_FALSE;
210 changes_hna = OLSR_FALSE;
211 changes_force = OLSR_FALSE;
217 olsr_trigger_forced_update(void *unused __attribute__((unused))) {
218 changes_force = OLSR_TRUE;
219 changes_neighborhood = OLSR_TRUE;
220 changes_topology = OLSR_TRUE;
221 changes_hna = OLSR_TRUE;
223 olsr_process_changes();
227 *Initialize all the tables used(neighbor,
228 *topology, MID, HNA, MPR, dup).
229 *Also initalizes other variables
232 olsr_init_tables(void)
234 changes_topology = OLSR_FALSE;
235 changes_neighborhood = OLSR_FALSE;
236 changes_hna = OLSR_FALSE;
238 /* Set avl tree comparator */
239 if (olsr_cnf->ipsize == 4) {
240 avl_comp_default = avl_comp_ipv4;
241 avl_comp_prefix_default = avl_comp_ipv4_prefix;
243 avl_comp_default = avl_comp_ipv6;
244 avl_comp_prefix_default = avl_comp_ipv6_prefix;
247 /* Initialize link set */
248 olsr_init_link_set();
250 /* Initialize duplicate table */
251 olsr_init_duplicate_set();
253 /* Initialize neighbor table */
254 olsr_init_neighbor_table();
256 /* Initialize routing table */
257 olsr_init_routing_table();
259 /* Initialize two hop table */
260 olsr_init_two_hop_table();
262 /* Initialize topology */
265 /* Initialize mpr selector table */
266 olsr_init_mprs_set();
268 /* Initialize MID set */
271 /* Initialize HNA set */
274 /* Initialize Layer 1/2 database */
275 // olsr_initialize_layer12();
277 /* Start periodic SPF and RIB recalculation */
278 if (olsr_cnf->lq_dinter > 0.0) {
279 olsr_start_timer((unsigned int)(olsr_cnf->lq_dinter * MSEC_PER_SEC), 5,
280 OLSR_TIMER_PERIODIC, &olsr_trigger_forced_update, NULL, 0);
285 *Check if a message is to be forwarded and forward
288 *@param m the OLSR message recieved
289 *@param originator the originator of this message
290 *@param seqno the seqno of the message
292 *@returns positive if forwarded
295 olsr_forward_message(union olsr_message *m,
296 union olsr_ip_addr *from_addr)
298 union olsr_ip_addr *src;
299 struct neighbor_entry *neighbor;
301 struct interface *ifn;
304 * Sven-Ola: We should not flood the mesh with overdue messages. Because
305 * of a bug in parser.c:parse_packet, we have a lot of messages because
306 * all older olsrd's have lq_fish enabled.
308 if (AF_INET == olsr_cnf->ip_version)
310 if (2 > m->v4.ttl || 255 < (int)m->v4.hopcnt + (int)m->v4.ttl) return 0;
314 if (2 > m->v6.ttl || 255 < (int)m->v6.hopcnt + (int)m->v6.ttl) return 0;
317 /* Lookup sender address */
318 src = mid_lookup_main_addr(from_addr);
322 neighbor=olsr_lookup_neighbor_table(src);
326 if(neighbor->status != SYM)
330 if(olsr_lookup_mprs_set(src) == NULL)
334 struct ipaddr_str buf;
336 OLSR_PRINTF(5, "Forward - sender %s not MPR selector\n", olsr_ip_to_string(&buf, src));
341 /* Treat TTL hopcnt */
342 if(olsr_cnf->ip_version == AF_INET)
355 /* Update packet data */
356 msgsize = ntohs(m->v4.olsr_msgsize);
358 /* looping trough interfaces */
359 for (ifn = ifnet; ifn ; ifn = ifn->int_next)
361 if(net_output_pending(ifn))
364 * Check if message is to big to be piggybacked
366 if(net_outbuffer_push(ifn, m, msgsize) != msgsize)
371 set_buffer_timer(ifn);
373 if(net_outbuffer_push(ifn, m, msgsize) != msgsize)
375 OLSR_PRINTF(1, "Received message to big to be forwarded in %s(%d bytes)!", ifn->int_name, msgsize);
376 olsr_syslog(OLSR_LOG_ERR, "Received message to big to be forwarded on %s(%d bytes)!", ifn->int_name, msgsize);
382 /* No forwarding pending */
383 set_buffer_timer(ifn);
385 if(net_outbuffer_push(ifn, m, msgsize) != msgsize)
387 OLSR_PRINTF(1, "Received message to big to be forwarded in %s(%d bytes)!", ifn->int_name, msgsize);
388 olsr_syslog(OLSR_LOG_ERR, "Received message to big to be forwarded on %s(%d bytes)!", ifn->int_name, msgsize);
397 set_buffer_timer(struct interface *ifn)
400 ifn->fwdtimer = GET_TIMESTAMP(random() * olsr_cnf->max_jitter * 1000 / RAND_MAX);
404 olsr_init_willingness(void)
406 if (olsr_cnf->willingness_auto) {
408 /* Run it first and then periodic. */
409 olsr_update_willingness(NULL);
411 olsr_start_timer((unsigned int)olsr_cnf->will_int * MSEC_PER_SEC, 5,
412 OLSR_TIMER_PERIODIC, &olsr_update_willingness, NULL, 0);
417 olsr_update_willingness(void *foo __attribute__((unused)))
419 int tmp_will = olsr_cnf->willingness;
421 /* Re-calculate willingness */
422 olsr_cnf->willingness = olsr_calculate_willingness();
424 if(tmp_will != olsr_cnf->willingness)
426 OLSR_PRINTF(1, "Local willingness updated: old %d new %d\n", tmp_will, olsr_cnf->willingness);
432 *Calculate this nodes willingness to act as a MPR
433 *based on either a fixed value or the power status
434 *of the node using APM
436 *@return a 8bit value from 0-7 representing the willingness
440 olsr_calculate_willingness(void)
442 struct olsr_apm_info ainfo;
444 /* If fixed willingness */
445 if(!olsr_cnf->willingness_auto)
446 return olsr_cnf->willingness;
448 if(apm_read(&ainfo) < 1)
451 apm_printinfo(&ainfo);
454 if(ainfo.ac_line_status == OLSR_AC_POWERED)
457 /* If battery powered
459 * juice > 78% will: 3
460 * 78% > juice > 26% will: 2
461 * 26% > juice will: 1
463 return (ainfo.battery_percentage / 26);
467 olsr_msgtype_to_string(olsr_u8_t msgtype)
469 static char type[20];
481 case(LQ_HELLO_MESSAGE):
489 snprintf(type, sizeof(type), "UNKNOWN(%d)", msgtype);
495 olsr_link_to_string(olsr_u8_t linktype)
497 static char type[20];
515 snprintf(type, sizeof(type), "UNKNOWN(%d)", linktype);
521 olsr_status_to_string(olsr_u8_t status)
523 static char type[20];
537 snprintf(type, sizeof(type), "UNKNOWN(%d)", status);
543 *Termination function to be called whenever a error occures
544 *that requires the daemon to terminate
546 *@param msg the message to write to the syslog and possibly stdout
550 olsr_exit(const char *msg, int val)
552 OLSR_PRINTF(1, "OLSR EXIT: %s\n", msg);
553 olsr_syslog(OLSR_LOG_ERR, "olsrd exit: %s\n", msg);
555 olsr_cnf->exit_value = val;
562 * Wrapper for malloc(3) that does error-checking
564 * @param size the number of bytes to allocalte
565 * @param caller a string identifying the caller for
566 * use in error messaging
568 * @return a void pointer to the memory allocated
571 olsr_malloc(size_t size, const char *id)
576 * Not all the callers do a proper cleaning of memory.
577 * Clean it on behalf of those.
579 ptr = calloc(1, size);
582 const char * const err_msg = strerror(errno);
583 OLSR_PRINTF(1, "OUT OF MEMORY: %s\n", err_msg);
584 olsr_syslog(OLSR_LOG_ERR, "olsrd: out of memory!: %s\n", err_msg);
585 olsr_exit(id, EXIT_FAILURE);
589 /* useful for debugging */
590 olsr_printf(1, "MEMORY: alloc %s %p, %u bytes\n",
599 *Wrapper for printf that prints to a specific
600 *debuglevel upper limit
605 olsr_printf(int loglevel, const char *format, ...)
607 if((loglevel <= olsr_cnf->debug_level) && debug_handle)
610 va_start(arglist, format);
611 vfprintf(debug_handle, format, arglist);