Add guards when closing connections so that we can call the cb
[dhcpcd-ui] / src / libdhcpcd / dhcpcd.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 // For strverscmp(3)
28 #define _GNU_SOURCE
29
30 #include <sys/socket.h>
31 #include <sys/stat.h>
32 #include <sys/un.h>
33
34 #include <arpa/inet.h>
35
36 #include <assert.h>
37 #include <errno.h>
38 #include <fcntl.h>
39 #include <libintl.h>
40 #include <limits.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <unistd.h>
45
46 #define IN_LIBDHCPCD
47
48 #include "config.h"
49 #include "dhcpcd.h"
50
51 #ifdef HAS_GETTEXT
52 #include <libintl.h>
53 #define _ gettext
54 #else
55 #define _(a) (a)
56 #endif
57
58 #ifndef SUN_LEN
59 #define SUN_LEN(su) \
60         (sizeof(*(su)) - sizeof((su)->sun_path) + strlen((su)->sun_path))
61 #endif
62
63 static const char * const dhcpcd_types[] =
64     { "link", "ipv4", "ra", "dhcp6", NULL };
65
66 static ssize_t
67 dhcpcd_command_fd(DHCPCD_CONNECTION *con,
68     int fd, const char *cmd, char **buffer)
69 {
70         size_t len;
71         ssize_t bytes;
72         char buf[1024], *p;
73         char *nbuf;
74
75         /* Each command is \n terminated.
76          * Each argument is NULL seperated.
77          * We may need to send a space one day, so the API
78          * in this function may need to be improved */
79         len = strlen(cmd) + 1;
80         if (con->terminate_commands)
81                 len++;
82         if (len > sizeof(buf)) {
83                 errno = ENOBUFS;
84                 return -1;
85         }
86         strlcpy(buf, cmd, sizeof(buf));
87         p = buf;
88         while ((p = strchr(p, ' ')) != NULL)
89                 *p++ = '\0';
90         if (con->terminate_commands) {
91                 buf[len - 2] = '\n';
92                 buf[len - 1] = '\0';
93         } else
94                 buf[len - 1] = '\0';
95         if (write(fd, buf, len) == -1)
96                 return -1;
97         if (buffer == NULL)
98                 return 0;
99
100         bytes = read(fd, buf, sizeof(size_t));
101         if (bytes == 0 || bytes == -1)
102                 return bytes;
103         memcpy(&len, buf, sizeof(size_t));
104         nbuf = realloc(*buffer, len + 1);
105         if (nbuf == NULL)
106                 return -1;
107         *buffer = nbuf;
108         bytes = read(fd, *buffer, len);
109         if (bytes != -1 && (size_t)bytes < len)
110                 *buffer[bytes] = '\0';
111         return bytes;
112 }
113
114 ssize_t
115 dhcpcd_command(DHCPCD_CONNECTION *con, const char *cmd, char **buffer)
116 {
117
118         return dhcpcd_command_fd(con, con->command_fd, cmd, buffer);
119 }
120
121 bool
122 dhcpcd_realloc(DHCPCD_CONNECTION *con, size_t len)
123 {
124
125         if (con->buflen < len) {
126                 char *nbuf;
127
128                 nbuf = realloc(con->buf, len);
129                 if (nbuf == NULL)
130                         return false;
131                 con->buf = nbuf;
132                 con->buflen = len;
133         }
134         return true;
135 }
136
137 ssize_t
138 dhcpcd_command_arg(DHCPCD_CONNECTION *con, const char *cmd, const char *arg,
139     char **buffer)
140 {
141         size_t cmdlen, len;
142
143         cmdlen = strlen(cmd);
144         len = cmdlen + strlen(arg) + 2;
145         if (!dhcpcd_realloc(con, len))
146                 return -1;
147         strlcpy(con->buf, cmd, con->buflen);
148         con->buf[cmdlen] = ' ';
149         strlcpy(con->buf + cmdlen + 1, arg, con->buflen - 1 - cmdlen);
150
151         return dhcpcd_command_fd(con, con->command_fd, con->buf, buffer);
152 }
153
154
155 static int
156 dhcpcd_connect(void)
157 {
158         int fd;
159         socklen_t len;
160         struct sockaddr_un sun;
161
162         fd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0);
163         if (fd == -1)
164                 return -1;
165
166         memset(&sun, 0, sizeof(sun));
167         sun.sun_family = AF_UNIX;
168         strlcpy(sun.sun_path, DHCPCD_SOCKET, sizeof(sun.sun_path));
169         len = (socklen_t)SUN_LEN(&sun);
170         if (connect(fd, (struct sockaddr *)&sun, len) == 0)
171                 return fd;
172         close(fd);
173         return -1;
174 }
175
176 static const char *
177 get_value(const char *data, size_t len, const char *var)
178 {
179         const char *end, *p;
180         size_t vlen;
181
182         assert(var);
183         end = data + len;
184         vlen = strlen(var);
185         p = NULL;
186         while (data + vlen + 1 < end) {
187                 if (strncmp(data, var, vlen) == 0) {
188                         p = data + vlen;
189                         break;
190                 }
191                 data += strlen(data) + 1;
192         }
193         if (p != NULL && *p != '\0')
194                 return p;
195         return NULL;
196 }
197
198 const char *
199 dhcpcd_get_value(const DHCPCD_IF *i, const char *var)
200 {
201
202         assert(i);
203         return get_value(i->data, i->data_len, var);
204 }
205
206 const char *
207 dhcpcd_get_prefix_value(const DHCPCD_IF *i, const char *prefix, const char *var)
208 {
209         char pvar[128], *p;
210         size_t plen, l;
211
212         p = pvar;
213         plen = sizeof(pvar);
214         l = strlcpy(p, prefix, plen);
215         if (l >= sizeof(pvar)) {
216                 errno = ENOBUFS;
217                 return NULL;
218         }
219         p += l;
220         plen -= l;
221         if (strlcpy(p, var, plen) >= plen) {
222                 errno = ENOBUFS;
223                 return NULL;
224         }
225         return dhcpcd_get_value(i, pvar);
226 }
227
228 static bool
229 strtobool(const char *var)
230 {
231
232         if (var == NULL)
233                 return false;
234
235          return (*var == '0' || *var == '\0' ||
236             strcmp(var, "false") == 0 ||
237             strcmp(var, "no") == 0) ? false : true;
238 }
239
240 static const char *
241 get_status(DHCPCD_CONNECTION *con)
242 {
243         DHCPCD_IF *i;
244         const char *status;
245
246         assert(con);
247         if (con->command_fd == -1 || con->listen_fd == -1)
248                 return "down";
249
250         status = "disconnected";
251         for (i = con->interfaces; i; i = i->next) {
252                 if (i->up) {
253                         if (strcmp(i->type, "link")) {
254                                 status = "connected";
255                                 break;
256                         } else
257                                 status = "connecting";
258                 }
259         }
260         return status;
261 }
262
263 static void
264 update_status(DHCPCD_CONNECTION *con, const char *nstatus)
265 {
266
267         assert(con);
268         if (nstatus == NULL)
269                 nstatus = get_status(con);
270         if (con->status == NULL || strcmp(nstatus, con->status)) {
271                 con->status = nstatus;
272                 if (con->status_cb)
273                         con->status_cb(con, con->status, con->status_context);
274         }
275 }
276
277 DHCPCD_IF *
278 dhcpcd_interfaces(DHCPCD_CONNECTION *con)
279 {
280
281         assert(con);
282         return con->interfaces;
283 }
284
285 DHCPCD_IF *
286 dhcpcd_get_if(DHCPCD_CONNECTION *con, const char *ifname, const char *type)
287 {
288         DHCPCD_IF *i;
289
290         assert(con);
291         for (i = con->interfaces; i; i = i->next)
292                 if (strcmp(i->ifname, ifname) == 0 &&
293                     strcmp(i->type, type) == 0)
294                         return i;
295         return NULL;
296 }
297
298 static DHCPCD_IF *
299 dhcpcd_new_if(DHCPCD_CONNECTION *con, char *data, size_t len)
300 {
301         const char *ifname, *ifclass, *reason, *type, *order, *flags;
302         char *orderdup, *o, *p;
303         DHCPCD_IF *e, *i, *l, *n, *nl;
304         int ti;
305
306         ifname = get_value(data, len, "interface=");
307         if (ifname == NULL || *ifname == '\0') {
308                 errno = ESRCH;
309                 return NULL;
310         }
311         reason = get_value(data, len, "reason=");
312         if (reason == NULL || *reason == '\0') {
313                 errno = ESRCH;
314                 return NULL;
315         }
316         ifclass = get_value(data, len, "ifclass=");
317         /* Skip pseudo interfaces */
318         if (ifclass && *ifclass != '\0') {
319                 errno = ENOTSUP;
320                 return NULL;
321         }
322         if (strcmp(reason, "RECONFIGURE") == 0) {
323                 errno = ENOTSUP;
324                 return NULL;
325         }
326         order = get_value(data, len, "interface_order=");
327         if (order == NULL || *order == '\0') {
328                 errno = ESRCH;
329                 return NULL;
330         }
331
332         if (strcmp(reason, "PREINIT") == 0 ||
333             strcmp(reason, "UNKNOWN") == 0 ||
334             strcmp(reason, "CARRIER") == 0 ||
335             strcmp(reason, "NOCARRIER") == 0 ||
336             strcmp(reason, "DEPARTED") == 0 ||
337             strcmp(reason, "STOPPED") == 0)
338                 type = "link";
339         else if (strcmp(reason, "ROUTERADVERT") == 0)
340                 type = "ra";
341         else if (reason[strlen(reason) - 1] == '6')
342                 type = "dhcp6";
343         else
344                 type = "ipv4";
345
346         i = NULL;
347        /* Remove all instances on carrier drop */
348         if (strcmp(reason, "NOCARRIER") == 0 ||
349             strcmp(reason, "DEPARTED") == 0 ||
350             strcmp(reason, "STOPPED") == 0)
351         {
352                 l = NULL;
353                 for (e = con->interfaces; e; e = n) {
354                         n = e->next;
355                         if (strcmp(e->ifname, ifname) == 0) {
356                                 if (strcmp(e->type, type) == 0)
357                                         l = i = e;
358                                 else {
359                                         if (l)
360                                                 l->next = e->next;
361                                         else
362                                                 con->interfaces = e->next;
363                                         free(e);
364                                 }
365                         } else
366                                 l = e;
367                 }
368         } else if (strcmp(type, "link")) {
369                 /* If link is down, ignore it */
370                 e = dhcpcd_get_if(con, ifname, "link");
371                 if (e && strcmp(e->reason, "NOCARRIER") == 0)
372                         return NULL;
373         }
374
375         orderdup = strdup(order);
376         if (orderdup == NULL)
377                 return NULL;
378
379         /* Find our pointer */
380         if (i == NULL) {
381                 l = NULL;
382                 for (e = con->interfaces; e; e = e->next) {
383                         if (strcmp(e->ifname, ifname) == 0 &&
384                             strcmp(e->type, type) == 0)
385                         {
386                                 i = e;
387                                 break;
388                         }
389                         l = e;
390                 }
391         }
392         if (i == NULL) {
393                 i = malloc(sizeof(*i));
394                 if (i == NULL) {
395                         free(orderdup);
396                         return NULL;
397                 }
398                 if (l)
399                         l->next = i;
400                 else
401                         con->interfaces = i;
402                 i->next = NULL;
403                 i->last_message = NULL;
404         } else
405                 free(i->data);
406
407         /* Now fill out our interface structure */
408         i->con = con;
409         i->data = data;
410         i->data_len = len;
411         i->ifname = ifname;
412         i->type = type;
413         i->reason = reason;
414         flags = dhcpcd_get_value(i, "ifflags=");
415         if (flags)
416                 i->flags = (unsigned int)strtoul(flags, NULL, 0);
417         else
418                 i->flags = 0;
419         if (strcmp(reason, "CARRIER") == 0)
420                 i->up = true;
421         else
422                 i->up = strtobool(dhcpcd_get_value(i, "if_up="));
423         i->wireless = strtobool(dhcpcd_get_value(i, "ifwireless="));
424         i->ssid = dhcpcd_get_value(i, i->up ? "new_ssid=" : "old_ssid=");
425
426        /* Sort! */
427         n = nl = NULL;
428         p = orderdup;
429         while ((o = strsep(&p, " ")) != NULL) {
430                 for (ti = 0; dhcpcd_types[ti]; ti++) {
431                         l = NULL;
432                         for (e = con->interfaces; e; e = e->next) {
433                                 if (strcmp(e->ifname, o) == 0 &&
434                                     strcmp(e->type, dhcpcd_types[ti]) == 0)
435                                         break;
436                                 l = e;
437                         }
438                         if (e == NULL)
439                                 continue;
440                         if (l)
441                                 l->next = e->next;
442                         else
443                                 con->interfaces = e->next;
444                         e->next = NULL;
445                         if (nl == NULL)
446                                 n = nl = e;
447                         else {
448                                 nl->next = e;
449                                 nl = e;
450                         }
451                 }
452         }
453         free(orderdup);
454         /* Free any stragglers */
455         while (con->interfaces) {
456                 e = con->interfaces->next;
457                 free(con->interfaces->data);
458                 free(con->interfaces->last_message);
459                 free(con->interfaces);
460                 con->interfaces = e;
461         }
462         con->interfaces = n;
463
464         return i;
465 }
466
467 static DHCPCD_IF *
468 dhcpcd_read_if(DHCPCD_CONNECTION *con, int fd)
469 {
470         char sbuf[sizeof(size_t)], *rbuf;
471         size_t len;
472         ssize_t bytes;
473         DHCPCD_IF *i;
474
475         bytes = read(fd, sbuf, sizeof(sbuf));
476         if (bytes == 0 || bytes == -1) {
477                 dhcpcd_close(con);
478                 return NULL;
479         }
480         memcpy(&len, sbuf, sizeof(len));
481         rbuf = malloc(len + 1);
482         if (rbuf == NULL)
483                 return NULL;
484         bytes = read(fd, rbuf, len);
485         if (bytes == 0 || bytes == -1) {
486                 free(rbuf);
487                 dhcpcd_close(con);
488                 return NULL;
489         }
490         if ((size_t)bytes != len) {
491                 free(rbuf);
492                 errno = EINVAL;
493                 return NULL;
494         }
495         rbuf[bytes] = '\0';
496
497         i = dhcpcd_new_if(con, rbuf, len);
498         if (i == NULL)
499                 free(rbuf);
500         return i;
501 }
502
503 static void
504 dhcpcd_dispatchif(DHCPCD_IF *i)
505 {
506
507         assert(i);
508         if (i->con->if_cb)
509                 i->con->if_cb(i, i->con->if_context);
510         dhcpcd_wpa_if_event(i);
511 }
512
513 void
514 dhcpcd_dispatch(DHCPCD_CONNECTION *con)
515 {
516         DHCPCD_IF *i;
517
518         assert(con);
519         i = dhcpcd_read_if(con, con->listen_fd);
520         if (i)
521                 dhcpcd_dispatchif(i);
522
523         /* Have to call update_status last as it could
524          * cause the interface to be destroyed. */
525         update_status(con, NULL);
526 }
527
528 DHCPCD_CONNECTION *
529 dhcpcd_new(void)
530 {
531         DHCPCD_CONNECTION *con;
532
533         con = calloc(1, sizeof(*con));
534         con->command_fd = con->listen_fd = -1;
535         con->open = false;
536         return con;
537 }
538
539 #ifndef __GLIBC__
540 /* Good enough for our needs */
541 static int
542 strverscmp(const char *s1, const char *s2)
543 {
544         int s1maj, s1min, s1part;
545         int s2maj, s2min, s2part;
546         int r;
547
548         s1min = s1part = 0;
549         if (sscanf(s1, "%d.%d.%d", &s1maj, &s1min, &s1part) < 1)
550                 return -1;
551         s2min = s2part = 0;
552         if (sscanf(s2, "%d.%d.%d", &s2maj, &s2min, &s2part) < 1)
553                 return -1;
554         r = s1maj - s2maj;
555         if (r != 0)
556                 return r;
557         r = s1min - s2min;
558         if (r != 0)
559                 return r;
560         return s1part - s2part;
561 }
562 #endif
563
564 int
565 dhcpcd_open(DHCPCD_CONNECTION *con)
566 {
567         char cmd[128];
568         ssize_t bytes;
569         size_t nifs, n;
570         DHCPCD_IF *i;
571
572         assert(con);
573         if (con->listen_fd != -1) {
574                 if (!con->open) {
575                         errno = EISCONN;
576                         return -1;
577                 }
578                 return con->listen_fd;
579         }
580         con->command_fd = dhcpcd_connect();
581         if (con->command_fd == -1)
582                 goto err_exit;
583
584         con->terminate_commands = false;
585         if (dhcpcd_command(con, "--version", &con->version) <= 0)
586                 goto err_exit;
587         con->terminate_commands =
588             strverscmp(con->version, "6.4.1") >= 0 ? true : false;
589
590         if (dhcpcd_command(con, "--getconfigfile", &con->cffile) <= 0)
591                 goto err_exit;
592
593         con->listen_fd = dhcpcd_connect();
594         if (con->listen_fd == -1)
595                 goto err_exit;
596         dhcpcd_command_fd(con, con->listen_fd, "--listen", NULL);
597
598         dhcpcd_command_fd(con, con->command_fd, "--getinterfaces", NULL);
599         bytes = read(con->command_fd, cmd, sizeof(nifs));
600         if (bytes != sizeof(nifs))
601                 goto err_exit;
602
603         memcpy(&nifs, cmd, sizeof(nifs));
604         /* We don't dispatch each interface here as that
605          * causes too much notification spam when the GUI starts */
606         for (n = 0; n < nifs; n++) {
607                 i = dhcpcd_read_if(con, con->command_fd);
608                 if (i)
609                         dhcpcd_wpa_if_event(i);
610         }
611         update_status(con, NULL);
612         con->open = true;
613         return con->listen_fd;
614
615 err_exit:
616         dhcpcd_close(con);
617         return -1;
618 }
619
620 int
621 dhcpcd_get_fd(DHCPCD_CONNECTION *con)
622 {
623
624         assert(con);
625         return con->listen_fd;
626 }
627
628 const char *
629 dhcpcd_status(DHCPCD_CONNECTION *con)
630 {
631
632         assert(con);
633         return con->status;
634 }
635
636 const char *
637 dhcpcd_version(DHCPCD_CONNECTION *con)
638 {
639
640         assert(con);
641         return con->version;
642 }
643
644 const char *
645 dhcpcd_cffile(DHCPCD_CONNECTION *con)
646 {
647
648         assert(con);
649         return con->cffile;
650 }
651
652 void
653 dhcpcd_set_if_callback(DHCPCD_CONNECTION *con,
654     void (*cb)(DHCPCD_IF *, void *), void *ctx)
655 {
656
657         assert(con);
658         con->if_cb = cb;
659         con->if_context = ctx;
660 }
661
662 void
663 dhcpcd_set_status_callback(DHCPCD_CONNECTION *con,
664     void (*cb)(DHCPCD_CONNECTION *, const char *, void *), void *ctx)
665 {
666
667         assert(con);
668         con->status_cb = cb;
669         con->status_context = ctx;
670 }
671
672 void
673 dhcpcd_close(DHCPCD_CONNECTION *con)
674 {
675         DHCPCD_WPA *wpa;
676
677         assert(con);
678
679         con->open = false;
680
681         /* Shut down WPA listeners as they aren't much good without dhcpcd.
682          * They'll be restarted anyway when dhcpcd comes back up. */
683         for (wpa = con->wpa; wpa; wpa = wpa->next)
684                 dhcpcd_wpa_close(con->wpa);
685
686         if (con->command_fd != -1)
687                 shutdown(con->command_fd, SHUT_RDWR);
688         if (con->listen_fd != -1)
689                 shutdown(con->listen_fd, SHUT_RDWR);
690
691         update_status(con, "down");
692
693         if (con->command_fd != -1) {
694                 close(con->command_fd);
695                 con->command_fd = -1;
696         }
697         if (con->listen_fd != -1) {
698                 close(con->listen_fd);
699                 con->listen_fd = -1;
700         }
701
702         if (con->cffile) {
703                 free(con->cffile);
704                 con->cffile = NULL;
705         }
706         if (con->version) {
707                 free(con->version);
708                 con->version = NULL;
709         }
710         if (con->buf) {
711                 free(con->buf);
712                 con->buf = NULL;
713                 con->buflen = 0;
714         }
715 }
716
717 DHCPCD_CONNECTION *
718 dhcpcd_if_connection(DHCPCD_IF *i)
719 {
720
721         assert(i);
722         return i->con;
723 }
724
725 char *
726 dhcpcd_if_message(DHCPCD_IF *i, bool *new_msg)
727 {
728         const char *ip, *iplen, *pfx;
729         char *msg, *p;
730         const char *reason = NULL;
731         size_t len;
732         bool showssid;
733
734         assert(i);
735         /* Don't report non SLAAC configurations */
736         if (strcmp(i->type, "ra") == 0 && i->up &&
737             dhcpcd_get_value(i, "ra1_prefix=") == NULL)
738                 return NULL;
739
740         showssid = false;
741         if (strcmp(i->reason, "EXPIRE") == 0)
742                 reason = _("Expired");
743         else if (strcmp(i->reason, "CARRIER") == 0) {
744                 if (i->wireless) {
745                         showssid = true;
746                         reason = _("Associated with");
747                 } else
748                         reason = _("Cable plugged in");
749         } else if (strcmp(i->reason, "NOCARRIER") == 0) {
750                 if (i->wireless) {
751                         if (i->ssid) {
752                                 reason = _("Disassociated from");
753                                 showssid = true;
754                         } else
755                                 reason = _("Not associated");
756                 } else
757                         reason = _("Cable unplugged");
758         } else if (strcmp(i->reason, "UNKNOWN") == 0)
759                 reason = _("Unknown link state");
760         else if (strcmp(i->reason, "FAIL") == 0)
761                 reason = _("Automatic configuration not possible");
762         else if (strcmp(i->reason, "3RDPARTY") == 0)
763                 reason = _("Waiting for 3rd Party configuration");
764
765         if (reason == NULL) {
766                 if (i->up)
767                         reason = _("Configured");
768                 else if (strcmp(i->type, "ra") == 0)
769                         reason = "Expired RA";
770                 else
771                         reason = i->reason;
772         }
773
774         pfx = i->up ? "new_" : "old_";
775         if ((ip = dhcpcd_get_prefix_value(i, pfx, "ip_address=")))
776                 iplen = dhcpcd_get_prefix_value(i, pfx, "subnet_cidr=");
777         else if ((ip = dhcpcd_get_value(i, "ra1_prefix=")))
778                 iplen = NULL;
779         else if ((ip = dhcpcd_get_prefix_value(i, pfx,
780             "dhcp6_ia_na1_ia_addr1=")))
781                 iplen = "128";
782         else {
783                 ip = NULL;
784                 iplen = NULL;
785         }
786
787         len = strlen(i->ifname) + strlen(reason) + 3;
788         if (showssid && i->ssid)
789                 len += strlen(i->ssid) + 1;
790         if (ip)
791                 len += strlen(ip) + 1;
792         if (iplen)
793                 len += strlen(iplen) + 1;
794         msg = p = malloc(len);
795         if (msg == NULL)
796                 return NULL;
797         p += snprintf(msg, len, "%s: %s", i->ifname, reason);
798         if (showssid)
799                 p += snprintf(p, len - (size_t)(p - msg), " %s", i->ssid);
800         if (iplen)
801                 p += snprintf(p, len - (size_t)(p - msg), " %s/%s", ip, iplen);
802         else if (ip)
803                 p += snprintf(p, len - (size_t)(p - msg), " %s", ip);
804
805         if (new_msg) {
806                 if (i->last_message == NULL || strcmp(i->last_message, msg))
807                         *new_msg = true;
808                 else
809                         *new_msg = false;
810         }
811         free(i->last_message);
812         i->last_message = strdup(msg);
813
814         return msg;
815 }
816
817 void
818 dhcpcd_free(DHCPCD_CONNECTION *con)
819 {
820         DHCPCD_IF *nif;
821         DHCPCD_WPA *nwpa;
822         DHCPCD_WI_HIST *nh;
823
824         assert(con);
825         while (con->interfaces) {
826                 nif = con->interfaces->next;
827                 free(con->interfaces->data);
828                 free(con->interfaces->last_message);
829                 free(con->interfaces);
830                 con->interfaces = nif;
831         }
832         while (con->wpa) {
833                 nwpa = con->wpa->next;
834                 dhcpcd_wpa_close(con->wpa);
835                 free(con->wpa);
836                 con->wpa = nwpa;
837         }
838         while (con->wi_history) {
839                 nh = con->wi_history->next;
840                 free(con->wi_history);
841                 con->wi_history = nh;
842         }
843         free(con);
844 }