res_pjsip: Remove ephemeral registered contacts on transport shutdown.
[asterisk/asterisk.git] / res / res_pjsip.c
index 59777c9..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">
                                                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>
@@ -2368,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);
 }
@@ -2713,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];
 
@@ -2738,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)) {
@@ -2776,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);
@@ -2843,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)
@@ -2859,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;
@@ -2893,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;
@@ -2912,18 +3101,19 @@ pjsip_dialog *ast_sip_create_dialog_uac(const struct ast_sip_endpoint *endpoint,
        if (res != PJ_SUCCESS) {
                if (res == PJSIP_EINVALIDURI) {
                        ast_log(LOG_ERROR,
-                               "Endpoint '%s': Could not create dialog to invalid URI '%s'.  Is endpoint registered?\n",
+                               "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;
        }
@@ -2931,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);
 
@@ -2959,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 };
@@ -3032,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;
        }
 
@@ -3081,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;
 
@@ -3106,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;
 
@@ -3117,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} };
@@ -3198,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) {
@@ -3223,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)) {
@@ -3242,6 +3449,8 @@ 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;
@@ -3283,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;
@@ -3547,7 +3758,6 @@ 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. */
@@ -3572,11 +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);
 
-       if (endpoint) {
-               sip_get_tpselector_from_endpoint(endpoint, &selector);
-               pjsip_tx_data_set_transport(tdata, &selector);
-       }
-
        if (timeout > 0) {
                pj_time_val timeout_timer_val = { timeout / 1000, timeout % 1000 };
 
@@ -4061,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;
@@ -4276,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.
  *
@@ -4292,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) {
@@ -4401,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) {
@@ -4409,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;
@@ -4474,7 +4751,6 @@ static int load_pjsip(void)
        return AST_MODULE_LOAD_SUCCESS;
 
 error:
-       unload_pjsip(NULL);
        return AST_MODULE_LOAD_DECLINE;
 }
 
@@ -4540,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();