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