From 5001912221859ae79c958d239e7932d6719f749a Mon Sep 17 00:00:00 2001 From: Julian Kornberger Date: Mon, 19 Dec 2016 15:47:17 +0100 Subject: [PATCH] Improve code as suggested by neoraider --- net/respondd-module-airtime/src/Makefile | 2 +- net/respondd-module-airtime/src/airtime.c | 17 ++++++++--------- net/respondd-module-airtime/src/airtime.h | 3 ++- net/respondd-module-airtime/src/ifaces.c | 14 ++++++-------- net/respondd-module-airtime/src/ifaces.h | 2 +- net/respondd-module-airtime/src/respondd.c | 14 +++++++------- 6 files changed, 25 insertions(+), 27 deletions(-) diff --git a/net/respondd-module-airtime/src/Makefile b/net/respondd-module-airtime/src/Makefile index 81212fd..0c99578 100644 --- a/net/respondd-module-airtime/src/Makefile +++ b/net/respondd-module-airtime/src/Makefile @@ -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 diff --git a/net/respondd-module-airtime/src/airtime.c b/net/respondd-module-airtime/src/airtime.c index bd85992..cd22314 100644 --- a/net/respondd-module-airtime/src/airtime.c +++ b/net/respondd-module-airtime/src/airtime.c @@ -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; } diff --git a/net/respondd-module-airtime/src/airtime.h b/net/respondd-module-airtime/src/airtime.h index c7c5fbb..e0ae614 100644 --- a/net/respondd-module-airtime/src/airtime.h +++ b/net/respondd-module-airtime/src/airtime.h @@ -1,5 +1,6 @@ #pragma once +#include #include 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); diff --git a/net/respondd-module-airtime/src/ifaces.c b/net/respondd-module-airtime/src/ifaces.c index f9d0514..ac0c252 100644 --- a/net/respondd-module-airtime/src/ifaces.c +++ b/net/respondd-module-airtime/src/ifaces.c @@ -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); diff --git a/net/respondd-module-airtime/src/ifaces.h b/net/respondd-module-airtime/src/ifaces.h index ea13ee3..e873087 100644 --- a/net/respondd-module-airtime/src/ifaces.h +++ b/net/respondd-module-airtime/src/ifaces.h @@ -6,4 +6,4 @@ struct iface_list { struct iface_list *next; }; -struct iface_list *get_ifaces(); +__attribute__((visibility("hidden"))) struct iface_list *get_ifaces(); diff --git a/net/respondd-module-airtime/src/respondd.c b/net/respondd-module-airtime/src/respondd.c index 0de1f49..0046993 100644 --- a/net/respondd-module-airtime/src/respondd.c +++ b/net/respondd-module-airtime/src/respondd.c @@ -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); }