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