From patchwork Sat Feb 9 13:42:08 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sven Eckelmann X-Patchwork-Id: 17787 X-Patchwork-Delegate: sw@simonwunderlich.de Return-Path: X-Original-To: patchwork@open-mesh.org Delivered-To: patchwork@open-mesh.org Received: from open-mesh.org (localhost [IPv6:::1]) by open-mesh.org (Postfix) with ESMTP id EEB8F81C4F; Sat, 9 Feb 2019 14:42:53 +0100 (CET) Authentication-Results: open-mesh.org; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=narfation.org header.i=@narfation.org header.b="hpyU4Zth"; dkim-atps=neutral Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=79.140.41.39; helo=v3-1039.vlinux.de; envelope-from=sven@narfation.org; receiver= Received: from v3-1039.vlinux.de (narfation.org [79.140.41.39]) by open-mesh.org (Postfix) with ESMTPS id BB6B781C16 for ; Sat, 9 Feb 2019 14:42:34 +0100 (CET) Received: from sven-desktop.home.narfation.org (p200300C597362EFD0000000000004065.dip0.t-ipconnect.de [IPv6:2003:c5:9736:2efd::4065]) by v3-1039.vlinux.de (Postfix) with ESMTPSA id 2396F11013A; Sat, 9 Feb 2019 14:42:33 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=narfation.org; s=20121; t=1549719753; bh=rjJecC0sUkpfUlGE56vVZsjCFyJ3WAycdYlhEGYcI9k=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=hpyU4Zth16qPyCf3WXIMPlvWS3mUUU50keGvkPeYWa6/ipNTYNbGFvbC/GsQKVPwq cUktYLm5tj/qlzfC+Jl6EUVaYxYBUIjnEPWoGgVhLZv4MnZs1Y1Xk2/3+jNyIUIVd/ AvT+HoBVMgZOCisXdOsPrO1kHM2Mh4kJUj0tyKeo= From: Sven Eckelmann To: b.a.t.m.a.n@lists.open-mesh.org Date: Sat, 9 Feb 2019 14:42:08 +0100 Message-Id: <20190209134222.15035-7-sven@narfation.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190209134222.15035-1-sven@narfation.org> References: <20190209134222.15035-1-sven@narfation.org> MIME-Version: 1.0 Subject: [B.A.T.M.A.N.] [PATCH v5 06/20] batctl: Add netlink simple query helper X-BeenThere: b.a.t.m.a.n@lists.open-mesh.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: The list for a Better Approach To Mobile Ad-hoc Networking List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: The list for a Better Approach To Mobile Ad-hoc Networking Errors-To: b.a.t.m.a.n-bounces@lists.open-mesh.org Sender: "B.A.T.M.A.N" All functions which will use the new configuration interface for meshifs, hardifs and vlans require a simple method to send messages and have one or multiple extra functionality: * add special attributes (attribute_cb) * return errors which happened during communication via netlink * parse the reply message (callback) The sys_simple_nlquery provides this but requires that the netlink socket and the single use nl_cb was allocated by the initialization helper via COMMAND_FLAG_MESH_IFACE. Signed-off-by: Sven Eckelmann --- sys.c | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ sys.h | 10 ++++++++++ 2 files changed, 69 insertions(+) diff --git a/sys.c b/sys.c index d4f1d6d..0972394 100644 --- a/sys.c +++ b/sys.c @@ -47,6 +47,65 @@ const char *sysfs_param_enable[] = { NULL, }; +static int sys_simple_nlerror(struct sockaddr_nl *nla __maybe_unused, + struct nlmsgerr *nlerr, void *arg) +{ + int *result = arg; + + if (nlerr->error != -EOPNOTSUPP) + fprintf(stderr, "Error received: %s\n", + strerror(-nlerr->error)); + + *result = nlerr->error; + + return NL_STOP; +} + +int sys_simple_nlquery(struct state *state, enum batadv_nl_commands nl_cmd, + nl_recvmsg_msg_cb_t attribute_cb, + nl_recvmsg_msg_cb_t callback) +{ + int result; + struct nl_msg *msg; + int ret; + + if (!state->sock) + return -EOPNOTSUPP; + + if (callback) { + result = -EOPNOTSUPP; + nl_cb_set(state->cb, NL_CB_VALID, NL_CB_CUSTOM, callback, + &result); + } else { + result = 0; + } + + nl_cb_err(state->cb, NL_CB_CUSTOM, sys_simple_nlerror, &result); + + msg = nlmsg_alloc(); + if (!msg) + return -ENOMEM; + + genlmsg_put(msg, NL_AUTO_PID, NL_AUTO_SEQ, state->batadv_family, 0, 0, + nl_cmd, 1); + nla_put_u32(msg, BATADV_ATTR_MESH_IFINDEX, state->mesh_ifindex); + + if (attribute_cb) { + ret = attribute_cb(msg, state); + if (ret < 0) { + nlmsg_free(msg); + return -ENOMEM; + } + } + + nl_send_auto_complete(state->sock, msg); + nlmsg_free(msg); + + nl_recvmsgs(state->sock, state->cb); + + return result; +} + static void settings_usage(struct state *state) { fprintf(stderr, "Usage: batctl [options] %s|%s [parameters] %s\n", diff --git a/sys.h b/sys.h index f212789..2f7f5f4 100644 --- a/sys.h +++ b/sys.h @@ -25,6 +25,12 @@ #include "main.h" +#include +#include + +#include "batman_adv.h" +#include "netlink.h" + #define SYS_BATIF_PATH_FMT "/sys/class/net/%s/mesh/" #define SYS_IFACE_PATH "/sys/class/net" #define SYS_IFACE_DIR SYS_IFACE_PATH"/%s/" @@ -47,4 +53,8 @@ extern const char *sysfs_param_enable[]; int handle_sys_setting(struct state *state, int argc, char **argv); +int sys_simple_nlquery(struct state *state, enum batadv_nl_commands nl_cmd, + nl_recvmsg_msg_cb_t attribute_cb, + nl_recvmsg_msg_cb_t callback); + #endif