Allow Asterisk to compile under GCC 4.10
[asterisk/asterisk.git] / channels / pjsip / dialplan_functions.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2013, 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 /*!
18  * \file
19  *
20  * \author \verbatim Joshua Colp <jcolp@digium.com> \endverbatim
21  * \author \verbatim Matt Jordan <mjordan@digium.com> \endverbatim
22  *
23  * \ingroup functions
24  *
25  * \brief PJSIP channel dialplan functions
26  */
27
28 /*** MODULEINFO
29         <support_level>core</support_level>
30  ***/
31
32 /*** DOCUMENTATION
33 <function name="PJSIP_DIAL_CONTACTS" language="en_US">
34         <synopsis>
35                 Return a dial string for dialing all contacts on an AOR.
36         </synopsis>
37         <syntax>
38                 <parameter name="endpoint" required="true">
39                         <para>Name of the endpoint</para>
40                 </parameter>
41                 <parameter name="aor" required="false">
42                         <para>Name of an AOR to use, if not specified the configured AORs on the endpoint are used</para>
43                 </parameter>
44                 <parameter name="request_user" required="false">
45                         <para>Optional request user to use in the request URI</para>
46                 </parameter>
47         </syntax>
48         <description>
49                 <para>Returns a properly formatted dial string for dialing all contacts on an AOR.</para>
50         </description>
51 </function>
52 <function name="PJSIP_MEDIA_OFFER" language="en_US">
53         <synopsis>
54                 Media and codec offerings to be set on an outbound SIP channel prior to dialing.
55         </synopsis>
56         <syntax>
57                 <parameter name="media" required="true">
58                         <para>types of media offered</para>
59                 </parameter>
60         </syntax>
61         <description>
62                 <para>Returns the codecs offered based upon the media choice</para>
63         </description>
64 </function>
65 <info name="PJSIPCHANNEL" language="en_US" tech="PJSIP">
66         <enumlist>
67                 <enum name="rtp">
68                         <para>R/O Retrieve media related information.</para>
69                         <parameter name="type" required="true">
70                                 <para>When <replaceable>rtp</replaceable> is specified, the
71                                 <literal>type</literal> parameter must be provided. It specifies
72                                 which RTP parameter to read.</para>
73                                 <enumlist>
74                                         <enum name="src">
75                                                 <para>Retrieve the local address for RTP.</para>
76                                         </enum>
77                                         <enum name="dest">
78                                                 <para>Retrieve the remote address for RTP.</para>
79                                         </enum>
80                                         <enum name="direct">
81                                                 <para>If direct media is enabled, this address is the remote address
82                                                 used for RTP.</para>
83                                         </enum>
84                                         <enum name="secure">
85                                                 <para>Whether or not the media stream is encrypted.</para>
86                                                 <enumlist>
87                                                         <enum name="0">
88                                                                 <para>The media stream is not encrypted.</para>
89                                                         </enum>
90                                                         <enum name="1">
91                                                                 <para>The media stream is encrypted.</para>
92                                                         </enum>
93                                                 </enumlist>
94                                         </enum>
95                                         <enum name="hold">
96                                                 <para>Whether or not the media stream is currently restricted
97                                                 due to a call hold.</para>
98                                                 <enumlist>
99                                                         <enum name="0">
100                                                                 <para>The media stream is not held.</para>
101                                                         </enum>
102                                                         <enum name="1">
103                                                                 <para>The media stream is held.</para>
104                                                         </enum>
105                                                 </enumlist>
106                                         </enum>
107                                 </enumlist>
108                         </parameter>
109                         <parameter name="media_type" required="false">
110                                 <para>When <replaceable>rtp</replaceable> is specified, the
111                                 <literal>media_type</literal> parameter may be provided. It specifies
112                                 which media stream the chosen RTP parameter should be retrieved
113                                 from.</para>
114                                 <enumlist>
115                                         <enum name="audio">
116                                                 <para>Retrieve information from the audio media stream.</para>
117                                                 <note><para>If not specified, <literal>audio</literal> is used
118                                                 by default.</para></note>
119                                         </enum>
120                                         <enum name="video">
121                                                 <para>Retrieve information from the video media stream.</para>
122                                         </enum>
123                                 </enumlist>
124                         </parameter>
125                 </enum>
126                 <enum name="rtcp">
127                         <para>R/O Retrieve RTCP statistics.</para>
128                         <parameter name="statistic" required="true">
129                                 <para>When <replaceable>rtcp</replaceable> is specified, the
130                                 <literal>statistic</literal> parameter must be provided. It specifies
131                                 which RTCP statistic parameter to read.</para>
132                                 <enumlist>
133                                         <enum name="all">
134                                                 <para>Retrieve a summary of all RTCP statistics.</para>
135                                                 <para>The following data items are returned in a semi-colon
136                                                 delineated list:</para>
137                                                 <enumlist>
138                                                         <enum name="ssrc">
139                                                                 <para>Our Synchronization Source identifier</para>
140                                                         </enum>
141                                                         <enum name="themssrc">
142                                                                 <para>Their Synchronization Source identifier</para>
143                                                         </enum>
144                                                         <enum name="lp">
145                                                                 <para>Our lost packet count</para>
146                                                         </enum>
147                                                         <enum name="rxjitter">
148                                                                 <para>Received packet jitter</para>
149                                                         </enum>
150                                                         <enum name="rxcount">
151                                                                 <para>Received packet count</para>
152                                                         </enum>
153                                                         <enum name="txjitter">
154                                                                 <para>Transmitted packet jitter</para>
155                                                         </enum>
156                                                         <enum name="txcount">
157                                                                 <para>Transmitted packet count</para>
158                                                         </enum>
159                                                         <enum name="rlp">
160                                                                 <para>Remote lost packet count</para>
161                                                         </enum>
162                                                         <enum name="rtt">
163                                                                 <para>Round trip time</para>
164                                                         </enum>
165                                                 </enumlist>
166                                         </enum>
167                                         <enum name="all_jitter">
168                                                 <para>Retrieve a summary of all RTCP Jitter statistics.</para>
169                                                 <para>The following data items are returned in a semi-colon
170                                                 delineated list:</para>
171                                                 <enumlist>
172                                                         <enum name="minrxjitter">
173                                                                 <para>Our minimum jitter</para>
174                                                         </enum>
175                                                         <enum name="maxrxjitter">
176                                                                 <para>Our max jitter</para>
177                                                         </enum>
178                                                         <enum name="avgrxjitter">
179                                                                 <para>Our average jitter</para>
180                                                         </enum>
181                                                         <enum name="stdevrxjitter">
182                                                                 <para>Our jitter standard deviation</para>
183                                                         </enum>
184                                                         <enum name="reported_minjitter">
185                                                                 <para>Their minimum jitter</para>
186                                                         </enum>
187                                                         <enum name="reported_maxjitter">
188                                                                 <para>Their max jitter</para>
189                                                         </enum>
190                                                         <enum name="reported_avgjitter">
191                                                                 <para>Their average jitter</para>
192                                                         </enum>
193                                                         <enum name="reported_stdevjitter">
194                                                                 <para>Their jitter standard deviation</para>
195                                                         </enum>
196                                                 </enumlist>
197                                         </enum>
198                                         <enum name="all_loss">
199                                                 <para>Retrieve a summary of all RTCP packet loss statistics.</para>
200                                                 <para>The following data items are returned in a semi-colon
201                                                 delineated list:</para>
202                                                 <enumlist>
203                                                         <enum name="minrxlost">
204                                                                 <para>Our minimum lost packets</para>
205                                                         </enum>
206                                                         <enum name="maxrxlost">
207                                                                 <para>Our max lost packets</para>
208                                                         </enum>
209                                                         <enum name="avgrxlost">
210                                                                 <para>Our average lost packets</para>
211                                                         </enum>
212                                                         <enum name="stdevrxlost">
213                                                                 <para>Our lost packets standard deviation</para>
214                                                         </enum>
215                                                         <enum name="reported_minlost">
216                                                                 <para>Their minimum lost packets</para>
217                                                         </enum>
218                                                         <enum name="reported_maxlost">
219                                                                 <para>Their max lost packets</para>
220                                                         </enum>
221                                                         <enum name="reported_avglost">
222                                                                 <para>Their average lost packets</para>
223                                                         </enum>
224                                                         <enum name="reported_stdevlost">
225                                                                 <para>Their lost packets standard deviation</para>
226                                                         </enum>
227                                                 </enumlist>
228                                         </enum>
229                                         <enum name="all_rtt">
230                                                 <para>Retrieve a summary of all RTCP round trip time information.</para>
231                                                 <para>The following data items are returned in a semi-colon
232                                                 delineated list:</para>
233                                                 <enumlist>
234                                                         <enum name="minrtt">
235                                                                 <para>Minimum round trip time</para>
236                                                         </enum>
237                                                         <enum name="maxrtt">
238                                                                 <para>Maximum round trip time</para>
239                                                         </enum>
240                                                         <enum name="avgrtt">
241                                                                 <para>Average round trip time</para>
242                                                         </enum>
243                                                         <enum name="stdevrtt">
244                                                                 <para>Standard deviation round trip time</para>
245                                                         </enum>
246                                                 </enumlist>
247                                         </enum>
248                                         <enum name="txcount"><para>Transmitted packet count</para></enum>
249                                         <enum name="rxcount"><para>Received packet count</para></enum>
250                                         <enum name="txjitter"><para>Transmitted packet jitter</para></enum>
251                                         <enum name="rxjitter"><para>Received packet jitter</para></enum>
252                                         <enum name="remote_maxjitter"><para>Their max jitter</para></enum>
253                                         <enum name="remote_minjitter"><para>Their minimum jitter</para></enum>
254                                         <enum name="remote_normdevjitter"><para>Their average jitter</para></enum>
255                                         <enum name="remote_stdevjitter"><para>Their jitter standard deviation</para></enum>
256                                         <enum name="local_maxjitter"><para>Our max jitter</para></enum>
257                                         <enum name="local_minjitter"><para>Our minimum jitter</para></enum>
258                                         <enum name="local_normdevjitter"><para>Our average jitter</para></enum>
259                                         <enum name="local_stdevjitter"><para>Our jitter standard deviation</para></enum>
260                                         <enum name="txploss"><para>Transmitted packet loss</para></enum>
261                                         <enum name="rxploss"><para>Received packet loss</para></enum>
262                                         <enum name="remote_maxrxploss"><para>Their max lost packets</para></enum>
263                                         <enum name="remote_minrxploss"><para>Their minimum lost packets</para></enum>
264                                         <enum name="remote_normdevrxploss"><para>Their average lost packets</para></enum>
265                                         <enum name="remote_stdevrxploss"><para>Their lost packets standard deviation</para></enum>
266                                         <enum name="local_maxrxploss"><para>Our max lost packets</para></enum>
267                                         <enum name="local_minrxploss"><para>Our minimum lost packets</para></enum>
268                                         <enum name="local_normdevrxploss"><para>Our average lost packets</para></enum>
269                                         <enum name="local_stdevrxploss"><para>Our lost packets standard deviation</para></enum>
270                                         <enum name="rtt"><para>Round trip time</para></enum>
271                                         <enum name="maxrtt"><para>Maximum round trip time</para></enum>
272                                         <enum name="minrtt"><para>Minimum round trip time</para></enum>
273                                         <enum name="normdevrtt"><para>Average round trip time</para></enum>
274                                         <enum name="stdevrtt"><para>Standard deviation round trip time</para></enum>
275                                         <enum name="local_ssrc"><para>Our Synchronization Source identifier</para></enum>
276                                         <enum name="remote_ssrc"><para>Their Synchronization Source identifier</para></enum>
277                                 </enumlist>
278                         </parameter>
279                         <parameter name="media_type" required="false">
280                                 <para>When <replaceable>rtcp</replaceable> is specified, the
281                                 <literal>media_type</literal> parameter may be provided. It specifies
282                                 which media stream the chosen RTCP parameter should be retrieved
283                                 from.</para>
284                                 <enumlist>
285                                         <enum name="audio">
286                                                 <para>Retrieve information from the audio media stream.</para>
287                                                 <note><para>If not specified, <literal>audio</literal> is used
288                                                 by default.</para></note>
289                                         </enum>
290                                         <enum name="video">
291                                                 <para>Retrieve information from the video media stream.</para>
292                                         </enum>
293                                 </enumlist>
294                         </parameter>
295                 </enum>
296                 <enum name="endpoint">
297                         <para>R/O The name of the endpoint associated with this channel.
298                         Use the <replaceable>PJSIP_ENDPOINT</replaceable> function to obtain
299                         further endpoint related information.</para>
300                 </enum>
301                 <enum name="pjsip">
302                         <para>R/O Obtain information about the current PJSIP channel and its
303                         session.</para>
304                         <parameter name="type" required="true">
305                                 <para>When <replaceable>pjsip</replaceable> is specified, the
306                                 <literal>type</literal> parameter must be provided. It specifies
307                                 which signalling parameter to read.</para>
308                                 <enumlist>
309                                         <enum name="secure">
310                                                 <para>Whether or not the signalling uses a secure transport.</para>
311                                                 <enumlist>
312                                                         <enum name="0"><para>The signalling uses a non-secure transport.</para></enum>
313                                                         <enum name="1"><para>The signalling uses a secure transport.</para></enum>
314                                                 </enumlist>
315                                         </enum>
316                                         <enum name="target_uri">
317                                                 <para>The request URI of the <literal>INVITE</literal> request associated with the creation of this channel.</para>
318                                         </enum>
319                                         <enum name="local_uri">
320                                                 <para>The local URI.</para>
321                                         </enum>
322                                         <enum name="remote_uri">
323                                                 <para>The remote URI.</para>
324                                         </enum>
325                                         <enum name="t38state">
326                                                 <para>The current state of any T.38 fax on this channel.</para>
327                                                 <enumlist>
328                                                         <enum name="DISABLED"><para>T.38 faxing is disabled on this channel.</para></enum>
329                                                         <enum name="LOCAL_REINVITE"><para>Asterisk has sent a <literal>re-INVITE</literal> to the remote end to initiate a T.38 fax.</para></enum>
330                                                         <enum name="REMOTE_REINVITE"><para>The remote end has sent a <literal>re-INVITE</literal> to Asterisk to initiate a T.38 fax.</para></enum>
331                                                         <enum name="ENABLED"><para>A T.38 fax session has been enabled.</para></enum>
332                                                         <enum name="REJECTED"><para>A T.38 fax session was attempted but was rejected.</para></enum>
333                                                 </enumlist>
334                                         </enum>
335                                         <enum name="local_addr">
336                                                 <para>On inbound calls, the full IP address and port number that
337                                                 the <literal>INVITE</literal> request was received on. On outbound
338                                                 calls, the full IP address and port number that the <literal>INVITE</literal>
339                                                 request was transmitted from.</para>
340                                         </enum>
341                                         <enum name="remote_addr">
342                                                 <para>On inbound calls, the full IP address and port number that
343                                                 the <literal>INVITE</literal> request was received from. On outbound
344                                                 calls, the full IP address and port number that the <literal>INVITE</literal>
345                                                 request was transmitted to.</para>
346                                         </enum>
347                                 </enumlist>
348                         </parameter>
349                 </enum>
350         </enumlist>
351 </info>
352 ***/
353
354 #include "asterisk.h"
355
356 #include <pjsip.h>
357 #include <pjlib.h>
358 #include <pjsip_ua.h>
359
360 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
361
362 #include "asterisk/astobj2.h"
363 #include "asterisk/module.h"
364 #include "asterisk/acl.h"
365 #include "asterisk/app.h"
366 #include "asterisk/channel.h"
367 #include "asterisk/format.h"
368 #include "asterisk/pbx.h"
369 #include "asterisk/res_pjsip.h"
370 #include "asterisk/res_pjsip_session.h"
371 #include "include/chan_pjsip.h"
372 #include "include/dialplan_functions.h"
373
374 /*!
375  * \brief String representations of the T.38 state enum
376  */
377 static const char *t38state_to_string[T38_MAX_ENUM] = {
378         [T38_DISABLED] = "DISABLED",
379         [T38_LOCAL_REINVITE] = "LOCAL_REINVITE",
380         [T38_PEER_REINVITE] = "REMOTE_REINVITE",
381         [T38_ENABLED] = "ENABLED",
382         [T38_REJECTED] = "REJECTED",
383 };
384
385 /*!
386  * \internal \brief Handle reading RTP information
387  */
388 static int channel_read_rtp(struct ast_channel *chan, const char *type, const char *field, char *buf, size_t buflen)
389 {
390         struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(chan);
391         struct chan_pjsip_pvt *pvt;
392         struct ast_sip_session_media *media = NULL;
393         struct ast_sockaddr addr;
394
395         if (!channel) {
396                 ast_log(AST_LOG_WARNING, "Channel %s has no pvt!\n", ast_channel_name(chan));
397                 return -1;
398         }
399
400         pvt = channel->pvt;
401         if (!pvt) {
402                 ast_log(AST_LOG_WARNING, "Channel %s has no chan_pjsip pvt!\n", ast_channel_name(chan));
403                 return -1;
404         }
405
406         if (ast_strlen_zero(type)) {
407                 ast_log(AST_LOG_WARNING, "You must supply a type field for 'rtp' information\n");
408                 return -1;
409         }
410
411         if (ast_strlen_zero(field) || !strcmp(field, "audio")) {
412                 media = pvt->media[SIP_MEDIA_AUDIO];
413         } else if (!strcmp(field, "video")) {
414                 media = pvt->media[SIP_MEDIA_VIDEO];
415         } else {
416                 ast_log(AST_LOG_WARNING, "Unknown media type field '%s' for 'rtp' information\n", field);
417                 return -1;
418         }
419
420         if (!media || !media->rtp) {
421                 ast_log(AST_LOG_WARNING, "Channel %s has no %s media/RTP session\n",
422                         ast_channel_name(chan), S_OR(field, "audio"));
423                 return -1;
424         }
425
426         if (!strcmp(type, "src")) {
427                 ast_rtp_instance_get_local_address(media->rtp, &addr);
428                 ast_copy_string(buf, ast_sockaddr_stringify(&addr), buflen);
429         } else if (!strcmp(type, "dest")) {
430                 ast_rtp_instance_get_remote_address(media->rtp, &addr);
431                 ast_copy_string(buf, ast_sockaddr_stringify(&addr), buflen);
432         } else if (!strcmp(type, "direct")) {
433                 ast_copy_string(buf, ast_sockaddr_stringify(&media->direct_media_addr), buflen);
434         } else if (!strcmp(type, "secure")) {
435                 snprintf(buf, buflen, "%d", media->srtp ? 1 : 0);
436         } else if (!strcmp(type, "hold")) {
437                 snprintf(buf, buflen, "%d", media->held ? 1 : 0);
438         } else {
439                 ast_log(AST_LOG_WARNING, "Unknown type field '%s' specified for 'rtp' information\n", type);
440                 return -1;
441         }
442
443         return 0;
444 }
445
446 /*!
447  * \internal \brief Handle reading RTCP information
448  */
449 static int channel_read_rtcp(struct ast_channel *chan, const char *type, const char *field, char *buf, size_t buflen)
450 {
451         struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(chan);
452         struct chan_pjsip_pvt *pvt;
453         struct ast_sip_session_media *media = NULL;
454
455         if (!channel) {
456                 ast_log(AST_LOG_WARNING, "Channel %s has no pvt!\n", ast_channel_name(chan));
457                 return -1;
458         }
459
460         pvt = channel->pvt;
461         if (!pvt) {
462                 ast_log(AST_LOG_WARNING, "Channel %s has no chan_pjsip pvt!\n", ast_channel_name(chan));
463                 return -1;
464         }
465
466         if (ast_strlen_zero(type)) {
467                 ast_log(AST_LOG_WARNING, "You must supply a type field for 'rtcp' information\n");
468                 return -1;
469         }
470
471         if (ast_strlen_zero(field) || !strcmp(field, "audio")) {
472                 media = pvt->media[SIP_MEDIA_AUDIO];
473         } else if (!strcmp(field, "video")) {
474                 media = pvt->media[SIP_MEDIA_VIDEO];
475         } else {
476                 ast_log(AST_LOG_WARNING, "Unknown media type field '%s' for 'rtcp' information\n", field);
477                 return -1;
478         }
479
480         if (!media || !media->rtp) {
481                 ast_log(AST_LOG_WARNING, "Channel %s has no %s media/RTP session\n",
482                         ast_channel_name(chan), S_OR(field, "audio"));
483                 return -1;
484         }
485
486         if (!strncasecmp(type, "all", 3)) {
487                 enum ast_rtp_instance_stat_field stat_field = AST_RTP_INSTANCE_STAT_FIELD_QUALITY;
488
489                 if (!strcasecmp(type, "all_jitter")) {
490                         stat_field = AST_RTP_INSTANCE_STAT_FIELD_QUALITY_JITTER;
491                 } else if (!strcasecmp(type, "all_rtt")) {
492                         stat_field = AST_RTP_INSTANCE_STAT_FIELD_QUALITY_RTT;
493                 } else if (!strcasecmp(type, "all_loss")) {
494                         stat_field = AST_RTP_INSTANCE_STAT_FIELD_QUALITY_LOSS;
495                 }
496
497                 if (!ast_rtp_instance_get_quality(media->rtp, stat_field, buf, buflen)) {
498                         ast_log(AST_LOG_WARNING, "Unable to retrieve 'rtcp' statistics for %s\n", ast_channel_name(chan));
499                         return -1;
500                 }
501         } else {
502                 struct ast_rtp_instance_stats stats;
503                 int i;
504                 struct {
505                         const char *name;
506                         enum { INT, DBL } type;
507                         union {
508                                 unsigned int *i4;
509                                 double *d8;
510                         };
511                 } lookup[] = {
512                         { "txcount",               INT, { .i4 = &stats.txcount, }, },
513                         { "rxcount",               INT, { .i4 = &stats.rxcount, }, },
514                         { "txjitter",              DBL, { .d8 = &stats.txjitter, }, },
515                         { "rxjitter",              DBL, { .d8 = &stats.rxjitter, }, },
516                         { "remote_maxjitter",      DBL, { .d8 = &stats.remote_maxjitter, }, },
517                         { "remote_minjitter",      DBL, { .d8 = &stats.remote_minjitter, }, },
518                         { "remote_normdevjitter",  DBL, { .d8 = &stats.remote_normdevjitter, }, },
519                         { "remote_stdevjitter",    DBL, { .d8 = &stats.remote_stdevjitter, }, },
520                         { "local_maxjitter",       DBL, { .d8 = &stats.local_maxjitter, }, },
521                         { "local_minjitter",       DBL, { .d8 = &stats.local_minjitter, }, },
522                         { "local_normdevjitter",   DBL, { .d8 = &stats.local_normdevjitter, }, },
523                         { "local_stdevjitter",     DBL, { .d8 = &stats.local_stdevjitter, }, },
524                         { "txploss",               INT, { .i4 = &stats.txploss, }, },
525                         { "rxploss",               INT, { .i4 = &stats.rxploss, }, },
526                         { "remote_maxrxploss",     DBL, { .d8 = &stats.remote_maxrxploss, }, },
527                         { "remote_minrxploss",     DBL, { .d8 = &stats.remote_minrxploss, }, },
528                         { "remote_normdevrxploss", DBL, { .d8 = &stats.remote_normdevrxploss, }, },
529                         { "remote_stdevrxploss",   DBL, { .d8 = &stats.remote_stdevrxploss, }, },
530                         { "local_maxrxploss",      DBL, { .d8 = &stats.local_maxrxploss, }, },
531                         { "local_minrxploss",      DBL, { .d8 = &stats.local_minrxploss, }, },
532                         { "local_normdevrxploss",  DBL, { .d8 = &stats.local_normdevrxploss, }, },
533                         { "local_stdevrxploss",    DBL, { .d8 = &stats.local_stdevrxploss, }, },
534                         { "rtt",                   DBL, { .d8 = &stats.rtt, }, },
535                         { "maxrtt",                DBL, { .d8 = &stats.maxrtt, }, },
536                         { "minrtt",                DBL, { .d8 = &stats.minrtt, }, },
537                         { "normdevrtt",            DBL, { .d8 = &stats.normdevrtt, }, },
538                         { "stdevrtt",              DBL, { .d8 = &stats.stdevrtt, }, },
539                         { "local_ssrc",            INT, { .i4 = &stats.local_ssrc, }, },
540                         { "remote_ssrc",           INT, { .i4 = &stats.remote_ssrc, }, },
541                         { NULL, },
542                 };
543
544                 if (ast_rtp_instance_get_stats(media->rtp, &stats, AST_RTP_INSTANCE_STAT_ALL)) {
545                         ast_log(AST_LOG_WARNING, "Unable to retrieve 'rtcp' statistics for %s\n", ast_channel_name(chan));
546                         return -1;
547                 }
548
549                 for (i = 0; !ast_strlen_zero(lookup[i].name); i++) {
550                         if (!strcasecmp(type, lookup[i].name)) {
551                                 if (lookup[i].type == INT) {
552                                         snprintf(buf, buflen, "%u", *lookup[i].i4);
553                                 } else {
554                                         snprintf(buf, buflen, "%f", *lookup[i].d8);
555                                 }
556                                 return 0;
557                         }
558                 }
559                 ast_log(AST_LOG_WARNING, "Unrecognized argument '%s' for 'rtcp' information\n", type);
560                 return -1;
561         }
562
563         return 0;
564 }
565
566 /*!
567  * \internal \brief Handle reading signalling information
568  */
569 static int channel_read_pjsip(struct ast_channel *chan, const char *type, const char *field, char *buf, size_t buflen)
570 {
571         struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(chan);
572         char *buf_copy;
573         pjsip_dialog *dlg;
574
575         if (!channel) {
576                 ast_log(AST_LOG_WARNING, "Channel %s has no pvt!\n", ast_channel_name(chan));
577                 return -1;
578         }
579
580         dlg = channel->session->inv_session->dlg;
581
582         if (!strcmp(type, "secure")) {
583                 snprintf(buf, buflen, "%d", dlg->secure ? 1 : 0);
584         } else if (!strcmp(type, "target_uri")) {
585                 pjsip_uri_print(PJSIP_URI_IN_REQ_URI, dlg->target, buf, buflen);
586                 buf_copy = ast_strdupa(buf);
587                 ast_escape_quoted(buf_copy, buf, buflen);
588         } else if (!strcmp(type, "local_uri")) {
589                 pjsip_uri_print(PJSIP_URI_IN_FROMTO_HDR, dlg->local.info->uri, buf, buflen);
590                 buf_copy = ast_strdupa(buf);
591                 ast_escape_quoted(buf_copy, buf, buflen);
592         } else if (!strcmp(type, "remote_uri")) {
593                 pjsip_uri_print(PJSIP_URI_IN_FROMTO_HDR, dlg->remote.info->uri, buf, buflen);
594                 buf_copy = ast_strdupa(buf);
595                 ast_escape_quoted(buf_copy, buf, buflen);
596         } else if (!strcmp(type, "t38state")) {
597                 ast_copy_string(buf, t38state_to_string[channel->session->t38state], buflen);
598         } else if (!strcmp(type, "local_addr")) {
599                 RAII_VAR(struct ast_datastore *, datastore, NULL, ao2_cleanup);
600                 struct transport_info_data *transport_data;
601
602                 datastore = ast_sip_session_get_datastore(channel->session, "transport_info");
603                 if (!datastore) {
604                         ast_log(AST_LOG_WARNING, "No transport information for channel %s\n", ast_channel_name(chan));
605                         return -1;
606                 }
607                 transport_data = datastore->data;
608
609                 if (pj_sockaddr_has_addr(&transport_data->local_addr)) {
610                         pj_sockaddr_print(&transport_data->local_addr, buf, buflen, 3);
611                 }
612         } else if (!strcmp(type, "remote_addr")) {
613                 RAII_VAR(struct ast_datastore *, datastore, NULL, ao2_cleanup);
614                 struct transport_info_data *transport_data;
615
616                 datastore = ast_sip_session_get_datastore(channel->session, "transport_info");
617                 if (!datastore) {
618                         ast_log(AST_LOG_WARNING, "No transport information for channel %s\n", ast_channel_name(chan));
619                         return -1;
620                 }
621                 transport_data = datastore->data;
622
623                 if (pj_sockaddr_has_addr(&transport_data->remote_addr)) {
624                         pj_sockaddr_print(&transport_data->remote_addr, buf, buflen, 3);
625                 }
626         } else {
627                 ast_log(AST_LOG_WARNING, "Unrecognized argument '%s' for 'pjsip' information\n", type);
628                 return -1;
629         }
630
631         return 0;
632 }
633
634 /*! \brief Struct used to push function arguments to task processor */
635 struct pjsip_func_args {
636         struct ast_channel *chan;
637         const char *param;
638         const char *type;
639         const char *field;
640         char *buf;
641         size_t len;
642         int ret;
643 };
644
645 /*! \internal \brief Taskprocessor callback that handles the read on a PJSIP thread */
646 static int read_pjsip(void *data)
647 {
648         struct pjsip_func_args *func_args = data;
649
650         if (!strcmp(func_args->param, "rtp")) {
651                 func_args->ret = channel_read_rtp(func_args->chan, func_args->type,
652                                                   func_args->field, func_args->buf,
653                                                   func_args->len);
654         } else if (!strcmp(func_args->param, "rtcp")) {
655                 func_args->ret = channel_read_rtcp(func_args->chan, func_args->type,
656                                                    func_args->field, func_args->buf,
657                                                    func_args->len);
658         } else if (!strcmp(func_args->param, "endpoint")) {
659                 struct ast_sip_channel_pvt *pvt = ast_channel_tech_pvt(func_args->chan);
660
661                 if (!pvt) {
662                         ast_log(AST_LOG_WARNING, "Channel %s has no pvt!\n", ast_channel_name(func_args->chan));
663                         return -1;
664                 }
665                 if (!pvt->session || !pvt->session->endpoint) {
666                         ast_log(AST_LOG_WARNING, "Channel %s has no endpoint!\n", ast_channel_name(func_args->chan));
667                         return -1;
668                 }
669                 snprintf(func_args->buf, func_args->len, "%s", ast_sorcery_object_get_id(pvt->session->endpoint));
670         } else if (!strcmp(func_args->param, "pjsip")) {
671                 func_args->ret = channel_read_pjsip(func_args->chan, func_args->type,
672                                                     func_args->field, func_args->buf,
673                                                     func_args->len);
674         } else {
675                 func_args->ret = -1;
676         }
677
678         return 0;
679 }
680
681
682 int pjsip_acf_channel_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
683 {
684         struct pjsip_func_args func_args = { 0, };
685         struct ast_sip_channel_pvt *channel;
686         char *parse = ast_strdupa(data);
687
688         AST_DECLARE_APP_ARGS(args,
689                 AST_APP_ARG(param);
690                 AST_APP_ARG(type);
691                 AST_APP_ARG(field);
692         );
693
694         if (!chan) {
695                 ast_log(LOG_WARNING, "No channel was provided to %s function.\n", cmd);
696                 return -1;
697         }
698         channel = ast_channel_tech_pvt(chan);
699
700         /* Check for zero arguments */
701         if (ast_strlen_zero(parse)) {
702                 ast_log(LOG_ERROR, "Cannot call %s without arguments\n", cmd);
703                 return -1;
704         }
705
706         AST_STANDARD_APP_ARGS(args, parse);
707
708         /* Sanity check */
709         if (strcmp(ast_channel_tech(chan)->type, "PJSIP")) {
710                 ast_log(LOG_ERROR, "Cannot call %s on a non-PJSIP channel\n", cmd);
711                 return 0;
712         }
713
714         if (!channel) {
715                 ast_log(AST_LOG_WARNING, "Channel %s has no pvt!\n", ast_channel_name(chan));
716                 return -1;
717         }
718
719         memset(buf, 0, len);
720
721         func_args.chan = chan;
722         func_args.param = args.param;
723         func_args.type = args.type;
724         func_args.field = args.field;
725         func_args.buf = buf;
726         func_args.len = len;
727         if (ast_sip_push_task_synchronous(channel->session->serializer, read_pjsip, &func_args)) {
728                 ast_log(LOG_WARNING, "Unable to read properties of channel %s: failed to push task\n", ast_channel_name(chan));
729                 return -1;
730         }
731
732         return func_args.ret;
733 }
734
735 int pjsip_acf_dial_contacts_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
736 {
737         RAII_VAR(struct ast_sip_endpoint *, endpoint, NULL, ao2_cleanup);
738         RAII_VAR(struct ast_str *, dial, NULL, ast_free_ptr);
739         const char *aor_name;
740         char *rest;
741
742         AST_DECLARE_APP_ARGS(args,
743                 AST_APP_ARG(endpoint_name);
744                 AST_APP_ARG(aor_name);
745                 AST_APP_ARG(request_user);
746         );
747
748         AST_STANDARD_APP_ARGS(args, data);
749
750         if (ast_strlen_zero(args.endpoint_name)) {
751                 ast_log(LOG_WARNING, "An endpoint name must be specified when using the '%s' dialplan function\n", cmd);
752                 return -1;
753         } else if (!(endpoint = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "endpoint", args.endpoint_name))) {
754                 ast_log(LOG_WARNING, "Specified endpoint '%s' was not found\n", args.endpoint_name);
755                 return -1;
756         }
757
758         aor_name = S_OR(args.aor_name, endpoint->aors);
759
760         if (ast_strlen_zero(aor_name)) {
761                 ast_log(LOG_WARNING, "No AOR has been provided and no AORs are configured on endpoint '%s'\n", args.endpoint_name);
762                 return -1;
763         } else if (!(dial = ast_str_create(len))) {
764                 ast_log(LOG_WARNING, "Could not get enough buffer space for dialing contacts\n");
765                 return -1;
766         } else if (!(rest = ast_strdupa(aor_name))) {
767                 ast_log(LOG_WARNING, "Could not duplicate provided AORs\n");
768                 return -1;
769         }
770
771         while ((aor_name = strsep(&rest, ","))) {
772                 RAII_VAR(struct ast_sip_aor *, aor, ast_sip_location_retrieve_aor(aor_name), ao2_cleanup);
773                 RAII_VAR(struct ao2_container *, contacts, NULL, ao2_cleanup);
774                 struct ao2_iterator it_contacts;
775                 struct ast_sip_contact *contact;
776
777                 if (!aor) {
778                         /* If the AOR provided is not found skip it, there may be more */
779                         continue;
780                 } else if (!(contacts = ast_sip_location_retrieve_aor_contacts(aor))) {
781                         /* No contacts are available, skip it as well */
782                         continue;
783                 } else if (!ao2_container_count(contacts)) {
784                         /* We were given a container but no contacts are in it... */
785                         continue;
786                 }
787
788                 it_contacts = ao2_iterator_init(contacts, 0);
789                 for (; (contact = ao2_iterator_next(&it_contacts)); ao2_ref(contact, -1)) {
790                         ast_str_append(&dial, -1, "PJSIP/");
791
792                         if (!ast_strlen_zero(args.request_user)) {
793                                 ast_str_append(&dial, -1, "%s@", args.request_user);
794                         }
795                         ast_str_append(&dial, -1, "%s/%s&", args.endpoint_name, contact->uri);
796                 }
797                 ao2_iterator_destroy(&it_contacts);
798         }
799
800         /* Trim the '&' at the end off */
801         ast_str_truncate(dial, ast_str_strlen(dial) - 1);
802
803         ast_copy_string(buf, ast_str_buffer(dial), len);
804
805         return 0;
806 }
807
808 static int media_offer_read_av(struct ast_sip_session *session, char *buf,
809                                size_t len, enum ast_format_type media_type)
810 {
811         int i, size = 0;
812         struct ast_format fmt;
813         const char *name;
814
815         for (i = 0; ast_codec_pref_index(&session->override_prefs, i, &fmt); ++i) {
816                 if (AST_FORMAT_GET_TYPE(fmt.id) != media_type) {
817                         continue;
818                 }
819
820                 name = ast_getformatname(&fmt);
821
822                 if (ast_strlen_zero(name)) {
823                         ast_log(LOG_WARNING, "PJSIP_MEDIA_OFFER unrecognized format %s\n", name);
824                         continue;
825                 }
826
827                 /* add one since we'll include a comma */
828                 size = strlen(name) + 1;
829                 len -= size;
830                 if ((len) < 0) {
831                         break;
832                 }
833
834                 /* no reason to use strncat here since we have already ensured buf has
835                    enough space, so strcat can be safely used */
836                 strcat(buf, name);
837                 strcat(buf, ",");
838         }
839
840         if (size) {
841                 /* remove the extra comma */
842                 buf[strlen(buf) - 1] = '\0';
843         }
844         return 0;
845 }
846
847 struct media_offer_data {
848         struct ast_sip_session *session;
849         enum ast_format_type media_type;
850         const char *value;
851 };
852
853 static int media_offer_write_av(void *obj)
854 {
855         struct media_offer_data *data = obj;
856         int i;
857         struct ast_format fmt;
858         /* remove all of the given media type first */
859         for (i = 0; ast_codec_pref_index(&data->session->override_prefs, i, &fmt); ++i) {
860                 if (AST_FORMAT_GET_TYPE(fmt.id) == data->media_type) {
861                         ast_codec_pref_remove(&data->session->override_prefs, &fmt);
862                 }
863         }
864         ast_format_cap_remove_bytype(data->session->req_caps, data->media_type);
865         ast_parse_allow_disallow(&data->session->override_prefs, data->session->req_caps, data->value, 1);
866
867         return 0;
868 }
869
870 int pjsip_acf_media_offer_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
871 {
872         struct ast_sip_channel_pvt *channel;
873
874         if (!chan) {
875                 ast_log(LOG_WARNING, "No channel was provided to %s function.\n", cmd);
876                 return -1;
877         }
878
879         channel = ast_channel_tech_pvt(chan);
880
881         if (!strcmp(data, "audio")) {
882                 return media_offer_read_av(channel->session, buf, len, AST_FORMAT_TYPE_AUDIO);
883         } else if (!strcmp(data, "video")) {
884                 return media_offer_read_av(channel->session, buf, len, AST_FORMAT_TYPE_VIDEO);
885         }
886
887         return 0;
888 }
889
890 int pjsip_acf_media_offer_write(struct ast_channel *chan, const char *cmd, char *data, const char *value)
891 {
892         struct ast_sip_channel_pvt *channel;
893         struct media_offer_data mdata = {
894                 .value = value
895         };
896
897         if (!chan) {
898                 ast_log(LOG_WARNING, "No channel was provided to %s function.\n", cmd);
899                 return -1;
900         }
901
902         channel = ast_channel_tech_pvt(chan);
903         mdata.session = channel->session;
904
905         if (!strcmp(data, "audio")) {
906                 mdata.media_type = AST_FORMAT_TYPE_AUDIO;
907         } else if (!strcmp(data, "video")) {
908                 mdata.media_type = AST_FORMAT_TYPE_VIDEO;
909         }
910
911         return ast_sip_push_task_synchronous(channel->session->serializer, media_offer_write_av, &mdata);
912 }