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="dtmf_mode" 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="media_address">
212 <synopsis>IP address used in SDP for media handling</synopsis>
214 At the time of SDP creation, the IP address defined here will be used as
215 the media address for individual streams in the SDP.
218 Be aware that the <literal>external_media_address</literal> option, set in Transport
219 configuration, can also affect the final media address used in the SDP.
223 <configOption name="force_rport" default="yes">
224 <synopsis>Force use of return port</synopsis>
226 <configOption name="ice_support" default="no">
227 <synopsis>Enable the ICE mechanism to help traverse NAT</synopsis>
229 <configOption name="identify_by" default="username,location">
230 <synopsis>Way(s) for Endpoint to be identified</synopsis>
232 An endpoint can be identified in multiple ways. Currently, the only supported
233 option is <literal>username</literal>, which matches the endpoint based on the
234 username in the From header.
236 <note><para>Endpoints can also be identified by IP address; however, that method
237 of identification is not handled by this configuration option. See the documentation
238 for the <literal>identify</literal> configuration section for more details on that
239 method of endpoint identification. If this option is set to <literal>username</literal>
240 and an <literal>identify</literal> configuration section exists for the endpoint, then
241 the endpoint can be identified in multiple ways.</para></note>
243 <enum name="username" />
247 <configOption name="redirect_method">
248 <synopsis>How redirects received from an endpoint are handled</synopsis>
250 When a redirect is received from an endpoint there are multiple ways it can be handled.
251 If this option is set to <literal>user</literal> the user portion of the redirect target
252 is treated as an extension within the dialplan and dialed using a Local channel. If this option
253 is set to <literal>uri_core</literal> the target URI is returned to the dialing application
254 which dials it using the PJSIP channel driver and endpoint originally used. If this option is
255 set to <literal>uri_pjsip</literal> the redirect occurs within chan_pjsip itself and is not exposed
256 to the core at all. The <literal>uri_pjsip</literal> option has the benefit of being more efficient
257 and also supporting multiple potential redirect targets. The con is that since redirection occurs
258 within chan_pjsip redirecting information is not forwarded and redirection can not be
263 <enum name="uri_core" />
264 <enum name="uri_pjsip" />
268 <configOption name="mailboxes">
269 <synopsis>Mailbox(es) to be associated with</synopsis>
271 <configOption name="moh_suggest" default="default">
272 <synopsis>Default Music On Hold class</synopsis>
274 <configOption name="outbound_auth">
275 <synopsis>Authentication object used for outbound requests</synopsis>
277 <configOption name="outbound_proxy">
278 <synopsis>Proxy through which to send requests, a full SIP URI must be provided</synopsis>
280 <configOption name="rewrite_contact">
281 <synopsis>Allow Contact header to be rewritten with the source IP address-port</synopsis>
283 On inbound SIP messages from this endpoint, the Contact header will be changed to have the
284 source IP address and port. This option does not affect outbound messages send to this
286 </para></description>
288 <configOption name="rtp_ipv6" default="no">
289 <synopsis>Allow use of IPv6 for RTP traffic</synopsis>
291 <configOption name="rtp_symmetric" default="no">
292 <synopsis>Enforce that RTP must be symmetric</synopsis>
294 <configOption name="send_diversion" default="yes">
295 <synopsis>Send the Diversion header, conveying the diversion
296 information to the called user agent</synopsis>
298 <configOption name="send_pai" default="no">
299 <synopsis>Send the P-Asserted-Identity header</synopsis>
301 <configOption name="send_rpid" default="no">
302 <synopsis>Send the Remote-Party-ID header</synopsis>
304 <configOption name="timers_min_se" default="90">
305 <synopsis>Minimum session timers expiration period</synopsis>
307 Minimium session timer expiration period. Time in seconds.
308 </para></description>
310 <configOption name="timers" default="yes">
311 <synopsis>Session timers for SIP packets</synopsis>
314 <enum name="forced" />
316 <enum name="required" />
321 <configOption name="timers_sess_expires" default="1800">
322 <synopsis>Maximum session timer expiration period</synopsis>
324 Maximium session timer expiration period. Time in seconds.
325 </para></description>
327 <configOption name="transport">
328 <synopsis>Desired transport configuration</synopsis>
330 This will set the desired transport configuration to send SIP data through.
332 <warning><para>Not specifying a transport will <emphasis>DEFAULT</emphasis>
333 to the first configured transport in <filename>pjsip.conf</filename> which is
334 valid for the URI we are trying to contact.
336 <warning><para>Transport configuration is not affected by reloads. In order to
337 change transports, a full Asterisk restart is required</para></warning>
340 <configOption name="trust_id_inbound" default="no">
341 <synopsis>Accept identification information received from this endpoint</synopsis>
342 <description><para>This option determines whether Asterisk will accept
343 identification from the endpoint from headers such as P-Asserted-Identity
344 or Remote-Party-ID header. This option applies both to calls originating from the
345 endpoint and calls originating from Asterisk. If <literal>no</literal>, the
346 configured Caller-ID from pjsip.conf will always be used as the identity for
347 the endpoint.</para></description>
349 <configOption name="trust_id_outbound" default="no">
350 <synopsis>Send private identification details to the endpoint.</synopsis>
351 <description><para>This option determines whether res_pjsip will send private
352 identification information to the endpoint. If <literal>no</literal>,
353 private Caller-ID information will not be forwarded to the endpoint.
354 "Private" in this case refers to any method of restricting identification.
355 Example: setting <replaceable>callerid_privacy</replaceable> to any
356 <literal>prohib</literal> variation.
357 Example: If <replaceable>trust_id_inbound</replaceable> is set to
358 <literal>yes</literal>, the presence of a <literal>Privacy: id</literal>
359 header in a SIP request or response would indicate the identification
360 provided in the request is private.</para></description>
362 <configOption name="type">
363 <synopsis>Must be of type 'endpoint'.</synopsis>
365 <configOption name="use_ptime" default="no">
366 <synopsis>Use Endpoint's requested packetisation interval</synopsis>
368 <configOption name="use_avpf" default="no">
369 <synopsis>Determines whether res_pjsip will use and enforce usage of AVPF for this
372 If set to <literal>yes</literal>, res_pjsip will use use the AVPF or SAVPF RTP
373 profile for all media offers on outbound calls and media updates and will
374 decline media offers not using the AVPF or SAVPF profile.
376 If set to <literal>no</literal>, res_pjsip will use use the AVP or SAVP RTP
377 profile for all media offers on outbound calls and media updates and will
378 decline media offers not using the AVP or SAVP profile.
379 </para></description>
381 <configOption name="media_encryption" default="no">
382 <synopsis>Determines whether res_pjsip will use and enforce usage of media encryption
383 for this endpoint.</synopsis>
386 <enum name="no"><para>
387 res_pjsip will offer no encryption and allow no encryption to be setup.
389 <enum name="sdes"><para>
390 res_pjsip will offer standard SRTP setup via in-SDP keys. Encrypted SIP
391 transport should be used in conjunction with this option to prevent
392 exposure of media encryption keys.
394 <enum name="dtls"><para>
395 res_pjsip will offer DTLS-SRTP setup.
400 <configOption name="inband_progress" default="no">
401 <synopsis>Determines whether chan_pjsip will indicate ringing using inband
404 If set to <literal>yes</literal>, chan_pjsip will send a 183 Session Progress
405 when told to indicate ringing and will immediately start sending ringing
408 If set to <literal>no</literal>, chan_pjsip will send a 180 Ringing when told
409 to indicate ringing and will NOT send it as audio.
410 </para></description>
412 <configOption name="call_group">
413 <synopsis>The numeric pickup groups for a channel.</synopsis>
415 Can be set to a comma separated list of numbers or ranges between the values
416 of 0-63 (maximum of 64 groups).
417 </para></description>
419 <configOption name="pickup_group">
420 <synopsis>The numeric pickup groups that a channel can pickup.</synopsis>
422 Can be set to a comma separated list of numbers or ranges between the values
423 of 0-63 (maximum of 64 groups).
424 </para></description>
426 <configOption name="named_call_group">
427 <synopsis>The named pickup groups for a channel.</synopsis>
429 Can be set to a comma separated list of case sensitive strings limited by
430 supported line length.
431 </para></description>
433 <configOption name="named_pickup_group">
434 <synopsis>The named pickup groups that a channel can pickup.</synopsis>
436 Can be set to a comma separated list of case sensitive strings limited by
437 supported line length.
438 </para></description>
440 <configOption name="device_state_busy_at" default="0">
441 <synopsis>The number of in-use channels which will cause busy to be returned as device state</synopsis>
443 When the number of in-use channels for the endpoint matches the devicestate_busy_at setting the
444 PJSIP channel driver will return busy as the device state instead of in use.
445 </para></description>
447 <configOption name="t38_udptl" default="no">
448 <synopsis>Whether T.38 UDPTL support is enabled or not</synopsis>
450 If set to yes T.38 UDPTL support will be enabled, and T.38 negotiation requests will be accepted
452 </para></description>
454 <configOption name="t38_udptl_ec" default="none">
455 <synopsis>T.38 UDPTL error correction method</synopsis>
458 <enum name="none"><para>
459 No error correction should be used.
461 <enum name="fec"><para>
462 Forward error correction should be used.
464 <enum name="redundancy"><para>
465 Redundacy error correction should be used.
470 <configOption name="t38_udptl_maxdatagram" default="0">
471 <synopsis>T.38 UDPTL maximum datagram size</synopsis>
473 This option can be set to override the maximum datagram of a remote endpoint for broken
475 </para></description>
477 <configOption name="fax_detect" default="no">
478 <synopsis>Whether CNG tone detection is enabled</synopsis>
480 This option can be set to send the session to the fax extension when a CNG tone is
482 </para></description>
484 <configOption name="t38_udptl_nat" default="no">
485 <synopsis>Whether NAT support is enabled on UDPTL sessions</synopsis>
487 When enabled the UDPTL stack will send UDPTL packets to the source address of
489 </para></description>
491 <configOption name="t38_udptl_ipv6" default="no">
492 <synopsis>Whether IPv6 is used for UDPTL Sessions</synopsis>
494 When enabled the UDPTL stack will use IPv6.
495 </para></description>
497 <configOption name="tone_zone">
498 <synopsis>Set which country's indications to use for channels created for this endpoint.</synopsis>
500 <configOption name="language">
501 <synopsis>Set the default language to use for channels created for this endpoint.</synopsis>
503 <configOption name="one_touch_recording" default="no">
504 <synopsis>Determines whether one-touch recording is allowed for this endpoint.</synopsis>
506 <ref type="configOption">recordonfeature</ref>
507 <ref type="configOption">recordofffeature</ref>
510 <configOption name="record_on_feature" default="automixmon">
511 <synopsis>The feature to enact when one-touch recording is turned on.</synopsis>
513 <para>When an INFO request for one-touch recording arrives with a Record header set to "on", this
514 feature will be enabled for the channel. The feature designated here can be any built-in
515 or dynamic feature defined in features.conf.</para>
516 <note><para>This setting has no effect if the endpoint's one_touch_recording option is disabled</para></note>
519 <ref type="configOption">one_touch_recording</ref>
520 <ref type="configOption">recordofffeature</ref>
523 <configOption name="record_off_feature" default="automixmon">
524 <synopsis>The feature to enact when one-touch recording is turned off.</synopsis>
526 <para>When an INFO request for one-touch recording arrives with a Record header set to "off", this
527 feature will be enabled for the channel. The feature designated here can be any built-in
528 or dynamic feature defined in features.conf.</para>
529 <note><para>This setting has no effect if the endpoint's one_touch_recording option is disabled</para></note>
532 <ref type="configOption">one_touch_recording</ref>
533 <ref type="configOption">recordonfeature</ref>
536 <configOption name="rtp_engine" default="asterisk">
537 <synopsis>Name of the RTP engine to use for channels created for this endpoint</synopsis>
539 <configOption name="allow_transfer" default="yes">
540 <synopsis>Determines whether SIP REFER transfers are allowed for this endpoint</synopsis>
542 <configOption name="sdp_owner" default="-">
543 <synopsis>String placed as the username portion of an SDP origin (o=) line.</synopsis>
545 <configOption name="sdp_session" default="Asterisk">
546 <synopsis>String used for the SDP session (s=) line.</synopsis>
548 <configOption name="tos_audio">
549 <synopsis>DSCP TOS bits for audio streams</synopsis>
551 See https://wiki.asterisk.org/wiki/display/AST/IP+Quality+of+Service for more information about QoS settings
552 </para></description>
554 <configOption name="tos_video">
555 <synopsis>DSCP TOS bits for video streams</synopsis>
557 See https://wiki.asterisk.org/wiki/display/AST/IP+Quality+of+Service for more information about QoS settings
558 </para></description>
560 <configOption name="cos_audio">
561 <synopsis>Priority for audio streams</synopsis>
563 See https://wiki.asterisk.org/wiki/display/AST/IP+Quality+of+Service for more information about QoS settings
564 </para></description>
566 <configOption name="cos_video">
567 <synopsis>Priority for video streams</synopsis>
569 See https://wiki.asterisk.org/wiki/display/AST/IP+Quality+of+Service for more information about QoS settings
570 </para></description>
572 <configOption name="allow_subscribe" default="yes">
573 <synopsis>Determines if endpoint is allowed to initiate subscriptions with Asterisk.</synopsis>
575 <configOption name="sub_min_expiry" default="60">
576 <synopsis>The minimum allowed expiry time for subscriptions initiated by the endpoint.</synopsis>
578 <configOption name="from_user">
579 <synopsis>Username to use in From header for requests to this endpoint.</synopsis>
581 <configOption name="mwi_from_user">
582 <synopsis>Username to use in From header for unsolicited MWI NOTIFYs to this endpoint.</synopsis>
584 <configOption name="from_domain">
585 <synopsis>Domain to user in From header for requests to this endpoint.</synopsis>
587 <configOption name="dtls_verify">
588 <synopsis>Verify that the provided peer certificate is valid</synopsis>
590 This option only applies if <replaceable>media_encryption</replaceable> is
591 set to <literal>dtls</literal>.
592 </para></description>
594 <configOption name="dtls_rekey">
595 <synopsis>Interval at which to renegotiate the TLS session and rekey the SRTP session</synopsis>
597 This option only applies if <replaceable>media_encryption</replaceable> is
598 set to <literal>dtls</literal>.
600 If this is not set or the value provided is 0 rekeying will be disabled.
601 </para></description>
603 <configOption name="dtls_cert_file">
604 <synopsis>Path to certificate file to present to peer</synopsis>
606 This option only applies if <replaceable>media_encryption</replaceable> is
607 set to <literal>dtls</literal>.
608 </para></description>
610 <configOption name="dtls_private_key">
611 <synopsis>Path to private key for certificate file</synopsis>
613 This option only applies if <replaceable>media_encryption</replaceable> is
614 set to <literal>dtls</literal>.
615 </para></description>
617 <configOption name="dtls_cipher">
618 <synopsis>Cipher to use for DTLS negotiation</synopsis>
620 This option only applies if <replaceable>media_encryption</replaceable> is
621 set to <literal>dtls</literal>.
623 Many options for acceptable ciphers. See link for more:
624 http://www.openssl.org/docs/apps/ciphers.html#CIPHER_STRINGS
625 </para></description>
627 <configOption name="dtls_ca_file">
628 <synopsis>Path to certificate authority certificate</synopsis>
630 This option only applies if <replaceable>media_encryption</replaceable> is
631 set to <literal>dtls</literal>.
632 </para></description>
634 <configOption name="dtls_ca_path">
635 <synopsis>Path to a directory containing certificate authority certificates</synopsis>
637 This option only applies if <replaceable>media_encryption</replaceable> is
638 set to <literal>dtls</literal>.
639 </para></description>
641 <configOption name="dtls_setup">
642 <synopsis>Whether we are willing to accept connections, connect to the other party, or both.</synopsis>
645 This option only applies if <replaceable>media_encryption</replaceable> is
646 set to <literal>dtls</literal>.
649 <enum name="active"><para>
650 res_pjsip will make a connection to the peer.
652 <enum name="passive"><para>
653 res_pjsip will accept connections from the peer.
655 <enum name="actpass"><para>
656 res_pjsip will offer and accept connections from the peer.
661 <configOption name="srtp_tag_32">
662 <synopsis>Determines whether 32 byte tags should be used instead of 80 byte tags.</synopsis>
664 This option only applies if <replaceable>media_encryption</replaceable> is
665 set to <literal>sdes</literal> or <literal>dtls</literal>.
666 </para></description>
669 <configObject name="auth">
670 <synopsis>Authentication type</synopsis>
672 Authentication objects hold the authentication information for use
673 by other objects such as <literal>endpoints</literal> or <literal>registrations</literal>.
674 This also allows for multiple objects to use a single auth object. See
675 the <literal>auth_type</literal> config option for password style choices.
676 </para></description>
677 <configOption name="auth_type" default="userpass">
678 <synopsis>Authentication type</synopsis>
680 This option specifies which of the password style config options should be read
681 when trying to authenticate an endpoint inbound request. If set to <literal>userpass</literal>
682 then we'll read from the 'password' option. For <literal>md5</literal> we'll read
687 <enum name="userpass"/>
691 <configOption name="nonce_lifetime" default="32">
692 <synopsis>Lifetime of a nonce associated with this authentication config.</synopsis>
694 <configOption name="md5_cred">
695 <synopsis>MD5 Hash used for authentication.</synopsis>
696 <description><para>Only used when auth_type is <literal>md5</literal>.</para></description>
698 <configOption name="password">
699 <synopsis>PlainText password used for authentication.</synopsis>
700 <description><para>Only used when auth_type is <literal>userpass</literal>.</para></description>
702 <configOption name="realm" default="asterisk">
703 <synopsis>SIP realm for endpoint</synopsis>
705 <configOption name="type">
706 <synopsis>Must be 'auth'</synopsis>
708 <configOption name="username">
709 <synopsis>Username to use for account</synopsis>
712 <configObject name="domain_alias">
713 <synopsis>Domain Alias</synopsis>
715 Signifies that a domain is an alias. If the domain on a session is
716 not found to match an AoR then this object is used to see if we have
717 an alias for the AoR to which the endpoint is binding. This objects
718 name as defined in configuration should be the domain alias and a
719 config option is provided to specify the domain to be aliased.
720 </para></description>
721 <configOption name="type">
722 <synopsis>Must be of type 'domain_alias'.</synopsis>
724 <configOption name="domain">
725 <synopsis>Domain to be aliased</synopsis>
728 <configObject name="transport">
729 <synopsis>SIP Transport</synopsis>
731 <emphasis>Transports</emphasis>
733 <para>There are different transports and protocol derivatives
734 supported by <literal>res_pjsip</literal>. They are in order of
735 preference: UDP, TCP, and WebSocket (WS).</para>
736 <note><para>Changes to transport configuration in pjsip.conf will only be
737 effected on a complete restart of Asterisk. A module reload
738 will not suffice.</para></note>
740 <configOption name="async_operations" default="1">
741 <synopsis>Number of simultaneous Asynchronous Operations</synopsis>
743 <configOption name="bind">
744 <synopsis>IP Address and optional port to bind to for this transport</synopsis>
746 <configOption name="ca_list_file">
747 <synopsis>File containing a list of certificates to read (TLS ONLY)</synopsis>
749 <configOption name="cert_file">
750 <synopsis>Certificate file for endpoint (TLS ONLY)</synopsis>
752 <configOption name="cipher">
753 <synopsis>Preferred Cryptography Cipher (TLS ONLY)</synopsis>
755 Many options for acceptable ciphers see link for more:
756 http://www.openssl.org/docs/apps/ciphers.html#CIPHER_STRINGS
757 </para></description>
759 <configOption name="domain">
760 <synopsis>Domain the transport comes from</synopsis>
762 <configOption name="external_media_address">
763 <synopsis>External IP address to use in RTP handling</synopsis>
765 When a request or response is sent out, if the destination of the
766 message is outside the IP network defined in the option <literal>localnet</literal>,
767 and the media address in the SDP is within the localnet network, then the
768 media address in the SDP will be rewritten to the value defined for
769 <literal>external_media_address</literal>.
770 </para></description>
772 <configOption name="external_signaling_address">
773 <synopsis>External address for SIP signalling</synopsis>
775 <configOption name="external_signaling_port" default="0">
776 <synopsis>External port for SIP signalling</synopsis>
778 <configOption name="method">
779 <synopsis>Method of SSL transport (TLS ONLY)</synopsis>
782 <enum name="default" />
783 <enum name="unspecified" />
784 <enum name="tlsv1" />
785 <enum name="sslv2" />
786 <enum name="sslv3" />
787 <enum name="sslv23" />
791 <configOption name="local_net">
792 <synopsis>Network to consider local (used for NAT purposes).</synopsis>
793 <description><para>This must be in CIDR or dotted decimal format with the IP
794 and mask separated with a slash ('/').</para></description>
796 <configOption name="password">
797 <synopsis>Password required for transport</synopsis>
799 <configOption name="priv_key_file">
800 <synopsis>Private key file (TLS ONLY)</synopsis>
802 <configOption name="protocol" default="udp">
803 <synopsis>Protocol to use for SIP traffic</synopsis>
814 <configOption name="require_client_cert" default="false">
815 <synopsis>Require client certificate (TLS ONLY)</synopsis>
817 <configOption name="type">
818 <synopsis>Must be of type 'transport'.</synopsis>
820 <configOption name="verify_client" default="false">
821 <synopsis>Require verification of client certificate (TLS ONLY)</synopsis>
823 <configOption name="verify_server" default="false">
824 <synopsis>Require verification of server certificate (TLS ONLY)</synopsis>
826 <configOption name="tos" default="false">
827 <synopsis>Enable TOS for the signalling sent over this transport</synopsis>
829 <para>See <literal>https://wiki.asterisk.org/wiki/display/AST/IP+Quality+of+Service</literal>
830 for more information on this parameter.</para>
831 <note><para>This option does not apply to the <replaceable>ws</replaceable>
832 or the <replaceable>wss</replaceable> protocols.</para></note>
835 <configOption name="cos" default="false">
836 <synopsis>Enable COS for the signalling sent over this transport</synopsis>
838 <para>See <literal>https://wiki.asterisk.org/wiki/display/AST/IP+Quality+of+Service</literal>
839 for more information on this parameter.</para>
840 <note><para>This option does not apply to the <replaceable>ws</replaceable>
841 or the <replaceable>wss</replaceable> protocols.</para></note>
845 <configObject name="contact">
846 <synopsis>A way of creating an aliased name to a SIP URI</synopsis>
848 Contacts are a way to hide SIP URIs from the dialplan directly.
849 They are also used to make a group of contactable parties when
850 in use with <literal>AoR</literal> lists.
851 </para></description>
852 <configOption name="type">
853 <synopsis>Must be of type 'contact'.</synopsis>
855 <configOption name="uri">
856 <synopsis>SIP URI to contact peer</synopsis>
858 <configOption name="expiration_time">
859 <synopsis>Time to keep alive a contact</synopsis>
861 Time to keep alive a contact. String style specification.
862 </para></description>
864 <configOption name="qualify_frequency" default="0">
865 <synopsis>Interval at which to qualify a contact</synopsis>
867 Interval between attempts to qualify the contact for reachability.
868 If <literal>0</literal> never qualify. Time in seconds.
869 </para></description>
871 <configOption name="outbound_proxy">
872 <synopsis>Outbound proxy used when sending OPTIONS request</synopsis>
874 If set the provided URI will be used as the outbound proxy when an
875 OPTIONS request is sent to a contact for qualify purposes.
876 </para></description>
879 <configObject name="aor">
880 <synopsis>The configuration for a location of an endpoint</synopsis>
882 An AoR is what allows Asterisk to contact an endpoint via res_pjsip. If no
883 AoRs are specified, an endpoint will not be reachable by Asterisk.
884 Beyond that, an AoR has other uses within Asterisk, such as inbound
887 An <literal>AoR</literal> is a way to allow dialing a group
888 of <literal>Contacts</literal> that all use the same
889 <literal>endpoint</literal> for calls.
891 This can be used as another way of grouping a list of contacts to dial
892 rather than specifing them each directly when dialing via the dialplan.
893 This must be used in conjuction with the <literal>PJSIP_DIAL_CONTACTS</literal>.
895 Registrations: For Asterisk to match an inbound registration to an endpoint,
896 the AoR object name must match the user portion of the SIP URI in the "To:"
897 header of the inbound SIP registration. That will usually be equivalent
898 to the "user name" set in your hard or soft phones configuration.
899 </para></description>
900 <configOption name="contact">
901 <synopsis>Permanent contacts assigned to AoR</synopsis>
903 Contacts specified will be called whenever referenced
904 by <literal>chan_pjsip</literal>.
906 Use a separate "contact=" entry for each contact required. Contacts
907 are specified using a SIP URI.
908 </para></description>
910 <configOption name="default_expiration" default="3600">
911 <synopsis>Default expiration time in seconds for contacts that are dynamically bound to an AoR.</synopsis>
913 <configOption name="mailboxes">
914 <synopsis>Mailbox(es) to be associated with</synopsis>
915 <description><para>This option applies when an external entity subscribes to an AoR
916 for message waiting indications. The mailboxes specified will be subscribed to.
917 More than one mailbox can be specified with a comma-delimited string.</para></description>
919 <configOption name="maximum_expiration" default="7200">
920 <synopsis>Maximum time to keep an AoR</synopsis>
922 Maximium time to keep a peer with explicit expiration. Time in seconds.
923 </para></description>
925 <configOption name="max_contacts" default="0">
926 <synopsis>Maximum number of contacts that can bind to an AoR</synopsis>
928 Maximum number of contacts that can associate with this AoR. This value does
929 not affect the number of contacts that can be added with the "contact" option.
930 It only limits contacts added through external interaction, such as
933 <note><para>This should be set to <literal>1</literal> and
934 <replaceable>remove_existing</replaceable> set to <literal>yes</literal> if you
935 wish to stick with the older <literal>chan_sip</literal> behaviour.
939 <configOption name="minimum_expiration" default="60">
940 <synopsis>Minimum keep alive time for an AoR</synopsis>
942 Minimum time to keep a peer with an explict expiration. Time in seconds.
943 </para></description>
945 <configOption name="remove_existing" default="no">
946 <synopsis>Determines whether new contacts replace existing ones.</synopsis>
948 On receiving a new registration to the AoR should it remove
949 the existing contact that was registered against it?
951 <note><para>This should be set to <literal>yes</literal> and
952 <replaceable>max_contacts</replaceable> set to <literal>1</literal> if you
953 wish to stick with the older <literal>chan_sip</literal> behaviour.
957 <configOption name="type">
958 <synopsis>Must be of type 'aor'.</synopsis>
960 <configOption name="qualify_frequency" default="0">
961 <synopsis>Interval at which to qualify an AoR</synopsis>
963 Interval between attempts to qualify the AoR for reachability.
964 If <literal>0</literal> never qualify. Time in seconds.
965 </para></description>
967 <configOption name="authenticate_qualify" default="no">
968 <synopsis>Authenticates a qualify request if needed</synopsis>
970 If true and a qualify request receives a challenge or authenticate response
971 authentication is attempted before declaring the contact available.
972 </para></description>
974 <configOption name="outbound_proxy">
975 <synopsis>Outbound proxy used when sending OPTIONS request</synopsis>
977 If set the provided URI will be used as the outbound proxy when an
978 OPTIONS request is sent to a contact for qualify purposes.
979 </para></description>
982 <configObject name="system">
983 <synopsis>Options that apply to the SIP stack as well as other system-wide settings</synopsis>
985 The settings in this section are global. In addition to being global, the values will
986 not be re-evaluated when a reload is performed. This is because the values must be set
987 before the SIP stack is initialized. The only way to reset these values is to either
988 restart Asterisk, or unload res_pjsip.so and then load it again.
989 </para></description>
990 <configOption name="timer_t1" default="500">
991 <synopsis>Set transaction timer T1 value (milliseconds).</synopsis>
993 Timer T1 is the base for determining how long to wait before retransmitting
994 requests that receive no response when using an unreliable transport (e.g. UDP).
995 For more information on this timer, see RFC 3261, Section 17.1.1.1.
996 </para></description>
998 <configOption name="timer_b" default="32000">
999 <synopsis>Set transaction timer B value (milliseconds).</synopsis>
1001 Timer B determines the maximum amount of time to wait after sending an INVITE
1002 request before terminating the transaction. It is recommended that this be set
1003 to 64 * Timer T1, but it may be set higher if desired. For more information on
1004 this timer, see RFC 3261, Section 17.1.1.1.
1005 </para></description>
1007 <configOption name="compact_headers" default="no">
1008 <synopsis>Use the short forms of common SIP header names.</synopsis>
1010 <configOption name="threadpool_initial_size" default="0">
1011 <synopsis>Initial number of threads in the res_pjsip threadpool.</synopsis>
1013 <configOption name="threadpool_auto_increment" default="5">
1014 <synopsis>The amount by which the number of threads is incremented when necessary.</synopsis>
1016 <configOption name="threadpool_idle_timeout" default="60">
1017 <synopsis>Number of seconds before an idle thread should be disposed of.</synopsis>
1019 <configOption name="threadpool_max_size" default="0">
1020 <synopsis>Maximum number of threads in the res_pjsip threadpool.
1021 A value of 0 indicates no maximum.</synopsis>
1023 <configOption name="type">
1024 <synopsis>Must be of type 'system'.</synopsis>
1027 <configObject name="global">
1028 <synopsis>Options that apply globally to all SIP communications</synopsis>
1030 The settings in this section are global. Unlike options in the <literal>system</literal>
1031 section, these options can be refreshed by performing a reload.
1032 </para></description>
1033 <configOption name="max_forwards" default="70">
1034 <synopsis>Value used in Max-Forwards header for SIP requests.</synopsis>
1036 <configOption name="type">
1037 <synopsis>Must be of type 'global'.</synopsis>
1039 <configOption name="user_agent" default="Asterisk <Asterisk Version>">
1040 <synopsis>Value used in User-Agent header for SIP requests and Server header for SIP responses.</synopsis>
1042 <configOption name="default_outbound_endpoint" default="default_outbound_endpoint">
1043 <synopsis>Endpoint to use when sending an outbound request to a URI without a specified endpoint.</synopsis>
1049 <manager name="PJSIPQualify" language="en_US">
1051 Qualify a chan_pjsip endpoint.
1054 <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
1055 <parameter name="Endpoint" required="true">
1056 <para>The endpoint you want to qualify.</para>
1060 <para>Qualify a chan_pjsip endpoint.</para>
1063 <manager name="PJSIPShowEndpoints" language="en_US">
1065 Lists PJSIP endpoints.
1070 Provides a listing of all endpoints. For each endpoint an <literal>EndpointList</literal> event
1071 is raised that contains relevant attributes and status information. Once all
1072 endpoints have been listed an <literal>EndpointListComplete</literal> event is issued.
1076 <manager name="PJSIPShowEndpoint" language="en_US">
1078 Detail listing of an endpoint and its objects.
1081 <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
1082 <parameter name="Endpoint" required="true">
1083 <para>The endpoint to list.</para>
1088 Provides a detailed listing of options for a given endpoint. Events are issued
1089 showing the configuration and status of the endpoint and associated objects. These
1090 events include <literal>EndpointDetail</literal>, <literal>AorDetail</literal>,
1091 <literal>AuthDetail</literal>, <literal>TransportDetail</literal>, and
1092 <literal>IdentifyDetail</literal>. Some events may be listed multiple times if multiple objects are
1093 associated (for instance AoRs). Once all detail events have been raised a final
1094 <literal>EndpointDetailComplete</literal> event is issued.
1101 static pjsip_endpoint *ast_pjsip_endpoint;
1103 static struct ast_threadpool *sip_threadpool;
1105 static int register_service(void *data)
1107 pjsip_module **module = data;
1108 if (!ast_pjsip_endpoint) {
1109 ast_log(LOG_ERROR, "There is no PJSIP endpoint. Unable to register services\n");
1112 if (pjsip_endpt_register_module(ast_pjsip_endpoint, *module) != PJ_SUCCESS) {
1113 ast_log(LOG_ERROR, "Unable to register module %.*s\n", (int) pj_strlen(&(*module)->name), pj_strbuf(&(*module)->name));
1116 ast_debug(1, "Registered SIP service %.*s (%p)\n", (int) pj_strlen(&(*module)->name), pj_strbuf(&(*module)->name), *module);
1117 ast_module_ref(ast_module_info->self);
1121 int ast_sip_register_service(pjsip_module *module)
1123 return ast_sip_push_task_synchronous(NULL, register_service, &module);
1126 static int unregister_service(void *data)
1128 pjsip_module **module = data;
1129 ast_module_unref(ast_module_info->self);
1130 if (!ast_pjsip_endpoint) {
1133 pjsip_endpt_unregister_module(ast_pjsip_endpoint, *module);
1134 ast_debug(1, "Unregistered SIP service %.*s\n", (int) pj_strlen(&(*module)->name), pj_strbuf(&(*module)->name));
1138 void ast_sip_unregister_service(pjsip_module *module)
1140 ast_sip_push_task_synchronous(NULL, unregister_service, &module);
1143 static struct ast_sip_authenticator *registered_authenticator;
1145 int ast_sip_register_authenticator(struct ast_sip_authenticator *auth)
1147 if (registered_authenticator) {
1148 ast_log(LOG_WARNING, "Authenticator %p is already registered. Cannot register a new one\n", registered_authenticator);
1151 registered_authenticator = auth;
1152 ast_debug(1, "Registered SIP authenticator module %p\n", auth);
1153 ast_module_ref(ast_module_info->self);
1157 void ast_sip_unregister_authenticator(struct ast_sip_authenticator *auth)
1159 if (registered_authenticator != auth) {
1160 ast_log(LOG_WARNING, "Trying to unregister authenticator %p but authenticator %p registered\n",
1161 auth, registered_authenticator);
1164 registered_authenticator = NULL;
1165 ast_debug(1, "Unregistered SIP authenticator %p\n", auth);
1166 ast_module_unref(ast_module_info->self);
1169 int ast_sip_requires_authentication(struct ast_sip_endpoint *endpoint, pjsip_rx_data *rdata)
1171 if (!registered_authenticator) {
1172 ast_log(LOG_WARNING, "No SIP authenticator registered. Assuming authentication is not required\n");
1176 return registered_authenticator->requires_authentication(endpoint, rdata);
1179 enum ast_sip_check_auth_result ast_sip_check_authentication(struct ast_sip_endpoint *endpoint,
1180 pjsip_rx_data *rdata, pjsip_tx_data *tdata)
1182 if (!registered_authenticator) {
1183 ast_log(LOG_WARNING, "No SIP authenticator registered. Assuming authentication is successful\n");
1186 return registered_authenticator->check_authentication(endpoint, rdata, tdata);
1189 static struct ast_sip_outbound_authenticator *registered_outbound_authenticator;
1191 int ast_sip_register_outbound_authenticator(struct ast_sip_outbound_authenticator *auth)
1193 if (registered_outbound_authenticator) {
1194 ast_log(LOG_WARNING, "Outbound authenticator %p is already registered. Cannot register a new one\n", registered_outbound_authenticator);
1197 registered_outbound_authenticator = auth;
1198 ast_debug(1, "Registered SIP outbound authenticator module %p\n", auth);
1199 ast_module_ref(ast_module_info->self);
1203 void ast_sip_unregister_outbound_authenticator(struct ast_sip_outbound_authenticator *auth)
1205 if (registered_outbound_authenticator != auth) {
1206 ast_log(LOG_WARNING, "Trying to unregister outbound authenticator %p but outbound authenticator %p registered\n",
1207 auth, registered_outbound_authenticator);
1210 registered_outbound_authenticator = NULL;
1211 ast_debug(1, "Unregistered SIP outbound authenticator %p\n", auth);
1212 ast_module_unref(ast_module_info->self);
1215 int ast_sip_create_request_with_auth(const struct ast_sip_auth_vector *auths, pjsip_rx_data *challenge,
1216 pjsip_transaction *tsx, pjsip_tx_data **new_request)
1218 if (!registered_outbound_authenticator) {
1219 ast_log(LOG_WARNING, "No SIP outbound authenticator registered. Cannot respond to authentication challenge\n");
1222 return registered_outbound_authenticator->create_request_with_auth(auths, challenge, tsx, new_request);
1225 struct endpoint_identifier_list {
1226 struct ast_sip_endpoint_identifier *identifier;
1227 AST_RWLIST_ENTRY(endpoint_identifier_list) list;
1230 static AST_RWLIST_HEAD_STATIC(endpoint_identifiers, endpoint_identifier_list);
1232 int ast_sip_register_endpoint_identifier(struct ast_sip_endpoint_identifier *identifier)
1234 struct endpoint_identifier_list *id_list_item;
1235 SCOPED_LOCK(lock, &endpoint_identifiers, AST_RWLIST_WRLOCK, AST_RWLIST_UNLOCK);
1237 id_list_item = ast_calloc(1, sizeof(*id_list_item));
1238 if (!id_list_item) {
1239 ast_log(LOG_ERROR, "Unabled to add endpoint identifier. Out of memory.\n");
1242 id_list_item->identifier = identifier;
1244 AST_RWLIST_INSERT_TAIL(&endpoint_identifiers, id_list_item, list);
1245 ast_debug(1, "Registered endpoint identifier %p\n", identifier);
1247 ast_module_ref(ast_module_info->self);
1251 void ast_sip_unregister_endpoint_identifier(struct ast_sip_endpoint_identifier *identifier)
1253 struct endpoint_identifier_list *iter;
1254 SCOPED_LOCK(lock, &endpoint_identifiers, AST_RWLIST_WRLOCK, AST_RWLIST_UNLOCK);
1255 AST_RWLIST_TRAVERSE_SAFE_BEGIN(&endpoint_identifiers, iter, list) {
1256 if (iter->identifier == identifier) {
1257 AST_RWLIST_REMOVE_CURRENT(list);
1259 ast_debug(1, "Unregistered endpoint identifier %p\n", identifier);
1260 ast_module_unref(ast_module_info->self);
1264 AST_RWLIST_TRAVERSE_SAFE_END;
1267 struct ast_sip_endpoint *ast_sip_identify_endpoint(pjsip_rx_data *rdata)
1269 struct endpoint_identifier_list *iter;
1270 struct ast_sip_endpoint *endpoint = NULL;
1271 SCOPED_LOCK(lock, &endpoint_identifiers, AST_RWLIST_RDLOCK, AST_RWLIST_UNLOCK);
1272 AST_RWLIST_TRAVERSE(&endpoint_identifiers, iter, list) {
1273 ast_assert(iter->identifier->identify_endpoint != NULL);
1274 endpoint = iter->identifier->identify_endpoint(rdata);
1282 AST_RWLIST_HEAD_STATIC(endpoint_formatters, ast_sip_endpoint_formatter);
1284 int ast_sip_register_endpoint_formatter(struct ast_sip_endpoint_formatter *obj)
1286 SCOPED_LOCK(lock, &endpoint_formatters, AST_RWLIST_WRLOCK, AST_RWLIST_UNLOCK);
1287 AST_RWLIST_INSERT_TAIL(&endpoint_formatters, obj, next);
1288 ast_module_ref(ast_module_info->self);
1292 void ast_sip_unregister_endpoint_formatter(struct ast_sip_endpoint_formatter *obj)
1294 struct ast_sip_endpoint_formatter *i;
1295 SCOPED_LOCK(lock, &endpoint_formatters, AST_RWLIST_WRLOCK, AST_RWLIST_UNLOCK);
1296 AST_RWLIST_TRAVERSE_SAFE_BEGIN(&endpoint_formatters, i, next) {
1298 AST_RWLIST_REMOVE_CURRENT(next);
1299 ast_module_unref(ast_module_info->self);
1303 AST_RWLIST_TRAVERSE_SAFE_END;
1306 int ast_sip_format_endpoint_ami(struct ast_sip_endpoint *endpoint,
1307 struct ast_sip_ami *ami, int *count)
1310 struct ast_sip_endpoint_formatter *i;
1311 SCOPED_LOCK(lock, &endpoint_formatters, AST_RWLIST_RDLOCK, AST_RWLIST_UNLOCK);
1313 AST_RWLIST_TRAVERSE(&endpoint_formatters, i, next) {
1314 if (i->format_ami && ((res = i->format_ami(endpoint, ami)) < 0)) {
1325 pjsip_endpoint *ast_sip_get_pjsip_endpoint(void)
1327 return ast_pjsip_endpoint;
1330 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)
1332 pj_str_t tmp, local_addr;
1334 pjsip_sip_uri *sip_uri;
1335 pjsip_transport_type_e type = PJSIP_TRANSPORT_UNSPECIFIED;
1337 char uuid_str[AST_UUID_STR_LEN];
1339 if (ast_strlen_zero(user)) {
1340 RAII_VAR(struct ast_uuid *, uuid, ast_uuid_generate(), ast_free_ptr);
1344 user = ast_uuid_to_str(uuid, uuid_str, sizeof(uuid_str));
1347 /* Parse the provided target URI so we can determine what transport it will end up using */
1348 pj_strdup_with_null(pool, &tmp, target);
1350 if (!(uri = pjsip_parse_uri(pool, tmp.ptr, tmp.slen, 0)) ||
1351 (!PJSIP_URI_SCHEME_IS_SIP(uri) && !PJSIP_URI_SCHEME_IS_SIPS(uri))) {
1355 sip_uri = pjsip_uri_get_uri(uri);
1357 /* Determine the transport type to use */
1358 if (PJSIP_URI_SCHEME_IS_SIPS(sip_uri)) {
1359 type = PJSIP_TRANSPORT_TLS;
1360 } else if (!sip_uri->transport_param.slen) {
1361 type = PJSIP_TRANSPORT_UDP;
1363 type = pjsip_transport_get_type_from_name(&sip_uri->transport_param);
1366 if (type == PJSIP_TRANSPORT_UNSPECIFIED) {
1370 /* If the host is IPv6 turn the transport into an IPv6 version */
1371 if (pj_strchr(&sip_uri->host, ':') && type < PJSIP_TRANSPORT_START_OTHER) {
1372 type = (pjsip_transport_type_e)(((int)type) + PJSIP_TRANSPORT_IPV6);
1375 if (!ast_strlen_zero(domain)) {
1376 from->ptr = pj_pool_alloc(pool, PJSIP_MAX_URL_SIZE);
1377 from->slen = pj_ansi_snprintf(from->ptr, PJSIP_MAX_URL_SIZE,
1379 (pjsip_transport_get_flag_from_type(type) & PJSIP_TRANSPORT_SECURE) ? "sips" : "sip",
1382 (type != PJSIP_TRANSPORT_UDP && type != PJSIP_TRANSPORT_UDP6) ? ";transport=" : "",
1383 (type != PJSIP_TRANSPORT_UDP && type != PJSIP_TRANSPORT_UDP6) ? pjsip_transport_get_type_name(type) : "");
1387 /* Get the local bound address for the transport that will be used when communicating with the provided URI */
1388 if (pjsip_tpmgr_find_local_addr(pjsip_endpt_get_tpmgr(ast_sip_get_pjsip_endpoint()), pool, type, selector,
1389 &local_addr, &local_port) != PJ_SUCCESS) {
1391 /* If no local address can be retrieved using the transport manager use the host one */
1392 pj_strdup(pool, &local_addr, pj_gethostname());
1393 local_port = pjsip_transport_get_default_port_for_type(PJSIP_TRANSPORT_UDP);
1396 /* If IPv6 was specified in the transport, set the proper type */
1397 if (pj_strchr(&local_addr, ':') && type < PJSIP_TRANSPORT_START_OTHER) {
1398 type = (pjsip_transport_type_e)(((int)type) + PJSIP_TRANSPORT_IPV6);
1401 from->ptr = pj_pool_alloc(pool, PJSIP_MAX_URL_SIZE);
1402 from->slen = pj_ansi_snprintf(from->ptr, PJSIP_MAX_URL_SIZE,
1403 "<%s:%s@%s%.*s%s:%d%s%s>",
1404 (pjsip_transport_get_flag_from_type(type) & PJSIP_TRANSPORT_SECURE) ? "sips" : "sip",
1406 (type & PJSIP_TRANSPORT_IPV6) ? "[" : "",
1407 (int)local_addr.slen,
1409 (type & PJSIP_TRANSPORT_IPV6) ? "]" : "",
1411 (type != PJSIP_TRANSPORT_UDP && type != PJSIP_TRANSPORT_UDP6) ? ";transport=" : "",
1412 (type != PJSIP_TRANSPORT_UDP && type != PJSIP_TRANSPORT_UDP6) ? pjsip_transport_get_type_name(type) : "");
1417 static int sip_get_tpselector_from_endpoint(const struct ast_sip_endpoint *endpoint, pjsip_tpselector *selector)
1419 RAII_VAR(struct ast_sip_transport *, transport, NULL, ao2_cleanup);
1420 const char *transport_name = endpoint->transport;
1422 if (ast_strlen_zero(transport_name)) {
1426 transport = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "transport", transport_name);
1428 if (!transport || !transport->state) {
1429 ast_log(LOG_ERROR, "Unable to retrieve PJSIP transport '%s' for endpoint '%s'\n",
1430 transport_name, ast_sorcery_object_get_id(endpoint));
1434 if (transport->state->transport) {
1435 selector->type = PJSIP_TPSELECTOR_TRANSPORT;
1436 selector->u.transport = transport->state->transport;
1437 } else if (transport->state->factory) {
1438 selector->type = PJSIP_TPSELECTOR_LISTENER;
1439 selector->u.listener = transport->state->factory;
1447 pjsip_dialog *ast_sip_create_dialog_uac(const struct ast_sip_endpoint *endpoint, const char *uri, const char *request_user)
1449 char enclosed_uri[PJSIP_MAX_URL_SIZE];
1450 pj_str_t local_uri = { "sip:temp@temp", 13 }, remote_uri, target_uri;
1451 pjsip_dialog *dlg = NULL;
1452 const char *outbound_proxy = endpoint->outbound_proxy;
1453 pjsip_tpselector selector = { .type = PJSIP_TPSELECTOR_NONE, };
1454 static const pj_str_t HCONTACT = { "Contact", 7 };
1456 snprintf(enclosed_uri, sizeof(enclosed_uri), "<%s>", uri);
1457 pj_cstr(&remote_uri, enclosed_uri);
1459 pj_cstr(&target_uri, uri);
1461 if (pjsip_dlg_create_uac(pjsip_ua_instance(), &local_uri, NULL, &remote_uri, &target_uri, &dlg) != PJ_SUCCESS) {
1465 if (sip_get_tpselector_from_endpoint(endpoint, &selector)) {
1466 pjsip_dlg_terminate(dlg);
1470 if (sip_dialog_create_from(dlg->pool, &local_uri, endpoint->fromuser, endpoint->fromdomain, &remote_uri, &selector)) {
1471 pjsip_dlg_terminate(dlg);
1475 /* Update the dialog with the new local URI, we do it afterwards so we can use the dialog pool for construction */
1476 pj_strdup_with_null(dlg->pool, &dlg->local.info_str, &local_uri);
1477 dlg->local.info->uri = pjsip_parse_uri(dlg->pool, dlg->local.info_str.ptr, dlg->local.info_str.slen, 0);
1478 dlg->local.contact = pjsip_parse_hdr(dlg->pool, &HCONTACT, local_uri.ptr, local_uri.slen, NULL);
1480 /* If a request user has been specified and we are permitted to change it, do so */
1481 if (!ast_strlen_zero(request_user)) {
1482 pjsip_sip_uri *sip_uri;
1484 if (PJSIP_URI_SCHEME_IS_SIP(dlg->target) || PJSIP_URI_SCHEME_IS_SIPS(dlg->target)) {
1485 sip_uri = pjsip_uri_get_uri(dlg->target);
1486 pj_strdup2(dlg->pool, &sip_uri->user, request_user);
1488 if (PJSIP_URI_SCHEME_IS_SIP(dlg->remote.info->uri) || PJSIP_URI_SCHEME_IS_SIPS(dlg->remote.info->uri)) {
1489 sip_uri = pjsip_uri_get_uri(dlg->remote.info->uri);
1490 pj_strdup2(dlg->pool, &sip_uri->user, request_user);
1494 /* We have to temporarily bump up the sess_count here so the dialog is not prematurely destroyed */
1497 pjsip_dlg_set_transport(dlg, &selector);
1499 if (!ast_strlen_zero(outbound_proxy)) {
1500 pjsip_route_hdr route_set, *route;
1501 static const pj_str_t ROUTE_HNAME = { "Route", 5 };
1504 pj_list_init(&route_set);
1506 pj_strdup2_with_null(dlg->pool, &tmp, outbound_proxy);
1507 if (!(route = pjsip_parse_hdr(dlg->pool, &ROUTE_HNAME, tmp.ptr, tmp.slen, NULL))) {
1509 pjsip_dlg_terminate(dlg);
1512 pj_list_push_back(&route_set, route);
1514 pjsip_dlg_set_route_set(dlg, &route_set);
1522 pjsip_dialog *ast_sip_create_dialog_uas(const struct ast_sip_endpoint *endpoint, pjsip_rx_data *rdata)
1526 pjsip_transport_type_e type = rdata->tp_info.transport->key.type;
1529 contact.ptr = pj_pool_alloc(rdata->tp_info.pool, PJSIP_MAX_URL_SIZE);
1530 contact.slen = pj_ansi_snprintf(contact.ptr, PJSIP_MAX_URL_SIZE,
1531 "<%s:%s%.*s%s:%d%s%s>",
1532 (pjsip_transport_get_flag_from_type(type) & PJSIP_TRANSPORT_SECURE) ? "sips" : "sip",
1533 (type & PJSIP_TRANSPORT_IPV6) ? "[" : "",
1534 (int)rdata->tp_info.transport->local_name.host.slen,
1535 rdata->tp_info.transport->local_name.host.ptr,
1536 (type & PJSIP_TRANSPORT_IPV6) ? "]" : "",
1537 rdata->tp_info.transport->local_name.port,
1538 (type != PJSIP_TRANSPORT_UDP && type != PJSIP_TRANSPORT_UDP6) ? ";transport=" : "",
1539 (type != PJSIP_TRANSPORT_UDP && type != PJSIP_TRANSPORT_UDP6) ? pjsip_transport_get_type_name(type) : "");
1541 status = pjsip_dlg_create_uas(pjsip_ua_instance(), rdata, &contact, &dlg);
1542 if (status != PJ_SUCCESS) {
1543 char err[PJ_ERR_MSG_SIZE];
1545 pj_strerror(status, err, sizeof(err));
1546 ast_log(LOG_ERROR, "Could not create dialog with endpoint %s. %s\n",
1547 ast_sorcery_object_get_id(endpoint), err);
1554 /* PJSIP doesn't know about the INFO method, so we have to define it ourselves */
1555 static const pjsip_method info_method = {PJSIP_OTHER_METHOD, {"INFO", 4} };
1556 static const pjsip_method message_method = {PJSIP_OTHER_METHOD, {"MESSAGE", 7} };
1560 const pjsip_method *pmethod;
1562 { "INVITE", &pjsip_invite_method },
1563 { "CANCEL", &pjsip_cancel_method },
1564 { "ACK", &pjsip_ack_method },
1565 { "BYE", &pjsip_bye_method },
1566 { "REGISTER", &pjsip_register_method },
1567 { "OPTIONS", &pjsip_options_method },
1568 { "SUBSCRIBE", &pjsip_subscribe_method },
1569 { "NOTIFY", &pjsip_notify_method },
1570 { "PUBLISH", &pjsip_publish_method },
1571 { "INFO", &info_method },
1572 { "MESSAGE", &message_method },
1575 static const pjsip_method *get_pjsip_method(const char *method)
1578 for (i = 0; i < ARRAY_LEN(methods); ++i) {
1579 if (!strcmp(method, methods[i].method)) {
1580 return methods[i].pmethod;
1586 static int create_in_dialog_request(const pjsip_method *method, struct pjsip_dialog *dlg, pjsip_tx_data **tdata)
1588 if (pjsip_dlg_create_request(dlg, method, -1, tdata) != PJ_SUCCESS) {
1589 ast_log(LOG_WARNING, "Unable to create in-dialog request.\n");
1596 static int create_out_of_dialog_request(const pjsip_method *method, struct ast_sip_endpoint *endpoint,
1597 const char *uri, pjsip_tx_data **tdata)
1599 RAII_VAR(struct ast_sip_contact *, contact, NULL, ao2_cleanup);
1600 pj_str_t remote_uri;
1603 pjsip_tpselector selector = { .type = PJSIP_TPSELECTOR_NONE, };
1605 if (ast_strlen_zero(uri)) {
1607 ast_log(LOG_ERROR, "An endpoint and/or uri must be specified\n");
1611 contact = ast_sip_location_retrieve_contact_from_aor_list(endpoint->aors);
1612 if (!contact || ast_strlen_zero(contact->uri)) {
1613 ast_log(LOG_ERROR, "Unable to retrieve contact for endpoint %s\n",
1614 ast_sorcery_object_get_id(endpoint));
1618 pj_cstr(&remote_uri, contact->uri);
1620 pj_cstr(&remote_uri, uri);
1624 if (sip_get_tpselector_from_endpoint(endpoint, &selector)) {
1625 ast_log(LOG_ERROR, "Unable to retrieve PJSIP transport selector for endpoint %s\n",
1626 ast_sorcery_object_get_id(endpoint));
1631 pool = pjsip_endpt_create_pool(ast_sip_get_pjsip_endpoint(), "Outbound request", 256, 256);
1634 ast_log(LOG_ERROR, "Unable to create PJLIB memory pool\n");
1638 if (sip_dialog_create_from(pool, &from, endpoint ? endpoint->fromuser : NULL,
1639 endpoint ? endpoint->fromdomain : NULL, &remote_uri, &selector)) {
1640 ast_log(LOG_ERROR, "Unable to create From header for %.*s request to endpoint %s\n",
1641 (int) pj_strlen(&method->name), pj_strbuf(&method->name), ast_sorcery_object_get_id(endpoint));
1642 pjsip_endpt_release_pool(ast_sip_get_pjsip_endpoint(), pool);
1646 if (pjsip_endpt_create_request(ast_sip_get_pjsip_endpoint(), method, &remote_uri,
1647 &from, &remote_uri, &from, NULL, -1, NULL, tdata) != PJ_SUCCESS) {
1648 ast_log(LOG_ERROR, "Unable to create outbound %.*s request to endpoint %s\n",
1649 (int) pj_strlen(&method->name), pj_strbuf(&method->name), ast_sorcery_object_get_id(endpoint));
1650 pjsip_endpt_release_pool(ast_sip_get_pjsip_endpoint(), pool);
1654 /* If an outbound proxy is specified on the endpoint apply it to this request */
1655 if (endpoint && !ast_strlen_zero(endpoint->outbound_proxy) &&
1656 ast_sip_set_outbound_proxy((*tdata), endpoint->outbound_proxy)) {
1657 ast_log(LOG_ERROR, "Unable to apply outbound proxy on request %.*s to endpoint %s\n",
1658 (int) pj_strlen(&method->name), pj_strbuf(&method->name), ast_sorcery_object_get_id(endpoint));
1659 pjsip_endpt_release_pool(ast_sip_get_pjsip_endpoint(), pool);
1663 /* We can release this pool since request creation copied all the necessary
1664 * data into the outbound request's pool
1666 pjsip_endpt_release_pool(ast_sip_get_pjsip_endpoint(), pool);
1670 int ast_sip_create_request(const char *method, struct pjsip_dialog *dlg,
1671 struct ast_sip_endpoint *endpoint, const char *uri,
1672 pjsip_tx_data **tdata)
1674 const pjsip_method *pmethod = get_pjsip_method(method);
1677 ast_log(LOG_WARNING, "Unknown method '%s'. Cannot send request\n", method);
1682 return create_in_dialog_request(pmethod, dlg, tdata);
1684 return create_out_of_dialog_request(pmethod, endpoint, uri, tdata);
1688 static int send_in_dialog_request(pjsip_tx_data *tdata, struct pjsip_dialog *dlg)
1690 if (pjsip_dlg_send_request(dlg, tdata, -1, NULL) != PJ_SUCCESS) {
1691 ast_log(LOG_WARNING, "Unable to send in-dialog request.\n");
1697 static void send_request_cb(void *token, pjsip_event *e)
1699 RAII_VAR(struct ast_sip_endpoint *, endpoint, token, ao2_cleanup);
1700 pjsip_transaction *tsx = e->body.tsx_state.tsx;
1701 pjsip_rx_data *challenge = e->body.tsx_state.src.rdata;
1702 pjsip_tx_data *tdata;
1704 if (tsx->status_code != 401 && tsx->status_code != 407) {
1708 if (!ast_sip_create_request_with_auth(&endpoint->outbound_auths, challenge, tsx, &tdata)) {
1709 pjsip_endpt_send_request(ast_sip_get_pjsip_endpoint(), tdata, -1, NULL, NULL);
1713 static int send_out_of_dialog_request(pjsip_tx_data *tdata, struct ast_sip_endpoint *endpoint)
1715 ao2_ref(endpoint, +1);
1716 if (pjsip_endpt_send_request(ast_sip_get_pjsip_endpoint(), tdata, -1, endpoint, send_request_cb) != PJ_SUCCESS) {
1717 ast_log(LOG_ERROR, "Error attempting to send outbound %.*s request to endpoint %s\n",
1718 (int) pj_strlen(&tdata->msg->line.req.method.name),
1719 pj_strbuf(&tdata->msg->line.req.method.name),
1720 ast_sorcery_object_get_id(endpoint));
1721 ao2_ref(endpoint, -1);
1728 int ast_sip_send_request(pjsip_tx_data *tdata, struct pjsip_dialog *dlg, struct ast_sip_endpoint *endpoint)
1730 ast_assert(tdata->msg->type == PJSIP_REQUEST_MSG);
1733 return send_in_dialog_request(tdata, dlg);
1735 return send_out_of_dialog_request(tdata, endpoint);
1739 int ast_sip_set_outbound_proxy(pjsip_tx_data *tdata, const char *proxy)
1741 pjsip_route_hdr *route;
1742 static const pj_str_t ROUTE_HNAME = { "Route", 5 };
1745 pj_strdup2_with_null(tdata->pool, &tmp, proxy);
1746 if (!(route = pjsip_parse_hdr(tdata->pool, &ROUTE_HNAME, tmp.ptr, tmp.slen, NULL))) {
1750 pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)route);
1755 int ast_sip_add_header(pjsip_tx_data *tdata, const char *name, const char *value)
1759 pjsip_generic_string_hdr *hdr;
1761 pj_cstr(&hdr_name, name);
1762 pj_cstr(&hdr_value, value);
1764 hdr = pjsip_generic_string_hdr_create(tdata->pool, &hdr_name, &hdr_value);
1766 pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr *) hdr);
1770 static pjsip_msg_body *ast_body_to_pjsip_body(pj_pool_t *pool, const struct ast_sip_body *body)
1776 pj_cstr(&type, body->type);
1777 pj_cstr(&subtype, body->subtype);
1778 pj_cstr(&body_text, body->body_text);
1780 return pjsip_msg_body_create(pool, &type, &subtype, &body_text);
1783 int ast_sip_add_body(pjsip_tx_data *tdata, const struct ast_sip_body *body)
1785 pjsip_msg_body *pjsip_body = ast_body_to_pjsip_body(tdata->pool, body);
1786 tdata->msg->body = pjsip_body;
1790 int ast_sip_add_body_multipart(pjsip_tx_data *tdata, const struct ast_sip_body *bodies[], int num_bodies)
1793 /* NULL for type and subtype automatically creates "multipart/mixed" */
1794 pjsip_msg_body *body = pjsip_multipart_create(tdata->pool, NULL, NULL);
1796 for (i = 0; i < num_bodies; ++i) {
1797 pjsip_multipart_part *part = pjsip_multipart_create_part(tdata->pool);
1798 part->body = ast_body_to_pjsip_body(tdata->pool, bodies[i]);
1799 pjsip_multipart_add_part(tdata->pool, body, part);
1802 tdata->msg->body = body;
1806 int ast_sip_append_body(pjsip_tx_data *tdata, const char *body_text)
1808 size_t combined_size = strlen(body_text) + tdata->msg->body->len;
1809 struct ast_str *body_buffer = ast_str_alloca(combined_size);
1811 ast_str_set(&body_buffer, 0, "%.*s%s", (int) tdata->msg->body->len, (char *) tdata->msg->body->data, body_text);
1813 tdata->msg->body->data = pj_pool_alloc(tdata->pool, combined_size);
1814 pj_memcpy(tdata->msg->body->data, ast_str_buffer(body_buffer), combined_size);
1815 tdata->msg->body->len = combined_size;
1820 struct ast_taskprocessor *ast_sip_create_serializer(void)
1822 struct ast_taskprocessor *serializer;
1823 RAII_VAR(struct ast_uuid *, uuid, ast_uuid_generate(), ast_free_ptr);
1824 char name[AST_UUID_STR_LEN];
1830 ast_uuid_to_str(uuid, name, sizeof(name));
1832 serializer = ast_threadpool_serializer(name, sip_threadpool);
1839 int ast_sip_push_task(struct ast_taskprocessor *serializer, int (*sip_task)(void *), void *task_data)
1842 return ast_taskprocessor_push(serializer, sip_task, task_data);
1844 return ast_threadpool_push(sip_threadpool, sip_task, task_data);
1848 struct sync_task_data {
1853 int (*task)(void *);
1857 static int sync_task(void *data)
1859 struct sync_task_data *std = data;
1860 std->fail = std->task(std->task_data);
1862 ast_mutex_lock(&std->lock);
1864 ast_cond_signal(&std->cond);
1865 ast_mutex_unlock(&std->lock);
1869 int ast_sip_push_task_synchronous(struct ast_taskprocessor *serializer, int (*sip_task)(void *), void *task_data)
1871 /* This method is an onion */
1872 struct sync_task_data std;
1874 if (ast_sip_thread_is_servant()) {
1875 return sip_task(task_data);
1878 ast_mutex_init(&std.lock);
1879 ast_cond_init(&std.cond, NULL);
1880 std.fail = std.complete = 0;
1881 std.task = sip_task;
1882 std.task_data = task_data;
1885 if (ast_taskprocessor_push(serializer, sync_task, &std)) {
1889 if (ast_threadpool_push(sip_threadpool, sync_task, &std)) {
1894 ast_mutex_lock(&std.lock);
1895 while (!std.complete) {
1896 ast_cond_wait(&std.cond, &std.lock);
1898 ast_mutex_unlock(&std.lock);
1900 ast_mutex_destroy(&std.lock);
1901 ast_cond_destroy(&std.cond);
1905 void ast_copy_pj_str(char *dest, const pj_str_t *src, size_t size)
1907 size_t chars_to_copy = MIN(size - 1, pj_strlen(src));
1908 memcpy(dest, pj_strbuf(src), chars_to_copy);
1909 dest[chars_to_copy] = '\0';
1912 int ast_sip_is_content_type(pjsip_media_type *content_type, char *type, char *subtype)
1914 pjsip_media_type compare;
1916 if (!content_type) {
1920 pjsip_media_type_init2(&compare, type, subtype);
1922 return pjsip_media_type_cmp(content_type, &compare, 0) ? 0 : -1;
1925 pj_caching_pool caching_pool;
1926 pj_pool_t *memory_pool;
1927 pj_thread_t *monitor_thread;
1928 static int monitor_continue;
1930 static void *monitor_thread_exec(void *endpt)
1932 while (monitor_continue) {
1933 const pj_time_val delay = {0, 10};
1934 pjsip_endpt_handle_events(ast_pjsip_endpoint, &delay);
1939 static void stop_monitor_thread(void)
1941 monitor_continue = 0;
1942 pj_thread_join(monitor_thread);
1945 AST_THREADSTORAGE(pj_thread_storage);
1946 AST_THREADSTORAGE(servant_id_storage);
1947 #define SIP_SERVANT_ID 0x5E2F1D
1949 static void sip_thread_start(void)
1951 pj_thread_desc *desc;
1952 pj_thread_t *thread;
1953 uint32_t *servant_id;
1955 servant_id = ast_threadstorage_get(&servant_id_storage, sizeof(*servant_id));
1957 ast_log(LOG_ERROR, "Could not set SIP servant ID in thread-local storage.\n");
1960 *servant_id = SIP_SERVANT_ID;
1962 desc = ast_threadstorage_get(&pj_thread_storage, sizeof(pj_thread_desc));
1964 ast_log(LOG_ERROR, "Could not get thread desc from thread-local storage. Expect awful things to occur\n");
1967 pj_bzero(*desc, sizeof(*desc));
1969 if (pj_thread_register("Asterisk Thread", *desc, &thread) != PJ_SUCCESS) {
1970 ast_log(LOG_ERROR, "Couldn't register thread with PJLIB.\n");
1974 int ast_sip_thread_is_servant(void)
1976 uint32_t *servant_id;
1978 servant_id = ast_threadstorage_get(&servant_id_storage, sizeof(*servant_id));
1983 return *servant_id == SIP_SERVANT_ID;
1986 void *ast_sip_dict_get(void *ht, const char *key)
1988 unsigned int hval = 0;
1994 return pj_hash_get(ht, key, PJ_HASH_KEY_STRING, &hval);
1997 void *ast_sip_dict_set(pj_pool_t* pool, void *ht,
1998 const char *key, void *val)
2001 ht = pj_hash_create(pool, 11);
2004 pj_hash_set(pool, ht, key, PJ_HASH_KEY_STRING, 0, val);
2009 static void remove_request_headers(pjsip_endpoint *endpt)
2011 const pjsip_hdr *request_headers = pjsip_endpt_get_request_headers(endpt);
2012 pjsip_hdr *iter = request_headers->next;
2014 while (iter != request_headers) {
2015 pjsip_hdr *to_erase = iter;
2017 pj_list_erase(to_erase);
2021 static int load_module(void)
2023 /* The third parameter is just copied from
2024 * example code from PJLIB. This can be adjusted
2028 struct ast_threadpool_options options;
2030 if (pj_init() != PJ_SUCCESS) {
2031 return AST_MODULE_LOAD_DECLINE;
2034 if (pjlib_util_init() != PJ_SUCCESS) {
2036 return AST_MODULE_LOAD_DECLINE;
2039 pj_caching_pool_init(&caching_pool, NULL, 1024 * 1024);
2040 if (pjsip_endpt_create(&caching_pool.factory, "SIP", &ast_pjsip_endpoint) != PJ_SUCCESS) {
2041 ast_log(LOG_ERROR, "Failed to create PJSIP endpoint structure. Aborting load\n");
2042 pj_caching_pool_destroy(&caching_pool);
2043 return AST_MODULE_LOAD_DECLINE;
2046 /* PJSIP will automatically try to add a Max-Forwards header. Since we want to control that,
2047 * we need to stop PJSIP from doing it automatically
2049 remove_request_headers(ast_pjsip_endpoint);
2051 memory_pool = pj_pool_create(&caching_pool.factory, "SIP", 1024, 1024, NULL);
2053 ast_log(LOG_ERROR, "Failed to create memory pool for SIP. Aborting load\n");
2054 pjsip_endpt_destroy(ast_pjsip_endpoint);
2055 ast_pjsip_endpoint = NULL;
2056 pj_caching_pool_destroy(&caching_pool);
2057 return AST_MODULE_LOAD_DECLINE;
2060 if (ast_sip_initialize_system()) {
2061 ast_log(LOG_ERROR, "Failed to initialize SIP 'system' configuration section. Aborting load\n");
2062 pj_pool_release(memory_pool);
2064 pjsip_endpt_destroy(ast_pjsip_endpoint);
2065 ast_pjsip_endpoint = NULL;
2066 pj_caching_pool_destroy(&caching_pool);
2067 return AST_MODULE_LOAD_DECLINE;
2070 sip_get_threadpool_options(&options);
2071 options.thread_start = sip_thread_start;
2072 sip_threadpool = ast_threadpool_create("SIP", NULL, &options);
2073 if (!sip_threadpool) {
2074 ast_log(LOG_ERROR, "Failed to create SIP threadpool. Aborting load\n");
2075 pj_pool_release(memory_pool);
2077 pjsip_endpt_destroy(ast_pjsip_endpoint);
2078 ast_pjsip_endpoint = NULL;
2079 pj_caching_pool_destroy(&caching_pool);
2080 return AST_MODULE_LOAD_DECLINE;
2083 pjsip_tsx_layer_init_module(ast_pjsip_endpoint);
2084 pjsip_ua_init_module(ast_pjsip_endpoint, NULL);
2086 monitor_continue = 1;
2087 status = pj_thread_create(memory_pool, "SIP", (pj_thread_proc *) &monitor_thread_exec,
2088 NULL, PJ_THREAD_DEFAULT_STACK_SIZE * 2, 0, &monitor_thread);
2089 if (status != PJ_SUCCESS) {
2090 ast_log(LOG_ERROR, "Failed to start SIP monitor thread. Aborting load\n");
2091 pj_pool_release(memory_pool);
2093 pjsip_endpt_destroy(ast_pjsip_endpoint);
2094 ast_pjsip_endpoint = NULL;
2095 pj_caching_pool_destroy(&caching_pool);
2096 return AST_MODULE_LOAD_DECLINE;
2099 ast_sip_initialize_global_headers();
2101 if (ast_res_pjsip_initialize_configuration(ast_module_info)) {
2102 ast_log(LOG_ERROR, "Failed to initialize SIP configuration. Aborting load\n");
2103 ast_sip_destroy_global_headers();
2104 stop_monitor_thread();
2105 pj_pool_release(memory_pool);
2107 pjsip_endpt_destroy(ast_pjsip_endpoint);
2108 ast_pjsip_endpoint = NULL;
2109 pj_caching_pool_destroy(&caching_pool);
2110 return AST_MODULE_LOAD_DECLINE;
2113 if (ast_sip_initialize_distributor()) {
2114 ast_log(LOG_ERROR, "Failed to register distributor module. Aborting load\n");
2115 ast_res_pjsip_destroy_configuration();
2116 ast_sip_destroy_global_headers();
2117 stop_monitor_thread();
2118 pj_pool_release(memory_pool);
2120 pjsip_endpt_destroy(ast_pjsip_endpoint);
2121 ast_pjsip_endpoint = NULL;
2122 pj_caching_pool_destroy(&caching_pool);
2123 return AST_MODULE_LOAD_DECLINE;
2126 if (ast_sip_initialize_outbound_authentication()) {
2127 ast_log(LOG_ERROR, "Failed to initialize outbound authentication. Aborting load\n");
2128 ast_sip_destroy_distributor();
2129 ast_res_pjsip_destroy_configuration();
2130 ast_sip_destroy_global_headers();
2131 stop_monitor_thread();
2132 pj_pool_release(memory_pool);
2134 pjsip_endpt_destroy(ast_pjsip_endpoint);
2135 ast_pjsip_endpoint = NULL;
2136 pj_caching_pool_destroy(&caching_pool);
2137 return AST_MODULE_LOAD_DECLINE;
2140 ast_res_pjsip_init_options_handling(0);
2142 ast_module_ref(ast_module_info->self);
2144 return AST_MODULE_LOAD_SUCCESS;
2147 static int reload_module(void)
2149 if (ast_res_pjsip_reload_configuration()) {
2150 return AST_MODULE_LOAD_DECLINE;
2152 ast_res_pjsip_init_options_handling(1);
2156 static int unload_module(void)
2158 /* This will never get called as this module can't be unloaded */
2162 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER, "Basic SIP resource",
2163 .load = load_module,
2164 .unload = unload_module,
2165 .reload = reload_module,
2166 .load_pri = AST_MODPRI_CHANNEL_DEPEND - 5,