Add a function to decode a hex string to binary.
[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                 if (strncmp(data, var, vlen) == 0) {
235                         p = data + vlen;
236                         break;
237                 }
238                 data += strlen(data) + 1;
239         }
240         if (p != NULL && *p != '\0')
241                 return p;
242         return NULL;
243 }
244
245 const char *
246 dhcpcd_get_value(const DHCPCD_IF *i, const char *var)
247 {
248
249         assert(i);
250         assert(var);
251         return get_value(i->data, i->data_len, var);
252 }
253
254 ssize_t
255 dhcpcd_decode(char *dst, size_t dlen, const char *src)
256 {
257
258         assert(dst);
259         assert(src);
260         return strnunvis(dst, dlen, src);
261 }
262
263 ssize_t
264 dhcpcd_decode_hex(char *dst, size_t dlen, const char *src)
265 {
266         size_t bytes, i;
267         char c;
268         int val, n;
269
270         bytes = 0;
271         while (*src) {
272                 if (dlen == 0 || dlen == 1) {
273                         errno = ENOSPC;
274                         return -1;
275                 }
276                 val = 0;
277                 for (i = 0; i < 2; i++) {
278                         c = *src++;
279                         if (c >= '0' && c <= '9')
280                                 n = c - '0';
281                         else if (c >= 'a' && c <= 'f')
282                                 n = 10 + c - 'a';
283                         else if (c >= 'A' && c <= 'F')
284                                 n = 10 + c - 'A';
285                         else {
286                                 errno = EINVAL;
287                                 return -1;
288                         }
289                         val = val * 16 + n;
290                 }
291                 *dst++ = (char)val;
292                 bytes += 2;
293                 dlen -= 2;
294                 if (*src == ':')
295                         src++;
296         }
297         return (ssize_t)bytes;
298 }
299
300 ssize_t
301 dhcpcd_decode_shell(char *dst, size_t dlen, const char *src)
302 {
303         char *tmp, *p, c, *e, *d;
304         ssize_t l;
305
306         assert(dst);
307         assert(src);
308
309         tmp = malloc(dlen);
310         if (tmp == NULL)
311                 return -1;
312         if ((l = dhcpcd_decode(tmp, dlen, src)) == -1) {
313                 free(tmp);
314                 return -1;
315         }
316
317         p = tmp;
318         e = tmp + l;
319         d = dst;
320         while (p < e) {
321                 c = *p++;
322
323                 if (isascii(c) && (isgraph(c) || iswhite(c))) {
324                         if (dlen < 2) {
325                                 errno = ENOSPC;
326                                 return -1;
327                         }
328                         *d++ = c;
329                         dlen--;
330                         continue;
331                 }
332                 
333                 if (dlen < 5) {
334                         errno = ENOSPC;
335                         return -1;
336                 }
337                 *d++ = '\\';
338                 *d++ = (((unsigned char)c >> 6) & 03) + '0';
339                 *d++ = (((unsigned char)c >> 3) & 07) + '0';
340                 *d++ = ( (unsigned char)c       & 07) + '0';
341                 dlen -= 4;
342         }
343         *d = '\0';
344         free(tmp);
345
346         return d - dst;
347 }
348
349 const char *
350 dhcpcd_get_prefix_value(const DHCPCD_IF *i, const char *prefix, const char *var)
351 {
352         char pvar[128], *p;
353         size_t plen, l;
354
355         assert(i);
356         assert(prefix);
357         assert(var);
358
359         p = pvar;
360         plen = sizeof(pvar);
361         l = strlcpy(p, prefix, plen);
362         if (l >= sizeof(pvar)) {
363                 errno = ENOBUFS;
364                 return NULL;
365         }
366         p += l;
367         plen -= l;
368         if (strlcpy(p, var, plen) >= plen) {
369                 errno = ENOBUFS;
370                 return NULL;
371         }
372         return dhcpcd_get_value(i, pvar);
373 }
374
375 static bool
376 strtobool(const char *var)
377 {
378
379         if (var == NULL)
380                 return false;
381
382          return (*var == '0' || *var == '\0' ||
383             strcmp(var, "false") == 0 ||
384             strcmp(var, "no") == 0) ? false : true;
385 }
386
387 static const char *
388 get_status(DHCPCD_CONNECTION *con)
389 {
390         DHCPCD_IF *i;
391         const char *status;
392
393         assert(con);
394         if (con->command_fd == -1)
395                 return "down";
396
397         if (con->listen_fd == -1)
398                 return "opened";
399
400         if (con->interfaces == NULL)
401                 return "initialised";
402
403         status = "disconnected";
404         for (i = con->interfaces; i; i = i->next) {
405                 if (i->up) {
406                         if (strcmp(i->type, "link")) {
407                                 status = "connected";
408                                 break;
409                         } else
410                                 status = "connecting";
411                 }
412         }
413         return status;
414 }
415
416 static void
417 update_status(DHCPCD_CONNECTION *con, const char *nstatus)
418 {
419
420         assert(con);
421         if (nstatus == NULL)
422                 nstatus = get_status(con);
423         if (con->status == NULL || strcmp(nstatus, con->status)) {
424                 con->status = nstatus;
425                 if (con->status_cb)
426                         con->status_cb(con, con->status, con->status_context);
427         }
428 }
429
430 DHCPCD_IF *
431 dhcpcd_interfaces(DHCPCD_CONNECTION *con)
432 {
433
434         assert(con);
435         return con->interfaces;
436 }
437
438 DHCPCD_IF *
439 dhcpcd_get_if(DHCPCD_CONNECTION *con, const char *ifname, const char *type)
440 {
441         DHCPCD_IF *i;
442
443         assert(con);
444         assert(ifname);
445         assert(type);
446
447         for (i = con->interfaces; i; i = i->next)
448                 if (strcmp(i->ifname, ifname) == 0 &&
449                     strcmp(i->type, type) == 0)
450                         return i;
451         return NULL;
452 }
453
454 static DHCPCD_IF *
455 dhcpcd_new_if(DHCPCD_CONNECTION *con, char *data, size_t len)
456 {
457         const char *ifname, *ifclass, *reason, *type, *order, *flags, *ssid;
458         char *orderdup, *o, *p;
459         DHCPCD_IF *e, *i, *l, *n, *nl;
460         int ti;
461         bool addedi;
462
463         ifname = get_value(data, len, "interface=");
464         if (ifname == NULL || *ifname == '\0') {
465                 errno = ESRCH;
466                 return NULL;
467         }
468         reason = get_value(data, len, "reason=");
469         if (reason == NULL || *reason == '\0') {
470                 errno = ESRCH;
471                 return NULL;
472         }
473         ifclass = get_value(data, len, "ifclass=");
474         /* Skip pseudo interfaces */
475         if (ifclass && *ifclass != '\0') {
476                 errno = ENOTSUP;
477                 return NULL;
478         }
479         if (strcmp(reason, "RECONFIGURE") == 0) {
480                 errno = ENOTSUP;
481                 return NULL;
482         }
483         order = get_value(data, len, "interface_order=");
484         if (order == NULL || *order == '\0') {
485                 errno = ESRCH;
486                 return NULL;
487         }
488
489         if (strcmp(reason, "PREINIT") == 0 ||
490             strcmp(reason, "UNKNOWN") == 0 ||
491             strcmp(reason, "CARRIER") == 0 ||
492             strcmp(reason, "NOCARRIER") == 0 ||
493             strcmp(reason, "DEPARTED") == 0 ||
494             strcmp(reason, "STOPPED") == 0)
495                 type = "link";
496         else if (strcmp(reason, "ROUTERADVERT") == 0)
497                 type = "ra";
498         else if (reason[strlen(reason) - 1] == '6')
499                 type = "dhcp6";
500         else
501                 type = "ipv4";
502
503         i = NULL;
504        /* Remove all instances on carrier drop */
505         if (strcmp(reason, "NOCARRIER") == 0 ||
506             strcmp(reason, "DEPARTED") == 0 ||
507             strcmp(reason, "STOPPED") == 0)
508         {
509                 l = NULL;
510                 for (e = con->interfaces; e; e = n) {
511                         n = e->next;
512                         if (strcmp(e->ifname, ifname) == 0) {
513                                 if (strcmp(e->type, type) == 0)
514                                         l = i = e;
515                                 else {
516                                         if (l)
517                                                 l->next = e->next;
518                                         else
519                                                 con->interfaces = e->next;
520                                         free(e);
521                                 }
522                         } else
523                                 l = e;
524                 }
525         } else if (strcmp(type, "link")) {
526                 /* If link is down, ignore it */
527                 e = dhcpcd_get_if(con, ifname, "link");
528                 if (e && !e->up)
529                         return NULL;
530         }
531
532         orderdup = strdup(order);
533         if (orderdup == NULL)
534                 return NULL;
535
536         /* Find our pointer */
537         if (i == NULL) {
538                 l = NULL;
539                 for (e = con->interfaces; e; e = e->next) {
540                         if (strcmp(e->ifname, ifname) == 0 &&
541                             strcmp(e->type, type) == 0)
542                         {
543                                 i = e;
544                                 break;
545                         }
546                         l = e;
547                 }
548         }
549         if (i == NULL) {
550                 i = malloc(sizeof(*i));
551                 if (i == NULL) {
552                         free(orderdup);
553                         return NULL;
554                 }
555                 if (l)
556                         l->next = i;
557                 else
558                         con->interfaces = i;
559                 i->next = NULL;
560                 i->last_message = NULL;
561         } else
562                 free(i->data);
563
564         /* Now fill out our interface structure */
565         i->con = con;
566         i->data = data;
567         i->data_len = len;
568         i->ifname = ifname;
569         i->type = type;
570         i->reason = reason;
571         flags = dhcpcd_get_value(i, "ifflags=");
572         if (flags)
573                 i->flags = (unsigned int)strtoul(flags, NULL, 0);
574         else
575                 i->flags = 0;
576         if (strcmp(reason, "CARRIER") == 0)
577                 i->up = true;
578         else
579                 i->up = strtobool(dhcpcd_get_value(i, "if_up="));
580         i->wireless = strtobool(dhcpcd_get_value(i, "ifwireless="));
581         ssid = dhcpcd_get_value(i, i->up ? "new_ssid=" : "old_ssid=");
582         if (ssid == NULL ||
583             dhcpcd_decode_shell(i->ssid, sizeof(i->ssid), ssid) == -1)
584                 *i->ssid = '\0';
585
586        /* Sort! */
587         n = nl = NULL;
588         p = orderdup;
589         addedi = false;
590         while ((o = strsep(&p, " ")) != NULL) {
591                 for (ti = 0; dhcpcd_types[ti]; ti++) {
592                         l = NULL;
593                         for (e = con->interfaces; e; e = e->next) {
594                                 if (strcmp(e->ifname, o) == 0 &&
595                                     strcmp(e->type, dhcpcd_types[ti]) == 0)
596                                         break;
597                                 l = e;
598                         }
599                         if (e == NULL)
600                                 continue;
601                         if (i == e)
602                                 addedi = true;
603                         if (l)
604                                 l->next = e->next;
605                         else
606                                 con->interfaces = e->next;
607                         e->next = NULL;
608                         if (nl == NULL)
609                                 n = nl = e;
610                         else {
611                                 nl->next = e;
612                                 nl = e;
613                         }
614                 }
615         }
616         free(orderdup);
617         /* Free any stragglers */
618         while (con->interfaces) {
619                 e = con->interfaces->next;
620                 free(con->interfaces->data);
621                 free(con->interfaces->last_message);
622                 free(con->interfaces);
623                 con->interfaces = e;
624         }
625         con->interfaces = n;
626
627         return addedi ? i : NULL;
628 }
629
630 static DHCPCD_IF *
631 dhcpcd_read_if(DHCPCD_CONNECTION *con, int fd)
632 {
633         char sbuf[sizeof(size_t)], *rbuf;
634         size_t len;
635         ssize_t bytes;
636         DHCPCD_IF *i;
637
638         bytes = read(fd, sbuf, sizeof(sbuf));
639         if (bytes == 0 || bytes == -1) {
640                 dhcpcd_close(con);
641                 return NULL;
642         }
643         memcpy(&len, sbuf, sizeof(len));
644         rbuf = malloc(len + 1);
645         if (rbuf == NULL)
646                 return NULL;
647         bytes = read(fd, rbuf, len);
648         if (bytes == 0 || bytes == -1) {
649                 free(rbuf);
650                 dhcpcd_close(con);
651                 return NULL;
652         }
653         if ((size_t)bytes != len) {
654                 free(rbuf);
655                 errno = EINVAL;
656                 return NULL;
657         }
658         rbuf[bytes] = '\0';
659
660         i = dhcpcd_new_if(con, rbuf, len);
661         if (i == NULL)
662                 free(rbuf);
663         return i;
664 }
665
666 static void
667 dhcpcd_dispatchif(DHCPCD_IF *i)
668 {
669
670         assert(i);
671         if (i->con->if_cb)
672                 i->con->if_cb(i, i->con->if_context);
673         dhcpcd_wpa_if_event(i);
674 }
675
676 void
677 dhcpcd_dispatch(DHCPCD_CONNECTION *con)
678 {
679         DHCPCD_IF *i;
680
681         assert(con);
682         i = dhcpcd_read_if(con, con->listen_fd);
683         if (i)
684                 dhcpcd_dispatchif(i);
685
686         /* Have to call update_status last as it could
687          * cause the interface to be destroyed. */
688         update_status(con, NULL);
689 }
690
691 DHCPCD_CONNECTION *
692 dhcpcd_new(void)
693 {
694         DHCPCD_CONNECTION *con;
695
696         con = calloc(1, sizeof(*con));
697         con->command_fd = con->listen_fd = -1;
698         con->open = false;
699         con->progname = "libdhcpcd";
700         return con;
701 }
702
703 void
704 dhcpcd_set_progname(DHCPCD_CONNECTION *con, const char *progname)
705 {
706
707         assert(con);
708         con->progname = progname;
709 }
710
711 const char *
712 dhcpcd_get_progname(const DHCPCD_CONNECTION *con)
713 {
714
715         assert(con);
716         return con->progname;
717 }
718
719 #ifndef HAVE_STRVERSCMP
720 /* Good enough for our needs */
721 static int
722 strverscmp(const char *s1, const char *s2)
723 {
724         int s1maj, s1min, s1part;
725         int s2maj, s2min, s2part;
726         int r;
727
728         s1min = s1part = 0;
729         if (sscanf(s1, "%d.%d.%d", &s1maj, &s1min, &s1part) < 1)
730                 return -1;
731         s2min = s2part = 0;
732         if (sscanf(s2, "%d.%d.%d", &s2maj, &s2min, &s2part) < 1)
733                 return -1;
734         r = s1maj - s2maj;
735         if (r != 0)
736                 return r;
737         r = s1min - s2min;
738         if (r != 0)
739                 return r;
740         return s1part - s2part;
741 }
742 #endif
743
744 int
745 dhcpcd_open(DHCPCD_CONNECTION *con, bool privileged)
746 {
747         const char *path = privileged ? DHCPCD_SOCKET : DHCPCD_UNPRIV_SOCKET;
748         char cmd[128];
749         ssize_t bytes;
750         size_t nifs, n;
751
752         assert(con);
753         if (con->open) {
754                 if (con->listen_fd != -1)
755                         return con->listen_fd;
756                 errno = EISCONN;
757                 return -1;
758         }
759         /* We need to block the command fd */
760         con->command_fd = dhcpcd_connect(path, 0);
761         if (con->command_fd == -1)
762                 goto err_exit;
763
764         con->terminate_commands = false;
765         if (dhcpcd_ctrl_command(con, "--version", &con->version) <= 0)
766                 goto err_exit;
767         con->terminate_commands =
768             strverscmp(con->version, "6.4.1") >= 0 ? true : false;
769
770         if (dhcpcd_ctrl_command(con, "--getconfigfile", &con->cffile) <= 0)
771                 goto err_exit;
772
773         con->open = true;
774         con->privileged = privileged;
775         update_status(con, NULL);
776
777         con->listen_fd = dhcpcd_connect(path, SOCK_NONBLOCK);
778         if (con->listen_fd == -1)
779                 goto err_exit;
780
781         dhcpcd_command_fd(con, con->listen_fd, false, "--listen", NULL);
782         dhcpcd_command_fd(con, con->command_fd, false, "--getinterfaces", NULL);
783         bytes = read(con->command_fd, cmd, sizeof(nifs));
784         if (bytes != sizeof(nifs))
785                 goto err_exit;
786         memcpy(&nifs, cmd, sizeof(nifs));
787         /* We don't dispatch each interface here as that
788          * causes too much notification spam when the GUI starts */
789         for (n = 0; n < nifs; n++)
790                 dhcpcd_read_if(con, con->command_fd);
791
792         update_status(con, NULL);
793
794         return con->listen_fd;
795
796 err_exit:
797         dhcpcd_close(con);
798         return -1;
799 }
800
801 int
802 dhcpcd_get_fd(DHCPCD_CONNECTION *con)
803 {
804
805         assert(con);
806         return con->listen_fd;
807 }
808
809 bool
810 dhcpcd_privileged(DHCPCD_CONNECTION *con)
811 {
812
813         assert(con);
814         return con->privileged;
815 }
816
817 const char *
818 dhcpcd_status(DHCPCD_CONNECTION *con)
819 {
820
821         assert(con);
822         return con->status;
823 }
824
825 const char *
826 dhcpcd_version(DHCPCD_CONNECTION *con)
827 {
828
829         assert(con);
830         return con->version;
831 }
832
833 const char *
834 dhcpcd_cffile(DHCPCD_CONNECTION *con)
835 {
836
837         assert(con);
838         return con->cffile;
839 }
840
841 void
842 dhcpcd_set_if_callback(DHCPCD_CONNECTION *con,
843     void (*cb)(DHCPCD_IF *, void *), void *ctx)
844 {
845
846         assert(con);
847         con->if_cb = cb;
848         con->if_context = ctx;
849 }
850
851 void
852 dhcpcd_set_status_callback(DHCPCD_CONNECTION *con,
853     void (*cb)(DHCPCD_CONNECTION *, const char *, void *), void *ctx)
854 {
855
856         assert(con);
857         con->status_cb = cb;
858         con->status_context = ctx;
859 }
860
861 void
862 dhcpcd_close(DHCPCD_CONNECTION *con)
863 {
864         DHCPCD_IF *nif;
865         DHCPCD_WPA *nwpa;
866         DHCPCD_WI_HIST *nh;
867
868         assert(con);
869
870         con->open = false;
871
872         /* Shut down WPA listeners as they aren't much good without dhcpcd.
873          * They'll be restarted anyway when dhcpcd comes back up. */
874         while (con->wpa) {
875                 nwpa = con->wpa->next;
876                 dhcpcd_wpa_close(con->wpa);
877                 free(con->wpa);
878                 con->wpa = nwpa;
879         }
880         while (con->wi_history) {
881                 nh = con->wi_history->next;
882                 free(con->wi_history);
883                 con->wi_history = nh;
884         }
885         while (con->interfaces) {
886                 nif = con->interfaces->next;
887                 free(con->interfaces->data);
888                 free(con->interfaces->last_message);
889                 free(con->interfaces);
890                 con->interfaces = nif;
891         }
892
893         if (con->command_fd != -1)
894                 shutdown(con->command_fd, SHUT_RDWR);
895         if (con->listen_fd != -1)
896                 shutdown(con->listen_fd, SHUT_RDWR);
897
898         update_status(con, "down");
899
900         if (con->command_fd != -1) {
901                 close(con->command_fd);
902                 con->command_fd = -1;
903         }
904         if (con->listen_fd != -1) {
905                 close(con->listen_fd);
906                 con->listen_fd = -1;
907         }
908
909         if (con->cffile) {
910                 free(con->cffile);
911                 con->cffile = NULL;
912         }
913         if (con->version) {
914                 free(con->version);
915                 con->version = NULL;
916         }
917         if (con->buf) {
918                 free(con->buf);
919                 con->buf = NULL;
920                 con->buflen = 0;
921         }
922 }
923
924 void
925 dhcpcd_free(DHCPCD_CONNECTION *con)
926 {
927
928         assert(con);
929         free(con);
930 }
931
932 DHCPCD_CONNECTION *
933 dhcpcd_if_connection(DHCPCD_IF *i)
934 {
935
936         assert(i);
937         return i->con;
938 }
939
940 char *
941 dhcpcd_if_message(DHCPCD_IF *i, bool *new_msg)
942 {
943         const char *ip, *iplen, *pfx;
944         char *msg, *p;
945         const char *reason = NULL;
946         size_t len;
947         bool showssid;
948
949         assert(i);
950         /* Don't report non SLAAC configurations */
951         if (strcmp(i->type, "ra") == 0 && i->up &&
952             dhcpcd_get_value(i, "ra1_prefix=") == NULL)
953                 return NULL;
954
955         showssid = false;
956         if (strcmp(i->reason, "EXPIRE") == 0)
957                 reason = _("Expired");
958         else if (strcmp(i->reason, "CARRIER") == 0) {
959                 if (i->wireless) {
960                         showssid = true;
961                         reason = _("Associated with");
962                 } else
963                         reason = _("Cable plugged in");
964         } else if (strcmp(i->reason, "NOCARRIER") == 0) {
965                 if (i->wireless) {
966                         if (i->ssid) {
967                                 reason = _("Disassociated from");
968                                 showssid = true;
969                         } else
970                                 reason = _("Not associated");
971                 } else
972                         reason = _("Cable unplugged");
973         } else if (strcmp(i->reason, "DEPARTED") == 0)
974                 reason = _("Departed");
975         else if (strcmp(i->reason, "UNKNOWN") == 0)
976                 reason = _("Unknown link state");
977         else if (strcmp(i->reason, "FAIL") == 0)
978                 reason = _("Automatic configuration not possible");
979         else if (strcmp(i->reason, "3RDPARTY") == 0)
980                 reason = _("Waiting for 3rd Party configuration");
981
982         if (reason == NULL) {
983                 if (i->up)
984                         reason = _("Configured");
985                 else if (strcmp(i->type, "ra") == 0)
986                         reason = "Expired RA";
987                 else
988                         reason = i->reason;
989         }
990
991         pfx = i->up ? "new_" : "old_";
992         if ((ip = dhcpcd_get_prefix_value(i, pfx, "ip_address=")))
993                 iplen = dhcpcd_get_prefix_value(i, pfx, "subnet_cidr=");
994         else if ((ip = dhcpcd_get_value(i, "ra1_prefix=")))
995                 iplen = NULL;
996         else if ((ip = dhcpcd_get_prefix_value(i, pfx,
997             "dhcp6_ia_na1_ia_addr1=")))
998                 iplen = "128";
999         else {
1000                 ip = NULL;
1001                 iplen = NULL;
1002         }
1003
1004         len = strlen(i->ifname) + strlen(reason) + 3;
1005         if (showssid && i->ssid)
1006                 len += strlen(i->ssid) + 1;
1007         if (ip)
1008                 len += strlen(ip) + 1;
1009         if (iplen)
1010                 len += strlen(iplen) + 1;
1011         msg = p = malloc(len);
1012         if (msg == NULL)
1013                 return NULL;
1014         p += snprintf(msg, len, "%s: %s", i->ifname, reason);
1015         if (showssid)
1016                 p += snprintf(p, len - (size_t)(p - msg), " %s", i->ssid);
1017         if (iplen)
1018                 snprintf(p, len - (size_t)(p - msg), " %s/%s", ip, iplen);
1019         else if (ip)
1020                 snprintf(p, len - (size_t)(p - msg), " %s", ip);
1021
1022         if (new_msg) {
1023                 if (i->last_message == NULL || strcmp(i->last_message, msg))
1024                         *new_msg = true;
1025                 else
1026                         *new_msg = false;
1027         }
1028         free(i->last_message);
1029         i->last_message = strdup(msg);
1030
1031         return msg;
1032 }