Change the default value for "allowsubscribe" to yes to match chan_sip.
[asterisk/asterisk.git] / res / res_sip.c
index 7f6b061..9b05235 100644 (file)
@@ -53,7 +53,7 @@
                                        It contains the core SIP related options only, endpoints are <emphasis>NOT</emphasis>
                                        dialable entries of their own. Communication with another SIP device is
                                        accomplished via Addresses of Record (AoRs) which have one or more
-                                       contacts assicated with them. Endpoints <emphasis>NOT</emphasis> configured to 
+                                       contacts assicated with them. Endpoints <emphasis>NOT</emphasis> configured to
                                        use a <literal>transport</literal> will default to first transport found
                                        in <filename>res_sip.conf</filename> that matches its type.
                                        </para>
                                        first transport that matches the type. A SIP URI of <literal>sip:5000@[11::33]</literal>
                                        will use the first IPv6 transport and try to send the request.
                                        </para>
+                                       <para>If the anonymous endpoint identifier is in use an endpoint with the name
+                                       "anonymous@domain" will be searched for as a last resort. If this is not found
+                                       it will fall back to searching for "anonymous". If neither endpoints are found
+                                       the anonymous endpoint identifier will not return an endpoint and anonymous
+                                       calling will not be possible.
+                                       </para>
                                </description>
                                <configOption name="100rel" default="yes">
                                        <synopsis>Allow support for RFC3262 provisional ACK tags</synopsis>
                                                </enumlist>
                                        </description>
                                </configOption>
+                               <configOption name="connected_line_method" default="invite">
+                                       <synopsis>Connected line method type</synopsis>
+                                       <description>
+                                               <para>Method used when updating connected line information.</para>
+                                               <enumlist>
+                                                       <enum name="invite" />
+                                                       <enum name="reinvite">
+                                                               <para>Alias for the <literal>invite</literal> value.</para>
+                                                       </enum>
+                                                       <enum name="update" />
+                                               </enumlist>
+                                       </description>
+                               </configOption>
                                <configOption name="direct_media" default="yes">
                                        <synopsis>Determines whether media may flow directly between endpoints.</synopsis>
                                </configOption>
                                <configOption name="outbound_proxy">
                                        <synopsis>Proxy through which to send requests</synopsis>
                                </configOption>
-                               <configOption name="qualify_frequency" default="0">
-                                       <synopsis>Interval at which to qualify an endpoint</synopsis>
-                                       <description><para>
-                                               Interval between attempts to qualify the endpoint for reachability.
-                                               If <literal>0</literal> never qualify. Time in seconds.
-                                       </para></description>
-                               </configOption>
                                <configOption name="rewrite_contact">
                                        <synopsis>Allow Contact header to be rewritten with the source IP address-port</synopsis>
                                </configOption>
                                <configOption name="use_ptime" default="no">
                                        <synopsis>Use Endpoint's requested packetisation interval</synopsis>
                                </configOption>
+                               <configOption name="use_avpf" default="no">
+                                       <synopsis>Determines whether res_sip will use and enforce usage of AVPF for this
+                                       endpoint.</synopsis>
+                                       <description><para>
+                                               If set to <literal>yes</literal>, res_sip will use use the AVPF or SAVPF RTP
+                                               profile for all media offers on outbound calls and media updates and will
+                                               decline media offers not using the AVPF or SAVPF profile.
+                                       </para><para>
+                                               If set to <literal>no</literal>, res_sip will use use the AVP or SAVP RTP
+                                               profile for all media offers on outbound calls and media updates and will
+                                               decline media offers not using the AVP or SAVP profile.
+                                       </para></description>
+                               </configOption>
+                               <configOption name="media_encryption" default="no">
+                                       <synopsis>Determines whether res_sip will use and enforce usage of media encryption
+                                       for this endpoint.</synopsis>
+                                       <description>
+                                               <enumlist>
+                                                       <enum name="no"><para>
+                                                               res_sip will offer no encryption and allow no encryption to be setup.
+                                                       </para></enum>
+                                                       <enum name="sdes"><para>
+                                                               res_sip will offer standard SRTP setup via in-SDP keys. Encrypted SIP
+                                                               transport should be used in conjunction with this option to prevent
+                                                               exposure of media encryption keys.
+                                                       </para></enum>
+                                                       <enum name="dtls"><para>
+                                                               res_sip will offer DTLS-SRTP setup.
+                                                       </para></enum>
+                                               </enumlist>
+                                       </description>
+                               </configOption>
+                               <configOption name="inband_progress" default="no">
+                                       <synopsis>Determines whether chan_gulp will indicate ringing using inband
+                                           progress.</synopsis>
+                                       <description><para>
+                                               If set to <literal>yes</literal>, chan_gulp will send a 183 Session Progress
+                                               when told to indicate ringing and will immediately start sending ringing
+                                               as audio.
+                                       </para><para>
+                                               If set to <literal>no</literal>, chan_gulp will send a 180 Ringing when told
+                                               to indicate ringing and will NOT send it as audio.
+                                       </para></description>
+                               </configOption>
+                               <configOption name="callgroup">
+                                       <synopsis>The numeric pickup groups for a channel.</synopsis>
+                                       <description><para>
+                                               Can be set to a comma separated list of numbers or ranges between the values
+                                               of 0-63 (maximum of 64 groups).
+                                       </para></description>
+                               </configOption>
+                               <configOption name="pickupgroup">
+                                       <synopsis>The numeric pickup groups that a channel can pickup.</synopsis>
+                                       <description><para>
+                                               Can be set to a comma separated list of numbers or ranges between the values
+                                               of 0-63 (maximum of 64 groups).
+                                       </para></description>
+                               </configOption>
+                               <configOption name="namedcallgroup">
+                                       <synopsis>The named pickup groups for a channel.</synopsis>
+                                       <description><para>
+                                               Can be set to a comma separated list of case sensitive strings limited by
+                                               supported line length.
+                                       </para></description>
+                               </configOption>
+                               <configOption name="namedpickupgroup">
+                                       <synopsis>The named pickup groups that a channel can pickup.</synopsis>
+                                       <description><para>
+                                               Can be set to a comma separated list of case sensitive strings limited by
+                                               supported line length.
+                                       </para></description>
+                               </configOption>
+                               <configOption name="devicestate_busy_at" default="0">
+                                       <synopsis>The number of in-use channels which will cause busy to be returned as device state</synopsis>
+                                       <description><para>
+                                               When the number of in-use channels for the endpoint matches the devicestate_busy_at setting the
+                                               Gulp channel driver will return busy as the device state instead of in use.
+                                       </para></description>
+                               </configOption>
+                               <configOption name="tonezone">
+                                       <synopsis>Set which country's indications to use for channels created for this endpoint.</synopsis>
+                               </configOption>
+                               <configOption name="language">
+                                       <synopsis>Set the default language to use for channels created for this endpoint.</synopsis>
+                               </configOption>
+                               <configOption name="one_touch_recording" default="no">
+                                       <synopsis>Determines whether one-touch recording is allowed for this endpoint.</synopsis>
+                                       <see-also>
+                                               <ref type="configOption">recordonfeature</ref>
+                                               <ref type="configOption">recordofffeature</ref>
+                                       </see-also>
+                               </configOption>
+                               <configOption name="recordonfeature" default="automixmon">
+                                       <synopsis>The feature to enact when one-touch recording is turned on.</synopsis>
+                                       <description>
+                                               <para>When an INFO request for one-touch recording arrives with a Record header set to "on", this
+                                               feature will be enabled for the channel. The feature designated here can be any built-in
+                                               or dynamic feature defined in features.conf.</para>
+                                               <note><para>This setting has no effect if the endpoint's one_touch_recording option is disabled</para></note>
+                                       </description>
+                                       <see-also>
+                                               <ref type="configOption">one_touch_recording</ref>
+                                               <ref type="configOption">recordofffeature</ref>
+                                       </see-also>
+                               </configOption>
+                               <configOption name="recordofffeature" default="automixmon">
+                                       <synopsis>The feature to enact when one-touch recording is turned off.</synopsis>
+                                       <description>
+                                               <para>When an INFO request for one-touch recording arrives with a Record header set to "off", this
+                                               feature will be enabled for the channel. The feature designated here can be any built-in
+                                               or dynamic feature defined in features.conf.</para>
+                                               <note><para>This setting has no effect if the endpoint's one_touch_recording option is disabled</para></note>
+                                       </description>
+                                       <see-also>
+                                               <ref type="configOption">one_touch_recording</ref>
+                                               <ref type="configOption">recordonfeature</ref>
+                                       </see-also>
+                               </configOption>
+                               <configOption name="rtpengine" default="asterisk">
+                                       <synopsis>Name of the RTP engine to use for channels created for this endpoint</synopsis>
+                               </configOption>
+                               <configOption name="allowtransfer" default="yes">
+                                       <synopsis>Determines whether SIP REFER transfers are allowed for this endpoint</synopsis>
+                               </configOption>
+                               <configOption name="sdpowner" default="-">
+                                       <synopsis>String placed as the username portion of an SDP origin (o=) line.</synopsis>
+                               </configOption>
+                               <configOption name="sdpsession" default="Asterisk">
+                                       <synopsis>String used for the SDP session (s=) line.</synopsis>
+                               </configOption>
+                               <configOption name="tos_audio">
+                                       <synopsis>DSCP TOS bits for audio streams</synopsis>
+                                       <description><para>
+                                               See https://wiki.asterisk.org/wiki/display/AST/IP+Quality+of+Service for more information about QoS settings
+                                       </para></description>
+                               </configOption>
+                               <configOption name="tos_video">
+                                       <synopsis>DSCP TOS bits for video streams</synopsis>
+                                       <description><para>
+                                               See https://wiki.asterisk.org/wiki/display/AST/IP+Quality+of+Service for more information about QoS settings
+                                       </para></description>
+                               </configOption>
+                               <configOption name="cos_audio">
+                                       <synopsis>Priority for audio streams</synopsis>
+                                       <description><para>
+                                               See https://wiki.asterisk.org/wiki/display/AST/IP+Quality+of+Service for more information about QoS settings
+                                       </para></description>
+                               </configOption>
+                               <configOption name="cos_video">
+                                       <synopsis>Priority for video streams</synopsis>
+                                       <description><para>
+                                               See https://wiki.asterisk.org/wiki/display/AST/IP+Quality+of+Service for more information about QoS settings
+                                       </para></description>
+                               </configOption>
+                               <configOption name="allowsubscribe" default="yes">
+                                       <synopsis>Determines if endpoint is allowed to initiate subscriptions with Asterisk.</synopsis>
+                               </configOption>
+                               <configOption name="subminexpiry" default="60">
+                                       <synopsis>The minimum allowed expiry time for subscriptions initiated by the endpoint.</synopsis>
+                               </configOption>
+                               <configOption name="fromuser">
+                                       <synopsis>Username to use in From header for requests to this endpoint.</synopsis>
+                               </configOption>
+                               <configOption name="mwifromuser">
+                                       <synopsis>Username to use in From header for unsolicited MWI NOTIFYs to this endpoint.</synopsis>
+                               </configOption>
+                               <configOption name="fromdomain">
+                                       <synopsis>Domain to user in From header for requests to this endpoint.</synopsis>
+                               </configOption>
+                               <configOption name="dtlsverify">
+                                       <synopsis>Verify that the provided peer certificate is valid</synopsis>
+                                       <description><para>
+                                               This option only applies if <replaceable>media_encryption</replaceable> is
+                                               set to <literal>dtls</literal>.
+                                       </para></description>
+                               </configOption>
+                               <configOption name="dtlsrekey">
+                                       <synopsis>Interval at which to renegotiate the TLS session and rekey the SRTP session</synopsis>
+                                       <description><para>
+                                               This option only applies if <replaceable>media_encryption</replaceable> is
+                                               set to <literal>dtls</literal>.
+                                       </para><para>
+                                               If this is not set or the value provided is 0 rekeying will be disabled.
+                                       </para></description>
+                               </configOption>
+                               <configOption name="dtlscertfile">
+                                       <synopsis>Path to certificate file to present to peer</synopsis>
+                                       <description><para>
+                                               This option only applies if <replaceable>media_encryption</replaceable> is
+                                               set to <literal>dtls</literal>.
+                                       </para></description>
+                               </configOption>
+                               <configOption name="dtlsprivatekey">
+                                       <synopsis>Path to private key for certificate file</synopsis>
+                                       <description><para>
+                                               This option only applies if <replaceable>media_encryption</replaceable> is
+                                               set to <literal>dtls</literal>.
+                                       </para></description>
+                               </configOption>
+                               <configOption name="dtlscipher">
+                                       <synopsis>Cipher to use for DTLS negotiation</synopsis>
+                                       <description><para>
+                                               This option only applies if <replaceable>media_encryption</replaceable> is
+                                               set to <literal>dtls</literal>.
+                                       </para><para>
+                                               Many options for acceptable ciphers. See link for more:
+                                               http://www.openssl.org/docs/apps/ciphers.html#CIPHER_STRINGS
+                                       </para></description>
+                               </configOption>
+                               <configOption name="dtlscafile">
+                                       <synopsis>Path to certificate authority certificate</synopsis>
+                                       <description><para>
+                                               This option only applies if <replaceable>media_encryption</replaceable> is
+                                               set to <literal>dtls</literal>.
+                                       </para></description>
+                               </configOption>
+                               <configOption name="dtlscapath">
+                                       <synopsis>Path to a directory containing certificate authority certificates</synopsis>
+                                       <description><para>
+                                               This option only applies if <replaceable>media_encryption</replaceable> is
+                                               set to <literal>dtls</literal>.
+                                       </para></description>
+                               </configOption>
+                               <configOption name="dtlssetup">
+                                       <synopsis>Whether we are willing to accept connections, connect to the other party, or both.</synopsis>
+                                       <description>
+                                               <para>
+                                                       This option only applies if <replaceable>media_encryption</replaceable> is
+                                                       set to <literal>dtls</literal>.
+                                               </para>
+                                               <enumlist>
+                                                       <enum name="active"><para>
+                                                               res_sip will make a connection to the peer.
+                                                       </para></enum>
+                                                       <enum name="passive"><para>
+                                                               res_sip will accept connections from the peer.
+                                                       </para></enum>
+                                                       <enum name="actpass"><para>
+                                                               res_sip will offer and accept connections from the peer.
+                                                       </para></enum>
+                                               </enumlist>
+                                       </description>
+                               </configOption>
+                               <configOption name="srtp_tag_32">
+                                       <synopsis>Determines whether 32 byte tags should be used instead of 80 byte tags.</synopsis>
+                                       <description><para>
+                                               This option only applies if <replaceable>media_encryption</replaceable> is
+                                               set to <literal>sdes</literal> or <literal>dtls</literal>.
+                                       </para></description>
+                               </configOption>
                        </configObject>
                        <configObject name="auth">
                                <synopsis>Authentication type</synopsis>
                                                Time to keep alive a contact. String style specification.
                                        </para></description>
                                </configOption>
+                               <configOption name="qualify_frequency" default="0">
+                                       <synopsis>Interval at which to qualify a contact</synopsis>
+                                       <description><para>
+                                               Interval between attempts to qualify the contact for reachability.
+                                               If <literal>0</literal> never qualify. Time in seconds.
+                                       </para></description>
+                               </configOption>
+                       </configObject>
+                       <configObject name="contact_status">
+                               <synopsis>Status for a contact</synopsis>
+                               <description><para>
+                                       The contact status keeps track of whether or not a contact is reachable
+                                       and how long it took to qualify the contact (round trip time).
+                               </para></description>
+                               <configOption name="status">
+                                       <synopsis>A contact's status</synopsis>
+                                       <description>
+                                               <enumlist>
+                                                       <enum name="AVAILABLE" />
+                                                       <enum name="UNAVAILABLE" />
+                                               </enumlist>
+                                       </description>
+                               </configOption>
+                               <configOption name="rtt">
+                                       <synopsis>Round trip time</synopsis>
+                                       <description><para>
+                                               The time, in microseconds, it took to qualify the contact.
+                                       </para></description>
+                               </configOption>
                        </configObject>
                        <configObject name="aor">
                                <synopsis>The configuration for a location of an endpoint</synopsis>
                                <configOption name="type">
                                        <synopsis>Must be of type 'aor'.</synopsis>
                                </configOption>
+                               <configOption name="qualify_frequency" default="0">
+                                       <synopsis>Interval at which to qualify an AoR</synopsis>
+                                       <description><para>
+                                               Interval between attempts to qualify the AoR for reachability.
+                                               If <literal>0</literal> never qualify. Time in seconds.
+                                       </para></description>
+                               </configOption>
+                               <configOption name="authenticate_qualify" default="no">
+                                       <synopsis>Authenticates a qualify request if needed</synopsis>
+                                       <description><para>
+                                               If true and a qualify request receives a challenge or authenticate response
+                                               authentication is attempted before declaring the contact available.
+                                       </para></description>
+                               </configOption>
+                       </configObject>
+                       <configObject name="system">
+                               <synopsis>Options that apply to the SIP stack as well as other system-wide settings</synopsis>
+                               <description><para>
+                                       The settings in this section are global. In addition to being global, the values will
+                                       not be re-evaluated when a reload is performed. This is because the values must be set
+                                       before the SIP stack is initialized. The only way to reset these values is to either 
+                                       restart Asterisk, or unload res_sip.so and then load it again.
+                               </para></description>
+                               <configOption name="timert1" default="500">
+                                       <synopsis>Set transaction timer T1 value (milliseconds).</synopsis>
+                                       <description><para>
+                                               Timer T1 is the base for determining how long to wait before retransmitting
+                                               requests that receive no response when using an unreliable transport (e.g. UDP).
+                                               For more information on this timer, see RFC 3261, Section 17.1.1.1.
+                                       </para></description>
+                               </configOption>
+                               <configOption name="timerb" default="32000">
+                                       <synopsis>Set transaction timer B value (milliseconds).</synopsis>
+                                       <description><para>
+                                               Timer B determines the maximum amount of time to wait after sending an INVITE
+                                               request before terminating the transaction. It is recommended that this be set
+                                               to 64 * Timer T1, but it may be set higher if desired. For more information on
+                                               this timer, see RFC 3261, Section 17.1.1.1.
+                                       </para></description>
+                               </configOption>
+                               <configOption name="compactheaders" default="no">
+                                       <synopsis>Use the short forms of common SIP header names.</synopsis>
+                               </configOption>
+                       </configObject>
+                       <configObject name="global">
+                               <synopsis>Options that apply globally to all SIP communications</synopsis>
+                               <description><para>
+                                       The settings in this section are global. Unlike options in the <literal>system</literal>
+                                       section, these options can be refreshed by performing a reload.
+                               </para></description>
+                               <configOption name="maxforwards" default="70">
+                                       <synopsis>Value used in Max-Forwards header for SIP requests.</synopsis>
+                               </configOption>
+                               <configOption name="useragent" default="Asterisk &lt;Asterisk Version&gt;">
+                                       <synopsis>Value used in User-Agent header for SIP requests and Server header for SIP responses.</synopsis>
+                               </configOption>
                        </configObject>
                </configFile>
        </configInfo>
@@ -734,7 +1081,7 @@ 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 pj_str_t *target, pjsip_tpselector *selector)
+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;
@@ -743,7 +1090,7 @@ static int sip_dialog_create_from(pj_pool_t *pool, pj_str_t *from, const char *u
        int local_port;
        char uuid_str[AST_UUID_STR_LEN];
 
-       if (!user) {
+       if (ast_strlen_zero(user)) {
                RAII_VAR(struct ast_uuid *, uuid, ast_uuid_generate(), ast_free_ptr);
                if (!uuid) {
                        return -1;
@@ -775,18 +1122,30 @@ static int sip_dialog_create_from(pj_pool_t *pool, pj_str_t *from, const char *u
        }
 
        /* If the host is IPv6 turn the transport into an IPv6 version */
-       if (pj_strchr(&sip_uri->host, ':')) {
+       if (pj_strchr(&sip_uri->host, ':') && type < PJSIP_TRANSPORT_START_OTHER) {
                type = (pjsip_transport_type_e)(((int)type) + PJSIP_TRANSPORT_IPV6);
        }
 
+       if (!ast_strlen_zero(domain)) {
+               from->ptr = pj_pool_alloc(pool, PJSIP_MAX_URL_SIZE);
+               from->slen = pj_ansi_snprintf(from->ptr, PJSIP_MAX_URL_SIZE,
+                               "<%s:%s@%s%s%s>",
+                               (pjsip_transport_get_flag_from_type(type) & PJSIP_TRANSPORT_SECURE) ? "sips" : "sip",
+                               user,
+                               domain,
+                               (type != PJSIP_TRANSPORT_UDP && type != PJSIP_TRANSPORT_UDP6) ? ";transport=" : "",
+                               (type != PJSIP_TRANSPORT_UDP && type != PJSIP_TRANSPORT_UDP6) ? pjsip_transport_get_type_name(type) : "");
+               return 0;
+       }
+
        /* Get the local bound address for the transport that will be used when communicating with the provided URI */
        if (pjsip_tpmgr_find_local_addr(pjsip_endpt_get_tpmgr(ast_sip_get_pjsip_endpoint()), pool, type, selector,
                                                              &local_addr, &local_port) != PJ_SUCCESS) {
                return -1;
        }
 
-       /* If IPv6 was not specified in the host but is in the transport, set the proper type */
-       if (!pj_strchr(&sip_uri->host, ':') && pj_strchr(&local_addr, ':')) {
+       /* 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);
        }
 
@@ -821,10 +1180,10 @@ static int sip_get_tpselector_from_endpoint(const struct ast_sip_endpoint *endpo
                return -1;
        }
 
-       if (transport->type == AST_TRANSPORT_UDP) {
+       if (transport->state->transport) {
                selector->type = PJSIP_TPSELECTOR_TRANSPORT;
                selector->u.transport = transport->state->transport;
-       } else if (transport->type == AST_TRANSPORT_TCP || transport->type == AST_TRANSPORT_TLS) {
+       } else if (transport->state->factory) {
                selector->type = PJSIP_TPSELECTOR_LISTENER;
                selector->u.listener = transport->state->factory;
        } else {
@@ -834,6 +1193,22 @@ static int sip_get_tpselector_from_endpoint(const struct ast_sip_endpoint *endpo
        return 0;
 }
 
+static int sip_get_tpselector_from_uri(const char *uri, pjsip_tpselector *selector)
+{
+       RAII_VAR(struct ast_sip_contact_transport *, contact_transport, NULL, ao2_cleanup);
+
+       contact_transport = ast_sip_location_retrieve_contact_transport_by_uri(uri);
+
+       if (!contact_transport) {
+               return -1;
+       }
+
+       selector->type = PJSIP_TPSELECTOR_TRANSPORT;
+       selector->u.transport = contact_transport->transport;
+
+       return 0;
+}
+
 pjsip_dialog *ast_sip_create_dialog(const struct ast_sip_endpoint *endpoint, const char *uri, const char *request_user)
 {
        pj_str_t local_uri = { "sip:temp@temp", 13 }, remote_uri;
@@ -848,12 +1223,12 @@ pjsip_dialog *ast_sip_create_dialog(const struct ast_sip_endpoint *endpoint, con
                return NULL;
        }
 
-       if (sip_get_tpselector_from_endpoint(endpoint, &selector)) {
+       if (sip_get_tpselector_from_uri(uri, &selector) && sip_get_tpselector_from_endpoint(endpoint, &selector)) {
                pjsip_dlg_terminate(dlg);
                return NULL;
        }
 
-       if (sip_dialog_create_from(dlg->pool, &local_uri, NULL, &remote_uri, &selector)) {
+       if (sip_dialog_create_from(dlg->pool, &local_uri, endpoint->fromuser, endpoint->fromdomain, &remote_uri, &selector)) {
                pjsip_dlg_terminate(dlg);
                return NULL;
        }
@@ -898,6 +1273,7 @@ pjsip_dialog *ast_sip_create_dialog(const struct ast_sip_endpoint *endpoint, con
 
 /* PJSIP doesn't know about the INFO method, so we have to define it ourselves */
 const pjsip_method pjsip_info_method = {PJSIP_OTHER_METHOD, {"INFO", 4} };
+const pjsip_method pjsip_message_method = {PJSIP_OTHER_METHOD, {"MESSAGE", 7} };
 
 static struct {
        const char *method;
@@ -913,6 +1289,7 @@ static struct {
        { "NOTIFY", &pjsip_notify_method },
        { "PUBLISH", &pjsip_publish_method },
        { "INFO", &pjsip_info_method },
+       { "MESSAGE", &pjsip_message_method },
 };
 
 static const pjsip_method *get_pjsip_method(const char *method)
@@ -946,6 +1323,11 @@ static int create_out_of_dialog_request(const pjsip_method *method, struct ast_s
        pjsip_tpselector selector = { .type = PJSIP_TPSELECTOR_NONE, };
 
        if (ast_strlen_zero(uri)) {
+               if (!endpoint) {
+                       ast_log(LOG_ERROR, "An endpoint and/or uri must be specified\n");
+                       return -1;
+               }
+
                contact = ast_sip_location_retrieve_contact_from_aor_list(endpoint->aors);
                if (!contact || ast_strlen_zero(contact->uri)) {
                        ast_log(LOG_ERROR, "Unable to retrieve contact for endpoint %s\n",
@@ -958,10 +1340,12 @@ static int create_out_of_dialog_request(const pjsip_method *method, struct ast_s
                pj_cstr(&remote_uri, uri);
        }
 
-       if (sip_get_tpselector_from_endpoint(endpoint, &selector)) {
-               ast_log(LOG_ERROR, "Unable to retrieve PJSIP transport selector for endpoint %s\n",
+       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;
+                       return -1;
+               }
        }
 
        pool = pjsip_endpt_create_pool(ast_sip_get_pjsip_endpoint(), "Outbound request", 256, 256);
@@ -971,7 +1355,8 @@ static int create_out_of_dialog_request(const pjsip_method *method, struct ast_s
                return -1;
        }
 
-       if (sip_dialog_create_from(pool, &from, NULL, &remote_uri, &selector)) {
+       if (sip_dialog_create_from(pool, &from, endpoint ? endpoint->fromuser : NULL,
+                               endpoint ? endpoint->fromdomain : NULL, &remote_uri, &selector)) {
                ast_log(LOG_ERROR, "Unable to create From header for %.*s request to endpoint %s\n",
                                (int) pj_strlen(&method->name), pj_strbuf(&method->name), ast_sorcery_object_get_id(endpoint));
                pjsip_endpt_release_pool(ast_sip_get_pjsip_endpoint(), pool);
@@ -994,7 +1379,8 @@ static int create_out_of_dialog_request(const pjsip_method *method, struct ast_s
 }
 
 int ast_sip_create_request(const char *method, struct pjsip_dialog *dlg,
-               struct ast_sip_endpoint *endpoint, const char *uri, pjsip_tx_data **tdata)
+               struct ast_sip_endpoint *endpoint, const char *uri,
+               pjsip_tx_data **tdata)
 {
        const pjsip_method *pmethod = get_pjsip_method(method);
 
@@ -1066,7 +1452,7 @@ int ast_sip_add_header(pjsip_tx_data *tdata, const char *name, const char *value
        pj_str_t hdr_name;
        pj_str_t hdr_value;
        pjsip_generic_string_hdr *hdr;
-       
+
        pj_cstr(&hdr_name, name);
        pj_cstr(&hdr_value, value);
 
@@ -1085,7 +1471,7 @@ static pjsip_msg_body *ast_body_to_pjsip_body(pj_pool_t *pool, const struct ast_
        pj_cstr(&type, body->type);
        pj_cstr(&subtype, body->subtype);
        pj_cstr(&body_text, body->body_text);
-       
+
        return pjsip_msg_body_create(pool, &type, &subtype, &body_text);
 }
 
@@ -1206,13 +1592,26 @@ int ast_sip_push_task_synchronous(struct ast_taskprocessor *serializer, int (*si
        return std.fail;
 }
 
-void ast_copy_pj_str(char *dest, pj_str_t *src, size_t size)
+void ast_copy_pj_str(char *dest, const pj_str_t *src, size_t size)
 {
        size_t chars_to_copy = MIN(size - 1, pj_strlen(src));
        memcpy(dest, pj_strbuf(src), chars_to_copy);
        dest[chars_to_copy] = '\0';
 }
 
+int ast_sip_is_content_type(pjsip_media_type *content_type, char *type, char *subtype)
+{
+       pjsip_media_type compare;
+
+       if (!content_type) {
+               return 0;
+       }
+
+       pjsip_media_type_init2(&compare, type, subtype);
+
+       return pjsip_media_type_cmp(content_type, &compare, 0) ? -1 : 0;
+}
+
 pj_caching_pool caching_pool;
 pj_pool_t *memory_pool;
 pj_thread_t *monitor_thread;
@@ -1235,7 +1634,7 @@ static void stop_monitor_thread(void)
 
 AST_THREADSTORAGE(pj_thread_storage);
 AST_THREADSTORAGE(servant_id_storage);
-#define SIP_SERVANT_ID 0xDEFECA7E
+#define SIP_SERVANT_ID 0x5E2F1D
 
 static void sip_thread_start(void)
 {
@@ -1274,6 +1673,18 @@ int ast_sip_thread_is_servant(void)
        return *servant_id == SIP_SERVANT_ID;
 }
 
+static void remove_request_headers(pjsip_endpoint *endpt)
+{
+       const pjsip_hdr *request_headers = pjsip_endpt_get_request_headers(endpt);
+       pjsip_hdr *iter = request_headers->next;
+
+       while (iter != request_headers) {
+               pjsip_hdr *to_erase = iter;
+               iter = iter->next;
+               pj_list_erase(to_erase);
+       }
+}
+
 static int load_module(void)
 {
     /* The third parameter is just copied from
@@ -1311,12 +1722,23 @@ static int load_module(void)
                ast_log(LOG_ERROR, "Failed to create PJSIP endpoint structure. Aborting load\n");
                goto error;
        }
+
+       /* PJSIP will automatically try to add a Max-Forwards header. Since we want to control that,
+        * we need to stop PJSIP from doing it automatically
+        */
+       remove_request_headers(ast_pjsip_endpoint);
+
        memory_pool = pj_pool_create(&caching_pool.factory, "SIP", 1024, 1024, NULL);
        if (!memory_pool) {
                ast_log(LOG_ERROR, "Failed to create memory pool for SIP. Aborting load\n");
                goto error;
        }
 
+       if (ast_sip_initialize_system()) {
+               ast_log(LOG_ERROR, "Failed to initialize SIP system configuration. Aborting load\n");
+               goto error;
+       }
+
        pjsip_tsx_layer_init_module(ast_pjsip_endpoint);
        pjsip_ua_init_module(ast_pjsip_endpoint, NULL);
 
@@ -1328,6 +1750,8 @@ static int load_module(void)
                goto error;
        }
 
+       ast_sip_initialize_global_headers();
+
        if (ast_res_sip_initialize_configuration()) {
                ast_log(LOG_ERROR, "Failed to initialize SIP configuration. Aborting load\n");
                goto error;
@@ -1345,10 +1769,14 @@ static int load_module(void)
 
        ast_res_sip_init_options_handling(0);
 
+       ast_res_sip_init_contact_transports();
+
 return AST_MODULE_LOAD_SUCCESS;
 
 error:
+       ast_sip_destroy_distributor();
        ast_res_sip_destroy_configuration();
+       ast_sip_destroy_global_headers();
        if (monitor_thread) {
                stop_monitor_thread();
        }
@@ -1390,7 +1818,9 @@ static int unload_pjsip(void *data)
 
 static int unload_module(void)
 {
+       ast_sip_destroy_distributor();
        ast_res_sip_destroy_configuration();
+       ast_sip_destroy_global_headers();
        if (monitor_thread) {
                stop_monitor_thread();
        }