config_libconfig.c 12.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
/*
 * Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The OpenAirInterface Software Alliance licenses this file to You under
 * the OAI Public License, Version 1.1  (the "License"); you may not use this file
 * except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.openairinterface.org/?page_id=698
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *-------------------------------------------------------------------------------
 * For more information about the OpenAirInterface (OAI) Software Alliance:
 *      contact@openairinterface.org
 */
/*! \file common/config/libconfig/config_libconfig.c
 * \brief: implementation libconfig configuration library
 * \author Francois TABURET
 * \date 2017
 * \version 0.1
 * \company NOKIA BellLabs France
 * \email: francois.taburet@nokia-bell-labs.com
 * \note
 * \warning
 */
31 32 33 34 35 36 37 38 39 40 41 42 43
#define _GNU_SOURCE
#include <libconfig.h>

#include <string.h>
#include <stdlib.h>

#include <arpa/inet.h>

#include "config_libconfig.h"
#include "config_libconfig_private.h"
#include "../config_userapi.h"
#include "errno.h"

44 45 46 47
#if ( LIBCONFIG_VER_MAJOR == 1 && LIBCONFIG_VER_MINOR < 5)
#define config_setting_lookup config_lookup_from
#endif

48 49 50 51 52 53
void config_libconfig_end(void );

int read_strlist(paramdef_t *cfgoptions,config_setting_t *setting, char *cfgpath)
{
const char *str;
int st;
54
int numelt;
55

56 57
   numelt=config_setting_length(setting);
   config_check_valptr(cfgoptions,(char **)&(cfgoptions->strlistptr), sizeof(char *) * numelt);
58
   st=0;
59
   for (int i=0; i< numelt ; i++) {
60 61 62 63
       str=config_setting_get_string_elem(setting,i);
       if (str==NULL) {
          printf("[LIBCONFIG] %s%i  not found in config file\n", cfgoptions->optname,i);
       } else {
64
            config_check_valptr(cfgoptions,&(cfgoptions->strlistptr[i]),strlen(str)+1);
65 66 67 68 69
            sprintf(cfgoptions->strlistptr[i],"%s",str);
	    st++;
            printf_params("[LIBCONFIG] %s%i: %s\n", cfgpath,i,cfgoptions->strlistptr[i]);
       }
   }
70
   cfgoptions->numelt=numelt;
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
   return st;
}

int read_intarray(paramdef_t *cfgoptions,config_setting_t *setting, char *cfgpath)
{

   cfgoptions->numelt=config_setting_length(setting);
   
   if (cfgoptions->numelt > 0) {
       cfgoptions->iptr=malloc(sizeof(int) * (cfgoptions->numelt));
       for (int i=0; i< cfgoptions->numelt && cfgoptions->iptr != NULL; i++) {
            cfgoptions->iptr[i]=config_setting_get_int_elem(setting,i);
            printf_params("[LIBCONFIG] %s[%i]: %i\n", cfgpath,i,cfgoptions->iptr[i]);
       } /* for loop on array element... */
   }
   return cfgoptions->numelt;
}




int config_libconfig_get(paramdef_t *cfgoptions,int numoptions, char *prefix )
{
  config_setting_t *setting;
95
  char *str;
96 97
  int i,u;
  long long int llu;
98
  double dbl; 
99 100 101
  int rst;
  int status=0;
  int notfound;
102
  int defval;
103
  int fatalerror=0;
104
  int numdefvals=0;
Cedric Roux's avatar
Cedric Roux committed
105 106
  char cfgpath[512];  /* 512 should be enough for the sprintf below */

107 108 109 110 111 112 113 114 115 116 117 118 119
  for(i=0;i<numoptions;i++) {

     if (prefix != NULL) {
         sprintf(cfgpath,"%s.%s",prefix,cfgoptions[i].optname);
     } else {
         sprintf(cfgpath,"%s",cfgoptions[i].optname);
     }

     if( (cfgoptions->paramflags & PARAMFLAG_DONOTREAD) != 0) {
         printf_params("[LIBCONFIG] %s.%s ignored\n", cfgpath,cfgoptions[i].optname );
         continue;
     }
     notfound=0;
120
     defval=0;
121 122 123
     switch(cfgoptions[i].type)
       {
       	case TYPE_STRING:
124 125 126 127 128 129
           if ( config_lookup_string(&(libconfig_privdata.cfg),cfgpath, (const char **)&str)) {
              if ( cfgoptions[i].numelt > 0  && str != NULL && strlen(str) >= cfgoptions[i].numelt ) {
                  fprintf(stderr,"[LIBCONFIG] %s:  %s exceeds maximum length of %i bytes, value truncated\n",
                           cfgpath,str,cfgoptions[i].numelt); 
                  str[strlen(str)-1] = 0;
              }
130
              if (cfgoptions[i].numelt == 0 ) {
131
        //          config_check_valptr(&(cfgoptions[i]), (char **)(&(cfgoptions[i].strptr)), sizeof(char *));
132 133
                  config_check_valptr(&(cfgoptions[i]), cfgoptions[i].strptr, strlen(str)+1);
                  sprintf( *(cfgoptions[i].strptr) , "%s", str);
134
                  printf_params("[LIBCONFIG] %s: \"%s\"\n", cfgpath,*(cfgoptions[i].strptr) );
135 136
              } else {
                 sprintf( (char *)(cfgoptions[i].strptr) , "%s", str);
137
                 printf_params("[LIBCONFIG] %s: \"%s\"\n", cfgpath,(char *)cfgoptions[i].strptr );
138
              }
139
           } else {
140
              defval=config_setdefault_string(&(cfgoptions[i]),prefix); 
141 142 143 144 145 146 147
	   }
       break;
       	case TYPE_STRINGLIST:
	   setting = config_setting_lookup (config_root_setting(&(libconfig_privdata.cfg)),cfgpath );
           if ( setting != NULL) {
              read_strlist(&cfgoptions[i],setting,cfgpath);
           } else {
148
              defval=config_setdefault_stringlist(&(cfgoptions[i]),prefix);
149 150
	   }
       break;
151 152 153 154 155 156 157
       	case TYPE_UINT8:
       	case TYPE_INT8:	
       	case TYPE_UINT16:
       	case TYPE_INT16:
       	case TYPE_UINT32:
       	case TYPE_INT32:
       	case TYPE_MASK:	
158
           if ( config_lookup_int(&(libconfig_privdata.cfg),cfgpath, &u)) {
159
              config_check_valptr(&(cfgoptions[i]), (char **)(&(cfgoptions[i].iptr)),sizeof(int32_t));
160
	      config_assign_int(&(cfgoptions[i]),cfgpath,u);
161
           } else {
162
              defval=config_setdefault_int(&(cfgoptions[i]),prefix);
163 164 165 166 167
	   }	      
        break;
       	case TYPE_UINT64:
       	case TYPE_INT64:
           if ( config_lookup_int64(&(libconfig_privdata.cfg),cfgpath, &llu)) {
168
              config_check_valptr(&(cfgoptions[i]), (char **)&(cfgoptions[i].i64ptr),sizeof(long long));
169 170 171 172 173 174 175 176
              if(cfgoptions[i].type==TYPE_UINT64) {
                 *(cfgoptions[i].u64ptr) = (uint64_t)llu;
                 printf_params("[LIBCONFIG] %s: %llu\n", cfgpath,(long long unsigned)(*(cfgoptions[i].u64ptr)) );
              } else {
                 *(cfgoptions[i].iptr) = llu;
                 printf_params("[LIBCONFIG] %s: %lli\n", cfgpath,(long long unsigned)(*(cfgoptions[i].i64ptr)) ); 
              }
           } else {
177
              defval=config_setdefault_int64(&(cfgoptions[i]),prefix);
178 179 180 181 182 183 184 185
	   }	      
        break;        
       	case TYPE_UINTARRAY:
       	case TYPE_INTARRAY:
	   setting = config_setting_lookup (config_root_setting(&(libconfig_privdata.cfg)),cfgpath );
           if ( setting != NULL) {
              read_intarray(&cfgoptions[i],setting,cfgpath);
           } else {
186
              defval=config_setdefault_intlist(&(cfgoptions[i]),prefix);
187 188
	   }    
        break;
189 190
       	case TYPE_DOUBLE:
           if ( config_lookup_float(&(libconfig_privdata.cfg),cfgpath, &dbl)) {
191
                 config_check_valptr(&(cfgoptions[i]), (char **)&(cfgoptions[i].dblptr),sizeof(double));
192 193 194
                 *(cfgoptions[i].dblptr) = dbl;
                 printf_params("[LIBCONFIG] %s: %lf\n", cfgpath,*(cfgoptions[i].dblptr) );
           } else {
195
              defval=config_setdefault_double(&(cfgoptions[i]),prefix);
196 197
	   }	      
        break;  
198
       	case TYPE_IPV4ADDR:
199
           if ( !config_lookup_string(&(libconfig_privdata.cfg),cfgpath, (const char **)&str)) {
200 201 202 203 204 205
              defval=config_setdefault_ipv4addr(&(cfgoptions[i]),prefix);
	   } else {
              rst=config_assign_ipv4addr(cfgoptions, str);
	      if (rst < 0) {
		 fprintf(stderr,"[LIBCONFIG] %s not valid for %s \n", str, cfgpath);
                 fatalerror=1;
206
              }
207
           }  
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
        break;
       	case TYPE_LIST:
	   setting = config_setting_lookup (config_root_setting(&(libconfig_privdata.cfg)),cfgpath );
           if ( setting) {
              cfgoptions[i].numelt=config_setting_length(setting);
           } else {
              notfound=1;
	   }
       break;
       default:
            fprintf(stderr,"[LIBCONFIG] %s type %i  not supported\n", cfgpath,cfgoptions[i].type);
            fatalerror=1;
       break;
       } /* switch on param type */
    if( notfound == 1) {
        printf("[LIBCONFIG] %s not found in %s ", cfgpath,libconfig_privdata.configfile );
        if ( (cfgoptions[i].paramflags & PARAMFLAG_MANDATORY) != 0) {
            fatalerror=1;
            printf("  mandatory parameter missing\n");
        } else {
           printf("\n");
        }
    } else {
231 232 233 234 235 236
      if (defval == 1) {
          numdefvals++;
	  cfgoptions[i].paramflags = cfgoptions[i].paramflags |  PARAMFLAG_PARAMSETDEF;
      } else {
          cfgoptions[i].paramflags = cfgoptions[i].paramflags |  PARAMFLAG_PARAMSET;
      }
237 238 239 240 241
      status++;
    }
  } /* for loop on options */
  printf("[LIBCONFIG] %s: %i/%i parameters successfully set, (%i to default value)\n",
         ((prefix == NULL)?"(root)":prefix), 
242
         status,numoptions,numdefvals );
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
  if (fatalerror == 1) {
      fprintf(stderr,"[LIBCONFIG] fatal errors found when processing  %s \n", libconfig_privdata.configfile );
      config_libconfig_end();
      end_configmodule();
  }
  return status;
}

int config_libconfig_getlist(paramlist_def_t *ParamList, 
                                   paramdef_t *params, int numparams, char *prefix)
{
config_setting_t *setting;
int i,j,status;
char *listpath=NULL;
char cfgpath[MAX_OPTNAME_SIZE*2 + 6]; /* prefix.listname.[listindex] */

    if (prefix != NULL)
        {
        i=asprintf(&listpath ,"%s.%s",prefix,ParamList->listname);
        }
    else
        {
        i=asprintf(&listpath ,"%s",ParamList->listname);
        }
    setting = config_lookup(&(libconfig_privdata.cfg), listpath);
    if ( setting) {
        status = ParamList->numelt = config_setting_length(setting);
        printf_params("[LIBCONFIG] %i %s in config file %s \n", 
               ParamList->numelt,listpath,libconfig_privdata.configfile );
    } else {
        printf("[LIBCONFIG] list %s not found in config file %s \n", 
               listpath,libconfig_privdata.configfile );
        ParamList->numelt= 0;
        status = -1;
    }
    if (ParamList->numelt > 0 && params != NULL) {
        ParamList->paramarray = malloc(ParamList->numelt * sizeof(paramdef_t *));
        if ( ParamList->paramarray != NULL) {
             config_get_if()->ptrs[config_get_if()->numptrs] = (char *)(ParamList->paramarray); 
             config_get_if()->numptrs++;
        } else {
             fprintf (stderr,"[LIBCONFIG] %s %d malloc error\n",__FILE__, __LINE__);
             exit(-1);
        }
        for (i=0 ; i < ParamList->numelt ; i++) {
            ParamList->paramarray[i] = malloc(numparams * sizeof(paramdef_t));
            if ( ParamList->paramarray[i] != NULL) {
                 config_get_if()->ptrs[config_get_if()->numptrs] = (char *)(ParamList->paramarray[i]); 
                 config_get_if()->numptrs++;
            } else {
                 fprintf (stderr,"[LIBCONFIG] %s %d malloc error\n",__FILE__, __LINE__);
                 exit(-1);
            }
            
            memcpy(ParamList->paramarray[i], params, sizeof(paramdef_t)*numparams);
            for (j=0;j<numparams;j++) {
                ParamList->paramarray[i][j].strptr = NULL ;
                } 
            sprintf(cfgpath,"%s.[%i]",listpath,i);
            config_libconfig_get(ParamList->paramarray[i], numparams, cfgpath );
        } /* for i... */
    } /* ParamList->numelt > 0 && params != NULL */
    if (listpath != NULL)
        free(listpath);
    return status;
}

int config_libconfig_init(char *cfgP[], int numP)
{
  config_init(&(libconfig_privdata.cfg));
  libconfig_privdata.configfile = strdup((char *)cfgP[0]);
  config_get_if()->numptrs=0;
  memset(config_get_if()->ptrs,0,sizeof(void *) * CONFIG_MAX_ALLOCATEDPTRS);

  /* Read the file. If there is an error, report it and exit. */
  if(! config_read_file(&(libconfig_privdata.cfg), libconfig_privdata.configfile)) {
319 320 321
    fprintf(stderr,"[LIBCONFIG] %s %d file %s - %d - %s\n",__FILE__, __LINE__,
                   libconfig_privdata.configfile, config_error_line(&(libconfig_privdata.cfg)),
                   config_error_text(&(libconfig_privdata.cfg)));
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
    config_destroy(&(libconfig_privdata.cfg));
    printf( "\n");
    return -1;
  }
  

  return 0;
}


void config_libconfig_end(void )
{
  config_destroy(&(libconfig_privdata.cfg));
  if ( libconfig_privdata.configfile != NULL ) {
     free(libconfig_privdata.configfile);
337
     libconfig_privdata.configfile=NULL;
338 339 340
  } 
  
}