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.13 2004/11/05 11:52:56 kattemat 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->open_ipc = DEF_OPEN_IPC;
256 cnf->use_hysteresis = DEF_USE_HYST;
257 cnf->hysteresis_param.scaling = HYST_SCALING;
258 cnf->hysteresis_param.thr_high = HYST_THRESHOLD_HIGH;
259 cnf->hysteresis_param.thr_low = HYST_THRESHOLD_LOW;
261 cnf->pollrate = DEF_POLLRATE;
263 cnf->tc_redundancy = TC_REDUNDANCY;
264 cnf->mpr_coverage = MPR_COVERAGE;
270 struct if_config_options *
271 get_default_if_config()
273 struct if_config_options *io = malloc(sizeof(struct if_config_options));
276 memset(io, 0, sizeof(struct if_config_options));
278 io->ipv6_addrtype = 1; /* XXX - FixMe */
280 if(inet_pton(AF_INET6, OLSR_IPV6_MCAST_SITE_LOCAL, &in6) < 0)
282 fprintf(stderr, "Failed converting IP address %s\n", OLSR_IPV6_MCAST_SITE_LOCAL);
285 memcpy(&io->ipv6_multi_site.v6, &in6, sizeof(struct in6_addr));
287 if(inet_pton(AF_INET6, OLSR_IPV6_MCAST_GLOBAL, &in6) < 0)
289 fprintf(stderr, "Failed converting IP address %s\n", OLSR_IPV6_MCAST_GLOBAL);
292 memcpy(&io->ipv6_multi_glbl.v6, &in6, sizeof(struct in6_addr));
295 io->hello_params.emission_interval = HELLO_INTERVAL;
296 io->hello_params.validity_time = NEIGHB_HOLD_TIME;
297 io->tc_params.emission_interval = TC_INTERVAL;
298 io->tc_params.validity_time = TOP_HOLD_TIME;
299 io->mid_params.emission_interval = MID_INTERVAL;
300 io->mid_params.validity_time = MID_HOLD_TIME;
301 io->hna_params.emission_interval = HNA_INTERVAL;
302 io->hna_params.validity_time = HNA_HOLD_TIME;
313 olsrd_write_cnf(struct olsrd_config *cnf, char *fname)
315 struct hna4_entry *h4 = cnf->hna4_entries;
316 struct hna6_entry *h6 = cnf->hna6_entries;
317 struct olsr_if *in = cnf->interfaces;
318 struct plugin_entry *pe = cnf->plugins;
319 struct plugin_param *pp;
320 char ipv6_buf[100]; /* buffer for IPv6 inet_htop */
325 fd = fopen(fname, "w");
329 fprintf(stderr, "Could not open file %s for writing\n%s\n", fname, strerror(errno));
333 printf("Writing config to file \"%s\".... ", fname);
335 fprintf(fd, "#\n# Configuration file for olsr.org olsrd\n# automatically generated by olsrd-cnf %s\n#\n\n\n", PARSER_VERSION);
338 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);
341 if(cnf->ip_version == AF_INET6)
342 fprintf(fd, "# IP version to use (4 or 6)\n\nIpVersion\t6\n\n");
344 fprintf(fd, "# IP version to use (4 or 6)\n\nIpVersion\t4\n\n");
348 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");
351 in4.s_addr = h4->net.v4;
352 fprintf(fd, " %s ", inet_ntoa(in4));
353 in4.s_addr = h4->netmask.v4;
354 fprintf(fd, "%s\n", inet_ntoa(in4));
357 fprintf(fd, "}\n\n");
361 fprintf(fd, "# HNA IPv6 routes\n# syntax: netaddr prefix\n# Example Internet gateway:\nHna6\n{\n");
364 fprintf(fd, " %s/%d\n", (char *)inet_ntop(AF_INET6, &h6->net.v6, ipv6_buf, sizeof(ipv6_buf)), h6->prefix_len);
368 fprintf(fd, "}\n\n");
371 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");
372 if(cnf->allow_no_interfaces)
373 fprintf(fd, "yes\n\n");
375 fprintf(fd, "no\n\n");
378 fprintf(fd, "# TOS(type of service) value for\n# the IP header of control traffic.\n# default is 16\n\n");
379 fprintf(fd, "TosValue\t%d\n\n", cnf->tos);
382 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");
383 if(cnf->willingness_auto)
384 fprintf(fd, "#Willingness\t4\n\n");
386 fprintf(fd, "Willingness%d\n\n", cnf->willingness);
389 fprintf(fd, "# Allow processes like the GUI front-end\n# to connect to the daemon. 'yes' or 'no'\n\n");
391 fprintf(fd, "IpcConnect\tyes\n\n");
393 fprintf(fd, "IpcConnect\tno\n\n");
398 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");
400 if(cnf->use_hysteresis)
402 fprintf(fd, "UseHysteresis\tyes\n\n");
403 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");
404 fprintf(fd, "HystScaling\t%0.2f\n", cnf->hysteresis_param.scaling);
405 fprintf(fd, "HystThrHigh\t%0.2f\n", cnf->hysteresis_param.thr_high);
406 fprintf(fd, "HystThrLow\t%0.2f\n", cnf->hysteresis_param.thr_low);
409 fprintf(fd, "UseHysteresis\tno\n\n");
414 fprintf(fd, "# Polling rate in seconds(float).\n# Auto uses default value 0.1 sec\n\n");
415 fprintf(fd, "Pollrate\t%0.2f\n", cnf->pollrate);
418 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");
419 fprintf(fd, "TcRedundancy\t%d\n\n", cnf->tc_redundancy);
422 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");
424 fprintf(fd, "MprCoverage\t%d\n\n", cnf->mpr_coverage);
429 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");
434 fprintf(fd, "LoadPlugin \"%s\"\n{\n", pe->name);
438 fprintf(fd, " PlParam \"%s\" \"%s\"\n", pp->key, pp->value);
451 fprintf(fd, "# Interfaces\n\n");
457 fprintf(fd, "Interface \"%s\"\n{\n", in->name);
460 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");
462 if(in->cnf->ipv4_broadcast.v4)
464 in4.s_addr = in->cnf->ipv4_broadcast.v4;
465 fprintf(fd, " Ip4Broadcast\t %s\n\n", inet_ntoa(in4));
469 fprintf(fd, " #Ip4Broadcast\t255.255.255.255\n\n");
473 fprintf(fd, " # IPv6 address scope to use.\n # Must be 'site-local' or 'global'\n\n");
474 if(in->cnf->ipv6_addrtype)
475 fprintf(fd, " Ip6AddrType \tsite-local\n\n");
477 fprintf(fd, " Ip6AddrType \tglobal\n\n");
479 fprintf(fd, " # IPv6 multicast address to use when\n # using site-local addresses.\n # If not defined, ff05::15 is used\n");
480 fprintf(fd, " Ip6MulticastSite\t%s\n\n", (char *)inet_ntop(AF_INET6, &in->cnf->ipv6_multi_site.v6, ipv6_buf, sizeof(ipv6_buf)));
481 fprintf(fd, " # IPv6 multicast address to use when\n # using global addresses\n # If not defined, ff0e::1 is used\n");
482 fprintf(fd, " Ip6MulticastGlobal\t%s\n\n", (char *)inet_ntop(AF_INET6, &in->cnf->ipv6_multi_glbl.v6, ipv6_buf, sizeof(ipv6_buf)));
486 fprintf(fd, " # Emission and validity intervals.\n # If not defined, RFC proposed values will\n # in most cases be used.\n\n");
489 if(in->cnf->hello_params.emission_interval != HELLO_INTERVAL)
490 fprintf(fd, " HelloInterval\t%0.2f\n", in->cnf->hello_params.emission_interval);
492 fprintf(fd, " #HelloInterval\t%0.2f\n", in->cnf->hello_params.emission_interval);
493 if(in->cnf->hello_params.validity_time != NEIGHB_HOLD_TIME)
494 fprintf(fd, " HelloValidityTime\t%0.2f\n", in->cnf->hello_params.validity_time);
496 fprintf(fd, " #HelloValidityTime\t%0.2f\n", in->cnf->hello_params.validity_time);
497 if(in->cnf->tc_params.emission_interval != TC_INTERVAL)
498 fprintf(fd, " TcInterval\t\t%0.2f\n", in->cnf->tc_params.emission_interval);
500 fprintf(fd, " #TcInterval\t\t%0.2f\n", in->cnf->tc_params.emission_interval);
501 if(in->cnf->tc_params.validity_time != TOP_HOLD_TIME)
502 fprintf(fd, " TcValidityTime\t%0.2f\n", in->cnf->tc_params.validity_time);
504 fprintf(fd, " #TcValidityTime\t%0.2f\n", in->cnf->tc_params.validity_time);
505 if(in->cnf->mid_params.emission_interval != MID_INTERVAL)
506 fprintf(fd, " MidInterval\t\t%0.2f\n", in->cnf->mid_params.emission_interval);
508 fprintf(fd, " #MidInterval\t%0.2f\n", in->cnf->mid_params.emission_interval);
509 if(in->cnf->mid_params.validity_time != MID_HOLD_TIME)
510 fprintf(fd, " MidValidityTime\t%0.2f\n", in->cnf->mid_params.validity_time);
512 fprintf(fd, " #MidValidityTime\t%0.2f\n", in->cnf->mid_params.validity_time);
513 if(in->cnf->hna_params.emission_interval != HNA_INTERVAL)
514 fprintf(fd, " HnaInterval\t\t%0.2f\n", in->cnf->hna_params.emission_interval);
516 fprintf(fd, " #HnaInterval\t%0.2f\n", in->cnf->hna_params.emission_interval);
517 if(in->cnf->hna_params.validity_time != HNA_HOLD_TIME)
518 fprintf(fd, " HnaValidityTime\t%0.2f\n", in->cnf->hna_params.validity_time);
520 fprintf(fd, " #HnaValidityTime\t%0.2f\n", in->cnf->hna_params.validity_time);
524 fprintf(fd, "}\n\n");
531 fprintf(fd, "\n# END AUTOGENERATED CONFIG\n");
544 olsrd_print_cnf(struct olsrd_config *cnf)
546 struct hna4_entry *h4 = cnf->hna4_entries;
547 struct hna6_entry *h6 = cnf->hna6_entries;
548 struct olsr_if *in = cnf->interfaces;
549 struct plugin_entry *pe = cnf->plugins;
550 char ipv6_buf[100]; /* buffer for IPv6 inet_htop */
553 printf(" *** olsrd configuration ***\n");
555 printf("Debug Level : %d\n", cnf->debug_level);
556 printf("IpVersion : %d\n", cnf->ip_version);
557 if(cnf->allow_no_interfaces)
558 printf("No interfaces : ALLOWED\n");
560 printf("No interfaces : NOT ALLOWED\n");
561 printf("TOS : 0x%02x\n", cnf->tos);
562 if(cnf->willingness_auto)
563 printf("Willingness : AUTO\n");
565 printf("Willingness : %d\n", cnf->willingness);
568 printf("IPC : ENABLED\n");
570 printf("IPC : DISABLED\n");
572 printf("Pollrate : %0.2f\n", cnf->pollrate);
574 printf("TC redundancy : %d\n", cnf->tc_redundancy);
576 printf("MPR coverage : %d\n", cnf->mpr_coverage);
581 printf("Interfaces:\n");
584 printf(" dev: \"%s\"\n", in->name);
586 if(in->cnf->ipv4_broadcast.v4)
588 in4.s_addr = in->cnf->ipv4_broadcast.v4;
589 printf("\tIPv4 broadcast : %s\n", inet_ntoa(in4));
593 printf("\tIPv4 broadcast : AUTO\n");
596 if(in->cnf->ipv6_addrtype)
597 printf("\tIPv6 addrtype : site-local\n");
599 printf("\tIPv6 addrtype : global\n");
601 //union olsr_ip_addr ipv6_multi_site;
602 //union olsr_ip_addr ipv6_multi_glbl;
603 printf("\tIPv6 multicast site : %s\n", (char *)inet_ntop(AF_INET6, &in->cnf->ipv6_multi_site.v6, ipv6_buf, sizeof(ipv6_buf)));
604 printf("\tIPv6 multicast global : %s\n", (char *)inet_ntop(AF_INET6, &in->cnf->ipv6_multi_glbl.v6, ipv6_buf, sizeof(ipv6_buf)));
606 printf("\tHELLO emission int : %0.2f\n", in->cnf->hello_params.emission_interval);
607 printf("\tHELLO validity time : %0.2f\n", in->cnf->hello_params.validity_time);
608 printf("\tTC emission int : %0.2f\n", in->cnf->tc_params.emission_interval);
609 printf("\tTC validity time : %0.2f\n", in->cnf->tc_params.validity_time);
610 printf("\tMID emission int : %0.2f\n", in->cnf->mid_params.emission_interval);
611 printf("\tMID validity time : %0.2f\n", in->cnf->mid_params.validity_time);
612 printf("\tHNA emission int : %0.2f\n", in->cnf->hna_params.emission_interval);
613 printf("\tHNA validity time : %0.2f\n", in->cnf->hna_params.validity_time);
628 printf("Plugins:\n");
632 printf("\tName: \"%s\"\n", pe->name);
638 if(cnf->use_hysteresis)
640 printf("Using hysteresis:\n");
641 printf("\tScaling : %0.2f\n", cnf->hysteresis_param.scaling);
642 printf("\tThr high: %0.2f\n", cnf->hysteresis_param.thr_high);
643 printf("\tThr low : %0.2f\n", cnf->hysteresis_param.thr_low);
646 printf("Not using hysteresis\n");
652 printf("HNA4 entries:\n");
655 in4.s_addr = h4->net.v4;
656 printf("\t%s/", inet_ntoa(in4));
657 in4.s_addr = h4->netmask.v4;
658 printf("%s\n", inet_ntoa(in4));
667 printf("HNA6 entries:\n");
670 printf("\t%s/%d\n", (char *)inet_ntop(AF_INET6, &h6->net.v6, ipv6_buf, sizeof(ipv6_buf)), h6->prefix_len);