Use constants rather than string comparison for a saner API.
[dhcpcd-ui] / src / dhcpcd-gtk / main.c
1 /*
2  * dhcpcd-gtk
3  * Copyright 2009-2015 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 <locale.h>
29 #include <stdlib.h>
30 #include <string.h>
31
32 #ifdef NOTIFY
33 #  include <libnotify/notify.h>
34 #ifndef NOTIFY_CHECK_VERSION
35 #  define NOTIFY_CHECK_VERSION(a,b,c) 0
36 #endif
37 static NotifyNotification *nn;
38 #endif
39
40 #include "config.h"
41 #include "dhcpcd.h"
42 #include "dhcpcd-gtk.h"
43
44 static GtkStatusIcon *status_icon;
45 static guint ani_timer;
46 static int ani_counter;
47 static bool online;
48 static bool carrier;
49
50 struct watch {
51         gpointer ref;
52         int fd;
53         guint eventid;
54         GIOChannel *gio;
55         struct watch *next;
56 };
57 static struct watch *watches;
58
59 WI_SCANS wi_scans;
60
61 static gboolean dhcpcd_try_open(gpointer data);
62 static gboolean dhcpcd_wpa_try_open(gpointer data);
63
64 WI_SCAN *
65 wi_scan_find(DHCPCD_WI_SCAN *scan)
66 {
67         WI_SCAN *w;
68         DHCPCD_WI_SCAN *dw;
69
70         TAILQ_FOREACH(w, &wi_scans, next) {
71                 for (dw = w->scans; dw; dw = dw->next)
72                         if (dw == scan)
73                                 return w;
74         }
75         return NULL;
76 }
77
78 const char *
79 get_strength_icon_name(int strength)
80 {
81
82         if (strength > 80)
83                 return "network-wireless-connected-100";
84         else if (strength > 55)
85                 return "network-wireless-connected-75";
86         else if (strength > 30)
87                 return "network-wireless-connected-50";
88         else if (strength > 5)
89                 return "network-wireless-connected-25";
90         else
91                 return "network-wireless-connected-00";
92 }
93
94 static DHCPCD_WI_SCAN *
95 get_strongest_scan()
96 {
97         WI_SCAN *w;
98         DHCPCD_WI_SCAN *scan, *s;
99
100         scan = NULL;
101         TAILQ_FOREACH(w, &wi_scans, next) {
102                 for (s = w->scans; s; s = s->next) {
103                         if (dhcpcd_wi_associated(w->interface, s) &&
104                             (scan == NULL ||
105                             s->strength.value > scan->strength.value))
106                                 scan = s;
107                 }
108         }
109         return scan;
110 }
111
112 static gboolean
113 animate_carrier(_unused gpointer data)
114 {
115         const char *icon;
116         DHCPCD_WI_SCAN *scan;
117
118         if (ani_timer == 0)
119                 return false;
120
121         scan = get_strongest_scan();
122         if (scan) {
123                 switch(ani_counter++) {
124                 case 0:
125                         icon = "network-wireless-connected-00";
126                         break;
127                 case 1:
128                         icon = "network-wireless-connected-25";
129                         break;
130                 case 2:
131                         icon = "network-wireless-connected-50";
132                         break;
133                 case 3:
134                         icon = "network-wireless-connected-75";
135                         break;
136                 default:
137                         icon = "network-wireless-connected-100";
138                         ani_counter = 0;
139                 }
140
141         } else {
142                 switch(ani_counter++) {
143                 case 0:
144                         icon = "network-transmit";
145                         break;
146                 case 1:
147                         icon = "network-receive";
148                         break;
149                 default:
150                         icon = "network-idle";
151                         ani_counter = 0;
152                         break;
153                 }
154         }
155         gtk_status_icon_set_from_icon_name(status_icon, icon);
156         return true;
157 }
158
159 static gboolean
160 animate_online(_unused gpointer data)
161 {
162         const char *icon;
163         DHCPCD_WI_SCAN *scan;
164
165         if (ani_timer == 0)
166                 return false;
167
168         if (ani_counter++ > 6) {
169                 ani_timer = 0;
170                 ani_counter = 0;
171                 return false;
172         }
173
174         scan = get_strongest_scan();
175         if (ani_counter % 2 == 0)
176                 icon = scan ? "network-wireless-connected-00" :
177                     "network-idle";
178         else
179                 icon = scan ? get_strength_icon_name(scan->strength.value) :
180                     "network-transmit-receive";
181         gtk_status_icon_set_from_icon_name(status_icon, icon);
182         return true;
183 }
184
185 static void
186 update_online(DHCPCD_CONNECTION *con, bool showif)
187 {
188         bool ison, iscarrier;
189         char *msg, *msgs, *tmp;
190         DHCPCD_IF *ifs, *i;
191
192         ison = iscarrier = false;
193         msgs = NULL;
194         ifs = dhcpcd_interfaces(con);
195         for (i = ifs; i; i = i->next) {
196                 if (i->type == DHT_LINK) {
197                         if (i->up)
198                                 iscarrier = true;
199                 } else {
200                         if (i->up)
201                                 ison = true;
202                 }
203                 msg = dhcpcd_if_message(i, NULL);
204                 if (msg) {
205                         if (showif)
206                                 g_message("%s", msg);
207                         if (msgs) {
208                                 tmp = g_strconcat(msgs, "\n", msg, NULL);
209                                 g_free(msgs);
210                                 g_free(msg);
211                                 msgs = tmp;
212                         } else
213                                 msgs = msg;
214                 } else if (showif)
215                         g_message("%s: %s", i->ifname, i->reason);
216         }
217
218         if (online != ison || carrier != iscarrier) {
219                 online = ison;
220                 carrier = iscarrier;
221                 if (ani_timer != 0) {
222                         g_source_remove(ani_timer);
223                         ani_timer = 0;
224                         ani_counter = 0;
225                 }
226                 if (ison) {
227                         animate_online(NULL);
228                         ani_timer = g_timeout_add(300, animate_online, NULL);
229                 } else if (iscarrier) {
230                         animate_carrier(NULL);
231                         ani_timer = g_timeout_add(500, animate_carrier, NULL);
232                 } else {
233                         gtk_status_icon_set_from_icon_name(status_icon,
234                             "network-offline");
235                 }
236         } else {
237                 const char *icon;
238                 DHCPCD_WI_SCAN *scan;
239
240                 scan = get_strongest_scan();
241                 icon = scan ? get_strength_icon_name(scan->strength.value) :
242                     "network-transmit-receive";
243                 gtk_status_icon_set_from_icon_name(status_icon, icon);
244         }
245
246         gtk_status_icon_set_tooltip_text(status_icon, msgs);
247         g_free(msgs);
248 }
249
250 void
251 notify_close(void)
252 {
253 #ifdef NOTIFY
254         if (nn != NULL)
255                 notify_notification_close(nn, NULL);
256 #endif
257 }
258
259 #ifdef NOTIFY
260 static char *notify_last_msg;
261
262 static void
263 notify_closed(void)
264 {
265         nn = NULL;
266 }
267
268 static void
269 notify(const char *title, const char *msg, const char *icon)
270 {
271
272         if (msg == NULL)
273                 return;
274         /* Don't spam the same message */
275         if (notify_last_msg) {
276                 if (notify_last_msg && strcmp(msg, notify_last_msg) == 0)
277                         return;
278                 g_free(notify_last_msg);
279         }
280         notify_last_msg = g_strdup(msg);
281
282         if (nn != NULL)
283                 notify_notification_close(nn, NULL);
284
285 #if NOTIFY_CHECK_VERSION(0,7,0)
286         nn = notify_notification_new(title, msg, icon);
287         notify_notification_set_hint(nn, "transient",
288             g_variant_new_boolean(TRUE));
289 #else
290         if (gtk_status_icon_get_visible(status_icon))
291                 nn = notify_notification_new_with_status_icon(title,
292                     msg, icon, status_icon);
293         else
294                 nn = notify_notification_new(title, msg, icon, NULL);
295 #endif
296
297         notify_notification_set_timeout(nn, 5000);
298         g_signal_connect(nn, "closed", G_CALLBACK(notify_closed), NULL);
299         notify_notification_show(nn, NULL);
300 }
301 #else
302 #  define notify(a, b, c)
303 #endif
304
305 static struct watch *
306 dhcpcd_findwatch(int fd, gpointer data, struct watch **last)
307 {
308         struct watch *w;
309
310         if (last)
311                 *last = NULL;
312         for (w = watches; w; w = w->next) {
313                 if (w->fd == fd || w->ref == data)
314                         return w;
315                 if (last)
316                         *last = w;
317         }
318         return NULL;
319 }
320
321 static void
322 dhcpcd_unwatch(int fd, gpointer data)
323 {
324         struct watch *w, *l;
325
326         if ((w = dhcpcd_findwatch(fd, data, &l))) {
327                 if (l)
328                         l->next = w->next;
329                 else
330                         watches = w->next;
331                 g_source_remove(w->eventid);
332                 g_io_channel_unref(w->gio);
333                 g_free(w);
334         }
335 }
336
337 static gboolean
338 dhcpcd_watch(int fd,
339     gboolean (*cb)(GIOChannel *, GIOCondition, gpointer),
340     gpointer data)
341 {
342         struct watch *w, *l;
343         GIOChannel *gio;
344         GIOCondition flags;
345         guint eventid;
346
347         /* Sanity */
348         if ((w = dhcpcd_findwatch(fd, data, &l))) {
349                 if (w->fd == fd)
350                         return TRUE;
351                 if (l)
352                         l->next = w->next;
353                 else
354                         watches = w->next;
355                 g_source_remove(w->eventid);
356                 g_io_channel_unref(w->gio);
357                 g_free(w);
358         }
359
360         gio = g_io_channel_unix_new(fd);
361         if (gio == NULL) {
362                 g_warning(_("Error creating new GIO Channel\n"));
363                 return FALSE;
364         }
365         flags = G_IO_IN | G_IO_ERR | G_IO_HUP;
366         if ((eventid = g_io_add_watch(gio, flags, cb, data)) == 0) {
367                 g_warning(_("Error creating watch\n"));
368                 g_io_channel_unref(gio);
369                 return FALSE;
370         }
371
372         w = g_try_malloc(sizeof(*w));
373         if (w == NULL) {
374                 g_warning(_("g_try_malloc\n"));
375                 g_source_remove(eventid);
376                 g_io_channel_unref(gio);
377                 return FALSE;
378         }
379
380         w->ref = data;
381         w->fd = fd;
382         w->eventid = eventid;
383         w->gio = gio;
384         w->next = watches;
385         watches = w;
386
387         return TRUE;
388 }
389
390 static void
391 dhcpcd_status_cb(DHCPCD_CONNECTION *con,
392     unsigned int status, const char *status_msg, _unused void *data)
393 {
394         static unsigned int last = DHC_UNKNOWN;
395         const char *msg;
396         bool refresh;
397         WI_SCAN *w;
398
399         g_message("Status changed to %s", status_msg);
400         if (status == DHC_DOWN) {
401                 msg = N_(last == DHC_UNKNOWN ?
402                     "Connection to dhcpcd lost" : "dhcpcd not running");
403                 if (ani_timer != 0) {
404                         g_source_remove(ani_timer);
405                         ani_timer = 0;
406                         ani_counter = 0;
407                 }
408                 online = carrier = false;
409                 gtk_status_icon_set_from_icon_name(status_icon,
410                     "network-offline");
411                 gtk_status_icon_set_tooltip_text(status_icon, msg);
412                 prefs_abort();
413                 menu_abort();
414                 wpa_abort();
415                 while ((w = TAILQ_FIRST(&wi_scans))) {
416                         TAILQ_REMOVE(&wi_scans, w, next);
417                         dhcpcd_wi_scans_free(w->scans);
418                         g_free(w);
419                 }
420                 dhcpcd_unwatch(-1, con);
421                 g_timeout_add(DHCPCD_RETRYOPEN, dhcpcd_try_open, con);
422         } else {
423                 if (last == DHC_UNKNOWN || last == DHC_DOWN) {
424                         g_message(_("Connected to %s-%s"), "dhcpcd",
425                             dhcpcd_version(con));
426                         refresh = true;
427                 } else
428                         refresh = last == DHC_OPENED ? true : false;
429                 update_online(con, refresh);
430         }
431
432         last = status;
433 }
434
435 static gboolean
436 dhcpcd_cb(_unused GIOChannel *gio, _unused GIOCondition c, gpointer data)
437 {
438         DHCPCD_CONNECTION *con;
439
440         con = (DHCPCD_CONNECTION *)data;
441         if (dhcpcd_get_fd(con) == -1) {
442                 g_warning(_("dhcpcd connection lost"));
443                 dhcpcd_unwatch(-1, con);
444                 g_timeout_add(DHCPCD_RETRYOPEN, dhcpcd_try_open, con);
445                 return FALSE;
446         }
447
448         dhcpcd_dispatch(con);
449         return TRUE;
450 }
451
452 static gboolean
453 dhcpcd_try_open(gpointer data)
454 {
455         DHCPCD_CONNECTION *con;
456         int fd;
457         static int last_error;
458
459         con = (DHCPCD_CONNECTION *)data;
460         fd = dhcpcd_open(con, true);
461         if (fd == -1) {
462                 if (errno == EACCES || errno == EPERM) {
463                         if ((fd = dhcpcd_open(con, false)) != -1)
464                                 goto unprived;
465                 }
466                 if (errno != last_error) {
467                         g_critical("dhcpcd_open: %s", strerror(errno));
468                         last_error = errno;
469                 }
470                 return TRUE;
471         }
472
473 unprived:
474         if (!dhcpcd_watch(fd, dhcpcd_cb, con)) {
475                 dhcpcd_close(con);
476                 return TRUE;
477         }
478
479         /* Start listening to WPA events */
480         dhcpcd_wpa_start(con);
481
482         return FALSE;
483 }
484
485 static void
486 dhcpcd_if_cb(DHCPCD_IF *i, _unused void *data)
487 {
488         DHCPCD_CONNECTION *con;
489         char *msg;
490         const char *icon;
491         bool new_msg;
492
493         /* We should ignore renew and stop so we don't annoy the user */
494         if (i->state != DHS_RENEW &&
495             i->state != DHS_STOP && i->state != DHS_STOPPED)
496         {
497                 msg = dhcpcd_if_message(i, &new_msg);
498                 if (msg) {
499                         g_message("%s", msg);
500                         if (new_msg) {
501                                 if (i->up)
502                                         icon = "network-transmit-receive";
503                                 //else
504                                 //      icon = "network-transmit";
505                                 if (!i->up)
506                                         icon = "network-offline";
507                                 notify(_("Network event"), msg, icon);
508                         }
509                         g_free(msg);
510                 }
511         }
512
513         /* Update the tooltip with connection information */
514         con = dhcpcd_if_connection(i);
515         update_online(con, false);
516
517         if (i->wireless) {
518                 DHCPCD_WI_SCAN *scans;
519                 WI_SCAN *w;
520
521                 TAILQ_FOREACH(w, &wi_scans, next) {
522                         if (w->interface == i)
523                                 break;
524                 }
525                 if (w) {
526                         scans = dhcpcd_wi_scans(i);
527                         menu_update_scans(w, scans);
528                 }
529         }
530 }
531
532 static gboolean
533 dhcpcd_wpa_cb(_unused GIOChannel *gio, _unused GIOCondition c,
534     gpointer data)
535 {
536         DHCPCD_WPA *wpa;
537         DHCPCD_IF *i;
538
539         wpa = (DHCPCD_WPA *)data;
540         if (dhcpcd_wpa_get_fd(wpa) == -1) {
541                 dhcpcd_unwatch(-1, wpa);
542
543                 /* If the interface hasn't left, try re-opening */
544                 i = dhcpcd_wpa_if(wpa);
545                 if (i == NULL ||
546                     i->state == DHS_DEPARTED || i->state == DHS_STOPPED)
547                         return TRUE;
548                 g_warning(_("dhcpcd WPA connection lost: %s"), i->ifname);
549                 g_timeout_add(DHCPCD_RETRYOPEN, dhcpcd_wpa_try_open, wpa);
550                 return FALSE;
551         }
552
553         dhcpcd_wpa_dispatch(wpa);
554         return TRUE;
555 }
556
557 static gboolean
558 dhcpcd_wpa_try_open(gpointer data)
559 {
560         DHCPCD_WPA *wpa;
561         int fd;
562         static int last_error;
563
564         wpa = (DHCPCD_WPA *)data;
565         fd = dhcpcd_wpa_open(wpa);
566         if (fd == -1) {
567                 if (errno != last_error)
568                         g_critical("dhcpcd_wpa_open: %s", strerror(errno));
569                 last_error = errno;
570                 return TRUE;
571         }
572
573         if (!dhcpcd_watch(fd, dhcpcd_wpa_cb, wpa)) {
574                 dhcpcd_wpa_close(wpa);
575                 return TRUE;
576         }
577
578         return FALSE;
579 }
580
581 static void
582 dhcpcd_wpa_scan_cb(DHCPCD_WPA *wpa, _unused void *data)
583 {
584         DHCPCD_IF *i;
585         WI_SCAN *w;
586         DHCPCD_WI_SCAN *scans, *s1, *s2;
587         char *txt, *t;
588         int lerrno, fd;
589         const char *msg;
590
591         /* This could be a new WPA so watch it */
592         fd = dhcpcd_wpa_get_fd(wpa);
593         if (fd == -1) {
594                 g_critical("No fd for WPA %p", wpa);
595                 dhcpcd_unwatch(-1, wpa);
596                 return;
597         }
598         dhcpcd_watch(fd, dhcpcd_wpa_cb, wpa);
599
600         i = dhcpcd_wpa_if(wpa);
601         if (i == NULL) {
602                 g_critical("No interface for WPA %p", wpa);
603                 return;
604         }
605         g_message(_("%s: Received scan results"), i->ifname);
606         lerrno = errno;
607         errno = 0;
608         scans = dhcpcd_wi_scans(i);
609         if (scans == NULL && errno)
610                 g_warning("%s: %s", i->ifname, strerror(errno));
611         errno = lerrno;
612         TAILQ_FOREACH(w, &wi_scans, next) {
613                 if (w->interface == i)
614                         break;
615         }
616         if (w == NULL) {
617                 w = g_malloc(sizeof(*w));
618                 w->interface = i;
619                 w->scans = scans;
620                 w->ifmenu = NULL;
621                 TAILQ_INIT(&w->menus);
622                 TAILQ_INSERT_TAIL(&wi_scans, w, next);
623         } else {
624                 txt = NULL;
625                 msg = N_("New Access Point");
626                 for (s1 = scans; s1; s1 = s1->next) {
627                         for (s2 = w->scans; s2; s2 = s2->next)
628                                 if (g_strcmp0(s1->ssid, s2->ssid) == 0)
629                                         break;
630                         if (s2 == NULL) {
631                                 if (txt == NULL)
632                                         txt = g_strdup(s1->ssid);
633                                 else {
634                                         msg = N_("New Access Points");
635                                         t = g_strconcat(txt, "\n",
636                                             s1->ssid, NULL);
637                                         g_free(txt);
638                                         txt = t;
639                                 }
640                         }
641                 }
642                 if (txt) {
643                         notify(msg, txt, "network-wireless");
644                         g_free(txt);
645                 }
646                 menu_update_scans(w, scans);
647         }
648
649         if (!ani_timer) {
650                 s1 = get_strongest_scan();
651                 if (s1)
652                         msg = get_strength_icon_name(s1->strength.value);
653                 else if (online)
654                         msg = "network-transmit-receive";
655                 else
656                         msg = "network-offline";
657                 gtk_status_icon_set_from_icon_name(status_icon, msg);
658         }
659 }
660
661 static void
662 dhcpcd_wpa_status_cb(DHCPCD_WPA *wpa,
663     unsigned int status, const char *status_msg, _unused void *data)
664 {
665         DHCPCD_IF *i;
666         WI_SCAN *w, *wn;
667
668         i = dhcpcd_wpa_if(wpa);
669         g_message("%s: WPA status %s", i->ifname, status_msg);
670         if (status == DHC_DOWN) {
671                 dhcpcd_unwatch(-1, wpa);
672                 TAILQ_FOREACH_SAFE(w, &wi_scans, next, wn) {
673                         if (w->interface == i) {
674                                 TAILQ_REMOVE(&wi_scans, w, next);
675                                 menu_remove_if(w);
676                                 dhcpcd_wi_scans_free(w->scans);
677                                 g_free(w);
678                         }
679                 }
680         }
681 }
682
683 static gboolean
684 bgscan(gpointer data)
685 {
686         WI_SCAN *w;
687         DHCPCD_CONNECTION *con;
688         DHCPCD_WPA *wpa;
689
690         con = (DHCPCD_CONNECTION *)data;
691         TAILQ_FOREACH(w, &wi_scans, next) {
692                 if (w->interface->wireless) {
693                         wpa = dhcpcd_wpa_find(con, w->interface->ifname);
694                         if (wpa &&
695                             (!w->interface->up ||
696                             dhcpcd_wpa_can_background_scan(wpa)))
697                                 dhcpcd_wpa_scan(wpa);
698                 }
699         }
700
701         return TRUE;
702 }
703
704 int
705 main(int argc, char *argv[])
706 {
707         DHCPCD_CONNECTION *con;
708
709         setlocale(LC_ALL, "");
710         bindtextdomain(PACKAGE, NULL);
711         bind_textdomain_codeset(PACKAGE, "UTF-8");
712         textdomain(PACKAGE);
713
714         gtk_init(&argc, &argv);
715         g_set_application_name("Network Configurator");
716         gtk_icon_theme_append_search_path(gtk_icon_theme_get_default(),
717             ICONDIR);
718         status_icon = gtk_status_icon_new_from_icon_name("network-offline");
719
720         gtk_status_icon_set_tooltip_text(status_icon,
721             _("Connecting to dhcpcd ..."));
722         gtk_status_icon_set_visible(status_icon, true);
723         online = false;
724 #ifdef NOTIFY
725         notify_init(PACKAGE);
726 #endif
727
728         TAILQ_INIT(&wi_scans);
729         g_message(_("Connecting ..."));
730         con = dhcpcd_new();
731         if (con ==  NULL) {
732                 g_critical("libdhcpcd: %s", strerror(errno));
733                 exit(EXIT_FAILURE);
734         }
735         dhcpcd_set_progname(con, "dhcpcd-gtk");
736         dhcpcd_set_status_callback(con, dhcpcd_status_cb, NULL);
737         dhcpcd_set_if_callback(con, dhcpcd_if_cb, NULL);
738         dhcpcd_wpa_set_scan_callback(con, dhcpcd_wpa_scan_cb, NULL);
739         dhcpcd_wpa_set_status_callback(con, dhcpcd_wpa_status_cb, NULL);
740         if (dhcpcd_try_open(con))
741                 g_timeout_add(DHCPCD_RETRYOPEN, dhcpcd_try_open, con);
742
743         menu_init(status_icon, con);
744         g_timeout_add(DHCPCD_WPA_SCAN_LONG, bgscan, con);
745
746         gtk_main();
747         dhcpcd_close(con);
748         dhcpcd_free(con);
749         return 0;
750 }