2 * OLSR ad-hoc routing table management protocol
3 * Copyright (C) 2004 Andreas Tønnesen (andreto@ifi.uio.no)
5 * This file is part of the olsr.org OLSR daemon.
7 * olsr.org is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * olsr.org is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with olsr.org; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * $Id: main.c,v 1.38 2004/11/12 22:11:54 kattemat Exp $
31 #include "interfaces.h"
32 #include "local_hna_set.h"
33 #include "scheduler.h"
35 #include "generate_msg.h"
36 #include "plugin_loader.h"
37 #include "socket_parser.h"
39 #include "link_layer.h"
43 #include "linux/tunnel.h"
47 #define close(x) closesocket(x)
48 int __stdcall SignalHandler(unsigned long signal);
49 void ListInterfaces(void);
56 * Local function prototypes
63 set_default_values(void);
66 set_default_ifcnfs(struct olsr_if *, struct if_config_options *);
74 main(int argc, char *argv[])
76 /* For address convertions */
80 struct if_config_options *default_ifcnf;
83 char conf_file_name[FILENAME_MAX];
93 /* Initialize socket list */
94 olsr_socket_entries = NULL;
97 /* Check if user is root */
98 if(getuid() || getgid())
100 fprintf(stderr, "You must be root(uid = 0) to run olsrd!\nExiting\n\n");
104 if (WSAStartup(0x0202, &WsaData))
106 fprintf(stderr, "Could not initialize WinSock.\n");
107 olsr_exit(__func__, EXIT_FAILURE);
112 olsr_openlog("olsrd");
114 /* Set default values */
115 set_default_values();
117 /* Initialize network functions */
120 /* Get initial timestep */
122 while (nowtm == NULL)
124 nowtm = gmtime((time_t *)&now.tv_sec);
127 /* The port to use for OLSR traffic */
128 olsr_udp_port = htons(OLSRPORT);
130 printf("\n *** %s ***\n Build date: %s\n http://www.olsr.org\n\n",
134 /* Using PID as random seed */
139 * Set configfile name and
140 * check if a configfile name was given as parameter
143 GetWindowsDirectory(conf_file_name, FILENAME_MAX - 11);
145 len = strlen(conf_file_name);
147 if (conf_file_name[len - 1] != '\\')
148 conf_file_name[len++] = '\\';
150 strcpy(conf_file_name + len, "olsrd.conf");
152 strncpy(conf_file_name, OLSRD_GLOBAL_CONF_FILE, FILENAME_MAX);
155 if ((argc > 1) && (strcmp(argv[1], "-f") == 0))
160 fprintf(stderr, "You must provide a filename when using the -f switch!\n");
164 if (stat(argv[1], &statbuf) < 0)
166 fprintf(stderr, "Could not find specified config file %s!\n%s\n\n", argv[1], strerror(errno));
170 strncpy(conf_file_name, argv[1], FILENAME_MAX);
176 * set up configuration prior to processing commandline options
178 if((olsr_cnf = olsrd_parse_cnf(conf_file_name)) == NULL)
180 printf("Using default config values(no configfile)\n");
181 olsr_cnf = olsrd_get_default_cnf();
183 if((default_ifcnf = get_default_if_config()) == NULL)
185 fprintf(stderr, "No default ifconfig found!\n");
190 * Process olsrd options.
194 while (argc > 0 && **argv == '-')
200 if (strcmp(*argv, "-int") == 0)
210 if(strcmp(*argv, "-f") == 0)
212 fprintf(stderr, "Configfilename must ALWAYS be first argument!\n\n");
213 olsr_exit(__func__, EXIT_FAILURE);
219 if(strcmp(*argv, "-ipv6") == 0)
221 olsr_cnf->ip_version = AF_INET6;
229 if(strcmp(*argv, "-bcast") == 0)
234 fprintf(stderr, "You must provide a broadcastaddr when using the -bcast switch!\n");
235 olsr_exit(__func__, EXIT_FAILURE);
237 if (inet_aton(*argv, &in) == 0)
239 printf("Invalid broadcast address! %s\nSkipping it!\n", *argv);
242 memcpy(&default_ifcnf->ipv4_broadcast.v4, &in.s_addr, sizeof(olsr_u32_t));
247 * Enable additional debugging information to be logged.
249 if (strcmp(*argv, "-d") == 0)
252 sscanf(*argv,"%d", &olsr_cnf->debug_level);
259 * Interfaces to be used by olsrd.
261 if (strcmp(*argv, "-i") == 0)
264 if(!argc || (*argv[0] == '-'))
266 fprintf(stderr, "You must provide an interface label!\n");
267 olsr_exit(__func__, EXIT_FAILURE);
273 while((argc) && (**argv != '-'))
282 * Set the hello interval to be used by olsrd.
285 if (strcmp(*argv, "-hint") == 0)
288 sscanf(*argv,"%f", &default_ifcnf->hello_params.emission_interval);
289 default_ifcnf->hello_params.validity_time = default_ifcnf->hello_params.emission_interval * 3;
295 * Set the HNA interval to be used by olsrd.
298 if (strcmp(*argv, "-hnaint") == 0)
301 sscanf(*argv,"%f", &default_ifcnf->hna_params.emission_interval);
302 default_ifcnf->hna_params.validity_time = default_ifcnf->hna_params.emission_interval * 3;
308 * Set the MID interval to be used by olsrd.
311 if (strcmp(*argv, "-midint") == 0)
314 sscanf(*argv,"%f", &default_ifcnf->mid_params.emission_interval);
315 default_ifcnf->mid_params.validity_time = default_ifcnf->mid_params.emission_interval * 3;
321 * Set the tc interval to be used by olsrd.
324 if (strcmp(*argv, "-tcint") == 0)
327 sscanf(*argv,"%f", &default_ifcnf->tc_params.emission_interval);
328 default_ifcnf->tc_params.validity_time = default_ifcnf->tc_params.emission_interval * 3;
334 * Set the tos bits to be used by olsrd.
337 if (strcmp(*argv, "-tos") == 0)
340 sscanf(*argv,"%d",(int *)&olsr_cnf->tos);
347 * Set the polling interval to be used by olsrd.
349 if (strcmp(*argv, "-T") == 0)
352 sscanf(*argv,"%f",&olsr_cnf->pollrate);
359 * Should we display the contents of packages beeing sent?
361 if (strcmp(*argv, "-dispin") == 0)
364 disp_pack_in = OLSR_TRUE;
369 * Should we display the contents of incoming packages?
371 if (strcmp(*argv, "-dispout") == 0)
374 disp_pack_out = OLSR_TRUE;
380 * Should we set up and send on a IPC socket for the front-end?
382 if (strcmp(*argv, "-ipc") == 0)
385 olsr_cnf->ipc_connections = 1;
386 olsr_cnf->open_ipc = OLSR_TRUE;
392 * Display link-layer info(experimental)
394 if (strcmp(*argv, "-llinfo") == 0)
402 * Use Internet gateway tunneling?
404 if (strcmp(*argv, "-tnl") == 0)
407 use_tunnel = OLSR_TRUE;
413 * IPv6 multicast addr
415 if (strcmp(*argv, "-multi") == 0)
418 if(inet_pton(AF_INET6, *argv, &in6) < 0)
420 fprintf(stderr, "Failed converting IP address %s\n", *argv);
424 memcpy(&default_ifcnf->ipv6_multi_glbl, &in6, sizeof(struct in6_addr));
433 * Should we display the contents of packages beeing sent?
435 if (strcmp(*argv, "-delgw") == 0)
444 olsr_exit(__func__, EXIT_FAILURE);
449 *Interfaces need to be specified
451 if(olsr_cnf->interfaces == NULL)
453 fprintf(stderr, "OLSRD: no interfaces specified!\nuse the -i switch to specify interface(s)\nor set interface(s) in the configuration file!\n");
455 olsr_exit(__func__, EXIT_FAILURE);
459 * Set configuration for command-line specified interfaces
461 set_default_ifcnfs(olsr_cnf->interfaces, default_ifcnf);
464 * Print configuration
466 olsrd_print_cnf(olsr_cnf);
469 *socket for icotl calls
471 if ((ioctl_s = socket(olsr_cnf->ip_version, SOCK_DGRAM, 0)) < 0)
474 olsr_syslog(OLSR_LOG_ERR, "ioctl socket: %m");
475 olsr_exit(__func__, 0);
478 #if defined __FreeBSD__ || defined __MacOSX__
479 if ((rts = socket(PF_ROUTE, SOCK_RAW, 0)) < 0)
481 olsr_syslog(OLSR_LOG_ERR, "routing socket: %m");
482 olsr_exit(__func__, 0);
487 *enable ip forwarding on host
489 enable_ip_forwarding(olsr_cnf->ip_version);
491 /* Initialize parser */
494 /* Initialize message sequencnumber */
497 /* Initialize dynamic willingness calculation */
498 olsr_init_willingness();
500 /* Sanity check for hysteresis values */
501 if((olsr_cnf->use_hysteresis) &&
502 (olsr_cnf->hysteresis_param.thr_high <= olsr_cnf->hysteresis_param.thr_low))
504 printf("Hysteresis threshold high lower than threshold low!!\nEdit the configuration file to fix this!\n\n");
505 olsr_exit(__func__, EXIT_FAILURE);
509 *Set up willingness/APM
511 if(olsr_cnf->willingness_auto)
515 olsr_printf(1, "Could not read APM info - setting default willingness(%d)\n", WILL_DEFAULT);
517 olsr_syslog(OLSR_LOG_ERR, "Could not read APM info - setting default willingness(%d)\n", WILL_DEFAULT);
519 olsr_cnf->willingness_auto = 0;
520 olsr_cnf->willingness = WILL_DEFAULT;
524 olsr_cnf->willingness = olsr_calculate_willingness();
526 olsr_printf(1, "Willingness set to %d - next update in %.1f secs\n", olsr_cnf->willingness, will_int);
531 *Set ipsize and minimum packetsize
533 if(olsr_cnf->ip_version == AF_INET6)
535 olsr_printf(1, "Using IP version 6\n");
536 ipsize = sizeof(struct in6_addr);
538 minsize = (int)sizeof(olsr_u8_t) * 7; /* Minimum packetsize IPv6 */
542 olsr_printf(1, "Using IP version 4\n");
543 ipsize = sizeof(olsr_u32_t);
545 minsize = (int)sizeof(olsr_u8_t) * 4; /* Minimum packetsize IPv4 */
549 /* Initializing networkinterfaces */
553 if(olsr_cnf->allow_no_interfaces)
555 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");
560 fprintf(stderr, "No interfaces detected!\nBailing out!\n");
561 olsr_exit(__func__, EXIT_FAILURE);
565 /* Print heartbeat to stdout */
567 if(olsr_cnf->debug_level > 0 && isatty(STDOUT_FILENO))
568 olsr_register_scheduler_event(&generate_stdout_pulse, NULL, STDOUT_PULSE_INT, 0, NULL);
570 gettimeofday(&now, NULL);
573 /* Initialize the IPC socket */
575 if(olsr_cnf->open_ipc)
579 /* Initialize link-layer notifications */
581 init_link_layer_notification();
584 /* Initialisation of different tables to be used.*/
591 /* Set up recieving tunnel if Inet gw */
592 if(use_tunnel && check_inet_gw())
593 set_up_gw_tunnel(&main_addr);
596 olsr_printf(1, "Main address: %s\n\n", olsr_ip_to_string(&main_addr));
600 if (olsr_cnf->debug_level == 0)
602 printf("%s detattching from the current process...\n", SOFTWARE_VERSION);
614 olsr_syslog(OLSR_LOG_INFO, "%s successfully started", SOFTWARE_VERSION);
620 /* ctrl-C and friends */
622 SetConsoleCtrlHandler(SignalHandler, OLSR_TRUE);
624 signal(SIGINT, olsr_shutdown);
625 signal(SIGTERM, olsr_shutdown);
628 /* Register socket poll event */
629 olsr_register_timeout_function(&poll_sockets);
631 /* Starting scheduler */
634 /* Like we're ever going to reach this ;-) */
645 *Function called at shutdown
650 SignalHandler(unsigned long signal)
653 olsr_shutdown(int signal)
656 struct interface *ifn;
658 olsr_printf(1, "Received signal %d - shutting down\n", signal);
660 olsr_delete_all_kernel_routes();
662 olsr_printf(1, "Closing sockets...\n");
664 /* front-end IPC socket */
665 if(olsr_cnf->open_ipc)
669 for (ifn = ifnet; ifn; ifn = ifn->int_next)
670 close(ifn->olsr_socket);
672 /* Closing plug-ins */
673 olsr_close_plugins();
675 /* Reset network settings */
676 restore_settings(olsr_cnf->ip_version);
681 #if defined __FreeBSD__ || defined __MacOSX__
686 olsr_syslog(OLSR_LOG_INFO, "%s stopped", SOFTWARE_VERSION);
688 olsr_printf(1, "\n <<<< %s - terminating >>>>\n http://www.olsr.org\n", SOFTWARE_VERSION);
698 *Sets the default values of variables at startup
703 memset(&main_addr, 0, sizeof(union olsr_ip_addr));
704 memset(&null_addr6, 0, sizeof (union olsr_ip_addr));
706 exit_value = EXIT_SUCCESS;
707 /* If the application exits by signal it is concidered success,
708 * if not, exit_value is set by the function calling olsr_exit.
713 dup_hold_time = DUP_HOLD_TIME;
715 will_int = 10 * HELLO_INTERVAL; /* Willingness update interval */
717 /* Gateway tunneling */
718 use_tunnel = OLSR_FALSE;
719 inet_tnl_added = OLSR_FALSE;
720 gw_tunnel = OLSR_FALSE;
723 del_gws = OLSR_FALSE;
725 /* Display packet content */
726 disp_pack_in = OLSR_FALSE;
727 disp_pack_out = OLSR_FALSE;
737 fprintf(stderr, "An error occured somwhere between your keyboard and your chair!\n");
738 fprintf(stderr, "usage: olsrd [-f <configfile>] [ -i interface1 interface2 ... ]\n");
739 fprintf(stderr, " [-d <debug_level>] [-ipv6] [-tnl] [-multi <IPv6 multicast address>]\n");
740 fprintf(stderr, " [-bcast <broadcastaddr>] [-ipc] [-dispin] [-dispout] [-delgw]\n");
741 fprintf(stderr, " [-hint <hello interval value (secs)>] [-tcint <tc interval value (secs)>]\n");
742 fprintf(stderr, " [-midint <mid interval value (secs)>] [-hnaint <hna interval value (secs)>]\n");
743 fprintf(stderr, " [-tos value (int)] [-T <Polling Rate (secs)>]\n");
749 * Sets the provided configuration on all unconfigured
753 set_default_ifcnfs(struct olsr_if *ifs, struct if_config_options *cnf)
761 ifs->cnf = olsr_malloc(sizeof(struct if_config_options), "Set default config");