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