Skip to content
Snippets Groups Projects
chan_sip.c 699 KiB
Newer Older
  • Learn to ignore specific revisions
  • Mark Spencer's avatar
    Mark Spencer committed
    /*
    
     * Asterisk -- An open source telephony toolkit.
    
    Mark Spencer's avatar
    Mark Spencer committed
     *
    
     * Copyright (C) 1999 - 2006, Digium, Inc.
    
    Mark Spencer's avatar
    Mark Spencer committed
     *
    
     * Mark Spencer <markster@digium.com>
    
    Mark Spencer's avatar
    Mark Spencer committed
     *
    
     * See http://www.asterisk.org for more information about
     * the Asterisk project. Please do not directly contact
     * any of the maintainers of this project for assistance;
     * the project provides a web site, mailing lists and IRC
     * channels for your use.
     *
    
    Mark Spencer's avatar
    Mark Spencer committed
     * This program is free software, distributed under the terms of
    
     * the GNU General Public License Version 2. See the LICENSE file
     * at the top of the source tree.
     */
    
    
    /*!
     * \file
     * \brief Implementation of Session Initiation Protocol
    
     *
     * \author Mark Spencer <markster@digium.com>
     *
     * See Also:
     * \arg \ref AstCREDITS
     *
    
     * Implementation of RFC 3261 - without S/MIME, TCP and TLS support
    
     * Configuration file \link Config_sip sip.conf \endlink
    
     * \todo SIP over TCP
     * \todo SIP over TLS
     * \todo Better support of forking
    
     * \todo VIA branch tag transaction checking
     * \todo Transaction support
    
    Olle Johansson's avatar
    Olle Johansson committed
     * \par Overview of the handling of SIP sessions
     * The SIP channel handles several types of SIP sessions, or dialogs,
     * not all of them being "telephone calls".
     * - Incoming calls that will be sent to the PBX core
     * - Outgoing calls, generated by the PBX
     * - SIP subscriptions and notifications of states and voicemail messages
     * - SIP registrations, both inbound and outbound
     * - SIP peer management (peerpoke, OPTIONS)
     * - SIP text messages
     *
     * In the SIP channel, there's a list of active SIP dialogs, which includes
     * all of these when they are active. "sip show channels" in the CLI will
     * show most of these, excluding subscriptions which are shown by
     * "sip show subscriptions"
     *
     * \par incoming packets
     * Incoming packets are received in the monitoring thread, then handled by
     * sipsock_read(). This function parses the packet and matches an existing
     * dialog or starts a new SIP dialog.
     * 
    
     * sipsock_read sends the packet to handle_incoming(), that parses a bit more.
     * If it is a response to an outbound request, the packet is sent to handle_response().
     * If it is a request, handle_incoming() sends it to one of a list of functions
    
    Olle Johansson's avatar
    Olle Johansson committed
     * depending on the request type - INVITE, OPTIONS, REFER, BYE, CANCEL etc
    
     * sipsock_read locks the ast_channel if it exists (an active call) and
     * unlocks it after we have processed the SIP message.
    
    Olle Johansson's avatar
    Olle Johansson committed
     *
     * A new INVITE is sent to handle_request_invite(), that will end up
     * starting a new channel in the PBX, the new channel after that executing
     * in a separate channel thread. This is an incoming "call".
     * When the call is answered, either by a bridged channel or the PBX itself
     * the sip_answer() function is called.
     *
     * The actual media - Video or Audio - is mostly handled by the RTP subsystem
     * in rtp.c 
     * 
     * \par Outbound calls
     * Outbound calls are set up by the PBX through the sip_request_call()
     * function. After that, they are activated by sip_call().
     * 
     * \par Hanging up
     * The PBX issues a hangup on both incoming and outgoing calls through
     * the sip_hangup() function
    
    /*** MODULEINFO
            <depend>res_features</depend>
     ***/
    
    
    #include "asterisk.h"
    
    ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
    
    
    Mark Spencer's avatar
    Mark Spencer committed
    #include <ctype.h>
    
    #include <sys/ioctl.h>
    #include <fcntl.h>
    #include <signal.h>
    #include <sys/signal.h>
    #include <regex.h>
    
    
    #include "asterisk/network.h"
    
    #include "asterisk/paths.h"	/* need ast_config_AST_SYSTEM_NAME */
    
    #include "asterisk/lock.h"
    #include "asterisk/channel.h"
    #include "asterisk/config.h"
    #include "asterisk/module.h"
    #include "asterisk/pbx.h"
    #include "asterisk/sched.h"
    #include "asterisk/io.h"
    #include "asterisk/rtp.h"
    
    #include "asterisk/acl.h"
    #include "asterisk/manager.h"
    #include "asterisk/callerid.h"
    #include "asterisk/cli.h"
    #include "asterisk/app.h"
    #include "asterisk/musiconhold.h"
    #include "asterisk/dsp.h"
    #include "asterisk/features.h"
    #include "asterisk/srv.h"
    #include "asterisk/astdb.h"
    #include "asterisk/causes.h"
    #include "asterisk/utils.h"
    #include "asterisk/file.h"
    #include "asterisk/astobj.h"
    #include "asterisk/dnsmgr.h"
    
    #include "asterisk/linkedlists.h"
    
    #include "asterisk/stringfields.h"
    
    #include "asterisk/monitor.h"
    
    #include "asterisk/threadstorage.h"
    
    #include "asterisk/translate.h"
    
    #include "asterisk/event.h"
    
    #ifndef FALSE
    
    #endif
    
    #ifndef TRUE
    
    #define XMIT_ERROR		-2
    
    
    Mark Spencer's avatar
    Mark Spencer committed
    /* #define VOCAL_DATA_HACK */
    
    Mark Spencer's avatar
    Mark Spencer committed
    #define DEFAULT_DEFAULT_EXPIRY  120
    
    #define DEFAULT_MIN_EXPIRY      60
    #define DEFAULT_MAX_EXPIRY      3600
    #define DEFAULT_REGISTRATION_TIMEOUT 20
    #define DEFAULT_MAX_FORWARDS    "70"
    
    
    /* guard limit must be larger than guard secs */
    
    /* guard min must be < 1000, and should be >= 250 */
    
    #define EXPIRY_GUARD_SECS       15                /*!< How long before expiry do we reregister */
    #define EXPIRY_GUARD_LIMIT      30                /*!< Below here, we use EXPIRY_GUARD_PCT instead of 
    
    	                                                 EXPIRY_GUARD_SECS */
    
    #define EXPIRY_GUARD_MIN        500                /*!< This is the minimum guard time applied. If 
                                                       GUARD_PCT turns out to be lower than this, it 
                                                       will use this time instead.
                                                       This is in milliseconds. */
    #define EXPIRY_GUARD_PCT        0.20                /*!< Percentage of expires timeout to use when 
                                                        below EXPIRY_GUARD_LIMIT */
    #define DEFAULT_EXPIRY 900                          /*!< Expire slowly */
    
    static int min_expiry = DEFAULT_MIN_EXPIRY;        /*!< Minimum accepted registration time */
    static int max_expiry = DEFAULT_MAX_EXPIRY;        /*!< Maximum accepted registration time */
    
    static int default_expiry = DEFAULT_DEFAULT_EXPIRY;
    
    static int expiry = DEFAULT_EXPIRY;
    
    
    #ifndef MAX
    #define MAX(a,b) ((a) > (b) ? (a) : (b))
    #endif
    
    #define CALLERID_UNKNOWN        "Unknown"
    
    #define DEFAULT_MAXMS                2000             /*!< Qualification: Must be faster than 2 seconds by default */
    
    #define DEFAULT_QUALIFYFREQ          60 * 1000        /*!< Qualification: How often to check for the host to be up */
    
    #define DEFAULT_FREQ_NOTOK           10 * 1000        /*!< Qualification: How often to check, if the host is down... */
    
    #define DEFAULT_RETRANS              1000             /*!< How frequently to retransmit Default: 2 * 500 ms in RFC 3261 */
    #define MAX_RETRANS                  6                /*!< Try only 6 times for retransmissions, a total of 7 transmissions */
    
    #define SIP_TIMER_T1                 500              /* SIP timer T1 (according to RFC 3261) */
    #define SIP_TRANS_TIMEOUT            64 * SIP_TIMER_T1/*!< SIP request timeout (rfc 3261) 64*T1 
    
                                                          \todo Use known T1 for timeout (peerpoke)
                                                          */
    
    #define DEFAULT_TRANS_TIMEOUT        -1               /* Use default SIP transaction timeout */
    
    #define MAX_AUTHTRIES                3                /*!< Try authentication three times, then fail */
    
    #define SIP_MAX_HEADERS              64               /*!< Max amount of SIP headers to read */
    #define SIP_MAX_LINES                64               /*!< Max amount of lines in SIP attachment (like SDP) */
    #define SIP_MAX_PACKET               4096             /*!< Also from RFC 3261 (2543), should sub headers tho */
    
    #define INITIAL_CSEQ                 101              /*!< our initial sip sequence number */
    
    /*! \brief Global jitterbuffer configuration - by default, jb is disabled */
    
    static struct ast_jb_conf default_jbconf =
    {
    
    	.flags = 0,
    
    	.max_size = -1,
    	.resync_threshold = -1,
    	.impl = ""
    };
    
    static struct ast_jb_conf global_jbconf;	/*!< Global jitterbuffer configuration */
    
    static const char config[] = "sip.conf";	/*!< Main configuration file */
    static const char notify_config[] = "sip_notify.conf";	/*!< Configuration file for sending Notify with CLI commands to reconfigure or reboot phones */
    
    /*! \brief Authorization scheme for call transfers 
    \note Not a bitfield flag, since there are plans for other modes,
    	like "only allow transfers for authenticated devices" */
    enum transfermodes {
    
    	TRANSFER_OPENFORALL,            /*!< Allow all SIP transfers */
    	TRANSFER_CLOSED,                /*!< Allow no SIP transfers */
    
    /*! \brief States for the INVITE transaction, not the dialog 
    	\note this is for the INVITE that sets up the dialog
    */
    enum invitestates {
    
    	INV_NONE = 0,	        /*!< No state at all, maybe not an INVITE dialog */
    	INV_CALLING = 1,	/*!< Invite sent, no answer */
    	INV_PROCEEDING = 2,	/*!< We got/sent 1xx message */
    	INV_EARLY_MEDIA = 3,    /*!< We got 18x message with to-tag back */
    	INV_COMPLETED = 4,	/*!< Got final response with error. Wait for ACK, then CONFIRMED */
    	INV_CONFIRMED = 5,	/*!< Confirmed response - we've got an ack (Incoming calls only) */
    	INV_TERMINATED = 6,	/*!< Transaction done - either successful (AST_STATE_UP) or failed, but done 
    				     The only way out of this is a BYE from one side */
    	INV_CANCELLED = 7,	/*!< Transaction cancelled by client or server in non-terminated state */
    
    	XMIT_CRITICAL = 2,              /*!< Transmit critical SIP message reliably, with re-transmits.
                                                  If it fails, it's critical and will cause a teardown of the session */
    	XMIT_RELIABLE = 1,              /*!< Transmit SIP message reliably, with re-transmits */
    	XMIT_UNRELIABLE = 0,            /*!< Transmit SIP message without bothering with re-transmits */
    };
    
    enum parse_register_result {
    	PARSE_REGISTER_FAILED,
    	PARSE_REGISTER_UPDATE,
    	PARSE_REGISTER_QUERY,
    
    enum subscriptiontype { 
    	NONE = 0,
    	XPIDF_XML,
    	DIALOG_INFO_XML,
    	CPIM_PIDF_XML,
    
    Olle Johansson's avatar
    Olle Johansson committed
    /*! \brief Subscription types that we support. We support
       - dialoginfo updates (really device status, not dialog info as was the original intent of the standard)
       - SIMPLE presence used for device status
       - Voicemail notification subscriptions
    */
    
    static const struct cfsubscription_types {
    	enum subscriptiontype type;
    	const char * const event;
    	const char * const mediatype;
    	const char * const text;
    } subscription_types[] = {
    
    Olle Johansson's avatar
    Olle Johansson committed
    	{ NONE,		   "-",        "unknown",	             "unknown" },
    
    Olle Johansson's avatar
    Olle Johansson committed
     	/* RFC 4235: SIP Dialog event package */
    
    	{ DIALOG_INFO_XML, "dialog",   "application/dialog-info+xml", "dialog-info+xml" },
    	{ CPIM_PIDF_XML,   "presence", "application/cpim-pidf+xml",   "cpim-pidf+xml" },  /* RFC 3863 */
    	{ PIDF_XML,        "presence", "application/pidf+xml",        "pidf+xml" },       /* RFC 3863 */
    
    	{ XPIDF_XML,       "presence", "application/xpidf+xml",       "xpidf+xml" },       /* Pre-RFC 3863 with MS additions */
    
    	{ MWI_NOTIFICATION,	"message-summary", "application/simple-message-summary", "mwi" } /* RFC 3842: Mailbox notification */
    
    /*! \brief Authentication types - proxy or www authentication 
    	\note Endpoints, like Asterisk, should always use WWW authentication to
    	allow multiple authentications in the same call - to the proxy and
    	to the end point.
    */
    
    enum sip_auth_type {
    
    /*! \brief Authentication result from check_auth* functions */
    enum check_auth_result {
    
    	AUTH_DONT_KNOW = -100,	/*!< no result, need to check further */
    		/* XXX maybe this is the same as AUTH_NOT_FOUND */
    
    
    	AUTH_SUCCESSFUL = 0,
    	AUTH_CHALLENGE_SENT = 1,
    	AUTH_SECRET_FAILED = -1,
    	AUTH_USERNAME_MISMATCH = -2,
    
    	AUTH_NOT_FOUND = -3,	/*!< returned by register_verify */
    
    	AUTH_FAKE_AUTH = -4,
    	AUTH_UNKNOWN_DOMAIN = -5,
    
    	AUTH_PEER_NOT_DYNAMIC = -6,
    	AUTH_ACL_FAILED = -7,
    
    /*! \brief States for outbound registrations (with register= lines in sip.conf */
    
    Olle Johansson's avatar
    Olle Johansson committed
    enum sipregistrystate {
    	REG_STATE_UNREGISTERED = 0,	/*!< We are not registred */
    
    		/* Initial state. We should have a timeout scheduled for the initial
    		 * (or next) registration transmission, calling sip_reregister
    		 */
    
    
    Olle Johansson's avatar
    Olle Johansson committed
    	REG_STATE_REGSENT,	/*!< Registration request sent */
    
    		/* sent initial request, waiting for an ack or a timeout to
    		 * retransmit the initial request.
    		*/
    
    
    Olle Johansson's avatar
    Olle Johansson committed
    	REG_STATE_AUTHSENT,	/*!< We have tried to authenticate */
    
    		/* entered after transmit_register with auth info,
    		 * waiting for an ack.
    		 */
    
    
    	REG_STATE_REGISTERED,	/*!< Registered and done */
    
    Olle Johansson's avatar
    Olle Johansson committed
    	REG_STATE_REJECTED,	/*!< Registration rejected */
    
    		/* only used when the remote party has an expire larger than
    		 * our max-expire. This is a final state from which we do not
    		 * recover (not sure how correctly).
    		 */
    
    
    Olle Johansson's avatar
    Olle Johansson committed
    	REG_STATE_TIMEOUT,	/*!< Registration timed out */
    
    Olle Johansson's avatar
    Olle Johansson committed
    	REG_STATE_NOAUTH,	/*!< We have no accepted credentials */
    
    		/* fatal - no chance to proceed */
    
    
    Olle Johansson's avatar
    Olle Johansson committed
    	REG_STATE_FAILED,	/*!< Registration failed after several tries */
    
    		/* fatal - no chance to proceed */
    
    /*! \brief definition of a sip proxy server
     *
     * For outbound proxies, this is allocated in the SIP peer dynamically or
     * statically as the global_outboundproxy. The pointer in a SIP message is just
     * a pointer and should *not* be de-allocated.
     */
    struct sip_proxy {
    	char name[MAXHOSTNAMELEN];      /*!< DNS name of domain/host or IP */
    	struct sockaddr_in ip;          /*!< Currently used IP address and port */
    	time_t last_dnsupdate;          /*!< When this was resolved */
    	int force;                      /*!< If it's an outbound proxy, Force use of this outbound proxy for all outbound requests */
    	/* Room for a SRV record chain based on the name */
    };
    
    
    /*! \brief States whether a SIP message can create a dialog in Asterisk. */
    
    enum can_create_dialog {
    	CAN_NOT_CREATE_DIALOG,
    	CAN_CREATE_DIALOG,
    	CAN_CREATE_DIALOG_UNSUPPORTED_METHOD,
    };
    
    /*! \brief SIP Request methods known by Asterisk 
    
       \note Do _NOT_ make any changes to this enum, or the array following it;
       if you think you are doing the right thing, you are probably
       not doing the right thing. If you think there are changes
       needed, get someone else to review them first _before_
       submitting a patch. If these two lists do not match properly
       bad things will happen.
    */
    
    enum sipmethod {
    	SIP_UNKNOWN,		/*!< Unknown response */
    	SIP_RESPONSE,		/*!< Not request, response to outbound request */
    	SIP_REGISTER,		/*!< Registration to the mothership, tell us where you are located */
    	SIP_OPTIONS,		/*!< Check capabilities of a device, used for "ping" too */
    	SIP_NOTIFY,		/*!< Status update, Part of the event package standard, result of a SUBSCRIBE or a REFER */
    	SIP_INVITE,		/*!< Set up a session */
    	SIP_ACK,		/*!< End of a three-way handshake started with INVITE. */
    	SIP_PRACK,		/*!< Reliable pre-call signalling. Not supported in Asterisk. */
    	SIP_BYE,		/*!< End of a session */
    	SIP_REFER,		/*!< Refer to another URI (transfer) */
    	SIP_SUBSCRIBE,		/*!< Subscribe for updates (voicemail, session status, device status, presence) */
    	SIP_MESSAGE,		/*!< Text messaging */
    	SIP_UPDATE,		/*!< Update a dialog. We can send UPDATE; but not accept it */
    	SIP_INFO,		/*!< Information updates during a session */
    	SIP_CANCEL,		/*!< Cancel an INVITE */
    	SIP_PUBLISH,		/*!< Not supported in Asterisk */
    	SIP_PING,		/*!< Not supported at all, no standard but still implemented out there */
    };
    
    /*! \brief The core structure to setup dialogs. We parse incoming messages by using
    	structure and then route the messages according to the type.
    
          \note Note that sip_methods[i].id == i must hold or the code breaks */
    
    Kevin P. Fleming's avatar
    Kevin P. Fleming committed
    static const struct  cfsip_methods { 
    
    	int need_rtp;		/*!< when this is the 'primary' use for a pvt structure, does it need RTP? */
    
    Kevin P. Fleming's avatar
    Kevin P. Fleming committed
    	char * const text;
    
    	{ SIP_UNKNOWN,	 RTP,    "-UNKNOWN-", 	CAN_CREATE_DIALOG },
    	{ SIP_RESPONSE,	 NO_RTP, "SIP/2.0",	CAN_NOT_CREATE_DIALOG },
    	{ SIP_REGISTER,	 NO_RTP, "REGISTER", 	CAN_CREATE_DIALOG },
     	{ SIP_OPTIONS,	 NO_RTP, "OPTIONS", 	CAN_CREATE_DIALOG },
    	{ SIP_NOTIFY,	 NO_RTP, "NOTIFY", 	CAN_CREATE_DIALOG },
    	{ SIP_INVITE,	 RTP,    "INVITE", 	CAN_CREATE_DIALOG },
    	{ SIP_ACK,	 NO_RTP, "ACK", 	CAN_NOT_CREATE_DIALOG },
    	{ SIP_PRACK,	 NO_RTP, "PRACK", 	CAN_NOT_CREATE_DIALOG },
    	{ SIP_BYE,	 NO_RTP, "BYE", 	CAN_NOT_CREATE_DIALOG },
    	{ SIP_REFER,	 NO_RTP, "REFER", 	CAN_CREATE_DIALOG },
    	{ SIP_SUBSCRIBE, NO_RTP, "SUBSCRIBE", 	CAN_CREATE_DIALOG },
    	{ SIP_MESSAGE,	 NO_RTP, "MESSAGE", 	CAN_CREATE_DIALOG },
    	{ SIP_UPDATE,	 NO_RTP, "UPDATE", 	CAN_NOT_CREATE_DIALOG },
    	{ SIP_INFO,	 NO_RTP, "INFO", 	CAN_NOT_CREATE_DIALOG },
    	{ SIP_CANCEL,	 NO_RTP, "CANCEL", 	CAN_NOT_CREATE_DIALOG },
    	{ SIP_PUBLISH,	 NO_RTP, "PUBLISH", 	CAN_CREATE_DIALOG_UNSUPPORTED_METHOD },
    	{ SIP_PING,	 NO_RTP, "PING", 	CAN_CREATE_DIALOG_UNSUPPORTED_METHOD }
    
    /*!  Define SIP option tags, used in Require: and Supported: headers 
     	We need to be aware of these properties in the phones to use 
    
    	the replace: header. We should not do that without knowing
    	that the other end supports it... 
    	This is nothing we can configure, we learn by the dialog
    	Supported: header on the REGISTER (peer) or the INVITE
    	(other devices)
    	We are not using many of these today, but will in the future.
    	This is documented in RFC 3261
    */
    #define SUPPORTED		1
    #define NOT_SUPPORTED		0
    
    
    /* SIP options */
    
    #define SIP_OPT_REPLACES	(1 << 0)
    #define SIP_OPT_100REL		(1 << 1)
    #define SIP_OPT_TIMER		(1 << 2)
    #define SIP_OPT_EARLY_SESSION	(1 << 3)
    #define SIP_OPT_JOIN		(1 << 4)
    #define SIP_OPT_PATH		(1 << 5)
    #define SIP_OPT_PREF		(1 << 6)
    #define SIP_OPT_PRECONDITION	(1 << 7)
    #define SIP_OPT_PRIVACY		(1 << 8)
    #define SIP_OPT_SDP_ANAT	(1 << 9)
    #define SIP_OPT_SEC_AGREE	(1 << 10)
    #define SIP_OPT_EVENTLIST	(1 << 11)
    #define SIP_OPT_GRUU		(1 << 12)
    #define SIP_OPT_TARGET_DIALOG	(1 << 13)
    
    #define SIP_OPT_NOREFERSUB	(1 << 14)
    #define SIP_OPT_HISTINFO	(1 << 15)
    #define SIP_OPT_RESPRIORITY	(1 << 16)
    
    /*! \brief List of well-known SIP options. If we get this in a require,
    
       we should check the list and answer accordingly. */
    
    Kevin P. Fleming's avatar
    Kevin P. Fleming committed
    static const struct cfsip_options {
    
    	int id;			/*!< Bitmap ID */
    	int supported;		/*!< Supported by Asterisk ? */
    	char * const text;	/*!< Text id, as in standard */
    
    } sip_options[] = {	/* XXX used in 3 places */
    
    	/* RFC3891: Replaces: header for transfer */
    
    	{ SIP_OPT_REPLACES,	SUPPORTED,	"replaces" },	
    
    Olle Johansson's avatar
    Olle Johansson committed
    	/* One version of Polycom firmware has the wrong label */
    	{ SIP_OPT_REPLACES,	SUPPORTED,	"replace" },	
    
    	/* RFC3262: PRACK 100% reliability */
    	{ SIP_OPT_100REL,	NOT_SUPPORTED,	"100rel" },	
    
    	/* RFC4028: SIP Session Timers */
    
    	{ SIP_OPT_TIMER,	NOT_SUPPORTED,	"timer" },
    	/* RFC3959: SIP Early session support */
    	{ SIP_OPT_EARLY_SESSION, NOT_SUPPORTED,	"early-session" },
    
    	/* RFC3911: SIP Join header support */
    
    	{ SIP_OPT_JOIN,		NOT_SUPPORTED,	"join" },
    	/* RFC3327: Path support */
    	{ SIP_OPT_PATH,		NOT_SUPPORTED,	"path" },
    	/* RFC3840: Callee preferences */
    	{ SIP_OPT_PREF,		NOT_SUPPORTED,	"pref" },
    	/* RFC3312: Precondition support */
    	{ SIP_OPT_PRECONDITION,	NOT_SUPPORTED,	"precondition" },
    	/* RFC3323: Privacy with proxies*/
    	{ SIP_OPT_PRIVACY,	NOT_SUPPORTED,	"privacy" },
    
    	/* RFC4092: Usage of the SDP ANAT Semantics in the SIP */
    	{ SIP_OPT_SDP_ANAT,	NOT_SUPPORTED,	"sdp-anat" },
    
    	/* RFC3329: Security agreement mechanism */
    	{ SIP_OPT_SEC_AGREE,	NOT_SUPPORTED,	"sec_agree" },
    
    	/* SIMPLE events:  RFC4662 */
    
    	{ SIP_OPT_EVENTLIST,	NOT_SUPPORTED,	"eventlist" },
    	/* GRUU: Globally Routable User Agent URI's */
    	{ SIP_OPT_GRUU,		NOT_SUPPORTED,	"gruu" },
    
    	/* RFC4538: Target-dialog */
    
    	{ SIP_OPT_TARGET_DIALOG,NOT_SUPPORTED,	"tdialog" },
    	/* Disable the REFER subscription, RFC 4488 */
    	{ SIP_OPT_NOREFERSUB,	NOT_SUPPORTED,	"norefersub" },
    	/* ietf-sip-history-info-06.txt */
    	{ SIP_OPT_HISTINFO,	NOT_SUPPORTED,	"histinfo" },
    	/* ietf-sip-resource-priority-10.txt */
    	{ SIP_OPT_RESPRIORITY,	NOT_SUPPORTED,	"resource-priority" },
    
    Olle Johansson's avatar
    Olle Johansson committed
    /*! \brief SIP Methods we support 
    	\todo This string should be set dynamically. We only support REFER and SUBSCRIBE is we have
    	allowsubscribe and allowrefer on in sip.conf.
    */
    
    #define ALLOWED_METHODS "INVITE, ACK, CANCEL, OPTIONS, BYE, REFER, SUBSCRIBE, NOTIFY"
    
    /*! \brief SIP Extensions we support */
    
    /*! \brief Standard SIP port from RFC 3261. DO NOT CHANGE THIS */
    #define STANDARD_SIP_PORT	5060
    /* Note: in many SIP headers, absence of a port number implies port 5060,
     * and this is why we cannot change the above constant.
     * There is a limited number of places in asterisk where we could,
     * in principle, use a different "default" port number, but
     * we do not support this feature at the moment.
    
     * You can run Asterisk with SIP on a different port with a configuration
     * option. If you change this value, the signalling will be incorrect.
    
    /*! \name DefaultValues Default values, set and reset in reload_config before reading configuration 
    
    Olle Johansson's avatar
    Olle Johansson committed
    
       These are default values in the source. There are other recommended values in the
    
       sip.conf.sample for new installations. These may differ to keep backwards compatibility,
       yet encouraging new behaviour on new installations 
     */
    
    #define DEFAULT_CONTEXT		"default"
    
    #define DEFAULT_MOHINTERPRET    "default"
    #define DEFAULT_MOHSUGGEST      ""
    
    #define DEFAULT_VMEXTEN 	"asterisk"
    #define DEFAULT_CALLERID 	"asterisk"
    #define DEFAULT_NOTIFYMIME 	"application/simple-message-summary"
    #define DEFAULT_ALLOWGUEST	TRUE
    
    #define DEFAULT_SRVLOOKUP	TRUE		/*!< Recommended setting is ON */
    
    #define DEFAULT_COMPACTHEADERS	FALSE
    
    #define DEFAULT_TOS_SIP         0               /*!< Call signalling packets should be marked as DSCP CS3, but the default is 0 to be compatible with previous versions. */
    #define DEFAULT_TOS_AUDIO       0               /*!< Audio packets should be marked as DSCP EF (Expedited Forwarding), but the default is 0 to be compatible with previous versions. */
    #define DEFAULT_TOS_VIDEO       0               /*!< Video packets should be marked as DSCP AF41, but the default is 0 to be compatible with previous versions. */
    
    #define DEFAULT_TOS_TEXT        0               /*!< Text packets should be marked as XXXX XXXX, but the default is 0 to be compatible with previous versions. */
    
    #define DEFAULT_COS_SIP         4
    #define DEFAULT_COS_AUDIO       5
    #define DEFAULT_COS_VIDEO       6
    
    #define DEFAULT_COS_TEXT        5
    
    #define DEFAULT_ALLOW_EXT_DOM	TRUE
    #define DEFAULT_REALM		"asterisk"
    #define DEFAULT_NOTIFYRINGING	TRUE
    #define DEFAULT_PEDANTIC	FALSE
    #define DEFAULT_AUTOCREATEPEER	FALSE
    #define DEFAULT_QUALIFY		FALSE
    
    #define DEFAULT_T1MIN		100		/*!< 100 MS for minimal roundtrip time */
    
    Olle Johansson's avatar
    Olle Johansson committed
    #define DEFAULT_MAX_CALL_BITRATE (384)		/*!< Max bitrate for video */
    
    #ifndef DEFAULT_USERAGENT
    #define DEFAULT_USERAGENT "Asterisk PBX"	/*!< Default Useragent: header unless re-defined in sip.conf */
    
    #define DEFAULT_SDPSESSION "Asterisk PBX"	/*!< Default SDP session name, (s=) header unless re-defined in sip.conf */
    #define DEFAULT_SDPOWNER "root"		/*!< Default SDP username field in (o=) header unless re-defined in sip.conf */
    
    #endif
    
    /*! \name DefaultSettings
    	Default setttings are used as a channel setting and as a default when
    	configuring devices 
    */
    /*@{*/ 
    
    static char default_context[AST_MAX_CONTEXT];
    
    static char default_subscribecontext[AST_MAX_CONTEXT];
    
    static char default_language[MAX_LANGUAGE];
    static char default_callerid[AST_MAX_EXTENSION];
    static char default_fromdomain[AST_MAX_EXTENSION];
    static char default_notifymime[AST_MAX_EXTENSION];
    static int default_qualify;		/*!< Default Qualify= setting */
    
    static char default_vmexten[AST_MAX_EXTENSION];
    
    static char default_mohinterpret[MAX_MUSICCLASS];  /*!< Global setting for moh class to use when put on hold */
    static char default_mohsuggest[MAX_MUSICCLASS];	   /*!< Global setting for moh class to suggest when putting 
                                                        *   a bridged channel on hold */
    
    Olle Johansson's avatar
    Olle Johansson committed
    static int default_maxcallbitrate;	/*!< Maximum bitrate for call */
    
    static struct ast_codec_pref default_prefs;		/*!< Default codec prefs */
    
    /*! \brief a place to store all global settings for the sip channel driver */
    struct sip_settings {
    	int peer_rtupdate;	/*!< G: Update database with registration data for peer? */
    	int rtsave_sysname;	/*!< G: Save system name at registration? */
    	int ignore_regexpire;	/*!< G: Ignore expiration of peer  */
    };
    
    static struct sip_settings sip_cfg;
    
    /*! \name GlobalSettings
    	Global settings apply to the channel (often settings you can change in the general section
    	of sip.conf
    */
    /*@{*/ 
    
    static int global_directrtpsetup;	/*!< Enable support for Direct RTP setup (no re-invites) */
    
    static int global_limitonpeers;		/*!< Match call limit on peers only */
    
    static int global_rtautoclear;		/*!< Realtime ?? */
    
    static int global_notifyringing;	/*!< Send notifications on ringing */
    
    static int global_notifyhold;		/*!< Send notifications on hold */
    
    static int global_alwaysauthreject;	/*!< Send 401 Unauthorized for all failing requests */
    
    static int global_srvlookup;		/*!< SRV Lookup on or off. Default is on */
    
    static int pedanticsipchecking;		/*!< Extra checking ?  Default off */
    static int autocreatepeer;		/*!< Auto creation of peers at registration? Default off. */
    
    static int global_match_auth_username;		/*!< Match auth username if available instead of From: Default off. */
    
    static int global_relaxdtmf;		/*!< Relax DTMF */
    
    static int global_rtptimeout;		/*!< Time out call if no RTP */
    
    static int global_rtpholdtimeout;	/*!< Time out call if no RTP during hold */
    
    static int global_rtpkeepalive;		/*!< Send RTP keepalives */
    static int global_reg_timeout;	
    static int global_regattempts_max;	/*!< Registration attempts before giving up */
    
    static int global_allowguest;		/*!< allow unauthenticated users/peers to connect? */
    
    static int global_callcounter;		/*!< Enable call counters for all devices. This is currently enabled by setting the peer
    						call-limit to 999. When we remove the call-limit from the code, we can make it
    						with just a boolean flag in the device structure */
    
    Olle Johansson's avatar
    Olle Johansson committed
    static int global_allowsubscribe;	/*!< Flag for disabling ALL subscriptions, this is FALSE only if all peers are FALSE 
    
    static unsigned int global_tos_sip;		/*!< IP type of service for SIP packets */
    static unsigned int global_tos_audio;		/*!< IP type of service for audio RTP packets */
    static unsigned int global_tos_video;		/*!< IP type of service for video RTP packets */
    
    static unsigned int global_tos_text;		/*!< IP type of service for text RTP packets */
    
    static unsigned int global_cos_sip;		/*!< 802.1p class of service for SIP packets */
    static unsigned int global_cos_audio;		/*!< 802.1p class of service for audio RTP packets */
    static unsigned int global_cos_video;		/*!< 802.1p class of service for video RTP packets */
    static unsigned int global_cos_text;		/*!< 802.1p class of service for text RTP packets */
    
    static int compactheaders;		/*!< send compact sip headers */
    static int recordhistory;		/*!< Record SIP history. Off by default */
    static int dumphistory;			/*!< Dump history to verbose before destroying SIP dialog */
    static char global_realm[MAXHOSTNAMELEN]; 		/*!< Default realm */
    
    static char global_regcontext[AST_MAX_CONTEXT];		/*!< Context for auto-extensions */
    
    static char global_useragent[AST_MAX_EXTENSION];	/*!< Useragent for the SIP channel */
    
    static char global_sdpsession[AST_MAX_EXTENSION];	/*!< SDP session name for the SIP channel */
    static char global_sdpowner[AST_MAX_EXTENSION];	/*!< SDP owner name for the SIP channel */
    
    static int allow_external_domains;	/*!< Accept calls to external SIP domains? */
    
    static int global_callevents;		/*!< Whether we send manager events or not */
    
    static int global_t1;			/*!< T1 time */
    
    static int global_t1min;		/*!< T1 roundtrip time minimum */
    
    static int global_timer_b;    /*!< Timer B - RFC 3261 Section 17.1.1.2 */
    
    static int global_regextenonqualify;  /*!< Whether to add/remove regexten when qualifying peers */
    
    static int global_autoframing;          /*!< Turn autoframing on or off. */
    
    static enum transfermodes global_allowtransfer;	/*!< SIP Refer restriction scheme */
    
    static struct sip_proxy global_outboundproxy;	/*!< Outbound proxy */
    
    static int global_matchexterniplocally; /*!< Match externip/externhost setting against localnet setting */
    
    static int global_qualifyfreq; /*!< Qualify frequency */
    
    
    /*! \brief Codecs that we support by default: */
    static int global_capability = AST_FORMAT_ULAW | AST_FORMAT_ALAW | AST_FORMAT_GSM | AST_FORMAT_H263;
    
    static int suserobjs = 0;                /*!< Static users */
    static int ruserobjs = 0;                /*!< Realtime users */
    static int speerobjs = 0;                /*!< Statis peers */
    static int rpeerobjs = 0;                /*!< Realtime peers */
    static int apeerobjs = 0;                /*!< Autocreated peer objects */
    static int regobjs = 0;                  /*!< Registry objects */
    
    static struct ast_flags global_flags[2] = {{0}};        /*!< global SIP_ flags */
    
    static char used_context[AST_MAX_CONTEXT]; /*!< name of automatically created context for unloading */
    
    AST_MUTEX_DEFINE_STATIC(netlock);
    
    
    /*! \brief Protect the monitoring thread, so only one process can kill or start it, and not
    
    Mark Spencer's avatar
    Mark Spencer committed
       when it's doing something critical. */
    
    
    AST_MUTEX_DEFINE_STATIC(monlock);
    
    AST_MUTEX_DEFINE_STATIC(sip_reload_lock);
    
    
    /*! \brief This is the thread for the monitor which checks for input on the channels
    
    Mark Spencer's avatar
    Mark Spencer committed
       which are not currently in use.  */
    
    static pthread_t monitor_thread = AST_PTHREADT_NULL;
    
    static int sip_reloading = FALSE;                       /*!< Flag for avoiding multiple reloads at the same time */
    static enum channelreloadreason sip_reloadreason;       /*!< Reason for last reload/load of configuration */
    
    static struct sched_context *sched;     /*!< The scheduling context */
    static struct io_context *io;           /*!< The IO context */
    
    static int *sipsock_read_id;            /*!< ID of IO entry for sipsock FD */
    
    #define DEC_CALL_LIMIT	0
    #define INC_CALL_LIMIT	1
    
    #define DEC_CALL_RINGING 2
    #define INC_CALL_RINGING 3
    
    Jason Parker's avatar
    Jason Parker committed
    /*! \brief The data grabbed from the UDP socket
     * \verbatim
    
     * Incoming messages: we first store the data from the socket in data[],
     * adding a trailing \0 to make string parsing routines happy.
     * Then call parse_request() and req.method = find_sip_method();
     * to initialize the other fields. The \r\n at the end of each line is   
     * replaced by \0, so that data[] is not a conforming SIP message anymore.
     * After this processing, rlPart1 is set to non-NULL to remember
     * that we can run get_header() on this kind of packet.
     *
     * parse_request() splits the first line as follows:
     * Requests have in the first line      method uri SIP/2.0
     *      rlPart1 = method; rlPart2 = uri;
     * Responses have in the first line     SIP/2.0 NNN description
     *      rlPart1 = SIP/2.0; rlPart2 = NNN + description;
     *
     * For outgoing packets, we initialize the fields with init_req() or init_resp()
     * (which fills the first line to "METHOD uri SIP/2.0" or "SIP/2.0 code text"),
     * and then fill the rest with add_header() and add_line().
     * The \r\n at the end of the line are still there, so the get_header()
     * and similar functions don't work on these packets. 
    
    Jason Parker's avatar
    Jason Parker committed
     * \endverbatim
    
    Mark Spencer's avatar
    Mark Spencer committed
    struct sip_request {
    
    	char *rlPart1; 	        /*!< SIP Method Name or "SIP/2.0" protocol version */
    	char *rlPart2; 	        /*!< The Request URI or Response Status */
    
    Jason Parker's avatar
    Jason Parker committed
    	int len;                /*!< bytes used in data[], excluding trailing null terminator. Rarely used. */
    
    	int headers;            /*!< # of SIP Headers */
    	int method;             /*!< Method of this request */
    	int lines;              /*!< Body Content */
    
    	unsigned int sdp_start; /*!< the line number where the SDP begins */
    	unsigned int sdp_end;   /*!< the line number where the SDP ends */
    	char debug;		/*!< print extra debugging if non zero */
    	char has_to_tag;	/*!< non-zero if packet has To: tag */
    	char ignore;		/*!< if non-zero This is a re-transmit, ignore it */
    
    Luigi Rizzo's avatar
    Luigi Rizzo committed
    	char *header[SIP_MAX_HEADERS];
    
    Mark Spencer's avatar
    Mark Spencer committed
    	char *line[SIP_MAX_LINES];
    	char data[SIP_MAX_PACKET];
    };
    
    
    /*! \brief structure used in transfers */
    struct sip_dual {
    
    	struct ast_channel *chan1;	/*!< First channel involved */
    	struct ast_channel *chan2;	/*!< Second channel involved */
    	struct sip_request req;		/*!< Request that caused the transfer (REFER) */
    	int seqno;			/*!< Sequence number */
    
    Mark Spencer's avatar
    Mark Spencer committed
    struct sip_pkt;
    
    
    /*! \brief Parameters to the transmit_invite function */
    
    Olle Johansson's avatar
    Olle Johansson committed
    	int addsipheaders;		/*!< Add extra SIP headers */
    
    	const char *uri_options;	/*!< URI options to add to the URI */
    	const char *vxml_url;		/*!< VXML url for Cisco phones */
    
    Olle Johansson's avatar
    Olle Johansson committed
    	char *auth;			/*!< Authentication */
    	char *authheader;		/*!< Auth header */
    
    	enum sip_auth_type auth_type;	/*!< Authentication type */
    
    	const char *replaces;		/*!< Replaces header for call transfers */
    	int transfer;			/*!< Flag - is this Invite part of a SIP transfer? (invite/replaces) */
    
    /*! \brief Structure to save routing information for a SIP session */
    
    struct sip_route {
    	struct sip_route *next;
    	char hop[0];
    };
    
    
    /*! \brief Modes for SIP domain handling in the PBX */
    
    Olle Johansson's avatar
    Olle Johansson committed
    	SIP_DOMAIN_AUTO,		/*!< This domain is auto-configured */
    	SIP_DOMAIN_CONFIG,		/*!< This domain is from configuration */
    
    /*! \brief Domain data structure. 
    	\note In the future, we will connect this to a configuration tree specific
    	for this domain
    */
    
    	char domain[MAXHOSTNAMELEN];		/*!< SIP domain we are responsible for */
    	char context[AST_MAX_EXTENSION];	/*!< Incoming context for this domain */
    	enum domain_mode mode;			/*!< How did we find this domain? */
    	AST_LIST_ENTRY(domain) list;		/*!< List mechanics */
    
    static AST_LIST_HEAD_STATIC(domain_list, domain);	/*!< The SIP domain list */
    
    /*! \brief sip_history: Structure for saving transactions within a SIP dialog */
    
    	AST_LIST_ENTRY(sip_history) list;
    	char event[0];	/* actually more, depending on needs */
    
    AST_LIST_HEAD_NOLOCK(sip_history_head, sip_history); /*!< history list, entry in sip_pvt */
    
    
    /*! \brief sip_auth: Credentials for authentication to other SIP services */
    
    struct sip_auth {
    
    	char realm[AST_MAX_EXTENSION];  /*!< Realm in which these credentials are valid */
    
    	char username[256];             /*!< Username */
    	char secret[256];               /*!< Secret */
    	char md5secret[256];            /*!< MD5Secret */
    	struct sip_auth *next;          /*!< Next auth structure in list */
    
    /*! \name SIPflags
    	Various flags for the flags field in the pvt structure 
    
    	Trying to sort these up (one or more of the following):
    	D: Dialog
    	P: Peer/user
    	G: Global flag
    	When flags are used by multiple structures, it is important that
    	they have a common layout so it is easy to copy them.
    
    Olle Johansson's avatar
    Olle Johansson committed
    */
    
    #define SIP_OUTGOING		(1 << 0)	/*!< D: Direction of the last transaction in this dialog */
    #define SIP_RINGING		(1 << 2)	/*!< D: Have sent 180 ringing */
    #define SIP_PROGRESS_SENT	(1 << 3)	/*!< D: Have sent 183 message progress */
    #define SIP_NEEDREINVITE	(1 << 4)	/*!< D: Do we need to send another reinvite? */
    #define SIP_PENDINGBYE		(1 << 5)	/*!< D: Need to send bye after we ack? */
    #define SIP_GOTREFER		(1 << 6)	/*!< D: Got a refer? */
    #define SIP_CALL_LIMIT		(1 << 7)	/*!< D: Call limit enforced for this call */
    #define SIP_INC_COUNT		(1 << 8)	/*!< D: Did this dialog increment the counter of in-use calls? */
    #define SIP_INC_RINGING		(1 << 9)	/*!< D: Did this connection increment the counter of in-use calls? */
    #define SIP_DEFER_BYE_ON_TRANSFER	(1 << 11)	/*!< D: Do not hangup at first ast_hangup */
    
    #define SIP_PROMISCREDIR	(1 << 12)	/*!< DP: Promiscuous redirection */
    #define SIP_TRUSTRPID		(1 << 13)	/*!< DP: Trust RPID headers? */
    #define SIP_USEREQPHONE		(1 << 14)	/*!< DP: Add user=phone to numeric URI. Default off */
    #define SIP_USECLIENTCODE	(1 << 15)	/*!< DP: Trust X-ClientCode info message */
    
    
    /* DTMF flags - see str2dtmfmode() and dtmfmode2str() */
    
    Olle Johansson's avatar
    Olle Johansson committed
    #define SIP_DTMF		(3 << 16)	/*!< DP: DTMF Support: four settings, uses two bits */
    #define SIP_DTMF_RFC2833	(0 << 16)	/*!< DP: DTMF Support: RTP DTMF - "rfc2833" */
    #define SIP_DTMF_INBAND		(1 << 16)	/*!< DP: DTMF Support: Inband audio, only for ULAW/ALAW - "inband" */
    #define SIP_DTMF_INFO		(2 << 16)	/*!< DP: DTMF Support: SIP Info messages - "info" */
    #define SIP_DTMF_AUTO		(3 << 16)	/*!< DP: DTMF Support: AUTO switch between rfc2833 and in-band DTMF */
    
    #define SIP_DTMF_SHORTINFO      (4 << 16)       /*!< DP: DTMF Support: SIP Info messages - "info" - short variant */
    
    Olle Johansson's avatar
    Olle Johansson committed
    #define SIP_NAT			(3 << 18)	/*!< DP: four settings, uses two bits */
    #define SIP_NAT_NEVER		(0 << 18)	/*!< DP: No nat support */
    #define SIP_NAT_RFC3581		(1 << 18)	/*!< DP: NAT RFC3581 */
    #define SIP_NAT_ROUTE		(2 << 18)	/*!< DP: NAT Only ROUTE */
    #define SIP_NAT_ALWAYS		(3 << 18)	/*!< DP: NAT Both ROUTE and RFC3581 */
    
    /* re-INVITE related settings */
    
    Olle Johansson's avatar
    Olle Johansson committed
    #define SIP_REINVITE		(7 << 20)	/*!< DP: three bits used */
    #define SIP_CAN_REINVITE	(1 << 20)	/*!< DP: allow peers to be reinvited to send media directly p2p */
    #define SIP_CAN_REINVITE_NAT	(2 << 20)	/*!< DP: allow media reinvite when new peer is behind NAT */
    #define SIP_REINVITE_UPDATE	(4 << 20)	/*!< DP: use UPDATE (RFC3311) when reinviting this peer */
    
    
    /* "insecure" settings - see insecure2str() */
    
    #define SIP_INSECURE		(3 << 23)	/*!< DP: two bits used */
    
    Olle Johansson's avatar
    Olle Johansson committed
    #define SIP_INSECURE_PORT	(1 << 23)	/*!< DP: don't require matching port for incoming requests */
    #define SIP_INSECURE_INVITE	(1 << 24)	/*!< DP: don't require authentication for incoming INVITEs */
    
    /* Sending PROGRESS in-band settings */
    
    Olle Johansson's avatar
    Olle Johansson committed
    #define SIP_PROG_INBAND		(3 << 25)	/*!< DP: three settings, uses two bits */
    
    #define SIP_PROG_INBAND_NEVER	(0 << 25)
    #define SIP_PROG_INBAND_NO	(1 << 25)
    #define SIP_PROG_INBAND_YES	(2 << 25)
    
    Olle Johansson's avatar
    Olle Johansson committed
    #define SIP_SENDRPID		(1 << 29)	/*!< DP: Remote Party-ID Support */
    #define SIP_G726_NONSTANDARD	(1 << 31)	/*!< DP: Use non-standard packing for G726-32 data */
    
    Olle Johansson's avatar
    Olle Johansson committed
    /*! \brief Flags to copy from peer/user to dialog */
    
    #define SIP_FLAGS_TO_COPY \
    	(SIP_PROMISCREDIR | SIP_TRUSTRPID | SIP_SENDRPID | SIP_DTMF | SIP_REINVITE | \
    
    	 SIP_PROG_INBAND | SIP_USECLIENTCODE | SIP_NAT | SIP_G726_NONSTANDARD | \
    
    	 SIP_USEREQPHONE | SIP_INSECURE)
    
    /*! \name SIPflags2
    	a second page of flags (for flags[1] */
    /*@{*/ 
    
    #define SIP_PAGE2_RTCACHEFRIENDS	(1 << 0)	/*!< GP: Should we keep RT objects in memory for extended time? */
    #define SIP_PAGE2_RTAUTOCLEAR		(1 << 2)	/*!< GP: Should we clean memory from peers after expiry? */
    
    /* Space for addition of other realtime flags in the future */
    
    Olle Johansson's avatar
    Olle Johansson committed
    #define SIP_PAGE2_VIDEOSUPPORT		(1 << 14)	/*!< DP: Video supported if offered? */
    #define SIP_PAGE2_TEXTSUPPORT		(1 << 15)	/*!< GDP: Global text enable */
    
    Olle Johansson's avatar
    Olle Johansson committed
    #define SIP_PAGE2_ALLOWSUBSCRIBE	(1 << 16)	/*!< GP: Allow subscriptions from this peer? */
    #define SIP_PAGE2_ALLOWOVERLAP		(1 << 17)	/*!< DP: Allow overlap dialing ? */
    #define SIP_PAGE2_SUBSCRIBEMWIONLY	(1 << 18)	/*!< GP: Only issue MWI notification if subscribed to */
    
    Olle Johansson's avatar
    Olle Johansson committed
    #define SIP_PAGE2_T38SUPPORT		(7 << 20)	/*!< GDP: T38 Fax Passthrough Support */
    
    #define SIP_PAGE2_T38SUPPORT_UDPTL	(1 << 20)	/*!< GDP: T38 Fax Passthrough Support */
    #define SIP_PAGE2_T38SUPPORT_RTP	(2 << 20)	/*!< GDP: T38 Fax Passthrough Support (not implemented) */
    #define SIP_PAGE2_T38SUPPORT_TCP	(4 << 20)	/*!< GDP: T38 Fax Passthrough Support (not implemented) */
    
    #define SIP_PAGE2_CALL_ONHOLD		(3 << 23)	/*!< D: Call hold states: */
    #define SIP_PAGE2_CALL_ONHOLD_ACTIVE    (1 << 23)       /*!< D: Active hold */
    #define SIP_PAGE2_CALL_ONHOLD_ONEDIR	(2 << 23)	/*!< D: One directional hold */
    #define SIP_PAGE2_CALL_ONHOLD_INACTIVE	(3 << 23)	/*!< D: Inactive hold */
    
    #define SIP_PAGE2_RFC2833_COMPENSATE    (1 << 25)	/*!< DP: Compensate for buggy RFC2833 implementations */
    #define SIP_PAGE2_BUGGY_MWI		(1 << 26)	/*!< DP: Buggy CISCO MWI fix */
    
    Joshua Colp's avatar
    Joshua Colp committed
    #define SIP_PAGE2_REGISTERTRYING        (1 << 29)       /*!< DP: Send 100 Trying on REGISTER attempts */
    
    Olle Johansson's avatar
    Olle Johansson committed
    
    
    Olle Johansson's avatar
    Olle Johansson committed
    #define SIP_PAGE2_FLAGS_TO_COPY \
    
    	(SIP_PAGE2_ALLOWSUBSCRIBE | SIP_PAGE2_ALLOWOVERLAP | SIP_PAGE2_VIDEOSUPPORT | \
    
    	SIP_PAGE2_T38SUPPORT | SIP_PAGE2_RFC2833_COMPENSATE | SIP_PAGE2_BUGGY_MWI | \
    	SIP_PAGE2_TEXTSUPPORT )
    
    /*@}*/ 
    
    /*! \name SIPflagsT38
    	T.38 set of flags */
    
    #define T38FAX_FILL_BIT_REMOVAL			(1 << 0)	/*!< Default: 0 (unset)*/
    
    #define T38FAX_TRANSCODING_MMR			(1 << 1)	/*!< Default: 0 (unset)*/
    
    #define T38FAX_TRANSCODING_JBIG			(1 << 2)	/*!< Default: 0 (unset)*/
    
    /* Rate management */
    #define T38FAX_RATE_MANAGEMENT_TRANSFERED_TCF	(0 << 3)
    #define T38FAX_RATE_MANAGEMENT_LOCAL_TCF	(1 << 3)	/*!< Unset for transferredTCF (UDPTL), set for localTCF (TPKT) */
    /* UDP Error correction */
    #define T38FAX_UDP_EC_NONE			(0 << 4)	/*!< two bits, if unset NO t38UDPEC field in T38 SDP*/
    #define T38FAX_UDP_EC_FEC			(1 << 4)	/*!< Set for t38UDPFEC */
    #define T38FAX_UDP_EC_REDUNDANCY		(2 << 4)	/*!< Set for t38UDPRedundancy */
    /* T38 Spec version */
    #define T38FAX_VERSION				(3 << 6)	/*!< two bits, 2 values so far, up to 4 values max */
    #define T38FAX_VERSION_0			(0 << 6)	/*!< Version 0 */
    #define T38FAX_VERSION_1			(1 << 6)	/*!< Version 1 */
    /* Maximum Fax Rate */
    #define T38FAX_RATE_2400			(1 << 8)	/*!< 2400 bps t38FaxRate */
    #define T38FAX_RATE_4800			(1 << 9)	/*!< 4800 bps t38FaxRate */
    #define T38FAX_RATE_7200			(1 << 10)	/*!< 7200 bps t38FaxRate */
    #define T38FAX_RATE_9600			(1 << 11)	/*!< 9600 bps t38FaxRate */
    #define T38FAX_RATE_12000			(1 << 12)	/*!< 12000 bps t38FaxRate */
    #define T38FAX_RATE_14400			(1 << 13)	/*!< 14400 bps t38FaxRate */
    
    
    /*!< This is default: NO MMR and JBIG transcoding, NO fill bit removal, transferredTCF TCF, UDP FEC, Version 0 and 9600 max fax rate */
    
    static int global_t38_capability = T38FAX_VERSION_0 | T38FAX_RATE_2400 | T38FAX_RATE_4800 | T38FAX_RATE_7200 | T38FAX_RATE_9600;
    
    /*! \brief debugging state
     * We store separately the debugging requests from the config file
     * and requests from the CLI. Debugging is enabled if either is set
     * (which means that if sipdebug is set in the config file, we can
     * only turn it off by reloading the config).
     */
    enum sip_debug_e {
    	sip_debug_none = 0,
    	sip_debug_config = 1,
    	sip_debug_console = 2,
    };
    
    static enum sip_debug_e sipdebug;
    
    /*! \brief extra debugging for 'text' related events.
     * At thie moment this is set together with sip_debug_console.
     * It should either go away or be implemented properly.
     */
    static int sipdebug_text;
    
    	T38_DISABLED = 0,                /*!< Not enabled */
    	T38_LOCAL_DIRECT,                /*!< Offered from local */
    	T38_LOCAL_REINVITE,              /*!< Offered from local - REINVITE */
    	T38_PEER_DIRECT,                 /*!< Offered from peer */
    	T38_PEER_REINVITE,               /*!< Offered from peer - REINVITE */
    	T38_ENABLED                      /*!< Negotiated (enabled) */
    
    };
    
    /*! \brief T.38 channel settings (at some point we need to make this alloc'ed */
    struct t38properties {
    	struct ast_flags t38support;	/*!< Flag for udptl, rtp or tcp support for this session */
    	int capability;			/*!< Our T38 capability */
    	int peercapability;		/*!< Peers T38 capability */
    	int jointcapability;		/*!< Supported T38 capability at both ends */
    	enum t38state state;		/*!< T.38 state */
    };
    
    
    /*! \brief Parameters to know status of transfer */
    enum referstatus {
    
    	REFER_IDLE,                    /*!< No REFER is in progress */
    	REFER_SENT,                    /*!< Sent REFER to transferee */
    	REFER_RECEIVED,                /*!< Received REFER from transferrer */
    
    	REFER_CONFIRMED,               /*!< Refer confirmed with a 100 TRYING (unused) */
    
    	REFER_ACCEPTED,                /*!< Accepted by transferee */
    	REFER_RINGING,                 /*!< Target Ringing */
    	REFER_200OK,                   /*!< Answered by transfer target */
    	REFER_FAILED,                  /*!< REFER declined - go on */
    	REFER_NOAUTH                   /*!< We had no auth for REFER */
    
    /*! \brief generic struct to map between strings and integers.
     * Fill it with x-s pairs, terminate with an entry with s = NULL;
     * Then you can call map_x_s(...) to map an integer to a string,
     * and map_s_x() for the string -> integer mapping.
     */
    struct _map_x_s {
    	int x;
    	const char *s;
    };              
    
    static const struct _map_x_s referstatusstrings[] = {
    
    	{ REFER_IDLE,		"<none>" },
    	{ REFER_SENT,		"Request sent" },
    	{ REFER_RECEIVED,	"Request received" },
    
    	{ REFER_CONFIRMED,	"Confirmed" },
    
    	{ REFER_ACCEPTED,	"Accepted" },
    	{ REFER_RINGING,	"Target ringing" },