starter: Use new parser to read config file

This commit is contained in:
Tobias Brunner
2014-06-19 14:00:49 +02:00
parent 640c75bb2e
commit 81ba3c1a5e
4 changed files with 507 additions and 783 deletions
+80 -127
View File
@@ -1,6 +1,7 @@
/* automatic handling of confread struct arguments /*
* Copyright (C) 2014 Tobias Brunner
* Copyright (C) 2006 Andreas Steffen * Copyright (C) 2006 Andreas Steffen
* Hochschule fuer Technik Rapperswil, Switzerland * Hochschule fuer Technik Rapperswil
* *
* This program is free software; you can redistribute it and/or modify it * This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the * under the terms of the GNU General Public License as published by the
@@ -20,7 +21,6 @@
#include <library.h> #include <library.h>
#include <utils/debug.h> #include <utils/debug.h>
#include "keywords.h"
#include "confread.h" #include "confread.h"
#include "args.h" #include "args.h"
@@ -221,37 +221,16 @@ static const token_info_t token_info[] =
/* /*
* assigns an argument value to a struct field * assigns an argument value to a struct field
*/ */
bool assign_arg(kw_token_t token, kw_token_t first, kw_list_t *kw, char *base, bool assign_arg(kw_token_t token, kw_token_t first, char *key, char *value,
bool *assigned) void *base, bool *assigned)
{ {
char *p = base + token_info[token].offset; char *p = (char*)base + token_info[token].offset;
const char **list = token_info[token].list; const char **list = token_info[token].list;
int index = -1; /* used for enumeration arguments */ int index = -1; /* used for enumeration arguments */
seen_t *seen = (seen_t*)base; /* seen flags are at the top of the struct */
*assigned = FALSE; *assigned = FALSE;
DBG3(DBG_APP, " %s=%s", kw->entry->name, kw->value); DBG3(DBG_APP, " %s=%s", key, value);
if (*seen & SEEN_KW(token, first))
{
DBG1(DBG_APP, "# duplicate '%s' option", kw->entry->name);
return FALSE;
}
if (token == KW_ESP || token == KW_AH)
{
if (*seen & (SEEN_KW(KW_ESP, first) | SEEN_KW(KW_AH, first)))
{
DBG1(DBG_APP, "# can't have both 'ah' and 'esp' options");
return FALSE;
}
}
/* set flag that this argument has been seen */
*seen |= SEEN_KW(token, first);
/* is there a keyword list? */ /* is there a keyword list? */
if (list != NULL) if (list != NULL)
@@ -261,26 +240,26 @@ bool assign_arg(kw_token_t token, kw_token_t first, kw_list_t *kw, char *base,
while (*list != NULL && !match) while (*list != NULL && !match)
{ {
index++; index++;
match = streq(kw->value, *list++); match = streq(value, *list++);
} }
if (!match) if (!match)
{ {
DBG1(DBG_APP, "# bad value: %s=%s", kw->entry->name, kw->value); DBG1(DBG_APP, "# bad value: %s=%s", key, value);
return FALSE; return FALSE;
} }
} }
switch (token_info[token].type) switch (token_info[token].type)
{ {
case ARG_NONE: case ARG_NONE:
DBG1(DBG_APP, "# option '%s' not supported yet", kw->entry->name); DBG1(DBG_APP, "# option '%s' not supported yet", key);
return FALSE; return FALSE;
case ARG_ENUM: case ARG_ENUM:
{ {
if (index < 0) if (index < 0)
{ {
DBG1(DBG_APP, "# bad enumeration value: %s=%s (%d)", DBG1(DBG_APP, "# bad enumeration value: %s=%s (%d)",
kw->entry->name, kw->value, index); key, value, index);
return FALSE; return FALSE;
} }
@@ -290,93 +269,86 @@ bool assign_arg(kw_token_t token, kw_token_t first, kw_list_t *kw, char *base,
*b = (index > 0); *b = (index > 0);
} }
else else
{ { /* FIXME: this is not entirely correct as the args are enums */
int *i = (int *)p; int *i = (int *)p;
*i = index; *i = index;
} }
break;
} }
break; case ARG_UINT:
case ARG_UINT:
{ {
char *endptr; char *endptr;
u_int *u = (u_int *)p; u_int *u = (u_int *)p;
*u = strtoul(kw->value, &endptr, 10); *u = strtoul(value, &endptr, 10);
if (*endptr != '\0') if (*endptr != '\0')
{ {
DBG1(DBG_APP, "# bad integer value: %s=%s", kw->entry->name, DBG1(DBG_APP, "# bad integer value: %s=%s", key, value);
kw->value);
return FALSE; return FALSE;
} }
break;
} }
break; case ARG_ULNG:
case ARG_ULNG: case ARG_PCNT:
case ARG_PCNT:
{ {
char *endptr; char *endptr;
unsigned long *l = (unsigned long *)p; unsigned long *l = (unsigned long *)p;
*l = strtoul(kw->value, &endptr, 10); *l = strtoul(value, &endptr, 10);
if (token_info[token].type == ARG_ULNG) if (token_info[token].type == ARG_ULNG)
{ {
if (*endptr != '\0') if (*endptr != '\0')
{ {
DBG1(DBG_APP, "# bad integer value: %s=%s", kw->entry->name, DBG1(DBG_APP, "# bad integer value: %s=%s", key, value);
kw->value);
return FALSE; return FALSE;
} }
} }
else else
{ {
if ((*endptr != '%') || (endptr[1] != '\0') || endptr == kw->value) if ((*endptr != '%') || (endptr[1] != '\0') || endptr == value)
{ {
DBG1(DBG_APP, "# bad percent value: %s=%s", kw->entry->name, DBG1(DBG_APP, "# bad percent value: %s=%s", key, value);
kw->value);
return FALSE; return FALSE;
} }
} }
break;
} }
break; case ARG_ULLI:
case ARG_ULLI:
{ {
char *endptr; char *endptr;
unsigned long long *ll = (unsigned long long *)p; unsigned long long *ll = (unsigned long long *)p;
*ll = strtoull(kw->value, &endptr, 10); *ll = strtoull(value, &endptr, 10);
if (*endptr != '\0') if (*endptr != '\0')
{ {
DBG1(DBG_APP, "# bad integer value: %s=%s", kw->entry->name, DBG1(DBG_APP, "# bad integer value: %s=%s", key, value);
kw->value);
return FALSE; return FALSE;
} }
break;
} }
break; case ARG_UBIN:
case ARG_UBIN:
{ {
char *endptr; char *endptr;
u_int *u = (u_int *)p; u_int *u = (u_int *)p;
*u = strtoul(kw->value, &endptr, 2); *u = strtoul(value, &endptr, 2);
if (*endptr != '\0') if (*endptr != '\0')
{ {
DBG1(DBG_APP, "# bad binary value: %s=%s", kw->entry->name, DBG1(DBG_APP, "# bad binary value: %s=%s", key, value);
kw->value);
return FALSE; return FALSE;
} }
break;
} }
break; case ARG_TIME:
case ARG_TIME:
{ {
char *endptr; char *endptr;
time_t *t = (time_t *)p; time_t *t = (time_t *)p;
*t = strtoul(kw->value, &endptr, 10); *t = strtoul(value, &endptr, 10);
/* time in seconds? */ /* time in seconds? */
if (*endptr == '\0' || (*endptr == 's' && endptr[1] == '\0')) if (*endptr == '\0' || (*endptr == 's' && endptr[1] == '\0'))
@@ -401,8 +373,7 @@ bool assign_arg(kw_token_t token, kw_token_t first, kw_list_t *kw, char *base,
break; break;
} }
} }
DBG1(DBG_APP, "# bad duration value: %s=%s", kw->entry->name, DBG1(DBG_APP, "# bad duration value: %s=%s", key, value);
kw->value);
return FALSE; return FALSE;
} }
case ARG_STR: case ARG_STR:
@@ -411,9 +382,8 @@ bool assign_arg(kw_token_t token, kw_token_t first, kw_list_t *kw, char *base,
/* free any existing string */ /* free any existing string */
free(*cp); free(*cp);
/* assign the new string */ /* assign the new string */
*cp = strdupnull(kw->value); *cp = strdupnull(value);
break; break;
} }
default: default:
@@ -427,45 +397,26 @@ bool assign_arg(kw_token_t token, kw_token_t first, kw_list_t *kw, char *base,
/* /*
* frees all dynamically allocated arguments in a struct * frees all dynamically allocated arguments in a struct
*/ */
void free_args(kw_token_t first, kw_token_t last, char *base) void free_args(kw_token_t first, kw_token_t last, void *base)
{ {
kw_token_t token; kw_token_t token;
for (token = first; token <= last; token++) for (token = first; token <= last; token++)
{ {
char *p = base + token_info[token].offset; char *p = (char*)base + token_info[token].offset;
switch (token_info[token].type) switch (token_info[token].type)
{ {
case ARG_STR: case ARG_STR:
{ {
char **cp = (char **)p; char **cp = (char **)p;
free(*cp); free(*cp);
*cp = NULL; *cp = NULL;
break;
} }
break; default:
default: break;
break;
}
}
}
/*
* clone all dynamically allocated arguments in a struct
*/
void clone_args(kw_token_t first, kw_token_t last, char *base1, char *base2)
{
kw_token_t token;
for (token = first; token <= last; token++)
{
if (token_info[token].type == ARG_STR)
{
char **cp1 = (char **)(base1 + token_info[token].offset);
char **cp2 = (char **)(base2 + token_info[token].offset);
*cp1 = strdupnull(*cp2);
} }
} }
} }
@@ -473,40 +424,42 @@ void clone_args(kw_token_t first, kw_token_t last, char *base1, char *base2)
/* /*
* compare all arguments in a struct * compare all arguments in a struct
*/ */
bool cmp_args(kw_token_t first, kw_token_t last, char *base1, char *base2) bool cmp_args(kw_token_t first, kw_token_t last, void *base1, void *base2)
{ {
kw_token_t token; kw_token_t token;
for (token = first; token <= last; token++) for (token = first; token <= last; token++)
{ {
char *p1 = base1 + token_info[token].offset; char *p1 = (char*)base1 + token_info[token].offset;
char *p2 = base2 + token_info[token].offset; char *p2 = (char*)base2 + token_info[token].offset;
switch (token_info[token].type) switch (token_info[token].type)
{ {
case ARG_ENUM: case ARG_ENUM:
if (token_info[token].list == LST_bool)
{ {
bool *b1 = (bool *)p1; if (token_info[token].list == LST_bool)
bool *b2 = (bool *)p2;
if (*b1 != *b2)
{ {
return FALSE; bool *b1 = (bool *)p1;
} bool *b2 = (bool *)p2;
}
else
{
int *i1 = (int *)p1;
int *i2 = (int *)p2;
if (*i1 != *i2) if (*b1 != *b2)
{ {
return FALSE; return FALSE;
}
} }
else
{
int *i1 = (int *)p1;
int *i2 = (int *)p2;
if (*i1 != *i2)
{
return FALSE;
}
}
break;
} }
break; case ARG_UINT:
case ARG_UINT:
{ {
u_int *u1 = (u_int *)p1; u_int *u1 = (u_int *)p1;
u_int *u2 = (u_int *)p2; u_int *u2 = (u_int *)p2;
@@ -515,10 +468,10 @@ bool cmp_args(kw_token_t first, kw_token_t last, char *base1, char *base2)
{ {
return FALSE; return FALSE;
} }
break;
} }
break; case ARG_ULNG:
case ARG_ULNG: case ARG_PCNT:
case ARG_PCNT:
{ {
unsigned long *l1 = (unsigned long *)p1; unsigned long *l1 = (unsigned long *)p1;
unsigned long *l2 = (unsigned long *)p2; unsigned long *l2 = (unsigned long *)p2;
@@ -527,9 +480,9 @@ bool cmp_args(kw_token_t first, kw_token_t last, char *base1, char *base2)
{ {
return FALSE; return FALSE;
} }
break;
} }
break; case ARG_ULLI:
case ARG_ULLI:
{ {
unsigned long long *ll1 = (unsigned long long *)p1; unsigned long long *ll1 = (unsigned long long *)p1;
unsigned long long *ll2 = (unsigned long long *)p2; unsigned long long *ll2 = (unsigned long long *)p2;
@@ -538,9 +491,9 @@ bool cmp_args(kw_token_t first, kw_token_t last, char *base1, char *base2)
{ {
return FALSE; return FALSE;
} }
break;
} }
break; case ARG_TIME:
case ARG_TIME:
{ {
time_t *t1 = (time_t *)p1; time_t *t1 = (time_t *)p1;
time_t *t2 = (time_t *)p2; time_t *t2 = (time_t *)p2;
@@ -549,9 +502,9 @@ bool cmp_args(kw_token_t first, kw_token_t last, char *base1, char *base2)
{ {
return FALSE; return FALSE;
} }
break;
} }
break; case ARG_STR:
case ARG_STR:
{ {
char **cp1 = (char **)p1; char **cp1 = (char **)p1;
char **cp2 = (char **)p2; char **cp2 = (char **)p2;
@@ -564,10 +517,10 @@ bool cmp_args(kw_token_t first, kw_token_t last, char *base1, char *base2)
{ {
return FALSE; return FALSE;
} }
break;
} }
break; default:
default: break;
break;
} }
} }
return TRUE; return TRUE;
+6 -10
View File
@@ -1,6 +1,6 @@
/* automatic handling of confread struct arguments /*
* Copyright (C) 2006 Andreas Steffen * Copyright (C) 2006 Andreas Steffen
* Hochschule fuer Technik Rapperswil, Switzerland * Hochschule fuer Technik Rapperswil
* *
* This program is free software; you can redistribute it and/or modify it * This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the * under the terms of the GNU General Public License as published by the
@@ -17,15 +17,11 @@
#define _ARGS_H_ #define _ARGS_H_
#include "keywords.h" #include "keywords.h"
#include "ipsec-parser.h"
extern bool assign_arg(kw_token_t token, kw_token_t first, kw_list_t *kw bool assign_arg(kw_token_t token, kw_token_t first, char *key, char *value,
, char *base, bool *assigned); void *base, bool *assigned);
extern void free_args(kw_token_t first, kw_token_t last, char *base); void free_args(kw_token_t first, kw_token_t last, void *base);
extern void clone_args(kw_token_t first, kw_token_t last, char *base1 bool cmp_args(kw_token_t first, kw_token_t last, void *base1, void *base2);
, char *base2);
extern bool cmp_args(kw_token_t first, kw_token_t last, char *base1
, char *base2);
#endif /* _ARGS_H_ */ #endif /* _ARGS_H_ */
+417 -608
View File
File diff suppressed because it is too large Load Diff
+4 -38
View File
@@ -18,13 +18,6 @@
#include <kernel/kernel_ipsec.h> #include <kernel/kernel_ipsec.h>
#include "ipsec-parser.h"
/** to mark seen keywords */
typedef u_int64_t seen_t;
#define SEEN_NONE 0;
#define SEEN_KW(kw, base) ((seen_t)1 << ((kw) - (base)))
typedef enum { typedef enum {
STARTUP_NO, STARTUP_NO,
STARTUP_ADD, STARTUP_ADD,
@@ -92,7 +85,6 @@ typedef enum {
typedef struct starter_end starter_end_t; typedef struct starter_end starter_end_t;
struct starter_end { struct starter_end {
seen_t seen;
char *auth; char *auth;
char *auth2; char *auth2;
char *id; char *id;
@@ -121,22 +113,10 @@ struct starter_end {
char *dns; char *dns;
}; };
typedef struct also also_t;
struct also {
char *name;
bool included;
also_t *next;
};
typedef struct starter_conn starter_conn_t; typedef struct starter_conn starter_conn_t;
struct starter_conn { struct starter_conn {
seen_t seen;
char *name; char *name;
also_t *also;
kw_list_t *kw;
u_int visit;
startup_t startup; startup_t startup;
starter_state_t state; starter_state_t state;
@@ -193,11 +173,7 @@ struct starter_conn {
typedef struct starter_ca starter_ca_t; typedef struct starter_ca starter_ca_t;
struct starter_ca { struct starter_ca {
seen_t seen;
char *name; char *name;
also_t *also;
kw_list_t *kw;
u_int visit;
startup_t startup; startup_t startup;
starter_state_t state; starter_state_t state;
@@ -217,7 +193,6 @@ typedef struct starter_config starter_config_t;
struct starter_config { struct starter_config {
struct { struct {
seen_t seen;
bool charonstart; bool charonstart;
char *charondebug; char *charondebug;
bool uniqueids; bool uniqueids;
@@ -229,23 +204,14 @@ struct starter_config {
u_int err; u_int err;
u_int non_fatal_err; u_int non_fatal_err;
/* do we parse also statements */ /* connections list */
bool parse_also;
/* ca %default */
starter_ca_t ca_default;
/* connections list (without %default) */
starter_ca_t *ca_first, *ca_last; starter_ca_t *ca_first, *ca_last;
/* conn %default */ /* connections list */
starter_conn_t conn_default;
/* connections list (without %default) */
starter_conn_t *conn_first, *conn_last; starter_conn_t *conn_first, *conn_last;
}; };
extern starter_config_t *confread_load(const char *file); starter_config_t *confread_load(const char *file);
extern void confread_free(starter_config_t *cfg); void confread_free(starter_config_t *cfg);
#endif /* _IPSEC_CONFREAD_H_ */ #endif /* _IPSEC_CONFREAD_H_ */