3 * Copyright 2009 Roy Marples <roy@marples.name>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include <dbus/dbus.h>
36 #include "libdhcpcd.h"
38 static DHCPCD_CONFIG *
39 dhcpcd_config_new(const char *opt, const char *val)
43 c = malloc(sizeof(*c));
46 c->option = strdup(opt);
47 if (c->option == NULL) {
51 c->value = strdup(val);
52 if (c->value == NULL) {
62 dhcpcd_config_free(DHCPCD_CONFIG *config)
76 dhcpcd_config_blocks_get(DHCPCD_CONNECTION *con, const char *block)
78 DBusMessage *msg, *reply;
84 msg = dbus_message_new_method_call(DHCPCD_SERVICE, DHCPCD_PATH,
85 DHCPCD_SERVICE, "GetConfigBlocks");
87 dhcpcd_error_set(con, NULL, errno);
90 dbus_message_iter_init_append(msg, &args);
91 dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &block);
92 reply = dhcpcd_send_reply(con, msg);
93 dbus_message_unref(msg);
96 dbus_error_init(&error);
99 if (!dbus_message_get_args(reply, &error, DBUS_TYPE_ARRAY,
100 DBUS_TYPE_STRING, &blocks, &n_blocks,
103 dhcpcd_error_set(con, error.message, 0);
104 dbus_error_free(&error);
106 dbus_message_unref(reply);
111 dhcpcd_config_load(DHCPCD_CONNECTION *con, const char *block, const char *name)
113 DHCPCD_CONFIG *config, *c, *l;
114 DBusMessage *msg, *reply;
115 DBusMessageIter args, array, item;
116 const char ns[] = "", *option, *value;
119 msg = dbus_message_new_method_call(DHCPCD_SERVICE, DHCPCD_PATH,
120 DHCPCD_SERVICE, "GetConfig");
122 dhcpcd_error_set(con, NULL, errno);
125 dbus_message_iter_init_append(msg, &args);
130 dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &block);
131 dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &name);
132 reply = dhcpcd_send_reply(con, msg);
133 dbus_message_unref(msg);
136 if (!dbus_message_iter_init(reply, &args) ||
137 dbus_message_iter_get_arg_type(&args) != DBUS_TYPE_ARRAY)
139 dbus_message_unref(reply);
140 dhcpcd_error_set(con, NULL, EINVAL);
144 errors = con->errors;
145 dbus_message_iter_recurse(&args, &array);
147 dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_STRUCT;
148 dbus_message_iter_next(&array))
150 dbus_message_iter_recurse(&array, &item);
151 if (!dhcpcd_iter_get(con, &item, DBUS_TYPE_STRING, &option) ||
152 !dhcpcd_iter_get(con, &item, DBUS_TYPE_STRING, &value))
154 c = dhcpcd_config_new(option, value);
156 dhcpcd_error_set(con, NULL, errno);
165 if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_INVALID) {
166 if (con->errors == errors)
167 dhcpcd_error_set(con, NULL, EINVAL);
168 dhcpcd_config_free(config);
171 dbus_message_unref(reply);
176 dhcpcd_config_save(DHCPCD_CONNECTION *con, const char *block, const char *name,
177 DHCPCD_CONFIG *config)
179 DBusMessage *msg, *reply;
180 DBusMessageIter args, array, item;
182 const char ns[] = "", *p;
185 msg = dbus_message_new_method_call(DHCPCD_SERVICE, DHCPCD_PATH,
186 DHCPCD_SERVICE, "SetConfig");
188 dhcpcd_error_set(con, 0, errno);
191 dbus_message_iter_init_append(msg, &args);
192 dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &block);
193 dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &name);
194 dbus_message_iter_open_container(&args, DBUS_TYPE_ARRAY,
195 DBUS_STRUCT_BEGIN_CHAR_AS_STRING
196 DBUS_TYPE_STRING_AS_STRING
197 DBUS_TYPE_STRING_AS_STRING
198 DBUS_STRUCT_END_CHAR_AS_STRING,
200 for (c = config; c; c = c->next) {
201 dbus_message_iter_open_container(&array,
202 DBUS_TYPE_STRUCT, NULL, &item);
203 dbus_message_iter_append_basic(&item,
204 DBUS_TYPE_STRING, &c->option);
205 if (c->value == NULL)
209 dbus_message_iter_append_basic(&item, DBUS_TYPE_STRING, &p);
210 dbus_message_iter_close_container(&array, &item);
212 dbus_message_iter_close_container(&args, &array);
214 reply = dhcpcd_send_reply(con, msg);
215 dbus_message_unref(msg);
219 dbus_message_unref(reply);
225 static DHCPCD_CONFIG *
226 dhcpcd_config_get1(DHCPCD_CONFIG *config, const char *opt, DHCPCD_CONFIG **lst)
230 for (c = config; c; c = c->next) {
231 if (strcmp(c->option, opt) == 0)
241 dhcpcd_config_get(DHCPCD_CONFIG *config, const char *opt)
245 c = dhcpcd_config_get1(config, opt, NULL);
251 static DHCPCD_CONFIG *
252 dhcpcd_config_get_static1(DHCPCD_CONFIG *config, const char *opt,
260 while ((c = dhcpcd_config_get1(c, "static", lst)) != NULL) {
261 if (strncmp(c->value, opt, len) == 0)
271 dhcpcd_config_get_static(DHCPCD_CONFIG *config, const char *opt)
275 c = dhcpcd_config_get_static1(config, opt, NULL);
278 return c->value + strlen(opt);
282 dhcpcd_config_set1(DHCPCD_CONFIG **config, const char *opt, const char *val,
285 DHCPCD_CONFIG *c, *l;
291 c = dhcpcd_config_get_static1(*config, opt, &l);
293 c = dhcpcd_config_get1(*config, opt, &l);
307 len = strlen(opt) + strlen(val) + 2;
311 snprintf(t, len, "%s%s", opt, val);
319 c = dhcpcd_config_new("static", t);
321 c = dhcpcd_config_new(opt, val);
336 dhcpcd_config_set(DHCPCD_CONFIG **config, const char *opt, const char *val)
338 return dhcpcd_config_set1(config, opt, val, false);
342 dhcpcd_config_set_static(DHCPCD_CONFIG **config,
343 const char *opt, const char *val)
345 return dhcpcd_config_set1(config, opt, val, true);