f1b90de4aa5f0efabac40d5a32c514999c6fe26e
[dhcpcd-ui] / src / libdhcpcd / dispatch.c
1 /*
2  * libdhcpcd
3  * Copyright 2009 Roy Marples <roy@marples.name>
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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
24  * SUCH DAMAGE.
25  */
26
27 #include <stdlib.h>
28 #include <strings.h>
29
30 #define IN_LIBDHCPCD
31 #include "libdhcpcd.h"
32
33 static const char *
34 dhcpcd_message_get_string(DHCPCD_MESSAGE *msg)
35 {
36         DBusMessageIter args;
37         char *str;
38
39         if (dbus_message_iter_init(msg, &args) &&
40             dbus_message_iter_get_arg_type(&args) == DBUS_TYPE_STRING)
41         {
42                 dbus_message_iter_get_basic(&args, &str);
43                 return str;
44         }
45         return NULL;
46 }
47
48 static void
49 dhcpcd_handle_event(DHCPCD_CONNECTION *con, DHCPCD_MESSAGE *msg)
50 {
51         DBusMessageIter args;
52         DHCPCD_IF *i, *e, *l, *n, *nl;
53         char *order, *o, *p;
54
55         if (!dbus_message_iter_init(msg, &args))
56                 return;
57         order = NULL;
58         i = dhcpcd_if_new(con, &args, &order);
59         if (i == NULL)
60                 return;
61         p = order;
62         n = nl = NULL;
63         while ((o = strsep(&p, " ")) != NULL) {
64                 l = NULL;
65                 for (e = con->interfaces; e; e = e->next) {
66                         if (strcmp(e->ifname, o) == 0)
67                                 break;
68                         l = e;
69                 }
70                 if (e == NULL) {
71                         e = i;
72                 } else {
73                         if (l != NULL)
74                                 l->next = e->next;
75                         else
76                                 con->interfaces = e->next;
77                         e->next = NULL;
78                 }
79                 if (e != i && strcmp(e->ifname, i->ifname) == 0) {
80                         /* Preserve the pointer */
81                         memcpy(e, i, sizeof(*e));
82                         free(i);
83                         i = e;
84                 }
85                 if (nl == NULL)
86                         n = nl = e;
87                 else {
88                         nl->next = e;
89                         nl = nl->next;
90                 }
91         }
92         if (nl != NULL)
93                 nl->next = con->interfaces;
94         con->interfaces = n;
95         if (con->event)
96                 con->event(con, i, con->signal_data);
97 }
98
99 bool
100 dhcpcd_dispatch_message(DHCPCD_CONNECTION *con, DHCPCD_MESSAGE *msg)
101 {
102         bool handled;
103         const char *str;
104
105         if (dbus_message_get_type(msg) != DBUS_MESSAGE_TYPE_SIGNAL)
106                 return false;
107
108         handled = true;
109         dbus_connection_ref(con->bus);
110         dbus_message_ref(msg);
111         if (dbus_message_is_signal(msg, DHCPCD_SERVICE, "StatusChanged")) {
112                 con->status = strdup(dhcpcd_message_get_string(msg));
113                 if (strcmp(con->status, "down") == 0) {
114                         dhcpcd_if_free(con->interfaces);
115                         con->interfaces = NULL;
116                 }
117                 if (con->status_changed)
118                         con->status_changed(con, con->status,
119                             con->signal_data);
120         }
121         else if (dbus_message_is_signal(msg, DHCPCD_SERVICE, "ScanResults"))
122         {
123                 if (con->wi_scanresults) {
124                         str = dhcpcd_message_get_string(msg);
125                         con->wi_scanresults(con, dhcpcd_if_find(con, str),
126                             con->signal_data);
127                 }
128         } else if (dbus_message_is_signal(msg, DHCPCD_SERVICE, "Event"))
129                 dhcpcd_handle_event(con, msg);
130         else
131                 handled = false;
132         dbus_message_unref(msg);
133         dbus_connection_unref(con->bus);
134         return handled;
135 }