Merge pull request #160 from FreifunkBremen/master

respondd-module-airtime: Improve error handling
This commit is contained in:
Matthias Schiffer 2017-03-02 16:31:56 +01:00 committed by GitHub
commit 0efcccd3ef
7 changed files with 125 additions and 123 deletions

View File

@ -9,7 +9,7 @@ all: respondd.so
%.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)
clean:

View File

@ -25,13 +25,10 @@
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <sys/socket.h>
#include <linux/nl80211.h>
#include <netlink/netlink.h>
#include <netlink/genl/genl.h>
#include <netlink/genl/ctrl.h>
#include <net/if.h>
#include "netlink.h"
#include "airtime.h"
/*
@ -60,78 +57,84 @@
* @__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) {
struct nlattr *tb[NL80211_ATTR_MAX + 1];
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 json_object *parent_json = (struct json_object *) arg;
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 (!tb[NL80211_ATTR_SURVEY_INFO]) {
fprintf(stderr, "survey data missing!\n");
if (!survey_info) {
fprintf(stderr, "respondd-module-airtime: survey data missing in netlink message\n");
goto abort;
}
if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX, tb[NL80211_ATTR_SURVEY_INFO], survey_policy)) {
fprintf(stderr, "failed to parse nested attributes!\n");
struct json_object *freq_json = json_object_new_object();
if (!freq_json) {
fprintf(stderr, "respondd-module-airtime: failed allocating JSON object\n");
goto abort;
}
// Channel active?
if (!sinfo[NL80211_SURVEY_INFO_IN_USE]){
goto abort;
// This variable counts the number of required attributes that are
// found in the message and is afterwards checked against the number of
// 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]);
result->active_time = nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME]);
result->busy_time = nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY]);
result->rx_time = nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_RX]);
result->tx_time = nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_TX]);
result->noise = nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
if (req_fields == 3)
json_object_array_add(parent_json, freq_json);
else
json_object_put(freq_json);
abort:
return NL_SKIP;
}
bool get_airtime(struct airtime_result *result, int ifx) {
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, 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;
bool get_airtime(struct json_object *result, int ifx) {
return nl_send_dump(survey_airtime_handler, result, NL80211_CMD_GET_SURVEY, ifx);
}

View File

@ -2,14 +2,6 @@
#include <stdbool.h>
#include <stdint.h>
#include <json-c/json.h>
struct airtime_result {
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);
__attribute__((visibility("hidden"))) bool get_airtime(struct json_object *result, int ifx);

View File

@ -1,12 +1,8 @@
#include <sys/socket.h>
#include <linux/nl80211.h>
#include <netlink/netlink.h>
#include <netlink/genl/genl.h>
#include <netlink/genl/ctrl.h>
#include <net/if.h>
#include <stdlib.h>
#include "ifaces.h"
#include "netlink.h"
static int iface_dump_handler(struct nl_msg *msg, void *arg) {
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);
if (!tb[NL80211_ATTR_WIPHY] || !tb[NL80211_ATTR_IFINDEX])
goto skip;
wiphy = nla_get_u32(tb[NL80211_ATTR_WIPHY]);
for (last_next = arg; *last_next != NULL; last_next = &(*last_next)->next) {
if ((*last_next)->wiphy == wiphy)
goto skip;
}
*last_next = malloc(sizeof(**last_next));
if (!*last_next)
goto skip;
(*last_next)->next = NULL;
(*last_next)->ifx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
(*last_next)->wiphy = wiphy;
@ -31,32 +32,7 @@ skip:
}
struct iface_list *get_ifaces() {
int ctrl;
struct nl_sock *sk = NULL;
struct nl_msg *msg = NULL;
struct iface_list *ifaces = 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, 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);
nl_send_dump(&iface_dump_handler, &ifaces, NL80211_CMD_GET_INTERFACE, 0);
return ifaces;
}

View File

@ -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;
}

View File

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

View File

@ -6,25 +6,7 @@
#include "airtime.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) {
struct airtime_result airtime = {0};
struct json_object *result, *wireless;
struct iface_list *ifaces;
@ -40,8 +22,7 @@ static struct json_object *respondd_provider_statistics(void) {
ifaces = get_ifaces();
while (ifaces != NULL) {
if (get_airtime(&airtime, ifaces->ifx))
fill_airtime_json(&airtime, wireless);
get_airtime(wireless, ifaces->ifx);
void *freeptr = ifaces;
ifaces = ifaces->next;