[asterisk-commits] seanbright: branch seanbright/issue5014 r143881 - /team/seanbright/issue5014/...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Sun Sep 21 11:47:28 CDT 2008


Author: seanbright
Date: Sun Sep 21 11:47:28 2008
New Revision: 143881

URL: http://svn.digium.com/view/asterisk?view=rev&rev=143881
Log:
Getting closer... I just need to figure out why the masqueraded channel
isn't dying like a good little dead channel.

Modified:
    team/seanbright/issue5014/channels/chan_sip.c

Modified: team/seanbright/issue5014/channels/chan_sip.c
URL: http://svn.digium.com/view/asterisk/team/seanbright/issue5014/channels/chan_sip.c?view=diff&rev=143881&r1=143880&r2=143881
==============================================================================
--- team/seanbright/issue5014/channels/chan_sip.c (original)
+++ team/seanbright/issue5014/channels/chan_sip.c Sun Sep 21 11:47:28 2008
@@ -1151,6 +1151,13 @@
 	REFER_200OK,                   /*!< Answered by transfer target */
 	REFER_FAILED,                  /*!< REFER declined - go on */
 	REFER_NOAUTH                   /*!< We had no auth for REFER */
+};
+
+/* \brief struct to store off exten and context for tech independent pickup.
+ */
+struct pickup_target {
+	char *exten;
+	char *context;
 };
 
 /*! \brief generic struct to map between strings and integers.
@@ -1957,6 +1964,7 @@
 static int sip_refer_allocate(struct sip_pvt *p);
 static void ast_quiet_chan(struct ast_channel *chan);
 static int attempt_transfer(struct sip_dual *transferer, struct sip_dual *target);
+static int do_magic_pickup(struct ast_channel *channel, const char *extension, const char *context);
 /*!
  * \brief generic function for determining if a correct transport is being 
  * used to contact a peer
@@ -17122,6 +17130,38 @@
 	return sip_uri_params_cmp(params1, params2);
 }
 
+static int do_magic_pickup(struct ast_channel *channel, const char *extension, const char *context)
+{
+	char *argument = NULL;
+	int length;
+	struct ast_app *pickup = pbx_findapp("Pickup");
+
+	if (!pickup) {
+		ast_log(LOG_ERROR, "Unable to perform pickup: Application 'Pickup' not loaded (app_directed_pickup.so).\n");
+		return -1;
+	}
+
+	/* Enough room for 'extension', 'context', and '@' (and \0) */
+	length = strlen(extension) + strlen(context) + 2;
+
+	if (!(argument = ast_malloc(length))) {
+		ast_log(LOG_ERROR, "Failed to allocate memory for pickup extension.\n");
+		return -1;
+	}
+
+	snprintf(argument, length, "%s@%s", extension, context);
+
+	ast_debug(2, "About to call Pickup(%s)\n", argument);
+
+	/* There is no point in capturing the return value since pickup_exec
+	   doesn't return anything meaningful unless the passed data is an empty
+	   string (which in our case it will not be) */
+	pbx_exec(channel, pickup, argument);
+
+	ast_free(argument);
+
+	return 0;
+}
 
 /*! \brief Handle incoming INVITE request
 \note 	If the INVITE has a Replaces header, it is part of an
@@ -17149,6 +17189,7 @@
 	int st_interval = 0;            /* Session-Timer negotiated refresh interval                */
 	enum st_refresher st_ref;       /* Session-Timer session refresher                          */
 	int dlg_min_se = -1;
+	struct pickup_target *pickup = NULL;
 	st_ref = SESSION_TIMER_REFRESHER_AUTO;
 
 	/* Find out what they support */
@@ -17285,31 +17326,41 @@
 			}
 		}
 
-		if (sipdebug) 
-			ast_debug(4, "Invite/replaces: Will use Replace-Call-ID : %s Fromtag: %s Totag: %s\n", replace_id, fromtag ? fromtag : "<no from tag>", totag ? totag : "<no to tag>");
-
+		if (sipdebug)
+			ast_debug(4, "Invite/replaces: Will use Replace-Call-ID : %s Fromtag: %s Totag: %s\n",
+					  replace_id,
+					  fromtag ? fromtag : "<no from tag>",
+					  totag ? totag : "<no to tag>");
 
 		/* Try to find call that we are replacing.
 		   If we have a Replaces header, we need to cancel that call if we succeed with this call.
 		   First we cheat a little and look for a magic call-id from phones that support
-		   dialog-info+xml...
-		*/
+		   dialog-info+xml so we can do technology independent pickup... */
 		if (strncmp(replace_id, "pickup-", 7) == 0) {
 			struct sip_pvt *subscription = NULL;
-			char *notify_call = &replace_id[7];
-
-			if ((subscription = get_sip_pvt_byid_locked(notify_call, NULL, NULL)) == NULL) {
-				ast_log(LOG_NOTICE, "Unable to find subscription with call-id: %s\n", notify_call);
+			replace_id += 7; /* Worst case we are looking at \0 */
+
+			if ((subscription = get_sip_pvt_byid_locked(replace_id, NULL, NULL)) == NULL) {
+				ast_log(LOG_NOTICE, "Unable to find subscription with call-id: %s\n", replace_id);
 				transmit_response_reliable(p, "481 Call Leg Does Not Exist (Replaces)", req);
 				error = 1;
 			} else {
-				ast_log(LOG_NOTICE, "Found it, we're going to pick up %s@%s!\n", subscription->exten, subscription->context);
-				sip_pvt_unlock(subscription);
-				ast_channel_unlock(subscription->owner);
+				if (!(pickup = ast_calloc(1, sizeof(*pickup)))) {
+					ast_log(LOG_NOTICE, "Memory allocation failed.\n");
+					error = 1;
+				} else {
+					ast_log(LOG_NOTICE, "Trying to pick up %s@%s\n", subscription->exten, subscription->context);
+					pickup->exten = ast_strdup(subscription->exten);
+					pickup->context = ast_strdup(subscription->context);
+					sip_pvt_unlock(subscription);
+					if (subscription->owner) {
+						ast_channel_unlock(subscription->owner);
+					}
+				}
 			}
 		}
 
-		if (!error && (p->refer->refer_call = get_sip_pvt_byid_locked(replace_id, totag, fromtag)) == NULL) {
+		if (!error && !pickup && (p->refer->refer_call = get_sip_pvt_byid_locked(replace_id, totag, fromtag)) == NULL) {
 			ast_log(LOG_NOTICE, "Supervised transfer attempted to replace non-existent call id (%s)!\n", replace_id);
 			transmit_response_reliable(p, "481 Call Leg Does Not Exist (Replaces)", req);
 			error = 1;
@@ -17328,7 +17379,7 @@
 			error = 1;
 		}
 
-		if (!error && !p->refer->refer_call->owner) {
+		if (!error && !pickup && !p->refer->refer_call->owner) {
 			/* Oops, someting wrong anyway, no owner, no call */
 			ast_log(LOG_NOTICE, "Supervised transfer attempted to replace non-existing call id (%s)!\n", replace_id);
 			/* Check for better return code */
@@ -17336,7 +17387,7 @@
 			error = 1;
 		}
 
-		if (!error && p->refer->refer_call->owner->_state != AST_STATE_RINGING && p->refer->refer_call->owner->_state != AST_STATE_RING && p->refer->refer_call->owner->_state != AST_STATE_UP ) {
+		if (!error && !pickup && p->refer->refer_call->owner->_state != AST_STATE_RINGING && p->refer->refer_call->owner->_state != AST_STATE_RING && p->refer->refer_call->owner->_state != AST_STATE_UP) {
 			ast_log(LOG_NOTICE, "Supervised transfer attempted to replace non-ringing or active call id (%s)!\n", replace_id);
 			transmit_response_reliable(p, "603 Declined (Replaces)", req);
 			error = 1;
@@ -17649,10 +17700,29 @@
 		p->lastinvite = seqno;
 
 	if (replace_id) { 	/* Attended transfer or call pickup - we're the target */
-		/* Go and take over the target call */
-		if (sipdebug)
-			ast_debug(4, "Sending this call to the invite/replcaes handler %s\n", p->callid);
-		return handle_invite_replaces(p, req, debug, seqno, sin);
+		if (pickup) {
+			append_history(p, "Xfer", "INVITE/Replace received");
+
+			/* Let the caller know we're giving it a shot */
+			transmit_response(p, "100 Trying", req);
+			ast_setstate(c, AST_STATE_RING);
+			ast_channel_unlock(c);
+
+			/* Do the pickup itself */
+			do_magic_pickup(c, pickup->exten, pickup->context);
+
+			/* Do a bit of cleanup */
+			sip_pvt_unlock(p);
+			ast_free(pickup->exten);
+			ast_free(pickup->context);
+			ast_free(pickup);
+			return 0;
+		} else {
+			/* Go and take over the target call */
+			if (sipdebug)
+				ast_debug(4, "Sending this call to the invite/replcaes handler %s\n", p->callid);
+			return handle_invite_replaces(p, req, debug, seqno, sin);
+		}
 	}
 
 




More information about the asterisk-commits mailing list