--- /usr/src/1/asterisk-1.4.13/channels/chan_sip.c	2007-10-10 10:42:00.000000000 -0400
+++ /usr/src/asterisk-1.4.13/channels/chan_sip.c	2007-10-11 01:08:39.623258000 -0400
@@ -4785,6 +4785,8 @@
 	char *boundary;
 	unsigned int x;
 	int boundaryisquoted = FALSE;
+	unsigned int found_application_sdp;
+	unsigned int found_end_of_headers;
 
 	content_type = get_header(req, "Content-Type");
 
@@ -4802,7 +4804,6 @@
 	/* if there is no boundary marker, it's invalid */
 	if (!(search = strcasestr(content_type, ";boundary=")))
 		return 0;
-
 	search += 10;
 	if (ast_strlen_zero(search))
 		return 0;
@@ -4817,31 +4818,39 @@
 	   at the beginning */
 	boundary = ast_strdupa(search - 2);
 	boundary[0] = boundary[1] = '-';
-
 	/* Remove final quote */
 	if (boundaryisquoted)
 		boundary[strlen(boundary) - 1] = '\0';
 
-	/* search for the boundary marker, but stop when there are not enough
-	   lines left for it, the Content-Type header and at least one line of
-	   body */
-	for (x = 0; x < (req->lines - 2); x++) {
-		if (!strncasecmp(req->line[x], boundary, strlen(boundary)) &&
-		    !strcasecmp(req->line[x + 1], "Content-Type: application/sdp")) {
-			x += 2;
-			req->sdp_start = x;
-
-			/* search for the end of the body part */
-			for ( ; x < req->lines; x++) {
-				if (!strncasecmp(req->line[x], boundary, strlen(boundary)))
-					break;
+	/* search for the boundary marker, the empty line delimiting headers from
+	sdp part and the end boundry if it exists */
+
+	   found_application_sdp = 0;
+	   found_end_of_headers = 0;
+	for (x = 0; x < (req->lines ); x++) {
+		if(!strncasecmp(req->line[x], boundary, strlen(boundary))){
+			if(found_application_sdp && found_end_of_headers){
+				req->sdp_end = x-1;
+				return 1;
+			}
+			found_application_sdp=0;
+		}
+		if(!strcasecmp(req->line[x], "Content-Type: application/sdp")){
+			found_application_sdp=1;
+		}
+		if(strlen(req->line[x]) == 0 ){
+			if(found_application_sdp && !found_end_of_headers){
+				req->sdp_start = x;
+				found_end_of_headers = 1;
 			}
-			req->sdp_end = x;
-			return 1;
 		}
 	}
-
-	return 0;
+	if(found_application_sdp && found_end_of_headers)
+			{
+				req->sdp_end = x;
+				return 1;
+			}
+	else return 0;
 }
 
 /*! \brief Change hold state for a call */
@@ -4920,6 +4929,10 @@
 		ast_log(LOG_ERROR, "Got SDP but have no RTP session allocated.\n");
 		return -1;
 	}
+	
+
+	ast_log(LOG_WARNING, "In Process sdp\n");
+
 
 	/* Initialize the temporary RTP structures we use to evaluate the offer from the peer */
 	newaudiortp = alloca(ast_rtp_alloc_size());
@@ -4972,6 +4985,9 @@
 		if ((sscanf(m, "audio %d/%d RTP/AVP %n", &x, &numberofports, &len) == 2) ||
 		    (sscanf(m, "audio %d RTP/AVP %n", &x, &len) == 1)) {
 			audio = TRUE;
+
+			ast_log(LOG_WARNING, "In Audio=true\n");
+
 			numberofmediastreams++;
 			/* Found audio stream in this media definition */
 			portno = x;
@@ -13400,6 +13416,8 @@
 			parse_sip_options(p, supported);
 	}
 
+	ast_log(LOG_WARNING, "In function handle request invite 1\n");
+
 	/* Find out what they require */
 	required = get_header(req, "Require");
 	if (!ast_strlen_zero(required)) {
@@ -13467,7 +13485,7 @@
 			p->invitestate = INV_COMPLETED;
 			return -1;
 		}
-
+		ast_log(LOG_WARNING, "In function handle request invite 2\n");
 		/*  Todo: (When we find phones that support this)
 			if the replaces header contains ";early-only"
 			we can only replace the call in early
@@ -13493,7 +13511,7 @@
 		if (sipdebug && option_debug > 3) 
 			ast_log(LOG_DEBUG,"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>");
 
-
+		ast_log(LOG_WARNING, "In function handle request invite 3\n");
 		/* 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 
 		*/
@@ -13543,13 +13561,13 @@
 		}
 	}
 
-
+	ast_log(LOG_WARNING, "In function handle request invite 4\n");
 	/* Check if this is an INVITE that sets up a new dialog or
 	   a re-invite in an existing dialog */
 
 	if (!ast_test_flag(req, SIP_PKT_IGNORE)) {
 		int newcall = (p->initreq.headers ? TRUE : FALSE);
-
+		ast_log(LOG_WARNING, "In function handle request invite 5\n");
 		sip_cancel_destroy(p);
 		/* This also counts as a pending invite */
 		p->pendinginvite = seqno;
@@ -13557,6 +13575,7 @@
 
 		copy_request(&p->initreq, req);		/* Save this INVITE as the transaction basis */
 		if (!p->owner) {	/* Not a re-invite */
+		ast_log(LOG_WARNING, "In function handle request invite 6\n");
 			if (debug)
 				ast_verbose("Using INVITE request as basis request - %s\n", p->callid);
 			if (newcall)
@@ -13565,7 +13584,14 @@
 		} else {	/* Re-invite on existing call */
 			ast_clear_flag(&p->flags[0], SIP_OUTGOING);	/* This is now an inbound dialog */
 			/* Handle SDP here if we already have an owner */
+
+		ast_log(LOG_WARNING, "In hadle invite\n");
+
 			if (find_sdp(req)) {
+
+				ast_log(LOG_WARNING, " In hadle invite found sdp\n");
+
+
 				if (process_sdp(p, req)) {
 					transmit_response(p, "488 Not acceptable here", req);
 					if (!p->lastinvite)
@@ -13587,7 +13613,7 @@
 		}
 	} else if (debug)
 		ast_verbose("Ignoring this INVITE request\n");
-
+ast_log(LOG_WARNING, "In function handle request invite 7\n");
 	
 	if (!p->lastinvite && !ast_test_flag(req, SIP_PKT_IGNORE) && !p->owner) {
 		/* This is a new invite */
@@ -13610,9 +13636,10 @@
 			ast_string_field_free(p, theirtag);
 			return 0;
 		}
-
+ast_log(LOG_WARNING, "In function handle request invite 8\n");
 		/* We have a succesful authentication, process the SDP portion if there is one */
 		if (find_sdp(req)) {
+			ast_log(LOG_WARNING, "In function handle request invite 9\n");
 			if (process_sdp(p, req)) {
 				/* Unacceptable codecs */
 				transmit_response_reliable(p, "488 Not acceptable here", req);
@@ -14892,6 +14919,9 @@
 	char *e;
 	int error = 0;
 
+	ast_log(LOG_WARNING, " In handle\n");
+
+
 	/* Get Method and Cseq */
 	cseq = get_header(req, "Cseq");
 	cmd = req->header[0];
@@ -15030,6 +15060,7 @@
 		res = handle_request_options(p, req);
 		break;
 	case SIP_INVITE:
+		ast_log(LOG_WARNING, " In handle sip_invite\n");
 		res = handle_request_invite(p, req, debug, seqno, sin, recount, e, nounlock);
 		break;
 	case SIP_REFER:
