2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2013, Digium, Inc.
6 * Mark Michelson <mmichelson@digium.com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
22 /* Needed for SUBSCRIBE, NOTIFY, and PUBLISH method definitions */
23 #include <pjsip_simple.h>
26 #include "asterisk/res_pjsip.h"
27 #include "res_pjsip/include/res_pjsip_private.h"
28 #include "asterisk/linkedlists.h"
29 #include "asterisk/logger.h"
30 #include "asterisk/lock.h"
31 #include "asterisk/utils.h"
32 #include "asterisk/astobj2.h"
33 #include "asterisk/module.h"
34 #include "asterisk/threadpool.h"
35 #include "asterisk/taskprocessor.h"
36 #include "asterisk/uuid.h"
37 #include "asterisk/sorcery.h"
40 <depend>pjproject</depend>
41 <depend>res_sorcery_config</depend>
42 <support_level>core</support_level>
46 <configInfo name="res_pjsip" language="en_US">
47 <synopsis>SIP Resource using PJProject</synopsis>
48 <configFile name="pjsip.conf">
49 <configObject name="endpoint">
50 <synopsis>Endpoint</synopsis>
52 The <emphasis>Endpoint</emphasis> is the primary configuration object.
53 It contains the core SIP related options only, endpoints are <emphasis>NOT</emphasis>
54 dialable entries of their own. Communication with another SIP device is
55 accomplished via Addresses of Record (AoRs) which have one or more
56 contacts assicated with them. Endpoints <emphasis>NOT</emphasis> configured to
57 use a <literal>transport</literal> will default to first transport found
58 in <filename>pjsip.conf</filename> that matches its type.
60 <para>Example: An Endpoint has been configured with no transport.
61 When it comes time to call an AoR, PJSIP will find the
62 first transport that matches the type. A SIP URI of <literal>sip:5000@[11::33]</literal>
63 will use the first IPv6 transport and try to send the request.
65 <para>If the anonymous endpoint identifier is in use an endpoint with the name
66 "anonymous@domain" will be searched for as a last resort. If this is not found
67 it will fall back to searching for "anonymous". If neither endpoints are found
68 the anonymous endpoint identifier will not return an endpoint and anonymous
69 calling will not be possible.
72 <configOption name="100rel" default="yes">
73 <synopsis>Allow support for RFC3262 provisional ACK tags</synopsis>
77 <enum name="required" />
82 <configOption name="aggregate_mwi" default="yes">
84 <description><para>When enabled, <replaceable>aggregate_mwi</replaceable> condenses message
85 waiting notifications from multiple mailboxes into a single NOTIFY. If it is disabled,
86 individual NOTIFYs are sent for each mailbox.</para></description>
88 <configOption name="allow">
89 <synopsis>Media Codec(s) to allow</synopsis>
91 <configOption name="aors">
92 <synopsis>AoR(s) to be used with the endpoint</synopsis>
94 List of comma separated AoRs that the endpoint should be associated with.
97 <configOption name="auth">
98 <synopsis>Authentication Object(s) associated with the endpoint</synopsis>
100 This is a comma-delimited list of <replaceable>auth</replaceable> sections defined
101 in <filename>pjsip.conf</filename> to be used to verify inbound connection attempts.
103 Endpoints without an <literal>authentication</literal> object
104 configured will allow connections without vertification.
105 </para></description>
107 <configOption name="callerid">
108 <synopsis>CallerID information for the endpoint</synopsis>
110 Must be in the format <literal>Name <Number></literal>,
111 or only <literal><Number></literal>.
112 </para></description>
114 <configOption name="callerid_privacy">
115 <synopsis>Default privacy level</synopsis>
118 <enum name="allowed_not_screened" />
119 <enum name="allowed_passed_screened" />
120 <enum name="allowed_failed_screened" />
121 <enum name="allowed" />
122 <enum name="prohib_not_screened" />
123 <enum name="prohib_passed_screened" />
124 <enum name="prohib_failed_screened" />
125 <enum name="prohib" />
126 <enum name="unavailable" />
130 <configOption name="callerid_tag">
131 <synopsis>Internal id_tag for the endpoint</synopsis>
133 <configOption name="context">
134 <synopsis>Dialplan context for inbound sessions</synopsis>
136 <configOption name="direct_media_glare_mitigation" default="none">
137 <synopsis>Mitigation of direct media (re)INVITE glare</synopsis>
140 This setting attempts to avoid creating INVITE glare scenarios
141 by disabling direct media reINVITEs in one direction thereby allowing
142 designated servers (according to this option) to initiate direct
143 media reINVITEs without contention and significantly reducing call
147 A more detailed description of how this option functions can be found on
148 the Asterisk wiki https://wiki.asterisk.org/wiki/display/AST/SIP+Direct+Media+Reinvite+Glare+Avoidance
152 <enum name="outgoing" />
153 <enum name="incoming" />
157 <configOption name="direct_media_method" default="invite">
158 <synopsis>Direct Media method type</synopsis>
160 <para>Method for setting up Direct Media between endpoints.</para>
162 <enum name="invite" />
163 <enum name="reinvite">
164 <para>Alias for the <literal>invite</literal> value.</para>
166 <enum name="update" />
170 <configOption name="connected_line_method" default="invite">
171 <synopsis>Connected line method type</synopsis>
173 <para>Method used when updating connected line information.</para>
175 <enum name="invite" />
176 <enum name="reinvite">
177 <para>Alias for the <literal>invite</literal> value.</para>
179 <enum name="update" />
183 <configOption name="direct_media" default="yes">
184 <synopsis>Determines whether media may flow directly between endpoints.</synopsis>
186 <configOption name="disable_direct_media_on_nat" default="no">
187 <synopsis>Disable direct media session refreshes when NAT obstructs the media session</synopsis>
189 <configOption name="disallow">
190 <synopsis>Media Codec(s) to disallow</synopsis>
192 <configOption name="dtmfmode" default="rfc4733">
193 <synopsis>DTMF mode</synopsis>
195 <para>This setting allows to choose the DTMF mode for endpoint communication.</para>
197 <enum name="rfc4733">
198 <para>DTMF is sent out of band of the main audio stream.This
199 supercedes the older <emphasis>RFC-2833</emphasis> used within
200 the older <literal>chan_sip</literal>.</para>
203 <para>DTMF is sent as part of audio stream.</para>
206 <para>DTMF is sent as SIP INFO packets.</para>
211 <configOption name="external_media_address">
212 <synopsis>IP used for External Media handling</synopsis>
214 <configOption name="force_rport" default="yes">
215 <synopsis>Force use of return port</synopsis>
217 <configOption name="ice_support" default="no">
218 <synopsis>Enable the ICE mechanism to help traverse NAT</synopsis>
220 <configOption name="identify_by" default="username,location">
221 <synopsis>Way(s) for Endpoint to be identified</synopsis>
223 There are currently two methods to identify an endpoint. By default
224 both are used to identify an endpoint.
227 <enum name="username" />
228 <enum name="location" />
229 <enum name="username,location" />
233 <configOption name="mailboxes">
234 <synopsis>Mailbox(es) to be associated with</synopsis>
236 <configOption name="mohsuggest" default="default">
237 <synopsis>Default Music On Hold class</synopsis>
239 <configOption name="outbound_auth">
240 <synopsis>Authentication object used for outbound requests</synopsis>
242 <configOption name="outbound_proxy">
243 <synopsis>Proxy through which to send requests</synopsis>
245 <configOption name="rewrite_contact">
246 <synopsis>Allow Contact header to be rewritten with the source IP address-port</synopsis>
248 <configOption name="rtp_ipv6" default="no">
249 <synopsis>Allow use of IPv6 for RTP traffic</synopsis>
251 <configOption name="rtp_symmetric" default="no">
252 <synopsis>Enforce that RTP must be symmetric</synopsis>
254 <configOption name="send_pai" default="no">
255 <synopsis>Send the P-Asserted-Identity header</synopsis>
257 <configOption name="send_rpid" default="no">
258 <synopsis>Send the Remote-Party-ID header</synopsis>
260 <configOption name="timers_min_se" default="90">
261 <synopsis>Minimum session timers expiration period</synopsis>
263 Minimium session timer expiration period. Time in seconds.
264 </para></description>
266 <configOption name="timers" default="yes">
267 <synopsis>Session timers for SIP packets</synopsis>
270 <enum name="forced" />
272 <enum name="required" />
277 <configOption name="timers_sess_expires" default="1800">
278 <synopsis>Maximum session timer expiration period</synopsis>
280 Maximium session timer expiration period. Time in seconds.
281 </para></description>
283 <configOption name="transport">
284 <synopsis>Desired transport configuration</synopsis>
286 This will set the desired transport configuration to send SIP data through.
288 <warning><para>Not specifying a transport will <emphasis>DEFAULT</emphasis>
289 to the first configured transport in <filename>pjsip.conf</filename> which is
290 valid for the URI we are trying to contact.
294 <configOption name="trust_id_inbound" default="no">
295 <synopsis>Accept identification information received from this endpoint</synopsis>
296 <description><para>This option determines whether Asterisk will accept
297 identification from the endpoint from headers such as P-Asserted-Identity
298 or Remote-Party-ID header. This option applies both to calls originating from the
299 endpoint and calls originating from Asterisk. If <literal>no</literal>, the
300 configured Caller-ID from pjsip.conf will always be used as the identity for
301 the endpoint.</para></description>
303 <configOption name="trust_id_outbound" default="no">
304 <synopsis>Send private identification details to the endpoint.</synopsis>
305 <description><para>This option determines whether res_pjsip will send private
306 identification information to the endpoint. If <literal>no</literal>,
307 private Caller-ID information will not be forwarded to the endpoint.
308 "Private" in this case refers to any method of restricting identification.
309 Example: setting <replaceable>callerid_privacy</replaceable> to any
310 <literal>prohib</literal> variation.
311 Example: If <replaceable>trust_id_inbound</replaceable> is set to
312 <literal>yes</literal>, the presence of a <literal>Privacy: id</literal>
313 header in a SIP request or response would indicate the identification
314 provided in the request is private.</para></description>
316 <configOption name="type">
317 <synopsis>Must be of type 'endpoint'.</synopsis>
319 <configOption name="use_ptime" default="no">
320 <synopsis>Use Endpoint's requested packetisation interval</synopsis>
322 <configOption name="use_avpf" default="no">
323 <synopsis>Determines whether res_pjsip will use and enforce usage of AVPF for this
326 If set to <literal>yes</literal>, res_pjsip will use use the AVPF or SAVPF RTP
327 profile for all media offers on outbound calls and media updates and will
328 decline media offers not using the AVPF or SAVPF profile.
330 If set to <literal>no</literal>, res_pjsip will use use the AVP or SAVP RTP
331 profile for all media offers on outbound calls and media updates and will
332 decline media offers not using the AVP or SAVP profile.
333 </para></description>
335 <configOption name="media_encryption" default="no">
336 <synopsis>Determines whether res_pjsip will use and enforce usage of media encryption
337 for this endpoint.</synopsis>
340 <enum name="no"><para>
341 res_pjsip will offer no encryption and allow no encryption to be setup.
343 <enum name="sdes"><para>
344 res_pjsip will offer standard SRTP setup via in-SDP keys. Encrypted SIP
345 transport should be used in conjunction with this option to prevent
346 exposure of media encryption keys.
348 <enum name="dtls"><para>
349 res_pjsip will offer DTLS-SRTP setup.
354 <configOption name="inband_progress" default="no">
355 <synopsis>Determines whether chan_pjsip will indicate ringing using inband
358 If set to <literal>yes</literal>, chan_pjsip will send a 183 Session Progress
359 when told to indicate ringing and will immediately start sending ringing
362 If set to <literal>no</literal>, chan_pjsip will send a 180 Ringing when told
363 to indicate ringing and will NOT send it as audio.
364 </para></description>
366 <configOption name="callgroup">
367 <synopsis>The numeric pickup groups for a channel.</synopsis>
369 Can be set to a comma separated list of numbers or ranges between the values
370 of 0-63 (maximum of 64 groups).
371 </para></description>
373 <configOption name="pickupgroup">
374 <synopsis>The numeric pickup groups that a channel can pickup.</synopsis>
376 Can be set to a comma separated list of numbers or ranges between the values
377 of 0-63 (maximum of 64 groups).
378 </para></description>
380 <configOption name="namedcallgroup">
381 <synopsis>The named pickup groups for a channel.</synopsis>
383 Can be set to a comma separated list of case sensitive strings limited by
384 supported line length.
385 </para></description>
387 <configOption name="namedpickupgroup">
388 <synopsis>The named pickup groups that a channel can pickup.</synopsis>
390 Can be set to a comma separated list of case sensitive strings limited by
391 supported line length.
392 </para></description>
394 <configOption name="devicestate_busy_at" default="0">
395 <synopsis>The number of in-use channels which will cause busy to be returned as device state</synopsis>
397 When the number of in-use channels for the endpoint matches the devicestate_busy_at setting the
398 PJSIP channel driver will return busy as the device state instead of in use.
399 </para></description>
401 <configOption name="t38udptl" default="no">
402 <synopsis>Whether T.38 UDPTL support is enabled or not</synopsis>
404 If set to yes T.38 UDPTL support will be enabled, and T.38 negotiation requests will be accepted
406 </para></description>
408 <configOption name="t38udptl_ec" default="none">
409 <synopsis>T.38 UDPTL error correction method</synopsis>
412 <enum name="none"><para>
413 No error correction should be used.
415 <enum name="fec"><para>
416 Forward error correction should be used.
418 <enum name="redundancy"><para>
419 Redundacy error correction should be used.
424 <configOption name="t38udptl_maxdatagram" default="0">
425 <synopsis>T.38 UDPTL maximum datagram size</synopsis>
427 This option can be set to override the maximum datagram of a remote endpoint for broken
429 </para></description>
431 <configOption name="faxdetect" default="no">
432 <synopsis>Whether CNG tone detection is enabled</synopsis>
434 This option can be set to send the session to the fax extension when a CNG tone is
436 </para></description>
438 <configOption name="t38udptl_nat" default="no">
439 <synopsis>Whether NAT support is enabled on UDPTL sessions</synopsis>
441 When enabled the UDPTL stack will send UDPTL packets to the source address of
443 </para></description>
445 <configOption name="t38udptl_ipv6" default="no">
446 <synopsis>Whether IPv6 is used for UDPTL Sessions</synopsis>
448 When enabled the UDPTL stack will use IPv6.
449 </para></description>
451 <configOption name="tonezone">
452 <synopsis>Set which country's indications to use for channels created for this endpoint.</synopsis>
454 <configOption name="language">
455 <synopsis>Set the default language to use for channels created for this endpoint.</synopsis>
457 <configOption name="one_touch_recording" default="no">
458 <synopsis>Determines whether one-touch recording is allowed for this endpoint.</synopsis>
460 <ref type="configOption">recordonfeature</ref>
461 <ref type="configOption">recordofffeature</ref>
464 <configOption name="recordonfeature" default="automixmon">
465 <synopsis>The feature to enact when one-touch recording is turned on.</synopsis>
467 <para>When an INFO request for one-touch recording arrives with a Record header set to "on", this
468 feature will be enabled for the channel. The feature designated here can be any built-in
469 or dynamic feature defined in features.conf.</para>
470 <note><para>This setting has no effect if the endpoint's one_touch_recording option is disabled</para></note>
473 <ref type="configOption">one_touch_recording</ref>
474 <ref type="configOption">recordofffeature</ref>
477 <configOption name="recordofffeature" default="automixmon">
478 <synopsis>The feature to enact when one-touch recording is turned off.</synopsis>
480 <para>When an INFO request for one-touch recording arrives with a Record header set to "off", this
481 feature will be enabled for the channel. The feature designated here can be any built-in
482 or dynamic feature defined in features.conf.</para>
483 <note><para>This setting has no effect if the endpoint's one_touch_recording option is disabled</para></note>
486 <ref type="configOption">one_touch_recording</ref>
487 <ref type="configOption">recordonfeature</ref>
490 <configOption name="rtpengine" default="asterisk">
491 <synopsis>Name of the RTP engine to use for channels created for this endpoint</synopsis>
493 <configOption name="allowtransfer" default="yes">
494 <synopsis>Determines whether SIP REFER transfers are allowed for this endpoint</synopsis>
496 <configOption name="sdpowner" default="-">
497 <synopsis>String placed as the username portion of an SDP origin (o=) line.</synopsis>
499 <configOption name="sdpsession" default="Asterisk">
500 <synopsis>String used for the SDP session (s=) line.</synopsis>
502 <configOption name="tos_audio">
503 <synopsis>DSCP TOS bits for audio streams</synopsis>
505 See https://wiki.asterisk.org/wiki/display/AST/IP+Quality+of+Service for more information about QoS settings
506 </para></description>
508 <configOption name="tos_video">
509 <synopsis>DSCP TOS bits for video streams</synopsis>
511 See https://wiki.asterisk.org/wiki/display/AST/IP+Quality+of+Service for more information about QoS settings
512 </para></description>
514 <configOption name="cos_audio">
515 <synopsis>Priority for audio streams</synopsis>
517 See https://wiki.asterisk.org/wiki/display/AST/IP+Quality+of+Service for more information about QoS settings
518 </para></description>
520 <configOption name="cos_video">
521 <synopsis>Priority for video streams</synopsis>
523 See https://wiki.asterisk.org/wiki/display/AST/IP+Quality+of+Service for more information about QoS settings
524 </para></description>
526 <configOption name="allowsubscribe" default="yes">
527 <synopsis>Determines if endpoint is allowed to initiate subscriptions with Asterisk.</synopsis>
529 <configOption name="subminexpiry" default="60">
530 <synopsis>The minimum allowed expiry time for subscriptions initiated by the endpoint.</synopsis>
532 <configOption name="fromuser">
533 <synopsis>Username to use in From header for requests to this endpoint.</synopsis>
535 <configOption name="mwifromuser">
536 <synopsis>Username to use in From header for unsolicited MWI NOTIFYs to this endpoint.</synopsis>
538 <configOption name="fromdomain">
539 <synopsis>Domain to user in From header for requests to this endpoint.</synopsis>
541 <configOption name="dtlsverify">
542 <synopsis>Verify that the provided peer certificate is valid</synopsis>
544 This option only applies if <replaceable>media_encryption</replaceable> is
545 set to <literal>dtls</literal>.
546 </para></description>
548 <configOption name="dtlsrekey">
549 <synopsis>Interval at which to renegotiate the TLS session and rekey the SRTP session</synopsis>
551 This option only applies if <replaceable>media_encryption</replaceable> is
552 set to <literal>dtls</literal>.
554 If this is not set or the value provided is 0 rekeying will be disabled.
555 </para></description>
557 <configOption name="dtlscertfile">
558 <synopsis>Path to certificate file to present to peer</synopsis>
560 This option only applies if <replaceable>media_encryption</replaceable> is
561 set to <literal>dtls</literal>.
562 </para></description>
564 <configOption name="dtlsprivatekey">
565 <synopsis>Path to private key for certificate file</synopsis>
567 This option only applies if <replaceable>media_encryption</replaceable> is
568 set to <literal>dtls</literal>.
569 </para></description>
571 <configOption name="dtlscipher">
572 <synopsis>Cipher to use for DTLS negotiation</synopsis>
574 This option only applies if <replaceable>media_encryption</replaceable> is
575 set to <literal>dtls</literal>.
577 Many options for acceptable ciphers. See link for more:
578 http://www.openssl.org/docs/apps/ciphers.html#CIPHER_STRINGS
579 </para></description>
581 <configOption name="dtlscafile">
582 <synopsis>Path to certificate authority certificate</synopsis>
584 This option only applies if <replaceable>media_encryption</replaceable> is
585 set to <literal>dtls</literal>.
586 </para></description>
588 <configOption name="dtlscapath">
589 <synopsis>Path to a directory containing certificate authority certificates</synopsis>
591 This option only applies if <replaceable>media_encryption</replaceable> is
592 set to <literal>dtls</literal>.
593 </para></description>
595 <configOption name="dtlssetup">
596 <synopsis>Whether we are willing to accept connections, connect to the other party, or both.</synopsis>
599 This option only applies if <replaceable>media_encryption</replaceable> is
600 set to <literal>dtls</literal>.
603 <enum name="active"><para>
604 res_pjsip will make a connection to the peer.
606 <enum name="passive"><para>
607 res_pjsip will accept connections from the peer.
609 <enum name="actpass"><para>
610 res_pjsip will offer and accept connections from the peer.
615 <configOption name="srtp_tag_32">
616 <synopsis>Determines whether 32 byte tags should be used instead of 80 byte tags.</synopsis>
618 This option only applies if <replaceable>media_encryption</replaceable> is
619 set to <literal>sdes</literal> or <literal>dtls</literal>.
620 </para></description>
623 <configObject name="auth">
624 <synopsis>Authentication type</synopsis>
626 Authentication objects hold the authenitcation information for use
627 by <literal>endpoints</literal>. This also allows for multiple <literal>
628 endpoints</literal> to use the same information. Choice of MD5/plaintext
629 and setting of username.
630 </para></description>
631 <configOption name="auth_type" default="userpass">
632 <synopsis>Authentication type</synopsis>
634 This option specifies which of the password style config options should be read,
635 either 'password' or 'md5_cred' when trying to authenticate an endpoint inbound request.
639 <enum name="userpass"/>
643 <configOption name="nonce_lifetime" default="32">
644 <synopsis>Lifetime of a nonce associated with this authentication config.</synopsis>
646 <configOption name="md5_cred">
647 <synopsis>MD5 Hash used for authentication.</synopsis>
648 <description><para>Only used when auth_type is <literal>md5</literal>.</para></description>
650 <configOption name="password">
651 <synopsis>PlainText password used for authentication.</synopsis>
652 <description><para>Only used when auth_type is <literal>userpass</literal>.</para></description>
654 <configOption name="realm" default="asterisk">
655 <synopsis>SIP realm for endpoint</synopsis>
657 <configOption name="type">
658 <synopsis>Must be 'auth'</synopsis>
660 <configOption name="username">
661 <synopsis>Username to use for account</synopsis>
664 <configObject name="nat_hook">
665 <synopsis>XXX This exists only to prevent XML documentation errors.</synopsis>
666 <configOption name="external_media_address">
667 <synopsis>I should be undocumented or hidden</synopsis>
669 <configOption name="method">
670 <synopsis>I should be undocumented or hidden</synopsis>
673 <configObject name="domain_alias">
674 <synopsis>Domain Alias</synopsis>
676 Signifies that a domain is an alias. Used for checking the domain of
677 the AoR to which the endpoint is binding.
678 </para></description>
679 <configOption name="type">
680 <synopsis>Must be of type 'domain_alias'.</synopsis>
682 <configOption name="domain">
683 <synopsis>Domain to be aliased</synopsis>
686 <configObject name="transport">
687 <synopsis>SIP Transport</synopsis>
689 <emphasis>Transports</emphasis>
691 <para>There are different transports and protocol derivatives
692 supported by <literal>res_pjsip</literal>. They are in order of
693 preference: UDP, TCP, and WebSocket (WS).</para>
695 Multiple endpoints using the same connection is <emphasis>NOT</emphasis>
696 supported. Doing so may result in broken calls.
699 <configOption name="async_operations" default="1">
700 <synopsis>Number of simultaneous Asynchronous Operations</synopsis>
702 <configOption name="bind">
703 <synopsis>IP Address and optional port to bind to for this transport</synopsis>
705 <configOption name="ca_list_file">
706 <synopsis>File containing a list of certificates to read (TLS ONLY)</synopsis>
708 <configOption name="cert_file">
709 <synopsis>Certificate file for endpoint (TLS ONLY)</synopsis>
711 <configOption name="cipher">
712 <synopsis>Preferred Cryptography Cipher (TLS ONLY)</synopsis>
714 Many options for acceptable ciphers see link for more:
715 http://www.openssl.org/docs/apps/ciphers.html#CIPHER_STRINGS
716 </para></description>
718 <configOption name="domain">
719 <synopsis>Domain the transport comes from</synopsis>
721 <configOption name="external_media_address">
722 <synopsis>External Address to use in RTP handling</synopsis>
724 <configOption name="external_signaling_address">
725 <synopsis>External address for SIP signalling</synopsis>
727 <configOption name="external_signaling_port" default="0">
728 <synopsis>External port for SIP signalling</synopsis>
730 <configOption name="method">
731 <synopsis>Method of SSL transport (TLS ONLY)</synopsis>
734 <enum name="default" />
735 <enum name="unspecified" />
736 <enum name="tlsv1" />
737 <enum name="sslv2" />
738 <enum name="sslv3" />
739 <enum name="sslv23" />
743 <configOption name="localnet">
744 <synopsis>Network to consider local (used for NAT purposes).</synopsis>
745 <description><para>This must be in CIDR or dotted decimal format with the IP
746 and mask separated with a slash ('/').</para></description>
748 <configOption name="password">
749 <synopsis>Password required for transport</synopsis>
751 <configOption name="privkey_file">
752 <synopsis>Private key file (TLS ONLY)</synopsis>
754 <configOption name="protocol" default="udp">
755 <synopsis>Protocol to use for SIP traffic</synopsis>
764 <configOption name="require_client_cert" default="false">
765 <synopsis>Require client certificate (TLS ONLY)</synopsis>
767 <configOption name="type">
768 <synopsis>Must be of type 'transport'.</synopsis>
770 <configOption name="verify_client" default="false">
771 <synopsis>Require verification of client certificate (TLS ONLY)</synopsis>
773 <configOption name="verify_server" default="false">
774 <synopsis>Require verification of server certificate (TLS ONLY)</synopsis>
777 <configObject name="contact">
778 <synopsis>A way of creating an aliased name to a SIP URI</synopsis>
780 Contacts are a way to hide SIP URIs from the dialplan directly.
781 They are also used to make a group of contactable parties when
782 in use with <literal>AoR</literal> lists.
783 </para></description>
784 <configOption name="type">
785 <synopsis>Must be of type 'contact'.</synopsis>
787 <configOption name="uri">
788 <synopsis>SIP URI to contact peer</synopsis>
790 <configOption name="expiration_time">
791 <synopsis>Time to keep alive a contact</synopsis>
793 Time to keep alive a contact. String style specification.
794 </para></description>
796 <configOption name="qualify_frequency" default="0">
797 <synopsis>Interval at which to qualify a contact</synopsis>
799 Interval between attempts to qualify the contact for reachability.
800 If <literal>0</literal> never qualify. Time in seconds.
801 </para></description>
804 <configObject name="contact_status">
805 <synopsis>Status for a contact</synopsis>
807 The contact status keeps track of whether or not a contact is reachable
808 and how long it took to qualify the contact (round trip time).
809 </para></description>
810 <configOption name="status">
811 <synopsis>A contact's status</synopsis>
814 <enum name="AVAILABLE" />
815 <enum name="UNAVAILABLE" />
819 <configOption name="rtt">
820 <synopsis>Round trip time</synopsis>
822 The time, in microseconds, it took to qualify the contact.
823 </para></description>
826 <configObject name="aor">
827 <synopsis>The configuration for a location of an endpoint</synopsis>
829 An AoR is what allows Asterisk to contact an endpoint via res_pjsip. If no
830 AoRs are specified, an endpoint will not be reachable by Asterisk.
831 Beyond that, an AoR has other uses within Asterisk.
833 An <literal>AoR</literal> is a way to allow dialing a group
834 of <literal>Contacts</literal> that all use the same
835 <literal>endpoint</literal> for calls.
837 This can be used as another way of grouping a list of contacts to dial
838 rather than specifing them each directly when dialing via the dialplan.
839 This must be used in conjuction with the <literal>PJSIP_DIAL_CONTACTS</literal>.
840 </para></description>
841 <configOption name="contact">
842 <synopsis>Permanent contacts assigned to AoR</synopsis>
844 Contacts included in this list will be called whenever referenced
845 by <literal>chan_pjsip</literal>.
846 </para></description>
848 <configOption name="default_expiration" default="3600">
849 <synopsis>Default expiration time in seconds for contacts that are dynamically bound to an AoR.</synopsis>
851 <configOption name="mailboxes">
852 <synopsis>Mailbox(es) to be associated with</synopsis>
853 <description><para>This option applies when an external entity subscribes to an AoR
854 for message waiting indications. The mailboxes specified here will be
855 subscribed to.</para></description>
857 <configOption name="maximum_expiration" default="7200">
858 <synopsis>Maximum time to keep an AoR</synopsis>
860 Maximium time to keep a peer with explicit expiration. Time in seconds.
861 </para></description>
863 <configOption name="max_contacts" default="0">
864 <synopsis>Maximum number of contacts that can bind to an AoR</synopsis>
866 Maximum number of contacts that can associate with this AoR.
868 <note><para>This should be set to <literal>1</literal> and
869 <replaceable>remove_existing</replaceable> set to <literal>yes</literal> if you
870 wish to stick with the older <literal>chan_sip</literal> behaviour.
874 <configOption name="minimum_expiration" default="60">
875 <synopsis>Minimum keep alive time for an AoR</synopsis>
877 Minimum time to keep a peer with an explict expiration. Time in seconds.
878 </para></description>
880 <configOption name="remove_existing" default="no">
881 <synopsis>Determines whether new contacts replace existing ones.</synopsis>
883 On receiving a new registration to the AoR should it remove
884 the existing contact that was registered against it?
886 <note><para>This should be set to <literal>yes</literal> and
887 <replaceable>max_contacts</replaceable> set to <literal>1</literal> if you
888 wish to stick with the older <literal>chan_sip</literal> behaviour.
892 <configOption name="type">
893 <synopsis>Must be of type 'aor'.</synopsis>
895 <configOption name="qualify_frequency" default="0">
896 <synopsis>Interval at which to qualify an AoR</synopsis>
898 Interval between attempts to qualify the AoR for reachability.
899 If <literal>0</literal> never qualify. Time in seconds.
900 </para></description>
902 <configOption name="authenticate_qualify" default="no">
903 <synopsis>Authenticates a qualify request if needed</synopsis>
905 If true and a qualify request receives a challenge or authenticate response
906 authentication is attempted before declaring the contact available.
907 </para></description>
910 <configObject name="system">
911 <synopsis>Options that apply to the SIP stack as well as other system-wide settings</synopsis>
913 The settings in this section are global. In addition to being global, the values will
914 not be re-evaluated when a reload is performed. This is because the values must be set
915 before the SIP stack is initialized. The only way to reset these values is to either
916 restart Asterisk, or unload res_pjsip.so and then load it again.
917 </para></description>
918 <configOption name="timert1" default="500">
919 <synopsis>Set transaction timer T1 value (milliseconds).</synopsis>
921 Timer T1 is the base for determining how long to wait before retransmitting
922 requests that receive no response when using an unreliable transport (e.g. UDP).
923 For more information on this timer, see RFC 3261, Section 17.1.1.1.
924 </para></description>
926 <configOption name="timerb" default="32000">
927 <synopsis>Set transaction timer B value (milliseconds).</synopsis>
929 Timer B determines the maximum amount of time to wait after sending an INVITE
930 request before terminating the transaction. It is recommended that this be set
931 to 64 * Timer T1, but it may be set higher if desired. For more information on
932 this timer, see RFC 3261, Section 17.1.1.1.
933 </para></description>
935 <configOption name="compactheaders" default="no">
936 <synopsis>Use the short forms of common SIP header names.</synopsis>
939 <configObject name="global">
940 <synopsis>Options that apply globally to all SIP communications</synopsis>
942 The settings in this section are global. Unlike options in the <literal>system</literal>
943 section, these options can be refreshed by performing a reload.
944 </para></description>
945 <configOption name="maxforwards" default="70">
946 <synopsis>Value used in Max-Forwards header for SIP requests.</synopsis>
948 <configOption name="useragent" default="Asterisk <Asterisk Version>">
949 <synopsis>Value used in User-Agent header for SIP requests and Server header for SIP responses.</synopsis>
957 static pjsip_endpoint *ast_pjsip_endpoint;
959 static struct ast_threadpool *sip_threadpool;
961 static int register_service(void *data)
963 pjsip_module **module = data;
964 if (!ast_pjsip_endpoint) {
965 ast_log(LOG_ERROR, "There is no PJSIP endpoint. Unable to register services\n");
968 if (pjsip_endpt_register_module(ast_pjsip_endpoint, *module) != PJ_SUCCESS) {
969 ast_log(LOG_ERROR, "Unable to register module %.*s\n", (int) pj_strlen(&(*module)->name), pj_strbuf(&(*module)->name));
972 ast_debug(1, "Registered SIP service %.*s (%p)\n", (int) pj_strlen(&(*module)->name), pj_strbuf(&(*module)->name), *module);
973 ast_module_ref(ast_module_info->self);
977 int ast_sip_register_service(pjsip_module *module)
979 return ast_sip_push_task_synchronous(NULL, register_service, &module);
982 static int unregister_service(void *data)
984 pjsip_module **module = data;
985 ast_module_unref(ast_module_info->self);
986 if (!ast_pjsip_endpoint) {
989 pjsip_endpt_unregister_module(ast_pjsip_endpoint, *module);
990 ast_debug(1, "Unregistered SIP service %.*s\n", (int) pj_strlen(&(*module)->name), pj_strbuf(&(*module)->name));
994 void ast_sip_unregister_service(pjsip_module *module)
996 ast_sip_push_task_synchronous(NULL, unregister_service, &module);
999 static struct ast_sip_authenticator *registered_authenticator;
1001 int ast_sip_register_authenticator(struct ast_sip_authenticator *auth)
1003 if (registered_authenticator) {
1004 ast_log(LOG_WARNING, "Authenticator %p is already registered. Cannot register a new one\n", registered_authenticator);
1007 registered_authenticator = auth;
1008 ast_debug(1, "Registered SIP authenticator module %p\n", auth);
1009 ast_module_ref(ast_module_info->self);
1013 void ast_sip_unregister_authenticator(struct ast_sip_authenticator *auth)
1015 if (registered_authenticator != auth) {
1016 ast_log(LOG_WARNING, "Trying to unregister authenticator %p but authenticator %p registered\n",
1017 auth, registered_authenticator);
1020 registered_authenticator = NULL;
1021 ast_debug(1, "Unregistered SIP authenticator %p\n", auth);
1022 ast_module_unref(ast_module_info->self);
1025 int ast_sip_requires_authentication(struct ast_sip_endpoint *endpoint, pjsip_rx_data *rdata)
1027 if (!registered_authenticator) {
1028 ast_log(LOG_WARNING, "No SIP authenticator registered. Assuming authentication is not required\n");
1032 return registered_authenticator->requires_authentication(endpoint, rdata);
1035 enum ast_sip_check_auth_result ast_sip_check_authentication(struct ast_sip_endpoint *endpoint,
1036 pjsip_rx_data *rdata, pjsip_tx_data *tdata)
1038 if (!registered_authenticator) {
1039 ast_log(LOG_WARNING, "No SIP authenticator registered. Assuming authentication is successful\n");
1042 return registered_authenticator->check_authentication(endpoint, rdata, tdata);
1045 static struct ast_sip_outbound_authenticator *registered_outbound_authenticator;
1047 int ast_sip_register_outbound_authenticator(struct ast_sip_outbound_authenticator *auth)
1049 if (registered_outbound_authenticator) {
1050 ast_log(LOG_WARNING, "Outbound authenticator %p is already registered. Cannot register a new one\n", registered_outbound_authenticator);
1053 registered_outbound_authenticator = auth;
1054 ast_debug(1, "Registered SIP outbound authenticator module %p\n", auth);
1055 ast_module_ref(ast_module_info->self);
1059 void ast_sip_unregister_outbound_authenticator(struct ast_sip_outbound_authenticator *auth)
1061 if (registered_outbound_authenticator != auth) {
1062 ast_log(LOG_WARNING, "Trying to unregister outbound authenticator %p but outbound authenticator %p registered\n",
1063 auth, registered_outbound_authenticator);
1066 registered_outbound_authenticator = NULL;
1067 ast_debug(1, "Unregistered SIP outbound authenticator %p\n", auth);
1068 ast_module_unref(ast_module_info->self);
1071 int ast_sip_create_request_with_auth(const struct ast_sip_auth_array *auths, pjsip_rx_data *challenge,
1072 pjsip_transaction *tsx, pjsip_tx_data **new_request)
1074 if (!registered_outbound_authenticator) {
1075 ast_log(LOG_WARNING, "No SIP outbound authenticator registered. Cannot respond to authentication challenge\n");
1078 return registered_outbound_authenticator->create_request_with_auth(auths, challenge, tsx, new_request);
1081 struct endpoint_identifier_list {
1082 struct ast_sip_endpoint_identifier *identifier;
1083 AST_RWLIST_ENTRY(endpoint_identifier_list) list;
1086 static AST_RWLIST_HEAD_STATIC(endpoint_identifiers, endpoint_identifier_list);
1088 int ast_sip_register_endpoint_identifier(struct ast_sip_endpoint_identifier *identifier)
1090 struct endpoint_identifier_list *id_list_item;
1091 SCOPED_LOCK(lock, &endpoint_identifiers, AST_RWLIST_WRLOCK, AST_RWLIST_UNLOCK);
1093 id_list_item = ast_calloc(1, sizeof(*id_list_item));
1094 if (!id_list_item) {
1095 ast_log(LOG_ERROR, "Unabled to add endpoint identifier. Out of memory.\n");
1098 id_list_item->identifier = identifier;
1100 AST_RWLIST_INSERT_TAIL(&endpoint_identifiers, id_list_item, list);
1101 ast_debug(1, "Registered endpoint identifier %p\n", identifier);
1103 ast_module_ref(ast_module_info->self);
1107 void ast_sip_unregister_endpoint_identifier(struct ast_sip_endpoint_identifier *identifier)
1109 struct endpoint_identifier_list *iter;
1110 SCOPED_LOCK(lock, &endpoint_identifiers, AST_RWLIST_WRLOCK, AST_RWLIST_UNLOCK);
1111 AST_RWLIST_TRAVERSE_SAFE_BEGIN(&endpoint_identifiers, iter, list) {
1112 if (iter->identifier == identifier) {
1113 AST_RWLIST_REMOVE_CURRENT(list);
1115 ast_debug(1, "Unregistered endpoint identifier %p\n", identifier);
1116 ast_module_unref(ast_module_info->self);
1120 AST_RWLIST_TRAVERSE_SAFE_END;
1123 struct ast_sip_endpoint *ast_sip_identify_endpoint(pjsip_rx_data *rdata)
1125 struct endpoint_identifier_list *iter;
1126 struct ast_sip_endpoint *endpoint = NULL;
1127 SCOPED_LOCK(lock, &endpoint_identifiers, AST_RWLIST_RDLOCK, AST_RWLIST_UNLOCK);
1128 AST_RWLIST_TRAVERSE(&endpoint_identifiers, iter, list) {
1129 ast_assert(iter->identifier->identify_endpoint != NULL);
1130 endpoint = iter->identifier->identify_endpoint(rdata);
1138 pjsip_endpoint *ast_sip_get_pjsip_endpoint(void)
1140 return ast_pjsip_endpoint;
1143 static int sip_dialog_create_from(pj_pool_t *pool, pj_str_t *from, const char *user, const char *domain, const pj_str_t *target, pjsip_tpselector *selector)
1145 pj_str_t tmp, local_addr;
1147 pjsip_sip_uri *sip_uri;
1148 pjsip_transport_type_e type = PJSIP_TRANSPORT_UNSPECIFIED;
1150 char uuid_str[AST_UUID_STR_LEN];
1152 if (ast_strlen_zero(user)) {
1153 RAII_VAR(struct ast_uuid *, uuid, ast_uuid_generate(), ast_free_ptr);
1157 user = ast_uuid_to_str(uuid, uuid_str, sizeof(uuid_str));
1160 /* Parse the provided target URI so we can determine what transport it will end up using */
1161 pj_strdup_with_null(pool, &tmp, target);
1163 if (!(uri = pjsip_parse_uri(pool, tmp.ptr, tmp.slen, 0)) ||
1164 (!PJSIP_URI_SCHEME_IS_SIP(uri) && !PJSIP_URI_SCHEME_IS_SIPS(uri))) {
1168 sip_uri = pjsip_uri_get_uri(uri);
1170 /* Determine the transport type to use */
1171 if (PJSIP_URI_SCHEME_IS_SIPS(sip_uri)) {
1172 type = PJSIP_TRANSPORT_TLS;
1173 } else if (!sip_uri->transport_param.slen) {
1174 type = PJSIP_TRANSPORT_UDP;
1176 type = pjsip_transport_get_type_from_name(&sip_uri->transport_param);
1179 if (type == PJSIP_TRANSPORT_UNSPECIFIED) {
1183 /* If the host is IPv6 turn the transport into an IPv6 version */
1184 if (pj_strchr(&sip_uri->host, ':') && type < PJSIP_TRANSPORT_START_OTHER) {
1185 type = (pjsip_transport_type_e)(((int)type) + PJSIP_TRANSPORT_IPV6);
1188 if (!ast_strlen_zero(domain)) {
1189 from->ptr = pj_pool_alloc(pool, PJSIP_MAX_URL_SIZE);
1190 from->slen = pj_ansi_snprintf(from->ptr, PJSIP_MAX_URL_SIZE,
1192 (pjsip_transport_get_flag_from_type(type) & PJSIP_TRANSPORT_SECURE) ? "sips" : "sip",
1195 (type != PJSIP_TRANSPORT_UDP && type != PJSIP_TRANSPORT_UDP6) ? ";transport=" : "",
1196 (type != PJSIP_TRANSPORT_UDP && type != PJSIP_TRANSPORT_UDP6) ? pjsip_transport_get_type_name(type) : "");
1200 /* Get the local bound address for the transport that will be used when communicating with the provided URI */
1201 if (pjsip_tpmgr_find_local_addr(pjsip_endpt_get_tpmgr(ast_sip_get_pjsip_endpoint()), pool, type, selector,
1202 &local_addr, &local_port) != PJ_SUCCESS) {
1206 /* If IPv6 was specified in the transport, set the proper type */
1207 if (pj_strchr(&local_addr, ':') && type < PJSIP_TRANSPORT_START_OTHER) {
1208 type = (pjsip_transport_type_e)(((int)type) + PJSIP_TRANSPORT_IPV6);
1211 from->ptr = pj_pool_alloc(pool, PJSIP_MAX_URL_SIZE);
1212 from->slen = pj_ansi_snprintf(from->ptr, PJSIP_MAX_URL_SIZE,
1213 "<%s:%s@%s%.*s%s:%d%s%s>",
1214 (pjsip_transport_get_flag_from_type(type) & PJSIP_TRANSPORT_SECURE) ? "sips" : "sip",
1216 (type & PJSIP_TRANSPORT_IPV6) ? "[" : "",
1217 (int)local_addr.slen,
1219 (type & PJSIP_TRANSPORT_IPV6) ? "]" : "",
1221 (type != PJSIP_TRANSPORT_UDP && type != PJSIP_TRANSPORT_UDP6) ? ";transport=" : "",
1222 (type != PJSIP_TRANSPORT_UDP && type != PJSIP_TRANSPORT_UDP6) ? pjsip_transport_get_type_name(type) : "");
1227 static int sip_get_tpselector_from_endpoint(const struct ast_sip_endpoint *endpoint, pjsip_tpselector *selector)
1229 RAII_VAR(struct ast_sip_transport *, transport, NULL, ao2_cleanup);
1230 const char *transport_name = endpoint->transport;
1232 if (ast_strlen_zero(transport_name)) {
1236 transport = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "transport", transport_name);
1238 if (!transport || !transport->state) {
1242 if (transport->state->transport) {
1243 selector->type = PJSIP_TPSELECTOR_TRANSPORT;
1244 selector->u.transport = transport->state->transport;
1245 } else if (transport->state->factory) {
1246 selector->type = PJSIP_TPSELECTOR_LISTENER;
1247 selector->u.listener = transport->state->factory;
1255 static int sip_get_tpselector_from_uri(const char *uri, pjsip_tpselector *selector)
1257 RAII_VAR(struct ast_sip_contact_transport *, contact_transport, NULL, ao2_cleanup);
1259 contact_transport = ast_sip_location_retrieve_contact_transport_by_uri(uri);
1261 if (!contact_transport) {
1265 selector->type = PJSIP_TPSELECTOR_TRANSPORT;
1266 selector->u.transport = contact_transport->transport;
1271 pjsip_dialog *ast_sip_create_dialog(const struct ast_sip_endpoint *endpoint, const char *uri, const char *request_user)
1273 pj_str_t local_uri = { "sip:temp@temp", 13 }, remote_uri;
1274 pjsip_dialog *dlg = NULL;
1275 const char *outbound_proxy = endpoint->outbound_proxy;
1276 pjsip_tpselector selector = { .type = PJSIP_TPSELECTOR_NONE, };
1277 static const pj_str_t HCONTACT = { "Contact", 7 };
1279 pj_cstr(&remote_uri, uri);
1281 if (pjsip_dlg_create_uac(pjsip_ua_instance(), &local_uri, NULL, &remote_uri, NULL, &dlg) != PJ_SUCCESS) {
1285 if (sip_get_tpselector_from_uri(uri, &selector) && sip_get_tpselector_from_endpoint(endpoint, &selector)) {
1286 pjsip_dlg_terminate(dlg);
1290 if (sip_dialog_create_from(dlg->pool, &local_uri, endpoint->fromuser, endpoint->fromdomain, &remote_uri, &selector)) {
1291 pjsip_dlg_terminate(dlg);
1295 /* Update the dialog with the new local URI, we do it afterwards so we can use the dialog pool for construction */
1296 pj_strdup_with_null(dlg->pool, &dlg->local.info_str, &local_uri);
1297 dlg->local.info->uri = pjsip_parse_uri(dlg->pool, dlg->local.info_str.ptr, dlg->local.info_str.slen, 0);
1298 dlg->local.contact = pjsip_parse_hdr(dlg->pool, &HCONTACT, local_uri.ptr, local_uri.slen, NULL);
1300 /* If a request user has been specified and we are permitted to change it, do so */
1301 if (!ast_strlen_zero(request_user) && (PJSIP_URI_SCHEME_IS_SIP(dlg->target) || PJSIP_URI_SCHEME_IS_SIPS(dlg->target))) {
1302 pjsip_sip_uri *target = pjsip_uri_get_uri(dlg->target);
1303 pj_strdup2(dlg->pool, &target->user, request_user);
1306 /* We have to temporarily bump up the sess_count here so the dialog is not prematurely destroyed */
1309 pjsip_dlg_set_transport(dlg, &selector);
1311 if (!ast_strlen_zero(outbound_proxy)) {
1312 pjsip_route_hdr route_set, *route;
1313 static const pj_str_t ROUTE_HNAME = { "Route", 5 };
1316 pj_list_init(&route_set);
1318 pj_strdup2_with_null(dlg->pool, &tmp, outbound_proxy);
1319 if (!(route = pjsip_parse_hdr(dlg->pool, &ROUTE_HNAME, tmp.ptr, tmp.slen, NULL))) {
1320 pjsip_dlg_terminate(dlg);
1323 pj_list_push_back(&route_set, route);
1325 pjsip_dlg_set_route_set(dlg, &route_set);
1333 /* PJSIP doesn't know about the INFO method, so we have to define it ourselves */
1334 const pjsip_method pjsip_info_method = {PJSIP_OTHER_METHOD, {"INFO", 4} };
1335 const pjsip_method pjsip_message_method = {PJSIP_OTHER_METHOD, {"MESSAGE", 7} };
1339 const pjsip_method *pmethod;
1341 { "INVITE", &pjsip_invite_method },
1342 { "CANCEL", &pjsip_cancel_method },
1343 { "ACK", &pjsip_ack_method },
1344 { "BYE", &pjsip_bye_method },
1345 { "REGISTER", &pjsip_register_method },
1346 { "OPTIONS", &pjsip_options_method },
1347 { "SUBSCRIBE", &pjsip_subscribe_method },
1348 { "NOTIFY", &pjsip_notify_method },
1349 { "PUBLISH", &pjsip_publish_method },
1350 { "INFO", &pjsip_info_method },
1351 { "MESSAGE", &pjsip_message_method },
1354 static const pjsip_method *get_pjsip_method(const char *method)
1357 for (i = 0; i < ARRAY_LEN(methods); ++i) {
1358 if (!strcmp(method, methods[i].method)) {
1359 return methods[i].pmethod;
1365 static int create_in_dialog_request(const pjsip_method *method, struct pjsip_dialog *dlg, pjsip_tx_data **tdata)
1367 if (pjsip_dlg_create_request(dlg, method, -1, tdata) != PJ_SUCCESS) {
1368 ast_log(LOG_WARNING, "Unable to create in-dialog request.\n");
1375 static int create_out_of_dialog_request(const pjsip_method *method, struct ast_sip_endpoint *endpoint,
1376 const char *uri, pjsip_tx_data **tdata)
1378 RAII_VAR(struct ast_sip_contact *, contact, NULL, ao2_cleanup);
1379 pj_str_t remote_uri;
1382 pjsip_tpselector selector = { .type = PJSIP_TPSELECTOR_NONE, };
1384 if (ast_strlen_zero(uri)) {
1386 ast_log(LOG_ERROR, "An endpoint and/or uri must be specified\n");
1390 contact = ast_sip_location_retrieve_contact_from_aor_list(endpoint->aors);
1391 if (!contact || ast_strlen_zero(contact->uri)) {
1392 ast_log(LOG_ERROR, "Unable to retrieve contact for endpoint %s\n",
1393 ast_sorcery_object_get_id(endpoint));
1397 pj_cstr(&remote_uri, contact->uri);
1399 pj_cstr(&remote_uri, uri);
1403 if (sip_get_tpselector_from_endpoint(endpoint, &selector)) {
1404 ast_log(LOG_ERROR, "Unable to retrieve PJSIP transport selector for endpoint %s\n",
1405 ast_sorcery_object_get_id(endpoint));
1410 pool = pjsip_endpt_create_pool(ast_sip_get_pjsip_endpoint(), "Outbound request", 256, 256);
1413 ast_log(LOG_ERROR, "Unable to create PJLIB memory pool\n");
1417 if (sip_dialog_create_from(pool, &from, endpoint ? endpoint->fromuser : NULL,
1418 endpoint ? endpoint->fromdomain : NULL, &remote_uri, &selector)) {
1419 ast_log(LOG_ERROR, "Unable to create From header for %.*s request to endpoint %s\n",
1420 (int) pj_strlen(&method->name), pj_strbuf(&method->name), ast_sorcery_object_get_id(endpoint));
1421 pjsip_endpt_release_pool(ast_sip_get_pjsip_endpoint(), pool);
1425 if (pjsip_endpt_create_request(ast_sip_get_pjsip_endpoint(), method, &remote_uri,
1426 &from, &remote_uri, &from, NULL, -1, NULL, tdata) != PJ_SUCCESS) {
1427 ast_log(LOG_ERROR, "Unable to create outbound %.*s request to endpoint %s\n",
1428 (int) pj_strlen(&method->name), pj_strbuf(&method->name), ast_sorcery_object_get_id(endpoint));
1429 pjsip_endpt_release_pool(ast_sip_get_pjsip_endpoint(), pool);
1433 /* We can release this pool since request creation copied all the necessary
1434 * data into the outbound request's pool
1436 pjsip_endpt_release_pool(ast_sip_get_pjsip_endpoint(), pool);
1440 int ast_sip_create_request(const char *method, struct pjsip_dialog *dlg,
1441 struct ast_sip_endpoint *endpoint, const char *uri,
1442 pjsip_tx_data **tdata)
1444 const pjsip_method *pmethod = get_pjsip_method(method);
1447 ast_log(LOG_WARNING, "Unknown method '%s'. Cannot send request\n", method);
1452 return create_in_dialog_request(pmethod, dlg, tdata);
1454 return create_out_of_dialog_request(pmethod, endpoint, uri, tdata);
1458 static int send_in_dialog_request(pjsip_tx_data *tdata, struct pjsip_dialog *dlg)
1460 if (pjsip_dlg_send_request(dlg, tdata, -1, NULL) != PJ_SUCCESS) {
1461 ast_log(LOG_WARNING, "Unable to send in-dialog request.\n");
1467 static void send_request_cb(void *token, pjsip_event *e)
1469 RAII_VAR(struct ast_sip_endpoint *, endpoint, token, ao2_cleanup);
1470 pjsip_transaction *tsx = e->body.tsx_state.tsx;
1471 pjsip_rx_data *challenge = e->body.tsx_state.src.rdata;
1472 pjsip_tx_data *tdata;
1474 if (tsx->status_code != 401 && tsx->status_code != 407) {
1478 if (!ast_sip_create_request_with_auth(&endpoint->outbound_auths, challenge, tsx, &tdata)) {
1479 pjsip_endpt_send_request(ast_sip_get_pjsip_endpoint(), tdata, -1, NULL, NULL);
1483 static int send_out_of_dialog_request(pjsip_tx_data *tdata, struct ast_sip_endpoint *endpoint)
1485 ao2_ref(endpoint, +1);
1486 if (pjsip_endpt_send_request(ast_sip_get_pjsip_endpoint(), tdata, -1, endpoint, send_request_cb) != PJ_SUCCESS) {
1487 ast_log(LOG_ERROR, "Error attempting to send outbound %.*s request to endpoint %s\n",
1488 (int) pj_strlen(&tdata->msg->line.req.method.name),
1489 pj_strbuf(&tdata->msg->line.req.method.name),
1490 ast_sorcery_object_get_id(endpoint));
1491 ao2_ref(endpoint, -1);
1498 int ast_sip_send_request(pjsip_tx_data *tdata, struct pjsip_dialog *dlg, struct ast_sip_endpoint *endpoint)
1500 ast_assert(tdata->msg->type == PJSIP_REQUEST_MSG);
1503 return send_in_dialog_request(tdata, dlg);
1505 return send_out_of_dialog_request(tdata, endpoint);
1509 int ast_sip_add_header(pjsip_tx_data *tdata, const char *name, const char *value)
1513 pjsip_generic_string_hdr *hdr;
1515 pj_cstr(&hdr_name, name);
1516 pj_cstr(&hdr_value, value);
1518 hdr = pjsip_generic_string_hdr_create(tdata->pool, &hdr_name, &hdr_value);
1520 pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr *) hdr);
1524 static pjsip_msg_body *ast_body_to_pjsip_body(pj_pool_t *pool, const struct ast_sip_body *body)
1530 pj_cstr(&type, body->type);
1531 pj_cstr(&subtype, body->subtype);
1532 pj_cstr(&body_text, body->body_text);
1534 return pjsip_msg_body_create(pool, &type, &subtype, &body_text);
1537 int ast_sip_add_body(pjsip_tx_data *tdata, const struct ast_sip_body *body)
1539 pjsip_msg_body *pjsip_body = ast_body_to_pjsip_body(tdata->pool, body);
1540 tdata->msg->body = pjsip_body;
1544 int ast_sip_add_body_multipart(pjsip_tx_data *tdata, const struct ast_sip_body *bodies[], int num_bodies)
1547 /* NULL for type and subtype automatically creates "multipart/mixed" */
1548 pjsip_msg_body *body = pjsip_multipart_create(tdata->pool, NULL, NULL);
1550 for (i = 0; i < num_bodies; ++i) {
1551 pjsip_multipart_part *part = pjsip_multipart_create_part(tdata->pool);
1552 part->body = ast_body_to_pjsip_body(tdata->pool, bodies[i]);
1553 pjsip_multipart_add_part(tdata->pool, body, part);
1556 tdata->msg->body = body;
1560 int ast_sip_append_body(pjsip_tx_data *tdata, const char *body_text)
1562 size_t combined_size = strlen(body_text) + tdata->msg->body->len;
1563 struct ast_str *body_buffer = ast_str_alloca(combined_size);
1565 ast_str_set(&body_buffer, 0, "%.*s%s", (int) tdata->msg->body->len, (char *) tdata->msg->body->data, body_text);
1567 tdata->msg->body->data = pj_pool_alloc(tdata->pool, combined_size);
1568 pj_memcpy(tdata->msg->body->data, ast_str_buffer(body_buffer), combined_size);
1569 tdata->msg->body->len = combined_size;
1574 struct ast_taskprocessor *ast_sip_create_serializer(void)
1576 struct ast_taskprocessor *serializer;
1577 RAII_VAR(struct ast_uuid *, uuid, ast_uuid_generate(), ast_free_ptr);
1578 char name[AST_UUID_STR_LEN];
1584 ast_uuid_to_str(uuid, name, sizeof(name));
1586 serializer = ast_threadpool_serializer(name, sip_threadpool);
1593 int ast_sip_push_task(struct ast_taskprocessor *serializer, int (*sip_task)(void *), void *task_data)
1596 return ast_taskprocessor_push(serializer, sip_task, task_data);
1598 return ast_threadpool_push(sip_threadpool, sip_task, task_data);
1602 struct sync_task_data {
1607 int (*task)(void *);
1611 static int sync_task(void *data)
1613 struct sync_task_data *std = data;
1614 std->fail = std->task(std->task_data);
1616 ast_mutex_lock(&std->lock);
1618 ast_cond_signal(&std->cond);
1619 ast_mutex_unlock(&std->lock);
1623 int ast_sip_push_task_synchronous(struct ast_taskprocessor *serializer, int (*sip_task)(void *), void *task_data)
1625 /* This method is an onion */
1626 struct sync_task_data std;
1627 ast_mutex_init(&std.lock);
1628 ast_cond_init(&std.cond, NULL);
1629 std.fail = std.complete = 0;
1630 std.task = sip_task;
1631 std.task_data = task_data;
1634 if (ast_taskprocessor_push(serializer, sync_task, &std)) {
1638 if (ast_threadpool_push(sip_threadpool, sync_task, &std)) {
1643 ast_mutex_lock(&std.lock);
1644 while (!std.complete) {
1645 ast_cond_wait(&std.cond, &std.lock);
1647 ast_mutex_unlock(&std.lock);
1649 ast_mutex_destroy(&std.lock);
1650 ast_cond_destroy(&std.cond);
1654 void ast_copy_pj_str(char *dest, const pj_str_t *src, size_t size)
1656 size_t chars_to_copy = MIN(size - 1, pj_strlen(src));
1657 memcpy(dest, pj_strbuf(src), chars_to_copy);
1658 dest[chars_to_copy] = '\0';
1661 int ast_sip_is_content_type(pjsip_media_type *content_type, char *type, char *subtype)
1663 pjsip_media_type compare;
1665 if (!content_type) {
1669 pjsip_media_type_init2(&compare, type, subtype);
1671 return pjsip_media_type_cmp(content_type, &compare, 0) ? -1 : 0;
1674 pj_caching_pool caching_pool;
1675 pj_pool_t *memory_pool;
1676 pj_thread_t *monitor_thread;
1677 static int monitor_continue;
1679 static void *monitor_thread_exec(void *endpt)
1681 while (monitor_continue) {
1682 const pj_time_val delay = {0, 10};
1683 pjsip_endpt_handle_events(ast_pjsip_endpoint, &delay);
1688 static void stop_monitor_thread(void)
1690 monitor_continue = 0;
1691 pj_thread_join(monitor_thread);
1694 AST_THREADSTORAGE(pj_thread_storage);
1695 AST_THREADSTORAGE(servant_id_storage);
1696 #define SIP_SERVANT_ID 0x5E2F1D
1698 static void sip_thread_start(void)
1700 pj_thread_desc *desc;
1701 pj_thread_t *thread;
1702 uint32_t *servant_id;
1704 servant_id = ast_threadstorage_get(&servant_id_storage, sizeof(*servant_id));
1706 ast_log(LOG_ERROR, "Could not set SIP servant ID in thread-local storage.\n");
1709 *servant_id = SIP_SERVANT_ID;
1711 desc = ast_threadstorage_get(&pj_thread_storage, sizeof(pj_thread_desc));
1713 ast_log(LOG_ERROR, "Could not get thread desc from thread-local storage. Expect awful things to occur\n");
1716 pj_bzero(*desc, sizeof(*desc));
1718 if (pj_thread_register("Asterisk Thread", *desc, &thread) != PJ_SUCCESS) {
1719 ast_log(LOG_ERROR, "Couldn't register thread with PJLIB.\n");
1723 int ast_sip_thread_is_servant(void)
1725 uint32_t *servant_id;
1727 servant_id = ast_threadstorage_get(&servant_id_storage, sizeof(*servant_id));
1732 return *servant_id == SIP_SERVANT_ID;
1735 static void remove_request_headers(pjsip_endpoint *endpt)
1737 const pjsip_hdr *request_headers = pjsip_endpt_get_request_headers(endpt);
1738 pjsip_hdr *iter = request_headers->next;
1740 while (iter != request_headers) {
1741 pjsip_hdr *to_erase = iter;
1743 pj_list_erase(to_erase);
1747 static int load_module(void)
1749 /* The third parameter is just copied from
1750 * example code from PJLIB. This can be adjusted
1755 /* XXX For the time being, create hard-coded threadpool
1756 * options. Just bump up by five threads every time we
1757 * don't have any available threads. Idle threads time
1758 * out after a minute. No maximum size
1760 struct ast_threadpool_options options = {
1761 .version = AST_THREADPOOL_OPTIONS_VERSION,
1762 .auto_increment = 5,
1766 .thread_start = sip_thread_start,
1768 sip_threadpool = ast_threadpool_create("SIP", NULL, &options);
1770 if (pj_init() != PJ_SUCCESS) {
1771 return AST_MODULE_LOAD_DECLINE;
1774 if (pjlib_util_init() != PJ_SUCCESS) {
1776 return AST_MODULE_LOAD_DECLINE;
1779 pj_caching_pool_init(&caching_pool, NULL, 1024 * 1024);
1780 if (pjsip_endpt_create(&caching_pool.factory, "SIP", &ast_pjsip_endpoint) != PJ_SUCCESS) {
1781 ast_log(LOG_ERROR, "Failed to create PJSIP endpoint structure. Aborting load\n");
1785 /* PJSIP will automatically try to add a Max-Forwards header. Since we want to control that,
1786 * we need to stop PJSIP from doing it automatically
1788 remove_request_headers(ast_pjsip_endpoint);
1790 memory_pool = pj_pool_create(&caching_pool.factory, "SIP", 1024, 1024, NULL);
1792 ast_log(LOG_ERROR, "Failed to create memory pool for SIP. Aborting load\n");
1796 if (ast_sip_initialize_system()) {
1797 ast_log(LOG_ERROR, "Failed to initialize SIP system configuration. Aborting load\n");
1801 pjsip_tsx_layer_init_module(ast_pjsip_endpoint);
1802 pjsip_ua_init_module(ast_pjsip_endpoint, NULL);
1804 monitor_continue = 1;
1805 status = pj_thread_create(memory_pool, "SIP", (pj_thread_proc *) &monitor_thread_exec,
1806 NULL, PJ_THREAD_DEFAULT_STACK_SIZE * 2, 0, &monitor_thread);
1807 if (status != PJ_SUCCESS) {
1808 ast_log(LOG_ERROR, "Failed to start SIP monitor thread. Aborting load\n");
1812 ast_sip_initialize_global_headers();
1814 if (ast_res_pjsip_initialize_configuration()) {
1815 ast_log(LOG_ERROR, "Failed to initialize SIP configuration. Aborting load\n");
1819 if (ast_sip_initialize_distributor()) {
1820 ast_log(LOG_ERROR, "Failed to register distributor module. Aborting load\n");
1824 if (ast_sip_initialize_outbound_authentication()) {
1825 ast_log(LOG_ERROR, "Failed to initialize outbound authentication. Aborting load\n");
1829 ast_res_pjsip_init_options_handling(0);
1831 ast_res_pjsip_init_contact_transports();
1833 return AST_MODULE_LOAD_SUCCESS;
1836 ast_sip_destroy_distributor();
1837 ast_res_pjsip_destroy_configuration();
1838 ast_sip_destroy_global_headers();
1839 if (monitor_thread) {
1840 stop_monitor_thread();
1843 pj_pool_release(memory_pool);
1846 if (ast_pjsip_endpoint) {
1847 pjsip_endpt_destroy(ast_pjsip_endpoint);
1848 ast_pjsip_endpoint = NULL;
1850 pj_caching_pool_destroy(&caching_pool);
1851 return AST_MODULE_LOAD_DECLINE;
1854 static int reload_module(void)
1856 if (ast_res_pjsip_reload_configuration()) {
1857 return AST_MODULE_LOAD_DECLINE;
1859 ast_res_pjsip_init_options_handling(1);
1863 static int unload_pjsip(void *data)
1866 pj_pool_release(memory_pool);
1869 if (ast_pjsip_endpoint) {
1870 pjsip_endpt_destroy(ast_pjsip_endpoint);
1871 ast_pjsip_endpoint = NULL;
1873 pj_caching_pool_destroy(&caching_pool);
1877 static int unload_module(void)
1879 ast_sip_destroy_distributor();
1880 ast_res_pjsip_destroy_configuration();
1881 ast_sip_destroy_global_headers();
1882 if (monitor_thread) {
1883 stop_monitor_thread();
1885 /* The thread this is called from cannot call PJSIP/PJLIB functions,
1886 * so we have to push the work to the threadpool to handle
1888 ast_sip_push_task_synchronous(NULL, unload_pjsip, NULL);
1890 ast_threadpool_shutdown(sip_threadpool);
1895 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER, "Basic SIP resource",
1896 .load = load_module,
1897 .unload = unload_module,
1898 .reload = reload_module,
1899 .load_pri = AST_MODPRI_CHANNEL_DEPEND - 5,