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