Debian Codegraph @

main/channel.c

raw symbols SCIP
1 /*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 1999 - 2006, Digium, Inc.
5 *
6 * Mark Spencer <[email protected]>
7 *
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
13 *
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
17 */
18
19 /*! \file
20 *
21 * \brief Channel Management
22 *
23 * \author Mark Spencer <[email protected]>
24 */
25
26 /*** MODULEINFO
27 <support_level>core</support_level>
28 ***/
29
30 #include "asterisk.h"
31
32 #include "asterisk/_private.h"
33
34 #include <sys/time.h>
35 #include <signal.h>
36 #include <math.h>
37
38 #include "asterisk/paths.h" /* use ast_config_AST_SYSTEM_NAME */
39
40 #include "asterisk/pbx.h"
41 #include "asterisk/frame.h"
42 #include "asterisk/mod_format.h"
43 #include "asterisk/sched.h"
44 #include "asterisk/channel.h"
45 #include "asterisk/cel.h"
46 #include "asterisk/musiconhold.h"
47 #include "asterisk/say.h"
48 #include "asterisk/file.h"
49 #include "asterisk/cli.h"
50 #include "asterisk/translate.h"
51 #include "asterisk/manager.h"
52 #include "asterisk/chanvars.h"
53 #include "asterisk/linkedlists.h"
54 #include "asterisk/indications.h"
55 #include "asterisk/causes.h"
56 #include "asterisk/callerid.h"
57 #include "asterisk/utils.h"
58 #include "asterisk/lock.h"
59 #include "asterisk/app.h"
60 #include "asterisk/transcap.h"
61 #include "asterisk/devicestate.h"
62 #include "asterisk/threadstorage.h"
63 #include "asterisk/slinfactory.h"
64 #include "asterisk/audiohook.h"
65 #include "asterisk/framehook.h"
66 #include "asterisk/timing.h"
67 #include "asterisk/autochan.h"
68 #include "asterisk/stringfields.h"
69 #include "asterisk/global_datastores.h"
70 #include "asterisk/channel_internal.h"
71 #include "asterisk/features.h"
72 #include "asterisk/bridge.h"
73 #include "asterisk/test.h"
74 #include "asterisk/stasis_channels.h"
75 #include "asterisk/max_forwards.h"
76 #include "asterisk/stream.h"
77 #include "asterisk/message.h"
78 #include "asterisk/rtp_engine.h"
79
80 #include "channelstorage.h"
81
82 /*** DOCUMENTATION
83 ***/
84
85 #if defined(KEEP_TILL_CHANNEL_PARTY_NUMBER_INFO_NEEDED)
86 #if defined(HAVE_PRI)
87 #include "libpri.h"
88 #endif /* defined(HAVE_PRI) */
89 #endif /* defined(KEEP_TILL_CHANNEL_PARTY_NUMBER_INFO_NEEDED) */
90
91 /* uncomment if you have problems with 'monitoring' synchronized files */
92 #if 0
93 #define MONITOR_CONSTANT_DELAY
94 #define MONITOR_DELAY 150 * 8 /*!< 150 ms of MONITORING DELAY */
95 #endif
96
97 static int chancount;
98
99 unsigned long global_fin, global_fout;
100
101 AST_THREADSTORAGE(state2str_threadbuf);
102 #define STATE2STR_BUFSIZE 32
103
104 /*! Default amount of time to use when emulating a DTMF digit as a begin and end
105 * 100ms */
106 #define AST_DEFAULT_EMULATE_DTMF_DURATION 100
107
108 /*! Default amount of time to use when emulating an MF digit as a begin and end
109 * 55ms */
110 #define DEFAULT_EMULATE_MF_DURATION 55
111
112 #define DEFAULT_AMA_FLAGS AST_AMA_DOCUMENTATION
113
114 /*! Minimum amount of time between the end of the last digit and the beginning
115 * of a new one - 45ms */
116 #define AST_MIN_DTMF_GAP 45
117
118 /*! \brief List of channel drivers */
119 struct chanlist {
120 const struct ast_channel_tech *tech;
121 AST_LIST_ENTRY(chanlist) list;
122 };
123
124 /*! \brief the list of registered channel types */
125 static AST_RWLIST_HEAD_STATIC(backends, chanlist);
126
127 /*! \brief map AST_CAUSE's to readable string representations
128 *
129 * \ref causes.h
130 */
131 struct causes_map {
132 int cause;
133 const char *name;
134 const char *desc;
135 };
136
137 static const struct causes_map causes[] = {
138 { AST_CAUSE_UNALLOCATED, "UNALLOCATED", "Unallocated (unassigned) number" },
139 { AST_CAUSE_NO_ROUTE_TRANSIT_NET, "NO_ROUTE_TRANSIT_NET", "No route to specified transmit network" },
140 { AST_CAUSE_NO_ROUTE_DESTINATION, "NO_ROUTE_DESTINATION", "No route to destination" },
141 { AST_CAUSE_MISDIALLED_TRUNK_PREFIX, "MISDIALLED_TRUNK_PREFIX", "Misdialed trunk prefix" },
142 { AST_CAUSE_CHANNEL_UNACCEPTABLE, "CHANNEL_UNACCEPTABLE", "Channel unacceptable" },
143 { AST_CAUSE_CALL_AWARDED_DELIVERED, "CALL_AWARDED_DELIVERED", "Call awarded and being delivered in an established channel" },
144 { AST_CAUSE_PRE_EMPTED, "PRE_EMPTED", "Pre-empted" },
145 { AST_CAUSE_NUMBER_PORTED_NOT_HERE, "NUMBER_PORTED_NOT_HERE", "Number ported elsewhere" },
146 { AST_CAUSE_NORMAL_CLEARING, "NORMAL_CLEARING", "Normal Clearing" },
147 { AST_CAUSE_USER_BUSY, "USER_BUSY", "User busy" },
148 { AST_CAUSE_NO_USER_RESPONSE, "NO_USER_RESPONSE", "No user responding" },
149 { AST_CAUSE_NO_ANSWER, "NO_ANSWER", "User alerting, no answer" },
150 { AST_CAUSE_SUBSCRIBER_ABSENT, "SUBSCRIBER_ABSENT", "Subscriber absent" },
151 { AST_CAUSE_CALL_REJECTED, "CALL_REJECTED", "Call Rejected" },
152 { AST_CAUSE_NUMBER_CHANGED, "NUMBER_CHANGED", "Number changed" },
153 { AST_CAUSE_REDIRECTED_TO_NEW_DESTINATION, "REDIRECTED_TO_NEW_DESTINATION", "Redirected to new destination" },
154 { AST_CAUSE_ANSWERED_ELSEWHERE, "ANSWERED_ELSEWHERE", "Answered elsewhere" },
155 { AST_CAUSE_DESTINATION_OUT_OF_ORDER, "DESTINATION_OUT_OF_ORDER", "Destination out of order" },
156 { AST_CAUSE_INVALID_NUMBER_FORMAT, "INVALID_NUMBER_FORMAT", "Invalid number format" },
157 { AST_CAUSE_FACILITY_REJECTED, "FACILITY_REJECTED", "Facility rejected" },
158 { AST_CAUSE_RESPONSE_TO_STATUS_ENQUIRY, "RESPONSE_TO_STATUS_ENQUIRY", "Response to STATus ENQuiry" },
159 { AST_CAUSE_NORMAL_UNSPECIFIED, "NORMAL_UNSPECIFIED", "Normal, unspecified" },
160 { AST_CAUSE_NORMAL_CIRCUIT_CONGESTION, "NORMAL_CIRCUIT_CONGESTION", "Circuit/channel congestion" },
161 { AST_CAUSE_NETWORK_OUT_OF_ORDER, "NETWORK_OUT_OF_ORDER", "Network out of order" },
162 { AST_CAUSE_NORMAL_TEMPORARY_FAILURE, "NORMAL_TEMPORARY_FAILURE", "Temporary failure" },
163 { AST_CAUSE_SWITCH_CONGESTION, "SWITCH_CONGESTION", "Switching equipment congestion" },
164 { AST_CAUSE_ACCESS_INFO_DISCARDED, "ACCESS_INFO_DISCARDED", "Access information discarded" },
165 { AST_CAUSE_REQUESTED_CHAN_UNAVAIL, "REQUESTED_CHAN_UNAVAIL", "Requested channel not available" },
166 { AST_CAUSE_FACILITY_NOT_SUBSCRIBED, "FACILITY_NOT_SUBSCRIBED", "Facility not subscribed" },
167 { AST_CAUSE_OUTGOING_CALL_BARRED, "OUTGOING_CALL_BARRED", "Outgoing call barred" },
168 { AST_CAUSE_INCOMING_CALL_BARRED, "INCOMING_CALL_BARRED", "Incoming call barred" },
169 { AST_CAUSE_BEARERCAPABILITY_NOTAUTH, "BEARERCAPABILITY_NOTAUTH", "Bearer capability not authorized" },
170 { AST_CAUSE_BEARERCAPABILITY_NOTAVAIL, "BEARERCAPABILITY_NOTAVAIL", "Bearer capability not available" },
171 { AST_CAUSE_BEARERCAPABILITY_NOTIMPL, "BEARERCAPABILITY_NOTIMPL", "Bearer capability not implemented" },
172 { AST_CAUSE_CHAN_NOT_IMPLEMENTED, "CHAN_NOT_IMPLEMENTED", "Channel not implemented" },
173 { AST_CAUSE_FACILITY_NOT_IMPLEMENTED, "FACILITY_NOT_IMPLEMENTED", "Facility not implemented" },
174 { AST_CAUSE_INVALID_CALL_REFERENCE, "INVALID_CALL_REFERENCE", "Invalid call reference value" },
175 { AST_CAUSE_INCOMPATIBLE_DESTINATION, "INCOMPATIBLE_DESTINATION", "Incompatible destination" },
176 { AST_CAUSE_INVALID_MSG_UNSPECIFIED, "INVALID_MSG_UNSPECIFIED", "Invalid message unspecified" },
177 { AST_CAUSE_MANDATORY_IE_MISSING, "MANDATORY_IE_MISSING", "Mandatory information element is missing" },
178 { AST_CAUSE_MESSAGE_TYPE_NONEXIST, "MESSAGE_TYPE_NONEXIST", "Message type nonexist." },
179 { AST_CAUSE_WRONG_MESSAGE, "WRONG_MESSAGE", "Wrong message" },
180 { AST_CAUSE_IE_NONEXIST, "IE_NONEXIST", "Info. element nonexist or not implemented" },
181 { AST_CAUSE_INVALID_IE_CONTENTS, "INVALID_IE_CONTENTS", "Invalid information element contents" },
182 { AST_CAUSE_WRONG_CALL_STATE, "WRONG_CALL_STATE", "Message not compatible with call state" },
183 { AST_CAUSE_RECOVERY_ON_TIMER_EXPIRE, "RECOVERY_ON_TIMER_EXPIRE", "Recover on timer expiry" },
184 { AST_CAUSE_MANDATORY_IE_LENGTH_ERROR, "MANDATORY_IE_LENGTH_ERROR", "Mandatory IE length error" },
185 { AST_CAUSE_PROTOCOL_ERROR, "PROTOCOL_ERROR", "Protocol error, unspecified" },
186 { AST_CAUSE_INTERWORKING, "INTERWORKING", "Interworking, unspecified" },
187 };
188
189 struct ast_variable *ast_channeltype_list(void)
190 {
191 struct chanlist *cl;
192 struct ast_variable *var = NULL, *prev = NULL;
193
194 AST_RWLIST_RDLOCK(&backends);
195 AST_RWLIST_TRAVERSE(&backends, cl, list) {
196 if (prev) {
197 if ((prev->next = ast_variable_new(cl->tech->type, cl->tech->description, "")))
198 prev = prev->next;
199 } else {
200 var = ast_variable_new(cl->tech->type, cl->tech->description, "");
201 prev = var;
202 }
203 }
204 AST_RWLIST_UNLOCK(&backends);
205
206 return var;
207 }
208
209 #if defined(KEEP_TILL_CHANNEL_PARTY_NUMBER_INFO_NEEDED)
210 static const char *party_number_ton2str(int ton)
211 {
212 #if defined(HAVE_PRI)
213 switch ((ton >> 4) & 0x07) {
214 case PRI_TON_INTERNATIONAL:
215 return "International";
216 case PRI_TON_NATIONAL:
217 return "National";
218 case PRI_TON_NET_SPECIFIC:
219 return "Network Specific";
220 case PRI_TON_SUBSCRIBER:
221 return "Subscriber";
222 case PRI_TON_ABBREVIATED:
223 return "Abbreviated";
224 case PRI_TON_RESERVED:
225 return "Reserved";
226 case PRI_TON_UNKNOWN:
227 default:
228 break;
229 }
230 #endif /* defined(HAVE_PRI) */
231 return "Unknown";
232 }
233 #endif /* defined(KEEP_TILL_CHANNEL_PARTY_NUMBER_INFO_NEEDED) */
234
235 #if defined(KEEP_TILL_CHANNEL_PARTY_NUMBER_INFO_NEEDED)
236 static const char *party_number_plan2str(int plan)
237 {
238 #if defined(HAVE_PRI)
239 switch (plan & 0x0F) {
240 default:
241 case PRI_NPI_UNKNOWN:
242 break;
243 case PRI_NPI_E163_E164:
244 return "Public (E.163/E.164)";
245 case PRI_NPI_X121:
246 return "Data (X.121)";
247 case PRI_NPI_F69:
248 return "Telex (F.69)";
249 case PRI_NPI_NATIONAL:
250 return "National Standard";
251 case PRI_NPI_PRIVATE:
252 return "Private";
253 case PRI_NPI_RESERVED:
254 return "Reserved";
255 }
256 #endif /* defined(HAVE_PRI) */
257 return "Unknown";
258 }
259 #endif /* defined(KEEP_TILL_CHANNEL_PARTY_NUMBER_INFO_NEEDED) */
260
261 /*! \brief Show channel types - CLI command */
262 static char *handle_cli_core_show_channeltypes(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
263 {
264 #define FORMAT "%-15.15s %-40.40s %-13.13s %-13.13s %-13.13s %-13.13s\n"
265 struct chanlist *cl;
266 int count_chan = 0;
267
268 switch (cmd) {
269 case CLI_INIT:
270 e->command = "core show channeltypes";
271 e->usage =
272 "Usage: core show channeltypes\n"
273 " Lists available channel types registered in your\n"
274 " Asterisk server.\n";
275 return NULL;
276 case CLI_GENERATE:
277 return NULL;
278 }
279
280 if (a->argc != 3)
281 return CLI_SHOWUSAGE;
282
283 ast_cli(a->fd, FORMAT, "Type", "Description", "Devicestate", "Presencestate", "Indications", "Transfer");
284 ast_cli(a->fd, FORMAT, "-------------", "-------------", "-------------", "-------------", "-------------", "-------------");
285
286 AST_RWLIST_RDLOCK(&backends);
287 AST_RWLIST_TRAVERSE(&backends, cl, list) {
288 ast_cli(a->fd, FORMAT, cl->tech->type, cl->tech->description,
289 (cl->tech->devicestate) ? "yes" : "no",
290 (cl->tech->presencestate) ? "yes" : "no",
291 (cl->tech->indicate) ? "yes" : "no",
292 (cl->tech->transfer) ? "yes" : "no");
293 count_chan++;
294 }
295 AST_RWLIST_UNLOCK(&backends);
296
297 ast_cli(a->fd, "----------\n%d channel drivers registered.\n", count_chan);
298
299 return CLI_SUCCESS;
300
301 #undef FORMAT
302 }
303
304 static char *complete_channeltypes(struct ast_cli_args *a)
305 {
306 struct chanlist *cl;
307 int wordlen;
308
309 if (a->pos != 3) {
310 return NULL;
311 }
312
313 wordlen = strlen(a->word);
314
315 AST_RWLIST_RDLOCK(&backends);
316 AST_RWLIST_TRAVERSE(&backends, cl, list) {
317 if (!strncasecmp(a->word, cl->tech->type, wordlen)) {
318 ast_cli_completion_add(ast_strdup(cl->tech->type));
319 }
320 }
321 AST_RWLIST_UNLOCK(&backends);
322
323 return NULL;
324 }
325
326 /*! \brief Show details about a channel driver - CLI command */
327 static char *handle_cli_core_show_channeltype(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
328 {
329 struct chanlist *cl = NULL;
330 struct ast_str *codec_buf = ast_str_alloca(AST_FORMAT_CAP_NAMES_LEN);
331
332 switch (cmd) {
333 case CLI_INIT:
334 e->command = "core show channeltype";
335 e->usage =
336 "Usage: core show channeltype <name>\n"
337 " Show details about the specified channel type, <name>.\n";
338 return NULL;
339 case CLI_GENERATE:
340 return complete_channeltypes(a);
341 }
342
343 if (a->argc != 4)
344 return CLI_SHOWUSAGE;
345
346 AST_RWLIST_RDLOCK(&backends);
347
348 AST_RWLIST_TRAVERSE(&backends, cl, list) {
349 if (!strncasecmp(cl->tech->type, a->argv[3], strlen(cl->tech->type)))
350 break;
351 }
352
353
354 if (!cl) {
355 ast_cli(a->fd, "\n%s is not a registered channel driver.\n", a->argv[3]);
356 AST_RWLIST_UNLOCK(&backends);
357 return CLI_FAILURE;
358 }
359
360 ast_cli(a->fd,
361 "-- Info about channel driver: %s --\n"
362 " Device State: %s\n"
363 "Presence State: %s\n"
364 " Indication: %s\n"
365 " Transfer : %s\n"
366 " Capabilities: %s\n"
367 " Digit Begin: %s\n"
368 " Digit End: %s\n"
369 " Send HTML : %s\n"
370 " Image Support: %s\n"
371 " Text Support: %s\n",
372 cl->tech->type,
373 (cl->tech->devicestate) ? "yes" : "no",
374 (cl->tech->presencestate) ? "yes" : "no",
375 (cl->tech->indicate) ? "yes" : "no",
376 (cl->tech->transfer) ? "yes" : "no",
377 ast_format_cap_get_names(cl->tech->capabilities, &codec_buf),
378 (cl->tech->send_digit_begin) ? "yes" : "no",
379 (cl->tech->send_digit_end) ? "yes" : "no",
380 (cl->tech->send_html) ? "yes" : "no",
381 (cl->tech->send_image) ? "yes" : "no",
382 (cl->tech->send_text) ? "yes" : "no"
383
384 );
385
386 AST_RWLIST_UNLOCK(&backends);
387
388 return CLI_SUCCESS;
389 }
390
391 static struct ast_cli_entry cli_channel[] = {
392 AST_CLI_DEFINE(handle_cli_core_show_channeltypes, "List available channel types"),
393 AST_CLI_DEFINE(handle_cli_core_show_channeltype, "Give more details on that channel type")
394 };
395
396 static struct ast_frame *kill_read(struct ast_channel *chan)
397 {
398 /* Hangup channel. */
399 return NULL;
400 }
401
402 static struct ast_frame *kill_exception(struct ast_channel *chan)
403 {
404 /* Hangup channel. */
405 return NULL;
406 }
407
408 static int kill_write(struct ast_channel *chan, struct ast_frame *frame)
409 {
410 /* Hangup channel. */
411 return -1;
412 }
413
414 static int kill_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
415 {
416 /* No problem fixing up the channel. */
417 return 0;
418 }
419
420 static int kill_hangup(struct ast_channel *chan)
421 {
422 ast_channel_tech_pvt_set(chan, NULL);
423 return 0;
424 }
425
426 /*!
427 * \brief Kill the channel channel driver technology descriptor.
428 *
429 * \details
430 * The purpose of this channel technology is to encourage the
431 * channel to hangup as quickly as possible.
432 *
433 * \note Used by DTMF atxfer and zombie channels.
434 */
435 const struct ast_channel_tech ast_kill_tech = {
436 .type = "Kill",
437 .description = "Kill channel (should not see this)",
438 .read = kill_read,
439 .exception = kill_exception,
440 .write = kill_write,
441 .fixup = kill_fixup,
442 .hangup = kill_hangup,
443 };
444
445 /*! \brief Checks to see if a channel is needing hang up */
446 int ast_check_hangup(struct ast_channel *chan)
447 {
448 if (ast_channel_softhangup_internal_flag(chan)) /* yes if soft hangup flag set */
449 return 1;
450 if (ast_tvzero(*ast_channel_whentohangup(chan))) /* no if no hangup scheduled */
451 return 0;
452 if (ast_tvdiff_ms(*ast_channel_whentohangup(chan), ast_tvnow()) > 0) /* no if hangup time has not come yet. */
453 return 0;
454 ast_debug(4, "Hangup time has come: %" PRIi64 "\n", ast_tvdiff_ms(*ast_channel_whentohangup(chan), ast_tvnow()));
455 ast_test_suite_event_notify("HANGUP_TIME", "Channel: %s", ast_channel_name(chan));
456 ast_channel_softhangup_internal_flag_add(chan, AST_SOFTHANGUP_TIMEOUT); /* record event */
457 return 1;
458 }
459
460 int ast_check_hangup_locked(struct ast_channel *chan)
461 {
462 int res;
463 ast_channel_lock(chan);
464 res = ast_check_hangup(chan);
465 ast_channel_unlock(chan);
466 return res;
467 }
468
469 void ast_channel_softhangup_withcause_locked(struct ast_channel *chan, int causecode)
470 {
471 ast_channel_lock(chan);
472
473 if (causecode > 0) {
474 ast_debug(1, "Setting hangupcause of channel %s to %d (is %d now)\n",
475 ast_channel_name(chan), causecode, ast_channel_hangupcause(chan));
476
477 ast_channel_hangupcause_set(chan, causecode);
478 }
479
480 ast_softhangup_nolock(chan, AST_SOFTHANGUP_EXPLICIT);
481
482 ast_channel_unlock(chan);
483 }
484
485 static int ast_channel_softhangup_cb(void *obj, void *arg, void *data, int flags)
486 {
487 struct ast_channel *chan = obj;
488
489 ast_softhangup(chan, AST_SOFTHANGUP_SHUTDOWN);
490
491 return 0;
492 }
493
494 void ast_softhangup_all(void)
495 {
496 ast_channel_callback(ast_channel_softhangup_cb, NULL, NULL, 0);
497 }
498
499 /*! \brief returns number of active/allocated channels */
500 int ast_active_channels(void)
501 {
502 return current_channel_storage_instance ? CHANNELSTORAGE_API(current_channel_storage_instance, active_channels, 1) : 0;
503 }
504
505 int ast_undestroyed_channels(void)
506 {
507 return ast_atomic_fetchadd_int(&chancount, 0);
508 }
509
510 /*! \brief Set when to hangup channel */
511 void ast_channel_setwhentohangup_tv(struct ast_channel *chan, struct timeval offset)
512 {
513 if (ast_tvzero(offset)) {
514 ast_channel_whentohangup_set(chan, &offset);
515 } else {
516 struct timeval tv = ast_tvadd(offset, ast_tvnow());
517 ast_channel_whentohangup_set(chan, &tv);
518 }
519 ast_queue_frame(chan, &ast_null_frame);
520 return;
521 }
522
523 /*! \brief Compare a offset with when to hangup channel */
524 int ast_channel_cmpwhentohangup_tv(struct ast_channel *chan, struct timeval offset)
525 {
526 struct timeval whentohangup;
527
528 if (ast_tvzero(*ast_channel_whentohangup(chan)))
529 return ast_tvzero(offset) ? 0 : -1;
530
531 if (ast_tvzero(offset))
532 return 1;
533
534 whentohangup = ast_tvadd(offset, ast_tvnow());
535
536 return ast_tvdiff_ms(whentohangup, *ast_channel_whentohangup(chan));
537 }
538
539 /*! \brief Register a new telephony channel in Asterisk */
540 int ast_channel_register(const struct ast_channel_tech *tech)
541 {
542 struct chanlist *chan;
543
544 AST_RWLIST_WRLOCK(&backends);
545
546 AST_RWLIST_TRAVERSE(&backends, chan, list) {
547 if (!strcasecmp(tech->type, chan->tech->type)) {
548 ast_log(LOG_WARNING, "Already have a handler for type '%s'\n", tech->type);
549 AST_RWLIST_UNLOCK(&backends);
550 return -1;
551 }
552 }
553
554 if (!(chan = ast_calloc(1, sizeof(*chan)))) {
555 AST_RWLIST_UNLOCK(&backends);
556 return -1;
557 }
558 chan->tech = tech;
559 AST_RWLIST_INSERT_HEAD(&backends, chan, list);
560
561 ast_debug(5, "Registered handler for '%s' (%s)\n", chan->tech->type, chan->tech->description);
562
563 ast_verb(5, "Registered channel type '%s' (%s)\n", chan->tech->type, chan->tech->description);
564
565 AST_RWLIST_UNLOCK(&backends);
566
567 return 0;
568 }
569
570 /*! \brief Unregister channel driver */
571 void ast_channel_unregister(const struct ast_channel_tech *tech)
572 {
573 struct chanlist *chan;
574
575 ast_debug(5, "Unregistering channel type '%s'\n", tech->type);
576
577 AST_RWLIST_WRLOCK(&backends);
578
579 AST_RWLIST_TRAVERSE_SAFE_BEGIN(&backends, chan, list) {
580 if (chan->tech == tech) {
581 AST_LIST_REMOVE_CURRENT(list);
582 ast_free(chan);
583 ast_verb(5, "Unregistered channel type '%s'\n", tech->type);
584 break;
585 }
586 }
587 AST_LIST_TRAVERSE_SAFE_END;
588
589 AST_RWLIST_UNLOCK(&backends);
590 }
591
592 /*! \brief Get handle to channel driver based on name */
593 const struct ast_channel_tech *ast_get_channel_tech(const char *name)
594 {
595 struct chanlist *chanls;
596 const struct ast_channel_tech *ret = NULL;
597
598 AST_RWLIST_RDLOCK(&backends);
599
600 AST_RWLIST_TRAVERSE(&backends, chanls, list) {
601 if (!strcasecmp(name, chanls->tech->type)) {
602 ret = chanls->tech;
603 break;
604 }
605 }
606
607 AST_RWLIST_UNLOCK(&backends);
608
609 return ret;
610 }
611
612 /*! \brief Gives the string form of a given hangup cause */
613 const char *ast_cause2str(int cause)
614 {
615 int x;
616
617 for (x = 0; x < ARRAY_LEN(causes); x++) {
618 if (causes[x].cause == cause)
619 return causes[x].desc;
620 }
621
622 return "Unknown";
623 }
624
625 /*! \brief Convert a symbolic hangup cause to number */
626 int ast_str2cause(const char *name)
627 {
628 int x;
629
630 for (x = 0; x < ARRAY_LEN(causes); x++)
631 if (!strncasecmp(causes[x].name, name, strlen(causes[x].name)))
632 return causes[x].cause;
633
634 return -1;
635 }
636
637 const char *ast_state2str(enum ast_channel_state state)
638 {
639 char *buf;
640
641 switch (state) {
642 case AST_STATE_DOWN:
643 return "Down";
644 case AST_STATE_RESERVED:
645 return "Rsrvd";
646 case AST_STATE_OFFHOOK:
647 return "OffHook";
648 case AST_STATE_DIALING:
649 return "Dialing";
650 case AST_STATE_RING:
651 return "Ring";
652 case AST_STATE_RINGING:
653 return "Ringing";
654 case AST_STATE_UP:
655 return "Up";
656 case AST_STATE_BUSY:
657 return "Busy";
658 case AST_STATE_DIALING_OFFHOOK:
659 return "Dialing Offhook";
660 case AST_STATE_PRERING:
661 return "Pre-ring";
662 case AST_STATE_MUTE:
663 return "Mute";
664 default:
665 if (!(buf = ast_threadstorage_get(&state2str_threadbuf, STATE2STR_BUFSIZE)))
666 return "Unknown";
667 snprintf(buf, STATE2STR_BUFSIZE, "Unknown (%u)", state);
668 return buf;
669 }
670 }
671
672 /*! \brief Gives the string form of a given transfer capability */
673 char *ast_transfercapability2str(int transfercapability)
674 {
675 switch (transfercapability) {
676 case AST_TRANS_CAP_SPEECH:
677 return "SPEECH";
678 case AST_TRANS_CAP_DIGITAL:
679 return "DIGITAL";
680 case AST_TRANS_CAP_RESTRICTED_DIGITAL:
681 return "RESTRICTED_DIGITAL";
682 case AST_TRANS_CAP_3_1K_AUDIO:
683 return "3K1AUDIO";
684 case AST_TRANS_CAP_DIGITAL_W_TONES:
685 return "DIGITAL_W_TONES";
686 case AST_TRANS_CAP_VIDEO:
687 return "VIDEO";
688 default:
689 return "UNKNOWN";
690 }
691 }
692
693 /*! \brief Channel technology used to extract a channel from a running application. The
694 * channel created with this technology will be immediately hung up - most external
695 * applications won't ever want to see this.
696 */
697 static const struct ast_channel_tech surrogate_tech = {
698 .type = "Surrogate",
699 .description = "Surrogate channel used to pull channel from an application",
700 .properties = AST_CHAN_TP_INTERNAL,
701 };
702
703 static const struct ast_channel_tech null_tech = {
704 .type = "NULL",
705 .description = "Null channel (should not see this)",
706 };
707
708 static void ast_channel_destructor(void *obj);
709 static void ast_dummy_channel_destructor(void *obj);
710
711 static int do_ids_conflict(const struct ast_assigned_ids *assignedids, int rdlock)
712 {
713 struct ast_channel *conflict;
714
715 if (!assignedids) {
716 return 0;
717 }
718
719 if (!ast_strlen_zero(assignedids->uniqueid)) {
720 conflict = CHANNELSTORAGE_API(current_channel_storage_instance,
721 get_by_uniqueid, assignedids->uniqueid, rdlock);
722 if (conflict) {
723 ast_log(LOG_ERROR, "Channel Unique ID '%s' already in use by channel %s(%p)\n",
724 assignedids->uniqueid, ast_channel_name(conflict), conflict);
725 ast_channel_unref(conflict);
726 return 1;
727 }
728 }
729
730 if (!ast_strlen_zero(assignedids->uniqueid2)) {
731 conflict = CHANNELSTORAGE_API(current_channel_storage_instance,
732 get_by_uniqueid, assignedids->uniqueid2, rdlock);
733 if (conflict) {
734 ast_log(LOG_ERROR, "Channel Unique ID2 '%s' already in use by channel %s(%p)\n",
735 assignedids->uniqueid2, ast_channel_name(conflict), conflict);
736 ast_channel_unref(conflict);
737 return 1;
738 }
739 }
740
741 return 0;
742 }
743
744 /*! \brief Create a new channel structure */
745 static struct ast_channel *__attribute__((format(printf, 15, 0)))
746 __ast_channel_alloc_ap(int needqueue, int state, const char *cid_num, const char *cid_name,
747 const char *acctcode, const char *exten, const char *context, const struct ast_assigned_ids *assignedids,
748 const struct ast_channel *requestor, enum ama_flags amaflag, struct ast_endpoint *endpoint,
749 struct ast_channel_initializers *initializers, const char *file, int line,
750 const char *function, const char *name_fmt, va_list ap)
751 {
752 struct ast_channel *tmp;
753 struct varshead *headp;
754 char *tech = "", *tech2 = NULL;
755 struct ast_format_cap *nativeformats;
756 struct ast_sched_context *schedctx;
757 struct ast_timer *timer;
758 struct timeval now;
759 const struct ast_channel_tech *channel_tech;
760 struct ast_stream_topology *topology;
761
762 /* If shutting down, don't allocate any new channels */
763 if (ast_shutting_down()) {
764 ast_log(LOG_WARNING, "Channel allocation failed: Refusing due to active shutdown\n");
765 return NULL;
766 }
767
768 tmp = __ast_channel_internal_alloc_with_initializers(ast_channel_destructor, assignedids, requestor, initializers,
769 file, line, function);
770 if (!tmp) {
771 /* Channel structure allocation failure. */
772 return NULL;
773 }
774
775 ast_channel_stage_snapshot(tmp);
776
777 /*
778 * Init file descriptors to unopened state so
779 * the destructor can know not to close them.
780 */
781 ast_channel_timingfd_set(tmp, -1);
782 ast_channel_internal_alertpipe_clear(tmp);
783 ast_channel_internal_fd_clear_all(tmp);
784
785 nativeformats = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
786 if (!nativeformats) {
787 /*
788 * Aborting the channel creation. We do not need to complete staging
789 * the channel snapshot because the channel has not been finalized or
790 * linked into the channels container yet. Nobody else knows about
791 * this channel nor will anybody ever know about it.
792 */
793 return ast_channel_unref(tmp);
794 }
795 ast_format_cap_append(nativeformats, ast_format_none, 0);
796 ast_channel_nativeformats_set(tmp, nativeformats);
797 ao2_ref(nativeformats, -1);
798
799 ast_channel_set_rawwriteformat(tmp, ast_format_none);
800 ast_channel_set_rawreadformat(tmp, ast_format_none);
801 ast_channel_set_writeformat(tmp, ast_format_none);
802 ast_channel_set_readformat(tmp, ast_format_none);
803
804 if (!(schedctx = ast_sched_context_create())) {
805 ast_log(LOG_WARNING, "Channel allocation failed: Unable to create schedule context\n");
806 /* See earlier channel creation abort comment above. */
807 return ast_channel_unref(tmp);
808 }
809 ast_channel_sched_set(tmp, schedctx);
810
811 ast_party_dialed_init(ast_channel_dialed(tmp));
812 ast_party_caller_init(ast_channel_caller(tmp));
813 ast_party_connected_line_init(ast_channel_connected(tmp));
814 ast_party_connected_line_init(ast_channel_connected_indicated(tmp));
815 ast_party_redirecting_init(ast_channel_redirecting(tmp));
816
817 if (cid_name) {
818 ast_channel_caller(tmp)->id.name.valid = 1;
819 ast_channel_caller(tmp)->id.name.str = ast_strdup(cid_name);
820 if (!ast_channel_caller(tmp)->id.name.str) {
821 /* See earlier channel creation abort comment above. */
822 return ast_channel_unref(tmp);
823 }
824 }
825 if (cid_num) {
826 ast_channel_caller(tmp)->id.number.valid = 1;
827 ast_channel_caller(tmp)->id.number.str = ast_strdup(cid_num);
828 if (!ast_channel_caller(tmp)->id.number.str) {
829 /* See earlier channel creation abort comment above. */
830 return ast_channel_unref(tmp);
831 }
832 }
833
834 if ((timer = ast_timer_open())) {
835 ast_channel_timer_set(tmp, timer);
836 if (strcmp(ast_timer_get_name(ast_channel_timer(tmp)), "timerfd")) {
837 needqueue = 0;
838 }
839 ast_channel_timingfd_set(tmp, ast_timer_fd(ast_channel_timer(tmp)));
840 }
841
842 if (needqueue && ast_channel_internal_alertpipe_init(tmp)) {
843 /* See earlier channel creation abort comment above. */
844 return ast_channel_unref(tmp);
845 }
846
847 if (!(topology = ast_stream_topology_alloc())) {
848 return ast_channel_unref(tmp);
849 }
850 ast_channel_internal_set_stream_topology(tmp, topology);
851
852 /* Always watch the alertpipe */
853 ast_channel_set_fd(tmp, AST_ALERT_FD, ast_channel_internal_alert_readfd(tmp));
854 /* And timing pipe */
855 ast_channel_set_fd(tmp, AST_TIMING_FD, ast_channel_timingfd(tmp));
856
857 /* Initial state */
858 ast_channel_state_set(tmp, state);
859 ast_channel_hold_state_set(tmp, AST_CONTROL_UNHOLD);
860
861 ast_channel_streamid_set(tmp, -1);
862 ast_channel_vstreamid_set(tmp, -1);
863
864 ast_channel_fin_set(tmp, global_fin);
865 ast_channel_fout_set(tmp, global_fout);
866
867 now = ast_tvnow();
868 ast_channel_creationtime_set(tmp, &now);
869
870 ast_channel_internal_setup_topics(tmp);
871
872 if (!ast_strlen_zero(name_fmt)) {
873 char *slash, *slash2;
874 /* Almost every channel is calling this function, and setting the name via the ast_string_field_build() call.
875 * And they all use slightly different formats for their name string.
876 * This means, to set the name here, we have to accept variable args, and call the string_field_build from here.
877 * This means, that the stringfields must have a routine that takes the va_lists directly, and
878 * uses them to build the string, instead of forming the va_lists internally from the vararg ... list.
879 * This new function was written so this can be accomplished.
880 */
881 ast_channel_name_build_va(tmp, name_fmt, ap);
882 tech = ast_strdupa(ast_channel_name(tmp));
883 if ((slash = strchr(tech, '/'))) {
884 if ((slash2 = strchr(slash + 1, '/'))) {
885 tech2 = slash + 1;
886 *slash2 = '\0';
887 }
888 *slash = '\0';
889 }
890 } else {
891 /*
892 * Start the string with '-' so it becomes an empty string
893 * in the destructor.
894 */
895 ast_channel_name_set(tmp, "-**Unknown**");
896 }
897
898 if (amaflag != AST_AMA_NONE) {
899 ast_channel_amaflags_set(tmp, amaflag);
900 } else {
901 ast_channel_amaflags_set(tmp, DEFAULT_AMA_FLAGS);
902 }
903
904 if (!ast_strlen_zero(acctcode)) {
905 ast_channel_accountcode_set(tmp, acctcode);
906 }
907 ast_channel_language_set(tmp, ast_defaultlanguage);
908
909 ast_channel_context_set(tmp, S_OR(context, "default"));
910 ast_channel_exten_set(tmp, S_OR(exten, "s"));
911 ast_channel_priority_set(tmp, 1);
912
913 headp = ast_channel_varshead(tmp);
914 AST_LIST_HEAD_INIT_NOLOCK(headp);
915
916 ast_pbx_hangup_handler_init(tmp);
917 AST_LIST_HEAD_INIT_NOLOCK(ast_channel_datastores(tmp));
918 AST_LIST_HEAD_INIT_NOLOCK(ast_channel_autochans(tmp));
919
920 channel_tech = ast_get_channel_tech(tech);
921 if (!channel_tech && !ast_strlen_zero(tech2)) {
922 channel_tech = ast_get_channel_tech(tech2);
923 }
924 if (channel_tech) {
925 ast_channel_tech_set(tmp, channel_tech);
926 } else {
927 ast_channel_tech_set(tmp, &null_tech);
928 }
929
930 /* You might scream "locking inversion" at seeing this but it is actually perfectly fine.
931 * Since the channel was just created nothing can know about it yet or even acquire it.
932 */
933 ast_channel_lock(tmp);
934
935 CHANNELSTORAGE_API(current_channel_storage_instance, wrlock);
936
937 if (do_ids_conflict(assignedids, 0)) {
938 ast_channel_internal_errno_set(AST_CHANNEL_ERROR_ID_EXISTS);
939 ast_channel_unlock(tmp);
940 CHANNELSTORAGE_API(current_channel_storage_instance, unlock);
941 /* See earlier channel creation abort comment above. */
942 return ast_channel_unref(tmp);
943 }
944
945 /* Finalize and link into the channels container. */
946 ast_channel_internal_finalize(tmp);
947 ast_atomic_fetchadd_int(&chancount, +1);
948 CHANNELSTORAGE_API(current_channel_storage_instance, insert, tmp, OBJ_NOLOCK, 0);
949 CHANNELSTORAGE_API(current_channel_storage_instance, unlock);
950
951
952 if (endpoint) {
953 ast_channel_endpoint_set(tmp, endpoint);
954 }
955
956 /*
957 * And now, since the channel structure is built, and has its name, let
958 * the world know of its existence
959 */
960 ast_channel_stage_snapshot_done(tmp);
961
962 ast_debug(1, "Channel %p '%s' allocated\n", tmp, ast_channel_name(tmp));
963
964 return tmp;
965 }
966
967 struct ast_channel *__ast_channel_alloc(int needqueue, int state, const char *cid_num,
968 const char *cid_name, const char *acctcode,
969 const char *exten, const char *context, const struct ast_assigned_ids *assignedids,
970 const struct ast_channel *requestor, enum ama_flags amaflag,
971 struct ast_endpoint *endpoint,
972 const char *file, int line, const char *function,
973 const char *name_fmt, ...)
974 {
975 va_list ap;
976 struct ast_channel *result;
977
978 va_start(ap, name_fmt);
979 result = __ast_channel_alloc_ap(needqueue, state, cid_num, cid_name, acctcode, exten, context,
980 assignedids, requestor, amaflag, endpoint, NULL, file, line, function, name_fmt, ap);
981 va_end(ap);
982
983 return result;
984 }
985
986 struct ast_channel *__ast_channel_alloc_with_initializers(int needqueue, int state, const char *cid_num,
987 const char *cid_name, const char *acctcode,
988 const char *exten, const char *context, const struct ast_assigned_ids *assignedids,
989 const struct ast_channel *requestor, enum ama_flags amaflag,
990 struct ast_endpoint *endpoint, struct ast_channel_initializers *initializers,
991 const char *file, int line, const char *function,
992 const char *name_fmt, ...)
993 {
994 va_list ap;
995 struct ast_channel *result;
996
997 va_start(ap, name_fmt);
998 result = __ast_channel_alloc_ap(needqueue, state, cid_num, cid_name, acctcode, exten, context,
999 assignedids, requestor, amaflag, endpoint, initializers, file, line, function, name_fmt, ap);
1000 va_end(ap);
1001
1002 return result;
1003 }
1004
1005 /* only do the minimum amount of work needed here to make a channel
1006 * structure that can be used to expand channel vars */
1007 struct ast_channel *__ast_dummy_channel_alloc(const char *file, int line, const char *function)
1008 {
1009 struct ast_channel *tmp;
1010 struct varshead *headp;
1011
1012 tmp = __ast_channel_internal_alloc(ast_dummy_channel_destructor, NULL, NULL,
1013 file, line, function);
1014 if (!tmp) {
1015 /* Dummy channel structure allocation failure. */
1016 return NULL;
1017 }
1018
1019 ast_pbx_hangup_handler_init(tmp);
1020 AST_LIST_HEAD_INIT_NOLOCK(ast_channel_datastores(tmp));
1021
1022 /*
1023 * Init file descriptors to unopened state just in case
1024 * autoservice is called on the channel or something tries to
1025 * read a frame from it.
1026 */
1027 ast_channel_timingfd_set(tmp, -1);
1028 ast_channel_internal_alertpipe_clear(tmp);
1029 ast_channel_internal_fd_clear_all(tmp);
1030
1031 ast_channel_hold_state_set(tmp, AST_CONTROL_UNHOLD);
1032
1033 ast_channel_internal_setup_topics(tmp);
1034
1035 headp = ast_channel_varshead(tmp);
1036 AST_LIST_HEAD_INIT_NOLOCK(headp);
1037
1038 return tmp;
1039 }
1040
1041 static int __ast_queue_frame(struct ast_channel *chan, struct ast_frame *fin, int head, struct ast_frame *after)
1042 {
1043 struct ast_frame *f;
1044 struct ast_frame *cur;
1045 unsigned int new_frames = 0;
1046 unsigned int new_voice_frames = 0;
1047 unsigned int queued_frames = 0;
1048 unsigned int queued_voice_frames = 0;
1049 AST_LIST_HEAD_NOLOCK(,ast_frame) frames;
1050
1051 ast_channel_lock(chan);
1052
1053 /*
1054 * Check the last frame on the queue if we are queuing the new
1055 * frames after it.
1056 */
1057 cur = AST_LIST_LAST(ast_channel_readq(chan));
1058 if (cur && cur->frametype == AST_FRAME_CONTROL && !head && (!after || after == cur)) {
1059 switch (cur->subclass.integer) {
1060 case AST_CONTROL_END_OF_Q:
1061 if (fin->frametype == AST_FRAME_CONTROL
1062 && fin->subclass.integer == AST_CONTROL_HANGUP) {
1063 /*
1064 * Destroy the end-of-Q marker frame so we can queue the hangup
1065 * frame in its place.
1066 */
1067 AST_LIST_REMOVE(ast_channel_readq(chan), cur, frame_list);
1068 ast_frfree(cur);
1069
1070 /*
1071 * This has degenerated to a normal queue append anyway. Since
1072 * we just destroyed the last frame in the queue we must make
1073 * sure that "after" is NULL or bad things will happen.
1074 */
1075 after = NULL;
1076 break;
1077 }
1078 /* Fall through */
1079 case AST_CONTROL_HANGUP:
1080 /* Don't queue anything. */
1081 ast_channel_unlock(chan);
1082 return 0;
1083 default:
1084 break;
1085 }
1086 }
1087
1088 /* Build copies of all the new frames and count them */
1089 AST_LIST_HEAD_INIT_NOLOCK(&frames);
1090 for (cur = fin; cur; cur = AST_LIST_NEXT(cur, frame_list)) {
1091 if (!(f = ast_frdup(cur))) {
1092 if (AST_LIST_FIRST(&frames)) {
1093 ast_frfree(AST_LIST_FIRST(&frames));
1094 }
1095 ast_channel_unlock(chan);
1096 return -1;
1097 }
1098
1099 AST_LIST_INSERT_TAIL(&frames, f, frame_list);
1100 new_frames++;
1101 if (f->frametype == AST_FRAME_VOICE) {
1102 new_voice_frames++;
1103 }
1104 }
1105
1106 /* Count how many frames exist on the queue */
1107 AST_LIST_TRAVERSE(ast_channel_readq(chan), cur, frame_list) {
1108 queued_frames++;
1109 if (cur->frametype == AST_FRAME_VOICE) {
1110 queued_voice_frames++;
1111 }
1112 }
1113
1114 if ((queued_frames + new_frames > 128 || queued_voice_frames + new_voice_frames > 96)) {
1115 int total_queued = queued_frames + new_frames;
1116 int total_voice = queued_voice_frames + new_voice_frames;
1117 int count = 0;
1118 ast_log(LOG_WARNING, "Exceptionally long %squeue length (%d voice / %d total) queuing to %s\n",
1119 queued_frames + new_frames > 128 ? "" : "voice ", total_voice, total_queued, ast_channel_name(chan));
1120 AST_LIST_TRAVERSE_SAFE_BEGIN(ast_channel_readq(chan), cur, frame_list) {
1121 /* Save the most recent frame */
1122 if (!AST_LIST_NEXT(cur, frame_list)) {
1123 break;
1124 } else if (cur->frametype == AST_FRAME_VOICE || cur->frametype == AST_FRAME_VIDEO || cur->frametype == AST_FRAME_NULL) {
1125 if (++count > 64) {
1126 break;
1127 }
1128 AST_LIST_REMOVE_CURRENT(frame_list);
1129 ast_frfree(cur);
1130
1131 /* Read from the alert pipe for each flushed frame. */
1132 ast_channel_internal_alert_read(chan);
1133 }
1134 }
1135 AST_LIST_TRAVERSE_SAFE_END;
1136 if (count) {
1137 ast_debug(4, "Discarded %d frame%s due to queue overload on %s\n", count, ESS(count), ast_channel_name(chan));
1138 }
1139 }
1140
1141 if (after) {
1142 AST_LIST_INSERT_LIST_AFTER(ast_channel_readq(chan), &frames, after, frame_list);
1143 } else {
1144 if (head) {
1145 AST_LIST_APPEND_LIST(&frames, ast_channel_readq(chan), frame_list);
1146 AST_LIST_HEAD_INIT_NOLOCK(ast_channel_readq(chan));
1147 }
1148 AST_LIST_APPEND_LIST(ast_channel_readq(chan), &frames, frame_list);
1149 }
1150
1151 if (ast_channel_alert_writable(chan)) {
1152 /* Write to the alert pipe for each added frame */
1153 while (new_frames--) {
1154 if (ast_channel_alert_write(chan)) {
1155 ast_log(LOG_WARNING, "Unable to write to alert pipe on %s (qlen = %u): %s!\n",
1156 ast_channel_name(chan), queued_frames, strerror(errno));
1157 break;
1158 }
1159 }
1160 } else if (ast_channel_timingfd(chan) > -1) {
1161 ast_timer_enable_continuous(ast_channel_timer(chan));
1162 } else if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_BLOCKING)) {
1163 pthread_kill(ast_channel_blocker(chan), SIGURG);
1164 }
1165
1166 ast_channel_unlock(chan);
1167
1168 return 0;
1169 }
1170
1171 int ast_queue_frame(struct ast_channel *chan, struct ast_frame *fin)
1172 {
1173 return __ast_queue_frame(chan, fin, 0, NULL);
1174 }
1175
1176 int ast_queue_frame_head(struct ast_channel *chan, struct ast_frame *fin)
1177 {
1178 return __ast_queue_frame(chan, fin, 1, NULL);
1179 }
1180
1181 /*! \brief Queue a hangup frame for channel */
1182 int ast_queue_hangup(struct ast_channel *chan)
1183 {
1184 RAII_VAR(struct ast_json *, blob, ast_json_object_create(), ast_json_unref);
1185 struct ast_frame f = { AST_FRAME_CONTROL, .subclass.integer = AST_CONTROL_HANGUP };
1186 int res, cause, tech_cause;
1187
1188 if (!blob) {
1189 return -1;
1190 }
1191
1192 /* Yeah, let's not change a lock-critical value without locking */
1193 ast_channel_lock(chan);
1194 ast_channel_softhangup_internal_flag_add(chan, AST_SOFTHANGUP_DEV);
1195
1196 cause = ast_channel_hangupcause(chan);
1197 if (cause) {
1198 ast_json_object_set(blob, "cause", ast_json_integer_create(cause));
1199 }
1200 tech_cause = ast_channel_tech_hangupcause(chan);
1201 if (tech_cause) {
1202 ast_json_object_set(blob, "tech_cause", ast_json_integer_create(tech_cause));
1203 }
1204
1205 ast_channel_publish_blob(chan, ast_channel_hangup_request_type(), blob);
1206
1207 res = ast_queue_frame(chan, &f);
1208 ast_channel_unlock(chan);
1209 return res;
1210 }
1211
1212 /*! \brief Queue a hangup frame for channel */
1213 int ast_queue_hangup_with_cause(struct ast_channel *chan, int cause)
1214 {
1215 RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
1216 struct ast_frame f = { AST_FRAME_CONTROL, .subclass.integer = AST_CONTROL_HANGUP };
1217 int res;
1218 int tech_cause = 0;
1219
1220 if (cause >= 0) {
1221 f.data.uint32 = cause;
1222 }
1223
1224 /* Yeah, let's not change a lock-critical value without locking */
1225 ast_channel_lock(chan);
1226 ast_channel_softhangup_internal_flag_add(chan, AST_SOFTHANGUP_DEV);
1227 if (cause < 0) {
1228 f.data.uint32 = ast_channel_hangupcause(chan);
1229 }
1230 blob = ast_json_pack("{s: i}",
1231 "cause", cause);
1232 if (!blob) {
1233 ast_channel_unlock(chan);
1234 return -1;
1235 }
1236
1237 tech_cause = ast_channel_tech_hangupcause(chan);
1238 if (tech_cause) {
1239 ast_json_object_set(blob, "tech_cause", ast_json_integer_create(tech_cause));
1240 }
1241
1242 ast_channel_publish_blob(chan, ast_channel_hangup_request_type(), blob);
1243
1244 res = ast_queue_frame(chan, &f);
1245 ast_channel_unlock(chan);
1246 return res;
1247 }
1248
1249 int ast_queue_hold(struct ast_channel *chan, const char *musicclass)
1250 {
1251 struct ast_frame f = { AST_FRAME_CONTROL, .subclass.integer = AST_CONTROL_HOLD };
1252 struct ast_json *blob = NULL;
1253 int res;
1254
1255 if (!ast_strlen_zero(musicclass)) {
1256 f.data.ptr = (void *) musicclass;
1257 f.datalen = strlen(musicclass) + 1;
1258
1259 blob = ast_json_pack("{s: s}",
1260 "musicclass", musicclass);
1261 }
1262
1263 ast_channel_lock(chan);
1264 ast_channel_publish_blob(chan, ast_channel_hold_type(), blob);
1265 ast_channel_unlock(chan);
1266
1267 res = ast_queue_frame(chan, &f);
1268
1269 ast_json_unref(blob);
1270
1271 return res;
1272 }
1273
1274 int ast_queue_unhold(struct ast_channel *chan)
1275 {
1276 struct ast_frame f = { AST_FRAME_CONTROL, .subclass.integer = AST_CONTROL_UNHOLD };
1277 int res;
1278
1279 ast_channel_lock(chan);
1280 ast_channel_publish_blob(chan, ast_channel_unhold_type(), NULL);
1281 ast_channel_unlock(chan);
1282
1283 res = ast_queue_frame(chan, &f);
1284
1285 return res;
1286 }
1287
1288 /*! \brief Queue a control frame */
1289 int ast_queue_control(struct ast_channel *chan, enum ast_control_frame_type control)
1290 {
1291 struct ast_frame f = { AST_FRAME_CONTROL, .subclass.integer = control };
1292 return ast_queue_frame(chan, &f);
1293 }
1294
1295 /*! \brief Queue a control frame with payload */
1296 int ast_queue_control_data(struct ast_channel *chan, enum ast_control_frame_type control,
1297 const void *data, size_t datalen)
1298 {
1299 struct ast_frame f = { AST_FRAME_CONTROL, .subclass.integer = control, .data.ptr = (void *) data, .datalen = datalen };
1300 return ast_queue_frame(chan, &f);
1301 }
1302
1303 /*! \brief Queue an ANSWER control frame with topology */
1304 int ast_queue_answer(struct ast_channel *chan, const struct ast_stream_topology *topology)
1305 {
1306 struct ast_frame f = {
1307 AST_FRAME_CONTROL,
1308 .subclass.integer = AST_CONTROL_ANSWER,
1309 .subclass.topology = (struct ast_stream_topology *)topology,
1310 };
1311 return ast_queue_frame(chan, &f);
1312 }
1313
1314 /*! \brief Set defer DTMF flag on channel */
1315 int ast_channel_defer_dtmf(struct ast_channel *chan)
1316 {
1317 int pre = 0;
1318
1319 if (chan) {
1320 ast_channel_lock(chan);
1321 pre = ast_test_flag(ast_channel_flags(chan), AST_FLAG_DEFER_DTMF);
1322 ast_set_flag(ast_channel_flags(chan), AST_FLAG_DEFER_DTMF);
1323 ast_channel_unlock(chan);
1324 }
1325 return pre;
1326 }
1327
1328 /*! \brief Unset defer DTMF flag on channel */
1329 void ast_channel_undefer_dtmf(struct ast_channel *chan)
1330 {
1331 if (chan) {
1332 ast_channel_clear_flag(chan, AST_FLAG_DEFER_DTMF);
1333 }
1334 }
1335
1336 struct ast_channel *ast_channel_callback(
1337 ao2_callback_data_fn *cb_fn, void *arg, void *data, int ao2_flags)
1338 {
1339 if (!current_channel_storage_instance) {
1340 return NULL;
1341 }
1342 if (!cb_fn) {
1343 ast_log(LOG_ERROR, "callback function must be provided\n");
1344 return NULL;
1345 }
1346 return CHANNELSTORAGE_API(current_channel_storage_instance, callback, cb_fn, arg, data, ao2_flags, 1);
1347 }
1348
1349 struct ast_channel_iterator *ast_channel_iterator_destroy(struct ast_channel_iterator *i)
1350 {
1351 if (!current_channel_storage_instance || !i) {
1352 return NULL;
1353 }
1354 return CHANNELSTORAGE_API(current_channel_storage_instance, iterator_destroy, i);
1355 }
1356
1357 struct ast_channel_iterator *ast_channel_iterator_by_exten_new(const char *exten, const char *context)
1358 {
1359 if (!current_channel_storage_instance) {
1360 return NULL;
1361 }
1362 if (ast_strlen_zero(exten) || ast_strlen_zero(context)) {
1363 ast_log(LOG_ERROR, "exten and context must be provided\n");
1364 return NULL;
1365 }
1366 return CHANNELSTORAGE_API(current_channel_storage_instance, iterator_by_exten_new, exten, context);
1367 }
1368 struct ast_channel_iterator *ast_channel_iterator_by_name_new(const char *name, size_t name_len)
1369 {
1370 if (!current_channel_storage_instance) {
1371 return NULL;
1372 }
1373 if (ast_strlen_zero(name)) {
1374 ast_log(LOG_ERROR, "name must be provided\n");
1375 return NULL;
1376 }
1377 return CHANNELSTORAGE_API(current_channel_storage_instance, iterator_by_name_new, name, name_len);
1378 }
1379
1380 struct ast_channel_iterator *ast_channel_iterator_all_new(void)
1381 {
1382 if (!current_channel_storage_instance) {
1383 return NULL;
1384 }
1385 return CHANNELSTORAGE_API(current_channel_storage_instance, iterator_all_new);
1386 }
1387
1388 struct ast_channel *ast_channel_iterator_next(struct ast_channel_iterator *i)
1389 {
1390 if (!current_channel_storage_instance || !i) {
1391 return NULL;
1392 }
1393 return CHANNELSTORAGE_API(current_channel_storage_instance, iterator_next, i);
1394 }
1395
1396 /*
1397 * REMINDER: Historically, this function can be provided a channel name
1398 * or uniqueid. This is a bit confusing, but it is what it is.
1399 */
1400 struct ast_channel *ast_channel_get_by_name_prefix(const char *name, size_t name_len)
1401 {
1402 if (!current_channel_storage_instance) {
1403 return NULL;
1404 }
1405 if (ast_strlen_zero(name)) {
1406 ast_log(LOG_ERROR, "name must be provided\n");
1407 return NULL;
1408 }
1409
1410 return CHANNELSTORAGE_API(current_channel_storage_instance, get_by_name_prefix_or_uniqueid, name, name_len, 1);
1411 }
1412
1413 /*
1414 * REMINDER: Historically, this function can be provided a channel name
1415 * or uniqueid. This is a bit confusing, but it is what it is.
1416 */
1417 struct ast_channel *ast_channel_get_by_name(const char *name)
1418 {
1419 if (!current_channel_storage_instance) {
1420 return NULL;
1421 }
1422 if (ast_strlen_zero(name)) {
1423 ast_log(LOG_ERROR, "name must be provided\n");
1424 return NULL;
1425 }
1426
1427 return CHANNELSTORAGE_API(current_channel_storage_instance, get_by_name_prefix_or_uniqueid, name, 0, 1);
1428 }
1429
1430 struct ast_channel *ast_channel_get_by_exten(const char *exten, const char *context)
1431 {
1432 if (!current_channel_storage_instance) {
1433 return NULL;
1434 }
1435 if (ast_strlen_zero(exten) || ast_strlen_zero(context)) {
1436 ast_log(LOG_ERROR, "exten and context must be provided\n");
1437 return NULL;
1438 }
1439 return CHANNELSTORAGE_API(current_channel_storage_instance, get_by_exten, exten, context, 1);
1440 }
1441
1442 struct ast_channel *ast_channel_get_by_uniqueid(const char *uniqueid)
1443 {
1444 if (!current_channel_storage_instance) {
1445 return NULL;
1446 }
1447 if (ast_strlen_zero(uniqueid)) {
1448 ast_log(LOG_ERROR, "uniqueid must be provided\n");
1449 return NULL;
1450 }
1451 return CHANNELSTORAGE_API(current_channel_storage_instance, get_by_uniqueid, uniqueid, 1);
1452 }
1453
1454 int ast_is_deferrable_frame(const struct ast_frame *frame)
1455 {
1456 /* Do not add a default entry in this switch statement. Each new
1457 * frame type should be addressed directly as to whether it should
1458 * be queued up or not.
1459 */
1460 switch (frame->frametype) {
1461 case AST_FRAME_BRIDGE_ACTION:
1462 case AST_FRAME_BRIDGE_ACTION_SYNC:
1463 case AST_FRAME_CONTROL:
1464 case AST_FRAME_TEXT:
1465 case AST_FRAME_TEXT_DATA:
1466 case AST_FRAME_IMAGE:
1467 case AST_FRAME_HTML:
1468 return 1;
1469
1470 case AST_FRAME_DTMF_END:
1471 case AST_FRAME_DTMF_BEGIN:
1472 case AST_FRAME_VOICE:
1473 case AST_FRAME_VIDEO:
1474 case AST_FRAME_NULL:
1475 case AST_FRAME_IAX:
1476 case AST_FRAME_CNG:
1477 case AST_FRAME_MODEM:
1478 case AST_FRAME_RTCP:
1479 return 0;
1480 }
1481 return 0;
1482 }
1483
1484 /*! \brief Wait, look for hangups and condition arg */
1485 static int safe_sleep_conditional(struct ast_channel *chan, int timeout_ms, int (*cond)(void*), void *data, unsigned int generate_silence)
1486 {
1487 struct ast_frame *f;
1488 struct ast_silence_generator *silgen = NULL;
1489 int res = 0;
1490 struct timeval start;
1491 int ms;
1492 AST_LIST_HEAD_NOLOCK(, ast_frame) deferred_frames;
1493
1494 AST_LIST_HEAD_INIT_NOLOCK(&deferred_frames);
1495
1496 /* If no other generator is present, start silencegen while waiting */
1497 if (generate_silence && ast_opt_transmit_silence && !ast_channel_generatordata(chan)) {
1498 silgen = ast_channel_start_silence_generator(chan);
1499 }
1500
1501 start = ast_tvnow();
1502 while ((ms = ast_remaining_ms(start, timeout_ms))) {
1503 struct ast_frame *dup_f = NULL;
1504
1505 if (cond && ((*cond)(data) == 0)) {
1506 break;
1507 }
1508 ms = ast_waitfor(chan, ms);
1509 if (ms < 0) {
1510 res = -1;
1511 break;
1512 }
1513 if (ms > 0) {
1514 f = ast_read(chan);
1515 if (!f) {
1516 res = -1;
1517 break;
1518 }
1519
1520 if (!ast_is_deferrable_frame(f)) {
1521 ast_frfree(f);
1522 continue;
1523 }
1524
1525 if ((dup_f = ast_frisolate(f))) {
1526 if (dup_f != f) {
1527 ast_frfree(f);
1528 }
1529 AST_LIST_INSERT_HEAD(&deferred_frames, dup_f, frame_list);
1530 }
1531 }
1532 }
1533
1534 /* stop silgen if present */
1535 if (silgen) {
1536 ast_channel_stop_silence_generator(chan, silgen);
1537 }
1538
1539 /* We need to free all the deferred frames, but we only need to
1540 * queue the deferred frames if there was no error and no
1541 * hangup was received
1542 */
1543 ast_channel_lock(chan);
1544 while ((f = AST_LIST_REMOVE_HEAD(&deferred_frames, frame_list))) {
1545 if (!res) {
1546 ast_queue_frame_head(chan, f);
1547 }
1548 ast_frfree(f);
1549 }
1550 ast_channel_unlock(chan);
1551
1552 return res;
1553 }
1554
1555 int ast_safe_sleep_conditional(struct ast_channel *chan, int timeout_ms, int (*cond)(void*), void *data)
1556 {
1557 return safe_sleep_conditional(chan, timeout_ms, cond, data, 1);
1558 }
1559
1560 /*! \brief Wait, look for hangups */
1561 int ast_safe_sleep(struct ast_channel *chan, int ms)
1562 {
1563 return safe_sleep_conditional(chan, ms, NULL, NULL, 1);
1564 }
1565
1566 int ast_safe_sleep_without_silence(struct ast_channel *chan, int ms)
1567 {
1568 return safe_sleep_conditional(chan, ms, NULL, NULL, 0);
1569 }
1570
1571 struct ast_channel *ast_channel_release(struct ast_channel *chan)
1572 {
1573 /* Safe, even if already unlinked. */
1574 ast_channel_unlink(chan);
1575 return ast_channel_unref(chan);
1576 }
1577
1578 void ast_party_name_init(struct ast_party_name *init)
1579 {
1580 init->str = NULL;
1581 init->char_set = AST_PARTY_CHAR_SET_ISO8859_1;
1582 init->presentation = AST_PRES_ALLOWED_USER_NUMBER_NOT_SCREENED;
1583 init->valid = 0;
1584 }
1585
1586 void ast_party_name_copy(struct ast_party_name *dest, const struct ast_party_name *src)
1587 {
1588 if (dest == src) {
1589 /* Don't copy to self */
1590 return;
1591 }
1592
1593 ast_free(dest->str);
1594 dest->str = ast_strdup(src->str);
1595 dest->char_set = src->char_set;
1596 dest->presentation = src->presentation;
1597 dest->valid = src->valid;
1598 }
1599
1600 void ast_party_name_set_init(struct ast_party_name *init, const struct ast_party_name *guide)
1601 {
1602 init->str = NULL;
1603 init->char_set = guide->char_set;
1604 init->presentation = guide->presentation;
1605 init->valid = guide->valid;
1606 }
1607
1608 void ast_party_name_set(struct ast_party_name *dest, const struct ast_party_name *src)
1609 {
1610 if (dest == src) {
1611 /* Don't set to self */
1612 return;
1613 }
1614
1615 if (src->str && src->str != dest->str) {
1616 ast_free(dest->str);
1617 dest->str = ast_strdup(src->str);
1618 }
1619
1620 dest->char_set = src->char_set;
1621 dest->presentation = src->presentation;
1622 dest->valid = src->valid;
1623 }
1624
1625 void ast_party_name_free(struct ast_party_name *doomed)
1626 {
1627 ast_free(doomed->str);
1628 doomed->str = NULL;
1629 }
1630
1631 void ast_party_number_init(struct ast_party_number *init)
1632 {
1633 init->str = NULL;
1634 init->plan = 0;/* Unknown */
1635 init->presentation = AST_PRES_ALLOWED_USER_NUMBER_NOT_SCREENED;
1636 init->valid = 0;
1637 }
1638
1639 void ast_party_number_copy(struct ast_party_number *dest, const struct ast_party_number *src)
1640 {
1641 if (dest == src) {
1642 /* Don't copy to self */
1643 return;
1644 }
1645
1646 ast_free(dest->str);
1647 dest->str = ast_strdup(src->str);
1648 dest->plan = src->plan;
1649 dest->presentation = src->presentation;
1650 dest->valid = src->valid;
1651 }
1652
1653 void ast_party_number_set_init(struct ast_party_number *init, const struct ast_party_number *guide)
1654 {
1655 init->str = NULL;
1656 init->plan = guide->plan;
1657 init->presentation = guide->presentation;
1658 init->valid = guide->valid;
1659 }
1660
1661 void ast_party_number_set(struct ast_party_number *dest, const struct ast_party_number *src)
1662 {
1663 if (dest == src) {
1664 /* Don't set to self */
1665 return;
1666 }
1667
1668 if (src->str && src->str != dest->str) {
1669 ast_free(dest->str);
1670 dest->str = ast_strdup(src->str);
1671 }
1672
1673 dest->plan = src->plan;
1674 dest->presentation = src->presentation;
1675 dest->valid = src->valid;
1676 }
1677
1678 void ast_party_number_free(struct ast_party_number *doomed)
1679 {
1680 ast_free(doomed->str);
1681 doomed->str = NULL;
1682 }
1683
1684 void ast_party_subaddress_init(struct ast_party_subaddress *init)
1685 {
1686 init->str = NULL;
1687 init->type = 0;
1688 init->odd_even_indicator = 0;
1689 init->valid = 0;
1690 }
1691
1692 void ast_party_subaddress_copy(struct ast_party_subaddress *dest, const struct ast_party_subaddress *src)
1693 {
1694 if (dest == src) {
1695 /* Don't copy to self */
1696 return;
1697 }
1698
1699 ast_free(dest->str);
1700 dest->str = ast_strdup(src->str);
1701 dest->type = src->type;
1702 dest->odd_even_indicator = src->odd_even_indicator;
1703 dest->valid = src->valid;
1704 }
1705
1706 void ast_party_subaddress_set_init(struct ast_party_subaddress *init, const struct ast_party_subaddress *guide)
1707 {
1708 init->str = NULL;
1709 init->type = guide->type;
1710 init->odd_even_indicator = guide->odd_even_indicator;
1711 init->valid = guide->valid;
1712 }
1713
1714 void ast_party_subaddress_set(struct ast_party_subaddress *dest, const struct ast_party_subaddress *src)
1715 {
1716 if (dest == src) {
1717 /* Don't set to self */
1718 return;
1719 }
1720
1721 if (src->str && src->str != dest->str) {
1722 ast_free(dest->str);
1723 dest->str = ast_strdup(src->str);
1724 }
1725
1726 dest->type = src->type;
1727 dest->odd_even_indicator = src->odd_even_indicator;
1728 dest->valid = src->valid;
1729 }
1730
1731 void ast_party_subaddress_free(struct ast_party_subaddress *doomed)
1732 {
1733 ast_free(doomed->str);
1734 doomed->str = NULL;
1735 }
1736
1737 void ast_set_party_id_all(struct ast_set_party_id *update_id)
1738 {
1739 update_id->name = 1;
1740 update_id->number = 1;
1741 update_id->subaddress = 1;
1742 }
1743
1744 void ast_party_id_init(struct ast_party_id *init)
1745 {
1746 ast_party_name_init(&init->name);
1747 ast_party_number_init(&init->number);
1748 ast_party_subaddress_init(&init->subaddress);
1749 init->tag = NULL;
1750 }
1751
1752 void ast_party_id_copy(struct ast_party_id *dest, const struct ast_party_id *src)
1753 {
1754 if (dest == src) {
1755 /* Don't copy to self */
1756 return;
1757 }
1758
1759 ast_party_name_copy(&dest->name, &src->name);
1760 ast_party_number_copy(&dest->number, &src->number);
1761 ast_party_subaddress_copy(&dest->subaddress, &src->subaddress);
1762
1763 ast_free(dest->tag);
1764 dest->tag = ast_strdup(src->tag);
1765 }
1766
1767 void ast_party_id_set_init(struct ast_party_id *init, const struct ast_party_id *guide)
1768 {
1769 ast_party_name_set_init(&init->name, &guide->name);
1770 ast_party_number_set_init(&init->number, &guide->number);
1771 ast_party_subaddress_set_init(&init->subaddress, &guide->subaddress);
1772 init->tag = NULL;
1773 }
1774
1775 void ast_party_id_set(struct ast_party_id *dest, const struct ast_party_id *src, const struct ast_set_party_id *update)
1776 {
1777 if (dest == src) {
1778 /* Don't set to self */
1779 return;
1780 }
1781
1782 if (!update || update->name) {
1783 ast_party_name_set(&dest->name, &src->name);
1784 }
1785 if (!update || update->number) {
1786 ast_party_number_set(&dest->number, &src->number);
1787 }
1788 if (!update || update->subaddress) {
1789 ast_party_subaddress_set(&dest->subaddress, &src->subaddress);
1790 }
1791
1792 if (src->tag && src->tag != dest->tag) {
1793 ast_free(dest->tag);
1794 dest->tag = ast_strdup(src->tag);
1795 }
1796 }
1797
1798 void ast_party_id_free(struct ast_party_id *doomed)
1799 {
1800 ast_party_name_free(&doomed->name);
1801 ast_party_number_free(&doomed->number);
1802 ast_party_subaddress_free(&doomed->subaddress);
1803
1804 ast_free(doomed->tag);
1805 doomed->tag = NULL;
1806 }
1807
1808 int ast_party_id_presentation(const struct ast_party_id *id)
1809 {
1810 int number_priority;
1811 int number_value;
1812 int number_screening;
1813 int name_priority;
1814 int name_value;
1815
1816 /* Determine name presentation priority. */
1817 if (!id->name.valid) {
1818 name_value = AST_PRES_UNAVAILABLE;
1819 name_priority = 3;
1820 } else {
1821 name_value = id->name.presentation & AST_PRES_RESTRICTION;
1822 switch (name_value) {
1823 case AST_PRES_RESTRICTED:
1824 name_priority = 0;
1825 break;
1826 case AST_PRES_ALLOWED:
1827 name_priority = 1;
1828 break;
1829 case AST_PRES_UNAVAILABLE:
1830 name_priority = 2;
1831 break;
1832 default:
1833 name_value = AST_PRES_UNAVAILABLE;
1834 name_priority = 3;
1835 break;
1836 }
1837 }
1838
1839 /* Determine number presentation priority. */
1840 if (!id->number.valid) {
1841 number_screening = AST_PRES_USER_NUMBER_UNSCREENED;
1842 number_value = AST_PRES_UNAVAILABLE;
1843 number_priority = 3;
1844 } else {
1845 number_screening = id->number.presentation & AST_PRES_NUMBER_TYPE;
1846 number_value = id->number.presentation & AST_PRES_RESTRICTION;
1847 switch (number_value) {
1848 case AST_PRES_RESTRICTED:
1849 number_priority = 0;
1850 break;
1851 case AST_PRES_ALLOWED:
1852 number_priority = 1;
1853 break;
1854 case AST_PRES_UNAVAILABLE:
1855 number_priority = 2;
1856 break;
1857 default:
1858 number_screening = AST_PRES_USER_NUMBER_UNSCREENED;
1859 number_value = AST_PRES_UNAVAILABLE;
1860 number_priority = 3;
1861 break;
1862 }
1863 }
1864
1865 /* Select the wining presentation value. */
1866 if (name_priority < number_priority) {
1867 number_value = name_value;
1868 }
1869 if (number_value == AST_PRES_UNAVAILABLE) {
1870 return AST_PRES_NUMBER_NOT_AVAILABLE;
1871 }
1872
1873 return number_value | number_screening;
1874 }
1875
1876 void ast_party_id_invalidate(struct ast_party_id *id)
1877 {
1878 id->name.valid = 0;
1879 id->number.valid = 0;
1880 id->subaddress.valid = 0;
1881 }
1882
1883 void ast_party_id_reset(struct ast_party_id *id)
1884 {
1885 ast_party_id_free(id);
1886 ast_party_id_init(id);
1887 }
1888
1889 struct ast_party_id ast_party_id_merge(struct ast_party_id *base, struct ast_party_id *overlay)
1890 {
1891 struct ast_party_id merged;
1892
1893 merged = *base;
1894 if (overlay->name.valid) {
1895 merged.name = overlay->name;
1896 }
1897 if (overlay->number.valid) {
1898 merged.number = overlay->number;
1899 }
1900 if (overlay->subaddress.valid) {
1901 merged.subaddress = overlay->subaddress;
1902 }
1903 /* Note the actual structure is returned and not a pointer to it! */
1904 return merged;
1905 }
1906
1907 void ast_party_id_merge_copy(struct ast_party_id *dest, struct ast_party_id *base, struct ast_party_id *overlay)
1908 {
1909 struct ast_party_id merged;
1910
1911 merged = ast_party_id_merge(base, overlay);
1912 ast_party_id_copy(dest, &merged);
1913 }
1914
1915 void ast_party_dialed_init(struct ast_party_dialed *init)
1916 {
1917 init->number.str = NULL;
1918 init->number.plan = 0;/* Unknown */
1919 ast_party_subaddress_init(&init->subaddress);
1920 init->transit_network_select = 0;
1921 }
1922
1923 void ast_party_dialed_copy(struct ast_party_dialed *dest, const struct ast_party_dialed *src)
1924 {
1925 if (dest == src) {
1926 /* Don't copy to self */
1927 return;
1928 }
1929
1930 ast_free(dest->number.str);
1931 dest->number.str = ast_strdup(src->number.str);
1932 dest->number.plan = src->number.plan;
1933 ast_party_subaddress_copy(&dest->subaddress, &src->subaddress);
1934 dest->transit_network_select = src->transit_network_select;
1935 }
1936
1937 void ast_party_dialed_set_init(struct ast_party_dialed *init, const struct ast_party_dialed *guide)
1938 {
1939 init->number.str = NULL;
1940 init->number.plan = guide->number.plan;
1941 ast_party_subaddress_set_init(&init->subaddress, &guide->subaddress);
1942 init->transit_network_select = guide->transit_network_select;
1943 }
1944
1945 void ast_party_dialed_set(struct ast_party_dialed *dest, const struct ast_party_dialed *src)
1946 {
1947 if (src->number.str && src->number.str != dest->number.str) {
1948 ast_free(dest->number.str);
1949 dest->number.str = ast_strdup(src->number.str);
1950 }
1951 dest->number.plan = src->number.plan;
1952
1953 ast_party_subaddress_set(&dest->subaddress, &src->subaddress);
1954
1955 dest->transit_network_select = src->transit_network_select;
1956 }
1957
1958 void ast_party_dialed_free(struct ast_party_dialed *doomed)
1959 {
1960 ast_free(doomed->number.str);
1961 doomed->number.str = NULL;
1962 ast_party_subaddress_free(&doomed->subaddress);
1963 }
1964
1965 void ast_party_caller_init(struct ast_party_caller *init)
1966 {
1967 ast_party_id_init(&init->id);
1968 ast_party_id_init(&init->ani);
1969 ast_party_id_init(&init->priv);
1970 init->ani2 = 0;
1971 }
1972
1973 void ast_party_caller_copy(struct ast_party_caller *dest, const struct ast_party_caller *src)
1974 {
1975 if (dest == src) {
1976 /* Don't copy to self */
1977 return;
1978 }
1979
1980 ast_party_id_copy(&dest->id, &src->id);
1981 ast_party_id_copy(&dest->ani, &src->ani);
1982 ast_party_id_copy(&dest->priv, &src->priv);
1983 dest->ani2 = src->ani2;
1984 }
1985
1986 void ast_party_caller_set_init(struct ast_party_caller *init, const struct ast_party_caller *guide)
1987 {
1988 ast_party_id_set_init(&init->id, &guide->id);
1989 ast_party_id_set_init(&init->ani, &guide->ani);
1990 ast_party_id_set_init(&init->priv, &guide->priv);
1991 init->ani2 = guide->ani2;
1992 }
1993
1994 void ast_party_caller_set(struct ast_party_caller *dest, const struct ast_party_caller *src, const struct ast_set_party_caller *update)
1995 {
1996 ast_party_id_set(&dest->id, &src->id, update ? &update->id : NULL);
1997 ast_party_id_set(&dest->ani, &src->ani, update ? &update->ani : NULL);
1998 ast_party_id_set(&dest->priv, &src->priv, update ? &update->priv : NULL);
1999 dest->ani2 = src->ani2;
2000 }
2001
2002 void ast_party_caller_free(struct ast_party_caller *doomed)
2003 {
2004 ast_party_id_free(&doomed->id);
2005 ast_party_id_free(&doomed->ani);
2006 ast_party_id_free(&doomed->priv);
2007 }
2008
2009 void ast_party_connected_line_init(struct ast_party_connected_line *init)
2010 {
2011 ast_party_id_init(&init->id);
2012 ast_party_id_init(&init->ani);
2013 ast_party_id_init(&init->priv);
2014 init->ani2 = 0;
2015 init->source = AST_CONNECTED_LINE_UPDATE_SOURCE_UNKNOWN;
2016 }
2017
2018 void ast_party_connected_line_copy(struct ast_party_connected_line *dest, const struct ast_party_connected_line *src)
2019 {
2020 if (dest == src) {
2021 /* Don't copy to self */
2022 return;
2023 }
2024
2025 ast_party_id_copy(&dest->id, &src->id);
2026 ast_party_id_copy(&dest->ani, &src->ani);
2027 ast_party_id_copy(&dest->priv, &src->priv);
2028 dest->ani2 = src->ani2;
2029 dest->source = src->source;
2030 }
2031
2032 void ast_party_connected_line_set_init(struct ast_party_connected_line *init, const struct ast_party_connected_line *guide)
2033 {
2034 ast_party_id_set_init(&init->id, &guide->id);
2035 ast_party_id_set_init(&init->ani, &guide->ani);
2036 ast_party_id_set_init(&init->priv, &guide->priv);
2037 init->ani2 = guide->ani2;
2038 init->source = guide->source;
2039 }
2040
2041 void ast_party_connected_line_set(struct ast_party_connected_line *dest, const struct ast_party_connected_line *src, const struct ast_set_party_connected_line *update)
2042 {
2043 ast_party_id_set(&dest->id, &src->id, update ? &update->id : NULL);
2044 ast_party_id_set(&dest->ani, &src->ani, update ? &update->ani : NULL);
2045 ast_party_id_set(&dest->priv, &src->priv, update ? &update->priv : NULL);
2046 dest->ani2 = src->ani2;
2047 dest->source = src->source;
2048 }
2049
2050 void ast_party_connected_line_collect_caller(struct ast_party_connected_line *connected, struct ast_party_caller *caller)
2051 {
2052 connected->id = caller->id;
2053 connected->ani = caller->ani;
2054 connected->priv = caller->priv;
2055 connected->ani2 = caller->ani2;
2056 connected->source = AST_CONNECTED_LINE_UPDATE_SOURCE_UNKNOWN;
2057 }
2058
2059 void ast_party_connected_line_free(struct ast_party_connected_line *doomed)
2060 {
2061 ast_party_id_free(&doomed->id);
2062 ast_party_id_free(&doomed->ani);
2063 ast_party_id_free(&doomed->priv);
2064 }
2065
2066 void ast_party_redirecting_reason_init(struct ast_party_redirecting_reason *init)
2067 {
2068 init->str = NULL;
2069 init->code = AST_REDIRECTING_REASON_UNKNOWN;
2070 }
2071
2072 void ast_party_redirecting_reason_copy(struct ast_party_redirecting_reason *dest, const struct ast_party_redirecting_reason *src)
2073 {
2074 if (dest == src) {
2075 return;
2076 }
2077
2078 ast_free(dest->str);
2079 dest->str = ast_strdup(src->str);
2080 dest->code = src->code;
2081 }
2082
2083 void ast_party_redirecting_reason_set_init(struct ast_party_redirecting_reason *init, const struct ast_party_redirecting_reason *guide)
2084 {
2085 init->str = NULL;
2086 init->code = guide->code;
2087 }
2088
2089 void ast_party_redirecting_reason_set(struct ast_party_redirecting_reason *dest, const struct ast_party_redirecting_reason *src)
2090 {
2091 if (dest == src) {
2092 return;
2093 }
2094
2095 if (src->str && src->str != dest->str) {
2096 ast_free(dest->str);
2097 dest->str = ast_strdup(src->str);
2098 }
2099
2100 dest->code = src->code;
2101 }
2102
2103 void ast_party_redirecting_reason_free(struct ast_party_redirecting_reason *doomed)
2104 {
2105 ast_free(doomed->str);
2106 }
2107
2108
2109 void ast_party_redirecting_init(struct ast_party_redirecting *init)
2110 {
2111 ast_party_id_init(&init->orig);
2112 ast_party_id_init(&init->from);
2113 ast_party_id_init(&init->to);
2114 ast_party_id_init(&init->priv_orig);
2115 ast_party_id_init(&init->priv_from);
2116 ast_party_id_init(&init->priv_to);
2117 ast_party_redirecting_reason_init(&init->reason);
2118 ast_party_redirecting_reason_init(&init->orig_reason);
2119 init->count = 0;
2120 }
2121
2122 void ast_party_redirecting_copy(struct ast_party_redirecting *dest, const struct ast_party_redirecting *src)
2123 {
2124 if (dest == src) {
2125 /* Don't copy to self */
2126 return;
2127 }
2128
2129 ast_party_id_copy(&dest->orig, &src->orig);
2130 ast_party_id_copy(&dest->from, &src->from);
2131 ast_party_id_copy(&dest->to, &src->to);
2132 ast_party_id_copy(&dest->priv_orig, &src->priv_orig);
2133 ast_party_id_copy(&dest->priv_from, &src->priv_from);
2134 ast_party_id_copy(&dest->priv_to, &src->priv_to);
2135 ast_party_redirecting_reason_copy(&dest->reason, &src->reason);
2136 ast_party_redirecting_reason_copy(&dest->orig_reason, &src->orig_reason);
2137 dest->count = src->count;
2138 }
2139
2140 void ast_party_redirecting_set_init(struct ast_party_redirecting *init, const struct ast_party_redirecting *guide)
2141 {
2142 ast_party_id_set_init(&init->orig, &guide->orig);
2143 ast_party_id_set_init(&init->from, &guide->from);
2144 ast_party_id_set_init(&init->to, &guide->to);
2145 ast_party_id_set_init(&init->priv_orig, &guide->priv_orig);
2146 ast_party_id_set_init(&init->priv_from, &guide->priv_from);
2147 ast_party_id_set_init(&init->priv_to, &guide->priv_to);
2148 ast_party_redirecting_reason_set_init(&init->reason, &guide->reason);
2149 ast_party_redirecting_reason_set_init(&init->orig_reason, &guide->orig_reason);
2150 init->count = guide->count;
2151 }
2152
2153 void ast_party_redirecting_set(struct ast_party_redirecting *dest, const struct ast_party_redirecting *src, const struct ast_set_party_redirecting *update)
2154 {
2155 ast_party_id_set(&dest->orig, &src->orig, update ? &update->orig : NULL);
2156 ast_party_id_set(&dest->from, &src->from, update ? &update->from : NULL);
2157 ast_party_id_set(&dest->to, &src->to, update ? &update->to : NULL);
2158 ast_party_id_set(&dest->priv_orig, &src->priv_orig, update ? &update->priv_orig : NULL);
2159 ast_party_id_set(&dest->priv_from, &src->priv_from, update ? &update->priv_from : NULL);
2160 ast_party_id_set(&dest->priv_to, &src->priv_to, update ? &update->priv_to : NULL);
2161 ast_party_redirecting_reason_set(&dest->reason, &src->reason);
2162 ast_party_redirecting_reason_set(&dest->orig_reason, &src->orig_reason);
2163 dest->count = src->count;
2164 }
2165
2166 void ast_party_redirecting_free(struct ast_party_redirecting *doomed)
2167 {
2168 ast_party_id_free(&doomed->orig);
2169 ast_party_id_free(&doomed->from);
2170 ast_party_id_free(&doomed->to);
2171 ast_party_id_free(&doomed->priv_orig);
2172 ast_party_id_free(&doomed->priv_from);
2173 ast_party_id_free(&doomed->priv_to);
2174 ast_party_redirecting_reason_free(&doomed->reason);
2175 ast_party_redirecting_reason_free(&doomed->orig_reason);
2176 }
2177
2178 /*! \brief Free a channel structure */
2179 static void ast_channel_destructor(void *obj)
2180 {
2181 struct ast_channel *chan = obj;
2182 struct ast_var_t *vardata;
2183 struct ast_frame *f;
2184 struct varshead *headp;
2185 struct ast_datastore *datastore;
2186 char device_name[AST_CHANNEL_NAME];
2187 ast_callid callid;
2188
2189 ast_debug(1, "Channel %p '%s' destroying\n", chan, ast_channel_name(chan));
2190
2191 /* If there is native format music-on-hold state, free it */
2192 if (ast_channel_music_state(chan)) {
2193 ast_moh_cleanup(chan);
2194 }
2195
2196 ast_pbx_hangup_handler_destroy(chan);
2197
2198 /* Things that may possibly raise Stasis messages shouldn't occur after this point */
2199 ast_set_flag(ast_channel_flags(chan), AST_FLAG_DEAD);
2200
2201 if (ast_channel_internal_is_finalized(chan)) {
2202 /* A channel snapshot should not be in the process of being staged now. */
2203 ast_assert(!ast_test_flag(ast_channel_flags(chan), AST_FLAG_SNAPSHOT_STAGE));
2204
2205 ast_channel_lock(chan);
2206 ast_channel_publish_final_snapshot(chan);
2207 ast_channel_unlock(chan);
2208 }
2209
2210 ast_channel_lock(chan);
2211
2212 ast_channel_endpoint_set(chan, NULL);
2213
2214 /* Get rid of each of the data stores on the channel */
2215 while ((datastore = AST_LIST_REMOVE_HEAD(ast_channel_datastores(chan), entry)))
2216 /* Free the data store */
2217 ast_datastore_free(datastore);
2218
2219 /* While the channel is locked, take the reference to its callid while we tear down the call. */
2220 callid = ast_channel_callid(chan);
2221 ast_channel_callid_cleanup(chan);
2222
2223 ast_channel_unlock(chan);
2224
2225 /* Lock and unlock the channel just to be sure nobody has it locked still
2226 due to a reference that was stored in a datastore. (i.e. app_chanspy) */
2227 ast_channel_lock(chan);
2228 ast_channel_unlock(chan);
2229
2230 if (ast_channel_tech_pvt(chan)) {
2231 ast_log_callid(LOG_WARNING, callid, "Channel '%s' may not have been hung up properly\n", ast_channel_name(chan));
2232 ast_free(ast_channel_tech_pvt(chan));
2233 }
2234
2235 if (ast_channel_sched(chan)) {
2236 ast_sched_context_destroy(ast_channel_sched(chan));
2237 }
2238
2239 if (ast_channel_internal_is_finalized(chan)) {
2240 char *dashptr;
2241
2242 ast_copy_string(device_name, ast_channel_name(chan), sizeof(device_name));
2243 if ((dashptr = strrchr(device_name, '-'))) {
2244 *dashptr = '\0';
2245 }
2246 } else {
2247 device_name[0] = '\0';
2248 }
2249
2250 /* Free translators */
2251 if (ast_channel_readtrans(chan))
2252 ast_translator_free_path(ast_channel_readtrans(chan));
2253 if (ast_channel_writetrans(chan))
2254 ast_translator_free_path(ast_channel_writetrans(chan));
2255 if (ast_channel_pbx(chan))
2256 ast_log_callid(LOG_WARNING, callid, "PBX may not have been terminated properly on '%s'\n", ast_channel_name(chan));
2257
2258 /* Free formats */
2259 ast_channel_set_oldwriteformat(chan, NULL);
2260 ast_channel_set_rawreadformat(chan, NULL);
2261 ast_channel_set_rawwriteformat(chan, NULL);
2262 ast_channel_set_readformat(chan, NULL);
2263 ast_channel_set_writeformat(chan, NULL);
2264
2265 ast_party_dialed_free(ast_channel_dialed(chan));
2266 ast_party_caller_free(ast_channel_caller(chan));
2267 ast_party_connected_line_free(ast_channel_connected(chan));
2268 ast_party_connected_line_free(ast_channel_connected_indicated(chan));
2269 ast_party_redirecting_free(ast_channel_redirecting(chan));
2270
2271 /* Close pipes if appropriate */
2272 ast_channel_internal_alertpipe_close(chan);
2273 if (ast_channel_timer(chan)) {
2274 ast_timer_close(ast_channel_timer(chan));
2275 ast_channel_timer_set(chan, NULL);
2276 }
2277 while ((f = AST_LIST_REMOVE_HEAD(ast_channel_readq(chan), frame_list)))
2278 ast_frfree(f);
2279
2280 /* loop over the variables list, freeing all data and deleting list items */
2281 /* no need to lock the list, as the channel is already locked */
2282 headp = ast_channel_varshead(chan);
2283 while ((vardata = AST_LIST_REMOVE_HEAD(headp, entries)))
2284 ast_var_delete(vardata);
2285
2286 ast_app_group_discard(chan);
2287
2288 /* Destroy the jitterbuffer */
2289 ast_jb_destroy(chan);
2290
2291 if (ast_channel_cdr(chan)) {
2292 ast_cdr_free(ast_channel_cdr(chan));
2293 ast_channel_cdr_set(chan, NULL);
2294 }
2295
2296 if (ast_channel_zone(chan)) {
2297 ast_channel_zone_set(chan, ast_tone_zone_unref(ast_channel_zone(chan)));
2298 }
2299
2300 ast_channel_internal_cleanup(chan);
2301
2302 if (device_name[0]) {
2303 /*
2304 * We have a device name to notify of a new state.
2305 *
2306 * Queue an unknown state, because, while we know that this particular
2307 * instance is dead, we don't know the state of all other possible
2308 * instances.
2309 */
2310 ast_devstate_changed_literal(AST_DEVICE_UNKNOWN, (ast_test_flag(ast_channel_flags(chan), AST_FLAG_DISABLE_DEVSTATE_CACHE) ? AST_DEVSTATE_NOT_CACHABLE : AST_DEVSTATE_CACHABLE), device_name);
2311 }
2312
2313 ast_channel_nativeformats_set(chan, NULL);
2314
2315 ast_channel_named_callgroups_set(chan, NULL);
2316 ast_channel_named_pickupgroups_set(chan, NULL);
2317
2318 ast_atomic_fetchadd_int(&chancount, -1);
2319 }
2320
2321 /*! \brief Free a dummy channel structure */
2322 static void ast_dummy_channel_destructor(void *obj)
2323 {
2324 struct ast_channel *chan = obj;
2325 struct ast_datastore *datastore;
2326 struct ast_var_t *vardata;
2327 struct varshead *headp;
2328
2329 ast_pbx_hangup_handler_destroy(chan);
2330
2331 /* Get rid of each of the data stores on the channel */
2332 while ((datastore = AST_LIST_REMOVE_HEAD(ast_channel_datastores(chan), entry))) {
2333 /* Free the data store */
2334 ast_datastore_free(datastore);
2335 }
2336
2337 ast_party_dialed_free(ast_channel_dialed(chan));
2338 ast_party_caller_free(ast_channel_caller(chan));
2339 ast_party_connected_line_free(ast_channel_connected(chan));
2340 ast_party_connected_line_free(ast_channel_connected_indicated(chan));
2341 ast_party_redirecting_free(ast_channel_redirecting(chan));
2342
2343 /* loop over the variables list, freeing all data and deleting list items */
2344 /* no need to lock the list, as the channel is already locked */
2345 headp = ast_channel_varshead(chan);
2346 while ((vardata = AST_LIST_REMOVE_HEAD(headp, entries)))
2347 ast_var_delete(vardata);
2348
2349 if (ast_channel_cdr(chan)) {
2350 ast_cdr_free(ast_channel_cdr(chan));
2351 ast_channel_cdr_set(chan, NULL);
2352 }
2353
2354 ast_channel_internal_cleanup(chan);
2355 }
2356
2357 int ast_channel_datastore_inherit(struct ast_channel *from, struct ast_channel *to)
2358 {
2359 struct ast_datastore *datastore = NULL, *datastore2;
2360
2361 AST_LIST_TRAVERSE(ast_channel_datastores(from), datastore, entry) {
2362 if (datastore->inheritance > 0) {
2363 datastore2 = ast_datastore_alloc(datastore->info, datastore->uid);
2364 if (datastore2) {
2365 datastore2->data = datastore->info->duplicate ? datastore->info->duplicate(datastore->data) : NULL;
2366 datastore2->inheritance = datastore->inheritance == DATASTORE_INHERIT_FOREVER ? DATASTORE_INHERIT_FOREVER : datastore->inheritance - 1;
2367 AST_LIST_INSERT_TAIL(ast_channel_datastores(to), datastore2, entry);
2368 }
2369 }
2370 }
2371 return 0;
2372 }
2373
2374 int ast_channel_datastore_add(struct ast_channel *chan, struct ast_datastore *datastore)
2375 {
2376 int res = 0;
2377
2378 AST_LIST_INSERT_HEAD(ast_channel_datastores(chan), datastore, entry);
2379
2380 return res;
2381 }
2382
2383 int ast_channel_datastore_remove(struct ast_channel *chan, struct ast_datastore *datastore)
2384 {
2385 return AST_LIST_REMOVE(ast_channel_datastores(chan), datastore, entry) ? 0 : -1;
2386 }
2387
2388 struct ast_datastore *ast_channel_datastore_find(struct ast_channel *chan, const struct ast_datastore_info *info, const char *uid)
2389 {
2390 struct ast_datastore *datastore = NULL;
2391
2392 if (info == NULL)
2393 return NULL;
2394
2395 AST_LIST_TRAVERSE(ast_channel_datastores(chan), datastore, entry) {
2396 if (datastore->info != info) {
2397 continue;
2398 }
2399
2400 if (uid == NULL) {
2401 /* matched by type only */
2402 break;
2403 }
2404
2405 if ((datastore->uid != NULL) && !strcasecmp(uid, datastore->uid)) {
2406 /* Matched by type AND uid */
2407 break;
2408 }
2409 }
2410
2411 return datastore;
2412 }
2413
2414 /*! Set the file descriptor on the channel */
2415 void ast_channel_set_fd(struct ast_channel *chan, int which, int fd)
2416 {
2417 ast_channel_internal_fd_set(chan, which, fd);
2418 return;
2419 }
2420
2421 void ast_channel_clear_softhangup(struct ast_channel *chan, int flag)
2422 {
2423 ast_channel_lock(chan);
2424
2425 ast_channel_softhangup_internal_flag_clear(chan, flag);
2426
2427 if (!ast_channel_softhangup_internal_flag(chan)) {
2428 struct ast_frame *fr;
2429
2430 /* If we have completely cleared the softhangup flag,
2431 * then we need to fully abort the hangup process. This requires
2432 * pulling the END_OF_Q frame out of the channel frame queue if it
2433 * still happens to be there. */
2434
2435 fr = AST_LIST_LAST(ast_channel_readq(chan));
2436 if (fr && fr->frametype == AST_FRAME_CONTROL &&
2437 fr->subclass.integer == AST_CONTROL_END_OF_Q) {
2438 AST_LIST_REMOVE(ast_channel_readq(chan), fr, frame_list);
2439 ast_frfree(fr);
2440 }
2441 }
2442
2443 ast_channel_unlock(chan);
2444 }
2445
2446 /*! \brief Softly hangup a channel, don't lock */
2447 int ast_softhangup_nolock(struct ast_channel *chan, int cause)
2448 {
2449 ast_debug(1, "Soft-Hanging (%#04x) up channel '%s'\n", (unsigned)cause, ast_channel_name(chan));
2450 /* Inform channel driver that we need to be hung up, if it cares */
2451 ast_channel_softhangup_internal_flag_add(chan, cause);
2452 ast_queue_frame(chan, &ast_null_frame);
2453 /* Interrupt any poll call or such */
2454 if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_BLOCKING))
2455 pthread_kill(ast_channel_blocker(chan), SIGURG);
2456 return 0;
2457 }
2458
2459 /*! \brief Softly hangup a channel, lock */
2460 int ast_softhangup(struct ast_channel *chan, int cause)
2461 {
2462 RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
2463 int res;
2464 int tech_cause = 0;
2465
2466 ast_channel_lock(chan);
2467
2468 res = ast_softhangup_nolock(chan, cause);
2469 blob = ast_json_pack("{s: i, s: b}",
2470 "cause", cause,
2471 "soft", 1);
2472
2473 tech_cause = ast_channel_tech_hangupcause(chan);
2474 if (tech_cause) {
2475 ast_json_object_set(blob, "tech_cause", ast_json_integer_create(tech_cause));
2476 }
2477
2478 ast_channel_publish_blob(chan, ast_channel_hangup_request_type(), blob);
2479 ast_channel_unlock(chan);
2480
2481 return res;
2482 }
2483
2484 static void free_translation(struct ast_channel *clonechan)
2485 {
2486 if (ast_channel_writetrans(clonechan)) {
2487 ast_translator_free_path(ast_channel_writetrans(clonechan));
2488 }
2489 if (ast_channel_readtrans(clonechan)) {
2490 ast_translator_free_path(ast_channel_readtrans(clonechan));
2491 }
2492 ast_channel_writetrans_set(clonechan, NULL);
2493 ast_channel_readtrans_set(clonechan, NULL);
2494 }
2495
2496 void ast_set_hangupsource(struct ast_channel *chan, const char *source, int force)
2497 {
2498 RAII_VAR(struct ast_channel *, bridge, ast_channel_bridge_peer(chan), ast_channel_cleanup);
2499
2500 ast_channel_lock(chan);
2501 if (force || ast_strlen_zero(ast_channel_hangupsource(chan))) {
2502 ast_channel_hangupsource_set(chan, source);
2503 }
2504 ast_channel_unlock(chan);
2505
2506 if (bridge) {
2507 ast_channel_lock(bridge);
2508 if (force || ast_strlen_zero(ast_channel_hangupsource(bridge))) {
2509 ast_channel_hangupsource_set(bridge, source);
2510 }
2511 ast_channel_unlock(bridge);
2512 }
2513 }
2514
2515 int ast_channel_has_audio_frame_or_monitor(struct ast_channel *chan)
2516 {
2517 return !ast_audiohook_write_list_empty(ast_channel_audiohooks(chan))
2518 || !ast_framehook_list_contains_no_active(ast_channel_framehooks(chan));
2519 }
2520
2521 int ast_channel_has_hook_requiring_audio(struct ast_channel *chan)
2522 {
2523 return !ast_audiohook_write_list_empty(ast_channel_audiohooks(chan))
2524 || !ast_framehook_list_contains_no_active_of_type(ast_channel_framehooks(chan), AST_FRAME_VOICE);
2525 }
2526
2527 static void destroy_hooks(struct ast_channel *chan)
2528 {
2529 if (ast_channel_audiohooks(chan)) {
2530 ast_audiohook_detach_list(ast_channel_audiohooks(chan));
2531 ast_channel_audiohooks_set(chan, NULL);
2532 }
2533
2534 ast_framehook_list_destroy(chan);
2535 }
2536
2537 /*! \brief Hangup a channel */
2538 void ast_hangup(struct ast_channel *chan)
2539 {
2540 /* Be NULL safe for RAII_VAR() usage. */
2541 if (!chan) {
2542 return;
2543 }
2544
2545 ast_debug(1, "Channel %p '%s' hanging up. Refs: %d\n", chan, ast_channel_name(chan),
2546 ao2_ref(chan, 0));
2547
2548 ast_autoservice_stop(chan);
2549
2550 ast_channel_lock(chan);
2551
2552 while (ast_channel_masq(chan) || ast_channel_masqr(chan)) {
2553 CHANNEL_DEADLOCK_AVOIDANCE(chan);
2554 }
2555
2556 /* Mark as a zombie so a masquerade cannot be setup on this channel. */
2557 ast_set_flag(ast_channel_flags(chan), AST_FLAG_ZOMBIE);
2558
2559 ast_channel_unlock(chan);
2560
2561 /*
2562 * XXX if running the hangup handlers here causes problems
2563 * because the handlers take too long to execute, we could move
2564 * the meat of this function into another thread. A thread
2565 * where channels go to die.
2566 *
2567 * If this is done, ast_autoservice_chan_hangup_peer() will no
2568 * longer be needed.
2569 */
2570 ast_pbx_hangup_handler_run(chan);
2571 ast_channel_unlink(chan);
2572 ast_channel_lock(chan);
2573
2574 destroy_hooks(chan);
2575
2576 free_translation(chan);
2577 /* Close audio stream */
2578 if (ast_channel_stream(chan)) {
2579 ast_closestream(ast_channel_stream(chan));
2580 ast_channel_stream_set(chan, NULL);
2581 }
2582 /* Close video stream */
2583 if (ast_channel_vstream(chan)) {
2584 ast_closestream(ast_channel_vstream(chan));
2585 ast_channel_vstream_set(chan, NULL);
2586 }
2587 if (ast_channel_sched(chan)) {
2588 ast_sched_context_destroy(ast_channel_sched(chan));
2589 ast_channel_sched_set(chan, NULL);
2590 }
2591
2592 if (ast_channel_generatordata(chan)) { /* Clear any tone stuff remaining */
2593 if (ast_channel_generator(chan) && ast_channel_generator(chan)->release) {
2594 ast_channel_generator(chan)->release(chan, ast_channel_generatordata(chan));
2595 }
2596 }
2597 ast_channel_generatordata_set(chan, NULL);
2598 ast_channel_generator_set(chan, NULL);
2599
2600 if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_BLOCKING)) {
2601 ast_log(LOG_WARNING, "Hard hangup called by thread LWP %d on %s, while blocked by thread LWP %d in procedure %s! Expect a failure\n",
2602 ast_get_tid(), ast_channel_name(chan), ast_channel_blocker_tid(chan),
2603 ast_channel_blockproc(chan));
2604 ast_assert(0);
2605 }
2606
2607 if (ast_channel_tech(chan)->hangup) {
2608 ast_channel_tech(chan)->hangup(chan);
2609 }
2610
2611 ast_channel_unlock(chan);
2612
2613 ast_cc_offer(chan);
2614
2615 ast_channel_unref(chan);
2616 }
2617
2618 /*!
2619 * \internal
2620 * \brief Set channel answered time if not already set.
2621 * \since 13.11.0
2622 *
2623 * \param chan Channel to set answered time.
2624 */
2625 static void set_channel_answer_time(struct ast_channel *chan)
2626 {
2627 if (ast_tvzero(ast_channel_answertime(chan))) {
2628 struct timeval answertime;
2629
2630 answertime = ast_tvnow();
2631 ast_channel_answertime_set(chan, &answertime);
2632 }
2633 }
2634
2635
2636 int ast_raw_answer_with_stream_topology(struct ast_channel *chan, struct ast_stream_topology *topology)
2637 {
2638 int res = 0;
2639 SCOPE_TRACE(1, "%s\n", ast_channel_name(chan));
2640
2641 ast_channel_lock(chan);
2642
2643 /* You can't answer an outbound call */
2644 if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_OUTGOING)) {
2645 ast_channel_unlock(chan);
2646 return 0;
2647 }
2648
2649 /* Stop if we're a zombie or need a soft hangup */
2650 if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_ZOMBIE) || ast_check_hangup(chan)) {
2651 ast_channel_unlock(chan);
2652 return -1;
2653 }
2654
2655 /*
2656 * Mark when incoming channel answered so we can know how
2657 * long the channel has been up.
2658 */
2659 set_channel_answer_time(chan);
2660
2661 ast_channel_unlock(chan);
2662
2663 switch (ast_channel_state(chan)) {
2664 case AST_STATE_RINGING:
2665 case AST_STATE_RING:
2666 ast_channel_lock(chan);
2667 if (ast_channel_tech(chan)->answer_with_stream_topology) {
2668 res = ast_channel_tech(chan)->answer_with_stream_topology(chan, topology);
2669
2670 } else if (ast_channel_tech(chan)->answer) {
2671 res = ast_channel_tech(chan)->answer(chan);
2672 }
2673 ast_setstate(chan, AST_STATE_UP);
2674 ast_channel_unlock(chan);
2675 break;
2676 case AST_STATE_UP:
2677 /* Fall through */
2678 default:
2679 ast_debug(2, "Skipping answer, since channel state on %s is %s\n", ast_channel_name(chan), ast_state2str(ast_channel_state(chan)));
2680 break;
2681 }
2682
2683 ast_indicate(chan, -1);
2684
2685 return res;
2686 }
2687
2688 int ast_raw_answer(struct ast_channel *chan)
2689 {
2690 return ast_raw_answer_with_stream_topology(chan, NULL);
2691 }
2692
2693 int __ast_answer(struct ast_channel *chan, unsigned int delay)
2694 {
2695 int res = 0;
2696 enum ast_channel_state old_state;
2697 SCOPE_TRACE(1, "%s\n", ast_channel_name(chan));
2698
2699 old_state = ast_channel_state(chan);
2700 if ((res = ast_raw_answer(chan))) {
2701 return res;
2702 }
2703
2704 switch (old_state) {
2705 case AST_STATE_RINGING:
2706 case AST_STATE_RING:
2707 /* wait for media to start flowing, but don't wait any longer
2708 * than 'delay' or 500 milliseconds, whichever is longer
2709 */
2710 do {
2711 AST_LIST_HEAD_NOLOCK(, ast_frame) frames;
2712 struct ast_frame *cur;
2713 struct ast_frame *new_frame;
2714 int timeout_ms = MAX(delay, 500);
2715 unsigned int done = 0;
2716 struct timeval start;
2717
2718 AST_LIST_HEAD_INIT_NOLOCK(&frames);
2719
2720 start = ast_tvnow();
2721 for (;;) {
2722 int ms = ast_remaining_ms(start, timeout_ms);
2723 ms = ast_waitfor(chan, ms);
2724 if (ms < 0) {
2725 ast_log(LOG_WARNING, "Error condition occurred when polling channel %s for a voice frame: %s\n", ast_channel_name(chan), strerror(errno));
2726 res = -1;
2727 break;
2728 }
2729 if (ms == 0) {
2730 ast_debug(2, "Didn't receive a media frame from %s within %u ms of answering. Continuing anyway\n", ast_channel_name(chan), MAX(delay, 500));
2731 break;
2732 }
2733 cur = ast_read(chan);
2734 if (!cur || ((cur->frametype == AST_FRAME_CONTROL) &&
2735 (cur->subclass.integer == AST_CONTROL_HANGUP))) {
2736 if (cur) {
2737 ast_frfree(cur);
2738 }
2739 res = -1;
2740 ast_debug(2, "Hangup of channel %s detected in answer routine\n", ast_channel_name(chan));
2741 break;
2742 }
2743
2744 if ((new_frame = ast_frisolate(cur)) != cur) {
2745 ast_frfree(cur);
2746 }
2747
2748 AST_LIST_INSERT_HEAD(&frames, new_frame, frame_list);
2749
2750 /* if a specific delay period was requested, continue
2751 * until that delay has passed. don't stop just because
2752 * incoming media has arrived.
2753 */
2754 if (delay) {
2755 continue;
2756 }
2757
2758 switch (new_frame->frametype) {
2759 /* all of these frametypes qualify as 'media' */
2760 case AST_FRAME_VOICE:
2761 case AST_FRAME_VIDEO:
2762 case AST_FRAME_TEXT:
2763 case AST_FRAME_TEXT_DATA:
2764 case AST_FRAME_DTMF_BEGIN:
2765 case AST_FRAME_DTMF_END:
2766 case AST_FRAME_IMAGE:
2767 case AST_FRAME_HTML:
2768 case AST_FRAME_MODEM:
2769 case AST_FRAME_RTCP:
2770 done = 1;
2771 break;
2772 case AST_FRAME_CONTROL:
2773 case AST_FRAME_IAX:
2774 case AST_FRAME_BRIDGE_ACTION:
2775 case AST_FRAME_BRIDGE_ACTION_SYNC:
2776 case AST_FRAME_NULL:
2777 case AST_FRAME_CNG:
2778 break;
2779 }
2780
2781 if (done) {
2782 break;
2783 }
2784 }
2785
2786 ast_channel_lock(chan);
2787 while ((cur = AST_LIST_REMOVE_HEAD(&frames, frame_list))) {
2788 if (res == 0) {
2789 ast_queue_frame_head(chan, cur);
2790 }
2791 ast_frfree(cur);
2792 }
2793 ast_channel_unlock(chan);
2794 } while (0);
2795 break;
2796 default:
2797 break;
2798 }
2799
2800 return res;
2801 }
2802
2803 int ast_answer(struct ast_channel *chan)
2804 {
2805 SCOPE_TRACE(1, "%s\n", ast_channel_name(chan));
2806 return __ast_answer(chan, 0);
2807 }
2808
2809 inline int ast_auto_answer(struct ast_channel *chan)
2810 {
2811 if (ast_channel_state(chan) == AST_STATE_UP) {
2812 /* Already answered */
2813 return 0;
2814 }
2815 return ast_answer(chan);
2816 }
2817
2818 int64_t ast_channel_get_duration_ms(struct ast_channel *chan)
2819 {
2820 ast_assert(NULL != chan);
2821
2822 if (ast_tvzero(ast_channel_creationtime(chan))) {
2823 return 0;
2824 }
2825 return ast_tvdiff_ms(ast_tvnow(), ast_channel_creationtime(chan));
2826 }
2827
2828 int ast_channel_get_duration(struct ast_channel *chan)
2829 {
2830 return (ast_channel_get_duration_ms(chan) / 1000);
2831 }
2832
2833 int64_t ast_channel_get_up_time_ms(struct ast_channel *chan)
2834 {
2835 ast_assert(NULL != chan);
2836
2837 if (ast_tvzero(ast_channel_answertime(chan))) {
2838 return 0;
2839 }
2840 return ast_tvdiff_ms(ast_tvnow(), ast_channel_answertime(chan));
2841 }
2842
2843 int ast_channel_get_up_time(struct ast_channel *chan)
2844 {
2845 return (ast_channel_get_up_time_ms(chan) / 1000);
2846 }
2847
2848 /*!
2849 * \brief Determine whether or not we have to trigger dtmf emulating using 50 fps timer events
2850 * especially when no voice frames are received during dtmf processing (direct media or muted
2851 * sender case using SIP INFO)
2852 */
2853 static inline int should_trigger_dtmf_emulating(struct ast_channel *chan)
2854 {
2855 if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_DEFER_DTMF | AST_FLAG_EMULATE_DTMF)) {
2856 /* We're in the middle of emulating a digit, or DTMF has been
2857 * explicitly deferred. Trigger dtmf with periodic 50 pfs timer events, then. */
2858 return 1;
2859 }
2860
2861 if (!ast_tvzero(*ast_channel_dtmf_tv(chan)) &&
2862 ast_tvdiff_ms(ast_tvnow(), *ast_channel_dtmf_tv(chan)) < 2*AST_MIN_DTMF_GAP) {
2863 /*
2864 * We're not in the middle of a digit, but it hasn't been long enough
2865 * since the last digit, so we'll have to trigger DTMF further on.
2866 * Using 2 times AST_MIN_DTMF_GAP to trigger readq reading for possible
2867 * buffered next dtmf event
2868 */
2869 return 1;
2870 }
2871
2872 return 0;
2873 }
2874
2875 static void deactivate_generator_nolock(struct ast_channel *chan)
2876 {
2877 if (ast_channel_generatordata(chan)) {
2878 struct ast_generator *generator = ast_channel_generator(chan);
2879
2880 if (generator && generator->release) {
2881 generator->release(chan, ast_channel_generatordata(chan));
2882 }
2883 ast_channel_generatordata_set(chan, NULL);
2884 ast_channel_generator_set(chan, NULL);
2885 ast_clear_flag(ast_channel_flags(chan), AST_FLAG_WRITE_INT);
2886 ast_settimeout(chan, 0, NULL, NULL);
2887 }
2888 }
2889
2890 void ast_deactivate_generator(struct ast_channel *chan)
2891 {
2892 ast_channel_lock(chan);
2893 deactivate_generator_nolock(chan);
2894 if (should_trigger_dtmf_emulating(chan)) {
2895 /* if in the middle of dtmf emulation keep 50 tick per sec timer on rolling */
2896 struct ast_timer *timer = ast_channel_timer(chan);
2897 if (timer) {
2898 ast_timer_set_rate(timer, 50);
2899 } else {
2900 ast_log(LOG_WARNING, "No timing module loaded, DTMF length may be inaccurate\n");
2901 }
2902 }
2903 ast_channel_unlock(chan);
2904 }
2905
2906 static void generator_write_format_change(struct ast_channel *chan)
2907 {
2908 struct ast_generator *generator;
2909
2910 ast_channel_lock(chan);
2911 generator = ast_channel_generator(chan);
2912 if (generator && generator->write_format_change) {
2913 generator->write_format_change(chan, ast_channel_generatordata(chan));
2914 }
2915 ast_channel_unlock(chan);
2916 }
2917
2918 static int generator_force(const void *data)
2919 {
2920 /* Called if generator doesn't have data */
2921 void *tmp;
2922 int res;
2923 int (*generate)(struct ast_channel *chan, void *tmp, int datalen, int samples) = NULL;
2924 struct ast_channel *chan = (struct ast_channel *)data;
2925
2926 ast_channel_lock(chan);
2927 tmp = ast_channel_generatordata(chan);
2928 ast_channel_generatordata_set(chan, NULL);
2929 if (ast_channel_generator(chan))
2930 generate = ast_channel_generator(chan)->generate;
2931 ast_channel_unlock(chan);
2932
2933 if (!tmp || !generate) {
2934 return 0;
2935 }
2936
2937 res = generate(chan, tmp, 0, ast_format_get_sample_rate(ast_channel_writeformat(chan)) / 50);
2938
2939 ast_channel_lock(chan);
2940 if (ast_channel_generator(chan) && generate == ast_channel_generator(chan)->generate) {
2941 ast_channel_generatordata_set(chan, tmp);
2942 }
2943 ast_channel_unlock(chan);
2944
2945 if (res) {
2946 ast_debug(1, "Auto-deactivating generator\n");
2947 ast_deactivate_generator(chan);
2948 }
2949
2950 return 0;
2951 }
2952
2953 int ast_activate_generator(struct ast_channel *chan, struct ast_generator *gen, void *params)
2954 {
2955 int res = 0;
2956 void *generatordata = NULL;
2957
2958 ast_channel_lock(chan);
2959 if (ast_channel_generatordata(chan)) {
2960 struct ast_generator *generator_old = ast_channel_generator(chan);
2961
2962 if (generator_old && generator_old->release) {
2963 generator_old->release(chan, ast_channel_generatordata(chan));
2964 }
2965 }
2966 if (gen->alloc && !(generatordata = gen->alloc(chan, params))) {
2967 res = -1;
2968 }
2969 ast_channel_generatordata_set(chan, generatordata);
2970 if (!res) {
2971 ast_settimeout(chan, 50, generator_force, chan);
2972 ast_channel_generator_set(chan, gen);
2973 }
2974 ast_channel_unlock(chan);
2975
2976 ast_prod(chan);
2977
2978 return res;
2979 }
2980
2981 /*! \brief Wait for x amount of time on a file descriptor to have input. */
2982 int ast_waitfor_n_fd(int *fds, int n, int *ms, int *exception)
2983 {
2984 int winner = -1;
2985 ast_waitfor_nandfds(NULL, 0, fds, n, exception, &winner, ms);
2986 return winner;
2987 }
2988
2989 /*! \brief Wait for x amount of time on a file descriptor to have input. */
2990 struct ast_channel *ast_waitfor_nandfds(struct ast_channel **c, int n, int *fds, int nfds,
2991 int *exception, int *outfd, int *ms)
2992 {
2993 struct timeval start = { 0 , 0 };
2994 struct pollfd *pfds = NULL;
2995 int res;
2996 long rms;
2997 int x, y, max;
2998 int sz = nfds;
2999 struct timeval now = { 0, 0 };
3000 struct timeval whentohangup = { 0, 0 }, diff;
3001 struct ast_channel *winner = NULL;
3002 struct fdmap {
3003 int chan;
3004 int fdno;
3005 } *fdmap = NULL;
3006
3007 if (outfd) {
3008 *outfd = -99999;
3009 }
3010 if (exception) {
3011 *exception = 0;
3012 }
3013
3014 for (x = 0; x < n; x++) {
3015 ast_channel_lock(c[x]);
3016 if (!ast_tvzero(*ast_channel_whentohangup(c[x]))) {
3017 if (ast_tvzero(whentohangup))
3018 now = ast_tvnow();
3019 diff = ast_tvsub(*ast_channel_whentohangup(c[x]), now);
3020 if (diff.tv_sec < 0 || ast_tvzero(diff)) {
3021 ast_test_suite_event_notify("HANGUP_TIME", "Channel: %s", ast_channel_name(c[x]));
3022 /* Should already be hungup */
3023 ast_channel_softhangup_internal_flag_add(c[x], AST_SOFTHANGUP_TIMEOUT);
3024 ast_channel_unlock(c[x]);
3025 return c[x];
3026 }
3027 if (ast_tvzero(whentohangup) || ast_tvcmp(diff, whentohangup) < 0)
3028 whentohangup = diff;
3029 }
3030 sz += ast_channel_fd_count(c[x]);
3031 ast_channel_unlock(c[x]);
3032 }
3033
3034 if (!sz) {
3035 return NULL;
3036 }
3037
3038 pfds = ast_alloca(sizeof(*pfds) * sz);
3039 fdmap = ast_alloca(sizeof(*fdmap) * sz);
3040
3041 /* Wait full interval */
3042 rms = *ms;
3043 /* INT_MAX, not LONG_MAX, because it matters on 64-bit */
3044 if (!ast_tvzero(whentohangup) && whentohangup.tv_sec < INT_MAX / 1000) {
3045 rms = whentohangup.tv_sec * 1000 + whentohangup.tv_usec / 1000; /* timeout in milliseconds */
3046 if (*ms >= 0 && *ms < rms) { /* original *ms still smaller */
3047 rms = *ms;
3048 }
3049 } else if (!ast_tvzero(whentohangup) && rms < 0) {
3050 /* Tiny corner case... call would need to last >24 days */
3051 rms = INT_MAX;
3052 }
3053 /*
3054 * Build the pollfd array, putting the channels' fds first,
3055 * followed by individual fds. Order is important because
3056 * individual fd's must have priority over channel fds.
3057 */
3058 max = 0;
3059 for (x = 0; x < n; x++) {
3060 ast_channel_lock(c[x]);
3061 for (y = 0; y < ast_channel_fd_count(c[x]); y++) {
3062 fdmap[max].fdno = y; /* fd y is linked to this pfds */
3063 fdmap[max].chan = x; /* channel x is linked to this pfds */
3064 max += ast_add_fd(&pfds[max], ast_channel_fd(c[x], y));
3065 }
3066 CHECK_BLOCKING(c[x]);
3067 ast_channel_unlock(c[x]);
3068 }
3069 /* Add the individual fds */
3070 for (x = 0; x < nfds; x++) {
3071 fdmap[max].chan = -1;
3072 max += ast_add_fd(&pfds[max], fds[x]);
3073 }
3074
3075 if (*ms > 0) {
3076 start = ast_tvnow();
3077 }
3078
3079 if (sizeof(int) == 4) { /* XXX fix timeout > 600000 on linux x86-32 */
3080 do {
3081 int kbrms = rms;
3082 if (kbrms > 600000) {
3083 kbrms = 600000;
3084 }
3085 res = ast_poll(pfds, max, kbrms);
3086 if (!res) {
3087 rms -= kbrms;
3088 }
3089 } while (!res && (rms > 0));
3090 } else {
3091 res = ast_poll(pfds, max, rms);
3092 }
3093 for (x = 0; x < n; x++) {
3094 ast_channel_lock(c[x]);
3095 ast_clear_flag(ast_channel_flags(c[x]), AST_FLAG_BLOCKING);
3096 ast_channel_unlock(c[x]);
3097 }
3098 if (res < 0) { /* Simulate a timeout if we were interrupted */
3099 if (errno != EINTR) {
3100 *ms = -1;
3101 }
3102 return NULL;
3103 }
3104 if (!ast_tvzero(whentohangup)) { /* if we have a timeout, check who expired */
3105 now = ast_tvnow();
3106 for (x = 0; x < n; x++) {
3107 if (!ast_tvzero(*ast_channel_whentohangup(c[x])) && ast_tvcmp(*ast_channel_whentohangup(c[x]), now) <= 0) {
3108 ast_test_suite_event_notify("HANGUP_TIME", "Channel: %s", ast_channel_name(c[x]));
3109 ast_channel_softhangup_internal_flag_add(c[x], AST_SOFTHANGUP_TIMEOUT);
3110 if (winner == NULL) {
3111 winner = c[x];
3112 }
3113 }
3114 }
3115 }
3116 if (res == 0) { /* no fd ready, reset timeout and done */
3117 *ms = 0; /* XXX use 0 since we may not have an exact timeout. */
3118 return winner;
3119 }
3120 /*
3121 * Then check if any channel or fd has a pending event.
3122 * Remember to check channels first and fds last, as they
3123 * must have priority on setting 'winner'
3124 */
3125 for (x = 0; x < max; x++) {
3126 res = pfds[x].revents;
3127 if (res == 0) {
3128 continue;
3129 }
3130 if (fdmap[x].chan >= 0) { /* this is a channel */
3131 winner = c[fdmap[x].chan]; /* override previous winners */
3132 ast_channel_lock(winner);
3133 if (res & POLLPRI) {
3134 ast_set_flag(ast_channel_flags(winner), AST_FLAG_EXCEPTION);
3135 } else {
3136 ast_clear_flag(ast_channel_flags(winner), AST_FLAG_EXCEPTION);
3137 }
3138 ast_channel_fdno_set(winner, fdmap[x].fdno);
3139 ast_channel_unlock(winner);
3140 } else { /* this is an fd */
3141 if (outfd) {
3142 *outfd = pfds[x].fd;
3143 }
3144 if (exception) {
3145 *exception = (res & POLLPRI) ? -1 : 0;
3146 }
3147 winner = NULL;
3148 }
3149 }
3150 if (*ms > 0) {
3151 *ms -= ast_tvdiff_ms(ast_tvnow(), start);
3152 if (*ms < 0) {
3153 *ms = 0;
3154 }
3155 }
3156 return winner;
3157 }
3158
3159 struct ast_channel *ast_waitfor_n(struct ast_channel **c, int n, int *ms)
3160 {
3161 return ast_waitfor_nandfds(c, n, NULL, 0, NULL, NULL, ms);
3162 }
3163
3164 int ast_waitfor(struct ast_channel *c, int ms)
3165 {
3166 if (ms < 0) {
3167 do {
3168 ms = 100000;
3169 ast_waitfor_nandfds(&c, 1, NULL, 0, NULL, NULL, &ms);
3170 } while (!ms);
3171 } else {
3172 ast_waitfor_nandfds(&c, 1, NULL, 0, NULL, NULL, &ms);
3173 }
3174 return ms;
3175 }
3176
3177 int ast_waitfordigit(struct ast_channel *c, int ms)
3178 {
3179 return ast_waitfordigit_full(c, ms, NULL, -1, -1);
3180 }
3181
3182 int ast_settimeout(struct ast_channel *c, unsigned int rate, int (*func)(const void *data), void *data)
3183 {
3184 return ast_settimeout_full(c, rate, func, data, 0);
3185 }
3186
3187 int ast_settimeout_full(struct ast_channel *c, unsigned int rate, int (*func)(const void *data), void *data, unsigned int is_ao2_obj)
3188 {
3189 int res;
3190 unsigned int real_rate = rate, max_rate;
3191 struct ast_timer *timer = ast_channel_timer(c);
3192
3193 ast_channel_lock(c);
3194
3195 if (ast_channel_timingfd(c) == -1) {
3196 ast_channel_unlock(c);
3197 return -1;
3198 }
3199
3200 if (!func) {
3201 rate = 0;
3202 data = NULL;
3203 }
3204
3205 if (rate && rate > (max_rate = ast_timer_get_max_rate(timer))) {
3206 real_rate = max_rate;
3207 }
3208
3209 ast_debug(3, "Scheduling timer at (%u requested / %u actual) timer ticks per second\n", rate, real_rate);
3210
3211 res = ast_timer_set_rate(timer, real_rate);
3212
3213 if (ast_channel_timingdata(c) && ast_test_flag(ast_channel_flags(c), AST_FLAG_TIMINGDATA_IS_AO2_OBJ)) {
3214 ao2_ref(ast_channel_timingdata(c), -1);
3215 }
3216
3217 ast_channel_timingfunc_set(c, func);
3218 ast_channel_timingdata_set(c, data);
3219
3220 if (data && is_ao2_obj) {
3221 ao2_ref(data, 1);
3222 ast_set_flag(ast_channel_flags(c), AST_FLAG_TIMINGDATA_IS_AO2_OBJ);
3223 } else {
3224 ast_clear_flag(ast_channel_flags(c), AST_FLAG_TIMINGDATA_IS_AO2_OBJ);
3225 }
3226
3227 if (func == NULL && rate == 0 && ast_channel_fdno(c) == AST_TIMING_FD) {
3228 /* Clearing the timing func and setting the rate to 0
3229 * means that we don't want to be reading from the timingfd
3230 * any more. Setting c->fdno to -1 means we won't have any
3231 * errant reads from the timingfd, meaning we won't potentially
3232 * miss any important frames.
3233 */
3234 ast_channel_fdno_set(c, -1);
3235 }
3236
3237 ast_channel_unlock(c);
3238
3239 return res;
3240 }
3241
3242 int ast_waitfordigit_full(struct ast_channel *c, int timeout_ms, const char *breakon, int audiofd, int cmdfd)
3243 {
3244 struct timeval start = ast_tvnow();
3245 int ms;
3246
3247 /* Stop if we're a zombie or need a soft hangup */
3248 if (ast_test_flag(ast_channel_flags(c), AST_FLAG_ZOMBIE) || ast_check_hangup(c))
3249 return -1;
3250
3251 /* Only look for the end of DTMF, don't bother with the beginning and don't emulate things */
3252 ast_channel_set_flag(c, AST_FLAG_END_DTMF_ONLY);
3253
3254 /* Wait for a digit, no more than timeout_ms milliseconds total.
3255 * Or, wait indefinitely if timeout_ms is <0.
3256 */
3257 while ((ms = ast_remaining_ms(start, timeout_ms))) {
3258 struct ast_channel *rchan;
3259 int outfd = -1;
3260
3261 errno = 0;
3262 /* While ast_waitfor_nandfds tries to help by reducing the timeout by how much was waited,
3263 * it is unhelpful if it waited less than a millisecond.
3264 */
3265 rchan = ast_waitfor_nandfds(&c, 1, &cmdfd, (cmdfd > -1) ? 1 : 0, NULL, &outfd, &ms);
3266
3267 if (!rchan && outfd < 0 && ms) {
3268 if (errno == 0 || errno == EINTR)
3269 continue;
3270 ast_log(LOG_WARNING, "Wait failed (%s)\n", strerror(errno));
3271 ast_channel_clear_flag(c, AST_FLAG_END_DTMF_ONLY);
3272 return -1;
3273 } else if (outfd > -1) {
3274 /* The FD we were watching has something waiting */
3275 ast_log(LOG_WARNING, "The FD we were waiting for has something waiting. Waitfordigit returning numeric 1\n");
3276 ast_channel_clear_flag(c, AST_FLAG_END_DTMF_ONLY);
3277 return 1;
3278 } else if (rchan) {
3279 int res;
3280 struct ast_frame *f = ast_read(c);
3281
3282 if (!f) {
3283 ast_channel_clear_flag(c, AST_FLAG_END_DTMF_ONLY);
3284
3285 return -1;
3286 }
3287
3288 switch (f->frametype) {
3289 case AST_FRAME_DTMF_BEGIN:
3290 break;
3291 case AST_FRAME_DTMF_END:
3292 res = f->subclass.integer;
3293 if (!breakon || strchr(breakon, res)) {
3294 ast_frfree(f);
3295 ast_channel_clear_flag(c, AST_FLAG_END_DTMF_ONLY);
3296 return res;
3297 }
3298 break;
3299 case AST_FRAME_CONTROL:
3300 switch (f->subclass.integer) {
3301 case AST_CONTROL_HANGUP:
3302 ast_frfree(f);
3303 ast_channel_clear_flag(c, AST_FLAG_END_DTMF_ONLY);
3304 return -1;
3305 case AST_CONTROL_STREAM_STOP:
3306 case AST_CONTROL_STREAM_SUSPEND:
3307 case AST_CONTROL_STREAM_RESTART:
3308 case AST_CONTROL_STREAM_REVERSE:
3309 case AST_CONTROL_STREAM_FORWARD:
3310 /* Fall-through and treat as if it were a DTMF signal. Items
3311 * that perform stream control will handle this. */
3312 res = f->subclass.integer;
3313 ast_frfree(f);
3314 ast_channel_clear_flag(c, AST_FLAG_END_DTMF_ONLY);
3315 return res;
3316 case AST_CONTROL_PVT_CAUSE_CODE:
3317 case AST_CONTROL_PROGRESS:
3318 case AST_CONTROL_RINGING:
3319 case AST_CONTROL_ANSWER:
3320 case AST_CONTROL_SRCUPDATE:
3321 case AST_CONTROL_SRCCHANGE:
3322 case AST_CONTROL_CONNECTED_LINE:
3323 case AST_CONTROL_REDIRECTING:
3324 case AST_CONTROL_UPDATE_RTP_PEER:
3325 case AST_CONTROL_HOLD:
3326 case AST_CONTROL_UNHOLD:
3327 case AST_CONTROL_FLASH:
3328 case -1:
3329 /* Unimportant */
3330 break;
3331 default:
3332 ast_log(LOG_WARNING, "Unexpected control subclass '%d'\n", f->subclass.integer);
3333 break;
3334 }
3335 break;
3336 case AST_FRAME_VOICE:
3337 /* Write audio if appropriate */
3338 if (audiofd > -1) {
3339 if (write(audiofd, f->data.ptr, f->datalen) < 0) {
3340 ast_log(LOG_WARNING, "write() failed: %s\n", strerror(errno));
3341 }
3342 }
3343 default:
3344 /* Ignore */
3345 break;
3346 }
3347 ast_frfree(f);
3348 }
3349 }
3350
3351 ast_channel_clear_flag(c, AST_FLAG_END_DTMF_ONLY);
3352
3353 return 0; /* Time is up */
3354 }
3355
3356 enum DtmfDirection {
3357 DTMF_RECEIVED,
3358 DTMF_SENT
3359 };
3360
3361 static const char *dtmf_direction_to_string(enum DtmfDirection direction)
3362 {
3363 switch (direction) {
3364 case DTMF_RECEIVED:
3365 return "Received";
3366 case DTMF_SENT:
3367 return "Sent";
3368 }
3369
3370 return "?";
3371 }
3372
3373 static void send_dtmf_begin_event(struct ast_channel *chan,
3374 enum DtmfDirection direction, const char digit)
3375 {
3376 RAII_VAR(struct ast_json *, channel_blob, NULL, ast_json_unref);
3377 char digit_str[] = { digit, '\0' };
3378
3379 channel_blob = ast_json_pack("{ s: s, s: s }",
3380 "digit", digit_str,
3381 "direction", dtmf_direction_to_string(direction));
3382
3383 if (channel_blob) {
3384 ast_channel_publish_blob(chan, ast_channel_dtmf_begin_type(), channel_blob);
3385 }
3386 }
3387
3388 static void send_dtmf_end_event(struct ast_channel *chan,
3389 enum DtmfDirection direction, const char digit, long duration_ms)
3390 {
3391 RAII_VAR(struct ast_json *, channel_blob, NULL, ast_json_unref);
3392 RAII_VAR(struct ast_json *, cel_blob, NULL, ast_json_unref);
3393 char digit_str[] = { digit, '\0' };
3394
3395 channel_blob = ast_json_pack("{ s: s, s: s, s: I }",
3396 "digit", digit_str,
3397 "direction", dtmf_direction_to_string(direction),
3398 "duration_ms", (ast_json_int_t)duration_ms);
3399
3400 if (channel_blob) {
3401 ast_channel_publish_blob(chan, ast_channel_dtmf_end_type(), channel_blob);
3402 }
3403
3404 cel_blob = ast_json_pack("{ s: s, s: { s: s, s: I }}",
3405 "event", dtmf_direction_to_string(direction),
3406 "extra",
3407 "digit", digit_str,
3408 "duration_ms", (ast_json_int_t)duration_ms);
3409
3410 if (cel_blob) {
3411 ast_cel_publish_event(chan, AST_CEL_DTMF, cel_blob);
3412 } else {
3413 ast_log(LOG_WARNING, "Unable to build extradata for DTMF CEL event on channel %s", ast_channel_name(chan));
3414 }
3415 }
3416
3417 static void send_flash_event(struct ast_channel *chan)
3418 {
3419 ast_channel_publish_blob(chan, ast_channel_flash_type(), NULL);
3420 }
3421
3422 static void send_wink_event(struct ast_channel *chan)
3423 {
3424 ast_channel_publish_blob(chan, ast_channel_wink_type(), NULL);
3425 }
3426
3427 static void ast_read_generator_actions(struct ast_channel *chan, struct ast_frame *f)
3428 {
3429 struct ast_generator *generator;
3430 void *gendata;
3431 int res;
3432 int samples;
3433
3434 generator = ast_channel_generator(chan);
3435 if (!generator
3436 || !generator->generate
3437 || f->frametype != AST_FRAME_VOICE
3438 || !ast_channel_generatordata(chan)
3439 || ast_channel_timingfunc(chan)) {
3440 return;
3441 }
3442
3443 /*
3444 * We must generate frames in phase locked mode since
3445 * we have no internal timer available.
3446 */
3447 if (ast_format_cmp(f->subclass.format, ast_channel_writeformat(chan)) == AST_FORMAT_CMP_NOT_EQUAL) {
3448 float factor;
3449 factor = ((float) ast_format_get_sample_rate(ast_channel_writeformat(chan))) / ((float) ast_format_get_sample_rate(f->subclass.format));
3450 samples = (int) (((float) f->samples) * factor);
3451 } else {
3452 samples = f->samples;
3453 }
3454
3455 gendata = ast_channel_generatordata(chan);
3456 ast_channel_generatordata_set(chan, NULL); /* reset, to let writes go through */
3457
3458 /*
3459 * This unlock is here based on two assumptions that hold true at
3460 * this point in the code. 1) this function is only called from
3461 * within __ast_read() and 2) all generators call ast_write() in
3462 * their generate callback.
3463 *
3464 * The reason this is added is so that when ast_write is called,
3465 * the lock that occurs there will not recursively lock the
3466 * channel. Doing this will allow deadlock avoidance to work in
3467 * deeper functions.
3468 */
3469 ast_channel_unlock(chan);
3470 res = generator->generate(chan, gendata, f->datalen, samples);
3471 ast_channel_lock(chan);
3472 if (generator == ast_channel_generator(chan)) {
3473 ast_channel_generatordata_set(chan, gendata);
3474 if (res) {
3475 ast_debug(1, "Auto-deactivating generator\n");
3476 ast_deactivate_generator(chan);
3477 }
3478 }
3479 }
3480
3481 static inline void queue_dtmf_readq(struct ast_channel *chan, struct ast_frame *f)
3482 {
3483 struct ast_frame *fr = ast_channel_dtmff(chan);
3484
3485 fr->frametype = AST_FRAME_DTMF_END;
3486 fr->subclass.integer = f->subclass.integer;
3487 fr->len = f->len;
3488
3489 /* The only time this function will be called is for a frame that just came
3490 * out of the channel driver. So, we want to stick it on the tail of the
3491 * readq. */
3492
3493 ast_queue_frame(chan, fr);
3494 }
3495
3496 /*!
3497 * \brief Determine whether or not we should ignore DTMF in the readq
3498 */
3499 static inline int should_skip_dtmf(struct ast_channel *chan)
3500 {
3501 if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_DEFER_DTMF | AST_FLAG_EMULATE_DTMF)) {
3502 /* We're in the middle of emulating a digit, or DTMF has been
3503 * explicitly deferred. Skip this digit, then. */
3504 return 1;
3505 }
3506
3507 if (!ast_tvzero(*ast_channel_dtmf_tv(chan)) &&
3508 ast_tvdiff_ms(ast_tvnow(), *ast_channel_dtmf_tv(chan)) < AST_MIN_DTMF_GAP) {
3509 /* We're not in the middle of a digit, but it hasn't been long enough
3510 * since the last digit, so we'll have to skip DTMF for now. */
3511 return 1;
3512 }
3513
3514 return 0;
3515 }
3516
3517 /*!
3518 * \brief calculates the number of samples to jump forward with in a monitor stream.
3519
3520 * \note When using ast_seekstream() with the read and write streams of a monitor,
3521 * the number of samples to seek forward must be of the same sample rate as the stream
3522 * or else the jump will not be calculated correctly.
3523 *
3524 * \retval number of samples to seek forward after rate conversion.
3525 */
3526 static inline int calc_monitor_jump(int samples, int sample_rate, int seek_rate)
3527 {
3528 int diff = sample_rate - seek_rate;
3529
3530 if (diff > 0) {
3531 samples = samples / (float) (sample_rate / seek_rate);
3532 } else if (diff < 0) {
3533 samples = samples * (float) (seek_rate / sample_rate);
3534 }
3535
3536 return samples;
3537 }
3538
3539 static struct ast_frame *__ast_read(struct ast_channel *chan, int dropaudio, int dropnondefault)
3540 {
3541 struct ast_frame *f = NULL; /* the return value */
3542 int prestate;
3543 int cause = 0;
3544 struct ast_stream *stream = NULL, *default_stream = NULL;
3545
3546 /* this function is very long so make sure there is only one return
3547 * point at the end (there are only two exceptions to this).
3548 */
3549 ast_channel_lock(chan);
3550
3551 /* Stop if we're a zombie or need a soft hangup */
3552 if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_ZOMBIE) || ast_check_hangup(chan)) {
3553 if (ast_channel_generator(chan))
3554 ast_deactivate_generator(chan);
3555
3556 /*
3557 * It is possible for chan->_softhangup to be set and there
3558 * still be control frames that need to be read. Instead of
3559 * just going to 'done' in the case of ast_check_hangup(), we
3560 * need to queue the end-of-Q frame so that it can mark the end
3561 * of the read queue. If there are frames to be read,
3562 * ast_queue_control() will be called repeatedly, but will only
3563 * queue the first end-of-Q frame.
3564 */
3565 if (ast_channel_softhangup_internal_flag(chan)) {
3566 ast_queue_control(chan, AST_CONTROL_END_OF_Q);
3567 } else {
3568 goto done;
3569 }
3570 } else {
3571 #ifdef AST_DEVMODE
3572 /*
3573 * The ast_waitfor() code records which of the channel's file
3574 * descriptors reported that data is available. In theory,
3575 * ast_read() should only be called after ast_waitfor() reports
3576 * that a channel has data available for reading but certain
3577 * situations with stasis and ARI could give a false indication.
3578 * For this reason, we don't stop any processing.
3579 */
3580 if (ast_channel_fdno(chan) == -1) {
3581 ast_debug(3,
3582 "ast_read() on chan '%s' called with no recorded file descriptor.\n",
3583 ast_channel_name(chan));
3584 }
3585 #endif
3586 }
3587
3588 prestate = ast_channel_state(chan);
3589
3590 if (ast_channel_timingfd(chan) > -1 && ast_channel_fdno(chan) == AST_TIMING_FD) {
3591 enum ast_timer_event res;
3592 int trigger_dtmf_emulating = should_trigger_dtmf_emulating(chan);
3593
3594 ast_clear_flag(ast_channel_flags(chan), AST_FLAG_EXCEPTION);
3595
3596 res = ast_timer_get_event(ast_channel_timer(chan));
3597
3598 switch (res) {
3599 case AST_TIMING_EVENT_EXPIRED:
3600 if (ast_timer_ack(ast_channel_timer(chan), 1) < 0) {
3601 ast_log(LOG_ERROR, "Failed to acknowledge timer in ast_read\n");
3602 goto done;
3603 }
3604
3605 if (ast_channel_timingfunc(chan)) {
3606 /* save a copy of func/data before unlocking the channel */
3607 ast_timing_func_t func = ast_channel_timingfunc(chan);
3608 void *data = ast_channel_timingdata(chan);
3609 int got_ref = 0;
3610 if (data && ast_test_flag(ast_channel_flags(chan), AST_FLAG_TIMINGDATA_IS_AO2_OBJ)) {
3611 ao2_ref(data, 1);
3612 got_ref = 1;
3613 }
3614 ast_channel_fdno_set(chan, -1);
3615 ast_channel_unlock(chan);
3616 func(data);
3617 if (got_ref) {
3618 ao2_ref(data, -1);
3619 }
3620
3621 if (trigger_dtmf_emulating) {
3622 /*
3623 * Since we're breaking out of this switch block and not
3624 * returning, we need to re-lock the channel.
3625 */
3626 ast_channel_lock(chan);
3627 /* generate null frame to trigger dtmf emulating */
3628 f = &ast_null_frame;
3629 break;
3630 }
3631 } else if (trigger_dtmf_emulating) {
3632 /* generate null frame to trigger dtmf emulating */
3633 f = &ast_null_frame;
3634 break;
3635 } else {
3636 ast_timer_set_rate(ast_channel_timer(chan), 0);
3637 /* generate very last null frame to trigger dtmf emulating */
3638 f = &ast_null_frame;
3639 break;
3640 }
3641
3642 /* cannot 'goto done' because the channel is already unlocked */
3643 return &ast_null_frame;
3644
3645 case AST_TIMING_EVENT_CONTINUOUS:
3646 if (AST_LIST_EMPTY(ast_channel_readq(chan)) ||
3647 !AST_LIST_NEXT(AST_LIST_FIRST(ast_channel_readq(chan)), frame_list)) {
3648 ast_timer_disable_continuous(ast_channel_timer(chan));
3649 }
3650 break;
3651 }
3652
3653 } else if (ast_channel_fd_isset(chan, AST_JITTERBUFFER_FD) && ast_channel_fdno(chan) == AST_JITTERBUFFER_FD) {
3654 ast_clear_flag(ast_channel_flags(chan), AST_FLAG_EXCEPTION);
3655 }
3656
3657 /* Read and ignore anything on the alertpipe, but read only
3658 one sizeof(blah) per frame that we send from it */
3659 if (ast_channel_internal_alert_read(chan) == AST_ALERT_READ_FATAL) {
3660 f = &ast_null_frame;
3661 goto done;
3662 }
3663
3664 /* Check for pending read queue */
3665 if (!AST_LIST_EMPTY(ast_channel_readq(chan))) {
3666 int skipped_dtmf_frame = 0;
3667 int skip_dtmf = should_skip_dtmf(chan);
3668
3669 AST_LIST_TRAVERSE_SAFE_BEGIN(ast_channel_readq(chan), f, frame_list) {
3670 /* We have to be picky about which frame we pull off of the readq because
3671 * there are cases where we want to leave DTMF frames on the queue until
3672 * some later time. */
3673
3674 if ( (f->frametype == AST_FRAME_DTMF_BEGIN || f->frametype == AST_FRAME_DTMF_END) && skip_dtmf) {
3675 skipped_dtmf_frame = 1;
3676 continue;
3677 }
3678
3679 AST_LIST_REMOVE_CURRENT(frame_list);
3680 break;
3681 }
3682 AST_LIST_TRAVERSE_SAFE_END;
3683
3684 if (!f) {
3685 /* There were no acceptable frames on the readq. */
3686 f = &ast_null_frame;
3687 if (!skipped_dtmf_frame) {
3688 /*
3689 * Do not trigger alert pipe if only buffered dtmf begin or end frames
3690 * are left in the readq.
3691 */
3692 ast_channel_alert_write(chan);
3693 } else {
3694 /*
3695 * Safely disable continuous timer events if only buffered dtmf begin or end
3696 * frames are left in the readq.
3697 */
3698 ast_timer_disable_continuous(ast_channel_timer(chan));
3699 }
3700 }
3701
3702 /* Interpret hangup and end-of-Q frames to return NULL */
3703 /* XXX why not the same for frames from the channel ? */
3704 if (f->frametype == AST_FRAME_CONTROL) {
3705 switch (f->subclass.integer) {
3706 case AST_CONTROL_HANGUP:
3707 ast_channel_softhangup_internal_flag_add(chan, AST_SOFTHANGUP_DEV);
3708 cause = f->data.uint32;
3709 /* Fall through */
3710 case AST_CONTROL_END_OF_Q:
3711 ast_frfree(f);
3712 f = NULL;
3713 break;
3714 default:
3715 break;
3716 }
3717 } else if (f->frametype == AST_FRAME_VOICE || f->frametype == AST_FRAME_VIDEO) {
3718 if (ast_channel_tech(chan) && ast_channel_tech(chan)->read_stream) {
3719 stream = ast_stream_topology_get_stream(ast_channel_get_stream_topology(chan), f->stream_num);
3720 default_stream = ast_channel_get_default_stream(chan, ast_format_get_type(f->subclass.format));
3721 } else {
3722 /* Since this channel driver does not support multistream determine the default stream this frame
3723 * originated from and update the frame to include it.
3724 */
3725 stream = default_stream = ast_channel_get_default_stream(chan, ast_format_get_type(f->subclass.format));
3726 /* In order to allow media to be passed up the underlying media type has to have a format negotiated on
3727 * the channel itself. In cases where this hasn't happened the channel driver is incorrectly passing up
3728 * a frame for a format that has not been negotiated. If this occurs just drop the frame as we have no
3729 * stream that it came from.
3730 */
3731 if (!stream) {
3732 ast_frfree(f);
3733 f = &ast_null_frame;
3734 } else {
3735 f->stream_num = ast_stream_get_position(stream);
3736 }
3737 }
3738 }
3739 } else {
3740 if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_EXCEPTION)) {
3741 if (ast_channel_tech(chan)->exception)
3742 f = ast_channel_tech(chan)->exception(chan);
3743 else {
3744 ast_log(LOG_WARNING, "Exception flag set on '%s', but no exception handler\n", ast_channel_name(chan));
3745 f = &ast_null_frame;
3746 }
3747 /* Clear the exception flag */
3748 ast_clear_flag(ast_channel_flags(chan), AST_FLAG_EXCEPTION);
3749 } else if (ast_channel_tech(chan) && ast_channel_tech(chan)->read_stream) {
3750 f = ast_channel_tech(chan)->read_stream(chan);
3751
3752 /* This channel driver supports multistream so the stream_num on the frame is valid, the only
3753 * thing different is that we need to find the default stream so we know whether to invoke the
3754 * default stream logic or not (such as transcoding).
3755 */
3756 if (f && (f->frametype == AST_FRAME_VOICE || f->frametype == AST_FRAME_VIDEO)) {
3757 stream = ast_stream_topology_get_stream(ast_channel_get_stream_topology(chan), f->stream_num);
3758 default_stream = ast_channel_get_default_stream(chan, ast_format_get_type(f->subclass.format));
3759 }
3760 } else if (ast_channel_tech(chan) && ast_channel_tech(chan)->read) {
3761 f = ast_channel_tech(chan)->read(chan);
3762
3763 /* Since this channel driver does not support multistream determine the default stream this frame
3764 * originated from and update the frame to include it.
3765 */
3766 if (f && (f->frametype == AST_FRAME_VOICE || f->frametype == AST_FRAME_VIDEO)) {
3767 stream = default_stream = ast_channel_get_default_stream(chan, ast_format_get_type(f->subclass.format));
3768 if (!stream) {
3769 ast_frfree(f);
3770 f = &ast_null_frame;
3771 } else {
3772 f->stream_num = ast_stream_get_position(stream);
3773 }
3774 }
3775 }
3776 else
3777 ast_log(LOG_WARNING, "No read routine on channel %s\n", ast_channel_name(chan));
3778 }
3779
3780 if (stream == default_stream) {
3781 /* Perform the framehook read event here. After the frame enters the framehook list
3782 * there is no telling what will happen, <insert mad scientist laugh here>!!! */
3783 f = ast_framehook_list_read_event(ast_channel_framehooks(chan), f);
3784 }
3785
3786 /*
3787 * Reset the recorded file descriptor that triggered this read so that we can
3788 * easily detect when ast_read() is called without properly using ast_waitfor().
3789 */
3790 ast_channel_fdno_set(chan, -1);
3791
3792 if (f) {
3793 struct ast_frame *readq_tail = AST_LIST_LAST(ast_channel_readq(chan));
3794 struct ast_control_read_action_payload *read_action_payload;
3795 struct ast_party_connected_line connected;
3796 int hooked = 0;
3797
3798 /* if the channel driver returned more than one frame, stuff the excess
3799 into the readq for the next ast_read call
3800 */
3801 if (AST_LIST_NEXT(f, frame_list)) {
3802 ast_queue_frame(chan, AST_LIST_NEXT(f, frame_list));
3803 ast_frfree(AST_LIST_NEXT(f, frame_list));
3804 AST_LIST_NEXT(f, frame_list) = NULL;
3805 }
3806
3807 if (dropnondefault && stream != default_stream) {
3808 /* If the frame originates from a non-default stream and the caller can not handle other streams
3809 * absorb the frame and replace it with a null one instead.
3810 */
3811 ast_frfree(f);
3812 f = &ast_null_frame;
3813 }
3814
3815 switch (f->frametype) {
3816 case AST_FRAME_CONTROL:
3817 if (f->subclass.integer == AST_CONTROL_ANSWER) {
3818 if (prestate == AST_STATE_UP && ast_channel_is_bridged(chan)) {
3819 ast_debug(1, "Dropping duplicate answer!\n");
3820 ast_frfree(f);
3821 f = &ast_null_frame;
3822 } else {
3823 /*
3824 * Mark when outgoing channel answered so we can know how
3825 * long the channel has been up.
3826 */
3827 set_channel_answer_time(chan);
3828
3829 ast_setstate(chan, AST_STATE_UP);
3830 }
3831 } else if (f->subclass.integer == AST_CONTROL_READ_ACTION) {
3832 read_action_payload = f->data.ptr;
3833 switch (read_action_payload->action) {
3834 case AST_FRAME_READ_ACTION_CONNECTED_LINE_MACRO:
3835 ast_party_connected_line_init(&connected);
3836 ast_party_connected_line_copy(&connected, ast_channel_connected(chan));
3837 if (ast_connected_line_parse_data(read_action_payload->payload,
3838 read_action_payload->payload_size, &connected)) {
3839 ast_party_connected_line_free(&connected);
3840 break;
3841 }
3842 ast_channel_unlock(chan);
3843 if (ast_channel_connected_line_sub(NULL, chan, &connected, 0)) {
3844 ast_indicate_data(chan, AST_CONTROL_CONNECTED_LINE,
3845 read_action_payload->payload,
3846 read_action_payload->payload_size);
3847 }
3848 ast_party_connected_line_free(&connected);
3849 ast_channel_lock(chan);
3850 break;
3851 case AST_FRAME_READ_ACTION_SEND_TEXT:
3852 ast_channel_unlock(chan);
3853 ast_sendtext(chan, (const char *)read_action_payload->payload);
3854 ast_channel_lock(chan);
3855 break;
3856 case AST_FRAME_READ_ACTION_SEND_TEXT_DATA:
3857 ast_channel_unlock(chan);
3858 ast_sendtext_data(chan, (struct ast_msg_data *)read_action_payload->payload);
3859 ast_channel_lock(chan);
3860 break;
3861 }
3862 ast_frfree(f);
3863 f = &ast_null_frame;
3864 } else if (f->subclass.integer == AST_CONTROL_STREAM_TOPOLOGY_REQUEST_CHANGE && dropnondefault) {
3865 /* The caller of this function is incapable of handling streams so we don't accept the change request
3866 * and stick to the streams currently on the channel.
3867 */
3868 ast_channel_stream_topology_changed(chan, ast_channel_get_stream_topology(chan));
3869 ast_frfree(f);
3870 f = &ast_null_frame;
3871 } else if (f->subclass.integer == AST_CONTROL_STREAM_TOPOLOGY_CHANGED && dropnondefault) {
3872 /* The caller of this function is incapable of handling streams so we absorb the notification that the
3873 * stream topology has changed.
3874 */
3875 ast_frfree(f);
3876 f = &ast_null_frame;
3877 } else if (f->subclass.integer == AST_CONTROL_FLASH) {
3878 send_flash_event(chan);
3879 } else if (f->subclass.integer == AST_CONTROL_WINK) {
3880 send_wink_event(chan);
3881 }
3882 break;
3883 case AST_FRAME_DTMF_END:
3884 send_dtmf_end_event(chan, DTMF_RECEIVED, f->subclass.integer, f->len);
3885 ast_log(LOG_DTMF, "DTMF end '%c' received on %s, duration %ld ms\n", f->subclass.integer, ast_channel_name(chan), f->len);
3886 /* Queue it up if DTMF is deferred, or if DTMF emulation is forced. */
3887 if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_DEFER_DTMF) || ast_test_flag(ast_channel_flags(chan), AST_FLAG_EMULATE_DTMF)) {
3888 queue_dtmf_readq(chan, f);
3889 ast_frfree(f);
3890 f = &ast_null_frame;
3891 } else if (!ast_test_flag(ast_channel_flags(chan), AST_FLAG_IN_DTMF | AST_FLAG_END_DTMF_ONLY)) {
3892 if (!ast_tvzero(*ast_channel_dtmf_tv(chan)) &&
3893 ast_tvdiff_ms(ast_tvnow(), *ast_channel_dtmf_tv(chan)) < AST_MIN_DTMF_GAP) {
3894 /* If it hasn't been long enough, defer this digit */
3895 queue_dtmf_readq(chan, f);
3896 ast_frfree(f);
3897 f = &ast_null_frame;
3898 } else {
3899 /* There was no begin, turn this into a begin and send the end later */
3900 struct timeval tv = ast_tvnow();
3901 f->frametype = AST_FRAME_DTMF_BEGIN;
3902 ast_set_flag(ast_channel_flags(chan), AST_FLAG_EMULATE_DTMF);
3903 ast_channel_dtmf_digit_to_emulate_set(chan, f->subclass.integer);
3904 ast_channel_dtmf_tv_set(chan, &tv);
3905 if (f->len) {
3906 if (f->len > option_dtmfminduration)
3907 ast_channel_emulate_dtmf_duration_set(chan, f->len);
3908 else
3909 ast_channel_emulate_dtmf_duration_set(chan, option_dtmfminduration);
3910 } else
3911 ast_channel_emulate_dtmf_duration_set(chan, AST_DEFAULT_EMULATE_DTMF_DURATION);
3912 ast_log(LOG_DTMF, "DTMF begin emulation of '%c' with duration %u queued on %s\n", f->subclass.integer, ast_channel_emulate_dtmf_duration(chan), ast_channel_name(chan));
3913
3914 /*
3915 * Start generating 50 fps timer events (null frames) for dtmf emulating
3916 * independently from any existing incoming voice frames.
3917 * If channel generator is already activated in regular mode use these
3918 * timer events to generate null frames.
3919 */
3920 if (!ast_channel_generator(chan)) {
3921 struct ast_timer *timer = ast_channel_timer(chan);
3922 if (timer) {
3923 ast_timer_set_rate(timer, 50);
3924 } else {
3925 ast_log(LOG_WARNING, "No timing module loaded, DTMF length may be inaccurate\n");
3926 }
3927 }
3928 }
3929 if (ast_channel_audiohooks(chan)) {
3930 struct ast_frame *old_frame = f;
3931 /*!
3932 * \todo XXX It is possible to write a digit to the audiohook twice
3933 * if the digit was originally read while the channel was in autoservice. */
3934 f = ast_audiohook_write_list(chan, ast_channel_audiohooks(chan), AST_AUDIOHOOK_DIRECTION_READ, f);
3935 if (old_frame != f)
3936 ast_frfree(old_frame);
3937 }
3938 } else {
3939 struct timeval now = ast_tvnow();
3940 if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_IN_DTMF)) {
3941 ast_log(LOG_DTMF, "DTMF end accepted with begin '%c' on %s\n", f->subclass.integer, ast_channel_name(chan));
3942 ast_clear_flag(ast_channel_flags(chan), AST_FLAG_IN_DTMF);
3943 if (!f->len)
3944 f->len = ast_tvdiff_ms(now, *ast_channel_dtmf_tv(chan));
3945
3946 /* detect tones that were received on
3947 * the wire with durations shorter than
3948 * option_dtmfminduration and set f->len
3949 * to the actual duration of the DTMF
3950 * frames on the wire. This will cause
3951 * dtmf emulation to be triggered later
3952 * on.
3953 */
3954 if (ast_tvdiff_ms(now, *ast_channel_dtmf_tv(chan)) < option_dtmfminduration) {
3955 f->len = ast_tvdiff_ms(now, *ast_channel_dtmf_tv(chan));
3956 ast_log(LOG_DTMF, "DTMF end '%c' detected to have actual duration %ld on the wire, emulation will be triggered on %s\n", f->subclass.integer, f->len, ast_channel_name(chan));
3957 }
3958 } else if (!f->len) {
3959 ast_log(LOG_DTMF, "DTMF end accepted without begin '%c' on %s\n", f->subclass.integer, ast_channel_name(chan));
3960 f->len = option_dtmfminduration;
3961 }
3962 if (f->len < option_dtmfminduration && !ast_test_flag(ast_channel_flags(chan), AST_FLAG_END_DTMF_ONLY)) {
3963 ast_log(LOG_DTMF, "DTMF end '%c' has duration %ld but want minimum %u, emulating on %s\n", f->subclass.integer, f->len, option_dtmfminduration, ast_channel_name(chan));
3964 ast_set_flag(ast_channel_flags(chan), AST_FLAG_EMULATE_DTMF);
3965 ast_channel_dtmf_digit_to_emulate_set(chan, f->subclass.integer);
3966 ast_channel_emulate_dtmf_duration_set(chan, option_dtmfminduration - f->len);
3967 ast_frfree(f);
3968 f = &ast_null_frame;
3969
3970 /* Start generating 50 fps timer events (null frames) for dtmf emulating
3971 * independently from any existing incoming voice frames.
3972 * If channel generator is already activated in regular mode use these
3973 * timer events to generate null frames.
3974 */
3975 if (!ast_channel_generator(chan)) {
3976 struct ast_timer *timer = ast_channel_timer(chan);
3977 if (timer) {
3978 ast_timer_set_rate(timer, 50);
3979 } else {
3980 ast_log(LOG_WARNING, "No timing module loaded, DTMF length may be inaccurate\n");
3981 }
3982 }
3983 } else {
3984 ast_log(LOG_DTMF, "DTMF end passthrough '%c' on %s\n", f->subclass.integer, ast_channel_name(chan));
3985 if (f->len < option_dtmfminduration) {
3986 f->len = option_dtmfminduration;
3987 }
3988 ast_channel_dtmf_tv_set(chan, &now);
3989
3990 /* Start generating 50 fps timer events (null frames) for dtmf emulating
3991 * independently from any existing incoming voice frames.
3992 * If channel generator is already activated in regular mode use these
3993 * timer events to generate null frames.
3994 */
3995 if (!ast_channel_generator(chan)) {
3996 struct ast_timer *timer = ast_channel_timer(chan);
3997 if (timer) {
3998 ast_timer_set_rate(timer, 50);
3999 } else {
4000 ast_log(LOG_WARNING, "No timing module loaded, DTMF length may be inaccurate\n");
4001 }
4002 }
4003 }
4004 if (ast_channel_audiohooks(chan)) {
4005 struct ast_frame *old_frame = f;
4006 f = ast_audiohook_write_list(chan, ast_channel_audiohooks(chan), AST_AUDIOHOOK_DIRECTION_READ, f);
4007 if (old_frame != f)
4008 ast_frfree(old_frame);
4009 }
4010 }
4011 break;
4012 case AST_FRAME_DTMF_BEGIN:
4013 send_dtmf_begin_event(chan, DTMF_RECEIVED, f->subclass.integer);
4014 ast_log(LOG_DTMF, "DTMF begin '%c' received on %s\n", f->subclass.integer, ast_channel_name(chan));
4015 if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_DEFER_DTMF | AST_FLAG_END_DTMF_ONLY | AST_FLAG_EMULATE_DTMF) ||
4016 (!ast_tvzero(*ast_channel_dtmf_tv(chan)) &&
4017 ast_tvdiff_ms(ast_tvnow(), *ast_channel_dtmf_tv(chan)) < AST_MIN_DTMF_GAP) ) {
4018 ast_log(LOG_DTMF, "DTMF begin ignored '%c' on %s\n", f->subclass.integer, ast_channel_name(chan));
4019 ast_frfree(f);
4020 f = &ast_null_frame;
4021 } else {
4022 struct timeval now = ast_tvnow();
4023 ast_set_flag(ast_channel_flags(chan), AST_FLAG_IN_DTMF);
4024 ast_channel_dtmf_tv_set(chan, &now);
4025 ast_log(LOG_DTMF, "DTMF begin passthrough '%c' on %s\n", f->subclass.integer, ast_channel_name(chan));
4026 }
4027 break;
4028 case AST_FRAME_NULL:
4029 /* The EMULATE_DTMF flag must be cleared here as opposed to when the duration
4030 * is reached , because we want to make sure we pass at least one
4031 * voice frame through before starting the next digit, to ensure a gap
4032 * between DTMF digits. */
4033 if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_EMULATE_DTMF)) {
4034 struct timeval now = ast_tvnow();
4035 if (!ast_channel_emulate_dtmf_duration(chan)) {
4036 ast_clear_flag(ast_channel_flags(chan), AST_FLAG_EMULATE_DTMF);
4037 ast_channel_dtmf_digit_to_emulate_set(chan, 0);
4038 } else if (ast_tvdiff_ms(now, *ast_channel_dtmf_tv(chan)) >= ast_channel_emulate_dtmf_duration(chan)) {
4039 ast_channel_emulate_dtmf_duration_set(chan, 0);
4040 ast_frfree(f);
4041 f = ast_channel_dtmff(chan);
4042 f->frametype = AST_FRAME_DTMF_END;
4043 f->subclass.integer = ast_channel_dtmf_digit_to_emulate(chan);
4044 f->len = ast_tvdiff_ms(now, *ast_channel_dtmf_tv(chan));
4045 ast_channel_dtmf_tv_set(chan, &now);
4046 ast_clear_flag(ast_channel_flags(chan), AST_FLAG_EMULATE_DTMF);
4047 ast_channel_dtmf_digit_to_emulate_set(chan, 0);
4048 ast_log(LOG_DTMF, "DTMF end emulation of '%c' queued on %s\n", f->subclass.integer, ast_channel_name(chan));
4049 if (ast_channel_audiohooks(chan)) {
4050 struct ast_frame *old_frame = f;
4051 f = ast_audiohook_write_list(chan, ast_channel_audiohooks(chan), AST_AUDIOHOOK_DIRECTION_READ, f);
4052 if (old_frame != f) {
4053 ast_frfree(old_frame);
4054 }
4055 }
4056
4057 /* Start generating 50 fps timer events (null frames) for dtmf emulating
4058 * independently from any existing incoming voice frames.
4059 * If channel generator is already activated in regular mode use these
4060 * timer events to generate null frames.
4061 */
4062 if (!ast_channel_generator(chan)) {
4063 struct ast_timer *timer = ast_channel_timer(chan);
4064 if (timer) {
4065 ast_timer_set_rate(timer, 50);
4066 } else {
4067 ast_log(LOG_WARNING, "No timing module loaded, DTMF length may be inaccurate\n");
4068 }
4069 }
4070 }
4071 }
4072 break;
4073 case AST_FRAME_VOICE:
4074 /* If media was received from a non-default stream don't perform any actions, let it just go through */
4075 if (stream != default_stream) {
4076 break;
4077 }
4078
4079 /* The EMULATE_DTMF flag must be cleared here as opposed to when the duration
4080 * is reached , because we want to make sure we pass at least one
4081 * voice frame through before starting the next digit, to ensure a gap
4082 * between DTMF digits. */
4083 if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_EMULATE_DTMF) && !ast_channel_emulate_dtmf_duration(chan)) {
4084 ast_clear_flag(ast_channel_flags(chan), AST_FLAG_EMULATE_DTMF);
4085 ast_channel_dtmf_digit_to_emulate_set(chan, 0);
4086 }
4087
4088 if (dropaudio || ast_test_flag(ast_channel_flags(chan), AST_FLAG_IN_DTMF)) {
4089 if (dropaudio)
4090 ast_read_generator_actions(chan, f);
4091 ast_frfree(f);
4092 f = &ast_null_frame;
4093 }
4094
4095 if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_EMULATE_DTMF) && !ast_test_flag(ast_channel_flags(chan), AST_FLAG_IN_DTMF)) {
4096 struct timeval now = ast_tvnow();
4097 if (ast_tvdiff_ms(now, *ast_channel_dtmf_tv(chan)) >= ast_channel_emulate_dtmf_duration(chan)) {
4098 ast_channel_emulate_dtmf_duration_set(chan, 0);
4099 ast_frfree(f);
4100 f = ast_channel_dtmff(chan);
4101 f->frametype = AST_FRAME_DTMF_END;
4102 f->subclass.integer = ast_channel_dtmf_digit_to_emulate(chan);
4103 f->len = ast_tvdiff_ms(now, *ast_channel_dtmf_tv(chan));
4104 ast_channel_dtmf_tv_set(chan, &now);
4105 if (ast_channel_audiohooks(chan)) {
4106 struct ast_frame *old_frame = f;
4107 f = ast_audiohook_write_list(chan, ast_channel_audiohooks(chan), AST_AUDIOHOOK_DIRECTION_READ, f);
4108 if (old_frame != f)
4109 ast_frfree(old_frame);
4110 }
4111 ast_log(LOG_DTMF, "DTMF end emulation of '%c' queued on %s\n", f->subclass.integer, ast_channel_name(chan));
4112 } else {
4113 /* Drop voice frames while we're still in the middle of the digit */
4114 ast_frfree(f);
4115 f = &ast_null_frame;
4116 }
4117 break;
4118 }
4119 if (f->frametype != AST_FRAME_VOICE) {
4120 break;
4121 }
4122 if (ast_format_cmp(f->subclass.format, ast_channel_rawreadformat(chan)) != AST_FORMAT_CMP_EQUAL
4123 && ast_format_cmp(f->subclass.format, ast_channel_readformat(chan)) != AST_FORMAT_CMP_EQUAL) {
4124 struct ast_format *core_format;
4125
4126 /*
4127 * Note: This frame may not be one of the current native
4128 * formats. We may have gotten it out of the read queue from
4129 * a previous multi-frame translation, from a framehook
4130 * injected frame, or the device we're talking to isn't
4131 * respecting negotiated formats. Regardless we will accept
4132 * all frames.
4133 *
4134 * Update the read translation path to handle the new format
4135 * that just came in. If the core wants slinear we need to
4136 * setup a new translation path because the core is usually
4137 * doing something with the audio itself and may not handle
4138 * any other format. e.g., Softmix bridge, holding bridge
4139 * announcer channel, recording, AMD... Otherwise, we'll
4140 * setup to pass the frame as is to the core. In this case
4141 * the core doesn't care. The channel is likely in
4142 * autoservice, safesleep, or the channel is in a bridge.
4143 * Let the bridge technology deal with format compatibility
4144 * between the channels in the bridge.
4145 *
4146 * Beware of the transcode_via_slin and genericplc options as
4147 * they force any transcoding to go through slin on a bridge.
4148 * Unfortunately transcode_via_slin is enabled by default and
4149 * genericplc is enabled in the codecs.conf.sample file.
4150 *
4151 * XXX Only updating translation to slinear frames has some
4152 * corner cases if slinear is one of the native formats and
4153 * there are different sample rates involved. We might wind
4154 * up with conflicting translation paths between channels
4155 * where the read translation path on this channel reduces
4156 * the sample rate followed by a write translation path on
4157 * the peer channel that increases the sample rate.
4158 */
4159 core_format = ast_channel_readformat(chan);
4160 if (!ast_format_cache_is_slinear(core_format)) {
4161 core_format = f->subclass.format;
4162 }
4163 if (ast_set_read_format_path(chan, f->subclass.format, core_format)) {
4164 /* Drop frame. We couldn't make it compatible with the core. */
4165 ast_frfree(f);
4166 f = &ast_null_frame;
4167 break;
4168 }
4169 }
4170 /*
4171 * Send frame to audiohooks if present, if frametype is linear, to preserve
4172 * functional compatibility with previous behavior. If not linear, hold off
4173 * until transcoding is done where we are more likely to have a linear frame
4174 */
4175 if (ast_channel_audiohooks(chan) && ast_format_cache_is_slinear(f->subclass.format)) {
4176 /* Place hooked after declaration */
4177 struct ast_frame *old_frame = f;
4178 hooked = 1;
4179
4180 f = ast_audiohook_write_list(chan, ast_channel_audiohooks(chan), AST_AUDIOHOOK_DIRECTION_READ, f);
4181 if (old_frame != f) {
4182 ast_frfree(old_frame);
4183 }
4184 }
4185
4186 if (ast_channel_readtrans(chan)
4187 && ast_format_cmp(f->subclass.format, ast_channel_rawreadformat(chan)) == AST_FORMAT_CMP_EQUAL) {
4188 f = ast_translate(ast_channel_readtrans(chan), f, 1);
4189 if (!f) {
4190 f = &ast_null_frame;
4191 }
4192 }
4193
4194 /* Second chance at hooking a linear frame, also the last chance */
4195 if (ast_channel_audiohooks(chan) && !hooked) {
4196 struct ast_frame *old_frame = f;
4197
4198 f = ast_audiohook_write_list(chan, ast_channel_audiohooks(chan), AST_AUDIOHOOK_DIRECTION_READ, f);
4199 if (old_frame != f) {
4200 ast_frfree(old_frame);
4201 }
4202 }
4203
4204 /*
4205 * It is possible for the translation process on the channel to have
4206 * produced multiple frames from the single input frame we passed it; if
4207 * this happens, queue the additional frames *before* the frames we may
4208 * have queued earlier. if the readq was empty, put them at the head of
4209 * the queue, and if it was not, put them just after the frame that was
4210 * at the end of the queue.
4211 */
4212 if (AST_LIST_NEXT(f, frame_list)) {
4213 struct ast_frame *cur, *multi_frame = AST_LIST_NEXT(f, frame_list);
4214
4215 /* Mark these frames as being re-queued */
4216 for (cur = multi_frame; cur; cur = AST_LIST_NEXT(cur, frame_list)) {
4217 ast_set_flag(cur, AST_FRFLAG_REQUEUED);
4218 }
4219
4220 if (!readq_tail) {
4221 ast_queue_frame_head(chan, multi_frame);
4222 } else {
4223 __ast_queue_frame(chan, multi_frame, 0, readq_tail);
4224 }
4225 ast_frfree(multi_frame);
4226 AST_LIST_NEXT(f, frame_list) = NULL;
4227 }
4228
4229 /*
4230 * Run generator sitting on the line if timing device not available
4231 * and synchronous generation of outgoing frames is necessary
4232 */
4233 ast_read_generator_actions(chan, f);
4234 break;
4235 case AST_FRAME_RTCP:
4236 /* Incoming RTCP feedback needs to get to the translator for
4237 * outgoing media, which means we treat it as an ast_write */
4238 if (ast_channel_writetrans(chan)) {
4239 ast_translate(ast_channel_writetrans(chan), f, 0);
4240 }
4241 break;
4242 default:
4243 /* Just pass it on! */
4244 break;
4245 }
4246 } else {
4247 /* Make sure we always return NULL in the future */
4248 if (!ast_channel_softhangup_internal_flag(chan)) {
4249 ast_channel_softhangup_internal_flag_add(chan, AST_SOFTHANGUP_DEV);
4250 }
4251 if (cause)
4252 ast_channel_hangupcause_set(chan, cause);
4253 if (ast_channel_generator(chan))
4254 ast_deactivate_generator(chan);
4255 /* We no longer End the CDR here */
4256 }
4257
4258 /* High bit prints debugging */
4259 if (ast_channel_fin(chan) & DEBUGCHAN_FLAG)
4260 ast_frame_dump(ast_channel_name(chan), f, "<<");
4261 ast_channel_fin_set(chan, FRAMECOUNT_INC(ast_channel_fin(chan)));
4262
4263 done:
4264 if (ast_channel_music_state(chan) && ast_channel_generator(chan) && ast_channel_generator(chan)->digit && f && f->frametype == AST_FRAME_DTMF_END)
4265 ast_channel_generator(chan)->digit(chan, f->subclass.integer);
4266
4267 if (ast_channel_audiohooks(chan) && ast_audiohook_write_list_empty(ast_channel_audiohooks(chan))) {
4268 /* The list gets recreated if audiohooks are added again later */
4269 ast_audiohook_detach_list(ast_channel_audiohooks(chan));
4270 ast_channel_audiohooks_set(chan, NULL);
4271 }
4272 ast_channel_unlock(chan);
4273 return f;
4274 }
4275
4276 struct ast_frame *ast_read(struct ast_channel *chan)
4277 {
4278 return __ast_read(chan, 0, 1);
4279 }
4280
4281 struct ast_frame *ast_read_stream(struct ast_channel *chan)
4282 {
4283 return __ast_read(chan, 0, 0);
4284 }
4285
4286 struct ast_frame *ast_read_noaudio(struct ast_channel *chan)
4287 {
4288 return __ast_read(chan, 1, 1);
4289 }
4290
4291 struct ast_frame *ast_read_stream_noaudio(struct ast_channel *chan)
4292 {
4293 return __ast_read(chan, 1, 0);
4294 }
4295
4296 int ast_indicate(struct ast_channel *chan, int condition)
4297 {
4298 return ast_indicate_data(chan, condition, NULL, 0);
4299 }
4300
4301 static int attribute_const is_visible_indication(enum ast_control_frame_type condition)
4302 {
4303 /* Don't include a default case here so that we get compiler warnings
4304 * when a new type is added. */
4305
4306 switch (condition) {
4307 case AST_CONTROL_PROGRESS:
4308 case AST_CONTROL_PROCEEDING:
4309 case AST_CONTROL_VIDUPDATE:
4310 case AST_CONTROL_SRCUPDATE:
4311 case AST_CONTROL_SRCCHANGE:
4312 case AST_CONTROL_RADIO_KEY:
4313 case AST_CONTROL_RADIO_UNKEY:
4314 case AST_CONTROL_OPTION:
4315 case AST_CONTROL_WINK:
4316 case AST_CONTROL_FLASH:
4317 case AST_CONTROL_OFFHOOK:
4318 case AST_CONTROL_TAKEOFFHOOK:
4319 case AST_CONTROL_ANSWER:
4320 case AST_CONTROL_HANGUP:
4321 case AST_CONTROL_CONNECTED_LINE:
4322 case AST_CONTROL_REDIRECTING:
4323 case AST_CONTROL_TRANSFER:
4324 case AST_CONTROL_T38_PARAMETERS:
4325 case _XXX_AST_CONTROL_T38:
4326 case AST_CONTROL_CC:
4327 case AST_CONTROL_READ_ACTION:
4328 case AST_CONTROL_AOC:
4329 case AST_CONTROL_END_OF_Q:
4330 case AST_CONTROL_MCID:
4331 case AST_CONTROL_UPDATE_RTP_PEER:
4332 case AST_CONTROL_PVT_CAUSE_CODE:
4333 case AST_CONTROL_MASQUERADE_NOTIFY:
4334 case AST_CONTROL_STREAM_TOPOLOGY_REQUEST_CHANGE:
4335 case AST_CONTROL_STREAM_TOPOLOGY_CHANGED:
4336 case AST_CONTROL_STREAM_TOPOLOGY_SOURCE_CHANGED:
4337 case AST_CONTROL_STREAM_STOP:
4338 case AST_CONTROL_STREAM_SUSPEND:
4339 case AST_CONTROL_STREAM_REVERSE:
4340 case AST_CONTROL_STREAM_FORWARD:
4341 case AST_CONTROL_STREAM_RESTART:
4342 case AST_CONTROL_PLAYBACK_BEGIN:
4343 case AST_CONTROL_RECORD_CANCEL:
4344 case AST_CONTROL_RECORD_STOP:
4345 case AST_CONTROL_RECORD_SUSPEND:
4346 case AST_CONTROL_RECORD_MUTE:
4347 break;
4348
4349 case AST_CONTROL_INCOMPLETE:
4350 case AST_CONTROL_CONGESTION:
4351 case AST_CONTROL_BUSY:
4352 case AST_CONTROL_RINGING:
4353 case AST_CONTROL_RING:
4354 case AST_CONTROL_HOLD:
4355 /* You can hear these */
4356 return 1;
4357
4358 case AST_CONTROL_UNHOLD:
4359 /* This is a special case. You stop hearing this. */
4360 break;
4361 }
4362
4363 return 0;
4364 }
4365
4366 void ast_channel_hangupcause_hash_set(struct ast_channel *chan, const struct ast_control_pvt_cause_code *cause_code, int datalen)
4367 {
4368 char causevar[256];
4369
4370 if (ast_channel_dialed_causes_add(chan, cause_code, datalen)) {
4371 ast_log(LOG_WARNING, "Unable to store hangup cause for %s on %s\n", cause_code->chan_name, ast_channel_name(chan));
4372 }
4373
4374 if (cause_code->emulate_sip_cause) {
4375 snprintf(causevar, sizeof(causevar), "HASH(SIP_CAUSE,%s)", cause_code->chan_name);
4376 ast_func_write(chan, causevar, cause_code->code);
4377 }
4378 }
4379
4380 enum ama_flags ast_channel_string2amaflag(const char *flag)
4381 {
4382 if (!strcasecmp(flag, "default"))
4383 return DEFAULT_AMA_FLAGS;
4384 if (!strcasecmp(flag, "omit"))
4385 return AST_AMA_OMIT;
4386 if (!strcasecmp(flag, "billing"))
4387 return AST_AMA_BILLING;
4388 if (!strcasecmp(flag, "documentation"))
4389 return AST_AMA_DOCUMENTATION;
4390 return AST_AMA_NONE;
4391 }
4392
4393 const char *ast_channel_amaflags2string(enum ama_flags flag)
4394 {
4395 switch (flag) {
4396 case AST_AMA_OMIT:
4397 return "OMIT";
4398 case AST_AMA_BILLING:
4399 return "BILLING";
4400 case AST_AMA_DOCUMENTATION:
4401 return "DOCUMENTATION";
4402 default:
4403 return "Unknown";
4404 }
4405 }
4406
4407 /*!
4408 * \internal
4409 * \brief Preprocess connected line update.
4410 * \since 12.0.0
4411 *
4412 * \param chan channel to change the indication
4413 * \param data pointer to payload data
4414 * \param datalen size of payload data
4415 *
4416 * \note This function assumes chan is locked.
4417 *
4418 * \retval 0 keep going.
4419 * \retval -1 quit now.
4420 */
4421 static int indicate_connected_line(struct ast_channel *chan, const void *data, size_t datalen)
4422 {
4423 struct ast_party_connected_line *chan_connected = ast_channel_connected(chan);
4424 struct ast_party_connected_line *chan_indicated = ast_channel_connected_indicated(chan);
4425 struct ast_party_connected_line connected;
4426 unsigned char current[1024];
4427 unsigned char proposed[1024];
4428 int current_size;
4429 int proposed_size;
4430 int res;
4431
4432 ast_party_connected_line_set_init(&connected, chan_connected);
4433 res = ast_connected_line_parse_data(data, datalen, &connected);
4434 if (!res) {
4435 ast_channel_set_connected_line(chan, &connected, NULL);
4436 }
4437 ast_party_connected_line_free(&connected);
4438 if (res) {
4439 return -1;
4440 }
4441
4442 current_size = ast_connected_line_build_data(current, sizeof(current),
4443 chan_indicated, NULL);
4444 proposed_size = ast_connected_line_build_data(proposed, sizeof(proposed),
4445 chan_connected, NULL);
4446 if (current_size == -1 || proposed_size == -1) {
4447 return -1;
4448 }
4449
4450 if (current_size == proposed_size && !memcmp(current, proposed, current_size)) {
4451 ast_debug(1, "%s: Dropping redundant connected line update \"%s\" <%s>.\n",
4452 ast_channel_name(chan),
4453 S_COR(chan_connected->id.name.valid, chan_connected->id.name.str, ""),
4454 S_COR(chan_connected->id.number.valid, chan_connected->id.number.str, ""));
4455 return -1;
4456 }
4457
4458 ast_party_connected_line_copy(chan_indicated, chan_connected);
4459 return 0;
4460 }
4461
4462 /*!
4463 * \internal
4464 * \brief Preprocess redirecting update.
4465 * \since 12.0.0
4466 *
4467 * \param chan channel to change the indication
4468 * \param data pointer to payload data
4469 * \param datalen size of payload data
4470 *
4471 * \note This function assumes chan is locked.
4472 *
4473 * \retval 0 keep going.
4474 * \retval -1 quit now.
4475 */
4476 static int indicate_redirecting(struct ast_channel *chan, const void *data, size_t datalen)
4477 {
4478 struct ast_party_redirecting redirecting;
4479 int res;
4480
4481 ast_party_redirecting_set_init(&redirecting, ast_channel_redirecting(chan));
4482 res = ast_redirecting_parse_data(data, datalen, &redirecting);
4483 if (!res) {
4484 ast_channel_set_redirecting(chan, &redirecting, NULL);
4485 }
4486 ast_party_redirecting_free(&redirecting);
4487 return res ? -1 : 0;
4488 }
4489
4490 static int indicate_data_internal(struct ast_channel *chan, int _condition, const void *data, size_t datalen)
4491 {
4492 /* By using an enum, we'll get compiler warnings for values not handled
4493 * in switch statements. */
4494 enum ast_control_frame_type condition = _condition;
4495 struct ast_tone_zone_sound *ts = NULL;
4496 const struct ast_control_t38_parameters *t38_parameters;
4497 int res;
4498
4499 switch (condition) {
4500 case AST_CONTROL_CONNECTED_LINE:
4501 if (indicate_connected_line(chan, data, datalen)) {
4502 res = 0;
4503 return res;
4504 }
4505 break;
4506 case AST_CONTROL_REDIRECTING:
4507 if (indicate_redirecting(chan, data, datalen)) {
4508 res = 0;
4509 return res;
4510 }
4511 break;
4512 case AST_CONTROL_HOLD:
4513 case AST_CONTROL_UNHOLD:
4514 ast_channel_hold_state_set(chan, _condition);
4515 break;
4516 case AST_CONTROL_T38_PARAMETERS:
4517 t38_parameters = data;
4518 switch (t38_parameters->request_response) {
4519 case AST_T38_REQUEST_NEGOTIATE:
4520 case AST_T38_NEGOTIATED:
4521 ast_channel_set_is_t38_active_nolock(chan, 1);
4522 break;
4523 case AST_T38_REQUEST_TERMINATE:
4524 case AST_T38_TERMINATED:
4525 case AST_T38_REFUSED:
4526 ast_channel_set_is_t38_active_nolock(chan, 0);
4527 break;
4528 default:
4529 break;
4530 }
4531 break;
4532 default:
4533 break;
4534 }
4535
4536 if (is_visible_indication(condition)) {
4537 /* A new visible indication is requested. */
4538 ast_channel_visible_indication_set(chan, _condition);
4539 } else if (condition == AST_CONTROL_UNHOLD || _condition < 0) {
4540 /* Visible indication is cleared/stopped. */
4541 ast_channel_visible_indication_set(chan, 0);
4542 }
4543
4544 if (ast_channel_tech(chan)->indicate) {
4545 /* See if the channel driver can handle this condition. */
4546 res = ast_channel_tech(chan)->indicate(chan, _condition, data, datalen);
4547 } else {
4548 res = -1;
4549 }
4550
4551 if (!res) {
4552 /* The channel driver successfully handled this indication */
4553 res = 0;
4554 return res;
4555 }
4556
4557 /* The channel driver does not support this indication, let's fake
4558 * it by doing our own tone generation if applicable. */
4559
4560 /*!\note If we compare the enumeration type, which does not have any
4561 * negative constants, the compiler may optimize this code away.
4562 * Therefore, we must perform an integer comparison here. */
4563 if (_condition < 0) {
4564 /* Stop any tones that are playing */
4565 ast_playtones_stop(chan);
4566 res = 0;
4567 return res;
4568 }
4569
4570 /* Handle conditions that we have tones for. */
4571 switch (condition) {
4572 case _XXX_AST_CONTROL_T38:
4573 /* deprecated T.38 control frame */
4574 res = -1;
4575 return res;
4576 case AST_CONTROL_T38_PARAMETERS:
4577 /* there is no way to provide 'default' behavior for these
4578 * control frames, so we need to return failure, but there
4579 * is also no value in the log message below being emitted
4580 * since failure to handle these frames is not an 'error'
4581 * so just return right now. in addition, we want to return
4582 * whatever value the channel driver returned, in case it
4583 * has some meaning.*/
4584 return res;
4585 case AST_CONTROL_RINGING:
4586 ts = ast_get_indication_tone(ast_channel_zone(chan), "ring");
4587 /* It is common practice for channel drivers to return -1 if trying
4588 * to indicate ringing on a channel which is up. The idea is to let the
4589 * core generate the ringing inband. However, we don't want the
4590 * warning message about not being able to handle the specific indication
4591 * to print nor do we want ast_indicate_data to return an "error" for this
4592 * condition
4593 */
4594 if (ast_channel_state(chan) == AST_STATE_UP) {
4595 res = 0;
4596 }
4597 break;
4598 case AST_CONTROL_BUSY:
4599 ts = ast_get_indication_tone(ast_channel_zone(chan), "busy");
4600 break;
4601 case AST_CONTROL_INCOMPLETE:
4602 case AST_CONTROL_CONGESTION:
4603 ts = ast_get_indication_tone(ast_channel_zone(chan), "congestion");
4604 break;
4605 case AST_CONTROL_PVT_CAUSE_CODE:
4606 ast_channel_hangupcause_hash_set(chan, data, datalen);
4607 res = 0;
4608 break;
4609 case AST_CONTROL_PROGRESS:
4610 case AST_CONTROL_PROCEEDING:
4611 case AST_CONTROL_VIDUPDATE:
4612 case AST_CONTROL_SRCUPDATE:
4613 case AST_CONTROL_SRCCHANGE:
4614 case AST_CONTROL_RADIO_KEY:
4615 case AST_CONTROL_RADIO_UNKEY:
4616 case AST_CONTROL_OPTION:
4617 case AST_CONTROL_WINK:
4618 case AST_CONTROL_FLASH:
4619 case AST_CONTROL_OFFHOOK:
4620 case AST_CONTROL_TAKEOFFHOOK:
4621 case AST_CONTROL_ANSWER:
4622 case AST_CONTROL_HANGUP:
4623 case AST_CONTROL_RING:
4624 case AST_CONTROL_HOLD:
4625 case AST_CONTROL_UNHOLD:
4626 case AST_CONTROL_TRANSFER:
4627 case AST_CONTROL_CONNECTED_LINE:
4628 case AST_CONTROL_REDIRECTING:
4629 case AST_CONTROL_CC:
4630 case AST_CONTROL_READ_ACTION:
4631 case AST_CONTROL_AOC:
4632 case AST_CONTROL_END_OF_Q:
4633 case AST_CONTROL_MCID:
4634 case AST_CONTROL_MASQUERADE_NOTIFY:
4635 case AST_CONTROL_UPDATE_RTP_PEER:
4636 case AST_CONTROL_STREAM_TOPOLOGY_REQUEST_CHANGE:
4637 case AST_CONTROL_STREAM_TOPOLOGY_CHANGED:
4638 case AST_CONTROL_STREAM_TOPOLOGY_SOURCE_CHANGED:
4639 case AST_CONTROL_STREAM_STOP:
4640 case AST_CONTROL_STREAM_SUSPEND:
4641 case AST_CONTROL_STREAM_REVERSE:
4642 case AST_CONTROL_STREAM_FORWARD:
4643 case AST_CONTROL_STREAM_RESTART:
4644 case AST_CONTROL_PLAYBACK_BEGIN:
4645 case AST_CONTROL_RECORD_CANCEL:
4646 case AST_CONTROL_RECORD_STOP:
4647 case AST_CONTROL_RECORD_SUSPEND:
4648 case AST_CONTROL_RECORD_MUTE:
4649 /* Nothing left to do for these. */
4650 res = 0;
4651 break;
4652 }
4653
4654 if (ts) {
4655 /* We have a tone to play, yay. */
4656 ast_debug(1, "Driver for channel '%s' does not support indication %u, emulating it\n", ast_channel_name(chan), condition);
4657 res = ast_playtones_start(chan, 0, ts->data, 1);
4658 if (!res) {
4659 ast_test_suite_event_notify("RINGING_INBAND",
4660 "Channel: %s\r\n",
4661 ast_channel_name(chan));
4662 }
4663 ts = ast_tone_zone_sound_unref(ts);
4664 }
4665
4666 if (res) {
4667 /* not handled */
4668 ast_log(LOG_WARNING, "Unable to handle indication %u for '%s'\n", condition, ast_channel_name(chan));
4669 }
4670
4671 return res;
4672 }
4673
4674 int ast_indicate_data(struct ast_channel *chan, int _condition, const void *data, size_t datalen)
4675 {
4676 int res;
4677 /* this frame is used by framehooks. if it is set, we must free it at the end of this function */
4678 struct ast_frame *awesome_frame = NULL;
4679
4680 ast_channel_lock(chan);
4681
4682 /* Don't bother if the channel is about to go away, anyway. */
4683 if ((ast_test_flag(ast_channel_flags(chan), AST_FLAG_ZOMBIE)
4684 || (ast_check_hangup(chan) && !ast_channel_is_leaving_bridge(chan)))
4685 && _condition != AST_CONTROL_MASQUERADE_NOTIFY) {
4686 res = -1;
4687 goto indicate_cleanup;
4688 }
4689
4690 if (!ast_framehook_list_is_empty(ast_channel_framehooks(chan))) {
4691 /* Do framehooks now, do it, go, go now */
4692 struct ast_frame frame = {
4693 .frametype = AST_FRAME_CONTROL,
4694 .subclass.integer = _condition,
4695 .data.ptr = (void *) data, /* this cast from const is only okay because we do the ast_frdup below */
4696 .datalen = datalen
4697 };
4698
4699 /* we have now committed to freeing this frame */
4700 awesome_frame = ast_frdup(&frame);
4701
4702 /* who knows what we will get back! the anticipation is killing me. */
4703 awesome_frame = ast_framehook_list_write_event(ast_channel_framehooks(chan),
4704 awesome_frame);
4705 if (!awesome_frame
4706 || awesome_frame->frametype != AST_FRAME_CONTROL) {
4707 res = 0;
4708 goto indicate_cleanup;
4709 }
4710
4711 _condition = awesome_frame->subclass.integer;
4712 data = awesome_frame->data.ptr;
4713 datalen = awesome_frame->datalen;
4714 }
4715
4716 res = indicate_data_internal(chan, _condition, data, datalen);
4717
4718 indicate_cleanup:
4719 ast_channel_unlock(chan);
4720 if (awesome_frame) {
4721 ast_frfree(awesome_frame);
4722 }
4723
4724 return res;
4725 }
4726
4727 int ast_recvchar(struct ast_channel *chan, int timeout)
4728 {
4729 int c;
4730 char *buf = ast_recvtext(chan, timeout);
4731 if (buf == NULL)
4732 return -1; /* error or timeout */
4733 c = *(unsigned char *)buf;
4734 ast_free(buf);
4735 return c;
4736 }
4737
4738 char *ast_recvtext(struct ast_channel *chan, int timeout)
4739 {
4740 int res;
4741 char *buf = NULL;
4742 struct timeval start = ast_tvnow();
4743 int ms;
4744
4745 while ((ms = ast_remaining_ms(start, timeout))) {
4746 struct ast_frame *f;
4747
4748 if (ast_check_hangup(chan)) {
4749 break;
4750 }
4751 res = ast_waitfor(chan, ms);
4752 if (res <= 0) {/* timeout or error */
4753 break;
4754 }
4755 f = ast_read(chan);
4756 if (f == NULL) {
4757 break; /* no frame */
4758 }
4759 if (f->frametype == AST_FRAME_CONTROL && f->subclass.integer == AST_CONTROL_HANGUP) {
4760 ast_frfree(f);
4761 break;
4762 } else if (f->frametype == AST_FRAME_TEXT) { /* what we want */
4763 buf = ast_strndup((char *) f->data.ptr, f->datalen); /* dup and break */
4764 ast_frfree(f);
4765 break;
4766 }
4767 ast_frfree(f);
4768 }
4769 return buf;
4770 }
4771
4772 int ast_sendtext_data(struct ast_channel *chan, struct ast_msg_data *msg)
4773 {
4774 int res = 0;
4775 const char *body = ast_msg_data_get_attribute(msg, AST_MSG_DATA_ATTR_BODY);
4776 const char *content_type = ast_msg_data_get_attribute(msg, AST_MSG_DATA_ATTR_CONTENT_TYPE);
4777
4778 ast_channel_lock(chan);
4779 /* Stop if we're a zombie or need a soft hangup */
4780 if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_ZOMBIE) || ast_check_hangup(chan)) {
4781 ast_channel_unlock(chan);
4782 return -1;
4783 }
4784
4785 CHECK_BLOCKING(chan);
4786 if (ast_channel_tech(chan)->write_text
4787 && (ast_strlen_zero(content_type) || ast_begins_with(content_type, "text/"))
4788 && (ast_format_cap_has_type(ast_channel_nativeformats(chan), AST_MEDIA_TYPE_TEXT))) {
4789 struct ast_frame f;
4790 /* T.140 payload does not include the null byte terminator */
4791 size_t body_len = strlen(body);
4792
4793 /* Process as T.140 text (moved here from ast_sendtext() */
4794 memset(&f, 0, sizeof(f));
4795 f.src = "DIALPLAN";
4796 f.subclass.format = ast_format_t140;
4797 f.frametype = AST_FRAME_TEXT;
4798 f.datalen = body_len;
4799 f.mallocd = AST_MALLOCD_DATA;
4800 f.data.ptr = ast_strdup(body);
4801 if (f.data.ptr) {
4802 res = ast_channel_tech(chan)->write_text(chan, &f);
4803 } else {
4804 res = -1;
4805 }
4806 ast_frfree(&f);
4807 } else if ((ast_channel_tech(chan)->properties & AST_CHAN_TP_SEND_TEXT_DATA)
4808 && ast_channel_tech(chan)->send_text_data) {
4809 /* Send enhanced message to a channel driver that supports it */
4810 ast_debug(1, "Sending TEXT_DATA from '%s' to %s:%s %s\n",
4811 ast_msg_data_get_attribute(msg, AST_MSG_DATA_ATTR_FROM),
4812 ast_msg_data_get_attribute(msg, AST_MSG_DATA_ATTR_TO),
4813 ast_channel_name(chan), body);
4814 res = ast_channel_tech(chan)->send_text_data(chan, msg);
4815 } else if (ast_channel_tech(chan)->send_text
4816 && (ast_strlen_zero(content_type) || ast_begins_with(content_type, "text/"))) {
4817 /* Send the body of an enhanced message to a channel driver that supports only a char str */
4818 ast_debug(1, "Sending TEXT to %s: %s\n", ast_channel_name(chan), body);
4819 res = ast_channel_tech(chan)->send_text(chan, body);
4820 } else {
4821 ast_debug(1, "Channel technology does not support sending content type '%s' on channel '%s'\n",
4822 S_OR(content_type, "text/plain"), ast_channel_name(chan));
4823 res = -1;
4824 }
4825 ast_clear_flag(ast_channel_flags(chan), AST_FLAG_BLOCKING);
4826 ast_channel_unlock(chan);
4827 return res;
4828 }
4829
4830 int ast_sendtext(struct ast_channel *chan, const char *text)
4831 {
4832 struct ast_msg_data *msg;
4833 int rc;
4834 struct ast_msg_data_attribute attrs[] =
4835 {
4836 {
4837 .type = AST_MSG_DATA_ATTR_BODY,
4838 .value = (char *)text,
4839 }
4840 };
4841
4842 if (ast_strlen_zero(text)) {
4843 return 0;
4844 }
4845
4846 msg = ast_msg_data_alloc(AST_MSG_DATA_SOURCE_TYPE_UNKNOWN, attrs, ARRAY_LEN(attrs));
4847 if (!msg) {
4848 return -1;
4849 }
4850 rc = ast_sendtext_data(chan, msg);
4851 ast_free(msg);
4852
4853 return rc;
4854 }
4855
4856 int ast_senddigit_mf_begin(struct ast_channel *chan, char digit)
4857 {
4858 static const char * const mf_tones[] = {
4859 "1300+1500", /* 0 */
4860 "700+900", /* 1 */
4861 "700+1100", /* 2 */
4862 "900+1100", /* 3 */
4863 "700+1300", /* 4 */
4864 "900+1300", /* 5 */
4865 "1100+1300", /* 6 */
4866 "700+1500", /* 7 */
4867 "900+1500", /* 8 */
4868 "1100+1500", /* 9 */
4869 "1100+1700", /* * (KP) */
4870 "1500+1700", /* # (ST) */
4871 "900+1700", /* A (STP) */
4872 "1300+1700", /* B (ST2P) */
4873 "700+1700" /* C (ST3P) */
4874 };
4875
4876 if (digit >= '0' && digit <='9') {
4877 ast_playtones_start(chan, 0, mf_tones[digit-'0'], 0);
4878 } else if (digit == '*') {
4879 ast_playtones_start(chan, 0, mf_tones[10], 0);
4880 } else if (digit == '#') {
4881 ast_playtones_start(chan, 0, mf_tones[11], 0);
4882 } else if (digit == 'A') {
4883 ast_playtones_start(chan, 0, mf_tones[12], 0);
4884 } else if (digit == 'B') {
4885 ast_playtones_start(chan, 0, mf_tones[13], 0);
4886 } else if (digit == 'C') {
4887 ast_playtones_start(chan, 0, mf_tones[14], 0);
4888 } else {
4889 /* not handled */
4890 ast_log(LOG_WARNING, "Unable to generate MF tone '%c' for '%s'\n", digit, ast_channel_name(chan));
4891 }
4892 return 0;
4893 }
4894
4895 int ast_senddigit_begin(struct ast_channel *chan, char digit)
4896 {
4897 /* Device does not support DTMF tones, lets fake
4898 * it by doing our own generation. */
4899 static const char * const dtmf_tones[] = {
4900 "941+1336", /* 0 */
4901 "697+1209", /* 1 */
4902 "697+1336", /* 2 */
4903 "697+1477", /* 3 */
4904 "770+1209", /* 4 */
4905 "770+1336", /* 5 */
4906 "770+1477", /* 6 */
4907 "852+1209", /* 7 */
4908 "852+1336", /* 8 */
4909 "852+1477", /* 9 */
4910 "697+1633", /* A */
4911 "770+1633", /* B */
4912 "852+1633", /* C */
4913 "941+1633", /* D */
4914 "941+1209", /* * */
4915 "941+1477" /* # */
4916 };
4917
4918 if (!ast_channel_tech(chan)->send_digit_begin)
4919 return 0;
4920
4921 ast_channel_lock(chan);
4922 ast_channel_sending_dtmf_digit_set(chan, digit);
4923 ast_channel_sending_dtmf_tv_set(chan, ast_tvnow());
4924 ast_channel_unlock(chan);
4925
4926 if (!ast_channel_tech(chan)->send_digit_begin(chan, digit))
4927 return 0;
4928
4929 if (digit >= '0' && digit <='9')
4930 ast_playtones_start(chan, 0, dtmf_tones[digit-'0'], 0);
4931 else if (digit >= 'A' && digit <= 'D')
4932 ast_playtones_start(chan, 0, dtmf_tones[digit-'A'+10], 0);
4933 else if (digit == '*')
4934 ast_playtones_start(chan, 0, dtmf_tones[14], 0);
4935 else if (digit == '#')
4936 ast_playtones_start(chan, 0, dtmf_tones[15], 0);
4937 else {
4938 /* not handled */
4939 ast_debug(1, "Unable to generate DTMF tone '%c' for '%s'\n", digit, ast_channel_name(chan));
4940 }
4941
4942 return 0;
4943 }
4944
4945 int ast_senddigit_end(struct ast_channel *chan, char digit, unsigned int duration)
4946 {
4947 int res = -1;
4948
4949 if (ast_channel_tech(chan)->send_digit_end)
4950 res = ast_channel_tech(chan)->send_digit_end(chan, digit, duration);
4951
4952 ast_channel_lock(chan);
4953 if (ast_channel_sending_dtmf_digit(chan) == digit) {
4954 ast_channel_sending_dtmf_digit_set(chan, 0);
4955 }
4956 ast_channel_unlock(chan);
4957
4958 if (res && ast_channel_generator(chan))
4959 ast_playtones_stop(chan);
4960
4961 return 0;
4962 }
4963
4964 int ast_senddigit_mf_end(struct ast_channel *chan)
4965 {
4966 if (ast_channel_generator(chan)) {
4967 ast_playtones_stop(chan);
4968 return 0;
4969 }
4970 return -1;
4971 }
4972
4973 int ast_senddigit_mf(struct ast_channel *chan, char digit, unsigned int duration,
4974 unsigned int durationkp, unsigned int durationst, int is_external)
4975 {
4976 if (duration < DEFAULT_EMULATE_MF_DURATION) {
4977 duration = DEFAULT_EMULATE_MF_DURATION;
4978 }
4979 if (ast_channel_tech(chan)->send_digit_begin) {
4980 if (digit == '*') {
4981 duration = durationkp;
4982 } else if (digit == '#' || digit == 'A' || digit == 'B' || digit == 'C') {
4983 duration = durationst;
4984 }
4985 ast_senddigit_mf_begin(chan, digit);
4986 if (is_external) {
4987 usleep(duration * 1000);
4988 } else {
4989 ast_safe_sleep(chan, duration);
4990 }
4991 }
4992 return ast_senddigit_mf_end(chan);
4993 }
4994
4995 int ast_senddigit(struct ast_channel *chan, char digit, unsigned int duration)
4996 {
4997 if (duration < AST_DEFAULT_EMULATE_DTMF_DURATION) {
4998 duration = AST_DEFAULT_EMULATE_DTMF_DURATION;
4999 }
5000 if (ast_channel_tech(chan)->send_digit_begin) {
5001 ast_senddigit_begin(chan, digit);
5002 ast_safe_sleep(chan, duration);
5003 }
5004
5005 return ast_senddigit_end(chan, digit, duration);
5006 }
5007
5008 int ast_senddigit_external(struct ast_channel *chan, char digit, unsigned int duration)
5009 {
5010 if (duration < AST_DEFAULT_EMULATE_DTMF_DURATION) {
5011 duration = AST_DEFAULT_EMULATE_DTMF_DURATION;
5012 }
5013 if (ast_channel_tech(chan)->send_digit_begin) {
5014 ast_senddigit_begin(chan, digit);
5015 usleep(duration * 1000);
5016 }
5017
5018 return ast_senddigit_end(chan, digit, duration);
5019 }
5020
5021 int ast_prod(struct ast_channel *chan)
5022 {
5023 struct ast_frame a = { AST_FRAME_VOICE };
5024 char nothing[128];
5025
5026 /* Send an empty audio frame to get things moving */
5027 if (ast_channel_state(chan) != AST_STATE_UP) {
5028 ast_debug(3, "Prodding channel '%s'\n", ast_channel_name(chan));
5029 a.subclass.format = ast_channel_rawwriteformat(chan);
5030 a.data.ptr = nothing + AST_FRIENDLY_OFFSET;
5031 a.src = "ast_prod"; /* this better match check in ast_write */
5032 if (ast_write(chan, &a))
5033 ast_log(LOG_WARNING, "Prodding channel '%s' failed\n", ast_channel_name(chan));
5034 }
5035 return 0;
5036 }
5037
5038 int ast_write_video(struct ast_channel *chan, struct ast_frame *fr)
5039 {
5040 int res;
5041 if (!ast_channel_tech(chan)->write_video)
5042 return 0;
5043 res = ast_write(chan, fr);
5044 if (!res)
5045 res = 1;
5046 return res;
5047 }
5048
5049 struct plc_ds {
5050 /* A buffer in which to store SLIN PLC
5051 * samples generated by the generic PLC
5052 * functionality in plc.c
5053 */
5054 int16_t *samples_buf;
5055 /* The current number of samples in the
5056 * samples_buf
5057 */
5058 size_t num_samples;
5059 plc_state_t plc_state;
5060 };
5061
5062 static void plc_ds_destroy(void *data)
5063 {
5064 struct plc_ds *plc = data;
5065 ast_free(plc->samples_buf);
5066 ast_free(plc);
5067 }
5068
5069 static const struct ast_datastore_info plc_ds_info = {
5070 .type = "plc",
5071 .destroy = plc_ds_destroy,
5072 };
5073
5074 static void adjust_frame_for_plc(struct ast_channel *chan, struct ast_frame *frame, struct ast_datastore *datastore)
5075 {
5076 int num_new_samples = frame->samples;
5077 struct plc_ds *plc = datastore->data;
5078
5079 /* As a general note, let me explain the somewhat odd calculations used when taking
5080 * the frame offset into account here. According to documentation in frame.h, the frame's
5081 * offset field indicates the number of bytes that the audio is offset. The plc->samples_buf
5082 * is not an array of bytes, but rather an array of 16-bit integers since it holds SLIN
5083 * samples. So I had two choices to make here with the offset.
5084 *
5085 * 1. Make the offset AST_FRIENDLY_OFFSET bytes. The main downside for this is that
5086 * I can't just add AST_FRIENDLY_OFFSET to the plc->samples_buf and have the pointer
5087 * arithmetic come out right. I would have to do some odd casting or division for this to
5088 * work as I wanted.
5089 * 2. Make the offset AST_FRIENDLY_OFFSET * 2 bytes. This allows the pointer arithmetic
5090 * to work out better with the plc->samples_buf. The downside here is that the buffer's
5091 * allocation contains an extra 64 bytes of unused space.
5092 *
5093 * I decided to go with option 2. This is why in the calloc statement and the statement that
5094 * sets the frame's offset, AST_FRIENDLY_OFFSET is multiplied by 2.
5095 */
5096
5097 /* If this audio frame has no samples to fill in, ignore it */
5098 if (!num_new_samples) {
5099 return;
5100 }
5101
5102 /* First, we need to be sure that our buffer is large enough to accommodate
5103 * the samples we need to fill in. This will likely only occur on the first
5104 * frame we write.
5105 */
5106 if (plc->num_samples < num_new_samples) {
5107 ast_free(plc->samples_buf);
5108 plc->samples_buf = ast_calloc(1, (num_new_samples * sizeof(*plc->samples_buf)) + (AST_FRIENDLY_OFFSET * 2));
5109 if (!plc->samples_buf) {
5110 ast_channel_datastore_remove(chan, datastore);
5111 ast_datastore_free(datastore);
5112 return;
5113 }
5114 plc->num_samples = num_new_samples;
5115 }
5116
5117 if (frame->datalen == 0) {
5118 plc_fillin(&plc->plc_state, plc->samples_buf + AST_FRIENDLY_OFFSET, frame->samples);
5119 frame->data.ptr = plc->samples_buf + AST_FRIENDLY_OFFSET;
5120 frame->datalen = num_new_samples * 2;
5121 frame->offset = AST_FRIENDLY_OFFSET * 2;
5122 } else {
5123 plc_rx(&plc->plc_state, frame->data.ptr, frame->samples);
5124 }
5125 }
5126
5127 static void apply_plc(struct ast_channel *chan, struct ast_frame *frame)
5128 {
5129 struct ast_datastore *datastore;
5130 struct plc_ds *plc;
5131
5132 datastore = ast_channel_datastore_find(chan, &plc_ds_info, NULL);
5133 if (datastore) {
5134 plc = datastore->data;
5135 adjust_frame_for_plc(chan, frame, datastore);
5136 return;
5137 }
5138
5139 datastore = ast_datastore_alloc(&plc_ds_info, NULL);
5140 if (!datastore) {
5141 return;
5142 }
5143 plc = ast_calloc(1, sizeof(*plc));
5144 if (!plc) {
5145 ast_datastore_free(datastore);
5146 return;
5147 }
5148 datastore->data = plc;
5149 ast_channel_datastore_add(chan, datastore);
5150 adjust_frame_for_plc(chan, frame, datastore);
5151 }
5152
5153 static int tech_write(struct ast_channel *chan, struct ast_stream *stream,
5154 struct ast_stream *default_stream, struct ast_frame *frame)
5155 {
5156 if (ast_channel_tech(chan)->write_stream) {
5157 return stream ? ast_channel_tech(chan)->write_stream(
5158 chan, ast_stream_get_position(stream), frame) : 0;
5159 }
5160
5161 return ((stream == default_stream) && ast_channel_tech(chan)->write) ?
5162 ast_channel_tech(chan)->write(chan, frame) : 0;
5163 }
5164
5165 int ast_write(struct ast_channel *chan, struct ast_frame *fr)
5166 {
5167 return ast_write_stream(chan, -1, fr);
5168 }
5169
5170 int ast_write_stream(struct ast_channel *chan, int stream_num, struct ast_frame *fr)
5171 {
5172 struct ast_stream *stream = NULL, *default_stream = NULL;
5173 int res = -1;
5174 struct ast_frame *f = NULL;
5175 int count = 0;
5176 int hooked = 0;
5177
5178 /*Deadlock avoidance*/
5179 while(ast_channel_trylock(chan)) {
5180 /*cannot goto done since the channel is not locked*/
5181 if(count++ > 10) {
5182 ast_debug(1, "Deadlock avoided for write to channel '%s'\n", ast_channel_name(chan));
5183 return 0;
5184 }
5185 usleep(1);
5186 }
5187
5188 /* Stop if we're a zombie or need a soft hangup */
5189 if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_ZOMBIE) || ast_check_hangup(chan)) {
5190 goto done;
5191 }
5192
5193 if (stream_num >= 0) {
5194 /* If we were told to write to an explicit stream then allow this frame through, no matter
5195 * if the type is expected or not (a framehook could change)
5196 */
5197 if (stream_num >= ast_stream_topology_get_count(ast_channel_get_stream_topology(chan))) {
5198 goto done;
5199 }
5200 stream = ast_stream_topology_get_stream(ast_channel_get_stream_topology(chan), stream_num);
5201 default_stream = ast_channel_get_default_stream(chan, ast_stream_get_type(stream));
5202 } else if (fr->frametype == AST_FRAME_VOICE || fr->frametype == AST_FRAME_VIDEO || fr->frametype == AST_FRAME_MODEM) {
5203 /* If we haven't been told of a stream then we need to figure out which once we need */
5204 enum ast_media_type type = AST_MEDIA_TYPE_UNKNOWN;
5205
5206 /* Some frame types have a fixed media type */
5207 if (fr->frametype == AST_FRAME_VOICE || fr->frametype == AST_FRAME_VIDEO) {
5208 type = ast_format_get_type(fr->subclass.format);
5209 } else if (fr->frametype == AST_FRAME_MODEM) {
5210 type = AST_MEDIA_TYPE_IMAGE;
5211 }
5212
5213 /* No stream was specified, so use the default one */
5214 stream = default_stream = ast_channel_get_default_stream(chan, type);
5215 }
5216
5217 /* Perform the framehook write event here. After the frame enters the framehook list
5218 * there is no telling what will happen, how awesome is that!!! */
5219 if ((stream == default_stream) && !(fr = ast_framehook_list_write_event(ast_channel_framehooks(chan), fr))) {
5220 res = 0;
5221 goto done;
5222 }
5223
5224 if (ast_channel_generatordata(chan) && (fr->frametype != AST_FRAME_RTCP) && (!fr->src || strcasecmp(fr->src, "ast_prod"))) {
5225 if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_WRITE_INT)) {
5226 ast_deactivate_generator(chan);
5227 } else {
5228 if (fr->frametype == AST_FRAME_DTMF_END) {
5229 /* There is a generator running while we're in the middle of a digit.
5230 * It's probably inband DTMF, so go ahead and pass it so it can
5231 * stop the generator */
5232 ast_channel_unlock(chan);
5233 res = ast_senddigit_end(chan, fr->subclass.integer, fr->len);
5234 ast_channel_lock(chan);
5235 } else if (fr->frametype == AST_FRAME_CONTROL
5236 && fr->subclass.integer == AST_CONTROL_UNHOLD) {
5237 /*
5238 * This is a side case where Echo is basically being called
5239 * and the person put themselves on hold and took themselves
5240 * off hold.
5241 */
5242 indicate_data_internal(chan, fr->subclass.integer, fr->data.ptr,
5243 fr->datalen);
5244 }
5245 res = 0; /* XXX explain, why 0 ? */
5246 goto done;
5247 }
5248 }
5249 /* High bit prints debugging */
5250 if (ast_channel_fout(chan) & DEBUGCHAN_FLAG)
5251 ast_frame_dump(ast_channel_name(chan), fr, ">>");
5252 switch (fr->frametype) {
5253 case AST_FRAME_CONTROL:
5254 indicate_data_internal(chan, fr->subclass.integer, fr->data.ptr, fr->datalen);
5255 res = 0;
5256 break;
5257 case AST_FRAME_DTMF_BEGIN:
5258 if (ast_channel_audiohooks(chan)) {
5259 struct ast_frame *old_frame = fr;
5260 fr = ast_audiohook_write_list(chan, ast_channel_audiohooks(chan), AST_AUDIOHOOK_DIRECTION_WRITE, fr);
5261 if (old_frame != fr)
5262 f = fr;
5263 }
5264 send_dtmf_begin_event(chan, DTMF_SENT, fr->subclass.integer);
5265 ast_channel_unlock(chan);
5266 res = ast_senddigit_begin(chan, fr->subclass.integer);
5267 ast_channel_lock(chan);
5268 break;
5269 case AST_FRAME_DTMF_END:
5270 if (ast_channel_audiohooks(chan)) {
5271 struct ast_frame *new_frame = fr;
5272
5273 new_frame = ast_audiohook_write_list(chan, ast_channel_audiohooks(chan), AST_AUDIOHOOK_DIRECTION_WRITE, fr);
5274 if (new_frame != fr) {
5275 ast_frfree(new_frame);
5276 }
5277 }
5278 send_dtmf_end_event(chan, DTMF_SENT, fr->subclass.integer, fr->len);
5279 ast_channel_unlock(chan);
5280 res = ast_senddigit_end(chan, fr->subclass.integer, fr->len);
5281 ast_channel_lock(chan);
5282 break;
5283 case AST_FRAME_TEXT:
5284 CHECK_BLOCKING(chan);
5285 if (ast_format_cmp(fr->subclass.format, ast_format_t140) == AST_FORMAT_CMP_EQUAL) {
5286 res = (ast_channel_tech(chan)->write_text == NULL) ? 0 :
5287 ast_channel_tech(chan)->write_text(chan, fr);
5288 } else {
5289 res = (ast_channel_tech(chan)->send_text == NULL) ? 0 :
5290 ast_channel_tech(chan)->send_text(chan, (char *) fr->data.ptr);
5291 }
5292 ast_clear_flag(ast_channel_flags(chan), AST_FLAG_BLOCKING);
5293 break;
5294 case AST_FRAME_HTML:
5295 CHECK_BLOCKING(chan);
5296 res = (ast_channel_tech(chan)->send_html == NULL) ? 0 :
5297 ast_channel_tech(chan)->send_html(chan, fr->subclass.integer, (char *) fr->data.ptr, fr->datalen);
5298 ast_clear_flag(ast_channel_flags(chan), AST_FLAG_BLOCKING);
5299 break;
5300 case AST_FRAME_VIDEO:
5301 /* XXX Handle translation of video codecs one day XXX */
5302 CHECK_BLOCKING(chan);
5303 if (ast_channel_tech(chan)->write_stream) {
5304 if (stream) {
5305 res = ast_channel_tech(chan)->write_stream(chan, ast_stream_get_position(stream), fr);
5306 } else {
5307 res = 0;
5308 }
5309 } else if ((stream == default_stream) && ast_channel_tech(chan)->write_video) {
5310 res = ast_channel_tech(chan)->write_video(chan, fr);
5311 } else {
5312 res = 0;
5313 }
5314 ast_clear_flag(ast_channel_flags(chan), AST_FLAG_BLOCKING);
5315 break;
5316 case AST_FRAME_MODEM:
5317 CHECK_BLOCKING(chan);
5318 res = tech_write(chan, stream, default_stream, fr);
5319 ast_clear_flag(ast_channel_flags(chan), AST_FLAG_BLOCKING);
5320 break;
5321 case AST_FRAME_VOICE:
5322 if (ast_opt_generic_plc && ast_format_cmp(fr->subclass.format, ast_format_slin) == AST_FORMAT_CMP_EQUAL) {
5323 apply_plc(chan, fr);
5324 }
5325
5326 f = fr;
5327
5328 /*
5329 * Send frame to audiohooks if present, if frametype is linear (else, later as per
5330 * previous behavior)
5331 */
5332 if ((stream == default_stream) && ast_channel_audiohooks(chan)) {
5333 if (ast_format_cache_is_slinear(fr->subclass.format)) {
5334 hooked = 1;
5335 f = ast_audiohook_write_list(chan, ast_channel_audiohooks(chan), AST_AUDIOHOOK_DIRECTION_WRITE, fr);
5336 }
5337 }
5338
5339 /* If the frame is in the raw write format, then it's easy... just use the frame - otherwise we will have to translate */
5340 if ((stream == default_stream) && ast_format_cmp(fr->subclass.format, ast_channel_rawwriteformat(chan)) != AST_FORMAT_CMP_EQUAL) {
5341 if (ast_format_cmp(ast_channel_writeformat(chan), fr->subclass.format) != AST_FORMAT_CMP_EQUAL) {
5342 struct ast_str *codec_buf = ast_str_alloca(AST_FORMAT_CAP_NAMES_LEN);
5343
5344 /*
5345 * We are not setup to write this frame. Things may have changed
5346 * on the peer side of the world and we try to adjust the format to
5347 * make it compatible again. However, bad things can happen if we
5348 * cannot setup a new translation path. Problems range from no
5349 * audio, one-way audio, to garbled audio. The best we can do is
5350 * request the call to hangup since we could not make it compatible.
5351 *
5352 * Being continuously spammed by this message likely indicates a
5353 * problem with the peer because it cannot make up its mind about
5354 * which format to use.
5355 */
5356 ast_debug(1, "Channel %s changing write format from %s to %s, native formats %s\n",
5357 ast_channel_name(chan),
5358 ast_format_get_name(ast_channel_writeformat(chan)),
5359 ast_format_get_name(fr->subclass.format),
5360 ast_format_cap_get_names(ast_channel_nativeformats(chan), &codec_buf));
5361 if (ast_set_write_format(chan, fr->subclass.format)) {
5362 /* Could not handle the new write format. Induce a hangup. */
5363 break;
5364 }
5365 }
5366
5367 if (ast_channel_writetrans(chan)) {
5368 struct ast_frame *trans_frame = ast_translate(ast_channel_writetrans(chan), f, 0);
5369 if (trans_frame != f && f != fr) {
5370 /*
5371 * If translate gives us a new frame and so did the audio
5372 * hook then we need to free the one from the audio hook.
5373 */
5374 ast_frfree(f);
5375 }
5376 f = trans_frame;
5377 }
5378 }
5379
5380 if (!f) {
5381 res = 0;
5382 break;
5383 }
5384
5385 if ((stream == default_stream) && ast_channel_audiohooks(chan) && !hooked) {
5386 struct ast_frame *prev = NULL, *new_frame, *cur, *dup;
5387 int freeoldlist = 0;
5388
5389 if (f != fr) {
5390 freeoldlist = 1;
5391 }
5392
5393 /* Since ast_audiohook_write may return a new frame, and the cur frame is
5394 * an item in a list of frames, create a new list adding each cur frame back to it
5395 * regardless if the cur frame changes or not. */
5396 for (cur = f; cur; cur = AST_LIST_NEXT(cur, frame_list)) {
5397 new_frame = ast_audiohook_write_list(chan, ast_channel_audiohooks(chan), AST_AUDIOHOOK_DIRECTION_WRITE, cur);
5398
5399 /* if this frame is different than cur, preserve the end of the list,
5400 * free the old frames, and set cur to be the new frame */
5401 if (new_frame != cur) {
5402
5403 /* doing an ast_frisolate here seems silly, but we are not guaranteed the new_frame
5404 * isn't part of local storage, meaning if ast_audiohook_write is called multiple
5405 * times it may override the previous frame we got from it unless we dup it */
5406 if ((dup = ast_frisolate(new_frame))) {
5407 AST_LIST_NEXT(dup, frame_list) = AST_LIST_NEXT(cur, frame_list);
5408 if (freeoldlist) {
5409 AST_LIST_NEXT(cur, frame_list) = NULL;
5410 ast_frfree(cur);
5411 }
5412 if (new_frame != dup) {
5413 ast_frfree(new_frame);
5414 }
5415 cur = dup;
5416 }
5417 }
5418
5419 /* now, regardless if cur is new or not, add it to the new list,
5420 * if the new list has not started, cur will become the first item. */
5421 if (prev) {
5422 AST_LIST_NEXT(prev, frame_list) = cur;
5423 } else {
5424 f = cur; /* set f to be the beginning of our new list */
5425 }
5426 prev = cur;
5427 }
5428 }
5429
5430 /* the translator on chan->writetrans may have returned multiple frames
5431 from the single frame we passed in; if so, feed each one of them to the
5432 channel, freeing each one after it has been written */
5433 CHECK_BLOCKING(chan);
5434 if ((f != fr) && AST_LIST_NEXT(f, frame_list)) {
5435 struct ast_frame *cur, *next = NULL;
5436 unsigned int skip = 0;
5437
5438 cur = f;
5439 while (cur) {
5440 next = AST_LIST_NEXT(cur, frame_list);
5441 AST_LIST_NEXT(cur, frame_list) = NULL;
5442 if (!skip) {
5443 res = tech_write(chan, stream, default_stream, cur);
5444 if (res < 0) {
5445 ast_channel_softhangup_internal_flag_add(chan, AST_SOFTHANGUP_DEV);
5446 skip = 1;
5447 } else if (next) {
5448 /* don't do this for the last frame in the list,
5449 as the code outside the loop will do it once
5450 */
5451 ast_channel_fout_set(chan, FRAMECOUNT_INC(ast_channel_fout(chan)));
5452 }
5453 }
5454 ast_frfree(cur);
5455 cur = next;
5456 }
5457
5458 /* reset f so the code below doesn't attempt to free it */
5459 f = NULL;
5460 } else {
5461 res = tech_write(chan, stream, default_stream, f);
5462 }
5463 ast_clear_flag(ast_channel_flags(chan), AST_FLAG_BLOCKING);
5464 break;
5465 case AST_FRAME_NULL:
5466 case AST_FRAME_IAX:
5467 /* Ignore these */
5468 res = 0;
5469 break;
5470 case AST_FRAME_RTCP:
5471 /* RTCP information is on a per-stream basis and only available on multistream capable channels */
5472 CHECK_BLOCKING(chan);
5473 if (ast_channel_tech(chan)->write_stream && stream) {
5474 res = ast_channel_tech(chan)->write_stream(chan, ast_stream_get_position(stream), fr);
5475 } else {
5476 res = 0;
5477 }
5478 ast_clear_flag(ast_channel_flags(chan), AST_FLAG_BLOCKING);
5479 break;
5480 default:
5481 /* At this point, fr is the incoming frame and f is NULL. Channels do
5482 * not expect to get NULL as a frame pointer and will segfault. Hence,
5483 * we output the original frame passed in. */
5484 CHECK_BLOCKING(chan);
5485 res = ast_channel_tech(chan)->write(chan, fr);
5486 ast_clear_flag(ast_channel_flags(chan), AST_FLAG_BLOCKING);
5487 break;
5488 }
5489
5490 if (f && f != fr)
5491 ast_frfree(f);
5492
5493 /* Consider a write failure to force a soft hangup */
5494 if (res < 0) {
5495 ast_channel_softhangup_internal_flag_add(chan, AST_SOFTHANGUP_DEV);
5496 } else {
5497 ast_channel_fout_set(chan, FRAMECOUNT_INC(ast_channel_fout(chan)));
5498 }
5499 done:
5500 if (ast_channel_audiohooks(chan) && ast_audiohook_write_list_empty(ast_channel_audiohooks(chan))) {
5501 /* The list gets recreated if audiohooks are added again later */
5502 ast_audiohook_detach_list(ast_channel_audiohooks(chan));
5503 ast_channel_audiohooks_set(chan, NULL);
5504 }
5505 ast_channel_unlock(chan);
5506 return res;
5507 }
5508
5509 int ast_set_read_format_path(struct ast_channel *chan, struct ast_format *raw_format, struct ast_format *core_format)
5510 {
5511 struct ast_trans_pvt *trans_old;
5512 struct ast_trans_pvt *trans_new;
5513
5514 if (ast_format_cmp(ast_channel_rawreadformat(chan), raw_format) == AST_FORMAT_CMP_EQUAL
5515 && ast_format_cmp(ast_channel_readformat(chan), core_format) == AST_FORMAT_CMP_EQUAL) {
5516 /* Nothing to setup */
5517 return 0;
5518 }
5519
5520 ast_debug(1, "Channel %s setting read format path: %s -> %s\n",
5521 ast_channel_name(chan),
5522 ast_format_get_name(raw_format),
5523 ast_format_get_name(core_format));
5524
5525 /* Setup new translation path. */
5526 if (ast_format_cmp(raw_format, core_format) != AST_FORMAT_CMP_EQUAL) {
5527 trans_new = ast_translator_build_path(core_format, raw_format);
5528 if (!trans_new) {
5529 return -1;
5530 }
5531 } else {
5532 /* No translation needed. */
5533 trans_new = NULL;
5534 }
5535 trans_old = ast_channel_readtrans(chan);
5536 if (trans_old) {
5537 ast_translator_free_path(trans_old);
5538 }
5539 ast_channel_readtrans_set(chan, trans_new);
5540 ast_channel_set_rawreadformat(chan, raw_format);
5541 ast_channel_set_readformat(chan, core_format);
5542 return 0;
5543 }
5544
5545 int ast_set_write_format_path(struct ast_channel *chan, struct ast_format *core_format, struct ast_format *raw_format)
5546 {
5547 struct ast_trans_pvt *trans_old;
5548 struct ast_trans_pvt *trans_new;
5549
5550 if (ast_format_cmp(ast_channel_rawwriteformat(chan), raw_format) == AST_FORMAT_CMP_EQUAL
5551 && ast_format_cmp(ast_channel_writeformat(chan), core_format) == AST_FORMAT_CMP_EQUAL) {
5552 /* Nothing to setup */
5553 return 0;
5554 }
5555
5556 ast_debug(1, "Channel %s setting write format path: %s -> %s\n",
5557 ast_channel_name(chan),
5558 ast_format_get_name(core_format),
5559 ast_format_get_name(raw_format));
5560
5561 /* Setup new translation path. */
5562 if (ast_format_cmp(raw_format, core_format) != AST_FORMAT_CMP_EQUAL) {
5563 trans_new = ast_translator_build_path(raw_format, core_format);
5564 if (!trans_new) {
5565 return -1;
5566 }
5567 } else {
5568 /* No translation needed. */
5569 trans_new = NULL;
5570 }
5571 trans_old = ast_channel_writetrans(chan);
5572 if (trans_old) {
5573 ast_translator_free_path(trans_old);
5574 }
5575 ast_channel_writetrans_set(chan, trans_new);
5576 ast_channel_set_rawwriteformat(chan, raw_format);
5577 ast_channel_set_writeformat(chan, core_format);
5578 return 0;
5579 }
5580
5581 struct set_format_access {
5582 const char *direction;
5583 struct ast_trans_pvt *(*get_trans)(const struct ast_channel *chan);
5584 void (*set_trans)(struct ast_channel *chan, struct ast_trans_pvt *value);
5585 struct ast_format *(*get_format)(struct ast_channel *chan);
5586 void (*set_format)(struct ast_channel *chan, struct ast_format *format);
5587 struct ast_format *(*get_rawformat)(struct ast_channel *chan);
5588 void (*set_rawformat)(struct ast_channel *chan, struct ast_format *format);
5589 int setoption;
5590 };
5591
5592 static const struct set_format_access set_format_access_read = {
5593 .direction = "read",
5594 .get_trans = ast_channel_readtrans,
5595 .set_trans = ast_channel_readtrans_set,
5596 .get_format = ast_channel_readformat,
5597 .set_format = ast_channel_set_readformat,
5598 .get_rawformat = ast_channel_rawreadformat,
5599 .set_rawformat = ast_channel_set_rawreadformat,
5600 .setoption = AST_OPTION_FORMAT_READ,
5601 };
5602
5603 static const struct set_format_access set_format_access_write = {
5604 .direction = "write",
5605 .get_trans = ast_channel_writetrans,
5606 .set_trans = ast_channel_writetrans_set,
5607 .get_format = ast_channel_writeformat,
5608 .set_format = ast_channel_set_writeformat,
5609 .get_rawformat = ast_channel_rawwriteformat,
5610 .set_rawformat = ast_channel_set_rawwriteformat,
5611 .setoption = AST_OPTION_FORMAT_WRITE,
5612 };
5613
5614 static int set_format(struct ast_channel *chan, struct ast_format_cap *cap_set, const int direction, int interleaved_stereo)
5615 {
5616 struct ast_trans_pvt *trans_pvt;
5617 struct ast_format_cap *cap_native;
5618 const struct set_format_access *access;
5619 struct ast_format *rawformat;
5620 struct ast_format *format;
5621 RAII_VAR(struct ast_format *, best_set_fmt, NULL, ao2_cleanup);
5622 RAII_VAR(struct ast_format *, best_native_fmt, NULL, ao2_cleanup);
5623 int res;
5624
5625 if (!direction) {
5626 /* reading */
5627 access = &set_format_access_read;
5628 } else {
5629 /* writing */
5630 access = &set_format_access_write;
5631 }
5632
5633 best_set_fmt = ast_format_cap_get_best_by_type(cap_set, AST_MEDIA_TYPE_AUDIO);
5634 if (!best_set_fmt) {
5635 /*
5636 * Not setting any audio formats?
5637 * Assume a call without any sounds (video, text)
5638 */
5639 return 0;
5640 }
5641
5642 /* See if the underlying channel driver is capable of performing transcoding for us */
5643 res = ast_channel_setoption(chan, access->setoption,
5644 &best_set_fmt, sizeof(best_set_fmt), 0);
5645 if (!res) {
5646 ast_debug(1, "Channel driver natively set channel %s to %s format %s\n",
5647 ast_channel_name(chan), access->direction, ast_format_get_name(best_set_fmt));
5648
5649 ast_channel_lock(chan);
5650 cap_native = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
5651 if (!cap_native
5652 || ast_format_cap_append(cap_native, best_set_fmt, 0)) {
5653 ast_channel_unlock(chan);
5654 ao2_cleanup(cap_native);
5655 return -1;
5656 }
5657 ast_channel_nativeformats_set(chan, cap_native);
5658 ao2_cleanup(cap_native);
5659 access->set_format(chan, best_set_fmt);
5660 access->set_rawformat(chan, best_set_fmt);
5661
5662 trans_pvt = access->get_trans(chan);
5663 if (trans_pvt) {
5664 ast_translator_free_path(trans_pvt);
5665 access->set_trans(chan, NULL);
5666 }
5667 ast_channel_unlock(chan);
5668
5669 /* If there is a generator on the channel, it needs to know about this
5670 * change if it is the write format. */
5671 if (direction && ast_channel_generatordata(chan)) {
5672 generator_write_format_change(chan);
5673 }
5674
5675 return 0;
5676 }
5677
5678 ast_channel_lock(chan);
5679
5680 format = access->get_format(chan);
5681 rawformat = access->get_rawformat(chan);
5682 ast_assert(format != NULL);
5683 ast_assert(rawformat != NULL);
5684
5685 cap_native = ast_channel_nativeformats(chan);
5686 if (ast_format_cap_empty(cap_native)) {
5687 ast_channel_unlock(chan);
5688 ast_log(LOG_ERROR, "Unable to set format because channel %s supports no formats\n",
5689 ast_channel_name(chan));
5690 return -1;
5691 }
5692
5693 /* Find a translation path from the native format to one of the desired formats */
5694 if (!direction) {
5695 /* reading */
5696 res = ast_translator_best_choice(cap_set, cap_native, &best_set_fmt, &best_native_fmt);
5697 } else {
5698 /* writing */
5699 res = ast_translator_best_choice(cap_native, cap_set, &best_native_fmt, &best_set_fmt);
5700 }
5701 if (res < 0) {
5702 struct ast_str *codec_native = ast_str_alloca(AST_FORMAT_CAP_NAMES_LEN);
5703 struct ast_str *codec_set = ast_str_alloca(AST_FORMAT_CAP_NAMES_LEN);
5704
5705 ast_format_cap_get_names(cap_native, &codec_native);
5706 ast_channel_unlock(chan);
5707 ast_format_cap_get_names(cap_set, &codec_set);
5708
5709 ast_log(LOG_WARNING, "Unable to find a codec translation path: %s -> %s\n",
5710 ast_str_buffer(direction ? codec_set : codec_native),
5711 ast_str_buffer(direction ? codec_native : codec_set));
5712 return -1;
5713 }
5714
5715 /* Now we have a good choice for both. */
5716 trans_pvt = access->get_trans(chan);
5717 if ((ast_format_cmp(rawformat, best_native_fmt) != AST_FORMAT_CMP_NOT_EQUAL) &&
5718 (ast_format_cmp(format, best_set_fmt) != AST_FORMAT_CMP_NOT_EQUAL) &&
5719 ((ast_format_cmp(rawformat, format) != AST_FORMAT_CMP_NOT_EQUAL) || access->get_trans(chan))) {
5720 /* the channel is already in these formats, so nothing to do, unless the interleaved format is not set correctly */
5721 if (trans_pvt != NULL) {
5722 if (trans_pvt->interleaved_stereo == interleaved_stereo) {
5723 ast_channel_unlock(chan);
5724 return 0;
5725 }
5726 }
5727 }
5728
5729 /* Free any translation we have right now */
5730 if (trans_pvt) {
5731 ast_translator_free_path(trans_pvt);
5732 access->set_trans(chan, NULL);
5733 }
5734
5735 /* Build a translation path from the raw format to the desired format */
5736 if (ast_format_cmp(best_set_fmt, best_native_fmt) != AST_FORMAT_CMP_NOT_EQUAL) {
5737 /*
5738 * If we were able to swap the native format to the format that
5739 * has been requested, then there is no need to try to build
5740 * a translation path.
5741 */
5742 res = 0;
5743 } else {
5744 if (!direction) {
5745 /* reading */
5746 trans_pvt = ast_translator_build_path(best_set_fmt, best_native_fmt);
5747 if (trans_pvt) {
5748 trans_pvt->interleaved_stereo = 0;
5749 }
5750 } else {
5751 /* writing */
5752 trans_pvt = ast_translator_build_path(best_native_fmt, best_set_fmt);
5753 if (trans_pvt) {
5754 trans_pvt->interleaved_stereo = interleaved_stereo;
5755 }
5756 }
5757 access->set_trans(chan, trans_pvt);
5758 res = trans_pvt ? 0 : -1;
5759 }
5760
5761 if (!res) {
5762 access->set_format(chan, best_set_fmt);
5763 access->set_rawformat(chan, best_native_fmt);
5764
5765 ast_debug(1, "Channel %s setting %s format path: %s -> %s\n",
5766 ast_channel_name(chan),
5767 access->direction,
5768 ast_format_get_name(direction ? best_set_fmt : best_native_fmt),
5769 ast_format_get_name(direction ? best_native_fmt : best_set_fmt));
5770 }
5771
5772 ast_channel_unlock(chan);
5773
5774 /* If there is a generator on the channel, it needs to know about this
5775 * change if it is the write format. */
5776 if (direction && ast_channel_generatordata(chan)) {
5777 generator_write_format_change(chan);
5778 }
5779
5780 return res;
5781 }
5782
5783 int ast_set_read_format(struct ast_channel *chan, struct ast_format *format)
5784 {
5785 struct ast_format_cap *cap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
5786 int res;
5787
5788 ast_assert(format != NULL);
5789
5790 if (!cap) {
5791 return -1;
5792 }
5793 ast_format_cap_append(cap, format, 0);
5794
5795 res = set_format(chan, cap, 0, 0);
5796
5797 ao2_cleanup(cap);
5798 return res;
5799 }
5800
5801 int ast_set_read_format_from_cap(struct ast_channel *chan, struct ast_format_cap *cap)
5802 {
5803 return set_format(chan, cap, 0, 0);
5804 }
5805
5806 int ast_set_write_format_interleaved_stereo(struct ast_channel *chan, struct ast_format *format)
5807 {
5808 struct ast_format_cap *cap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
5809 int res;
5810
5811 ast_assert(format != NULL);
5812
5813 if (!cap) {
5814 return -1;
5815 }
5816 ast_format_cap_append(cap, format, 0);
5817
5818 res = set_format(chan, cap, 1, 1);
5819
5820 ao2_cleanup(cap);
5821 return res;
5822 }
5823
5824 int ast_set_write_format(struct ast_channel *chan, struct ast_format *format)
5825 {
5826 struct ast_format_cap *cap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
5827 int res;
5828
5829 ast_assert(format != NULL);
5830
5831 if (!cap) {
5832 return -1;
5833 }
5834 ast_format_cap_append(cap, format, 0);
5835
5836 res = set_format(chan, cap, 1, 0);
5837
5838 ao2_cleanup(cap);
5839 return res;
5840 }
5841
5842 int ast_set_write_format_from_cap(struct ast_channel *chan, struct ast_format_cap *cap)
5843 {
5844 return set_format(chan, cap, 1, 0);
5845 }
5846
5847 const char *ast_channel_reason2str(int reason)
5848 {
5849 switch (reason) /* the following appear to be the only ones actually returned by request_and_dial */
5850 {
5851 case 0:
5852 return "Call Failure (not BUSY, and not NO_ANSWER, maybe Circuit busy or down?)";
5853 case AST_CONTROL_HANGUP:
5854 return "Hangup";
5855 case AST_CONTROL_RING:
5856 return "Local Ring";
5857 case AST_CONTROL_RINGING:
5858 return "Remote end Ringing";
5859 case AST_CONTROL_ANSWER:
5860 return "Remote end has Answered";
5861 case AST_CONTROL_BUSY:
5862 return "Remote end is Busy";
5863 case AST_CONTROL_CONGESTION:
5864 return "Congestion (circuits busy)";
5865 default:
5866 return "Unknown Reason!!";
5867 }
5868 }
5869
5870 static void handle_cause(int cause, int *outstate)
5871 {
5872 if (outstate) {
5873 /* compute error and return */
5874 if (cause == AST_CAUSE_BUSY)
5875 *outstate = AST_CONTROL_BUSY;
5876 else if (cause == AST_CAUSE_CONGESTION)
5877 *outstate = AST_CONTROL_CONGESTION;
5878 else
5879 *outstate = 0;
5880 }
5881 }
5882
5883 /*!
5884 * \internal
5885 * \brief Helper function to inherit info from parent channel.
5886 *
5887 * \param new_chan Channel inheriting information.
5888 * \param parent Channel new_chan inherits information.
5889 * \param orig Channel being replaced by the call forward channel.
5890 */
5891 static void call_forward_inherit(struct ast_channel *new_chan, struct ast_channel *parent, struct ast_channel *orig)
5892 {
5893 if (!ast_test_flag(ast_channel_flags(parent), AST_FLAG_ZOMBIE) && !ast_check_hangup(parent)) {
5894 struct ast_party_redirecting redirecting;
5895
5896 /*
5897 * The parent is not a ZOMBIE or hungup so update it with the
5898 * original channel's redirecting information.
5899 */
5900 ast_party_redirecting_init(&redirecting);
5901 ast_channel_lock(orig);
5902 ast_party_redirecting_copy(&redirecting, ast_channel_redirecting(orig));
5903 ast_channel_unlock(orig);
5904 if (ast_channel_redirecting_sub(orig, parent, &redirecting, 0)) {
5905 ast_channel_update_redirecting(parent, &redirecting, NULL);
5906 }
5907 ast_party_redirecting_free(&redirecting);
5908 }
5909
5910 /* Safely inherit variables and datastores from the parent channel. */
5911 ast_channel_lock_both(parent, new_chan);
5912 ast_channel_inherit_variables(parent, new_chan);
5913 ast_channel_datastore_inherit(parent, new_chan);
5914 ast_max_forwards_decrement(new_chan);
5915 ast_channel_unlock(new_chan);
5916 ast_channel_unlock(parent);
5917 }
5918
5919 struct ast_channel *ast_call_forward(struct ast_channel *caller, struct ast_channel *orig, int *timeout, struct ast_format_cap *cap, struct outgoing_helper *oh, int *outstate)
5920 {
5921 char tmpchan[256];
5922 char forwarder[AST_CHANNEL_NAME];
5923 struct ast_channel *new_chan = NULL;
5924 char *data, *type;
5925 int cause = 0;
5926 int res;
5927
5928 /* gather data and request the new forward channel */
5929 ast_copy_string(tmpchan, ast_channel_call_forward(orig), sizeof(tmpchan));
5930 ast_copy_string(forwarder, ast_channel_name(orig), sizeof(forwarder));
5931 if ((data = strchr(tmpchan, '/'))) {
5932 *data++ = '\0';
5933 type = tmpchan;
5934 } else {
5935 const char *forward_context;
5936 ast_channel_lock(orig);
5937 forward_context = pbx_builtin_getvar_helper(orig, "FORWARD_CONTEXT");
5938 snprintf(tmpchan, sizeof(tmpchan), "%s@%s", ast_channel_call_forward(orig), S_OR(forward_context, ast_channel_context(orig)));
5939 ast_channel_unlock(orig);
5940 data = tmpchan;
5941 type = "Local";
5942 }
5943 if (!(new_chan = ast_request(type, cap, NULL, orig, data, &cause))) {
5944 ast_log(LOG_NOTICE, "Unable to create channel for call forward to '%s/%s' (cause = %d)\n", type, data, cause);
5945 handle_cause(cause, outstate);
5946 ast_hangup(orig);
5947 return NULL;
5948 }
5949
5950 /* Copy/inherit important information into new channel */
5951 if (oh) {
5952 if (oh->vars) {
5953 ast_set_variables(new_chan, oh->vars);
5954 }
5955 if (oh->parent_channel) {
5956 call_forward_inherit(new_chan, oh->parent_channel, orig);
5957 }
5958 if (!ast_strlen_zero(oh->account)) {
5959 ast_channel_lock(new_chan);
5960 ast_channel_stage_snapshot(new_chan);
5961 ast_channel_accountcode_set(new_chan, oh->account);
5962 ast_channel_peeraccount_set(new_chan, oh->account);
5963 ast_channel_stage_snapshot_done(new_chan);
5964 ast_channel_unlock(new_chan);
5965 }
5966 } else if (caller) { /* no outgoing helper so use caller if available */
5967 call_forward_inherit(new_chan, caller, orig);
5968 }
5969
5970 ast_channel_lock_both(orig, new_chan);
5971 ast_channel_set_flag(new_chan, AST_FLAG_ORIGINATED);
5972 pbx_builtin_setvar_helper(new_chan, "FORWARDERNAME", forwarder);
5973 ast_party_connected_line_copy(ast_channel_connected(new_chan), ast_channel_connected(orig));
5974 ast_party_redirecting_copy(ast_channel_redirecting(new_chan), ast_channel_redirecting(orig));
5975 ast_channel_req_accountcodes(new_chan, orig, AST_CHANNEL_REQUESTOR_REPLACEMENT);
5976 ast_channel_unlock(new_chan);
5977 ast_channel_unlock(orig);
5978
5979 /* call new channel */
5980 res = ast_call(new_chan, data, 0);
5981 if (timeout) {
5982 *timeout = res;
5983 }
5984 if (res) {
5985 ast_log(LOG_NOTICE, "Unable to call forward to channel %s/%s\n", type, (char *)data);
5986 ast_hangup(orig);
5987 ast_hangup(new_chan);
5988 return NULL;
5989 }
5990 ast_hangup(orig);
5991
5992 return new_chan;
5993 }
5994
5995 struct ast_channel *__ast_request_and_dial(const char *type, struct ast_format_cap *cap, const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor, const char *addr, int timeout, int *outstate, const char *cid_num, const char *cid_name, struct outgoing_helper *oh)
5996 {
5997 int dummy_outstate;
5998 int cause = 0;
5999 struct ast_channel *chan;
6000 int res = 0;
6001 int last_subclass = 0;
6002 struct ast_party_connected_line connected;
6003
6004 if (outstate)
6005 *outstate = 0;
6006 else
6007 outstate = &dummy_outstate; /* make outstate always a valid pointer */
6008
6009 chan = ast_request(type, cap, assignedids, requestor, addr, &cause);
6010 if (!chan) {
6011 ast_log(LOG_NOTICE, "Unable to request channel %s/%s\n", type, addr);
6012 handle_cause(cause, outstate);
6013 return NULL;
6014 }
6015
6016 if (oh) {
6017 if (oh->vars) {
6018 ast_set_variables(chan, oh->vars);
6019 }
6020 if (!ast_strlen_zero(oh->cid_num) && !ast_strlen_zero(oh->cid_name)) {
6021 /*
6022 * Use the oh values instead of the function parameters for the
6023 * outgoing CallerID.
6024 */
6025 cid_num = oh->cid_num;
6026 cid_name = oh->cid_name;
6027 }
6028 if (oh->parent_channel) {
6029 /* Safely inherit variables and datastores from the parent channel. */
6030 ast_channel_lock_both(oh->parent_channel, chan);
6031 ast_channel_inherit_variables(oh->parent_channel, chan);
6032 ast_channel_datastore_inherit(oh->parent_channel, chan);
6033 ast_max_forwards_decrement(chan);
6034 ast_channel_unlock(oh->parent_channel);
6035 ast_channel_unlock(chan);
6036 }
6037 if (!ast_strlen_zero(oh->account)) {
6038 ast_channel_lock(chan);
6039 ast_channel_stage_snapshot(chan);
6040 ast_channel_accountcode_set(chan, oh->account);
6041 ast_channel_peeraccount_set(chan, oh->account);
6042 ast_channel_stage_snapshot_done(chan);
6043 ast_channel_unlock(chan);
6044 }
6045 }
6046
6047 /*
6048 * It seems strange to set the CallerID on an outgoing call leg
6049 * to whom we are calling, but this function's callers are doing
6050 * various Originate methods. This call leg goes to the local
6051 * user. Once the local user answers, the dialplan needs to be
6052 * able to access the CallerID from the CALLERID function as if
6053 * the local user had placed this call.
6054 */
6055 ast_set_callerid(chan, cid_num, cid_name, cid_num);
6056
6057 ast_channel_lock(chan);
6058 ast_set_flag(ast_channel_flags(chan), AST_FLAG_ORIGINATED);
6059 ast_channel_unlock(chan);
6060 ast_party_connected_line_set_init(&connected, ast_channel_connected(chan));
6061 if (cid_num) {
6062 connected.id.number.valid = 1;
6063 connected.id.number.str = (char *) cid_num;
6064 connected.id.number.presentation = AST_PRES_ALLOWED_USER_NUMBER_NOT_SCREENED;
6065 }
6066 if (cid_name) {
6067 connected.id.name.valid = 1;
6068 connected.id.name.str = (char *) cid_name;
6069 connected.id.name.presentation = AST_PRES_ALLOWED_USER_NUMBER_NOT_SCREENED;
6070 }
6071 ast_channel_set_connected_line(chan, &connected, NULL);
6072 if (requestor) {
6073 ast_channel_lock_both(chan, (struct ast_channel *) requestor);
6074 ast_channel_req_accountcodes(chan, requestor, AST_CHANNEL_REQUESTOR_BRIDGE_PEER);
6075 ast_channel_unlock(chan);
6076 ast_channel_unlock((struct ast_channel *) requestor);
6077 }
6078
6079 if (ast_call(chan, addr, 0)) { /* ast_call failed... */
6080 ast_log(LOG_NOTICE, "Unable to call channel %s/%s\n", type, addr);
6081 } else {
6082 struct timeval start = ast_tvnow();
6083 res = 1; /* mark success in case chan->_state is already AST_STATE_UP */
6084 while (timeout && ast_channel_state(chan) != AST_STATE_UP) {
6085 struct ast_frame *f;
6086 int ms = ast_remaining_ms(start, timeout);
6087
6088 res = ast_waitfor(chan, ms);
6089 if (res == 0) { /* timeout, treat it like ringing */
6090 *outstate = AST_CONTROL_RINGING;
6091 break;
6092 }
6093 if (res < 0) /* error or done */
6094 break;
6095 if (!ast_strlen_zero(ast_channel_call_forward(chan))) {
6096 if (!(chan = ast_call_forward(NULL, chan, NULL, cap, oh, outstate))) {
6097 return NULL;
6098 }
6099 continue;
6100 }
6101
6102 f = ast_read(chan);
6103 if (!f) {
6104 *outstate = AST_CONTROL_HANGUP;
6105 res = 0;
6106 break;
6107 }
6108 if (f->frametype == AST_FRAME_CONTROL) {
6109 switch (f->subclass.integer) {
6110 case AST_CONTROL_RINGING: /* record but keep going */
6111 *outstate = f->subclass.integer;
6112 break;
6113
6114 case AST_CONTROL_BUSY:
6115 *outstate = f->subclass.integer;
6116 timeout = 0;
6117 break;
6118
6119 case AST_CONTROL_INCOMPLETE:
6120 *outstate = AST_CONTROL_CONGESTION;
6121 timeout = 0;
6122 break;
6123
6124 case AST_CONTROL_CONGESTION:
6125 *outstate = f->subclass.integer;
6126 timeout = 0;
6127 break;
6128
6129 case AST_CONTROL_ANSWER:
6130 *outstate = f->subclass.integer;
6131 timeout = 0; /* trick to force exit from the while() */
6132 break;
6133
6134 case AST_CONTROL_PVT_CAUSE_CODE:
6135 ast_channel_hangupcause_hash_set(chan, f->data.ptr, f->datalen);
6136 break;
6137
6138 case AST_CONTROL_PROGRESS:
6139 if (oh && oh->connect_on_early_media) {
6140 *outstate = f->subclass.integer;
6141 timeout = 0; /* trick to force exit from the while() */
6142 break;
6143 }
6144 /* Fallthrough */
6145 /* Ignore these */
6146 case AST_CONTROL_PROCEEDING:
6147 case AST_CONTROL_HOLD:
6148 case AST_CONTROL_UNHOLD:
6149 case AST_CONTROL_VIDUPDATE:
6150 case AST_CONTROL_SRCUPDATE:
6151 case AST_CONTROL_SRCCHANGE:
6152 case AST_CONTROL_CONNECTED_LINE:
6153 case AST_CONTROL_REDIRECTING:
6154 case AST_CONTROL_CC:
6155 case -1: /* Ignore -- just stopping indications */
6156 break;
6157
6158 default:
6159 ast_log(LOG_NOTICE, "Don't know what to do with control frame %d\n", f->subclass.integer);
6160 }
6161 last_subclass = f->subclass.integer;
6162 }
6163 ast_frfree(f);
6164 }
6165 }
6166
6167 /* Final fixups */
6168 if (oh) {
6169 if (!ast_strlen_zero(oh->context))
6170 ast_channel_context_set(chan, oh->context);
6171 if (!ast_strlen_zero(oh->exten))
6172 ast_channel_exten_set(chan, oh->exten);
6173 if (oh->priority)
6174 ast_channel_priority_set(chan, oh->priority);
6175 }
6176 if (ast_channel_state(chan) == AST_STATE_UP)
6177 *outstate = AST_CONTROL_ANSWER;
6178
6179 if (res <= 0) {
6180 ast_channel_lock(chan);
6181 if (AST_CONTROL_RINGING == last_subclass) {
6182 ast_channel_hangupcause_set(chan, AST_CAUSE_NO_ANSWER);
6183 }
6184 ast_channel_unlock(chan);
6185 ast_hangup(chan);
6186 chan = NULL;
6187 }
6188 return chan;
6189 }
6190
6191 struct ast_channel *ast_request_and_dial(const char *type, struct ast_format_cap *cap, const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor, const char *addr, int timeout, int *outstate, const char *cidnum, const char *cidname)
6192 {
6193 return __ast_request_and_dial(type, cap, assignedids, requestor, addr, timeout, outstate, cidnum, cidname, NULL);
6194 }
6195
6196 static int set_security_requirements(const struct ast_channel *requestor, struct ast_channel *out)
6197 {
6198 int ops[2][2] = {
6199 {AST_OPTION_SECURE_SIGNALING, 0},
6200 {AST_OPTION_SECURE_MEDIA, 0},
6201 };
6202 int i;
6203 struct ast_channel *r = (struct ast_channel *) requestor; /* UGLY */
6204 struct ast_datastore *ds;
6205
6206 if (!requestor || !out) {
6207 return 0;
6208 }
6209
6210 ast_channel_lock(r);
6211 if ((ds = ast_channel_datastore_find(r, &secure_call_info, NULL))) {
6212 struct ast_secure_call_store *encrypt = ds->data;
6213 ops[0][1] = encrypt->signaling;
6214 ops[1][1] = encrypt->media;
6215 } else {
6216 ast_channel_unlock(r);
6217 return 0;
6218 }
6219 ast_channel_unlock(r);
6220
6221 for (i = 0; i < 2; i++) {
6222 if (ops[i][1]) {
6223 if (ast_channel_setoption(out, ops[i][0], &ops[i][1], sizeof(ops[i][1]), 0)) {
6224 /* We require a security feature, but the channel won't provide it */
6225 return -1;
6226 }
6227 } else {
6228 /* We don't care if we can't clear the option on a channel that doesn't support it */
6229 ast_channel_setoption(out, ops[i][0], &ops[i][1], sizeof(ops[i][1]), 0);
6230 }
6231 }
6232
6233 return 0;
6234 }
6235
6236 static struct ast_channel *request_channel(const char *type, struct ast_format_cap *request_cap, struct ast_stream_topology *topology,
6237 const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor, const char *addr, int *cause)
6238 {
6239 struct chanlist *chan;
6240 struct ast_channel *c = NULL;
6241 int res;
6242 int foo;
6243
6244 if (!cause)
6245 cause = &foo;
6246 *cause = AST_CAUSE_NOTDEFINED;
6247
6248 if (AST_RWLIST_RDLOCK(&backends)) {
6249 ast_log(LOG_WARNING, "Unable to lock technology backend list\n");
6250 return NULL;
6251 }
6252
6253 AST_RWLIST_TRAVERSE(&backends, chan, list) {
6254 if (strcasecmp(type, chan->tech->type)) {
6255 continue;
6256 }
6257
6258 break;
6259 }
6260
6261 AST_RWLIST_UNLOCK(&backends);
6262
6263 if (!chan) {
6264 ast_log(LOG_WARNING, "No channel type registered for '%s'\n", type);
6265 *cause = AST_CAUSE_NOSUCHDRIVER;
6266 return NULL;
6267 }
6268
6269 /* Allow either format capabilities or stream topology to be provided and adapt */
6270 if (chan->tech->requester_with_stream_topology) {
6271 struct ast_stream_topology *tmp_converted_topology = NULL;
6272
6273 if (!topology && request_cap) {
6274 /* Turn the requested capabilities into a stream topology */
6275 topology = tmp_converted_topology = ast_stream_topology_create_from_format_cap(request_cap);
6276 }
6277
6278 c = chan->tech->requester_with_stream_topology(type, topology, assignedids, requestor, addr, cause);
6279
6280 ast_stream_topology_free(tmp_converted_topology);
6281 } else if (chan->tech->requester) {
6282 struct ast_format_cap *tmp_converted_cap = NULL;
6283 struct ast_format_cap *tmp_cap;
6284 RAII_VAR(struct ast_format *, tmp_fmt, NULL, ao2_cleanup);
6285 RAII_VAR(struct ast_format *, best_audio_fmt, NULL, ao2_cleanup);
6286 struct ast_format_cap *joint_cap;
6287
6288 if (!request_cap && topology) {
6289 /* Turn the request stream topology into capabilities */
6290 request_cap = tmp_converted_cap = ast_stream_topology_get_formats(topology);
6291 }
6292
6293 /* find the best audio format to use */
6294 tmp_cap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
6295 if (!tmp_cap) {
6296 AST_RWLIST_UNLOCK(&backends);
6297 return NULL;
6298 }
6299
6300 ast_format_cap_append_from_cap(tmp_cap, request_cap, AST_MEDIA_TYPE_AUDIO);
6301 if (!ast_format_cap_empty(tmp_cap)) {
6302 /* We have audio - is it possible to connect the various calls to each other?
6303 (Avoid this check for calls without audio, like text+video calls)
6304 */
6305 res = ast_translator_best_choice(tmp_cap, chan->tech->capabilities, &tmp_fmt, &best_audio_fmt);
6306 ao2_ref(tmp_cap, -1);
6307 if (res < 0) {
6308 struct ast_str *tech_codecs = ast_str_alloca(AST_FORMAT_CAP_NAMES_LEN);
6309 struct ast_str *request_codecs = ast_str_alloca(AST_FORMAT_CAP_NAMES_LEN);
6310
6311 ast_log(LOG_WARNING, "No translator path exists for channel type %s (native %s) to %s\n", type,
6312 ast_format_cap_get_names(chan->tech->capabilities, &tech_codecs),
6313 ast_format_cap_get_names(request_cap, &request_codecs));
6314 *cause = AST_CAUSE_BEARERCAPABILITY_NOTAVAIL;
6315 ao2_cleanup(tmp_converted_cap);
6316 return NULL;
6317 }
6318 }
6319
6320 /* XXX Only the audio format calculated as being the best for translation
6321 * purposes is used for the request. This is because we don't have the ability
6322 * to signal to the initiator which one of their codecs that was offered is
6323 * the one that was selected, particularly in a chain of Local channels.
6324 */
6325 joint_cap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
6326 if (!joint_cap) {
6327 ao2_cleanup(tmp_converted_cap);
6328 return NULL;
6329 }
6330 ast_format_cap_append_from_cap(joint_cap, request_cap, AST_MEDIA_TYPE_UNKNOWN);
6331 ast_format_cap_remove_by_type(joint_cap, AST_MEDIA_TYPE_AUDIO);
6332 if (best_audio_fmt) { /* text+video call? then, this is NULL */
6333 ast_format_cap_append(joint_cap, best_audio_fmt, 0);
6334 }
6335 ao2_cleanup(tmp_converted_cap);
6336
6337 c = chan->tech->requester(type, joint_cap, assignedids, requestor, addr, cause);
6338 ao2_ref(joint_cap, -1);
6339 }
6340
6341 if (!c) {
6342 return NULL;
6343 }
6344
6345 if (requestor) {
6346 ast_callid callid;
6347
6348 ast_channel_lock_both(c, (struct ast_channel *) requestor);
6349
6350 /* Set the newly created channel's callid to the same as the requestor. */
6351 callid = ast_channel_callid(requestor);
6352 if (callid) {
6353 ast_channel_callid_set(c, callid);
6354 }
6355
6356 ast_channel_unlock(c);
6357 ast_channel_unlock((struct ast_channel *) requestor);
6358 }
6359
6360 if (set_security_requirements(requestor, c)) {
6361 ast_log(LOG_WARNING, "Setting security requirements failed\n");
6362 ast_hangup(c);
6363 *cause = AST_CAUSE_BEARERCAPABILITY_NOTAVAIL;
6364 return NULL;
6365 }
6366
6367 /* no need to generate a Newchannel event here; it is done in the channel_alloc call */
6368 return c;
6369 }
6370
6371 struct ast_channel *ast_request(const char *type, struct ast_format_cap *request_cap, const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor, const char *addr, int *cause)
6372 {
6373 return request_channel(type, request_cap, NULL, assignedids, requestor, addr, cause);
6374 }
6375
6376 struct ast_channel *ast_request_with_stream_topology(const char *type, struct ast_stream_topology *topology, const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor, const char *addr, int *cause)
6377 {
6378 return request_channel(type, NULL, topology, assignedids, requestor, addr, cause);
6379 }
6380
6381 /*!
6382 * \internal
6383 * \brief Setup new channel accountcodes from the requestor channel after ast_request().
6384 * \since 13.0.0
6385 *
6386 * \param chan New channel to get accountcodes setup.
6387 * \param requestor Requesting channel to get accountcodes from.
6388 * \param relationship What the new channel was created for.
6389 * \param precious TRUE if pre-existing accountcodes on chan will not be overwritten.
6390 *
6391 * \pre The chan and requestor channels are already locked.
6392 */
6393 static void channel_req_accountcodes(struct ast_channel *chan, const struct ast_channel *requestor, enum ast_channel_requestor_relationship relationship, int precious)
6394 {
6395 /*
6396 * The primary reason for the existence of this function is
6397 * so local channels can propagate accountcodes to the ;2
6398 * channel before ast_call().
6399 *
6400 * The secondary reason is to propagate the CHANNEL(peeraccount)
6401 * value set before Dial, FollowMe, and Queue while maintaining
6402 * the historic straight across accountcode propagation as a
6403 * fallback.
6404 */
6405 switch (relationship) {
6406 case AST_CHANNEL_REQUESTOR_BRIDGE_PEER:
6407 /* Crossover the requestor's accountcode and peeraccount */
6408 if (!precious || ast_strlen_zero(ast_channel_accountcode(chan))) {
6409 /*
6410 * The newly created channel does not have an accountcode
6411 * or we don't care.
6412 */
6413 if (!ast_strlen_zero(ast_channel_peeraccount(requestor))) {
6414 /*
6415 * Set it to the requestor's peeraccount. This allows the
6416 * dialplan to indicate the accountcode to use when dialing
6417 * by setting CHANNEL(peeraccount).
6418 */
6419 ast_channel_accountcode_set(chan, ast_channel_peeraccount(requestor));
6420 } else if (!precious
6421 && !ast_strlen_zero(ast_channel_accountcode(requestor))) {
6422 /*
6423 * Fallback to the historic propagation and set it to the
6424 * requestor's accountcode.
6425 */
6426 ast_channel_accountcode_set(chan, ast_channel_accountcode(requestor));
6427 }
6428 }
6429 if (!ast_strlen_zero(ast_channel_accountcode(requestor))) {
6430 ast_channel_peeraccount_set(chan, ast_channel_accountcode(requestor));
6431 }
6432 break;
6433 case AST_CHANNEL_REQUESTOR_REPLACEMENT:
6434 /* Pass the requestor's accountcode and peeraccount straight. */
6435 if (!precious || ast_strlen_zero(ast_channel_accountcode(chan))) {
6436 /*
6437 * The newly created channel does not have an accountcode
6438 * or we don't care.
6439 */
6440 if (!ast_strlen_zero(ast_channel_accountcode(requestor))) {
6441 ast_channel_accountcode_set(chan, ast_channel_accountcode(requestor));
6442 }
6443 }
6444 if (!ast_strlen_zero(ast_channel_peeraccount(requestor))) {
6445 ast_channel_peeraccount_set(chan, ast_channel_peeraccount(requestor));
6446 }
6447 break;
6448 }
6449 }
6450
6451 void ast_channel_req_accountcodes(struct ast_channel *chan, const struct ast_channel *requestor, enum ast_channel_requestor_relationship relationship)
6452 {
6453 channel_req_accountcodes(chan, requestor, relationship, 0);
6454 }
6455
6456 void ast_channel_req_accountcodes_precious(struct ast_channel *chan, const struct ast_channel *requestor, enum ast_channel_requestor_relationship relationship)
6457 {
6458 channel_req_accountcodes(chan, requestor, relationship, 1);
6459 }
6460
6461 int ast_pre_call(struct ast_channel *chan, const char *sub_args)
6462 {
6463 int (*pre_call)(struct ast_channel *chan, const char *sub_args);
6464
6465 ast_channel_lock(chan);
6466 pre_call = ast_channel_tech(chan)->pre_call;
6467 if (pre_call) {
6468 int res;
6469
6470 res = pre_call(chan, sub_args);
6471 ast_channel_unlock(chan);
6472 return res;
6473 }
6474 ast_channel_unlock(chan);
6475 return ast_app_exec_sub(NULL, chan, sub_args, 0);
6476 }
6477
6478 int ast_call(struct ast_channel *chan, const char *addr, int timeout)
6479 {
6480 /* Place an outgoing call, but don't wait any longer than timeout ms before returning.
6481 If the remote end does not answer within the timeout, then do NOT hang up, but
6482 return anyway. */
6483 int res = -1;
6484 /* Stop if we're a zombie or need a soft hangup */
6485 ast_channel_lock(chan);
6486 if (!ast_test_flag(ast_channel_flags(chan), AST_FLAG_ZOMBIE) && !ast_check_hangup(chan)) {
6487 if (ast_channel_tech(chan)->call)
6488 res = ast_channel_tech(chan)->call(chan, addr, timeout);
6489 ast_set_flag(ast_channel_flags(chan), AST_FLAG_OUTGOING);
6490 }
6491 ast_channel_unlock(chan);
6492 return res;
6493 }
6494
6495 /*!
6496 \brief Transfer a call to dest, if the channel supports transfer
6497
6498 Called by:
6499 \arg app_transfer
6500 \arg the manager interface
6501 */
6502 int ast_transfer(struct ast_channel *chan, char *dest)
6503 {
6504 int protocol;
6505 return ast_transfer_protocol(chan, dest, &protocol);
6506 }
6507
6508 /*!
6509 \brief Transfer a call to dest, if the channel supports transfer
6510
6511 Called by:
6512 \arg app_transfer
6513 \arg the manager interface
6514 */
6515 int ast_transfer_protocol(struct ast_channel *chan, char *dest, int *protocol)
6516 {
6517 int res = -1;
6518
6519 if (protocol) {
6520 *protocol = 0;
6521 }
6522
6523 /* Stop if we're a zombie or need a soft hangup */
6524 ast_channel_lock(chan);
6525 if (!ast_test_flag(ast_channel_flags(chan), AST_FLAG_ZOMBIE) && !ast_check_hangup(chan)) {
6526 if (ast_channel_tech(chan)->transfer) {
6527 res = ast_channel_tech(chan)->transfer(chan, dest);
6528 if (!res)
6529 res = 1;
6530 } else
6531 res = 0;
6532 }
6533 ast_channel_unlock(chan);
6534
6535 if (res <= 0) {
6536 return res;
6537 }
6538
6539 for (;;) {
6540 struct ast_frame *fr;
6541
6542 res = ast_waitfor(chan, -1);
6543
6544 if (res < 0 || !(fr = ast_read(chan))) {
6545 res = -1;
6546 break;
6547 }
6548
6549 if (fr->frametype == AST_FRAME_CONTROL && fr->subclass.integer == AST_CONTROL_TRANSFER) {
6550 enum ast_control_transfer *message = fr->data.ptr;
6551
6552 if (*message == AST_TRANSFER_SUCCESS) {
6553 res = 1;
6554 } else {
6555 res = -1;
6556 /* Message can contain a protocol specific code
6557 AST_TRANSFER_SUCCESS indicates success
6558 Else, failure. Protocol will be set to the failure reason.
6559 SIP example, 0 is success, else error code 3xx-6xx */
6560 if (protocol) {
6561 *protocol = *message;
6562 }
6563 }
6564
6565 ast_frfree(fr);
6566 break;
6567 }
6568
6569 ast_frfree(fr);
6570 }
6571
6572 return res;
6573 }
6574
6575 int ast_readstring(struct ast_channel *c, char *s, int len, int timeout, int ftimeout, char *enders)
6576 {
6577 return ast_readstring_full(c, s, len, timeout, ftimeout, enders, -1, -1);
6578 }
6579
6580 int ast_readstring_full(struct ast_channel *c, char *s, int len, int timeout, int ftimeout, char *enders, int audiofd, int ctrlfd)
6581 {
6582 int pos = 0; /* index in the buffer where we accumulate digits */
6583 int to = ftimeout;
6584
6585 struct ast_silence_generator *silgen = NULL;
6586
6587 /* Stop if we're a zombie or need a soft hangup */
6588 if (ast_test_flag(ast_channel_flags(c), AST_FLAG_ZOMBIE) || ast_check_hangup(c))
6589 return -1;
6590 if (!len)
6591 return -1;
6592 for (;;) {
6593 int d;
6594 if (ast_channel_stream(c)) {
6595 d = ast_waitstream_full(c, AST_DIGIT_ANY, audiofd, ctrlfd);
6596 ast_stopstream(c);
6597 if (!silgen && ast_opt_transmit_silence)
6598 silgen = ast_channel_start_silence_generator(c);
6599 usleep(1000);
6600 if (!d)
6601 d = ast_waitfordigit_full(c, to, NULL, audiofd, ctrlfd);
6602 } else {
6603 if (!silgen && ast_opt_transmit_silence)
6604 silgen = ast_channel_start_silence_generator(c);
6605 d = ast_waitfordigit_full(c, to, NULL, audiofd, ctrlfd);
6606 }
6607 if (d < 0) {
6608 ast_channel_stop_silence_generator(c, silgen);
6609 return AST_GETDATA_FAILED;
6610 }
6611 if (d == 0) {
6612 s[pos] = '\0';
6613 ast_channel_stop_silence_generator(c, silgen);
6614 return AST_GETDATA_TIMEOUT;
6615 }
6616 if (d == 1) {
6617 s[pos] = '\0';
6618 ast_channel_stop_silence_generator(c, silgen);
6619 return AST_GETDATA_INTERRUPTED;
6620 }
6621 if (strchr(enders, d) && (pos == 0)) {
6622 s[pos] = '\0';
6623 ast_channel_stop_silence_generator(c, silgen);
6624 return AST_GETDATA_EMPTY_END_TERMINATED;
6625 }
6626 if (!strchr(enders, d)) {
6627 s[pos++] = d;
6628 }
6629 if (strchr(enders, d) || (pos >= len)) {
6630 s[pos] = '\0';
6631 ast_channel_stop_silence_generator(c, silgen);
6632 return AST_GETDATA_COMPLETE;
6633 }
6634 to = timeout;
6635 }
6636 /* Never reached */
6637 return 0;
6638 }
6639
6640 int ast_channel_supports_html(struct ast_channel *chan)
6641 {
6642 return (ast_channel_tech(chan)->send_html) ? 1 : 0;
6643 }
6644
6645 int ast_channel_sendhtml(struct ast_channel *chan, int subclass, const char *data, int datalen)
6646 {
6647 if (ast_channel_tech(chan)->send_html)
6648 return ast_channel_tech(chan)->send_html(chan, subclass, data, datalen);
6649 return -1;
6650 }
6651
6652 int ast_channel_sendurl(struct ast_channel *chan, const char *url)
6653 {
6654 return ast_channel_sendhtml(chan, AST_HTML_URL, url, strlen(url) + 1);
6655 }
6656
6657 /*! \brief Set up translation from one channel to another */
6658 static int ast_channel_make_compatible_helper(struct ast_channel *from, struct ast_channel *to)
6659 {
6660 struct ast_format_cap *src_cap;
6661 struct ast_format_cap *dst_cap;
6662 RAII_VAR(struct ast_format *, best_src_fmt, NULL, ao2_cleanup);
6663 RAII_VAR(struct ast_format *, best_dst_fmt, NULL, ao2_cleanup);
6664 int no_path;
6665
6666 /*
6667 * We cannot short circuit this code because it is possible to ask
6668 * to make compatible two channels that are "compatible" because
6669 * they already have translation paths setup but together make for
6670 * a sub-optimal path. e.g., The From channel has g722 -> ulaw
6671 * and the To channel has ulaw -> g722. They are "compatible" but
6672 * together the translations are unnecessary and the audio loses
6673 * fidelity in the process.
6674 */
6675
6676 ast_channel_lock_both(from, to);
6677
6678 src_cap = ast_channel_nativeformats(from); /* shallow copy, do not destroy */
6679 dst_cap = ast_channel_nativeformats(to); /* shallow copy, do not destroy */
6680
6681 /* If there's no audio in this call, don't bother with trying to find a translation path */
6682 if (!ast_format_cap_has_type(src_cap, AST_MEDIA_TYPE_AUDIO)
6683 || !ast_format_cap_has_type(dst_cap, AST_MEDIA_TYPE_AUDIO)) {
6684 ast_channel_unlock(to);
6685 ast_channel_unlock(from);
6686 return 0;
6687 }
6688
6689 no_path = ast_translator_best_choice(dst_cap, src_cap, &best_dst_fmt, &best_src_fmt);
6690
6691 ast_channel_unlock(to);
6692 ast_channel_unlock(from);
6693
6694 if (no_path) {
6695 ast_log(LOG_WARNING, "No path to translate from %s to %s\n",
6696 ast_channel_name(from), ast_channel_name(to));
6697 return -1;
6698 }
6699
6700 /* if the best path is not 'pass through', then
6701 * transcoding is needed; if desired, force transcode path
6702 * to use SLINEAR between channels, but only if there is
6703 * no direct conversion available. If generic PLC is
6704 * desired, then transcoding via SLINEAR is a requirement
6705 * even if the formats are the same.
6706 */
6707 if (ast_opt_generic_plc_on_equal_codecs
6708 || (ast_format_cmp(best_dst_fmt, best_src_fmt) == AST_FORMAT_CMP_NOT_EQUAL
6709 && (ast_opt_generic_plc || ast_opt_transcode_via_slin))) {
6710
6711 int use_slin = (ast_format_cache_is_slinear(best_src_fmt)
6712 || ast_format_cache_is_slinear(best_dst_fmt))
6713 ? 1 : ast_opt_generic_plc_on_equal_codecs;
6714
6715 if (use_slin || ast_translate_path_steps(best_dst_fmt, best_src_fmt) != 1) {
6716 int best_sample_rate = (ast_format_get_sample_rate(best_src_fmt) > ast_format_get_sample_rate(best_dst_fmt)) ?
6717 ast_format_get_sample_rate(best_src_fmt) : ast_format_get_sample_rate(best_dst_fmt);
6718
6719 /* pick the best signed linear format based upon what preserves the sample rate the best. */
6720 ao2_replace(best_src_fmt, ast_format_cache_get_slin_by_rate(best_sample_rate));
6721 }
6722 }
6723
6724 if (ast_set_read_format(from, best_src_fmt)) {
6725 ast_log(LOG_WARNING, "Unable to set read format on channel %s to %s\n",
6726 ast_channel_name(from), ast_format_get_name(best_src_fmt));
6727 return -1;
6728 }
6729 if (ast_set_write_format(to, best_src_fmt)) {
6730 ast_log(LOG_WARNING, "Unable to set write format on channel %s to %s\n",
6731 ast_channel_name(to), ast_format_get_name(best_src_fmt));
6732 return -1;
6733 }
6734 return 0;
6735 }
6736
6737 int ast_channel_make_compatible(struct ast_channel *chan, struct ast_channel *peer)
6738 {
6739 /*
6740 * Set up translation from the peer to the chan first in case we
6741 * need to hear any in-band tones and the other direction fails.
6742 */
6743 if (ast_channel_make_compatible_helper(peer, chan)) {
6744 return -1;
6745 }
6746
6747 /* Set up translation from the chan to the peer */
6748 if (ast_channel_make_compatible_helper(chan, peer)) {
6749 return -1;
6750 }
6751
6752 return 0;
6753 }
6754
6755 /*! \brief this function simply changes the name of the channel and issues a manager_event
6756 * with out unlinking and linking the channel from the ao2_container. This should
6757 * only be used when the channel has already been unlinked from the ao2_container.
6758 */
6759 static void __ast_change_name_nolink(struct ast_channel *chan, const char *newname)
6760 {
6761 /*** DOCUMENTATION
6762 <managerEvent language="en_US" name="Rename">
6763 <managerEventInstance class="EVENT_FLAG_CALL">
6764 <since>
6765 <version>16.24.0</version>
6766 <version>18.10.0</version>
6767 <version>19.2.0</version>
6768 </since>
6769 <synopsis>Raised when the name of a channel is changed.</synopsis>
6770 </managerEventInstance>
6771 </managerEvent>
6772 ***/
6773 ast_manager_event(chan, EVENT_FLAG_CALL, "Rename",
6774 "Channel: %s\r\n"
6775 "Newname: %s\r\n"
6776 "Uniqueid: %s\r\n",
6777 ast_channel_name(chan), newname, ast_channel_uniqueid(chan));
6778 ast_channel_name_set(chan, newname);
6779 }
6780
6781 void ast_change_name(struct ast_channel *chan, const char *newname)
6782 {
6783 /* We must re-link, as the hash value will change here. */
6784 CHANNELSTORAGE_API(current_channel_storage_instance, wrlock);
6785 ast_channel_lock(chan);
6786 CHANNELSTORAGE_API(current_channel_storage_instance, remove, chan, 0);
6787 __ast_change_name_nolink(chan, newname);
6788 CHANNELSTORAGE_API(current_channel_storage_instance, insert, chan, 0, 0);
6789 ast_channel_unlock(chan);
6790 CHANNELSTORAGE_API(current_channel_storage_instance, unlock);
6791 }
6792
6793 void ast_channel_inherit_variables(const struct ast_channel *parent, struct ast_channel *child)
6794 {
6795 struct ast_var_t *current;
6796 struct ast_var_t *newvar;
6797 const char *varname;
6798 int vartype;
6799
6800 AST_LIST_TRAVERSE(ast_channel_varshead((struct ast_channel *) parent), current, entries) {
6801 varname = ast_var_full_name(current);
6802 if (!varname) {
6803 continue;
6804 }
6805
6806 vartype = 0;
6807 if (varname[0] == '_') {
6808 vartype = 1;
6809 if (varname[1] == '_') {
6810 vartype = 2;
6811 }
6812 }
6813
6814 switch (vartype) {
6815 case 1:
6816 newvar = ast_var_assign(&varname[1], ast_var_value(current));
6817 break;
6818 case 2:
6819 newvar = ast_var_assign(varname, ast_var_value(current));
6820 break;
6821 default:
6822 continue;
6823 }
6824 if (newvar) {
6825 ast_debug(1, "Inheriting variable %s from %s to %s.\n",
6826 ast_var_full_name(newvar), ast_channel_name(parent),
6827 ast_channel_name(child));
6828 AST_LIST_INSERT_TAIL(ast_channel_varshead(child), newvar, entries);
6829 ast_channel_publish_varset(child, ast_var_full_name(newvar),
6830 ast_var_value(newvar));
6831 }
6832 }
6833 }
6834
6835 /*!
6836 \brief Clone channel variables from 'clone' channel into 'original' channel
6837
6838 All variables except those related to app_groupcount are cloned.
6839 Variables are actually _removed_ from 'clone' channel, presumably
6840 because it will subsequently be destroyed.
6841
6842 \note Assumes locks will be in place on both channels when called.
6843 */
6844 static void clone_variables(struct ast_channel *original, struct ast_channel *clonechan)
6845 {
6846 struct ast_var_t *current, *newvar;
6847 /* Append variables from clone channel into original channel */
6848 /* XXX Is this always correct? We have to in order to keep MACROS working XXX */
6849 AST_LIST_APPEND_LIST(ast_channel_varshead(original), ast_channel_varshead(clonechan), entries);
6850
6851 /* then, dup the varshead list into the clone */
6852
6853 AST_LIST_TRAVERSE(ast_channel_varshead(original), current, entries) {
6854 newvar = ast_var_assign(current->name, current->value);
6855 if (newvar)
6856 AST_LIST_INSERT_TAIL(ast_channel_varshead(clonechan), newvar, entries);
6857 }
6858 }
6859
6860
6861 void ast_channel_name_to_dial_string(char *channel_name)
6862 {
6863 char *dash;
6864
6865 /* Truncate after the dash */
6866 dash = strrchr(channel_name, '-');
6867 if (dash) {
6868 *dash = '\0';
6869 }
6870 }
6871
6872 /*!
6873 * \brief Masquerade a channel
6874 *
6875 * \note Assumes _NO_ channels and _NO_ channel pvt's are locked. If a channel is locked while calling
6876 * this function, it invalidates our channel container locking order. All channels
6877 * must be unlocked before it is permissible to lock the channels' ao2 container.
6878 */
6879 static void channel_do_masquerade(struct ast_channel *original, struct ast_channel *clonechan)
6880 {
6881 int x;
6882 int origstate;
6883 unsigned int orig_disablestatecache;
6884 unsigned int clone_disablestatecache;
6885 int visible_indication;
6886 int clone_hold_state;
6887 int moh_is_playing;
6888 struct ast_frame *current;
6889 const struct ast_channel_tech *t;
6890 void *t_pvt;
6891 union {
6892 struct ast_hangup_handler_list handlers;
6893 struct ast_party_dialed dialed;
6894 struct ast_party_caller caller;
6895 struct ast_party_connected_line connected;
6896 struct ast_party_redirecting redirecting;
6897 } exchange;
6898 struct ast_channel *bridged;
6899 struct ast_format *rformat;
6900 struct ast_format *wformat;
6901 struct ast_format *tmp_format;
6902 struct ast_format_cap *tmp_cap;
6903 char tmp_name[AST_CHANNEL_NAME];
6904 char clone_sending_dtmf_digit;
6905 struct timeval clone_sending_dtmf_tv;
6906
6907 ast_debug(3, "Masquerading %s(%u) into the structure of %s(%u)\n",
6908 ast_channel_name(clonechan), ast_channel_state(clonechan),
6909 ast_channel_name(original), ast_channel_state(original));
6910 /* XXX This operation is a bit odd. We're essentially putting the guts of
6911 * the clone channel into the original channel. Start by killing off the
6912 * original channel's backend. While the features are nice, which is the
6913 * reason we're keeping it, it's still awesomely weird. XXX */
6914
6915 /* Indicate to each channel that a masquerade is about to begin. */
6916 x = 1;
6917 ast_indicate_data(original, AST_CONTROL_MASQUERADE_NOTIFY, &x, sizeof(x));
6918 ast_indicate_data(clonechan, AST_CONTROL_MASQUERADE_NOTIFY, &x, sizeof(x));
6919
6920 /*
6921 * The container lock is necessary for proper locking order
6922 * because the channels must be unlinked to change their
6923 * names.
6924 *
6925 * The original and clonechan locks must be held while the
6926 * channel contents are shuffled around for the masquerade.
6927 *
6928 * The masq and masqr pointers need to be left alone until the masquerade
6929 * has restabilized the channels to hold off ast_hangup() and until
6930 * AST_FLAG_ZOMBIE can be set on the clonechan.
6931 */
6932 CHANNELSTORAGE_API(current_channel_storage_instance, wrlock);
6933
6934 /* Bump the refs to ensure that they won't disappear on us. */
6935 ast_channel_ref(original);
6936 ast_channel_ref(clonechan);
6937
6938 /*
6939 * Since channel name and unique id will change, and both could be keys
6940 * in the channel storage backend, we need to remove them from the backend.
6941 * We'll add them back in after the changes are compete.
6942 */
6943 CHANNELSTORAGE_API(current_channel_storage_instance, remove, original, 0);
6944 CHANNELSTORAGE_API(current_channel_storage_instance, remove, clonechan, 0);
6945
6946 CHANNELSTORAGE_API(current_channel_storage_instance, unlock);
6947
6948 moh_is_playing = ast_test_flag(ast_channel_flags(original), AST_FLAG_MOH);
6949 if (moh_is_playing) {
6950 /* Stop MOH on the old original channel. */
6951 ast_moh_stop(original);
6952 }
6953
6954 /*
6955 * Stop any visible indication on the original channel so we can
6956 * transfer it to the clonechan taking the original's place.
6957 */
6958 ast_channel_lock(original);
6959 visible_indication = ast_channel_visible_indication(original);
6960 ast_channel_unlock(original);
6961 ast_indicate(original, -1);
6962
6963 /* Start the masquerade channel contents rearrangement. */
6964 ast_channel_lock_both(original, clonechan);
6965
6966 ast_debug(1, "Actually Masquerading %s(%u) into the structure of %s(%u)\n",
6967 ast_channel_name(clonechan), ast_channel_state(clonechan),
6968 ast_channel_name(original), ast_channel_state(original));
6969
6970 /* When all is said and done force new snapshot segments so they are
6971 * up to date.
6972 */
6973 ast_set_flag(ast_channel_snapshot_segment_flags(original), AST_FLAGS_ALL);
6974 ast_set_flag(ast_channel_snapshot_segment_flags(clonechan), AST_FLAGS_ALL);
6975
6976 /*
6977 * Remember the original read/write formats. We turn off any
6978 * translation on either one
6979 */
6980 rformat = ao2_bump(ast_channel_readformat(original));
6981 wformat = ao2_bump(ast_channel_writeformat(original));
6982 free_translation(clonechan);
6983 free_translation(original);
6984
6985 clone_hold_state = ast_channel_hold_state(clonechan);
6986
6987 /* Save the current DTMF digit being sent if any. */
6988 clone_sending_dtmf_digit = ast_channel_sending_dtmf_digit(clonechan);
6989 clone_sending_dtmf_tv = ast_channel_sending_dtmf_tv(clonechan);
6990
6991 /* Swap uniqueid's of the channels. This needs to happen before channel renames,
6992 * so rename events get the proper id's.
6993 */
6994 ast_channel_internal_swap_uniqueid_and_linkedid(clonechan, original);
6995
6996 /* Make sure the Stasis topic on the channel is updated appropriately */
6997 ast_channel_internal_swap_topics(clonechan, original);
6998
6999 /* Swap endpoint forward so channel created with endpoint exchanges its state
7000 * with other channel for proper endpoint cleanup.
7001 */
7002 ast_channel_internal_swap_endpoint_forward(clonechan, original);
7003
7004 /* The old snapshots need to follow the channels so the snapshot update is correct */
7005 ast_channel_internal_swap_snapshots(clonechan, original);
7006
7007 /* Now we swap the endpoints if present */
7008 ast_channel_internal_swap_endpoints(clonechan, original);
7009
7010 /* Swap channel names. This uses ast_channel_name_set directly, so we
7011 * don't get any spurious rename events.
7012 */
7013 ast_copy_string(tmp_name, ast_channel_name(clonechan), sizeof(tmp_name));
7014 ast_channel_name_set(clonechan, ast_channel_name(original));
7015 ast_channel_name_set(original, tmp_name);
7016
7017 /* Swap the technologies */
7018 t = ast_channel_tech(original);
7019 ast_channel_tech_set(original, ast_channel_tech(clonechan));
7020 ast_channel_tech_set(clonechan, t);
7021
7022 t_pvt = ast_channel_tech_pvt(original);
7023 ast_channel_tech_pvt_set(original, ast_channel_tech_pvt(clonechan));
7024 ast_channel_tech_pvt_set(clonechan, t_pvt);
7025
7026 /* Swap the alertpipes */
7027 ast_channel_internal_alertpipe_swap(original, clonechan);
7028
7029 /*
7030 * Swap the readq's. The end result should be this:
7031 *
7032 * 1) All frames should be on the new (original) channel.
7033 * 2) Any frames that were already on the new channel before this
7034 * masquerade need to be at the end of the readq, after all of the
7035 * frames on the old (clone) channel.
7036 * 3) The alertpipe needs to get poked for every frame that was already
7037 * on the new channel, since we are now using the alert pipe from the
7038 * old (clone) channel.
7039 */
7040 {
7041 AST_LIST_HEAD_NOLOCK(, ast_frame) tmp_readq;
7042
7043 AST_LIST_HEAD_INIT_NOLOCK(&tmp_readq);
7044 AST_LIST_APPEND_LIST(&tmp_readq, ast_channel_readq(original), frame_list);
7045 AST_LIST_APPEND_LIST(ast_channel_readq(original), ast_channel_readq(clonechan), frame_list);
7046
7047 while ((current = AST_LIST_REMOVE_HEAD(&tmp_readq, frame_list))) {
7048 AST_LIST_INSERT_TAIL(ast_channel_readq(original), current, frame_list);
7049 if (ast_channel_alert_write(original)) {
7050 ast_log(LOG_WARNING, "write() failed: %s\n", strerror(errno));
7051 }
7052 }
7053 }
7054
7055 /* Swap the raw formats */
7056 tmp_format = ao2_bump(ast_channel_rawreadformat(original));
7057 ast_channel_set_rawreadformat(original, ast_channel_rawreadformat(clonechan));
7058 ast_channel_set_rawreadformat(clonechan, tmp_format);
7059 ao2_cleanup(tmp_format);
7060
7061 tmp_format = ao2_bump(ast_channel_rawwriteformat(original));
7062 ast_channel_set_rawwriteformat(original, ast_channel_rawwriteformat(clonechan));
7063 ast_channel_set_rawwriteformat(clonechan, tmp_format);
7064 ao2_cleanup(tmp_format);
7065
7066 ast_channel_softhangup_internal_flag_set(clonechan, AST_SOFTHANGUP_DEV);
7067
7068 /* And of course, so does our current state. Note we need not
7069 call ast_setstate since the event manager doesn't really consider
7070 these separate. We do this early so that the clone has the proper
7071 state of the original channel. */
7072 origstate = ast_channel_state(original);
7073 ast_channel_state_set(original, ast_channel_state(clonechan));
7074 ast_channel_state_set(clonechan, origstate);
7075
7076 /* And the swap the cachable state too. Otherwise we'd start caching
7077 * Local channels and ignoring real ones. */
7078 orig_disablestatecache = ast_test_flag(ast_channel_flags(original), AST_FLAG_DISABLE_DEVSTATE_CACHE);
7079 clone_disablestatecache = ast_test_flag(ast_channel_flags(clonechan), AST_FLAG_DISABLE_DEVSTATE_CACHE);
7080 if (orig_disablestatecache != clone_disablestatecache) {
7081 if (orig_disablestatecache) {
7082 ast_clear_flag(ast_channel_flags(original), AST_FLAG_DISABLE_DEVSTATE_CACHE);
7083 ast_set_flag(ast_channel_flags(clonechan), AST_FLAG_DISABLE_DEVSTATE_CACHE);
7084 } else {
7085 ast_set_flag(ast_channel_flags(original), AST_FLAG_DISABLE_DEVSTATE_CACHE);
7086 ast_clear_flag(ast_channel_flags(clonechan), AST_FLAG_DISABLE_DEVSTATE_CACHE);
7087 }
7088 }
7089
7090 /* Keep the same language. */
7091 ast_channel_language_set(original, ast_channel_language(clonechan));
7092
7093 /* Keep the same parkinglot. */
7094 ast_channel_parkinglot_set(original, ast_channel_parkinglot(clonechan));
7095
7096 /* Clear all existing file descriptors */
7097 ast_channel_internal_fd_clear_all(original);
7098
7099 /* Copy all file descriptors present on clonechan to original */
7100 for (x = 0; x < ast_channel_fd_count(clonechan); x++) {
7101 ast_channel_set_fd(original, x, ast_channel_fd(clonechan, x));
7102 }
7103
7104 ast_app_group_update(clonechan, original);
7105
7106 /* Swap hangup handlers. */
7107 exchange.handlers = *ast_channel_hangup_handlers(original);
7108 *ast_channel_hangup_handlers(original) = *ast_channel_hangup_handlers(clonechan);
7109 *ast_channel_hangup_handlers(clonechan) = exchange.handlers;
7110
7111 /* Call fixup handlers for the clone chan */
7112 if (AST_LIST_FIRST(ast_channel_datastores(clonechan))) {
7113 struct ast_datastore *ds;
7114 /* We use a safe traversal here because some fixup routines actually
7115 * remove the datastore from the list and free them.
7116 */
7117 AST_LIST_TRAVERSE_SAFE_BEGIN(ast_channel_datastores(clonechan), ds, entry) {
7118 if (ds->info->chan_fixup) {
7119 ds->info->chan_fixup(ds->data, clonechan, original);
7120 }
7121 }
7122 AST_LIST_TRAVERSE_SAFE_END;
7123 }
7124
7125 /* Call breakdown handlers for the original chan */
7126 if (AST_LIST_FIRST(ast_channel_datastores(original))) {
7127 struct ast_datastore *ds;
7128 /* We use a safe traversal here because some breakdown routines may
7129 * remove the datastore from the list and free them.
7130 */
7131 AST_LIST_TRAVERSE_SAFE_BEGIN(ast_channel_datastores(original), ds, entry) {
7132 if (ds->info->chan_breakdown) {
7133 ds->info->chan_breakdown(ds->data, clonechan, original);
7134 }
7135 }
7136 AST_LIST_TRAVERSE_SAFE_END;
7137 }
7138
7139 /* Move data stores over */
7140 if (AST_LIST_FIRST(ast_channel_datastores(clonechan))) {
7141 AST_LIST_APPEND_LIST(ast_channel_datastores(original), ast_channel_datastores(clonechan), entry);
7142 }
7143
7144 /* Move framehooks over */
7145 ast_framehook_list_fixup(clonechan, original);
7146
7147 /* Move audiohooks over */
7148 ast_audiohook_move_all(clonechan, original);
7149
7150 ast_autochan_new_channel(clonechan, original);
7151
7152 clone_variables(original, clonechan);
7153 /* Presence of ADSI capable CPE follows clone */
7154 ast_channel_adsicpe_set(original, ast_channel_adsicpe(clonechan));
7155 /* Bridge remains the same */
7156 /* CDR fields remain the same */
7157 /* XXX What about blocking, softhangup, blocker, and lock and blockproc? XXX */
7158 /* Application and data remain the same */
7159 /* Clone exception becomes real one, as with fdno */
7160 ast_set_flag(ast_channel_flags(original), ast_test_flag(ast_channel_flags(clonechan), AST_FLAG_EXCEPTION | AST_FLAG_OUTGOING));
7161 ast_channel_fdno_set(original, ast_channel_fdno(clonechan));
7162 /* Schedule context remains the same */
7163 /* Stream stuff stays the same */
7164 /* Keep the original state. The fixup code will need to work with it most likely */
7165
7166 /*
7167 * Just swap the whole structures, nevermind the allocations,
7168 * they'll work themselves out.
7169 */
7170 exchange.dialed = *ast_channel_dialed(original);
7171 ast_channel_dialed_set(original, ast_channel_dialed(clonechan));
7172 ast_channel_dialed_set(clonechan, &exchange.dialed);
7173
7174 /* Reset any earlier private caller id representations */
7175 ast_party_id_reset(&ast_channel_caller(original)->priv);
7176 ast_party_id_reset(&ast_channel_caller(clonechan)->priv);
7177
7178 exchange.caller = *ast_channel_caller(original);
7179 ast_channel_caller_set(original, ast_channel_caller(clonechan));
7180 ast_channel_caller_set(clonechan, &exchange.caller);
7181
7182 /* Reset any earlier private connected id representations */
7183 ast_party_id_reset(&ast_channel_connected(original)->priv);
7184 ast_party_id_reset(&ast_channel_connected(clonechan)->priv);
7185
7186 exchange.connected = *ast_channel_connected(original);
7187 ast_channel_connected_set(original, ast_channel_connected(clonechan));
7188 ast_channel_connected_set(clonechan, &exchange.connected);
7189
7190 /* Reset any earlier private redirecting orig, from or to representations */
7191 ast_party_id_reset(&ast_channel_redirecting(original)->priv_orig);
7192 ast_party_id_reset(&ast_channel_redirecting(clonechan)->priv_orig);
7193 ast_party_id_reset(&ast_channel_redirecting(original)->priv_from);
7194 ast_party_id_reset(&ast_channel_redirecting(clonechan)->priv_from);
7195 ast_party_id_reset(&ast_channel_redirecting(original)->priv_to);
7196 ast_party_id_reset(&ast_channel_redirecting(clonechan)->priv_to);
7197
7198 exchange.redirecting = *ast_channel_redirecting(original);
7199 ast_channel_redirecting_set(original, ast_channel_redirecting(clonechan));
7200 ast_channel_redirecting_set(clonechan, &exchange.redirecting);
7201
7202 ast_channel_publish_snapshot(original);
7203
7204 /* Restore original timing file descriptor */
7205 ast_channel_set_fd(original, AST_TIMING_FD, ast_channel_timingfd(original));
7206
7207 /* Our native formats are different now */
7208 tmp_cap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
7209 if (tmp_cap) {
7210 ast_format_cap_append_from_cap(tmp_cap, ast_channel_nativeformats(clonechan), AST_MEDIA_TYPE_UNKNOWN);
7211 ast_channel_nativeformats_set(original, tmp_cap);
7212 ao2_ref(tmp_cap, -1);
7213 }
7214
7215 /* Context, extension, priority, app data, jump table, remain the same */
7216 /* pvt switches. pbx stays the same, as does next */
7217
7218 /* Set the write format */
7219 ast_set_write_format(original, wformat);
7220
7221 /* Set the read format */
7222 ast_set_read_format(original, rformat);
7223
7224 /* Copy the music class */
7225 ast_channel_musicclass_set(original, ast_channel_musicclass(clonechan));
7226
7227 /* copy over accuntcode and set peeraccount across the bridge */
7228 ast_channel_accountcode_set(original, S_OR(ast_channel_accountcode(clonechan), ""));
7229
7230 /* copy over userfield */
7231 ast_channel_userfield_set(original, ast_channel_userfield(clonechan));
7232
7233 ast_debug(1, "Putting channel %s in %s/%s formats\n", ast_channel_name(original),
7234 ast_format_get_name(wformat), ast_format_get_name(rformat));
7235
7236 /* Fixup the original clonechan's physical side */
7237 if (ast_channel_tech(original)->fixup && ast_channel_tech(original)->fixup(clonechan, original)) {
7238 ast_log(LOG_WARNING, "Channel type '%s' could not fixup channel %s, strange things may happen. (clonechan)\n",
7239 ast_channel_tech(original)->type, ast_channel_name(original));
7240 }
7241
7242 /* Fixup the original original's physical side */
7243 if (ast_channel_tech(clonechan)->fixup && ast_channel_tech(clonechan)->fixup(original, clonechan)) {
7244 ast_log(LOG_WARNING, "Channel type '%s' could not fixup channel %s, strange things may happen. (original)\n",
7245 ast_channel_tech(clonechan)->type, ast_channel_name(clonechan));
7246 }
7247
7248 ast_channel_internal_swap_stream_topology(original, clonechan);
7249
7250 /*
7251 * Now, at this point, the "clone" channel is totally F'd up.
7252 * We mark it as a zombie so nothing tries to touch it.
7253 *
7254 * This must be done before we unlock clonechan to prevent
7255 * setting up another masquerade on the clonechan.
7256 */
7257 ast_set_flag(ast_channel_flags(clonechan), AST_FLAG_ZOMBIE);
7258 ast_queue_frame(clonechan, &ast_null_frame);
7259
7260 ast_channel_unlock(original);
7261 ast_channel_unlock(clonechan);
7262
7263 /*
7264 * Indicate to each channel that a masquerade is complete.
7265 *
7266 * We can still do this to clonechan even though it is a
7267 * zombie because ast_indicate_data() will explicitly pass
7268 * this control and ast_hangup() is held off until the
7269 * ast_channel_masq() and ast_channel_masqr() pointers are
7270 * cleared.
7271 */
7272 x = 0;
7273 ast_indicate_data(original, AST_CONTROL_MASQUERADE_NOTIFY, &x, sizeof(x));
7274 ast_indicate_data(clonechan, AST_CONTROL_MASQUERADE_NOTIFY, &x, sizeof(x));
7275
7276 ast_bridge_notify_masquerade(original);
7277
7278 if (clone_hold_state == AST_CONTROL_HOLD) {
7279 ast_debug(1, "Channel %s simulating UNHOLD for masquerade.\n",
7280 ast_channel_name(original));
7281 ast_indicate(original, AST_CONTROL_UNHOLD);
7282 }
7283 if (clone_sending_dtmf_digit) {
7284 /*
7285 * The clonechan was sending a DTMF digit that was not completed
7286 * before the masquerade.
7287 */
7288 ast_channel_end_dtmf(original, clone_sending_dtmf_digit, clone_sending_dtmf_tv,
7289 "masquerade");
7290 }
7291
7292 /*
7293 * If an indication is currently playing, maintain it on the
7294 * channel that is taking the place of original.
7295 *
7296 * This is needed because the masquerade is swapping out the
7297 * internals of the channel, and the new channel private data
7298 * needs to be made aware of the current visible indication
7299 * (RINGING, CONGESTION, etc.)
7300 */
7301 if (visible_indication) {
7302 if (visible_indication == AST_CONTROL_HOLD) {
7303 const char *latest_musicclass;
7304 int len;
7305
7306 ast_channel_lock(original);
7307 latest_musicclass = ast_strdupa(ast_channel_latest_musicclass(original));
7308 ast_channel_unlock(original);
7309 if (ast_strlen_zero(latest_musicclass)) {
7310 latest_musicclass = NULL;
7311 len = 0;
7312 } else {
7313 len = strlen(latest_musicclass) + 1;
7314 }
7315 ast_indicate_data(original, visible_indication, latest_musicclass, len);
7316 } else {
7317 ast_indicate(original, visible_indication);
7318 }
7319 }
7320
7321 /*
7322 * If MOH was playing on the original channel then it needs to be
7323 * maintained on the channel that is replacing it.
7324 */
7325 if (moh_is_playing) {
7326 /* Start MOH on the new original channel. */
7327 ast_moh_start(original, NULL, NULL);
7328 }
7329
7330 ast_channel_lock(original);
7331
7332 /* Signal any blocker */
7333 if (ast_test_flag(ast_channel_flags(original), AST_FLAG_BLOCKING)) {
7334 pthread_kill(ast_channel_blocker(original), SIGURG);
7335 }
7336
7337 ast_debug(1, "Done Masquerading %s (%u)\n", ast_channel_name(original), ast_channel_state(original));
7338 ast_channel_unlock(original);
7339
7340 if ((bridged = ast_channel_bridge_peer(original))) {
7341 ast_indicate(bridged, AST_CONTROL_SRCCHANGE);
7342 ast_channel_unref(bridged);
7343 }
7344 ast_indicate(original, AST_CONTROL_SRCCHANGE);
7345
7346 /* Now that the operation is complete, we can clear the masq
7347 * and masqr fields of both channels.
7348 */
7349 ast_channel_lock_both(original, clonechan);
7350 ast_channel_masq_set(original, NULL);
7351 ast_channel_masqr_set(clonechan, NULL);
7352 ast_channel_unlock(original);
7353 ast_channel_unlock(clonechan);
7354
7355 CHANNELSTORAGE_API(current_channel_storage_instance, wrlock);
7356 CHANNELSTORAGE_API(current_channel_storage_instance, insert, clonechan, 0, 0);
7357 CHANNELSTORAGE_API(current_channel_storage_instance, insert, original, 0, 0);
7358 CHANNELSTORAGE_API(current_channel_storage_instance, unlock);
7359
7360 /* Release our held safety references. */
7361 ast_channel_unref(original);
7362 ast_channel_unref(clonechan);
7363
7364 ao2_cleanup(rformat);
7365 ao2_cleanup(wformat);
7366 }
7367
7368 void ast_set_callerid(struct ast_channel *chan, const char *cid_num, const char *cid_name, const char *cid_ani)
7369 {
7370 ast_channel_lock(chan);
7371
7372 if (cid_num) {
7373 ast_channel_caller(chan)->id.number.valid = 1;
7374 ast_free(ast_channel_caller(chan)->id.number.str);
7375 ast_channel_caller(chan)->id.number.str = ast_strdup(cid_num);
7376 }
7377 if (cid_name) {
7378 ast_channel_caller(chan)->id.name.valid = 1;
7379 ast_free(ast_channel_caller(chan)->id.name.str);
7380 ast_channel_caller(chan)->id.name.str = ast_strdup(cid_name);
7381 }
7382 if (cid_ani) {
7383 ast_channel_caller(chan)->ani.number.valid = 1;
7384 ast_free(ast_channel_caller(chan)->ani.number.str);
7385 ast_channel_caller(chan)->ani.number.str = ast_strdup(cid_ani);
7386 }
7387
7388 ast_channel_publish_snapshot(chan);
7389
7390 ast_channel_unlock(chan);
7391 }
7392
7393 void ast_channel_set_caller(struct ast_channel *chan, const struct ast_party_caller *caller, const struct ast_set_party_caller *update)
7394 {
7395 if (ast_channel_caller(chan) == caller) {
7396 /* Don't set to self */
7397 return;
7398 }
7399
7400 ast_channel_lock(chan);
7401 ast_party_caller_set(ast_channel_caller(chan), caller, update);
7402 ast_channel_snapshot_invalidate_segment(chan, AST_CHANNEL_SNAPSHOT_INVALIDATE_CALLER);
7403 ast_channel_unlock(chan);
7404 }
7405
7406 void ast_channel_set_caller_event(struct ast_channel *chan, const struct ast_party_caller *caller, const struct ast_set_party_caller *update)
7407 {
7408 if (ast_channel_caller(chan) == caller) {
7409 /* Don't set to self */
7410 return;
7411 }
7412
7413 ast_channel_lock(chan);
7414 ast_party_caller_set(ast_channel_caller(chan), caller, update);
7415 ast_channel_snapshot_invalidate_segment(chan, AST_CHANNEL_SNAPSHOT_INVALIDATE_CALLER);
7416 ast_channel_publish_snapshot(chan);
7417 ast_channel_unlock(chan);
7418 }
7419
7420 int ast_setstate(struct ast_channel *chan, enum ast_channel_state state)
7421 {
7422 int oldstate = ast_channel_state(chan);
7423 char name[AST_CHANNEL_NAME], *dashptr;
7424
7425 if (oldstate == state)
7426 return 0;
7427
7428 ast_copy_string(name, ast_channel_name(chan), sizeof(name));
7429 if ((dashptr = strrchr(name, '-'))) {
7430 *dashptr = '\0';
7431 }
7432
7433 ast_channel_state_set(chan, state);
7434
7435 ast_channel_publish_snapshot(chan);
7436
7437 /* We have to pass AST_DEVICE_UNKNOWN here because it is entirely possible that the channel driver
7438 * for this channel is using the callback method for device state. If we pass in an actual state here
7439 * we override what they are saying the state is and things go amuck. */
7440 ast_devstate_changed_literal(AST_DEVICE_UNKNOWN, (ast_test_flag(ast_channel_flags(chan), AST_FLAG_DISABLE_DEVSTATE_CACHE) ? AST_DEVSTATE_NOT_CACHABLE : AST_DEVSTATE_CACHABLE), name);
7441
7442 return 0;
7443 }
7444
7445 /*! \brief Bridge two channels together (early) */
7446 int ast_channel_early_bridge(struct ast_channel *c0, struct ast_channel *c1)
7447 {
7448 /* Make sure we can early bridge, if not error out */
7449 if (!ast_channel_tech(c0)->early_bridge || (c1 && (!ast_channel_tech(c1)->early_bridge || ast_channel_tech(c0)->early_bridge != ast_channel_tech(c1)->early_bridge)))
7450 return -1;
7451
7452 return ast_channel_tech(c0)->early_bridge(c0, c1);
7453 }
7454
7455 /*! \brief Sets an option on a channel */
7456 int ast_channel_setoption(struct ast_channel *chan, int option, void *data, int datalen, int block)
7457 {
7458 int res;
7459
7460 ast_channel_lock(chan);
7461 if (!ast_channel_tech(chan)->setoption) {
7462 errno = ENOSYS;
7463 ast_channel_unlock(chan);
7464 return -1;
7465 }
7466
7467 if (block)
7468 ast_log(LOG_ERROR, "XXX Blocking not implemented yet XXX\n");
7469
7470 res = ast_channel_tech(chan)->setoption(chan, option, data, datalen);
7471 ast_channel_unlock(chan);
7472
7473 return res;
7474 }
7475
7476 int ast_channel_queryoption(struct ast_channel *chan, int option, void *data, int *datalen, int block)
7477 {
7478 int res;
7479
7480 ast_channel_lock(chan);
7481 if (!ast_channel_tech(chan)->queryoption) {
7482 errno = ENOSYS;
7483 ast_channel_unlock(chan);
7484 return -1;
7485 }
7486
7487 if (block)
7488 ast_log(LOG_ERROR, "XXX Blocking not implemented yet XXX\n");
7489
7490 res = ast_channel_tech(chan)->queryoption(chan, option, data, datalen);
7491 ast_channel_unlock(chan);
7492
7493 return res;
7494 }
7495
7496 struct tonepair_def {
7497 int freq1;
7498 int freq2;
7499 int duration;
7500 int vol;
7501 };
7502
7503 struct tonepair_state {
7504 int fac1;
7505 int fac2;
7506 int v1_1;
7507 int v2_1;
7508 int v3_1;
7509 int v1_2;
7510 int v2_2;
7511 int v3_2;
7512 struct ast_format *origwfmt;
7513 int pos;
7514 int duration;
7515 int modulate;
7516 struct ast_frame f;
7517 unsigned char offset[AST_FRIENDLY_OFFSET];
7518 short data[4000];
7519 };
7520
7521 static void tonepair_release(struct ast_channel *chan, void *params)
7522 {
7523 struct tonepair_state *ts = params;
7524
7525 if (chan) {
7526 ast_set_write_format(chan, ts->origwfmt);
7527 }
7528 ao2_cleanup(ts->origwfmt);
7529 ast_free(ts);
7530 }
7531
7532 static void *tonepair_alloc(struct ast_channel *chan, void *params)
7533 {
7534 struct tonepair_state *ts;
7535 struct tonepair_def *td = params;
7536
7537 if (!(ts = ast_calloc(1, sizeof(*ts)))) {
7538 return NULL;
7539 }
7540
7541 ts->origwfmt = ao2_bump(ast_channel_writeformat(chan));
7542 if (ast_set_write_format(chan, ast_format_slin)) {
7543 ast_log(LOG_WARNING, "Unable to set '%s' to signed linear format (write)\n", ast_channel_name(chan));
7544 tonepair_release(NULL, ts);
7545 ts = NULL;
7546 } else {
7547 ts->fac1 = 2.0 * cos(2.0 * M_PI * (td->freq1 / 8000.0)) * 32768.0;
7548 ts->v1_1 = 0;
7549 ts->v2_1 = sin(-4.0 * M_PI * (td->freq1 / 8000.0)) * td->vol;
7550 ts->v3_1 = sin(-2.0 * M_PI * (td->freq1 / 8000.0)) * td->vol;
7551 ts->v2_1 = 0;
7552 ts->fac2 = 2.0 * cos(2.0 * M_PI * (td->freq2 / 8000.0)) * 32768.0;
7553 ts->v2_2 = sin(-4.0 * M_PI * (td->freq2 / 8000.0)) * td->vol;
7554 ts->v3_2 = sin(-2.0 * M_PI * (td->freq2 / 8000.0)) * td->vol;
7555 ts->duration = td->duration;
7556 ts->modulate = 0;
7557 }
7558 /* Let interrupts interrupt :) */
7559 ast_set_flag(ast_channel_flags(chan), AST_FLAG_WRITE_INT);
7560 return ts;
7561 }
7562
7563 static int tonepair_generator(struct ast_channel *chan, void *data, int len, int samples)
7564 {
7565 struct tonepair_state *ts = data;
7566 int x;
7567
7568 /* we need to prepare a frame with 16 * timelen samples as we're
7569 * generating SLIN audio
7570 */
7571 len = samples * 2;
7572
7573 if (len > sizeof(ts->data) / 2 - 1) {
7574 ast_log(LOG_WARNING, "Can't generate that much data!\n");
7575 return -1;
7576 }
7577 memset(&ts->f, 0, sizeof(ts->f));
7578 for (x=0;x<len/2;x++) {
7579 ts->v1_1 = ts->v2_1;
7580 ts->v2_1 = ts->v3_1;
7581 ts->v3_1 = (ts->fac1 * ts->v2_1 >> 15) - ts->v1_1;
7582
7583 ts->v1_2 = ts->v2_2;
7584 ts->v2_2 = ts->v3_2;
7585 ts->v3_2 = (ts->fac2 * ts->v2_2 >> 15) - ts->v1_2;
7586 if (ts->modulate) {
7587 int p;
7588 p = ts->v3_2 - 32768;
7589 if (p < 0) p = -p;
7590 p = ((p * 9) / 10) + 1;
7591 ts->data[x] = (ts->v3_1 * p) >> 15;
7592 } else
7593 ts->data[x] = ts->v3_1 + ts->v3_2;
7594 }
7595 ts->f.frametype = AST_FRAME_VOICE;
7596 ts->f.subclass.format = ast_format_slin;
7597 ts->f.datalen = len;
7598 ts->f.samples = samples;
7599 ts->f.offset = AST_FRIENDLY_OFFSET;
7600 ts->f.data.ptr = ts->data;
7601 ast_write(chan, &ts->f);
7602 ts->pos += x;
7603 if (ts->duration > 0) {
7604 if (ts->pos >= ts->duration * 8)
7605 return -1;
7606 }
7607 return 0;
7608 }
7609
7610 static struct ast_generator tonepair = {
7611 .alloc = tonepair_alloc,
7612 .release = tonepair_release,
7613 .generate = tonepair_generator,
7614 };
7615
7616 int ast_tonepair_start(struct ast_channel *chan, int freq1, int freq2, int duration, int vol)
7617 {
7618 struct tonepair_def d = { 0, };
7619
7620 d.freq1 = freq1;
7621 d.freq2 = freq2;
7622 d.duration = duration;
7623 d.vol = (vol < 1) ? 8192 : vol; /* force invalid to 8192 */
7624 if (ast_activate_generator(chan, &tonepair, &d))
7625 return -1;
7626 return 0;
7627 }
7628
7629 void ast_tonepair_stop(struct ast_channel *chan)
7630 {
7631 ast_deactivate_generator(chan);
7632 }
7633
7634 int ast_tonepair(struct ast_channel *chan, int freq1, int freq2, int duration, int vol)
7635 {
7636 int res;
7637
7638 if ((res = ast_tonepair_start(chan, freq1, freq2, duration, vol)))
7639 return res;
7640
7641 /* Give us some wiggle room */
7642 while (ast_channel_generatordata(chan) && ast_waitfor(chan, 100) >= 0) {
7643 struct ast_frame *f = ast_read(chan);
7644 if (f)
7645 ast_frfree(f);
7646 else
7647 return -1;
7648 }
7649 return 0;
7650 }
7651
7652 ast_group_t ast_get_group(const char *s)
7653 {
7654 char *piece;
7655 char *c;
7656 int start=0, finish=0, x;
7657 ast_group_t group = 0;
7658
7659 if (ast_strlen_zero(s))
7660 return 0;
7661
7662 c = ast_strdupa(s);
7663
7664 while ((piece = strsep(&c, ","))) {
7665 if (sscanf(piece, "%30d-%30d", &start, &finish) == 2) {
7666 /* Range */
7667 } else if (sscanf(piece, "%30d", &start)) {
7668 /* Just one */
7669 finish = start;
7670 } else {
7671 ast_log(LOG_ERROR, "Syntax error parsing group configuration '%s' at '%s'. Ignoring.\n", s, piece);
7672 continue;
7673 }
7674 for (x = start; x <= finish; x++) {
7675 if ((x > 63) || (x < 0)) {
7676 ast_log(LOG_WARNING, "Ignoring invalid group %d (maximum group is 63)\n", x);
7677 } else
7678 group |= ((ast_group_t) 1 << x);
7679 }
7680 }
7681 return group;
7682 }
7683
7684 /*! \brief Named group member structure */
7685 struct namedgroup_member {
7686 /*! Pre-built hash of group member name. */
7687 unsigned int hash;
7688 /*! Group member name. (End allocation of name string.) */
7689 char name[1];
7690 };
7691
7692 /*! \brief Comparison function used for named group container */
7693 static int namedgroup_cmp_cb(void *obj, void *arg, int flags)
7694 {
7695 const struct namedgroup_member *an = obj;
7696 const struct namedgroup_member *bn = arg;
7697
7698 return strcmp(an->name, bn->name) ? 0 : CMP_MATCH | CMP_STOP;
7699 }
7700
7701 /*! \brief Hashing function used for named group container */
7702 static int namedgroup_hash_cb(const void *obj, const int flags)
7703 {
7704 const struct namedgroup_member *member = obj;
7705
7706 return member->hash;
7707 }
7708
7709 struct ast_namedgroups *ast_get_namedgroups(const char *s)
7710 {
7711 struct ao2_container *namedgroups;
7712 char *piece;
7713 char *c;
7714
7715 if (!s) {
7716 return NULL;
7717 }
7718
7719 /*! \brief Remove leading and trailing whitespace */
7720 c = ast_trim_blanks(ast_strdupa(ast_skip_blanks(s)));
7721 if (ast_strlen_zero(c)) {
7722 return NULL;
7723 }
7724
7725 namedgroups = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_NOLOCK, 0, 19,
7726 namedgroup_hash_cb, NULL, namedgroup_cmp_cb);
7727 if (!namedgroups) {
7728 return NULL;
7729 }
7730
7731 while ((piece = strsep(&c, ","))) {
7732 struct namedgroup_member *member;
7733 size_t len;
7734
7735 /* remove leading/trailing whitespace */
7736 piece = ast_strip(piece);
7737
7738 len = strlen(piece);
7739 if (!len) {
7740 continue;
7741 }
7742
7743 member = ao2_alloc_options(sizeof(*member) + len, NULL, AO2_ALLOC_OPT_LOCK_NOLOCK);
7744 if (!member) {
7745 ao2_ref(namedgroups, -1);
7746 return NULL;
7747 }
7748 strcpy(member->name, piece);/* Safe */
7749 member->hash = ast_str_hash(member->name);
7750
7751 /* every group name may exist only once, delete duplicates */
7752 ao2_find(namedgroups, member, OBJ_POINTER | OBJ_UNLINK | OBJ_NODATA);
7753 ao2_link(namedgroups, member);
7754 ao2_ref(member, -1);
7755 }
7756
7757 if (!ao2_container_count(namedgroups)) {
7758 /* There were no group names specified. */
7759 ao2_ref(namedgroups, -1);
7760 namedgroups = NULL;
7761 }
7762
7763 return (struct ast_namedgroups *) namedgroups;
7764 }
7765
7766 struct ast_namedgroups *ast_unref_namedgroups(struct ast_namedgroups *groups)
7767 {
7768 ao2_cleanup(groups);
7769 return NULL;
7770 }
7771
7772 struct ast_namedgroups *ast_ref_namedgroups(struct ast_namedgroups *groups)
7773 {
7774 if (groups) {
7775 ao2_ref(groups, 1);
7776 }
7777 return groups;
7778 }
7779
7780 static int (*ast_moh_start_ptr)(struct ast_channel *, const char *, const char *) = NULL;
7781 static void (*ast_moh_stop_ptr)(struct ast_channel *) = NULL;
7782 static void (*ast_moh_cleanup_ptr)(struct ast_channel *) = NULL;
7783
7784 void ast_install_music_functions(int (*start_ptr)(struct ast_channel *, const char *, const char *),
7785 void (*stop_ptr)(struct ast_channel *),
7786 void (*cleanup_ptr)(struct ast_channel *))
7787 {
7788 ast_moh_start_ptr = start_ptr;
7789 ast_moh_stop_ptr = stop_ptr;
7790 ast_moh_cleanup_ptr = cleanup_ptr;
7791 }
7792
7793 void ast_uninstall_music_functions(void)
7794 {
7795 ast_moh_start_ptr = NULL;
7796 ast_moh_stop_ptr = NULL;
7797 ast_moh_cleanup_ptr = NULL;
7798 }
7799
7800 int ast_moh_start(struct ast_channel *chan, const char *mclass, const char *interpclass)
7801 {
7802 if (ast_moh_start_ptr)
7803 return ast_moh_start_ptr(chan, mclass, interpclass);
7804
7805 ast_verb(3, "Music class %s requested but no musiconhold loaded.\n", mclass ? mclass : (interpclass ? interpclass : "default"));
7806
7807 return -1;
7808 }
7809
7810 void ast_moh_stop(struct ast_channel *chan)
7811 {
7812 if (ast_moh_stop_ptr)
7813 ast_moh_stop_ptr(chan);
7814 }
7815
7816 void ast_moh_cleanup(struct ast_channel *chan)
7817 {
7818 if (ast_moh_cleanup_ptr)
7819 ast_moh_cleanup_ptr(chan);
7820 }
7821
7822 /*!
7823 * \brief List of channel variables to append to all channel-related events.
7824 */
7825 struct manager_channel_variable {
7826 AST_LIST_ENTRY(manager_channel_variable) entry;
7827 unsigned int isfunc:1;
7828 char name[];
7829 };
7830
7831 AST_RWLIST_HEAD(external_vars, manager_channel_variable);
7832
7833 static struct external_vars ami_vars;
7834 static struct external_vars ari_vars;
7835
7836 static void free_external_channelvars(struct external_vars *channelvars)
7837 {
7838 struct manager_channel_variable *var;
7839 AST_RWLIST_WRLOCK(channelvars);
7840 while ((var = AST_RWLIST_REMOVE_HEAD(channelvars, entry))) {
7841 ast_free(var);
7842 }
7843 AST_RWLIST_UNLOCK(channelvars);
7844 }
7845
7846 static int channel_has_external_vars(struct external_vars *channelvars)
7847 {
7848 int vars_present;
7849
7850 AST_RWLIST_RDLOCK(channelvars);
7851 vars_present = !AST_LIST_EMPTY(channelvars);
7852 AST_RWLIST_UNLOCK(channelvars);
7853
7854 return vars_present;
7855 }
7856
7857 int ast_channel_has_manager_vars(void)
7858 {
7859 return channel_has_external_vars(&ami_vars);
7860 }
7861
7862 int ast_channel_has_ari_vars(void)
7863 {
7864 return channel_has_external_vars(&ari_vars);
7865 }
7866
7867 static void channel_set_external_vars(struct external_vars *channelvars, size_t varc, char **vars)
7868 {
7869 size_t i;
7870
7871 free_external_channelvars(channelvars);
7872 AST_RWLIST_WRLOCK(channelvars);
7873 for (i = 0; i < varc; ++i) {
7874 const char *var = vars[i];
7875 struct manager_channel_variable *mcv;
7876 if (!(mcv = ast_calloc(1, sizeof(*mcv) + strlen(var) + 1))) {
7877 break;
7878 }
7879 strcpy(mcv->name, var); /* SAFE */
7880 if (strchr(var, '(')) {
7881 mcv->isfunc = 1;
7882 }
7883 AST_RWLIST_INSERT_TAIL(channelvars, mcv, entry);
7884 }
7885 AST_RWLIST_UNLOCK(channelvars);
7886
7887 }
7888
7889 void ast_channel_set_manager_vars(size_t varc, char **vars)
7890 {
7891 channel_set_external_vars(&ami_vars, varc, vars);
7892 }
7893
7894 void ast_channel_set_ari_vars(size_t varc, char **vars)
7895 {
7896 channel_set_external_vars(&ari_vars, varc, vars);
7897 }
7898
7899 int ast_channel_set_ari_var_reportable(struct ast_channel *chan, const char *variable, int report_events)
7900 {
7901 char *var_str;
7902 size_t i, count;
7903
7904 SCOPED_CHANNELLOCK(lock, chan);
7905
7906 if (ast_strlen_zero(variable)) {
7907 return -1;
7908 }
7909
7910 count = ast_channel_internal_ari_reportable_vars_count(chan);
7911 for (i = 0; i < count; ++i) {
7912 var_str = ast_channel_internal_ari_reportable_vars_get(chan, i);
7913 if (!strcmp(var_str, variable)) {
7914 if (!report_events) {
7915 var_str = ast_channel_internal_ari_reportable_vars_remove(chan, i);
7916 ast_free(var_str);
7917 }
7918 return 0;
7919 }
7920 }
7921
7922 if (!report_events) {
7923 return 0;
7924 }
7925
7926 var_str = ast_strdup(variable);
7927 if (!var_str) {
7928 return -1;
7929 }
7930
7931 if (ast_channel_internal_ari_reportable_vars_append(chan, var_str)) {
7932 ast_free(var_str);
7933 return -1;
7934 }
7935
7936 return 0;
7937 }
7938
7939 /*!
7940 * \brief Destructor for lists of variables.
7941 * \param obj AO2 object.
7942 */
7943 static void varshead_dtor(void *obj)
7944 {
7945 struct varshead *head = obj;
7946 struct ast_var_t *var;
7947
7948 while ((var = AST_RWLIST_REMOVE_HEAD(head, entries))) {
7949 ast_var_delete(var);
7950 }
7951 }
7952
7953 struct varshead *ast_channel_get_vars(struct ast_channel *chan)
7954 {
7955 RAII_VAR(struct varshead *, ret, NULL, ao2_cleanup);
7956 struct ast_var_t *cv;
7957
7958 ret = ao2_alloc(sizeof(*ret), varshead_dtor);
7959
7960 if (!ret) {
7961 return NULL;
7962 }
7963
7964 AST_LIST_TRAVERSE(ast_channel_varshead(chan), cv, entries) {
7965 struct ast_var_t *var = ast_var_assign(ast_var_name(cv), ast_var_value(cv));
7966
7967 if (!var) {
7968 return NULL;
7969 }
7970
7971 AST_LIST_INSERT_TAIL(ret, var, entries);
7972 }
7973
7974 ao2_ref(ret, +1);
7975 return ret;
7976 }
7977
7978 static struct varshead *channel_get_external_vars(struct external_vars *channelvars,
7979 struct ast_channel *chan)
7980 {
7981 RAII_VAR(struct varshead *, ret, NULL, ao2_cleanup);
7982 RAII_VAR(struct ast_str *, tmp, NULL, ast_free);
7983 struct manager_channel_variable *mcv;
7984 SCOPED_LOCK(lock, channelvars, AST_RWLIST_RDLOCK, AST_RWLIST_UNLOCK);
7985
7986 if (AST_LIST_EMPTY(channelvars)) {
7987 return NULL;
7988 }
7989
7990 ret = ao2_alloc(sizeof(*ret), varshead_dtor);
7991 tmp = ast_str_create(16);
7992
7993 if (!ret || !tmp) {
7994 return NULL;
7995 }
7996
7997 AST_LIST_TRAVERSE(channelvars, mcv, entry) {
7998 const char *val = NULL;
7999 struct ast_var_t *var;
8000
8001 if (mcv->isfunc) {
8002 if (ast_func_read2(chan, mcv->name, &tmp, 0) == 0) {
8003 val = ast_str_buffer(tmp);
8004 } else {
8005 ast_log(LOG_ERROR,
8006 "Error invoking function %s\n", mcv->name);
8007 }
8008 } else {
8009 val = pbx_builtin_getvar_helper(chan, mcv->name);
8010 }
8011
8012 var = ast_var_assign(mcv->name, val ? val : "");
8013 if (!var) {
8014 return NULL;
8015 }
8016
8017 AST_RWLIST_INSERT_TAIL(ret, var, entries);
8018 }
8019
8020 ao2_ref(ret, +1);
8021 return ret;
8022
8023 }
8024
8025 struct varshead *ast_channel_get_manager_vars(struct ast_channel *chan)
8026 {
8027 return channel_get_external_vars(&ami_vars, chan);
8028 }
8029
8030 struct varshead *ast_channel_get_ari_vars(struct ast_channel *chan)
8031 {
8032 RAII_VAR(struct varshead *, ret, NULL, ao2_cleanup);
8033 RAII_VAR(struct ast_str *, tmp, NULL, ast_free);
8034 char *var_str;
8035 size_t i;
8036
8037 SCOPED_CHANNELLOCK(lock, chan);
8038
8039 ret = channel_get_external_vars(&ari_vars, chan);
8040
8041 if (ast_channel_internal_ari_reportable_vars_count(chan) == 0) {
8042 if (!ret) {
8043 return NULL;
8044 }
8045
8046 ao2_ref(ret, +1);
8047 return ret;
8048 }
8049
8050 if (!ret) {
8051 ret = ao2_alloc(sizeof(*ret), varshead_dtor);
8052 if (!ret) {
8053 return NULL;
8054 }
8055 }
8056
8057 tmp = ast_str_create(16);
8058 if (!tmp) {
8059 return NULL;
8060 }
8061
8062 for (i = 0; i < ast_channel_internal_ari_reportable_vars_count(chan); ++i) {
8063 const char *val = NULL;
8064 struct ast_var_t *var;
8065 int already_present = 0;
8066 struct ast_var_t *existing;
8067
8068 var_str = ast_channel_internal_ari_reportable_vars_get(chan, i);
8069
8070 AST_LIST_TRAVERSE(ret, existing, entries) {
8071 if (!strcmp(ast_var_name(existing), var_str)) {
8072 already_present = 1;
8073 break;
8074 }
8075 }
8076 if (already_present) {
8077 continue;
8078 }
8079
8080 if (strchr(var_str, '(')) {
8081 if (ast_func_read2(chan, var_str, &tmp, 0) == 0) {
8082 val = ast_str_buffer(tmp);
8083 } else {
8084 ast_log(LOG_ERROR,
8085 "Error invoking function %s\n", var_str);
8086 }
8087 } else {
8088 val = pbx_builtin_getvar_helper(chan, var_str);
8089 }
8090
8091 var = ast_var_assign(var_str, val ? val : "");
8092 if (!var) {
8093 return NULL;
8094 }
8095
8096 AST_RWLIST_INSERT_TAIL(ret, var, entries);
8097 }
8098
8099 ao2_ref(ret, +1);
8100 return ret;
8101 }
8102
8103 void ast_channel_close_storage(void)
8104 {
8105 ast_channelstorage_close(current_channel_storage_instance);
8106 }
8107
8108 static void channels_shutdown(void)
8109 {
8110 free_external_channelvars(&ami_vars);
8111 free_external_channelvars(&ari_vars);
8112
8113 ast_cli_unregister_multiple(cli_channel, ARRAY_LEN(cli_channel));
8114
8115 ast_channelstorage_close(current_channel_storage_instance);
8116 current_channel_storage_instance = NULL;
8117
8118 ast_channel_unregister(&surrogate_tech);
8119 }
8120
8121 int ast_channel_open_storage()
8122 {
8123 if (!current_channel_storage_driver) {
8124 int rc = internal_channel_set_current_storage_driver(AST_CHANNELSTORAGE_DEFAULT_TYPE);
8125 if (rc) {
8126 ast_log(LOG_ERROR, "No channel storage backends available\n");
8127 return -1;
8128 }
8129 }
8130
8131 current_channel_storage_instance =
8132 ast_channelstorage_open(current_channel_storage_driver, "channels");
8133 if (!current_channel_storage_instance) {
8134 ast_log(LOG_ERROR, "Failed to open channel storage driver '%s'\n",
8135 current_channel_storage_driver->driver_name);
8136 return -1;
8137 }
8138
8139 return 0;
8140 }
8141
8142 const char *ast_channel_get_current_storage_driver_name(void)
8143 {
8144 return current_channel_storage_driver ?
8145 current_channel_storage_driver->driver_name : "NOT SET";
8146 }
8147
8148 int internal_channel_set_current_storage_driver(const char *driver_name)
8149 {
8150 if (ast_fully_booted) {
8151 ast_log(LOG_ERROR, "Cannot change channel storage driver after Asterisk has started\n");
8152 return -1;
8153 }
8154 current_channel_storage_driver = ast_channelstorage_get_driver(driver_name);
8155 if (current_channel_storage_driver) {
8156 return 0;
8157 }
8158 ast_log(LOG_WARNING,
8159 "Invalid channel storage backend '%s' specified. Attempting to use default '%s'.\n",
8160 driver_name, AST_CHANNELSTORAGE_DEFAULT_TYPE);
8161 current_channel_storage_driver = ast_channelstorage_get_driver(AST_CHANNELSTORAGE_DEFAULT_TYPE);
8162 if (current_channel_storage_driver) {
8163 return 0;
8164 }
8165 ast_log(LOG_ERROR, "Unable to find default channel storage backend '%s'.\n",
8166 AST_CHANNELSTORAGE_DEFAULT_TYPE);
8167 return -1;
8168 }
8169
8170 int ast_channels_init(void)
8171 {
8172
8173 if (ast_channelstorage_init() != 0) {
8174 return -1;
8175 }
8176
8177 /*
8178 * channel_storage_type is a global variable set by options.c
8179 * from the "channel_storage_backend" option in asterisk.conf.
8180 */
8181 if (ast_channel_open_storage() != 0) {
8182 return -1;
8183 }
8184
8185 ast_channel_register(&surrogate_tech);
8186
8187 ast_stasis_channels_init();
8188
8189 ast_cli_register_multiple(cli_channel, ARRAY_LEN(cli_channel));
8190
8191 ast_register_cleanup(channels_shutdown);
8192
8193 AST_RWLIST_HEAD_INIT(&ami_vars);
8194 AST_RWLIST_HEAD_INIT(&ari_vars);
8195
8196 return 0;
8197 }
8198
8199 /*! \brief Print call group and pickup group ---*/
8200 char *ast_print_group(char *buf, int buflen, ast_group_t group)
8201 {
8202 unsigned int i;
8203 int first = 1;
8204 char num[3];
8205
8206 buf[0] = '\0';
8207
8208 if (!group) /* Return empty string if no group */
8209 return buf;
8210
8211 for (i = 0; i <= 63; i++) { /* Max group is 63 */
8212 if (group & ((ast_group_t) 1 << i)) {
8213 if (!first) {
8214 strncat(buf, ", ", buflen - strlen(buf) - 1);
8215 } else {
8216 first = 0;
8217 }
8218 snprintf(num, sizeof(num), "%u", i);
8219 strncat(buf, num, buflen - strlen(buf) - 1);
8220 }
8221 }
8222 return buf;
8223 }
8224
8225 char *ast_print_namedgroups(struct ast_str **buf, struct ast_namedgroups *group)
8226 {
8227 struct ao2_container *grp = (struct ao2_container *) group;
8228 struct namedgroup_member *ng;
8229 int first = 1;
8230 struct ao2_iterator it;
8231
8232 if (!grp) {
8233 return ast_str_buffer(*buf);
8234 }
8235
8236 for (it = ao2_iterator_init(grp, 0); (ng = ao2_iterator_next(&it)); ao2_ref(ng, -1)) {
8237 if (!first) {
8238 ast_str_append(buf, 0, ", ");
8239 } else {
8240 first = 0;
8241 }
8242 ast_str_append(buf, 0, "%s", ng->name);
8243 }
8244 ao2_iterator_destroy(&it);
8245
8246 return ast_str_buffer(*buf);
8247 }
8248
8249 static int namedgroup_match(void *obj, void *arg, int flags)
8250 {
8251 void *match;
8252
8253 match = ao2_find(arg, obj, OBJ_POINTER);
8254 ao2_cleanup(match);
8255
8256 return match ? CMP_MATCH | CMP_STOP : 0;
8257 }
8258
8259 int ast_namedgroups_intersect(struct ast_namedgroups *a, struct ast_namedgroups *b)
8260 {
8261 void *match;
8262 struct ao2_container *group_a = (struct ao2_container *) a;
8263 struct ao2_container *group_b = (struct ao2_container *) b;
8264
8265 if (!a || !b) {
8266 return 0;
8267 }
8268
8269 /*
8270 * Do groups a and b intersect? Since a and b are hash tables,
8271 * the average time complexity is:
8272 * O(a.count <= b.count ? a.count : b.count)
8273 */
8274 if (ao2_container_count(group_b) < ao2_container_count(group_a)) {
8275 /* Traverse over the smaller group. */
8276 SWAP(group_a, group_b);
8277 }
8278 match = ao2_callback(group_a, 0, namedgroup_match, group_b);
8279 ao2_cleanup(match);
8280
8281 return match != NULL;
8282 }
8283
8284 void ast_set_variables(struct ast_channel *chan, struct ast_variable *vars)
8285 {
8286 struct ast_variable *cur;
8287
8288 for (cur = vars; cur; cur = cur->next) {
8289 pbx_builtin_setvar_helper(chan, cur->name, cur->value);
8290 }
8291 }
8292
8293 static void *silence_generator_alloc(struct ast_channel *chan, void *data)
8294 {
8295 /* just store the data pointer in the channel structure */
8296 return data;
8297 }
8298
8299 static void silence_generator_release(struct ast_channel *chan, void *data)
8300 {
8301 /* nothing to do */
8302 }
8303
8304 static int silence_generator_generate(struct ast_channel *chan, void *data, int len, int samples)
8305 {
8306 short buf[samples];
8307 struct ast_frame frame = {
8308 .frametype = AST_FRAME_VOICE,
8309 .data.ptr = buf,
8310 .samples = samples,
8311 .datalen = sizeof(buf),
8312 };
8313 frame.subclass.format = ast_format_slin;
8314
8315 memset(buf, 0, sizeof(buf));
8316
8317 if (ast_write(chan, &frame))
8318 return -1;
8319
8320 return 0;
8321 }
8322
8323 static struct ast_generator silence_generator = {
8324 .alloc = silence_generator_alloc,
8325 .release = silence_generator_release,
8326 .generate = silence_generator_generate,
8327 };
8328
8329 struct ast_silence_generator {
8330 struct ast_format *old_write_format;
8331 };
8332
8333 struct ast_silence_generator *ast_channel_start_silence_generator(struct ast_channel *chan)
8334 {
8335 struct ast_silence_generator *state;
8336
8337 if (!(state = ast_calloc(1, sizeof(*state)))) {
8338 return NULL;
8339 }
8340
8341 state->old_write_format = ao2_bump(ast_channel_writeformat(chan));
8342
8343 if (ast_set_write_format(chan, ast_format_slin) < 0) {
8344 ast_log(LOG_ERROR, "Could not set write format to SLINEAR\n");
8345 ast_free(state);
8346 return NULL;
8347 }
8348
8349 ast_activate_generator(chan, &silence_generator, state);
8350
8351 ast_debug(1, "Started silence generator on '%s'\n", ast_channel_name(chan));
8352
8353 return state;
8354 }
8355
8356 static int deactivate_silence_generator(struct ast_channel *chan)
8357 {
8358 ast_channel_lock(chan);
8359
8360 if (!ast_channel_generatordata(chan)) {
8361 ast_debug(1, "Trying to stop silence generator when there is no generator on '%s'\n",
8362 ast_channel_name(chan));
8363 ast_channel_unlock(chan);
8364 return 0;
8365 }
8366 if (ast_channel_generator(chan) != &silence_generator) {
8367 ast_debug(1, "Trying to stop silence generator when it is not the current generator on '%s'\n",
8368 ast_channel_name(chan));
8369 ast_channel_unlock(chan);
8370 return 0;
8371 }
8372 deactivate_generator_nolock(chan);
8373
8374 ast_channel_unlock(chan);
8375
8376 return 1;
8377 }
8378
8379 void ast_channel_stop_silence_generator(struct ast_channel *chan, struct ast_silence_generator *state)
8380 {
8381 if (!state) {
8382 return;
8383 }
8384
8385 if (deactivate_silence_generator(chan)) {
8386 ast_debug(1, "Stopped silence generator on '%s'\n", ast_channel_name(chan));
8387 if (ast_set_write_format(chan, state->old_write_format) < 0) {
8388 ast_log(LOG_ERROR, "Could not return write format to its original state\n");
8389 }
8390 }
8391 ao2_cleanup(state->old_write_format);
8392 ast_free(state);
8393 }
8394
8395
8396 /*
8397 * Wrappers for various ast_say_*() functions that call the full version
8398 * of the same functions.
8399 * The proper place would be say.c, but that file is optional and one
8400 * must be able to build asterisk even without it (using a loadable 'say'
8401 * implementation that only supplies the 'full' version of the functions.
8402 */
8403
8404 int ast_say_number(struct ast_channel *chan, int num,
8405 const char *ints, const char *language, const char *options)
8406 {
8407 return ast_say_number_full(chan, num, ints, language, options, -1, -1);
8408 }
8409
8410 int ast_say_ordinal(struct ast_channel *chan, int num,
8411 const char *ints, const char *language, const char *options)
8412 {
8413 return ast_say_ordinal_full(chan, num, ints, language, options, -1, -1);
8414 }
8415
8416 int ast_say_enumeration(struct ast_channel *chan, int num,
8417 const char *ints, const char *language, const char *options)
8418 {
8419 return ast_say_enumeration_full(chan, num, ints, language, options, -1, -1);
8420 }
8421
8422 int ast_say_digits(struct ast_channel *chan, int num,
8423 const char *ints, const char *lang)
8424 {
8425 return ast_say_digits_full(chan, num, ints, lang, -1, -1);
8426 }
8427
8428 int ast_say_digit_str(struct ast_channel *chan, const char *str,
8429 const char *ints, const char *lang)
8430 {
8431 return ast_say_digit_str_full(chan, str, ints, lang, -1, -1);
8432 }
8433
8434 int ast_say_money_str(struct ast_channel *chan, const char *str,
8435 const char *ints, const char *lang)
8436 {
8437 return ast_say_money_str_full(chan, str, ints, lang, -1, -1);
8438 }
8439
8440 int ast_say_character_str(struct ast_channel *chan, const char *str,
8441 const char *ints, const char *lang, enum ast_say_case_sensitivity sensitivity)
8442 {
8443 return ast_say_character_str_full(chan, str, ints, lang, sensitivity, -1, -1);
8444 }
8445
8446 int ast_say_phonetic_str(struct ast_channel *chan, const char *str,
8447 const char *ints, const char *lang)
8448 {
8449 return ast_say_phonetic_str_full(chan, str, ints, lang, -1, -1);
8450 }
8451
8452 int ast_say_digits_full(struct ast_channel *chan, int num,
8453 const char *ints, const char *lang, int audiofd, int ctrlfd)
8454 {
8455 char buf[256];
8456
8457 snprintf(buf, sizeof(buf), "%d", num);
8458
8459 return ast_say_digit_str_full(chan, buf, ints, lang, audiofd, ctrlfd);
8460 }
8461
8462 void ast_connected_line_copy_from_caller(struct ast_party_connected_line *dest, const struct ast_party_caller *src)
8463 {
8464 ast_party_id_copy(&dest->id, &src->id);
8465 ast_party_id_copy(&dest->ani, &src->ani);
8466 dest->ani2 = src->ani2;
8467 }
8468
8469 void ast_connected_line_copy_to_caller(struct ast_party_caller *dest, const struct ast_party_connected_line *src)
8470 {
8471 ast_party_id_copy(&dest->id, &src->id);
8472 ast_party_id_copy(&dest->ani, &src->ani);
8473
8474 dest->ani2 = src->ani2;
8475 }
8476
8477 void ast_channel_set_connected_line(struct ast_channel *chan, const struct ast_party_connected_line *connected, const struct ast_set_party_connected_line *update)
8478 {
8479 if (ast_channel_connected(chan) == connected) {
8480 /* Don't set to self */
8481 return;
8482 }
8483
8484 ast_channel_lock(chan);
8485 ast_party_connected_line_set(ast_channel_connected(chan), connected, update);
8486 ast_channel_snapshot_invalidate_segment(chan, AST_CHANNEL_SNAPSHOT_INVALIDATE_CONNECTED);
8487 ast_channel_publish_snapshot(chan);
8488 ast_channel_unlock(chan);
8489 }
8490
8491 /*! \note Should follow struct ast_party_name */
8492 struct ast_party_name_ies {
8493 /*! \brief Subscriber name ie */
8494 int str;
8495 /*! \brief Character set ie. */
8496 int char_set;
8497 /*! \brief presentation-indicator ie */
8498 int presentation;
8499 /*! \brief valid/present ie */
8500 int valid;
8501 };
8502
8503 /*!
8504 * \internal
8505 * \since 1.8
8506 * \brief Build a party name information data frame component.
8507 *
8508 * \param data Buffer to fill with the frame data
8509 * \param datalen Size of the buffer to fill
8510 * \param name Party name information
8511 * \param label Name of particular party name
8512 * \param ies Data frame ie values for the party name components
8513 *
8514 * \retval -1 if error
8515 * \retval Amount of data buffer used
8516 */
8517 static int party_name_build_data(unsigned char *data, size_t datalen, const struct ast_party_name *name, const char *label, const struct ast_party_name_ies *ies)
8518 {
8519 size_t length;
8520 size_t pos = 0;
8521
8522 /*
8523 * The size of integer values must be fixed in case the frame is
8524 * shipped to another machine.
8525 */
8526 if (name->str) {
8527 length = strlen(name->str);
8528 if (datalen < pos + (sizeof(data[0]) * 2) + length) {
8529 ast_log(LOG_WARNING, "No space left for %s name\n", label);
8530 return -1;
8531 }
8532 data[pos++] = ies->str;
8533 data[pos++] = length;
8534 memcpy(data + pos, name->str, length);
8535 pos += length;
8536 }
8537
8538 if (datalen < pos + (sizeof(data[0]) * 2) + 1) {
8539 ast_log(LOG_WARNING, "No space left for %s name char set\n", label);
8540 return -1;
8541 }
8542 data[pos++] = ies->char_set;
8543 data[pos++] = 1;
8544 data[pos++] = name->char_set;
8545
8546 if (datalen < pos + (sizeof(data[0]) * 2) + 1) {
8547 ast_log(LOG_WARNING, "No space left for %s name presentation\n", label);
8548 return -1;
8549 }
8550 data[pos++] = ies->presentation;
8551 data[pos++] = 1;
8552 data[pos++] = name->presentation;
8553
8554 if (datalen < pos + (sizeof(data[0]) * 2) + 1) {
8555 ast_log(LOG_WARNING, "No space left for %s name valid\n", label);
8556 return -1;
8557 }
8558 data[pos++] = ies->valid;
8559 data[pos++] = 1;
8560 data[pos++] = name->valid;
8561
8562 return pos;
8563 }
8564
8565 /*! \note Should follow struct ast_party_number */
8566 struct ast_party_number_ies {
8567 /*! \brief Subscriber phone number ie */
8568 int str;
8569 /*! \brief Type-Of-Number and Numbering-Plan ie */
8570 int plan;
8571 /*! \brief presentation-indicator ie */
8572 int presentation;
8573 /*! \brief valid/present ie */
8574 int valid;
8575 };
8576
8577 /*!
8578 * \internal
8579 * \since 1.8
8580 * \brief Build a party number information data frame component.
8581 *
8582 * \param data Buffer to fill with the frame data
8583 * \param datalen Size of the buffer to fill
8584 * \param number Party number information
8585 * \param label Name of particular party number
8586 * \param ies Data frame ie values for the party number components
8587 *
8588 * \retval -1 if error
8589 * \retval Amount of data buffer used
8590 */
8591 static int party_number_build_data(unsigned char *data, size_t datalen, const struct ast_party_number *number, const char *label, const struct ast_party_number_ies *ies)
8592 {
8593 size_t length;
8594 size_t pos = 0;
8595
8596 /*
8597 * The size of integer values must be fixed in case the frame is
8598 * shipped to another machine.
8599 */
8600 if (number->str) {
8601 length = strlen(number->str);
8602 if (datalen < pos + (sizeof(data[0]) * 2) + length) {
8603 ast_log(LOG_WARNING, "No space left for %s number\n", label);
8604 return -1;
8605 }
8606 data[pos++] = ies->str;
8607 data[pos++] = length;
8608 memcpy(data + pos, number->str, length);
8609 pos += length;
8610 }
8611
8612 if (datalen < pos + (sizeof(data[0]) * 2) + 1) {
8613 ast_log(LOG_WARNING, "No space left for %s numbering plan\n", label);
8614 return -1;
8615 }
8616 data[pos++] = ies->plan;
8617 data[pos++] = 1;
8618 data[pos++] = number->plan;
8619
8620 if (datalen < pos + (sizeof(data[0]) * 2) + 1) {
8621 ast_log(LOG_WARNING, "No space left for %s number presentation\n", label);
8622 return -1;
8623 }
8624 data[pos++] = ies->presentation;
8625 data[pos++] = 1;
8626 data[pos++] = number->presentation;
8627
8628 if (datalen < pos + (sizeof(data[0]) * 2) + 1) {
8629 ast_log(LOG_WARNING, "No space left for %s number valid\n", label);
8630 return -1;
8631 }
8632 data[pos++] = ies->valid;
8633 data[pos++] = 1;
8634 data[pos++] = number->valid;
8635
8636 return pos;
8637 }
8638
8639 /*! \note Should follow struct ast_party_subaddress */
8640 struct ast_party_subaddress_ies {
8641 /*! \brief subaddress ie. */
8642 int str;
8643 /*! \brief subaddress type ie */
8644 int type;
8645 /*! \brief odd/even indicator ie */
8646 int odd_even_indicator;
8647 /*! \brief valid/present ie */
8648 int valid;
8649 };
8650
8651 /*!
8652 * \internal
8653 * \since 1.8
8654 * \brief Build a party subaddress information data frame component.
8655 *
8656 * \param data Buffer to fill with the frame data
8657 * \param datalen Size of the buffer to fill
8658 * \param subaddress Party subaddress information
8659 * \param label Name of particular party subaddress
8660 * \param ies Data frame ie values for the party subaddress components
8661 *
8662 * \retval -1 if error
8663 * \retval Amount of data buffer used
8664 */
8665 static int party_subaddress_build_data(unsigned char *data, size_t datalen, const struct ast_party_subaddress *subaddress, const char *label, const struct ast_party_subaddress_ies *ies)
8666 {
8667 size_t length;
8668 size_t pos = 0;
8669
8670 /*
8671 * The size of integer values must be fixed in case the frame is
8672 * shipped to another machine.
8673 */
8674 if (subaddress->str) {
8675 length = strlen(subaddress->str);
8676 if (datalen < pos + (sizeof(data[0]) * 2) + length) {
8677 ast_log(LOG_WARNING, "No space left for %s subaddress\n", label);
8678 return -1;
8679 }
8680 data[pos++] = ies->str;
8681 data[pos++] = length;
8682 memcpy(data + pos, subaddress->str, length);
8683 pos += length;
8684 }
8685
8686 if (datalen < pos + (sizeof(data[0]) * 2) + 1) {
8687 ast_log(LOG_WARNING, "No space left for %s type of subaddress\n", label);
8688 return -1;
8689 }
8690 data[pos++] = ies->type;
8691 data[pos++] = 1;
8692 data[pos++] = subaddress->type;
8693
8694 if (datalen < pos + (sizeof(data[0]) * 2) + 1) {
8695 ast_log(LOG_WARNING,
8696 "No space left for %s subaddress odd-even indicator\n", label);
8697 return -1;
8698 }
8699 data[pos++] = ies->odd_even_indicator;
8700 data[pos++] = 1;
8701 data[pos++] = subaddress->odd_even_indicator;
8702
8703 if (datalen < pos + (sizeof(data[0]) * 2) + 1) {
8704 ast_log(LOG_WARNING, "No space left for %s subaddress valid\n", label);
8705 return -1;
8706 }
8707 data[pos++] = ies->valid;
8708 data[pos++] = 1;
8709 data[pos++] = subaddress->valid;
8710
8711 return pos;
8712 }
8713
8714 /*! \note Should follow struct ast_party_id */
8715 struct ast_party_id_ies {
8716 /*! \brief Subscriber name ies */
8717 struct ast_party_name_ies name;
8718 /*! \brief Subscriber phone number ies */
8719 struct ast_party_number_ies number;
8720 /*! \brief Subscriber subaddress ies. */
8721 struct ast_party_subaddress_ies subaddress;
8722 /*! \brief User party id tag ie. */
8723 int tag;
8724 /*!
8725 * \brief Combined name and number presentation ie.
8726 * \note Not sent if value is zero.
8727 */
8728 int combined_presentation;
8729 };
8730
8731 /*!
8732 * \internal
8733 * \since 1.8
8734 * \brief Build a party id information data frame component.
8735 *
8736 * \param data Buffer to fill with the frame data
8737 * \param datalen Size of the buffer to fill
8738 * \param id Party id information
8739 * \param label Name of particular party id
8740 * \param ies Data frame ie values for the party id components
8741 * \param update What id information to build. NULL if all.
8742 *
8743 * \retval -1 if error
8744 * \retval Amount of data buffer used
8745 */
8746 static int party_id_build_data(unsigned char *data, size_t datalen,
8747 const struct ast_party_id *id, const char *label, const struct ast_party_id_ies *ies,
8748 const struct ast_set_party_id *update)
8749 {
8750 size_t length;
8751 size_t pos = 0;
8752 int res;
8753
8754 /*
8755 * The size of integer values must be fixed in case the frame is
8756 * shipped to another machine.
8757 */
8758
8759 if (!update || update->name) {
8760 res = party_name_build_data(data + pos, datalen - pos, &id->name, label,
8761 &ies->name);
8762 if (res < 0) {
8763 return -1;
8764 }
8765 pos += res;
8766 }
8767
8768 if (!update || update->number) {
8769 res = party_number_build_data(data + pos, datalen - pos, &id->number, label,
8770 &ies->number);
8771 if (res < 0) {
8772 return -1;
8773 }
8774 pos += res;
8775 }
8776
8777 if (!update || update->subaddress) {
8778 res = party_subaddress_build_data(data + pos, datalen - pos, &id->subaddress,
8779 label, &ies->subaddress);
8780 if (res < 0) {
8781 return -1;
8782 }
8783 pos += res;
8784 }
8785
8786 /* *************** Party id user tag **************************** */
8787 if (id->tag) {
8788 length = strlen(id->tag);
8789 if (datalen < pos + (sizeof(data[0]) * 2) + length) {
8790 ast_log(LOG_WARNING, "No space left for %s tag\n", label);
8791 return -1;
8792 }
8793 data[pos++] = ies->tag;
8794 data[pos++] = length;
8795 memcpy(data + pos, id->tag, length);
8796 pos += length;
8797 }
8798
8799 /* *************** Party id combined presentation *************** */
8800 if (ies->combined_presentation && (!update || update->number)) {
8801 int presentation;
8802
8803 if (!update || update->name) {
8804 presentation = ast_party_id_presentation(id);
8805 } else {
8806 /*
8807 * We must compromise because not all the information is available
8808 * to determine a combined presentation value.
8809 * We will only send the number presentation instead.
8810 */
8811 presentation = id->number.presentation;
8812 }
8813
8814 if (datalen < pos + (sizeof(data[0]) * 2) + 1) {
8815 ast_log(LOG_WARNING, "No space left for %s combined presentation\n", label);
8816 return -1;
8817 }
8818 data[pos++] = ies->combined_presentation;
8819 data[pos++] = 1;
8820 data[pos++] = presentation;
8821 }
8822
8823 return pos;
8824 }
8825
8826 /*!
8827 * \brief Element identifiers for connected line indication frame data
8828 * \note Only add to the end of this enum.
8829 */
8830 enum {
8831 AST_CONNECTED_LINE_NUMBER,
8832 AST_CONNECTED_LINE_NAME,
8833 AST_CONNECTED_LINE_NUMBER_PLAN,
8834 AST_CONNECTED_LINE_ID_PRESENTATION,/* Combined number and name presentation. */
8835 AST_CONNECTED_LINE_SOURCE,
8836 AST_CONNECTED_LINE_SUBADDRESS,
8837 AST_CONNECTED_LINE_SUBADDRESS_TYPE,
8838 AST_CONNECTED_LINE_SUBADDRESS_ODD_EVEN,
8839 AST_CONNECTED_LINE_SUBADDRESS_VALID,
8840 AST_CONNECTED_LINE_TAG,
8841 AST_CONNECTED_LINE_VERSION,
8842 /*
8843 * No more party id combined number and name presentation values
8844 * need to be created.
8845 */
8846 AST_CONNECTED_LINE_NAME_VALID,
8847 AST_CONNECTED_LINE_NAME_CHAR_SET,
8848 AST_CONNECTED_LINE_NAME_PRESENTATION,
8849 AST_CONNECTED_LINE_NUMBER_VALID,
8850 AST_CONNECTED_LINE_NUMBER_PRESENTATION,
8851 AST_CONNECTED_LINE_PRIV_NUMBER,
8852 AST_CONNECTED_LINE_PRIV_NUMBER_PLAN,
8853 AST_CONNECTED_LINE_PRIV_NUMBER_VALID,
8854 AST_CONNECTED_LINE_PRIV_NUMBER_PRESENTATION,
8855 AST_CONNECTED_LINE_PRIV_NAME,
8856 AST_CONNECTED_LINE_PRIV_NAME_VALID,
8857 AST_CONNECTED_LINE_PRIV_NAME_CHAR_SET,
8858 AST_CONNECTED_LINE_PRIV_NAME_PRESENTATION,
8859 AST_CONNECTED_LINE_PRIV_SUBADDRESS,
8860 AST_CONNECTED_LINE_PRIV_SUBADDRESS_TYPE,
8861 AST_CONNECTED_LINE_PRIV_SUBADDRESS_ODD_EVEN,
8862 AST_CONNECTED_LINE_PRIV_SUBADDRESS_VALID,
8863 AST_CONNECTED_LINE_PRIV_TAG,
8864 };
8865
8866 int ast_connected_line_build_data(unsigned char *data, size_t datalen, const struct ast_party_connected_line *connected, const struct ast_set_party_connected_line *update)
8867 {
8868 int32_t value;
8869 size_t pos = 0;
8870 int res;
8871
8872 static const struct ast_party_id_ies ies = {
8873 .name.str = AST_CONNECTED_LINE_NAME,
8874 .name.char_set = AST_CONNECTED_LINE_NAME_CHAR_SET,
8875 .name.presentation = AST_CONNECTED_LINE_NAME_PRESENTATION,
8876 .name.valid = AST_CONNECTED_LINE_NAME_VALID,
8877
8878 .number.str = AST_CONNECTED_LINE_NUMBER,
8879 .number.plan = AST_CONNECTED_LINE_NUMBER_PLAN,
8880 .number.presentation = AST_CONNECTED_LINE_NUMBER_PRESENTATION,
8881 .number.valid = AST_CONNECTED_LINE_NUMBER_VALID,
8882
8883 .subaddress.str = AST_CONNECTED_LINE_SUBADDRESS,
8884 .subaddress.type = AST_CONNECTED_LINE_SUBADDRESS_TYPE,
8885 .subaddress.odd_even_indicator = AST_CONNECTED_LINE_SUBADDRESS_ODD_EVEN,
8886 .subaddress.valid = AST_CONNECTED_LINE_SUBADDRESS_VALID,
8887
8888 .tag = AST_CONNECTED_LINE_TAG,
8889 .combined_presentation = AST_CONNECTED_LINE_ID_PRESENTATION,
8890 };
8891
8892 static const struct ast_party_id_ies priv_ies = {
8893 .name.str = AST_CONNECTED_LINE_PRIV_NAME,
8894 .name.char_set = AST_CONNECTED_LINE_PRIV_NAME_CHAR_SET,
8895 .name.presentation = AST_CONNECTED_LINE_PRIV_NAME_PRESENTATION,
8896 .name.valid = AST_CONNECTED_LINE_PRIV_NAME_VALID,
8897
8898 .number.str = AST_CONNECTED_LINE_PRIV_NUMBER,
8899 .number.plan = AST_CONNECTED_LINE_PRIV_NUMBER_PLAN,
8900 .number.presentation = AST_CONNECTED_LINE_PRIV_NUMBER_PRESENTATION,
8901 .number.valid = AST_CONNECTED_LINE_PRIV_NUMBER_VALID,
8902
8903 .subaddress.str = AST_CONNECTED_LINE_PRIV_SUBADDRESS,
8904 .subaddress.type = AST_CONNECTED_LINE_PRIV_SUBADDRESS_TYPE,
8905 .subaddress.odd_even_indicator = AST_CONNECTED_LINE_PRIV_SUBADDRESS_ODD_EVEN,
8906 .subaddress.valid = AST_CONNECTED_LINE_PRIV_SUBADDRESS_VALID,
8907
8908 .tag = AST_CONNECTED_LINE_PRIV_TAG,
8909 .combined_presentation = 0,/* Not sent. */
8910 };
8911
8912 /*
8913 * The size of integer values must be fixed in case the frame is
8914 * shipped to another machine.
8915 */
8916
8917 /* Connected line frame version */
8918 if (datalen < pos + (sizeof(data[0]) * 2) + 1) {
8919 ast_log(LOG_WARNING, "No space left for connected line frame version\n");
8920 return -1;
8921 }
8922 data[pos++] = AST_CONNECTED_LINE_VERSION;
8923 data[pos++] = 1;
8924 data[pos++] = 2;/* Version 1 did not have a version ie */
8925
8926 res = party_id_build_data(data + pos, datalen - pos, &connected->id,
8927 "connected line", &ies, update ? &update->id : NULL);
8928 if (res < 0) {
8929 return -1;
8930 }
8931 pos += res;
8932
8933 res = party_id_build_data(data + pos, datalen - pos, &connected->priv,
8934 "connected line priv", &priv_ies, update ? &update->priv : NULL);
8935 if (res < 0) {
8936 return -1;
8937 }
8938 pos += res;
8939
8940 /* Connected line source */
8941 if (datalen < pos + (sizeof(data[0]) * 2) + sizeof(value)) {
8942 ast_log(LOG_WARNING, "No space left for connected line source\n");
8943 return -1;
8944 }
8945 data[pos++] = AST_CONNECTED_LINE_SOURCE;
8946 data[pos++] = sizeof(value);
8947 value = htonl(connected->source);
8948 memcpy(data + pos, &value, sizeof(value));
8949 pos += sizeof(value);
8950
8951 return pos;
8952 }
8953
8954 int ast_connected_line_parse_data(const unsigned char *data, size_t datalen, struct ast_party_connected_line *connected)
8955 {
8956 size_t pos;
8957 unsigned char ie_len;
8958 unsigned char ie_id;
8959 int32_t value;
8960 int frame_version = 1;
8961 int combined_presentation = 0;
8962 int got_combined_presentation = 0;/* TRUE if got a combined name and number presentation value. */
8963
8964 for (pos = 0; pos < datalen; pos += ie_len) {
8965 if (datalen < pos + sizeof(ie_id) + sizeof(ie_len)) {
8966 ast_log(LOG_WARNING, "Invalid connected line update\n");
8967 return -1;
8968 }
8969 ie_id = data[pos++];
8970 ie_len = data[pos++];
8971 if (datalen < pos + ie_len) {
8972 ast_log(LOG_WARNING, "Invalid connected line update\n");
8973 return -1;
8974 }
8975
8976 switch (ie_id) {
8977 /* Connected line party frame version */
8978 case AST_CONNECTED_LINE_VERSION:
8979 if (ie_len != 1) {
8980 ast_log(LOG_WARNING, "Invalid connected line frame version (%u)\n",
8981 (unsigned) ie_len);
8982 break;
8983 }
8984 frame_version = data[pos];
8985 break;
8986 /* Connected line party id name */
8987 case AST_CONNECTED_LINE_NAME:
8988 ast_free(connected->id.name.str);
8989 connected->id.name.str = ast_malloc(ie_len + 1);
8990 if (connected->id.name.str) {
8991 memcpy(connected->id.name.str, data + pos, ie_len);
8992 connected->id.name.str[ie_len] = 0;
8993 }
8994 break;
8995 case AST_CONNECTED_LINE_NAME_CHAR_SET:
8996 if (ie_len != 1) {
8997 ast_log(LOG_WARNING, "Invalid connected line name char set (%u)\n",
8998 (unsigned) ie_len);
8999 break;
9000 }
9001 connected->id.name.char_set = data[pos];
9002 break;
9003 case AST_CONNECTED_LINE_NAME_PRESENTATION:
9004 if (ie_len != 1) {
9005 ast_log(LOG_WARNING, "Invalid connected line name presentation (%u)\n",
9006 (unsigned) ie_len);
9007 break;
9008 }
9009 connected->id.name.presentation = data[pos];
9010 break;
9011 case AST_CONNECTED_LINE_NAME_VALID:
9012 if (ie_len != 1) {
9013 ast_log(LOG_WARNING, "Invalid connected line name valid (%u)\n",
9014 (unsigned) ie_len);
9015 break;
9016 }
9017 connected->id.name.valid = data[pos];
9018 break;
9019 /* Connected line party id number */
9020 case AST_CONNECTED_LINE_NUMBER:
9021 ast_free(connected->id.number.str);
9022 connected->id.number.str = ast_malloc(ie_len + 1);
9023 if (connected->id.number.str) {
9024 memcpy(connected->id.number.str, data + pos, ie_len);
9025 connected->id.number.str[ie_len] = 0;
9026 }
9027 break;
9028 case AST_CONNECTED_LINE_NUMBER_PLAN:
9029 if (ie_len != 1) {
9030 ast_log(LOG_WARNING, "Invalid connected line numbering plan (%u)\n",
9031 (unsigned) ie_len);
9032 break;
9033 }
9034 connected->id.number.plan = data[pos];
9035 break;
9036 case AST_CONNECTED_LINE_NUMBER_PRESENTATION:
9037 if (ie_len != 1) {
9038 ast_log(LOG_WARNING, "Invalid connected line number presentation (%u)\n",
9039 (unsigned) ie_len);
9040 break;
9041 }
9042 connected->id.number.presentation = data[pos];
9043 break;
9044 case AST_CONNECTED_LINE_NUMBER_VALID:
9045 if (ie_len != 1) {
9046 ast_log(LOG_WARNING, "Invalid connected line number valid (%u)\n",
9047 (unsigned) ie_len);
9048 break;
9049 }
9050 connected->id.number.valid = data[pos];
9051 break;
9052 /* Connected line party id subaddress */
9053 case AST_CONNECTED_LINE_SUBADDRESS:
9054 ast_free(connected->id.subaddress.str);
9055 connected->id.subaddress.str = ast_malloc(ie_len + 1);
9056 if (connected->id.subaddress.str) {
9057 memcpy(connected->id.subaddress.str, data + pos, ie_len);
9058 connected->id.subaddress.str[ie_len] = 0;
9059 }
9060 break;
9061 case AST_CONNECTED_LINE_SUBADDRESS_TYPE:
9062 if (ie_len != 1) {
9063 ast_log(LOG_WARNING, "Invalid connected line type of subaddress (%u)\n",
9064 (unsigned) ie_len);
9065 break;
9066 }
9067 connected->id.subaddress.type = data[pos];
9068 break;
9069 case AST_CONNECTED_LINE_SUBADDRESS_ODD_EVEN:
9070 if (ie_len != 1) {
9071 ast_log(LOG_WARNING,
9072 "Invalid connected line subaddress odd-even indicator (%u)\n",
9073 (unsigned) ie_len);
9074 break;
9075 }
9076 connected->id.subaddress.odd_even_indicator = data[pos];
9077 break;
9078 case AST_CONNECTED_LINE_SUBADDRESS_VALID:
9079 if (ie_len != 1) {
9080 ast_log(LOG_WARNING, "Invalid connected line subaddress valid (%u)\n",
9081 (unsigned) ie_len);
9082 break;
9083 }
9084 connected->id.subaddress.valid = data[pos];
9085 break;
9086 /* Connected line party tag */
9087 case AST_CONNECTED_LINE_TAG:
9088 ast_free(connected->id.tag);
9089 connected->id.tag = ast_malloc(ie_len + 1);
9090 if (connected->id.tag) {
9091 memcpy(connected->id.tag, data + pos, ie_len);
9092 connected->id.tag[ie_len] = 0;
9093 }
9094 break;
9095 /* Connected line party id combined presentation */
9096 case AST_CONNECTED_LINE_ID_PRESENTATION:
9097 if (ie_len != 1) {
9098 ast_log(LOG_WARNING, "Invalid connected line combined presentation (%u)\n",
9099 (unsigned) ie_len);
9100 break;
9101 }
9102 combined_presentation = data[pos];
9103 got_combined_presentation = 1;
9104 break;
9105 /* Private connected line party id name */
9106 case AST_CONNECTED_LINE_PRIV_NAME:
9107 ast_free(connected->priv.name.str);
9108 connected->priv.name.str = ast_malloc(ie_len + 1);
9109 if (connected->priv.name.str) {
9110 memcpy(connected->priv.name.str, data + pos, ie_len);
9111 connected->priv.name.str[ie_len] = 0;
9112 }
9113 break;
9114 case AST_CONNECTED_LINE_PRIV_NAME_CHAR_SET:
9115 if (ie_len != 1) {
9116 ast_log(LOG_WARNING, "Invalid connected line private name char set (%u)\n",
9117 (unsigned) ie_len);
9118 break;
9119 }
9120 connected->priv.name.char_set = data[pos];
9121 break;
9122 case AST_CONNECTED_LINE_PRIV_NAME_PRESENTATION:
9123 if (ie_len != 1) {
9124 ast_log(LOG_WARNING, "Invalid connected line private name presentation (%u)\n",
9125 (unsigned) ie_len);
9126 break;
9127 }
9128 connected->priv.name.presentation = data[pos];
9129 break;
9130 case AST_CONNECTED_LINE_PRIV_NAME_VALID:
9131 if (ie_len != 1) {
9132 ast_log(LOG_WARNING, "Invalid connected line private name valid (%u)\n",
9133 (unsigned) ie_len);
9134 break;
9135 }
9136 connected->priv.name.valid = data[pos];
9137 break;
9138 /* Private connected line party id number */
9139 case AST_CONNECTED_LINE_PRIV_NUMBER:
9140 ast_free(connected->priv.number.str);
9141 connected->priv.number.str = ast_malloc(ie_len + 1);
9142 if (connected->priv.number.str) {
9143 memcpy(connected->priv.number.str, data + pos, ie_len);
9144 connected->priv.number.str[ie_len] = 0;
9145 }
9146 break;
9147 case AST_CONNECTED_LINE_PRIV_NUMBER_PLAN:
9148 if (ie_len != 1) {
9149 ast_log(LOG_WARNING, "Invalid connected line private numbering plan (%u)\n",
9150 (unsigned) ie_len);
9151 break;
9152 }
9153 connected->priv.number.plan = data[pos];
9154 break;
9155 case AST_CONNECTED_LINE_PRIV_NUMBER_PRESENTATION:
9156 if (ie_len != 1) {
9157 ast_log(LOG_WARNING, "Invalid connected line private number presentation (%u)\n",
9158 (unsigned) ie_len);
9159 break;
9160 }
9161 connected->priv.number.presentation = data[pos];
9162 break;
9163 case AST_CONNECTED_LINE_PRIV_NUMBER_VALID:
9164 if (ie_len != 1) {
9165 ast_log(LOG_WARNING, "Invalid connected line private number valid (%u)\n",
9166 (unsigned) ie_len);
9167 break;
9168 }
9169 connected->priv.number.valid = data[pos];
9170 break;
9171 /* Private connected line party id subaddress */
9172 case AST_CONNECTED_LINE_PRIV_SUBADDRESS:
9173 ast_free(connected->priv.subaddress.str);
9174 connected->priv.subaddress.str = ast_malloc(ie_len + 1);
9175 if (connected->priv.subaddress.str) {
9176 memcpy(connected->priv.subaddress.str, data + pos, ie_len);
9177 connected->priv.subaddress.str[ie_len] = 0;
9178 }
9179 break;
9180 case AST_CONNECTED_LINE_PRIV_SUBADDRESS_TYPE:
9181 if (ie_len != 1) {
9182 ast_log(LOG_WARNING, "Invalid connected line private type of subaddress (%u)\n",
9183 (unsigned) ie_len);
9184 break;
9185 }
9186 connected->priv.subaddress.type = data[pos];
9187 break;
9188 case AST_CONNECTED_LINE_PRIV_SUBADDRESS_ODD_EVEN:
9189 if (ie_len != 1) {
9190 ast_log(LOG_WARNING,
9191 "Invalid connected line private subaddress odd-even indicator (%u)\n",
9192 (unsigned) ie_len);
9193 break;
9194 }
9195 connected->priv.subaddress.odd_even_indicator = data[pos];
9196 break;
9197 case AST_CONNECTED_LINE_PRIV_SUBADDRESS_VALID:
9198 if (ie_len != 1) {
9199 ast_log(LOG_WARNING, "Invalid connected line private subaddress valid (%u)\n",
9200 (unsigned) ie_len);
9201 break;
9202 }
9203 connected->priv.subaddress.valid = data[pos];
9204 break;
9205 /* Private connected line party tag */
9206 case AST_CONNECTED_LINE_PRIV_TAG:
9207 ast_free(connected->priv.tag);
9208 connected->priv.tag = ast_malloc(ie_len + 1);
9209 if (connected->priv.tag) {
9210 memcpy(connected->priv.tag, data + pos, ie_len);
9211 connected->priv.tag[ie_len] = 0;
9212 }
9213 break;
9214 /* Connected line party source */
9215 case AST_CONNECTED_LINE_SOURCE:
9216 if (ie_len != sizeof(value)) {
9217 ast_log(LOG_WARNING, "Invalid connected line source (%u)\n",
9218 (unsigned) ie_len);
9219 break;
9220 }
9221 memcpy(&value, data + pos, sizeof(value));
9222 connected->source = ntohl(value);
9223 break;
9224 /* Connected line party unknown element */
9225 default:
9226 ast_debug(1, "Unknown connected line element: %u (%u)\n",
9227 (unsigned) ie_id, (unsigned) ie_len);
9228 break;
9229 }
9230 }
9231
9232 switch (frame_version) {
9233 case 1:
9234 /*
9235 * The other end is an earlier version that we need to adjust
9236 * for compatibility.
9237 */
9238 connected->id.name.valid = 1;
9239 connected->id.name.char_set = AST_PARTY_CHAR_SET_ISO8859_1;
9240 connected->id.number.valid = 1;
9241 if (got_combined_presentation) {
9242 connected->id.name.presentation = combined_presentation;
9243 connected->id.number.presentation = combined_presentation;
9244 }
9245 break;
9246 case 2:
9247 /* The other end is at the same level as we are. */
9248 break;
9249 default:
9250 /*
9251 * The other end is newer than we are.
9252 * We need to assume that they are compatible with us.
9253 */
9254 ast_debug(1, "Connected line frame has newer version: %u\n",
9255 (unsigned) frame_version);
9256 break;
9257 }
9258
9259 return 0;
9260 }
9261
9262 void ast_channel_update_connected_line(struct ast_channel *chan, const struct ast_party_connected_line *connected, const struct ast_set_party_connected_line *update)
9263 {
9264 unsigned char data[1024]; /* This should be large enough */
9265 size_t datalen;
9266
9267 datalen = ast_connected_line_build_data(data, sizeof(data), connected, update);
9268 if (datalen == (size_t) -1) {
9269 return;
9270 }
9271
9272 ast_indicate_data(chan, AST_CONTROL_CONNECTED_LINE, data, datalen);
9273 }
9274
9275 void ast_channel_queue_connected_line_update(struct ast_channel *chan, const struct ast_party_connected_line *connected, const struct ast_set_party_connected_line *update)
9276 {
9277 unsigned char data[1024]; /* This should be large enough */
9278 size_t datalen;
9279
9280 datalen = ast_connected_line_build_data(data, sizeof(data), connected, update);
9281 if (datalen == (size_t) -1) {
9282 return;
9283 }
9284
9285 ast_queue_control_data(chan, AST_CONTROL_CONNECTED_LINE, data, datalen);
9286 }
9287
9288 void ast_channel_set_redirecting(struct ast_channel *chan, const struct ast_party_redirecting *redirecting, const struct ast_set_party_redirecting *update)
9289 {
9290 if (ast_channel_redirecting(chan) == redirecting) {
9291 /* Don't set to self */
9292 return;
9293 }
9294
9295 ast_channel_lock(chan);
9296 ast_party_redirecting_set(ast_channel_redirecting(chan), redirecting, update);
9297 ast_channel_snapshot_invalidate_segment(chan, AST_CHANNEL_SNAPSHOT_INVALIDATE_CALLER);
9298 ast_channel_publish_snapshot(chan);
9299 ast_channel_unlock(chan);
9300 }
9301
9302 /*!
9303 * \brief Element identifiers for redirecting indication frame data
9304 * \note Only add to the end of this enum.
9305 */
9306 enum {
9307 AST_REDIRECTING_FROM_NUMBER,
9308 AST_REDIRECTING_FROM_NAME,
9309 AST_REDIRECTING_FROM_NUMBER_PLAN,
9310 AST_REDIRECTING_FROM_ID_PRESENTATION,/* Combined number and name presentation. */
9311 AST_REDIRECTING_TO_NUMBER,
9312 AST_REDIRECTING_TO_NAME,
9313 AST_REDIRECTING_TO_NUMBER_PLAN,
9314 AST_REDIRECTING_TO_ID_PRESENTATION,/* Combined number and name presentation. */
9315 AST_REDIRECTING_REASON_CODE,
9316 AST_REDIRECTING_COUNT,
9317 AST_REDIRECTING_FROM_SUBADDRESS,
9318 AST_REDIRECTING_FROM_SUBADDRESS_TYPE,
9319 AST_REDIRECTING_FROM_SUBADDRESS_ODD_EVEN,
9320 AST_REDIRECTING_FROM_SUBADDRESS_VALID,
9321 AST_REDIRECTING_TO_SUBADDRESS,
9322 AST_REDIRECTING_TO_SUBADDRESS_TYPE,
9323 AST_REDIRECTING_TO_SUBADDRESS_ODD_EVEN,
9324 AST_REDIRECTING_TO_SUBADDRESS_VALID,
9325 AST_REDIRECTING_FROM_TAG,
9326 AST_REDIRECTING_TO_TAG,
9327 AST_REDIRECTING_VERSION,
9328 /*
9329 * No more party id combined number and name presentation values
9330 * need to be created.
9331 */
9332 AST_REDIRECTING_FROM_NAME_VALID,
9333 AST_REDIRECTING_FROM_NAME_CHAR_SET,
9334 AST_REDIRECTING_FROM_NAME_PRESENTATION,
9335 AST_REDIRECTING_FROM_NUMBER_VALID,
9336 AST_REDIRECTING_FROM_NUMBER_PRESENTATION,
9337 AST_REDIRECTING_TO_NAME_VALID,
9338 AST_REDIRECTING_TO_NAME_CHAR_SET,
9339 AST_REDIRECTING_TO_NAME_PRESENTATION,
9340 AST_REDIRECTING_TO_NUMBER_VALID,
9341 AST_REDIRECTING_TO_NUMBER_PRESENTATION,
9342 AST_REDIRECTING_ORIG_NUMBER,
9343 AST_REDIRECTING_ORIG_NUMBER_VALID,
9344 AST_REDIRECTING_ORIG_NUMBER_PLAN,
9345 AST_REDIRECTING_ORIG_NUMBER_PRESENTATION,
9346 AST_REDIRECTING_ORIG_NAME,
9347 AST_REDIRECTING_ORIG_NAME_VALID,
9348 AST_REDIRECTING_ORIG_NAME_CHAR_SET,
9349 AST_REDIRECTING_ORIG_NAME_PRESENTATION,
9350 AST_REDIRECTING_ORIG_SUBADDRESS,
9351 AST_REDIRECTING_ORIG_SUBADDRESS_TYPE,
9352 AST_REDIRECTING_ORIG_SUBADDRESS_ODD_EVEN,
9353 AST_REDIRECTING_ORIG_SUBADDRESS_VALID,
9354 AST_REDIRECTING_ORIG_TAG,
9355 AST_REDIRECTING_ORIG_REASON_CODE,
9356 AST_REDIRECTING_PRIV_TO_NUMBER,
9357 AST_REDIRECTING_PRIV_TO_NUMBER_PLAN,
9358 AST_REDIRECTING_PRIV_TO_NUMBER_VALID,
9359 AST_REDIRECTING_PRIV_TO_NUMBER_PRESENTATION,
9360 AST_REDIRECTING_PRIV_TO_NAME,
9361 AST_REDIRECTING_PRIV_TO_NAME_VALID,
9362 AST_REDIRECTING_PRIV_TO_NAME_CHAR_SET,
9363 AST_REDIRECTING_PRIV_TO_NAME_PRESENTATION,
9364 AST_REDIRECTING_PRIV_TO_SUBADDRESS,
9365 AST_REDIRECTING_PRIV_TO_SUBADDRESS_TYPE,
9366 AST_REDIRECTING_PRIV_TO_SUBADDRESS_ODD_EVEN,
9367 AST_REDIRECTING_PRIV_TO_SUBADDRESS_VALID,
9368 AST_REDIRECTING_PRIV_TO_TAG,
9369 AST_REDIRECTING_PRIV_FROM_NUMBER,
9370 AST_REDIRECTING_PRIV_FROM_NUMBER_PLAN,
9371 AST_REDIRECTING_PRIV_FROM_NUMBER_VALID,
9372 AST_REDIRECTING_PRIV_FROM_NUMBER_PRESENTATION,
9373 AST_REDIRECTING_PRIV_FROM_NAME,
9374 AST_REDIRECTING_PRIV_FROM_NAME_VALID,
9375 AST_REDIRECTING_PRIV_FROM_NAME_CHAR_SET,
9376 AST_REDIRECTING_PRIV_FROM_NAME_PRESENTATION,
9377 AST_REDIRECTING_PRIV_FROM_SUBADDRESS,
9378 AST_REDIRECTING_PRIV_FROM_SUBADDRESS_TYPE,
9379 AST_REDIRECTING_PRIV_FROM_SUBADDRESS_ODD_EVEN,
9380 AST_REDIRECTING_PRIV_FROM_SUBADDRESS_VALID,
9381 AST_REDIRECTING_PRIV_FROM_TAG,
9382 AST_REDIRECTING_PRIV_ORIG_NUMBER,
9383 AST_REDIRECTING_PRIV_ORIG_NUMBER_VALID,
9384 AST_REDIRECTING_PRIV_ORIG_NUMBER_PLAN,
9385 AST_REDIRECTING_PRIV_ORIG_NUMBER_PRESENTATION,
9386 AST_REDIRECTING_PRIV_ORIG_NAME,
9387 AST_REDIRECTING_PRIV_ORIG_NAME_VALID,
9388 AST_REDIRECTING_PRIV_ORIG_NAME_CHAR_SET,
9389 AST_REDIRECTING_PRIV_ORIG_NAME_PRESENTATION,
9390 AST_REDIRECTING_PRIV_ORIG_SUBADDRESS,
9391 AST_REDIRECTING_PRIV_ORIG_SUBADDRESS_TYPE,
9392 AST_REDIRECTING_PRIV_ORIG_SUBADDRESS_ODD_EVEN,
9393 AST_REDIRECTING_PRIV_ORIG_SUBADDRESS_VALID,
9394 AST_REDIRECTING_PRIV_ORIG_TAG,
9395 AST_REDIRECTING_REASON_STR,
9396 AST_REDIRECTING_ORIG_REASON_STR,
9397 };
9398
9399 struct ast_party_redirecting_reason_ies {
9400 int code;
9401 int str;
9402 };
9403
9404 static int redirecting_reason_build_data(unsigned char *data, size_t datalen,
9405 const struct ast_party_redirecting_reason *reason, const char *label,
9406 const struct ast_party_redirecting_reason_ies *ies)
9407 {
9408 size_t length;
9409 size_t pos = 0;
9410 int32_t value;
9411
9412 if (datalen < pos + (sizeof(data[0]) * 2) + sizeof(value)) {
9413 ast_log(LOG_WARNING, "No space left for %s code\n", label);
9414 return -1;
9415 }
9416 data[pos++] = ies->code;
9417 data[pos++] = sizeof(value);
9418 value = htonl(reason->code);
9419 memcpy(data + pos, &value, sizeof(value));
9420 pos += sizeof(value);
9421
9422 if (reason->str) {
9423 length = strlen(reason->str);
9424 if (datalen < pos + (sizeof(data[0]) * 2) + length) {
9425 ast_log(LOG_WARNING, "No space left for %s string\n", label);
9426 return -1;
9427 }
9428 data[pos++] = ies->str;
9429 data[pos++] = length;
9430 memcpy(data + pos, reason->str, length);
9431 pos += length;
9432 }
9433
9434 return pos;
9435 }
9436
9437 int ast_redirecting_build_data(unsigned char *data, size_t datalen, const struct ast_party_redirecting *redirecting, const struct ast_set_party_redirecting *update)
9438 {
9439 int32_t value;
9440 size_t pos = 0;
9441 int res;
9442
9443 static const struct ast_party_id_ies orig_ies = {
9444 .name.str = AST_REDIRECTING_ORIG_NAME,
9445 .name.char_set = AST_REDIRECTING_ORIG_NAME_CHAR_SET,
9446 .name.presentation = AST_REDIRECTING_ORIG_NAME_PRESENTATION,
9447 .name.valid = AST_REDIRECTING_ORIG_NAME_VALID,
9448
9449 .number.str = AST_REDIRECTING_ORIG_NUMBER,
9450 .number.plan = AST_REDIRECTING_ORIG_NUMBER_PLAN,
9451 .number.presentation = AST_REDIRECTING_ORIG_NUMBER_PRESENTATION,
9452 .number.valid = AST_REDIRECTING_ORIG_NUMBER_VALID,
9453
9454 .subaddress.str = AST_REDIRECTING_ORIG_SUBADDRESS,
9455 .subaddress.type = AST_REDIRECTING_ORIG_SUBADDRESS_TYPE,
9456 .subaddress.odd_even_indicator = AST_REDIRECTING_ORIG_SUBADDRESS_ODD_EVEN,
9457 .subaddress.valid = AST_REDIRECTING_ORIG_SUBADDRESS_VALID,
9458
9459 .tag = AST_REDIRECTING_ORIG_TAG,
9460 .combined_presentation = 0,/* Not sent. */
9461 };
9462 static const struct ast_party_id_ies from_ies = {
9463 .name.str = AST_REDIRECTING_FROM_NAME,
9464 .name.char_set = AST_REDIRECTING_FROM_NAME_CHAR_SET,
9465 .name.presentation = AST_REDIRECTING_FROM_NAME_PRESENTATION,
9466 .name.valid = AST_REDIRECTING_FROM_NAME_VALID,
9467
9468 .number.str = AST_REDIRECTING_FROM_NUMBER,
9469 .number.plan = AST_REDIRECTING_FROM_NUMBER_PLAN,
9470 .number.presentation = AST_REDIRECTING_FROM_NUMBER_PRESENTATION,
9471 .number.valid = AST_REDIRECTING_FROM_NUMBER_VALID,
9472
9473 .subaddress.str = AST_REDIRECTING_FROM_SUBADDRESS,
9474 .subaddress.type = AST_REDIRECTING_FROM_SUBADDRESS_TYPE,
9475 .subaddress.odd_even_indicator = AST_REDIRECTING_FROM_SUBADDRESS_ODD_EVEN,
9476 .subaddress.valid = AST_REDIRECTING_FROM_SUBADDRESS_VALID,
9477
9478 .tag = AST_REDIRECTING_FROM_TAG,
9479 .combined_presentation = AST_REDIRECTING_FROM_ID_PRESENTATION,
9480 };
9481 static const struct ast_party_id_ies to_ies = {
9482 .name.str = AST_REDIRECTING_TO_NAME,
9483 .name.char_set = AST_REDIRECTING_TO_NAME_CHAR_SET,
9484 .name.presentation = AST_REDIRECTING_TO_NAME_PRESENTATION,
9485 .name.valid = AST_REDIRECTING_TO_NAME_VALID,
9486
9487 .number.str = AST_REDIRECTING_TO_NUMBER,
9488 .number.plan = AST_REDIRECTING_TO_NUMBER_PLAN,
9489 .number.presentation = AST_REDIRECTING_TO_NUMBER_PRESENTATION,
9490 .number.valid = AST_REDIRECTING_TO_NUMBER_VALID,
9491
9492 .subaddress.str = AST_REDIRECTING_TO_SUBADDRESS,
9493 .subaddress.type = AST_REDIRECTING_TO_SUBADDRESS_TYPE,
9494 .subaddress.odd_even_indicator = AST_REDIRECTING_TO_SUBADDRESS_ODD_EVEN,
9495 .subaddress.valid = AST_REDIRECTING_TO_SUBADDRESS_VALID,
9496
9497 .tag = AST_REDIRECTING_TO_TAG,
9498 .combined_presentation = AST_REDIRECTING_TO_ID_PRESENTATION,
9499 };
9500 static const struct ast_party_id_ies priv_orig_ies = {
9501 .name.str = AST_REDIRECTING_PRIV_ORIG_NAME,
9502 .name.char_set = AST_REDIRECTING_PRIV_ORIG_NAME_CHAR_SET,
9503 .name.presentation = AST_REDIRECTING_PRIV_ORIG_NAME_PRESENTATION,
9504 .name.valid = AST_REDIRECTING_PRIV_ORIG_NAME_VALID,
9505
9506 .number.str = AST_REDIRECTING_PRIV_ORIG_NUMBER,
9507 .number.plan = AST_REDIRECTING_PRIV_ORIG_NUMBER_PLAN,
9508 .number.presentation = AST_REDIRECTING_PRIV_ORIG_NUMBER_PRESENTATION,
9509 .number.valid = AST_REDIRECTING_PRIV_ORIG_NUMBER_VALID,
9510
9511 .subaddress.str = AST_REDIRECTING_PRIV_ORIG_SUBADDRESS,
9512 .subaddress.type = AST_REDIRECTING_PRIV_ORIG_SUBADDRESS_TYPE,
9513 .subaddress.odd_even_indicator = AST_REDIRECTING_PRIV_ORIG_SUBADDRESS_ODD_EVEN,
9514 .subaddress.valid = AST_REDIRECTING_PRIV_ORIG_SUBADDRESS_VALID,
9515
9516 .tag = AST_REDIRECTING_PRIV_ORIG_TAG,
9517 .combined_presentation = 0,/* Not sent. */
9518 };
9519 static const struct ast_party_id_ies priv_from_ies = {
9520 .name.str = AST_REDIRECTING_PRIV_FROM_NAME,
9521 .name.char_set = AST_REDIRECTING_PRIV_FROM_NAME_CHAR_SET,
9522 .name.presentation = AST_REDIRECTING_PRIV_FROM_NAME_PRESENTATION,
9523 .name.valid = AST_REDIRECTING_PRIV_FROM_NAME_VALID,
9524
9525 .number.str = AST_REDIRECTING_PRIV_FROM_NUMBER,
9526 .number.plan = AST_REDIRECTING_PRIV_FROM_NUMBER_PLAN,
9527 .number.presentation = AST_REDIRECTING_PRIV_FROM_NUMBER_PRESENTATION,
9528 .number.valid = AST_REDIRECTING_PRIV_FROM_NUMBER_VALID,
9529
9530 .subaddress.str = AST_REDIRECTING_PRIV_FROM_SUBADDRESS,
9531 .subaddress.type = AST_REDIRECTING_PRIV_FROM_SUBADDRESS_TYPE,
9532 .subaddress.odd_even_indicator = AST_REDIRECTING_PRIV_FROM_SUBADDRESS_ODD_EVEN,
9533 .subaddress.valid = AST_REDIRECTING_PRIV_FROM_SUBADDRESS_VALID,
9534
9535 .tag = AST_REDIRECTING_PRIV_FROM_TAG,
9536 .combined_presentation = 0,/* Not sent. */
9537 };
9538 static const struct ast_party_id_ies priv_to_ies = {
9539 .name.str = AST_REDIRECTING_PRIV_TO_NAME,
9540 .name.char_set = AST_REDIRECTING_PRIV_TO_NAME_CHAR_SET,
9541 .name.presentation = AST_REDIRECTING_PRIV_TO_NAME_PRESENTATION,
9542 .name.valid = AST_REDIRECTING_PRIV_TO_NAME_VALID,
9543
9544 .number.str = AST_REDIRECTING_PRIV_TO_NUMBER,
9545 .number.plan = AST_REDIRECTING_PRIV_TO_NUMBER_PLAN,
9546 .number.presentation = AST_REDIRECTING_PRIV_TO_NUMBER_PRESENTATION,
9547 .number.valid = AST_REDIRECTING_PRIV_TO_NUMBER_VALID,
9548
9549 .subaddress.str = AST_REDIRECTING_PRIV_TO_SUBADDRESS,
9550 .subaddress.type = AST_REDIRECTING_PRIV_TO_SUBADDRESS_TYPE,
9551 .subaddress.odd_even_indicator = AST_REDIRECTING_PRIV_TO_SUBADDRESS_ODD_EVEN,
9552 .subaddress.valid = AST_REDIRECTING_PRIV_TO_SUBADDRESS_VALID,
9553
9554 .tag = AST_REDIRECTING_PRIV_TO_TAG,
9555 .combined_presentation = 0,/* Not sent. */
9556 };
9557 static const struct ast_party_redirecting_reason_ies reason_ies = {
9558 .code = AST_REDIRECTING_REASON_CODE,
9559 .str = AST_REDIRECTING_REASON_STR,
9560 };
9561
9562 static const struct ast_party_redirecting_reason_ies orig_reason_ies = {
9563 .code = AST_REDIRECTING_ORIG_REASON_CODE,
9564 .str = AST_REDIRECTING_ORIG_REASON_STR,
9565 };
9566
9567 /* Redirecting frame version */
9568 if (datalen < pos + (sizeof(data[0]) * 2) + 1) {
9569 ast_log(LOG_WARNING, "No space left for redirecting frame version\n");
9570 return -1;
9571 }
9572 data[pos++] = AST_REDIRECTING_VERSION;
9573 data[pos++] = 1;
9574 data[pos++] = 2;/* Version 1 did not have a version ie */
9575
9576 res = party_id_build_data(data + pos, datalen - pos, &redirecting->orig,
9577 "redirecting-orig", &orig_ies, update ? &update->orig : NULL);
9578 if (res < 0) {
9579 return -1;
9580 }
9581 pos += res;
9582
9583 res = party_id_build_data(data + pos, datalen - pos, &redirecting->from,
9584 "redirecting-from", &from_ies, update ? &update->from : NULL);
9585 if (res < 0) {
9586 return -1;
9587 }
9588 pos += res;
9589
9590 res = party_id_build_data(data + pos, datalen - pos, &redirecting->to,
9591 "redirecting-to", &to_ies, update ? &update->to : NULL);
9592 if (res < 0) {
9593 return -1;
9594 }
9595 pos += res;
9596
9597 res = party_id_build_data(data + pos, datalen - pos, &redirecting->priv_orig,
9598 "redirecting-priv-orig", &priv_orig_ies, update ? &update->priv_orig : NULL);
9599 if (res < 0) {
9600 return -1;
9601 }
9602 pos += res;
9603
9604 res = party_id_build_data(data + pos, datalen - pos, &redirecting->priv_from,
9605 "redirecting-priv-from", &priv_from_ies, update ? &update->priv_from : NULL);
9606 if (res < 0) {
9607 return -1;
9608 }
9609 pos += res;
9610
9611 res = party_id_build_data(data + pos, datalen - pos, &redirecting->priv_to,
9612 "redirecting-priv-to", &priv_to_ies, update ? &update->priv_to : NULL);
9613 if (res < 0) {
9614 return -1;
9615 }
9616 pos += res;
9617
9618 /* Redirecting reason */
9619 res = redirecting_reason_build_data(data + pos, datalen - pos, &redirecting->reason,
9620 "redirecting-reason", &reason_ies);
9621 if (res < 0) {
9622 return -1;
9623 }
9624 pos += res;
9625
9626 /* Redirecting original reason */
9627 res = redirecting_reason_build_data(data + pos, datalen - pos, &redirecting->orig_reason,
9628 "redirecting-orig-reason", &orig_reason_ies);
9629 if (res < 0) {
9630 return -1;
9631 }
9632 pos += res;
9633
9634 /* Redirecting count */
9635 if (datalen < pos + (sizeof(data[0]) * 2) + sizeof(value)) {
9636 ast_log(LOG_WARNING, "No space left for redirecting count\n");
9637 return -1;
9638 }
9639 data[pos++] = AST_REDIRECTING_COUNT;
9640 data[pos++] = sizeof(value);
9641 value = htonl(redirecting->count);
9642 memcpy(data + pos, &value, sizeof(value));
9643 pos += sizeof(value);
9644
9645 return pos;
9646 }
9647
9648 int ast_redirecting_parse_data(const unsigned char *data, size_t datalen, struct ast_party_redirecting *redirecting)
9649 {
9650 size_t pos;
9651 unsigned char ie_len;
9652 unsigned char ie_id;
9653 int32_t value;
9654 int frame_version = 1;
9655 int from_combined_presentation = 0;
9656 int got_from_combined_presentation = 0;/* TRUE if got a combined name and number presentation value. */
9657 int to_combined_presentation = 0;
9658 int got_to_combined_presentation = 0;/* TRUE if got a combined name and number presentation value. */
9659
9660 for (pos = 0; pos < datalen; pos += ie_len) {
9661 if (datalen < pos + sizeof(ie_id) + sizeof(ie_len)) {
9662 ast_log(LOG_WARNING, "Invalid redirecting update\n");
9663 return -1;
9664 }
9665 ie_id = data[pos++];
9666 ie_len = data[pos++];
9667 if (datalen < pos + ie_len) {
9668 ast_log(LOG_WARNING, "Invalid redirecting update\n");
9669 return -1;
9670 }
9671
9672 switch (ie_id) {
9673 /* Redirecting frame version */
9674 case AST_REDIRECTING_VERSION:
9675 if (ie_len != 1) {
9676 ast_log(LOG_WARNING, "Invalid redirecting frame version (%u)\n",
9677 (unsigned) ie_len);
9678 break;
9679 }
9680 frame_version = data[pos];
9681 break;
9682 /* Redirecting-orig party id name */
9683 case AST_REDIRECTING_ORIG_NAME:
9684 ast_free(redirecting->orig.name.str);
9685 redirecting->orig.name.str = ast_malloc(ie_len + 1);
9686 if (redirecting->orig.name.str) {
9687 memcpy(redirecting->orig.name.str, data + pos, ie_len);
9688 redirecting->orig.name.str[ie_len] = 0;
9689 }
9690 break;
9691 case AST_REDIRECTING_ORIG_NAME_CHAR_SET:
9692 if (ie_len != 1) {
9693 ast_log(LOG_WARNING, "Invalid redirecting-orig name char set (%u)\n",
9694 (unsigned) ie_len);
9695 break;
9696 }
9697 redirecting->orig.name.char_set = data[pos];
9698 break;
9699 case AST_REDIRECTING_ORIG_NAME_PRESENTATION:
9700 if (ie_len != 1) {
9701 ast_log(LOG_WARNING, "Invalid redirecting-orig name presentation (%u)\n",
9702 (unsigned) ie_len);
9703 break;
9704 }
9705 redirecting->orig.name.presentation = data[pos];
9706 break;
9707 case AST_REDIRECTING_ORIG_NAME_VALID:
9708 if (ie_len != 1) {
9709 ast_log(LOG_WARNING, "Invalid redirecting-orig name valid (%u)\n",
9710 (unsigned) ie_len);
9711 break;
9712 }
9713 redirecting->orig.name.valid = data[pos];
9714 break;
9715 /* Redirecting-orig party id number */
9716 case AST_REDIRECTING_ORIG_NUMBER:
9717 ast_free(redirecting->orig.number.str);
9718 redirecting->orig.number.str = ast_malloc(ie_len + 1);
9719 if (redirecting->orig.number.str) {
9720 memcpy(redirecting->orig.number.str, data + pos, ie_len);
9721 redirecting->orig.number.str[ie_len] = 0;
9722 }
9723 break;
9724 case AST_REDIRECTING_ORIG_NUMBER_PLAN:
9725 if (ie_len != 1) {
9726 ast_log(LOG_WARNING, "Invalid redirecting-orig numbering plan (%u)\n",
9727 (unsigned) ie_len);
9728 break;
9729 }
9730 redirecting->orig.number.plan = data[pos];
9731 break;
9732 case AST_REDIRECTING_ORIG_NUMBER_PRESENTATION:
9733 if (ie_len != 1) {
9734 ast_log(LOG_WARNING, "Invalid redirecting-orig number presentation (%u)\n",
9735 (unsigned) ie_len);
9736 break;
9737 }
9738 redirecting->orig.number.presentation = data[pos];
9739 break;
9740 case AST_REDIRECTING_ORIG_NUMBER_VALID:
9741 if (ie_len != 1) {
9742 ast_log(LOG_WARNING, "Invalid redirecting-orig number valid (%u)\n",
9743 (unsigned) ie_len);
9744 break;
9745 }
9746 redirecting->orig.number.valid = data[pos];
9747 break;
9748 /* Redirecting-orig party id subaddress */
9749 case AST_REDIRECTING_ORIG_SUBADDRESS:
9750 ast_free(redirecting->orig.subaddress.str);
9751 redirecting->orig.subaddress.str = ast_malloc(ie_len + 1);
9752 if (redirecting->orig.subaddress.str) {
9753 memcpy(redirecting->orig.subaddress.str, data + pos, ie_len);
9754 redirecting->orig.subaddress.str[ie_len] = 0;
9755 }
9756 break;
9757 case AST_REDIRECTING_ORIG_SUBADDRESS_TYPE:
9758 if (ie_len != 1) {
9759 ast_log(LOG_WARNING, "Invalid redirecting-orig type of subaddress (%u)\n",
9760 (unsigned) ie_len);
9761 break;
9762 }
9763 redirecting->orig.subaddress.type = data[pos];
9764 break;
9765 case AST_REDIRECTING_ORIG_SUBADDRESS_ODD_EVEN:
9766 if (ie_len != 1) {
9767 ast_log(LOG_WARNING,
9768 "Invalid redirecting-orig subaddress odd-even indicator (%u)\n",
9769 (unsigned) ie_len);
9770 break;
9771 }
9772 redirecting->orig.subaddress.odd_even_indicator = data[pos];
9773 break;
9774 case AST_REDIRECTING_ORIG_SUBADDRESS_VALID:
9775 if (ie_len != 1) {
9776 ast_log(LOG_WARNING, "Invalid redirecting-orig subaddress valid (%u)\n",
9777 (unsigned) ie_len);
9778 break;
9779 }
9780 redirecting->orig.subaddress.valid = data[pos];
9781 break;
9782 /* Redirecting-orig party id tag */
9783 case AST_REDIRECTING_ORIG_TAG:
9784 ast_free(redirecting->orig.tag);
9785 redirecting->orig.tag = ast_malloc(ie_len + 1);
9786 if (redirecting->orig.tag) {
9787 memcpy(redirecting->orig.tag, data + pos, ie_len);
9788 redirecting->orig.tag[ie_len] = 0;
9789 }
9790 break;
9791 /* Redirecting-from party id name */
9792 case AST_REDIRECTING_FROM_NAME:
9793 ast_free(redirecting->from.name.str);
9794 redirecting->from.name.str = ast_malloc(ie_len + 1);
9795 if (redirecting->from.name.str) {
9796 memcpy(redirecting->from.name.str, data + pos, ie_len);
9797 redirecting->from.name.str[ie_len] = 0;
9798 }
9799 break;
9800 case AST_REDIRECTING_FROM_NAME_CHAR_SET:
9801 if (ie_len != 1) {
9802 ast_log(LOG_WARNING, "Invalid redirecting-from name char set (%u)\n",
9803 (unsigned) ie_len);
9804 break;
9805 }
9806 redirecting->from.name.char_set = data[pos];
9807 break;
9808 case AST_REDIRECTING_FROM_NAME_PRESENTATION:
9809 if (ie_len != 1) {
9810 ast_log(LOG_WARNING, "Invalid redirecting-from name presentation (%u)\n",
9811 (unsigned) ie_len);
9812 break;
9813 }
9814 redirecting->from.name.presentation = data[pos];
9815 break;
9816 case AST_REDIRECTING_FROM_NAME_VALID:
9817 if (ie_len != 1) {
9818 ast_log(LOG_WARNING, "Invalid redirecting-from name valid (%u)\n",
9819 (unsigned) ie_len);
9820 break;
9821 }
9822 redirecting->from.name.valid = data[pos];
9823 break;
9824 /* Redirecting-from party id number */
9825 case AST_REDIRECTING_FROM_NUMBER:
9826 ast_free(redirecting->from.number.str);
9827 redirecting->from.number.str = ast_malloc(ie_len + 1);
9828 if (redirecting->from.number.str) {
9829 memcpy(redirecting->from.number.str, data + pos, ie_len);
9830 redirecting->from.number.str[ie_len] = 0;
9831 }
9832 break;
9833 case AST_REDIRECTING_FROM_NUMBER_PLAN:
9834 if (ie_len != 1) {
9835 ast_log(LOG_WARNING, "Invalid redirecting-from numbering plan (%u)\n",
9836 (unsigned) ie_len);
9837 break;
9838 }
9839 redirecting->from.number.plan = data[pos];
9840 break;
9841 case AST_REDIRECTING_FROM_NUMBER_PRESENTATION:
9842 if (ie_len != 1) {
9843 ast_log(LOG_WARNING, "Invalid redirecting-from number presentation (%u)\n",
9844 (unsigned) ie_len);
9845 break;
9846 }
9847 redirecting->from.number.presentation = data[pos];
9848 break;
9849 case AST_REDIRECTING_FROM_NUMBER_VALID:
9850 if (ie_len != 1) {
9851 ast_log(LOG_WARNING, "Invalid redirecting-from number valid (%u)\n",
9852 (unsigned) ie_len);
9853 break;
9854 }
9855 redirecting->from.number.valid = data[pos];
9856 break;
9857 /* Redirecting-from party id combined presentation */
9858 case AST_REDIRECTING_FROM_ID_PRESENTATION:
9859 if (ie_len != 1) {
9860 ast_log(LOG_WARNING, "Invalid redirecting-from combined presentation (%u)\n",
9861 (unsigned) ie_len);
9862 break;
9863 }
9864 from_combined_presentation = data[pos];
9865 got_from_combined_presentation = 1;
9866 break;
9867 /* Redirecting-from party id subaddress */
9868 case AST_REDIRECTING_FROM_SUBADDRESS:
9869 ast_free(redirecting->from.subaddress.str);
9870 redirecting->from.subaddress.str = ast_malloc(ie_len + 1);
9871 if (redirecting->from.subaddress.str) {
9872 memcpy(redirecting->from.subaddress.str, data + pos, ie_len);
9873 redirecting->from.subaddress.str[ie_len] = 0;
9874 }
9875 break;
9876 case AST_REDIRECTING_FROM_SUBADDRESS_TYPE:
9877 if (ie_len != 1) {
9878 ast_log(LOG_WARNING, "Invalid redirecting-from type of subaddress (%u)\n",
9879 (unsigned) ie_len);
9880 break;
9881 }
9882 redirecting->from.subaddress.type = data[pos];
9883 break;
9884 case AST_REDIRECTING_FROM_SUBADDRESS_ODD_EVEN:
9885 if (ie_len != 1) {
9886 ast_log(LOG_WARNING,
9887 "Invalid redirecting-from subaddress odd-even indicator (%u)\n",
9888 (unsigned) ie_len);
9889 break;
9890 }
9891 redirecting->from.subaddress.odd_even_indicator = data[pos];
9892 break;
9893 case AST_REDIRECTING_FROM_SUBADDRESS_VALID:
9894 if (ie_len != 1) {
9895 ast_log(LOG_WARNING, "Invalid redirecting-from subaddress valid (%u)\n",
9896 (unsigned) ie_len);
9897 break;
9898 }
9899 redirecting->from.subaddress.valid = data[pos];
9900 break;
9901 /* Redirecting-from party id tag */
9902 case AST_REDIRECTING_FROM_TAG:
9903 ast_free(redirecting->from.tag);
9904 redirecting->from.tag = ast_malloc(ie_len + 1);
9905 if (redirecting->from.tag) {
9906 memcpy(redirecting->from.tag, data + pos, ie_len);
9907 redirecting->from.tag[ie_len] = 0;
9908 }
9909 break;
9910 /* Redirecting-to party id name */
9911 case AST_REDIRECTING_TO_NAME:
9912 ast_free(redirecting->to.name.str);
9913 redirecting->to.name.str = ast_malloc(ie_len + 1);
9914 if (redirecting->to.name.str) {
9915 memcpy(redirecting->to.name.str, data + pos, ie_len);
9916 redirecting->to.name.str[ie_len] = 0;
9917 }
9918 break;
9919 case AST_REDIRECTING_TO_NAME_CHAR_SET:
9920 if (ie_len != 1) {
9921 ast_log(LOG_WARNING, "Invalid redirecting-to name char set (%u)\n",
9922 (unsigned) ie_len);
9923 break;
9924 }
9925 redirecting->to.name.char_set = data[pos];
9926 break;
9927 case AST_REDIRECTING_TO_NAME_PRESENTATION:
9928 if (ie_len != 1) {
9929 ast_log(LOG_WARNING, "Invalid redirecting-to name presentation (%u)\n",
9930 (unsigned) ie_len);
9931 break;
9932 }
9933 redirecting->to.name.presentation = data[pos];
9934 break;
9935 case AST_REDIRECTING_TO_NAME_VALID:
9936 if (ie_len != 1) {
9937 ast_log(LOG_WARNING, "Invalid redirecting-to name valid (%u)\n",
9938 (unsigned) ie_len);
9939 break;
9940 }
9941 redirecting->to.name.valid = data[pos];
9942 break;
9943 /* Redirecting-to party id number */
9944 case AST_REDIRECTING_TO_NUMBER:
9945 ast_free(redirecting->to.number.str);
9946 redirecting->to.number.str = ast_malloc(ie_len + 1);
9947 if (redirecting->to.number.str) {
9948 memcpy(redirecting->to.number.str, data + pos, ie_len);
9949 redirecting->to.number.str[ie_len] = 0;
9950 }
9951 break;
9952 case AST_REDIRECTING_TO_NUMBER_PLAN:
9953 if (ie_len != 1) {
9954 ast_log(LOG_WARNING, "Invalid redirecting-to numbering plan (%u)\n",
9955 (unsigned) ie_len);
9956 break;
9957 }
9958 redirecting->to.number.plan = data[pos];
9959 break;
9960 case AST_REDIRECTING_TO_NUMBER_PRESENTATION:
9961 if (ie_len != 1) {
9962 ast_log(LOG_WARNING, "Invalid redirecting-to number presentation (%u)\n",
9963 (unsigned) ie_len);
9964 break;
9965 }
9966 redirecting->to.number.presentation = data[pos];
9967 break;
9968 case AST_REDIRECTING_TO_NUMBER_VALID:
9969 if (ie_len != 1) {
9970 ast_log(LOG_WARNING, "Invalid redirecting-to number valid (%u)\n",
9971 (unsigned) ie_len);
9972 break;
9973 }
9974 redirecting->to.number.valid = data[pos];
9975 break;
9976 /* Redirecting-to party id combined presentation */
9977 case AST_REDIRECTING_TO_ID_PRESENTATION:
9978 if (ie_len != 1) {
9979 ast_log(LOG_WARNING, "Invalid redirecting-to combined presentation (%u)\n",
9980 (unsigned) ie_len);
9981 break;
9982 }
9983 to_combined_presentation = data[pos];
9984 got_to_combined_presentation = 1;
9985 break;
9986 /* Redirecting-to party id subaddress */
9987 case AST_REDIRECTING_TO_SUBADDRESS:
9988 ast_free(redirecting->to.subaddress.str);
9989 redirecting->to.subaddress.str = ast_malloc(ie_len + 1);
9990 if (redirecting->to.subaddress.str) {
9991 memcpy(redirecting->to.subaddress.str, data + pos, ie_len);
9992 redirecting->to.subaddress.str[ie_len] = 0;
9993 }
9994 break;
9995 case AST_REDIRECTING_TO_SUBADDRESS_TYPE:
9996 if (ie_len != 1) {
9997 ast_log(LOG_WARNING, "Invalid redirecting-to type of subaddress (%u)\n",
9998 (unsigned) ie_len);
9999 break;
10000 }
10001 redirecting->to.subaddress.type = data[pos];
10002 break;
10003 case AST_REDIRECTING_TO_SUBADDRESS_ODD_EVEN:
10004 if (ie_len != 1) {
10005 ast_log(LOG_WARNING,
10006 "Invalid redirecting-to subaddress odd-even indicator (%u)\n",
10007 (unsigned) ie_len);
10008 break;
10009 }
10010 redirecting->to.subaddress.odd_even_indicator = data[pos];
10011 break;
10012 case AST_REDIRECTING_TO_SUBADDRESS_VALID:
10013 if (ie_len != 1) {
10014 ast_log(LOG_WARNING, "Invalid redirecting-to subaddress valid (%u)\n",
10015 (unsigned) ie_len);
10016 break;
10017 }
10018 redirecting->to.subaddress.valid = data[pos];
10019 break;
10020 /* Redirecting-to party id tag */
10021 case AST_REDIRECTING_TO_TAG:
10022 ast_free(redirecting->to.tag);
10023 redirecting->to.tag = ast_malloc(ie_len + 1);
10024 if (redirecting->to.tag) {
10025 memcpy(redirecting->to.tag, data + pos, ie_len);
10026 redirecting->to.tag[ie_len] = 0;
10027 }
10028 break;
10029 /* Private redirecting-orig party id name */
10030 case AST_REDIRECTING_PRIV_ORIG_NAME:
10031 ast_free(redirecting->priv_orig.name.str);
10032 redirecting->priv_orig.name.str = ast_malloc(ie_len + 1);
10033 if (redirecting->priv_orig.name.str) {
10034 memcpy(redirecting->priv_orig.name.str, data + pos, ie_len);
10035 redirecting->priv_orig.name.str[ie_len] = 0;
10036 }
10037 break;
10038 case AST_REDIRECTING_PRIV_ORIG_NAME_CHAR_SET:
10039 if (ie_len != 1) {
10040 ast_log(LOG_WARNING, "Invalid private redirecting-orig name char set (%u)\n",
10041 (unsigned) ie_len);
10042 break;
10043 }
10044 redirecting->priv_orig.name.char_set = data[pos];
10045 break;
10046 case AST_REDIRECTING_PRIV_ORIG_NAME_PRESENTATION:
10047 if (ie_len != 1) {
10048 ast_log(LOG_WARNING, "Invalid private redirecting-orig name presentation (%u)\n",
10049 (unsigned) ie_len);
10050 break;
10051 }
10052 redirecting->priv_orig.name.presentation = data[pos];
10053 break;
10054 case AST_REDIRECTING_PRIV_ORIG_NAME_VALID:
10055 if (ie_len != 1) {
10056 ast_log(LOG_WARNING, "Invalid private redirecting-orig name valid (%u)\n",
10057 (unsigned) ie_len);
10058 break;
10059 }
10060 redirecting->priv_orig.name.valid = data[pos];
10061 break;
10062 /* Private redirecting-orig party id number */
10063 case AST_REDIRECTING_PRIV_ORIG_NUMBER:
10064 ast_free(redirecting->priv_orig.number.str);
10065 redirecting->priv_orig.number.str = ast_malloc(ie_len + 1);
10066 if (redirecting->priv_orig.number.str) {
10067 memcpy(redirecting->priv_orig.number.str, data + pos, ie_len);
10068 redirecting->priv_orig.number.str[ie_len] = 0;
10069 }
10070 break;
10071 case AST_REDIRECTING_PRIV_ORIG_NUMBER_PLAN:
10072 if (ie_len != 1) {
10073 ast_log(LOG_WARNING, "Invalid private redirecting-orig numbering plan (%u)\n",
10074 (unsigned) ie_len);
10075 break;
10076 }
10077 redirecting->priv_orig.number.plan = data[pos];
10078 break;
10079 case AST_REDIRECTING_PRIV_ORIG_NUMBER_PRESENTATION:
10080 if (ie_len != 1) {
10081 ast_log(LOG_WARNING, "Invalid private redirecting-orig number presentation (%u)\n",
10082 (unsigned) ie_len);
10083 break;
10084 }
10085 redirecting->priv_orig.number.presentation = data[pos];
10086 break;
10087 case AST_REDIRECTING_PRIV_ORIG_NUMBER_VALID:
10088 if (ie_len != 1) {
10089 ast_log(LOG_WARNING, "Invalid private redirecting-orig number valid (%u)\n",
10090 (unsigned) ie_len);
10091 break;
10092 }
10093 redirecting->priv_orig.number.valid = data[pos];
10094 break;
10095 /* Private redirecting-orig party id subaddress */
10096 case AST_REDIRECTING_PRIV_ORIG_SUBADDRESS:
10097 ast_free(redirecting->priv_orig.subaddress.str);
10098 redirecting->priv_orig.subaddress.str = ast_malloc(ie_len + 1);
10099 if (redirecting->priv_orig.subaddress.str) {
10100 memcpy(redirecting->priv_orig.subaddress.str, data + pos, ie_len);
10101 redirecting->priv_orig.subaddress.str[ie_len] = 0;
10102 }
10103 break;
10104 case AST_REDIRECTING_PRIV_ORIG_SUBADDRESS_TYPE:
10105 if (ie_len != 1) {
10106 ast_log(LOG_WARNING, "Invalid private redirecting-orig type of subaddress (%u)\n",
10107 (unsigned) ie_len);
10108 break;
10109 }
10110 redirecting->priv_orig.subaddress.type = data[pos];
10111 break;
10112 case AST_REDIRECTING_PRIV_ORIG_SUBADDRESS_ODD_EVEN:
10113 if (ie_len != 1) {
10114 ast_log(LOG_WARNING,
10115 "Invalid private redirecting-orig subaddress odd-even indicator (%u)\n",
10116 (unsigned) ie_len);
10117 break;
10118 }
10119 redirecting->priv_orig.subaddress.odd_even_indicator = data[pos];
10120 break;
10121 case AST_REDIRECTING_PRIV_ORIG_SUBADDRESS_VALID:
10122 if (ie_len != 1) {
10123 ast_log(LOG_WARNING, "Invalid private redirecting-orig subaddress valid (%u)\n",
10124 (unsigned) ie_len);
10125 break;
10126 }
10127 redirecting->priv_orig.subaddress.valid = data[pos];
10128 break;
10129 /* Private redirecting-orig party id tag */
10130 case AST_REDIRECTING_PRIV_ORIG_TAG:
10131 ast_free(redirecting->priv_orig.tag);
10132 redirecting->priv_orig.tag = ast_malloc(ie_len + 1);
10133 if (redirecting->priv_orig.tag) {
10134 memcpy(redirecting->priv_orig.tag, data + pos, ie_len);
10135 redirecting->priv_orig.tag[ie_len] = 0;
10136 }
10137 break;
10138 /* Private redirecting-from party id name */
10139 case AST_REDIRECTING_PRIV_FROM_NAME:
10140 ast_free(redirecting->priv_from.name.str);
10141 redirecting->priv_from.name.str = ast_malloc(ie_len + 1);
10142 if (redirecting->priv_from.name.str) {
10143 memcpy(redirecting->priv_from.name.str, data + pos, ie_len);
10144 redirecting->priv_from.name.str[ie_len] = 0;
10145 }
10146 break;
10147 case AST_REDIRECTING_PRIV_FROM_NAME_CHAR_SET:
10148 if (ie_len != 1) {
10149 ast_log(LOG_WARNING, "Invalid private redirecting-from name char set (%u)\n",
10150 (unsigned) ie_len);
10151 break;
10152 }
10153 redirecting->priv_from.name.char_set = data[pos];
10154 break;
10155 case AST_REDIRECTING_PRIV_FROM_NAME_PRESENTATION:
10156 if (ie_len != 1) {
10157 ast_log(LOG_WARNING, "Invalid private redirecting-from name presentation (%u)\n",
10158 (unsigned) ie_len);
10159 break;
10160 }
10161 redirecting->priv_from.name.presentation = data[pos];
10162 break;
10163 case AST_REDIRECTING_PRIV_FROM_NAME_VALID:
10164 if (ie_len != 1) {
10165 ast_log(LOG_WARNING, "Invalid private redirecting-from name valid (%u)\n",
10166 (unsigned) ie_len);
10167 break;
10168 }
10169 redirecting->priv_from.name.valid = data[pos];
10170 break;
10171 /* Private redirecting-from party id number */
10172 case AST_REDIRECTING_PRIV_FROM_NUMBER:
10173 ast_free(redirecting->priv_from.number.str);
10174 redirecting->priv_from.number.str = ast_malloc(ie_len + 1);
10175 if (redirecting->priv_from.number.str) {
10176 memcpy(redirecting->priv_from.number.str, data + pos, ie_len);
10177 redirecting->priv_from.number.str[ie_len] = 0;
10178 }
10179 break;
10180 case AST_REDIRECTING_PRIV_FROM_NUMBER_PLAN:
10181 if (ie_len != 1) {
10182 ast_log(LOG_WARNING, "Invalid private redirecting-from numbering plan (%u)\n",
10183 (unsigned) ie_len);
10184 break;
10185 }
10186 redirecting->priv_from.number.plan = data[pos];
10187 break;
10188 case AST_REDIRECTING_PRIV_FROM_NUMBER_PRESENTATION:
10189 if (ie_len != 1) {
10190 ast_log(LOG_WARNING, "Invalid private redirecting-from number presentation (%u)\n",
10191 (unsigned) ie_len);
10192 break;
10193 }
10194 redirecting->priv_from.number.presentation = data[pos];
10195 break;
10196 case AST_REDIRECTING_PRIV_FROM_NUMBER_VALID:
10197 if (ie_len != 1) {
10198 ast_log(LOG_WARNING, "Invalid private redirecting-from number valid (%u)\n",
10199 (unsigned) ie_len);
10200 break;
10201 }
10202 redirecting->priv_from.number.valid = data[pos];
10203 break;
10204 /* Private redirecting-from party id subaddress */
10205 case AST_REDIRECTING_PRIV_FROM_SUBADDRESS:
10206 ast_free(redirecting->priv_from.subaddress.str);
10207 redirecting->priv_from.subaddress.str = ast_malloc(ie_len + 1);
10208 if (redirecting->priv_from.subaddress.str) {
10209 memcpy(redirecting->priv_from.subaddress.str, data + pos, ie_len);
10210 redirecting->priv_from.subaddress.str[ie_len] = 0;
10211 }
10212 break;
10213 case AST_REDIRECTING_PRIV_FROM_SUBADDRESS_TYPE:
10214 if (ie_len != 1) {
10215 ast_log(LOG_WARNING, "Invalid private redirecting-from type of subaddress (%u)\n",
10216 (unsigned) ie_len);
10217 break;
10218 }
10219 redirecting->priv_from.subaddress.type = data[pos];
10220 break;
10221 case AST_REDIRECTING_PRIV_FROM_SUBADDRESS_ODD_EVEN:
10222 if (ie_len != 1) {
10223 ast_log(LOG_WARNING,
10224 "Invalid private redirecting-from subaddress odd-even indicator (%u)\n",
10225 (unsigned) ie_len);
10226 break;
10227 }
10228 redirecting->priv_from.subaddress.odd_even_indicator = data[pos];
10229 break;
10230 case AST_REDIRECTING_PRIV_FROM_SUBADDRESS_VALID:
10231 if (ie_len != 1) {
10232 ast_log(LOG_WARNING, "Invalid private redirecting-from subaddress valid (%u)\n",
10233 (unsigned) ie_len);
10234 break;
10235 }
10236 redirecting->priv_from.subaddress.valid = data[pos];
10237 break;
10238 /* Private redirecting-from party id tag */
10239 case AST_REDIRECTING_PRIV_FROM_TAG:
10240 ast_free(redirecting->priv_from.tag);
10241 redirecting->priv_from.tag = ast_malloc(ie_len + 1);
10242 if (redirecting->priv_from.tag) {
10243 memcpy(redirecting->priv_from.tag, data + pos, ie_len);
10244 redirecting->priv_from.tag[ie_len] = 0;
10245 }
10246 break;
10247 /* Private redirecting-to party id name */
10248 case AST_REDIRECTING_PRIV_TO_NAME:
10249 ast_free(redirecting->priv_to.name.str);
10250 redirecting->priv_to.name.str = ast_malloc(ie_len + 1);
10251 if (redirecting->priv_to.name.str) {
10252 memcpy(redirecting->priv_to.name.str, data + pos, ie_len);
10253 redirecting->priv_to.name.str[ie_len] = 0;
10254 }
10255 break;
10256 case AST_REDIRECTING_PRIV_TO_NAME_CHAR_SET:
10257 if (ie_len != 1) {
10258 ast_log(LOG_WARNING, "Invalid private redirecting-to name char set (%u)\n",
10259 (unsigned) ie_len);
10260 break;
10261 }
10262 redirecting->priv_to.name.char_set = data[pos];
10263 break;
10264 case AST_REDIRECTING_PRIV_TO_NAME_PRESENTATION:
10265 if (ie_len != 1) {
10266 ast_log(LOG_WARNING, "Invalid private redirecting-to name presentation (%u)\n",
10267 (unsigned) ie_len);
10268 break;
10269 }
10270 redirecting->priv_to.name.presentation = data[pos];
10271 break;
10272 case AST_REDIRECTING_PRIV_TO_NAME_VALID:
10273 if (ie_len != 1) {
10274 ast_log(LOG_WARNING, "Invalid private redirecting-to name valid (%u)\n",
10275 (unsigned) ie_len);
10276 break;
10277 }
10278 redirecting->priv_to.name.valid = data[pos];
10279 break;
10280 /* Private redirecting-to party id number */
10281 case AST_REDIRECTING_PRIV_TO_NUMBER:
10282 ast_free(redirecting->priv_to.number.str);
10283 redirecting->priv_to.number.str = ast_malloc(ie_len + 1);
10284 if (redirecting->priv_to.number.str) {
10285 memcpy(redirecting->priv_to.number.str, data + pos, ie_len);
10286 redirecting->priv_to.number.str[ie_len] = 0;
10287 }
10288 break;
10289 case AST_REDIRECTING_PRIV_TO_NUMBER_PLAN:
10290 if (ie_len != 1) {
10291 ast_log(LOG_WARNING, "Invalid private redirecting-to numbering plan (%u)\n",
10292 (unsigned) ie_len);
10293 break;
10294 }
10295 redirecting->priv_to.number.plan = data[pos];
10296 break;
10297 case AST_REDIRECTING_PRIV_TO_NUMBER_PRESENTATION:
10298 if (ie_len != 1) {
10299 ast_log(LOG_WARNING, "Invalid private redirecting-to number presentation (%u)\n",
10300 (unsigned) ie_len);
10301 break;
10302 }
10303 redirecting->priv_to.number.presentation = data[pos];
10304 break;
10305 case AST_REDIRECTING_PRIV_TO_NUMBER_VALID:
10306 if (ie_len != 1) {
10307 ast_log(LOG_WARNING, "Invalid private redirecting-to number valid (%u)\n",
10308 (unsigned) ie_len);
10309 break;
10310 }
10311 redirecting->priv_to.number.valid = data[pos];
10312 break;
10313 /* Private redirecting-to party id subaddress */
10314 case AST_REDIRECTING_PRIV_TO_SUBADDRESS:
10315 ast_free(redirecting->priv_to.subaddress.str);
10316 redirecting->priv_to.subaddress.str = ast_malloc(ie_len + 1);
10317 if (redirecting->priv_to.subaddress.str) {
10318 memcpy(redirecting->priv_to.subaddress.str, data + pos, ie_len);
10319 redirecting->priv_to.subaddress.str[ie_len] = 0;
10320 }
10321 break;
10322 case AST_REDIRECTING_PRIV_TO_SUBADDRESS_TYPE:
10323 if (ie_len != 1) {
10324 ast_log(LOG_WARNING, "Invalid private redirecting-to type of subaddress (%u)\n",
10325 (unsigned) ie_len);
10326 break;
10327 }
10328 redirecting->priv_to.subaddress.type = data[pos];
10329 break;
10330 case AST_REDIRECTING_PRIV_TO_SUBADDRESS_ODD_EVEN:
10331 if (ie_len != 1) {
10332 ast_log(LOG_WARNING,
10333 "Invalid private redirecting-to subaddress odd-even indicator (%u)\n",
10334 (unsigned) ie_len);
10335 break;
10336 }
10337 redirecting->priv_to.subaddress.odd_even_indicator = data[pos];
10338 break;
10339 case AST_REDIRECTING_PRIV_TO_SUBADDRESS_VALID:
10340 if (ie_len != 1) {
10341 ast_log(LOG_WARNING, "Invalid private redirecting-to subaddress valid (%u)\n",
10342 (unsigned) ie_len);
10343 break;
10344 }
10345 redirecting->priv_to.subaddress.valid = data[pos];
10346 break;
10347 /* Private redirecting-to party id tag */
10348 case AST_REDIRECTING_PRIV_TO_TAG:
10349 ast_free(redirecting->priv_to.tag);
10350 redirecting->priv_to.tag = ast_malloc(ie_len + 1);
10351 if (redirecting->priv_to.tag) {
10352 memcpy(redirecting->priv_to.tag, data + pos, ie_len);
10353 redirecting->priv_to.tag[ie_len] = 0;
10354 }
10355 break;
10356 /* Redirecting reason code */
10357 case AST_REDIRECTING_REASON_CODE:
10358 if (ie_len != sizeof(value)) {
10359 ast_log(LOG_WARNING, "Invalid redirecting reason (%u)\n",
10360 (unsigned) ie_len);
10361 break;
10362 }
10363 memcpy(&value, data + pos, sizeof(value));
10364 redirecting->reason.code = ntohl(value);
10365 break;
10366 /* Redirecting reason string */
10367 case AST_REDIRECTING_REASON_STR:
10368 ast_free(redirecting->reason.str);
10369 redirecting->reason.str = ast_malloc(ie_len + 1);
10370 if (redirecting->reason.str) {
10371 memcpy(redirecting->reason.str, data + pos, ie_len);
10372 redirecting->reason.str[ie_len] = 0;
10373 }
10374 break;
10375 /* Redirecting orig-reason code */
10376 case AST_REDIRECTING_ORIG_REASON_CODE:
10377 if (ie_len != sizeof(value)) {
10378 ast_log(LOG_WARNING, "Invalid redirecting original reason (%u)\n",
10379 (unsigned) ie_len);
10380 break;
10381 }
10382 memcpy(&value, data + pos, sizeof(value));
10383 redirecting->orig_reason.code = ntohl(value);
10384 break;
10385 /* Redirecting orig-reason string */
10386 case AST_REDIRECTING_ORIG_REASON_STR:
10387 ast_free(redirecting->orig_reason.str);
10388 redirecting->orig_reason.str = ast_malloc(ie_len + 1);
10389 if (redirecting->orig_reason.str) {
10390 memcpy(redirecting->orig_reason.str, data + pos, ie_len);
10391 redirecting->orig_reason.str[ie_len] = 0;
10392 }
10393 break;
10394 /* Redirecting count */
10395 case AST_REDIRECTING_COUNT:
10396 if (ie_len != sizeof(value)) {
10397 ast_log(LOG_WARNING, "Invalid redirecting count (%u)\n",
10398 (unsigned) ie_len);
10399 break;
10400 }
10401 memcpy(&value, data + pos, sizeof(value));
10402 redirecting->count = ntohl(value);
10403 break;
10404 /* Redirecting unknown element */
10405 default:
10406 ast_debug(1, "Unknown redirecting element: %u (%u)\n",
10407 (unsigned) ie_id, (unsigned) ie_len);
10408 break;
10409 }
10410 }
10411
10412 switch (frame_version) {
10413 case 1:
10414 /*
10415 * The other end is an earlier version that we need to adjust
10416 * for compatibility.
10417 *
10418 * The earlier version did not have the orig party id or
10419 * orig_reason value.
10420 */
10421 redirecting->from.name.valid = 1;
10422 redirecting->from.name.char_set = AST_PARTY_CHAR_SET_ISO8859_1;
10423 redirecting->from.number.valid = 1;
10424 if (got_from_combined_presentation) {
10425 redirecting->from.name.presentation = from_combined_presentation;
10426 redirecting->from.number.presentation = from_combined_presentation;
10427 }
10428
10429 redirecting->to.name.valid = 1;
10430 redirecting->to.name.char_set = AST_PARTY_CHAR_SET_ISO8859_1;
10431 redirecting->to.number.valid = 1;
10432 if (got_to_combined_presentation) {
10433 redirecting->to.name.presentation = to_combined_presentation;
10434 redirecting->to.number.presentation = to_combined_presentation;
10435 }
10436 break;
10437 case 2:
10438 /* The other end is at the same level as we are. */
10439 break;
10440 default:
10441 /*
10442 * The other end is newer than we are.
10443 * We need to assume that they are compatible with us.
10444 */
10445 ast_debug(1, "Redirecting frame has newer version: %u\n",
10446 (unsigned) frame_version);
10447 break;
10448 }
10449
10450 return 0;
10451 }
10452
10453 void ast_channel_update_redirecting(struct ast_channel *chan, const struct ast_party_redirecting *redirecting, const struct ast_set_party_redirecting *update)
10454 {
10455 unsigned char data[1024]; /* This should be large enough */
10456 size_t datalen;
10457
10458 datalen = ast_redirecting_build_data(data, sizeof(data), redirecting, update);
10459 if (datalen == (size_t) -1) {
10460 return;
10461 }
10462
10463 ast_indicate_data(chan, AST_CONTROL_REDIRECTING, data, datalen);
10464 }
10465
10466 void ast_channel_queue_redirecting_update(struct ast_channel *chan, const struct ast_party_redirecting *redirecting, const struct ast_set_party_redirecting *update)
10467 {
10468 unsigned char data[1024]; /* This should be large enough */
10469 size_t datalen;
10470
10471 datalen = ast_redirecting_build_data(data, sizeof(data), redirecting, update);
10472 if (datalen == (size_t) -1) {
10473 return;
10474 }
10475
10476 ast_queue_control_data(chan, AST_CONTROL_REDIRECTING, data, datalen);
10477 }
10478
10479 /*!
10480 * Storage to determine if the current thread is running an intercept dialplan routine.
10481 */
10482 AST_THREADSTORAGE_RAW(in_intercept_routine);
10483
10484 /*!
10485 * \internal
10486 * \brief Set the current intercept dialplan routine status mode.
10487 * \since 13.14.0
10488 *
10489 * \param in_intercept_mode New intercept mode. (Non-zero if in intercept mode)
10490 */
10491 static void channel_set_intercept_mode(int in_intercept_mode)
10492 {
10493 int status;
10494
10495 status = ast_threadstorage_set_ptr(&in_intercept_routine,
10496 in_intercept_mode ? &(int) { 1 } : NULL);
10497 if (status) {
10498 ast_log(LOG_ERROR, "Failed to set dialplan intercept mode\n");
10499 }
10500 }
10501
10502 int ast_channel_get_intercept_mode(void)
10503 {
10504 return ast_threadstorage_get_ptr(&in_intercept_routine) ? 1 : 0;
10505 }
10506
10507 int ast_channel_connected_line_sub(struct ast_channel *autoservice_chan, struct ast_channel *sub_chan, const void *connected_info, int is_frame)
10508 {
10509 const char *sub;
10510 const char *sub_args;
10511 int retval;
10512
10513 ast_channel_lock(sub_chan);
10514 sub = pbx_builtin_getvar_helper(sub_chan, "CONNECTED_LINE_SEND_SUB");
10515 sub = ast_strdupa(S_OR(sub, ""));
10516 sub_args = pbx_builtin_getvar_helper(sub_chan, "CONNECTED_LINE_SEND_SUB_ARGS");
10517 sub_args = ast_strdupa(S_OR(sub_args, ""));
10518
10519 if (ast_strlen_zero(sub)) {
10520 ast_channel_unlock(sub_chan);
10521 return -1;
10522 }
10523
10524 if (is_frame) {
10525 const struct ast_frame *frame = connected_info;
10526
10527 ast_connected_line_parse_data(frame->data.ptr, frame->datalen, ast_channel_connected(sub_chan));
10528 } else {
10529 const struct ast_party_connected_line *connected = connected_info;
10530
10531 ast_party_connected_line_copy(ast_channel_connected(sub_chan), connected);
10532 }
10533 ast_channel_unlock(sub_chan);
10534
10535 channel_set_intercept_mode(1);
10536 retval = ast_app_run_sub(autoservice_chan, sub_chan, sub, sub_args, 0);
10537 channel_set_intercept_mode(0);
10538 if (!retval) {
10539 struct ast_party_connected_line saved_connected;
10540
10541 ast_party_connected_line_init(&saved_connected);
10542 ast_channel_lock(sub_chan);
10543 ast_party_connected_line_copy(&saved_connected, ast_channel_connected(sub_chan));
10544 ast_channel_unlock(sub_chan);
10545 ast_channel_update_connected_line(sub_chan, &saved_connected, NULL);
10546 ast_party_connected_line_free(&saved_connected);
10547 }
10548
10549 return retval;
10550 }
10551
10552 int ast_channel_redirecting_sub(struct ast_channel *autoservice_chan, struct ast_channel *sub_chan, const void *redirecting_info, int is_frame)
10553 {
10554 const char *sub;
10555 const char *sub_args;
10556 int retval;
10557
10558 ast_channel_lock(sub_chan);
10559 sub = pbx_builtin_getvar_helper(sub_chan, "REDIRECTING_SEND_SUB");
10560 sub = ast_strdupa(S_OR(sub, ""));
10561 sub_args = pbx_builtin_getvar_helper(sub_chan, "REDIRECTING_SEND_SUB_ARGS");
10562 sub_args = ast_strdupa(S_OR(sub_args, ""));
10563
10564 if (ast_strlen_zero(sub)) {
10565 ast_channel_unlock(sub_chan);
10566 return -1;
10567 }
10568
10569 if (is_frame) {
10570 const struct ast_frame *frame = redirecting_info;
10571
10572 ast_redirecting_parse_data(frame->data.ptr, frame->datalen, ast_channel_redirecting(sub_chan));
10573 } else {
10574 const struct ast_party_redirecting *redirecting = redirecting_info;
10575
10576 ast_party_redirecting_copy(ast_channel_redirecting(sub_chan), redirecting);
10577 }
10578 ast_channel_unlock(sub_chan);
10579
10580 channel_set_intercept_mode(1);
10581 retval = ast_app_run_sub(autoservice_chan, sub_chan, sub, sub_args, 0);
10582 channel_set_intercept_mode(0);
10583 if (!retval) {
10584 struct ast_party_redirecting saved_redirecting;
10585
10586 ast_party_redirecting_init(&saved_redirecting);
10587 ast_channel_lock(sub_chan);
10588 ast_party_redirecting_copy(&saved_redirecting, ast_channel_redirecting(sub_chan));
10589 ast_channel_unlock(sub_chan);
10590 ast_channel_update_redirecting(sub_chan, &saved_redirecting, NULL);
10591 ast_party_redirecting_free(&saved_redirecting);
10592 }
10593
10594 return retval;
10595 }
10596
10597 static void *channel_cc_params_copy(void *data)
10598 {
10599 const struct ast_cc_config_params *src = data;
10600 struct ast_cc_config_params *dest = ast_cc_config_params_init();
10601 if (!dest) {
10602 return NULL;
10603 }
10604 ast_cc_copy_config_params(dest, src);
10605 return dest;
10606 }
10607
10608 static void channel_cc_params_destroy(void *data)
10609 {
10610 struct ast_cc_config_params *cc_params = data;
10611 ast_cc_config_params_destroy(cc_params);
10612 }
10613
10614 static const struct ast_datastore_info cc_channel_datastore_info = {
10615 .type = "Call Completion",
10616 .duplicate = channel_cc_params_copy,
10617 .destroy = channel_cc_params_destroy,
10618 };
10619
10620 int ast_channel_cc_params_init(struct ast_channel *chan,
10621 const struct ast_cc_config_params *base_params)
10622 {
10623 struct ast_cc_config_params *cc_params;
10624 struct ast_datastore *cc_datastore;
10625
10626 if (!(cc_params = ast_cc_config_params_init())) {
10627 return -1;
10628 }
10629
10630 if (!(cc_datastore = ast_datastore_alloc(&cc_channel_datastore_info, NULL))) {
10631 ast_cc_config_params_destroy(cc_params);
10632 return -1;
10633 }
10634
10635 if (base_params) {
10636 ast_cc_copy_config_params(cc_params, base_params);
10637 }
10638 cc_datastore->data = cc_params;
10639 ast_channel_datastore_add(chan, cc_datastore);
10640 return 0;
10641 }
10642
10643 struct ast_cc_config_params *ast_channel_get_cc_config_params(struct ast_channel *chan)
10644 {
10645 struct ast_datastore *cc_datastore;
10646
10647 if (!ast_cc_is_enabled()) {
10648 return NULL;
10649 }
10650
10651 if (!(cc_datastore = ast_channel_datastore_find(chan, &cc_channel_datastore_info, NULL))) {
10652 /* If we can't find the datastore, it almost definitely means that the channel type being
10653 * used has not had its driver modified to parse CC config parameters. The best action
10654 * to take here is to create the parameters on the spot with the defaults set.
10655 */
10656 if (ast_channel_cc_params_init(chan, NULL)) {
10657 return NULL;
10658 }
10659 if (!(cc_datastore = ast_channel_datastore_find(chan, &cc_channel_datastore_info, NULL))) {
10660 /* Should be impossible */
10661 return NULL;
10662 }
10663 }
10664
10665 ast_assert(cc_datastore->data != NULL);
10666 return cc_datastore->data;
10667 }
10668
10669 int ast_channel_get_device_name(struct ast_channel *chan, char *device_name, size_t name_buffer_length)
10670 {
10671 int len = name_buffer_length;
10672 char *dash;
10673 if (!ast_channel_queryoption(chan, AST_OPTION_DEVICE_NAME, device_name, &len, 0)) {
10674 return 0;
10675 }
10676
10677 /* Dang. Do it the old-fashioned way */
10678 ast_copy_string(device_name, ast_channel_name(chan), name_buffer_length);
10679 if ((dash = strrchr(device_name, '-'))) {
10680 *dash = '\0';
10681 }
10682
10683 return 0;
10684 }
10685
10686 int ast_channel_get_cc_agent_type(struct ast_channel *chan, char *agent_type, size_t size)
10687 {
10688 int len = size;
10689 char *slash;
10690
10691 if (!ast_channel_queryoption(chan, AST_OPTION_CC_AGENT_TYPE, agent_type, &len, 0)) {
10692 return 0;
10693 }
10694
10695 ast_copy_string(agent_type, ast_channel_name(chan), size);
10696 if ((slash = strchr(agent_type, '/'))) {
10697 *slash = '\0';
10698 }
10699 return 0;
10700 }
10701
10702 void ast_channel_unlink(struct ast_channel *chan)
10703 {
10704 CHANNELSTORAGE_API(current_channel_storage_instance, remove, chan, 1);
10705 }
10706
10707 struct ast_bridge *ast_channel_get_bridge(const struct ast_channel *chan)
10708 {
10709 struct ast_bridge *bridge;
10710
10711 bridge = ast_channel_internal_bridge(chan);
10712 if (bridge) {
10713 ao2_ref(bridge, +1);
10714 }
10715 return bridge;
10716 }
10717
10718 int ast_channel_is_bridged(const struct ast_channel *chan)
10719 {
10720 return ast_channel_internal_bridge(chan) != NULL;
10721 }
10722
10723 int ast_channel_is_leaving_bridge(struct ast_channel *chan)
10724 {
10725 int hangup_flags = ast_channel_softhangup_internal_flag(chan);
10726 int hangup_test = hangup_flags & AST_SOFTHANGUP_ASYNCGOTO;
10727 int unbridge = ast_channel_unbridged(chan);
10728
10729 /* This function should only return true if either the unbridged flag or
10730 * the ASYNCGOTO soft hangup flag is set and when no other soft hangup
10731 * flags are set. Any other soft hangup flags being set should make it
10732 * return false.
10733 */
10734 return ((hangup_test || unbridge) && (hangup_test == hangup_flags));
10735 }
10736
10737 struct ast_channel *ast_channel_bridge_peer(struct ast_channel *chan)
10738 {
10739 struct ast_channel *peer;
10740 struct ast_bridge *bridge;
10741
10742 /* Get the bridge the channel is in. */
10743 ast_channel_lock(chan);
10744 bridge = ast_channel_get_bridge(chan);
10745 ast_channel_unlock(chan);
10746 if (!bridge) {
10747 return NULL;
10748 }
10749
10750 peer = ast_bridge_peer(bridge, chan);
10751 ao2_ref(bridge, -1);
10752 return peer;
10753 }
10754
10755 struct ast_bridge_channel *ast_channel_get_bridge_channel(struct ast_channel *chan)
10756 {
10757 struct ast_bridge_channel *bridge_channel;
10758
10759 bridge_channel = ast_channel_internal_bridge_channel(chan);
10760 if (bridge_channel) {
10761 ao2_ref(bridge_channel, +1);
10762 }
10763 return bridge_channel;
10764 }
10765
10766 struct ast_channel *ast_channel_yank(struct ast_channel *yankee)
10767 {
10768 struct ast_channel *yanked_chan;
10769 struct {
10770 char *accountcode;
10771 char *exten;
10772 char *context;
10773 char *name;
10774 int amaflags;
10775 int priority;
10776 struct ast_format *readformat;
10777 struct ast_format *writeformat;
10778 } my_vars = { 0, };
10779
10780 ast_channel_lock(yankee);
10781 my_vars.accountcode = ast_strdupa(ast_channel_accountcode(yankee));
10782 my_vars.exten = ast_strdupa(ast_channel_exten(yankee));
10783 my_vars.context = ast_strdupa(ast_channel_context(yankee));
10784 my_vars.name = ast_strdupa(ast_channel_name(yankee));
10785 my_vars.amaflags = ast_channel_amaflags(yankee);
10786 my_vars.priority = ast_channel_priority(yankee);
10787 /* The priority as returned by ast_channel_yank is where the channel
10788 * should go if the dialplan is executed on it. If the channel is
10789 * already executing dialplan then the priority currently set is
10790 * where it is currently. We increment it so it becomes where it should
10791 * execute.
10792 */
10793 if (ast_test_flag(ast_channel_flags(yankee), AST_FLAG_IN_AUTOLOOP)) {
10794 my_vars.priority++;
10795 }
10796 my_vars.writeformat = ao2_bump(ast_channel_writeformat(yankee));
10797 my_vars.readformat = ao2_bump(ast_channel_readformat(yankee));
10798 ast_channel_unlock(yankee);
10799
10800 /* Do not hold any channel locks while calling channel_alloc() since the function
10801 * locks the channel container when linking the new channel in. */
10802 if (!(yanked_chan = ast_channel_alloc(0, AST_STATE_DOWN, 0, 0, my_vars.accountcode,
10803 my_vars.exten, my_vars.context, NULL, yankee, my_vars.amaflags,
10804 "Surrogate/%s", my_vars.name))) {
10805 ao2_cleanup(my_vars.writeformat);
10806 ao2_cleanup(my_vars.readformat);
10807 return NULL;
10808 }
10809
10810 /* Make formats okay */
10811 ast_channel_set_readformat(yanked_chan, my_vars.readformat);
10812 ast_channel_set_writeformat(yanked_chan, my_vars.writeformat);
10813 ao2_cleanup(my_vars.readformat);
10814 ao2_cleanup(my_vars.writeformat);
10815 ast_channel_priority_set(yanked_chan, my_vars.priority);
10816
10817 ast_channel_unlock(yanked_chan);
10818
10819 if (ast_channel_move(yanked_chan, yankee)) {
10820 ast_hangup(yanked_chan);
10821 return NULL;
10822 }
10823
10824 return yanked_chan;
10825 }
10826
10827 /*!
10828 * Mutex that prevents multiple ast_channel_move() operations
10829 * from occurring simultaneously. This is necessary since the
10830 * involved channels have to be locked and unlocked throughout
10831 * the move operation.
10832 *
10833 * The most important data being protected are the masq and masqr
10834 * data on channels. We don't want them getting criss-crossed due
10835 * to multiple moves mucking with them.
10836 */
10837 AST_MUTEX_DEFINE_STATIC(channel_move_lock);
10838
10839 int ast_channel_move(struct ast_channel *dest, struct ast_channel *source)
10840 {
10841 RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
10842 SCOPED_MUTEX(lock, &channel_move_lock);
10843
10844 if (dest == source) {
10845 ast_log(LOG_WARNING, "Can't move channel '%s' into itself!\n",
10846 ast_channel_name(dest));
10847 return -1;
10848 }
10849
10850 ast_channel_lock_both(dest, source);
10851
10852 if (ast_test_flag(ast_channel_flags(dest), AST_FLAG_ZOMBIE)
10853 || ast_test_flag(ast_channel_flags(source), AST_FLAG_ZOMBIE)) {
10854 /* Zombies! Run! */
10855 ast_log(LOG_WARNING,
10856 "Can't move channel. One or both is dead (%s <-- %s)\n",
10857 ast_channel_name(dest), ast_channel_name(source));
10858 ast_channel_unlock(source);
10859 ast_channel_unlock(dest);
10860 return -1;
10861 }
10862
10863 ast_channel_masq_set(dest, source);
10864 ast_channel_masqr_set(source, dest);
10865
10866 blob = ast_json_pack("{s: s}",
10867 "newchanneluniqueid", ast_channel_uniqueid(dest));
10868 ast_channel_publish_blob(source, ast_channel_masquerade_type(), blob);
10869
10870 ast_channel_unlock(dest);
10871 ast_channel_unlock(source);
10872
10873 channel_do_masquerade(dest, source);
10874 return 0;
10875 }
10876
10877 static void suppress_datastore_destroy_cb(void *data)
10878 {
10879 ao2_cleanup(data);
10880 }
10881
10882 static const struct ast_datastore_info suppress_datastore_voice = {
10883 .type = "suppressvoice",
10884 .destroy = suppress_datastore_destroy_cb
10885 };
10886
10887 static void suppress_framehook_destroy_cb(void *data)
10888 {
10889 ao2_cleanup(data);
10890 }
10891
10892 struct suppress_data {
10893 enum ast_frame_type frametype;
10894 unsigned int direction;
10895 int framehook_id;
10896 };
10897
10898 static void suppress_framehook_fixup_cb(void *data, int framehook_id, struct ast_channel *old_chan, struct ast_channel *new_chan)
10899 {
10900 struct suppress_data *suppress = data;
10901
10902 suppress->framehook_id = framehook_id;
10903 }
10904
10905 static struct ast_frame *suppress_framehook_event_cb(struct ast_channel *chan, struct ast_frame *frame, enum ast_framehook_event event, void *data)
10906 {
10907 struct suppress_data *suppress = data;
10908 int suppress_frame = 0;
10909
10910 if (!frame) {
10911 return NULL;
10912 }
10913
10914 if (frame->frametype != suppress->frametype) {
10915 return frame;
10916 }
10917
10918 if (event == AST_FRAMEHOOK_EVENT_READ && (suppress->direction & AST_MUTE_DIRECTION_READ)) {
10919 suppress_frame = 1;
10920 } else if (event == AST_FRAMEHOOK_EVENT_WRITE && (suppress->direction & AST_MUTE_DIRECTION_WRITE)) {
10921 suppress_frame = 1;
10922 }
10923
10924 if (suppress_frame) {
10925 switch (frame->frametype) {
10926 case AST_FRAME_VOICE:
10927 if (event == AST_FRAMEHOOK_EVENT_READ) {
10928 ast_frfree(frame);
10929 }
10930 frame = &ast_null_frame;
10931 break;
10932 default:
10933 break;
10934 }
10935 }
10936
10937 return frame;
10938 }
10939
10940 static const struct ast_datastore_info *suppress_get_datastore_information(enum ast_frame_type frametype)
10941 {
10942 switch (frametype) {
10943 case AST_FRAME_VOICE:
10944 return &suppress_datastore_voice;
10945 default:
10946 return NULL;
10947 }
10948 }
10949
10950 int ast_channel_suppress(struct ast_channel *chan, unsigned int direction, enum ast_frame_type frametype)
10951 {
10952 struct suppress_data *suppress;
10953 const struct ast_datastore_info *datastore_info = NULL;
10954 struct ast_datastore *datastore = NULL;
10955 struct ast_framehook_interface interface = {
10956 .version = AST_FRAMEHOOK_INTERFACE_VERSION,
10957 .event_cb = suppress_framehook_event_cb,
10958 .destroy_cb = suppress_framehook_destroy_cb,
10959 .chan_fixup_cb = suppress_framehook_fixup_cb,
10960 };
10961 int framehook_id;
10962
10963 if (!(datastore_info = suppress_get_datastore_information(frametype))) {
10964 ast_log(LOG_WARNING, "Attempted to suppress an unsupported frame type (%u).\n", frametype);
10965 return -1;
10966 }
10967
10968 if ((datastore = ast_channel_datastore_find(chan, datastore_info, NULL))) {
10969 suppress = datastore->data;
10970 suppress->direction |= direction;
10971 return 0;
10972 }
10973
10974 if (!(suppress = ao2_alloc(sizeof(*suppress), NULL))) {
10975 ast_log(LOG_WARNING, "Failed to allocate data while attempting to suppress a stream.\n");
10976 return -1;
10977 }
10978
10979 suppress->frametype = frametype;
10980 suppress->direction |= direction;
10981
10982 interface.data = suppress;
10983
10984 framehook_id = ast_framehook_attach(chan, &interface);
10985 if (framehook_id < 0) {
10986 /* Hook attach failed. Get rid of the evidence. */
10987 ast_log(LOG_WARNING, "Failed to attach framehook while attempting to suppress a stream.\n");
10988 ao2_ref(suppress, -1);
10989 return -1;
10990 }
10991
10992 /* One ref for the framehook */
10993 ao2_ref(suppress, +1);
10994
10995 suppress->framehook_id = framehook_id;
10996
10997 if (!(datastore = ast_datastore_alloc(datastore_info, NULL))) {
10998 ast_log(LOG_WARNING, "Failed to allocate datastore while attempting to suppress a stream.\n");
10999 ast_framehook_detach(chan, framehook_id);
11000 ao2_ref(suppress, -1);
11001 return -1;
11002 }
11003
11004 /* the ref provided by the allocation is taken by the datastore */
11005 datastore->data = suppress;
11006
11007 ast_channel_datastore_add(chan, datastore);
11008
11009 return 0;
11010 }
11011
11012 int ast_channel_unsuppress(struct ast_channel *chan, unsigned int direction, enum ast_frame_type frametype)
11013 {
11014 const struct ast_datastore_info *datastore_info = NULL;
11015 struct ast_datastore *datastore = NULL;
11016 struct suppress_data *suppress;
11017
11018 if (!(datastore_info = suppress_get_datastore_information(frametype))) {
11019 ast_log(LOG_WARNING, "Attempted to unsuppress an unsupported frame type (%u).\n", frametype);
11020 return -1;
11021 }
11022
11023 if (!(datastore = ast_channel_datastore_find(chan, datastore_info, NULL))) {
11024 /* Nothing to do! */
11025 return 0;
11026 }
11027
11028 suppress = datastore->data;
11029
11030 suppress->direction &= ~(direction);
11031
11032 if (suppress->direction == 0) {
11033 /* Nothing left to suppress. Bye! */
11034 ast_framehook_detach(chan, suppress->framehook_id);
11035 ast_channel_datastore_remove(chan, datastore);
11036 ast_datastore_free(datastore);
11037 }
11038
11039 return 0;
11040 }
11041
11042 void ast_channel_end_dtmf(struct ast_channel *chan, char digit, struct timeval start, const char *why)
11043 {
11044 int dead;
11045 long duration;
11046
11047 ast_channel_lock(chan);
11048 dead = ast_test_flag(ast_channel_flags(chan), AST_FLAG_ZOMBIE)
11049 || (ast_channel_softhangup_internal_flag(chan)
11050 & ~AST_SOFTHANGUP_ASYNCGOTO);
11051 ast_channel_unlock(chan);
11052 if (dead) {
11053 /* Channel is a zombie or a real hangup. */
11054 return;
11055 }
11056
11057 duration = ast_tvdiff_ms(ast_tvnow(), start);
11058 if (duration < option_dtmfminduration) {
11059 duration = option_dtmfminduration;
11060 }
11061 ast_senddigit_end(chan, digit, duration);
11062 ast_log(LOG_DTMF, "DTMF end '%c' simulated on %s due to %s, duration %ld ms\n",
11063 digit, ast_channel_name(chan), why, duration);
11064 }
11065
11066 static void features_destroy(void *obj)
11067 {
11068 ast_bridge_features_destroy(obj);
11069 }
11070
11071 static const struct ast_datastore_info bridge_features_info = {
11072 .type = "bridge-features",
11073 .destroy = features_destroy,
11074 };
11075
11076 struct ast_bridge_features *ast_channel_feature_hooks_get(struct ast_channel *chan)
11077 {
11078 struct ast_datastore *datastore;
11079
11080 datastore = ast_channel_datastore_find(chan, &bridge_features_info, NULL);
11081 if (!datastore) {
11082 return NULL;
11083 }
11084 return datastore->data;
11085 }
11086
11087 static int channel_feature_hooks_set_full(struct ast_channel *chan, struct ast_bridge_features *features, int replace)
11088 {
11089 struct ast_datastore *datastore;
11090 struct ast_bridge_features *ds_features;
11091
11092 datastore = ast_channel_datastore_find(chan, &bridge_features_info, NULL);
11093 if (datastore) {
11094 ds_features = datastore->data;
11095 if (replace) {
11096 ast_bridge_features_cleanup(ds_features);
11097 ast_bridge_features_init(ds_features);
11098 }
11099 if (features) {
11100 ast_bridge_features_merge(ds_features, features);
11101 }
11102 return 0;
11103 }
11104
11105 datastore = ast_datastore_alloc(&bridge_features_info, NULL);
11106 if (!datastore) {
11107 return -1;
11108 }
11109
11110 ds_features = ast_bridge_features_new();
11111 if (!ds_features) {
11112 ast_datastore_free(datastore);
11113 return -1;
11114 }
11115
11116 if (features) {
11117 ast_bridge_features_merge(ds_features, features);
11118 }
11119 datastore->data = ds_features;
11120 ast_channel_datastore_add(chan, datastore);
11121 return 0;
11122 }
11123
11124 int ast_channel_feature_hooks_append(struct ast_channel *chan, struct ast_bridge_features *features)
11125 {
11126 return channel_feature_hooks_set_full(chan, features, 0);
11127 }
11128
11129 int ast_channel_feature_hooks_replace(struct ast_channel *chan, struct ast_bridge_features *features)
11130 {
11131 return channel_feature_hooks_set_full(chan, features, 1);
11132 }
11133
11134 enum ast_channel_error ast_channel_errno(void)
11135 {
11136 return ast_channel_internal_errno();
11137 }
11138
11139 int ast_channel_request_stream_topology_change(struct ast_channel *chan,
11140 struct ast_stream_topology *topology, void *change_source)
11141 {
11142 int res;
11143
11144 ast_assert(chan != NULL);
11145 ast_assert(topology != NULL);
11146
11147 ast_channel_lock(chan);
11148 if (!ast_channel_is_multistream(chan) || !ast_channel_tech(chan)->indicate) {
11149 ast_channel_unlock(chan);
11150 return -1;
11151 }
11152
11153 if (ast_stream_topology_equal(ast_channel_get_stream_topology(chan), topology)) {
11154 ast_debug(2, "%s: Topologies already match. Current: %s Requested: %s\n",
11155 ast_channel_name(chan),
11156 ast_str_tmp(256, ast_stream_topology_to_str(ast_channel_get_stream_topology(chan), &STR_TMP)),
11157 ast_str_tmp(256, ast_stream_topology_to_str(topology, &STR_TMP)));
11158 ast_channel_unlock(chan);
11159 return 0;
11160 }
11161
11162 ast_channel_internal_set_stream_topology_change_source(chan, change_source);
11163
11164 res = ast_channel_tech(chan)->indicate(chan, AST_CONTROL_STREAM_TOPOLOGY_REQUEST_CHANGE, topology, sizeof(topology));
11165 ast_channel_unlock(chan);
11166 return res;
11167 }
11168
11169 int ast_channel_stream_topology_changed(struct ast_channel *chan, struct ast_stream_topology *topology)
11170 {
11171 ast_assert(chan != NULL);
11172 ast_assert(topology != NULL);
11173
11174 if (!ast_channel_is_multistream(chan) || !ast_channel_tech(chan)->indicate) {
11175 return -1;
11176 }
11177
11178 return ast_channel_tech(chan)->indicate(chan, AST_CONTROL_STREAM_TOPOLOGY_CHANGED, topology, sizeof(topology));
11179 }
11180
11181 int ast_channel_stream_topology_changed_externally(struct ast_channel *chan)
11182 {
11183 int res;
11184 struct ast_frame f = { AST_FRAME_CONTROL, .subclass.integer = AST_CONTROL_STREAM_TOPOLOGY_CHANGED };
11185
11186 ast_assert(chan != NULL);
11187
11188 if (!ast_channel_is_multistream(chan)) {
11189 return -1;
11190 }
11191
11192 ast_channel_lock(chan);
11193 ast_channel_internal_set_stream_topology_change_source(chan, (void *)&ast_stream_topology_changed_external);
11194 res = ast_queue_frame(chan, &f);
11195 ast_channel_unlock(chan);
11196
11197 return res;
11198 }
11199
11200 void ast_channel_set_flag(struct ast_channel *chan, unsigned int flag)
11201 {
11202 ast_channel_lock(chan);
11203 ast_set_flag(ast_channel_flags(chan), flag);
11204 ast_channel_unlock(chan);
11205 }
11206
11207 void ast_channel_clear_flag(struct ast_channel *chan, unsigned int flag)
11208 {
11209 ast_channel_lock(chan);
11210 ast_clear_flag(ast_channel_flags(chan), flag);
11211 ast_channel_unlock(chan);
11212 }
11213