New configfile parser and configuration scheme integrated
[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.8 2004/10/18 13:13:36 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 "interfaces.h"
34 #include <string.h>
35 #include <stdlib.h>
36
37 #include "olsrd_cfgparser.h"
38
39 /**
40  *Funtion that tries to read and parse the config
41  *file "filename"
42  *@param filename the name(full path) of the config file
43  *@return negative on error
44  */
45 int
46 read_config_file(char *filename)
47 {
48   struct olsr_if *in;
49   union hna_netmask netmask;
50   struct hna4_entry *h4;
51   struct hna6_entry *h6;
52
53   /*
54    * NB - CHECK IPv6 MULTICAST!
55    */
56   if((olsr_cnf = olsrd_parse_cnf(filename)) != NULL)
57     {
58       olsrd_print_cnf(olsr_cnf);  
59     }
60   else
61     {
62       printf("Using default config values(no configfile)\n");
63       olsr_cnf = olsrd_get_default_cnf();
64       return -1;
65     }
66
67   /* Add interfaces */
68   in = olsr_cnf->interfaces;
69   while(in)
70     {
71       queue_if(in->name, in->if_options);
72       in = in->next;
73     }
74
75   /* Add HNA4 entries */
76   h4 = olsr_cnf->hna4_entries;
77
78   while(h4)
79     {
80       netmask.v4 = h4->netmask;
81       add_local_hna4_entry((union olsr_ip_addr *)&h4->net, &netmask);
82       h4 = h4->next;
83     }
84
85   /* Add HNA6 entries */
86   h6 = olsr_cnf->hna6_entries;
87
88   while(h6)
89     {
90       netmask.v6 = h6->prefix_len;
91       add_local_hna6_entry(&h6->net, &netmask);
92       h6 = h6->next;
93     }
94
95   /* Add plugins */
96
97
98   return 0;
99
100 #if 0
101   FILE *conf_file;
102   char line[CONFIG_MAX_LINESIZE];
103   char *linebuf, *firstbuf;
104   char tmp[FILENAME_MAX];
105   int ipv, prefix6, tmp_debug_level;
106   char addr[20], mask[20];
107   char addr6[50];
108   struct in_addr in;
109   struct in6_addr in6;
110   union olsr_ip_addr net;
111   union hna_netmask netmask;
112
113
114
115           /*
116            * IPv4 broadcast 255.255.255.255 or interface detection
117            */
118           if(strncmp(line, "IP4BROAD", 8) == 0)
119             {
120               sscanf(&line[8], "%s", tmp);
121               if(strncmp(tmp, "auto", 4) != 0)
122                 {
123                   if (inet_aton(tmp, &in) == 0)
124                     {
125                       olsr_printf(2, "Invalid broadcast address! %s\nSkipping it!\n", tmp);
126                       continue;
127                     }
128
129                   bcast_set = 1;
130                  
131                   memcpy(&bcastaddr.sin_addr, &in.s_addr, sizeof(olsr_u32_t));
132
133                 }
134
135               olsr_printf(2, "IPv4 broadcast:            %s\n", tmp);
136
137               continue;
138             }
139
140
141           /*
142            * IPv6 address to prioritize
143            */
144           if(strncmp(line, "IP6ADDRTYPE", 11) == 0)
145             {
146               sscanf(&line[11], "%s", tmp);
147               if(strncmp(tmp, "site-local", 10) == 0)
148                 {
149                   ipv6_addrtype = IPV6_ADDR_SITELOCAL;
150                 }
151               else
152                 {
153                   if(strncmp(tmp, "global", 6) == 0)
154                     {
155                       ipv6_addrtype = 0;
156                     }
157                   else
158                     {
159                       fprintf(stderr, "Error parsing IPv6 type \"%s\" from configfile\n", tmp);
160                       exit(1);
161                     }
162                 }
163
164               olsr_printf(2, "IPv6 addrtype:             %s\n", tmp);
165
166               continue;
167             }
168
169
170           /*
171            * IPv6 multicast address for site local interfaces
172            */
173           if(strncmp(line, "IP6MULTI-SITE", 13) == 0)
174             {
175               sscanf(&line[13], "%s", tmp);
176               if(strncmp(tmp, "auto", 4) == 0)
177                 strncpy(ipv6_mult_site, OLSR_IPV6_MCAST_SITE_LOCAL, strlen(OLSR_IPV6_MCAST_SITE_LOCAL));
178               else
179                 strncpy(ipv6_mult_site, tmp, strlen(tmp));
180
181               olsr_printf(2, "IPv6 multicast site-local: %s\n", ipv6_mult_site);
182
183               continue;
184             }
185
186
187           /*
188            * IPv6 multicast address for global interfaces
189            */
190           if(strncmp(line, "IP6MULTI-GLOBAL", 15) == 0)
191             {
192               sscanf(&line[15], "%s", tmp);
193               if(strncmp(tmp, "auto", 4) == 0)
194                 strncpy(ipv6_mult_global, OLSR_IPV6_MCAST_GLOBAL, strlen(OLSR_IPV6_MCAST_GLOBAL));
195               else
196                 strncpy(ipv6_mult_global, tmp, strlen(tmp));
197
198               olsr_printf(2, "IPv6 multicast global:     %s\n", ipv6_mult_global);
199
200               continue;
201             }
202
203
204
205           /*
206            * Hello interval
207            */
208           if(strncmp(line, "HELLOINT", 8) == 0)
209             {
210               sscanf(&line[8], "%s", tmp);
211               if(strncmp(tmp, "auto", 4) != 0)
212                 sscanf(&line[8], "%f", &hello_int);
213
214
215               olsr_printf(2, "Hello interval:            %s\n", tmp);
216
217               continue;
218             }
219
220
221           /*
222            * Hello hold multiplier
223            */
224           if(strncmp(line, "HELLOMULTI", 10) == 0)
225             {
226               sscanf(&line[10], "%s", tmp);
227               if(strncmp(tmp, "auto", 4) != 0)
228                 sscanf(&line[10], "%d", &neighbor_timeout_mult);
229
230
231               olsr_printf(2, "Hello multiplier:          %s\n", tmp);
232
233               continue;
234             }
235
236
237           /*
238            * MID interval
239            */
240           if(strncmp(line, "MIDINT", 6) == 0)
241             {
242               sscanf(&line[6], "%s", tmp);
243               if(strncmp(tmp, "auto", 4) != 0)
244                 sscanf(tmp, "%f", &mid_int);
245
246
247               olsr_printf(2, "MID interval:              %s\n", tmp);
248
249               continue;
250             }
251
252
253           /*
254            * MID hold multiplier
255            */
256           if(strncmp(line, "MIDMULTI", 8) == 0)
257             {
258               sscanf(&line[8], "%s", tmp);
259               if(strncmp(tmp, "auto", 4) != 0)
260                 sscanf(tmp, "%d", &hna_timeout_mult);
261
262
263               olsr_printf(2, "HNA multiplier:            %s\n", tmp);
264
265               continue;
266             }
267
268
269           /*
270            * HNA interval
271            */
272           if(strncmp(line, "HNAINT", 6) == 0)
273             {
274               sscanf(&line[6], "%s", tmp);
275               if(strncmp(tmp, "auto", 4) != 0)
276                 sscanf(tmp, "%f", &hna_int);
277
278
279               olsr_printf(2, "HNA interval:              %s\n", tmp);
280
281               continue;
282             }
283
284
285           /*
286            * HNA hold multiplier
287            */
288           if(strncmp(line, "HNAMULTI", 8) == 0)
289             {
290               sscanf(&line[8], "%s", tmp);
291               if(strncmp(tmp, "auto", 4) != 0)
292                 sscanf(tmp, "%d", &hna_timeout_mult);
293
294
295               olsr_printf(2, "HNA multiplier:            %s\n", tmp);
296
297               continue;
298             }
299
300
301
302           /*
303            * Hello non-wlan interval
304            */
305           if(strncmp(line, "NWHELLOINT", 10) == 0)
306             {
307               sscanf(&line[10], "%s", tmp);
308               if(strncmp(tmp, "auto", 4) != 0)
309                 {
310                   sscanf(&line[10], "%f", &hello_int_nw);
311                 }
312
313               olsr_printf(2, "Non-WLAN HELLO interval:   %s\n", tmp);
314
315               continue;
316             }
317
318           /*
319            * Hello hold multiplier non-WLAN
320            */
321           if(strncmp(line, "NWHELLOMULTI", 12) == 0)
322             {
323               sscanf(&line[12], "%s", tmp);
324               if(strncmp(tmp, "auto", 4) != 0)
325                 sscanf(&line[12], "%d", &neighbor_timeout_mult_nw);
326
327
328               olsr_printf(2, "Hello multiplier non-WLAN: %s\n", tmp);
329
330               continue;
331             }
332
333
334
335           /*
336            * TC interval
337            */
338           if(strncmp(line, "TCINT", 5) == 0)
339             {
340               sscanf(&line[5], "%s", tmp);
341               if(strncmp(tmp, "auto", 4) != 0)
342                 sscanf(&line[5], "%f", &tc_int);
343
344
345               olsr_printf(2, "TC interval:               %s\n", tmp);
346
347               continue;
348             }
349
350
351           /*
352            * TC hold multiplier
353            */
354           if(strncmp(line, "TCMULTI", 7) == 0)
355             {
356               sscanf(&line[7], "%s", tmp);
357               if(strncmp(tmp, "auto", 4) != 0)
358                 sscanf(&line[7], "%d", &topology_timeout_mult);
359
360
361               olsr_printf(2, "TC multiplier:             %s\n", tmp);
362
363               continue;
364             }
365
366
367           /*
368            * Type Of Service
369            */
370           if(strncmp(line, "TOSVALUE", 8) == 0)
371             {
372               sscanf(&line[8], "%s", tmp);
373               if(strncmp(tmp, "auto", 4) != 0)
374                 sscanf(&line[8], "%d", (int *)&tos);
375
376
377               olsr_printf(2, "TOS:                       %s\n", tmp);
378
379               continue;
380             }
381
382           /*
383            * Willingness
384            */
385           if(strncmp(line, "WILLINGNESS", 11) == 0)
386             {
387               sscanf(&line[11], "%s", tmp);
388               if(strncmp(tmp, "auto", 4) != 0)
389                 {
390                   sscanf(&line[11], "%d", &my_willingness);
391                   if((0 > my_willingness) || (my_willingness > 7))
392                     {
393                       fprintf(stderr, "Error setting willingness! Bad value: %d\n", my_willingness);
394                     }
395                   else
396                     {
397                       willingness_set = 1;
398                     }
399                 }
400
401               olsr_printf(2, "Willingness:               %s\n", tmp);
402
403               continue;
404             }
405
406
407           /*
408            * Willingness
409            */
410           if(strncmp(line, "IPC-CONNECT", 11) == 0)
411             {
412               sscanf(&line[11], "%s", tmp);
413               if(strncmp(tmp, "yes", 3) == 0)
414                 {
415                   use_ipc = 1;
416                 }
417               olsr_printf(2, "IPC connections:           %s\n", tmp);
418
419               continue;
420             }
421
422
423
424           /*
425            *Hysteresis usage
426            */
427           if(strncmp(line, "USE_HYSTERESIS", 14) == 0)
428             {
429               sscanf(&line[14], "%s", tmp);
430               if(strncmp(tmp, "yes", 3) == 0)
431                 use_hysteresis = 1;
432               if(strncmp(tmp, "no", 2) == 0)
433                 use_hysteresis = 0;
434
435               olsr_printf(2, "Use hysteresis:            %s\n", tmp);
436
437               continue;
438             }
439
440
441           /*
442            *Hysteresis scaling
443            */
444           if(strncmp(line, "HYST_SCALING", 12) == 0)
445             {
446               sscanf(&line[12], "%4s", tmp);
447               if(strncmp(tmp, "auto", 4) != 0)
448                 sscanf(&line[12], "%4f", &hyst_scaling);
449
450               olsr_printf(2, "Hyst scaling:              %s\n", tmp);
451
452               continue;
453             }
454
455
456           /*
457            *Hysteresis low threshold
458            */
459           if(strncmp(line, "HYST_THR_LOW", 12) == 0)
460             {
461               sscanf(&line[12], "%s", tmp);
462               if(strncmp(tmp, "auto", 4) != 0)
463                 sscanf(&line[12], "%4f", &hyst_threshold_low);
464
465               olsr_printf(2, "Hyst threshold low:        %s\n", tmp);
466
467               continue;
468             }
469
470           /*
471            *Hysteresis high threshold
472            */
473           if(strncmp(line, "HYST_THR_HIGH", 13) == 0)
474             {
475               sscanf(&line[13], "%s", tmp);
476               if(strncmp(tmp, "auto", 4) != 0)
477                 sscanf(&line[13], "%4f", &hyst_threshold_high);
478
479               olsr_printf(2, "Hyst threshold high:       %s\n", tmp);
480
481               continue;
482             }
483
484
485           /*
486            *Topology redundancy
487            */
488           if(strncmp(line, "TC_REDUNDANCY", 13) == 0)
489             {
490               sscanf(&line[13], "%s", tmp);
491               if(strncmp(tmp, "auto", 4) != 0)
492                 sscanf(&line[13], "%d", &tc_redundancy);
493
494               olsr_printf(2, "TC redunanacy:             %s\n", tmp);
495
496               continue;
497             }
498
499
500           /*
501            *MPR redundancy
502            */
503           if(strncmp(line, "MPR_COVERAGE", 12) == 0)
504             {
505               sscanf(&line[12], "%s", tmp);
506               if(strncmp(tmp, "auto", 4) != 0)
507                 sscanf(&line[12], "%d", &mpr_coverage);
508
509               olsr_printf(2, "MPR coverage:              %s\n", tmp);
510
511               continue;
512             }
513
514
515
516           /*
517            *PLUGIN
518            */
519           if(strncmp(line, "LOAD_PLUGIN", 11) == 0)
520             {
521               sscanf(&line[11], "%s", tmp);
522
523               olsr_add_plugin(tmp);
524
525               olsr_printf(2, "PLUGIN:                    %s\n", tmp);
526
527               continue;
528             }
529
530
531           //olsr_syslog(OLSR_LOG_ERR, "Could not parse config file(%s) line:\"%s\"", filename, line);
532
533           olsr_printf(1, "Could not parse config file(%s) line: %s", filename, line);
534         
535       else
536         olsr_printf(5, "Skipping: %s", line);
537     
538
539
540   fclose(conf_file);
541   return 0;
542
543 #endif
544 }
545
546
547
548 struct if_config_options *
549 get_default_ifcnf(struct olsrd_config *cnf)
550 {
551   struct if_config_options *ifc = cnf->if_options;
552
553   while(ifc)
554     {
555       if(!strcmp(ifc->name, DEFAULT_IF_CONFIG_NAME))
556         return ifc;
557       ifc = ifc->next;
558     }
559   return NULL;
560 }