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 An endpoint can be identified in multiple ways. Currently, the only supported
224 option is <literal>username</literal>, which matches the endpoint based on the
225 username in the From header.
227 <note><para>Endpoints can also be identified by IP address; however, that method
228 of identification is not handled by this configuration option. See the documentation
229 for the <literal>identify</literal> configuration section for more details on that
230 method of endpoint identification. If this option is set to <literal>username</literal>
231 and an <literal>identify</literal> configuration section exists for the endpoint, then
232 the endpoint can be identified in multiple ways.</para></note>
234 <enum name="username" />
238 <configOption name="mailboxes">
239 <synopsis>Mailbox(es) to be associated with</synopsis>
241 <configOption name="mohsuggest" default="default">
242 <synopsis>Default Music On Hold class</synopsis>
244 <configOption name="outbound_auth">
245 <synopsis>Authentication object used for outbound requests</synopsis>
247 <configOption name="outbound_proxy">
248 <synopsis>Proxy through which to send requests</synopsis>
250 <configOption name="rewrite_contact">
251 <synopsis>Allow Contact header to be rewritten with the source IP address-port</synopsis>
253 <configOption name="rtp_ipv6" default="no">
254 <synopsis>Allow use of IPv6 for RTP traffic</synopsis>
256 <configOption name="rtp_symmetric" default="no">
257 <synopsis>Enforce that RTP must be symmetric</synopsis>
259 <configOption name="send_pai" default="no">
260 <synopsis>Send the P-Asserted-Identity header</synopsis>
262 <configOption name="send_rpid" default="no">
263 <synopsis>Send the Remote-Party-ID header</synopsis>
265 <configOption name="timers_min_se" default="90">
266 <synopsis>Minimum session timers expiration period</synopsis>
268 Minimium session timer expiration period. Time in seconds.
269 </para></description>
271 <configOption name="timers" default="yes">
272 <synopsis>Session timers for SIP packets</synopsis>
275 <enum name="forced" />
277 <enum name="required" />
282 <configOption name="timers_sess_expires" default="1800">
283 <synopsis>Maximum session timer expiration period</synopsis>
285 Maximium session timer expiration period. Time in seconds.
286 </para></description>
288 <configOption name="transport">
289 <synopsis>Desired transport configuration</synopsis>
291 This will set the desired transport configuration to send SIP data through.
293 <warning><para>Not specifying a transport will <emphasis>DEFAULT</emphasis>
294 to the first configured transport in <filename>pjsip.conf</filename> which is
295 valid for the URI we are trying to contact.
297 <warning><para>Transport configuration is not affected by reloads. In order to
298 change transports, a full Asterisk restart is required</para></warning>
301 <configOption name="trust_id_inbound" default="no">
302 <synopsis>Accept identification information received from this endpoint</synopsis>
303 <description><para>This option determines whether Asterisk will accept
304 identification from the endpoint from headers such as P-Asserted-Identity
305 or Remote-Party-ID header. This option applies both to calls originating from the
306 endpoint and calls originating from Asterisk. If <literal>no</literal>, the
307 configured Caller-ID from pjsip.conf will always be used as the identity for
308 the endpoint.</para></description>
310 <configOption name="trust_id_outbound" default="no">
311 <synopsis>Send private identification details to the endpoint.</synopsis>
312 <description><para>This option determines whether res_pjsip will send private
313 identification information to the endpoint. If <literal>no</literal>,
314 private Caller-ID information will not be forwarded to the endpoint.
315 "Private" in this case refers to any method of restricting identification.
316 Example: setting <replaceable>callerid_privacy</replaceable> to any
317 <literal>prohib</literal> variation.
318 Example: If <replaceable>trust_id_inbound</replaceable> is set to
319 <literal>yes</literal>, the presence of a <literal>Privacy: id</literal>
320 header in a SIP request or response would indicate the identification
321 provided in the request is private.</para></description>
323 <configOption name="type">
324 <synopsis>Must be of type 'endpoint'.</synopsis>
326 <configOption name="use_ptime" default="no">
327 <synopsis>Use Endpoint's requested packetisation interval</synopsis>
329 <configOption name="use_avpf" default="no">
330 <synopsis>Determines whether res_pjsip will use and enforce usage of AVPF for this
333 If set to <literal>yes</literal>, res_pjsip will use use the AVPF or SAVPF RTP
334 profile for all media offers on outbound calls and media updates and will
335 decline media offers not using the AVPF or SAVPF profile.
337 If set to <literal>no</literal>, res_pjsip will use use the AVP or SAVP RTP
338 profile for all media offers on outbound calls and media updates and will
339 decline media offers not using the AVP or SAVP profile.
340 </para></description>
342 <configOption name="media_encryption" default="no">
343 <synopsis>Determines whether res_pjsip will use and enforce usage of media encryption
344 for this endpoint.</synopsis>
347 <enum name="no"><para>
348 res_pjsip will offer no encryption and allow no encryption to be setup.
350 <enum name="sdes"><para>
351 res_pjsip will offer standard SRTP setup via in-SDP keys. Encrypted SIP
352 transport should be used in conjunction with this option to prevent
353 exposure of media encryption keys.
355 <enum name="dtls"><para>
356 res_pjsip will offer DTLS-SRTP setup.
361 <configOption name="inband_progress" default="no">
362 <synopsis>Determines whether chan_pjsip will indicate ringing using inband
365 If set to <literal>yes</literal>, chan_pjsip will send a 183 Session Progress
366 when told to indicate ringing and will immediately start sending ringing
369 If set to <literal>no</literal>, chan_pjsip will send a 180 Ringing when told
370 to indicate ringing and will NOT send it as audio.
371 </para></description>
373 <configOption name="callgroup">
374 <synopsis>The numeric pickup groups for a channel.</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="pickupgroup">
381 <synopsis>The numeric pickup groups that a channel can pickup.</synopsis>
383 Can be set to a comma separated list of numbers or ranges between the values
384 of 0-63 (maximum of 64 groups).
385 </para></description>
387 <configOption name="namedcallgroup">
388 <synopsis>The named pickup groups for a channel.</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="namedpickupgroup">
395 <synopsis>The named pickup groups that a channel can pickup.</synopsis>
397 Can be set to a comma separated list of case sensitive strings limited by
398 supported line length.
399 </para></description>
401 <configOption name="devicestate_busy_at" default="0">
402 <synopsis>The number of in-use channels which will cause busy to be returned as device state</synopsis>
404 When the number of in-use channels for the endpoint matches the devicestate_busy_at setting the
405 PJSIP channel driver will return busy as the device state instead of in use.
406 </para></description>
408 <configOption name="t38udptl" default="no">
409 <synopsis>Whether T.38 UDPTL support is enabled or not</synopsis>
411 If set to yes T.38 UDPTL support will be enabled, and T.38 negotiation requests will be accepted
413 </para></description>
415 <configOption name="t38udptl_ec" default="none">
416 <synopsis>T.38 UDPTL error correction method</synopsis>
419 <enum name="none"><para>
420 No error correction should be used.
422 <enum name="fec"><para>
423 Forward error correction should be used.
425 <enum name="redundancy"><para>
426 Redundacy error correction should be used.
431 <configOption name="t38udptl_maxdatagram" default="0">
432 <synopsis>T.38 UDPTL maximum datagram size</synopsis>
434 This option can be set to override the maximum datagram of a remote endpoint for broken
436 </para></description>
438 <configOption name="faxdetect" default="no">
439 <synopsis>Whether CNG tone detection is enabled</synopsis>
441 This option can be set to send the session to the fax extension when a CNG tone is
443 </para></description>
445 <configOption name="t38udptl_nat" default="no">
446 <synopsis>Whether NAT support is enabled on UDPTL sessions</synopsis>
448 When enabled the UDPTL stack will send UDPTL packets to the source address of
450 </para></description>
452 <configOption name="t38udptl_ipv6" default="no">
453 <synopsis>Whether IPv6 is used for UDPTL Sessions</synopsis>
455 When enabled the UDPTL stack will use IPv6.
456 </para></description>
458 <configOption name="tonezone">
459 <synopsis>Set which country's indications to use for channels created for this endpoint.</synopsis>
461 <configOption name="language">
462 <synopsis>Set the default language to use for channels created for this endpoint.</synopsis>
464 <configOption name="one_touch_recording" default="no">
465 <synopsis>Determines whether one-touch recording is allowed for this endpoint.</synopsis>
467 <ref type="configOption">recordonfeature</ref>
468 <ref type="configOption">recordofffeature</ref>
471 <configOption name="recordonfeature" default="automixmon">
472 <synopsis>The feature to enact when one-touch recording is turned on.</synopsis>
474 <para>When an INFO request for one-touch recording arrives with a Record header set to "on", this
475 feature will be enabled for the channel. The feature designated here can be any built-in
476 or dynamic feature defined in features.conf.</para>
477 <note><para>This setting has no effect if the endpoint's one_touch_recording option is disabled</para></note>
480 <ref type="configOption">one_touch_recording</ref>
481 <ref type="configOption">recordofffeature</ref>
484 <configOption name="recordofffeature" default="automixmon">
485 <synopsis>The feature to enact when one-touch recording is turned off.</synopsis>
487 <para>When an INFO request for one-touch recording arrives with a Record header set to "off", this
488 feature will be enabled for the channel. The feature designated here can be any built-in
489 or dynamic feature defined in features.conf.</para>
490 <note><para>This setting has no effect if the endpoint's one_touch_recording option is disabled</para></note>
493 <ref type="configOption">one_touch_recording</ref>
494 <ref type="configOption">recordonfeature</ref>
497 <configOption name="rtpengine" default="asterisk">
498 <synopsis>Name of the RTP engine to use for channels created for this endpoint</synopsis>
500 <configOption name="allowtransfer" default="yes">
501 <synopsis>Determines whether SIP REFER transfers are allowed for this endpoint</synopsis>
503 <configOption name="sdpowner" default="-">
504 <synopsis>String placed as the username portion of an SDP origin (o=) line.</synopsis>
506 <configOption name="sdpsession" default="Asterisk">
507 <synopsis>String used for the SDP session (s=) line.</synopsis>
509 <configOption name="tos_audio">
510 <synopsis>DSCP TOS bits for audio streams</synopsis>
512 See https://wiki.asterisk.org/wiki/display/AST/IP+Quality+of+Service for more information about QoS settings
513 </para></description>
515 <configOption name="tos_video">
516 <synopsis>DSCP TOS bits for video streams</synopsis>
518 See https://wiki.asterisk.org/wiki/display/AST/IP+Quality+of+Service for more information about QoS settings
519 </para></description>
521 <configOption name="cos_audio">
522 <synopsis>Priority for audio streams</synopsis>
524 See https://wiki.asterisk.org/wiki/display/AST/IP+Quality+of+Service for more information about QoS settings
525 </para></description>
527 <configOption name="cos_video">
528 <synopsis>Priority for video streams</synopsis>
530 See https://wiki.asterisk.org/wiki/display/AST/IP+Quality+of+Service for more information about QoS settings
531 </para></description>
533 <configOption name="allowsubscribe" default="yes">
534 <synopsis>Determines if endpoint is allowed to initiate subscriptions with Asterisk.</synopsis>
536 <configOption name="subminexpiry" default="60">
537 <synopsis>The minimum allowed expiry time for subscriptions initiated by the endpoint.</synopsis>
539 <configOption name="fromuser">
540 <synopsis>Username to use in From header for requests to this endpoint.</synopsis>
542 <configOption name="mwifromuser">
543 <synopsis>Username to use in From header for unsolicited MWI NOTIFYs to this endpoint.</synopsis>
545 <configOption name="fromdomain">
546 <synopsis>Domain to user in From header for requests to this endpoint.</synopsis>
548 <configOption name="dtlsverify">
549 <synopsis>Verify that the provided peer certificate is valid</synopsis>
551 This option only applies if <replaceable>media_encryption</replaceable> is
552 set to <literal>dtls</literal>.
553 </para></description>
555 <configOption name="dtlsrekey">
556 <synopsis>Interval at which to renegotiate the TLS session and rekey the SRTP session</synopsis>
558 This option only applies if <replaceable>media_encryption</replaceable> is
559 set to <literal>dtls</literal>.
561 If this is not set or the value provided is 0 rekeying will be disabled.
562 </para></description>
564 <configOption name="dtlscertfile">
565 <synopsis>Path to certificate file to present to peer</synopsis>
567 This option only applies if <replaceable>media_encryption</replaceable> is
568 set to <literal>dtls</literal>.
569 </para></description>
571 <configOption name="dtlsprivatekey">
572 <synopsis>Path to private key for certificate file</synopsis>
574 This option only applies if <replaceable>media_encryption</replaceable> is
575 set to <literal>dtls</literal>.
576 </para></description>
578 <configOption name="dtlscipher">
579 <synopsis>Cipher to use for DTLS negotiation</synopsis>
581 This option only applies if <replaceable>media_encryption</replaceable> is
582 set to <literal>dtls</literal>.
584 Many options for acceptable ciphers. See link for more:
585 http://www.openssl.org/docs/apps/ciphers.html#CIPHER_STRINGS
586 </para></description>
588 <configOption name="dtlscafile">
589 <synopsis>Path to certificate authority certificate</synopsis>
591 This option only applies if <replaceable>media_encryption</replaceable> is
592 set to <literal>dtls</literal>.
593 </para></description>
595 <configOption name="dtlscapath">
596 <synopsis>Path to a directory containing certificate authority certificates</synopsis>
598 This option only applies if <replaceable>media_encryption</replaceable> is
599 set to <literal>dtls</literal>.
600 </para></description>
602 <configOption name="dtlssetup">
603 <synopsis>Whether we are willing to accept connections, connect to the other party, or both.</synopsis>
606 This option only applies if <replaceable>media_encryption</replaceable> is
607 set to <literal>dtls</literal>.
610 <enum name="active"><para>
611 res_pjsip will make a connection to the peer.
613 <enum name="passive"><para>
614 res_pjsip will accept connections from the peer.
616 <enum name="actpass"><para>
617 res_pjsip will offer and accept connections from the peer.
622 <configOption name="srtp_tag_32">
623 <synopsis>Determines whether 32 byte tags should be used instead of 80 byte tags.</synopsis>
625 This option only applies if <replaceable>media_encryption</replaceable> is
626 set to <literal>sdes</literal> or <literal>dtls</literal>.
627 </para></description>
630 <configObject name="auth">
631 <synopsis>Authentication type</synopsis>
633 Authentication objects hold the authentication information for use
634 by other objects such as <literal>endpoints</literal> or <literal>registrations</literal>.
635 This also allows for multiple objects to use a single auth object. See
636 the <literal>auth_type</literal> config option for password style choices.
637 </para></description>
638 <configOption name="auth_type" default="userpass">
639 <synopsis>Authentication type</synopsis>
641 This option specifies which of the password style config options should be read
642 when trying to authenticate an endpoint inbound request. If set to <literal>userpass</literal>
643 then we'll read from the 'password' option. For <literal>md5</literal> we'll read
648 <enum name="userpass"/>
652 <configOption name="nonce_lifetime" default="32">
653 <synopsis>Lifetime of a nonce associated with this authentication config.</synopsis>
655 <configOption name="md5_cred">
656 <synopsis>MD5 Hash used for authentication.</synopsis>
657 <description><para>Only used when auth_type is <literal>md5</literal>.</para></description>
659 <configOption name="password">
660 <synopsis>PlainText password used for authentication.</synopsis>
661 <description><para>Only used when auth_type is <literal>userpass</literal>.</para></description>
663 <configOption name="realm" default="asterisk">
664 <synopsis>SIP realm for endpoint</synopsis>
666 <configOption name="type">
667 <synopsis>Must be 'auth'</synopsis>
669 <configOption name="username">
670 <synopsis>Username to use for account</synopsis>
673 <configObject name="nat_hook">
674 <synopsis>XXX This exists only to prevent XML documentation errors.</synopsis>
675 <configOption name="external_media_address">
676 <synopsis>I should be undocumented or hidden</synopsis>
678 <configOption name="method">
679 <synopsis>I should be undocumented or hidden</synopsis>
682 <configObject name="domain_alias">
683 <synopsis>Domain Alias</synopsis>
685 Signifies that a domain is an alias. If the domain on a session is
686 not found to match an AoR then this object is used to see if we have
687 an alias for the AoR to which the endpoint is binding. This objects
688 name as defined in configuration should be the domain alias and a
689 config option is provided to specify the domain to be aliased.
690 </para></description>
691 <configOption name="type">
692 <synopsis>Must be of type 'domain_alias'.</synopsis>
694 <configOption name="domain">
695 <synopsis>Domain to be aliased</synopsis>
698 <configObject name="transport">
699 <synopsis>SIP Transport</synopsis>
701 <emphasis>Transports</emphasis>
703 <para>There are different transports and protocol derivatives
704 supported by <literal>res_pjsip</literal>. They are in order of
705 preference: UDP, TCP, and WebSocket (WS).</para>
706 <note><para>Changes to transport configuration in pjsip.conf will only be
707 effected on a complete restart of Asterisk. A module reload
708 will not suffice.</para></note>
710 <configOption name="async_operations" default="1">
711 <synopsis>Number of simultaneous Asynchronous Operations</synopsis>
713 <configOption name="bind">
714 <synopsis>IP Address and optional port to bind to for this transport</synopsis>
716 <configOption name="ca_list_file">
717 <synopsis>File containing a list of certificates to read (TLS ONLY)</synopsis>
719 <configOption name="cert_file">
720 <synopsis>Certificate file for endpoint (TLS ONLY)</synopsis>
722 <configOption name="cipher">
723 <synopsis>Preferred Cryptography Cipher (TLS ONLY)</synopsis>
725 Many options for acceptable ciphers see link for more:
726 http://www.openssl.org/docs/apps/ciphers.html#CIPHER_STRINGS
727 </para></description>
729 <configOption name="domain">
730 <synopsis>Domain the transport comes from</synopsis>
732 <configOption name="external_media_address">
733 <synopsis>External Address to use in RTP handling</synopsis>
735 <configOption name="external_signaling_address">
736 <synopsis>External address for SIP signalling</synopsis>
738 <configOption name="external_signaling_port" default="0">
739 <synopsis>External port for SIP signalling</synopsis>
741 <configOption name="method">
742 <synopsis>Method of SSL transport (TLS ONLY)</synopsis>
745 <enum name="default" />
746 <enum name="unspecified" />
747 <enum name="tlsv1" />
748 <enum name="sslv2" />
749 <enum name="sslv3" />
750 <enum name="sslv23" />
754 <configOption name="localnet">
755 <synopsis>Network to consider local (used for NAT purposes).</synopsis>
756 <description><para>This must be in CIDR or dotted decimal format with the IP
757 and mask separated with a slash ('/').</para></description>
759 <configOption name="password">
760 <synopsis>Password required for transport</synopsis>
762 <configOption name="privkey_file">
763 <synopsis>Private key file (TLS ONLY)</synopsis>
765 <configOption name="protocol" default="udp">
766 <synopsis>Protocol to use for SIP traffic</synopsis>
775 <configOption name="require_client_cert" default="false">
776 <synopsis>Require client certificate (TLS ONLY)</synopsis>
778 <configOption name="type">
779 <synopsis>Must be of type 'transport'.</synopsis>
781 <configOption name="verify_client" default="false">
782 <synopsis>Require verification of client certificate (TLS ONLY)</synopsis>
784 <configOption name="verify_server" default="false">
785 <synopsis>Require verification of server certificate (TLS ONLY)</synopsis>
788 <configObject name="contact">
789 <synopsis>A way of creating an aliased name to a SIP URI</synopsis>
791 Contacts are a way to hide SIP URIs from the dialplan directly.
792 They are also used to make a group of contactable parties when
793 in use with <literal>AoR</literal> lists.
794 </para></description>
795 <configOption name="type">
796 <synopsis>Must be of type 'contact'.</synopsis>
798 <configOption name="uri">
799 <synopsis>SIP URI to contact peer</synopsis>
801 <configOption name="expiration_time">
802 <synopsis>Time to keep alive a contact</synopsis>
804 Time to keep alive a contact. String style specification.
805 </para></description>
807 <configOption name="qualify_frequency" default="0">
808 <synopsis>Interval at which to qualify a contact</synopsis>
810 Interval between attempts to qualify the contact for reachability.
811 If <literal>0</literal> never qualify. Time in seconds.
812 </para></description>
815 <configObject name="contact_status">
816 <synopsis>Status for a contact</synopsis>
818 The contact status keeps track of whether or not a contact is reachable
819 and how long it took to qualify the contact (round trip time).
820 </para></description>
821 <configOption name="status">
822 <synopsis>A contact's status</synopsis>
825 <enum name="AVAILABLE" />
826 <enum name="UNAVAILABLE" />
830 <configOption name="rtt">
831 <synopsis>Round trip time</synopsis>
833 The time, in microseconds, it took to qualify the contact.
834 </para></description>
837 <configObject name="aor">
838 <synopsis>The configuration for a location of an endpoint</synopsis>
840 An AoR is what allows Asterisk to contact an endpoint via res_pjsip. If no
841 AoRs are specified, an endpoint will not be reachable by Asterisk.
842 Beyond that, an AoR has other uses within Asterisk, such as inbound
845 An <literal>AoR</literal> is a way to allow dialing a group
846 of <literal>Contacts</literal> that all use the same
847 <literal>endpoint</literal> for calls.
849 This can be used as another way of grouping a list of contacts to dial
850 rather than specifing them each directly when dialing via the dialplan.
851 This must be used in conjuction with the <literal>PJSIP_DIAL_CONTACTS</literal>.
853 Registrations: For Asterisk to match an inbound registration to an endpoint,
854 the AoR object name must match the user portion of the SIP URI in the "To:"
855 header of the inbound SIP registration. That will usually be equivalent
856 to the "user name" set in your hard or soft phones configuration.
857 </para></description>
858 <configOption name="contact">
859 <synopsis>Permanent contacts assigned to AoR</synopsis>
861 Contacts specified will be called whenever referenced
862 by <literal>chan_pjsip</literal>.
864 Use a separate "contact=" entry for each contact required. Contacts
865 are specified using a SIP URI.
866 </para></description>
868 <configOption name="default_expiration" default="3600">
869 <synopsis>Default expiration time in seconds for contacts that are dynamically bound to an AoR.</synopsis>
871 <configOption name="mailboxes">
872 <synopsis>Mailbox(es) to be associated with</synopsis>
873 <description><para>This option applies when an external entity subscribes to an AoR
874 for message waiting indications. The mailboxes specified will be subscribed to.
875 More than one mailbox can be specified with a comma-delimited string.</para></description>
877 <configOption name="maximum_expiration" default="7200">
878 <synopsis>Maximum time to keep an AoR</synopsis>
880 Maximium time to keep a peer with explicit expiration. Time in seconds.
881 </para></description>
883 <configOption name="max_contacts" default="0">
884 <synopsis>Maximum number of contacts that can bind to an AoR</synopsis>
886 Maximum number of contacts that can associate with this AoR. This value does
887 not affect the number of contacts that can be added with the "contact" option.
888 It only limits contacts added through external interaction, such as
891 <note><para>This should be set to <literal>1</literal> and
892 <replaceable>remove_existing</replaceable> set to <literal>yes</literal> if you
893 wish to stick with the older <literal>chan_sip</literal> behaviour.
897 <configOption name="minimum_expiration" default="60">
898 <synopsis>Minimum keep alive time for an AoR</synopsis>
900 Minimum time to keep a peer with an explict expiration. Time in seconds.
901 </para></description>
903 <configOption name="remove_existing" default="no">
904 <synopsis>Determines whether new contacts replace existing ones.</synopsis>
906 On receiving a new registration to the AoR should it remove
907 the existing contact that was registered against it?
909 <note><para>This should be set to <literal>yes</literal> and
910 <replaceable>max_contacts</replaceable> set to <literal>1</literal> if you
911 wish to stick with the older <literal>chan_sip</literal> behaviour.
915 <configOption name="type">
916 <synopsis>Must be of type 'aor'.</synopsis>
918 <configOption name="qualify_frequency" default="0">
919 <synopsis>Interval at which to qualify an AoR</synopsis>
921 Interval between attempts to qualify the AoR for reachability.
922 If <literal>0</literal> never qualify. Time in seconds.
923 </para></description>
925 <configOption name="authenticate_qualify" default="no">
926 <synopsis>Authenticates a qualify request if needed</synopsis>
928 If true and a qualify request receives a challenge or authenticate response
929 authentication is attempted before declaring the contact available.
930 </para></description>
933 <configObject name="system">
934 <synopsis>Options that apply to the SIP stack as well as other system-wide settings</synopsis>
936 The settings in this section are global. In addition to being global, the values will
937 not be re-evaluated when a reload is performed. This is because the values must be set
938 before the SIP stack is initialized. The only way to reset these values is to either
939 restart Asterisk, or unload res_pjsip.so and then load it again.
940 </para></description>
941 <configOption name="timert1" default="500">
942 <synopsis>Set transaction timer T1 value (milliseconds).</synopsis>
944 Timer T1 is the base for determining how long to wait before retransmitting
945 requests that receive no response when using an unreliable transport (e.g. UDP).
946 For more information on this timer, see RFC 3261, Section 17.1.1.1.
947 </para></description>
949 <configOption name="timerb" default="32000">
950 <synopsis>Set transaction timer B value (milliseconds).</synopsis>
952 Timer B determines the maximum amount of time to wait after sending an INVITE
953 request before terminating the transaction. It is recommended that this be set
954 to 64 * Timer T1, but it may be set higher if desired. For more information on
955 this timer, see RFC 3261, Section 17.1.1.1.
956 </para></description>
958 <configOption name="compactheaders" default="no">
959 <synopsis>Use the short forms of common SIP header names.</synopsis>
961 <configOption name="threadpool_initial_size" default="0">
962 <synopsis>Initial number of threads in the res_pjsip threadpool.</synopsis>
964 <configOption name="threadpool_auto_increment" default="5">
965 <synopsis>The amount by which the number of threads is incremented when necessary.</synopsis>
967 <configOption name="threadpool_idle_timeout" default="60">
968 <synopsis>Number of seconds before an idle thread should be disposed of.</synopsis>
970 <configOption name="threadpool_max_size" default="0">
971 <synopsis>Maximum number of threads in the res_pjsip threadpool.
972 A value of 0 indicates no maximum.</synopsis>
974 <configOption name="type">
975 <synopsis>Must be of type 'system'.</synopsis>
978 <configObject name="global">
979 <synopsis>Options that apply globally to all SIP communications</synopsis>
981 The settings in this section are global. Unlike options in the <literal>system</literal>
982 section, these options can be refreshed by performing a reload.
983 </para></description>
984 <configOption name="maxforwards" default="70">
985 <synopsis>Value used in Max-Forwards header for SIP requests.</synopsis>
987 <configOption name="useragent" default="Asterisk <Asterisk Version>">
988 <synopsis>Value used in User-Agent header for SIP requests and Server header for SIP responses.</synopsis>
990 <configOption name="type">
991 <synopsis>Must be of type 'global'.</synopsis>
996 <manager name="PJSIPQualify" language="en_US">
998 Qualify a chan_pjsip endpoint.
1001 <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
1002 <parameter name="Endpoint" required="true">
1003 <para>The endpoint you want to qualify.</para>
1007 <para>Qualify a chan_pjsip endpoint.</para>
1013 static pjsip_endpoint *ast_pjsip_endpoint;
1015 static struct ast_threadpool *sip_threadpool;
1017 static int register_service(void *data)
1019 pjsip_module **module = data;
1020 if (!ast_pjsip_endpoint) {
1021 ast_log(LOG_ERROR, "There is no PJSIP endpoint. Unable to register services\n");
1024 if (pjsip_endpt_register_module(ast_pjsip_endpoint, *module) != PJ_SUCCESS) {
1025 ast_log(LOG_ERROR, "Unable to register module %.*s\n", (int) pj_strlen(&(*module)->name), pj_strbuf(&(*module)->name));
1028 ast_debug(1, "Registered SIP service %.*s (%p)\n", (int) pj_strlen(&(*module)->name), pj_strbuf(&(*module)->name), *module);
1029 ast_module_ref(ast_module_info->self);
1033 int ast_sip_register_service(pjsip_module *module)
1035 return ast_sip_push_task_synchronous(NULL, register_service, &module);
1038 static int unregister_service(void *data)
1040 pjsip_module **module = data;
1041 ast_module_unref(ast_module_info->self);
1042 if (!ast_pjsip_endpoint) {
1045 pjsip_endpt_unregister_module(ast_pjsip_endpoint, *module);
1046 ast_debug(1, "Unregistered SIP service %.*s\n", (int) pj_strlen(&(*module)->name), pj_strbuf(&(*module)->name));
1050 void ast_sip_unregister_service(pjsip_module *module)
1052 ast_sip_push_task_synchronous(NULL, unregister_service, &module);
1055 static struct ast_sip_authenticator *registered_authenticator;
1057 int ast_sip_register_authenticator(struct ast_sip_authenticator *auth)
1059 if (registered_authenticator) {
1060 ast_log(LOG_WARNING, "Authenticator %p is already registered. Cannot register a new one\n", registered_authenticator);
1063 registered_authenticator = auth;
1064 ast_debug(1, "Registered SIP authenticator module %p\n", auth);
1065 ast_module_ref(ast_module_info->self);
1069 void ast_sip_unregister_authenticator(struct ast_sip_authenticator *auth)
1071 if (registered_authenticator != auth) {
1072 ast_log(LOG_WARNING, "Trying to unregister authenticator %p but authenticator %p registered\n",
1073 auth, registered_authenticator);
1076 registered_authenticator = NULL;
1077 ast_debug(1, "Unregistered SIP authenticator %p\n", auth);
1078 ast_module_unref(ast_module_info->self);
1081 int ast_sip_requires_authentication(struct ast_sip_endpoint *endpoint, pjsip_rx_data *rdata)
1083 if (!registered_authenticator) {
1084 ast_log(LOG_WARNING, "No SIP authenticator registered. Assuming authentication is not required\n");
1088 return registered_authenticator->requires_authentication(endpoint, rdata);
1091 enum ast_sip_check_auth_result ast_sip_check_authentication(struct ast_sip_endpoint *endpoint,
1092 pjsip_rx_data *rdata, pjsip_tx_data *tdata)
1094 if (!registered_authenticator) {
1095 ast_log(LOG_WARNING, "No SIP authenticator registered. Assuming authentication is successful\n");
1098 return registered_authenticator->check_authentication(endpoint, rdata, tdata);
1101 static struct ast_sip_outbound_authenticator *registered_outbound_authenticator;
1103 int ast_sip_register_outbound_authenticator(struct ast_sip_outbound_authenticator *auth)
1105 if (registered_outbound_authenticator) {
1106 ast_log(LOG_WARNING, "Outbound authenticator %p is already registered. Cannot register a new one\n", registered_outbound_authenticator);
1109 registered_outbound_authenticator = auth;
1110 ast_debug(1, "Registered SIP outbound authenticator module %p\n", auth);
1111 ast_module_ref(ast_module_info->self);
1115 void ast_sip_unregister_outbound_authenticator(struct ast_sip_outbound_authenticator *auth)
1117 if (registered_outbound_authenticator != auth) {
1118 ast_log(LOG_WARNING, "Trying to unregister outbound authenticator %p but outbound authenticator %p registered\n",
1119 auth, registered_outbound_authenticator);
1122 registered_outbound_authenticator = NULL;
1123 ast_debug(1, "Unregistered SIP outbound authenticator %p\n", auth);
1124 ast_module_unref(ast_module_info->self);
1127 int ast_sip_create_request_with_auth(const struct ast_sip_auth_array *auths, pjsip_rx_data *challenge,
1128 pjsip_transaction *tsx, pjsip_tx_data **new_request)
1130 if (!registered_outbound_authenticator) {
1131 ast_log(LOG_WARNING, "No SIP outbound authenticator registered. Cannot respond to authentication challenge\n");
1134 return registered_outbound_authenticator->create_request_with_auth(auths, challenge, tsx, new_request);
1137 struct endpoint_identifier_list {
1138 struct ast_sip_endpoint_identifier *identifier;
1139 AST_RWLIST_ENTRY(endpoint_identifier_list) list;
1142 static AST_RWLIST_HEAD_STATIC(endpoint_identifiers, endpoint_identifier_list);
1144 int ast_sip_register_endpoint_identifier(struct ast_sip_endpoint_identifier *identifier)
1146 struct endpoint_identifier_list *id_list_item;
1147 SCOPED_LOCK(lock, &endpoint_identifiers, AST_RWLIST_WRLOCK, AST_RWLIST_UNLOCK);
1149 id_list_item = ast_calloc(1, sizeof(*id_list_item));
1150 if (!id_list_item) {
1151 ast_log(LOG_ERROR, "Unabled to add endpoint identifier. Out of memory.\n");
1154 id_list_item->identifier = identifier;
1156 AST_RWLIST_INSERT_TAIL(&endpoint_identifiers, id_list_item, list);
1157 ast_debug(1, "Registered endpoint identifier %p\n", identifier);
1159 ast_module_ref(ast_module_info->self);
1163 void ast_sip_unregister_endpoint_identifier(struct ast_sip_endpoint_identifier *identifier)
1165 struct endpoint_identifier_list *iter;
1166 SCOPED_LOCK(lock, &endpoint_identifiers, AST_RWLIST_WRLOCK, AST_RWLIST_UNLOCK);
1167 AST_RWLIST_TRAVERSE_SAFE_BEGIN(&endpoint_identifiers, iter, list) {
1168 if (iter->identifier == identifier) {
1169 AST_RWLIST_REMOVE_CURRENT(list);
1171 ast_debug(1, "Unregistered endpoint identifier %p\n", identifier);
1172 ast_module_unref(ast_module_info->self);
1176 AST_RWLIST_TRAVERSE_SAFE_END;
1179 struct ast_sip_endpoint *ast_sip_identify_endpoint(pjsip_rx_data *rdata)
1181 struct endpoint_identifier_list *iter;
1182 struct ast_sip_endpoint *endpoint = NULL;
1183 SCOPED_LOCK(lock, &endpoint_identifiers, AST_RWLIST_RDLOCK, AST_RWLIST_UNLOCK);
1184 AST_RWLIST_TRAVERSE(&endpoint_identifiers, iter, list) {
1185 ast_assert(iter->identifier->identify_endpoint != NULL);
1186 endpoint = iter->identifier->identify_endpoint(rdata);
1194 pjsip_endpoint *ast_sip_get_pjsip_endpoint(void)
1196 return ast_pjsip_endpoint;
1199 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)
1201 pj_str_t tmp, local_addr;
1203 pjsip_sip_uri *sip_uri;
1204 pjsip_transport_type_e type = PJSIP_TRANSPORT_UNSPECIFIED;
1206 char uuid_str[AST_UUID_STR_LEN];
1208 if (ast_strlen_zero(user)) {
1209 RAII_VAR(struct ast_uuid *, uuid, ast_uuid_generate(), ast_free_ptr);
1213 user = ast_uuid_to_str(uuid, uuid_str, sizeof(uuid_str));
1216 /* Parse the provided target URI so we can determine what transport it will end up using */
1217 pj_strdup_with_null(pool, &tmp, target);
1219 if (!(uri = pjsip_parse_uri(pool, tmp.ptr, tmp.slen, 0)) ||
1220 (!PJSIP_URI_SCHEME_IS_SIP(uri) && !PJSIP_URI_SCHEME_IS_SIPS(uri))) {
1224 sip_uri = pjsip_uri_get_uri(uri);
1226 /* Determine the transport type to use */
1227 if (PJSIP_URI_SCHEME_IS_SIPS(sip_uri)) {
1228 type = PJSIP_TRANSPORT_TLS;
1229 } else if (!sip_uri->transport_param.slen) {
1230 type = PJSIP_TRANSPORT_UDP;
1232 type = pjsip_transport_get_type_from_name(&sip_uri->transport_param);
1235 if (type == PJSIP_TRANSPORT_UNSPECIFIED) {
1239 /* If the host is IPv6 turn the transport into an IPv6 version */
1240 if (pj_strchr(&sip_uri->host, ':') && type < PJSIP_TRANSPORT_START_OTHER) {
1241 type = (pjsip_transport_type_e)(((int)type) + PJSIP_TRANSPORT_IPV6);
1244 if (!ast_strlen_zero(domain)) {
1245 from->ptr = pj_pool_alloc(pool, PJSIP_MAX_URL_SIZE);
1246 from->slen = pj_ansi_snprintf(from->ptr, PJSIP_MAX_URL_SIZE,
1248 (pjsip_transport_get_flag_from_type(type) & PJSIP_TRANSPORT_SECURE) ? "sips" : "sip",
1251 (type != PJSIP_TRANSPORT_UDP && type != PJSIP_TRANSPORT_UDP6) ? ";transport=" : "",
1252 (type != PJSIP_TRANSPORT_UDP && type != PJSIP_TRANSPORT_UDP6) ? pjsip_transport_get_type_name(type) : "");
1256 /* Get the local bound address for the transport that will be used when communicating with the provided URI */
1257 if (pjsip_tpmgr_find_local_addr(pjsip_endpt_get_tpmgr(ast_sip_get_pjsip_endpoint()), pool, type, selector,
1258 &local_addr, &local_port) != PJ_SUCCESS) {
1262 /* If IPv6 was specified in the transport, set the proper type */
1263 if (pj_strchr(&local_addr, ':') && type < PJSIP_TRANSPORT_START_OTHER) {
1264 type = (pjsip_transport_type_e)(((int)type) + PJSIP_TRANSPORT_IPV6);
1267 from->ptr = pj_pool_alloc(pool, PJSIP_MAX_URL_SIZE);
1268 from->slen = pj_ansi_snprintf(from->ptr, PJSIP_MAX_URL_SIZE,
1269 "<%s:%s@%s%.*s%s:%d%s%s>",
1270 (pjsip_transport_get_flag_from_type(type) & PJSIP_TRANSPORT_SECURE) ? "sips" : "sip",
1272 (type & PJSIP_TRANSPORT_IPV6) ? "[" : "",
1273 (int)local_addr.slen,
1275 (type & PJSIP_TRANSPORT_IPV6) ? "]" : "",
1277 (type != PJSIP_TRANSPORT_UDP && type != PJSIP_TRANSPORT_UDP6) ? ";transport=" : "",
1278 (type != PJSIP_TRANSPORT_UDP && type != PJSIP_TRANSPORT_UDP6) ? pjsip_transport_get_type_name(type) : "");
1283 static int sip_get_tpselector_from_endpoint(const struct ast_sip_endpoint *endpoint, pjsip_tpselector *selector)
1285 RAII_VAR(struct ast_sip_transport *, transport, NULL, ao2_cleanup);
1286 const char *transport_name = endpoint->transport;
1288 if (ast_strlen_zero(transport_name)) {
1292 transport = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "transport", transport_name);
1294 if (!transport || !transport->state) {
1298 if (transport->state->transport) {
1299 selector->type = PJSIP_TPSELECTOR_TRANSPORT;
1300 selector->u.transport = transport->state->transport;
1301 } else if (transport->state->factory) {
1302 selector->type = PJSIP_TPSELECTOR_LISTENER;
1303 selector->u.listener = transport->state->factory;
1311 static int sip_get_tpselector_from_uri(const char *uri, pjsip_tpselector *selector)
1313 RAII_VAR(struct ast_sip_contact_transport *, contact_transport, NULL, ao2_cleanup);
1315 contact_transport = ast_sip_location_retrieve_contact_transport_by_uri(uri);
1317 if (!contact_transport) {
1321 selector->type = PJSIP_TPSELECTOR_TRANSPORT;
1322 selector->u.transport = contact_transport->transport;
1327 pjsip_dialog *ast_sip_create_dialog(const struct ast_sip_endpoint *endpoint, const char *uri, const char *request_user)
1329 pj_str_t local_uri = { "sip:temp@temp", 13 }, remote_uri;
1330 pjsip_dialog *dlg = NULL;
1331 const char *outbound_proxy = endpoint->outbound_proxy;
1332 pjsip_tpselector selector = { .type = PJSIP_TPSELECTOR_NONE, };
1333 static const pj_str_t HCONTACT = { "Contact", 7 };
1335 pj_cstr(&remote_uri, uri);
1337 if (pjsip_dlg_create_uac(pjsip_ua_instance(), &local_uri, NULL, &remote_uri, NULL, &dlg) != PJ_SUCCESS) {
1341 if (sip_get_tpselector_from_uri(uri, &selector) && sip_get_tpselector_from_endpoint(endpoint, &selector)) {
1342 pjsip_dlg_terminate(dlg);
1346 if (sip_dialog_create_from(dlg->pool, &local_uri, endpoint->fromuser, endpoint->fromdomain, &remote_uri, &selector)) {
1347 pjsip_dlg_terminate(dlg);
1351 /* Update the dialog with the new local URI, we do it afterwards so we can use the dialog pool for construction */
1352 pj_strdup_with_null(dlg->pool, &dlg->local.info_str, &local_uri);
1353 dlg->local.info->uri = pjsip_parse_uri(dlg->pool, dlg->local.info_str.ptr, dlg->local.info_str.slen, 0);
1354 dlg->local.contact = pjsip_parse_hdr(dlg->pool, &HCONTACT, local_uri.ptr, local_uri.slen, NULL);
1356 /* If a request user has been specified and we are permitted to change it, do so */
1357 if (!ast_strlen_zero(request_user) && (PJSIP_URI_SCHEME_IS_SIP(dlg->target) || PJSIP_URI_SCHEME_IS_SIPS(dlg->target))) {
1358 pjsip_sip_uri *target = pjsip_uri_get_uri(dlg->target);
1359 pj_strdup2(dlg->pool, &target->user, request_user);
1362 /* We have to temporarily bump up the sess_count here so the dialog is not prematurely destroyed */
1365 pjsip_dlg_set_transport(dlg, &selector);
1367 if (!ast_strlen_zero(outbound_proxy)) {
1368 pjsip_route_hdr route_set, *route;
1369 static const pj_str_t ROUTE_HNAME = { "Route", 5 };
1372 pj_list_init(&route_set);
1374 pj_strdup2_with_null(dlg->pool, &tmp, outbound_proxy);
1375 if (!(route = pjsip_parse_hdr(dlg->pool, &ROUTE_HNAME, tmp.ptr, tmp.slen, NULL))) {
1376 pjsip_dlg_terminate(dlg);
1379 pj_list_push_back(&route_set, route);
1381 pjsip_dlg_set_route_set(dlg, &route_set);
1389 /* PJSIP doesn't know about the INFO method, so we have to define it ourselves */
1390 const pjsip_method pjsip_info_method = {PJSIP_OTHER_METHOD, {"INFO", 4} };
1391 const pjsip_method pjsip_message_method = {PJSIP_OTHER_METHOD, {"MESSAGE", 7} };
1395 const pjsip_method *pmethod;
1397 { "INVITE", &pjsip_invite_method },
1398 { "CANCEL", &pjsip_cancel_method },
1399 { "ACK", &pjsip_ack_method },
1400 { "BYE", &pjsip_bye_method },
1401 { "REGISTER", &pjsip_register_method },
1402 { "OPTIONS", &pjsip_options_method },
1403 { "SUBSCRIBE", &pjsip_subscribe_method },
1404 { "NOTIFY", &pjsip_notify_method },
1405 { "PUBLISH", &pjsip_publish_method },
1406 { "INFO", &pjsip_info_method },
1407 { "MESSAGE", &pjsip_message_method },
1410 static const pjsip_method *get_pjsip_method(const char *method)
1413 for (i = 0; i < ARRAY_LEN(methods); ++i) {
1414 if (!strcmp(method, methods[i].method)) {
1415 return methods[i].pmethod;
1421 static int create_in_dialog_request(const pjsip_method *method, struct pjsip_dialog *dlg, pjsip_tx_data **tdata)
1423 if (pjsip_dlg_create_request(dlg, method, -1, tdata) != PJ_SUCCESS) {
1424 ast_log(LOG_WARNING, "Unable to create in-dialog request.\n");
1431 static int create_out_of_dialog_request(const pjsip_method *method, struct ast_sip_endpoint *endpoint,
1432 const char *uri, pjsip_tx_data **tdata)
1434 RAII_VAR(struct ast_sip_contact *, contact, NULL, ao2_cleanup);
1435 pj_str_t remote_uri;
1438 pjsip_tpselector selector = { .type = PJSIP_TPSELECTOR_NONE, };
1440 if (ast_strlen_zero(uri)) {
1442 ast_log(LOG_ERROR, "An endpoint and/or uri must be specified\n");
1446 contact = ast_sip_location_retrieve_contact_from_aor_list(endpoint->aors);
1447 if (!contact || ast_strlen_zero(contact->uri)) {
1448 ast_log(LOG_ERROR, "Unable to retrieve contact for endpoint %s\n",
1449 ast_sorcery_object_get_id(endpoint));
1453 pj_cstr(&remote_uri, contact->uri);
1455 pj_cstr(&remote_uri, uri);
1459 if (sip_get_tpselector_from_endpoint(endpoint, &selector)) {
1460 ast_log(LOG_ERROR, "Unable to retrieve PJSIP transport selector for endpoint %s\n",
1461 ast_sorcery_object_get_id(endpoint));
1466 pool = pjsip_endpt_create_pool(ast_sip_get_pjsip_endpoint(), "Outbound request", 256, 256);
1469 ast_log(LOG_ERROR, "Unable to create PJLIB memory pool\n");
1473 if (sip_dialog_create_from(pool, &from, endpoint ? endpoint->fromuser : NULL,
1474 endpoint ? endpoint->fromdomain : NULL, &remote_uri, &selector)) {
1475 ast_log(LOG_ERROR, "Unable to create From header for %.*s request to endpoint %s\n",
1476 (int) pj_strlen(&method->name), pj_strbuf(&method->name), ast_sorcery_object_get_id(endpoint));
1477 pjsip_endpt_release_pool(ast_sip_get_pjsip_endpoint(), pool);
1481 if (pjsip_endpt_create_request(ast_sip_get_pjsip_endpoint(), method, &remote_uri,
1482 &from, &remote_uri, &from, NULL, -1, NULL, tdata) != PJ_SUCCESS) {
1483 ast_log(LOG_ERROR, "Unable to create outbound %.*s request to endpoint %s\n",
1484 (int) pj_strlen(&method->name), pj_strbuf(&method->name), ast_sorcery_object_get_id(endpoint));
1485 pjsip_endpt_release_pool(ast_sip_get_pjsip_endpoint(), pool);
1489 /* We can release this pool since request creation copied all the necessary
1490 * data into the outbound request's pool
1492 pjsip_endpt_release_pool(ast_sip_get_pjsip_endpoint(), pool);
1496 int ast_sip_create_request(const char *method, struct pjsip_dialog *dlg,
1497 struct ast_sip_endpoint *endpoint, const char *uri,
1498 pjsip_tx_data **tdata)
1500 const pjsip_method *pmethod = get_pjsip_method(method);
1503 ast_log(LOG_WARNING, "Unknown method '%s'. Cannot send request\n", method);
1508 return create_in_dialog_request(pmethod, dlg, tdata);
1510 return create_out_of_dialog_request(pmethod, endpoint, uri, tdata);
1514 static int send_in_dialog_request(pjsip_tx_data *tdata, struct pjsip_dialog *dlg)
1516 if (pjsip_dlg_send_request(dlg, tdata, -1, NULL) != PJ_SUCCESS) {
1517 ast_log(LOG_WARNING, "Unable to send in-dialog request.\n");
1523 static void send_request_cb(void *token, pjsip_event *e)
1525 RAII_VAR(struct ast_sip_endpoint *, endpoint, token, ao2_cleanup);
1526 pjsip_transaction *tsx = e->body.tsx_state.tsx;
1527 pjsip_rx_data *challenge = e->body.tsx_state.src.rdata;
1528 pjsip_tx_data *tdata;
1530 if (tsx->status_code != 401 && tsx->status_code != 407) {
1534 if (!ast_sip_create_request_with_auth(&endpoint->outbound_auths, challenge, tsx, &tdata)) {
1535 pjsip_endpt_send_request(ast_sip_get_pjsip_endpoint(), tdata, -1, NULL, NULL);
1539 static int send_out_of_dialog_request(pjsip_tx_data *tdata, struct ast_sip_endpoint *endpoint)
1541 ao2_ref(endpoint, +1);
1542 if (pjsip_endpt_send_request(ast_sip_get_pjsip_endpoint(), tdata, -1, endpoint, send_request_cb) != PJ_SUCCESS) {
1543 ast_log(LOG_ERROR, "Error attempting to send outbound %.*s request to endpoint %s\n",
1544 (int) pj_strlen(&tdata->msg->line.req.method.name),
1545 pj_strbuf(&tdata->msg->line.req.method.name),
1546 ast_sorcery_object_get_id(endpoint));
1547 ao2_ref(endpoint, -1);
1554 int ast_sip_send_request(pjsip_tx_data *tdata, struct pjsip_dialog *dlg, struct ast_sip_endpoint *endpoint)
1556 ast_assert(tdata->msg->type == PJSIP_REQUEST_MSG);
1559 return send_in_dialog_request(tdata, dlg);
1561 return send_out_of_dialog_request(tdata, endpoint);
1565 int ast_sip_add_header(pjsip_tx_data *tdata, const char *name, const char *value)
1569 pjsip_generic_string_hdr *hdr;
1571 pj_cstr(&hdr_name, name);
1572 pj_cstr(&hdr_value, value);
1574 hdr = pjsip_generic_string_hdr_create(tdata->pool, &hdr_name, &hdr_value);
1576 pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr *) hdr);
1580 static pjsip_msg_body *ast_body_to_pjsip_body(pj_pool_t *pool, const struct ast_sip_body *body)
1586 pj_cstr(&type, body->type);
1587 pj_cstr(&subtype, body->subtype);
1588 pj_cstr(&body_text, body->body_text);
1590 return pjsip_msg_body_create(pool, &type, &subtype, &body_text);
1593 int ast_sip_add_body(pjsip_tx_data *tdata, const struct ast_sip_body *body)
1595 pjsip_msg_body *pjsip_body = ast_body_to_pjsip_body(tdata->pool, body);
1596 tdata->msg->body = pjsip_body;
1600 int ast_sip_add_body_multipart(pjsip_tx_data *tdata, const struct ast_sip_body *bodies[], int num_bodies)
1603 /* NULL for type and subtype automatically creates "multipart/mixed" */
1604 pjsip_msg_body *body = pjsip_multipart_create(tdata->pool, NULL, NULL);
1606 for (i = 0; i < num_bodies; ++i) {
1607 pjsip_multipart_part *part = pjsip_multipart_create_part(tdata->pool);
1608 part->body = ast_body_to_pjsip_body(tdata->pool, bodies[i]);
1609 pjsip_multipart_add_part(tdata->pool, body, part);
1612 tdata->msg->body = body;
1616 int ast_sip_append_body(pjsip_tx_data *tdata, const char *body_text)
1618 size_t combined_size = strlen(body_text) + tdata->msg->body->len;
1619 struct ast_str *body_buffer = ast_str_alloca(combined_size);
1621 ast_str_set(&body_buffer, 0, "%.*s%s", (int) tdata->msg->body->len, (char *) tdata->msg->body->data, body_text);
1623 tdata->msg->body->data = pj_pool_alloc(tdata->pool, combined_size);
1624 pj_memcpy(tdata->msg->body->data, ast_str_buffer(body_buffer), combined_size);
1625 tdata->msg->body->len = combined_size;
1630 struct ast_taskprocessor *ast_sip_create_serializer(void)
1632 struct ast_taskprocessor *serializer;
1633 RAII_VAR(struct ast_uuid *, uuid, ast_uuid_generate(), ast_free_ptr);
1634 char name[AST_UUID_STR_LEN];
1640 ast_uuid_to_str(uuid, name, sizeof(name));
1642 serializer = ast_threadpool_serializer(name, sip_threadpool);
1649 int ast_sip_push_task(struct ast_taskprocessor *serializer, int (*sip_task)(void *), void *task_data)
1652 return ast_taskprocessor_push(serializer, sip_task, task_data);
1654 return ast_threadpool_push(sip_threadpool, sip_task, task_data);
1658 struct sync_task_data {
1663 int (*task)(void *);
1667 static int sync_task(void *data)
1669 struct sync_task_data *std = data;
1670 std->fail = std->task(std->task_data);
1672 ast_mutex_lock(&std->lock);
1674 ast_cond_signal(&std->cond);
1675 ast_mutex_unlock(&std->lock);
1679 int ast_sip_push_task_synchronous(struct ast_taskprocessor *serializer, int (*sip_task)(void *), void *task_data)
1681 /* This method is an onion */
1682 struct sync_task_data std;
1683 ast_mutex_init(&std.lock);
1684 ast_cond_init(&std.cond, NULL);
1685 std.fail = std.complete = 0;
1686 std.task = sip_task;
1687 std.task_data = task_data;
1690 if (ast_taskprocessor_push(serializer, sync_task, &std)) {
1694 if (ast_threadpool_push(sip_threadpool, sync_task, &std)) {
1699 ast_mutex_lock(&std.lock);
1700 while (!std.complete) {
1701 ast_cond_wait(&std.cond, &std.lock);
1703 ast_mutex_unlock(&std.lock);
1705 ast_mutex_destroy(&std.lock);
1706 ast_cond_destroy(&std.cond);
1710 void ast_copy_pj_str(char *dest, const pj_str_t *src, size_t size)
1712 size_t chars_to_copy = MIN(size - 1, pj_strlen(src));
1713 memcpy(dest, pj_strbuf(src), chars_to_copy);
1714 dest[chars_to_copy] = '\0';
1717 int ast_sip_is_content_type(pjsip_media_type *content_type, char *type, char *subtype)
1719 pjsip_media_type compare;
1721 if (!content_type) {
1725 pjsip_media_type_init2(&compare, type, subtype);
1727 return pjsip_media_type_cmp(content_type, &compare, 0) ? -1 : 0;
1730 pj_caching_pool caching_pool;
1731 pj_pool_t *memory_pool;
1732 pj_thread_t *monitor_thread;
1733 static int monitor_continue;
1735 static void *monitor_thread_exec(void *endpt)
1737 while (monitor_continue) {
1738 const pj_time_val delay = {0, 10};
1739 pjsip_endpt_handle_events(ast_pjsip_endpoint, &delay);
1744 static void stop_monitor_thread(void)
1746 monitor_continue = 0;
1747 pj_thread_join(monitor_thread);
1750 AST_THREADSTORAGE(pj_thread_storage);
1751 AST_THREADSTORAGE(servant_id_storage);
1752 #define SIP_SERVANT_ID 0x5E2F1D
1754 static void sip_thread_start(void)
1756 pj_thread_desc *desc;
1757 pj_thread_t *thread;
1758 uint32_t *servant_id;
1760 servant_id = ast_threadstorage_get(&servant_id_storage, sizeof(*servant_id));
1762 ast_log(LOG_ERROR, "Could not set SIP servant ID in thread-local storage.\n");
1765 *servant_id = SIP_SERVANT_ID;
1767 desc = ast_threadstorage_get(&pj_thread_storage, sizeof(pj_thread_desc));
1769 ast_log(LOG_ERROR, "Could not get thread desc from thread-local storage. Expect awful things to occur\n");
1772 pj_bzero(*desc, sizeof(*desc));
1774 if (pj_thread_register("Asterisk Thread", *desc, &thread) != PJ_SUCCESS) {
1775 ast_log(LOG_ERROR, "Couldn't register thread with PJLIB.\n");
1779 int ast_sip_thread_is_servant(void)
1781 uint32_t *servant_id;
1783 servant_id = ast_threadstorage_get(&servant_id_storage, sizeof(*servant_id));
1788 return *servant_id == SIP_SERVANT_ID;
1791 static void remove_request_headers(pjsip_endpoint *endpt)
1793 const pjsip_hdr *request_headers = pjsip_endpt_get_request_headers(endpt);
1794 pjsip_hdr *iter = request_headers->next;
1796 while (iter != request_headers) {
1797 pjsip_hdr *to_erase = iter;
1799 pj_list_erase(to_erase);
1803 static int load_module(void)
1805 /* The third parameter is just copied from
1806 * example code from PJLIB. This can be adjusted
1810 struct ast_threadpool_options options;
1812 if (pj_init() != PJ_SUCCESS) {
1813 return AST_MODULE_LOAD_DECLINE;
1816 if (pjlib_util_init() != PJ_SUCCESS) {
1818 return AST_MODULE_LOAD_DECLINE;
1821 pj_caching_pool_init(&caching_pool, NULL, 1024 * 1024);
1822 if (pjsip_endpt_create(&caching_pool.factory, "SIP", &ast_pjsip_endpoint) != PJ_SUCCESS) {
1823 ast_log(LOG_ERROR, "Failed to create PJSIP endpoint structure. Aborting load\n");
1827 /* PJSIP will automatically try to add a Max-Forwards header. Since we want to control that,
1828 * we need to stop PJSIP from doing it automatically
1830 remove_request_headers(ast_pjsip_endpoint);
1832 memory_pool = pj_pool_create(&caching_pool.factory, "SIP", 1024, 1024, NULL);
1834 ast_log(LOG_ERROR, "Failed to create memory pool for SIP. Aborting load\n");
1838 if (ast_sip_initialize_system()) {
1839 ast_log(LOG_ERROR, "Failed to initialize SIP system configuration. Aborting load\n");
1843 sip_get_threadpool_options(&options);
1844 options.thread_start = sip_thread_start;
1845 sip_threadpool = ast_threadpool_create("SIP", NULL, &options);
1846 if (!sip_threadpool) {
1847 ast_log(LOG_ERROR, "Failed to create SIP threadpool. Aborting load\n");
1851 pjsip_tsx_layer_init_module(ast_pjsip_endpoint);
1852 pjsip_ua_init_module(ast_pjsip_endpoint, NULL);
1854 monitor_continue = 1;
1855 status = pj_thread_create(memory_pool, "SIP", (pj_thread_proc *) &monitor_thread_exec,
1856 NULL, PJ_THREAD_DEFAULT_STACK_SIZE * 2, 0, &monitor_thread);
1857 if (status != PJ_SUCCESS) {
1858 ast_log(LOG_ERROR, "Failed to start SIP monitor thread. Aborting load\n");
1862 ast_sip_initialize_global_headers();
1864 if (ast_res_pjsip_initialize_configuration()) {
1865 ast_log(LOG_ERROR, "Failed to initialize SIP configuration. Aborting load\n");
1869 if (ast_sip_initialize_distributor()) {
1870 ast_log(LOG_ERROR, "Failed to register distributor module. Aborting load\n");
1874 if (ast_sip_initialize_outbound_authentication()) {
1875 ast_log(LOG_ERROR, "Failed to initialize outbound authentication. Aborting load\n");
1879 ast_res_pjsip_init_options_handling(0);
1881 ast_res_pjsip_init_contact_transports();
1883 return AST_MODULE_LOAD_SUCCESS;
1886 ast_sip_destroy_distributor();
1887 ast_res_pjsip_destroy_configuration();
1888 ast_sip_destroy_global_headers();
1889 if (monitor_thread) {
1890 stop_monitor_thread();
1893 pj_pool_release(memory_pool);
1896 if (ast_pjsip_endpoint) {
1897 pjsip_endpt_destroy(ast_pjsip_endpoint);
1898 ast_pjsip_endpoint = NULL;
1900 pj_caching_pool_destroy(&caching_pool);
1901 return AST_MODULE_LOAD_DECLINE;
1904 static int reload_module(void)
1906 if (ast_res_pjsip_reload_configuration()) {
1907 return AST_MODULE_LOAD_DECLINE;
1909 ast_res_pjsip_init_options_handling(1);
1913 static int unload_pjsip(void *data)
1916 pj_pool_release(memory_pool);
1919 if (ast_pjsip_endpoint) {
1920 pjsip_endpt_destroy(ast_pjsip_endpoint);
1921 ast_pjsip_endpoint = NULL;
1923 pj_caching_pool_destroy(&caching_pool);
1927 static int unload_module(void)
1929 ast_res_pjsip_cleanup_options_handling();
1930 ast_sip_destroy_distributor();
1931 ast_res_pjsip_destroy_configuration();
1932 ast_sip_destroy_global_headers();
1933 if (monitor_thread) {
1934 stop_monitor_thread();
1936 /* The thread this is called from cannot call PJSIP/PJLIB functions,
1937 * so we have to push the work to the threadpool to handle
1939 ast_sip_push_task_synchronous(NULL, unload_pjsip, NULL);
1941 ast_threadpool_shutdown(sip_threadpool);
1946 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER, "Basic SIP resource",
1947 .load = load_module,
1948 .unload = unload_module,
1949 .reload = reload_module,
1950 .load_pri = AST_MODPRI_CHANNEL_DEPEND - 5,