[Asterisk-cvs] asterisk/channels chan_sip.c,1.203,1.204

martinp at lists.digium.com martinp at lists.digium.com
Fri Oct 31 09:47:40 CST 2003


Update of /usr/cvsroot/asterisk/channels
In directory mongoose.digium.com:/tmp/cvs-serv16450/channels

Modified Files:
	chan_sip.c 
Log Message:
Add externip keyword so that it's possible to use asterisk behind a NAT through port forwarding


Index: chan_sip.c
===================================================================
RCS file: /usr/cvsroot/asterisk/channels/chan_sip.c,v
retrieving revision 1.203
retrieving revision 1.204
diff -u -d -r1.203 -r1.204
--- chan_sip.c	25 Oct 2003 17:41:02 -0000	1.203
+++ chan_sip.c	31 Oct 2003 16:13:49 -0000	1.204
@@ -387,7 +387,7 @@
 static int sipsock  = -1;
 static int globalnat = 0;
 static int globalcanreinvite = REINVITE_INVITE;
-
+static int use_external_ip = 0;
 
 static struct sockaddr_in bindaddr;
 
@@ -425,10 +425,14 @@
 
 static int ast_sip_ouraddrfor(struct in_addr *them, struct in_addr *us)
 {
-	if (bindaddr.sin_addr.s_addr)
-		memcpy(us, &bindaddr.sin_addr, sizeof(struct in_addr));
-	else
-		return ast_ouraddrfor(them, us);
+	if (use_external_ip) {
+		return -1;
+	} else {
+		if (bindaddr.sin_addr.s_addr)
+			memcpy(us, &bindaddr.sin_addr, sizeof(struct in_addr));
+		else
+			return ast_ouraddrfor(them, us);
+		}
 	return 0;
 }
 
@@ -2985,7 +2989,7 @@
 		strncpy(p->username, r->username, sizeof(p->username)-1);
 		strncpy(p->exten, r->contact, sizeof(p->exten) - 1);
 		/* Always bind to our IP if specified */
-		if (bindaddr.sin_addr.s_addr)
+		if (!use_external_ip && bindaddr.sin_addr.s_addr)
 			memcpy(&p->ourip, &bindaddr.sin_addr, sizeof(p->ourip));
 		build_contact(p);
 	}
@@ -5979,6 +5983,13 @@
 			} else {
 				memcpy(&bindaddr.sin_addr, hp->h_addr, sizeof(bindaddr.sin_addr));
 			}
+		} else if (!strcasecmp(v->name, "externip")) {
+			if (!(hp = gethostbyname(v->value))) {
+				ast_log(LOG_WARNING, "Invalid address for externip keyword: %s\n", v->value);
+			} else {
+				memcpy(&__ourip, hp->h_addr, sizeof(__ourip));
+				use_external_ip = 1;
+			}
 		} else if (!strcasecmp(v->name, "allow")) {
 			format = ast_getformatbyname(v->value);
 			if (format < 1) 
@@ -6053,16 +6064,17 @@
 		}
 		cat = ast_category_browse(cfg, cat);
 	}
-	
-	if (ntohl(bindaddr.sin_addr.s_addr)) {
-		memcpy(&__ourip, &bindaddr.sin_addr, sizeof(__ourip));
-	} else {
-		hp = gethostbyname(ourhost);
-		if (!hp) {
-			ast_log(LOG_WARNING, "Unable to get IP address for %s, SIP disabled\n", ourhost);
-			return 0;
+	if (!use_external_ip) {	
+		if (ntohl(bindaddr.sin_addr.s_addr)) {
+			memcpy(&__ourip, &bindaddr.sin_addr, sizeof(__ourip));
+		} else {
+			hp = gethostbyname(ourhost);
+			if (!hp) {
+				ast_log(LOG_WARNING, "Unable to get IP address for %s, SIP disabled\n", ourhost);
+				return 0;
+			}
+			memcpy(&__ourip, hp->h_addr, sizeof(__ourip));
 		}
-		memcpy(&__ourip, hp->h_addr, sizeof(__ourip));
 	}
 	if (!ntohs(bindaddr.sin_port))
 		bindaddr.sin_port = ntohs(DEFAULT_SIP_PORT);




More information about the svn-commits mailing list