libiwinfo: fail after 5 tries when there's no response from wpa_supplicant
[project/luci.git] / contrib / package / iwinfo / src / iwinfo_nl80211.c
index 5d1add9..fa714e9 100644 (file)
@@ -333,18 +333,38 @@ static char * nl80211_hostapd_info(const char *ifname)
        return NULL;
 }
 
-static char * nl80211_wpasupp_info(const char *ifname, const char *cmd)
+static inline int nl80211_wpactl_recv(int sock, char *buf, int blen)
 {
-       int sock = -1, len;
+       fd_set rfds;
+       struct timeval tv = { 2, 0 };
+
+       FD_ZERO(&rfds);
+       FD_SET(sock, &rfds);
+
+       memset(buf, 0, blen);
+
+
+       if( select(sock + 1, &rfds, NULL, NULL, &tv) < 0 )
+               return -1;
+
+       if( !FD_ISSET(sock, &rfds) )
+               return -1;
+
+       return recv(sock, buf, blen, 0);
+}
+
+static char * nl80211_wpactl_info(const char *ifname, const char *cmd,
+                                                                  const char *event)
+{
+       int numtry = 0;
+       int sock = -1;
        char *rv = NULL;
        size_t remote_length, local_length;
-       static char buffer[1024] = { 0 };
+       static char buffer[10240] = { 0 };
 
-       struct timeval tv = { 2, 0 };
        struct sockaddr_un local = { 0 };
        struct sockaddr_un remote = { 0 };
 
-       fd_set rfds;
 
        sock = socket(PF_UNIX, SOCK_DGRAM, 0);
        if( sock < 0 )
@@ -367,25 +387,26 @@ static char * nl80211_wpasupp_info(const char *ifname, const char *cmd)
        if( bind(sock, (struct sockaddr *) &local, local_length) )
                goto out;
 
-       send(sock, cmd, strlen(cmd), 0);
 
-       while( 1 )
-       {
-               FD_ZERO(&rfds);
-               FD_SET(sock, &rfds);
+       send(sock, "ATTACH", 6, 0);
 
-               if( select(sock + 1, &rfds, NULL, NULL, &tv) < 0 )
-                       goto out;
+       if( nl80211_wpactl_recv(sock, buffer, sizeof(buffer)) <= 0 )
+               goto out;
 
-               if( !FD_ISSET(sock, &rfds) )
-                       break;
 
-               if( (len = recv(sock, buffer, sizeof(buffer), 0)) <= 0 )
-                       goto out;
+       send(sock, cmd, strlen(cmd), 0);
 
-               buffer[len] = 0;
+       while( numtry++ < 5 )
+       {
+               if( nl80211_wpactl_recv(sock, buffer, sizeof(buffer)) <= 0 )
+               {
+                       if( event )
+                               continue;
+
+                       break;
+               }
 
-               if( buffer[0] != '<' )
+               if( (!event && buffer[0] != '<') || strstr(buffer, event) )
                        break;
        }
 
@@ -426,7 +447,9 @@ static char * nl80211_phy2ifname(const char *ifname)
        DIR *d;
        struct dirent *e;
 
-       if( !strncmp(ifname, "phy", 3) )
+       if( !ifname )
+               return NULL;
+       else if( !strncmp(ifname, "phy", 3) )
                phyidx = atoi(&ifname[3]);
        else if( !strncmp(ifname, "radio", 5) )
                phyidx = atoi(&ifname[5]);
@@ -604,11 +627,11 @@ int nl80211_get_channel(const char *ifname, int *buf)
 {
        char *first;
 
-       if( wext_get_channel(ifname, buf) &&
-           NULL != (first = nl80211_phy2ifname(nl80211_ifname2phy(ifname))) )
-       {
+       if( !wext_get_channel(ifname, buf) )
+               return 0;
+
+       else if( (first = nl80211_phy2ifname(nl80211_ifname2phy(ifname))) != NULL )
                return wext_get_channel(first, buf);
-       }
 
        return -1;
 }
@@ -617,11 +640,11 @@ int nl80211_get_frequency(const char *ifname, int *buf)
 {
        char *first;
 
-       if( wext_get_channel(ifname, buf) &&
-           NULL != (first = nl80211_phy2ifname(nl80211_ifname2phy(ifname))) )
-       {
+       if( !wext_get_frequency(ifname, buf) )
+               return 0;
+
+       else if( (first = nl80211_phy2ifname(nl80211_ifname2phy(ifname))) != NULL )
                return wext_get_frequency(first, buf);
-       }
 
        return -1;
 }
@@ -752,10 +775,11 @@ int nl80211_get_signal(const char *ifname, int *buf)
        return -1;
 }
 
-int nl80211_get_noise(const char *ifname, int *buf)
+static int nl80211_get_noise_cb(struct nl_msg *msg, void *arg)
 {
-       int rv = -1;
-       struct nl80211_msg_conveyor *req, *res;
+       int8_t *noise = arg;
+       struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
+       struct nlattr *tb[NL80211_ATTR_MAX + 1];
        struct nlattr *si[NL80211_SURVEY_INFO_MAX + 1];
 
        static struct nla_policy sp[NL80211_SURVEY_INFO_MAX + 1] = {
@@ -763,28 +787,48 @@ int nl80211_get_noise(const char *ifname, int *buf)
                [NL80211_SURVEY_INFO_NOISE]     = { .type = NLA_U8  },
        };
 
+       nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
+               genlmsg_attrlen(gnlh, 0), NULL);
+
+       if (!tb[NL80211_ATTR_SURVEY_INFO])
+               return NL_SKIP;
+
+       if (nla_parse_nested(si, NL80211_SURVEY_INFO_MAX,
+                                                tb[NL80211_ATTR_SURVEY_INFO], sp))
+               return NL_SKIP;
+
+       if (!si[NL80211_SURVEY_INFO_NOISE])
+               return NL_SKIP;
+
+       if (!*noise || si[NL80211_SURVEY_INFO_IN_USE])
+               *noise = (int8_t)nla_get_u8(si[NL80211_SURVEY_INFO_NOISE]);
+
+       return NL_SKIP;
+}
+
+
+int nl80211_get_noise(const char *ifname, int *buf)
+{
+       int8_t noise;
+       struct nl80211_msg_conveyor *req;
+
        req = nl80211_msg(ifname, NL80211_CMD_GET_SURVEY, NLM_F_DUMP);
-       if( req )
+       if (req)
        {
-               res = nl80211_send(req);
-               if( res )
+               noise = 0;
+
+               nl80211_cb(req, nl80211_get_noise_cb, &noise);
+               nl80211_send(req);
+               nl80211_free(req);
+
+               if (noise)
                {
-                       if( res->attr[NL80211_ATTR_SURVEY_INFO] )
-                       {
-                               if( !nla_parse_nested(si, NL80211_SURVEY_INFO_MAX,
-                                               res->attr[NL80211_ATTR_SURVEY_INFO], sp) &&
-                                       si[NL80211_SURVEY_INFO_NOISE] )
-                               {
-                                       *buf = (int8_t)nla_get_u8(si[NL80211_SURVEY_INFO_NOISE]);
-                                       rv = 0;
-                               }
-                       }
-                       nl80211_free(res);
+                       *buf = noise;
+                       return 0;
                }
-               nl80211_free(req);
        }
 
-       return rv;
+       return -1;
 }
 
 int nl80211_get_quality(const char *ifname, int *buf)
@@ -842,10 +886,34 @@ int nl80211_get_encryption(const char *ifname, char *buf)
        /* Hostapd */
        if( (res = nl80211_hostapd_info(ifname)) )
        {
-               if( (val = nl80211_getval(ifname, res, "auth_algs")) && (val > 0) )
-               {
+               if( (val = nl80211_getval(ifname, res, "wpa")) != NULL )
+                       c->wpa_version = atoi(val);
+
+               val = nl80211_getval(ifname, res, "wpa_key_mgmt");
+
+               if( !val || strstr(val, "PSK") )
+                       c->auth_suites |= IWINFO_KMGMT_PSK;
+
+               if( val && strstr(val, "EAP") )
+                       c->auth_suites |= IWINFO_KMGMT_8021x;
+
+               if( val && strstr(val, "NONE") )
                        c->auth_suites |= IWINFO_KMGMT_NONE;
 
+               if( (val = nl80211_getval(ifname, res, "wpa_pairwise")) != NULL )
+               {
+                       if( strstr(val, "TKIP") )
+                               c->pair_ciphers |= IWINFO_CIPHER_TKIP;
+
+                       if( strstr(val, "CCMP") )
+                               c->pair_ciphers |= IWINFO_CIPHER_CCMP;
+
+                       if( strstr(val, "NONE") )
+                               c->pair_ciphers |= IWINFO_CIPHER_NONE;
+               }
+
+               if( (val = nl80211_getval(ifname, res, "auth_algs")) != NULL )
+               {
                        switch(atoi(val)) {
                                case 1:
                                        c->auth_algs |= IWINFO_AUTH_OPEN;
@@ -877,42 +945,8 @@ int nl80211_get_encryption(const char *ifname, char *buf)
                                                c->pair_ciphers |= IWINFO_CIPHER_WEP104;
                                }
                        }
-
-                       c->group_ciphers = c->pair_ciphers;
-
-                       return 0;
-               }
-
-
-               if( (val = nl80211_getval(ifname, res, "wpa")) != NULL )
-                       c->wpa_version = atoi(val);
-
-
-               val = nl80211_getval(ifname, res, "wpa_key_mgmt");
-
-               if( !val || strstr(val, "PSK") )
-                       c->auth_suites |= IWINFO_KMGMT_PSK;
-
-               if( val && strstr(val, "EAP") )
-                       c->auth_suites |= IWINFO_KMGMT_8021x;
-
-               if( val && strstr(val, "NONE") )
-                       c->auth_suites |= IWINFO_KMGMT_NONE;
-
-
-               if( (val = nl80211_getval(ifname, res, "wpa_pairwise")) != NULL )
-               {
-                       if( strstr(val, "TKIP") )
-                               c->pair_ciphers |= IWINFO_CIPHER_TKIP;
-
-                       if( strstr(val, "CCMP") )
-                               c->pair_ciphers |= IWINFO_CIPHER_CCMP;
-
-                       if( strstr(val, "NONE") )
-                               c->pair_ciphers |= IWINFO_CIPHER_NONE;
                }
 
-
                c->group_ciphers = c->pair_ciphers;
                c->enabled = (c->auth_algs || c->auth_suites) ? 1 : 0;
 
@@ -920,7 +954,7 @@ int nl80211_get_encryption(const char *ifname, char *buf)
        }
 
        /* WPA supplicant */
-       else if( (res = nl80211_wpasupp_info(ifname, "STATUS")) &&
+       else if( (res = nl80211_wpactl_info(ifname, "STATUS", NULL)) &&
                 (val = nl80211_getval(NULL, res, "pairwise_cipher")) )
        {
                /* WEP */
@@ -1160,11 +1194,7 @@ int nl80211_get_txpwrlist(const char *ifname, char *buf, int *len)
 static void nl80211_get_scancrypto(const char *spec,
        struct iwinfo_crypto_entry *c)
 {
-       if( strstr(spec, "OPEN") )
-       {
-               c->enabled = 0;
-       }
-       else
+       if( strstr(spec, "WPA") || strstr(spec, "WEP") )
        {
                c->enabled = 1;
 
@@ -1205,6 +1235,10 @@ static void nl80211_get_scancrypto(const char *spec,
 
                c->group_ciphers = c->pair_ciphers;
        }
+       else
+       {
+               c->enabled = 0;
+       }
 }
 
 int nl80211_get_scanlist(const char *ifname, char *buf, int *len)
@@ -1236,11 +1270,9 @@ int nl80211_get_scanlist(const char *ifname, char *buf, int *len)
        struct iwinfo_scanlist_entry *e = (struct iwinfo_scanlist_entry *)buf;
 
        /* WPA supplicant */
-       if( (res = nl80211_wpasupp_info(ifname, "SCAN")) && !strcmp(res, "OK\n") )
+       if( (res = nl80211_wpactl_info(ifname, "SCAN", "CTRL-EVENT-SCAN-RESULTS")) )
        {
-               sleep(2);
-
-               if( (res = nl80211_wpasupp_info(ifname, "SCAN_RESULTS")) )
+               if( (res = nl80211_wpactl_info(ifname, "SCAN_RESULTS", NULL)) )
                {
                        nl80211_get_quality_max(ifname, &qmax);
 
@@ -1249,7 +1281,7 @@ int nl80211_get_scanlist(const char *ifname, char *buf, int *len)
 
                        count = 0;
 
-                       while( sscanf(res, "%17s %d %d %255s %127[^\n]\n",
+                       while( sscanf(res, "%17s %d %d %255s%*[ \t]%127[^\n]\n",
                                      bssid, &freq, &rssi, cipher, ssid) > 0 )
                        {
                                /* BSSID */
@@ -1302,6 +1334,10 @@ int nl80211_get_scanlist(const char *ifname, char *buf, int *len)
 
                                count++;
                                e++;
+
+                               memset(ssid, 0, sizeof(ssid));
+                               memset(bssid, 0, sizeof(bssid));
+                               memset(cipher, 0, sizeof(cipher));
                        }
 
                        *len = count * sizeof(struct iwinfo_scanlist_entry);