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