From bdabb047b46719800ef5f46caceeb232f824d087 Mon Sep 17 00:00:00 2001 From: Sebastian Wicki Date: Fri, 26 Jul 2013 13:53:26 +0200 Subject: [PATCH] Use the numerical network id to identify a network * This fixes a bug where a REESTABLISHED event is triggered instead of the DISCONNECT/CONNECT events if the two networks use the same SSID. --- wpa_actiond.c | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/wpa_actiond.c b/wpa_actiond.c index d60d885..427789b 100644 --- a/wpa_actiond.c +++ b/wpa_actiond.c @@ -147,16 +147,33 @@ static int min(int i, int j) { return i < j ? i : j; } -static void get_status(struct wpa_ctrl *ctrl, char *ssid, char *idstr) { +static void get_status(struct wpa_ctrl *ctrl, int *id, char *ssid, char *idstr) { char reply[BUFLEN], *pos = &reply[0], *pos2; size_t reply_len = BUFLEN-1; *ssid = '\0'; + *id = -1; if(wpa_ctrl_request(ctrl, "STATUS", strlen("STATUS"), reply, &reply_len, NULL)) return; reply[reply_len] = '\0'; while(1) { + pos = strstr(pos, "id="); + if(pos == NULL || pos == reply || *(pos-1) == '\n') + break; + pos += strlen("id="); + } + + if(pos != NULL) { + pos += strlen("id="); + *id = strtol(pos, &pos2, 10); + if(*pos2 != '\n' && *pos2 != '\0') + *id = -1; + } + + pos = &reply[0]; + + while(1) { pos = strstr(pos, "ssid="); if(pos == NULL || pos == reply || *(pos-1) == '\n') break; @@ -287,12 +304,15 @@ static void loop(const char *iface, const char *ctrlpath, const int disconnect_t struct timeval timeout; struct timeval ping_timeout; /* save ssid */ + int id, old_id; char ssid[SSIDLEN], old_ssid[SSIDLEN]; char idstr[IDSTRLEN], old_idstr[IDSTRLEN]; /* path to control socket */ char *ctrlsock = NULL; int ctrlsocklen; + id = -1; + old_id = -1; ssid[0] = '\0'; old_ssid[0] = '\0'; idstr[0] = '\0'; @@ -336,7 +356,7 @@ static void loop(const char *iface, const char *ctrlpath, const int disconnect_t } /* set initial ssid */ - get_status(ctrl, ssid, idstr); + get_status(ctrl, &id, ssid, idstr); logevent(WPA_ACTIOND_LOG_STARTED, iface, ""); @@ -391,9 +411,9 @@ static void loop(const char *iface, const char *ctrlpath, const int disconnect_t switch(ev) { case WPA_ACTIOND_EVENT_CONNECTED: /* connect to network */ - get_status(ctrl, ssid, idstr); + get_status(ctrl, &id, ssid, idstr); if(state == WPA_ACTIOND_STATE_CONNECTION_LOST) { - if(strcmp(old_ssid, ssid)) { + if(id != old_id) { logevent(WPA_ACTIOND_LOG_DISCONNECTED, iface, old_ssid); action(WPA_ACTIOND_ACTION_DISCONNECT, iface, old_ssid, old_idstr, wpa_ctrl_get_fd(ctrl), script); logevent(WPA_ACTIOND_LOG_CONNECTED, iface, ssid); @@ -416,6 +436,7 @@ static void loop(const char *iface, const char *ctrlpath, const int disconnect_t logevent(WPA_ACTIOND_LOG_CONNECTION_LOST, iface, ssid); action(WPA_ACTIOND_ACTION_CONNECTION_LOST, iface, ssid, idstr, wpa_ctrl_get_fd(ctrl), script); state = WPA_ACTIOND_STATE_CONNECTION_LOST; + old_id = id; strncpy(old_ssid, ssid, SSIDLEN); strncpy(old_idstr, idstr, IDSTRLEN); @@ -429,7 +450,7 @@ static void loop(const char *iface, const char *ctrlpath, const int disconnect_t break; case WPA_ACTIOND_EVENT_FAILED: /* connecting failed */ - get_status(ctrl, ssid, idstr); + get_status(ctrl, &id, ssid, idstr); logevent(WPA_ACTIOND_LOG_FAILED, iface, ssid); action(WPA_ACTIOND_ACTION_FAILED, iface, ssid, idstr, wpa_ctrl_get_fd(ctrl), script); break; -- 1.8.3.2