public inbox for b.a.t.m.a.n@lists.open-mesh.org
 help / color / mirror / Atom feed
From: Simon Wunderlich <sw@simonwunderlich.de>
To: b.a.t.m.a.n@lists.open-mesh.org
Cc: Simon Wunderlich <simon@open-mesh.com>
Subject: [B.A.T.M.A.N.] [PATCH-next v3 1/2] batctl: check for batman interface
Date: Wed, 12 Mar 2014 17:04:47 +0100	[thread overview]
Message-ID: <1394640288-1611-1-git-send-email-sw@simonwunderlich.de> (raw)

From: Simon Wunderlich <simon@open-mesh.com>

Check if the interface supplied by -m (or bat0 by default) is actually a
batman interface. The check is performed by testing if the sysfs path
is available.

This change is required since debugfs now contains originator tables per
hard interface. This might be confusing for users as batctl -m eth0 o
will now (accidently) print these tables without eth0 beeing a batman
interface, assuming eth0 is a hard interface used by batman-adv.

Some reordering of commands were required to first allow commands which
don't depend on a (correctly set up) mesh interface, like bisect or
interface.

Signed-off-by: Simon Wunderlich <simon@open-mesh.com>
---
Changes to PATCHv2:

 * fix VLAN check, this is now similar to the sysfs handling code
---
 main.c |   32 +++++++++++++++++---------------
 sys.c  |   30 ++++++++++++++++++++++++++++++
 sys.h  |    1 +
 3 files changed, 48 insertions(+), 15 deletions(-)

diff --git a/main.c b/main.c
index a37c0b7..44cf49b 100644
--- a/main.c
+++ b/main.c
@@ -137,7 +137,23 @@ int main(int argc, char **argv)
 		exit(EXIT_FAILURE);
 	}
 
-	if ((strcmp(argv[1], "ping") == 0) || (strcmp(argv[1], "p") == 0)) {
+	if ((strcmp(argv[1], "interface") == 0) || (strcmp(argv[1], "if") == 0)) {
+
+		ret = interface(mesh_iface, argc - 1, argv + 1);
+
+	} else if ((strcmp(argv[1], "tcpdump") == 0) || (strcmp(argv[1], "td") == 0)) {
+
+		ret = tcpdump(argc - 1, argv + 1);
+
+#ifdef BATCTL_BISECT
+	} else if ((strcmp(argv[1], "bisect_iv") == 0)) {
+
+		ret = bisect_iv(argc - 1, argv + 1);
+#endif
+	} 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);
+		exit(EXIT_FAILURE);
+	} else if ((strcmp(argv[1], "ping") == 0) || (strcmp(argv[1], "p") == 0)) {
 
 		ret = ping(mesh_iface, argc - 1, argv + 1);
 
@@ -145,14 +161,6 @@ int main(int argc, char **argv)
 
 		ret = traceroute(mesh_iface, argc - 1, argv + 1);
 
-	} else if ((strcmp(argv[1], "tcpdump") == 0) || (strcmp(argv[1], "td") == 0)) {
-
-		ret = tcpdump(argc - 1, argv + 1);
-
-	} else if ((strcmp(argv[1], "interface") == 0) || (strcmp(argv[1], "if") == 0)) {
-
-		ret = interface(mesh_iface, argc - 1, argv + 1);
-
 	} else if ((strcmp(argv[1], "loglevel") == 0) || (strcmp(argv[1], "ll") == 0)) {
 
 		ret = handle_loglevel(mesh_iface, argc - 1, argv + 1);
@@ -179,12 +187,6 @@ int main(int argc, char **argv)
 
 		ret = translate(mesh_iface, argc - 1, argv + 1);
 
-#ifdef BATCTL_BISECT
-	} else if ((strcmp(argv[1], "bisect_iv") == 0)) {
-
-		ret = bisect_iv(argc - 1, argv + 1);
-#endif
-
 	} else {
 
 		for (i = 0; i < BATCTL_SETTINGS_NUM; i++) {
diff --git a/sys.c b/sys.c
index fd6e107..0b98d82 100644
--- a/sys.c
+++ b/sys.c
@@ -566,3 +566,33 @@ out:
 	free(path_buff);
 	return res;
 }
+
+int check_mesh_iface(char *mesh_iface)
+{
+	char *base_dev = NULL;
+	char path_buff[PATH_BUFF_LEN];
+	int ret = -1, vid;
+	DIR *dir;
+
+	/* use the parent interface if this is a VLAN */
+	vid = vlan_get_link(mesh_iface, &base_dev);
+	if (vid >= 0)
+		snprintf(path_buff, PATH_BUFF_LEN, SYS_VLAN_PATH, base_dev, vid);
+	else
+		snprintf(path_buff, PATH_BUFF_LEN, SYS_BATIF_PATH_FMT, mesh_iface);
+	path_buff[PATH_BUFF_LEN - 1] = '\0';
+
+	/* try to open the mesh sys directory */
+	dir = opendir(path_buff);
+	if (!dir)
+		goto out;
+
+	closedir(dir);
+
+	ret = 0;
+out:
+	if (base_dev)
+		free(base_dev);
+
+	return ret;
+}
diff --git a/sys.h b/sys.h
index 5633822..5aec020 100644
--- a/sys.h
+++ b/sys.h
@@ -71,5 +71,6 @@ int interface(char *mesh_iface, int argc, char **argv);
 int handle_loglevel(char *mesh_iface, int argc, char **argv);
 int handle_sys_setting(char *mesh_iface, int setting, int argc, char **argv);
 int handle_gw_setting(char *mesh_iface, int argc, char **argv);
+int check_mesh_iface(char *mesh_iface);
 
 #endif
-- 
1.7.10.4


             reply	other threads:[~2014-03-12 16:04 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-03-12 16:04 Simon Wunderlich [this message]
2014-03-12 16:04 ` [B.A.T.M.A.N.] [PATCH-next v3 2/2] batctl: add option for multiif orig table Simon Wunderlich
2014-03-12 19:26   ` Marek Lindner
2014-03-12 19:25 ` [B.A.T.M.A.N.] [PATCH-next v3 1/2] batctl: check for batman interface Marek Lindner

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=1394640288-1611-1-git-send-email-sw@simonwunderlich.de \
    --to=sw@simonwunderlich.de \
    --cc=b.a.t.m.a.n@lists.open-mesh.org \
    --cc=simon@open-mesh.com \
    /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