Work with a seperate interface for Router Advertisements
[dhcpcd-ui] / src / libdhcpcd / dispatch.c
1 /*
2  * libdhcpcd
3  * Copyright 2009-2012 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                                 e = i;
77                         } else {
78                                 if (l != NULL)
79                                         l->next = e->next;
80                                 else
81                                         con->interfaces = e->next;
82                                 e->next = NULL;
83                                 if (i != NULL &&
84                                     strcmp(e->ifname, i->ifname) == 0 &&
85                                     strcmp(e->type, i->type) == 0)
86                                 {
87                                         /* Preserve the pointer for
88                                          * our wireless history */
89                                         memcpy(e, i, sizeof(*e));
90                                         free(i);
91                                         i = e;
92                                 }
93                         }
94                         if (nl == NULL)
95                                 n = nl = e;
96                         else {
97                                 nl->next = e;
98                                 nl = nl->next;
99                         }
100                 }
101         }
102
103         if (nl != NULL)
104                 nl->next = con->interfaces;
105         con->interfaces = n;
106
107         if (con->event)
108                 con->event(con, i, con->signal_data);
109 }
110
111 bool
112 dhcpcd_dispatch_message(DHCPCD_CONNECTION *con, DHCPCD_MESSAGE *msg)
113 {
114         bool handled;
115         const char *str;
116         DHCPCD_IF *ifp;
117
118         if (dbus_message_get_type(msg) != DBUS_MESSAGE_TYPE_SIGNAL)
119                 return false;
120
121         handled = true;
122         dbus_connection_ref(con->bus);
123         dbus_message_ref(msg);
124         if (dbus_message_is_signal(msg, DHCPCD_SERVICE, "StatusChanged")) {
125                 con->status = strdup(dhcpcd_message_get_string(msg));
126                 if (strcmp(con->status, "down") == 0) {
127                         dhcpcd_if_free(con->interfaces);
128                         con->interfaces = NULL;
129                 }
130                 if (con->status_changed)
131                         con->status_changed(con, con->status,
132                             con->signal_data);
133         }
134         else if (dbus_message_is_signal(msg, DHCPCD_SERVICE, "ScanResults"))
135         {
136                 if (con->wi_scanresults) {
137                         str = dhcpcd_message_get_string(msg);
138                         ifp = dhcpcd_if_find(con, str, "ipv4");
139                         if (ifp)
140                                 con->wi_scanresults(con, ifp, con->signal_data);
141                 }
142         } else if (dbus_message_is_signal(msg, DHCPCD_SERVICE, "Event"))
143                 dhcpcd_handle_event(con, msg);
144         else
145                 handled = false;
146         dbus_message_unref(msg);
147         dbus_connection_unref(con->bus);
148         return handled;
149 }