Split listening to WPA events out of dhcpcd_open as its
authorRoy Marples <roy@marples.name>
Wed, 3 Sep 2014 13:21:22 +0000 (13:21 +0000)
committerRoy Marples <roy@marples.name>
Wed, 3 Sep 2014 13:21:22 +0000 (13:21 +0000)
not needd for dhcpcd-online.

src/dhcpcd-gtk/main.c
src/dhcpcd-qt/dhcpcd-qt.cpp
src/libdhcpcd/dhcpcd.c
src/libdhcpcd/dhcpcd.h
src/libdhcpcd/wpa.c

index fb6575713126b3914fcce9c091e4d312b3ab521f..54090fe18e731b4a0bfd488e7828e79b1278bbbc 100644 (file)
@@ -354,7 +354,7 @@ dhcpcd_status_cb(DHCPCD_CONNECTION *con, const char *status, _unused void *data)
                            dhcpcd_version(con));
                        refresh = true;
                } else
-                       refresh = false;
+                       refresh = g_strcmp0(last, "opened") ? false : true;
                update_online(con, refresh);
        }
 
@@ -389,9 +389,10 @@ dhcpcd_try_open(gpointer data)
        con = (DHCPCD_CONNECTION *)data;
        fd = dhcpcd_open(con);
        if (fd == -1) {
-               if (errno != last_error)
+               if (errno != last_error) {
                        g_critical("dhcpcd_open: %s", strerror(errno));
-               last_error = errno;
+                       last_error = errno;
+               }
                return TRUE;
        }
 
@@ -400,6 +401,9 @@ dhcpcd_try_open(gpointer data)
                return TRUE;
        }
 
+       /* Start listening to WPA events */
+       dhcpcd_wpa_start(con);
+
        return FALSE;
 }
 
index 5d08f9f73a7cacbb57feca45de507f41e0defe56..c38db32e4c6c68b7d8fe1c452acf32de55b40dc4 100644 (file)
@@ -181,12 +181,14 @@ void DhcpcdQt::statusCallback(const char *status)
        } else {
                bool refresh;
 
-               if ((lastStatus == NULL || strcmp(lastStatus, "down") == 0)) {
+               if (lastStatus == NULL || strcmp(lastStatus, "down") == 0) {
                        qDebug("Connected to dhcpcd-%s", dhcpcd_version(con));
                        refresh = true;
                } else
-                       refresh = false;
+                       refresh = strcmp(lastStatus, "opened") ? false : true;
+               printf ("refresh %d\n", refresh);
                updateOnline(refresh);
+               printf ("updated\n");
        }
 
        free(lastStatus);
@@ -330,6 +332,9 @@ void DhcpcdQt::tryOpen() {
                return;
        }
 
+       /* Start listening to WPA events */
+       dhcpcd_wpa_start(con);
+
        if (retryOpenTimer) {
                delete retryOpenTimer;
                retryOpenTimer = NULL;
index 4f2e74090d46f0f2c2a8e67f5450e695b2a6b507..1bc2b13cbb4a62d6f707ecbf780776eb5598ef06 100644 (file)
@@ -244,9 +244,15 @@ get_status(DHCPCD_CONNECTION *con)
        const char *status;
 
        assert(con);
-       if (con->command_fd == -1 || con->listen_fd == -1)
+       if (con->command_fd == -1)
                return "down";
 
+       if (con->listen_fd == -1)
+               return "opened";
+
+       if (con->interfaces == NULL)
+               return "initialised";
+
        status = "disconnected";
        for (i = con->interfaces; i; i = i->next) {
                if (i->up) {
@@ -570,12 +576,11 @@ dhcpcd_open(DHCPCD_CONNECTION *con)
        DHCPCD_IF *i;
 
        assert(con);
-       if (con->listen_fd != -1) {
-               if (!con->open) {
-                       errno = EISCONN;
-                       return -1;
-               }
-               return con->listen_fd;
+       if (con->open) {
+               if (con->listen_fd != -1)
+                       return con->listen_fd;
+               errno = EISCONN;
+               return -1;
        }
        /* We need to block the command fd */
        con->command_fd = dhcpcd_connect(0);
@@ -591,26 +596,26 @@ dhcpcd_open(DHCPCD_CONNECTION *con)
        if (dhcpcd_command(con, "--getconfigfile", &con->cffile) <= 0)
                goto err_exit;
 
+       con->open = true;
+       update_status(con, NULL);
+
        con->listen_fd = dhcpcd_connect(SOCK_NONBLOCK);
        if (con->listen_fd == -1)
                goto err_exit;
-       dhcpcd_command_fd(con, con->listen_fd, "--listen", NULL);
 
+       dhcpcd_command_fd(con, con->listen_fd, "--listen", NULL);
        dhcpcd_command_fd(con, con->command_fd, "--getinterfaces", NULL);
        bytes = read(con->command_fd, cmd, sizeof(nifs));
        if (bytes != sizeof(nifs))
                goto err_exit;
-
        memcpy(&nifs, cmd, sizeof(nifs));
        /* We don't dispatch each interface here as that
         * causes too much notification spam when the GUI starts */
-       for (n = 0; n < nifs; n++) {
+       for (n = 0; n < nifs; n++)
                i = dhcpcd_read_if(con, con->command_fd);
-               if (i)
-                       dhcpcd_wpa_if_event(i);
-       }
+
        update_status(con, NULL);
-       con->open = true;
+
        return con->listen_fd;
 
 err_exit:
index f05e6dc2a236896141ef69ef981afa8dac851ba4..788bd23a7b666622f9d4527c54caab1908e93cc4 100644 (file)
@@ -144,6 +144,7 @@ typedef struct dhcpcd_connection {
        void *if_context;
        void (*status_cb)(struct dhcpcd_connection *, 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 *);
@@ -196,6 +197,7 @@ ssize_t dhcpcd_command_arg(DHCPCD_CONNECTION *, const char *, const char *,
 #define dhcpcd_rebind(c, i)    dhcpcd_command_arg((c), "-n", (i), NULL)
 #define dhcpcd_release(c, i)   dhcpcd_command_arg((c), "-k", (i), NULL)
 
+void dhcpcd_wpa_start(DHCPCD_CONNECTION *);
 DHCPCD_WPA *dhcpcd_wpa_find(DHCPCD_CONNECTION *, const char *);
 DHCPCD_WPA *dhcpcd_wpa_new(DHCPCD_CONNECTION *, const char *);
 DHCPCD_CONNECTION *dhcpcd_wpa_connection(DHCPCD_WPA *);
index d47632170d5500a016f8edea6f54925b1e784d24..50ffae563d16fb4466c4a3f300009c6c81711cf9 100644 (file)
@@ -644,10 +644,22 @@ dhcpcd_wpa_if_event(DHCPCD_IF *i)
                        wpa = dhcpcd_wpa_find(i->con, i->ifname);
                        if (wpa)
                                dhcpcd_wpa_close(wpa);
-               } else if (i->up) {
+               } else if (i->up && i->con->wpa_started) {
                        wpa = dhcpcd_wpa_new(i->con, i->ifname);
                        if (wpa && wpa->listen_fd == -1)
                                dhcpcd_wpa_open(wpa);
                }
        }
 }
+
+void
+dhcpcd_wpa_start(DHCPCD_CONNECTION *con)
+{
+       DHCPCD_IF *i;
+
+       assert(con);
+       con->wpa_started = true;
+
+       for (i = con->interfaces; i; i = i->next)
+               dhcpcd_wpa_if_event(i);
+}