From c56a11d4713ceb12ccd2f7d2a2c1b48b13a10dee Mon Sep 17 00:00:00 2001
From: Sven Eckelmann <sven@narfation.org>
Date: Wed, 28 Feb 2018 10:06:57 +0100
Subject: [RFC] batctl: tp_meter: Translate client mac and IPs to originator
 mac

A user may want to check the path selected by batman-adv for any client
when only the MAC of the device with the IP is known, but not the
originator address. batctl can assist the user by parsing the global
translation table to automatically translate it for him.

The tp_meter itself will only be done against the originator and not
against the actual client because the client cannot understand batman-adv
tp_meter packets.

The translation from IP to mac address can also be tried by the tp_meter
code when the interface is in the same IP subnet as the destination IP.

Signed-off-by: Sven Eckelmann <sven@narfation.org>
---
 man/batctl.8 |  5 ++++-
 tp_meter.c   | 17 +++++++++++++----
 2 files changed, 17 insertions(+), 5 deletions(-)

diff --git a/man/batctl.8 b/man/batctl.8
index 7cf66ca..79f38b2 100644
--- a/man/batctl.8
+++ b/man/batctl.8
@@ -333,7 +333,7 @@ specific sequence number, min. Furthermore using "\-o" you can filter the output
 given batctl will not replace the MAC addresses with bat\-host names in the output.
 .RE
 .br
-.IP "\fBthroughputmeter\fP|\fBtp\fP \fBMAC\fP"
+.IP "\fBthroughputmeter\fP|\fBtp\fP[\fB\-T\fP] \fBMAC_address\fP|\fBbat\-host_name\fP|\fBhost_name\fP|\fBIP_address\fP"
 This command starts a throughput test entirely controlled by batman module in
 kernel space: the computational resources needed to align memory and copy data
 between user and kernel space that are required by other user space tools may
@@ -348,6 +348,9 @@ of a successful experiment the throughput in KBytes per second is returned,
 togheter with the experiment duration in millisecond and the amount of bytes
 transferred. If too many packets are lost or the specified MAC address is not
 reachable, a message notifing the error is returned instead of the result.
+
+With "\-T" you can disable the automatic translation of a client MAC address to
+the originator address which is responsible for this client.
 .RE
 .br
 .SH FILES
diff --git a/tp_meter.c b/tp_meter.c
index c790485..7d24272 100644
--- a/tp_meter.c
+++ b/tp_meter.c
@@ -383,10 +383,11 @@ void tp_sig_handler(int sig)
 
 static void tp_meter_usage(void)
 {
-	fprintf(stderr, "Usage: batctl tp [parameters] <MAC>\n");
+	fprintf(stderr, "Usage: batctl tp [parameters] mac|bat-host|host_name|IPv4_address\n");
 	fprintf(stderr, "Parameters:\n");
 	fprintf(stderr, "\t -t <time> test length in milliseconds\n");
 	fprintf(stderr, "\t -n don't convert addresses to bat-host names\n");
+	fprintf(stderr, " \t -T don't try to translate mac to originator address\n");
 }
 
 int tp_meter(char *mesh_iface, int argc, char **argv)
@@ -396,6 +397,7 @@ int tp_meter(char *mesh_iface, int argc, char **argv)
 	char *dst_string;
 	int ret = EXIT_FAILURE;
 	int found_args = 1, read_opt = USE_BAT_HOSTS;
+	int disable_translate_mac = 0;
 	uint32_t time = 0;
 	int optchar;
 	struct nl_sock *listen_sock = NULL;
@@ -412,7 +414,7 @@ int tp_meter(char *mesh_iface, int argc, char **argv)
 		.found = false,
 	};
 
-	while ((optchar = getopt(argc, argv, "t:n")) != -1) {
+	while ((optchar = getopt(argc, argv, "t:nT")) != -1) {
 		switch (optchar) {
 		case 't':
 			found_args += 2;
@@ -422,6 +424,10 @@ int tp_meter(char *mesh_iface, int argc, char **argv)
 			read_opt &= ~USE_BAT_HOSTS;
 			found_args += 1;
 			break;
+		case 'T':
+			disable_translate_mac = 1;
+			found_args += 1;
+			break;
 		default:
 			tp_meter_usage();
 			return EXIT_FAILURE;
@@ -443,16 +449,19 @@ int tp_meter(char *mesh_iface, int argc, char **argv)
 		dst_mac = &bat_host->mac_addr;
 
 	if (!dst_mac) {
-		dst_mac = ether_aton(dst_string);
+		dst_mac = resolve_mac(dst_string);
 
 		if (!dst_mac) {
-			printf("Error - the tp meter destination is not a mac address or bat-host name: %s\n",
+			printf("Error - mac address of the tp meter destination could not be resolved and is not a bat-host name %s\n",
 			       dst_string);
 			goto out;
 		}
 	}
 
 
+	if (!disable_translate_mac)
+		dst_mac = translate_mac(mesh_iface, dst_mac);
+
 	if (bat_host && (read_opt & USE_BAT_HOSTS))
 		dst_string = bat_host->name;
 	else
-- 
2.11.0

