[asterisk-commits] dvossel: branch dvossel/stun_monitor r281719 - in /team/dvossel/stun_monitor:...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Wed Aug 11 09:45:21 CDT 2010


Author: dvossel
Date: Wed Aug 11 09:45:17 2010
New Revision: 281719

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=281719
Log:
res_stun_monitor generates network change event when external address changes.  sip now subscribes to this event.

Modified:
    team/dvossel/stun_monitor/channels/chan_sip.c
    team/dvossel/stun_monitor/include/asterisk/event_defs.h
    team/dvossel/stun_monitor/res/res_stun_monitor.c

Modified: team/dvossel/stun_monitor/channels/chan_sip.c
URL: http://svnview.digium.com/svn/asterisk/team/dvossel/stun_monitor/channels/chan_sip.c?view=diff&rev=281719&r1=281718&r2=281719
==============================================================================
--- team/dvossel/stun_monitor/channels/chan_sip.c (original)
+++ team/dvossel/stun_monitor/channels/chan_sip.c Wed Aug 11 09:45:17 2010
@@ -762,6 +762,8 @@
 static struct ast_flags global_flags[3] = {{0}};  /*!< global SIP_ flags */
 static int global_t38_maxdatagram;                /*!< global T.38 FaxMaxDatagram override */
 
+static struct ast_event_sub *network_change_event_subscription = NULL;     /*!< subscription id for network change events */
+
 static char used_context[AST_MAX_CONTEXT];        /*!< name of automatically created context for unloading */
 
 AST_MUTEX_DEFINE_STATIC(netlock);
@@ -1334,6 +1336,7 @@
 static void sip_poke_all_peers(void);
 static void sip_peer_hold(struct sip_pvt *p, int hold);
 static void mwi_event_cb(const struct ast_event *, void *);
+static void network_change_event_cb(const struct ast_event *, void *);
 
 /*--- Applications, functions, CLI and manager command helpers */
 static const char *sip_nat_mode(const struct sip_pvt *p);
@@ -13422,6 +13425,31 @@
 	ao2_lock(peer);
 	sip_send_mwi_to_peer(peer, event, 0);
 	ao2_unlock(peer);
+}
+
+static inline void network_change_event_subscribe(void)
+{
+	if (!network_change_event_subscription) {
+		network_change_event_subscription = ast_event_subscribe(AST_EVENT_NETWORK_CHANGE,
+			network_change_event_cb,
+			"SIP Network Change ",
+			NULL);
+	}
+}
+
+static inline void network_change_event_unsubscribe(void)
+{
+	if (network_change_event_subscription) {
+		network_change_event_subscription = ast_event_unsubscribe(network_change_event_subscription);
+	}
+}
+
+static void network_change_event_cb(const struct ast_event *event, void *userdata)
+{
+
+	//todohere
+	ast_log(LOG_NOTICE, "SIP, got a network change event\n");
+
 }
 
 /*! \brief Callback for the devicestate notification (SUBSCRIBE) support subsystem
@@ -28356,6 +28384,7 @@
 
 
 	sip_register_tests();
+	network_change_event_subscribe();
 
 	return AST_MODULE_LOAD_SUCCESS;
 }
@@ -28498,6 +28527,7 @@
 	ast_cc_agent_unregister(&sip_cc_agent_callbacks);
 
 	sip_reqresp_parser_exit();
+	network_change_event_unsubscribe();
 	sip_unregister_tests();
 
 	return 0;

Modified: team/dvossel/stun_monitor/include/asterisk/event_defs.h
URL: http://svnview.digium.com/svn/asterisk/team/dvossel/stun_monitor/include/asterisk/event_defs.h?view=diff&rev=281719&r1=281718&r2=281719
==============================================================================
--- team/dvossel/stun_monitor/include/asterisk/event_defs.h (original)
+++ team/dvossel/stun_monitor/include/asterisk/event_defs.h Wed Aug 11 09:45:17 2010
@@ -52,8 +52,10 @@
 	AST_EVENT_CEL                 = 0x07,
 	/*! A report of a security related event (see security_events.h) */
 	AST_EVENT_SECURITY            = 0x08,
+	/*! Used by res_stun_monitor to alert listeners to an exernal network address change. */
+	AST_EVENT_NETWORK_CHANGE      = 0x09,
 	/*! Number of event types.  This should be the last event type + 1 */
-	AST_EVENT_TOTAL               = 0x09,
+	AST_EVENT_TOTAL               = 0x0a,
 };
 
 /*! \brief Event Information Element types */

Modified: team/dvossel/stun_monitor/res/res_stun_monitor.c
URL: http://svnview.digium.com/svn/asterisk/team/dvossel/stun_monitor/res/res_stun_monitor.c?view=diff&rev=281719&r1=281718&r2=281719
==============================================================================
--- team/dvossel/stun_monitor/res/res_stun_monitor.c (original)
+++ team/dvossel/stun_monitor/res/res_stun_monitor.c Wed Aug 11 09:45:17 2010
@@ -64,34 +64,53 @@
 static int stun_monitor_request(const void *blarg)
 {
 	int res;
+	int generate_event = 0;
 	struct sockaddr_in answer = { 0, };
+
 
 	/* once the stun socket goes away, this scheduler item will go away as well */
 	ast_mutex_lock(&args.lock);
-	if (args.stunsock > -1) {
-		if (!(ast_stun_request(args.stunsock, &args.stunaddr, NULL, &answer)) &&
-			(memcmp(&args.externaladdr, &answer, sizeof(args.externaladdr)))) {
-			const char *newaddr = ast_strdupa(ast_inet_ntoa(answer.sin_addr));
-			int newport = ntohs(answer.sin_port);
-
-			ast_log(LOG_NOTICE, "STUN MONITOR: Old external address/port %s:%d now seen as %s:%d \n",
-				ast_inet_ntoa(args.externaladdr.sin_addr), ntohs(args.externaladdr.sin_port),
-				newaddr, newport);
-
-			memcpy(&args.externaladdr, &answer, sizeof(args.externaladdr));
-
-			if (args.externaladdr_known) {
-				/* todohere external addr addr is different, push event out */
-			} else {
-				/* this was the first external address we found, do not alert listeners
-				 * until this address changes to something else. */
-				args.externaladdr_known = 1;
-			}
-		}
-	} else {
+	if (args.stunsock == -1) {
 		ast_log(LOG_ERROR, "STUN monitor: can not send STUN request, socket is not open\n");
-	}
-
+		goto monitor_request_cleanup;
+	}
+	if (!(ast_stun_request(args.stunsock, &args.stunaddr, NULL, &answer)) &&
+		(memcmp(&args.externaladdr, &answer, sizeof(args.externaladdr)))) {
+		const char *newaddr = ast_strdupa(ast_inet_ntoa(answer.sin_addr));
+		int newport = ntohs(answer.sin_port);
+
+		ast_log(LOG_NOTICE, "STUN MONITOR: Old external address/port %s:%d now seen as %s:%d \n",
+			ast_inet_ntoa(args.externaladdr.sin_addr), ntohs(args.externaladdr.sin_port),
+			newaddr, newport);
+
+		memcpy(&args.externaladdr, &answer, sizeof(args.externaladdr));
+
+		if (args.externaladdr_known) {
+			/* the external address was already known, and has changed... generate event. */
+			generate_event = 1;
+
+		} else {
+			/* this was the first external address we found, do not alert listeners
+			 * until this address changes to something else. */
+			args.externaladdr_known = 1;
+		}
+	}
+
+	if (generate_event) {
+		struct ast_event *event = ast_event_new(AST_EVENT_NETWORK_CHANGE, AST_EVENT_IE_END);
+		if (!event) {
+			ast_log(LOG_ERROR, "STUN monitor: could not create AST_EVENT_NETWORK_CHANGE event.\n");
+			goto monitor_request_cleanup;
+		}
+		if (ast_event_queue(event)) {
+			ast_event_destroy(event);
+			event = NULL;
+			ast_log(LOG_ERROR, "STUN monitor: could not queue AST_EVENT_NETWORK_CHANGE event.\n");
+			goto monitor_request_cleanup;
+		}
+	}
+
+monitor_request_cleanup:
 	/* always refresh this scheduler item.  It will be removed elsewhere when
 	 * it is supposed to go away */
 	res = args.refresh * 1000;




More information about the asterisk-commits mailing list