Asterisk - The Open Source Telephony Project  18.5.0
channel.c
Go to the documentation of this file.
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/musiconhold.h"
46 #include "asterisk/say.h"
47 #include "asterisk/file.h"
48 #include "asterisk/cli.h"
49 #include "asterisk/translate.h"
50 #include "asterisk/manager.h"
51 #include "asterisk/chanvars.h"
52 #include "asterisk/linkedlists.h"
53 #include "asterisk/indications.h"
54 #include "asterisk/causes.h"
55 #include "asterisk/callerid.h"
56 #include "asterisk/utils.h"
57 #include "asterisk/lock.h"
58 #include "asterisk/app.h"
59 #include "asterisk/transcap.h"
60 #include "asterisk/devicestate.h"
61 #include "asterisk/threadstorage.h"
62 #include "asterisk/slinfactory.h"
63 #include "asterisk/audiohook.h"
64 #include "asterisk/framehook.h"
65 #include "asterisk/timing.h"
66 #include "asterisk/autochan.h"
67 #include "asterisk/stringfields.h"
70 #include "asterisk/features.h"
71 #include "asterisk/bridge.h"
72 #include "asterisk/test.h"
74 #include "asterisk/max_forwards.h"
75 #include "asterisk/stream.h"
76 #include "asterisk/message.h"
77 
78 /*** DOCUMENTATION
79  ***/
80 
81 #if defined(KEEP_TILL_CHANNEL_PARTY_NUMBER_INFO_NEEDED)
82 #if defined(HAVE_PRI)
83 #include "libpri.h"
84 #endif /* defined(HAVE_PRI) */
85 #endif /* defined(KEEP_TILL_CHANNEL_PARTY_NUMBER_INFO_NEEDED) */
86 
87 /* uncomment if you have problems with 'monitoring' synchronized files */
88 #if 0
89 #define MONITOR_CONSTANT_DELAY
90 #define MONITOR_DELAY 150 * 8 /*!< 150 ms of MONITORING DELAY */
91 #endif
92 
93 static int chancount;
94 
95 unsigned long global_fin, global_fout;
96 
98 #define STATE2STR_BUFSIZE 32
99 
100 /*! Default amount of time to use when emulating a DTMF digit as a begin and end
101  * 100ms */
102 #define AST_DEFAULT_EMULATE_DTMF_DURATION 100
103 
104 /*! Default amount of time to use when emulating an MF digit as a begin and end
105  * 55ms */
106 #define DEFAULT_EMULATE_MF_DURATION 55
107 
108 #define DEFAULT_AMA_FLAGS AST_AMA_DOCUMENTATION
109 
110 /*! Minimum amount of time between the end of the last digit and the beginning
111  * of a new one - 45ms */
112 #define AST_MIN_DTMF_GAP 45
113 
114 /*! \brief List of channel drivers */
115 struct chanlist {
116  const struct ast_channel_tech *tech;
118 };
119 
120 /*! \brief the list of registered channel types */
122 
123 /*! \brief All active channels on the system */
124 static struct ao2_container *channels;
125 
126 /*! \brief map AST_CAUSE's to readable string representations
127  *
128  * \ref causes.h
129 */
130 struct causes_map {
131  int cause;
132  const char *name;
133  const char *desc;
134 };
135 
136 static const struct causes_map causes[] = {
137  { AST_CAUSE_UNALLOCATED, "UNALLOCATED", "Unallocated (unassigned) number" },
138  { AST_CAUSE_NO_ROUTE_TRANSIT_NET, "NO_ROUTE_TRANSIT_NET", "No route to specified transmit network" },
139  { AST_CAUSE_NO_ROUTE_DESTINATION, "NO_ROUTE_DESTINATION", "No route to destination" },
140  { AST_CAUSE_MISDIALLED_TRUNK_PREFIX, "MISDIALLED_TRUNK_PREFIX", "Misdialed trunk prefix" },
141  { AST_CAUSE_CHANNEL_UNACCEPTABLE, "CHANNEL_UNACCEPTABLE", "Channel unacceptable" },
142  { AST_CAUSE_CALL_AWARDED_DELIVERED, "CALL_AWARDED_DELIVERED", "Call awarded and being delivered in an established channel" },
143  { AST_CAUSE_PRE_EMPTED, "PRE_EMPTED", "Pre-empted" },
144  { AST_CAUSE_NUMBER_PORTED_NOT_HERE, "NUMBER_PORTED_NOT_HERE", "Number ported elsewhere" },
145  { AST_CAUSE_NORMAL_CLEARING, "NORMAL_CLEARING", "Normal Clearing" },
146  { AST_CAUSE_USER_BUSY, "USER_BUSY", "User busy" },
147  { AST_CAUSE_NO_USER_RESPONSE, "NO_USER_RESPONSE", "No user responding" },
148  { AST_CAUSE_NO_ANSWER, "NO_ANSWER", "User alerting, no answer" },
149  { AST_CAUSE_SUBSCRIBER_ABSENT, "SUBSCRIBER_ABSENT", "Subscriber absent" },
150  { AST_CAUSE_CALL_REJECTED, "CALL_REJECTED", "Call Rejected" },
151  { AST_CAUSE_NUMBER_CHANGED, "NUMBER_CHANGED", "Number changed" },
152  { AST_CAUSE_REDIRECTED_TO_NEW_DESTINATION, "REDIRECTED_TO_NEW_DESTINATION", "Redirected to new destination" },
153  { AST_CAUSE_ANSWERED_ELSEWHERE, "ANSWERED_ELSEWHERE", "Answered elsewhere" },
154  { AST_CAUSE_DESTINATION_OUT_OF_ORDER, "DESTINATION_OUT_OF_ORDER", "Destination out of order" },
155  { AST_CAUSE_INVALID_NUMBER_FORMAT, "INVALID_NUMBER_FORMAT", "Invalid number format" },
156  { AST_CAUSE_FACILITY_REJECTED, "FACILITY_REJECTED", "Facility rejected" },
157  { AST_CAUSE_RESPONSE_TO_STATUS_ENQUIRY, "RESPONSE_TO_STATUS_ENQUIRY", "Response to STATus ENQuiry" },
158  { AST_CAUSE_NORMAL_UNSPECIFIED, "NORMAL_UNSPECIFIED", "Normal, unspecified" },
159  { AST_CAUSE_NORMAL_CIRCUIT_CONGESTION, "NORMAL_CIRCUIT_CONGESTION", "Circuit/channel congestion" },
160  { AST_CAUSE_NETWORK_OUT_OF_ORDER, "NETWORK_OUT_OF_ORDER", "Network out of order" },
161  { AST_CAUSE_NORMAL_TEMPORARY_FAILURE, "NORMAL_TEMPORARY_FAILURE", "Temporary failure" },
162  { AST_CAUSE_SWITCH_CONGESTION, "SWITCH_CONGESTION", "Switching equipment congestion" },
163  { AST_CAUSE_ACCESS_INFO_DISCARDED, "ACCESS_INFO_DISCARDED", "Access information discarded" },
164  { AST_CAUSE_REQUESTED_CHAN_UNAVAIL, "REQUESTED_CHAN_UNAVAIL", "Requested channel not available" },
165  { AST_CAUSE_FACILITY_NOT_SUBSCRIBED, "FACILITY_NOT_SUBSCRIBED", "Facility not subscribed" },
166  { AST_CAUSE_OUTGOING_CALL_BARRED, "OUTGOING_CALL_BARRED", "Outgoing call barred" },
167  { AST_CAUSE_INCOMING_CALL_BARRED, "INCOMING_CALL_BARRED", "Incoming call barred" },
168  { AST_CAUSE_BEARERCAPABILITY_NOTAUTH, "BEARERCAPABILITY_NOTAUTH", "Bearer capability not authorized" },
169  { AST_CAUSE_BEARERCAPABILITY_NOTAVAIL, "BEARERCAPABILITY_NOTAVAIL", "Bearer capability not available" },
170  { AST_CAUSE_BEARERCAPABILITY_NOTIMPL, "BEARERCAPABILITY_NOTIMPL", "Bearer capability not implemented" },
171  { AST_CAUSE_CHAN_NOT_IMPLEMENTED, "CHAN_NOT_IMPLEMENTED", "Channel not implemented" },
172  { AST_CAUSE_FACILITY_NOT_IMPLEMENTED, "FACILITY_NOT_IMPLEMENTED", "Facility not implemented" },
173  { AST_CAUSE_INVALID_CALL_REFERENCE, "INVALID_CALL_REFERENCE", "Invalid call reference value" },
174  { AST_CAUSE_INCOMPATIBLE_DESTINATION, "INCOMPATIBLE_DESTINATION", "Incompatible destination" },
175  { AST_CAUSE_INVALID_MSG_UNSPECIFIED, "INVALID_MSG_UNSPECIFIED", "Invalid message unspecified" },
176  { AST_CAUSE_MANDATORY_IE_MISSING, "MANDATORY_IE_MISSING", "Mandatory information element is missing" },
177  { AST_CAUSE_MESSAGE_TYPE_NONEXIST, "MESSAGE_TYPE_NONEXIST", "Message type nonexist." },
178  { AST_CAUSE_WRONG_MESSAGE, "WRONG_MESSAGE", "Wrong message" },
179  { AST_CAUSE_IE_NONEXIST, "IE_NONEXIST", "Info. element nonexist or not implemented" },
180  { AST_CAUSE_INVALID_IE_CONTENTS, "INVALID_IE_CONTENTS", "Invalid information element contents" },
181  { AST_CAUSE_WRONG_CALL_STATE, "WRONG_CALL_STATE", "Message not compatible with call state" },
182  { AST_CAUSE_RECOVERY_ON_TIMER_EXPIRE, "RECOVERY_ON_TIMER_EXPIRE", "Recover on timer expiry" },
183  { AST_CAUSE_MANDATORY_IE_LENGTH_ERROR, "MANDATORY_IE_LENGTH_ERROR", "Mandatory IE length error" },
184  { AST_CAUSE_PROTOCOL_ERROR, "PROTOCOL_ERROR", "Protocol error, unspecified" },
185  { AST_CAUSE_INTERWORKING, "INTERWORKING", "Interworking, unspecified" },
186 };
187 
189 {
190  struct chanlist *cl;
191  struct ast_variable *var = NULL, *prev = NULL;
192 
195  if (prev) {
196  if ((prev->next = ast_variable_new(cl->tech->type, cl->tech->description, "")))
197  prev = prev->next;
198  } else {
199  var = ast_variable_new(cl->tech->type, cl->tech->description, "");
200  prev = var;
201  }
202  }
204 
205  return var;
206 }
207 
208 #if defined(KEEP_TILL_CHANNEL_PARTY_NUMBER_INFO_NEEDED)
209 static const char *party_number_ton2str(int ton)
210 {
211 #if defined(HAVE_PRI)
212  switch ((ton >> 4) & 0x07) {
213  case PRI_TON_INTERNATIONAL:
214  return "International";
215  case PRI_TON_NATIONAL:
216  return "National";
217  case PRI_TON_NET_SPECIFIC:
218  return "Network Specific";
219  case PRI_TON_SUBSCRIBER:
220  return "Subscriber";
221  case PRI_TON_ABBREVIATED:
222  return "Abbreviated";
223  case PRI_TON_RESERVED:
224  return "Reserved";
225  case PRI_TON_UNKNOWN:
226  default:
227  break;
228  }
229 #endif /* defined(HAVE_PRI) */
230  return "Unknown";
231 }
232 #endif /* defined(KEEP_TILL_CHANNEL_PARTY_NUMBER_INFO_NEEDED) */
233 
234 #if defined(KEEP_TILL_CHANNEL_PARTY_NUMBER_INFO_NEEDED)
235 static const char *party_number_plan2str(int plan)
236 {
237 #if defined(HAVE_PRI)
238  switch (plan & 0x0F) {
239  default:
240  case PRI_NPI_UNKNOWN:
241  break;
242  case PRI_NPI_E163_E164:
243  return "Public (E.163/E.164)";
244  case PRI_NPI_X121:
245  return "Data (X.121)";
246  case PRI_NPI_F69:
247  return "Telex (F.69)";
248  case PRI_NPI_NATIONAL:
249  return "National Standard";
250  case PRI_NPI_PRIVATE:
251  return "Private";
252  case PRI_NPI_RESERVED:
253  return "Reserved";
254  }
255 #endif /* defined(HAVE_PRI) */
256  return "Unknown";
257 }
258 #endif /* defined(KEEP_TILL_CHANNEL_PARTY_NUMBER_INFO_NEEDED) */
259 
260 /*! \brief Show channel types - CLI command */
261 static char *handle_cli_core_show_channeltypes(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
262 {
263 #define FORMAT "%-15.15s %-40.40s %-13.13s %-13.13s %-13.13s %-13.13s\n"
264  struct chanlist *cl;
265  int count_chan = 0;
266 
267  switch (cmd) {
268  case CLI_INIT:
269  e->command = "core show channeltypes";
270  e->usage =
271  "Usage: core show channeltypes\n"
272  " Lists available channel types registered in your\n"
273  " Asterisk server.\n";
274  return NULL;
275  case CLI_GENERATE:
276  return NULL;
277  }
278 
279  if (a->argc != 3)
280  return CLI_SHOWUSAGE;
281 
282  ast_cli(a->fd, FORMAT, "Type", "Description", "Devicestate", "Presencestate", "Indications", "Transfer");
283  ast_cli(a->fd, FORMAT, "-------------", "-------------", "-------------", "-------------", "-------------", "-------------");
284 
287  ast_cli(a->fd, FORMAT, cl->tech->type, cl->tech->description,
288  (cl->tech->devicestate) ? "yes" : "no",
289  (cl->tech->presencestate) ? "yes" : "no",
290  (cl->tech->indicate) ? "yes" : "no",
291  (cl->tech->transfer) ? "yes" : "no");
292  count_chan++;
293  }
295 
296  ast_cli(a->fd, "----------\n%d channel drivers registered.\n", count_chan);
297 
298  return CLI_SUCCESS;
299 
300 #undef FORMAT
301 }
302 
303 static char *complete_channeltypes(struct ast_cli_args *a)
304 {
305  struct chanlist *cl;
306  int wordlen;
307 
308  if (a->pos != 3) {
309  return NULL;
310  }
311 
312  wordlen = strlen(a->word);
313 
316  if (!strncasecmp(a->word, cl->tech->type, wordlen)) {
318  }
319  }
321 
322  return NULL;
323 }
324 
325 /*! \brief Show details about a channel driver - CLI command */
326 static char *handle_cli_core_show_channeltype(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
327 {
328  struct chanlist *cl = NULL;
329  struct ast_str *codec_buf = ast_str_alloca(AST_FORMAT_CAP_NAMES_LEN);
330 
331  switch (cmd) {
332  case CLI_INIT:
333  e->command = "core show channeltype";
334  e->usage =
335  "Usage: core show channeltype <name>\n"
336  " Show details about the specified channel type, <name>.\n";
337  return NULL;
338  case CLI_GENERATE:
339  return complete_channeltypes(a);
340  }
341 
342  if (a->argc != 4)
343  return CLI_SHOWUSAGE;
344 
346 
348  if (!strncasecmp(cl->tech->type, a->argv[3], strlen(cl->tech->type)))
349  break;
350  }
351 
352 
353  if (!cl) {
354  ast_cli(a->fd, "\n%s is not a registered channel driver.\n", a->argv[3]);
356  return CLI_FAILURE;
357  }
358 
359  ast_cli(a->fd,
360  "-- Info about channel driver: %s --\n"
361  " Device State: %s\n"
362  "Presence State: %s\n"
363  " Indication: %s\n"
364  " Transfer : %s\n"
365  " Capabilities: %s\n"
366  " Digit Begin: %s\n"
367  " Digit End: %s\n"
368  " Send HTML : %s\n"
369  " Image Support: %s\n"
370  " Text Support: %s\n",
371  cl->tech->type,
372  (cl->tech->devicestate) ? "yes" : "no",
373  (cl->tech->presencestate) ? "yes" : "no",
374  (cl->tech->indicate) ? "yes" : "no",
375  (cl->tech->transfer) ? "yes" : "no",
376  ast_format_cap_get_names(cl->tech->capabilities, &codec_buf),
377  (cl->tech->send_digit_begin) ? "yes" : "no",
378  (cl->tech->send_digit_end) ? "yes" : "no",
379  (cl->tech->send_html) ? "yes" : "no",
380  (cl->tech->send_image) ? "yes" : "no",
381  (cl->tech->send_text) ? "yes" : "no"
382 
383  );
384 
386 
387  return CLI_SUCCESS;
388 }
389 
390 static struct ast_cli_entry cli_channel[] = {
391  AST_CLI_DEFINE(handle_cli_core_show_channeltypes, "List available channel types"),
392  AST_CLI_DEFINE(handle_cli_core_show_channeltype, "Give more details on that channel type")
393 };
394 
395 static struct ast_frame *kill_read(struct ast_channel *chan)
396 {
397  /* Hangup channel. */
398  return NULL;
399 }
400 
401 static struct ast_frame *kill_exception(struct ast_channel *chan)
402 {
403  /* Hangup channel. */
404  return NULL;
405 }
406 
407 static int kill_write(struct ast_channel *chan, struct ast_frame *frame)
408 {
409  /* Hangup channel. */
410  return -1;
411 }
412 
413 static int kill_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
414 {
415  /* No problem fixing up the channel. */
416  return 0;
417 }
418 
419 static int kill_hangup(struct ast_channel *chan)
420 {
422  return 0;
423 }
424 
425 /*!
426  * \brief Kill the channel channel driver technology descriptor.
427  *
428  * \details
429  * The purpose of this channel technology is to encourage the
430  * channel to hangup as quickly as possible.
431  *
432  * \note Used by DTMF atxfer and zombie channels.
433  */
435  .type = "Kill",
436  .description = "Kill channel (should not see this)",
437  .read = kill_read,
438  .exception = kill_exception,
439  .write = kill_write,
440  .fixup = kill_fixup,
441  .hangup = kill_hangup,
442 };
443 
444 /*! \brief Checks to see if a channel is needing hang up */
446 {
447  if (ast_channel_softhangup_internal_flag(chan)) /* yes if soft hangup flag set */
448  return 1;
449  if (ast_tvzero(*ast_channel_whentohangup(chan))) /* no if no hangup scheduled */
450  return 0;
451  if (ast_tvdiff_ms(*ast_channel_whentohangup(chan), ast_tvnow()) > 0) /* no if hangup time has not come yet. */
452  return 0;
453  ast_debug(4, "Hangup time has come: %" PRIi64 "\n", ast_tvdiff_ms(*ast_channel_whentohangup(chan), ast_tvnow()));
454  ast_test_suite_event_notify("HANGUP_TIME", "Channel: %s", ast_channel_name(chan));
456  return 1;
457 }
458 
460 {
461  int res;
462  ast_channel_lock(chan);
463  res = ast_check_hangup(chan);
464  ast_channel_unlock(chan);
465  return res;
466 }
467 
469 {
470  ast_channel_lock(chan);
471 
472  if (causecode > 0) {
473  ast_debug(1, "Setting hangupcause of channel %s to %d (is %d now)\n",
474  ast_channel_name(chan), causecode, ast_channel_hangupcause(chan));
475 
476  ast_channel_hangupcause_set(chan, causecode);
477  }
478 
480 
481  ast_channel_unlock(chan);
482 }
483 
484 static int ast_channel_softhangup_cb(void *obj, void *arg, int flags)
485 {
486  struct ast_channel *chan = obj;
487 
489 
490  return 0;
491 }
492 
494 {
496 }
497 
498 /*! \brief returns number of active/allocated channels */
500 {
501  return channels ? ao2_container_count(channels) : 0;
502 }
503 
505 {
507 }
508 
509 /*! \brief Set when to hangup channel */
510 void ast_channel_setwhentohangup_tv(struct ast_channel *chan, struct timeval offset)
511 {
512  if (ast_tvzero(offset)) {
513  ast_channel_whentohangup_set(chan, &offset);
514  } else {
515  struct timeval tv = ast_tvadd(offset, ast_tvnow());
516  ast_channel_whentohangup_set(chan, &tv);
517  }
519  return;
520 }
521 
522 /*! \brief Compare a offset with when to hangup channel */
523 int ast_channel_cmpwhentohangup_tv(struct ast_channel *chan, struct timeval offset)
524 {
525  struct timeval whentohangup;
526 
528  return ast_tvzero(offset) ? 0 : -1;
529 
530  if (ast_tvzero(offset))
531  return 1;
532 
533  whentohangup = ast_tvadd(offset, ast_tvnow());
534 
535  return ast_tvdiff_ms(whentohangup, *ast_channel_whentohangup(chan));
536 }
537 
538 /*! \brief Register a new telephony channel in Asterisk */
540 {
541  struct chanlist *chan;
542 
544 
546  if (!strcasecmp(tech->type, chan->tech->type)) {
547  ast_log(LOG_WARNING, "Already have a handler for type '%s'\n", tech->type);
549  return -1;
550  }
551  }
552 
553  if (!(chan = ast_calloc(1, sizeof(*chan)))) {
555  return -1;
556  }
557  chan->tech = tech;
559 
560  ast_debug(1, "Registered handler for '%s' (%s)\n", chan->tech->type, chan->tech->description);
561 
562  ast_verb(2, "Registered channel type '%s' (%s)\n", chan->tech->type, chan->tech->description);
563 
565 
566  return 0;
567 }
568 
569 /*! \brief Unregister channel driver */
571 {
572  struct chanlist *chan;
573 
574  ast_debug(1, "Unregistering channel type '%s'\n", tech->type);
575 
577 
579  if (chan->tech == tech) {
581  ast_free(chan);
582  ast_verb(2, "Unregistered channel type '%s'\n", tech->type);
583  break;
584  }
585  }
587 
589 }
590 
591 /*! \brief Get handle to channel driver based on name */
592 const struct ast_channel_tech *ast_get_channel_tech(const char *name)
593 {
594  struct chanlist *chanls;
595  const struct ast_channel_tech *ret = NULL;
596 
598 
599  AST_RWLIST_TRAVERSE(&backends, chanls, list) {
600  if (!strcasecmp(name, chanls->tech->type)) {
601  ret = chanls->tech;
602  break;
603  }
604  }
605 
607 
608  return ret;
609 }
610 
611 /*! \brief Gives the string form of a given hangup cause */
612 const char *ast_cause2str(int cause)
613 {
614  int x;
615 
616  for (x = 0; x < ARRAY_LEN(causes); x++) {
617  if (causes[x].cause == cause)
618  return causes[x].desc;
619  }
620 
621  return "Unknown";
622 }
623 
624 /*! \brief Convert a symbolic hangup cause to number */
625 int ast_str2cause(const char *name)
626 {
627  int x;
628 
629  for (x = 0; x < ARRAY_LEN(causes); x++)
630  if (!strncasecmp(causes[x].name, name, strlen(causes[x].name)))
631  return causes[x].cause;
632 
633  return -1;
634 }
635 
636 /*! \brief Gives the string form of a given channel state.
637  *
638  * \note This function is not reentrant.
639  *
640  * \param state
641  */
643 {
644  char *buf;
645 
646  switch (state) {
647  case AST_STATE_DOWN:
648  return "Down";
649  case AST_STATE_RESERVED:
650  return "Rsrvd";
651  case AST_STATE_OFFHOOK:
652  return "OffHook";
653  case AST_STATE_DIALING:
654  return "Dialing";
655  case AST_STATE_RING:
656  return "Ring";
657  case AST_STATE_RINGING:
658  return "Ringing";
659  case AST_STATE_UP:
660  return "Up";
661  case AST_STATE_BUSY:
662  return "Busy";
664  return "Dialing Offhook";
665  case AST_STATE_PRERING:
666  return "Pre-ring";
667  case AST_STATE_MUTE:
668  return "Mute";
669  default:
671  return "Unknown";
672  snprintf(buf, STATE2STR_BUFSIZE, "Unknown (%u)", state);
673  return buf;
674  }
675 }
676 
677 /*! \brief Gives the string form of a given transfer capability */
678 char *ast_transfercapability2str(int transfercapability)
679 {
680  switch (transfercapability) {
682  return "SPEECH";
684  return "DIGITAL";
686  return "RESTRICTED_DIGITAL";
688  return "3K1AUDIO";
690  return "DIGITAL_W_TONES";
691  case AST_TRANS_CAP_VIDEO:
692  return "VIDEO";
693  default:
694  return "UNKNOWN";
695  }
696 }
697 
698 /*! \brief Channel technology used to extract a channel from a running application. The
699  * channel created with this technology will be immediately hung up - most external
700  * applications won't ever want to see this.
701  */
702 static const struct ast_channel_tech surrogate_tech = {
703  .type = "Surrogate",
704  .description = "Surrogate channel used to pull channel from an application",
705  .properties = AST_CHAN_TP_INTERNAL,
706 };
707 
708 static const struct ast_channel_tech null_tech = {
709  .type = "NULL",
710  .description = "Null channel (should not see this)",
711 };
712 
713 static void ast_channel_destructor(void *obj);
714 static void ast_dummy_channel_destructor(void *obj);
715 static int ast_channel_by_uniqueid_cb(void *obj, void *arg, void *data, int flags);
716 
717 static int does_id_conflict(const char *uniqueid)
718 {
719  struct ast_channel *conflict;
720  size_t length = 0;
721 
722  if (ast_strlen_zero(uniqueid)) {
723  return 0;
724  }
725 
726  conflict = ast_channel_callback(ast_channel_by_uniqueid_cb, (char *) uniqueid, &length, OBJ_NOLOCK);
727  if (conflict) {
728  ast_log(LOG_ERROR, "Channel Unique ID '%s' already in use by channel %s(%p)\n",
729  uniqueid, ast_channel_name(conflict), conflict);
730  ast_channel_unref(conflict);
731  return 1;
732  }
733 
734  return 0;
735 }
736 
737 /*! \brief Create a new channel structure */
738 static struct ast_channel *__attribute__((format(printf, 15, 0)))
739 __ast_channel_alloc_ap(int needqueue, int state, const char *cid_num, const char *cid_name,
740  const char *acctcode, const char *exten, const char *context, const struct ast_assigned_ids *assignedids,
741  const struct ast_channel *requestor, enum ama_flags amaflag, struct ast_endpoint *endpoint,
742  const char *file, int line,
743  const char *function, const char *name_fmt, va_list ap)
744 {
745  struct ast_channel *tmp;
746  struct varshead *headp;
747  char *tech = "", *tech2 = NULL;
748  struct ast_format_cap *nativeformats;
749  struct ast_sched_context *schedctx;
750  struct ast_timer *timer;
751  struct timeval now;
752  const struct ast_channel_tech *channel_tech;
753  struct ast_stream_topology *topology;
754 
755  /* If shutting down, don't allocate any new channels */
756  if (ast_shutting_down()) {
757  ast_log(LOG_WARNING, "Channel allocation failed: Refusing due to active shutdown\n");
758  return NULL;
759  }
760 
761  tmp = __ast_channel_internal_alloc(ast_channel_destructor, assignedids, requestor,
762  file, line, function);
763  if (!tmp) {
764  /* Channel structure allocation failure. */
765  return NULL;
766  }
767 
769 
770  /*
771  * Init file descriptors to unopened state so
772  * the destructor can know not to close them.
773  */
774  ast_channel_timingfd_set(tmp, -1);
777 
779  if (!nativeformats) {
780  /*
781  * Aborting the channel creation. We do not need to complete staging
782  * the channel snapshot because the channel has not been finalized or
783  * linked into the channels container yet. Nobody else knows about
784  * this channel nor will anybody ever know about it.
785  */
786  return ast_channel_unref(tmp);
787  }
788  ast_format_cap_append(nativeformats, ast_format_none, 0);
789  ast_channel_nativeformats_set(tmp, nativeformats);
790  ao2_ref(nativeformats, -1);
791 
796 
797  if (!(schedctx = ast_sched_context_create())) {
798  ast_log(LOG_WARNING, "Channel allocation failed: Unable to create schedule context\n");
799  /* See earlier channel creation abort comment above. */
800  return ast_channel_unref(tmp);
801  }
802  ast_channel_sched_set(tmp, schedctx);
803 
809 
810  if (cid_name) {
811  ast_channel_caller(tmp)->id.name.valid = 1;
813  if (!ast_channel_caller(tmp)->id.name.str) {
814  /* See earlier channel creation abort comment above. */
815  return ast_channel_unref(tmp);
816  }
817  }
818  if (cid_num) {
819  ast_channel_caller(tmp)->id.number.valid = 1;
821  if (!ast_channel_caller(tmp)->id.number.str) {
822  /* See earlier channel creation abort comment above. */
823  return ast_channel_unref(tmp);
824  }
825  }
826 
827  if ((timer = ast_timer_open())) {
828  ast_channel_timer_set(tmp, timer);
829  if (strcmp(ast_timer_get_name(ast_channel_timer(tmp)), "timerfd")) {
830  needqueue = 0;
831  }
833  }
834 
835  if (needqueue && ast_channel_internal_alertpipe_init(tmp)) {
836  /* See earlier channel creation abort comment above. */
837  return ast_channel_unref(tmp);
838  }
839 
840  if (!(topology = ast_stream_topology_alloc())) {
841  return ast_channel_unref(tmp);
842  }
844 
845  /* Always watch the alertpipe */
847  /* And timing pipe */
849 
850  /* Initial state */
853 
854  ast_channel_streamid_set(tmp, -1);
855  ast_channel_vstreamid_set(tmp, -1);
856 
859 
860  now = ast_tvnow();
861  ast_channel_creationtime_set(tmp, &now);
862 
864 
865  if (!ast_strlen_zero(name_fmt)) {
866  char *slash, *slash2;
867  /* Almost every channel is calling this function, and setting the name via the ast_string_field_build() call.
868  * And they all use slightly different formats for their name string.
869  * This means, to set the name here, we have to accept variable args, and call the string_field_build from here.
870  * This means, that the stringfields must have a routine that takes the va_lists directly, and
871  * uses them to build the string, instead of forming the va_lists internally from the vararg ... list.
872  * This new function was written so this can be accomplished.
873  */
874  ast_channel_name_build_va(tmp, name_fmt, ap);
876  if ((slash = strchr(tech, '/'))) {
877  if ((slash2 = strchr(slash + 1, '/'))) {
878  tech2 = slash + 1;
879  *slash2 = '\0';
880  }
881  *slash = '\0';
882  }
883  } else {
884  /*
885  * Start the string with '-' so it becomes an empty string
886  * in the destructor.
887  */
888  ast_channel_name_set(tmp, "-**Unknown**");
889  }
890 
891  if (amaflag != AST_AMA_NONE) {
892  ast_channel_amaflags_set(tmp, amaflag);
893  } else {
895  }
896 
897  if (!ast_strlen_zero(acctcode)) {
898  ast_channel_accountcode_set(tmp, acctcode);
899  }
900  ast_channel_language_set(tmp, ast_defaultlanguage);
901 
902  ast_channel_context_set(tmp, S_OR(context, "default"));
903  ast_channel_exten_set(tmp, S_OR(exten, "s"));
904  ast_channel_priority_set(tmp, 1);
905 
906  headp = ast_channel_varshead(tmp);
908 
912 
913  channel_tech = ast_get_channel_tech(tech);
914  if (!channel_tech && !ast_strlen_zero(tech2)) {
915  channel_tech = ast_get_channel_tech(tech2);
916  }
917  if (channel_tech) {
918  ast_channel_tech_set(tmp, channel_tech);
919  } else {
920  ast_channel_tech_set(tmp, &null_tech);
921  }
922 
923  /* You might scream "locking inversion" at seeing this but it is actually perfectly fine.
924  * Since the channel was just created nothing can know about it yet or even acquire it.
925  */
926  ast_channel_lock(tmp);
927 
928  ao2_lock(channels);
929 
930  if (assignedids && (does_id_conflict(assignedids->uniqueid) || does_id_conflict(assignedids->uniqueid2))) {
932  ao2_unlock(channels);
933  ast_channel_unlock(tmp);
934  /* See earlier channel creation abort comment above. */
935  return ast_channel_unref(tmp);
936  }
937 
938  /* Finalize and link into the channels container. */
941  ao2_link_flags(channels, tmp, OBJ_NOLOCK);
942 
943  ao2_unlock(channels);
944 
945  if (endpoint) {
946  ast_endpoint_add_channel(endpoint, tmp);
947  }
948 
949  /*
950  * And now, since the channel structure is built, and has its name, let
951  * the world know of its existance
952  */
954 
955  ast_debug(1, "Channel %p '%s' allocated\n", tmp, ast_channel_name(tmp));
956 
957  return tmp;
958 }
959 
960 struct ast_channel *__ast_channel_alloc(int needqueue, int state, const char *cid_num,
961  const char *cid_name, const char *acctcode,
962  const char *exten, const char *context, const struct ast_assigned_ids *assignedids,
963  const struct ast_channel *requestor, enum ama_flags amaflag,
964  struct ast_endpoint *endpoint,
965  const char *file, int line, const char *function,
966  const char *name_fmt, ...)
967 {
968  va_list ap;
969  struct ast_channel *result;
970 
971  va_start(ap, name_fmt);
972  result = __ast_channel_alloc_ap(needqueue, state, cid_num, cid_name, acctcode, exten, context,
973  assignedids, requestor, amaflag, endpoint, file, line, function, name_fmt, ap);
974  va_end(ap);
975 
976  return result;
977 }
978 
979 /* only do the minimum amount of work needed here to make a channel
980  * structure that can be used to expand channel vars */
981 struct ast_channel *__ast_dummy_channel_alloc(const char *file, int line, const char *function)
982 {
983  struct ast_channel *tmp;
984  struct varshead *headp;
985 
987  file, line, function);
988  if (!tmp) {
989  /* Dummy channel structure allocation failure. */
990  return NULL;
991  }
992 
995 
996  /*
997  * Init file descriptors to unopened state just in case
998  * autoservice is called on the channel or something tries to
999  * read a frame from it.
1000  */
1001  ast_channel_timingfd_set(tmp, -1);
1004 
1006 
1008 
1009  headp = ast_channel_varshead(tmp);
1011 
1012  return tmp;
1013 }
1014 
1015 static int __ast_queue_frame(struct ast_channel *chan, struct ast_frame *fin, int head, struct ast_frame *after)
1016 {
1017  struct ast_frame *f;
1018  struct ast_frame *cur;
1019  unsigned int new_frames = 0;
1020  unsigned int new_voice_frames = 0;
1021  unsigned int queued_frames = 0;
1022  unsigned int queued_voice_frames = 0;
1024 
1025  ast_channel_lock(chan);
1026 
1027  /*
1028  * Check the last frame on the queue if we are queuing the new
1029  * frames after it.
1030  */
1031  cur = AST_LIST_LAST(ast_channel_readq(chan));
1032  if (cur && cur->frametype == AST_FRAME_CONTROL && !head && (!after || after == cur)) {
1033  switch (cur->subclass.integer) {
1034  case AST_CONTROL_END_OF_Q:
1035  if (fin->frametype == AST_FRAME_CONTROL
1036  && fin->subclass.integer == AST_CONTROL_HANGUP) {
1037  /*
1038  * Destroy the end-of-Q marker frame so we can queue the hangup
1039  * frame in its place.
1040  */
1042  ast_frfree(cur);
1043 
1044  /*
1045  * This has degenerated to a normal queue append anyway. Since
1046  * we just destroyed the last frame in the queue we must make
1047  * sure that "after" is NULL or bad things will happen.
1048  */
1049  after = NULL;
1050  break;
1051  }
1052  /* Fall through */
1053  case AST_CONTROL_HANGUP:
1054  /* Don't queue anything. */
1055  ast_channel_unlock(chan);
1056  return 0;
1057  default:
1058  break;
1059  }
1060  }
1061 
1062  /* Build copies of all the new frames and count them */
1064  for (cur = fin; cur; cur = AST_LIST_NEXT(cur, frame_list)) {
1065  if (!(f = ast_frdup(cur))) {
1066  if (AST_LIST_FIRST(&frames)) {
1068  }
1069  ast_channel_unlock(chan);
1070  return -1;
1071  }
1072 
1074  new_frames++;
1075  if (f->frametype == AST_FRAME_VOICE) {
1076  new_voice_frames++;
1077  }
1078  }
1079 
1080  /* Count how many frames exist on the queue */
1082  queued_frames++;
1083  if (cur->frametype == AST_FRAME_VOICE) {
1084  queued_voice_frames++;
1085  }
1086  }
1087 
1088  if ((queued_frames + new_frames > 128 || queued_voice_frames + new_voice_frames > 96)) {
1089  int count = 0;
1090  ast_log(LOG_WARNING, "Exceptionally long %squeue length queuing to %s\n", queued_frames + new_frames > 128 ? "" : "voice ", ast_channel_name(chan));
1092  /* Save the most recent frame */
1093  if (!AST_LIST_NEXT(cur, frame_list)) {
1094  break;
1095  } else if (cur->frametype == AST_FRAME_VOICE || cur->frametype == AST_FRAME_VIDEO || cur->frametype == AST_FRAME_NULL) {
1096  if (++count > 64) {
1097  break;
1098  }
1100  ast_frfree(cur);
1101 
1102  /* Read from the alert pipe for each flushed frame. */
1104  }
1105  }
1107  }
1108 
1109  if (after) {
1111  } else {
1112  if (head) {
1115  }
1117  }
1118 
1119  if (ast_channel_alert_writable(chan)) {
1120  /* Write to the alert pipe for each added frame */
1121  while (new_frames--) {
1122  if (ast_channel_alert_write(chan)) {
1123  ast_log(LOG_WARNING, "Unable to write to alert pipe on %s (qlen = %u): %s!\n",
1124  ast_channel_name(chan), queued_frames, strerror(errno));
1125  break;
1126  }
1127  }
1128  } else if (ast_channel_timingfd(chan) > -1) {
1130  } else if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_BLOCKING)) {
1131  pthread_kill(ast_channel_blocker(chan), SIGURG);
1132  }
1133 
1134  ast_channel_unlock(chan);
1135 
1136  return 0;
1137 }
1138 
1139 int ast_queue_frame(struct ast_channel *chan, struct ast_frame *fin)
1140 {
1141  return __ast_queue_frame(chan, fin, 0, NULL);
1142 }
1143 
1145 {
1146  return __ast_queue_frame(chan, fin, 1, NULL);
1147 }
1148 
1149 /*! \brief Queue a hangup frame for channel */
1151 {
1152  struct ast_frame f = { AST_FRAME_CONTROL, .subclass.integer = AST_CONTROL_HANGUP };
1153  int res;
1154 
1155  /* Yeah, let's not change a lock-critical value without locking */
1156  ast_channel_lock(chan);
1159 
1160  res = ast_queue_frame(chan, &f);
1161  ast_channel_unlock(chan);
1162  return res;
1163 }
1164 
1165 /*! \brief Queue a hangup frame for channel */
1167 {
1168  RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
1169  struct ast_frame f = { AST_FRAME_CONTROL, .subclass.integer = AST_CONTROL_HANGUP };
1170  int res;
1171 
1172  if (cause >= 0) {
1173  f.data.uint32 = cause;
1174  }
1175  ast_log(LOG_WARNING, "Queuing hangup with cause: %d !!!\n", cause);
1176  /* Yeah, let's not change a lock-critical value without locking */
1177  ast_channel_lock(chan);
1179  if (cause < 0) {
1181  }
1182  blob = ast_json_pack("{s: i}",
1183  "cause", cause);
1185 
1186  res = ast_queue_frame(chan, &f);
1187  ast_channel_unlock(chan);
1188  return res;
1189 }
1190 
1191 int ast_queue_hold(struct ast_channel *chan, const char *musicclass)
1192 {
1193  struct ast_frame f = { AST_FRAME_CONTROL, .subclass.integer = AST_CONTROL_HOLD };
1194  struct ast_json *blob = NULL;
1195  int res;
1196 
1197  if (!ast_strlen_zero(musicclass)) {
1198  f.data.ptr = (void *) musicclass;
1199  f.datalen = strlen(musicclass) + 1;
1200 
1201  blob = ast_json_pack("{s: s}",
1202  "musicclass", musicclass);
1203  }
1204 
1205  ast_channel_lock(chan);
1207  ast_channel_unlock(chan);
1208 
1209  res = ast_queue_frame(chan, &f);
1210 
1211  ast_json_unref(blob);
1212 
1213  return res;
1214 }
1215 
1217 {
1218  struct ast_frame f = { AST_FRAME_CONTROL, .subclass.integer = AST_CONTROL_UNHOLD };
1219  int res;
1220 
1221  ast_channel_lock(chan);
1223  ast_channel_unlock(chan);
1224 
1225  res = ast_queue_frame(chan, &f);
1226 
1227  return res;
1228 }
1229 
1230 /*! \brief Queue a control frame */
1232 {
1233  struct ast_frame f = { AST_FRAME_CONTROL, .subclass.integer = control };
1234  return ast_queue_frame(chan, &f);
1235 }
1236 
1237 /*! \brief Queue a control frame with payload */
1239  const void *data, size_t datalen)
1240 {
1241  struct ast_frame f = { AST_FRAME_CONTROL, .subclass.integer = control, .data.ptr = (void *) data, .datalen = datalen };
1242  return ast_queue_frame(chan, &f);
1243 }
1244 
1245 /*! \brief Queue an ANSWER control frame with topology */
1246 int ast_queue_answer(struct ast_channel *chan, const struct ast_stream_topology *topology)
1247 {
1248  struct ast_frame f = {
1250  .subclass.integer = AST_CONTROL_ANSWER,
1251  .subclass.topology = (struct ast_stream_topology *)topology,
1252  };
1253  return ast_queue_frame(chan, &f);
1254 }
1255 
1256 /*! \brief Set defer DTMF flag on channel */
1258 {
1259  int pre = 0;
1260 
1261  if (chan) {
1262  ast_channel_lock(chan);
1265  ast_channel_unlock(chan);
1266  }
1267  return pre;
1268 }
1269 
1270 /*! \brief Unset defer DTMF flag on channel */
1272 {
1273  if (chan) {
1275  }
1276 }
1277 
1279  void *data, int ao2_flags)
1280 {
1281  return ao2_callback_data(channels, ao2_flags, cb_fn, arg, data);
1282 }
1283 
1284 static int ast_channel_by_name_cb(void *obj, void *arg, void *data, int flags)
1285 {
1286  struct ast_channel *chan = obj;
1287  const char *name = arg;
1288  size_t name_len = *(size_t *) data;
1289  int ret = CMP_MATCH;
1290 
1291  if (ast_strlen_zero(name)) {
1292  ast_log(LOG_ERROR, "BUG! Must supply a channel name or partial name to match!\n");
1293  return CMP_STOP;
1294  }
1295 
1296  ast_channel_lock(chan);
1297  if ((!name_len && strcasecmp(ast_channel_name(chan), name))
1298  || (name_len && strncasecmp(ast_channel_name(chan), name, name_len))) {
1299  ret = 0; /* name match failed, keep looking */
1300  }
1301  ast_channel_unlock(chan);
1302 
1303  return ret;
1304 }
1305 
1306 static int ast_channel_by_exten_cb(void *obj, void *arg, void *data, int flags)
1307 {
1308  struct ast_channel *chan = obj;
1309  char *context = arg;
1310  char *exten = data;
1311  int ret = CMP_MATCH;
1312 
1313  if (ast_strlen_zero(exten) || ast_strlen_zero(context)) {
1314  ast_log(LOG_ERROR, "BUG! Must have a context and extension to match!\n");
1315  return CMP_STOP;
1316  }
1317 
1318  ast_channel_lock(chan);
1319  if (strcasecmp(ast_channel_context(chan), context) && strcasecmp(ast_channel_macrocontext(chan), context)) {
1320  ret = 0; /* Context match failed, continue */
1321  } else if (strcasecmp(ast_channel_exten(chan), exten) && strcasecmp(ast_channel_macroexten(chan), exten)) {
1322  ret = 0; /* Extension match failed, continue */
1323  }
1324  ast_channel_unlock(chan);
1325 
1326  return ret;
1327 }
1328 
1329 static int ast_channel_by_uniqueid_cb(void *obj, void *arg, void *data, int flags)
1330 {
1331  struct ast_channel *chan = obj;
1332  char *uniqueid = arg;
1333  size_t id_len = *(size_t *) data;
1334  int ret = CMP_MATCH;
1335 
1336  if (ast_strlen_zero(uniqueid)) {
1337  ast_log(LOG_ERROR, "BUG! Must supply a uniqueid or partial uniqueid to match!\n");
1338  return CMP_STOP;
1339  }
1340 
1341  ast_channel_lock(chan);
1342  if ((!id_len && strcasecmp(ast_channel_uniqueid(chan), uniqueid))
1343  || (id_len && strncasecmp(ast_channel_uniqueid(chan), uniqueid, id_len))) {
1344  ret = 0; /* uniqueid match failed, keep looking */
1345  }
1346  ast_channel_unlock(chan);
1347 
1348  return ret;
1349 }
1350 
1352  /* storage for non-dynamically allocated iterator */
1353  struct ao2_iterator simple_iterator;
1354  /* pointer to the actual iterator (simple_iterator or a dynamically
1355  * allocated iterator)
1356  */
1358 };
1359 
1361 {
1363  ast_free(i);
1364 
1365  return NULL;
1366 }
1367 
1369 {
1370  struct ast_channel_iterator *i;
1371  char *l_exten = (char *) exten;
1372  char *l_context = (char *) context;
1373 
1374  if (!(i = ast_calloc(1, sizeof(*i)))) {
1375  return NULL;
1376  }
1377 
1379  l_context, l_exten, OBJ_MULTIPLE);
1380  if (!i->active_iterator) {
1381  ast_free(i);
1382  return NULL;
1383  }
1384 
1385  return i;
1386 }
1387 
1388 struct ast_channel_iterator *ast_channel_iterator_by_name_new(const char *name, size_t name_len)
1389 {
1390  struct ast_channel_iterator *i;
1391  char *l_name = (char *) name;
1392 
1393  if (!(i = ast_calloc(1, sizeof(*i)))) {
1394  return NULL;
1395  }
1396 
1398  l_name, &name_len,
1399  OBJ_MULTIPLE | (name_len == 0 /* match the whole word, so optimize */ ? OBJ_KEY : 0));
1400  if (!i->active_iterator) {
1401  ast_free(i);
1402  return NULL;
1403  }
1404 
1405  return i;
1406 }
1407 
1409 {
1410  struct ast_channel_iterator *i;
1411 
1412  if (!(i = ast_calloc(1, sizeof(*i)))) {
1413  return NULL;
1414  }
1415 
1416  i->simple_iterator = ao2_iterator_init(channels, 0);
1418 
1419  return i;
1420 }
1421 
1423 {
1425 }
1426 
1427 /* Legacy function, not currently used for lookups, but we need a cmp_fn */
1428 static int ast_channel_cmp_cb(void *obj, void *arg, int flags)
1429 {
1430  ast_log(LOG_ERROR, "BUG! Should never be called!\n");
1431  return CMP_STOP;
1432 }
1433 
1434 struct ast_channel *ast_channel_get_by_name_prefix(const char *name, size_t name_len)
1435 {
1436  struct ast_channel *chan;
1437  char *l_name = (char *) name;
1438 
1439  chan = ast_channel_callback(ast_channel_by_name_cb, l_name, &name_len,
1440  (name_len == 0) /* optimize if it is a complete name match */ ? OBJ_KEY : 0);
1441  if (chan) {
1442  return chan;
1443  }
1444 
1445  if (ast_strlen_zero(l_name)) {
1446  /* We didn't have a name to search for so quit. */
1447  return NULL;
1448  }
1449 
1450  /* Now try a search for uniqueid. */
1451  return ast_channel_callback(ast_channel_by_uniqueid_cb, l_name, &name_len, 0);
1452 }
1453 
1455 {
1456  return ast_channel_get_by_name_prefix(name, 0);
1457 }
1458 
1459 struct ast_channel *ast_channel_get_by_exten(const char *exten, const char *context)
1460 {
1461  char *l_exten = (char *) exten;
1462  char *l_context = (char *) context;
1463 
1464  return ast_channel_callback(ast_channel_by_exten_cb, l_context, l_exten, 0);
1465 }
1466 
1467 int ast_is_deferrable_frame(const struct ast_frame *frame)
1468 {
1469  /* Do not add a default entry in this switch statement. Each new
1470  * frame type should be addressed directly as to whether it should
1471  * be queued up or not.
1472  */
1473  switch (frame->frametype) {
1476  case AST_FRAME_CONTROL:
1477  case AST_FRAME_TEXT:
1478  case AST_FRAME_TEXT_DATA:
1479  case AST_FRAME_IMAGE:
1480  case AST_FRAME_HTML:
1481  return 1;
1482 
1483  case AST_FRAME_DTMF_END:
1484  case AST_FRAME_DTMF_BEGIN:
1485  case AST_FRAME_VOICE:
1486  case AST_FRAME_VIDEO:
1487  case AST_FRAME_NULL:
1488  case AST_FRAME_IAX:
1489  case AST_FRAME_CNG:
1490  case AST_FRAME_MODEM:
1491  case AST_FRAME_RTCP:
1492  return 0;
1493  }
1494  return 0;
1495 }
1496 
1497 /*! \brief Wait, look for hangups and condition arg */
1498 static int safe_sleep_conditional(struct ast_channel *chan, int timeout_ms, int (*cond)(void*), void *data, unsigned int generate_silence)
1499 {
1500  struct ast_frame *f;
1501  struct ast_silence_generator *silgen = NULL;
1502  int res = 0;
1503  struct timeval start;
1504  int ms;
1505  AST_LIST_HEAD_NOLOCK(, ast_frame) deferred_frames;
1506 
1507  AST_LIST_HEAD_INIT_NOLOCK(&deferred_frames);
1508 
1509  /* If no other generator is present, start silencegen while waiting */
1510  if (generate_silence && ast_opt_transmit_silence && !ast_channel_generatordata(chan)) {
1511  silgen = ast_channel_start_silence_generator(chan);
1512  }
1513 
1514  start = ast_tvnow();
1515  while ((ms = ast_remaining_ms(start, timeout_ms))) {
1516  struct ast_frame *dup_f = NULL;
1517 
1518  if (cond && ((*cond)(data) == 0)) {
1519  break;
1520  }
1521  ms = ast_waitfor(chan, ms);
1522  if (ms < 0) {
1523  res = -1;
1524  break;
1525  }
1526  if (ms > 0) {
1527  f = ast_read(chan);
1528  if (!f) {
1529  res = -1;
1530  break;
1531  }
1532 
1533  if (!ast_is_deferrable_frame(f)) {
1534  ast_frfree(f);
1535  continue;
1536  }
1537 
1538  if ((dup_f = ast_frisolate(f))) {
1539  if (dup_f != f) {
1540  ast_frfree(f);
1541  }
1542  AST_LIST_INSERT_HEAD(&deferred_frames, dup_f, frame_list);
1543  }
1544  }
1545  }
1546 
1547  /* stop silgen if present */
1548  if (silgen) {
1549  ast_channel_stop_silence_generator(chan, silgen);
1550  }
1551 
1552  /* We need to free all the deferred frames, but we only need to
1553  * queue the deferred frames if there was no error and no
1554  * hangup was received
1555  */
1556  ast_channel_lock(chan);
1557  while ((f = AST_LIST_REMOVE_HEAD(&deferred_frames, frame_list))) {
1558  if (!res) {
1559  ast_queue_frame_head(chan, f);
1560  }
1561  ast_frfree(f);
1562  }
1563  ast_channel_unlock(chan);
1564 
1565  return res;
1566 }
1567 
1568 int ast_safe_sleep_conditional(struct ast_channel *chan, int timeout_ms, int (*cond)(void*), void *data)
1569 {
1570  return safe_sleep_conditional(chan, timeout_ms, cond, data, 1);
1571 }
1572 
1573 /*! \brief Wait, look for hangups */
1574 int ast_safe_sleep(struct ast_channel *chan, int ms)
1575 {
1576  return safe_sleep_conditional(chan, ms, NULL, NULL, 1);
1577 }
1578 
1580 {
1581  return safe_sleep_conditional(chan, ms, NULL, NULL, 0);
1582 }
1583 
1585 {
1586  /* Safe, even if already unlinked. */
1587  ao2_unlink(channels, chan);
1588  return ast_channel_unref(chan);
1589 }
1590 
1592 {
1593  init->str = NULL;
1596  init->valid = 0;
1597 }
1598 
1599 void ast_party_name_copy(struct ast_party_name *dest, const struct ast_party_name *src)
1600 {
1601  if (dest == src) {
1602  /* Don't copy to self */
1603  return;
1604  }
1605 
1606  ast_free(dest->str);
1607  dest->str = ast_strdup(src->str);
1608  dest->char_set = src->char_set;
1609  dest->presentation = src->presentation;
1610  dest->valid = src->valid;
1611 }
1612 
1613 void ast_party_name_set_init(struct ast_party_name *init, const struct ast_party_name *guide)
1614 {
1615  init->str = NULL;
1616  init->char_set = guide->char_set;
1617  init->presentation = guide->presentation;
1618  init->valid = guide->valid;
1619 }
1620 
1621 void ast_party_name_set(struct ast_party_name *dest, const struct ast_party_name *src)
1622 {
1623  if (dest == src) {
1624  /* Don't set to self */
1625  return;
1626  }
1627 
1628  if (src->str && src->str != dest->str) {
1629  ast_free(dest->str);
1630  dest->str = ast_strdup(src->str);
1631  }
1632 
1633  dest->char_set = src->char_set;
1634  dest->presentation = src->presentation;
1635  dest->valid = src->valid;
1636 }
1637 
1639 {
1640  ast_free(doomed->str);
1641  doomed->str = NULL;
1642 }
1643 
1645 {
1646  init->str = NULL;
1647  init->plan = 0;/* Unknown */
1649  init->valid = 0;
1650 }
1651 
1652 void ast_party_number_copy(struct ast_party_number *dest, const struct ast_party_number *src)
1653 {
1654  if (dest == src) {
1655  /* Don't copy to self */
1656  return;
1657  }
1658 
1659  ast_free(dest->str);
1660  dest->str = ast_strdup(src->str);
1661  dest->plan = src->plan;
1662  dest->presentation = src->presentation;
1663  dest->valid = src->valid;
1664 }
1665 
1666 void ast_party_number_set_init(struct ast_party_number *init, const struct ast_party_number *guide)
1667 {
1668  init->str = NULL;
1669  init->plan = guide->plan;
1670  init->presentation = guide->presentation;
1671  init->valid = guide->valid;
1672 }
1673 
1674 void ast_party_number_set(struct ast_party_number *dest, const struct ast_party_number *src)
1675 {
1676  if (dest == src) {
1677  /* Don't set to self */
1678  return;
1679  }
1680 
1681  if (src->str && src->str != dest->str) {
1682  ast_free(dest->str);
1683  dest->str = ast_strdup(src->str);
1684  }
1685 
1686  dest->plan = src->plan;
1687  dest->presentation = src->presentation;
1688  dest->valid = src->valid;
1689 }
1690 
1692 {
1693  ast_free(doomed->str);
1694  doomed->str = NULL;
1695 }
1696 
1698 {
1699  init->str = NULL;
1700  init->type = 0;
1701  init->odd_even_indicator = 0;
1702  init->valid = 0;
1703 }
1704 
1706 {
1707  if (dest == src) {
1708  /* Don't copy to self */
1709  return;
1710  }
1711 
1712  ast_free(dest->str);
1713  dest->str = ast_strdup(src->str);
1714  dest->type = src->type;
1716  dest->valid = src->valid;
1717 }
1718 
1720 {
1721  init->str = NULL;
1722  init->type = guide->type;
1723  init->odd_even_indicator = guide->odd_even_indicator;
1724  init->valid = guide->valid;
1725 }
1726 
1728 {
1729  if (dest == src) {
1730  /* Don't set to self */
1731  return;
1732  }
1733 
1734  if (src->str && src->str != dest->str) {
1735  ast_free(dest->str);
1736  dest->str = ast_strdup(src->str);
1737  }
1738 
1739  dest->type = src->type;
1741  dest->valid = src->valid;
1742 }
1743 
1745 {
1746  ast_free(doomed->str);
1747  doomed->str = NULL;
1748 }
1749 
1751 {
1752  update_id->name = 1;
1753  update_id->number = 1;
1754  update_id->subaddress = 1;
1755 }
1756 
1758 {
1759  ast_party_name_init(&init->name);
1760  ast_party_number_init(&init->number);
1762  init->tag = NULL;
1763 }
1764 
1765 void ast_party_id_copy(struct ast_party_id *dest, const struct ast_party_id *src)
1766 {
1767  if (dest == src) {
1768  /* Don't copy to self */
1769  return;
1770  }
1771 
1772  ast_party_name_copy(&dest->name, &src->name);
1773  ast_party_number_copy(&dest->number, &src->number);
1775 
1776  ast_free(dest->tag);
1777  dest->tag = ast_strdup(src->tag);
1778 }
1779 
1780 void ast_party_id_set_init(struct ast_party_id *init, const struct ast_party_id *guide)
1781 {
1782  ast_party_name_set_init(&init->name, &guide->name);
1783  ast_party_number_set_init(&init->number, &guide->number);
1785  init->tag = NULL;
1786 }
1787 
1788 void ast_party_id_set(struct ast_party_id *dest, const struct ast_party_id *src, const struct ast_set_party_id *update)
1789 {
1790  if (dest == src) {
1791  /* Don't set to self */
1792  return;
1793  }
1794 
1795  if (!update || update->name) {
1796  ast_party_name_set(&dest->name, &src->name);
1797  }
1798  if (!update || update->number) {
1799  ast_party_number_set(&dest->number, &src->number);
1800  }
1801  if (!update || update->subaddress) {
1803  }
1804 
1805  if (src->tag && src->tag != dest->tag) {
1806  ast_free(dest->tag);
1807  dest->tag = ast_strdup(src->tag);
1808  }
1809 }
1810 
1811 void ast_party_id_free(struct ast_party_id *doomed)
1812 {
1813  ast_party_name_free(&doomed->name);
1814  ast_party_number_free(&doomed->number);
1816 
1817  ast_free(doomed->tag);
1818  doomed->tag = NULL;
1819 }
1820 
1822 {
1823  int number_priority;
1824  int number_value;
1825  int number_screening;
1826  int name_priority;
1827  int name_value;
1828 
1829  /* Determine name presentation priority. */
1830  if (!id->name.valid) {
1831  name_value = AST_PRES_UNAVAILABLE;
1832  name_priority = 3;
1833  } else {
1834  name_value = id->name.presentation & AST_PRES_RESTRICTION;
1835  switch (name_value) {
1836  case AST_PRES_RESTRICTED:
1837  name_priority = 0;
1838  break;
1839  case AST_PRES_ALLOWED:
1840  name_priority = 1;
1841  break;
1842  case AST_PRES_UNAVAILABLE:
1843  name_priority = 2;
1844  break;
1845  default:
1846  name_value = AST_PRES_UNAVAILABLE;
1847  name_priority = 3;
1848  break;
1849  }
1850  }
1851 
1852  /* Determine number presentation priority. */
1853  if (!id->number.valid) {
1854  number_screening = AST_PRES_USER_NUMBER_UNSCREENED;
1855  number_value = AST_PRES_UNAVAILABLE;
1856  number_priority = 3;
1857  } else {
1858  number_screening = id->number.presentation & AST_PRES_NUMBER_TYPE;
1859  number_value = id->number.presentation & AST_PRES_RESTRICTION;
1860  switch (number_value) {
1861  case AST_PRES_RESTRICTED:
1862  number_priority = 0;
1863  break;
1864  case AST_PRES_ALLOWED:
1865  number_priority = 1;
1866  break;
1867  case AST_PRES_UNAVAILABLE:
1868  number_priority = 2;
1869  break;
1870  default:
1871  number_screening = AST_PRES_USER_NUMBER_UNSCREENED;
1872  number_value = AST_PRES_UNAVAILABLE;
1873  number_priority = 3;
1874  break;
1875  }
1876  }
1877 
1878  /* Select the wining presentation value. */
1879  if (name_priority < number_priority) {
1880  number_value = name_value;
1881  }
1882  if (number_value == AST_PRES_UNAVAILABLE) {
1884  }
1885 
1886  return number_value | number_screening;
1887 }
1888 
1890 {
1891  id->name.valid = 0;
1892  id->number.valid = 0;
1893  id->subaddress.valid = 0;
1894 }
1895 
1897 {
1898  ast_party_id_free(id);
1899  ast_party_id_init(id);
1900 }
1901 
1902 struct ast_party_id ast_party_id_merge(struct ast_party_id *base, struct ast_party_id *overlay)
1903 {
1904  struct ast_party_id merged;
1905 
1906  merged = *base;
1907  if (overlay->name.valid) {
1908  merged.name = overlay->name;
1909  }
1910  if (overlay->number.valid) {
1911  merged.number = overlay->number;
1912  }
1913  if (overlay->subaddress.valid) {
1914  merged.subaddress = overlay->subaddress;
1915  }
1916  /* Note the actual structure is returned and not a pointer to it! */
1917  return merged;
1918 }
1919 
1920 void ast_party_id_merge_copy(struct ast_party_id *dest, struct ast_party_id *base, struct ast_party_id *overlay)
1921 {
1922  struct ast_party_id merged;
1923 
1924  merged = ast_party_id_merge(base, overlay);
1925  ast_party_id_copy(dest, &merged);
1926 }
1927 
1929 {
1930  init->number.str = NULL;
1931  init->number.plan = 0;/* Unknown */
1933  init->transit_network_select = 0;
1934 }
1935 
1936 void ast_party_dialed_copy(struct ast_party_dialed *dest, const struct ast_party_dialed *src)
1937 {
1938  if (dest == src) {
1939  /* Don't copy to self */
1940  return;
1941  }
1942 
1943  ast_free(dest->number.str);
1944  dest->number.str = ast_strdup(src->number.str);
1945  dest->number.plan = src->number.plan;
1948 }
1949 
1950 void ast_party_dialed_set_init(struct ast_party_dialed *init, const struct ast_party_dialed *guide)
1951 {
1952  init->number.str = NULL;
1953  init->number.plan = guide->number.plan;
1956 }
1957 
1958 void ast_party_dialed_set(struct ast_party_dialed *dest, const struct ast_party_dialed *src)
1959 {
1960  if (src->number.str && src->number.str != dest->number.str) {
1961  ast_free(dest->number.str);
1962  dest->number.str = ast_strdup(src->number.str);
1963  }
1964  dest->number.plan = src->number.plan;
1965 
1967 
1969 }
1970 
1972 {
1973  ast_free(doomed->number.str);
1974  doomed->number.str = NULL;
1976 }
1977 
1979 {
1980  ast_party_id_init(&init->id);
1981  ast_party_id_init(&init->ani);
1982  ast_party_id_init(&init->priv);
1983  init->ani2 = 0;
1984 }
1985 
1986 void ast_party_caller_copy(struct ast_party_caller *dest, const struct ast_party_caller *src)
1987 {
1988  if (dest == src) {
1989  /* Don't copy to self */
1990  return;
1991  }
1992 
1993  ast_party_id_copy(&dest->id, &src->id);
1994  ast_party_id_copy(&dest->ani, &src->ani);
1995  ast_party_id_copy(&dest->priv, &src->priv);
1996  dest->ani2 = src->ani2;
1997 }
1998 
1999 void ast_party_caller_set_init(struct ast_party_caller *init, const struct ast_party_caller *guide)
2000 {
2001  ast_party_id_set_init(&init->id, &guide->id);
2002  ast_party_id_set_init(&init->ani, &guide->ani);
2003  ast_party_id_set_init(&init->priv, &guide->priv);
2004  init->ani2 = guide->ani2;
2005 }
2006 
2007 void ast_party_caller_set(struct ast_party_caller *dest, const struct ast_party_caller *src, const struct ast_set_party_caller *update)
2008 {
2009  ast_party_id_set(&dest->id, &src->id, update ? &update->id : NULL);
2010  ast_party_id_set(&dest->ani, &src->ani, update ? &update->ani : NULL);
2011  ast_party_id_set(&dest->priv, &src->priv, update ? &update->priv : NULL);
2012  dest->ani2 = src->ani2;
2013 }
2014 
2016 {
2017  ast_party_id_free(&doomed->id);
2018  ast_party_id_free(&doomed->ani);
2019  ast_party_id_free(&doomed->priv);
2020 }
2021 
2023 {
2024  ast_party_id_init(&init->id);
2025  ast_party_id_init(&init->ani);
2026  ast_party_id_init(&init->priv);
2027  init->ani2 = 0;
2029 }
2030 
2032 {
2033  if (dest == src) {
2034  /* Don't copy to self */
2035  return;
2036  }
2037 
2038  ast_party_id_copy(&dest->id, &src->id);
2039  ast_party_id_copy(&dest->ani, &src->ani);
2040  ast_party_id_copy(&dest->priv, &src->priv);
2041  dest->ani2 = src->ani2;
2042  dest->source = src->source;
2043 }
2044 
2046 {
2047  ast_party_id_set_init(&init->id, &guide->id);
2048  ast_party_id_set_init(&init->ani, &guide->ani);
2049  ast_party_id_set_init(&init->priv, &guide->priv);
2050  init->ani2 = guide->ani2;
2051  init->source = guide->source;
2052 }
2053 
2055 {
2056  ast_party_id_set(&dest->id, &src->id, update ? &update->id : NULL);
2057  ast_party_id_set(&dest->ani, &src->ani, update ? &update->ani : NULL);
2058  ast_party_id_set(&dest->priv, &src->priv, update ? &update->priv : NULL);
2059  dest->ani2 = src->ani2;
2060  dest->source = src->source;
2061 }
2062 
2064 {
2065  connected->id = caller->id;
2066  connected->ani = caller->ani;
2067  connected->priv = caller->priv;
2068  connected->ani2 = caller->ani2;
2070 }
2071 
2073 {
2074  ast_party_id_free(&doomed->id);
2075  ast_party_id_free(&doomed->ani);
2076  ast_party_id_free(&doomed->priv);
2077 }
2078 
2080 {
2081  init->str = NULL;
2083 }
2084 
2086 {
2087  if (dest == src) {
2088  return;
2089  }
2090 
2091  ast_free(dest->str);
2092  dest->str = ast_strdup(src->str);
2093  dest->code = src->code;
2094 }
2095 
2097 {
2098  init->str = NULL;
2099  init->code = guide->code;
2100 }
2101 
2103 {
2104  if (dest == src) {
2105  return;
2106  }
2107 
2108  if (src->str && src->str != dest->str) {
2109  ast_free(dest->str);
2110  dest->str = ast_strdup(src->str);
2111  }
2112 
2113  dest->code = src->code;
2114 }
2115 
2117 {
2118  ast_free(doomed->str);
2119 }
2120 
2121 
2123 {
2124  ast_party_id_init(&init->orig);
2125  ast_party_id_init(&init->from);
2126  ast_party_id_init(&init->to);
2127  ast_party_id_init(&init->priv_orig);
2128  ast_party_id_init(&init->priv_from);
2129  ast_party_id_init(&init->priv_to);
2132  init->count = 0;
2133 }
2134 
2136 {
2137  if (dest == src) {
2138  /* Don't copy to self */
2139  return;
2140  }
2141 
2142  ast_party_id_copy(&dest->orig, &src->orig);
2143  ast_party_id_copy(&dest->from, &src->from);
2144  ast_party_id_copy(&dest->to, &src->to);
2145  ast_party_id_copy(&dest->priv_orig, &src->priv_orig);
2146  ast_party_id_copy(&dest->priv_from, &src->priv_from);
2147  ast_party_id_copy(&dest->priv_to, &src->priv_to);
2150  dest->count = src->count;
2151 }
2152 
2154 {
2155  ast_party_id_set_init(&init->orig, &guide->orig);
2156  ast_party_id_set_init(&init->from, &guide->from);
2157  ast_party_id_set_init(&init->to, &guide->to);
2158  ast_party_id_set_init(&init->priv_orig, &guide->priv_orig);
2159  ast_party_id_set_init(&init->priv_from, &guide->priv_from);
2160  ast_party_id_set_init(&init->priv_to, &guide->priv_to);
2163  init->count = guide->count;
2164 }
2165 
2167 {
2168  ast_party_id_set(&dest->orig, &src->orig, update ? &update->orig : NULL);
2169  ast_party_id_set(&dest->from, &src->from, update ? &update->from : NULL);
2170  ast_party_id_set(&dest->to, &src->to, update ? &update->to : NULL);
2171  ast_party_id_set(&dest->priv_orig, &src->priv_orig, update ? &update->priv_orig : NULL);
2172  ast_party_id_set(&dest->priv_from, &src->priv_from, update ? &update->priv_from : NULL);
2173  ast_party_id_set(&dest->priv_to, &src->priv_to, update ? &update->priv_to : NULL);
2176  dest->count = src->count;
2177 }
2178 
2180 {
2181  ast_party_id_free(&doomed->orig);
2182  ast_party_id_free(&doomed->from);
2183  ast_party_id_free(&doomed->to);
2184  ast_party_id_free(&doomed->priv_orig);
2185  ast_party_id_free(&doomed->priv_from);
2186  ast_party_id_free(&doomed->priv_to);
2189 }
2190 
2191 /*! \brief Free a channel structure */
2192 static void ast_channel_destructor(void *obj)
2193 {
2194  struct ast_channel *chan = obj;
2195  struct ast_var_t *vardata;
2196  struct ast_frame *f;
2197  struct varshead *headp;
2198  struct ast_datastore *datastore;
2199  char device_name[AST_CHANNEL_NAME];
2200  ast_callid callid;
2201 
2202  ast_debug(1, "Channel %p '%s' destroying\n", chan, ast_channel_name(chan));
2203 
2204  /* Stop monitoring */
2205  if (ast_channel_monitor(chan)) {
2206  ast_channel_monitor(chan)->stop(chan, 0);
2207  }
2208 
2209  /* If there is native format music-on-hold state, free it */
2210  if (ast_channel_music_state(chan)) {
2211  ast_moh_cleanup(chan);
2212  }
2213 
2215 
2216  /* Things that may possibly raise Stasis messages shouldn't occur after this point */
2218 
2220  /* A channel snapshot should not be in the process of being staged now. */
2222 
2223  ast_channel_lock(chan);
2225  ast_channel_unlock(chan);
2226  }
2227 
2228  ast_channel_lock(chan);
2229 
2230  /* Get rid of each of the data stores on the channel */
2231  while ((datastore = AST_LIST_REMOVE_HEAD(ast_channel_datastores(chan), entry)))
2232  /* Free the data store */
2233  ast_datastore_free(datastore);
2234 
2235  /* While the channel is locked, take the reference to its callid while we tear down the call. */
2236  callid = ast_channel_callid(chan);
2238 
2239  ast_channel_unlock(chan);
2240 
2241  /* Lock and unlock the channel just to be sure nobody has it locked still
2242  due to a reference that was stored in a datastore. (i.e. app_chanspy) */
2243  ast_channel_lock(chan);
2244  ast_channel_unlock(chan);
2245 
2246  if (ast_channel_tech_pvt(chan)) {
2247  ast_log_callid(LOG_WARNING, callid, "Channel '%s' may not have been hung up properly\n", ast_channel_name(chan));
2249  }
2250 
2251  if (ast_channel_sched(chan)) {
2253  }
2254 
2256  char *dashptr;
2257 
2258  ast_copy_string(device_name, ast_channel_name(chan), sizeof(device_name));
2259  if ((dashptr = strrchr(device_name, '-'))) {
2260  *dashptr = '\0';
2261  }
2262  } else {
2263  device_name[0] = '\0';
2264  }
2265 
2266  /* Free translators */
2267  if (ast_channel_readtrans(chan))
2269  if (ast_channel_writetrans(chan))
2271  if (ast_channel_pbx(chan))
2272  ast_log_callid(LOG_WARNING, callid, "PBX may not have been terminated properly on '%s'\n", ast_channel_name(chan));
2273 
2274  /* Free formats */
2280 
2286 
2287  /* Close pipes if appropriate */
2289  if (ast_channel_timer(chan)) {
2291  ast_channel_timer_set(chan, NULL);
2292  }
2293  while ((f = AST_LIST_REMOVE_HEAD(ast_channel_readq(chan), frame_list)))
2294  ast_frfree(f);
2295 
2296  /* loop over the variables list, freeing all data and deleting list items */
2297  /* no need to lock the list, as the channel is already locked */
2298  headp = ast_channel_varshead(chan);
2299  while ((vardata = AST_LIST_REMOVE_HEAD(headp, entries)))
2300  ast_var_delete(vardata);
2301 
2302  ast_app_group_discard(chan);
2303 
2304  /* Destroy the jitterbuffer */
2305  ast_jb_destroy(chan);
2306 
2307  if (ast_channel_cdr(chan)) {
2309  ast_channel_cdr_set(chan, NULL);
2310  }
2311 
2312  if (ast_channel_zone(chan)) {
2314  }
2315 
2317 
2318  if (device_name[0]) {
2319  /*
2320  * We have a device name to notify of a new state.
2321  *
2322  * Queue an unknown state, because, while we know that this particular
2323  * instance is dead, we don't know the state of all other possible
2324  * instances.
2325  */
2327  }
2328 
2330 
2333 
2335 }
2336 
2337 /*! \brief Free a dummy channel structure */
2338 static void ast_dummy_channel_destructor(void *obj)
2339 {
2340  struct ast_channel *chan = obj;
2341  struct ast_datastore *datastore;
2342  struct ast_var_t *vardata;
2343  struct varshead *headp;
2344 
2346 
2347  /* Get rid of each of the data stores on the channel */
2348  while ((datastore = AST_LIST_REMOVE_HEAD(ast_channel_datastores(chan), entry))) {
2349  /* Free the data store */
2350  ast_datastore_free(datastore);
2351  }
2352 
2358 
2359  /* loop over the variables list, freeing all data and deleting list items */
2360  /* no need to lock the list, as the channel is already locked */
2361  headp = ast_channel_varshead(chan);
2362  while ((vardata = AST_LIST_REMOVE_HEAD(headp, entries)))
2363  ast_var_delete(vardata);
2364 
2365  if (ast_channel_cdr(chan)) {
2367  ast_channel_cdr_set(chan, NULL);
2368  }
2369 
2371 }
2372 
2374 {
2375  struct ast_datastore *datastore = NULL, *datastore2;
2376 
2377  AST_LIST_TRAVERSE(ast_channel_datastores(from), datastore, entry) {
2378  if (datastore->inheritance > 0) {
2379  datastore2 = ast_datastore_alloc(datastore->info, datastore->uid);
2380  if (datastore2) {
2381  datastore2->data = datastore->info->duplicate ? datastore->info->duplicate(datastore->data) : NULL;
2382  datastore2->inheritance = datastore->inheritance == DATASTORE_INHERIT_FOREVER ? DATASTORE_INHERIT_FOREVER : datastore->inheritance - 1;
2384  }
2385  }
2386  }
2387  return 0;
2388 }
2389 
2390 int ast_channel_datastore_add(struct ast_channel *chan, struct ast_datastore *datastore)
2391 {
2392  int res = 0;
2393 
2395 
2396  return res;
2397 }
2398 
2399 int ast_channel_datastore_remove(struct ast_channel *chan, struct ast_datastore *datastore)
2400 {
2401  return AST_LIST_REMOVE(ast_channel_datastores(chan), datastore, entry) ? 0 : -1;
2402 }
2403 
2404 struct ast_datastore *ast_channel_datastore_find(struct ast_channel *chan, const struct ast_datastore_info *info, const char *uid)
2405 {
2406  struct ast_datastore *datastore = NULL;
2407 
2408  if (info == NULL)
2409  return NULL;
2410 
2411  AST_LIST_TRAVERSE(ast_channel_datastores(chan), datastore, entry) {
2412  if (datastore->info != info) {
2413  continue;
2414  }
2415 
2416  if (uid == NULL) {
2417  /* matched by type only */
2418  break;
2419  }
2420 
2421  if ((datastore->uid != NULL) && !strcasecmp(uid, datastore->uid)) {
2422  /* Matched by type AND uid */
2423  break;
2424  }
2425  }
2426 
2427  return datastore;
2428 }
2429 
2430 /*! Set the file descriptor on the channel */
2431 void ast_channel_set_fd(struct ast_channel *chan, int which, int fd)
2432 {
2433  ast_channel_internal_fd_set(chan, which, fd);
2434  return;
2435 }
2436 
2438 {
2439  ast_channel_lock(chan);
2440 
2442 
2444  struct ast_frame *fr;
2445 
2446  /* If we have completely cleared the softhangup flag,
2447  * then we need to fully abort the hangup process. This requires
2448  * pulling the END_OF_Q frame out of the channel frame queue if it
2449  * still happens to be there. */
2450 
2451  fr = AST_LIST_LAST(ast_channel_readq(chan));
2452  if (fr && fr->frametype == AST_FRAME_CONTROL &&
2455  ast_frfree(fr);
2456  }
2457  }
2458 
2459  ast_channel_unlock(chan);
2460 }
2461 
2462 /*! \brief Softly hangup a channel, don't lock */
2463 int ast_softhangup_nolock(struct ast_channel *chan, int cause)
2464 {
2465  ast_debug(1, "Soft-Hanging (%#04x) up channel '%s'\n", (unsigned)cause, ast_channel_name(chan));
2466  /* Inform channel driver that we need to be hung up, if it cares */
2469  /* Interrupt any poll call or such */
2471  pthread_kill(ast_channel_blocker(chan), SIGURG);
2472  return 0;
2473 }
2474 
2475 /*! \brief Softly hangup a channel, lock */
2476 int ast_softhangup(struct ast_channel *chan, int cause)
2477 {
2478  RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
2479  int res;
2480 
2481  ast_channel_lock(chan);
2482  res = ast_softhangup_nolock(chan, cause);
2483  blob = ast_json_pack("{s: i, s: b}",
2484  "cause", cause,
2485  "soft", 1);
2487  ast_channel_unlock(chan);
2488 
2489  return res;
2490 }
2491 
2492 static void free_translation(struct ast_channel *clonechan)
2493 {
2494  if (ast_channel_writetrans(clonechan)) {
2496  }
2497  if (ast_channel_readtrans(clonechan)) {
2499  }
2500  ast_channel_writetrans_set(clonechan, NULL);
2501  ast_channel_readtrans_set(clonechan, NULL);
2502 }
2503 
2504 void ast_set_hangupsource(struct ast_channel *chan, const char *source, int force)
2505 {
2507 
2508  ast_channel_lock(chan);
2509  if (force || ast_strlen_zero(ast_channel_hangupsource(chan))) {
2510  ast_channel_hangupsource_set(chan, source);
2511  }
2512  ast_channel_unlock(chan);
2513 
2514  if (bridge) {
2515  ast_channel_lock(bridge);
2516  if (force || ast_strlen_zero(ast_channel_hangupsource(bridge))) {
2517  ast_channel_hangupsource_set(bridge, source);
2518  }
2519  ast_channel_unlock(bridge);
2520  }
2521 }
2522 
2524 {
2525  return ast_channel_monitor(chan)
2528 }
2529 
2531 {
2532  return ast_channel_monitor(chan)
2535 }
2536 
2537 static void destroy_hooks(struct ast_channel *chan)
2538 {
2539  if (ast_channel_audiohooks(chan)) {
2542  }
2543 
2545 }
2546 
2547 /*! \brief Hangup a channel */
2548 void ast_hangup(struct ast_channel *chan)
2549 {
2550  /* Be NULL safe for RAII_VAR() usage. */
2551  if (!chan) {
2552  return;
2553  }
2554 
2555  ast_debug(1, "Channel %p '%s' hanging up. Refs: %d\n", chan, ast_channel_name(chan),
2556  ao2_ref(chan, 0));
2557 
2558  ast_autoservice_stop(chan);
2559 
2560  ast_channel_lock(chan);
2561 
2562  while (ast_channel_masq(chan) || ast_channel_masqr(chan)) {
2564  }
2565 
2566  /* Mark as a zombie so a masquerade cannot be setup on this channel. */
2568 
2569  ast_channel_unlock(chan);
2570 
2571  /*
2572  * XXX if running the hangup handlers here causes problems
2573  * because the handlers take too long to execute, we could move
2574  * the meat of this function into another thread. A thread
2575  * where channels go to die.
2576  *
2577  * If this is done, ast_autoservice_chan_hangup_peer() will no
2578  * longer be needed.
2579  */
2581  ao2_unlink(channels, chan);
2582  ast_channel_lock(chan);
2583 
2584  destroy_hooks(chan);
2585 
2586  free_translation(chan);
2587  /* Close audio stream */
2588  if (ast_channel_stream(chan)) {
2591  }
2592  /* Close video stream */
2593  if (ast_channel_vstream(chan)) {
2596  }
2597  if (ast_channel_sched(chan)) {
2599  ast_channel_sched_set(chan, NULL);
2600  }
2601 
2602  if (ast_channel_generatordata(chan)) { /* Clear any tone stuff remaining */
2603  if (ast_channel_generator(chan) && ast_channel_generator(chan)->release) {
2605  }
2606  }
2609 
2611  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",
2613  ast_channel_blockproc(chan));
2614  ast_assert(0);
2615  }
2616 
2617  if (ast_channel_tech(chan)->hangup) {
2618  ast_channel_tech(chan)->hangup(chan);
2619  }
2620 
2621  ast_channel_unlock(chan);
2622 
2623  ast_cc_offer(chan);
2624 
2625  ast_channel_unref(chan);
2626 }
2627 
2628 /*!
2629  * \internal
2630  * \brief Set channel answered time if not already set.
2631  * \since 13.11.0
2632  *
2633  * \param chan Channel to set answered time.
2634  *
2635  * \return Nothing
2636  */
2637 static void set_channel_answer_time(struct ast_channel *chan)
2638 {
2639  if (ast_tvzero(ast_channel_answertime(chan))) {
2640  struct timeval answertime;
2641 
2642  answertime = ast_tvnow();
2643  ast_channel_answertime_set(chan, &answertime);
2644  }
2645 }
2646 
2647 
2649 {
2650  int res = 0;
2651  SCOPE_TRACE(1, "%s\n", ast_channel_name(chan));
2652 
2653  ast_channel_lock(chan);
2654 
2655  /* You can't answer an outbound call */
2657  ast_channel_unlock(chan);
2658  return 0;
2659  }
2660 
2661  /* Stop if we're a zombie or need a soft hangup */
2663  ast_channel_unlock(chan);
2664  return -1;
2665  }
2666 
2667  /*
2668  * Mark when incoming channel answered so we can know how
2669  * long the channel has been up.
2670  */
2672 
2673  ast_channel_unlock(chan);
2674 
2675  switch (ast_channel_state(chan)) {
2676  case AST_STATE_RINGING:
2677  case AST_STATE_RING:
2678  ast_channel_lock(chan);
2679  if (ast_channel_tech(chan)->answer_with_stream_topology) {
2680  res = ast_channel_tech(chan)->answer_with_stream_topology(chan, topology);
2681 
2682  } else if (ast_channel_tech(chan)->answer) {
2683  res = ast_channel_tech(chan)->answer(chan);
2684  }
2685  ast_setstate(chan, AST_STATE_UP);
2686  ast_channel_unlock(chan);
2687  break;
2688  case AST_STATE_UP:
2689  break;
2690  default:
2691  break;
2692  }
2693 
2694  ast_indicate(chan, -1);
2695 
2696  return res;
2697 }
2698 
2699 int ast_raw_answer(struct ast_channel *chan)
2700 {
2702 }
2703 
2704 int __ast_answer(struct ast_channel *chan, unsigned int delay)
2705 {
2706  int res = 0;
2707  enum ast_channel_state old_state;
2708  SCOPE_TRACE(1, "%s\n", ast_channel_name(chan));
2709 
2710  old_state = ast_channel_state(chan);
2711  if ((res = ast_raw_answer(chan))) {
2712  return res;
2713  }
2714 
2715  switch (old_state) {
2716  case AST_STATE_RINGING:
2717  case AST_STATE_RING:
2718  /* wait for media to start flowing, but don't wait any longer
2719  * than 'delay' or 500 milliseconds, whichever is longer
2720  */
2721  do {
2723  struct ast_frame *cur;
2724  struct ast_frame *new_frame;
2725  int timeout_ms = MAX(delay, 500);
2726  unsigned int done = 0;
2727  struct timeval start;
2728 
2730 
2731  start = ast_tvnow();
2732  for (;;) {
2733  int ms = ast_remaining_ms(start, timeout_ms);
2734  ms = ast_waitfor(chan, ms);
2735  if (ms < 0) {
2736  ast_log(LOG_WARNING, "Error condition occurred when polling channel %s for a voice frame: %s\n", ast_channel_name(chan), strerror(errno));
2737  res = -1;
2738  break;
2739  }
2740  if (ms == 0) {
2741  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));
2742  break;
2743  }
2744  cur = ast_read(chan);
2745  if (!cur || ((cur->frametype == AST_FRAME_CONTROL) &&
2746  (cur->subclass.integer == AST_CONTROL_HANGUP))) {
2747  if (cur) {
2748  ast_frfree(cur);
2749  }
2750  res = -1;
2751  ast_debug(2, "Hangup of channel %s detected in answer routine\n", ast_channel_name(chan));
2752  break;
2753  }
2754 
2755  if ((new_frame = ast_frisolate(cur)) != cur) {
2756  ast_frfree(cur);
2757  }
2758 
2759  AST_LIST_INSERT_HEAD(&frames, new_frame, frame_list);
2760 
2761  /* if a specific delay period was requested, continue
2762  * until that delay has passed. don't stop just because
2763  * incoming media has arrived.
2764  */
2765  if (delay) {
2766  continue;
2767  }
2768 
2769  switch (new_frame->frametype) {
2770  /* all of these frametypes qualify as 'media' */
2771  case AST_FRAME_VOICE:
2772  case AST_FRAME_VIDEO:
2773  case AST_FRAME_TEXT:
2774  case AST_FRAME_TEXT_DATA:
2775  case AST_FRAME_DTMF_BEGIN:
2776  case AST_FRAME_DTMF_END:
2777  case AST_FRAME_IMAGE:
2778  case AST_FRAME_HTML:
2779  case AST_FRAME_MODEM:
2780  case AST_FRAME_RTCP:
2781  done = 1;
2782  break;
2783  case AST_FRAME_CONTROL:
2784  case AST_FRAME_IAX:
2787  case AST_FRAME_NULL:
2788  case AST_FRAME_CNG:
2789  break;
2790  }
2791 
2792  if (done) {
2793  break;
2794  }
2795  }
2796 
2797  ast_channel_lock(chan);
2798  while ((cur = AST_LIST_REMOVE_HEAD(&frames, frame_list))) {
2799  if (res == 0) {
2800  ast_queue_frame_head(chan, cur);
2801  }
2802  ast_frfree(cur);
2803  }
2804  ast_channel_unlock(chan);
2805  } while (0);
2806  break;
2807  default:
2808  break;
2809  }
2810 
2811  return res;
2812 }
2813 
2814 int ast_answer(struct ast_channel *chan)
2815 {
2816  SCOPE_TRACE(1, "%s\n", ast_channel_name(chan));
2817  return __ast_answer(chan, 0);
2818 }
2819 
2820 inline int ast_auto_answer(struct ast_channel *chan)
2821 {
2822  if (ast_channel_state(chan) == AST_STATE_UP) {
2823  /* Already answered */
2824  return 0;
2825  }
2826  return ast_answer(chan);
2827 }
2828 
2830 {
2831  ast_assert(NULL != chan);
2832 
2833  if (ast_tvzero(ast_channel_creationtime(chan))) {
2834  return 0;
2835  }
2837 }
2838 
2840 {
2841  return (ast_channel_get_duration_ms(chan) / 1000);
2842 }
2843 
2845 {
2846  ast_assert(NULL != chan);
2847 
2848  if (ast_tvzero(ast_channel_answertime(chan))) {
2849  return 0;
2850  }
2852 }
2853 
2855 {
2856  return (ast_channel_get_up_time_ms(chan) / 1000);
2857 }
2858 
2859 /*!
2860  * \brief Determine whether or not we have to trigger dtmf emulating using 50 fps timer events
2861  * especially when no voice frames are received during dtmf processing (direct media or muted
2862  * sender case using SIP INFO)
2863  */
2864 static inline int should_trigger_dtmf_emulating(struct ast_channel *chan)
2865 {
2867  /* We're in the middle of emulating a digit, or DTMF has been
2868  * explicitly deferred. Trigger dtmf with periodic 50 pfs timer events, then. */
2869  return 1;
2870  }
2871 
2872  if (!ast_tvzero(*ast_channel_dtmf_tv(chan)) &&
2874  /*
2875  * We're not in the middle of a digit, but it hasn't been long enough
2876  * since the last digit, so we'll have to trigger DTMF furtheron.
2877  * Using 2 times AST_MIN_DTMF_GAP to trigger readq reading for possible
2878  * buffered next dtmf event
2879  */
2880  return 1;
2881  }
2882 
2883  return 0;
2884 }
2885 
2886 static void deactivate_generator_nolock(struct ast_channel *chan)
2887 {
2888  if (ast_channel_generatordata(chan)) {
2890 
2891  if (generator && generator->release) {
2892  generator->release(chan, ast_channel_generatordata(chan));
2893  }
2898  ast_settimeout(chan, 0, NULL, NULL);
2899  }
2900 }
2901 
2903 {
2904  ast_channel_lock(chan);
2906  if (should_trigger_dtmf_emulating(chan)) {
2907  /* if in the middle of dtmf emulation keep 50 tick per sec timer on rolling */
2909  }
2910  ast_channel_unlock(chan);
2911 }
2912 
2914 {
2915  struct ast_generator *generator;
2916 
2917  ast_channel_lock(chan);
2918  generator = ast_channel_generator(chan);
2919  if (generator && generator->write_format_change) {
2920  generator->write_format_change(chan, ast_channel_generatordata(chan));
2921  }
2922  ast_channel_unlock(chan);
2923 }
2924 
2925 static int generator_force(const void *data)
2926 {
2927  /* Called if generator doesn't have data */
2928  void *tmp;
2929  int res;
2930  int (*generate)(struct ast_channel *chan, void *tmp, int datalen, int samples) = NULL;
2931  struct ast_channel *chan = (struct ast_channel *)data;
2932 
2933  ast_channel_lock(chan);
2934  tmp = ast_channel_generatordata(chan);
2936  if (ast_channel_generator(chan))
2937  generate = ast_channel_generator(chan)->generate;
2938  ast_channel_unlock(chan);
2939 
2940  if (!tmp || !generate) {
2941  return 0;
2942  }
2943 
2944  res = generate(chan, tmp, 0, ast_format_get_sample_rate(ast_channel_writeformat(chan)) / 50);
2945 
2946  ast_channel_lock(chan);
2947  if (ast_channel_generator(chan) && generate == ast_channel_generator(chan)->generate) {
2948  ast_channel_generatordata_set(chan, tmp);
2949  }
2950  ast_channel_unlock(chan);
2951 
2952  if (res) {
2953  ast_debug(1, "Auto-deactivating generator\n");
2955  }
2956 
2957  return 0;
2958 }
2959 
2960 int ast_activate_generator(struct ast_channel *chan, struct ast_generator *gen, void *params)
2961 {
2962  int res = 0;
2963  void *generatordata = NULL;
2964 
2965  ast_channel_lock(chan);
2966  if (ast_channel_generatordata(chan)) {
2967  struct ast_generator *generator_old = ast_channel_generator(chan);
2968 
2969  if (generator_old && generator_old->release) {
2970  generator_old->release(chan, ast_channel_generatordata(chan));
2971  }
2972  }
2973  if (gen->alloc && !(generatordata = gen->alloc(chan, params))) {
2974  res = -1;
2975  }
2976  ast_channel_generatordata_set(chan, generatordata);
2977  if (!res) {
2978  ast_settimeout(chan, 50, generator_force, chan);
2979  ast_channel_generator_set(chan, gen);
2980  }
2981  ast_channel_unlock(chan);
2982 
2983  ast_prod(chan);
2984 
2985  return res;
2986 }
2987 
2988 /*! \brief Wait for x amount of time on a file descriptor to have input. */
2989 int ast_waitfor_n_fd(int *fds, int n, int *ms, int *exception)
2990 {
2991  int winner = -1;
2992  ast_waitfor_nandfds(NULL, 0, fds, n, exception, &winner, ms);
2993  return winner;
2994 }
2995 
2996 /*! \brief Wait for x amount of time on a file descriptor to have input. */
2997 struct ast_channel *ast_waitfor_nandfds(struct ast_channel **c, int n, int *fds, int nfds,
2998  int *exception, int *outfd, int *ms)
2999 {
3000  struct timeval start = { 0 , 0 };
3001  struct pollfd *pfds = NULL;
3002  int res;
3003  long rms;
3004  int x, y, max;
3005  int sz = nfds;
3006  struct timeval now = { 0, 0 };
3007  struct timeval whentohangup = { 0, 0 }, diff;
3008  struct ast_channel *winner = NULL;
3009  struct fdmap {
3010  int chan;
3011  int fdno;
3012  } *fdmap = NULL;
3013 
3014  if (outfd) {
3015  *outfd = -99999;
3016  }
3017  if (exception) {
3018  *exception = 0;
3019  }
3020 
3021  for (x = 0; x < n; x++) {
3022  ast_channel_lock(c[x]);
3023  if (!ast_tvzero(*ast_channel_whentohangup(c[x]))) {
3024  if (ast_tvzero(whentohangup))
3025  now = ast_tvnow();
3026  diff = ast_tvsub(*ast_channel_whentohangup(c[x]), now);
3027  if (diff.tv_sec < 0 || ast_tvzero(diff)) {
3028  ast_test_suite_event_notify("HANGUP_TIME", "Channel: %s", ast_channel_name(c[x]));
3029  /* Should already be hungup */
3031  ast_channel_unlock(c[x]);
3032  return c[x];
3033  }
3034  if (ast_tvzero(whentohangup) || ast_tvcmp(diff, whentohangup) < 0)
3035  whentohangup = diff;
3036  }
3037  sz += ast_channel_fd_count(c[x]);
3038  ast_channel_unlock(c[x]);
3039  }
3040 
3041  if (!sz) {
3042  return NULL;
3043  }
3044 
3045  pfds = ast_alloca(sizeof(*pfds) * sz);
3046  fdmap = ast_alloca(sizeof(*fdmap) * sz);
3047 
3048  /* Wait full interval */
3049  rms = *ms;
3050  /* INT_MAX, not LONG_MAX, because it matters on 64-bit */
3051  if (!ast_tvzero(whentohangup) && whentohangup.tv_sec < INT_MAX / 1000) {
3052  rms = whentohangup.tv_sec * 1000 + whentohangup.tv_usec / 1000; /* timeout in milliseconds */
3053  if (*ms >= 0 && *ms < rms) { /* original *ms still smaller */
3054  rms = *ms;
3055  }
3056  } else if (!ast_tvzero(whentohangup) && rms < 0) {
3057  /* Tiny corner case... call would need to last >24 days */
3058  rms = INT_MAX;
3059  }
3060  /*
3061  * Build the pollfd array, putting the channels' fds first,
3062  * followed by individual fds. Order is important because
3063  * individual fd's must have priority over channel fds.
3064  */
3065  max = 0;
3066  for (x = 0; x < n; x++) {
3067  ast_channel_lock(c[x]);
3068  for (y = 0; y < ast_channel_fd_count(c[x]); y++) {
3069  fdmap[max].fdno = y; /* fd y is linked to this pfds */
3070  fdmap[max].chan = x; /* channel x is linked to this pfds */
3071  max += ast_add_fd(&pfds[max], ast_channel_fd(c[x], y));
3072  }
3073  CHECK_BLOCKING(c[x]);
3074  ast_channel_unlock(c[x]);
3075  }
3076  /* Add the individual fds */
3077  for (x = 0; x < nfds; x++) {
3078  fdmap[max].chan = -1;
3079  max += ast_add_fd(&pfds[max], fds[x]);
3080  }
3081 
3082  if (*ms > 0) {
3083  start = ast_tvnow();
3084  }
3085 
3086  if (sizeof(int) == 4) { /* XXX fix timeout > 600000 on linux x86-32 */
3087  do {
3088  int kbrms = rms;
3089  if (kbrms > 600000) {
3090  kbrms = 600000;
3091  }
3092  res = ast_poll(pfds, max, kbrms);
3093  if (!res) {
3094  rms -= kbrms;
3095  }
3096  } while (!res && (rms > 0));
3097  } else {
3098  res = ast_poll(pfds, max, rms);
3099  }
3100  for (x = 0; x < n; x++) {
3101  ast_channel_lock(c[x]);
3103  ast_channel_unlock(c[x]);
3104  }
3105  if (res < 0) { /* Simulate a timeout if we were interrupted */
3106  if (errno != EINTR) {
3107  *ms = -1;
3108  }
3109  return NULL;
3110  }
3111  if (!ast_tvzero(whentohangup)) { /* if we have a timeout, check who expired */
3112  now = ast_tvnow();
3113  for (x = 0; x < n; x++) {
3114  if (!ast_tvzero(*ast_channel_whentohangup(c[x])) && ast_tvcmp(*ast_channel_whentohangup(c[x]), now) <= 0) {
3115  ast_test_suite_event_notify("HANGUP_TIME", "Channel: %s", ast_channel_name(c[x]));
3117  if (winner == NULL) {
3118  winner = c[x];
3119  }
3120  }
3121  }
3122  }
3123  if (res == 0) { /* no fd ready, reset timeout and done */
3124  *ms = 0; /* XXX use 0 since we may not have an exact timeout. */
3125  return winner;
3126  }
3127  /*
3128  * Then check if any channel or fd has a pending event.
3129  * Remember to check channels first and fds last, as they
3130  * must have priority on setting 'winner'
3131  */
3132  for (x = 0; x < max; x++) {
3133  res = pfds[x].revents;
3134  if (res == 0) {
3135  continue;
3136  }
3137  if (fdmap[x].chan >= 0) { /* this is a channel */
3138  winner = c[fdmap[x].chan]; /* override previous winners */
3139  ast_channel_lock(winner);
3140  if (res & POLLPRI) {
3142  } else {
3144  }
3145  ast_channel_fdno_set(winner, fdmap[x].fdno);
3146  ast_channel_unlock(winner);
3147  } else { /* this is an fd */
3148  if (outfd) {
3149  *outfd = pfds[x].fd;
3150  }
3151  if (exception) {
3152  *exception = (res & POLLPRI) ? -1 : 0;
3153  }
3154  winner = NULL;
3155  }
3156  }
3157  if (*ms > 0) {
3158  *ms -= ast_tvdiff_ms(ast_tvnow(), start);
3159  if (*ms < 0) {
3160  *ms = 0;
3161  }
3162  }
3163  return winner;
3164 }
3165 
3166 struct ast_channel *ast_waitfor_n(struct ast_channel **c, int n, int *ms)
3167 {
3168  return ast_waitfor_nandfds(c, n, NULL, 0, NULL, NULL, ms);
3169 }
3170 
3171 int ast_waitfor(struct ast_channel *c, int ms)
3172 {
3173  if (ms < 0) {
3174  do {
3175  ms = 100000;
3176  ast_waitfor_nandfds(&c, 1, NULL, 0, NULL, NULL, &ms);
3177  } while (!ms);
3178  } else {
3179  ast_waitfor_nandfds(&c, 1, NULL, 0, NULL, NULL, &ms);
3180  }
3181  return ms;
3182 }
3183 
3184 int ast_waitfordigit(struct ast_channel *c, int ms)
3185 {
3186  return ast_waitfordigit_full(c, ms, NULL, -1, -1);
3187 }
3188 
3189 int ast_settimeout(struct ast_channel *c, unsigned int rate, int (*func)(const void *data), void *data)
3190 {
3191  return ast_settimeout_full(c, rate, func, data, 0);
3192 }
3193 
3194 int ast_settimeout_full(struct ast_channel *c, unsigned int rate, int (*func)(const void *data), void *data, unsigned int is_ao2_obj)
3195 {
3196  int res;
3197  unsigned int real_rate = rate, max_rate;
3198 
3199  ast_channel_lock(c);
3200 
3201  if (ast_channel_timingfd(c) == -1) {
3202  ast_channel_unlock(c);
3203  return -1;
3204  }
3205 
3206  if (!func) {
3207  rate = 0;
3208  data = NULL;
3209  }
3210 
3211  if (rate && rate > (max_rate = ast_timer_get_max_rate(ast_channel_timer(c)))) {
3212  real_rate = max_rate;
3213  }
3214 
3215  ast_debug(1, "Scheduling timer at (%u requested / %u actual) timer ticks per second\n", rate, real_rate);
3216 
3217  res = ast_timer_set_rate(ast_channel_timer(c), real_rate);
3218 
3221  }
3222 
3223  ast_channel_timingfunc_set(c, func);
3224  ast_channel_timingdata_set(c, data);
3225 
3226  if (data && is_ao2_obj) {
3227  ao2_ref(data, 1);
3229  } else {
3231  }
3232 
3233  if (func == NULL && rate == 0 && ast_channel_fdno(c) == AST_TIMING_FD) {
3234  /* Clearing the timing func and setting the rate to 0
3235  * means that we don't want to be reading from the timingfd
3236  * any more. Setting c->fdno to -1 means we won't have any
3237  * errant reads from the timingfd, meaning we won't potentially
3238  * miss any important frames.
3239  */
3240  ast_channel_fdno_set(c, -1);
3241  }
3242 
3243  ast_channel_unlock(c);
3244 
3245  return res;
3246 }
3247 
3248 int ast_waitfordigit_full(struct ast_channel *c, int timeout_ms, const char *breakon, int audiofd, int cmdfd)
3249 {
3250  struct timeval start = ast_tvnow();
3251  int ms;
3252 
3253  /* Stop if we're a zombie or need a soft hangup */
3255  return -1;
3256 
3257  /* Only look for the end of DTMF, don't bother with the beginning and don't emulate things */
3259 
3260  /* Wait for a digit, no more than timeout_ms milliseconds total.
3261  * Or, wait indefinitely if timeout_ms is <0.
3262  */
3263  while ((ms = ast_remaining_ms(start, timeout_ms))) {
3264  struct ast_channel *rchan;
3265  int outfd = -1;
3266 
3267  errno = 0;
3268  /* While ast_waitfor_nandfds tries to help by reducing the timeout by how much was waited,
3269  * it is unhelpful if it waited less than a millisecond.
3270  */
3271  rchan = ast_waitfor_nandfds(&c, 1, &cmdfd, (cmdfd > -1) ? 1 : 0, NULL, &outfd, &ms);
3272 
3273  if (!rchan && outfd < 0 && ms) {
3274  if (errno == 0 || errno == EINTR)
3275  continue;
3276  ast_log(LOG_WARNING, "Wait failed (%s)\n", strerror(errno));
3278  return -1;
3279  } else if (outfd > -1) {
3280  /* The FD we were watching has something waiting */
3281  ast_log(LOG_WARNING, "The FD we were waiting for has something waiting. Waitfordigit returning numeric 1\n");
3283  return 1;
3284  } else if (rchan) {
3285  int res;
3286  struct ast_frame *f = ast_read(c);
3287 
3288  if (!f) {
3290 
3291  return -1;
3292  }
3293 
3294  switch (f->frametype) {
3295  case AST_FRAME_DTMF_BEGIN:
3296  break;
3297  case AST_FRAME_DTMF_END:
3298  res = f->subclass.integer;
3299  if (!breakon || strchr(breakon, res)) {
3300  ast_frfree(f);
3302  return res;
3303  }
3304  break;
3305  case AST_FRAME_CONTROL:
3306  switch (f->subclass.integer) {
3307  case AST_CONTROL_HANGUP:
3308  ast_frfree(f);
3310  return -1;
3316  /* Fall-through and treat as if it were a DTMF signal. Items
3317  * that perform stream control will handle this. */
3318  res = f->subclass.integer;
3319  ast_frfree(f);
3321  return res;
3323  case AST_CONTROL_RINGING:
3324  case AST_CONTROL_ANSWER:
3325  case AST_CONTROL_SRCUPDATE:
3326  case AST_CONTROL_SRCCHANGE:
3330  case AST_CONTROL_HOLD:
3331  case AST_CONTROL_UNHOLD:
3332  case AST_CONTROL_FLASH:
3333  case -1:
3334  /* Unimportant */
3335  break;
3336  default:
3337  ast_log(LOG_WARNING, "Unexpected control subclass '%d'\n", f->subclass.integer);
3338  break;
3339  }
3340  break;
3341  case AST_FRAME_VOICE:
3342  /* Write audio if appropriate */
3343  if (audiofd > -1) {
3344  if (write(audiofd, f->data.ptr, f->datalen) < 0) {
3345  ast_log(LOG_WARNING, "write() failed: %s\n", strerror(errno));
3346  }
3347  }
3348  default:
3349  /* Ignore */
3350  break;
3351  }
3352  ast_frfree(f);
3353  }
3354  }
3355 
3357 
3358  return 0; /* Time is up */
3359 }
3360 
3364 };
3365 
3367 {
3368  switch (direction) {
3369  case DTMF_RECEIVED:
3370  return "Received";
3371  case DTMF_SENT:
3372  return "Sent";
3373  }
3374 
3375  return "?";
3376 }
3377 
3378 static void send_dtmf_begin_event(struct ast_channel *chan,
3379  enum DtmfDirection direction, const char digit)
3380 {
3381  RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
3382  char digit_str[] = { digit, '\0' };
3383 
3384  blob = ast_json_pack("{ s: s, s: s }",
3385  "digit", digit_str,
3386  "direction", dtmf_direction_to_string(direction));
3387  if (!blob) {
3388  return;
3389  }
3390 
3392 }
3393 
3394 static void send_dtmf_end_event(struct ast_channel *chan,
3395  enum DtmfDirection direction, const char digit, long duration_ms)
3396 {
3397  RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
3398  char digit_str[] = { digit, '\0' };
3399 
3400  blob = ast_json_pack("{ s: s, s: s, s: I }",
3401  "digit", digit_str,
3402  "direction", dtmf_direction_to_string(direction),
3403  "duration_ms", (ast_json_int_t)duration_ms);
3404  if (!blob) {
3405  return;
3406  }
3407 
3409 }
3410 
3411 static void send_flash_event(struct ast_channel *chan)
3412 {
3414 }
3415 
3416 static void ast_read_generator_actions(struct ast_channel *chan, struct ast_frame *f)
3417 {
3418  struct ast_generator *generator;
3419  void *gendata;
3420  int res;
3421  int samples;
3422 
3423  generator = ast_channel_generator(chan);
3424  if (!generator
3425  || !generator->generate
3426  || f->frametype != AST_FRAME_VOICE
3427  || !ast_channel_generatordata(chan)
3428  || ast_channel_timingfunc(chan)) {
3429  return;
3430  }
3431 
3432  /*
3433  * We must generate frames in phase locked mode since
3434  * we have no internal timer available.
3435  */
3437  float factor;
3439  samples = (int) (((float) f->samples) * factor);
3440  } else {
3441  samples = f->samples;
3442  }
3443 
3444  gendata = ast_channel_generatordata(chan);
3445  ast_channel_generatordata_set(chan, NULL); /* reset, to let writes go through */
3446 
3447  /*
3448  * This unlock is here based on two assumptions that hold true at
3449  * this point in the code. 1) this function is only called from
3450  * within __ast_read() and 2) all generators call ast_write() in
3451  * their generate callback.
3452  *
3453  * The reason this is added is so that when ast_write is called,
3454  * the lock that occurs there will not recursively lock the
3455  * channel. Doing this will allow deadlock avoidance to work in
3456  * deeper functions.
3457  */
3458  ast_channel_unlock(chan);
3459  res = generator->generate(chan, gendata, f->datalen, samples);
3460  ast_channel_lock(chan);
3461  if (generator == ast_channel_generator(chan)) {
3462  ast_channel_generatordata_set(chan, gendata);
3463  if (res) {
3464  ast_debug(1, "Auto-deactivating generator\n");
3466  }
3467  }
3468 }
3469 
3470 static inline void queue_dtmf_readq(struct ast_channel *chan, struct ast_frame *f)
3471 {
3472  struct ast_frame *fr = ast_channel_dtmff(chan);
3473  ast_log(LOG_WARNING, "Came out of the chan driv\n");
3474 
3476  fr->subclass.integer = f->subclass.integer;
3477  fr->len = f->len;
3478 
3479  /* The only time this function will be called is for a frame that just came
3480  * out of the channel driver. So, we want to stick it on the tail of the
3481  * readq. */
3482 
3483  ast_queue_frame(chan, fr);
3484 }
3485 
3486 /*!
3487  * \brief Determine whether or not we should ignore DTMF in the readq
3488  */
3489 static inline int should_skip_dtmf(struct ast_channel *chan)
3490 {
3492  /* We're in the middle of emulating a digit, or DTMF has been
3493  * explicitly deferred. Skip this digit, then. */
3494  return 1;
3495  }
3496 
3497  if (!ast_tvzero(*ast_channel_dtmf_tv(chan)) &&
3499  /* We're not in the middle of a digit, but it hasn't been long enough
3500  * since the last digit, so we'll have to skip DTMF for now. */
3501  return 1;
3502  }
3503 
3504  return 0;
3505 }
3506 
3507 /*!
3508  * \brief calculates the number of samples to jump forward with in a monitor stream.
3509 
3510  * \note When using ast_seekstream() with the read and write streams of a monitor,
3511  * the number of samples to seek forward must be of the same sample rate as the stream
3512  * or else the jump will not be calculated correctly.
3513  *
3514  * \retval number of samples to seek forward after rate conversion.
3515  */
3516 static inline int calc_monitor_jump(int samples, int sample_rate, int seek_rate)
3517 {
3518  int diff = sample_rate - seek_rate;
3519 
3520  if (diff > 0) {
3521  samples = samples / (float) (sample_rate / seek_rate);
3522  } else if (diff < 0) {
3523  samples = samples * (float) (seek_rate / sample_rate);
3524  }
3525 
3526  return samples;
3527 }
3528 
3529 static struct ast_frame *__ast_read(struct ast_channel *chan, int dropaudio, int dropnondefault)
3530 {
3531  struct ast_frame *f = NULL; /* the return value */
3532  int prestate;
3533  int cause = 0;
3534  struct ast_stream *stream = NULL, *default_stream = NULL;
3535 
3536  /* this function is very long so make sure there is only one return
3537  * point at the end (there are only two exceptions to this).
3538  */
3539  ast_channel_lock(chan);
3540 
3541  /* Stop if we're a zombie or need a soft hangup */
3543  if (ast_channel_generator(chan))
3545 
3546  /*
3547  * It is possible for chan->_softhangup to be set and there
3548  * still be control frames that need to be read. Instead of
3549  * just going to 'done' in the case of ast_check_hangup(), we
3550  * need to queue the end-of-Q frame so that it can mark the end
3551  * of the read queue. If there are frames to be read,
3552  * ast_queue_control() will be called repeatedly, but will only
3553  * queue the first end-of-Q frame.
3554  */
3556  ast_log(LOG_WARNING, "GOT HERE\n");
3558  } else {
3559  goto done;
3560  }
3561  } else {
3562 #ifdef AST_DEVMODE
3563  /*
3564  * The ast_waitfor() code records which of the channel's file
3565  * descriptors reported that data is available. In theory,
3566  * ast_read() should only be called after ast_waitfor() reports
3567  * that a channel has data available for reading. However,
3568  * there still may be some edge cases throughout the code where
3569  * ast_read() is called improperly. This can potentially cause
3570  * problems, so if this is a developer build, make a lot of
3571  * noise if this happens so that it can be addressed.
3572  *
3573  * One of the potential problems is blocking on a dead channel.
3574  */
3575  if (ast_channel_fdno(chan) == -1) {
3577  "ast_read() on chan '%s' called with no recorded file descriptor.\n",
3578  ast_channel_name(chan));
3579  }
3580 #endif
3581  }
3582 
3583  prestate = ast_channel_state(chan);
3584 
3585  if (ast_channel_timingfd(chan) > -1 && ast_channel_fdno(chan) == AST_TIMING_FD) {
3586  enum ast_timer_event res;
3587  int trigger_dtmf_emulating = should_trigger_dtmf_emulating(chan);
3588 
3590 
3592 
3593  switch (res) {
3595  if (ast_timer_ack(ast_channel_timer(chan), 1) < 0) {
3596  ast_log(LOG_ERROR, "Failed to acknoweldge timer in ast_read\n");
3597  goto done;
3598  }
3599 
3600  if (ast_channel_timingfunc(chan)) {
3601  /* save a copy of func/data before unlocking the channel */
3603  void *data = ast_channel_timingdata(chan);
3604  int got_ref = 0;
3606  ao2_ref(data, 1);
3607  got_ref = 1;
3608  }
3609  ast_channel_fdno_set(chan, -1);
3610  ast_channel_unlock(chan);
3611  func(data);
3612  if (got_ref) {
3613  ao2_ref(data, -1);
3614  }
3615 
3616  if (trigger_dtmf_emulating) {
3617  /*
3618  * Since we're breaking out of this switch block and not
3619  * returning, we need to re-lock the channel.
3620  */
3621  ast_channel_lock(chan);
3622  /* generate null frame to trigger dtmf emulating */
3623  f = &ast_null_frame;
3624  break;
3625  }
3626  } else if (trigger_dtmf_emulating) {
3627  /* generate null frame to trigger dtmf emualating */
3628  f = &ast_null_frame;
3629  break;
3630  } else {
3632  /* generate very last null frame to trigger dtmf emulating */
3633  f = &ast_null_frame;
3634  break;
3635  }
3636 
3637  /* cannot 'goto done' because the channel is already unlocked */
3638  return &ast_null_frame;
3639 
3641  if (AST_LIST_EMPTY(ast_channel_readq(chan)) ||
3644  }
3645  break;
3646  }
3647 
3649  /* if the AST_GENERATOR_FD is set, call the generator with args
3650  * set to -1 so it can do whatever it needs to.
3651  */
3652  void *tmp = ast_channel_generatordata(chan);
3653  ast_channel_generatordata_set(chan, NULL); /* reset to let ast_write get through */
3654  ast_channel_generator(chan)->generate(chan, tmp, -1, -1);
3655  ast_channel_generatordata_set(chan, tmp);
3656  f = &ast_null_frame;
3657  ast_channel_fdno_set(chan, -1);
3658  goto done;
3661  }
3662 
3663  /* Read and ignore anything on the alertpipe, but read only
3664  one sizeof(blah) per frame that we send from it */
3666  f = &ast_null_frame;
3667  goto done;
3668  }
3669 
3670  /* Check for pending read queue */
3671  if (!AST_LIST_EMPTY(ast_channel_readq(chan))) {
3672  int skipped_dtmf_frame = 0;
3673  int skip_dtmf = should_skip_dtmf(chan);
3674 
3676  /* We have to be picky about which frame we pull off of the readq because
3677  * there are cases where we want to leave DTMF frames on the queue until
3678  * some later time. */
3679 
3680  if ( (f->frametype == AST_FRAME_DTMF_BEGIN || f->frametype == AST_FRAME_DTMF_END) && skip_dtmf) {
3681  skipped_dtmf_frame = 1;
3682  continue;
3683  }
3684 
3686  break;
3687  }
3689 
3690  if (!f) {
3691  /* There were no acceptable frames on the readq. */
3692  f = &ast_null_frame;
3693  if (!skipped_dtmf_frame) {
3694  /*
3695  * Do not trigger alert pipe if only buffered dtmf begin or end frames
3696  * are left in the readq.
3697  */
3699  } else {
3700  /*
3701  * Safely disable continous timer events if only buffered dtmf begin or end
3702  * frames are left in the readq.
3703  */
3705  }
3706  }
3707 
3708  /* Interpret hangup and end-of-Q frames to return NULL */
3709  /* XXX why not the same for frames from the channel ? */
3710  if (f->frametype == AST_FRAME_CONTROL) {
3711  switch (f->subclass.integer) {
3712  case AST_CONTROL_HANGUP:
3714  cause = f->data.uint32;
3715  /* Fall through */
3716  case AST_CONTROL_END_OF_Q:
3717  ast_frfree(f);
3718  f = NULL;
3719  break;
3720  default:
3721  break;
3722  }
3723  } else if (f->frametype == AST_FRAME_VOICE || f->frametype == AST_FRAME_VIDEO) {
3724  if (ast_channel_tech(chan) && ast_channel_tech(chan)->read_stream) {
3727  } else {
3728  /* Since this channel driver does not support multistream determine the default stream this frame
3729  * originated from and update the frame to include it.
3730  */
3731  stream = default_stream = ast_channel_get_default_stream(chan, ast_format_get_type(f->subclass.format));
3732  /* In order to allow media to be passed up the underlying media type has to have a format negotiated on
3733  * the channel itself. In cases where this hasn't happened the channel driver is incorrectly passing up
3734  * a frame for a format that has not been negotiated. If this occurs just drop the frame as we have no
3735  * stream that it came from.
3736  */
3737  if (!stream) {
3738  ast_frfree(f);
3739  f = &ast_null_frame;
3740  } else {
3741  f->stream_num = ast_stream_get_position(stream);
3742  }
3743  }
3744  }
3745  } else {
3747  if (ast_channel_tech(chan)->exception)
3748  f = ast_channel_tech(chan)->exception(chan);
3749  else {
3750  ast_log(LOG_WARNING, "Exception flag set on '%s', but no exception handler\n", ast_channel_name(chan));
3751  f = &ast_null_frame;
3752  }
3753  /* Clear the exception flag */
3755  } else if (ast_channel_tech(chan) && ast_channel_tech(chan)->read_stream) {
3756  f = ast_channel_tech(chan)->read_stream(chan);
3757 
3758  /* This channel driver supports multistream so the stream_num on the frame is valid, the only
3759  * thing different is that we need to find the default stream so we know whether to invoke the
3760  * default stream logic or not (such as transcoding).
3761  */
3762  if (f && (f->frametype == AST_FRAME_VOICE || f->frametype == AST_FRAME_VIDEO)) {
3765  }
3766  } else if (ast_channel_tech(chan) && ast_channel_tech(chan)->read) {
3767  f = ast_channel_tech(chan)->read(chan);
3768 
3769  /* Since this channel driver does not support multistream determine the default stream this frame
3770  * originated from and update the frame to include it.
3771  */
3772  if (f && (f->frametype == AST_FRAME_VOICE || f->frametype == AST_FRAME_VIDEO)) {
3773  stream = default_stream = ast_channel_get_default_stream(chan, ast_format_get_type(f->subclass.format));
3774  if (!stream) {
3775  ast_frfree(f);
3776  f = &ast_null_frame;
3777  } else {
3778  f->stream_num = ast_stream_get_position(stream);
3779  }
3780  }
3781  }
3782  else
3783  ast_log(LOG_WARNING, "No read routine on channel %s\n", ast_channel_name(chan));
3784  }
3785 
3786  if (stream == default_stream) {
3787  /* Perform the framehook read event here. After the frame enters the framehook list
3788  * there is no telling what will happen, <insert mad scientist laugh here>!!! */
3790  }
3791 
3792  /*
3793  * Reset the recorded file descriptor that triggered this read so that we can
3794  * easily detect when ast_read() is called without properly using ast_waitfor().
3795  */
3796  ast_channel_fdno_set(chan, -1);
3797 
3798  if (f) {
3799  struct ast_frame *readq_tail = AST_LIST_LAST(ast_channel_readq(chan));
3800  struct ast_control_read_action_payload *read_action_payload;
3801  struct ast_party_connected_line connected;
3802  int hooked = 0;
3803 
3804 
3805  /* if the channel driver returned more than one frame, stuff the excess
3806  into the readq for the next ast_read call
3807  */
3808  if (AST_LIST_NEXT(f, frame_list)) {
3812  }
3813 
3814  if (dropnondefault && stream != default_stream) {
3815  /* If the frame originates from a non-default stream and the caller can not handle other streams
3816  * absorb the frame and replace it with a null one instead.
3817  */
3818  ast_frfree(f);
3819  f = &ast_null_frame;
3820  }
3821 
3822  switch (f->frametype) {
3823  case AST_FRAME_CONTROL:
3824  if (f->subclass.integer == AST_CONTROL_ANSWER) {
3825  if (prestate == AST_STATE_UP && ast_channel_is_bridged(chan)) {
3826  ast_debug(1, "Dropping duplicate answer!\n");
3827  ast_frfree(f);
3828  f = &ast_null_frame;
3829  } else {
3830  /*
3831  * Mark when outgoing channel answered so we can know how
3832  * long the channel has been up.
3833  */
3835 
3836  ast_setstate(chan, AST_STATE_UP);
3837  }
3838  } else if (f->subclass.integer == AST_CONTROL_READ_ACTION) {
3839  read_action_payload = f->data.ptr;
3840  switch (read_action_payload->action) {
3842  ast_party_connected_line_init(&connected);
3844  if (ast_connected_line_parse_data(read_action_payload->payload,
3845  read_action_payload->payload_size, &connected)) {
3846  ast_party_connected_line_free(&connected);
3847  break;
3848  }
3849  ast_channel_unlock(chan);
3850  if (ast_channel_connected_line_sub(NULL, chan, &connected, 0) &&
3851  ast_channel_connected_line_macro(NULL, chan, &connected, 1, 0)) {
3853  read_action_payload->payload,
3854  read_action_payload->payload_size);
3855  }
3856  ast_party_connected_line_free(&connected);
3857  ast_channel_lock(chan);
3858  break;
3860  ast_channel_unlock(chan);
3861  ast_sendtext(chan, (const char *)read_action_payload->payload);
3862  ast_channel_lock(chan);
3863  break;
3865  ast_channel_unlock(chan);
3866  ast_sendtext_data(chan, (struct ast_msg_data *)read_action_payload->payload);
3867  ast_channel_lock(chan);
3868  break;
3869  }
3870  ast_frfree(f);
3871  f = &ast_null_frame;
3872  } else if (f->subclass.integer == AST_CONTROL_STREAM_TOPOLOGY_REQUEST_CHANGE && dropnondefault) {
3873  /* The caller of this function is incapable of handling streams so we don't accept the change request
3874  * and stick to the streams currently on the channel.
3875  */
3877  ast_frfree(f);
3878  f = &ast_null_frame;
3879  } else if (f->subclass.integer == AST_CONTROL_STREAM_TOPOLOGY_CHANGED && dropnondefault) {
3880  /* The caller of this function is incapable of handling streams so we absorb the notification that the
3881  * stream topology has changed.
3882  */
3883  ast_frfree(f);
3884  f = &ast_null_frame;
3885  } else if (f->subclass.integer == AST_CONTROL_FLASH) {
3886  send_flash_event(chan);
3887  }
3888  break;
3889  case AST_FRAME_DTMF_END:
3891  ast_log(LOG_DTMF, "DTMF end '%c' received on %s, duration %ld ms\n", f->subclass.integer, ast_channel_name(chan), f->len);
3892  /* Queue it up if DTMF is deferred, or if DTMF emulation is forced. */
3894  queue_dtmf_readq(chan, f);
3895  ast_frfree(f);
3896  f = &ast_null_frame;
3898  if (!ast_tvzero(*ast_channel_dtmf_tv(chan)) &&
3900  /* If it hasn't been long enough, defer this digit */
3901  queue_dtmf_readq(chan, f);
3902  ast_frfree(f);
3903  f = &ast_null_frame;
3904  } else {
3905  /* There was no begin, turn this into a begin and send the end later */
3906  struct timeval tv = ast_tvnow();
3910  ast_channel_dtmf_tv_set(chan, &tv);
3911  if (f->len) {
3912  if (f->len > option_dtmfminduration)
3914  else
3916  } else
3918  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));
3919 
3920  /*
3921  * Start generating 50 fps timer events (null frames) for dtmf emulating
3922  * independently from any existing incoming voice frames.
3923  * If channel generator is already activated in regular mode use these
3924  * timer events to generate null frames.
3925  */
3926  if (!ast_channel_generator(chan)) {
3928  }
3929  }
3930  if (ast_channel_audiohooks(chan)) {
3931  struct ast_frame *old_frame = f;
3932  /*!
3933  * \todo XXX It is possible to write a digit to the audiohook twice
3934  * if the digit was originally read while the channel was in autoservice. */
3936  if (old_frame != f)
3937  ast_frfree(old_frame);
3938  }
3939  } else {
3940  struct timeval now = ast_tvnow();
3942  ast_log(LOG_DTMF, "DTMF end accepted with begin '%c' on %s\n", f->subclass.integer, ast_channel_name(chan));
3944  if (!f->len)
3945  f->len = ast_tvdiff_ms(now, *ast_channel_dtmf_tv(chan));
3946 
3947  /* detect tones that were received on
3948  * the wire with durations shorter than
3949  * option_dtmfminduration and set f->len
3950  * to the actual duration of the DTMF
3951  * frames on the wire. This will cause
3952  * dtmf emulation to be triggered later
3953  * on.
3954  */
3956  f->len = ast_tvdiff_ms(now, *ast_channel_dtmf_tv(chan));
3957  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));
3958  }
3959  } else if (!f->len) {
3960  ast_log(LOG_DTMF, "DTMF end accepted without begin '%c' on %s\n", f->subclass.integer, ast_channel_name(chan));
3962  }
3964  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));
3968  ast_frfree(f);
3969  f = &ast_null_frame;
3970 
3971  /* Start generating 50 fps timer events (null frames) for dtmf emulating
3972  * independently from any existing incoming voice frames.
3973  * If channel generator is already activated in regular mode use these
3974  * timer events to generate null frames.
3975  */
3976  if (!ast_channel_generator(chan)) {
3978  }
3979  } else {
3980  ast_log(LOG_DTMF, "DTMF end passthrough '%c' on %s\n", f->subclass.integer, ast_channel_name(chan));
3981  if (f->len < option_dtmfminduration) {
3983  }
3984  ast_channel_dtmf_tv_set(chan, &now);
3985 
3986  /* Start generating 50 fps timer events (null frames) for dtmf emulating
3987  * independently from any existing incoming voice frames.
3988  * If channel generator is already activated in regular mode use these
3989  * timer events to generate null frames.
3990  */
3991  if (!ast_channel_generator(chan)) {
3993  }
3994  }
3995  if (ast_channel_audiohooks(chan)) {
3996  struct ast_frame *old_frame = f;
3998  if (old_frame != f)
3999  ast_frfree(old_frame);
4000  }
4001  }
4002  break;
4003  case AST_FRAME_DTMF_BEGIN:
4005  ast_log(LOG_DTMF, "DTMF begin '%c' received on %s\n", f->subclass.integer, ast_channel_name(chan));
4007  (!ast_tvzero(*ast_channel_dtmf_tv(chan)) &&
4009  ast_log(LOG_DTMF, "DTMF begin ignored '%c' on %s\n", f->subclass.integer, ast_channel_name(chan));
4010  ast_frfree(f);
4011  f = &ast_null_frame;
4012  } else {
4013  struct timeval now = ast_tvnow();
4015  ast_channel_dtmf_tv_set(chan, &now);
4016  ast_log(LOG_DTMF, "DTMF begin passthrough '%c' on %s\n", f->subclass.integer, ast_channel_name(chan));
4017  }
4018  break;
4019  case AST_FRAME_NULL:
4020  /* The EMULATE_DTMF flag must be cleared here as opposed to when the duration
4021  * is reached , because we want to make sure we pass at least one
4022  * voice frame through before starting the next digit, to ensure a gap
4023  * between DTMF digits. */
4025  struct timeval now = ast_tvnow();
4026  if (!ast_channel_emulate_dtmf_duration(chan)) {
4029  } else if (ast_tvdiff_ms(now, *ast_channel_dtmf_tv(chan)) >= ast_channel_emulate_dtmf_duration(chan)) {
4031  ast_frfree(f);
4032  f = ast_channel_dtmff(chan);
4035  f->len = ast_tvdiff_ms(now, *ast_channel_dtmf_tv(chan));
4036  ast_channel_dtmf_tv_set(chan, &now);
4039  ast_log(LOG_DTMF, "DTMF end emulation of '%c' queued on %s\n", f->subclass.integer, ast_channel_name(chan));
4040  if (ast_channel_audiohooks(chan)) {
4041  struct ast_frame *old_frame = f;
4043  if (old_frame != f) {
4044  ast_frfree(old_frame);
4045  }
4046  }
4047 
4048  /* Start generating 50 fps timer events (null frames) for dtmf emulating
4049  * independently from any existing incoming voice frames.
4050  * If channel generator is already activated in regular mode use these
4051  * timer events to generate null frames.
4052  */
4053  if (!ast_channel_generator(chan)) {
4055  }
4056  }
4057  }
4058  break;
4059  case AST_FRAME_VOICE:
4060  /* If media was received from a non-default stream don't perform any actions, let it just go through */
4061  if (stream != default_stream) {
4062  break;
4063  }
4064 
4065  /* The EMULATE_DTMF flag must be cleared here as opposed to when the duration
4066  * is reached , because we want to make sure we pass at least one
4067  * voice frame through before starting the next digit, to ensure a gap
4068  * between DTMF digits. */
4070  ast_log(LOG_WARNING, "!!! t5a\n");
4073  }
4074 
4075  if (dropaudio || ast_test_flag(ast_channel_flags(chan), AST_FLAG_IN_DTMF)) {
4076  ast_log(LOG_WARNING, "!!! t5b\n");
4077  if (dropaudio)
4078  ast_read_generator_actions(chan, f);
4079  ast_frfree(f);
4080  f = &ast_null_frame;
4081  }
4082 
4084  struct timeval now = ast_tvnow();
4085  ast_log(LOG_WARNING, "!!! t5c\n");
4088  ast_frfree(f);
4089  f = ast_channel_dtmff(chan);
4092  f->len = ast_tvdiff_ms(now, *ast_channel_dtmf_tv(chan));
4093  ast_channel_dtmf_tv_set(chan, &now);
4094  if (ast_channel_audiohooks(chan)) {
4095  struct ast_frame *old_frame = f;
4097  if (old_frame != f)
4098  ast_frfree(old_frame);
4099  }
4100  ast_log(LOG_DTMF, "DTMF end emulation of '%c' queued on %s\n", f->subclass.integer, ast_channel_name(chan));
4101  } else {
4102  /* Drop voice frames while we're still in the middle of the digit */
4103  ast_frfree(f);
4104  f = &ast_null_frame;
4105  }
4106  break;
4107  }
4108  if (f->frametype != AST_FRAME_VOICE) {
4109  ast_log(LOG_WARNING, "!!! t5d\n");
4110  break;
4111  }
4114  struct ast_format *core_format;
4115 
4116  /*
4117  * Note: This frame may not be one of the current native
4118  * formats. We may have gotten it out of the read queue from
4119  * a previous multi-frame translation, from a framehook
4120  * injected frame, or the device we're talking to isn't
4121  * respecting negotiated formats. Regardless we will accept
4122  * all frames.
4123  *
4124  * Update the read translation path to handle the new format
4125  * that just came in. If the core wants slinear we need to
4126  * setup a new translation path because the core is usually
4127  * doing something with the audio itself and may not handle
4128  * any other format. e.g., Softmix bridge, holding bridge
4129  * announcer channel, recording, AMD... Otherwise, we'll
4130  * setup to pass the frame as is to the core. In this case
4131  * the core doesn't care. The channel is likely in
4132  * autoservice, safesleep, or the channel is in a bridge.
4133  * Let the bridge technology deal with format compatibility
4134  * between the channels in the bridge.
4135  *
4136  * Beware of the transcode_via_slin and genericplc options as
4137  * they force any transcoding to go through slin on a bridge.
4138  * Unfortunately transcode_via_slin is enabled by default and
4139  * genericplc is enabled in the codecs.conf.sample file.
4140  *
4141  * XXX Only updating translation to slinear frames has some
4142  * corner cases if slinear is one of the native formats and
4143  * there are different sample rates involved. We might wind
4144  * up with conflicting translation paths between channels
4145  * where the read translation path on this channel reduces
4146  * the sample rate followed by a write translation path on
4147  * the peer channel that increases the sample rate.
4148  */
4149  core_format = ast_channel_readformat(chan);
4150  if (!ast_format_cache_is_slinear(core_format)) {
4151  core_format = f->subclass.format;
4152  }
4153  ast_log(LOG_WARNING, "!!! t5f\n");
4154  if (ast_set_read_format_path(chan, f->subclass.format, core_format)) {
4155  /* Drop frame. We couldn't make it compatible with the core. */
4156  ast_frfree(f);
4157  f = &ast_null_frame;
4158  break;
4159  }
4160  }
4161  /*
4162  * Send frame to audiohooks if present, if frametype is linear, to preserve
4163  * functional compatibility with previous behavior. If not linear, hold off
4164  * until transcoding is done where we are more likely to have a linear frame
4165  */
4167  /* Place hooked after declaration */
4168  struct ast_frame *old_frame = f;
4169  hooked = 1;
4170  ast_log(LOG_WARNING, "!!! t5h\n");
4172  if (old_frame != f) {
4173  ast_frfree(old_frame);
4174  }
4175  }
4176 
4177  if (ast_channel_monitor(chan) && ast_channel_monitor(chan)->read_stream) {
4178  /* XXX what does this do ? */
4179 #ifndef MONITOR_CONSTANT_DELAY
4180  int jump = ast_channel_outsmpl(chan) - ast_channel_insmpl(chan) - 4 * f->samples;
4181  if (jump >= 0) {
4184  ast_format_get_sample_rate(ast_channel_monitor(chan)->read_stream->fmt->format));
4185  if (ast_seekstream(ast_channel_monitor(chan)->read_stream, jump, SEEK_FORCECUR) == -1) {
4186  ast_log(LOG_WARNING, "Failed to perform seek in monitoring read stream, synchronization between the files may be broken\n");
4187  }
4189  } else {
4191  }
4192 #else
4193  int jump = calc_monitor_jump((ast_channel_outsmpl(chan) - ast_channel_insmpl(chan)),
4195  ast_format_get_sample_rate(ast_channel_monitor(chan)->read_stream->fmt->format));
4196  if (jump - MONITOR_DELAY >= 0) {
4197  if (ast_seekstream(ast_channel_monitor(chan)->read_stream, jump - f->samples, SEEK_FORCECUR) == -1) {
4198  ast_log(LOG_WARNING, "Failed to perform seek in monitoring read stream, synchronization between the files may be broken\n");
4199  }
4201  } else {
4202  ast_channel_insmpl(chan) += f->samples;
4203  }
4204 #endif
4206  if (ast_writestream(ast_channel_monitor(chan)->read_stream, f) < 0)
4207  ast_log(LOG_WARNING, "Failed to write data to channel monitor read stream\n");
4208  }
4209  ast_log(LOG_WARNING, "!!! t5jj\n");
4210  }
4211 
4212  if (ast_channel_readtrans(chan)
4214  f = ast_translate(ast_channel_readtrans(chan), f, 1);
4215  if (!f) {
4216  f = &ast_null_frame;
4217  }
4218  }
4219 
4220  /* Second chance at hooking a linear frame, also the last chance */
4221  if (ast_channel_audiohooks(chan) && !hooked) {
4222  struct ast_frame *old_frame = f;
4224  if (old_frame != f) {
4225  ast_frfree(old_frame);
4226  }
4227  }
4228 
4229  /*
4230  * It is possible for the translation process on the channel to have
4231  * produced multiple frames from the single input frame we passed it; if
4232  * this happens, queue the additional frames *before* the frames we may
4233  * have queued earlier. if the readq was empty, put them at the head of
4234  * the queue, and if it was not, put them just after the frame that was
4235  * at the end of the queue.
4236  */
4237  if (AST_LIST_NEXT(f, frame_list)) {
4238  struct ast_frame *cur, *multi_frame = AST_LIST_NEXT(f, frame_list);
4239  ast_log(LOG_WARNING, "!!! t5k\n");
4240  /* Mark these frames as being re-queued */
4241  for (cur = multi_frame; cur; cur = AST_LIST_NEXT(cur, frame_list)) {
4243  }
4244 
4245  if (!readq_tail) {
4246  ast_queue_frame_head(chan, multi_frame);
4247  } else {
4248  __ast_queue_frame(chan, multi_frame, 0, readq_tail);
4249  }
4250  ast_frfree(multi_frame);
4252  }
4253 
4254  /*
4255  * Run generator sitting on the line if timing device not available
4256  * and synchronous generation of outgoing frames is necessary
4257  */
4258  ast_read_generator_actions(chan, f);
4259  break;
4260  case AST_FRAME_RTCP:
4261  /* Incoming RTCP feedback needs to get to the translator for
4262  * outgoing media, which means we treat it as an ast_write */
4263  if (ast_channel_writetrans(chan)) {
4265  }
4266  break;
4267  default:
4268  /* Just pass it on! */
4269  break;
4270  }
4271  } else {
4272  /* Make sure we always return NULL in the future */
4275  }
4276  if (cause)
4277  ast_channel_hangupcause_set(chan, cause);
4278  if (ast_channel_generator(chan))
4280  /* We no longer End the CDR here */
4281  }
4282 
4283  /* High bit prints debugging */
4284  if (ast_channel_fin(chan) & DEBUGCHAN_FLAG)
4285  ast_frame_dump(ast_channel_name(chan), f, "<<");
4287 
4288 done:
4290  ast_channel_generator(chan)->digit(chan, f->subclass.integer);
4291  }
4292 
4294  /* The list gets recreated if audiohooks are added again later */
4297  }
4298  ast_channel_unlock(chan);
4299  return f;
4300 }
4301 
4302 struct ast_frame *ast_read(struct ast_channel *chan)
4303 {
4304  return __ast_read(chan, 0, 1);
4305 }
4306 
4308 {
4309  return __ast_read(chan, 0, 0);
4310 }
4311 
4313 {
4314  return __ast_read(chan, 1, 1);
4315 }
4316 
4318 {
4319  return __ast_read(chan, 1, 0);
4320 }
4321 
4322 int ast_indicate(struct ast_channel *chan, int condition)
4323 {
4324  return ast_indicate_data(chan, condition, NULL, 0);
4325 }
4326 
4328 {
4329  /* Don't include a default case here so that we get compiler warnings
4330  * when a new type is added. */
4331 
4332  switch (condition) {
4333  case AST_CONTROL_PROGRESS:
4335  case AST_CONTROL_VIDUPDATE:
4336  case AST_CONTROL_SRCUPDATE:
4337  case AST_CONTROL_SRCCHANGE:
4338  case AST_CONTROL_RADIO_KEY:
4340  case AST_CONTROL_OPTION:
4341  case AST_CONTROL_WINK:
4342  case AST_CONTROL_FLASH:
4343  case AST_CONTROL_OFFHOOK:
4345  case AST_CONTROL_ANSWER:
4346  case AST_CONTROL_HANGUP:
4349  case AST_CONTROL_TRANSFER:
4351  case _XXX_AST_CONTROL_T38:
4352  case AST_CONTROL_CC:
4354  case AST_CONTROL_AOC:
4355  case AST_CONTROL_END_OF_Q:
4356  case AST_CONTROL_MCID:
4372  break;
4373 
4376  case AST_CONTROL_BUSY:
4377  case AST_CONTROL_RINGING:
4378  case AST_CONTROL_RING:
4379  case AST_CONTROL_HOLD:
4380  /* You can hear these */
4381  return 1;
4382 
4383  case AST_CONTROL_UNHOLD:
4384  /* This is a special case. You stop hearing this. */
4385  break;
4386  }
4387 
4388  return 0;
4389 }
4390 
4391 void ast_channel_hangupcause_hash_set(struct ast_channel *chan, const struct ast_control_pvt_cause_code *cause_code, int datalen)
4392 {
4393  char causevar[256];
4394 
4395  if (ast_channel_dialed_causes_add(chan, cause_code, datalen)) {
4396  ast_log(LOG_WARNING, "Unable to store hangup cause for %s on %s\n", cause_code->chan_name, ast_channel_name(chan));
4397  }
4398 
4399  if (cause_code->emulate_sip_cause) {
4400  snprintf(causevar, sizeof(causevar), "HASH(SIP_CAUSE,%s)", cause_code->chan_name);
4401  ast_func_write(chan, causevar, cause_code->code);
4402  }
4403 }
4404 
4406 {
4407  if (!strcasecmp(flag, "default"))
4408  return DEFAULT_AMA_FLAGS;
4409  if (!strcasecmp(flag, "omit"))
4410  return AST_AMA_OMIT;
4411  if (!strcasecmp(flag, "billing"))
4412  return AST_AMA_BILLING;
4413  if (!strcasecmp(flag, "documentation"))
4414  return AST_AMA_DOCUMENTATION;
4415  return AST_AMA_NONE;
4416 }
4417 
4419 {
4420  switch (flag) {
4421  case AST_AMA_OMIT:
4422  return "OMIT";
4423  case AST_AMA_BILLING:
4424  return "BILLING";
4425  case AST_AMA_DOCUMENTATION:
4426  return "DOCUMENTATION";
4427  default:
4428  return "Unknown";
4429  }
4430 }
4431 
4432 /*!
4433  * \internal
4434  * \brief Preprocess connected line update.
4435  * \since 12.0.0
4436  *
4437  * \param chan channel to change the indication
4438  * \param data pointer to payload data
4439  * \param datalen size of payload data
4440  *
4441  * \note This function assumes chan is locked.
4442  *
4443  * \retval 0 keep going.
4444  * \retval -1 quit now.
4445  */
4446 static int indicate_connected_line(struct ast_channel *chan, const void *data, size_t datalen)
4447 {
4448  struct ast_party_connected_line *chan_connected = ast_channel_connected(chan);
4449  struct ast_party_connected_line *chan_indicated = ast_channel_connected_indicated(chan);
4450  struct ast_party_connected_line connected;
4451  unsigned char current[1024];
4452  unsigned char proposed[1024];
4453  int current_size;
4454  int proposed_size;
4455  int res;
4456 
4457  ast_party_connected_line_set_init(&connected, chan_connected);
4458  res = ast_connected_line_parse_data(data, datalen, &connected);
4459  if (!res) {
4460  ast_channel_set_connected_line(chan, &connected, NULL);
4461  }
4462  ast_party_connected_line_free(&connected);
4463  if (res) {
4464  return -1;
4465  }
4466 
4467  current_size = ast_connected_line_build_data(current, sizeof(current),
4468  chan_indicated, NULL);
4469  proposed_size = ast_connected_line_build_data(proposed, sizeof(proposed),
4470  chan_connected, NULL);
4471  if (current_size == -1 || proposed_size == -1) {
4472  return -1;
4473  }
4474 
4475  if (current_size == proposed_size && !memcmp(current, proposed, current_size)) {
4476  ast_debug(1, "%s: Dropping redundant connected line update \"%s\" <%s>.\n",
4477  ast_channel_name(chan),
4478  S_COR(chan_connected->id.name.valid, chan_connected->id.name.str, ""),
4479  S_COR(chan_connected->id.number.valid, chan_connected->id.number.str, ""));
4480  return -1;
4481  }
4482 
4483  ast_party_connected_line_copy(chan_indicated, chan_connected);
4484  return 0;
4485 }
4486 
4487 /*!
4488  * \internal
4489  * \brief Preprocess redirecting update.
4490  * \since 12.0.0
4491  *
4492  * \param chan channel to change the indication
4493  * \param data pointer to payload data
4494  * \param datalen size of payload data
4495  *
4496  * \note This function assumes chan is locked.
4497  *
4498  * \retval 0 keep going.
4499  * \retval -1 quit now.
4500  */
4501 static int indicate_redirecting(struct ast_channel *chan, const void *data, size_t datalen)
4502 {
4503  struct ast_party_redirecting redirecting;
4504  int res;
4505 
4507  res = ast_redirecting_parse_data(data, datalen, &redirecting);
4508  if (!res) {
4509  ast_channel_set_redirecting(chan, &redirecting, NULL);
4510  }
4511  ast_party_redirecting_free(&redirecting);
4512  return res ? -1 : 0;
4513 }
4514 
4515 static int indicate_data_internal(struct ast_channel *chan, int _condition, const void *data, size_t datalen)
4516 {
4517  /* By using an enum, we'll get compiler warnings for values not handled
4518  * in switch statements. */
4519  enum ast_control_frame_type condition = _condition;
4520  struct ast_tone_zone_sound *ts = NULL;
4521  const struct ast_control_t38_parameters *t38_parameters;
4522  int res;
4523 
4524  switch (condition) {
4526  if (indicate_connected_line(chan, data, datalen)) {
4527  res = 0;
4528  return res;
4529  }
4530  break;
4532  if (indicate_redirecting(chan, data, datalen)) {
4533  res = 0;
4534  return res;
4535  }
4536  break;
4537  case AST_CONTROL_HOLD:
4538  case AST_CONTROL_UNHOLD:
4539  ast_channel_hold_state_set(chan, _condition);
4540  break;
4542  t38_parameters = data;
4543  switch (t38_parameters->request_response) {
4545  case AST_T38_NEGOTIATED:
4547  break;
4549  case AST_T38_TERMINATED:
4550  case AST_T38_REFUSED:
4552  break;
4553  default:
4554  break;
4555  }
4556  break;
4557  default:
4558  break;
4559  }
4560 
4561  if (is_visible_indication(condition)) {
4562  /* A new visible indication is requested. */
4563  ast_channel_visible_indication_set(chan, _condition);
4564  } else if (condition == AST_CONTROL_UNHOLD || _condition < 0) {
4565  /* Visible indication is cleared/stopped. */
4567  }
4568 
4569  if (ast_channel_tech(chan)->indicate) {
4570  /* See if the channel driver can handle this condition. */
4571  res = ast_channel_tech(chan)->indicate(chan, _condition, data, datalen);
4572  } else {
4573  res = -1;
4574  }
4575 
4576  if (!res) {
4577  /* The channel driver successfully handled this indication */
4578  res = 0;
4579  return res;
4580  }
4581 
4582  /* The channel driver does not support this indication, let's fake
4583  * it by doing our own tone generation if applicable. */
4584 
4585  /*!\note If we compare the enumeration type, which does not have any
4586  * negative constants, the compiler may optimize this code away.
4587  * Therefore, we must perform an integer comparison here. */
4588  if (_condition < 0) {
4589  /* Stop any tones that are playing */
4590  ast_playtones_stop(chan);
4591  res = 0;
4592  return res;
4593  }
4594 
4595  /* Handle conditions that we have tones for. */
4596  switch (condition) {
4597  case _XXX_AST_CONTROL_T38:
4598  /* deprecated T.38 control frame */
4599  res = -1;
4600  return res;
4602  /* there is no way to provide 'default' behavior for these
4603  * control frames, so we need to return failure, but there
4604  * is also no value in the log message below being emitted
4605  * since failure to handle these frames is not an 'error'
4606  * so just return right now. in addition, we want to return
4607  * whatever value the channel driver returned, in case it
4608  * has some meaning.*/
4609  return res;
4610  case AST_CONTROL_RINGING:
4611  ts = ast_get_indication_tone(ast_channel_zone(chan), "ring");
4612  /* It is common practice for channel drivers to return -1 if trying
4613  * to indicate ringing on a channel which is up. The idea is to let the
4614  * core generate the ringing inband. However, we don't want the
4615  * warning message about not being able to handle the specific indication
4616  * to print nor do we want ast_indicate_data to return an "error" for this
4617  * condition
4618  */
4619  if (ast_channel_state(chan) == AST_STATE_UP) {
4620  res = 0;
4621  }
4622  break;
4623  case AST_CONTROL_BUSY:
4624  ts = ast_get_indication_tone(ast_channel_zone(chan), "busy");
4625  break;
4628  ts = ast_get_indication_tone(ast_channel_zone(chan), "congestion");
4629  break;
4631  ast_channel_hangupcause_hash_set(chan, data, datalen);
4632  res = 0;
4633  break;
4634  case AST_CONTROL_PROGRESS:
4636  case AST_CONTROL_VIDUPDATE:
4637  case AST_CONTROL_SRCUPDATE:
4638  case AST_CONTROL_SRCCHANGE:
4639  case AST_CONTROL_RADIO_KEY:
4641  case AST_CONTROL_OPTION:
4642  case AST_CONTROL_WINK:
4643  case AST_CONTROL_FLASH:
4644  case AST_CONTROL_OFFHOOK:
4646  case AST_CONTROL_ANSWER:
4647  case AST_CONTROL_HANGUP:
4648  case AST_CONTROL_RING:
4649  case AST_CONTROL_HOLD:
4650  case AST_CONTROL_UNHOLD:
4651  case AST_CONTROL_TRANSFER:
4654  case AST_CONTROL_CC:
4656  case AST_CONTROL_AOC:
4657  case AST_CONTROL_END_OF_Q:
4658  case AST_CONTROL_MCID:
4673  /* Nothing left to do for these. */
4674  res = 0;
4675  break;
4676  }
4677 
4678  if (ts) {
4679  /* We have a tone to play, yay. */
4680  ast_debug(1, "Driver for channel '%s' does not support indication %u, emulating it\n", ast_channel_name(chan), condition);
4681  res = ast_playtones_start(chan, 0, ts->data, 1);
4682  if (!res) {
4683  ast_test_suite_event_notify("RINGING_INBAND",
4684  "Channel: %s\r\n",
4685  ast_channel_name(chan));
4686  }
4687  ts = ast_tone_zone_sound_unref(ts);
4688  }
4689 
4690  if (res) {
4691  /* not handled */
4692  ast_log(LOG_WARNING, "Unable to handle indication %u for '%s'\n", condition, ast_channel_name(chan));
4693  }
4694 
4695  return res;
4696 }
4697 
4698 int ast_indicate_data(struct ast_channel *chan, int _condition, const void *data, size_t datalen)
4699 {
4700  int res;
4701  /* this frame is used by framehooks. if it is set, we must free it at the end of this function */
4702  struct ast_frame *awesome_frame = NULL;
4703 
4704  ast_channel_lock(chan);
4705 
4706  /* Don't bother if the channel is about to go away, anyway. */
4708  || (ast_check_hangup(chan) && !ast_channel_is_leaving_bridge(chan)))
4709  && _condition != AST_CONTROL_MASQUERADE_NOTIFY) {
4710  res = -1;
4711  goto indicate_cleanup;
4712  }
4713 
4715  /* Do framehooks now, do it, go, go now */
4716  struct ast_frame frame = {
4718  .subclass.integer = _condition,
4719  .data.ptr = (void *) data, /* this cast from const is only okay because we do the ast_frdup below */
4720  .datalen = datalen
4721  };
4722 
4723  /* we have now committed to freeing this frame */
4724  awesome_frame = ast_frdup(&frame);
4725 
4726  /* who knows what we will get back! the anticipation is killing me. */
4728  awesome_frame);
4729  if (!awesome_frame
4730  || awesome_frame->frametype != AST_FRAME_CONTROL) {
4731  res = 0;
4732  goto indicate_cleanup;
4733  }
4734 
4735  _condition = awesome_frame->subclass.integer;
4736  data = awesome_frame->data.ptr;
4737  datalen = awesome_frame->datalen;
4738  }
4739 
4740  res = indicate_data_internal(chan, _condition, data, datalen);
4741 
4742 indicate_cleanup:
4743  ast_channel_unlock(chan);
4744  if (awesome_frame) {
4745  ast_frfree(awesome_frame);
4746  }
4747 
4748  return res;
4749 }
4750 
4751 int ast_recvchar(struct ast_channel *chan, int timeout)
4752 {
4753  int c;
4754  char *buf = ast_recvtext(chan, timeout);
4755  if (buf == NULL)
4756  return -1; /* error or timeout */
4757  c = *(unsigned char *)buf;
4758  ast_free(buf);
4759  return c;
4760 }
4761 
4762 char *ast_recvtext(struct ast_channel *chan, int timeout)
4763 {
4764  int res;
4765  char *buf = NULL;
4766  struct timeval start = ast_tvnow();
4767  int ms;
4768 
4769  while ((ms = ast_remaining_ms(start, timeout))) {
4770  struct ast_frame *f;
4771 
4772  if (ast_check_hangup(chan)) {
4773  break;
4774  }
4775  res = ast_waitfor(chan, ms);
4776  if (res <= 0) {/* timeout or error */
4777  break;
4778  }
4779  f = ast_read(chan);
4780  if (f == NULL) {
4781  break; /* no frame */
4782  }
4784  ast_frfree(f);
4785  break;
4786  } else if (f->frametype == AST_FRAME_TEXT) { /* what we want */
4787  buf = ast_strndup((char *) f->data.ptr, f->datalen); /* dup and break */
4788  ast_frfree(f);
4789  break;
4790  }
4791  ast_frfree(f);
4792  }
4793  return buf;
4794 }
4795 
4796 int ast_sendtext_data(struct ast_channel *chan, struct ast_msg_data *msg)
4797 {
4798  int res = 0;
4799  const char *body = ast_msg_data_get_attribute(msg, AST_MSG_DATA_ATTR_BODY);
4800  const char *content_type = ast_msg_data_get_attribute(msg, AST_MSG_DATA_ATTR_CONTENT_TYPE);
4801 
4802  ast_channel_lock(chan);
4803  /* Stop if we're a zombie or need a soft hangup */
4805  ast_channel_unlock(chan);
4806  return -1;
4807  }
4808 
4809  CHECK_BLOCKING(chan);
4810  if (ast_channel_tech(chan)->write_text
4811  && (ast_strlen_zero(content_type) || ast_begins_with(content_type, "text/"))
4813  struct ast_frame f;
4814  /* T.140 payload does not include the null byte terminator */
4815  size_t body_len = strlen(body);
4816 
4817  /* Process as T.140 text (moved here from ast_sendtext() */
4818  memset(&f, 0, sizeof(f));
4819  f.src = "DIALPLAN";
4822  f.datalen = body_len;
4824  f.data.ptr = ast_strdup(body);
4825  if (f.data.ptr) {
4826  res = ast_channel_tech(chan)->write_text(chan, &f);
4827  } else {
4828  res = -1;
4829  }
4830  ast_frfree(&f);
4831  } else if ((ast_channel_tech(chan)->properties & AST_CHAN_TP_SEND_TEXT_DATA)
4832  && ast_channel_tech(chan)->send_text_data) {
4833  /* Send enhanced message to a channel driver that supports it */
4834  ast_debug(1, "Sending TEXT_DATA from '%s' to %s:%s %s\n",
4837  ast_channel_name(chan), body);
4838  res = ast_channel_tech(chan)->send_text_data(chan, msg);
4839  } else if (ast_channel_tech(chan)->send_text
4840  && (ast_strlen_zero(content_type) || ast_begins_with(content_type, "text/"))) {
4841  /* Send the body of an enhanced message to a channel driver that supports only a char str */
4842  ast_debug(1, "Sending TEXT to %s: %s\n", ast_channel_name(chan), body);
4843  res = ast_channel_tech(chan)->send_text(chan, body);
4844  } else {
4845  ast_debug(1, "Channel technology does not support sending content type '%s' on channel '%s'\n",
4846  S_OR(content_type, "text/plain"), ast_channel_name(chan));
4847  res = -1;
4848  }
4850  ast_channel_unlock(chan);
4851  return res;
4852 }
4853 
4854 int ast_sendtext(struct ast_channel *chan, const char *text)
4855 {
4856  struct ast_msg_data *msg;
4857  int rc;
4858  struct ast_msg_data_attribute attrs[] =
4859  {
4860  {
4862  .value = (char *)text,
4863  }
4864  };
4865 
4866  if (ast_strlen_zero(text)) {
4867  return 0;
4868  }
4869 
4871  if (!msg) {
4872  return -1;
4873  }
4874  rc = ast_sendtext_data(chan, msg);
4875  ast_free(msg);
4876 
4877  return rc;
4878 }
4879 
4881 {
4882  static const char * const mf_tones[] = {
4883  "1300+1500", /* 0 */
4884  "700+900", /* 1 */
4885  "700+1100", /* 2 */
4886  "900+1100", /* 3 */
4887  "700+1300", /* 4 */
4888  "900+1300", /* 5 */
4889  "1100+1300", /* 6 */
4890  "700+1500", /* 7 */
4891  "900+1500", /* 8 */
4892  "1100+1500", /* 9 */
4893  "1100+1700", /* * (KP) */
4894  "1500+1700", /* # (ST) */
4895  "900+1700", /* A (STP) */
4896  "1300+1700", /* B (ST2P) */
4897  "700+1700" /* C (ST3P) */
4898  };
4899 
4900  if (digit >= '0' && digit <='9') {
4901  ast_playtones_start(chan, 0, mf_tones[digit-'0'], 0);
4902  } else if (digit == '*') {
4903  ast_playtones_start(chan, 0, mf_tones[10], 0);
4904  } else if (digit == '#') {
4905  ast_playtones_start(chan, 0, mf_tones[11], 0);
4906  } else if (digit == 'A') {
4907  ast_playtones_start(chan, 0, mf_tones[12], 0);
4908  } else if (digit == 'B') {
4909  ast_playtones_start(chan, 0, mf_tones[13], 0);
4910  } else if (digit == 'C') {
4911  ast_playtones_start(chan, 0, mf_tones[14], 0);
4912  } else {
4913  /* not handled */
4914  ast_log(LOG_WARNING, "Unable to generate MF tone '%c' for '%s'\n", digit, ast_channel_name(chan));
4915  }
4916  return 0;
4917 }
4918 
4919 int ast_senddigit_begin(struct ast_channel *chan, char digit)
4920 {
4921  /* Device does not support DTMF tones, lets fake
4922  * it by doing our own generation. */
4923  static const char * const dtmf_tones[] = {
4924  "941+1336", /* 0 */
4925  "697+1209", /* 1 */
4926  "697+1336", /* 2 */
4927  "697+1477", /* 3 */
4928  "770+1209", /* 4 */
4929  "770+1336", /* 5 */
4930  "770+1477", /* 6 */
4931  "852+1209", /* 7 */
4932  "852+1336", /* 8 */
4933  "852+1477", /* 9 */
4934  "697+1633", /* A */
4935  "770+1633", /* B */
4936  "852+1633", /* C */
4937  "941+1633", /* D */
4938  "941+1209", /* * */
4939  "941+1477" /* # */
4940  };
4941 
4942  if (!ast_channel_tech(chan)->send_digit_begin)
4943  return 0;
4944 
4945  ast_channel_lock(chan);
4948  ast_channel_unlock(chan);
4949 
4950  if (!ast_channel_tech(chan)->send_digit_begin(chan, digit))
4951  return 0;
4952 
4953  if (digit >= '0' && digit <='9')
4954  ast_playtones_start(chan, 0, dtmf_tones[digit-'0'], 0);
4955  else if (digit >= 'A' && digit <= 'D')
4956  ast_playtones_start(chan, 0, dtmf_tones[digit-'A'+10], 0);
4957  else if (digit == '*')
4958  ast_playtones_start(chan, 0, dtmf_tones[14], 0);
4959  else if (digit == '#')
4960  ast_playtones_start(chan, 0, dtmf_tones[15], 0);
4961  else {
4962  /* not handled */
4963  ast_debug(1, "Unable to generate DTMF tone '%c' for '%s'\n", digit, ast_channel_name(chan));
4964  }
4965 
4966  return 0;
4967 }
4968 
4969 int ast_senddigit_end(struct ast_channel *chan, char digit, unsigned int duration)
4970 {
4971  int res = -1;
4972 
4973  if (ast_channel_tech(chan)->send_digit_end)
4974  res = ast_channel_tech(chan)->send_digit_end(chan, digit, duration);
4975 
4976  ast_channel_lock(chan);
4977  if (ast_channel_sending_dtmf_digit(chan) == digit) {
4979  }
4980  ast_channel_unlock(chan);
4981 
4982  if (res && ast_channel_generator(chan))
4983  ast_playtones_stop(chan);
4984 
4985  return 0;
4986 }
4987 
4989 {
4990  if (ast_channel_generator(chan)) {
4991  ast_playtones_stop(chan);
4992  return 0;
4993  }
4994  return -1;
4995 }
4996 
4997 int ast_senddigit_mf(struct ast_channel *chan, char digit, unsigned int duration,
4998  unsigned int durationkp, unsigned int durationst, int is_external)
4999 {
5000  if (duration < DEFAULT_EMULATE_MF_DURATION) {
5001  duration = DEFAULT_EMULATE_MF_DURATION;
5002  }
5003  if (ast_channel_tech(chan)->send_digit_begin) {
5004  if (digit == '*') {
5005  duration = durationkp;
5006  } else if (digit == '#' || digit == 'A' || digit == 'B' || digit == 'C') {
5007  duration = durationst;
5008  }
5009  ast_senddigit_mf_begin(chan, digit);
5010  if (is_external) {
5011  usleep(duration * 1000);
5012  } else {
5013  ast_safe_sleep(chan, duration);
5014  }
5015  }
5016  return ast_senddigit_mf_end(chan);
5017 }
5018 
5019 int ast_senddigit(struct ast_channel *chan, char digit, unsigned int duration)
5020 {
5021  if (duration < AST_DEFAULT_EMULATE_DTMF_DURATION) {
5023  }
5024  if (ast_channel_tech(chan)->send_digit_begin) {
5025  ast_senddigit_begin(chan, digit);
5026  ast_safe_sleep(chan, duration);
5027  }
5028 
5029  return ast_senddigit_end(chan, digit, duration);
5030 }
5031 
5032 int ast_senddigit_external(struct ast_channel *chan, char digit, unsigned int duration)
5033 {
5034  if (duration < AST_DEFAULT_EMULATE_DTMF_DURATION) {
5036  }
5037  if (ast_channel_tech(chan)->send_digit_begin) {
5038  ast_senddigit_begin(chan, digit);
5039  usleep(duration * 1000);
5040  }
5041 
5042  return ast_senddigit_end(chan, digit, duration);
5043 }
5044 
5045 int ast_prod(struct ast_channel *chan)
5046 {
5047  struct ast_frame a = { AST_FRAME_VOICE };
5048  char nothing[128];
5049 
5050  /* Send an empty audio frame to get things moving */
5051  if (ast_channel_state(chan) != AST_STATE_UP) {
5052  ast_debug(1, "Prodding channel '%s'\n", ast_channel_name(chan));
5054  a.data.ptr = nothing + AST_FRIENDLY_OFFSET;
5055  a.src = "ast_prod"; /* this better match check in ast_write */
5056  if (ast_write(chan, &a))
5057  ast_log(LOG_WARNING, "Prodding channel '%s' failed\n", ast_channel_name(chan));
5058  }
5059  return 0;
5060 }
5061 
5062 int ast_write_video(struct ast_channel *chan, struct ast_frame *fr)
5063 {
5064  int res;
5065  if (!ast_channel_tech(chan)->write_video)
5066  return 0;
5067  res = ast_write(chan, fr);
5068  if (!res)
5069  res = 1;
5070  return res;
5071 }
5072 
5073 struct plc_ds {
5074  /* A buffer in which to store SLIN PLC
5075  * samples generated by the generic PLC
5076  * functionality in plc.c
5077  */
5079  /* The current number of samples in the
5080  * samples_buf
5081  */
5082  size_t num_samples;
5084 };
5085 
5086 static void plc_ds_destroy(void *data)
5087 {
5088  struct plc_ds *plc = data;
5089  ast_free(plc->samples_buf);
5090  ast_free(plc);
5091 }
5092 
5093 static const struct ast_datastore_info plc_ds_info = {
5094  .type = "plc",
5095  .destroy = plc_ds_destroy,
5096 };
5097 
5098 static void adjust_frame_for_plc(struct ast_channel *chan, struct ast_frame *frame, struct ast_datastore *datastore)
5099 {
5100  int num_new_samples = frame->samples;
5101  struct plc_ds *plc = datastore->data;
5102 
5103  /* As a general note, let me explain the somewhat odd calculations used when taking
5104  * the frame offset into account here. According to documentation in frame.h, the frame's
5105  * offset field indicates the number of bytes that the audio is offset. The plc->samples_buf
5106  * is not an array of bytes, but rather an array of 16-bit integers since it holds SLIN
5107  * samples. So I had two choices to make here with the offset.
5108  *
5109  * 1. Make the offset AST_FRIENDLY_OFFSET bytes. The main downside for this is that
5110  * I can't just add AST_FRIENDLY_OFFSET to the plc->samples_buf and have the pointer
5111  * arithmetic come out right. I would have to do some odd casting or division for this to
5112  * work as I wanted.
5113  * 2. Make the offset AST_FRIENDLY_OFFSET * 2 bytes. This allows the pointer arithmetic
5114  * to work out better with the plc->samples_buf. The downside here is that the buffer's
5115  * allocation contains an extra 64 bytes of unused space.
5116  *
5117  * I decided to go with option 2. This is why in the calloc statement and the statement that
5118  * sets the frame's offset, AST_FRIENDLY_OFFSET is multiplied by 2.
5119  */
5120 
5121  /* If this audio frame has no samples to fill in, ignore it */
5122  if (!num_new_samples) {
5123  return;
5124  }
5125 
5126  /* First, we need to be sure that our buffer is large enough to accomodate
5127  * the samples we need to fill in. This will likely only occur on the first
5128  * frame we write.
5129  */
5130  if (plc->num_samples < num_new_samples) {
5131  ast_free(plc->samples_buf);
5132  plc->samples_buf = ast_calloc(1, (num_new_samples * sizeof(*plc->samples_buf)) + (AST_FRIENDLY_OFFSET * 2));
5133  if (!plc->samples_buf) {
5134  ast_channel_datastore_remove(chan, datastore);
5135  ast_datastore_free(datastore);
5136  return;
5137  }
5138  plc->num_samples = num_new_samples;
5139  }
5140 
5141  if (frame->datalen == 0) {
5143  frame->data.ptr = plc->samples_buf + AST_FRIENDLY_OFFSET;
5144  frame->datalen = num_new_samples * 2;
5145  frame->offset = AST_FRIENDLY_OFFSET * 2;
5146  } else {
5147  plc_rx(&plc->plc_state, frame->data.ptr, frame->samples);
5148  }
5149 }
5150 
5151 static void apply_plc(struct ast_channel *chan, struct ast_frame *frame)
5152 {
5153  struct ast_datastore *datastore;
5154  struct plc_ds *plc;
5155 
5156  datastore = ast_channel_datastore_find(chan, &plc_ds_info, NULL);
5157  if (datastore) {
5158  plc = datastore->data;
5159  adjust_frame_for_plc(chan, frame, datastore);
5160  return;
5161  }
5162 
5163  datastore = ast_datastore_alloc(&plc_ds_info, NULL);
5164  if (!datastore) {
5165  return;
5166  }
5167  plc = ast_calloc(1, sizeof(*plc));
5168  if (!plc) {
5169  ast_datastore_free(datastore);
5170  return;
5171  }
5172  datastore->data = plc;
5173  ast_channel_datastore_add(chan, datastore);
5174  adjust_frame_for_plc(chan, frame, datastore);
5175 }
5176 
5177 static int tech_write(struct ast_channel *chan, struct ast_stream *stream,
5178  struct ast_stream *default_stream, struct ast_frame *frame)
5179 {
5180  if (ast_channel_tech(chan)->write_stream) {
5181  return stream ? ast_channel_tech(chan)->write_stream(
5182  chan, ast_stream_get_position(stream), frame) : 0;
5183  }
5184 
5185  return ((stream == default_stream) && ast_channel_tech(chan)->write) ?
5186  ast_channel_tech(chan)->write(chan, frame) : 0;
5187 }
5188 
5189 int ast_write(struct ast_channel *chan, struct ast_frame *fr)
5190 {
5191  return ast_write_stream(chan, -1, fr);
5192 }
5193 
5194 int ast_write_stream(struct ast_channel *chan, int stream_num, struct ast_frame *fr)
5195 {
5196  struct ast_stream *stream = NULL, *default_stream = NULL;
5197  int res = -1;
5198  struct ast_frame *f = NULL;
5199  int count = 0;
5200  int hooked = 0;
5201 
5202  /*Deadlock avoidance*/
5203  while(ast_channel_trylock(chan)) {
5204  /*cannot goto done since the channel is not locked*/
5205  if(count++ > 10) {
5206  ast_debug(1, "Deadlock avoided for write to channel '%s'\n", ast_channel_name(chan));
5207  return 0;
5208  }
5209  usleep(1);
5210  }
5211 
5212  /* Stop if we're a zombie or need a soft hangup */
5214  goto done;
5215  }
5216 
5217  if (stream_num >= 0) {
5218  /* If we were told to write to an explicit stream then allow this frame through, no matter
5219  * if the type is expected or not (a framehook could change)
5220  */
5222  goto done;
5223  }
5225  default_stream = ast_channel_get_default_stream(chan, ast_stream_get_type(stream));
5226  } else if (fr->frametype == AST_FRAME_VOICE || fr->frametype == AST_FRAME_VIDEO || fr->frametype == AST_FRAME_MODEM) {
5227  /* If we haven't been told of a stream then we need to figure out which once we need */
5229 
5230  /* Some frame types have a fixed media type */
5231  if (fr->frametype == AST_FRAME_VOICE || fr->frametype == AST_FRAME_VIDEO) {
5232  type = ast_format_get_type(fr->subclass.format);
5233  } else if (fr->frametype == AST_FRAME_MODEM) {
5234  type = AST_MEDIA_TYPE_IMAGE;
5235  }
5236 
5237  /* No stream was specified, so use the default one */
5238  stream = default_stream = ast_channel_get_default_stream(chan, type);
5239  }
5240 
5241  /* Perform the framehook write event here. After the frame enters the framehook list
5242  * there is no telling what will happen, how awesome is that!!! */
5243  if ((stream == default_stream) && !(fr = ast_framehook_list_write_event(ast_channel_framehooks(chan), fr))) {
5244  res = 0;
5245  goto done;
5246  }
5247 
5248  if (ast_channel_generatordata(chan) && (fr->frametype != AST_FRAME_RTCP) && (!fr->src || strcasecmp(fr->src, "ast_prod"))) {
5251  } else {
5252  if (fr->frametype == AST_FRAME_DTMF_END) {
5253  /* There is a generator running while we're in the middle of a digit.
5254  * It's probably inband DTMF, so go ahead and pass it so it can
5255  * stop the generator */
5256  ast_channel_unlock(chan);
5257  res = ast_senddigit_end(chan, fr->subclass.integer, fr->len);
5258  ast_channel_lock(chan);
5259  } else if (fr->frametype == AST_FRAME_CONTROL
5260  && fr->subclass.integer == AST_CONTROL_UNHOLD) {
5261  /*
5262  * This is a side case where Echo is basically being called
5263  * and the person put themselves on hold and took themselves
5264  * off hold.
5265  */
5267  fr->datalen);
5268  }
5269  res = 0; /* XXX explain, why 0 ? */
5270  goto done;
5271  }
5272  }
5273  /* High bit prints debugging */
5274  if (ast_channel_fout(chan) & DEBUGCHAN_FLAG)
5275  ast_frame_dump(ast_channel_name(chan), fr, ">>");
5276  switch (fr->frametype) {
5277  case AST_FRAME_CONTROL:
5278  indicate_data_internal(chan, fr->subclass.integer, fr->data.ptr, fr->datalen);
5279  res = 0;
5280  break;
5281  case AST_FRAME_DTMF_BEGIN:
5282  if (ast_channel_audiohooks(chan)) {
5283  struct ast_frame *old_frame = fr;
5285  if (old_frame != fr)
5286  f = fr;
5287  }
5289  ast_channel_unlock(chan);
5290  res = ast_senddigit_begin(chan, fr->subclass.integer);
5291  ast_channel_lock(chan);
5292  break;
5293  case AST_FRAME_DTMF_END:
5294  if (ast_channel_audiohooks(chan)) {
5295  struct ast_frame *new_frame = fr;
5296 
5298  if (new_frame != fr) {
5299  ast_frfree(new_frame);
5300  }
5301  }
5303  ast_channel_unlock(chan);
5304  res = ast_senddigit_end(chan, fr->subclass.integer, fr->len);
5305  ast_channel_lock(chan);
5306  break;
5307  case AST_FRAME_TEXT:
5308  CHECK_BLOCKING(chan);
5310  res = (ast_channel_tech(chan)->write_text == NULL) ? 0 :
5311  ast_channel_tech(chan)->write_text(chan, fr);
5312  } else {
5313  res = (ast_channel_tech(chan)->send_text == NULL) ? 0 :
5314  ast_channel_tech(chan)->send_text(chan, (char *) fr->data.ptr);
5315  }
5317  break;
5318  case AST_FRAME_HTML:
5319  CHECK_BLOCKING(chan);
5320  res = (ast_channel_tech(chan)->send_html == NULL) ? 0 :
5321  ast_channel_tech(chan)->send_html(chan, fr->subclass.integer, (char *) fr->data.ptr, fr->datalen);
5323  break;
5324  case AST_FRAME_VIDEO:
5325  /* XXX Handle translation of video codecs one day XXX */
5326  CHECK_BLOCKING(chan);
5327  if (ast_channel_tech(chan)->write_stream) {
5328  if (stream) {
5329  res = ast_channel_tech(chan)->write_stream(chan, ast_stream_get_position(stream), fr);
5330  } else {
5331  res = 0;
5332  }
5333  } else if ((stream == default_stream) && ast_channel_tech(chan)->write_video) {
5334  res = ast_channel_tech(chan)->write_video(chan, fr);
5335  } else {
5336  res = 0;
5337  }
5339  break;
5340  case AST_FRAME_MODEM:
5341  CHECK_BLOCKING(chan);
5342  res = tech_write(chan, stream, default_stream, fr);
5344  break;
5345  case AST_FRAME_VOICE:
5347  apply_plc(chan, fr);
5348  }
5349 
5350  f = fr;
5351 
5352  /*
5353  * Send frame to audiohooks if present, if frametype is linear (else, later as per
5354  * previous behavior)
5355  */
5356  if ((stream == default_stream) && ast_channel_audiohooks(chan)) {
5358  hooked = 1;
5360  }
5361  }
5362 
5363  /* If the frame is in the raw write format, then it's easy... just use the frame - otherwise we will have to translate */
5364  if ((stream == default_stream) && ast_format_cmp(fr->subclass.format, ast_channel_rawwriteformat(chan)) != AST_FORMAT_CMP_EQUAL) {
5366  struct ast_str *codec_buf = ast_str_alloca(AST_FORMAT_CAP_NAMES_LEN);
5367 
5368  /*
5369  * We are not setup to write this frame. Things may have changed
5370  * on the peer side of the world and we try to adjust the format to
5371  * make it compatible again. However, bad things can happen if we
5372  * cannot setup a new translation path. Problems range from no
5373  * audio, one-way audio, to garbled audio. The best we can do is
5374  * request the call to hangup since we could not make it compatible.
5375  *
5376  * Being continuously spammed by this message likely indicates a
5377  * problem with the peer because it cannot make up its mind about
5378  * which format to use.
5379  */
5380  ast_debug(1, "Channel %s changing write format from %s to %s, native formats %s\n",
5381  ast_channel_name(chan),
5385  if (ast_set_write_format(chan, fr->subclass.format)) {
5386  /* Could not handle the new write format. Induce a hangup. */
5387  break;
5388  }
5389  }
5390 
5391  if (ast_channel_writetrans(chan)) {
5392  struct ast_frame *trans_frame = ast_translate(ast_channel_writetrans(chan), f, 0);
5393  if (trans_frame != f && f != fr) {
5394  /*
5395  * If translate gives us a new frame and so did the audio
5396  * hook then we need to free the one from the audio hook.
5397  */
5398  ast_frfree(f);
5399  }
5400  f = trans_frame;
5401  }
5402  }
5403 
5404  if (!f) {
5405  res = 0;
5406  break;
5407  }
5408 
5409  if ((stream == default_stream) && ast_channel_audiohooks(chan) && !hooked) {
5410  struct ast_frame *prev = NULL, *new_frame, *cur, *dup;
5411  int freeoldlist = 0;
5412 
5413  if (f != fr) {
5414  freeoldlist = 1;
5415  }
5416 
5417  /* Since ast_audiohook_write may return a new frame, and the cur frame is
5418  * an item in a list of frames, create a new list adding each cur frame back to it
5419  * regardless if the cur frame changes or not. */
5420  for (cur = f; cur; cur = AST_LIST_NEXT(cur, frame_list)) {
5422 
5423  /* if this frame is different than cur, preserve the end of the list,
5424  * free the old frames, and set cur to be the new frame */
5425  if (new_frame != cur) {
5426 
5427  /* doing an ast_frisolate here seems silly, but we are not guaranteed the new_frame
5428  * isn't part of local storage, meaning if ast_audiohook_write is called multiple
5429  * times it may override the previous frame we got from it unless we dup it */
5430  if ((dup = ast_frisolate(new_frame))) {
5432  if (freeoldlist) {
5433  AST_LIST_NEXT(cur, frame_list) = NULL;
5434  ast_frfree(cur);
5435  }
5436  if (new_frame != dup) {
5437  ast_frfree(new_frame);
5438  }
5439  cur = dup;
5440  }
5441  }
5442 
5443  /* now, regardless if cur is new or not, add it to the new list,
5444  * if the new list has not started, cur will become the first item. */
5445  if (prev) {
5446  AST_LIST_NEXT(prev, frame_list) = cur;
5447  } else {
5448  f = cur; /* set f to be the beginning of our new list */
5449  }
5450  prev = cur;
5451  }
5452  }
5453 
5454  /* If Monitor is running on this channel, then we have to write frames out there too */
5455  /* the translator on chan->writetrans may have returned multiple frames
5456  from the single frame we passed in; if so, feed each one of them to the
5457  monitor */
5458  if ((stream == default_stream) && ast_channel_monitor(chan) && ast_channel_monitor(chan)->write_stream) {
5459  struct ast_frame *cur;
5460 
5461  for (cur = f; cur; cur = AST_LIST_NEXT(cur, frame_list)) {
5462  /* XXX must explain this code */
5463 #ifndef MONITOR_CONSTANT_DELAY
5464  int jump = ast_channel_insmpl(chan) - ast_channel_outsmpl(chan) - 4 * cur->samples;
5465  if (jump >= 0) {
5469  if (ast_seekstream(ast_channel_monitor(chan)->write_stream, jump, SEEK_FORCECUR) == -1) {
5470  ast_log(LOG_WARNING, "Failed to perform seek in monitoring write stream, synchronization between the files may be broken\n");
5471  }
5473  } else {
5475  }
5476 #else
5477  int jump = calc_monitor_jump((ast_channel_insmpl(chan) - ast_channel_outsmpl(chan)),
5480  if (jump - MONITOR_DELAY >= 0) {
5481  if (ast_seekstream(ast_channel_monitor(chan)->write_stream, jump - cur->samples, SEEK_FORCECUR) == -1) {
5482  ast_log(LOG_WARNING, "Failed to perform seek in monitoring write stream, synchronization between the files may be broken\n");
5483  }
5485  } else {
5487  }
5488 #endif
5490  if (ast_writestream(ast_channel_monitor(chan)->write_stream, cur) < 0)
5491  ast_log(LOG_WARNING, "Failed to write data to channel monitor write stream\n");
5492  }
5493  }
5494  }
5495 
5496  /* the translator on chan->writetrans may have returned multiple frames
5497  from the single frame we passed in; if so, feed each one of them to the
5498  channel, freeing each one after it has been written */
5499  CHECK_BLOCKING(chan);
5500  if ((f != fr) && AST_LIST_NEXT(f, frame_list)) {
5501  struct ast_frame *cur, *next = NULL;
5502  unsigned int skip = 0;
5503 
5504  cur = f;
5505  while (cur) {
5506  next = AST_LIST_NEXT(cur, frame_list);
5507  AST_LIST_NEXT(cur, frame_list) = NULL;
5508  if (!skip) {
5509  res = tech_write(chan, stream, default_stream, cur);
5510  if (res < 0) {
5512  skip = 1;
5513  } else if (next) {
5514  /* don't do this for the last frame in the list,
5515  as the code outside the loop will do it once
5516  */
5518  }
5519  }
5520  ast_frfree(cur);
5521  cur = next;
5522  }
5523 
5524  /* reset f so the code below doesn't attempt to free it */
5525  f = NULL;
5526  } else {
5527  res = tech_write(chan, stream, default_stream, f);
5528  }
5530  break;
5531  case AST_FRAME_NULL:
5532  case AST_FRAME_IAX:
5533  /* Ignore these */
5534  res = 0;
5535  break;
5536  case AST_FRAME_RTCP:
5537  /* RTCP information is on a per-stream basis and only available on multistream capable channels */
5538  CHECK_BLOCKING(chan);
5539  if (ast_channel_tech(chan)->write_stream && stream) {
5540  res = ast_channel_tech(chan)->write_stream(chan, ast_stream_get_position(stream), fr);
5541  } else {
5542  res = 0;
5543  }
5545  break;
5546  default:
5547  /* At this point, fr is the incoming frame and f is NULL. Channels do
5548  * not expect to get NULL as a frame pointer and will segfault. Hence,
5549  * we output the original frame passed in. */
5550  CHECK_BLOCKING(chan);
5551  res = ast_channel_tech(chan)->write(chan, fr);
5553  break;
5554  }
5555 
5556  if (f && f != fr)
5557  ast_frfree(f);
5558 
5559  /* Consider a write failure to force a soft hangup */
5560  if (res < 0) {
5562  } else {
5564  }
5565 done:
5567  /* The list gets recreated if audiohooks are added again later */
5570  }
5571  ast_channel_unlock(chan);
5572  return res;
5573 }
5574 
5575 int ast_set_read_format_path(struct ast_channel *chan, struct ast_format *raw_format, struct ast_format *core_format)
5576 {
5577  struct ast_trans_pvt *trans_old;
5578  struct ast_trans_pvt *trans_new;
5579 
5581  && ast_format_cmp(ast_channel_readformat(chan), core_format) == AST_FORMAT_CMP_EQUAL) {
5582  /* Nothing to setup */
5583  return 0;
5584  }
5585 
5586  ast_debug(1, "Channel %s setting read format path: %s -> %s\n",
5587  ast_channel_name(chan),
5588  ast_format_get_name(raw_format),
5589  ast_format_get_name(core_format));
5590 
5591  /* Setup new translation path. */
5592  if (ast_format_cmp(raw_format, core_format) != AST_FORMAT_CMP_EQUAL) {
5593  trans_new = ast_translator_build_path(core_format, raw_format);
5594  if (!trans_new) {
5595  return -1;
5596  }
5597  } else {
5598  /* No translation needed. */
5599  trans_new = NULL;
5600  }
5601  trans_old = ast_channel_readtrans(chan);
5602  if (trans_old) {
5603  ast_translator_free_path(trans_old);
5604  }
5605  ast_channel_readtrans_set(chan, trans_new);
5606  ast_channel_set_rawreadformat(chan, raw_format);
5607  ast_channel_set_readformat(chan, core_format);
5608  return 0;
5609 }
5610 
5611 int ast_set_write_format_path(struct ast_channel *chan, struct ast_format *core_format, struct ast_format *raw_format)
5612 {
5613  struct ast_trans_pvt *trans_old;
5614  struct ast_trans_pvt *trans_new;
5615 
5617  && ast_format_cmp(ast_channel_writeformat(chan), core_format) == AST_FORMAT_CMP_EQUAL) {
5618  /* Nothing to setup */
5619  return 0;
5620  }
5621 
5622  ast_debug(1, "Channel %s setting write format path: %s -> %s\n",
5623  ast_channel_name(chan),
5624  ast_format_get_name(core_format),
5625  ast_format_get_name(raw_format));
5626 
5627  /* Setup new translation path. */
5628  if (ast_format_cmp(raw_format, core_format) != AST_FORMAT_CMP_EQUAL) {
5629  trans_new = ast_translator_build_path(raw_format, core_format);
5630  if (!trans_new) {
5631  return -1;
5632  }
5633  } else {
5634  /* No translation needed. */
5635  trans_new = NULL;
5636  }
5637  trans_old = ast_channel_writetrans(chan);
5638  if (trans_old) {
5639  ast_translator_free_path(trans_old);
5640  }
5641  ast_channel_writetrans_set(chan, trans_new);
5642  ast_channel_set_rawwriteformat(chan, raw_format);
5643  ast_channel_set_writeformat(chan, core_format);
5644  return 0;
5645 }
5646 
5648  const char *direction;
5649  struct ast_trans_pvt *(*get_trans)(const struct ast_channel *chan);
5650  void (*set_trans)(struct ast_channel *chan, struct ast_trans_pvt *value);
5651  struct ast_format *(*get_format)(struct ast_channel *chan);
5652  void (*set_format)(struct ast_channel *chan, struct ast_format *format);
5653  struct ast_format *(*get_rawformat)(struct ast_channel *chan);
5654  void (*set_rawformat)(struct ast_channel *chan, struct ast_format *format);
5656 };
5657 
5659  .direction = "read",
5660  .get_trans = ast_channel_readtrans,
5661  .set_trans = ast_channel_readtrans_set,
5662  .get_format = ast_channel_readformat,
5663  .set_format = ast_channel_set_readformat,
5664  .get_rawformat = ast_channel_rawreadformat,
5665  .set_rawformat = ast_channel_set_rawreadformat,
5666  .setoption = AST_OPTION_FORMAT_READ,
5667 };
5668 
5670  .direction = "write",
5671  .get_trans = ast_channel_writetrans,
5672  .set_trans = ast_channel_writetrans_set,
5673  .get_format = ast_channel_writeformat,
5674  .set_format = ast_channel_set_writeformat,
5675  .get_rawformat = ast_channel_rawwriteformat,
5676  .set_rawformat = ast_channel_set_rawwriteformat,
5677  .setoption = AST_OPTION_FORMAT_WRITE,
5678 };
5679 
5680 static int set_format(struct ast_channel *chan, struct ast_format_cap *cap_set, const int direction, int interleaved_stereo)
5681 {
5682  struct ast_trans_pvt *trans_pvt;
5683  struct ast_format_cap *cap_native;
5684  const struct set_format_access *access;
5685  struct ast_format *rawformat;
5686  struct ast_format *format;
5687  RAII_VAR(struct ast_format *, best_set_fmt, NULL, ao2_cleanup);
5688  RAII_VAR(struct ast_format *, best_native_fmt, NULL, ao2_cleanup);
5689  int res;
5690 
5691  if (!direction) {
5692  /* reading */
5693  access = &set_format_access_read;
5694  } else {
5695  /* writing */
5696  access = &set_format_access_write;
5697  }
5698 
5699  best_set_fmt = ast_format_cap_get_best_by_type(cap_set, AST_MEDIA_TYPE_AUDIO);
5700  if (!best_set_fmt) {
5701  /*
5702  * Not setting any audio formats?
5703  * Assume a call without any sounds (video, text)
5704  */
5705  return 0;
5706  }
5707 
5708  /* See if the underlying channel driver is capable of performing transcoding for us */
5709  res = ast_channel_setoption(chan, access->setoption,
5710  &best_set_fmt, sizeof(best_set_fmt), 0);
5711  if (!res) {
5712  ast_debug(1, "Channel driver natively set channel %s to %s format %s\n",
5713  ast_channel_name(chan), access->direction, ast_format_get_name(best_set_fmt));
5714 
5715  ast_channel_lock(chan);
5717  if (!cap_native
5718  || ast_format_cap_append(cap_native, best_set_fmt, 0)) {
5719  ast_channel_unlock(chan);
5720  ao2_cleanup(cap_native);
5721  return -1;
5722  }
5723  ast_channel_nativeformats_set(chan, cap_native);
5724  ao2_cleanup(cap_native);
5725  access->set_format(chan, best_set_fmt);
5726  access->set_rawformat(chan, best_set_fmt);
5727 
5728  trans_pvt = access->get_trans(chan);
5729  if (trans_pvt) {
5730  ast_translator_free_path(trans_pvt);
5731  access->set_trans(chan, NULL);
5732  }
5733  ast_channel_unlock(chan);
5734 
5735  /* If there is a generator on the channel, it needs to know about this
5736  * change if it is the write format. */
5737  if (direction && ast_channel_generatordata(chan)) {
5739  }
5740 
5741  return 0;
5742  }
5743 
5744  ast_channel_lock(chan);
5745 
5746  format = access->get_format(chan);
5747  rawformat = access->get_rawformat(chan);
5748  ast_assert(format != NULL);
5749  ast_assert(rawformat != NULL);
5750 
5751  cap_native = ast_channel_nativeformats(chan);
5752  if (ast_format_cap_empty(cap_native)) {
5753  ast_channel_unlock(chan);
5754  ast_log(LOG_ERROR, "Unable to set format because channel %s supports no formats\n",
5755  ast_channel_name(chan));
5756  return -1;
5757  }
5758 
5759  /* Find a translation path from the native format to one of the desired formats */
5760  if (!direction) {
5761  /* reading */
5762  res = ast_translator_best_choice(cap_set, cap_native, &best_set_fmt, &best_native_fmt);
5763  } else {
5764  /* writing */
5765  res = ast_translator_best_choice(cap_native, cap_set, &best_native_fmt, &best_set_fmt);
5766  }
5767  if (res < 0) {
5768  struct ast_str *codec_native = ast_str_alloca(AST_FORMAT_CAP_NAMES_LEN);
5769  struct ast_str *codec_set = ast_str_alloca(AST_FORMAT_CAP_NAMES_LEN);
5770 
5771  ast_format_cap_get_names(cap_native, &codec_native);
5772  ast_channel_unlock(chan);
5773  ast_format_cap_get_names(cap_set, &codec_set);
5774 
5775  ast_log(LOG_WARNING, "Unable to find a codec translation path: %s -> %s\n",
5776  ast_str_buffer(direction ? codec_set : codec_native),
5777  ast_str_buffer(direction ? codec_native : codec_set));
5778  return -1;
5779  }
5780 
5781  /* Now we have a good choice for both. */
5782  trans_pvt = access->get_trans(chan);
5783  if ((ast_format_cmp(rawformat, best_native_fmt) != AST_FORMAT_CMP_NOT_EQUAL) &&
5784  (ast_format_cmp(format, best_set_fmt) != AST_FORMAT_CMP_NOT_EQUAL) &&
5785  ((ast_format_cmp(rawformat, format) != AST_FORMAT_CMP_NOT_EQUAL) || access->get_trans(chan))) {
5786  /* the channel is already in these formats, so nothing to do, unless the interleaved format is not set correctly */
5787  if (trans_pvt != NULL) {
5788  if (trans_pvt->interleaved_stereo == interleaved_stereo) {
5789  ast_channel_unlock(chan);
5790  return 0;
5791  }
5792  }
5793  }
5794 
5795  /* Free any translation we have right now */
5796  if (trans_pvt) {
5797  ast_translator_free_path(trans_pvt);
5798  access->set_trans(chan, NULL);
5799  }
5800 
5801  /* Build a translation path from the raw format to the desired format */
5802  if (ast_format_cmp(best_set_fmt, best_native_fmt) != AST_FORMAT_CMP_NOT_EQUAL) {
5803  /*
5804  * If we were able to swap the native format to the format that
5805  * has been requested, then there is no need to try to build
5806  * a translation path.
5807  */
5808  res = 0;
5809  } else {
5810  if (!direction) {
5811  /* reading */
5812  trans_pvt = ast_translator_build_path(best_set_fmt, best_native_fmt);
5813  if (trans_pvt) {
5814  trans_pvt->interleaved_stereo = 0;
5815  }
5816  } else {
5817  /* writing */
5818  trans_pvt = ast_translator_build_path(best_native_fmt, best_set_fmt);
5819  if (trans_pvt) {
5820  trans_pvt->interleaved_stereo = interleaved_stereo;
5821  }
5822  }
5823  access->set_trans(chan, trans_pvt);
5824  res = trans_pvt ? 0 : -1;
5825  }
5826 
5827  if (!res) {
5828  access->set_format(chan, best_set_fmt);
5829  access->set_rawformat(chan, best_native_fmt);
5830 
5831  ast_debug(1, "Channel %s setting %s format path: %s -> %s\n",
5832  ast_channel_name(chan),
5833  access->direction,
5834  ast_format_get_name(direction ? best_set_fmt : best_native_fmt),
5835  ast_format_get_name(direction ? best_native_fmt : best_set_fmt));
5836  }
5837 
5838  ast_channel_unlock(chan);
5839 
5840  /* If there is a generator on the channel, it needs to know about this
5841  * change if it is the write format. */
5842  if (direction && ast_channel_generatordata(chan)) {
5844  }
5845 
5846  return res;
5847 }
5848 
5850 {
5852  int res;
5853 
5854  ast_assert(format != NULL);
5855 
5856  if (!cap) {
5857  return -1;
5858  }
5859  ast_format_cap_append(cap, format, 0);
5860 
5861  res = set_format(chan, cap, 0, 0);
5862 
5863  ao2_cleanup(cap);
5864  return res;
5865 }
5866 
5868 {
5869  return set_format(chan, cap, 0, 0);
5870 }
5871 
5873 {
5875  int res;
5876 
5877  ast_assert(format != NULL);
5878 
5879  if (!cap) {
5880  return -1;
5881  }
5882  ast_format_cap_append(cap, format, 0);
5883 
5884  res = set_format(chan, cap, 1, 1);
5885 
5886  ao2_cleanup(cap);
5887  return res;
5888 }
5889 
5891 {
5893  int res;
5894 
5895  ast_assert(format != NULL);
5896 
5897  if (!cap) {
5898  return -1;
5899  }
5900  ast_format_cap_append(cap, format, 0);
5901 
5902  res = set_format(chan, cap, 1, 0);
5903 
5904  ao2_cleanup(cap);
5905  return res;
5906 }
5907 
5909 {
5910  return set_format(chan, cap, 1, 0);
5911 }
5912 
5913 const char *ast_channel_reason2str(int reason)
5914 {
5915  switch (reason) /* the following appear to be the only ones actually returned by request_and_dial */
5916  {
5917  case 0:
5918  return "Call Failure (not BUSY, and not NO_ANSWER, maybe Circuit busy or down?)";
5919  case AST_CONTROL_HANGUP:
5920  return "Hangup";
5921  case AST_CONTROL_RING:
5922  return "Local Ring";
5923  case AST_CONTROL_RINGING:
5924  return "Remote end Ringing";
5925  case AST_CONTROL_ANSWER:
5926  return "Remote end has Answered";
5927  case AST_CONTROL_BUSY:
5928  return "Remote end is Busy";
5930  return "Congestion (circuits busy)";
5931  default:
5932  return "Unknown Reason!!";
5933  }
5934 }
5935 
5936 static void handle_cause(int cause, int *outstate)
5937 {
5938  if (outstate) {
5939  /* compute error and return */
5940  if (cause == AST_CAUSE_BUSY)
5941  *outstate = AST_CONTROL_BUSY;
5942  else if (cause == AST_CAUSE_CONGESTION)
5943  *outstate = AST_CONTROL_CONGESTION;
5944  else
5945  *outstate = 0;
5946  }
5947 }
5948 
5949 /*!
5950  * \internal
5951  * \brief Helper function to inherit info from parent channel.
5952  *
5953  * \param new_chan Channel inheriting information.
5954  * \param parent Channel new_chan inherits information.
5955  * \param orig Channel being replaced by the call forward channel.
5956  *
5957  * \return Nothing
5958  */
5959 static void call_forward_inherit(struct ast_channel *new_chan, struct ast_channel *parent, struct ast_channel *orig)
5960 {
5961  if (!ast_test_flag(ast_channel_flags(parent), AST_FLAG_ZOMBIE) && !ast_check_hangup(parent)) {
5962  struct ast_party_redirecting redirecting;
5963 
5964  /*
5965  * The parent is not a ZOMBIE or hungup so update it with the
5966  * original channel's redirecting information.
5967  */
5968  ast_party_redirecting_init(&redirecting);
5969  ast_channel_lock(orig);
5971  ast_channel_unlock(orig);
5972  if (ast_channel_redirecting_sub(orig, parent, &redirecting, 0) &&
5973  ast_channel_redirecting_macro(orig, parent, &redirecting, 1, 0)) {
5974  ast_channel_update_redirecting(parent, &redirecting, NULL);
5975  }
5976  ast_party_redirecting_free(&redirecting);
5977  }
5978 
5979  /* Safely inherit variables and datastores from the parent channel. */
5980  ast_channel_lock_both(parent, new_chan);
5981  ast_channel_inherit_variables(parent, new_chan);
5982  ast_channel_datastore_inherit(parent, new_chan);
5983  ast_max_forwards_decrement(new_chan);
5984  ast_channel_unlock(new_chan);
5985  ast_channel_unlock(parent);
5986 }
5987 
5988 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)
5989 {
5990  char tmpchan[256];
5991  char forwarder[AST_CHANNEL_NAME];
5992  struct ast_channel *new_chan = NULL;
5993  char *data, *type;
5994  int cause = 0;
5995  int res;
5996 
5997  /* gather data and request the new forward channel */
5998  ast_copy_string(tmpchan, ast_channel_call_forward(orig), sizeof(tmpchan));
5999  ast_copy_string(forwarder, ast_channel_name(orig), sizeof(forwarder));
6000  if ((data = strchr(tmpchan, '/'))) {
6001  *data++ = '\0';
6002  type = tmpchan;
6003  } else {
6004  const char *forward_context;
6005  ast_channel_lock(orig);
6006  forward_context = pbx_builtin_getvar_helper(orig, "FORWARD_CONTEXT");
6007  snprintf(tmpchan, sizeof(tmpchan), "%s@%s", ast_channel_call_forward(orig), S_OR(forward_context, ast_channel_context(orig)));
6008  ast_channel_unlock(orig);
6009  data = tmpchan;
6010  type = "Local";
6011  }
6012  if (!(new_chan = ast_request(type, cap, NULL, orig, data, &cause))) {
6013  ast_log(LOG_NOTICE, "Unable to create channel for call forward to '%s/%s' (cause = %d)\n", type, data, cause);
6014  handle_cause(cause, outstate);
6015  ast_hangup(orig);
6016  return NULL;
6017  }
6018 
6019  /* Copy/inherit important information into new channel */
6020  if (oh) {
6021  if (oh->vars) {
6022  ast_channel_lock(new_chan);
6023  ast_set_variables(new_chan, oh->vars);
6024  ast_channel_unlock(new_chan);
6025  }
6026  if (oh->parent_channel) {
6027  call_forward_inherit(new_chan, oh->parent_channel, orig);
6028  }
6029  if (!ast_strlen_zero(oh->account)) {
6030  ast_channel_lock(new_chan);
6031  ast_channel_stage_snapshot(new_chan);
6032  ast_channel_accountcode_set(new_chan, oh->account);
6033  ast_channel_peeraccount_set(new_chan, oh->account);
6035  ast_channel_unlock(new_chan);
6036  }
6037  } else if (caller) { /* no outgoing helper so use caller if available */
6038  call_forward_inherit(new_chan, caller, orig);
6039  }
6040 
6041  ast_channel_lock_both(orig, new_chan);
6043  pbx_builtin_setvar_helper(new_chan, "FORWARDERNAME", forwarder);
6047  ast_channel_unlock(new_chan);
6048  ast_channel_unlock(orig);
6049 
6050  /* call new channel */
6051  res = ast_call(new_chan, data, 0);
6052  if (timeout) {
6053  *timeout = res;
6054  }
6055  if (res) {
6056  ast_log(LOG_NOTICE, "Unable to call forward to channel %s/%s\n", type, (char *)data);
6057  ast_hangup(orig);
6058  ast_hangup(new_chan);
6059  return NULL;
6060  }
6061  ast_hangup(orig);
6062 
6063  return new_chan;
6064 }
6065 
6066 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)
6067 {
6068  int dummy_outstate;
6069  int cause = 0;
6070  struct ast_channel *chan;
6071  int res = 0;
6072  int last_subclass = 0;
6073  struct ast_party_connected_line connected;
6074 
6075  if (outstate)
6076  *outstate = 0;
6077  else
6078  outstate = &dummy_outstate; /* make outstate always a valid pointer */
6079 
6080  chan = ast_request(type, cap, assignedids, requestor, addr, &cause);
6081  if (!chan) {
6082  ast_log(LOG_NOTICE, "Unable to request channel %s/%s\n", type, addr);
6083  handle_cause(cause, outstate);
6084  return NULL;
6085  }
6086 
6087  if (oh) {
6088  if (oh->vars) {
6089  ast_channel_lock(chan);
6090  ast_set_variables(chan, oh->vars);
6091  ast_channel_unlock(chan);
6092  }
6093  if (!ast_strlen_zero(oh->cid_num) && !ast_strlen_zero(oh->cid_name)) {
6094  /*
6095  * Use the oh values instead of the function parameters for the
6096  * outgoing CallerID.
6097  */
6098  cid_num = oh->cid_num;
6099  cid_name = oh->cid_name;
6100  }
6101  if (oh->parent_channel) {
6102  /* Safely inherit variables and datastores from the parent channel. */
6108  ast_channel_unlock(chan);
6109  }
6110  if (!ast_strlen_zero(oh->account)) {
6111  ast_channel_lock(chan);
6113  ast_channel_accountcode_set(chan, oh->account);
6114  ast_channel_peeraccount_set(chan, oh->account);
6116  ast_channel_unlock(chan);
6117  }
6118  }
6119 
6120  /*
6121  * I seems strange to set the CallerID on an outgoing call leg
6122  * to whom we are calling, but this function's callers are doing
6123  * various Originate methods. This call leg goes to the local
6124  * user. Once the local user answers, the dialplan needs to be
6125  * able to access the CallerID from the CALLERID function as if
6126  * the local user had placed this call.
6127  */
6128  ast_set_callerid(chan, cid_num, cid_name, cid_num);
6129 
6130  ast_channel_lock(chan);
6132  ast_channel_unlock(chan);
6134  if (cid_num) {
6135  connected.id.number.valid = 1;
6136  connected.id.number.str = (char *) cid_num;
6138  }
6139  if (cid_name) {
6140  connected.id.name.valid = 1;
6141  connected.id.name.str = (char *) cid_name;
6143  }
6144  ast_channel_set_connected_line(chan, &connected, NULL);
6145  if (requestor) {
6146  ast_channel_lock_both(chan, (struct ast_channel *) requestor);
6148  ast_channel_unlock(chan);
6149  ast_channel_unlock((struct ast_channel *) requestor);
6150  }
6151 
6152  if (ast_call(chan, addr, 0)) { /* ast_call failed... */
6153  ast_log(LOG_NOTICE, "Unable to call channel %s/%s\n", type, addr);
6154  } else {
6155  struct timeval start = ast_tvnow();
6156  res = 1; /* mark success in case chan->_state is already AST_STATE_UP */
6157  while (timeout && ast_channel_state(chan) != AST_STATE_UP) {
6158  struct ast_frame *f;
6159  int ms = ast_remaining_ms(start, timeout);
6160 
6161  res = ast_waitfor(chan, ms);
6162  if (res == 0) { /* timeout, treat it like ringing */
6163  *outstate = AST_CONTROL_RINGING;
6164  break;
6165  }
6166  if (res < 0) /* error or done */
6167  break;
6169  if (!(chan = ast_call_forward(NULL, chan, NULL, cap, oh, outstate))) {
6170  return NULL;
6171  }
6172  continue;
6173  }
6174 
6175  f = ast_read(chan);
6176  if (!f) {
6177  *outstate = AST_CONTROL_HANGUP;
6178  res = 0;
6179  break;
6180  }
6181  if (f->frametype == AST_FRAME_CONTROL) {
6182  switch (f->subclass.integer) {
6183  case AST_CONTROL_RINGING: /* record but keep going */
6184  *outstate = f->subclass.integer;
6185  break;
6186 
6187  case AST_CONTROL_BUSY:
6188  *outstate = f->subclass.integer;
6189  timeout = 0;
6190  break;
6191 
6193  *outstate = AST_CONTROL_CONGESTION;
6194  timeout = 0;
6195  break;
6196 
6198  *outstate = f->subclass.integer;
6199  timeout = 0;
6200  break;
6201 
6202  case AST_CONTROL_ANSWER:
6203  *outstate = f->subclass.integer;
6204  timeout = 0; /* trick to force exit from the while() */
6205  break;
6206 
6209  break;
6210 
6211  case AST_CONTROL_PROGRESS:
6212  if (oh && oh->connect_on_early_media) {
6213  *outstate = f->subclass.integer;
6214  timeout = 0; /* trick to force exit from the while() */
6215  break;
6216  }
6217  /* Fallthrough */
6218  /* Ignore these */
6220  case AST_CONTROL_HOLD:
6221  case AST_CONTROL_UNHOLD:
6222  case AST_CONTROL_VIDUPDATE:
6223  case AST_CONTROL_SRCUPDATE:
6224  case AST_CONTROL_SRCCHANGE:
6227  case AST_CONTROL_CC:
6228  case -1: /* Ignore -- just stopping indications */
6229  break;
6230 
6231  default:
6232  ast_log(LOG_NOTICE, "Don't know what to do with control frame %d\n", f->subclass.integer);
6233  }
6234  last_subclass = f->subclass.integer;
6235  }
6236  ast_frfree(f);
6237  }
6238  }
6239 
6240  /* Final fixups */
6241  if (oh) {
6242  if (!ast_strlen_zero(oh->context))
6243  ast_channel_context_set(chan, oh->context);
6244  if (!ast_strlen_zero(oh->exten))
6245  ast_channel_exten_set(chan, oh->exten);
6246  if (oh->priority)
6248  }
6249  if (ast_channel_state(chan) == AST_STATE_UP)
6250  *outstate = AST_CONTROL_ANSWER;
6251 
6252  if (res <= 0) {
6253  ast_channel_lock(chan);
6254  if (AST_CONTROL_RINGING == last_subclass) {
6256  }
6257  ast_channel_unlock(chan);
6258  ast_hangup(chan);
6259  chan = NULL;
6260  }
6261  return chan;
6262 }
6263 
6264 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)
6265 {
6266  return __ast_request_and_dial(type, cap, assignedids, requestor, addr, timeout, outstate, cidnum, cidname, NULL);
6267 }
6268 
6269 static int set_security_requirements(const struct ast_channel *requestor, struct ast_channel *out)
6270 {
6271  int ops[2][2] = {
6274  };
6275  int i;
6276  struct ast_channel *r = (struct ast_channel *) requestor; /* UGLY */
6277  struct ast_datastore *ds;
6278 
6279  if (!requestor || !out) {
6280  return 0;
6281  }
6282 
6283  ast_channel_lock(r);
6285  struct ast_secure_call_store *encrypt = ds->data;
6286  ops[0][1] = encrypt->signaling;
6287  ops[1][1] = encrypt->media;
6288  } else {
6289  ast_channel_unlock(r);
6290  return 0;
6291  }
6292  ast_channel_unlock(r);
6293 
6294  for (i = 0; i < 2; i++) {
6295  if (ops[i][1]) {
6296  if (ast_channel_setoption(out, ops[i][0], &ops[i][1], sizeof(ops[i][1]), 0)) {
6297  /* We require a security feature, but the channel won't provide it */
6298  return -1;
6299  }
6300  } else {
6301  /* We don't care if we can't clear the option on a channel that doesn't support it */
6302  ast_channel_setoption(out, ops[i][0], &ops[i][1], sizeof(ops[i][1]), 0);
6303  }
6304  }
6305 
6306  return 0;
6307 }
6308 
6309 static struct ast_channel *request_channel(const char *type, struct ast_format_cap *request_cap, struct ast_stream_topology *topology,
6310  const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor, const char *addr, int *cause)
6311 {
6312  struct chanlist *chan;
6313  struct ast_channel *c = NULL;
6314  int res;
6315  int foo;
6316 
6317  if (!cause)
6318  cause = &foo;
6319  *cause = AST_CAUSE_NOTDEFINED;
6320 
6321  if (AST_RWLIST_RDLOCK(&backends)) {
6322  ast_log(LOG_WARNING, "Unable to lock technology backend list\n");
6323  return NULL;
6324  }
6325 
6326  AST_RWLIST_TRAVERSE(&backends, chan, list) {
6327  if (strcasecmp(type, chan->tech->type)) {
6328  continue;
6329  }
6330 
6331  break;
6332  }
6333 
6335 
6336  if (!chan) {
6337  ast_log(LOG_WARNING, "No channel type registered for '%s'\n", type);
6338  *cause = AST_CAUSE_NOSUCHDRIVER;
6339  return NULL;
6340  }
6341 
6342  /* Allow either format capabilities or stream topology to be provided and adapt */
6343  if (chan->tech->requester_with_stream_topology) {
6344  struct ast_stream_topology *tmp_converted_topology = NULL;
6345 
6346  if (!topology && request_cap) {
6347  /* Turn the requested capabilities into a stream topology */
6348  topology = tmp_converted_topology = ast_stream_topology_create_from_format_cap(request_cap);
6349  }
6350 
6351  c = chan->tech->requester_with_stream_topology(type, topology, assignedids, requestor, addr, cause);
6352 
6353  ast_stream_topology_free(tmp_converted_topology);
6354  } else if (chan->tech->requester) {
6355  struct ast_format_cap *tmp_converted_cap = NULL;
6356  struct ast_format_cap *tmp_cap;
6357  RAII_VAR(struct ast_format *, tmp_fmt, NULL, ao2_cleanup);
6358  RAII_VAR(struct ast_format *, best_audio_fmt, NULL, ao2_cleanup);
6359  struct ast_format_cap *joint_cap;
6360 
6361  if (!request_cap && topology) {
6362  /* Turn the request stream topology into capabilities */
6363  request_cap = tmp_converted_cap = ast_stream_topology_get_formats(topology);
6364  }
6365 
6366  /* find the best audio format to use */
6368  if (!tmp_cap) {
6370  return NULL;
6371  }
6372 
6374  if (!ast_format_cap_empty(tmp_cap)) {
6375  /* We have audio - is it possible to connect the various calls to each other?
6376  (Avoid this check for calls without audio, like text+video calls)
6377  */
6378  res = ast_translator_best_choice(tmp_cap, chan->tech->capabilities, &tmp_fmt, &best_audio_fmt);
6379  ao2_ref(tmp_cap, -1);
6380  if (res < 0) {
6381  struct ast_str *tech_codecs = ast_str_alloca(AST_FORMAT_CAP_NAMES_LEN);
6382  struct ast_str *request_codecs = ast_str_alloca(AST_FORMAT_CAP_NAMES_LEN);
6383 
6384  ast_log(LOG_WARNING, "No translator path exists for channel type %s (native %s) to %s\n", type,
6385  ast_format_cap_get_names(chan->tech->capabilities, &tech_codecs),
6386  ast_format_cap_get_names(request_cap, &request_codecs));
6388  ao2_cleanup(tmp_converted_cap);
6389  return NULL;
6390  }
6391  }
6392 
6393  /* XXX Only the audio format calculated as being the best for translation
6394  * purposes is used for the request. This is because we don't have the ability
6395  * to signal to the initiator which one of their codecs that was offered is
6396  * the one that was selected, particularly in a chain of Local channels.
6397  */
6399  if (!joint_cap) {
6400  ao2_cleanup(tmp_converted_cap);
6401  return NULL;
6402  }
6403  ast_format_cap_append_from_cap(joint_cap, request_cap, AST_MEDIA_TYPE_UNKNOWN);
6405  if (best_audio_fmt) { /* text+video call? then, this is NULL */
6406  ast_format_cap_append(joint_cap, best_audio_fmt, 0);
6407  }
6408  ao2_cleanup(tmp_converted_cap);
6409 
6410  c = chan->tech->requester(type, joint_cap, assignedids, requestor, addr, cause);
6411  ao2_ref(joint_cap, -1);
6412  }
6413 
6414  if (!c) {
6415  return NULL;
6416  }
6417 
6418  if (requestor) {
6419  ast_callid callid;
6420 
6421  ast_channel_lock_both(c, (struct ast_channel *) requestor);
6422 
6423  /* Set the newly created channel's callid to the same as the requestor. */
6424  callid = ast_channel_callid(requestor);
6425  if (callid) {
6426  ast_channel_callid_set(c, callid);
6427  }
6428 
6429  ast_channel_unlock(c);
6430  ast_channel_unlock((struct ast_channel *) requestor);
6431  }
6432 
6433  if (set_security_requirements(requestor, c)) {
6434  ast_log(LOG_WARNING, "Setting security requirements failed\n");
6435  ast_hangup(c);
6437  return NULL;
6438  }
6439 
6440  /* no need to generate a Newchannel event here; it is done in the channel_alloc call */
6441  return c;
6442 }
6443 
6444 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)
6445 {
6446  return request_channel(type, request_cap, NULL, assignedids, requestor, addr, cause);
6447 }
6448 
6449 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)
6450 {
6451  return request_channel(type, NULL, topology, assignedids, requestor, addr, cause);
6452 }
6453 
6454 /*!
6455  * \internal
6456  * \brief Setup new channel accountcodes from the requestor channel after ast_request().
6457  * \since 13.0.0
6458  *
6459  * \param chan New channel to get accountcodes setup.
6460  * \param requestor Requesting channel to get accountcodes from.
6461  * \param relationship What the new channel was created for.
6462  * \param precious TRUE if pre-existing accountcodes on chan will not be overwritten.
6463  *
6464  * \pre The chan and requestor channels are already locked.
6465  *
6466  * \return Nothing
6467  */
6468 static void channel_req_accountcodes(struct ast_channel *chan, const struct ast_channel *requestor, enum ast_channel_requestor_relationship relationship, int precious)
6469 {
6470  /*
6471  * The primary reason for the existence of this function is
6472  * so local channels can propagate accountcodes to the ;2
6473  * channel before ast_call().
6474  *
6475  * The secondary reason is to propagate the CHANNEL(peeraccount)
6476  * value set before Dial, FollowMe, and Queue while maintaining
6477  * the historic straight across accountcode propagation as a
6478  * fallback.
6479  */
6480  switch (relationship) {
6482  /* Crossover the requestor's accountcode and peeraccount */
6483  if (!precious || ast_strlen_zero(ast_channel_accountcode(chan))) {
6484  /*
6485  * The newly created channel does not have an accountcode
6486  * or we don't care.
6487  */
6488  if (!ast_strlen_zero(ast_channel_peeraccount(requestor))) {
6489  /*
6490  * Set it to the requestor's peeraccount. This allows the
6491  * dialplan to indicate the accountcode to use when dialing
6492  * by setting CHANNEL(peeraccount).
6493  */
6494  ast_channel_accountcode_set(chan, ast_channel_peeraccount(requestor));
6495  } else if (!precious
6496  && !ast_strlen_zero(ast_channel_accountcode(requestor))) {
6497  /*
6498  * Fallback to the historic propagation and set it to the
6499  * requestor's accountcode.
6500  */
6501  ast_channel_accountcode_set(chan, ast_channel_accountcode(requestor));
6502  }
6503  }
6504  if (!ast_strlen_zero(ast_channel_accountcode(requestor))) {
6505  ast_channel_peeraccount_set(chan, ast_channel_accountcode(requestor));
6506  }
6507  break;
6509  /* Pass the requestor's accountcode and peeraccount straight. */
6510  if (!precious || ast_strlen_zero(ast_channel_accountcode(chan))) {
6511  /*
6512  * The newly created channel does not have an accountcode
6513  * or we don't care.
6514  */
6515  if (!ast_strlen_zero(ast_channel_accountcode(requestor))) {
6516  ast_channel_accountcode_set(chan, ast_channel_accountcode(requestor));
6517  }
6518  }
6519  if (!ast_strlen_zero(ast_channel_peeraccount(requestor))) {
6520  ast_channel_peeraccount_set(chan, ast_channel_peeraccount(requestor));
6521  }
6522  break;
6523  }
6524 }
6525 
6526 void ast_channel_req_accountcodes(struct ast_channel *chan, const struct ast_channel *requestor, enum ast_channel_requestor_relationship relationship)
6527 {
6528  channel_req_accountcodes(chan, requestor, relationship, 0);
6529 }
6530 
6531 void ast_channel_req_accountcodes_precious(struct ast_channel *chan, const struct ast_channel *requestor, enum ast_channel_requestor_relationship relationship)
6532 {
6533  channel_req_accountcodes(chan, requestor, relationship, 1);
6534 }
6535 
6536 int ast_pre_call(struct ast_channel *chan, const char *sub_args)
6537 {
6538  int (*pre_call)(struct ast_channel *chan, const char *sub_args);
6539 
6540  ast_channel_lock(chan);
6541  pre_call = ast_channel_tech(chan)->pre_call;
6542  if (pre_call) {
6543  int res;
6544 
6545  res = pre_call(chan, sub_args);
6546  ast_channel_unlock(chan);
6547  return res;
6548  }
6549  ast_channel_unlock(chan);
6550  return ast_app_exec_sub(NULL, chan, sub_args, 0);
6551 }
6552 
6553 int ast_call(struct ast_channel *chan, const char *addr, int timeout)
6554 {
6555  /* Place an outgoing call, but don't wait any longer than timeout ms before returning.
6556  If the remote end does not answer within the timeout, then do NOT hang up, but
6557  return anyway. */
6558  int res = -1;
6559  /* Stop if we're a zombie or need a soft hangup */
6560  ast_channel_lock(chan);
6562  if (ast_channel_tech(chan)->call)
6563  res = ast_channel_tech(chan)->call(chan, addr, timeout);
6565  }
6566  ast_channel_unlock(chan);
6567  return res;
6568 }
6569 
6570 /*!
6571  \brief Transfer a call to dest, if the channel supports transfer
6572 
6573  Called by:
6574  \arg app_transfer
6575  \arg the manager interface
6576 */
6577 int ast_transfer(struct ast_channel *chan, char *dest)
6578 {
6579  int protocol;
6580  return ast_transfer_protocol(chan, dest, &protocol);
6581 }
6582 
6583 /*!
6584  \brief Transfer a call to dest, if the channel supports transfer
6585 
6586  \param chan channel to transfer
6587  \param dest destination to transfer to
6588  \param protocol is the protocol result
6589  SIP example, 0=success, 3xx-6xx is SIP error code
6590 
6591  Called by:
6592  \arg app_transfer
6593  \arg the manager interface
6594 */
6595 int ast_transfer_protocol(struct ast_channel *chan, char *dest, int *protocol)
6596 {
6597  int res = -1;
6598 
6599  if (protocol) {
6600  *protocol = 0;
6601  }
6602 
6603  /* Stop if we're a zombie or need a soft hangup */
6604  ast_channel_lock(chan);
6606  if (ast_channel_tech(chan)->transfer) {
6607  res = ast_channel_tech(chan)->transfer(chan, dest);
6608  if (!res)
6609  res = 1;
6610  } else
6611  res = 0;
6612  }
6613  ast_channel_unlock(chan);
6614 
6615  if (res <= 0) {
6616  return res;
6617  }
6618 
6619  for (;;) {
6620  struct ast_frame *fr;
6621 
6622  res = ast_waitfor(chan, -1);
6623 
6624  if (res < 0 || !(fr = ast_read(chan))) {
6625  res = -1;
6626  break;
6627  }
6628 
6630  enum ast_control_transfer *message = fr->data.ptr;
6631 
6632  if (*message == AST_TRANSFER_SUCCESS) {
6633  res = 1;
6634  } else {
6635  res = -1;
6636  /* Message can contain a protocol specific code
6637  AST_TRANSFER_SUCCESS indicates success
6638  Else, failure. Protocol will be set to the failure reason.
6639  SIP example, 0 is success, else error code 3xx-6xx */
6640  if (protocol) {
6641  *protocol = *message;
6642  }
6643  }
6644 
6645  ast_frfree(fr);
6646  break;
6647  }
6648 
6649  ast_frfree(fr);
6650  }
6651 
6652  return res;
6653 }
6654 
6655 int ast_readstring(struct ast_channel *c, char *s, int len, int timeout, int ftimeout, char *enders)
6656 {
6657  return ast_readstring_full(c, s, len, timeout, ftimeout, enders, -1, -1);
6658 }
6659 
6660 int ast_readstring_full(struct ast_channel *c, char *s, int len, int timeout, int ftimeout, char *enders, int audiofd, int ctrlfd)
6661 {
6662  int pos = 0; /* index in the buffer where we accumulate digits */
6663  int to = ftimeout;
6664 
6665  struct ast_silence_generator *silgen = NULL;
6666 
6667  /* Stop if we're a zombie or need a soft hangup */
6669  return -1;
6670  if (!len)
6671  return -1;
6672  for (;;) {
6673  int d;
6674  if (ast_channel_stream(c)) {
6675  d = ast_waitstream_full(c, AST_DIGIT_ANY, audiofd, ctrlfd);
6676  ast_stopstream(c);
6677  if (!silgen && ast_opt_transmit_silence)
6679  usleep(1000);
6680  if (!d)
6681  d = ast_waitfordigit_full(c, to, NULL, audiofd, ctrlfd);
6682  } else {
6683  if (!silgen && ast_opt_transmit_silence)
6685  d = ast_waitfordigit_full(c, to, NULL, audiofd, ctrlfd);
6686  }
6687  if (d < 0) {
6689  return AST_GETDATA_FAILED;
6690  }
6691  if (d == 0) {
6692  s[pos] = '\0';
6694  return AST_GETDATA_TIMEOUT;
6695  }
6696  if (d == 1) {
6697  s[pos] = '\0';
6699  return AST_GETDATA_INTERRUPTED;
6700  }
6701  if (strchr(enders, d) && (pos == 0)) {
6702  s[pos] = '\0';
6705  }
6706  if (!strchr(enders, d)) {
6707  s[pos++] = d;
6708  }
6709  if (strchr(enders, d) || (pos >= len)) {
6710  s[pos] = '\0';
6712  return AST_GETDATA_COMPLETE;
6713  }
6714  to = timeout;
6715  }
6716  /* Never reached */
6717  return 0;
6718 }
6719 
6721 {
6722  return (ast_channel_tech(chan)->send_html) ? 1 : 0;
6723 }
6724 
6725 int ast_channel_sendhtml(struct ast_channel *chan, int subclass, const char *data, int datalen)
6726 {
6727  if (ast_channel_tech(chan)->send_html)
6728  return ast_channel_tech(chan)->send_html(chan, subclass, data, datalen);
6729  return -1;
6730 }
6731 
6732 int ast_channel_sendurl(struct ast_channel *chan, const char *url)
6733 {
6734  return ast_channel_sendhtml(chan, AST_HTML_URL, url, strlen(url) + 1);
6735 }
6736 
6737 /*! \brief Set up translation from one channel to another */
6738 static int ast_channel_make_compatible_helper(struct ast_channel *from, struct ast_channel *to)
6739 {
6740  struct ast_format_cap *src_cap;
6741  struct ast_format_cap *dst_cap;
6742  RAII_VAR(struct ast_format *, best_src_fmt, NULL, ao2_cleanup);
6743  RAII_VAR(struct ast_format *, best_dst_fmt, NULL, ao2_cleanup);
6744  int no_path;
6745 
6746  /*
6747  * We cannot short circuit this code because it is possible to ask
6748  * to make compatible two channels that are "compatible" because
6749  * they already have translation paths setup but together make for
6750  * a sub-optimal path. e.g., The From channel has g722 -> ulaw
6751  * and the To channel has ulaw -> g722. They are "compatible" but
6752  * together the translations are unnecessary and the audio loses
6753  * fidelity in the process.
6754  */
6755 
6756  ast_channel_lock_both(from, to);
6757 
6758  src_cap = ast_channel_nativeformats(from); /* shallow copy, do not destroy */
6759  dst_cap = ast_channel_nativeformats(to); /* shallow copy, do not destroy */
6760 
6761  /* If there's no audio in this call, don't bother with trying to find a translation path */
6764  ast_channel_unlock(to);
6765  ast_channel_unlock(from);
6766  return 0;
6767  }
6768 
6769  no_path = ast_translator_best_choice(dst_cap, src_cap, &best_dst_fmt, &best_src_fmt);
6770 
6771  ast_channel_unlock(to);
6772  ast_channel_unlock(from);
6773 
6774  if (no_path) {
6775  ast_log(LOG_WARNING, "No path to translate from %s to %s\n",
6776  ast_channel_name(from), ast_channel_name(to));
6777  return -1;
6778  }
6779 
6780  /* if the best path is not 'pass through', then
6781  * transcoding is needed; if desired, force transcode path
6782  * to use SLINEAR between channels, but only if there is
6783  * no direct conversion available. If generic PLC is
6784  * desired, then transcoding via SLINEAR is a requirement
6785  * even if the formats are the same.
6786  */
6788  || (ast_format_cmp(best_dst_fmt, best_src_fmt) == AST_FORMAT_CMP_NOT_EQUAL
6790 
6791  int use_slin = (ast_format_cache_is_slinear(best_src_fmt)
6792  || ast_format_cache_is_slinear(best_dst_fmt))
6794 
6795  if (use_slin || ast_translate_path_steps(best_dst_fmt, best_src_fmt) != 1) {
6796  int best_sample_rate = (ast_format_get_sample_rate(best_src_fmt) > ast_format_get_sample_rate(best_dst_fmt)) ?
6797  ast_format_get_sample_rate(best_src_fmt) : ast_format_get_sample_rate(best_dst_fmt);
6798 
6799  /* pick the best signed linear format based upon what preserves the sample rate the best. */
6800  ao2_replace(best_src_fmt, ast_format_cache_get_slin_by_rate(best_sample_rate));
6801  }
6802  }
6803 
6804  if (ast_set_read_format(from, best_src_fmt)) {
6805  ast_log(LOG_WARNING, "Unable to set read format on channel %s to %s\n",
6806  ast_channel_name(from), ast_format_get_name(best_src_fmt));
6807  return -1;
6808  }
6809  if (ast_set_write_format(to, best_src_fmt)) {
6810  ast_log(LOG_WARNING, "Unable to set write format on channel %s to %s\n",
6811  ast_channel_name(to), ast_format_get_name(best_src_fmt));
6812  return -1;
6813  }
6814  return 0;
6815 }
6816 
6817 int ast_channel_make_compatible(struct ast_channel *chan, struct ast_channel *peer)
6818 {
6819  /*
6820  * Set up translation from the peer to the chan first in case we
6821  * need to hear any in-band tones and the other direction fails.
6822  */
6823  if (ast_channel_make_compatible_helper(peer, chan)) {
6824  return -1;
6825  }
6826 
6827  /* Set up translation from the chan to the peer */
6828  if (ast_channel_make_compatible_helper(chan, peer)) {
6829  return -1;
6830  }
6831 
6832  return 0;
6833 }
6834 
6835 /*! \brief this function simply changes the name of the channel and issues a manager_event
6836  * with out unlinking and linking the channel from the ao2_container. This should
6837  * only be used when the channel has already been unlinked from the ao2_container.
6838  */
6839 static void __ast_change_name_nolink(struct ast_channel *chan, const char *newname)
6840 {
6841  /*** DOCUMENTATION
6842  <managerEventInstance>
6843  <synopsis>Raised when the name of a channel is changed.</synopsis>
6844  </managerEventInstance>
6845  ***/
6846  ast_manager_event(chan, EVENT_FLAG_CALL, "Rename",
6847  "Channel: %s\r\n"
6848  "Newname: %s\r\n"
6849  "Uniqueid: %s\r\n",
6850  ast_channel_name(chan), newname, ast_channel_uniqueid(chan));
6851  ast_channel_name_set(chan, newname);
6852 }
6853 
6854 void ast_change_name(struct ast_channel *chan, const char *newname)
6855 {
6856  /* We must re-link, as the hash value will change here. */
6857  ao2_lock(channels);
6858  ast_channel_lock(chan);
6859  ao2_unlink(channels, chan);
6860  __ast_change_name_nolink(chan, newname);
6861  ao2_link(channels, chan);
6862  ast_channel_unlock(chan);
6863  ao2_unlock(channels);
6864 }
6865 
6866 void ast_channel_inherit_variables(const struct ast_channel *parent, struct ast_channel *child)
6867 {
6868  struct ast_var_t *current;
6869  struct ast_var_t *newvar;
6870  const char *varname;
6871  int vartype;
6872 
6873  AST_LIST_TRAVERSE(ast_channel_varshead((struct ast_channel *) parent), current, entries) {
6874  varname = ast_var_full_name(current);
6875  if (!varname) {
6876  continue;
6877  }
6878 
6879  vartype = 0;
6880  if (varname[0] == '_') {
6881  vartype = 1;
6882  if (varname[1] == '_') {
6883  vartype = 2;
6884  }
6885  }
6886 
6887  switch (vartype) {
6888  case 1:
6889  newvar = ast_var_assign(&varname[1], ast_var_value(current));
6890  break;
6891  case 2:
6892  newvar = ast_var_assign(varname, ast_var_value(current));
6893  break;
6894  default:
6895  continue;
6896  }
6897  if (newvar) {
6898  ast_debug(1, "Inheriting variable %s from %s to %s.\n",
6899  ast_var_full_name(newvar), ast_channel_name(parent),
6900  ast_channel_name(child));
6903  ast_var_value(newvar));
6904  }
6905  }
6906 }
6907 
6908 /*!
6909  \brief Clone channel variables from 'clone' channel into 'original' channel
6910 
6911  All variables except those related to app_groupcount are cloned.
6912  Variables are actually _removed_ from 'clone' channel, presumably
6913  because it will subsequently be destroyed.
6914 
6915  \note Assumes locks will be in place on both channels when called.
6916 */
6917 static void clone_variables(struct ast_channel *original, struct ast_channel *clonechan)
6918 {
6919  struct ast_var_t *current, *newvar;
6920  /* Append variables from clone channel into original channel */
6921  /* XXX Is this always correct? We have to in order to keep MACROS working XXX */
6923 
6924  /* then, dup the varshead list into the clone */
6925 
6926  AST_LIST_TRAVERSE(ast_channel_varshead(original), current, entries) {
6927  newvar = ast_var_assign(current->name, current->value);
6928  if (newvar)
6929  AST_LIST_INSERT_TAIL(ast_channel_varshead(clonechan), newvar, entries);
6930  }
6931 }
6932 
6933 
6934 void ast_channel_name_to_dial_string(char *channel_name)
6935 {
6936  char *dash;
6937 
6938  /* Truncate after the dash */
6939  dash = strrchr(channel_name, '-');
6940  if (dash) {
6941  *dash = '\0';
6942  }
6943 }
6944 
6945 /*!
6946  * \brief Masquerade a channel
6947  *
6948  * \note Assumes _NO_ channels and _NO_ channel pvt's are locked. If a channel is locked while calling
6949  * this function, it invalidates our channel container locking order. All channels
6950  * must be unlocked before it is permissible to lock the channels' ao2 container.
6951  */
6952 static void channel_do_masquerade(struct ast_channel *original, struct ast_channel *clonechan)
6953 {
6954  int x;
6955  int origstate;
6956  unsigned int orig_disablestatecache;
6957  unsigned int clone_disablestatecache;
6958  int generator_fd;
6959  int visible_indication;
6960  int clone_hold_state;
6961  int moh_is_playing;
6962  struct ast_frame *current;
6963  const struct ast_channel_tech *t;
6964  void *t_pvt;
6965  union {
6966  struct ast_hangup_handler_list handlers;
6967  struct ast_party_dialed dialed;
6968  struct ast_party_caller caller;
6969  struct ast_party_connected_line connected;
6970  struct ast_party_redirecting redirecting;
6971  } exchange;
6972  struct ast_channel *bridged;
6973  struct ast_format *rformat;
6974  struct ast_format *wformat;
6975  struct ast_format *tmp_format;
6976  struct ast_format_cap *tmp_cap;
6977  char tmp_name[AST_CHANNEL_NAME];
6978  char clone_sending_dtmf_digit;
6979  struct timeval clone_sending_dtmf_tv;
6980 
6981  /* XXX This operation is a bit odd. We're essentially putting the guts of
6982  * the clone channel into the original channel. Start by killing off the
6983  * original channel's backend. While the features are nice, which is the
6984  * reason we're keeping it, it's still awesomely weird. XXX */
6985 
6986  /* Indicate to each channel that a masquerade is about to begin. */
6987  x = 1;
6988  ast_indicate_data(original, AST_CONTROL_MASQUERADE_NOTIFY, &x, sizeof(x));
6989  ast_indicate_data(clonechan, AST_CONTROL_MASQUERADE_NOTIFY, &x, sizeof(x));
6990 
6991  /*
6992  * The container lock is necessary for proper locking order
6993  * because the channels must be unlinked to change their
6994  * names.
6995  *
6996  * The original and clonechan locks must be held while the
6997  * channel contents are shuffled around for the masquerade.
6998  *
6999  * The masq and masqr pointers need to be left alone until the masquerade
7000  * has restabilized the channels to hold off ast_hangup() and until
7001  * AST_FLAG_ZOMBIE can be set on the clonechan.
7002  */
7003  ao2_lock(channels);
7004 
7005  /* Bump the refs to ensure that they won't dissapear on us. */
7006  ast_channel_ref(original);
7007  ast_channel_ref(clonechan);
7008 
7009  /* unlink from channels container as name (which is the hash value) will change */
7010  ao2_unlink(channels, original);
7011  ao2_unlink(channels, clonechan);
7012 
7013  moh_is_playing = ast_test_flag(ast_channel_flags(original), AST_FLAG_MOH);
7014  if (moh_is_playing) {
7015  /* Stop MOH on the old original channel. */
7016  ast_moh_stop(original);
7017  }
7018 
7019  /*
7020  * Stop any visible indication on the original channel so we can
7021  * transfer it to the clonechan taking the original's place.
7022  */
7023  ast_channel_lock(original);
7024  visible_indication = ast_channel_visible_indication(original);
7025  ast_channel_unlock(original);
7026  ast_indicate(original, -1);
7027 
7028  /* Start the masquerade channel contents rearangement. */
7029  ast_channel_lock_both(original, clonechan);
7030 
7031  ast_debug(1, "Actually Masquerading %s(%u) into the structure of %s(%u)\n",
7032  ast_channel_name(clonechan), ast_channel_state(clonechan),
7033  ast_channel_name(original), ast_channel_state(original));
7034 
7035  /* When all is said and done force new snapshot segments so they are
7036  * up to date.
7037  */
7040 
7041  /*
7042  * Remember the original read/write formats. We turn off any
7043  * translation on either one
7044  */
7045  rformat = ao2_bump(ast_channel_readformat(original));
7046  wformat = ao2_bump(ast_channel_writeformat(original));
7047  free_translation(clonechan);
7048  free_translation(original);
7049 
7050  clone_hold_state = ast_channel_hold_state(clonechan);
7051 
7052  /* Save the current DTMF digit being sent if any. */
7053  clone_sending_dtmf_digit = ast_channel_sending_dtmf_digit(clonechan);
7054  clone_sending_dtmf_tv = ast_channel_sending_dtmf_tv(clonechan);
7055 
7056  /* Swap uniqueid's of the channels. This needs to happen before channel renames,
7057  * so rename events get the proper id's.
7058  */
7060 
7061  /* Make sure the Stasis topic on the channel is updated appropriately */
7062  ast_channel_internal_swap_topics(clonechan, original);
7063 
7064  /* Swap endpoint forward so channel created with endpoint exchanges its state
7065  * with other channel for proper endpoint cleanup.
7066  */
7067  ast_channel_internal_swap_endpoint_forward(clonechan, original);
7068 
7069  /* The old snapshots need to follow the channels so the snapshot update is correct */
7070  ast_channel_internal_swap_snapshots(clonechan, original);
7071 
7072  /* Swap channel names. This uses ast_channel_name_set directly, so we
7073  * don't get any spurious rename events.
7074  */
7075  ast_copy_string(tmp_name, ast_channel_name(clonechan), sizeof(tmp_name));
7076  ast_channel_name_set(clonechan, ast_channel_name(original));
7077  ast_channel_name_set(original, tmp_name);
7078 
7079  /* Swap the technologies */
7080  t = ast_channel_tech(original);
7081  ast_channel_tech_set(original, ast_channel_tech(clonechan));
7082  ast_channel_tech_set(clonechan, t);
7083 
7084  t_pvt = ast_channel_tech_pvt(original);
7085  ast_channel_tech_pvt_set(original, ast_channel_tech_pvt(clonechan));
7086  ast_channel_tech_pvt_set(clonechan, t_pvt);
7087 
7088  /* Swap the alertpipes */
7089  ast_channel_internal_alertpipe_swap(original, clonechan);
7090 
7091  /*
7092  * Swap the readq's. The end result should be this:
7093  *
7094  * 1) All frames should be on the new (original) channel.
7095  * 2) Any frames that were already on the new channel before this
7096  * masquerade need to be at the end of the readq, after all of the
7097  * frames on the old (clone) channel.
7098  * 3) The alertpipe needs to get poked for every frame that was already
7099  * on the new channel, since we are now using the alert pipe from the
7100  * old (clone) channel.
7101  */
7102  {
7103  AST_LIST_HEAD_NOLOCK(, ast_frame) tmp_readq;
7104 
7105  AST_LIST_HEAD_INIT_NOLOCK(&tmp_readq);
7106  AST_LIST_APPEND_LIST(&tmp_readq, ast_channel_readq(original), frame_list);
7108 
7109  while ((current = AST_LIST_REMOVE_HEAD(&tmp_readq, frame_list))) {
7110  AST_LIST_INSERT_TAIL(ast_channel_readq(original), current, frame_list);
7111  if (ast_channel_alert_write(original)) {
7112  ast_log(LOG_WARNING, "write() failed: %s\n", strerror(errno));
7113  }
7114  }
7115  }
7116 
7117  /* Swap the raw formats */
7118  tmp_format = ao2_bump(ast_channel_rawreadformat(original));
7120  ast_channel_set_rawreadformat(clonechan, tmp_format);
7121  ao2_cleanup(tmp_format);
7122 
7123  tmp_format = ao2_bump(ast_channel_rawwriteformat(original));
7125  ast_channel_set_rawwriteformat(clonechan, tmp_format);
7126  ao2_cleanup(tmp_format);
7127 
7129 
7130  /* And of course, so does our current state. Note we need not
7131  call ast_setstate since the event manager doesn't really consider
7132  these separate. We do this early so that the clone has the proper
7133  state of the original channel. */
7134  origstate = ast_channel_state(original);
7135  ast_channel_state_set(original, ast_channel_state(clonechan));
7136  ast_channel_state_set(clonechan, origstate);
7137 
7138  /* And the swap the cachable state too. Otherwise we'd start caching
7139  * Local channels and ignoring real ones. */
7140  orig_disablestatecache = ast_test_flag(ast_channel_flags(original), AST_FLAG_DISABLE_DEVSTATE_CACHE);
7141  clone_disablestatecache = ast_test_flag(ast_channel_flags(clonechan), AST_FLAG_DISABLE_DEVSTATE_CACHE);
7142  if (orig_disablestatecache != clone_disablestatecache) {
7143  if (orig_disablestatecache) {
7146  } else {
7149  }
7150  }
7151 
7152  /* Update the type. */
7153  t_pvt = ast_channel_monitor(original);
7154  ast_channel_monitor_set(original, ast_channel_monitor(clonechan));
7155  ast_channel_monitor_set(clonechan, t_pvt);
7156 
7157  /* Keep the same language. */
7158  ast_channel_language_set(original, ast_channel_language(clonechan));
7159 
7160  /* Keep the same parkinglot. */
7161  ast_channel_parkinglot_set(original, ast_channel_parkinglot(clonechan));
7162 
7163  /* Clear all existing file descriptors but retain the generator */
7164  generator_fd = ast_channel_fd(original, AST_GENERATOR_FD);
7166  ast_channel_set_fd(original, AST_GENERATOR_FD, generator_fd);
7167 
7168  /* Copy all file descriptors present on clonechan to original, skipping generator */
7169  for (x = 0; x < ast_channel_fd_count(clonechan); x++) {
7170  if (x != AST_GENERATOR_FD)
7171  ast_channel_set_fd(original, x, ast_channel_fd(clonechan, x));
7172  }
7173 
7174  ast_app_group_update(clonechan, original);
7175 
7176  /* Swap hangup handlers. */
7177  exchange.handlers = *ast_channel_hangup_handlers(original);
7179  *ast_channel_hangup_handlers(clonechan) = exchange.handlers;
7180 
7181  /* Call fixup handlers for the clone chan */
7182  if (AST_LIST_FIRST(ast_channel_datastores(clonechan))) {
7183  struct ast_datastore *ds;
7184  /* We use a safe traversal here because some fixup routines actually
7185  * remove the datastore from the list and free them.
7186  */
7188  if (ds->info->chan_fixup) {
7189  ds->info->chan_fixup(ds->data, clonechan, original);
7190  }
7191  }
7193  }
7194 
7195  /* Call breakdown handlers for the original chan */
7196  if (AST_LIST_FIRST(ast_channel_datastores(original))) {
7197  struct ast_datastore *ds;
7198  /* We use a safe traversal here because some breakdown routines may
7199  * remove the datastore from the list and free them.
7200  */
7202  if (ds->info->chan_breakdown) {
7203  ds->info->chan_breakdown(ds->data, clonechan, original);
7204  }
7205  }
7207  }
7208 
7209  /* Move data stores over */
7210  if (AST_LIST_FIRST(ast_channel_datastores(clonechan))) {
7212  }
7213 
7214  /* Move framehooks over */
7215  ast_framehook_list_fixup(clonechan, original);
7216 
7217  /* Move audiohooks over */
7218  ast_audiohook_move_all(clonechan, original);
7219 
7220  ast_autochan_new_channel(clonechan, original);
7221 
7222  clone_variables(original, clonechan);
7223  /* Presense of ADSI capable CPE follows clone */
7224  ast_channel_adsicpe_set(original, ast_channel_adsicpe(clonechan));
7225  /* Bridge remains the same */
7226  /* CDR fields remain the same */
7227  /* XXX What about blocking, softhangup, blocker, and lock and blockproc? XXX */
7228  /* Application and data remain the same */
7229  /* Clone exception becomes real one, as with fdno */
7231  ast_channel_fdno_set(original, ast_channel_fdno(clonechan));
7232  /* Schedule context remains the same */
7233  /* Stream stuff stays the same */
7234  /* Keep the original state. The fixup code will need to work with it most likely */
7235 
7236  /*
7237  * Just swap the whole structures, nevermind the allocations,
7238  * they'll work themselves out.
7239  */
7240  exchange.dialed = *ast_channel_dialed(original);
7241  ast_channel_dialed_set(original, ast_channel_dialed(clonechan));
7242  ast_channel_dialed_set(clonechan, &exchange.dialed);
7243 
7244  /* Reset any earlier private caller id representations */
7245  ast_party_id_reset(&ast_channel_caller(original)->priv);
7246  ast_party_id_reset(&ast_channel_caller(clonechan)->priv);
7247 
7248  exchange.caller = *ast_channel_caller(original);
7249  ast_channel_caller_set(original, ast_channel_caller(clonechan));
7250  ast_channel_caller_set(clonechan, &exchange.caller);
7251 
7252  /* Reset any earlier private connected id representations */
7253  ast_party_id_reset(&ast_channel_connected(original)->priv);
7254  ast_party_id_reset(&ast_channel_connected(clonechan)->priv);
7255 
7256  exchange.connected = *ast_channel_connected(original);
7257  ast_channel_connected_set(original, ast_channel_connected(clonechan));
7258  ast_channel_connected_set(clonechan, &exchange.connected);
7259 
7260  /* Reset any earlier private redirecting orig, from or to representations */
7261  ast_party_id_reset(&ast_channel_redirecting(original)->priv_orig);
7262  ast_party_id_reset(&ast_channel_redirecting(clonechan)->priv_orig);
7263  ast_party_id_reset(&ast_channel_redirecting(original)->priv_from);
7264  ast_party_id_reset(&ast_channel_redirecting(clonechan)->priv_from);
7265  ast_party_id_reset(&ast_channel_redirecting(original)->priv_to);
7266  ast_party_id_reset(&ast_channel_redirecting(clonechan)->priv_to);
7267 
7268  exchange.redirecting = *ast_channel_redirecting(original);
7270  ast_channel_redirecting_set(clonechan, &exchange.redirecting);
7271 
7272  ast_channel_publish_snapshot(original);
7273 
7274  /* Restore original timing file descriptor */
7276 
7277  /* Our native formats are different now */
7279  if (tmp_cap) {
7281  ast_channel_nativeformats_set(original, tmp_cap);
7282  ao2_ref(tmp_cap, -1);
7283  }
7284 
7285  /* Context, extension, priority, app data, jump table, remain the same */
7286  /* pvt switches. pbx stays the same, as does next */
7287 
7288  /* Set the write format */
7289  ast_set_write_format(original, wformat);
7290 
7291  /* Set the read format */
7292  ast_set_read_format(original, rformat);
7293 
7294  /* Copy the music class */
7295  ast_channel_musicclass_set(original, ast_channel_musicclass(clonechan));
7296 
7297  /* copy over accuntcode and set peeraccount across the bridge */
7298  ast_channel_accountcode_set(original, S_OR(ast_channel_accountcode(clonechan), ""));
7299 
7300  ast_debug(1, "Putting channel %s in %s/%s formats\n", ast_channel_name(original),
7301  ast_format_get_name(wformat), ast_format_get_name(rformat));
7302 
7303  /* Fixup the original clonechan's physical side */
7304  if (ast_channel_tech(original)->fixup && ast_channel_tech(original)->fixup(clonechan, original)) {
7305  ast_log(LOG_WARNING, "Channel type '%s' could not fixup channel %s, strange things may happen. (clonechan)\n",
7306  ast_channel_tech(original)->type, ast_channel_name(original));
7307  }
7308 
7309  /* Fixup the original original's physical side */
7310  if (ast_channel_tech(clonechan)->fixup && ast_channel_tech(clonechan)->fixup(original, clonechan)) {
7311  ast_log(LOG_WARNING, "Channel type '%s' could not fixup channel %s, strange things may happen. (original)\n",
7312  ast_channel_tech(clonechan)->type, ast_channel_name(clonechan));
7313  }
7314 
7315  ast_channel_internal_swap_stream_topology(original, clonechan);
7316 
7317  /*
7318  * Now, at this point, the "clone" channel is totally F'd up.
7319  * We mark it as a zombie so nothing tries to touch it.
7320  *
7321  * This must be done before we unlock clonechan to prevent
7322  * setting up another masquerade on the clonechan.
7323  */
7325  ast_queue_frame(clonechan, &ast_null_frame);
7326 
7327  ast_channel_unlock(original);
7328  ast_channel_unlock(clonechan);
7329 
7330  /*
7331  * Indicate to each channel that a masquerade is complete.
7332  *
7333  * We can still do this to clonechan even though it is a
7334  * zombie because ast_indicate_data() will explicitly pass
7335  * this control and ast_hangup() is held off until the
7336  * ast_channel_masq() and ast_channel_masqr() pointers are
7337  * cleared.
7338  */
7339  x = 0;
7340  ast_indicate_data(original, AST_CONTROL_MASQUERADE_NOTIFY, &x, sizeof(x));
7341  ast_indicate_data(clonechan, AST_CONTROL_MASQUERADE_NOTIFY, &x, sizeof(x));
7342 
7343  ast_bridge_notify_masquerade(original);
7344 
7345  if (clone_hold_state == AST_CONTROL_HOLD) {
7346  ast_debug(1, "Channel %s simulating UNHOLD for masquerade.\n",
7347  ast_channel_name(original));
7348  ast_indicate(original, AST_CONTROL_UNHOLD);
7349  }
7350  if (clone_sending_dtmf_digit) {
7351  /*
7352  * The clonechan was sending a DTMF digit that was not completed
7353  * before the masquerade.
7354  */
7355  ast_channel_end_dtmf(original, clone_sending_dtmf_digit, clone_sending_dtmf_tv,
7356  "masquerade");
7357  }
7358 
7359  /*
7360  * If an indication is currently playing, maintain it on the
7361  * channel that is taking the place of original.
7362  *
7363  * This is needed because the masquerade is swapping out the
7364  * internals of the channel, and the new channel private data
7365  * needs to be made aware of the current visible indication
7366  * (RINGING, CONGESTION, etc.)
7367  */
7368  if (visible_indication) {
7369  if (visible_indication == AST_CONTROL_HOLD) {
7370  const char *latest_musicclass;
7371  int len;
7372 
7373  ast_channel_lock(original);
7374  latest_musicclass = ast_strdupa(ast_channel_latest_musicclass(original));
7375  ast_channel_unlock(original);
7376  if (ast_strlen_zero(latest_musicclass)) {
7377  latest_musicclass = NULL;
7378  len = 0;
7379  } else {
7380  len = strlen(latest_musicclass) + 1;
7381  }
7382  ast_indicate_data(original, visible_indication, latest_musicclass, len);
7383  } else {
7384  ast_indicate(original, visible_indication);
7385  }
7386  }
7387 
7388  /*
7389  * If MOH was playing on the original channel then it needs to be
7390  * maintained on the channel that is replacing it.
7391  */
7392  if (moh_is_playing) {
7393  /* Start MOH on the new original channel. */
7394  ast_moh_start(original, NULL, NULL);
7395  }
7396 
7397  ast_channel_lock(original);
7398 
7399  /* Signal any blocker */
7401  pthread_kill(ast_channel_blocker(original), SIGURG);
7402  }
7403 
7404  ast_debug(1, "Done Masquerading %s (%u)\n", ast_channel_name(original), ast_channel_state(original));
7405  ast_channel_unlock(original);
7406 
7407  if ((bridged = ast_channel_bridge_peer(original))) {
7409  ast_channel_unref(bridged);
7410  }
7412 
7413  /* Now that the operation is complete, we can clear the masq
7414  * and masqr fields of both channels.
7415  */
7416  ast_channel_lock_both(original, clonechan);
7417  ast_channel_masq_set(original, NULL);
7418  ast_channel_masqr_set(clonechan, NULL);
7419  ast_channel_unlock(original);
7420  ast_channel_unlock(clonechan);
7421 
7422  ao2_link(channels, clonechan);
7423  ao2_link(channels, original);
7424  ao2_unlock(channels);
7425 
7426  /* Release our held safety references. */
7427  ast_channel_unref(original);
7428  ast_channel_unref(clonechan);
7429 
7430  ao2_cleanup(rformat);
7431  ao2_cleanup(wformat);
7432 }
7433 
7434 void ast_set_callerid(struct ast_channel *chan, const char *cid_num, const char *cid_name, const char *cid_ani)
7435 {
7436  ast_channel_lock(chan);
7437 
7438  if (cid_num) {
7439  ast_channel_caller(chan)->id.number.valid = 1;
7440  ast_free(ast_channel_caller(chan)->id.number.str);
7441  ast_channel_caller(chan)->id.number.str = ast_strdup(cid_num);
7442  }
7443  if (cid_name) {
7444  ast_channel_caller(chan)->id.name.valid = 1;
7445  ast_free(ast_channel_caller(chan)->id.name.str);
7446  ast_channel_caller(chan)->id.name.str = ast_strdup(cid_name);
7447  }
7448  if (cid_ani) {
7449  ast_channel_caller(chan)->ani.number.valid = 1;
7450  ast_free(ast_channel_caller(chan)->ani.number.str);
7451  ast_channel_caller(chan)->ani.number.str = ast_strdup(cid_ani);
7452  }
7453 
7455 
7456  ast_channel_unlock(chan);
7457 }
7458 
7459 void ast_channel_set_caller(struct ast_channel *chan, const struct ast_party_caller *caller, const struct ast_set_party_caller *update)
7460 {
7461  if (ast_channel_caller(chan) == caller) {
7462  /* Don't set to self */
7463  return;
7464  }
7465 
7466  ast_channel_lock(chan);
7467  ast_party_caller_set(ast_channel_caller(chan), caller, update);
7469  ast_channel_unlock(chan);
7470 }
7471 
7472 void ast_channel_set_caller_event(struct ast_channel *chan, const struct ast_party_caller *caller, const struct ast_set_party_caller *update)
7473 {
7474  if (ast_channel_caller(chan) == caller) {
7475  /* Don't set to self */
7476  return;
7477  }
7478 
7479  ast_channel_lock(chan);
7480  ast_party_caller_set(ast_channel_caller(chan), caller, update);
7483  ast_channel_unlock(chan);
7484 }
7485 
7487 {
7488  int oldstate = ast_channel_state(chan);
7489  char name[AST_CHANNEL_NAME], *dashptr;
7490 
7491  if (oldstate == state)
7492  return 0;
7493 
7494  ast_copy_string(name, ast_channel_name(chan), sizeof(name));
7495  if ((dashptr = strrchr(name, '-'))) {
7496  *dashptr = '\0';
7497  }
7498 
7499  ast_channel_state_set(chan, state);
7500 
7502 
7503  /* We have to pass AST_DEVICE_UNKNOWN here because it is entirely possible that the channel driver
7504  * for this channel is using the callback method for device state. If we pass in an actual state here
7505  * we override what they are saying the state is and things go amuck. */
7507 
7508  return 0;
7509 }
7510 
7511 /*! \brief Bridge two channels together (early) */
7513 {
7514  /* Make sure we can early bridge, if not error out */
7515  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)))
7516  return -1;
7517 
7518  return ast_channel_tech(c0)->early_bridge(c0, c1);
7519 }
7520 
7521 /*! \brief Sets an option on a channel */
7522 int ast_channel_setoption(struct ast_channel *chan, int option, void *data, int datalen, int block)
7523 {
7524  int res;
7525 
7526  ast_channel_lock(chan);
7527  if (!ast_channel_tech(chan)->setoption) {
7528  errno = ENOSYS;
7529  ast_channel_unlock(chan);
7530  return -1;
7531  }
7532 
7533  if (block)
7534  ast_log(LOG_ERROR, "XXX Blocking not implemented yet XXX\n");
7535 
7536  res = ast_channel_tech(chan)->setoption(chan, option, data, datalen);
7537  ast_channel_unlock(chan);
7538 
7539  return res;
7540 }
7541 
7542 int ast_channel_queryoption(struct ast_channel *chan, int option, void *data, int *datalen, int block)
7543 {
7544  int res;
7545 
7546  ast_channel_lock(chan);
7547  if (!ast_channel_tech(chan)->queryoption) {
7548  errno = ENOSYS;
7549  ast_channel_unlock(chan);
7550  return -1;
7551  }
7552 
7553  if (block)
7554  ast_log(LOG_ERROR, "XXX Blocking not implemented yet XXX\n");
7555 
7556  res = ast_channel_tech(chan)->queryoption(chan, option, data, datalen);
7557  ast_channel_unlock(chan);
7558 
7559  return res;
7560 }
7561 
7563  int freq1;
7564  int freq2;
7566  int vol;
7567 };
7568 
7570  int fac1;
7571  int fac2;
7572  int v1_1;
7573  int v2_1;
7574  int v3_1;
7575  int v1_2;
7576  int v2_2;
7577  int v3_2;
7579  int pos;
7582  struct ast_frame f;
7583  unsigned char offset[AST_FRIENDLY_OFFSET];
7584  short data[4000];
7585 };
7586 
7587 static void tonepair_release(struct ast_channel *chan, void *params)
7588 {
7589  struct tonepair_state *ts = params;
7590 
7591  if (chan) {
7592  ast_set_write_format(chan, ts->origwfmt);
7593  }
7594  ao2_cleanup(ts->origwfmt);
7595  ast_free(ts);
7596 }
7597 
7598 static void *tonepair_alloc(struct ast_channel *chan, void *params)
7599 {
7600  struct tonepair_state *ts;
7601  struct tonepair_def *td = params;
7602 
7603  if (!(ts = ast_calloc(1, sizeof(*ts)))) {
7604  return NULL;
7605  }
7606 
7609  ast_log(LOG_WARNING, "Unable to set '%s' to signed linear format (write)\n", ast_channel_name(chan));
7610  tonepair_release(NULL, ts);
7611  ts = NULL;
7612  } else {
7613  ts->fac1 = 2.0 * cos(2.0 * M_PI * (td->freq1 / 8000.0)) * 32768.0;
7614  ts->v1_1 = 0;
7615  ts->v2_1 = sin(-4.0 * M_PI * (td->freq1 / 8000.0)) * td->vol;
7616  ts->v3_1 = sin(-2.0 * M_PI * (td->freq1 / 8000.0)) * td->vol;
7617  ts->v2_1 = 0;
7618  ts->fac2 = 2.0 * cos(2.0 * M_PI * (td->freq2 / 8000.0)) * 32768.0;
7619  ts->v2_2 = sin(-4.0 * M_PI * (td->freq2 / 8000.0)) * td->vol;
7620  ts->v3_2 = sin(-2.0 * M_PI * (td->freq2 / 8000.0)) * td->vol;
7621  ts->duration = td->duration;
7622  ts->modulate = 0;
7623  }
7624  /* Let interrupts interrupt :) */
7626  return ts;
7627 }
7628 
7629 static int tonepair_generator(struct ast_channel *chan, void *data, int len, int samples)
7630 {
7631  struct tonepair_state *ts = data;
7632  int x;
7633 
7634  /* we need to prepare a frame with 16 * timelen samples as we're
7635  * generating SLIN audio
7636  */
7637  len = samples * 2;
7638 
7639  if (len > sizeof(ts->data) / 2 - 1) {
7640  ast_log(LOG_WARNING, "Can't generate that much data!\n");
7641  return -1;
7642  }
7643  memset(&ts->f, 0, sizeof(ts->f));
7644  for (x=0;x<len/2;x++) {
7645  ts->v1_1 = ts->v2_1;
7646  ts->v2_1 = ts->v3_1;
7647  ts->v3_1 = (ts->fac1 * ts->v2_1 >> 15) - ts->v1_1;
7648 
7649  ts->v1_2 = ts->v2_2;
7650  ts->v2_2 = ts->v3_2;
7651  ts->v3_2 = (ts->fac2 * ts->v2_2 >> 15) - ts->v1_2;
7652  if (ts->modulate) {
7653  int p;
7654  p = ts->v3_2 - 32768;
7655  if (p < 0) p = -p;
7656  p = ((p * 9) / 10) + 1;
7657  ts->data[x] = (ts->v3_1 * p) >> 15;
7658  } else
7659  ts->data[x] = ts->v3_1 + ts->v3_2;
7660  }
7661  ts->f.frametype = AST_FRAME_VOICE;
7663  ts->f.datalen = len;
7664  ts->f.samples = samples;
7666  ts->f.data.ptr = ts->data;
7667  ast_write(chan, &ts->f);
7668  ts->pos += x;
7669  if (ts->duration > 0) {
7670  if (ts->pos >= ts->duration * 8)
7671  return -1;
7672  }
7673  return 0;
7674 }
7675 
7676 static struct ast_generator tonepair = {
7677  .alloc = tonepair_alloc,
7678  .release = tonepair_release,
7679  .generate = tonepair_generator,
7680 };
7681 
7682 int ast_tonepair_start(struct ast_channel *chan, int freq1, int freq2, int duration, int vol)
7683 {
7684  struct tonepair_def d = { 0, };
7685 
7686  d.freq1 = freq1;
7687  d.freq2 = freq2;
7688  d.duration = duration;
7689  d.vol = (vol < 1) ? 8192 : vol; /* force invalid to 8192 */
7690  if (ast_activate_generator(chan, &tonepair, &d))
7691  return -1;
7692  return 0;
7693 }
7694 
7695 void ast_tonepair_stop(struct ast_channel *chan)
7696 {
7698 }
7699 
7700 int ast_tonepair(struct ast_channel *chan, int freq1, int freq2, int duration, int vol)
7701 {
7702  int res;
7703 
7704  if ((res = ast_tonepair_start(chan, freq1, freq2, duration, vol)))
7705  return res;
7706 
7707  /* Give us some wiggle room */
7708  while (ast_channel_generatordata(chan) && ast_waitfor(chan, 100) >= 0) {
7709  struct ast_frame *f = ast_read(chan);
7710  if (f)
7711  ast_frfree(f);
7712  else
7713  return -1;
7714  }
7715  return 0;
7716 }
7717 
7719 {
7720  char *piece;
7721  char *c;
7722  int start=0, finish=0, x;
7723  ast_group_t group = 0;
7724 
7725  if (ast_strlen_zero(s))
7726  return 0;
7727 
7728  c = ast_strdupa(s);
7729 
7730  while ((piece = strsep(&c, ","))) {
7731  if (sscanf(piece, "%30d-%30d", &start, &finish) == 2) {
7732  /* Range */
7733  } else if (sscanf(piece, "%30d", &start)) {
7734  /* Just one */
7735  finish = start;
7736  } else {
7737  ast_log(LOG_ERROR, "Syntax error parsing group configuration '%s' at '%s'. Ignoring.\n", s, piece);
7738  continue;
7739  }
7740  for (x = start; x <= finish; x++) {
7741  if ((x > 63) || (x < 0)) {
7742  ast_log(LOG_WARNING, "Ignoring invalid group %d (maximum group is 63)\n", x);
7743  } else
7744  group |= ((ast_group_t) 1 << x);
7745  }
7746  }
7747  return group;
7748 }
7749 
7750 /*! \brief Named group member structure */
7752  /*! Pre-built hash of group member name. */
7753  unsigned int hash;
7754  /*! Group member name. (End allocation of name string.) */
7755  char name[1];
7756 };
7757 
7758 /*! \brief Comparison function used for named group container */
7759 static int namedgroup_cmp_cb(void *obj, void *arg, int flags)
7760 {
7761  const struct namedgroup_member *an = obj;
7762  const struct namedgroup_member *bn = arg;
7763 
7764  return strcmp(an->name, bn->name) ? 0 : CMP_MATCH | CMP_STOP;
7765 }
7766 
7767 /*! \brief Hashing function used for named group container */
7768 static int namedgroup_hash_cb(const void *obj, const int flags)
7769 {
7770  const struct namedgroup_member *member = obj;
7771 
7772  return member->hash;
7773 }
7774 
7775 struct ast_namedgroups *ast_get_namedgroups(const char *s)
7776 {
7777  struct ao2_container *namedgroups;
7778  char *piece;
7779  char *c;
7780 
7781  if (!s) {
7782  return NULL;
7783  }
7784 
7785  /*! \brief Remove leading and trailing whitespace */
7787  if (ast_strlen_zero(c)) {
7788  return NULL;
7789  }
7790 
7793  if (!namedgroups) {
7794  return NULL;
7795  }
7796 
7797  while ((piece = strsep(&c, ","))) {
7798  struct namedgroup_member *member;
7799  size_t len;
7800 
7801  /* remove leading/trailing whitespace */
7802  piece = ast_strip(piece);
7803 
7804  len = strlen(piece);
7805  if (!len) {
7806  continue;
7807  }
7808 
7809  member = ao2_alloc_options(sizeof(*member) + len, NULL, AO2_ALLOC_OPT_LOCK_NOLOCK);
7810  if (!member) {
7811  ao2_ref(namedgroups, -1);
7812  return NULL;
7813  }
7814  strcpy(member->name, piece);/* Safe */
7815  member->hash = ast_str_hash(member->name);
7816 
7817  /* every group name may exist only once, delete duplicates */
7818  ao2_find(namedgroups, member, OBJ_POINTER | OBJ_UNLINK | OBJ_NODATA);
7819  ao2_link(namedgroups, member);
7820  ao2_ref(member, -1);
7821  }
7822 
7823  if (!ao2_container_count(namedgroups)) {
7824  /* There were no group names specified. */
7825  ao2_ref(namedgroups, -1);
7826  namedgroups = NULL;
7827  }
7828 
7829  return (struct ast_namedgroups *) namedgroups;
7830 }
7831 
7832 struct ast_namedgroups *ast_unref_namedgroups(struct ast_namedgroups *groups)
7833 {
7834  ao2_cleanup(groups);
7835  return NULL;
7836 }
7837 
7838 struct ast_namedgroups *ast_ref_namedgroups(struct ast_namedgroups *groups)
7839 {
7840  if (groups) {
7841  ao2_ref(groups, 1);
7842  }
7843  return groups;
7844 }
7845 
7846 static int (*ast_moh_start_ptr)(struct ast_channel *, const char *, const char *) = NULL;
7847 static void (*ast_moh_stop_ptr)(struct ast_channel *) = NULL;
7848 static void (*ast_moh_cleanup_ptr)(struct ast_channel *) = NULL;
7849 
7850 void ast_install_music_functions(int (*start_ptr)(struct ast_channel *, const char *, const char *),
7851  void (*stop_ptr)(struct ast_channel *),
7852  void (*cleanup_ptr)(struct ast_channel *))
7853 {
7854  ast_moh_start_ptr = start_ptr;
7855  ast_moh_stop_ptr = stop_ptr;
7856  ast_moh_cleanup_ptr = cleanup_ptr;
7857 }
7858 
7860 {
7864 }
7865 
7866 int ast_moh_start(struct ast_channel *chan, const char *mclass, const char *interpclass)
7867 {
7868  if (ast_moh_start_ptr)
7869  return ast_moh_start_ptr(chan, mclass, interpclass);
7870 
7871  ast_verb(3, "Music class %s requested but no musiconhold loaded.\n", mclass ? mclass : (interpclass ? interpclass : "default"));
7872 
7873  return -1;
7874 }
7875 
7876 void ast_moh_stop(struct ast_channel *chan)
7877 {
7878  if (ast_moh_stop_ptr)
7879  ast_moh_stop_ptr(chan);
7880 }
7881 
7882 void ast_moh_cleanup(struct ast_channel *chan)
7883 {
7884  if (ast_moh_cleanup_ptr)
7885  ast_moh_cleanup_ptr(chan);
7886 }
7887 
7888 static int ast_channel_hash_cb(const void *obj, const int flags)
7889 {
7890  const char *name = (flags & OBJ_KEY) ? obj : ast_channel_name((struct ast_channel *) obj);
7891 
7892  /* If the name isn't set, return 0 so that the ao2_find() search will
7893  * start in the first bucket. */
7894  if (ast_strlen_zero(name)) {
7895  return 0;
7896  }
7897 
7898  return ast_str_case_hash(name);
7899 }
7900 
7901 /*!
7902  * \internal
7903  * \brief Print channel object key (name).
7904  * \since 12.0.0
7905  *
7906  * \param v_obj A pointer to the object we want the key printed.
7907  * \param where User data needed by prnt to determine where to put output.
7908  * \param prnt Print output callback function to use.
7909  *
7910  * \return Nothing
7911  */
7912 static void prnt_channel_key(void *v_obj, void *where, ao2_prnt_fn *prnt)
7913 {
7914  struct ast_channel *chan = v_obj;
7915 
7916  if (!chan) {
7917  return;
7918  }
7919  prnt(where, "%s", ast_channel_name(chan));
7920 }
7921 
7922 /*!
7923  * \brief List of channel variables to append to all channel-related events.
7924  */
7927  unsigned int isfunc:1;
7928  char name[];
7929 };
7930 
7932 
7933 static struct external_vars ami_vars;
7934 static struct external_vars ari_vars;
7935 
7936 static void free_external_channelvars(struct external_vars *channelvars)
7937 {
7938  struct manager_channel_variable *var;
7939  AST_RWLIST_WRLOCK(channelvars);
7940  while ((var = AST_RWLIST_REMOVE_HEAD(channelvars, entry))) {
7941  ast_free(var);
7942  }
7943  AST_RWLIST_UNLOCK(channelvars);
7944 }
7945 
7946 static int channel_has_external_vars(struct external_vars *channelvars)
7947 {
7948  int vars_present;
7949 
7950  AST_RWLIST_RDLOCK(channelvars);
7951  vars_present = !AST_LIST_EMPTY(channelvars);
7952  AST_RWLIST_UNLOCK(channelvars);
7953 
7954  return vars_present;
7955 }
7956 
7958 {
7960 }
7961 
7963 {
7965 }
7966 
7967 static void channel_set_external_vars(struct external_vars *channelvars, size_t varc, char **vars)
7968 {
7969  size_t i;
7970 
7971  free_external_channelvars(channelvars);
7972  AST_RWLIST_WRLOCK(channelvars);
7973  for (i = 0; i < varc; ++i) {
7974  const char *var = vars[i];
7975  struct manager_channel_variable *mcv;
7976  if (!(mcv = ast_calloc(1, sizeof(*mcv) + strlen(var) + 1))) {
7977  break;
7978  }
7979  strcpy(mcv->name, var); /* SAFE */
7980  if (strchr(var, '(')) {
7981  mcv->isfunc = 1;
7982  }
7983  AST_RWLIST_INSERT_TAIL(channelvars, mcv, entry);
7984  }
7985  AST_RWLIST_UNLOCK(channelvars);
7986 
7987 }
7988 
7989 void ast_channel_set_manager_vars(size_t varc, char **vars)
7990 {
7991  channel_set_external_vars(&ami_vars, varc, vars);
7992 }
7993 
7994 void ast_channel_set_ari_vars(size_t varc, char **vars)
7995 {
7996  channel_set_external_vars(&ari_vars, varc, vars);
7997 }
7998 
7999 /*!
8000  * \brief Destructor for lists of variables.
8001  * \param obj AO2 object.
8002  */
8003 static void varshead_dtor(void *obj)
8004 {
8005  struct varshead *head = obj;
8006  struct ast_var_t *var;
8007 
8008  while ((var = AST_RWLIST_REMOVE_HEAD(head, entries))) {
8009  ast_var_delete(var);
8010  }
8011 }
8012 
8014 {
8015  RAII_VAR(struct varshead *, ret, NULL, ao2_cleanup);
8016  struct ast_var_t *cv;
8017 
8018  ret = ao2_alloc(sizeof(*ret), varshead_dtor);
8019 
8020  if (!ret) {
8021  return NULL;
8022  }
8023 
8026 
8027  if (!var) {
8028  return NULL;
8029  }
8030 
8031  AST_LIST_INSERT_TAIL(ret, var, entries);
8032  }
8033 
8034  ao2_ref(ret, +1);
8035  return ret;
8036 }
8037 
8038 static struct varshead *channel_get_external_vars(struct external_vars *channelvars,
8039  struct ast_channel *chan)
8040 {
8041  RAII_VAR(struct varshead *, ret, NULL, ao2_cleanup);
8042  RAII_VAR(struct ast_str *, tmp, NULL, ast_free);
8043  struct manager_channel_variable *mcv;
8045 
8046  if (AST_LIST_EMPTY(channelvars)) {
8047  return NULL;
8048  }
8049 
8050  ret = ao2_alloc(sizeof(*ret), varshead_dtor);
8051  tmp = ast_str_create(16);
8052 
8053  if (!ret || !tmp) {
8054  return NULL;
8055  }
8056 
8057  AST_LIST_TRAVERSE(channelvars, mcv, entry) {
8058  const char *val = NULL;
8059  struct ast_var_t *var;
8060 
8061  if (mcv->isfunc) {
8062  if (ast_func_read2(chan, mcv->name, &tmp, 0) == 0) {
8063  val = ast_str_buffer(tmp);
8064  } else {
8066  "Error invoking function %s\n", mcv->name);
8067  }
8068  } else {
8069  val = pbx_builtin_getvar_helper(chan, mcv->name);
8070  }
8071 
8072  var = ast_var_assign(mcv->name, val ? val : "");
8073  if (!var) {
8074  return NULL;
8075  }
8076 
8077  AST_RWLIST_INSERT_TAIL(ret, var, entries);
8078  }
8079 
8080  ao2_ref(ret, +1);
8081  return ret;
8082 
8083 }
8084 
8086 {
8087  return channel_get_external_vars(&ami_vars, chan);
8088 }
8089 
8091 {
8092  return channel_get_external_vars(&ari_vars, chan);
8093 }
8094 
8095 static void channels_shutdown(void)
8096 {
8099 
8100  ast_cli_unregister_multiple(cli_channel, ARRAY_LEN(cli_channel));
8101  if (channels) {
8102  ao2_container_unregister("channels");
8103  ao2_ref(channels, -1);
8104  channels = NULL;
8105  }
8106  ast_channel_unregister(&surrogate_tech);
8107 }
8108 
8110 {
8113  if (!channels) {
8114  return -1;
8115  }
8116  ao2_container_register("channels", channels, prnt_channel_key);
8117 
8118  ast_channel_register(&surrogate_tech);
8119 
8121 
8122  ast_cli_register_multiple(cli_channel, ARRAY_LEN(cli_channel));
8123 
8125 
8128 
8129  return 0;
8130 }
8131 
8132 /*! \brief Print call group and pickup group ---*/
8133 char *ast_print_group(char *buf, int buflen, ast_group_t group)
8134 {
8135  unsigned int i;
8136  int first = 1;
8137  char num[3];
8138 
8139  buf[0] = '\0';
8140 
8141  if (!group) /* Return empty string if no group */
8142  return buf;
8143 
8144  for (i = 0; i <= 63; i++) { /* Max group is 63 */
8145  if (group & ((ast_group_t) 1 << i)) {
8146  if (!first) {
8147  strncat(buf, ", ", buflen - strlen(buf) - 1);
8148  } else {
8149  first = 0;
8150  }
8151  snprintf(num, sizeof(num), "%u", i);
8152  strncat(buf, num, buflen - strlen(buf) - 1);
8153  }
8154  }
8155  return buf;
8156 }
8157 
8158 char *ast_print_namedgroups(struct ast_str **buf, struct ast_namedgroups *group)
8159 {
8160  struct ao2_container *grp = (struct ao2_container *) group;
8161  struct namedgroup_member *ng;
8162  int first = 1;
8163  struct ao2_iterator it;
8164 
8165  if (!grp) {
8166  return ast_str_buffer(*buf);
8167  }
8168 
8169  for (it = ao2_iterator_init(grp, 0); (ng = ao2_iterator_next(&it)); ao2_ref(ng, -1)) {
8170  if (!first) {
8171  ast_str_append(buf, 0, ", ");
8172  } else {
8173  first = 0;
8174  }
8175  ast_str_append(buf, 0, "%s", ng->name);
8176  }
8177  ao2_iterator_destroy(&it);
8178 
8179  return ast_str_buffer(*buf);
8180 }
8181 
8182 static int namedgroup_match(void *obj, void *arg, int flags)
8183 {
8184  void *match;
8185 
8186  match = ao2_find(arg, obj, OBJ_POINTER);
8187  ao2_cleanup(match);
8188 
8189  return match ? CMP_MATCH | CMP_STOP : 0;
8190 }
8191 
8192 int ast_namedgroups_intersect(struct ast_namedgroups *a, struct ast_namedgroups *b)
8193 {
8194  void *match;
8195  struct ao2_container *group_a = (struct ao2_container *) a;
8196  struct ao2_container *group_b = (struct ao2_container *) b;
8197 
8198  if (!a || !b) {
8199  return 0;
8200  }
8201 
8202  /*
8203  * Do groups a and b intersect? Since a and b are hash tables,
8204  * the average time complexity is:
8205  * O(a.count <= b.count ? a.count : b.count)
8206  */
8207  if (ao2_container_count(group_b) < ao2_container_count(group_a)) {
8208  /* Traverse over the smaller group. */
8209  SWAP(group_a, group_b);
8210  }
8211  match = ao2_callback(group_a, 0, namedgroup_match, group_b);
8212  ao2_cleanup(match);
8213 
8214  return match != NULL;
8215 }
8216 
8217 void ast_set_variables(struct ast_channel *chan, struct ast_variable *vars)
8218 {
8219  struct ast_variable *cur;
8220 
8221  for (cur = vars; cur; cur = cur->next) {
8222  pbx_builtin_setvar_helper(chan, cur->name, cur->value);
8223  }
8224 }
8225 
8226 static void *silence_generator_alloc(struct ast_channel *chan, void *data)
8227 {
8228  /* just store the data pointer in the channel structure */
8229  return data;
8230 }
8231 
8232 static void silence_generator_release(struct ast_channel *chan, void *data)
8233 {
8234  /* nothing to do */
8235 }
8236 
8237 static int silence_generator_generate(struct ast_channel *chan, void *data, int len, int samples)
8238 {
8239  short buf[samples];
8240  struct ast_frame frame = {
8242  .data.ptr = buf,
8243  .samples = samples,
8244  .datalen = sizeof(buf),
8245  };
8247 
8248  memset(buf, 0, sizeof(buf));
8249 
8250  if (ast_write(chan, &frame))
8251  return -1;
8252 
8253  return 0;
8254 }
8255 
8258  .release = silence_generator_release,
8259  .generate = silence_generator_generate,
8260 };
8261 
8264 };
8265 
8267 {
8268  struct ast_silence_generator *state;
8269 
8270  if (!(state = ast_calloc(1, sizeof(*state)))) {
8271  return NULL;
8272  }
8273 
8275 
8276  if (ast_set_write_format(chan, ast_format_slin) < 0) {
8277  ast_log(LOG_ERROR, "Could not set write format to SLINEAR\n");
8278  ast_free(state);
8279  return NULL;
8280  }
8281 
8282  ast_activate_generator(chan, &silence_generator, state);
8283 
8284  ast_debug(1, "Started silence generator on '%s'\n", ast_channel_name(chan));
8285 
8286  return state;
8287 }
8288 
8290 {
8291  ast_channel_lock(chan);
8292 
8293  if (!ast_channel_generatordata(chan)) {
8294  ast_debug(1, "Trying to stop silence generator when there is no generator on '%s'\n",
8295  ast_channel_name(chan));
8296  ast_channel_unlock(chan);
8297  return 0;
8298  }
8299  if (ast_channel_generator(chan) != &silence_generator) {
8300  ast_debug(1, "Trying to stop silence generator when it is not the current generator on '%s'\n",
8301  ast_channel_name(chan));
8302  ast_channel_unlock(chan);
8303  return 0;
8304  }
8306 
8307  ast_channel_unlock(chan);
8308 
8309  return 1;
8310 }
8311 
8313 {
8314  if (!state) {
8315  return;
8316  }
8317 
8318  if (deactivate_silence_generator(chan)) {
8319  ast_debug(1, "Stopped silence generator on '%s'\n", ast_channel_name(chan));
8320  if (ast_set_write_format(chan, state->old_write_format) < 0) {
8321  ast_log(LOG_ERROR, "Could not return write format to its original state\n");
8322  }
8323  }
8324  ao2_cleanup(state->old_write_format);
8325  ast_free(state);
8326 }
8327 
8328 
8329 /*
8330  * Wrappers for various ast_say_*() functions that call the full version
8331  * of the same functions.
8332  * The proper place would be say.c, but that file is optional and one
8333  * must be able to build asterisk even without it (using a loadable 'say'
8334  * implementation that only supplies the 'full' version of the functions.
8335  */
8336 
8337 int ast_say_number(struct ast_channel *chan, int num,
8338  const char *ints, const char *language, const char *options)
8339 {
8340  return ast_say_number_full(chan, num, ints, language, options, -1, -1);
8341 }
8342 
8343 int ast_say_enumeration(struct ast_channel *chan, int num,
8344  const char *ints, const char *language, const char *options)
8345 {
8346  return ast_say_enumeration_full(chan, num, ints, language, options, -1, -1);
8347 }
8348 
8349 int ast_say_digits(struct ast_channel *chan, int num,
8350  const char *ints, const char *lang)
8351 {
8352  return ast_say_digits_full(chan, num, ints, lang, -1, -1);
8353 }
8354 
8355 int ast_say_digit_str(struct ast_channel *chan, const char *str,
8356  const char *ints, const char *lang)
8357 {
8358  return ast_say_digit_str_full(chan, str, ints, lang, -1, -1);
8359 }
8360 
8361 int ast_say_money_str(struct ast_channel *chan, const char *str,
8362  const char *ints, const char *lang)
8363 {
8364  return ast_say_money_str_full(chan, str, ints, lang, -1, -1);
8365 }
8366 
8367 int ast_say_character_str(struct ast_channel *chan, const char *str,
8368  const char *ints, const char *lang, enum ast_say_case_sensitivity sensitivity)
8369 {
8370  return ast_say_character_str_full(chan, str, ints, lang, sensitivity, -1, -1);
8371 }
8372 
8373 int ast_say_phonetic_str(struct ast_channel *chan, const char *str,
8374  const char *ints, const char *lang)
8375 {
8376  return ast_say_phonetic_str_full(chan, str, ints, lang, -1, -1);
8377 }
8378 
8379 int ast_say_digits_full(struct ast_channel *chan, int num,
8380  const char *ints, const char *lang, int audiofd, int ctrlfd)
8381 {
8382  char buf[256];
8383 
8384  snprintf(buf, sizeof(buf), "%d", num);
8385 
8386  return ast_say_digit_str_full(chan, buf, ints, lang, audiofd, ctrlfd);
8387 }
8388 
8390 {
8391  ast_party_id_copy(&dest->id, &src->id);
8392  ast_party_id_copy(&dest->ani, &src->ani);
8393  dest->ani2 = src->ani2;
8394 }
8395 
8397 {
8398  ast_party_id_copy(&dest->id, &src->id);
8399  ast_party_id_copy(&dest->ani, &src->ani);
8400 
8401  dest->ani2 = src->ani2;
8402 }
8403 
8405 {
8406  if (ast_channel_connected(chan) == connected) {
8407  /* Don't set to self */
8408  return;
8409  }
8410 
8411  ast_channel_lock(chan);
8412  ast_party_connected_line_set(ast_channel_connected(chan), connected, update);
8415  ast_channel_unlock(chan);
8416 }
8417 
8418 /*! \note Should follow struct ast_party_name */
8420  /*! \brief Subscriber name ie */
8421  int str;
8422  /*! \brief Character set ie. */
8424  /*! \brief presentation-indicator ie */
8426  /*! \brief valid/present ie */
8427  int valid;
8428 };
8429 
8430 /*!
8431  * \internal
8432  * \since 1.8
8433  * \brief Build a party name information data frame component.
8434  *
8435  * \param data Buffer to fill with the frame data
8436  * \param datalen Size of the buffer to fill
8437  * \param name Party name information
8438  * \param label Name of particular party name
8439  * \param ies Data frame ie values for the party name components
8440  *
8441  * \retval -1 if error
8442  * \retval Amount of data buffer used
8443  */
8444 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)
8445 {
8446  size_t length;
8447  size_t pos = 0;
8448 
8449  /*
8450  * The size of integer values must be fixed in case the frame is
8451  * shipped to another machine.
8452  */
8453  if (name->str) {
8454  length = strlen(name->str);
8455  if (datalen < pos + (sizeof(data[0]) * 2) + length) {
8456  ast_log(LOG_WARNING, "No space left for %s name\n", label);
8457  return -1;
8458  }
8459  data[pos++] = ies->str;
8460  data[pos++] = length;
8461  memcpy(data + pos, name->str, length);
8462  pos += length;
8463  }
8464 
8465  if (datalen < pos + (sizeof(data[0]) * 2) + 1) {
8466  ast_log(LOG_WARNING, "No space left for %s name char set\n", label);
8467  return -1;
8468  }
8469  data[pos++] = ies->char_set;
8470  data[pos++] = 1;
8471  data[pos++] = name->char_set;
8472 
8473  if (datalen < pos + (sizeof(data[0]) * 2) + 1) {
8474  ast_log(LOG_WARNING, "No space left for %s name presentation\n", label);
8475  return -1;
8476  }
8477  data[pos++] = ies->presentation;
8478  data[pos++] = 1;
8479  data[pos++] = name->presentation;
8480 
8481  if (datalen < pos + (sizeof(data[0]) * 2) + 1) {
8482  ast_log(LOG_WARNING, "No space left for %s name valid\n", label);
8483  return -1;
8484  }
8485  data[pos++] = ies->valid;
8486  data[pos++] = 1;
8487  data[pos++] = name->valid;
8488 
8489  return pos;
8490 }
8491 
8492 /*! \note Should follow struct ast_party_number */
8494  /*! \brief Subscriber phone number ie */
8495  int str;
8496  /*! \brief Type-Of-Number and Numbering-Plan ie */
8497  int plan;
8498  /*! \brief presentation-indicator ie */
8500  /*! \brief valid/present ie */
8501  int valid;
8502 };
8503 
8504 /*!
8505  * \internal
8506  * \since 1.8
8507  * \brief Build a party number information data frame component.
8508  *
8509  * \param data Buffer to fill with the frame data
8510  * \param datalen Size of the buffer to fill
8511  * \param number Party number information
8512  * \param label Name of particular party number
8513  * \param ies Data frame ie values for the party number components
8514  *
8515  * \retval -1 if error
8516  * \retval Amount of data buffer used
8517  */
8518 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)
8519 {
8520  size_t length;
8521  size_t pos = 0;
8522 
8523  /*
8524  * The size of integer values must be fixed in case the frame is
8525  * shipped to another machine.
8526  */
8527  if (number->str) {
8528  length = strlen(number->str);
8529  if (datalen < pos + (sizeof(data[0]) * 2) + length) {
8530  ast_log(LOG_WARNING, "No space left for %s number\n", label);
8531  return -1;
8532  }
8533  data[pos++] = ies->str;
8534  data[pos++] = length;
8535  memcpy(data + pos, number->str, length);
8536  pos += length;
8537  }
8538 
8539  if (datalen < pos + (sizeof(data[0]) * 2) + 1) {
8540  ast_log(LOG_WARNING, "No space left for %s numbering plan\n", label);
8541  return -1;
8542  }
8543  data[pos++] = ies->plan;
8544  data[pos++] = 1;
8545  data[pos++] = number->plan;
8546 
8547  if (datalen < pos + (sizeof(data[0]) * 2) + 1) {
8548  ast_log(LOG_WARNING, "No space left for %s number presentation\n", label);
8549  return -1;
8550  }
8551  data[pos++] = ies->presentation;
8552  data[pos++] = 1;
8553  data[pos++] = number->presentation;
8554 
8555  if (datalen < pos + (sizeof(data[0]) * 2) + 1) {
8556  ast_log(LOG_WARNING, "No space left for %s number valid\n", label);
8557  return -1;
8558  }
8559  data[pos++] = ies->valid;
8560  data[pos++] = 1;
8561  data[pos++] = number->valid;
8562 
8563  return pos;
8564 }
8565 
8566 /*! \note Should follow struct ast_party_subaddress */
8568  /*! \brief subaddress ie. */
8569  int str;
8570  /*! \brief subaddress type ie */
8571  int type;
8572  /*! \brief odd/even indicator ie */
8574  /*! \brief valid/present ie */
8575  int valid;
8576 };
8577 
8578 /*!
8579  * \internal
8580  * \since 1.8
8581  * \brief Build a party subaddress information data frame component.
8582  *
8583  * \param data Buffer to fill with the frame data
8584  * \param datalen Size of the buffer to fill
8585  * \param subaddress Party subaddress information
8586  * \param label Name of particular party subaddress
8587  * \param ies Data frame ie values for the party subaddress components
8588  *
8589  * \retval -1 if error
8590  * \retval Amount of data buffer used
8591  */
8592 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)
8593 {
8594  size_t length;
8595  size_t pos = 0;
8596 
8597  /*
8598  * The size of integer values must be fixed in case the frame is
8599  * shipped to another machine.
8600  */
8601  if (subaddress->str) {
8602  length = strlen(subaddress->str);
8603  if (datalen < pos + (sizeof(data[0]) * 2) + length) {
8604  ast_log(LOG_WARNING, "No space left for %s subaddress\n", label);
8605  return -1;
8606  }
8607  data[pos++] = ies->str;
8608  data[pos++] = length;
8609  memcpy(data + pos, subaddress->str, length);
8610  pos += length;
8611  }
8612 
8613  if (datalen < pos + (sizeof(data[0]) * 2) + 1) {
8614  ast_log(LOG_WARNING, "No space left for %s type of subaddress\n", label);
8615  return -1;
8616  }
8617  data[pos++] = ies->type;
8618  data[pos++] = 1;
8619  data[pos++] = subaddress->type;
8620 
8621  if (datalen < pos + (sizeof(data[0]) * 2) + 1) {
8623  "No space left for %s subaddress odd-even indicator\n", label);
8624  return -1;
8625  }
8626  data[pos++] = ies->odd_even_indicator;
8627  data[pos++] = 1;
8628  data[pos++] = subaddress->odd_even_indicator;
8629 
8630  if (datalen < pos + (sizeof(data[0]) * 2) + 1) {
8631  ast_log(LOG_WARNING, "No space left for %s subaddress valid\n", label);
8632  return -1;
8633  }
8634  data[pos++] = ies->valid;
8635  data[pos++] = 1;
8636  data[pos++] = subaddress->valid;
8637 
8638  return pos;
8639 }
8640 
8641 /*! \note Should follow struct ast_party_id */
8643  /*! \brief Subscriber name ies */
8645  /*! \brief Subscriber phone number ies */
8647  /*! \brief Subscriber subaddress ies. */
8648  struct ast_party_subaddress_ies subaddress;
8649  /*! \brief User party id tag ie. */
8650  int tag;
8651  /*!
8652  * \brief Combined name and number presentation ie.
8653  * \note Not sent if value is zero.
8654  */
8656 };
8657 
8658 /*!
8659  * \internal
8660  * \since 1.8
8661  * \brief Build a party id information data frame component.
8662  *
8663  * \param data Buffer to fill with the frame data
8664  * \param datalen Size of the buffer to fill
8665  * \param id Party id information
8666  * \param label Name of particular party id
8667  * \param ies Data frame ie values for the party id components
8668  * \param update What id information to build. NULL if all.
8669  *
8670  * \retval -1 if error
8671  * \retval Amount of data buffer used
8672  */
8673 static int party_id_build_data(unsigned char *data, size_t datalen,
8674  const struct ast_party_id *id, const char *label, const struct ast_party_id_ies *ies,
8675  const struct ast_set_party_id *update)
8676 {
8677  size_t length;
8678  size_t pos = 0;
8679  int res;
8680 
8681  /*
8682  * The size of integer values must be fixed in case the frame is
8683  * shipped to another machine.
8684  */
8685 
8686  if (!update || update->name) {
8687  res = party_name_build_data(data + pos, datalen - pos, &id->name, label,
8688  &ies->name);
8689  if (res < 0) {
8690  return -1;
8691  }
8692  pos += res;
8693  }
8694 
8695  if (!update || update->number) {
8696  res = party_number_build_data(data + pos, datalen - pos, &id->number, label,
8697  &ies->number);
8698  if (res < 0) {
8699  return -1;
8700  }
8701  pos += res;
8702  }
8703 
8704  if (!update || update->subaddress) {
8705  res = party_subaddress_build_data(data + pos, datalen - pos, &id->subaddress,
8706  label, &ies->subaddress);
8707  if (res < 0) {
8708  return -1;
8709  }
8710  pos += res;
8711  }
8712 
8713  /* *************** Party id user tag **************************** */
8714  if (id->tag) {
8715  length = strlen(id->tag);
8716  if (datalen < pos + (sizeof(data[0]) * 2) + length) {
8717  ast_log(LOG_WARNING, "No space left for %s tag\n", label);
8718  return -1;
8719  }
8720  data[pos++] = ies->tag;
8721  data[pos++] = length;
8722  memcpy(data + pos, id->tag, length);
8723  pos += length;
8724  }
8725 
8726  /* *************** Party id combined presentation *************** */
8727  if (ies->combined_presentation && (!update || update->number)) {
8728  int presentation;
8729 
8730  if (!update || update->name) {
8731  presentation = ast_party_id_presentation(id);
8732  } else {
8733  /*
8734  * We must compromise because not all the information is available
8735  * to determine a combined presentation value.
8736  * We will only send the number presentation instead.
8737  */
8738  presentation = id->number.presentation;
8739  }
8740 
8741  if (datalen < pos + (sizeof(data[0]) * 2) + 1) {
8742  ast_log(LOG_WARNING, "No space left for %s combined presentation\n", label);
8743  return -1;
8744  }
8745  data[pos++] = ies->combined_presentation;
8746  data[pos++] = 1;
8747  data[pos++] = presentation;
8748  }
8749 
8750  return pos;
8751 }
8752 
8753 /*!
8754  * \brief Element identifiers for connected line indication frame data
8755  * \note Only add to the end of this enum.
8756  */
8757 enum {
8761  AST_CONNECTED_LINE_ID_PRESENTATION,/* Combined number and name presentation. */
8769  /*
8770  * No more party id combined number and name presentation values
8771  * need to be created.
8772  */
8791 };
8792 
8793 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)
8794 {
8795  int32_t value;
8796  size_t pos = 0;
8797  int res;
8798 
8799  static const struct ast_party_id_ies ies = {
8801  .name.char_set = AST_CONNECTED_LINE_NAME_CHAR_SET,
8802  .name.presentation = AST_CONNECTED_LINE_NAME_PRESENTATION,
8803  .name.valid = AST_CONNECTED_LINE_NAME_VALID,
8804 
8805  .number.str = AST_CONNECTED_LINE_NUMBER,
8806  .number.plan = AST_CONNECTED_LINE_NUMBER_PLAN,
8807  .number.presentation = AST_CONNECTED_LINE_NUMBER_PRESENTATION,
8808  .number.valid = AST_CONNECTED_LINE_NUMBER_VALID,
8809 
8810  .subaddress.str = AST_CONNECTED_LINE_SUBADDRESS,
8811  .subaddress.type = AST_CONNECTED_LINE_SUBADDRESS_TYPE,
8812  .subaddress.odd_even_indicator = AST_CONNECTED_LINE_SUBADDRESS_ODD_EVEN,
8813  .subaddress.valid = AST_CONNECTED_LINE_SUBADDRESS_VALID,
8814 
8815  .tag = AST_CONNECTED_LINE_TAG,
8816  .combined_presentation = AST_CONNECTED_LINE_ID_PRESENTATION,
8817  };
8818 
8819  static const struct ast_party_id_ies priv_ies = {
8821  .name.char_set = AST_CONNECTED_LINE_PRIV_NAME_CHAR_SET,
8822  .name.presentation = AST_CONNECTED_LINE_PRIV_NAME_PRESENTATION,
8823  .name.valid = AST_CONNECTED_LINE_PRIV_NAME_VALID,
8824 
8825  .number.str = AST_CONNECTED_LINE_PRIV_NUMBER,
8826  .number.plan = AST_CONNECTED_LINE_PRIV_NUMBER_PLAN,
8827  .number.presentation = AST_CONNECTED_LINE_PRIV_NUMBER_PRESENTATION,
8828  .number.valid = AST_CONNECTED_LINE_PRIV_NUMBER_VALID,
8829 
8830  .subaddress.str = AST_CONNECTED_LINE_PRIV_SUBADDRESS,
8831  .subaddress.type = AST_CONNECTED_LINE_PRIV_SUBADDRESS_TYPE,
8832  .subaddress.odd_even_indicator = AST_CONNECTED_LINE_PRIV_SUBADDRESS_ODD_EVEN,
8833  .subaddress.valid = AST_CONNECTED_LINE_PRIV_SUBADDRESS_VALID,
8834 
8836  .combined_presentation = 0,/* Not sent. */
8837  };
8838 
8839  /*
8840  * The size of integer values must be fixed in case the frame is
8841  * shipped to another machine.
8842  */
8843 
8844  /* Connected line frame version */
8845  if (datalen < pos + (sizeof(data[0]) * 2) + 1) {
8846  ast_log(LOG_WARNING, "No space left for connected line frame version\n");
8847  return -1;
8848  }
8849  data[pos++] = AST_CONNECTED_LINE_VERSION;
8850  data[pos++] = 1;
8851  data[pos++] = 2;/* Version 1 did not have a version ie */
8852 
8853  res = party_id_build_data(data + pos, datalen - pos, &connected->id,
8854  "connected line", &ies, update ? &update->id : NULL);
8855  if (res < 0) {
8856  return -1;
8857  }
8858  pos += res;
8859 
8860  res = party_id_build_data(data + pos, datalen - pos, &connected->priv,
8861  "connected line priv", &priv_ies, update ? &update->priv : NULL);
8862  if (res < 0) {
8863  return -1;
8864  }
8865  pos += res;
8866 
8867  /* Connected line source */
8868  if (datalen < pos + (sizeof(data[0]) * 2) + sizeof(value)) {
8869  ast_log(LOG_WARNING, "No space left for connected line source\n");
8870  return -1;
8871  }
8872  data[pos++] = AST_CONNECTED_LINE_SOURCE;
8873  data[pos++] = sizeof(value);
8874  value = htonl(connected->source);
8875  memcpy(data + pos, &value, sizeof(value));
8876  pos += sizeof(value);
8877 
8878  return pos;
8879 }
8880 
8881 int ast_connected_line_parse_data(const unsigned char *data, size_t datalen, struct ast_party_connected_line *connected)
8882 {
8883  size_t pos;
8884  unsigned char ie_len;
8885  unsigned char ie_id;
8886  int32_t value;
8887  int frame_version = 1;
8888  int combined_presentation = 0;
8889  int got_combined_presentation = 0;/* TRUE if got a combined name and number presentation value. */
8890 
8891  for (pos = 0; pos < datalen; pos += ie_len) {
8892  if (datalen < pos + sizeof(ie_id) + sizeof(ie_len)) {
8893  ast_log(LOG_WARNING, "Invalid connected line update\n");
8894  return -1;
8895  }
8896  ie_id = data[pos++];
8897  ie_len = data[pos++];
8898  if (datalen < pos + ie_len) {
8899  ast_log(LOG_WARNING, "Invalid connected line update\n");
8900  return -1;
8901  }
8902 
8903  switch (ie_id) {
8904 /* Connected line party frame version */
8906  if (ie_len != 1) {
8907  ast_log(LOG_WARNING, "Invalid connected line frame version (%u)\n",
8908  (unsigned) ie_len);
8909  break;
8910  }
8911  frame_version = data[pos];
8912  break;
8913 /* Connected line party id name */
8915  ast_free(connected->id.name.str);
8916  connected->id.name.str = ast_malloc(ie_len + 1);
8917  if (connected->id.name.str) {
8918  memcpy(connected->id.name.str, data + pos, ie_len);
8919  connected->id.name.str[ie_len] = 0;
8920  }
8921  break;
8923  if (ie_len != 1) {
8924  ast_log(LOG_WARNING, "Invalid connected line name char set (%u)\n",
8925  (unsigned) ie_len);
8926  break;
8927  }
8928  connected->id.name.char_set = data[pos];
8929  break;
8931  if (ie_len != 1) {
8932  ast_log(LOG_WARNING, "Invalid connected line name presentation (%u)\n",
8933  (unsigned) ie_len);
8934  break;
8935  }
8936  connected->id.name.presentation = data[pos];
8937  break;
8939  if (ie_len != 1) {
8940  ast_log(LOG_WARNING, "Invalid connected line name valid (%u)\n",
8941  (unsigned) ie_len);
8942  break;
8943  }
8944  connected->id.name.valid = data[pos];
8945  break;
8946 /* Connected line party id number */
8948  ast_free(connected->id.number.str);
8949  connected->id.number.str = ast_malloc(ie_len + 1);
8950  if (connected->id.number.str) {
8951  memcpy(connected->id.number.str, data + pos, ie_len);
8952  connected->id.number.str[ie_len] = 0;
8953  }
8954  break;
8956  if (ie_len != 1) {
8957  ast_log(LOG_WARNING, "Invalid connected line numbering plan (%u)\n",
8958  (unsigned) ie_len);
8959  break;
8960  }
8961  connected->id.number.plan = data[pos];
8962  break;
8964  if (ie_len != 1) {
8965  ast_log(LOG_WARNING, "Invalid connected line number presentation (%u)\n",
8966  (unsigned) ie_len);
8967  break;
8968  }
8969  connected->id.number.presentation = data[pos];
8970  break;
8972  if (ie_len != 1) {
8973  ast_log(LOG_WARNING, "Invalid connected line number valid (%u)\n",
8974  (unsigned) ie_len);
8975  break;
8976  }
8977  connected->id.number.valid = data[pos];
8978  break;
8979 /* Connected line party id subaddress */
8981  ast_free(connected->id.subaddress.str);
8982  connected->id.subaddress.str = ast_malloc(ie_len + 1);
8983  if (connected->id.subaddress.str) {
8984  memcpy(connected->id.subaddress.str, data + pos, ie_len);
8985  connected->id.subaddress.str[ie_len] = 0;
8986  }
8987  break;
8989  if (ie_len != 1) {
8990  ast_log(LOG_WARNING, "Invalid connected line type of subaddress (%u)\n",
8991  (unsigned) ie_len);
8992  break;
8993  }
8994  connected->id.subaddress.type = data[pos];
8995  break;
8997  if (ie_len != 1) {
8999  "Invalid connected line subaddress odd-even indicator (%u)\n",
9000  (unsigned) ie_len);
9001  break;
9002  }
9003  connected->id.subaddress.odd_even_indicator = data[pos];
9004  break;
9006  if (ie_len != 1) {
9007  ast_log(LOG_WARNING, "Invalid connected line subaddress valid (%u)\n",
9008  (unsigned) ie_len);
9009  break;
9010  }
9011  connected->id.subaddress.valid = data[pos];
9012  break;
9013 /* Connected line party tag */
9015  ast_free(connected->id.tag);
9016  connected->id.tag = ast_malloc(ie_len + 1);
9017  if (connected->id.tag) {
9018  memcpy(connected->id.tag, data + pos, ie_len);
9019  connected->id.tag[ie_len] = 0;
9020  }
9021  break;
9022 /* Connected line party id combined presentation */
9024  if (ie_len != 1) {
9025  ast_log(LOG_WARNING, "Invalid connected line combined presentation (%u)\n",
9026  (unsigned) ie_len);
9027  break;
9028  }
9029  combined_presentation = data[pos];
9030  got_combined_presentation = 1;
9031  break;
9032 /* Private connected line party id name */
9034  ast_free(connected->priv.name.str);
9035  connected->priv.name.str = ast_malloc(ie_len + 1);
9036  if (connected->priv.name.str) {
9037  memcpy(connected->priv.name.str, data + pos, ie_len);
9038  connected->priv.name.str[ie_len] = 0;
9039  }
9040  break;
9042  if (ie_len != 1) {
9043  ast_log(LOG_WARNING, "Invalid connected line private name char set (%u)\n",
9044  (unsigned) ie_len);
9045  break;
9046  }
9047  connected->priv.name.char_set = data[pos];
9048  break;
9050  if (ie_len != 1) {
9051  ast_log(LOG_WARNING, "Invalid connected line private name presentation (%u)\n",
9052  (unsigned) ie_len);
9053  break;
9054  }
9055  connected->priv.name.presentation = data[pos];
9056  break;
9058  if (ie_len != 1) {
9059  ast_log(LOG_WARNING, "Invalid connected line private name valid (%u)\n",
9060  (unsigned) ie_len);
9061  break;
9062  }
9063  connected->priv.name.valid = data[pos];
9064  break;
9065 /* Private connected line party id number */
9067  ast_free(connected->priv.number.str);
9068  connected->priv.number.str = ast_malloc(ie_len + 1);
9069  if (connected->priv.number.str) {
9070  memcpy(connected->priv.number.str, data + pos, ie_len);
9071  connected->priv.number.str[ie_len] = 0;
9072  }
9073  break;
9075  if (ie_len != 1) {
9076  ast_log(LOG_WARNING, "Invalid connected line private numbering plan (%u)\n",
9077  (unsigned) ie_len);
9078  break;
9079  }
9080  connected->priv.number.plan = data[pos];
9081  break;
9083  if (ie_len != 1) {
9084  ast_log(LOG_WARNING, "Invalid connected line private number presentation (%u)\n",
9085  (unsigned) ie_len);
9086  break;
9087  }
9088  connected->priv.number.presentation = data[pos];
9089  break;
9091  if (ie_len != 1) {
9092  ast_log(LOG_WARNING, "Invalid connected line private number valid (%u)\n",
9093  (unsigned) ie_len);
9094  break;
9095  }
9096  connected->priv.number.valid = data[pos];
9097  break;
9098 /* Private connected line party id subaddress */
9100  ast_free(connected->priv.subaddress.str);
9101  connected->priv.subaddress.str = ast_malloc(ie_len + 1);
9102  if (connected->priv.subaddress.str) {
9103  memcpy(connected->priv.subaddress.str, data + pos, ie_len);
9104  connected->priv.subaddress.str[ie_len] = 0;
9105  }
9106  break;
9108  if (ie_len != 1) {
9109  ast_log(LOG_WARNING, "Invalid connected line private type of subaddress (%u)\n",
9110  (unsigned) ie_len);
9111  break;
9112  }
9113  connected->priv.subaddress.type = data[pos];
9114  break;
9116  if (ie_len != 1) {
9118  "Invalid connected line private subaddress odd-even indicator (%u)\n",
9119  (unsigned) ie_len);
9120  break;
9121  }
9122  connected->priv.subaddress.odd_even_indicator = data[pos];
9123  break;
9125  if (ie_len != 1) {
9126  ast_log(LOG_WARNING, "Invalid connected line private subaddress valid (%u)\n",
9127  (unsigned) ie_len);
9128  break;
9129  }
9130  connected->priv.subaddress.valid = data[pos];
9131  break;
9132 /* Private connected line party tag */
9134  ast_free(connected->priv.tag);
9135  connected->priv.tag = ast_malloc(ie_len + 1);
9136  if (connected->priv.tag) {
9137  memcpy(connected->priv.tag, data + pos, ie_len);
9138  connected->priv.tag[ie_len] = 0;
9139  }
9140  break;
9141 /* Connected line party source */
9143  if (ie_len != sizeof(value)) {
9144  ast_log(LOG_WARNING, "Invalid connected line source (%u)\n",
9145  (unsigned) ie_len);
9146  break;
9147  }
9148  memcpy(&value, data + pos, sizeof(value));
9149  connected->source = ntohl(value);
9150  break;
9151 /* Connected line party unknown element */
9152  default:
9153  ast_debug(1, "Unknown connected line element: %u (%u)\n",
9154  (unsigned) ie_id, (unsigned) ie_len);
9155  break;
9156  }
9157  }
9158 
9159  switch (frame_version) {
9160  case 1:
9161  /*
9162  * The other end is an earlier version that we need to adjust
9163  * for compatibility.
9164  */
9165  connected->id.name.valid = 1;
9167  connected->id.number.valid = 1;
9168  if (got_combined_presentation) {
9171  }
9172  break;
9173  case 2:
9174  /* The other end is at the same level as we are. */
9175  break;
9176  default:
9177  /*
9178  * The other end is newer than we are.
9179  * We need to assume that they are compatible with us.
9180  */
9181  ast_debug(1, "Connected line frame has newer version: %u\n",
9182  (unsigned) frame_version);
9183  break;
9184  }
9185 
9186  return 0;
9187 }
9188 
9190 {
9191  unsigned char data[1024]; /* This should be large enough */
9192  size_t datalen;
9193 
9194  datalen = ast_connected_line_build_data(data, sizeof(data), connected, update);
9195  if (datalen == (size_t) -1) {
9196  return;
9197  }
9198 
9199  ast_indicate_data(chan, AST_CONTROL_CONNECTED_LINE, data, datalen);
9200 }
9201 
9203 {
9204  unsigned char data[1024]; /* This should be large enough */
9205  size_t datalen;
9206 
9207  datalen = ast_connected_line_build_data(data, sizeof(data), connected, update);
9208  if (datalen == (size_t) -1) {
9209  return;
9210  }
9211 
9212  ast_queue_control_data(chan, AST_CONTROL_CONNECTED_LINE, data, datalen);
9213 }
9214 
9215 void ast_channel_set_redirecting(struct ast_channel *chan, const struct ast_party_redirecting *redirecting, const struct ast_set_party_redirecting *update)
9216 {
9217  if (ast_channel_redirecting(chan) == redirecting) {
9218  /* Don't set to self */
9219  return;
9220  }
9221 
9222  ast_channel_lock(chan);
9223  ast_party_redirecting_set(ast_channel_redirecting(chan), redirecting, update);
9225  ast_channel_unlock(chan);
9226 }
9227 
9228 /*!
9229  * \brief Element identifiers for redirecting indication frame data
9230  * \note Only add to the end of this enum.
9231  */
9232 enum {
9236  AST_REDIRECTING_FROM_ID_PRESENTATION,/* Combined number and name presentation. */
9240  AST_REDIRECTING_TO_ID_PRESENTATION,/* Combined number and name presentation. */
9254  /*
9255  * No more party id combined number and name presentation values
9256  * need to be created.
9257  */
9323 };
9324 
9326  int code;
9327  int str;
9328 };
9329 
9330 static int redirecting_reason_build_data(unsigned char *data, size_t datalen,
9331  const struct ast_party_redirecting_reason *reason, const char *label,
9332  const struct ast_party_redirecting_reason_ies *ies)
9333 {
9334  size_t length;
9335  size_t pos = 0;
9336  int32_t value;
9337 
9338  if (datalen < pos + (sizeof(data[0]) * 2) + sizeof(value)) {
9339  ast_log(LOG_WARNING, "No space left for %s code\n", label);
9340  return -1;
9341  }
9342  data[pos++] = ies->code;
9343  data[pos++] = sizeof(value);
9344  value = htonl(reason->code);
9345  memcpy(data + pos, &value, sizeof(value));
9346  pos += sizeof(value);
9347 
9348  if (reason->str) {
9349  length = strlen(reason->str);
9350  if (datalen < pos + (sizeof(data[0]) * 2) + length) {
9351  ast_log(LOG_WARNING, "No space left for %s string\n", label);
9352  return -1;
9353  }
9354  data[pos++] = ies->str;
9355  data[pos++] = length;
9356  memcpy(data + pos, reason->str, length);
9357  pos += length;
9358  }
9359 
9360  return pos;
9361 }
9362 
9363 int ast_redirecting_build_data(unsigned char *data, size_t datalen, const struct ast_party_redirecting *redirecting, const struct ast_set_party_redirecting *update)
9364 {
9365  int32_t value;
9366  size_t pos = 0;
9367  int res;
9368 
9369  static const struct ast_party_id_ies orig_ies = {
9371  .name.char_set = AST_REDIRECTING_ORIG_NAME_CHAR_SET,
9372  .name.presentation = AST_REDIRECTING_ORIG_NAME_PRESENTATION,
9373  .name.valid = AST_REDIRECTING_ORIG_NAME_VALID,
9374 
9375  .number.str = AST_REDIRECTING_ORIG_NUMBER,
9376  .number.plan = AST_REDIRECTING_ORIG_NUMBER_PLAN,
9377  .number.presentation = AST_REDIRECTING_ORIG_NUMBER_PRESENTATION,
9378  .number.valid = AST_REDIRECTING_ORIG_NUMBER_VALID,
9379 
9380  .subaddress.str = AST_REDIRECTING_ORIG_SUBADDRESS,
9381  .subaddress.type = AST_REDIRECTING_ORIG_SUBADDRESS_TYPE,
9382  .subaddress.odd_even_indicator = AST_REDIRECTING_ORIG_SUBADDRESS_ODD_EVEN,
9383  .subaddress.valid = AST_REDIRECTING_ORIG_SUBADDRESS_VALID,
9384 
9385  .tag = AST_REDIRECTING_ORIG_TAG,
9386  .combined_presentation = 0,/* Not sent. */
9387  };
9388  static const struct ast_party_id_ies from_ies = {
9390  .name.char_set = AST_REDIRECTING_FROM_NAME_CHAR_SET,
9391  .name.presentation = AST_REDIRECTING_FROM_NAME_PRESENTATION,
9392  .name.valid = AST_REDIRECTING_FROM_NAME_VALID,
9393 
9394  .number.str = AST_REDIRECTING_FROM_NUMBER,
9395  .number.plan = AST_REDIRECTING_FROM_NUMBER_PLAN,
9396  .number.presentation = AST_REDIRECTING_FROM_NUMBER_PRESENTATION,
9397  .number.valid = AST_REDIRECTING_FROM_NUMBER_VALID,
9398 
9399  .subaddress.str = AST_REDIRECTING_FROM_SUBADDRESS,
9400  .subaddress.type = AST_REDIRECTING_FROM_SUBADDRESS_TYPE,
9401  .subaddress.odd_even_indicator = AST_REDIRECTING_FROM_SUBADDRESS_ODD_EVEN,
9402  .subaddress.valid = AST_REDIRECTING_FROM_SUBADDRESS_VALID,
9403 
9404  .tag = AST_REDIRECTING_FROM_TAG,
9405  .combined_presentation = AST_REDIRECTING_FROM_ID_PRESENTATION,
9406  };
9407  static const struct ast_party_id_ies to_ies = {
9409  .name.char_set = AST_REDIRECTING_TO_NAME_CHAR_SET,
9410  .name.presentation = AST_REDIRECTING_TO_NAME_PRESENTATION,
9411  .name.valid = AST_REDIRECTING_TO_NAME_VALID,
9412 
9413  .number.str = AST_REDIRECTING_TO_NUMBER,
9414  .number.plan = AST_REDIRECTING_TO_NUMBER_PLAN,
9415  .number.presentation = AST_REDIRECTING_TO_NUMBER_PRESENTATION,
9416  .number.valid = AST_REDIRECTING_TO_NUMBER_VALID,
9417 
9418  .subaddress.str = AST_REDIRECTING_TO_SUBADDRESS,
9419  .subaddress.type = AST_REDIRECTING_TO_SUBADDRESS_TYPE,
9420  .subaddress.odd_even_indicator = AST_REDIRECTING_TO_SUBADDRESS_ODD_EVEN,
9421  .subaddress.valid = AST_REDIRECTING_TO_SUBADDRESS_VALID,
9422 
9423  .tag = AST_REDIRECTING_TO_TAG,
9424  .combined_presentation = AST_REDIRECTING_TO_ID_PRESENTATION,
9425  };
9426  static const struct ast_party_id_ies priv_orig_ies = {
9428  .name.char_set = AST_REDIRECTING_PRIV_ORIG_NAME_CHAR_SET,
9429  .name.presentation = AST_REDIRECTING_PRIV_ORIG_NAME_PRESENTATION,
9431 
9432  .number.str = AST_REDIRECTING_PRIV_ORIG_NUMBER,
9434  .number.presentation = AST_REDIRECTING_PRIV_ORIG_NUMBER_PRESENTATION,
9436 
9437  .subaddress.str = AST_REDIRECTING_PRIV_ORIG_SUBADDRESS,
9438  .subaddress.type = AST_REDIRECTING_PRIV_ORIG_SUBADDRESS_TYPE,
9439  .subaddress.odd_even_indicator = AST_REDIRECTING_PRIV_ORIG_SUBADDRESS_ODD_EVEN,
9440  .subaddress.valid = AST_REDIRECTING_PRIV_ORIG_SUBADDRESS_VALID,
9441 
9443  .combined_presentation = 0,/* Not sent. */
9444  };
9445  static const struct ast_party_id_ies priv_from_ies = {
9447  .name.char_set = AST_REDIRECTING_PRIV_FROM_NAME_CHAR_SET,
9448  .name.presentation = AST_REDIRECTING_PRIV_FROM_NAME_PRESENTATION,
9450 
9451  .number.str = AST_REDIRECTING_PRIV_FROM_NUMBER,
9453  .number.presentation = AST_REDIRECTING_PRIV_FROM_NUMBER_PRESENTATION,
9455 
9456  .subaddress.str = AST_REDIRECTING_PRIV_FROM_SUBADDRESS,
9457  .subaddress.type = AST_REDIRECTING_PRIV_FROM_SUBADDRESS_TYPE,
9458  .subaddress.odd_even_indicator = AST_REDIRECTING_PRIV_FROM_SUBADDRESS_ODD_EVEN,
9459  .subaddress.valid = AST_REDIRECTING_PRIV_FROM_SUBADDRESS_VALID,
9460 
9462  .combined_presentation = 0,/* Not sent. */
9463  };
9464  static const struct ast_party_id_ies priv_to_ies = {
9466  .name.char_set = AST_REDIRECTING_PRIV_TO_NAME_CHAR_SET,
9467  .name.presentation = AST_REDIRECTING_PRIV_TO_NAME_PRESENTATION,
9468  .name.valid = AST_REDIRECTING_PRIV_TO_NAME_VALID,
9469 
9470  .number.str = AST_REDIRECTING_PRIV_TO_NUMBER,
9471  .number.plan = AST_REDIRECTING_PRIV_TO_NUMBER_PLAN,
9472  .number.presentation = AST_REDIRECTING_PRIV_TO_NUMBER_PRESENTATION,
9473  .number.valid = AST_REDIRECTING_PRIV_TO_NUMBER_VALID,
9474 
9475  .subaddress.str = AST_REDIRECTING_PRIV_TO_SUBADDRESS,
9476  .subaddress.type = AST_REDIRECTING_PRIV_TO_SUBADDRESS_TYPE,
9477  .subaddress.odd_even_indicator = AST_REDIRECTING_PRIV_TO_SUBADDRESS_ODD_EVEN,
9478  .subaddress.valid = AST_REDIRECTING_PRIV_TO_SUBADDRESS_VALID,
9479 
9481  .combined_presentation = 0,/* Not sent. */
9482  };
9483  static const struct ast_party_redirecting_reason_ies reason_ies = {
9486  };
9487 
9488  static const struct ast_party_redirecting_reason_ies orig_reason_ies = {
9491  };
9492 
9493  /* Redirecting frame version */
9494  if (datalen < pos + (sizeof(data[0]) * 2) + 1) {
9495  ast_log(LOG_WARNING, "No space left for redirecting frame version\n");
9496  return -1;
9497  }
9498  data[pos++] = AST_REDIRECTING_VERSION;
9499  data[pos++] = 1;
9500  data[pos++] = 2;/* Version 1 did not have a version ie */
9501 
9502  res = party_id_build_data(data + pos, datalen - pos, &redirecting->orig,
9503  "redirecting-orig", &orig_ies, update ? &update->orig : NULL);
9504  if (res < 0) {
9505  return -1;
9506  }
9507  pos += res;
9508 
9509  res = party_id_build_data(data + pos, datalen - pos, &redirecting->from,
9510  "redirecting-from", &from_ies, update ? &update->from : NULL);
9511  if (res < 0) {
9512  return -1;
9513  }
9514  pos += res;
9515 
9516  res = party_id_build_data(data + pos, datalen - pos, &redirecting->to,
9517  "redirecting-to", &to_ies, update ? &update->to : NULL);
9518  if (res < 0) {
9519  return -1;
9520  }
9521  pos += res;
9522 
9523  res = party_id_build_data(data + pos, datalen - pos, &redirecting->priv_orig,
9524  "redirecting-priv-orig", &priv_orig_ies, update ? &update->priv_orig : NULL);
9525  if (res < 0) {
9526  return -1;
9527  }
9528  pos += res;
9529 
9530  res = party_id_build_data(data + pos, datalen - pos, &redirecting->priv_from,
9531  "redirecting-priv-from", &priv_from_ies, update ? &update->priv_from : NULL);
9532  if (res < 0) {
9533  return -1;
9534  }
9535  pos += res;
9536 
9537  res = party_id_build_data(data + pos, datalen - pos, &redirecting->priv_to,
9538  "redirecting-priv-to", &priv_to_ies, update ? &update->priv_to : NULL);
9539  if (res < 0) {
9540  return -1;
9541  }
9542  pos += res;
9543 
9544  /* Redirecting reason */
9545  res = redirecting_reason_build_data(data + pos, datalen - pos, &redirecting->reason,
9546  "redirecting-reason", &reason_ies);
9547  if (res < 0) {
9548  return -1;
9549  }
9550  pos += res;
9551 
9552  /* Redirecting original reason */
9553  res = redirecting_reason_build_data(data + pos, datalen - pos, &redirecting->orig_reason,
9554  "redirecting-orig-reason", &orig_reason_ies);
9555  if (res < 0) {
9556  return -1;
9557  }
9558  pos += res;
9559 
9560  /* Redirecting count */
9561  if (datalen < pos + (sizeof(data[0]) * 2) + sizeof(value)) {
9562  ast_log(LOG_WARNING, "No space left for redirecting count\n");
9563  return -1;
9564  }
9565  data[pos++] = AST_REDIRECTING_COUNT;
9566  data[pos++] = sizeof(value);
9567  value = htonl(redirecting->count);
9568  memcpy(data + pos, &value, sizeof(value));
9569  pos += sizeof(value);
9570 
9571  return pos;
9572 }
9573 
9574 int ast_redirecting_parse_data(const unsigned char *data, size_t datalen, struct ast_party_redirecting *redirecting)
9575 {
9576  size_t pos;
9577  unsigned char ie_len;
9578  unsigned char ie_id;
9579  int32_t value;
9580  int frame_version = 1;
9581  int from_combined_presentation = 0;
9582  int got_from_combined_presentation = 0;/* TRUE if got a combined name and number presentation value. */
9583  int to_combined_presentation = 0;
9584  int got_to_combined_presentation = 0;/* TRUE if got a combined name and number presentation value. */
9585 
9586  for (pos = 0; pos < datalen; pos += ie_len) {
9587  if (datalen < pos + sizeof(ie_id) + sizeof(ie_len)) {
9588  ast_log(LOG_WARNING, "Invalid redirecting update\n");
9589  return -1;
9590  }
9591  ie_id = data[pos++];
9592  ie_len = data[pos++];
9593  if (datalen < pos + ie_len) {
9594  ast_log(LOG_WARNING, "Invalid redirecting update\n");
9595  return -1;
9596  }
9597 
9598  switch (ie_id) {
9599 /* Redirecting frame version */
9601  if (ie_len != 1) {
9602  ast_log(LOG_WARNING, "Invalid redirecting frame version (%u)\n",
9603  (unsigned) ie_len);
9604  break;
9605  }
9606  frame_version = data[pos];
9607  break;
9608 /* Redirecting-orig party id name */
9610  ast_free(redirecting->orig.name.str);
9611  redirecting->orig.name.str = ast_malloc(ie_len + 1);
9612  if (redirecting->orig.name.str) {
9613  memcpy(redirecting->orig.name.str, data + pos, ie_len);
9614  redirecting->orig.name.str[ie_len] = 0;
9615  }
9616  break;
9618  if (ie_len != 1) {
9619  ast_log(LOG_WARNING, "Invalid redirecting-orig name char set (%u)\n",
9620  (unsigned) ie_len);
9621  break;
9622  }
9623  redirecting->orig.name.char_set = data[pos];
9624  break;
9626  if (ie_len != 1) {
9627  ast_log(LOG_WARNING, "Invalid redirecting-orig name presentation (%u)\n",
9628  (unsigned) ie_len);
9629  break;
9630  }
9631  redirecting->orig.name.presentation = data[pos];
9632  break;
9634  if (ie_len != 1) {
9635  ast_log(LOG_WARNING, "Invalid redirecting-orig name valid (%u)\n",
9636  (unsigned) ie_len);
9637  break;
9638  }
9639  redirecting->orig.name.valid = data[pos];
9640  break;
9641 /* Redirecting-orig party id number */
9643  ast_free(redirecting->orig.number.str);
9644  redirecting->orig.number.str = ast_malloc(ie_len + 1);
9645  if (redirecting->orig.number.str) {
9646  memcpy(redirecting->orig.number.str, data + pos, ie_len);
9647  redirecting->orig.number.str[ie_len] = 0;
9648  }
9649  break;
9651  if (ie_len != 1) {
9652  ast_log(LOG_WARNING, "Invalid redirecting-orig numbering plan (%u)\n",
9653  (unsigned) ie_len);
9654  break;
9655  }
9656  redirecting->orig.number.plan = data[pos];
9657  break;
9659  if (ie_len != 1) {
9660  ast_log(LOG_WARNING, "Invalid redirecting-orig number presentation (%u)\n",
9661  (unsigned) ie_len);
9662  break;
9663  }
9664  redirecting->orig.number.presentation = data[pos];
9665  break;
9667  if (ie_len != 1) {
9668  ast_log(LOG_WARNING, "Invalid redirecting-orig number valid (%u)\n",
9669  (unsigned) ie_len);
9670  break;
9671  }
9672  redirecting->orig.number.valid = data[pos];
9673  break;
9674 /* Redirecting-orig party id subaddress */
9676  ast_free(redirecting->orig.subaddress.str);
9677  redirecting->orig.subaddress.str = ast_malloc(ie_len + 1);
9678  if (redirecting->orig.subaddress.str) {
9679  memcpy(redirecting->orig.subaddress.str, data + pos, ie_len);
9680  redirecting->orig.subaddress.str[ie_len] = 0;
9681  }
9682  break;
9684  if (ie_len != 1) {
9685  ast_log(LOG_WARNING, "Invalid redirecting-orig type of subaddress (%u)\n",
9686  (unsigned) ie_len);
9687  break;
9688  }
9689  redirecting->orig.subaddress.type = data[pos];
9690  break;
9692  if (ie_len != 1) {
9694  "Invalid redirecting-orig subaddress odd-even indicator (%u)\n",
9695  (unsigned) ie_len);
9696  break;
9697  }
9698  redirecting->orig.subaddress.odd_even_indicator = data[pos];
9699  break;
9701  if (ie_len != 1) {
9702  ast_log(LOG_WARNING, "Invalid redirecting-orig subaddress valid (%u)\n",
9703  (unsigned) ie_len);
9704  break;
9705  }
9706  redirecting->orig.subaddress.valid = data[pos];
9707  break;
9708 /* Redirecting-orig party id tag */
9710  ast_free(redirecting->orig.tag);
9711  redirecting->orig.tag = ast_malloc(ie_len + 1);
9712  if (redirecting->orig.tag) {
9713  memcpy(redirecting->orig.tag, data + pos, ie_len);
9714  redirecting->orig.tag[ie_len] = 0;
9715  }
9716  break;
9717 /* Redirecting-from party id name */
9719  ast_free(redirecting->from.name.str);
9720  redirecting->from.name.str = ast_malloc(ie_len + 1);
9721  if (redirecting->from.name.str) {
9722  memcpy(redirecting->from.name.str, data + pos, ie_len);
9723  redirecting->from.name.str[ie_len] = 0;
9724  }
9725  break;
9727  if (ie_len != 1) {
9728  ast_log(LOG_WARNING, "Invalid redirecting-from name char set (%u)\n",
9729  (unsigned) ie_len);
9730  break;
9731  }
9732  redirecting->from.name.char_set = data[pos];
9733  break;
9735  if (ie_len != 1) {
9736  ast_log(LOG_WARNING, "Invalid redirecting-from name presentation (%u)\n",
9737  (unsigned) ie_len);
9738  break;
9739  }
9740  redirecting->from.name.presentation = data[pos];
9741  break;
9743  if (ie_len != 1) {
9744  ast_log(LOG_WARNING, "Invalid redirecting-from name valid (%u)\n",
9745  (unsigned) ie_len);
9746  break;
9747  }
9748  redirecting->from.name.valid = data[pos];
9749  break;
9750 /* Redirecting-from party id number */
9752  ast_free(redirecting->from.number.str);
9753  redirecting->from.number.str = ast_malloc(ie_len + 1);
9754  if (redirecting->from.number.str) {
9755  memcpy(redirecting->from.number.str, data + pos, ie_len);
9756  redirecting->from.number.str[ie_len] = 0;
9757  }
9758  break;
9760  if (ie_len != 1) {
9761  ast_log(LOG_WARNING, "Invalid redirecting-from numbering plan (%u)\n",
9762  (unsigned) ie_len);
9763  break;
9764  }
9765  redirecting->from.number.plan = data[pos];
9766  break;
9768  if (ie_len != 1) {
9769  ast_log(LOG_WARNING, "Invalid redirecting-from number presentation (%u)\n",
9770  (unsigned) ie_len);
9771  break;
9772  }
9773  redirecting->from.number.presentation = data[pos];
9774  break;
9776  if (ie_len != 1) {
9777  ast_log(LOG_WARNING, "Invalid redirecting-from number valid (%u)\n",
9778  (unsigned) ie_len);
9779  break;
9780  }
9781  redirecting->from.number.valid = data[pos];
9782  break;
9783 /* Redirecting-from party id combined presentation */
9785  if (ie_len != 1) {
9786  ast_log(LOG_WARNING, "Invalid redirecting-from combined presentation (%u)\n",
9787  (unsigned) ie_len);
9788  break;
9789  }
9790  from_combined_presentation = data[pos];
9791  got_from_combined_presentation = 1;
9792  break;
9793 /* Redirecting-from party id subaddress */
9795  ast_free(redirecting->from.subaddress.str);
9796  redirecting->from.subaddress.str = ast_malloc(ie_len + 1);
9797  if (redirecting->from.subaddress.str) {
9798  memcpy(redirecting->from.subaddress.str, data + pos, ie_len);
9799  redirecting->from.subaddress.str[ie_len] = 0;
9800  }
9801  break;
9803  if (ie_len != 1) {
9804  ast_log(LOG_WARNING, "Invalid redirecting-from type of subaddress (%u)\n",
9805  (unsigned) ie_len);
9806  break;
9807  }
9808  redirecting->from.subaddress.type = data[pos];
9809  break;
9811  if (ie_len != 1) {
9813  "Invalid redirecting-from subaddress odd-even indicator (%u)\n",
9814  (unsigned) ie_len);
9815  break;
9816  }
9817  redirecting->from.subaddress.odd_even_indicator = data[pos];
9818  break;
9820  if (ie_len != 1) {
9821  ast_log(LOG_WARNING, "Invalid redirecting-from subaddress valid (%u)\n",
9822  (unsigned) ie_len);
9823  break;
9824  }
9825  redirecting->from.subaddress.valid = data[pos];
9826  break;
9827 /* Redirecting-from party id tag */
9829  ast_free(redirecting->from.tag);
9830  redirecting->from.tag = ast_malloc(ie_len + 1);
9831  if (redirecting->from.tag) {
9832  memcpy(redirecting->from.tag, data + pos, ie_len);
9833  redirecting->from.tag[ie_len] = 0;
9834  }
9835  break;
9836 /* Redirecting-to party id name */
9838  ast_free(redirecting->to.name.str);
9839  redirecting->to.name.str = ast_malloc(ie_len + 1);
9840  if (redirecting->to.name.str) {
9841  memcpy(redirecting->to.name.str, data + pos, ie_len);
9842  redirecting->to.name.str[ie_len] = 0;
9843  }
9844  break;
9846  if (ie_len != 1) {
9847  ast_log(LOG_WARNING, "Invalid redirecting-to name char set (%u)\n",
9848  (unsigned) ie_len);
9849  break;
9850  }
9851  redirecting->to.name.char_set = data[pos];
9852  break;
9854  if (ie_len != 1) {
9855  ast_log(LOG_WARNING, "Invalid redirecting-to name presentation (%u)\n",
9856  (unsigned) ie_len);
9857  break;
9858  }
9859  redirecting->to.name.presentation = data[pos];
9860  break;
9862  if (ie_len != 1) {
9863  ast_log(LOG_WARNING, "Invalid redirecting-to name valid (%u)\n",
9864  (unsigned) ie_len);
9865  break;
9866  }
9867  redirecting->to.name.valid = data[pos];
9868  break;
9869 /* Redirecting-to party id number */
9871  ast_free(redirecting->to.number.str);
9872  redirecting->to.number.str = ast_malloc(ie_len + 1);
9873  if (redirecting->to.number.str) {
9874  memcpy(redirecting->to.number.str, data + pos, ie_len);
9875  redirecting->to.number.str[ie_len] = 0;
9876  }
9877  break;
9879  if (ie_len != 1) {
9880  ast_log(LOG_WARNING, "Invalid redirecting-to numbering plan (%u)\n",
9881  (unsigned) ie_len);
9882  break;
9883  }
9884  redirecting->to.number.plan = data[pos];
9885  break;
9887  if (ie_len != 1) {
9888  ast_log(LOG_WARNING, "Invalid redirecting-to number presentation (%u)\n",
9889  (unsigned) ie_len);
9890  break;
9891  }
9892  redirecting->to.number.presentation = data[pos];
9893  break;
9895  if (ie_len != 1) {
9896  ast_log(LOG_WARNING, "Invalid redirecting-to number valid (%u)\n",
9897  (unsigned) ie_len);
9898  break;
9899  }
9900  redirecting->to.number.valid = data[pos];
9901  break;
9902 /* Redirecting-to party id combined presentation */
9904  if (ie_len != 1) {
9905  ast_log(LOG_WARNING, "Invalid redirecting-to combined presentation (%u)\n",
9906  (unsigned) ie_len);
9907  break;
9908  }
9909  to_combined_presentation = data[pos];
9910  got_to_combined_presentation = 1;
9911  break;
9912 /* Redirecting-to party id subaddress */
9914  ast_free(redirecting->to.subaddress.str);
9915  redirecting->to.subaddress.str = ast_malloc(ie_len + 1);
9916  if (redirecting->to.subaddress.str) {
9917  memcpy(redirecting->to.subaddress.str, data + pos, ie_len);
9918  redirecting->to.subaddress.str[ie_len] = 0;
9919  }
9920  break;
9922  if (ie_len != 1) {
9923  ast_log(LOG_WARNING, "Invalid redirecting-to type of subaddress (%u)\n",
9924  (unsigned) ie_len);
9925  break;
9926  }
9927  redirecting->to.subaddress.type = data[pos];
9928  break;
9930  if (ie_len != 1) {
9932  "Invalid redirecting-to subaddress odd-even indicator (%u)\n",
9933  (unsigned) ie_len);
9934  break;
9935  }
9936  redirecting->to.subaddress.odd_even_indicator = data[pos];
9937  break;
9939  if (ie_len != 1) {
9940  ast_log(LOG_WARNING, "Invalid redirecting-to subaddress valid (%u)\n",
9941  (unsigned) ie_len);
9942  break;
9943  }
9944  redirecting->to.subaddress.valid = data[pos];
9945  break;
9946 /* Redirecting-to party id tag */
9948  ast_free(redirecting->to.tag);
9949  redirecting->to.tag = ast_malloc(ie_len + 1);
9950  if (redirecting->to.tag) {
9951  memcpy(redirecting->to.tag, data + pos, ie_len);
9952  redirecting->to.tag[ie_len] = 0;
9953  }
9954  break;
9955 /* Private redirecting-orig party id name */
9957  ast_free(redirecting->priv_orig.name.str);
9958  redirecting->priv_orig.name.str = ast_malloc(ie_len + 1);
9959  if (redirecting->priv_orig.name.str) {
9960  memcpy(redirecting->priv_orig.name.str, data + pos, ie_len);
9961  redirecting->priv_orig.name.str[ie_len] = 0;
9962  }
9963  break;
9965  if (ie_len != 1) {
9966  ast_log(LOG_WARNING, "Invalid private redirecting-orig name char set (%u)\n",
9967  (unsigned) ie_len);
9968  break;
9969  }
9970  redirecting->priv_orig.name.char_set = data[pos];
9971  break;
9973  if (ie_len != 1) {
9974  ast_log(LOG_WARNING, "Invalid private redirecting-orig name presentation (%u)\n",
9975  (unsigned) ie_len);
9976  break;
9977  }
9978  redirecting->priv_orig.name.presentation = data[pos];
9979  break;
9981  if (ie_len != 1) {
9982  ast_log(LOG_WARNING, "Invalid private redirecting-orig name valid (%u)\n",
9983  (unsigned) ie_len);
9984  break;
9985  }
9986  redirecting->priv_orig.name.valid = data[pos];
9987  break;
9988 /* Private redirecting-orig party id number */
9990  ast_free(redirecting->priv_orig.number.str);
9991  redirecting->priv_orig.number.str = ast_malloc(ie_len + 1);
9992  if (redirecting->priv_orig.number.str) {
9993  memcpy(redirecting->priv_orig.number.str, data + pos, ie_len);
9994  redirecting->priv_orig.number.str[ie_len] = 0;
9995  }
9996  break;
9998  if (ie_len != 1) {
9999  ast_log(LOG_WARNING, "Invalid private redirecting-orig numbering plan (%u)\n",
10000  (unsigned) ie_len);
10001  break;
10002  }
10003  redirecting->priv_orig.number.plan = data[pos];
10004  break;
10006  if (ie_len != 1) {
10007  ast_log(LOG_WARNING, "Invalid private redirecting-orig number presentation (%u)\n",
10008  (unsigned) ie_len);
10009  break;
10010  }
10011  redirecting->priv_orig.number.presentation = data[pos];
10012  break;
10014  if (ie_len != 1) {
10015  ast_log(LOG_WARNING, "Invalid private redirecting-orig number valid (%u)\n",
10016  (unsigned) ie_len);
10017  break;
10018  }
10019  redirecting->priv_orig.number.valid = data[pos];
10020  break;
10021 /* Private redirecting-orig party id subaddress */
10023  ast_free(redirecting->priv_orig.subaddress.str);
10024  redirecting->priv_orig.subaddress.str = ast_malloc(ie_len + 1);
10025  if (redirecting->priv_orig.subaddress.str) {
10026  memcpy(redirecting->priv_orig.subaddress.str, data + pos, ie_len);
10027  redirecting->priv_orig.subaddress.str[ie_len] = 0;
10028  }
10029  break;
10031  if (ie_len != 1) {
10032  ast_log(LOG_WARNING, "Invalid private redirecting-orig type of subaddress (%u)\n",
10033  (unsigned) ie_len);
10034  break;
10035  }
10036  redirecting->priv_orig.subaddress.type = data[pos];
10037  break;
10039  if (ie_len != 1) {
10041  "Invalid private redirecting-orig subaddress odd-even indicator (%u)\n",
10042  (unsigned) ie_len);
10043  break;
10044  }
10045  redirecting->priv_orig.subaddress.odd_even_indicator = data[pos];
10046  break;
10048  if (ie_len != 1) {
10049  ast_log(LOG_WARNING, "Invalid private redirecting-orig subaddress valid (%u)\n",
10050  (unsigned) ie_len);
10051  break;
10052  }
10053  redirecting->priv_orig.subaddress.valid = data[pos];
10054  break;
10055 /* Private redirecting-orig party id tag */
10057  ast_free(redirecting->priv_orig.tag);
10058  redirecting->priv_orig.tag = ast_malloc(ie_len + 1);
10059  if (redirecting->priv_orig.tag) {
10060  memcpy(redirecting->priv_orig.tag, data + pos, ie_len);
10061  redirecting->priv_orig.tag[ie_len] = 0;
10062  }
10063  break;
10064 /* Private redirecting-from party id name */
10066  ast_free(redirecting->priv_from.name.str);
10067  redirecting->priv_from.name.str = ast_malloc(ie_len + 1);
10068  if (redirecting->priv_from.name.str) {
10069  memcpy(redirecting->priv_from.name.str, data + pos, ie_len);
10070  redirecting->priv_from.name.str[ie_len] = 0;
10071  }
10072  break;
10074  if (ie_len != 1) {
10075  ast_log(LOG_WARNING, "Invalid private redirecting-from name char set (%u)\n",
10076  (unsigned) ie_len);
10077  break;
10078  }
10079  redirecting->priv_from.name.char_set = data[pos];
10080  break;
10082  if (ie_len != 1) {
10083  ast_log(LOG_WARNING, "Invalid private redirecting-from name presentation (%u)\n",
10084  (unsigned) ie_len);
10085  break;
10086  }
10087  redirecting->priv_from.name.presentation = data[pos];
10088  break;
10090  if (ie_len != 1) {
10091  ast_log(LOG_WARNING, "Invalid private redirecting-from name valid (%u)\n",
10092  (unsigned) ie_len);
10093  break;
10094  }
10095  redirecting->priv_from.name.valid = data[pos];
10096  break;
10097 /* Private redirecting-from party id number */
10099  ast_free(redirecting->priv_from.number.str);
10100  redirecting->priv_from.number.str = ast_malloc(ie_len + 1);
10101  if (redirecting->priv_from.number.str) {
10102  memcpy(redirecting->priv_from.number.str, data + pos, ie_len);
10103  redirecting->priv_from.number.str[ie_len] = 0;
10104  }
10105  break;
10107  if (ie_len != 1) {
10108  ast_log(LOG_WARNING, "Invalid private redirecting-from numbering plan (%u)\n",
10109  (unsigned) ie_len);
10110  break;
10111  }
10112  redirecting->priv_from.number.plan = data[pos];
10113  break;
10115  if (ie_len != 1) {
10116  ast_log(LOG_WARNING, "Invalid private redirecting-from number presentation (%u)\n",
10117  (unsigned) ie_len);
10118  break;
10119  }
10120  redirecting->priv_from.number.presentation = data[pos];
10121  break;
10123  if (ie_len != 1) {
10124  ast_log(LOG_WARNING, "Invalid private redirecting-from number valid (%u)\n",
10125  (unsigned) ie_len);
10126  break;
10127  }
10128  redirecting->priv_from.number.valid = data[pos];
10129  break;
10130 /* Private redirecting-from party id subaddress */
10132  ast_free(redirecting->priv_from.subaddress.str);
10133  redirecting->priv_from.subaddress.str = ast_malloc(ie_len + 1);
10134  if (redirecting->priv_from.subaddress.str) {
10135  memcpy(redirecting->priv_from.subaddress.str, data + pos, ie_len);
10136  redirecting->priv_from.subaddress.str[ie_len] = 0;
10137  }
10138  break;
10140  if (ie_len != 1) {
10141  ast_log(LOG_WARNING, "Invalid private redirecting-from type of subaddress (%u)\n",
10142  (unsigned) ie_len);
10143  break;
10144  }
10145  redirecting->priv_from.subaddress.type = data[pos];
10146  break;
10148  if (ie_len != 1) {
10150  "Invalid private redirecting-from subaddress odd-even indicator (%u)\n",
10151  (unsigned) ie_len);
10152  break;
10153  }
10154  redirecting->priv_from.subaddress.odd_even_indicator = data[pos];
10155  break;
10157  if (ie_len != 1) {
10158  ast_log(LOG_WARNING, "Invalid private redirecting-from subaddress valid (%u)\n",
10159  (unsigned) ie_len);
10160  break;
10161  }
10162  redirecting->priv_from.subaddress.valid = data[pos];
10163  break;
10164 /* Private redirecting-from party id tag */
10166  ast_free(redirecting->priv_from.tag);
10167  redirecting->priv_from.tag = ast_malloc(ie_len + 1);
10168  if (redirecting->priv_from.tag) {
10169  memcpy(redirecting->priv_from.tag, data + pos, ie_len);
10170  redirecting->priv_from.tag[ie_len] = 0;
10171  }
10172  break;
10173 /* Private redirecting-to party id name */
10175  ast_free(redirecting->priv_to.name.str);
10176  redirecting->priv_to.name.str = ast_malloc(ie_len + 1);
10177  if (redirecting->priv_to.name.str) {
10178  memcpy(redirecting->priv_to.name.str, data + pos, ie_len);
10179  redirecting->priv_to.name.str[ie_len] = 0;
10180  }
10181  break;
10183  if (ie_len != 1) {
10184  ast_log(LOG_WARNING, "Invalid private redirecting-to name char set (%u)\n",
10185  (unsigned) ie_len);
10186  break;
10187  }
10188  redirecting->priv_to.name.char_set = data[pos];
10189  break;
10191  if (ie_len != 1) {
10192  ast_log(LOG_WARNING, "Invalid private redirecting-to name presentation (%u)\n",
10193  (unsigned) ie_len);
10194  break;
10195  }
10196  redirecting->priv_to.name.presentation = data[pos];
10197  break;
10199  if (ie_len != 1) {
10200  ast_log(LOG_WARNING, "Invalid private redirecting-to name valid (%u)\n",
10201  (unsigned) ie_len);
10202  break;
10203  }
10204  redirecting->priv_to.name.valid = data[pos];
10205  break;
10206 /* Private redirecting-to party id number */
10208  ast_free(redirecting->priv_to.number.str);
10209  redirecting->priv_to.number.str = ast_malloc(ie_len + 1);
10210  if (redirecting->priv_to.number.str) {
10211  memcpy(redirecting->priv_to.number.str, data + pos, ie_len);
10212  redirecting->priv_to.number.str[ie_len] = 0;
10213  }
10214  break;
10216  if (ie_len != 1) {
10217  ast_log(LOG_WARNING, "Invalid private redirecting-to numbering plan (%u)\n",
10218  (unsigned) ie_len);
10219  break;
10220  }
10221  redirecting->priv_to.number.plan = data[pos];
10222  break;
10224  if (ie_len != 1) {
10225  ast_log(LOG_WARNING, "Invalid private redirecting-to number presentation (%u)\n",
10226  (unsigned) ie_len);
10227  break;
10228  }
10229  redirecting->priv_to.number.presentation = data[pos];
10230  break;
10232  if (ie_len != 1) {
10233  ast_log(LOG_WARNING, "Invalid private redirecting-to number valid (%u)\n",
10234  (unsigned) ie_len);
10235  break;
10236  }
10237  redirecting->priv_to.number.valid = data[pos];
10238  break;
10239 /* Private redirecting-to party id subaddress */
10241  ast_free(redirecting->priv_to.subaddress.str);
10242  redirecting->priv_to.subaddress.str = ast_malloc(ie_len + 1);
10243  if (redirecting->priv_to.subaddress.str) {
10244  memcpy(redirecting->priv_to.subaddress.str, data + pos, ie_len);
10245  redirecting->priv_to.subaddress.str[ie_len] = 0;
10246  }
10247  break;
10249  if (ie_len != 1) {
10250  ast_log(LOG_WARNING, "Invalid private redirecting-to type of subaddress (%u)\n",
10251  (unsigned) ie_len);
10252  break;
10253  }
10254  redirecting->priv_to.subaddress.type = data[pos];
10255  break;
10257  if (ie_len != 1) {
10259  "Invalid private redirecting-to subaddress odd-even indicator (%u)\n",
10260  (unsigned) ie_len);
10261  break;
10262  }
10263  redirecting->priv_to.subaddress.odd_even_indicator = data[pos];
10264  break;
10266  if (ie_len != 1) {
10267  ast_log(LOG_WARNING, "Invalid private redirecting-to subaddress valid (%u)\n",
10268  (unsigned) ie_len);
10269  break;
10270  }
10271  redirecting->priv_to.subaddress.valid = data[pos];
10272  break;
10273 /* Private redirecting-to party id tag */
10275  ast_free(redirecting->priv_to.tag);
10276  redirecting->priv_to.tag = ast_malloc(ie_len + 1);
10277  if (redirecting->priv_to.tag) {
10278  memcpy(redirecting->priv_to.tag, data + pos, ie_len);
10279  redirecting->priv_to.tag[ie_len] = 0;
10280  }
10281  break;
10282 /* Redirecting reason code */
10284  if (ie_len != sizeof(value)) {
10285  ast_log(LOG_WARNING, "Invalid redirecting reason (%u)\n",
10286  (unsigned) ie_len);
10287  break;
10288  }
10289  memcpy(&value, data + pos, sizeof(value));
10290  redirecting->reason.code = ntohl(value);
10291  break;
10292 /* Redirecting reason string */
10294  ast_free(redirecting->reason.str);
10295  redirecting->reason.str = ast_malloc(ie_len + 1);
10296  if (redirecting->reason.str) {
10297  memcpy(redirecting->reason.str, data + pos, ie_len);
10298  redirecting->reason.str[ie_len] = 0;
10299  }
10300  break;
10301 /* Redirecting orig-reason code */
10303  if (ie_len != sizeof(value)) {
10304  ast_log(LOG_WARNING, "Invalid redirecting original reason (%u)\n",
10305  (unsigned) ie_len);
10306  break;
10307  }
10308  memcpy(&value, data + pos, sizeof(value));
10309  redirecting->orig_reason.code = ntohl(value);
10310  break;
10311 /* Redirecting orig-reason string */
10313  ast_free(redirecting->orig_reason.str);
10314  redirecting->orig_reason.str = ast_malloc(ie_len + 1);
10315  if (redirecting->orig_reason.str) {
10316  memcpy(redirecting->orig_reason.str, data + pos, ie_len);
10317  redirecting->orig_reason.str[ie_len] = 0;
10318  }
10319  break;
10320 /* Redirecting count */
10321  case AST_REDIRECTING_COUNT:
10322  if (ie_len != sizeof(value)) {
10323  ast_log(LOG_WARNING, "Invalid redirecting count (%u)\n",
10324  (unsigned) ie_len);
10325  break;
10326  }
10327  memcpy(&value, data + pos, sizeof(value));
10328  redirecting->count = ntohl(value);
10329  break;
10330 /* Redirecting unknown element */
10331  default:
10332  ast_debug(1, "Unknown redirecting element: %u (%u)\n",
10333  (unsigned) ie_id, (unsigned) ie_len);
10334  break;
10335  }
10336  }
10337 
10338  switch (frame_version) {
10339  case 1:
10340  /*
10341  * The other end is an earlier version that we need to adjust
10342  * for compatibility.
10343  *
10344  * The earlier version did not have the orig party id or
10345  * orig_reason value.
10346  */
10347  redirecting->from.name.valid = 1;
10349  redirecting->from.number.valid = 1;
10350  if (got_from_combined_presentation) {
10351  redirecting->from.name.presentation = from_combined_presentation;
10352  redirecting->from.number.presentation = from_combined_presentation;
10353  }
10354 
10355  redirecting->to.name.valid = 1;
10357  redirecting->to.number.valid = 1;
10358  if (got_to_combined_presentation) {
10359  redirecting->to.name.presentation = to_combined_presentation;
10360  redirecting->to.number.presentation = to_combined_presentation;
10361  }
10362  break;
10363  case 2:
10364  /* The other end is at the same level as we are. */
10365  break;
10366  default:
10367  /*
10368  * The other end is newer than we are.
10369  * We need to assume that they are compatible with us.
10370  */
10371  ast_debug(1, "Redirecting frame has newer version: %u\n",
10372  (unsigned) frame_version);
10373  break;
10374  }
10375 
10376  return 0;
10377 }
10378 
10379 void ast_channel_update_redirecting(struct ast_channel *chan, const struct ast_party_redirecting *redirecting, const struct ast_set_party_redirecting *update)
10380 {
10381  unsigned char data[1024]; /* This should be large enough */
10382  size_t datalen;
10383 
10384  datalen = ast_redirecting_build_data(data, sizeof(data), redirecting, update);
10385  if (datalen == (size_t) -1) {
10386  return;
10387  }
10388 
10389  ast_indicate_data(chan, AST_CONTROL_REDIRECTING, data, datalen);
10390 }
10391 
10393 {
10394  unsigned char data[1024]; /* This should be large enough */
10395  size_t datalen;
10396 
10397  datalen = ast_redirecting_build_data(data, sizeof(data), redirecting, update);
10398  if (datalen == (size_t) -1) {
10399  return;
10400  }
10401 
10402  ast_queue_control_data(chan, AST_CONTROL_REDIRECTING, data, datalen);
10403 }
10404 
10405 /*!
10406  * Storage to determine if the current thread is running an intercept dialplan routine.
10407  */
10408 AST_THREADSTORAGE_RAW(in_intercept_routine);
10409 
10410 /*!
10411  * \internal
10412  * \brief Set the current intercept dialplan routine status mode.
10413  * \since 13.14.0
10414  *
10415  * \param in_intercept_mode New intercept mode. (Non-zero if in intercept mode)
10416  *
10417  * \return Nothing
10418  */
10419 static void channel_set_intercept_mode(int in_intercept_mode)
10420 {
10421  int status;
10422 
10423  status = ast_threadstorage_set_ptr(&in_intercept_routine,
10424  in_intercept_mode ? (void *) 1 : (void *) 0);
10425  if (status) {
10426  ast_log(LOG_ERROR, "Failed to set dialplan intercept mode\n");
10427  }
10428 }
10429 
10431 {
10432  return ast_threadstorage_get_ptr(&in_intercept_routine) ? 1 : 0;
10433 }
10434 
10435 int ast_channel_connected_line_macro(struct ast_channel *autoservice_chan, struct ast_channel *macro_chan, const void *connected_info, int is_caller, int is_frame)
10436 {
10437  static int deprecation_warning = 0;
10438  const char *macro;
10439  const char *macro_args;
10440  int retval;
10441 
10442  ast_channel_lock(macro_chan);
10443  macro = pbx_builtin_getvar_helper(macro_chan, is_caller
10444  ? "CONNECTED_LINE_CALLER_SEND_MACRO" : "CONNECTED_LINE_CALLEE_SEND_MACRO");
10445  macro = ast_strdupa(S_OR(macro, ""));
10446  macro_args = pbx_builtin_getvar_helper(macro_chan, is_caller
10447  ? "CONNECTED_LINE_CALLER_SEND_MACRO_ARGS" : "CONNECTED_LINE_CALLEE_SEND_MACRO_ARGS");
10448  macro_args = ast_strdupa(S_OR(macro_args, ""));
10449 
10450  if (ast_strlen_zero(macro)) {
10451  ast_channel_unlock(macro_chan);
10452  return -1;
10453  }
10454 
10455  if (!deprecation_warning) {
10456  deprecation_warning = 1;
10457  ast_log(LOG_WARNING, "Usage of CONNECTED_LINE_CALLE[ER]_SEND_MACRO is deprecated. Please use CONNECTED_LINE_SEND_SUB instead.\n");
10458  }
10459  if (is_frame) {
10460  const struct ast_frame *frame = connected_info;
10461 
10463  } else {
10464  const struct ast_party_connected_line *connected = connected_info;
10465 
10466  ast_party_connected_line_copy(ast_channel_connected(macro_chan), connected);
10467  }
10468  ast_channel_unlock(macro_chan);
10469 
10471  retval = ast_app_run_macro(autoservice_chan, macro_chan, macro, macro_args);
10473  if (!retval) {
10474  struct ast_party_connected_line saved_connected;
10475 
10476  ast_party_connected_line_init(&saved_connected);
10477  ast_channel_lock(macro_chan);
10478  ast_party_connected_line_copy(&saved_connected, ast_channel_connected(macro_chan));
10479  ast_channel_unlock(macro_chan);
10480  ast_channel_update_connected_line(macro_chan, &saved_connected, NULL);
10481  ast_party_connected_line_free(&saved_connected);
10482  }
10483 
10484  return retval;
10485 }
10486 
10487 int ast_channel_redirecting_macro(struct ast_channel *autoservice_chan, struct ast_channel *macro_chan, const void *redirecting_info, int is_caller, int is_frame)
10488 {
10489  static int deprecation_warning = 0;
10490  const char *macro;
10491  const char *macro_args;
10492  int retval;
10493 
10494  ast_channel_lock(macro_chan);
10495  macro = pbx_builtin_getvar_helper(macro_chan, is_caller
10496  ? "REDIRECTING_CALLER_SEND_MACRO" : "REDIRECTING_CALLEE_SEND_MACRO");
10497  macro = ast_strdupa(S_OR(macro, ""));
10498  macro_args = pbx_builtin_getvar_helper(macro_chan, is_caller
10499  ? "REDIRECTING_CALLER_SEND_MACRO_ARGS" : "REDIRECTING_CALLEE_SEND_MACRO_ARGS");
10500  macro_args = ast_strdupa(S_OR(macro_args, ""));
10501 
10502  if (ast_strlen_zero(macro)) {
10503  ast_channel_unlock(macro_chan);
10504  return -1;
10505  }
10506 
10507  if (!deprecation_warning) {
10508  deprecation_warning = 1;
10509  ast_log(LOG_WARNING, "Usage of REDIRECTING_CALLE[ER]_SEND_MACRO is deprecated. Please use REDIRECTING_SEND_SUB instead.\n");
10510  }
10511  if (is_frame) {
10512  const struct ast_frame *frame = redirecting_info;
10513 
10514  ast_redirecting_parse_data(frame->data.ptr, frame->datalen, ast_channel_redirecting(macro_chan));
10515  } else {
10516  const struct ast_party_redirecting *redirecting = redirecting_info;
10517 
10518  ast_party_redirecting_copy(ast_channel_redirecting(macro_chan), redirecting);
10519  }
10520  ast_channel_unlock(macro_chan);
10521 
10523  retval = ast_app_run_macro(autoservice_chan, macro_chan, macro, macro_args);
10525  if (!retval) {
10526  struct ast_party_redirecting saved_redirecting;
10527 
10528  ast_party_redirecting_init(&saved_redirecting);
10529  ast_channel_lock(macro_chan);
10530  ast_party_redirecting_copy(&saved_redirecting, ast_channel_redirecting(macro_chan));
10531  ast_channel_unlock(macro_chan);
10532  ast_channel_update_redirecting(macro_chan, &saved_redirecting, NULL);
10533  ast_party_redirecting_free(&saved_redirecting);
10534  }
10535 
10536  return retval;
10537 }
10538 
10539 int ast_channel_connected_line_sub(struct ast_channel *autoservice_chan, struct ast_channel *sub_chan, const void *connected_info, int is_frame)
10540 {
10541  const char *sub;
10542  const char *sub_args;
10543  int retval;
10544 
10545  ast_channel_lock(sub_chan);
10546  sub = pbx_builtin_getvar_helper(sub_chan, "CONNECTED_LINE_SEND_SUB");
10547  sub = ast_strdupa(S_OR(sub, ""));
10548  sub_args = pbx_builtin_getvar_helper(sub_chan, "CONNECTED_LINE_SEND_SUB_ARGS");
10549  sub_args = ast_strdupa(S_OR(sub_args, ""));
10550 
10551  if (ast_strlen_zero(sub)) {
10552  ast_channel_unlock(sub_chan);
10553  return -1;
10554  }
10555 
10556  if (is_frame) {
10557  const struct ast_frame *frame = connected_info;
10558 
10560  } else {
10561  const struct ast_party_connected_line *connected = connected_info;
10562 
10564  }
10565  ast_channel_unlock(sub_chan);
10566 
10568  retval = ast_app_run_sub(autoservice_chan, sub_chan, sub, sub_args, 0);
10570  if (!retval) {
10571  struct ast_party_connected_line saved_connected;
10572 
10573  ast_party_connected_line_init(&saved_connected);
10574  ast_channel_lock(sub_chan);
10575  ast_party_connected_line_copy(&saved_connected, ast_channel_connected(sub_chan));
10576  ast_channel_unlock(sub_chan);
10577  ast_channel_update_connected_line(sub_chan, &saved_connected, NULL);
10578  ast_party_connected_line_free(&saved_connected);
10579  }
10580 
10581  return retval;
10582 }
10583 
10584 int ast_channel_redirecting_sub(struct ast_channel *autoservice_chan, struct ast_channel *sub_chan, const void *redirecting_info, int is_frame)
10585 {
10586  const char *sub;
10587  const char *sub_args;
10588  int retval;
10589 
10590  ast_channel_lock(sub_chan);
10591  sub = pbx_builtin_getvar_helper(sub_chan, "REDIRECTING_SEND_SUB");
10592  sub = ast_strdupa(S_OR(sub, ""));
10593  sub_args = pbx_builtin_getvar_helper(sub_chan, "REDIRECTING_SEND_SUB_ARGS");
10594  sub_args = ast_strdupa(S_OR(sub_args, ""));
10595 
10596  if (ast_strlen_zero(sub)) {
10597  ast_channel_unlock(sub_chan);
10598  return -1;
10599  }
10600 
10601  if (is_frame) {
10602  const struct ast_frame *frame = redirecting_info;
10603 
10605  } else {
10606  const struct ast_party_redirecting *redirecting = redirecting_info;
10607 
10608  ast_party_redirecting_copy(ast_channel_redirecting(sub_chan), redirecting);
10609  }
10610  ast_channel_unlock(sub_chan);
10611 
10613  retval = ast_app_run_sub(autoservice_chan, sub_chan, sub, sub_args, 0);
10615  if (!retval) {
10616  struct ast_party_redirecting saved_redirecting;
10617 
10618  ast_party_redirecting_init(&saved_redirecting);
10619  ast_channel_lock(sub_chan);
10620  ast_party_redirecting_copy(&saved_redirecting, ast_channel_redirecting(sub_chan));
10621  ast_channel_unlock(sub_chan);
10622  ast_channel_update_redirecting(sub_chan, &saved_redirecting, NULL);
10623  ast_party_redirecting_free(&saved_redirecting);
10624  }
10625 
10626  return retval;
10627 }
10628 
10629 static void *channel_cc_params_copy(void *data)
10630 {
10631  const struct ast_cc_config_params *src = data;
10633  if (!dest) {
10634  return NULL;
10635  }
10636  ast_cc_copy_config_params(dest, src);
10637  return dest;
10638 }
10639 
10640 static void channel_cc_params_destroy(void *data)
10641 {
10642  struct ast_cc_config_params *cc_params = data;
10643  ast_cc_config_params_destroy(cc_params);
10644 }
10645 
10647  .type = "Call Completion",
10648  .duplicate = channel_cc_params_copy,
10649  .destroy = channel_cc_params_destroy,
10650 };
10651 
10653  const struct ast_cc_config_params *base_params)
10654 {
10655  struct ast_cc_config_params *cc_params;
10656  struct ast_datastore *cc_datastore;
10657 
10658  if (!(cc_params = ast_cc_config_params_init())) {
10659  return -1;
10660  }
10661 
10662  if (!(cc_datastore = ast_datastore_alloc(&cc_channel_datastore_info, NULL))) {
10663  ast_cc_config_params_destroy(cc_params);
10664  return -1;
10665  }
10666 
10667  if (base_params) {
10668  ast_cc_copy_config_params(cc_params, base_params);
10669  }
10670  cc_datastore->data = cc_params;
10671  ast_channel_datastore_add(chan, cc_datastore);
10672  return 0;
10673 }
10674 
10676 {
10677  struct ast_datastore *cc_datastore;
10678 
10679  if (!(cc_datastore = ast_channel_datastore_find(chan, &cc_channel_datastore_info, NULL))) {
10680  /* If we can't find the datastore, it almost definitely means that the channel type being
10681  * used has not had its driver modified to parse CC config parameters. The best action
10682  * to take here is to create the parameters on the spot with the defaults set.
10683  */
10684  if (ast_channel_cc_params_init(chan, NULL)) {
10685  return NULL;
10686  }
10687  if (!(cc_datastore = ast_channel_datastore_find(chan, &cc_channel_datastore_info, NULL))) {
10688  /* Should be impossible */
10689  return NULL;
10690  }
10691  }
10692 
10693  ast_assert(cc_datastore->data != NULL);
10694  return cc_datastore->data;
10695 }
10696 
10697 int ast_channel_get_device_name(struct ast_channel *chan, char *device_name, size_t name_buffer_length)
10698 {
10699  int len = name_buffer_length;
10700  char *dash;
10701  if (!ast_channel_queryoption(chan, AST_OPTION_DEVICE_NAME, device_name, &len, 0)) {
10702  return 0;
10703  }
10704 
10705  /* Dang. Do it the old-fashioned way */
10706  ast_copy_string(device_name, ast_channel_name(chan), name_buffer_length);
10707  if ((dash = strrchr(device_name, '-'))) {
10708  *dash = '\0';
10709  }
10710 
10711  return 0;
10712 }
10713 
10714 int ast_channel_get_cc_agent_type(struct ast_channel *chan, char *agent_type, size_t size)
10715 {
10716  int len = size;
10717  char *slash;
10718 
10719  if (!ast_channel_queryoption(chan, AST_OPTION_CC_AGENT_TYPE, agent_type, &len, 0)) {
10720  return 0;
10721  }
10722 
10723  ast_copy_string(agent_type, ast_channel_name(chan), size);
10724  if ((slash = strchr(agent_type, '/'))) {
10725  *slash = '\0';
10726  }
10727  return 0;
10728 }
10729 
10731 {
10732  ao2_unlink(channels, chan);
10733 }
10734 
10735 struct ast_bridge *ast_channel_get_bridge(const struct ast_channel *chan)
10736 {
10737  struct ast_bridge *bridge;
10738 
10739  bridge = ast_channel_internal_bridge(chan);
10740  if (bridge) {
10741  ao2_ref(bridge, +1);
10742  }
10743  return bridge;
10744 }
10745 
10746 int ast_channel_is_bridged(const struct ast_channel *chan)
10747 {
10748  return ast_channel_internal_bridge(chan) != NULL;
10749 }
10750 
10752 {
10753  int hangup_flags = ast_channel_softhangup_internal_flag(chan);
10754  int hangup_test = hangup_flags & AST_SOFTHANGUP_ASYNCGOTO;
10755  int unbridge = ast_channel_unbridged(chan);
10756 
10757  /* This function should only return true if either the unbridged flag or
10758  * the ASYNCGOTO soft hangup flag is set and when no other soft hangup
10759  * flags are set. Any other soft hangup flags being set should make it
10760  * return false.
10761  */
10762  return ((hangup_test || unbridge) && (hangup_test == hangup_flags));
10763 }
10764 
10766 {
10767  struct ast_channel *peer;
10768  struct ast_bridge *bridge;
10769 
10770  /* Get the bridge the channel is in. */
10771  ast_channel_lock(chan);
10772  bridge = ast_channel_get_bridge(chan);
10773  ast_channel_unlock(chan);
10774  if (!bridge) {
10775  return NULL;
10776  }
10777 
10778  peer = ast_bridge_peer(bridge, chan);
10779  ao2_ref(bridge, -1);
10780  return peer;
10781 }
10782 
10784 {
10785  struct ast_bridge_channel *bridge_channel;
10786 
10787  bridge_channel = ast_channel_internal_bridge_channel(chan);
10788  if (bridge_channel) {
10789  ao2_ref(bridge_channel, +1);
10790  }
10791  return bridge_channel;
10792 }
10793 
10795 {
10796  struct ast_channel *yanked_chan;
10797  struct {
10798  char *accountcode;
10799  char *exten;
10800  char *context;
10801  char *name;
10802  int amaflags;
10803  int priority;
10804  struct ast_format *readformat;
10805  struct ast_format *writeformat;
10806  } my_vars = { 0, };
10807 
10808  ast_channel_lock(yankee);
10809  my_vars.accountcode = ast_strdupa(ast_channel_accountcode(yankee));
10810  my_vars.exten = ast_strdupa(ast_channel_exten(yankee));
10811  my_vars.context = ast_strdupa(ast_channel_context(yankee));
10812  my_vars.name = ast_strdupa(ast_channel_name(yankee));
10813  my_vars.amaflags = ast_channel_amaflags(yankee);
10814  my_vars.priority = ast_channel_priority(yankee);
10815  /* The priority as returned by ast_channel_yank is where the channel
10816  * should go if the dialplan is executed on it. If the channel is
10817  * already executing dialplan then the priority currently set is
10818  * where it is currently. We increment it so it becomes where it should
10819  * execute.
10820  */
10822  my_vars.priority++;
10823  }
10824  my_vars.writeformat = ao2_bump(ast_channel_writeformat(yankee));
10825  my_vars.readformat = ao2_bump(ast_channel_readformat(yankee));
10826  ast_channel_unlock(yankee);
10827 
10828  /* Do not hold any channel locks while calling channel_alloc() since the function
10829  * locks the channel container when linking the new channel in. */
10830  if (!(yanked_chan = ast_channel_alloc(0, AST_STATE_DOWN, 0, 0, my_vars.accountcode,
10831  my_vars.exten, my_vars.context, NULL, yankee, my_vars.amaflags,
10832  "Surrogate/%s", my_vars.name))) {
10833  ao2_cleanup(my_vars.writeformat);
10834  ao2_cleanup(my_vars.readformat);
10835  return NULL;
10836  }
10837 
10838  /* Make formats okay */
10839  ast_channel_set_readformat(yanked_chan, my_vars.readformat);
10840  ast_channel_set_writeformat(yanked_chan, my_vars.writeformat);
10841  ao2_cleanup(my_vars.readformat);
10842  ao2_cleanup(my_vars.writeformat);
10843  ast_channel_priority_set(yanked_chan, my_vars.priority);
10844 
10845  ast_channel_unlock(yanked_chan);
10846 
10847  if (ast_channel_move(yanked_chan, yankee)) {
10848  ast_hangup(yanked_chan);
10849  return NULL;
10850  }
10851 
10852  return yanked_chan;
10853 }
10854 
10855 /*!
10856  * Mutex that prevents multiple ast_channel_move() operations
10857  * from occurring simultaneously. This is necessary since the
10858  * involved channels have to be locked and unlocked throughout
10859  * the move operation.
10860  *
10861  * The most important data being protected are the masq and masqr
10862  * data on channels. We don't want them getting criss-crossed due
10863  * to multiple moves mucking with them.
10864  */
10866 
10867 int ast_channel_move(struct ast_channel *dest, struct ast_channel *source)
10868 {
10869  RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
10871 
10872  if (dest == source) {
10873  ast_log(LOG_WARNING, "Can't move channel '%s' into itself!\n",
10874  ast_channel_name(dest));
10875  return -1;
10876  }
10877 
10878  ast_channel_lock_both(dest, source);
10879 
10882  /* Zombies! Run! */
10884  "Can't move channel. One or both is dead (%s <-- %s)\n",
10885  ast_channel_name(dest), ast_channel_name(source));
10886  ast_channel_unlock(source);
10887  ast_channel_unlock(dest);
10888  return -1;
10889  }
10890 
10891  ast_channel_masq_set(dest, source);
10892  ast_channel_masqr_set(source, dest);
10893 
10894  blob = ast_json_pack("{s: s}",
10895  "newchanneluniqueid", ast_channel_uniqueid(dest));
10897 
10898  ast_channel_unlock(dest);
10899  ast_channel_unlock(source);
10900 
10901  channel_do_masquerade(dest, source);
10902  return 0;
10903 }
10904 
10905 static void suppress_datastore_destroy_cb(void *data)
10906 {
10907  ao2_cleanup(data);
10908 }
10909 
10911  .type = "suppressvoice",
10913 };
10914 
10915 static void suppress_framehook_destroy_cb(void *data)
10916 {
10917  ao2_cleanup(data);
10918 }
10919 
10921  enum ast_frame_type frametype;
10922  unsigned int direction;
10924 };
10925 
10926 static void suppress_framehook_fixup_cb(void *data, int framehook_id, struct ast_channel *old_chan, struct ast_channel *new_chan)
10927 {
10928  struct suppress_data *suppress = data;
10929 
10930  suppress->framehook_id = framehook_id;
10931 }
10932 
10933 static struct ast_frame *suppress_framehook_event_cb(struct ast_channel *chan, struct ast_frame *frame, enum ast_framehook_event event, void *data)
10934 {
10935  struct suppress_data *suppress = data;
10936  int suppress_frame = 0;
10937 
10938  if (!frame) {
10939  return NULL;
10940  }
10941 
10942  if (frame->frametype != suppress->frametype) {
10943  return frame;
10944  }
10945 
10946  if (event == AST_FRAMEHOOK_EVENT_READ && (suppress->direction & AST_MUTE_DIRECTION_READ)) {
10947  suppress_frame = 1;
10948  } else if (event == AST_FRAMEHOOK_EVENT_WRITE && (suppress->direction & AST_MUTE_DIRECTION_WRITE)) {
10949  suppress_frame = 1;
10950  }
10951 
10952  if (suppress_frame) {
10953  switch (frame->frametype) {
10954  case AST_FRAME_VOICE:
10955  if (event == AST_FRAMEHOOK_EVENT_READ) {
10956  ast_frfree(frame);
10957  }
10958  frame = &ast_null_frame;
10959  break;
10960  default:
10961  break;
10962  }
10963  }
10964 
10965  return frame;
10966 }
10967 
10969 {
10970  switch (frametype) {
10971  case AST_FRAME_VOICE:
10972  return &suppress_datastore_voice;
10973  default:
10974  return NULL;
10975  }
10976 }
10977 
10978 int ast_channel_suppress(struct ast_channel *chan, unsigned int direction, enum ast_frame_type frametype)
10979 {
10980  struct suppress_data *suppress;
10981  const struct ast_datastore_info *datastore_info = NULL;
10982  struct ast_datastore *datastore = NULL;
10983  struct ast_framehook_interface interface = {
10985  .event_cb = suppress_framehook_event_cb,
10986  .destroy_cb = suppress_framehook_destroy_cb,
10987  .chan_fixup_cb = suppress_framehook_fixup_cb,
10988  };
10989  int framehook_id;
10990 
10991  if (!(datastore_info = suppress_get_datastore_information(frametype))) {
10992  ast_log(LOG_WARNING, "Attempted to suppress an unsupported frame type (%u).\n", frametype);
10993  return -1;
10994  }
10995 
10996  if ((datastore = ast_channel_datastore_find(chan, datastore_info, NULL))) {
10997  suppress = datastore->data;
10998  suppress->direction |= direction;
10999  return 0;
11000  }
11001 
11002  if (!(suppress = ao2_alloc(sizeof(*suppress), NULL))) {
11003  ast_log(LOG_WARNING, "Failed to allocate data while attempting to suppress a stream.\n");
11004  return -1;
11005  }
11006 
11007  suppress->frametype = frametype;
11008  suppress->direction |= direction;
11009 
11010  interface.data = suppress;
11011 
11012  framehook_id = ast_framehook_attach(chan, &interface);
11013  if (framehook_id < 0) {
11014  /* Hook attach failed. Get rid of the evidence. */
11015  ast_log(LOG_WARNING, "Failed to attach framehook while attempting to suppress a stream.\n");
11016  ao2_ref(suppress, -1);
11017  return -1;
11018  }
11019 
11020  /* One ref for the framehook */
11021  ao2_ref(suppress, +1);
11022 
11023  suppress->framehook_id = framehook_id;
11024 
11025  if (!(datastore = ast_datastore_alloc(datastore_info, NULL))) {
11026  ast_log(LOG_WARNING, "Failed to allocate datastore while attempting to suppress a stream.\n");
11027  ast_framehook_detach(chan, framehook_id);
11028  ao2_ref(suppress, -1);
11029  return -1;
11030  }
11031 
11032  /* the ref provided by the allocation is taken by the datastore */
11033  datastore->data = suppress;
11034 
11035  ast_channel_datastore_add(chan, datastore);
11036 
11037  return 0;
11038 }
11039 
11040 int ast_channel_unsuppress(struct ast_channel *chan, unsigned int direction, enum ast_frame_type frametype)
11041 {
11042  const struct ast_datastore_info *datastore_info = NULL;
11043  struct ast_datastore *datastore = NULL;
11044  struct suppress_data *suppress;
11045 
11046  if (!(datastore_info = suppress_get_datastore_information(frametype))) {
11047  ast_log(LOG_WARNING, "Attempted to unsuppress an unsupported frame type (%u).\n", frametype);
11048  return -1;
11049  }
11050 
11051  if (!(datastore = ast_channel_datastore_find(chan, datastore_info, NULL))) {
11052  /* Nothing to do! */
11053  return 0;
11054  }
11055 
11056  suppress = datastore->data;
11057 
11058  suppress->direction &= ~(direction);
11059 
11060  if (suppress->direction == 0) {
11061  /* Nothing left to suppress. Bye! */
11062  ast_framehook_detach(chan, suppress->framehook_id);
11063  ast_channel_datastore_remove(chan, datastore);
11064  ast_datastore_free(datastore);
11065  }
11066 
11067  return 0;
11068 }
11069 
11070 void ast_channel_end_dtmf(struct ast_channel *chan, char digit, struct timeval start, const char *why)
11071 {
11072  int dead;
11073  long duration;
11074 
11075  ast_channel_lock(chan);
11079  ast_channel_unlock(chan);
11080  if (dead) {
11081  /* Channel is a zombie or a real hangup. */
11082  return;
11083  }
11084 
11085  duration = ast_tvdiff_ms(ast_tvnow(), start);
11086  if (duration < option_dtmfminduration) {
11087  duration = option_dtmfminduration;
11088  }
11089  ast_senddigit_end(chan, digit, duration);
11090  ast_log(LOG_DTMF, "DTMF end '%c' simulated on %s due to %s, duration %ld ms\n",
11091  digit, ast_channel_name(chan), why, duration);
11092 }
11093 
11094 static void features_destroy(void *obj)
11095 {
11097 }
11098 
11100  .type = "bridge-features",
11101  .destroy = features_destroy,
11102 };
11103 
11105 {
11106  struct ast_datastore *datastore;
11107 
11108  datastore = ast_channel_datastore_find(chan, &bridge_features_info, NULL);
11109  if (!datastore) {
11110  return NULL;
11111  }
11112  return datastore->data;
11113 }
11114 
11115 static int channel_feature_hooks_set_full(struct ast_channel *chan, struct ast_bridge_features *features, int replace)
11116 {
11117  struct ast_datastore *datastore;
11118  struct ast_bridge_features *ds_features;
11119 
11120  datastore = ast_channel_datastore_find(chan, &bridge_features_info, NULL);
11121  if (datastore) {
11122  ds_features = datastore->data;
11123  if (replace) {
11124  ast_bridge_features_cleanup(ds_features);
11125  ast_bridge_features_init(ds_features);
11126  }
11127  if (features) {
11128  ast_bridge_features_merge(ds_features, features);
11129  }
11130  return 0;
11131  }
11132 
11133  datastore = ast_datastore_alloc(&bridge_features_info, NULL);
11134  if (!datastore) {
11135  return -1;
11136  }
11137 
11138  ds_features = ast_bridge_features_new();
11139  if (!ds_features) {
11140  ast_datastore_free(datastore);
11141  return -1;
11142  }
11143 
11144  if (features) {
11145  ast_bridge_features_merge(ds_features, features);
11146  }
11147  datastore->data = ds_features;
11148  ast_channel_datastore_add(chan, datastore);
11149  return 0;
11150 }
11151 
11153 {
11154  return channel_feature_hooks_set_full(chan, features, 0);
11155 }
11156 
11158 {
11159  return channel_feature_hooks_set_full(chan, features, 1);
11160 }
11161 
11163 {
11164  return ast_channel_internal_errno();
11165 }
11166 
11168  struct ast_stream_topology *topology, void *change_source)
11169 {
11170  int res;
11171 
11172  ast_assert(chan != NULL);
11173  ast_assert(topology != NULL);
11174 
11175  ast_channel_lock(chan);
11176  if (!ast_channel_is_multistream(chan) || !ast_channel_tech(chan)->indicate) {
11177  ast_channel_unlock(chan);
11178  return -1;
11179  }
11180 
11182  ast_debug(2, "%s: Topologies already match. Current: %s Requested: %s\n",
11183  ast_channel_name(chan),
11185  ast_str_tmp(256, ast_stream_topology_to_str(topology, &STR_TMP)));
11186  ast_channel_unlock(chan);
11187  return 0;
11188  }
11189 
11191 
11192  res = ast_channel_tech(chan)->indicate(chan, AST_CONTROL_STREAM_TOPOLOGY_REQUEST_CHANGE, topology, sizeof(topology));
11193  ast_channel_unlock(chan);
11194  return res;
11195 }
11196 
11198 {
11199  ast_assert(chan != NULL);
11200  ast_assert(topology != NULL);
11201 
11202  if (!ast_channel_is_multistream(chan) || !ast_channel_tech(chan)->indicate) {
11203  return -1;
11204  }
11205 
11206  return ast_channel_tech(chan)->indicate(chan, AST_CONTROL_STREAM_TOPOLOGY_CHANGED, topology, sizeof(topology));
11207 }
11208 
11210 {
11211  int res;
11212  struct ast_frame f = { AST_FRAME_CONTROL, .subclass.integer = AST_CONTROL_STREAM_TOPOLOGY_CHANGED };
11213 
11214  ast_assert(chan != NULL);
11215 
11216  if (!ast_channel_is_multistream(chan)) {
11217  return -1;
11218  }
11219 
11220  ast_channel_lock(chan);
11222  res = ast_queue_frame(chan, &f);
11223  ast_channel_unlock(chan);
11224 
11225  return res;
11226 }
11227 
11228 void ast_channel_set_flag(struct ast_channel *chan, unsigned int flag)
11229 {
11230  ast_channel_lock(chan);
11231  ast_set_flag(ast_channel_flags(chan), flag);
11232  ast_channel_unlock(chan);
11233 }
11234 
11235 void ast_channel_clear_flag(struct ast_channel *chan, unsigned int flag)
11236 {
11237  ast_channel_lock(chan);
11238  ast_clear_flag(ast_channel_flags(chan), flag);
11239  ast_channel_unlock(chan);
11240 }
int ast_say_digit_str(struct ast_channel *chan, const char *str, const char *ints, const char *lang)
says digits of a string
Definition: channel.c:8355
int valid
valid/present ie
Definition: channel.c:8427
struct stasis_message_type * ast_channel_hold_type(void)
Message type for when a channel is placed on hold.
struct ast_party_id ast_party_id_merge(struct ast_party_id *base, struct ast_party_id *overlay)
Merge a given party id into another given party id.
Definition: channel.c:1902
struct ast_party_caller * ast_channel_caller(struct ast_channel *chan)
const char * type
Definition: datastore.h:32
int(*const answer_with_stream_topology)(struct ast_channel *chan, struct ast_stream_topology *topology)
Answer the channel with topology.
Definition: channel.h:721
#define AST_CAUSE_PROTOCOL_ERROR
Definition: causes.h:144
void ast_channel_dtmf_digit_to_emulate_set(struct ast_channel *chan, char value)
static char musicclass[MAX_MUSICCLASS]
Definition: chan_mgcp.c:162
struct ast_variable * next
#define AST_THREADSTORAGE(name)
Define a thread storage variable.
Definition: threadstorage.h:84
int ast_say_number(struct ast_channel *chan, int num, const char *ints, const char *language, const char *options)
says a number
Definition: channel.c:8337
unsigned long long ast_group_t
Definition: channel.h:214
void ast_party_name_set(struct ast_party_name *dest, const struct ast_party_name *src)
Set the source party name information into the destination party name.
Definition: channel.c:1621
void ast_channel_internal_alertpipe_clear(struct ast_channel *chan)
static const char type[]
Definition: chan_ooh323.c:109
int ast_waitfor_n_fd(int *fds, int n, int *ms, int *exception)
Wait for x amount of time on a file descriptor to have input.
Definition: channel.c:2989
void ast_channel_fdno_set(struct ast_channel *chan, int value)
Information needed to identify an endpoint in a call.
Definition: channel.h:339
enum sip_cc_notify_state state
Definition: chan_sip.c:959
const char * ast_channel_blockproc(const struct ast_channel *chan)
Tone Indication Support.
#define AST_LIST_INSERT_LIST_AFTER(head, list, elm, field)
Inserts a whole list after a specific entry in a list.
Definition: linkedlists.h:811
void ast_channel_unlink(struct ast_channel *chan)
Remove a channel from the global channels container.
Definition: channel.c:10730
int char_set
Character set ie.
Definition: channel.c:8423
static char accountcode[AST_MAX_ACCOUNT_CODE]
Definition: chan_iax2.c:428
void ast_channel_timingfunc_set(struct ast_channel *chan, ast_timing_func_t value)
int presentation
Q.931 encoded presentation-indicator encoded field.
Definition: channel.h:278
int ast_queue_control(struct ast_channel *chan, enum ast_control_frame_type control)
Queue a control frame.
Definition: channel.c:1231
int ast_channel_fd_isset(const struct ast_channel *chan, int which)
int ast_check_hangup_locked(struct ast_channel *chan)
Definition: channel.c:459
#define AST_CAUSE_MANDATORY_IE_MISSING
Definition: causes.h:136
char digit
void ast_party_dialed_set_init(struct ast_party_dialed *init, const struct ast_party_dialed *guide)
Initialize the given dialed structure using the given guide for a set update operation.
Definition: channel.c:1950
#define ast_channel_lock(chan)
Definition: channel.h:2945
#define AST_CAUSE_BEARERCAPABILITY_NOTAUTH
Definition: causes.h:128
void ast_channel_clear_softhangup(struct ast_channel *chan, int flag)
Clear a set of softhangup flags from a channel.
Definition: channel.c:2437
static int indicate(void *data)
Definition: chan_pjsip.c:1333
static char exten[AST_MAX_EXTENSION]
Definition: chan_alsa.c:118
Main Channel structure associated with a channel.
void(* set_trans)(struct ast_channel *chan, struct ast_trans_pvt *value)
Definition: channel.c:5650
static void features_destroy(void *obj)
Definition: channel.c:11094
struct ast_party_dialed::@246 number
Dialed/Called number.
SAY_EXTERN int(* ast_say_number_full)(struct ast_channel *chan, int num, const char *ints, const char *lang, const char *options, int audiofd, int ctrlfd) SAY_INIT(ast_say_number_full)
Same as ast_say_number() with audiofd for received audio and returns 1 on ctrlfd being readable...
Definition: say.h:86
void ast_bridge_features_cleanup(struct ast_bridge_features *features)
Clean up the contents of a bridge features structure.
Definition: bridge.c:3720
Music on hold handling.
const char * cid_num
Definition: channel.h:1110
int ast_set_read_format(struct ast_channel *chan, struct ast_format *format)
Sets read format on channel chan.
Definition: channel.c:5849
#define AST_CLI_DEFINE(fn, txt,...)
Definition: cli.h:197
void ast_set_party_id_all(struct ast_set_party_id *update_id)
Set the update marker to update all information of a corresponding party id.
Definition: channel.c:1750
void *(* duplicate)(void *data)
Definition: datastore.h:33
int plan
Q.931 Type-Of-Number and Numbering-Plan encoded fields.
Definition: channel.h:389
#define ast_frdup(fr)
Copies a frame.
static const struct set_format_access set_format_access_write
Definition: channel.c:5669
unsigned int cos
Definition: chan_iax2.c:352
char * str
Subscriber phone number (Malloced)
Definition: channel.h:292
int ast_channel_get_up_time(struct ast_channel *chan)
Obtain how long it has been since the channel was answered.
Definition: channel.c:2854
void ast_party_caller_init(struct ast_party_caller *init)
Initialize the given caller structure.
Definition: channel.c:1978
int(*const transfer)(struct ast_channel *chan, const char *newdest)
Blind transfer other side (see app_transfer.c and ast_transfer()
Definition: channel.h:784
General Asterisk channel transcoding definitions.
int ast_channel_hold_state(const struct ast_channel *chan)
struct ast_party_connected_line connected
Channel Connected Line ID information.
int(*const setoption)(struct ast_channel *chan, int option, void *data, int datalen)
Set a given option. Called with chan locked.
Definition: channel.h:778
enum ast_media_type ast_format_get_type(const struct ast_format *format)
Get the media type of a format.
Definition: format.c:354
#define AST_CAUSE_SUBSCRIBER_ABSENT
Definition: causes.h:109
const char *const type
Definition: channel.h:630
#define AST_CAUSE_OUTGOING_CALL_BARRED
Definition: causes.h:126
void ast_party_caller_set(struct ast_party_caller *dest, const struct ast_party_caller *src, const struct ast_set_party_caller *update)
Set the caller information based on another caller source.
Definition: channel.c:2007
#define AST_CAUSE_INCOMPATIBLE_DESTINATION
Definition: causes.h:134
Asterisk locking-related definitions:
unsigned int ast_timer_get_max_rate(const struct ast_timer *handle)
Get maximum rate supported for a timer.
Definition: timing.c:191
static void adjust_frame_for_plc(struct ast_channel *chan, struct ast_frame *frame, struct ast_datastore *datastore)
Definition: channel.c:5098
struct stasis_message_type * ast_channel_dtmf_end_type(void)
Message type for when DTMF ends on a channel.
Asterisk main include file. File version handling, generic pbx functions.
int ast_shutting_down(void)
Definition: asterisk.c:1834
#define AST_LIST_FIRST(head)
Returns the first entry contained in a list.
Definition: linkedlists.h:420
struct ast_datastore * ast_channel_datastore_find(struct ast_channel *chan, const struct ast_datastore_info *info, const char *uid)
Find a datastore on a channel.
Definition: channel.c:2404
void ast_channel_visible_indication_set(struct ast_channel *chan, int value)
int ao2_container_count(struct ao2_container *c)
Returns the number of elements in a container.
SAY_EXTERN int(* ast_say_enumeration_full)(struct ast_channel *chan, int num, const char *ints, const char *lang, const char *options, int audiofd, int ctrlfd) SAY_INIT(ast_say_enumeration_full)
Same as ast_say_enumeration() with audiofd for received audio and returns 1 on ctrlfd being readable...
Definition: say.h:106
struct ast_party_id priv_to
Call is redirecting to a new party (Sent to the caller) - private representation. ...
Definition: channel.h:540
#define ARRAY_LEN(a)
Definition: isdn_lib.c:42
char * str
Subscriber phone number (Malloced)
Definition: channel.h:387
char chan_name[AST_CHANNEL_NAME]
void ast_channel_timer_set(struct ast_channel *chan, struct ast_timer *value)
struct ast_format *(* get_rawformat)(struct ast_channel *chan)
Definition: channel.c:5653
struct stasis_message_type * ast_channel_unhold_type(void)
Message type for when a channel is removed from hold.
Channels have this property if they implement send_text_data.
Definition: channel.h:976
struct ast_party_caller caller
Channel Caller ID information.
static ast_mutex_t channel_move_lock
Definition: channel.c:10865
struct ast_filestream * ast_channel_vstream(const struct ast_channel *chan)
void * ast_channel_generatordata(const struct ast_channel *chan)
struct ast_set_party_id ani
Definition: channel.h:445
void ast_channel_whentohangup_set(struct ast_channel *chan, struct timeval *value)
int ast_channel_is_multistream(struct ast_channel *chan)
Determine if a channel is multi-stream capable.
void * ast_threadstorage_get(struct ast_threadstorage *ts, size_t init_size)
Retrieve thread storage.
const char * direction
Definition: channel.c:5648
void ast_party_connected_line_set_init(struct ast_party_connected_line *init, const struct ast_party_connected_line *guide)
Initialize the given connected line structure using the given guide for a set update operation...
Definition: channel.c:2045
struct ast_json * ast_json_pack(char const *format,...)
Helper for creating complex JSON values.
Definition: json.c:591
static int amaflags
Definition: chan_iax2.c:431
int ast_channel_internal_alert_readfd(struct ast_channel *chan)
static int __ast_queue_frame(struct ast_channel *chan, struct ast_frame *fin, int head, struct ast_frame *after)
Definition: channel.c:1015
static int attribute_const is_visible_indication(enum ast_control_frame_type condition)
Definition: channel.c:4327
void ast_channel_insmpl_set(struct ast_channel *chan, unsigned long value)
static struct ast_tone_zone * ast_tone_zone_unref(struct ast_tone_zone *tz)
Release a reference to an ast_tone_zone.
Definition: indications.h:205
struct varshead * ast_channel_get_ari_vars(struct ast_channel *chan)
Gets the variables for a given channel, as specified by ast_channel_set_ari_vars().
Definition: channel.c:8090
#define AST_RWLIST_HEAD_STATIC(name, type)
Defines a structure to be used to hold a read/write list of specified type, statically initialized...
Definition: linkedlists.h:332
static int channel_feature_hooks_set_full(struct ast_channel *chan, struct ast_bridge_features *features, int replace)
Definition: channel.c:11115
int(*const write_video)(struct ast_channel *chan, struct ast_frame *frame)
Write a frame, in standard format.
Definition: channel.h:787
struct ast_cdr * ast_channel_cdr(const struct ast_channel *chan)
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)
Set the connected line information in the Asterisk channel.
Definition: channel.c:8404
unsigned int option_dtmfminduration
Definition: options.c:83
int ast_channel_get_duration(struct ast_channel *chan)
Obtain how long the channel since the channel was created.
Definition: channel.c:2839
void ast_channel_snapshot_invalidate_segment(struct ast_channel *chan, enum ast_channel_snapshot_segment_invalidation segment)
Invalidate a channel snapshot segment from being reused.
int str
Subscriber phone number ie.
Definition: channel.c:8495
struct ast_stream_topology * ast_channel_get_stream_topology(const struct ast_channel *chan)
Retrieve the topology of streams on a channel.
#define DEBUGCHAN_FLAG
Definition: channel.h:858
static void ast_dummy_channel_destructor(void *obj)
Free a dummy channel structure.
Definition: channel.c:2338
static const struct ast_datastore_info cc_channel_datastore_info
Definition: channel.c:10646
struct ast_party_subaddress_ies subaddress
Subscriber subaddress ies.
Definition: channel.c:8648
const char * tech
Definition: app_dial.c:802
int presentation
Q.931 presentation-indicator and screening-indicator encoded fields.
Definition: channel.h:296
ast_channel_requestor_relationship
Definition: channel.h:1477
enum ast_msg_data_attribute_type type
Definition: message.h:463
void ast_cc_config_params_destroy(struct ast_cc_config_params *params)
Free memory from CCSS configuration params.
Definition: ccss.c:693
void ast_channel_queue_redirecting_update(struct ast_channel *chan, const struct ast_party_redirecting *redirecting, const struct ast_set_party_redirecting *update)
Queue a redirecting update frame on a channel.
Definition: channel.c:10392
CallerID (and other GR30) management and generation Includes code and algorithms from the Zapata libr...
void ast_channel_set_writeformat(struct ast_channel *chan, struct ast_format *format)
Definition: ast_expr2.c:325
#define AST_CAUSE_INCOMING_CALL_BARRED
Definition: causes.h:127
#define STATE2STR_BUFSIZE
Definition: channel.c:98
void ast_channel_set_oldwriteformat(struct ast_channel *chan, struct ast_format *format)
struct ast_channel * parent_channel
Definition: channel.h:1114
int(* stop)(struct ast_channel *chan, int need_lock)
Definition: channel.h:4250
int ast_softhangup(struct ast_channel *chan, int cause)
Softly hangup a channel, lock.
Definition: channel.c:2476
void ast_party_dialed_copy(struct ast_party_dialed *dest, const struct ast_party_dialed *src)
Copy the source dialed party information to the destination dialed party.
Definition: channel.c:1936
struct ast_party_id id
Connected party ID.
Definition: channel.h:459
const char * ast_channel_reason2str(int reason)
return an english explanation of the code returned thru __ast_request_and_dial&#39;s &#39;outstate&#39; argument ...
Definition: channel.c:5913
void *(* alloc)(struct ast_channel *chan, void *params)
Definition: channel.h:227
short int16_t
Definition: db.h:59
static int set_format(struct ast_channel *chan, struct ast_format_cap *cap_set, const int direction, int interleaved_stereo)
Definition: channel.c:5680
static int calc_monitor_jump(int samples, int sample_rate, int seek_rate)
calculates the number of samples to jump forward with in a monitor stream.
Definition: channel.c:3516
#define AST_DIGIT_ANY
Definition: file.h:48
const char * name
Definition: channel.c:132
void(* chan_breakdown)(void *data, struct ast_channel *old_chan, struct ast_channel *new_chan)
Fix up channel references on the channel being masqueraded into.
Definition: datastore.h:64
Structure that contains features information.
struct ast_format_cap * ast_stream_topology_get_formats(struct ast_stream_topology *topology)
Create a format capabilities structure representing the topology.
Definition: stream.c:930
int ast_cli_unregister_multiple(struct ast_cli_entry *e, int len)
Unregister multiple commands.
Definition: clicompat.c:30
int ast_readstring(struct ast_channel *c, char *s, int len, int timeout, int ftimeout, char *enders)
Reads multiple digits.
Definition: channel.c:6655
void ast_log_callid(int level, const char *file, int line, const char *function, ast_callid callid, const char *fmt,...)
Used for sending a log message with a known call_id This is a modified logger function which is funct...
Definition: logger.c:2149
void * ast_channel_tech_pvt(const struct ast_channel *chan)
#define AST_CAUSE_NORMAL_TEMPORARY_FAILURE
Definition: causes.h:121
int ast_queue_hold(struct ast_channel *chan, const char *musicclass)
Queue a hold frame.
Definition: channel.c:1191
static void suppress_framehook_destroy_cb(void *data)
Definition: channel.c:10915
void ast_party_subaddress_set(struct ast_party_subaddress *dest, const struct ast_party_subaddress *src)
Set the source party subaddress information into the destination party subaddress.
Definition: channel.c:1727
int(*const write)(struct ast_channel *chan, struct ast_frame *frame)
Write a frame, in standard format (see frame.h)
Definition: channel.h:751
#define ast_channel_unref(c)
Decrease channel reference count.
Definition: channel.h:2981
void ast_audiohook_move_all(struct ast_channel *old_chan, struct ast_channel *new_chan)
Move all audiohooks from one channel to another.
Definition: audiohook.c:700
#define AST_CAUSE_NOSUCHDRIVER
Definition: causes.h:155
#define ast_test_flag(p, flag)
Definition: utils.h:63
static void send_dtmf_begin_event(struct ast_channel *chan, enum DtmfDirection direction, const char digit)
Definition: channel.c:3378
char ast_channel_sending_dtmf_digit(const struct ast_channel *chan)
#define AST_CAUSE_SWITCH_CONGESTION
Definition: causes.h:122
int ast_softhangup_nolock(struct ast_channel *chan, int cause)
Softly hangup a channel, don&#39;t lock.
Definition: channel.c:2463
unsigned int ast_channel_fout(const struct ast_channel *chan)
struct ast_party_id priv_orig
Who originally redirected the call (Sent to the party the call is redirected toward) - private repres...
Definition: channel.h:534
#define AST_OPTION_DEVICE_NAME
void ast_audiohook_detach_list(struct ast_audiohook_list *audiohook_list)
Detach audiohooks from list and destroy said list.
Definition: audiohook.c:594
Device state management.
static void update(int code_size, int y, int wi, int fi, int dq, int sr, int dqsez, struct g726_state *state_ptr)
Definition: codec_g726.c:367
Support for translation of data formats. translate.c.
static void free_translation(struct ast_channel *clonechan)
Definition: channel.c:2492
int ast_namedgroups_intersect(struct ast_namedgroups *a, struct ast_namedgroups *b)
Return TRUE if group a and b contain at least one common groupname.
Definition: channel.c:8192
void ast_party_dialed_set(struct ast_party_dialed *dest, const struct ast_party_dialed *src)
Set the dialed information based on another dialed source.
Definition: channel.c:1958
int ast_app_run_macro(struct ast_channel *autoservice_chan, struct ast_channel *macro_chan, const char *macro_name, const char *macro_args)
Run a macro on a channel, placing an optional second channel into autoservice.
Definition: main/app.c:320
void ast_party_number_set_init(struct ast_party_number *init, const struct ast_party_number *guide)
Initialize the given party number structure using the given guide for a set update operation...
Definition: channel.c:1666
int ast_party_id_presentation(const struct ast_party_id *id)
Determine the overall presentation value for the given party.
Definition: channel.c:1821
const char * ast_var_value(const struct ast_var_t *var)
Definition: chanvars.c:80
void ast_json_unref(struct ast_json *value)
Decrease refcount on value. If refcount reaches zero, value is freed.
Definition: json.c:73
struct ast_party_name name
Subscriber name.
Definition: channel.h:341
#define OBJ_KEY
Definition: astobj2.h:1155
struct ast_party_id from
Who is redirecting the call (Sent to the party the call is redirected toward)
Definition: channel.h:528
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)
Request a channel of a given type, with data as optional information used by the low level module and...
Definition: channel.c:6066
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
#define AST_CAUSE_UNALLOCATED
Definition: causes.h:97
void ast_channel_hangupcause_set(struct ast_channel *chan, int value)
static struct aco_type agent_type
plc_state_t plc_state
Definition: channel.c:5083
int presentation
presentation-indicator ie
Definition: channel.c:8425
struct ast_silence_generator * ast_channel_start_silence_generator(struct ast_channel *chan)
Starts a silence generator on the given channel.
Definition: channel.c:8266
int ast_func_read2(struct ast_channel *chan, const char *function, struct ast_str **str, ssize_t maxlen)
executes a read operation on a function
#define AST_DEFAULT_EMULATE_DTMF_DURATION
Definition: channel.c:102
int ast_safe_sleep(struct ast_channel *chan, int ms)
Wait, look for hangups.
Definition: channel.c:1574
Channel Variables.
int(*const send_digit_end)(struct ast_channel *chan, char digit, unsigned int duration)
Stop sending a literal DTMF digit.
Definition: channel.h:691
#define AST_RWLIST_WRLOCK(head)
Write locks a list.
Definition: linkedlists.h:51
int ast_channels_init(void)
Definition: channel.c:8109
#define OBJ_POINTER
Definition: astobj2.h:1154
#define AST_CAUSE_INVALID_MSG_UNSPECIFIED
Definition: causes.h:135
struct chanlist::@369 list
int ast_settimeout_full(struct ast_channel *c, unsigned int rate, int(*func)(const void *data), void *data, unsigned int is_ao2_obj)
Definition: channel.c:3194
static int ast_channel_by_exten_cb(void *obj, void *arg, void *data, int flags)
Definition: channel.c:1306
#define AST_CAUSE_NETWORK_OUT_OF_ORDER
Definition: causes.h:120
const char * ast_var_name(const struct ast_var_t *var)
Definition: chanvars.c:60
void ast_channel_set_rawwriteformat(struct ast_channel *chan, struct ast_format *format)
int ast_audiohook_write_list_empty(struct ast_audiohook_list *audiohook_list)
Determine if a audiohook_list is empty or not.
Definition: audiohook.c:1112
enum ast_media_type ast_stream_get_type(const struct ast_stream *stream)
Get the media type of a stream.
Definition: stream.c:316
int ast_connected_line_parse_data(const unsigned char *data, size_t datalen, struct ast_party_connected_line *connected)
Parse connected line indication frame data.
Definition: channel.c:8881
#define ast_set_flag(p, flag)
Definition: utils.h:70
struct ast_framehook_list * ast_channel_framehooks(const struct ast_channel *chan)
struct ast_party_number_ies number
Subscriber phone number ies.
Definition: channel.c:8646
int ast_framehook_list_is_empty(struct ast_framehook_list *framehooks)
Determine if an framehook list is empty or not.
Definition: framehook.c:274
void ast_channel_dialed_set(struct ast_channel *chan, struct ast_party_dialed *value)
descriptor for a cli entry.
Definition: cli.h:171
const int argc
Definition: cli.h:160
#define LOG_WARNING
Definition: logger.h:274
unsigned long ast_channel_insmpl(const struct ast_channel *chan)
ast_timer_event
Definition: timing.h:57
#define AST_MALLOCD_DATA
Audiohooks Architecture.
char * ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
Definition: strings.h:714
#define AST_FORMAT_CAP_NAMES_LEN
Definition: format_cap.h:326
int ast_channel_stream_topology_changed(struct ast_channel *chan, struct ast_stream_topology *topology)
Provide notice to a channel that the stream topology has changed.
Definition: channel.c:11197
AST_JSON_INT_T ast_json_int_t
Primarily used to cast when packing to an "I" type.
Definition: json.h:87
ast_framehook_event
These are the types of events that the framehook&#39;s event callback can receive.
Definition: framehook.h:151
void ast_pbx_hangup_handler_init(struct ast_channel *chan)
Init the hangup handler container on a channel.
static struct test_val d
#define ao2_callback(c, flags, cb_fn, arg)
Definition: astobj2.h:1716
void ast_moh_stop(struct ast_channel *chan)
Turn off music on hold on a given channel.
Definition: channel.c:7876
int ast_bridge_features_init(struct ast_bridge_features *features)
Initialize bridge features structure.
Definition: bridge.c:3687
int ast_say_digits(struct ast_channel *chan, int num, const char *ints, const char *lang)
says digits
Definition: channel.c:8349
struct timeval ast_channel_answertime(struct ast_channel *chan)
void ast_channel_internal_set_stream_topology_change_source(struct ast_channel *chan, void *change_source)
int plan
Type-Of-Number and Numbering-Plan ie.
Definition: channel.c:8497
static void * tonepair_alloc(struct ast_channel *chan, void *params)
Definition: channel.c:7598
static int timeout
Definition: cdr_mysql.c:86
struct ast_party_id priv_from
Who is redirecting the call (Sent to the party the call is redirected toward) - private representatio...
Definition: channel.h:537
#define AST_MIN_DTMF_GAP
Definition: channel.c:112
static int tmp()
Definition: bt_open.c:389
#define AST_RWLIST_UNLOCK(head)
Attempts to unlock a read/write based list.
Definition: linkedlists.h:150
int ast_queue_unhold(struct ast_channel *chan)
Queue an unhold frame.
Definition: channel.c:1216
static int ast_add_fd(struct pollfd *pfd, int fd)
if fd is a valid descriptor, set *pfd with the descriptor
Definition: channel.h:2842
#define AST_CAUSE_FACILITY_REJECTED
Definition: causes.h:116
#define AST_CAUSE_ACCESS_INFO_DISCARDED
Definition: causes.h:123
char * ast_recvtext(struct ast_channel *chan, int timeout)
Receives a text string from a channel Read a string of text from a channel.
Definition: channel.c:4762
static void destroy_hooks(struct ast_channel *chan)
Definition: channel.c:2537
#define AST_CAUSE_RECOVERY_ON_TIMER_EXPIRE
Definition: causes.h:142
void ast_tonepair_stop(struct ast_channel *chan)
Definition: channel.c:7695
int ast_channel_has_hook_requiring_audio(struct ast_channel *chan)
Check if the channel has any active hooks that require audio.
Definition: channel.c:2530
Structure for variables, used for configurations and for channel variables.
static int(* ast_moh_start_ptr)(struct ast_channel *, const char *, const char *)
Definition: channel.c:7846
struct ast_tone_zone * ast_channel_zone(const struct ast_channel *chan)
void ast_uninstall_music_functions(void)
Definition: channel.c:7859
ast_control_transfer
#define var
Definition: ast_expr2f.c:614
void ast_channel_generator_set(struct ast_channel *chan, struct ast_generator *value)
void(* set_format)(struct ast_channel *chan, struct ast_format *format)
Definition: channel.c:5652
struct ast_frame *(*const read_stream)(struct ast_channel *chan)
Read a frame (or chain of frames from the same stream), in standard format (see frame.h), with stream num.
Definition: channel.h:748
Named group member structure.
Definition: channel.c:7751
#define AST_CAUSE_MISDIALLED_TRUNK_PREFIX
Definition: causes.h:100
int ast_framehook_detach(struct ast_channel *chan, int framehook_id)
Detach an framehook from a channel.
Definition: framehook.c:177
globally accessible channel datastores
int ast_tvzero(const struct timeval t)
Returns true if the argument is 0,0.
Definition: time.h:108
uint64_t flags
Definition: app_dial.c:807
struct ast_set_party_id priv_orig
Definition: channel.h:564
void ast_channel_vstream_set(struct ast_channel *chan, struct ast_filestream *value)
#define AST_LIST_NEXT(elm, field)
Returns the next entry in the list after the given entry.
Definition: linkedlists.h:438
Test Framework API.
int(*const answer)(struct ast_channel *chan)
Answer the channel.
Definition: channel.h:708
AST_THREADSTORAGE_RAW(in_intercept_routine)
static void write_stream(struct ogg_vorbis_desc *s, FILE *f)
Write out any pending encoded data.
struct ast_party_redirecting redirecting
Redirecting/Diversion information.
static void silence_generator_release(struct ast_channel *chan, void *data)
Definition: channel.c:8232
enum ast_control_t38 request_response
#define EVENT_FLAG_CALL
Definition: manager.h:72
const struct ast_channel_tech * tech
Definition: channel.c:116
Definition: cli.h:152
int char_set
Character set the name is using.
Definition: channel.h:273
void ast_timer_close(struct ast_timer *handle)
Close an opened timing handle.
Definition: timing.c:154
Assume that the ao2_container is already locked.
Definition: astobj2.h:1067
Structure to pass both assignedid values to channel drivers.
Definition: channel.h:605
int ast_answer(struct ast_channel *chan)
Answer a channel.
Definition: channel.c:2814
void ast_channel_readtrans_set(struct ast_channel *chan, struct ast_trans_pvt *value)
static int kill_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
Definition: channel.c:413
void ast_party_id_free(struct ast_party_id *doomed)
Destroy the party id contents.
Definition: channel.c:1811
Structure for a data store type.
Definition: datastore.h:31
Structure used to transport a message through the frame core.
Definition: message.c:1406
ast_channel_state
ast_channel states
Definition: channelstate.h:35
void ast_channel_audiohooks_set(struct ast_channel *chan, struct ast_audiohook_list *value)
struct ast_timer * ast_timer_open(void)
Open a timer.
Definition: timing.c:122
static void clone_variables(struct ast_channel *original, struct ast_channel *clonechan)
Clone channel variables from &#39;clone&#39; channel into &#39;original&#39; channel.
Definition: channel.c:6917
char * str
Subscriber name (Malloced)
Definition: channel.h:265
int ast_transfer(struct ast_channel *chan, char *dest)
Transfer a call to dest, if the channel supports transfer.
Definition: channel.c:6577
struct stasis_message_type * ast_channel_flash_type(void)
Message type for when a hook flash occurs on a channel.
int ast_queue_hangup(struct ast_channel *chan)
Queue a hangup frame for channel.
Definition: channel.c:1150
void ast_frame_dump(const char *name, struct ast_frame *f, char *prefix)
Definition: main/frame.c:731
Definition: astman.c:222
int ast_say_character_str(struct ast_channel *chan, const char *str, const char *ints, const char *lang, enum ast_say_case_sensitivity sensitivity)
function to pronounce character and phonetic strings
Definition: channel.c:8367
#define AST_NUM_CHANNEL_BUCKETS
Definition: channel.h:156
static void * channel_cc_params_copy(void *data)
Definition: channel.c:10629
void ast_channel_answertime_set(struct ast_channel *chan, struct timeval *value)
int ast_senddigit_begin(struct ast_channel *chan, char digit)
Send a DTMF digit to a channel.
Definition: channel.c:4919
Definition of a media format.
Definition: format.c:43
int ast_recvchar(struct ast_channel *chan, int timeout)
Receives a text character from a channel.
Definition: channel.c:4751
void ast_party_id_copy(struct ast_party_id *dest, const struct ast_party_id *src)
Copy the source party id information to the destination party id.
Definition: channel.c:1765
void ast_party_connected_line_init(struct ast_party_connected_line *init)
Initialize the given connected line structure.
Definition: channel.c:2022
static void channel_set_intercept_mode(int in_intercept_mode)
Definition: channel.c:10419
int ast_str_append(struct ast_str **buf, ssize_t max_len, const char *fmt,...)
Append to a thread local dynamic string.
Definition: strings.h:1091
#define ast_cli_register_multiple(e, len)
Register multiple commands.
Definition: cli.h:265
void ast_channel_named_pickupgroups_set(struct ast_channel *chan, struct ast_namedgroups *value)
int(*const hangup)(struct ast_channel *chan)
Hangup (and possibly destroy) the channel.
Definition: channel.h:705
unsigned char valid
TRUE if the subaddress information is valid/present.
Definition: channel.h:329
struct ast_format *(* get_format)(struct ast_channel *chan)
Definition: channel.c:5651
void ao2_iterator_destroy(struct ao2_iterator *iter)
Destroy a container iterator.
void ast_party_redirecting_set(struct ast_party_redirecting *dest, const struct ast_party_redirecting *src, const struct ast_set_party_redirecting *update)
Set the redirecting information based on another redirecting source.
Definition: channel.c:2166
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)
Definition: channel.c:8518
static struct external_vars ami_vars
Definition: channel.c:7933
int connect_on_early_media
Definition: channel.h:1109
void ast_channel_internal_alertpipe_close(struct ast_channel *chan)
#define AST_OPTION_CC_AGENT_TYPE
ast_control_frame_type
Internal control frame subtype field values.
static const struct ast_datastore_info * suppress_get_datastore_information(enum ast_frame_type frametype)
Definition: channel.c:10968
int plc_fillin(plc_state_t *s, int16_t amp[], int len)
Fill-in a block of missing audio samples.
Definition: plc.c:175
int ast_channel_connected_line_macro(struct ast_channel *autoservice_chan, struct ast_channel *macro_chan, const void *connected_info, int is_caller, int is_frame)
Run a connected line interception macro and update a channel&#39;s connected line information.
Definition: channel.c:10435
struct timeval ast_tvnow(void)
Returns current timeval. Meant to replace calls to gettimeofday().
Definition: time.h:150
struct ast_stream_topology * ast_stream_topology_create_from_format_cap(struct ast_format_cap *cap)
A helper function that, given a format capabilities structure, creates a topology and separates the m...
Definition: stream.c:848
static void send_flash_event(struct ast_channel *chan)
Definition: channel.c:3411
void ast_channel_inherit_variables(const struct ast_channel *parent, struct ast_channel *child)
Inherits channel variable from parent to child channel.
Definition: channel.c:6866
unsigned int ast_callid
Definition: logger.h:87
struct ast_bridge * ast_channel_internal_bridge(const struct ast_channel *chan)
static int should_skip_dtmf(struct ast_channel *chan)
Determine whether or not we should ignore DTMF in the readq.
Definition: channel.c:3489
int ani2
Automatic Number Identification 2 (Info Digits)
Definition: channel.h:476
int(* generate)(struct ast_channel *chan, void *data, int len, int samples)
Definition: channel.h:234
struct ast_format * ast_format_cap_get_best_by_type(const struct ast_format_cap *cap, enum ast_media_type type)
Get the most preferred format for a particular media type.
Definition: format_cap.c:417
int presentation
presentation-indicator ie
Definition: channel.c:8499
void ast_channel_unregister(const struct ast_channel_tech *tech)
Unregister channel driver.
Definition: channel.c:570
int64_t ast_channel_get_duration_ms(struct ast_channel *chan)
Obtain how long it&#39;s been, in milliseconds, since the channel was created.
Definition: channel.c:2829
int ast_channel_sendhtml(struct ast_channel *chan, int subclass, const char *data, int datalen)
Sends HTML on given channel Send HTML or URL on link.
Definition: channel.c:6725
#define AST_LIST_EMPTY(head)
Checks whether the specified list contains any entries.
Definition: linkedlists.h:449
#define ao2_alloc_options(data_size, destructor_fn, options)
Definition: astobj2.h:406
#define ast_assert(a)
Definition: utils.h:695
#define ao2_link_flags(container, obj, flags)
Definition: astobj2.h:1572
struct ast_msg_data * ast_msg_data_alloc(enum ast_msg_data_source_type source, struct ast_msg_data_attribute attributes[], size_t count)
Allocates an ast_msg_data structure.
Definition: message.c:1418
enum ast_frame_type frametype
Definition: channel.c:10921
struct ast_frame * ast_translate(struct ast_trans_pvt *tr, struct ast_frame *f, int consume)
translates one or more frames Apply an input frame into the translator and receive zero or one output...
Definition: translate.c:565
#define ast_opt_transmit_silence
Definition: options.h:124
#define AST_OPTION_SECURE_MEDIA
#define ao2_unlock(a)
Definition: astobj2.h:730
static struct test_val c
void ast_channel_internal_errno_set(enum ast_channel_error error)
static int match(struct ast_sockaddr *addr, unsigned short callno, unsigned short dcallno, const struct chan_iax2_pvt *cur, int check_dcallno)
Definition: chan_iax2.c:2315
char * text
Definition: app_queue.c:1508
int ast_channel_blocker_tid(const struct ast_channel *chan)
struct ast_frame * ast_read_stream(struct ast_channel *chan)
Reads a frame, but does not filter to just the default streams.
Definition: channel.c:4307
#define ast_str_alloca(init_len)
Definition: strings.h:800
const char * ast_format_get_name(const struct ast_format *format)
Get the name associated with a format.
Definition: format.c:334
int64_t ast_tvdiff_ms(struct timeval end, struct timeval start)
Computes the difference (in milliseconds) between two struct timeval instances.
Definition: time.h:98
#define ast_strdup(str)
A wrapper for strdup()
Definition: astmm.h:243
static int call(void *data)
Definition: chan_pjsip.c:2358
Structure for a data store object.
Definition: datastore.h:68
#define SWAP(a, b)
Definition: utils.h:230
const char * str
Definition: app_jack.c:147
struct varshead * ast_channel_varshead(struct ast_channel *chan)
static int hangup(void *data)
Definition: chan_pjsip.c:2483
Generic File Format Support. Should be included by clients of the file handling routines. File service providers should instead include mod_format.h.
struct ast_variable * ast_channeltype_list(void)
return an ast_variable list of channeltypes
Definition: channel.c:188
struct ast_namedgroups * ast_unref_namedgroups(struct ast_namedgroups *groups)
Definition: channel.c:7832
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)
Queue a connected line update frame on a channel.
Definition: channel.c:9202
void ast_playtones_stop(struct ast_channel *chan)
Stop playing tones on a channel.
Definition: indications.c:393
struct ast_stream * ast_stream_topology_get_stream(const struct ast_stream_topology *topology, unsigned int position)
Get a specific stream from the topology.
Definition: stream.c:788
void ast_channel_softhangup_internal_flag_set(struct ast_channel *chan, int value)
char * str
Malloced subaddress string.
Definition: channel.h:314
#define AST_RWLIST_HEAD_INIT(head)
Initializes an rwlist head structure.
Definition: linkedlists.h:638
#define NULL
Definition: resample.c:96
const char * data
static void set_channel_answer_time(struct ast_channel *chan)
Definition: channel.c:2637
struct ao2_iterator simple_iterator
Definition: channel.c:1353
#define AST_CAUSE_NO_USER_RESPONSE
Definition: causes.h:107
static int should_trigger_dtmf_emulating(struct ast_channel *chan)
Determine whether or not we have to trigger dtmf emulating using 50 fps timer events especially when ...
Definition: channel.c:2864
const struct ast_datastore_info secure_call_info
void ast_party_redirecting_reason_set(struct ast_party_redirecting_reason *dest, const struct ast_party_redirecting_reason *src)
Set the redirecting reason information based on another redirecting reason source.
Definition: channel.c:2102
void ast_party_id_merge_copy(struct ast_party_id *dest, struct ast_party_id *base, struct ast_party_id *overlay)
Copy a merge of a given party id into another given party id to a given destination party id...
Definition: channel.c:1920
Definitions to aid in the use of thread local storage.
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)
Request a channel of a given type, with data as optional information used by the low level module and...
Definition: channel.c:6264
Out-of-call text message support.
int ast_set_write_format_interleaved_stereo(struct ast_channel *chan, struct ast_format *format)
Sets write format for a channel. All internal data will than be handled in an interleaved format...
Definition: channel.c:5872
#define AST_CAUSE_NORMAL_CIRCUIT_CONGESTION
Definition: causes.h:119
int value
Definition: syslog.c:37
void ast_cli(int fd, const char *fmt,...)
Definition: clicompat.c:6
void ast_channel_zone_set(struct ast_channel *chan, struct ast_tone_zone *value)
#define ast_cc_config_params_init()
Allocate and initialize an ast_cc_config_params structure.
Definition: ccss.h:135
void ast_channel_internal_fd_clear_all(struct ast_channel *chan)
#define AST_LIST_REMOVE(head, elm, field)
Removes a specific entry from a list.
Definition: linkedlists.h:855
static struct ast_frame * kill_exception(struct ast_channel *chan)
Definition: channel.c:401
int ast_queue_frame(struct ast_channel *chan, struct ast_frame *fin)
Queue one or more frames to a channel&#39;s frame queue.
Definition: channel.c:1139
#define AST_CAUSE_INVALID_NUMBER_FORMAT
Definition: causes.h:115
void ast_channel_internal_swap_stream_topology(struct ast_channel *chan1, struct ast_channel *chan2)
int ast_framehook_list_destroy(struct ast_channel *chan)
This is used by the channel API to detach and destroy all framehooks on a channel during channel dest...
Definition: framehook.c:206
static int priority
#define AST_MUTE_DIRECTION_WRITE
Definition: channel.h:4938
#define AST_LIST_TRAVERSE_SAFE_END
Closes a safe loop traversal block.
Definition: linkedlists.h:614
#define AST_PRES_ALLOWED_USER_NUMBER_NOT_SCREENED
Definition: callerid.h:329
const char * ast_channel_call_forward(const struct ast_channel *chan)
static char cid_num[AST_MAX_EXTENSION]
Definition: chan_mgcp.c:164
int ast_channel_priority(const struct ast_channel *chan)
int code
enum AST_REDIRECTING_REASON value for redirection
Definition: channel.h:511
int() ao2_callback_data_fn(void *obj, void *arg, void *data, int flags)
Type of a generic callback function.
Definition: astobj2.h:1248
struct ast_channel * ast_waitfor_nandfds(struct ast_channel **c, int n, int *fds, int nfds, int *exception, int *outfd, int *ms)
Wait for x amount of time on a file descriptor to have input.
Definition: channel.c:2997
unsigned char odd_even_indicator
TRUE if odd number of address signals.
Definition: channel.h:327
static int transfer
Definition: chan_mgcp.c:194
#define ast_verb(level,...)
Definition: logger.h:463
void ast_bridge_features_destroy(struct ast_bridge_features *features)
Destroy an allocated bridge features struct.
Definition: bridge.c:3741
struct ast_party_name_ies name
Subscriber name ies.
Definition: channel.c:8644
struct ast_trans_pvt * ast_channel_readtrans(const struct ast_channel *chan)
struct ast_channel * ast_bridge_peer(struct ast_bridge *bridge, struct ast_channel *chan)
Get the channel&#39;s bridge peer only if the bridge is two-party.
Definition: bridge.c:4142
struct ast_format * ast_format_none
Built-in "null" format.
Definition: format_cache.c:251
Indicate what information in ast_party_caller should be set.
Definition: channel.h:441
int ast_raw_answer_with_stream_topology(struct ast_channel *chan, struct ast_stream_topology *topology)
Answer a channel passing in a stream topology.
Definition: channel.c:2648
void ast_channel_callid_cleanup(struct ast_channel *chan)
static const char ast_stream_topology_changed_external[]
Set as the change source reason when a channel stream topology has been changed externally as a resul...
Definition: channel.h:222
#define AST_ALERT_FD
Definition: channel.h:201
int ast_atomic_fetchadd_int(volatile int *p, int v)
Atomically add v to *p and return the previous value of *p.
Definition: lock.h:755
int ast_datastore_free(struct ast_datastore *datastore)
Free a data store object.
Definition: datastore.c:68
struct ast_variable * vars
Definition: channel.h:1113
unsigned long global_fin
Definition: channel.c:95
int ast_translator_best_choice(struct ast_format_cap *dst_cap, struct ast_format_cap *src_cap, struct ast_format **dst_fmt_out, struct ast_format **src_fmt_out)
Chooses the best translation path.
Definition: translate.c:1385
the list of registered channel types
Definition: channel.c:121
int ast_app_group_update(struct ast_channel *oldchan, struct ast_channel *newchan)
Update all group counting for a channel to a new one.
Definition: main/app.c:2143
SAY_EXTERN int(* ast_say_money_str_full)(struct ast_channel *chan, const char *num, const char *ints, const char *lang, int audiofd, int ctrlfd) SAY_INIT(ast_say_money_str_full)
Definition: say.h:151
const char * pbx_builtin_getvar_helper(struct ast_channel *chan, const char *name)
Return a pointer to the value of the corresponding channel variable.
struct ast_frame_subclass subclass
Header for providers of file and format handling routines. Clients of these routines should include "...
void ast_change_name(struct ast_channel *chan, const char *newname)
Change channel name.
Definition: channel.c:6854
Media Stream API.
int ast_channel_datastore_inherit(struct ast_channel *from, struct ast_channel *to)
Inherit datastores from a parent to a child.
Definition: channel.c:2373
const char * uid
Definition: datastore.h:69
void ast_channel_set_caller(struct ast_channel *chan, const struct ast_party_caller *caller, const struct ast_set_party_caller *update)
Set the caller id information in the Asterisk channel.
Definition: channel.c:7459
void ast_party_dialed_init(struct ast_party_dialed *init)
Initialize the given dialed structure.
Definition: channel.c:1928
static int does_id_conflict(const char *uniqueid)
Definition: channel.c:717
void ast_channel_writetrans_set(struct ast_channel *chan, struct ast_trans_pvt *value)
Utility functions.
int ast_channel_datastore_remove(struct ast_channel *chan, struct ast_datastore *datastore)
Remove a datastore from a channel.
Definition: channel.c:2399
#define AST_PRES_RESTRICTED
Definition: callerid.h:325
int ast_indicate(struct ast_channel *chan, int condition)
Indicates condition of channel.
Definition: channel.c:4322
#define AST_OPTION_FORMAT_WRITE
void ast_party_redirecting_reason_free(struct ast_party_redirecting_reason *doomed)
Destroy the redirecting reason contents.
Definition: channel.c:2116
static int set_security_requirements(const struct ast_channel *requestor, struct ast_channel *out)
Definition: channel.c:6269
static struct ast_threadstorage state2str_threadbuf
Definition: channel.c:97
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)
Forwards a call to a new channel specified by the original channel&#39;s call_forward str...
Definition: channel.c:5988
void() ao2_prnt_fn(void *where, const char *fmt,...)
Print output.
Definition: astobj2.h:1442
#define ast_strlen_zero(foo)
Definition: strings.h:52
void ast_party_caller_set_init(struct ast_party_caller *init, const struct ast_party_caller *guide)
Initialize the given caller structure using the given guide for a set update operation.
Definition: channel.c:1999
static const struct ast_datastore_info suppress_datastore_voice
Definition: channel.c:10910
struct timeval * ast_channel_whentohangup(struct ast_channel *chan)
struct ast_format * ast_channel_readformat(struct ast_channel *chan)
#define ast_manager_event(chan, category, event, contents,...)
Definition: manager.h:250
struct ast_set_party_id from
Definition: channel.h:560
int ast_channel_suppress(struct ast_channel *chan, unsigned int direction, enum ast_frame_type frametype)
Suppress passing of a frame type on a channel.
Definition: channel.c:10978
struct ast_party_id orig
Who originally redirected the call (Sent to the party the call is redirected toward) ...
Definition: channel.h:525
void ast_channel_stream_set(struct ast_channel *chan, struct ast_filestream *value)
void ast_channel_tech_set(struct ast_channel *chan, const struct ast_channel_tech *value)
int done
Definition: test_amihooks.c:48
int(*const call)(struct ast_channel *chan, const char *addr, int timeout)
Make a call.
Definition: channel.h:702
ast_group_t ast_get_group(const char *s)
Definition: channel.c:7718
static int ast_channel_hash_cb(const void *obj, const int flags)
Definition: channel.c:7888
int ast_framehook_list_contains_no_active_of_type(struct ast_framehook_list *framehooks, enum ast_frame_type type)
Determine if a framehook list is free of active framehooks consuming a specific type of frame...
Definition: framehook.c:287
struct chanlist * next
Definition: app_dial.c:797
Number structure.
Definition: app_followme.c:154
static void plc_ds_destroy(void *data)
Definition: channel.c:5086
#define attribute_const
Definition: compiler.h:41
#define ao2_bump(obj)
Definition: astobj2.h:491
void ast_channel_internal_alertpipe_swap(struct ast_channel *chan1, struct ast_channel *chan2)
Swap the interal alertpipe between two channels.
static const struct ast_channel_tech surrogate_tech
Channel technology used to extract a channel from a running application. The channel created with thi...
Definition: channel.c:702
static int deactivate_silence_generator(struct ast_channel *chan)
Definition: channel.c:8289
int ast_framehook_attach(struct ast_channel *chan, struct ast_framehook_interface *i)
Attach an framehook onto a channel for frame interception.
Definition: framehook.c:132
static int indicate_data_internal(struct ast_channel *chan, int _condition, const void *data, size_t datalen)
Definition: channel.c:4515
void ast_party_caller_free(struct ast_party_caller *doomed)
Destroy the caller party contents.
Definition: channel.c:2015
struct ast_party_id id
Caller party ID.
Definition: channel.h:421
static void suppress_framehook_fixup_cb(void *data, int framehook_id, struct ast_channel *old_chan, struct ast_channel *new_chan)
Definition: channel.c:10926
void ast_party_name_init(struct ast_party_name *init)
Initialize the given name structure.
Definition: channel.c:1591
void(* write_format_change)(struct ast_channel *chan, void *data)
Definition: channel.h:239
void ast_set_hangupsource(struct ast_channel *chan, const char *source, int force)
Set the source of the hangup in this channel and it&#39;s bridge.
Definition: channel.c:2504
#define AST_CAUSE_RESPONSE_TO_STATUS_ENQUIRY
Definition: causes.h:117
int ast_moh_start(struct ast_channel *chan, const char *mclass, const char *interpclass)
Turn on music on hold on a given channel.
Definition: channel.c:7866
int ast_set_write_format_from_cap(struct ast_channel *chan, struct ast_format_cap *cap)
Sets write format on channel chan Set write format for channel to whichever component of "format" is ...
Definition: channel.c:5908
int duration
Definition: channel.c:7565
#define AST_RWLIST_RDLOCK(head)
Read locks a list.
Definition: linkedlists.h:77
List of channel variables to append to all channel-related events.
Definition: channel.c:7925
void ao2_container_unregister(const char *name)
Unregister a container for CLI stats and integrity check.
void ast_party_subaddress_init(struct ast_party_subaddress *init)
Initialize the given subaddress structure.
Definition: channel.c:1697
#define ast_str_tmp(init_len, __expr)
Definition: strings.h:1136
void ast_party_number_free(struct ast_party_number *doomed)
Destroy the party number contents.
Definition: channel.c:1691
struct ast_readq_list * ast_channel_readq(struct ast_channel *chan)
#define AST_RWLIST_INSERT_HEAD
Definition: linkedlists.h:717
int ast_check_hangup(struct ast_channel *chan)
Checks to see if a channel is needing hang up.
Definition: channel.c:445
char * ast_print_group(char *buf, int buflen, ast_group_t group)
Print call group and pickup group —.
Definition: channel.c:8133
void ast_channel_internal_swap_endpoint_forward(struct ast_channel *a, struct ast_channel *b)
Swap endpoint_forward between two channels.
void(* chan_fixup)(void *data, struct ast_channel *old_chan, struct ast_channel *new_chan)
Fix up channel references on the masquerading channel.
Definition: datastore.h:50
static int kill_hangup(struct ast_channel *chan)
Definition: channel.c:419
int ast_indicate_data(struct ast_channel *chan, int _condition, const void *data, size_t datalen)
Indicates condition of channel, with payload.
Definition: channel.c:4698
int ao2_container_register(const char *name, struct ao2_container *self, ao2_prnt_obj_fn *prnt_obj)
Register a container for CLI stats and integrity check.
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:452
void ast_party_redirecting_reason_init(struct ast_party_redirecting_reason *init)
Initialize the given redirecting reason structure.
Definition: channel.c:2079
#define ast_log
Definition: astobj2.c:42
const char * exten
Definition: channel.h:1107
void ast_channel_masq_set(struct ast_channel *chan, struct ast_channel *value)
void ast_channel_set_rawreadformat(struct ast_channel *chan, struct ast_format *format)
int ast_channel_supports_html(struct ast_channel *chan)
Checks for HTML support on a channel.
Definition: channel.c:6720
int ast_say_enumeration(struct ast_channel *chan, int num, const char *ints, const char *language, const char *options)
says an enumeration
Definition: channel.c:8343
short data[4000]
Definition: channel.c:7584
#define AST_CAUSE_NO_ROUTE_TRANSIT_NET
Definition: causes.h:98
#define MAX(a, b)
Definition: utils.h:228
void ast_channel_cdr_set(struct ast_channel *chan, struct ast_cdr *value)
struct ast_frame f
Definition: channel.c:7582
unsigned int ast_channel_emulate_dtmf_duration(const struct ast_channel *chan)
unsigned int ast_channel_fin(const struct ast_channel *chan)
int16_t * samples_buf
Definition: channel.c:5078
static int redirecting_reason_build_data(unsigned char *data, size_t datalen, const struct ast_party_redirecting_reason *reason, const char *label, const struct ast_party_redirecting_reason_ies *ies)
Definition: channel.c:9330
void ast_party_connected_line_free(struct ast_party_connected_line *doomed)
Destroy the connected line information contents.
Definition: channel.c:2072
int ast_set_read_format_from_cap(struct ast_channel *chan, struct ast_format_cap *cap)
Sets read format on channel chan from capabilities Set read format for channel to whichever component...
Definition: channel.c:5867
const char * cid_name
Definition: channel.h:1111
unsigned int isfunc
Definition: channel.c:7927
void ast_channel_internal_swap_topics(struct ast_channel *a, struct ast_channel *b)
Swap topics beteween two channels.
const char * ast_stream_topology_to_str(const struct ast_stream_topology *topology, struct ast_str **buf)
Get a string representing the topology for debugging/display purposes.
Definition: stream.c:936
enum ast_format_cmp_res ast_format_cmp(const struct ast_format *format1, const struct ast_format *format2)
Compare two formats.
Definition: format.c:201
static int ast_channel_softhangup_cb(void *obj, void *arg, int flags)
Definition: channel.c:484
int ast_channel_redirecting_sub(struct ast_channel *autoservice_chan, struct ast_channel *sub_chan, const void *redirecting_info, int is_frame)
Run a redirecting interception subroutine and update a channel&#39;s redirecting information.
Definition: channel.c:10584
struct ast_trans_pvt * ast_translator_build_path(struct ast_format *dest, struct ast_format *source)
Builds a translator path Build a path (possibly NULL) from source to dest.
Definition: translate.c:485
void ast_party_id_set(struct ast_party_id *dest, const struct ast_party_id *src, const struct ast_set_party_id *update)
Set the source party id information into the destination party id.
Definition: channel.c:1788
#define AST_MUTE_DIRECTION_READ
Definition: channel.h:4937
int ast_channel_get_device_name(struct ast_channel *chan, char *device_name, size_t name_buffer_length)
Get a device name given its channel structure.
Definition: channel.c:10697
void ast_moh_cleanup(struct ast_channel *chan)
Definition: channel.c:7882
const char * ast_channel_latest_musicclass(const struct ast_channel *chan)
struct ast_party_id ani
Automatic Number Identification (ANI)
Definition: channel.h:428
int ast_channel_early_bridge(struct ast_channel *c0, struct ast_channel *c1)
Bridge two channels together (early)
Definition: channel.c:7512
static struct ast_channel * request_channel(const char *type, struct ast_format_cap *request_cap, struct ast_stream_topology *topology, const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor, const char *addr, int *cause)
Definition: channel.c:6309
int ast_call(struct ast_channel *chan, const char *addr, int timeout)
Make a call.
Definition: channel.c:6553
General Asterisk PBX channel definitions.
static void varshead_dtor(void *obj)
Destructor for lists of variables.
Definition: channel.c:8003
#define AST_PRES_USER_NUMBER_UNSCREENED
Definition: callerid.h:318
#define SCOPED_MUTEX(varname, lock)
scoped lock specialization for mutexes
Definition: lock.h:587
int ast_register_cleanup(void(*func)(void))
Register a function to be executed before Asterisk gracefully exits.
Definition: clicompat.c:19
const char * ast_channel_accountcode(const struct ast_channel *chan)
Asterisk file paths, configured in asterisk.conf.
int(*const send_html)(struct ast_channel *chan, int subclass, const char *data, int len)
Send HTML data.
Definition: channel.h:763
#define AST_FRIENDLY_OFFSET
Offset into a frame&#39;s data buffer.
int ast_threadstorage_set_ptr(struct ast_threadstorage *ts, void *ptr)
Set a raw pointer from threadstorage.
const char * src
static char * handle_cli_core_show_channeltype(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
Show details about a channel driver - CLI command.
Definition: channel.c:326
static int ast_channel_by_uniqueid_cb(void *obj, void *arg, void *data, int flags)
Definition: channel.c:1329
struct ast_channel * chan
Definition: app_dial.c:798
void ast_channel_stop_silence_generator(struct ast_channel *chan, struct ast_silence_generator *state)
Stops a previously-started silence generator on the given channel.
Definition: channel.c:8312
SAY_EXTERN int(* ast_say_digit_str_full)(struct ast_channel *chan, const char *num, const char *ints, const char *lang, int audiofd, int ctrlfd) SAY_INIT(ast_say_digit_str_full)
Same as ast_say_digit_str() with audiofd for received audio and returns 1 on ctrlfd being readable...
Definition: say.h:143
int ast_write_stream(struct ast_channel *chan, int stream_num, struct ast_frame *fr)
Write a frame to a stream This function writes the given frame to the indicated stream on the channel...
Definition: channel.c:5194
static const struct set_format_access set_format_access_read
Definition: channel.c:5658
struct ast_stream * ast_channel_get_default_stream(struct ast_channel *chan, enum ast_media_type type)
Retrieve the default stream of a specific media type on a channel.
struct ast_namedgroups * ast_get_namedgroups(const char *s)
Create an ast_namedgroups set with group names from comma separated string.
Definition: channel.c:7775
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.
Definition: utils.h:911
void ast_channel_generatordata_set(struct ast_channel *chan, void *value)
int ast_get_tid(void)
Get current thread ID.
Definition: main/utils.c:2504
void ast_channel_clear_flag(struct ast_channel *chan, unsigned int flag)
Definition: channel.c:11235
static struct ast_generator gen
const int fd
Definition: cli.h:159
void ast_connected_line_copy_to_caller(struct ast_party_caller *dest, const struct ast_party_connected_line *src)
Copy the connected line information to the caller information.
Definition: channel.c:8396
void ast_channel_nativeformats_set(struct ast_channel *chan, struct ast_format_cap *value)
struct ast_party_redirecting_reason orig_reason
Reason for the redirection by the original party.
Definition: channel.h:546
struct ast_trans_pvt * ast_channel_writetrans(const struct ast_channel *chan)
void ast_channel_stage_snapshot_done(struct ast_channel *chan)
Clear flag to indicate channel snapshot is being staged, and publish snapshot.
#define AST_TRANS_CAP_DIGITAL_W_TONES
Definition: transcap.h:39
static int frames
Definition: parser.c:51
struct ast_party_connected_line * ast_channel_connected(struct ast_channel *chan)
const char * account
Definition: channel.h:1112
int ast_channel_visible_indication(const struct ast_channel *chan)
unsigned char subaddress
Definition: channel.h:368
static void handle_cause(int cause, int *outstate)
Definition: channel.c:5936
int ast_timer_disable_continuous(const struct ast_timer *handle)
Disable continuous mode.
Definition: timing.c:181
#define M_PI
Definition: resample.c:83
#define ast_poll(a, b, c)
Definition: poll-compat.h:88
struct ast_pbx * ast_channel_pbx(const struct ast_channel *chan)
#define AST_CAUSE_ANSWERED_ELSEWHERE
Definition: causes.h:113
int(*const write_text)(struct ast_channel *chan, struct ast_frame *frame)
Write a text frame, in standard format.
Definition: channel.h:790
#define AST_TRANS_CAP_3_1K_AUDIO
Definition: transcap.h:37
struct ast_bridge * ast_channel_get_bridge(const struct ast_channel *chan)
Get the bridge associated with a channel.
Definition: channel.c:10735
ast_mutex_t lock
Definition: app_meetme.c:1091
struct ast_channel * ast_channel_iterator_next(struct ast_channel_iterator *i)
Get the next channel for a channel iterator.
Definition: channel.c:1422
void ast_channel_set_flag(struct ast_channel *chan, unsigned int flag)
Set a flag on a channel.
Definition: channel.c:11228
ast_cond_t cond
Definition: app_meetme.c:1090
char ast_defaultlanguage[]
Definition: options.c:98
#define AST_RWLIST_TRAVERSE
Definition: linkedlists.h:493
char * ast_strip(char *s)
Strip leading/trailing whitespace from a string.
Definition: strings.h:219
#define AST_CAUSE_NORMAL_CLEARING
Definition: causes.h:105
Scheduler Routines (derived from cheops)
#define AST_CAUSE_CHAN_NOT_IMPLEMENTED
Definition: causes.h:131
Caller Party information.
Definition: channel.h:419
#define AST_LIST_REMOVE_CURRENT(field)
Removes the current entry from a list during a traversal.
Definition: linkedlists.h:556
static int indicate_connected_line(struct ast_channel *chan, const void *data, size_t datalen)
Definition: channel.c:4446
#define ast_channel_cleanup(c)
Cleanup a channel reference.
Definition: channel.h:2992
ast_alert_status_t ast_channel_internal_alert_read(struct ast_channel *chan)
Asterisk internal frame definitions.
static int tech_write(struct ast_channel *chan, struct ast_stream *stream, struct ast_stream *default_stream, struct ast_frame *frame)
Definition: channel.c:5177
#define ao2_ref(o, delta)
Definition: astobj2.h:464
void ast_channel_set_readformat(struct ast_channel *chan, struct ast_format *format)
#define S_COR(a, b, c)
returns the equivalent of logic or for strings, with an additional boolean check: second one if not e...
Definition: strings.h:85
void ast_party_number_copy(struct ast_party_number *dest, const struct ast_party_number *src)
Copy the source party number information to the destination party number.
Definition: channel.c:1652
void ast_jb_destroy(struct ast_channel *chan)
Destroys jitterbuffer on a channel.
Definition: abstract_jb.c:502
struct ast_datastore_list * ast_channel_datastores(struct ast_channel *chan)
void(* release)(struct ast_channel *chan, void *data)
Definition: channel.h:229
int ast_redirecting_parse_data(const unsigned char *data, size_t datalen, struct ast_party_redirecting *redirecting)
Parse redirecting indication frame data.
Definition: channel.c:9574
struct ast_cc_config_params * ast_channel_get_cc_config_params(struct ast_channel *chan)
Get the CCSS parameters from a channel.
Definition: channel.c:10675
const struct ast_datastore_info * info
Definition: datastore.h:71
struct ast_party_id priv
Private caller party ID.
Definition: channel.h:431
#define ao2_lock(a)
Definition: astobj2.h:718
struct ast_channel_iterator * ast_channel_iterator_all_new(void)
Create a new channel iterator.
Definition: channel.c:1408
void ast_channel_undefer_dtmf(struct ast_channel *chan)
Unset defer DTMF flag on channel.
Definition: channel.c:1271
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:300
static struct ast_tone_zone_sound * ast_tone_zone_sound_unref(struct ast_tone_zone_sound *ts)
Release a reference to an ast_tone_zone_sound.
Definition: indications.h:227
static int silence_generator_generate(struct ast_channel *chan, void *data, int len, int samples)
Definition: channel.c:8237
List of channel drivers.
Definition: app_dial.c:796
void ast_channel_internal_cleanup(struct ast_channel *chan)
#define AST_GENERATOR_FD
Definition: channel.h:204
static int ast_channel_cmp_cb(void *obj, void *arg, int flags)
Definition: channel.c:1428
struct ast_bridge_channel * ast_channel_internal_bridge_channel(const struct ast_channel *chan)
struct ast_set_party_id id
Definition: channel.h:443
map AST_CAUSE&#39;s to readable string representations
Definition: channel.c:130
void ast_party_subaddress_free(struct ast_party_subaddress *doomed)
Destroy the party subaddress contents.
Definition: channel.c:1744
#define ast_format_cap_append(cap, format, framing)
Definition: format_cap.h:103
void ast_channel_amaflags_set(struct ast_channel *chan, enum ama_flags value)
#define SCOPED_LOCK(varname, lock, lockfunc, unlockfunc)
Scoped Locks.
Definition: lock.h:581
A set of macros to manage forward-linked lists.
static char language[MAX_LANGUAGE]
Definition: chan_alsa.c:117
void ast_party_name_free(struct ast_party_name *doomed)
Destroy the party name contents.
Definition: channel.c:1638
int ast_senddigit_end(struct ast_channel *chan, char digit, unsigned int duration)
Send a DTMF digit to a channel.
Definition: channel.c:4969
#define ast_malloc(len)
A wrapper for malloc()
Definition: astmm.h:193
unsigned int hash
Definition: channel.c:7753
void ast_channel_internal_swap_uniqueid_and_linkedid(struct ast_channel *a, struct ast_channel *b)
Swap uniqueid and linkedid beteween two channels.
int ast_sendtext_data(struct ast_channel *chan, struct ast_msg_data *msg)
Sends text to a channel in an ast_msg_data structure wrapper with ast_sendtext as fallback...
Definition: channel.c:4796
#define ast_variable_new(name, value, filename)
struct varshead * ast_channel_get_vars(struct ast_channel *chan)
Gets the variables for a given channel, as set using pbx_builtin_setvar_helper(). ...
Definition: channel.c:8013
static struct ast_generator silence_generator
Definition: channel.c:8256
int(*const send_text)(struct ast_channel *chan, const char *text)
Display or transmit text.
Definition: channel.h:757
#define AST_LIST_REMOVE_HEAD(head, field)
Removes and returns the head entry from a list.
Definition: linkedlists.h:832
#define AST_CAUSE_NO_ANSWER
Definition: causes.h:108
#define ast_opt_generic_plc
Definition: options.h:134
static const struct causes_map causes[]
Definition: channel.c:136
int ast_settimeout(struct ast_channel *c, unsigned int rate, int(*func)(const void *data), void *data)
Enable or disable timer ticks for a channel.
Definition: channel.c:3189
AST_LIST_HEAD_NOLOCK(contactliststruct, contact)
void * ast_channel_timingdata(const struct ast_channel *chan)
int ast_channel_unsuppress(struct ast_channel *chan, unsigned int direction, enum ast_frame_type frametype)
Stop suppressing of a frame type on a channel.
Definition: channel.c:11040
struct ast_party_id ani
Automatic Number Identification (ANI)
Definition: channel.h:466
int ast_auto_answer(struct ast_channel *chan)
Answer a channel, if it&#39;s not already answered.
Definition: channel.c:2820
static int answer(void *data)
Definition: chan_pjsip.c:682
Channels with this particular technology are an implementation detail of Asterisk and should generall...
Definition: channel.h:972
void ast_party_id_reset(struct ast_party_id *id)
Destroy and initialize the given party id structure.
Definition: channel.c:1896
#define ast_format_cap_alloc(flags)
Definition: format_cap.h:52
int odd_even_indicator
odd/even indicator ie
Definition: channel.c:8573
const char * ast_state2str(enum ast_channel_state state)
Gives the string form of a given channel state.
Definition: channel.c:642
void ast_channel_caller_set(struct ast_channel *chan, struct ast_party_caller *value)
static void apply_plc(struct ast_channel *chan, struct ast_frame *frame)
Definition: channel.c:5151
#define AST_CAUSE_NUMBER_PORTED_NOT_HERE
Definition: causes.h:104
#define AST_CAUSE_FACILITY_NOT_IMPLEMENTED
Definition: causes.h:132
#define AST_CAUSE_NOTDEFINED
Definition: causes.h:154
Structure to describe a channel "technology", ie a channel driver See for examples: ...
Definition: channel.h:629
void ast_channel_adsicpe_set(struct ast_channel *chan, enum ast_channel_adsicpe value)
const char * ast_channel_exten(const struct ast_channel *chan)
int(* ast_timing_func_t)(const void *data)
Definition: channel.h:900
Core PBX routines and definitions.
int ast_endpoint_add_channel(struct ast_endpoint *endpoint, struct ast_channel *chan)
Adds a channel to the given endpoint.
static struct external_vars ari_vars
Definition: channel.c:7934
void ast_party_subaddress_copy(struct ast_party_subaddress *dest, const struct ast_party_subaddress *src)
Copy the source party subaddress information to the destination party subaddress. ...
Definition: channel.c:1705
int ast_format_cache_is_slinear(struct ast_format *format)
Determines if a format is one of the cached slin formats.
Definition: format_cache.c:542
int(* pre_call)(struct ast_channel *chan, const char *sub_args)
Execute a Gosub call on the channel in a technology specific way before a call is placed...
Definition: channel.h:842
void ast_channel_dtmf_tv_set(struct ast_channel *chan, struct timeval *value)
int ast_autoservice_stop(struct ast_channel *chan)
Stop servicing a channel for us...
Definition: autoservice.c:266
void ast_format_cap_remove_by_type(struct ast_format_cap *cap, enum ast_media_type type)
Remove all formats matching a specific format type.
Definition: format_cap.c:525
struct ast_party_connected_line connected
Definition: app_dial.c:809
int ast_channel_get_cc_agent_type(struct ast_channel *chan, char *agent_type, size_t size)
Find the appropriate CC agent type to use given a channel.
Definition: channel.c:10714
struct ast_set_party_id priv
Definition: channel.h:496
#define AST_RWLIST_TRAVERSE_SAFE_BEGIN
Definition: linkedlists.h:544
static void channel_req_accountcodes(struct ast_channel *chan, const struct ast_channel *requestor, enum ast_channel_requestor_relationship relationship, int precious)
Definition: channel.c:6468
ast_say_case_sensitivity
Controls how ast_say_character_str denotes the case of characters in a string.
Definition: say.h:162
#define AST_CAUSE_DESTINATION_OUT_OF_ORDER
Definition: causes.h:114
const char * ast_channel_amaflags2string(enum ama_flags flag)
Convert the enum representation of an AMA flag to a string representation.
Definition: channel.c:4418
struct ast_sched_context * ast_sched_context_create(void)
Create a scheduler context.
Definition: sched.c:236
#define ast_test_suite_event_notify(s, f,...)
Definition: test.h:196
#define ast_alloca(size)
call __builtin_alloca to ensure we get gcc builtin semantics
Definition: astmm.h:290
int ast_channel_fdno(const struct ast_channel *chan)
void ast_channel_masqr_set(struct ast_channel *chan, struct ast_channel *value)
void ast_channel_timingdata_set(struct ast_channel *chan, void *value)
const char * ast_channel_uniqueid(const struct ast_channel *chan)
ast_frame_type
Frame types.
int ast_senddigit_mf_begin(struct ast_channel *chan, char digit)
Send an MF digit to a channel.
Definition: channel.c:4880
void ast_party_redirecting_free(struct ast_party_redirecting *doomed)
Destroy the redirecting information contents.
Definition: channel.c:2179
const char *const * argv
Definition: cli.h:161
static void channel_do_masquerade(struct ast_channel *original, struct ast_channel *clonechan)
Masquerade a channel.
Definition: channel.c:6952
void ast_party_connected_line_copy(struct ast_party_connected_line *dest, const struct ast_party_connected_line *src)
Copy the source connected line information to the destination connected line.
Definition: channel.c:2031
void ast_bridge_features_merge(struct ast_bridge_features *into, const struct ast_bridge_features *from)
Merge one ast_bridge_features into another.
Definition: bridge.c:3662
int ast_channel_timingfd(const struct ast_channel *chan)
struct ast_party_dialed dialed
Dialed/Called information.
int ast_channel_make_compatible(struct ast_channel *chan, struct ast_channel *peer)
Make the frame formats of two channels compatible.
Definition: channel.c:6817
#define DEFAULT_AMA_FLAGS
Definition: channel.c:108
int ast_readstring_full(struct ast_channel *c, char *s, int len, int timeout, int ftimeout, char *enders, int audiofd, int ctrlfd)
Definition: channel.c:6660
Redirecting reason information.
Definition: channel.h:502
The AMI - Asterisk Manager Interface - is a TCP protocol created to manage Asterisk with third-party ...
void ast_framehook_list_fixup(struct ast_channel *old_chan, struct ast_channel *new_chan)
This is used by the channel API during a masquerade operation to move all mobile framehooks from the ...
Definition: framehook.c:223
int tag
User party id tag ie.
Definition: channel.c:8650
int ast_waitfordigit_full(struct ast_channel *c, int timeout_ms, const char *breakon, int audiofd, int cmdfd)
Wait for a digit Same as ast_waitfordigit() with audio fd for outputting read audio and ctrlfd to mon...
Definition: channel.c:3248
struct ast_party_subaddress subaddress
Subscriber subaddress.
Definition: channel.h:345
void ast_channel_stage_snapshot(struct ast_channel *chan)
Set flag to indicate channel snapshot is being staged.
"smart" channels that update automatically if a channel is masqueraded
Dialed/Called Party information.
Definition: channel.h:379
static const float mf_tones[]
Definition: dsp.c:323
static int namedgroup_hash_cb(const void *obj, const int flags)
Hashing function used for named group container.
Definition: channel.c:7768
DtmfDirection
Definition: channel.c:3361
Structure that contains information about a bridge.
Definition: bridge.h:357
#define AST_CAUSE_FACILITY_NOT_SUBSCRIBED
Definition: causes.h:125
int interleaved_stereo
Definition: translate.h:238
int ast_timer_ack(const struct ast_timer *handle, unsigned int quantity)
Acknowledge a timer event.
Definition: timing.c:171
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)
Set the connected line information based on another connected line source.
Definition: channel.c:2054
Indicate what information in ast_party_id should be set.
Definition: channel.h:362
struct ast_format * ast_channel_rawreadformat(struct ast_channel *chan)
void ast_channel_req_accountcodes(struct ast_channel *chan, const struct ast_channel *requestor, enum ast_channel_requestor_relationship relationship)
Setup new channel accountcodes from the requestor channel after ast_request().
Definition: channel.c:6526
char name[0]
Definition: chanvars.h:31
int ast_sendtext(struct ast_channel *chan, const char *text)
Sends text to a channel.
Definition: channel.c:4854
int ast_say_phonetic_str(struct ast_channel *chan, const char *str, const char *ints, const char *lang)
Definition: channel.c:8373
#define AST_CAUSE_REDIRECTED_TO_NEW_DESTINATION
Definition: causes.h:112
struct ast_sched_context * ast_channel_sched(const struct ast_channel *chan)
#define LOG_ERROR
Definition: logger.h:285
int ast_tvcmp(struct timeval _a, struct timeval _b)
Compres two struct timeval instances returning -1, 0, 1 if the first arg is smaller, equal or greater to the second.
Definition: time.h:128
void ast_party_redirecting_init(struct ast_party_redirecting *init)
Initialize the given redirecting structure.
Definition: channel.c:2122
#define AST_LIST_INSERT_TAIL(head, elm, field)
Appends a list entry to the tail of a list.
Definition: linkedlists.h:730
struct ast_generator * ast_channel_generator(const struct ast_channel *chan)
#define ao2_container_alloc_hash(ao2_options, container_options, n_buckets, hash_fn, sort_fn, cmp_fn)
Definition: astobj2.h:1310
void ast_party_caller_copy(struct ast_party_caller *dest, const struct ast_party_caller *src)
Copy the source caller information to the destination caller.
Definition: channel.c:1986
struct ast_frame * ast_audiohook_write_list(struct ast_channel *chan, struct ast_audiohook_list *audiohook_list, enum ast_audiohook_direction direction, struct ast_frame *frame)
Pass a frame off to be handled by the audiohook core.
Definition: audiohook.c:1127
void ast_party_redirecting_reason_set_init(struct ast_party_redirecting_reason *init, const struct ast_party_redirecting_reason *guide)
Initialize the given redirecting reason structure using the given guide for a set update operation...
Definition: channel.c:2096
#define ast_opt_transcode_via_slin
Definition: options.h:116
int(*const write_stream)(struct ast_channel *chan, int stream_num, struct ast_frame *frame)
Write a frame on a specific stream, in standard format (see frame.h)
Definition: channel.h:754
struct ast_channel * __ast_dummy_channel_alloc(const char *file, int line, const char *function)
Definition: channel.c:981
int ast_channel_has_ari_vars(void)
Return whether or not any ARI variables have been set.
Definition: channel.c:7962
struct ast_channel * ast_channel_callback(ao2_callback_data_fn *cb_fn, void *arg, void *data, int ao2_flags)
Call a function with every active channel.
Definition: channel.c:1278
struct timeval * ast_channel_dtmf_tv(struct ast_channel *chan)
The descriptor of a dynamic string XXX storage will be optimized later if needed We use the ts field ...
Definition: strings.h:584
int ast_cc_offer(struct ast_channel *caller_chan)
Offer CC to a caller.
Definition: ccss.c:3751
static struct ast_generator generator
Definition: app_fax.c:359
Format capabilities structure, holds formats + preference order + etc.
Definition: format_cap.c:54
Default structure for translators, with the basic fields and buffers, all allocated as part of the sa...
Definition: translate.h:213
struct ast_channel_iterator * ast_channel_iterator_destroy(struct ast_channel_iterator *i)
Destroy a channel iterator.
Definition: channel.c:1360
#define AST_CAUSE_CALL_AWARDED_DELIVERED
Definition: causes.h:102
struct ast_channel * ast_waitfor_n(struct ast_channel **c, int n, int *ms)
Waits for input on a group of channels Wait for input on an array of channels for a given # of millis...
Definition: channel.c:3166
Internal channel functions for channel.c to use.
int ast_stream_topology_equal(const struct ast_stream_topology *left, const struct ast_stream_topology *right)
Compare two stream topologies to see if they are equal.
Definition: stream.c:696
int ast_channel_internal_alertpipe_init(struct ast_channel *chan)
static struct ast_frame * suppress_framehook_event_cb(struct ast_channel *chan, struct ast_frame *frame, enum ast_framehook_event event, void *data)
Definition: channel.c:10933
struct ast_set_party_id priv_from
Definition: channel.h:566
static struct ast_channel * __ast_channel_alloc_ap(int needqueue, int state, const char *cid_num, const char *cid_name, const char *acctcode, const char *exten, const char *context, const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor, enum ama_flags amaflag, struct ast_endpoint *endpoint, const char *file, int line, const char *function, const char *name_fmt, va_list ap)
Create a new channel structure.
Definition: channel.c:739
#define ao2_unlink(container, obj)
Definition: astobj2.h:1598
#define CLI_SHOWUSAGE
Definition: cli.h:45
struct ast_stream_topology * ast_stream_topology_alloc(void)
Create a stream topology.
Definition: stream.c:650
static struct ao2_container * channels
All active channels on the system.
Definition: channel.c:124
void ast_party_name_copy(struct ast_party_name *dest, const struct ast_party_name *src)
Copy the source party name information to the destination party name.
Definition: channel.c:1599
unsigned char number
Definition: channel.h:366
#define AST_CAUSE_NORMAL_UNSPECIFIED
Definition: causes.h:118
int plan
Q.931 Type-Of-Number and Numbering-Plan encoded fields.
Definition: channel.h:294
void ast_channel_named_callgroups_set(struct ast_channel *chan, struct ast_namedgroups *value)
int ast_remaining_ms(struct timeval start, int max_ms)
Calculate remaining milliseconds given a starting timestamp and upper bound.
Definition: main/utils.c:2033
#define AST_CAUSE_CHANNEL_UNACCEPTABLE
Definition: causes.h:101
void ast_channel_publish_varset(struct ast_channel *chan, const char *variable, const char *value)
Publish a ast_channel_varset for a channel.
enum ama_flags ast_channel_string2amaflag(const char *flag)
Convert a string to a detail record AMA flag.
Definition: channel.c:4405
#define AST_PRES_NUMBER_TYPE
Definition: callerid.h:317
int ast_set_write_format_path(struct ast_channel *chan, struct ast_format *core_format, struct ast_format *raw_format)
Set specific write path on channel.
Definition: channel.c:5611
void ast_set_callerid(struct ast_channel *chan, const char *cid_num, const char *cid_name, const char *cid_ani)
Set caller ID number, name and ANI and generate AMI event.
Definition: channel.c:7434
struct ast_bridge_features * ast_channel_feature_hooks_get(struct ast_channel *chan)
Gets the channel-attached features a channel has access to upon being bridged.
Definition: channel.c:11104
void ast_channel_publish_final_snapshot(struct ast_channel *chan)
Send the final channel snapshot for a channel, thus removing it from cache.
#define AST_FRAMEHOOK_INTERFACE_VERSION
Definition: framehook.h:227
void ast_channel_streamid_set(struct ast_channel *chan, int value)
void ast_var_delete(struct ast_var_t *var)
Definition: extconf.c:2473
def info(msg)
int ast_queue_answer(struct ast_channel *chan, const struct ast_stream_topology *topology)
Queue an ANSWER control frame with topology.
Definition: channel.c:1246
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
char * value
Definition: chanvars.h:30
int ani2
Automatic Number Identification 2 (Info Digits)
Definition: channel.h:434
struct ast_channel * ast_channel_masq(const struct ast_channel *chan)
const char * ast_msg_data_get_attribute(struct ast_msg_data *msg, enum ast_msg_data_attribute_type attribute_type)
Get attribute from ast_msg_data.
Definition: message.c:1533
#define AST_TIMING_FD
Definition: channel.h:202
ast_channel_adsicpe
Definition: channel.h:869
int errno
Description of a tone.
Definition: indications.h:35
struct ast_tone_zone_sound * ast_get_indication_tone(const struct ast_tone_zone *zone, const char *indication)
Locate a tone zone sound.
Definition: indications.c:455
#define ao2_callback_data(container, flags, cb_fn, arg, data)
Definition: astobj2.h:1743
struct ast_channel * ast_channel_get_by_exten(const char *exten, const char *context)
Find a channel by extension and context.
Definition: channel.c:1459
#define AST_TRANS_CAP_VIDEO
Definition: transcap.h:40
struct timeval ast_tvadd(struct timeval a, struct timeval b)
Returns the sum of two timevals a + b.
Definition: extconf.c:2283
pthread_t ast_channel_blocker(const struct ast_channel *chan)
Connected Line/Party information.
Definition: channel.h:457
int ast_channel_get_intercept_mode(void)
Am I currently running an intercept dialplan routine.
Definition: channel.c:10430
struct ast_frame * ast_read(struct ast_channel *chan)
Reads a frame.
Definition: channel.c:4302
const ast_string_field name
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)
Definition: channel.c:8444
struct ast_party_dialed * ast_channel_dialed(struct ast_channel *chan)
int ast_write_video(struct ast_channel *chan, struct ast_frame *fr)
Write video frame to a channel This function writes the given frame to the indicated channel...
Definition: channel.c:5062
char * ast_skip_blanks(const char *str)
Gets a pointer to the first non-whitespace character in a string.
Definition: strings.h:157
struct sla_ringing_trunk * first
Definition: app_meetme.c:1092
#define ast_strndup(str, len)
A wrapper for strndup()
Definition: astmm.h:258
void ast_channel_fout_set(struct ast_channel *chan, unsigned int value)
#define AST_CAUSE_WRONG_MESSAGE
Definition: causes.h:138
enum ast_frame_read_action action
void ast_channel_name_to_dial_string(char *channel_name)
Removes the trailing identifiers from a channel name string.
Definition: channel.c:6934
#define ao2_iterator_next(iter)
Definition: astobj2.h:1933
#define FORMAT
#define ao2_alloc(data_size, destructor_fn)
Definition: astobj2.h:411
#define AST_PRES_NUMBER_NOT_AVAILABLE
Definition: callerid.h:353
struct timeval ast_channel_creationtime(struct ast_channel *chan)
ast_channel_error
Definition: channel.h:5020
static void channel_set_external_vars(struct external_vars *channelvars, size_t varc, char **vars)
Definition: channel.c:7967
#define AST_LIST_LAST(head)
Returns the last entry contained in a list.
Definition: linkedlists.h:428
Redirecting Line information. RDNIS (Redirecting Directory Number Information Service) Where a call d...
Definition: channel.h:523
#define LOG_NOTICE
Definition: logger.h:263
static void __ast_change_name_nolink(struct ast_channel *chan, const char *newname)
this function simply changes the name of the channel and issues a manager_event with out unlinking an...
Definition: channel.c:6839
int ast_app_run_sub(struct ast_channel *autoservice_chan, struct ast_channel *sub_chan, const char *sub_location, const char *sub_args, int ignore_hangup)
Run a subroutine on a channel, placing an optional second channel into autoservice.
Definition: main/app.c:401
const char * ast_format_cap_get_names(const struct ast_format_cap *cap, struct ast_str **buf)
Get the names of codecs of a set of formats.
Definition: format_cap.c:736
char * ast_trim_blanks(char *str)
Trims trailing whitespace characters from a string.
Definition: strings.h:182
struct ast_frame * ast_framehook_list_write_event(struct ast_framehook_list *framehooks, struct ast_frame *frame)
This is used by the channel API push a frame write event to a channel&#39;s framehook list...
Definition: framehook.c:313
int ast_seekstream(struct ast_filestream *fs, off_t sample_offset, int whence)
Seeks into stream.
Definition: file.c:1038
const char * ast_channel_peeraccount(const struct ast_channel *chan)
void ast_channel_end_dtmf(struct ast_channel *chan, char digit, struct timeval start, const char *why)
Simulate a DTMF end on a broken bridge channel.
Definition: channel.c:11070
const char * ast_var_full_name(const struct ast_var_t *var)
Definition: chanvars.c:75
#define AST_LIST_TRAVERSE(head, var, field)
Loops over (traverses) the entries in a list.
Definition: linkedlists.h:490
enum ast_channel_error ast_channel_errno(void)
Get error code for latest channel operation.
Definition: channel.c:11162
#define AST_LIST_ENTRY(type)
Declare a forward link structure inside a list entry.
Definition: linkedlists.h:409
int ast_tonepair(struct ast_channel *chan, int freq1, int freq2, int duration, int vol)
Definition: channel.c:7700
void(* digit)(struct ast_channel *chan, char digit)
Definition: channel.h:236
long int flag
Definition: f2c.h:83
#define AST_LIST_INSERT_HEAD(head, elm, field)
Inserts a list entry at the head of a list.
Definition: linkedlists.h:710
char * str
a string value for the redirecting reason
Definition: channel.h:508
int ast_channel_feature_hooks_append(struct ast_channel *chan, struct ast_bridge_features *features)
Appends to the channel-attached features a channel has access to upon being bridged.
Definition: channel.c:11152
int ast_channel_cc_params_init(struct ast_channel *chan, const struct ast_cc_config_params *base_params)
Set up datastore with CCSS parameters for a channel.
Definition: channel.c:10652
#define ast_opt_generic_plc_on_equal_codecs
Definition: options.h:136
#define ast_channel_unlock(chan)
Definition: channel.h:2946
int ast_senddigit_external(struct ast_channel *chan, char digit, unsigned int duration)
Send a DTMF digit to a channel from an external thread.
Definition: channel.c:5032
int ast_write(struct ast_channel *chan, struct ast_frame *fr)
Write a frame to a channel This function writes the given frame to the indicated channel.
Definition: channel.c:5189
#define CLI_FAILURE
Definition: cli.h:46
A machine to gather up arbitrary frames and convert them to raw slinear on demand.
struct ast_channel * ast_channel_bridge_peer(struct ast_channel *chan)
Get the channel&#39;s bridge peer only if the bridge is two-party.
Definition: channel.c:10765
struct ast_channel_iterator * ast_channel_iterator_by_exten_new(const char *exten, const char *context)
Create a new channel iterator based on extension.
Definition: channel.c:1368
#define SEEK_FORCECUR
Definition: file.h:51
struct ast_frame *(*const exception)(struct ast_channel *chan)
Handle an exception, reading a frame.
Definition: channel.h:766
static const char name[]
Definition: cdr_mysql.c:74
struct ast_hangup_handler_list * ast_channel_hangup_handlers(struct ast_channel *chan)
int source
Information about the source of an update.
Definition: channel.h:483
void ast_party_subaddress_set_init(struct ast_party_subaddress *init, const struct ast_party_subaddress *guide)
Initialize the given party subaddress structure using the given guide for a set update operation...
Definition: channel.c:1719
#define ast_free(a)
Definition: astmm.h:182
char * command
Definition: cli.h:186
int valid
valid/present ie
Definition: channel.c:8575
#define AST_CAUSE_BEARERCAPABILITY_NOTIMPL
Definition: causes.h:130
int framehook_id
Definition: channel.c:10923
void ast_party_id_set_init(struct ast_party_id *init, const struct ast_party_id *guide)
Initialize the given party id structure using the given guide for a set update operation.
Definition: channel.c:1780
#define ast_calloc(num, len)
A wrapper for calloc()
Definition: astmm.h:204
static char * complete_channeltypes(struct ast_cli_args *a)
Definition: channel.c:303
void ast_autochan_new_channel(struct ast_channel *old_chan, struct ast_channel *new_chan)
Switch what channel autochans point to.
Definition: autochan.c:86
size_t num_samples
Definition: channel.c:5082
void ast_party_name_set_init(struct ast_party_name *init, const struct ast_party_name *guide)
Initialize the given party name structure using the given guide for a set update operation.
Definition: channel.c:1613
#define AST_OPTION_SECURE_SIGNALING
struct ast_var_t::@249 entries
int ast_channel_setoption(struct ast_channel *chan, int option, void *data, int datalen, int block)
Sets an option on a channel.
Definition: channel.c:7522
int valid
valid/present ie
Definition: channel.c:8501
#define AST_CHANNEL_NAME
Definition: channel.h:172
struct ast_channel * ast_channel_get_by_name(const char *name)
Find a channel by name.
Definition: channel.c:1454
static int safe_sleep_conditional(struct ast_channel *chan, int timeout_ms, int(*cond)(void *), void *data, unsigned int generate_silence)
Wait, look for hangups and condition arg.
Definition: channel.c:1498
int str
subaddress ie.
Definition: channel.c:8569
static int ast_channel_by_name_cb(void *obj, void *arg, void *data, int flags)
Definition: channel.c:1284
static int tonepair_generator(struct ast_channel *chan, void *data, int len, int samples)
Definition: channel.c:7629
#define AST_FLAGS_ALL
Definition: utils.h:196
#define ast_var_assign(name, value)
Definition: chanvars.h:40
void ast_bridge_notify_masquerade(struct ast_channel *chan)
Notify bridging that this channel was just masqueraded.
Definition: bridge.c:1482
#define CHANNEL_DEADLOCK_AVOIDANCE(chan)
Definition: lock.h:352
int ast_closestream(struct ast_filestream *f)
Closes a stream.
Definition: file.c:1068
int __ast_answer(struct ast_channel *chan, unsigned int delay)
Answer a channel, with a selectable delay before returning.
Definition: channel.c:2704
int ast_channel_stream_topology_changed_externally(struct ast_channel *chan)
Provide notice from a channel that the topology has changed on it as a result of the remote party ren...
Definition: channel.c:11209
struct ast_filestream * ast_channel_stream(const struct ast_channel *chan)
static void generator_write_format_change(struct ast_channel *chan)
Definition: channel.c:2913
int ast_is_deferrable_frame(const struct ast_frame *frame)
Should we keep this frame for later?
Definition: channel.c:1467
#define AST_CAUSE_NUMBER_CHANGED
Definition: causes.h:111
struct ast_bridge_channel * ast_channel_get_bridge_channel(struct ast_channel *chan)
Get a reference to the channel&#39;s bridge pointer.
Definition: channel.c:10783
int ast_prod(struct ast_channel *chan)
Send empty audio to prime a channel driver.
Definition: channel.c:5045
void ast_party_number_init(struct ast_party_number *init)
Initialize the given number structure.
Definition: channel.c:1644
static void(* ast_moh_stop_ptr)(struct ast_channel *)
Definition: channel.c:7847
int ast_channel_sendurl(struct ast_channel *chan, const char *url)
Sends a URL on a given link Send URL on link.
Definition: channel.c:6732
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)
Definition: channel.c:8592
const char * word
Definition: cli.h:163
#define AST_RWLIST_REMOVE_HEAD
Definition: linkedlists.h:843
const struct ast_channel_tech * ast_get_channel_tech(const char *name)
Get handle to channel driver based on name.
Definition: channel.c:592
Prototypes for public functions only of internal interest,.
int ast_channel_has_audio_frame_or_monitor(struct ast_channel *chan)
Check if the channel has active audiohooks, active framehooks, or a monitor.
Definition: channel.c:2523
#define AST_HTML_URL
#define FRAMECOUNT_INC(x)
Definition: channel.h:861
struct ao2_iterator * active_iterator
Definition: channel.c:1357
void ast_channel_softhangup_withcause_locked(struct ast_channel *chan, int causecode)
Lock the given channel, then request softhangup on the channel with the given causecode.
Definition: channel.c:468
int ast_senddigit(struct ast_channel *chan, char digit, unsigned int duration)
Send a DTMF digit to a channel.
Definition: channel.c:5019
struct ast_frame * ast_read_stream_noaudio(struct ast_channel *chan)
Reads a frame, but does not filter to just the default streams, returning AST_FRAME_NULL frame if aud...
Definition: channel.c:4317
#define DATASTORE_INHERIT_FOREVER
Definition: channel.h:193
const char * ast_channel_hangupsource(const struct ast_channel *chan)
#define AST_CAUSE_MANDATORY_IE_LENGTH_ERROR
Definition: causes.h:143
void ast_channel_hangupcause_hash_set(struct ast_channel *chan, const struct ast_control_pvt_cause_code *cause_code, int datalen)
Sets the HANGUPCAUSE hash and optionally the SIP_CAUSE hash on the given channel. ...
Definition: channel.c:4391
#define ast_frisolate(fr)
Makes a frame independent of any static storage.
struct ast_autochan_list * ast_channel_autochans(struct ast_channel *chan)
#define ao2_find(container, arg, flags)
Definition: astobj2.h:1756
void * ast_threadstorage_get_ptr(struct ast_threadstorage *ts)
Retrieve a raw pointer from threadstorage.
void ast_channel_timingfd_set(struct ast_channel *chan, int value)
unsigned int inheritance
Definition: datastore.h:73
void ast_cc_copy_config_params(struct ast_cc_config_params *dest, const struct ast_cc_config_params *src)
copy CCSS configuration parameters from one structure to another
Definition: ccss.c:861
static int namedgroup_cmp_cb(void *obj, void *arg, int flags)
Comparison function used for named group container.
Definition: channel.c:7759
static int party_id_build_data(unsigned char *data, size_t datalen, const struct ast_party_id *id, const char *label, const struct ast_party_id_ies *ies, const struct ast_set_party_id *update)
Definition: channel.c:8673
void ast_deactivate_generator(struct ast_channel *chan)
Definition: channel.c:2902
int ast_timer_fd(const struct ast_timer *handle)
Get a poll()-able file descriptor for a timer.
Definition: timing.c:161
int ast_timer_set_rate(const struct ast_timer *handle, unsigned int rate)
Set the timing tick rate.
Definition: timing.c:166
static void channels_shutdown(void)
Definition: channel.c:8095
static void tonepair_release(struct ast_channel *chan, void *params)
Definition: channel.c:7587
#define AST_CAUSE_INTERWORKING
Definition: causes.h:145
struct ast_party_redirecting_reason reason
Reason for the redirection.
Definition: channel.h:543
struct ast_party_subaddress subaddress
Dialed/Called subaddress.
Definition: channel.h:392
static int generator_force(const void *data)
Definition: channel.c:2925
int ast_stream_topology_get_count(const struct ast_stream_topology *topology)
Get the number of streams in a topology.
Definition: stream.c:765
int ast_timer_enable_continuous(const struct ast_timer *handle)
Enable continuous mode.
Definition: timing.c:176
int ast_framehook_list_contains_no_active(struct ast_framehook_list *framehooks)
Determine if a framehook list is free of active framehooks or not.
Definition: framehook.c:282
struct stasis_message_type * ast_channel_dtmf_begin_type(void)
Message type for when DTMF begins on a channel.
enum ast_channel_error ast_channel_internal_errno(void)
void ast_party_redirecting_copy(struct ast_party_redirecting *dest, const struct ast_party_redirecting *src)
Copy the source redirecting information to the destination redirecting.
Definition: channel.c:2135
void ast_channel_update_redirecting(struct ast_channel *chan, const struct ast_party_redirecting *redirecting, const struct ast_set_party_redirecting *update)
Indicate that the redirecting id has changed.
Definition: channel.c:10379
int64_t ast_channel_get_up_time_ms(struct ast_channel *chan)
Obtain how long it has been since the channel was answered in ms.
Definition: channel.c:2844
#define ast_clear_flag(p, flag)
Definition: utils.h:77
int ast_channel_cmpwhentohangup_tv(struct ast_channel *chan, struct timeval offset)
Compare a offset with when to hangup channel.
Definition: channel.c:523
struct ast_party_redirecting * ast_channel_redirecting(struct ast_channel *chan)
char * ast_print_namedgroups(struct ast_str **buf, struct ast_namedgroups *group)
Print named call groups and named pickup groups.
Definition: channel.c:8158
static char cid_name[AST_MAX_EXTENSION]
Definition: chan_mgcp.c:165
struct ast_frame *(*const read)(struct ast_channel *chan)
Read a frame (or chain of frames from the same stream), in standard format (see frame.h)
Definition: channel.h:735
void ast_channel_callid_set(struct ast_channel *chan, ast_callid value)
Indicate what information in ast_party_connected_line should be set.
Definition: channel.h:490
static void send_text(unsigned char pos, unsigned char inverse, struct unistimsession *pte, const char *text)
#define CHECK_BLOCKING(c)
Set the blocking indication on the channel.
Definition: channel.h:2894
int ast_raw_answer(struct ast_channel *chan)
Answer a channel.
Definition: channel.c:2699
int ast_pbx_hangup_handler_run(struct ast_channel *chan)
Run all hangup handlers on the channel.
int ast_senddigit_mf_end(struct ast_channel *chan)
End sending an MF digit to a channel.
Definition: channel.c:4988
const char * usage
Definition: cli.h:177
static int replace(struct ast_channel *chan, const char *cmd, char *data, struct ast_str **buf, ssize_t len)
Definition: func_strings.c:790
void ast_channel_exten_set(struct ast_channel *chan, const char *value)
struct ast_format_cap * ast_channel_nativeformats(const struct ast_channel *chan)
struct varshead * ast_channel_get_manager_vars(struct ast_channel *chan)
Gets the variables for a given channel, as specified by ast_channel_set_manager_vars().
Definition: channel.c:8085
void ast_party_id_invalidate(struct ast_party_id *id)
Invalidate all components of the given party id.
Definition: channel.c:1889
const char * desc
Definition: channel.c:133
int pbx_builtin_setvar_helper(struct ast_channel *chan, const char *name, const char *value)
Add a variable to the channel variable stack, removing the most recently set value for the same name...
int ast_pre_call(struct ast_channel *chan, const char *sub_args)
Execute a Gosub call on the channel before a call is placed.
Definition: channel.c:6536
#define AST_CAUSE_INVALID_IE_CONTENTS
Definition: causes.h:140
struct ast_set_party_id orig
Definition: channel.h:558
void ast_channel_sending_dtmf_tv_set(struct ast_channel *chan, struct timeval value)
char ast_channel_dtmf_digit_to_emulate(const struct ast_channel *chan)
#define AST_PRES_RESTRICTION
Definition: callerid.h:323
struct ast_bridge_features * ast_bridge_features_new(void)
Allocate a new bridge features struct.
Definition: bridge.c:3750
static int channel_has_external_vars(struct external_vars *channelvars)
Definition: channel.c:7946
struct ast_frame ast_null_frame
Definition: main/frame.c:79
struct timeval ast_channel_sending_dtmf_tv(const struct ast_channel *chan)
static void channel_cc_params_destroy(void *data)
Definition: channel.c:10640
void ast_channel_creationtime_set(struct ast_channel *chan, struct timeval *value)
int combined_presentation
Combined name and number presentation ie.
Definition: channel.c:8655
int ast_channel_is_leaving_bridge(struct ast_channel *chan)
Determine if a channel is leaving a bridge, but not hung up.
Definition: channel.c:10751
#define ast_channel_lock_both(chan1, chan2)
Lock two channels.
Definition: channel.h:2952
FrameHook Architecture.
#define CLI_SUCCESS
Definition: cli.h:44
struct ast_channel * ast_channel_get_by_name_prefix(const char *name, size_t name_len)
Find a channel by a name prefix.
Definition: channel.c:1434
#define LOG_DTMF
Definition: logger.h:307
char * tag
User-set "tag".
Definition: channel.h:355
int ast_channel_fd(const struct ast_channel *chan, int which)
struct ast_set_party_id id
Definition: channel.h:492
int ast_set_read_format_path(struct ast_channel *chan, struct ast_format *raw_format, struct ast_format *core_format)
Set specific read path on channel.
Definition: channel.c:5575
int type
Q.931 subaddress type.
Definition: channel.h:321
int ast_channel_softhangup_internal_flag(struct ast_channel *chan)
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)
Requests a channel.
Definition: channel.c:6444
void * data
Definition: datastore.h:70
#define AST_RWLIST_INSERT_TAIL
Definition: linkedlists.h:740
struct ast_set_party_id priv
Definition: channel.h:447
#define AST_CAUSE_REQUESTED_CHAN_UNAVAIL
Definition: causes.h:124
int transit_network_select
Transit Network Select.
Definition: channel.h:398
#define AST_LIST_HEAD_INIT_NOLOCK(head)
Initializes a list head structure.
Definition: linkedlists.h:680
int ast_waitstream_full(struct ast_channel *c, const char *breakon, int audiofd, int monfd)
Definition: file.c:1785
char * ast_transfercapability2str(int transfercapability)
Gives the string form of a given transfer capability.
Definition: channel.c:678
#define AST_CAUSE_PRE_EMPTED
Definition: causes.h:103
int ast_channel_defer_dtmf(struct ast_channel *chan)
Set defer DTMF flag on channel.
Definition: channel.c:1257
struct ast_format * origwfmt
Definition: channel.c:7578
Structure that contains information regarding a channel in a bridge.
char * strsep(char **str, const char *delims)
struct ast_set_party_id to
Definition: channel.h:562
#define AST_CAUSE_NO_ROUTE_DESTINATION
Definition: causes.h:99
static void send_dtmf_end_event(struct ast_channel *chan, enum DtmfDirection direction, const char digit, long duration_ms)
Definition: channel.c:3394
int ast_channel_dialed_causes_add(const struct ast_channel *chan, const struct ast_control_pvt_cause_code *cause_code, int datalen)
Add cause code information to the channel.
int ast_max_forwards_decrement(struct ast_channel *chan)
Decrement the max forwards count for a particular channel.
Definition: max_forwards.c:135
static const struct ast_datastore_info plc_ds_info
Definition: channel.c:5093
unsigned char name
Definition: channel.h:364
static void(* ast_moh_cleanup_ptr)(struct ast_channel *)
Definition: channel.c:7848
struct ast_channel_iterator * ast_channel_iterator_by_name_new(const char *name, size_t name_len)
Create a new channel iterator based on name.
Definition: channel.c:1388
FILE * out
Definition: utils/frame.c:33
int ast_channel_internal_is_finalized(struct ast_channel *chan)
#define ast_channel_ref(c)
Increase channel reference count.
Definition: channel.h:2970
#define ao2_replace(dst, src)
Definition: astobj2.h:517
static int chancount
Definition: channel.c:93
int ast_channel_is_bridged(const struct ast_channel *chan)
Determine if a channel is in a bridge.
Definition: channel.c:10746
Indicate what information in ast_party_redirecting should be set.
Definition: channel.h:556
When we need to walk through a container, we use an ao2_iterator to keep track of the current positio...
Definition: astobj2.h:1841
void ast_party_redirecting_reason_copy(struct ast_party_redirecting_reason *dest, const struct ast_party_redirecting_reason *src)
Copy the source redirecting reason information to the destination redirecting reason.
Definition: channel.c:2085
#define ao2_cleanup(obj)
Definition: astobj2.h:1958
int count
Number of times the call was redirected.
Definition: channel.h:549
Standard Command Line Interface.
struct ast_channel * __ast_channel_alloc(int needqueue, int state, const char *cid_num, const char *cid_name, const char *acctcode, const char *exten, const char *context, const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor, enum ama_flags amaflag, struct ast_endpoint *endpoint, const char *file, int line, const char *function, const char *name_fmt,...)
Create a channel structure.
Definition: channel.c:960
int ast_channel_datastore_add(struct ast_channel *chan, struct ast_datastore *datastore)
Add a datastore to a channel.
Definition: channel.c:2390
struct ast_namedgroups * ast_ref_namedgroups(struct ast_namedgroups *groups)
Definition: channel.c:7838
int ast_channel_queryoption(struct ast_channel *chan, int option, void *data, int *datalen, int block)
Checks the value of an option.
Definition: channel.c:7542
#define SCOPE_TRACE(level,...)
Print a trace message with details when a scope is entered or existed.
Definition: logger.h:749
int ast_channel_hangupcause(const struct ast_channel *chan)
void ast_channel_context_set(struct ast_channel *chan, const char *value)
int cause
Definition: channel.c:131
struct ast_party_id to
Call is redirecting to a new party (Sent to the caller)
Definition: channel.h:531
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Definition: strings.h:401
static int ast_channel_make_compatible_helper(struct ast_channel *from, struct ast_channel *to)
Set up translation from one channel to another.
Definition: channel.c:6738
#define S_OR(a, b)
returns the equivalent of logic or for strings: first one if not empty, otherwise second one...
Definition: strings.h:79
int type
subaddress type ie
Definition: channel.c:8571
void ast_channel_set_manager_vars(size_t varc, char **vars)
Sets the variables to be stored in the manager_vars field of all snapshots.
Definition: channel.c:7989
struct ast_set_party_id ani
Definition: channel.h:494
const char * ast_channel_name(const struct ast_channel *chan)
#define AST_CAUSE_IE_NONEXIST
Definition: causes.h:139
int ast_writestream(struct ast_filestream *fs, struct ast_frame *f)
Writes a frame to a stream.
Definition: file.c:209
void(* set_rawformat)(struct ast_channel *chan, struct ast_format *format)
Definition: channel.c:5654
int ast_channel_fd_count(const struct ast_channel *chan)
Retrieve the number of file decriptor positions present on the channel.
const int pos
Definition: cli.h:164
const char * context
Definition: channel.h:1106
static void call_forward_inherit(struct ast_channel *new_chan, struct ast_channel *parent, struct ast_channel *orig)
Definition: channel.c:5959
#define AST_CAUSE_USER_BUSY
Definition: causes.h:106
void ast_party_dialed_free(struct ast_party_dialed *doomed)
Destroy the dialed party contents.
Definition: channel.c:1971
void ast_channel_state_set(struct ast_channel *chan, enum ast_channel_state)
int ast_waitfor(struct ast_channel *c, int ms)
Wait for input on a channel.
Definition: channel.c:3171
int ast_channel_alert_writable(struct ast_channel *chan)
static const struct ast_channel_tech null_tech
Definition: channel.c:708
void * ast_channel_music_state(const struct ast_channel *chan)
Information needed to specify a number in a call.
Definition: channel.h:290
void ast_pbx_hangup_handler_destroy(struct ast_channel *chan)
Destroy the hangup handler container on a channel.
static ENTRY retval
Definition: hsearch.c:50
unsigned int ast_format_get_sample_rate(const struct ast_format *format)
Get the sample rate of a media format.
Definition: format.c:379
#define ast_frfree(fr)
int ast_redirecting_build_data(unsigned char *data, size_t datalen, const struct ast_party_redirecting *redirecting, const struct ast_set_party_redirecting *update)
Build the redirecting id data frame.
Definition: channel.c:9363
#define AST_PRES_ALLOWED
Definition: callerid.h:324
static PGresult * result
Definition: cel_pgsql.c:88
void ast_channel_connected_set(struct ast_channel *chan, struct ast_party_connected_line *value)
#define AST_OPTION_FORMAT_READ
void ast_hangup(struct ast_channel *chan)
Hangup a channel.
Definition: channel.c:2548
struct ast_set_party_id priv_to
Definition: channel.h:568
void ast_channel_sched_set(struct ast_channel *chan, struct ast_sched_context *value)
#define AST_CAUSE_BUSY
Definition: causes.h:148
unsigned long global_fout
Definition: channel.c:95
void ast_cdr_free(struct ast_cdr *cdr)
Free a CDR record.
Definition: cdr.c:3411
struct stasis_forward * sub
Definition: res_corosync.c:240
Data structure associated with a single frame of data.
void ast_channel_emulate_dtmf_duration_set(struct ast_channel *chan, unsigned int value)
int ast_app_exec_sub(struct ast_channel *autoservice_chan, struct ast_channel *sub_chan, const char *sub_args, int ignore_hangup)
Run a subroutine on a channel, placing an optional second channel into autoservice.
Definition: main/app.c:370
Internal Asterisk hangup causes.
void ast_channel_set_caller_event(struct ast_channel *chan, const struct ast_party_caller *caller, const struct ast_set_party_caller *update)
Set the caller id information in the Asterisk channel and generate an AMI event if the caller id name...
Definition: channel.c:7472
static int force_inline attribute_pure ast_begins_with(const char *str, const char *prefix)
Definition: strings.h:94
void ast_channel_softhangup_internal_flag_add(struct ast_channel *chan, int value)
void ast_party_number_set(struct ast_party_number *dest, const struct ast_party_number *src)
Set the source party number information into the destination party number.
Definition: channel.c:1674
const char * ast_channel_language(const struct ast_channel *chan)
int ast_channel_redirecting_macro(struct ast_channel *autoservice_chan, struct ast_channel *macro_chan, const void *redirecting_info, int is_caller, int is_frame)
Run a redirecting interception macro and update a channel&#39;s redirecting information.
Definition: channel.c:10487
static const struct ast_datastore_info bridge_features_info
Definition: channel.c:11099
static void ast_channel_destructor(void *obj)
Free a channel structure.
Definition: channel.c:2192
Abstract JSON element (object, array, string, int, ...).
int ast_playtones_start(struct ast_channel *chan, int vol, const char *tonelist, int interruptible)
Start playing a list of tones on a channel.
Definition: indications.c:302
struct ast_flags * ast_channel_snapshot_segment_flags(struct ast_channel *chan)
int(*const send_text_data)(struct ast_channel *chan, struct ast_msg_data *data)
Display or transmit text with data.
Definition: channel.h:845
const struct ast_channel_tech ast_kill_tech
Kill the channel channel driver technology descriptor.
Definition: channel.c:434
int ast_channel_unbridged(struct ast_channel *chan)
This function will check if the bridge needs to be re-evaluated due to external changes.
#define AST_JITTERBUFFER_FD
Definition: channel.h:205
int(*const queryoption)(struct ast_channel *chan, int option, void *data, int *datalen)
Query a given option. Called with chan locked.
Definition: channel.h:781
static struct ast_frame * __ast_read(struct ast_channel *chan, int dropaudio, int dropnondefault)
Definition: channel.c:3529
#define AST_TRANS_CAP_RESTRICTED_DIGITAL
Definition: transcap.h:36
struct ast_channel * ast_channel_yank(struct ast_channel *yankee)
Gain control of a channel in the system.
Definition: channel.c:10794
int plc_rx(plc_state_t *s, int16_t amp[], int len)
Process a block of received audio samples.
Definition: plc.c:132
struct ast_channel_id uniqueid
SAY_EXTERN int(* ast_say_character_str_full)(struct ast_channel *chan, const char *num, const char *ints, const char *lang, enum ast_say_case_sensitivity sensitivity, int audiofd, int ctrlfd) SAY_INIT(ast_say_character_str_full)
Definition: say.h:175
static struct test_val b
int ast_stasis_channels_init(void)
Initialize the stasis channel topic and message types.
Definition: search.h:40
const char * ast_channel_context(const struct ast_channel *chan)
struct timeval ast_tvsub(struct timeval a, struct timeval b)
Returns the difference of two timevals a - b.
Definition: extconf.c:2298
const char * ast_cause2str(int cause)
Gives the string form of a given hangup cause.
Definition: channel.c:612
const char * data
Description of a tone.
Definition: indications.h:52
void ast_channel_publish_snapshot(struct ast_channel *chan)
Publish a ast_channel_snapshot for a channel.
void ast_party_id_init(struct ast_party_id *init)
Initialize the given party id structure.
Definition: channel.c:1757
#define ast_datastore_alloc(info, uid)
Definition: datastore.h:89
unsigned long ast_channel_outsmpl(const struct ast_channel *chan)
union ast_frame::@263 data
ast_media_type
Types of media.
Definition: codec.h:30
static char * handle_cli_core_show_channeltypes(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
Show channel types - CLI command.
Definition: channel.c:261
int ast_channel_register(const struct ast_channel_tech *tech)
Register a new telephony channel in Asterisk.
Definition: channel.c:539
#define AST_LIST_TRAVERSE_SAFE_BEGIN(head, var, field)
Loops safely over (traverses) the entries in a list.
Definition: linkedlists.h:528
enum ast_frame_type frametype
int ast_transfer_protocol(struct ast_channel *chan, char *dest, int *protocol)
Transfer a call to dest, if the channel supports transfer.
Definition: channel.c:6595
int ast_format_cap_empty(const struct ast_format_cap *cap)
Determine if a format cap has no formats in it.
Definition: format_cap.c:746
void ast_softhangup_all(void)
Soft hangup all active channels.
Definition: channel.c:493
#define AST_PRES_UNAVAILABLE
Definition: callerid.h:326
int int32_t
Definition: db.h:60
unsigned int direction
Definition: channel.c:10922
Generic container type.
int ast_format_cap_append_from_cap(struct ast_format_cap *dst, const struct ast_format_cap *src, enum ast_media_type type)
Append the formats of provided type in src to dst.
Definition: format_cap.c:269
#define ast_channel_trylock(chan)
Definition: channel.h:2947
ama_flags
Channel AMA Flags.
Definition: channel.h:1178
int ast_channel_request_stream_topology_change(struct ast_channel *chan, struct ast_stream_topology *topology, void *change_source)
Request that the stream topology of a channel change.
Definition: channel.c:11167
unsigned char valid
TRUE if the name information is valid/present.
Definition: channel.h:280
int ast_say_digits_full(struct ast_channel *chan, int num, const char *ints, const char *lang, int audiofd, int ctrlfd)
Same as ast_say_digits() with audiofd for received audio and returns 1 on ctrlfd being readable...
Definition: channel.c:8379
static struct test_options options
int ast_app_group_discard(struct ast_channel *chan)
Discard all group counting for a channel.
Definition: main/app.c:2162
int ast_safe_sleep_without_silence(struct ast_channel *chan, int ms)
Wait for a specified amount of time, looking for hangups, and do not generate silence.
Definition: channel.c:1579
void ast_channel_publish_blob(struct ast_channel *chan, struct stasis_message_type *type, struct ast_json *blob)
Publish a channel blob message.
static char context[AST_MAX_CONTEXT]
Definition: chan_alsa.c:116
#define AST_TRANS_CAP_SPEECH
Definition: transcap.h:34
Call Parking and Pickup API Includes code and algorithms from the Zapata library. ...
struct ast_flags * ast_channel_flags(struct ast_channel *chan)
Information needed to specify a subaddress in a call.
Definition: channel.h:308
struct ast_trans_pvt *(* get_trans)(const struct ast_channel *chan)
Definition: channel.c:5649
static void * silence_generator_alloc(struct ast_channel *chan, void *data)
Definition: channel.c:8226
static int kill_write(struct ast_channel *chan, struct ast_frame *frame)
Definition: channel.c:407
int ast_senddigit_mf(struct ast_channel *chan, char digit, unsigned int duration, unsigned int durationkp, unsigned int durationst, int is_external)
Send an MF digit to a channel.
Definition: channel.c:4997
void ast_channel_internal_fd_set(struct ast_channel *chan, int which, int value)
void ast_channel_hold_state_set(struct ast_channel *chan, int value)
void ast_channel_internal_swap_snapshots(struct ast_channel *a, struct ast_channel *b)
Swap snapshots beteween two channels.
struct ast_format * format
int ast_channel_move(struct ast_channel *dest, struct ast_channel *source)
Move a channel from its current location to a new location.
Definition: channel.c:10867
int ast_waitfordigit(struct ast_channel *c, int ms)
Waits for a digit.
Definition: channel.c:3184
void ast_channel_fin_set(struct ast_channel *chan, unsigned int value)
static void free_external_channelvars(struct external_vars *channelvars)
Definition: channel.c:7936
int ast_devstate_changed_literal(enum ast_device_state state, enum ast_devstate_cache cachable, const char *device)
Tells Asterisk the State for Device is changed.
Definition: devicestate.c:471
#define DEFAULT_EMULATE_MF_DURATION
Definition: channel.c:106
static void queue_dtmf_readq(struct ast_channel *chan, struct ast_frame *f)
Definition: channel.c:3470
enum ast_timer_event ast_timer_get_event(const struct ast_timer *handle)
Retrieve timing event.
Definition: timing.c:186
struct ast_audiohook_list * ast_channel_audiohooks(const struct ast_channel *chan)
int ast_str2cause(const char *name)
Convert a symbolic hangup cause to number.
Definition: channel.c:625
void ast_stream_topology_free(struct ast_stream_topology *topology)
Unreference and destroy a stream topology.
Definition: stream.c:743
int ast_cli_completion_add(char *value)
Add a result to a request for completion options.
Definition: main/cli.c:2726
const char * ast_channel_macrocontext(const struct ast_channel *chan)
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)
Build the connected line information data frame.
Definition: channel.c:8793
const char * ast_timer_get_name(const struct ast_timer *handle)
Get name of timer in use.
Definition: timing.c:196
struct ast_format * old_write_format
Definition: channel.c:8263
int ast_stream_get_position(const struct ast_stream *stream)
Get the position of the stream in the topology.
Definition: stream.c:500
static char url[512]
void ast_channel_name_set(struct ast_channel *chan, const char *name)
Set the channel name.
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)
Requests a channel (specifying stream topology)
Definition: channel.c:6449
struct ast_channel * __ast_channel_internal_alloc(void(*destructor)(void *obj), const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor, const char *file, int line, const char *function)
struct stasis_message_type * ast_channel_hangup_request_type(void)
Message type for when a hangup is requested on a channel.
Say numbers and dates (maybe words one day too)
int ast_channel_internal_setup_topics(struct ast_channel *chan)
unsigned int ast_translate_path_steps(struct ast_format *dest, struct ast_format *src)
Returns the number of steps required to convert from &#39;src&#39; to &#39;dest&#39;.
Definition: translate.c:1491
void ast_channel_set_ari_vars(size_t varc, char **vars)
Sets the variables to be stored in the ari_vars field of all snapshots.
Definition: channel.c:7994
void ast_channel_set_redirecting(struct ast_channel *chan, const struct ast_party_redirecting *redirecting, const struct ast_set_party_redirecting *update)
Set the redirecting id information in the Asterisk channel.
Definition: channel.c:9215
#define AST_CAUSE_CALL_REJECTED
Definition: causes.h:110
direction
void ast_channel_priority_set(struct ast_channel *chan, int value)
Bridging API.
static struct ast_timer * timer
Definition: chan_iax2.c:360
#define ast_channel_alloc(needqueue, state, cid_num, cid_name, acctcode, exten, context, assignedids, requestor, amaflag,...)
Create a channel structure.
Definition: channel.h:1259
struct ast_format * ast_format_t140
Built-in cached t140 format.
Definition: format_cache.c:236
struct ast_party_connected_line * ast_channel_connected_indicated(struct ast_channel *chan)
struct ast_format * ast_format_slin
Built-in cached signed linear 8kHz format.
Definition: format_cache.c:41
#define AST_CAUSE_BEARERCAPABILITY_NOTAVAIL
Definition: causes.h:129
int ast_set_write_format(struct ast_channel *chan, struct ast_format *format)
Sets write format on channel chan.
Definition: channel.c:5890
struct ast_format * ast_channel_writeformat(struct ast_channel *chan)
struct ast_channel_monitor * ast_channel_monitor(const struct ast_channel *chan)
void ast_channel_internal_finalize(struct ast_channel *chan)
int ast_undestroyed_channels(void)
Definition: channel.c:504
SAY_EXTERN int(* ast_say_phonetic_str_full)(struct ast_channel *chan, const char *num, const char *ints, const char *lang, int audiofd, int ctrlfd) SAY_INIT(ast_say_phonetic_str_full)
Definition: say.h:180
ast_timing_func_t ast_channel_timingfunc(const struct ast_channel *chan)
struct stasis_message_type * ast_channel_masquerade_type(void)
Message type for when a channel is being masqueraded.
struct ast_frame * ast_channel_dtmff(struct ast_channel *chan)
static int namedgroup_match(void *obj, void *arg, int flags)
Definition: channel.c:8182
static snd_pcm_format_t format
Definition: chan_alsa.c:102
struct ast_channel * ast_channel_release(struct ast_channel *chan)
Unlink and release reference to a channel.
Definition: channel.c:1584
int ast_func_write(struct ast_channel *chan, const char *function, const char *value)
executes a write operation on a function
void ast_channel_set_is_t38_active_nolock(struct ast_channel *chan, int is_t38_active)
Variant of ast_channel_set_is_t38_active. Use this if the channel is already locked prior to calling...
#define AST_CAUSE_MESSAGE_TYPE_NONEXIST
Definition: causes.h:137
static void prnt_channel_key(void *v_obj, void *where, ao2_prnt_fn *prnt)
Definition: channel.c:7912
static void suppress_datastore_destroy_cb(void *data)
Definition: channel.c:10905
enum ama_flags ast_channel_amaflags(const struct ast_channel *chan)
Information needed to specify a name in a call.
Definition: channel.h:263
int(*const indicate)(struct ast_channel *c, int condition, const void *data, size_t datalen)
Indicate a particular condition (e.g. AST_CONTROL_BUSY or AST_CONTROL_RINGING or AST_CONTROL_CONGESTI...
Definition: channel.h:772
void ast_channel_tech_pvt_set(struct ast_channel *chan, void *value)
enum ast_bridge_result(*const early_bridge)(struct ast_channel *c0, struct ast_channel *c1)
Bridge two channels of the same type together (early)
Definition: channel.h:769
Application convenience functions, designed to give consistent look and feel to Asterisk apps...
void ast_connected_line_copy_from_caller(struct ast_party_connected_line *dest, const struct ast_party_caller *src)
Copy the caller information to the connected line information.
Definition: channel.c:8389
struct ast_party_id priv
Private connected party ID.
Definition: channel.h:469
int ast_say_money_str(struct ast_channel *chan, const char *str, const char *ints, const char *lang)
function to pronounce monetary amounts
Definition: channel.c:8361
struct ast_format * ast_channel_rawwriteformat(struct ast_channel *chan)
static struct ast_frame * kill_read(struct ast_channel *chan)
Definition: channel.c:395
unsigned char valid
TRUE if the number information is valid/present.
Definition: channel.h:298
void ast_channel_sending_dtmf_digit_set(struct ast_channel *chan, char value)
const struct ast_channel_tech * ast_channel_tech(const struct ast_channel *chan)
ast_callid ast_channel_callid(const struct ast_channel *chan)
static void ast_read_generator_actions(struct ast_channel *chan, struct ast_frame *f)
Definition: channel.c:3416
void ast_channel_vstreamid_set(struct ast_channel *chan, int value)
struct ao2_iterator ao2_iterator_init(struct ao2_container *c, int flags) attribute_warn_unused_result
Create an iterator for a container.
struct ast_channel * ast_channel_masqr(const struct ast_channel *chan)
int ast_queue_hangup_with_cause(struct ast_channel *chan, int cause)
Queue a hangup frame for channel.
Definition: channel.c:1166
void ast_sched_context_destroy(struct ast_sched_context *c)
destroys a schedule context
Definition: sched.c:269
const char * ast_channel_parkinglot(const struct ast_channel *chan)
int str
Subscriber name ie.
Definition: channel.c:8421
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)
Indicate that the connected line information has changed.
Definition: channel.c:9189
struct ast_frame * ast_framehook_list_read_event(struct ast_framehook_list *framehooks, struct ast_frame *frame)
This is used by the channel API push a frame read event to a channel&#39;s framehook list.
Definition: framehook.c:318
static void deactivate_generator_nolock(struct ast_channel *chan)
Definition: channel.c:2886
#define AST_CAUSE_INVALID_CALL_REFERENCE
Definition: causes.h:133
#define AST_CAUSE_CONGESTION
Definition: causes.h:152
const char * ast_channel_macroexten(const struct ast_channel *chan)
#define AST_TRANS_CAP_DIGITAL
Definition: transcap.h:35
void ast_channel_outsmpl_set(struct ast_channel *chan, unsigned long value)
static struct ast_cli_entry cli_channel[]
Definition: channel.c:390
void ast_channel_softhangup_internal_flag_clear(struct ast_channel *chan, int value)
int ast_setstate(struct ast_channel *chan, enum ast_channel_state state)
Change the state of a channel.
Definition: channel.c:7486
Timing source management.
#define AST_MUTEX_DEFINE_STATIC(mutex)
Definition: lock.h:518
#define AST_CAUSE_WRONG_CALL_STATE
Definition: causes.h:141
int ast_stopstream(struct ast_channel *c)
Stops a stream.
Definition: file.c:187
void ast_party_connected_line_collect_caller(struct ast_party_connected_line *connected, struct ast_party_caller *caller)
Collect the caller party information into a connected line structure.
Definition: channel.c:2063
int ast_format_cap_has_type(const struct ast_format_cap *cap, enum ast_media_type type)
Find out if the capabilities structure has any formats of a specific type.
Definition: format_cap.c:615
static const char * dtmf_direction_to_string(enum DtmfDirection direction)
Definition: channel.c:3366
int ast_queue_control_data(struct ast_channel *chan, enum ast_control_frame_type control, const void *data, size_t datalen)
Queue a control frame with payload.
Definition: channel.c:1238
void ast_install_music_functions(int(*start_ptr)(struct ast_channel *, const char *, const char *), void(*stop_ptr)(struct ast_channel *), void(*cleanup_ptr)(struct ast_channel *))
Definition: channel.c:7850
static struct groups groups
static force_inline int attribute_pure ast_str_case_hash(const char *str)
Compute a hash value on a case-insensitive string.
Definition: strings.h:1250
int ast_channel_connected_line_sub(struct ast_channel *autoservice_chan, struct ast_channel *sub_chan, const void *connected_info, int is_frame)
Run a connected line interception subroutine and update a channel&#39;s connected line information...
Definition: channel.c:10539
struct ast_format * ast_format_cache_get_slin_by_rate(unsigned int rate)
Retrieve the best signed linear format given a sample rate.
Definition: format_cache.c:520
#define AST_RWLIST_HEAD(name, type)
Defines a structure to be used to hold a read/write list of specified type.
Definition: linkedlists.h:198
void ast_channel_set_fd(struct ast_channel *chan, int which, int fd)
Definition: channel.c:2431
void ast_translator_free_path(struct ast_trans_pvt *tr)
Frees a translator path Frees the given translator path structure.
Definition: translate.c:475
int ast_channel_alert_write(struct ast_channel *chan)
const char * ast_channel_musicclass(const struct ast_channel *chan)
jack_status_t status
Definition: app_jack.c:146
static struct ast_generator tonepair
Definition: channel.c:7676
struct ast_timer * ast_channel_timer(const struct ast_channel *chan)
void ast_party_redirecting_set_init(struct ast_party_redirecting *init, const struct ast_party_redirecting *guide)
Initialize the given redirecting id structure using the given guide for a set update operation...
Definition: channel.c:2153
int ast_active_channels(void)
returns number of active/allocated channels
Definition: channel.c:499
void ast_channel_internal_set_stream_topology(struct ast_channel *chan, struct ast_stream_topology *topology)
#define ast_str_create(init_len)
Create a malloc&#39;ed dynamic length string.
Definition: strings.h:620
void ast_channel_redirecting_set(struct ast_channel *chan, struct ast_party_redirecting *value)
void ast_channel_monitor_set(struct ast_channel *chan, struct ast_channel_monitor *value)
int ast_channel_feature_hooks_replace(struct ast_channel *chan, struct ast_bridge_features *features)
Sets the channel-attached features a channel has access to upon being bridged.
Definition: channel.c:11157
static int indicate_redirecting(struct ast_channel *chan, const void *data, size_t datalen)
Definition: channel.c:4501
void ast_channel_req_accountcodes_precious(struct ast_channel *chan, const struct ast_channel *requestor, enum ast_channel_requestor_relationship relationship)
Setup new channel accountcodes from the requestor channel after ast_request().
Definition: channel.c:6531
struct ast_frame * ast_read_noaudio(struct ast_channel *chan)
Reads a frame, returning AST_FRAME_NULL frame if audio.
Definition: channel.c:4312
void ast_set_variables(struct ast_channel *chan, struct ast_variable *vars)
adds a list of channel variables to a channel
Definition: channel.c:8217
int ast_safe_sleep_conditional(struct ast_channel *chan, int timeout_ms, int(*cond)(void *), void *data)
Wait for a specified amount of time, looking for hangups and a condition argument.
Definition: channel.c:1568
static force_inline int attribute_pure ast_str_hash(const char *str)
Compute a hash value on a string.
Definition: strings.h:1206
#define AST_LIST_APPEND_LIST(head, list, field)
Appends a whole list to the tail of a list.
Definition: linkedlists.h:782
int ast_channel_has_manager_vars(void)
Return whether or not any manager variables have been set.
Definition: channel.c:7957
static struct varshead * channel_get_external_vars(struct external_vars *channelvars, struct ast_channel *chan)
Definition: channel.c:8038
void ast_channel_setwhentohangup_tv(struct ast_channel *chan, struct timeval offset)
Set when to hangup channel.
Definition: channel.c:510
int ast_activate_generator(struct ast_channel *chan, struct ast_generator *gen, void *params)
Definition: channel.c:2960
static struct test_val a
struct ast_party_number number
Subscriber phone number.
Definition: channel.h:343
int ast_tonepair_start(struct ast_channel *chan, int freq1, int freq2, int duration, int vol)
Definition: channel.c:7682
#define ao2_link(container, obj)
Definition: astobj2.h:1549
int ast_queue_frame_head(struct ast_channel *chan, struct ast_frame *fin)
Queue one or more frames to the head of a channel&#39;s frame queue.
Definition: channel.c:1144
#define max(a, b)
Definition: f2c.h:198