Add private representation of caller, connected and redirecting party ids.
[asterisk/asterisk.git] / main / channel_internal_api.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2012, 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 Accessor API
22  *
23  * This file is intended to be the only file that ever accesses the
24  * internals of an ast_channel. All other files should use the
25  * accessor functions defined here.
26  *
27  * \author Terry Wilson
28  */
29
30 /*** MODULEINFO
31         <support_level>core</support_level>
32  ***/
33
34 #include "asterisk.h"
35
36 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
37
38 #include <unistd.h>
39 #include <fcntl.h>
40
41 #include "asterisk/channel.h"
42 #include "asterisk/stringfields.h"
43 #include "asterisk/data.h"
44 #include "asterisk/indications.h"
45 #include "asterisk/channel_internal.h"
46 #include "asterisk/test.h"
47
48 /*!
49  * \brief Main Channel structure associated with a channel.
50  *
51  * \note When adding fields to this structure, it is important to add the field
52  *       'in position' with like-aligned fields, so as to keep the compiler from
53  *       having to add padding to align fields. The structure's fields are sorted
54  *       in this order: pointers, structures, long, int/enum, short, char. This
55  *       is especially important on 64-bit architectures, where mixing 4-byte
56  *       and 8-byte fields causes 4 bytes of padding to be added before many
57  *       8-byte fields.
58  */
59 struct ast_channel {
60         const struct ast_channel_tech *tech;            /*!< Technology (point to channel driver) */
61         void *tech_pvt;                                 /*!< Private data used by the technology driver */
62         void *music_state;                              /*!< Music State*/
63         void *generatordata;                            /*!< Current generator data if there is any */
64         struct ast_generator *generator;                /*!< Current active data generator */
65         struct ast_channel * bridged_channel;                   /*!< Who are we bridged to, if we're bridged.
66                                                          *   Who is proxying for us, if we are proxied (i.e. chan_agent).
67                                                          *   Do not access directly, use ast_bridged_channel(chan) */
68         struct ast_channel *masq;                       /*!< Channel that will masquerade as us */
69         struct ast_channel *masqr;                      /*!< Who we are masquerading as */
70         const char *blockproc;                          /*!< Procedure causing blocking */
71         const char *appl;                               /*!< Current application */
72         const char *data;                               /*!< Data passed to current application */
73         struct ast_sched_context *sched;                /*!< Schedule context */
74         struct ast_filestream *stream;                  /*!< Stream itself. */
75         struct ast_filestream *vstream;                 /*!< Video Stream itself. */
76         ast_timing_func_t timingfunc;
77         void *timingdata;
78         struct ast_pbx *pbx;                            /*!< PBX private structure for this channel */
79         struct ast_trans_pvt *writetrans;               /*!< Write translation path */
80         struct ast_trans_pvt *readtrans;                /*!< Read translation path */
81         struct ast_audiohook_list *audiohooks;
82         struct ast_framehook_list *framehooks;
83         struct ast_cdr *cdr;                            /*!< Call Detail Record */
84         struct ast_tone_zone *zone;                     /*!< Tone zone as set in indications.conf or
85                                                          *   in the CHANNEL dialplan function */
86         struct ast_channel_monitor *monitor;            /*!< Channel monitoring */
87         struct ast_callid *callid;                      /*!< Bound call identifier pointer */
88 #ifdef HAVE_EPOLL
89         struct ast_epoll_data *epfd_data[AST_MAX_FDS];
90 #endif
91         struct ao2_container *dialed_causes;            /*!< Contains tech-specific and Asterisk cause data from dialed channels */
92
93         AST_DECLARE_STRING_FIELDS(
94                 AST_STRING_FIELD(name);         /*!< ASCII unique channel name */
95                 AST_STRING_FIELD(language);     /*!< Language requested for voice prompts */
96                 AST_STRING_FIELD(musicclass);   /*!< Default music class */
97                 AST_STRING_FIELD(accountcode);  /*!< Account code for billing */
98                 AST_STRING_FIELD(peeraccount);  /*!< Peer account code for billing */
99                 AST_STRING_FIELD(userfield);    /*!< Userfield for CEL billing */
100                 AST_STRING_FIELD(call_forward); /*!< Where to forward to if asked to dial on this interface */
101                 AST_STRING_FIELD(uniqueid);     /*!< Unique Channel Identifier */
102                 AST_STRING_FIELD(linkedid);     /*!< Linked Channel Identifier -- gets propagated by linkage */
103                 AST_STRING_FIELD(parkinglot);   /*! Default parking lot, if empty, default parking lot  */
104                 AST_STRING_FIELD(hangupsource); /*! Who is responsible for hanging up this channel */
105                 AST_STRING_FIELD(dialcontext);  /*!< Dial: Extension context that we were called from */
106         );
107
108         struct timeval whentohangup; /*!< Non-zero, set to actual time when channel is to be hung up */
109         pthread_t blocker;           /*!< If anyone is blocking, this is them */
110
111         /*!
112          * \brief Dialed/Called information.
113          * \note Set on incoming channels to indicate the originally dialed party.
114          * \note Dialed Number Identifier (DNID)
115          */
116         struct ast_party_dialed dialed;
117
118         /*!
119          * \brief Channel Caller ID information.
120          * \note The caller id information is the caller id of this
121          * channel when it is used to initiate a call.
122          */
123         struct ast_party_caller caller;
124
125         /*!
126          * \brief Channel Connected Line ID information.
127          * \note The connected line information identifies the channel
128          * connected/bridged to this channel.
129          */
130         struct ast_party_connected_line connected;
131
132         /*! \brief Redirecting/Diversion information */
133         struct ast_party_redirecting redirecting;
134
135         struct ast_frame dtmff;                         /*!< DTMF frame */
136         struct varshead varshead;                       /*!< A linked list for channel variables. See \ref AstChanVar */
137         ast_group_t callgroup;                          /*!< Call group for call pickups */
138         ast_group_t pickupgroup;                        /*!< Pickup group - which calls groups can be picked up? */
139         struct ast_namedgroups *named_callgroups;       /*!< Named call group for call pickups */
140         struct ast_namedgroups *named_pickupgroups;     /*!< Named pickup group - which call groups can be picked up? */
141         struct timeval creationtime;                    /*!< The time of channel creation */
142         struct ast_readq_list readq;
143         struct ast_jb jb;                               /*!< The jitterbuffer state */
144         struct timeval dtmf_tv;                         /*!< The time that an in process digit began, or the last digit ended */
145         struct ast_hangup_handler_list hangup_handlers;/*!< Hangup handlers on the channel. */
146         struct ast_datastore_list datastores; /*!< Data stores on the channel */
147         struct ast_autochan_list autochans; /*!< Autochans on the channel */
148         unsigned long insmpl;                           /*!< Track the read/written samples for monitor use */
149         unsigned long outsmpl;                          /*!< Track the read/written samples for monitor use */
150
151         int fds[AST_MAX_FDS];                           /*!< File descriptors for channel -- Drivers will poll on
152                                                          *   these file descriptors, so at least one must be non -1.
153                                                          *   See \arg \ref AstFileDesc */
154         int softhangup;                         /*!< Whether or not we have been hung up...  Do not set this value
155                                                          *   directly, use ast_softhangup() */
156         int fdno;                                       /*!< Which fd had an event detected on */
157         int streamid;                                   /*!< For streaming playback, the schedule ID */
158         int vstreamid;                                  /*!< For streaming video playback, the schedule ID */
159         struct ast_format oldwriteformat;  /*!< Original writer format */
160         int timingfd;                                   /*!< Timing fd */
161         enum ast_channel_state state;                   /*!< State of line -- Don't write directly, use ast_setstate() */
162         int rings;                                      /*!< Number of rings so far */
163         int priority;                                   /*!< Dialplan: Current extension priority */
164         int macropriority;                              /*!< Macro: Current non-macro priority. See app_macro.c */
165         int amaflags;                                   /*!< Set BEFORE PBX is started to determine AMA flags */
166         enum ast_channel_adsicpe adsicpe;               /*!< Whether or not ADSI is detected on CPE */
167         unsigned int fin;                               /*!< Frames in counters. The high bit is a debug mask, so
168                                                          *   the counter is only in the remaining bits */
169         unsigned int fout;                              /*!< Frames out counters. The high bit is a debug mask, so
170                                                          *   the counter is only in the remaining bits */
171         int hangupcause;                                /*!< Why is the channel hanged up. See causes.h */
172         unsigned int finalized:1;       /*!< Whether or not the channel has been successfully allocated */
173         struct ast_flags flags;                         /*!< channel flags of AST_FLAG_ type */
174         int alertpipe[2];
175         struct ast_format_cap *nativeformats;         /*!< Kinds of data this channel can natively handle */
176         struct ast_format readformat;            /*!< Requested read format (after translation) */
177         struct ast_format writeformat;           /*!< Requested write format (after translation) */
178         struct ast_format rawreadformat;         /*!< Raw read format (before translation) */
179         struct ast_format rawwriteformat;        /*!< Raw write format (before translation) */
180         unsigned int emulate_dtmf_duration;             /*!< Number of ms left to emulate DTMF for */
181 #ifdef HAVE_EPOLL
182         int epfd;
183 #endif
184         int visible_indication;                         /*!< Indication currently playing on the channel */
185
186         unsigned short transfercapability;              /*!< ISDN Transfer Capability - AST_FLAG_DIGITAL is not enough */
187
188         struct ast_bridge *bridge;                      /*!< Bridge this channel is participating in */
189         struct ast_timer *timer;                        /*!< timer object that provided timingfd */
190
191         char context[AST_MAX_CONTEXT];                  /*!< Dialplan: Current extension context */
192         char exten[AST_MAX_EXTENSION];                  /*!< Dialplan: Current extension number */
193         char macrocontext[AST_MAX_CONTEXT];             /*!< Macro: Current non-macro context. See app_macro.c */
194         char macroexten[AST_MAX_EXTENSION];             /*!< Macro: Current non-macro extension. See app_macro.c */
195         char dtmf_digit_to_emulate;                     /*!< Digit being emulated */
196 };
197
198 /* AST_DATA definitions, which will probably have to be re-thought since the channel will be opaque */
199
200 #if 0   /* XXX AstData: ast_callerid no longer exists. (Equivalent code not readily apparent.) */
201 #define DATA_EXPORT_CALLERID(MEMBER)                            \
202         MEMBER(ast_callerid, cid_dnid, AST_DATA_STRING)         \
203         MEMBER(ast_callerid, cid_num, AST_DATA_STRING)          \
204         MEMBER(ast_callerid, cid_name, AST_DATA_STRING)         \
205         MEMBER(ast_callerid, cid_ani, AST_DATA_STRING)          \
206         MEMBER(ast_callerid, cid_pres, AST_DATA_INTEGER)        \
207         MEMBER(ast_callerid, cid_ani2, AST_DATA_INTEGER)        \
208         MEMBER(ast_callerid, cid_tag, AST_DATA_STRING)
209
210 AST_DATA_STRUCTURE(ast_callerid, DATA_EXPORT_CALLERID);
211 #endif
212
213 #define DATA_EXPORT_CHANNEL(MEMBER)                                             \
214         MEMBER(ast_channel, blockproc, AST_DATA_STRING)                         \
215         MEMBER(ast_channel, appl, AST_DATA_STRING)                              \
216         MEMBER(ast_channel, data, AST_DATA_STRING)                              \
217         MEMBER(ast_channel, name, AST_DATA_STRING) \
218         MEMBER(ast_channel, language, AST_DATA_STRING)                          \
219         MEMBER(ast_channel, musicclass, AST_DATA_STRING)                        \
220         MEMBER(ast_channel, accountcode, AST_DATA_STRING)                       \
221         MEMBER(ast_channel, peeraccount, AST_DATA_STRING)                       \
222         MEMBER(ast_channel, userfield, AST_DATA_STRING)                         \
223         MEMBER(ast_channel, call_forward, AST_DATA_STRING)                      \
224         MEMBER(ast_channel, uniqueid, AST_DATA_STRING)                          \
225         MEMBER(ast_channel, linkedid, AST_DATA_STRING)                          \
226         MEMBER(ast_channel, parkinglot, AST_DATA_STRING)                        \
227         MEMBER(ast_channel, hangupsource, AST_DATA_STRING)                      \
228         MEMBER(ast_channel, dialcontext, AST_DATA_STRING)                       \
229         MEMBER(ast_channel, rings, AST_DATA_INTEGER)                            \
230         MEMBER(ast_channel, priority, AST_DATA_INTEGER)                         \
231         MEMBER(ast_channel, macropriority, AST_DATA_INTEGER)                    \
232         MEMBER(ast_channel, adsicpe, AST_DATA_INTEGER)                          \
233         MEMBER(ast_channel, fin, AST_DATA_UNSIGNED_INTEGER)                     \
234         MEMBER(ast_channel, fout, AST_DATA_UNSIGNED_INTEGER)                    \
235         MEMBER(ast_channel, emulate_dtmf_duration, AST_DATA_UNSIGNED_INTEGER)   \
236         MEMBER(ast_channel, visible_indication, AST_DATA_INTEGER)               \
237         MEMBER(ast_channel, context, AST_DATA_STRING)                           \
238         MEMBER(ast_channel, exten, AST_DATA_STRING)                             \
239         MEMBER(ast_channel, macrocontext, AST_DATA_STRING)                      \
240         MEMBER(ast_channel, macroexten, AST_DATA_STRING)
241
242 AST_DATA_STRUCTURE(ast_channel, DATA_EXPORT_CHANNEL);
243
244 static void channel_data_add_flags(struct ast_data *tree,
245         struct ast_channel *chan)
246 {
247         ast_data_add_bool(tree, "DEFER_DTMF", ast_test_flag(ast_channel_flags(chan), AST_FLAG_DEFER_DTMF));
248         ast_data_add_bool(tree, "WRITE_INT", ast_test_flag(ast_channel_flags(chan), AST_FLAG_WRITE_INT));
249         ast_data_add_bool(tree, "BLOCKING", ast_test_flag(ast_channel_flags(chan), AST_FLAG_BLOCKING));
250         ast_data_add_bool(tree, "ZOMBIE", ast_test_flag(ast_channel_flags(chan), AST_FLAG_ZOMBIE));
251         ast_data_add_bool(tree, "EXCEPTION", ast_test_flag(ast_channel_flags(chan), AST_FLAG_EXCEPTION));
252         ast_data_add_bool(tree, "MOH", ast_test_flag(ast_channel_flags(chan), AST_FLAG_MOH));
253         ast_data_add_bool(tree, "SPYING", ast_test_flag(ast_channel_flags(chan), AST_FLAG_SPYING));
254         ast_data_add_bool(tree, "NBRIDGE", ast_test_flag(ast_channel_flags(chan), AST_FLAG_NBRIDGE));
255         ast_data_add_bool(tree, "IN_AUTOLOOP", ast_test_flag(ast_channel_flags(chan), AST_FLAG_IN_AUTOLOOP));
256         ast_data_add_bool(tree, "OUTGOING", ast_test_flag(ast_channel_flags(chan), AST_FLAG_OUTGOING));
257         ast_data_add_bool(tree, "IN_DTMF", ast_test_flag(ast_channel_flags(chan), AST_FLAG_IN_DTMF));
258         ast_data_add_bool(tree, "EMULATE_DTMF", ast_test_flag(ast_channel_flags(chan), AST_FLAG_EMULATE_DTMF));
259         ast_data_add_bool(tree, "END_DTMF_ONLY", ast_test_flag(ast_channel_flags(chan), AST_FLAG_END_DTMF_ONLY));
260         ast_data_add_bool(tree, "MASQ_NOSTREAM", ast_test_flag(ast_channel_flags(chan), AST_FLAG_MASQ_NOSTREAM));
261         ast_data_add_bool(tree, "BRIDGE_HANGUP_RUN", ast_test_flag(ast_channel_flags(chan), AST_FLAG_BRIDGE_HANGUP_RUN));
262         ast_data_add_bool(tree, "BRIDGE_HANGUP_DONT", ast_test_flag(ast_channel_flags(chan), AST_FLAG_BRIDGE_HANGUP_DONT));
263         ast_data_add_bool(tree, "DISABLE_WORKAROUNDS", ast_test_flag(ast_channel_flags(chan), AST_FLAG_DISABLE_WORKAROUNDS));
264 }
265
266 int ast_channel_data_add_structure(struct ast_data *tree,
267         struct ast_channel *chan, int add_bridged)
268 {
269         struct ast_channel *bc;
270         struct ast_data *data_bridged;
271         struct ast_data *data_cdr;
272         struct ast_data *data_flags;
273         struct ast_data *data_zones;
274         struct ast_data *enum_node;
275         struct ast_data *data_softhangup;
276 #if 0   /* XXX AstData: ast_callerid no longer exists. (Equivalent code not readily apparent.) */
277         struct ast_data *data_callerid;
278         char value_str[100];
279 #endif
280
281         if (!tree) {
282                 return -1;
283         }
284
285         ast_data_add_structure(ast_channel, tree, chan);
286
287         if (add_bridged) {
288                 bc = ast_bridged_channel(chan);
289                 if (bc) {
290                         data_bridged = ast_data_add_node(tree, "bridged");
291                         if (!data_bridged) {
292                                 return -1;
293                         }
294                         ast_channel_data_add_structure(data_bridged, bc, 0);
295                 }
296         }
297
298         ast_data_add_codec(tree, "oldwriteformat", ast_channel_oldwriteformat(chan));
299         ast_data_add_codec(tree, "readformat", ast_channel_readformat(chan));
300         ast_data_add_codec(tree, "writeformat", ast_channel_writeformat(chan));
301         ast_data_add_codec(tree, "rawreadformat", ast_channel_rawreadformat(chan));
302         ast_data_add_codec(tree, "rawwriteformat", ast_channel_rawwriteformat(chan));
303         ast_data_add_codecs(tree, "nativeformats", ast_channel_nativeformats(chan));
304
305         /* state */
306         enum_node = ast_data_add_node(tree, "state");
307         if (!enum_node) {
308                 return -1;
309         }
310         ast_data_add_str(enum_node, "text", ast_state2str(ast_channel_state(chan)));
311         ast_data_add_int(enum_node, "value", ast_channel_state(chan));
312
313         /* hangupcause */
314         enum_node = ast_data_add_node(tree, "hangupcause");
315         if (!enum_node) {
316                 return -1;
317         }
318         ast_data_add_str(enum_node, "text", ast_cause2str(ast_channel_hangupcause(chan)));
319         ast_data_add_int(enum_node, "value", ast_channel_hangupcause(chan));
320
321         /* amaflags */
322         enum_node = ast_data_add_node(tree, "amaflags");
323         if (!enum_node) {
324                 return -1;
325         }
326         ast_data_add_str(enum_node, "text", ast_cdr_flags2str(ast_channel_amaflags(chan)));
327         ast_data_add_int(enum_node, "value", ast_channel_amaflags(chan));
328
329         /* transfercapability */
330         enum_node = ast_data_add_node(tree, "transfercapability");
331         if (!enum_node) {
332                 return -1;
333         }
334         ast_data_add_str(enum_node, "text", ast_transfercapability2str(ast_channel_transfercapability(chan)));
335         ast_data_add_int(enum_node, "value", ast_channel_transfercapability(chan));
336
337         /* _softphangup */
338         data_softhangup = ast_data_add_node(tree, "softhangup");
339         if (!data_softhangup) {
340                 return -1;
341         }
342         ast_data_add_bool(data_softhangup, "dev", ast_channel_softhangup_internal_flag(chan) & AST_SOFTHANGUP_DEV);
343         ast_data_add_bool(data_softhangup, "asyncgoto", ast_channel_softhangup_internal_flag(chan) & AST_SOFTHANGUP_ASYNCGOTO);
344         ast_data_add_bool(data_softhangup, "shutdown", ast_channel_softhangup_internal_flag(chan) & AST_SOFTHANGUP_SHUTDOWN);
345         ast_data_add_bool(data_softhangup, "timeout", ast_channel_softhangup_internal_flag(chan) & AST_SOFTHANGUP_TIMEOUT);
346         ast_data_add_bool(data_softhangup, "appunload", ast_channel_softhangup_internal_flag(chan) & AST_SOFTHANGUP_APPUNLOAD);
347         ast_data_add_bool(data_softhangup, "explicit", ast_channel_softhangup_internal_flag(chan) & AST_SOFTHANGUP_EXPLICIT);
348         ast_data_add_bool(data_softhangup, "unbridge", ast_channel_softhangup_internal_flag(chan) & AST_SOFTHANGUP_UNBRIDGE);
349
350         /* channel flags */
351         data_flags = ast_data_add_node(tree, "flags");
352         if (!data_flags) {
353                 return -1;
354         }
355         channel_data_add_flags(data_flags, chan);
356
357         ast_data_add_uint(tree, "timetohangup", ast_channel_whentohangup(chan)->tv_sec);
358
359 #if 0   /* XXX AstData: ast_callerid no longer exists. (Equivalent code not readily apparent.) */
360         /* callerid */
361         data_callerid = ast_data_add_node(tree, "callerid");
362         if (!data_callerid) {
363                 return -1;
364         }
365         ast_data_add_structure(ast_callerid, data_callerid, &(chan->cid));
366         /* insert the callerid ton */
367         enum_node = ast_data_add_node(data_callerid, "cid_ton");
368         if (!enum_node) {
369                 return -1;
370         }
371         ast_data_add_int(enum_node, "value", chan->cid.cid_ton);
372         snprintf(value_str, sizeof(value_str), "TON: %s/Plan: %s",
373                 party_number_ton2str(chan->cid.cid_ton),
374                 party_number_plan2str(chan->cid.cid_ton));
375         ast_data_add_str(enum_node, "text", value_str);
376 #endif
377
378         /* tone zone */
379         if (ast_channel_zone(chan)) {
380                 data_zones = ast_data_add_node(tree, "zone");
381                 if (!data_zones) {
382                         return -1;
383                 }
384                 ast_tone_zone_data_add_structure(data_zones, ast_channel_zone(chan));
385         }
386
387         /* insert cdr */
388         data_cdr = ast_data_add_node(tree, "cdr");
389         if (!data_cdr) {
390                 return -1;
391         }
392
393         ast_cdr_data_add_structure(data_cdr, ast_channel_cdr(chan), 1);
394
395         return 0;
396 }
397
398 int ast_channel_data_cmp_structure(const struct ast_data_search *tree,
399         struct ast_channel *chan, const char *structure_name)
400 {
401         return ast_data_search_cmp_structure(tree, ast_channel, chan, structure_name);
402 }
403
404 /* ACCESSORS */
405
406 #define DEFINE_STRINGFIELD_SETTERS_FOR(field) \
407 void ast_channel_##field##_set(struct ast_channel *chan, const char *value) \
408 { \
409         ast_string_field_set(chan, field, value); \
410 } \
411   \
412 void ast_channel_##field##_build_va(struct ast_channel *chan, const char *fmt, va_list ap) \
413 { \
414         ast_string_field_build_va(chan, field, fmt, ap); \
415 } \
416 void ast_channel_##field##_build(struct ast_channel *chan, const char *fmt, ...) \
417 { \
418         va_list ap; \
419         va_start(ap, fmt); \
420         ast_channel_##field##_build_va(chan, fmt, ap); \
421         va_end(ap); \
422 }
423
424 DEFINE_STRINGFIELD_SETTERS_FOR(name);
425 DEFINE_STRINGFIELD_SETTERS_FOR(language);
426 DEFINE_STRINGFIELD_SETTERS_FOR(musicclass);
427 DEFINE_STRINGFIELD_SETTERS_FOR(accountcode);
428 DEFINE_STRINGFIELD_SETTERS_FOR(peeraccount);
429 DEFINE_STRINGFIELD_SETTERS_FOR(userfield);
430 DEFINE_STRINGFIELD_SETTERS_FOR(call_forward);
431 DEFINE_STRINGFIELD_SETTERS_FOR(uniqueid);
432 DEFINE_STRINGFIELD_SETTERS_FOR(parkinglot);
433 DEFINE_STRINGFIELD_SETTERS_FOR(hangupsource);
434 DEFINE_STRINGFIELD_SETTERS_FOR(dialcontext);
435
436 #define DEFINE_STRINGFIELD_GETTER_FOR(field) const char *ast_channel_##field(const struct ast_channel *chan) \
437 { \
438         return chan->field; \
439 }
440
441 DEFINE_STRINGFIELD_GETTER_FOR(name);
442 DEFINE_STRINGFIELD_GETTER_FOR(language);
443 DEFINE_STRINGFIELD_GETTER_FOR(musicclass);
444 DEFINE_STRINGFIELD_GETTER_FOR(accountcode);
445 DEFINE_STRINGFIELD_GETTER_FOR(peeraccount);
446 DEFINE_STRINGFIELD_GETTER_FOR(userfield);
447 DEFINE_STRINGFIELD_GETTER_FOR(call_forward);
448 DEFINE_STRINGFIELD_GETTER_FOR(uniqueid);
449 DEFINE_STRINGFIELD_GETTER_FOR(linkedid);
450 DEFINE_STRINGFIELD_GETTER_FOR(parkinglot);
451 DEFINE_STRINGFIELD_GETTER_FOR(hangupsource);
452 DEFINE_STRINGFIELD_GETTER_FOR(dialcontext);
453
454 void ast_channel_linkedid_set(struct ast_channel *chan, const char *value)
455 {
456         ast_assert(!ast_strlen_zero(value));
457         ast_string_field_set(chan, linkedid, value);
458 }
459
460 const char *ast_channel_appl(const struct ast_channel *chan)
461 {
462         return chan->appl;
463 }
464 void ast_channel_appl_set(struct ast_channel *chan, const char *value)
465 {
466         chan->appl = value;
467 }
468 const char *ast_channel_blockproc(const struct ast_channel *chan)
469 {
470         return chan->blockproc;
471 }
472 void ast_channel_blockproc_set(struct ast_channel *chan, const char *value)
473 {
474         chan->blockproc = value;
475 }
476 const char *ast_channel_data(const struct ast_channel *chan)
477 {
478         return chan->data;
479 }
480 void ast_channel_data_set(struct ast_channel *chan, const char *value)
481 {
482         chan->data = value;
483 }
484
485 const char *ast_channel_context(const struct ast_channel *chan)
486 {
487         return chan->context;
488 }
489 void ast_channel_context_set(struct ast_channel *chan, const char *value)
490 {
491         ast_copy_string(chan->context, value, sizeof(chan->context));
492 }
493 const char *ast_channel_exten(const struct ast_channel *chan)
494 {
495         return chan->exten;
496 }
497 void ast_channel_exten_set(struct ast_channel *chan, const char *value)
498 {
499         ast_copy_string(chan->exten, value, sizeof(chan->exten));
500 }
501 const char *ast_channel_macrocontext(const struct ast_channel *chan)
502 {
503         return chan->macrocontext;
504 }
505 void ast_channel_macrocontext_set(struct ast_channel *chan, const char *value)
506 {
507         ast_copy_string(chan->macrocontext, value, sizeof(chan->macrocontext));
508 }
509 const char *ast_channel_macroexten(const struct ast_channel *chan)
510 {
511         return chan->macroexten;
512 }
513 void ast_channel_macroexten_set(struct ast_channel *chan, const char *value)
514 {
515         ast_copy_string(chan->macroexten, value, sizeof(chan->macroexten));
516 }
517
518 char ast_channel_dtmf_digit_to_emulate(const struct ast_channel *chan)
519 {
520         return chan->dtmf_digit_to_emulate;
521 }
522 void ast_channel_dtmf_digit_to_emulate_set(struct ast_channel *chan, char value)
523 {
524         chan->dtmf_digit_to_emulate = value;
525 }
526 int ast_channel_amaflags(const struct ast_channel *chan)
527 {
528         return chan->amaflags;
529 }
530 void ast_channel_amaflags_set(struct ast_channel *chan, int value)
531 {
532         chan->amaflags = value;
533 }
534 #ifdef HAVE_EPOLL
535 int ast_channel_epfd(const struct ast_channel *chan)
536 {
537         return chan->epfd;
538 }
539 void ast_channel_epfd_set(struct ast_channel *chan, int value)
540 {
541         chan->epfd = value;
542 }
543 #endif
544 int ast_channel_fdno(const struct ast_channel *chan)
545 {
546         return chan->fdno;
547 }
548 void ast_channel_fdno_set(struct ast_channel *chan, int value)
549 {
550         chan->fdno = value;
551 }
552 int ast_channel_hangupcause(const struct ast_channel *chan)
553 {
554         return chan->hangupcause;
555 }
556 void ast_channel_hangupcause_set(struct ast_channel *chan, int value)
557 {
558         chan->hangupcause = value;
559 }
560 int ast_channel_macropriority(const struct ast_channel *chan)
561 {
562         return chan->macropriority;
563 }
564 void ast_channel_macropriority_set(struct ast_channel *chan, int value)
565 {
566         chan->macropriority = value;
567 }
568 int ast_channel_priority(const struct ast_channel *chan)
569 {
570         return chan->priority;
571 }
572 void ast_channel_priority_set(struct ast_channel *chan, int value)
573 {
574         chan->priority = value;
575 }
576 int ast_channel_rings(const struct ast_channel *chan)
577 {
578         return chan->rings;
579 }
580 void ast_channel_rings_set(struct ast_channel *chan, int value)
581 {
582         chan->rings = value;
583 }
584 int ast_channel_streamid(const struct ast_channel *chan)
585 {
586         return chan->streamid;
587 }
588 void ast_channel_streamid_set(struct ast_channel *chan, int value)
589 {
590         chan->streamid = value;
591 }
592 int ast_channel_timingfd(const struct ast_channel *chan)
593 {
594         return chan->timingfd;
595 }
596 void ast_channel_timingfd_set(struct ast_channel *chan, int value)
597 {
598         chan->timingfd = value;
599 }
600 int ast_channel_visible_indication(const struct ast_channel *chan)
601 {
602         return chan->visible_indication;
603 }
604 void ast_channel_visible_indication_set(struct ast_channel *chan, int value)
605 {
606         chan->visible_indication = value;
607 }
608 int ast_channel_vstreamid(const struct ast_channel *chan)
609 {
610         return chan->vstreamid;
611 }
612 void ast_channel_vstreamid_set(struct ast_channel *chan, int value)
613 {
614         chan->vstreamid = value;
615 }
616 unsigned short ast_channel_transfercapability(const struct ast_channel *chan)
617 {
618         return chan->transfercapability;
619 }
620 void ast_channel_transfercapability_set(struct ast_channel *chan, unsigned short value)
621 {
622         chan->transfercapability = value;
623 }
624 unsigned int ast_channel_emulate_dtmf_duration(const struct ast_channel *chan)
625 {
626         return chan->emulate_dtmf_duration;
627 }
628 void ast_channel_emulate_dtmf_duration_set(struct ast_channel *chan, unsigned int value)
629 {
630         chan->emulate_dtmf_duration = value;
631 }
632 unsigned int ast_channel_fin(const struct ast_channel *chan)
633 {
634         return chan->fin;
635 }
636 void ast_channel_fin_set(struct ast_channel *chan, unsigned int value)
637 {
638         chan->fin = value;
639 }
640 unsigned int ast_channel_fout(const struct ast_channel *chan)
641 {
642         return chan->fout;
643 }
644 void ast_channel_fout_set(struct ast_channel *chan, unsigned int value)
645 {
646         chan->fout = value;
647 }
648 unsigned long ast_channel_insmpl(const struct ast_channel *chan)
649 {
650         return chan->insmpl;
651 }
652 void ast_channel_insmpl_set(struct ast_channel *chan, unsigned long value)
653 {
654         chan->insmpl = value;
655 }
656 unsigned long ast_channel_outsmpl(const struct ast_channel *chan)
657 {
658         return chan->outsmpl;
659 }
660 void ast_channel_outsmpl_set(struct ast_channel *chan, unsigned long value)
661 {
662         chan->outsmpl = value;
663 }
664 void *ast_channel_generatordata(const struct ast_channel *chan)
665 {
666         return chan->generatordata;
667 }
668 void ast_channel_generatordata_set(struct ast_channel *chan, void *value)
669 {
670         chan->generatordata = value;
671 }
672 void *ast_channel_music_state(const struct ast_channel *chan)
673 {
674         return chan->music_state;
675 }
676 void ast_channel_music_state_set(struct ast_channel *chan, void *value)
677 {
678         chan->music_state = value;
679 }
680 void *ast_channel_tech_pvt(const struct ast_channel *chan)
681 {
682         return chan->tech_pvt;
683 }
684 void ast_channel_tech_pvt_set(struct ast_channel *chan, void *value)
685 {
686         chan->tech_pvt = value;
687 }
688 void *ast_channel_timingdata(const struct ast_channel *chan)
689 {
690         return chan->timingdata;
691 }
692 void ast_channel_timingdata_set(struct ast_channel *chan, void *value)
693 {
694         chan->timingdata = value;
695 }
696 struct ast_audiohook_list *ast_channel_audiohooks(const struct ast_channel *chan)
697 {
698         return chan->audiohooks;
699 }
700 void ast_channel_audiohooks_set(struct ast_channel *chan, struct ast_audiohook_list *value)
701 {
702         chan->audiohooks = value;
703 }
704 struct ast_cdr *ast_channel_cdr(const struct ast_channel *chan)
705 {
706         return chan->cdr;
707 }
708 void ast_channel_cdr_set(struct ast_channel *chan, struct ast_cdr *value)
709 {
710         chan->cdr = value;
711 }
712 struct ast_channel *ast_channel_masq(const struct ast_channel *chan)
713 {
714         return chan->masq;
715 }
716 void ast_channel_masq_set(struct ast_channel *chan, struct ast_channel *value)
717 {
718         chan->masq = value;
719 }
720 struct ast_channel *ast_channel_masqr(const struct ast_channel *chan)
721 {
722         return chan->masqr;
723 }
724 void ast_channel_masqr_set(struct ast_channel *chan, struct ast_channel *value)
725 {
726         chan->masqr = value;
727 }
728 struct ast_channel_monitor *ast_channel_monitor(const struct ast_channel *chan)
729 {
730         return chan->monitor;
731 }
732 void ast_channel_monitor_set(struct ast_channel *chan, struct ast_channel_monitor *value)
733 {
734         chan->monitor = value;
735 }
736 struct ast_filestream *ast_channel_stream(const struct ast_channel *chan)
737 {
738         return chan->stream;
739 }
740 void ast_channel_stream_set(struct ast_channel *chan, struct ast_filestream *value)
741 {
742         chan->stream = value;
743 }
744 struct ast_filestream *ast_channel_vstream(const struct ast_channel *chan)
745 {
746         return chan->vstream;
747 }
748 void ast_channel_vstream_set(struct ast_channel *chan, struct ast_filestream *value)
749 {
750         chan->vstream = value;
751 }
752 struct ast_format_cap *ast_channel_nativeformats(const struct ast_channel *chan)
753 {
754         return chan->nativeformats;
755 }
756 void ast_channel_nativeformats_set(struct ast_channel *chan, struct ast_format_cap *value)
757 {
758         chan->nativeformats = value;
759 }
760 struct ast_framehook_list *ast_channel_framehooks(const struct ast_channel *chan)
761 {
762         return chan->framehooks;
763 }
764 void ast_channel_framehooks_set(struct ast_channel *chan, struct ast_framehook_list *value)
765 {
766         chan->framehooks = value;
767 }
768 struct ast_generator *ast_channel_generator(const struct ast_channel *chan)
769 {
770         return chan->generator;
771 }
772 void ast_channel_generator_set(struct ast_channel *chan, struct ast_generator *value)
773 {
774         chan->generator = value;
775 }
776 struct ast_pbx *ast_channel_pbx(const struct ast_channel *chan)
777 {
778         return chan->pbx;
779 }
780 void ast_channel_pbx_set(struct ast_channel *chan, struct ast_pbx *value)
781 {
782         chan->pbx = value;
783 }
784 struct ast_sched_context *ast_channel_sched(const struct ast_channel *chan)
785 {
786         return chan->sched;
787 }
788 void ast_channel_sched_set(struct ast_channel *chan, struct ast_sched_context *value)
789 {
790         chan->sched = value;
791 }
792 struct ast_timer *ast_channel_timer(const struct ast_channel *chan)
793 {
794         return chan->timer;
795 }
796 void ast_channel_timer_set(struct ast_channel *chan, struct ast_timer *value)
797 {
798         chan->timer = value;
799 }
800 struct ast_tone_zone *ast_channel_zone(const struct ast_channel *chan)
801 {
802         return chan->zone;
803 }
804 void ast_channel_zone_set(struct ast_channel *chan, struct ast_tone_zone *value)
805 {
806         chan->zone = value;
807 }
808 struct ast_trans_pvt *ast_channel_readtrans(const struct ast_channel *chan)
809 {
810         return chan->readtrans;
811 }
812 void ast_channel_readtrans_set(struct ast_channel *chan, struct ast_trans_pvt *value)
813 {
814         chan->readtrans = value;
815 }
816 struct ast_trans_pvt *ast_channel_writetrans(const struct ast_channel *chan)
817 {
818         return chan->writetrans;
819 }
820 void ast_channel_writetrans_set(struct ast_channel *chan, struct ast_trans_pvt *value)
821 {
822         chan->writetrans = value;
823 }
824 const struct ast_channel_tech *ast_channel_tech(const struct ast_channel *chan)
825 {
826         return chan->tech;
827 }
828 void ast_channel_tech_set(struct ast_channel *chan, const struct ast_channel_tech *value)
829 {
830         chan->tech = value;
831 }
832 enum ast_channel_adsicpe ast_channel_adsicpe(const struct ast_channel *chan)
833 {
834         return chan->adsicpe;
835 }
836 void ast_channel_adsicpe_set(struct ast_channel *chan, enum ast_channel_adsicpe value)
837 {
838         chan->adsicpe = value;
839 }
840 enum ast_channel_state ast_channel_state(const struct ast_channel *chan)
841 {
842         return chan->state;
843 }
844 struct ast_callid *ast_channel_callid(const struct ast_channel *chan)
845 {
846         if (chan->callid) {
847                 ast_callid_ref(chan->callid);
848                 return chan->callid;
849         }
850         return NULL;
851 }
852 void ast_channel_callid_set(struct ast_channel *chan, struct ast_callid *callid)
853 {
854         char call_identifier_from[AST_CALLID_BUFFER_LENGTH];
855         char call_identifier_to[AST_CALLID_BUFFER_LENGTH];
856         call_identifier_from[0] = '\0';
857         ast_callid_strnprint(call_identifier_to, sizeof(call_identifier_to), callid);
858         if (chan->callid) {
859                 ast_callid_strnprint(call_identifier_from, sizeof(call_identifier_from), chan->callid);
860                 ast_debug(3, "Channel Call ID changing from %s to %s\n", call_identifier_from, call_identifier_to);
861                 /* unbind if already set */
862                 ast_callid_unref(chan->callid);
863         }
864
865         chan->callid = ast_callid_ref(callid);
866
867         ast_test_suite_event_notify("CallIDChange",
868                 "State: CallIDChange\r\n"
869                 "Channel: %s\r\n"
870                 "CallID: %s\r\n"
871                 "PriorCallID: %s",
872                 ast_channel_name(chan),
873                 call_identifier_to,
874                 call_identifier_from);
875
876 }
877 void ast_channel_state_set(struct ast_channel *chan, enum ast_channel_state value)
878 {
879         chan->state = value;
880 }
881 struct ast_format *ast_channel_oldwriteformat(struct ast_channel *chan)
882 {
883         return &chan->oldwriteformat;
884 }
885 struct ast_format *ast_channel_rawreadformat(struct ast_channel *chan)
886 {
887         return &chan->rawreadformat;
888 }
889 struct ast_format *ast_channel_rawwriteformat(struct ast_channel *chan)
890 {
891         return &chan->rawwriteformat;
892 }
893 struct ast_format *ast_channel_readformat(struct ast_channel *chan)
894 {
895         return &chan->readformat;
896 }
897 struct ast_format *ast_channel_writeformat(struct ast_channel *chan)
898 {
899         return &chan->writeformat;
900 }
901 struct ast_hangup_handler_list *ast_channel_hangup_handlers(struct ast_channel *chan)
902 {
903         return &chan->hangup_handlers;
904 }
905 struct ast_datastore_list *ast_channel_datastores(struct ast_channel *chan)
906 {
907         return &chan->datastores;
908 }
909 struct ast_autochan_list *ast_channel_autochans(struct ast_channel *chan)
910 {
911         return &chan->autochans;
912 }
913 struct ast_readq_list *ast_channel_readq(struct ast_channel *chan)
914 {
915         return &chan->readq;
916 }
917 struct ast_frame *ast_channel_dtmff(struct ast_channel *chan)
918 {
919         return &chan->dtmff;
920 }
921 struct ast_jb *ast_channel_jb(struct ast_channel *chan)
922 {
923         return &chan->jb;
924 }
925 struct ast_party_caller *ast_channel_caller(struct ast_channel *chan)
926 {
927         return &chan->caller;
928 }
929 struct ast_party_connected_line *ast_channel_connected(struct ast_channel *chan)
930 {
931         return &chan->connected;
932 }
933 struct ast_party_id ast_channel_connected_effective_id(struct ast_channel *chan)
934 {
935         return ast_party_id_merge(&chan->connected.id, &chan->connected.priv);
936 }
937 struct ast_party_dialed *ast_channel_dialed(struct ast_channel *chan)
938 {
939         return &chan->dialed;
940 }
941 struct ast_party_redirecting *ast_channel_redirecting(struct ast_channel *chan)
942 {
943         return &chan->redirecting;
944 }
945 struct ast_party_id ast_channel_redirecting_effective_orig(struct ast_channel *chan)
946 {
947         return ast_party_id_merge(&chan->redirecting.orig, &chan->redirecting.priv_orig);
948 }
949 struct ast_party_id ast_channel_redirecting_effective_from(struct ast_channel *chan)
950 {
951         return ast_party_id_merge(&chan->redirecting.from, &chan->redirecting.priv_from);
952 }
953 struct ast_party_id ast_channel_redirecting_effective_to(struct ast_channel *chan)
954 {
955         return ast_party_id_merge(&chan->redirecting.to, &chan->redirecting.priv_to);
956 }
957 struct timeval *ast_channel_dtmf_tv(struct ast_channel *chan)
958 {
959         return &chan->dtmf_tv;
960 }
961 struct timeval *ast_channel_whentohangup(struct ast_channel *chan)
962 {
963         return &chan->whentohangup;
964 }
965 struct varshead *ast_channel_varshead(struct ast_channel *chan)
966 {
967         return &chan->varshead;
968 }
969 void ast_channel_dtmff_set(struct ast_channel *chan, struct ast_frame *value)
970 {
971         chan->dtmff = *value;
972 }
973 void ast_channel_jb_set(struct ast_channel *chan, struct ast_jb *value)
974 {
975         chan->jb = *value;
976 }
977 void ast_channel_caller_set(struct ast_channel *chan, struct ast_party_caller *value)
978 {
979         chan->caller = *value;
980 }
981 void ast_channel_connected_set(struct ast_channel *chan, struct ast_party_connected_line *value)
982 {
983         chan->connected = *value;
984 }
985 void ast_channel_dialed_set(struct ast_channel *chan, struct ast_party_dialed *value)
986 {
987         chan->dialed = *value;
988 }
989 void ast_channel_redirecting_set(struct ast_channel *chan, struct ast_party_redirecting *value)
990 {
991         chan->redirecting = *value;
992 }
993 void ast_channel_dtmf_tv_set(struct ast_channel *chan, struct timeval *value)
994 {
995         chan->dtmf_tv = *value;
996 }
997 void ast_channel_whentohangup_set(struct ast_channel *chan, struct timeval *value)
998 {
999         chan->whentohangup = *value;
1000 }
1001 void ast_channel_varshead_set(struct ast_channel *chan, struct varshead *value)
1002 {
1003         chan->varshead = *value;
1004 }
1005 struct timeval ast_channel_creationtime(struct ast_channel *chan)
1006 {
1007         return chan->creationtime;
1008 }
1009 void ast_channel_creationtime_set(struct ast_channel *chan, struct timeval *value)
1010 {
1011         chan->creationtime = *value;
1012 }
1013
1014 /* Evil softhangup accessors */
1015 int ast_channel_softhangup_internal_flag(struct ast_channel *chan)
1016 {
1017         return chan->softhangup;
1018 }
1019 void ast_channel_softhangup_internal_flag_set(struct ast_channel *chan, int value)
1020 {
1021         chan->softhangup = value;
1022 }
1023 void ast_channel_softhangup_internal_flag_add(struct ast_channel *chan, int value)
1024 {
1025         chan->softhangup |= value;
1026 }
1027 void ast_channel_softhangup_internal_flag_clear(struct ast_channel *chan, int value)
1028 {
1029         chan ->softhangup &= ~value;
1030 }
1031
1032 void ast_channel_callid_cleanup(struct ast_channel *chan)
1033 {
1034         if (chan->callid) {
1035                 chan->callid = ast_callid_unref(chan->callid);
1036         }
1037 }
1038
1039 /* Typedef accessors */
1040 ast_group_t ast_channel_callgroup(const struct ast_channel *chan)
1041 {
1042         return chan->callgroup;
1043 }
1044 void ast_channel_callgroup_set(struct ast_channel *chan, ast_group_t value)
1045 {
1046         chan->callgroup = value;
1047 }
1048 ast_group_t ast_channel_pickupgroup(const struct ast_channel *chan)
1049 {
1050         return chan->pickupgroup;
1051 }
1052 void ast_channel_pickupgroup_set(struct ast_channel *chan, ast_group_t value)
1053 {
1054         chan->pickupgroup = value;
1055 }
1056 struct ast_namedgroups *ast_channel_named_callgroups(const struct ast_channel *chan)
1057 {
1058         return chan->named_callgroups;
1059 }
1060 void ast_channel_named_callgroups_set(struct ast_channel *chan, struct ast_namedgroups *value)
1061 {
1062         ast_unref_namedgroups(chan->named_callgroups);
1063         chan->named_callgroups = ast_ref_namedgroups(value);
1064 }
1065 struct ast_namedgroups *ast_channel_named_pickupgroups(const struct ast_channel *chan)
1066 {
1067         return chan->named_pickupgroups;
1068 }
1069 void ast_channel_named_pickupgroups_set(struct ast_channel *chan, struct ast_namedgroups *value)
1070 {
1071         ast_unref_namedgroups(chan->named_pickupgroups);
1072         chan->named_pickupgroups = ast_ref_namedgroups(value);
1073 }
1074
1075 /* Alertpipe functions */
1076 int ast_channel_alert_write(struct ast_channel *chan)
1077 {
1078         char blah = 0x7F;
1079         return ast_channel_alert_writable(chan) && write(chan->alertpipe[1], &blah, sizeof(blah)) != sizeof(blah);
1080 }
1081
1082 ast_alert_status_t ast_channel_internal_alert_read(struct ast_channel *chan)
1083 {
1084         int flags;
1085         char blah;
1086
1087         if (!ast_channel_internal_alert_readable(chan)) {
1088                 return AST_ALERT_NOT_READABLE;
1089         }
1090
1091         flags = fcntl(chan->alertpipe[0], F_GETFL);
1092         /* For some odd reason, the alertpipe occasionally loses nonblocking status,
1093          * which immediately causes a deadlock scenario.  Detect and prevent this. */
1094         if ((flags & O_NONBLOCK) == 0) {
1095                 ast_log(LOG_ERROR, "Alertpipe on channel %s lost O_NONBLOCK?!!\n", ast_channel_name(chan));
1096                 if (fcntl(chan->alertpipe[0], F_SETFL, flags | O_NONBLOCK) < 0) {
1097                         ast_log(LOG_WARNING, "Unable to set alertpipe nonblocking! (%d: %s)\n", errno, strerror(errno));
1098                         return AST_ALERT_READ_FATAL;
1099                 }
1100         }
1101         if (read(chan->alertpipe[0], &blah, sizeof(blah)) < 0) {
1102                 if (errno != EINTR && errno != EAGAIN) {
1103                         ast_log(LOG_WARNING, "read() failed: %s\n", strerror(errno));
1104                         return AST_ALERT_READ_FAIL;
1105                 }
1106         }
1107
1108         return AST_ALERT_READ_SUCCESS;
1109 }
1110
1111 int ast_channel_alert_writable(struct ast_channel *chan)
1112 {
1113         return chan->alertpipe[1] > -1;
1114 }
1115
1116 int ast_channel_internal_alert_readable(struct ast_channel *chan)
1117 {
1118         return chan->alertpipe[0] > -1;
1119 }
1120
1121 void ast_channel_internal_alertpipe_clear(struct ast_channel *chan)
1122 {
1123         chan->alertpipe[0] = chan->alertpipe[1] = -1;
1124 }
1125
1126 void ast_channel_internal_alertpipe_close(struct ast_channel *chan)
1127 {
1128         if (ast_channel_internal_alert_readable(chan)) {
1129                 close(chan->alertpipe[0]);
1130         }
1131         if (ast_channel_alert_writable(chan)) {
1132                 close(chan->alertpipe[1]);
1133         }
1134 }
1135
1136 int ast_channel_internal_alertpipe_init(struct ast_channel *chan)
1137 {
1138         if (pipe(chan->alertpipe)) {
1139                 ast_log(LOG_WARNING, "Channel allocation failed: Can't create alert pipe! Try increasing max file descriptors with ulimit -n\n");
1140                 return -1;
1141         } else {
1142                 int flags = fcntl(chan->alertpipe[0], F_GETFL);
1143                 if (fcntl(chan->alertpipe[0], F_SETFL, flags | O_NONBLOCK) < 0) {
1144                         ast_log(LOG_WARNING, "Channel allocation failed: Unable to set alertpipe nonblocking! (%d: %s)\n", errno, strerror(errno));
1145                         return -1;
1146                 }
1147                 flags = fcntl(chan->alertpipe[1], F_GETFL);
1148                 if (fcntl(chan->alertpipe[1], F_SETFL, flags | O_NONBLOCK) < 0) {
1149                         ast_log(LOG_WARNING, "Channel allocation failed: Unable to set alertpipe nonblocking! (%d: %s)\n", errno, strerror(errno));
1150                         return -1;
1151                 }
1152         }
1153         return 0;
1154 }
1155
1156 int ast_channel_internal_alert_readfd(struct ast_channel *chan)
1157 {
1158         return chan->alertpipe[0];
1159 }
1160
1161 void ast_channel_internal_alertpipe_swap(struct ast_channel *chan1, struct ast_channel *chan2)
1162 {
1163         int i;
1164         for (i = 0; i < ARRAY_LEN(chan1->alertpipe); i++) {
1165                 SWAP(chan1->alertpipe[i], chan2->alertpipe[i]);
1166         }
1167 }
1168
1169 /* file descriptor array accessors */
1170 void ast_channel_internal_fd_set(struct ast_channel *chan, int which, int value)
1171 {
1172         chan->fds[which] = value;
1173 }
1174 void ast_channel_internal_fd_clear(struct ast_channel *chan, int which)
1175 {
1176         ast_channel_internal_fd_set(chan, which, -1);
1177 }
1178 void ast_channel_internal_fd_clear_all(struct ast_channel *chan)
1179 {
1180         int i;
1181         for (i = 0; i < AST_MAX_FDS; i++) {
1182                 ast_channel_internal_fd_clear(chan, i);
1183         }
1184 }
1185 int ast_channel_fd(const struct ast_channel *chan, int which)
1186 {
1187         return chan->fds[which];
1188 }
1189 int ast_channel_fd_isset(const struct ast_channel *chan, int which)
1190 {
1191         return ast_channel_fd(chan, which) > -1;
1192 }
1193
1194 #ifdef HAVE_EPOLL
1195 struct ast_epoll_data *ast_channel_internal_epfd_data(const struct ast_channel *chan, int which)
1196 {
1197         return chan->epfd_data[which];
1198 }
1199 void ast_channel_internal_epfd_data_set(struct ast_channel *chan, int which , struct ast_epoll_data *value)
1200 {
1201         chan->epfd_data[which] = value;
1202 }
1203 #endif
1204
1205 pthread_t ast_channel_blocker(const struct ast_channel *chan)
1206 {
1207         return chan->blocker;
1208 }
1209 void ast_channel_blocker_set(struct ast_channel *chan, pthread_t value)
1210 {
1211         chan->blocker = value;
1212 }
1213
1214 ast_timing_func_t ast_channel_timingfunc(const struct ast_channel *chan)
1215 {
1216         return chan->timingfunc;
1217 }
1218 void ast_channel_timingfunc_set(struct ast_channel *chan, ast_timing_func_t value)
1219 {
1220         chan->timingfunc = value;
1221 }
1222
1223 struct ast_bridge *ast_channel_internal_bridge(const struct ast_channel *chan)
1224 {
1225         return chan->bridge;
1226 }
1227 void ast_channel_internal_bridge_set(struct ast_channel *chan, struct ast_bridge *value)
1228 {
1229         chan->bridge = value;
1230 }
1231
1232 struct ast_channel *ast_channel_internal_bridged_channel(const struct ast_channel *chan)
1233 {
1234         return chan->bridged_channel;
1235 }
1236 void ast_channel_internal_bridged_channel_set(struct ast_channel *chan, struct ast_channel *value)
1237 {
1238         chan->bridged_channel = value;
1239 }
1240
1241 struct ast_flags *ast_channel_flags(struct ast_channel *chan)
1242 {
1243         return &chan->flags;
1244 }
1245
1246 static int collect_names_cb(void *obj, void *arg, int flags) {
1247         struct ast_control_pvt_cause_code *cause_code = obj;
1248         struct ast_str **str = arg;
1249
1250         ast_str_append(str, 0, "%s%s", (ast_str_strlen(*str) ? "," : ""), cause_code->chan_name);
1251
1252         return 0;
1253 }
1254
1255 struct ast_str *ast_channel_dialed_causes_channels(const struct ast_channel *chan)
1256 {
1257         struct ast_str *chanlist = ast_str_create(128);
1258
1259         if (!chanlist) {
1260                 return NULL;
1261         }
1262
1263         ao2_callback(chan->dialed_causes, 0, collect_names_cb, &chanlist);
1264
1265         return chanlist;
1266 }
1267
1268 struct ast_control_pvt_cause_code *ast_channel_dialed_causes_find(const struct ast_channel *chan, const char *chan_name)
1269 {
1270         return ao2_find(chan->dialed_causes, chan_name, OBJ_KEY);
1271 }
1272
1273 int ast_channel_dialed_causes_add(const struct ast_channel *chan, const struct ast_control_pvt_cause_code *cause_code, int datalen)
1274 {
1275         struct ast_control_pvt_cause_code *ao2_cause_code;
1276         ao2_find(chan->dialed_causes, cause_code->chan_name, OBJ_KEY | OBJ_UNLINK | OBJ_NODATA);
1277         ao2_cause_code = ao2_alloc(datalen, NULL);
1278
1279         if (ao2_cause_code) {
1280                 memcpy(ao2_cause_code, cause_code, datalen);
1281                 ao2_link(chan->dialed_causes, ao2_cause_code);
1282                 ao2_ref(ao2_cause_code, -1);
1283                 return 0;
1284         } else {
1285                 return -1;
1286         }
1287 }
1288
1289 void ast_channel_dialed_causes_clear(const struct ast_channel *chan)
1290 {
1291         ao2_callback(chan->dialed_causes, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, NULL, NULL);
1292 }
1293
1294 /* \brief Hash function for pvt cause code frames */
1295 static int pvt_cause_hash_fn(const void *vpc, const int flags)
1296 {
1297         const struct ast_control_pvt_cause_code *pc = vpc;
1298         return ast_str_hash(ast_tech_to_upper(ast_strdupa(pc->chan_name)));
1299 }
1300
1301 /* \brief Comparison function for pvt cause code frames */
1302 static int pvt_cause_cmp_fn(void *obj, void *vstr, int flags)
1303 {
1304         struct ast_control_pvt_cause_code *pc = obj;
1305         char *str = ast_tech_to_upper(ast_strdupa(vstr));
1306         char *pc_str = ast_tech_to_upper(ast_strdupa(pc->chan_name));
1307         return !strcmp(pc_str, str) ? CMP_MATCH | CMP_STOP : 0;
1308 }
1309
1310 #define DIALED_CAUSES_BUCKETS 37
1311
1312 struct ast_channel *__ast_channel_internal_alloc(void (*destructor)(void *obj), const char *file, int line, const char *function)
1313 {
1314         struct ast_channel *tmp;
1315 #if defined(REF_DEBUG)
1316         tmp = __ao2_alloc_debug(sizeof(*tmp), destructor,
1317                 AO2_ALLOC_OPT_LOCK_MUTEX, "", file, line, function, 1);
1318 #elif defined(__AST_DEBUG_MALLOC)
1319         tmp = __ao2_alloc_debug(sizeof(*tmp), destructor,
1320                 AO2_ALLOC_OPT_LOCK_MUTEX, "", file, line, function, 0);
1321 #else
1322         tmp = ao2_alloc(sizeof(*tmp), destructor);
1323 #endif
1324
1325         if ((ast_string_field_init(tmp, 128))) {
1326                 return ast_channel_unref(tmp);
1327         }
1328
1329         if (!(tmp->dialed_causes = ao2_container_alloc(DIALED_CAUSES_BUCKETS, pvt_cause_hash_fn, pvt_cause_cmp_fn))) {
1330                 return ast_channel_unref(tmp);
1331         }
1332
1333         return tmp;
1334 }
1335
1336 void ast_channel_internal_cleanup(struct ast_channel *chan)
1337 {
1338         if (chan->dialed_causes) {
1339                 ao2_t_ref(chan->dialed_causes, -1,
1340                         "done with dialed causes since the channel is going away");
1341                 chan->dialed_causes = NULL;
1342         }
1343
1344         ast_string_field_free_memory(chan);
1345 }
1346
1347 void ast_channel_internal_finalize(struct ast_channel *chan)
1348 {
1349         chan->finalized = 1;
1350 }
1351
1352 int ast_channel_internal_is_finalized(struct ast_channel *chan)
1353 {
1354         return chan->finalized;
1355 }