Use constants rather than string comparison for a saner API.
authorRoy Marples <roy@marples.name>
Mon, 11 May 2015 14:39:53 +0000 (14:39 +0000)
committerRoy Marples <roy@marples.name>
Mon, 11 May 2015 14:39:53 +0000 (14:39 +0000)
src/dhcpcd-curses/dhcpcd-curses.c
src/dhcpcd-curses/dhcpcd-curses.h
src/dhcpcd-gtk/main.c
src/dhcpcd-gtk/menu.c
src/dhcpcd-gtk/prefs.c
src/dhcpcd-qt/dhcpcd-preferences.cpp
src/dhcpcd-qt/dhcpcd-qt.cpp
src/dhcpcd-qt/dhcpcd-qt.h
src/libdhcpcd/dhcpcd.c
src/libdhcpcd/dhcpcd.h
src/libdhcpcd/wpa.c

index eebb53c322ac16570478458f8ae320e9b9e83dad..3584b9b57b49ba9c91188e53e73281bf37bccb86 100644 (file)
@@ -148,7 +148,7 @@ update_online(struct ctx *ctx, bool show_if)
        msgs_len = 0;
        ifs = dhcpcd_interfaces(ctx->con);
        for (i = ifs; i; i = i->next) {
-               if (strcmp(i->type, "link") == 0) {
+               if (i->type == DHT_LINK) {
                        if (i->up)
                                carrier = true;
                } else {
@@ -236,14 +236,15 @@ unprived:
 }
 
 static void
-status_cb(DHCPCD_CONNECTION *con, const char *status, void *arg)
+status_cb(DHCPCD_CONNECTION *con,
+    unsigned int status, const char *status_msg, void *arg)
 {
        struct ctx *ctx = arg;
 
-       debug(ctx, _("Status changed to %s"), status);
-       set_status(ctx, status);
+       debug(ctx, _("Status changed to %s"), status_msg);
+       set_status(ctx, status_msg);
 
-       if (strcmp(status, "down") == 0) {
+       if (status == DHC_DOWN) {
                eloop_event_delete(ctx->eloop, ctx->fd, NULL, NULL, 0);
                ctx->fd = -1;
                ctx->online = ctx->carrier = false;
@@ -254,20 +255,19 @@ status_cb(DHCPCD_CONNECTION *con, const char *status, void *arg)
        } else {
                bool refresh;
 
-               if (ctx->last_status == NULL ||
-                   strcmp(ctx->last_status, "down") == 0)
+               if (ctx->last_status == DHC_UNKNOWN ||
+                   ctx->last_status == DHC_DOWN)
                {
                        debug(ctx, _("Connected to dhcpcd-%s"),
                            dhcpcd_version(con));
                        refresh = true;
                } else
                        refresh =
-                           strcmp(ctx->last_status, "opened") ? false : true;
+                           ctx->last_status == DHC_OPENED ? true : false;
                update_online(ctx, refresh);
        }
 
-       free(ctx->last_status);
-       ctx->last_status = strdup(status);
+       ctx->last_status = status;
 }
 
 static void
@@ -275,9 +275,8 @@ if_cb(DHCPCD_IF *i, void *arg)
 {
        struct ctx *ctx = arg;
 
-       if (strcmp(i->reason, "RENEW") &&
-           strcmp(i->reason, "STOP") &&
-           strcmp(i->reason, "STOPPED"))
+       if (i->state == DHS_RENEW ||
+           i->state == DHS_STOP || i->state == DHS_STOPPED)
        {
                char *msg;
                bool new_msg;
@@ -399,15 +398,16 @@ wpa_scan_cb(DHCPCD_WPA *wpa, void *arg)
 }
 
 static void
-wpa_status_cb(DHCPCD_WPA *wpa, const char *status, void *arg)
+wpa_status_cb(DHCPCD_WPA *wpa,
+    unsigned int status, const char *status_msg, void *arg)
 {
        struct ctx *ctx = arg;
        DHCPCD_IF *i;
        WI_SCAN *w, *wn;
 
        i = dhcpcd_wpa_if(wpa);
-       debug(ctx, _("%s: WPA status %s"), i->ifname, status);
-       if (strcmp(status, "down") == 0) {
+       debug(ctx, _("%s: WPA status %s"), i->ifname, status_msg);
+       if (status == DHC_DOWN) {
                eloop_event_delete(ctx->eloop, -1, NULL, wpa, 0);
                TAILQ_FOREACH_SAFE(w, &ctx->wi_scans, next, wn) {
                        if (w->interface == i) {
@@ -577,7 +577,6 @@ main(void)
        /* Free everything else */
        eloop_free(ctx.eloop);
        endwin();
-       free(ctx.last_status);
 
 #ifdef HAVE_NC_FREE_AND_EXIT
        /* undefined ncurses function to allow valgrind debugging */
index 6aca30e0aaaf47d5004f16a4c18d2f25910cb519..c94d09c76699fbcd1db17d872987d192d6fb0416 100644 (file)
@@ -63,7 +63,7 @@ struct ctx {
        int fd;
        bool online;
        bool carrier;
-       char *last_status;
+       unsigned int last_status;
        size_t status_len;
        WI_SCANS wi_scans;
 
index 047c1f079be7e487b63db14b13b66a7d20bbc4ec..88de9e13d6deefdfab3c9fea0145267f8f076780 100644 (file)
@@ -193,7 +193,7 @@ update_online(DHCPCD_CONNECTION *con, bool showif)
        msgs = NULL;
        ifs = dhcpcd_interfaces(con);
        for (i = ifs; i; i = i->next) {
-               if (g_strcmp0(i->type, "link") == 0) {
+               if (i->type == DHT_LINK) {
                        if (i->up)
                                iscarrier = true;
                } else {
@@ -388,17 +388,17 @@ dhcpcd_watch(int fd,
 }
 
 static void
-dhcpcd_status_cb(DHCPCD_CONNECTION *con, const char *status,
-    _unused void *data)
+dhcpcd_status_cb(DHCPCD_CONNECTION *con,
+    unsigned int status, const char *status_msg, _unused void *data)
 {
-       static char *last = NULL;
+       static unsigned int last = DHC_UNKNOWN;
        const char *msg;
        bool refresh;
        WI_SCAN *w;
 
-       g_message("Status changed to %s", status);
-       if (g_strcmp0(status, "down") == 0) {
-               msg = N_(last ?
+       g_message("Status changed to %s", status_msg);
+       if (status == DHC_DOWN) {
+               msg = N_(last == DHC_UNKNOWN ?
                    "Connection to dhcpcd lost" : "dhcpcd not running");
                if (ani_timer != 0) {
                        g_source_remove(ani_timer);
@@ -420,17 +420,16 @@ dhcpcd_status_cb(DHCPCD_CONNECTION *con, const char *status,
                dhcpcd_unwatch(-1, con);
                g_timeout_add(DHCPCD_RETRYOPEN, dhcpcd_try_open, con);
        } else {
-               if ((last == NULL || g_strcmp0(last, "down") == 0)) {
+               if (last == DHC_UNKNOWN || last == DHC_DOWN) {
                        g_message(_("Connected to %s-%s"), "dhcpcd",
                            dhcpcd_version(con));
                        refresh = true;
                } else
-                       refresh = g_strcmp0(last, "opened") ? false : true;
+                       refresh = last == DHC_OPENED ? true : false;
                update_online(con, refresh);
        }
 
-       g_free(last);
-       last = g_strdup(status);
+       last = status;
 }
 
 static gboolean
@@ -492,9 +491,8 @@ dhcpcd_if_cb(DHCPCD_IF *i, _unused void *data)
        bool new_msg;
 
        /* We should ignore renew and stop so we don't annoy the user */
-       if (g_strcmp0(i->reason, "RENEW") &&
-           g_strcmp0(i->reason, "STOP") &&
-           g_strcmp0(i->reason, "STOPPED"))
+       if (i->state != DHS_RENEW &&
+           i->state != DHS_STOP && i->state != DHS_STOPPED)
        {
                msg = dhcpcd_if_message(i, &new_msg);
                if (msg) {
@@ -545,8 +543,7 @@ dhcpcd_wpa_cb(_unused GIOChannel *gio, _unused GIOCondition c,
                /* If the interface hasn't left, try re-opening */
                i = dhcpcd_wpa_if(wpa);
                if (i == NULL ||
-                   g_strcmp0(i->reason, "DEPARTED") == 0 ||
-                   g_strcmp0(i->reason, "STOPPED") == 0)
+                   i->state == DHS_DEPARTED || i->state == DHS_STOPPED)
                        return TRUE;
                g_warning(_("dhcpcd WPA connection lost: %s"), i->ifname);
                g_timeout_add(DHCPCD_RETRYOPEN, dhcpcd_wpa_try_open, wpa);
@@ -662,14 +659,15 @@ dhcpcd_wpa_scan_cb(DHCPCD_WPA *wpa, _unused void *data)
 }
 
 static void
-dhcpcd_wpa_status_cb(DHCPCD_WPA *wpa, const char *status, _unused void *data)
+dhcpcd_wpa_status_cb(DHCPCD_WPA *wpa,
+    unsigned int status, const char *status_msg, _unused void *data)
 {
        DHCPCD_IF *i;
        WI_SCAN *w, *wn;
 
        i = dhcpcd_wpa_if(wpa);
-       g_message("%s: WPA status %s", i->ifname, status);
-       if (g_strcmp0(status, "down") == 0) {
+       g_message("%s: WPA status %s", i->ifname, status_msg);
+       if (status == DHC_DOWN) {
                dhcpcd_unwatch(-1, wpa);
                TAILQ_FOREACH_SAFE(w, &wi_scans, next, wn) {
                        if (w->interface == i) {
index 932dfd49e4c0aeb5d4cc14e3974dbb42126aa65d..ce23424227477c9a8af1041839b9f84892d688ee 100644 (file)
@@ -426,7 +426,7 @@ on_popup(GtkStatusIcon *icon, guint button, guint32 atime, gpointer data)
        image = gtk_image_new_from_icon_name("preferences-system-network",
            GTK_ICON_SIZE_MENU);
        gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item), image);
-       if (g_strcmp0(dhcpcd_status(con), "down") == 0)
+       if (dhcpcd_status(con, NULL) == DHC_DOWN)
                gtk_widget_set_sensitive(item, false);
        else
                g_signal_connect(G_OBJECT(item), "activate",
index 2e065a73c3ae2b68dac698f77b14557a21a778b3..3f060df7b08170fc26579c5718a3fa68af89d5f5 100644 (file)
@@ -65,7 +65,7 @@ show_config(DHCPCD_OPTION *conf)
                autocnf = false;
        else {
                if ((val = dhcpcd_config_get(conf, "inform")) == NULL &&
-                   (iface && iface->flags & IFF_POINTOPOINT))
+                   (iface && iface->ifflags & IFF_POINTOPOINT))
                        autocnf = false;
                else
                        autocnf = true;
@@ -130,7 +130,7 @@ make_config(DHCPCD_OPTION **conf)
 
        ret = true;
        a = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(autoconf));
-       if (iface && iface->flags & IFF_POINTOPOINT)
+       if (iface && iface->ifflags & IFF_POINTOPOINT)
                set_option(conf, true, "ip_address=", a ? NULL : ns, &ret);
        else {
                val = gtk_entry_get_text(GTK_ENTRY(address));
@@ -333,7 +333,7 @@ names_on_change(_unused GtkWidget *widget, gpointer data)
                        }
        }
        gtk_widget_set_sensitive(address,
-           !iface || (iface->flags & IFF_POINTOPOINT) == 0);
+           !iface || (iface->ifflags & IFF_POINTOPOINT) == 0);
        if (block && name) {
                errno = 0;
                config = dhcpcd_config_read(con, block, name);
@@ -517,7 +517,7 @@ prefs_show(DHCPCD_CONNECTION *con)
                return;
        }
 
-       if (g_strcmp0(dhcpcd_status(con), "down") == 0)
+       if (dhcpcd_status(con, NULL) == DHC_DOWN)
                return;
 
        dialog = gtk_window_new(GTK_WINDOW_TOPLEVEL);
index 794754d7adce2e5603cf4f52a245ab3644b285a4..7366a06877a7e74814da3635b9ddaaf4e333df77 100644 (file)
@@ -255,7 +255,7 @@ void DhcpcdPreferences::showConfig()
                 a = false;
         else
                 a = !((val = dhcpcd_config_get(config, "inform")) == NULL &&
-                    (iface && iface->flags & IFF_POINTOPOINT));
+                    (iface && iface->ifflags & IFF_POINTOPOINT));
        autoConf->setChecked(a);
        ip->setText(val);
        router->setText(dhcpcd_config_get_static(config, "routers="));
@@ -272,7 +272,7 @@ bool DhcpcdPreferences::changedConfig()
                 a = false;
         else
                 a = !((val = dhcpcd_config_get(config, "inform")) == NULL &&
-                    (iface && iface->flags & IFF_POINTOPOINT));
+                    (iface && iface->ifflags & IFF_POINTOPOINT));
        if (autoConf->isChecked() != a)
                return true;
        if (ip->text().compare(val))
@@ -326,7 +326,7 @@ bool DhcpcdPreferences::makeConfig()
 
        a = autoConf->isChecked();
        ret = true;
-       if (iface && iface->flags & IFF_POINTOPOINT)
+       if (iface && iface->ifflags & IFF_POINTOPOINT)
                setOption("ip_address=", a ? NULL : ns, &ret);
         else {
                val = getString(ip);
@@ -413,9 +413,9 @@ void DhcpcdPreferences::showBlock(const QString &txt)
 
        if (eBlock && eWhat) {
                if (strcmp(eWhat, "interface") == 0)
-                       iface = dhcpcd_get_if(con, eBlock, "link");
+                       iface = dhcpcd_get_if(con, eBlock, DHT_LINK);
                ip->setEnabled(iface == NULL ||
-                   !(iface->flags & IFF_POINTOPOINT));
+                   !(iface->ifflags & IFF_POINTOPOINT));
                errno = 0;
                config = dhcpcd_config_read(con, eWhat, eBlock);
                if (config == NULL && errno) {
@@ -481,7 +481,7 @@ void DhcpcdPreferences::rebind()
        found = false;
        worked = true;
        for (i = dhcpcd_interfaces(con); i; i = i->next) {
-               if (strcmp(i->type, "link") == 0 &&
+               if (i->type == DHT_LINK &&
                    (i->ssid && strcmp(i->ssid, eBlock) == 0))
                {
                        found = true;
index 7318e703dbfa89fb9461b6c26bcb797128f4d287..45bfda3ee68b83f217d09b15e70080690e103793 100644 (file)
@@ -51,7 +51,7 @@ DhcpcdQt::DhcpcdQt()
        createTrayIcon();
 
        onLine = carrier = false;
-       lastStatus = NULL;
+       lastStatus = DHC_UNKNOWN;
        aniTimer = new QTimer(this);
        connect(aniTimer, SIGNAL(timeout()), this, SLOT(animate()));
        notifier = NULL;
@@ -91,8 +91,6 @@ DhcpcdQt::~DhcpcdQt()
                dhcpcd_free(con);
        }
 
-       free(lastStatus);
-
        for (auto &wi : *wis)
                wi->deleteLater();
        delete wis;
@@ -214,7 +212,7 @@ void DhcpcdQt::updateOnline(bool showIf)
        isOn = isCarrier = false;
        ifs = dhcpcd_interfaces(con);
        for (i = ifs; i; i = i->next) {
-               if (strcmp(i->type, "link") == 0) {
+               if (i->type == DHT_LINK) {
                        if (i->up)
                                isCarrier = true;
                } else {
@@ -252,11 +250,11 @@ void DhcpcdQt::updateOnline(bool showIf)
        trayIcon->setToolTip(msgs);
 }
 
-void DhcpcdQt::statusCallback(const char *status)
+void DhcpcdQt::statusCallback(unsigned int status, const char *status_msg)
 {
 
-       qDebug("Status changed to %s", status);
-       if (strcmp(status, "down") == 0) {
+       qDebug("Status changed to %s", status_msg);
+       if (status == DHC_DOWN) {
                aniTimer->stop();
                aniCounter = 0;
                onLine = carrier = false;
@@ -280,18 +278,17 @@ void DhcpcdQt::statusCallback(const char *status)
        } else {
                bool refresh;
 
-               if (lastStatus == NULL || strcmp(lastStatus, "down") == 0) {
+               if (lastStatus == DHC_UNKNOWN || lastStatus == DHC_DOWN) {
                        qDebug("Connected to dhcpcd-%s", dhcpcd_version(con));
                        refresh = true;
                } else
-                       refresh = strcmp(lastStatus, "opened") ? false : true;
+                       refresh = lastStatus == DHC_OPENED ? true : false;
                updateOnline(refresh);
        }
 
-       free(lastStatus);
-       lastStatus = strdup(status);
+       lastStatus = status;
 
-       if (strcmp(status, "down") == 0) {
+       if (status == DHC_DOWN) {
                if (retryOpenTimer == NULL) {
                        retryOpenTimer = new QTimer(this);
                        connect(retryOpenTimer, SIGNAL(timeout()),
@@ -302,11 +299,11 @@ void DhcpcdQt::statusCallback(const char *status)
 }
 
 void DhcpcdQt::dhcpcd_status_cb(_unused DHCPCD_CONNECTION *con,
-    const char *status, void *d)
+    unsigned int status, const char *status_msg, void *d)
 {
        DhcpcdQt *dhcpcdQt = (DhcpcdQt *)d;
 
-       dhcpcdQt->statusCallback(status);
+       dhcpcdQt->statusCallback(status, status_msg);
 }
 
 void DhcpcdQt::ifCallback(DHCPCD_IF *i)
@@ -314,9 +311,8 @@ void DhcpcdQt::ifCallback(DHCPCD_IF *i)
        char *msg;
        bool new_msg;
 
-       if (strcmp(i->reason, "RENEW") &&
-           strcmp(i->reason, "STOP") &&
-           strcmp(i->reason, "STOPPED"))
+       if (i->state == DHS_RENEW ||
+           i->state == DHS_STOP || i->state == DHS_STOPPED)
        {
                msg = dhcpcd_if_message(i, &new_msg);
                if (msg) {
@@ -453,13 +449,14 @@ void DhcpcdQt::dhcpcd_wpa_scan_cb(DHCPCD_WPA *wpa, void *d)
        dhcpcdQt->scanCallback(wpa);
 }
 
-void DhcpcdQt::wpaStatusCallback(DHCPCD_WPA *wpa, const char *status)
+void DhcpcdQt::wpaStatusCallback(DHCPCD_WPA *wpa,
+    unsigned int status, const char *status_msg)
 {
        DHCPCD_IF *i;
 
        i = dhcpcd_wpa_if(wpa);
-       qDebug("%s: WPA status %s", i->ifname, status);
-       if (strcmp(status, "down") == 0) {
+       qDebug("%s: WPA status %s", i->ifname, status_msg);
+       if (status == DHC_DOWN) {
                DhcpcdWi *wi = findWi(wpa);
                if (wi) {
                        wis->removeOne(wi);
@@ -468,12 +465,12 @@ void DhcpcdQt::wpaStatusCallback(DHCPCD_WPA *wpa, const char *status)
        }
 }
 
-void DhcpcdQt::dhcpcd_wpa_status_cb(DHCPCD_WPA *wpa, const char *status,
-    void *d)
+void DhcpcdQt::dhcpcd_wpa_status_cb(DHCPCD_WPA *wpa,
+    unsigned int status, const char *status_msg, void *d)
 {
        DhcpcdQt *dhcpcdQt = (DhcpcdQt *)d;
 
-       dhcpcdQt->wpaStatusCallback(wpa, status);
+       dhcpcdQt->wpaStatusCallback(wpa, status, status_msg);
 }
 
 void DhcpcdQt::tryOpen() {
index a364c4616f6529182c314305954efd213c513bcf..af4e659b52ce284b731d0f278961b6cfc938ae7c 100644 (file)
@@ -58,16 +58,17 @@ public:
 
        DHCPCD_CONNECTION *getConnection();
        static void dhcpcd_status_cb(DHCPCD_CONNECTION *con,
-           const char *status, void *d);
-       void statusCallback(const char *status);
+           unsigned int status, const char *status_msg, void *d);
+       void statusCallback(unsigned int, const char *status);
        static void dhcpcd_if_cb(DHCPCD_IF *i, void *d);
        void ifCallback(DHCPCD_IF *i);
 
        static void dhcpcd_wpa_scan_cb(DHCPCD_WPA *wpa, void *d);
        void scanCallback(DHCPCD_WPA *wpa);
-       static void dhcpcd_wpa_status_cb(DHCPCD_WPA *wpa, const char *status,
-           void *d);
-       void wpaStatusCallback(DHCPCD_WPA *wpa, const char *status);
+       static void dhcpcd_wpa_status_cb(DHCPCD_WPA *wpa, unsigned int status,
+           const char *status_msg, void *d);
+       void wpaStatusCallback(DHCPCD_WPA *wpa,
+           unsigned int status, const char *status_msg);
 
        static const char * signalStrengthIcon(DHCPCD_WI_SCAN *scan);
        static QIcon getIcon(QString category, QString name);
@@ -95,7 +96,7 @@ private:
        QList<DhcpcdWi *> *wis;
        DhcpcdWi *findWi(DHCPCD_WPA *wpa);
 
-       char *lastStatus;
+       unsigned int lastStatus;
        bool onLine;
        bool carrier;
        QTimer *aniTimer;
index c8bdd14fbfd8cb48cd54b610fbc3dc06c77a55ab..73f0ee4ea64a979f3d0b132fadff9604953ddfca 100644 (file)
 #define iswhite(c)     (c == ' ' || c == '\t' || c == '\n')
 #endif
 
-static const char * const dhcpcd_types[] =
-    { "link", "ipv4", "ra", "dhcp6", NULL };
+static const char * const dhcpcd_cstates[DHC_MAX] = {
+       "unknown",
+       "down",
+       "opened",
+       "initialised",
+       "disconnected",
+       "connecting",
+       "connected"
+};
+
+struct dhcpcd_vs {
+       unsigned int val;
+       const char *str;
+};
+
+static const struct dhcpcd_vs dhcpcd_states[] = {
+    { DHS_DUMP,                "DUMP" },
+    { DHS_TEST,                "TEST" },
+    { DHS_STOPPED,     "STOPPED" },
+    { DHS_FAIL,                "FAIL" },
+    { DHS_STOP,                "STOP" },
+    { DHS_PREINIT,     "PREINIT" },
+    { DHS_DEPARTED,    "DEPARTED" },
+    { DHS_NOCARRIER,   "NOCARRIER" },
+    { DHS_NAK,         "NAK" },
+    { DHS_EXPIRE,      "EXPIRE" },
+    { DHS_RECONFIGURE, "RECONFIGURE" },
+    { DHS_CARRIER,     "CARRIER" },
+    { DHS_STATIC,      "STATIC" },
+    { DHS_3RDPARTY,    "3RDPARTY" },
+    { DHS_IPV4LL,      "IPV4LL" },
+    { DHS_INFORM,      "INFORM" },
+    { DHS_BOUND,       "BOUND" },
+    { DHS_RENEW,       "RENEW" },
+    { DHS_REBIND,      "REBIND" },
+    { DHS_REBOOT,      "REBOOT" },
+    { DHS_ROUTERADVERT,        "ROUTERADVERT" },
+    { DHS_BOUND,       "DELEGATED" },
+    { DHS_UNKNOWN,     NULL    }
+};
 
 static ssize_t
 dhcpcd_command_fd(DHCPCD_CONNECTION *con,
@@ -459,46 +497,47 @@ strtobool(const char *var)
            strcmp(var, "no") == 0) ? false : true;
 }
 
-static const char *
+static unsigned int
 get_status(DHCPCD_CONNECTION *con)
 {
        DHCPCD_IF *i;
-       const char *status;
+       unsigned int status;
 
        assert(con);
        if (con->command_fd == -1)
-               return "down";
+               return DHC_DOWN;
 
        if (con->listen_fd == -1)
-               return "opened";
+               return DHC_OPENED;
 
        if (con->interfaces == NULL)
-               return "initialised";
+               return DHC_INITIALISED;
 
-       status = "disconnected";
+       status = DHC_DISCONNECTED;
        for (i = con->interfaces; i; i = i->next) {
                if (i->up) {
-                       if (strcmp(i->type, "link")) {
-                               status = "connected";
+                       if (i->type == DHT_LINK) {
+                               status = DHC_CONNECTED;
                                break;
                        } else
-                               status = "connecting";
+                               status = DHC_CONNECTING;
                }
        }
        return status;
 }
 
 static void
-update_status(DHCPCD_CONNECTION *con, const char *nstatus)
+update_status(DHCPCD_CONNECTION *con, unsigned int nstatus)
 {
 
        assert(con);
-       if (nstatus == NULL)
+       if (nstatus == DHC_UNKNOWN)
                nstatus = get_status(con);
-       if (con->status == NULL || strcmp(nstatus, con->status)) {
+       if (con->status != nstatus) {
                con->status = nstatus;
                if (con->status_cb)
-                       con->status_cb(con, con->status, con->status_context);
+                       con->status_cb(con, con->status,
+                           dhcpcd_cstates[con->status], con->status_context);
        }
 }
 
@@ -523,7 +562,7 @@ dhcpcd_interface_names(DHCPCD_CONNECTION *con, size_t *nnames)
 
        n = 0;
        for (i = con->interfaces; i; i = i->next) {
-               if (strcmp(i->type, "link") == 0)
+                if (i->type == DHT_LINK)
                        n++;
        }
        names = malloc(sizeof(char *) * (n + 1));
@@ -531,7 +570,7 @@ dhcpcd_interface_names(DHCPCD_CONNECTION *con, size_t *nnames)
                return NULL;
        n = 0;
        for (i = con->interfaces; i; i = i->next) {
-               if (strcmp(i->type, "link") == 0) {
+               if (i->type == DHT_LINK) {
                        names[n] = strdup(i->ifname);
                        if (names[n] == NULL) {
                                dhcpcd_freev(names);
@@ -585,7 +624,7 @@ dhcpcd_interface_names_sorted(DHCPCD_CONNECTION *con)
 }
 
 DHCPCD_IF *
-dhcpcd_get_if(DHCPCD_CONNECTION *con, const char *ifname, const char *type)
+dhcpcd_get_if(DHCPCD_CONNECTION *con, const char *ifname, unsigned int type)
 {
        DHCPCD_IF *i;
 
@@ -594,19 +633,74 @@ dhcpcd_get_if(DHCPCD_CONNECTION *con, const char *ifname, const char *type)
        assert(type);
 
        for (i = con->interfaces; i; i = i->next)
-               if (strcmp(i->ifname, ifname) == 0 &&
-                   strcmp(i->type, type) == 0)
+               if (i->type == type && strcmp(i->ifname, ifname) == 0)
                        return i;
        return NULL;
 }
 
+static unsigned int
+dhcpcd_reason_to_state1(const char *reason, int *isdhcp6)
+{
+       unsigned int i;
+       char rbuf[32], *p;
+
+       assert(reason);
+
+       /* Take a copy of reason and trim 6 from the end
+        * so our DHCPv6 state can match DHCP states */
+       if (strlcpy(rbuf, reason, sizeof(rbuf)) >= sizeof(rbuf))
+               return DHS_UNKNOWN;
+       p = rbuf + (strlen(rbuf) - 1);
+       if (*p == '6') {
+               if (isdhcp6)
+                       *isdhcp6 = 1;
+               *p = '\0';
+       } else if (isdhcp6)
+               *isdhcp6 = 0;
+
+       for (i = 0; dhcpcd_states[i].val != DHS_UNKNOWN; i++) {
+               if (strcmp(rbuf, dhcpcd_states[i].str) == 0)
+                       return dhcpcd_states[i].val;
+       }
+       return DHS_UNKNOWN;
+}
+
+static void
+dhcpcd_reason_to_statetype(const char *reason,
+    unsigned int *state, unsigned int *type)
+{
+       int isdhcp6;
+
+       assert(state);
+       assert(type);
+       *state = dhcpcd_reason_to_state1(reason, &isdhcp6);
+       switch (*state) {
+       case DHS_UNKNOWN:
+       case DHS_PREINIT:
+       case DHS_CARRIER:
+       case DHS_NOCARRIER:
+       case DHS_DEPARTED:
+       case DHS_STOPPED:
+               *type = DHT_LINK;
+               return;
+       case DHS_ROUTERADVERT:
+               *type =  DHT_RA;
+               return;
+       }
+
+       if (isdhcp6)
+               *type = DHT_DHCP6;
+       else
+               *type = DHT_IPV4;
+}
+
 static DHCPCD_IF *
 dhcpcd_new_if(DHCPCD_CONNECTION *con, char *data, size_t len)
 {
-       const char *ifname, *ifclass, *reason, *type, *order, *flags;
+       const char *ifname, *ifclass, *reason, *order, *flags;
+       unsigned int state, type;
        char *orderdup, *o, *p;
        DHCPCD_IF *e, *i, *l, *n, *nl;
-       int ti;
        bool addedi;
 
 #if 0
@@ -627,15 +721,15 @@ dhcpcd_new_if(DHCPCD_CONNECTION *con, char *data, size_t len)
                errno = ESRCH;
                return NULL;
        }
+       dhcpcd_reason_to_statetype(reason, &state, &type);
+
        ifclass = get_value(data, len, "ifclass");
        /* Skip pseudo interfaces */
        if (ifclass && *ifclass != '\0') {
                errno = ENOTSUP;
                return NULL;
        }
-       if (strcmp(reason, "RECONFIGURE") == 0 ||
-           strcmp(reason, "INFORM") == 0 || strcmp(reason, "INFORM6") == 0)
-       {
+       if (state == DHS_RECONFIGURE || state == DHS_INFORM) {
                errno = ENOTSUP;
                return NULL;
        }
@@ -645,31 +739,16 @@ dhcpcd_new_if(DHCPCD_CONNECTION *con, char *data, size_t len)
                return NULL;
        }
 
-       if (strcmp(reason, "PREINIT") == 0 ||
-           strcmp(reason, "UNKNOWN") == 0 ||
-           strcmp(reason, "CARRIER") == 0 ||
-           strcmp(reason, "NOCARRIER") == 0 ||
-           strcmp(reason, "DEPARTED") == 0 ||
-           strcmp(reason, "STOPPED") == 0)
-               type = "link";
-       else if (strcmp(reason, "ROUTERADVERT") == 0)
-               type = "ra";
-       else if (reason[strlen(reason) - 1] == '6')
-               type = "dhcp6";
-       else
-               type = "ipv4";
-
        i = NULL;
        /* Remove all instances on carrier drop */
-        if (strcmp(reason, "NOCARRIER") == 0 ||
-            strcmp(reason, "DEPARTED") == 0 ||
-            strcmp(reason, "STOPPED") == 0)
+        if (state == DHS_NOCARRIER ||
+           state == DHS_DEPARTED || state == DHS_STOPPED)
         {
                 l = NULL;
                 for (e = con->interfaces; e; e = n) {
                         n = e->next;
                         if (strcmp(e->ifname, ifname) == 0) {
-                                if (strcmp(e->type, type) == 0)
+                                if (e->type == type)
                                         l = i = e;
                                 else {
                                         if (l)
@@ -681,9 +760,9 @@ dhcpcd_new_if(DHCPCD_CONNECTION *con, char *data, size_t len)
                         } else
                                 l = e;
                 }
-        } else if (strcmp(type, "link")) {
+        } else if (type != DHT_LINK) {
                /* If link is down, ignore it */
-               e = dhcpcd_get_if(con, ifname, "link");
+               e = dhcpcd_get_if(con, ifname, DHT_LINK);
                if (e && !e->up)
                        return NULL;
        }
@@ -696,9 +775,7 @@ dhcpcd_new_if(DHCPCD_CONNECTION *con, char *data, size_t len)
         if (i == NULL) {
                 l = NULL;
                 for (e = con->interfaces; e; e = e->next) {
-                        if (strcmp(e->ifname, ifname) == 0 &&
-                            strcmp(e->type, type) == 0)
-                        {
+                        if (e->type == type && strcmp(e->ifname, ifname) == 0) {
                                 i = e;
                                 break;
                         }
@@ -726,14 +803,14 @@ dhcpcd_new_if(DHCPCD_CONNECTION *con, char *data, size_t len)
        i->data_len = len;
        i->ifname = ifname;
        i->type = type;
+       i->state = state;
        i->reason = reason;
        flags = dhcpcd_get_value(i, "ifflags");
        if (flags)
-               i->flags = (unsigned int)strtoul(flags, NULL, 0);
+               i->ifflags = (unsigned int)strtoul(flags, NULL, 0);
        else
-               i->flags = 0;
-       if (strcmp(reason, "CARRIER") == 0 ||
-           strcmp(reason, "DELEGATED6") == 0)
+               i->ifflags = 0;
+       if (state == DHS_CARRIER || state == DHS_DELEGATED)
                i->up = true;
        else
                i->up = strtobool(dhcpcd_get_value(i, "if_up"));
@@ -747,11 +824,10 @@ dhcpcd_new_if(DHCPCD_CONNECTION *con, char *data, size_t len)
        p = orderdup;
        addedi = false;
         while ((o = strsep(&p, " ")) != NULL) {
-                for (ti = 0; dhcpcd_types[ti]; ti++) {
+                for (type = 0; type < DHT_MAX; type++) {
                         l = NULL;
                         for (e = con->interfaces; e; e = e->next) {
-                                if (strcmp(e->ifname, o) == 0 &&
-                                    strcmp(e->type, dhcpcd_types[ti]) == 0)
+                                if (e->type == type && strcmp(e->ifname, o) == 0)
                                         break;
                                 l = e;
                         }
@@ -844,7 +920,7 @@ dhcpcd_dispatch(DHCPCD_CONNECTION *con)
 
        /* Have to call update_status last as it could
         * cause the interface to be destroyed. */
-       update_status(con, NULL);
+       update_status(con, DHC_UNKNOWN);
 }
 
 DHCPCD_CONNECTION *
@@ -931,7 +1007,7 @@ dhcpcd_open(DHCPCD_CONNECTION *con, bool privileged)
 
        con->open = true;
        con->privileged = privileged;
-       update_status(con, NULL);
+       update_status(con, DHC_UNKNOWN);
 
        con->listen_fd = dhcpcd_connect(path, SOCK_NONBLOCK);
        if (con->listen_fd == -1)
@@ -948,7 +1024,7 @@ dhcpcd_open(DHCPCD_CONNECTION *con, bool privileged)
        for (n = 0; n < nifs; n++)
                dhcpcd_read_if(con, con->command_fd);
 
-       update_status(con, NULL);
+       update_status(con, DHC_UNKNOWN);
 
        return con->listen_fd;
 
@@ -973,11 +1049,13 @@ dhcpcd_privileged(DHCPCD_CONNECTION *con)
        return con->privileged;
 }
 
-const char *
-dhcpcd_status(DHCPCD_CONNECTION *con)
+unsigned int
+dhcpcd_status(DHCPCD_CONNECTION *con, const char **status)
 {
 
        assert(con);
+       if (status)
+               *status = dhcpcd_cstates[con->status];
        return con->status;
 }
 
@@ -1009,7 +1087,8 @@ dhcpcd_set_if_callback(DHCPCD_CONNECTION *con,
 
 void
 dhcpcd_set_status_callback(DHCPCD_CONNECTION *con,
-    void (*cb)(DHCPCD_CONNECTION *, const char *, void *), void *ctx)
+    void (*cb)(DHCPCD_CONNECTION *, unsigned int, const char *, void *),
+    void *ctx)
 {
 
        assert(con);
@@ -1054,7 +1133,7 @@ dhcpcd_close(DHCPCD_CONNECTION *con)
        if (con->listen_fd != -1)
                shutdown(con->listen_fd, SHUT_RDWR);
 
-       update_status(con, "down");
+       update_status(con, DHC_DOWN);
 
        if (con->command_fd != -1) {
                close(con->command_fd);
@@ -1107,14 +1186,16 @@ dhcpcd_if_message(DHCPCD_IF *i, bool *new_msg)
 
        assert(i);
        /* Don't report non SLAAC configurations */
-       if (strcmp(i->type, "ra") == 0 && i->up &&
+       if (i->type == DHT_RA && i->up &&
            dhcpcd_get_value(i, "ra1_prefix") == NULL)
                return NULL;
 
        showssid = false;
-       if (strcmp(i->reason, "EXPIRE") == 0)
+       switch (i->state) {
+       case DHS_EXPIRE:
                reason = _("Expired");
-       else if (strcmp(i->reason, "CARRIER") == 0) {
+               break;
+       case DHS_CARRIER:
                if (i->wireless) {
                        showssid = true;
                        reason = _("Associated with");
@@ -1132,7 +1213,8 @@ dhcpcd_if_message(DHCPCD_IF *i, bool *new_msg)
                                return NULL;
                        reason = _("Link is up, configuring");
                }
-       } else if (strcmp(i->reason, "NOCARRIER") == 0) {
+               break;
+       case DHS_NOCARRIER:
                if (i->wireless) {
                        if (i->ssid) {
                                reason = _("Disassociated from");
@@ -1141,22 +1223,28 @@ dhcpcd_if_message(DHCPCD_IF *i, bool *new_msg)
                                reason = _("Not associated");
                } else
                        reason = _("Link is down");
-       } else if (strcmp(i->reason, "DEPARTED") == 0)
+               break;
+       case DHS_DEPARTED:
                reason = _("Departed");
-       else if (strcmp(i->reason, "UNKNOWN") == 0)
+               break;
+       case DHS_UNKNOWN:
                reason = _("Unknown link state");
-       else if (strcmp(i->reason, "FAIL") == 0)
+               break;
+       case DHS_FAIL:
                reason = _("Automatic configuration not possible");
-       else if (strcmp(i->reason, "3RDPARTY") == 0)
+               break;
+       case DHS_3RDPARTY:
                reason = _("Waiting for 3rd Party configuration");
+               break;
+       }
 
        if (reason == NULL) {
                if (i->up) {
-                       if (strcmp(i->reason, "DELEGATED6") == 0)
+                       if (i->state == DHS_DELEGATED)
                                reason = _("Delegated");
                        else
                                reason = _("Configured");
-               } else if (strcmp(i->type, "ra") == 0)
+               } else if (i->type == DHT_RA)
                        reason = "Expired RA";
                else
                        reason = i->reason;
index 92635eefc3b73628282a216e410f5c9ceda1812c..6cfc2579f63d3623a5f1120cfa2da112f539b8a2 100644 (file)
@@ -67,6 +67,48 @@ extern "C" {
 #define TYPESIZE               8
 #define REASONSIZE             16
 
+#define DHC_UNKNOWN             0
+#define DHC_DOWN                1
+#define DHC_OPENED              2
+#define DHC_INITIALISED                 3
+#define DHC_DISCONNECTED        4
+#define DHC_CONNECTING          5
+#define DHC_CONNECTED           6
+#define DHC_MAX                         7
+
+#define DHT_UNKNOWN             0
+#define DHT_LINK                1
+#define DHT_DHCP                2
+#define DHT_IPV4                2
+#define DHT_RA                  3
+#define DHT_DHCP6               4
+#define DHT_MAX                         5
+
+#define DHS_UNKNOWN             0
+#define DHS_DUMP                1
+#define DHS_TEST                2
+#define DHS_STOPPED             3
+#define DHS_FAIL                4
+#define DHS_STOP                5
+#define DHS_PREINIT             6
+#define DHS_DEPARTED            7
+#define DHS_NOCARRIER           8
+#define DHS_NAK                         9
+#define DHS_EXPIRE             10
+#define DHS_RECONFIGURE                11
+#define DHS_CARRIER            12
+#define DHS_STATIC             13
+#define DHS_3RDPARTY           14
+#define DHS_IPV4LL             15
+#define DHS_INFORM             16
+#define DHS_BOUND              17
+#define DHS_RENEW              18
+#define DHS_REBIND             19
+#define        DHS_REBOOT              20
+#define DHS_ROUTERADVERT       21
+#define DHS_DELEGATED          22
+#define DHR_MAX                        23
+
 typedef struct dhcpcd_wi_avs {
        int value;
        int average;
@@ -93,10 +135,11 @@ typedef struct dhcpcd_wi_scan {
 typedef struct dhcpcd_if {
        struct dhcpcd_if *next;
        const char *ifname;
-       const char *type;
+       unsigned int type;
+       unsigned int state;
        const char *reason;
 
-       unsigned int flags;
+       unsigned int ifflags;
        bool up;
        bool wireless;
        const char *ssid;
@@ -112,10 +155,11 @@ typedef struct dhcpcd_if {
 typedef struct dhcpcd_if {
        struct dhcpcd_if *next;
        const char *ifname;
-       const char *type;
+       unsigned int type;
+       unsigned int state;
        const char *reason;
 
-       int flags;
+       unsigned int ifflags;
        bool up;
        bool wireless;
        const char *ssid;
@@ -165,12 +209,13 @@ typedef struct dhcpcd_connection {
 
        void (*if_cb)(DHCPCD_IF *, void *);
        void *if_context;
-       void (*status_cb)(struct dhcpcd_connection *, const char *, void *);
+       void (*status_cb)(struct dhcpcd_connection *,
+           unsigned int, const char *, void *);
        void *status_context;
        bool wpa_started;
        void (*wi_scanresults_cb)(DHCPCD_WPA *, void *);
        void *wi_scanresults_context;
-       void (*wpa_status_cb)(DHCPCD_WPA *, const char *, void *);
+       void (*wpa_status_cb)(DHCPCD_WPA *, unsigned int, const char *, void *);
        void *wpa_status_context;
 
        char *buf;
@@ -181,7 +226,7 @@ typedef struct dhcpcd_connection {
        char *error;
        int err;
        int errors;
-       const char *status;
+       unsigned int status;
 
        char *cffile;
 } DHCPCD_CONNECTION;
@@ -196,7 +241,7 @@ DHCPCD_CONNECTION * dhcpcd_new(void);
 const char * dhcpcd_version(DHCPCD_CONNECTION *);
 void dhcpcd_set_progname(DHCPCD_CONNECTION *, const char *);
 const char * dhcpcd_get_progname(const DHCPCD_CONNECTION *);
-const char * dhcpcd_status(DHCPCD_CONNECTION *);
+unsigned int dhcpcd_status(DHCPCD_CONNECTION *, const char **);
 const char * dhcpcd_cffile(DHCPCD_CONNECTION *);
 bool dhcpcd_realloc(DHCPCD_CONNECTION *, size_t);
 int dhcpcd_open(DHCPCD_CONNECTION *, bool priv);
@@ -205,7 +250,7 @@ void dhcpcd_free(DHCPCD_CONNECTION *);
 void dhcpcd_set_if_callback(DHCPCD_CONNECTION *,
     void (*)(DHCPCD_IF *, void *), void *);
 void dhcpcd_set_status_callback(DHCPCD_CONNECTION *,
-    void (*)(DHCPCD_CONNECTION *, const char *, void *), void *);
+    void (*)(DHCPCD_CONNECTION *, unsigned int, const char *, void *), void *);
 int dhcpcd_get_fd(DHCPCD_CONNECTION *);
 bool dhcpcd_privileged(DHCPCD_CONNECTION *);
 void dhcpcd_dispatch(DHCPCD_CONNECTION *);
@@ -213,7 +258,7 @@ DHCPCD_IF * dhcpcd_interfaces(DHCPCD_CONNECTION *);
 char **dhcpcd_interface_names(DHCPCD_CONNECTION *, size_t *);
 void dhcpcd_freev(char **);
 char **dhcpcd_interface_names_sorted(DHCPCD_CONNECTION *);
-DHCPCD_IF * dhcpcd_get_if(DHCPCD_CONNECTION *, const char *, const char *);
+DHCPCD_IF * dhcpcd_get_if(DHCPCD_CONNECTION *, const char *, unsigned int);
 DHCPCD_CONNECTION * dhcpcd_if_connection(DHCPCD_IF *);
 const char *dhcpcd_get_value(const DHCPCD_IF *, const char *);
 const char *dhcpcd_get_prefix_value(const DHCPCD_IF *, const char *,
@@ -242,7 +287,7 @@ void dhcpcd_wpa_if_event(DHCPCD_IF *);
 void dhcpcd_wpa_set_scan_callback(DHCPCD_CONNECTION *,
     void (*)(DHCPCD_WPA *, void *), void *);
 void dhcpcd_wpa_set_status_callback(DHCPCD_CONNECTION *,
-    void (*)(DHCPCD_WPA *, const char *, void *), void *);
+    void (*)(DHCPCD_WPA *, unsigned int, const char *, void *), void *);
 int dhcpcd_wi_scan_compare(DHCPCD_WI_SCAN *a, DHCPCD_WI_SCAN *b);
 DHCPCD_WI_SCAN * dhcpcd_wi_scans(DHCPCD_IF *);
 bool dhcpcd_wi_associated(DHCPCD_IF *i, DHCPCD_WI_SCAN *s);
index db734d461590b296f86000a5514b921973de7e6a..282a26921eadc3bed2dda9ee7e1507d2fb267827 100644 (file)
@@ -853,7 +853,8 @@ dhcpcd_wpa_close(DHCPCD_WPA *wpa)
        shutdown(wpa->listen_fd, SHUT_RDWR);
 
        if (wpa->con->wpa_status_cb)
-               wpa->con->wpa_status_cb(wpa, "down",
+               wpa->con->wpa_status_cb(wpa,
+                   DHC_DOWN, "down",
                    wpa->con->wpa_status_context);
 
        close(wpa->command_fd);
@@ -916,7 +917,7 @@ dhcpcd_wpa_if(DHCPCD_WPA *wpa)
 {
 
        assert(wpa);
-       return dhcpcd_get_if(wpa->con, wpa->ifname, "link");
+       return dhcpcd_get_if(wpa->con, wpa->ifname, DHT_LINK);
 }
 
 int
@@ -991,7 +992,8 @@ dhcpcd_wpa_set_scan_callback(DHCPCD_CONNECTION *con,
 
 
 void dhcpcd_wpa_set_status_callback(DHCPCD_CONNECTION * con,
-    void (*cb)(DHCPCD_WPA *, const char *, void *), void *context)
+    void (*cb)(DHCPCD_WPA *, unsigned int, const char *, void *),
+    void *context)
 {
 
        assert(con);
@@ -1037,10 +1039,8 @@ dhcpcd_wpa_if_event(DHCPCD_IF *i)
        DHCPCD_WPA *wpa;
 
        assert(i);
-       if (strcmp(i->type, "link") == 0) {
-               if (strcmp(i->reason, "STOPPED") == 0 ||
-                   strcmp(i->reason, "DEPARTED") == 0)
-               {
+       if (i->type == DHT_LINK) {
+               if (i->state == DHS_STOPPED || i->state == DHS_DEPARTED) {
                        wpa = dhcpcd_wpa_find(i->con, i->ifname);
                        if (wpa)
                                dhcpcd_wpa_close(wpa);