Commit b5290af9 authored by Juliusz Chroboczek's avatar Juliusz Chroboczek

Add support for default parameters to config parser.

parent 61789872
......@@ -369,7 +369,7 @@ main(int argc, char **argv)
}
for(i = optind; i < argc; i++) {
vrc = add_interface(argv[i], NULL);
vrc = add_interface(argv[i], default_interface_conf);
if(vrc == NULL)
goto fail;
}
......
......@@ -152,9 +152,16 @@ An interface is configured by a single line with the following format:
.I name
.RI [ parameter ...]
.PP
.I Name
where
.I name
is the name of the interface (something like
.BR eth0 ).
The default values for all interfaces can be changed by
a line of the form
.IP
.B default
.RI [ parameter ...]
.PP
Each
.I parameter
......
......@@ -40,6 +40,7 @@ THE SOFTWARE.
struct filter *input_filters = NULL;
struct filter *output_filters = NULL;
struct filter *redistribute_filters = NULL;
struct interface_conf *default_interface_conf = NULL;
struct interface_conf *interface_confs = NULL;
/* This file implements a recursive descent parser with one character
......@@ -375,24 +376,17 @@ parse_filter(int c, gnc_t gnc, void *closure)
}
static struct interface_conf *
parse_ifconf(int c, gnc_t gnc, void *closure)
parse_anonymous_ifconf(int c, gnc_t gnc, void *closure,
struct interface_conf *if_conf)
{
char *token;
struct interface_conf *if_conf;
if(if_conf == NULL) {
if_conf = calloc(1, sizeof(struct interface_conf));
if(if_conf == NULL)
goto error;
c = skip_whitespace(c, gnc, closure);
if(c < -1 || c == '\n' || c == '#')
goto error;
c = getstring(c, &token, gnc, closure);
if(c < -1 || token == NULL)
goto error;
if_conf->ifname = token;
}
while(c >= 0 && c != '\n') {
c = skip_whitespace(c, gnc, closure);
......@@ -481,6 +475,33 @@ parse_ifconf(int c, gnc_t gnc, void *closure)
return NULL;
}
static struct interface_conf *
parse_ifconf(int c, gnc_t gnc, void *closure)
{
char *token;
struct interface_conf *if_conf;
if_conf = calloc(1, sizeof(struct interface_conf));
if(if_conf == NULL)
goto error;
c = skip_whitespace(c, gnc, closure);
if(c < -1 || c == '\n' || c == '#')
goto error;
c = getstring(c, &token, gnc, closure);
if(c < -1 || token == NULL)
goto error;
if_conf->ifname = token;
return parse_anonymous_ifconf(c, gnc, closure, if_conf);
error:
free(if_conf);
return NULL;
}
static void
add_filter(struct filter *filter, struct filter **filters)
{
......@@ -591,6 +612,18 @@ parse_config(gnc_t gnc, void *closure)
if(if_conf == NULL)
return -1;
add_ifconf(if_conf, &interface_confs);
} else if(strcmp(token, "default") == 0) {
struct interface_conf *if_conf;
if_conf = parse_anonymous_ifconf(c, gnc, closure, NULL);
if(if_conf == NULL)
return -1;
if(default_interface_conf == NULL)
default_interface_conf = if_conf;
else {
merge_ifconf(default_interface_conf,
if_conf, default_interface_conf);
free(if_conf);
}
} else {
return -1;
}
......@@ -789,6 +822,8 @@ finalise_config()
if_conf = interface_confs;
interface_confs = interface_confs->next;
if_conf->next = NULL;
if(default_interface_conf)
merge_ifconf(if_conf, if_conf, default_interface_conf);
vrc = add_interface(if_conf->ifname, if_conf);
if(vrc == NULL) {
fprintf(stderr, "Couldn't add interface %s.\n", if_conf->ifname);
......
......@@ -34,6 +34,8 @@ struct filter {
struct filter *next;
};
extern struct interface_conf *default_interface_conf;
int parse_config_from_file(const char *filename, int *line_return);
int parse_config_from_string(char *string);
void renumber_filters(void);
......
......@@ -62,8 +62,6 @@ add_interface(char *ifname, struct interface_conf *if_conf)
{
struct interface *ifp;
assert(!if_conf || strcmp(ifname, if_conf->ifname) == 0);
FOR_ALL_INTERFACES(ifp) {
if(strcmp(ifp->name, ifname) == 0) {
assert(if_conf == NULL);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment