[svn-commits] russell: branch group/sip_session_timers r84364 - /team/group/sip_session_tim...

SVN commits to the Digium repositories svn-commits at lists.digium.com
Tue Oct 2 08:36:49 CDT 2007


Author: russell
Date: Tue Oct  2 08:36:48 2007
New Revision: 84364

URL: http://svn.digium.com/view/asterisk?view=rev&rev=84364
Log:
Merge patch from issue #10665, fixing the conflicts and API changes that have
occurred in the past few weeks.  This branch is ready for use in testing this
patch.

Modified:
    team/group/sip_session_timers/channels/chan_sip.c

Modified: team/group/sip_session_timers/channels/chan_sip.c
URL: http://svn.digium.com/view/asterisk/team/group/sip_session_timers/channels/chan_sip.c?view=diff&rev=84364&r1=84363&r2=84364
==============================================================================
--- team/group/sip_session_timers/channels/chan_sip.c (original)
+++ team/group/sip_session_timers/channels/chan_sip.c Tue Oct  2 08:36:48 2007
@@ -211,6 +211,9 @@
 
 #define INITIAL_CSEQ                 101              /*!< our initial sip sequence number */
 
+#define DEFAULT_MAX_SE               1800             /*!< Session-Timer Default Session-Expires period (RFC 4028) */
+#define DEFAULT_MIN_SE               90               /*!< Session-Timer Default Min-SE period (RFC 4028) */
+
 /*! \brief Global jitterbuffer configuration - by default, jb is disabled */
 static struct ast_jb_conf default_jbconf =
 {
@@ -381,6 +384,21 @@
 	REG_STATE_FAILED,	/*!< Registration failed after several tries */
 		/* fatal - no chance to proceed */
 };
+
+/*! \brief Modes in which Astreisk can be configured to run SIP Session-Timers */
+enum st_mode {
+        SESSION_TIMER_MODE_ACCEPT,      /*!< Honor inbound Session-Timer requests */
+        SESSION_TIMER_MODE_ORIGINATE,   /*!< Originate outbound and honor inbound requests */
+        SESSION_TIMER_MODE_REFUSE       /*!< Ignore inbound Session-Timers requests */
+};
+
+/*! \brief The entity playing the refresher role for Session-Timers */
+enum st_refresher {
+        SESSION_TIMER_REFRESHER_AUTO,    /*!< Negotiated                      */
+        SESSION_TIMER_REFRESHER_UAC,     /*!< Session is refreshed by the UAC */
+        SESSION_TIMER_REFRESHER_UAS      /*!< Session is refreshed by the UAS */
+};
+
 
 /*! \brief definition of a sip proxy server
  *
@@ -458,6 +476,8 @@
 #define SIP_OPT_NOREFERSUB	(1 << 14)
 #define SIP_OPT_HISTINFO	(1 << 15)
 #define SIP_OPT_RESPRIORITY	(1 << 16)
+#define SIP_OPT_UNKNOWN		(1 << 17)
+
 
 /*! \brief List of well-known SIP options. If we get this in a require,
    we should check the list and answer accordingly. */
@@ -472,8 +492,8 @@
 	{ 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" },
+	/* RFC4028: SIP Session-Timers */
+	{ SIP_OPT_TIMER,	SUPPORTED,	"timer" },
 	/* RFC3959: SIP Early session support */
 	{ SIP_OPT_EARLY_SESSION, NOT_SUPPORTED,	"early-session" },
 	/* RFC3911: SIP Join header support */
@@ -509,7 +529,7 @@
 #define ALLOWED_METHODS "INVITE, ACK, CANCEL, OPTIONS, BYE, REFER, SUBSCRIBE, NOTIFY"
 
 /*! \brief SIP Extensions we support */
-#define SUPPORTED_EXTENSIONS "replaces" 
+#define SUPPORTED_EXTENSIONS "replaces, timer" 
 
 /*! \brief Standard SIP port from RFC 3261. DO NOT CHANGE THIS */
 #define STANDARD_SIP_PORT	5060
@@ -640,6 +660,12 @@
 static int regobjs = 0;                  /*!< Registry objects */
 
 static struct ast_flags global_flags[2] = {{0}};        /*!< global SIP_ flags */
+
+static enum st_mode global_st_mode;           /*!< Mode of operation for Session-Timers           */
+static enum st_refresher global_st_refresher; /*!< Session-Timer refresher                        */
+static int global_min_se;                     /*!< Lowest threshold for session refresh interval  */
+static int global_max_se;                     /*!< Highest threshold for session refresh interval */
+
 
 AST_MUTEX_DEFINE_STATIC(netlock);
 
@@ -1059,6 +1085,7 @@
 
 	int timer_t1;				/*!< SIP timer T1, ms rtt */
 	unsigned int sipoptions;		/*!< Supported SIP options on the other end */
+	unsigned int reqsipoptions;		/*!< Required SIP options on the other end */
 	struct ast_codec_pref prefs;		/*!< codec prefs */
 	int capability;				/*!< Special capability (codec) */
 	int jointcapability;			/*!< Supported capability at both ends (codecs) */
@@ -1079,6 +1106,8 @@
 	char tag[11];				/*!< Our tag for this session */
 	int sessionid;				/*!< SDP Session ID */
 	int sessionversion;			/*!< SDP Session Version */
+	int sessionversion_remote;		/*!< Remote UA's SDP Session Version */
+	int session_modify;			/*!< Session modification request true/false  */
 	struct sockaddr_in sa;			/*!< Our peer */
 	struct sockaddr_in redirip;		/*!< Where our RTP should be going if not to us */
 	struct sockaddr_in vredirip;		/*!< Where our Video RTP should be going if not to us */
@@ -1127,7 +1156,15 @@
 							before strolling to the Grokyzpå
 							(A bit unsure of this, please correct if
 							you know more) */
+
+	int st_active;                          /*!< Session-Timers on/off */ 
+	int st_interval;                        /*!< Session-Timers negotiated session refresh interval */
+	int st_schedid;                         /*!< Session-Timers ast_sched scheduler id */
+	enum st_refresher st_ref;               /*!< Session-Timers session refresher */
+	int st_expirys;                         /*!< Session-Timers number of expirys */
+	int st_active_peer_ua;                  /*!< Session-Timers on/off in peer UA */
 };
+
 
 /*! Max entires in the history list for a sip_pvt */
 #define MAX_HISTORY_ENTRIES 50
@@ -1233,6 +1270,11 @@
 	struct ast_variable *chanvars;	/*!< Variables to set for channel created by user */
 	int maxcallbitrate;		/*!< Maximum Bitrate for a video call */
 	int autoframing;
+
+	enum st_mode st_mode_oper;      /*!< Mode of operation for Session-Timers           */
+	enum st_refresher st_ref;       /*!< Session-Timer refresher                        */
+	int st_min_se;                  /*!< Lowest threshold for session refresh interval  */
+	int st_max_se;                  /*!< Highest threshold for session refresh interval */
 };
 
 /*!
@@ -1318,6 +1360,11 @@
 	struct ast_variable *chanvars;	/*!<  Variables to set for channel created by user */
 	struct sip_pvt *mwipvt;		/*!<  Subscription for MWI */
 	int autoframing;
+
+	enum st_mode st_mode_oper;      /*!< Mode of operation for Session-Timers           */
+	enum st_refresher st_ref;       /*!< Session-Timer refresher                        */
+	int st_min_se;                  /*!< Lowest threshold for session refresh interval  */
+	int st_max_se;                  /*!< Highest threshold for session refresh interval */
 };
 
 
@@ -1493,7 +1540,7 @@
 static int transmit_response(struct sip_pvt *p, const char *msg, const struct sip_request *req);
 static int transmit_response_reliable(struct sip_pvt *p, const char *msg, const struct sip_request *req);
 static int transmit_response_with_date(struct sip_pvt *p, const char *msg, const struct sip_request *req);
-static int transmit_response_with_sdp(struct sip_pvt *p, const char *msg, const struct sip_request *req, enum xmittype reliable);
+static int transmit_response_with_sdp(struct sip_pvt *p, const char *msg, const struct sip_request *req, enum xmittype reliable, int oldsdp);
 static int transmit_response_with_unsupported(struct sip_pvt *p, const char *msg, const struct sip_request *req, const char *unsupported);
 static int transmit_response_with_auth(struct sip_pvt *p, const char *msg, const struct sip_request *req, const char *rand, enum xmittype reliable, const char *header, int stale);
 static int transmit_response_with_allow(struct sip_pvt *p, const char *msg, const struct sip_request *req, enum xmittype reliable);
@@ -1501,7 +1548,7 @@
 static int transmit_request(struct sip_pvt *p, int sipmethod, int inc, enum xmittype reliable, int newbranch);
 static int transmit_request_with_auth(struct sip_pvt *p, int sipmethod, int seqno, enum xmittype reliable, int newbranch);
 static int transmit_invite(struct sip_pvt *p, int sipmethod, int sdp, int init);
-static int transmit_reinvite_with_sdp(struct sip_pvt *p, int t38version);
+static int transmit_reinvite_with_sdp(struct sip_pvt *p, int t38version, int oldsdp);
 static int transmit_info_with_digit(struct sip_pvt *p, const char digit, unsigned int duration);
 static int transmit_info_with_vidupdate(struct sip_pvt *p);
 static int transmit_message_with_text(struct sip_pvt *p, const char *text);
@@ -1555,7 +1602,7 @@
 static void add_noncodec_to_sdp(const struct sip_pvt *p, int format, int sample_rate,
 				struct ast_str **m_buf, struct ast_str **a_buf,
 				int debug);
-static enum sip_result add_sdp(struct sip_request *resp, struct sip_pvt *p);
+static enum sip_result add_sdp(struct sip_request *resp, struct sip_pvt *p, int oldsdp);
 static void do_setnat(struct sip_pvt *p, int natflags);
 static void stop_media_flows(struct sip_pvt *p);
 
@@ -1792,6 +1839,21 @@
 static struct ast_udptl *sip_get_udptl_peer(struct ast_channel *chan);
 static int sip_set_udptl_peer(struct ast_channel *chan, struct ast_udptl *udptl);
 
+/*------ Session-Timers functions --------- */
+static void proc_422_rsp(struct sip_pvt *p, struct sip_request *rsp);
+static int  proc_session_timer(const void *vp);
+static void stop_session_timer(struct sip_pvt *p);
+static void start_session_timer(struct sip_pvt *p);
+static void restart_session_timer(struct sip_pvt *p);
+static const char *strefresher2str (enum st_refresher r);
+static int parse_session_expires (const char *p_hdrval, int *const p_interval, enum st_refresher *const p_ref);
+static int parse_minse (const char *p_hdrval, int *const p_interval);
+static int st_get_max_se(struct sip_pvt *);
+static int st_get_min_se(struct sip_pvt *);
+static enum st_refresher st_get_refresher(struct sip_pvt*);
+static enum st_mode st_get_mode(struct sip_pvt*);
+
+
 /*! \brief Definition of this channel for PBX channel registration */
 static const struct ast_channel_tech sip_tech = {
 	.type = "SIP",
@@ -2062,6 +2124,15 @@
 				break;
 			}
 		}
+
+		/* This function is used to parse both Suported: and Require: headers.
+		Let the caller of this function know that an unknown option tag was 
+		encountered, so that if the UAC requires it then the request can be 
+		rejected with a 420 response. */
+		if (!found) {
+			profile |= SIP_OPT_UNKNOWN;
+		}
+
 		if (!found && sipdebug) {
 			if (!strncasecmp(next, "x-", 2))
 				ast_debug(3, "Found private SIP option, not supported: %s\n", next);
@@ -2471,6 +2542,10 @@
 	if (p->do_history)
 		append_history(p, "SchedDestroy", "%d ms", ms);
 	p->autokillid = ast_sched_add(sched, ms, __sip_autodestruct, dialog_ref(p));
+
+	if (p->st_active == TRUE && p->st_schedid != 0) {
+		stop_session_timer(p);
+	}
 }
 
 /*! \brief Cancel destruction of SIP dialog.
@@ -2811,7 +2886,7 @@
 		break;
 	case AST_STATE_UP:
 		if (!p->pendinginvite) {		/* We are up, and have no outstanding invite */
-			transmit_reinvite_with_sdp(p, FALSE);
+			transmit_reinvite_with_sdp(p, FALSE, FALSE);
 		} else if (!ast_test_flag(&p->flags[0], SIP_PENDINGBYE)) {
 			ast_set_flag(&p->flags[0], SIP_NEEDREINVITE);	
 		}	
@@ -4212,7 +4287,7 @@
 			ast_debug(2,"T38State change to %d on channel %s\n", p->t38.state, ast->name);
 			res = transmit_response_with_t38_sdp(p, "200 OK", &p->initreq, XMIT_CRITICAL);
 		} else 
-			res = transmit_response_with_sdp(p, "200 OK", &p->initreq, XMIT_CRITICAL);
+			res = transmit_response_with_sdp(p, "200 OK", &p->initreq, XMIT_CRITICAL, FALSE);
 	}
 	sip_pvt_unlock(p);
 	return res;
@@ -4245,7 +4320,7 @@
 				if ((ast->_state != AST_STATE_UP) &&
 				    !ast_test_flag(&p->flags[0], SIP_PROGRESS_SENT) &&
 				    !ast_test_flag(&p->flags[0], SIP_OUTGOING)) {
-					transmit_response_with_sdp(p, "183 Session Progress", &p->initreq, XMIT_UNRELIABLE);
+					transmit_response_with_sdp(p, "183 Session Progress", &p->initreq, XMIT_UNRELIABLE, FALSE);
 					ast_set_flag(&p->flags[0], SIP_PROGRESS_SENT);	
 				}
 				p->lastrtptx = time(NULL);
@@ -4262,7 +4337,7 @@
 				if ((ast->_state != AST_STATE_UP) &&
 				    !ast_test_flag(&p->flags[0], SIP_PROGRESS_SENT) &&
 				    !ast_test_flag(&p->flags[0], SIP_OUTGOING)) {
-					transmit_response_with_sdp(p, "183 Session Progress", &p->initreq, XMIT_UNRELIABLE);
+					transmit_response_with_sdp(p, "183 Session Progress", &p->initreq, XMIT_UNRELIABLE, FALSE);
 					ast_set_flag(&p->flags[0], SIP_PROGRESS_SENT);	
 				}
 				p->lastrtptx = time(NULL);
@@ -4279,7 +4354,7 @@
 				if ((ast->_state != AST_STATE_UP) &&
 				    !ast_test_flag(&p->flags[0], SIP_PROGRESS_SENT) &&
 				    !ast_test_flag(&p->flags[0], SIP_OUTGOING)) {
-					transmit_response_with_sdp(p, "183 Session Progress", &p->initreq, XMIT_UNRELIABLE);
+					transmit_response_with_sdp(p, "183 Session Progress", &p->initreq, XMIT_UNRELIABLE, FALSE);
 					ast_set_flag(&p->flags[0], SIP_PROGRESS_SENT);	
 				}
 				p->lastrtptx = time(NULL);
@@ -4474,7 +4549,7 @@
 		    !ast_test_flag(&p->flags[0], SIP_PROGRESS_SENT) &&
 		    !ast_test_flag(&p->flags[0], SIP_OUTGOING)) {
 			p->invitestate = INV_EARLY_MEDIA;
-			transmit_response_with_sdp(p, "183 Session Progress", &p->initreq, XMIT_UNRELIABLE);
+			transmit_response_with_sdp(p, "183 Session Progress", &p->initreq, XMIT_UNRELIABLE, FALSE);
 			ast_set_flag(&p->flags[0], SIP_PROGRESS_SENT);	
 			break;
 		}
@@ -4914,7 +4989,7 @@
 			if (!p->pendinginvite) {
 				ast_debug(3, "Sending reinvite on SIP (%s) for T.38 negotiation.\n",ast->name);
 				p->t38.state = T38_LOCAL_REINVITE;
-				transmit_reinvite_with_sdp(p, TRUE);
+				transmit_reinvite_with_sdp(p, TRUE, FALSE);
 				ast_debug(2, "T38 state changed to %d on channel %s\n", p->t38.state, ast->name);
 			}
 		} else if (!ast_test_flag(&p->flags[0], SIP_PENDINGBYE)) {
@@ -4992,6 +5067,14 @@
 	p->autokillid = -1;
 	p->subscribed = NONE;
 	p->stateid = -1;
+	p->sessionversion_remote = -1;
+	p->session_modify = TRUE;
+	p->st_active = FALSE;			/* Session-Timers on/off */
+  	p->st_interval = 0;			/* Session-Timers negotiated session refresh interval */
+    	p->st_schedid = -1;			/* Session-Timers ast_sched scheduler id */
+    	p->st_ref = 0;				/* Session-Timers session refresher */
+    	p->st_expirys = 0;			/* Session-Timers number of expirys */
+    	p->st_active_peer_ua = FALSE;		/* Session-Timers on/off in peer UA */
 	p->prefs = default_prefs;		/* Set default codecs for this call */
 
 	if (intended_method != SIP_OPTIONS)	/* Peerpoke has it's own system */
@@ -5094,6 +5177,7 @@
 		p->t38.jointcapability = p->t38.capability;
 	}
 	ast_string_field_set(p, context, default_context);
+
 
 	/* Add to active dialog list */
 	dialoglist_lock();
@@ -5500,6 +5584,9 @@
 	const char *m;		/* SDP media offer */
 	const char *c;
 	const char *a;
+	const char *o;		/* Pointer to o= line	*/
+	char *o_copy;		/* Copy of o= line	*/
+	char *token;
 	char host[258];
 	int len = -1;
 	int portno = -1;		/*!< RTP Audio port number */
@@ -5541,6 +5628,7 @@
 	int last_rtpmap_codec=0;
 
 	char buf[BUFSIZ];
+	int rua_version;
 
 	if (!p->rtp) {
 		ast_log(LOG_ERROR, "Got SDP but have no RTP session allocated.\n");
@@ -5566,6 +5654,50 @@
 	/* Update our last rtprx when we receive an SDP, too */
 	p->lastrtprx = p->lastrtptx = time(NULL); /* XXX why both ? */
 
+	/* Store the SDP version number of remote UA. This will allow us to 
+	distinguish between session modifications and session refreshes. If 
+	the remote UA does not send an incremented SDP version number in a 
+	subsequent RE-INVITE then that means its not changing media session. 
+	The RE-INVITE may have been sent to update connected party, remote  
+	target or to refresh the session (Session-Timers).  Asterisk must not 
+	change media session and increment its own version number in answer 
+	SDP in this case. */ 
+	
+	o = get_sdp(req, "o");
+	if (ast_strlen_zero(o)) {
+		ast_log(LOG_WARNING, "SDP sytax error. SDP without an o= line\n");
+		return -1;
+	}
+
+	o_copy = ast_strdupa(o);
+	token = strsep (&o_copy, " ");  /* Skip username   */
+	if (!o_copy) { 
+		ast_log(LOG_WARNING, "SDP sytax error in o= line username\n");
+		return -1;
+	}
+	token = strsep (&o_copy, " ");  /* Skip session-id */
+	if (!o_copy) { 
+		ast_log(LOG_WARNING, "SDP sytax error in o= line session-id\n");
+		return -1;
+	}
+	token = strsep (&o_copy, " ");  /* Version         */
+	if (!o_copy) { 
+		ast_log(LOG_WARNING, "SDP sytax error in o= line\n");
+		return -1;
+	}
+	if (!sscanf(token, "%d", &rua_version)) {
+		ast_log(LOG_WARNING, "SDP sytax error in o= line version\n");
+		return -1;
+	}
+
+	if (p->sessionversion_remote < 0 || p->sessionversion_remote != rua_version) {
+ 		p->sessionversion_remote = rua_version;
+		p->session_modify = TRUE;
+	} else if (p->sessionversion_remote == rua_version) {
+		p->session_modify = FALSE;
+		ast_debug(2, "SDP version number same as previous SDP\n");
+		return 0;
+	} 
 
 	/* Try to find first media stream */
 	m = get_sdp(req, "m");
@@ -6438,6 +6570,17 @@
 		add_header(resp, "User-Agent", global_useragent);
 	add_header(resp, "Allow", ALLOWED_METHODS);
 	add_header(resp, "Supported", SUPPORTED_EXTENSIONS);
+
+	/* Add Session-Timers related headers if the feature is active for this session */
+	if(p->st_active == TRUE && p->st_active_peer_ua == TRUE) {
+		char se_hdr[256];
+		int se_len = 0;
+		se_len = sprintf (&se_hdr[se_len], "%d;refresher=%s", p->st_interval, strefresher2str(p->st_ref));
+		se_hdr[se_len] = '\0';
+		add_header(resp, "Require", "timer");
+		add_header(resp, "Session-Expires", se_hdr);
+	}
+
 	if (msg[0] == '2' && (p->method == SIP_SUBSCRIBE || p->method == SIP_REGISTER)) {
 		/* For registration responses, we also need expiry and
 		   contact info */
@@ -6569,6 +6712,20 @@
 		ast_string_field_free(p, url);
 	}
 
+	/* Add Session-Timers related headers if the feature is active for this session */
+	if(p->st_active == TRUE && p->st_active_peer_ua == TRUE) {
+		char se_hdr[256];
+		int se_len = 0;
+		se_len = sprintf (&se_hdr[se_len], "%d;refresher=%s", p->st_interval, strefresher2str(p->st_ref));
+		se_hdr[se_len] = '\0';
+		add_header(req, "Require", "timer");
+		add_header(req, "Session-Expires", se_hdr);
+		se_len = 0;
+		se_len = sprintf (&se_hdr[se_len], "%d", st_get_min_se(p));
+		se_hdr[se_len] = '\0';
+		add_header(req, "Min-SE", se_hdr);
+	}
+
 	return 0;
 }
 
@@ -6679,6 +6836,27 @@
 	add_header_contentLength(&resp, 0);
 	return send_response(p, &resp, XMIT_UNRELIABLE, 0);
 }
+
+/*! \brief Transmit 422 response with Min-SE header (Session-Timers)  */
+static int transmit_response_with_minse(struct sip_pvt *p, const char *msg, const struct sip_request *req, int minse_int)
+{
+    struct sip_request resp;
+    char minse_str[20];
+    int minse_str_len;
+
+    respprep(&resp, p, msg, req);
+    append_date(&resp);
+
+    minse_str_len = sprintf (&minse_str[0], "%d", minse_int);
+    if (minse_str_len) {
+        minse_str[minse_str_len] = '\0';
+        add_header(&resp, "Min-SE", &minse_str[0]);
+    }
+
+    add_header_contentLength(&resp, 0);
+    return send_response(p, &resp, XMIT_UNRELIABLE, 0);
+}
+
 
 /*! \brief Transmit response, Make sure you get an ACK
 	This is only used for responses to INVITEs, where we need to make sure we get an ACK
@@ -7043,8 +7221,13 @@
 
 #define SDP_SAMPLE_RATE(x) (x == AST_FORMAT_G722) ? 16000 : 8000
 
-/*! \brief Add Session Description Protocol message */
-static enum sip_result add_sdp(struct sip_request *resp, struct sip_pvt *p)
+/*! \brief Add Session Description Protocol message 
+
+    If oldsdp is TRUE, then the SDP version number is not incremented. This mechanism
+    is used in Session-Timers where RE-INVITEs are used for refreshing SIP sessions 
+    without modifying the media session in any way. 
+*/
+static enum sip_result add_sdp(struct sip_request *resp, struct sip_pvt *p, int oldsdp)
 {
 	int len = 0;
 	int alreadysent = 0;
@@ -7097,8 +7280,10 @@
 	if (!p->sessionid) {
 		p->sessionid = (int)ast_random();
 		p->sessionversion = p->sessionid;
-	} else
-		p->sessionversion++;
+	} else {
+		if (oldsdp == FALSE)
+			p->sessionversion++;
+	}
 
 	capability = p->jointcapability;
 
@@ -7360,7 +7545,7 @@
 /*! \brief Used for 200 OK and 183 early media 
 	\return Will return XMIT_ERROR for network errors.
 */
-static int transmit_response_with_sdp(struct sip_pvt *p, const char *msg, const struct sip_request *req, enum xmittype reliable)
+static int transmit_response_with_sdp(struct sip_pvt *p, const char *msg, const struct sip_request *req, enum xmittype reliable, int oldsdp)
 {
 	struct sip_request resp;
 	int seqno;
@@ -7375,7 +7560,7 @@
 			ast_rtp_codec_setpref(p->rtp, &p->prefs);
 		}
 		try_suggested_sip_codec(p);	
-		add_sdp(&resp, p);
+		add_sdp(&resp, p, oldsdp);
 	} else 
 		ast_log(LOG_ERROR, "Can't add SDP to response, since we have no RTP session allocated. Call-ID %s\n", p->callid);
 	if (reliable && !p->pendinginvite)
@@ -7432,28 +7617,38 @@
 	
 	If t38version is TRUE, we send T38 SDP for re-invite from audio/video to
 	T38 UDPTL transmission on the channel
+
+    If oldsdp is TRUE then the SDP version number is not incremented. This
+    is needed for Session-Timers so we can send a re-invite to refresh the
+    SIP session without modifying the media session. 
 */
-static int transmit_reinvite_with_sdp(struct sip_pvt *p, int t38version)
-{
-	struct sip_request req;
-
-	reqprep(&req, p, ast_test_flag(&p->flags[0], SIP_REINVITE_UPDATE) ?  SIP_UPDATE : SIP_INVITE, 0, 1);
-	
-	add_header(&req, "Allow", ALLOWED_METHODS);
-	add_header(&req, "Supported", SUPPORTED_EXTENSIONS);
-	if (sipdebug)
-		add_header(&req, "X-asterisk-Info", "SIP re-invite (External RTP bridge)");
-	if (p->do_history)
-		append_history(p, "ReInv", "Re-invite sent");
-	if (t38version)
-		add_t38_sdp(&req, p);
-	else
-		add_sdp(&req, p);
-	/* Use this as the basis */
-	initialize_initreq(p, &req);
-	p->lastinvite = p->ocseq;
-	ast_set_flag(&p->flags[0], SIP_OUTGOING);		/* Change direction of this dialog */
-	return send_request(p, &req, XMIT_CRITICAL, p->ocseq);
+static int transmit_reinvite_with_sdp(struct sip_pvt *p, int t38version, int oldsdp)
+{
+    struct sip_request req;
+
+    reqprep(&req, p, ast_test_flag(&p->flags[0], SIP_REINVITE_UPDATE) ?  SIP_UPDATE : SIP_INVITE, 0, 1);
+
+    add_header(&req, "Allow", ALLOWED_METHODS);
+    add_header(&req, "Supported", SUPPORTED_EXTENSIONS);
+    if (sipdebug) {
+        if (oldsdp == TRUE)
+            add_header(&req, "X-asterisk-Info", "SIP re-invite (Session-Timers)");
+        else
+            add_header(&req, "X-asterisk-Info", "SIP re-invite (External RTP bridge)");
+    }
+
+    if (p->do_history)
+        append_history(p, "ReInv", "Re-invite sent");
+    if (t38version)
+        add_t38_sdp(&req, p);
+    else
+        add_sdp(&req, p, oldsdp);
+
+    /* Use this as the basis */
+    initialize_initreq(p, &req);
+    p->lastinvite = p->ocseq;
+    ast_set_flag(&p->flags[0], SIP_OUTGOING);       /* Change direction of this dialog */
+    return send_request(p, &req, XMIT_CRITICAL, p->ocseq);
 }
 
 /* \brief Remove URI parameters at end of URI, not in username part though */
@@ -7749,6 +7944,27 @@
 		add_header(&req, "Require", "replaces");
 	}
 
+	/* Add Session-Timers related headers */
+	if (st_get_mode(p) == SESSION_TIMER_MODE_ORIGINATE) {
+		char i2astr[10];
+		int i2aidx;
+
+		if (!p->st_interval) {
+			p->st_interval = st_get_max_se(p);
+		} 
+
+		p->st_active = TRUE;
+		
+		i2aidx = sprintf (&i2astr[0], "%d", p->st_interval);
+		i2astr[i2aidx] = '\0';
+		add_header(&req, "Session-Expires", i2astr);
+
+		i2aidx = 0;
+		i2aidx = sprintf (&i2astr[0], "%d", st_get_min_se(p));
+		i2astr[i2aidx] = '\0';
+		add_header(&req, "Min-SE", i2astr);
+	}
+
 	add_header(&req, "Allow", ALLOWED_METHODS);
 	add_header(&req, "Supported", SUPPORTED_EXTENSIONS);
 	if (p->options && p->options->addsipheaders && p->owner) {
@@ -7791,7 +8007,7 @@
 			ast_debug(1, "T38 is in state %d on channel %s\n", p->t38.state, p->owner ? p->owner->name : "<none>");
 			add_t38_sdp(&req, p);
 		} else if (p->rtp) 
-			add_sdp(&req, p);
+			add_sdp(&req, p, FALSE);
 	} else {
 		add_header_contentLength(&req, 0);
 	}
@@ -10573,6 +10789,45 @@
 /*! \brief  Report Peer status in character string
  *  \return 0 if peer is unreachable, 1 if peer is online, -1 if unmonitored
  */
+
+
+/* Session-Timer Modes */
+static struct _map_x_s stmodes[] = {
+        { SESSION_TIMER_MODE_ACCEPT,    "Accept"},
+        { SESSION_TIMER_MODE_ORIGINATE, "Originate"},
+        { SESSION_TIMER_MODE_REFUSE,    "Refuse"},
+        { -1,                           NULL},
+};
+
+static const char *stmode2str(enum st_mode m)
+{
+	return map_x_s(stmodes, m, "Unknown");
+}
+
+static enum st_mode str2stmode(const char *s)
+{
+	return map_s_x(stmodes, s, -1);
+}
+
+/* Session-Timer Refreshers */
+static struct _map_x_s strefreshers[] = {
+        { SESSION_TIMER_REFRESHER_AUTO,     "auto"},
+        { SESSION_TIMER_REFRESHER_UAC,      "uac"},
+        { SESSION_TIMER_REFRESHER_UAS,      "uas"},
+        { -1,                               NULL},
+};
+
+static const char *strefresher2str(enum st_refresher r)
+{
+	return map_x_s(strefreshers, r, "Unknown");
+}
+
+static enum st_refresher str2strefresher(const char *s)
+{
+	return map_s_x(strefreshers, s, -1);
+}
+
+
 static int peer_status(struct sip_peer *peer, char *status, int statuslen)
 {
 	int res = 0;
@@ -11367,6 +11622,12 @@
 			for (v = peer->chanvars ; v ; v = v->next)
  				ast_cli(fd, "                 %s = %s\n", v->name, v->value);
 		}
+
+		ast_cli(fd, "  Session-Timers   : %s\n", stmode2str(peer->st_mode_oper));
+		ast_cli(fd, "  Session-Refresher: %s\n", strefresher2str(peer->st_ref));
+		ast_cli(fd, "  Session-Expires  : %d secs\n", peer->st_max_se);
+		ast_cli(fd, "  Min-SE           : %d secs\n", peer->st_min_se);
+
 		ast_cli(fd,"\n");
 		unref_peer(peer);
 	} else  if (peer && type == 1) { /* manager listing */
@@ -11512,7 +11773,14 @@
 			for (v = user->chanvars ; v ; v = v->next)
  				ast_cli(a->fd, "                 %s = %s\n", v->name, v->value);
 		}
+
+ 		ast_cli(a->fd, "  Session-Timers   : %s\n", stmode2str(user->st_mode_oper));
+ 		ast_cli(a->fd, "  Session-Refresher: %s\n", strefresher2str(user->st_ref));
+ 		ast_cli(a->fd, "  Session-Expires  : %d secs\n", user->st_max_se);
+ 		ast_cli(a->fd, "  Min-SE           : %d secs\n", user->st_min_se);
+
 		ast_cli(a->fd,"\n");
+
 		unref_user(user);
 	} else {
 		ast_cli(a->fd,"User %s not found.\n", a->argv[3]);
@@ -11743,6 +12011,10 @@
 	ast_cli(a->fd, "  Auto-Framing:           %s\n", cli_yesno(global_autoframing));
 	ast_cli(a->fd, "  Outb. proxy:            %s %s\n", ast_strlen_zero(global_outboundproxy.name) ? "<not set>" : global_outboundproxy.name,
 							global_outboundproxy.force ? "(forced)" : "");
+	ast_cli(a->fd, "  Session-Timers:         %s\n", stmode2str(global_st_mode));
+	ast_cli(a->fd, "  Session-Refresher:      %s\n", strefresher2str (global_st_refresher));
+	ast_cli(a->fd, "  Session-Expires:        %d secs\n", global_max_se);
+	ast_cli(a->fd, "  Min-SE:                 %d secs\n", global_min_se);
 
 	ast_cli(a->fd, "\nDefault Settings:\n");
 	ast_cli(a->fd, "-----------------\n");
@@ -12120,9 +12392,21 @@
 					if (cur->sipoptions & sip_options[x].id)
 						ast_cli(a->fd, "%s ", sip_options[x].text);
 				}
+				ast_cli(a->fd, "\n");
 			} else
 				ast_cli(a->fd, "(none)\n");
+
+ 			ast_cli(a->fd, "  Session-Timer:          %s\n", cur->st_active ? "Active" : "Inactive");
+ 			if (cur->st_active == TRUE) {
+ 				ast_cli(a->fd, "  Session-Timer Interval:  %d\n", cur->st_interval);
+ 				ast_cli(a->fd, "  Session-Timer Refresher: %s\n", strefresher2str(cur->st_ref));
+ 				ast_cli(a->fd, "  Session-Timer Expirys:   %d\n", cur->st_expirys);
+ 				ast_cli(a->fd, "  Session-Timer Sched Id:  %d\n", cur->st_schedid);
+ 				ast_cli(a->fd, "  Session-Timer Peer Sts:  %s\n", cur->st_active_peer_ua ? "Active" : "Inactive");
+ 			}
+
 			ast_cli(a->fd, "\n\n");
+
 			found++;
 		}
 	}
@@ -13066,7 +13350,7 @@
 	} else if (ast_test_flag(&p->flags[0], SIP_NEEDREINVITE)) {
 		ast_debug(2, "Sending pending reinvite on '%s'\n", p->callid);
 		/* Didn't get to reinvite yet, so do it now */
-		transmit_reinvite_with_sdp(p, FALSE);
+		transmit_reinvite_with_sdp(p, FALSE, FALSE);
 		ast_clear_flag(&p->flags[0], SIP_NEEDREINVITE);	
 	}
 }
@@ -13079,6 +13363,8 @@
 	int xmitres = 0;
 	int reinvite = (p->owner && p->owner->_state == AST_STATE_UP);
 	struct ast_channel *bridgepeer = NULL;
+	char *p_hdrval;
+	int rtn;
 	
 	if (reinvite)
 		ast_debug(4, "SIP response %d to RE-invite on %s call %s\n", resp, outgoing ? "outgoing" : "incoming", p->callid);
@@ -13249,6 +13535,38 @@
 			if (!req->ignore)
 				ast_set_flag(&p->flags[0], SIP_PENDINGBYE);	
 		}
+
+		/* Check for Session-Timers related headers */
+		if (st_get_mode(p) != SESSION_TIMER_MODE_REFUSE && p->outgoing_call == TRUE && !reinvite) {
+			p_hdrval = (char*)get_header(req, "Session-Expires");
+        		if (!ast_strlen_zero(p_hdrval)) {
+				/* UAS supports Session-Timers */
+				enum st_refresher tmp_st_ref = SESSION_TIMER_REFRESHER_AUTO;
+				int tmp_st_interval = 0;
+				rtn = parse_session_expires(p_hdrval, &tmp_st_interval, &tmp_st_ref);
+				if (rtn != 0) {
+					ast_set_flag(&p->flags[0], SIP_PENDINGBYE);	
+				}
+				if (tmp_st_ref == SESSION_TIMER_REFRESHER_UAC || 
+					tmp_st_ref == SESSION_TIMER_REFRESHER_UAS) {
+					p->st_ref = tmp_st_ref;
+				} 
+				if (tmp_st_interval) {
+					p->st_interval = tmp_st_interval;
+				}
+				p->st_active = TRUE;
+				p->st_active_peer_ua = TRUE;
+			} else {
+				/* UAS doesn't support Session-Timers */
+				if (st_get_mode(p) == SESSION_TIMER_MODE_ORIGINATE) {
+					p->st_ref = SESSION_TIMER_REFRESHER_UAC;
+					p->st_active_peer_ua = FALSE;
+				}
+			}
+			start_session_timer(p);
+		}
+
+
 		/* If I understand this right, the branch is different for a non-200 ACK only */
 		p->invitestate = INV_TERMINATED;
 		xmitres = transmit_request(p, SIP_ACK, seqno, XMIT_UNRELIABLE, TRUE);
@@ -13303,6 +13621,13 @@
 			ast_queue_control(p->owner, AST_CONTROL_CONGESTION);
 		sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
 		break;
+
+	case 422: /* Session-Timers: Session interval too small */
+		xmitres = transmit_request(p, SIP_ACK, seqno, XMIT_UNRELIABLE, FALSE);
+		ast_string_field_free(p, theirtag);	
+		proc_422_rsp(p, req);
+		break;
+
 	case 487: /* Cancelled transaction */
 		/* We have sent CANCEL on an outbound INVITE 
 			This transaction is already scheduled to be killed by sip_hangup().
@@ -13775,6 +14100,13 @@
 				p->needdestroy = 1;
 			}
 			break;
+
+		case 422: /* Session-Timers: Session Interval Too Small */
+			if (sipmethod == SIP_INVITE) {
+				handle_response_invite(p, resp, rest, req, seqno);
+			}
+			break;
+
 		case 481: /* Call leg does not exist */
 			if (sipmethod == SIP_INVITE) {
 				handle_response_invite(p, resp, rest, req, seqno);
@@ -14482,7 +14814,7 @@
 		/* We should answer something here. If we are here, the
 			call we are replacing exists, so an accepted 
 			can't harm */
-		transmit_response_with_sdp(p, "200 OK", req, XMIT_RELIABLE);
+		transmit_response_with_sdp(p, "200 OK", req, XMIT_RELIABLE, FALSE);
 		/* Do something more clever here */
 		ast_channel_unlock(c);
 		sip_pvt_unlock(p->refer->refer_call);
@@ -14516,7 +14848,7 @@
 	   Targetcall is not touched by the masq */
 
 	/* Answer the incoming call and set channel to UP state */
-	transmit_response_with_sdp(p, "200 OK", req, XMIT_RELIABLE);
+	transmit_response_with_sdp(p, "200 OK", req, XMIT_RELIABLE, FALSE);
 		
 	ast_setstate(c, AST_STATE_UP);
 	
@@ -14612,6 +14944,17 @@
 	unsigned int required_profile = 0;
 	struct ast_channel *c = NULL;		/* New channel */
 	int reinvite = 0;
+	int rtn;
+
+	const char *p_uac_se_hdr;       /* UAC's Session-Expires header string                      */
+	const char *p_uac_min_se;       /* UAC's requested Min-SE interval (char string)            */
+	int uac_max_se = -1;            /* UAC's Session-Expires in integer format                  */
+	int uac_min_se = -1;            /* UAC's Min-SE in integer format                           */
+	int st_active = FALSE;          /* Session-Timer on/off boolean                             */
+	int st_interval = 0;            /* Session-Timer negotiated refresh interval                */
+	enum st_refresher st_ref;       /* Session-Timer session refresher                          */
+	int dlg_min_se = -1;
+	st_ref = SESSION_TIMER_REFRESHER_AUTO;
 
 	/* Find out what they support */
 	if (!p->sipoptions) {
@@ -14624,8 +14967,8 @@
 	required = get_header(req, "Require");
 	if (!ast_strlen_zero(required)) {
 		required_profile = parse_sip_options(NULL, required);
-		if (required_profile && required_profile != SIP_OPT_REPLACES) {
-			/* At this point we only support REPLACES */
+		if (required_profile && required_profile != SIP_OPT_REPLACES && required_profile != SIP_OPT_TIMER) {
+			/* At this point we only support REPLACES and Session-Timer */
 			transmit_response_with_unsupported(p, "420 Bad extension (unsupported)", req, required);
 			ast_log(LOG_WARNING,"Received SIP INVITE with unsupported required extension: %s\n", required);
 			p->invitestate = INV_COMPLETED;
@@ -14634,6 +14977,11 @@
 			return -1;
 		}
 	}
+
+	/* The option tags may be present in Supported: or Require: headers.
+	Include the Require: option tags for further processing as well */
+	p->sipoptions |= required_profile;
+	p->reqsipoptions = required_profile;
 
 	/* Check if this is a loop */
 	if (ast_test_flag(&p->flags[0], SIP_OUTGOING) && p->owner && (p->owner->_state != AST_STATE_UP)) {
@@ -14760,7 +15108,6 @@
 			return -1;
 		}
 	}
-
 
 	/* Check if this is an INVITE that sets up a new dialog or
 	   a re-invite in an existing dialog */
@@ -14886,6 +15233,7 @@
 			sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
 			return 0;
 		} else {
+
 			/* If no extension was specified, use the s one */
 			/* Basically for calling to IP/Host name only */
 			if (ast_strlen_zero(p->exten))
@@ -14912,8 +15260,135 @@
 			else
 				ast_debug(2, "Got a SIP re-transmit of INVITE for call %s\n", p->callid);
 		}
+
 		reinvite = 1;
 		c = p->owner;
+	}
+
+	/* Session-Timers */
+	if (p->sipoptions == SIP_OPT_TIMER) {
+		/* The UAC has requested session-timers for this session. Negotiate
+		the session refresh interval and who will be the refresher */
+		ast_debug(2, "Incoming INVITE with 'timer' option enabled\n");
+
+		/* Parse the Session-Expires header */
+		p_uac_se_hdr = get_header(req, "Session-Expires");
+		if (!ast_strlen_zero(p_uac_se_hdr)) {
+			rtn = parse_session_expires(p_uac_se_hdr, &uac_max_se, &st_ref);
+			if (rtn != 0) {
+				transmit_response(p, "400 Session-Expires Invalid Syntax", req);
+				p->invitestate = INV_COMPLETED;
+				if (!p->lastinvite) {
+					sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
+				}
+				return -1;
+			}
+		}
+
+		/* Parse the Min-SE header */
+		p_uac_min_se = get_header(req, "Min-SE");
+		if (!ast_strlen_zero(p_uac_min_se)) {
+			rtn = parse_minse(p_uac_min_se, &uac_min_se); 
+			if (rtn != 0) {
+        			transmit_response(p, "400 Min-SE Invalid Syntax", req);
+       	   			p->invitestate = INV_COMPLETED;
+       	   			if (!p->lastinvite) {
+					sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
+				}
+				return -1;
+			}
+		}
+
+		dlg_min_se = st_get_min_se(p);
+		switch (st_get_mode(p)) {
+			case SESSION_TIMER_MODE_ACCEPT:
+			case SESSION_TIMER_MODE_ORIGINATE:
+				if (uac_max_se > 0 && uac_max_se < dlg_min_se) {
+					transmit_response_with_minse(p, "422 Session Interval Too Small", req, dlg_min_se);
+					p->invitestate = INV_COMPLETED;
+					if (!p->lastinvite) {
+						sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
+					}
+					return -1;
+				}
+
+				p->st_active_peer_ua = TRUE;
+				st_active = TRUE;
+				if (st_ref == SESSION_TIMER_REFRESHER_AUTO) {
+					st_ref = st_get_refresher(p);
+				}
+
+				if (uac_max_se > 0) {
+					int dlg_max_se = st_get_max_se(p);
+					if (dlg_max_se >= uac_min_se) {
+						st_interval = (uac_max_se < dlg_max_se) ? uac_max_se : dlg_max_se;
+					} else {
+						st_interval = uac_max_se;
+					}
+				} else {
+					st_interval = uac_min_se;
+				}
+				break;
+
+			case SESSION_TIMER_MODE_REFUSE:
+				if (p->reqsipoptions == SIP_OPT_TIMER) {
+					transmit_response_with_unsupported(p, "420 Option Disabled", req, required);
+					ast_log(LOG_WARNING,"Received SIP INVITE with supported but disabled option: %s\n", required);
+					p->invitestate = INV_COMPLETED;
+					if (!p->lastinvite) {
+						sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
+					}
+					return -1;
+				}
+				break;
+
+			default:
+				ast_log(LOG_ERROR, "Internal Error %d at %s:%d\n", st_get_mode(p), __FILE__, __LINE__);
+				break;
+		}
+	} else {
+		/* The UAC did not request session-timers.  Asterisk (UAS), will now decide
+		(based on session-timer-mode in sip.conf) whether to run session-timers for
+		this session or not. */
+		p->st_active_peer_ua = FALSE;
+		switch (st_get_mode(p)) {
+			case SESSION_TIMER_MODE_ORIGINATE:
+				st_active = TRUE;
+				st_interval = st_get_max_se(p);
+				st_ref = SESSION_TIMER_REFRESHER_UAS;
+				break;
+
+			default:
+				break;
+		}
+	}
+
+	if (reinvite == 0) {
+		/* Session-Timers: Start session refresh timer based on negotiation/config */
+		if (st_active == TRUE) {
+			p->st_active   = TRUE;
+			p->st_interval = st_interval;
+			p->st_ref      = st_ref;
+			start_session_timer(p);
+		}
+	} else {
+		if (p->st_active == TRUE) {
+			/* Session-Timers:  A re-invite request sent within a dialog will serve as 
+			a refresh request, no matter whether the re-invite was sent for refreshing 
+			the session or modifying it.*/
+			ast_debug (2, "Restarting session-timers on a refresh - %s\n", p->callid);
+
+			/* The UAC may be adjusting the session-timers mid-session */
+			if (st_interval > 0) {
+				p->st_interval = st_interval;
+				p->st_ref      = st_ref;
+			}
+
+			restart_session_timer(p);
+			if (p->st_expirys > 0) {
+				p->st_expirys--;
+			}
+		}
 	}
 
 	if (!req->ignore && p)
@@ -15080,9 +15555,10 @@
 					}
 				} 
 				/* Respond to normal re-invite */
-				if (sendok)
+				if (sendok) {
 					/* If this is not a re-invite or something to ignore - it's critical */
-					transmit_response_with_sdp(p, "200 OK", req, (reinvite || req->ignore) ?  XMIT_UNRELIABLE : XMIT_CRITICAL);
+					transmit_response_with_sdp(p, "200 OK", req, (reinvite || req->ignore) ?  XMIT_UNRELIABLE : XMIT_CRITICAL, p->session_modify == TRUE ? FALSE:TRUE); 
+				}
 			}
 			p->invitestate = INV_TERMINATED;
 			break;
@@ -15702,6 +16178,8 @@
 	}
 
 	stop_media_flows(p); /* Immediately stop RTP, VRTP and UDPTL as applicable */
+    stop_session_timer(p); /* Stop Session-Timer */
+
 
 	if (!ast_strlen_zero(get_header(req, "Also"))) {
 		ast_log(LOG_NOTICE, "Client '%s' using deprecated BYE/Also transfer method.  Ask vendor to support REFER instead\n",
@@ -16680,6 +17158,263 @@
 	return 0;
 }
 
+
+/*! \brief Session-Timers: Restart session timer */
+static void restart_session_timer(struct sip_pvt *p)
+{
+	if (p->st_active == TRUE) {
+		if (ast_sched_del(sched, p->st_schedid) != 0) {
+			ast_log(LOG_WARNING, "ast_sched_del failed: %d - %s\n", p->st_schedid, p->callid);
+		}
+
+		ast_debug(2, "Session timer stopped: %d - %s\n", p->st_schedid, p->callid);
+		start_session_timer(p);
+	}
+}
+
+
+/*! \brief Session-Timers: Stop session timer */
+static void stop_session_timer(struct sip_pvt *p)
+{
+	if (p->st_active == TRUE) {
+		p->st_active = FALSE;
+		ast_sched_del(sched, p->st_schedid);
+		ast_debug(2, "Session timer stopped: %d - %s\n", p->st_schedid, p->callid);
+	}
+}
+
+
+/*! \brief Session-Timers: Start session timer */
+static void start_session_timer(struct sip_pvt *p)
+{
+	p->st_schedid  = ast_sched_add(sched, p->st_interval * 1000 / 2, proc_session_timer, p);
+	if (p->st_schedid < 0) {
+		ast_log(LOG_ERROR, "ast_sched_add failed.\n");

[... 406 lines stripped ...]



More information about the svn-commits mailing list