[asterisk-commits] russell: branch seanbright/issue5014 r145201 - /team/seanbright/issue5014/cha...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Tue Sep 30 16:14:57 CDT 2008


Author: russell
Date: Tue Sep 30 16:14:56 2008
New Revision: 145201

URL: http://svn.digium.com/view/asterisk?view=rev&rev=145201
Log:
 - kill off pickup_target struct and localize its usage to the function it was
   used in
 - use a single call to ast_str_append() 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/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=145201&r1=145200&r2=145201
==============================================================================
--- team/seanbright/issue5014/channels/chan_sip.c (original)
+++ team/seanbright/issue5014/channels/chan_sip.c Tue Sep 30 16:14:56 2008
@@ -1151,13 +1151,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 context for tech independent pickup.
- */
-struct pickup_target {
-	char *exten;
-	char *context;
 };
 
 /*! \brief generic struct to map between strings and integers.
@@ -9379,18 +9372,21 @@
 		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_str_append(&tmp, 0, "<dialog id=\"%s\" call-id=\"pickup-%s\" direction=\"recipient\">\n", p->exten, p->callid);
-			ast_str_append(&tmp, 0, "<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_str_append(&tmp, 0, "<identity>%s</identity>\n", mto);
-			ast_str_append(&tmp, 0, "<target uri=\"%s\"/>\n", mto);
-			ast_str_append(&tmp, 0, "</remote>\n");
-			ast_str_append(&tmp, 0, "<local>\n");
-			ast_str_append(&tmp, 0, "<identity>%s</identity>\n", mto);
-			ast_str_append(&tmp, 0, "<target uri=\"%s\"/>\n", mto);
-			ast_str_append(&tmp, 0, "</local>\n");
+			ast_str_append(&tmp, 0, 
+					"<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_str_append(&tmp, 0, "<dialog id=\"%s\">\n", p->exten);
 		}
@@ -17140,8 +17136,7 @@
 
 static int do_magic_pickup(struct ast_channel *channel, const char *extension, const char *context)
 {
-	char *argument = NULL;
-	int length;
+	struct ast_str *str = ast_str_alloca(AST_MAX_EXTENSION + AST_MAX_CONTEXT + 2);
 	struct ast_app *pickup = pbx_findapp("Pickup");
 
 	if (!pickup) {
@@ -17149,24 +17144,14 @@
 		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);
+	ast_str_set(&str, 0, "%s@%s", extension, context);
+
+	ast_debug(2, "About to call Pickup(%s)\n", str->str);
 
 	/* 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);
+	pbx_exec(channel, pickup, str->str);
 
 	return 0;
 }
@@ -17197,7 +17182,12 @@
 	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;
+	struct {
+		char exten[AST_MAX_EXTENSION];
+		char context[AST_MAX_CONTEXT];
+	} pickup = {
+		.exten = "",	
+	};
 	st_ref = SESSION_TIMER_REFRESHER_AUTO;
 
 	/* Find out what they support */
@@ -17353,22 +17343,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_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);
-					}
+				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));
+				sip_pvt_unlock(subscription);
+				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;
@@ -17387,7 +17372,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 */
@@ -17395,7 +17380,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;
@@ -17708,7 +17693,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 */
@@ -17718,15 +17703,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