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