3 * The olsr.org Optimized Link-State Routing daemon(olsrd)
4 * Copyright (c) 2004, Andreas Tønnesen(andreto@olsr.org)
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 * $Id: main.c,v 1.44 2004/11/20 23:37:39 kattemat Exp $
42 #include "interfaces.h"
43 #include "local_hna_set.h"
44 #include "scheduler.h"
46 #include "generate_msg.h"
47 #include "plugin_loader.h"
48 #include "socket_parser.h"
50 #include "link_layer.h"
54 #define close(x) closesocket(x)
55 int __stdcall SignalHandler(unsigned long signal);
56 void ListInterfaces(void);
57 void DisableIcmpRedirects(void);
64 * Local function prototypes
67 olsr_reconfigure(int);
73 set_default_values(void);
76 set_default_ifcnfs(struct olsr_if *, struct if_config_options *);
78 static char **olsr_argv;
85 main(int argc, char *argv[])
87 /* For address convertions */
91 struct if_config_options *default_ifcnf;
94 char conf_file_name[FILENAME_MAX];
103 setbuf(stdout, NULL);
104 setbuf(stderr, NULL);
106 /* Initialize socket list */
107 olsr_socket_entries = NULL;
110 /* Check if user is root */
111 if(getuid() || getgid())
113 fprintf(stderr, "You must be root(uid = 0) to run olsrd!\nExiting\n\n");
117 DisableIcmpRedirects();
119 if (WSAStartup(0x0202, &WsaData))
121 fprintf(stderr, "Could not initialize WinSock.\n");
122 olsr_exit(__func__, EXIT_FAILURE);
127 olsr_openlog("olsrd");
129 /* Set default values */
130 set_default_values();
132 /* Initialize network functions */
135 /* Get initial timestep */
137 while (nowtm == NULL)
139 nowtm = gmtime((time_t *)&now.tv_sec);
142 /* The port to use for OLSR traffic */
143 olsr_udp_port = htons(OLSRPORT);
145 printf("\n *** %s ***\n Build date: %s\n http://www.olsr.org\n\n",
149 /* Using PID as random seed */
154 * Set configfile name and
155 * check if a configfile name was given as parameter
158 GetWindowsDirectory(conf_file_name, FILENAME_MAX - 11);
160 len = strlen(conf_file_name);
162 if (conf_file_name[len - 1] != '\\')
163 conf_file_name[len++] = '\\';
165 strcpy(conf_file_name + len, "olsrd.conf");
167 strncpy(conf_file_name, OLSRD_GLOBAL_CONF_FILE, FILENAME_MAX);
170 if ((argc > 1) && (strcmp(argv[1], "-f") == 0))
175 fprintf(stderr, "You must provide a filename when using the -f switch!\n");
179 if (stat(argv[1], &statbuf) < 0)
181 fprintf(stderr, "Could not find specified config file %s!\n%s\n\n", argv[1], strerror(errno));
185 strncpy(conf_file_name, argv[1], FILENAME_MAX);
191 * set up configuration prior to processing commandline options
193 if((olsr_cnf = olsrd_parse_cnf(conf_file_name)) == NULL)
195 printf("Using default config values(no configfile)\n");
196 olsr_cnf = olsrd_get_default_cnf();
198 if((default_ifcnf = get_default_if_config()) == NULL)
200 fprintf(stderr, "No default ifconfig found!\n");
205 * Process olsrd options.
209 while (argc > 0 && **argv == '-')
215 if (strcmp(*argv, "-int") == 0)
225 if(strcmp(*argv, "-f") == 0)
227 fprintf(stderr, "Configfilename must ALWAYS be first argument!\n\n");
228 olsr_exit(__func__, EXIT_FAILURE);
234 if(strcmp(*argv, "-ipv6") == 0)
236 olsr_cnf->ip_version = AF_INET6;
244 if(strcmp(*argv, "-bcast") == 0)
249 fprintf(stderr, "You must provide a broadcastaddr when using the -bcast switch!\n");
250 olsr_exit(__func__, EXIT_FAILURE);
252 if (inet_aton(*argv, &in) == 0)
254 printf("Invalid broadcast address! %s\nSkipping it!\n", *argv);
257 memcpy(&default_ifcnf->ipv4_broadcast.v4, &in.s_addr, sizeof(olsr_u32_t));
262 * Enable additional debugging information to be logged.
264 if (strcmp(*argv, "-d") == 0)
267 sscanf(*argv,"%d", &olsr_cnf->debug_level);
274 * Interfaces to be used by olsrd.
276 if (strcmp(*argv, "-i") == 0)
279 if(!argc || (*argv[0] == '-'))
281 fprintf(stderr, "You must provide an interface label!\n");
282 olsr_exit(__func__, EXIT_FAILURE);
288 while((argc) && (**argv != '-'))
297 * Set the hello interval to be used by olsrd.
300 if (strcmp(*argv, "-hint") == 0)
303 sscanf(*argv,"%f", &default_ifcnf->hello_params.emission_interval);
304 default_ifcnf->hello_params.validity_time = default_ifcnf->hello_params.emission_interval * 3;
310 * Set the HNA interval to be used by olsrd.
313 if (strcmp(*argv, "-hnaint") == 0)
316 sscanf(*argv,"%f", &default_ifcnf->hna_params.emission_interval);
317 default_ifcnf->hna_params.validity_time = default_ifcnf->hna_params.emission_interval * 3;
323 * Set the MID interval to be used by olsrd.
326 if (strcmp(*argv, "-midint") == 0)
329 sscanf(*argv,"%f", &default_ifcnf->mid_params.emission_interval);
330 default_ifcnf->mid_params.validity_time = default_ifcnf->mid_params.emission_interval * 3;
336 * Set the tc interval to be used by olsrd.
339 if (strcmp(*argv, "-tcint") == 0)
342 sscanf(*argv,"%f", &default_ifcnf->tc_params.emission_interval);
343 default_ifcnf->tc_params.validity_time = default_ifcnf->tc_params.emission_interval * 3;
349 * Set the tos bits to be used by olsrd.
352 if (strcmp(*argv, "-tos") == 0)
355 sscanf(*argv,"%d",(int *)&olsr_cnf->tos);
362 * Set the polling interval to be used by olsrd.
364 if (strcmp(*argv, "-T") == 0)
367 sscanf(*argv,"%f",&olsr_cnf->pollrate);
374 * Should we display the contents of packages beeing sent?
376 if (strcmp(*argv, "-dispin") == 0)
379 disp_pack_in = OLSR_TRUE;
384 * Should we display the contents of incoming packages?
386 if (strcmp(*argv, "-dispout") == 0)
389 disp_pack_out = OLSR_TRUE;
395 * Should we set up and send on a IPC socket for the front-end?
397 if (strcmp(*argv, "-ipc") == 0)
400 olsr_cnf->ipc_connections = 1;
401 olsr_cnf->open_ipc = OLSR_TRUE;
407 * Display link-layer info(experimental)
409 if (strcmp(*argv, "-llinfo") == 0)
417 * IPv6 multicast addr
419 if (strcmp(*argv, "-multi") == 0)
422 if(inet_pton(AF_INET6, *argv, &in6) < 0)
424 fprintf(stderr, "Failed converting IP address %s\n", *argv);
428 memcpy(&default_ifcnf->ipv6_multi_glbl, &in6, sizeof(struct in6_addr));
437 * Should we display the contents of packages beeing sent?
439 if (strcmp(*argv, "-delgw") == 0)
448 olsr_exit(__func__, EXIT_FAILURE);
452 /* Sanity check configuration */
453 if(olsrd_sanity_check_cnf(olsr_cnf) < 0)
456 fprintf(stderr, "Bad configuration!\n");
457 olsr_exit(__func__, EXIT_FAILURE);
461 * Set configuration for command-line specified interfaces
463 set_default_ifcnfs(olsr_cnf->interfaces, default_ifcnf);
466 * Print configuration
468 olsrd_print_cnf(olsr_cnf);
471 *socket for icotl calls
473 if ((ioctl_s = socket(olsr_cnf->ip_version, SOCK_DGRAM, 0)) < 0)
476 olsr_syslog(OLSR_LOG_ERR, "ioctl socket: %m");
477 olsr_exit(__func__, 0);
480 #if defined __FreeBSD__ || defined __MacOSX__
481 if ((rts = socket(PF_ROUTE, SOCK_RAW, 0)) < 0)
483 olsr_syslog(OLSR_LOG_ERR, "routing socket: %m");
484 olsr_exit(__func__, 0);
489 *enable ip forwarding on host
491 enable_ip_forwarding(olsr_cnf->ip_version);
493 /* Initialize parser */
496 /* Initialize message sequencnumber */
499 /* Initialize dynamic willingness calculation */
500 olsr_init_willingness();
503 *Set up willingness/APM
505 if(olsr_cnf->willingness_auto)
509 olsr_printf(1, "Could not read APM info - setting default willingness(%d)\n", WILL_DEFAULT);
511 olsr_syslog(OLSR_LOG_ERR, "Could not read APM info - setting default willingness(%d)\n", WILL_DEFAULT);
513 olsr_cnf->willingness_auto = 0;
514 olsr_cnf->willingness = WILL_DEFAULT;
518 olsr_cnf->willingness = olsr_calculate_willingness();
520 olsr_printf(1, "Willingness set to %d - next update in %.1f secs\n", olsr_cnf->willingness, will_int);
525 *Set ipsize and minimum packetsize
527 if(olsr_cnf->ip_version == AF_INET6)
529 olsr_printf(1, "Using IP version 6\n");
530 ipsize = sizeof(struct in6_addr);
532 minsize = (int)sizeof(olsr_u8_t) * 7; /* Minimum packetsize IPv6 */
536 olsr_printf(1, "Using IP version 4\n");
537 ipsize = sizeof(olsr_u32_t);
539 minsize = (int)sizeof(olsr_u8_t) * 4; /* Minimum packetsize IPv4 */
543 /* Initializing networkinterfaces */
547 if(olsr_cnf->allow_no_interfaces)
549 fprintf(stderr, "No interfaces detected! This might be intentional, but it also might mean that your configuration is fubar.\nI will continue after 5 seconds...\n");
554 fprintf(stderr, "No interfaces detected!\nBailing out!\n");
555 olsr_exit(__func__, EXIT_FAILURE);
559 /* Print heartbeat to stdout */
561 if(olsr_cnf->debug_level > 0 && isatty(STDOUT_FILENO))
562 olsr_register_scheduler_event(&generate_stdout_pulse, NULL, STDOUT_PULSE_INT, 0, NULL);
564 gettimeofday(&now, NULL);
567 /* Initialize the IPC socket */
569 if(olsr_cnf->open_ipc)
573 /* Initialize link-layer notifications */
575 init_link_layer_notification();
578 /* Initialisation of different tables to be used.*/
584 olsr_printf(1, "Main address: %s\n\n", olsr_ip_to_string(&main_addr));
588 if (olsr_cnf->debug_level == 0)
590 printf("%s detattching from the current process...\n", SOFTWARE_VERSION);
602 olsr_syslog(OLSR_LOG_INFO, "%s successfully started", SOFTWARE_VERSION);
608 /* ctrl-C and friends */
610 SetConsoleCtrlHandler(SignalHandler, OLSR_TRUE);
612 signal(SIGHUP, olsr_reconfigure);
613 signal(SIGINT, olsr_shutdown);
614 signal(SIGTERM, olsr_shutdown);
617 /* Register socket poll event */
618 olsr_register_timeout_function(&poll_sockets);
620 /* Starting scheduler */
623 /* Like we're ever going to reach this ;-) */
631 * Reconfigure olsrd. Currently kind of a hack...
636 olsr_reconfigure(int signal)
642 printf("Restarting %s\n", olsr_argv[0]);
643 execv(olsr_argv[0], olsr_argv);
647 printf("RECONFIGURING!\n");
653 *Function called at shutdown
658 SignalHandler(unsigned long signal)
661 olsr_shutdown(int signal)
664 struct interface *ifn;
666 olsr_printf(1, "Received signal %d - shutting down\n", signal);
668 olsr_delete_all_kernel_routes();
670 olsr_printf(1, "Closing sockets...\n");
672 /* front-end IPC socket */
673 if(olsr_cnf->open_ipc)
677 for (ifn = ifnet; ifn; ifn = ifn->int_next)
678 close(ifn->olsr_socket);
680 /* Closing plug-ins */
681 olsr_close_plugins();
683 /* Reset network settings */
684 restore_settings(olsr_cnf->ip_version);
689 #if defined __FreeBSD__ || defined __MacOSX__
694 olsr_syslog(OLSR_LOG_INFO, "%s stopped", SOFTWARE_VERSION);
696 olsr_printf(1, "\n <<<< %s - terminating >>>>\n http://www.olsr.org\n", SOFTWARE_VERSION);
706 *Sets the default values of variables at startup
711 memset(&main_addr, 0, sizeof(union olsr_ip_addr));
712 memset(&null_addr6, 0, sizeof (union olsr_ip_addr));
714 exit_value = EXIT_SUCCESS;
715 /* If the application exits by signal it is concidered success,
716 * if not, exit_value is set by the function calling olsr_exit.
721 dup_hold_time = DUP_HOLD_TIME;
723 will_int = 10 * HELLO_INTERVAL; /* Willingness update interval */
726 del_gws = OLSR_FALSE;
728 /* Display packet content */
729 disp_pack_in = OLSR_FALSE;
730 disp_pack_out = OLSR_FALSE;
740 fprintf(stderr, "An error occured somwhere between your keyboard and your chair!\n");
741 fprintf(stderr, "usage: olsrd [-f <configfile>] [ -i interface1 interface2 ... ]\n");
742 fprintf(stderr, " [-d <debug_level>] [-ipv6] [-multi <IPv6 multicast address>]\n");
743 fprintf(stderr, " [-bcast <broadcastaddr>] [-ipc] [-dispin] [-dispout] [-delgw]\n");
744 fprintf(stderr, " [-hint <hello interval (secs)>] [-tcint <tc interval (secs)>]\n");
745 fprintf(stderr, " [-midint <mid interval (secs)>] [-hnaint <hna interval (secs)>]\n");
746 fprintf(stderr, " [-tos value (int)] [-T <Polling Rate (secs)>]\n");
752 * Sets the provided configuration on all unconfigured
756 set_default_ifcnfs(struct olsr_if *ifs, struct if_config_options *cnf)
764 ifs->cnf = olsr_malloc(sizeof(struct if_config_options), "Set default config");