[Asterisk-code-review] func_framedrop: New function (asterisk[master])
N A
asteriskteam at digium.com
Wed Jun 16 15:31:41 CDT 2021
N A has uploaded this change for review. ( https://gerrit.asterisk.org/c/asterisk/+/16071 )
Change subject: func_framedrop: New function
......................................................................
func_framedrop: New function
Adds function to selectively drop specified frames
in the TX or RX direction on a channel, including
control frames.
ASTERISK-29478
Change-Id: I8147c9d55d74e2e48861edba6b22f930920541ec
---
A doc/CHANGES-staging/func_framedrop.txt
A funcs/func_framedrop.c
2 files changed, 342 insertions(+), 0 deletions(-)
git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/71/16071/1
diff --git a/doc/CHANGES-staging/func_framedrop.txt b/doc/CHANGES-staging/func_framedrop.txt
new file mode 100644
index 0000000..c17bccd
--- /dev/null
+++ b/doc/CHANGES-staging/func_framedrop.txt
@@ -0,0 +1,5 @@
+Subject: func_framedrop
+
+New function to selectively drop specified frames
+in either direction on a channel.
+
diff --git a/funcs/func_framedrop.c b/funcs/func_framedrop.c
new file mode 100644
index 0000000..6b0e2fa
--- /dev/null
+++ b/funcs/func_framedrop.c
@@ -0,0 +1,337 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2021, Naveen Albert
+ *
+ * Naveen Albert <asterisk at phreaknet.org>
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License Version 2. See the LICENSE file
+ * at the top of the source tree.
+ */
+
+/*! \file
+ *
+ * \brief Function that drops specified frames from channels
+ *
+ * \author Naveen Albert <asterisk at phreaknet.org>
+ *
+ * \ingroup functions
+ */
+
+/*** MODULEINFO
+ <support_level>extended</support_level>
+ ***/
+
+#include "asterisk.h"
+
+#include "asterisk/module.h"
+#include "asterisk/channel.h"
+#include "asterisk/pbx.h"
+#include "asterisk/framehook.h"
+
+/*** DOCUMENTATION
+ <function name="FRAME_DROP" language="en_US">
+ <synopsis>
+ Intercepts specified frames on a channel instead of passing them on.
+ </synopsis>
+ <syntax>
+ <parameter name="filter list type" required="true">
+ <para>A filter can be applied to the trace to limit what frames are suppressed. This
+ filter can be a <literal>TX</literal> or <literal>RX</literal> list of frame types
+ and control frame types.</para>
+ <para>Below are the different types of frames that can be dropped.</para>
+ <enumlist>
+ <enum name = "DTMF_BEGIN" />
+ <enum name = "DTMF_END" />
+ <enum name = "VOICE" />
+ <enum name = "VIDEO" />
+ <enum name = "CONTROL" />
+ <enum name = "NULL" />
+ <enum name = "IAX" />
+ <enum name = "TEXT" />
+ <enum name = "TEXT_DATA" />
+ <enum name = "IMAGE" />
+ <enum name = "HTML" />
+ <enum name = "CNG" />
+ <enum name = "MODEM" />
+ <para>The following CONTROL frames can also be dropped:</para>
+ <enum name = "HANGUP" />
+ <enum name = "RING" />
+ <enum name = "RINGING" />
+ <enum name = "ANSWER" />
+ <enum name = "BUSY" />
+ <enum name = "TAKEOFFHOOK" />
+ <enum name = "OFFHOOK" />
+ <enum name = "CONGESTION" />
+ <enum name = "FLASH" />
+ <enum name = "WINK" />
+ <enum name = "OPTION" />
+ <enum name = "RADIO_KEY" />
+ <enum name = "RADIO_UNKEY" />
+ <enum name = "PROGRESS" />
+ <enum name = "PROCEEDING" />
+ <enum name = "HOLD" />
+ <enum name = "UNHOLD" />
+ <enum name = "VIDUPDATE" />
+ <enum name = "SRCUPDATE" />
+ <enum name = "TRANSFER" />
+ <enum name = "CONNECTED_LINE" />
+ <enum name = "REDIRECTING" />
+ <enum name = "T38_PARAMETERS" />
+ <enum name = "CC" />
+ <enum name = "SRCCHANGE" />
+ <enum name = "READ_ACTION" />
+ <enum name = "AOC" />
+ <enum name = "MCID" />
+ <enum name = "INCOMPLETE" />
+ <enum name = "END_OF_Q" />
+ <enum name = "UPDATE_RTP_PEER" />
+ <enum name = "PVT_CAUSE_CODE" />
+ <enum name = "MASQUERADE_NOTIFY" />
+ <enum name = "STREAM_TOPOLOGY_REQUEST_CHANGE" />
+ <enum name = "STREAM_TOPOLOGY_CHANGED" />
+ <enum name = "STREAM_TOPOLOGY_SOURCE_CHANGED" />
+ <enum name = "STREAM_STOP" />
+ <enum name = "STREAM_SUSPEND" />
+ <enum name = "STREAM_RESTART" />
+ <enum name = "STREAM_REVERSE" />
+ <enum name = "STREAM_FORWARD" />
+ <enum name = "RECORD_CANCEL" />
+ <enum name = "RECORD_STOP" />
+ <enum name = "RECORD_SUSPEND" />
+ <enum name = "RECORD_MUTE" />
+ </enumlist>
+ </parameter>
+ </syntax>
+ <description>
+ <para>Examples:</para>
+ <para>exten => 1,1,Set(FRAME_DROP(TX)=DTMF_BEGIN,DTMF_END); drop only DTMF frames towards this channel.</para>
+ <para>exten => 1,1,Set(FRAME_DROP(TX)=ANSWER); drop only ANSWER CONTROL frames towards this channel.</para>
+ <para>exten => 1,1,Set(FRAME_DROP(RX)=DTMF_BEGIN,DTMF_END); drop only DTMF frames received on this channel.</para>
+ </description>
+ </function>
+ ***/
+
+static struct {
+ enum ast_frame_type type;
+ const char *str;
+} frametype2str[] = {
+ { AST_FRAME_DTMF_BEGIN, "DTMF_BEGIN" },
+ { AST_FRAME_DTMF_END, "DTMF_END" },
+ { AST_FRAME_VOICE, "VOICE" },
+ { AST_FRAME_VIDEO, "VIDEO" },
+ { AST_FRAME_CONTROL, "CONTROL" },
+ { AST_FRAME_NULL, "NULL" },
+ { AST_FRAME_IAX, "IAX" },
+ { AST_FRAME_TEXT, "TEXT" },
+ { AST_FRAME_TEXT_DATA, "TEXT_DATA" },
+ { AST_FRAME_IMAGE, "IMAGE" },
+ { AST_FRAME_HTML, "HTML" },
+ { AST_FRAME_CNG, "CNG" },
+ { AST_FRAME_MODEM, "MODEM" },
+};
+
+static struct {
+ int type;
+ const char *str;
+} controlframetype2str[] = {
+ { AST_CONTROL_HANGUP, "HANGUP" },
+ { AST_CONTROL_RING, "RING" },
+ { AST_CONTROL_RINGING, "RINGING" },
+ { AST_CONTROL_ANSWER, "ANSWER" },
+ { AST_CONTROL_BUSY, "BUSY" },
+ { AST_CONTROL_TAKEOFFHOOK, "TAKEOFFHOOK" },
+ { AST_CONTROL_OFFHOOK, "OFFHOOK" },
+ { AST_CONTROL_CONGESTION, "CONGESTION" },
+ { AST_CONTROL_FLASH, "FLASH" },
+ { AST_CONTROL_WINK, "WINK" },
+ { AST_CONTROL_OPTION, "OPTION" },
+ { AST_CONTROL_RADIO_KEY, "RADIO_KEY" },
+ { AST_CONTROL_RADIO_UNKEY, "RADIO_UNKEY" },
+ { AST_CONTROL_PROGRESS, "PROGRESS" },
+ { AST_CONTROL_PROCEEDING, "PROCEEDING" },
+ { AST_CONTROL_HOLD, "HOLD" },
+ { AST_CONTROL_UNHOLD, "UNHOLD" },
+ { AST_CONTROL_VIDUPDATE, "VIDUPDATE" },
+ { AST_CONTROL_SRCUPDATE, "SRCUPDATE" },
+ { AST_CONTROL_TRANSFER, "TRANSFER" },
+ { AST_CONTROL_CONNECTED_LINE, "CONNECTED_LINE" },
+ { AST_CONTROL_REDIRECTING, "REDIRECTING" },
+ { AST_CONTROL_T38_PARAMETERS, "T38_PARAMETERS" },
+ { AST_CONTROL_CC, "CC" },
+ { AST_CONTROL_SRCCHANGE, "SRCCHANGE" },
+ { AST_CONTROL_READ_ACTION, "READ_ACTION" },
+ { AST_CONTROL_AOC, "AOC" },
+ { AST_CONTROL_MCID, "MCID" },
+ { AST_CONTROL_INCOMPLETE, "INCOMPLETE" },
+ { AST_CONTROL_END_OF_Q, "END_OF_Q" },
+ { AST_CONTROL_UPDATE_RTP_PEER, "UPDATE_RTP_PEER" },
+ { AST_CONTROL_PVT_CAUSE_CODE, "PVT_CAUSE_CODE" },
+ { AST_CONTROL_MASQUERADE_NOTIFY, "MASQUERADE_NOTIFY" },
+ { AST_CONTROL_STREAM_TOPOLOGY_REQUEST_CHANGE, "STREAM_TOPOLOGY_REQUEST_CHANGE" },
+ { AST_CONTROL_STREAM_TOPOLOGY_CHANGED, "STREAM_TOPOLOGY_CHANGED" },
+ { AST_CONTROL_STREAM_TOPOLOGY_SOURCE_CHANGED, "STREAM_TOPOLOGY_SOURCE_CHANGED" },
+ { AST_CONTROL_STREAM_STOP, "STREAM_STOP" },
+ { AST_CONTROL_STREAM_SUSPEND, "STREAM_SUSPEND" },
+ { AST_CONTROL_STREAM_RESTART, "STREAM_RESTART" },
+ { AST_CONTROL_STREAM_REVERSE, "STREAM_REVERSE" },
+ { AST_CONTROL_STREAM_FORWARD, "STREAM_FORWARD" },
+ { AST_CONTROL_RECORD_CANCEL, "RECORD_CANCEL" },
+ { AST_CONTROL_RECORD_STOP, "RECORD_STOP" },
+ { AST_CONTROL_RECORD_SUSPEND, "RECORD_SUSPEND" },
+ { AST_CONTROL_RECORD_MUTE, "RECORD_MUTE" },
+};
+
+struct frame_drop_data {
+ int list_type; /* 0 = TX, 1 = RX */
+ int values[ARRAY_LEN(frametype2str)];
+ int controlvalues[ARRAY_LEN(controlframetype2str)];
+};
+
+static void datastore_destroy_cb(void *data) {
+ ast_free(data);
+}
+
+static const struct ast_datastore_info frame_drop_datastore = {
+ .type = "framedrop",
+ .destroy = datastore_destroy_cb
+};
+
+static void hook_destroy_cb(void *framedata)
+{
+ ast_free(framedata);
+}
+
+static struct ast_frame *hook_event_cb(struct ast_channel *chan, struct ast_frame *frame, enum ast_framehook_event event, void *data)
+{
+ int i;
+ int drop_frame = 0;
+ struct frame_drop_data *framedata = data;
+ if (!frame) {
+ return frame;
+ }
+
+ if (!((event == AST_FRAMEHOOK_EVENT_WRITE && framedata->list_type == 1) ||
+ (event == AST_FRAMEHOOK_EVENT_READ && framedata->list_type == 0))) {
+ return frame;
+ }
+
+ if (frame->frametype == AST_FRAME_CONTROL) {
+ for (i = 0; i < ARRAY_LEN(controlframetype2str); i++) {
+ if (frame->subclass.integer == controlframetype2str[i].type) {
+ if (framedata->controlvalues[i]) {
+ drop_frame = 1;
+ }
+ break;
+ }
+ }
+ } else {
+ for (i = 0; i < ARRAY_LEN(frametype2str); i++) {
+ if (frame->frametype == frametype2str[i].type) {
+ if (framedata->values[i]) {
+ drop_frame = 1;
+ }
+ break;
+ }
+ }
+ }
+
+ if (drop_frame) {
+ ast_frfree(frame);
+ frame = &ast_null_frame;
+ }
+ return frame;
+}
+
+static int frame_drop_helper(struct ast_channel *chan, const char *cmd, char *data, const char *value)
+{
+ struct frame_drop_data *framedata;
+ struct ast_datastore *datastore = NULL;
+ struct ast_framehook_interface interface = {
+ .version = AST_FRAMEHOOK_INTERFACE_VERSION,
+ .event_cb = hook_event_cb,
+ .destroy_cb = hook_destroy_cb,
+ };
+ int i = 0;
+
+ if (!(framedata = ast_calloc(1, sizeof(*framedata)))) {
+ return 0;
+ }
+
+ interface.data = framedata;
+
+ if (!strcasecmp(data, "RX")) {
+ framedata->list_type = 1;
+ } else {
+ framedata->list_type = 0;
+ }
+
+ for (i = 0; i < ARRAY_LEN(frametype2str); i++) {
+ if (strcasestr(value, frametype2str[i].str)) {
+ framedata->values[i] = 1;
+ }
+ }
+
+ for (i = 0; i < ARRAY_LEN(controlframetype2str); i++) {
+ if (strcasestr(value, controlframetype2str[i].str)) {
+ framedata->controlvalues[i] = 1;
+ }
+ }
+
+ ast_channel_lock(chan);
+ i = ast_framehook_attach(chan, &interface);
+ if (i >= 0) {
+ int *id;
+ if ((datastore = ast_channel_datastore_find(chan, &frame_drop_datastore, NULL))) {
+ id = datastore->data;
+ ast_framehook_detach(chan, *id);
+ ast_channel_datastore_remove(chan, datastore);
+ ast_datastore_free(datastore);
+ }
+
+ if (!(datastore = ast_datastore_alloc(&frame_drop_datastore, NULL))) {
+ ast_framehook_detach(chan, i);
+ ast_channel_unlock(chan);
+ return 0;
+ }
+
+ if (!(id = ast_calloc(1, sizeof(int)))) {
+ ast_datastore_free(datastore);
+ ast_framehook_detach(chan, i);
+ ast_channel_unlock(chan);
+ return 0;
+ }
+
+ *id = i; /* Store off the id. The channel is still locked so it is safe to access this ptr. */
+ datastore->data = id;
+ ast_channel_datastore_add(chan, datastore);
+ }
+ ast_channel_unlock(chan);
+
+ return 0;
+}
+
+static struct ast_custom_function frame_drop_function = {
+ .name = "FRAME_DROP",
+ .write = frame_drop_helper,
+};
+
+static int unload_module(void)
+{
+ return ast_custom_function_unregister(&frame_drop_function);
+}
+
+static int load_module(void)
+{
+ int res = ast_custom_function_register(&frame_drop_function);
+ return res ? AST_MODULE_LOAD_DECLINE : AST_MODULE_LOAD_SUCCESS;
+}
+
+AST_MODULE_INFO_STANDARD_EXTENDED(ASTERISK_GPL_KEY, "Function to drop frames on a channel.");
--
To view, visit https://gerrit.asterisk.org/c/asterisk/+/16071
To unsubscribe, or for help writing mail filters, visit https://gerrit.asterisk.org/settings
Gerrit-Project: asterisk
Gerrit-Branch: master
Gerrit-Change-Id: I8147c9d55d74e2e48861edba6b22f930920541ec
Gerrit-Change-Number: 16071
Gerrit-PatchSet: 1
Gerrit-Owner: N A <mail at interlinked.x10host.com>
Gerrit-MessageType: newchange
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-code-review/attachments/20210616/628da345/attachment-0001.html>
More information about the asterisk-code-review
mailing list