2 * OLSR ad-hoc routing table management protocol config parser
3 * Copyright (C) 2004 Andreas Tønnesen (andreto@olsr.org)
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: olsrd_conf.c,v 1.19 2004/11/20 17:10:03 tlopatic Exp $
31 #include <sys/types.h>
32 #include <sys/socket.h>
33 #include <netinet/in.h>
34 #include <arpa/inet.h>
36 #include "olsrd_conf.h"
40 extern int yyparse(void);
47 void __attribute__ ((constructor))
50 void __attribute__ ((destructor))
60 /* Print plugin info to stdout */
61 printf("olsrd config file parser %s loaded\n", PARSER_VERSION);
72 printf("See you around!\n");
80 /* Build as standalone binary */
82 main(int argc, char *argv[])
84 struct olsrd_config *cnf;
88 fprintf(stderr, "Usage: olsrd_cfgparser [filename] -print\n\n");
92 if((cnf = olsrd_parse_cnf(argv[1])) != NULL)
94 if((argc > 2) && (!strcmp(argv[2], "-print")))
97 olsrd_write_cnf(cnf, "./out.conf");
100 printf("Use -print to view parsed values\n");
101 printf("Configfile parsed OK\n");
105 printf("Failed parsing \"%s\"\n", argv[1]);
113 /* Build as part of olsrd */
120 struct olsrd_config *
121 olsrd_parse_cnf(char *filename)
125 cnf = malloc(sizeof(struct olsrd_config));
128 fprintf(stderr, "Out of memory %s\n", __func__);
132 set_default_cnf(cnf);
134 printf("Parsing file: \"%s\"\n", filename);
136 yyin = fopen(filename, "r");
140 fprintf(stderr, "Cannot open configuration file '%s': %s.\n",
141 filename, strerror(errno));
158 in = cnf->interfaces;
162 /* set various stuff */
163 in->index = cnf->ifcnt++;
164 in->configured = OLSR_FALSE;
175 olsrd_free_cnf(struct olsrd_config *cnf)
177 struct hna4_entry *h4d, *h4 = cnf->hna4_entries;
178 struct hna6_entry *h6d, *h6 = cnf->hna6_entries;
179 struct olsr_if *ind, *in = cnf->interfaces;
180 struct plugin_entry *ped, *pe = cnf->plugins;
181 struct if_config_options *iod, *io;
226 struct olsrd_config *
227 olsrd_get_default_cnf()
229 cnf = malloc(sizeof(struct olsrd_config));
232 fprintf(stderr, "Out of memory %s\n", __func__);
236 set_default_cnf(cnf);
245 set_default_cnf(struct olsrd_config *cnf)
247 memset(cnf, 0, sizeof(struct olsrd_config));
249 cnf->debug_level = DEF_DEBUGLVL;
250 cnf->ip_version = AF_INET;
251 cnf->allow_no_interfaces = DEF_ALLOW_NO_INTS;
253 cnf->willingness_auto = DEF_WILL_AUTO;
254 cnf->ipc_connections = DEF_IPC_CONNECTIONS;
255 cnf->open_ipc = cnf->ipc_connections ? OLSR_TRUE : OLSR_FALSE;
257 cnf->use_hysteresis = DEF_USE_HYST;
258 cnf->hysteresis_param.scaling = HYST_SCALING;
259 cnf->hysteresis_param.thr_high = HYST_THRESHOLD_HIGH;
260 cnf->hysteresis_param.thr_low = HYST_THRESHOLD_LOW;
262 cnf->pollrate = DEF_POLLRATE;
264 cnf->tc_redundancy = TC_REDUNDANCY;
265 cnf->mpr_coverage = MPR_COVERAGE;
266 cnf->lq_level = DEF_LQ_LEVEL;
267 cnf->lq_wsize = DEF_LQ_WSIZE;
268 cnf->clear_screen = DEF_CLEAR_SCREEN;
274 struct if_config_options *
275 get_default_if_config()
277 struct if_config_options *io = malloc(sizeof(struct if_config_options));
280 memset(io, 0, sizeof(struct if_config_options));
282 io->ipv6_addrtype = 1; /* XXX - FixMe */
284 if(inet_pton(AF_INET6, OLSR_IPV6_MCAST_SITE_LOCAL, &in6) < 0)
286 fprintf(stderr, "Failed converting IP address %s\n", OLSR_IPV6_MCAST_SITE_LOCAL);
289 memcpy(&io->ipv6_multi_site.v6, &in6, sizeof(struct in6_addr));
291 if(inet_pton(AF_INET6, OLSR_IPV6_MCAST_GLOBAL, &in6) < 0)
293 fprintf(stderr, "Failed converting IP address %s\n", OLSR_IPV6_MCAST_GLOBAL);
296 memcpy(&io->ipv6_multi_glbl.v6, &in6, sizeof(struct in6_addr));
299 io->hello_params.emission_interval = HELLO_INTERVAL;
300 io->hello_params.validity_time = NEIGHB_HOLD_TIME;
301 io->tc_params.emission_interval = TC_INTERVAL;
302 io->tc_params.validity_time = TOP_HOLD_TIME;
303 io->mid_params.emission_interval = MID_INTERVAL;
304 io->mid_params.validity_time = MID_HOLD_TIME;
305 io->hna_params.emission_interval = HNA_INTERVAL;
306 io->hna_params.validity_time = HNA_HOLD_TIME;
317 olsrd_write_cnf(struct olsrd_config *cnf, char *fname)
319 struct hna4_entry *h4 = cnf->hna4_entries;
320 struct hna6_entry *h6 = cnf->hna6_entries;
321 struct olsr_if *in = cnf->interfaces;
322 struct plugin_entry *pe = cnf->plugins;
323 struct plugin_param *pp;
324 struct ipc_host *ih = cnf->ipc_hosts;
325 struct ipc_net *ie = cnf->ipc_nets;
327 char ipv6_buf[100]; /* buffer for IPv6 inet_htop */
332 fd = fopen(fname, "w");
336 fprintf(stderr, "Could not open file %s for writing\n%s\n", fname, strerror(errno));
340 printf("Writing config to file \"%s\".... ", fname);
342 fprintf(fd, "#\n# Configuration file for olsr.org olsrd\n# automatically generated by olsrd-cnf %s\n#\n\n\n", PARSER_VERSION);
345 fprintf(fd, "# Debug level(0-9)\n# If set to 0 the daemon runs in the background\n\nDebugLevel\t%d\n\n", cnf->debug_level);
348 if(cnf->ip_version == AF_INET6)
349 fprintf(fd, "# IP version to use (4 or 6)\n\nIpVersion\t6\n\n");
351 fprintf(fd, "# IP version to use (4 or 6)\n\nIpVersion\t4\n\n");
355 fprintf(fd, "# HNA IPv4 routes\n# syntax: netaddr netmask\n# Example Internet gateway:\n# 0.0.0.0 0.0.0.0\n\nHna4\n{\n");
358 in4.s_addr = h4->net.v4;
359 fprintf(fd, " %s ", inet_ntoa(in4));
360 in4.s_addr = h4->netmask.v4;
361 fprintf(fd, "%s\n", inet_ntoa(in4));
364 fprintf(fd, "}\n\n");
368 fprintf(fd, "# HNA IPv6 routes\n# syntax: netaddr prefix\n# Example Internet gateway:\nHna6\n{\n");
371 fprintf(fd, " %s/%d\n", (char *)inet_ntop(AF_INET6, &h6->net.v6, ipv6_buf, sizeof(ipv6_buf)), h6->prefix_len);
375 fprintf(fd, "}\n\n");
378 fprintf(fd, "# Should olsrd keep on running even if there are\n# no interfaces available? This is a good idea\n# for a PCMCIA/USB hotswap environment.\n# \"yes\" OR \"no\"\n\nAllowNoInt\t");
379 if(cnf->allow_no_interfaces)
380 fprintf(fd, "yes\n\n");
382 fprintf(fd, "no\n\n");
385 fprintf(fd, "# TOS(type of service) value for\n# the IP header of control traffic.\n# default is 16\n\n");
386 fprintf(fd, "TosValue\t%d\n\n", cnf->tos);
389 fprintf(fd, "# The fixed willingness to use(0-7)\n# If not set willingness will be calculated\n# dynammically based on battery/power status\n\n");
390 if(cnf->willingness_auto)
391 fprintf(fd, "#Willingness\t4\n\n");
393 fprintf(fd, "Willingness%d\n\n", cnf->willingness);
396 fprintf(fd, "# Allow processes like the GUI front-end\n# to connect to the daemon.\n\n");
397 fprintf(fd, "IpcConnect\n{\n");
398 fprintf(fd, " MaxConnections %d\n\n", cnf->ipc_connections);
402 in4.s_addr = ih->host.v4;
403 fprintf(fd, " Host %s\n", inet_ntoa(in4));
409 in4.s_addr = ie->net.v4;
410 fprintf(fd, " Net %s ", inet_ntoa(in4));
411 in4.s_addr = ie->mask.v4;
412 fprintf(fd, "%s\n", inet_ntoa(in4));
416 fprintf(fd, "}\n\n");
421 fprintf(fd, "# Wether to use hysteresis or not\n# Hysteresis adds more robustness to the\n# link sensing but delays neighbor registration.\n# Used by default. 'yes' or 'no'\n\n");
423 if(cnf->use_hysteresis)
425 fprintf(fd, "UseHysteresis\tyes\n\n");
426 fprintf(fd, "# Hysteresis parameters\n# Do not alter these unless you know \n# what you are doing!\n# Set to auto by default. Allowed\n# values are floating point values\n# in the interval 0,1\n# THR_LOW must always be lower than\n# THR_HIGH!!\n\n");
427 fprintf(fd, "HystScaling\t%0.2f\n", cnf->hysteresis_param.scaling);
428 fprintf(fd, "HystThrHigh\t%0.2f\n", cnf->hysteresis_param.thr_high);
429 fprintf(fd, "HystThrLow\t%0.2f\n", cnf->hysteresis_param.thr_low);
432 fprintf(fd, "UseHysteresis\tno\n\n");
437 fprintf(fd, "# Polling rate in seconds(float).\n# Auto uses default value 0.05 sec\n\n");
438 fprintf(fd, "Pollrate\t%0.2f\n", cnf->pollrate);
441 fprintf(fd, "# TC redundancy\n# Specifies how much neighbor info should\n# be sent in TC messages\n# Possible values are:\n# 0 - only send MPR selectors\n# 1 - send MPR selectors and MPRs\n# 2 - send all neighbors\n#\n# defaults to 0\n\n");
442 fprintf(fd, "TcRedundancy\t%d\n\n", cnf->tc_redundancy);
445 fprintf(fd, "# MPR coverage\n# Specifies how many MPRs a node should\n# try select to reach every 2 hop neighbor\n# Can be set to any integer >0\n# defaults to 1\n\n");
447 fprintf(fd, "MprCoverage\t%d\n\n", cnf->mpr_coverage);
449 fprintf(fd, "# Link quality level\n# 0 = do not use link quality\n# 1 = use link quality for MPR selection\n# 2 = use link quality for MPR selection and routing\n\n");
450 fprintf(fd, "LinkQualityLevel\t%d\n\n", cnf->lq_level);
452 fprintf(fd, "# Link quality window size\n\n");
453 fprintf(fd, "LinkQualityWinSize\t%d\n\n", cnf->lq_wsize);
455 fprintf(fd, "# Clear screen when printing debug output?\n\n");
456 fprintf(fd, "ClearScreen\t%s\n\n", cnf->clear_screen ? "yes" : "no");
459 fprintf(fd, "# Olsrd plugins to load\n# This must be the absolute path to the file\n# or the loader will use the following scheme:\n# - Try the paths in the LD_LIBRARY_PATH \n# environment variable.\n# - The list of libraries cached in /etc/ld.so.cache\n# - /lib, followed by /usr/lib\n\n");
464 fprintf(fd, "LoadPlugin \"%s\"\n{\n", pe->name);
468 fprintf(fd, " PlParam \"%s\" \"%s\"\n", pp->key, pp->value);
481 fprintf(fd, "# Interfaces\n\n");
487 fprintf(fd, "Interface \"%s\"\n{\n", in->name);
490 fprintf(fd, " # IPv4 broadcast address to use. The\n # one usefull example would be 255.255.255.255\n # If not defined the broadcastaddress\n # every card is configured with is used\n\n");
492 if(in->cnf->ipv4_broadcast.v4)
494 in4.s_addr = in->cnf->ipv4_broadcast.v4;
495 fprintf(fd, " Ip4Broadcast\t %s\n\n", inet_ntoa(in4));
499 fprintf(fd, " #Ip4Broadcast\t255.255.255.255\n\n");
503 fprintf(fd, " # IPv6 address scope to use.\n # Must be 'site-local' or 'global'\n\n");
504 if(in->cnf->ipv6_addrtype)
505 fprintf(fd, " Ip6AddrType \tsite-local\n\n");
507 fprintf(fd, " Ip6AddrType \tglobal\n\n");
509 fprintf(fd, " # IPv6 multicast address to use when\n # using site-local addresses.\n # If not defined, ff05::15 is used\n");
510 fprintf(fd, " Ip6MulticastSite\t%s\n\n", (char *)inet_ntop(AF_INET6, &in->cnf->ipv6_multi_site.v6, ipv6_buf, sizeof(ipv6_buf)));
511 fprintf(fd, " # IPv6 multicast address to use when\n # using global addresses\n # If not defined, ff0e::1 is used\n");
512 fprintf(fd, " Ip6MulticastGlobal\t%s\n\n", (char *)inet_ntop(AF_INET6, &in->cnf->ipv6_multi_glbl.v6, ipv6_buf, sizeof(ipv6_buf)));
516 fprintf(fd, " # Emission and validity intervals.\n # If not defined, RFC proposed values will\n # in most cases be used.\n\n");
519 if(in->cnf->hello_params.emission_interval != HELLO_INTERVAL)
520 fprintf(fd, " HelloInterval\t%0.2f\n", in->cnf->hello_params.emission_interval);
522 fprintf(fd, " #HelloInterval\t%0.2f\n", in->cnf->hello_params.emission_interval);
523 if(in->cnf->hello_params.validity_time != NEIGHB_HOLD_TIME)
524 fprintf(fd, " HelloValidityTime\t%0.2f\n", in->cnf->hello_params.validity_time);
526 fprintf(fd, " #HelloValidityTime\t%0.2f\n", in->cnf->hello_params.validity_time);
527 if(in->cnf->tc_params.emission_interval != TC_INTERVAL)
528 fprintf(fd, " TcInterval\t\t%0.2f\n", in->cnf->tc_params.emission_interval);
530 fprintf(fd, " #TcInterval\t\t%0.2f\n", in->cnf->tc_params.emission_interval);
531 if(in->cnf->tc_params.validity_time != TOP_HOLD_TIME)
532 fprintf(fd, " TcValidityTime\t%0.2f\n", in->cnf->tc_params.validity_time);
534 fprintf(fd, " #TcValidityTime\t%0.2f\n", in->cnf->tc_params.validity_time);
535 if(in->cnf->mid_params.emission_interval != MID_INTERVAL)
536 fprintf(fd, " MidInterval\t\t%0.2f\n", in->cnf->mid_params.emission_interval);
538 fprintf(fd, " #MidInterval\t%0.2f\n", in->cnf->mid_params.emission_interval);
539 if(in->cnf->mid_params.validity_time != MID_HOLD_TIME)
540 fprintf(fd, " MidValidityTime\t%0.2f\n", in->cnf->mid_params.validity_time);
542 fprintf(fd, " #MidValidityTime\t%0.2f\n", in->cnf->mid_params.validity_time);
543 if(in->cnf->hna_params.emission_interval != HNA_INTERVAL)
544 fprintf(fd, " HnaInterval\t\t%0.2f\n", in->cnf->hna_params.emission_interval);
546 fprintf(fd, " #HnaInterval\t%0.2f\n", in->cnf->hna_params.emission_interval);
547 if(in->cnf->hna_params.validity_time != HNA_HOLD_TIME)
548 fprintf(fd, " HnaValidityTime\t%0.2f\n", in->cnf->hna_params.validity_time);
550 fprintf(fd, " #HnaValidityTime\t%0.2f\n", in->cnf->hna_params.validity_time);
554 fprintf(fd, "}\n\n");
561 fprintf(fd, "\n# END AUTOGENERATED CONFIG\n");
574 olsrd_print_cnf(struct olsrd_config *cnf)
576 struct hna4_entry *h4 = cnf->hna4_entries;
577 struct hna6_entry *h6 = cnf->hna6_entries;
578 struct olsr_if *in = cnf->interfaces;
579 struct plugin_entry *pe = cnf->plugins;
580 struct ipc_host *ih = cnf->ipc_hosts;
581 struct ipc_net *ie = cnf->ipc_nets;
582 char ipv6_buf[100]; /* buffer for IPv6 inet_htop */
585 printf(" *** olsrd configuration ***\n");
587 printf("Debug Level : %d\n", cnf->debug_level);
588 if(cnf->ip_version == AF_INET6)
589 printf("IpVersion : 6\n");
591 printf("IpVersion : 4\n");
592 if(cnf->allow_no_interfaces)
593 printf("No interfaces : ALLOWED\n");
595 printf("No interfaces : NOT ALLOWED\n");
596 printf("TOS : 0x%02x\n", cnf->tos);
597 if(cnf->willingness_auto)
598 printf("Willingness : AUTO\n");
600 printf("Willingness : %d\n", cnf->willingness);
602 printf("IPC connections : %d\n", cnf->ipc_connections);
606 in4.s_addr = ih->host.v4;
607 printf("\tHost %s\n", inet_ntoa(in4));
613 in4.s_addr = ie->net.v4;
614 printf("\tNet %s/", inet_ntoa(in4));
615 in4.s_addr = ie->mask.v4;
616 printf("%s\n", inet_ntoa(in4));
621 printf("Pollrate : %0.2f\n", cnf->pollrate);
623 printf("TC redundancy : %d\n", cnf->tc_redundancy);
625 printf("MPR coverage : %d\n", cnf->mpr_coverage);
627 printf("LQ level : %d\n", cnf->lq_level);
629 printf("LQ window size : %d\n", cnf->lq_wsize);
631 printf("Clear screen : %s\n", cnf->clear_screen ? "yes" : "no");
636 printf("Interfaces:\n");
639 printf(" dev: \"%s\"\n", in->name);
641 if(in->cnf->ipv4_broadcast.v4)
643 in4.s_addr = in->cnf->ipv4_broadcast.v4;
644 printf("\tIPv4 broadcast : %s\n", inet_ntoa(in4));
648 printf("\tIPv4 broadcast : AUTO\n");
651 if(in->cnf->ipv6_addrtype)
652 printf("\tIPv6 addrtype : site-local\n");
654 printf("\tIPv6 addrtype : global\n");
656 //union olsr_ip_addr ipv6_multi_site;
657 //union olsr_ip_addr ipv6_multi_glbl;
658 printf("\tIPv6 multicast site : %s\n", (char *)inet_ntop(AF_INET6, &in->cnf->ipv6_multi_site.v6, ipv6_buf, sizeof(ipv6_buf)));
659 printf("\tIPv6 multicast global : %s\n", (char *)inet_ntop(AF_INET6, &in->cnf->ipv6_multi_glbl.v6, ipv6_buf, sizeof(ipv6_buf)));
661 printf("\tHELLO emission int : %0.2f\n", in->cnf->hello_params.emission_interval);
662 printf("\tHELLO validity time : %0.2f\n", in->cnf->hello_params.validity_time);
663 printf("\tTC emission int : %0.2f\n", in->cnf->tc_params.emission_interval);
664 printf("\tTC validity time : %0.2f\n", in->cnf->tc_params.validity_time);
665 printf("\tMID emission int : %0.2f\n", in->cnf->mid_params.emission_interval);
666 printf("\tMID validity time : %0.2f\n", in->cnf->mid_params.validity_time);
667 printf("\tHNA emission int : %0.2f\n", in->cnf->hna_params.emission_interval);
668 printf("\tHNA validity time : %0.2f\n", in->cnf->hna_params.validity_time);
683 printf("Plugins:\n");
687 printf("\tName: \"%s\"\n", pe->name);
693 if(cnf->use_hysteresis)
695 printf("Using hysteresis:\n");
696 printf("\tScaling : %0.2f\n", cnf->hysteresis_param.scaling);
697 printf("\tThr high: %0.2f\n", cnf->hysteresis_param.thr_high);
698 printf("\tThr low : %0.2f\n", cnf->hysteresis_param.thr_low);
701 printf("Not using hysteresis\n");
707 printf("HNA4 entries:\n");
710 in4.s_addr = h4->net.v4;
711 printf("\t%s/", inet_ntoa(in4));
712 in4.s_addr = h4->netmask.v4;
713 printf("%s\n", inet_ntoa(in4));
722 printf("HNA6 entries:\n");
725 printf("\t%s/%d\n", (char *)inet_ntop(AF_INET6, &h6->net.v6, ipv6_buf, sizeof(ipv6_buf)), h6->prefix_len);