[Asterisk-Dev] PATCH: Ability to override "From" for calls to a peer, also fix inaddrcmp sense

Stephen Davies steve at daviesfam.org
Wed Apr 9 08:59:01 MST 2003


Hi Mark,

With this patch I'm able to make PSTN calls via Packet8.  The patch adds a
"from=" for SIP peers, allowing you to specifically set the From header to
be used on calls out to that peer.

I use it like this:

[packet8.net]
type=peer
host=packet8.net
from="Steve Davies - +1 847-897-4611" <sip:1234567890 at packet8.net>

When calls go out to Packet8 this is the From header that will be
used.  Including the right magic number in the SIP URI seems to be enough
authentication for them. (so I'll keep my magic number to myself...)

I think this will be useful with other providers too.  I'll test
iConnectHere this evening.

By the way - Packet8 advertises that it is G723.1 only - but in fact for
outgoing (to PSTN) calls it will do G711 too.  For inbound (from
PSTN) calls their gateway wants only G723.1 from us.

This patch also fixes the sense of the inaddrcmp test, and adds what I
think are useful and important debug messages showing where users and
peers are matched for calls.

Regards,
Steve
-------------- next part --------------
Index: channels/chan_sip.c
===================================================================
RCS file: /usr/cvsroot/asterisk/channels/chan_sip.c,v
retrieving revision 1.61
diff -u -r1.61 chan_sip.c
--- channels/chan_sip.c	9 Apr 2003 05:38:39 -0000	1.61
+++ channels/chan_sip.c	9 Apr 2003 15:36:14 -0000
@@ -185,6 +185,7 @@
 	char remote_party_id[256];
 	char context[AST_MAX_EXTENSION];
 	char fromdomain[AST_MAX_EXTENSION];	/* Domain to show in the from field */
+	char from[AST_MAX_EXTENSION];		/* Explicit overriding From URI for this call */
 	char language[MAX_LANGUAGE];
 	char theirtag[256];				/* Their tag */
 	char username[81];
@@ -257,6 +258,7 @@
 	char methods[80];
 	char username[80];
 	char fromdomain[80];
+	char from[80];
 	char mailbox[AST_MAX_EXTENSION];
 	int lastmsgssent;
 	time_t	lastmsgcheck;
@@ -575,6 +577,8 @@
 	p = peerl.peers;
 	while(p) {
 		if (!strcasecmp(p->name, peer)) {
+			if (sipdebug)
+				ast_verbose("Found peer for \"%s\"\n", peer);
 			found++;
 			r->capability = p->capability;
 			r->nat = p->nat;
@@ -585,6 +589,7 @@
 			strncpy(r->peername, p->username, sizeof(r->peername)-1);
 			strncpy(r->peersecret, p->secret, sizeof(r->peersecret)-1);
 			strncpy(r->username, p->username, sizeof(r->username)-1);
+			strncpy(r->from, p->from, sizeof(r->from)-1);
 			if (strlen(p->fromdomain))
 				strncpy(r->fromdomain, p->fromdomain, sizeof(r->fromdomain)-1);
 			r->insecure = p->insecure;
@@ -617,6 +622,8 @@
 	}
 	ast_pthread_mutex_unlock(&peerl.lock);
 	if (!p && !found) {
+		if (sipdebug)
+			ast_verbose("No peer for \"%s\", looking up in DNS\n", peer);
 		hp = gethostbyname(peer);
 		if (hp) {
 			memcpy(&r->sa.sin_addr, hp->h_addr, sizeof(r->sa.sin_addr));
@@ -2260,6 +2267,9 @@
 		snprintf(from, sizeof(from), "\"%s\" <sip:%s@%s:%d>;tag=as%08x", n, l, strlen(p->fromdomain) ? p->fromdomain : inet_ntoa(p->ourip), ourport, p->tag);
 	else
 		snprintf(from, sizeof(from), "\"%s\" <sip:%s@%s>;tag=as%08x", n, l, strlen(p->fromdomain) ? p->fromdomain : inet_ntoa(p->ourip), p->tag);
+	/* Do we have an overriding "From"?  if so copy.  Later perhaps attempt to "merge" with the one calculated above */
+	if (strlen(p->from))
+		snprintf(from, sizeof(from), "%s;tag=as%08x", p->from, p->tag);
 
 	if (strlen(p->username)) {
 		if (ntohs(p->sa.sin_port) != DEFAULT_SIP_PORT) {
@@ -3184,10 +3194,14 @@
 	strncpy(p->callerid, of, sizeof(p->callerid) - 1);
 	if (!strlen(of))
 			return 0;
+	if (sipdebug)
+		ast_verbose("Looking for known user \"%s\"\n", of);
 	ast_pthread_mutex_lock(&userl.lock);
 	user = userl.users;
 	while(user) {
 		if (!strcasecmp(user->name, of)) {
+			if (sipdebug)
+				ast_verbose("Found matching user\n");
 			p->nat = user->nat;
 			if (p->rtp) {
 				ast_log(LOG_DEBUG, "Setting NAT on RTP to %d\n", p->nat);
@@ -3220,10 +3234,15 @@
 	ast_pthread_mutex_unlock(&userl.lock);
 	if (!user) {
 	/* If we didn't find a user match, check for peers */
+		if (sipdebug)
+			ast_verbose("Couldn't find known user - instead looking for peer at %s:%d\n", inet_ntoa(p->recv.sin_addr), ntohs(p->recv.sin_port));
 		ast_pthread_mutex_lock(&peerl.lock);
 		peer = peerl.peers;
 		while(peer) {
-			if (!inaddrcmp(&peer->addr, &p->recv)) {
+			/*if (sipdebug) ast_verbose("...comparing against %s at %s:%d\n", peer->name, inet_ntoa(peer->addr.sin_addr), ntohs(peer->addr.sin_port));*/
+			if (inaddrcmp(&peer->addr, &p->recv)) {
+				if (sipdebug)
+					ast_verbose("Found peer \"%s\"\n", peer->name);
 				/* Take the peer */
 				p->nat = peer->nat;
 				if (p->rtp) {
@@ -4840,6 +4859,9 @@
 				strncpy(peer->context, v->value, sizeof(peer->context)-1);
 			else if (!strcasecmp(v->name, "fromdomain"))
 				strncpy(peer->fromdomain, v->value, sizeof(peer->fromdomain)-1);
+			else if (!strcasecmp(v->name, "from"))
+				/* An overriding "From" URI for this peer */
+				strncpy(peer->from, v->value, sizeof(peer->from)-1);
             else if (!strcasecmp(v->name, "dtmfmode")) {
 				if (!strcasecmp(v->value, "inband"))
 					peer->dtmfmode=SIP_DTMF_INBAND;


More information about the asterisk-dev mailing list