[v3] batctl: Dynamically select header format in netlink_print_neighbors

Message ID 20240910192201.14442-1-NoahBPeterson1997@gmail.com (mailing list archive)
State Accepted, archived
Delegated to: Simon Wunderlich
Headers
Series [v3] batctl: Dynamically select header format in netlink_print_neighbors |

Commit Message

Noah Peterson Sept. 10, 2024, 7:22 p.m. UTC
  From: Noah Peterson <NoahBPeterson1997@gmail.com>

The netlink_print_neighbors() function previously used a static header
format, which did not account for variations between the neighbor list
output from different BATMAN routing algorithms (BATMAN_IV vs. BATMAN_V).

This change ensures that the table header output in `batctl n` is accurate
for both BATMAN routing algorithms.

Signed-off-by: Noah Peterson <noahbpeterson1997@gmail.com>
---

v3: Correcting commit message and newlines, noted by Sven
<sven@narfation.org>
v2: Changing output based on BATMAN routing algorithm, noted by Sven
<sven@narfation.org>

This patch introduces a dynamic header selection mechanism that adjusts
the output format based on the active routing algorithm:
- For BATMAN_IV, the header is set to "IF             Neighbor              last-seen".
- For BATMAN_V, it changes to "         Neighbor   last-seen      speed           IF".
---
 neighbors.c | 22 +++++++++++++++++++++-
 1 file changed, 21 insertions(+), 1 deletion(-)

--
2.39.3 (Apple Git-146)
  

Patch

diff --git a/neighbors.c b/neighbors.c
index 3102b0e..f34952b 100644
--- a/neighbors.c
+++ b/neighbors.c
@@ -6,6 +6,7 @@ 
  * License-Filename: LICENSES/preferred/GPL-2.0
  */

+#include <errno.h>
 #include <net/if.h>
 #include <netinet/if_ether.h>
 #include <netlink/netlink.h>
@@ -119,9 +120,28 @@  static int netlink_print_neighbors(struct state *state, char *orig_iface,
 				   int read_opts, float orig_timeout,
 				   float watch_interval)
 {
+	char *header = NULL;
+	char *info_header;
+
+	/* only parse routing algorithm name */
+	last_err = -EINVAL;
+	info_header = netlink_get_info(state, BATADV_CMD_GET_ORIGINATORS, NULL);
+	free(info_header);
+
+	if (strlen(algo_name_buf) == 0)
+		return last_err;
+
+	if (!strcmp("BATMAN_IV", algo_name_buf))
+		header = "IF             Neighbor              last-seen\n";
+	if (!strcmp("BATMAN_V", algo_name_buf))
+		header = "         Neighbor   last-seen      speed           IF\n";
+
+	if (!header)
+		return -EINVAL;
+
 	return netlink_print_common(state, orig_iface, read_opts,
 				    orig_timeout, watch_interval,
-				    "IF             Neighbor              last-seen\n",
+				    header,
 				    BATADV_CMD_GET_NEIGHBORS,
 				    neighbors_callback);
 }