Don't report cable carrier if we have a configured address.
[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         if (dst && --dlen == 0) {
378                 errno = ENOSPC;
379                 return -1;
380         }
381         *dst = '\0';
382         return bytes;
383 }
384
385 ssize_t
386 dhcpcd_decode_hex(char *dst, size_t dlen, const char *src)
387 {
388         size_t bytes, i;
389         char c;
390         int val, n;
391
392         bytes = 0;
393         while (*src) {
394                 if (dlen == 0 || dlen == 1) {
395                         errno = ENOSPC;
396                         return -1;
397                 }
398                 val = 0;
399                 for (i = 0; i < 2; i++) {
400                         c = *src++;
401                         if (c >= '0' && c <= '9')
402                                 n = c - '0';
403                         else if (c >= 'a' && c <= 'f')
404                                 n = 10 + c - 'a';
405                         else if (c >= 'A' && c <= 'F')
406                                 n = 10 + c - 'A';
407                         else {
408                                 errno = EINVAL;
409                                 return -1;
410                         }
411                         val = val * 16 + n;
412                 }
413                 *dst++ = (char)val;
414                 bytes += 2;
415                 dlen -= 2;
416                 if (*src == ':')
417                         src++;
418         }
419         return (ssize_t)bytes;
420 }
421
422 const char *
423 dhcpcd_get_prefix_value(const DHCPCD_IF *i, const char *prefix, const char *var)
424 {
425         char pvar[128], *p;
426         size_t plen, l;
427
428         assert(i);
429         assert(prefix);
430         assert(var);
431
432         p = pvar;
433         plen = sizeof(pvar);
434         l = strlcpy(p, prefix, plen);
435         if (l >= sizeof(pvar)) {
436                 errno = ENOBUFS;
437                 return NULL;
438         }
439         p += l;
440         plen -= l;
441         if (strlcpy(p, var, plen) >= plen) {
442                 errno = ENOBUFS;
443                 return NULL;
444         }
445         return dhcpcd_get_value(i, pvar);
446 }
447
448 static bool
449 strtobool(const char *var)
450 {
451
452         if (var == NULL)
453                 return false;
454
455          return (*var == '0' || *var == '\0' ||
456             strcmp(var, "false") == 0 ||
457             strcmp(var, "no") == 0) ? false : true;
458 }
459
460 static const char *
461 get_status(DHCPCD_CONNECTION *con)
462 {
463         DHCPCD_IF *i;
464         const char *status;
465
466         assert(con);
467         if (con->command_fd == -1)
468                 return "down";
469
470         if (con->listen_fd == -1)
471                 return "opened";
472
473         if (con->interfaces == NULL)
474                 return "initialised";
475
476         status = "disconnected";
477         for (i = con->interfaces; i; i = i->next) {
478                 if (i->up) {
479                         if (strcmp(i->type, "link")) {
480                                 status = "connected";
481                                 break;
482                         } else
483                                 status = "connecting";
484                 }
485         }
486         return status;
487 }
488
489 static void
490 update_status(DHCPCD_CONNECTION *con, const char *nstatus)
491 {
492
493         assert(con);
494         if (nstatus == NULL)
495                 nstatus = get_status(con);
496         if (con->status == NULL || strcmp(nstatus, con->status)) {
497                 con->status = nstatus;
498                 if (con->status_cb)
499                         con->status_cb(con, con->status, con->status_context);
500         }
501 }
502
503 DHCPCD_IF *
504 dhcpcd_interfaces(DHCPCD_CONNECTION *con)
505 {
506
507         assert(con);
508         return con->interfaces;
509 }
510
511 DHCPCD_IF *
512 dhcpcd_get_if(DHCPCD_CONNECTION *con, const char *ifname, const char *type)
513 {
514         DHCPCD_IF *i;
515
516         assert(con);
517         assert(ifname);
518         assert(type);
519
520         for (i = con->interfaces; i; i = i->next)
521                 if (strcmp(i->ifname, ifname) == 0 &&
522                     strcmp(i->type, type) == 0)
523                         return i;
524         return NULL;
525 }
526
527 static DHCPCD_IF *
528 dhcpcd_new_if(DHCPCD_CONNECTION *con, char *data, size_t len)
529 {
530         const char *ifname, *ifclass, *reason, *type, *order, *flags;
531         char *orderdup, *o, *p;
532         DHCPCD_IF *e, *i, *l, *n, *nl;
533         int ti;
534         bool addedi;
535
536 #if 0
537         char *dp = data, *de = data + len;
538         while (dp < de) {
539                 printf ("XX: %s\n", dp);
540                 dp += strlen(dp) + 1;
541         }
542 #endif
543
544         ifname = get_value(data, len, "interface");
545         if (ifname == NULL || *ifname == '\0') {
546                 errno = ESRCH;
547                 return NULL;
548         }
549         reason = get_value(data, len, "reason");
550         if (reason == NULL || *reason == '\0') {
551                 errno = ESRCH;
552                 return NULL;
553         }
554         ifclass = get_value(data, len, "ifclass");
555         /* Skip pseudo interfaces */
556         if (ifclass && *ifclass != '\0') {
557                 errno = ENOTSUP;
558                 return NULL;
559         }
560         if (strcmp(reason, "RECONFIGURE") == 0 ||
561             strcmp(reason, "INFORM") == 0 || strcmp(reason, "INFORM6") == 0)
562         {
563                 errno = ENOTSUP;
564                 return NULL;
565         }
566         order = get_value(data, len, "interface_order");
567         if (order == NULL || *order == '\0') {
568                 errno = ESRCH;
569                 return NULL;
570         }
571
572         if (strcmp(reason, "PREINIT") == 0 ||
573             strcmp(reason, "UNKNOWN") == 0 ||
574             strcmp(reason, "CARRIER") == 0 ||
575             strcmp(reason, "NOCARRIER") == 0 ||
576             strcmp(reason, "DEPARTED") == 0 ||
577             strcmp(reason, "STOPPED") == 0)
578                 type = "link";
579         else if (strcmp(reason, "ROUTERADVERT") == 0)
580                 type = "ra";
581         else if (reason[strlen(reason) - 1] == '6')
582                 type = "dhcp6";
583         else
584                 type = "ipv4";
585
586         i = NULL;
587        /* Remove all instances on carrier drop */
588         if (strcmp(reason, "NOCARRIER") == 0 ||
589             strcmp(reason, "DEPARTED") == 0 ||
590             strcmp(reason, "STOPPED") == 0)
591         {
592                 l = NULL;
593                 for (e = con->interfaces; e; e = n) {
594                         n = e->next;
595                         if (strcmp(e->ifname, ifname) == 0) {
596                                 if (strcmp(e->type, type) == 0)
597                                         l = i = e;
598                                 else {
599                                         if (l)
600                                                 l->next = e->next;
601                                         else
602                                                 con->interfaces = e->next;
603                                         free(e);
604                                 }
605                         } else
606                                 l = e;
607                 }
608         } else if (strcmp(type, "link")) {
609                 /* If link is down, ignore it */
610                 e = dhcpcd_get_if(con, ifname, "link");
611                 if (e && !e->up)
612                         return NULL;
613         }
614
615         orderdup = strdup(order);
616         if (orderdup == NULL)
617                 return NULL;
618
619         /* Find our pointer */
620         if (i == NULL) {
621                 l = NULL;
622                 for (e = con->interfaces; e; e = e->next) {
623                         if (strcmp(e->ifname, ifname) == 0 &&
624                             strcmp(e->type, type) == 0)
625                         {
626                                 i = e;
627                                 break;
628                         }
629                         l = e;
630                 }
631         }
632         if (i == NULL) {
633                 i = malloc(sizeof(*i));
634                 if (i == NULL) {
635                         free(orderdup);
636                         return NULL;
637                 }
638                 if (l)
639                         l->next = i;
640                 else
641                         con->interfaces = i;
642                 i->next = NULL;
643                 i->last_message = NULL;
644         } else
645                 free(i->data);
646
647         /* Now fill out our interface structure */
648         i->con = con;
649         i->data = data;
650         i->data_len = len;
651         i->ifname = ifname;
652         i->type = type;
653         i->reason = reason;
654         flags = dhcpcd_get_value(i, "ifflags");
655         if (flags)
656                 i->flags = (unsigned int)strtoul(flags, NULL, 0);
657         else
658                 i->flags = 0;
659         if (strcmp(reason, "CARRIER") == 0)
660                 i->up = true;
661         else
662                 i->up = strtobool(dhcpcd_get_value(i, "if_up"));
663         i->wireless = strtobool(dhcpcd_get_value(i, "ifwireless"));
664         i->ssid = dhcpcd_get_value(i, "ifssid");
665         if (i->ssid == NULL && i->wireless)
666                 i->ssid = dhcpcd_get_value(i, i->up ? "new_ssid" : "old_ssid");
667
668        /* Sort! */
669         n = nl = NULL;
670         p = orderdup;
671         addedi = false;
672         while ((o = strsep(&p, " ")) != NULL) {
673                 for (ti = 0; dhcpcd_types[ti]; ti++) {
674                         l = NULL;
675                         for (e = con->interfaces; e; e = e->next) {
676                                 if (strcmp(e->ifname, o) == 0 &&
677                                     strcmp(e->type, dhcpcd_types[ti]) == 0)
678                                         break;
679                                 l = e;
680                         }
681                         if (e == NULL)
682                                 continue;
683                         if (i == e)
684                                 addedi = true;
685                         if (l)
686                                 l->next = e->next;
687                         else
688                                 con->interfaces = e->next;
689                         e->next = NULL;
690                         if (nl == NULL)
691                                 n = nl = e;
692                         else {
693                                 nl->next = e;
694                                 nl = e;
695                         }
696                 }
697         }
698         free(orderdup);
699         /* Free any stragglers */
700         while (con->interfaces) {
701                 e = con->interfaces->next;
702                 free(con->interfaces->data);
703                 free(con->interfaces->last_message);
704                 free(con->interfaces);
705                 con->interfaces = e;
706         }
707         con->interfaces = n;
708
709         return addedi ? i : NULL;
710 }
711
712 static DHCPCD_IF *
713 dhcpcd_read_if(DHCPCD_CONNECTION *con, int fd)
714 {
715         char sbuf[sizeof(size_t)], *rbuf;
716         size_t len;
717         ssize_t bytes;
718         DHCPCD_IF *i;
719
720         bytes = read(fd, sbuf, sizeof(sbuf));
721         if (bytes == 0 || bytes == -1) {
722                 dhcpcd_close(con);
723                 return NULL;
724         }
725         memcpy(&len, sbuf, sizeof(len));
726         rbuf = malloc(len + 1);
727         if (rbuf == NULL)
728                 return NULL;
729         bytes = read(fd, rbuf, len);
730         if (bytes == 0 || bytes == -1) {
731                 free(rbuf);
732                 dhcpcd_close(con);
733                 return NULL;
734         }
735         if ((size_t)bytes != len) {
736                 free(rbuf);
737                 errno = EINVAL;
738                 return NULL;
739         }
740         rbuf[bytes] = '\0';
741
742         i = dhcpcd_new_if(con, rbuf, len);
743         if (i == NULL)
744                 free(rbuf);
745         return i;
746 }
747
748 static void
749 dhcpcd_dispatchif(DHCPCD_IF *i)
750 {
751
752         assert(i);
753         if (i->con->if_cb)
754                 i->con->if_cb(i, i->con->if_context);
755         dhcpcd_wpa_if_event(i);
756 }
757
758 void
759 dhcpcd_dispatch(DHCPCD_CONNECTION *con)
760 {
761         DHCPCD_IF *i;
762
763         assert(con);
764         i = dhcpcd_read_if(con, con->listen_fd);
765         if (i)
766                 dhcpcd_dispatchif(i);
767
768         /* Have to call update_status last as it could
769          * cause the interface to be destroyed. */
770         update_status(con, NULL);
771 }
772
773 DHCPCD_CONNECTION *
774 dhcpcd_new(void)
775 {
776         DHCPCD_CONNECTION *con;
777
778         con = calloc(1, sizeof(*con));
779         con->command_fd = con->listen_fd = -1;
780         con->open = false;
781         con->progname = "libdhcpcd";
782         return con;
783 }
784
785 void
786 dhcpcd_set_progname(DHCPCD_CONNECTION *con, const char *progname)
787 {
788
789         assert(con);
790         con->progname = progname;
791 }
792
793 const char *
794 dhcpcd_get_progname(const DHCPCD_CONNECTION *con)
795 {
796
797         assert(con);
798         return con->progname;
799 }
800
801 #ifndef HAVE_STRVERSCMP
802 /* Good enough for our needs */
803 static int
804 strverscmp(const char *s1, const char *s2)
805 {
806         int s1maj, s1min, s1part;
807         int s2maj, s2min, s2part;
808         int r;
809
810         s1min = s1part = 0;
811         if (sscanf(s1, "%d.%d.%d", &s1maj, &s1min, &s1part) < 1)
812                 return -1;
813         s2min = s2part = 0;
814         if (sscanf(s2, "%d.%d.%d", &s2maj, &s2min, &s2part) < 1)
815                 return -1;
816         r = s1maj - s2maj;
817         if (r != 0)
818                 return r;
819         r = s1min - s2min;
820         if (r != 0)
821                 return r;
822         return s1part - s2part;
823 }
824 #endif
825
826 int
827 dhcpcd_open(DHCPCD_CONNECTION *con, bool privileged)
828 {
829         const char *path = privileged ? DHCPCD_SOCKET : DHCPCD_UNPRIV_SOCKET;
830         char cmd[128];
831         ssize_t bytes;
832         size_t nifs, n;
833
834         assert(con);
835         if (con->open) {
836                 if (con->listen_fd != -1)
837                         return con->listen_fd;
838                 errno = EISCONN;
839                 return -1;
840         }
841         /* We need to block the command fd */
842         con->command_fd = dhcpcd_connect(path, 0);
843         if (con->command_fd == -1)
844                 goto err_exit;
845
846         con->terminate_commands = false;
847         if (dhcpcd_ctrl_command(con, "--version", &con->version) <= 0)
848                 goto err_exit;
849         con->terminate_commands =
850             strverscmp(con->version, "6.4.1") >= 0 ? true : false;
851
852         if (dhcpcd_ctrl_command(con, "--getconfigfile", &con->cffile) <= 0)
853                 goto err_exit;
854
855         con->open = true;
856         con->privileged = privileged;
857         update_status(con, NULL);
858
859         con->listen_fd = dhcpcd_connect(path, SOCK_NONBLOCK);
860         if (con->listen_fd == -1)
861                 goto err_exit;
862
863         dhcpcd_command_fd(con, con->listen_fd, false, "--listen", NULL);
864         dhcpcd_command_fd(con, con->command_fd, false, "--getinterfaces", NULL);
865         bytes = read(con->command_fd, cmd, sizeof(nifs));
866         if (bytes != sizeof(nifs))
867                 goto err_exit;
868         memcpy(&nifs, cmd, sizeof(nifs));
869         /* We don't dispatch each interface here as that
870          * causes too much notification spam when the GUI starts */
871         for (n = 0; n < nifs; n++)
872                 dhcpcd_read_if(con, con->command_fd);
873
874         update_status(con, NULL);
875
876         return con->listen_fd;
877
878 err_exit:
879         dhcpcd_close(con);
880         return -1;
881 }
882
883 int
884 dhcpcd_get_fd(DHCPCD_CONNECTION *con)
885 {
886
887         assert(con);
888         return con->listen_fd;
889 }
890
891 bool
892 dhcpcd_privileged(DHCPCD_CONNECTION *con)
893 {
894
895         assert(con);
896         return con->privileged;
897 }
898
899 const char *
900 dhcpcd_status(DHCPCD_CONNECTION *con)
901 {
902
903         assert(con);
904         return con->status;
905 }
906
907 const char *
908 dhcpcd_version(DHCPCD_CONNECTION *con)
909 {
910
911         assert(con);
912         return con->version;
913 }
914
915 const char *
916 dhcpcd_cffile(DHCPCD_CONNECTION *con)
917 {
918
919         assert(con);
920         return con->cffile;
921 }
922
923 void
924 dhcpcd_set_if_callback(DHCPCD_CONNECTION *con,
925     void (*cb)(DHCPCD_IF *, void *), void *ctx)
926 {
927
928         assert(con);
929         con->if_cb = cb;
930         con->if_context = ctx;
931 }
932
933 void
934 dhcpcd_set_status_callback(DHCPCD_CONNECTION *con,
935     void (*cb)(DHCPCD_CONNECTION *, const char *, void *), void *ctx)
936 {
937
938         assert(con);
939         con->status_cb = cb;
940         con->status_context = ctx;
941 }
942
943 void
944 dhcpcd_close(DHCPCD_CONNECTION *con)
945 {
946         DHCPCD_IF *nif;
947         DHCPCD_WPA *nwpa;
948         DHCPCD_WI_HIST *nh;
949
950         assert(con);
951
952         con->open = false;
953
954         /* Shut down WPA listeners as they aren't much good without dhcpcd.
955          * They'll be restarted anyway when dhcpcd comes back up. */
956         while (con->wpa) {
957                 nwpa = con->wpa->next;
958                 dhcpcd_wpa_close(con->wpa);
959                 free(con->wpa);
960                 con->wpa = nwpa;
961         }
962         while (con->wi_history) {
963                 nh = con->wi_history->next;
964                 free(con->wi_history);
965                 con->wi_history = nh;
966         }
967         while (con->interfaces) {
968                 nif = con->interfaces->next;
969                 free(con->interfaces->data);
970                 free(con->interfaces->last_message);
971                 free(con->interfaces);
972                 con->interfaces = nif;
973         }
974
975         if (con->command_fd != -1)
976                 shutdown(con->command_fd, SHUT_RDWR);
977         if (con->listen_fd != -1)
978                 shutdown(con->listen_fd, SHUT_RDWR);
979
980         update_status(con, "down");
981
982         if (con->command_fd != -1) {
983                 close(con->command_fd);
984                 con->command_fd = -1;
985         }
986         if (con->listen_fd != -1) {
987                 close(con->listen_fd);
988                 con->listen_fd = -1;
989         }
990
991         if (con->cffile) {
992                 free(con->cffile);
993                 con->cffile = NULL;
994         }
995         if (con->version) {
996                 free(con->version);
997                 con->version = NULL;
998         }
999         if (con->buf) {
1000                 free(con->buf);
1001                 con->buf = NULL;
1002                 con->buflen = 0;
1003         }
1004 }
1005
1006 void
1007 dhcpcd_free(DHCPCD_CONNECTION *con)
1008 {
1009
1010         assert(con);
1011         free(con);
1012 }
1013
1014 DHCPCD_CONNECTION *
1015 dhcpcd_if_connection(DHCPCD_IF *i)
1016 {
1017
1018         assert(i);
1019         return i->con;
1020 }
1021
1022 char *
1023 dhcpcd_if_message(DHCPCD_IF *i, bool *new_msg)
1024 {
1025         const char *ip, *iplen, *pfx;
1026         char *msg, *p;
1027         const char *reason = NULL;
1028         size_t len;
1029         bool showssid;
1030
1031         assert(i);
1032         /* Don't report non SLAAC configurations */
1033         if (strcmp(i->type, "ra") == 0 && i->up &&
1034             dhcpcd_get_value(i, "ra1_prefix") == NULL)
1035                 return NULL;
1036
1037         showssid = false;
1038         if (strcmp(i->reason, "EXPIRE") == 0)
1039                 reason = _("Expired");
1040         else if (strcmp(i->reason, "CARRIER") == 0) {
1041                 if (i->wireless) {
1042                         showssid = true;
1043                         reason = _("Associated with");
1044                 } else {
1045                         /* Don't report able in if we have addresses */
1046                         const DHCPCD_IF *ci;
1047
1048                         for (ci = i->con->interfaces; ci; ci = ci->next) {
1049                                 if (ci != i &&
1050                                     strcmp(i->ifname, ci->ifname) == 0 &&
1051                                     ci->up)
1052                                         break;
1053                         }
1054                         if (ci)
1055                                 return NULL;
1056                         reason = _("Cable plugged in");
1057                 }
1058         } else if (strcmp(i->reason, "NOCARRIER") == 0) {
1059                 if (i->wireless) {
1060                         if (i->ssid) {
1061                                 reason = _("Disassociated from");
1062                                 showssid = true;
1063                         } else
1064                                 reason = _("Not associated");
1065                 } else
1066                         reason = _("Cable unplugged");
1067         } else if (strcmp(i->reason, "DEPARTED") == 0)
1068                 reason = _("Departed");
1069         else if (strcmp(i->reason, "UNKNOWN") == 0)
1070                 reason = _("Unknown link state");
1071         else if (strcmp(i->reason, "FAIL") == 0)
1072                 reason = _("Automatic configuration not possible");
1073         else if (strcmp(i->reason, "3RDPARTY") == 0)
1074                 reason = _("Waiting for 3rd Party configuration");
1075
1076         if (reason == NULL) {
1077                 if (i->up)
1078                         reason = _("Configured");
1079                 else if (strcmp(i->type, "ra") == 0)
1080                         reason = "Expired RA";
1081                 else
1082                         reason = i->reason;
1083         }
1084
1085         pfx = i->up ? "new_" : "old_";
1086         if ((ip = dhcpcd_get_prefix_value(i, pfx, "ip_address")))
1087                 iplen = dhcpcd_get_prefix_value(i, pfx, "subnet_cidr");
1088         else if ((ip = dhcpcd_get_value(i, "ra1_prefix")))
1089                 iplen = NULL;
1090         else if ((ip = dhcpcd_get_prefix_value(i, pfx,
1091             "dhcp6_ia_na1_ia_addr1")))
1092                 iplen = "128";
1093         else {
1094                 ip = NULL;
1095                 iplen = NULL;
1096         }
1097
1098         len = strlen(i->ifname) + strlen(reason) + 3;
1099         if (showssid && i->ssid)
1100                 len += strlen(i->ssid) + 1;
1101         if (ip)
1102                 len += strlen(ip) + 1;
1103         if (iplen)
1104                 len += strlen(iplen) + 1;
1105         msg = p = malloc(len);
1106         if (msg == NULL)
1107                 return NULL;
1108         p += snprintf(msg, len, "%s: %s", i->ifname, reason);
1109         if (showssid)
1110                 p += snprintf(p, len - (size_t)(p - msg), " %s", i->ssid);
1111         if (iplen)
1112                 snprintf(p, len - (size_t)(p - msg), " %s/%s", ip, iplen);
1113         else if (ip)
1114                 snprintf(p, len - (size_t)(p - msg), " %s", ip);
1115
1116         if (new_msg) {
1117                 if (i->last_message == NULL || strcmp(i->last_message, msg))
1118                         *new_msg = true;
1119                 else
1120                         *new_msg = false;
1121         }
1122         free(i->last_message);
1123         i->last_message = strdup(msg);
1124
1125         return msg;
1126 }