networkd-util.c revision 765afd5c4dbc71940d6dd6007ecc3eaa5a0b2aa1
0d8efe3535b3ce9ecbc2c6482007edfc3ac492d0Dave Reisner/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
328b5bc96e452b67ae2eb3dee3a09ff3ce03f662Dave Reisner
0d8efe3535b3ce9ecbc2c6482007edfc3ac492d0Dave Reisner/***
0d8efe3535b3ce9ecbc2c6482007edfc3ac492d0Dave Reisner This file is part of systemd.
0d8efe3535b3ce9ecbc2c6482007edfc3ac492d0Dave Reisner
0d8efe3535b3ce9ecbc2c6482007edfc3ac492d0Dave Reisner Copyright 2013 Tom Gundersen <teg@jklm.no>
0d8efe3535b3ce9ecbc2c6482007edfc3ac492d0Dave Reisner
0d8efe3535b3ce9ecbc2c6482007edfc3ac492d0Dave Reisner systemd is free software; you can redistribute it and/or modify it
0d8efe3535b3ce9ecbc2c6482007edfc3ac492d0Dave Reisner under the terms of the GNU Lesser General Public License as published by
0d8efe3535b3ce9ecbc2c6482007edfc3ac492d0Dave Reisner the Free Software Foundation; either version 2.1 of the License, or
0d8efe3535b3ce9ecbc2c6482007edfc3ac492d0Dave Reisner (at your option) any later version.
328b5bc96e452b67ae2eb3dee3a09ff3ce03f662Dave Reisner
328b5bc96e452b67ae2eb3dee3a09ff3ce03f662Dave Reisner systemd is distributed in the hope that it will be useful, but
328b5bc96e452b67ae2eb3dee3a09ff3ce03f662Dave Reisner WITHOUT ANY WARRANTY; without even the implied warranty of
328b5bc96e452b67ae2eb3dee3a09ff3ce03f662Dave Reisner MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
328b5bc96e452b67ae2eb3dee3a09ff3ce03f662Dave Reisner Lesser General Public License for more details.
328b5bc96e452b67ae2eb3dee3a09ff3ce03f662Dave Reisner
328b5bc96e452b67ae2eb3dee3a09ff3ce03f662Dave Reisner You should have received a copy of the GNU Lesser General Public License
328b5bc96e452b67ae2eb3dee3a09ff3ce03f662Dave Reisner along with systemd; If not, see <http://www.gnu.org/licenses/>.
328b5bc96e452b67ae2eb3dee3a09ff3ce03f662Dave Reisner***/
328b5bc96e452b67ae2eb3dee3a09ff3ce03f662Dave Reisner
328b5bc96e452b67ae2eb3dee3a09ff3ce03f662Dave Reisner#include "conf-parser.h"
328b5bc96e452b67ae2eb3dee3a09ff3ce03f662Dave Reisner#include "networkd-util.h"
328b5bc96e452b67ae2eb3dee3a09ff3ce03f662Dave Reisner#include "parse-util.h"
328b5bc96e452b67ae2eb3dee3a09ff3ce03f662Dave Reisner#include "string-table.h"
328b5bc96e452b67ae2eb3dee3a09ff3ce03f662Dave Reisner#include "string-util.h"
328b5bc96e452b67ae2eb3dee3a09ff3ce03f662Dave Reisner#include "util.h"
328b5bc96e452b67ae2eb3dee3a09ff3ce03f662Dave Reisner
328b5bc96e452b67ae2eb3dee3a09ff3ce03f662Dave Reisnerconst char *address_family_boolean_to_string(AddressFamilyBoolean b) {
328b5bc96e452b67ae2eb3dee3a09ff3ce03f662Dave Reisner if (b == ADDRESS_FAMILY_YES ||
328b5bc96e452b67ae2eb3dee3a09ff3ce03f662Dave Reisner b == ADDRESS_FAMILY_NO)
328b5bc96e452b67ae2eb3dee3a09ff3ce03f662Dave Reisner return yes_no(b == ADDRESS_FAMILY_YES);
328b5bc96e452b67ae2eb3dee3a09ff3ce03f662Dave Reisner
328b5bc96e452b67ae2eb3dee3a09ff3ce03f662Dave Reisner if (b == ADDRESS_FAMILY_IPV4)
328b5bc96e452b67ae2eb3dee3a09ff3ce03f662Dave Reisner return "ipv4";
328b5bc96e452b67ae2eb3dee3a09ff3ce03f662Dave Reisner if (b == ADDRESS_FAMILY_IPV6)
328b5bc96e452b67ae2eb3dee3a09ff3ce03f662Dave Reisner return "ipv6";
328b5bc96e452b67ae2eb3dee3a09ff3ce03f662Dave Reisner
328b5bc96e452b67ae2eb3dee3a09ff3ce03f662Dave Reisner return NULL;
328b5bc96e452b67ae2eb3dee3a09ff3ce03f662Dave Reisner}
328b5bc96e452b67ae2eb3dee3a09ff3ce03f662Dave Reisner
328b5bc96e452b67ae2eb3dee3a09ff3ce03f662Dave ReisnerAddressFamilyBoolean address_family_boolean_from_string(const char *s) {
328b5bc96e452b67ae2eb3dee3a09ff3ce03f662Dave Reisner int r;
328b5bc96e452b67ae2eb3dee3a09ff3ce03f662Dave Reisner
328b5bc96e452b67ae2eb3dee3a09ff3ce03f662Dave Reisner /* Make this a true superset of a boolean */
328b5bc96e452b67ae2eb3dee3a09ff3ce03f662Dave Reisner
328b5bc96e452b67ae2eb3dee3a09ff3ce03f662Dave Reisner r = parse_boolean(s);
0d8efe3535b3ce9ecbc2c6482007edfc3ac492d0Dave Reisner if (r > 0)
328b5bc96e452b67ae2eb3dee3a09ff3ce03f662Dave Reisner return ADDRESS_FAMILY_YES;
328b5bc96e452b67ae2eb3dee3a09ff3ce03f662Dave Reisner if (r == 0)
0d8efe3535b3ce9ecbc2c6482007edfc3ac492d0Dave Reisner return ADDRESS_FAMILY_NO;
328b5bc96e452b67ae2eb3dee3a09ff3ce03f662Dave Reisner
328b5bc96e452b67ae2eb3dee3a09ff3ce03f662Dave Reisner if (streq(s, "ipv4"))
0d8efe3535b3ce9ecbc2c6482007edfc3ac492d0Dave Reisner return ADDRESS_FAMILY_IPV4;
0d8efe3535b3ce9ecbc2c6482007edfc3ac492d0Dave Reisner if (streq(s, "ipv6"))
0d8efe3535b3ce9ecbc2c6482007edfc3ac492d0Dave Reisner return ADDRESS_FAMILY_IPV6;
0d8efe3535b3ce9ecbc2c6482007edfc3ac492d0Dave Reisner
0d8efe3535b3ce9ecbc2c6482007edfc3ac492d0Dave Reisner return _ADDRESS_FAMILY_BOOLEAN_INVALID;
0d8efe3535b3ce9ecbc2c6482007edfc3ac492d0Dave Reisner}
0d8efe3535b3ce9ecbc2c6482007edfc3ac492d0Dave Reisner
0d8efe3535b3ce9ecbc2c6482007edfc3ac492d0Dave ReisnerDEFINE_CONFIG_PARSE_ENUM(config_parse_address_family_boolean, address_family_boolean, AddressFamilyBoolean, "Failed to parse option");
0d8efe3535b3ce9ecbc2c6482007edfc3ac492d0Dave Reisner
0d8efe3535b3ce9ecbc2c6482007edfc3ac492d0Dave Reisnerint config_parse_address_family_boolean_with_kernel(
0d8efe3535b3ce9ecbc2c6482007edfc3ac492d0Dave Reisner const char* unit,
d0069261a71665a965b26d208dee098ea629cbbeDave Reisner const char *filename,
0d8efe3535b3ce9ecbc2c6482007edfc3ac492d0Dave Reisner unsigned line,
0d8efe3535b3ce9ecbc2c6482007edfc3ac492d0Dave Reisner const char *section,
unsigned section_line,
const char *lvalue,
int ltype,
const char *rvalue,
void *data,
void *userdata) {
AddressFamilyBoolean *fwd = data, s;
assert(filename);
assert(lvalue);
assert(rvalue);
assert(data);
/* This function is mostly obsolete now. It simply redirects
* "kernel" to "no". In older networkd versions we used to
* distuingish IPForward=off from IPForward=kernel, where the
* former would explicitly turn off forwarding while the
* latter would simply not touch the setting. But that logic
* is gone, hence silently accept the old setting, but turn it
* to "no". */
s = address_family_boolean_from_string(rvalue);
if (s < 0) {
if (streq(rvalue, "kernel"))
s = ADDRESS_FAMILY_NO;
else {
log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse IPForward= option, ignoring: %s", rvalue);
return 0;
}
}
*fwd = s;
return 0;
}
static const char* const resolve_support_table[_RESOLVE_SUPPORT_MAX] = {
[RESOLVE_SUPPORT_NO] = "no",
[RESOLVE_SUPPORT_YES] = "yes",
[RESOLVE_SUPPORT_RESOLVE] = "resolve",
};
DEFINE_STRING_TABLE_LOOKUP(resolve_support, ResolveSupport);
int config_parse_resolve(
const char* unit,
const char *filename,
unsigned line,
const char *section,
unsigned section_line,
const char *lvalue,
int ltype,
const char *rvalue,
void *data,
void *userdata) {
ResolveSupport *resolve = data;
int k;
assert(filename);
assert(lvalue);
assert(rvalue);
assert(resolve);
/* Our enum shall be a superset of booleans, hence first try
* to parse as boolean, and then as enum */
k = parse_boolean(rvalue);
if (k > 0)
*resolve = RESOLVE_SUPPORT_YES;
else if (k == 0)
*resolve = RESOLVE_SUPPORT_NO;
else {
ResolveSupport s;
s = resolve_support_from_string(rvalue);
if (s < 0){
log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse %s= option, ignoring: %s", lvalue, rvalue);
return 0;
}
*resolve = s;
}
return 0;
}