res_pjsip: Remove ephemeral registered contacts on transport shutdown.
[asterisk/asterisk.git] / res / res_pjsip.c
index 916c464..ca0c301 100644 (file)
                                <configOption name="allow">
                                        <synopsis>Media Codec(s) to allow</synopsis>
                                </configOption>
+                               <configOption name="allow_overlap" default="yes">
+                                       <synopsis>Enable RFC3578 overlap dialing support.</synopsis>
+                               </configOption>
                                <configOption name="aors">
                                        <synopsis>AoR(s) to be used with the endpoint</synopsis>
                                        <description><para>
                                                This is a comma-delimited list of <replaceable>auth</replaceable> sections defined
                                                in <filename>pjsip.conf</filename> to be used to verify inbound connection attempts.
                                                </para><para>
-                                               Endpoints without an <literal>authentication</literal> object
-                                               configured will allow connections without vertification.
-                                       </para></description>
+                                               Endpoints without an authentication object
+                                               configured will allow connections without verification.</para>
+                                               <note><para>
+                                               Using the same auth section for inbound and outbound
+                                               authentication is not recommended.  There is a difference in
+                                               meaning for an empty realm setting between inbound and outbound
+                                               authentication uses.  See the auth realm description for details.
+                                               </para></note>
+                                       </description>
                                </configOption>
                                <configOption name="callerid">
                                        <synopsis>CallerID information for the endpoint</synopsis>
                                        <description>
                                                <para>Method used when updating connected line information.</para>
                                                <enumlist>
-                                                       <enum name="invite" />
+                                                       <enum name="invite">
+                                                       <para>When set to <literal>invite</literal>, check the remote's Allow header and
+                                                       if UPDATE is allowed, send UPDATE instead of INVITE to avoid SDP
+                                                       renegotiation.  If UPDATE is not Allowed, send INVITE.</para>
+                                                       </enum>
                                                        <enum name="reinvite">
                                                                <para>Alias for the <literal>invite</literal> value.</para>
                                                        </enum>
-                                                       <enum name="update" />
+                                                       <enum name="update">
+                                                       <para>If set to <literal>update</literal>, send UPDATE regardless of what the remote
+                                                       Allows. </para>
+                                                       </enum>
                                                </enumlist>
                                        </description>
                                </configOption>
                                                        <enum name="auto">
                                                                <para>DTMF is sent as RFC 4733 if the other side supports it or as INBAND if not.</para>
                                                        </enum>
+                                                       <enum name="auto_info">
+                                                               <para>DTMF is sent as RFC 4733 if the other side supports it or as SIP INFO if not.</para>
+                                                       </enum>
                                                </enumlist>
                                        </description>
                                </configOption>
                                        <synopsis>Default Music On Hold class</synopsis>
                                </configOption>
                                <configOption name="outbound_auth">
-                                       <synopsis>Authentication object used for outbound requests</synopsis>
+                                       <synopsis>Authentication object(s) used for outbound requests</synopsis>
+                                       <description><para>
+                                               This is a comma-delimited list of <replaceable>auth</replaceable>
+                                               sections defined in <filename>pjsip.conf</filename> used to respond
+                                               to outbound connection authentication challenges.</para>
+                                               <note><para>
+                                               Using the same auth section for inbound and outbound
+                                               authentication is not recommended.  There is a difference in
+                                               meaning for an empty realm setting between inbound and outbound
+                                               authentication uses.  See the auth realm description for details.
+                                               </para></note>
+                                       </description>
                                </configOption>
                                <configOption name="outbound_proxy">
-                                       <synopsis>Proxy through which to send requests, a full SIP URI must be provided</synopsis>
+                                       <synopsis>Full SIP URI of the outbound proxy used to send requests</synopsis>
                                </configOption>
                                <configOption name="rewrite_contact">
                                        <synopsis>Allow Contact header to be rewritten with the source IP address-port</synopsis>
                                        <description><para>
-                                               On inbound SIP messages from this endpoint, the Contact header or an appropriate Record-Route
-                                               header will be changed to have the source IP address and port. This option does not affect
-                                               outbound messages sent to this endpoint.
+                                               On inbound SIP messages from this endpoint, the Contact header or an
+                                               appropriate Record-Route header will be changed to have the source IP
+                                               address and port.  This option does not affect outbound messages sent to
+                                               this endpoint.  This option helps servers communicate with endpoints
+                                               that are behind NATs.  This option also helps reuse reliable transport
+                                               connections such as TCP and TLS.
                                        </para></description>
                                </configOption>
                                <configOption name="rtp_ipv6" default="no">
                                                On outbound requests, force the user portion of the Contact header to this value.
                                        </para></description>
                                </configOption>
-                                <configOption name="asymmetric_rtp_codec" default="no">
-                                        <synopsis>Allow the sending and receiving RTP codec to differ</synopsis>
-                                        <description><para>
-                                                When set to "yes" the codec in use for sending will be allowed to differ from
-                                                that of the received one. PJSIP will not automatically switch the sending one
-                                                to the receiving one.
-                                        </para></description>
-                                </configOption>
+                               <configOption name="asymmetric_rtp_codec" default="no">
+                                       <synopsis>Allow the sending and receiving RTP codec to differ</synopsis>
+                                       <description><para>
+                                               When set to "yes" the codec in use for sending will be allowed to differ from
+                                               that of the received one. PJSIP will not automatically switch the sending one
+                                               to the receiving one.
+                                       </para></description>
+                               </configOption>
+                               <configOption name="rtcp_mux" default="no">
+                                       <synopsis>Enable RFC 5761 RTCP multiplexing on the RTP port</synopsis>
+                                       <description><para>
+                                               With this option enabled, Asterisk will attempt to negotiate the use of the "rtcp-mux"
+                                               attribute on all media streams. This will result in RTP and RTCP being sent and received
+                                               on the same port. This shifts the demultiplexing logic to the application rather than
+                                               the transport layer. This option is useful when interoperating with WebRTC endpoints
+                                               since they mandate this option's use.
+                                       </para></description>
+                               </configOption>
+                               <configOption name="refer_blind_progress" default="yes">
+                                       <synopsis>Whether to notifies all the progress details on blind transfer</synopsis>
+                                       <description><para>
+                                               Some SIP phones (Mitel/Aastra, Snom) expect a sip/frag "200 OK"
+                                               after REFER has been accepted. If set to <literal>no</literal> then asterisk
+                                               will not send the progress details, but immediately will send "200 OK".
+                                       </para></description>
+                               </configOption>
+                               <configOption name="notify_early_inuse_ringing" default="no">
+                                       <synopsis>Whether to notifies dialog-info 'early' on InUse&amp;Ringing state</synopsis>
+                                       <description><para>
+                                               Control whether dialog-info subscriptions get 'early' state
+                                               on Ringing when already INUSE.
+                                       </para></description>
+                               </configOption>
+                               <configOption name="max_audio_streams" default="1">
+                                       <synopsis>The maximum number of allowed audio streams for the endpoint</synopsis>
+                                       <description><para>
+                                               This option enforces a limit on the maximum simultaneous negotiated audio
+                                               streams allowed for the endpoint.
+                                       </para></description>
+                               </configOption>
+                               <configOption name="max_video_streams" default="1">
+                                       <synopsis>The maximum number of allowed video streams for the endpoint</synopsis>
+                                       <description><para>
+                                               This option enforces a limit on the maximum simultaneous negotiated video
+                                               streams allowed for the endpoint.
+                                       </para></description>
+                               </configOption>
+                               <configOption name="bundle" default="no">
+                                       <synopsis>Enable RTP bundling</synopsis>
+                                       <description><para>
+                                               With this option enabled, Asterisk will attempt to negotiate the use of bundle.
+                                               If negotiated this will result in multiple RTP streams being carried over the same
+                                               underlying transport. Note that enabling bundle will also enable the rtcp_mux option.
+                                       </para></description>
+                               </configOption>
+                               <configOption name="webrtc" default="no">
+                                       <synopsis>Defaults and enables some options that are relevant to WebRTC</synopsis>
+                                       <description><para>
+                                               When set to "yes" this also enables the following values that are needed in
+                                               order for basic WebRTC support to work: rtcp_mux, use_avpf, ice_support, and
+                                               use_received_transport. The following configuration settings also get defaulted
+                                               as follows:</para>
+                                               <para>media_encryption=dtls</para>
+                                               <para>dtls_verify=fingerprint</para>
+                                               <para>dtls_setup=actpass</para>
+                                       </description>
+                               </configOption>
                        </configObject>
                        <configObject name="auth">
                                <synopsis>Authentication type</synopsis>
                                        <synopsis>PlainText password used for authentication.</synopsis>
                                        <description><para>Only used when auth_type is <literal>userpass</literal>.</para></description>
                                </configOption>
-                               <configOption name="realm" default="asterisk">
+                               <configOption name="realm">
                                        <synopsis>SIP realm for endpoint</synopsis>
+                                       <description><para>
+                                               The treatment of this value depends upon how the authentication
+                                               object is used.
+                                               </para><para>
+                                               When used as an inbound authentication object, the realm is sent
+                                               as part of the challenge so the peer can know which key to use
+                                               when responding.  An empty value will use the
+                                               <replaceable>global</replaceable> section's
+                                               <literal>default_realm</literal> value when issuing a challenge.
+                                               </para><para>
+                                               When used as an outbound authentication object, the realm is
+                                               matched with the received challenge realm to determine which
+                                               authentication object to use when responding to the challenge.  An
+                                               empty value matches any challenging realm when determining
+                                               which authentication object matches a received challenge.
+                                               </para>
+                                               <note><para>
+                                               Using the same auth section for inbound and outbound
+                                               authentication is not recommended.  There is a difference in
+                                               meaning for an empty realm setting between inbound and outbound
+                                               authentication uses.</para></note>
+                                       </description>
                                </configOption>
                                <configOption name="type">
                                        <synopsis>Must be 'auth'</synopsis>
                                                in-progress calls.</para>
                                        </description>
                                </configOption>
+                               <configOption name="symmetric_transport" default="no">
+                                       <synopsis>Use the same transport for outgoing reqests as incoming ones.</synopsis>
+                                       <description>
+                                               <para>When a request from a dynamic contact
+                                                       comes in on a transport with this option set to 'yes',
+                                                       the transport name will be saved and used for subsequent
+                                                       outgoing requests like OPTIONS, NOTIFY and INVITE.  It's
+                                                       saved as a contact uri parameter named 'x-ast-txp' and will
+                                                       display with the contact uri in CLI, AMI, and ARI output.
+                                                       On the outgoing request, if a transport wasn't explicitly
+                                                       set on the endpoint AND the request URI is not a hostname,
+                                                       the saved transport will be used and the 'x-ast-txp'
+                                                       parameter stripped from the outgoing packet.
+                                               </para>
+                                       </description>
+                               </configOption>
                        </configObject>
                        <configObject name="contact">
                                <synopsis>A way of creating an aliased name to a SIP URI</synopsis>
                                                in incoming SIP REGISTER requests and is not intended to be configured manually.
                                        </para></description>
                                </configOption>
+                               <configOption name="prune_on_boot">
+                                       <synopsis>A contact that cannot survive a restart/boot.</synopsis>
+                                       <description><para>
+                                               The option is set if the incoming SIP REGISTER contact is rewritten
+                                               on a reliable transport and is not intended to be configured manually.
+                                       </para></description>
+                               </configOption>
                        </configObject>
                        <configObject name="aor">
                                <synopsis>The configuration for a location of an endpoint</synopsis>
                                                used.</synopsis>
                                </configOption>
                                <configOption name="default_realm" default="asterisk">
-                                       <synopsis>When Asterisk generates an challenge, the digest will be
+                                       <synopsis>When Asterisk generates a challenge, the digest realm will be
                                                set to this value if there is no better option (such as auth/realm) to be
                                                used.</synopsis>
                                </configOption>
                                <parameter name="SubscribeContext">
                                        <para><xi:include xpointer="xpointer(/docs/configInfo[@name='res_pjsip']/configFile[@name='pjsip.conf']/configObject[@name='endpoint']/configOption[@name='subscribe_context']/synopsis/node())"/></para>
                                </parameter>
+                               <parameter name="Allowoverlap">
+                                       <para><xi:include xpointer="xpointer(/docs/configInfo[@name='res_pjsip']/configFile[@name='pjsip.conf']/configObject[@name='endpoint']/configOption[@name='allow_overlap']/synopsis/node())"/></para>
+                               </parameter>
                        </syntax>
                </managerEventInstance>
        </managerEvent>
                                        <para>Absolute time that this contact is no longer valid after</para>
                                </parameter>
                                <parameter name="ViaAddress">
-                                       <para>IP address:port of the last Via header in REGISTER request</para>
+                                       <para>IP address:port of the last Via header in REGISTER request.
+                                       Will only appear in the event if available.</para>
                                </parameter>
                                <parameter name="CallID">
-                                       <para>Content of the Call-ID header in REGISTER request</para>
+                                       <para>Content of the Call-ID header in REGISTER request.
+                                       Will only appear in the event if available.</para>
+                               </parameter>
+                               <parameter name="ID">
+                                       <para>The sorcery ID of the contact.</para>
+                               </parameter>
+                               <parameter name="AuthenticateQualify">
+                                       <para>A boolean indicating whether a qualify should be authenticated.</para>
+                               </parameter>
+                               <parameter name="OutboundProxy">
+                                       <para>The contact's outbound proxy.</para>
+                               </parameter>
+                               <parameter name="Path">
+                                       <para>The Path header received on the REGISTER.</para>
+                               </parameter>
+                               <parameter name="QualifyFrequency">
+                                       <para>The interval in seconds at which the contact will be qualified.</para>
+                               </parameter>
+                               <parameter name="QualifyTimeout">
+                                       <para>The elapsed time in decimal seconds after which an OPTIONS
+                                       message is sent before the contact is considered unavailable.</para>
                                </parameter>
                        </syntax>
                </managerEventInstance>
@@ -2347,7 +2508,7 @@ enum ast_sip_check_auth_result ast_sip_check_authentication(struct ast_sip_endpo
 {
        if (!registered_authenticator) {
                ast_log(LOG_WARNING, "No SIP authenticator registered. Assuming authentication is successful\n");
-               return 0;
+               return AST_SIP_AUTHENTICATION_SUCCESS;
        }
        return registered_authenticator->check_authentication(endpoint, rdata, tdata);
 }
@@ -2692,12 +2853,59 @@ pjsip_endpoint *ast_sip_get_pjsip_endpoint(void)
        return ast_pjsip_endpoint;
 }
 
-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)
+int ast_sip_get_transport_name(const struct ast_sip_endpoint *endpoint,
+       pjsip_sip_uri *sip_uri, char *buf, size_t buf_len)
+{
+       char *host = NULL;
+       static const pj_str_t x_name = { AST_SIP_X_AST_TXP, AST_SIP_X_AST_TXP_LEN };
+       pjsip_param *x_transport;
+
+       if (!ast_strlen_zero(endpoint->transport)) {
+               ast_copy_string(buf, endpoint->transport, buf_len);
+               return 0;
+       }
+
+       x_transport = pjsip_param_find(&sip_uri->other_param, &x_name);
+       if (!x_transport) {
+               return -1;
+       }
+
+       /* Only use x_transport if the uri host is an ip (4 or 6) address */
+       host = ast_alloca(sip_uri->host.slen + 1);
+       ast_copy_pj_str(host, &sip_uri->host, sip_uri->host.slen + 1);
+       if (!ast_sockaddr_parse(NULL, host, PARSE_PORT_FORBID)) {
+               return -1;
+       }
+
+       ast_copy_pj_str(buf, &x_transport->value, buf_len);
+
+       return 0;
+}
+
+int ast_sip_dlg_set_transport(const struct ast_sip_endpoint *endpoint, pjsip_dialog *dlg,
+       pjsip_tpselector *selector)
+{
+       pjsip_sip_uri *uri;
+       pjsip_tpselector sel = { .type = PJSIP_TPSELECTOR_NONE, };
+
+       uri = pjsip_uri_get_uri(dlg->target);
+       if (!selector) {
+               selector = &sel;
+       }
+
+       ast_sip_set_tpselector_from_ep_or_uri(endpoint, uri, selector);
+       pjsip_dlg_set_transport(dlg, selector);
+
+       return 0;
+}
+
+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)
 {
        pj_str_t tmp, local_addr;
        pjsip_uri *uri;
        pjsip_sip_uri *sip_uri;
-       pjsip_transport_type_e type = PJSIP_TRANSPORT_UNSPECIFIED;
+       pjsip_transport_type_e type;
        int local_port;
        char default_user[PJSIP_MAX_URL_SIZE];
 
@@ -2717,21 +2925,21 @@ static int sip_dialog_create_from(pj_pool_t *pool, pj_str_t *from, const char *u
        sip_uri = pjsip_uri_get_uri(uri);
 
        /* Determine the transport type to use */
+       type = pjsip_transport_get_type_from_name(&sip_uri->transport_param);
        if (PJSIP_URI_SCHEME_IS_SIPS(sip_uri)) {
-               type = PJSIP_TRANSPORT_TLS;
+               if (type == PJSIP_TRANSPORT_UNSPECIFIED
+                       || !(pjsip_transport_get_flag_from_type(type) & PJSIP_TRANSPORT_SECURE)) {
+                       type = PJSIP_TRANSPORT_TLS;
+               }
        } else if (!sip_uri->transport_param.slen) {
                type = PJSIP_TRANSPORT_UDP;
-       } else {
-               type = pjsip_transport_get_type_from_name(&sip_uri->transport_param);
-       }
-
-       if (type == PJSIP_TRANSPORT_UNSPECIFIED) {
+       } else if (type == PJSIP_TRANSPORT_UNSPECIFIED) {
                return -1;
        }
 
        /* If the host is IPv6 turn the transport into an IPv6 version */
-       if (pj_strchr(&sip_uri->host, ':') && type < PJSIP_TRANSPORT_START_OTHER) {
-               type = (pjsip_transport_type_e)(((int)type) + PJSIP_TRANSPORT_IPV6);
+       if (pj_strchr(&sip_uri->host, ':')) {
+               type |= PJSIP_TRANSPORT_IPV6;
        }
 
        if (!ast_strlen_zero(domain)) {
@@ -2755,8 +2963,8 @@ static int sip_dialog_create_from(pj_pool_t *pool, pj_str_t *from, const char *u
        }
 
        /* If IPv6 was specified in the transport, set the proper type */
-       if (pj_strchr(&local_addr, ':') && type < PJSIP_TRANSPORT_START_OTHER) {
-               type = (pjsip_transport_type_e)(((int)type) + PJSIP_TRANSPORT_IPV6);
+       if (pj_strchr(&local_addr, ':')) {
+               type |= PJSIP_TRANSPORT_IPV6;
        }
 
        from->ptr = pj_pool_alloc(pool, PJSIP_MAX_URL_SIZE);
@@ -2822,15 +3030,16 @@ int ast_sip_set_tpselector_from_transport_name(const char *transport_name, pjsip
        return ast_sip_set_tpselector_from_transport(transport, selector);
 }
 
-static int sip_get_tpselector_from_endpoint(const struct ast_sip_endpoint *endpoint, pjsip_tpselector *selector)
+int ast_sip_set_tpselector_from_ep_or_uri(const struct ast_sip_endpoint *endpoint,
+       pjsip_sip_uri *sip_uri, pjsip_tpselector *selector)
 {
-       const char *transport_name = endpoint->transport;
+       char transport_name[128];
 
-       if (ast_strlen_zero(transport_name)) {
+       if (ast_sip_get_transport_name(endpoint, sip_uri, transport_name, sizeof(transport_name))) {
                return 0;
        }
 
-       return ast_sip_set_tpselector_from_transport_name(endpoint->transport, selector);
+       return ast_sip_set_tpselector_from_transport_name(transport_name, selector);
 }
 
 void ast_sip_add_usereqphone(const struct ast_sip_endpoint *endpoint, pj_pool_t *pool, pjsip_uri *uri)
@@ -2838,8 +3047,8 @@ void ast_sip_add_usereqphone(const struct ast_sip_endpoint *endpoint, pj_pool_t
        pjsip_sip_uri *sip_uri;
        int i = 0;
        pjsip_param *param;
-       const pj_str_t STR_USER = { "user", 4 };
-       const pj_str_t STR_PHONE = { "phone", 5 };
+       static const pj_str_t STR_USER = { "user", 4 };
+       static const pj_str_t STR_PHONE = { "phone", 5 };
 
        if (!endpoint || !endpoint->usereqphone || (!PJSIP_URI_SCHEME_IS_SIP(uri) && !PJSIP_URI_SCHEME_IS_SIPS(uri))) {
                return;
@@ -2872,7 +3081,8 @@ void ast_sip_add_usereqphone(const struct ast_sip_endpoint *endpoint, pj_pool_t
        pj_list_insert_before(&sip_uri->other_param, param);
 }
 
-pjsip_dialog *ast_sip_create_dialog_uac(const struct ast_sip_endpoint *endpoint, const char *uri, const char *request_user)
+pjsip_dialog *ast_sip_create_dialog_uac(const struct ast_sip_endpoint *endpoint,
+       const char *uri, const char *request_user)
 {
        char enclosed_uri[PJSIP_MAX_URL_SIZE];
        pj_str_t local_uri = { "sip:temp@temp", 13 }, remote_uri, target_uri;
@@ -2890,18 +3100,20 @@ pjsip_dialog *ast_sip_create_dialog_uac(const struct ast_sip_endpoint *endpoint,
        res = pjsip_dlg_create_uac(pjsip_ua_instance(), &local_uri, NULL, &remote_uri, &target_uri, &dlg);
        if (res != PJ_SUCCESS) {
                if (res == PJSIP_EINVALIDURI) {
-                       ast_log(LOG_ERROR, "Could not create dialog to endpoint '%s' as URI '%s' is not valid\n",
+                       ast_log(LOG_ERROR,
+                               "Endpoint '%s': Could not create dialog to invalid URI '%s'.  Is endpoint registered and reachable?\n",
                                ast_sorcery_object_get_id(endpoint), uri);
                }
                return NULL;
        }
 
-       if (sip_get_tpselector_from_endpoint(endpoint, &selector)) {
-               pjsip_dlg_terminate(dlg);
-               return NULL;
-       }
+       /* We have to temporarily bump up the sess_count here so the dialog is not prematurely destroyed */
+       dlg->sess_count++;
+
+       ast_sip_dlg_set_transport(endpoint, dlg, &selector);
 
        if (sip_dialog_create_from(dlg->pool, &local_uri, endpoint->fromuser, endpoint->fromdomain, &remote_uri, &selector)) {
+               dlg->sess_count--;
                pjsip_dlg_terminate(dlg);
                return NULL;
        }
@@ -2909,6 +3121,14 @@ pjsip_dialog *ast_sip_create_dialog_uac(const struct ast_sip_endpoint *endpoint,
        /* Update the dialog with the new local URI, we do it afterwards so we can use the dialog pool for construction */
        pj_strdup_with_null(dlg->pool, &dlg->local.info_str, &local_uri);
        dlg->local.info->uri = pjsip_parse_uri(dlg->pool, dlg->local.info_str.ptr, dlg->local.info_str.slen, 0);
+       if (!dlg->local.info->uri) {
+               ast_log(LOG_ERROR,
+                       "Could not parse URI '%s' for endpoint '%s'\n",
+                       dlg->local.info_str.ptr, ast_sorcery_object_get_id(endpoint));
+               dlg->sess_count--;
+               pjsip_dlg_terminate(dlg);
+               return NULL;
+       }
 
        dlg->local.contact = pjsip_parse_hdr(dlg->pool, &HCONTACT, local_uri.ptr, local_uri.slen, NULL);
 
@@ -2937,11 +3157,6 @@ pjsip_dialog *ast_sip_create_dialog_uac(const struct ast_sip_endpoint *endpoint,
        ast_sip_add_usereqphone(endpoint, dlg->pool, dlg->target);
        ast_sip_add_usereqphone(endpoint, dlg->pool, dlg->remote.info->uri);
 
-       /* We have to temporarily bump up the sess_count here so the dialog is not prematurely destroyed */
-       dlg->sess_count++;
-
-       pjsip_dlg_set_transport(dlg, &selector);
-
        if (!ast_strlen_zero(outbound_proxy)) {
                pjsip_route_hdr route_set, *route;
                static const pj_str_t ROUTE_HNAME = { "Route", 5 };
@@ -3010,10 +3225,13 @@ pjsip_dialog *ast_sip_create_dialog_uas(const struct ast_sip_endpoint *endpoint,
        pjsip_transport_type_e type = rdata->tp_info.transport->key.type;
        pjsip_tpselector selector = { .type = PJSIP_TPSELECTOR_NONE, };
        pjsip_transport *transport;
+       pjsip_contact_hdr *contact_hdr;
 
        ast_assert(status != NULL);
 
-       if (sip_get_tpselector_from_endpoint(endpoint, &selector)) {
+       contact_hdr = pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_CONTACT, NULL);
+       if (ast_sip_set_tpselector_from_ep_or_uri(endpoint, pjsip_uri_get_uri(contact_hdr->uri),
+               &selector)) {
                return NULL;
        }
 
@@ -3059,8 +3277,8 @@ pjsip_dialog *ast_sip_create_dialog_uas(const struct ast_sip_endpoint *endpoint,
        return dlg;
 }
 
-int ast_sip_create_rdata(pjsip_rx_data *rdata, char *packet, const char *src_name, int src_port,
-       char *transport_type, const char *local_name, int local_port)
+int ast_sip_create_rdata_with_contact(pjsip_rx_data *rdata, char *packet, const char *src_name, int src_port,
+       char *transport_type, const char *local_name, int local_port, const char *contact)
 {
        pj_str_t tmp;
 
@@ -3084,6 +3302,16 @@ int ast_sip_create_rdata(pjsip_rx_data *rdata, char *packet, const char *src_nam
                return -1;
        }
 
+       if (!ast_strlen_zero(contact)) {
+               pjsip_contact_hdr *contact_hdr;
+
+               contact_hdr = pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_CONTACT, NULL);
+               if (contact_hdr) {
+                       contact_hdr->uri = pjsip_parse_uri(rdata->tp_info.pool, (char *)contact,
+                               strlen(contact), PJSIP_PARSE_URI_AS_NAMEADDR);
+               }
+       }
+
        pj_strdup2(rdata->tp_info.pool, &rdata->msg_info.via->recvd_param, rdata->pkt_info.src_name);
        rdata->msg_info.via->rport_param = -1;
 
@@ -3095,6 +3323,13 @@ int ast_sip_create_rdata(pjsip_rx_data *rdata, char *packet, const char *src_nam
        return 0;
 }
 
+int ast_sip_create_rdata(pjsip_rx_data *rdata, char *packet, const char *src_name, int src_port,
+       char *transport_type, const char *local_name, int local_port)
+{
+       return ast_sip_create_rdata_with_contact(rdata, packet, src_name, src_port, transport_type,
+               local_name, local_port, NULL);
+}
+
 /* PJSIP doesn't know about the INFO method, so we have to define it ourselves */
 static const pjsip_method info_method = {PJSIP_OTHER_METHOD, {"INFO", 4} };
 static const pjsip_method message_method = {PJSIP_OTHER_METHOD, {"MESSAGE", 7} };
@@ -3176,14 +3411,6 @@ static int create_out_of_dialog_request(const pjsip_method *method, struct ast_s
                pj_cstr(&remote_uri, uri);
        }
 
-       if (endpoint) {
-               if (sip_get_tpselector_from_endpoint(endpoint, &selector)) {
-                       ast_log(LOG_ERROR, "Unable to retrieve PJSIP transport selector for endpoint %s\n",
-                               ast_sorcery_object_get_id(endpoint));
-                       return -1;
-               }
-       }
-
        pool = pjsip_endpt_create_pool(ast_sip_get_pjsip_endpoint(), "Outbound request", 256, 256);
 
        if (!pool) {
@@ -3201,6 +3428,8 @@ static int create_out_of_dialog_request(const pjsip_method *method, struct ast_s
                return -1;
        }
 
+       ast_sip_set_tpselector_from_ep_or_uri(endpoint, pjsip_uri_get_uri(sip_uri), &selector);
+
        fromuser = endpoint ? (!ast_strlen_zero(endpoint->fromuser) ? endpoint->fromuser : ast_sorcery_object_get_id(endpoint)) : NULL;
        if (sip_dialog_create_from(pool, &from, fromuser,
                                endpoint ? endpoint->fromdomain : NULL, &remote_uri, &selector)) {
@@ -3220,12 +3449,15 @@ static int create_out_of_dialog_request(const pjsip_method *method, struct ast_s
                return -1;
        }
 
+       pjsip_tx_data_set_transport(*tdata, &selector);
+
        if (endpoint && !ast_strlen_zero(endpoint->contact_user)){
                pjsip_contact_hdr *contact_hdr;
                pjsip_sip_uri *contact_uri;
                static const pj_str_t HCONTACT = { "Contact", 7 };
+               static const pj_str_t HCONTACTSHORT = { "m", 1 };
 
-               contact_hdr = pjsip_msg_find_hdr_by_name((*tdata)->msg, &HCONTACT, NULL);
+               contact_hdr = pjsip_msg_find_hdr_by_names((*tdata)->msg, &HCONTACT, &HCONTACTSHORT, NULL);
                if (contact_hdr) {
                        contact_uri = pjsip_uri_get_uri(contact_hdr->uri);
                        pj_strdup2(pool, &contact_uri->user, endpoint->contact_user);
@@ -3260,6 +3492,8 @@ int ast_sip_create_request(const char *method, struct pjsip_dialog *dlg,
 {
        const pjsip_method *pmethod = get_pjsip_method(method);
 
+       ast_assert(endpoint != NULL);
+
        if (!pmethod) {
                ast_log(LOG_WARNING, "Unknown method '%s'. Cannot send request\n", method);
                return -1;
@@ -3381,6 +3615,8 @@ struct send_request_wrapper {
        void (*callback)(void *token, pjsip_event *e);
        /*! Non-zero when the callback is called. */
        unsigned int cb_called;
+       /*! Non-zero if endpt_send_request_cb() was called. */
+       unsigned int send_cb_called;
        /*! Timeout timer. */
        pj_timer_entry *timeout_timer;
        /*! Original timeout. */
@@ -3396,6 +3632,13 @@ struct send_request_wrapper {
 static void endpt_send_request_cb(void *token, pjsip_event *e)
 {
        struct send_request_wrapper *req_wrapper = token;
+       unsigned int cb_called;
+
+       /*
+        * Needed because we cannot otherwise tell if this callback was
+        * called when pjsip_endpt_send_request() returns error.
+        */
+       req_wrapper->send_cb_called = 1;
 
        if (e->body.tsx_state.type == PJSIP_EVENT_TIMER) {
                ast_debug(2, "%p: PJSIP tsx timer expired\n", req_wrapper);
@@ -3425,7 +3668,6 @@ static void endpt_send_request_cb(void *token, pjsip_event *e)
                timers_cancelled = pj_timer_heap_cancel_if_active(
                        pjsip_endpt_get_timer_heap(ast_sip_get_pjsip_endpoint()),
                        req_wrapper->timeout_timer, TIMER_INACTIVE);
-
                if (timers_cancelled > 0) {
                        /* If the timer was cancelled the callback will never run so
                         * clean up its reference to the wrapper.
@@ -3433,25 +3675,27 @@ static void endpt_send_request_cb(void *token, pjsip_event *e)
                        ast_debug(3, "%p: Timer cancelled\n", req_wrapper);
                        ao2_ref(req_wrapper, -1);
                } else {
-                       /* If it wasn't cancelled, it MAY be in the callback already
-                        * waiting on the lock so set the id to INACTIVE so
-                        * when the callback comes out of the lock, it knows to not
-                        * proceed.
+                       /*
+                        * If it wasn't cancelled, it MAY be in the callback already
+                        * waiting on the lock.  When we release the lock, it will
+                        * now know not to proceed.
                         */
                        ast_debug(3, "%p: Timer already expired\n", req_wrapper);
-                       req_wrapper->timeout_timer->id = TIMER_INACTIVE;
                }
        }
 
+       cb_called = req_wrapper->cb_called;
+       req_wrapper->cb_called = 1;
+       ao2_unlock(req_wrapper);
+
        /* It's possible that our own timer expired and called the callbacks
         * so no need to call them again.
         */
-       if (!req_wrapper->cb_called && req_wrapper->callback) {
+       if (!cb_called && req_wrapper->callback) {
                req_wrapper->callback(req_wrapper->token, e);
-               req_wrapper->cb_called = 1;
                ast_debug(2, "%p: Callbacks executed\n", req_wrapper);
        }
-       ao2_unlock(req_wrapper);
+
        ao2_ref(req_wrapper, -1);
 }
 
@@ -3462,15 +3706,16 @@ static void endpt_send_request_cb(void *token, pjsip_event *e)
  */
 static void send_request_timer_callback(pj_timer_heap_t *theap, pj_timer_entry *entry)
 {
-       pjsip_event event;
        struct send_request_wrapper *req_wrapper = entry->user_data;
+       unsigned int cb_called;
 
        ast_debug(2, "%p: Internal tsx timer expired after %d msec\n",
                req_wrapper, req_wrapper->timeout);
 
        ao2_lock(req_wrapper);
-       /* If the id is not TIMEOUT_TIMER2 then the timer was cancelled above
-        * while the lock was being held so just clean up.
+       /*
+        * If the id is not TIMEOUT_TIMER2 then the timer was cancelled
+        * before we got the lock or it was already handled so just clean up.
         */
        if (entry->id != TIMEOUT_TIMER2) {
                ao2_unlock(req_wrapper);
@@ -3478,20 +3723,24 @@ static void send_request_timer_callback(pj_timer_heap_t *theap, pj_timer_entry *
                ao2_ref(req_wrapper, -1);
                return;
        }
+       entry->id = TIMER_INACTIVE;
 
        ast_debug(3, "%p: Timer handled here\n", req_wrapper);
 
-       PJSIP_EVENT_INIT_TX_MSG(event, req_wrapper->tdata);
-       event.body.tsx_state.type = PJSIP_EVENT_TIMER;
-       entry->id = TIMER_INACTIVE;
+       cb_called = req_wrapper->cb_called;
+       req_wrapper->cb_called = 1;
+       ao2_unlock(req_wrapper);
+
+       if (!cb_called && req_wrapper->callback) {
+               pjsip_event event;
+
+               PJSIP_EVENT_INIT_TX_MSG(event, req_wrapper->tdata);
+               event.body.tsx_state.type = PJSIP_EVENT_TIMER;
 
-       if (!req_wrapper->cb_called && req_wrapper->callback) {
                req_wrapper->callback(req_wrapper->token, &event);
-               req_wrapper->cb_called = 1;
                ast_debug(2, "%p: Callbacks executed\n", req_wrapper);
        }
 
-       ao2_unlock(req_wrapper);
        ao2_ref(req_wrapper, -1);
 }
 
@@ -3509,7 +3758,12 @@ static pj_status_t endpt_send_request(struct ast_sip_endpoint *endpoint,
        struct send_request_wrapper *req_wrapper;
        pj_status_t ret_val;
        pjsip_endpoint *endpt = ast_sip_get_pjsip_endpoint();
-       pjsip_tpselector selector = { .type = PJSIP_TPSELECTOR_NONE, };
+
+       if (!cb && token) {
+               /* Silly.  Without a callback we cannot do anything with token. */
+               pjsip_tx_data_dec_ref(tdata);
+               return PJ_EINVAL;
+       }
 
        /* Create wrapper to detect if the callback was actually called on an error. */
        req_wrapper = ao2_alloc(sizeof(*req_wrapper), send_request_wrapper_destructor);
@@ -3528,8 +3782,6 @@ static pj_status_t endpt_send_request(struct ast_sip_endpoint *endpoint,
        /* Add a reference to tdata.  The wrapper destructor cleans it up. */
        pjsip_tx_data_add_ref(tdata);
 
-       ao2_lock(req_wrapper);
-
        if (timeout > 0) {
                pj_time_val timeout_timer_val = { timeout / 1000, timeout % 1000 };
 
@@ -3540,9 +3792,6 @@ static pj_status_t endpt_send_request(struct ast_sip_endpoint *endpoint,
                pj_timer_entry_init(req_wrapper->timeout_timer, TIMEOUT_TIMER2,
                        req_wrapper, send_request_timer_callback);
 
-               pj_timer_heap_cancel_if_active(pjsip_endpt_get_timer_heap(endpt),
-                       req_wrapper->timeout_timer, TIMER_INACTIVE);
-
                /* We need to insure that the wrapper and tdata are available if/when the
                 * timer callback is executed.
                 */
@@ -3550,41 +3799,28 @@ static pj_status_t endpt_send_request(struct ast_sip_endpoint *endpoint,
                ret_val = pj_timer_heap_schedule(pjsip_endpt_get_timer_heap(endpt),
                        req_wrapper->timeout_timer, &timeout_timer_val);
                if (ret_val != PJ_SUCCESS) {
-                       ao2_unlock(req_wrapper);
                        ast_log(LOG_ERROR,
                                "Failed to set timer.  Not sending %.*s request to endpoint %s.\n",
                                (int) pj_strlen(&tdata->msg->line.req.method.name),
                                pj_strbuf(&tdata->msg->line.req.method.name),
                                endpoint ? ast_sorcery_object_get_id(endpoint) : "<unknown>");
                        ao2_t_ref(req_wrapper, -2, "Drop timer and routine ref");
+                       pjsip_tx_data_dec_ref(tdata);
                        return ret_val;
                }
-
-               req_wrapper->timeout_timer->id = TIMEOUT_TIMER2;
-       } else {
-               req_wrapper->timeout_timer = NULL;
        }
 
        /* We need to insure that the wrapper and tdata are available when the
         * transaction callback is executed.
         */
        ao2_ref(req_wrapper, +1);
-
-       if (endpoint) {
-               sip_get_tpselector_from_endpoint(endpoint, &selector);
-               pjsip_tx_data_set_transport(tdata, &selector);
-       }
-
        ret_val = pjsip_endpt_send_request(endpt, tdata, -1, req_wrapper, endpt_send_request_cb);
        if (ret_val != PJ_SUCCESS) {
                char errmsg[PJ_ERR_MSG_SIZE];
 
-               if (timeout > 0) {
-                       int timers_cancelled = pj_timer_heap_cancel_if_active(pjsip_endpt_get_timer_heap(endpt),
-                               req_wrapper->timeout_timer, TIMER_INACTIVE);
-                       if (timers_cancelled > 0) {
-                               ao2_ref(req_wrapper, -1);
-                       }
+               if (!req_wrapper->send_cb_called) {
+                       /* endpt_send_request_cb is not expected to ever be called now. */
+                       ao2_ref(req_wrapper, -1);
                }
 
                /* Complain of failure to send the request. */
@@ -3594,20 +3830,44 @@ static pj_status_t endpt_send_request(struct ast_sip_endpoint *endpoint,
                        pj_strbuf(&tdata->msg->line.req.method.name),
                        endpoint ? ast_sorcery_object_get_id(endpoint) : "<unknown>");
 
-               /* Was the callback called? */
-               if (req_wrapper->cb_called) {
+               if (timeout > 0) {
+                       int timers_cancelled;
+
+                       ao2_lock(req_wrapper);
+                       timers_cancelled = pj_timer_heap_cancel_if_active(
+                               pjsip_endpt_get_timer_heap(endpt),
+                               req_wrapper->timeout_timer, TIMER_INACTIVE);
+                       if (timers_cancelled > 0) {
+                               ao2_ref(req_wrapper, -1);
+                       }
+
+                       /* Was the callback called? */
+                       if (req_wrapper->cb_called) {
+                               /*
+                                * Yes so we cannot report any error.  The callback
+                                * has already freed any resources associated with
+                                * token.
+                                */
+                               ret_val = PJ_SUCCESS;
+                       } else {
+                               /*
+                                * No so we claim it is called so our caller can free
+                                * any resources associated with token because of
+                                * failure.
+                                */
+                               req_wrapper->cb_called = 1;
+                       }
+                       ao2_unlock(req_wrapper);
+               } else if (req_wrapper->cb_called) {
                        /*
-                        * Yes so we cannot report any error.  The callback
-                        * has already freed any resources associated with
+                        * We cannot report any error.  The callback has
+                        * already freed any resources associated with
                         * token.
                         */
                        ret_val = PJ_SUCCESS;
-               } else {
-                       /* No and it is not expected to ever be called. */
-                       ao2_ref(req_wrapper, -1);
                }
        }
-       ao2_unlock(req_wrapper);
+
        ao2_ref(req_wrapper, -1);
        return ret_val;
 }
@@ -4006,6 +4266,18 @@ void ast_copy_pj_str(char *dest, const pj_str_t *src, size_t size)
        dest[chars_to_copy] = '\0';
 }
 
+int ast_copy_pj_str2(char **dest, const pj_str_t *src)
+{
+       int res = ast_asprintf(dest, "%.*s", (int)pj_strlen(src), pj_strbuf(src));
+
+       if (res < 0) {
+               *dest = NULL;
+       }
+
+       return res;
+}
+
+
 int ast_sip_is_content_type(pjsip_media_type *content_type, char *type, char *subtype)
 {
        pjsip_media_type compare;
@@ -4221,6 +4493,56 @@ const char *ast_sip_get_host_ip_string(int af)
        return NULL;
 }
 
+int ast_sip_dtmf_to_str(const enum ast_sip_dtmf_mode dtmf,
+                       char *buf, size_t buf_len)
+{
+       switch (dtmf) {
+       case AST_SIP_DTMF_NONE:
+               ast_copy_string(buf, "none", buf_len);
+               break;
+       case AST_SIP_DTMF_RFC_4733:
+               ast_copy_string(buf, "rfc4733", buf_len);
+               break;
+       case AST_SIP_DTMF_INBAND:
+               ast_copy_string(buf, "inband", buf_len);
+               break;
+       case AST_SIP_DTMF_INFO:
+               ast_copy_string(buf, "info", buf_len);
+               break;
+       case AST_SIP_DTMF_AUTO:
+               ast_copy_string(buf, "auto", buf_len);
+               break;
+       case AST_SIP_DTMF_AUTO_INFO:
+               ast_copy_string(buf, "auto_info", buf_len);
+               break;
+       default:
+               buf[0] = '\0';
+               return -1;
+       }
+       return 0;
+}
+
+int ast_sip_str_to_dtmf(const char * dtmf_mode)
+{
+       int result = -1;
+
+       if (!strcasecmp(dtmf_mode, "info")) {
+               result = AST_SIP_DTMF_INFO;
+       } else if (!strcasecmp(dtmf_mode, "rfc4733")) {
+               result = AST_SIP_DTMF_RFC_4733;
+       } else if (!strcasecmp(dtmf_mode, "inband")) {
+               result = AST_SIP_DTMF_INBAND;
+       } else if (!strcasecmp(dtmf_mode, "none")) {
+               result = AST_SIP_DTMF_NONE;
+       } else if (!strcasecmp(dtmf_mode, "auto")) {
+               result = AST_SIP_DTMF_AUTO;
+       } else if (!strcasecmp(dtmf_mode, "auto_info")) {
+               result = AST_SIP_DTMF_AUTO_INFO;
+       }
+
+       return result;
+}
+
 /*!
  * \brief Set name and number information on an identity header.
  *
@@ -4237,11 +4559,15 @@ void ast_sip_modify_id_header(pj_pool_t *pool, pjsip_fromto_hdr *id_hdr, const s
        id_uri = pjsip_uri_get_uri(id_name_addr->uri);
 
        if (id->name.valid) {
-               int name_buf_len = strlen(id->name.str) * 2 + 1;
-               char *name_buf = ast_alloca(name_buf_len);
+               if (!ast_strlen_zero(id->name.str)) {
+                       int name_buf_len = strlen(id->name.str) * 2 + 1;
+                       char *name_buf = ast_alloca(name_buf_len);
 
-               ast_escape_quoted(id->name.str, name_buf, name_buf_len);
-               pj_strdup2(pool, &id_name_addr->display, name_buf);
+                       ast_escape_quoted(id->name.str, name_buf, name_buf_len);
+                       pj_strdup2(pool, &id_name_addr->display, name_buf);
+               } else {
+                       pj_strdup2(pool, &id_name_addr->display, NULL);
+               }
        }
 
        if (id->number.valid) {
@@ -4346,6 +4672,7 @@ static int unload_pjsip(void *data)
                ast_sip_destroy_system();
                ast_sip_destroy_global_headers();
                internal_sip_unregister_service(&supplement_module);
+               ast_sip_destroy_transport_events();
        }
 
        if (monitor_thread) {
@@ -4354,8 +4681,13 @@ static int unload_pjsip(void *data)
        }
 
        if (memory_pool) {
-               pj_pool_release(memory_pool);
+               /* This mimics the behavior of pj_pool_safe_release
+                * which was introduced in pjproject 2.6.
+                */
+               pj_pool_t *temp_pool = memory_pool;
+
                memory_pool = NULL;
+               pj_pool_release(temp_pool);
        }
 
        ast_pjsip_endpoint = NULL;
@@ -4419,7 +4751,6 @@ static int load_pjsip(void)
        return AST_MODULE_LOAD_SUCCESS;
 
 error:
-       unload_pjsip(NULL);
        return AST_MODULE_LOAD_DECLINE;
 }
 
@@ -4485,6 +4816,11 @@ static int load_module(void)
                goto error;
        }
 
+       if (ast_sip_initialize_transport_events()) {
+               ast_log(LOG_ERROR, "Failed to initialize SIP transport monitor. Aborting load\n");
+               goto error;
+       }
+
        ast_sip_initialize_dns();
 
        ast_sip_initialize_global_headers();