Updated UniK olsrd to olsr.org in licence headers
[olsrd.git] / src / configfile.c
1
2 /*
3  * OLSR ad-hoc routing table management protocol
4  * Copyright (C) 2004 Andreas T√łnnesen (andreto@ifi.uio.no)
5  *
6  * This file is part of the olsr.org OLSR daemon.
7  *
8  * olsr.org is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * olsr.org is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with olsr.org; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  *
22  */
23  
24
25 #include "defs.h"
26 #include "configfile.h"
27 #include "local_hna_set.h"
28 #include "olsr.h"
29 #include "plugin_loader.h"
30 #include <string.h>
31 #include <stdlib.h>
32 #include <syslog.h>
33
34
35 /**
36  *Funtion that tries to read and parse the config
37  *file "filename"
38  *@param filename the name(full path) of the config file
39  *@return negative on error
40  */
41 int
42 read_config_file(char *filename)
43 {
44   FILE *conf_file;
45   char line[CONFIG_MAX_LINESIZE];
46   char *linebuf, *firstbuf;
47   char tmp[FILENAME_MAX];
48   int ipv, prefix6, tmp_debug_level;
49   char addr[20], mask[20];
50   char addr6[50];
51   struct in_addr in;
52   struct in6_addr in6;
53   union olsr_ip_addr net;
54   union hna_netmask netmask;
55
56   conf_file = fopen(filename, "r");
57
58   olsr_printf(2, "Trying to read configuration from %s...", filename);
59
60   if(conf_file == NULL)
61     {
62       olsr_printf(2, "failed\n");
63       return -1;
64     }
65
66   olsr_printf(2, "OK\n\n");
67
68   /*output:
69    *Description: (at position 28)<value>
70    */
71
72   while(fgets(line, CONFIG_MAX_LINESIZE, conf_file))
73     {
74       if((line[0] != ' ') && 
75          (line[0] != '#') && 
76          (line[0] != '\n'))
77         {
78           olsr_printf(5, "parsing: %s", line);
79
80           /*
81            * IP version
82            */
83           if(strncmp(line, "IPVERSION", 9) == 0)
84             {
85               sscanf(&line[9], "%1d", &ipv);
86               switch(ipv)
87                 {
88                 case(4):
89                   ipversion = AF_INET;
90                   break;
91                 case(6):
92                   ipversion = AF_INET6;
93                   break;
94                 default:
95                   fprintf(stderr, "configfile: IPVERSION parsing failed(%d)!\n", ipv);
96                   break;
97                 }
98               olsr_printf(2, "IPVERSION:                 %d\n", ipv);
99               continue;
100             }
101
102           /* 
103            * Interfaces 
104            */
105           if(strncmp(line, "INTERFACES", 10) == 0)
106             {
107
108               linebuf = olsr_malloc(strlen(&line[10] + 1), "Config");
109               firstbuf = linebuf;
110               linebuf = strtok(&line[10], " \t\n");
111
112               /* Interfaces to run on */
113               while(linebuf != NULL)
114                 {
115                   option_i = 1;
116                   /* Insert into queue */
117                   queue_if(linebuf);
118
119                   olsr_printf(2, "adding interface:          %s\n", linebuf);
120
121                   linebuf = strtok(NULL, " \t\n");                
122                 }
123
124               free(firstbuf);
125               continue;
126             }
127
128           /* 
129            * Run if no iterfaces are present? 
130            */
131           if(strncmp(line, "ALLOW_NO_INT", 12) == 0)
132             {
133               sscanf(&line[12], "%s", tmp);
134               if(strncmp(tmp, "yes", 3) == 0)
135                 {
136                   allow_no_int = 1;
137                 }
138               else
139                 {
140                   allow_no_int = 0;
141                 }
142
143               olsr_printf(2, "Allow no interfaces:       %s\n", tmp);
144
145               continue;
146             }
147
148           /*
149            * HNA
150            */
151           if(strncmp(line, "HNA4", 4) == 0)
152             {
153               sscanf(&line[4], "%s %s", addr, mask);
154               if (inet_aton(addr, &in) == 0)
155                 {
156                   fprintf(stderr, "Error parsing HNA addr %s from configfile\n", addr);
157                   olsr_exit(__func__, EXIT_FAILURE);
158                 }
159               memcpy(&net, &in.s_addr, sizeof(olsr_u32_t));
160               if (inet_aton(mask, &in) == 0)
161                 {
162                   fprintf(stderr, "Error parsing HNA netmask %s from configfile\n", addr);
163                   olsr_exit(__func__, EXIT_FAILURE);
164                 }
165               memcpy(&netmask, &in.s_addr, sizeof(olsr_u32_t));
166
167               olsr_printf(2, "HNA:                       %s/%s\n", addr, mask);     
168               
169               add_local_hna4_entry(&net, &netmask);
170               continue;
171             }
172
173
174           /*
175            * HNA IPv6
176            */
177           if(strncmp(line, "HNA6", 4) == 0)
178             {
179               memset(addr6, 0, 50);
180               sscanf(&line[4], "%s %d", addr6, &prefix6);
181
182               //prefix6 = atol(&line[4 + strlen(addr6)]);
183               if(inet_pton(AF_INET6, addr6, &in6) < 0)
184                 {
185                   fprintf(stderr, "Error parsing HNA INET6 addr %s from configfile\n", addr6);
186                   olsr_exit(__func__, EXIT_FAILURE);
187                 }
188               memcpy(&net, &in6, sizeof(struct in6_addr));
189
190               /* Create the netmask */
191               if((prefix6 < 0) || (prefix6 > 128))
192                 {
193                   fprintf(stderr, "Error parsing HNA INET6 prefix %d for %s from configfile\n", prefix6, addr6);
194                   olsr_exit(__func__, EXIT_FAILURE);
195                 }             
196               //olsr_printf(2, "HNA6: %s\n", (char *)inet_ntop(AF_INET6, &in6, addr6, 50));
197               //if (inet_aton(mask, &in) == 0) continue;
198               //memcpy(&h.hna_net.netmask, &in.s_addr, sizeof(olsr_u32_t));
199               
200
201               olsr_printf(2, "HNA6:                      %s/%d\n", addr6, prefix6);         
202
203               netmask.v6 = prefix6;
204
205               add_local_hna6_entry(&net, &netmask);
206
207               continue;
208             }
209
210
211           /*
212            *Pollrate
213            */
214           if(strncmp(line, "POLLRATE", 8) == 0)
215             {
216               sscanf(&line[8], "%4s", tmp);
217               if(strncmp(tmp, "auto", 4) != 0)
218                 sscanf(&line[8], "%4f", &polling_int);
219
220               olsr_printf(2, "Polling rate:              %s\n", tmp);
221
222               continue;
223             }
224
225
226
227           /*
228            *DEBUGLEVEL
229            */
230           if(strncmp(line, "DEBUG", 5) == 0)
231             {
232               sscanf(&line[5], "%d", &tmp_debug_level);
233               if((tmp_debug_level < 0) || (tmp_debug_level > 9))
234                 {
235                   fprintf(stderr, "Wrong DEBUG value \"%d\" in config file!\n", debug_level);
236                   olsr_exit(__func__, EXIT_FAILURE);
237                 }
238               debug_level = tmp_debug_level;
239               olsr_printf(2, "Debug level:               %d\n", debug_level);
240
241               continue;
242             }
243
244           /*
245            * IPv4 broadcast 255.255.255.255 or interface detection
246            */
247           if(strncmp(line, "IP4BROAD", 8) == 0)
248             {
249               sscanf(&line[8], "%s", tmp);
250               if(strncmp(tmp, "auto", 4) != 0)
251                 {
252                   if (inet_aton(tmp, &in) == 0)
253                     {
254                       olsr_printf(2, "Invalid broadcast address! %s\nSkipping it!\n", tmp);
255                       continue;
256                     }
257
258                   bcast_set = 1;
259                  
260                   memcpy(&bcastaddr.sin_addr, &in.s_addr, sizeof(olsr_u32_t));
261
262                 }
263
264               olsr_printf(2, "IPv4 broadcast:            %s\n", tmp);
265
266               continue;
267             }
268
269
270           /*
271            * IPv6 address to prioritize
272            */
273           if(strncmp(line, "IP6ADDRTYPE", 11) == 0)
274             {
275               sscanf(&line[11], "%s", tmp);
276               if(strncmp(tmp, "site-local", 10) == 0)
277                 {
278                   ipv6_addrtype = IPV6_ADDR_SITELOCAL;
279                 }
280               else
281                 {
282                   if(strncmp(tmp, "global", 6) == 0)
283                     {
284                       ipv6_addrtype = 0;
285                     }
286                   else
287                     {
288                       fprintf(stderr, "Error parsing IPv6 type \"%s\" from configfile\n", tmp);
289                       exit(1);
290                     }
291                 }
292
293               olsr_printf(2, "IPv6 addrtype:             %s\n", tmp);
294
295               continue;
296             }
297
298
299           /*
300            * IPv6 multicast address for site local interfaces
301            */
302           if(strncmp(line, "IP6MULTI-SITE", 13) == 0)
303             {
304               sscanf(&line[13], "%s", tmp);
305               if(strncmp(tmp, "auto", 4) == 0)
306                 strncpy(ipv6_mult_site, OLSR_IPV6_MCAST_SITE_LOCAL, strlen(OLSR_IPV6_MCAST_SITE_LOCAL));
307               else
308                 strncpy(ipv6_mult_site, tmp, strlen(tmp));
309
310               olsr_printf(2, "IPv6 multicast site-local: %s\n", ipv6_mult_site);
311
312               continue;
313             }
314
315
316           /*
317            * IPv6 multicast address for global interfaces
318            */
319           if(strncmp(line, "IP6MULTI-GLOBAL", 15) == 0)
320             {
321               sscanf(&line[15], "%s", tmp);
322               if(strncmp(tmp, "auto", 4) == 0)
323                 strncpy(ipv6_mult_global, OLSR_IPV6_MCAST_GLOBAL, strlen(OLSR_IPV6_MCAST_GLOBAL));
324               else
325                 strncpy(ipv6_mult_global, tmp, strlen(tmp));
326
327               olsr_printf(2, "IPv6 multicast global:     %s\n", ipv6_mult_global);
328
329               continue;
330             }
331
332
333
334           /*
335            * Hello interval
336            */
337           if(strncmp(line, "HELLOINT", 8) == 0)
338             {
339               sscanf(&line[8], "%s", tmp);
340               if(strncmp(tmp, "auto", 4) != 0)
341                 sscanf(&line[8], "%f", &hello_int);
342
343
344               olsr_printf(2, "Hello interval:            %s\n", tmp);
345
346               continue;
347             }
348
349
350           /*
351            * Hello hold multiplier
352            */
353           if(strncmp(line, "HELLOMULTI", 10) == 0)
354             {
355               sscanf(&line[10], "%s", tmp);
356               if(strncmp(tmp, "auto", 4) != 0)
357                 sscanf(&line[10], "%d", &neighbor_timeout_mult);
358
359
360               olsr_printf(2, "Hello multiplier:          %s\n", tmp);
361
362               continue;
363             }
364
365
366           /*
367            * MID interval
368            */
369           if(strncmp(line, "MIDINT", 6) == 0)
370             {
371               sscanf(&line[6], "%s", tmp);
372               if(strncmp(tmp, "auto", 4) != 0)
373                 sscanf(tmp, "%f", &mid_int);
374
375
376               olsr_printf(2, "MID interval:              %s\n", tmp);
377
378               continue;
379             }
380
381
382           /*
383            * MID hold multiplier
384            */
385           if(strncmp(line, "MIDMULTI", 8) == 0)
386             {
387               sscanf(&line[8], "%s", tmp);
388               if(strncmp(tmp, "auto", 4) != 0)
389                 sscanf(tmp, "%d", &hna_timeout_mult);
390
391
392               olsr_printf(2, "HNA multiplier:            %s\n", tmp);
393
394               continue;
395             }
396
397
398           /*
399            * HNA interval
400            */
401           if(strncmp(line, "HNAINT", 6) == 0)
402             {
403               sscanf(&line[6], "%s", tmp);
404               if(strncmp(tmp, "auto", 4) != 0)
405                 sscanf(tmp, "%f", &hna_int);
406
407
408               olsr_printf(2, "HNA interval:              %s\n", tmp);
409
410               continue;
411             }
412
413
414           /*
415            * HNA hold multiplier
416            */
417           if(strncmp(line, "HNAMULTI", 8) == 0)
418             {
419               sscanf(&line[8], "%s", tmp);
420               if(strncmp(tmp, "auto", 4) != 0)
421                 sscanf(tmp, "%d", &hna_timeout_mult);
422
423
424               olsr_printf(2, "HNA multiplier:            %s\n", tmp);
425
426               continue;
427             }
428
429
430
431           /*
432            * Hello non-wlan interval
433            */
434           if(strncmp(line, "NWHELLOINT", 10) == 0)
435             {
436               sscanf(&line[10], "%s", tmp);
437               if(strncmp(tmp, "auto", 4) != 0)
438                 {
439                   sscanf(&line[10], "%f", &hello_int_nw);
440                 }
441
442               olsr_printf(2, "Non-WLAN HELLO interval:   %s\n", tmp);
443
444               continue;
445             }
446
447           /*
448            * Hello hold multiplier non-WLAN
449            */
450           if(strncmp(line, "NWHELLOMULTI", 12) == 0)
451             {
452               sscanf(&line[12], "%s", tmp);
453               if(strncmp(tmp, "auto", 4) != 0)
454                 sscanf(&line[12], "%d", &neighbor_timeout_mult_nw);
455
456
457               olsr_printf(2, "Hello multiplier non-WLAN: %s\n", tmp);
458
459               continue;
460             }
461
462
463
464           /*
465            * TC interval
466            */
467           if(strncmp(line, "TCINT", 5) == 0)
468             {
469               sscanf(&line[5], "%s", tmp);
470               if(strncmp(tmp, "auto", 4) != 0)
471                 sscanf(&line[5], "%f", &tc_int);
472
473
474               olsr_printf(2, "TC interval:               %s\n", tmp);
475
476               continue;
477             }
478
479
480           /*
481            * TC hold multiplier
482            */
483           if(strncmp(line, "TCMULTI", 7) == 0)
484             {
485               sscanf(&line[7], "%s", tmp);
486               if(strncmp(tmp, "auto", 4) != 0)
487                 sscanf(&line[7], "%d", &topology_timeout_mult);
488
489
490               olsr_printf(2, "TC multiplier:             %s\n", tmp);
491
492               continue;
493             }
494
495
496           /*
497            * Type Of Service
498            */
499           if(strncmp(line, "TOSVALUE", 8) == 0)
500             {
501               sscanf(&line[8], "%s", tmp);
502               if(strncmp(tmp, "auto", 4) != 0)
503                 sscanf(&line[8], "%d", (int *)&tos);
504
505
506               olsr_printf(2, "TOS:                       %s\n", tmp);
507
508               continue;
509             }
510
511           /*
512            * Willingness
513            */
514           if(strncmp(line, "WILLINGNESS", 11) == 0)
515             {
516               sscanf(&line[11], "%s", tmp);
517               if(strncmp(tmp, "auto", 4) != 0)
518                 {
519                   sscanf(&line[11], "%d", &my_willingness);
520                   if((0 > my_willingness) || (my_willingness > 7))
521                     {
522                       fprintf(stderr, "Error setting willingness! Bad value: %d\n", my_willingness);
523                     }
524                   else
525                     {
526                       willingness_set = 1;
527                     }
528                 }
529
530               olsr_printf(2, "Willingness:               %s\n", tmp);
531
532               continue;
533             }
534
535
536           /*
537            * Willingness
538            */
539           if(strncmp(line, "IPC-CONNECT", 11) == 0)
540             {
541               sscanf(&line[11], "%s", tmp);
542               if(strncmp(tmp, "yes", 3) == 0)
543                 {
544                   use_ipc = 1;
545                 }
546               olsr_printf(2, "IPC connections:           %s\n", tmp);
547
548               continue;
549             }
550
551
552
553           /*
554            *Hysteresis usage
555            */
556           if(strncmp(line, "USE_HYSTERESIS", 14) == 0)
557             {
558               sscanf(&line[14], "%s", tmp);
559               if(strncmp(tmp, "yes", 3) == 0)
560                 use_hysteresis = 1;
561               if(strncmp(tmp, "no", 2) == 0)
562                 use_hysteresis = 0;
563
564               olsr_printf(2, "Use hysteresis:            %s\n", tmp);
565
566               continue;
567             }
568
569
570           /*
571            *Hysteresis scaling
572            */
573           if(strncmp(line, "HYST_SCALING", 12) == 0)
574             {
575               sscanf(&line[12], "%4s", tmp);
576               if(strncmp(tmp, "auto", 4) != 0)
577                 sscanf(&line[12], "%4f", &hyst_scaling);
578
579               olsr_printf(2, "Hyst scaling:              %s\n", tmp);
580
581               continue;
582             }
583
584
585           /*
586            *Hysteresis low threshold
587            */
588           if(strncmp(line, "HYST_THR_LOW", 12) == 0)
589             {
590               sscanf(&line[12], "%s", tmp);
591               if(strncmp(tmp, "auto", 4) != 0)
592                 sscanf(&line[12], "%4f", &hyst_threshold_low);
593
594               olsr_printf(2, "Hyst threshold low:        %s\n", tmp);
595
596               continue;
597             }
598
599           /*
600            *Hysteresis high threshold
601            */
602           if(strncmp(line, "HYST_THR_HIGH", 13) == 0)
603             {
604               sscanf(&line[13], "%s", tmp);
605               if(strncmp(tmp, "auto", 4) != 0)
606                 sscanf(&line[13], "%4f", &hyst_threshold_high);
607
608               olsr_printf(2, "Hyst threshold high:       %s\n", tmp);
609
610               continue;
611             }
612
613
614           /*
615            *Topology redundancy
616            */
617           if(strncmp(line, "TC_REDUNDANCY", 13) == 0)
618             {
619               sscanf(&line[13], "%s", tmp);
620               if(strncmp(tmp, "auto", 4) != 0)
621                 sscanf(&line[13], "%d", &tc_redundancy);
622
623               olsr_printf(2, "TC redunanacy:             %s\n", tmp);
624
625               continue;
626             }
627
628
629           /*
630            *MPR redundancy
631            */
632           if(strncmp(line, "MPR_COVERAGE", 12) == 0)
633             {
634               sscanf(&line[12], "%s", tmp);
635               if(strncmp(tmp, "auto", 4) != 0)
636                 sscanf(&line[12], "%d", &mpr_coverage);
637
638               olsr_printf(2, "MPR coverage:              %s\n", tmp);
639
640               continue;
641             }
642
643
644
645           /*
646            *PLUGIN
647            */
648           if(strncmp(line, "LOAD_PLUGIN", 11) == 0)
649             {
650               sscanf(&line[11], "%s", tmp);
651
652               olsr_add_plugin(tmp);
653
654               olsr_printf(2, "PLUGIN:                    %s\n", tmp);
655
656               continue;
657             }
658
659
660           //olsr_syslog(OLSR_LOG_ERR, "Could not parse config file(%s) line:\"%s\"", filename, line);
661
662           olsr_printf(1, "Could not parse config file(%s) line: %s", filename, line);
663         }
664       else
665         olsr_printf(5, "Skipping: %s", line);
666     }
667
668
669   fclose(conf_file);
670   return 0;
671 }
672
673