3 * The olsr.org Optimized Link-State Routing daemon(olsrd)
4 * Copyright (c) 2004-2009, the olsr.org team - see HISTORY file
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of olsr.org, olsrd nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
34 * Visit http://www.olsr.org for more information.
36 * If you find this software useful feel free to make a donation
37 * to the project. For more information see the website or contact
38 * the copyright holders.
43 * All these functions are global
50 #include "duplicate_set.h"
54 #include "scheduler.h"
57 #include "neighbor_table.h"
59 #include "lq_packet.h"
60 #include "common/avl.h"
62 #include "lq_plugin.h"
63 #include "olsr_logging.h"
70 static void olsr_update_willingness(void *);
71 static void olsr_trigger_forced_update(void *);
73 bool changes_topology;
74 bool changes_neighborhood;
79 * Process changes functions
83 int (*function) (int, int, int);
87 static struct pcf *pcf_list;
89 static uint16_t message_seqno;
92 *Initialize the message sequence number as a random value
97 message_seqno = random() & 0xFFFF;
98 OLSR_DEBUG(LOG_MAIN, "Settings initial message sequence number to %u\n", message_seqno);
102 * Get and increment the message sequence number
109 return message_seqno++;
114 register_pcf(int (*f) (int, int, int))
118 OLSR_DEBUG(LOG_MAIN, "Registering pcf function\n");
120 new_pcf = olsr_malloc(sizeof(struct pcf), "New PCF");
122 new_pcf->function = f;
123 new_pcf->next = pcf_list;
130 *Process changes in neighborhood or/and topology.
131 *Re-calculates the neighborhood/topology if there
132 *are any updates - then calls the right functions to
133 *update the routing table.
137 olsr_process_changes(void)
139 struct pcf *tmp_pc_list;
141 if (changes_neighborhood)
142 OLSR_DEBUG(LOG_MAIN, "CHANGES IN NEIGHBORHOOD\n");
143 if (changes_topology)
144 OLSR_DEBUG(LOG_MAIN, "CHANGES IN TOPOLOGY\n");
146 OLSR_DEBUG(LOG_MAIN, "CHANGES IN HNA\n");
148 if (!changes_force && 0 >= olsr_cnf->lq_dlimit)
151 if (!changes_neighborhood && !changes_topology && !changes_hna)
154 if (olsr_cnf->log_target_stderr && olsr_cnf->clear_screen && isatty(STDOUT_FILENO)) {
156 printf(" *** %s (%s on %s) ***\n", olsrd_version, build_date, build_host);
159 if (changes_neighborhood) {
160 olsr_calculate_lq_mpr();
163 /* calculate the routing table */
164 if (changes_neighborhood || changes_topology || changes_hna) {
165 olsr_calculate_routing_table();
168 olsr_print_link_set();
169 olsr_print_neighbor_table();
170 olsr_print_tc_table();
171 olsr_print_mid_set();
172 olsr_print_duplicate_table();
173 olsr_print_hna_set();
175 for (tmp_pc_list = pcf_list; tmp_pc_list != NULL; tmp_pc_list = tmp_pc_list->next) {
176 tmp_pc_list->function(changes_neighborhood, changes_topology, changes_hna);
179 changes_neighborhood = false;
180 changes_topology = false;
182 changes_force = false;
186 * Callback for the periodic route calculation.
189 olsr_trigger_forced_update(void *unused __attribute__ ((unused)))
192 changes_force = true;
193 changes_neighborhood = true;
194 changes_topology = true;
197 olsr_process_changes();
201 *Initialize all the tables used(neighbor,
202 *topology, MID, HNA, MPR, dup).
203 *Also initalizes other variables
206 olsr_init_tables(void)
208 /* Some cookies for stats keeping */
209 static struct olsr_cookie_info *periodic_spf_timer_cookie = NULL;
211 changes_topology = false;
212 changes_neighborhood = false;
215 /* Initialize link set */
216 olsr_init_link_set();
218 /* Initialize duplicate table */
219 olsr_init_duplicate_set();
221 /* Initialize neighbor table */
222 olsr_init_neighbor_table();
224 /* Initialize routing table */
225 olsr_init_routing_table();
227 /* Initialize topology */
230 /* Initialize MID set */
233 /* Initialize HNA set */
236 /* Start periodic SPF and RIB recalculation */
237 if (olsr_cnf->lq_dinter > 0) {
238 periodic_spf_timer_cookie = olsr_alloc_cookie("Periodic SPF", OLSR_COOKIE_TYPE_TIMER);
239 olsr_start_timer(olsr_cnf->lq_dinter, 5,
240 OLSR_TIMER_PERIODIC, &olsr_trigger_forced_update, NULL, periodic_spf_timer_cookie);
245 * Shared code to write the message header
247 uint8_t *olsr_put_msg_hdr(uint8_t **curr, struct olsr_message *msg)
254 pkt_put_u8(curr, msg->type);
255 pkt_put_reltime(curr, msg->vtime);
257 pkt_put_u16(curr, msg->size);
258 pkt_put_ipaddress(curr, &msg->originator);
259 pkt_put_u8(curr, msg->ttl);
260 pkt_put_u8(curr, msg->hopcnt);
261 pkt_put_u16(curr, msg->seqno);
267 *Check if a message is to be forwarded and forward
270 *@param m the OLSR message recieved
272 *@returns positive if forwarded
275 olsr_forward_message(struct olsr_message *msg, uint8_t *binary, struct interface *in_if, union olsr_ip_addr *from_addr)
277 union olsr_ip_addr *src;
278 struct nbr_entry *neighbor;
279 struct interface *ifn;
281 #if !defined REMOVE_LOG_DEBUG
282 struct ipaddr_str buf;
285 /* Lookup sender address */
286 src = olsr_lookup_main_addr_by_alias(from_addr);
290 neighbor = olsr_lookup_nbr_entry(src, true);
292 OLSR_DEBUG(LOG_PACKET_PARSING, "Not forwarding message type %d because no nbr entry found for %s\n",
293 msg->type, olsr_ip_to_string(&buf, src));
296 if (!neighbor->is_sym) {
297 OLSR_DEBUG(LOG_PACKET_PARSING, "Not forwarding message type %d because received by non-symmetric neighbor %s\n",
298 msg->type, olsr_ip_to_string(&buf, src));
303 if (neighbor->mprs_count == 0) {
304 OLSR_DEBUG(LOG_PACKET_PARSING, "Not forwarding message type %d because we are no MPR for %s\n",
305 msg->type, olsr_ip_to_string(&buf, src));
306 /* don't forward packages if not a MPR */
310 /* check if we already forwarded this message */
311 if (olsr_is_duplicate_message(msg, true, NULL)) {
312 OLSR_DEBUG(LOG_PACKET_PARSING, "Not forwarding message type %d from %s because we already forwarded it.\n",
313 msg->type, olsr_ip_to_string(&buf, src));
314 return 0; /* it's a duplicate, forget about it */
317 /* Treat TTL hopcnt */
321 olsr_put_msg_hdr(&tmp, msg);
324 OLSR_DEBUG(LOG_PACKET_PARSING, "Not forwarding message type %d from %s because TTL is 0.\n",
325 msg->type, olsr_ip_to_string(&buf, src));
326 return 0; /* TTL 0, forget about it */
328 OLSR_DEBUG(LOG_PACKET_PARSING, "Forwarding message type %d from %s.\n",
329 msg->type, olsr_ip_to_string(&buf, src));
331 /* looping trough interfaces */
332 OLSR_FOR_ALL_INTERFACES(ifn) {
333 if (net_output_pending(ifn)) {
334 /* dont forward to incoming interface if interface is mode ether */
335 if (in_if->mode == IF_MODE_ETHER && ifn == in_if)
339 * Check if message is to big to be piggybacked
341 if (net_outbuffer_push(ifn, binary, msg->size) != msg->size) {
345 set_buffer_timer(ifn);
347 if (net_outbuffer_push(ifn, binary, msg->size) != msg->size) {
348 OLSR_WARN(LOG_NETWORKING, "Received message to big to be forwarded in %s(%d bytes)!", ifn->int_name, msg->size);
352 /* No forwarding pending */
353 set_buffer_timer(ifn);
355 if (net_outbuffer_push(ifn, binary, msg->size) != msg->size) {
356 OLSR_WARN(LOG_NETWORKING, "Received message to big to be forwarded in %s(%d bytes)!", ifn->int_name, msg->size);
360 OLSR_FOR_ALL_INTERFACES_END(ifn);
366 * Wrapper for the timer callback.
369 olsr_expire_buffer_timer(void *context)
371 struct interface *ifn;
373 ifn = (struct interface *)context;
376 * Clear the pointer to indicate that this timer has
377 * been expired and needs to be restarted in case there
378 * will be another message queued in the future.
380 ifn->buffer_hold_timer = NULL;
383 * Do we have something to emit ?
385 if (!net_output_pending(ifn)) {
389 OLSR_DEBUG(LOG_NETWORKING, "Buffer Holdtimer for %s timed out, sending data.\n", ifn->int_name);
397 * Kick a hold-down timer which defers building of a message.
398 * This has the desired effect that olsr messages get bigger.
401 set_buffer_timer(struct interface *ifn)
405 * Bail if there is already a timer running.
407 if (ifn->buffer_hold_timer) {
412 * This is the first message since the last time this interface has
413 * been drained. Flush the buffer in second or so.
415 ifn->buffer_hold_timer =
416 olsr_start_timer(OLSR_BUFFER_HOLD_TIME, OLSR_BUFFER_HOLD_JITTER,
417 OLSR_TIMER_ONESHOT, &olsr_expire_buffer_timer, ifn, buffer_hold_timer_cookie);
421 olsr_init_willingness(void)
423 /* Some cookies for stats keeping */
424 static struct olsr_cookie_info *willingness_timer_cookie = NULL;
426 if (olsr_cnf->willingness_auto) {
427 OLSR_INFO(LOG_MAIN, "Initialize automatic willingness...\n");
428 /* Run it first and then periodic. */
429 olsr_update_willingness(NULL);
431 willingness_timer_cookie = olsr_alloc_cookie("Update Willingness", OLSR_COOKIE_TYPE_TIMER);
432 olsr_start_timer(olsr_cnf->will_int, 5,
433 OLSR_TIMER_PERIODIC, &olsr_update_willingness, NULL, willingness_timer_cookie);
438 olsr_update_willingness(void *foo __attribute__ ((unused)))
440 int tmp_will = olsr_cnf->willingness;
442 /* Re-calculate willingness */
443 olsr_cnf->willingness = olsr_calculate_willingness();
445 if (tmp_will != olsr_cnf->willingness) {
446 OLSR_INFO(LOG_MAIN, "Local willingness updated: old %d new %d\n", tmp_will, olsr_cnf->willingness);
452 *Calculate this nodes willingness to act as a MPR
453 *based on either a fixed value or the power status
454 *of the node using APM
456 *@return a 8bit value from 0-7 representing the willingness
460 olsr_calculate_willingness(void)
462 struct olsr_apm_info ainfo;
464 /* If fixed willingness */
465 if (!olsr_cnf->willingness_auto)
466 return olsr_cnf->willingness;
468 if (apm_read(&ainfo) < 1)
471 apm_printinfo(&ainfo);
474 if (ainfo.ac_line_status == OLSR_AC_POWERED)
477 /* If battery powered
479 * juice > 78% will: 3
480 * 78% > juice > 26% will: 2
481 * 26% > juice will: 1
483 return (ainfo.battery_percentage / 26);
487 olsr_msgtype_to_string(uint8_t msgtype)
489 static char type[20];
492 case (HELLO_MESSAGE):
500 case (LQ_HELLO_MESSAGE):
502 case (LQ_TC_MESSAGE):
508 snprintf(type, sizeof(type), "UNKNOWN(%d)", msgtype);
514 olsr_link_to_string(uint8_t linktype)
516 static char type[20];
531 snprintf(type, sizeof(type), "UNKNOWN(%d)", linktype);
537 olsr_status_to_string(uint8_t status)
539 static char type[20];
552 snprintf(type, sizeof(type), "UNKNOWN(%d)", status);
558 *Termination function to be called whenever a error occures
559 *that requires the daemon to terminate
561 *@param val the exit code for OLSR
568 olsr_cnf->exit_value = val;
569 if (app_state == STATE_INIT) {
572 app_state = STATE_SHUTDOWN;
577 * Wrapper for malloc(3) that does error-checking
579 * @param size the number of bytes to allocalte
580 * @param caller a string identifying the caller for
581 * use in error messaging
583 * @return a void pointer to the memory allocated
586 olsr_malloc(size_t size, const char *id __attribute__ ((unused)))
591 * Not all the callers do a proper cleaning of memory.
592 * Clean it on behalf of those.
594 ptr = calloc(1, size);
597 OLSR_ERROR(LOG_MAIN, "Out of memory for id '%s': %s\n", id, strerror(errno));
598 olsr_exit(EXIT_FAILURE);
604 * Same as strdup but works with olsr_malloc
607 olsr_strdup(const char *s)
609 char *ret = olsr_malloc(1 + strlen(s), "olsr_strdup");
615 * Same as strndup but works with olsr_malloc
618 olsr_strndup(const char *s, size_t n)
620 size_t len = n < strlen(s) ? n : strlen(s);
621 char *ret = olsr_malloc(1 + len, "olsr_strndup");
622 strncpy(ret, s, len);
630 * indent-tabs-mode: nil