No need to log extra messages here.
[dhcpcd-ui] / src / libdhcpcd / dispatch.c
1 /*
2  * libdhcpcd
3  * Copyright 2009-2014 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 <string.h>
29
30 #define IN_LIBDHCPCD
31 #include "libdhcpcd.h"
32
33 static const char * const dhcpcd_types[] =
34     { "link", "ipv4", "ra", "dhcp6", NULL };
35
36 static const char *
37 dhcpcd_message_get_string(DHCPCD_MESSAGE *msg)
38 {
39         DBusMessageIter args;
40         char *str;
41
42         if (dbus_message_iter_init(msg, &args) &&
43             dbus_message_iter_get_arg_type(&args) == DBUS_TYPE_STRING)
44         {
45                 dbus_message_iter_get_basic(&args, &str);
46                 return str;
47         }
48         return NULL;
49 }
50
51 static void
52 dhcpcd_handle_event(DHCPCD_CONNECTION *con, DHCPCD_MESSAGE *msg)
53 {
54         DBusMessageIter args;
55         DHCPCD_IF *i, *e, *l, *n, *nl;
56         char *order, *o, *p;
57         int ti;
58
59         if (!dbus_message_iter_init(msg, &args))
60                 return;
61         order = NULL;
62         i = dhcpcd_if_new(con, &args, &order);
63         if (i == NULL)
64                 return;
65         p = order;
66         n = nl = NULL;
67
68         /* Remove all instances on carrier drop */
69         if (strcmp(i->reason, "NOCARRIER") == 0 ||
70             strcmp(i->reason, "DEPARTED") == 0 ||
71             strcmp(i->reason, "STOPPED") == 0)
72         {
73                 l = NULL;
74                 for (e = con->interfaces; e; e = n) {
75                         n = e->next;
76                         if (strcmp(e->ifname, i->ifname) == 0) {
77                                 if (strcmp(e->type, i->type) == 0)
78                                         l = nl = e;
79                                 else {
80                                         if (l)
81                                                 l->next = e->next;
82                                         else
83                                                 con->interfaces = e->next;
84                                         free(e);
85                                 }
86                         } else
87                                 l = e;
88                 }
89         }
90
91         /* Find our pointer */
92         if (nl == NULL) {
93                 l = NULL;
94                 for (e = con->interfaces; e; e = e->next) {
95                         if (strcmp(e->ifname, i->ifname) == 0 &&
96                             strcmp(e->type, i->type) == 0)
97                         {
98                                 nl = e;
99                                 break;
100                         }
101                         l = e;
102                 }
103         }
104         if (nl) {
105                 /* Preserve the pointer for wireless history */
106                 n = nl->next;
107                 memcpy(nl, i, sizeof(*i));
108                 nl->next = n;
109                 free(i);
110                 i = nl;
111         } else {
112                 /* Append it then */
113                 if (l)
114                         l->next = i;
115                 else
116                         con->interfaces = i;
117                 i->next = NULL;
118         }
119
120         /* Sort! */
121         n = nl = NULL;
122         while ((o = strsep(&p, " ")) != NULL) {
123                 for (ti = 0; dhcpcd_types[ti]; ti++) {
124                         l = NULL;
125                         for (e = con->interfaces; e; e = e->next) {
126                                 if (strcmp(e->ifname, o) == 0 &&
127                                     strcmp(e->type, dhcpcd_types[ti]) == 0)
128                                         break;
129                                 l = e;
130                         }
131                         if (e == NULL)
132                                 continue;
133                         if (l)
134                                 l->next = e->next;
135                         else
136                                 con->interfaces = e->next;
137                         e->next = NULL;
138                         if (nl == NULL)
139                                 n = nl = e;
140                         else {
141                                 nl->next = e;
142                                 nl = e;
143                         }
144                 }
145         }
146         /* Free any stragglers */
147         while (con->interfaces) {
148                 e = con->interfaces->next;
149                 free(con->interfaces);
150                 con->interfaces = e;
151         }
152         con->interfaces = n;
153
154         if (con->event)
155                 con->event(con, i, con->signal_data);
156 }
157
158 bool
159 dhcpcd_dispatch_message(DHCPCD_CONNECTION *con, DHCPCD_MESSAGE *msg)
160 {
161         bool handled;
162         const char *str;
163         DHCPCD_IF *ifp;
164
165         if (dbus_message_get_type(msg) != DBUS_MESSAGE_TYPE_SIGNAL)
166                 return false;
167
168         handled = true;
169         dbus_connection_ref(con->bus);
170         dbus_message_ref(msg);
171         if (dbus_message_is_signal(msg, DHCPCD_SERVICE, "StatusChanged")) {
172                 con->status = strdup(dhcpcd_message_get_string(msg));
173                 if (strcmp(con->status, "down") == 0) {
174                         dhcpcd_if_free(con->interfaces);
175                         con->interfaces = NULL;
176                 }
177                 if (con->status_changed)
178                         con->status_changed(con, con->status,
179                             con->signal_data);
180         }
181         else if (dbus_message_is_signal(msg, DHCPCD_SERVICE, "ScanResults"))
182         {
183                 if (con->wi_scanresults) {
184                         str = dhcpcd_message_get_string(msg);
185                         ifp = dhcpcd_if_find(con, str, "link");
186                         if (ifp)
187                                 con->wi_scanresults(con, ifp, con->signal_data);
188                 }
189         } else if (dbus_message_is_signal(msg, DHCPCD_SERVICE, "Event"))
190                 dhcpcd_handle_event(con, msg);
191         else
192                 handled = false;
193         dbus_message_unref(msg);
194         dbus_connection_unref(con->bus);
195         return handled;
196 }