4b9b61e02b7f13c9936f8095d27838deb1e58879
[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 <ctype.h>
38 #include <errno.h>
39 #include <fcntl.h>
40 #include <libintl.h>
41 #include <limits.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <unistd.h>
46
47 #define IN_LIBDHCPCD
48
49 #include "config.h"
50 #include "dhcpcd.h"
51
52 #ifdef HAS_GETTEXT
53 #include <libintl.h>
54 #define _ gettext
55 #else
56 #define _(a) (a)
57 #endif
58
59 #ifdef HAVE_VIS_H
60 #include <vis.h>
61 #endif
62
63 #ifndef SUN_LEN
64 #define SUN_LEN(su) \
65         (sizeof(*(su)) - sizeof((su)->sun_path) + strlen((su)->sun_path))
66 #endif
67
68 #ifndef iswhite
69 #define iswhite(c)      (c == ' ' || c == '\t' || c == '\n')
70 #endif
71
72 static const char * const dhcpcd_types[] =
73     { "link", "ipv4", "ra", "dhcp6", NULL };
74
75 static ssize_t
76 dhcpcd_command_fd(DHCPCD_CONNECTION *con,
77     int fd, bool progname, const char *cmd, char **buffer)
78 {
79         size_t pl, cl, len;
80         ssize_t bytes;
81         char buf[1024], *p;
82         char *nbuf;
83
84         assert(con);
85         assert(cmd);
86
87         /* Each command is \n terminated.
88          * Each argument is NULL seperated.
89          * We may need to send a space one day, so the API
90          * in this function may need to be improved */
91         cl = strlen(cmd);
92         if (progname) {
93                 pl = strlen(con->progname);
94                 len = pl + 1 + cl + 1;
95         } else {
96                 pl = 0;
97                 len = cl + 1;
98         }
99         if (con->terminate_commands)
100                 len++;
101         if (len > sizeof(buf)) {
102                 errno = ENOBUFS;
103                 return -1;
104         }
105         p = buf;
106         if (progname) {
107                 memcpy(buf, con->progname, pl);
108                 buf[pl] = '\0';
109                 p = buf + pl + 1;
110         }
111         memcpy(p, cmd, cl);
112         p[cl] = '\0';
113         while ((p = strchr(p, ' ')) != NULL)
114                 *p++ = '\0';
115         if (con->terminate_commands) {
116                 buf[len - 2] = '\n';
117                 buf[len - 1] = '\0';
118         } else
119                 buf[len - 1] = '\0';
120         if (write(fd, buf, len) == -1)
121                 return -1;
122         if (buffer == NULL)
123                 return 0;
124
125         bytes = read(fd, buf, sizeof(size_t));
126         if (bytes == 0 || bytes == -1)
127                 return bytes;
128         memcpy(&len, buf, sizeof(size_t));
129         nbuf = realloc(*buffer, len + 1);
130         if (nbuf == NULL)
131                 return -1;
132         *buffer = nbuf;
133         bytes = read(fd, *buffer, len);
134         if (bytes != -1 && (size_t)bytes < len)
135                 *buffer[bytes] = '\0';
136         return bytes;
137 }
138
139 ssize_t
140 dhcpcd_command(DHCPCD_CONNECTION *con, const char *cmd, char **buffer)
141 {
142
143         assert(con);
144         if (!con->privileged) {
145                 errno = EACCES;
146                 return -1;
147         }
148         return dhcpcd_command_fd(con, con->command_fd, true, cmd, buffer);
149 }
150
151 static ssize_t
152 dhcpcd_ctrl_command(DHCPCD_CONNECTION *con, const char *cmd, char **buffer)
153 {
154
155         return dhcpcd_command_fd(con, con->command_fd, false, cmd, buffer);
156 }
157
158 bool
159 dhcpcd_realloc(DHCPCD_CONNECTION *con, size_t len)
160 {
161
162         assert(con);
163         if (con->buflen < len) {
164                 char *nbuf;
165
166                 nbuf = realloc(con->buf, len);
167                 if (nbuf == NULL)
168                         return false;
169                 con->buf = nbuf;
170                 con->buflen = len;
171         }
172         return true;
173 }
174
175 ssize_t
176 dhcpcd_command_arg(DHCPCD_CONNECTION *con, const char *cmd, const char *arg,
177     char **buffer)
178 {
179         size_t cmdlen, len;
180
181         assert(con);
182         assert(cmd);
183
184         cmdlen = strlen(cmd);
185         if (arg)
186                 len = cmdlen + strlen(arg) + 2;
187         else
188                 len = cmdlen + 1;
189         if (!dhcpcd_realloc(con, len))
190                 return -1;
191         strlcpy(con->buf, cmd, con->buflen);
192         if (arg) {
193                 con->buf[cmdlen] = ' ';
194                 strlcpy(con->buf + cmdlen + 1, arg, con->buflen - 1 - cmdlen);
195         }
196
197         return dhcpcd_command_fd(con, con->command_fd, true, con->buf, buffer);
198 }
199
200
201 static int
202 dhcpcd_connect(const char *path, int opts)
203 {
204         int fd;
205         socklen_t len;
206         struct sockaddr_un sun;
207
208         assert(path);
209         fd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | opts, 0);
210         if (fd == -1)
211                 return -1;
212
213         memset(&sun, 0, sizeof(sun));
214         sun.sun_family = AF_UNIX;
215         strlcpy(sun.sun_path, path, sizeof(sun.sun_path));
216         len = (socklen_t)SUN_LEN(&sun);
217         if (connect(fd, (struct sockaddr *)&sun, len) == 0)
218                 return fd;
219         close(fd);
220         return -1;
221 }
222
223 static const char *
224 get_value(const char *data, size_t len, const char *var)
225 {
226         const char *end, *p;
227         size_t vlen;
228
229         assert(var);
230         end = data + len;
231         vlen = strlen(var);
232         p = NULL;
233         while (data + vlen + 1 < end) {
234                 /* Skip past NUL padding */
235                 if (*data == '\0') {
236                         data++;
237                         continue;
238                 }
239                 if (strncmp(data, var, vlen) == 0 && data[vlen] == '=') {
240                         p = data + vlen + 1;
241                         break;
242                 }
243                 data += strlen(data) + 1;
244         }
245         if (p != NULL && *p != '\0')
246                 return p;
247         return NULL;
248 }
249
250 const char *
251 dhcpcd_get_value(const DHCPCD_IF *i, const char *var)
252 {
253
254         assert(i);
255         assert(var);
256         return get_value(i->data, i->data_len, var);
257 }
258
259 ssize_t
260 dhcpcd_encode_string_escape(char *dst, size_t len, const char *src, size_t slen)
261 {
262         const char *end;
263         size_t bytes;
264         int c;
265
266         end = src + slen;
267         bytes = 0;
268         while (src < end) {
269                 c = *src++;
270                 if ((c == '\\' || !isascii(c) || !isprint(c))) {
271                         if (c == '\\') {
272                                 if (dst) {
273                                         if (len  == 0 || len == 1) {
274                                                 errno = ENOSPC;
275                                                 return -1;
276                                         }
277                                         *dst++ = '\\'; *dst++ = '\\';
278                                         len -= 2;
279                                 }
280                                 bytes += 2;
281                                 continue;
282                         }
283                         if (dst) {
284                                 if (len < 5) {
285                                         errno = ENOSPC;
286                                         return -1;
287                                 }
288                                 *dst++ = '\\';
289                                 *dst++ = (((unsigned char)c >> 6) & 03) + '0';
290                                 *dst++ = (((unsigned char)c >> 3) & 07) + '0';
291                                 *dst++ = ( (unsigned char)c       & 07) + '0';
292                                 len -= 4;
293                         }
294                         bytes += 4;
295                 } else {
296                         if (dst) {
297                                 if (len == 0) {
298                                         errno = ENOSPC;
299                                         return -1;
300                                 }
301                                 *dst++ = (char)c;
302                                 len--;
303                         }
304                         bytes++;
305                 }
306         }
307
308         if (dst) {
309                 if (len == 0) {
310                         errno = ENOSPC;
311                         return -1;
312                 }
313                 *dst = '\0';
314         }
315
316         return (ssize_t)bytes;
317 }
318
319 ssize_t
320 dhcpcd_decode_string_escape(char *dst, size_t dlen, const char *src)
321 {
322         char c, esc;
323         int oct;
324         ssize_t bytes;
325
326         bytes = 0;
327         for (;;) {
328                 c = *src++;
329                 if (c == '\0')
330                         break;
331                 if (dst && --dlen == 0) {
332                         errno = ENOSPC;
333                         return -1;
334                 }
335                 switch (c) {
336                 case '\\':
337                         if (*src == '\0') {
338                                 errno = EINVAL;
339                                 return -1;
340                         }
341                         esc = *src++;
342                         switch (esc) {
343                         case '\\':
344                         case '0':
345                         case '1':
346                         case '3':
347                         case '4':
348                         case '5':
349                         case '6':
350                         case '7':
351                                 oct = esc - '0';
352                                 if (*src >= '0' && *src <='7')
353                                         oct = oct * 8 + (*src++ - '0');
354                                 else {
355                                         errno = EINVAL;
356                                         return -1;
357                                 }
358                                 if (*src >= '0' && *src <='7')
359                                         oct = oct * 8 + (*src++ - '0');
360                                 else {
361                                         errno = EINVAL;
362                                         return -1;
363                                 }
364                                 if (dst)
365                                         *dst++ = (char)oct;
366                         default:
367                                 errno = EINVAL;
368                                 return -1;
369                         }
370                         break;
371                 default:
372                         if (dst)
373                                 *dst++ = c;
374                 }
375                 bytes++;
376         }
377
378         if (dst) {
379                 if (--dlen == 0) {
380                         errno = ENOSPC;
381                         return -1;
382                 }
383                 *dst = '\0';
384         }
385         return bytes;
386 }
387
388 ssize_t
389 dhcpcd_decode_hex(char *dst, size_t dlen, const char *src)
390 {
391         size_t bytes, i;
392         char c;
393         int val, n;
394
395         bytes = 0;
396         while (*src) {
397                 if (dlen == 0 || dlen == 1) {
398                         errno = ENOSPC;
399                         return -1;
400                 }
401                 val = 0;
402                 for (i = 0; i < 2; i++) {
403                         c = *src++;
404                         if (c >= '0' && c <= '9')
405                                 n = c - '0';
406                         else if (c >= 'a' && c <= 'f')
407                                 n = 10 + c - 'a';
408                         else if (c >= 'A' && c <= 'F')
409                                 n = 10 + c - 'A';
410                         else {
411                                 errno = EINVAL;
412                                 return -1;
413                         }
414                         val = val * 16 + n;
415                 }
416                 *dst++ = (char)val;
417                 bytes += 2;
418                 dlen -= 2;
419                 if (*src == ':')
420                         src++;
421         }
422         return (ssize_t)bytes;
423 }
424
425 const char *
426 dhcpcd_get_prefix_value(const DHCPCD_IF *i, const char *prefix, const char *var)
427 {
428         char pvar[128], *p;
429         size_t plen, l;
430
431         assert(i);
432         assert(prefix);
433         assert(var);
434
435         p = pvar;
436         plen = sizeof(pvar);
437         l = strlcpy(p, prefix, plen);
438         if (l >= sizeof(pvar)) {
439                 errno = ENOBUFS;
440                 return NULL;
441         }
442         p += l;
443         plen -= l;
444         if (strlcpy(p, var, plen) >= plen) {
445                 errno = ENOBUFS;
446                 return NULL;
447         }
448         return dhcpcd_get_value(i, pvar);
449 }
450
451 static bool
452 strtobool(const char *var)
453 {
454
455         if (var == NULL)
456                 return false;
457
458          return (*var == '0' || *var == '\0' ||
459             strcmp(var, "false") == 0 ||
460             strcmp(var, "no") == 0) ? false : true;
461 }
462
463 static const char *
464 get_status(DHCPCD_CONNECTION *con)
465 {
466         DHCPCD_IF *i;
467         const char *status;
468
469         assert(con);
470         if (con->command_fd == -1)
471                 return "down";
472
473         if (con->listen_fd == -1)
474                 return "opened";
475
476         if (con->interfaces == NULL)
477                 return "initialised";
478
479         status = "disconnected";
480         for (i = con->interfaces; i; i = i->next) {
481                 if (i->up) {
482                         if (strcmp(i->type, "link")) {
483                                 status = "connected";
484                                 break;
485                         } else
486                                 status = "connecting";
487                 }
488         }
489         return status;
490 }
491
492 static void
493 update_status(DHCPCD_CONNECTION *con, const char *nstatus)
494 {
495
496         assert(con);
497         if (nstatus == NULL)
498                 nstatus = get_status(con);
499         if (con->status == NULL || strcmp(nstatus, con->status)) {
500                 con->status = nstatus;
501                 if (con->status_cb)
502                         con->status_cb(con, con->status, con->status_context);
503         }
504 }
505
506 DHCPCD_IF *
507 dhcpcd_interfaces(DHCPCD_CONNECTION *con)
508 {
509
510         assert(con);
511         return con->interfaces;
512 }
513
514 DHCPCD_IF *
515 dhcpcd_get_if(DHCPCD_CONNECTION *con, const char *ifname, const char *type)
516 {
517         DHCPCD_IF *i;
518
519         assert(con);
520         assert(ifname);
521         assert(type);
522
523         for (i = con->interfaces; i; i = i->next)
524                 if (strcmp(i->ifname, ifname) == 0 &&
525                     strcmp(i->type, type) == 0)
526                         return i;
527         return NULL;
528 }
529
530 static DHCPCD_IF *
531 dhcpcd_new_if(DHCPCD_CONNECTION *con, char *data, size_t len)
532 {
533         const char *ifname, *ifclass, *reason, *type, *order, *flags;
534         char *orderdup, *o, *p;
535         DHCPCD_IF *e, *i, *l, *n, *nl;
536         int ti;
537         bool addedi;
538
539 #if 0
540         char *dp = data, *de = data + len;
541         while (dp < de) {
542                 printf ("XX: %s\n", dp);
543                 dp += strlen(dp) + 1;
544         }
545 #endif
546
547         ifname = get_value(data, len, "interface");
548         if (ifname == NULL || *ifname == '\0') {
549                 errno = ESRCH;
550                 return NULL;
551         }
552         reason = get_value(data, len, "reason");
553         if (reason == NULL || *reason == '\0') {
554                 errno = ESRCH;
555                 return NULL;
556         }
557         ifclass = get_value(data, len, "ifclass");
558         /* Skip pseudo interfaces */
559         if (ifclass && *ifclass != '\0') {
560                 errno = ENOTSUP;
561                 return NULL;
562         }
563         if (strcmp(reason, "RECONFIGURE") == 0 ||
564             strcmp(reason, "INFORM") == 0 || strcmp(reason, "INFORM6") == 0)
565         {
566                 errno = ENOTSUP;
567                 return NULL;
568         }
569         order = get_value(data, len, "interface_order");
570         if (order == NULL || *order == '\0') {
571                 errno = ESRCH;
572                 return NULL;
573         }
574
575         if (strcmp(reason, "PREINIT") == 0 ||
576             strcmp(reason, "UNKNOWN") == 0 ||
577             strcmp(reason, "CARRIER") == 0 ||
578             strcmp(reason, "NOCARRIER") == 0 ||
579             strcmp(reason, "DEPARTED") == 0 ||
580             strcmp(reason, "STOPPED") == 0)
581                 type = "link";
582         else if (strcmp(reason, "ROUTERADVERT") == 0)
583                 type = "ra";
584         else if (reason[strlen(reason) - 1] == '6')
585                 type = "dhcp6";
586         else
587                 type = "ipv4";
588
589         i = NULL;
590        /* Remove all instances on carrier drop */
591         if (strcmp(reason, "NOCARRIER") == 0 ||
592             strcmp(reason, "DEPARTED") == 0 ||
593             strcmp(reason, "STOPPED") == 0)
594         {
595                 l = NULL;
596                 for (e = con->interfaces; e; e = n) {
597                         n = e->next;
598                         if (strcmp(e->ifname, ifname) == 0) {
599                                 if (strcmp(e->type, type) == 0)
600                                         l = i = e;
601                                 else {
602                                         if (l)
603                                                 l->next = e->next;
604                                         else
605                                                 con->interfaces = e->next;
606                                         free(e);
607                                 }
608                         } else
609                                 l = e;
610                 }
611         } else if (strcmp(type, "link")) {
612                 /* If link is down, ignore it */
613                 e = dhcpcd_get_if(con, ifname, "link");
614                 if (e && !e->up)
615                         return NULL;
616         }
617
618         orderdup = strdup(order);
619         if (orderdup == NULL)
620                 return NULL;
621
622         /* Find our pointer */
623         if (i == NULL) {
624                 l = NULL;
625                 for (e = con->interfaces; e; e = e->next) {
626                         if (strcmp(e->ifname, ifname) == 0 &&
627                             strcmp(e->type, type) == 0)
628                         {
629                                 i = e;
630                                 break;
631                         }
632                         l = e;
633                 }
634         }
635         if (i == NULL) {
636                 i = malloc(sizeof(*i));
637                 if (i == NULL) {
638                         free(orderdup);
639                         return NULL;
640                 }
641                 if (l)
642                         l->next = i;
643                 else
644                         con->interfaces = i;
645                 i->next = NULL;
646                 i->last_message = NULL;
647         } else
648                 free(i->data);
649
650         /* Now fill out our interface structure */
651         i->con = con;
652         i->data = data;
653         i->data_len = len;
654         i->ifname = ifname;
655         i->type = type;
656         i->reason = reason;
657         flags = dhcpcd_get_value(i, "ifflags");
658         if (flags)
659                 i->flags = (unsigned int)strtoul(flags, NULL, 0);
660         else
661                 i->flags = 0;
662         if (strcmp(reason, "CARRIER") == 0 ||
663             strcmp(reason, "DELEGATED6") == 0)
664                 i->up = true;
665         else
666                 i->up = strtobool(dhcpcd_get_value(i, "if_up"));
667         i->wireless = strtobool(dhcpcd_get_value(i, "ifwireless"));
668         i->ssid = dhcpcd_get_value(i, "ifssid");
669         if (i->ssid == NULL && i->wireless)
670                 i->ssid = dhcpcd_get_value(i, i->up ? "new_ssid" : "old_ssid");
671
672        /* Sort! */
673         n = nl = NULL;
674         p = orderdup;
675         addedi = false;
676         while ((o = strsep(&p, " ")) != NULL) {
677                 for (ti = 0; dhcpcd_types[ti]; ti++) {
678                         l = NULL;
679                         for (e = con->interfaces; e; e = e->next) {
680                                 if (strcmp(e->ifname, o) == 0 &&
681                                     strcmp(e->type, dhcpcd_types[ti]) == 0)
682                                         break;
683                                 l = e;
684                         }
685                         if (e == NULL)
686                                 continue;
687                         if (i == e)
688                                 addedi = true;
689                         if (l)
690                                 l->next = e->next;
691                         else
692                                 con->interfaces = e->next;
693                         e->next = NULL;
694                         if (nl == NULL)
695                                 n = nl = e;
696                         else {
697                                 nl->next = e;
698                                 nl = e;
699                         }
700                 }
701         }
702         free(orderdup);
703         /* Free any stragglers */
704         while (con->interfaces) {
705                 e = con->interfaces->next;
706                 free(con->interfaces->data);
707                 free(con->interfaces->last_message);
708                 free(con->interfaces);
709                 con->interfaces = e;
710         }
711         con->interfaces = n;
712
713         return addedi ? i : NULL;
714 }
715
716 static DHCPCD_IF *
717 dhcpcd_read_if(DHCPCD_CONNECTION *con, int fd)
718 {
719         char sbuf[sizeof(size_t)], *rbuf;
720         size_t len;
721         ssize_t bytes;
722         DHCPCD_IF *i;
723
724         bytes = read(fd, sbuf, sizeof(sbuf));
725         if (bytes == 0 || bytes == -1) {
726                 dhcpcd_close(con);
727                 return NULL;
728         }
729         memcpy(&len, sbuf, sizeof(len));
730         rbuf = malloc(len + 1);
731         if (rbuf == NULL)
732                 return NULL;
733         bytes = read(fd, rbuf, len);
734         if (bytes == 0 || bytes == -1) {
735                 free(rbuf);
736                 dhcpcd_close(con);
737                 return NULL;
738         }
739         if ((size_t)bytes != len) {
740                 free(rbuf);
741                 errno = EINVAL;
742                 return NULL;
743         }
744         rbuf[bytes] = '\0';
745
746         i = dhcpcd_new_if(con, rbuf, len);
747         if (i == NULL)
748                 free(rbuf);
749         return i;
750 }
751
752 static void
753 dhcpcd_dispatchif(DHCPCD_IF *i)
754 {
755
756         assert(i);
757         if (i->con->if_cb)
758                 i->con->if_cb(i, i->con->if_context);
759         dhcpcd_wpa_if_event(i);
760 }
761
762 void
763 dhcpcd_dispatch(DHCPCD_CONNECTION *con)
764 {
765         DHCPCD_IF *i;
766
767         assert(con);
768         i = dhcpcd_read_if(con, con->listen_fd);
769         if (i)
770                 dhcpcd_dispatchif(i);
771
772         /* Have to call update_status last as it could
773          * cause the interface to be destroyed. */
774         update_status(con, NULL);
775 }
776
777 DHCPCD_CONNECTION *
778 dhcpcd_new(void)
779 {
780         DHCPCD_CONNECTION *con;
781
782         con = calloc(1, sizeof(*con));
783         con->command_fd = con->listen_fd = -1;
784         con->open = false;
785         con->progname = "libdhcpcd";
786         return con;
787 }
788
789 void
790 dhcpcd_set_progname(DHCPCD_CONNECTION *con, const char *progname)
791 {
792
793         assert(con);
794         con->progname = progname;
795 }
796
797 const char *
798 dhcpcd_get_progname(const DHCPCD_CONNECTION *con)
799 {
800
801         assert(con);
802         return con->progname;
803 }
804
805 #ifndef HAVE_STRVERSCMP
806 /* Good enough for our needs */
807 static int
808 strverscmp(const char *s1, const char *s2)
809 {
810         int s1maj, s1min, s1part;
811         int s2maj, s2min, s2part;
812         int r;
813
814         s1min = s1part = 0;
815         if (sscanf(s1, "%d.%d.%d", &s1maj, &s1min, &s1part) < 1)
816                 return -1;
817         s2min = s2part = 0;
818         if (sscanf(s2, "%d.%d.%d", &s2maj, &s2min, &s2part) < 1)
819                 return -1;
820         r = s1maj - s2maj;
821         if (r != 0)
822                 return r;
823         r = s1min - s2min;
824         if (r != 0)
825                 return r;
826         return s1part - s2part;
827 }
828 #endif
829
830 int
831 dhcpcd_open(DHCPCD_CONNECTION *con, bool privileged)
832 {
833         const char *path = privileged ? DHCPCD_SOCKET : DHCPCD_UNPRIV_SOCKET;
834         char cmd[128];
835         ssize_t bytes;
836         size_t nifs, n;
837
838         assert(con);
839         if (con->open) {
840                 if (con->listen_fd != -1)
841                         return con->listen_fd;
842                 errno = EISCONN;
843                 return -1;
844         }
845         /* We need to block the command fd */
846         con->command_fd = dhcpcd_connect(path, 0);
847         if (con->command_fd == -1)
848                 goto err_exit;
849
850         con->terminate_commands = false;
851         if (dhcpcd_ctrl_command(con, "--version", &con->version) <= 0)
852                 goto err_exit;
853         con->terminate_commands =
854             strverscmp(con->version, "6.4.1") >= 0 ? true : false;
855
856         if (dhcpcd_ctrl_command(con, "--getconfigfile", &con->cffile) <= 0)
857                 goto err_exit;
858
859         con->open = true;
860         con->privileged = privileged;
861         update_status(con, NULL);
862
863         con->listen_fd = dhcpcd_connect(path, SOCK_NONBLOCK);
864         if (con->listen_fd == -1)
865                 goto err_exit;
866
867         dhcpcd_command_fd(con, con->listen_fd, false, "--listen", NULL);
868         dhcpcd_command_fd(con, con->command_fd, false, "--getinterfaces", NULL);
869         bytes = read(con->command_fd, cmd, sizeof(nifs));
870         if (bytes != sizeof(nifs))
871                 goto err_exit;
872         memcpy(&nifs, cmd, sizeof(nifs));
873         /* We don't dispatch each interface here as that
874          * causes too much notification spam when the GUI starts */
875         for (n = 0; n < nifs; n++)
876                 dhcpcd_read_if(con, con->command_fd);
877
878         update_status(con, NULL);
879
880         return con->listen_fd;
881
882 err_exit:
883         dhcpcd_close(con);
884         return -1;
885 }
886
887 int
888 dhcpcd_get_fd(DHCPCD_CONNECTION *con)
889 {
890
891         assert(con);
892         return con->listen_fd;
893 }
894
895 bool
896 dhcpcd_privileged(DHCPCD_CONNECTION *con)
897 {
898
899         assert(con);
900         return con->privileged;
901 }
902
903 const char *
904 dhcpcd_status(DHCPCD_CONNECTION *con)
905 {
906
907         assert(con);
908         return con->status;
909 }
910
911 const char *
912 dhcpcd_version(DHCPCD_CONNECTION *con)
913 {
914
915         assert(con);
916         return con->version;
917 }
918
919 const char *
920 dhcpcd_cffile(DHCPCD_CONNECTION *con)
921 {
922
923         assert(con);
924         return con->cffile;
925 }
926
927 void
928 dhcpcd_set_if_callback(DHCPCD_CONNECTION *con,
929     void (*cb)(DHCPCD_IF *, void *), void *ctx)
930 {
931
932         assert(con);
933         con->if_cb = cb;
934         con->if_context = ctx;
935 }
936
937 void
938 dhcpcd_set_status_callback(DHCPCD_CONNECTION *con,
939     void (*cb)(DHCPCD_CONNECTION *, const char *, void *), void *ctx)
940 {
941
942         assert(con);
943         con->status_cb = cb;
944         con->status_context = ctx;
945 }
946
947 void
948 dhcpcd_close(DHCPCD_CONNECTION *con)
949 {
950         DHCPCD_IF *nif;
951         DHCPCD_WPA *nwpa;
952         DHCPCD_WI_HIST *nh;
953
954         assert(con);
955
956         con->open = false;
957
958         /* Shut down WPA listeners as they aren't much good without dhcpcd.
959          * They'll be restarted anyway when dhcpcd comes back up. */
960         while (con->wpa) {
961                 nwpa = con->wpa->next;
962                 dhcpcd_wpa_close(con->wpa);
963                 free(con->wpa);
964                 con->wpa = nwpa;
965         }
966         while (con->wi_history) {
967                 nh = con->wi_history->next;
968                 free(con->wi_history);
969                 con->wi_history = nh;
970         }
971         while (con->interfaces) {
972                 nif = con->interfaces->next;
973                 free(con->interfaces->data);
974                 free(con->interfaces->last_message);
975                 free(con->interfaces);
976                 con->interfaces = nif;
977         }
978
979         if (con->command_fd != -1)
980                 shutdown(con->command_fd, SHUT_RDWR);
981         if (con->listen_fd != -1)
982                 shutdown(con->listen_fd, SHUT_RDWR);
983
984         update_status(con, "down");
985
986         if (con->command_fd != -1) {
987                 close(con->command_fd);
988                 con->command_fd = -1;
989         }
990         if (con->listen_fd != -1) {
991                 close(con->listen_fd);
992                 con->listen_fd = -1;
993         }
994
995         if (con->cffile) {
996                 free(con->cffile);
997                 con->cffile = NULL;
998         }
999         if (con->version) {
1000                 free(con->version);
1001                 con->version = NULL;
1002         }
1003         if (con->buf) {
1004                 free(con->buf);
1005                 con->buf = NULL;
1006                 con->buflen = 0;
1007         }
1008 }
1009
1010 void
1011 dhcpcd_free(DHCPCD_CONNECTION *con)
1012 {
1013
1014         assert(con);
1015         free(con);
1016 }
1017
1018 DHCPCD_CONNECTION *
1019 dhcpcd_if_connection(DHCPCD_IF *i)
1020 {
1021
1022         assert(i);
1023         return i->con;
1024 }
1025
1026 char *
1027 dhcpcd_if_message(DHCPCD_IF *i, bool *new_msg)
1028 {
1029         const char *ip, *iplen, *pfx;
1030         char *msg, *p;
1031         const char *reason = NULL;
1032         size_t len;
1033         bool showssid;
1034
1035         assert(i);
1036         /* Don't report non SLAAC configurations */
1037         if (strcmp(i->type, "ra") == 0 && i->up &&
1038             dhcpcd_get_value(i, "ra1_prefix") == NULL)
1039                 return NULL;
1040
1041         showssid = false;
1042         if (strcmp(i->reason, "EXPIRE") == 0)
1043                 reason = _("Expired");
1044         else if (strcmp(i->reason, "CARRIER") == 0) {
1045                 if (i->wireless) {
1046                         showssid = true;
1047                         reason = _("Associated with");
1048                 } else {
1049                         /* Don't report able in if we have addresses */
1050                         const DHCPCD_IF *ci;
1051
1052                         for (ci = i->con->interfaces; ci; ci = ci->next) {
1053                                 if (ci != i &&
1054                                     strcmp(i->ifname, ci->ifname) == 0 &&
1055                                     ci->up)
1056                                         break;
1057                         }
1058                         if (ci)
1059                                 return NULL;
1060                         reason = _("Link is up, configuring");
1061                 }
1062         } else if (strcmp(i->reason, "NOCARRIER") == 0) {
1063                 if (i->wireless) {
1064                         if (i->ssid) {
1065                                 reason = _("Disassociated from");
1066                                 showssid = true;
1067                         } else
1068                                 reason = _("Not associated");
1069                 } else
1070                         reason = _("Link is down");
1071         } else if (strcmp(i->reason, "DEPARTED") == 0)
1072                 reason = _("Departed");
1073         else if (strcmp(i->reason, "UNKNOWN") == 0)
1074                 reason = _("Unknown link state");
1075         else if (strcmp(i->reason, "FAIL") == 0)
1076                 reason = _("Automatic configuration not possible");
1077         else if (strcmp(i->reason, "3RDPARTY") == 0)
1078                 reason = _("Waiting for 3rd Party configuration");
1079
1080         if (reason == NULL) {
1081                 if (i->up) {
1082                         if (strcmp(i->reason, "DELEGATED6") == 0)
1083                                 reason = _("Delegated");
1084                         else
1085                                 reason = _("Configured");
1086                 } else if (strcmp(i->type, "ra") == 0)
1087                         reason = "Expired RA";
1088                 else
1089                         reason = i->reason;
1090         }
1091
1092         pfx = i->up ? "new_" : "old_";
1093         if ((ip = dhcpcd_get_prefix_value(i, pfx, "ip_address")))
1094                 iplen = dhcpcd_get_prefix_value(i, pfx, "subnet_cidr");
1095         else if ((ip = dhcpcd_get_value(i, "ra1_addr")))
1096                 iplen = NULL;
1097         else if ((ip = dhcpcd_get_value(i, "ra1_prefix")))
1098                 iplen = NULL;
1099         else if ((ip = dhcpcd_get_prefix_value(i, pfx,
1100             "dhcp6_ia_na1_ia_addr1")))
1101                 iplen = "128";
1102         else if ((ip = dhcpcd_get_prefix_value(i, pfx,
1103             "delegated_dhcp6_prefix")))
1104                 iplen = NULL;
1105         else {
1106                 ip = NULL;
1107                 iplen = NULL;
1108         }
1109
1110         len = strlen(i->ifname) + strlen(reason) + 3;
1111         if (showssid && i->ssid)
1112                 len += strlen(i->ssid) + 1;
1113         if (ip)
1114                 len += strlen(ip) + 1;
1115         if (iplen)
1116                 len += strlen(iplen) + 1;
1117         msg = p = malloc(len);
1118         if (msg == NULL)
1119                 return NULL;
1120         p += snprintf(msg, len, "%s: %s", i->ifname, reason);
1121         if (showssid)
1122                 p += snprintf(p, len - (size_t)(p - msg), " %s", i->ssid);
1123         if (iplen)
1124                 snprintf(p, len - (size_t)(p - msg), " %s/%s", ip, iplen);
1125         else if (ip)
1126                 snprintf(p, len - (size_t)(p - msg), " %s", ip);
1127
1128         if (new_msg) {
1129                 if (i->last_message == NULL || strcmp(i->last_message, msg))
1130                         *new_msg = true;
1131                 else
1132                         *new_msg = false;
1133         }
1134         free(i->last_message);
1135         i->last_message = strdup(msg);
1136
1137         return msg;
1138 }