Add dhcpcd_wpa_ping to test if wpa_supplicant is alive.
authorRoy Marples <roy@marples.name>
Tue, 2 Dec 2014 22:14:55 +0000 (22:14 +0000)
committerRoy Marples <roy@marples.name>
Tue, 2 Dec 2014 22:14:55 +0000 (22:14 +0000)
Style.

src/libdhcpcd/dhcpcd.h
src/libdhcpcd/wpa.c

index c980aa30cbdbbf5fd6b4268bd22aa1a10c9785d4..ff8f2483dd0781a56f5fe94e1577553449b3428f 100644 (file)
@@ -48,6 +48,7 @@ extern "C" {
 #endif
 
 #define DHCPCD_RETRYOPEN       100     /* milliseconds */
+#define DHCPCD_WPA_PING                500     /* milliseconds */
 #define DHCPCD_WI_HIST_MAX     10      /* Recall 10 scans for averages */
 
 /* Each non printable byte of the SSID is represented as \000 */
@@ -243,6 +244,7 @@ int dhcpcd_wpa_find_network_new(DHCPCD_WPA *, const char *);
 bool dhcpcd_wpa_command(DHCPCD_WPA *, const char *);
 bool dhcpcd_wpa_command_arg(DHCPCD_WPA *, const char *, const char *);
 
+bool dhcpcd_wpa_ping(DHCPCD_WPA *);
 bool dhcpcd_wpa_scan(DHCPCD_WPA *);
 bool dhcpcd_wpa_reconfigure(DHCPCD_WPA *);
 bool dhcpcd_wpa_reassociate(DHCPCD_WPA *);
index 8d53351c504ea74282b7b6674c0b7e5233376a99..59ccb9dac68228d6106e94cadbb8a01fe6f5b16f 100644 (file)
@@ -98,7 +98,7 @@ wpa_cmd(int fd, const char *cmd, char *buffer, size_t len)
        if (buffer)
                *buffer = '\0';
        bytes = write(fd, cmd, strlen(cmd));
-       if (bytes == -1 || bytes == 0)
+       if (bytes == -1)
                return -1;
        if (buffer == NULL || len == 0)
                return 0;
@@ -129,6 +129,17 @@ dhcpcd_wpa_command(DHCPCD_WPA *wpa, const char *cmd)
 }
 
 bool
+dhcpcd_wpa_ping(DHCPCD_WPA *wpa)
+{
+       char buf[10];
+       ssize_t bytes;
+
+       bytes = wpa_cmd(wpa->command_fd, "PING", buf, sizeof(buf));
+       return (bytes == -1 || bytes == 0 ||
+           strcmp(buf, "PONG\n")) ? false : true;
+}
+
+bool
 dhcpcd_wpa_command_arg(DHCPCD_WPA *wpa, const char *cmd, const char *arg)
 {
        size_t cmdlen, nlen;
@@ -979,7 +990,7 @@ dhcpcd_wpa_dispatch(DHCPCD_WPA *wpa)
 
        assert(wpa);
        bytes = (size_t)read(wpa->listen_fd, buffer, sizeof(buffer));
-       if ((ssize_t)bytes == -1 || bytes == 0) {
+       if ((ssize_t)bytes == -1) {
                dhcpcd_wpa_close(wpa);
                return;
        }
@@ -988,16 +999,19 @@ dhcpcd_wpa_dispatch(DHCPCD_WPA *wpa)
        bytes = strlen(buffer);
        if (buffer[bytes - 1] == ' ')
                buffer[--bytes] = '\0';
-       for (p = buffer + 1; *p != '\0'; p++)
+       for (p = buffer + 1; *p != '\0'; p++) {
                if (*p == '>') {
                        p++;
                        break;
                }
+       }
+
        if (strcmp(p, "CTRL-EVENT-SCAN-RESULTS") == 0 &&
            wpa->con->wi_scanresults_cb)
                wpa->con->wi_scanresults_cb(wpa,
                    wpa->con->wi_scanresults_context);
-       return;
+       else if (strcmp(p, "CTRL-EVENT-TERMINATING") == 0)
+               dhcpcd_wpa_close(wpa);
 }
 
 void
@@ -1106,9 +1120,9 @@ dhcpcd_wpa_configure1(DHCPCD_WPA *wpa, DHCPCD_WI_SCAN *s, const char *psk)
                retval = DHCPCD_WPA_SUCCESS;
        else
                retval = DHCPCD_WPA_ERR_WRITE;
-       /* Selecting a network disbales the others.
+       /* Selecting a network disables the others.
         * This should not be saved. */
-       if (!dhcpcd_wpa_network_select(wpa, id))
+       if (!dhcpcd_wpa_network_select(wpa, id) && retval == DHCPCD_WPA_SUCCESS)
                return DHCPCD_WPA_ERR_SELECT;
        return retval;
 }