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(NULL);
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;
219 *Initialize all the tables used(neighbor,
220 *topology, MID, HNA, MPR, dup).
221 *Also initalizes other variables
224 olsr_init_tables(void)
226 changes_topology = OLSR_FALSE;
227 changes_neighborhood = OLSR_FALSE;
228 changes_hna = OLSR_FALSE;
230 /* Set avl tree comparator */
231 if (olsr_cnf->ipsize == 4) {
232 avl_comp_default = NULL;
233 avl_comp_prefix_default = avl_comp_ipv4_prefix;
235 avl_comp_default = avl_comp_ipv6;
236 avl_comp_prefix_default = avl_comp_ipv6_prefix;
239 /* Initialize link set */
240 olsr_init_link_set();
242 /* Initialize duplicate table */
243 olsr_init_duplicate_set();
245 /* Initialize neighbor table */
246 olsr_init_neighbor_table();
248 /* Initialize routing table */
249 olsr_init_routing_table();
251 /* Initialize two hop table */
252 olsr_init_two_hop_table();
254 /* Initialize topology */
257 /* Initialize mpr selector table */
258 olsr_init_mprs_set();
260 /* Initialize MID set */
263 /* Initialize HNA set */
266 /* Start periodic SPF and RIB recalculation */
267 if (olsr_cnf->lq_dinter > 0.0) {
268 olsr_start_timer((unsigned int)(olsr_cnf->lq_dinter * MSEC_PER_SEC), 5,
269 OLSR_TIMER_PERIODIC, &olsr_calculate_routing_table, NULL, 0);
274 *Check if a message is to be forwarded and forward
277 *@param m the OLSR message recieved
278 *@param originator the originator of this message
279 *@param seqno the seqno of the message
281 *@returns positive if forwarded
284 olsr_forward_message(union olsr_message *m,
285 union olsr_ip_addr *from_addr)
287 union olsr_ip_addr *src;
288 struct neighbor_entry *neighbor;
290 struct interface *ifn;
293 * Sven-Ola: We should not flood the mesh with overdue messages. Because
294 * of a bug in parser.c:parse_packet, we have a lot of messages because
295 * all older olsrd's have lq_fish enabled.
297 if (AF_INET == olsr_cnf->ip_version)
299 if (2 > m->v4.ttl || 255 < (int)m->v4.hopcnt + (int)m->v4.ttl) return 0;
303 if (2 > m->v6.ttl || 255 < (int)m->v6.hopcnt + (int)m->v6.ttl) return 0;
306 /* Lookup sender address */
307 src = mid_lookup_main_addr(from_addr);
311 neighbor=olsr_lookup_neighbor_table(src);
315 if(neighbor->status != SYM)
319 if(olsr_lookup_mprs_set(src) == NULL)
323 struct ipaddr_str buf;
325 OLSR_PRINTF(5, "Forward - sender %s not MPR selector\n", olsr_ip_to_string(&buf, src));
330 /* Treat TTL hopcnt */
331 if(olsr_cnf->ip_version == AF_INET)
344 /* Update packet data */
345 msgsize = ntohs(m->v4.olsr_msgsize);
347 /* looping trough interfaces */
348 for (ifn = ifnet; ifn ; ifn = ifn->int_next)
350 if(net_output_pending(ifn))
353 * Check if message is to big to be piggybacked
355 if(net_outbuffer_push(ifn, m, msgsize) != msgsize)
360 set_buffer_timer(ifn);
362 if(net_outbuffer_push(ifn, m, msgsize) != msgsize)
364 OLSR_PRINTF(1, "Received message to big to be forwarded in %s(%d bytes)!", ifn->int_name, msgsize);
365 olsr_syslog(OLSR_LOG_ERR, "Received message to big to be forwarded on %s(%d bytes)!", ifn->int_name, msgsize);
371 /* No forwarding pending */
372 set_buffer_timer(ifn);
374 if(net_outbuffer_push(ifn, m, msgsize) != msgsize)
376 OLSR_PRINTF(1, "Received message to big to be forwarded in %s(%d bytes)!", ifn->int_name, msgsize);
377 olsr_syslog(OLSR_LOG_ERR, "Received message to big to be forwarded on %s(%d bytes)!", ifn->int_name, msgsize);
386 set_buffer_timer(struct interface *ifn)
389 ifn->fwdtimer = GET_TIMESTAMP(random() * olsr_cnf->max_jitter * 1000 / RAND_MAX);
393 olsr_init_willingness(void)
395 if (olsr_cnf->willingness_auto) {
397 /* Run it first and then periodic. */
398 olsr_update_willingness(NULL);
400 olsr_start_timer((unsigned int)olsr_cnf->will_int * MSEC_PER_SEC, 5,
401 OLSR_TIMER_PERIODIC, &olsr_update_willingness, NULL, 0);
406 olsr_update_willingness(void *foo __attribute__((unused)))
408 int tmp_will = olsr_cnf->willingness;
410 /* Re-calculate willingness */
411 olsr_cnf->willingness = olsr_calculate_willingness();
413 if(tmp_will != olsr_cnf->willingness)
415 OLSR_PRINTF(1, "Local willingness updated: old %d new %d\n", tmp_will, olsr_cnf->willingness);
421 *Calculate this nodes willingness to act as a MPR
422 *based on either a fixed value or the power status
423 *of the node using APM
425 *@return a 8bit value from 0-7 representing the willingness
429 olsr_calculate_willingness(void)
431 struct olsr_apm_info ainfo;
433 /* If fixed willingness */
434 if(!olsr_cnf->willingness_auto)
435 return olsr_cnf->willingness;
437 if(apm_read(&ainfo) < 1)
440 apm_printinfo(&ainfo);
443 if(ainfo.ac_line_status == OLSR_AC_POWERED)
446 /* If battery powered
448 * juice > 78% will: 3
449 * 78% > juice > 26% will: 2
450 * 26% > juice will: 1
452 return (ainfo.battery_percentage / 26);
456 olsr_msgtype_to_string(olsr_u8_t msgtype)
458 static char type[20];
470 case(LQ_HELLO_MESSAGE):
478 snprintf(type, sizeof(type), "UNKNOWN(%d)", msgtype);
484 olsr_link_to_string(olsr_u8_t linktype)
486 static char type[20];
504 snprintf(type, sizeof(type), "UNKNOWN(%d)", linktype);
510 olsr_status_to_string(olsr_u8_t status)
512 static char type[20];
526 snprintf(type, sizeof(type), "UNKNOWN(%d)", status);
532 *Termination function to be called whenever a error occures
533 *that requires the daemon to terminate
535 *@param msg the message to write to the syslog and possibly stdout
539 olsr_exit(const char *msg, int val)
541 OLSR_PRINTF(1, "OLSR EXIT: %s\n", msg);
542 olsr_syslog(OLSR_LOG_ERR, "olsrd exit: %s\n", msg);
544 olsr_cnf->exit_value = val;
551 * Wrapper for malloc(3) that does error-checking
553 * @param size the number of bytes to allocalte
554 * @param caller a string identifying the caller for
555 * use in error messaging
557 * @return a void pointer to the memory allocated
560 olsr_malloc(size_t size, const char *id)
565 * Not all the callers do a proper cleaning of memory.
566 * Clean it on behalf of those.
568 ptr = calloc(1, size);
571 const char * const err_msg = strerror(errno);
572 OLSR_PRINTF(1, "OUT OF MEMORY: %s\n", err_msg);
573 olsr_syslog(OLSR_LOG_ERR, "olsrd: out of memory!: %s\n", err_msg);
574 olsr_exit(id, EXIT_FAILURE);
582 *Wrapper for printf that prints to a specific
583 *debuglevel upper limit
588 olsr_printf(int loglevel, const char *format, ...)
590 if((loglevel <= olsr_cnf->debug_level) && debug_handle)
593 va_start(arglist, format);
594 vfprintf(debug_handle, format, arglist);