5 #include <sys/socket.h>
6 #include <netinet/in.h>
9 #include "olsrd_conf.h"
13 extern int yyparse(void);
20 void __attribute__ ((constructor))
23 void __attribute__ ((destructor))
33 /* Print plugin info to stdout */
34 printf("olsrd config file parser %s loaded\n", SOFTWARE_VERSION);
45 printf("See you around!\n");
53 /* Build as standalone binary */
55 main(int argc, char *argv[])
57 struct olsrd_config *cnf;
61 fprintf(stderr, "Usage: olsrd_cfgparser [filename] -print\n\n");
65 if((cnf = olsrd_parse_cnf(argv[1])) != NULL)
67 if((argc > 2) && (!strcmp(argv[2], "-print")))
70 olsrd_write_cnf(cnf, "./out.conf");
73 printf("Use -print to view parsed values\n");
74 printf("Configfile parsed OK\n");
78 printf("Failed parsing \"%s\"\n", argv[1]);
86 /* Build as part of olsrd */
94 olsrd_parse_cnf(char *filename)
98 cnf = malloc(sizeof(struct olsrd_config));
101 fprintf(stderr, "Out of memory %s\n", __func__);
105 set_default_cnf(cnf);
107 printf("Parsing file: \"%s\"\n", filename);
109 yyin = fopen(filename, "r");
113 fprintf(stderr, "Cannot open configuration file '%s': %s.\n",
114 filename, strerror(errno));
130 /* Verify interface rulesets */
131 in = cnf->interfaces;
135 in->if_options = find_if_rule_by_name(cnf->if_options, in->config);
137 if(in->if_options == NULL)
139 fprintf(stderr, "ERROR: Could not find a matching ruleset \"%s\" for %s\n", in->config, in->name);
157 olsrd_free_cnf(struct olsrd_config *cnf)
159 struct hna4_entry *h4d, *h4 = cnf->hna4_entries;
160 struct hna6_entry *h6d, *h6 = cnf->hna6_entries;
161 struct olsr_if *ind, *in = cnf->interfaces;
162 struct plugin_entry *ped, *pe = cnf->plugins;
163 struct if_config_options *iod, *io = cnf->if_options;
210 set_default_cnf(struct olsrd_config *cnf)
212 memset(cnf, 0, sizeof(struct olsrd_config));
214 cnf->debug_level = 1;
216 cnf->allow_no_interfaces = 1;
218 cnf->auto_willingness = 1;
221 cnf->use_hysteresis = 1;
222 cnf->hysteresis_param.scaling = HYST_SCALING;
223 cnf->hysteresis_param.thr_high = HYST_THRESHOLD_HIGH;
224 cnf->hysteresis_param.thr_low = HYST_THRESHOLD_LOW;
228 cnf->tc_redundancy = TC_REDUNDANCY;
229 cnf->mpr_coverage = MPR_COVERAGE;
238 olsrd_write_cnf(struct olsrd_config *cnf, char *fname)
240 struct hna4_entry *h4 = cnf->hna4_entries;
241 struct hna6_entry *h6 = cnf->hna6_entries;
242 struct olsr_if *in = cnf->interfaces;
243 struct plugin_entry *pe = cnf->plugins;
244 struct if_config_options *io = cnf->if_options;
245 char ipv6_buf[100]; /* buffer for IPv6 inet_htop */
250 fd = fopen(fname, "w");
254 fprintf(stderr, "Could not open file %s for writing\n%s\n", fname, strerror(errno));
258 printf("Writing config to file \"%s\".... ", fname);
260 fprintf(fd, "#\n# Configuration file for olsr.org olsrd\n# automatically generated by olsrd-cnf %s\n#\n\n\n", SOFTWARE_VERSION);
263 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);
266 fprintf(fd, "# IP version to use (4 or 6)\n\nIpVersion\t%d\n\n", cnf->ip_version);
270 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");
276 fprintf(fd, " %s ", inet_ntoa(in4));
277 in4.s_addr=h4->netmask;
278 fprintf(fd, "%s\n", inet_ntoa(in4));
282 fprintf(fd, "}\n\n");
286 fprintf(fd, "# HNA IPv6 routes\n# syntax: netaddr prefix\n# Example Internet gateway:\nHna6\n{\n");
291 fprintf(fd, " %s/%d\n", (char *)inet_ntop(AF_INET6, &h6->net.v6, ipv6_buf, sizeof(ipv6_buf)), h6->prefix_len);
296 fprintf(fd, "}\n\n");
300 fprintf(fd, "# Interfaces and their rulesets\nInterfaces\n{\n");
306 fprintf(fd, " \"%s\" \"%s\"\n", in->name, in->config);
310 fprintf(fd, "}\n\n");
314 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");
315 if(cnf->allow_no_interfaces)
316 fprintf(fd, "yes\n\n");
318 fprintf(fd, "no\n\n");
321 fprintf(fd, "# TOS(type of service) value for\n# the IP header of control traffic.\n# default is 16\n\n");
322 fprintf(fd, "TosValue\t%d\n\n", cnf->tos);
325 fprintf(fd, "# The fixed willingness to use(0-7)\n# or \"auto\" to set willingness dynammically\n# based on battery/power status\n\n");
326 if(cnf->auto_willingness)
327 fprintf(fd, "Willingness\tauto\n\n");
329 fprintf(fd, "Willingness%d\n\n", cnf->fixed_willingness);
332 fprintf(fd, "# Allow processes like the GUI front-end\n# to connect to the daemon. 'yes' or 'no'\n\n");
334 fprintf(fd, "IpcConnect\tyes\n\n");
336 fprintf(fd, "IpcConnect\tno\n\n");
341 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");
343 if(cnf->use_hysteresis)
345 fprintf(fd, "UseHysteresis\tyes\n\n");
346 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");
347 fprintf(fd, "HystScaling\t%0.2f\n", cnf->hysteresis_param.scaling);
348 fprintf(fd, "HystThrHigh\t%0.2f\n", cnf->hysteresis_param.thr_high);
349 fprintf(fd, "HystThrLow\t%0.2f\n", cnf->hysteresis_param.thr_low);
352 fprintf(fd, "UseHysteresis\tno\n\n");
357 fprintf(fd, "# Polling rate in seconds(float).\n# Auto uses default value 0.1 sec\n\n");
358 fprintf(fd, "Pollrate\t%0.2f\n", cnf->pollrate);
361 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");
362 fprintf(fd, "TcRedundancy\t%d\n\n", cnf->tc_redundancy);
365 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");
367 fprintf(fd, "MprCoverage\t%d\n\n", cnf->mpr_coverage);
372 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\nLoadPlugin\n{\n");
377 fprintf(fd, " PlName \"%s\"\n", pe->name);
381 fprintf(fd, "}\n\n");
387 fprintf(fd, "IfSetup \"%s\"\n{\n", io->name);
390 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");
392 if(io->ipv4_broadcast.v4)
394 in4.s_addr = io->ipv4_broadcast.v4;
395 fprintf(fd, " Ip4Broadcast\t %s\n\n", inet_ntoa(in4));
399 fprintf(fd, " #Ip4Broadcast\t255.255.255.255\n\n");
403 fprintf(fd, " # IPv6 address scope to use.\n # Must be 'site-local' or 'global'\n\n");
404 if(io->ipv6_addrtype)
405 fprintf(fd, " Ip6AddrType \tsite-local\n\n");
407 fprintf(fd, " Ip6AddrType \tglobal\n\n");
409 fprintf(fd, " # IPv6 multicast address to use when\n # using site-local addresses.\n # If not defined, ff05::15 is used\n");
410 fprintf(fd, " Ip6MulticastSite\t%s\n\n", (char *)inet_ntop(AF_INET6, &io->ipv6_multi_site.v6, ipv6_buf, sizeof(ipv6_buf)));
411 fprintf(fd, " # IPv6 multicast address to use when\n # using global addresses\n # If not defined, ff0e::1 is used\n");
412 fprintf(fd, " Ip6MulticastGlobal\t%s\n\n", (char *)inet_ntop(AF_INET6, &io->ipv6_multi_glbl.v6, ipv6_buf, sizeof(ipv6_buf)));
416 fprintf(fd, " # Emission intervals.\n # If not defined, RFC proposed values will\n # in most cases be used.\n\n");
419 fprintf(fd, " HelloInterval\t%0.2f\n", io->hello_params.emission_interval);
420 fprintf(fd, " HelloValidityTime\t%0.2f\n", io->hello_params.validity_time);
421 fprintf(fd, " TcInterval\t\t%0.2f\n", io->tc_params.emission_interval);
422 fprintf(fd, " TcValidityTime\t%0.2f\n", io->tc_params.validity_time);
423 fprintf(fd, " MidInterval\t\t%0.2f\n", io->mid_params.emission_interval);
424 fprintf(fd, " MidValidityTime\t%0.2f\n", io->mid_params.validity_time);
425 fprintf(fd, " HnaInterval\t\t%0.2f\n", io->hna_params.emission_interval);
426 fprintf(fd, " HnaValidityTime\t%0.2f\n", io->hna_params.validity_time);
432 fprintf(fd, "}\n\n\n");
435 fprintf(fd, "\n# END AUTOGENERATED CONFIG\n");
448 olsrd_print_cnf(struct olsrd_config *cnf)
450 struct hna4_entry *h4 = cnf->hna4_entries;
451 struct hna6_entry *h6 = cnf->hna6_entries;
452 struct olsr_if *in = cnf->interfaces;
453 struct plugin_entry *pe = cnf->plugins;
454 struct if_config_options *io = cnf->if_options;
455 char ipv6_buf[100]; /* buffer for IPv6 inet_htop */
458 printf(" *** olsrd configuration ***\n");
460 printf("Debug Level : %d\n", cnf->debug_level);
461 printf("IpVersion : %d\n", cnf->ip_version);
462 if(cnf->allow_no_interfaces)
463 printf("No interfaces : ALLOWED\n");
465 printf("No interfaces : NOT ALLOWED\n");
466 printf("TOS : 0x%02x\n", cnf->tos);
467 if(cnf->auto_willingness)
468 printf("Willingness : AUTO\n");
470 printf("Willingness : %d\n", cnf->fixed_willingness);
473 printf("IPC : ENABLED\n");
475 printf("IPC : DISABLED\n");
477 printf("Pollrate : %0.2f\n", cnf->pollrate);
479 printf("TC redundancy : %d\n", cnf->tc_redundancy);
481 printf("MPR coverage : %d\n", cnf->mpr_coverage);
486 printf("Interfaces:\n");
489 printf("\tdev: \"%s\" ruleset: \"%s\"\n", in->name, in->config);
497 printf("Interface ruleset \"%s\":\n", io->name);
500 if(io->ipv4_broadcast.v4)
502 in4.s_addr = io->ipv4_broadcast.v4;
503 printf("\tIPv4 broadcast : %s\n", inet_ntoa(in4));
507 printf("\tIPv4 broadcast : AUTO\n");
510 if(io->ipv6_addrtype)
511 printf("\tIPv6 addrtype : site-local\n");
513 printf("\tIPv6 addrtype : global\n");
515 //union olsr_ip_addr ipv6_multi_site;
516 //union olsr_ip_addr ipv6_multi_glbl;
517 printf("\tIPv6 multicast site : %s\n", (char *)inet_ntop(AF_INET6, &io->ipv6_multi_site.v6, ipv6_buf, sizeof(ipv6_buf)));
518 printf("\tIPv6 multicast global : %s\n", (char *)inet_ntop(AF_INET6, &io->ipv6_multi_glbl.v6, ipv6_buf, sizeof(ipv6_buf)));
520 printf("\tHELLO emission int : %0.2f\n", io->hello_params.emission_interval);
521 printf("\tHELLO validity time : %0.2f\n", io->hello_params.validity_time);
522 printf("\tTC emission int : %0.2f\n", io->tc_params.emission_interval);
523 printf("\tTC validity time : %0.2f\n", io->tc_params.validity_time);
524 printf("\tMID emission int : %0.2f\n", io->mid_params.emission_interval);
525 printf("\tMID validity time : %0.2f\n", io->mid_params.validity_time);
526 printf("\tHNA emission int : %0.2f\n", io->hna_params.emission_interval);
527 printf("\tHNA validity time : %0.2f\n", io->hna_params.validity_time);
537 printf("Plugins:\n");
541 printf("\tName: \"%s\"\n", pe->name);
547 if(cnf->use_hysteresis)
549 printf("Using hysteresis:\n");
550 printf("\tScaling : %0.2f\n", cnf->hysteresis_param.scaling);
551 printf("\tThr high: %0.2f\n", cnf->hysteresis_param.thr_high);
552 printf("\tThr low : %0.2f\n", cnf->hysteresis_param.thr_low);
555 printf("Not using hysteresis\n");
561 printf("HNA4 entries:\n");
565 printf("\t%s/", inet_ntoa(in4));
566 in4.s_addr=h4->netmask;
567 printf("%s\n", inet_ntoa(in4));
576 printf("HNA6 entries:\n");
579 printf("\t%s/%d\n", (char *)inet_ntop(AF_INET6, &h6->net.v6, ipv6_buf, sizeof(ipv6_buf)), h6->prefix_len);
588 struct if_config_options *
589 find_if_rule_by_name(struct if_config_options *io, char *name)
594 if(strcmp(io->name, name) == 0)