Bring over configure imporvements from dhcpcd.
[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 *
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         static const char *types[] = { "ipv4", "ra", NULL };
55         int ti;
56
57         if (!dbus_message_iter_init(msg, &args))
58                 return;
59         order = NULL;
60         i = dhcpcd_if_new(con, &args, &order);
61         if (i == NULL)
62                 return;
63         p = order;
64         n = nl = NULL;
65
66         while ((o = strsep(&p, " ")) != NULL) {
67                 for (ti = 0; ti < 2; ti++) {
68                         l = NULL;
69                         for (e = con->interfaces; e; e = e->next) {
70                                 if (strcmp(e->ifname, o) == 0 &&
71                                     strcmp(e->type, types[ti]) == 0)
72                                         break;
73                                 l = e;
74                         }
75                         if (e == NULL) {
76                                 if (strcmp(i->ifname, o) != 0 ||
77                                     strcmp(i->type, types[ti]) == 0)
78                                         continue;
79                                 e = i;
80                         } else {
81                                 if (l != NULL)
82                                         l->next = e->next;
83                                 else
84                                         con->interfaces = e->next;
85                                 if (i != NULL &&
86                                     strcmp(e->ifname, i->ifname) == 0 &&
87                                     strcmp(e->type, i->type) == 0)
88                                 {
89                                         /* Preserve the pointer for
90                                          * our wireless history */
91                                         memcpy(e, i, sizeof(*e));
92                                         free(i);
93                                         i = e;
94                                 }
95                                 e->next = NULL;
96                         }
97                         if (nl == NULL)
98                                 n = nl = e;
99                         else {
100                                 nl->next = e;
101                                 nl = nl->next;
102                         }
103                 }
104         }
105
106         if (nl != NULL)
107                 nl->next = con->interfaces;
108         con->interfaces = n;
109
110         if (con->event)
111                 con->event(con, i, con->signal_data);
112 }
113
114 bool
115 dhcpcd_dispatch_message(DHCPCD_CONNECTION *con, DHCPCD_MESSAGE *msg)
116 {
117         bool handled;
118         const char *str;
119         DHCPCD_IF *ifp;
120
121         if (dbus_message_get_type(msg) != DBUS_MESSAGE_TYPE_SIGNAL)
122                 return false;
123
124         handled = true;
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;
132                 }
133                 if (con->status_changed)
134                         con->status_changed(con, con->status,
135                             con->signal_data);
136         }
137         else if (dbus_message_is_signal(msg, DHCPCD_SERVICE, "ScanResults"))
138         {
139                 if (con->wi_scanresults) {
140                         str = dhcpcd_message_get_string(msg);
141                         ifp = dhcpcd_if_find(con, str, "ipv4");
142                         if (ifp)
143                                 con->wi_scanresults(con, ifp, con->signal_data);
144                 }
145         } else if (dbus_message_is_signal(msg, DHCPCD_SERVICE, "Event"))
146                 dhcpcd_handle_event(con, msg);
147         else
148                 handled = false;
149         dbus_message_unref(msg);
150         dbus_connection_unref(con->bus);
151         return handled;
152 }