ast_channel opaquification of pointers and integral types
[asterisk/asterisk.git] / funcs / func_channel.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2006, Digium, Inc.
5  *
6  * See http://www.asterisk.org for more information about
7  * the Asterisk project. Please do not directly contact
8  * any of the maintainers of this project for assistance;
9  * the project provides a web site, mailing lists and IRC
10  * channels for your use.
11  *
12  * This program is free software, distributed under the terms of
13  * the GNU General Public License Version 2. See the LICENSE file
14  * at the top of the source tree.
15  */
16
17 /*! \file
18  *
19  * \brief Channel info dialplan functions
20  *
21  * \author Kevin P. Fleming <kpfleming@digium.com>
22  * \author Ben Winslow
23  * 
24  * \ingroup functions
25  */
26
27 /*** MODULEINFO
28         <support_level>core</support_level>
29  ***/
30
31 #include "asterisk.h"
32
33 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
34
35 #include <regex.h>
36 #include <ctype.h>
37
38 #include "asterisk/module.h"
39 #include "asterisk/channel.h"
40 #include "asterisk/pbx.h"
41 #include "asterisk/utils.h"
42 #include "asterisk/app.h"
43 #include "asterisk/indications.h"
44 #include "asterisk/stringfields.h"
45 #include "asterisk/global_datastores.h"
46
47 /*** DOCUMENTATION
48         <function name="CHANNELS" language="en_US">
49                 <synopsis>
50                         Gets the list of channels, optionally filtering by a regular expression.
51                 </synopsis>
52                 <syntax>
53                         <parameter name="regular_expression" />
54                 </syntax>
55                 <description>
56                         <para>Gets the list of channels, optionally filtering by a <replaceable>regular_expression</replaceable>. If
57                         no argument is provided, all known channels are returned. The
58                         <replaceable>regular_expression</replaceable> must correspond to
59                         the POSIX.2 specification, as shown in <emphasis>regex(7)</emphasis>. The list returned
60                         will be space-delimited.</para>
61                 </description>
62         </function>
63         <function name="MASTER_CHANNEL" language="en_US">
64                 <synopsis>
65                         Gets or sets variables on the master channel
66                 </synopsis>
67                 <description>
68                         <para>Allows access to the channel which created the current channel, if any.  If the channel is already
69                         a master channel, then accesses local channel variables.</para>
70                 </description>
71         </function>
72         <function name="CHANNEL" language="en_US">
73                 <synopsis>
74                         Gets/sets various pieces of information about the channel.
75                 </synopsis>
76                 <syntax>
77                         <parameter name="item" required="true">
78                                 <para>Standard items (provided by all channel technologies) are:</para>
79                                 <enumlist>
80                                         <enum name="audioreadformat">
81                                                 <para>R/O format currently being read.</para>
82                                         </enum>
83                                         <enum name="audionativeformat">
84                                                 <para>R/O format used natively for audio.</para>
85                                         </enum>
86                                         <enum name="audiowriteformat">
87                                                 <para>R/O format currently being written.</para>
88                                         </enum>
89                                         <enum name="callgroup">
90                                                 <para>R/W call groups for call pickup.</para>
91                                         </enum>
92                                         <enum name="pickupgroup">
93                                                 <para>R/W call groups for call pickup.</para>
94                                         </enum>
95                                         <enum name="channeltype">
96                                                 <para>R/O technology used for channel.</para>
97                                         </enum>
98                                         <enum name="checkhangup">
99                                                 <para>R/O Whether the channel is hanging up (1/0)</para>
100                                         </enum>
101                                         <enum name="language">
102                                                 <para>R/W language for sounds played.</para>
103                                         </enum>
104                                         <enum name="musicclass">
105                                                 <para>R/W class (from musiconhold.conf) for hold music.</para>
106                                         </enum>
107                                         <enum name="name">
108                                                 <para>The name of the channel</para>
109                                         </enum>
110                                         <enum name="parkinglot">
111                                                 <para>R/W parkinglot for parking.</para>
112                                         </enum>
113                                         <enum name="rxgain">
114                                                 <para>R/W set rxgain level on channel drivers that support it.</para>
115                                         </enum>
116                                         <enum name="secure_bridge_signaling">
117                                                 <para>Whether or not channels bridged to this channel require secure signaling</para>
118                                         </enum>
119                                         <enum name="secure_bridge_media">
120                                                 <para>Whether or not channels bridged to this channel require secure media</para>
121                                         </enum>
122                                         <enum name="state">
123                                                 <para>R/O state for channel</para>
124                                         </enum>
125                                         <enum name="tonezone">
126                                                 <para>R/W zone for indications played</para>
127                                         </enum>
128                                         <enum name="transfercapability">
129                                                 <para>R/W ISDN Transfer Capability, one of:</para>
130                                                 <enumlist>
131                                                         <enum name="SPEECH" />
132                                                         <enum name="DIGITAL" />
133                                                         <enum name="RESTRICTED_DIGITAL" />
134                                                         <enum name="3K1AUDIO" />
135                                                         <enum name="DIGITAL_W_TONES" />
136                                                         <enum name="VIDEO" />
137                                                 </enumlist>
138                                         </enum>
139                                         <enum name="txgain">
140                                                 <para>R/W set txgain level on channel drivers that support it.</para>
141                                         </enum>
142                                         <enum name="videonativeformat">
143                                                 <para>R/O format used natively for video</para>
144                                         </enum>
145                                         <enum name="trace">
146                                                 <para>R/W whether or not context tracing is enabled, only available
147                                                 <emphasis>if CHANNEL_TRACE is defined</emphasis>.</para>
148                                         </enum>
149                                 </enumlist>
150                                 <para><emphasis>chan_sip</emphasis> provides the following additional options:</para>
151                                 <enumlist>
152                                         <enum name="peerip">
153                                                 <para>R/O Get the IP address of the peer.</para>
154                                         </enum>
155                                         <enum name="recvip">
156                                                 <para>R/O Get the source IP address of the peer.</para>
157                                         </enum>
158                                         <enum name="from">
159                                                 <para>R/O Get the URI from the From: header.</para>
160                                         </enum>
161                                         <enum name="uri">
162                                                 <para>R/O Get the URI from the Contact: header.</para>
163                                         </enum>
164                                         <enum name="useragent">
165                                                 <para>R/O Get the useragent.</para>
166                                         </enum>
167                                         <enum name="peername">
168                                                 <para>R/O Get the name of the peer.</para>
169                                         </enum>
170                                         <enum name="t38passthrough">
171                                                 <para>R/O <literal>1</literal> if T38 is offered or enabled in this channel,
172                                                 otherwise <literal>0</literal></para>
173                                         </enum>
174                                         <enum name="rtpqos">
175                                                 <para>R/O Get QOS information about the RTP stream</para>
176                                                 <para>    This option takes two additional arguments:</para>
177                                                 <para>    Argument 1:</para>
178                                                 <para>     <literal>audio</literal>             Get data about the audio stream</para>
179                                                 <para>     <literal>video</literal>             Get data about the video stream</para>
180                                                 <para>     <literal>text</literal>              Get data about the text stream</para>
181                                                 <para>    Argument 2:</para>
182                                                 <para>     <literal>local_ssrc</literal>        Local SSRC (stream ID)</para>
183                                                 <para>     <literal>local_lostpackets</literal> Local lost packets</para>
184                                                 <para>     <literal>local_jitter</literal>      Local calculated jitter</para>
185                                                 <para>     <literal>local_maxjitter</literal>   Local calculated jitter (maximum)</para>
186                                                 <para>     <literal>local_minjitter</literal>   Local calculated jitter (minimum)</para>
187                                                 <para>     <literal>local_normdevjitter</literal>Local calculated jitter (normal deviation)</para>
188                                                 <para>     <literal>local_stdevjitter</literal> Local calculated jitter (standard deviation)</para>
189                                                 <para>     <literal>local_count</literal>       Number of received packets</para>
190                                                 <para>     <literal>remote_ssrc</literal>       Remote SSRC (stream ID)</para>
191                                                 <para>     <literal>remote_lostpackets</literal>Remote lost packets</para>
192                                                 <para>     <literal>remote_jitter</literal>     Remote reported jitter</para>
193                                                 <para>     <literal>remote_maxjitter</literal>  Remote calculated jitter (maximum)</para>
194                                                 <para>     <literal>remote_minjitter</literal>  Remote calculated jitter (minimum)</para>
195                                                 <para>     <literal>remote_normdevjitter</literal>Remote calculated jitter (normal deviation)</para>
196                                                 <para>     <literal>remote_stdevjitter</literal>Remote calculated jitter (standard deviation)</para>
197                                                 <para>     <literal>remote_count</literal>      Number of transmitted packets</para>
198                                                 <para>     <literal>rtt</literal>               Round trip time</para>
199                                                 <para>     <literal>maxrtt</literal>            Round trip time (maximum)</para>
200                                                 <para>     <literal>minrtt</literal>            Round trip time (minimum)</para>
201                                                 <para>     <literal>normdevrtt</literal>        Round trip time (normal deviation)</para>
202                                                 <para>     <literal>stdevrtt</literal>          Round trip time (standard deviation)</para>
203                                                 <para>     <literal>all</literal>               All statistics (in a form suited to logging,
204                                                 but not for parsing)</para>
205                                         </enum>
206                                         <enum name="rtpdest">
207                                                 <para>R/O Get remote RTP destination information.</para>
208                                                 <para>   This option takes one additional argument:</para>
209                                                 <para>    Argument 1:</para>
210                                                 <para>     <literal>audio</literal>             Get audio destination</para>
211                                                 <para>     <literal>video</literal>             Get video destination</para>
212                                                 <para>     <literal>text</literal>              Get text destination</para>
213                                         </enum>
214                                 </enumlist>
215                                 <para><emphasis>chan_iax2</emphasis> provides the following additional options:</para>
216                                 <enumlist>
217                                         <enum name="peerip">
218                                                 <para>R/O Get the peer's ip address.</para>
219                                         </enum>
220                                         <enum name="peername">
221                                                 <para>R/O Get the peer's username.</para>
222                                         </enum>
223                                 </enumlist>
224                                 <para><emphasis>chan_dahdi</emphasis> provides the following additional options:</para>
225                                 <enumlist>
226                                         <enum name="dahdi_channel">
227                                                 <para>R/O DAHDI channel related to this channel.</para>
228                                         </enum>
229                                         <enum name="dahdi_span">
230                                                 <para>R/O DAHDI span related to this channel.</para>
231                                         </enum>
232                                         <enum name="dahdi_type">
233                                                 <para>R/O DAHDI channel type, one of:</para>
234                                                 <enumlist>
235                                                         <enum name="analog" />
236                                                         <enum name="mfc/r2" />
237                                                         <enum name="pri" />
238                                                         <enum name="pseudo" />
239                                                         <enum name="ss7" />
240                                                 </enumlist>
241                                         </enum>
242                                         <enum name="keypad_digits">
243                                                 <para>R/O PRI Keypad digits that came in with the SETUP message.</para>
244                                         </enum>
245                                         <enum name="reversecharge">
246                                                 <para>R/O PRI Reverse Charging Indication, one of:</para>
247                                                 <enumlist>
248                                                         <enum name="-1"> <para>None</para></enum>
249                                                         <enum name=" 1"> <para>Reverse Charging Requested</para></enum>
250                                                 </enumlist>
251                                         </enum>
252                                         <enum name="no_media_path">
253                                                 <para>R/O PRI Nonzero if the channel has no B channel.
254                                                 The channel is either on hold or a call waiting call.</para>
255                                         </enum>
256                                 </enumlist>
257                                 <para><emphasis>chan_ooh323</emphasis> provides the following additional options:</para>
258                                 <enumlist>
259                                         <enum name="faxdetect">
260                                                 <para>Fax Detect [R/W]</para>
261                                                 <para>Returns 0 or 1</para>
262                                                 <para>Write yes or no</para>
263                                         </enum>
264                                         <enum name="t38support">
265                                                 <para>t38support [R/W]</para>
266                                                 <para>Returns 0 or 1</para>
267                                                 <para>Write yes or no</para>
268                                         </enum>
269                                         <enum name="h323id">
270                                                 <para>Returns h323id [R]</para>
271                                         </enum>
272                                 </enumlist>
273                         </parameter>
274                 </syntax>
275                 <description>
276                         <para>Gets/sets various pieces of information about the channel, additional <replaceable>item</replaceable> may
277                         be available from the channel driver; see its documentation for details. Any <replaceable>item</replaceable>
278                         requested that is not available on the current channel will return an empty string.</para>
279                 </description>
280         </function>
281  ***/
282
283 #define locked_copy_string(chan, dest, source, len) \
284         do { \
285                 ast_channel_lock(chan); \
286                 ast_copy_string(dest, source, len); \
287                 ast_channel_unlock(chan); \
288         } while (0)
289 #define locked_string_field_set(chan, field, source) \
290         do { \
291                 ast_channel_lock(chan); \
292                 ast_channel_##field##_set(chan, source); \
293                 ast_channel_unlock(chan); \
294         } while (0)
295
296 static const char * const transfercapability_table[0x20] = {
297         "SPEECH", "UNK", "UNK", "UNK", "UNK", "UNK", "UNK", "UNK",
298         "DIGITAL", "RESTRICTED_DIGITAL", "UNK", "UNK", "UNK", "UNK", "UNK", "UNK",
299         "3K1AUDIO", "DIGITAL_W_TONES", "UNK", "UNK", "UNK", "UNK", "UNK", "UNK",
300         "VIDEO", "UNK", "UNK", "UNK", "UNK", "UNK", "UNK", "UNK", };
301
302 static int func_channel_read(struct ast_channel *chan, const char *function,
303                              char *data, char *buf, size_t len)
304 {
305         int ret = 0;
306         char tmp[512];
307         struct ast_format_cap *tmpcap;
308
309         if (!strcasecmp(data, "audionativeformat")) {
310                 if ((tmpcap = ast_format_cap_get_type(ast_channel_nativeformats(chan), AST_FORMAT_TYPE_AUDIO))) {
311                         ast_copy_string(buf, ast_getformatname_multiple(tmp, sizeof(tmp), tmpcap), len);
312                         tmpcap = ast_format_cap_destroy(tmpcap);
313                 }
314         } else if (!strcasecmp(data, "videonativeformat")) {
315                 if ((tmpcap = ast_format_cap_get_type(ast_channel_nativeformats(chan), AST_FORMAT_TYPE_VIDEO))) {
316                         ast_copy_string(buf, ast_getformatname_multiple(tmp, sizeof(tmp), tmpcap), len);
317                         tmpcap = ast_format_cap_destroy(tmpcap);
318                 }
319         } else if (!strcasecmp(data, "audioreadformat")) {
320                 ast_copy_string(buf, ast_getformatname(&chan->readformat), len);
321         } else if (!strcasecmp(data, "audiowriteformat")) {
322                 ast_copy_string(buf, ast_getformatname(&chan->writeformat), len);
323 #ifdef CHANNEL_TRACE
324         } else if (!strcasecmp(data, "trace")) {
325                 ast_channel_lock(chan);
326                 ast_copy_string(buf, ast_channel_trace_is_enabled(chan) ? "1" : "0", len);
327                 ast_channel_unlock(chan);
328 #endif
329         } else if (!strcasecmp(data, "tonezone") && ast_channel_zone(chan))
330                 locked_copy_string(chan, buf, ast_channel_zone(chan)->country, len);
331         else if (!strcasecmp(data, "language"))
332                 locked_copy_string(chan, buf, ast_channel_language(chan), len);
333         else if (!strcasecmp(data, "musicclass"))
334                 locked_copy_string(chan, buf, ast_channel_musicclass(chan), len);
335         else if (!strcasecmp(data, "name")) {
336                 locked_copy_string(chan, buf, ast_channel_name(chan), len);
337         } else if (!strcasecmp(data, "parkinglot"))
338                 locked_copy_string(chan, buf, ast_channel_parkinglot(chan), len);
339         else if (!strcasecmp(data, "state"))
340                 locked_copy_string(chan, buf, ast_state2str(ast_channel_state(chan)), len);
341         else if (!strcasecmp(data, "channeltype"))
342                 locked_copy_string(chan, buf, ast_channel_tech(chan)->type, len);
343         else if (!strcasecmp(data, "accountcode"))
344                 locked_copy_string(chan, buf, ast_channel_accountcode(chan), len);
345         else if (!strcasecmp(data, "checkhangup")) {
346                 ast_channel_lock(chan);
347                 ast_copy_string(buf, ast_check_hangup(chan) ? "1" : "0", len);
348                 ast_channel_unlock(chan);
349         } else if (!strcasecmp(data, "peeraccount"))
350                 locked_copy_string(chan, buf, ast_channel_peeraccount(chan), len);
351         else if (!strcasecmp(data, "hangupsource"))
352                 locked_copy_string(chan, buf, ast_channel_hangupsource(chan), len);
353         else if (!strcasecmp(data, "appname") && ast_channel_appl(chan))
354                 locked_copy_string(chan, buf, ast_channel_appl(chan), len);
355         else if (!strcasecmp(data, "appdata") && ast_channel_data(chan))
356                 locked_copy_string(chan, buf, ast_channel_data(chan), len);
357         else if (!strcasecmp(data, "exten") && ast_channel_data(chan))
358                 locked_copy_string(chan, buf, ast_channel_exten(chan), len);
359         else if (!strcasecmp(data, "context") && ast_channel_data(chan))
360                 locked_copy_string(chan, buf, ast_channel_context(chan), len);
361         else if (!strcasecmp(data, "userfield") && ast_channel_data(chan))
362                 locked_copy_string(chan, buf, ast_channel_userfield(chan), len);
363         else if (!strcasecmp(data, "channame") && ast_channel_data(chan))
364                 locked_copy_string(chan, buf, ast_channel_name(chan), len);
365         else if (!strcasecmp(data, "linkedid")) {
366                 ast_channel_lock(chan);
367                 if (ast_strlen_zero(ast_channel_linkedid(chan))) {
368                         /* fall back on the channel's uniqueid if linkedid is unset */
369                         ast_copy_string(buf, ast_channel_uniqueid(chan), len);
370                 }
371                 else {
372                         ast_copy_string(buf, ast_channel_linkedid(chan), len);
373                 }
374                 ast_channel_unlock(chan);
375         } else if (!strcasecmp(data, "peer")) {
376                 struct ast_channel *p;
377                 ast_channel_lock(chan);
378                 p = ast_bridged_channel(chan);
379                 if (p || ast_channel_tech(chan) || ast_channel_cdr(chan)) /* dummy channel? if so, we hid the peer name in the language */
380                         ast_copy_string(buf, (p ? ast_channel_name(p) : ""), len);
381                 else {
382                         /* a dummy channel can still pass along bridged peer info via
383                            the BRIDGEPEER variable */
384                         const char *pname = pbx_builtin_getvar_helper(chan, "BRIDGEPEER");
385                         if (!ast_strlen_zero(pname))
386                                 ast_copy_string(buf, pname, len); /* a horrible kludge, but... how else? */
387                         else
388                                 buf[0] = 0;
389                 }
390                 ast_channel_unlock(chan);
391         } else if (!strcasecmp(data, "uniqueid")) {
392                 locked_copy_string(chan, buf, ast_channel_uniqueid(chan), len);
393         } else if (!strcasecmp(data, "transfercapability")) {
394                 locked_copy_string(chan, buf, transfercapability_table[ast_channel_transfercapability(chan) & 0x1f], len);
395         } else if (!strcasecmp(data, "callgroup")) {
396                 char groupbuf[256];
397                 locked_copy_string(chan, buf,  ast_print_group(groupbuf, sizeof(groupbuf), chan->callgroup), len);
398         } else if (!strcasecmp(data, "pickupgroup")) {
399                 char groupbuf[256];
400                 locked_copy_string(chan, buf,  ast_print_group(groupbuf, sizeof(groupbuf), chan->pickupgroup), len);
401         } else if (!strcasecmp(data, "amaflags")) {
402                 char amabuf[256];
403                 snprintf(amabuf,sizeof(amabuf), "%d", ast_channel_amaflags(chan));
404                 locked_copy_string(chan, buf, amabuf, len);
405         } else if (!strncasecmp(data, "secure_bridge_", 14)) {
406                 struct ast_datastore *ds;
407                 ast_channel_lock(chan);
408                 if ((ds = ast_channel_datastore_find(chan, &secure_call_info, NULL))) {
409                         struct ast_secure_call_store *encrypt = ds->data;
410                         if (!strcasecmp(data, "secure_bridge_signaling")) {
411                                 snprintf(buf, len, "%s", encrypt->signaling ? "1" : "");
412                         } else if (!strcasecmp(data, "secure_bridge_media")) {
413                                 snprintf(buf, len, "%s", encrypt->media ? "1" : "");
414                         }
415                 }
416                 ast_channel_unlock(chan);
417         } else if (!ast_channel_tech(chan) || !ast_channel_tech(chan)->func_channel_read || ast_channel_tech(chan)->func_channel_read(chan, function, data, buf, len)) {
418                 ast_log(LOG_WARNING, "Unknown or unavailable item requested: '%s'\n", data);
419                 ret = -1;
420         }
421
422         return ret;
423 }
424
425 static int func_channel_write_real(struct ast_channel *chan, const char *function,
426                               char *data, const char *value)
427 {
428         int ret = 0;
429         signed char gainset;
430
431         if (!strcasecmp(data, "language"))
432                 locked_string_field_set(chan, language, value);
433         else if (!strcasecmp(data, "parkinglot"))
434                 locked_string_field_set(chan, parkinglot, value);
435         else if (!strcasecmp(data, "musicclass"))
436                 locked_string_field_set(chan, musicclass, value);
437         else if (!strcasecmp(data, "accountcode"))
438                 locked_string_field_set(chan, accountcode, value);
439         else if (!strcasecmp(data, "userfield"))
440                 locked_string_field_set(chan, userfield, value);
441         else if (!strcasecmp(data, "amaflags")) {
442                 ast_channel_lock(chan);
443                 if(isdigit(*value)) {
444                         int amaflags;
445                         sscanf(value, "%30d", &amaflags);
446                         ast_channel_amaflags_set(chan, amaflags);
447                 } else if (!strcasecmp(value,"OMIT")){
448                         ast_channel_amaflags_set(chan, 1);
449                 } else if (!strcasecmp(value,"BILLING")){
450                         ast_channel_amaflags_set(chan, 2);
451                 } else if (!strcasecmp(value,"DOCUMENTATION")){
452                         ast_channel_amaflags_set(chan, 3);
453                 }
454                 ast_channel_unlock(chan);
455         } else if (!strcasecmp(data, "peeraccount"))
456                 locked_string_field_set(chan, peeraccount, value);
457         else if (!strcasecmp(data, "hangupsource"))
458                 /* XXX - should we be forcing this here? */
459                 ast_set_hangupsource(chan, value, 0);
460 #ifdef CHANNEL_TRACE
461         else if (!strcasecmp(data, "trace")) {
462                 ast_channel_lock(chan);
463                 if (ast_true(value)) 
464                         ret = ast_channel_trace_enable(chan);
465                 else if (ast_false(value))
466                         ret = ast_channel_trace_disable(chan);
467                 else {
468                         ret = -1;
469                         ast_log(LOG_WARNING, "Invalid value for CHANNEL(trace).");
470                 }
471                 ast_channel_unlock(chan);
472         }
473 #endif
474         else if (!strcasecmp(data, "tonezone")) {
475                 struct ast_tone_zone *new_zone;
476                 if (!(new_zone = ast_get_indication_zone(value))) {
477                         ast_log(LOG_ERROR, "Unknown country code '%s' for tonezone. Check indications.conf for available country codes.\n", value);
478                         ret = -1;       
479                 } else {
480                         ast_channel_lock(chan);
481                         if (ast_channel_zone(chan)) {
482                                 ast_channel_zone_set(chan, ast_tone_zone_unref(ast_channel_zone(chan)));
483                         }
484                         ast_channel_zone_set(chan, ast_tone_zone_ref(new_zone));
485                         ast_channel_unlock(chan);
486                         new_zone = ast_tone_zone_unref(new_zone);
487                 }
488         } else if (!strcasecmp(data, "callgroup")) {
489                 chan->callgroup = ast_get_group(value);
490         } else if (!strcasecmp(data, "pickupgroup")) {
491                 chan->pickupgroup = ast_get_group(value);
492         } else if (!strcasecmp(data, "txgain")) {
493                 sscanf(value, "%4hhd", &gainset);
494                 ast_channel_setoption(chan, AST_OPTION_TXGAIN, &gainset, sizeof(gainset), 0);
495         } else if (!strcasecmp(data, "rxgain")) {
496                 sscanf(value, "%4hhd", &gainset);
497                 ast_channel_setoption(chan, AST_OPTION_RXGAIN, &gainset, sizeof(gainset), 0);
498         } else if (!strcasecmp(data, "transfercapability")) {
499                 unsigned short i;
500                 for (i = 0; i < 0x20; i++) {
501                         if (!strcasecmp(transfercapability_table[i], value) && strcmp(value, "UNK")) {
502                                 ast_channel_transfercapability_set(chan, i);
503                                 break;
504                         }
505                 }
506         } else if (!strncasecmp(data, "secure_bridge_", 14)) {
507                 struct ast_datastore *ds;
508                 struct ast_secure_call_store *store;
509
510                 if (!chan || !value) {
511                         return -1;
512                 }
513
514                 ast_channel_lock(chan);
515                 if (!(ds = ast_channel_datastore_find(chan, &secure_call_info, NULL))) {
516                         if (!(ds = ast_datastore_alloc(&secure_call_info, NULL))) {
517                                 ast_channel_unlock(chan);
518                                 return -1;
519                         }
520                         if (!(store = ast_calloc(1, sizeof(*store)))) {
521                                 ast_channel_unlock(chan);
522                                 ast_free(ds);
523                                 return -1;
524                         }
525                         ds->data = store;
526                         ast_channel_datastore_add(chan, ds);
527                 } else {
528                         store = ds->data;
529                 }
530                 ast_channel_unlock(chan);
531
532                 if (!strcasecmp(data, "secure_bridge_signaling")) {
533                         store->signaling = ast_true(value) ? 1 : 0;
534                 } else if (!strcasecmp(data, "secure_bridge_media")) {
535                         store->media = ast_true(value) ? 1 : 0;
536                 }
537         } else if (!ast_channel_tech(chan)->func_channel_write
538                  || ast_channel_tech(chan)->func_channel_write(chan, function, data, value)) {
539                 ast_log(LOG_WARNING, "Unknown or unavailable item requested: '%s'\n",
540                                 data);
541                 ret = -1;
542         }
543
544         return ret;
545 }
546
547 static int func_channel_write(struct ast_channel *chan, const char *function, char *data, const char *value)
548 {
549         int res;
550         ast_chan_write_info_t write_info = {
551                 .version = AST_CHAN_WRITE_INFO_T_VERSION,
552                 .write_fn = func_channel_write_real,
553                 .chan = chan,
554                 .function = function,
555                 .data = data,
556                 .value = value,
557         };
558
559         res = func_channel_write_real(chan, function, data, value);
560         ast_channel_setoption(chan, AST_OPTION_CHANNEL_WRITE, &write_info, sizeof(write_info), 0);
561
562         return res;
563 }
564
565 static struct ast_custom_function channel_function = {
566         .name = "CHANNEL",
567         .read = func_channel_read,
568         .write = func_channel_write,
569 };
570
571 static int func_channels_read(struct ast_channel *chan, const char *function, char *data, char *buf, size_t maxlen)
572 {
573         struct ast_channel *c = NULL;
574         regex_t re;
575         int res;
576         size_t buflen = 0;
577         struct ast_channel_iterator *iter;
578
579         buf[0] = '\0';
580
581         if (!ast_strlen_zero(data)) {
582                 if ((res = regcomp(&re, data, REG_EXTENDED | REG_ICASE | REG_NOSUB))) {
583                         regerror(res, &re, buf, maxlen);
584                         ast_log(LOG_WARNING, "Error compiling regular expression for %s(%s): %s\n", function, data, buf);
585                         return -1;
586                 }
587         }
588
589         if (!(iter = ast_channel_iterator_all_new())) {
590                 if (!ast_strlen_zero(data)) {
591                         regfree(&re);
592                 }
593                 return -1;
594         }
595
596         while ((c = ast_channel_iterator_next(iter))) {
597                 ast_channel_lock(c);
598                 if (ast_strlen_zero(data) || regexec(&re, ast_channel_name(c), 0, NULL, 0) == 0) {
599                         size_t namelen = strlen(ast_channel_name(c));
600                         if (buflen + namelen + (ast_strlen_zero(buf) ? 0 : 1) + 1 < maxlen) {
601                                 if (!ast_strlen_zero(buf)) {
602                                         strcat(buf, " ");
603                                         buflen++;
604                                 }
605                                 strcat(buf, ast_channel_name(c));
606                                 buflen += namelen;
607                         } else {
608                                 ast_log(LOG_WARNING, "Number of channels exceeds the available buffer space.  Output will be truncated!\n");
609                         }
610                 }
611                 ast_channel_unlock(c);
612                 c = ast_channel_unref(c);
613         }
614
615         ast_channel_iterator_destroy(iter);
616
617         if (!ast_strlen_zero(data)) {
618                 regfree(&re);
619         }
620
621         return 0;
622 }
623
624 static struct ast_custom_function channels_function = {
625         .name = "CHANNELS",
626         .read = func_channels_read,
627 };
628
629 static int func_mchan_read(struct ast_channel *chan, const char *function,
630                              char *data, struct ast_str **buf, ssize_t len)
631 {
632         struct ast_channel *mchan = ast_channel_get_by_name(ast_channel_linkedid(chan));
633         char *template = alloca(4 + strlen(data));
634         sprintf(template, "${%s}", data); /* SAFE */
635         ast_str_substitute_variables(buf, len, mchan ? mchan : chan, template);
636         if (mchan) {
637                 ast_channel_unref(mchan);
638         }
639         return 0;
640 }
641
642 static int func_mchan_write(struct ast_channel *chan, const char *function,
643                               char *data, const char *value)
644 {
645         struct ast_channel *mchan = ast_channel_get_by_name(ast_channel_linkedid(chan));
646         pbx_builtin_setvar_helper(mchan ? mchan : chan, data, value);
647         if (mchan) {
648                 ast_channel_unref(mchan);
649         }
650         return 0;
651 }
652
653 static struct ast_custom_function mchan_function = {
654         .name = "MASTER_CHANNEL",
655         .read2 = func_mchan_read,
656         .write = func_mchan_write,
657 };
658
659 static int unload_module(void)
660 {
661         int res = 0;
662
663         res |= ast_custom_function_unregister(&channel_function);
664         res |= ast_custom_function_unregister(&channels_function);
665         res |= ast_custom_function_unregister(&mchan_function);
666
667         return res;
668 }
669
670 static int load_module(void)
671 {
672         int res = 0;
673
674         res |= ast_custom_function_register(&channel_function);
675         res |= ast_custom_function_register(&channels_function);
676         res |= ast_custom_function_register(&mchan_function);
677
678         return res;
679 }
680
681 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Channel information dialplan functions");