]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/network/networkd-util.c
util-lib: split our string related calls from util.[ch] into its own file string...
[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 "string-util.h"
25 #include "util.h"
26
27 const char *address_family_boolean_to_string(AddressFamilyBoolean b) {
28 if (b == ADDRESS_FAMILY_YES ||
29 b == ADDRESS_FAMILY_NO)
30 return yes_no(b == ADDRESS_FAMILY_YES);
31
32 if (b == ADDRESS_FAMILY_IPV4)
33 return "ipv4";
34 if (b == ADDRESS_FAMILY_IPV6)
35 return "ipv6";
36
37 return NULL;
38 }
39
40 AddressFamilyBoolean address_family_boolean_from_string(const char *s) {
41 int r;
42
43 /* Make this a true superset of a boolean */
44
45 r = parse_boolean(s);
46 if (r > 0)
47 return ADDRESS_FAMILY_YES;
48 if (r == 0)
49 return ADDRESS_FAMILY_NO;
50
51 if (streq(s, "ipv4"))
52 return ADDRESS_FAMILY_IPV4;
53 if (streq(s, "ipv6"))
54 return ADDRESS_FAMILY_IPV6;
55
56 return _ADDRESS_FAMILY_BOOLEAN_INVALID;
57 }
58
59 DEFINE_CONFIG_PARSE_ENUM(config_parse_address_family_boolean, address_family_boolean, AddressFamilyBoolean, "Failed to parse option");
60
61 int config_parse_address_family_boolean_with_kernel(
62 const char* unit,
63 const char *filename,
64 unsigned line,
65 const char *section,
66 unsigned section_line,
67 const char *lvalue,
68 int ltype,
69 const char *rvalue,
70 void *data,
71 void *userdata) {
72
73 AddressFamilyBoolean *fwd = data, s;
74
75 assert(filename);
76 assert(lvalue);
77 assert(rvalue);
78 assert(data);
79
80 s = address_family_boolean_from_string(rvalue);
81 if (s < 0) {
82 if (streq(rvalue, "kernel"))
83 s = _ADDRESS_FAMILY_BOOLEAN_INVALID;
84 else {
85 log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse IPForwarding= option, ignoring: %s", rvalue);
86 return 0;
87 }
88 }
89
90 *fwd = s;
91
92 return 0;
93 }
94
95 static const char* const resolve_support_table[_RESOLVE_SUPPORT_MAX] = {
96 [RESOLVE_SUPPORT_NO] = "no",
97 [RESOLVE_SUPPORT_YES] = "yes",
98 [RESOLVE_SUPPORT_RESOLVE] = "resolve",
99 };
100
101 DEFINE_STRING_TABLE_LOOKUP(resolve_support, ResolveSupport);
102
103 int config_parse_resolve(
104 const char* unit,
105 const char *filename,
106 unsigned line,
107 const char *section,
108 unsigned section_line,
109 const char *lvalue,
110 int ltype,
111 const char *rvalue,
112 void *data,
113 void *userdata) {
114
115 ResolveSupport *resolve = data;
116 int k;
117
118 assert(filename);
119 assert(lvalue);
120 assert(rvalue);
121 assert(resolve);
122
123 /* Our enum shall be a superset of booleans, hence first try
124 * to parse as boolean, and then as enum */
125
126 k = parse_boolean(rvalue);
127 if (k > 0)
128 *resolve = RESOLVE_SUPPORT_YES;
129 else if (k == 0)
130 *resolve = RESOLVE_SUPPORT_NO;
131 else {
132 ResolveSupport s;
133
134 s = resolve_support_from_string(rvalue);
135 if (s < 0){
136 log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse %s= option, ignoring: %s", lvalue, rvalue);
137 return 0;
138 }
139
140 *resolve = s;
141 }
142
143 return 0;
144 }