Split listening to WPA events out of dhcpcd_open as its
[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(int opts)
157 {
158         int fd;
159         socklen_t len;
160         struct sockaddr_un sun;
161
162         fd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | opts, 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)
248                 return "down";
249
250         if (con->listen_fd == -1)
251                 return "opened";
252
253         if (con->interfaces == NULL)
254                 return "initialised";
255
256         status = "disconnected";
257         for (i = con->interfaces; i; i = i->next) {
258                 if (i->up) {
259                         if (strcmp(i->type, "link")) {
260                                 status = "connected";
261                                 break;
262                         } else
263                                 status = "connecting";
264                 }
265         }
266         return status;
267 }
268
269 static void
270 update_status(DHCPCD_CONNECTION *con, const char *nstatus)
271 {
272
273         assert(con);
274         if (nstatus == NULL)
275                 nstatus = get_status(con);
276         if (con->status == NULL || strcmp(nstatus, con->status)) {
277                 con->status = nstatus;
278                 if (con->status_cb)
279                         con->status_cb(con, con->status, con->status_context);
280         }
281 }
282
283 DHCPCD_IF *
284 dhcpcd_interfaces(DHCPCD_CONNECTION *con)
285 {
286
287         assert(con);
288         return con->interfaces;
289 }
290
291 DHCPCD_IF *
292 dhcpcd_get_if(DHCPCD_CONNECTION *con, const char *ifname, const char *type)
293 {
294         DHCPCD_IF *i;
295
296         assert(con);
297         for (i = con->interfaces; i; i = i->next)
298                 if (strcmp(i->ifname, ifname) == 0 &&
299                     strcmp(i->type, type) == 0)
300                         return i;
301         return NULL;
302 }
303
304 static DHCPCD_IF *
305 dhcpcd_new_if(DHCPCD_CONNECTION *con, char *data, size_t len)
306 {
307         const char *ifname, *ifclass, *reason, *type, *order, *flags;
308         char *orderdup, *o, *p;
309         DHCPCD_IF *e, *i, *l, *n, *nl;
310         int ti;
311
312         ifname = get_value(data, len, "interface=");
313         if (ifname == NULL || *ifname == '\0') {
314                 errno = ESRCH;
315                 return NULL;
316         }
317         reason = get_value(data, len, "reason=");
318         if (reason == NULL || *reason == '\0') {
319                 errno = ESRCH;
320                 return NULL;
321         }
322         ifclass = get_value(data, len, "ifclass=");
323         /* Skip pseudo interfaces */
324         if (ifclass && *ifclass != '\0') {
325                 errno = ENOTSUP;
326                 return NULL;
327         }
328         if (strcmp(reason, "RECONFIGURE") == 0) {
329                 errno = ENOTSUP;
330                 return NULL;
331         }
332         order = get_value(data, len, "interface_order=");
333         if (order == NULL || *order == '\0') {
334                 errno = ESRCH;
335                 return NULL;
336         }
337
338         if (strcmp(reason, "PREINIT") == 0 ||
339             strcmp(reason, "UNKNOWN") == 0 ||
340             strcmp(reason, "CARRIER") == 0 ||
341             strcmp(reason, "NOCARRIER") == 0 ||
342             strcmp(reason, "DEPARTED") == 0 ||
343             strcmp(reason, "STOPPED") == 0)
344                 type = "link";
345         else if (strcmp(reason, "ROUTERADVERT") == 0)
346                 type = "ra";
347         else if (reason[strlen(reason) - 1] == '6')
348                 type = "dhcp6";
349         else
350                 type = "ipv4";
351
352         i = NULL;
353        /* Remove all instances on carrier drop */
354         if (strcmp(reason, "NOCARRIER") == 0 ||
355             strcmp(reason, "DEPARTED") == 0 ||
356             strcmp(reason, "STOPPED") == 0)
357         {
358                 l = NULL;
359                 for (e = con->interfaces; e; e = n) {
360                         n = e->next;
361                         if (strcmp(e->ifname, ifname) == 0) {
362                                 if (strcmp(e->type, type) == 0)
363                                         l = i = e;
364                                 else {
365                                         if (l)
366                                                 l->next = e->next;
367                                         else
368                                                 con->interfaces = e->next;
369                                         free(e);
370                                 }
371                         } else
372                                 l = e;
373                 }
374         } else if (strcmp(type, "link")) {
375                 /* If link is down, ignore it */
376                 e = dhcpcd_get_if(con, ifname, "link");
377                 if (e && strcmp(e->reason, "NOCARRIER") == 0)
378                         return NULL;
379         }
380
381         orderdup = strdup(order);
382         if (orderdup == NULL)
383                 return NULL;
384
385         /* Find our pointer */
386         if (i == NULL) {
387                 l = NULL;
388                 for (e = con->interfaces; e; e = e->next) {
389                         if (strcmp(e->ifname, ifname) == 0 &&
390                             strcmp(e->type, type) == 0)
391                         {
392                                 i = e;
393                                 break;
394                         }
395                         l = e;
396                 }
397         }
398         if (i == NULL) {
399                 i = malloc(sizeof(*i));
400                 if (i == NULL) {
401                         free(orderdup);
402                         return NULL;
403                 }
404                 if (l)
405                         l->next = i;
406                 else
407                         con->interfaces = i;
408                 i->next = NULL;
409                 i->last_message = NULL;
410         } else
411                 free(i->data);
412
413         /* Now fill out our interface structure */
414         i->con = con;
415         i->data = data;
416         i->data_len = len;
417         i->ifname = ifname;
418         i->type = type;
419         i->reason = reason;
420         flags = dhcpcd_get_value(i, "ifflags=");
421         if (flags)
422                 i->flags = (unsigned int)strtoul(flags, NULL, 0);
423         else
424                 i->flags = 0;
425         if (strcmp(reason, "CARRIER") == 0)
426                 i->up = true;
427         else
428                 i->up = strtobool(dhcpcd_get_value(i, "if_up="));
429         i->wireless = strtobool(dhcpcd_get_value(i, "ifwireless="));
430         i->ssid = dhcpcd_get_value(i, i->up ? "new_ssid=" : "old_ssid=");
431
432        /* Sort! */
433         n = nl = NULL;
434         p = orderdup;
435         while ((o = strsep(&p, " ")) != NULL) {
436                 for (ti = 0; dhcpcd_types[ti]; ti++) {
437                         l = NULL;
438                         for (e = con->interfaces; e; e = e->next) {
439                                 if (strcmp(e->ifname, o) == 0 &&
440                                     strcmp(e->type, dhcpcd_types[ti]) == 0)
441                                         break;
442                                 l = e;
443                         }
444                         if (e == NULL)
445                                 continue;
446                         if (l)
447                                 l->next = e->next;
448                         else
449                                 con->interfaces = e->next;
450                         e->next = NULL;
451                         if (nl == NULL)
452                                 n = nl = e;
453                         else {
454                                 nl->next = e;
455                                 nl = e;
456                         }
457                 }
458         }
459         free(orderdup);
460         /* Free any stragglers */
461         while (con->interfaces) {
462                 e = con->interfaces->next;
463                 free(con->interfaces->data);
464                 free(con->interfaces->last_message);
465                 free(con->interfaces);
466                 con->interfaces = e;
467         }
468         con->interfaces = n;
469
470         return i;
471 }
472
473 static DHCPCD_IF *
474 dhcpcd_read_if(DHCPCD_CONNECTION *con, int fd)
475 {
476         char sbuf[sizeof(size_t)], *rbuf;
477         size_t len;
478         ssize_t bytes;
479         DHCPCD_IF *i;
480
481         bytes = read(fd, sbuf, sizeof(sbuf));
482         if (bytes == 0 || bytes == -1) {
483                 dhcpcd_close(con);
484                 return NULL;
485         }
486         memcpy(&len, sbuf, sizeof(len));
487         rbuf = malloc(len + 1);
488         if (rbuf == NULL)
489                 return NULL;
490         bytes = read(fd, rbuf, len);
491         if (bytes == 0 || bytes == -1) {
492                 free(rbuf);
493                 dhcpcd_close(con);
494                 return NULL;
495         }
496         if ((size_t)bytes != len) {
497                 free(rbuf);
498                 errno = EINVAL;
499                 return NULL;
500         }
501         rbuf[bytes] = '\0';
502
503         i = dhcpcd_new_if(con, rbuf, len);
504         if (i == NULL)
505                 free(rbuf);
506         return i;
507 }
508
509 static void
510 dhcpcd_dispatchif(DHCPCD_IF *i)
511 {
512
513         assert(i);
514         if (i->con->if_cb)
515                 i->con->if_cb(i, i->con->if_context);
516         dhcpcd_wpa_if_event(i);
517 }
518
519 void
520 dhcpcd_dispatch(DHCPCD_CONNECTION *con)
521 {
522         DHCPCD_IF *i;
523
524         assert(con);
525         i = dhcpcd_read_if(con, con->listen_fd);
526         if (i)
527                 dhcpcd_dispatchif(i);
528
529         /* Have to call update_status last as it could
530          * cause the interface to be destroyed. */
531         update_status(con, NULL);
532 }
533
534 DHCPCD_CONNECTION *
535 dhcpcd_new(void)
536 {
537         DHCPCD_CONNECTION *con;
538
539         con = calloc(1, sizeof(*con));
540         con->command_fd = con->listen_fd = -1;
541         con->open = false;
542         return con;
543 }
544
545 #ifndef __GLIBC__
546 /* Good enough for our needs */
547 static int
548 strverscmp(const char *s1, const char *s2)
549 {
550         int s1maj, s1min, s1part;
551         int s2maj, s2min, s2part;
552         int r;
553
554         s1min = s1part = 0;
555         if (sscanf(s1, "%d.%d.%d", &s1maj, &s1min, &s1part) < 1)
556                 return -1;
557         s2min = s2part = 0;
558         if (sscanf(s2, "%d.%d.%d", &s2maj, &s2min, &s2part) < 1)
559                 return -1;
560         r = s1maj - s2maj;
561         if (r != 0)
562                 return r;
563         r = s1min - s2min;
564         if (r != 0)
565                 return r;
566         return s1part - s2part;
567 }
568 #endif
569
570 int
571 dhcpcd_open(DHCPCD_CONNECTION *con)
572 {
573         char cmd[128];
574         ssize_t bytes;
575         size_t nifs, n;
576         DHCPCD_IF *i;
577
578         assert(con);
579         if (con->open) {
580                 if (con->listen_fd != -1)
581                         return con->listen_fd;
582                 errno = EISCONN;
583                 return -1;
584         }
585         /* We need to block the command fd */
586         con->command_fd = dhcpcd_connect(0);
587         if (con->command_fd == -1)
588                 goto err_exit;
589
590         con->terminate_commands = false;
591         if (dhcpcd_command(con, "--version", &con->version) <= 0)
592                 goto err_exit;
593         con->terminate_commands =
594             strverscmp(con->version, "6.4.1") >= 0 ? true : false;
595
596         if (dhcpcd_command(con, "--getconfigfile", &con->cffile) <= 0)
597                 goto err_exit;
598
599         con->open = true;
600         update_status(con, NULL);
601
602         con->listen_fd = dhcpcd_connect(SOCK_NONBLOCK);
603         if (con->listen_fd == -1)
604                 goto err_exit;
605
606         dhcpcd_command_fd(con, con->listen_fd, "--listen", NULL);
607         dhcpcd_command_fd(con, con->command_fd, "--getinterfaces", NULL);
608         bytes = read(con->command_fd, cmd, sizeof(nifs));
609         if (bytes != sizeof(nifs))
610                 goto err_exit;
611         memcpy(&nifs, cmd, sizeof(nifs));
612         /* We don't dispatch each interface here as that
613          * causes too much notification spam when the GUI starts */
614         for (n = 0; n < nifs; n++)
615                 i = dhcpcd_read_if(con, con->command_fd);
616
617         update_status(con, NULL);
618
619         return con->listen_fd;
620
621 err_exit:
622         dhcpcd_close(con);
623         return -1;
624 }
625
626 int
627 dhcpcd_get_fd(DHCPCD_CONNECTION *con)
628 {
629
630         assert(con);
631         return con->listen_fd;
632 }
633
634 const char *
635 dhcpcd_status(DHCPCD_CONNECTION *con)
636 {
637
638         assert(con);
639         return con->status;
640 }
641
642 const char *
643 dhcpcd_version(DHCPCD_CONNECTION *con)
644 {
645
646         assert(con);
647         return con->version;
648 }
649
650 const char *
651 dhcpcd_cffile(DHCPCD_CONNECTION *con)
652 {
653
654         assert(con);
655         return con->cffile;
656 }
657
658 void
659 dhcpcd_set_if_callback(DHCPCD_CONNECTION *con,
660     void (*cb)(DHCPCD_IF *, void *), void *ctx)
661 {
662
663         assert(con);
664         con->if_cb = cb;
665         con->if_context = ctx;
666 }
667
668 void
669 dhcpcd_set_status_callback(DHCPCD_CONNECTION *con,
670     void (*cb)(DHCPCD_CONNECTION *, const char *, void *), void *ctx)
671 {
672
673         assert(con);
674         con->status_cb = cb;
675         con->status_context = ctx;
676 }
677
678 void
679 dhcpcd_close(DHCPCD_CONNECTION *con)
680 {
681         DHCPCD_WPA *wpa;
682
683         assert(con);
684
685         con->open = false;
686
687         /* Shut down WPA listeners as they aren't much good without dhcpcd.
688          * They'll be restarted anyway when dhcpcd comes back up. */
689         for (wpa = con->wpa; wpa; wpa = wpa->next)
690                 dhcpcd_wpa_close(con->wpa);
691
692         if (con->command_fd != -1)
693                 shutdown(con->command_fd, SHUT_RDWR);
694         if (con->listen_fd != -1)
695                 shutdown(con->listen_fd, SHUT_RDWR);
696
697         update_status(con, "down");
698
699         if (con->command_fd != -1) {
700                 close(con->command_fd);
701                 con->command_fd = -1;
702         }
703         if (con->listen_fd != -1) {
704                 close(con->listen_fd);
705                 con->listen_fd = -1;
706         }
707
708         if (con->cffile) {
709                 free(con->cffile);
710                 con->cffile = NULL;
711         }
712         if (con->version) {
713                 free(con->version);
714                 con->version = NULL;
715         }
716         if (con->buf) {
717                 free(con->buf);
718                 con->buf = NULL;
719                 con->buflen = 0;
720         }
721 }
722
723 DHCPCD_CONNECTION *
724 dhcpcd_if_connection(DHCPCD_IF *i)
725 {
726
727         assert(i);
728         return i->con;
729 }
730
731 char *
732 dhcpcd_if_message(DHCPCD_IF *i, bool *new_msg)
733 {
734         const char *ip, *iplen, *pfx;
735         char *msg, *p;
736         const char *reason = NULL;
737         size_t len;
738         bool showssid;
739
740         assert(i);
741         /* Don't report non SLAAC configurations */
742         if (strcmp(i->type, "ra") == 0 && i->up &&
743             dhcpcd_get_value(i, "ra1_prefix=") == NULL)
744                 return NULL;
745
746         showssid = false;
747         if (strcmp(i->reason, "EXPIRE") == 0)
748                 reason = _("Expired");
749         else if (strcmp(i->reason, "CARRIER") == 0) {
750                 if (i->wireless) {
751                         showssid = true;
752                         reason = _("Associated with");
753                 } else
754                         reason = _("Cable plugged in");
755         } else if (strcmp(i->reason, "NOCARRIER") == 0) {
756                 if (i->wireless) {
757                         if (i->ssid) {
758                                 reason = _("Disassociated from");
759                                 showssid = true;
760                         } else
761                                 reason = _("Not associated");
762                 } else
763                         reason = _("Cable unplugged");
764         } else if (strcmp(i->reason, "UNKNOWN") == 0)
765                 reason = _("Unknown link state");
766         else if (strcmp(i->reason, "FAIL") == 0)
767                 reason = _("Automatic configuration not possible");
768         else if (strcmp(i->reason, "3RDPARTY") == 0)
769                 reason = _("Waiting for 3rd Party configuration");
770
771         if (reason == NULL) {
772                 if (i->up)
773                         reason = _("Configured");
774                 else if (strcmp(i->type, "ra") == 0)
775                         reason = "Expired RA";
776                 else
777                         reason = i->reason;
778         }
779
780         pfx = i->up ? "new_" : "old_";
781         if ((ip = dhcpcd_get_prefix_value(i, pfx, "ip_address=")))
782                 iplen = dhcpcd_get_prefix_value(i, pfx, "subnet_cidr=");
783         else if ((ip = dhcpcd_get_value(i, "ra1_prefix=")))
784                 iplen = NULL;
785         else if ((ip = dhcpcd_get_prefix_value(i, pfx,
786             "dhcp6_ia_na1_ia_addr1=")))
787                 iplen = "128";
788         else {
789                 ip = NULL;
790                 iplen = NULL;
791         }
792
793         len = strlen(i->ifname) + strlen(reason) + 3;
794         if (showssid && i->ssid)
795                 len += strlen(i->ssid) + 1;
796         if (ip)
797                 len += strlen(ip) + 1;
798         if (iplen)
799                 len += strlen(iplen) + 1;
800         msg = p = malloc(len);
801         if (msg == NULL)
802                 return NULL;
803         p += snprintf(msg, len, "%s: %s", i->ifname, reason);
804         if (showssid)
805                 p += snprintf(p, len - (size_t)(p - msg), " %s", i->ssid);
806         if (iplen)
807                 p += snprintf(p, len - (size_t)(p - msg), " %s/%s", ip, iplen);
808         else if (ip)
809                 p += snprintf(p, len - (size_t)(p - msg), " %s", ip);
810
811         if (new_msg) {
812                 if (i->last_message == NULL || strcmp(i->last_message, msg))
813                         *new_msg = true;
814                 else
815                         *new_msg = false;
816         }
817         free(i->last_message);
818         i->last_message = strdup(msg);
819
820         return msg;
821 }
822
823 void
824 dhcpcd_free(DHCPCD_CONNECTION *con)
825 {
826         DHCPCD_IF *nif;
827         DHCPCD_WPA *nwpa;
828         DHCPCD_WI_HIST *nh;
829
830         assert(con);
831         while (con->interfaces) {
832                 nif = con->interfaces->next;
833                 free(con->interfaces->data);
834                 free(con->interfaces->last_message);
835                 free(con->interfaces);
836                 con->interfaces = nif;
837         }
838         while (con->wpa) {
839                 nwpa = con->wpa->next;
840                 dhcpcd_wpa_close(con->wpa);
841                 free(con->wpa);
842                 con->wpa = nwpa;
843         }
844         while (con->wi_history) {
845                 nh = con->wi_history->next;
846                 free(con->wi_history);
847                 con->wi_history = nh;
848         }
849         free(con);
850 }