]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/network/networkd-util.c
Merge pull request #6941 from andir/use-in_set
[thirdparty/systemd.git] / src / network / networkd-util.c
1 /***
2 This file is part of systemd.
3
4 Copyright 2013 Tom Gundersen <teg@jklm.no>
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #include "conf-parser.h"
21 #include "networkd-util.h"
22 #include "parse-util.h"
23 #include "string-table.h"
24 #include "string-util.h"
25 #include "util.h"
26
27 const char *address_family_boolean_to_string(AddressFamilyBoolean b) {
28 if (IN_SET(b, ADDRESS_FAMILY_YES, ADDRESS_FAMILY_NO))
29 return yes_no(b == ADDRESS_FAMILY_YES);
30
31 if (b == ADDRESS_FAMILY_IPV4)
32 return "ipv4";
33 if (b == ADDRESS_FAMILY_IPV6)
34 return "ipv6";
35
36 return NULL;
37 }
38
39 AddressFamilyBoolean address_family_boolean_from_string(const char *s) {
40 int r;
41
42 /* Make this a true superset of a boolean */
43
44 r = parse_boolean(s);
45 if (r > 0)
46 return ADDRESS_FAMILY_YES;
47 if (r == 0)
48 return ADDRESS_FAMILY_NO;
49
50 if (streq(s, "ipv4"))
51 return ADDRESS_FAMILY_IPV4;
52 if (streq(s, "ipv6"))
53 return ADDRESS_FAMILY_IPV6;
54
55 return _ADDRESS_FAMILY_BOOLEAN_INVALID;
56 }
57
58 DEFINE_CONFIG_PARSE_ENUM(config_parse_address_family_boolean, address_family_boolean, AddressFamilyBoolean, "Failed to parse option");
59
60 int config_parse_address_family_boolean_with_kernel(
61 const char* unit,
62 const char *filename,
63 unsigned line,
64 const char *section,
65 unsigned section_line,
66 const char *lvalue,
67 int ltype,
68 const char *rvalue,
69 void *data,
70 void *userdata) {
71
72 AddressFamilyBoolean *fwd = data, s;
73
74 assert(filename);
75 assert(lvalue);
76 assert(rvalue);
77 assert(data);
78
79 /* This function is mostly obsolete now. It simply redirects
80 * "kernel" to "no". In older networkd versions we used to
81 * distuingish IPForward=off from IPForward=kernel, where the
82 * former would explicitly turn off forwarding while the
83 * latter would simply not touch the setting. But that logic
84 * is gone, hence silently accept the old setting, but turn it
85 * to "no". */
86
87 s = address_family_boolean_from_string(rvalue);
88 if (s < 0) {
89 if (streq(rvalue, "kernel"))
90 s = ADDRESS_FAMILY_NO;
91 else {
92 log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse IPForward= option, ignoring: %s", rvalue);
93 return 0;
94 }
95 }
96
97 *fwd = s;
98
99 return 0;
100 }