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