3 * Copyright 2009-2012 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
31 #include "libdhcpcd.h"
34 dhcpcd_message_get_string(DHCPCD_MESSAGE *msg)
39 if (dbus_message_iter_init(msg, &args) &&
40 dbus_message_iter_get_arg_type(&args) == DBUS_TYPE_STRING)
42 dbus_message_iter_get_basic(&args, &str);
49 dhcpcd_handle_event(DHCPCD_CONNECTION *con, DHCPCD_MESSAGE *msg)
52 DHCPCD_IF *i, *e, *l, *n, *nl;
54 static const char *types[] = { "ipv4", "ra", NULL };
57 if (!dbus_message_iter_init(msg, &args))
60 i = dhcpcd_if_new(con, &args, &order);
66 while ((o = strsep(&p, " ")) != NULL) {
67 for (ti = 0; ti < 2; ti++) {
69 for (e = con->interfaces; e; e = e->next) {
70 if (strcmp(e->ifname, o) == 0 &&
71 strcmp(e->type, types[ti]) == 0)
81 con->interfaces = e->next;
84 strcmp(e->ifname, i->ifname) == 0 &&
85 strcmp(e->type, i->type) == 0)
87 /* Preserve the pointer for
88 * our wireless history */
89 memcpy(e, i, sizeof(*e));
104 nl->next = con->interfaces;
108 con->event(con, i, con->signal_data);
112 dhcpcd_dispatch_message(DHCPCD_CONNECTION *con, DHCPCD_MESSAGE *msg)
118 if (dbus_message_get_type(msg) != DBUS_MESSAGE_TYPE_SIGNAL)
122 dbus_connection_ref(con->bus);
123 dbus_message_ref(msg);
124 if (dbus_message_is_signal(msg, DHCPCD_SERVICE, "StatusChanged")) {
125 con->status = strdup(dhcpcd_message_get_string(msg));
126 if (strcmp(con->status, "down") == 0) {
127 dhcpcd_if_free(con->interfaces);
128 con->interfaces = NULL;
130 if (con->status_changed)
131 con->status_changed(con, con->status,
134 else if (dbus_message_is_signal(msg, DHCPCD_SERVICE, "ScanResults"))
136 if (con->wi_scanresults) {
137 str = dhcpcd_message_get_string(msg);
138 ifp = dhcpcd_if_find(con, str, "ipv4");
140 con->wi_scanresults(con, ifp, con->signal_data);
142 } else if (dbus_message_is_signal(msg, DHCPCD_SERVICE, "Event"))
143 dhcpcd_handle_event(con, msg);
146 dbus_message_unref(msg);
147 dbus_connection_unref(con->bus);