Tue Jun 15 15:54:26 CEST 2004
--- asterisk-cvs-040609-nogren-1/asterisk/include/asterisk/vmodem.h	2004-06-14 10:46:32.000000000 +0200
+++ asterisk-cvs-040609-nogren/asterisk/include/asterisk/vmodem.h	2004-06-14 10:01:15.000000000 +0200
@@ -109,6 +109,8 @@ struct ast_modem_pvt {
 	char msn[AST_MAX_EXTENSION];	
 	/*! Multiple Subscriber Number we listen to (; seperated list) */
 	char incomingmsn[AST_MAX_EXTENSION];	
+	/*! Multiple Subscriber Number we accept for outgoing calls (; seperated list) */
+	char outgoingmsn[AST_MAX_EXTENSION];	
 	/*! Group(s) we belong to if available */
 	unsigned int group;
 	/*! Caller ID if available */
--- asterisk-cvs-040609-nogren-1/asterisk/channels/chan_modem.c	2004-06-14 10:46:32.000000000 +0200
+++ asterisk-cvs-040609-nogren/asterisk/channels/chan_modem.c	2004-06-14 10:00:24.000000000 +0200
@@ -63,6 +63,9 @@ static char msn[AST_MAX_EXTENSION]="";
 /* Default Listen */
 static char incomingmsn[AST_MAX_EXTENSION]="";
 
+/* Default valid outgoing MSN */
+static char outgoingmsn[AST_MAX_EXTENSION]="";
+
 /* Default skip dtmf */
 static int allowoutbanddtmf = 1;
 
@@ -721,6 +724,7 @@ static struct ast_modem_pvt *mkif(char *
 		strncpy(tmp->language, language, sizeof(tmp->language)-1);
 		strncpy(tmp->msn, msn, sizeof(tmp->msn)-1);
 		strncpy(tmp->incomingmsn, incomingmsn, sizeof(tmp->incomingmsn)-1);
+		strncpy(tmp->outgoingmsn, outgoingmsn, sizeof(tmp->outgoingmsn)-1);
 		tmp->allowoutbanddtmf = allowoutbanddtmf;
 		tmp->generateinbanddtmf = generateinbanddtmf;
 		tmp->detectinband = detectinband;
@@ -985,6 +989,8 @@ int load_module()
 			strncpy(msn, v->value, sizeof(msn)-1);
 		} else if (!strcasecmp(v->name, "incomingmsn")) {
 			strncpy(incomingmsn, v->value, sizeof(incomingmsn)-1);
+		} else if (!strcasecmp(v->name, "outgoingmsn")) {
+			strncpy(outgoingmsn, v->value, sizeof(outgoingmsn)-1);
 		} else if (!strcasecmp(v->name, "allowoutbanddtmf")) {
                         allowoutbanddtmf = atoi(v->value);
 		} else if (!strcasecmp(v->name, "generateinbanddtmf")) {
--- asterisk-cvs-040609-nogren-1/asterisk/channels/chan_modem_i4l.c	2004-06-14 10:46:32.000000000 +0200
+++ asterisk-cvs-040609-nogren/asterisk/channels/chan_modem_i4l.c	2004-06-15 15:23:44.000000000 +0200
@@ -25,6 +25,7 @@
 #include <asterisk/logger.h>
 #include <asterisk/options.h>
 #include <asterisk/dsp.h>
+#include <asterisk/callerid.h>
 #include "alaw.h"
 
 #define STATE_COMMAND 	0
@@ -583,6 +584,31 @@ static int i4l_dialdigit(struct ast_mode
 static int i4l_dial(struct ast_modem_pvt *p, char *stuff)
 {
 	char cmd[80];
+	char tmp[255];
+	char *name, *num;
+	struct ast_channel *c = p->owner;
+
+	// Find callerid number first, to set the correct A number
+	if (c && c->callerid && ! c->restrictcid) {
+	  ast_log(LOG_DEBUG, "Finding callerid from %s...\n",c->callerid);
+	  strncpy(tmp, c->callerid, sizeof(tmp) - 1);
+	  ast_callerid_parse(tmp, &name, &num);
+	  if (num) {
+	    ast_shrink_phone_number(num);
+
+	    if(strlen(p->outgoingmsn) && strstr(p->outgoingmsn,num) != NULL) {
+	      // Tell ISDN4Linux to use this as A number
+	      snprintf(cmd, sizeof(cmd), "AT&E%s\n", num);
+	      if (ast_modem_send(p, cmd, strlen(cmd))) {
+		ast_log(LOG_WARNING, "Unable to set A number to %s\n",num);
+	      }
+
+	    } else {
+	      ast_log(LOG_WARNING, "Outgoing MSN %s not allowed (see outgoingmsn=%s in modem.conf)\n",num,p->outgoingmsn);
+	    }
+	  }
+	}
+
 	snprintf(cmd, sizeof(cmd), "ATD%c %s\n", p->dialtype,stuff);
 	if (ast_modem_send(p, cmd, strlen(cmd))) {
 		ast_log(LOG_WARNING, "Unable to dial\n");
--- asterisk-cvs-040609-nogren-1/asterisk/configs/modem.conf.sample	2004-06-14 10:59:14.000000000 +0200
+++ asterisk-cvs-040609-nogren/asterisk/configs/modem.conf.sample	2004-06-15 15:51:32.000000000 +0200
@@ -91,6 +91,11 @@ group=1		; group=1,2,3,9-12
 ; Relax the DTMF detection made with detectinband
 ;relaxdtmf=0
 
+; If set, only these numbers are allowed to be set as A number
+; when making an outbound call. callerid is used to set A
+; number.
+outgoingmsn=50780023,50780024
+;
 ;device => /dev/ttyI3
 ;device => /dev/ttyI4
 
--- asterisk-cvs-040609-nogren-1/asterisk/channels/chan_sip.c	2004-06-09 03:45:08.000000000 +0200
+++ asterisk-cvs-040609-nogren/asterisk/channels/chan_sip.c	2004-06-15 15:53:37.000000000 +0200
@@ -154,6 +154,8 @@ static int globalrtptimeout = 0;
 
 static int globalrtpholdtimeout = 0;
 
+static int globaltrustrpid = 0;
+
 static int usecnt =0;
 AST_MUTEX_DEFINE_STATIC(usecnt_lock);
 
@@ -326,6 +328,8 @@ static struct sip_pvt {
     	int stateid;
 	int dialogver;
 	
+        int trustrpid;
+	
         int dtmfmode;
         struct ast_dsp *vad;
 	
@@ -379,6 +383,7 @@ struct sip_user {
 	int outUse;
 	int outgoinglimit;
 	int restrictcid;
+        int trustrpid;
 	struct ast_ha *ha;
 	struct sip_user *next;
 };
@@ -411,6 +416,7 @@ struct sip_peer {
 	unsigned int callgroup;
 	unsigned int pickupgroup;
         int dtmfmode;
+        int trustrpid;
 	struct sockaddr_in addr;
 	struct in_addr mask;
 
@@ -2071,6 +2077,7 @@ static struct sip_pvt *sip_alloc(char *c
 	/* Assign default music on hold class */
         strncpy(p->musicclass, globalmusicclass, sizeof(p->musicclass));
 	p->dtmfmode = globaldtmfmode;
+	p->trustrpid = globaltrustrpid;
 	p->rtptimeout = globalrtptimeout;
 	p->rtpholdtimeout = globalrtpholdtimeout;
 	p->capability = capability;
@@ -4726,12 +4733,45 @@ static char *get_calleridname(char *inpu
 	return output;
 }
 
+/*--- get_rpid_num: Get caller id number from Remote-Party-ID header field 
+ *                  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)
+{
+  char *start;
+  char *end;
+
+  if(!input) {
+    output = NULL;
+    return 0;
+  }
+
+  start = strchr(input,':');
+  end = strchr(input,'@');
+  start++;
+  end--;
+  if (!start || !end || (end <= input)) {
+    output = NULL;
+    return 0;
+  }
+  /* we found "number" */
+  strncpy(output,start,(int)(end-start+1));
+
+  if(strstr(input,"privacy=full") || strstr(input,"privacy=uri"))
+    return 1;
+
+  return 0;
+}
+
+
 /*--- check_user: Check if matching user or peer is defined ---*/
 static int check_user(struct sip_pvt *p, struct sip_request *req, char *cmd, char *uri, int reliable, struct sockaddr_in *sin, int ignore)
 {
 	struct sip_user *user;
 	struct sip_peer *peer;
 	char *of, from[256] = "", *c;
+	char *rpid,rpid_num[50];
 	int res = 0;
 	char *t;
 	char calleridname[50];
@@ -4744,6 +4784,14 @@ static int check_user(struct sip_pvt *p,
 	strncpy(from, of, sizeof(from) - 1);
 	memset(calleridname,0,sizeof(calleridname));
 	get_calleridname(from,calleridname);
+
+	rpid = get_header(req, "Remote-Party-ID");
+	if(!ast_strlen_zero(rpid))
+	  strncpy(p->remote_party_id,rpid,sizeof(p->remote_party_id)-1);
+	memset(rpid_num,0,sizeof(rpid_num));
+	if(!ast_strlen_zero(p->remote_party_id)) 
+	  p->restrictcid = get_rpid_num(p->remote_party_id,rpid_num);
+
 	of = ditch_braces(from);
 	if (ast_strlen_zero(p->exten)) {
 		t = uri;
@@ -4795,6 +4843,7 @@ static int check_user(struct sip_pvt *p,
 			strncpy(p->accountcode, user->accountcode, sizeof(p->accountcode)  -1);
 			strncpy(p->language, user->language, sizeof(p->language)  -1);
 			strncpy(p->musicclass, user->musicclass, sizeof(p->musicclass)  -1);
+			p->trustrpid = user->trustrpid;
 			p->canreinvite = user->canreinvite;
 			p->amaflags = user->amaflags;
 			p->callgroup = user->callgroup;
@@ -4869,6 +4920,8 @@ static int check_user(struct sip_pvt *p,
 					else
 						p->noncodeccapability &= ~AST_RTP_DTMF;
 				}
+				p->trustrpid = peer->trustrpid;
+
 			}
 			if (peer->temponly) {
 				if (peer->ha) {
@@ -4882,6 +4935,15 @@ static int check_user(struct sip_pvt *p,
 		ast_mutex_unlock(&peerl.lock);
 
 	}
+
+	/* replace callerid if rpid found, and not restricted */
+	if(!ast_strlen_zero(rpid_num) && p->trustrpid) {
+	  if (*calleridname)
+	    sprintf(p->callerid,"\"%s\" <%s>",calleridname,rpid_num);
+	  else
+	    strncpy(p->callerid, rpid_num, sizeof(p->callerid) - 1);
+	}
+
 	return res;
 }
 
@@ -7209,6 +7271,7 @@ static struct sip_user *build_user(char 
 		user->capability = capability;
 
 		user->canreinvite = globalcanreinvite;
+		user->trustrpid = globaltrustrpid;
 		/* set default context */
 		strncpy(user->context, context, sizeof(user->context)-1);
 		strncpy(user->language, language, sizeof(user->language)-1);
@@ -7285,6 +7348,8 @@ static struct sip_user *build_user(char 
 				user->insecure = ast_true(v->value);
 			} else if (!strcasecmp(v->name, "restrictcid")) {
 				user->restrictcid = ast_true(v->value);
+			} else if (!strcasecmp(v->name, "trustrpid")) {
+                                user->trustrpid = ast_true(v->value);
 			}
 			/*else if (strcasecmp(v->name,"type"))
 			 *	ast_log(LOG_WARNING, "Ignoring %s\n", v->name);
@@ -7321,6 +7386,7 @@ static struct sip_peer *temp_peer(char *
 	peer->rtpholdtimeout = globalrtpholdtimeout;
 	peer->selfdestruct = 1;
 	peer->dynamic = 1;
+	peer->trustrpid = globaltrustrpid;
 	reg_source_db(peer);
 	return peer;
 }
@@ -7380,6 +7446,7 @@ static struct sip_peer *build_peer(char 
 		peer->rtptimeout = globalrtptimeout;
 		peer->rtpholdtimeout = globalrtpholdtimeout;
 		peer->dtmfmode = 0;
+		peer->trustrpid = globaltrustrpid;
 		while(v) {
 			if (!strcasecmp(v->name, "secret")) 
 				strncpy(peer->secret, v->value, sizeof(peer->secret)-1);
@@ -7503,6 +7570,8 @@ static struct sip_peer *build_peer(char 
 					ast_log(LOG_WARNING, "Qualification of peer '%s' should be 'yes', 'no', or a number of milliseconds at line %d of sip.conf\n", peer->name, v->lineno);
 					peer->maxms = 0;
 				}
+			} else if (!strcasecmp(v->name, "trustrpid")) {
+                                peer->trustrpid = ast_true(v->value);
 			}
 			/* else if (strcasecmp(v->name,"type"))
 			 *	ast_log(LOG_WARNING, "Ignoring %s\n", v->name);
@@ -7616,6 +7685,8 @@ static int reload_config(void)
 			autocreatepeer = ast_true(v->value);
 		} else if (!strcasecmp(v->name, "srvlookup")) {
 			srvlookup = ast_true(v->value);
+		} else if (!strcasecmp(v->name, "trustrpid")) {
+		        globaltrustrpid = ast_true(v->value);
 		} else if (!strcasecmp(v->name, "pedantic")) {
 			pedanticsipchecking = ast_true(v->value);
 		} else if (!strcasecmp(v->name, "canreinvite")) {
--- asterisk-cvs-040609-nogren-1/asterisk/configs/sip.conf.sample	2004-05-28 00:12:55.000000000 +0200
+++ asterisk-cvs-040609-nogren/asterisk/configs/sip.conf.sample	2004-06-14 17:10:02.000000000 +0200
@@ -52,6 +52,7 @@ bindaddr=0.0.0.0		; IP address to bind t
 				; when we're not on hold
 ;rtpholdtimeout=300		; Terminate call if 300 seconds of no RTP activity
 				; when we're on hold (must be > rtptimeout)
+;trustrpid = no			; If Remote-Party-ID should be trusted
 
 ; Asterisk can register as a SIP user agent to a SIP proxy (provider)
 ; Format for the register statement is:
@@ -115,6 +116,7 @@ bindaddr=0.0.0.0		; IP address to bind t
 ; allow                       allow
 ; disallow                    disallow
 ; insecure                    insecure
+; trustrpid                   trustrpid
 ; callerid
 ; accountcode
 ; amaflags
