[asterisk-commits] branch oej/subscribemwi r10625 - /team/oej/subscribemwi/channels/chan_sip.c

asterisk-commits at lists.digium.com asterisk-commits at lists.digium.com
Tue Feb 21 03:43:07 MST 2006


Author: oej
Date: Tue Feb 21 04:43:05 2006
New Revision: 10625

URL: http://svn.digium.com/view/asterisk?rev=10625&view=rev
Log:
- Handle re-subscriptions properly, authenticate those too

Modified:
    team/oej/subscribemwi/channels/chan_sip.c

Modified: team/oej/subscribemwi/channels/chan_sip.c
URL: http://svn.digium.com/view/asterisk/team/oej/subscribemwi/channels/chan_sip.c?rev=10625&r1=10624&r2=10625&view=diff
==============================================================================
--- team/oej/subscribemwi/channels/chan_sip.c (original)
+++ team/oej/subscribemwi/channels/chan_sip.c Tue Feb 21 04:43:05 2006
@@ -6542,7 +6542,8 @@
 					update_peer(peer, p->expiry);
 					/* Say OK and ask subsystem to retransmit msg counter */
 					transmit_response_with_date(p, "200 OK", req);
-					peer->lastmsgssent = -1;
+					if (!ast_test_flag((&peer->flags_page2), SIP_PAGE2_SUBSCRIBEMWIONLY))
+						peer->lastmsgssent = -1;
 					res = 0;
 					break;
 				}
@@ -10789,6 +10790,8 @@
 	int res = 0;
 	int firststate = AST_EXTENSION_REMOVED;
 	struct sip_peer *authpeer = NULL;
+	char *event = get_header(req, "Event");	/* Get Event package name */
+	char *accept = get_header(req, "Accept");
 
 	if (p->initreq.headers) {	
 		/* We already have a dialog */
@@ -10806,7 +10809,7 @@
 				ast_log(LOG_DEBUG, "Got a new subscription %s (possibly with auth)\n", p->callid);
 		}
 	}
-	if (!ignore && !p->initreq.headers) {
+	if (!ignore && !p->initreq.headers) {	/* Set up dialog, new subscription */
 		/* Use this as the basis */
 		if (debug)
 			ast_verbose("Creating new subscription\n");
@@ -10818,99 +10821,87 @@
 	} else if (debug && ignore)
 		ast_verbose("Ignoring this SUBSCRIBE request\n");
 
-	if (!p->lastinvite) {
-
-		char *event = get_header(req, "Event");	/* Get Event package name */
-		char *accept = get_header(req, "Accept");
-
-		/* Handle authentication if this is our first subscribe */
-		res = check_user_full(p, req, SIP_SUBSCRIBE, e, 0, sin, ignore, &authpeer);
-		if (res) {
-			if (res < 0) {
-				ast_log(LOG_NOTICE, "Failed to authenticate user %s for SUBSCRIBE\n", get_header(req, "From"));
+
+
+	/* Handle authentication if this is our first subscribe */
+	res = check_user_full(p, req, SIP_SUBSCRIBE, e, 0, sin, ignore, &authpeer);
+	if (res) {
+		if (res < 0) {
+			ast_log(LOG_NOTICE, "Failed to authenticate user %s for SUBSCRIBE\n", get_header(req, "From"));
+			ast_set_flag(p, SIP_NEEDDESTROY);	
+		}
+		return 0;
+	}
+
+	/* Initialize the context if it hasn't been already */
+	if (!ast_strlen_zero(p->subscribecontext))
+		ast_string_field_set(p, context, p->subscribecontext);
+	else if (ast_strlen_zero(p->context))
+		ast_string_field_set(p, context, default_context);
+
+	/* Get destination right away */
+	gotdest = get_destination(p, NULL);
+	build_contact(p);
+	if (gotdest) {
+		if (gotdest < 0)
+			transmit_response(p, "404 Not Found (gotdest)", req);
+		else
+			transmit_response(p, "484 Address Incomplete", req);	/* Overlap dialing on SUBSCRIBE?? */
+		ast_set_flag(p, SIP_NEEDDESTROY);	
+		return 0;
+	} else {
+
+		/* Initialize tag for new subscriptions */	
+		if (ast_strlen_zero(p->tag))
+			make_our_tag(p->tag, sizeof(p->tag));
+
+		if (!strcmp(event, "presence") || !strcmp(event, "dialog")) { /* Presence, RFC 3842 */
+
+			/* Header from Xten Eye-beam Accept: multipart/related, application/rlmi+xml, application/pidf+xml, application/xpidf+xml */
+ 			if (strstr(accept, "application/pidf+xml")) {
+ 				p->subscribed = PIDF_XML;         /* RFC 3863 format */
+ 			} else if (strstr(accept, "application/dialog-info+xml")) {
+ 				p->subscribed = DIALOG_INFO_XML;
+ 				/* IETF draft: draft-ietf-sipping-dialog-package-05.txt */
+ 			} else if (strstr(accept, "application/cpim-pidf+xml")) {
+ 				p->subscribed = CPIM_PIDF_XML;    /* RFC 3863 format */
+ 			} else if (strstr(accept, "application/xpidf+xml")) {
+ 				p->subscribed = XPIDF_XML;        /* Early pre-RFC 3863 format with MSN additions (Microsoft Messenger) */
+ 			} else if (strstr(p->useragent, "Polycom")) {
+ 				p->subscribed = XPIDF_XML;        /*  Polycoms subscribe for "event: dialog" but don't include an "accept:" header */
+			} else {
+ 				/* Can't find a format for events that we know about */
+ 				transmit_response(p, "489 Bad Event", req);
+ 				ast_set_flag(p, SIP_NEEDDESTROY);	
+ 				return 0;
+ 			}
+ 		} else if (!strcmp(event, "message-summary") && !strcmp(accept, "application/simple-message-summary")) {
+			/* Looks like they actually want a mailbox status 
+			  This version of Asterisk supports mailbox subscriptions
+
+			*/
+			if (!authpeer || ast_strlen_zero(authpeer->mailbox)) {
+				transmit_response(p, "404 Not found (no mailbox)", req);
 				ast_set_flag(p, SIP_NEEDDESTROY);	
+				ast_log(LOG_NOTICE, "Received SIP subscribe for peer without mailbox: %s\n", authpeer->name);
+				return 0;
 			}
-			return 0;
-		}
-
-		/* Initialize the context if it hasn't been already */
-		if (!ast_strlen_zero(p->subscribecontext))
-			ast_string_field_set(p, context, p->subscribecontext);
-		else if (ast_strlen_zero(p->context))
-			ast_string_field_set(p, context, default_context);
-
-		/* Get destination right away */
-		gotdest = get_destination(p, NULL);
-		build_contact(p);
-		if (gotdest) {
-			if (gotdest < 0)
-				transmit_response(p, "404 Not Found (gotdest)", req);
-			else
-				transmit_response(p, "484 Address Incomplete", req);	/* Overlap dialing on SUBSCRIBE?? */
+
+			transmit_response(p, "200 OK", req);
+ 			p->subscribed = MWI_NOTIFICATION;
+			if (authpeer->mwipvt != p)
+				sip_destroy(authpeer->mwipvt);
+			authpeer->mwipvt = p;	/* Link from peer to pvt */
+			p->relatedpeer = authpeer;	/* Link from pvt to peer */
+		} else { /* At this point, Asterisk does not understand the specified event */
+			transmit_response(p, "489 Bad Event", req);
+			if (option_debug > 1)
+				ast_log(LOG_DEBUG, "Received SIP subscribe for unknown event package: %s\n", event);
 			ast_set_flag(p, SIP_NEEDDESTROY);	
 			return 0;
-		} else {
-
-			/* Initialize tag for new subscriptions */	
-			if (ast_strlen_zero(p->tag))
-				make_our_tag(p->tag, sizeof(p->tag));
-
-			if (!strcmp(event, "presence") || !strcmp(event, "dialog")) { /* Presence, RFC 3842 */
-
- 				/* Header from Xten Eye-beam Accept: multipart/related, application/rlmi+xml, application/pidf+xml, application/xpidf+xml */
- 				if (strstr(accept, "application/pidf+xml")) {
- 					p->subscribed = PIDF_XML;         /* RFC 3863 format */
- 				} else if (strstr(accept, "application/dialog-info+xml")) {
- 					p->subscribed = DIALOG_INFO_XML;
- 					/* IETF draft: draft-ietf-sipping-dialog-package-05.txt */
- 				} else if (strstr(accept, "application/cpim-pidf+xml")) {
- 					p->subscribed = CPIM_PIDF_XML;    /* RFC 3863 format */
- 				} else if (strstr(accept, "application/xpidf+xml")) {
- 					p->subscribed = XPIDF_XML;        /* Early pre-RFC 3863 format with MSN additions (Microsoft Messenger) */
- 				} else if (strstr(p->useragent, "Polycom")) {
- 					p->subscribed = XPIDF_XML;        /*  Polycoms subscribe for "event: dialog" but don't include an "accept:" header */
-				} else {
- 					/* Can't find a format for events that we know about */
- 					transmit_response(p, "489 Bad Event", req);
- 					ast_set_flag(p, SIP_NEEDDESTROY);	
- 					return 0;
- 				}
- 			} else if (!strcmp(event, "message-summary") && !strcmp(accept, "application/simple-message-summary")) {
-				/* Looks like they actually want a mailbox status */
-
-				/* At this point, we should check if they subscribe to a mailbox that
-				  has the same extension as the peer or the mailbox id. If we configure
-				  the context to be the same as a SIP domain, we could check mailbox
-				  context as well. To be able to securely accept subscribes on mailbox
-				  IDs, not extensions, we need to check the digest auth user to make
-				  sure that the user has access to the mailbox.
-				 
-				  Since we do not act on this subscribe anyway, we might as well 
-				  accept any authenticated peer with a mailbox definition in their 
-				  config section.
-				
-				*/
-				if (!authpeer || ast_strlen_zero(authpeer->mailbox)) {
-					transmit_response(p, "404 Not found (no mailbox)", req);
-					ast_set_flag(p, SIP_NEEDDESTROY);	
-					ast_log(LOG_NOTICE, "Received SIP subscribe for peer without mailbox: %s\n", authpeer->name);
-					return 0;
-				}
-
-				transmit_response(p, "200 OK", req);
- 				p->subscribed = MWI_NOTIFICATION;
-				authpeer->mwipvt = p;	/* Link from peer to pvt */
-				p->relatedpeer = authpeer;	/* Link from pvt to peer */
-			} else { /* At this point, Asterisk does not understand the specified event */
-				transmit_response(p, "489 Bad Event", req);
-				if (option_debug > 1)
-					ast_log(LOG_DEBUG, "Received SIP subscribe for unknown event package: %s\n", event);
-				ast_set_flag(p, SIP_NEEDDESTROY);	
-				return 0;
-			}
-			if (p->subscribed != MWI_NOTIFICATION)
-				p->stateid = ast_extension_state_add(p->context, p->exten, cb_extensionstate, p);
-		}
+		}
+		if (p->subscribed != MWI_NOTIFICATION)
+			p->stateid = ast_extension_state_add(p->context, p->exten, cb_extensionstate, p);
 	}
 
 	if (!ignore && p)
@@ -10925,8 +10916,8 @@
 			p->expiry = min_expiry;
 
 		if (sipdebug || option_debug > 1) {
-			if (p->subscribed == MWI_NOTIFICATION)
-				ast_log(LOG_DEBUG, "Adding subscription for mailbox notification - peer %s Mailbox %s\n", authpeer->name, authpeer->mailbox);
+			if (p->subscribed == MWI_NOTIFICATION && p->relatedpeer)
+				ast_log(LOG_DEBUG, "Adding subscription for mailbox notification - peer %s Mailbox %s\n", p->relatedpeer->name, p->relatedpeer->mailbox);
 			else
 				ast_log(LOG_DEBUG, "Adding subscription for extension %s context %s for peer %s\n", p->exten, p->context, p->username);
 		}
@@ -10936,10 +10927,11 @@
 			sip_scheddestroy(p, (p->expiry + 10) * 1000);	/* Set timer for destruction of call at expiration */
 
 		if (p->subscribed == MWI_NOTIFICATION) {
-			if (authpeer)	/* Send first notification */
-				ASTOBJ_WRLOCK(authpeer);
-				sip_send_mwi_to_peer(authpeer);
-				ASTOBJ_UNLOCK(authpeer);
+			if (p->relatedpeer) {	/* Send first notification */
+				ASTOBJ_WRLOCK(p->relatedpeer);
+				sip_send_mwi_to_peer(p->relatedpeer);
+				ASTOBJ_UNLOCK(p->relatedpeer);
+			}
 		} else {
 			if ((firststate = ast_extension_state(NULL, p->context, p->exten)) < 0) {
 				ast_log(LOG_ERROR, "Got SUBSCRIBE for extensions without hint. Please add hint to %s in context %s\n", p->exten, p->context);
@@ -11460,16 +11452,25 @@
 			/* If this peer requires subscription for mwi, check if we have
 				a subscription */
 			if(ast_test_flag((&peer->flags_page2), SIP_PAGE2_SUBSCRIBEMWIONLY)) {
-				if (!peer->mwipvt)
+				if (!peer->mwipvt) {
 					sendmwi = FALSE;
+					time(&peer->lastmsgcheck);	/* Reset timer */
+					//OEJ
+					ast_log(LOG_DEBUG, "Not sending MWI - no subscription for peer %s\n", peer->name);
+				} else {
+					//OEJ
+					ast_log(LOG_DEBUG, "Sending MWI to peer - active subscription for peer %s\n", peer->name);
+				}
 			
-			}
+			} else
+				ast_log(LOG_DEBUG, "Automatically always sending MWI to peer %s\n", peer->name);
+
 			if (sendmwi) {
 				ASTOBJ_WRLOCK(peer);
 				sip_send_mwi_to_peer(peer);
 				ASTOBJ_UNLOCK(peer);
-				ASTOBJ_UNREF(peer,sip_destroy_peer);
 			}
+			ASTOBJ_UNREF(peer,sip_destroy_peer);
 		} else {
 			/* Reset where we come from */
 			lastpeernum = -1;
@@ -12307,7 +12308,7 @@
 			ast_copy_string(peer->musicclass, v->value, sizeof(peer->musicclass));
 		} else if (!strcasecmp(v->name, "mailbox")) {
 			ast_copy_string(peer->mailbox, v->value, sizeof(peer->mailbox));
-		} else if (realtime && !strcasecmp(v->name, "subscribemwi")) {
+		} else if (!strcasecmp(v->name, "subscribemwi")) {
 			if (ast_true(v->value))
 				ast_set_flag((&peer->flags_page2), SIP_PAGE2_SUBSCRIBEMWIONLY);
 		} else if (!strcasecmp(v->name, "vmexten")) {



More information about the asterisk-commits mailing list