| 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 |
#include "asterisk.h" |
| 31 |
|
| 32 |
#include "asterisk/channel.h" |
| 33 |
#include "asterisk/linkedlists.h" |
| 34 |
#include "asterisk/framehook.h" |
| 35 |
#include "asterisk/frame.h" |
| 36 |
|
| 37 |
struct ast_framehook { |
| 38 |
struct ast_framehook_interface i; |
| 39 |
|
| 40 |
struct ast_channel *chan; |
| 41 |
|
| 42 |
unsigned int id; |
| 43 |
|
| 44 |
int detach_and_destroy_me; |
| 45 |
|
| 46 |
AST_LIST_ENTRY(ast_framehook) list; |
| 47 |
}; |
| 48 |
|
| 49 |
struct ast_framehook_list { |
| 50 |
|
| 51 |
unsigned int count; |
| 52 |
|
| 53 |
unsigned int id_count; |
| 54 |
AST_LIST_HEAD_NOLOCK(, ast_framehook) list; |
| 55 |
}; |
| 56 |
|
| 57 |
enum framehook_detachment_mode |
| 58 |
{ |
| 59 |
|
| 60 |
FRAMEHOOK_DETACH_DESTROY = 0, |
| 61 |
|
| 62 |
|
| 63 |
FRAMEHOOK_DETACH_PRESERVE, |
| 64 |
}; |
| 65 |
|
| 66 |
static void framehook_detach(struct ast_framehook *framehook, enum framehook_detachment_mode mode) |
| 67 |
{ |
| 68 |
struct ast_frame *frame; |
| 69 |
frame = framehook->i.event_cb(framehook->chan, NULL, AST_FRAMEHOOK_EVENT_DETACHED, framehook->i.data); |
| 70 |
|
| 71 |
|
| 72 |
if (frame) { |
| 73 |
ast_frfree(frame); |
| 74 |
} |
| 75 |
framehook->chan = NULL; |
| 76 |
|
| 77 |
if (mode == FRAMEHOOK_DETACH_DESTROY && framehook->i.destroy_cb) { |
| 78 |
framehook->i.destroy_cb(framehook->i.data); |
| 79 |
} |
| 80 |
ast_free(framehook); |
| 81 |
} |
| 82 |
|
| 83 |
static struct ast_frame *framehook_list_push_event(struct ast_framehook_list *framehooks, struct ast_frame *frame, enum ast_framehook_event event) |
| 84 |
{ |
| 85 |
struct ast_framehook *framehook; |
| 86 |
struct ast_frame *original_frame; |
| 87 |
int *skip; |
| 88 |
size_t skip_size; |
| 89 |
|
| 90 |
if (!framehooks) { |
| 91 |
return frame; |
| 92 |
} |
| 93 |
|
| 94 |
skip_size = sizeof(int) * framehooks->count; |
| 95 |
skip = ast_alloca(skip_size); |
| 96 |
memset(skip, 0, skip_size); |
| 97 |
|
| 98 |
do { |
| 99 |
unsigned int num = 0; |
| 100 |
original_frame = frame; |
| 101 |
|
| 102 |
AST_LIST_TRAVERSE_SAFE_BEGIN(&framehooks->list, framehook, list) { |
| 103 |
if (framehook->detach_and_destroy_me) { |
| 104 |
|
| 105 |
AST_LIST_REMOVE_CURRENT(list); |
| 106 |
framehook_detach(framehook, FRAMEHOOK_DETACH_DESTROY); |
| 107 |
continue; |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
if (skip[num]) { |
| 112 |
num++; |
| 113 |
continue; |
| 114 |
} |
| 115 |
|
| 116 |
frame = framehook->i.event_cb(framehook->chan, frame, event, framehook->i.data); |
| 117 |
|
| 118 |
if (frame != original_frame) { |
| 119 |
|
| 120 |
skip[num] = 1; |
| 121 |
break; |
| 122 |
} |
| 123 |
|
| 124 |
num++; |
| 125 |
} |
| 126 |
AST_LIST_TRAVERSE_SAFE_END; |
| 127 |
} while (frame != original_frame); |
| 128 |
|
| 129 |
return frame; |
| 130 |
} |
| 131 |
|
| 132 |
int ast_framehook_attach(struct ast_channel *chan, struct ast_framehook_interface *i) |
| 133 |
{ |
| 134 |
struct ast_framehook *framehook; |
| 135 |
struct ast_framehook_list *fh_list; |
| 136 |
struct ast_frame *frame; |
| 137 |
if (i->version != AST_FRAMEHOOK_INTERFACE_VERSION) { |
| 138 |
ast_log(LOG_ERROR, "Version '%hu' of framehook interface not what we compiled against (%i)\n", |
| 139 |
i->version, AST_FRAMEHOOK_INTERFACE_VERSION); |
| 140 |
return -1; |
| 141 |
} |
| 142 |
if (!i->event_cb || !(framehook = ast_calloc(1, sizeof(*framehook)))) { |
| 143 |
return -1; |
| 144 |
} |
| 145 |
framehook->i = *i; |
| 146 |
framehook->chan = chan; |
| 147 |
|
| 148 |
|
| 149 |
if (!ast_channel_framehooks(chan)) { |
| 150 |
if (!(fh_list = ast_calloc(1, sizeof(*ast_channel_framehooks(chan))))) { |
| 151 |
ast_free(framehook); |
| 152 |
return -1; |
| 153 |
} |
| 154 |
ast_channel_framehooks_set(chan, fh_list); |
| 155 |
} |
| 156 |
|
| 157 |
ast_channel_framehooks(chan)->count++; |
| 158 |
framehook->id = ++ast_channel_framehooks(chan)->id_count; |
| 159 |
AST_LIST_INSERT_TAIL(&ast_channel_framehooks(chan)->list, framehook, list); |
| 160 |
|
| 161 |
|
| 162 |
frame = framehook->i.event_cb(framehook->chan, NULL, AST_FRAMEHOOK_EVENT_ATTACHED, framehook->i.data); |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
if (frame) { |
| 167 |
ast_frfree(frame); |
| 168 |
} |
| 169 |
|
| 170 |
if (ast_channel_is_bridged(chan)) { |
| 171 |
ast_channel_set_unbridged_nolock(chan, 1); |
| 172 |
} |
| 173 |
|
| 174 |
return framehook->id; |
| 175 |
} |
| 176 |
|
| 177 |
int ast_framehook_detach(struct ast_channel *chan, int id) |
| 178 |
{ |
| 179 |
struct ast_framehook *framehook; |
| 180 |
int res = -1; |
| 181 |
|
| 182 |
if (!ast_channel_framehooks(chan)) { |
| 183 |
return res; |
| 184 |
} |
| 185 |
|
| 186 |
AST_LIST_TRAVERSE_SAFE_BEGIN(&ast_channel_framehooks(chan)->list, framehook, list) { |
| 187 |
if (framehook->id == id) { |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
framehook->detach_and_destroy_me = 1; |
| 193 |
res = 0; |
| 194 |
break; |
| 195 |
} |
| 196 |
} |
| 197 |
AST_LIST_TRAVERSE_SAFE_END; |
| 198 |
|
| 199 |
if (!res && ast_channel_is_bridged(chan)) { |
| 200 |
ast_channel_set_unbridged_nolock(chan, 1); |
| 201 |
} |
| 202 |
|
| 203 |
return res; |
| 204 |
} |
| 205 |
|
| 206 |
int ast_framehook_list_destroy(struct ast_channel *chan) |
| 207 |
{ |
| 208 |
struct ast_framehook *framehook; |
| 209 |
|
| 210 |
if (!ast_channel_framehooks(chan)) { |
| 211 |
return 0; |
| 212 |
} |
| 213 |
AST_LIST_TRAVERSE_SAFE_BEGIN(&ast_channel_framehooks(chan)->list, framehook, list) { |
| 214 |
AST_LIST_REMOVE_CURRENT(list); |
| 215 |
framehook_detach(framehook, FRAMEHOOK_DETACH_DESTROY); |
| 216 |
} |
| 217 |
AST_LIST_TRAVERSE_SAFE_END; |
| 218 |
ast_free(ast_channel_framehooks(chan)); |
| 219 |
ast_channel_framehooks_set(chan, NULL); |
| 220 |
return 0; |
| 221 |
} |
| 222 |
|
| 223 |
void ast_framehook_list_fixup(struct ast_channel *old_chan, struct ast_channel *new_chan) |
| 224 |
{ |
| 225 |
struct ast_framehook *framehook; |
| 226 |
int moved_framehook_id; |
| 227 |
|
| 228 |
if (ast_channel_framehooks(new_chan)) { |
| 229 |
AST_LIST_TRAVERSE_SAFE_BEGIN(&ast_channel_framehooks(new_chan)->list, framehook, list) { |
| 230 |
if (framehook->i.disable_inheritance) { |
| 231 |
ast_framehook_detach(new_chan, framehook->id); |
| 232 |
continue; |
| 233 |
} |
| 234 |
|
| 235 |
if (framehook->i.chan_breakdown_cb) { |
| 236 |
framehook->i.chan_breakdown_cb(framehook->i.data, framehook->id, |
| 237 |
old_chan, new_chan); |
| 238 |
} |
| 239 |
} |
| 240 |
AST_LIST_TRAVERSE_SAFE_END; |
| 241 |
} |
| 242 |
|
| 243 |
if (!ast_channel_framehooks(old_chan)) { |
| 244 |
return; |
| 245 |
} |
| 246 |
|
| 247 |
if (!AST_LIST_EMPTY(&ast_channel_framehooks(old_chan)->list) |
| 248 |
&& ast_channel_is_bridged(old_chan)) { |
| 249 |
ast_channel_set_unbridged_nolock(old_chan, 1); |
| 250 |
} |
| 251 |
while ((framehook = AST_LIST_REMOVE_HEAD(&ast_channel_framehooks(old_chan)->list, list))) { |
| 252 |
|
| 253 |
if (framehook->i.disable_inheritance) { |
| 254 |
framehook_detach(framehook, FRAMEHOOK_DETACH_DESTROY); |
| 255 |
continue; |
| 256 |
} |
| 257 |
|
| 258 |
|
| 259 |
moved_framehook_id = ast_framehook_attach(new_chan, &framehook->i); |
| 260 |
if (moved_framehook_id < 0) { |
| 261 |
ast_log(LOG_WARNING, "Failed framehook copy during masquerade. Expect loss of features.\n"); |
| 262 |
framehook_detach(framehook, FRAMEHOOK_DETACH_DESTROY); |
| 263 |
} else { |
| 264 |
if (framehook->i.chan_fixup_cb) { |
| 265 |
framehook->i.chan_fixup_cb(framehook->i.data, moved_framehook_id, |
| 266 |
old_chan, new_chan); |
| 267 |
} |
| 268 |
|
| 269 |
framehook_detach(framehook, FRAMEHOOK_DETACH_PRESERVE); |
| 270 |
} |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
int ast_framehook_list_is_empty(struct ast_framehook_list *framehooks) |
| 275 |
{ |
| 276 |
if (!framehooks) { |
| 277 |
return 1; |
| 278 |
} |
| 279 |
return AST_LIST_EMPTY(&framehooks->list) ? 1 : 0; |
| 280 |
} |
| 281 |
|
| 282 |
int ast_framehook_list_contains_no_active(struct ast_framehook_list *framehooks) |
| 283 |
{ |
| 284 |
return ast_framehook_list_contains_no_active_of_type(framehooks, 0); |
| 285 |
} |
| 286 |
|
| 287 |
int ast_framehook_list_contains_no_active_of_type(struct ast_framehook_list *framehooks, |
| 288 |
enum ast_frame_type type) |
| 289 |
{ |
| 290 |
struct ast_framehook *cur; |
| 291 |
|
| 292 |
if (!framehooks) { |
| 293 |
return 1; |
| 294 |
} |
| 295 |
|
| 296 |
if (AST_LIST_EMPTY(&framehooks->list)) { |
| 297 |
return 1; |
| 298 |
} |
| 299 |
|
| 300 |
AST_LIST_TRAVERSE(&framehooks->list, cur, list) { |
| 301 |
if (cur->detach_and_destroy_me) { |
| 302 |
continue; |
| 303 |
} |
| 304 |
if (type && cur->i.consume_cb && !cur->i.consume_cb(cur->i.data, type)) { |
| 305 |
continue; |
| 306 |
} |
| 307 |
return 0; |
| 308 |
} |
| 309 |
|
| 310 |
return 1; |
| 311 |
} |
| 312 |
|
| 313 |
struct ast_frame *ast_framehook_list_write_event(struct ast_framehook_list *framehooks, struct ast_frame *frame) |
| 314 |
{ |
| 315 |
return framehook_list_push_event(framehooks, frame, AST_FRAMEHOOK_EVENT_WRITE); |
| 316 |
} |
| 317 |
|
| 318 |
struct ast_frame *ast_framehook_list_read_event(struct ast_framehook_list *framehooks, struct ast_frame *frame) |
| 319 |
{ |
| 320 |
return framehook_list_push_event(framehooks, frame, AST_FRAMEHOOK_EVENT_READ); |
| 321 |
} |