b.a.t.m.a.n.lists.open-mesh.org archive mirror
 help / color / mirror / Atom feed
From: Sven Eckelmann <sven@narfation.org>
To: b.a.t.m.a.n@lists.open-mesh.org
Cc: Sven Eckelmann <sven@narfation.org>
Subject: [PATCH 6/6] batctl: routing_algo: List using shared genl socket
Date: Mon, 10 May 2021 21:49:45 +0200	[thread overview]
Message-ID: <20210510194945.103735-7-sven@narfation.org> (raw)
In-Reply-To: <20210510194945.103735-1-sven@narfation.org>

The interface subcommand doesn't need to allocate a new netlink socket to
get the list of interfaces. There is already a netlink socket available
which was used to query the routing algorithm of each interface.
Just use this batadv genl socket to also dump the list of all available
routing algorithms.

Signed-off-by: Sven Eckelmann <sven@narfation.org>
---
 routing_algo.c | 53 ++++++++++++++++----------------------------------
 1 file changed, 17 insertions(+), 36 deletions(-)

diff --git a/routing_algo.c b/routing_algo.c
index 8fb4fab..0adf32f 100644
--- a/routing_algo.c
+++ b/routing_algo.c
@@ -79,58 +79,35 @@ static int routing_algos_callback(struct nl_msg *msg, void *arg __maybe_unused)
 	return NL_OK;
 }
 
-static int print_routing_algos(void)
+static int print_routing_algos(struct state *state)
 {
-	struct nl_sock *sock;
 	struct nl_msg *msg;
 	struct nl_cb *cb;
-	int family;
 	struct print_opts opts = {
 		.callback = routing_algos_callback,
 	};
 
-	sock = nl_socket_alloc();
-	if (!sock)
-		return -ENOMEM;
-
-	genl_connect(sock);
-
-	family = genl_ctrl_resolve(sock, BATADV_NL_NAME);
-	if (family < 0) {
-		last_err = -EOPNOTSUPP;
-		goto err_free_sock;
-	}
-
 	msg = nlmsg_alloc();
-	if (!msg) {
-		last_err = -ENOMEM;
-		goto err_free_sock;
-	}
-
-	genlmsg_put(msg, NL_AUTO_PID, NL_AUTO_SEQ, family, 0, NLM_F_DUMP,
-		    BATADV_CMD_GET_ROUTING_ALGOS, 1);
-
-	nl_send_auto_complete(sock, msg);
+	if (!msg)
+		return -ENOMEM;
 
+	genlmsg_put(msg, NL_AUTO_PID, NL_AUTO_SEQ, state->batadv_family, 0,
+		    NLM_F_DUMP, BATADV_CMD_GET_ROUTING_ALGOS, 1);
+	nl_send_auto_complete(state->sock, msg);
 	nlmsg_free(msg);
 
 	opts.remaining_header = strdup("Available routing algorithms:\n");
 
 	cb = nl_cb_alloc(NL_CB_DEFAULT);
-	if (!cb) {
-		last_err = -ENOMEM;
-		goto err_free_sock;
-	}
+	if (!cb)
+		return -ENOMEM;
 
 	nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, netlink_print_common_cb,
 		  &opts);
 	nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, netlink_stop_callback, NULL);
 	nl_cb_err(cb, NL_CB_CUSTOM, netlink_print_error, NULL);
 
-	nl_recvmsgs(sock, cb);
-
-err_free_sock:
-	nl_socket_free(sock);
+	nl_recvmsgs(state->sock, cb);
 
 	if (!last_err)
 		netlink_print_remaining_header(&opts);
@@ -316,19 +293,23 @@ static int routing_algo(struct state *state, int argc, char **argv)
 		return EXIT_FAILURE;
 
 	print_ra_interfaces(state);
-	netlink_destroy(state);
 
 	res = read_file(SYS_SELECTED_RA_PATH, USE_READ_BUFF);
 	if (res != EXIT_SUCCESS)
-		return EXIT_FAILURE;
+		goto err_free_netlink;
 
 	printf("Selected routing algorithm (used when next batX interface is created):\n");
 	printf(" => %s\n", line_ptr);
 	free(line_ptr);
 	line_ptr = NULL;
 
-	print_routing_algos();
-	return EXIT_SUCCESS;
+	print_routing_algos(state);
+	res = EXIT_SUCCESS;
+
+err_free_netlink:
+	netlink_destroy(state);
+
+	return res;
 }
 
 COMMAND(SUBCOMMAND, routing_algo, "ra", 0, NULL,
-- 
2.30.2


      parent reply	other threads:[~2021-05-10 19:49 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-05-10 19:49 [PATCH 0/6] batctl: Use shared batadv genl socket for requests Sven Eckelmann
2021-05-10 19:49 ` [PATCH 1/6] batctl: Consume genl ACKs after setting reads Sven Eckelmann
2021-05-10 19:49 ` [PATCH 2/6] batctl: throughputmeter: Use global genl socket Sven Eckelmann
2021-05-10 19:49 ` [PATCH 3/6] batctl: interface: List using shared " Sven Eckelmann
2021-05-10 19:49 ` [PATCH 4/6] batctl: Get meshif info " Sven Eckelmann
2021-05-10 19:49 ` [PATCH 5/6] batctl: Use common genl socket for netlink_query_common Sven Eckelmann
2021-05-10 19:49 ` Sven Eckelmann [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210510194945.103735-7-sven@narfation.org \
    --to=sven@narfation.org \
    --cc=b.a.t.m.a.n@lists.open-mesh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).