libiwinfo: rework nl80211 interface code, plug remaining memory leaks
authorJo-Philipp Wich <jow@openwrt.org>
Wed, 7 Sep 2011 04:17:00 +0000 (04:17 +0000)
committerJo-Philipp Wich <jow@openwrt.org>
Wed, 7 Sep 2011 04:17:00 +0000 (04:17 +0000)
contrib/package/iwinfo/Makefile
contrib/package/iwinfo/src/iwinfo_nl80211.c
contrib/package/iwinfo/src/iwinfo_nl80211.h

index cffa8f9be18165420e5b8c8d4dbd70f5df1f3379..014e7a31b085535a44a68c401d883836e4772bd2 100644 (file)
@@ -7,7 +7,7 @@
 include $(TOPDIR)/rules.mk
 
 PKG_NAME:=libiwinfo
-PKG_RELEASE:=15
+PKG_RELEASE:=16
 
 PKG_BUILD_DIR := $(BUILD_DIR)/$(PKG_NAME)
 
index 280714ee528663a2c63769811d233964ea4eb18c..abaeb511d0cce98db47c7c8e843d12e8298e74c6 100644 (file)
@@ -34,16 +34,16 @@ static int nl80211_init(void)
 {
        int err, fd;
 
-       if( !nls )
+       if (!nls)
        {
                nls = malloc(sizeof(struct nl80211_state));
-               if( !nls ) {
+               if (!nls) {
                        err = -ENOMEM;
                        goto err;
                }
 
                nls->nl_sock = nl_socket_alloc();
-               if( !nls->nl_sock ) {
+               if (!nls->nl_sock) {
                        err = -ENOMEM;
                        goto err;
                }
@@ -54,7 +54,7 @@ static int nl80211_init(void)
                }
 
                fd = nl_socket_get_fd(nls->nl_sock);
-               if( fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC) < 0 )
+               if (fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC) < 0)
                {
                        err = -EINVAL;
                        goto err;
@@ -66,7 +66,7 @@ static int nl80211_init(void)
                }
 
                nls->nl80211 = genl_ctrl_search_by_name(nls->nl_cache, "nl80211");
-               if( !nls->nl80211 )
+               if (!nls->nl80211)
                {
                        err = -ENOENT;
                        goto err;
@@ -105,28 +105,17 @@ static int nl80211_msg_ack(struct nl_msg *msg, void *arg)
 
 static int nl80211_msg_response(struct nl_msg *msg, void *arg)
 {
-       struct nl80211_msg_conveyor *cv = arg;
-
-       nlmsg_get(msg);
-
-       cv->msg = msg;
-       cv->hdr = nlmsg_data(nlmsg_hdr(cv->msg));
-
-       nla_parse(cv->attr, NL80211_ATTR_MAX,
-               genlmsg_attrdata(cv->hdr, 0),
-               genlmsg_attrlen(cv->hdr, 0), NULL);
-
        return NL_SKIP;
 }
 
 static void nl80211_free(struct nl80211_msg_conveyor *cv)
 {
-       if( cv )
+       if (cv)
        {
-               if( cv->cb )
+               if (cv->cb)
                        nl_cb_put(cv->cb);
 
-               if( cv->msg )
+               if (cv->msg)
                        nlmsg_free(cv->msg);
 
                cv->cb  = NULL;
@@ -142,40 +131,38 @@ static struct nl80211_msg_conveyor * nl80211_msg(const char *ifname, int cmd, in
        struct nl_msg *req = NULL;
        struct nl_cb *cb = NULL;
 
-       if( nl80211_init() < 0 )
+       if (nl80211_init() < 0)
                goto err;
 
-       if( !strncmp(ifname, "phy", 3) )
+       if (!strncmp(ifname, "phy", 3))
                phyidx = atoi(&ifname[3]);
-       else if( !strncmp(ifname, "radio", 5) )
+       else if (!strncmp(ifname, "radio", 5))
                phyidx = atoi(&ifname[5]);
-       else if( !strncmp(ifname, "mon.", 4) )
+       else if (!strncmp(ifname, "mon.", 4))
                ifidx = if_nametoindex(&ifname[4]);
        else
                ifidx = if_nametoindex(ifname);
 
-       if( (ifidx < 0) && (phyidx < 0) )
+       if ((ifidx < 0) && (phyidx < 0))
                return NULL;
 
        req = nlmsg_alloc();
-       if( !req )
+       if (!req)
                goto err;
 
        cb = nl_cb_alloc(NL_CB_DEFAULT);
-       if( !cb )
+       if (!cb)
                goto err;
 
        genlmsg_put(req, 0, 0, genl_family_get_id(nls->nl80211), 0,
                flags, cmd, 0);
 
-       if( ifidx > -1 )
+       if (ifidx > -1)
                NLA_PUT_U32(req, NL80211_ATTR_IFINDEX, ifidx);
 
-       if( phyidx > -1 )
+       if (phyidx > -1)
                NLA_PUT_U32(req, NL80211_ATTR_WIPHY, phyidx);
 
-       nlmsg_get(req);
-
        cv.msg       = req;
        cv.cb        = cb;
        cv.custom_cb = 0;
@@ -184,10 +171,10 @@ static struct nl80211_msg_conveyor * nl80211_msg(const char *ifname, int cmd, in
 
 err:
 nla_put_failure:
-       if( cb )
+       if (cb)
                nl_cb_put(cb);
 
-       if( req )
+       if (req)
                nlmsg_free(req);
 
        return NULL;
@@ -205,10 +192,10 @@ static struct nl80211_msg_conveyor * nl80211_send(struct nl80211_msg_conveyor *c
        static struct nl80211_msg_conveyor rcv;
        int err = 1;
 
-       if( !cv->custom_cb )
+       if (!cv->custom_cb)
                nl_cb_set(cv->cb, NL_CB_VALID, NL_CB_CUSTOM, nl80211_msg_response, &rcv);
 
-       if( nl_send_auto_complete(nls->nl_sock, cv->msg) < 0 )
+       if (nl_send_auto_complete(nls->nl_sock, cv->msg) < 0)
                goto err;
 
        nl_cb_err(cv->cb,               NL_CB_CUSTOM, nl80211_msg_error,  &err);
@@ -227,6 +214,17 @@ err:
        return NULL;
 }
 
+static struct nlattr ** nl80211_parse(struct nl_msg *msg)
+{
+       struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
+       static struct nlattr *attr[NL80211_ATTR_MAX + 1];
+
+       nla_parse(attr, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
+               genlmsg_attrlen(gnlh, 0), NULL);
+
+       return attr;
+}
+
 static int nl80211_freq2channel(int freq)
 {
     if (freq == 2484)
@@ -250,28 +248,28 @@ static char * nl80211_getval(const char *ifname, const char *buf, const char *ke
 
        for( i = 0, len = strlen(buf); i < len; i++ )
        {
-               if( !lkey[0] && (buf[i] == ' ' || buf[i] == '\t') )
+               if (!lkey[0] && (buf[i] == ' ' || buf[i] == '\t'))
                {
                        ln++;
                }
-               else if( !lkey[0] && (buf[i] == '=') )
+               else if (!lkey[0] && (buf[i] == '='))
                {
-                       if( (&buf[i] - ln) > 0 )
+                       if ((&buf[i] - ln) > 0)
                                memcpy(lkey, ln, min(sizeof(lkey) - 1, &buf[i] - ln));
                }
-               else if( buf[i] == '\n' )
+               else if (buf[i] == '\n')
                {
-                       if( lkey[0] )
+                       if (lkey[0])
                        {
                                memcpy(lval, ln + strlen(lkey) + 1,
                                        min(sizeof(lval) - 1, &buf[i] - ln - strlen(lkey) - 1));
 
-                               if( (ifname != NULL ) &&
+                               if ((ifname != NULL) &&
                                    (!strcmp(lkey, "interface") || !strcmp(lkey, "bss")) )
                                {
                                        matched_if = !strcmp(lval, ifname);
                                }
-                               else if( matched_if && !strcmp(lkey, key) )
+                               else if (matched_if && !strcmp(lkey, key))
                                {
                                        return lval;
                                }
@@ -286,24 +284,29 @@ static char * nl80211_getval(const char *ifname, const char *buf, const char *ke
        return NULL;
 }
 
+static int nl80211_ifname2phy_cb(struct nl_msg *msg, void *arg)
+{
+       char *buf = arg;
+       struct nlattr **attr = nl80211_parse(msg);
+
+       if (attr[NL80211_ATTR_WIPHY_NAME])
+               sprintf(buf, "%s", nla_data(attr[NL80211_ATTR_WIPHY_NAME]));
+       else
+               buf[0] = 0;
+
+       return NL_SKIP;
+}
+
 static char * nl80211_ifname2phy(const char *ifname)
 {
        static char phy[32] = { 0 };
-       struct nl80211_msg_conveyor *req, *res;
+       struct nl80211_msg_conveyor *req;
 
        req = nl80211_msg(ifname, NL80211_CMD_GET_WIPHY, 0);
-       if( req )
+       if (req)
        {
-               res = nl80211_send(req);
-               if( res )
-               {
-                       if( res->attr[NL80211_ATTR_WIPHY_NAME] )
-                       {
-                               snprintf(phy, sizeof(phy), "%s",
-                                        nla_get_string(res->attr[NL80211_ATTR_WIPHY_NAME]));
-                       }
-                       nl80211_free(res);
-               }
+               nl80211_cb(req, nl80211_ifname2phy_cb, phy);
+               nl80211_send(req);
                nl80211_free(req);
        }
 
@@ -317,11 +320,11 @@ static char * nl80211_hostapd_info(const char *ifname)
        static char buf[4096] = { 0 };
        FILE *conf;
 
-       if( (phy = nl80211_ifname2phy(ifname)) != NULL )
+       if ((phy = nl80211_ifname2phy(ifname)) != NULL)
        {
                snprintf(path, sizeof(path), "/var/run/hostapd-%s.conf", phy);
 
-               if( (conf = fopen(path, "r")) != NULL )
+               if ((conf = fopen(path, "r")) != NULL)
                {
                        fread(buf, sizeof(buf) - 1, 1, conf);
                        fclose(conf);
@@ -344,10 +347,10 @@ static inline int nl80211_wpactl_recv(int sock, char *buf, int blen)
        memset(buf, 0, blen);
 
 
-       if( select(sock + 1, &rfds, NULL, NULL, &tv) < 0 )
+       if (select(sock + 1, &rfds, NULL, NULL, &tv) < 0)
                return -1;
 
-       if( !FD_ISSET(sock, &rfds) )
+       if (!FD_ISSET(sock, &rfds))
                return -1;
 
        return recv(sock, buf, blen, 0);
@@ -367,30 +370,30 @@ static char * nl80211_wpactl_info(const char *ifname, const char *cmd,
 
 
        sock = socket(PF_UNIX, SOCK_DGRAM, 0);
-       if( sock < 0 )
+       if (sock < 0)
                return NULL;
 
        remote.sun_family = AF_UNIX;
        remote_length = sizeof(remote.sun_family) + sprintf(remote.sun_path,
                "/var/run/wpa_supplicant-%s/%s", ifname, ifname);
 
-       if( fcntl(sock, F_SETFD, fcntl(sock, F_GETFD) | FD_CLOEXEC) < 0 )
+       if (fcntl(sock, F_SETFD, fcntl(sock, F_GETFD) | FD_CLOEXEC) < 0)
                goto out;
 
-       if( connect(sock, (struct sockaddr *) &remote, remote_length) )
+       if (connect(sock, (struct sockaddr *) &remote, remote_length))
                goto out;
 
        local.sun_family = AF_UNIX;
        local_length = sizeof(local.sun_family) + sprintf(local.sun_path,
                "/var/run/iwinfo-%s-%d", ifname, getpid());
 
-       if( bind(sock, (struct sockaddr *) &local, local_length) )
+       if (bind(sock, (struct sockaddr *) &local, local_length))
                goto out;
 
 
        send(sock, "ATTACH", 6, 0);
 
-       if( nl80211_wpactl_recv(sock, buffer, sizeof(buffer)) <= 0 )
+       if (nl80211_wpactl_recv(sock, buffer, sizeof(buffer)) <= 0)
                goto out;
 
 
@@ -398,15 +401,15 @@ static char * nl80211_wpactl_info(const char *ifname, const char *cmd,
 
        while( numtry++ < 5 )
        {
-               if( nl80211_wpactl_recv(sock, buffer, sizeof(buffer)) <= 0 )
+               if (nl80211_wpactl_recv(sock, buffer, sizeof(buffer)) <= 0)
                {
-                       if( event )
+                       if (event)
                                continue;
 
                        break;
                }
 
-               if( (!event && buffer[0] != '<') || strstr(buffer, event) )
+               if ((!event && buffer[0] != '<') || strstr(buffer, event))
                        break;
        }
 
@@ -415,7 +418,7 @@ static char * nl80211_wpactl_info(const char *ifname, const char *cmd,
 out:
        close(sock);
 
-       if( local.sun_family )
+       if (local.sun_family)
                unlink(local.sun_path);
 
        return rv;
@@ -427,9 +430,9 @@ static inline int nl80211_readint(const char *path)
        int rv = -1;
        char buffer[16];
 
-       if( (fd = open(path, O_RDONLY)) > -1 )
+       if ((fd = open(path, O_RDONLY)) > -1)
        {
-               if( read(fd, buffer, sizeof(buffer)) > 0 )
+               if (read(fd, buffer, sizeof(buffer)) > 0)
                        rv = atoi(buffer);
 
                close(fd);
@@ -447,23 +450,23 @@ static char * nl80211_phy2ifname(const char *ifname)
        DIR *d;
        struct dirent *e;
 
-       if( !ifname )
+       if (!ifname)
                return NULL;
-       else if( !strncmp(ifname, "phy", 3) )
+       else if (!strncmp(ifname, "phy", 3))
                phyidx = atoi(&ifname[3]);
-       else if( !strncmp(ifname, "radio", 5) )
+       else if (!strncmp(ifname, "radio", 5))
                phyidx = atoi(&ifname[5]);
 
-       if( phyidx > -1 )
+       if (phyidx > -1)
        {
-               if( (d = opendir("/sys/class/net")) != NULL )
+               if ((d = opendir("/sys/class/net")) != NULL)
                {
                        while( (e = readdir(d)) != NULL )
                        {
                                snprintf(buffer, sizeof(buffer),
                                        "/sys/class/net/%s/phy80211/index", e->d_name);
 
-                               if( nl80211_readint(buffer) == phyidx )
+                               if (nl80211_readint(buffer) == phyidx)
                                {
                                        snprintf(buffer, sizeof(buffer),
                                                "/sys/class/net/%s/ifindex", e->d_name);
@@ -492,19 +495,16 @@ static char * nl80211_ifadd(const char *ifname)
        struct nl80211_msg_conveyor *req, *res;
 
        req = nl80211_msg(ifname, NL80211_CMD_NEW_INTERFACE, 0);
-       if( req )
+       if (req)
        {
                snprintf(nif, sizeof(nif), "tmp.%s", ifname);
 
                NLA_PUT_STRING(req->msg, NL80211_ATTR_IFNAME, nif);
                NLA_PUT_U32(req->msg, NL80211_ATTR_IFTYPE, NL80211_IFTYPE_STATION);
 
-               res = nl80211_send(req);
-               if( res )
-               {
-                       rv = nif;
-                       nl80211_free(res);
-               }
+               nl80211_send(req);
+
+               rv = nif;
 
        nla_put_failure:
                nl80211_free(req);
@@ -518,11 +518,11 @@ static void nl80211_ifdel(const char *ifname)
        struct nl80211_msg_conveyor *req;
 
        req = nl80211_msg(ifname, NL80211_CMD_DEL_INTERFACE, 0);
-       if( req )
+       if (req)
        {
                NLA_PUT_STRING(req->msg, NL80211_ATTR_IFNAME, ifname);
 
-               nl80211_free(nl80211_send(req));
+               nl80211_send(req);
 
        nla_put_failure:
                nl80211_free(req);
@@ -535,18 +535,18 @@ static void nl80211_hostapd_hup(const char *ifname)
        char buf[32];
        char *phy = nl80211_ifname2phy(ifname);
 
-       if( phy )
+       if (phy)
        {
                snprintf(buf, sizeof(buf), "/var/run/wifi-%s.pid", phy);
-               if( (fd = open(buf, O_RDONLY)) > 0 )
+               if ((fd = open(buf, O_RDONLY)) > 0)
                {
-                       if( read(fd, buf, sizeof(buf)) > 0 )
+                       if (read(fd, buf, sizeof(buf)) > 0)
                                pid = atoi(buf);
 
                        close(fd);
                }
 
-               if( pid > 0 )
+               if (pid > 0)
                        kill(pid, 1);
        }
 }
@@ -559,12 +559,12 @@ int nl80211_probe(const char *ifname)
 
 void nl80211_close(void)
 {
-       if( nls )
+       if (nls)
        {
-               if( nls->nl_sock )
+               if (nls->nl_sock)
                        nl_socket_free(nls->nl_sock);
 
-               if( nls->nl_cache )
+               if (nls->nl_cache)
                        nl_cache_free(nls->nl_cache);
 
                free(nls);
@@ -581,7 +581,7 @@ int nl80211_get_ssid(const char *ifname, char *buf)
 {
        char *ssid;
 
-       if( !wext_get_ssid(ifname, buf) )
+       if (!wext_get_ssid(ifname, buf))
        {
                return 0;
        }
@@ -600,12 +600,12 @@ int nl80211_get_bssid(const char *ifname, char *buf)
        char *bssid;
        unsigned char mac[6];
 
-       if( !wext_get_bssid(ifname, buf) )
+       if (!wext_get_bssid(ifname, buf))
        {
                return 0;
        }
-       else if( (bssid = nl80211_hostapd_info(ifname)) &&
-                (bssid = nl80211_getval(ifname, bssid, "bssid")) )
+       else if((bssid = nl80211_hostapd_info(ifname)) &&
+                (bssid = nl80211_getval(ifname, bssid, "bssid")))
        {
                mac[0] = strtol(&bssid[0],  NULL, 16);
                mac[1] = strtol(&bssid[3],  NULL, 16);
@@ -627,10 +627,10 @@ int nl80211_get_channel(const char *ifname, int *buf)
 {
        char *first;
 
-       if( !wext_get_channel(ifname, buf) )
+       if (!wext_get_channel(ifname, buf))
                return 0;
 
-       else if( (first = nl80211_phy2ifname(nl80211_ifname2phy(ifname))) != NULL )
+       else if ((first = nl80211_phy2ifname(nl80211_ifname2phy(ifname))) != NULL)
                return wext_get_channel(first, buf);
 
        return -1;
@@ -640,10 +640,10 @@ int nl80211_get_frequency(const char *ifname, int *buf)
 {
        char *first;
 
-       if( !wext_get_frequency(ifname, buf) )
+       if (!wext_get_frequency(ifname, buf))
                return 0;
 
-       else if( (first = nl80211_phy2ifname(nl80211_ifname2phy(ifname))) != NULL )
+       else if ((first = nl80211_phy2ifname(nl80211_ifname2phy(ifname))) != NULL)
                return wext_get_frequency(first, buf);
 
        return -1;
@@ -660,9 +660,7 @@ static int nl80211_fill_signal_cb(struct nl_msg *msg, void *arg)
        int8_t dbm;
        int16_t mbit;
        struct nl80211_rssi_rate *rr = arg;
-
-       struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
-       struct nlattr *attr[NL80211_ATTR_MAX + 1];
+       struct nlattr **attr = nl80211_parse(msg);
        struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1];
        struct nlattr *rinfo[NL80211_RATE_INFO_MAX + 1];
 
@@ -686,26 +684,23 @@ static int nl80211_fill_signal_cb(struct nl_msg *msg, void *arg)
                [NL80211_RATE_INFO_SHORT_GI]     = { .type = NLA_FLAG },
        };
 
-       nla_parse(attr, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
-                 genlmsg_attrlen(gnlh, 0), NULL);
-
-       if( attr[NL80211_ATTR_STA_INFO] )
+       if (attr[NL80211_ATTR_STA_INFO])
        {
                if( !nla_parse_nested(sinfo, NL80211_STA_INFO_MAX,
                                attr[NL80211_ATTR_STA_INFO], stats_policy) )
                {
-                       if( sinfo[NL80211_STA_INFO_SIGNAL] )
+                       if (sinfo[NL80211_STA_INFO_SIGNAL])
                        {
                                dbm = nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL]);
                                rr->rssi = rr->rssi ? (int8_t)((rr->rssi + dbm) / 2) : dbm;
                        }
 
-                       if( sinfo[NL80211_STA_INFO_TX_BITRATE] )
+                       if (sinfo[NL80211_STA_INFO_TX_BITRATE])
                        {
                                if( !nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX,
                                                sinfo[NL80211_STA_INFO_TX_BITRATE], rate_policy) )
                                {
-                                       if( rinfo[NL80211_RATE_INFO_BITRATE] )
+                                       if (rinfo[NL80211_RATE_INFO_BITRATE])
                                        {
                                                mbit = nla_get_u16(rinfo[NL80211_RATE_INFO_BITRATE]);
                                                rr->rate = rr->rate
@@ -742,7 +737,7 @@ static void nl80211_fill_signal(const char *ifname, struct nl80211_rssi_rate *r)
                                if (req)
                                {
                                        nl80211_cb(req, nl80211_fill_signal_cb, r);
-                                       nl80211_free(nl80211_send(req));
+                                       nl80211_send(req);
                                        nl80211_free(req);
                                }
                        }
@@ -756,7 +751,7 @@ int nl80211_get_bitrate(const char *ifname, int *buf)
 {
        struct nl80211_rssi_rate rr;
 
-       if( !wext_get_bitrate(ifname, buf) )
+       if (!wext_get_bitrate(ifname, buf))
                return 0;
 
        nl80211_fill_signal(ifname, &rr);
@@ -774,7 +769,7 @@ int nl80211_get_signal(const char *ifname, int *buf)
 {
        struct nl80211_rssi_rate rr;
 
-       if( !wext_get_signal(ifname, buf) )
+       if (!wext_get_signal(ifname, buf))
                return 0;
 
        nl80211_fill_signal(ifname, &rr);
@@ -791,8 +786,7 @@ int nl80211_get_signal(const char *ifname, int *buf)
 static int nl80211_get_noise_cb(struct nl_msg *msg, void *arg)
 {
        int8_t *noise = arg;
-       struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
-       struct nlattr *tb[NL80211_ATTR_MAX + 1];
+       struct nlattr **tb = nl80211_parse(msg);
        struct nlattr *si[NL80211_SURVEY_INFO_MAX + 1];
 
        static struct nla_policy sp[NL80211_SURVEY_INFO_MAX + 1] = {
@@ -800,9 +794,6 @@ static int nl80211_get_noise_cb(struct nl_msg *msg, void *arg)
                [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;
 
@@ -831,7 +822,7 @@ int nl80211_get_noise(const char *ifname, int *buf)
                noise = 0;
 
                nl80211_cb(req, nl80211_get_noise_cb, &noise);
-               nl80211_free(nl80211_send(req));
+               nl80211_send(req);
                nl80211_free(req);
 
                if (noise)
@@ -848,15 +839,15 @@ int nl80211_get_quality(const char *ifname, int *buf)
 {
        int signal;
 
-       if( wext_get_quality(ifname, buf) )
+       if (wext_get_quality(ifname, buf))
        {
                *buf = 0;
 
-               if( !nl80211_get_signal(ifname, &signal) )
+               if (!nl80211_get_signal(ifname, &signal))
                {
                        /* A positive signal level is usually just a quality
                         * value, pass through as-is */
-                       if( signal >= 0 )
+                       if (signal >= 0)
                        {
                                *buf = signal;
                        }
@@ -866,9 +857,9 @@ int nl80211_get_quality(const char *ifname, int *buf)
                         * by adding 110 to the signal level */
                        else
                        {
-                               if( signal < -110 )
+                               if (signal < -110)
                                        signal = -110;
-                               else if( signal > -40 )
+                               else if (signal > -40)
                                        signal = -40;
 
                                *buf = (signal + 110);
@@ -881,7 +872,7 @@ int nl80211_get_quality(const char *ifname, int *buf)
 
 int nl80211_get_quality_max(const char *ifname, int *buf)
 {
-       if( wext_get_quality_max(ifname, buf) )
+       if (wext_get_quality_max(ifname, buf))
                /* The cfg80211 wext compat layer assumes a maximum
                 * quality of 70 */
                *buf = 70;
@@ -901,12 +892,12 @@ int nl80211_get_encryption(const char *ifname, char *buf)
            (val = nl80211_getval(NULL, res, "pairwise_cipher")) )
        {
                /* WEP */
-               if( strstr(val, "WEP") )
+               if (strstr(val, "WEP"))
                {
-                       if( strstr(val, "WEP-40") )
+                       if (strstr(val, "WEP-40"))
                                c->pair_ciphers |= IWINFO_CIPHER_WEP40;
 
-                       else if( strstr(val, "WEP-104") )
+                       else if (strstr(val, "WEP-104"))
                                c->pair_ciphers |= IWINFO_CIPHER_WEP104;
 
                        c->enabled       = 1;
@@ -919,57 +910,57 @@ int nl80211_get_encryption(const char *ifname, char *buf)
                /* WPA */
                else
                {
-                       if( strstr(val, "TKIP") )
+                       if (strstr(val, "TKIP"))
                                c->pair_ciphers |= IWINFO_CIPHER_TKIP;
 
-                       else if( strstr(val, "CCMP") )
+                       else if (strstr(val, "CCMP"))
                                c->pair_ciphers |= IWINFO_CIPHER_CCMP;
 
-                       else if( strstr(val, "NONE") )
+                       else if (strstr(val, "NONE"))
                                c->pair_ciphers |= IWINFO_CIPHER_NONE;
 
-                       else if( strstr(val, "WEP-40") )
+                       else if (strstr(val, "WEP-40"))
                                c->pair_ciphers |= IWINFO_CIPHER_WEP40;
 
-                       else if( strstr(val, "WEP-104") )
+                       else if (strstr(val, "WEP-104"))
                                c->pair_ciphers |= IWINFO_CIPHER_WEP104;
 
 
-                       if( (val = nl80211_getval(NULL, res, "group_cipher")) )
+                       if ((val = nl80211_getval(NULL, res, "group_cipher")))
                        {
-                               if( strstr(val, "TKIP") )
+                               if (strstr(val, "TKIP"))
                                        c->group_ciphers |= IWINFO_CIPHER_TKIP;
 
-                               else if( strstr(val, "CCMP") )
+                               else if (strstr(val, "CCMP"))
                                        c->group_ciphers |= IWINFO_CIPHER_CCMP;
 
-                               else if( strstr(val, "NONE") )
+                               else if (strstr(val, "NONE"))
                                        c->group_ciphers |= IWINFO_CIPHER_NONE;
 
-                               else if( strstr(val, "WEP-40") )
+                               else if (strstr(val, "WEP-40"))
                                        c->group_ciphers |= IWINFO_CIPHER_WEP40;
 
-                               else if( strstr(val, "WEP-104") )
+                               else if (strstr(val, "WEP-104"))
                                        c->group_ciphers |= IWINFO_CIPHER_WEP104;
                        }
 
 
-                       if( (val = nl80211_getval(NULL, res, "key_mgmt")) )
+                       if ((val = nl80211_getval(NULL, res, "key_mgmt")))
                        {
-                               if( strstr(val, "WPA2") )
+                               if (strstr(val, "WPA2"))
                                        c->wpa_version = 2;
 
-                               else if( strstr(val, "WPA") )
+                               else if (strstr(val, "WPA"))
                                        c->wpa_version = 1;
 
 
-                               if( strstr(val, "PSK") )
+                               if (strstr(val, "PSK"))
                                        c->auth_suites |= IWINFO_KMGMT_PSK;
 
-                               else if( strstr(val, "EAP") || strstr(val, "802.1X") )
+                               else if (strstr(val, "EAP") || strstr(val, "802.1X"))
                                        c->auth_suites |= IWINFO_KMGMT_8021x;
 
-                               else if( strstr(val, "NONE") )
+                               else if (strstr(val, "NONE"))
                                        c->auth_suites |= IWINFO_KMGMT_NONE;
                        }
 
@@ -980,35 +971,35 @@ int nl80211_get_encryption(const char *ifname, char *buf)
        }
 
        /* Hostapd */
-       else if( (res = nl80211_hostapd_info(ifname)) )
+       else if ((res = nl80211_hostapd_info(ifname)))
        {
-               if( (val = nl80211_getval(ifname, res, "wpa")) != NULL )
+               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") )
+               if (!val || strstr(val, "PSK"))
                        c->auth_suites |= IWINFO_KMGMT_PSK;
 
-               if( val && strstr(val, "EAP") )
+               if (val && strstr(val, "EAP"))
                        c->auth_suites |= IWINFO_KMGMT_8021x;
 
-               if( val && strstr(val, "NONE") )
+               if (val && strstr(val, "NONE"))
                        c->auth_suites |= IWINFO_KMGMT_NONE;
 
-               if( (val = nl80211_getval(ifname, res, "wpa_pairwise")) != NULL )
+               if ((val = nl80211_getval(ifname, res, "wpa_pairwise")) != NULL)
                {
-                       if( strstr(val, "TKIP") )
+                       if (strstr(val, "TKIP"))
                                c->pair_ciphers |= IWINFO_CIPHER_TKIP;
 
-                       if( strstr(val, "CCMP") )
+                       if (strstr(val, "CCMP"))
                                c->pair_ciphers |= IWINFO_CIPHER_CCMP;
 
-                       if( strstr(val, "NONE") )
+                       if (strstr(val, "NONE"))
                                c->pair_ciphers |= IWINFO_CIPHER_NONE;
                }
 
-               if( (val = nl80211_getval(ifname, res, "auth_algs")) != NULL )
+               if ((val = nl80211_getval(ifname, res, "auth_algs")) != NULL)
                {
                        switch(atoi(val)) {
                                case 1:
@@ -1032,12 +1023,12 @@ int nl80211_get_encryption(const char *ifname, char *buf)
                        {
                                snprintf(k, sizeof(k), "wep_key%d", i);
 
-                               if( (val = nl80211_getval(ifname, res, k)) )
+                               if ((val = nl80211_getval(ifname, res, k)))
                                {
-                                       if( (strlen(val) == 5) || (strlen(val) == 10) )
+                                       if ((strlen(val) == 5) || (strlen(val) == 10))
                                                c->pair_ciphers |= IWINFO_CIPHER_WEP40;
 
-                                       else if( (strlen(val) == 13) || (strlen(val) == 26) )
+                                       else if ((strlen(val) == 13) || (strlen(val) == 26))
                                                c->pair_ciphers |= IWINFO_CIPHER_WEP104;
                                }
                        }
@@ -1055,9 +1046,9 @@ int nl80211_get_encryption(const char *ifname, char *buf)
 
 static int nl80211_get_assoclist_cb(struct nl_msg *msg, void *arg)
 {
-       struct nl80211_assoc_count *ac = arg;
-       struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
-       struct nlattr *attr[NL80211_ATTR_MAX + 1];
+       struct nl80211_arraybuf *arr = arg;
+       struct iwinfo_assoclist_entry *e = arr->buf;
+       struct nlattr **attr = nl80211_parse(msg);
        struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1];
 
        static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
@@ -1073,25 +1064,24 @@ static int nl80211_get_assoclist_cb(struct nl_msg *msg, void *arg)
                [NL80211_STA_INFO_PLINK_STATE]   = { .type = NLA_U8     },
        };
 
-       nla_parse(attr, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
-               genlmsg_attrlen(gnlh, 0), NULL);
+       /* advance to end of array */
+       e += arr->count;
 
-       if( attr[NL80211_ATTR_MAC] )
-               memcpy(ac->entry->mac, nla_data(attr[NL80211_ATTR_MAC]), 6);
+       if (attr[NL80211_ATTR_MAC])
+               memcpy(e->mac, nla_data(attr[NL80211_ATTR_MAC]), 6);
 
-       if( attr[NL80211_ATTR_STA_INFO] )
+       if (attr[NL80211_ATTR_STA_INFO])
        {
-               if!nla_parse_nested(sinfo, NL80211_STA_INFO_MAX,
-                               attr[NL80211_ATTR_STA_INFO], stats_policy) )
+               if (!nla_parse_nested(sinfo, NL80211_STA_INFO_MAX,
+                               attr[NL80211_ATTR_STA_INFO], stats_policy))
                {
-                       if( sinfo[NL80211_STA_INFO_SIGNAL] )
-                               ac->entry->signal = nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL]);
+                       if (sinfo[NL80211_STA_INFO_SIGNAL])
+                               e->signal = nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL]);
                }
        }
 
-       ac->entry->noise = ac->noise;
-       ac->entry++;
-       ac->count++;
+       e->noise = 0; /* filled in by caller */
+       arr->count++;
 
        return NL_SKIP;
 }
@@ -1099,17 +1089,14 @@ static int nl80211_get_assoclist_cb(struct nl_msg *msg, void *arg)
 int nl80211_get_assoclist(const char *ifname, char *buf, int *len)
 {
        DIR *d;
+       int i, noise = 0;
        struct dirent *de;
-       struct nl80211_assoc_count ac;
        struct nl80211_msg_conveyor *req;
+       struct nl80211_arraybuf arr = { .buf = buf, .count = 0 };
+       struct iwinfo_assoclist_entry *e;
 
        if ((d = opendir("/sys/class/net")) != NULL)
        {
-               ac.count = 0;
-               ac.entry = (struct iwinfo_assoclist_entry *)buf;
-
-               nl80211_get_noise(ifname, &ac.noise);
-
                while ((de = readdir(d)) != NULL)
                {
                        if (!strncmp(de->d_name, ifname, strlen(ifname)) &&
@@ -1121,31 +1108,37 @@ int nl80211_get_assoclist(const char *ifname, char *buf, int *len)
 
                                if (req)
                                {
-                                       nl80211_cb(req, nl80211_get_assoclist_cb, &ac);
-                                       nl80211_free(nl80211_send(req));
+                                       nl80211_cb(req, nl80211_get_assoclist_cb, &arr);
+                                       nl80211_send(req);
                                        nl80211_free(req);
                                }
+
+                               break;
                        }
                }
 
                closedir(d);
 
-               *len = (ac.count * sizeof(struct iwinfo_assoclist_entry));
+               if (!nl80211_get_noise(ifname, &noise))
+                       for (i = 0, e = arr.buf; i < arr.count; i++, e++)
+                               e->noise = noise;
+
+               *len = (arr.count * sizeof(struct iwinfo_assoclist_entry));
                return 0;
        }
 
        return -1;
 }
 
-int nl80211_get_txpwrlist(const char *ifname, char *buf, int *len)
+static int nl80211_get_txpwrlist_cb(struct nl_msg *msg, void *arg)
 {
+       int *dbm_max = arg;
        int ch_cur, ch_cmp, bands_remain, freqs_remain;
-       int dbm_max = -1, dbm_cur, dbm_cnt;
-       struct nl80211_msg_conveyor *req, *res;
+
+       struct nlattr **attr = nl80211_parse(msg);
        struct nlattr *bands[NL80211_BAND_ATTR_MAX + 1];
        struct nlattr *freqs[NL80211_FREQUENCY_ATTR_MAX + 1];
        struct nlattr *band, *freq;
-       struct iwinfo_txpwrlist_entry entry;
 
        static struct nla_policy freq_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
                [NL80211_FREQUENCY_ATTR_FREQ]         = { .type = NLA_U32  },
@@ -1156,51 +1149,63 @@ int nl80211_get_txpwrlist(const char *ifname, char *buf, int *len)
                [NL80211_FREQUENCY_ATTR_MAX_TX_POWER] = { .type = NLA_U32  },
        };
 
-       if( nl80211_get_channel(ifname, &ch_cur) )
-               ch_cur = 0;
+       ch_cur = *dbm_max; /* value int* is initialized with channel by caller */
+       *dbm_max = -1;
 
-       req = nl80211_msg(ifname, NL80211_CMD_GET_WIPHY, 0);
-       if( req )
+       nla_for_each_nested(band, attr[NL80211_ATTR_WIPHY_BANDS], bands_remain)
        {
-               res = nl80211_send(req);
-               if( res )
+               nla_parse(bands, NL80211_BAND_ATTR_MAX, nla_data(band),
+                         nla_len(band), NULL);
+
+               nla_for_each_nested(freq,
+                       bands[NL80211_BAND_ATTR_FREQS], freqs_remain)
                {
-                       nla_for_each_nested(band,
-                               res->attr[NL80211_ATTR_WIPHY_BANDS], bands_remain)
+                       nla_parse(freqs, NL80211_FREQUENCY_ATTR_MAX,
+                               nla_data(freq), nla_len(freq), freq_policy);
+
+                       ch_cmp = nl80211_freq2channel(
+                               nla_get_u32(freqs[NL80211_FREQUENCY_ATTR_FREQ]));
+
+                       if( (!ch_cur || (ch_cmp == ch_cur)) &&
+                               freqs[NL80211_FREQUENCY_ATTR_MAX_TX_POWER] )
                        {
-                               nla_parse(bands, NL80211_BAND_ATTR_MAX, nla_data(band),
-                                         nla_len(band), NULL);
+                               *dbm_max = (int)(0.01 * nla_get_u32(
+                                       freqs[NL80211_FREQUENCY_ATTR_MAX_TX_POWER]));
 
-                               nla_for_each_nested(freq,
-                                       bands[NL80211_BAND_ATTR_FREQS], freqs_remain)
-                               {
-                                       nla_parse(freqs, NL80211_FREQUENCY_ATTR_MAX,
-                                               nla_data(freq), nla_len(freq), freq_policy);
+                               break;
+                       }
+               }
+       }
 
-                                       ch_cmp = nl80211_freq2channel(
-                                               nla_get_u32(freqs[NL80211_FREQUENCY_ATTR_FREQ]));
+       return NL_SKIP;
+}
 
-                                       if( (!ch_cur || (ch_cmp == ch_cur)) &&
-                                           freqs[NL80211_FREQUENCY_ATTR_MAX_TX_POWER] )
-                                       {
-                                               dbm_max = (int)(0.01 * nla_get_u32(
-                                                       freqs[NL80211_FREQUENCY_ATTR_MAX_TX_POWER]));
+int nl80211_get_txpwrlist(const char *ifname, char *buf, int *len)
+{
+       int ch_cur;
+       int dbm_max = -1, dbm_cur, dbm_cnt;
+       struct nl80211_msg_conveyor *req;
+       struct iwinfo_txpwrlist_entry entry;
 
-                                               break;
-                                       }
-                               }
-                       }
+       if (nl80211_get_channel(ifname, &ch_cur))
+               ch_cur = 0;
 
-                       nl80211_free(res);
-               }
+       req = nl80211_msg(ifname, NL80211_CMD_GET_WIPHY, 0);
+       if (req)
+       {
+               /* initialize the value pointer with channel for callback */
+               dbm_max = ch_cur;
+
+               nl80211_cb(req, nl80211_get_txpwrlist_cb, &dbm_max);
+               nl80211_send(req);
                nl80211_free(req);
        }
 
-       if( dbm_max > -1 )
+       if (dbm_max > -1)
        {
-               fordbm_cur = 0, dbm_cnt = 0;
+               for (dbm_cur = 0, dbm_cnt = 0;
                     dbm_cur < dbm_max;
-                    dbm_cur++, dbm_cnt++ )
+                    dbm_cur++, dbm_cnt++)
                {
                        entry.dbm = dbm_cur;
                        entry.mw  = iwinfo_dbm2mw(dbm_cur);
@@ -1224,43 +1229,43 @@ 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, "WPA") || strstr(spec, "WEP") )
+       if (strstr(spec, "WPA") || strstr(spec, "WEP"))
        {
                c->enabled = 1;
 
-               if( strstr(spec, "WPA2-") && strstr(spec, "WPA-") )
+               if (strstr(spec, "WPA2-") && strstr(spec, "WPA-"))
                        c->wpa_version = 3;
 
-               else if( strstr(spec, "WPA2") )
+               else if (strstr(spec, "WPA2"))
                        c->wpa_version = 2;
 
-               else if( strstr(spec, "WPA") )
+               else if (strstr(spec, "WPA"))
                        c->wpa_version = 1;
 
-               else if( strstr(spec, "WEP") )
+               else if (strstr(spec, "WEP"))
                        c->auth_algs = IWINFO_AUTH_OPEN | IWINFO_AUTH_SHARED;
 
 
-               if( strstr(spec, "PSK") )
+               if (strstr(spec, "PSK"))
                        c->auth_suites |= IWINFO_KMGMT_PSK;
 
-               if( strstr(spec, "802.1X") || strstr(spec, "EAP") )
+               if (strstr(spec, "802.1X") || strstr(spec, "EAP"))
                        c->auth_suites |= IWINFO_KMGMT_8021x;
 
-               if( strstr(spec, "WPA-NONE") )
+               if (strstr(spec, "WPA-NONE"))
                        c->auth_suites |= IWINFO_KMGMT_NONE;
 
 
-               if( strstr(spec, "TKIP") )
+               if (strstr(spec, "TKIP"))
                        c->pair_ciphers |= IWINFO_CIPHER_TKIP;
 
-               if( strstr(spec, "CCMP") )
+               if (strstr(spec, "CCMP"))
                        c->pair_ciphers |= IWINFO_CIPHER_CCMP;
 
-               if( strstr(spec, "WEP-40") )
+               if (strstr(spec, "WEP-40"))
                        c->pair_ciphers |= IWINFO_CIPHER_WEP40;
 
-               if( strstr(spec, "WEP-104") )
+               if (strstr(spec, "WEP-104"))
                        c->pair_ciphers |= IWINFO_CIPHER_WEP104;
 
                c->group_ciphers = c->pair_ciphers;
@@ -1280,16 +1285,16 @@ int nl80211_get_scanlist(const char *ifname, char *buf, int *len)
        char cipher[256] = { 0 };
 
        /* Got a radioX pseudo interface, find some interface on it or create one */
-       if( !strncmp(ifname, "radio", 5) )
+       if (!strncmp(ifname, "radio", 5))
        {
                /* Reuse existing interface */
-               if( (res = nl80211_phy2ifname(ifname)) != NULL )
+               if ((res = nl80211_phy2ifname(ifname)) != NULL)
                {
                        return nl80211_get_scanlist(res, buf, len);
                }
 
                /* Need to spawn a temporary iface for scanning */
-               else if( (res = nl80211_ifadd(ifname)) != NULL )
+               else if ((res = nl80211_ifadd(ifname)) != NULL)
                {
                        count = nl80211_get_scanlist(res, buf, len);
                        nl80211_ifdel(res);
@@ -1300,9 +1305,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_wpactl_info(ifname, "SCAN", "CTRL-EVENT-SCAN-RESULTS")) )
+       if ((res = nl80211_wpactl_info(ifname, "SCAN", "CTRL-EVENT-SCAN-RESULTS")))
        {
-               if( (res = nl80211_wpactl_info(ifname, "SCAN_RESULTS", NULL)) )
+               if ((res = nl80211_wpactl_info(ifname, "SCAN_RESULTS", NULL)))
                {
                        nl80211_get_quality_max(ifname, &qmax);
 
@@ -1336,14 +1341,14 @@ int nl80211_get_scanlist(const char *ifname, char *buf, int *len)
                                e->signal = rssi;
 
                                /* Quality */
-                               if( rssi < 0 )
+                               if (rssi < 0)
                                {
                                        /* The cfg80211 wext compat layer assumes a signal range
                                         * of -110 dBm to -40 dBm, the quality value is derived
                                         * by adding 110 to the signal level */
-                                       if( rssi < -110 )
+                                       if (rssi < -110)
                                                rssi = -110;
-                                       else if( rssi > -40 )
+                                       else if (rssi > -40)
                                                rssi = -40;
 
                                        e->quality = (rssi + 110);
@@ -1379,9 +1384,9 @@ int nl80211_get_scanlist(const char *ifname, char *buf, int *len)
        else
        {
                /* Got a temp interface, don't create yet another one */
-               if( !strncmp(ifname, "tmp.", 4) )
+               if (!strncmp(ifname, "tmp.", 4))
                {
-                       if( !iwinfo_ifup(ifname) )
+                       if (!iwinfo_ifup(ifname))
                                return -1;
 
                        wext_get_scanlist(ifname, buf, len);
@@ -1392,15 +1397,15 @@ int nl80211_get_scanlist(const char *ifname, char *buf, int *len)
                /* Spawn a new scan interface */
                else
                {
-                       if( !(res = nl80211_ifadd(ifname)) )
+                       if (!(res = nl80211_ifadd(ifname)))
                                goto out;
 
-                       if( !iwinfo_ifmac(res) )
+                       if (!iwinfo_ifmac(res))
                                goto out;
 
                        /* if we can take the new interface up, the driver supports an
                         * additional interface and there's no need to tear down the ap */
-                       if( iwinfo_ifup(res) )
+                       if (iwinfo_ifup(res))
                        {
                                wext_get_scanlist(res, buf, len);
                                iwinfo_ifdown(res);
@@ -1408,7 +1413,7 @@ int nl80211_get_scanlist(const char *ifname, char *buf, int *len)
 
                        /* driver cannot create secondary interface, take down ap
                         * during scan */
-                       else if( iwinfo_ifdown(ifname) && iwinfo_ifup(res) )
+                       else if (iwinfo_ifdown(ifname) && iwinfo_ifup(res))
                        {
                                wext_get_scanlist(res, buf, len);
                                iwinfo_ifdown(res);
@@ -1425,83 +1430,108 @@ int nl80211_get_scanlist(const char *ifname, char *buf, int *len)
        return -1;
 }
 
-int nl80211_get_freqlist(const char *ifname, char *buf, int *len)
+static int nl80211_get_freqlist_cb(struct nl_msg *msg, void *arg)
 {
-       int count = 0, bands_remain, freqs_remain;
-       struct nl80211_msg_conveyor *req, *res;
+       int bands_remain, freqs_remain;
+
+       struct nl80211_arraybuf *arr = arg;
+       struct iwinfo_freqlist_entry *e = arr->buf;
+
+       struct nlattr **attr = nl80211_parse(msg);
        struct nlattr *bands[NL80211_BAND_ATTR_MAX + 1];
        struct nlattr *freqs[NL80211_FREQUENCY_ATTR_MAX + 1];
        struct nlattr *band, *freq;
-       struct iwinfo_freqlist_entry *e = (struct iwinfo_freqlist_entry *)buf;
 
-       req = nl80211_msg(ifname, NL80211_CMD_GET_WIPHY, 0);
-       if( req )
+       static struct nla_policy freq_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
+               [NL80211_FREQUENCY_ATTR_FREQ]         = { .type = NLA_U32  },
+               [NL80211_FREQUENCY_ATTR_DISABLED]     = { .type = NLA_FLAG },
+               [NL80211_FREQUENCY_ATTR_PASSIVE_SCAN] = { .type = NLA_FLAG },
+               [NL80211_FREQUENCY_ATTR_NO_IBSS]      = { .type = NLA_FLAG },
+               [NL80211_FREQUENCY_ATTR_RADAR]        = { .type = NLA_FLAG },
+               [NL80211_FREQUENCY_ATTR_MAX_TX_POWER] = { .type = NLA_U32  },
+       };
+
+       nla_for_each_nested(band, attr[NL80211_ATTR_WIPHY_BANDS], bands_remain)
        {
-               res = nl80211_send(req);
-               if( res && res->attr[NL80211_ATTR_WIPHY_BANDS] )
-               {
-                       nla_for_each_nested(band,
-                               res->attr[NL80211_ATTR_WIPHY_BANDS], bands_remain)
-                       {
-                               nla_parse(bands, NL80211_BAND_ATTR_MAX, nla_data(band),
-                                         nla_len(band), NULL);
+               nla_parse(bands, NL80211_BAND_ATTR_MAX, nla_data(band),
+                       nla_len(band), NULL);
 
-                               nla_for_each_nested(freq,
-                                       bands[NL80211_BAND_ATTR_FREQS], freqs_remain)
-                               {
-                                       nla_parse(freqs, NL80211_FREQUENCY_ATTR_MAX,
-                                               nla_data(freq), nla_len(freq), NULL);
+               nla_for_each_nested(freq,
+                       bands[NL80211_BAND_ATTR_FREQS], freqs_remain)
+               {
+                       nla_parse(freqs, NL80211_FREQUENCY_ATTR_MAX,
+                               nla_data(freq), nla_len(freq), NULL);
 
-                                       if( !freqs[NL80211_FREQUENCY_ATTR_FREQ] ||
-                                           freqs[NL80211_FREQUENCY_ATTR_DISABLED] )
-                                               continue;
+                       if( !freqs[NL80211_FREQUENCY_ATTR_FREQ] ||
+                               freqs[NL80211_FREQUENCY_ATTR_DISABLED] )
+                               continue;
 
-                                       e->mhz = nla_get_u32(freqs[NL80211_FREQUENCY_ATTR_FREQ]);
-                                       e->channel = nl80211_freq2channel(e->mhz);
+                       e->mhz = nla_get_u32(freqs[NL80211_FREQUENCY_ATTR_FREQ]);
+                       e->channel = nl80211_freq2channel(e->mhz);
 
-                                       e->restricted = (
-                                               freqs[NL80211_FREQUENCY_ATTR_PASSIVE_SCAN] ||
-                                               freqs[NL80211_FREQUENCY_ATTR_NO_IBSS]      ||
-                                               freqs[NL80211_FREQUENCY_ATTR_RADAR]
-                                       ) ? 1 : 0;
+                       e->restricted = (
+                               freqs[NL80211_FREQUENCY_ATTR_PASSIVE_SCAN] ||
+                               freqs[NL80211_FREQUENCY_ATTR_NO_IBSS]      ||
+                               freqs[NL80211_FREQUENCY_ATTR_RADAR]
+                       ) ? 1 : 0;
 
-                                       e++;
-                                       count++;
-                               }
-                       }
-                       nl80211_free(res);
+                       e++;
+                       arr->count++;
                }
+       }
+
+       return NL_SKIP;
+}
+
+int nl80211_get_freqlist(const char *ifname, char *buf, int *len)
+{
+       struct nl80211_msg_conveyor *req;
+       struct nl80211_arraybuf arr = { .buf = buf, .count = 0 };
+
+       req = nl80211_msg(ifname, NL80211_CMD_GET_WIPHY, 0);
+       if (req)
+       {
+               nl80211_cb(req, nl80211_get_freqlist_cb, &arr);
+               nl80211_send(req);
                nl80211_free(req);
        }
 
-       if( count > 0 )
+       if (arr.count > 0)
        {
-               *len = count * sizeof(struct iwinfo_freqlist_entry);
+               *len = arr.count * sizeof(struct iwinfo_freqlist_entry);
                return 0;
        }
 
        return -1;
 }
 
+static int nl80211_get_country_cb(struct nl_msg *msg, void *arg)
+{
+       char *buf = arg;
+       struct nlattr **attr = nl80211_parse(msg);
+
+       if (attr[NL80211_ATTR_REG_ALPHA2])
+               memcpy(buf, nla_data(attr[NL80211_ATTR_REG_ALPHA2]), 2);
+       else
+               buf[0] = 0;
+
+       return NL_SKIP;
+}
+
 int nl80211_get_country(const char *ifname, char *buf)
 {
        int rv = -1;
-       struct nl80211_msg_conveyor *req, *res;
+       struct nl80211_msg_conveyor *req;
 
        req = nl80211_msg(ifname, NL80211_CMD_GET_REG, 0);
-       if( req )
+       if (req)
        {
-               res = nl80211_send(req);
-               if( res )
-               {
-                       if( res->attr[NL80211_ATTR_REG_ALPHA2] )
-                       {
-                               memcpy(buf, nla_data(res->attr[NL80211_ATTR_REG_ALPHA2]), 2);
-                               rv = 0;
-                       }
-                       nl80211_free(res);
-               }
+               nl80211_cb(req, nl80211_get_country_cb, buf);
+               nl80211_send(req);
                nl80211_free(req);
+
+               if (buf[0])
+                       rv = 0;
        }
 
        return rv;
@@ -1524,56 +1554,66 @@ int nl80211_get_countrylist(const char *ifname, char *buf, int *len)
        return 0;
 }
 
-int nl80211_get_hwmodelist(const char *ifname, int *buf)
+static int nl80211_get_hwmodelist_cb(struct nl_msg *msg, void *arg)
 {
+       int *modes = arg;
        int bands_remain, freqs_remain;
-       struct nl80211_msg_conveyor *req, *res;
+       uint16_t caps = 0;
+       struct nlattr **attr = nl80211_parse(msg);
        struct nlattr *bands[NL80211_BAND_ATTR_MAX + 1];
        struct nlattr *freqs[NL80211_FREQUENCY_ATTR_MAX + 1];
        struct nlattr *band, *freq;
-       uint16_t caps = 0;
 
-       req = nl80211_msg(ifname, NL80211_CMD_GET_WIPHY, 0);
-       if( req )
+       *modes = 0;
+
+       if (attr[NL80211_ATTR_WIPHY_BANDS])
        {
-               res = nl80211_send(req);
-               if( res && res->attr[NL80211_ATTR_WIPHY_BANDS] )
+               nla_for_each_nested(band, attr[NL80211_ATTR_WIPHY_BANDS], bands_remain)
                {
-                       nla_for_each_nested(band,
-                               res->attr[NL80211_ATTR_WIPHY_BANDS], bands_remain)
-                       {
-                               nla_parse(bands, NL80211_BAND_ATTR_MAX, nla_data(band),
-                                         nla_len(band), NULL);
+                       nla_parse(bands, NL80211_BAND_ATTR_MAX, nla_data(band),
+                               nla_len(band), NULL);
 
-                               if( bands[NL80211_BAND_ATTR_HT_CAPA] )
-                                       caps = nla_get_u16(bands[NL80211_BAND_ATTR_HT_CAPA]);
+                       if (bands[NL80211_BAND_ATTR_HT_CAPA])
+                               caps = nla_get_u16(bands[NL80211_BAND_ATTR_HT_CAPA]);
 
-                               /* Treat any nonzero capability as 11n */
-                               if( caps > 0 )
-                                       *buf |= IWINFO_80211_N;
+                       /* Treat any nonzero capability as 11n */
+                       if (caps > 0)
+                               *modes |= IWINFO_80211_N;
 
-                               nla_for_each_nested(freq,
-                                       bands[NL80211_BAND_ATTR_FREQS], freqs_remain)
-                               {
-                                       nla_parse(freqs, NL80211_FREQUENCY_ATTR_MAX,
-                                               nla_data(freq), nla_len(freq), NULL);
+                       nla_for_each_nested(freq,
+                               bands[NL80211_BAND_ATTR_FREQS], freqs_remain)
+                       {
+                               nla_parse(freqs, NL80211_FREQUENCY_ATTR_MAX,
+                                       nla_data(freq), nla_len(freq), NULL);
 
-                                       if( !freqs[NL80211_FREQUENCY_ATTR_FREQ] )
-                                               continue;
+                               if (!freqs[NL80211_FREQUENCY_ATTR_FREQ])
+                                       continue;
 
-                                       if( nla_get_u32(freqs[NL80211_FREQUENCY_ATTR_FREQ]) < 2485 )
-                                       {
-                                               *buf |= IWINFO_80211_B;
-                                               *buf |= IWINFO_80211_G;
-                                       }
-                                       else
-                                       {
-                                               *buf |= IWINFO_80211_A;
-                                       }
+                               if (nla_get_u32(freqs[NL80211_FREQUENCY_ATTR_FREQ]) < 2485)
+                               {
+                                       *modes |= IWINFO_80211_B;
+                                       *modes |= IWINFO_80211_G;
+                               }
+                               else
+                               {
+                                       *modes |= IWINFO_80211_A;
                                }
                        }
-                       nl80211_free(res);
                }
+       }
+
+       return NL_SKIP;
+}
+
+int nl80211_get_hwmodelist(const char *ifname, int *buf)
+{
+       struct nl80211_msg_conveyor *req;
+
+       req = nl80211_msg(ifname, NL80211_CMD_GET_WIPHY, 0);
+       if (req)
+       {
+               nl80211_cb(req, nl80211_get_hwmodelist_cb, buf);
+               nl80211_send(req);
                nl80211_free(req);
        }
 
@@ -1585,7 +1625,7 @@ int nl80211_get_mbssid_support(const char *ifname, int *buf)
        /* Test whether we can create another interface */
        char *nif = nl80211_ifadd(ifname);
 
-       if( nif )
+       if (nif)
        {
                *buf = (iwinfo_ifmac(nif) && iwinfo_ifup(nif));
 
index 7f6ffeaaa2614bc54d80eaf5f3d86750fcae730a..edad085b436c736add1c9df785107030dd31b61f 100644 (file)
@@ -45,8 +45,6 @@ struct nl80211_msg_conveyor {
        int custom_cb;
        struct nl_msg *msg;
        struct nl_cb *cb;
-       struct genlmsghdr *hdr;
-       struct nlattr *attr[NL80211_ATTR_MAX + 1];
 };
 
 struct nl80211_rssi_rate {
@@ -60,6 +58,11 @@ struct nl80211_assoc_count {
        int count;
 };
 
+struct nl80211_arraybuf {
+       void *buf;
+       int count;
+};
+
 int nl80211_probe(const char *ifname);
 int nl80211_get_mode(const char *ifname, char *buf);
 int nl80211_get_ssid(const char *ifname, char *buf);