[asterisk-commits] branch oej/sipdiversion r18829 - in /team/oej/sipdiversion: ./ channels/

asterisk-commits at lists.digium.com asterisk-commits at lists.digium.com
Mon Apr 10 08:32:25 MST 2006


Author: oej
Date: Mon Apr 10 10:32:20 2006
New Revision: 18829

URL: http://svn.digium.com/view/asterisk?rev=18829&view=rev
Log:
Reset, resolve - go!

Modified:
    team/oej/sipdiversion/   (props changed)
    team/oej/sipdiversion/channels/chan_misdn.c
    team/oej/sipdiversion/channels/chan_sip.c

Propchange: team/oej/sipdiversion/
------------------------------------------------------------------------------
    automerge = http://edvina.net/training/

Propchange: team/oej/sipdiversion/
------------------------------------------------------------------------------
--- svnmerge-integrated (original)
+++ svnmerge-integrated Mon Apr 10 10:32:20 2006
@@ -1,1 +1,1 @@
-/trunk:1-18777
+/trunk:1-18828

Modified: team/oej/sipdiversion/channels/chan_misdn.c
URL: http://svn.digium.com/view/asterisk/team/oej/sipdiversion/channels/chan_misdn.c?rev=18829&r1=18828&r2=18829&view=diff
==============================================================================
--- team/oej/sipdiversion/channels/chan_misdn.c (original)
+++ team/oej/sipdiversion/channels/chan_misdn.c Mon Apr 10 10:32:20 2006
@@ -3556,7 +3556,7 @@
 		
 		send_cause2ast(ch->ast,bc);
 
-		if (misdn_inband_avail(bc) && ch->state != MISDN_CONNECTED) {
+		if ( ch->orginator==ORG_AST && !bc->nt && misdn_inband_avail(bc) && ch->state != MISDN_CONNECTED) {
 			/* If there's inband information available (e.g. a
 			   recorded message saying what was wrong with the
 			   dialled number, or perhaps even giving an
@@ -3588,7 +3588,6 @@
 		*/
 		
 		misdn_lib_send_event(bc,EVENT_RELEASE);
-		
 	}
 	break;
 	

Modified: team/oej/sipdiversion/channels/chan_sip.c
URL: http://svn.digium.com/view/asterisk/team/oej/sipdiversion/channels/chan_sip.c?rev=18829&r1=18828&r2=18829&view=diff
==============================================================================
--- team/oej/sipdiversion/channels/chan_sip.c (original)
+++ team/oej/sipdiversion/channels/chan_sip.c Mon Apr 10 10:32:20 2006
@@ -101,7 +101,6 @@
 #define TRUE 1
 #endif
 
- 
 #define VIDEO_CODEC_MASK	0x1fc0000 /*!< Video codecs from H.261 thru AST_FORMAT_MAX_VIDEO */
 #ifndef IPTOS_MINCOST
 #define IPTOS_MINCOST		0x02
@@ -263,31 +262,6 @@
 	{ SIP_PUBLISH,	 NO_RTP, "PUBLISH" }
 };
 
-/*! \brief Structure for conversion between compressed SIP and "normal" SIP */
-static const struct cfalias {
-	char * const fullname;
-	char * const shortname;
-} aliases[] = {
-	{ "Content-Type", "c" },
-	{ "Content-Encoding", "e" },
-	{ "From", "f" },
-	{ "Call-ID", "i" },
-	{ "Contact", "m" },
-	{ "Content-Length", "l" },
-	{ "Subject", "s" },
-	{ "To", "t" },
-	{ "Supported", "k" },
-	{ "Refer-To", "r" },
-	{ "Referred-By", "b" },
-	{ "Allow-Events", "u" },
-	{ "Event", "o" },
-	{ "Via", "v" },
-	{ "Accept-Contact", "a" },
-	{ "Reject-Contact", "j" },
-	{ "Request-Disposition", "d" },
-	{ "Session-Expires", "x" },
-};
-
 /*!  Define SIP option tags, used in Require: and Supported: headers 
  	We need to be aware of these properties in the phones to use 
 	the replace: header. We should not do that without knowing
@@ -322,7 +296,7 @@
 	int id;			/*!< Bitmap ID */
 	int supported;		/*!< Supported by Asterisk ? */
 	char * const text;	/*!< Text id, as in standard */
-} sip_options[] = {
+} sip_options[] = {	/* XXX used in 3 places */
 	/* Replaces: header for transfer */
 	{ SIP_OPT_REPLACES,	SUPPORTED,	"replaces" },	
 	/* RFC3262: PRACK 100% reliability */
@@ -483,13 +457,31 @@
 	int len;		/*!< Length */
 	int headers;		/*!< # of SIP Headers */
 	int method;		/*!< Method of this request */
+	int lines;		/*!< SDP Content */
+	unsigned int flags;	/*!< SIP_PKT Flags for this packet */
 	char *header[SIP_MAX_HEADERS];
-	int lines;		/*!< SDP Content */
 	char *line[SIP_MAX_LINES];
 	char data[SIP_MAX_PACKET];
-	int debug;		/*!< Debug flag for this packet */
-	unsigned int flags;	/*!< SIP_PKT Flags for this packet */
 };
+
+/*
+ * A sip packet is stored into the data[] buffer, with the header followed
+ * by an empty line and the body of the message.
+ * On outgoing packets, data is accumulated in data[] with len reflecting
+ * the next available byte, headers and lines count the number of lines
+ * in both parts. There are no '\0' in data[0..len-1].
+ *
+ * On received packet, the input read from the socket is copied into data[],
+ * len is set and the string is NUL-terminated. Then a parser fills up
+ * the other fields -header[] and line[] to point to the lines of the
+ * message, rlPart1 and rlPart2 parse the first lnie as below:
+ *
+ * Requests have in the first line	METHOD URI SIP/2.0
+ *	rlPart1 = method; rlPart2 = uri;
+ * Responses have in the first line	SIP/2.0 code description
+ *	rlPart1 = SIP/2.0; rlPart2 = code + description;
+ *
+ */
 
 /*! \brief structure used in transfers */
 struct sip_dual {
@@ -789,6 +781,7 @@
 };
 
 /*! \brief Structure for SIP peer data, we place calls to peers if registered  or fixed IP address (host) */
+/* XXX field 'name' must be first otherwise sip_addrcmp() will fail */
 struct sip_peer {
 	ASTOBJ_COMPONENTS(struct sip_peer);	/*!< name, refcount, objflags,  object pointers */
 					/*!< peer->name is the unique name of this object */
@@ -921,7 +914,7 @@
 /*---------------------------- Forward declarations of functions in chan_sip.c */
 static int transmit_response(struct sip_pvt *p, char *msg, struct sip_request *req);
 static int transmit_response_with_sdp(struct sip_pvt *p, char *msg, struct sip_request *req, enum xmittype reliable);
-static int transmit_response_with_unsupported(struct sip_pvt *p, char *msg, struct sip_request *req, char *unsupported);
+static int transmit_response_with_unsupported(struct sip_pvt *p, const char *msg, struct sip_request *req, const char *unsupported);
 static int transmit_response_with_auth(struct sip_pvt *p, const char *msg, struct sip_request *req, const char *rand, enum xmittype reliable, const char *header, int stale);
 static int transmit_request(struct sip_pvt *p, int sipmethod, int inc, enum xmittype reliable, int newbranch);
 static int transmit_request_with_auth(struct sip_pvt *p, int sipmethod, int inc, enum xmittype reliable, int newbranch);
@@ -966,16 +959,16 @@
 static void sip_dump_history(struct sip_pvt *dialog);	/* Dump history to LOG_DEBUG at end of dialog, before destroying data */
 static const struct cfsubscription_types *find_subscription_type(enum subscriptiontype subtype);
 static int transmit_state_notify(struct sip_pvt *p, int state, int full);
-static char *gettag(struct sip_request *req, char *header, char *tagbuf, int tagbufsize);
-static int find_sip_method(char *msg);
-static unsigned int parse_sip_options(struct sip_pvt *pvt, char *supported);
+static const char *gettag(const struct sip_request *req, char *header, char *tagbuf, int tagbufsize);
+static int find_sip_method(const char *msg);
+static unsigned int parse_sip_options(struct sip_pvt *pvt, const char *supported);
 static void sip_destroy(struct sip_pvt *p);
 static void sip_destroy_peer(struct sip_peer *peer);
 static void sip_destroy_user(struct sip_user *user);
 static void parse_request(struct sip_request *req);
-static char *get_header(struct sip_request *req, const char *name);
+static const char *get_header(const struct sip_request *req, const char *name);
 static void copy_request(struct sip_request *dst,struct sip_request *src);
-static int transmit_response_reliable(struct sip_pvt *p, char *msg, struct sip_request *req);
+static int transmit_response_reliable(struct sip_pvt *p, const char *msg, struct sip_request *req);
 static int transmit_register(struct sip_registry *r, int sipmethod, char *auth, char *authheader);
 static int sip_poke_peer(struct sip_peer *peer);
 static int __sip_do_register(struct sip_registry *r);
@@ -1024,25 +1017,37 @@
 };
 
 
-/*! \brief Find SIP method from header
- * Strictly speaking, SIP methods are case SENSITIVE, but we don't check 
- * following Jon Postel's rule: Be gentle in what you accept, strict with what you send */
-static int find_sip_method(char *msg)
+/*! \brief returns true if 'name' (with optional trailing whitespace)
+ * matches the sip method 'id'.
+ * Strictly speaking, SIP methods are case SENSITIVE, but we do
+ * a case-insensitive comparison to be more tolerant.
+ * following Jon Postel's rule: Be gentle in what you accept, strict with what you send 
+ */
+static int method_match(enum sipmethod id, const char *name)
+{
+	int len = strlen(sip_methods[id].text);
+	int l_name = name ? strlen(name) : 0;
+	/* true if the string is long enough, and ends with whitespace, and matches */
+	return (l_name >= len && name[len] < 33 &&
+		!strncasecmp(sip_methods[id].text, name, len));
+}
+
+/*! \brief  find_sip_method: Find SIP method from header */
+static int find_sip_method(const char *msg)
 {
 	int i, res = 0;
 	
 	if (ast_strlen_zero(msg))
 		return 0;
-
 	for (i = 1; i < (sizeof(sip_methods) / sizeof(sip_methods[0])) && !res; i++) {
-		if (!strcasecmp(sip_methods[i].text, msg)) 
+		if (method_match(i, msg))
 			res = sip_methods[i].id;
 	}
 	return res;
 }
 
 /*! \brief Parse supported header in incoming packet */
-static unsigned int parse_sip_options(struct sip_pvt *pvt, char *supported)
+static unsigned int parse_sip_options(struct sip_pvt *pvt, const char *supported)
 {
 	char *next, *sep;
 	char *temp = ast_strdupa(supported);
@@ -1093,29 +1098,35 @@
 	return 1;
 }
 
+/* The real destination address for a write */
+static const struct sockaddr_in *sip_real_dst(const struct sip_pvt *p)
+{
+	return ast_test_flag(&p->flags[0], SIP_NAT) & SIP_NAT_ROUTE ? &p->recv : &p->sa;
+}
+
+static const char *sip_nat_mode(const struct sip_pvt *p)
+{
+	return ast_test_flag(&p->flags[0], SIP_NAT) & SIP_NAT_ROUTE ? "NAT" : "no NAT";
+}
+
 /*! \brief Test PVT for debugging output */
 static inline int sip_debug_test_pvt(struct sip_pvt *p) 
 {
 	if (!sipdebug)
 		return 0;
-	return sip_debug_test_addr(ast_test_flag(&p->flags[0], SIP_NAT_ROUTE) ? &p->recv : &p->sa);
-}
-
+	return sip_debug_test_addr(sip_real_dst(p));
+}
 
 /*! \brief Transmit SIP message */
 static int __sip_xmit(struct sip_pvt *p, char *data, int len)
 {
 	int res;
 	char iabuf[INET_ADDRSTRLEN];
-
-	if (ast_test_flag(&p->flags[0], SIP_NAT_ROUTE))
-		res=sendto(sipsock, data, len, 0, (struct sockaddr *)&p->recv, sizeof(struct sockaddr_in));
-	else
-		res=sendto(sipsock, data, len, 0, (struct sockaddr *)&p->sa, sizeof(struct sockaddr_in));
-
-	if (res != len) {
-		ast_log(LOG_WARNING, "sip_xmit of %p (len %d) to %s:%d returned %d: %s\n", data, len, ast_inet_ntoa(iabuf, sizeof(iabuf), p->sa.sin_addr), ntohs(p->sa.sin_port), res, strerror(errno));
-	}
+	const struct sockaddr_in *dst = sip_real_dst(p);
+	res=sendto(sipsock, data, len, 0, (const struct sockaddr *)dst, sizeof(struct sockaddr_in));
+
+	if (res != len)
+		ast_log(LOG_WARNING, "sip_xmit of %p (len %d) to %s:%d returned %d: %s\n", data, len, ast_inet_ntoa(iabuf, sizeof(iabuf), dst->sin_addr), ntohs(dst->sin_port), res, strerror(errno));
 	return res;
 }
 
@@ -1329,7 +1340,6 @@
 	pkt->timer_t1 = p->timer_t1;	/* Set SIP timer T1 */
 	if (fatal)
 		ast_set_flag(pkt, FLAG_FATAL);
-
 	if (pkt->timer_t1)
 		siptimer_a = pkt->timer_t1 * 2;
 
@@ -1396,10 +1406,11 @@
 /*! \brief Cancel destruction of SIP dialog */
 static int sip_cancel_destroy(struct sip_pvt *p)
 {
-	if (p->autokillid > -1)
+	if (p->autokillid > -1) {
 		ast_sched_del(sched, p->autokillid);
-	append_history(p, "CancelDestroy", "");
-	p->autokillid = -1;
+		append_history(p, "CancelDestroy", "");
+		p->autokillid = -1;
+	}
 	return 0;
 }
 
@@ -1476,16 +1487,14 @@
 {
 	struct sip_pkt *cur;
 	int res = -1;
-	char *msg = sip_methods[sipmethod].text;
-
-	for (cur = p->packets; cur ; cur = cur->next) {
-		if ((cur->seqno == seqno) && ((ast_test_flag(cur, FLAG_RESPONSE)) == resp) &&
-			((ast_test_flag(cur, FLAG_RESPONSE)) || 
-			 (!strncasecmp(msg, cur->data, strlen(msg)) && (cur->data[strlen(msg)] < 33)))) {
+
+	for (cur = p->packets; cur; cur = cur->next) {
+		if (cur->seqno == seqno && ast_test_flag(cur, FLAG_RESPONSE) == resp &&
+			(ast_test_flag(cur, FLAG_RESPONSE) || method_match(sipmethod, cur->data))) {
 			/* this is our baby */
 			if (cur->retransid > -1) {
 				if (option_debug > 3 && sipdebug)
-					ast_log(LOG_DEBUG, "*** SIP TIMER: Cancelling retransmission #%d - %s (got response)\n", cur->retransid, msg);
+					ast_log(LOG_DEBUG, "*** SIP TIMER: Cancelling retransmission #%d - %s (got response)\n", cur->retransid, sip_methods[sipmethod].text);
 				ast_sched_del(sched, cur->retransid);
 			}
 			cur->retransid = -1;
@@ -1626,6 +1635,7 @@
 	char ipaddr[20];
 	char regseconds[20];
 	time_t nowtime;
+	const char *fc = fullcontact ? "fullcontact" : NULL;
 	
 	time(&nowtime);
 	nowtime += expirey;
@@ -1633,10 +1643,9 @@
 	ast_inet_ntoa(ipaddr, sizeof(ipaddr), sin->sin_addr);
 	snprintf(port, sizeof(port), "%d", ntohs(sin->sin_port));
 	
-	if (fullcontact)
-		ast_update_realtime("sippeers", "name", peername, "ipaddr", ipaddr, "port", port, "regseconds", regseconds, "username", username, "fullcontact", fullcontact, NULL);
-	else
-		ast_update_realtime("sippeers", "name", peername, "ipaddr", ipaddr, "port", port, "regseconds", regseconds, "username", username, NULL);
+	ast_update_realtime("sippeers", "name", peername, "ipaddr", ipaddr,
+		"port", port, "regseconds", regseconds,
+		"username", username, fc, fullcontact, NULL); /* note fc _can_ be NULL */
 }
 
 /*! \brief Automatically add peer extension to dial plan */
@@ -1879,6 +1888,8 @@
 /*! \brief Create address structure from peer reference */
 static int create_addr_from_peer(struct sip_pvt *r, struct sip_peer *peer)
 {
+	int natflags;
+
 	if ((peer->addr.sin_addr.s_addr || peer->defaddr.sin_addr.s_addr) &&
 	    (!peer->maxms || ((peer->lastms >= 0)  && (peer->lastms <= peer->maxms)))) {
 		r->sa = (peer->addr.sin_addr.s_addr) ? peer->addr : peer->defaddr;
@@ -1895,15 +1906,16 @@
 		r->vrtp = NULL;
 	}
 	r->prefs = peer->prefs;
+	natflags = ast_test_flag(&r->flags[0], SIP_NAT) & SIP_NAT_ROUTE;
 	if (r->rtp) {
 		if (option_debug)
-			ast_log(LOG_DEBUG, "Setting NAT on RTP to %d\n", (ast_test_flag(&r->flags[0], SIP_NAT) & SIP_NAT_ROUTE));
-		ast_rtp_setnat(r->rtp, (ast_test_flag(&r->flags[0], SIP_NAT) & SIP_NAT_ROUTE));
+			ast_log(LOG_DEBUG, "Setting NAT on RTP to %d\n", natflags);
+		ast_rtp_setnat(r->rtp, natflags);
 	}
 	if (r->vrtp) {
 		if (option_debug)
-			ast_log(LOG_DEBUG, "Setting NAT on VRTP to %d\n", (ast_test_flag(&r->flags[0], SIP_NAT) & SIP_NAT_ROUTE));
-		ast_rtp_setnat(r->vrtp, (ast_test_flag(&r->flags[0], SIP_NAT) & SIP_NAT_ROUTE));
+			ast_log(LOG_DEBUG, "Setting NAT on VRTP to %d\n", natflags);
+		ast_rtp_setnat(r->vrtp, natflags);
 	}
 	ast_string_field_set(r, peername, peer->username);
 	ast_string_field_set(r, authname, peer->username);
@@ -1927,8 +1939,7 @@
 	if (ast_strlen_zero(r->tohost)) {
 		char iabuf[INET_ADDRSTRLEN];
 
-		ast_inet_ntoa(iabuf, sizeof(iabuf),  peer->addr.sin_addr.s_addr ? peer->addr.sin_addr : peer->defaddr.sin_addr);
-
+		ast_inet_ntoa(iabuf, sizeof(iabuf),  r->sa.sin_addr);
 		ast_string_field_set(r, tohost, iabuf);
 	}
 	if (!ast_strlen_zero(peer->fromdomain))
@@ -1974,10 +1985,8 @@
 
 	ast_copy_string(peer, opeer, sizeof(peer));
 	port = strchr(peer, ':');
-	if (port) {
-		*port = '\0';
-		port++;
-	}
+	if (port)
+		*port++ = '\0';
 	dialog->sa.sin_family = AF_INET;
 	dialog->timer_t1 = 500; /* Default SIP retransmission timer T1 (RFC 3261) */
 	p = find_peer(peer, NULL, 1);
@@ -1992,10 +2001,7 @@
 			return -1;
 
 		hostn = peer;
-		if (port)
-			portno = atoi(port);
-		else
-			portno = DEFAULT_SIP_PORT;
+		portno = port ? atoi(port) : DEFAULT_SIP_PORT;
 		if (srvlookup) {
 			char service[MAXHOSTNAMELEN];
 			int tportno;
@@ -2032,6 +2038,7 @@
 	ast_mutex_lock(&p->lock);
 	p->initid = -1;
 	if (p->owner) {
+		/* XXX fails on possible deadlock */
 		if (!ast_mutex_trylock(&p->owner->lock)) {
 			ast_log(LOG_NOTICE, "Auto-congesting %s\n", p->owner->name);
 			ast_queue_control(p->owner, AST_CONTROL_CONGESTION);
@@ -2198,9 +2205,8 @@
 	/* remove all current packets in this dialog */
 	while((cp = p->packets)) {
 		p->packets = p->packets->next;
-		if (cp->retransid > -1) {
+		if (cp->retransid > -1)
 			ast_sched_del(sched, cp->retransid);
-		}
 		free(cp);
 	}
 	if (p->chanvars) {
@@ -2430,10 +2436,9 @@
    31 normal unspecified                   480 Temporarily unavailable
 \endverbatim
 */
-static char *hangup_cause2sip(int cause)
-{
-	switch(cause)
-	{
+static const char *hangup_cause2sip(int cause)
+{
+	switch (cause) {
 		case AST_CAUSE_UNALLOCATED:		/* 1 */
 		case AST_CAUSE_NO_ROUTE_DESTINATION:	/* 3 IAX2: Can't find extension in context */
 		case AST_CAUSE_NO_ROUTE_TRANSIT_NET:	/* 2 */
@@ -2542,10 +2547,10 @@
 					update_call_counter(p, INC_CALL_LIMIT);
 				}
 			} else {	/* Incoming call, not up */
-				char *res;
-				if (ast->hangupcause && ((res = hangup_cause2sip(ast->hangupcause)))) {
+				const char *res;
+				if (ast->hangupcause && (res = hangup_cause2sip(ast->hangupcause)))
 					transmit_response_reliable(p, res, &p->initreq);
-				} else 
+				else 
 					transmit_response_reliable(p, "603 Declined", &p->initreq);
 			}
 		} else {	/* Call is in UP state, send BYE */
@@ -2669,16 +2674,18 @@
         Basically update any ->owner links */
 static int sip_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
 {
+	int ret = -1;
 	struct sip_pvt *p = newchan->tech_pvt;
+
 	ast_mutex_lock(&p->lock);
-	if (p->owner != oldchan) {
+	if (p->owner != oldchan)
 		ast_log(LOG_WARNING, "old channel wasn't %p but was %p\n", oldchan, p->owner);
-		ast_mutex_unlock(&p->lock);
-		return -1;
-	}
-	p->owner = newchan;
+	else {
+		p->owner = newchan;
+		ret = 0;
+	}
 	ast_mutex_unlock(&p->lock);
-	return 0;
+	return ret;
 }
 
 /*! \brief Send DTMF character on SIP channel
@@ -2901,26 +2908,20 @@
 	if (!ast_strlen_zero(i->exten) && strcmp(i->exten, "s"))
 		tmp->cid.cid_dnid = ast_strdup(i->exten);
 	tmp->priority = 1;
-	if (!ast_strlen_zero(i->uri)) {
+	if (!ast_strlen_zero(i->uri))
 		pbx_builtin_setvar_helper(tmp, "SIPURI", i->uri);
-	}
-	if (!ast_strlen_zero(i->domain)) {
+	if (!ast_strlen_zero(i->domain))
 		pbx_builtin_setvar_helper(tmp, "SIPDOMAIN", i->domain);
-	}
-	if (!ast_strlen_zero(i->useragent)) {
+	if (!ast_strlen_zero(i->useragent))
 		pbx_builtin_setvar_helper(tmp, "SIPUSERAGENT", i->useragent);
-	}
-	if (!ast_strlen_zero(i->callid)) {
+	if (!ast_strlen_zero(i->callid))
 		pbx_builtin_setvar_helper(tmp, "SIPCALLID", i->callid);
-	}
 	ast_setstate(tmp, state);
-	if (state != AST_STATE_DOWN) {
-		if (ast_pbx_start(tmp)) {
-			ast_log(LOG_WARNING, "Unable to start PBX on %s\n", tmp->name);
-			tmp->hangupcause = AST_CAUSE_SWITCH_CONGESTION;
-			ast_hangup(tmp);
-			tmp = NULL;
-		}
+	if (state != AST_STATE_DOWN && ast_pbx_start(tmp)) {
+		ast_log(LOG_WARNING, "Unable to start PBX on %s\n", tmp->name);
+		tmp->hangupcause = AST_CAUSE_SWITCH_CONGESTION;
+		ast_hangup(tmp);
+		tmp = NULL;
 	}
 	/* Set channel variables for this call from configuration */
 	for (v = i->chanvars ; v ; v = v->next)
@@ -2932,52 +2933,64 @@
 }
 
 /*! \brief Reads one line of SIP message body */
-static char* get_sdp_by_line(char* line, char *name, int nameLen)
-{
-	if (strncasecmp(line, name, nameLen) == 0 && line[nameLen] == '=') {
+static const char* get_sdp_by_line(const char* line, const char *name, int nameLen)
+{
+	if (strncasecmp(line, name, nameLen) == 0 && line[nameLen] == '=')
 		return ast_skip_blanks(line + nameLen + 1);
-	}
 	return "";
 }
 
-/*! \brief Gets all kind of SIP message bodies, including SDP,
-   but the name wrongly applies _only_ sdp */
-static char *get_sdp(struct sip_request *req, char *name) 
-{
-	int x;
+/*! \brief get_sdp_iterate: lookup 'name' in the request starting
+ * at the 'start' line. Returns the matching line, and 'start'
+ * is updated with the next line number.
+ */
+static const char* get_sdp_iterate(int* start,
+			     struct sip_request *req, const char *name)
+{
 	int len = strlen(name);
-	char *r;
-
-	for (x = 0; x < req->lines; x++) {
-		r = get_sdp_by_line(req->line[x], name, len);
+
+	while (*start < req->lines) {
+		const char *r = get_sdp_by_line(req->line[(*start)++], name, len);
 		if (r[0] != '\0')
 			return r;
 	}
 	return "";
 }
 
-
-static void sdpLineNum_iterator_init(int* iterator) 
-{
-	*iterator = 0;
-}
-
-static char* get_sdp_iterate(int* iterator,
-			     struct sip_request *req, char *name)
-{
-	int len = strlen(name);
-	char *r;
-
-	while (*iterator < req->lines) {
-		r = get_sdp_by_line(req->line[(*iterator)++], name, len);
-		if (r[0] != '\0')
-			return r;
-	}
-	return "";
-}
-
-static char *find_alias(const char *name, char *_default)
-{
+/*! \brief  get_sdp: Gets all kind of SIP message bodies, including SDP,
+   but the name wrongly applies _only_ sdp */
+static const char *get_sdp(struct sip_request *req, const char *name) 
+{
+	int dummy = 0;
+	return get_sdp_iterate(&dummy, req, name);
+}
+
+static const char *find_alias(const char *name, const char *_default)
+{
+	/*! \brief Structure for conversion between compressed SIP and "normal" SIP */
+	static const struct cfalias {
+		char * const fullname;
+		char * const shortname;
+	} aliases[] = {
+		{ "Content-Type", "c" },
+		{ "Content-Encoding", "e" },
+		{ "From", "f" },
+		{ "Call-ID", "i" },
+		{ "Contact", "m" },
+		{ "Content-Length", "l" },
+		{ "Subject", "s" },
+		{ "To", "t" },
+		{ "Supported", "k" },
+		{ "Refer-To", "r" },
+		{ "Referred-By", "b" },
+		{ "Allow-Events", "u" },
+		{ "Event", "o" },
+		{ "Via", "v" },
+		{ "Accept-Contact",      "a" },
+		{ "Reject-Contact",      "j" },
+		{ "Request-Disposition", "d" },
+		{ "Session-Expires",     "x" },
+	};
 	int x;
 	for (x=0;x<sizeof(aliases) / sizeof(aliases[0]); x++) 
 		if (!strcasecmp(aliases[x].fullname, name))
@@ -2985,7 +2998,7 @@
 	return _default;
 }
 
-static char *__get_header(struct sip_request *req, const char *name, int *start)
+static const char *__get_header(const struct sip_request *req, const char *name, int *start)
 {
 	int pass;
 
@@ -3021,7 +3034,7 @@
 }
 
 /*! \brief Get header from SIP request */
-static char *get_header(struct sip_request *req, const char *name)
+static const char *get_header(const struct sip_request *req, const char *name)
 {
 	int start = 0;
 	return __get_header(req, name, &start);
@@ -3200,13 +3213,15 @@
 	}
 
 	if (useglobal_nat && sin) {
+		int natflags;
 		/* Setup NAT structure according to global settings if we have an address */
 		ast_copy_flags(&p->flags[0], &global_flags[0], SIP_NAT);
 		p->recv = *sin;
+		natflags = ast_test_flag(&p->flags[0], SIP_NAT) & SIP_NAT_ROUTE;
 		if (p->rtp)
-			ast_rtp_setnat(p->rtp, ast_test_flag(&p->flags[0], SIP_NAT_ROUTE));
+			ast_rtp_setnat(p->rtp, natflags);
 		if (p->vrtp)
-			ast_rtp_setnat(p->vrtp, ast_test_flag(&p->flags[0], SIP_NAT_ROUTE));
+			ast_rtp_setnat(p->vrtp, natflags);
 	}
 
 	if (p->method != SIP_REGISTER)
@@ -3239,12 +3254,10 @@
 static struct sip_pvt *find_call(struct sip_request *req, struct sockaddr_in *sin, const int intended_method)
 {
 	struct sip_pvt *p;
-	char *callid;
-	char *tag = "";
+	char *tag = "";	/* note, tag is never NULL */
 	char totag[128];
 	char fromtag[128];
-
-	callid = get_header(req, "Call-ID");
+	const char *callid = get_header(req, "Call-ID");
 
 	if (pedanticsipchecking) {
 		/* In principle Call-ID's uniquely identify a call, but with a forking SIP proxy
@@ -3257,11 +3270,7 @@
 			ast_set_flag(req, SIP_PKT_WITH_TOTAG);	/* Used in handle_request/response */
 		gettag(req, "From", fromtag, sizeof(fromtag));
 
-		if (req->method == SIP_RESPONSE)
-			tag = totag;
-		else
-			tag = fromtag;
-			
+		tag = (req->method == SIP_RESPONSE) ? totag : fromtag;
 
 		if (option_debug > 4 )
 			ast_log(LOG_DEBUG, "= Looking for  Call ID: %s (Checking %s) --From tag %s --To-tag %s  \n", callid, req->method==SIP_RESPONSE ? "To" : "From", fromtag, totag);
@@ -3325,10 +3334,8 @@
 	stringp=copy;
 	username = stringp;
 	hostname = strrchr(stringp, '@');
-	if (hostname) {
-		*hostname = '\0';
-		hostname++;
-	}
+	if (hostname)
+		*hostname++ = '\0';
 	if (ast_strlen_zero(username) || ast_strlen_zero(hostname)) {
 		ast_log(LOG_WARNING, "Format for registration is user[:secret[:authuser]]@host[:port][/contact] at line %d\n", lineno);
 		return -1;
@@ -3390,7 +3397,7 @@
 /*! \brief  Parse multiline SIP headers into one header
 	This is enabled if pedanticsipchecking is enabled */
 static int lws2sws(char *msgbuf, int len) 
-{ 
+{
 	int h = 0, t = 0; 
 	int lws = 0; 
 
@@ -3507,9 +3514,9 @@
 /*! \brief Process SIP SDP and activate RTP channels*/
 static int process_sdp(struct sip_pvt *p, struct sip_request *req)
 {
-	char *m;
-	char *c;
-	char *a;
+	const char *m;
+	const char *c;
+	const char *a;
 	char host[258];
 	char iabuf[INET_ADDRSTRLEN];
 	int len = -1;
@@ -3518,7 +3525,7 @@
 	int peercapability, peernoncodeccapability;
 	int vpeercapability=0, vpeernoncodeccapability=0;
 	struct sockaddr_in sin;
-	char *codecs;
+	const char *codecs;
 	struct hostent *hp;
 	struct ast_hostent ahp;
 	int codec;
@@ -3544,7 +3551,7 @@
 		return -1;
 	}
 	m = get_sdp(req, "m");
-	sdpLineNum_iterator_init(&destiterator);
+	destiterator = 0;
 	c = get_sdp_iterate(&destiterator, req, "c");
 	if (ast_strlen_zero(m) || ast_strlen_zero(c)) {
 		ast_log(LOG_WARNING, "Insufficient information for SDP (m = '%s', c = '%s')\n", m, c);
@@ -3560,7 +3567,7 @@
 		ast_log(LOG_WARNING, "Unable to lookup host in c= line, '%s'\n", c);
 		return -1;
 	}
-	sdpLineNum_iterator_init(&iterator);
+	iterator = 0;
 	ast_set_flag(&p->flags[0], SIP_NOVIDEO);	
 	while ((m = get_sdp_iterate(&iterator, req, "m"))[0] != '\0') {
 		int found = 0;
@@ -3659,7 +3666,7 @@
 	/* Next, scan through each "a=rtpmap:" line, noting each
 	 * specified RTP payload type (with corresponding MIME subtype):
 	 */
-	sdpLineNum_iterator_init(&iterator);
+	iterator = 0;
 	while ((a = get_sdp_iterate(&iterator, req, "a"))[0] != '\0') {
 		char* mimeSubtype = ast_strdupa(a); /* ensures we have enough space */
 		if (!strcasecmp(a, "sendonly")) {
@@ -3784,8 +3791,6 @@
 /*! \brief Add header to SIP message */
 static int add_header(struct sip_request *req, const char *var, const char *value)
 {
-	int x = 0;
-
 	if (req->headers == SIP_MAX_HEADERS) {
 		ast_log(LOG_WARNING, "Out of SIP header space\n");
 		return -1;
@@ -3803,11 +3808,8 @@
 
 	req->header[req->headers] = req->data + req->len;
 
-	if (compactheaders) {
-		for (x = 0; x < (sizeof(aliases) / sizeof(aliases[0])); x++)
-			if (!strcasecmp(aliases[x].fullname, var))
-				var = aliases[x].shortname;
-	}
+	if (compactheaders)
+		var = find_alias(var, var);
 
 	snprintf(req->header[req->headers], sizeof(req->data) - req->len - 4, "%s: %s\r\n", var, value);
 	req->len += strlen(req->header[req->headers]);
@@ -3874,6 +3876,7 @@
 static int copy_header(struct sip_request *req, struct sip_request *orig, char *field)
 {
 	const char *tmp = get_header(orig, field);
+
 	if (!ast_strlen_zero(tmp)) /* Add what we're responding to */
 		return add_header(req, field, tmp);
 	ast_log(LOG_NOTICE, "No field '%s' present to copy\n", field);
@@ -3887,6 +3890,7 @@
 	int copied = 0;
 	for (;;) {
 		const char *tmp = __get_header(orig, field, &start);
+
 		if (ast_strlen_zero(tmp))
 			break;
 		/* Add what we're responding to */
@@ -3911,6 +3915,7 @@
 
 	for (;;) {
 		const char *oh = __get_header(orig, field, &start);
+
 		if (ast_strlen_zero(oh))
 			break;
 
@@ -3973,14 +3978,15 @@
 	p = r;
 	for (;route ; route = route->next) {
 		n = strlen(route->hop);
-		if ( n + 3 > rem)
+		if (rem < n+3) /* we need room for ",<route>" */
 			break;
-		if (p != r) {
+		if (p != r) {	/* add a separator after fist route */
 			*p++ = ',';
 			--rem;
 		}
 		*p++ = '<';
-		ast_copy_string(p, route->hop, rem);  p += n;
+		ast_copy_string(p, route->hop, rem); /* cannot fail */
+		p += n;
 		*p++ = '>';
 		rem -= (n+2);
 	}
@@ -4019,6 +4025,7 @@
 	if (hn > sizeof(hostname)) 
 		hn = sizeof(hostname);
 	ast_copy_string(hostname, h, hn);
+	/* XXX bug here if string has been trimmed to sizeof(hostname) */
 	h += hn - 1;
 
 	/* Is "port" present? if not default to DEFAULT_SIP_PORT */
@@ -4088,7 +4095,8 @@
 /*! \brief Prepare SIP response packet */
 static int respprep(struct sip_request *resp, struct sip_pvt *p, const char *msg, struct sip_request *req)
 {
-	char newto[256], *ot;
+	char newto[256];
+	const char *ot;
 
 	memset(resp, 0, sizeof(*resp));
 	init_resp(resp, msg, req);
@@ -4104,10 +4112,8 @@
 			snprintf(newto, sizeof(newto), "%s;tag=%s", ot, p->theirtag);
 		else if (p->tag && !ast_test_flag(&p->flags[0], SIP_OUTGOING))
 			snprintf(newto, sizeof(newto), "%s;tag=%s", ot, p->tag);
-		else {
+		else
 			ast_copy_string(newto, ot, sizeof(newto));
-			newto[sizeof(newto) - 1] = '\0';
-		}
 		ot = newto;
 	}
 	add_header(resp, "To", ot);
@@ -4142,7 +4148,7 @@
 	char newto[256];
 	const char *c;
 	char *n;
-	char *ot, *of;
+	const char *ot, *of;
 	int is_strict = FALSE;		/*!< Strict routing flag */
 
 	memset(req, 0, sizeof(struct sip_request));
@@ -4236,7 +4242,7 @@
 }
 
 /*! \brief Base transmit response function */
-static int __transmit_response(struct sip_pvt *p, char *msg, struct sip_request *req, enum xmittype reliable)
+static int __transmit_response(struct sip_pvt *p, const char *msg, struct sip_request *req, enum xmittype reliable)
 {
 	struct sip_request resp;
 	int seqno = 0;
@@ -4263,7 +4269,7 @@
 }
 
 /*! \brief Transmit response, no retransmits */
-static int transmit_response_with_unsupported(struct sip_pvt *p, char *msg, struct sip_request *req, char *unsupported) 
+static int transmit_response_with_unsupported(struct sip_pvt *p, const char *msg, struct sip_request *req, const char *unsupported) 
 {
 	struct sip_request resp;
 	respprep(&resp, p, msg, req);
@@ -4275,7 +4281,7 @@
 /*! \brief Transmit response, Make sure you get an ACK
 	This is only used for responses to INVITEs, where we need to make sure we get an ACK
 */
-static int transmit_response_reliable(struct sip_pvt *p, char *msg, struct sip_request *req)
+static int transmit_response_reliable(struct sip_pvt *p, const char *msg, struct sip_request *req)
 {
 	return __transmit_response(p, msg, req, XMIT_CRITICAL);
 }
@@ -4726,7 +4732,7 @@
 	if (sipdebug)
 		add_header(&req, "X-asterisk-info", "SIP re-invite (RTP bridge)");
 	if (recordhistory)
-		append_history(p, "%s", "Re-invite sent");
+		append_history(p, "ReInv", "Re-invite sent");
 	add_sdp(&req, p);
 	/* Use this as the basis */
 	copy_request(&p->initreq, &req);
@@ -5096,10 +5102,7 @@
 
 	switch (state) {
 	case (AST_EXTENSION_RINGING | AST_EXTENSION_INUSE):
-		if (global_notifyringing)
-			statestring = "early";
-		else
-			statestring = "confirmed";
+		statestring = (global_notifyringing) ? "early" : "confirmed";
 		local_state = NOTIFY_INUSE;
 		pidfstate = "busy";
 		pidfnote = "Ringing";
@@ -5261,7 +5264,8 @@
 	add_header(&req, "Content-Type", default_notifymime);
 
 	ast_build_string(&t, &maxbytes, "Messages-Waiting: %s\r\n", newmsgs ? "yes" : "no");
-	ast_build_string(&t, &maxbytes, "Message-Account: sip:%s@%s\r\n", !ast_strlen_zero(vmexten) ? vmexten : default_vmexten, S_OR(p->fromdomain, ast_inet_ntoa(iabuf, sizeof(iabuf), p->ourip)));
+	ast_build_string(&t, &maxbytes, "Message-Account: sip:%s@%s\r\n",
+		S_OR(vmexten, default_vmexten), S_OR(p->fromdomain, ast_inet_ntoa(iabuf, sizeof(iabuf), p->ourip)));
 	ast_build_string(&t, &maxbytes, "Voice-Message: %d/%d (0/0)\r\n", newmsgs, oldmsgs);
 	if (p->subscribed) {
 		if (p->expiry)
@@ -5412,7 +5416,7 @@
 		r->call = NULL;
 		ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);	
 		/* Pretend to ACK anything just in case */
-		__sip_pretend_ack(p);
+		__sip_pretend_ack(p); /* XXX we need p locked, not sure we have */
 	}
 	/* If we have a limit, stop registration and give up */
 	if (global_regattempts_max && (r->regattempts > global_regattempts_max)) {
@@ -5631,7 +5635,8 @@
 {
 	struct sip_request req;
 	char from[256];
-	char *of, *c;
+	const char *of;
+	char *c;
 	char referto[256];
 
 	/* Are we transfering an inbound or outbound call? */
@@ -5642,9 +5647,9 @@
 	ast_copy_string(from, of, sizeof(from));
 	of = get_in_brackets(from);
 	ast_string_field_set(p, from, of);
-	if (strncmp(of, "sip:", 4)) {
+	if (strncmp(of, "sip:", 4))
 		ast_log(LOG_NOTICE, "From address missing 'sip:', using it anyway\n");
-	} else
+	else
 		of += 4;
 	/* Get just the username part */
 	if ((c = strchr(dest, '@')))
@@ -5887,6 +5892,7 @@
 	/* Work on a copy */
 	contact = ast_strdupa(pvt->fullcontact);
 
+	/* XXX this code is repeated all over */
 	/* Make sure it's a SIP URL */
 	if (strncasecmp(contact, "sip:", 4)) {
 		ast_log(LOG_NOTICE, "'%s' is not a valid SIP contact (missing sip:) trying to use anyway\n", contact);
@@ -5941,11 +5947,11 @@
 	char contact[80]; 
 	char data[256];
 	char iabuf[INET_ADDRSTRLEN];
-	char *expires = get_header(req, "Expires");
+	const char *expires = get_header(req, "Expires");
 	int expiry = atoi(expires);
 	char *c, *n, *pt;
 	int port;
-	char *useragent;
+	const char *useragent;
 	struct hostent *hp;
 	struct ast_hostent ahp;
 	struct sockaddr_in oldsin;
@@ -6076,9 +6082,8 @@
 	useragent = get_header(req, "User-Agent");
 	if (useragent && strcasecmp(useragent, p->useragent)) {
 		ast_copy_string(p->useragent, useragent, sizeof(p->useragent));
-		if (option_verbose > 3) {
+		if (option_verbose > 3)
 			ast_verbose(VERBOSE_PREFIX_3 "Saved useragent \"%s\" for peer %s\n",p->useragent,p->name);  
-		}
 	}
 	return PARSE_REGISTER_UPDATE;
 }
@@ -6098,12 +6103,12 @@
 /*! \brief List all routes - mostly for debugging */
 static void list_route(struct sip_route *route)
 {
-	if (!route) {
+	if (!route)
 		ast_verbose("list_route: no route\n");
-		return;
-	}
-	for (;route; route = route->next)
-		ast_verbose("list_route: hop: <%s>\n", route->hop);
+	else {
+		for (;route; route = route->next)
+			ast_verbose("list_route: hop: <%s>\n", route->hop);
+	}
 }
 
 /*! \brief Build route list from Record-Route header */
@@ -6112,7 +6117,7 @@
 	struct sip_route *thishop, *head, *tail;
 	int start = 0;
 	int len;
-	char *rr, *contact, *c;
+	const char *rr, *contact, *c;
 
 	/* Once a persistant route is set, don't fool with it */
 	if (p->route && p->route_persistant) {
@@ -6134,7 +6139,8 @@
 	for (;;) {
 		/* Each Record-Route header */
 		rr = __get_header(req, "Record-Route", &start);
-		if (*rr == '\0') break;
+		if (*rr == '\0')
+			break;
 		for (;;) {
 			/* Each route entry */
 			/* Find < */
@@ -6354,11 +6360,12 @@
 
 			/* Schedule auto destroy in 32 seconds */
 			sip_scheddestroy(p, 32000);
-			return 1;	/* Challenge sent */
+			return 1;	/* XXX should it be -1 ? */
 		} 
 		if (good_response) /* Auth is OK */
 			return 0;
 
+		/* XXX is this needed ? */
 		/* Ok, we have a bad username/secret pair */
 		/* Challenge again, and again, and again */
 		transmit_response_with_auth(p, response, req, p->randdata, reliable, respheader, 0);
@@ -6632,8 +6639,8 @@
 		}
 	}
 
-	if ((a = strchr(c, '@')) || (a = strchr(c, ';')))
-		*a = '\0';
+	a = c;
+	strsep(&a, "@;");	/* trim anything after @ or ; */
 
 	if (sip_debug_test_pvt(p))
 		ast_verbose(" -- RDNIS for tis call is is %s (reason %s)\n", c, reason ? reason : "");
@@ -6776,7 +6783,10 @@
 static int get_refer_info(struct sip_pvt *sip_pvt, struct sip_request *outgoing_req)
 {
 
-	char *p_refer_to = NULL, *p_referred_by = NULL, *h_refer_to = NULL, *h_referred_by = NULL, *h_contact = NULL;
+	const char *p_refer_to = NULL;
+	const char *h_contact = NULL;
+	const char *p_referred_by = NULL;
+	char *h_refer_to = NULL, *h_referred_by = NULL;
 	char *replace_callid = "", *refer_to = NULL, *referred_by = NULL, *ptr = NULL;
 	struct sip_request *req = NULL;
 	struct sip_pvt *sip_pvt_ptr = NULL;
@@ -6798,9 +6808,8 @@
 		ast_log(LOG_WARNING, "No Referrred-By Header That's not illegal\n");
 		return -1;
 	} else {
-		if (pedanticsipchecking) {
+		if (pedanticsipchecking)
 			ast_uri_decode(h_referred_by);
-		}
 		referred_by = get_in_brackets(h_referred_by);
 	}
 	h_contact = get_header(req, "Contact");
@@ -6844,18 +6853,12 @@
 		}
 	}
 	
-	if ((ptr = strchr(refer_to, '@')))	/* Skip domain (should be saved in SIPDOMAIN) */
-		*ptr = '\0';
-	if ((ptr = strchr(refer_to, ';'))) 
-		*ptr = '\0';
-	
-	if (referred_by) {
-		if ((ptr = strchr(referred_by, '@')))
-			*ptr = '\0';
-		if ((ptr = strchr(referred_by, ';'))) 
-			*ptr = '\0';
-	}
-	
+	/* strip domain and everything after ';' (domain should be saved in SIPDOMAIN) */
+	ptr = refer_to;
+	strsep(&ptr, "@;");	/* trim anything after @ or ; */
+	ptr = referred_by;
+	strsep(&ptr, "@;");	/* trim anything after @ or ;, NULL is ok */
+
 	if (sip_debug_test_pvt(sip_pvt)) {
 		ast_verbose("Transfer to %s in %s\n", refer_to, sip_pvt->context);
 		if (referred_by)
@@ -6917,17 +6920,14 @@
 	if (!req)
 		req = &p->initreq;
 	ast_copy_string(tmp, get_header(req, "Also"), sizeof(tmp));
-	
 	c = get_in_brackets(tmp);
 	if (strncmp(c, "sip:", 4)) {
 		ast_log(LOG_WARNING, "Huh?  Not a SIP header (%s)?\n", c);
 		return -1;
 	}
 	c += 4;
-	if ((a = strchr(c, '@')))
-		*a = '\0';
-	if ((a = strchr(c, ';'))) 
-		*a = '\0';
+	a = c;
+	strsep(&a, "@;");	/* trim anything after @ or ; */
 	
 	if (sip_debug_test_pvt(p))
 		ast_verbose("Looking for %s in %s\n", c, p->context);
@@ -6990,22 +6990,22 @@
 		p->sa.sin_port = htons(pt ? atoi(pt) : DEFAULT_SIP_PORT);
 
 		if (sip_debug_test_pvt(p)) {
-			c = (ast_test_flag(&p->flags[0], SIP_NAT) & SIP_NAT_ROUTE) ? "NAT" : "non-NAT";
-			ast_verbose("Sending to %s : %d (%s)\n", ast_inet_ntoa(iabuf, sizeof(iabuf), p->sa.sin_addr), ntohs(p->sa.sin_port), c);
+			const struct sockaddr_in *dst = sip_real_dst(p);
+			ast_verbose("Sending to %s : %d (%s)\n", ast_inet_ntoa(iabuf, sizeof(iabuf), dst->sin_addr), ntohs(dst->sin_port), sip_nat_mode(p));
 		}
 	}
 	return 0;
 }
 
 /*! \brief  Get caller id name from SIP headers */
-static char *get_calleridname(char *input, char *output, size_t outputsize)
-{
-	char *end = strchr(input,'<');
-	char *tmp = strchr(input,'\"');
+static char *get_calleridname(const char *input, char *output, size_t outputsize)
+{
+	const char *end = strchr(input,'<');	/* first_bracket */
+	const char *tmp = strchr(input,'\"');	/* first quote */
 	int bytes = 0;
 	int maxbytes = outputsize - 1;
 
-	if (!end || end == input)
+	if (!end || end == input)	/* we require a part in brackets */
 		return NULL;
 
 	/* move away from "<" */
@@ -7044,7 +7044,7 @@
  *	Returns true if number should be restricted (privacy setting found)
  *	output is set to NULL if no number found
  */
-static int get_rpid_num(char *input,char *output, int maxlen)
+static int get_rpid_num(const char *input, char *output, int maxlen)
 {
 	char *start;
 	char *end;
@@ -7082,8 +7082,10 @@
 {
 	struct sip_user *user = NULL;
 	struct sip_peer *peer;
-	char *of, from[256], *c;
-	char *rpid, rpid_num[50];
+	char from[256], *c;
+	char *of;
+	char rpid_num[50];
+	const char *rpid;
 	char iabuf[INET_ADDRSTRLEN];
 	int res = 0;
 	char *t;
@@ -7097,12 +7099,10 @@
 	while (*t && *t > 32 && *t != ';')
 		t++;
 	*t = '\0';
-	of = get_header(req, "From");
+	ast_copy_string(from, get_header(req, "From"), sizeof(from));	/* XXX bug in original code, overwrote string */
 	if (pedanticsipchecking)
-		ast_uri_decode(of);
-
-	ast_copy_string(from, of, sizeof(from));
-	
+		ast_uri_decode(from);
+	/* XXX here tries to map the username for invite things */
 	memset(calleridname, 0, sizeof(calleridname));
 	get_calleridname(from, calleridname, sizeof(calleridname));
 	if (calleridname[0])
@@ -7111,7 +7111,7 @@
 	rpid = get_header(req, "Remote-Party-ID");
 	memset(rpid_num, 0, sizeof(rpid_num));
 	if (!ast_strlen_zero(rpid)) 
-		p->callingpres = get_rpid_num(rpid,rpid_num, sizeof(rpid_num));
+		p->callingpres = get_rpid_num(rpid, rpid_num, sizeof(rpid_num));
 
 	of = get_in_brackets(from);
 	if (ast_strlen_zero(p->exten)) {
@@ -7334,6 +7334,7 @@
 				if (!ast_strlen_zero(peer->username)) {

[... 309 lines stripped ...]


More information about the asterisk-commits mailing list