2 * Asterisk -- A telephony toolkit for Linux.
4 * ChanSpy Listen in on any channel.
6 * Copyright (C) 2005 Anthony Minessale II (anthmct@yahoo.com)
10 * This program is free software, distributed under the terms of
11 * the GNU General Public License
14 #include <asterisk/file.h>
15 #include <asterisk/logger.h>
16 #include <asterisk/channel.h>
17 #include <asterisk/features.h>
18 #include <asterisk/options.h>
19 #include <asterisk/app.h>
20 #include <asterisk/utils.h>
21 #include <asterisk/say.h>
22 #include <asterisk/channel_pvt.h>
23 #include <asterisk/pbx.h>
24 #include <asterisk/translate.h>
25 #include <asterisk/module.h>
26 #include <asterisk/lock.h>
32 AST_MUTEX_DEFINE_STATIC(modlock);
34 #define ast_fit_in_short(in) (in < -32768 ? -32768 : in > 32767 ? 32767 : in)
35 #define find_smallest_of_three(a, b, c) ((a < b && a < c) ? a : (b < a && b < c) ? b : c)
36 #define AST_NAME_STRLEN 256
37 #define ALL_DONE(u, ret) LOCAL_USER_REMOVE(u); return ret;
38 #define get_volfactor(x) x ? ((x > 0) ? (1 << x) : ((1 << abs(x)) * -1)) : 0
39 #define minmax(x,y) x ? (x > y) ? y : ((x < (y * -1)) ? (y * -1) : x) : 0
43 static char *synopsis = "Tap into any type of asterisk channel and listen to audio";
44 static char *app = "ChanSpy";
45 static char *desc = " Chanspy([<scanspec>][|<options>])\n\n"
47 " - q: quiet, don't announce channels beep, etc.\n"
48 " - b: bridged, only spy on channels involved in a bridged call.\n"
49 " - v([-4..4]): adjust the initial volume. (negative is quieter)\n"
50 " - g(grp): enforce group. Match only calls where their ${SPYGROUP} is 'grp'.\n\n"
51 "If <scanspec> is specified, only channel names *beginning* with that string will be scanned.\n"
52 "('all' or an empty string are also both valid <scanspec>)\n\n"
54 "Dialing # cycles the volume level.\n"
55 "Dialing * will stop spying and look for another channel to spy on.\n"
56 "Dialing a series of digits followed by # builds a channel name to append to <scanspec>\n"
57 "(e.g. run Chanspy(Agent) and dial 1234# while spying to jump to channel Agent/1234)\n\n"
60 #define OPTION_QUIET (1 << 0) /* Quiet, no announcement */
61 #define OPTION_BRIDGED (1 << 1) /* Only look at bridged calls */
62 #define OPTION_VOLUME (1 << 2) /* Specify initial volume */
63 #define OPTION_GROUP (1 << 3) /* Only look at channels in group */
65 AST_DECLARE_OPTIONS(chanspy_opts,{
66 ['q'] = { OPTION_QUIET },
67 ['b'] = { OPTION_BRIDGED },
68 ['v'] = { OPTION_VOLUME, 1 },
69 ['g'] = { OPTION_GROUP, 2 },
76 struct chanspy_translation_helper {
77 struct ast_trans_pvt *trans0;
78 struct ast_trans_pvt *trans1;
80 struct ast_channel_spy *spy;
84 static struct ast_channel *local_get_channel_by_name(char *name);
85 static struct ast_channel *local_channel_walk(struct ast_channel *chan);
86 static void spy_release(struct ast_channel *chan, void *data);
87 static void *spy_alloc(struct ast_channel *chan, void *params);
88 static struct ast_frame *spy_queue_shift(struct ast_channel_spy *spy, int qnum);
89 static void ast_flush_spy_queue(struct ast_channel_spy *spy);
90 static int spy_generate(struct ast_channel *chan, void *data, int len, int samples);
91 static void start_spying(struct ast_channel *chan, struct ast_channel *spychan, struct ast_channel_spy *spy);
92 static void stop_spying(struct ast_channel *chan, struct ast_channel_spy *spy);
93 static int channel_spy(struct ast_channel *chan, struct ast_channel *spyee, int *volfactor);
94 static int chanspy_exec(struct ast_channel *chan, void *data);
97 static struct ast_channel *local_get_channel_by_name(char *name)
99 struct ast_channel *ret;
100 ast_mutex_lock(&modlock);
101 if ((ret = ast_get_channel_by_name_locked(name))) {
102 ast_mutex_unlock(&ret->lock);
104 ast_mutex_unlock(&modlock);
109 static struct ast_channel *local_channel_walk(struct ast_channel *chan)
111 struct ast_channel *ret;
112 ast_mutex_lock(&modlock);
113 if ((ret = ast_channel_walk_locked(chan))) {
114 ast_mutex_unlock(&ret->lock);
116 ast_mutex_unlock(&modlock);
120 static void spy_release(struct ast_channel *chan, void *data)
122 struct chanspy_translation_helper *csth = data;
125 same = (csth->trans0 == csth->trans1) ? 1 : 0;
128 ast_translator_free_path(csth->trans0);
134 ast_translator_free_path(csth->trans1);
140 static void *spy_alloc(struct ast_channel *chan, void *params)
145 static struct ast_frame *spy_queue_shift(struct ast_channel_spy *spy, int qnum)
149 if (qnum < 0 || qnum > 1)
152 f = spy->queue[qnum];
154 spy->queue[qnum] = f->next;
161 static void ast_flush_spy_queue(struct ast_channel_spy *spy)
163 struct ast_frame *f=NULL;
165 ast_mutex_lock(&spy->lock);
168 while((f = spy_queue_shift(spy, x)))
171 ast_mutex_unlock(&spy->lock);
175 static int spy_generate(struct ast_channel *chan, void *data, int len, int samples)
177 struct ast_frame *f0, *f1, write_frame, *f;
178 int x=0, framelen_a = 0, framelen_b = 0, size = 0;
179 short buf[320], buf0[320], buf1[320];
180 struct chanspy_translation_helper *csth = data;
183 ast_mutex_lock(&csth->spy->lock);
184 f0 = spy_queue_shift(csth->spy, 0);
185 f1 = spy_queue_shift(csth->spy, 1);
186 ast_mutex_unlock(&csth->spy->lock);
190 if (f0->subclass != AST_FORMAT_SLINEAR && (csth->trans0 = ast_translator_build_path(AST_FORMAT_SLINEAR, f0->subclass)) == NULL) {
191 ast_log(LOG_WARNING, "Cannot build a path from %s to slin\n", ast_getformatname(f0->subclass));
195 if (f1->subclass == f0->subclass) {
196 csth->trans1 = csth->trans0;
197 } else if (f1->subclass != AST_FORMAT_SLINEAR && (csth->trans1 = ast_translator_build_path(AST_FORMAT_SLINEAR, f1->subclass)) == NULL) {
198 ast_log(LOG_WARNING, "Cannot build a path from %s to slin\n", ast_getformatname(f1->subclass));
205 memset(buf, 0, sizeof(buf));
206 memset(buf0, 0, sizeof(buf0));
207 memset(buf1, 0, sizeof(buf1));
209 if ((f = ast_translate(csth->trans0, f0, 0))) {
210 framelen_a = f->datalen * sizeof(short);
211 memcpy(buf0, f->data, framelen_a);
216 framelen_a = f0->datalen * sizeof(short);
217 memcpy(buf0, f0->data, framelen_a);
220 if ((f = ast_translate(csth->trans1, f1, 0))) {
221 framelen_b = f->datalen * sizeof(short);
222 memcpy(buf1, f->data, framelen_b);
227 framelen_b = f1->datalen * sizeof(short);
228 memcpy(buf1, f1->data, framelen_b);
230 size = find_smallest_of_three(len, framelen_a, framelen_b);
232 vf = get_volfactor(csth->volfactor);
234 for(x=0; x < size; x++) {
242 buf[x] = ast_fit_in_short(buf0[x] + buf1[x]);
244 memset(&write_frame, 0, sizeof(write_frame));
245 write_frame.frametype = AST_FRAME_VOICE;
246 write_frame.subclass = AST_FORMAT_SLINEAR;
247 write_frame.datalen = size;
248 write_frame.samples = size;
249 write_frame.data = buf;
250 write_frame.offset = 0;
251 ast_write(chan, &write_frame);
268 static struct ast_generator spygen = {
270 release: spy_release,
271 generate: spy_generate,
274 static void start_spying(struct ast_channel *chan, struct ast_channel *spychan, struct ast_channel_spy *spy)
277 struct ast_channel_spy *cptr=NULL;
278 struct ast_channel *peer;
281 ast_log(LOG_WARNING, "Attaching %s to %s\n", spychan->name, chan->name);
284 ast_mutex_lock(&chan->lock);
286 for(cptr=chan->spiers;cptr && cptr->next;cptr=cptr->next);
291 ast_mutex_unlock(&chan->lock);
292 if ( ast_test_flag(chan, AST_FLAG_NBRIDGE) && (peer = ast_bridged_channel(chan))) {
293 ast_softhangup(peer, AST_SOFTHANGUP_UNBRIDGE);
298 static void stop_spying(struct ast_channel *chan, struct ast_channel_spy *spy)
300 struct ast_channel_spy *cptr=NULL, *prev=NULL;
303 while(ast_mutex_trylock(&chan->lock)) {
304 /* if its locked already it's almost surely hanging up and we are too late
305 we can safely remove the head pointer if it points at us without needing a lock.
306 since everybody spying will be in the same boat whomever is pointing at the head
307 will surely erase it which is all we really need since it's a linked list of
308 staticly declared structs that belong to each spy.
310 if (chan->spiers == spy) {
321 for(cptr=chan->spiers; cptr; cptr=cptr->next) {
324 prev->next = cptr->next;
331 ast_mutex_unlock(&chan->lock);
335 static int channel_spy(struct ast_channel *chan, struct ast_channel *spyee, int *volfactor)
337 struct chanspy_translation_helper csth;
338 int running = 1, res = 0, x = 0;
341 struct ast_channel_spy spy;
343 if (chan && !ast_check_hangup(chan) && spyee && !ast_check_hangup(spyee)) {
344 memset(inp, 0, sizeof(inp));
345 name = ast_strdupa(spyee->name);
346 if (option_verbose >= 2)
347 ast_verbose(VERBOSE_PREFIX_2 "Spying on channel %s\n", name);
349 memset(&spy, 0, sizeof(struct ast_channel_spy));
350 spy.status = CHANSPY_RUNNING;
351 ast_mutex_init(&spy.lock);
352 start_spying(spyee, chan, &spy);
354 memset(&csth, 0, sizeof(csth));
355 csth.volfactor = *volfactor;
357 ast_activate_generator(chan, &spygen, &csth);
359 while(spy.status == CHANSPY_RUNNING && chan && !ast_check_hangup(chan) && spyee && !ast_check_hangup(spyee) && running == 1) {
360 res = ast_waitfordigit(chan, 100);
362 if (x == sizeof(inp)) {
370 } else if (res == '*') {
372 } else if (res == '#') {
373 if (!ast_strlen_zero(inp)) {
374 running = x ? atoi(inp) : -1;
378 if (csth.volfactor > 4) {
381 if (option_verbose > 2) {
382 ast_verbose(VERBOSE_PREFIX_3"Setting spy volume on %s to %d\n", chan->name, csth.volfactor);
384 *volfactor = csth.volfactor;
386 } else if (res >= 48 && res <= 57) {
390 ast_deactivate_generator(chan);
391 stop_spying(spyee, &spy);
393 if (option_verbose >= 2)
394 ast_verbose(VERBOSE_PREFIX_2 "Done Spying on channel %s\n", name);
396 ast_flush_spy_queue(&spy);
400 ast_mutex_destroy(&spy.lock);
406 static int chanspy_exec(struct ast_channel *chan, void *data)
410 struct ast_channel *peer=NULL, *prev=NULL;
412 char name[AST_NAME_STRLEN], peer_name[AST_NAME_STRLEN];
413 int count=0, waitms=100, num=0;
415 struct ast_flags flags;
422 char *mygroup = NULL;
426 if (!(args = ast_strdupa((char *)data))) {
427 ast_log(LOG_ERROR, "Out of memory!\n");
431 if (ast_set_read_format(chan, AST_FORMAT_SLINEAR) < 0) {
432 ast_log(LOG_ERROR, "Could Not Set Read Format.\n");
435 if (ast_set_write_format(chan, AST_FORMAT_SLINEAR) < 0) {
436 ast_log(LOG_ERROR, "Could Not Set Write Format.\n");
443 ast_set_flag(chan, AST_FLAG_SPYING); /* so nobody can spy on us while we are spying */
446 if ((argc = ast_separate_app_args(args, '|', argv, sizeof(argv) / sizeof(argv[0])))) {
451 if (ast_strlen_zero(spec) || !strcmp(spec, "all")) {
458 ast_parseoptions(chanspy_opts, &flags, opts, options);
459 if (ast_test_flag(&flags, OPTION_GROUP))
461 silent = ast_test_flag(&flags, OPTION_QUIET);
462 bronly = ast_test_flag(&flags, OPTION_BRIDGED);
463 if (ast_test_flag(&flags, OPTION_VOLUME) && opts[1]) {
464 if (sscanf(opts[1], "%d", &volfactor) != 1)
465 ast_log(LOG_NOTICE, "volfactor must be a number between -16 and 16\n");
466 else if (volfactor > 16)
468 else if (volfactor < -16)
475 res = ast_streamfile(chan, "beep", chan->language);
477 res = ast_waitstream(chan, "");
479 ast_clear_flag(chan, AST_FLAG_SPYING);
484 res = ast_waitfordigit(chan, waitms);
486 ast_clear_flag(chan, AST_FLAG_SPYING);
490 peer = local_channel_walk(NULL);
500 group = pbx_builtin_getvar_helper(chan, "SPYGROUP");
502 if (mygroup && group && strcmp(group, mygroup)) {
505 if (!spec || ((strlen(spec) < strlen(peer->name) &&
506 !strncasecmp(peer->name, spec, strlen(spec))))) {
508 if (peer && (!bronly || ast_bridged_channel(peer)) &&
509 !ast_check_hangup(peer) && !ast_test_flag(peer, AST_FLAG_SPYING)) {
512 strncpy(peer_name, peer->name, AST_NAME_STRLEN);
513 ptr = strchr(peer_name, '/');
516 for (x = 0 ; x < strlen(peer_name) ; x++) {
517 if(peer_name[x] == '/') {
520 peer_name[x] = tolower(peer_name[x]);
524 if (ast_fileexists(peer_name, NULL, NULL) != -1) {
525 res = ast_streamfile(chan, peer_name, chan->language);
527 res = ast_waitstream(chan, "");
531 res = ast_say_character_str(chan, peer_name, "", chan->language);
533 ast_say_digits(chan, atoi(ptr), "", chan->language);
537 res = channel_spy(chan, peer, &volfactor);
539 ast_clear_flag(chan, AST_FLAG_SPYING);
541 } else if (res > 1 && spec) {
542 snprintf(name, AST_NAME_STRLEN, "%s/%d", spec, res);
544 ast_say_digits(chan, res, "", chan->language);
545 peer=local_get_channel_by_name(name);
552 if ((peer = local_channel_walk(peer)) == NULL)
555 waitms = count ? 100 : 5000;
559 ast_clear_flag(chan, AST_FLAG_SPYING);
563 int unload_module(void)
565 STANDARD_HANGUP_LOCALUSERS;
566 return ast_unregister_application(app);
569 int load_module(void)
571 return ast_register_application(app, chanspy_exec, synopsis, desc);
574 char *description(void)
582 STANDARD_USECOUNT(res);
588 return ASTERISK_GPL_KEY;