[Asterisk-code-review] res_rtp_asterisk: Add a DEVMODE RTP drop packets CLI command (asterisk[18])

Kevin Harwell asteriskteam at digium.com
Thu Mar 25 18:01:35 CDT 2021


Kevin Harwell has uploaded this change for review. ( https://gerrit.asterisk.org/c/asterisk/+/15707 )


Change subject: res_rtp_asterisk: Add a DEVMODE RTP drop packets CLI command
......................................................................

res_rtp_asterisk: Add a DEVMODE RTP drop packets CLI command

This patch makes it so when Asterisk is compiled in DEVMODE a CLI
command is available that allows someone to drop incoming RTP
packets. The command allows for dropping of packets once, or on a
timed interval (e.g. drop 10 packets every 5 seconds). A user can
also specify to drop packets by IP address.

Change-Id: I25fa7ae9bad6ed68e273bbcccf0ee51cae6e7024
---
M res/res_rtp_asterisk.c
1 file changed, 210 insertions(+), 0 deletions(-)



  git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/07/15707/1

diff --git a/res/res_rtp_asterisk.c b/res/res_rtp_asterisk.c
index b0a3f2a..534ee89 100644
--- a/res/res_rtp_asterisk.c
+++ b/res/res_rtp_asterisk.c
@@ -63,6 +63,7 @@
 #include <ifaddrs.h>
 #endif
 
+#include "asterisk/conversions.h"
 #include "asterisk/options.h"
 #include "asterisk/logger_category.h"
 #include "asterisk/stun.h"
@@ -7529,6 +7530,73 @@
 	return AST_LIST_FIRST(&frames);
 }
 
+#ifdef AST_DEVMODE
+
+struct rtp_drop_packets_data {
+	unsigned int use_random_num;
+	unsigned int use_random_interval;
+	unsigned int num_to_drop;
+	unsigned int num_dropped;
+	struct timeval interval;
+	struct timeval next;
+	struct ast_sockaddr addr;
+	unsigned int port;
+} drop_packets_data;
+
+static void drop_packets_data_update(struct timeval tv)
+{
+	drop_packets_data.num_dropped = drop_packets_data.use_random_num ?
+		ast_random() % drop_packets_data.num_to_drop : 0;
+
+	if (drop_packets_data.use_random_interval) {
+		/* Calculate as a percentage of specified drop packets interval */
+		struct timeval interval = ast_time_create_by_unit(ast_time_tv_to_usec(
+			&drop_packets_data.interval) * ((double)(ast_random() % 100 + 1) / 100),
+			TIME_UNIT_MICROSECOND);
+
+		drop_packets_data.next = ast_tvadd(tv, interval);
+	} else {
+		drop_packets_data.next = ast_tvadd(tv, drop_packets_data.interval);
+	}
+}
+
+static int should_drop_packets(struct ast_sockaddr *addr)
+{
+	struct timeval tv;
+
+	if (!drop_packets_data.num_to_drop) {
+		return 0;
+	}
+
+	if (!ast_sockaddr_isnull(&drop_packets_data.addr) &&
+		(drop_packets_data.port ?
+			ast_sockaddr_cmp(&drop_packets_data.addr, addr) :
+			ast_sockaddr_cmp_addr(&drop_packets_data.addr, addr)) != 0) {
+		return 0;
+	}
+
+	if (drop_packets_data.num_dropped < drop_packets_data.num_to_drop) {
+		++drop_packets_data.num_dropped;
+		return 1;
+	}
+
+	if (ast_tvzero(drop_packets_data.interval)) {
+		/* If no interval then drop specified number of packets and be done */
+		drop_packets_data.num_to_drop = 0;
+		return 0;
+	}
+
+	tv = ast_tvnow();
+	if (ast_tvcmp(tv, drop_packets_data.next) == -1) {
+		return 0;
+	}
+
+	drop_packets_data_update(tv);
+	return 1;
+}
+
+#endif
+
 /*! \pre instance is locked */
 static struct ast_frame *ast_rtp_read(struct ast_rtp_instance *instance, int rtcp)
 {
@@ -7823,6 +7891,14 @@
 	seqno &= 0xffff;
 	timestamp = ntohl(rtpheader[1]);
 
+#ifdef AST_DEVMODE
+	if (should_drop_packets(&addr)) {
+		ast_debug(0, "(%p) RTP: drop received packet from %s (type %-2.2d, seq %-6.6u, ts %-6.6u, len %-6.6d)\n",
+			instance, ast_sockaddr_stringify(&addr), payloadtype, seqno, timestamp, res - hdrlen);
+		return &ast_null_frame;
+	}
+#endif
+
 	if (rtp_debug_test_addr(&addr)) {
 		ast_verbose("Got  RTP packet from    %s (type %-2.2d, seq %-6.6u, ts %-6.6u, len %-6.6d)\n",
 			    ast_sockaddr_stringify(&addr),
@@ -9088,11 +9164,145 @@
 	return CLI_SUCCESS;
 }
 
+#ifdef AST_DEVMODE
+
+static unsigned int use_random(struct ast_cli_args *a, int pos, unsigned int index)
+{
+	return pos >= index && !ast_strlen_zero(a->argv[index - 1]) &&
+		!strcasecmp(a->argv[index - 1], "random");
+}
+
+static char *handle_cli_rtp_drop_packets(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
+{
+	static const char * const completions_2[] = { "stop", "<N>", NULL };
+	static const char * const completions_3[] = { "random", "incoming packets", NULL };
+	static const char * const completions_5[] = { "on", "every", NULL };
+	static const char * const completions_units[] =	{ "random", "usec", "msec", "sec", "min", NULL };
+
+	unsigned int use_random_num = 0;
+	unsigned int use_random_interval = 0;
+	unsigned int num_to_drop = 0;
+	unsigned int interval = 0;
+	const char *interval_s = NULL;
+	const char *unit_s = NULL;
+	struct ast_sockaddr addr;
+	const char *addr_s = NULL;
+
+	switch (cmd) {
+	case CLI_INIT:
+		e->command = "rtp drop";
+		e->usage =
+			"Usage: rtp drop [stop|[<N> [random] incoming packets[ every <N> [random] {usec|msec|sec|min}][ on <ip[:port]>]]\n"
+			"       Drop RTP packets.\n";
+		return NULL;
+	case CLI_GENERATE:
+		use_random_num = use_random(a, a->pos, 4);
+		use_random_interval = use_random(a, a->pos, 8 + use_random_num) ||
+			use_random(a, a->pos, 10 + use_random_num);
+
+		switch (a->pos - use_random_num - use_random_interval) {
+		case 2:
+			return ast_cli_complete(a->word, completions_2, a->n);
+		case 3:
+			return ast_cli_complete(a->word, completions_3 + use_random_num, a->n);
+		case 5:
+			return ast_cli_complete(a->word, completions_5, a->n);
+		case 7:
+			if (!strcasecmp(a->argv[a->pos - 2], "on")) {
+				ast_cli_completion_add(ast_strdup("every"));
+				break;
+			}
+			/* Fall through */
+		case 9:
+			if (!strcasecmp(a->argv[a->pos - 2 - use_random_interval], "every")) {
+				return ast_cli_complete(a->word, completions_units + use_random_interval, a->n);
+			}
+			break;
+		case 8:
+			if (!strcasecmp(a->argv[a->pos - 3 - use_random_interval], "every")) {
+				ast_cli_completion_add(ast_strdup("on"));
+			}
+			break;
+		}
+
+		return NULL;
+	}
+
+	if (a->argc < 3) {
+		return CLI_SHOWUSAGE;
+	}
+
+	use_random_num = use_random(a, a->argc, 4);
+	use_random_interval = use_random(a, a->argc, 8 + use_random_num) ||
+		use_random(a, a->argc, 10 + use_random_num);
+
+	if (!strcasecmp(a->argv[2], "stop")) {
+		/* rtp drop stop */
+	} else if (a->argc < 5) {
+		return CLI_SHOWUSAGE;
+	} else if (ast_str_to_uint(a->argv[2], &num_to_drop)) {
+		ast_cli(a->fd, "%s is not a valid number of packets to drop\n", a->argv[2]);
+		return CLI_FAILURE;
+	} else if (a->argc - use_random_num == 5) {
+		/* rtp drop <N> [random] incoming packets */
+	} else if (a->argc - use_random_num >= 7 && !strcasecmp(a->argv[5 + use_random_num], "on")) {
+		/* rtp drop <N> [random] incoming packets on <ip[:port]> */
+		addr_s = a->argv[6 + use_random_num];
+		if (a->argc - use_random_num - use_random_interval == 10 &&
+				!strcasecmp(a->argv[7 + use_random_num], "every")) {
+			/* rtp drop <N> [random] incoming packets on <ip[:port]> every <N> [random] {usec|msec|sec|min} */
+			interval_s = a->argv[8 + use_random_num];
+			unit_s = a->argv[9 + use_random_num + use_random_interval];
+		}
+	} else if (a->argc - use_random_num >= 8 && !strcasecmp(a->argv[5 + use_random_num], "every")) {
+		/* rtp drop <N> [random] incoming packets every <N> [random] {usec|msec|sec|min} */
+		interval_s = a->argv[6 + use_random_num];
+		unit_s = a->argv[7 + use_random_num + use_random_interval];
+		if (a->argc == 10 + use_random_num + use_random_interval &&
+				!strcasecmp(a->argv[8 + use_random_num + use_random_interval], "on")) {
+			/* rtp drop <N> [random] incoming packets every <N> [random] {usec|msec|sec|min} on <ip[:port]> */
+			addr_s = a->argv[9 + use_random_num + use_random_interval];
+		}
+	} else {
+		return CLI_SHOWUSAGE;
+	}
+
+	if (a->argc - use_random_num >= 8 && !interval_s && !addr_s) {
+		return CLI_SHOWUSAGE;
+	}
+
+	if (interval_s && ast_str_to_uint(interval_s, &interval)) {
+		ast_cli(a->fd, "%s is not a valid interval number\n", interval_s);
+		return CLI_FAILURE;
+	}
+
+	memset(&addr, 0, sizeof(addr));
+	if (addr_s && !ast_sockaddr_parse(&addr, addr_s, 0)) {
+		ast_cli(a->fd, "%s is not a valid hostname[:port]\n", addr_s);
+		return CLI_FAILURE;
+	}
+
+	drop_packets_data.use_random_num = use_random_num;
+	drop_packets_data.use_random_interval = use_random_interval;
+	drop_packets_data.num_to_drop = num_to_drop;
+	drop_packets_data.interval = ast_time_create_by_unit_str(interval, unit_s);
+	ast_sockaddr_copy(&drop_packets_data.addr, &addr);
+	drop_packets_data.port = ast_sockaddr_port(&addr);
+
+	drop_packets_data_update(ast_tvnow());
+
+	return CLI_SUCCESS;
+}
+#endif
+
 static struct ast_cli_entry cli_rtp[] = {
 	AST_CLI_DEFINE(handle_cli_rtp_set_debug,  "Enable/Disable RTP debugging"),
 	AST_CLI_DEFINE(handle_cli_rtp_settings,   "Display RTP settings"),
 	AST_CLI_DEFINE(handle_cli_rtcp_set_debug, "Enable/Disable RTCP debugging"),
 	AST_CLI_DEFINE(handle_cli_rtcp_set_stats, "Enable/Disable RTCP stats"),
+#ifdef AST_DEVMODE
+	AST_CLI_DEFINE(handle_cli_rtp_drop_packets, "Drop RTP packets"),
+#endif
 };
 
 static int rtp_reload(int reload, int by_external_config)

-- 
To view, visit https://gerrit.asterisk.org/c/asterisk/+/15707
To unsubscribe, or for help writing mail filters, visit https://gerrit.asterisk.org/settings

Gerrit-Project: asterisk
Gerrit-Branch: 18
Gerrit-Change-Id: I25fa7ae9bad6ed68e273bbcccf0ee51cae6e7024
Gerrit-Change-Number: 15707
Gerrit-PatchSet: 1
Gerrit-Owner: Kevin Harwell <kharwell at digium.com>
Gerrit-MessageType: newchange
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-code-review/attachments/20210325/8e58f929/attachment-0001.html>


More information about the asterisk-code-review mailing list