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