[asterisk-commits] seanbright: branch seanbright/issue5014-1.4 r145258 - /team/seanbright/issue5...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Tue Sep 30 17:26:05 CDT 2008


Author: seanbright
Date: Tue Sep 30 17:26:04 2008
New Revision: 145258

URL: http://svn.digium.com/view/asterisk?view=rev&rev=145258
Log:
 - kill off pickup_target struct and localize its usage to the function it was
   used in
 - use a single call to ast_build_string() for inserting dialog-info bits
 - use stack instead of heap for the Pickup app args in the magic pickup function
 - use stack for the pickup target info in the invite handler

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

Modified: team/seanbright/issue5014-1.4/channels/chan_sip.c
URL: http://svn.digium.com/view/asterisk/team/seanbright/issue5014-1.4/channels/chan_sip.c?view=diff&rev=145258&r1=145257&r2=145258
==============================================================================
--- team/seanbright/issue5014-1.4/channels/chan_sip.c (original)
+++ team/seanbright/issue5014-1.4/channels/chan_sip.c Tue Sep 30 17:26:04 2008
@@ -873,13 +873,6 @@
         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 contact for tech independent pickup.
- */
-struct pickup_target {
-	char *exten;
-	char *context;
 };
 
 static const struct c_referstatusstring {
@@ -7401,18 +7394,20 @@
 		if ((state & AST_EXTENSION_RINGING) && global_notifyringing) {
 			/* We create a fake call-id which the phone will send back in an INVITE
 			   Replaces header which we can grab and do some magic with. */
-			ast_build_string(&t, &maxbytes, "<dialog id=\"%s\" call-id=\"pickup-%s\" direction=\"recipient\">\n", p->exten, p->callid);
-			ast_build_string(&t, &maxbytes, "<remote>\n");
-			/* Note that the identity and target elements for the local participant are currently
-			   (and may forever be) incorrect since we have no reliable way to get at that information
-			   at the moment.  Luckily the phone seems to still live happily without it being correct */
-			ast_build_string(&t, &maxbytes, "<identity>%s</identity>\n", mto);
-			ast_build_string(&t, &maxbytes, "<target uri=\"%s\"/>\n", mto);
-			ast_build_string(&t, &maxbytes, "</remote>\n");
-			ast_build_string(&t, &maxbytes, "<local>\n");
-			ast_build_string(&t, &maxbytes, "<identity>%s</identity>\n", mto);
-			ast_build_string(&t, &maxbytes, "<target uri=\"%s\"/>\n", mto);
-			ast_build_string(&t, &maxbytes, "</local>\n");
+			ast_build_string(&t, &maxbytes,
+							 "<dialog id=\"%s\" call-id=\"pickup-%s\" direction=\"recipient\">\n"
+							 "<remote>\n"
+							 /* Note that the identity and target elements for the local participant are currently
+								(and may forever be) incorrect since we have no reliable way to get at that information
+								at the moment.  Luckily the phone seems to still live happily without it being correct */
+							 "<identity>%s</identity>\n"
+							 "<target uri=\"%s\"/>\n"
+							 "</remote>\n"
+							 "<local>\n"
+							 "<identity>%s</identity>\n"
+							 "<target uri=\"%s\"/>\n"
+							 "</local>\n",
+							 p->exten, p->callid, mto, mto, mto, mto);
 		} else {
 			ast_build_string(&t, &maxbytes, "<dialog id=\"%s\">\n", p->exten);
 		}
@@ -14105,20 +14100,12 @@
 
 static int do_magic_pickup(struct ast_channel *channel, const char *extension, const char *context)
 {
-	char *argument = NULL;
-	int length;
+	int length = AST_MAX_EXTENSION + AST_MAX_CONTEXT + 2;
+	char *argument = alloca(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;
 	}
 
@@ -14128,8 +14115,6 @@
 	   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;	
 }
@@ -14150,7 +14135,12 @@
 	unsigned int required_profile = 0;
 	struct ast_channel *c = NULL;		/* New channel */
 	int reinvite = 0;
-	struct pickup_target *pickup = NULL;
+	struct {
+		char exten[AST_MAX_EXTENSION];
+		char context[AST_MAX_CONTEXT];
+	} pickup = {
+		.exten = "",
+	};
 
 	/* Find out what they support */
 	if (!p->sipoptions) {
@@ -14300,22 +14290,17 @@
 				transmit_response_reliable(p, "481 Call Leg Does Not Exist (Replaces)", req);
 				error = 1;
 			} else {
-				if (!(pickup = ast_calloc(1, sizeof(*pickup)))) {
-					ast_log(LOG_ERROR, "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);
-					ast_mutex_unlock(&subscription->lock);
-					if (subscription->owner) {
-						ast_channel_unlock(subscription->owner);
-					}
+				ast_log(LOG_NOTICE, "Trying to pick up %s@%s\n", subscription->exten, subscription->context);
+				ast_copy_string(pickup.exten, subscription->exten, sizeof(pickup.exten));
+				ast_copy_string(pickup.context, subscription->context, sizeof(pickup.context));
+				ast_mutex_unlock(&subscription->lock);
+				if (subscription->owner) {
+					ast_channel_unlock(subscription->owner);
 				}
 			}
 		}
 
-		if (!error && !pickup && (p->refer->refer_call = get_sip_pvt_byid_locked(replace_id, totag, fromtag)) == NULL) {
+		if (!error && ast_strlen_zero(pickup.exten) && (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;
@@ -14334,7 +14319,7 @@
 			error = 1;
 		}
 
-		if (!error && !pickup && !p->refer->refer_call->owner) {
+		if (!error && ast_strlen_zero(pickup.exten) && !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 */
@@ -14342,7 +14327,7 @@
 			error = 1;
 		}
 
-		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 ) {
+		if (!error && ast_strlen_zero(pickup.exten) && 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;
@@ -14532,7 +14517,7 @@
 		p->lastinvite = seqno;
 
 	if (replace_id) { 	/* Attended transfer or call pickup - we're the target */
-		if (pickup) {
+		if (!ast_strlen_zero(pickup.exten)) {
 			append_history(p, "Xfer", "INVITE/Replace received");
 
 			/* Let the caller know we're giving it a shot */
@@ -14542,15 +14527,11 @@
 			/* Do the pickup itself */
 			ast_channel_unlock(c);
 			*nounlock = 1;
-			do_magic_pickup(c, pickup->exten, pickup->context);
+			do_magic_pickup(c, pickup.exten, pickup.context);
 
 			/* Now we're either masqueraded or we failed to pickup, in either case we... */
 			ast_hangup(c);
 
-			/* Do a bit of cleanup */
-			ast_free(pickup->exten);
-			ast_free(pickup->context);
-			ast_free(pickup);
 			return 0;
 		} else {
 			/* Go and take over the target call */




More information about the asterisk-commits mailing list