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