Debian Codegraph @

main/dial.c

raw symbols SCIP
1 /*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 1999 - 2007, Digium, Inc.
5 *
6 * Joshua Colp <[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 Dialing API
22 *
23 * \author Joshua Colp <[email protected]>
24 */
25
26 /*** MODULEINFO
27 <support_level>core</support_level>
28 ***/
29
30 #include "asterisk.h"
31
32 #include <sys/time.h>
33 #include <signal.h>
34
35 #include "asterisk/channel.h"
36 #include "asterisk/utils.h"
37 #include "asterisk/lock.h"
38 #include "asterisk/linkedlists.h"
39 #include "asterisk/dial.h"
40 #include "asterisk/pbx.h"
41 #include "asterisk/musiconhold.h"
42 #include "asterisk/app.h"
43 #include "asterisk/causes.h"
44 #include "asterisk/stasis_channels.h"
45 #include "asterisk/max_forwards.h"
46
47 /*! \brief Main dialing structure. Contains global options, channels being dialed, and more! */
48 struct ast_dial {
49 int num; /*!< Current number to give to next dialed channel */
50 int timeout; /*!< Maximum time allowed for dial attempts */
51 int actual_timeout; /*!< Actual timeout based on all factors (ie: channels) */
52 enum ast_dial_result state; /*!< Status of dial */
53 void *options[AST_DIAL_OPTION_MAX]; /*!< Global options */
54 ast_dial_state_callback state_callback; /*!< Status callback */
55 void *user_data; /*!< Attached user data */
56 AST_LIST_HEAD(, ast_dial_channel) channels; /*!< Channels being dialed */
57 pthread_t thread; /*!< Thread (if running in async) */
58 ast_callid callid; /*!< callid (if running in async) */
59 ast_mutex_t lock; /*! Lock to protect the thread information above */
60 };
61
62 /*! \brief Dialing channel structure. Contains per-channel dialing options, asterisk channel, and more! */
63 struct ast_dial_channel {
64 int num; /*!< Unique number for dialed channel */
65 int timeout; /*!< Maximum time allowed for attempt */
66 char *tech; /*!< Technology being dialed */
67 char *device; /*!< Device being dialed */
68 void *options[AST_DIAL_OPTION_MAX]; /*!< Channel specific options */
69 int cause; /*!< Cause code in case of failure */
70 unsigned int is_running_app:1; /*!< Is this running an application? */
71 char *assignedid1; /*!< UniqueID to assign channel */
72 char *assignedid2; /*!< UniqueID to assign 2nd channel */
73 struct ast_channel *owner; /*!< Asterisk channel */
74 AST_LIST_ENTRY(ast_dial_channel) list; /*!< Linked list information */
75 };
76
77 /*! \brief Typedef for dial option enable */
78 typedef void *(*ast_dial_option_cb_enable)(void *data);
79
80 /*! \brief Typedef for dial option disable */
81 typedef int (*ast_dial_option_cb_disable)(void *data);
82
83 /*! \brief Structure for 'ANSWER_EXEC' option */
84 struct answer_exec_struct {
85 char app[AST_MAX_APP]; /*!< Application name */
86 char *args; /*!< Application arguments */
87 };
88
89 /*! \brief Enable function for 'ANSWER_EXEC' option */
90 static void *answer_exec_enable(void *data)
91 {
92 struct answer_exec_struct *answer_exec = NULL;
93 char *app = ast_strdupa((char*)data), *args = NULL;
94
95 /* Not giving any data to this option is bad, mmmk? */
96 if (ast_strlen_zero(app))
97 return NULL;
98
99 /* Create new data structure */
100 if (!(answer_exec = ast_calloc(1, sizeof(*answer_exec))))
101 return NULL;
102
103 /* Parse out application and arguments */
104 if ((args = strchr(app, ','))) {
105 *args++ = '\0';
106 answer_exec->args = ast_strdup(args);
107 }
108
109 /* Copy application name */
110 ast_copy_string(answer_exec->app, app, sizeof(answer_exec->app));
111
112 return answer_exec;
113 }
114
115 /*! \brief Disable function for 'ANSWER_EXEC' option */
116 static int answer_exec_disable(void *data)
117 {
118 struct answer_exec_struct *answer_exec = data;
119
120 /* Make sure we have a value */
121 if (!answer_exec)
122 return -1;
123
124 /* If arguments are present, free them too */
125 if (answer_exec->args)
126 ast_free(answer_exec->args);
127
128 /* This is simple - just free the structure */
129 ast_free(answer_exec);
130
131 return 0;
132 }
133
134 static void *music_enable(void *data)
135 {
136 return ast_strdup(data);
137 }
138
139 static int music_disable(void *data)
140 {
141 if (!data)
142 return -1;
143
144 ast_free(data);
145
146 return 0;
147 }
148
149 static void *predial_enable(void *data)
150 {
151 return ast_strdup(data);
152 }
153
154 static int predial_disable(void *data)
155 {
156 if (!data) {
157 return -1;
158 }
159
160 ast_free(data);
161
162 return 0;
163 }
164
165 /*! \brief Application execution function for 'ANSWER_EXEC' option */
166 static void answer_exec_run(struct ast_dial *dial, struct ast_dial_channel *dial_channel, char *app, char *args)
167 {
168 struct ast_channel *chan = dial_channel->owner;
169
170 /* Execute the application, if available */
171 if (ast_pbx_exec_application(chan, app, args)) {
172 /* If the application was not found, return immediately */
173 return;
174 }
175
176 /* If another thread is not taking over hang up the channel */
177 ast_mutex_lock(&dial->lock);
178 if (dial->thread != AST_PTHREADT_STOP) {
179 ast_hangup(chan);
180 dial_channel->owner = NULL;
181 }
182 ast_mutex_unlock(&dial->lock);
183
184 return;
185 }
186
187 struct ast_option_types {
188 enum ast_dial_option option;
189 ast_dial_option_cb_enable enable;
190 ast_dial_option_cb_disable disable;
191 };
192
193 /*!
194 * \brief Map options to respective handlers (enable/disable).
195 *
196 * \note This list MUST be perfectly kept in order with enum
197 * ast_dial_option, or else madness will happen.
198 */
199 static const struct ast_option_types option_types[] = {
200 { AST_DIAL_OPTION_RINGING, NULL, NULL }, /*!< Always indicate ringing to caller */
201 { AST_DIAL_OPTION_ANSWER_EXEC, answer_exec_enable, answer_exec_disable }, /*!< Execute application upon answer in async mode */
202 { AST_DIAL_OPTION_MUSIC, music_enable, music_disable }, /*!< Play music to the caller instead of ringing */
203 { AST_DIAL_OPTION_DISABLE_CALL_FORWARDING, NULL, NULL }, /*!< Disable call forwarding on channels */
204 { AST_DIAL_OPTION_PREDIAL, predial_enable, predial_disable }, /*!< Execute a subroutine on the outbound channels prior to dialing */
205 { AST_DIAL_OPTION_DIAL_REPLACES_SELF, NULL, NULL }, /*!< The dial operation is a replacement for the requester */
206 { AST_DIAL_OPTION_SELF_DESTROY, NULL, NULL}, /*!< Destroy self at end of ast_dial_run */
207 { AST_DIAL_OPTION_MAX, NULL, NULL }, /*!< Terminator of list */
208 };
209
210 /*! \brief Maximum number of channels we can watch at a time */
211 #define AST_MAX_WATCHERS 256
212
213 /*! \brief Macro for finding the option structure to use on a dialed channel */
214 #define FIND_RELATIVE_OPTION(dial, dial_channel, ast_dial_option) (dial_channel->options[ast_dial_option] ? dial_channel->options[ast_dial_option] : dial->options[ast_dial_option])
215
216 /*! \brief Macro that determines whether a channel is the caller or not */
217 #define IS_CALLER(chan, owner) (chan == owner ? 1 : 0)
218
219 /*! \brief New dialing structure
220 * \note Create a dialing structure
221 * \return Returns a calloc'd ast_dial structure, NULL on failure
222 */
223 struct ast_dial *ast_dial_create(void)
224 {
225 struct ast_dial *dial = NULL;
226
227 /* Allocate new memory for structure */
228 if (!(dial = ast_calloc(1, sizeof(*dial))))
229 return NULL;
230
231 /* Initialize list of channels */
232 AST_LIST_HEAD_INIT(&dial->channels);
233
234 /* Initialize thread to NULL */
235 dial->thread = AST_PTHREADT_NULL;
236
237 /* No timeout exists... yet */
238 dial->timeout = -1;
239 dial->actual_timeout = -1;
240
241 /* Can't forget about the lock */
242 ast_mutex_init(&dial->lock);
243
244 return dial;
245 }
246
247 static int dial_append_common(struct ast_dial *dial, struct ast_dial_channel *channel,
248 const char *tech, const char *device, const struct ast_assigned_ids *assignedids)
249 {
250 /* Record technology and device for when we actually dial */
251 channel->tech = ast_strdup(tech);
252 channel->device = ast_strdup(device);
253
254 /* Store the assigned id */
255 if (assignedids && !ast_strlen_zero(assignedids->uniqueid)) {
256 channel->assignedid1 = ast_strdup(assignedids->uniqueid);
257
258 if (!ast_strlen_zero(assignedids->uniqueid2)) {
259 channel->assignedid2 = ast_strdup(assignedids->uniqueid2);
260 }
261 }
262
263 /* Grab reference number from dial structure */
264 channel->num = ast_atomic_fetchadd_int(&dial->num, +1);
265
266 /* No timeout exists... yet */
267 channel->timeout = -1;
268
269 /* Insert into channels list */
270 AST_LIST_INSERT_TAIL(&dial->channels, channel, list);
271
272 return channel->num;
273
274 }
275
276 /*! \brief Append a channel
277 * \note Appends a channel to a dialing structure
278 * \return Returns channel reference number on success, -1 on failure
279 */
280 int ast_dial_append(struct ast_dial *dial, const char *tech, const char *device, const struct ast_assigned_ids *assignedids)
281 {
282 struct ast_dial_channel *channel = NULL;
283
284 /* Make sure we have required arguments */
285 if (!dial || !tech || !device)
286 return -1;
287
288 /* Allocate new memory for dialed channel structure */
289 if (!(channel = ast_calloc(1, sizeof(*channel))))
290 return -1;
291
292 return dial_append_common(dial, channel, tech, device, assignedids);
293 }
294
295 int ast_dial_append_channel(struct ast_dial *dial, struct ast_channel *chan)
296 {
297 struct ast_dial_channel *channel;
298 char *tech;
299 char *device;
300 char *dash;
301
302 if (!dial || !chan) {
303 return -1;
304 }
305
306 channel = ast_calloc(1, sizeof(*channel));
307 if (!channel) {
308 return -1;
309 }
310 channel->owner = chan;
311
312 tech = ast_strdupa(ast_channel_name(chan));
313
314 device = strchr(tech, '/');
315 if (!device) {
316 ast_free(channel);
317 return -1;
318 }
319 *device++ = '\0';
320
321 dash = strrchr(device, '-');
322 if (dash) {
323 *dash = '\0';
324 }
325
326 return dial_append_common(dial, channel, tech, device, NULL);
327 }
328
329 /*! \brief Helper function that requests all channels */
330 static int begin_dial_prerun(struct ast_dial_channel *channel, struct ast_channel *chan, struct ast_format_cap *cap, const char *predial_string)
331 {
332 struct ast_format_cap *cap_all_audio = NULL;
333 struct ast_format_cap *cap_request;
334 struct ast_format_cap *requester_cap = NULL;
335 struct ast_assigned_ids assignedids = {
336 .uniqueid = channel->assignedid1,
337 .uniqueid2 = channel->assignedid2,
338 };
339
340 if (chan) {
341 int max_forwards;
342
343 ast_channel_lock(chan);
344 max_forwards = ast_max_forwards_get(chan);
345 requester_cap = ao2_bump(ast_channel_nativeformats(chan));
346 ast_channel_unlock(chan);
347
348 if (max_forwards <= 0) {
349 ast_log(LOG_WARNING, "Cannot dial from channel '%s'. Max forwards exceeded\n",
350 ast_channel_name(chan));
351 }
352 }
353
354 if (!channel->owner) {
355 if (cap && ast_format_cap_count(cap)) {
356 cap_request = cap;
357 } else if (requester_cap) {
358 cap_request = requester_cap;
359 } else {
360 cap_all_audio = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
361 ast_format_cap_append_by_type(cap_all_audio, AST_MEDIA_TYPE_AUDIO);
362 cap_request = cap_all_audio;
363 }
364
365 /* If we fail to create our owner channel bail out */
366 if (!(channel->owner = ast_request(channel->tech, cap_request, &assignedids, chan, channel->device, &channel->cause))) {
367 ao2_cleanup(cap_all_audio);
368 return -1;
369 }
370 cap_request = NULL;
371 ao2_cleanup(requester_cap);
372 ao2_cleanup(cap_all_audio);
373 }
374
375 if (chan) {
376 ast_channel_lock_both(chan, channel->owner);
377 } else {
378 ast_channel_lock(channel->owner);
379 }
380
381 ast_channel_stage_snapshot(channel->owner);
382
383 ast_channel_appl_set(channel->owner, "AppDial2");
384 ast_channel_data_set(channel->owner, "(Outgoing Line)");
385
386 memset(ast_channel_whentohangup(channel->owner), 0, sizeof(*ast_channel_whentohangup(channel->owner)));
387
388 /* Inherit everything from he who spawned this dial */
389 if (chan) {
390 ast_channel_inherit_variables(chan, channel->owner);
391 ast_channel_datastore_inherit(chan, channel->owner);
392 ast_max_forwards_decrement(channel->owner);
393
394 /* Copy over callerid information */
395 ast_party_redirecting_copy(ast_channel_redirecting(channel->owner), ast_channel_redirecting(chan));
396
397 ast_channel_dialed(channel->owner)->transit_network_select = ast_channel_dialed(chan)->transit_network_select;
398
399 ast_connected_line_copy_from_caller(ast_channel_connected(channel->owner), ast_channel_caller(chan));
400
401 ast_channel_language_set(channel->owner, ast_channel_language(chan));
402 if (channel->options[AST_DIAL_OPTION_DIAL_REPLACES_SELF]) {
403 ast_channel_req_accountcodes(channel->owner, chan, AST_CHANNEL_REQUESTOR_REPLACEMENT);
404 } else {
405 ast_channel_req_accountcodes(channel->owner, chan, AST_CHANNEL_REQUESTOR_BRIDGE_PEER);
406 }
407 if (ast_strlen_zero(ast_channel_musicclass(channel->owner)))
408 ast_channel_musicclass_set(channel->owner, ast_channel_musicclass(chan));
409
410 ast_channel_adsicpe_set(channel->owner, ast_channel_adsicpe(chan));
411 ast_channel_transfercapability_set(channel->owner, ast_channel_transfercapability(chan));
412 ast_channel_unlock(chan);
413 }
414
415 ast_channel_stage_snapshot_done(channel->owner);
416 ast_channel_unlock(channel->owner);
417
418 if (!ast_strlen_zero(predial_string)) {
419 if (chan) {
420 ast_autoservice_start(chan);
421 }
422 ast_pre_call(channel->owner, predial_string);
423 if (chan) {
424 ast_autoservice_stop(chan);
425 }
426 }
427
428 return 0;
429 }
430
431 int ast_dial_prerun(struct ast_dial *dial, struct ast_channel *chan, struct ast_format_cap *cap)
432 {
433 struct ast_dial_channel *channel;
434 int res = -1;
435 char *predial_string = dial->options[AST_DIAL_OPTION_PREDIAL];
436
437 AST_LIST_LOCK(&dial->channels);
438 AST_LIST_TRAVERSE(&dial->channels, channel, list) {
439 if ((res = begin_dial_prerun(channel, chan, cap, predial_string))) {
440 break;
441 }
442 }
443 AST_LIST_UNLOCK(&dial->channels);
444
445 return res;
446 }
447
448 /*! \brief Helper function that does the beginning dialing per-appended channel */
449 static int begin_dial_channel(struct ast_dial_channel *channel, struct ast_channel *chan, int async, const char *predial_string, struct ast_channel *forwarder_chan)
450 {
451 int res = 1;
452 char forwarder[AST_CHANNEL_NAME];
453
454 /* If no owner channel exists yet execute pre-run */
455 if (!channel->owner && begin_dial_prerun(channel, chan, NULL, predial_string)) {
456 return 0;
457 }
458
459 if (forwarder_chan) {
460 ast_copy_string(forwarder, ast_channel_name(forwarder_chan), sizeof(forwarder));
461 ast_channel_lock(channel->owner);
462 pbx_builtin_setvar_helper(channel->owner, "FORWARDERNAME", forwarder);
463 ast_channel_unlock(channel->owner);
464 }
465
466 /* Attempt to actually call this device */
467 if ((res = ast_call(channel->owner, channel->device, 0))) {
468 res = 0;
469 ast_hangup(channel->owner);
470 channel->owner = NULL;
471 } else {
472 ast_channel_publish_dial(async ? NULL : chan, channel->owner, channel->device, NULL);
473 res = 1;
474 ast_verb(3, "Called %s\n", channel->device);
475 }
476
477 return res;
478 }
479
480 /*! \brief Helper function that does the beginning dialing per dial structure */
481 static int begin_dial(struct ast_dial *dial, struct ast_channel *chan, int async)
482 {
483 struct ast_dial_channel *channel = NULL;
484 int success = 0;
485 char *predial_string = dial->options[AST_DIAL_OPTION_PREDIAL];
486
487 /* Iterate through channel list, requesting and calling each one */
488 AST_LIST_LOCK(&dial->channels);
489 AST_LIST_TRAVERSE(&dial->channels, channel, list) {
490 success += begin_dial_channel(channel, chan, async, predial_string, NULL);
491 }
492 AST_LIST_UNLOCK(&dial->channels);
493
494 /* If number of failures matches the number of channels, then this truly failed */
495 return success;
496 }
497
498 /*! \brief Helper function to handle channels that have been call forwarded */
499 static int handle_call_forward(struct ast_dial *dial, struct ast_dial_channel *channel, struct ast_channel *chan)
500 {
501 struct ast_channel *original = channel->owner;
502 char *tmp = ast_strdupa(ast_channel_call_forward(channel->owner));
503 char *tech = "Local", *device = tmp, *stuff;
504 char *predial_string = dial->options[AST_DIAL_OPTION_PREDIAL];
505
506 /* If call forwarding is disabled just drop the original channel and don't attempt to dial the new one */
507 if (FIND_RELATIVE_OPTION(dial, channel, AST_DIAL_OPTION_DISABLE_CALL_FORWARDING)) {
508 ast_hangup(original);
509 channel->owner = NULL;
510 return 0;
511 }
512
513 /* Figure out the new destination */
514 if ((stuff = strchr(tmp, '/'))) {
515 *stuff++ = '\0';
516 tech = tmp;
517 device = stuff;
518 } else {
519 const char *forward_context;
520 char destination[AST_MAX_CONTEXT + AST_MAX_EXTENSION + 1];
521
522 ast_channel_lock(original);
523 forward_context = pbx_builtin_getvar_helper(original, "FORWARD_CONTEXT");
524 snprintf(destination, sizeof(destination), "%s@%s", tmp, S_OR(forward_context, ast_channel_context(original)));
525 ast_channel_unlock(original);
526 device = ast_strdupa(destination);
527 }
528
529 /* Drop old destination information */
530 ast_free(channel->tech);
531 ast_free(channel->device);
532 ast_free(channel->assignedid1);
533 channel->assignedid1 = NULL;
534 ast_free(channel->assignedid2);
535 channel->assignedid2 = NULL;
536
537 /* Update the dial channel with the new destination information */
538 channel->tech = ast_strdup(tech);
539 channel->device = ast_strdup(device);
540 AST_LIST_UNLOCK(&dial->channels);
541
542 /* Drop the original channel */
543 channel->owner = NULL;
544
545 /* Finally give it a go... send it out into the world */
546 begin_dial_channel(channel, chan, chan ? 0 : 1, predial_string, original);
547
548 ast_channel_publish_dial_forward(chan, original, channel->owner, NULL, "CANCEL",
549 ast_channel_call_forward(original));
550
551 ast_hangup(original);
552
553 return 0;
554 }
555
556 /*! \brief Helper function that finds the dialed channel based on owner */
557 static struct ast_dial_channel *find_relative_dial_channel(struct ast_dial *dial, struct ast_channel *owner)
558 {
559 struct ast_dial_channel *channel = NULL;
560
561 AST_LIST_LOCK(&dial->channels);
562 AST_LIST_TRAVERSE(&dial->channels, channel, list) {
563 if (channel->owner == owner)
564 break;
565 }
566 AST_LIST_UNLOCK(&dial->channels);
567
568 return channel;
569 }
570
571 static void set_state(struct ast_dial *dial, enum ast_dial_result state)
572 {
573 dial->state = state;
574
575 if (dial->state_callback)
576 dial->state_callback(dial);
577 }
578
579 /*! \brief Helper function that handles frames */
580 static void handle_frame(struct ast_dial *dial, struct ast_dial_channel *channel, struct ast_frame *fr, struct ast_channel *chan)
581 {
582 if (fr->frametype == AST_FRAME_CONTROL) {
583 switch (fr->subclass.integer) {
584 case AST_CONTROL_ANSWER:
585 if (chan) {
586 ast_verb(3, "%s answered %s\n", ast_channel_name(channel->owner), ast_channel_name(chan));
587 } else {
588 ast_verb(3, "%s answered\n", ast_channel_name(channel->owner));
589 }
590 AST_LIST_LOCK(&dial->channels);
591 AST_LIST_REMOVE(&dial->channels, channel, list);
592 AST_LIST_INSERT_HEAD(&dial->channels, channel, list);
593 AST_LIST_UNLOCK(&dial->channels);
594 ast_channel_publish_dial(chan, channel->owner, channel->device, "ANSWER");
595 set_state(dial, AST_DIAL_RESULT_ANSWERED);
596 break;
597 case AST_CONTROL_BUSY:
598 ast_verb(3, "%s is busy\n", ast_channel_name(channel->owner));
599 ast_channel_publish_dial(chan, channel->owner, channel->device, "BUSY");
600 ast_hangup(channel->owner);
601 channel->cause = AST_CAUSE_USER_BUSY;
602 channel->owner = NULL;
603 break;
604 case AST_CONTROL_CONGESTION:
605 ast_verb(3, "%s is circuit-busy\n", ast_channel_name(channel->owner));
606 ast_channel_publish_dial(chan, channel->owner, channel->device, "CONGESTION");
607 ast_hangup(channel->owner);
608 channel->cause = AST_CAUSE_NORMAL_CIRCUIT_CONGESTION;
609 channel->owner = NULL;
610 break;
611 case AST_CONTROL_INCOMPLETE:
612 ast_verb(3, "%s dialed Incomplete extension %s\n", ast_channel_name(channel->owner), ast_channel_exten(channel->owner));
613 if (chan) {
614 ast_indicate(chan, AST_CONTROL_INCOMPLETE);
615 } else {
616 ast_hangup(channel->owner);
617 channel->cause = AST_CAUSE_UNALLOCATED;
618 channel->owner = NULL;
619 }
620 break;
621 case AST_CONTROL_RINGING:
622 ast_verb(3, "%s is ringing\n", ast_channel_name(channel->owner));
623 ast_channel_publish_dial(chan, channel->owner, channel->device, "RINGING");
624 if (chan && !dial->options[AST_DIAL_OPTION_MUSIC])
625 ast_indicate(chan, AST_CONTROL_RINGING);
626 set_state(dial, AST_DIAL_RESULT_RINGING);
627 break;
628 case AST_CONTROL_PROGRESS:
629 ast_channel_publish_dial(chan, channel->owner, channel->device, "PROGRESS");
630 if (chan) {
631 ast_verb(3, "%s is making progress, passing it to %s\n", ast_channel_name(channel->owner), ast_channel_name(chan));
632 ast_indicate(chan, AST_CONTROL_PROGRESS);
633 } else {
634 ast_verb(3, "%s is making progress\n", ast_channel_name(channel->owner));
635 }
636 set_state(dial, AST_DIAL_RESULT_PROGRESS);
637 break;
638 case AST_CONTROL_VIDUPDATE:
639 if (!chan) {
640 break;
641 }
642 ast_verb(3, "%s requested a video update, passing it to %s\n", ast_channel_name(channel->owner), ast_channel_name(chan));
643 ast_indicate(chan, AST_CONTROL_VIDUPDATE);
644 break;
645 case AST_CONTROL_SRCUPDATE:
646 if (!chan) {
647 break;
648 }
649 ast_verb(3, "%s requested a source update, passing it to %s\n", ast_channel_name(channel->owner), ast_channel_name(chan));
650 ast_indicate(chan, AST_CONTROL_SRCUPDATE);
651 break;
652 case AST_CONTROL_CONNECTED_LINE:
653 if (!chan) {
654 break;
655 }
656 ast_verb(3, "%s connected line has changed, passing it to %s\n", ast_channel_name(channel->owner), ast_channel_name(chan));
657 if (ast_channel_connected_line_sub(channel->owner, chan, fr, 1)) {
658 ast_indicate_data(chan, AST_CONTROL_CONNECTED_LINE, fr->data.ptr, fr->datalen);
659 }
660 break;
661 case AST_CONTROL_REDIRECTING:
662 if (!chan) {
663 break;
664 }
665 ast_verb(3, "%s redirecting info has changed, passing it to %s\n", ast_channel_name(channel->owner), ast_channel_name(chan));
666 if (ast_channel_redirecting_sub(channel->owner, chan, fr, 1)) {
667 ast_indicate_data(chan, AST_CONTROL_REDIRECTING, fr->data.ptr, fr->datalen);
668 }
669 break;
670 case AST_CONTROL_PROCEEDING:
671 ast_channel_publish_dial(chan, channel->owner, channel->device, "PROCEEDING");
672 if (chan) {
673 ast_verb(3, "%s is proceeding, passing it to %s\n", ast_channel_name(channel->owner), ast_channel_name(chan));
674 ast_indicate(chan, AST_CONTROL_PROCEEDING);
675 } else {
676 ast_verb(3, "%s is proceeding\n", ast_channel_name(channel->owner));
677 }
678 set_state(dial, AST_DIAL_RESULT_PROCEEDING);
679 break;
680 case AST_CONTROL_HOLD:
681 if (!chan) {
682 break;
683 }
684 ast_verb(3, "Call on %s placed on hold\n", ast_channel_name(chan));
685 ast_indicate_data(chan, AST_CONTROL_HOLD, fr->data.ptr, fr->datalen);
686 break;
687 case AST_CONTROL_UNHOLD:
688 if (!chan) {
689 break;
690 }
691 ast_verb(3, "Call on %s left from hold\n", ast_channel_name(chan));
692 ast_indicate(chan, AST_CONTROL_UNHOLD);
693 break;
694 case AST_CONTROL_OFFHOOK:
695 case AST_CONTROL_FLASH:
696 break;
697 case AST_CONTROL_PVT_CAUSE_CODE:
698 if (chan) {
699 ast_indicate_data(chan, AST_CONTROL_PVT_CAUSE_CODE, fr->data.ptr, fr->datalen);
700 }
701 break;
702 case -1:
703 if (chan) {
704 /* Prod the channel */
705 ast_indicate(chan, -1);
706 }
707 break;
708 default:
709 break;
710 }
711 }
712 }
713
714 /*! \brief Helper function to handle when a timeout occurs on dialing attempt */
715 static int handle_timeout_trip(struct ast_dial *dial, struct timeval start)
716 {
717 struct ast_dial_channel *channel = NULL;
718 int diff = ast_tvdiff_ms(ast_tvnow(), start), lowest_timeout = -1, new_timeout = -1;
719
720 /* If there is no difference yet return the dial timeout so we can go again, we were likely interrupted */
721 if (!diff) {
722 return dial->timeout;
723 }
724
725 /* If the global dial timeout tripped switch the state to timeout so our channel loop will drop every channel */
726 if (diff >= dial->timeout) {
727 set_state(dial, AST_DIAL_RESULT_TIMEOUT);
728 new_timeout = 0;
729 }
730
731 /* Go through dropping out channels that have met their timeout */
732 AST_LIST_TRAVERSE(&dial->channels, channel, list) {
733 if (dial->state == AST_DIAL_RESULT_TIMEOUT || diff >= channel->timeout) {
734 ast_channel_lock(channel->owner);
735 ast_channel_hangupcause_set(channel->owner, AST_CAUSE_NO_ANSWER);
736 ast_channel_unlock(channel->owner);
737 ast_hangup(channel->owner);
738 channel->cause = AST_CAUSE_NO_ANSWER;
739 channel->owner = NULL;
740 } else if ((lowest_timeout == -1) || (lowest_timeout > channel->timeout)) {
741 lowest_timeout = channel->timeout;
742 }
743 }
744
745 /* Calculate the new timeout using the lowest timeout found */
746 if (lowest_timeout >= 0)
747 new_timeout = lowest_timeout - diff;
748
749 return new_timeout;
750 }
751
752 const char *ast_hangup_cause_to_dial_status(int hangup_cause)
753 {
754 switch(hangup_cause) {
755 case AST_CAUSE_BUSY:
756 return "BUSY";
757 case AST_CAUSE_CONGESTION:
758 return "CONGESTION";
759 case AST_CAUSE_NO_ROUTE_DESTINATION:
760 case AST_CAUSE_UNREGISTERED:
761 return "CHANUNAVAIL";
762 case AST_CAUSE_NO_ANSWER:
763 default:
764 return "NOANSWER";
765 }
766 }
767
768 /*! \brief Helper function that basically keeps tabs on dialing attempts */
769 static enum ast_dial_result monitor_dial(struct ast_dial *dial, struct ast_channel *chan)
770 {
771 int timeout = -1;
772 struct ast_channel *cs[AST_MAX_WATCHERS], *who = NULL;
773 struct ast_dial_channel *channel = NULL;
774 struct answer_exec_struct *answer_exec = NULL;
775 struct timeval start;
776
777 set_state(dial, AST_DIAL_RESULT_TRYING);
778
779 /* If the "always indicate ringing" option is set, change state to ringing and indicate to the owner if present */
780 if (dial->options[AST_DIAL_OPTION_RINGING]) {
781 set_state(dial, AST_DIAL_RESULT_RINGING);
782 if (chan)
783 ast_indicate(chan, AST_CONTROL_RINGING);
784 } else if (chan && dial->options[AST_DIAL_OPTION_MUSIC] &&
785 !ast_strlen_zero(dial->options[AST_DIAL_OPTION_MUSIC])) {
786 char *original_moh = ast_strdupa(ast_channel_musicclass(chan));
787 ast_indicate(chan, -1);
788 ast_channel_musicclass_set(chan, dial->options[AST_DIAL_OPTION_MUSIC]);
789 ast_moh_start(chan, dial->options[AST_DIAL_OPTION_MUSIC], NULL);
790 ast_channel_musicclass_set(chan, original_moh);
791 }
792
793 /* Record start time for timeout purposes */
794 start = ast_tvnow();
795
796 /* We actually figured out the maximum timeout we can do as they were added, so we can directly access the info */
797 timeout = dial->actual_timeout;
798
799 /* Go into an infinite loop while we are trying */
800 while ((dial->state != AST_DIAL_RESULT_UNANSWERED) && (dial->state != AST_DIAL_RESULT_ANSWERED) && (dial->state != AST_DIAL_RESULT_HANGUP) && (dial->state != AST_DIAL_RESULT_TIMEOUT)) {
801 int pos = 0, count = 0;
802 struct ast_frame *fr = NULL;
803
804 /* Set up channel structure array */
805 pos = count = 0;
806 if (chan)
807 cs[pos++] = chan;
808
809 /* Add channels we are attempting to dial */
810 AST_LIST_LOCK(&dial->channels);
811 AST_LIST_TRAVERSE(&dial->channels, channel, list) {
812 if (channel->owner) {
813 cs[pos++] = channel->owner;
814 count++;
815 }
816 }
817 AST_LIST_UNLOCK(&dial->channels);
818
819 /* If we have no outbound channels in progress, switch state to unanswered and stop */
820 if (!count) {
821 set_state(dial, AST_DIAL_RESULT_UNANSWERED);
822 break;
823 }
824
825 /* Just to be safe... */
826 if (dial->thread == AST_PTHREADT_STOP)
827 break;
828
829 /* Wait for frames from channels */
830 who = ast_waitfor_n(cs, pos, &timeout);
831
832 /* Check to see if our thread is being canceled */
833 if (dial->thread == AST_PTHREADT_STOP)
834 break;
835
836 /* If the timeout no longer exists OR if we got no channel it basically means the timeout was tripped, so handle it */
837 if (!timeout || !who) {
838 timeout = handle_timeout_trip(dial, start);
839 continue;
840 }
841
842 /* Find relative dial channel */
843 if (!chan || !IS_CALLER(chan, who))
844 channel = find_relative_dial_channel(dial, who);
845
846 /* See if this channel has been forwarded elsewhere */
847 if (!ast_strlen_zero(ast_channel_call_forward(who))) {
848 handle_call_forward(dial, channel, chan);
849 continue;
850 }
851
852 /* Attempt to read in a frame */
853 if (!(fr = ast_read(who))) {
854 /* If this is the caller then we switch state to hangup and stop */
855 if (chan && IS_CALLER(chan, who)) {
856 set_state(dial, AST_DIAL_RESULT_HANGUP);
857 break;
858 }
859 ast_channel_publish_dial(chan, who, channel->device, ast_hangup_cause_to_dial_status(ast_channel_hangupcause(who)));
860 ast_hangup(who);
861 channel->owner = NULL;
862 continue;
863 }
864
865 /* Process the frame */
866 handle_frame(dial, channel, fr, chan);
867
868 /* Free the received frame and start all over */
869 ast_frfree(fr);
870 }
871
872 /* Do post-processing from loop */
873 if (dial->state == AST_DIAL_RESULT_ANSWERED) {
874 /* Hangup everything except that which answered */
875 AST_LIST_LOCK(&dial->channels);
876 AST_LIST_TRAVERSE(&dial->channels, channel, list) {
877 if (!channel->owner || channel->owner == who)
878 continue;
879 ast_channel_publish_dial(chan, channel->owner, channel->device, "CANCEL");
880 ast_hangup(channel->owner);
881 channel->cause = AST_CAUSE_ANSWERED_ELSEWHERE;
882 channel->owner = NULL;
883 }
884 AST_LIST_UNLOCK(&dial->channels);
885 /* If ANSWER_EXEC is enabled as an option, execute application on answered channel */
886 if ((channel = find_relative_dial_channel(dial, who)) && (answer_exec = FIND_RELATIVE_OPTION(dial, channel, AST_DIAL_OPTION_ANSWER_EXEC))) {
887 channel->is_running_app = 1;
888 answer_exec_run(dial, channel, answer_exec->app, answer_exec->args);
889 channel->is_running_app = 0;
890 }
891
892 if (chan && dial->options[AST_DIAL_OPTION_MUSIC] &&
893 !ast_strlen_zero(dial->options[AST_DIAL_OPTION_MUSIC])) {
894 ast_moh_stop(chan);
895 }
896 } else if (dial->state == AST_DIAL_RESULT_HANGUP) {
897 /* Hangup everything */
898 AST_LIST_LOCK(&dial->channels);
899 AST_LIST_TRAVERSE(&dial->channels, channel, list) {
900 if (!channel->owner)
901 continue;
902 ast_channel_publish_dial(chan, channel->owner, channel->device, "CANCEL");
903 ast_hangup(channel->owner);
904 channel->cause = AST_CAUSE_NORMAL_CLEARING;
905 channel->owner = NULL;
906 }
907 AST_LIST_UNLOCK(&dial->channels);
908 }
909
910 if (dial->options[AST_DIAL_OPTION_SELF_DESTROY]) {
911 enum ast_dial_result state = dial->state;
912
913 ast_dial_destroy(dial);
914 return state;
915 }
916
917 return dial->state;
918 }
919
920 /*! \brief Dial async thread function */
921 static void *async_dial(void *data)
922 {
923 struct ast_dial *dial = data;
924 if (dial->callid) {
925 ast_callid_threadassoc_add(dial->callid);
926 }
927
928 /* This is really really simple... we basically pass monitor_dial a NULL owner and it changes it's behavior */
929 monitor_dial(dial, NULL);
930
931 return NULL;
932 }
933
934 /*! \brief Execute dialing synchronously or asynchronously
935 * \note Dials channels in a dial structure.
936 * \return Returns dial result code. (TRYING/INVALID/FAILED/ANSWERED/TIMEOUT/UNANSWERED).
937 */
938 enum ast_dial_result ast_dial_run(struct ast_dial *dial, struct ast_channel *chan, int async)
939 {
940 enum ast_dial_result res = AST_DIAL_RESULT_TRYING;
941
942 /* Ensure required arguments are passed */
943 if (!dial) {
944 ast_debug(1, "invalid #1\n");
945 return AST_DIAL_RESULT_INVALID;
946 }
947
948 /* If there are no channels to dial we can't very well try to dial them */
949 if (AST_LIST_EMPTY(&dial->channels)) {
950 ast_debug(1, "invalid #2\n");
951 return AST_DIAL_RESULT_INVALID;
952 }
953
954 /* Dial each requested channel */
955 if (!begin_dial(dial, chan, async))
956 return AST_DIAL_RESULT_FAILED;
957
958 /* If we are running async spawn a thread and send it away... otherwise block here */
959 if (async) {
960 /* reference be released at dial destruction if it isn't NULL */
961 dial->callid = ast_read_threadstorage_callid();
962 dial->state = AST_DIAL_RESULT_TRYING;
963 /* Try to create a thread */
964 if (ast_pthread_create(&dial->thread, NULL, async_dial, dial)) {
965 /* Failed to create the thread - hangup all dialed channels and return failed */
966 ast_dial_hangup(dial);
967 res = AST_DIAL_RESULT_FAILED;
968 }
969 } else {
970 res = monitor_dial(dial, chan);
971 }
972
973 return res;
974 }
975
976 /*! \brief Return channel that answered
977 * \note Returns the Asterisk channel that answered
978 * \param dial Dialing structure
979 */
980 struct ast_channel *ast_dial_answered(struct ast_dial *dial)
981 {
982 if (!dial)
983 return NULL;
984
985 return ((dial->state == AST_DIAL_RESULT_ANSWERED) ? AST_LIST_FIRST(&dial->channels)->owner : NULL);
986 }
987
988 /*! \brief Steal the channel that answered
989 * \note Returns the Asterisk channel that answered and removes it from the dialing structure
990 * \param dial Dialing structure
991 */
992 struct ast_channel *ast_dial_answered_steal(struct ast_dial *dial)
993 {
994 struct ast_channel *chan = NULL;
995
996 if (!dial)
997 return NULL;
998
999 if (dial->state == AST_DIAL_RESULT_ANSWERED) {
1000 chan = AST_LIST_FIRST(&dial->channels)->owner;
1001 AST_LIST_FIRST(&dial->channels)->owner = NULL;
1002 }
1003
1004 return chan;
1005 }
1006
1007 /*! \brief Return state of dial
1008 * \note Returns the state of the dial attempt
1009 * \param dial Dialing structure
1010 */
1011 enum ast_dial_result ast_dial_state(struct ast_dial *dial)
1012 {
1013 return dial->state;
1014 }
1015
1016 /*! \brief Cancel async thread
1017 * \note Cancel a running async thread
1018 * \param dial Dialing structure
1019 */
1020 enum ast_dial_result ast_dial_join(struct ast_dial *dial)
1021 {
1022 pthread_t thread;
1023
1024 /* If the dial structure is not running in async, return failed */
1025 if (dial->thread == AST_PTHREADT_NULL)
1026 return AST_DIAL_RESULT_FAILED;
1027
1028 /* Record thread */
1029 thread = dial->thread;
1030
1031 /* Boom, commence locking */
1032 ast_mutex_lock(&dial->lock);
1033
1034 /* Stop the thread */
1035 dial->thread = AST_PTHREADT_STOP;
1036
1037 /* If the answered channel is running an application we have to soft hangup it, can't just poke the thread */
1038 AST_LIST_LOCK(&dial->channels);
1039 if (AST_LIST_FIRST(&dial->channels)->is_running_app) {
1040 struct ast_channel *chan = AST_LIST_FIRST(&dial->channels)->owner;
1041 if (chan) {
1042 ast_channel_lock(chan);
1043 ast_softhangup(chan, AST_SOFTHANGUP_EXPLICIT);
1044 ast_channel_unlock(chan);
1045 }
1046 } else {
1047 struct ast_dial_channel *channel = NULL;
1048
1049 /* Now we signal it with SIGURG so it will break out of it's waitfor */
1050 pthread_kill(thread, SIGURG);
1051
1052 /* pthread_kill may not be enough, if outgoing channel has already got an answer (no more in waitfor) but is not yet running an application. Force soft hangup. */
1053 AST_LIST_TRAVERSE(&dial->channels, channel, list) {
1054 if (channel->owner) {
1055 ast_softhangup(channel->owner, AST_SOFTHANGUP_EXPLICIT);
1056 }
1057 }
1058 }
1059 AST_LIST_UNLOCK(&dial->channels);
1060
1061 /* Yay done with it */
1062 ast_mutex_unlock(&dial->lock);
1063
1064 /* Finally wait for the thread to exit */
1065 pthread_join(thread, NULL);
1066
1067 /* Yay thread is all gone */
1068 dial->thread = AST_PTHREADT_NULL;
1069
1070 return dial->state;
1071 }
1072
1073 /*! \brief Hangup channels
1074 * \note Hangup all active channels
1075 * \param dial Dialing structure
1076 */
1077 void ast_dial_hangup(struct ast_dial *dial)
1078 {
1079 struct ast_dial_channel *channel = NULL;
1080
1081 if (!dial)
1082 return;
1083
1084 AST_LIST_LOCK(&dial->channels);
1085 AST_LIST_TRAVERSE(&dial->channels, channel, list) {
1086 ast_hangup(channel->owner);
1087 channel->owner = NULL;
1088 }
1089 AST_LIST_UNLOCK(&dial->channels);
1090
1091 return;
1092 }
1093
1094 int ast_dial_destroy(struct ast_dial *dial)
1095 {
1096 int i = 0;
1097 struct ast_dial_channel *channel = NULL;
1098
1099 if (!dial)
1100 return -1;
1101
1102 /* Hangup and deallocate all the dialed channels */
1103 AST_LIST_LOCK(&dial->channels);
1104 AST_LIST_TRAVERSE_SAFE_BEGIN(&dial->channels, channel, list) {
1105 /* Disable any enabled options */
1106 for (i = 0; i < AST_DIAL_OPTION_MAX; i++) {
1107 if (!channel->options[i])
1108 continue;
1109 if (option_types[i].disable)
1110 option_types[i].disable(channel->options[i]);
1111 channel->options[i] = NULL;
1112 }
1113
1114 /* Hang up channel if need be */
1115 ast_hangup(channel->owner);
1116 channel->owner = NULL;
1117
1118 /* Free structure */
1119 ast_free(channel->tech);
1120 ast_free(channel->device);
1121 ast_free(channel->assignedid1);
1122 ast_free(channel->assignedid2);
1123
1124 AST_LIST_REMOVE_CURRENT(list);
1125 ast_free(channel);
1126 }
1127 AST_LIST_TRAVERSE_SAFE_END;
1128 AST_LIST_UNLOCK(&dial->channels);
1129
1130 /* Disable any enabled options globally */
1131 for (i = 0; i < AST_DIAL_OPTION_MAX; i++) {
1132 if (!dial->options[i])
1133 continue;
1134 if (option_types[i].disable)
1135 option_types[i].disable(dial->options[i]);
1136 dial->options[i] = NULL;
1137 }
1138
1139 /* Lock be gone! */
1140 ast_mutex_destroy(&dial->lock);
1141
1142 /* Free structure */
1143 ast_free(dial);
1144
1145 return 0;
1146 }
1147
1148 int ast_dial_option_global_enable(struct ast_dial *dial, enum ast_dial_option option, void *data)
1149 {
1150 /* If the option is already enabled, return failure */
1151 if (dial->options[option])
1152 return -1;
1153
1154 /* Execute enable callback if it exists, if not simply make sure the value is set */
1155 if (option_types[option].enable)
1156 dial->options[option] = option_types[option].enable(data);
1157 else
1158 dial->options[option] = (void*)1;
1159
1160 return 0;
1161 }
1162
1163 /*! \brief Helper function for finding a channel in a dial structure based on number
1164 */
1165 static struct ast_dial_channel *find_dial_channel(struct ast_dial *dial, int num)
1166 {
1167 struct ast_dial_channel *channel = AST_LIST_LAST(&dial->channels);
1168
1169 /* We can try to predict programmer behavior, the last channel they added is probably the one they wanted to modify */
1170 if (channel->num == num)
1171 return channel;
1172
1173 /* Hrm not at the end... looking through the list it is! */
1174 AST_LIST_LOCK(&dial->channels);
1175 AST_LIST_TRAVERSE(&dial->channels, channel, list) {
1176 if (channel->num == num)
1177 break;
1178 }
1179 AST_LIST_UNLOCK(&dial->channels);
1180
1181 return channel;
1182 }
1183
1184 int ast_dial_option_enable(struct ast_dial *dial, int num, enum ast_dial_option option, void *data)
1185 {
1186 struct ast_dial_channel *channel = NULL;
1187
1188 /* Ensure we have required arguments */
1189 if (!dial || AST_LIST_EMPTY(&dial->channels))
1190 return -1;
1191
1192 if (!(channel = find_dial_channel(dial, num)))
1193 return -1;
1194
1195 /* If the option is already enabled, return failure */
1196 if (channel->options[option])
1197 return -1;
1198
1199 /* Execute enable callback if it exists, if not simply make sure the value is set */
1200 if (option_types[option].enable)
1201 channel->options[option] = option_types[option].enable(data);
1202 else
1203 channel->options[option] = (void*)1;
1204
1205 return 0;
1206 }
1207
1208 int ast_dial_option_global_disable(struct ast_dial *dial, enum ast_dial_option option)
1209 {
1210 /* If the option is not enabled, return failure */
1211 if (!dial->options[option]) {
1212 return -1;
1213 }
1214
1215 /* Execute callback of option to disable if it exists */
1216 if (option_types[option].disable)
1217 option_types[option].disable(dial->options[option]);
1218
1219 /* Finally disable option on the structure */
1220 dial->options[option] = NULL;
1221
1222 return 0;
1223 }
1224
1225 int ast_dial_option_disable(struct ast_dial *dial, int num, enum ast_dial_option option)
1226 {
1227 struct ast_dial_channel *channel = NULL;
1228
1229 /* Ensure we have required arguments */
1230 if (!dial || AST_LIST_EMPTY(&dial->channels))
1231 return -1;
1232
1233 if (!(channel = find_dial_channel(dial, num)))
1234 return -1;
1235
1236 /* If the option is not enabled, return failure */
1237 if (!channel->options[option])
1238 return -1;
1239
1240 /* Execute callback of option to disable it if it exists */
1241 if (option_types[option].disable)
1242 option_types[option].disable(channel->options[option]);
1243
1244 /* Finally disable the option on the structure */
1245 channel->options[option] = NULL;
1246
1247 return 0;
1248 }
1249
1250 int ast_dial_reason(struct ast_dial *dial, int num)
1251 {
1252 struct ast_dial_channel *channel;
1253
1254 if (!dial || AST_LIST_EMPTY(&dial->channels) || !(channel = find_dial_channel(dial, num))) {
1255 return -1;
1256 }
1257
1258 return channel->cause;
1259 }
1260
1261 struct ast_channel *ast_dial_get_channel(struct ast_dial *dial, int num)
1262 {
1263 struct ast_dial_channel *channel;
1264
1265 if (!dial || AST_LIST_EMPTY(&dial->channels) || !(channel = find_dial_channel(dial, num))) {
1266 return NULL;
1267 }
1268
1269 return channel->owner;
1270 }
1271
1272 void ast_dial_set_state_callback(struct ast_dial *dial, ast_dial_state_callback callback)
1273 {
1274 dial->state_callback = callback;
1275 }
1276
1277 void ast_dial_set_user_data(struct ast_dial *dial, void *user_data)
1278 {
1279 dial->user_data = user_data;
1280 }
1281
1282 void *ast_dial_get_user_data(struct ast_dial *dial)
1283 {
1284 return dial->user_data;
1285 }
1286
1287 void ast_dial_set_global_timeout(struct ast_dial *dial, int timeout)
1288 {
1289 dial->timeout = timeout;
1290
1291 if (dial->timeout > 0 && (dial->actual_timeout > dial->timeout || dial->actual_timeout == -1))
1292 dial->actual_timeout = dial->timeout;
1293
1294 return;
1295 }
1296
1297 void ast_dial_set_timeout(struct ast_dial *dial, int num, int timeout)
1298 {
1299 struct ast_dial_channel *channel = NULL;
1300
1301 if (!(channel = find_dial_channel(dial, num)))
1302 return;
1303
1304 channel->timeout = timeout;
1305
1306 if (channel->timeout > 0 && (dial->actual_timeout > channel->timeout || dial->actual_timeout == -1))
1307 dial->actual_timeout = channel->timeout;
1308
1309 return;
1310 }