Record a history of scan results so we can use an average for a more accurate display.
[dhcpcd-ui] / src / libdhcpcd / wpa.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 <errno.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31
32 #define IN_LIBDHCPCD
33 #include "libdhcpcd.h"
34
35 #define HIST_MAX 10 /* Max history per ssid/bssid */
36
37 void
38 dhcpcd_wi_history_clear(DHCPCD_CONNECTION *con)
39 {
40         DHCPCD_WI_HIST *h;
41
42         while (con->wi_history) {
43                 h = con->wi_history->next;
44                 free(con->wi_history);
45                 con->wi_history = h;
46         }
47 }
48
49 void
50 dhcpcd_wi_scans_free(DHCPCD_WI_SCAN *wis)
51 {
52         DHCPCD_WI_SCAN *n;
53
54         while (wis) {
55                 n = wis->next;
56                 free(wis);
57                 wis = n;
58         }
59 }
60
61 static DHCPCD_WI_SCAN *
62 dhcpcd_scanresult_new(DHCPCD_CONNECTION *con, DBusMessageIter *array)
63 {
64         DBusMessageIter dict, entry, var;
65         DHCPCD_WI_SCAN *wis;
66         char *s;
67         int32_t i32;
68         int errors;
69
70         wis = malloc(sizeof(*wis));
71         if (wis == NULL) {
72                 dhcpcd_error_set(con, NULL, errno);
73                 return NULL;
74         }
75         memset(wis, 0, sizeof(*wis));
76         errors = con->errors;
77         dbus_message_iter_recurse(array, &dict);
78         for (;
79              dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY;
80              dbus_message_iter_next(&dict))
81         {
82                 dbus_message_iter_recurse(&dict, &entry);
83                 if (!dhcpcd_iter_get(con, &entry, DBUS_TYPE_STRING, &s))
84                     break;
85                 if (dbus_message_iter_get_arg_type(&entry) !=
86                     DBUS_TYPE_VARIANT)
87                         break;
88                 dbus_message_iter_recurse(&entry, &var);
89                 if (strcmp(s, "BSSID") == 0) {
90                         if (!dhcpcd_iter_get(con, &var, DBUS_TYPE_STRING, &s))
91                                 break;
92                         strlcpy(wis->bssid, s, sizeof(wis->bssid));
93                 } else if (strcmp(s, "Frequency") == 0) {
94                         if (!dhcpcd_iter_get(con, &var, DBUS_TYPE_INT32, &i32))
95                                 break;
96                         wis->frequency = i32;
97                 } else if (strcmp(s, "Quality") == 0) {
98                         if (!dhcpcd_iter_get(con, &var, DBUS_TYPE_INT32, &i32))
99                                 break;
100                         wis->quality.value = i32;
101                 } else if (strcmp(s, "Noise") == 0) {
102                         if (!dhcpcd_iter_get(con, &var, DBUS_TYPE_INT32, &i32))
103                                 break;
104                         wis->noise.value = i32;
105                 } else if (strcmp(s, "Level") == 0) {
106                         if (!dhcpcd_iter_get(con, &var, DBUS_TYPE_INT32, &i32))
107                                 break;
108                         wis->level.value = i32;
109                 } else if (strcmp(s, "Flags") == 0) {
110                         if (!dhcpcd_iter_get(con, &var, DBUS_TYPE_STRING, &s))
111                                 break;
112                         strlcpy(wis->flags, s, sizeof(wis->flags));
113                 } else if (strcmp(s, "SSID") == 0) {
114                         if (!dhcpcd_iter_get(con, &var, DBUS_TYPE_STRING, &s))
115                                 break;
116                         strlcpy(wis->ssid, s, sizeof(wis->ssid));
117                 }
118         }
119         if (dbus_message_iter_get_arg_type(&dict) != DBUS_TYPE_INVALID) {
120                 if (con->errors == errors)
121                         dhcpcd_error_set(con, NULL, EINVAL);
122                 free(wis);
123                 return NULL;
124         }
125         return wis;
126 }
127
128 DHCPCD_WI_SCAN *
129 dhcpcd_wi_scans(DHCPCD_CONNECTION *con, DHCPCD_IF *i)
130 {
131         DBusMessage *msg;
132         DBusMessageIter args, array;
133         DHCPCD_WI_SCAN *wis, *scans, *l;
134         DHCPCD_WI_HIST *h, *hl;
135         int errors, nh;
136
137         msg = dhcpcd_message_reply(con, "ScanResults", i->ifname);
138         if (!dbus_message_iter_init(msg, &args) ||
139             dbus_message_iter_get_arg_type(&args) != DBUS_TYPE_ARRAY)
140         {
141                 dhcpcd_error_set(con, NULL, EINVAL);
142                 return NULL;
143         }
144
145         scans = l = NULL;
146         errors = con->errors;
147         dbus_message_iter_recurse(&args, &array);
148         for(;
149             dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_ARRAY;
150             dbus_message_iter_next(&array))
151         {
152                 wis = dhcpcd_scanresult_new(con, &array);
153                 if (wis == NULL)
154                         break;
155                 nh = 1;
156                 hl = NULL;
157                 wis->quality.average = wis->quality.value;
158                 wis->noise.average = wis->noise.value;
159                 wis->level.average = wis->level.value;
160                 for (h = con->wi_history; h; h = h->next) {
161                         if (strcmp(h->ifname, i->ifname) == 0 &&
162                             strcmp(h->bssid, wis->bssid) == 0)
163                         {
164                                 wis->quality.average += h->quality;
165                                 wis->noise.average += h->noise;
166                                 wis->level.average += h->level;
167                                 if (++nh == HIST_MAX) {
168                                         hl->next = h->next;
169                                         free(h);
170                                         break;
171                                 }
172                         }
173                         hl = h;
174                 }
175                 if (nh != 1) {
176                         wis->quality.average /= nh;
177                         wis->noise.average /= nh;
178                         wis->level.average /= nh;
179                 }
180                 h = malloc(sizeof(*h));
181                 if (h) {
182                         strlcpy(h->ifname, i->ifname, sizeof(h->ifname));
183                         strlcpy(h->bssid, wis->bssid, sizeof(h->bssid));
184                         h->quality = wis->quality.value;
185                         h->noise = wis->noise.value;
186                         h->level = wis->level.value;
187                         h->next = con->wi_history;
188                         con->wi_history = h;
189                 }
190                 if (l == NULL)
191                         scans = l = wis;
192                 else {
193                         l->next = wis;
194                         l = l->next;
195                 }
196         }
197         if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_INVALID) {
198                 if (con->errors == errors)
199                         dhcpcd_error_set(con, NULL, EINVAL);
200                 dhcpcd_wi_scans_free(scans);
201                 scans = NULL;
202         }
203         dbus_message_unref(msg);
204         return scans;
205 }
206
207 static int
208 dhcpcd_wpa_find_network(DHCPCD_CONNECTION *con, DHCPCD_IF *i, const char *ssid)
209 {
210         DBusMessage *msg;
211         DBusMessageIter args, array, entry;
212         int32_t id;
213         char *s;
214         int errors;
215
216         msg = dhcpcd_message_reply(con, "ListNetworks", i->ifname);
217         if (msg == NULL)
218                 return -1;
219         if (!dbus_message_iter_init(msg, &args)) {
220                 dhcpcd_error_set(con, NULL, EINVAL);
221                 return -1;
222         }
223
224         errors = con->errors;
225         for(;
226             dbus_message_iter_get_arg_type(&args) == DBUS_TYPE_ARRAY;
227             dbus_message_iter_next(&args))
228         {
229                 dbus_message_iter_recurse(&args, &array);
230                 for(;
231                     dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_STRUCT;
232                     dbus_message_iter_next(&array))
233                 {
234                         dbus_message_iter_recurse(&array, &entry);
235                         if (!dhcpcd_iter_get(con, &entry,
236                                 DBUS_TYPE_INT32, &id) ||
237                             !dhcpcd_iter_get(con, &entry,
238                                 DBUS_TYPE_STRING, &s))
239                                 break;
240                         if (strcmp(s, ssid) == 0) {
241                                 dbus_message_unref(msg);
242                                 return (int)id;
243                         }
244                 }
245         }
246         if (dbus_message_iter_get_arg_type(&args) != DBUS_TYPE_INVALID &&
247             con->errors == errors)
248                 dhcpcd_error_set(con, NULL, EINVAL);
249         dbus_message_unref(msg);
250         return -1;
251 }
252         
253 static int
254 dhcpcd_wpa_add_network(DHCPCD_CONNECTION *con, DHCPCD_IF *i)
255 {
256         DBusMessage *msg;
257         DBusMessageIter args;
258         int32_t id;
259         int ret;
260
261         msg = dhcpcd_message_reply(con, "AddNetwork", i->ifname);
262         if (msg == NULL)
263                 return -1;
264         ret = -1;
265         if (dbus_message_iter_init(msg, &args)) {
266                 if (dhcpcd_iter_get(con, &args, DBUS_TYPE_INT32, &id))
267                         ret = id;
268         } else
269                 dhcpcd_error_set(con, NULL, EINVAL);
270         dbus_message_unref(msg);
271         return ret;
272 }
273
274 bool
275 dhcpcd_wpa_set_network(DHCPCD_CONNECTION *con, DHCPCD_IF *i, int id,
276     const char *opt, const char *val)
277 {
278         DBusMessage *msg, *reply;
279         DBusMessageIter args;
280         bool retval;
281         char *ifname;
282
283         msg = dbus_message_new_method_call(DHCPCD_SERVICE, DHCPCD_PATH,
284             DHCPCD_SERVICE, "SetNetwork");
285         if (msg == NULL) {
286                 dhcpcd_error_set(con, 0, errno);
287                 return false;
288         }
289         dbus_message_iter_init_append(msg, &args);
290         ifname = i->ifname;
291         dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &ifname);
292         dbus_message_iter_append_basic(&args, DBUS_TYPE_INT32, &id);
293         dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &opt);
294         dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &val);
295         reply = dhcpcd_send_reply(con, msg);
296         dbus_message_unref(msg);
297         if (reply == NULL)
298                 retval = false;
299         else {
300                 dbus_message_unref(reply);
301                 retval = true;
302         }
303         return retval;
304 }
305
306 int
307 dhcpcd_wpa_find_network_new(DHCPCD_CONNECTION *con, DHCPCD_IF *i,
308     const char *ssid)
309 {
310         int errors, id;
311         char *q;
312         size_t len;
313         bool retval;
314
315         len = strlen(ssid) + 3;
316         q = malloc(len);
317         if (q == NULL) {
318                 dhcpcd_error_set(con, 0, errno);
319                 return -1;
320         }
321         errors = con->errors;
322         id = dhcpcd_wpa_find_network(con, i, ssid);
323         if (id != -1 || con->errors != errors) {
324                 free(q);
325                 return id;
326         }
327         id = dhcpcd_wpa_add_network(con, i);
328         if (id == -1) {
329                 free(q);
330                 return -1;
331         }
332         snprintf(q, len, "\"%s\"", ssid);
333         retval = dhcpcd_wpa_set_network(con, i, id, "ssid", q);
334         free(q);
335         return retval;
336 }
337
338 bool
339 dhcpcd_wpa_command(DHCPCD_CONNECTION *con, DHCPCD_IF *i,
340     const char *cmd, int id)
341 {
342         DBusMessage *msg, *reply;
343         DBusMessageIter args;
344         char *ifname;
345         bool retval;
346         
347         msg = dbus_message_new_method_call(DHCPCD_SERVICE, DHCPCD_PATH,
348             DHCPCD_SERVICE, cmd);
349         if (msg == NULL) {
350                 dhcpcd_error_set(con, 0, errno);
351                 return false;
352         }
353         dbus_message_iter_init_append(msg, &args);
354         ifname = i->ifname;
355         dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &ifname);
356         if (id != -1)
357                 dbus_message_iter_append_basic(&args, DBUS_TYPE_INT32, &id);
358         reply = dhcpcd_send_reply(con, msg);
359         dbus_message_unref(msg);
360         if (reply == NULL)
361                 retval = false;
362         else {
363                 dbus_message_unref(reply);
364                 retval = true;
365         }
366         return retval;
367 }