Debian Codegraph @

funcs/func_frame_drop.c

raw symbols SCIP
1 /*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2021, Naveen Albert
5 *
6 * Naveen Albert <[email protected]>
7 *
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
13 *
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
17 */
18
19 /*! \file
20 *
21 * \brief Function that drops specified frames from channels
22 *
23 * \author Naveen Albert <[email protected]>
24 *
25 * \ingroup functions
26 */
27
28 /*** MODULEINFO
29 <support_level>extended</support_level>
30 ***/
31
32 #include "asterisk.h"
33
34 #include "asterisk/module.h"
35 #include "asterisk/channel.h"
36 #include "asterisk/pbx.h"
37 #include "asterisk/framehook.h"
38
39 /*** DOCUMENTATION
40 <function name="FRAME_DROP" language="en_US">
41 <since>
42 <version>16.21.0</version>
43 <version>18.7.0</version>
44 </since>
45 <synopsis>
46 Drops specific frame types in the TX or RX direction on a channel.
47 </synopsis>
48 <syntax>
49 <parameter name="direction" required="true">
50 <para>List of frame types to be dropped for the specified direction. Direction can be <literal>TX</literal> or <literal>RX</literal>. The <literal>TX</literal> direction will prevent Asterisk from sending frames to a channel, and the <literal>RX</literal> direction will prevent Asterisk from receiving frames from a channel.</para>
51 <para>Subsequent calls to this function will replace previous settings, allowing certain frames to be dropped only temporarily, for instance.</para>
52 <para>Below are the different types of frames that can be dropped. Other actions may need to be taken in conjunction with use of this function:
53 for instance, if you drop ANSWER control frames, you should explicitly use <literal>Progress()</literal> for your call or undesired behavior
54 may occur.</para>
55 <enumlist>
56 <enum name = "DTMF_BEGIN" />
57 <enum name = "DTMF_END" />
58 <enum name = "VOICE" />
59 <enum name = "VIDEO" />
60 <enum name = "CONTROL" />
61 <enum name = "NULL" />
62 <enum name = "IAX" />
63 <enum name = "TEXT" />
64 <enum name = "TEXT_DATA" />
65 <enum name = "IMAGE" />
66 <enum name = "HTML" />
67 <enum name = "CNG" />
68 <enum name = "MODEM" />
69 </enumlist>
70 <para>The following CONTROL frames can also be dropped:</para>
71 <enumlist>
72 <enum name = "RING" />
73 <enum name = "RINGING" />
74 <enum name = "ANSWER" />
75 <enum name = "BUSY" />
76 <enum name = "TAKEOFFHOOK" />
77 <enum name = "OFFHOOK" />
78 <enum name = "CONGESTION" />
79 <enum name = "FLASH" />
80 <enum name = "WINK" />
81 <enum name = "PROGRESS" />
82 <enum name = "PROCEEDING" />
83 <enum name = "HOLD" />
84 <enum name = "UNHOLD" />
85 <enum name = "VIDUPDATE" />
86 <enum name = "CONNECTED_LINE" />
87 <enum name = "REDIRECTING" />
88 </enumlist>
89 </parameter>
90 </syntax>
91 <description>
92 <para>Examples:</para>
93 <example title="Drop only DTMF frames towards this channel">
94 exten => 1,1,Set(FRAME_DROP(TX)=DTMF_BEGIN,DTMF_END)
95 </example>
96 <example title="Drop only Answer control frames towards this channel">
97 exten => 1,1,Set(FRAME_DROP(TX)=ANSWER)
98 </example>
99 <example title="Drop only DTMF frames received on this channel">
100 exten => 1,1,Set(FRAME_DROP(RX)=DTMF_BEGIN,DTMF_END)
101 </example>
102 </description>
103 </function>
104 ***/
105
106 static struct {
107 enum ast_frame_type type;
108 const char *str;
109 } frametype2str[] = {
110 { AST_FRAME_DTMF_BEGIN, ",DTMF_BEGIN," },
111 { AST_FRAME_DTMF_END, ",DTMF_END," },
112 { AST_FRAME_VOICE, ",VOICE," },
113 { AST_FRAME_VIDEO, ",VIDEO," },
114 { AST_FRAME_CONTROL, ",CONTROL," },
115 { AST_FRAME_NULL, ",NULL," },
116 { AST_FRAME_IAX, ",IAX," },
117 { AST_FRAME_TEXT, ",TEXT," },
118 { AST_FRAME_TEXT_DATA, ",TEXT_DATA," },
119 { AST_FRAME_IMAGE, ",IMAGE," },
120 { AST_FRAME_HTML, ",HTML," },
121 { AST_FRAME_CNG, ",CNG," },
122 { AST_FRAME_MODEM, ",MODEM," },
123 };
124
125 static struct {
126 int type;
127 const char *str;
128 } controlframetype2str[] = {
129 { AST_CONTROL_RING, ",RING," },
130 { AST_CONTROL_RINGING, ",RINGING," },
131 { AST_CONTROL_ANSWER, ",ANSWER," },
132 { AST_CONTROL_BUSY, ",BUSY," },
133 { AST_CONTROL_TAKEOFFHOOK, ",TAKEOFFHOOK," },
134 { AST_CONTROL_OFFHOOK, ",OFFHOOK," },
135 { AST_CONTROL_CONGESTION, ",CONGESTION," },
136 { AST_CONTROL_FLASH, ",FLASH," },
137 { AST_CONTROL_WINK, ",WINK," },
138 { AST_CONTROL_PROGRESS, ",PROGRESS," },
139 { AST_CONTROL_PROCEEDING, ",PROCEEDING," },
140 { AST_CONTROL_HOLD, ",HOLD," },
141 { AST_CONTROL_UNHOLD, ",UNHOLD," },
142 { AST_CONTROL_VIDUPDATE, ",VIDUPDATE," },
143 { AST_CONTROL_CONNECTED_LINE, ",CONNECTED_LINE," },
144 { AST_CONTROL_REDIRECTING, ",REDIRECTING," },
145 };
146
147 enum direction {
148 TX = 0,
149 RX,
150 };
151
152 struct frame_drop_data {
153 enum direction list_type;
154 int values[ARRAY_LEN(frametype2str)];
155 int controlvalues[ARRAY_LEN(controlframetype2str)];
156 };
157
158 static void datastore_destroy_cb(void *data) {
159 ast_free(data);
160 }
161
162 static const struct ast_datastore_info frame_drop_datastore = {
163 .type = "framedrop",
164 .destroy = datastore_destroy_cb
165 };
166
167 static void hook_destroy_cb(void *framedata)
168 {
169 ast_free(framedata);
170 }
171
172 static struct ast_frame *hook_event_cb(struct ast_channel *chan, struct ast_frame *frame, enum ast_framehook_event event, void *data)
173 {
174 int i;
175 int drop_frame = 0;
176 char buf[64];
177 struct frame_drop_data *framedata = data;
178 if (!frame) {
179 return frame;
180 }
181
182 if (!((event == AST_FRAMEHOOK_EVENT_WRITE && framedata->list_type == TX) ||
183 (event == AST_FRAMEHOOK_EVENT_READ && framedata->list_type == RX))) {
184 return frame;
185 }
186
187 if (frame->frametype == AST_FRAME_CONTROL) {
188 for (i = 0; i < ARRAY_LEN(controlframetype2str); i++) {
189 if (frame->subclass.integer == controlframetype2str[i].type) {
190 if (framedata->controlvalues[i]) {
191 drop_frame = 1;
192 ast_frame_subclass2str(frame, buf, sizeof(buf), NULL, 0);
193 }
194 break;
195 }
196 }
197 } else {
198 for (i = 0; i < ARRAY_LEN(frametype2str); i++) {
199 if (frame->frametype == frametype2str[i].type) {
200 if (framedata->values[i]) {
201 drop_frame = 1;
202 ast_frame_type2str(frame->frametype, buf, sizeof(buf));
203 }
204 break;
205 }
206 }
207 }
208
209 if (drop_frame) {
210 ast_frfree(frame);
211 frame = &ast_null_frame;
212 ast_debug(2, "Dropping %s frame\n", buf);
213 }
214 return frame;
215 }
216
217 static int frame_drop_helper(struct ast_channel *chan, const char *cmd, char *data, const char *value)
218 {
219 char *buffer;
220 struct frame_drop_data *framedata;
221 struct ast_datastore *datastore = NULL;
222 struct ast_framehook_interface interface = {
223 .version = AST_FRAMEHOOK_INTERFACE_VERSION,
224 .event_cb = hook_event_cb,
225 .destroy_cb = hook_destroy_cb,
226 };
227 int i = 0;
228
229 if (!(framedata = ast_calloc(1, sizeof(*framedata)))) {
230 return 0;
231 }
232
233 interface.data = framedata;
234
235 if (!strcasecmp(data, "RX")) {
236 framedata->list_type = RX;
237 } else {
238 framedata->list_type = TX;
239 }
240
241 if (ast_asprintf(&buffer, ",%s,", value) < 0) {
242 ast_free(framedata);
243 return -1;
244 }
245 for (i = 0; i < ARRAY_LEN(frametype2str); i++) {
246 if (strcasestr(buffer, frametype2str[i].str)) {
247 framedata->values[i] = 1;
248 }
249 }
250
251 for (i = 0; i < ARRAY_LEN(controlframetype2str); i++) {
252 if (strcasestr(buffer, controlframetype2str[i].str)) {
253 framedata->controlvalues[i] = 1;
254 }
255 }
256 ast_free(buffer);
257
258 ast_channel_lock(chan);
259 i = ast_framehook_attach(chan, &interface);
260 if (i >= 0) {
261 int *id;
262 if ((datastore = ast_channel_datastore_find(chan, &frame_drop_datastore, NULL))) {
263 id = datastore->data;
264 ast_framehook_detach(chan, *id);
265 ast_channel_datastore_remove(chan, datastore);
266 ast_datastore_free(datastore);
267 }
268
269 if (!(datastore = ast_datastore_alloc(&frame_drop_datastore, NULL))) {
270 ast_framehook_detach(chan, i);
271 ast_channel_unlock(chan);
272 return 0;
273 }
274
275 if (!(id = ast_calloc(1, sizeof(int)))) {
276 ast_datastore_free(datastore);
277 ast_framehook_detach(chan, i);
278 ast_channel_unlock(chan);
279 return 0;
280 }
281
282 *id = i; /* Store off the id. The channel is still locked so it is safe to access this ptr. */
283 datastore->data = id;
284 ast_channel_datastore_add(chan, datastore);
285 }
286 ast_channel_unlock(chan);
287
288 return 0;
289 }
290
291 static struct ast_custom_function frame_drop_function = {
292 .name = "FRAME_DROP",
293 .write = frame_drop_helper,
294 };
295
296 static int unload_module(void)
297 {
298 return ast_custom_function_unregister(&frame_drop_function);
299 }
300
301 static int load_module(void)
302 {
303 int res = ast_custom_function_register(&frame_drop_function);
304 return res ? AST_MODULE_LOAD_DECLINE : AST_MODULE_LOAD_SUCCESS;
305 }
306
307 AST_MODULE_INFO_STANDARD_EXTENDED(ASTERISK_GPL_KEY, "Function to drop frames on a channel.");