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.28 2004/11/05 11:52:55 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 #include "win32/tunnel.h"
49 int __stdcall SignalHandler(unsigned long signal);
50 void ListInterfaces(void);
51 #elif defined __FreeBSD__
55 # error "Unsupported system"
59 * Local function prototypes
66 set_default_values(void);
69 set_default_ifcnfs(struct olsr_if *, struct if_config_options *);
73 * Local variable declarations
76 static pthread_t main_thread;
85 main(int argc, char *argv[])
87 /* For address convertions */
91 /* The thread for the scheduler */
94 struct if_config_options *default_ifcnf;
97 char conf_file_name[FILENAME_MAX];
104 setbuf(stdout, NULL);
105 setbuf(stderr, NULL);
107 /* Initialize socket list */
108 olsr_socket_entries = NULL;
111 /* Check if user is root */
112 if(getuid() || getgid())
114 fprintf(stderr, "You must be root(uid = 0) to run olsrd!\nExiting\n\n");
118 if (WSAStartup(0x0202, &WsaData))
120 fprintf(stderr, "Could not initialize WinSock.\n");
121 olsr_exit(__func__, EXIT_FAILURE);
126 olsr_openlog("olsrd");
128 /* Set default values */
129 set_default_values();
131 /* Initialize network functions */
134 /* Get initial timestep */
136 while (nowtm == NULL)
138 nowtm = gmtime((time_t *)&now.tv_sec);
141 /* The port to use for OLSR traffic */
142 olsr_udp_port = htons(OLSRPORT);
144 printf("\n *** %s ***\n Build date: %s\n http://www.olsr.org\n\n",
148 /* Using PID as random seed */
153 * Set configfile name and
154 * check if a configfile name was given as parameter
157 GetWindowsDirectory(conf_file_name, FILENAME_MAX - 11);
159 len = strlen(conf_file_name);
161 if (conf_file_name[len - 1] != '\\')
162 conf_file_name[len++] = '\\';
164 strcpy(conf_file_name + len, "olsrd.conf");
166 strncpy(conf_file_name, OLSRD_GLOBAL_CONF_FILE, FILENAME_MAX);
169 if ((argc > 1) && (strcmp(argv[1], "-f") == 0))
174 fprintf(stderr, "You must provide a filename when using the -f switch!\n");
178 if (stat(argv[1], &statbuf) < 0)
180 fprintf(stderr, "Could not find specified config file %s!\n%s\n\n", argv[1], strerror(errno));
184 strncpy(conf_file_name, argv[1], FILENAME_MAX);
190 * set up configuration prior to processing commandline options
192 if((olsr_cnf = olsrd_parse_cnf(conf_file_name)) == NULL)
194 printf("Using default config values(no configfile)\n");
195 olsr_cnf = olsrd_get_default_cnf();
197 if((default_ifcnf = get_default_if_config()) == NULL)
199 fprintf(stderr, "No default ifconfig found!\n");
204 * Process olsrd options.
208 while (argc > 0 && **argv == '-')
214 if (strcmp(*argv, "-int") == 0)
224 if(strcmp(*argv, "-f") == 0)
226 fprintf(stderr, "Configfilename must ALWAYS be first argument!\n\n");
227 olsr_exit(__func__, EXIT_FAILURE);
233 if(strcmp(*argv, "-ipv6") == 0)
235 olsr_cnf->ip_version = AF_INET6;
243 if(strcmp(*argv, "-bcast") == 0)
248 fprintf(stderr, "You must provide a broadcastaddr when using the -bcast switch!\n");
249 olsr_exit(__func__, EXIT_FAILURE);
251 if (inet_aton(*argv, &in) == 0)
253 printf("Invalid broadcast address! %s\nSkipping it!\n", *argv);
256 memcpy(&default_ifcnf->ipv4_broadcast.v4, &in.s_addr, sizeof(olsr_u32_t));
261 * Enable additional debugging information to be logged.
263 if (strcmp(*argv, "-d") == 0)
266 sscanf(*argv,"%d", &olsr_cnf->debug_level);
273 * Interfaces to be used by olsrd.
275 if (strcmp(*argv, "-i") == 0)
278 if(!argc || (*argv[0] == '-'))
280 fprintf(stderr, "You must provide an interface label!\n");
281 olsr_exit(__func__, EXIT_FAILURE);
287 while((argc) && (**argv != '-'))
296 * Set the hello interval to be used by olsrd.
299 if (strcmp(*argv, "-hint") == 0)
302 sscanf(*argv,"%f", &default_ifcnf->hello_params.emission_interval);
303 default_ifcnf->hello_params.validity_time = default_ifcnf->hello_params.emission_interval * 3;
309 * Set the HNA interval to be used by olsrd.
312 if (strcmp(*argv, "-hnaint") == 0)
315 sscanf(*argv,"%f", &default_ifcnf->hna_params.emission_interval);
316 default_ifcnf->hna_params.validity_time = default_ifcnf->hna_params.emission_interval * 3;
322 * Set the MID interval to be used by olsrd.
325 if (strcmp(*argv, "-midint") == 0)
328 sscanf(*argv,"%f", &default_ifcnf->mid_params.emission_interval);
329 default_ifcnf->mid_params.validity_time = default_ifcnf->mid_params.emission_interval * 3;
335 * Set the tc interval to be used by olsrd.
338 if (strcmp(*argv, "-tcint") == 0)
341 sscanf(*argv,"%f", &default_ifcnf->tc_params.emission_interval);
342 default_ifcnf->tc_params.validity_time = default_ifcnf->tc_params.emission_interval * 3;
348 * Set the tos bits to be used by olsrd.
351 if (strcmp(*argv, "-tos") == 0)
354 sscanf(*argv,"%d",(int *)&olsr_cnf->tos);
361 * Set the polling interval to be used by olsrd.
363 if (strcmp(*argv, "-T") == 0)
366 sscanf(*argv,"%f",&olsr_cnf->pollrate);
373 * Should we display the contents of packages beeing sent?
375 if (strcmp(*argv, "-dispin") == 0)
378 disp_pack_in = OLSR_TRUE;
383 * Should we display the contents of incoming packages?
385 if (strcmp(*argv, "-dispout") == 0)
388 disp_pack_out = OLSR_TRUE;
394 * Should we set up and send on a IPC socket for the front-end?
396 if (strcmp(*argv, "-ipc") == 0)
399 olsr_cnf->open_ipc = OLSR_TRUE;
405 * Display link-layer info(experimental)
407 if (strcmp(*argv, "-llinfo") == 0)
415 * Use Internet gateway tunneling?
417 if (strcmp(*argv, "-tnl") == 0)
420 use_tunnel = OLSR_TRUE;
426 * IPv6 multicast addr
428 if (strcmp(*argv, "-multi") == 0)
431 if(inet_pton(AF_INET6, *argv, &in6) < 0)
433 fprintf(stderr, "Failed converting IP address %s\n", *argv);
437 memcpy(&default_ifcnf->ipv6_multi_glbl, &in6, sizeof(struct in6_addr));
446 * Should we display the contents of packages beeing sent?
448 if (strcmp(*argv, "-delgw") == 0)
457 olsr_exit(__func__, EXIT_FAILURE);
462 *Interfaces need to be specified
464 if(olsr_cnf->interfaces == NULL)
466 fprintf(stderr, "OLSRD: no interfaces specified!\nuse the -i switch to specify interface(s)\nor set interface(s) in the configuration file!\n");
468 olsr_exit(__func__, EXIT_FAILURE);
472 * Set configuration for command-line specified interfaces
474 set_default_ifcnfs(olsr_cnf->interfaces, default_ifcnf);
477 * Print configuration
479 olsrd_print_cnf(olsr_cnf);
482 *socket for icotl calls
484 if ((ioctl_s = socket(olsr_cnf->ip_version, SOCK_DGRAM, 0)) < 0)
487 olsr_syslog(OLSR_LOG_ERR, "ioctl socket: %m");
488 olsr_exit(__func__, 0);
492 if ((rts = socket(PF_ROUTE, SOCK_RAW, 0)) < 0)
494 olsr_syslog(OLSR_LOG_ERR, "routing socket: %m");
495 olsr_exit(__func__, 0);
500 *enable ip forwarding on host
502 enable_ip_forwarding(olsr_cnf->ip_version);
504 /* Initialize scheduler MUST HAPPEN BEFORE REGISTERING ANY FUNCTIONS! */
505 init_scheduler(olsr_cnf->pollrate);
507 /* Initialize parser */
510 /* Initialize message sequencnumber */
513 /* Initialize dynamic willingness calculation */
514 olsr_init_willingness();
516 /* Sanity check for hysteresis values */
517 if((olsr_cnf->use_hysteresis) &&
518 (olsr_cnf->hysteresis_param.thr_high <= olsr_cnf->hysteresis_param.thr_low))
520 printf("Hysteresis threshold high lower than threshold low!!\nEdit the configuration file to fix this!\n\n");
521 olsr_exit(__func__, EXIT_FAILURE);
525 *Set up willingness/APM
527 if(olsr_cnf->willingness_auto)
531 olsr_printf(1, "Could not read APM info - setting default willingness(%d)\n", WILL_DEFAULT);
533 olsr_syslog(OLSR_LOG_ERR, "Could not read APM info - setting default willingness(%d)\n", WILL_DEFAULT);
535 olsr_cnf->willingness_auto = 0;
536 olsr_cnf->willingness = WILL_DEFAULT;
540 olsr_cnf->willingness = olsr_calculate_willingness();
542 olsr_printf(1, "Willingness set to %d - next update in %.1f secs\n", olsr_cnf->willingness, will_int);
547 *Set ipsize and minimum packetsize
549 if(olsr_cnf->ip_version == AF_INET6)
551 olsr_printf(1, "Using IP version 6\n");
552 ipsize = sizeof(struct in6_addr);
554 minsize = (int)sizeof(olsr_u8_t) * 7; /* Minimum packetsize IPv6 */
558 olsr_printf(1, "Using IP version 4\n");
559 ipsize = sizeof(olsr_u32_t);
561 minsize = (int)sizeof(olsr_u8_t) * 4; /* Minimum packetsize IPv4 */
565 /* Initializing networkinterfaces */
569 if(olsr_cnf->allow_no_interfaces)
571 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");
576 fprintf(stderr, "No interfaces detected!\nBailing out!\n");
577 olsr_exit(__func__, EXIT_FAILURE);
581 /* Print tables to stdout */
582 if(olsr_cnf->debug_level > 0)
583 olsr_register_scheduler_event(&generate_tabledisplay, NULL, HELLO_INTERVAL, 0, NULL);
586 gettimeofday(&now, NULL);
589 /* Initialize the IPC socket */
591 if(olsr_cnf->open_ipc)
594 #if !defined WIN32 && !defined __FreeBSD__
595 /* Initialize link-layer notifications */
597 init_link_layer_notification();
600 /* Initialisation of different tables to be used.*/
607 /* Set up recieving tunnel if Inet gw */
608 if(use_tunnel && check_inet_gw())
609 set_up_gw_tunnel(&main_addr);
612 olsr_printf(1, "Main address: %s\n\n", olsr_ip_to_string(&main_addr));
614 olsr_printf(1, "NEIGHBORS: l=linkstate, m=MPR, w=willingness\n\n");
619 if (olsr_cnf->debug_level == 0)
621 printf("%s detattching from the current process...\n", SOFTWARE_VERSION);
629 /* Starting scheduler */
630 start_scheduler(&thread);
635 olsr_syslog(OLSR_LOG_INFO, "%s successfully started", SOFTWARE_VERSION);
641 /* ctrl-C and friends */
643 SetConsoleCtrlHandler(SignalHandler, OLSR_TRUE);
645 signal(SIGINT, olsr_shutdown);
646 signal(SIGTERM, olsr_shutdown);
649 /* Go into listenloop */
652 /* Like we're ever going to reach this ;-) */
663 *Function called at shutdown
668 SignalHandler(unsigned long signal)
671 olsr_shutdown(int signal)
674 struct interface *ifn;
676 if(main_thread != pthread_self())
682 olsr_printf(1, "Received signal %d - shutting down\n", signal);
684 olsr_delete_all_kernel_routes();
686 olsr_printf(1, "Closing sockets...\n");
688 /* front-end IPC socket */
689 if(olsr_cnf->open_ipc)
693 for (ifn = ifnet; ifn; ifn = ifn->int_next)
694 close(ifn->olsr_socket);
696 /* Closing plug-ins */
697 olsr_close_plugins();
699 /* Reset network settings */
700 restore_settings(olsr_cnf->ip_version);
710 olsr_syslog(OLSR_LOG_INFO, "%s stopped", SOFTWARE_VERSION);
712 olsr_printf(1, "\n <<<< %s - terminating >>>>\n http://www.olsr.org\n", SOFTWARE_VERSION);
722 *Sets the default values of variables at startup
727 memset(&main_addr, 0, sizeof(union olsr_ip_addr));
728 memset(&null_addr6, 0, sizeof (union olsr_ip_addr));
730 exit_value = EXIT_SUCCESS;
731 /* If the application exits by signal it is concidered success,
732 * if not, exit_value is set by the function calling olsr_exit.
737 dup_hold_time = DUP_HOLD_TIME;
739 will_int = 10 * HELLO_INTERVAL; /* Willingness update interval */
743 /* Get main thread ID */
744 main_thread = pthread_self();
747 /* Gateway tunneling */
748 use_tunnel = OLSR_FALSE;
749 inet_tnl_added = OLSR_FALSE;
750 gw_tunnel = OLSR_FALSE;
753 del_gws = OLSR_FALSE;
755 /* Display packet content */
756 disp_pack_in = OLSR_FALSE;
757 disp_pack_out = OLSR_FALSE;
767 fprintf(stderr, "An error occured somwhere between your keyboard and your chair!\n");
768 fprintf(stderr, "usage: olsrd [-f <configfile>] [ -i interface1 interface2 ... ]\n");
769 fprintf(stderr, " [-d <debug_level>] [-ipv6] [-tnl] [-multi <IPv6 multicast address>]\n");
770 fprintf(stderr, " [-bcast <broadcastaddr>] [-ipc] [-dispin] [-dispout] [-delgw]\n");
771 fprintf(stderr, " [-hint <hello interval value (secs)>] [-tcint <tc interval value (secs)>]\n");
772 fprintf(stderr, " [-midint <mid interval value (secs)>] [-hnaint <hna interval value (secs)>]\n");
773 fprintf(stderr, " [-tos value (int)] [-T <Polling Rate (secs)>]\n");
779 * Sets the provided configuration on all unconfigured
783 set_default_ifcnfs(struct olsr_if *ifs, struct if_config_options *cnf)
791 ifs->cnf = olsr_malloc(sizeof(struct if_config_options), "Set default config");