pmlag

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

config.c (3564B)


      1 #include <stdio.h>
      2 #include <stdlib.h>
      3 #include <string.h>
      4 
      5 #include "config.h"
      6 
      7 #include "benhoyt/inih.h"
      8 #include "finwo/mindex.h"
      9 
     10 int mindex_bond_cmp(const void *a, const void *b, void *udata) {
     11   (void)udata;
     12   const struct pmlag_configuration_bond *_a = a;
     13   const struct pmlag_configuration_bond *_b = b;
     14   return strcmp(_a->name, _b->name);
     15 }
     16 
     17 void mindex_bond_purge(void *item, void *udata) {
     18   struct pmlag_configuration_bond *bond = item;
     19   (void)udata;
     20   if (!item) return;
     21   if (bond->name) free(bond->name);
     22 }
     23 
     24 #ifndef MIN
     25 #define MIN(a,b) ((a)<(b)?(a):(b))
     26 #endif
     27 
     28 static int config_load_handler(
     29   void *user,
     30   const char *section,
     31   const char *name,
     32   const char *value
     33 ) {
     34   // struct pmlag_bond  *bond  = NULL;
     35   // struct pmlag_iface *iface = NULL;
     36   // int bond_index = 0;
     37   // int iface_index = 0;
     38   struct pmlag_configuration* config = (struct pmlag_configuration *) user;
     39 
     40   // Detect invalid entries
     41   int i, l;
     42   for( i = 0, l = strlen(name) ; i < l ; i++ ) {
     43     if (name[i] == 0x5F                   ) continue; // '_'
     44     if (name[i] >= 0x30 && name[i] <= 0x39) continue; // 0-9
     45     if (name[i] >= 0x41 && name[i] <= 0x5A) continue; // A-Z
     46     if (name[i] >= 0x61 && name[i] <= 0x7A) continue; // a-z
     47     return 0; // Reject
     48   }
     49 
     50   // Strip # comments from values
     51   char *found_comment = strstr(value, "#");
     52   if (found_comment) {
     53     for(; found_comment > (value + 1) ;) {
     54       if ( *(found_comment) >= 0x21 && *(found_comment-1) <= 0x7E ) break;
     55       found_comment--;
     56     }
     57     *found_comment = 0x00;
     58   }
     59 
     60   // Find or create bond
     61   struct pmlag_configuration_bond *config_bond = mindex_get(config->bonds, (&(struct pmlag_configuration_bond){ .name = (char *)section }));
     62   if (!config_bond) {
     63     config_bond = calloc(1, sizeof(struct pmlag_configuration_bond));
     64     config_bond->name = strdup(section);
     65     mindex_set(config->bonds, config_bond);
     66   }
     67 
     68   // // Find the bond
     69   // struct pmlag_configuration_bond *config_bond = NULL;
     70   // for( i = 0 ; i < config->bond_len ; i++ ) {
     71   //   if (!strcmp(config->bonds[i].name, section)) {
     72   //     config_bond = &(config->bonds[i]);
     73   //     break;
     74   //   }
     75   // }
     76   // printf("%s:%d\n", __FILE__, __LINE__);
     77   // // Build new bond if missing
     78   // if (!config_bond) {
     79   //   // Expand array size if needed
     80   //   if ((config->bond_len+1) > config->bond_cap) {
     81   //     config->bond_cap = MIN(1, config->bond_cap*2);
     82   //     config->bonds = realloc(config->bonds, config->bond_cap * sizeof(struct pmlag_configuration_bond));
     83   //   }
     84   //   // Append new entry to list
     85   //   memset(&(config->bonds[config->bond_len]), 0, sizeof(struct pmlag_configuration_bond));
     86   //   config_bond = &(config->bonds[config->bond_len]);
     87   //   config->bond_len++;
     88   // }
     89 
     90   // Detect # comments and remove them
     91 
     92   return 1;
     93 }
     94 
     95 struct pmlag_configuration * config_load(char * filepath, struct pmlag_configuration *config) {
     96   // Load config, entry-by-entry
     97   if (!config) {
     98     config = calloc(1, sizeof(struct pmlag_configuration));
     99   }
    100   if (!config->bonds) {
    101     config->bonds = mindex_init(mindex_bond_cmp, mindex_bond_purge, NULL);
    102   }
    103 
    104   if (ini_parse(filepath, config_load_handler, config) < 0) {
    105     fprintf(stderr, "Can not load %s\n", filepath);
    106     return NULL;
    107   }
    108 
    109   printf("bond_len: %ld\n", mindex_length(config->bonds));
    110   printf("\n");
    111   printf("bonds:\n");
    112   int l = mindex_length(config->bonds);
    113   for(int i = 0 ; i < l ; i++) {
    114     struct pmlag_configuration_bond *bond = mindex_nth(config->bonds, i);
    115     printf("  - %s\n", bond->name);
    116   }
    117   printf("\n");
    118 
    119   return config;
    120 }