Improve code as suggested by neoraider

This commit is contained in:
Julian Kornberger 2016-12-19 15:47:17 +01:00
parent 243fc5a373
commit 5001912221
6 changed files with 25 additions and 27 deletions

View File

@ -13,4 +13,4 @@ respondd.so: airtime.c ifaces.c respondd.c
$(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -shared -fPIC -D_GNU_SOURCE -lnl-tiny -o $@ $^ $(LDLIBS)
clean:
rm -rf *.so airtime-test
rm -rf *.so

View File

@ -78,13 +78,13 @@ static int survey_airtime_handler(struct nl_msg *msg, void *arg) {
goto abort;
}
if(nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX, tb[NL80211_ATTR_SURVEY_INFO], survey_policy)) {
if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX, tb[NL80211_ATTR_SURVEY_INFO], survey_policy)) {
fprintf(stderr, "failed to parse nested attributes!\n");
goto abort;
}
// Channel active?
if(!sinfo[NL80211_SURVEY_INFO_IN_USE]){
if (!sinfo[NL80211_SURVEY_INFO_IN_USE]){
goto abort;
}
@ -99,13 +99,14 @@ abort:
return NL_SKIP;
}
int get_airtime(struct airtime_result *result, int ifx) {
int error = 0;
bool get_airtime(struct airtime_result *result, int ifx) {
bool ok = true;
int ctrl;
struct nl_sock *sk = NULL;
struct nl_msg *msg = NULL;
#define CHECK(x) { if (!(x)) { fprintf(stderr, "airtime.c: error on line %d\n", __LINE__); error = 1; goto out; } }
#define CHECK(x) { if (!(x)) { fprintf(stderr, "%s: error on line %d\n", __FILE__, __LINE__); ok = false; goto out; } }
CHECK(sk = nl_socket_alloc());
CHECK(genl_connect(sk) >= 0);
@ -113,9 +114,7 @@ int get_airtime(struct airtime_result *result, int ifx) {
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());
/* TODO: check return? */
genlmsg_put(msg, 0, 0, ctrl, 0, NLM_F_DUMP, NL80211_CMD_GET_SURVEY, 0);
CHECK(genlmsg_put(msg, 0, 0, ctrl, 0, NLM_F_DUMP, NL80211_CMD_GET_SURVEY, 0));
NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifx);
@ -132,5 +131,5 @@ out:
if (sk)
nl_socket_free(sk);
return error;
return ok;
}

View File

@ -1,5 +1,6 @@
#pragma once
#include <stdbool.h>
#include <stdint.h>
struct airtime_result {
@ -11,4 +12,4 @@ struct airtime_result {
uint8_t noise;
};
int get_airtime(struct airtime_result *result, int ifx);
__attribute__((visibility("hidden"))) bool get_airtime(struct airtime_result *result, int ifx);

View File

@ -8,7 +8,7 @@
#include "ifaces.h"
static int iface_dump_handler(struct nl_msg *msg, struct iface_list **arg) {
static int iface_dump_handler(struct nl_msg *msg, void *arg) {
struct nlattr *tb[NL80211_ATTR_MAX + 1];
struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
int wiphy;
@ -19,14 +19,14 @@ static int iface_dump_handler(struct nl_msg *msg, struct iface_list **arg) {
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 abort;
goto skip;
}
*last_next = malloc(sizeof(**last_next));
(*last_next)->next = NULL;
(*last_next)->ifx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
(*last_next)->wiphy = wiphy;
abort:
skip:
return NL_SKIP;
}
@ -36,17 +36,15 @@ struct iface_list *get_ifaces() {
struct nl_msg *msg = NULL;
struct iface_list *ifaces = NULL;
#define CHECK(x) { if (!(x)) { fprintf(stderr, "airtime.c: error on line %d\n", __LINE__); goto out; } }
#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, (nl_recvmsg_msg_cb_t) iface_dump_handler, &ifaces) == 0);
CHECK(nl_socket_modify_cb(sk, NL_CB_VALID, NL_CB_CUSTOM, iface_dump_handler, &ifaces) == 0);
CHECK(msg = nlmsg_alloc());
/* TODO: check return? */
genlmsg_put(msg, 0, 0, ctrl, 0, NLM_F_DUMP, NL80211_CMD_GET_INTERFACE, 0);
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);

View File

@ -6,4 +6,4 @@ struct iface_list {
struct iface_list *next;
};
struct iface_list *get_ifaces();
__attribute__((visibility("hidden"))) struct iface_list *get_ifaces();

View File

@ -6,8 +6,8 @@
#include "airtime.h"
#include "ifaces.h"
void fill_airtime_json(struct airtime_result *air, struct json_object *wireless) {
struct json_object *obj = NULL;
static void fill_airtime_json(struct airtime_result *air, struct json_object *wireless) {
struct json_object *obj;
obj = json_object_new_object();
if(!obj)
@ -24,10 +24,9 @@ void fill_airtime_json(struct airtime_result *air, struct json_object *wireless)
}
static struct json_object *respondd_provider_statistics(void) {
struct airtime_result airtime = {};
struct airtime_result airtime = {0};
struct json_object *result, *wireless;
struct iface_list *ifaces;
void *freeptr;
result = json_object_new_object();
if (!result)
@ -41,9 +40,10 @@ static struct json_object *respondd_provider_statistics(void) {
ifaces = get_ifaces();
while (ifaces != NULL) {
get_airtime(&airtime, ifaces->ifx);
fill_airtime_json(&airtime, wireless);
freeptr = ifaces;
if(get_airtime(&airtime, ifaces->ifx))
fill_airtime_json(&airtime, wireless);
void *freeptr = ifaces;
ifaces = ifaces->next;
free(freeptr);
}