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