pmlag

Poor man's link aggregation
git clone git://git.finwo.net/app/pmlag
Log | Files | Refs | LICENSE

config.c (3363B)


      1 #include <stdio.h>
      2 #include <stdlib.h>
      3 #include <strings.h>
      4 
      5 #include "util/config.h"
      6 
      7 #include "config.h"
      8 
      9 struct cnf_directive * cfg_parse_redistribute(FILE *fd, struct cnf_directive *dir, void *user) {
     10   int i;
     11 
     12   if (dir->argc < 1) {
     13     fprintf(stderr, "`redistribute` directive needs at least 1 argument\n");
     14     exit(1);
     15   }
     16 
     17   // TODO: start building redistribute entry
     18 
     19 
     20   for(;;) {
     21     if (dir) cnf_directive_free(dir);
     22     dir = cnf_directive_read(fd);
     23     if (!dir) break; // EOF = done
     24     // Make following blocks identical in structure
     25     if(0) {}
     26 
     27     else if (!strcasecmp("ttl", dir->name)) {
     28       // TODO: route->ttl = atoi(argv[0])
     29     }
     30 
     31     else if (!strcasecmp("metric", dir->name)) {
     32       // TODO: route->metric = atoi(argv[0])
     33     }
     34 
     35     else {
     36       // unknown directive
     37       break;
     38     }
     39   }
     40 
     41   // Cleanup
     42   // TODO: store redist in current router def
     43 
     44   // Return the unparsed dir if set
     45   return dir;
     46 }
     47 
     48 struct cnf_directive * cfg_parse_route(FILE *fd, struct cnf_directive *dir, void *user) {
     49   int i;
     50 
     51   if (dir->argc < 1) {
     52     fprintf(stderr, "`route` directive needs at least 1\n");
     53     exit(1);
     54   }
     55 
     56   // TODO: start building route
     57 
     58   for(;;) {
     59     if (dir) cnf_directive_free(dir);
     60     dir = cnf_directive_read(fd);
     61     if (!dir) break; // EOF = done
     62 cfg_parse_route_reparse:
     63     // Make following blocks identical in structure
     64     if(0) {}
     65 
     66     else if (!strcasecmp("ttl", dir->name)) {
     67       // TODO: route->ttl = atoi(argv[0])
     68     }
     69 
     70     else if (!strcasecmp("metric", dir->name)) {
     71       // TODO: route->metric = atoi(argv[0])
     72     }
     73 
     74     else if (!strcasecmp("neighbour", dir->name)) {
     75       // TODO: route->neighbour[] = parse_mac(argv[0])
     76     }
     77 
     78     else {
     79       // unknown directive
     80       break;
     81     }
     82   }
     83 
     84   // Cleanup
     85   // TODO: store route in current router def
     86 
     87   // Return the unparsed dir if set
     88   return dir;
     89 }
     90 
     91 struct cnf_directive * cfg_parse_router(FILE *fd, struct cnf_directive *dir, void *user) {
     92   int i;
     93 
     94   if (dir->argc != 1) {
     95     fprintf(stderr, "`router` directive accepts only 1 argument\n");
     96     exit(1);
     97   }
     98 
     99   printf("Defining router: %s\n", dir->argv[0]);
    100 
    101   for(;;) {
    102     if (dir) cnf_directive_free(dir);
    103     dir = cnf_directive_read(fd);
    104     if (!dir) break; // EOF = done
    105 
    106 cfg_parse_router_reparse:
    107 
    108     // Make following blocks identical in structure
    109     if(0) {}
    110 
    111     else if (!strcasecmp("interface", dir->name)) {
    112       for( i = 0 ; i < dir->argc ; i++ ) {
    113         printf("  add iface: %s\n", dir->argv[i]);
    114       }
    115     }
    116 
    117     else if (!strcasecmp("address", dir->name)) {
    118       for( i = 0 ; i < dir->argc ; i++ ) {
    119         printf("  got addr: %s\n", dir->argv[i]);
    120       }
    121     }
    122 
    123     // else if (!strcasecmp("mac", dir->name)) {
    124     //   if (dir->argc != 1) {
    125     //     fprintf(stderr, "`mac` directive accepts only 1 argument\n");
    126     //     exit(1);
    127     //   }
    128     //   printf("  set mac: %s\n", dir->argv[0]);
    129     // }
    130 
    131     else if (!strcasecmp("route", dir->name)) {
    132       dir = cfg_parse_route(fd, dir, user);
    133       if (!dir) continue;
    134       goto cfg_parse_router_reparse;
    135     }
    136 
    137     else if (!strcasecmp("redistribute", dir->name)) {
    138       dir = cfg_parse_redistribute(fd, dir, user);
    139       if (!dir) continue;
    140       goto cfg_parse_router_reparse;
    141     }
    142 
    143 
    144     else {
    145       // unknown directive
    146       break;
    147     }
    148 
    149   }
    150 
    151   // TODO: store router in actual config
    152 
    153   return dir;
    154 }