]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/network/networkd-util.c
util-lib: split string parsing related calls from util.[ch] into parse-util.[ch]
[thirdparty/systemd.git] / src / network / networkd-util.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4 This file is part of systemd.
5
6 Copyright 2013 Tom Gundersen <teg@jklm.no>
7
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
12
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include "conf-parser.h"
23 #include "networkd-util.h"
24 #include "parse-util.h"
25 #include "string-util.h"
26 #include "util.h"
27
28 const char *address_family_boolean_to_string(AddressFamilyBoolean b) {
29 if (b == ADDRESS_FAMILY_YES ||
30 b == ADDRESS_FAMILY_NO)
31 return yes_no(b == ADDRESS_FAMILY_YES);
32
33 if (b == ADDRESS_FAMILY_IPV4)
34 return "ipv4";
35 if (b == ADDRESS_FAMILY_IPV6)
36 return "ipv6";
37
38 return NULL;
39 }
40
41 AddressFamilyBoolean address_family_boolean_from_string(const char *s) {
42 int r;
43
44 /* Make this a true superset of a boolean */
45
46 r = parse_boolean(s);
47 if (r > 0)
48 return ADDRESS_FAMILY_YES;
49 if (r == 0)
50 return ADDRESS_FAMILY_NO;
51
52 if (streq(s, "ipv4"))
53 return ADDRESS_FAMILY_IPV4;
54 if (streq(s, "ipv6"))
55 return ADDRESS_FAMILY_IPV6;
56
57 return _ADDRESS_FAMILY_BOOLEAN_INVALID;
58 }
59
60 DEFINE_CONFIG_PARSE_ENUM(config_parse_address_family_boolean, address_family_boolean, AddressFamilyBoolean, "Failed to parse option");
61
62 int config_parse_address_family_boolean_with_kernel(
63 const char* unit,
64 const char *filename,
65 unsigned line,
66 const char *section,
67 unsigned section_line,
68 const char *lvalue,
69 int ltype,
70 const char *rvalue,
71 void *data,
72 void *userdata) {
73
74 AddressFamilyBoolean *fwd = data, s;
75
76 assert(filename);
77 assert(lvalue);
78 assert(rvalue);
79 assert(data);
80
81 s = address_family_boolean_from_string(rvalue);
82 if (s < 0) {
83 if (streq(rvalue, "kernel"))
84 s = _ADDRESS_FAMILY_BOOLEAN_INVALID;
85 else {
86 log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse IPForwarding= option, ignoring: %s", rvalue);
87 return 0;
88 }
89 }
90
91 *fwd = s;
92
93 return 0;
94 }
95
96 static const char* const resolve_support_table[_RESOLVE_SUPPORT_MAX] = {
97 [RESOLVE_SUPPORT_NO] = "no",
98 [RESOLVE_SUPPORT_YES] = "yes",
99 [RESOLVE_SUPPORT_RESOLVE] = "resolve",
100 };
101
102 DEFINE_STRING_TABLE_LOOKUP(resolve_support, ResolveSupport);
103
104 int config_parse_resolve(
105 const char* unit,
106 const char *filename,
107 unsigned line,
108 const char *section,
109 unsigned section_line,
110 const char *lvalue,
111 int ltype,
112 const char *rvalue,
113 void *data,
114 void *userdata) {
115
116 ResolveSupport *resolve = data;
117 int k;
118
119 assert(filename);
120 assert(lvalue);
121 assert(rvalue);
122 assert(resolve);
123
124 /* Our enum shall be a superset of booleans, hence first try
125 * to parse as boolean, and then as enum */
126
127 k = parse_boolean(rvalue);
128 if (k > 0)
129 *resolve = RESOLVE_SUPPORT_YES;
130 else if (k == 0)
131 *resolve = RESOLVE_SUPPORT_NO;
132 else {
133 ResolveSupport s;
134
135 s = resolve_support_from_string(rvalue);
136 if (s < 0){
137 log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse %s= option, ignoring: %s", lvalue, rvalue);
138 return 0;
139 }
140
141 *resolve = s;
142 }
143
144 return 0;
145 }