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