Merge pull request #160 from FreifunkBremen/master
respondd-module-airtime: Improve error handling
This commit is contained in:
commit
0efcccd3ef
|
@ -9,7 +9,7 @@ all: respondd.so
|
||||||
|
|
||||||
%.c: %.h
|
%.c: %.h
|
||||||
|
|
||||||
respondd.so: airtime.c ifaces.c respondd.c
|
respondd.so: netlink.c airtime.c ifaces.c respondd.c
|
||||||
$(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -shared -fPIC -D_GNU_SOURCE -lnl-tiny -o $@ $^ $(LDLIBS)
|
$(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -shared -fPIC -D_GNU_SOURCE -lnl-tiny -o $@ $^ $(LDLIBS)
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
|
|
|
@ -25,13 +25,10 @@
|
||||||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <sys/socket.h>
|
|
||||||
#include <linux/nl80211.h>
|
#include <linux/nl80211.h>
|
||||||
#include <netlink/netlink.h>
|
|
||||||
#include <netlink/genl/genl.h>
|
#include <netlink/genl/genl.h>
|
||||||
#include <netlink/genl/ctrl.h>
|
|
||||||
#include <net/if.h>
|
|
||||||
|
|
||||||
|
#include "netlink.h"
|
||||||
#include "airtime.h"
|
#include "airtime.h"
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -60,78 +57,84 @@
|
||||||
* @__NL80211_SURVEY_INFO_AFTER_LAST: internal use
|
* @__NL80211_SURVEY_INFO_AFTER_LAST: internal use
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
static const char const* msg_names[NL80211_SURVEY_INFO_MAX + 1] = {
|
||||||
|
[NL80211_SURVEY_INFO_FREQUENCY] = "frequency",
|
||||||
|
[NL80211_SURVEY_INFO_CHANNEL_TIME] = "active",
|
||||||
|
[NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY] = "busy",
|
||||||
|
[NL80211_SURVEY_INFO_CHANNEL_TIME_RX] = "rx",
|
||||||
|
[NL80211_SURVEY_INFO_CHANNEL_TIME_TX] = "tx",
|
||||||
|
[NL80211_SURVEY_INFO_NOISE] = "noise",
|
||||||
|
};
|
||||||
|
|
||||||
static int survey_airtime_handler(struct nl_msg *msg, void *arg) {
|
static int survey_airtime_handler(struct nl_msg *msg, void *arg) {
|
||||||
struct nlattr *tb[NL80211_ATTR_MAX + 1];
|
struct json_object *parent_json = (struct json_object *) arg;
|
||||||
struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
|
|
||||||
static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
|
|
||||||
[NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
|
|
||||||
[NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
|
|
||||||
};
|
|
||||||
|
|
||||||
struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
|
struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
|
||||||
struct airtime_result *result = (struct airtime_result *) arg;
|
struct nlattr *survey_info = nla_find(genlmsg_attrdata(gnlh, 0), genlmsg_attrlen(gnlh, 0), NL80211_ATTR_SURVEY_INFO);
|
||||||
|
|
||||||
nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0), genlmsg_attrlen(gnlh, 0), NULL);
|
if (!survey_info) {
|
||||||
|
fprintf(stderr, "respondd-module-airtime: survey data missing in netlink message\n");
|
||||||
if (!tb[NL80211_ATTR_SURVEY_INFO]) {
|
|
||||||
fprintf(stderr, "survey data missing!\n");
|
|
||||||
goto abort;
|
goto abort;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX, tb[NL80211_ATTR_SURVEY_INFO], survey_policy)) {
|
struct json_object *freq_json = json_object_new_object();
|
||||||
fprintf(stderr, "failed to parse nested attributes!\n");
|
if (!freq_json) {
|
||||||
|
fprintf(stderr, "respondd-module-airtime: failed allocating JSON object\n");
|
||||||
goto abort;
|
goto abort;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Channel active?
|
// This variable counts the number of required attributes that are
|
||||||
if (!sinfo[NL80211_SURVEY_INFO_IN_USE]){
|
// found in the message and is afterwards checked against the number of
|
||||||
goto abort;
|
// required attributes.
|
||||||
|
unsigned int req_fields = 0;
|
||||||
|
|
||||||
|
int rem;
|
||||||
|
struct nlattr *nla;
|
||||||
|
nla_for_each_nested(nla, survey_info, rem) {
|
||||||
|
int type = nla_type(nla);
|
||||||
|
|
||||||
|
if (type > NL80211_SURVEY_INFO_MAX)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
switch (type) {
|
||||||
|
// these are the required fields
|
||||||
|
case NL80211_SURVEY_INFO_IN_USE:
|
||||||
|
case NL80211_SURVEY_INFO_FREQUENCY:
|
||||||
|
case NL80211_SURVEY_INFO_CHANNEL_TIME:
|
||||||
|
req_fields++;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!msg_names[type])
|
||||||
|
continue;
|
||||||
|
|
||||||
|
struct json_object *data_json = NULL;
|
||||||
|
switch (nla_len(nla)) {
|
||||||
|
case sizeof(uint64_t):
|
||||||
|
data_json = json_object_new_int64(nla_get_u64(nla));
|
||||||
|
break;
|
||||||
|
case sizeof(uint32_t):
|
||||||
|
data_json = json_object_new_int(nla_get_u32(nla));
|
||||||
|
break;
|
||||||
|
case sizeof(uint8_t):
|
||||||
|
data_json = json_object_new_int(nla_get_u8(nla));
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
fprintf(stderr, "respondd-module-airtime: Unexpected NL attribute length: %d\n", nla_len(nla));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (data_json)
|
||||||
|
json_object_object_add(freq_json, msg_names[type], data_json);
|
||||||
}
|
}
|
||||||
|
|
||||||
result->frequency = nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]);
|
if (req_fields == 3)
|
||||||
result->active_time = nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME]);
|
json_object_array_add(parent_json, freq_json);
|
||||||
result->busy_time = nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY]);
|
else
|
||||||
result->rx_time = nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_RX]);
|
json_object_put(freq_json);
|
||||||
result->tx_time = nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_TX]);
|
|
||||||
result->noise = nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
|
|
||||||
|
|
||||||
abort:
|
abort:
|
||||||
return NL_SKIP;
|
return NL_SKIP;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool get_airtime(struct airtime_result *result, int ifx) {
|
bool get_airtime(struct json_object *result, int ifx) {
|
||||||
bool ok = false;
|
return nl_send_dump(survey_airtime_handler, result, NL80211_CMD_GET_SURVEY, ifx);
|
||||||
int ctrl;
|
|
||||||
struct nl_sock *sk = NULL;
|
|
||||||
struct nl_msg *msg = NULL;
|
|
||||||
|
|
||||||
|
|
||||||
#define CHECK(x) { if (!(x)) { fprintf(stderr, "%s: error on line %d\n", __FILE__, __LINE__); goto out; } }
|
|
||||||
|
|
||||||
CHECK(sk = nl_socket_alloc());
|
|
||||||
CHECK(genl_connect(sk) >= 0);
|
|
||||||
|
|
||||||
CHECK(ctrl = genl_ctrl_resolve(sk, NL80211_GENL_NAME));
|
|
||||||
CHECK(nl_socket_modify_cb(sk, NL_CB_VALID, NL_CB_CUSTOM, survey_airtime_handler, result) == 0);
|
|
||||||
CHECK(msg = nlmsg_alloc());
|
|
||||||
CHECK(genlmsg_put(msg, 0, 0, ctrl, 0, NLM_F_DUMP, NL80211_CMD_GET_SURVEY, 0));
|
|
||||||
|
|
||||||
NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifx);
|
|
||||||
|
|
||||||
CHECK(nl_send_auto_complete(sk, msg) >= 0);
|
|
||||||
CHECK(nl_recvmsgs_default(sk) >= 0);
|
|
||||||
|
|
||||||
#undef CHECK
|
|
||||||
|
|
||||||
ok = true;
|
|
||||||
|
|
||||||
nla_put_failure:
|
|
||||||
out:
|
|
||||||
if (msg)
|
|
||||||
nlmsg_free(msg);
|
|
||||||
|
|
||||||
if (sk)
|
|
||||||
nl_socket_free(sk);
|
|
||||||
|
|
||||||
return ok;
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,14 +2,6 @@
|
||||||
|
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
#include <json-c/json.h>
|
||||||
|
|
||||||
struct airtime_result {
|
__attribute__((visibility("hidden"))) bool get_airtime(struct json_object *result, int ifx);
|
||||||
uint64_t active_time;
|
|
||||||
uint64_t busy_time;
|
|
||||||
uint64_t rx_time;
|
|
||||||
uint64_t tx_time;
|
|
||||||
uint32_t frequency;
|
|
||||||
uint8_t noise;
|
|
||||||
};
|
|
||||||
|
|
||||||
__attribute__((visibility("hidden"))) bool get_airtime(struct airtime_result *result, int ifx);
|
|
||||||
|
|
|
@ -1,12 +1,8 @@
|
||||||
#include <sys/socket.h>
|
|
||||||
#include <linux/nl80211.h>
|
#include <linux/nl80211.h>
|
||||||
#include <netlink/netlink.h>
|
|
||||||
#include <netlink/genl/genl.h>
|
#include <netlink/genl/genl.h>
|
||||||
#include <netlink/genl/ctrl.h>
|
|
||||||
#include <net/if.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
|
|
||||||
#include "ifaces.h"
|
#include "ifaces.h"
|
||||||
|
#include "netlink.h"
|
||||||
|
|
||||||
static int iface_dump_handler(struct nl_msg *msg, void *arg) {
|
static int iface_dump_handler(struct nl_msg *msg, void *arg) {
|
||||||
struct nlattr *tb[NL80211_ATTR_MAX + 1];
|
struct nlattr *tb[NL80211_ATTR_MAX + 1];
|
||||||
|
@ -16,12 +12,17 @@ static int iface_dump_handler(struct nl_msg *msg, void *arg) {
|
||||||
|
|
||||||
nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0), genlmsg_attrlen(gnlh, 0), NULL);
|
nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0), genlmsg_attrlen(gnlh, 0), NULL);
|
||||||
|
|
||||||
|
if (!tb[NL80211_ATTR_WIPHY] || !tb[NL80211_ATTR_IFINDEX])
|
||||||
|
goto skip;
|
||||||
|
|
||||||
wiphy = nla_get_u32(tb[NL80211_ATTR_WIPHY]);
|
wiphy = nla_get_u32(tb[NL80211_ATTR_WIPHY]);
|
||||||
for (last_next = arg; *last_next != NULL; last_next = &(*last_next)->next) {
|
for (last_next = arg; *last_next != NULL; last_next = &(*last_next)->next) {
|
||||||
if ((*last_next)->wiphy == wiphy)
|
if ((*last_next)->wiphy == wiphy)
|
||||||
goto skip;
|
goto skip;
|
||||||
}
|
}
|
||||||
*last_next = malloc(sizeof(**last_next));
|
*last_next = malloc(sizeof(**last_next));
|
||||||
|
if (!*last_next)
|
||||||
|
goto skip;
|
||||||
(*last_next)->next = NULL;
|
(*last_next)->next = NULL;
|
||||||
(*last_next)->ifx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
|
(*last_next)->ifx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
|
||||||
(*last_next)->wiphy = wiphy;
|
(*last_next)->wiphy = wiphy;
|
||||||
|
@ -31,32 +32,7 @@ skip:
|
||||||
}
|
}
|
||||||
|
|
||||||
struct iface_list *get_ifaces() {
|
struct iface_list *get_ifaces() {
|
||||||
int ctrl;
|
|
||||||
struct nl_sock *sk = NULL;
|
|
||||||
struct nl_msg *msg = NULL;
|
|
||||||
struct iface_list *ifaces = NULL;
|
struct iface_list *ifaces = NULL;
|
||||||
|
nl_send_dump(&iface_dump_handler, &ifaces, NL80211_CMD_GET_INTERFACE, 0);
|
||||||
#define CHECK(x) { if (!(x)) { fprintf(stderr, "%s: error on line %d\n", __FILE__, __LINE__); goto out; } }
|
|
||||||
|
|
||||||
CHECK(sk = nl_socket_alloc());
|
|
||||||
CHECK(genl_connect(sk) >= 0);
|
|
||||||
|
|
||||||
CHECK(ctrl = genl_ctrl_resolve(sk, NL80211_GENL_NAME));
|
|
||||||
CHECK(nl_socket_modify_cb(sk, NL_CB_VALID, NL_CB_CUSTOM, iface_dump_handler, &ifaces) == 0);
|
|
||||||
CHECK(msg = nlmsg_alloc());
|
|
||||||
CHECK(genlmsg_put(msg, 0, 0, ctrl, 0, NLM_F_DUMP, NL80211_CMD_GET_INTERFACE, 0));
|
|
||||||
|
|
||||||
CHECK(nl_send_auto_complete(sk, msg) >= 0);
|
|
||||||
CHECK(nl_recvmsgs_default(sk) >= 0);
|
|
||||||
|
|
||||||
#undef CHECK
|
|
||||||
|
|
||||||
out:
|
|
||||||
if (msg)
|
|
||||||
nlmsg_free(msg);
|
|
||||||
|
|
||||||
if (sk)
|
|
||||||
nl_socket_free(sk);
|
|
||||||
|
|
||||||
return ifaces;
|
return ifaces;
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,43 @@
|
||||||
|
#include <linux/nl80211.h>
|
||||||
|
#include <netlink/genl/genl.h>
|
||||||
|
#include <netlink/genl/ctrl.h>
|
||||||
|
|
||||||
|
#include "netlink.h"
|
||||||
|
|
||||||
|
bool nl_send_dump(nl_recvmsg_msg_cb_t cb, void *cb_arg, int cmd, uint32_t cmd_arg) {
|
||||||
|
bool ok = false;
|
||||||
|
int ctrl;
|
||||||
|
struct nl_sock *sk = NULL;
|
||||||
|
struct nl_msg *msg = NULL;
|
||||||
|
|
||||||
|
|
||||||
|
#define CHECK(x) { if (!(x)) { fprintf(stderr, "%s: error on line %d\n", __FILE__, __LINE__); goto out; } }
|
||||||
|
|
||||||
|
CHECK(sk = nl_socket_alloc());
|
||||||
|
CHECK(genl_connect(sk) >= 0);
|
||||||
|
|
||||||
|
CHECK(ctrl = genl_ctrl_resolve(sk, NL80211_GENL_NAME));
|
||||||
|
CHECK(nl_socket_modify_cb(sk, NL_CB_VALID, NL_CB_CUSTOM, cb, cb_arg) == 0);
|
||||||
|
CHECK(msg = nlmsg_alloc());
|
||||||
|
CHECK(genlmsg_put(msg, 0, 0, ctrl, 0, NLM_F_DUMP, cmd, 0));
|
||||||
|
|
||||||
|
if (cmd_arg != 0)
|
||||||
|
NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, cmd_arg);
|
||||||
|
|
||||||
|
CHECK(nl_send_auto_complete(sk, msg) >= 0);
|
||||||
|
CHECK(nl_recvmsgs_default(sk) >= 0);
|
||||||
|
|
||||||
|
#undef CHECK
|
||||||
|
|
||||||
|
ok = true;
|
||||||
|
|
||||||
|
nla_put_failure:
|
||||||
|
out:
|
||||||
|
if (msg)
|
||||||
|
nlmsg_free(msg);
|
||||||
|
|
||||||
|
if (sk)
|
||||||
|
nl_socket_free(sk);
|
||||||
|
|
||||||
|
return ok;
|
||||||
|
}
|
|
@ -0,0 +1,7 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <netlink/handlers.h>
|
||||||
|
|
||||||
|
__attribute__((visibility("hidden"))) bool nl_send_dump(nl_recvmsg_msg_cb_t cb, void *cb_arg, int cmd, uint32_t cmd_arg);
|
|
@ -6,25 +6,7 @@
|
||||||
#include "airtime.h"
|
#include "airtime.h"
|
||||||
#include "ifaces.h"
|
#include "ifaces.h"
|
||||||
|
|
||||||
static void fill_airtime_json(struct airtime_result *air, struct json_object *wireless) {
|
|
||||||
struct json_object *obj;
|
|
||||||
|
|
||||||
obj = json_object_new_object();
|
|
||||||
if (!obj)
|
|
||||||
return;
|
|
||||||
|
|
||||||
json_object_object_add(obj, "frequency", json_object_new_int(air->frequency));
|
|
||||||
json_object_object_add(obj, "active", json_object_new_int64(air->active_time));
|
|
||||||
json_object_object_add(obj, "busy", json_object_new_int64(air->busy_time));
|
|
||||||
json_object_object_add(obj, "rx", json_object_new_int64(air->rx_time));
|
|
||||||
json_object_object_add(obj, "tx", json_object_new_int64(air->tx_time));
|
|
||||||
json_object_object_add(obj, "noise", json_object_new_int(air->noise));
|
|
||||||
|
|
||||||
json_object_array_add(wireless, obj);
|
|
||||||
}
|
|
||||||
|
|
||||||
static struct json_object *respondd_provider_statistics(void) {
|
static struct json_object *respondd_provider_statistics(void) {
|
||||||
struct airtime_result airtime = {0};
|
|
||||||
struct json_object *result, *wireless;
|
struct json_object *result, *wireless;
|
||||||
struct iface_list *ifaces;
|
struct iface_list *ifaces;
|
||||||
|
|
||||||
|
@ -40,8 +22,7 @@ static struct json_object *respondd_provider_statistics(void) {
|
||||||
|
|
||||||
ifaces = get_ifaces();
|
ifaces = get_ifaces();
|
||||||
while (ifaces != NULL) {
|
while (ifaces != NULL) {
|
||||||
if (get_airtime(&airtime, ifaces->ifx))
|
get_airtime(wireless, ifaces->ifx);
|
||||||
fill_airtime_json(&airtime, wireless);
|
|
||||||
|
|
||||||
void *freeptr = ifaces;
|
void *freeptr = ifaces;
|
||||||
ifaces = ifaces->next;
|
ifaces = ifaces->next;
|
||||||
|
|
Loading…
Reference in New Issue