Wed Jan 8 2020 09:49:46

Asterisk developer's documentation


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 <markster@digium.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18 
19 /*! \file
20  *
21  * \brief Channel Management
22  *
23  * \author Mark Spencer <markster@digium.com>
24  */
25 
26 /*** MODULEINFO
27  <support_level>core</support_level>
28  ***/
29 
30 #include "asterisk.h"
31 
32 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 423006 $")
33 
34 #include "asterisk/_private.h"
35 
36 #include <sys/time.h>
37 #include <signal.h>
38 #include <math.h>
39 
40 #include "asterisk/paths.h" /* use ast_config_AST_SYSTEM_NAME */
41 
42 #include "asterisk/pbx.h"
43 #include "asterisk/frame.h"
44 #include "asterisk/mod_format.h"
45 #include "asterisk/sched.h"
46 #include "asterisk/channel.h"
47 #include "asterisk/musiconhold.h"
48 #include "asterisk/say.h"
49 #include "asterisk/file.h"
50 #include "asterisk/cli.h"
51 #include "asterisk/translate.h"
52 #include "asterisk/manager.h"
53 #include "asterisk/cel.h"
54 #include "asterisk/chanvars.h"
55 #include "asterisk/linkedlists.h"
56 #include "asterisk/indications.h"
57 #include "asterisk/monitor.h"
58 #include "asterisk/causes.h"
59 #include "asterisk/callerid.h"
60 #include "asterisk/utils.h"
61 #include "asterisk/lock.h"
62 #include "asterisk/app.h"
63 #include "asterisk/transcap.h"
64 #include "asterisk/devicestate.h"
65 #include "asterisk/threadstorage.h"
66 #include "asterisk/slinfactory.h"
67 #include "asterisk/audiohook.h"
68 #include "asterisk/framehook.h"
69 #include "asterisk/timing.h"
70 #include "asterisk/autochan.h"
71 #include "asterisk/stringfields.h"
73 #include "asterisk/data.h"
74 #include "asterisk/features.h"
75 #include "asterisk/test.h"
76 
77 #ifdef HAVE_EPOLL
78 #include <sys/epoll.h>
79 #endif
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 
88  struct ast_channel *chan;
89  int which;
90 };
91 
92 /* uncomment if you have problems with 'monitoring' synchronized files */
93 #if 0
94 #define MONITOR_CONSTANT_DELAY
95 #define MONITOR_DELAY 150 * 8 /*!< 150 ms of MONITORING DELAY */
96 #endif
97 
98 /*! \brief Prevent new channel allocation if shutting down. */
99 static int shutting_down;
100 
101 static int uniqueint;
102 static int chancount;
103 
104 unsigned long global_fin, global_fout;
105 
107 #define STATE2STR_BUFSIZE 32
108 
109 /*! Default amount of time to use when emulating a digit as a begin and end
110  * 100ms */
111 #define AST_DEFAULT_EMULATE_DTMF_DURATION 100
112 
113 /*! Minimum allowed digit length - 80ms */
114 #define AST_MIN_DTMF_DURATION 80
115 
116 /*! Minimum amount of time between the end of the last digit and the beginning
117  * of a new one - 45ms */
118 #define AST_MIN_DTMF_GAP 45
119 
120 /*! \brief List of channel drivers */
121 struct chanlist {
122  const struct ast_channel_tech *tech;
124 };
125 
126 #ifdef CHANNEL_TRACE
127 /*! \brief Structure to hold channel context backtrace data */
128 struct ast_chan_trace_data {
129  int enabled;
130  AST_LIST_HEAD_NOLOCK(, ast_chan_trace) trace;
131 };
132 
133 /*! \brief Structure to save contexts where an ast_chan has been into */
134 struct ast_chan_trace {
135  char context[AST_MAX_CONTEXT];
136  char exten[AST_MAX_EXTENSION];
137  int priority;
138  AST_LIST_ENTRY(ast_chan_trace) entry;
139 };
140 #endif
141 
142 /*! \brief the list of registered channel types */
144 
145 #ifdef LOW_MEMORY
146 #define NUM_CHANNEL_BUCKETS 61
147 #else
148 #define NUM_CHANNEL_BUCKETS 1567
149 #endif
150 
151 #if 0 /* XXX AstData: ast_callerid no longer exists. (Equivalent code not readily apparent.) */
152 #define DATA_EXPORT_CALLERID(MEMBER) \
153  MEMBER(ast_callerid, cid_dnid, AST_DATA_STRING) \
154  MEMBER(ast_callerid, cid_num, AST_DATA_STRING) \
155  MEMBER(ast_callerid, cid_name, AST_DATA_STRING) \
156  MEMBER(ast_callerid, cid_ani, AST_DATA_STRING) \
157  MEMBER(ast_callerid, cid_pres, AST_DATA_INTEGER) \
158  MEMBER(ast_callerid, cid_ani2, AST_DATA_INTEGER) \
159  MEMBER(ast_callerid, cid_tag, AST_DATA_STRING)
160 
161 AST_DATA_STRUCTURE(ast_callerid, DATA_EXPORT_CALLERID);
162 #endif
163 
164 #define DATA_EXPORT_CHANNEL(MEMBER) \
165  MEMBER(ast_channel, blockproc, AST_DATA_STRING) \
166  MEMBER(ast_channel, appl, AST_DATA_STRING) \
167  MEMBER(ast_channel, data, AST_DATA_STRING) \
168  MEMBER(ast_channel, name, AST_DATA_STRING) \
169  MEMBER(ast_channel, language, AST_DATA_STRING) \
170  MEMBER(ast_channel, musicclass, AST_DATA_STRING) \
171  MEMBER(ast_channel, accountcode, AST_DATA_STRING) \
172  MEMBER(ast_channel, peeraccount, AST_DATA_STRING) \
173  MEMBER(ast_channel, userfield, AST_DATA_STRING) \
174  MEMBER(ast_channel, call_forward, AST_DATA_STRING) \
175  MEMBER(ast_channel, uniqueid, AST_DATA_STRING) \
176  MEMBER(ast_channel, linkedid, AST_DATA_STRING) \
177  MEMBER(ast_channel, parkinglot, AST_DATA_STRING) \
178  MEMBER(ast_channel, hangupsource, AST_DATA_STRING) \
179  MEMBER(ast_channel, dialcontext, AST_DATA_STRING) \
180  MEMBER(ast_channel, rings, AST_DATA_INTEGER) \
181  MEMBER(ast_channel, priority, AST_DATA_INTEGER) \
182  MEMBER(ast_channel, macropriority, AST_DATA_INTEGER) \
183  MEMBER(ast_channel, adsicpe, AST_DATA_INTEGER) \
184  MEMBER(ast_channel, fin, AST_DATA_UNSIGNED_INTEGER) \
185  MEMBER(ast_channel, fout, AST_DATA_UNSIGNED_INTEGER) \
186  MEMBER(ast_channel, emulate_dtmf_duration, AST_DATA_UNSIGNED_INTEGER) \
187  MEMBER(ast_channel, visible_indication, AST_DATA_INTEGER) \
188  MEMBER(ast_channel, context, AST_DATA_STRING) \
189  MEMBER(ast_channel, exten, AST_DATA_STRING) \
190  MEMBER(ast_channel, macrocontext, AST_DATA_STRING) \
191  MEMBER(ast_channel, macroexten, AST_DATA_STRING)
192 
194 
195 
196 /*! \brief All active channels on the system */
197 static struct ao2_container *channels;
198 
199 /*! \brief map AST_CAUSE's to readable string representations
200  *
201  * \ref causes.h
202 */
203 struct causes_map {
204  int cause;
205  const char *name;
206  const char *desc;
207 };
208 
209 static const struct causes_map causes[] = {
210  { AST_CAUSE_UNALLOCATED, "UNALLOCATED", "Unallocated (unassigned) number" },
211  { AST_CAUSE_NO_ROUTE_TRANSIT_NET, "NO_ROUTE_TRANSIT_NET", "No route to specified transmit network" },
212  { AST_CAUSE_NO_ROUTE_DESTINATION, "NO_ROUTE_DESTINATION", "No route to destination" },
213  { AST_CAUSE_MISDIALLED_TRUNK_PREFIX, "MISDIALLED_TRUNK_PREFIX", "Misdialed trunk prefix" },
214  { AST_CAUSE_CHANNEL_UNACCEPTABLE, "CHANNEL_UNACCEPTABLE", "Channel unacceptable" },
215  { AST_CAUSE_CALL_AWARDED_DELIVERED, "CALL_AWARDED_DELIVERED", "Call awarded and being delivered in an established channel" },
216  { AST_CAUSE_PRE_EMPTED, "PRE_EMPTED", "Pre-empted" },
217  { AST_CAUSE_NUMBER_PORTED_NOT_HERE, "NUMBER_PORTED_NOT_HERE", "Number ported elsewhere" },
218  { AST_CAUSE_NORMAL_CLEARING, "NORMAL_CLEARING", "Normal Clearing" },
219  { AST_CAUSE_USER_BUSY, "USER_BUSY", "User busy" },
220  { AST_CAUSE_NO_USER_RESPONSE, "NO_USER_RESPONSE", "No user responding" },
221  { AST_CAUSE_NO_ANSWER, "NO_ANSWER", "User alerting, no answer" },
222  { AST_CAUSE_SUBSCRIBER_ABSENT, "SUBSCRIBER_ABSENT", "Subscriber absent" },
223  { AST_CAUSE_CALL_REJECTED, "CALL_REJECTED", "Call Rejected" },
224  { AST_CAUSE_NUMBER_CHANGED, "NUMBER_CHANGED", "Number changed" },
225  { AST_CAUSE_REDIRECTED_TO_NEW_DESTINATION, "REDIRECTED_TO_NEW_DESTINATION", "Redirected to new destination" },
226  { AST_CAUSE_ANSWERED_ELSEWHERE, "ANSWERED_ELSEWHERE", "Answered elsewhere" },
227  { AST_CAUSE_DESTINATION_OUT_OF_ORDER, "DESTINATION_OUT_OF_ORDER", "Destination out of order" },
228  { AST_CAUSE_INVALID_NUMBER_FORMAT, "INVALID_NUMBER_FORMAT", "Invalid number format" },
229  { AST_CAUSE_FACILITY_REJECTED, "FACILITY_REJECTED", "Facility rejected" },
230  { AST_CAUSE_RESPONSE_TO_STATUS_ENQUIRY, "RESPONSE_TO_STATUS_ENQUIRY", "Response to STATus ENQuiry" },
231  { AST_CAUSE_NORMAL_UNSPECIFIED, "NORMAL_UNSPECIFIED", "Normal, unspecified" },
232  { AST_CAUSE_NORMAL_CIRCUIT_CONGESTION, "NORMAL_CIRCUIT_CONGESTION", "Circuit/channel congestion" },
233  { AST_CAUSE_NETWORK_OUT_OF_ORDER, "NETWORK_OUT_OF_ORDER", "Network out of order" },
234  { AST_CAUSE_NORMAL_TEMPORARY_FAILURE, "NORMAL_TEMPORARY_FAILURE", "Temporary failure" },
235  { AST_CAUSE_SWITCH_CONGESTION, "SWITCH_CONGESTION", "Switching equipment congestion" },
236  { AST_CAUSE_ACCESS_INFO_DISCARDED, "ACCESS_INFO_DISCARDED", "Access information discarded" },
237  { AST_CAUSE_REQUESTED_CHAN_UNAVAIL, "REQUESTED_CHAN_UNAVAIL", "Requested channel not available" },
238  { AST_CAUSE_FACILITY_NOT_SUBSCRIBED, "FACILITY_NOT_SUBSCRIBED", "Facility not subscribed" },
239  { AST_CAUSE_OUTGOING_CALL_BARRED, "OUTGOING_CALL_BARRED", "Outgoing call barred" },
240  { AST_CAUSE_INCOMING_CALL_BARRED, "INCOMING_CALL_BARRED", "Incoming call barred" },
241  { AST_CAUSE_BEARERCAPABILITY_NOTAUTH, "BEARERCAPABILITY_NOTAUTH", "Bearer capability not authorized" },
242  { AST_CAUSE_BEARERCAPABILITY_NOTAVAIL, "BEARERCAPABILITY_NOTAVAIL", "Bearer capability not available" },
243  { AST_CAUSE_BEARERCAPABILITY_NOTIMPL, "BEARERCAPABILITY_NOTIMPL", "Bearer capability not implemented" },
244  { AST_CAUSE_CHAN_NOT_IMPLEMENTED, "CHAN_NOT_IMPLEMENTED", "Channel not implemented" },
245  { AST_CAUSE_FACILITY_NOT_IMPLEMENTED, "FACILITY_NOT_IMPLEMENTED", "Facility not implemented" },
246  { AST_CAUSE_INVALID_CALL_REFERENCE, "INVALID_CALL_REFERENCE", "Invalid call reference value" },
247  { AST_CAUSE_INCOMPATIBLE_DESTINATION, "INCOMPATIBLE_DESTINATION", "Incompatible destination" },
248  { AST_CAUSE_INVALID_MSG_UNSPECIFIED, "INVALID_MSG_UNSPECIFIED", "Invalid message unspecified" },
249  { AST_CAUSE_MANDATORY_IE_MISSING, "MANDATORY_IE_MISSING", "Mandatory information element is missing" },
250  { AST_CAUSE_MESSAGE_TYPE_NONEXIST, "MESSAGE_TYPE_NONEXIST", "Message type nonexist." },
251  { AST_CAUSE_WRONG_MESSAGE, "WRONG_MESSAGE", "Wrong message" },
252  { AST_CAUSE_IE_NONEXIST, "IE_NONEXIST", "Info. element nonexist or not implemented" },
253  { AST_CAUSE_INVALID_IE_CONTENTS, "INVALID_IE_CONTENTS", "Invalid information element contents" },
254  { AST_CAUSE_WRONG_CALL_STATE, "WRONG_CALL_STATE", "Message not compatible with call state" },
255  { AST_CAUSE_RECOVERY_ON_TIMER_EXPIRE, "RECOVERY_ON_TIMER_EXPIRE", "Recover on timer expiry" },
256  { AST_CAUSE_MANDATORY_IE_LENGTH_ERROR, "MANDATORY_IE_LENGTH_ERROR", "Mandatory IE length error" },
257  { AST_CAUSE_PROTOCOL_ERROR, "PROTOCOL_ERROR", "Protocol error, unspecified" },
258  { AST_CAUSE_INTERWORKING, "INTERWORKING", "Interworking, unspecified" },
259 };
260 
262 {
263  struct chanlist *cl;
264  struct ast_variable *var = NULL, *prev = NULL;
265 
267  AST_RWLIST_TRAVERSE(&backends, cl, list) {
268  if (prev) {
269  if ((prev->next = ast_variable_new(cl->tech->type, cl->tech->description, "")))
270  prev = prev->next;
271  } else {
272  var = ast_variable_new(cl->tech->type, cl->tech->description, "");
273  prev = var;
274  }
275  }
277 
278  return var;
279 }
280 
281 static void channel_data_add_flags(struct ast_data *tree,
282  struct ast_channel *chan)
283 {
284  ast_data_add_bool(tree, "DEFER_DTMF", ast_test_flag(chan, AST_FLAG_DEFER_DTMF));
285  ast_data_add_bool(tree, "WRITE_INT", ast_test_flag(chan, AST_FLAG_WRITE_INT));
286  ast_data_add_bool(tree, "BLOCKING", ast_test_flag(chan, AST_FLAG_BLOCKING));
287  ast_data_add_bool(tree, "ZOMBIE", ast_test_flag(chan, AST_FLAG_ZOMBIE));
288  ast_data_add_bool(tree, "EXCEPTION", ast_test_flag(chan, AST_FLAG_EXCEPTION));
289  ast_data_add_bool(tree, "MOH", ast_test_flag(chan, AST_FLAG_MOH));
290  ast_data_add_bool(tree, "SPYING", ast_test_flag(chan, AST_FLAG_SPYING));
291  ast_data_add_bool(tree, "NBRIDGE", ast_test_flag(chan, AST_FLAG_NBRIDGE));
292  ast_data_add_bool(tree, "IN_AUTOLOOP", ast_test_flag(chan, AST_FLAG_IN_AUTOLOOP));
293  ast_data_add_bool(tree, "OUTGOING", ast_test_flag(chan, AST_FLAG_OUTGOING));
294  ast_data_add_bool(tree, "IN_DTMF", ast_test_flag(chan, AST_FLAG_IN_DTMF));
295  ast_data_add_bool(tree, "EMULATE_DTMF", ast_test_flag(chan, AST_FLAG_EMULATE_DTMF));
296  ast_data_add_bool(tree, "END_DTMF_ONLY", ast_test_flag(chan, AST_FLAG_END_DTMF_ONLY));
297  ast_data_add_bool(tree, "ANSWERED_ELSEWHERE", ast_test_flag(chan, AST_FLAG_ANSWERED_ELSEWHERE));
298  ast_data_add_bool(tree, "MASQ_NOSTREAM", ast_test_flag(chan, AST_FLAG_MASQ_NOSTREAM));
299  ast_data_add_bool(tree, "BRIDGE_HANGUP_RUN", ast_test_flag(chan, AST_FLAG_BRIDGE_HANGUP_RUN));
300  ast_data_add_bool(tree, "BRIDGE_HANGUP_DONT", ast_test_flag(chan, AST_FLAG_BRIDGE_HANGUP_DONT));
301  ast_data_add_bool(tree, "DISABLE_WORKAROUNDS", ast_test_flag(chan, AST_FLAG_DISABLE_WORKAROUNDS));
302  ast_data_add_bool(tree, "DISABLE_DEVSTATE_CACHE", ast_test_flag(chan, AST_FLAG_DISABLE_DEVSTATE_CACHE));
303 }
304 
305 #if defined(KEEP_TILL_CHANNEL_PARTY_NUMBER_INFO_NEEDED)
306 static const char *party_number_ton2str(int ton)
307 {
308 #if defined(HAVE_PRI)
309  switch ((ton >> 4) & 0x07) {
310  case PRI_TON_INTERNATIONAL:
311  return "International";
312  case PRI_TON_NATIONAL:
313  return "National";
314  case PRI_TON_NET_SPECIFIC:
315  return "Network Specific";
316  case PRI_TON_SUBSCRIBER:
317  return "Subscriber";
318  case PRI_TON_ABBREVIATED:
319  return "Abbreviated";
320  case PRI_TON_RESERVED:
321  return "Reserved";
322  case PRI_TON_UNKNOWN:
323  default:
324  break;
325  }
326 #endif /* defined(HAVE_PRI) */
327  return "Unknown";
328 }
329 #endif /* defined(KEEP_TILL_CHANNEL_PARTY_NUMBER_INFO_NEEDED) */
330 
331 #if defined(KEEP_TILL_CHANNEL_PARTY_NUMBER_INFO_NEEDED)
332 static const char *party_number_plan2str(int plan)
333 {
334 #if defined(HAVE_PRI)
335  switch (plan & 0x0F) {
336  default:
337  case PRI_NPI_UNKNOWN:
338  break;
339  case PRI_NPI_E163_E164:
340  return "Public (E.163/E.164)";
341  case PRI_NPI_X121:
342  return "Data (X.121)";
343  case PRI_NPI_F69:
344  return "Telex (F.69)";
345  case PRI_NPI_NATIONAL:
346  return "National Standard";
347  case PRI_NPI_PRIVATE:
348  return "Private";
349  case PRI_NPI_RESERVED:
350  return "Reserved";
351  }
352 #endif /* defined(HAVE_PRI) */
353  return "Unknown";
354 }
355 #endif /* defined(KEEP_TILL_CHANNEL_PARTY_NUMBER_INFO_NEEDED) */
356 
358  struct ast_channel *chan, int add_bridged)
359 {
360  struct ast_channel *bc;
361  struct ast_data *data_bridged;
362  struct ast_data *data_cdr;
363  struct ast_data *data_flags;
364  struct ast_data *data_zones;
365  struct ast_data *enum_node;
366  struct ast_data *data_softhangup;
367 #if 0 /* XXX AstData: ast_callerid no longer exists. (Equivalent code not readily apparent.) */
368  struct ast_data *data_callerid;
369  char value_str[100];
370 #endif
371 
372  if (!tree) {
373  return -1;
374  }
375 
376  ast_data_add_structure(ast_channel, tree, chan);
377 
378  if (add_bridged) {
379  bc = ast_bridged_channel(chan);
380  if (bc) {
381  data_bridged = ast_data_add_node(tree, "bridged");
382  if (!data_bridged) {
383  return -1;
384  }
385  ast_channel_data_add_structure(data_bridged, bc, 0);
386  }
387  }
388 
389  ast_data_add_codecs(tree, "oldwriteformat", chan->oldwriteformat);
390  ast_data_add_codecs(tree, "nativeformats", chan->nativeformats);
391  ast_data_add_codecs(tree, "readformat", chan->readformat);
392  ast_data_add_codecs(tree, "writeformat", chan->writeformat);
393  ast_data_add_codecs(tree, "rawreadformat", chan->rawreadformat);
394  ast_data_add_codecs(tree, "rawwriteformat", chan->rawwriteformat);
395 
396  /* state */
397  enum_node = ast_data_add_node(tree, "state");
398  if (!enum_node) {
399  return -1;
400  }
401  ast_data_add_str(enum_node, "text", ast_state2str(chan->_state));
402  ast_data_add_int(enum_node, "value", chan->_state);
403 
404  /* hangupcause */
405  enum_node = ast_data_add_node(tree, "hangupcause");
406  if (!enum_node) {
407  return -1;
408  }
409  ast_data_add_str(enum_node, "text", ast_cause2str(chan->hangupcause));
410  ast_data_add_int(enum_node, "value", chan->hangupcause);
411 
412  /* amaflags */
413  enum_node = ast_data_add_node(tree, "amaflags");
414  if (!enum_node) {
415  return -1;
416  }
417  ast_data_add_str(enum_node, "text", ast_cdr_flags2str(chan->amaflags));
418  ast_data_add_int(enum_node, "value", chan->amaflags);
419 
420  /* transfercapability */
421  enum_node = ast_data_add_node(tree, "transfercapability");
422  if (!enum_node) {
423  return -1;
424  }
426  ast_data_add_int(enum_node, "value", chan->transfercapability);
427 
428  /* _softphangup */
429  data_softhangup = ast_data_add_node(tree, "softhangup");
430  if (!data_softhangup) {
431  return -1;
432  }
433  ast_data_add_bool(data_softhangup, "dev", chan->_softhangup & AST_SOFTHANGUP_DEV);
434  ast_data_add_bool(data_softhangup, "asyncgoto", chan->_softhangup & AST_SOFTHANGUP_ASYNCGOTO);
435  ast_data_add_bool(data_softhangup, "shutdown", chan->_softhangup & AST_SOFTHANGUP_SHUTDOWN);
436  ast_data_add_bool(data_softhangup, "timeout", chan->_softhangup & AST_SOFTHANGUP_TIMEOUT);
437  ast_data_add_bool(data_softhangup, "appunload", chan->_softhangup & AST_SOFTHANGUP_APPUNLOAD);
438  ast_data_add_bool(data_softhangup, "explicit", chan->_softhangup & AST_SOFTHANGUP_EXPLICIT);
439  ast_data_add_bool(data_softhangup, "unbridge", chan->_softhangup & AST_SOFTHANGUP_UNBRIDGE);
440 
441  /* channel flags */
442  data_flags = ast_data_add_node(tree, "flags");
443  if (!data_flags) {
444  return -1;
445  }
446  channel_data_add_flags(data_flags, chan);
447 
448  ast_data_add_uint(tree, "timetohangup", chan->whentohangup.tv_sec);
449 
450 #if 0 /* XXX AstData: ast_callerid no longer exists. (Equivalent code not readily apparent.) */
451  /* callerid */
452  data_callerid = ast_data_add_node(tree, "callerid");
453  if (!data_callerid) {
454  return -1;
455  }
456  ast_data_add_structure(ast_callerid, data_callerid, &(chan->cid));
457  /* insert the callerid ton */
458  enum_node = ast_data_add_node(data_callerid, "cid_ton");
459  if (!enum_node) {
460  return -1;
461  }
462  ast_data_add_int(enum_node, "value", chan->cid.cid_ton);
463  snprintf(value_str, sizeof(value_str), "TON: %s/Plan: %s",
464  party_number_ton2str(chan->cid.cid_ton),
465  party_number_plan2str(chan->cid.cid_ton));
466  ast_data_add_str(enum_node, "text", value_str);
467 #endif
468 
469  /* tone zone */
470  if (chan->zone) {
471  data_zones = ast_data_add_node(tree, "zone");
472  if (!data_zones) {
473  return -1;
474  }
475  ast_tone_zone_data_add_structure(data_zones, chan->zone);
476  }
477 
478  /* insert cdr */
479  data_cdr = ast_data_add_node(tree, "cdr");
480  if (!data_cdr) {
481  return -1;
482  }
483 
484  ast_cdr_data_add_structure(data_cdr, chan->cdr, 1);
485 
486  return 0;
487 }
488 
490  struct ast_channel *chan, const char *structure_name)
491 {
492  return ast_data_search_cmp_structure(tree, ast_channel, chan, structure_name);
493 }
494 
495 /*! \brief Show channel types - CLI command */
496 static char *handle_cli_core_show_channeltypes(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
497 {
498 #define FORMAT "%-10.10s %-40.40s %-12.12s %-12.12s %-12.12s\n"
499  struct chanlist *cl;
500  int count_chan = 0;
501 
502  switch (cmd) {
503  case CLI_INIT:
504  e->command = "core show channeltypes";
505  e->usage =
506  "Usage: core show channeltypes\n"
507  " Lists available channel types registered in your\n"
508  " Asterisk server.\n";
509  return NULL;
510  case CLI_GENERATE:
511  return NULL;
512  }
513 
514  if (a->argc != 3)
515  return CLI_SHOWUSAGE;
516 
517  ast_cli(a->fd, FORMAT, "Type", "Description", "Devicestate", "Indications", "Transfer");
518  ast_cli(a->fd, FORMAT, "----------", "-----------", "-----------", "-----------", "--------");
519 
521  AST_RWLIST_TRAVERSE(&backends, cl, list) {
522  ast_cli(a->fd, FORMAT, cl->tech->type, cl->tech->description,
523  (cl->tech->devicestate) ? "yes" : "no",
524  (cl->tech->indicate) ? "yes" : "no",
525  (cl->tech->transfer) ? "yes" : "no");
526  count_chan++;
527  }
529 
530  ast_cli(a->fd, "----------\n%d channel drivers registered.\n", count_chan);
531 
532  return CLI_SUCCESS;
533 
534 #undef FORMAT
535 }
536 
537 static char *complete_channeltypes(struct ast_cli_args *a)
538 {
539  struct chanlist *cl;
540  int which = 0;
541  int wordlen;
542  char *ret = NULL;
543 
544  if (a->pos != 3)
545  return NULL;
546 
547  wordlen = strlen(a->word);
548 
550  AST_RWLIST_TRAVERSE(&backends, cl, list) {
551  if (!strncasecmp(a->word, cl->tech->type, wordlen) && ++which > a->n) {
552  ret = ast_strdup(cl->tech->type);
553  break;
554  }
555  }
557 
558  return ret;
559 }
560 
561 /*! \brief Show details about a channel driver - CLI command */
562 static char *handle_cli_core_show_channeltype(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
563 {
564  struct chanlist *cl = NULL;
565  char buf[512];
566 
567  switch (cmd) {
568  case CLI_INIT:
569  e->command = "core show channeltype";
570  e->usage =
571  "Usage: core show channeltype <name>\n"
572  " Show details about the specified channel type, <name>.\n";
573  return NULL;
574  case CLI_GENERATE:
575  return complete_channeltypes(a);
576  }
577 
578  if (a->argc != 4)
579  return CLI_SHOWUSAGE;
580 
582 
583  AST_RWLIST_TRAVERSE(&backends, cl, list) {
584  if (!strncasecmp(cl->tech->type, a->argv[3], strlen(cl->tech->type)))
585  break;
586  }
587 
588 
589  if (!cl) {
590  ast_cli(a->fd, "\n%s is not a registered channel driver.\n", a->argv[3]);
592  return CLI_FAILURE;
593  }
594 
595  ast_cli(a->fd,
596  "-- Info about channel driver: %s --\n"
597  " Device State: %s\n"
598  " Indication: %s\n"
599  " Transfer : %s\n"
600  " Capabilities: %s\n"
601  " Digit Begin: %s\n"
602  " Digit End: %s\n"
603  " Send HTML : %s\n"
604  " Image Support: %s\n"
605  " Text Support: %s\n",
606  cl->tech->type,
607  (cl->tech->devicestate) ? "yes" : "no",
608  (cl->tech->indicate) ? "yes" : "no",
609  (cl->tech->transfer) ? "yes" : "no",
610  ast_getformatname_multiple(buf, sizeof(buf), (cl->tech->capabilities) ? cl->tech->capabilities : -1),
611  (cl->tech->send_digit_begin) ? "yes" : "no",
612  (cl->tech->send_digit_end) ? "yes" : "no",
613  (cl->tech->send_html) ? "yes" : "no",
614  (cl->tech->send_image) ? "yes" : "no",
615  (cl->tech->send_text) ? "yes" : "no"
616 
617  );
618 
620 
621  return CLI_SUCCESS;
622 }
623 
624 static struct ast_cli_entry cli_channel[] = {
625  AST_CLI_DEFINE(handle_cli_core_show_channeltypes, "List available channel types"),
626  AST_CLI_DEFINE(handle_cli_core_show_channeltype, "Give more details on that channel type")
627 };
628 
629 static struct ast_frame *kill_read(struct ast_channel *chan)
630 {
631  /* Hangup channel. */
632  return NULL;
633 }
634 
635 static struct ast_frame *kill_exception(struct ast_channel *chan)
636 {
637  /* Hangup channel. */
638  return NULL;
639 }
640 
641 static int kill_write(struct ast_channel *chan, struct ast_frame *frame)
642 {
643  /* Hangup channel. */
644  return -1;
645 }
646 
647 static int kill_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
648 {
649  /* No problem fixing up the channel. */
650  return 0;
651 }
652 
653 static int kill_hangup(struct ast_channel *chan)
654 {
655  chan->tech_pvt = NULL;
656  return 0;
657 }
658 
659 /*!
660  * \brief Kill the channel channel driver technology descriptor.
661  *
662  * \details
663  * The purpose of this channel technology is to encourage the
664  * channel to hangup as quickly as possible.
665  *
666  * \note Used by DTMF atxfer and zombie channels.
667  */
669  .type = "Kill",
670  .description = "Kill channel (should not see this)",
671  .capabilities = -1,
672  .read = kill_read,
673  .exception = kill_exception,
674  .write = kill_write,
675  .fixup = kill_fixup,
676  .hangup = kill_hangup,
677 };
678 
679 #ifdef CHANNEL_TRACE
680 /*! \brief Destructor for the channel trace datastore */
681 static void ast_chan_trace_destroy_cb(void *data)
682 {
683  struct ast_chan_trace *trace;
684  struct ast_chan_trace_data *traced = data;
685  while ((trace = AST_LIST_REMOVE_HEAD(&traced->trace, entry))) {
686  ast_free(trace);
687  }
688  ast_free(traced);
689 }
690 
691 /*! \brief Datastore to put the linked list of ast_chan_trace and trace status */
692 static const struct ast_datastore_info ast_chan_trace_datastore_info = {
693  .type = "ChanTrace",
694  .destroy = ast_chan_trace_destroy_cb
695 };
696 
697 /*! \brief Put the channel backtrace in a string */
698 int ast_channel_trace_serialize(struct ast_channel *chan, struct ast_str **buf)
699 {
700  int total = 0;
701  struct ast_chan_trace *trace;
702  struct ast_chan_trace_data *traced;
703  struct ast_datastore *store;
704 
705  ast_channel_lock(chan);
706  store = ast_channel_datastore_find(chan, &ast_chan_trace_datastore_info, NULL);
707  if (!store) {
708  ast_channel_unlock(chan);
709  return total;
710  }
711  traced = store->data;
712  ast_str_reset(*buf);
713  AST_LIST_TRAVERSE(&traced->trace, trace, entry) {
714  if (ast_str_append(buf, 0, "[%d] => %s, %s, %d\n", total, trace->context, trace->exten, trace->priority) < 0) {
715  ast_log(LOG_ERROR, "Data Buffer Size Exceeded!\n");
716  total = -1;
717  break;
718  }
719  total++;
720  }
721  ast_channel_unlock(chan);
722  return total;
723 }
724 
725 /* !\brief Whether or not context tracing is enabled */
726 int ast_channel_trace_is_enabled(struct ast_channel *chan)
727 {
728  struct ast_datastore *store = ast_channel_datastore_find(chan, &ast_chan_trace_datastore_info, NULL);
729  if (!store)
730  return 0;
731  return ((struct ast_chan_trace_data *)store->data)->enabled;
732 }
733 
734 /*! \brief Update the context backtrace data if tracing is enabled */
735 static int ast_channel_trace_data_update(struct ast_channel *chan, struct ast_chan_trace_data *traced)
736 {
737  struct ast_chan_trace *trace;
738  if (!traced->enabled)
739  return 0;
740  /* If the last saved context does not match the current one
741  OR we have not saved any context so far, then save the current context */
742  if ((!AST_LIST_EMPTY(&traced->trace) && strcasecmp(AST_LIST_FIRST(&traced->trace)->context, chan->context)) ||
743  (AST_LIST_EMPTY(&traced->trace))) {
744  /* Just do some debug logging */
745  if (AST_LIST_EMPTY(&traced->trace))
746  ast_log(LOG_DEBUG, "Setting initial trace context to %s\n", chan->context);
747  else
748  ast_log(LOG_DEBUG, "Changing trace context from %s to %s\n", AST_LIST_FIRST(&traced->trace)->context, chan->context);
749  /* alloc or bail out */
750  trace = ast_malloc(sizeof(*trace));
751  if (!trace)
752  return -1;
753  /* save the current location and store it in the trace list */
754  ast_copy_string(trace->context, chan->context, sizeof(trace->context));
755  ast_copy_string(trace->exten, chan->exten, sizeof(trace->exten));
756  trace->priority = chan->priority;
757  AST_LIST_INSERT_HEAD(&traced->trace, trace, entry);
758  }
759  return 0;
760 }
761 
762 /*! \brief Update the context backtrace if tracing is enabled */
763 int ast_channel_trace_update(struct ast_channel *chan)
764 {
765  struct ast_datastore *store = ast_channel_datastore_find(chan, &ast_chan_trace_datastore_info, NULL);
766  if (!store)
767  return 0;
768  return ast_channel_trace_data_update(chan, store->data);
769 }
770 
771 /*! \brief Enable context tracing in the channel */
772 int ast_channel_trace_enable(struct ast_channel *chan)
773 {
774  struct ast_datastore *store = ast_channel_datastore_find(chan, &ast_chan_trace_datastore_info, NULL);
775  struct ast_chan_trace_data *traced;
776  if (!store) {
777  store = ast_datastore_alloc(&ast_chan_trace_datastore_info, "ChanTrace");
778  if (!store)
779  return -1;
780  traced = ast_calloc(1, sizeof(*traced));
781  if (!traced) {
782  ast_datastore_free(store);
783  return -1;
784  }
785  store->data = traced;
786  AST_LIST_HEAD_INIT_NOLOCK(&traced->trace);
787  ast_channel_datastore_add(chan, store);
788  }
789  ((struct ast_chan_trace_data *)store->data)->enabled = 1;
790  ast_channel_trace_data_update(chan, store->data);
791  return 0;
792 }
793 
794 /*! \brief Disable context tracing in the channel */
795 int ast_channel_trace_disable(struct ast_channel *chan)
796 {
797  struct ast_datastore *store = ast_channel_datastore_find(chan, &ast_chan_trace_datastore_info, NULL);
798  if (!store)
799  return 0;
800  ((struct ast_chan_trace_data *)store->data)->enabled = 0;
801  return 0;
802 }
803 #endif /* CHANNEL_TRACE */
804 
805 /*! \brief Checks to see if a channel is needing hang up */
806 int ast_check_hangup(struct ast_channel *chan)
807 {
808  if (chan->_softhangup) /* yes if soft hangup flag set */
809  return 1;
810  if (ast_tvzero(chan->whentohangup)) /* no if no hangup scheduled */
811  return 0;
812  if (ast_tvdiff_ms(chan->whentohangup, ast_tvnow()) > 0) /* no if hangup time has not come yet. */
813  return 0;
814  ast_debug(4, "Hangup time has come: %" PRIi64 "\n", ast_tvdiff_ms(chan->whentohangup, ast_tvnow()));
815  ast_test_suite_event_notify("HANGUP_TIME", "Channel: %s", chan->name);
816  chan->_softhangup |= AST_SOFTHANGUP_TIMEOUT; /* record event */
817  return 1;
818 }
819 
821 {
822  int res;
823  ast_channel_lock(chan);
824  res = ast_check_hangup(chan);
825  ast_channel_unlock(chan);
826  return res;
827 }
828 
829 static int ast_channel_softhangup_cb(void *obj, void *arg, int flags)
830 {
831  struct ast_channel *chan = obj;
832 
834 
835  return 0;
836 }
837 
838 void ast_begin_shutdown(int hangup)
839 {
840  shutting_down = 1;
841 
842  if (hangup) {
844  }
845 }
846 
847 /*! \brief returns number of active/allocated channels */
849 {
850  return channels ? ao2_container_count(channels) : 0;
851 }
852 
854 {
855  return ast_atomic_fetchadd_int(&chancount, 0);
856 }
857 
858 /*! \brief Cancel a shutdown in progress */
860 {
861  shutting_down = 0;
862 }
863 
864 /*! \brief Returns non-zero if Asterisk is being shut down */
866 {
867  return shutting_down;
868 }
869 
870 /*! \brief Set when to hangup channel */
871 void ast_channel_setwhentohangup_tv(struct ast_channel *chan, struct timeval offset)
872 {
873  chan->whentohangup = ast_tvzero(offset) ? offset : ast_tvadd(offset, ast_tvnow());
875  return;
876 }
877 
878 void ast_channel_setwhentohangup(struct ast_channel *chan, time_t offset)
879 {
880  struct timeval when = { offset, };
881  ast_channel_setwhentohangup_tv(chan, when);
882 }
883 
884 /*! \brief Compare a offset with when to hangup channel */
885 int ast_channel_cmpwhentohangup_tv(struct ast_channel *chan, struct timeval offset)
886 {
887  struct timeval whentohangup;
888 
889  if (ast_tvzero(chan->whentohangup))
890  return ast_tvzero(offset) ? 0 : -1;
891 
892  if (ast_tvzero(offset))
893  return 1;
894 
895  whentohangup = ast_tvadd(offset, ast_tvnow());
896 
897  return ast_tvdiff_ms(whentohangup, chan->whentohangup);
898 }
899 
900 int ast_channel_cmpwhentohangup(struct ast_channel *chan, time_t offset)
901 {
902  struct timeval when = { offset, };
903  return ast_channel_cmpwhentohangup_tv(chan, when);
904 }
905 
906 /*! \brief Register a new telephony channel in Asterisk */
908 {
909  struct chanlist *chan;
910 
912 
913  AST_RWLIST_TRAVERSE(&backends, chan, list) {
914  if (!strcasecmp(tech->type, chan->tech->type)) {
915  ast_log(LOG_WARNING, "Already have a handler for type '%s'\n", tech->type);
917  return -1;
918  }
919  }
920 
921  if (!(chan = ast_calloc(1, sizeof(*chan)))) {
923  return -1;
924  }
925  chan->tech = tech;
926  AST_RWLIST_INSERT_HEAD(&backends, chan, list);
927 
928  ast_debug(1, "Registered handler for '%s' (%s)\n", chan->tech->type, chan->tech->description);
929 
930  ast_verb(2, "Registered channel type '%s' (%s)\n", chan->tech->type, chan->tech->description);
931 
933 
934  return 0;
935 }
936 
937 /*! \brief Unregister channel driver */
939 {
940  struct chanlist *chan;
941 
942  ast_debug(1, "Unregistering channel type '%s'\n", tech->type);
943 
945 
947  if (chan->tech == tech) {
949  ast_free(chan);
950  ast_verb(2, "Unregistered channel type '%s'\n", tech->type);
951  break;
952  }
953  }
955 
957 }
958 
959 /*! \brief Get handle to channel driver based on name */
960 const struct ast_channel_tech *ast_get_channel_tech(const char *name)
961 {
962  struct chanlist *chanls;
963  const struct ast_channel_tech *ret = NULL;
964 
966 
967  AST_RWLIST_TRAVERSE(&backends, chanls, list) {
968  if (!strcasecmp(name, chanls->tech->type)) {
969  ret = chanls->tech;
970  break;
971  }
972  }
973 
975 
976  return ret;
977 }
978 
979 /*! \brief Gives the string form of a given hangup cause */
980 const char *ast_cause2str(int cause)
981 {
982  int x;
983 
984  for (x = 0; x < ARRAY_LEN(causes); x++) {
985  if (causes[x].cause == cause)
986  return causes[x].desc;
987  }
988 
989  return "Unknown";
990 }
991 
992 /*! \brief Convert a symbolic hangup cause to number */
993 int ast_str2cause(const char *name)
994 {
995  int x;
996 
997  for (x = 0; x < ARRAY_LEN(causes); x++)
998  if (!strncasecmp(causes[x].name, name, strlen(causes[x].name)))
999  return causes[x].cause;
1000 
1001  return -1;
1002 }
1003 
1004 /*! \brief Gives the string form of a given channel state.
1005  \note This function is not reentrant.
1006  */
1008 {
1009  char *buf;
1010 
1011  switch (state) {
1012  case AST_STATE_DOWN:
1013  return "Down";
1014  case AST_STATE_RESERVED:
1015  return "Rsrvd";
1016  case AST_STATE_OFFHOOK:
1017  return "OffHook";
1018  case AST_STATE_DIALING:
1019  return "Dialing";
1020  case AST_STATE_RING:
1021  return "Ring";
1022  case AST_STATE_RINGING:
1023  return "Ringing";
1024  case AST_STATE_UP:
1025  return "Up";
1026  case AST_STATE_BUSY:
1027  return "Busy";
1029  return "Dialing Offhook";
1030  case AST_STATE_PRERING:
1031  return "Pre-ring";
1032  default:
1034  return "Unknown";
1035  snprintf(buf, STATE2STR_BUFSIZE, "Unknown (%u)", state);
1036  return buf;
1037  }
1038 }
1039 
1040 /*! \brief Gives the string form of a given transfer capability */
1041 char *ast_transfercapability2str(int transfercapability)
1042 {
1043  switch (transfercapability) {
1044  case AST_TRANS_CAP_SPEECH:
1045  return "SPEECH";
1046  case AST_TRANS_CAP_DIGITAL:
1047  return "DIGITAL";
1049  return "RESTRICTED_DIGITAL";
1051  return "3K1AUDIO";
1053  return "DIGITAL_W_TONES";
1054  case AST_TRANS_CAP_VIDEO:
1055  return "VIDEO";
1056  default:
1057  return "UNKNOWN";
1058  }
1059 }
1060 
1061 /*! \brief Pick the best audio codec */
1063 {
1064  /* This just our opinion, expressed in code. We are asked to choose
1065  the best codec to use, given no information */
1066  int x;
1067  static const format_t prefs[] =
1068  {
1069  /*! Okay, ulaw is used by all telephony equipment, so start with it */
1071  /*! Unless of course, you're a silly European, so then prefer ALAW */
1077  /*! G.722 is better then all below, but not as common as the above... so give ulaw and alaw priority */
1079  /*! Okay, well, signed linear is easy to translate into other stuff */
1082  /*! G.726 is standard ADPCM, in RFC3551 packing order */
1084  /*! G.726 is standard ADPCM, in AAL2 packing order */
1086  /*! ADPCM has great sound quality and is still pretty easy to translate */
1088  /*! Okay, we're down to vocoders now, so pick GSM because it's small and easier to
1089  translate and sounds pretty good */
1091  /*! iLBC is not too bad */
1093  /*! Speex is free, but computationally more expensive than GSM */
1096  /*! Ick, LPC10 sounds terrible, but at least we have code for it, if you're tacky enough
1097  to use it */
1099  /*! G.729a is faster than 723 and slightly less expensive */
1101  /*! Down to G.723.1 which is proprietary but at least designed for voice */
1103  };
1104  char buf[512];
1105 
1106  /* Strip out video */
1107  fmts &= AST_FORMAT_AUDIO_MASK;
1108 
1109  /* Find the first preferred codec in the format given */
1110  for (x = 0; x < ARRAY_LEN(prefs); x++) {
1111  if (fmts & prefs[x])
1112  return prefs[x];
1113  }
1114 
1115  ast_log(LOG_WARNING, "Don't know any of %s formats\n", ast_getformatname_multiple(buf, sizeof(buf), fmts));
1116 
1117  return 0;
1118 }
1119 
1120 static const struct ast_channel_tech null_tech = {
1121  .type = "NULL",
1122  .description = "Null channel (should not see this)",
1123 };
1124 
1125 static void ast_channel_destructor(void *obj);
1126 static void ast_dummy_channel_destructor(void *obj);
1127 
1128 /*! \brief Create a new channel structure */
1129 static struct ast_channel * attribute_malloc __attribute__((format(printf, 13, 0)))
1130 __ast_channel_alloc_ap(int needqueue, int state, const char *cid_num, const char *cid_name,
1131  const char *acctcode, const char *exten, const char *context,
1132  const char *linkedid, const int amaflag, const char *file, int line,
1133  const char *function, const char *name_fmt, va_list ap1, va_list ap2)
1134 {
1135  struct ast_channel *tmp;
1136  int x;
1137  int flags;
1138  struct varshead *headp;
1139  char *tech = "", *tech2 = NULL;
1140 
1141  /* If shutting down, don't allocate any new channels */
1142  if (shutting_down) {
1143  ast_log(LOG_WARNING, "Channel allocation failed: Refusing due to active shutdown\n");
1144  return NULL;
1145  }
1146 
1147 #if defined(REF_DEBUG)
1148  tmp = __ao2_alloc_debug(sizeof(*tmp), ast_channel_destructor, "", file, line,
1149  function, 1);
1150 #elif defined(__AST_DEBUG_MALLOC)
1151  tmp = __ao2_alloc_debug(sizeof(*tmp), ast_channel_destructor, "", file, line,
1152  function, 0);
1153 #else
1154  tmp = ao2_alloc(sizeof(*tmp), ast_channel_destructor);
1155 #endif
1156  if (!tmp) {
1157  /* Channel structure allocation failure. */
1158  return NULL;
1159  }
1160 
1161  /*
1162  * Init file descriptors to unopened state so
1163  * the destructor can know not to close them.
1164  */
1165  tmp->timingfd = -1;
1166  for (x = 0; x < ARRAY_LEN(tmp->alertpipe); ++x) {
1167  tmp->alertpipe[x] = -1;
1168  }
1169  for (x = 0; x < ARRAY_LEN(tmp->fds); ++x) {
1170  tmp->fds[x] = -1;
1171  }
1172 #ifdef HAVE_EPOLL
1173  tmp->epfd = epoll_create(25);
1174 #endif
1175 
1176  if (!(tmp->sched = sched_context_create())) {
1177  ast_log(LOG_WARNING, "Channel allocation failed: Unable to create schedule context\n");
1178  return ast_channel_unref(tmp);
1179  }
1180 
1185 
1186  if (cid_name) {
1187  tmp->caller.id.name.valid = 1;
1188  tmp->caller.id.name.str = ast_strdup(cid_name);
1189  if (!tmp->caller.id.name.str) {
1190  return ast_channel_unref(tmp);
1191  }
1192  }
1193  if (cid_num) {
1194  tmp->caller.id.number.valid = 1;
1195  tmp->caller.id.number.str = ast_strdup(cid_num);
1196  if (!tmp->caller.id.number.str) {
1197  return ast_channel_unref(tmp);
1198  }
1199  }
1200 
1201  if ((tmp->timer = ast_timer_open())) {
1202  if (strcmp(ast_timer_get_name(tmp->timer), "timerfd")) {
1203  needqueue = 0;
1204  }
1205  tmp->timingfd = ast_timer_fd(tmp->timer);
1206  }
1207 
1208  if (needqueue) {
1209  if (pipe(tmp->alertpipe)) {
1210  ast_log(LOG_WARNING, "Channel allocation failed: Can't create alert pipe! Try increasing max file descriptors with ulimit -n\n");
1211  return ast_channel_unref(tmp);
1212  } else {
1213  flags = fcntl(tmp->alertpipe[0], F_GETFL);
1214  if (fcntl(tmp->alertpipe[0], F_SETFL, flags | O_NONBLOCK) < 0) {
1215  ast_log(LOG_WARNING, "Channel allocation failed: Unable to set alertpipe nonblocking! (%d: %s)\n", errno, strerror(errno));
1216  return ast_channel_unref(tmp);
1217  }
1218  flags = fcntl(tmp->alertpipe[1], F_GETFL);
1219  if (fcntl(tmp->alertpipe[1], F_SETFL, flags | O_NONBLOCK) < 0) {
1220  ast_log(LOG_WARNING, "Channel allocation failed: Unable to set alertpipe nonblocking! (%d: %s)\n", errno, strerror(errno));
1221  return ast_channel_unref(tmp);
1222  }
1223  }
1224  }
1225 
1226  /*
1227  * This is the last place the channel constructor can fail.
1228  *
1229  * The destructor takes advantage of this fact to ensure that the
1230  * AST_CEL_CHANNEL_END is not posted if we have not posted the
1231  * AST_CEL_CHANNEL_START yet.
1232  */
1233  if ((ast_string_field_init(tmp, 128))) {
1234  return ast_channel_unref(tmp);
1235  }
1236 
1237  /* Always watch the alertpipe */
1238  ast_channel_set_fd(tmp, AST_ALERT_FD, tmp->alertpipe[0]);
1239  /* And timing pipe */
1241 
1242  /* Initial state */
1243  tmp->_state = state;
1244 
1245  tmp->streamid = -1;
1246 
1247  tmp->fin = global_fin;
1248  tmp->fout = global_fout;
1249 
1251  ast_string_field_build(tmp, uniqueid, "%li.%d", (long) time(NULL),
1252  ast_atomic_fetchadd_int(&uniqueint, 1));
1253  } else {
1254  ast_string_field_build(tmp, uniqueid, "%s-%li.%d", ast_config_AST_SYSTEM_NAME,
1255  (long) time(NULL), ast_atomic_fetchadd_int(&uniqueint, 1));
1256  }
1257 
1258  if (!ast_strlen_zero(linkedid)) {
1259  ast_string_field_set(tmp, linkedid, linkedid);
1260  } else {
1261  ast_string_field_set(tmp, linkedid, tmp->uniqueid);
1262  }
1263 
1264  if (!ast_strlen_zero(name_fmt)) {
1265  char *slash, *slash2;
1266  /* Almost every channel is calling this function, and setting the name via the ast_string_field_build() call.
1267  * And they all use slightly different formats for their name string.
1268  * This means, to set the name here, we have to accept variable args, and call the string_field_build from here.
1269  * This means, that the stringfields must have a routine that takes the va_lists directly, and
1270  * uses them to build the string, instead of forming the va_lists internally from the vararg ... list.
1271  * This new function was written so this can be accomplished.
1272  */
1273  ast_string_field_build_va(tmp, name, name_fmt, ap1, ap2);
1274  tech = ast_strdupa(tmp->name);
1275  if ((slash = strchr(tech, '/'))) {
1276  if ((slash2 = strchr(slash + 1, '/'))) {
1277  tech2 = slash + 1;
1278  *slash2 = '\0';
1279  }
1280  *slash = '\0';
1281  }
1282  } else {
1283  /*
1284  * Start the string with '-' so it becomes an empty string
1285  * in the destructor.
1286  */
1287  ast_string_field_set(tmp, name, "-**Unknown**");
1288  }
1289 
1290  /* Reminder for the future: under what conditions do we NOT want to track cdrs on channels? */
1291 
1292  /* These 4 variables need to be set up for the cdr_init() to work right */
1293  if (amaflag)
1294  tmp->amaflags = amaflag;
1295  else
1297 
1298  if (!ast_strlen_zero(acctcode))
1299  ast_string_field_set(tmp, accountcode, acctcode);
1300  else
1302 
1303  if (!ast_strlen_zero(context))
1304  ast_copy_string(tmp->context, context, sizeof(tmp->context));
1305  else
1306  strcpy(tmp->context, "default");
1307 
1308  if (!ast_strlen_zero(exten))
1309  ast_copy_string(tmp->exten, exten, sizeof(tmp->exten));
1310  else
1311  strcpy(tmp->exten, "s");
1312 
1313  tmp->priority = 1;
1314 
1315  tmp->cdr = ast_cdr_alloc();
1316  ast_cdr_init(tmp->cdr, tmp);
1317  ast_cdr_start(tmp->cdr);
1318 
1319  ast_atomic_fetchadd_int(&chancount, +1);
1320  ast_cel_report_event(tmp, AST_CEL_CHANNEL_START, NULL, NULL, NULL);
1321 
1322  headp = &tmp->varshead;
1324 
1326 
1328 
1330 
1331  tmp->tech = &null_tech;
1332 
1333  ao2_link(channels, tmp);
1334 
1335  /*
1336  * And now, since the channel structure is built, and has its name, let's
1337  * call the manager event generator with this Newchannel event. This is the
1338  * proper and correct place to make this call, but you sure do have to pass
1339  * a lot of data into this func to do it here!
1340  */
1341  if (ast_get_channel_tech(tech) || (tech2 && ast_get_channel_tech(tech2))) {
1342  ast_manager_event(tmp, EVENT_FLAG_CALL, "Newchannel",
1343  "Channel: %s\r\n"
1344  "ChannelState: %d\r\n"
1345  "ChannelStateDesc: %s\r\n"
1346  "CallerIDNum: %s\r\n"
1347  "CallerIDName: %s\r\n"
1348  "AccountCode: %s\r\n"
1349  "Exten: %s\r\n"
1350  "Context: %s\r\n"
1351  "Uniqueid: %s\r\n",
1352  tmp->name,
1353  state,
1354  ast_state2str(state),
1355  S_OR(cid_num, ""),
1356  S_OR(cid_name, ""),
1357  tmp->accountcode,
1358  S_OR(exten, ""),
1359  S_OR(context, ""),
1360  tmp->uniqueid);
1361  }
1362 
1363  return tmp;
1364 }
1365 
1366 struct ast_channel *__ast_channel_alloc(int needqueue, int state, const char *cid_num,
1367  const char *cid_name, const char *acctcode,
1368  const char *exten, const char *context,
1369  const char *linkedid, const int amaflag,
1370  const char *file, int line, const char *function,
1371  const char *name_fmt, ...)
1372 {
1373  va_list ap1, ap2;
1374  struct ast_channel *result;
1375 
1376  va_start(ap1, name_fmt);
1377  va_start(ap2, name_fmt);
1378  result = __ast_channel_alloc_ap(needqueue, state, cid_num, cid_name, acctcode, exten, context,
1379  linkedid, amaflag, file, line, function, name_fmt, ap1, ap2);
1380  va_end(ap1);
1381  va_end(ap2);
1382 
1383  return result;
1384 }
1385 
1386 /* only do the minimum amount of work needed here to make a channel
1387  * structure that can be used to expand channel vars */
1388 #if defined(REF_DEBUG) || defined(__AST_DEBUG_MALLOC)
1389 struct ast_channel *__ast_dummy_channel_alloc(const char *file, int line, const char *function)
1390 #else
1392 #endif
1393 {
1394  struct ast_channel *tmp;
1395  struct varshead *headp;
1396  int x;
1397 
1398 #if defined(REF_DEBUG)
1399  tmp = __ao2_alloc_debug(sizeof(*tmp), ast_dummy_channel_destructor, "dummy channel",
1400  file, line, function, 1);
1401 #elif defined(__AST_DEBUG_MALLOC)
1402  tmp = __ao2_alloc_debug(sizeof(*tmp), ast_dummy_channel_destructor, "dummy channel",
1403  file, line, function, 0);
1404 #else
1405  tmp = ao2_alloc(sizeof(*tmp), ast_dummy_channel_destructor);
1406 #endif
1407  if (!tmp) {
1408  /* Dummy channel structure allocation failure. */
1409  return NULL;
1410  }
1411 
1412  if ((ast_string_field_init(tmp, 128))) {
1413  return ast_channel_unref(tmp);
1414  }
1415 
1416  /*
1417  * Init file descriptors to unopened state just in case
1418  * autoservice is called on the channel or something tries to
1419  * read a frame from it.
1420  */
1421  tmp->timingfd = -1;
1422  for (x = 0; x < ARRAY_LEN(tmp->alertpipe); ++x) {
1423  tmp->alertpipe[x] = -1;
1424  }
1425  for (x = 0; x < ARRAY_LEN(tmp->fds); ++x) {
1426  tmp->fds[x] = -1;
1427  }
1428 #ifdef HAVE_EPOLL
1429  tmp->epfd = -1;
1430 #endif
1431 
1432  headp = &tmp->varshead;
1434 
1435  return tmp;
1436 }
1437 
1438 static int __ast_queue_frame(struct ast_channel *chan, struct ast_frame *fin, int head, struct ast_frame *after)
1439 {
1440  struct ast_frame *f;
1441  struct ast_frame *cur;
1442  unsigned int new_frames = 0;
1443  unsigned int new_voice_frames = 0;
1444  unsigned int queued_frames = 0;
1445  unsigned int queued_voice_frames = 0;
1447 
1448  ast_channel_lock(chan);
1449 
1450  /*
1451  * Check the last frame on the queue if we are queuing the new
1452  * frames after it.
1453  */
1454  cur = AST_LIST_LAST(&chan->readq);
1455  if (cur && cur->frametype == AST_FRAME_CONTROL && !head && (!after || after == cur)) {
1456  switch (cur->subclass.integer) {
1457  case AST_CONTROL_END_OF_Q:
1458  if (fin->frametype == AST_FRAME_CONTROL
1459  && fin->subclass.integer == AST_CONTROL_HANGUP) {
1460  /*
1461  * Destroy the end-of-Q marker frame so we can queue the hangup
1462  * frame in its place.
1463  */
1464  AST_LIST_REMOVE(&chan->readq, cur, frame_list);
1465  ast_frfree(cur);
1466 
1467  /*
1468  * This has degenerated to a normal queue append anyway. Since
1469  * we just destroyed the last frame in the queue we must make
1470  * sure that "after" is NULL or bad things will happen.
1471  */
1472  after = NULL;
1473  break;
1474  }
1475  /* Fall through */
1476  case AST_CONTROL_HANGUP:
1477  /* Don't queue anything. */
1478  ast_channel_unlock(chan);
1479  return 0;
1480  default:
1481  break;
1482  }
1483  }
1484 
1485  /* Build copies of all the new frames and count them */
1487  for (cur = fin; cur; cur = AST_LIST_NEXT(cur, frame_list)) {
1488  if (!(f = ast_frdup(cur))) {
1489  if (AST_LIST_FIRST(&frames)) {
1491  }
1492  ast_channel_unlock(chan);
1493  return -1;
1494  }
1495 
1497  new_frames++;
1498  if (f->frametype == AST_FRAME_VOICE) {
1499  new_voice_frames++;
1500  }
1501  }
1502 
1503  /* Count how many frames exist on the queue */
1504  AST_LIST_TRAVERSE(&chan->readq, cur, frame_list) {
1505  queued_frames++;
1506  if (cur->frametype == AST_FRAME_VOICE) {
1507  queued_voice_frames++;
1508  }
1509  }
1510 
1511  if ((queued_frames + new_frames > 128 || queued_voice_frames + new_voice_frames > 96)) {
1512  int count = 0;
1513  ast_log(LOG_WARNING, "Exceptionally long %squeue length queuing to %s\n", queued_frames + new_frames > 128 ? "" : "voice ", chan->name);
1515  /* Save the most recent frame */
1516  if (!AST_LIST_NEXT(cur, frame_list)) {
1517  break;
1518  } else if (cur->frametype == AST_FRAME_VOICE || cur->frametype == AST_FRAME_VIDEO || cur->frametype == AST_FRAME_NULL) {
1519  if (++count > 64) {
1520  break;
1521  }
1523  ast_frfree(cur);
1524  }
1525  }
1527  }
1528 
1529  if (after) {
1531  } else {
1532  if (head) {
1535  }
1537  }
1538 
1539  if (chan->alertpipe[1] > -1) {
1540  int blah[new_frames];
1541 
1542  memset(blah, 1, sizeof(blah));
1543  if (write(chan->alertpipe[1], &blah, sizeof(blah)) != (sizeof(blah))) {
1544  ast_log(LOG_WARNING, "Unable to write to alert pipe on %s (qlen = %u): %s!\n",
1545  chan->name, queued_frames, strerror(errno));
1546  }
1547  } else if (chan->timingfd > -1) {
1549  } else if (ast_test_flag(chan, AST_FLAG_BLOCKING)) {
1550  pthread_kill(chan->blocker, SIGURG);
1551  }
1552 
1553  ast_channel_unlock(chan);
1554 
1555  return 0;
1556 }
1557 
1558 int ast_queue_frame(struct ast_channel *chan, struct ast_frame *fin)
1559 {
1560  return __ast_queue_frame(chan, fin, 0, NULL);
1561 }
1562 
1563 int ast_queue_frame_head(struct ast_channel *chan, struct ast_frame *fin)
1564 {
1565  return __ast_queue_frame(chan, fin, 1, NULL);
1566 }
1567 
1568 /*! \brief Queue a hangup frame for channel */
1570 {
1571  struct ast_frame f = { AST_FRAME_CONTROL, .subclass.integer = AST_CONTROL_HANGUP };
1572  /* Yeah, let's not change a lock-critical value without locking */
1573  if (!ast_channel_trylock(chan)) {
1575  ast_channel_unlock(chan);
1576  }
1577  return ast_queue_frame(chan, &f);
1578 }
1579 
1580 /*! \brief Queue a hangup frame for channel */
1581 int ast_queue_hangup_with_cause(struct ast_channel *chan, int cause)
1582 {
1583  struct ast_frame f = { AST_FRAME_CONTROL, .subclass.integer = AST_CONTROL_HANGUP };
1584 
1585  if (cause >= 0)
1586  f.data.uint32 = cause;
1587 
1588  /* Yeah, let's not change a lock-critical value without locking */
1589  if (!ast_channel_trylock(chan)) {
1591  if (cause < 0)
1592  f.data.uint32 = chan->hangupcause;
1593 
1594  ast_channel_unlock(chan);
1595  }
1596 
1597  return ast_queue_frame(chan, &f);
1598 }
1599 
1600 /*! \brief Queue a control frame */
1602 {
1603  struct ast_frame f = { AST_FRAME_CONTROL, .subclass.integer = control };
1604  return ast_queue_frame(chan, &f);
1605 }
1606 
1607 /*! \brief Queue a control frame with payload */
1609  const void *data, size_t datalen)
1610 {
1611  struct ast_frame f = { AST_FRAME_CONTROL, .subclass.integer = control, .data.ptr = (void *) data, .datalen = datalen };
1612  return ast_queue_frame(chan, &f);
1613 }
1614 
1615 /*! \brief Set defer DTMF flag on channel */
1617 {
1618  int pre = 0;
1619 
1620  if (chan) {
1621  pre = ast_test_flag(chan, AST_FLAG_DEFER_DTMF);
1623  }
1624  return pre;
1625 }
1626 
1627 /*! \brief Unset defer DTMF flag on channel */
1629 {
1630  if (chan)
1632 }
1633 
1635  void *data, int ao2_flags)
1636 {
1637  return ao2_callback_data(channels, ao2_flags, cb_fn, arg, data);
1638 }
1639 
1641  /* storage for non-dynamically allocated iterator */
1642  struct ao2_iterator simple_iterator;
1643  /* pointer to the actual iterator (simple_iterator or a dynamically
1644  * allocated iterator)
1645  */
1647 };
1648 
1650 {
1652  ast_free(i);
1653 
1654  return NULL;
1655 }
1656 
1658  size_t name_len, const char *exten,
1659  const char *context)
1660 {
1661  struct ast_channel_iterator *i;
1662  struct ast_channel tmp_chan = {
1663  .name = name,
1664  /* This is sort of a hack. Basically, we're using an arbitrary field
1665  * in ast_channel to pass the name_len for a prefix match. If this
1666  * gets changed, then the compare callback must be changed, too. */
1667  .rings = name_len,
1668  };
1669 
1670  if (!(i = ast_calloc(1, sizeof(*i)))) {
1671  return NULL;
1672  }
1673 
1674  if (exten) {
1675  ast_copy_string(tmp_chan.exten, exten, sizeof(tmp_chan.exten));
1676  }
1677 
1678  if (context) {
1679  ast_copy_string(tmp_chan.context, context, sizeof(tmp_chan.context));
1680  }
1681 
1682  if (!(i->active_iterator = ao2_find(channels, &tmp_chan,
1683  OBJ_MULTIPLE | ((!ast_strlen_zero(name) && (name_len == 0)) ? OBJ_POINTER : 0)))) {
1684  ast_free(i);
1685  return NULL;
1686  }
1687 
1688  return i;
1689 }
1690 
1692 {
1693  return channel_iterator_search(NULL, 0, exten, context);
1694 }
1695 
1696 struct ast_channel_iterator *ast_channel_iterator_by_name_new(const char *name, size_t name_len)
1697 {
1698  return channel_iterator_search(name, name_len, NULL, NULL);
1699 }
1700 
1702 {
1703  struct ast_channel_iterator *i;
1704 
1705  if (!(i = ast_calloc(1, sizeof(*i)))) {
1706  return NULL;
1707  }
1708 
1711 
1712  return i;
1713 }
1714 
1716 {
1718 }
1719 
1720 static int ast_channel_cmp_cb(void *obj, void *arg, int flags)
1721 {
1722  struct ast_channel *chan = obj, *cmp_args = arg;
1723  size_t name_len;
1724  int ret = CMP_MATCH;
1725 
1726  /* This is sort of a hack. Basically, we're using an arbitrary field
1727  * in ast_channel to pass the name_len for a prefix match. If this
1728  * gets changed, then the uses of ao2_find() must be changed, too. */
1729  name_len = cmp_args->rings;
1730 
1731  ast_channel_lock(chan);
1732 
1733  if (!ast_strlen_zero(cmp_args->name)) { /* match by name */
1734  if ((!name_len && strcasecmp(chan->name, cmp_args->name)) ||
1735  (name_len && strncasecmp(chan->name, cmp_args->name, name_len))) {
1736  ret = 0; /* name match failed */
1737  }
1738  } else if (!ast_strlen_zero(cmp_args->exten)) {
1739  if (cmp_args->context && strcasecmp(chan->context, cmp_args->context) &&
1740  strcasecmp(chan->macrocontext, cmp_args->context)) {
1741  ret = 0; /* context match failed */
1742  }
1743  if (ret && strcasecmp(chan->exten, cmp_args->exten) &&
1744  strcasecmp(chan->macroexten, cmp_args->exten)) {
1745  ret = 0; /* exten match failed */
1746  }
1747  } else if (!ast_strlen_zero(cmp_args->uniqueid)) {
1748  if ((!name_len && strcasecmp(chan->uniqueid, cmp_args->uniqueid)) ||
1749  (name_len && strncasecmp(chan->uniqueid, cmp_args->uniqueid, name_len))) {
1750  ret = 0; /* uniqueid match failed */
1751  }
1752  } else {
1753  ret = 0;
1754  }
1755 
1756  ast_channel_unlock(chan);
1757 
1758  return ret;
1759 }
1760 
1761 static struct ast_channel *ast_channel_get_full(const char *name, size_t name_len,
1762  const char *exten, const char *context)
1763 {
1764  struct ast_channel tmp_chan = {
1765  .name = name,
1766  /* This is sort of a hack. Basically, we're using an arbitrary field
1767  * in ast_channel to pass the name_len for a prefix match. If this
1768  * gets changed, then the compare callback must be changed, too. */
1769  .rings = name_len,
1770  };
1771  struct ast_channel *chan;
1772 
1773  if (exten) {
1774  ast_copy_string(tmp_chan.exten, exten, sizeof(tmp_chan.exten));
1775  }
1776 
1777  if (context) {
1778  ast_copy_string(tmp_chan.context, context, sizeof(tmp_chan.context));
1779  }
1780 
1781  if ((chan = ao2_find(channels, &tmp_chan,
1782  (!ast_strlen_zero(name) && (name_len == 0)) ? OBJ_POINTER : 0))) {
1783  return chan;
1784  }
1785 
1786  if (!name) {
1787  return NULL;
1788  }
1789 
1790  /* If name was specified, but the result was NULL,
1791  * try a search on uniqueid, instead. */
1792 
1793  {
1794  struct ast_channel tmp_chan2 = {
1795  .uniqueid = name,
1796  .rings = name_len,
1797  };
1798 
1799  return ao2_find(channels, &tmp_chan2, 0);
1800  }
1801 }
1802 
1804 {
1805  return ast_channel_get_full(name, 0, NULL, NULL);
1806 }
1807 
1808 struct ast_channel *ast_channel_get_by_name_prefix(const char *name, size_t name_len)
1809 {
1810  return ast_channel_get_full(name, name_len, NULL, NULL);
1811 }
1812 
1813 struct ast_channel *ast_channel_get_by_exten(const char *exten, const char *context)
1814 {
1815  return ast_channel_get_full(NULL, 0, exten, context);
1816 }
1817 
1818 int ast_is_deferrable_frame(const struct ast_frame *frame)
1819 {
1820  /* Do not add a default entry in this switch statement. Each new
1821  * frame type should be addressed directly as to whether it should
1822  * be queued up or not.
1823  */
1824  switch (frame->frametype) {
1825  case AST_FRAME_CONTROL:
1826  case AST_FRAME_TEXT:
1827  case AST_FRAME_IMAGE:
1828  case AST_FRAME_HTML:
1829  return 1;
1830 
1831  case AST_FRAME_DTMF_END:
1832  case AST_FRAME_DTMF_BEGIN:
1833  case AST_FRAME_VOICE:
1834  case AST_FRAME_VIDEO:
1835  case AST_FRAME_NULL:
1836  case AST_FRAME_IAX:
1837  case AST_FRAME_CNG:
1838  case AST_FRAME_MODEM:
1839  return 0;
1840  }
1841  return 0;
1842 }
1843 
1844 /*! \brief Wait, look for hangups and condition arg */
1845 int ast_safe_sleep_conditional(struct ast_channel *chan, int timeout_ms, int (*cond)(void*), void *data)
1846 {
1847  struct ast_frame *f;
1848  struct ast_silence_generator *silgen = NULL;
1849  int res = 0;
1850  struct timeval start;
1851  int ms;
1852  AST_LIST_HEAD_NOLOCK(, ast_frame) deferred_frames;
1853 
1854  AST_LIST_HEAD_INIT_NOLOCK(&deferred_frames);
1855 
1856  /* If no other generator is present, start silencegen while waiting */
1857  if (ast_opt_transmit_silence && !chan->generatordata) {
1858  silgen = ast_channel_start_silence_generator(chan);
1859  }
1860 
1861  start = ast_tvnow();
1862  while ((ms = ast_remaining_ms(start, timeout_ms))) {
1863  struct ast_frame *dup_f = NULL;
1864 
1865  if (cond && ((*cond)(data) == 0)) {
1866  break;
1867  }
1868  ms = ast_waitfor(chan, ms);
1869  if (ms < 0) {
1870  res = -1;
1871  break;
1872  }
1873  if (ms > 0) {
1874  f = ast_read(chan);
1875  if (!f) {
1876  res = -1;
1877  break;
1878  }
1879 
1880  if (!ast_is_deferrable_frame(f)) {
1881  ast_frfree(f);
1882  continue;
1883  }
1884 
1885  if ((dup_f = ast_frisolate(f))) {
1886  if (dup_f != f) {
1887  ast_frfree(f);
1888  }
1889  AST_LIST_INSERT_HEAD(&deferred_frames, dup_f, frame_list);
1890  }
1891  }
1892  }
1893 
1894  /* stop silgen if present */
1895  if (silgen) {
1896  ast_channel_stop_silence_generator(chan, silgen);
1897  }
1898 
1899  /* We need to free all the deferred frames, but we only need to
1900  * queue the deferred frames if there was no error and no
1901  * hangup was received
1902  */
1903  ast_channel_lock(chan);
1904  while ((f = AST_LIST_REMOVE_HEAD(&deferred_frames, frame_list))) {
1905  if (!res) {
1906  ast_queue_frame_head(chan, f);
1907  }
1908  ast_frfree(f);
1909  }
1910  ast_channel_unlock(chan);
1911 
1912  return res;
1913 }
1914 
1915 /*! \brief Wait, look for hangups */
1916 int ast_safe_sleep(struct ast_channel *chan, int ms)
1917 {
1918  return ast_safe_sleep_conditional(chan, ms, NULL, NULL);
1919 }
1920 
1922 {
1923  /* Safe, even if already unlinked. */
1924  ao2_unlink(channels, chan);
1925  return ast_channel_unref(chan);
1926 }
1927 
1929 {
1930  init->str = NULL;
1933  init->valid = 0;
1934 }
1935 
1936 void ast_party_name_copy(struct ast_party_name *dest, const struct ast_party_name *src)
1937 {
1938  if (dest == src) {
1939  /* Don't copy to self */
1940  return;
1941  }
1942 
1943  ast_free(dest->str);
1944  dest->str = ast_strdup(src->str);
1945  dest->char_set = src->char_set;
1946  dest->presentation = src->presentation;
1947  dest->valid = src->valid;
1948 }
1949 
1950 void ast_party_name_set_init(struct ast_party_name *init, const struct ast_party_name *guide)
1951 {
1952  init->str = NULL;
1953  init->char_set = guide->char_set;
1954  init->presentation = guide->presentation;
1955  init->valid = guide->valid;
1956 }
1957 
1958 void ast_party_name_set(struct ast_party_name *dest, const struct ast_party_name *src)
1959 {
1960  if (dest == src) {
1961  /* Don't set to self */
1962  return;
1963  }
1964 
1965  if (src->str && src->str != dest->str) {
1966  ast_free(dest->str);
1967  dest->str = ast_strdup(src->str);
1968  }
1969 
1970  dest->char_set = src->char_set;
1971  dest->presentation = src->presentation;
1972  dest->valid = src->valid;
1973 }
1974 
1976 {
1977  ast_free(doomed->str);
1978  doomed->str = NULL;
1979 }
1980 
1982 {
1983  init->str = NULL;
1984  init->plan = 0;/* Unknown */
1986  init->valid = 0;
1987 }
1988 
1989 void ast_party_number_copy(struct ast_party_number *dest, const struct ast_party_number *src)
1990 {
1991  if (dest == src) {
1992  /* Don't copy to self */
1993  return;
1994  }
1995 
1996  ast_free(dest->str);
1997  dest->str = ast_strdup(src->str);
1998  dest->plan = src->plan;
1999  dest->presentation = src->presentation;
2000  dest->valid = src->valid;
2001 }
2002 
2003 void ast_party_number_set_init(struct ast_party_number *init, const struct ast_party_number *guide)
2004 {
2005  init->str = NULL;
2006  init->plan = guide->plan;
2007  init->presentation = guide->presentation;
2008  init->valid = guide->valid;
2009 }
2010 
2011 void ast_party_number_set(struct ast_party_number *dest, const struct ast_party_number *src)
2012 {
2013  if (dest == src) {
2014  /* Don't set to self */
2015  return;
2016  }
2017 
2018  if (src->str && src->str != dest->str) {
2019  ast_free(dest->str);
2020  dest->str = ast_strdup(src->str);
2021  }
2022 
2023  dest->plan = src->plan;
2024  dest->presentation = src->presentation;
2025  dest->valid = src->valid;
2026 }
2027 
2029 {
2030  ast_free(doomed->str);
2031  doomed->str = NULL;
2032 }
2033 
2035 {
2036  init->str = NULL;
2037  init->type = 0;
2038  init->odd_even_indicator = 0;
2039  init->valid = 0;
2040 }
2041 
2043 {
2044  if (dest == src) {
2045  /* Don't copy to self */
2046  return;
2047  }
2048 
2049  ast_free(dest->str);
2050  dest->str = ast_strdup(src->str);
2051  dest->type = src->type;
2053  dest->valid = src->valid;
2054 }
2055 
2057 {
2058  init->str = NULL;
2059  init->type = guide->type;
2060  init->odd_even_indicator = guide->odd_even_indicator;
2061  init->valid = guide->valid;
2062 }
2063 
2065 {
2066  if (dest == src) {
2067  /* Don't set to self */
2068  return;
2069  }
2070 
2071  if (src->str && src->str != dest->str) {
2072  ast_free(dest->str);
2073  dest->str = ast_strdup(src->str);
2074  }
2075 
2076  dest->type = src->type;
2078  dest->valid = src->valid;
2079 }
2080 
2082 {
2083  ast_free(doomed->str);
2084  doomed->str = NULL;
2085 }
2086 
2088 {
2089  ast_party_name_init(&init->name);
2090  ast_party_number_init(&init->number);
2092  init->tag = NULL;
2093 }
2094 
2095 void ast_party_id_copy(struct ast_party_id *dest, const struct ast_party_id *src)
2096 {
2097  if (dest == src) {
2098  /* Don't copy to self */
2099  return;
2100  }
2101 
2102  ast_party_name_copy(&dest->name, &src->name);
2103  ast_party_number_copy(&dest->number, &src->number);
2105 
2106  ast_free(dest->tag);
2107  dest->tag = ast_strdup(src->tag);
2108 }
2109 
2110 void ast_party_id_set_init(struct ast_party_id *init, const struct ast_party_id *guide)
2111 {
2112  ast_party_name_set_init(&init->name, &guide->name);
2113  ast_party_number_set_init(&init->number, &guide->number);
2115  init->tag = NULL;
2116 }
2117 
2118 void ast_party_id_set(struct ast_party_id *dest, const struct ast_party_id *src, const struct ast_set_party_id *update)
2119 {
2120  if (dest == src) {
2121  /* Don't set to self */
2122  return;
2123  }
2124 
2125  if (!update || update->name) {
2126  ast_party_name_set(&dest->name, &src->name);
2127  }
2128  if (!update || update->number) {
2129  ast_party_number_set(&dest->number, &src->number);
2130  }
2131  if (!update || update->subaddress) {
2133  }
2134 
2135  if (src->tag && src->tag != dest->tag) {
2136  ast_free(dest->tag);
2137  dest->tag = ast_strdup(src->tag);
2138  }
2139 }
2140 
2141 void ast_party_id_free(struct ast_party_id *doomed)
2142 {
2143  ast_party_name_free(&doomed->name);
2144  ast_party_number_free(&doomed->number);
2146 
2147  ast_free(doomed->tag);
2148  doomed->tag = NULL;
2149 }
2150 
2152 {
2153  int number_priority;
2154  int number_value;
2155  int number_screening;
2156  int name_priority;
2157  int name_value;
2158 
2159  /* Determine name presentation priority. */
2160  if (!id->name.valid) {
2161  name_value = AST_PRES_UNAVAILABLE;
2162  name_priority = 3;
2163  } else {
2164  name_value = id->name.presentation & AST_PRES_RESTRICTION;
2165  switch (name_value) {
2166  case AST_PRES_RESTRICTED:
2167  name_priority = 0;
2168  break;
2169  case AST_PRES_ALLOWED:
2170  name_priority = 1;
2171  break;
2172  case AST_PRES_UNAVAILABLE:
2173  name_priority = 2;
2174  break;
2175  default:
2176  name_value = AST_PRES_UNAVAILABLE;
2177  name_priority = 3;
2178  break;
2179  }
2180  }
2181 
2182  /* Determine number presentation priority. */
2183  if (!id->number.valid) {
2184  number_screening = AST_PRES_USER_NUMBER_UNSCREENED;
2185  number_value = AST_PRES_UNAVAILABLE;
2186  number_priority = 3;
2187  } else {
2188  number_screening = id->number.presentation & AST_PRES_NUMBER_TYPE;
2189  number_value = id->number.presentation & AST_PRES_RESTRICTION;
2190  switch (number_value) {
2191  case AST_PRES_RESTRICTED:
2192  number_priority = 0;
2193  break;
2194  case AST_PRES_ALLOWED:
2195  number_priority = 1;
2196  break;
2197  case AST_PRES_UNAVAILABLE:
2198  number_priority = 2;
2199  break;
2200  default:
2201  number_screening = AST_PRES_USER_NUMBER_UNSCREENED;
2202  number_value = AST_PRES_UNAVAILABLE;
2203  number_priority = 3;
2204  break;
2205  }
2206  }
2207 
2208  /* Select the wining presentation value. */
2209  if (name_priority < number_priority) {
2210  number_value = name_value;
2211  }
2212  if (number_value == AST_PRES_UNAVAILABLE) {
2214  }
2215 
2216  return number_value | number_screening;
2217 }
2218 
2220 {
2221  init->number.str = NULL;
2222  init->number.plan = 0;/* Unknown */
2224  init->transit_network_select = 0;
2225 }
2226 
2227 void ast_party_dialed_copy(struct ast_party_dialed *dest, const struct ast_party_dialed *src)
2228 {
2229  if (dest == src) {
2230  /* Don't copy to self */
2231  return;
2232  }
2233 
2234  ast_free(dest->number.str);
2235  dest->number.str = ast_strdup(src->number.str);
2236  dest->number.plan = src->number.plan;
2239 }
2240 
2241 void ast_party_dialed_set_init(struct ast_party_dialed *init, const struct ast_party_dialed *guide)
2242 {
2243  init->number.str = NULL;
2244  init->number.plan = guide->number.plan;
2247 }
2248 
2249 void ast_party_dialed_set(struct ast_party_dialed *dest, const struct ast_party_dialed *src)
2250 {
2251  if (src->number.str && src->number.str != dest->number.str) {
2252  ast_free(dest->number.str);
2253  dest->number.str = ast_strdup(src->number.str);
2254  }
2255  dest->number.plan = src->number.plan;
2256 
2258 
2260 }
2261 
2263 {
2264  ast_free(doomed->number.str);
2265  doomed->number.str = NULL;
2267 }
2268 
2270 {
2271  ast_party_id_init(&init->id);
2272  ast_party_id_init(&init->ani);
2273  init->ani2 = 0;
2274 }
2275 
2276 void ast_party_caller_copy(struct ast_party_caller *dest, const struct ast_party_caller *src)
2277 {
2278  if (dest == src) {
2279  /* Don't copy to self */
2280  return;
2281  }
2282 
2283  ast_party_id_copy(&dest->id, &src->id);
2284  ast_party_id_copy(&dest->ani, &src->ani);
2285  dest->ani2 = src->ani2;
2286 }
2287 
2288 void ast_party_caller_set_init(struct ast_party_caller *init, const struct ast_party_caller *guide)
2289 {
2290  ast_party_id_set_init(&init->id, &guide->id);
2291  ast_party_id_set_init(&init->ani, &guide->ani);
2292  init->ani2 = guide->ani2;
2293 }
2294 
2295 void ast_party_caller_set(struct ast_party_caller *dest, const struct ast_party_caller *src, const struct ast_set_party_caller *update)
2296 {
2297  ast_party_id_set(&dest->id, &src->id, update ? &update->id : NULL);
2298  ast_party_id_set(&dest->ani, &src->ani, update ? &update->ani : NULL);
2299  dest->ani2 = src->ani2;
2300 }
2301 
2303 {
2304  ast_party_id_free(&doomed->id);
2305  ast_party_id_free(&doomed->ani);
2306 }
2307 
2309 {
2310  ast_party_id_init(&init->id);
2311  ast_party_id_init(&init->ani);
2312  init->ani2 = 0;
2314 }
2315 
2317 {
2318  if (dest == src) {
2319  /* Don't copy to self */
2320  return;
2321  }
2322 
2323  ast_party_id_copy(&dest->id, &src->id);
2324  ast_party_id_copy(&dest->ani, &src->ani);
2325  dest->ani2 = src->ani2;
2326  dest->source = src->source;
2327 }
2328 
2330 {
2331  ast_party_id_set_init(&init->id, &guide->id);
2332  ast_party_id_set_init(&init->ani, &guide->ani);
2333  init->ani2 = guide->ani2;
2334  init->source = guide->source;
2335 }
2336 
2338 {
2339  ast_party_id_set(&dest->id, &src->id, update ? &update->id : NULL);
2340  ast_party_id_set(&dest->ani, &src->ani, update ? &update->ani : NULL);
2341  dest->ani2 = src->ani2;
2342  dest->source = src->source;
2343 }
2344 
2346 {
2347  connected->id = caller->id;
2348  connected->ani = caller->ani;
2349  connected->ani2 = caller->ani2;
2351 }
2352 
2354 {
2355  ast_party_id_free(&doomed->id);
2356  ast_party_id_free(&doomed->ani);
2357 }
2358 
2360 {
2361  ast_party_id_init(&init->from);
2362  ast_party_id_init(&init->to);
2363  init->count = 0;
2365 }
2366 
2368 {
2369  if (dest == src) {
2370  /* Don't copy to self */
2371  return;
2372  }
2373 
2374  ast_party_id_copy(&dest->from, &src->from);
2375  ast_party_id_copy(&dest->to, &src->to);
2376  dest->count = src->count;
2377  dest->reason = src->reason;
2378 }
2379 
2381 {
2382  ast_party_id_set_init(&init->from, &guide->from);
2383  ast_party_id_set_init(&init->to, &guide->to);
2384  init->count = guide->count;
2385  init->reason = guide->reason;
2386 }
2387 
2389 {
2390  ast_party_id_set(&dest->from, &src->from, update ? &update->from : NULL);
2391  ast_party_id_set(&dest->to, &src->to, update ? &update->to : NULL);
2392  dest->reason = src->reason;
2393  dest->count = src->count;
2394 }
2395 
2397 {
2398  ast_party_id_free(&doomed->from);
2399  ast_party_id_free(&doomed->to);
2400 }
2401 
2402 /*! \brief Free a channel structure */
2403 static void ast_channel_destructor(void *obj)
2404 {
2405  struct ast_channel *chan = obj;
2406  int fd;
2407 #ifdef HAVE_EPOLL
2408  int i;
2409 #endif
2410  struct ast_var_t *vardata;
2411  struct ast_frame *f;
2412  struct varshead *headp;
2413  struct ast_datastore *datastore;
2414  char device_name[AST_CHANNEL_NAME];
2415 
2416  if (chan->name) {
2417  /* The string fields were initialized. */
2418  ast_cel_report_event(chan, AST_CEL_CHANNEL_END, NULL, NULL, NULL);
2420  }
2421 
2422  /* Get rid of each of the data stores on the channel */
2423  ast_channel_lock(chan);
2424  while ((datastore = AST_LIST_REMOVE_HEAD(&chan->datastores, entry)))
2425  /* Free the data store */
2426  ast_datastore_free(datastore);
2427  ast_channel_unlock(chan);
2428 
2429  /* Lock and unlock the channel just to be sure nobody has it locked still
2430  due to a reference that was stored in a datastore. (i.e. app_chanspy) */
2431  ast_channel_lock(chan);
2432  ast_channel_unlock(chan);
2433 
2434  if (chan->tech_pvt) {
2435  ast_log(LOG_WARNING, "Channel '%s' may not have been hung up properly\n", chan->name);
2436  ast_free(chan->tech_pvt);
2437  }
2438 
2439  if (chan->sched)
2441 
2442  if (chan->name) {
2443  char *dashptr;
2444 
2445  /* The string fields were initialized. */
2446  ast_copy_string(device_name, chan->name, sizeof(device_name));
2447  if ((dashptr = strrchr(device_name, '-'))) {
2448  *dashptr = '\0';
2449  }
2450  } else {
2451  device_name[0] = '\0';
2452  }
2453 
2454  /* Stop monitoring */
2455  if (chan->monitor)
2456  chan->monitor->stop( chan, 0 );
2457 
2458  /* If there is native format music-on-hold state, free it */
2459  if (chan->music_state)
2460  ast_moh_cleanup(chan);
2461 
2462  /* Free translators */
2463  if (chan->readtrans)
2465  if (chan->writetrans)
2467  if (chan->pbx)
2468  ast_log(LOG_WARNING, "PBX may not have been terminated properly on '%s'\n", chan->name);
2469 
2470  ast_party_dialed_free(&chan->dialed);
2471  ast_party_caller_free(&chan->caller);
2474 
2475  /* Close pipes if appropriate */
2476  if ((fd = chan->alertpipe[0]) > -1)
2477  close(fd);
2478  if ((fd = chan->alertpipe[1]) > -1)
2479  close(fd);
2480  if (chan->timer) {
2481  ast_timer_close(chan->timer);
2482  chan->timer = NULL;
2483  }
2484 #ifdef HAVE_EPOLL
2485  for (i = 0; i < AST_MAX_FDS; i++) {
2486  if (chan->epfd_data[i])
2487  free(chan->epfd_data[i]);
2488  }
2489  close(chan->epfd);
2490 #endif
2491  while ((f = AST_LIST_REMOVE_HEAD(&chan->readq, frame_list)))
2492  ast_frfree(f);
2493 
2494  /* loop over the variables list, freeing all data and deleting list items */
2495  /* no need to lock the list, as the channel is already locked */
2496  headp = &chan->varshead;
2497  while ((vardata = AST_LIST_REMOVE_HEAD(headp, entries)))
2498  ast_var_delete(vardata);
2499 
2500  ast_app_group_discard(chan);
2501 
2502  /* Destroy the jitterbuffer */
2503  ast_jb_destroy(chan);
2504 
2505  if (chan->cdr) {
2506  ast_cdr_discard(chan->cdr);
2507  chan->cdr = NULL;
2508  }
2509 
2510  if (chan->zone) {
2511  chan->zone = ast_tone_zone_unref(chan->zone);
2512  }
2513 
2515 
2516  if (device_name[0]) {
2517  /*
2518  * We have a device name to notify of a new state.
2519  *
2520  * Queue an unknown state, because, while we know that this particular
2521  * instance is dead, we don't know the state of all other possible
2522  * instances.
2523  */
2525  }
2526  ast_atomic_fetchadd_int(&chancount, -1);
2527 }
2528 
2529 /*! \brief Free a dummy channel structure */
2530 static void ast_dummy_channel_destructor(void *obj)
2531 {
2532  struct ast_channel *chan = obj;
2533  struct ast_var_t *vardata;
2534  struct varshead *headp;
2535  struct ast_datastore *datastore;
2536 
2537  /* Get rid of each of the data stores on the channel */
2538  while ((datastore = AST_LIST_REMOVE_HEAD(&chan->datastores, entry))) {
2539  /* Free the data store */
2540  ast_datastore_free(datastore);
2541  }
2542 
2543  headp = &chan->varshead;
2544 
2545  ast_party_dialed_free(&chan->dialed);
2546  ast_party_caller_free(&chan->caller);
2549 
2550  /* loop over the variables list, freeing all data and deleting list items */
2551  /* no need to lock the list, as the channel is already locked */
2552  while ((vardata = AST_LIST_REMOVE_HEAD(headp, entries)))
2553  ast_var_delete(vardata);
2554 
2555  if (chan->cdr) {
2556  ast_cdr_discard(chan->cdr);
2557  chan->cdr = NULL;
2558  }
2559 
2561 }
2562 
2564 {
2565  return ast_datastore_alloc(info, uid);
2566 }
2567 
2569 {
2570  return ast_datastore_free(datastore);
2571 }
2572 
2574 {
2575  struct ast_datastore *datastore = NULL, *datastore2;
2576 
2577  AST_LIST_TRAVERSE(&from->datastores, datastore, entry) {
2578  if (datastore->inheritance > 0) {
2579  datastore2 = ast_datastore_alloc(datastore->info, datastore->uid);
2580  if (datastore2) {
2581  datastore2->data = datastore->info->duplicate ? datastore->info->duplicate(datastore->data) : NULL;
2582  datastore2->inheritance = datastore->inheritance == DATASTORE_INHERIT_FOREVER ? DATASTORE_INHERIT_FOREVER : datastore->inheritance - 1;
2583  AST_LIST_INSERT_TAIL(&to->datastores, datastore2, entry);
2584  }
2585  }
2586  }
2587  return 0;
2588 }
2589 
2590 int ast_channel_datastore_add(struct ast_channel *chan, struct ast_datastore *datastore)
2591 {
2592  int res = 0;
2593 
2594  AST_LIST_INSERT_HEAD(&chan->datastores, datastore, entry);
2595 
2596  return res;
2597 }
2598 
2599 int ast_channel_datastore_remove(struct ast_channel *chan, struct ast_datastore *datastore)
2600 {
2601  return AST_LIST_REMOVE(&chan->datastores, datastore, entry) ? 0 : -1;
2602 }
2603 
2604 struct ast_datastore *ast_channel_datastore_find(struct ast_channel *chan, const struct ast_datastore_info *info, const char *uid)
2605 {
2606  struct ast_datastore *datastore = NULL;
2607 
2608  if (info == NULL)
2609  return NULL;
2610 
2611  AST_LIST_TRAVERSE(&chan->datastores, datastore, entry) {
2612  if (datastore->info != info) {
2613  continue;
2614  }
2615 
2616  if (uid == NULL) {
2617  /* matched by type only */
2618  break;
2619  }
2620 
2621  if ((datastore->uid != NULL) && !strcasecmp(uid, datastore->uid)) {
2622  /* Matched by type AND uid */
2623  break;
2624  }
2625  }
2626 
2627  return datastore;
2628 }
2629 
2630 /*! Set the file descriptor on the channel */
2631 void ast_channel_set_fd(struct ast_channel *chan, int which, int fd)
2632 {
2633 #ifdef HAVE_EPOLL
2634  struct epoll_event ev;
2635  struct ast_epoll_data *aed = NULL;
2636 
2637  if (chan->fds[which] > -1) {
2638  epoll_ctl(chan->epfd, EPOLL_CTL_DEL, chan->fds[which], &ev);
2639  aed = chan->epfd_data[which];
2640  }
2641 
2642  /* If this new fd is valid, add it to the epoll */
2643  if (fd > -1) {
2644  if (!aed && (!(aed = ast_calloc(1, sizeof(*aed)))))
2645  return;
2646 
2647  chan->epfd_data[which] = aed;
2648  aed->chan = chan;
2649  aed->which = which;
2650 
2651  ev.events = EPOLLIN | EPOLLPRI | EPOLLERR | EPOLLHUP;
2652  ev.data.ptr = aed;
2653  epoll_ctl(chan->epfd, EPOLL_CTL_ADD, fd, &ev);
2654  } else if (aed) {
2655  /* We don't have to keep around this epoll data structure now */
2656  free(aed);
2657  chan->epfd_data[which] = NULL;
2658  }
2659 #endif
2660  chan->fds[which] = fd;
2661  return;
2662 }
2663 
2664 /*! Add a channel to an optimized waitfor */
2665 void ast_poll_channel_add(struct ast_channel *chan0, struct ast_channel *chan1)
2666 {
2667 #ifdef HAVE_EPOLL
2668  struct epoll_event ev;
2669  int i = 0;
2670 
2671  if (chan0->epfd == -1)
2672  return;
2673 
2674  /* Iterate through the file descriptors on chan1, adding them to chan0 */
2675  for (i = 0; i < AST_MAX_FDS; i++) {
2676  if (chan1->fds[i] == -1)
2677  continue;
2678  ev.events = EPOLLIN | EPOLLPRI | EPOLLERR | EPOLLHUP;
2679  ev.data.ptr = chan1->epfd_data[i];
2680  epoll_ctl(chan0->epfd, EPOLL_CTL_ADD, chan1->fds[i], &ev);
2681  }
2682 
2683 #endif
2684  return;
2685 }
2686 
2687 /*! Delete a channel from an optimized waitfor */
2688 void ast_poll_channel_del(struct ast_channel *chan0, struct ast_channel *chan1)
2689 {
2690 #ifdef HAVE_EPOLL
2691  struct epoll_event ev;
2692  int i = 0;
2693 
2694  if (chan0->epfd == -1)
2695  return;
2696 
2697  for (i = 0; i < AST_MAX_FDS; i++) {
2698  if (chan1->fds[i] == -1)
2699  continue;
2700  epoll_ctl(chan0->epfd, EPOLL_CTL_DEL, chan1->fds[i], &ev);
2701  }
2702 
2703 #endif
2704  return;
2705 }
2706 
2707 void ast_channel_clear_softhangup(struct ast_channel *chan, int flag)
2708 {
2709  ast_channel_lock(chan);
2710 
2711  chan->_softhangup &= ~flag;
2712 
2713  if (!chan->_softhangup) {
2714  struct ast_frame *fr;
2715 
2716  /* If we have completely cleared the softhangup flag,
2717  * then we need to fully abort the hangup process. This requires
2718  * pulling the END_OF_Q frame out of the channel frame queue if it
2719  * still happens to be there. */
2720 
2721  fr = AST_LIST_LAST(&chan->readq);
2722  if (fr && fr->frametype == AST_FRAME_CONTROL &&
2724  AST_LIST_REMOVE(&chan->readq, fr, frame_list);
2725  ast_frfree(fr);
2726  }
2727  }
2728 
2729  ast_channel_unlock(chan);
2730 }
2731 
2732 /*! \brief Softly hangup a channel, don't lock */
2733 int ast_softhangup_nolock(struct ast_channel *chan, int cause)
2734 {
2735  ast_debug(1, "Soft-Hanging up channel '%s'\n", chan->name);
2736  /* Inform channel driver that we need to be hung up, if it cares */
2737  chan->_softhangup |= cause;
2739  /* Interrupt any poll call or such */
2740  if (ast_test_flag(chan, AST_FLAG_BLOCKING))
2741  pthread_kill(chan->blocker, SIGURG);
2742  return 0;
2743 }
2744 
2745 /*! \brief Softly hangup a channel, lock */
2746 int ast_softhangup(struct ast_channel *chan, int cause)
2747 {
2748  int res;
2749 
2750  ast_channel_lock(chan);
2751  res = ast_softhangup_nolock(chan, cause);
2752  ast_channel_unlock(chan);
2753 
2754  return res;
2755 }
2756 
2757 static void free_translation(struct ast_channel *clonechan)
2758 {
2759  if (clonechan->writetrans)
2761  if (clonechan->readtrans)
2762  ast_translator_free_path(clonechan->readtrans);
2763  clonechan->writetrans = NULL;
2764  clonechan->readtrans = NULL;
2765  clonechan->rawwriteformat = clonechan->nativeformats;
2766  clonechan->rawreadformat = clonechan->nativeformats;
2767 }
2768 
2769 void ast_set_hangupsource(struct ast_channel *chan, const char *source, int force)
2770 {
2771  struct ast_channel *bridge;
2772 
2773  ast_channel_lock(chan);
2774  if (force || ast_strlen_zero(chan->hangupsource)) {
2775  ast_string_field_set(chan, hangupsource, source);
2776  }
2777  bridge = ast_bridged_channel(chan);
2778  if (bridge) {
2779  ast_channel_ref(bridge);
2780  }
2781  ast_channel_unlock(chan);
2782 
2783  if (bridge) {
2784  ast_channel_lock(bridge);
2785  if (force || ast_strlen_zero(bridge->hangupsource)) {
2786  ast_string_field_set(bridge, hangupsource, source);
2787  }
2788  ast_channel_unlock(bridge);
2789  ast_channel_unref(bridge);
2790  }
2791 }
2792 
2793 static void destroy_hooks(struct ast_channel *chan)
2794 {
2795  if (chan->audiohooks) {
2797  chan->audiohooks = NULL;
2798  }
2799 
2801 }
2802 
2803 /*! \brief Hangup a channel */
2804 int ast_hangup(struct ast_channel *chan)
2805 {
2806  char extra_str[64]; /* used for cel logging below */
2807  int was_zombie;
2808 
2809  ast_autoservice_stop(chan);
2810 
2811  ast_channel_lock(chan);
2812 
2813  /*
2814  * Do the masquerade if someone is setup to masquerade into us.
2815  *
2816  * NOTE: We must hold the channel lock after testing for a
2817  * pending masquerade and setting the channel as a zombie to
2818  * prevent __ast_channel_masquerade() from setting up a
2819  * masquerade with a dead channel.
2820  */
2821  while (chan->masq) {
2822  ast_channel_unlock(chan);
2823  ast_do_masquerade(chan);
2824  ast_channel_lock(chan);
2825  }
2826 
2827  if (chan->masqr) {
2828  /*
2829  * This channel is one which will be masqueraded into something.
2830  * Mark it as a zombie already so ast_do_masquerade() will know
2831  * to free it later.
2832  */
2834  destroy_hooks(chan);
2835  ast_channel_unlock(chan);
2836  return 0;
2837  }
2838 
2839  /* Mark as a zombie so a masquerade cannot be setup on this channel. */
2840  if (!(was_zombie = ast_test_flag(chan, AST_FLAG_ZOMBIE))) {
2842  }
2843 
2844  ast_channel_unlock(chan);
2845  ao2_unlink(channels, chan);
2846  ast_channel_lock(chan);
2847 
2848  destroy_hooks(chan);
2849 
2850  free_translation(chan);
2851  /* Close audio stream */
2852  if (chan->stream) {
2853  ast_closestream(chan->stream);
2854  chan->stream = NULL;
2855  }
2856  /* Close video stream */
2857  if (chan->vstream) {
2858  ast_closestream(chan->vstream);
2859  chan->vstream = NULL;
2860  }
2861  if (chan->sched) {
2863  chan->sched = NULL;
2864  }
2865 
2866  if (chan->generatordata) { /* Clear any tone stuff remaining */
2867  if (chan->generator && chan->generator->release) {
2868  chan->generator->release(chan, chan->generatordata);
2869  }
2870  }
2871  chan->generatordata = NULL;
2872  chan->generator = NULL;
2873 
2874  snprintf(extra_str, sizeof(extra_str), "%d,%s,%s", chan->hangupcause, chan->hangupsource, S_OR(pbx_builtin_getvar_helper(chan, "DIALSTATUS"), ""));
2875  ast_cel_report_event(chan, AST_CEL_HANGUP, NULL, extra_str, NULL);
2876 
2877  if (ast_test_flag(chan, AST_FLAG_BLOCKING)) {
2878  ast_log(LOG_WARNING, "Hard hangup called by thread %ld on %s, while fd "
2879  "is blocked by thread %ld in procedure %s! Expect a failure\n",
2880  (long) pthread_self(), chan->name, (long)chan->blocker, chan->blockproc);
2882  }
2883  if (!was_zombie) {
2884  ast_debug(1, "Hanging up channel '%s'\n", chan->name);
2885 
2886  if (chan->tech->hangup) {
2887  chan->tech->hangup(chan);
2888  }
2889  } else {
2890  ast_debug(1, "Hanging up zombie '%s'\n", chan->name);
2891  }
2892 
2893  ast_channel_unlock(chan);
2894 
2895  ast_cc_offer(chan);
2896  ast_manager_event(chan, EVENT_FLAG_CALL, "Hangup",
2897  "Channel: %s\r\n"
2898  "Uniqueid: %s\r\n"
2899  "CallerIDNum: %s\r\n"
2900  "CallerIDName: %s\r\n"
2901  "ConnectedLineNum: %s\r\n"
2902  "ConnectedLineName: %s\r\n"
2903  "Cause: %d\r\n"
2904  "Cause-txt: %s\r\n",
2905  chan->name,
2906  chan->uniqueid,
2907  S_COR(chan->caller.id.number.valid, chan->caller.id.number.str, "<unknown>"),
2908  S_COR(chan->caller.id.name.valid, chan->caller.id.name.str, "<unknown>"),
2909  S_COR(chan->connected.id.number.valid, chan->connected.id.number.str, "<unknown>"),
2910  S_COR(chan->connected.id.name.valid, chan->connected.id.name.str, "<unknown>"),
2911  chan->hangupcause,
2912  ast_cause2str(chan->hangupcause)
2913  );
2914 
2915  if (chan->cdr && !ast_test_flag(chan->cdr, AST_CDR_FLAG_BRIDGED) &&
2918  ast_channel_lock(chan);
2919  ast_cdr_end(chan->cdr);
2920  ast_cdr_detach(chan->cdr);
2921  chan->cdr = NULL;
2922  ast_channel_unlock(chan);
2923  }
2924 
2925  ast_channel_unref(chan);
2926 
2927  return 0;
2928 }
2929 
2930 int ast_raw_answer(struct ast_channel *chan, int cdr_answer)
2931 {
2932  int res = 0;
2933 
2934  ast_channel_lock(chan);
2935 
2936  /* You can't answer an outbound call */
2937  if (ast_test_flag(chan, AST_FLAG_OUTGOING)) {
2938  ast_channel_unlock(chan);
2939  return 0;
2940  }
2941 
2942  /* Stop if we're a zombie or need a soft hangup */
2943  if (ast_test_flag(chan, AST_FLAG_ZOMBIE) || ast_check_hangup(chan)) {
2944  ast_channel_unlock(chan);
2945  return -1;
2946  }
2947 
2948  ast_channel_unlock(chan);
2949 
2950  switch (chan->_state) {
2951  case AST_STATE_RINGING:
2952  case AST_STATE_RING:
2953  ast_channel_lock(chan);
2954  if (chan->tech->answer) {
2955  res = chan->tech->answer(chan);
2956  }
2957  ast_setstate(chan, AST_STATE_UP);
2958  if (cdr_answer) {
2959  ast_cdr_answer(chan->cdr);
2960  }
2961  ast_cel_report_event(chan, AST_CEL_ANSWER, NULL, NULL, NULL);
2962  ast_channel_unlock(chan);
2963  break;
2964  case AST_STATE_UP:
2965  ast_cel_report_event(chan, AST_CEL_ANSWER, NULL, NULL, NULL);
2966  /* Calling ast_cdr_answer when it it has previously been called
2967  * is essentially a no-op, so it is safe.
2968  */
2969  if (cdr_answer) {
2970  ast_cdr_answer(chan->cdr);
2971  }
2972  break;
2973  default:
2974  break;
2975  }
2976 
2977  ast_indicate(chan, -1);
2978 
2979  return res;
2980 }
2981 
2982 int __ast_answer(struct ast_channel *chan, unsigned int delay, int cdr_answer)
2983 {
2984  int res = 0;
2985  enum ast_channel_state old_state;
2986 
2987  old_state = chan->_state;
2988  if ((res = ast_raw_answer(chan, cdr_answer))) {
2989  return res;
2990  }
2991 
2992  switch (old_state) {
2993  case AST_STATE_RINGING:
2994  case AST_STATE_RING:
2995  /* wait for media to start flowing, but don't wait any longer
2996  * than 'delay' or 500 milliseconds, whichever is longer
2997  */
2998  do {
3000  struct ast_frame *cur, *new;
3001  int timeout_ms = MAX(delay, 500);
3002  unsigned int done = 0;
3003  struct timeval start;
3004 
3006 
3007  start = ast_tvnow();
3008  for (;;) {
3009  int ms = ast_remaining_ms(start, timeout_ms);
3010  ms = ast_waitfor(chan, ms);
3011  if (ms < 0) {
3012  ast_log(LOG_WARNING, "Error condition occurred when polling channel %s for a voice frame: %s\n", chan->name, strerror(errno));
3013  res = -1;
3014  break;
3015  }
3016  if (ms == 0) {
3017  ast_debug(2, "Didn't receive a media frame from %s within %u ms of answering. Continuing anyway\n", chan->name, MAX(delay, 500));
3018  break;
3019  }
3020  cur = ast_read(chan);
3021  if (!cur || ((cur->frametype == AST_FRAME_CONTROL) &&
3022  (cur->subclass.integer == AST_CONTROL_HANGUP))) {
3023  if (cur) {
3024  ast_frfree(cur);
3025  }
3026  res = -1;
3027  ast_debug(2, "Hangup of channel %s detected in answer routine\n", chan->name);
3028  break;
3029  }
3030 
3031  if ((new = ast_frisolate(cur)) != cur) {
3032  ast_frfree(cur);
3033  }
3034 
3036 
3037  /* if a specific delay period was requested, continue
3038  * until that delay has passed. don't stop just because
3039  * incoming media has arrived.
3040  */
3041  if (delay) {
3042  continue;
3043  }
3044 
3045  switch (new->frametype) {
3046  /* all of these frametypes qualify as 'media' */
3047  case AST_FRAME_VOICE:
3048  case AST_FRAME_VIDEO:
3049  case AST_FRAME_TEXT:
3050  case AST_FRAME_DTMF_BEGIN:
3051  case AST_FRAME_DTMF_END:
3052  case AST_FRAME_IMAGE:
3053  case AST_FRAME_HTML:
3054  case AST_FRAME_MODEM:
3055  done = 1;
3056  break;
3057  case AST_FRAME_CONTROL:
3058  case AST_FRAME_IAX:
3059  case AST_FRAME_NULL:
3060  case AST_FRAME_CNG:
3061  break;
3062  }
3063 
3064  if (done) {
3065  break;
3066  }
3067  }
3068 
3069  if (res == 0) {
3070  ast_channel_lock(chan);
3071  while ((cur = AST_LIST_REMOVE_HEAD(&frames, frame_list))) {
3072  ast_queue_frame_head(chan, cur);
3073  ast_frfree(cur);
3074  }
3075  ast_channel_unlock(chan);
3076  }
3077  } while (0);
3078  break;
3079  default:
3080  break;
3081  }
3082 
3083  return res;
3084 }
3085 
3086 int ast_answer(struct ast_channel *chan)
3087 {
3088  return __ast_answer(chan, 0, 1);
3089 }
3090 
3091 static void deactivate_generator_nolock(struct ast_channel *chan)
3092 {
3093  if (chan->generatordata) {
3094  struct ast_generator *generator = chan->generator;
3095 
3096  if (generator && generator->release) {
3097  generator->release(chan, chan->generatordata);
3098  }
3099  chan->generatordata = NULL;
3100  chan->generator = NULL;
3103  ast_settimeout(chan, 0, NULL, NULL);
3104  }
3105 }
3106 
3108 {
3109  ast_channel_lock(chan);
3111  ast_channel_unlock(chan);
3112 }
3113 
3114 static int generator_force(const void *data)
3115 {
3116  /* Called if generator doesn't have data */
3117  void *tmp;
3118  int res;
3119  int (*generate)(struct ast_channel *chan, void *tmp, int datalen, int samples) = NULL;
3120  struct ast_channel *chan = (struct ast_channel *)data;
3121 
3122  ast_channel_lock(chan);
3123  tmp = chan->generatordata;
3124  chan->generatordata = NULL;
3125  if (chan->generator)
3126  generate = chan->generator->generate;
3127  ast_channel_unlock(chan);
3128 
3129  if (!tmp || !generate)
3130  return 0;
3131 
3132  res = generate(chan, tmp, 0, ast_format_rate(chan->writeformat & AST_FORMAT_AUDIO_MASK) / 50);
3133 
3134  ast_channel_lock(chan);
3135  if (chan->generator && generate == chan->generator->generate) {
3136  chan->generatordata = tmp;
3137  }
3138  ast_channel_unlock(chan);
3139 
3140  if (res) {
3141  ast_debug(1, "Auto-deactivating generator\n");
3143  }
3144 
3145  return 0;
3146 }
3147 
3148 int ast_activate_generator(struct ast_channel *chan, struct ast_generator *gen, void *params)
3149 {
3150  int res = 0;
3151 
3152  ast_channel_lock(chan);
3153  if (chan->generatordata) {
3154  struct ast_generator *generator_old = chan->generator;
3155 
3156  if (generator_old && generator_old->release) {
3157  generator_old->release(chan, chan->generatordata);
3158  }
3159  chan->generatordata = NULL;
3160  }
3161  if (gen->alloc && !(chan->generatordata = gen->alloc(chan, params))) {
3162  res = -1;
3163  }
3164  if (!res) {
3165  ast_settimeout(chan, 50, generator_force, chan);
3166  chan->generator = gen;
3167  }
3168  ast_channel_unlock(chan);
3169 
3170  ast_prod(chan);
3171 
3172  return res;
3173 }
3174 
3175 /*! \brief Wait for x amount of time on a file descriptor to have input. */
3176 int ast_waitfor_n_fd(int *fds, int n, int *ms, int *exception)
3177 {
3178  int winner = -1;
3179  ast_waitfor_nandfds(NULL, 0, fds, n, exception, &winner, ms);
3180  return winner;
3181 }
3182 
3183 /*! \brief Wait for x amount of time on a file descriptor to have input. */
3184 #ifdef HAVE_EPOLL
3185 static struct ast_channel *ast_waitfor_nandfds_classic(struct ast_channel **c, int n, int *fds, int nfds,
3186  int *exception, int *outfd, int *ms)
3187 #else
3188 struct ast_channel *ast_waitfor_nandfds(struct ast_channel **c, int n, int *fds, int nfds,
3189  int *exception, int *outfd, int *ms)
3190 #endif
3191 {
3192  struct timeval start = { 0 , 0 };
3193  struct pollfd *pfds = NULL;
3194  int res;
3195  long rms;
3196  int x, y, max;
3197  int sz;
3198  struct timeval now = { 0, 0 };
3199  struct timeval whentohangup = { 0, 0 }, diff;
3200  struct ast_channel *winner = NULL;
3201  struct fdmap {
3202  int chan;
3203  int fdno;
3204  } *fdmap = NULL;
3205 
3206  if (outfd)
3207  *outfd = -99999;
3208  if (exception)
3209  *exception = 0;
3210 
3211  if ((sz = n * AST_MAX_FDS + nfds)) {
3212  pfds = ast_alloca(sizeof(*pfds) * sz);
3213  fdmap = ast_alloca(sizeof(*fdmap) * sz);
3214  } else {
3215  /* nothing to allocate and no FDs to check */
3216  return NULL;
3217  }
3218 
3219  /* Perform any pending masquerades */
3220  for (x = 0; x < n; x++) {
3221  while (c[x]->masq) {
3222  ast_do_masquerade(c[x]);
3223  }
3224 
3225  ast_channel_lock(c[x]);
3226  if (!ast_tvzero(c[x]->whentohangup)) {
3227  if (ast_tvzero(whentohangup))
3228  now = ast_tvnow();
3229  diff = ast_tvsub(c[x]->whentohangup, now);
3230  if (diff.tv_sec < 0 || ast_tvzero(diff)) {
3231  ast_test_suite_event_notify("HANGUP_TIME", "Channel: %s", c[x]->name);
3232  /* Should already be hungup */
3234  ast_channel_unlock(c[x]);
3235  return c[x];
3236  }
3237  if (ast_tvzero(whentohangup) || ast_tvcmp(diff, whentohangup) < 0)
3238  whentohangup = diff;
3239  }
3240  ast_channel_unlock(c[x]);
3241  }
3242  /* Wait full interval */
3243  rms = *ms;
3244  /* INT_MAX, not LONG_MAX, because it matters on 64-bit */
3245  if (!ast_tvzero(whentohangup) && whentohangup.tv_sec < INT_MAX / 1000) {
3246  rms = whentohangup.tv_sec * 1000 + whentohangup.tv_usec / 1000; /* timeout in milliseconds */
3247  if (*ms >= 0 && *ms < rms) { /* original *ms still smaller */
3248  rms = *ms;
3249  }
3250  } else if (!ast_tvzero(whentohangup) && rms < 0) {
3251  /* Tiny corner case... call would need to last >24 days */
3252  rms = INT_MAX;
3253  }
3254  /*
3255  * Build the pollfd array, putting the channels' fds first,
3256  * followed by individual fds. Order is important because
3257  * individual fd's must have priority over channel fds.
3258  */
3259  max = 0;
3260  for (x = 0; x < n; x++) {
3261  for (y = 0; y < AST_MAX_FDS; y++) {
3262  fdmap[max].fdno = y; /* fd y is linked to this pfds */
3263  fdmap[max].chan = x; /* channel x is linked to this pfds */
3264  max += ast_add_fd(&pfds[max], c[x]->fds[y]);
3265  }
3266  CHECK_BLOCKING(c[x]);
3267  }
3268  /* Add the individual fds */
3269  for (x = 0; x < nfds; x++) {
3270  fdmap[max].chan = -1;
3271  max += ast_add_fd(&pfds[max], fds[x]);
3272  }
3273 
3274  if (*ms > 0)
3275  start = ast_tvnow();
3276 
3277  if (sizeof(int) == 4) { /* XXX fix timeout > 600000 on linux x86-32 */
3278  do {
3279  int kbrms = rms;
3280  if (kbrms > 600000)
3281  kbrms = 600000;
3282  res = ast_poll(pfds, max, kbrms);
3283  if (!res)
3284  rms -= kbrms;
3285  } while (!res && (rms > 0));
3286  } else {
3287  res = ast_poll(pfds, max, rms);
3288  }
3289  for (x = 0; x < n; x++)
3291  if (res < 0) { /* Simulate a timeout if we were interrupted */
3292  if (errno != EINTR)
3293  *ms = -1;
3294  return NULL;
3295  }
3296  if (!ast_tvzero(whentohangup)) { /* if we have a timeout, check who expired */
3297  now = ast_tvnow();
3298  for (x = 0; x < n; x++) {
3299  if (!ast_tvzero(c[x]->whentohangup) && ast_tvcmp(c[x]->whentohangup, now) <= 0) {
3300  ast_test_suite_event_notify("HANGUP_TIME", "Channel: %s", c[x]->name);
3302  if (winner == NULL)
3303  winner = c[x];
3304  }
3305  }
3306  }
3307  if (res == 0) { /* no fd ready, reset timeout and done */
3308  *ms = 0; /* XXX use 0 since we may not have an exact timeout. */
3309  return winner;
3310  }
3311  /*
3312  * Then check if any channel or fd has a pending event.
3313  * Remember to check channels first and fds last, as they
3314  * must have priority on setting 'winner'
3315  */
3316  for (x = 0; x < max; x++) {
3317  res = pfds[x].revents;
3318  if (res == 0)
3319  continue;
3320  if (fdmap[x].chan >= 0) { /* this is a channel */
3321  winner = c[fdmap[x].chan]; /* override previous winners */
3322  if (res & POLLPRI)
3324  else
3326  winner->fdno = fdmap[x].fdno;
3327  } else { /* this is an fd */
3328  if (outfd)
3329  *outfd = pfds[x].fd;
3330  if (exception)
3331  *exception = (res & POLLPRI) ? -1 : 0;
3332  winner = NULL;
3333  }
3334  }
3335  if (*ms > 0) {
3336  *ms -= ast_tvdiff_ms(ast_tvnow(), start);
3337  if (*ms < 0)
3338  *ms = 0;
3339  }
3340  return winner;
3341 }
3342 
3343 #ifdef HAVE_EPOLL
3344 static struct ast_channel *ast_waitfor_nandfds_simple(struct ast_channel *chan, int *ms)
3345 {
3346  struct timeval start = { 0 , 0 };
3347  int res = 0;
3348  struct epoll_event ev[1];
3349  long diff, rms = *ms;
3350  struct ast_channel *winner = NULL;
3351  struct ast_epoll_data *aed = NULL;
3352 
3353 
3354  /* See if this channel needs to be masqueraded */
3355  while (chan->masq) {
3356  ast_do_masquerade(chan);
3357  }
3358 
3359  ast_channel_lock(chan);
3360  /* Figure out their timeout */
3361  if (!ast_tvzero(chan->whentohangup)) {
3362  if ((diff = ast_tvdiff_ms(chan->whentohangup, ast_tvnow())) < 0) {
3363  /* They should already be hungup! */
3365  ast_channel_unlock(chan);
3366  return NULL;
3367  }
3368  /* If this value is smaller then the current one... make it priority */
3369  if (rms > diff)
3370  rms = diff;
3371  }
3372 
3373  ast_channel_unlock(chan);
3374 
3375  /* Time to make this channel block... */
3376  CHECK_BLOCKING(chan);
3377 
3378  if (*ms > 0)
3379  start = ast_tvnow();
3380 
3381  /* We don't have to add any file descriptors... they are already added, we just have to wait! */
3382  res = epoll_wait(chan->epfd, ev, 1, rms);
3383 
3384  /* Stop blocking */
3386 
3387  /* Simulate a timeout if we were interrupted */
3388  if (res < 0) {
3389  if (errno != EINTR)
3390  *ms = -1;
3391  return NULL;
3392  }
3393 
3394  /* If this channel has a timeout see if it expired */
3395  if (!ast_tvzero(chan->whentohangup)) {
3396  if (ast_tvdiff_ms(ast_tvnow(), chan->whentohangup) >= 0) {
3398  winner = chan;
3399  }
3400  }
3401 
3402  /* No fd ready, reset timeout and be done for now */
3403  if (!res) {
3404  *ms = 0;
3405  return winner;
3406  }
3407 
3408  /* See what events are pending */
3409  aed = ev[0].data.ptr;
3410  chan->fdno = aed->which;
3411  if (ev[0].events & EPOLLPRI)
3413  else
3415 
3416  if (*ms > 0) {
3417  *ms -= ast_tvdiff_ms(ast_tvnow(), start);
3418  if (*ms < 0)
3419  *ms = 0;
3420  }
3421 
3422  return chan;
3423 }
3424 
3425 static struct ast_channel *ast_waitfor_nandfds_complex(struct ast_channel **c, int n, int *ms)
3426 {
3427  struct timeval start = { 0 , 0 };
3428  int res = 0, i;
3429  struct epoll_event ev[25] = { { 0, } };
3430  struct timeval now = { 0, 0 };
3431  long whentohangup = 0, diff = 0, rms = *ms;
3432  struct ast_channel *winner = NULL;
3433 
3434  for (i = 0; i < n; i++) {
3435  while (c[i]->masq) {
3436  ast_do_masquerade(c[i]);
3437  }
3438 
3439  ast_channel_lock(c[i]);
3440  if (!ast_tvzero(c[i]->whentohangup)) {
3441  if (whentohangup == 0)
3442  now = ast_tvnow();
3443  if ((diff = ast_tvdiff_ms(c[i]->whentohangup, now)) < 0) {
3445  ast_channel_unlock(c[i]);
3446  return c[i];
3447  }
3448  if (!whentohangup || whentohangup > diff)
3449  whentohangup = diff;
3450  }
3451  ast_channel_unlock(c[i]);
3452  CHECK_BLOCKING(c[i]);
3453  }
3454 
3455  rms = *ms;
3456  if (whentohangup) {
3457  rms = whentohangup;
3458  if (*ms >= 0 && *ms < rms)
3459  rms = *ms;
3460  }
3461 
3462  if (*ms > 0)
3463  start = ast_tvnow();
3464 
3465  res = epoll_wait(c[0]->epfd, ev, 25, rms);
3466 
3467  for (i = 0; i < n; i++)
3469 
3470  if (res < 0) {
3471  if (errno != EINTR)
3472  *ms = -1;
3473  return NULL;
3474  }
3475 
3476  if (whentohangup) {
3477  now = ast_tvnow();
3478  for (i = 0; i < n; i++) {
3479  if (!ast_tvzero(c[i]->whentohangup) && ast_tvdiff_ms(now, c[i]->whentohangup) >= 0) {
3481  if (!winner)
3482  winner = c[i];
3483  }
3484  }
3485  }
3486 
3487  if (!res) {
3488  *ms = 0;
3489  return winner;
3490  }
3491 
3492  for (i = 0; i < res; i++) {
3493  struct ast_epoll_data *aed = ev[i].data.ptr;
3494 
3495  if (!ev[i].events || !aed)
3496  continue;
3497 
3498  winner = aed->chan;
3499  if (ev[i].events & EPOLLPRI)
3501  else
3503  winner->fdno = aed->which;
3504  }
3505 
3506  if (*ms > 0) {
3507  *ms -= ast_tvdiff_ms(ast_tvnow(), start);
3508  if (*ms < 0)
3509  *ms = 0;
3510  }
3511 
3512  return winner;
3513 }
3514 
3515 struct ast_channel *ast_waitfor_nandfds(struct ast_channel **c, int n, int *fds, int nfds,
3516  int *exception, int *outfd, int *ms)
3517 {
3518  /* Clear all provided values in one place. */
3519  if (outfd)
3520  *outfd = -99999;
3521  if (exception)
3522  *exception = 0;
3523 
3524  /* If no epoll file descriptor is available resort to classic nandfds */
3525  if (!n || nfds || c[0]->epfd == -1)
3526  return ast_waitfor_nandfds_classic(c, n, fds, nfds, exception, outfd, ms);
3527  else if (!nfds && n == 1)
3528  return ast_waitfor_nandfds_simple(c[0], ms);
3529  else
3530  return ast_waitfor_nandfds_complex(c, n, ms);
3531 }
3532 #endif
3533 
3534 struct ast_channel *ast_waitfor_n(struct ast_channel **c, int n, int *ms)
3535 {
3536  return ast_waitfor_nandfds(c, n, NULL, 0, NULL, NULL, ms);
3537 }
3538 
3539 int ast_waitfor(struct ast_channel *c, int ms)
3540 {
3541  if (ms < 0) {
3542  do {
3543  ms = 100000;
3544  ast_waitfor_nandfds(&c, 1, NULL, 0, NULL, NULL, &ms);
3545  } while (!ms);
3546  } else {
3547  ast_waitfor_nandfds(&c, 1, NULL, 0, NULL, NULL, &ms);
3548  }
3549  return ms;
3550 }
3551 
3552 int ast_waitfordigit(struct ast_channel *c, int ms)
3553 {
3554  return ast_waitfordigit_full(c, ms, -1, -1);
3555 }
3556 
3557 int ast_settimeout(struct ast_channel *c, unsigned int rate, int (*func)(const void *data), void *data)
3558 {
3559  return ast_settimeout_full(c, rate, func, data, 0);
3560 }
3561 
3562 int ast_settimeout_full(struct ast_channel *c, unsigned int rate, int (*func)(const void *data), void *data, unsigned int is_ao2_obj)
3563 {
3564  int res;
3565  unsigned int real_rate = rate, max_rate;
3566 
3567  ast_channel_lock(c);
3568 
3569  if (c->timingfd == -1) {
3570  ast_channel_unlock(c);
3571  return -1;
3572  }
3573 
3574  if (!func) {
3575  rate = 0;
3576  data = NULL;
3577  }
3578 
3579  if (rate && rate > (max_rate = ast_timer_get_max_rate(c->timer))) {
3580  real_rate = max_rate;
3581  }
3582 
3583  ast_debug(1, "Scheduling timer at (%u requested / %u actual) timer ticks per second\n", rate, real_rate);
3584 
3585  res = ast_timer_set_rate(c->timer, real_rate);
3586 
3588  ao2_ref(c->timingdata, -1);
3589  }
3590 
3591  c->timingfunc = func;
3592  c->timingdata = data;
3593 
3594  if (data && is_ao2_obj) {
3595  ao2_ref(data, 1);
3597  } else {
3599  }
3600 
3601  if (func == NULL && rate == 0 && c->fdno == AST_TIMING_FD) {
3602  /* Clearing the timing func and setting the rate to 0
3603  * means that we don't want to be reading from the timingfd
3604  * any more. Setting c->fdno to -1 means we won't have any
3605  * errant reads from the timingfd, meaning we won't potentially
3606  * miss any important frames.
3607  */
3608  c->fdno = -1;
3609  }
3610 
3611  ast_channel_unlock(c);
3612 
3613  return res;
3614 }
3615 
3616 int ast_waitfordigit_full(struct ast_channel *c, int timeout_ms, int audiofd, int cmdfd)
3617 {
3618  struct timeval start = ast_tvnow();
3619  int ms;
3620 
3621  /* Stop if we're a zombie or need a soft hangup */
3623  return -1;
3624 
3625  /* Only look for the end of DTMF, don't bother with the beginning and don't emulate things */
3627 
3628  /* Wait for a digit, no more than timeout_ms milliseconds total.
3629  * Or, wait indefinitely if timeout_ms is <0.
3630  */
3631  while ((ms = ast_remaining_ms(start, timeout_ms))) {
3632  struct ast_channel *rchan;
3633  int outfd = -1;
3634 
3635  errno = 0;
3636  /* While ast_waitfor_nandfds tries to help by reducing the timeout by how much was waited,
3637  * it is unhelpful if it waited less than a millisecond.
3638  */
3639  rchan = ast_waitfor_nandfds(&c, 1, &cmdfd, (cmdfd > -1) ? 1 : 0, NULL, &outfd, &ms);
3640 
3641  if (!rchan && outfd < 0 && ms) {
3642  if (errno == 0 || errno == EINTR)
3643  continue;
3644  ast_log(LOG_WARNING, "Wait failed (%s)\n", strerror(errno));
3646  return -1;
3647  } else if (outfd > -1) {
3648  /* The FD we were watching has something waiting */
3649  ast_log(LOG_WARNING, "The FD we were waiting for has something waiting. Waitfordigit returning numeric 1\n");
3651  return 1;
3652  } else if (rchan) {
3653  int res;
3654  struct ast_frame *f = ast_read(c);
3655  if (!f)
3656  return -1;
3657 
3658  switch (f->frametype) {
3659  case AST_FRAME_DTMF_BEGIN:
3660  break;
3661  case AST_FRAME_DTMF_END:
3662  res = f->subclass.integer;
3663  ast_frfree(f);
3665  return res;
3666  case AST_FRAME_CONTROL:
3667  switch (f->subclass.integer) {
3668  case AST_CONTROL_HANGUP:
3669  ast_frfree(f);
3671  return -1;
3672  case AST_CONTROL_RINGING:
3673  case AST_CONTROL_ANSWER:
3674  case AST_CONTROL_SRCUPDATE:
3675  case AST_CONTROL_SRCCHANGE:
3679  case AST_CONTROL_HOLD:
3680  case AST_CONTROL_UNHOLD:
3681  case -1:
3682  /* Unimportant */
3683  break;
3684  default:
3685  ast_log(LOG_WARNING, "Unexpected control subclass '%d'\n", f->subclass.integer);
3686  break;
3687  }
3688  break;
3689  case AST_FRAME_VOICE:
3690  /* Write audio if appropriate */
3691  if (audiofd > -1) {
3692  if (write(audiofd, f->data.ptr, f->datalen) < 0) {
3693  ast_log(LOG_WARNING, "write() failed: %s\n", strerror(errno));
3694  }
3695  }
3696  default:
3697  /* Ignore */
3698  break;
3699  }
3700  ast_frfree(f);
3701  }
3702  }
3703 
3705 
3706  return 0; /* Time is up */
3707 }
3708 
3709 static void send_dtmf_event(struct ast_channel *chan, const char *direction, const char digit, const char *begin, const char *end)
3710 {
3712  "DTMF",
3713  "Channel: %s\r\n"
3714  "Uniqueid: %s\r\n"
3715  "Digit: %c\r\n"
3716  "Direction: %s\r\n"
3717  "Begin: %s\r\n"
3718  "End: %s\r\n",
3719  chan->name, chan->uniqueid, digit, direction, begin, end);
3720 }
3721 
3722 static void ast_read_generator_actions(struct ast_channel *chan, struct ast_frame *f)
3723 {
3724  struct ast_generator *generator;
3725  void *gendata;
3726  int res;
3727  int samples;
3728 
3729  generator = chan->generator;
3730  if (!generator
3731  || !generator->generate
3732  || f->frametype != AST_FRAME_VOICE
3733  || !chan->generatordata
3734  || chan->timingfunc) {
3735  return;
3736  }
3737 
3738  /*
3739  * We must generate frames in phase locked mode since
3740  * we have no internal timer available.
3741  */
3742 
3743  if (f->subclass.codec != chan->writeformat) {
3744  float factor;
3745 
3746  factor = ((float) ast_format_rate(chan->writeformat)) / ((float) ast_format_rate(f->subclass.codec));
3747  samples = (int) (((float) f->samples) * factor);
3748  } else {
3749  samples = f->samples;
3750  }
3751 
3752  gendata = chan->generatordata;
3753  chan->generatordata = NULL; /* reset, to let writes go through */
3754 
3755  /*
3756  * This unlock is here based on two assumptions that hold true at
3757  * this point in the code. 1) this function is only called from
3758  * within __ast_read() and 2) all generators call ast_write() in
3759  * their generate callback.
3760  *
3761  * The reason this is added is so that when ast_write is called,
3762  * the lock that occurs there will not recursively lock the
3763  * channel. Doing this will allow deadlock avoidance to work in
3764  * deeper functions.
3765  */
3766  ast_channel_unlock(chan);
3767  res = generator->generate(chan, gendata, f->datalen, samples);
3768  ast_channel_lock(chan);
3769  if (generator == chan->generator) {
3770  chan->generatordata = gendata;
3771  if (res) {
3772  ast_debug(1, "Auto-deactivating generator\n");
3774  }
3775  }
3776 }
3777 
3778 static inline void queue_dtmf_readq(struct ast_channel *chan, struct ast_frame *f)
3779 {
3780  struct ast_frame *fr = &chan->dtmff;
3781 
3783  fr->subclass.integer = f->subclass.integer;
3784  fr->len = f->len;
3785 
3786  /* The only time this function will be called is for a frame that just came
3787  * out of the channel driver. So, we want to stick it on the tail of the
3788  * readq. */
3789 
3790  ast_queue_frame(chan, fr);
3791 }
3792 
3793 /*!
3794  * \brief Determine whether or not we should ignore DTMF in the readq
3795  */
3796 static inline int should_skip_dtmf(struct ast_channel *chan)
3797 {
3799  /* We're in the middle of emulating a digit, or DTMF has been
3800  * explicitly deferred. Skip this digit, then. */
3801  return 1;
3802  }
3803 
3804  if (!ast_tvzero(chan->dtmf_tv) &&
3806  /* We're not in the middle of a digit, but it hasn't been long enough
3807  * since the last digit, so we'll have to skip DTMF for now. */
3808  return 1;
3809  }
3810 
3811  return 0;
3812 }
3813 
3814 /*!
3815  * \brief calculates the number of samples to jump forward with in a monitor stream.
3816 
3817  * \note When using ast_seekstream() with the read and write streams of a monitor,
3818  * the number of samples to seek forward must be of the same sample rate as the stream
3819  * or else the jump will not be calculated correctly.
3820  *
3821  * \retval number of samples to seek forward after rate conversion.
3822  */
3823 static inline int calc_monitor_jump(int samples, int sample_rate, int seek_rate)
3824 {
3825  int diff = sample_rate - seek_rate;
3826 
3827  if (diff > 0) {
3828  samples = samples / (float) (sample_rate / seek_rate);
3829  } else if (diff < 0) {
3830  samples = samples * (float) (seek_rate / sample_rate);
3831  }
3832 
3833  return samples;
3834 }
3835 
3836 static struct ast_frame *__ast_read(struct ast_channel *chan, int dropaudio)
3837 {
3838  struct ast_frame *f = NULL; /* the return value */
3839  int blah;
3840  int prestate;
3841  int cause = 0;
3842 
3843  /* this function is very long so make sure there is only one return
3844  * point at the end (there are only two exceptions to this).
3845  */
3846 
3847  if (chan->masq) {
3848  ast_do_masquerade(chan);
3849  return &ast_null_frame;
3850  }
3851 
3852  /* if here, no masq has happened, lock the channel and proceed */
3853  ast_channel_lock(chan);
3854 
3855  /* Stop if we're a zombie or need a soft hangup */
3856  if (ast_test_flag(chan, AST_FLAG_ZOMBIE) || ast_check_hangup(chan)) {
3857  if (chan->generator)
3859 
3860  /*
3861  * It is possible for chan->_softhangup to be set and there
3862  * still be control frames that need to be read. Instead of
3863  * just going to 'done' in the case of ast_check_hangup(), we
3864  * need to queue the end-of-Q frame so that it can mark the end
3865  * of the read queue. If there are frames to be read,
3866  * ast_queue_control() will be called repeatedly, but will only
3867  * queue the first end-of-Q frame.
3868  */
3869  if (chan->_softhangup) {
3871  } else {
3872  goto done;
3873  }
3874  } else {
3875 #ifdef AST_DEVMODE
3876  /*
3877  * The ast_waitfor() code records which of the channel's file
3878  * descriptors reported that data is available. In theory,
3879  * ast_read() should only be called after ast_waitfor() reports
3880  * that a channel has data available for reading. However,
3881  * there still may be some edge cases throughout the code where
3882  * ast_read() is called improperly. This can potentially cause
3883  * problems, so if this is a developer build, make a lot of
3884  * noise if this happens so that it can be addressed.
3885  *
3886  * One of the potential problems is blocking on a dead channel.
3887  */
3888  if (chan->fdno == -1) {
3890  "ast_read() on chan '%s' called with no recorded file descriptor.\n",
3891  chan->name);
3892  }
3893 #endif
3894  }
3895 
3896  prestate = chan->_state;
3897  if (chan->timingfd > -1 && chan->fdno == AST_TIMING_FD) {
3898  enum ast_timer_event res;
3899 
3901 
3902  res = ast_timer_get_event(chan->timer);
3903 
3904  switch (res) {
3906  if (ast_timer_ack(chan->timer, 1) < 0) {
3907  ast_log(LOG_ERROR, "Failed to acknoweldge timer in ast_read\n");
3908  goto done;
3909  }
3910 
3911  if (chan->timingfunc) {
3912  /* save a copy of func/data before unlocking the channel */
3913  int (*func)(const void *) = chan->timingfunc;
3914  void *data = chan->timingdata;
3915  int got_ref = 0;
3917  ao2_ref(data, 1);
3918  got_ref = 1;
3919  }
3920  chan->fdno = -1;
3921  ast_channel_unlock(chan);
3922  func(data);
3923  if (got_ref) {
3924  ao2_ref(data, -1);
3925  }
3926  } else {
3927  ast_timer_set_rate(chan->timer, 0);
3928  chan->fdno = -1;
3929  ast_channel_unlock(chan);
3930  }
3931 
3932  /* cannot 'goto done' because the channel is already unlocked */
3933  return &ast_null_frame;
3934 
3936  if (AST_LIST_EMPTY(&chan->readq) ||
3939  }
3940  break;
3941  }
3942 
3943  } else if (chan->fds[AST_GENERATOR_FD] > -1 && chan->fdno == AST_GENERATOR_FD) {
3944  /* if the AST_GENERATOR_FD is set, call the generator with args
3945  * set to -1 so it can do whatever it needs to.
3946  */
3947  void *tmp = chan->generatordata;
3948  chan->generatordata = NULL; /* reset to let ast_write get through */
3949  chan->generator->generate(chan, tmp, -1, -1);
3950  chan->generatordata = tmp;
3951  f = &ast_null_frame;
3952  chan->fdno = -1;
3953  goto done;
3954  }
3955 
3956  /* Read and ignore anything on the alertpipe, but read only
3957  one sizeof(blah) per frame that we send from it */
3958  if (chan->alertpipe[0] > -1) {
3959  int flags = fcntl(chan->alertpipe[0], F_GETFL);
3960  /* For some odd reason, the alertpipe occasionally loses nonblocking status,
3961  * which immediately causes a deadlock scenario. Detect and prevent this. */
3962  if ((flags & O_NONBLOCK) == 0) {
3963  ast_log(LOG_ERROR, "Alertpipe on channel %s lost O_NONBLOCK?!!\n", chan->name);
3964  if (fcntl(chan->alertpipe[0], F_SETFL, flags | O_NONBLOCK) < 0) {
3965  ast_log(LOG_WARNING, "Unable to set alertpipe nonblocking! (%d: %s)\n", errno, strerror(errno));
3966  f = &ast_null_frame;
3967  goto done;
3968  }
3969  }
3970  if (read(chan->alertpipe[0], &blah, sizeof(blah)) < 0) {
3971  if (errno != EINTR && errno != EAGAIN)
3972  ast_log(LOG_WARNING, "read() failed: %s\n", strerror(errno));
3973  }
3974  }
3975 
3976 
3977  /* Check for pending read queue */
3978  if (!AST_LIST_EMPTY(&chan->readq)) {
3979  int skip_dtmf = should_skip_dtmf(chan);
3980 
3982  /* We have to be picky about which frame we pull off of the readq because
3983  * there are cases where we want to leave DTMF frames on the queue until
3984  * some later time. */
3985 
3986  if ( (f->frametype == AST_FRAME_DTMF_BEGIN || f->frametype == AST_FRAME_DTMF_END) && skip_dtmf) {
3987  continue;
3988  }
3989 
3991  break;
3992  }
3994 
3995  if (!f) {
3996  /* There were no acceptable frames on the readq. */
3997  f = &ast_null_frame;
3998  if (chan->alertpipe[0] > -1) {
3999  int poke = 0;
4000  /* Restore the state of the alertpipe since we aren't ready for any
4001  * of the frames in the readq. */
4002  if (write(chan->alertpipe[1], &poke, sizeof(poke)) != sizeof(poke)) {
4003  ast_log(LOG_ERROR, "Failed to write to alertpipe: %s\n", strerror(errno));
4004  }
4005  }
4006  }
4007 
4008  /* Interpret hangup and end-of-Q frames to return NULL */
4009  /* XXX why not the same for frames from the channel ? */
4010  if (f->frametype == AST_FRAME_CONTROL) {
4011  switch (f->subclass.integer) {
4012  case AST_CONTROL_HANGUP:
4014  cause = f->data.uint32;
4015  /* Fall through */
4016  case AST_CONTROL_END_OF_Q:
4017  ast_frfree(f);
4018  f = NULL;
4019  break;
4020  default:
4021  break;
4022  }
4023  }
4024  } else {
4025  chan->blocker = pthread_self();
4026  if (ast_test_flag(chan, AST_FLAG_EXCEPTION)) {
4027  if (chan->tech->exception)
4028  f = chan->tech->exception(chan);
4029  else {
4030  ast_log(LOG_WARNING, "Exception flag set on '%s', but no exception handler\n", chan->name);
4031  f = &ast_null_frame;
4032  }
4033  /* Clear the exception flag */
4035  } else if (chan->tech && chan->tech->read)
4036  f = chan->tech->read(chan);
4037  else
4038  ast_log(LOG_WARNING, "No read routine on channel %s\n", chan->name);
4039  }
4040 
4041  /*
4042  * Reset the recorded file descriptor that triggered this read so that we can
4043  * easily detect when ast_read() is called without properly using ast_waitfor().
4044  */
4045  chan->fdno = -1;
4046 
4047  /* Perform the framehook read event here. After the frame enters the framehook list
4048  * there is no telling what will happen, <insert mad scientist laugh here>!!! */
4050 
4051  if (f) {
4052  struct ast_frame *readq_tail = AST_LIST_LAST(&chan->readq);
4053  struct ast_control_read_action_payload *read_action_payload;
4054  struct ast_party_connected_line connected;
4055 
4056  /* if the channel driver returned more than one frame, stuff the excess
4057  into the readq for the next ast_read call
4058  */
4059  if (AST_LIST_NEXT(f, frame_list)) {
4062  AST_LIST_NEXT(f, frame_list) = NULL;
4063  }
4064 
4065  switch (f->frametype) {
4066  case AST_FRAME_CONTROL:
4067  if (f->subclass.integer == AST_CONTROL_ANSWER) {
4068  if (!ast_test_flag(chan, AST_FLAG_OUTGOING)) {
4069  ast_debug(1, "Ignoring answer on an inbound call!\n");
4070  ast_frfree(f);
4071  f = &ast_null_frame;
4072  } else if (prestate == AST_STATE_UP && ast_bridged_channel(chan)) {
4073  ast_debug(1, "Dropping duplicate answer!\n");
4074  ast_frfree(f);
4075  f = &ast_null_frame;
4076  } else {
4077  /* Answer the CDR */
4078  ast_setstate(chan, AST_STATE_UP);
4079  /* removed a call to ast_cdr_answer(chan->cdr) from here. */
4080  ast_cel_report_event(chan, AST_CEL_ANSWER, NULL, NULL, NULL);
4081  }
4082  } else if (f->subclass.integer == AST_CONTROL_READ_ACTION) {
4083  read_action_payload = f->data.ptr;
4084  switch (read_action_payload->action) {
4086  ast_party_connected_line_init(&connected);
4087  ast_party_connected_line_copy(&connected, &chan->connected);
4088  if (ast_connected_line_parse_data(read_action_payload->payload,
4089  read_action_payload->payload_size, &connected)) {
4090  ast_party_connected_line_free(&connected);
4091  break;
4092  }
4093  ast_channel_unlock(chan);
4094  if (ast_channel_connected_line_macro(NULL, chan, &connected, 1, 0)) {
4096  read_action_payload->payload,
4097  read_action_payload->payload_size);
4098  }
4099  ast_party_connected_line_free(&connected);
4100  ast_channel_lock(chan);
4101  break;
4102  }
4103  ast_frfree(f);
4104  f = &ast_null_frame;
4105  }
4106  break;
4107  case AST_FRAME_DTMF_END:
4108  send_dtmf_event(chan, "Received", f->subclass.integer, "No", "Yes");
4109  ast_log(LOG_DTMF, "DTMF end '%c' received on %s, duration %ld ms\n", f->subclass.integer, chan->name, f->len);
4110  /* Queue it up if DTMF is deferred, or if DTMF emulation is forced. */
4112  queue_dtmf_readq(chan, f);
4113  ast_frfree(f);
4114  f = &ast_null_frame;
4115  } else if (!ast_test_flag(chan, AST_FLAG_IN_DTMF | AST_FLAG_END_DTMF_ONLY)) {
4116  if (!ast_tvzero(chan->dtmf_tv) &&
4118  /* If it hasn't been long enough, defer this digit */
4119  queue_dtmf_readq(chan, f);
4120  ast_frfree(f);
4121  f = &ast_null_frame;
4122  } else {
4123  /* There was no begin, turn this into a begin and send the end later */
4126  chan->emulate_dtmf_digit = f->subclass.integer;
4127  chan->dtmf_tv = ast_tvnow();
4128  if (f->len) {
4129  if (f->len > AST_MIN_DTMF_DURATION)
4130  chan->emulate_dtmf_duration = f->len;
4131  else
4133  } else
4135  ast_log(LOG_DTMF, "DTMF begin emulation of '%c' with duration %u queued on %s\n", f->subclass.integer, chan->emulate_dtmf_duration, chan->name);
4136  }
4137  if (chan->audiohooks) {
4138  struct ast_frame *old_frame = f;
4139  /*!
4140  * \todo XXX It is possible to write a digit to the audiohook twice
4141  * if the digit was originally read while the channel was in autoservice. */
4143  if (old_frame != f)
4144  ast_frfree(old_frame);
4145  }
4146  } else {
4147  struct timeval now = ast_tvnow();
4148  if (ast_test_flag(chan, AST_FLAG_IN_DTMF)) {
4149  ast_log(LOG_DTMF, "DTMF end accepted with begin '%c' on %s\n", f->subclass.integer, chan->name);
4151  if (!f->len)
4152  f->len = ast_tvdiff_ms(now, chan->dtmf_tv);
4153 
4154  /* detect tones that were received on
4155  * the wire with durations shorter than
4156  * AST_MIN_DTMF_DURATION and set f->len
4157  * to the actual duration of the DTMF
4158  * frames on the wire. This will cause
4159  * dtmf emulation to be triggered later
4160  * on.
4161  */
4162  if (ast_tvdiff_ms(now, chan->dtmf_tv) < AST_MIN_DTMF_DURATION) {
4163  f->len = ast_tvdiff_ms(now, chan->dtmf_tv);
4164  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, chan->name);
4165  }
4166  } else if (!f->len) {
4167  ast_log(LOG_DTMF, "DTMF end accepted without begin '%c' on %s\n", f->subclass.integer, chan->name);
4169  }
4171  ast_log(LOG_DTMF, "DTMF end '%c' has duration %ld but want minimum %d, emulating on %s\n", f->subclass.integer, f->len, AST_MIN_DTMF_DURATION, chan->name);
4173  chan->emulate_dtmf_digit = f->subclass.integer;
4175  ast_frfree(f);
4176  f = &ast_null_frame;
4177  } else {
4178  ast_log(LOG_DTMF, "DTMF end passthrough '%c' on %s\n", f->subclass.integer, chan->name);
4179  if (f->len < AST_MIN_DTMF_DURATION) {
4181  }
4182  chan->dtmf_tv = now;
4183  }
4184  if (chan->audiohooks) {
4185  struct ast_frame *old_frame = f;
4187  if (old_frame != f)
4188  ast_frfree(old_frame);
4189  }
4190  }
4191  break;
4192  case AST_FRAME_DTMF_BEGIN:
4193  send_dtmf_event(chan, "Received", f->subclass.integer, "Yes", "No");
4194  ast_log(LOG_DTMF, "DTMF begin '%c' received on %s\n", f->subclass.integer, chan->name);
4196  (!ast_tvzero(chan->dtmf_tv) &&
4198  ast_log(LOG_DTMF, "DTMF begin ignored '%c' on %s\n", f->subclass.integer, chan->name);
4199  ast_frfree(f);
4200  f = &ast_null_frame;
4201  } else {
4203  chan->dtmf_tv = ast_tvnow();
4204  ast_log(LOG_DTMF, "DTMF begin passthrough '%c' on %s\n", f->subclass.integer, chan->name);
4205  }
4206  break;
4207  case AST_FRAME_NULL:
4208  /* The EMULATE_DTMF flag must be cleared here as opposed to when the duration
4209  * is reached , because we want to make sure we pass at least one
4210  * voice frame through before starting the next digit, to ensure a gap
4211  * between DTMF digits. */
4212  if (ast_test_flag(chan, AST_FLAG_EMULATE_DTMF)) {
4213  struct timeval now = ast_tvnow();
4214  if (!chan->emulate_dtmf_duration) {
4216  chan->emulate_dtmf_digit = 0;
4217  } else if (ast_tvdiff_ms(now, chan->dtmf_tv) >= chan->emulate_dtmf_duration) {
4218  chan->emulate_dtmf_duration = 0;
4219  ast_frfree(f);
4220  f = &chan->dtmff;
4222  f->subclass.integer = chan->emulate_dtmf_digit;
4223  f->len = ast_tvdiff_ms(now, chan->dtmf_tv);
4224  chan->dtmf_tv = now;
4226  chan->emulate_dtmf_digit = 0;
4227  ast_log(LOG_DTMF, "DTMF end emulation of '%c' queued on %s\n", f->subclass.integer, chan->name);
4228  if (chan->audiohooks) {
4229  struct ast_frame *old_frame = f;
4231  if (old_frame != f) {
4232  ast_frfree(old_frame);
4233  }
4234  }
4235  }
4236  }
4237  break;
4238  case AST_FRAME_VOICE:
4239  /* The EMULATE_DTMF flag must be cleared here as opposed to when the duration
4240  * is reached , because we want to make sure we pass at least one
4241  * voice frame through before starting the next digit, to ensure a gap
4242  * between DTMF digits. */
4245  chan->emulate_dtmf_digit = 0;
4246  }
4247 
4248  if (dropaudio || ast_test_flag(chan, AST_FLAG_IN_DTMF)) {
4249  if (dropaudio)
4250  ast_read_generator_actions(chan, f);
4251  ast_frfree(f);
4252  f = &ast_null_frame;
4253  }
4254 
4256  struct timeval now = ast_tvnow();
4257  if (ast_tvdiff_ms(now, chan->dtmf_tv) >= chan->emulate_dtmf_duration) {
4258  chan->emulate_dtmf_duration = 0;
4259  ast_frfree(f);
4260  f = &chan->dtmff;
4262  f->subclass.integer = chan->emulate_dtmf_digit;
4263  f->len = ast_tvdiff_ms(now, chan->dtmf_tv);
4264  chan->dtmf_tv = now;
4265  if (chan->audiohooks) {
4266  struct ast_frame *old_frame = f;
4268  if (old_frame != f)
4269  ast_frfree(old_frame);
4270  }
4271  ast_log(LOG_DTMF, "DTMF end emulation of '%c' queued on %s\n", f->subclass.integer, chan->name);
4272  } else {
4273  /* Drop voice frames while we're still in the middle of the digit */
4274  ast_frfree(f);
4275  f = &ast_null_frame;
4276  }
4277  } else if ((f->frametype == AST_FRAME_VOICE) && !(f->subclass.codec & chan->nativeformats)) {
4278  /* This frame is not one of the current native formats -- drop it on the floor */
4279  char to[200];
4280  ast_log(LOG_NOTICE, "Dropping incompatible voice frame on %s of format %s since our native format has changed to %s\n",
4281  chan->name, ast_getformatname(f->subclass.codec), ast_getformatname_multiple(to, sizeof(to), chan->nativeformats));
4282  ast_frfree(f);
4283  f = &ast_null_frame;
4284  } else if ((f->frametype == AST_FRAME_VOICE)) {
4285  /* Send frame to audiohooks if present */
4286  if (chan->audiohooks) {
4287  struct ast_frame *old_frame = f;
4289  if (old_frame != f)
4290  ast_frfree(old_frame);
4291  }
4292  if (chan->monitor && chan->monitor->read_stream ) {
4293  /* XXX what does this do ? */
4294 #ifndef MONITOR_CONSTANT_DELAY
4295  int jump = chan->outsmpl - chan->insmpl - 4 * f->samples;
4296  if (jump >= 0) {
4298  if (ast_seekstream(chan->monitor->read_stream, jump, SEEK_FORCECUR) == -1)
4299  ast_log(LOG_WARNING, "Failed to perform seek in monitoring read stream, synchronization between the files may be broken\n");
4300  chan->insmpl += (chan->outsmpl - chan->insmpl) + f->samples;
4301  } else
4302  chan->insmpl+= f->samples;
4303 #else
4305  if (jump - MONITOR_DELAY >= 0) {
4306  if (ast_seekstream(chan->monitor->read_stream, jump - f->samples, SEEK_FORCECUR) == -1)
4307  ast_log(LOG_WARNING, "Failed to perform seek in monitoring read stream, synchronization between the files may be broken\n");
4308  chan->insmpl += chan->outsmpl - chan->insmpl;
4309  } else
4310  chan->insmpl += f->samples;
4311 #endif
4312  if (chan->monitor->state == AST_MONITOR_RUNNING) {
4313  if (ast_writestream(chan->monitor->read_stream, f) < 0)
4314  ast_log(LOG_WARNING, "Failed to write data to channel monitor read stream\n");
4315  }
4316  }
4317 
4318  if (chan->readtrans && (f = ast_translate(chan->readtrans, f, 1)) == NULL) {
4319  f = &ast_null_frame;
4320  }
4321 
4322  /* it is possible for the translation process on chan->readtrans to have
4323  produced multiple frames from the single input frame we passed it; if
4324  this happens, queue the additional frames *before* the frames we may
4325  have queued earlier. if the readq was empty, put them at the head of
4326  the queue, and if it was not, put them just after the frame that was
4327  at the end of the queue.
4328  */
4329  if (AST_LIST_NEXT(f, frame_list)) {
4330  if (!readq_tail) {
4332  } else {
4333  __ast_queue_frame(chan, AST_LIST_NEXT(f, frame_list), 0, readq_tail);
4334  }
4336  AST_LIST_NEXT(f, frame_list) = NULL;
4337  }
4338 
4339  /* Run generator sitting on the line if timing device not available
4340  * and synchronous generation of outgoing frames is necessary */
4341  ast_read_generator_actions(chan, f);
4342  }
4343  break;
4344  default:
4345  /* Just pass it on! */
4346  break;
4347  }
4348  } else {
4349  /* Make sure we always return NULL in the future */
4350  if (!chan->_softhangup) {
4352  }
4353  if (cause)
4354  chan->hangupcause = cause;
4355  if (chan->generator)
4357  /* We no longer End the CDR here */
4358  }
4359 
4360  /* High bit prints debugging */
4361  if (chan->fin & DEBUGCHAN_FLAG)
4362  ast_frame_dump(chan->name, f, "<<");
4363  chan->fin = FRAMECOUNT_INC(chan->fin);
4364 
4365 done:
4366  if (chan->music_state && chan->generator && chan->generator->digit && f && f->frametype == AST_FRAME_DTMF_END)
4367  chan->generator->digit(chan, f->subclass.integer);
4368 
4370  /* The list gets recreated if audiohooks are added again later */
4372  chan->audiohooks = NULL;
4373  }
4374  ast_channel_unlock(chan);
4375  return f;
4376 }
4377 
4379 {
4380  return chan->timingfd > -1;
4381 }
4382 
4383 struct ast_frame *ast_read(struct ast_channel *chan)
4384 {
4385  return __ast_read(chan, 0);
4386 }
4387 
4389 {
4390  return __ast_read(chan, 1);
4391 }
4392 
4393 int ast_indicate(struct ast_channel *chan, int condition)
4394 {
4395  return ast_indicate_data(chan, condition, NULL, 0);
4396 }
4397 
4399 {
4400  /* Don't include a default case here so that we get compiler warnings
4401  * when a new type is added. */
4402 
4403  switch (condition) {
4404  case AST_CONTROL_PROGRESS:
4406  case AST_CONTROL_VIDUPDATE:
4407  case AST_CONTROL_SRCUPDATE:
4408  case AST_CONTROL_SRCCHANGE:
4409  case AST_CONTROL_RADIO_KEY:
4411  case AST_CONTROL_OPTION:
4412  case AST_CONTROL_WINK:
4413  case AST_CONTROL_FLASH:
4414  case AST_CONTROL_OFFHOOK:
4416  case AST_CONTROL_ANSWER:
4417  case AST_CONTROL_HANGUP:
4420  case AST_CONTROL_TRANSFER:
4422  case _XXX_AST_CONTROL_T38:
4423  case AST_CONTROL_CC:
4425  case AST_CONTROL_AOC:
4426  case AST_CONTROL_END_OF_Q:
4428  break;
4429 
4432  case AST_CONTROL_BUSY:
4433  case AST_CONTROL_RINGING:
4434  case AST_CONTROL_RING:
4435  case AST_CONTROL_HOLD:
4436  /* You can hear these */
4437  return 1;
4438 
4439  case AST_CONTROL_UNHOLD:
4440  /* This is a special case. You stop hearing this. */
4441  break;
4442  }
4443 
4444  return 0;
4445 }
4446 
4447 int ast_indicate_data(struct ast_channel *chan, int _condition,
4448  const void *data, size_t datalen)
4449 {
4450  /* By using an enum, we'll get compiler warnings for values not handled
4451  * in switch statements. */
4452  enum ast_control_frame_type condition = _condition;
4453  struct ast_tone_zone_sound *ts = NULL;
4454  int res;
4455  /* this frame is used by framehooks. if it is set, we must free it at the end of this function */
4456  struct ast_frame *awesome_frame = NULL;
4457 
4458  ast_channel_lock(chan);
4459 
4460  /* Don't bother if the channel is about to go away, anyway. */
4461  if (ast_test_flag(chan, AST_FLAG_ZOMBIE) || ast_check_hangup(chan)) {
4462  res = -1;
4463  goto indicate_cleanup;
4464  }
4465 
4467  /* Do framehooks now, do it, go, go now */
4468  struct ast_frame frame = {
4470  .subclass.integer = condition,
4471  .data.ptr = (void *) data, /* this cast from const is only okay because we do the ast_frdup below */
4472  .datalen = datalen
4473  };
4474 
4475  /* we have now committed to freeing this frame */
4476  awesome_frame = ast_frdup(&frame);
4477 
4478  /* who knows what we will get back! the anticipation is killing me. */
4479  if (!(awesome_frame = ast_framehook_list_write_event(chan->framehooks, awesome_frame))
4480  || awesome_frame->frametype != AST_FRAME_CONTROL) {
4481 
4482  res = 0;
4483  goto indicate_cleanup;
4484  }
4485 
4486  condition = awesome_frame->subclass.integer;
4487  data = awesome_frame->data.ptr;
4488  datalen = awesome_frame->datalen;
4489  }
4490 
4491  switch (condition) {
4493  {
4494  struct ast_party_connected_line connected;
4495 
4496  ast_party_connected_line_set_init(&connected, &chan->connected);
4497  res = ast_connected_line_parse_data(data, datalen, &connected);
4498  if (!res) {
4499  ast_channel_set_connected_line(chan, &connected, NULL);
4500  }
4501  ast_party_connected_line_free(&connected);
4502  }
4503  break;
4504 
4506  {
4507  struct ast_party_redirecting redirecting;
4508 
4509  ast_party_redirecting_set_init(&redirecting, &chan->redirecting);
4510  res = ast_redirecting_parse_data(data, datalen, &redirecting);
4511  if (!res) {
4512  ast_channel_set_redirecting(chan, &redirecting, NULL);
4513  }
4514  ast_party_redirecting_free(&redirecting);
4515  }
4516  break;
4517 
4518  default:
4519  break;
4520  }
4521 
4522  if (is_visible_indication(condition)) {
4523  /* A new visible indication is requested. */
4524  chan->visible_indication = condition;
4525  } else if (condition == AST_CONTROL_UNHOLD || _condition < 0) {
4526  /* Visible indication is cleared/stopped. */
4527  chan->visible_indication = 0;
4528  }
4529 
4530  if (chan->tech->indicate) {
4531  /* See if the channel driver can handle this condition. */
4532  res = chan->tech->indicate(chan, condition, data, datalen);
4533  } else {
4534  res = -1;
4535  }
4536 
4537  if (!res) {
4538  /* The channel driver successfully handled this indication */
4539  res = 0;
4540  goto indicate_cleanup;
4541  }
4542 
4543  /* The channel driver does not support this indication, let's fake
4544  * it by doing our own tone generation if applicable. */
4545 
4546  /*!\note If we compare the enumeration type, which does not have any
4547  * negative constants, the compiler may optimize this code away.
4548  * Therefore, we must perform an integer comparison here. */
4549  if (_condition < 0) {
4550  /* Stop any tones that are playing */
4551  ast_playtones_stop(chan);
4552  res = 0;
4553  goto indicate_cleanup;
4554  }
4555 
4556  /* Handle conditions that we have tones for. */
4557  switch (condition) {
4558  case _XXX_AST_CONTROL_T38:
4559  /* deprecated T.38 control frame */
4560  res = -1;
4561  goto indicate_cleanup;
4563  /* there is no way to provide 'default' behavior for these
4564  * control frames, so we need to return failure, but there
4565  * is also no value in the log message below being emitted
4566  * since failure to handle these frames is not an 'error'
4567  * so just return right now. in addition, we want to return
4568  * whatever value the channel driver returned, in case it
4569  * has some meaning.*/
4570  goto indicate_cleanup;
4571  case AST_CONTROL_RINGING:
4572  ts = ast_get_indication_tone(chan->zone, "ring");
4573  /* It is common practice for channel drivers to return -1 if trying
4574  * to indicate ringing on a channel which is up. The idea is to let the
4575  * core generate the ringing inband. However, we don't want the
4576  * warning message about not being able to handle the specific indication
4577  * to print nor do we want ast_indicate_data to return an "error" for this
4578  * condition
4579  */
4580  if (chan->_state == AST_STATE_UP) {
4581  res = 0;
4582  }
4583  break;
4584  case AST_CONTROL_BUSY:
4585  ts = ast_get_indication_tone(chan->zone, "busy");
4586  break;
4589  ts = ast_get_indication_tone(chan->zone, "congestion");
4590  break;
4591  case AST_CONTROL_PROGRESS:
4593  case AST_CONTROL_VIDUPDATE:
4594  case AST_CONTROL_SRCUPDATE:
4595  case AST_CONTROL_SRCCHANGE:
4596  case AST_CONTROL_RADIO_KEY:
4598  case AST_CONTROL_OPTION:
4599  case AST_CONTROL_WINK:
4600  case AST_CONTROL_FLASH:
4601  case AST_CONTROL_OFFHOOK:
4603  case AST_CONTROL_ANSWER:
4604  case AST_CONTROL_HANGUP:
4605  case AST_CONTROL_RING:
4606  case AST_CONTROL_HOLD:
4607  case AST_CONTROL_UNHOLD:
4608  case AST_CONTROL_TRANSFER:
4611  case AST_CONTROL_CC:
4613  case AST_CONTROL_AOC:
4614  case AST_CONTROL_END_OF_Q:
4616  /* Nothing left to do for these. */
4617  res = 0;
4618  break;
4619  }
4620 
4621  if (ts) {
4622  /* We have a tone to play, yay. */
4623  ast_debug(1, "Driver for channel '%s' does not support indication %u, emulating it\n", chan->name, condition);
4624  res = ast_playtones_start(chan, 0, ts->data, 1);
4625  ts = ast_tone_zone_sound_unref(ts);
4626  }
4627 
4628  if (res) {
4629  /* not handled */
4630  ast_log(LOG_WARNING, "Unable to handle indication %u for '%s'\n", condition, chan->name);
4631  }
4632 
4633 indicate_cleanup:
4634  ast_channel_unlock(chan);
4635  if (awesome_frame) {
4636  ast_frfree(awesome_frame);
4637  }
4638 
4639  return res;
4640 }
4641 
4642 int ast_recvchar(struct ast_channel *chan, int timeout)
4643 {
4644  int c;
4645  char *buf = ast_recvtext(chan, timeout);
4646  if (buf == NULL)
4647  return -1; /* error or timeout */
4648  c = *(unsigned char *)buf;
4649  ast_free(buf);
4650  return c;
4651 }
4652 
4653 char *ast_recvtext(struct ast_channel *chan, int timeout)
4654 {
4655  int res;
4656  char *buf = NULL;
4657  struct timeval start = ast_tvnow();
4658  int ms;
4659 
4660  while ((ms = ast_remaining_ms(start, timeout))) {
4661  struct ast_frame *f;
4662 
4663  if (ast_check_hangup(chan)) {
4664  break;
4665  }
4666  res = ast_waitfor(chan, ms);
4667  if (res <= 0) {/* timeout or error */
4668  break;
4669  }
4670  f = ast_read(chan);
4671  if (f == NULL) {
4672  break; /* no frame */
4673  }
4675  ast_frfree(f);
4676  break;
4677  } else if (f->frametype == AST_FRAME_TEXT) { /* what we want */
4678  buf = ast_strndup((char *) f->data.ptr, f->datalen); /* dup and break */
4679  ast_frfree(f);
4680  break;
4681  }
4682  ast_frfree(f);
4683  }
4684  return buf;
4685 }
4686 
4687 int ast_sendtext(struct ast_channel *chan, const char *text)
4688 {
4689  int res = 0;
4690 
4691  ast_channel_lock(chan);
4692  /* Stop if we're a zombie or need a soft hangup */
4693  if (ast_test_flag(chan, AST_FLAG_ZOMBIE) || ast_check_hangup(chan)) {
4694  ast_channel_unlock(chan);
4695  return -1;
4696  }
4697  CHECK_BLOCKING(chan);
4698  if (chan->tech->send_text)
4699  res = chan->tech->send_text(chan, text);
4701  ast_channel_unlock(chan);
4702  return res;
4703 }
4704 
4705 int ast_senddigit_begin(struct ast_channel *chan, char digit)
4706 {
4707  /* Device does not support DTMF tones, lets fake
4708  * it by doing our own generation. */
4709  static const char * const dtmf_tones[] = {
4710  "941+1336", /* 0 */
4711  "697+1209", /* 1 */
4712  "697+1336", /* 2 */
4713  "697+1477", /* 3 */
4714  "770+1209", /* 4 */
4715  "770+1336", /* 5 */
4716  "770+1477", /* 6 */
4717  "852+1209", /* 7 */
4718  "852+1336", /* 8 */
4719  "852+1477", /* 9 */
4720  "697+1633", /* A */
4721  "770+1633", /* B */
4722  "852+1633", /* C */
4723  "941+1633", /* D */
4724  "941+1209", /* * */
4725  "941+1477" /* # */
4726  };
4727 
4728  if (!chan->tech->send_digit_begin)
4729  return 0;
4730 
4731  ast_channel_lock(chan);
4732  chan->sending_dtmf_digit = digit;
4733  chan->sending_dtmf_tv = ast_tvnow();
4734  ast_channel_unlock(chan);
4735 
4736  if (!chan->tech->send_digit_begin(chan, digit))
4737  return 0;
4738 
4739  if (digit >= '0' && digit <='9')
4740  ast_playtones_start(chan, 0, dtmf_tones[digit-'0'], 0);
4741  else if (digit >= 'A' && digit <= 'D')
4742  ast_playtones_start(chan, 0, dtmf_tones[digit-'A'+10], 0);
4743  else if (digit == '*')
4744  ast_playtones_start(chan, 0, dtmf_tones[14], 0);
4745  else if (digit == '#')
4746  ast_playtones_start(chan, 0, dtmf_tones[15], 0);
4747  else {
4748  /* not handled */
4749  ast_debug(1, "Unable to generate DTMF tone '%c' for '%s'\n", digit, chan->name);
4750  }
4751 
4752  return 0;
4753 }
4754 
4755 int ast_senddigit_end(struct ast_channel *chan, char digit, unsigned int duration)
4756 {
4757  int res = -1;
4758 
4759  ast_channel_lock(chan);
4760  if (chan->sending_dtmf_digit == digit) {
4761  chan->sending_dtmf_digit = 0;
4762  }
4763  ast_channel_unlock(chan);
4764 
4765  if (chan->tech->send_digit_end)
4766  res = chan->tech->send_digit_end(chan, digit, duration);
4767 
4768  if (res && chan->generator)
4769  ast_playtones_stop(chan);
4770 
4771  return 0;
4772 }
4773 
4774 int ast_senddigit(struct ast_channel *chan, char digit, unsigned int duration)
4775 {
4776  if (chan->tech->send_digit_begin) {
4777  ast_senddigit_begin(chan, digit);
4779  }
4780 
4781  return ast_senddigit_end(chan, digit, (duration >= AST_DEFAULT_EMULATE_DTMF_DURATION ? duration : AST_DEFAULT_EMULATE_DTMF_DURATION));
4782 }
4783 
4784 int ast_prod(struct ast_channel *chan)
4785 {
4786  struct ast_frame a = { AST_FRAME_VOICE };
4787  char nothing[128];
4788 
4789  /* Send an empty audio frame to get things moving */
4790  if (chan->_state != AST_STATE_UP) {
4791  ast_debug(1, "Prodding channel '%s'\n", chan->name);
4792  a.subclass.codec = chan->rawwriteformat;
4793  a.data.ptr = nothing + AST_FRIENDLY_OFFSET;
4794  a.src = "ast_prod"; /* this better match check in ast_write */
4795  if (ast_write(chan, &a))
4796  ast_log(LOG_WARNING, "Prodding channel '%s' failed\n", chan->name);
4797  }
4798  return 0;
4799 }
4800 
4801 int ast_write_video(struct ast_channel *chan, struct ast_frame *fr)
4802 {
4803  int res;
4804  if (!chan->tech->write_video)
4805  return 0;
4806  res = ast_write(chan, fr);
4807  if (!res)
4808  res = 1;
4809  return res;
4810 }
4811 
4812 struct plc_ds {
4813  /* A buffer in which to store SLIN PLC
4814  * samples generated by the generic PLC
4815  * functionality in plc.c
4816  */
4817  int16_t *samples_buf;
4818  /* The current number of samples in the
4819  * samples_buf
4820  */
4821  size_t num_samples;
4823 };
4824 
4825 static void plc_ds_destroy(void *data)
4826 {
4827  struct plc_ds *plc = data;
4828  ast_free(plc->samples_buf);
4829  ast_free(plc);
4830 }
4831 
4832 static const struct ast_datastore_info plc_ds_info = {
4833  .type = "plc",
4834  .destroy = plc_ds_destroy,
4835 };
4836 
4837 static void adjust_frame_for_plc(struct ast_channel *chan, struct ast_frame *frame, struct ast_datastore *datastore)
4838 {
4839  int num_new_samples = frame->samples;
4840  struct plc_ds *plc = datastore->data;
4841 
4842  /* As a general note, let me explain the somewhat odd calculations used when taking
4843  * the frame offset into account here. According to documentation in frame.h, the frame's
4844  * offset field indicates the number of bytes that the audio is offset. The plc->samples_buf
4845  * is not an array of bytes, but rather an array of 16-bit integers since it holds SLIN
4846  * samples. So I had two choices to make here with the offset.
4847  *
4848  * 1. Make the offset AST_FRIENDLY_OFFSET bytes. The main downside for this is that
4849  * I can't just add AST_FRIENDLY_OFFSET to the plc->samples_buf and have the pointer
4850  * arithmetic come out right. I would have to do some odd casting or division for this to
4851  * work as I wanted.
4852  * 2. Make the offset AST_FRIENDLY_OFFSET * 2 bytes. This allows the pointer arithmetic
4853  * to work out better with the plc->samples_buf. The downside here is that the buffer's
4854  * allocation contains an extra 64 bytes of unused space.
4855  *
4856  * I decided to go with option 2. This is why in the calloc statement and the statement that
4857  * sets the frame's offset, AST_FRIENDLY_OFFSET is multiplied by 2.
4858  */
4859 
4860  /* If this audio frame has no samples to fill in, ignore it */
4861  if (!num_new_samples) {
4862  return;
4863  }
4864 
4865  /* First, we need to be sure that our buffer is large enough to accomodate
4866  * the samples we need to fill in. This will likely only occur on the first
4867  * frame we write.
4868  */
4869  if (plc->num_samples < num_new_samples) {
4870  ast_free(plc->samples_buf);
4871  plc->samples_buf = ast_calloc(1, (num_new_samples * sizeof(*plc->samples_buf)) + (AST_FRIENDLY_OFFSET * 2));
4872  if (!plc->samples_buf) {
4873  ast_channel_datastore_remove(chan, datastore);
4874  ast_datastore_free(datastore);
4875  return;
4876  }
4877  plc->num_samples = num_new_samples;
4878  }
4879 
4880  if (frame->datalen == 0) {
4882  frame->data.ptr = plc->samples_buf + AST_FRIENDLY_OFFSET;
4883  frame->datalen = num_new_samples * 2;
4884  frame->offset = AST_FRIENDLY_OFFSET * 2;
4885  } else {
4886  plc_rx(&plc->plc_state, frame->data.ptr, frame->samples);
4887  }
4888 }
4889 
4890 static void apply_plc(struct ast_channel *chan, struct ast_frame *frame)
4891 {
4892  struct ast_datastore *datastore;
4893  struct plc_ds *plc;
4894 
4895  datastore = ast_channel_datastore_find(chan, &plc_ds_info, NULL);
4896  if (datastore) {
4897  plc = datastore->data;
4898  adjust_frame_for_plc(chan, frame, datastore);
4899  return;
4900  }
4901 
4902  datastore = ast_datastore_alloc(&plc_ds_info, NULL);
4903  if (!datastore) {
4904  return;
4905  }
4906  plc = ast_calloc(1, sizeof(*plc));
4907  if (!plc) {
4908  ast_datastore_free(datastore);
4909  return;
4910  }
4911  datastore->data = plc;
4912  ast_channel_datastore_add(chan, datastore);
4913  adjust_frame_for_plc(chan, frame, datastore);
4914 }
4915 
4916 int ast_write(struct ast_channel *chan, struct ast_frame *fr)
4917 {
4918  int res = -1;
4919  struct ast_frame *f = NULL;
4920  int count = 0;
4921 
4922  /*Deadlock avoidance*/
4923  while(ast_channel_trylock(chan)) {
4924  /*cannot goto done since the channel is not locked*/
4925  if(count++ > 10) {
4926  ast_debug(1, "Deadlock avoided for write to channel '%s'\n", chan->name);
4927  return 0;
4928  }
4929  usleep(1);
4930  }
4931  /* Stop if we're a zombie or need a soft hangup */
4932  if (ast_test_flag(chan, AST_FLAG_ZOMBIE) || ast_check_hangup(chan))
4933  goto done;
4934 
4935  /* Handle any pending masquerades */
4936  while (chan->masq) {
4937  ast_channel_unlock(chan);
4938  ast_do_masquerade(chan);
4939  ast_channel_lock(chan);
4940  }
4941  if (chan->masqr) {
4942  res = 0; /* XXX explain, why 0 ? */
4943  goto done;
4944  }
4945 
4946  /* Perform the framehook write event here. After the frame enters the framehook list
4947  * there is no telling what will happen, how awesome is that!!! */
4948  if (!(fr = ast_framehook_list_write_event(chan->framehooks, fr))) {
4949  res = 0;
4950  goto done;
4951  }
4952 
4953  if (chan->generatordata && (!fr->src || strcasecmp(fr->src, "ast_prod"))) {
4954  if (ast_test_flag(chan, AST_FLAG_WRITE_INT)) {
4956  } else {
4957  if (fr->frametype == AST_FRAME_DTMF_END) {
4958  /* There is a generator running while we're in the middle of a digit.
4959  * It's probably inband DTMF, so go ahead and pass it so it can
4960  * stop the generator */
4962  ast_channel_unlock(chan);
4963  res = ast_senddigit_end(chan, fr->subclass.integer, fr->len);
4964  ast_channel_lock(chan);
4965  CHECK_BLOCKING(chan);
4966  } else if (fr->frametype == AST_FRAME_CONTROL && fr->subclass.integer == AST_CONTROL_UNHOLD) {
4967  /* This is a side case where Echo is basically being called and the person put themselves on hold and took themselves off hold */
4968  res = (chan->tech->indicate == NULL) ? 0 :
4969  chan->tech->indicate(chan, fr->subclass.integer, fr->data.ptr, fr->datalen);
4970  }
4971  res = 0; /* XXX explain, why 0 ? */
4972  goto done;
4973  }
4974  }
4975  /* High bit prints debugging */
4976  if (chan->fout & DEBUGCHAN_FLAG)
4977  ast_frame_dump(chan->name, fr, ">>");
4978  CHECK_BLOCKING(chan);
4979  switch (fr->frametype) {
4980  case AST_FRAME_CONTROL:
4981  res = (chan->tech->indicate == NULL) ? 0 :
4982  chan->tech->indicate(chan, fr->subclass.integer, fr->data.ptr, fr->datalen);
4983  break;
4984  case AST_FRAME_DTMF_BEGIN:
4985  if (chan->audiohooks) {
4986  struct ast_frame *old_frame = fr;
4988  if (old_frame != fr)
4989  f = fr;
4990  }
4991  send_dtmf_event(chan, "Sent", fr->subclass.integer, "Yes", "No");
4993  ast_channel_unlock(chan);
4994  res = ast_senddigit_begin(chan, fr->subclass.integer);
4995  ast_channel_lock(chan);
4996  CHECK_BLOCKING(chan);
4997  break;
4998  case AST_FRAME_DTMF_END:
4999  if (chan->audiohooks) {
5000  struct ast_frame *new_frame = fr;
5001 
5003  if (new_frame != fr) {
5004  ast_frfree(new_frame);
5005  }
5006  }
5007  send_dtmf_event(chan, "Sent", fr->subclass.integer, "No", "Yes");
5009  ast_channel_unlock(chan);
5010  res = ast_senddigit_end(chan, fr->subclass.integer, fr->len);
5011  ast_channel_lock(chan);
5012  CHECK_BLOCKING(chan);
5013  break;
5014  case AST_FRAME_TEXT:
5015  if (fr->subclass.integer == AST_FORMAT_T140) {
5016  res = (chan->tech->write_text == NULL) ? 0 :
5017  chan->tech->write_text(chan, fr);
5018  } else {
5019  res = (chan->tech->send_text == NULL) ? 0 :
5020  chan->tech->send_text(chan, (char *) fr->data.ptr);
5021  }
5022  break;
5023  case AST_FRAME_HTML:
5024  res = (chan->tech->send_html == NULL) ? 0 :
5025  chan->tech->send_html(chan, fr->subclass.integer, (char *) fr->data.ptr, fr->datalen);
5026  break;
5027  case AST_FRAME_VIDEO:
5028  /* XXX Handle translation of video codecs one day XXX */
5029  res = (chan->tech->write_video == NULL) ? 0 :
5030  chan->tech->write_video(chan, fr);
5031  break;
5032  case AST_FRAME_MODEM:
5033  res = (chan->tech->write == NULL) ? 0 :
5034  chan->tech->write(chan, fr);
5035  break;
5036  case AST_FRAME_VOICE:
5037  if (chan->tech->write == NULL)
5038  break; /*! \todo XXX should return 0 maybe ? */
5039 
5041  apply_plc(chan, fr);
5042  }
5043 
5044  /* If the frame is in the raw write format, then it's easy... just use the frame - otherwise we will have to translate */
5045  if (fr->subclass.codec == chan->rawwriteformat) {
5046  f = fr;
5047  } else {
5048  if ((!(fr->subclass.codec & chan->nativeformats)) && (chan->writeformat != fr->subclass.codec)) {
5049  char nf[512];
5050 
5051  /*
5052  * XXX Something is not right. We are not compatible with this
5053  * frame. Bad things can happen. Problems range from no audio,
5054  * one-way audio, to unexplained line hangups. As a last resort
5055  * try to adjust the format. Ideally, we do not want to do this
5056  * because it indicates a deeper problem. For now, we log these
5057  * events to reduce user impact and help identify the problem
5058  * areas.
5059  */
5060  ast_log(LOG_WARNING, "Codec mismatch on channel %s setting write format to %s from %s native formats %s\n",
5063  ast_set_write_format(chan, fr->subclass.codec);
5064  }
5065 
5066  f = (chan->writetrans) ? ast_translate(chan->writetrans, fr, 0) : fr;
5067  }
5068 
5069  if (!f) {
5070  res = 0;
5071  break;
5072  }
5073 
5074  if (chan->audiohooks) {
5075  struct ast_frame *prev = NULL, *new_frame, *cur, *dup;
5076  int freeoldlist = 0;
5077 
5078  if (f != fr) {
5079  freeoldlist = 1;
5080  }
5081 
5082  /* Since ast_audiohook_write may return a new frame, and the cur frame is
5083  * an item in a list of frames, create a new list adding each cur frame back to it
5084  * regardless if the cur frame changes or not. */
5085  for (cur = f; cur; cur = AST_LIST_NEXT(cur, frame_list)) {
5086  new_frame = ast_audiohook_write_list(chan, chan->audiohooks, AST_AUDIOHOOK_DIRECTION_WRITE, cur);
5087 
5088  /* if this frame is different than cur, preserve the end of the list,
5089  * free the old frames, and set cur to be the new frame */
5090  if (new_frame != cur) {
5091 
5092  /* doing an ast_frisolate here seems silly, but we are not guaranteed the new_frame
5093  * isn't part of local storage, meaning if ast_audiohook_write is called multiple
5094  * times it may override the previous frame we got from it unless we dup it */
5095  if ((dup = ast_frisolate(new_frame))) {
5097  if (freeoldlist) {
5098  AST_LIST_NEXT(cur, frame_list) = NULL;
5099  ast_frfree(cur);
5100  }
5101  if (new_frame != dup) {
5102  ast_frfree(new_frame);
5103  }
5104  cur = dup;
5105  }
5106  }
5107 
5108  /* now, regardless if cur is new or not, add it to the new list,
5109  * if the new list has not started, cur will become the first item. */
5110  if (prev) {
5111  AST_LIST_NEXT(prev, frame_list) = cur;
5112  } else {
5113  f = cur; /* set f to be the beginning of our new list */
5114  }
5115  prev = cur;
5116  }
5117  }
5118 
5119  /* If Monitor is running on this channel, then we have to write frames out there too */
5120  /* the translator on chan->writetrans may have returned multiple frames
5121  from the single frame we passed in; if so, feed each one of them to the
5122  monitor */
5123  if (chan->monitor && chan->monitor->write_stream) {
5124  struct ast_frame *cur;
5125 
5126  for (cur = f; cur; cur = AST_LIST_NEXT(cur, frame_list)) {
5127  /* XXX must explain this code */
5128 #ifndef MONITOR_CONSTANT_DELAY
5129  int jump = chan->insmpl - chan->outsmpl - 4 * cur->samples;
5130  if (jump >= 0) {
5132  if (ast_seekstream(chan->monitor->write_stream, jump, SEEK_FORCECUR) == -1)
5133  ast_log(LOG_WARNING, "Failed to perform seek in monitoring write stream, synchronization between the files may be broken\n");
5134  chan->outsmpl += (chan->insmpl - chan->outsmpl) + cur->samples;
5135  } else {
5136  chan->outsmpl += cur->samples;
5137  }
5138 #else
5140  if (jump - MONITOR_DELAY >= 0) {
5141  if (ast_seekstream(chan->monitor->write_stream, jump - cur->samples, SEEK_FORCECUR) == -1)
5142  ast_log(LOG_WARNING, "Failed to perform seek in monitoring write stream, synchronization between the files may be broken\n");
5143  chan->outsmpl += chan->insmpl - chan->outsmpl;
5144  } else {
5145  chan->outsmpl += cur->samples;
5146  }
5147 #endif
5148  if (chan->monitor->state == AST_MONITOR_RUNNING) {
5149  if (ast_writestream(chan->monitor->write_stream, cur) < 0)
5150  ast_log(LOG_WARNING, "Failed to write data to channel monitor write stream\n");
5151  }
5152  }
5153  }
5154 
5155  /* the translator on chan->writetrans may have returned multiple frames
5156  from the single frame we passed in; if so, feed each one of them to the
5157  channel, freeing each one after it has been written */
5158  if ((f != fr) && AST_LIST_NEXT(f, frame_list)) {
5159  struct ast_frame *cur, *next;
5160  unsigned int skip = 0;
5161 
5162  for (cur = f, next = AST_LIST_NEXT(cur, frame_list);
5163  cur;
5164  cur = next, next = cur ? AST_LIST_NEXT(cur, frame_list) : NULL) {
5165  if (!skip) {
5166  if ((res = chan->tech->write(chan, cur)) < 0) {
5168  skip = 1;
5169  } else if (next) {
5170  /* don't do this for the last frame in the list,
5171  as the code outside the loop will do it once
5172  */
5173  chan->fout = FRAMECOUNT_INC(chan->fout);
5174  }
5175  }
5176  ast_frfree(cur);
5177  }
5178 
5179  /* reset f so the code below doesn't attempt to free it */
5180  f = NULL;
5181  } else {
5182  res = chan->tech->write(chan, f);
5183  }
5184  break;
5185  case AST_FRAME_NULL:
5186  case AST_FRAME_IAX:
5187  /* Ignore these */
5188  res = 0;
5189  break;
5190  default:
5191  /* At this point, fr is the incoming frame and f is NULL. Channels do
5192  * not expect to get NULL as a frame pointer and will segfault. Hence,
5193  * we output the original frame passed in. */
5194  res = chan->tech->write(chan, fr);
5195  break;
5196  }
5197 
5198  if (f && f != fr)
5199  ast_frfree(f);
5201 
5202  /* Consider a write failure to force a soft hangup */
5203  if (res < 0) {
5205  } else {
5206  chan->fout = FRAMECOUNT_INC(chan->fout);
5207  }
5208 done:
5210  /* The list gets recreated if audiohooks are added again later */
5212  chan->audiohooks = NULL;
5213  }
5214  ast_channel_unlock(chan);
5215  return res;
5216 }
5217 
5218 static int set_format(struct ast_channel *chan, format_t fmt, format_t *rawformat, format_t *format,
5219  struct ast_trans_pvt **trans, const int direction)
5220 {
5221  format_t native, native_fmt = ast_best_codec(fmt);
5222  int res;
5223  char from[200], to[200];
5224 
5225  /* Make sure we only consider audio */
5226  fmt &= AST_FORMAT_AUDIO_MASK;
5227 
5228  native = chan->nativeformats;
5229 
5230  if (!fmt || !native) /* No audio requested */
5231  return 0; /* Let's try a call without any sounds (video, text) */
5232 
5233  /* See if the underlying channel driver is capable of performing transcoding for us */
5234  if (!ast_channel_setoption(chan, direction ? AST_OPTION_FORMAT_WRITE : AST_OPTION_FORMAT_READ, &native_fmt, sizeof(int*), 0)) {
5235  ast_debug(1, "Channel driver natively set channel %s to %s format %s\n", chan->name,
5236  direction ? "write" : "read", ast_getformatname(native_fmt));
5237  chan->nativeformats = *rawformat = *format = native_fmt;
5238  if (*trans) {
5239  ast_translator_free_path(*trans);
5240  }
5241  *trans = NULL;
5242  return 0;
5243  }
5244 
5245  /* Find a translation path from the native format to one of the desired formats */
5246  if (!direction)
5247  /* reading */
5248  res = ast_translator_best_choice(&fmt, &native);
5249  else
5250  /* writing */
5251  res = ast_translator_best_choice(&native, &fmt);
5252 
5253  if (res < 0) {
5254  ast_log(LOG_WARNING, "Unable to find a codec translation path from %s to %s\n",
5255  ast_getformatname_multiple(from, sizeof(from), native),
5256  ast_getformatname_multiple(to, sizeof(to), fmt));
5257  return -1;
5258  }
5259 
5260  /* Now we have a good choice for both. */
5261  ast_channel_lock(chan);
5262 
5263  if ((*rawformat == native) && (*format == fmt) && ((*rawformat == *format) || (*trans))) {
5264  /* the channel is already in these formats, so nothing to do */
5265  ast_channel_unlock(chan);
5266  return 0;
5267  }
5268 
5269  *rawformat = native;
5270  /* User perspective is fmt */
5271  *format = fmt;
5272  /* Free any read translation we have right now */
5273  if (*trans) {
5274  ast_translator_free_path(*trans);
5275  *trans = NULL;
5276  }
5277  /* Build a translation path from the raw format to the desired format */
5278  if (*format == *rawformat) {
5279  /*
5280  * If we were able to swap the native format to the format that
5281  * has been requested, then there is no need to try to build
5282  * a translation path.
5283  */
5284  res = 0;
5285  } else {
5286  if (!direction) {
5287  /* reading */
5288  *trans = ast_translator_build_path(*format, *rawformat);
5289  } else {
5290  /* writing */
5291  *trans = ast_translator_build_path(*rawformat, *format);
5292  }
5293  res = *trans ? 0 : -1;
5294  }
5295  ast_channel_unlock(chan);
5296  ast_debug(1, "Set channel %s to %s format %s\n", chan->name,
5297  direction ? "write" : "read", ast_getformatname(fmt));
5298  return res;
5299 }
5300 
5302 {
5303  return set_format(chan, fmt, &chan->rawreadformat, &chan->readformat,
5304  &chan->readtrans, 0);
5305 }
5306 
5308 {
5309  return set_format(chan, fmt, &chan->rawwriteformat, &chan->writeformat,
5310  &chan->writetrans, 1);
5311 }
5312 
5313 const char *ast_channel_reason2str(int reason)
5314 {
5315  switch (reason) /* the following appear to be the only ones actually returned by request_and_dial */
5316  {
5317  case 0:
5318  return "Call Failure (not BUSY, and not NO_ANSWER, maybe Circuit busy or down?)";
5319  case AST_CONTROL_HANGUP:
5320  return "Hangup";
5321  case AST_CONTROL_RING:
5322  return "Local Ring";
5323  case AST_CONTROL_RINGING:
5324  return "Remote end Ringing";
5325  case AST_CONTROL_ANSWER:
5326  return "Remote end has Answered";
5327  case AST_CONTROL_BUSY:
5328  return "Remote end is Busy";
5330  return "Congestion (circuits busy)";
5331  default:
5332  return "Unknown Reason!!";
5333  }
5334 }
5335 
5336 static void handle_cause(int cause, int *outstate)
5337 {
5338  if (outstate) {
5339  /* compute error and return */
5340  if (cause == AST_CAUSE_BUSY)
5341  *outstate = AST_CONTROL_BUSY;
5342  else if (cause == AST_CAUSE_CONGESTION)
5343  *outstate = AST_CONTROL_CONGESTION;
5344  else
5345  *outstate = 0;
5346  }
5347 }
5348 
5349 /*!
5350  * \internal
5351  * \brief Helper function to inherit info from parent channel.
5352  *
5353  * \param new_chan Channel inheriting information.
5354  * \param parent Channel new_chan inherits information.
5355  * \param orig Channel being replaced by the call forward channel.
5356  *
5357  * \return Nothing
5358  */
5359 static void call_forward_inherit(struct ast_channel *new_chan, struct ast_channel *parent, struct ast_channel *orig)
5360 {
5361  if (!ast_test_flag(parent, AST_FLAG_ZOMBIE) && !ast_check_hangup(parent)) {
5362  struct ast_party_redirecting redirecting;
5363 
5364  /*
5365  * The parent is not a ZOMBIE or hungup so update it with the
5366  * original channel's redirecting information.
5367  */
5368  ast_party_redirecting_init(&redirecting);
5369  ast_channel_lock(orig);
5370  ast_party_redirecting_copy(&redirecting, &orig->redirecting);
5371  ast_channel_unlock(orig);
5372  if (ast_channel_redirecting_macro(orig, parent, &redirecting, 1, 0)) {
5373  ast_channel_update_redirecting(parent, &redirecting, NULL);
5374  }
5375  ast_party_redirecting_free(&redirecting);
5376  }
5377 
5378  /* Safely inherit variables and datastores from the parent channel. */
5379  ast_channel_lock_both(parent, new_chan);
5380  ast_channel_inherit_variables(parent, new_chan);
5381  ast_channel_datastore_inherit(parent, new_chan);
5382  ast_channel_unlock(new_chan);
5383  ast_channel_unlock(parent);
5384 }
5385 
5386 struct ast_channel *ast_call_forward(struct ast_channel *caller, struct ast_channel *orig, int *timeout, format_t format, struct outgoing_helper *oh, int *outstate)
5387 {
5388  char tmpchan[256];
5389  struct ast_channel *new_chan = NULL;
5390  char *data, *type;
5391  int cause = 0;
5392  int res;
5393 
5394  /* gather data and request the new forward channel */
5395  ast_copy_string(tmpchan, orig->call_forward, sizeof(tmpchan));
5396  if ((data = strchr(tmpchan, '/'))) {
5397  *data++ = '\0';
5398  type = tmpchan;
5399  } else {
5400  const char *forward_context;
5401  ast_channel_lock(orig);
5402  forward_context = pbx_builtin_getvar_helper(orig, "FORWARD_CONTEXT");
5403  snprintf(tmpchan, sizeof(tmpchan), "%s@%s", orig->call_forward, S_OR(forward_context, orig->context));
5404  ast_channel_unlock(orig);
5405  data = tmpchan;
5406  type = "Local";
5407  }
5408  if (!(new_chan = ast_request(type, format, orig, data, &cause))) {
5409  ast_log(LOG_NOTICE, "Unable to create channel for call forward to '%s/%s' (cause = %d)\n", type, data, cause);
5410  handle_cause(cause, outstate);
5411  ast_hangup(orig);
5412  return NULL;
5413  }
5414 
5415  /* Copy/inherit important information into new channel */
5416  if (oh) {
5417  if (oh->vars) {
5418  ast_set_variables(new_chan, oh->vars);
5419  }
5420  if (oh->parent_channel) {
5421  call_forward_inherit(new_chan, oh->parent_channel, orig);
5422  }
5423  if (oh->account) {
5424  ast_channel_lock(new_chan);
5425  ast_cdr_setaccount(new_chan, oh->account);
5426  ast_channel_unlock(new_chan);
5427  }
5428  } else if (caller) { /* no outgoing helper so use caller if available */
5429  call_forward_inherit(new_chan, caller, orig);
5430  }
5431 
5432  ast_channel_lock_both(orig, new_chan);
5433  ast_copy_flags(new_chan->cdr, orig->cdr, AST_CDR_FLAG_ORIGINATED);
5434  ast_string_field_set(new_chan, accountcode, orig->accountcode);
5437  ast_channel_unlock(new_chan);
5438  ast_channel_unlock(orig);
5439 
5440  /* call new channel */
5441  res = ast_call(new_chan, data, 0);
5442  if (timeout) {
5443  *timeout = res;
5444  }
5445  if (res) {
5446  ast_log(LOG_NOTICE, "Unable to call forward to channel %s/%s\n", type, (char *)data);
5447  ast_hangup(orig);
5448  ast_hangup(new_chan);
5449  return NULL;
5450  }
5451  ast_hangup(orig);
5452 
5453  return new_chan;
5454 }
5455 
5456 struct ast_channel *__ast_request_and_dial(const char *type, format_t format, const struct ast_channel *requestor, void *data, int timeout, int *outstate, const char *cid_num, const char *cid_name, struct outgoing_helper *oh)
5457 {
5458  int dummy_outstate;
5459  int cause = 0;
5460  struct ast_channel *chan;
5461  int res = 0;
5462  int last_subclass = 0;
5463  struct ast_party_connected_line connected;
5464 
5465  if (outstate)
5466  *outstate = 0;
5467  else
5468  outstate = &dummy_outstate; /* make outstate always a valid pointer */
5469 
5470  chan = ast_request(type, format, requestor, data, &cause);
5471  if (!chan) {
5472  ast_log(LOG_NOTICE, "Unable to request channel %s/%s\n", type, (char *)data);
5473  handle_cause(cause, outstate);
5474  return NULL;
5475  }
5476 
5477  if (oh) {
5478  if (oh->vars) {
5479  ast_set_variables(chan, oh->vars);
5480  }
5481  if (!ast_strlen_zero(oh->cid_num) && !ast_strlen_zero(oh->cid_name)) {
5482  /*
5483  * Use the oh values instead of the function parameters for the
5484  * outgoing CallerID.
5485  */
5486  cid_num = oh->cid_num;
5487  cid_name = oh->cid_name;
5488  }
5489  if (oh->parent_channel) {
5490  /* Safely inherit variables and datastores from the parent channel. */
5495  ast_channel_unlock(chan);
5496  }
5497  if (oh->account) {
5498  ast_channel_lock(chan);
5499  ast_cdr_setaccount(chan, oh->account);
5500  ast_channel_unlock(chan);
5501  }
5502  }
5503 
5504  /*
5505  * I seems strange to set the CallerID on an outgoing call leg
5506  * to whom we are calling, but this function's callers are doing
5507  * various Originate methods. This call leg goes to the local
5508  * user. Once the local user answers, the dialplan needs to be
5509  * able to access the CallerID from the CALLERID function as if
5510  * the local user had placed this call.
5511  */
5512  ast_set_callerid(chan, cid_num, cid_name, cid_num);
5513 
5515  ast_party_connected_line_set_init(&connected, &chan->connected);
5516  if (cid_num) {
5517  connected.id.number.valid = 1;
5518  connected.id.number.str = (char *) cid_num;
5520  }
5521  if (cid_name) {
5522  connected.id.name.valid = 1;
5523  connected.id.name.str = (char *) cid_name;
5525  }
5526  ast_channel_set_connected_line(chan, &connected, NULL);
5527 
5528  if (ast_call(chan, data, 0)) { /* ast_call failed... */
5529  ast_log(LOG_NOTICE, "Unable to call channel %s/%s\n", type, (char *)data);
5530  } else {
5531  struct timeval start = ast_tvnow();
5532  res = 1; /* mark success in case chan->_state is already AST_STATE_UP */
5533  while (timeout && chan->_state != AST_STATE_UP) {
5534  struct ast_frame *f;
5535  int ms = ast_remaining_ms(start, timeout);
5536 
5537  res = ast_waitfor(chan, ms);
5538  if (res == 0) { /* timeout, treat it like ringing */
5539  *outstate = AST_CONTROL_RINGING;
5540  break;
5541  }
5542  if (res < 0) /* error or done */
5543  break;
5544  if (!ast_strlen_zero(chan->call_forward)) {
5545  if (!(chan = ast_call_forward(NULL, chan, NULL, format, oh, outstate))) {
5546  return NULL;
5547  }
5548  continue;
5549  }
5550 
5551  f = ast_read(chan);
5552  if (!f) {
5553  *outstate = AST_CONTROL_HANGUP;
5554  res = 0;
5555  break;
5556  }
5557  if (f->frametype == AST_FRAME_CONTROL) {
5558  switch (f->subclass.integer) {
5559  case AST_CONTROL_RINGING: /* record but keep going */
5560  *outstate = f->subclass.integer;
5561  break;
5562 
5563  case AST_CONTROL_BUSY:
5564  ast_cdr_busy(chan->cdr);
5565  *outstate = f->subclass.integer;
5566  timeout = 0;
5567  break;
5568 
5570  ast_cdr_failed(chan->cdr);
5571  *outstate = AST_CONTROL_CONGESTION;
5572  timeout = 0;
5573  break;
5574 
5576  ast_cdr_failed(chan->cdr);
5577  *outstate = f->subclass.integer;
5578  timeout = 0;
5579  break;
5580 
5581  case AST_CONTROL_ANSWER:
5582  ast_cdr_answer(chan->cdr);
5583  *outstate = f->subclass.integer;
5584  timeout = 0; /* trick to force exit from the while() */
5585  break;
5586 
5587  /* Ignore these */
5588  case AST_CONTROL_PROGRESS:
5590  case AST_CONTROL_HOLD:
5591  case AST_CONTROL_UNHOLD:
5592  case AST_CONTROL_VIDUPDATE:
5593  case AST_CONTROL_SRCUPDATE:
5594  case AST_CONTROL_SRCCHANGE:
5597  case AST_CONTROL_CC:
5598  case -1: /* Ignore -- just stopping indications */
5599  break;
5600 
5601  default:
5602  ast_log(LOG_NOTICE, "Don't know what to do with control frame %d\n", f->subclass.integer);
5603  }
5604  last_subclass = f->subclass.integer;
5605  }
5606  ast_frfree(f);
5607  }
5608  }
5609 
5610  /* Final fixups */
5611  if (oh) {
5612  if (!ast_strlen_zero(oh->context))
5613  ast_copy_string(chan->context, oh->context, sizeof(chan->context));
5614  if (!ast_strlen_zero(oh->exten))
5615  ast_copy_string(chan->exten, oh->exten, sizeof(chan->exten));
5616  if (oh->priority)
5617  chan->priority = oh->priority;
5618  }
5619  if (chan->_state == AST_STATE_UP)
5620  *outstate = AST_CONTROL_ANSWER;
5621 
5622  if (res <= 0) {
5623  ast_channel_lock(chan);
5624  if (AST_CONTROL_RINGING == last_subclass) {
5626  }
5627  if (!chan->cdr && (chan->cdr = ast_cdr_alloc())) {
5628  ast_cdr_init(chan->cdr, chan);
5629  }
5630  if (chan->cdr) {
5631  char tmp[256];
5632 
5633  snprintf(tmp, sizeof(tmp), "%s/%s", type, (char *)data);
5634  ast_cdr_setapp(chan->cdr, "Dial", tmp);
5635  ast_cdr_update(chan);
5636  ast_cdr_start(chan->cdr);
5637  ast_cdr_end(chan->cdr);
5638  /* If the cause wasn't handled properly */
5639  if (ast_cdr_disposition(chan->cdr, chan->hangupcause)) {
5640  ast_cdr_failed(chan->cdr);
5641  }
5642  }
5643  ast_channel_unlock(chan);
5644  ast_hangup(chan);
5645  chan = NULL;
5646  }
5647  return chan;
5648 }
5649 
5650 struct ast_channel *ast_request_and_dial(const char *type, format_t format, const struct ast_channel *requestor, void *data, int timeout, int *outstate, const char *cidnum, const char *cidname)
5651 {
5652  return __ast_request_and_dial(type, format, requestor, data, timeout, outstate, cidnum, cidname, NULL);
5653 }
5654 
5655 static int set_security_requirements(const struct ast_channel *requestor, struct ast_channel *out)
5656 {
5657  int ops[2][2] = {
5660  };
5661  int i;
5662  struct ast_channel *r = (struct ast_channel *) requestor; /* UGLY */
5663  struct ast_datastore *ds;
5664 
5665  if (!requestor || !out) {
5666  return 0;
5667  }
5668 
5669  ast_channel_lock(r);
5670  if ((ds = ast_channel_datastore_find(r, &secure_call_info, NULL))) {
5671  struct ast_secure_call_store *encrypt = ds->data;
5672  ops[0][1] = encrypt->signaling;
5673  ops[1][1] = encrypt->media;
5674  } else {
5675  ast_channel_unlock(r);
5676  return 0;
5677  }
5678  ast_channel_unlock(r);
5679 
5680  for (i = 0; i < 2; i++) {
5681  if (ops[i][1]) {
5682  if (ast_channel_setoption(out, ops[i][0], &ops[i][1], sizeof(ops[i][1]), 0)) {
5683  /* We require a security feature, but the channel won't provide it */
5684  return -1;
5685  }
5686  } else {
5687  /* We don't care if we can't clear the option on a channel that doesn't support it */
5688  ast_channel_setoption(out, ops[i][0], &ops[i][1], sizeof(ops[i][1]), 0);
5689  }
5690  }
5691 
5692  return 0;
5693 }
5694 
5695 struct ast_channel *ast_request(const char *type, format_t format, const struct ast_channel *requestor, void *data, int *cause)
5696 {
5697  struct chanlist *chan;
5698  struct ast_channel *c;
5700  format_t fmt;
5701  int res;
5702  int foo;
5703  format_t videoformat = format & AST_FORMAT_VIDEO_MASK;
5704  format_t textformat = format & AST_FORMAT_TEXT_MASK;
5705 
5706  if (!cause)
5707  cause = &foo;
5708  *cause = AST_CAUSE_NOTDEFINED;
5709 
5710  if (AST_RWLIST_RDLOCK(&backends)) {
5711  ast_log(LOG_WARNING, "Unable to lock technology backend list\n");
5712  return NULL;
5713  }
5714 
5715  AST_RWLIST_TRAVERSE(&backends, chan, list) {
5716  if (strcasecmp(type, chan->tech->type))
5717  continue;
5718 
5719  capabilities = chan->tech->capabilities;
5720  fmt = format & AST_FORMAT_AUDIO_MASK;
5721  if (fmt) {
5722  /* We have audio - is it possible to connect the various calls to each other?
5723  (Avoid this check for calls without audio, like text+video calls)
5724  */
5725  res = ast_translator_best_choice(&fmt, &capabilities);
5726  if (res < 0) {
5727  char tmp1[256], tmp2[256];
5728  ast_log(LOG_WARNING, "No translator path exists for channel type %s (native %s) to %s\n", type,
5729  ast_getformatname_multiple(tmp1, sizeof(tmp1), chan->tech->capabilities),
5730  ast_getformatname_multiple(tmp2, sizeof(tmp2), format));
5733  return NULL;
5734  }
5735  }
5737  if (!chan->tech->requester)
5738  return NULL;
5739 
5740  if (!(c = chan->tech->requester(type, capabilities | videoformat | textformat, requestor, data, cause)))
5741  return NULL;
5742 
5743  if (set_security_requirements(requestor, c)) {
5744  ast_log(LOG_WARNING, "Setting security requirements failed\n");
5745  c = ast_channel_release(c);
5747  return NULL;
5748  }
5749 
5750  /* no need to generate a Newchannel event here; it is done in the channel_alloc call */
5751  return c;
5752  }
5753 
5754  ast_log(LOG_WARNING, "No channel type registered for '%s'\n", type);
5755  *cause = AST_CAUSE_NOSUCHDRIVER;
5757 
5758  return NULL;
5759 }
5760 
5761 int ast_call(struct ast_channel *chan, char *addr, int timeout)
5762 {
5763  /* Place an outgoing call, but don't wait any longer than timeout ms before returning.
5764  If the remote end does not answer within the timeout, then do NOT hang up, but
5765  return anyway. */
5766  int res = -1;
5767  /* Stop if we're a zombie or need a soft hangup */
5768  ast_channel_lock(chan);
5769  if (!ast_test_flag(chan, AST_FLAG_ZOMBIE) && !ast_check_hangup(chan)) {
5770  if (chan->cdr) {
5772  }
5773  if (chan->tech->call)
5774  res = chan->tech->call(chan, addr, timeout);
5776  }
5777  ast_channel_unlock(chan);
5778  return res;
5779 }
5780 
5781 /*!
5782  \brief Transfer a call to dest, if the channel supports transfer
5783 
5784  Called by:
5785  \arg app_transfer
5786  \arg the manager interface
5787 */
5788 int ast_transfer(struct ast_channel *chan, char *dest)
5789 {
5790  int res = -1;
5791 
5792  /* Stop if we're a zombie or need a soft hangup */
5793  ast_channel_lock(chan);
5794  if (!ast_test_flag(chan, AST_FLAG_ZOMBIE) && !ast_check_hangup(chan)) {
5795  if (chan->tech->transfer) {
5796  res = chan->tech->transfer(chan, dest);
5797  if (!res)
5798  res = 1;
5799  } else
5800  res = 0;
5801  }
5802  ast_channel_unlock(chan);
5803 
5804  if (res <= 0) {
5805  return res;
5806  }
5807 
5808  for (;;) {
5809  struct ast_frame *fr;
5810 
5811  res = ast_waitfor(chan, -1);
5812 
5813  if (res < 0 || !(fr = ast_read(chan))) {
5814  res = -1;
5815  break;
5816  }
5817 
5819  enum ast_control_transfer *message = fr->data.ptr;
5820 
5821  if (*message == AST_TRANSFER_SUCCESS) {
5822  res = 1;
5823  } else {
5824  res = -1;
5825  }
5826 
5827  ast_frfree(fr);
5828  break;
5829  }
5830 
5831  ast_frfree(fr);
5832  }
5833 
5834  return res;
5835 }
5836 
5837 int ast_readstring(struct ast_channel *c, char *s, int len, int timeout, int ftimeout, char *enders)
5838 {
5839  return ast_readstring_full(c, s, len, timeout, ftimeout, enders, -1, -1);
5840 }
5841 
5842 int ast_readstring_full(struct ast_channel *c, char *s, int len, int timeout, int ftimeout, char *enders, int audiofd, int ctrlfd)
5843 {
5844  int pos = 0; /* index in the buffer where we accumulate digits */
5845  int to = ftimeout;
5846 
5847  struct ast_silence_generator *silgen = NULL;
5848 
5849  /* Stop if we're a zombie or need a soft hangup */
5851  return -1;
5852  if (!len)
5853  return -1;
5854  for (;;) {
5855  int d;
5856  if (c->stream) {
5857  d = ast_waitstream_full(c, AST_DIGIT_ANY, audiofd, ctrlfd);
5858  ast_stopstream(c);
5859  if (!silgen && ast_opt_transmit_silence)
5861  usleep(1000);
5862  if (!d)
5863  d = ast_waitfordigit_full(c, to, audiofd, ctrlfd);
5864  } else {
5865  if (!silgen && ast_opt_transmit_silence)
5867  d = ast_waitfordigit_full(c, to, audiofd, ctrlfd);
5868  }
5869  if (d < 0) {
5871  return AST_GETDATA_FAILED;
5872  }
5873  if (d == 0) {
5874  s[pos] = '\0';
5876  return AST_GETDATA_TIMEOUT;
5877  }
5878  if (d == 1) {
5879  s[pos] = '\0';
5881  return AST_GETDATA_INTERRUPTED;
5882  }
5883  if (strchr(enders, d) && (pos == 0)) {
5884  s[pos] = '\0';
5887  }
5888  if (!strchr(enders, d)) {
5889  s[pos++] = d;
5890  }
5891  if (strchr(enders, d) || (pos >= len)) {
5892  s[pos] = '\0';
5894  return AST_GETDATA_COMPLETE;
5895  }
5896  to = timeout;
5897  }
5898  /* Never reached */
5899  return 0;
5900 }
5901 
5903 {
5904  return (chan->tech->send_html) ? 1 : 0;
5905 }
5906 
5907 int ast_channel_sendhtml(struct ast_channel *chan, int subclass, const char *data, int datalen)
5908 {
5909  if (chan->tech->send_html)
5910  return chan->tech->send_html(chan, subclass, data, datalen);
5911  return -1;
5912 }
5913 
5914 int ast_channel_sendurl(struct ast_channel *chan, const char *url)
5915 {
5916  return ast_channel_sendhtml(chan, AST_HTML_URL, url, strlen(url) + 1);
5917 }
5918 
5919 /*! \brief Set up translation from one channel to another */
5920 static int ast_channel_make_compatible_helper(struct ast_channel *from, struct ast_channel *to)
5921 {
5922  format_t src, dst;
5923  int use_slin;
5924 
5925  /* See if the channel driver can natively make these two channels compatible */
5926  if (from->tech->bridge && from->tech->bridge == to->tech->bridge &&
5927  !ast_channel_setoption(from, AST_OPTION_MAKE_COMPATIBLE, to, sizeof(struct ast_channel *), 0)) {
5928  return 0;
5929  }
5930 
5931  if (from->readformat == to->writeformat && from->writeformat == to->readformat) {
5932  /* Already compatible! Moving on ... */
5933  return 0;
5934  }
5935 
5936  /* Set up translation from the 'from' channel to the 'to' channel */
5937  src = from->nativeformats;
5938  dst = to->nativeformats;
5939 
5940  /* If there's no audio in this call, don't bother with trying to find a translation path */
5941  if ((src & AST_FORMAT_AUDIO_MASK) == 0 || (dst & AST_FORMAT_AUDIO_MASK) == 0)
5942  return 0;
5943 
5944  if (ast_translator_best_choice(&dst, &src) < 0) {
5945  ast_log(LOG_WARNING, "No path to translate from %s to %s\n", from->name, to->name);
5946  return -1;
5947  }
5948 
5949  /* if the best path is not 'pass through', then
5950  * transcoding is needed; if desired, force transcode path
5951  * to use SLINEAR between channels, but only if there is
5952  * no direct conversion available. If generic PLC is
5953  * desired, then transcoding via SLINEAR is a requirement
5954  */
5955  use_slin = (src == AST_FORMAT_SLINEAR || dst == AST_FORMAT_SLINEAR);
5956  if ((src != dst) && (ast_opt_generic_plc || ast_opt_transcode_via_slin) &&
5957  (ast_translate_path_steps(dst, src) != 1 || use_slin))
5958  dst = AST_FORMAT_SLINEAR;
5959  if (ast_set_read_format(from, dst) < 0) {
5960  ast_log(LOG_WARNING, "Unable to set read format on channel %s to %s\n", from->name, ast_getformatname(dst));
5961  return -1;
5962  }
5963  if (ast_set_write_format(to, dst) < 0) {
5964  ast_log(LOG_WARNING, "Unable to set write format on channel %s to %s\n", to->name, ast_getformatname(dst));
5965  return -1;
5966  }
5967  return 0;
5968 }
5969 
5970 int ast_channel_make_compatible(struct ast_channel *chan, struct ast_channel *peer)
5971 {
5972  /* Some callers do not check return code, and we must try to set all call legs correctly */
5973  int rc = 0;
5974 
5975  /* Set up translation from the chan to the peer */
5976  rc = ast_channel_make_compatible_helper(chan, peer);
5977 
5978  if (rc < 0)
5979  return rc;
5980 
5981  /* Set up translation from the peer to the chan */
5982  rc = ast_channel_make_compatible_helper(peer, chan);
5983 
5984  return rc;
5985 }
5986 
5987 static int __ast_channel_masquerade(struct ast_channel *original, struct ast_channel *clonechan, struct ast_datastore *xfer_ds)
5988 {
5989  int res = -1;
5990  struct ast_channel *final_orig, *final_clone, *base;
5991 
5992  for (;;) {
5993  final_orig = original;
5994  final_clone = clonechan;
5995 
5996  ast_channel_lock_both(original, clonechan);
5997 
5998  if (ast_test_flag(original, AST_FLAG_ZOMBIE)
5999  || ast_test_flag(clonechan, AST_FLAG_ZOMBIE)) {
6000  /* Zombies! Run! */
6002  "Can't setup masquerade. One or both channels is dead. (%s <-- %s)\n",
6003  original->name, clonechan->name);
6004  ast_channel_unlock(clonechan);
6005  ast_channel_unlock(original);
6006  return -1;
6007  }
6008 
6009  /*
6010  * Each of these channels may be sitting behind a channel proxy
6011  * (i.e. chan_agent) and if so, we don't really want to
6012  * masquerade it, but its proxy
6013  */
6014  if (original->_bridge
6015  && (original->_bridge != ast_bridged_channel(original))
6016  && (original->_bridge->_bridge != original)) {
6017  final_orig = original->_bridge;
6018  }
6019  if (clonechan->_bridge
6020  && (clonechan->_bridge != ast_bridged_channel(clonechan))
6021  && (clonechan->_bridge->_bridge != clonechan)) {
6022  final_clone = clonechan->_bridge;
6023  }
6024  if (final_clone->tech->get_base_channel
6025  && (base = final_clone->tech->get_base_channel(final_clone))) {
6026  final_clone = base;
6027  }
6028 
6029  if ((final_orig != original) || (final_clone != clonechan)) {
6030  /*
6031  * Lots and lots of deadlock avoidance. The main one we're
6032  * competing with is ast_write(), which locks channels
6033  * recursively, when working with a proxy channel.
6034  */
6035  if (ast_channel_trylock(final_orig)) {
6036  ast_channel_unlock(clonechan);
6037  ast_channel_unlock(original);
6038 
6039  /* Try again */
6040  continue;
6041  }
6042  if (ast_channel_trylock(final_clone)) {
6043  ast_channel_unlock(final_orig);
6044  ast_channel_unlock(clonechan);
6045  ast_channel_unlock(original);
6046 
6047  /* Try again */
6048  continue;
6049  }
6050  ast_channel_unlock(clonechan);
6051  ast_channel_unlock(original);
6052  original = final_orig;
6053  clonechan = final_clone;
6054 
6055  if (ast_test_flag(original, AST_FLAG_ZOMBIE)
6056  || ast_test_flag(clonechan, AST_FLAG_ZOMBIE)) {
6057  /* Zombies! Run! */
6059  "Can't setup masquerade. One or both channels is dead. (%s <-- %s)\n",
6060  original->name, clonechan->name);
6061  ast_channel_unlock(clonechan);
6062  ast_channel_unlock(original);
6063  return -1;
6064  }
6065  }
6066  break;
6067  }
6068 
6069  if (original == clonechan) {
6070  ast_log(LOG_WARNING, "Can't masquerade channel '%s' into itself!\n", original->name);
6071  ast_channel_unlock(clonechan);
6072  ast_channel_unlock(original);
6073  return -1;
6074  }
6075 
6076  ast_debug(1, "Planning to masquerade channel %s into the structure of %s\n",
6077  clonechan->name, original->name);
6078 
6079  if (!original->masqr && !original->masq && !clonechan->masq && !clonechan->masqr) {
6080  original->masq = clonechan;
6081  clonechan->masqr = original;
6082  if (xfer_ds) {
6083  ast_channel_datastore_add(original, xfer_ds);
6084  }
6085  ast_queue_frame(original, &ast_null_frame);
6086  ast_queue_frame(clonechan, &ast_null_frame);
6087  ast_debug(1, "Done planning to masquerade channel %s into the structure of %s\n", clonechan->name, original->name);
6088  res = 0;
6089  } else if (original->masq) {
6090  ast_log(LOG_WARNING, "%s is already going to masquerade as %s\n",
6091  original->masq->name, original->name);
6092  } else if (original->masqr) {
6093  /* not yet as a previously planned masq hasn't yet happened */
6094  ast_log(LOG_WARNING, "%s is already going to masquerade as %s\n",
6095  original->name, original->masqr->name);
6096  } else if (clonechan->masq) {
6097  ast_log(LOG_WARNING, "%s is already going to masquerade as %s\n",
6098  clonechan->masq->name, clonechan->name);
6099  } else { /* (clonechan->masqr) */
6100  ast_log(LOG_WARNING, "%s is already going to masquerade as %s\n",
6101  clonechan->name, clonechan->masqr->name);
6102  }
6103 
6104  ast_channel_unlock(clonechan);
6105  ast_channel_unlock(original);
6106 
6107  return res;
6108 }
6109 
6110 int ast_channel_masquerade(struct ast_channel *original, struct ast_channel *clone)
6111 {
6112  return __ast_channel_masquerade(original, clone, NULL);
6113 }
6114 
6115 /*!
6116  * \internal
6117  * \brief Copy the source connected line information to the destination for a transfer.
6118  * \since 1.8
6119  *
6120  * \param dest Destination connected line
6121  * \param src Source connected line
6122  *
6123  * \return Nothing
6124  */
6126 {
6127  struct ast_party_connected_line connected;
6128 
6129  connected = *((struct ast_party_connected_line *) src);
6131 
6132  /* Make sure empty strings will be erased. */
6133  if (!connected.id.name.str) {
6134  connected.id.name.str = "";
6135  }
6136  if (!connected.id.number.str) {
6137  connected.id.number.str = "";
6138  }
6139  if (!connected.id.subaddress.str) {
6140  connected.id.subaddress.str = "";
6141  }
6142  if (!connected.id.tag) {
6143  connected.id.tag = "";
6144  }
6145 
6146  ast_party_connected_line_copy(dest, &connected);
6147 }
6148 
6149 /*! Transfer masquerade connected line exchange data. */
6151  /*! New ID for the target of the transfer (Masquerade original channel) */
6152  struct ast_party_connected_line target_id;
6153  /*! New ID for the transferee of the transfer (Masquerade clone channel) */
6154  struct ast_party_connected_line transferee_id;
6155  /*! TRUE if the target call is held. (Masquerade original channel) */
6157  /*! TRUE if the transferee call is held. (Masquerade clone channel) */
6159 };
6160 
6161 /*!
6162  * \internal
6163  * \brief Destroy the transfer connected line exchange datastore information.
6164  * \since 1.8
6165  *
6166  * \param data The datastore payload to destroy.
6167  *
6168  * \return Nothing
6169  */
6170 static void xfer_ds_destroy(void *data)
6171 {
6172  struct xfer_masquerade_ds *ds = data;
6173 
6176  ast_free(ds);
6177 }
6178 
6179 static const struct ast_datastore_info xfer_ds_info = {
6180  .type = "xfer_colp",
6181  .destroy = xfer_ds_destroy,
6182 };
6183 
6185  struct ast_channel *target_chan,
6186  const struct ast_party_connected_line *target_id,
6187  int target_held,
6188  struct ast_channel *transferee_chan,
6189  const struct ast_party_connected_line *transferee_id,
6190  int transferee_held)
6191 {
6192  struct ast_datastore *xfer_ds;
6193  struct xfer_masquerade_ds *xfer_colp;
6194  int res;
6195 
6196  xfer_ds = ast_datastore_alloc(&xfer_ds_info, NULL);
6197  if (!xfer_ds) {
6198  return -1;
6199  }
6200 
6201  xfer_colp = ast_calloc(1, sizeof(*xfer_colp));
6202  if (!xfer_colp) {
6203  ast_datastore_free(xfer_ds);
6204  return -1;
6205  }
6206  party_connected_line_copy_transfer(&xfer_colp->target_id, target_id);
6207  xfer_colp->target_held = target_held;
6208  party_connected_line_copy_transfer(&xfer_colp->transferee_id, transferee_id);
6209  xfer_colp->transferee_held = transferee_held;
6210  xfer_ds->data = xfer_colp;
6211 
6212  res = __ast_channel_masquerade(target_chan, transferee_chan, xfer_ds);
6213  if (res) {
6214  ast_datastore_free(xfer_ds);
6215  }
6216  return res;
6217 }
6218 
6219 /*! \brief this function simply changes the name of the channel and issues a manager_event
6220  * with out unlinking and linking the channel from the ao2_container. This should
6221  * only be used when the channel has already been unlinked from the ao2_container.
6222  */
6223 static void __ast_change_name_nolink(struct ast_channel *chan, const char *newname)
6224 {
6225  ast_manager_event(chan, EVENT_FLAG_CALL, "Rename", "Channel: %s\r\nNewname: %s\r\nUniqueid: %s\r\n", chan->name, newname, chan->uniqueid);
6226  ast_string_field_set(chan, name, newname);
6227 }
6228 
6229 void ast_change_name(struct ast_channel *chan, const char *newname)
6230 {
6231  /* We must re-link, as the hash value will change here. */
6232  ao2_lock(channels);
6233  ast_channel_lock(chan);
6234  ao2_unlink(channels, chan);
6235  __ast_change_name_nolink(chan, newname);
6236  ao2_link(channels, chan);
6237  ast_channel_unlock(chan);
6239 }
6240 
6241 void ast_channel_inherit_variables(const struct ast_channel *parent, struct ast_channel *child)
6242 {
6243  struct ast_var_t *current, *newvar;
6244  const char *varname;
6245 
6246  AST_LIST_TRAVERSE(&parent->varshead, current, entries) {
6247  int vartype = 0;
6248 
6249  varname = ast_var_full_name(current);
6250  if (!varname)
6251  continue;
6252 
6253  if (varname[0] == '_') {
6254  vartype = 1;
6255  if (varname[1] == '_')
6256  vartype = 2;
6257  }
6258 
6259  switch (vartype) {
6260  case 1:
6261  newvar = ast_var_assign(&varname[1], ast_var_value(current));
6262  if (newvar) {
6263  AST_LIST_INSERT_TAIL(&child->varshead, newvar, entries);
6264  ast_debug(1, "Inheriting variable %s from %s to %s.\n",
6265  ast_var_name(newvar), parent->name, child->name);
6266  }
6267  break;
6268  case 2:
6269  newvar = ast_var_assign(varname, ast_var_value(current));
6270  if (newvar) {
6271  AST_LIST_INSERT_TAIL(&child->varshead, newvar, entries);
6272  ast_debug(1, "Inheriting variable %s from %s to %s.\n",
6273  ast_var_name(newvar), parent->name, child->name);
6274  }
6275  break;
6276  default:
6277  break;
6278  }
6279  }
6280 }
6281 
6282 /*!
6283  \brief Clone channel variables from 'clone' channel into 'original' channel
6284 
6285  All variables except those related to app_groupcount are cloned.
6286  Variables are actually _removed_ from 'clone' channel, presumably
6287  because it will subsequently be destroyed.
6288 
6289  \note Assumes locks will be in place on both channels when called.
6290 */
6291 static void clone_variables(struct ast_channel *original, struct ast_channel *clonechan)
6292 {
6293  struct ast_var_t *current, *newvar;
6294  /* Append variables from clone channel into original channel */
6295  /* XXX Is this always correct? We have to in order to keep MACROS working XXX */
6296  AST_LIST_APPEND_LIST(&original->varshead, &clonechan->varshead, entries);
6297 
6298  /* then, dup the varshead list into the clone */
6299 
6300  AST_LIST_TRAVERSE(&original->varshead, current, entries) {
6301  newvar = ast_var_assign(current->name, current->value);
6302  if (newvar)
6303  AST_LIST_INSERT_TAIL(&clonechan->varshead, newvar, entries);
6304  }
6305 }
6306 
6307 
6308 
6309 /* return the oldest of two linkedids. linkedid is derived from
6310  uniqueid which is formed like this: [systemname-]ctime.seq
6311 
6312  The systemname, and the dash are optional, followed by the epoch
6313  time followed by an integer sequence. Note that this is not a
6314  decimal number, since 1.2 is less than 1.11 in uniqueid land.
6315 
6316  To compare two uniqueids, we parse out the integer values of the
6317  time and the sequence numbers and compare them, with time trumping
6318  sequence.
6319 */
6320 static const char *oldest_linkedid(const char *a, const char *b)
6321 {
6322  const char *satime, *saseq;
6323  const char *sbtime, *sbseq;
6324  const char *dash;
6325 
6326  unsigned int atime, aseq, btime, bseq;
6327 
6328  if (ast_strlen_zero(a))
6329  return b;
6330 
6331  if (ast_strlen_zero(b))
6332  return a;
6333 
6334  satime = a;
6335  sbtime = b;
6336 
6337  /* jump over the system name */
6338  if ((dash = strrchr(satime, '-'))) {
6339  satime = dash+1;
6340  }
6341  if ((dash = strrchr(sbtime, '-'))) {
6342  sbtime = dash+1;
6343  }
6344 
6345  /* the sequence comes after the '.' */
6346  saseq = strchr(satime, '.');
6347  sbseq = strchr(sbtime, '.');
6348  if (!saseq || !sbseq)
6349  return NULL;
6350  saseq++;
6351  sbseq++;
6352 
6353  /* convert it all to integers */
6354  atime = atoi(satime); /* note that atoi is ignoring the '.' after the time string */
6355  btime = atoi(sbtime); /* note that atoi is ignoring the '.' after the time string */
6356  aseq = atoi(saseq);
6357  bseq = atoi(sbseq);
6358 
6359  /* and finally compare */
6360  if (atime == btime) {
6361  return (aseq < bseq) ? a : b;
6362  }
6363  else {
6364  return (atime < btime) ? a : b;
6365  }
6366 }
6367 
6368 /*! Set the channel's linkedid to the given string, and also check to
6369  * see if the channel's old linkedid is now being retired */
6370 static void ast_channel_change_linkedid(struct ast_channel *chan, const char *linkedid)
6371 {
6372  ast_assert(linkedid != NULL);
6373  /* if the linkedid for this channel is being changed from something, check... */
6374  if (!strcmp(chan->linkedid, linkedid)) {
6375  return;
6376  }
6377 
6379  ast_string_field_set(chan, linkedid, linkedid);
6380  ast_cel_linkedid_ref(linkedid);
6381 }
6382 
6383 /*!
6384  \brief Propagate the oldest linkedid between associated channels
6385 
6386 */
6387 void ast_channel_set_linkgroup(struct ast_channel *chan, struct ast_channel *peer)
6388 {
6389  const char* linkedid=NULL;
6390  struct ast_channel *bridged;
6391 
6392  linkedid = oldest_linkedid(chan->linkedid, peer->linkedid);
6393  linkedid = oldest_linkedid(linkedid, chan->uniqueid);
6394  linkedid = oldest_linkedid(linkedid, peer->uniqueid);
6395  if (chan->_bridge) {
6396  bridged = ast_bridged_channel(chan);
6397  if (bridged && bridged != peer) {
6398  linkedid = oldest_linkedid(linkedid, bridged->linkedid);
6399  linkedid = oldest_linkedid(linkedid, bridged->uniqueid);
6400  }
6401  }
6402  if (peer->_bridge) {
6403  bridged = ast_bridged_channel(peer);
6404  if (bridged && bridged != chan) {
6405  linkedid = oldest_linkedid(linkedid, bridged->linkedid);
6406  linkedid = oldest_linkedid(linkedid, bridged->uniqueid);
6407  }
6408  }
6409 
6410  /* just in case setting a stringfield to itself causes problems */
6411  linkedid = ast_strdupa(linkedid);
6412 
6413  ast_channel_change_linkedid(chan, linkedid);
6414  ast_channel_change_linkedid(peer, linkedid);
6415  if (chan->_bridge) {
6416  bridged = ast_bridged_channel(chan);
6417  if (bridged && bridged != peer) {
6418  ast_channel_change_linkedid(bridged, linkedid);
6419  }
6420  }
6421  if (peer->_bridge) {
6422  bridged = ast_bridged_channel(peer);
6423  if (bridged && bridged != chan) {
6424  ast_channel_change_linkedid(bridged, linkedid);
6425  }
6426  }
6427 }
6428 
6429 /* copy accountcode and peeraccount across during a link */
6430 static void ast_set_owners_and_peers(struct ast_channel *chan1,
6431  struct ast_channel *chan2)
6432 {
6433  if (!ast_strlen_zero(chan1->accountcode) && ast_strlen_zero(chan2->peeraccount)) {
6434  ast_log(LOG_DEBUG, "setting peeraccount to %s for %s from data on channel %s\n",
6435  chan1->accountcode, chan2->name, chan1->name);
6437  }
6438  if (!ast_strlen_zero(chan2->accountcode) && ast_strlen_zero(chan1->peeraccount)) {
6439  ast_log(LOG_DEBUG, "setting peeraccount to %s for %s from data on channel %s\n",
6440  chan2->accountcode, chan1->name, chan2->name);
6442  }
6443  if (!ast_strlen_zero(chan1->peeraccount) && ast_strlen_zero(chan2->accountcode)) {
6444  ast_log(LOG_DEBUG, "setting accountcode to %s for %s from data on channel %s\n",
6445  chan1->peeraccount, chan2->name, chan1->name);
6447  }
6448  if (!ast_strlen_zero(chan2->peeraccount) && ast_strlen_zero(chan1->accountcode)) {
6449  ast_log(LOG_DEBUG, "setting accountcode to %s for %s from data on channel %s\n",
6450  chan2->peeraccount, chan1->name, chan2->name);
6452  }
6453  if (0 != strcmp(chan1->accountcode, chan2->peeraccount)) {
6454  ast_log(LOG_DEBUG, "changing peeraccount from %s to %s on %s to match channel %s\n",
6455  chan2->peeraccount, chan1->peeraccount, chan2->name, chan1->name);
6457  }
6458  if (0 != strcmp(chan2->accountcode, chan1->peeraccount)) {
6459  ast_log(LOG_DEBUG, "changing peeraccount from %s to %s on %s to match channel %s\n",
6460  chan1->peeraccount, chan2->peeraccount, chan1->name, chan2->name);
6462  }
6463 }
6464 
6465 /*!
6466  * \pre chan is locked
6467  */
6468 static void report_new_callerid(struct ast_channel *chan)
6469 {
6470  int pres;
6471 
6472  pres = ast_party_id_presentation(&chan->caller.id);
6473  ast_manager_event(chan, EVENT_FLAG_CALL, "NewCallerid",
6474  "Channel: %s\r\n"
6475  "CallerIDNum: %s\r\n"
6476  "CallerIDName: %s\r\n"
6477  "Uniqueid: %s\r\n"
6478  "CID-CallingPres: %d (%s)\r\n",
6479  chan->name,
6480  S_COR(chan->caller.id.number.valid, chan->caller.id.number.str, ""),
6481  S_COR(chan->caller.id.name.valid, chan->caller.id.name.str, ""),
6482  chan->uniqueid,
6483  pres,
6485  );
6486 }
6487 
6488 /*!
6489  * \internal
6490  * \brief Transfer COLP between target and transferee channels.
6491  * \since 1.8
6492  *
6493  * \param transferee Transferee channel to exchange connected line information.
6494  * \param colp Connected line information to exchange.
6495  *
6496  * \return Nothing
6497  */
6498 static void masquerade_colp_transfer(struct ast_channel *transferee, struct xfer_masquerade_ds *colp)
6499 {
6500  struct ast_control_read_action_payload *frame_payload;
6501  int payload_size;
6502  int frame_size;
6503  unsigned char connected_line_data[1024];
6504 
6505  /* Release any hold on the target. */
6506  if (colp->target_held) {
6508  }
6509 
6510  /*
6511  * Since transferee may not actually be bridged to another channel,
6512  * there is no way for us to queue a frame so that its connected
6513  * line status will be updated. Instead, we use the somewhat
6514  * hackish approach of using a special control frame type that
6515  * instructs ast_read() to perform a specific action. In this
6516  * case, the frame we queue tells ast_read() to call the
6517  * connected line interception macro configured for transferee.
6518  */
6519  payload_size = ast_connected_line_build_data(connected_line_data,
6520  sizeof(connected_line_data), &colp->target_id, NULL);
6521  if (payload_size != -1) {
6522  frame_size = payload_size + sizeof(*frame_payload);
6523  frame_payload = ast_alloca(frame_size);
6524  frame_payload->action = AST_FRAME_READ_ACTION_CONNECTED_LINE_MACRO;
6525  frame_payload->payload_size = payload_size;
6526  memcpy(frame_payload->payload, connected_line_data, payload_size);
6527  ast_queue_control_data(transferee, AST_CONTROL_READ_ACTION, frame_payload,
6528  frame_size);
6529  }
6530  /*
6531  * In addition to queueing the read action frame so that the
6532  * connected line info on transferee will be updated, we also are
6533  * going to queue a plain old connected line update on transferee to
6534  * update the target.
6535  */
6536  ast_channel_queue_connected_line_update(transferee, &colp->transferee_id, NULL);
6537 }
6538 
6539 /*!
6540  * \brief Masquerade a channel
6541  *
6542  * \note Assumes _NO_ channels and _NO_ channel pvt's are locked. If a channel is locked while calling
6543  * this function, it invalidates our channel container locking order. All channels
6544  * must be unlocked before it is permissible to lock the channels' ao2 container.
6545  */
6546 int ast_do_masquerade(struct ast_channel *original)
6547 {
6548  int x;
6549  int i;
6550  int origstate;
6551  unsigned int orig_disablestatecache;
6552  unsigned int clone_disablestatecache;
6553  int visible_indication;
6554  int moh_is_playing;
6555  int clone_was_zombie = 0;/*!< TRUE if the clonechan was a zombie before the masquerade. */
6556  struct ast_frame *current;
6557  const struct ast_channel_tech *t;
6558  void *t_pvt;
6559  union {
6560  struct ast_party_dialed dialed;
6561  struct ast_party_caller caller;
6562  struct ast_party_connected_line connected;
6563  struct ast_party_redirecting redirecting;
6564  } exchange;
6565  struct ast_channel *clonechan, *chans[2];
6566  struct ast_channel *bridged;
6567  struct ast_cdr *cdr;
6568  struct ast_datastore *xfer_ds;
6569  struct xfer_masquerade_ds *xfer_colp;
6570  format_t rformat;
6571  format_t wformat;
6572  format_t tmp_format;
6573  char newn[AST_CHANNEL_NAME];
6574  char orig[AST_CHANNEL_NAME];
6575  char masqn[AST_CHANNEL_NAME];
6576  char zombn[AST_CHANNEL_NAME];
6577  char clone_sending_dtmf_digit;
6578  struct timeval clone_sending_dtmf_tv;
6579 
6580  /* XXX This operation is a bit odd. We're essentially putting the guts of
6581  * the clone channel into the original channel. Start by killing off the
6582  * original channel's backend. While the features are nice, which is the
6583  * reason we're keeping it, it's still awesomely weird. XXX */
6584 
6585  /*
6586  * The reasoning for the channels ao2_container lock here is
6587  * complex.
6588  *
6589  * There is a race condition that exists for this function.
6590  * Since all pvt and channel locks must be let go before calling
6591  * ast_do_masquerade, it is possible that it could be called
6592  * multiple times for the same channel. In order to prevent the
6593  * race condition with competing threads to do the masquerade
6594  * and new masquerade attempts, the channels container must be
6595  * locked for the entire masquerade. The original and clonechan
6596  * need to be unlocked earlier to avoid potential deadlocks with
6597  * the chan_local deadlock avoidance method.
6598  *
6599  * The container lock blocks competing masquerade attempts from
6600  * starting as well as being necessary for proper locking order
6601  * because the channels must to be unlinked to change their
6602  * names.
6603  *
6604  * The original and clonechan locks must be held while the
6605  * channel contents are shuffled around for the masquerade.
6606  *
6607  * The masq and masqr pointers need to be left alone until the
6608  * masquerade has restabilized the channels to prevent another
6609  * masquerade request until the AST_FLAG_ZOMBIE can be set on
6610  * the clonechan.
6611  */
6612  ao2_lock(channels);
6613 
6614  /*
6615  * Lock the original channel to determine if the masquerade is
6616  * still required.
6617  */
6618  ast_channel_lock(original);
6619 
6620  clonechan = original->masq;
6621  if (!clonechan) {
6622  /*
6623  * The masq is already completed by another thread or never
6624  * needed to be done to begin with.
6625  */
6626  ast_channel_unlock(original);
6628  return 0;
6629  }
6630 
6631  /* Bump the refs to ensure that they won't dissapear on us. */
6632  ast_channel_ref(original);
6633  ast_channel_ref(clonechan);
6634 
6635  /* unlink from channels container as name (which is the hash value) will change */
6636  ao2_unlink(channels, original);
6637  ao2_unlink(channels, clonechan);
6638 
6639  /* Get any transfer masquerade connected line exchange data. */
6640  xfer_ds = ast_channel_datastore_find(original, &xfer_ds_info, NULL);
6641  if (xfer_ds) {
6642  ast_channel_datastore_remove(original, xfer_ds);
6643  xfer_colp = xfer_ds->data;
6644  } else {
6645  xfer_colp = NULL;
6646  }
6647 
6648  moh_is_playing = ast_test_flag(original, AST_FLAG_MOH);
6649 
6650  /*
6651  * Stop any visible indication on the original channel so we can
6652  * transfer it to the clonechan taking the original's place.
6653  */
6654  visible_indication = original->visible_indication;
6655  ast_channel_unlock(original);
6656  ast_indicate(original, -1);
6657 
6658  /*
6659  * Release any hold on the transferee channel before going any
6660  * further with the masquerade.
6661  */
6662  if (xfer_colp && xfer_colp->transferee_held) {
6663  ast_indicate(clonechan, AST_CONTROL_UNHOLD);
6664  }
6665 
6666  /* Start the masquerade channel contents rearangement. */
6667  ast_channel_lock_both(original, clonechan);
6668 
6669  ast_debug(4, "Actually Masquerading %s(%u) into the structure of %s(%u)\n",
6670  clonechan->name, clonechan->_state, original->name, original->_state);
6671 
6672  chans[0] = clonechan;
6673  chans[1] = original;
6674  ast_manager_event_multichan(EVENT_FLAG_CALL, "Masquerade", 2, chans,
6675  "Clone: %s\r\n"
6676  "CloneState: %s\r\n"
6677  "Original: %s\r\n"
6678  "OriginalState: %s\r\n",
6679  clonechan->name, ast_state2str(clonechan->_state), original->name, ast_state2str(original->_state));
6680 
6681  /*
6682  * Remember the original read/write formats. We turn off any
6683  * translation on either one
6684  */
6685  rformat = original->readformat;
6686  wformat = original->writeformat;
6687  free_translation(clonechan);
6688  free_translation(original);
6689 
6690  /* Save the current DTMF digit being sent if any. */
6691  clone_sending_dtmf_digit = clonechan->sending_dtmf_digit;
6692  clone_sending_dtmf_tv = clonechan->sending_dtmf_tv;
6693 
6694  /* Save the original name */
6695  ast_copy_string(orig, original->name, sizeof(orig));
6696  /* Save the new name */
6697  ast_copy_string(newn, clonechan->name, sizeof(newn));
6698  /* Create the masq name */
6699  snprintf(masqn, sizeof(masqn), "%s<MASQ>", newn);
6700 
6701  /* Mangle the name of the clone channel */
6702  __ast_change_name_nolink(clonechan, masqn);
6703 
6704  /* Copy the name from the clone channel */
6705  __ast_change_name_nolink(original, newn);
6706 
6707  /* share linked id's */
6708  ast_channel_set_linkgroup(original, clonechan);
6709 
6710  /* Swap the technologies */
6711  t = original->tech;
6712  original->tech = clonechan->tech;
6713  clonechan->tech = t;
6714 
6715  t_pvt = original->tech_pvt;
6716  original->tech_pvt = clonechan->tech_pvt;
6717  clonechan->tech_pvt = t_pvt;
6718 
6719  /* Swap the cdrs */
6720  cdr = original->cdr;
6721  /* swap cdr uniqueid between channels, to get cdr uniqueid = channel uniqueid */
6722  ast_copy_string(clonechan->cdr->uniqueid, cdr->uniqueid, sizeof(cdr->uniqueid));
6723  original->cdr = clonechan->cdr;
6724  clonechan->cdr = cdr;
6725 
6726  /* Swap the alertpipes */
6727  for (i = 0; i < 2; i++) {
6728  x = original->alertpipe[i];
6729  original->alertpipe[i] = clonechan->alertpipe[i];
6730  clonechan->alertpipe[i] = x;
6731  }
6732 
6733  /*
6734  * Swap the readq's. The end result should be this:
6735  *
6736  * 1) All frames should be on the new (original) channel.
6737  * 2) Any frames that were already on the new channel before this
6738  * masquerade need to be at the end of the readq, after all of the
6739  * frames on the old (clone) channel.
6740  * 3) The alertpipe needs to get poked for every frame that was already
6741  * on the new channel, since we are now using the alert pipe from the
6742  * old (clone) channel.
6743  */
6744  {
6745  AST_LIST_HEAD_NOLOCK(, ast_frame) tmp_readq;
6746 
6747  AST_LIST_HEAD_INIT_NOLOCK(&tmp_readq);
6748  AST_LIST_APPEND_LIST(&tmp_readq, &original->readq, frame_list);
6749  AST_LIST_APPEND_LIST(&original->readq, &clonechan->readq, frame_list);
6750 
6751  while ((current = AST_LIST_REMOVE_HEAD(&tmp_readq, frame_list))) {
6752  AST_LIST_INSERT_TAIL(&original->readq, current, frame_list);
6753  if (original->alertpipe[1] > -1) {
6754  int poke = 0;
6755 
6756  if (write(original->alertpipe[1], &poke, sizeof(poke)) < 0) {
6757  ast_log(LOG_WARNING, "write() failed: %s\n", strerror(errno));
6758  }
6759  }
6760  }
6761  }
6762 
6763  /* Swap the raw formats */
6764  tmp_format = original->rawreadformat;
6765  original->rawreadformat = clonechan->rawreadformat;
6766  clonechan->rawreadformat = tmp_format;
6767 
6768  tmp_format = original->rawwriteformat;
6769  original->rawwriteformat = clonechan->rawwriteformat;
6770  clonechan->rawwriteformat = tmp_format;
6771 
6772  clonechan->_softhangup = AST_SOFTHANGUP_DEV;
6773 
6774  /* And of course, so does our current state. Note we need not
6775  call ast_setstate since the event manager doesn't really consider
6776  these separate. We do this early so that the clone has the proper
6777  state of the original channel. */
6778  origstate = original->_state;
6779  original->_state = clonechan->_state;
6780  clonechan->_state = origstate;
6781 
6782  /* And the swap the cachable state too. Otherwise we'd start caching
6783  * Local channels and ignoring real ones. */
6784  orig_disablestatecache = ast_test_flag(original, AST_FLAG_DISABLE_DEVSTATE_CACHE);
6785  clone_disablestatecache = ast_test_flag(clonechan, AST_FLAG_DISABLE_DEVSTATE_CACHE);
6786  if (orig_disablestatecache != clone_disablestatecache) {
6787  if (orig_disablestatecache) {
6790  } else {
6793  }
6794  }
6795 
6796  /* Mangle the name of the clone channel */
6797  snprintf(zombn, sizeof(zombn), "%s<ZOMBIE>", orig); /* quick, hide the brains! */
6798  __ast_change_name_nolink(clonechan, zombn);
6799 
6800  /* Update the type. */
6801  t_pvt = original->monitor;
6802  original->monitor = clonechan->monitor;
6803  clonechan->monitor = t_pvt;
6804 
6805  /* Keep the same language. */
6806  ast_string_field_set(original, language, clonechan->language);
6807 
6808  /* Keep the same parkinglot. */
6809  ast_string_field_set(original, parkinglot, clonechan->parkinglot);
6810 
6811  /* Copy the FD's other than the generator fd */
6812  for (x = 0; x < AST_MAX_FDS; x++) {
6813  if (x != AST_GENERATOR_FD)
6814  ast_channel_set_fd(original, x, clonechan->fds[x]);
6815  }
6816 
6817  ast_app_group_update(clonechan, original);
6818 
6819  /* Move data stores over */
6820  if (AST_LIST_FIRST(&clonechan->datastores)) {
6821  struct ast_datastore *ds;
6822  /* We use a safe traversal here because some fixup routines actually
6823  * remove the datastore from the list and free them.
6824  */
6825  AST_LIST_TRAVERSE_SAFE_BEGIN(&clonechan->datastores, ds, entry) {
6826  if (ds->info->chan_fixup)
6827  ds->info->chan_fixup(ds->data, clonechan, original);
6828  }
6830  AST_LIST_APPEND_LIST(&original->datastores, &clonechan->datastores, entry);
6831  }
6832 
6833  ast_autochan_new_channel(clonechan, original);
6834 
6835  clone_variables(original, clonechan);
6836  /* Presense of ADSI capable CPE follows clone */
6837  original->adsicpe = clonechan->adsicpe;
6838  /* Bridge remains the same */
6839  /* CDR fields remain the same */
6840  /* XXX What about blocking, softhangup, blocker, and lock and blockproc? XXX */
6841  /* Application and data remain the same */
6842  /* Clone exception becomes real one, as with fdno */
6844  original->fdno = clonechan->fdno;
6845  /* Schedule context remains the same */
6846  /* Stream stuff stays the same */
6847  /* Keep the original state. The fixup code will need to work with it most likely */
6848 
6849  /*
6850  * Just swap the whole structures, nevermind the allocations,
6851  * they'll work themselves out.
6852  */
6853  exchange.dialed = original->dialed;
6854  original->dialed = clonechan->dialed;
6855  clonechan->dialed = exchange.dialed;
6856 
6857  exchange.caller = original->caller;
6858  original->caller = clonechan->caller;
6859  clonechan->caller = exchange.caller;
6860 
6861  exchange.connected = original->connected;
6862  original->connected = clonechan->connected;
6863  clonechan->connected = exchange.connected;
6864 
6865  exchange.redirecting = original->redirecting;
6866  original->redirecting = clonechan->redirecting;
6867  clonechan->redirecting = exchange.redirecting;
6868 
6869  report_new_callerid(original);
6870 
6871  /* Restore original timing file descriptor */
6872  ast_channel_set_fd(original, AST_TIMING_FD, original->timingfd);
6873 
6874  /* Our native formats are different now */
6875  original->nativeformats = clonechan->nativeformats;
6876 
6877  /* Context, extension, priority, app data, jump table, remain the same */
6878  /* pvt switches. pbx stays the same, as does next */
6879 
6880  /* Set the write format */
6881  ast_set_write_format(original, wformat);
6882 
6883  /* Set the read format */
6884  ast_set_read_format(original, rformat);
6885 
6886  /* Copy the music class */
6887  ast_string_field_set(original, musicclass, clonechan->musicclass);
6888 
6889  /* Copy whentohangup time */
6890  original->whentohangup = clonechan->whentohangup;
6891 
6892  /* copy over accuntcode and set peeraccount across the bridge */
6893  ast_string_field_set(original, accountcode, S_OR(clonechan->accountcode, ""));
6894  if (original->_bridge) {
6895  /* XXX - should we try to lock original->_bridge here? */
6896  ast_string_field_set(original->_bridge, peeraccount, S_OR(clonechan->accountcode, ""));
6897  ast_cel_report_event(original, AST_CEL_BRIDGE_UPDATE, NULL, NULL, NULL);
6898  }
6899 
6900  ast_debug(1, "Putting channel %s in %s/%s formats\n", original->name,
6901  ast_getformatname(wformat), ast_getformatname(rformat));
6902 
6903  /* Fixup the original clonechan's physical side */
6904  if (original->tech->fixup && original->tech->fixup(clonechan, original)) {
6905  ast_log(LOG_WARNING, "Channel type '%s' could not fixup channel %s, strange things may happen. (clonechan)\n",
6906  original->tech->type, original->name);
6907  }
6908 
6909  /* Fixup the original original's physical side */
6910  if (clonechan->tech->fixup && clonechan->tech->fixup(original, clonechan)) {
6911  ast_log(LOG_WARNING, "Channel type '%s' could not fixup channel %s, strange things may happen. (original)\n",
6912  clonechan->tech->type, clonechan->name);
6913  }
6914 
6915  /*
6916  * Now, at this point, the "clone" channel is totally F'd up.
6917  * We mark it as a zombie so nothing tries to touch it. If it's
6918  * already been marked as a zombie, then we must free it (since
6919  * it already is considered invalid).
6920  *
6921  * This must be done before we unlock clonechan to prevent
6922  * setting up another masquerade on the clonechan.
6923  */
6924  if (ast_test_flag(clonechan, AST_FLAG_ZOMBIE)) {
6925  clone_was_zombie = 1;
6926  } else {
6927  ast_set_flag(clonechan, AST_FLAG_ZOMBIE);
6928  ast_queue_frame(clonechan, &ast_null_frame);
6929  }
6930 
6931  /* clear the masquerade channels */
6932  original->masq = NULL;
6933  clonechan->masqr = NULL;
6934 
6935  /*
6936  * When we unlock original here, it can be immediately setup to
6937  * masquerade again or hungup. The new masquerade or hangup
6938  * will not actually happen until we release the channels
6939  * container lock.
6940  */
6941  ast_channel_unlock(original);
6942 
6943  /* Disconnect the original original's physical side */
6944  if (clonechan->tech->hangup && clonechan->tech->hangup(clonechan)) {
6945  ast_log(LOG_WARNING, "Hangup failed! Strange things may happen!\n");
6946  } else {
6947  /*
6948  * We just hung up the original original's physical side of the
6949  * channel. Set the new zombie to use the kill channel driver
6950  * for safety.
6951  */
6952  clonechan->tech = &ast_kill_tech;
6953  }
6954 
6955  ast_channel_unlock(clonechan);
6956 
6957  if (clone_sending_dtmf_digit) {
6958  /*
6959  * The clonechan was sending a DTMF digit that was not completed
6960  * before the masquerade.
6961  */
6962  ast_bridge_end_dtmf(original, clone_sending_dtmf_digit, clone_sending_dtmf_tv,
6963  "masquerade");
6964  }
6965 
6966  /*
6967  * If an indication is currently playing, maintain it on the
6968  * channel that is taking the place of original.
6969  *
6970  * This is needed because the masquerade is swapping out the
6971  * internals of the channel, and the new channel private data
6972  * needs to be made aware of the current visible indication
6973  * (RINGING, CONGESTION, etc.)
6974  */
6975  if (visible_indication) {
6976  ast_indicate(original, visible_indication);
6977  }
6978 
6979  /* if moh is playing on the original channel then it needs to be
6980  maintained on the channel that is replacing it. */
6981  if (moh_is_playing) {
6982  ast_moh_start(original, NULL, NULL);
6983  }
6984 
6985  ast_channel_lock(original);
6986 
6987  /* Signal any blocker */
6988  if (ast_test_flag(original, AST_FLAG_BLOCKING)) {
6989  pthread_kill(original->blocker, SIGURG);
6990  }
6991 
6992  ast_debug(1, "Done Masquerading %s (%u)\n", original->name, original->_state);
6993 
6994  if ((bridged = ast_bridged_channel(original))) {
6995  ast_channel_ref(bridged);
6996  ast_channel_unlock(original);
6998  ast_channel_unref(bridged);
6999  } else {
7000  ast_channel_unlock(original);
7001  }
7003 
7004  if (xfer_colp) {
7005  /*
7006  * After the masquerade, the original channel pointer actually
7007  * points to the new transferee channel and the bridged channel
7008  * is still the intended transfer target party.
7009  */
7010  masquerade_colp_transfer(original, xfer_colp);
7011  }
7012 
7013  if (xfer_ds) {
7014  ast_datastore_free(xfer_ds);
7015  }
7016 
7017  if (clone_was_zombie) {
7018  ast_channel_lock(clonechan);
7019  ast_debug(1, "Destroying channel clone '%s'\n", clonechan->name);
7020  ast_manager_event(clonechan, EVENT_FLAG_CALL, "Hangup",
7021  "Channel: %s\r\n"
7022  "Uniqueid: %s\r\n"
7023  "Cause: %d\r\n"
7024  "Cause-txt: %s\r\n",
7025  clonechan->name,
7026  clonechan->uniqueid,
7027  clonechan->hangupcause,
7028  ast_cause2str(clonechan->hangupcause)
7029  );
7030  ast_channel_unlock(clonechan);
7031 
7032  /*
7033  * Drop the system reference to destroy the channel since it is
7034  * already unlinked.
7035  */
7036  ast_channel_unref(clonechan);
7037  } else {
7038  ao2_link(channels, clonechan);
7039  }
7040 
7041  ao2_link(channels, original);
7043 
7044  /* Release our held safety references. */
7045  ast_channel_unref(original);
7046  ast_channel_unref(clonechan);
7047 
7048  return 0;
7049 }
7050 
7051 void ast_set_callerid(struct ast_channel *chan, const char *cid_num, const char *cid_name, const char *cid_ani)
7052 {
7053  ast_channel_lock(chan);
7054 
7055  if (cid_num) {
7056  chan->caller.id.number.valid = 1;
7057  ast_free(chan->caller.id.number.str);
7058  chan->caller.id.number.str = ast_strdup(cid_num);
7059  }
7060  if (cid_name) {
7061  chan->caller.id.name.valid = 1;
7062  ast_free(chan->caller.id.name.str);
7063  chan->caller.id.name.str = ast_strdup(cid_name);
7064  }
7065  if (cid_ani) {
7066  chan->caller.ani.number.valid = 1;
7067  ast_free(chan->caller.ani.number.str);
7068  chan->caller.ani.number.str = ast_strdup(cid_ani);
7069  }
7070  if (chan->cdr) {
7071  ast_cdr_setcid(chan->cdr, chan);
7072  }
7073 
7074  report_new_callerid(chan);
7075 
7076  ast_channel_unlock(chan);
7077 }
7078 
7079 void ast_channel_set_caller(struct ast_channel *chan, const struct ast_party_caller *caller, const struct ast_set_party_caller *update)
7080 {
7081  if (&chan->caller == caller) {
7082  /* Don't set to self */
7083  return;
7084  }
7085 
7086  ast_channel_lock(chan);
7087  ast_party_caller_set(&chan->caller, caller, update);
7088  ast_channel_unlock(chan);
7089 }
7090 
7091 void ast_channel_set_caller_event(struct ast_channel *chan, const struct ast_party_caller *caller, const struct ast_set_party_caller *update)
7092 {
7093  const char *pre_set_number;
7094  const char *pre_set_name;
7095 
7096  if (&chan->caller == caller) {
7097  /* Don't set to self */
7098  return;
7099  }
7100 
7101  ast_channel_lock(chan);
7102  pre_set_number =
7103  S_COR(chan->caller.id.number.valid, chan->caller.id.number.str, NULL);
7104  pre_set_name = S_COR(chan->caller.id.name.valid, chan->caller.id.name.str, NULL);
7105  ast_party_caller_set(&chan->caller, caller, update);
7106  if (S_COR(chan->caller.id.number.valid, chan->caller.id.number.str, NULL)
7107  != pre_set_number
7108  || S_COR(chan->caller.id.name.valid, chan->caller.id.name.str, NULL)
7109  != pre_set_name) {
7110  /* The caller id name or number changed. */
7111  report_new_callerid(chan);
7112  }
7113  if (chan->cdr) {
7114  ast_cdr_setcid(chan->cdr, chan);
7115  }
7116  ast_channel_unlock(chan);
7117 }
7118 
7120 {
7121  int oldstate = chan->_state;
7122  char name[AST_CHANNEL_NAME], *dashptr;
7123 
7124  if (oldstate == state)
7125  return 0;
7126 
7127  ast_copy_string(name, chan->name, sizeof(name));
7128  if ((dashptr = strrchr(name, '-'))) {
7129  *dashptr = '\0';
7130  }
7131 
7132  chan->_state = state;
7133 
7134  /* We have to pass AST_DEVICE_UNKNOWN here because it is entirely possible that the channel driver
7135  * for this channel is using the callback method for device state. If we pass in an actual state here
7136  * we override what they are saying the state is and things go amuck. */
7138 
7139  /* setstate used to conditionally report Newchannel; this is no more */
7140  ast_manager_event(chan, EVENT_FLAG_CALL, "Newstate",
7141  "Channel: %s\r\n"
7142  "ChannelState: %u\r\n"
7143  "ChannelStateDesc: %s\r\n"
7144  "CallerIDNum: %s\r\n"
7145  "CallerIDName: %s\r\n"
7146  "ConnectedLineNum: %s\r\n"
7147  "ConnectedLineName: %s\r\n"
7148  "Uniqueid: %s\r\n",
7149  chan->name, chan->_state, ast_state2str(chan->_state),
7150  S_COR(chan->caller.id.number.valid, chan->caller.id.number.str, ""),
7151  S_COR(chan->caller.id.name.valid, chan->caller.id.name.str, ""),
7152  S_COR(chan->connected.id.number.valid, chan->connected.id.number.str, ""),
7153  S_COR(chan->connected.id.name.valid, chan->connected.id.name.str, ""),
7154  chan->uniqueid);
7155 
7156  return 0;
7157 }
7158 
7159 /*! \brief Find bridged channel */
7161 {
7162  struct ast_channel *bridged;
7163  bridged = chan->_bridge;
7164  if (bridged && bridged->tech->bridged_channel)
7165  bridged = bridged->tech->bridged_channel(chan, bridged);
7166  return bridged;
7167 }
7168 
7169 static void bridge_playfile(struct ast_channel *chan, struct ast_channel *peer, const char *sound, int remain)
7170 {
7171  int min = 0, sec = 0, check;
7172 
7173  check = ast_autoservice_start(peer);
7174  if (check)
7175  return;
7176 
7177  if (remain > 0) {
7178  if (remain / 60 > 1) {
7179  min = remain / 60;
7180  sec = remain % 60;
7181  } else {
7182  sec = remain;
7183  }
7184  }
7185 
7186  if (!strcmp(sound,"timeleft")) { /* Queue support */
7187  ast_stream_and_wait(chan, "vm-youhave", "");
7188  if (min) {
7189  ast_say_number(chan, min, AST_DIGIT_ANY, chan->language, NULL);
7190  ast_stream_and_wait(chan, "queue-minutes", "");
7191  }
7192  if (sec) {
7193  ast_say_number(chan, sec, AST_DIGIT_ANY, chan->language, NULL);
7194  ast_stream_and_wait(chan, "queue-seconds", "");
7195  }
7196  } else {
7197  ast_stream_and_wait(chan, sound, "");
7198  }
7199 
7200  ast_autoservice_stop(peer);
7201 }
7202 
7204  struct ast_bridge_config *config, struct ast_frame **fo,
7205  struct ast_channel **rc)
7206 {
7207  /* Copy voice back and forth between the two channels. */
7208  struct ast_channel *cs[3];
7209  struct ast_frame *f;
7211  format_t o0nativeformats;
7212  format_t o1nativeformats;
7213  int watch_c0_dtmf;
7214  int watch_c1_dtmf;
7215  void *pvt0, *pvt1;
7216  /* Indicates whether a frame was queued into a jitterbuffer */
7217  int frame_put_in_jb = 0;
7218  int jb_in_use;
7219  int to;
7220 
7221  cs[0] = c0;
7222  cs[1] = c1;
7223  pvt0 = c0->tech_pvt;
7224  pvt1 = c1->tech_pvt;
7225  o0nativeformats = c0->nativeformats;
7226  o1nativeformats = c1->nativeformats;
7227  watch_c0_dtmf = config->flags & AST_BRIDGE_DTMF_CHANNEL_0;
7228  watch_c1_dtmf = config->flags & AST_BRIDGE_DTMF_CHANNEL_1;
7229 
7230  /* Check the need of a jitterbuffer for each channel */
7231  jb_in_use = ast_jb_do_usecheck(c0, c1);
7232  if (jb_in_use)
7233  ast_jb_empty_and_reset(c0, c1);
7234 
7235  ast_poll_channel_add(c0, c1);
7236 
7237  if (config->feature_timer > 0 && ast_tvzero(config->nexteventts)) {
7238  /* nexteventts is not set when the bridge is not scheduled to
7239  * break, so calculate when the bridge should possibly break
7240  * if a partial feature match timed out */
7241  config->nexteventts = ast_tvadd(ast_tvnow(), ast_samp2tv(config->feature_timer, 1000));
7242  }
7243 
7244  for (;;) {
7245  struct ast_channel *who, *other;
7246 
7247  if ((c0->tech_pvt != pvt0) || (c1->tech_pvt != pvt1) ||
7248  (o0nativeformats != c0->nativeformats) ||
7249  (o1nativeformats != c1->nativeformats)) {
7250  /* Check for Masquerade, codec changes, etc */
7251  res = AST_BRIDGE_RETRY;
7252  break;
7253  }
7254  if (config->nexteventts.tv_sec) {
7255  to = ast_tvdiff_ms(config->nexteventts, ast_tvnow());
7256  if (to <= 0) {
7257  if (config->timelimit && !config->feature_timer && !ast_test_flag(config, AST_FEATURE_WARNING_ACTIVE)) {
7258  res = AST_BRIDGE_RETRY;
7259  /* generic bridge ending to play warning */
7261  } else if (config->feature_timer) {
7262  /* feature timer expired - make sure we do not play warning */
7264  res = AST_BRIDGE_RETRY;
7265  } else {
7266  res = AST_BRIDGE_COMPLETE;
7267  }
7268  break;
7269  }
7270  } else {
7271  /* If a feature has been started and the bridge is configured to
7272  * to not break, leave the channel bridge when the feature timer
7273  * time has elapsed so the DTMF will be sent to the other side.
7274  */
7275  if (!ast_tvzero(config->nexteventts)) {
7276  int diff = ast_tvdiff_ms(config->nexteventts, ast_tvnow());
7277  if (diff <= 0) {
7278  res = AST_BRIDGE_RETRY;
7279  break;
7280  }
7281  }
7282  to = -1;
7283  }
7284  /* Calculate the appropriate max sleep interval - in general, this is the time,
7285  left to the closest jb delivery moment */
7286  if (jb_in_use)
7287  to = ast_jb_get_when_to_wakeup(c0, c1, to);
7288  who = ast_waitfor_n(cs, 2, &to);
7289  if (!who) {
7290  /* No frame received within the specified timeout - check if we have to deliver now */
7291  if (jb_in_use)
7292  ast_jb_get_and_deliver(c0, c1);
7293  if ((c0->_softhangup | c1->_softhangup) & AST_SOFTHANGUP_UNBRIDGE) {/* Bit operators are intentional. */
7296  }
7299  }
7300  ast_channel_lock_both(c0, c1);
7301  c0->_bridge = c1;
7302  c1->_bridge = c0;
7303  ast_channel_unlock(c0);
7304  ast_channel_unlock(c1);
7305  }
7306  continue;
7307  }
7308  f = ast_read(who);
7309  if (!f) {
7310  *fo = NULL;
7311  *rc = who;
7312  ast_debug(1, "Didn't get a frame from channel: %s\n",who->name);
7313  break;
7314  }
7315 
7316  other = (who == c0) ? c1 : c0; /* the 'other' channel */
7317  /* Try add the frame info the who's bridged channel jitterbuff */
7318  if (jb_in_use)
7319  frame_put_in_jb = !ast_jb_put(other, f);
7320 
7321  if ((f->frametype == AST_FRAME_CONTROL) && !(config->flags & AST_BRIDGE_IGNORE_SIGS)) {
7322  int bridge_exit = 0;
7323 
7324  switch (f->subclass.integer) {
7325  case AST_CONTROL_AOC:
7326  ast_indicate_data(other, f->subclass.integer, f->data.ptr, f->datalen);
7327  break;
7329  if (ast_channel_redirecting_macro(who, other, f, other == c0, 1)) {
7330  ast_indicate_data(other, f->subclass.integer, f->data.ptr, f->datalen);
7331  }
7332  break;
7334  if (ast_channel_connected_line_macro(who, other, f, other == c0, 1)) {
7335  ast_indicate_data(other, f->subclass.integer, f->data.ptr, f->datalen);
7336  }
7337  break;
7338  case AST_CONTROL_HOLD:
7339  case AST_CONTROL_UNHOLD:
7340  case AST_CONTROL_VIDUPDATE:
7341  case AST_CONTROL_SRCUPDATE:
7342  case AST_CONTROL_SRCCHANGE:
7344  ast_indicate_data(other, f->subclass.integer, f->data.ptr, f->datalen);
7345  if (jb_in_use) {
7346  ast_jb_empty_and_reset(c0, c1);
7347  }
7348  break;
7349  default:
7350  *fo = f;
7351  *rc = who;
7352  bridge_exit = 1;
7353  ast_debug(1, "Got a FRAME_CONTROL (%d) frame on channel %s\n", f->subclass.integer, who->name);
7354  break;
7355  }
7356  if (bridge_exit)
7357  break;
7358  }
7359  if ((f->frametype == AST_FRAME_VOICE) ||
7360  (f->frametype == AST_FRAME_DTMF_BEGIN) ||
7361  (f->frametype == AST_FRAME_DTMF) ||
7362  (f->frametype == AST_FRAME_VIDEO) ||
7363  (f->frametype == AST_FRAME_IMAGE) ||
7364  (f->frametype == AST_FRAME_HTML) ||
7365  (f->frametype == AST_FRAME_MODEM) ||
7366  (f->frametype == AST_FRAME_TEXT)) {
7367  /* monitored dtmf causes exit from bridge */
7368  int monitored_source = (who == c0) ? watch_c0_dtmf : watch_c1_dtmf;
7369 
7370  if (monitored_source &&
7371  (f->frametype == AST_FRAME_DTMF_END ||
7373  *fo = f;
7374  *rc = who;
7375  ast_debug(1, "Got DTMF %s on channel (%s)\n",
7376  f->frametype == AST_FRAME_DTMF_END ? "end" : "begin",
7377  who->name);
7378 
7379  break;
7380  }
7381  /* Write immediately frames, not passed through jb */
7382  if (!frame_put_in_jb)
7383  ast_write(other, f);
7384 
7385  /* Check if we have to deliver now */
7386  if (jb_in_use)
7387  ast_jb_get_and_deliver(c0, c1);
7388  }
7389  /* XXX do we want to pass on also frames not matched above ? */
7390  ast_frfree(f);
7391 
7392 #ifndef HAVE_EPOLL
7393  /* Swap who gets priority */
7394  cs[2] = cs[0];
7395  cs[0] = cs[1];
7396  cs[1] = cs[2];
7397 #endif
7398  }
7399 
7400  ast_poll_channel_del(c0, c1);
7401 
7402  return res;
7403 }
7404 
7405 /*! \brief Bridge two channels together (early) */
7407 {
7408  /* Make sure we can early bridge, if not error out */
7409  if (!c0->tech->early_bridge || (c1 && (!c1->tech->early_bridge || c0->tech->early_bridge != c1->tech->early_bridge)))
7410  return -1;
7411 
7412  return c0->tech->early_bridge(c0, c1);
7413 }
7414 
7415 /*! \brief Send manager event for bridge link and unlink events.
7416  * \param onoff Link/Unlinked
7417  * \param type 1 for core, 2 for native
7418  * \param c0 first channel in bridge
7419  * \param c1 second channel in bridge
7420 */
7421 static void manager_bridge_event(int onoff, int type, struct ast_channel *c0, struct ast_channel *c1)
7422 {
7423  struct ast_channel *chans[2] = { c0, c1 };
7424  ast_manager_event_multichan(EVENT_FLAG_CALL, "Bridge", 2, chans,
7425  "Bridgestate: %s\r\n"
7426  "Bridgetype: %s\r\n"
7427  "Channel1: %s\r\n"
7428  "Channel2: %s\r\n"
7429  "Uniqueid1: %s\r\n"
7430  "Uniqueid2: %s\r\n"
7431  "CallerID1: %s\r\n"
7432  "CallerID2: %s\r\n",
7433  onoff ? "Link" : "Unlink",
7434  type == 1 ? "core" : "native",
7435  c0->name, c1->name,
7436  c0->uniqueid, c1->uniqueid,
7437  S_COR(c0->caller.id.number.valid, c0->caller.id.number.str, ""),
7438  S_COR(c1->caller.id.number.valid, c1->caller.id.number.str, ""));
7439 }
7440 
7441 static void update_bridge_vars(struct ast_channel *c0, struct ast_channel *c1)
7442 {
7443  const char *c0_name;
7444  const char *c1_name;
7445  const char *c0_pvtid = NULL;
7446  const char *c1_pvtid = NULL;
7447 
7448  ast_channel_lock(c1);
7449  c1_name = ast_strdupa(c1->name);
7450  if (c1->tech->get_pvt_uniqueid) {
7451  c1_pvtid = ast_strdupa(c1->tech->get_pvt_uniqueid(c1));
7452  }
7453  ast_channel_unlock(c1);
7454 
7455  ast_channel_lock(c0);
7456  if (!ast_strlen_zero(pbx_builtin_getvar_helper(c0, "BRIDGEPEER"))) {
7457  pbx_builtin_setvar_helper(c0, "BRIDGEPEER", c1_name);
7458  }
7459  if (c1_pvtid) {
7460  pbx_builtin_setvar_helper(c0, "BRIDGEPVTCALLID", c1_pvtid);
7461  }
7462  c0_name = ast_strdupa(c0->name);
7463  if (c0->tech->get_pvt_uniqueid) {
7464  c0_pvtid = ast_strdupa(c0->tech->get_pvt_uniqueid(c0));
7465  }
7466  ast_channel_unlock(c0);
7467 
7468  ast_channel_lock(c1);
7469  if (!ast_strlen_zero(pbx_builtin_getvar_helper(c1, "BRIDGEPEER"))) {
7470  pbx_builtin_setvar_helper(c1, "BRIDGEPEER", c0_name);
7471  }
7472  if (c0_pvtid) {
7473  pbx_builtin_setvar_helper(c1, "BRIDGEPVTCALLID", c0_pvtid);
7474  }
7475  ast_channel_unlock(c1);
7476 }
7477 
7478 static void bridge_play_sounds(struct ast_channel *c0, struct ast_channel *c1)
7479 {
7480  const char *s, *sound;
7481 
7482  /* See if we need to play an audio file to any side of the bridge */
7483 
7484  ast_channel_lock(c0);
7485  if ((s = pbx_builtin_getvar_helper(c0, "BRIDGE_PLAY_SOUND"))) {
7486  sound = ast_strdupa(s);
7487  ast_channel_unlock(c0);
7488  bridge_playfile(c0, c1, sound, 0);
7489  pbx_builtin_setvar_helper(c0, "BRIDGE_PLAY_SOUND", NULL);
7490  } else {
7491  ast_channel_unlock(c0);
7492  }
7493 
7494  ast_channel_lock(c1);
7495  if ((s = pbx_builtin_getvar_helper(c1, "BRIDGE_PLAY_SOUND"))) {
7496  sound = ast_strdupa(s);
7497  ast_channel_unlock(c1);
7498  bridge_playfile(c1, c0, sound, 0);
7499  pbx_builtin_setvar_helper(c1, "BRIDGE_PLAY_SOUND", NULL);
7500  } else {
7501  ast_channel_unlock(c1);
7502  }
7503 }
7504 
7505 /*! \brief Bridge two channels together */
7507  struct ast_bridge_config *config, struct ast_frame **fo, struct ast_channel **rc)
7508 {
7509  struct ast_channel *chans[2] = { c0, c1 };
7511  format_t o0nativeformats;
7512  format_t o1nativeformats;
7513  long time_left_ms=0;
7514  char caller_warning = 0;
7515  char callee_warning = 0;
7516 
7517  *fo = NULL;
7518 
7519  if (c0->_bridge) {
7520  ast_log(LOG_WARNING, "%s is already in a bridge with %s\n",
7521  c0->name, c0->_bridge->name);
7522  return -1;
7523  }
7524  if (c1->_bridge) {
7525  ast_log(LOG_WARNING, "%s is already in a bridge with %s\n",
7526  c1->name, c1->_bridge->name);
7527  return -1;
7528  }
7529 
7530  /* Stop if we're a zombie or need a soft hangup */
7533  return -1;
7534 
7535  caller_warning = ast_test_flag(&config->features_caller, AST_FEATURE_PLAY_WARNING);
7536  callee_warning = ast_test_flag(&config->features_callee, AST_FEATURE_PLAY_WARNING);
7537 
7538  if (ast_tvzero(config->start_time)) {
7539  config->start_time = ast_tvnow();
7540  if (config->start_sound) {
7541  if (caller_warning) {
7542  bridge_playfile(c0, c1, config->start_sound, config->timelimit / 1000);
7543  }
7544  if (callee_warning) {
7545  bridge_playfile(c1, c0, config->start_sound, config->timelimit / 1000);
7546  }
7547  }
7548  }
7549 
7550  /* Keep track of bridge */
7551  ast_channel_lock_both(c0, c1);
7552  c0->_bridge = c1;
7553  c1->_bridge = c0;
7554  ast_channel_unlock(c0);
7555  ast_channel_unlock(c1);
7556 
7557  ast_set_owners_and_peers(c0, c1);
7558 
7559  o0nativeformats = c0->nativeformats;
7560  o1nativeformats = c1->nativeformats;
7561 
7562  if (config->feature_timer && !ast_tvzero(config->nexteventts)) {
7563  config->nexteventts = ast_tvadd(config->feature_start_time, ast_samp2tv(config->feature_timer, 1000));
7564  } else if (config->timelimit) {
7565  time_left_ms = config->timelimit - ast_tvdiff_ms(ast_tvnow(), config->start_time);
7566  config->nexteventts = ast_tvadd(config->start_time, ast_samp2tv(config->timelimit, 1000));
7567  if ((caller_warning || callee_warning) && config->play_warning) {
7568  long next_warn = config->play_warning;
7569  if (time_left_ms < config->play_warning && config->warning_freq > 0) {
7570  /* At least one warning was played, which means we are returning after feature */
7571  long warns_passed = (config->play_warning - time_left_ms) / config->warning_freq;
7572  /* It is 'warns_passed * warning_freq' NOT '(warns_passed + 1) * warning_freq',
7573  because nexteventts will be updated once again in the 'if (!to)' block */
7574  next_warn = config->play_warning - warns_passed * config->warning_freq;
7575  }
7576  config->nexteventts = ast_tvsub(config->nexteventts, ast_samp2tv(next_warn, 1000));
7577  }
7578  } else {
7579  config->nexteventts.tv_sec = 0;
7580  config->nexteventts.tv_usec = 0;
7581  }
7582 
7583  if (!c0->tech->send_digit_begin)
7585  if (!c1->tech->send_digit_begin)
7587  manager_bridge_event(1, 1, c0, c1);
7588 
7589  /* Before we enter in and bridge these two together tell them both the source of audio has changed */
7592 
7593  for (/* ever */;;) {
7594  struct timeval now = { 0, };
7595  int to;
7596 
7597  to = -1;
7598 
7599  if (!ast_tvzero(config->nexteventts)) {
7600  now = ast_tvnow();
7601  to = ast_tvdiff_ms(config->nexteventts, now);
7602  if (to <= 0) {
7603  if (!config->timelimit) {
7604  res = AST_BRIDGE_COMPLETE;
7605  break;
7606  }
7607  to = 0;
7608  }
7609  }
7610 
7611  if (config->timelimit) {
7612  time_left_ms = config->timelimit - ast_tvdiff_ms(now, config->start_time);
7613  if (time_left_ms < 0) {
7614  time_left_ms = 0;
7615  }
7616 
7617  if (time_left_ms < to) {
7618  to = time_left_ms;
7619  }
7620 
7621  if (time_left_ms <= 0) {
7622  if (caller_warning && config->end_sound)
7623  bridge_playfile(c0, c1, config->end_sound, 0);
7624  if (callee_warning && config->end_sound)
7625  bridge_playfile(c1, c0, config->end_sound, 0);
7626  *fo = NULL;
7627  res = AST_BRIDGE_COMPLETE;
7628  ast_test_suite_event_notify("BRIDGE_TIMELIMIT", "Channel1: %s\r\nChannel2: %s", c0->name, c1->name);
7629  break;
7630  }
7631 
7632  if (!to) {
7633  if (time_left_ms >= 5000 && config->warning_sound && config->play_warning && ast_test_flag(config, AST_FEATURE_WARNING_ACTIVE)) {
7634  int t = (time_left_ms + 500) / 1000; /* round to nearest second */
7635  if (caller_warning)
7636  bridge_playfile(c0, c1, config->warning_sound, t);
7637  if (callee_warning)
7638  bridge_playfile(c1, c0, config->warning_sound, t);
7639  }
7640 
7641  if (config->warning_freq && (time_left_ms > (config->warning_freq + 5000))) {
7642  config->nexteventts = ast_tvadd(config->nexteventts, ast_samp2tv(config->warning_freq, 1000));
7643  } else {
7644  config->nexteventts = ast_tvadd(config->start_time, ast_samp2tv(config->timelimit, 1000));
7645  }
7646  }
7648  }
7649 
7650  if ((c0->_softhangup | c1->_softhangup) & AST_SOFTHANGUP_UNBRIDGE) {/* Bit operators are intentional. */
7653  }
7656  }
7657  ast_channel_lock_both(c0, c1);
7658  c0->_bridge = c1;
7659  c1->_bridge = c0;
7660  ast_channel_unlock(c0);
7661  ast_channel_unlock(c1);
7662  ast_debug(1, "Unbridge signal received. Ending native bridge.\n");
7663  continue;
7664  }
7665 
7666  /* Stop if we're a zombie or need a soft hangup */
7669  *fo = NULL;
7670  res = AST_BRIDGE_COMPLETE;
7671  ast_debug(1, "Bridge stops because we're zombie or need a soft hangup: c0=%s, c1=%s, flags: %s,%s,%s,%s\n",
7672  c0->name, c1->name,
7673  ast_test_flag(c0, AST_FLAG_ZOMBIE) ? "Yes" : "No",
7674  ast_check_hangup(c0) ? "Yes" : "No",
7675  ast_test_flag(c1, AST_FLAG_ZOMBIE) ? "Yes" : "No",
7676  ast_check_hangup(c1) ? "Yes" : "No");
7677  break;
7678  }
7679 
7680  update_bridge_vars(c0, c1);
7681 
7682  bridge_play_sounds(c0, c1);
7683 
7684  if (c0->tech->bridge &&
7685  /* if < 1 ms remains use generic bridging for accurate timing */
7686  (!config->timelimit || to > 1000 || to == -1) &&
7687  (c0->tech->bridge == c1->tech->bridge) &&
7688  !c0->monitor && !c1->monitor &&
7689  !c0->audiohooks && !c1->audiohooks &&
7691  !c0->masq && !c0->masqr && !c1->masq && !c1->masqr) {
7692  int timeoutms = to - 1000 > 0 ? to - 1000 : to;
7693  /* Looks like they share a bridge method and nothing else is in the way */
7696  if ((res = c0->tech->bridge(c0, c1, config->flags, fo, rc, timeoutms)) == AST_BRIDGE_COMPLETE) {
7697  ast_manager_event_multichan(EVENT_FLAG_CALL, "Unlink", 2, chans,
7698  "Channel1: %s\r\n"
7699  "Channel2: %s\r\n"
7700  "Uniqueid1: %s\r\n"
7701  "Uniqueid2: %s\r\n"
7702  "CallerID1: %s\r\n"
7703  "CallerID2: %s\r\n",
7704  c0->name, c1->name,
7705  c0->uniqueid, c1->uniqueid,
7706  S_COR(c0->caller.id.number.valid, c0->caller.id.number.str, "<unknown>"),
7707  S_COR(c1->caller.id.number.valid, c1->caller.id.number.str, "<unknown>"));
7708 
7709  ast_debug(1, "Returning from native bridge, channels: %s, %s\n", c0->name, c1->name);
7710 
7713 
7714  if ((c0->_softhangup | c1->_softhangup) & AST_SOFTHANGUP_UNBRIDGE) {/* Bit operators are intentional. */
7715  continue;
7716  }
7717 
7718  ast_channel_lock_both(c0, c1);
7719  c0->_bridge = NULL;
7720  c1->_bridge = NULL;
7721  ast_channel_unlock(c0);
7722  ast_channel_unlock(c1);
7723  return res;
7724  } else {
7727  }
7728  switch (res) {
7729  case AST_BRIDGE_RETRY:
7730  if (config->play_warning) {
7732  }
7733  continue;
7734  default:
7735  ast_verb(3, "Native bridging %s and %s ended\n", c0->name, c1->name);
7736  /* fallthrough */
7738  break;
7739  }
7740  }
7741 
7742  if (((c0->writeformat != c1->readformat) || (c0->readformat != c1->writeformat) ||
7743  (c0->nativeformats != o0nativeformats) || (c1->nativeformats != o1nativeformats)) &&
7744  !(c0->generator || c1->generator)) {
7745  if (ast_channel_make_compatible(c0, c1)) {
7746  ast_log(LOG_WARNING, "Can't make %s and %s compatible\n", c0->name, c1->name);
7747  manager_bridge_event(0, 1, c0, c1);
7748  return AST_BRIDGE_FAILED;
7749  }
7750  o0nativeformats = c0->nativeformats;
7751  o1nativeformats = c1->nativeformats;
7752  }
7753 
7754  update_bridge_vars(c0, c1);
7755 
7756  res = ast_generic_bridge(c0, c1, config, fo, rc);
7757  if (res != AST_BRIDGE_RETRY) {
7758  break;
7759  } else if (config->feature_timer) {
7760  /* feature timer expired but has not been updated, sending to ast_bridge_call to do so */
7761  break;
7762  }
7763  }
7764 
7767 
7768  /* Now that we have broken the bridge the source will change yet again */
7771 
7772  ast_channel_lock_both(c0, c1);
7773  c0->_bridge = NULL;
7774  c1->_bridge = NULL;
7775  ast_channel_unlock(c0);
7776  ast_channel_unlock(c1);
7777 
7778  ast_manager_event_multichan(EVENT_FLAG_CALL, "Unlink", 2, chans,
7779  "Channel1: %s\r\n"
7780  "Channel2: %s\r\n"
7781  "Uniqueid1: %s\r\n"
7782  "Uniqueid2: %s\r\n"
7783  "CallerID1: %s\r\n"
7784  "CallerID2: %s\r\n",
7785  c0->name, c1->name,
7786  c0->uniqueid, c1->uniqueid,
7787  S_COR(c0->caller.id.number.valid, c0->caller.id.number.str, "<unknown>"),
7788  S_COR(c1->caller.id.number.valid, c1->caller.id.number.str, "<unknown>"));
7789  ast_debug(1, "Bridge stops bridging channels %s and %s\n", c0->name, c1->name);
7790 
7791  return res;
7792 }
7793 
7794 /*! \brief Sets an option on a channel */
7795 int ast_channel_setoption(struct ast_channel *chan, int option, void *data, int datalen, int block)
7796 {
7797  int res;
7798 
7799  ast_channel_lock(chan);
7800  if (!chan->tech->setoption) {
7801  errno = ENOSYS;
7802  ast_channel_unlock(chan);
7803  return -1;
7804  }
7805 
7806  if (block)
7807  ast_log(LOG_ERROR, "XXX Blocking not implemented yet XXX\n");
7808 
7809  res = chan->tech->setoption(chan, option, data, datalen);
7810  ast_channel_unlock(chan);
7811 
7812  return res;
7813 }
7814 
7815 int ast_channel_queryoption(struct ast_channel *chan, int option, void *data, int *datalen, int block)
7816 {
7817  int res;
7818 
7819  ast_channel_lock(chan);
7820  if (!chan->tech->queryoption) {
7821  errno = ENOSYS;
7822  ast_channel_unlock(chan);
7823  return -1;
7824  }
7825 
7826  if (block)
7827  ast_log(LOG_ERROR, "XXX Blocking not implemented yet XXX\n");
7828 
7829  res = chan->tech->queryoption(chan, option, data, datalen);
7830  ast_channel_unlock(chan);
7831 
7832  return res;
7833 }
7834 
7836  int freq1;
7837  int freq2;
7839  int vol;
7840 };
7841 
7843  int fac1;
7844  int fac2;
7845  int v1_1;
7846  int v2_1;
7847  int v3_1;
7848  int v1_2;
7849  int v2_2;
7850  int v3_2;
7852  int pos;
7855  struct ast_frame f;
7856  unsigned char offset[AST_FRIENDLY_OFFSET];
7857  short data[4000];
7858 };
7859 
7860 static void tonepair_release(struct ast_channel *chan, void *params)
7861 {
7862  struct tonepair_state *ts = params;
7863 
7864  if (chan)
7865  ast_set_write_format(chan, ts->origwfmt);
7866  ast_free(ts);
7867 }
7868 
7869 static void *tonepair_alloc(struct ast_channel *chan, void *params)
7870 {
7871  struct tonepair_state *ts;
7872  struct tonepair_def *td = params;
7873 
7874  if (!(ts = ast_calloc(1, sizeof(*ts))))
7875  return NULL;
7876  ts->origwfmt = chan->writeformat;
7878  ast_log(LOG_WARNING, "Unable to set '%s' to signed linear format (write)\n", chan->name);
7879  tonepair_release(NULL, ts);
7880  ts = NULL;
7881  } else {
7882  ts->fac1 = 2.0 * cos(2.0 * M_PI * (td->freq1 / 8000.0)) * 32768.0;
7883  ts->v1_1 = 0;
7884  ts->v2_1 = sin(-4.0 * M_PI * (td->freq1 / 8000.0)) * td->vol;
7885  ts->v3_1 = sin(-2.0 * M_PI * (td->freq1 / 8000.0)) * td->vol;
7886  ts->v2_1 = 0;
7887  ts->fac2 = 2.0 * cos(2.0 * M_PI * (td->freq2 / 8000.0)) * 32768.0;
7888  ts->v2_2 = sin(-4.0 * M_PI * (td->freq2 / 8000.0)) * td->vol;
7889  ts->v3_2 = sin(-2.0 * M_PI * (td->freq2 / 8000.0)) * td->vol;
7890  ts->duration = td->duration;
7891  ts->modulate = 0;
7892  }
7893  /* Let interrupts interrupt :) */
7895  return ts;
7896 }
7897 
7898 static int tonepair_generator(struct ast_channel *chan, void *data, int len, int samples)
7899 {
7900  struct tonepair_state *ts = data;
7901  int x;
7902 
7903  /* we need to prepare a frame with 16 * timelen samples as we're
7904  * generating SLIN audio
7905  */
7906  len = samples * 2;
7907 
7908  if (len > sizeof(ts->data) / 2 - 1) {
7909  ast_log(LOG_WARNING, "Can't generate that much data!\n");
7910  return -1;
7911  }
7912  memset(&ts->f, 0, sizeof(ts->f));
7913  for (x=0;x<len/2;x++) {
7914  ts->v1_1 = ts->v2_1;
7915  ts->v2_1 = ts->v3_1;
7916  ts->v3_1 = (ts->fac1 * ts->v2_1 >> 15) - ts->v1_1;
7917 
7918  ts->v1_2 = ts->v2_2;
7919  ts->v2_2 = ts->v3_2;
7920  ts->v3_2 = (ts->fac2 * ts->v2_2 >> 15) - ts->v1_2;
7921  if (ts->modulate) {
7922  int p;
7923  p = ts->v3_2 - 32768;
7924  if (p < 0) p = -p;
7925  p = ((p * 9) / 10) + 1;
7926  ts->data[x] = (ts->v3_1 * p) >> 15;
7927  } else
7928  ts->data[x] = ts->v3_1 + ts->v3_2;
7929  }
7930  ts->f.frametype = AST_FRAME_VOICE;
7932  ts->f.datalen = len;
7933  ts->f.samples = samples;
7935  ts->f.data.ptr = ts->data;
7936  ast_write(chan, &ts->f);
7937  ts->pos += x;
7938  if (ts->duration > 0) {
7939  if (ts->pos >= ts->duration * 8)
7940  return -1;
7941  }
7942  return 0;
7943 }
7944 
7945 static struct ast_generator tonepair = {
7946  .alloc = tonepair_alloc,
7947  .release = tonepair_release,
7948  .generate = tonepair_generator,
7949 };
7950 
7951 int ast_tonepair_start(struct ast_channel *chan, int freq1, int freq2, int duration, int vol)
7952 {
7953  struct tonepair_def d = { 0, };
7954 
7955  d.freq1 = freq1;
7956  d.freq2 = freq2;
7957  d.duration = duration;
7958  d.vol = (vol < 1) ? 8192 : vol; /* force invalid to 8192 */
7959  if (ast_activate_generator(chan, &tonepair, &d))
7960  return -1;
7961  return 0;
7962 }
7963 
7964 void ast_tonepair_stop(struct ast_channel *chan)
7965 {
7967 }
7968 
7969 int ast_tonepair(struct ast_channel *chan, int freq1, int freq2, int duration, int vol)
7970 {
7971  int res;
7972 
7973  if ((res = ast_tonepair_start(chan, freq1, freq2, duration, vol)))
7974  return res;
7975 
7976  /* Give us some wiggle room */
7977  while (chan->generatordata && ast_waitfor(chan, 100) >= 0) {
7978  struct ast_frame *f = ast_read(chan);
7979  if (f)
7980  ast_frfree(f);
7981  else
7982  return -1;
7983  }
7984  return 0;
7985 }
7986 
7988 {
7989  char *piece;
7990  char *c;
7991  int start=0, finish=0, x;
7992  ast_group_t group = 0;
7993 
7994  if (ast_strlen_zero(s))
7995  return 0;
7996 
7997  c = ast_strdupa(s);
7998 
7999  while ((piece = strsep(&c, ","))) {
8000  if (sscanf(piece, "%30d-%30d", &start, &finish) == 2) {
8001  /* Range */
8002  } else if (sscanf(piece, "%30d", &start)) {
8003  /* Just one */
8004  finish = start;
8005  } else {
8006  ast_log(LOG_ERROR, "Syntax error parsing group configuration '%s' at '%s'. Ignoring.\n", s, piece);
8007  continue;
8008  }
8009  for (x = start; x <= finish; x++) {
8010  if ((x > 63) || (x < 0)) {
8011  ast_log(LOG_WARNING, "Ignoring invalid group %d (maximum group is 63)\n", x);
8012  } else
8013  group |= ((ast_group_t) 1 << x);
8014  }
8015  }
8016  return group;
8017 }
8018 
8019 static int (*ast_moh_start_ptr)(struct ast_channel *, const char *, const char *) = NULL;
8020 static void (*ast_moh_stop_ptr)(struct ast_channel *) = NULL;
8021 static void (*ast_moh_cleanup_ptr)(struct ast_channel *) = NULL;
8022 
8023 void ast_install_music_functions(int (*start_ptr)(struct ast_channel *, const char *, const char *),
8024  void (*stop_ptr)(struct ast_channel *),
8025  void (*cleanup_ptr)(struct ast_channel *))
8026 {
8027  ast_moh_start_ptr = start_ptr;
8028  ast_moh_stop_ptr = stop_ptr;
8029  ast_moh_cleanup_ptr = cleanup_ptr;
8030 }
8031 
8033 {
8034  ast_moh_start_ptr = NULL;
8035  ast_moh_stop_ptr = NULL;
8036  ast_moh_cleanup_ptr = NULL;
8037 }
8038 
8039 /*! \brief Turn on music on hold on a given channel */
8040 int ast_moh_start(struct ast_channel *chan, const char *mclass, const char *interpclass)
8041 {
8042  if (ast_moh_start_ptr)
8043  return ast_moh_start_ptr(chan, mclass, interpclass);
8044 
8045  ast_verb(3, "Music class %s requested but no musiconhold loaded.\n", mclass ? mclass : (interpclass ? interpclass : "default"));
8046 
8047  return 0;
8048 }
8049 
8050 /*! \brief Turn off music on hold on a given channel */
8051 void ast_moh_stop(struct ast_channel *chan)
8052 {
8053  if (ast_moh_stop_ptr)
8054  ast_moh_stop_ptr(chan);
8055 }
8056 
8057 void ast_moh_cleanup(struct ast_channel *chan)
8058 {
8059  if (ast_moh_cleanup_ptr)
8060  ast_moh_cleanup_ptr(chan);
8061 }
8062 
8063 static int ast_channel_hash_cb(const void *obj, const int flags)
8064 {
8065  const struct ast_channel *chan = obj;
8066 
8067  /* If the name isn't set, return 0 so that the ao2_find() search will
8068  * start in the first bucket. */
8069  if (ast_strlen_zero(chan->name)) {
8070  return 0;
8071  }
8072 
8073  return ast_str_case_hash(chan->name);
8074 }
8075 
8077 {
8078  struct ast_variable *var;
8079  struct ast_flags config_flags = { 0 };
8080  struct ast_config *cfg = ast_config_load("codecs.conf", config_flags);
8082  return 0;
8083  for (var = ast_variable_browse(cfg, "plc"); var; var = var->next) {
8084  if (!strcasecmp(var->name, "genericplc")) {
8086  }
8087  }
8088  ast_config_destroy(cfg);
8089  return 0;
8090 }
8091 
8092 /*!
8093  * \internal
8094  * \brief Implements the channels provider.
8095  */
8096 static int data_channels_provider_handler(const struct ast_data_search *search,
8097  struct ast_data *root)
8098 {
8099  struct ast_channel *c;
8100  struct ast_channel_iterator *iter = NULL;
8101  struct ast_data *data_channel;
8102 
8103  for (iter = ast_channel_iterator_all_new();
8104  iter && (c = ast_channel_iterator_next(iter)); ast_channel_unref(c)) {
8105  ast_channel_lock(c);
8106 
8107  data_channel = ast_data_add_node(root, "channel");
8108  if (!data_channel) {
8109  ast_channel_unlock(c);
8110  continue;
8111  }
8112 
8113  if (ast_channel_data_add_structure(data_channel, c, 1) < 0) {
8114  ast_log(LOG_ERROR, "Unable to add channel structure for channel: %s\n", c->name);
8115  }
8116 
8117  ast_channel_unlock(c);
8118 
8119  if (!ast_data_search_match(search, data_channel)) {
8120  ast_data_remove_node(root, data_channel);
8121  }
8122  }
8123  if (iter) {
8125  }
8126 
8127  return 0;
8128 }
8129 
8130 /*!
8131  * \internal
8132  * \brief Implements the channeltypes provider.
8133  */
8134 static int data_channeltypes_provider_handler(const struct ast_data_search *search,
8135  struct ast_data *data_root)
8136 {
8137  struct chanlist *cl;
8138  struct ast_data *data_type;
8139 
8141  AST_RWLIST_TRAVERSE(&backends, cl, list) {
8142  data_type = ast_data_add_node(data_root, "type");
8143  if (!data_type) {
8144  continue;
8145  }
8146  ast_data_add_str(data_type, "name", cl->tech->type);
8147  ast_data_add_str(data_type, "description", cl->tech->description);
8148  ast_data_add_bool(data_type, "devicestate", cl->tech->devicestate ? 1 : 0);
8149  ast_data_add_bool(data_type, "indications", cl->tech->indicate ? 1 : 0);
8150  ast_data_add_bool(data_type, "transfer", cl->tech->transfer ? 1 : 0);
8151  ast_data_add_bool(data_type, "send_digit_begin", cl->tech->send_digit_begin ? 1 : 0);
8152  ast_data_add_bool(data_type, "send_digit_end", cl->tech->send_digit_end ? 1 : 0);
8153  ast_data_add_bool(data_type, "call", cl->tech->call ? 1 : 0);
8154  ast_data_add_bool(data_type, "hangup", cl->tech->hangup ? 1 : 0);
8155  ast_data_add_bool(data_type, "answer", cl->tech->answer ? 1 : 0);
8156  ast_data_add_bool(data_type, "read", cl->tech->read ? 1 : 0);
8157  ast_data_add_bool(data_type, "write", cl->tech->write ? 1 : 0);
8158  ast_data_add_bool(data_type, "send_text", cl->tech->send_text ? 1 : 0);
8159  ast_data_add_bool(data_type, "send_image", cl->tech->send_image ? 1 : 0);
8160  ast_data_add_bool(data_type, "send_html", cl->tech->send_html ? 1 : 0);
8161  ast_data_add_bool(data_type, "exception", cl->tech->exception ? 1 : 0);
8162  ast_data_add_bool(data_type, "bridge", cl->tech->bridge ? 1 : 0);
8163  ast_data_add_bool(data_type, "early_bridge", cl->tech->early_bridge ? 1 : 0);
8164  ast_data_add_bool(data_type, "fixup", cl->tech->fixup ? 1 : 0);
8165  ast_data_add_bool(data_type, "setoption", cl->tech->setoption ? 1 : 0);
8166  ast_data_add_bool(data_type, "queryoption", cl->tech->queryoption ? 1 : 0);
8167  ast_data_add_bool(data_type, "write_video", cl->tech->write_video ? 1 : 0);
8168  ast_data_add_bool(data_type, "write_text", cl->tech->write_text ? 1 : 0);
8169  ast_data_add_bool(data_type, "bridged_channel", cl->tech->bridged_channel ? 1 : 0);
8170  ast_data_add_bool(data_type, "func_channel_read", cl->tech->func_channel_read ? 1 : 0);
8171  ast_data_add_bool(data_type, "func_channel_write", cl->tech->func_channel_write ? 1 : 0);
8172  ast_data_add_bool(data_type, "get_base_channel", cl->tech->get_base_channel ? 1 : 0);
8173  ast_data_add_bool(data_type, "set_base_channel", cl->tech->set_base_channel ? 1 : 0);
8174  ast_data_add_bool(data_type, "get_pvt_uniqueid", cl->tech->get_pvt_uniqueid ? 1 : 0);
8175  ast_data_add_bool(data_type, "cc_callback", cl->tech->cc_callback ? 1 : 0);
8176 
8177  ast_data_add_codecs(data_type, "capabilities", cl->tech->capabilities);
8178 
8179  if (!ast_data_search_match(search, data_type)) {
8180  ast_data_remove_node(data_root, data_type);
8181  }
8182  }
8184 
8185  return 0;
8186 }
8187 
8188 /*!
8189  * \internal
8190  * \brief /asterisk/core/channels provider.
8191  */
8192 static const struct ast_data_handler channels_provider = {
8195 };
8196 
8197 /*!
8198  * \internal
8199  * \brief /asterisk/core/channeltypes provider.
8200  */
8204 };
8205 
8206 static const struct ast_data_entry channel_providers[] = {
8207  AST_DATA_ENTRY("/asterisk/core/channels", &channels_provider),
8208  AST_DATA_ENTRY("/asterisk/core/channeltypes", &channeltypes_provider),
8209 };
8210 
8211 static void channels_shutdown(void)
8212 {
8213  ast_data_unregister(NULL);
8215  if (channels) {
8216  ao2_ref(channels, -1);
8217  channels = NULL;
8218  }
8219 }
8220 
8222 {
8225 
8227 
8228  ast_data_register_multiple_core(channel_providers, ARRAY_LEN(channel_providers));
8229 
8230  ast_plc_reload();
8231 
8233 }
8234 
8235 /*! \brief Print call group and pickup group ---*/
8236 char *ast_print_group(char *buf, int buflen, ast_group_t group)
8237 {
8238  unsigned int i;
8239  int first = 1;
8240  char num[3];
8241 
8242  buf[0] = '\0';
8243 
8244  if (!group) /* Return empty string if no group */
8245  return buf;
8246 
8247  for (i = 0; i <= 63; i++) { /* Max group is 63 */
8248  if (group & ((ast_group_t) 1 << i)) {
8249  if (!first) {
8250  strncat(buf, ", ", buflen - strlen(buf) - 1);
8251  } else {
8252  first = 0;
8253  }
8254  snprintf(num, sizeof(num), "%u", i);
8255  strncat(buf, num, buflen - strlen(buf) - 1);
8256  }
8257  }
8258  return buf;
8259 }
8260 
8261 void ast_set_variables(struct ast_channel *chan, struct ast_variable *vars)
8262 {
8263  struct ast_variable *cur;
8264 
8265  for (cur = vars; cur; cur = cur->next)
8266  pbx_builtin_setvar_helper(chan, cur->name, cur->value);
8267 }
8268 
8269 static void *silence_generator_alloc(struct ast_channel *chan, void *data)
8270 {
8271  /* just store the data pointer in the channel structure */
8272  return data;
8273 }
8274 
8275 static void silence_generator_release(struct ast_channel *chan, void *data)
8276 {
8277  /* nothing to do */
8278 }
8279 
8280 static int silence_generator_generate(struct ast_channel *chan, void *data, int len, int samples)
8281 {
8282  short buf[samples];
8283  struct ast_frame frame = {
8285  .subclass.codec = AST_FORMAT_SLINEAR,
8286  .data.ptr = buf,
8287  .samples = samples,
8288  .datalen = sizeof(buf),
8289  };
8290 
8291  memset(buf, 0, sizeof(buf));
8292 
8293  if (ast_write(chan, &frame))
8294  return -1;
8295 
8296  return 0;
8297 }
8298 
8301  .release = silence_generator_release,
8302  .generate = silence_generator_generate,
8303 };
8304 
8307 };
8308 
8310 {
8311  struct ast_silence_generator *state;
8312 
8313  if (!(state = ast_calloc(1, sizeof(*state)))) {
8314  return NULL;
8315  }
8316 
8317  state->old_write_format = chan->writeformat;
8318 
8319  if (ast_set_write_format(chan, AST_FORMAT_SLINEAR) < 0) {
8320  ast_log(LOG_ERROR, "Could not set write format to SLINEAR\n");
8321  ast_free(state);
8322  return NULL;
8323  }
8324 
8325  ast_activate_generator(chan, &silence_generator, state);
8326 
8327  ast_debug(1, "Started silence generator on '%s'\n", chan->name);
8328 
8329  return state;
8330 }
8331 
8333 {
8334  ast_channel_lock(chan);
8335 
8336  if (!chan->generatordata) {
8337  ast_debug(1, "Trying to stop silence generator when there is no generator on '%s'\n",
8338  chan->name);
8339  ast_channel_unlock(chan);
8340  return 0;
8341  }
8342  if (chan->generator != &silence_generator) {
8343  ast_debug(1, "Trying to stop silence generator when it is not the current generator on '%s'\n",
8344  chan->name);
8345  ast_channel_unlock(chan);
8346  return 0;
8347  }
8349 
8350  ast_channel_unlock(chan);
8351 
8352  return 1;
8353 }
8354 
8356 {
8357  if (!state) {
8358  return;
8359  }
8360 
8361  if (deactivate_silence_generator(chan)) {
8362  ast_debug(1, "Stopped silence generator on '%s'\n", chan->name);
8363 
8364  if (ast_set_write_format(chan, state->old_write_format) < 0)
8365  ast_log(LOG_ERROR, "Could not return write format to its original state\n");
8366  }
8367  ast_free(state);
8368 }
8369 
8370 
8371 /*! \ brief Convert channel reloadreason (ENUM) to text string for manager event */
8373 {
8374  switch (reason) {
8375  case CHANNEL_MODULE_LOAD:
8376  return "LOAD (Channel module load)";
8377 
8378  case CHANNEL_MODULE_RELOAD:
8379  return "RELOAD (Channel module reload)";
8380 
8381  case CHANNEL_CLI_RELOAD:
8382  return "CLIRELOAD (Channel module reload by CLI command)";
8383 
8384  default:
8385  return "MANAGERRELOAD (Channel module reload by manager)";
8386  }
8387 };
8388 
8389 /*
8390  * Wrappers for various ast_say_*() functions that call the full version
8391  * of the same functions.
8392  * The proper place would be say.c, but that file is optional and one
8393  * must be able to build asterisk even without it (using a loadable 'say'
8394  * implementation that only supplies the 'full' version of the functions.
8395  */
8396 
8397 int ast_say_number(struct ast_channel *chan, int num,
8398  const char *ints, const char *language, const char *options)
8399 {
8400  return ast_say_number_full(chan, num, ints, language, options, -1, -1);
8401 }
8402 
8403 int ast_say_enumeration(struct ast_channel *chan, int num,
8404  const char *ints, const char *language, const char *options)
8405 {
8406  return ast_say_enumeration_full(chan, num, ints, language, options, -1, -1);
8407 }
8408 
8409 int ast_say_digits(struct ast_channel *chan, int num,
8410  const char *ints, const char *lang)
8411 {
8412  return ast_say_digits_full(chan, num, ints, lang, -1, -1);
8413 }
8414 
8415 int ast_say_digit_str(struct ast_channel *chan, const char *str,
8416  const char *ints, const char *lang)
8417 {
8418  return ast_say_digit_str_full(chan, str, ints, lang, -1, -1);
8419 }
8420 
8421 int ast_say_character_str(struct ast_channel *chan, const char *str,
8422  const char *ints, const char *lang)
8423 {
8424  return ast_say_character_str_full(chan, str, ints, lang, -1, -1);
8425 }
8426 
8427 int ast_say_phonetic_str(struct ast_channel *chan, const char *str,
8428  const char *ints, const char *lang)
8429 {
8430  return ast_say_phonetic_str_full(chan, str, ints, lang, -1, -1);
8431 }
8432 
8433 int ast_say_digits_full(struct ast_channel *chan, int num,
8434  const char *ints, const char *lang, int audiofd, int ctrlfd)
8435 {
8436  char buf[256];
8437 
8438  snprintf(buf, sizeof(buf), "%d", num);
8439 
8440  return ast_say_digit_str_full(chan, buf, ints, lang, audiofd, ctrlfd);
8441 }
8442 
8444 {
8445  ast_party_id_copy(&dest->id, &src->id);
8446  ast_party_id_copy(&dest->ani, &src->ani);
8447  dest->ani2 = src->ani2;
8448 }
8449 
8451 {
8452  ast_party_id_copy(&dest->id, &src->id);
8453  ast_party_id_copy(&dest->ani, &src->ani);
8454 
8455  dest->ani2 = src->ani2;
8456 }
8457 
8459 {
8460  if (&chan->connected == connected) {
8461  /* Don't set to self */
8462  return;
8463  }
8464 
8465  ast_channel_lock(chan);
8466  ast_party_connected_line_set(&chan->connected, connected, update);
8467  ast_channel_unlock(chan);
8468 }
8469 
8470 /*! \note Should follow struct ast_party_name */
8472  /*! \brief Subscriber name ie */
8473  int str;
8474  /*! \brief Character set ie. */
8476  /*! \brief presentation-indicator ie */
8478  /*! \brief valid/present ie */
8479  int valid;
8480 };
8481 
8482 /*!
8483  * \internal
8484  * \since 1.8
8485  * \brief Build a party name information data frame component.
8486  *
8487  * \param data Buffer to fill with the frame data
8488  * \param datalen Size of the buffer to fill
8489  * \param name Party name information
8490  * \param label Name of particular party name
8491  * \param ies Data frame ie values for the party name components
8492  *
8493  * \retval -1 if error
8494  * \retval Amount of data buffer used
8495  */
8496 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)
8497 {
8498  size_t length;
8499  size_t pos = 0;
8500 
8501  /*
8502  * The size of integer values must be fixed in case the frame is
8503  * shipped to another machine.
8504  */
8505  if (name->str) {
8506  length = strlen(name->str);
8507  if (datalen < pos + (sizeof(data[0]) * 2) + length) {
8508  ast_log(LOG_WARNING, "No space left for %s name\n", label);
8509  return -1;
8510  }
8511  data[pos++] = ies->str;
8512  data[pos++] = length;
8513  memcpy(data + pos, name->str, length);
8514  pos += length;
8515  }
8516 
8517  if (datalen < pos + (sizeof(data[0]) * 2) + 1) {
8518  ast_log(LOG_WARNING, "No space left for %s name char set\n", label);
8519  return -1;
8520  }
8521  data[pos++] = ies->char_set;
8522  data[pos++] = 1;
8523  data[pos++] = name->char_set;
8524 
8525  if (datalen < pos + (sizeof(data[0]) * 2) + 1) {
8526  ast_log(LOG_WARNING, "No space left for %s name presentation\n", label);
8527  return -1;
8528  }
8529  data[pos++] = ies->presentation;
8530  data[pos++] = 1;
8531  data[pos++] = name->presentation;
8532 
8533  if (datalen < pos + (sizeof(data[0]) * 2) + 1) {
8534  ast_log(LOG_WARNING, "No space left for %s name valid\n", label);
8535  return -1;
8536  }
8537  data[pos++] = ies->valid;
8538  data[pos++] = 1;
8539  data[pos++] = name->valid;
8540 
8541  return pos;
8542 }
8543 
8544 /*! \note Should follow struct ast_party_number */
8546  /*! \brief Subscriber phone number ie */
8547  int str;
8548  /*! \brief Type-Of-Number and Numbering-Plan ie */
8549  int plan;
8550  /*! \brief presentation-indicator ie */
8552  /*! \brief valid/present ie */
8553  int valid;
8554 };
8555 
8556 /*!
8557  * \internal
8558  * \since 1.8
8559  * \brief Build a party number information data frame component.
8560  *
8561  * \param data Buffer to fill with the frame data
8562  * \param datalen Size of the buffer to fill
8563  * \param number Party number information
8564  * \param label Name of particular party number
8565  * \param ies Data frame ie values for the party number components
8566  *
8567  * \retval -1 if error
8568  * \retval Amount of data buffer used
8569  */
8570 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)
8571 {
8572  size_t length;
8573  size_t pos = 0;
8574 
8575  /*
8576  * The size of integer values must be fixed in case the frame is
8577  * shipped to another machine.
8578  */
8579  if (number->str) {
8580  length = strlen(number->str);
8581  if (datalen < pos + (sizeof(data[0]) * 2) + length) {
8582  ast_log(LOG_WARNING, "No space left for %s number\n", label);
8583  return -1;
8584  }
8585  data[pos++] = ies->str;
8586  data[pos++] = length;
8587  memcpy(data + pos, number->str, length);
8588  pos += length;
8589  }
8590 
8591  if (datalen < pos + (sizeof(data[0]) * 2) + 1) {
8592  ast_log(LOG_WARNING, "No space left for %s numbering plan\n", label);
8593  return -1;
8594  }
8595  data[pos++] = ies->plan;
8596  data[pos++] = 1;
8597  data[pos++] = number->plan;
8598 
8599  if (datalen < pos + (sizeof(data[0]) * 2) + 1) {
8600  ast_log(LOG_WARNING, "No space left for %s number presentation\n", label);
8601  return -1;
8602  }
8603  data[pos++] = ies->presentation;
8604  data[pos++] = 1;
8605  data[pos++] = number->presentation;
8606 
8607  if (datalen < pos + (sizeof(data[0]) * 2) + 1) {
8608  ast_log(LOG_WARNING, "No space left for %s number valid\n", label);
8609  return -1;
8610  }
8611  data[pos++] = ies->valid;
8612  data[pos++] = 1;
8613  data[pos++] = number->valid;
8614 
8615  return pos;
8616 }
8617 
8618 /*! \note Should follow struct ast_party_subaddress */
8620  /*! \brief subaddress ie. */
8621  int str;
8622  /*! \brief subaddress type ie */
8623  int type;
8624  /*! \brief odd/even indicator ie */
8626  /*! \brief valid/present ie */
8627  int valid;
8628 };
8629 
8630 /*!
8631  * \internal
8632  * \since 1.8
8633  * \brief Build a party subaddress information data frame component.
8634  *
8635  * \param data Buffer to fill with the frame data
8636  * \param datalen Size of the buffer to fill
8637  * \param subaddress Party subaddress information
8638  * \param label Name of particular party subaddress
8639  * \param ies Data frame ie values for the party subaddress components
8640  *
8641  * \retval -1 if error
8642  * \retval Amount of data buffer used
8643  */
8644 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)
8645 {
8646  size_t length;
8647  size_t pos = 0;
8648 
8649  /*
8650  * The size of integer values must be fixed in case the frame is
8651  * shipped to another machine.
8652  */
8653  if (subaddress->str) {
8654  length = strlen(subaddress->str);
8655  if (datalen < pos + (sizeof(data[0]) * 2) + length) {
8656  ast_log(LOG_WARNING, "No space left for %s subaddress\n", label);
8657  return -1;
8658  }
8659  data[pos++] = ies->str;
8660  data[pos++] = length;
8661  memcpy(data + pos, subaddress->str, length);
8662  pos += length;
8663  }
8664 
8665  if (datalen < pos + (sizeof(data[0]) * 2) + 1) {
8666  ast_log(LOG_WARNING, "No space left for %s type of subaddress\n", label);
8667  return -1;
8668  }
8669  data[pos++] = ies->type;
8670  data[pos++] = 1;
8671  data[pos++] = subaddress->type;
8672 
8673  if (datalen < pos + (sizeof(data[0]) * 2) + 1) {
8675  "No space left for %s subaddress odd-even indicator\n", label);
8676  return -1;
8677  }
8678  data[pos++] = ies->odd_even_indicator;
8679  data[pos++] = 1;
8680  data[pos++] = subaddress->odd_even_indicator;
8681 
8682  if (datalen < pos + (sizeof(data[0]) * 2) + 1) {
8683  ast_log(LOG_WARNING, "No space left for %s subaddress valid\n", label);
8684  return -1;
8685  }
8686  data[pos++] = ies->valid;
8687  data[pos++] = 1;
8688  data[pos++] = subaddress->valid;
8689 
8690  return pos;
8691 }
8692 
8693 /*! \note Should follow struct ast_party_id */
8695  /*! \brief Subscriber name ies */
8697  /*! \brief Subscriber phone number ies */
8699  /*! \brief Subscriber subaddress ies. */
8700  struct ast_party_subaddress_ies subaddress;
8701  /*! \brief User party id tag ie. */
8702  int tag;
8703  /*! \brief Combined name and number presentation ie. */
8705 };
8706 
8707 /*!
8708  * \internal
8709  * \since 1.8
8710  * \brief Build a party id information data frame component.
8711  *
8712  * \param data Buffer to fill with the frame data
8713  * \param datalen Size of the buffer to fill
8714  * \param id Party id information
8715  * \param label Name of particular party id
8716  * \param ies Data frame ie values for the party id components
8717  * \param update What id information to build. NULL if all.
8718  *
8719  * \retval -1 if error
8720  * \retval Amount of data buffer used
8721  */
8722 static int party_id_build_data(unsigned char *data, size_t datalen,
8723  const struct ast_party_id *id, const char *label, const struct ast_party_id_ies *ies,
8724  const struct ast_set_party_id *update)
8725 {
8726  size_t length;
8727  size_t pos = 0;
8728  int res;
8729 
8730  /*
8731  * The size of integer values must be fixed in case the frame is
8732  * shipped to another machine.
8733  */
8734 
8735  if (!update || update->name) {
8736  res = party_name_build_data(data + pos, datalen - pos, &id->name, label,
8737  &ies->name);
8738  if (res < 0) {
8739  return -1;
8740  }
8741  pos += res;
8742  }
8743 
8744  if (!update || update->number) {
8745  res = party_number_build_data(data + pos, datalen - pos, &id->number, label,
8746  &ies->number);
8747  if (res < 0) {
8748  return -1;
8749  }
8750  pos += res;
8751  }
8752 
8753  if (!update || update->subaddress) {
8754  res = party_subaddress_build_data(data + pos, datalen - pos, &id->subaddress,
8755  label, &ies->subaddress);
8756  if (res < 0) {
8757  return -1;
8758  }
8759  pos += res;
8760  }
8761 
8762  /* *************** Party id user tag **************************** */
8763  if (id->tag) {
8764  length = strlen(id->tag);
8765  if (datalen < pos + (sizeof(data[0]) * 2) + length) {
8766  ast_log(LOG_WARNING, "No space left for %s tag\n", label);
8767  return -1;
8768  }
8769  data[pos++] = ies->tag;
8770  data[pos++] = length;
8771  memcpy(data + pos, id->tag, length);
8772  pos += length;
8773  }
8774 
8775  /* *************** Party id combined presentation *************** */
8776  if (!update || update->number) {
8777  int presentation;
8778 
8779  if (!update || update->name) {
8780  presentation = ast_party_id_presentation(id);
8781  } else {
8782  /*
8783  * We must compromise because not all the information is available
8784  * to determine a combined presentation value.
8785  * We will only send the number presentation instead.
8786  */
8787  presentation = id->number.presentation;
8788  }
8789 
8790  if (datalen < pos + (sizeof(data[0]) * 2) + 1) {
8791  ast_log(LOG_WARNING, "No space left for %s combined presentation\n", label);
8792  return -1;
8793  }
8794  data[pos++] = ies->combined_presentation;
8795  data[pos++] = 1;
8796  data[pos++] = presentation;
8797  }
8798 
8799  return pos;
8800 }
8801 
8802 /*!
8803  * \brief Element identifiers for connected line indication frame data
8804  * \note Only add to the end of this enum.
8805  */
8806 enum {
8810  AST_CONNECTED_LINE_ID_PRESENTATION,/* Combined number and name presentation. */
8823 };
8824 
8825 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)
8826 {
8827  int32_t value;
8828  size_t pos = 0;
8829  int res;
8830 
8831  static const struct ast_party_id_ies ies = {
8833  .name.char_set = AST_CONNECTED_LINE_NAME_CHAR_SET,
8834  .name.presentation = AST_CONNECTED_LINE_NAME_PRESENTATION,
8835  .name.valid = AST_CONNECTED_LINE_NAME_VALID,
8836 
8837  .number.str = AST_CONNECTED_LINE_NUMBER,
8838  .number.plan = AST_CONNECTED_LINE_NUMBER_PLAN,
8839  .number.presentation = AST_CONNECTED_LINE_NUMBER_PRESENTATION,
8840  .number.valid = AST_CONNECTED_LINE_NUMBER_VALID,
8841 
8842  .subaddress.str = AST_CONNECTED_LINE_SUBADDRESS,
8843  .subaddress.type = AST_CONNECTED_LINE_SUBADDRESS_TYPE,
8844  .subaddress.odd_even_indicator = AST_CONNECTED_LINE_SUBADDRESS_ODD_EVEN,
8845  .subaddress.valid = AST_CONNECTED_LINE_SUBADDRESS_VALID,
8846 
8847  .tag = AST_CONNECTED_LINE_TAG,
8848  .combined_presentation = AST_CONNECTED_LINE_ID_PRESENTATION,
8849  };
8850 
8851  /*
8852  * The size of integer values must be fixed in case the frame is
8853  * shipped to another machine.
8854  */
8855 
8856  /* Connected line frame version */
8857  if (datalen < pos + (sizeof(data[0]) * 2) + 1) {
8858  ast_log(LOG_WARNING, "No space left for connected line frame version\n");
8859  return -1;
8860  }
8861  data[pos++] = AST_CONNECTED_LINE_VERSION;
8862  data[pos++] = 1;
8863  data[pos++] = 2;/* Version 1 did not have a version ie */
8864 
8865  res = party_id_build_data(data + pos, datalen - pos, &connected->id,
8866  "connected line", &ies, update ? &update->id : NULL);
8867  if (res < 0) {
8868  return -1;
8869  }
8870  pos += res;
8871 
8872  /* Connected line source */
8873  if (datalen < pos + (sizeof(data[0]) * 2) + sizeof(value)) {
8874  ast_log(LOG_WARNING, "No space left for connected line source\n");
8875  return -1;
8876  }
8877  data[pos++] = AST_CONNECTED_LINE_SOURCE;
8878  data[pos++] = sizeof(value);
8879  value = htonl(connected->source);
8880  memcpy(data + pos, &value, sizeof(value));
8881  pos += sizeof(value);
8882 
8883  return pos;
8884 }
8885 
8886 int ast_connected_line_parse_data(const unsigned char *data, size_t datalen, struct ast_party_connected_line *connected)
8887 {
8888  size_t pos;
8889  unsigned char ie_len;
8890  unsigned char ie_id;
8891  int32_t value;
8892  int frame_version = 1;
8893  int combined_presentation = 0;
8894  int got_combined_presentation = 0;/* TRUE if got a combined name and number presentation value. */
8895 
8896  for (pos = 0; pos < datalen; pos += ie_len) {
8897  if (datalen < pos + sizeof(ie_id) + sizeof(ie_len)) {
8898  ast_log(LOG_WARNING, "Invalid connected line update\n");
8899  return -1;
8900  }
8901  ie_id = data[pos++];
8902  ie_len = data[pos++];
8903  if (datalen < pos + ie_len) {
8904  ast_log(LOG_WARNING, "Invalid connected line update\n");
8905  return -1;
8906  }
8907 
8908  switch (ie_id) {
8909 /* Connected line party frame version */
8911  if (ie_len != 1) {
8912  ast_log(LOG_WARNING, "Invalid connected line frame version (%u)\n",
8913  (unsigned) ie_len);
8914  break;
8915  }
8916  frame_version = data[pos];
8917  break;
8918 /* Connected line party id name */
8920  ast_free(connected->id.name.str);
8921  connected->id.name.str = ast_malloc(ie_len + 1);
8922  if (connected->id.name.str) {
8923  memcpy(connected->id.name.str, data + pos, ie_len);
8924  connected->id.name.str[ie_len] = 0;
8925  }
8926  break;
8928  if (ie_len != 1) {
8929  ast_log(LOG_WARNING, "Invalid connected line name char set (%u)\n",
8930  (unsigned) ie_len);
8931  break;
8932  }
8933  connected->id.name.char_set = data[pos];
8934  break;
8936  if (ie_len != 1) {
8937  ast_log(LOG_WARNING, "Invalid connected line name presentation (%u)\n",
8938  (unsigned) ie_len);
8939  break;
8940  }
8941  connected->id.name.presentation = data[pos];
8942  break;
8944  if (ie_len != 1) {
8945  ast_log(LOG_WARNING, "Invalid connected line name valid (%u)\n",
8946  (unsigned) ie_len);
8947  break;
8948  }
8949  connected->id.name.valid = data[pos];
8950  break;
8951 /* Connected line party id number */
8953  ast_free(connected->id.number.str);
8954  connected->id.number.str = ast_malloc(ie_len + 1);
8955  if (connected->id.number.str) {
8956  memcpy(connected->id.number.str, data + pos, ie_len);
8957  connected->id.number.str[ie_len] = 0;
8958  }
8959  break;
8961  if (ie_len != 1) {
8962  ast_log(LOG_WARNING, "Invalid connected line numbering plan (%u)\n",
8963  (unsigned) ie_len);
8964  break;
8965  }
8966  connected->id.number.plan = data[pos];
8967  break;
8969  if (ie_len != 1) {
8970  ast_log(LOG_WARNING, "Invalid connected line number presentation (%u)\n",
8971  (unsigned) ie_len);
8972  break;
8973  }
8974  connected->id.number.presentation = data[pos];
8975  break;
8977  if (ie_len != 1) {
8978  ast_log(LOG_WARNING, "Invalid connected line number valid (%u)\n",
8979  (unsigned) ie_len);
8980  break;
8981  }
8982  connected->id.number.valid = data[pos];
8983  break;
8984 /* Connected line party id combined presentation */
8986  if (ie_len != 1) {
8987  ast_log(LOG_WARNING, "Invalid connected line combined presentation (%u)\n",
8988  (unsigned) ie_len);
8989  break;
8990  }
8991  combined_presentation = data[pos];
8992  got_combined_presentation = 1;
8993  break;
8994 /* Connected line party id subaddress */
8996  ast_free(connected->id.subaddress.str);
8997  connected->id.subaddress.str = ast_malloc(ie_len + 1);
8998  if (connected->id.subaddress.str) {
8999  memcpy(connected->id.subaddress.str, data + pos, ie_len);
9000  connected->id.subaddress.str[ie_len] = 0;
9001  }
9002  break;
9004  if (ie_len != 1) {
9005  ast_log(LOG_WARNING, "Invalid connected line type of subaddress (%u)\n",
9006  (unsigned) ie_len);
9007  break;
9008  }
9009  connected->id.subaddress.type = data[pos];
9010  break;
9012  if (ie_len != 1) {
9014  "Invalid connected line subaddress odd-even indicator (%u)\n",
9015  (unsigned) ie_len);
9016  break;
9017  }
9018  connected->id.subaddress.odd_even_indicator = data[pos];
9019  break;
9021  if (ie_len != 1) {
9022  ast_log(LOG_WARNING, "Invalid connected line subaddress valid (%u)\n",
9023  (unsigned) ie_len);
9024  break;
9025  }
9026  connected->id.subaddress.valid = data[pos];
9027  break;
9028 /* Connected line party tag */
9030  ast_free(connected->id.tag);
9031  connected->id.tag = ast_malloc(ie_len + 1);
9032  if (connected->id.tag) {
9033  memcpy(connected->id.tag, data + pos, ie_len);
9034  connected->id.tag[ie_len] = 0;
9035  }
9036  break;
9037 /* Connected line party source */
9039  if (ie_len != sizeof(value)) {
9040  ast_log(LOG_WARNING, "Invalid connected line source (%u)\n",
9041  (unsigned) ie_len);
9042  break;
9043  }
9044  memcpy(&value, data + pos, sizeof(value));
9045  connected->source = ntohl(value);
9046  break;
9047 /* Connected line party unknown element */
9048  default:
9049  ast_log(LOG_DEBUG, "Unknown connected line element: %u (%u)\n",
9050  (unsigned) ie_id, (unsigned) ie_len);
9051  break;
9052  }
9053  }
9054 
9055  switch (frame_version) {
9056  case 1:
9057  /*
9058  * The other end is an earlier version that we need to adjust
9059  * for compatibility.
9060  */
9061  connected->id.name.valid = 1;
9063  connected->id.number.valid = 1;
9064  if (got_combined_presentation) {
9067  }
9068  break;
9069  case 2:
9070  /* The other end is at the same level as we are. */
9071  break;
9072  default:
9073  /*
9074  * The other end is newer than we are.
9075  * We need to assume that they are compatible with us.
9076  */
9077  ast_log(LOG_DEBUG, "Connected line frame has newer version: %u\n",
9078  (unsigned) frame_version);
9079  break;
9080  }
9081 
9082  return 0;
9083 }
9084 
9086 {
9087  unsigned char data[1024]; /* This should be large enough */
9088  size_t datalen;
9089 
9090  datalen = ast_connected_line_build_data(data, sizeof(data), connected, update);
9091  if (datalen == (size_t) -1) {
9092  return;
9093  }
9094 
9095  ast_indicate_data(chan, AST_CONTROL_CONNECTED_LINE, data, datalen);
9096 }
9097 
9099 {
9100  unsigned char data[1024]; /* This should be large enough */
9101  size_t datalen;
9102 
9103  datalen = ast_connected_line_build_data(data, sizeof(data), connected, update);
9104  if (datalen == (size_t) -1) {
9105  return;
9106  }
9107 
9108  ast_queue_control_data(chan, AST_CONTROL_CONNECTED_LINE, data, datalen);
9109 }
9110 
9111 void ast_channel_set_redirecting(struct ast_channel *chan, const struct ast_party_redirecting *redirecting, const struct ast_set_party_redirecting *update)
9112 {
9113  if (&chan->redirecting == redirecting) {
9114  /* Don't set to self */
9115  return;
9116  }
9117 
9118  ast_channel_lock(chan);
9119  ast_party_redirecting_set(&chan->redirecting, redirecting, update);
9120  ast_channel_unlock(chan);
9121 }
9122 
9123 /*!
9124  * \brief Element identifiers for redirecting indication frame data
9125  * \note Only add to the end of this enum.
9126  */
9127 enum {
9159 };
9160 
9161 int ast_redirecting_build_data(unsigned char *data, size_t datalen, const struct ast_party_redirecting *redirecting, const struct ast_set_party_redirecting *update)
9162 {
9163  int32_t value;
9164  size_t pos = 0;
9165  int res;
9166 
9167  static const struct ast_party_id_ies from_ies = {
9169  .name.char_set = AST_REDIRECTING_FROM_NAME_CHAR_SET,
9170  .name.presentation = AST_REDIRECTING_FROM_NAME_PRESENTATION,
9171  .name.valid = AST_REDIRECTING_FROM_NAME_VALID,
9172 
9173  .number.str = AST_REDIRECTING_FROM_NUMBER,
9174  .number.plan = AST_REDIRECTING_FROM_NUMBER_PLAN,
9175  .number.presentation = AST_REDIRECTING_FROM_NUMBER_PRESENTATION,
9176  .number.valid = AST_REDIRECTING_FROM_NUMBER_VALID,
9177 
9178  .subaddress.str = AST_REDIRECTING_FROM_SUBADDRESS,
9179  .subaddress.type = AST_REDIRECTING_FROM_SUBADDRESS_TYPE,
9180  .subaddress.odd_even_indicator = AST_REDIRECTING_FROM_SUBADDRESS_ODD_EVEN,
9181  .subaddress.valid = AST_REDIRECTING_FROM_SUBADDRESS_VALID,
9182 
9183  .tag = AST_REDIRECTING_FROM_TAG,
9184  .combined_presentation = AST_REDIRECTING_FROM_ID_PRESENTATION,
9185  };
9186  static const struct ast_party_id_ies to_ies = {
9188  .name.char_set = AST_REDIRECTING_TO_NAME_CHAR_SET,
9189  .name.presentation = AST_REDIRECTING_TO_NAME_PRESENTATION,
9190  .name.valid = AST_REDIRECTING_TO_NAME_VALID,
9191 
9192  .number.str = AST_REDIRECTING_TO_NUMBER,
9193  .number.plan = AST_REDIRECTING_TO_NUMBER_PLAN,
9194  .number.presentation = AST_REDIRECTING_TO_NUMBER_PRESENTATION,
9195  .number.valid = AST_REDIRECTING_TO_NUMBER_VALID,
9196 
9197  .subaddress.str = AST_REDIRECTING_TO_SUBADDRESS,
9198  .subaddress.type = AST_REDIRECTING_TO_SUBADDRESS_TYPE,
9199  .subaddress.odd_even_indicator = AST_REDIRECTING_TO_SUBADDRESS_ODD_EVEN,
9200  .subaddress.valid = AST_REDIRECTING_TO_SUBADDRESS_VALID,
9201 
9202  .tag = AST_REDIRECTING_TO_TAG,
9203  .combined_presentation = AST_REDIRECTING_TO_ID_PRESENTATION,
9204  };
9205 
9206  /* Redirecting frame version */
9207  if (datalen < pos + (sizeof(data[0]) * 2) + 1) {
9208  ast_log(LOG_WARNING, "No space left for redirecting frame version\n");
9209  return -1;
9210  }
9211  data[pos++] = AST_REDIRECTING_VERSION;
9212  data[pos++] = 1;
9213  data[pos++] = 2;/* Version 1 did not have a version ie */
9214 
9215  res = party_id_build_data(data + pos, datalen - pos, &redirecting->from,
9216  "redirecting-from", &from_ies, update ? &update->from : NULL);
9217  if (res < 0) {
9218  return -1;
9219  }
9220  pos += res;
9221 
9222  res = party_id_build_data(data + pos, datalen - pos, &redirecting->to,
9223  "redirecting-to", &to_ies, update ? &update->to : NULL);
9224  if (res < 0) {
9225  return -1;
9226  }
9227  pos += res;
9228 
9229  /* Redirecting reason */
9230  if (datalen < pos + (sizeof(data[0]) * 2) + sizeof(value)) {
9231  ast_log(LOG_WARNING, "No space left for redirecting reason\n");
9232  return -1;
9233  }
9234  data[pos++] = AST_REDIRECTING_REASON;
9235  data[pos++] = sizeof(value);
9236  value = htonl(redirecting->reason);
9237  memcpy(data + pos, &value, sizeof(value));
9238  pos += sizeof(value);
9239 
9240  /* Redirecting count */
9241  if (datalen < pos + (sizeof(data[0]) * 2) + sizeof(value)) {
9242  ast_log(LOG_WARNING, "No space left for redirecting count\n");
9243  return -1;
9244  }
9245  data[pos++] = AST_REDIRECTING_COUNT;
9246  data[pos++] = sizeof(value);
9247  value = htonl(redirecting->count);
9248  memcpy(data + pos, &value, sizeof(value));
9249  pos += sizeof(value);
9250 
9251  return pos;
9252 }
9253 
9254 int ast_redirecting_parse_data(const unsigned char *data, size_t datalen, struct ast_party_redirecting *redirecting)
9255 {
9256  size_t pos;
9257  unsigned char ie_len;
9258  unsigned char ie_id;
9259  int32_t value;
9260  int frame_version = 1;
9261  int from_combined_presentation = 0;
9262  int got_from_combined_presentation = 0;/* TRUE if got a combined name and number presentation value. */
9263  int to_combined_presentation = 0;
9264  int got_to_combined_presentation = 0;/* TRUE if got a combined name and number presentation value. */
9265 
9266  for (pos = 0; pos < datalen; pos += ie_len) {
9267  if (datalen < pos + sizeof(ie_id) + sizeof(ie_len)) {
9268  ast_log(LOG_WARNING, "Invalid redirecting update\n");
9269  return -1;
9270  }
9271  ie_id = data[pos++];
9272  ie_len = data[pos++];
9273  if (datalen < pos + ie_len) {
9274  ast_log(LOG_WARNING, "Invalid redirecting update\n");
9275  return -1;
9276  }
9277 
9278  switch (ie_id) {
9279 /* Redirecting frame version */
9281  if (ie_len != 1) {
9282  ast_log(LOG_WARNING, "Invalid redirecting frame version (%u)\n",
9283  (unsigned) ie_len);
9284  break;
9285  }
9286  frame_version = data[pos];
9287  break;
9288 /* Redirecting-from party id name */
9290  ast_free(redirecting->from.name.str);
9291  redirecting->from.name.str = ast_malloc(ie_len + 1);
9292  if (redirecting->from.name.str) {
9293  memcpy(redirecting->from.name.str, data + pos, ie_len);
9294  redirecting->from.name.str[ie_len] = 0;
9295  }
9296  break;
9298  if (ie_len != 1) {
9299  ast_log(LOG_WARNING, "Invalid redirecting-from name char set (%u)\n",
9300  (unsigned) ie_len);
9301  break;
9302  }
9303  redirecting->from.name.char_set = data[pos];
9304  break;
9306  if (ie_len != 1) {
9307  ast_log(LOG_WARNING, "Invalid redirecting-from name presentation (%u)\n",
9308  (unsigned) ie_len);
9309  break;
9310  }
9311  redirecting->from.name.presentation = data[pos];
9312  break;
9314  if (ie_len != 1) {
9315  ast_log(LOG_WARNING, "Invalid redirecting-from name valid (%u)\n",
9316  (unsigned) ie_len);
9317  break;
9318  }
9319  redirecting->from.name.valid = data[pos];
9320  break;
9321 /* Redirecting-from party id number */
9323  ast_free(redirecting->from.number.str);
9324  redirecting->from.number.str = ast_malloc(ie_len + 1);
9325  if (redirecting->from.number.str) {
9326  memcpy(redirecting->from.number.str, data + pos, ie_len);
9327  redirecting->from.number.str[ie_len] = 0;
9328  }
9329  break;
9331  if (ie_len != 1) {
9332  ast_log(LOG_WARNING, "Invalid redirecting-from numbering plan (%u)\n",
9333  (unsigned) ie_len);
9334  break;
9335  }
9336  redirecting->from.number.plan = data[pos];
9337  break;
9339  if (ie_len != 1) {
9340  ast_log(LOG_WARNING, "Invalid redirecting-from number presentation (%u)\n",
9341  (unsigned) ie_len);
9342  break;
9343  }
9344  redirecting->from.number.presentation = data[pos];
9345  break;
9347  if (ie_len != 1) {
9348  ast_log(LOG_WARNING, "Invalid redirecting-from number valid (%u)\n",
9349  (unsigned) ie_len);
9350  break;
9351  }
9352  redirecting->from.number.valid = data[pos];
9353  break;
9354 /* Redirecting-from party id combined presentation */
9356  if (ie_len != 1) {
9357  ast_log(LOG_WARNING, "Invalid redirecting-from combined presentation (%u)\n",
9358  (unsigned) ie_len);
9359  break;
9360  }
9361  from_combined_presentation = data[pos];
9362  got_from_combined_presentation = 1;
9363  break;
9364 /* Redirecting-from party id subaddress */
9366  ast_free(redirecting->from.subaddress.str);
9367  redirecting->from.subaddress.str = ast_malloc(ie_len + 1);
9368  if (redirecting->from.subaddress.str) {
9369  memcpy(redirecting->from.subaddress.str, data + pos, ie_len);
9370  redirecting->from.subaddress.str[ie_len] = 0;
9371  }
9372  break;
9374  if (ie_len != 1) {
9375  ast_log(LOG_WARNING, "Invalid redirecting-from type of subaddress (%u)\n",
9376  (unsigned) ie_len);
9377  break;
9378  }
9379  redirecting->from.subaddress.type = data[pos];
9380  break;
9382  if (ie_len != 1) {
9384  "Invalid redirecting-from subaddress odd-even indicator (%u)\n",
9385  (unsigned) ie_len);
9386  break;
9387  }
9388  redirecting->from.subaddress.odd_even_indicator = data[pos];
9389  break;
9391  if (ie_len != 1) {
9392  ast_log(LOG_WARNING, "Invalid redirecting-from subaddress valid (%u)\n",
9393  (unsigned) ie_len);
9394  break;
9395  }
9396  redirecting->from.subaddress.valid = data[pos];
9397  break;
9398 /* Redirecting-from party id tag */
9400  ast_free(redirecting->from.tag);
9401  redirecting->from.tag = ast_malloc(ie_len + 1);
9402  if (redirecting->from.tag) {
9403  memcpy(redirecting->from.tag, data + pos, ie_len);
9404  redirecting->from.tag[ie_len] = 0;
9405  }
9406  break;
9407 /* Redirecting-to party id name */
9409  ast_free(redirecting->to.name.str);
9410  redirecting->to.name.str = ast_malloc(ie_len + 1);
9411  if (redirecting->to.name.str) {
9412  memcpy(redirecting->to.name.str, data + pos, ie_len);
9413  redirecting->to.name.str[ie_len] = 0;
9414  }
9415  break;
9417  if (ie_len != 1) {
9418  ast_log(LOG_WARNING, "Invalid redirecting-to name char set (%u)\n",
9419  (unsigned) ie_len);
9420  break;
9421  }
9422  redirecting->to.name.char_set = data[pos];
9423  break;
9425  if (ie_len != 1) {
9426  ast_log(LOG_WARNING, "Invalid redirecting-to name presentation (%u)\n",
9427  (unsigned) ie_len);
9428  break;
9429  }
9430  redirecting->to.name.presentation = data[pos];
9431  break;
9433  if (ie_len != 1) {
9434  ast_log(LOG_WARNING, "Invalid redirecting-to name valid (%u)\n",
9435  (unsigned) ie_len);
9436  break;
9437  }
9438  redirecting->to.name.valid = data[pos];
9439  break;
9440 /* Redirecting-to party id number */
9442  ast_free(redirecting->to.number.str);
9443  redirecting->to.number.str = ast_malloc(ie_len + 1);
9444  if (redirecting->to.number.str) {
9445  memcpy(redirecting->to.number.str, data + pos, ie_len);
9446  redirecting->to.number.str[ie_len] = 0;
9447  }
9448  break;
9450  if (ie_len != 1) {
9451  ast_log(LOG_WARNING, "Invalid redirecting-to numbering plan (%u)\n",
9452  (unsigned) ie_len);
9453  break;
9454  }
9455  redirecting->to.number.plan = data[pos];
9456  break;
9458  if (ie_len != 1) {
9459  ast_log(LOG_WARNING, "Invalid redirecting-to number presentation (%u)\n",
9460  (unsigned) ie_len);
9461  break;
9462  }
9463  redirecting->to.number.presentation = data[pos];
9464  break;
9466  if (ie_len != 1) {
9467  ast_log(LOG_WARNING, "Invalid redirecting-to number valid (%u)\n",
9468  (unsigned) ie_len);
9469  break;
9470  }
9471  redirecting->to.number.valid = data[pos];
9472  break;
9473 /* Redirecting-to party id combined presentation */
9475  if (ie_len != 1) {
9476  ast_log(LOG_WARNING, "Invalid redirecting-to combined presentation (%u)\n",
9477  (unsigned) ie_len);
9478  break;
9479  }
9480  to_combined_presentation = data[pos];
9481  got_to_combined_presentation = 1;
9482  break;
9483 /* Redirecting-to party id subaddress */
9485  ast_free(redirecting->to.subaddress.str);
9486  redirecting->to.subaddress.str = ast_malloc(ie_len + 1);
9487  if (redirecting->to.subaddress.str) {
9488  memcpy(redirecting->to.subaddress.str, data + pos, ie_len);
9489  redirecting->to.subaddress.str[ie_len] = 0;
9490  }
9491  break;
9493  if (ie_len != 1) {
9494  ast_log(LOG_WARNING, "Invalid redirecting-to type of subaddress (%u)\n",
9495  (unsigned) ie_len);
9496  break;
9497  }
9498  redirecting->to.subaddress.type = data[pos];
9499  break;
9501  if (ie_len != 1) {
9503  "Invalid redirecting-to subaddress odd-even indicator (%u)\n",
9504  (unsigned) ie_len);
9505  break;
9506  }
9507  redirecting->to.subaddress.odd_even_indicator = data[pos];
9508  break;
9510  if (ie_len != 1) {
9511  ast_log(LOG_WARNING, "Invalid redirecting-to subaddress valid (%u)\n",
9512  (unsigned) ie_len);
9513  break;
9514  }
9515  redirecting->to.subaddress.valid = data[pos];
9516  break;
9517 /* Redirecting-to party id tag */
9519  ast_free(redirecting->to.tag);
9520  redirecting->to.tag = ast_malloc(ie_len + 1);
9521  if (redirecting->to.tag) {
9522  memcpy(redirecting->to.tag, data + pos, ie_len);
9523  redirecting->to.tag[ie_len] = 0;
9524  }
9525  break;
9526 /* Redirecting reason */
9528  if (ie_len != sizeof(value)) {
9529  ast_log(LOG_WARNING, "Invalid redirecting reason (%u)\n",
9530  (unsigned) ie_len);
9531  break;
9532  }
9533  memcpy(&value, data + pos, sizeof(value));
9534  redirecting->reason = ntohl(value);
9535  break;
9536 /* Redirecting count */
9537  case AST_REDIRECTING_COUNT:
9538  if (ie_len != sizeof(value)) {
9539  ast_log(LOG_WARNING, "Invalid redirecting count (%u)\n",
9540  (unsigned) ie_len);
9541  break;
9542  }
9543  memcpy(&value, data + pos, sizeof(value));
9544  redirecting->count = ntohl(value);
9545  break;
9546 /* Redirecting unknown element */
9547  default:
9548  ast_log(LOG_DEBUG, "Unknown redirecting element: %u (%u)\n",
9549  (unsigned) ie_id, (unsigned) ie_len);
9550  break;
9551  }
9552  }
9553 
9554  switch (frame_version) {
9555  case 1:
9556  /*
9557  * The other end is an earlier version that we need to adjust
9558  * for compatibility.
9559  */
9560  redirecting->from.name.valid = 1;
9562  redirecting->from.number.valid = 1;
9563  if (got_from_combined_presentation) {
9564  redirecting->from.name.presentation = from_combined_presentation;
9565  redirecting->from.number.presentation = from_combined_presentation;
9566  }
9567 
9568  redirecting->to.name.valid = 1;
9570  redirecting->to.number.valid = 1;
9571  if (got_to_combined_presentation) {
9572  redirecting->to.name.presentation = to_combined_presentation;
9573  redirecting->to.number.presentation = to_combined_presentation;
9574  }
9575  break;
9576  case 2:
9577  /* The other end is at the same level as we are. */
9578  break;
9579  default:
9580  /*
9581  * The other end is newer than we are.
9582  * We need to assume that they are compatible with us.
9583  */
9584  ast_log(LOG_DEBUG, "Redirecting frame has newer version: %u\n",
9585  (unsigned) frame_version);
9586  break;
9587  }
9588 
9589  return 0;
9590 }
9591 
9592 void ast_channel_update_redirecting(struct ast_channel *chan, const struct ast_party_redirecting *redirecting, const struct ast_set_party_redirecting *update)
9593 {
9594  unsigned char data[1024]; /* This should be large enough */
9595  size_t datalen;
9596 
9597  datalen = ast_redirecting_build_data(data, sizeof(data), redirecting, update);
9598  if (datalen == (size_t) -1) {
9599  return;
9600  }
9601 
9602  ast_indicate_data(chan, AST_CONTROL_REDIRECTING, data, datalen);
9603 }
9604 
9606 {
9607  unsigned char data[1024]; /* This should be large enough */
9608  size_t datalen;
9609 
9610  datalen = ast_redirecting_build_data(data, sizeof(data), redirecting, update);
9611  if (datalen == (size_t) -1) {
9612  return;
9613  }
9614 
9615  ast_queue_control_data(chan, AST_CONTROL_REDIRECTING, data, datalen);
9616 }
9617 
9618 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)
9619 {
9620  const char *macro;
9621  const char *macro_args;
9622  int retval;
9623 
9624  ast_channel_lock(macro_chan);
9625  macro = pbx_builtin_getvar_helper(macro_chan, is_caller
9626  ? "CONNECTED_LINE_CALLER_SEND_MACRO" : "CONNECTED_LINE_CALLEE_SEND_MACRO");
9627  macro = ast_strdupa(S_OR(macro, ""));
9628  macro_args = pbx_builtin_getvar_helper(macro_chan, is_caller
9629  ? "CONNECTED_LINE_CALLER_SEND_MACRO_ARGS" : "CONNECTED_LINE_CALLEE_SEND_MACRO_ARGS");
9630  macro_args = ast_strdupa(S_OR(macro_args, ""));
9631 
9632  if (ast_strlen_zero(macro)) {
9633  ast_channel_unlock(macro_chan);
9634  return -1;
9635  }
9636 
9637  if (is_frame) {
9638  const struct ast_frame *frame = connected_info;
9639 
9640  ast_connected_line_parse_data(frame->data.ptr, frame->datalen, &macro_chan->connected);
9641  } else {
9642  const struct ast_party_connected_line *connected = connected_info;
9643 
9644  ast_party_connected_line_copy(&macro_chan->connected, connected);
9645  }
9646  ast_channel_unlock(macro_chan);
9647 
9648  retval = ast_app_run_macro(autoservice_chan, macro_chan, macro, macro_args);
9649  if (!retval) {
9650  struct ast_party_connected_line saved_connected;
9651 
9652  ast_party_connected_line_init(&saved_connected);
9653  ast_channel_lock(macro_chan);
9654  ast_party_connected_line_copy(&saved_connected, &macro_chan->connected);
9655  ast_channel_unlock(macro_chan);
9656  ast_channel_update_connected_line(macro_chan, &saved_connected, NULL);
9657  ast_party_connected_line_free(&saved_connected);
9658  }
9659 
9660  return retval;
9661 }
9662 
9663 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)
9664 {
9665  const char *macro;
9666  const char *macro_args;
9667  int retval;
9668 
9669  ast_channel_lock(macro_chan);
9670  macro = pbx_builtin_getvar_helper(macro_chan, is_caller
9671  ? "REDIRECTING_CALLER_SEND_MACRO" : "REDIRECTING_CALLEE_SEND_MACRO");
9672  macro = ast_strdupa(S_OR(macro, ""));
9673  macro_args = pbx_builtin_getvar_helper(macro_chan, is_caller
9674  ? "REDIRECTING_CALLER_SEND_MACRO_ARGS" : "REDIRECTING_CALLEE_SEND_MACRO_ARGS");
9675  macro_args = ast_strdupa(S_OR(macro_args, ""));
9676 
9677  if (ast_strlen_zero(macro)) {
9678  ast_channel_unlock(macro_chan);
9679  return -1;
9680  }
9681 
9682  if (is_frame) {
9683  const struct ast_frame *frame = redirecting_info;
9684 
9685  ast_redirecting_parse_data(frame->data.ptr, frame->datalen, &macro_chan->redirecting);
9686  } else {
9687  const struct ast_party_redirecting *redirecting = redirecting_info;
9688 
9689  ast_party_redirecting_copy(&macro_chan->redirecting, redirecting);
9690  }
9691  ast_channel_unlock(macro_chan);
9692 
9693  retval = ast_app_run_macro(autoservice_chan, macro_chan, macro, macro_args);
9694  if (!retval) {
9695  struct ast_party_redirecting saved_redirecting;
9696 
9697  ast_party_redirecting_init(&saved_redirecting);
9698  ast_channel_lock(macro_chan);
9699  ast_party_redirecting_copy(&saved_redirecting, &macro_chan->redirecting);
9700  ast_channel_unlock(macro_chan);
9701  ast_channel_update_redirecting(macro_chan, &saved_redirecting, NULL);
9702  ast_party_redirecting_free(&saved_redirecting);
9703  }
9704 
9705  return retval;
9706 }
9707 
9708 static void *channel_cc_params_copy(void *data)
9709 {
9710  const struct ast_cc_config_params *src = data;
9712  if (!dest) {
9713  return NULL;
9714  }
9715  ast_cc_copy_config_params(dest, src);
9716  return dest;
9717 }
9718 
9719 static void channel_cc_params_destroy(void *data)
9720 {
9721  struct ast_cc_config_params *cc_params = data;
9722  ast_cc_config_params_destroy(cc_params);
9723 }
9724 
9726  .type = "Call Completion",
9727  .duplicate = channel_cc_params_copy,
9728  .destroy = channel_cc_params_destroy,
9729 };
9730 
9732  const struct ast_cc_config_params *base_params)
9733 {
9734  struct ast_cc_config_params *cc_params;
9735  struct ast_datastore *cc_datastore;
9736 
9737  if (!(cc_params = ast_cc_config_params_init())) {
9738  return -1;
9739  }
9740 
9741  if (!(cc_datastore = ast_datastore_alloc(&cc_channel_datastore_info, NULL))) {
9742  ast_cc_config_params_destroy(cc_params);
9743  return -1;
9744  }
9745 
9746  if (base_params) {
9747  ast_cc_copy_config_params(cc_params, base_params);
9748  }
9749  cc_datastore->data = cc_params;
9750  ast_channel_datastore_add(chan, cc_datastore);
9751  return 0;
9752 }
9753 
9755 {
9756  struct ast_datastore *cc_datastore;
9757 
9758  if (!(cc_datastore = ast_channel_datastore_find(chan, &cc_channel_datastore_info, NULL))) {
9759  /* If we can't find the datastore, it almost definitely means that the channel type being
9760  * used has not had its driver modified to parse CC config parameters. The best action
9761  * to take here is to create the parameters on the spot with the defaults set.
9762  */
9763  if (ast_channel_cc_params_init(chan, NULL)) {
9764  return NULL;
9765  }
9766  if (!(cc_datastore = ast_channel_datastore_find(chan, &cc_channel_datastore_info, NULL))) {
9767  /* Should be impossible */
9768  return NULL;
9769  }
9770  }
9771 
9772  ast_assert(cc_datastore->data != NULL);
9773  return cc_datastore->data;
9774 }
9775 
9776 int ast_channel_get_device_name(struct ast_channel *chan, char *device_name, size_t name_buffer_length)
9777 {
9778  int len = name_buffer_length;
9779  char *dash;
9780  if (!ast_channel_queryoption(chan, AST_OPTION_DEVICE_NAME, device_name, &len, 0)) {
9781  return 0;
9782  }
9783 
9784  /* Dang. Do it the old-fashioned way */
9785  ast_copy_string(device_name, chan->name, name_buffer_length);
9786  if ((dash = strrchr(device_name, '-'))) {
9787  *dash = '\0';
9788  }
9789 
9790  return 0;
9791 }
9792 
9793 int ast_channel_get_cc_agent_type(struct ast_channel *chan, char *agent_type, size_t size)
9794 {
9795  int len = size;
9796  char *slash;
9797 
9798  if (!ast_channel_queryoption(chan, AST_OPTION_CC_AGENT_TYPE, agent_type, &len, 0)) {
9799  return 0;
9800  }
9801 
9802  ast_copy_string(agent_type, chan->name, size);
9803  if ((slash = strchr(agent_type, '/'))) {
9804  *slash = '\0';
9805  }
9806  return 0;
9807 }
9808 
9809 /* DO NOT PUT ADDITIONAL FUNCTIONS BELOW THIS BOUNDARY
9810  *
9811  * ONLY FUNCTIONS FOR PROVIDING BACKWARDS ABI COMPATIBILITY BELONG HERE
9812  *
9813  */
9814 
9815 /* Provide binary compatibility for modules that call ast_channel_alloc() directly;
9816  * newly compiled modules will call __ast_channel_alloc() via the macros in channel.h
9817  */
9818 #undef ast_channel_alloc
9819 struct ast_channel __attribute__((format(printf, 10, 11)))
9820  *ast_channel_alloc(int needqueue, int state, const char *cid_num,
9821  const char *cid_name, const char *acctcode,
9822  const char *exten, const char *context,
9823  const char *linkedid, const int amaflag,
9824  const char *name_fmt, ...);
9825 struct ast_channel *ast_channel_alloc(int needqueue, int state, const char *cid_num,
9826  const char *cid_name, const char *acctcode,
9827  const char *exten, const char *context,
9828  const char *linkedid, const int amaflag,
9829  const char *name_fmt, ...)
9830 {
9831  va_list ap1, ap2;
9832  struct ast_channel *result;
9833 
9834 
9835  va_start(ap1, name_fmt);
9836  va_start(ap2, name_fmt);
9837  result = __ast_channel_alloc_ap(needqueue, state, cid_num, cid_name, acctcode, exten, context,
9838  linkedid, amaflag, __FILE__, __LINE__, __FUNCTION__, name_fmt, ap1, ap2);
9839  va_end(ap1);
9840  va_end(ap2);
9841 
9842  return result;
9843 }
int valid
valid/present ie
Definition: channel.c:8479
int(* func_channel_read)(struct ast_channel *chan, const char *function, char *data, char *buf, size_t len)
Provide additional read items for CHANNEL() dialplan function.
Definition: channel.h:600
const char * type
Definition: datastore.h:32
const ast_string_field peeraccount
Definition: channel.h:787
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:2345
int ast_cdr_disposition(struct ast_cdr *cdr, int cause)
Save the result of the call based on the AST_CAUSE_*.
Definition: cdr.c:790
#define AST_CAUSE_PROTOCOL_ERROR
Definition: causes.h:144
void * music_state
Definition: channel.h:745
static char musicclass[MAX_MUSICCLASS]
Definition: chan_mgcp.c:155
#define AST_THREADSTORAGE(name)
Define a thread storage variable.
Definition: threadstorage.h:81
struct ast_channel * ast_waitfor_n(struct ast_channel **chan, 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:3534
int ast_safe_sleep(struct ast_channel *chan, int ms)
Wait for a specified amount of time, looking for hangups.
Definition: channel.c:1916
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:1936
int ast_queue_hangup(struct ast_channel *chan)
Queue a hangup frame.
Definition: channel.c:1569
unsigned long long ast_group_t
Definition: channel.h:175
union ast_frame_subclass subclass
Definition: frame.h:146
int ast_hangup(struct ast_channel *chan)
Hang up a channel.
Definition: channel.c:2804
int ast_channel_early_bridge(struct ast_channel *c0, struct ast_channel *c1)
Bridge two channels together (early)
Definition: channel.c:7406
Information needed to identify an endpoint in a call.
Definition: channel.h:288
enum sip_cc_notify_state state
Definition: chan_sip.c:842
int ast_recvchar(struct ast_channel *chan, int timeout)
Receives a text character from a channel.
Definition: channel.c:4642
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:797
int char_set
Character set ie.
Definition: channel.c:8475
struct ast_channel * chan
Definition: channel.c:88
void ast_party_connected_line_init(struct ast_party_connected_line *init)
Initialize the given connected line structure.
Definition: channel.c:2308
static char accountcode[AST_MAX_ACCOUNT_CODE]
Definition: chan_iax2.c:383
int presentation
Q.931 encoded presentation-indicator encoded field.
Definition: channel.h:227
#define AST_CAUSE_MANDATORY_IE_MISSING
Definition: causes.h:136
struct ast_channel * masqr
Definition: channel.h:752
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:7051
#define ast_channel_lock(chan)
Definition: channel.h:2466
#define AST_CAUSE_BEARERCAPABILITY_NOTAUTH
Definition: causes.h:128
#define AST_OPTION_FORMAT_WRITE
Definition: frame.h:498
void ast_bridge_end_dtmf(struct ast_channel *chan, char digit, struct timeval start, const char *why)
Simulate a DTMF end on a broken bridge channel.
Definition: features.c:3927
static char exten[AST_MAX_EXTENSION]
Definition: chan_alsa.c:109
Main Channel structure associated with a channel.
Definition: channel.h:742
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:2388
Music on hold handling.
const char * cid_num
Definition: channel.h:1002
struct ast_channel * ast_channel_get_by_exten(const char *exten, const char *context)
Find a channel by extension and context.
Definition: channel.c:1813
#define AST_CLI_DEFINE(fn, txt,...)
Definition: cli.h:191
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:8825
void ast_cdr_failed(struct ast_cdr *cdr)
Fail a call.
Definition: cdr.c:764
int plan
Q.931 Type-Of-Number and Numbering-Plan encoded fields.
Definition: channel.h:338
long feature_timer
Definition: channel.h:980
char * str
Subscriber phone number (Malloced)
Definition: channel.h:241
struct ast_channel * masq
Definition: channel.h:751
General Asterisk channel transcoding definitions.
struct chanlist::@238 list
struct ast_party_connected_line connected
Channel Connected Line ID information.
Definition: channel.h:811
#define AST_CAUSE_SUBSCRIBER_ABSENT
Definition: causes.h:109
const char *const type
Definition: channel.h:508
#define AST_CAUSE_OUTGOING_CALL_BARRED
Definition: causes.h:126
#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:208
static void adjust_frame_for_plc(struct ast_channel *chan, struct ast_frame *frame, struct ast_datastore *datastore)
Definition: channel.c:4837
struct ast_channel * ast_channel_iterator_next(struct ast_channel_iterator *i)
Get the next channel for a channel iterator.
Definition: channel.c:1715
Asterisk main include file. File version handling, generic pbx functions.
void ast_poll_channel_add(struct ast_channel *chan0, struct ast_channel *chan1)
Definition: channel.c:2665
#define AST_BRIDGE_IGNORE_SIGS
Ignore all signal frames except NULL.
Definition: channel.h:1940
#define ao2_link(arg1, arg2)
Definition: astobj2.h:785
#define AST_LIST_FIRST(head)
Returns the first entry contained in a list.
Definition: linkedlists.h:420
struct ast_channel *(*const requester)(const char *type, format_t format, const struct ast_channel *requestor, void *data, int *cause)
Requester - to set up call data structures (pvt&#39;s)
Definition: channel.h:519
static void bridge_play_sounds(struct ast_channel *c0, struct ast_channel *c1)
Definition: channel.c:7478
int ao2_container_count(struct ao2_container *c)
Returns the number of elements in a container.
Definition: astobj2.c:470
int ast_channel_cmpwhentohangup(struct ast_channel *chan, time_t offset)
Compare a offset with the settings of when to hang a channel up.
Definition: channel.c:900
int(*const send_image)(struct ast_channel *chan, struct ast_frame *frame)
Display or send an image.
Definition: channel.h:560
static const char config[]
Definition: cdr_csv.c:57
#define ARRAY_LEN(a)
Definition: isdn_lib.c:42
int offset
Definition: frame.h:156
The data tree to be returned by the callbacks and managed by functions local to this file...
Definition: data.c:85
char * str
Subscriber phone number (Malloced)
Definition: channel.h:336
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:7091
int(*const write_video)(struct ast_channel *chan, struct ast_frame *frame)
Write a frame, in standard format.
Definition: channel.h:591
static char parkinglot[AST_MAX_CONTEXT]
Definition: chan_mgcp.c:156
int ast_queue_control(struct ast_channel *chan, enum ast_control_frame_type control)
Queue a control frame with payload.
Definition: channel.c:1601
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:9254
struct ast_frame ast_null_frame
Definition: frame.c:131
struct timeval feature_start_time
Definition: channel.h:979
struct ast_party_caller caller
Channel Caller ID information.
Definition: channel.h:804
struct ast_set_party_id ani
Definition: channel.h:391
struct ast_tone_zone * zone
Definition: channel.h:767
void * ast_threadstorage_get(struct ast_threadstorage *ts, size_t init_size)
Retrieve thread storage.
int ast_autoservice_start(struct ast_channel *chan)
Automatically service a channel for us...
Definition: autoservice.c:179
char * strsep(char **str, const char *delims)
void ast_frame_dump(const char *name, struct ast_frame *f, char *prefix)
Definition: frame.c:778
static int __ast_queue_frame(struct ast_channel *chan, struct ast_frame *fin, int head, struct ast_frame *after)
Definition: channel.c:1438
static int attribute_const is_visible_indication(enum ast_control_frame_type condition)
Definition: channel.c:4398
#define ast_string_field_build_va(x, field, fmt, args1, args2)
Set a field to a complex (built) value.
Definition: stringfields.h:391
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:204
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:2003
format_t capabilities
Definition: channel.h:511
int priority
Definition: channel.h:841
#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
int timingfd
Definition: channel.h:838
struct ast_channel_tech * ast_get_channel_tech(const char *name)
Get a channel technology structure by name.
Definition: channel.c:960
int str
Subscriber phone number ie.
Definition: channel.c:8547
const ast_string_field uniqueid
Definition: channel.h:787
#define DEBUGCHAN_FLAG
Definition: channel.h:648
struct ast_flags features_callee
Definition: channel.h:976
static void ast_dummy_channel_destructor(void *obj)
Free a dummy channel structure.
Definition: channel.c:2530
static struct ast_channel_tech null_tech
Definition: channel.c:1120
struct ast_party_subaddress_ies subaddress
Subscriber subaddress ies.
Definition: channel.c:8700
int presentation
Q.931 presentation-indicator and screening-indicator encoded fields.
Definition: channel.h:245
channelreloadreason
Channel reload reasons for manager events at load or reload of configuration.
Definition: channel.h:1060
void ast_cc_config_params_destroy(struct ast_cc_config_params *params)
Free memory from CCSS configuration params.
Definition: ccss.c:579
#define ast_strdup(a)
Definition: astmm.h:109
int ast_channel_masquerade(struct ast_channel *original, struct ast_channel *clone)
Weird function made for call transfers.
Definition: channel.c:6110
CallerID (and other GR30) management and generation Includes code and algorithms from the Zapata libr...
format_t writeformat
Definition: channel.h:854
#define AST_CAUSE_INCOMING_CALL_BARRED
Definition: causes.h:127
int visible_indication
Definition: channel.h:861
#define STATE2STR_BUFSIZE
Definition: channel.c:107
void ast_cel_check_retire_linkedid(struct ast_channel *chan)
Check and potentially retire a Linked ID.
Definition: cel.c:429
struct ast_channel * parent_channel
Definition: channel.h:1006
static struct ast_data_handler channels_provider
Definition: channel.c:8192
#define ast_alloca(size)
call __builtin_alloca to ensure we get gcc builtin semantics
Definition: utils.h:653
struct ast_party_id id
Connected party ID.
Definition: channel.h:403
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:3823
#define AST_DIGIT_ANY
Definition: file.h:47
const char * name
Definition: channel.c:205
int ast_cli_unregister_multiple(struct ast_cli_entry *e, int len)
Unregister multiple commands.
Definition: cli.c:2177
int ast_jb_put(struct ast_channel *chan, struct ast_frame *f)
Puts a frame into a channel jitterbuffer.
Definition: abstract_jb.c:306
#define AST_CAUSE_NORMAL_TEMPORARY_FAILURE
Definition: causes.h:121
#define ast_channel_unref(c)
Decrease channel reference count.
Definition: channel.h:2502
#define AST_CAUSE_NOSUCHDRIVER
Definition: causes.h:155
const char * ast_describe_caller_presentation(int data)
Convert caller ID pres value to explanatory string.
Definition: callerid.c:1165
#define ast_set2_flag(p, value, flag)
Definition: utils.h:94
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:2329
#define ast_test_flag(p, flag)
Definition: utils.h:63
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:2042
#define AST_CAUSE_SWITCH_CONGESTION
Definition: causes.h:122
int __ast_answer(struct ast_channel *chan, unsigned int delay, int cdr_answer)
Answer a channel, with a selectable delay before returning.
Definition: channel.c:2982
void ast_channel_setwhentohangup_tv(struct ast_channel *chan, struct timeval offset)
Set when to hang a channel up.
Definition: channel.c:871
Device state management.
int ast_activate_generator(struct ast_channel *chan, struct ast_generator *gen, void *params)
Definition: channel.c:3148
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.
int ast_indicate(struct ast_channel *chan, int condition)
Indicates condition of channel.
Definition: channel.c:4393
static void free_translation(struct ast_channel *clonechan)
Definition: channel.c:2757
void ast_cdr_end(struct ast_cdr *cdr)
End a call.
Definition: cdr.c:933
Call Event Logging API.
const char * ast_var_value(const struct ast_var_t *var)
Definition: chanvars.c:89
struct ast_party_name name
Subscriber name.
Definition: channel.h:290
char emulate_dtmf_digit
Definition: channel.h:872
#define AST_FORMAT_G723_1
Definition: frame.h:242
void ast_jb_empty_and_reset(struct ast_channel *c0, struct ast_channel *c1)
drops all frames from a jitterbuffer and resets it
Definition: abstract_jb.c:627
void ast_channel_unregister(const struct ast_channel_tech *tech)
Unregister a channel technology.
Definition: channel.c:938
struct ast_party_id from
Who is redirecting the call (Sent to the party the call is redirected toward)
Definition: channel.h:449
void * ptr
Definition: frame.h:160
void * tech_pvt
Definition: channel.h:744
#define AST_CAUSE_UNALLOCATED
Definition: causes.h:97
int ast_channel_queryoption(struct ast_channel *channel, int option, void *data, int *datalen, int block)
Checks the value of an option.
Definition: channel.c:7815
plc_state_t plc_state
Definition: channel.c:4822
struct ast_var_t * ast_var_assign(const char *name, const char *value)
Definition: chanvars.c:41
int presentation
presentation-indicator ie
Definition: channel.c:8477
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:9592
#define AST_DEFAULT_EMULATE_DTMF_DURATION
Definition: channel.c:111
Channel Variables.
#define AST_RWLIST_WRLOCK(head)
Write locks a list.
Definition: linkedlists.h:51
char context[AST_MAX_CONTEXT]
Definition: channel.h:868
int(*const send_digit_begin)(struct ast_channel *chan, char digit)
Start sending a literal DTMF digit.
Definition: channel.h:528
#define AST_CAUSE_INVALID_MSG_UNSPECIFIED
Definition: causes.h:135
struct ast_variable * ast_channeltype_list(void)
return an ast_variable list of channeltypes
Definition: channel.c:261
struct ast_channel * ast_channel_release(struct ast_channel *chan)
Unlink and release reference to a channel.
Definition: channel.c:1921
#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:69
int ast_channel_supports_html(struct ast_channel *channel)
Checks for HTML support on a channel.
Definition: channel.c:5902
int ast_audiohook_write_list_empty(struct ast_audiohook_list *audiohook_list)
determines if a audiohook_list is empty or not.
Definition: audiohook.c:744
#define ao2_iterator_next(arg1)
Definition: astobj2.h:1126
#define ast_set_flag(p, flag)
Definition: utils.h:70
int ast_check_hangup_locked(struct ast_channel *chan)
Definition: channel.c:820
struct ast_party_number_ies number
Subscriber phone number ies.
Definition: channel.c:8698
int ast_framehook_list_is_empty(struct ast_framehook_list *framehooks)
Determine if an framehook list is empty or not.
Definition: framehook.c:172
struct ast_channel * __ast_request_and_dial(const char *type, format_t format, const struct ast_channel *requestor, void *data, int timeout, int *reason, 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:5456
static int(* ast_moh_start_ptr)(struct ast_channel *, const char *, const char *)
Definition: channel.c:8019
descriptor for a cli entry.
Definition: cli.h:165
const int argc
Definition: cli.h:154
#define LOG_WARNING
Definition: logger.h:144
ast_timer_event
Definition: timing.h:57
int ast_cel_report_event(struct ast_channel *chan, enum ast_cel_event_type event_type, const char *userdefevname, const char *extra, struct ast_channel *peer2)
Report a channel event.
Definition: cel.c:645
unsigned int fout
Definition: channel.h:847
Audiohooks Architecture.
const char * blockproc
Definition: channel.h:753
struct ast_variable * ast_variable_browse(const struct ast_config *config, const char *category)
Goes through variables.
Definition: config.c:597
static struct aji_capabilities * capabilities
Definition: res_jabber.c:393
#define ast_data_search_cmp_structure(search, structure_name, structure, structure_name_cmp)
Definition: data.h:420
int ast_party_id_presentation(const struct ast_party_id *id)
Determine the overall presentation value for the given party.
Definition: channel.c:2151
#define ao2_callback(c, flags, cb_fn, arg)
Definition: astobj2.h:910
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:9085
static struct ast_frame * __ast_read(struct ast_channel *chan, int dropaudio)
Definition: channel.c:3836
static struct ast_datastore_info plc_ds_info
Definition: channel.c:4832
struct ast_timer * timer
Definition: channel.h:866
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:2295
int plan
Type-Of-Number and Numbering-Plan ie.
Definition: channel.c:8549
static int set_format(struct ast_channel *chan, format_t fmt, format_t *rawformat, format_t *format, struct ast_trans_pvt **trans, const int direction)
Definition: channel.c:5218
#define AST_FRAME_DTMF
Definition: frame.h:128
static void * tonepair_alloc(struct ast_channel *chan, void *params)
Definition: channel.c:7869
#define AST_MIN_DTMF_GAP
Definition: channel.c:118
#define AST_RWLIST_UNLOCK(head)
Attempts to unlock a read/write based list.
Definition: linkedlists.h:150
int ast_cdr_setaccount(struct ast_channel *chan, const char *account)
Set account code, will generate AMI event.
Definition: cdr.c:990
void ast_begin_shutdown(int hangup)
Definition: channel.c:838
int ast_say_digits_full(struct ast_channel *chan, int num, const char *ints, const char *lang, int audiofd, int ctrlfd)
Definition: channel.c:8433
int ast_jb_get_when_to_wakeup(struct ast_channel *c0, struct ast_channel *c1, int time_left)
Calculates the time, left to the closest delivery moment in a bridge.
Definition: abstract_jb.c:266
static int ast_add_fd(struct pollfd *pfd, int fd)
if fd is a valid descriptor, set *pfd with the descriptor
Definition: channel.h:2390
This entries are for multiple registers.
Definition: data.h:253
#define AST_CAUSE_FACILITY_REJECTED
Definition: causes.h:116
#define AST_CAUSE_ACCESS_INFO_DISCARDED
Definition: causes.h:123
static void destroy_hooks(struct ast_channel *chan)
Definition: channel.c:2793
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:3562
#define AST_CAUSE_RECOVERY_ON_TIMER_EXPIRE
Definition: causes.h:142
struct ast_datastore_info * info
Definition: datastore.h:57
Data retrieval API.
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:2095
Structure for variables, used for configurations and for channel variables.
Definition: config.h:75
int ast_channel_datastore_free(struct ast_datastore *datastore)
Free a channel data store object.
Definition: channel.c:2568
struct ast_generator * generator
Definition: channel.h:747
unsigned int emulate_dtmf_duration
Definition: channel.h:857
unsigned long global_fin
Definition: channel.c:104
#define var
Definition: ast_expr2f.c:606
void ast_set_variables(struct ast_channel *chan, struct ast_variable *vars)
adds a list of channel variables to a channel
Definition: channel.c:8261
int ast_app_group_discard(struct ast_channel *chan)
Discard all group counting for a channel.
Definition: app.c:1348
static struct ast_channel * ast_channel_get_full(const char *name, size_t name_len, const char *exten, const char *context)
Definition: channel.c:1761
#define AST_CAUSE_MISDIALLED_TRUNK_PREFIX
Definition: causes.h:100
int(* cc_callback)(struct ast_channel *inbound, const char *dest, ast_cc_callback_fn callback)
Call a function with cc parameters as a function parameter.
Definition: channel.h:635
globally accessible channel datastores
format_t rawwriteformat
Definition: channel.h:856
int ast_tvzero(const struct timeval t)
Returns true if the argument is 0,0.
Definition: time.h:100
uint64_t flags
Definition: app_dial.c:667
char uniqueid[150]
Definition: cdr.h:121
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:7079
#define AST_LIST_NEXT(elm, field)
Returns the next entry in the list after the given entry.
Definition: linkedlists.h:438
Test Framework API.
struct ast_frame * ast_read(struct ast_channel *chan)
Reads a frame.
Definition: channel.c:4383
struct ast_frame *(*const read)(struct ast_channel *chan)
Read a frame, in standard format (see frame.h)
Definition: channel.h:551
struct ast_party_redirecting redirecting
Redirecting/Diversion information.
Definition: channel.h:814
int ast_say_digit_str(struct ast_channel *chan, const char *num, const char *ints, const char *lang)
says digits of a string
Definition: channel.c:8415
static void silence_generator_release(struct ast_channel *chan, void *data)
Definition: channel.c:8275
channel birth
Definition: cel.h:50
#define EVENT_FLAG_CALL
Definition: manager.h:72
Definition: cli.h:146
int char_set
Character set the name is using.
Definition: channel.h:222
void ast_timer_close(struct ast_timer *handle)
Close an opened timing handle.
Definition: timing.c:150
#define DATA_EXPORT_CHANNEL(MEMBER)
Definition: channel.c:164
#define AST_OPTION_DEVICE_NAME
Definition: frame.h:516
unsigned long outsmpl
Definition: channel.h:827
void *(* duplicate)(void *data)
Definition: datastore.h:33
static int kill_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
Definition: channel.c:647
Structure for a data store type.
Definition: datastore.h:31
ast_channel_state
ast_channel states
Definition: channelstate.h:35
#define EVENT_FLAG_DTMF
Definition: manager.h:79
struct ast_timer * ast_timer_open(void)
Open a timer.
Definition: timing.c:123
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:6291
char * str
Subscriber name (Malloced)
Definition: channel.h:214
int ast_say_digits(struct ast_channel *chan, int num, const char *ints, const char *lang)
says digits
Definition: channel.c:8409
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:4447
unsigned long global_fout
Definition: channel.c:104
struct timeval nexteventts
Definition: channel.h:978
static void * channel_cc_params_copy(void *data)
Definition: channel.c:9708
struct ast_data * ast_data_add_uint(struct ast_data *root, const char *childname, unsigned int value)
Add an unsigned integer node type.
Definition: data.c:2332
void ast_party_caller_free(struct ast_party_caller *doomed)
Destroy the caller party contents.
Definition: channel.c:2302
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:900
unsigned char valid
TRUE if the subaddress information is valid/present.
Definition: channel.h:278
int(*const send_digit_end)(struct ast_channel *chan, char digit, unsigned int duration)
Stop sending a literal DTMF digit.
Definition: channel.h:535
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:8570
format_t ast_best_codec(format_t fmts)
Pick the best audio codec.
Definition: channel.c:1062
int(* stop)(struct ast_channel *chan, int need_lock)
Definition: monitor.h:50
int plc_fillin(plc_state_t *s, int16_t amp[], int len)
Fill-in a block of missing audio samples.
Definition: plc.c:175
struct timeval ast_tvnow(void)
Returns current timeval. Meant to replace calls to gettimeofday().
Definition: time.h:142
static force_inline int ast_format_rate(format_t format)
Get the sample rate for a given format.
Definition: frame.h:809
static int uniqueint
Definition: channel.c:101
static int should_skip_dtmf(struct ast_channel *chan)
Determine whether or not we should ignore DTMF in the readq.
Definition: channel.c:3796
int ani2
Automatic Number Identification 2 (Info Digits)
Definition: channel.h:417
struct ast_data * ast_data_add_bool(struct ast_data *root, const char *childname, unsigned int boolean)
Add a boolean node type.
Definition: data.c:2344
int presentation
presentation-indicator ie
Definition: channel.c:8551
#define AST_LIST_EMPTY(head)
Checks whether the specified list contains any entries.
Definition: linkedlists.h:449
#define ast_assert(a)
Definition: utils.h:738
struct ast_cdr * cdr
Definition: channel.h:766
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:328
#define ast_opt_transmit_silence
Definition: options.h:120
#define ao2_unlock(a)
Definition: astobj2.h:497
unsigned short transfercapability
Definition: channel.h:863
int ast_channel_register(const struct ast_channel_tech *tech)
Register a channel technology (a new channel driver) Called by a channel module to register the kind ...
Definition: channel.c:907
struct ao2_iterator ao2_iterator_init(struct ao2_container *c, int flags)
Create an iterator for a container.
Definition: astobj2.c:818
#define ast_copy_flags(dest, src, flagz)
Definition: utils.h:84
char * text
Definition: app_queue.c:1091
format_t nativeformats
Definition: channel.h:852
int64_t ast_tvdiff_ms(struct timeval end, struct timeval start)
Computes the difference (in milliseconds) between two struct timeval instances.
Definition: time.h:90
void ast_party_id_free(struct ast_party_id *doomed)
Destroy the party id contents.
Definition: channel.c:2141
Structure for a data store object.
Definition: datastore.h:54
void ast_party_connected_line_free(struct ast_party_connected_line *doomed)
Destroy the connected line information contents.
Definition: channel.c:2353
int alertpipe[2]
Definition: channel.h:851
struct ast_frame * ast_frisolate(struct ast_frame *fr)
Makes a frame independent of any static storage.
Definition: frame.c:391
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:2604
int(*const queryoption)(struct ast_channel *chan, int option, void *data, int *datalen)
Query a given option. Called with chan locked.
Definition: channel.h:585
static void update_bridge_vars(struct ast_channel *c0, struct ast_channel *c1)
Definition: channel.c:7441
const char * str
Definition: app_jack.c:144
int ast_channel_connected_line_macro(struct ast_channel *autoservice_chan, struct ast_channel *macro_chan, const void *connected_info, int caller, int frame)
Run a connected line interception macro and update a channel&#39;s connected line information.
Definition: channel.c:9618
Generic File Format Support. Should be included by clients of the file handling routines. File service providers should instead include mod_format.h.
static void send_dtmf_event(struct ast_channel *chan, const char *direction, const char digit, const char *begin, const char *end)
Definition: channel.c:3709
int ast_prod(struct ast_channel *chan)
Send empty audio to prime a channel driver.
Definition: channel.c:4784
const char * ast_state2str(enum ast_channel_state)
Gives the string form of a given channel state.
Definition: channel.c:1007
format_t codec
Definition: frame.h:137
struct ast_datastore *attribute_malloc ast_channel_datastore_alloc(const struct ast_datastore_info *info, const char *uid)
Create a channel data store object.
Definition: channel.c:2563
#define AST_OPTION_MAKE_COMPATIBLE
Definition: frame.h:504
int ast_senddigit_begin(struct ast_channel *chan, char digit)
Send a DTMF digit to a channel.
Definition: channel.c:4705
void ast_playtones_stop(struct ast_channel *chan)
Stop playing tones on a channel.
Definition: indications.c:411
char * str
Malloced subaddress string.
Definition: channel.h:263
const char * data
Definition: channel.h:755
struct ao2_iterator simple_iterator
Definition: channel.c:1642
#define AST_CAUSE_NO_USER_RESPONSE
Definition: causes.h:107
char * ast_cdr_flags2str(int flags)
Definition: cdr.c:977
Definitions to aid in the use of thread local storage.
int ast_senddigit(struct ast_channel *chan, char digit, unsigned int duration)
Send a DTMF digit to a channel.
Definition: channel.c:4774
#define AST_CAUSE_NORMAL_CIRCUIT_CONGESTION
Definition: causes.h:119
int value
Definition: syslog.c:39
void ast_cli(int fd, const char *fmt,...)
Definition: cli.c:105
#define ast_cc_config_params_init()
Allocate and initialize an ast_cc_config_params structure.
Definition: ccss.h:135
int ast_default_amaflags
Definition: cdr.c:59
#define AST_LIST_REMOVE(head, elm, field)
Removes a specific entry from a list.
Definition: linkedlists.h:841
static struct ast_frame * kill_exception(struct ast_channel *chan)
Definition: channel.c:635
const ast_string_field linkedid
Definition: channel.h:787
#define LOG_DEBUG
Definition: logger.h:122
#define AST_DATA_ENTRY(__path, __handler)
Definition: data.h:260
int ast_channel_setoption(struct ast_channel *channel, int option, void *data, int datalen, int block)
Sets an option on a channel.
Definition: channel.c:7795
int(*const fixup)(struct ast_channel *oldchan, struct ast_channel *newchan)
Fix up a channel: If a channel is consumed, this is called. Basically update any -&gt;owner links...
Definition: channel.h:579
#define AST_CAUSE_INVALID_NUMBER_FORMAT
Definition: causes.h:115
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:155
enum ast_channel_adsicpe adsicpe
Definition: channel.h:844
#define AST_HTML_URL
Definition: frame.h:222
format_t rawreadformat
Definition: channel.h:855
void ast_moh_stop(struct ast_channel *chan)
Turn off music on hold on a given channel.
Definition: channel.c:8051
#define AST_LIST_TRAVERSE_SAFE_END
Closes a safe loop traversal block.
Definition: linkedlists.h:600
#define AST_PRES_ALLOWED_USER_NUMBER_NOT_SCREENED
Definition: callerid.h:329
static void xfer_ds_destroy(void *data)
Definition: channel.c:6170
static char cid_num[AST_MAX_EXTENSION]
Definition: chan_mgcp.c:157
struct ast_channel * ast_waitfor_nandfds(struct ast_channel **chan, int n, int *fds, int nfds, int *exception, int *outfd, int *ms)
Waits for activity on a group of channels.
Definition: channel.c:3188
static struct ast_data_handler channeltypes_provider
Definition: channel.c:8201
static void(* ast_moh_cleanup_ptr)(struct ast_channel *)
Definition: channel.c:8021
void ast_var_delete(struct ast_var_t *var)
Definition: chanvars.c:63
unsigned char odd_even_indicator
TRUE if odd number of address signals.
Definition: channel.h:276
#define ast_verb(level,...)
Definition: logger.h:243
struct ast_party_name_ies name
Subscriber name ies.
Definition: channel.c:8696
void ast_config_destroy(struct ast_config *config)
Destroys a config.
Definition: config.c:1037
static const char * oldest_linkedid(const char *a, const char *b)
Definition: channel.c:6320
Indicate what information in ast_party_caller should be set.
Definition: channel.h:387
void ast_uninstall_music_functions(void)
Definition: channel.c:8032
#define AST_ALERT_FD
Definition: channel.h:163
int ast_atomic_fetchadd_int(volatile int *p, int v)
Atomically add v to *p and return * the previous value of *p. This can be used to handle reference co...
Definition: lock.h:603
int ast_datastore_free(struct ast_datastore *datastore)
Free a data store object.
Definition: datastore.c:65
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 char *linkedid, const int amaflag, const char *name_fmt,...)
Definition: channel.c:9825
struct ast_variable * vars
Definition: channel.h:1005
format_t origwfmt
Definition: channel.c:7851
the list of registered channel types
Definition: channel.c:143
#define AST_FORMAT_G729A
Definition: frame.h:258
const char * pbx_builtin_getvar_helper(struct ast_channel *chan, const char *name)
Return a pointer to the value of the corresponding channel variable.
Definition: pbx.c:10475
Header for providers of file and format handling routines. Clients of these routines should include &quot;...
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:9754
int(*const answer)(struct ast_channel *chan)
Answer the channel.
Definition: channel.h:548
char defaultlanguage[]
Definition: asterisk.c:227
struct ast_frame dtmff
Definition: channel.h:816
const char * uid
Definition: datastore.h:55
#define AST_FORMAT_LPC10
Definition: frame.h:256
static int frames
Definition: iax2-parser.c:49
String fields in structures.
Utility functions.
char * ast_print_group(char *buf, int buflen, ast_group_t group)
print call- and pickup groups into buffer
Definition: channel.c:8236
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:2276
int ast_queue_hangup_with_cause(struct ast_channel *chan, int cause)
Queue a hangup frame with hangupcause set.
Definition: channel.c:1581
#define AST_PRES_RESTRICTED
Definition: callerid.h:325
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:1808
const char * start_sound
Definition: channel.h:986
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:2249
static int set_security_requirements(const struct ast_channel *requestor, struct ast_channel *out)
Definition: channel.c:5655
static struct ast_threadstorage state2str_threadbuf
Definition: channel.c:106
int ast_cdr_data_add_structure(struct ast_data *tree, struct ast_cdr *cdr, int recur)
Definition: cdr.c:1674
#define ast_manager_event(chan, category, event, contents,...)
Definition: manager.h:221
struct ast_set_party_id from
Definition: channel.h:467
struct ast_channel_tech * tech
Definition: channel.c:122
int ast_channel_defer_dtmf(struct ast_channel *chan)
Defers DTMF so that you only read things like hangups and audio.
Definition: channel.c:1616
#define M_PI
const char * ast_config_AST_SYSTEM_NAME
Definition: asterisk.c:273
#define CONFIG_STATUS_FILEMISSING
Definition: config.h:50
static enum ast_bridge_result ast_generic_bridge(struct ast_channel *c0, struct ast_channel *c1, struct ast_bridge_config *config, struct ast_frame **fo, struct ast_channel **rc)
Definition: channel.c:7203
static int ast_channel_hash_cb(const void *obj, const int flags)
Definition: channel.c:8063
Number structure.
Definition: app_followme.c:109
struct chanlist * next
Definition: app_dial.c:665
static void plc_ds_destroy(void *data)
Definition: channel.c:4825
#define attribute_const
Definition: compiler.h:41
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:2118
ast_control_transfer
Definition: frame.h:422
unsigned int flags
Definition: channel.h:850
struct ast_trans_pvt * ast_translator_build_path(format_t dest, format_t source)
Builds a translator path Build a path (possibly NULL) from source to dest.
Definition: translate.c:282
#define AST_OPTION_FORMAT_READ
Definition: frame.h:494
static int deactivate_silence_generator(struct ast_channel *chan)
Definition: channel.c:8332
static void manager_bridge_event(int onoff, int type, struct ast_channel *c0, struct ast_channel *c1)
Send manager event for bridge link and unlink events.
Definition: channel.c:7421
format_t format
Definition: mod_format.h:47
struct ast_party_id id
Caller party ID.
Definition: channel.h:370
int ast_cdr_setcid(struct ast_cdr *cdr, struct ast_channel *chan)
Initialize based on a channel.
Definition: cdr.c:883
int ast_channel_bridge(struct ast_channel *c0, struct ast_channel *c1, struct ast_bridge_config *config, struct ast_frame **fo, struct ast_channel **rc)
Bridge two channels together.
Definition: channel.c:7506
#define AST_OPTION_SECURE_MEDIA
Definition: frame.h:525
int ast_set_write_format(struct ast_channel *chan, format_t format)
Sets write format on channel chan Set write format for channel to whichever component of &quot;format&quot; is ...
Definition: channel.c:5307
#define AST_CAUSE_RESPONSE_TO_STATUS_ENQUIRY
Definition: causes.h:117
int duration
Definition: channel.c:7838
void ast_change_name(struct ast_channel *chan, const char *newname)
Change channel name.
Definition: channel.c:6229
int ast_channel_datastore_inherit(struct ast_channel *from, struct ast_channel *to)
Inherit datastores from a parent to a child.
Definition: channel.c:2573
#define AST_RWLIST_RDLOCK(head)
Read locks a list.
Definition: linkedlists.h:77
void ast_moh_cleanup(struct ast_channel *chan)
Definition: channel.c:8057
#define AST_RWLIST_INSERT_HEAD
Definition: linkedlists.h:703
a masquerade happened to alter the participants on a bridge
Definition: cel.h:94
static int kill_hangup(struct ast_channel *chan)
Definition: channel.c:653
int ast_set_read_format(struct ast_channel *chan, format_t format)
Sets read format on channel chan Set read format for channel to whichever component of &quot;format&quot; is be...
Definition: channel.c:5301
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:236
int(*const setoption)(struct ast_channel *chan, int option, void *data, int datalen)
Set a given option. Called with chan locked.
Definition: channel.h:582
const char * exten
Definition: channel.h:1000
format_t oldwriteformat
Definition: channel.h:837
int ast_channel_data_add_structure(struct ast_data *tree, struct ast_channel *chan, int add_bridged)
Insert into an astdata tree, the channel structure.
Definition: channel.c:357
short data[4000]
Definition: channel.c:7857
#define AST_CAUSE_NO_ROUTE_TRANSIT_NET
Definition: causes.h:98
#define MAX(a, b)
Definition: utils.h:228
#define AST_DATA_HANDLER_VERSION
The Data API structures version.
Definition: data.h:204
struct ast_frame f
Definition: channel.c:7855
int16_t * samples_buf
Definition: channel.c:4817
const char * value
Definition: config.h:79
const char * cid_name
Definition: channel.h:1003
void * __ao2_alloc_debug(const size_t data_size, ao2_destructor_fn destructor_fn, char *tag, const char *file, int line, const char *funcname, int ref_debug)
Definition: astobj2.c:335
static int ast_channel_softhangup_cb(void *obj, void *arg, int flags)
Definition: channel.c:829
unsigned int flags
Definition: channel.h:987
#define ao2_callback_data(arg1, arg2, arg3, arg4, arg5)
Definition: astobj2.h:943
int ast_waitfor_n_fd(int *fds, int n, int *ms, int *exception)
Waits for input on an fd.
Definition: channel.c:3176
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:9098
struct ast_party_id ani
Automatic Number Identification (ANI)
Definition: channel.h:377
struct ast_channel * ast_call_forward(struct ast_channel *caller, struct ast_channel *orig, int *timeout, format_t format, 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:5386
General Asterisk PBX channel definitions.
#define AST_PRES_USER_NUMBER_UNSCREENED
Definition: callerid.h:318
#define ast_data_register_multiple_core(data_entries, entries)
Definition: data.h:379
int(* set_base_channel)(struct ast_channel *chan, struct ast_channel *base)
Set base channel (agent and local)
Definition: channel.h:609
struct ast_party_dialed::@155 number
Dialed/Called number.
#define AST_FRIENDLY_OFFSET
Offset into a frame&#39;s data buffer.
Definition: frame.h:204
int ast_raw_answer(struct ast_channel *chan, int cdr_answer)
Answer a channel.
Definition: channel.c:2930
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:2769
Asterisk file paths, configured in asterisk.conf.
int(*const hangup)(struct ast_channel *chan)
Hangup (and possibly destroy) the channel.
Definition: channel.h:545
void ast_channel_clear_softhangup(struct ast_channel *chan, int flag)
Clear a set of softhangup flags from a channel.
Definition: channel.c:2707
const char * src
Definition: frame.h:158
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:562
struct ast_channel * chan
Definition: app_dial.c:666
void ast_channel_undefer_dtmf(struct ast_channel *chan)
Unset defer DTMF flag on channel.
Definition: channel.c:1628
struct timeval start_time
Definition: channel.h:977
int ast_write_video(struct ast_channel *chan, struct ast_frame *frame)
Write video frame to a channel This function writes the given frame to the indicated channel...
Definition: channel.c:4801
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)
Definition: say.h:105
static struct ast_generator gen
const int fd
Definition: cli.h:153
#define AST_FORMAT_ALAW
Definition: frame.h:248
#define ast_string_field_init(x, size)
Initialize a field pool and fields.
Definition: stringfields.h:249
void ast_party_subaddress_free(struct ast_party_subaddress *doomed)
Destroy the party subaddress contents.
Definition: channel.c:2081
#define AST_TRANS_CAP_DIGITAL_W_TONES
Definition: transcap.h:39
#define ast_config_load(filename, flags)
Load a config file.
Definition: config.h:170
const char * account
Definition: channel.h:1004
#define AST_FORMAT_SPEEX
Definition: frame.h:260
unsigned int enabled
Definition: devicestate.c:205
int ast_senddigit_end(struct ast_channel *chan, char digit, unsigned int duration)
Send a DTMF digit to a channel.
Definition: channel.c:4755
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:63
unsigned char subaddress
Definition: channel.h:317
static void handle_cause(int cause, int *outstate)
Definition: channel.c:5336
const int n
Definition: cli.h:159
int ast_timer_disable_continuous(const struct ast_timer *handle)
Disable continuous mode.
Definition: timing.c:190
int ast_channel_data_cmp_structure(const struct ast_data_search *tree, struct ast_channel *chan, const char *structure_name)
Compare to channel structures using the data api.
Definition: channel.c:489
struct ast_audiohook_list * audiohooks
Definition: channel.h:764
#define ast_poll(a, b, c)
Definition: poll-compat.h:88
#define AST_CAUSE_ANSWERED_ELSEWHERE
Definition: causes.h:113
#define AST_TRANS_CAP_3_1K_AUDIO
Definition: transcap.h:37
Asterisk internal frame definitions.
struct ast_data * ast_data_add_node(struct ast_data *root, const char *childname)
Add a container child.
Definition: data.c:2317
ast_cond_t cond
Definition: app_meetme.c:963
#define AST_FORMAT_TEXT_MASK
Definition: frame.h:297
#define AST_MAX_EXTENSION
Definition: channel.h:135
#define AST_RWLIST_TRAVERSE
Definition: linkedlists.h:493
int datalen
Definition: frame.h:148
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:3557
#define AST_CAUSE_NORMAL_CLEARING
Definition: causes.h:105
Scheduler Routines (derived from cheops)
#define AST_CAUSE_CHAN_NOT_IMPLEMENTED
Definition: causes.h:131
void ast_cdr_answer(struct ast_cdr *cdr)
Answer a call.
Definition: cdr.c:737
#define ast_data_unregister(path)
Definition: data.h:394
char ast_default_accountcode[AST_MAX_ACCOUNT_CODE]
Definition: cdr.c:60
void ast_party_number_init(struct ast_party_number *init)
Initialize the given number structure.
Definition: channel.c:1981
bridge configuration
Definition: channel.h:974
#define AST_FORMAT_G726
Definition: frame.h:264
Caller Party information.
Definition: channel.h:368
#define AST_LIST_REMOVE_CURRENT(field)
Removes the current entry from a list during a traversal.
Definition: linkedlists.h:554
int(*const write_text)(struct ast_channel *chan, struct ast_frame *frame)
Write a text frame, in standard format.
Definition: channel.h:594
const char * end_sound
Definition: channel.h:985
#define AST_FORMAT_G726_AAL2
Definition: frame.h:250
#define ao2_ref(o, delta)
Definition: astobj2.h:472
int ast_shutting_down(void)
Returns non-zero if Asterisk is being shut down.
Definition: channel.c:865
#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:83
int ast_softhangup(struct ast_channel *chan, int reason)
Softly hangup up a channel.
Definition: channel.c:2746
void ast_jb_destroy(struct ast_channel *chan)
Destroys jitterbuffer on a channel.
Definition: abstract_jb.c:536
int ast_register_atexit(void(*func)(void))
Register a function to be executed before Asterisk exits.
Definition: asterisk.c:998
#define ao2_lock(a)
Definition: astobj2.h:488
struct timeval ast_samp2tv(unsigned int _nsamp, unsigned int _rate)
Returns a timeval corresponding to the duration of n samples at rate r. Useful to convert samples to ...
Definition: time.h:191
const ast_string_field parkinglot
Definition: channel.h:787
struct ast_format * fmt
Definition: mod_format.h:102
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:226
static int silence_generator_generate(struct ast_channel *chan, void *data, int len, int samples)
Definition: channel.c:8280
static struct causes_map causes[]
Definition: channel.c:209
List of channel drivers.
Definition: app_dial.c:664
int ast_internal_timing_enabled(struct ast_channel *chan)
Check if the channel can run in internal timing mode.
Definition: channel.c:4378
#define AST_BRIDGE_DTMF_CHANNEL_1
Report DTMF on channel 1.
Definition: channel.h:1934
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:9793
#define AST_GENERATOR_FD
Definition: channel.h:166
static int ast_channel_cmp_cb(void *obj, void *arg, int flags)
Definition: channel.c:1720
int ast_plc_reload(void)
Reload genericplc configuration value from codecs.conf.
Definition: channel.c:8076
char * value
Definition: chanvars.h:30
struct sched_context * sched
Definition: channel.h:756
struct ast_set_party_id id
Definition: channel.h:389
map AST_CAUSE&#39;s to readable string representations
Definition: channel.c:203
struct ast_frame *(*const exception)(struct ast_channel *chan)
Handle an exception, reading a frame.
Definition: channel.h:566
const char *(* get_pvt_uniqueid)(struct ast_channel *chan)
Get the unique identifier for the PVT, i.e. SIP call-ID for SIP.
Definition: channel.h:612
A set of macros to manage forward-linked lists.
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:9111
static char language[MAX_LANGUAGE]
Definition: chan_alsa.c:108
int streamid
Definition: channel.h:835
const char * name
Definition: config.h:77
int ast_data_add_codecs(struct ast_data *root, const char *node_name, format_t capability)
Add the list of codecs in the root node based on the capability parameter.
Definition: data.c:3112
int(* timingfunc)(const void *data)
Definition: channel.h:759
hangup terminates connection
Definition: cel.h:54
void ast_channel_set_linkgroup(struct ast_channel *chan, struct ast_channel *peer)
propagate the linked id between chan and peer
Definition: channel.c:6387
char name[0]
Definition: chanvars.h:31
struct ast_channel * _bridge
Definition: channel.h:748
static struct ast_generator silence_generator
Definition: channel.c:8299
ast_group_t ast_get_group(const char *s)
Definition: channel.c:7987
#define AST_LIST_REMOVE_HEAD(head, field)
Removes and returns the head entry from a list.
Definition: linkedlists.h:818
#define AST_CAUSE_NO_ANSWER
Definition: causes.h:108
#define ast_opt_generic_plc
Definition: options.h:132
int ast_cel_linkedid_ref(const char *linkedid)
Inform CEL that a new linkedid is being used.
Definition: cel.c:597
char sending_dtmf_digit
Definition: channel.h:873
struct ast_party_id ani
Automatic Number Identification (ANI)
Definition: channel.h:410
int odd_even_indicator
odd/even indicator ie
Definition: channel.c:8625
static void apply_plc(struct ast_channel *chan, struct ast_frame *frame)
Definition: channel.c:4890
#define AST_FORMAT_SLINEAR16
Definition: frame.h:272
#define AST_CAUSE_NUMBER_PORTED_NOT_HERE
Definition: causes.h:104
AST_REDIRECTING_REASON
redirecting reason codes.
Definition: callerid.h:390
#define AST_CAUSE_FACILITY_NOT_IMPLEMENTED
Definition: causes.h:132
#define AST_CAUSE_NOTDEFINED
Definition: causes.h:154
Structure to describe a channel &quot;technology&quot;, ie a channel driver See for examples: ...
Definition: channel.h:507
Core PBX routines and definitions.
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:9731
int ast_queue_frame(struct ast_channel *chan, struct ast_frame *f)
Queue one or more frames to a channel&#39;s frame queue.
Definition: channel.c:1558
int ast_cdr_update(struct ast_channel *chan)
Update CDR on a channel.
Definition: cdr.c:1083
int ast_autoservice_stop(struct ast_channel *chan)
Stop servicing a channel for us...
Definition: autoservice.c:238
void ast_party_number_free(struct ast_party_number *doomed)
Destroy the party number contents.
Definition: channel.c:2028
int ast_check_hangup(struct ast_channel *chan)
Check to see if a channel is needing hang up.
Definition: channel.c:806
struct ast_party_connected_line connected
Definition: app_dial.c:669
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:159
#define AST_RWLIST_TRAVERSE_SAFE_BEGIN
Definition: linkedlists.h:542
struct ast_flags features_caller
Definition: channel.h:975
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:2056
struct ast_datastore_info secure_call_info
#define AST_CAUSE_DESTINATION_OUT_OF_ORDER
Definition: causes.h:114
#define ast_data_add_structure(structure_name, root, structure)
Definition: data.h:620
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:1634
The list of nodes with their search requirement.
Definition: data.c:122
static void report_new_callerid(struct ast_channel *chan)
Definition: channel.c:6468
#define ast_test_suite_event_notify(s, f,...)
Definition: test.h:184
const char *const * argv
Definition: cli.h:155
enum ast_bridge_result(*const bridge)(struct ast_channel *c0, struct ast_channel *c1, int flags, struct ast_frame **fo, struct ast_channel **rc, int timeoutms)
Bridge two channels of the same type together.
Definition: channel.h:569
int fds[AST_MAX_FDS]
Definition: channel.h:829
struct ast_party_dialed dialed
Dialed/Called information.
Definition: channel.h:797
struct ast_silence_generator * ast_channel_start_silence_generator(struct ast_channel *chan)
Starts a silence generator on the given channel.
Definition: channel.c:8309
struct ast_trans_pvt * writetrans
Definition: channel.h:762
The AMI - Asterisk Manager Interface - is a TCP protocol created to manage Asterisk with third-party ...
#define AST_LIST_HEAD_NOLOCK(name, type)
Defines a structure to be used to hold a list of specified type (with no lock).
Definition: linkedlists.h:224
int tag
User party id tag ie.
Definition: channel.c:8702
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: utils.h:663
static void bridge_playfile(struct ast_channel *chan, struct ast_channel *peer, const char *sound, int remain)
Definition: channel.c:7169
struct ast_party_subaddress subaddress
Subscriber subaddress.
Definition: channel.h:294
&quot;smart&quot; channels that update automatically if a channel is masqueraded
Dialed/Called Party information.
Definition: channel.h:328
static void ast_set_owners_and_peers(struct ast_channel *chan1, struct ast_channel *chan2)
Definition: channel.c:6430
int ast_tone_zone_data_add_structure(struct ast_data *tree, struct ast_tone_zone *zone)
Add a tone_zone structure to the data tree specified.
Definition: indications.c:1127
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:8886
static void masquerade_colp_transfer(struct ast_channel *transferee, struct xfer_masquerade_ds *colp)
Definition: channel.c:6498
Responsible for call detail data.
Definition: cdr.h:82
#define AST_CAUSE_FACILITY_NOT_SUBSCRIBED
Definition: causes.h:125
int ast_timer_ack(const struct ast_timer *handle, unsigned int quantity)
Acknowledge a timer event.
Definition: timing.c:172
Indicate what information in ast_party_id should be set.
Definition: channel.h:311
ast_control_frame_type
Internal control frame subtype field values.
Definition: frame.h:319
char * ast_getformatname(format_t format)
Get the name of a format.
Definition: frame.c:578
struct ast_channel_tech ast_kill_tech
Kill the channel channel driver technology descriptor.
Definition: channel.c:668
void ast_cdr_start(struct ast_cdr *cdr)
Start a call.
Definition: cdr.c:727
#define AST_CAUSE_REDIRECTED_TO_NEW_DESTINATION
Definition: causes.h:112
void ast_party_dialed_init(struct ast_party_dialed *init)
Initialize the given dialed structure.
Definition: channel.c:2219
static int shutting_down
Prevent new channel allocation if shutting down.
Definition: channel.c:99
#define LOG_ERROR
Definition: logger.h:155
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:120
#define AST_LIST_INSERT_TAIL(head, elm, field)
Appends a list entry to the tail of a list.
Definition: linkedlists.h:716
int ast_channel_transfer_masquerade(struct ast_channel *target_chan, const struct ast_party_connected_line *target_id, int target_held, struct ast_channel *transferee_chan, const struct ast_party_connected_line *transferee_id, int transferee_held)
Setup a masquerade to transfer a call.
Definition: channel.c:6184
#define AST_FORMAT_SIREN7
Definition: frame.h:268
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:762
#define ast_opt_transcode_via_slin
Definition: options.h:114
int attribute_pure ast_true(const char *val)
Make sure something is true. Determine if a string containing a boolean value is &quot;true&quot;. This function checks to see whether a string passed to it is an indication of an &quot;true&quot; value. It checks to see if the string is &quot;yes&quot;, &quot;true&quot;, &quot;y&quot;, &quot;t&quot;, &quot;on&quot; or &quot;1&quot;.
Definition: utils.c:1533
void sched_context_destroy(struct sched_context *c)
destroys a schedule context Destroys (free&#39;s) the given sched_context structure
Definition: sched.c:267
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:2288
The descriptor of a dynamic string XXX storage will be optimized later if needed We use the ts field ...
Definition: strings.h:364
int ast_cc_offer(struct ast_channel *caller_chan)
Offer CC to a caller.
Definition: ccss.c:3485
static struct ast_generator generator
Definition: app_fax.c:361
Default structure for translators, with the basic fields and buffers, all allocated as part of the sa...
Definition: translate.h:135
#define AST_CAUSE_CALL_AWARDED_DELIVERED
Definition: causes.h:102
int64_t format_t
Definition: frame_defs.h:32
#define free(a)
Definition: astmm.h:94
unsigned int fin
Definition: channel.h:845
#define CLI_SHOWUSAGE
Definition: cli.h:44
static struct ao2_container * channels
All active channels on the system.
Definition: channel.c:197
void ast_data_remove_node(struct ast_data *root, struct ast_data *child)
Remove a node that was added using ast_data_add_.
Definition: data.c:2486
unsigned char number
Definition: channel.h:315
#define AST_CAUSE_NORMAL_UNSPECIFIED
Definition: causes.h:118
int plan
Q.931 Type-Of-Number and Numbering-Plan encoded fields.
Definition: channel.h:243
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:1958
static struct ast_codec_pref prefs
Definition: chan_iax2.c:258
unsigned int ast_translate_path_steps(format_t dest, format_t src)
Returns the number of steps required to convert from &#39;src&#39; to &#39;dest&#39;.
Definition: translate.c:1059
struct timeval sending_dtmf_tv
Definition: channel.h:874
int ast_remaining_ms(struct timeval start, int max_ms)
Calculate remaining milliseconds given a starting timestamp and upper bound.
Definition: utils.c:1615
#define AST_CAUSE_CHANNEL_UNACCEPTABLE
Definition: causes.h:101
unsigned long insmpl
Definition: channel.h:826
#define AST_PRES_NUMBER_TYPE
Definition: callerid.h:317
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:4653
static void party_connected_line_copy_transfer(struct ast_party_connected_line *dest, const struct ast_party_connected_line *src)
Definition: channel.c:6125
const ast_string_field call_forward
Definition: channel.h:787
#define AST_FORMAT_ULAW
Definition: frame.h:246
void ast_party_name_init(struct ast_party_name *init)
Initialize the given name structure.
Definition: channel.c:1928
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:2337
struct ast_framehook_list * framehooks
Definition: channel.h:765
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
int ani2
Automatic Number Identification 2 (Info Digits)
Definition: channel.h:380
SAY_EXTERN int(* ast_say_character_str_full)(struct ast_channel *chan, const char *num, const char *ints, const char *lang, int audiofd, int ctrlfd) SAY_INIT(ast_say_character_str_full)
Definition: say.h:154
#define AST_TIMING_FD
Definition: channel.h:164
void ast_cdr_detach(struct ast_cdr *cdr)
Detaches the detail record for posting (and freeing) either now or at a later time in bulk with other...
Definition: cdr.c:1328
Description of a tone.
Definition: indications.h:36
enum ast_channel_state _state
Definition: channel.h:839
#define AST_FORMAT_TESTLAW
Definition: frame.h:303
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:473
struct ast_channel * ast_bridged_channel(struct ast_channel *chan)
Find bridged channel.
Definition: channel.c:7160
#define AST_TRANS_CAP_VIDEO
Definition: transcap.h:40
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:1691
struct timeval ast_tvadd(struct timeval a, struct timeval b)
Returns the sum of two timevals a + b.
Definition: utils.c:1587
void(* release)(struct ast_channel *chan, void *data)
Definition: channel.h:181
Connected Line/Party information.
Definition: channel.h:401
const ast_string_field name
Definition: channel.h:787
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:2110
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:8496
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:8040
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:1950
struct sla_ringing_trunk * first
Definition: app_meetme.c:965
const ast_string_field hangupsource
Definition: channel.h:787
struct ast_party_connected_line transferee_id
Definition: channel.c:6154
void ast_party_name_free(struct ast_party_name *doomed)
Destroy the party name contents.
Definition: channel.c:1975
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
#define AST_OPTION_SECURE_SIGNALING
Definition: frame.h:524
void * timingdata
Definition: channel.h:760
int _softhangup
Definition: channel.h:832
#define AST_CAUSE_WRONG_MESSAGE
Definition: causes.h:138
enum ast_frame_read_action action
Definition: frame.h:375
#define FORMAT
#define ao2_alloc(data_size, destructor_fn)
Definition: astobj2.h:430
struct ast_datastore * ast_datastore_alloc(const struct ast_datastore_info *info, const char *uid)
Definition: datastore.c:98
#define AST_PRES_NUMBER_NOT_AVAILABLE
Definition: callerid.h:353
#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:447
#define LOG_NOTICE
Definition: logger.h:133
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:6223
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:180
struct ast_channel *(* get_base_channel)(struct ast_channel *chan)
Retrieve base channel (agent and local)
Definition: channel.h:606
int ast_seekstream(struct ast_filestream *fs, off_t sample_offset, int whence)
Seeks into stream.
Definition: file.c:879
struct ast_filestream * read_stream
Definition: monitor.h:41
int ast_softhangup_nolock(struct ast_channel *chan, int reason)
Softly hangup up a channel (no channel lock)
Definition: channel.c:2733
int ast_channel_sendhtml(struct ast_channel *channel, int subclass, const char *data, int datalen)
Sends HTML on given channel Send HTML or URL on link.
Definition: channel.c:5907
#define AST_MIN_DTMF_DURATION
Definition: channel.c:114
const char * ast_var_full_name(const struct ast_var_t *var)
Definition: chanvars.c:84
struct ast_bridge * bridge
Definition: channel.h:865
#define AST_LIST_TRAVERSE(head, var, field)
Loops over (traverses) the entries in a list.
Definition: linkedlists.h:490
#define AST_LIST_ENTRY(type)
Declare a forward link structure inside a list entry.
Definition: linkedlists.h:409
int fdno
Definition: channel.h:834
#define AST_LIST_INSERT_HEAD(head, elm, field)
Inserts a list entry at the head of a list.
Definition: linkedlists.h:696
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: app.c:294
int ast_channel_sendurl(struct ast_channel *channel, const char *url)
Sends a URL on a given link Send URL on link.
Definition: channel.c:5914
#define AST_FORMAT_VIDEO_MASK
Definition: frame.h:290
int(*const send_html)(struct ast_channel *chan, int subclass, const char *data, int len)
Send HTML data.
Definition: channel.h:563
#define ao2_find(arg1, arg2, arg3)
Definition: astobj2.h:964
#define ast_channel_unlock(chan)
Definition: channel.h:2467
int(*const transfer)(struct ast_channel *chan, const char *newdest)
Blind transfer other side (see app_transfer.c and ast_transfer()
Definition: channel.h:588
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:8355
#define CLI_FAILURE
Definition: cli.h:45
void *(* alloc)(struct ast_channel *chan, void *params)
Definition: channel.h:180
A machine to gather up arbitrary frames and convert them to raw slinear on demand.
channel end
Definition: cel.h:52
int errno
const char * ast_cause2str(int state) attribute_pure
Gives the string form of a given cause code.
Definition: channel.c:980
struct ast_channel::datastores datastores
static const char name[]
#define AST_MAX_CONTEXT
Definition: channel.h:136
struct ast_var_t::@158 entries
int(*const send_text)(struct ast_channel *chan, const char *text)
Display or transmit text.
Definition: channel.h:557
#define SEEK_FORCECUR
Definition: file.h:50
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:6241
int source
Information about the source of an update.
Definition: channel.h:424
#define ast_free(a)
Definition: astmm.h:97
char * command
Definition: cli.h:180
int valid
valid/present ie
Definition: channel.c:8627
#define AST_CAUSE_BEARERCAPABILITY_NOTIMPL
Definition: causes.h:130
static char * complete_channeltypes(struct ast_cli_args *a)
Definition: channel.c:537
char macrocontext[AST_MAX_CONTEXT]
Definition: channel.h:870
void ast_autochan_new_channel(struct ast_channel *old_chan, struct ast_channel *new_chan)
Switch what channel autochans point to.
Definition: autochan.c:83
size_t num_samples
Definition: channel.c:4821
void ast_cancel_shutdown(void)
Cancel a shutdown in progress.
Definition: channel.c:859
struct ast_filestream * vstream
Definition: channel.h:758
int valid
valid/present ie
Definition: channel.c:8553
#define AST_CHANNEL_NAME
Definition: channel.h:137
int str
subaddress ie.
Definition: channel.c:8621
static int __ast_channel_masquerade(struct ast_channel *original, struct ast_channel *clonechan, struct ast_datastore *xfer_ds)
Definition: channel.c:5987
#define AST_FORMAT_AUDIO_MASK
Definition: frame.h:274
struct ast_filestream * write_stream
Definition: monitor.h:42
static int tonepair_generator(struct ast_channel *chan, void *data, int len, int samples)
Definition: channel.c:7898
#define AST_MAX_FDS
Definition: channel.h:158
int ast_stream_and_wait(struct ast_channel *chan, const char *file, const char *digits)
stream file until digit If the file name is non-empty, try to play it.
Definition: file.c:1370
int ast_say_character_str(struct ast_channel *chan, const char *num, const char *ints, const char *lang)
Definition: channel.c:8421
struct ast_channel *attribute_malloc __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 char *linkedid, const int amaflag, const char *file, int line, const char *function, const char *name_fmt,...)
Create a channel structure.
Definition: channel.c:1366
int ast_closestream(struct ast_filestream *f)
Closes a stream.
Definition: file.c:904
#define AST_FORMAT_SIREN14
Definition: frame.h:270
struct ast_channel *(*const bridged_channel)(struct ast_channel *chan, struct ast_channel *bridge)
Find bridged channel.
Definition: channel.h:597
#define AST_CAUSE_NUMBER_CHANGED
Definition: causes.h:111
static struct ast_format f[]
Definition: format_g726.c:181
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:8023
int ast_write(struct ast_channel *chan, struct ast_frame *frame)
Write a frame to a channel This function writes the given frame to the indicated channel.
Definition: channel.c:4916
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:8644
const char * word
Definition: cli.h:157
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:1989
#define FRAMECOUNT_INC(x)
Definition: channel.h:651
struct ao2_iterator * active_iterator
Definition: channel.c:1646
void ast_cdr_setapp(struct ast_cdr *cdr, const char *app, const char *data)
Set the last executed application.
Definition: cdr.c:822
int ast_call(struct ast_channel *chan, char *addr, int timeout)
Make a call.
Definition: channel.c:5761
#define DATASTORE_INHERIT_FOREVER
Definition: channel.h:156
#define AST_CAUSE_MANDATORY_IE_LENGTH_ERROR
Definition: causes.h:143
unsigned int inheritance
Definition: datastore.h:58
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:741
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:8722
int ast_timer_fd(const struct ast_timer *handle)
Get a poll()-able file descriptor for a timer.
Definition: timing.c:158
int ast_timer_set_rate(const struct ast_timer *handle, unsigned int rate)
Set the timing tick rate.
Definition: timing.c:163
static void channels_shutdown(void)
Definition: channel.c:8211
static void tonepair_release(struct ast_channel *chan, void *params)
Definition: channel.c:7860
#define AST_CAUSE_INTERWORKING
Definition: causes.h:145
if(yyss+yystacksize-1<=yyssp)
Definition: ast_expr2.c:1874
struct ast_party_subaddress subaddress
Dialed/Called subaddress.
Definition: channel.h:341
void ast_party_id_init(struct ast_party_id *init)
Initialize the given party id structure.
Definition: channel.c:2087
void ast_channel_setwhentohangup(struct ast_channel *chan, time_t offset)
Set when to hang a channel up.
Definition: channel.c:878
static const char type[]
Definition: chan_nbs.c:57
static int generator_force(const void *data)
Definition: channel.c:3114
int ast_timer_enable_continuous(const struct ast_timer *handle)
Enable continuous mode.
Definition: timing.c:181
const char *const description
Definition: channel.h:509
void ao2_iterator_destroy(struct ao2_iterator *i)
Destroy a container iterator.
Definition: astobj2.c:833
#define ast_string_field_build(x, field, fmt, args...)
Set a field to a complex (built) value.
Definition: stringfields.h:367
Structure used to handle boolean flags.
Definition: utils.h:200
static struct ast_channel_iterator * channel_iterator_search(const char *name, size_t name_len, const char *exten, const char *context)
Definition: channel.c:1657
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:1696
#define ast_clear_flag(p, flag)
Definition: utils.h:77
void ast_channel_set_fd(struct ast_channel *chan, int which, int fd)
Definition: channel.c:2631
static char cid_name[AST_MAX_EXTENSION]
Definition: chan_mgcp.c:158
Indicate what information in ast_party_connected_line should be set.
Definition: channel.h:431
#define CHECK_BLOCKING(c)
Definition: channel.h:2427
int ast_say_phonetic_str(struct ast_channel *chan, const char *num, const char *ints, const char *lang)
Definition: channel.c:8427
const char * usage
Definition: cli.h:171
int(* generate)(struct ast_channel *chan, void *data, int len, int samples)
Definition: channel.h:186
int ast_waitfordigit_full(struct ast_channel *c, int ms, int audiofd, int ctrlfd)
Wait for a digit Same as ast_waitfordigit() with audio fd for outputting read audio and ctrlfd to mon...
Definition: channel.c:3616
const char * desc
Definition: channel.c:206
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...
Definition: pbx.c:10546
int(*const write)(struct ast_channel *chan, struct ast_frame *frame)
Write a frame, in standard format (see frame.h)
Definition: channel.h:554
#define NUM_CHANNEL_BUCKETS
Definition: channel.c:148
#define AST_CAUSE_INVALID_IE_CONTENTS
Definition: causes.h:140
char macroexten[AST_MAX_EXTENSION]
Definition: channel.h:871
#define AST_PRES_RESTRICTION
Definition: callerid.h:323
int ast_waitfordigit(struct ast_channel *c, int ms)
Waits for a digit.
Definition: channel.c:3552
#define attribute_malloc
Definition: compiler.h:59
static void channel_cc_params_destroy(void *data)
Definition: channel.c:9719
int combined_presentation
Combined name and number presentation ie.
Definition: channel.c:8704
#define ast_channel_lock_both(chan1, chan2)
Lock two channels.
Definition: channel.h:2473
FrameHook Architecture.
void ast_str_reset(struct ast_str *buf)
Reset the content of a dynamic string. Useful before a series of ast_str_append.
Definition: strings.h:436
#define CLI_SUCCESS
Definition: cli.h:43
#define LOG_DTMF
Definition: logger.h:177
char * tag
User-set &quot;tag&quot;.
Definition: channel.h:304
struct ast_set_party_id id
Definition: channel.h:433
void ast_deactivate_generator(struct ast_channel *chan)
Definition: channel.c:3107
int type
Q.931 subaddress type.
Definition: channel.h:270
void * data
Definition: datastore.h:56
#define AST_CAUSE_REQUESTED_CHAN_UNAVAIL
Definition: causes.h:124
void ast_poll_channel_del(struct ast_channel *chan0, struct ast_channel *chan1)
Definition: channel.c:2688
int transit_network_select
Transit Network Select.
Definition: channel.h:347
void ast_cdr_discard(struct ast_cdr *cdr)
Discard and free a CDR record.
Definition: cdr.c:488
#define AST_LIST_HEAD_INIT_NOLOCK(head)
Initializes a list head structure.
Definition: linkedlists.h:666
int ast_waitstream_full(struct ast_channel *c, const char *breakon, int audiofd, int monfd)
Definition: file.c:1348
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: app.c:1329
struct ast_flags ast_options
Definition: asterisk.c:178
#define AST_FORMAT_T140
Definition: frame.h:294
#define AST_CAUSE_PRE_EMPTED
Definition: causes.h:103
int(*const call)(struct ast_channel *chan, char *addr, int timeout)
Call a given phone number (address, etc), but don&#39;t take longer than timeout seconds to do so...
Definition: channel.h:542
void ast_party_redirecting_free(struct ast_party_redirecting *doomed)
Destroy the redirecting information contents.
Definition: channel.c:2396
int(*const devicestate)(void *data)
Definition: channel.h:521
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:2316
struct ast_set_party_id to
Definition: channel.h:469
#define AST_CAUSE_NO_ROUTE_DESTINATION
Definition: causes.h:99
unsigned char name
Definition: channel.h:313
#define AST_FORMAT_SLINEAR
Definition: frame.h:254
#define ast_channel_ref(c)
Increase channel reference count.
Definition: channel.h:2491
struct sched_context * sched_context_create(void)
New schedule context.
Definition: sched.c:246
static int chancount
Definition: channel.c:102
int ast_waitfor(struct ast_channel *chan, int ms)
Wait for input on a channel.
Definition: channel.c:3539
int ast_channel_make_compatible(struct ast_channel *c0, struct ast_channel *c1)
Makes two channel formats compatible.
Definition: channel.c:5970
Indicate what information in ast_party_redirecting should be set.
Definition: channel.h:465
When we need to walk through a container, we use an ao2_iterator to keep track of the current positio...
Definition: astobj2.h:1053
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)
Definition: say.h:140
struct ast_channel * ast_request_and_dial(const char *type, format_t format, const struct ast_channel *requestor, void *data, int timeout, int *reason, const char *cid_num, const char *cid_name)
Request a channel of a given type, with data as optional information used by the low level module and...
Definition: channel.c:5650
void ast_tonepair_stop(struct ast_channel *chan)
Definition: channel.c:7964
#define AST_FORMAT_G719
Definition: frame.h:299
int ast_say_number(struct ast_channel *chan, int num, const char *ints, const char *lang, const char *options)
says a number
Definition: channel.c:8397
int count
Number of times the call was redirected.
Definition: channel.h:455
Standard Command Line Interface.
format_t readformat
Definition: channel.h:853
#define ast_calloc(a, b)
Definition: astmm.h:82
int cause
Definition: channel.c:204
struct ast_party_id to
Call is redirecting to a new party (Sent to the caller)
Definition: channel.h:452
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Definition: strings.h:223
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:8443
#define ao2_container_alloc(arg1, arg2, arg3)
Definition: astobj2.h:734
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:5920
#define S_OR(a, b)
returns the equivalent of logic or for strings: first one if not empty, otherwise second one...
Definition: strings.h:77
int type
subaddress type ie
Definition: channel.c:8623
void ast_party_subaddress_init(struct ast_party_subaddress *init)
Initialize the given subaddress structure.
Definition: channel.c:2034
A ringing phone is answered.
Definition: cel.h:56
static struct ast_channel *attribute_malloc __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 char *linkedid, const int amaflag, const char *file, int line, const char *function, const char *name_fmt, va_list ap1, va_list ap2)
Create a new channel structure.
Definition: channel.c:1130
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:573
struct ast_set_party_id ani
Definition: channel.h:435
#define AST_CAUSE_IE_NONEXIST
Definition: causes.h:139
void ast_party_redirecting_init(struct ast_party_redirecting *init)
Initialize the given redirecting structure.
Definition: channel.c:2359
int ast_writestream(struct ast_filestream *fs, struct ast_frame *f)
Writes a frame to a stream.
Definition: file.c:150
int ast_cli_register_multiple(struct ast_cli_entry *e, int len)
Register multiple commands.
Definition: cli.c:2167
const int pos
Definition: cli.h:158
const char * context
Definition: channel.h:999
static void call_forward_inherit(struct ast_channel *new_chan, struct ast_channel *parent, struct ast_channel *orig)
Definition: channel.c:5359
#define AST_CAUSE_USER_BUSY
Definition: causes.h:106
void ast_jb_get_and_deliver(struct ast_channel *c0, struct ast_channel *c1)
Deliver the queued frames that should be delivered now for both channels.
Definition: abstract_jb.c:371
pthread_t blocker
Definition: channel.h:790
Channel monitoring.
int ast_readstring_full(struct ast_channel *c, char *s, int len, int timeout, int rtimeout, char *enders, int audiofd, int ctrlfd)
Definition: channel.c:5842
struct ast_channel_iterator * ast_channel_iterator_destroy(struct ast_channel_iterator *i)
Destroy a channel iterator.
Definition: channel.c:1649
struct ast_category * root
Definition: config.c:238
int ast_setstate(struct ast_channel *chan, enum ast_channel_state)
Change the state of a channel.
Definition: channel.c:7119
Information needed to specify a number in a call.
Definition: channel.h:239
int ast_answer(struct ast_channel *chan)
Answer a channel.
Definition: channel.c:3086
#define AST_PRES_ALLOWED
Definition: callerid.h:324
char * ast_transfercapability2str(int transfercapability) attribute_const
Gives the string form of a given transfer capability.
Definition: channel.c:1041
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:1608
Application convenience functions, designed to give consistent look and feel to Asterisk apps...
#define AST_FORMAT_ADPCM
Definition: frame.h:252
int ast_transfer(struct ast_channel *chan, char *dest)
Transfer a channel (if supported).
Definition: channel.c:5788
#define AST_CAUSE_BUSY
Definition: causes.h:148
const ast_string_field musicclass
Definition: channel.h:787
int ast_is_deferrable_frame(const struct ast_frame *frame)
Should we keep this frame for later?
Definition: channel.c:1818
char * ast_getformatname_multiple(char *buf, size_t size, format_t format)
Get the names of a set of formats.
Definition: frame.c:591
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:9776
const ast_string_field accountcode
Definition: channel.h:787
Data structure associated with a single frame of data.
Definition: frame.h:142
#define AST_DATA_STRUCTURE(__struct, __name)
Definition: data.h:300
static struct ast_datastore_info cc_channel_datastore_info
Definition: channel.c:9725
int hangupcause
Definition: channel.h:849
Internal Asterisk hangup causes.
#define ast_manager_event_multichan(category, event, nchans, chans, contents,...)
Definition: manager.h:226
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:2380
static int total
Definition: res_adsi.c:967
static void ast_channel_change_linkedid(struct ast_channel *chan, const char *linkedid)
Definition: channel.c:6370
static void ast_channel_destructor(void *obj)
Free a channel structure.
Definition: channel.c:2403
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:319
#define AST_TRANS_CAP_RESTRICTED_DIGITAL
Definition: transcap.h:36
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)
Definition: say.h:86
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:8450
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::autochans autochans
#define AST_FORMAT_G722
Definition: frame.h:266
int ast_tonepair_start(struct ast_channel *chan, int freq1, int freq2, int duration, int vol)
Definition: channel.c:7951
struct timeval dtmf_tv
Definition: channel.h:822
void ast_party_dialed_free(struct ast_party_dialed *doomed)
Destroy the dialed party contents.
Definition: channel.c:2262
struct timeval ast_tvsub(struct timeval a, struct timeval b)
Returns the difference of two timevals a - b.
Definition: utils.c:1601
struct timeval whentohangup
Definition: channel.h:789
const char * data
Description of a tone.
Definition: indications.h:53
uint32_t uint32
Definition: frame.h:160
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:496
#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
Definition: frame.h:144
int ast_str2cause(const char *name) attribute_pure
Convert the string form of a cause code to a number.
Definition: channel.c:993
struct ast_variable * next
Definition: config.h:82
static void(* ast_moh_stop_ptr)(struct ast_channel *)
Definition: channel.c:8020
const char * warning_sound
Definition: channel.h:984
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:9605
int ast_safe_sleep_conditional(struct ast_channel *chan, int ms, int(*cond)(void *), void *data)
Wait for a specified amount of time, looking for hangups and a condition argument.
Definition: channel.c:1845
static struct adsi_event events[]
Definition: app_adsiprog.c:78
int ast_undestroyed_channels(void)
Definition: channel.c:853
#define AST_PRES_UNAVAILABLE
Definition: callerid.h:326
#define ast_channel_trylock(chan)
Definition: channel.h:2468
long int disposition
Definition: cdr.h:110
void ast_channels_init(void)
Definition: channel.c:8221
struct ast_trans_pvt * readtrans
Definition: channel.h:763
#define ast_frfree(fr)
Definition: frame.h:583
unsigned char valid
TRUE if the name information is valid/present.
Definition: channel.h:229
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:5313
const char * channelreloadreason2txt(enum channelreloadreason reason)
Convert enum channelreloadreason to text string for manager event.
Definition: channel.c:8372
#define CONFIG_STATUS_FILEINVALID
Definition: config.h:52
static char context[AST_MAX_CONTEXT]
Definition: chan_alsa.c:107
#define AST_TRANS_CAP_SPEECH
Definition: transcap.h:34
Call Parking and Pickup API Includes code and algorithms from the Zapata library. ...
int ast_readstring(struct ast_channel *c, char *s, int len, int timeout, int rtimeout, char *enders)
Reads multiple digits.
Definition: channel.c:5837
Information needed to specify a subaddress in a call.
Definition: channel.h:257
int ast_tonepair(struct ast_channel *chan, int freq1, int freq2, int duration, int vol)
Definition: channel.c:7969
struct ast_party_connected_line target_id
Definition: channel.c:6152
struct ast_channel_monitor * monitor
Definition: channel.h:769
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:2011
static void * silence_generator_alloc(struct ast_channel *chan, void *data)
Definition: channel.c:8269
static int kill_write(struct ast_channel *chan, struct ast_frame *frame)
Definition: channel.c:641
unsigned char payload[0]
Definition: frame.h:381
struct ast_channel_iterator * ast_channel_iterator_all_new(void)
Create a new channel iterator.
Definition: channel.c:1701
static unsigned int cos
Definition: chan_h323.c:147
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:472
static void queue_dtmf_readq(struct ast_channel *chan, struct ast_frame *f)
Definition: channel.c:3778
enum ast_timer_event ast_timer_get_event(const struct ast_timer *handle)
Retrieve timing event.
Definition: timing.c:199
int reason
enum AST_REDIRECTING_REASON value for redirection
Definition: channel.h:458
struct ast_channel * ast_dummy_channel_alloc(void)
Create a fake channel structure.
Definition: channel.c:1391
struct ast_filestream * stream
Definition: channel.h:757
const char * ast_timer_get_name(const struct ast_timer *handle)
Get name of timer in use.
Definition: timing.c:217
enum AST_MONITORING_STATE state
Definition: monitor.h:49
int ast_say_enumeration(struct ast_channel *chan, int num, const char *ints, const char *lang, const char *options)
says an enumeration
Definition: channel.c:8403
static char url[512]
int ast_queue_frame_head(struct ast_channel *chan, struct ast_frame *f)
Queue one or more frames to the head of a channel&#39;s frame queue.
Definition: channel.c:1563
void ast_party_caller_init(struct ast_party_caller *init)
Initialize the given caller structure.
Definition: channel.c:2269
Say numbers and dates (maybe words one day too)
int(* func_channel_write)(struct ast_channel *chan, const char *function, char *data, const char *value)
Provide additional write items for CHANNEL() dialplan function.
Definition: channel.h:603
#define AST_CAUSE_CALL_REJECTED
Definition: causes.h:110
struct ast_channel * ast_channel_get_by_name(const char *name)
Find a channel by name.
Definition: channel.c:1803
#define AST_CAUSE_BEARERCAPABILITY_NOTAVAIL
Definition: causes.h:129
#define ast_malloc(a)
Definition: astmm.h:91
struct ast_data * ast_data_add_str(struct ast_data *root, const char *childname, const char *string)
Add a string node type.
Definition: data.c:2401
int amaflags
Definition: channel.h:843
int ast_jb_do_usecheck(struct ast_channel *c0, struct ast_channel *c1)
Checks the need of a jb use in a generic bridge.
Definition: abstract_jb.c:205
struct ast_channel * ast_request(const char *type, format_t format, const struct ast_channel *requestor, void *data, int *status)
Requests a channel.
Definition: channel.c:5695
static snd_pcm_format_t format
Definition: chan_alsa.c:93
union ast_frame::@172 data
#define AST_CAUSE_MESSAGE_TYPE_NONEXIST
Definition: causes.h:137
struct ast_channel_tech * tech
Definition: channel.h:743
int ast_active_channels(void)
returns number of active/allocated channels
Definition: channel.c:848
#define AST_FORMAT_ILBC
Definition: frame.h:262
struct ast_channel::@156 readq
int ast_channel_datastore_add(struct ast_channel *chan, struct ast_datastore *datastore)
Add a datastore to a channel.
Definition: channel.c:2590
Information needed to specify a name in a call.
Definition: channel.h:212
struct ast_data * ast_data_add_int(struct ast_data *root, const char *childname, int value)
Add an integer node type.
Definition: data.c:2322
int ast_audiohook_detach_list(struct ast_audiohook_list *audiohook_list)
Detach audiohooks from list and destroy said list.
Definition: audiohook.c:418
#define ast_string_field_free_memory(x)
free all memory - to be called before destroying the object
Definition: stringfields.h:253
struct ast_frame * ast_read_noaudio(struct ast_channel *chan)
Reads a frame, returning AST_FRAME_NULL frame if audio.
Definition: channel.c:4388
void(* chan_fixup)(void *data, struct ast_channel *old_chan, struct ast_channel *new_chan)
Fix up channel references.
Definition: datastore.h:50
#define ast_strndup(a, b)
Definition: astmm.h:115
#define AST_FORMAT_GSM
Definition: frame.h:244
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:576
static struct ast_frame * kill_read(struct ast_channel *chan)
Definition: channel.c:629
unsigned char valid
TRUE if the number information is valid/present.
Definition: channel.h:247
long len
Definition: frame.h:170
#define ao2_unlink(arg1, arg2)
Definition: astobj2.h:817
static void ast_read_generator_actions(struct ast_channel *chan, struct ast_frame *f)
Definition: channel.c:3722
static int data_channels_provider_handler(const struct ast_data_search *search, struct ast_data *root)
Definition: channel.c:8096
int ast_channel_datastore_remove(struct ast_channel *chan, struct ast_datastore *datastore)
Remove a datastore from a channel.
Definition: channel.c:2599
void * generatordata
Definition: channel.h:746
int ast_cdr_init(struct ast_cdr *cdr, struct ast_channel *chan)
Initialize based on a channel.
Definition: cdr.c:897
static struct ast_datastore_info xfer_ds_info
Definition: channel.c:6179
int ast_data_search_match(const struct ast_data_search *search, struct ast_data *data)
Check the current generated node to know if it matches the search condition.
Definition: data.c:1458
struct varshead varshead
Definition: channel.h:817
int str
Subscriber name ie.
Definition: channel.c:8473
const ast_string_field language
Definition: channel.h:787
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:8458
uint32_t version
Structure version.
Definition: data.h:247
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:185
static void deactivate_generator_nolock(struct ast_channel *chan)
Definition: channel.c:3091
int( ao2_callback_data_fn)(void *obj, void *arg, void *data, int flags)
Type of a generic callback function.
Definition: astobj2.h:643
#define AST_CAUSE_INVALID_CALL_REFERENCE
Definition: causes.h:133
#define AST_CAUSE_CONGESTION
Definition: causes.h:152
struct ast_cdr * ast_cdr_alloc(void)
Allocate a CDR record.
Definition: cdr.c:499
#define AST_TRANS_CAP_DIGITAL
Definition: transcap.h:35
static struct ast_cli_entry cli_channel[]
Definition: channel.c:624
static void channel_data_add_flags(struct ast_data *tree, struct ast_channel *chan)
Definition: channel.c:281
#define AST_BRIDGE_DTMF_CHANNEL_0
Report DTMF on channel 0.
Definition: channel.h:1932
int ast_do_masquerade(struct ast_channel *chan)
Start masquerading a channel.
Definition: channel.c:6546
char exten[AST_MAX_EXTENSION]
Definition: channel.h:869
Timing source management.
#define AST_CAUSE_WRONG_CALL_STATE
Definition: causes.h:141
int ast_stopstream(struct ast_channel *c)
Stops a stream.
Definition: file.c:128
struct ast_frame * ast_frdup(const struct ast_frame *fr)
Copies a frame.
Definition: frame.c:474
struct ast_variable * ast_variable_new(const char *name, const char *value, const char *filename)
Definition: config.c:278
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:9161
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:2367
static struct ast_data_entry channel_providers[]
Definition: channel.c:8206
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:989
format_t ast_translator_best_choice(format_t *dsts, format_t *srcs)
Chooses the best translation path.
Definition: translate.c:984
int ast_channel_cmpwhentohangup_tv(struct ast_channel *chan, struct timeval offset)
Compare a offset with the settings of when to hang a channel up.
Definition: channel.c:885
The structure of the node handler.
Definition: data.h:245
#define AST_OPTION_CC_AGENT_TYPE
Definition: frame.h:520
void ast_translator_free_path(struct ast_trans_pvt *tr)
Frees a translator path Frees the given translator path structure.
Definition: translate.c:272
static struct ast_generator tonepair
Definition: channel.c:7945
#define ASTERISK_FILE_VERSION(file, version)
Register/unregister a source code file with the core.
Definition: asterisk.h:180
int samples
Definition: frame.h:150
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:2227
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:9663
static int data_channeltypes_provider_handler(const struct ast_data_search *search, struct ast_data *data_root)
Definition: channel.c:8134
#define CONFIG_STATUS_FILEUNCHANGED
Definition: config.h:51
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:2241
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:2064
struct ast_pbx * pbx
Definition: channel.h:761
int ast_sendtext(struct ast_channel *chan, const char *text)
Sends text to a channel.
Definition: channel.c:4687
ast_bridge_result
Definition: channel.h:168
#define AST_LIST_APPEND_LIST(head, list, field)
Appends a whole list to the tail of a list.
Definition: linkedlists.h:768
static int frame_size[4]
Definition: format_g726.c:53
#define ast_string_field_set(x, field, data)
Set a field to a simple string value.
Definition: stringfields.h:344
#define AST_FORMAT_SPEEX16
Definition: frame.h:301
void ast_cdr_busy(struct ast_cdr *cdr)
Busy a call.
Definition: cdr.c:753
static ast_group_t group
Definition: chan_agent.c:221
struct ast_party_number number
Subscriber phone number.
Definition: channel.h:292
void(* digit)(struct ast_channel *chan, char digit)
Definition: channel.h:188