config_userapi.c 15.7 KB
Newer Older
1
2
3
4
5
/*
 * 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
6
 * the OAI Public License, Version 1.1  (the "License"); you may not use this file
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
 * 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/config_userapi.c
 * \brief configuration module, api implementation to access configuration parameters
 * \author Francois TABURET
 * \date 2017
 * \version 0.1
 * \company NOKIA BellLabs France
 * \email: francois.taburet@nokia-bell-labs.com
 * \note
 * \warning
 */
32
33
34

#define _GNU_SOURCE

35
36
37
38
39
40
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <dlfcn.h>
41
#include <arpa/inet.h>
42
#include <platform_types.h>
43
#include "config_userapi.h"
44
#include "../utils/LOG/log.h"
45

Raphael Defosseux's avatar
Raphael Defosseux committed
46
47
configmodule_interface_t *config_get_if(void) {
  if (cfgptr == NULL) {
48
49
  	if (isLogInitDone())
       LOG_W(ENB_APP,"[CONFIG] %s %d config module not initialized\n",__FILE__,__LINE__);
Raphael Defosseux's avatar
Raphael Defosseux committed
50
51
52
  }

  return cfgptr;
53
54
}

Raphael Defosseux's avatar
Raphael Defosseux committed
55
56
57
58
59
60
61
62
63
64
char *config_check_valptr(paramdef_t *cfgoptions, char **ptr, int length) {
  if (ptr == NULL ) {
    ptr = malloc(sizeof(char *));

    if (ptr != NULL) {
      *ptr=NULL;
      cfgoptions->strptr=ptr;

      if ( (cfgoptions->paramflags & PARAMFLAG_NOFREE) == 0) {
        config_get_if()->ptrs[config_get_if()->numptrs] = (char *)ptr;
65
	config_get_if()->ptrsAllocated[config_get_if()->numptrs] = true;
Raphael Defosseux's avatar
Raphael Defosseux committed
66
67
68
        config_get_if()->numptrs++;
      }
    } else {
69
70
      CONFIG_PRINTF_ERROR("[CONFIG] %s %d option %s, cannot allocate pointer: %s \n",
                          __FILE__, __LINE__, cfgoptions->optname, strerror(errno));
Raphael Defosseux's avatar
Raphael Defosseux committed
71
72
73
74
75
76
77
78
79
    }
  }

  printf_ptrs("[CONFIG] %s ptr: 0x%08lx requested size: %i\n",cfgoptions->optname,(uintptr_t)(ptr),length);

  if(cfgoptions->numelt > 0 && PARAM_ISSCALAR(cfgoptions)  ) { /* already allocated */
    if (*ptr != NULL) {
      return *ptr;
    } else {
80
81
      CONFIG_PRINTF_ERROR("[CONFIG] %s %d option %s, definition error: value pointer is NULL, declared as %i bytes allocated\n",
                          __FILE__, __LINE__,cfgoptions->optname, cfgoptions->numelt);
Raphael Defosseux's avatar
Raphael Defosseux committed
82
83
84
85
    }
  }

  if (*ptr == NULL) {
86
87
    // LTS: dummy fix, waiting Francois full fix in 4G branch
    // the issue is we don't know at this point the size we will get
Thomas Laurent's avatar
Thomas Laurent committed
88
89
90
91
92
93
    // for parmeters on the command line, 
    // The length sould probably managed, in a later version
    // 100 is a very large value for a string parameter of today OAI
    if (length<100)
       length=100;
    *ptr = malloc(length);
Raphael Defosseux's avatar
Raphael Defosseux committed
94
95
96
97
98
99
100
101
102

    if ( *ptr != NULL) {
      memset(*ptr,0,length);

      if ( (cfgoptions->paramflags & PARAMFLAG_NOFREE) == 0) {
        config_get_if()->ptrs[config_get_if()->numptrs] = *ptr;
        config_get_if()->numptrs++;
      }
    } else {
103
      CONFIG_PRINTF_ERROR("[CONFIG] %s %d malloc error\n",__FILE__, __LINE__);
Raphael Defosseux's avatar
Raphael Defosseux committed
104
105
106
107
    }
  }

  return *ptr;
108
109
}

Raphael Defosseux's avatar
Raphael Defosseux committed
110
111
112
void config_assign_int(paramdef_t *cfgoptions, char *fullname, int val) {
  int tmpval=val;

113
  if ( ((cfgoptions->paramflags &PARAMFLAG_BOOL) != 0) && tmpval >0) {
Raphael Defosseux's avatar
Raphael Defosseux committed
114
    tmpval =1;
115
  }
Raphael Defosseux's avatar
Raphael Defosseux committed
116

117
  switch (cfgoptions->type) {
Raphael Defosseux's avatar
Raphael Defosseux committed
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
    case TYPE_UINT8:
      *(cfgoptions->u8ptr) = (uint8_t)tmpval;
      printf_params("[CONFIG] %s: %u\n", fullname, (uint8_t)tmpval);
      break;

    case TYPE_INT8:
      *(cfgoptions->i8ptr) = (int8_t)tmpval;
      printf_params("[CONFIG] %s: %i\n", fullname, (int8_t)tmpval);
      break;

    case TYPE_UINT16:
      *(cfgoptions->u16ptr) = (uint16_t)tmpval;
      printf_params("[CONFIG] %s: %hu\n", fullname, (uint16_t)tmpval);
      break;

    case TYPE_INT16:
      *(cfgoptions->i16ptr) = (int16_t)tmpval;
      printf_params("[CONFIG] %s: %hi\n", fullname, (int16_t)tmpval);
      break;

    case TYPE_UINT32:
      *(cfgoptions->uptr) = (uint32_t)tmpval;
      printf_params("[CONFIG] %s: %u\n", fullname, (uint32_t)tmpval);
      break;

    case TYPE_MASK:
      *(cfgoptions->uptr) = *(cfgoptions->uptr) | (uint32_t)tmpval;
      printf_params("[CONFIG] %s: 0x%08x\n", fullname, (uint32_t)tmpval);
      break;

    case TYPE_INT32:
      *(cfgoptions->iptr) = (int32_t)tmpval;
      printf_params("[CONFIG] %s: %i\n", fullname, (int32_t)tmpval);
      break;

    default:
      fprintf (stderr,"[CONFIG] %s %i type %i non integer parameter %s not assigned\n",__FILE__, __LINE__,cfgoptions->type,fullname);
      break;
156
157
  }
}
158
void config_assign_processedint(paramdef_t *cfgoption, int val) {
Raphael Defosseux's avatar
Raphael Defosseux committed
159
160
161
162
163
  cfgoption->processedvalue = malloc(sizeof(int));

  if (  cfgoption->processedvalue != NULL) {
    *(cfgoption->processedvalue) = val;
  } else {
164
    CONFIG_PRINTF_ERROR("[CONFIG] %s %d malloc error\n",__FILE__, __LINE__);
Raphael Defosseux's avatar
Raphael Defosseux committed
165
  }
166
}
167

168
int config_get_processedint(paramdef_t *cfgoption) {
Raphael Defosseux's avatar
Raphael Defosseux committed
169
170
171
172
173
174
175
176
177
178
179
180
181
  int ret;

  if (  cfgoption->processedvalue != NULL) {
    ret=*(cfgoption->processedvalue);
    free( cfgoption->processedvalue);
    cfgoption->processedvalue=NULL;
    printf_params("[CONFIG] %s:  set from %s to %i\n",cfgoption->optname, *(cfgoption->strptr), ret);
  } else {
    fprintf (stderr,"[CONFIG] %s %d %s has no processed integer availablle\n",__FILE__, __LINE__, cfgoption->optname);
    ret=0;
  }

  return ret;
182
}
183
void config_printhelp(paramdef_t *params,int numparams, char *prefix) {
184
  printf("\n-----Help for section %-26s: %03i entries------\n",(prefix==NULL)?"(root section)":prefix,numparams);
185

Raphael Defosseux's avatar
Raphael Defosseux committed
186
  for (int i=0 ; i<numparams ; i++) {
187
188
189
    printf("    %s%s: %s",
           (strlen(params[i].optname) <= 1) ? "-" : "--",
           params[i].optname,
190
           (params[i].helpstr != NULL)?params[i].helpstr:"Help string not specified\n");
191
192
193
  }   /* for on params entries */

  printf("--------------------------------------------------------------------\n\n");
194
195
}

196
int config_execcheck(paramdef_t *params, int numparams, char *prefix) {
Raphael Defosseux's avatar
Raphael Defosseux committed
197
198
199
200
201
202
203
204
205
206
207
208
209
  int st=0;

  for (int i=0 ; i<numparams ; i++) {
    if ( params[i].chkPptr == NULL) {
      continue;
    }

    if (params[i].chkPptr->s4.f4 != NULL) {
      st += params[i].chkPptr->s4.f4(&(params[i]));
    }
  }

  if (st != 0) {
210
    CONFIG_PRINTF_ERROR("[CONFIG] config_execcheck: section %s %i parameters with wrong value\n", prefix, -st);
Raphael Defosseux's avatar
Raphael Defosseux committed
211
212
213
  }

  return st;
214
215
}

Turker YILMAZ's avatar
Turker YILMAZ committed
216
int config_paramidx_fromname(paramdef_t *params, int numparams, char *name) {
217
218
219
220
221
222
223
224
225
  for (int i=0; i<numparams ; i++) {
    if (strcmp(name,params[i].optname) == 0)
      return i;
  }

  fprintf(stderr,"[CONFIG]config_paramidx_fromname , %s is not a valid parameter name\n",name);
  return -1;
}

226
int config_get(paramdef_t *params, int numparams, char *prefix) {
Raphael Defosseux's avatar
Raphael Defosseux committed
227
  int ret= -1;
228

229
  if (CONFIG_ISFLAGSET(CONFIG_ABORT)) {
230
    fprintf(stderr,"[CONFIG] config_get, section %s skipped, config module not properly initialized\n",prefix);
231
    return ret;
232
  }
Raphael Defosseux's avatar
Raphael Defosseux committed
233

234
  configmodule_interface_t *cfgif = config_get_if();
235
  if (cfgif != NULL) {
236
    ret = config_get_if()->get(params, numparams, prefix);
Raphael Defosseux's avatar
Raphael Defosseux committed
237

238
    if (ret >= 0) {
239
240
      config_process_cmdline(params, numparams, prefix);
      config_execcheck(params, numparams, prefix);
241
    }
Raphael Defosseux's avatar
Raphael Defosseux committed
242

243
    return ret;
244
  }
Raphael Defosseux's avatar
Raphael Defosseux committed
245

246
  return ret;
247
}
248

Raphael Defosseux's avatar
Raphael Defosseux committed
249
int config_getlist(paramlist_def_t *ParamList, paramdef_t *params, int numparams, char *prefix) {
250
251
252
253
  if (CONFIG_ISFLAGSET(CONFIG_ABORT)) {
    fprintf(stderr,"[CONFIG] config_get skipped, config module not properly initialized\n");
    return -1;
  }
Raphael Defosseux's avatar
Raphael Defosseux committed
254

255
256
257
258
  if (!config_get_if())
    return -1;

  const int ret = config_get_if()->getlist(ParamList, params, numparams, prefix);
Raphael Defosseux's avatar
Raphael Defosseux committed
259

260
261
  if (ret >= 0 && params) {
    char *newprefix;
Raphael Defosseux's avatar
Raphael Defosseux committed
262

263
264
    if (prefix) {
      int rc = asprintf(&newprefix, "%s.%s", prefix, ParamList->listname);
Raphael Defosseux's avatar
Raphael Defosseux committed
265

266
267
      if (rc < 0) newprefix = NULL;
    } else {
268
      newprefix = ParamList->listname;
269
    }
Raphael Defosseux's avatar
Raphael Defosseux committed
270

271
    char cfgpath[MAX_OPTNAME_SIZE*2 + 6]; /* prefix.listname.[listindex] */
Raphael Defosseux's avatar
Raphael Defosseux committed
272

273
274
275
    for (int i = 0; i < ParamList->numelt; ++i) {
      // TODO config_process_cmdline?
      sprintf(cfgpath, "%s.[%i]", newprefix, i);
276
      config_process_cmdline(ParamList->paramarray[i],numparams,cfgpath);
277
278
      config_execcheck(ParamList->paramarray[i], numparams, cfgpath);
    }
Raphael Defosseux's avatar
Raphael Defosseux committed
279

280
281
282
    if (prefix)
      free(newprefix);
  }
Raphael Defosseux's avatar
Raphael Defosseux committed
283

284
285
286
  return ret;
}

Raphael Defosseux's avatar
Raphael Defosseux committed
287
int config_isparamset(paramdef_t *params,int paramidx) {
288
  if ((params[paramidx].paramflags & PARAMFLAG_PARAMSET) != 0) {
Raphael Defosseux's avatar
Raphael Defosseux committed
289
    return 1;
290
  } else {
Raphael Defosseux's avatar
Raphael Defosseux committed
291
    return 0;
292
  }
293
}
294

295
void print_intvalueerror(paramdef_t *param, char *fname, int *okval, int numokval) {
Raphael Defosseux's avatar
Raphael Defosseux committed
296
297
298
299
300
301
302
303
  fprintf(stderr,"[CONFIG] %s: %s: %i invalid value, authorized values:\n       ",
          fname,param->optname, (int)*(param->uptr));

  for ( int i=0; i<numokval ; i++) {
    fprintf(stderr, " %i",okval[i]);
  }

  fprintf(stderr, " \n");
304
}
Raphael Defosseux's avatar
Raphael Defosseux committed
305
306
307
308

int config_check_intval(paramdef_t *param) {
  if ( param == NULL ) {
    fprintf(stderr,"[CONFIG] config_check_intval: NULL param argument\n");
309
    return -1;
Raphael Defosseux's avatar
Raphael Defosseux committed
310
311
312
313
314
315
316
317
318
319
  }

  for ( int i=0; i<param->chkPptr->s1.num_okintval ; i++) {
    if( *(param->uptr) == param->chkPptr->s1.okintval[i] ) {
      return 0;
    }
  }

  print_intvalueerror(param,"config_check_intval", param->chkPptr->s1.okintval,param->chkPptr->s1.num_okintval);
  return -1;
320
321
}

Raphael Defosseux's avatar
Raphael Defosseux committed
322
323
324
325
326
327
328
329
330
331
332
int config_check_modify_integer(paramdef_t *param) {
  for (int i=0; i < param->chkPptr->s1a.num_okintval ; i++) {
    if (*(param->uptr) == param->chkPptr->s1a.okintval[i] ) {
      printf_params("[CONFIG] %s:  read value %i, set to %i\n",param->optname,*(param->uptr),param->chkPptr->s1a.setintval [i]);
      *(param->uptr) = param->chkPptr->s1a.setintval [i];
      return 0;
    }
  }

  print_intvalueerror(param,"config_check_modify_integer", param->chkPptr->s1a.okintval,param->chkPptr->s1a.num_okintval);
  return -1;
333
334
}

Raphael Defosseux's avatar
Raphael Defosseux committed
335
336
337
338
339
340
341
342
int config_check_intrange(paramdef_t *param) {
  if( *(param->iptr) >= param->chkPptr->s2.okintrange[0]  && *(param->iptr) <= param->chkPptr->s2.okintrange[1]  ) {
    return 0;
  }

  fprintf(stderr,"[CONFIG] config_check_intrange: %s: %i invalid value, authorized range: %i %i\n",
          param->optname, (int)*(param->uptr), param->chkPptr->s2.okintrange[0], param->chkPptr->s2.okintrange[1]);
  return -1;
343
344
345
}

void print_strvalueerror(paramdef_t *param, char *fname, char **okval, int numokval) {
Raphael Defosseux's avatar
Raphael Defosseux committed
346
347
348
349
350
351
352
353
  fprintf(stderr,"[CONFIG] %s: %s: %s invalid value, authorized values:\n       ",
          fname,param->optname, *(param->strptr));

  for ( int i=0; i<numokval ; i++) {
    fprintf(stderr, " %s",okval[i]);
  }

  fprintf(stderr, " \n");
354
}
Raphael Defosseux's avatar
Raphael Defosseux committed
355
356
357
358

int config_check_strval(paramdef_t *param) {
  if ( param == NULL ) {
    fprintf(stderr,"[CONFIG] config_check_strval: NULL param argument\n");
359
    return -1;
Raphael Defosseux's avatar
Raphael Defosseux committed
360
  }
361

Raphael Defosseux's avatar
Raphael Defosseux committed
362
363
364
365
366
  for ( int i=0; i<param->chkPptr->s3.num_okstrval ; i++) {
    if( strcasecmp(*(param->strptr),param->chkPptr->s3.okstrval[i] ) == 0) {
      return 0;
    }
  }
367

Raphael Defosseux's avatar
Raphael Defosseux committed
368
369
370
  print_strvalueerror(param, "config_check_strval", param->chkPptr->s3.okstrval, param->chkPptr->s3.num_okstrval);
  return -1;
}
371

Raphael Defosseux's avatar
Raphael Defosseux committed
372
373
374
375
376
377
378
int config_checkstr_assign_integer(paramdef_t *param) {
  for (int i=0; i < param->chkPptr->s3a.num_okstrval ; i++) {
    if (strcasecmp(*(param->strptr),param->chkPptr->s3a.okstrval[i]  ) == 0) {
      config_assign_processedint(param, param->chkPptr->s3a.setintval[i]);
      return 0;
    }
  }
379

Raphael Defosseux's avatar
Raphael Defosseux committed
380
381
  print_strvalueerror(param, "config_check_strval", param->chkPptr->s3a.okstrval, param->chkPptr->s3a.num_okstrval);
  return -1;
382
}
383

384
385
386
387
388
389
void config_set_checkfunctions(paramdef_t *params, checkedparam_t *checkfunctions, int numparams) {
  for (int i=0; i< numparams ; i++ ) {
    params[i].chkPptr = &(checkfunctions[i]);
  }
}

Raphael Defosseux's avatar
Raphael Defosseux committed
390
int config_setdefault_string(paramdef_t *cfgoptions, char *prefix) {
391
  int status = 0;
Raphael Defosseux's avatar
Raphael Defosseux committed
392

393
  if( cfgoptions->defstrval != NULL) {
Raphael Defosseux's avatar
Raphael Defosseux committed
394
395
396
397
398
399
400
    status=1;

    if (cfgoptions->numelt == 0 ) {
      config_check_valptr(cfgoptions, cfgoptions->strptr, strlen(cfgoptions->defstrval)+1);
      sprintf(*(cfgoptions->strptr), "%s",cfgoptions->defstrval);
      printf_params("[CONFIG] %s.%s set to default value \"%s\"\n", ((prefix == NULL) ? "" : prefix), cfgoptions->optname, *(cfgoptions->strptr));
    } else {
401
402
      sprintf((char *)(cfgoptions->strptr), "%s",cfgoptions->defstrval);
      printf_params("[CONFIG] %s.%s set to default value \"%s\"\n", ((prefix == NULL) ? "" : prefix), cfgoptions->optname, (char *)(cfgoptions->strptr));
Raphael Defosseux's avatar
Raphael Defosseux committed
403
    }
404
  }
Raphael Defosseux's avatar
Raphael Defosseux committed
405

406
407
408
  return status;
}

Raphael Defosseux's avatar
Raphael Defosseux committed
409
int config_setdefault_stringlist(paramdef_t *cfgoptions, char *prefix) {
410
  int status = 0;
Raphael Defosseux's avatar
Raphael Defosseux committed
411

412
  if( cfgoptions->defstrlistval != NULL) {
Raphael Defosseux's avatar
Raphael Defosseux committed
413
414
415
416
417
    cfgoptions->strlistptr=cfgoptions->defstrlistval;
    status=1;

    for(int j=0; j<cfgoptions->numelt; j++)
      printf_params("[CONFIG] %s.%s[%i] set to default value %s\n", ((prefix == NULL) ? "" : prefix), cfgoptions->optname,j, cfgoptions->strlistptr[j]);
418
  }
Raphael Defosseux's avatar
Raphael Defosseux committed
419

420
421
422
  return status;
}

Raphael Defosseux's avatar
Raphael Defosseux committed
423
int config_setdefault_int(paramdef_t *cfgoptions, char *prefix) {
424
425
  int status = 0;
  config_check_valptr(cfgoptions, (char **)(&(cfgoptions->iptr)),sizeof(int32_t));
Raphael Defosseux's avatar
Raphael Defosseux committed
426

427
  if( ((cfgoptions->paramflags & PARAMFLAG_MANDATORY) == 0)) {
Raphael Defosseux's avatar
Raphael Defosseux committed
428
429
430
    config_assign_int(cfgoptions,cfgoptions->optname,cfgoptions->defintval);
    status=1;
    printf_params("[CONFIG] %s.%s set to default value\n", ((prefix == NULL) ? "" : prefix), cfgoptions->optname);
431
  }
Raphael Defosseux's avatar
Raphael Defosseux committed
432

433
  return status;
Raphael Defosseux's avatar
Raphael Defosseux committed
434
}
435

Raphael Defosseux's avatar
Raphael Defosseux committed
436
int config_setdefault_int64(paramdef_t *cfgoptions, char *prefix) {
437
438
  int status = 0;
  config_check_valptr(cfgoptions, (char **)&(cfgoptions->i64ptr),sizeof(long long));
Raphael Defosseux's avatar
Raphael Defosseux committed
439

440
  if( ((cfgoptions->paramflags & PARAMFLAG_MANDATORY) == 0)) {
Raphael Defosseux's avatar
Raphael Defosseux committed
441
442
443
    *(cfgoptions->u64ptr)=cfgoptions->defuintval;
    status=1;
    printf_params("[CONFIG] %s.%s set to default value %llu\n", ((prefix == NULL) ? "" : prefix), cfgoptions->optname, (long long unsigned)(*(cfgoptions->u64ptr)));
444
  }
Raphael Defosseux's avatar
Raphael Defosseux committed
445

446
447
448
  return status;
}

Raphael Defosseux's avatar
Raphael Defosseux committed
449
450
451
int config_setdefault_intlist(paramdef_t *cfgoptions, char *prefix) {
  int status = 0;

452
  if( cfgoptions->defintarrayval != NULL) {
Raphael Defosseux's avatar
Raphael Defosseux committed
453
    config_check_valptr(cfgoptions,(char **)&(cfgoptions->iptr), sizeof(int32_t *));
454
455
    cfgoptions->iptr=cfgoptions->defintarrayval;
    status=1;
Raphael Defosseux's avatar
Raphael Defosseux committed
456

457
    for (int j=0; j<cfgoptions->numelt ; j++) {
458
      printf_params("[CONFIG] %s[%i] set to default value %i\n",cfgoptions->optname,j,(int)cfgoptions->iptr[j]);
459
460
    }
  }
Raphael Defosseux's avatar
Raphael Defosseux committed
461

462
463
464
  return status;
}

Raphael Defosseux's avatar
Raphael Defosseux committed
465
int config_setdefault_double(paramdef_t *cfgoptions, char *prefix) {
466
467
  int status = 0;
  config_check_valptr(cfgoptions, (char **)&(cfgoptions->dblptr),sizeof(double));
Raphael Defosseux's avatar
Raphael Defosseux committed
468

469
  if( ((cfgoptions->paramflags & PARAMFLAG_MANDATORY) == 0)) {
470
    *(cfgoptions->dblptr)=cfgoptions->defdblval;
Raphael Defosseux's avatar
Raphael Defosseux committed
471
    status=1;
472
    printf_params("[CONFIG] %s set to default value %lf\n",cfgoptions->optname, *(cfgoptions->dblptr));
473
  }
Raphael Defosseux's avatar
Raphael Defosseux committed
474

475
  return status;
Raphael Defosseux's avatar
Raphael Defosseux committed
476
}
477

Raphael Defosseux's avatar
Raphael Defosseux committed
478
int config_assign_ipv4addr(paramdef_t *cfgoptions, char *ipv4addr) {
479
  config_check_valptr(cfgoptions,(char **)&(cfgoptions->uptr), sizeof(int));
480
  int rst=inet_pton(AF_INET, ipv4addr,cfgoptions->uptr );
Raphael Defosseux's avatar
Raphael Defosseux committed
481

482
  if (rst == 1 && *(cfgoptions->uptr) > 0) {
Raphael Defosseux's avatar
Raphael Defosseux committed
483
484
    printf_params("[CONFIG] %s: %s\n",cfgoptions->optname, ipv4addr);
    return 1;
485
  } else {
Raphael Defosseux's avatar
Raphael Defosseux committed
486
487
488
489
490
491
492
493
    if ( strncmp(ipv4addr,ANY_IPV4ADDR_STRING,sizeof(ANY_IPV4ADDR_STRING)) == 0) {
      printf_params("[CONFIG] %s:%s (INADDR_ANY) \n",cfgoptions->optname,ipv4addr);
      *cfgoptions->uptr=INADDR_ANY;
      return 1;
    } else {
      fprintf(stderr,"[CONFIG] %s not valid for %s \n", ipv4addr, cfgoptions->optname);
      return -1;
    }
494
  }
Raphael Defosseux's avatar
Raphael Defosseux committed
495

496
497
498
499
  return 0;
}


Raphael Defosseux's avatar
Raphael Defosseux committed
500
int config_setdefault_ipv4addr(paramdef_t *cfgoptions,  char *prefix) {
501
  int status = 0;
Raphael Defosseux's avatar
Raphael Defosseux committed
502

503
  if (cfgoptions->defstrval != NULL) {
Raphael Defosseux's avatar
Raphael Defosseux committed
504
    status = config_assign_ipv4addr(cfgoptions, cfgoptions->defstrval);
505
  }
Raphael Defosseux's avatar
Raphael Defosseux committed
506

507
  return status;
Raphael Defosseux's avatar
Raphael Defosseux committed
508
}