| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 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 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 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; |
| 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."); |