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