[asterisk-commits] dvossel: branch dvossel/gtalk_fixup r291116 - in /team/dvossel/gtalk_fixup: c...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Mon Oct 11 14:10:51 CDT 2010


Author: dvossel
Date: Mon Oct 11 14:10:49 2010
New Revision: 291116

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=291116
Log:
chan_gtalk can now get externip from stun server

Modified:
    team/dvossel/gtalk_fixup/channels/chan_gtalk.c
    team/dvossel/gtalk_fixup/configs/gtalk.conf.sample
    team/dvossel/gtalk_fixup/include/asterisk/stun.h
    team/dvossel/gtalk_fixup/res/res_stun_monitor.c

Modified: team/dvossel/gtalk_fixup/channels/chan_gtalk.c
URL: http://svnview.digium.com/svn/asterisk/team/dvossel/gtalk_fixup/channels/chan_gtalk.c?view=diff&rev=291116&r1=291115&r2=291116
==============================================================================
--- team/dvossel/gtalk_fixup/channels/chan_gtalk.c (original)
+++ team/dvossel/gtalk_fixup/channels/chan_gtalk.c Mon Oct 11 14:10:49 2010
@@ -233,6 +233,7 @@
 };
 
 static char externip[16];
+static struct sockaddr_in stunaddr; /*!< the stun server we get the externip from */
 
 static struct gtalk_container gtalk_list;
 
@@ -1331,6 +1332,40 @@
 	return 1;
 }
 
+static int gtalk_get_externip(struct sockaddr_in *dst)
+{
+	int sock;
+	char *newaddr;
+	struct sockaddr_in answer = { 0, };
+	struct ast_sockaddr tmp_dst;
+
+
+	sock = socket(AF_INET, SOCK_DGRAM, 0);
+	if (sock < 0) {
+		ast_log(LOG_WARNING, "Unable to create STUN socket: %s\n", strerror(errno));
+		return -1;
+	}
+
+	ast_sockaddr_from_sin(&tmp_dst, dst);
+	if (ast_connect(sock, &tmp_dst) != 0) {
+		ast_log(LOG_WARNING, "STUN Failed to connect to %s\n", ast_sockaddr_stringify(&tmp_dst));
+		close(sock);
+		return -1;
+	}
+
+	if ((ast_stun_request(sock, &stunaddr, NULL, &answer))) {
+		close(sock);
+		return -1;
+	}
+
+	newaddr = ast_strdupa(ast_inet_ntoa(answer.sin_addr));
+	memcpy(externip, newaddr, sizeof(externip));
+
+	close(sock);
+	return 0;
+
+}
+
 static int gtalk_update_stun(struct gtalk *client, struct gtalk_pvt *p)
 {
 	struct gtalk_candidate *tmp;
@@ -1470,8 +1505,9 @@
 {
 	struct ast_frame *f;
 
-	if (!p->rtp)
+	if (!p->rtp) {
 		return &ast_null_frame;
+	}
 	f = ast_rtp_instance_read(p->rtp, 0);
 	gtalk_update_stun(p->parent, p);
 	if (p->owner) {
@@ -2063,32 +2099,40 @@
 	/* Copy the default jb config over global_jbconf */
 	memcpy(&global_jbconf, &default_jbconf, sizeof(struct ast_jb_conf));
 
+	/* set defaults */
+	memset(&stunaddr, 0, sizeof(stunaddr));
+
 	cat = ast_category_browse(cfg, NULL);
 	for (var = ast_variable_browse(cfg, "general"); var; var = var->next) {
 		/* handle jb conf */
 		if (!ast_jb_read_conf(&global_jbconf, var->name, var->value))
 			continue;
 
-		if (!strcasecmp(var->name, "allowguest"))
-			allowguest =
-				(ast_true(ast_variable_retrieve(cfg, "general", "allowguest"))) ? 1 : 0;
-		else if (!strcasecmp(var->name, "disallow"))
+		if (!strcasecmp(var->name, "allowguest")) {
+			allowguest = (ast_true(ast_variable_retrieve(cfg, "general", "allowguest"))) ? 1 : 0;
+		} else if (!strcasecmp(var->name, "disallow")) {
 			ast_parse_allow_disallow(&prefs, &global_capability, var->value, 0);
-		else if (!strcasecmp(var->name, "allow"))
+		} else if (!strcasecmp(var->name, "allow")) {
 			ast_parse_allow_disallow(&prefs, &global_capability, var->value, 1);
-		else if (!strcasecmp(var->name, "context"))
+		} else if (!strcasecmp(var->name, "context")) {
 			ast_copy_string(context, var->value, sizeof(context));
-		else if (!strcasecmp(var->name, "externip"))
+		} else if (!strcasecmp(var->name, "externip")) {
 			ast_copy_string(externip, var->value, sizeof(externip));
-		else if (!strcasecmp(var->name, "parkinglot"))
+		} else if (!strcasecmp(var->name, "parkinglot")) {
 			ast_copy_string(parkinglot, var->value, sizeof(parkinglot));
-		else if (!strcasecmp(var->name, "bindaddr")) {
+		} else if (!strcasecmp(var->name, "bindaddr")) {
 			if (!(hp = ast_gethostbyname(var->value, &ahp))) {
 				ast_log(LOG_WARNING, "Invalid address: %s\n", var->value);
 			} else {
 				memcpy(&bindaddr.sin_addr, hp->h_addr, sizeof(bindaddr.sin_addr));
 			}
-		}
+		} else if (!strcasecmp(var->name, "stunaddr")) {
+			stunaddr.sin_port = htons(STANDARD_STUN_PORT);
+			if (ast_parse_arg(var->value, PARSE_INADDR, &stunaddr)) {
+				ast_log(LOG_WARNING, "Invalid STUN server address: %s\n", var->value);
+			}
+		}
+
 /*  Idea to allow for custom candidates  */
 /*
 		else if (!strcasecmp(var->name, "candidate")) {
@@ -2165,6 +2209,10 @@
 		}
 		cat = ast_category_browse(cfg, cat);
 	}
+
+	if (stunaddr.sin_addr.s_addr) {
+		gtalk_get_externip(&stunaddr);
+	}
 	gtalk_free_candidates(global_candidates);
 	return 1;
 }
@@ -2194,12 +2242,14 @@
 	}
 
 	sched = sched_context_create();
-	if (!sched) 
+	if (!sched) {
 		ast_log(LOG_WARNING, "Unable to create schedule context\n");
+	}
 
 	io = io_context_create();
-	if (!io) 
+	if (!io) {
 		ast_log(LOG_WARNING, "Unable to create I/O context\n");
+	}
 
 	ast_sockaddr_from_sin(&bindaddr_tmp, &bindaddr);
 	if (ast_find_ourip(&ourip_tmp, &bindaddr_tmp)) {

Modified: team/dvossel/gtalk_fixup/configs/gtalk.conf.sample
URL: http://svnview.digium.com/svn/asterisk/team/dvossel/gtalk_fixup/configs/gtalk.conf.sample?view=diff&rev=291116&r1=291115&r2=291116
==============================================================================
--- team/dvossel/gtalk_fixup/configs/gtalk.conf.sample (original)
+++ team/dvossel/gtalk_fixup/configs/gtalk.conf.sample Mon Oct 11 14:10:49 2010
@@ -1,21 +1,25 @@
 ;[general]
-;context=default        ;;Context to dump call into
-;bindaddr=0.0.0.0       ;;Address to bind to
-;externip=127.0.0.1     ;;Set your external ip if you are behind a NAT.
-;allowguest=yes			;;Allow calls from people not in
-                        ;;list of peers
+;context=default     ;Context to dump call into
+;bindaddr=0.0.0.0    ;Address to bind to
+;externip=127.0.0.1  ;Set your external ip if you are behind a NAT.
+
+;stunaddr=mystunserver.com ; Get your external ip from a STUN server.
+                           ; Note, if the STUN query is successful, this will
+                           ; replace any value placed in externip;
+;allowguest=yes    ;Allow calls from people not in
+                   ;list of peers
 ;
-;[guest]          ;;special account for options on guest account
+;[guest]        ;special account for options on guest account
 ;disallow=all
 ;allow=ulaw
 ;context=guest
 ;
 ;[ogorman]
-;username=ogorman at gmail.com ;;username of the peer your
-                            ;;calling or accepting calls from
+;username=ogorman at gmail.com ;username of the peer your
+                            ;calling or accepting calls from
 ;disallow=all
 ;allow=ulaw
 ;context=default
-;connection=asterisk        ;;client or component in jabber.conf
-                            ;;for the call to leave on.
+;connection=asterisk   ;client or component in jabber.conf
+                       ;for the call to leave on.
 ;

Modified: team/dvossel/gtalk_fixup/include/asterisk/stun.h
URL: http://svnview.digium.com/svn/asterisk/team/dvossel/gtalk_fixup/include/asterisk/stun.h?view=diff&rev=291116&r1=291115&r2=291116
==============================================================================
--- team/dvossel/gtalk_fixup/include/asterisk/stun.h (original)
+++ team/dvossel/gtalk_fixup/include/asterisk/stun.h Mon Oct 11 14:10:49 2010
@@ -31,6 +31,8 @@
 #if defined(__cplusplus) || defined(c_plusplus)
 extern "C" {
 #endif
+
+static const int STANDARD_STUN_PORT = 3478;
 
 enum ast_stun_result {
 	AST_STUN_IGNORE = 0,

Modified: team/dvossel/gtalk_fixup/res/res_stun_monitor.c
URL: http://svnview.digium.com/svn/asterisk/team/dvossel/gtalk_fixup/res/res_stun_monitor.c?view=diff&rev=291116&r1=291115&r2=291116
==============================================================================
--- team/dvossel/gtalk_fixup/res/res_stun_monitor.c (original)
+++ team/dvossel/gtalk_fixup/res/res_stun_monitor.c Mon Oct 11 14:10:49 2010
@@ -36,7 +36,6 @@
 #include "asterisk/lock.h"
 #include <fcntl.h>
 
-static const int STANDARD_STUN_PORT = 3478;
 static const int DEFAULT_MONITOR_REFRESH = 30;
 
 static const char stun_conf_file[] = "res_stun_monitor.conf";




More information about the asterisk-commits mailing list