3 * Copyright 2009-2014 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)
76 if (strcmp(i->ifname, o) != 0 ||
77 strcmp(i->type, types[ti]) == 0)
84 con->interfaces = e->next;
86 strcmp(e->ifname, i->ifname) == 0 &&
87 strcmp(e->type, i->type) == 0)
89 /* Preserve the pointer for
90 * our wireless history */
91 memcpy(e, i, sizeof(*e));
107 nl->next = con->interfaces;
111 con->event(con, i, con->signal_data);
115 dhcpcd_dispatch_message(DHCPCD_CONNECTION *con, DHCPCD_MESSAGE *msg)
121 if (dbus_message_get_type(msg) != DBUS_MESSAGE_TYPE_SIGNAL)
125 dbus_connection_ref(con->bus);
126 dbus_message_ref(msg);
127 if (dbus_message_is_signal(msg, DHCPCD_SERVICE, "StatusChanged")) {
128 con->status = strdup(dhcpcd_message_get_string(msg));
129 if (strcmp(con->status, "down") == 0) {
130 dhcpcd_if_free(con->interfaces);
131 con->interfaces = NULL;
133 if (con->status_changed)
134 con->status_changed(con, con->status,
137 else if (dbus_message_is_signal(msg, DHCPCD_SERVICE, "ScanResults"))
139 if (con->wi_scanresults) {
140 str = dhcpcd_message_get_string(msg);
141 ifp = dhcpcd_if_find(con, str, "ipv4");
143 con->wi_scanresults(con, ifp, con->signal_data);
145 } else if (dbus_message_is_signal(msg, DHCPCD_SERVICE, "Event"))
146 dhcpcd_handle_event(con, msg);
149 dbus_message_unref(msg);
150 dbus_connection_unref(con->bus);