respondd-module-airtime: Move common netlink query code to separate function

This commit is contained in:
Jan-Philipp Litza 2017-02-08 19:32:43 +01:00
parent 138ddeedf3
commit 7b56f09e0f
5 changed files with 56 additions and 71 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"
/*
@ -99,39 +96,6 @@ 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

@ -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];
@ -31,32 +27,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);