static int old_milliwatt_exec(struct ast_channel *chan) { ast_set_write_format_by_id(chan, AST_FORMAT_ULAW); ast_set_read_format_by_id(chan, AST_FORMAT_ULAW); if (ast_channel_state(chan) != AST_STATE_UP) { ast_answer(chan); } if (ast_activate_generator(chan,&milliwattgen,"milliwatt") < 0) { ast_log(LOG_WARNING,"Failed to activate generator on '%s'\n",ast_channel_name(chan)); return -1; } while (!ast_safe_sleep(chan, 10000)) ; ast_deactivate_generator(chan); return -1; }
static int milliwatt_exec(struct ast_channel *chan, void *data) { struct localuser *u; LOCAL_USER_ADD(u); ast_set_write_format(chan, AST_FORMAT_ULAW); ast_set_read_format(chan, AST_FORMAT_ULAW); if (chan->_state != AST_STATE_UP) { ast_answer(chan); } if (ast_activate_generator(chan,&milliwattgen,"milliwatt") < 0) { ast_log(LOG_WARNING,"Failed to activate generator on '%s'\n",chan->name); return -1; } while(!ast_safe_sleep(chan, 10000)); ast_deactivate_generator(chan); LOCAL_USER_REMOVE(u); return -1; }
static int channel_spy(struct ast_channel *chan, struct ast_autochan *spyee_autochan, int *volfactor, int fd, struct spy_dtmf_options *user_options, struct ast_flags *flags, char *exitcontext) { struct chanspy_translation_helper csth; int running = 0, res, x = 0; char inp[24] = {0}; char *name; struct ast_frame *f; struct ast_silence_generator *silgen = NULL; struct ast_autochan *spyee_bridge_autochan = NULL; const char *spyer_name; struct ast_channel *chans[] = { chan, spyee_autochan->chan }; ast_channel_lock(chan); spyer_name = ast_strdupa(chan->name); ast_channel_unlock(chan); /* We now hold the channel lock on spyee */ if (ast_check_hangup(chan) || ast_check_hangup(spyee_autochan->chan)) { return 0; } ast_channel_lock(spyee_autochan->chan); name = ast_strdupa(spyee_autochan->chan->name); ast_channel_unlock(spyee_autochan->chan); ast_verb(2, "Spying on channel %s\n", name); ast_manager_event_multichan(EVENT_FLAG_CALL, "ChanSpyStart", 2, chans, "SpyerChannel: %s\r\n" "SpyeeChannel: %s\r\n", spyer_name, name); memset(&csth, 0, sizeof(csth)); ast_copy_flags(&csth.flags, flags, AST_FLAGS_ALL); ast_audiohook_init(&csth.spy_audiohook, AST_AUDIOHOOK_TYPE_SPY, "ChanSpy"); if (start_spying(spyee_autochan, spyer_name, &csth.spy_audiohook)) { ast_audiohook_destroy(&csth.spy_audiohook); return 0; } ast_audiohook_init(&csth.whisper_audiohook, AST_AUDIOHOOK_TYPE_WHISPER, "ChanSpy"); ast_audiohook_init(&csth.bridge_whisper_audiohook, AST_AUDIOHOOK_TYPE_WHISPER, "Chanspy"); if (start_spying(spyee_autochan, spyer_name, &csth.whisper_audiohook)) { ast_log(LOG_WARNING, "Unable to attach whisper audiohook to spyee %s. Whisper mode disabled!\n", name); } if ((spyee_bridge_autochan = ast_autochan_setup(ast_bridged_channel(spyee_autochan->chan)))) { ast_channel_lock(spyee_bridge_autochan->chan); if (start_spying(spyee_bridge_autochan, spyer_name, &csth.bridge_whisper_audiohook)) { ast_log(LOG_WARNING, "Unable to attach barge audiohook on spyee %s. Barge mode disabled!\n", name); } ast_channel_unlock(spyee_bridge_autochan->chan); } ast_channel_lock(chan); ast_set_flag(chan, AST_FLAG_END_DTMF_ONLY); ast_channel_unlock(chan); csth.volfactor = *volfactor; if (csth.volfactor) { csth.spy_audiohook.options.read_volume = csth.volfactor; csth.spy_audiohook.options.write_volume = csth.volfactor; } csth.fd = fd; if (ast_test_flag(flags, OPTION_PRIVATE)) silgen = ast_channel_start_silence_generator(chan); else ast_activate_generator(chan, &spygen, &csth); /* We can no longer rely on 'spyee' being an actual channel; it can be hung up and freed out from under us. However, the channel destructor will put NULL into our csth.spy.chan field when that happens, so that is our signal that the spyee channel has gone away. */ /* Note: it is very important that the ast_waitfor() be the first condition in this expression, so that if we wait for some period of time before receiving a frame from our spying channel, we check for hangup on the spied-on channel _after_ knowing that a frame has arrived, since the spied-on channel could have gone away while we were waiting */ while ((res = ast_waitfor(chan, -1) > -1) && csth.spy_audiohook.status == AST_AUDIOHOOK_STATUS_RUNNING) { if (!(f = ast_read(chan)) || ast_check_hangup(chan)) { running = -1; break; } if (ast_test_flag(flags, OPTION_BARGE) && f->frametype == AST_FRAME_VOICE) { ast_audiohook_lock(&csth.whisper_audiohook); ast_audiohook_lock(&csth.bridge_whisper_audiohook); ast_audiohook_write_frame(&csth.whisper_audiohook, AST_AUDIOHOOK_DIRECTION_WRITE, f); ast_audiohook_write_frame(&csth.bridge_whisper_audiohook, AST_AUDIOHOOK_DIRECTION_WRITE, f); ast_audiohook_unlock(&csth.whisper_audiohook); ast_audiohook_unlock(&csth.bridge_whisper_audiohook); ast_frfree(f); continue; } else if (ast_test_flag(flags, OPTION_WHISPER) && f->frametype == AST_FRAME_VOICE) { ast_audiohook_lock(&csth.whisper_audiohook); ast_audiohook_write_frame(&csth.whisper_audiohook, AST_AUDIOHOOK_DIRECTION_WRITE, f); ast_audiohook_unlock(&csth.whisper_audiohook); ast_frfree(f); continue; } res = (f->frametype == AST_FRAME_DTMF) ? f->subclass.integer : 0; ast_frfree(f); if (!res) continue; if (x == sizeof(inp)) x = 0; if (res < 0) { running = -1; break; } if (ast_test_flag(flags, OPTION_EXIT)) { char tmp[2]; tmp[0] = res; tmp[1] = '\0'; if (!ast_goto_if_exists(chan, exitcontext, tmp, 1)) { ast_debug(1, "Got DTMF %c, goto context %s\n", tmp[0], exitcontext); pbx_builtin_setvar_helper(chan, "SPY_CHANNEL", name); running = -2; break; } else { ast_debug(2, "Exit by single digit did not work in chanspy. Extension %s does not exist in context %s\n", tmp, exitcontext); } } else if (res >= '0' && res <= '9') { if (ast_test_flag(flags, OPTION_DTMF_SWITCH_MODES)) { change_spy_mode(res, flags); } else { inp[x++] = res; } } if (res == user_options->cycle) { running = 0; break; } else if (res == user_options->exit) { running = -2; break; } else if (res == user_options->volume) { if (!ast_strlen_zero(inp)) { running = atoi(inp); break; } (*volfactor)++; if (*volfactor > 4) *volfactor = -4; ast_verb(3, "Setting spy volume on %s to %d\n", chan->name, *volfactor); csth.volfactor = *volfactor; csth.spy_audiohook.options.read_volume = csth.volfactor; csth.spy_audiohook.options.write_volume = csth.volfactor; } } if (ast_test_flag(flags, OPTION_PRIVATE)) ast_channel_stop_silence_generator(chan, silgen); else ast_deactivate_generator(chan); ast_channel_lock(chan); ast_clear_flag(chan, AST_FLAG_END_DTMF_ONLY); ast_channel_unlock(chan); ast_audiohook_lock(&csth.whisper_audiohook); ast_audiohook_detach(&csth.whisper_audiohook); ast_audiohook_unlock(&csth.whisper_audiohook); ast_audiohook_destroy(&csth.whisper_audiohook); ast_audiohook_lock(&csth.bridge_whisper_audiohook); ast_audiohook_detach(&csth.bridge_whisper_audiohook); ast_audiohook_unlock(&csth.bridge_whisper_audiohook); ast_audiohook_destroy(&csth.bridge_whisper_audiohook); ast_audiohook_lock(&csth.spy_audiohook); ast_audiohook_detach(&csth.spy_audiohook); ast_audiohook_unlock(&csth.spy_audiohook); ast_audiohook_destroy(&csth.spy_audiohook); if (spyee_bridge_autochan) { ast_autochan_destroy(spyee_bridge_autochan); } ast_verb(2, "Done Spying on channel %s\n", name); ast_manager_event(chan, EVENT_FLAG_CALL, "ChanSpyStop", "SpyeeChannel: %s\r\n", name); return running; }
static int transmit_audio(fax_session *s) { int res = -1; struct ast_format original_read_fmt; struct ast_format original_write_fmt; fax_state_t fax; t30_state_t *t30state; struct ast_frame *inf = NULL; int last_state = 0; struct timeval now, start, state_change; enum ast_t38_state t38_state; struct ast_control_t38_parameters t38_parameters = { .version = 0, .max_ifp = 800, .rate = AST_T38_RATE_14400, .rate_management = AST_T38_RATE_MANAGEMENT_TRANSFERRED_TCF, .fill_bit_removal = 1, /* * spandsp has API calls to support MMR and JBIG transcoding, but they aren't * implemented quite yet... so don't offer them to the remote endpoint * .transcoding_mmr = 1, * .transcoding_jbig = 1, */ }; ast_format_clear(&original_read_fmt); ast_format_clear(&original_write_fmt); /* if in called party mode, try to use T.38 */ if (s->caller_mode == FALSE) { /* check if we are already in T.38 mode (unlikely), or if we can request * a switch... if so, request it now and wait for the result, rather * than starting an audio FAX session that will have to be cancelled */ if ((t38_state = ast_channel_get_t38_state(s->chan)) == T38_STATE_NEGOTIATED) { return 1; } else if ((t38_state != T38_STATE_UNAVAILABLE) && (t38_parameters.request_response = AST_T38_REQUEST_NEGOTIATE, (ast_indicate_data(s->chan, AST_CONTROL_T38_PARAMETERS, &t38_parameters, sizeof(t38_parameters)) == 0))) { /* wait up to five seconds for negotiation to complete */ unsigned int timeout = 5000; int ms; ast_debug(1, "Negotiating T.38 for receive on %s\n", ast_channel_name(s->chan)); while (timeout > 0) { ms = ast_waitfor(s->chan, 1000); if (ms < 0) { ast_log(LOG_WARNING, "something bad happened while channel '%s' was polling.\n", ast_channel_name(s->chan)); return -1; } if (!ms) { /* nothing happened */ if (timeout > 0) { timeout -= 1000; continue; } else { ast_log(LOG_WARNING, "channel '%s' timed-out during the T.38 negotiation.\n", ast_channel_name(s->chan)); break; } } if (!(inf = ast_read(s->chan))) { return -1; } if ((inf->frametype == AST_FRAME_CONTROL) && (inf->subclass.integer == AST_CONTROL_T38_PARAMETERS) && (inf->datalen == sizeof(t38_parameters))) { struct ast_control_t38_parameters *parameters = inf->data.ptr; switch (parameters->request_response) { case AST_T38_NEGOTIATED: ast_debug(1, "Negotiated T.38 for receive on %s\n", ast_channel_name(s->chan)); res = 1; break; case AST_T38_REFUSED: ast_log(LOG_WARNING, "channel '%s' refused to negotiate T.38\n", ast_channel_name(s->chan)); break; default: ast_log(LOG_ERROR, "channel '%s' failed to negotiate T.38\n", ast_channel_name(s->chan)); break; } ast_frfree(inf); if (res == 1) { return 1; } else { break; } } ast_frfree(inf); } } } #if SPANDSP_RELEASE_DATE >= 20080725 /* for spandsp shaphots 0.0.6 and higher */ t30state = &fax.t30; #else /* for spandsp release 0.0.5 */ t30state = &fax.t30_state; #endif ast_format_copy(&original_read_fmt, ast_channel_readformat(s->chan)); if (original_read_fmt.id != AST_FORMAT_SLINEAR) { res = ast_set_read_format_by_id(s->chan, AST_FORMAT_SLINEAR); if (res < 0) { ast_log(LOG_WARNING, "Unable to set to linear read mode, giving up\n"); goto done; } } ast_format_copy(&original_write_fmt, ast_channel_writeformat(s->chan)); if (original_write_fmt.id != AST_FORMAT_SLINEAR) { res = ast_set_write_format_by_id(s->chan, AST_FORMAT_SLINEAR); if (res < 0) { ast_log(LOG_WARNING, "Unable to set to linear write mode, giving up\n"); goto done; } } /* Initialize T30 terminal */ fax_init(&fax, s->caller_mode); /* Setup logging */ set_logging(&fax.logging); set_logging(&t30state->logging); /* Configure terminal */ set_local_info(t30state, s); set_file(t30state, s); set_ecm(t30state, TRUE); fax_set_transmit_on_idle(&fax, TRUE); t30_set_phase_e_handler(t30state, phase_e_handler, s); start = state_change = ast_tvnow(); ast_activate_generator(s->chan, &generator, &fax); while (!s->finished) { inf = NULL; if ((res = ast_waitfor(s->chan, 25)) < 0) { ast_debug(1, "Error waiting for a frame\n"); break; } /* Watchdog */ now = ast_tvnow(); if (ast_tvdiff_sec(now, start) > WATCHDOG_TOTAL_TIMEOUT || ast_tvdiff_sec(now, state_change) > WATCHDOG_STATE_TIMEOUT) { ast_log(LOG_WARNING, "It looks like we hung. Aborting.\n"); res = -1; break; } if (!res) { /* There was timeout waiting for a frame. Loop around and wait again */ continue; } /* There is a frame available. Get it */ res = 0; if (!(inf = ast_read(s->chan))) { ast_debug(1, "Channel hangup\n"); res = -1; break; } ast_debug(10, "frame %d/%u, len=%d\n", inf->frametype, (unsigned int) inf->subclass.format.id, inf->datalen); /* Check the frame type. Format also must be checked because there is a chance that a frame in old format was already queued before we set channel format to slinear so it will still be received by ast_read */ if (inf->frametype == AST_FRAME_VOICE && inf->subclass.format.id == AST_FORMAT_SLINEAR) { if (fax_rx(&fax, inf->data.ptr, inf->samples) < 0) { /* I know fax_rx never returns errors. The check here is for good style only */ ast_log(LOG_WARNING, "fax_rx returned error\n"); res = -1; break; } if (last_state != t30state->state) { state_change = ast_tvnow(); last_state = t30state->state; } } else if ((inf->frametype == AST_FRAME_CONTROL) && (inf->subclass.integer == AST_CONTROL_T38_PARAMETERS)) { struct ast_control_t38_parameters *parameters = inf->data.ptr; if (parameters->request_response == AST_T38_NEGOTIATED) { /* T38 switchover completed */ s->t38parameters = *parameters; ast_debug(1, "T38 negotiated, finishing audio loop\n"); res = 1; break; } else if (parameters->request_response == AST_T38_REQUEST_NEGOTIATE) { t38_parameters.request_response = AST_T38_NEGOTIATED; ast_debug(1, "T38 request received, accepting\n"); /* Complete T38 switchover */ ast_indicate_data(s->chan, AST_CONTROL_T38_PARAMETERS, &t38_parameters, sizeof(t38_parameters)); /* Do not break audio loop, wait until channel driver finally acks switchover * with AST_T38_NEGOTIATED */ } } ast_frfree(inf); inf = NULL; } ast_debug(1, "Loop finished, res=%d\n", res); if (inf) ast_frfree(inf); ast_deactivate_generator(s->chan); /* If we are switching to T38, remove phase E handler. Otherwise it will be executed by t30_terminate, display diagnostics and set status variables although no transmittion has taken place yet. */ if (res > 0) { t30_set_phase_e_handler(t30state, NULL, NULL); } t30_terminate(t30state); fax_release(&fax); done: if (original_write_fmt.id != AST_FORMAT_SLINEAR) { if (ast_set_write_format(s->chan, &original_write_fmt) < 0) ast_log(LOG_WARNING, "Unable to restore write format on '%s'\n", ast_channel_name(s->chan)); } if (original_read_fmt.id != AST_FORMAT_SLINEAR) { if (ast_set_read_format(s->chan, &original_read_fmt) < 0) ast_log(LOG_WARNING, "Unable to restore read format on '%s'\n", ast_channel_name(s->chan)); } return res; } static int transmit_t38(fax_session *s) { int res = 0; t38_terminal_state_t t38; struct ast_frame *inf = NULL; int last_state = 0; struct timeval now, start, state_change, last_frame; t30_state_t *t30state; t38_core_state_t *t38state; #if SPANDSP_RELEASE_DATE >= 20080725 /* for spandsp shaphots 0.0.6 and higher */ t30state = &t38.t30; t38state = &t38.t38_fe.t38; #else /* for spandsp releases 0.0.5 */ t30state = &t38.t30_state; t38state = &t38.t38; #endif /* Initialize terminal */ memset(&t38, 0, sizeof(t38)); if (t38_terminal_init(&t38, s->caller_mode, t38_tx_packet_handler, s->chan) == NULL) { ast_log(LOG_WARNING, "Unable to start T.38 termination.\n"); res = -1; goto disable_t38; } t38_set_max_datagram_size(t38state, s->t38parameters.max_ifp); if (s->t38parameters.fill_bit_removal) { t38_set_fill_bit_removal(t38state, TRUE); } if (s->t38parameters.transcoding_mmr) { t38_set_mmr_transcoding(t38state, TRUE); } if (s->t38parameters.transcoding_jbig) { t38_set_jbig_transcoding(t38state, TRUE); } /* Setup logging */ set_logging(&t38.logging); set_logging(&t30state->logging); set_logging(&t38state->logging); /* Configure terminal */ set_local_info(t30state, s); set_file(t30state, s); set_ecm(t30state, TRUE); t30_set_phase_e_handler(t30state, phase_e_handler, s); now = start = state_change = ast_tvnow(); while (!s->finished) { inf = NULL; if ((res = ast_waitfor(s->chan, 25)) < 0) { ast_debug(1, "Error waiting for a frame\n"); break; } last_frame = now; /* Watchdog */ now = ast_tvnow(); if (ast_tvdiff_sec(now, start) > WATCHDOG_TOTAL_TIMEOUT || ast_tvdiff_sec(now, state_change) > WATCHDOG_STATE_TIMEOUT) { ast_log(LOG_WARNING, "It looks like we hung. Aborting.\n"); res = -1; break; } t38_terminal_send_timeout(&t38, ast_tvdiff_us(now, last_frame) / (1000000 / 8000)); if (!res) { /* There was timeout waiting for a frame. Loop around and wait again */ continue; } /* There is a frame available. Get it */ res = 0; if (!(inf = ast_read(s->chan))) { ast_debug(1, "Channel hangup\n"); res = -1; break; } ast_debug(10, "frame %d/%d, len=%d\n", inf->frametype, inf->subclass.integer, inf->datalen); if (inf->frametype == AST_FRAME_MODEM && inf->subclass.integer == AST_MODEM_T38) { t38_core_rx_ifp_packet(t38state, inf->data.ptr, inf->datalen, inf->seqno); if (last_state != t30state->state) { state_change = ast_tvnow(); last_state = t30state->state; } } else if (inf->frametype == AST_FRAME_CONTROL && inf->subclass.integer == AST_CONTROL_T38_PARAMETERS) { struct ast_control_t38_parameters *parameters = inf->data.ptr; if (parameters->request_response == AST_T38_TERMINATED) { ast_debug(1, "T38 down, finishing\n"); break; } } ast_frfree(inf); inf = NULL; } ast_debug(1, "Loop finished, res=%d\n", res); if (inf) ast_frfree(inf); t30_terminate(t30state); t38_terminal_release(&t38); disable_t38: /* if we are not the caller, it's our job to shut down the T.38 * session when the FAX transmisson is complete. */ if ((s->caller_mode == FALSE) && (ast_channel_get_t38_state(s->chan) == T38_STATE_NEGOTIATED)) { struct ast_control_t38_parameters t38_parameters = { .request_response = AST_T38_REQUEST_TERMINATE, }; if (ast_indicate_data(s->chan, AST_CONTROL_T38_PARAMETERS, &t38_parameters, sizeof(t38_parameters)) == 0) { /* wait up to five seconds for negotiation to complete */ unsigned int timeout = 5000; int ms; ast_debug(1, "Shutting down T.38 on %s\n", ast_channel_name(s->chan)); while (timeout > 0) { ms = ast_waitfor(s->chan, 1000); if (ms < 0) { ast_log(LOG_WARNING, "something bad happened while channel '%s' was polling.\n", ast_channel_name(s->chan)); return -1; } if (!ms) { /* nothing happened */ if (timeout > 0) { timeout -= 1000; continue; } else { ast_log(LOG_WARNING, "channel '%s' timed-out during the T.38 shutdown.\n", ast_channel_name(s->chan)); break; } } if (!(inf = ast_read(s->chan))) { return -1; } if ((inf->frametype == AST_FRAME_CONTROL) && (inf->subclass.integer == AST_CONTROL_T38_PARAMETERS) && (inf->datalen == sizeof(t38_parameters))) { struct ast_control_t38_parameters *parameters = inf->data.ptr; switch (parameters->request_response) { case AST_T38_TERMINATED: ast_debug(1, "Shut down T.38 on %s\n", ast_channel_name(s->chan)); break; case AST_T38_REFUSED: ast_log(LOG_WARNING, "channel '%s' refused to disable T.38\n", ast_channel_name(s->chan)); break; default: ast_log(LOG_ERROR, "channel '%s' failed to disable T.38\n", ast_channel_name(s->chan)); break; } ast_frfree(inf); break; } ast_frfree(inf); } } } return res; }
static int app_exec(struct ast_channel *chan, void *data) { struct ast_module_user *lu; struct playlist_entry *entry; const char *args = data; int child_stdin[2] = { 0,0 }; int child_stdout[2] = { 0,0 }; int child_stderr[2] = { 0,0 }; int res = -1; int test_available_fd = -1; int gen_active = 0; int pid; char *argv[32]; int argc = 1; char *buf, *command; FILE *child_commands = NULL; FILE *child_errors = NULL; FILE *child_events = NULL; struct ivr_localuser foo = { .playlist = AST_LIST_HEAD_INIT_VALUE, .finishlist = AST_LIST_HEAD_INIT_VALUE, }; struct ivr_localuser *u = &foo; sigset_t fullset, oldset; lu = ast_module_user_add(chan); sigfillset(&fullset); pthread_sigmask(SIG_BLOCK, &fullset, &oldset); u->abort_current_sound = 0; u->chan = chan; if (ast_strlen_zero(args)) { ast_log(LOG_WARNING, "ExternalIVR requires a command to execute\n"); ast_module_user_remove(lu); return -1; } buf = ast_strdupa(data); argc = ast_app_separate_args(buf, '|', argv, sizeof(argv) / sizeof(argv[0])); if (pipe(child_stdin)) { ast_chan_log(LOG_WARNING, chan, "Could not create pipe for child input: %s\n", strerror(errno)); goto exit; } if (pipe(child_stdout)) { ast_chan_log(LOG_WARNING, chan, "Could not create pipe for child output: %s\n", strerror(errno)); goto exit; } if (pipe(child_stderr)) { ast_chan_log(LOG_WARNING, chan, "Could not create pipe for child errors: %s\n", strerror(errno)); goto exit; } if (chan->_state != AST_STATE_UP) { ast_answer(chan); } if (ast_activate_generator(chan, &gen, u) < 0) { ast_chan_log(LOG_WARNING, chan, "Failed to activate generator\n"); goto exit; } else gen_active = 1; pid = fork(); if (pid < 0) { ast_log(LOG_WARNING, "Failed to fork(): %s\n", strerror(errno)); goto exit; } if (!pid) { /* child process */ int i; signal(SIGPIPE, SIG_DFL); pthread_sigmask(SIG_UNBLOCK, &fullset, NULL); if (ast_opt_high_priority) ast_set_priority(0); dup2(child_stdin[0], STDIN_FILENO); dup2(child_stdout[1], STDOUT_FILENO); dup2(child_stderr[1], STDERR_FILENO); for (i = STDERR_FILENO + 1; i < 1024; i++) close(i); execv(argv[0], argv); fprintf(stderr, "Failed to execute '%s': %s\n", argv[0], strerror(errno)); _exit(1); } else { /* parent process */ int child_events_fd = child_stdin[1]; int child_commands_fd = child_stdout[0]; int child_errors_fd = child_stderr[0]; struct ast_frame *f; int ms; int exception; int ready_fd; int waitfds[2] = { child_errors_fd, child_commands_fd }; struct ast_channel *rchan; pthread_sigmask(SIG_SETMASK, &oldset, NULL); close(child_stdin[0]); child_stdin[0] = 0; close(child_stdout[1]); child_stdout[1] = 0; close(child_stderr[1]); child_stderr[1] = 0; if (!(child_events = fdopen(child_events_fd, "w"))) { ast_chan_log(LOG_WARNING, chan, "Could not open stream for child events\n"); goto exit; } if (!(child_commands = fdopen(child_commands_fd, "r"))) { ast_chan_log(LOG_WARNING, chan, "Could not open stream for child commands\n"); goto exit; } if (!(child_errors = fdopen(child_errors_fd, "r"))) { ast_chan_log(LOG_WARNING, chan, "Could not open stream for child errors\n"); goto exit; } test_available_fd = open("/dev/null", O_RDONLY); setvbuf(child_events, NULL, _IONBF, 0); setvbuf(child_commands, NULL, _IONBF, 0); setvbuf(child_errors, NULL, _IONBF, 0); res = 0; while (1) { if (ast_test_flag(chan, AST_FLAG_ZOMBIE)) { ast_chan_log(LOG_NOTICE, chan, "Is a zombie\n"); res = -1; break; } if (ast_check_hangup(chan)) { ast_chan_log(LOG_NOTICE, chan, "Got check_hangup\n"); send_child_event(child_events, 'H', NULL, chan); res = -1; break; } ready_fd = 0; ms = 100; errno = 0; exception = 0; rchan = ast_waitfor_nandfds(&chan, 1, waitfds, 2, &exception, &ready_fd, &ms); if (!AST_LIST_EMPTY(&u->finishlist)) { AST_LIST_LOCK(&u->finishlist); while ((entry = AST_LIST_REMOVE_HEAD(&u->finishlist, list))) { send_child_event(child_events, 'F', entry->filename, chan); free(entry); } AST_LIST_UNLOCK(&u->finishlist); } if (rchan) { /* the channel has something */ f = ast_read(chan); if (!f) { ast_chan_log(LOG_NOTICE, chan, "Returned no frame\n"); send_child_event(child_events, 'H', NULL, chan); res = -1; break; } if (f->frametype == AST_FRAME_DTMF) { send_child_event(child_events, f->subclass, NULL, chan); if (u->option_autoclear) { if (!u->abort_current_sound && !u->playing_silence) send_child_event(child_events, 'T', NULL, chan); AST_LIST_LOCK(&u->playlist); while ((entry = AST_LIST_REMOVE_HEAD(&u->playlist, list))) { send_child_event(child_events, 'D', entry->filename, chan); free(entry); } if (!u->playing_silence) u->abort_current_sound = 1; AST_LIST_UNLOCK(&u->playlist); } } else if ((f->frametype == AST_FRAME_CONTROL) && (f->subclass == AST_CONTROL_HANGUP)) { ast_chan_log(LOG_NOTICE, chan, "Got AST_CONTROL_HANGUP\n"); send_child_event(child_events, 'H', NULL, chan); ast_frfree(f); res = -1; break; } ast_frfree(f); } else if (ready_fd == child_commands_fd) { char input[1024]; if (exception || feof(child_commands)) { ast_chan_log(LOG_WARNING, chan, "Child process went away\n"); res = -1; break; } if (!fgets(input, sizeof(input), child_commands)) continue; command = ast_strip(input); ast_chan_log(LOG_DEBUG, chan, "got command '%s'\n", input); if (strlen(input) < 4) continue; if (input[0] == 'S') { if (ast_fileexists(&input[2], NULL, u->chan->language) == -1) { ast_chan_log(LOG_WARNING, chan, "Unknown file requested '%s'\n", &input[2]); send_child_event(child_events, 'Z', NULL, chan); strcpy(&input[2], "exception"); } if (!u->abort_current_sound && !u->playing_silence) send_child_event(child_events, 'T', NULL, chan); AST_LIST_LOCK(&u->playlist); while ((entry = AST_LIST_REMOVE_HEAD(&u->playlist, list))) { send_child_event(child_events, 'D', entry->filename, chan); free(entry); } if (!u->playing_silence) u->abort_current_sound = 1; entry = make_entry(&input[2]); if (entry) AST_LIST_INSERT_TAIL(&u->playlist, entry, list); AST_LIST_UNLOCK(&u->playlist); } else if (input[0] == 'A') { if (ast_fileexists(&input[2], NULL, u->chan->language) == -1) { ast_chan_log(LOG_WARNING, chan, "Unknown file requested '%s'\n", &input[2]); send_child_event(child_events, 'Z', NULL, chan); strcpy(&input[2], "exception"); } entry = make_entry(&input[2]); if (entry) { AST_LIST_LOCK(&u->playlist); AST_LIST_INSERT_TAIL(&u->playlist, entry, list); AST_LIST_UNLOCK(&u->playlist); } } else if (input[0] == 'H') { ast_chan_log(LOG_NOTICE, chan, "Hanging up: %s\n", &input[2]); send_child_event(child_events, 'H', NULL, chan); break; } else if (input[0] == 'O') { if (!strcasecmp(&input[2], "autoclear")) u->option_autoclear = 1; else if (!strcasecmp(&input[2], "noautoclear")) u->option_autoclear = 0; else ast_chan_log(LOG_WARNING, chan, "Unknown option requested '%s'\n", &input[2]); } } else if (ready_fd == child_errors_fd) { char input[1024]; if (exception || (dup2(child_commands_fd, test_available_fd) == -1) || feof(child_errors)) { ast_chan_log(LOG_WARNING, chan, "Child process went away\n"); res = -1; break; } if (fgets(input, sizeof(input), child_errors)) { command = ast_strip(input); ast_chan_log(LOG_NOTICE, chan, "stderr: %s\n", command); } } else if ((ready_fd < 0) && ms) { if (errno == 0 || errno == EINTR) continue; ast_chan_log(LOG_WARNING, chan, "Wait failed (%s)\n", strerror(errno)); break; } } } exit: if (gen_active) ast_deactivate_generator(chan); if (child_events) fclose(child_events); if (child_commands) fclose(child_commands); if (child_errors) fclose(child_errors); if (test_available_fd > -1) { close(test_available_fd); } if (child_stdin[0]) close(child_stdin[0]); if (child_stdin[1]) close(child_stdin[1]); if (child_stdout[0]) close(child_stdout[0]); if (child_stdout[1]) close(child_stdout[1]); if (child_stderr[0]) close(child_stderr[0]); if (child_stderr[1]) close(child_stderr[1]); while ((entry = AST_LIST_REMOVE_HEAD(&u->playlist, list))) free(entry); ast_module_user_remove(lu); return res; }
static int transmit_audio(fax_session *s) { int res = -1; int original_read_fmt = AST_FORMAT_SLINEAR; int original_write_fmt = AST_FORMAT_SLINEAR; fax_state_t fax; struct ast_dsp *dsp = NULL; int detect_tone = 0; struct ast_frame *inf = NULL; struct ast_frame *fr; int last_state = 0; struct timeval now, start, state_change; enum ast_control_t38 t38control; original_read_fmt = s->chan->readformat; if (original_read_fmt != AST_FORMAT_SLINEAR) { res = ast_set_read_format(s->chan, AST_FORMAT_SLINEAR); if (res < 0) { ast_log(LOG_WARNING, "Unable to set to linear read mode, giving up\n"); goto done; } } original_write_fmt = s->chan->writeformat; if (original_write_fmt != AST_FORMAT_SLINEAR) { res = ast_set_write_format(s->chan, AST_FORMAT_SLINEAR); if (res < 0) { ast_log(LOG_WARNING, "Unable to set to linear write mode, giving up\n"); goto done; } } /* Initialize T30 terminal */ fax_init(&fax, s->caller_mode); /* Setup logging */ set_logging(&fax.logging); set_logging(&fax.t30_state.logging); /* Configure terminal */ set_local_info(&fax.t30_state, s); set_file(&fax.t30_state, s); set_ecm(&fax.t30_state, TRUE); fax_set_transmit_on_idle(&fax, TRUE); t30_set_phase_e_handler(&fax.t30_state, phase_e_handler, s); if (s->t38state == T38_STATE_UNAVAILABLE) { ast_debug(1, "T38 is unavailable on %s\n", s->chan->name); } else if (!s->direction) { /* We are receiving side and this means we are the side which should request T38 when the fax is detected. Use DSP to detect fax tone */ ast_debug(1, "Setting up CNG detection on %s\n", s->chan->name); dsp = ast_dsp_new(); ast_dsp_set_features(dsp, DSP_FEATURE_FAX_DETECT); ast_dsp_set_faxmode(dsp, DSP_FAXMODE_DETECT_CNG); detect_tone = 1; } start = state_change = ast_tvnow(); ast_activate_generator(s->chan, &generator, &fax); while (!s->finished) { res = ast_waitfor(s->chan, 20); if (res < 0) break; else if (res > 0) res = 0; inf = ast_read(s->chan); if (inf == NULL) { ast_debug(1, "Channel hangup\n"); res = -1; break; } ast_debug(10, "frame %d/%d, len=%d\n", inf->frametype, inf->subclass, inf->datalen); /* Detect fax tone */ if (detect_tone && inf->frametype == AST_FRAME_VOICE) { /* Duplicate frame because ast_dsp_process may free the frame passed */ fr = ast_frdup(inf); /* Do not pass channel to ast_dsp_process otherwise it may queue modified audio frame back */ fr = ast_dsp_process(NULL, dsp, fr); if (fr && fr->frametype == AST_FRAME_DTMF && fr->subclass == 'f') { ast_debug(1, "Fax tone detected. Requesting T38\n"); t38control = AST_T38_REQUEST_NEGOTIATE; ast_indicate_data(s->chan, AST_CONTROL_T38, &t38control, sizeof(t38control)); detect_tone = 0; } ast_frfree(fr); } /* Check the frame type. Format also must be checked because there is a chance that a frame in old format was already queued before we set chanel format to slinear so it will still be received by ast_read */ if (inf->frametype == AST_FRAME_VOICE && inf->subclass == AST_FORMAT_SLINEAR) { if (fax_rx(&fax, inf->data, inf->samples) < 0) { /* I know fax_rx never returns errors. The check here is for good style only */ ast_log(LOG_WARNING, "fax_rx returned error\n"); res = -1; break; } /* Watchdog */ if (last_state != fax.t30_state.state) { state_change = ast_tvnow(); last_state = fax.t30_state.state; } } else if (inf->frametype == AST_FRAME_CONTROL && inf->subclass == AST_CONTROL_T38 && inf->datalen == sizeof(enum ast_control_t38)) { t38control =*((enum ast_control_t38 *) inf->data); if (t38control == AST_T38_NEGOTIATED) { /* T38 switchover completed */ ast_debug(1, "T38 negotiated, finishing audio loop\n"); res = 1; break; } } ast_frfree(inf); inf = NULL; /* Watchdog */ now = ast_tvnow(); if (ast_tvdiff_sec(now, start) > WATCHDOG_TOTAL_TIMEOUT || ast_tvdiff_sec(now, state_change) > WATCHDOG_STATE_TIMEOUT) { ast_log(LOG_WARNING, "It looks like we hung. Aborting.\n"); res = -1; break; } } ast_debug(1, "Loop finished, res=%d\n", res); if (inf) ast_frfree(inf); if (dsp) ast_dsp_free(dsp); ast_deactivate_generator(s->chan); /* If we are switching to T38, remove phase E handler. Otherwise it will be executed by t30_terminate, display diagnostics and set status variables although no transmittion has taken place yet. */ if (res > 0) { t30_set_phase_e_handler(&fax.t30_state, NULL, NULL); } t30_terminate(&fax.t30_state); fax_release(&fax); done: if (original_write_fmt != AST_FORMAT_SLINEAR) { if (ast_set_write_format(s->chan, original_write_fmt) < 0) ast_log(LOG_WARNING, "Unable to restore write format on '%s'\n", s->chan->name); } if (original_read_fmt != AST_FORMAT_SLINEAR) { if (ast_set_read_format(s->chan, original_read_fmt) < 0) ast_log(LOG_WARNING, "Unable to restore read format on '%s'\n", s->chan->name); } return res; }
static int channel_spy(struct ast_channel *chan, struct ast_channel *spyee, int *volfactor, int fd) { struct chanspy_translation_helper csth; int running = 0, res = 0, x = 0; char inp[24] = "", *name = NULL; struct ast_frame *f = NULL; if ((chan && ast_check_hangup(chan)) || (spyee && ast_check_hangup(spyee))) return 0; name = ast_strdupa(spyee->name); if (option_verbose > 1) ast_verbose(VERBOSE_PREFIX_2 "Spying on channel %s\n", name); memset(&csth, 0, sizeof(csth)); ast_set_flag(&csth.spy, CHANSPY_FORMAT_AUDIO); ast_set_flag(&csth.spy, CHANSPY_TRIGGER_NONE); ast_set_flag(&csth.spy, CHANSPY_MIXAUDIO); csth.spy.type = chanspy_spy_type; csth.spy.status = CHANSPY_RUNNING; csth.spy.read_queue.format = AST_FORMAT_SLINEAR; csth.spy.write_queue.format = AST_FORMAT_SLINEAR; ast_mutex_init(&csth.spy.lock); csth.volfactor = *volfactor; set_volume(chan, &csth); if (csth.volfactor) { ast_set_flag(&csth.spy, CHANSPY_READ_VOLADJUST); csth.spy.read_vol_adjustment = csth.volfactor; ast_set_flag(&csth.spy, CHANSPY_WRITE_VOLADJUST); csth.spy.write_vol_adjustment = csth.volfactor; } csth.fd = fd; if (start_spying(spyee, chan, &csth.spy)) { ast_channel_spy_free(&csth.spy); return 0; } ast_activate_generator(chan, &spygen, &csth); while (csth.spy.status == CHANSPY_RUNNING && (res = ast_waitfor(chan, -1) > -1)) { /* Read in frame from channel, break out if no frame */ if (!(f = ast_read(chan))) break; /* Now if this is DTMF then we have to handle it as such, otherwise just skip it */ res = 0; if (f->frametype == AST_FRAME_DTMF) res = f->subclass; ast_frfree(f); if (!res) continue; if (x == sizeof(inp)) x = 0; if (res < 0) { running = -1; break; } /* Process DTMF digits */ if (res == '#') { if (!ast_strlen_zero(inp)) { running = x ? atoi(inp) : -1; break; } else { (*volfactor)++; if (*volfactor > 4) *volfactor = -1; if (option_verbose > 2) ast_verbose(VERBOSE_PREFIX_3 "Setting spy volume on %s to %d\n", chan->name, *volfactor); csth.volfactor = *volfactor; set_volume(chan, &csth); if (csth.volfactor) { ast_set_flag(&csth.spy, CHANSPY_READ_VOLADJUST); csth.spy.read_vol_adjustment = csth.volfactor; ast_set_flag(&csth.spy, CHANSPY_WRITE_VOLADJUST); csth.spy.write_vol_adjustment = csth.volfactor; } else { ast_clear_flag(&csth.spy, CHANSPY_READ_VOLADJUST); ast_clear_flag(&csth.spy, CHANSPY_WRITE_VOLADJUST); } } } else if (res == '*') { break; } else if (res >= 48 && res <= 57) { inp[x++] = res; } } ast_deactivate_generator(chan); csth.spy.status = CHANSPY_DONE; ast_mutex_lock(&csth.spy.lock); if (csth.spy.chan) { ast_mutex_lock(&csth.spy.chan->lock); ast_channel_spy_remove(csth.spy.chan, &csth.spy); ast_mutex_unlock(&csth.spy.chan->lock); } ast_mutex_unlock(&csth.spy.lock); if (option_verbose > 1) ast_verbose(VERBOSE_PREFIX_2 "Done Spying on channel %s\n", name); ast_channel_spy_free(&csth.spy); return running; }
static int channel_spy(struct ast_channel *chan, struct chanspy_ds *spyee_chanspy_ds, int *volfactor, int fd, const struct ast_flags *flags) { struct chanspy_translation_helper csth; int running = 0, res, x = 0; char inp[24] = {0}; char *name; struct ast_frame *f; struct ast_silence_generator *silgen = NULL; struct ast_channel *spyee = NULL; const char *spyer_name; ast_channel_lock(chan); spyer_name = ast_strdupa(chan->name); ast_channel_unlock(chan); ast_mutex_lock(&spyee_chanspy_ds->lock); while ((spyee = spyee_chanspy_ds->chan) && ast_channel_trylock(spyee)) { /* avoid a deadlock here, just in case spyee is masqueraded and * chanspy_ds_chan_fixup() is called with the channel locked */ DEADLOCK_AVOIDANCE(&spyee_chanspy_ds->lock); } ast_mutex_unlock(&spyee_chanspy_ds->lock); if (!spyee) return 0; /* We now hold the channel lock on spyee */ if (ast_check_hangup(chan) || ast_check_hangup(spyee)) { ast_channel_unlock(spyee); return 0; } name = ast_strdupa(spyee->name); if (option_verbose >= 2) ast_verbose(VERBOSE_PREFIX_2 "Spying on channel %s\n", name); memset(&csth, 0, sizeof(csth)); ast_audiohook_init(&csth.spy_audiohook, AST_AUDIOHOOK_TYPE_SPY, "ChanSpy"); if (start_spying(spyee, spyer_name, &csth.spy_audiohook)) { ast_audiohook_destroy(&csth.spy_audiohook); ast_channel_unlock(spyee); return 0; } if (ast_test_flag(flags, OPTION_WHISPER)) { ast_audiohook_init(&csth.whisper_audiohook, AST_AUDIOHOOK_TYPE_WHISPER, "ChanSpy"); start_spying(spyee, spyer_name, &csth.whisper_audiohook); } ast_channel_unlock(spyee); spyee = NULL; csth.volfactor = *volfactor; if (csth.volfactor) { csth.spy_audiohook.options.read_volume = csth.volfactor; csth.spy_audiohook.options.write_volume = csth.volfactor; } csth.fd = fd; if (ast_test_flag(flags, OPTION_PRIVATE)) silgen = ast_channel_start_silence_generator(chan); else ast_activate_generator(chan, &spygen, &csth); /* We can no longer rely on 'spyee' being an actual channel; it can be hung up and freed out from under us. However, the channel destructor will put NULL into our csth.spy.chan field when that happens, so that is our signal that the spyee channel has gone away. */ /* Note: it is very important that the ast_waitfor() be the first condition in this expression, so that if we wait for some period of time before receiving a frame from our spying channel, we check for hangup on the spied-on channel _after_ knowing that a frame has arrived, since the spied-on channel could have gone away while we were waiting */ while ((res = ast_waitfor(chan, -1) > -1) && csth.spy_audiohook.status == AST_AUDIOHOOK_STATUS_RUNNING) { if (!(f = ast_read(chan)) || ast_check_hangup(chan)) { running = -1; break; } if (ast_test_flag(flags, OPTION_WHISPER) && (f->frametype == AST_FRAME_VOICE)) { ast_audiohook_lock(&csth.whisper_audiohook); ast_audiohook_write_frame(&csth.whisper_audiohook, AST_AUDIOHOOK_DIRECTION_WRITE, f); ast_audiohook_unlock(&csth.whisper_audiohook); ast_frfree(f); continue; } res = (f->frametype == AST_FRAME_DTMF) ? f->subclass : 0; ast_frfree(f); if (!res) continue; if (x == sizeof(inp)) x = 0; if (res < 0) { running = -1; break; } if (res == '*') { running = 0; break; } else if (res == '#') { if (!ast_strlen_zero(inp)) { running = atoi(inp); break; } (*volfactor)++; if (*volfactor > 4) *volfactor = -4; if (option_verbose > 2) ast_verbose(VERBOSE_PREFIX_3 "Setting spy volume on %s to %d\n", chan->name, *volfactor); csth.volfactor = *volfactor; csth.spy_audiohook.options.read_volume = csth.volfactor; csth.spy_audiohook.options.write_volume = csth.volfactor; } else if (res >= '0' && res <= '9') { inp[x++] = res; } } if (ast_test_flag(flags, OPTION_PRIVATE)) ast_channel_stop_silence_generator(chan, silgen); else ast_deactivate_generator(chan); if (ast_test_flag(flags, OPTION_WHISPER)) { ast_audiohook_lock(&csth.whisper_audiohook); ast_audiohook_detach(&csth.whisper_audiohook); ast_audiohook_unlock(&csth.whisper_audiohook); ast_audiohook_destroy(&csth.whisper_audiohook); } ast_audiohook_lock(&csth.spy_audiohook); ast_audiohook_detach(&csth.spy_audiohook); ast_audiohook_unlock(&csth.spy_audiohook); ast_audiohook_destroy(&csth.spy_audiohook); if (option_verbose >= 2) ast_verbose(VERBOSE_PREFIX_2 "Done Spying on channel %s\n", name); return running; }