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