Support the new link type.
authorRoy Marples <roy@marples.name>
Tue, 6 May 2014 19:04:56 +0000 (19:04 +0000)
committerRoy Marples <roy@marples.name>
Tue, 6 May 2014 19:04:56 +0000 (19:04 +0000)
Report status cleaner.

src/dhcpcd-gtk/main.c
src/libdhcpcd/dispatch.c
src/libdhcpcd/main.c
src/libdhcpcd/misc.c

index 9caf5fe3249cc1a410b1add145afbc5559f5dfda..54e1dd74558e89582c0f9dc0c38cbe7019132423 100644 (file)
@@ -121,46 +121,31 @@ animate_online(_unused gpointer data)
 static void
 update_online(DHCPCD_CONNECTION *con, bool showif)
 {
-       bool ison, iscarrier, isstop;
+       bool ison, iscarrier;
        char *msg, *msgs, *tmp;
-       DHCPCD_IF *ifs, *i, *j;
+       DHCPCD_IF *ifs, *i;
 
        ison = iscarrier = false;
        msgs = NULL;
        ifs = dhcpcd_interfaces(con);
        for (i = ifs; i; i = i->next) {
-               if (showif)
-                       g_message("%s: %s", i->ifname, i->reason);
-               if (strcmp(i->reason, "RELEASE") == 0 ||
-                   strcmp(i->reason, "STOP") == 0)
-                       continue;
-               if (strcmp(i->type, "ipv4") != 0) {
-                       isstop = false;
-                       for (j = ifs; j; j = j->next)
-                               if (strcmp(j->ifname, i->ifname) == 0 &&
-                                   strcmp(j->type, "ipv4") == 0)
-                               {
-                                       if (strcmp(j->reason, "STOP") == 0)
-                                               isstop = true;
-                                       break;
-                               }
-                       if (isstop)
-                               continue;
-               }
                if (i->up)
                        ison = iscarrier = true;
                if (!iscarrier && g_strcmp0(i->reason, "CARRIER") == 0)
                        iscarrier = true;
                msg = dhcpcd_if_message(i);
                if (msg) {
+                       if (showif)
+                               g_message("%s", msg);
                        if (msgs) {
                                tmp = g_strconcat(msgs, "\n", msg, NULL);
                                g_free(msgs);
                                g_free(msg);
                                msgs = tmp;
                        } else
-                                   msgs = msg;
-               }
+                               msgs = msg;
+               } else if (showif)
+                       g_message("%s: %s", i->ifname, i->reason);
        }
 
        if (online != ison || carrier != iscarrier) {
@@ -253,7 +238,7 @@ event_cb(DHCPCD_CONNECTION *con, DHCPCD_IF *i, _unused void *data)
        char *msg;
        const char *icon;
 
-       g_message("%s: %s", i->ifname, i->reason);
+       g_message("interface event: %s: %s", i->ifname, i->reason);
        update_online(con, false);
 
        /* We should ignore renew and stop so we don't annoy the user */
@@ -262,14 +247,17 @@ event_cb(DHCPCD_CONNECTION *con, DHCPCD_IF *i, _unused void *data)
                return;
 
        msg = dhcpcd_if_message(i);
-       if (i->up)
-               icon = "network-transmit-receive";
-       //else
-       //      icon = "network-transmit";
-       if (!i->up)
-               icon = "network-offline";
-       notify(_("Network event"), msg, icon);
-       g_free(msg);
+       if (msg) {
+               g_message("%s", msg);
+               if (i->up)
+                       icon = "network-transmit-receive";
+               //else
+               //      icon = "network-transmit";
+               if (!i->up)
+                       icon = "network-offline";
+               notify(_("Network event"), msg, icon);
+               g_free(msg);
+       }
 }
 
 static void
index 5243059aa374a355b7682ceaa300a7ff7f18cde6..79208334f587e3d92921a2f2775b2083a184d80a 100644 (file)
@@ -30,6 +30,8 @@
 #define IN_LIBDHCPCD
 #include "libdhcpcd.h"
 
+static const char *dhcpcd_types[] = { "link", "ipv4", "ra", "dhcp6", NULL };
+
 static const char *
 dhcpcd_message_get_string(DHCPCD_MESSAGE *msg)
 {
@@ -51,7 +53,6 @@ dhcpcd_handle_event(DHCPCD_CONNECTION *con, DHCPCD_MESSAGE *msg)
        DBusMessageIter args;
        DHCPCD_IF *i, *e, *l, *n, *nl;
        char *order, *o, *p;
-       static const char *types[] = { "ipv4", "ra", NULL };
        int ti;
 
        if (!dbus_message_iter_init(msg, &args))
@@ -63,48 +64,90 @@ dhcpcd_handle_event(DHCPCD_CONNECTION *con, DHCPCD_MESSAGE *msg)
        p = order;
        n = nl = NULL;
 
+       /* Remove all instances on carrier drop */
+       if (strcmp(i->reason, "NOCARRIER") == 0 ||
+           strcmp(i->reason, "DEPARTED") == 0 ||
+           strcmp(i->reason, "STOPPED") == 0)
+       {
+               l = NULL;
+               for (e = con->interfaces; e; e = n) {
+                       n = e->next;
+                       if (strcmp(e->ifname, i->ifname) == 0) {
+                               if (strcmp(e->type, i->type) == 0)
+                                       l = nl = e;
+                               else {
+                                       if (l)
+                                               l->next = e->next;
+                                       else
+                                               con->interfaces = e->next;
+                                       free(e);
+                               }
+                       } else
+                               l = e;
+               }
+       }
+
+       /* Find our pointer */
+       if (nl == NULL) {
+               l = NULL;
+               for (e = con->interfaces; e; e = e->next) {
+                       if (strcmp(e->ifname, i->ifname) == 0 &&
+                           strcmp(e->type, i->type) == 0)
+                       {
+                               nl = e;
+                               break;
+                       }
+                       l = e;
+               }
+       }
+       if (nl) {
+               /* Preserve the pointer for wireless history */
+               n = nl->next;
+               memcpy(nl, i, sizeof(*i));
+               nl->next = n;
+               free(i);
+               i = nl;
+       } else {
+               /* Append it then */
+               if (l)
+                       l->next = i;
+               else
+                       con->interfaces = i;
+               i->next = NULL;
+       }
+
+       /* Sort! */
+       n = nl = NULL;
        while ((o = strsep(&p, " ")) != NULL) {
-               for (ti = 0; ti < 2; ti++) {
+               for (ti = 0; dhcpcd_types[ti]; ti++) {
                        l = NULL;
                        for (e = con->interfaces; e; e = e->next) {
                                if (strcmp(e->ifname, o) == 0 &&
-                                   strcmp(e->type, types[ti]) == 0)
+                                   strcmp(e->type, dhcpcd_types[ti]) == 0)
                                        break;
                                l = e;
                        }
-                       if (e == NULL) {
-                               if (strcmp(i->ifname, o) != 0 ||
-                                   strcmp(i->type, types[ti]) == 0)
-                                       continue;
-                               e = i;
-                       } else {
-                               if (l != NULL)
-                                       l->next = e->next;
-                               else
-                                       con->interfaces = e->next;
-                               if (i != NULL &&
-                                   strcmp(e->ifname, i->ifname) == 0 &&
-                                   strcmp(e->type, i->type) == 0)
-                               {
-                                       /* Preserve the pointer for
-                                        * our wireless history */
-                                       memcpy(e, i, sizeof(*e));
-                                       free(i);
-                                       i = e;
-                               }
-                               e->next = NULL;
-                       }
+                       if (e == NULL)
+                               continue;
+                       if (l)
+                               l->next = e->next;
+                       else
+                               con->interfaces = e->next;
+                       e->next = NULL;
                        if (nl == NULL)
                                n = nl = e;
                        else {
                                nl->next = e;
-                               nl = nl->next;
+                               nl = e;
                        }
                }
        }
-
-       if (nl != NULL)
-               nl->next = con->interfaces;
+       /* Free any stragglers */
+       while (con->interfaces) {
+               e = con->interfaces->next;
+               free(con->interfaces);
+               con->interfaces = e;
+       }
        con->interfaces = n;
 
        if (con->event)
@@ -138,7 +181,7 @@ dhcpcd_dispatch_message(DHCPCD_CONNECTION *con, DHCPCD_MESSAGE *msg)
        {
                if (con->wi_scanresults) {
                        str = dhcpcd_message_get_string(msg);
-                       ifp = dhcpcd_if_find(con, str, "ipv4");
+                       ifp = dhcpcd_if_find(con, str, "link");
                        if (ifp)
                                con->wi_scanresults(con, ifp, con->signal_data);
                }
index f1d3c2a82aee841f462fa77a4c58768a4ce390a9..d63844fe4e5d30fcfd93edd5d06ea3edf5ad5897 100644 (file)
@@ -526,6 +526,7 @@ dhcpcd_interfaces(DHCPCD_CONNECTION *con)
        DBusMessageIter args, dict, entry;
        DHCPCD_IF *i, *l;
        int errors;
+       char *stopif;
 
        if (con->interfaces != NULL)
                return con->interfaces;
@@ -544,6 +545,7 @@ dhcpcd_interfaces(DHCPCD_CONNECTION *con)
        l = NULL;
        errors = con->errors;
        dbus_message_iter_recurse(&args, &dict);
+       stopif = NULL;
        for (;
             dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY;
             dbus_message_iter_next(&dict))
@@ -553,6 +555,14 @@ dhcpcd_interfaces(DHCPCD_CONNECTION *con)
                i = dhcpcd_if_new(con, &entry, NULL);
                if (i == NULL)
                        break;
+               if (stopif && strcmp(stopif, i->ifname) == 0) {
+                       free(i);
+                       continue;
+               }
+               if (strcmp(i->reason, "NOCARRIER") == 0 ||
+                   strcmp(i->reason, "DEPARTED") == 0 ||
+                   strcmp(i->reason, "STOPPED") == 0)
+                       stopif = i->ifname;
                if (l == NULL)
                        con->interfaces = i;
                else
@@ -629,7 +639,7 @@ dhcpcd_set_signal_functions(DHCPCD_CONNECTION *con,
        }
        if (con->wi_scanresults) {
                for (i = dhcpcd_interfaces(con); i; i = i->next)
-                       if (i->wireless && strcmp(i->type, "ipv4") == 0)
+                       if (i->wireless && strcmp(i->type, "link") == 0)
                                con->wi_scanresults(con, i, data);
        }
 }
index 325fedc7180f98bf870d34bc2f45fc2d34f99ce4..2ea94e5b08313537406bb06cd3259689bb3c8f5c 100644 (file)
@@ -74,7 +74,9 @@ dhcpcd_if_message(const DHCPCD_IF *i)
                } else
                        reason = _("Cable unplugged");
                showip = false;
-       } else if (strcmp(i->reason, "FAIL") == 0)
+       } else if (strcmp(i->reason, "UNKNOWN") == 0)
+               reason = _("Unknown link state");
+       else if (strcmp(i->reason, "FAIL") == 0)
                reason = _("Automatic configuration not possible");
        else if (strcmp(i->reason, "3RDPARTY") == 0)
                reason = _("Waiting for 3rd Party configuration");
@@ -110,7 +112,8 @@ dhcpcd_if_message(const DHCPCD_IF *i)
        if (showssid)
                p += snprintf(p, len - (size_t)(p - msg), " %s", i->ssid);
        if (i->ip.s_addr != 0 && showip) {
-               p += snprintf(p, len - (size_t)(p - msg), " %s", inet_ntoa(i->ip));
+               p += snprintf(p, len - (size_t)(p - msg), " %s",
+                   inet_ntoa(i->ip));
                if (i->cidr != 0)
                        snprintf(p, len - (size_t)(p - msg), "/%d", i->cidr);
        } else if (!IN6_IS_ADDR_UNSPECIFIED(&i->ip6) && showip) {
@@ -120,7 +123,8 @@ dhcpcd_if_message(const DHCPCD_IF *i)
                p += snprintf(p, len - (size_t)(p - msg), " %s",
                    inet_ntop(AF_INET6, &i->prefix, buf, INET6_ADDRSTRLEN));
                if (i->prefix_len != 0)
-                       snprintf(p, len - (size_t)(p - msg), "/%d", i->prefix_len);
+                       snprintf(p, len - (size_t)(p - msg),
+                           "/%d", i->prefix_len);
        }
        return msg;
 }