libiwinfo: rework nl80211 interface code, plug remaining memory leaks

This commit is contained in:
Jo-Philipp Wich 2011-09-07 04:17:00 +00:00
parent b1cc78085e
commit ebc7582205
3 changed files with 380 additions and 337 deletions

View 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)

View file

@ -105,17 +105,6 @@ 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;
}
@ -174,8 +163,6 @@ static struct nl80211_msg_conveyor * nl80211_msg(const char *ifname, int cmd, in
if (phyidx > -1)
NLA_PUT_U32(req, NL80211_ATTR_WIPHY, phyidx);
nlmsg_get(req);
cv.msg = req;
cv.cb = cb;
cv.custom_cb = 0;
@ -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)
@ -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)
{
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);
}
@ -499,12 +502,9 @@ static char * nl80211_ifadd(const char *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 )
{
nl80211_send(req);
rv = nif;
nl80211_free(res);
}
nla_put_failure:
nl80211_free(req);
@ -522,7 +522,7 @@ static void nl80211_ifdel(const char *ifname)
{
NLA_PUT_STRING(req->msg, NL80211_ATTR_IFNAME, ifname);
nl80211_free(nl80211_send(req));
nl80211_send(req);
nla_put_failure:
nl80211_free(req);
@ -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,9 +684,6 @@ 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( !nla_parse_nested(sinfo, NL80211_STA_INFO_MAX,
@ -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);
}
}
@ -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)
@ -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,11 +1064,11 @@ 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);
memcpy(e->mac, nla_data(attr[NL80211_ATTR_MAC]), 6);
if (attr[NL80211_ATTR_STA_INFO])
{
@ -1085,13 +1076,12 @@ static int nl80211_get_assoclist_cb(struct nl_msg *msg, void *arg)
attr[NL80211_ATTR_STA_INFO], stats_policy))
{
if (sinfo[NL80211_STA_INFO_SIGNAL])
ac->entry->signal = nla_get_u8(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,17 +1149,10 @@ 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 )
{
res = nl80211_send(req);
if( res )
{
nla_for_each_nested(band,
res->attr[NL80211_ATTR_WIPHY_BANDS], bands_remain)
nla_for_each_nested(band, attr[NL80211_ATTR_WIPHY_BANDS], bands_remain)
{
nla_parse(bands, NL80211_BAND_ATTR_MAX, nla_data(band),
nla_len(band), NULL);
@ -1183,7 +1169,7 @@ int nl80211_get_txpwrlist(const char *ifname, char *buf, int *len)
if( (!ch_cur || (ch_cmp == ch_cur)) &&
freqs[NL80211_FREQUENCY_ATTR_MAX_TX_POWER] )
{
dbm_max = (int)(0.01 * nla_get_u32(
*dbm_max = (int)(0.01 * nla_get_u32(
freqs[NL80211_FREQUENCY_ATTR_MAX_TX_POWER]));
break;
@ -1191,8 +1177,27 @@ int nl80211_get_txpwrlist(const char *ifname, char *buf, int *len)
}
}
nl80211_free(res);
return NL_SKIP;
}
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;
if (nl80211_get_channel(ifname, &ch_cur))
ch_cur = 0;
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);
}
@ -1425,23 +1430,28 @@ 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 )
{
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)
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)
{
nla_parse(bands, NL80211_BAND_ATTR_MAX, nla_data(band),
nla_len(band), NULL);
@ -1466,42 +1476,62 @@ int nl80211_get_freqlist(const char *ifname, char *buf, int *len)
) ? 1 : 0;
e++;
count++;
arr->count++;
}
}
nl80211_free(res);
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)
{
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,23 +1554,21 @@ 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,
res->attr[NL80211_ATTR_WIPHY_BANDS], bands_remain)
nla_for_each_nested(band, attr[NL80211_ATTR_WIPHY_BANDS], bands_remain)
{
nla_parse(bands, NL80211_BAND_ATTR_MAX, nla_data(band),
nla_len(band), NULL);
@ -1550,7 +1578,7 @@ int nl80211_get_hwmodelist(const char *ifname, int *buf)
/* Treat any nonzero capability as 11n */
if (caps > 0)
*buf |= IWINFO_80211_N;
*modes |= IWINFO_80211_N;
nla_for_each_nested(freq,
bands[NL80211_BAND_ATTR_FREQS], freqs_remain)
@ -1563,17 +1591,29 @@ int nl80211_get_hwmodelist(const char *ifname, int *buf)
if (nla_get_u32(freqs[NL80211_FREQUENCY_ATTR_FREQ]) < 2485)
{
*buf |= IWINFO_80211_B;
*buf |= IWINFO_80211_G;
*modes |= IWINFO_80211_B;
*modes |= IWINFO_80211_G;
}
else
{
*buf |= IWINFO_80211_A;
*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);
}

View 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);