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
Subject: [B.A.T.M.A.N.] [PATCH 07/38] batctl: Move routing_algo command to separate file
Date: Mon, 22 Oct 2018 00:54:53 +0200	[thread overview]
Message-ID: <20181021225524.8155-8-sven@narfation.org> (raw)
In-Reply-To: <20181021225524.8155-1-sven@narfation.org>

More complex commands in batctl are stored in separate files which are
called like the actual command name. This makes it easier to group
functionality and detect which parts belong to a more complex construct.

Signed-off-by: Sven Eckelmann <sven@narfation.org>
---
 Makefile       |   1 +
 main.c         |   3 +-
 routing_algo.c | 127 +++++++++++++++++++++++++++++++++++++++++++++++++
 routing_algo.h |  28 +++++++++++
 sys.c          |  92 -----------------------------------
 sys.h          |   3 --
 6 files changed, 158 insertions(+), 96 deletions(-)
 create mode 100644 routing_algo.c
 create mode 100644 routing_algo.h

diff --git a/Makefile b/Makefile
index 9b960b6..9ff4fe4 100755
--- a/Makefile
+++ b/Makefile
@@ -40,6 +40,7 @@ OBJ += log.o
 OBJ += main.o
 OBJ += netlink.o
 OBJ += ping.o
+OBJ += routing_algo.o
 OBJ += statistics.o
 OBJ += sys.o
 OBJ += tcpdump.o
diff --git a/main.c b/main.c
index 6160a72..4abee13 100644
--- a/main.c
+++ b/main.c
@@ -41,6 +41,7 @@
 #include "loglevel.h"
 #include "log.h"
 #include "gw_mode.h"
+#include "routing_algo.h"
 #include "functions.h"
 
 char mesh_dfl_iface[] = "bat0";
@@ -153,7 +154,7 @@ int main(int argc, char **argv)
 #endif
 	} else if ((strcmp(argv[1], "routing_algo") == 0) || (strcmp(argv[1], "ra") == 0)) {
 
-		ret = handle_ra_setting(argc - 1, argv + 1);
+		ret = routing_algo(mesh_iface, argc - 1, argv + 1);
 
 	} else if (check_mesh_iface(mesh_iface) < 0) {
 		fprintf(stderr, "Error - interface %s is not present or not a batman-adv interface\n", mesh_iface);
diff --git a/routing_algo.c b/routing_algo.c
new file mode 100644
index 0000000..89af6c5
--- /dev/null
+++ b/routing_algo.c
@@ -0,0 +1,127 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (C) 2009-2018  B.A.T.M.A.N. contributors:
+ *
+ * Marek Lindner <mareklindner@neomailbox.ch>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of version 2 of the GNU General Public
+ * License as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA
+ *
+ * License-Filename: LICENSES/preferred/GPL-2.0
+ */
+
+#include <dirent.h>
+#include <errno.h>
+#include <getopt.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "functions.h"
+#include "main.h"
+#include "sys.h"
+
+#define SYS_SELECTED_RA_PATH	"/sys/module/batman_adv/parameters/routing_algo"
+#define SYS_ROUTING_ALGO_FMT	SYS_IFACE_PATH"/%s/mesh/routing_algo"
+
+static void ra_mode_usage(void)
+{
+	fprintf(stderr, "Usage: batctl [options] routing_algo [algorithm]\n");
+	fprintf(stderr, "options:\n");
+	fprintf(stderr, " \t -h print this help\n");
+}
+
+int routing_algo(char *mesh_iface __maybe_unused, int argc, char **argv)
+{
+	DIR *iface_base_dir;
+	struct dirent *iface_dir;
+	int optchar;
+	char *path_buff;
+	int res = EXIT_FAILURE;
+	int first_iface = 1;
+
+	while ((optchar = getopt(argc, argv, "h")) != -1) {
+		switch (optchar) {
+		case 'h':
+			ra_mode_usage();
+			return EXIT_SUCCESS;
+		default:
+			ra_mode_usage();
+			return EXIT_FAILURE;
+		}
+	}
+
+	check_root_or_die("batctl routing_algo");
+
+	if (argc == 2) {
+		res = write_file(SYS_SELECTED_RA_PATH, "", argv[1], NULL);
+		goto out;
+	}
+
+	path_buff = malloc(PATH_BUFF_LEN);
+	if (!path_buff) {
+		fprintf(stderr, "Error - could not allocate path buffer: out of memory ?\n");
+		goto out;
+	}
+
+	iface_base_dir = opendir(SYS_IFACE_PATH);
+	if (!iface_base_dir) {
+		fprintf(stderr, "Error - the directory '%s' could not be read: %s\n",
+			SYS_IFACE_PATH, strerror(errno));
+		fprintf(stderr, "Is the batman-adv module loaded and sysfs mounted ?\n");
+		goto free_buff;
+	}
+
+	while ((iface_dir = readdir(iface_base_dir)) != NULL) {
+		snprintf(path_buff, PATH_BUFF_LEN, SYS_ROUTING_ALGO_FMT, iface_dir->d_name);
+		res = read_file("", path_buff, USE_READ_BUFF | SILENCE_ERRORS, 0, 0, 0);
+		if (res != EXIT_SUCCESS)
+			continue;
+
+		if (line_ptr[strlen(line_ptr) - 1] == '\n')
+			line_ptr[strlen(line_ptr) - 1] = '\0';
+
+		if (first_iface) {
+			first_iface = 0;
+			printf("Active routing protocol configuration:\n");
+		}
+
+		printf(" * %s: %s\n", iface_dir->d_name, line_ptr);
+
+		free(line_ptr);
+		line_ptr = NULL;
+	}
+
+	closedir(iface_base_dir);
+	free(path_buff);
+
+	if (!first_iface)
+		printf("\n");
+
+	res = read_file("", SYS_SELECTED_RA_PATH, USE_READ_BUFF, 0, 0, 0);
+	if (res != EXIT_SUCCESS)
+		return EXIT_FAILURE;
+
+	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;
+
+free_buff:
+	free(path_buff);
+out:
+	return res;
+}
diff --git a/routing_algo.h b/routing_algo.h
new file mode 100644
index 0000000..db246c6
--- /dev/null
+++ b/routing_algo.h
@@ -0,0 +1,28 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/* Copyright (C) 2009-2018  B.A.T.M.A.N. contributors:
+ *
+ * Marek Lindner <mareklindner@neomailbox.ch>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of version 2 of the GNU General Public
+ * License as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA
+ *
+ * License-Filename: LICENSES/preferred/GPL-2.0
+ */
+
+#ifndef _BATCTL_ROUTING_ALGO_H
+#define _BATCTL_ROUTING_ALGO_H
+
+int routing_algo(char *mesh_iface, int argc, char **argv);
+
+#endif
diff --git a/sys.c b/sys.c
index 14f322c..2cb288e 100644
--- a/sys.c
+++ b/sys.c
@@ -205,95 +205,3 @@ int handle_sys_setting(char *mesh_iface, int setting, int argc, char **argv)
 	free(base_dev);
 	return res;
 }
-
-static void ra_mode_usage(void)
-{
-	fprintf(stderr, "Usage: batctl [options] routing_algo [algorithm]\n");
-	fprintf(stderr, "options:\n");
-	fprintf(stderr, " \t -h print this help\n");
-}
-
-int handle_ra_setting(int argc, char **argv)
-{
-	DIR *iface_base_dir;
-	struct dirent *iface_dir;
-	int optchar;
-	char *path_buff;
-	int res = EXIT_FAILURE;
-	int first_iface = 1;
-
-	while ((optchar = getopt(argc, argv, "h")) != -1) {
-		switch (optchar) {
-		case 'h':
-			ra_mode_usage();
-			return EXIT_SUCCESS;
-		default:
-			ra_mode_usage();
-			return EXIT_FAILURE;
-		}
-	}
-
-	check_root_or_die("batctl routing_algo");
-
-	if (argc == 2) {
-		res = write_file(SYS_SELECTED_RA_PATH, "", argv[1], NULL);
-		goto out;
-	}
-
-	path_buff = malloc(PATH_BUFF_LEN);
-	if (!path_buff) {
-		fprintf(stderr, "Error - could not allocate path buffer: out of memory ?\n");
-		goto out;
-	}
-
-	iface_base_dir = opendir(SYS_IFACE_PATH);
-	if (!iface_base_dir) {
-		fprintf(stderr, "Error - the directory '%s' could not be read: %s\n",
-			SYS_IFACE_PATH, strerror(errno));
-		fprintf(stderr, "Is the batman-adv module loaded and sysfs mounted ?\n");
-		goto free_buff;
-	}
-
-	while ((iface_dir = readdir(iface_base_dir)) != NULL) {
-		snprintf(path_buff, PATH_BUFF_LEN, SYS_ROUTING_ALGO_FMT, iface_dir->d_name);
-		res = read_file("", path_buff, USE_READ_BUFF | SILENCE_ERRORS, 0, 0, 0);
-		if (res != EXIT_SUCCESS)
-			continue;
-
-		if (line_ptr[strlen(line_ptr) - 1] == '\n')
-			line_ptr[strlen(line_ptr) - 1] = '\0';
-
-		if (first_iface) {
-			first_iface = 0;
-			printf("Active routing protocol configuration:\n");
-		}
-
-		printf(" * %s: %s\n", iface_dir->d_name, line_ptr);
-
-		free(line_ptr);
-		line_ptr = NULL;
-	}
-
-	closedir(iface_base_dir);
-	free(path_buff);
-
-	if (!first_iface)
-		printf("\n");
-
-	res = read_file("", SYS_SELECTED_RA_PATH, USE_READ_BUFF, 0, 0, 0);
-	if (res != EXIT_SUCCESS)
-		return EXIT_FAILURE;
-
-	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;
-
-free_buff:
-	free(path_buff);
-out:
-	return res;
-}
diff --git a/sys.h b/sys.h
index e1d0f9b..5f0280f 100644
--- a/sys.h
+++ b/sys.h
@@ -33,8 +33,6 @@
 #define SYS_MESH_IFACE_FMT	SYS_IFACE_PATH"/%s/batman_adv/mesh_iface"
 #define SYS_IFACE_STATUS_FMT	SYS_IFACE_PATH"/%s/batman_adv/iface_status"
 #define SYS_VLAN_PATH		SYS_IFACE_PATH"/%s/mesh/vlan%d/"
-#define SYS_ROUTING_ALGO_FMT	SYS_IFACE_PATH"/%s/mesh/routing_algo"
-#define SYS_SELECTED_RA_PATH	"/sys/module/batman_adv/parameters/routing_algo"
 #define VLAN_ID_MAX_LEN		4
 
 enum batctl_settings_list {
@@ -63,6 +61,5 @@ extern const char *sysfs_param_server[];
 extern const struct settings_data batctl_settings[BATCTL_SETTINGS_NUM];
 
 int handle_sys_setting(char *mesh_iface, int setting, int argc, char **argv);
-int handle_ra_setting(int argc, char **argv);
 
 #endif
-- 
2.19.1


  parent reply	other threads:[~2018-10-21 22:54 UTC|newest]

Thread overview: 41+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-10-21 22:54 [B.A.T.M.A.N.] [PATCH 00/38] batctl: pre-netlink restructuring, part 1 Sven Eckelmann
2018-10-21 22:54 ` [B.A.T.M.A.N.] [PATCH 01/38] batctl: Drop unused define SOCKET_PATH_FMT Sven Eckelmann
2018-10-21 22:54 ` [B.A.T.M.A.N.] [PATCH 02/38] batctl: Use common code organization for statistics Sven Eckelmann
2018-10-21 22:54 ` [B.A.T.M.A.N.] [PATCH 03/38] batctl: Drop legacy vis_* related warning messages Sven Eckelmann
2018-10-21 22:54 ` [B.A.T.M.A.N.] [PATCH 04/38] batctl: Move loglevel command to separate file Sven Eckelmann
2018-10-21 22:54 ` [B.A.T.M.A.N.] [PATCH 05/38] batctl: Move log " Sven Eckelmann
2018-10-21 22:54 ` [B.A.T.M.A.N.] [PATCH 06/38] batctl: Move gw_mode " Sven Eckelmann
2018-10-21 22:54 ` Sven Eckelmann [this message]
2018-10-21 22:54 ` [B.A.T.M.A.N.] [PATCH 08/38] batctl: Rename tp_meter to throughputmeter Sven Eckelmann
2018-10-21 22:54 ` [B.A.T.M.A.N.] [PATCH 09/38] batctl: Introduce datastructure for subcommands Sven Eckelmann
2018-10-21 22:54 ` [B.A.T.M.A.N.] [PATCH 10/38] batctl: Add per command flags Sven Eckelmann
2018-10-21 22:54 ` [B.A.T.M.A.N.] [PATCH 11/38] batctl: Use command structure for remaining subcommands Sven Eckelmann
2018-10-21 22:54 ` [B.A.T.M.A.N.] [PATCH 12/38] batctl: Use getopt to parse main options Sven Eckelmann
2018-10-21 22:54 ` [B.A.T.M.A.N.] [PATCH 13/38] batctl: Store usage line next to command Sven Eckelmann
2018-10-21 22:55 ` [B.A.T.M.A.N.] [PATCH 14/38] batctl: Prepare command infrastructure for shared functions Sven Eckelmann
2018-10-21 22:55 ` [B.A.T.M.A.N.] [PATCH 15/38] batctl: Add type to command to structure usage output Sven Eckelmann
2018-10-21 22:55 ` [B.A.T.M.A.N.] [PATCH 16/38] batctl: Convert debug table to command infrastructure Sven Eckelmann
2018-10-21 22:55 ` [B.A.T.M.A.N.] [PATCH 17/38] batctl: Convert sysfs settings " Sven Eckelmann
2018-10-21 22:55 ` [B.A.T.M.A.N.] [PATCH 18/38] batctl: Move backbonetable debug table to own file Sven Eckelmann
2018-10-21 22:55 ` [B.A.T.M.A.N.] [PATCH 19/38] batctl: Move claimtable " Sven Eckelmann
2018-10-21 22:55 ` [B.A.T.M.A.N.] [PATCH 20/38] batctl: Move dat_cache " Sven Eckelmann
2018-10-21 22:55 ` [B.A.T.M.A.N.] [PATCH 21/38] batctl: Move gateways " Sven Eckelmann
2018-10-21 22:55 ` [B.A.T.M.A.N.] [PATCH 22/38] batctl: Move mcast_flags " Sven Eckelmann
2018-10-21 22:55 ` [B.A.T.M.A.N.] [PATCH 23/38] batctl: Move nc_nodes " Sven Eckelmann
2018-10-21 22:55 ` [B.A.T.M.A.N.] [PATCH 24/38] batctl: Move neighbors " Sven Eckelmann
2018-10-21 22:55 ` [B.A.T.M.A.N.] [PATCH 25/38] batctl: Move originators " Sven Eckelmann
2018-10-21 22:55 ` [B.A.T.M.A.N.] [PATCH 26/38] batctl: Move transglobal " Sven Eckelmann
2018-10-21 22:55 ` [B.A.T.M.A.N.] [PATCH 27/38] batctl: Move translocal " Sven Eckelmann
2018-10-21 22:55 ` [B.A.T.M.A.N.] [PATCH 28/38] batctl: Move aggregation setting " Sven Eckelmann
2018-10-21 22:55 ` [B.A.T.M.A.N.] [PATCH 29/38] batctl: Move bonding " Sven Eckelmann
2018-10-21 22:55 ` [B.A.T.M.A.N.] [PATCH 30/38] batctl: Move bridge_loop_avoidance " Sven Eckelmann
2018-10-21 22:55 ` [B.A.T.M.A.N.] [PATCH 31/38] batctl: Move distributed_arp_table " Sven Eckelmann
2018-10-21 22:55 ` [B.A.T.M.A.N.] [PATCH 32/38] batctl: Move fragmentation " Sven Eckelmann
2018-10-21 22:55 ` [B.A.T.M.A.N.] [PATCH 33/38] batctl: Move isolation_mark " Sven Eckelmann
2018-10-21 22:55 ` [B.A.T.M.A.N.] [PATCH 34/38] batctl: Move multicast_mode " Sven Eckelmann
2018-10-21 22:55 ` [B.A.T.M.A.N.] [PATCH 35/38] batctl: Move network_coding " Sven Eckelmann
2018-10-21 22:55 ` [B.A.T.M.A.N.] [PATCH 36/38] batctl: Move orig_interval " Sven Eckelmann
2018-10-21 23:02   ` [B.A.T.M.A.N.] [PATCH v2 " Sven Eckelmann
2018-10-21 22:55 ` [B.A.T.M.A.N.] [PATCH 37/38] batctl: Use external netlink socket for debug tables Sven Eckelmann
2018-10-21 22:55 ` [B.A.T.M.A.N.] [PATCH 38/38] batctl: Add command to monitor for netlink events Sven Eckelmann
2018-10-22 16:42 ` [B.A.T.M.A.N.] [PATCH 00/38] batctl: pre-netlink restructuring, part 1 Sven Eckelmann

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=20181021225524.8155-8-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).