[asterisk-commits] branch 1.2-netsec - r7927 in /branches/1.2-netsec: ./ channels/ include/aster...

asterisk-commits at lists.digium.com asterisk-commits at lists.digium.com
Mon Jan 9 21:10:35 CST 2006


Author: kpfleming
Date: Mon Jan  9 21:10:34 2006
New Revision: 7927

URL: http://svn.digium.com/view/asterisk?rev=7927&view=rev
Log:
initial import of Asterisk SIP support for network security devices

Modified:
    branches/1.2-netsec/Makefile
    branches/1.2-netsec/channels/Makefile
    branches/1.2-netsec/channels/chan_sip.c
    branches/1.2-netsec/include/asterisk/rtp.h
    branches/1.2-netsec/rtp.c

Modified: branches/1.2-netsec/Makefile
URL: http://svn.digium.com/view/asterisk/branches/1.2-netsec/Makefile?rev=7927&r1=7926&r2=7927&view=diff
==============================================================================
--- branches/1.2-netsec/Makefile (original)
+++ branches/1.2-netsec/Makefile Mon Jan  9 21:10:34 2006
@@ -104,6 +104,9 @@
 # Inforce the detection of busy singal (get rid of false hangups)
 # Don't use together with -DBUSYDETECT_TONEONLY
 BUSYDETECT+= #-DBUSYDETECT_COMPARE_TONE_AND_SILENCE
+
+# Comment this if you want to disable MIDCOM
+MIDCOM = -DMIDCOM
 
 ifneq ($(OSARCH),SunOS)
   ASTLIBDIR=$(INSTALL_PREFIX)/usr/lib/asterisk
@@ -331,6 +334,7 @@
 ASTCFLAGS+= $(TRACE_FRAMES)
 ASTCFLAGS+= $(MALLOC_DEBUG)
 ASTCFLAGS+= $(BUSYDETECT)
+ASTCFLAGS+= $(MIDCOM)
 ASTCFLAGS+= $(OPTIONS)
 ASTCFLAGS+= -fomit-frame-pointer 
 SUBDIRS=res channels pbx apps codecs formats agi cdr funcs utils stdtime

Modified: branches/1.2-netsec/channels/Makefile
URL: http://svn.digium.com/view/asterisk/branches/1.2-netsec/channels/Makefile?rev=7927&r1=7926&r2=7927&view=diff
==============================================================================
--- branches/1.2-netsec/channels/Makefile (original)
+++ branches/1.2-netsec/channels/Makefile Mon Jan  9 21:10:34 2006
@@ -95,6 +95,11 @@
 ifneq ($(wildcard $(CROSS_COMPILE_TARGET)/usr/lib/libpri.so.1)$(wildcard $(CROSS_COMPILE_TARGET)/usr/local/lib/libpri.so.1),)
   CFLAGS+=-DZAPATA_PRI
   ZAPPRI=-lpri
+endif
+
+ifneq ($(wildcard $(CROSS_COMPILE_TARGET)/usr/lib/asterisk/modules/res_netsec.so)$(wildcard $(CROSS_COMPILE_TARGET)/usr/local/lib/asterisk/modules/res_netsec.so),)
+  CFLAGS+=-DSIP_MIDCOM
+#  NETSEC=-lnetsec -lssl -lcrypto -lstdc++
 endif
 
 ifneq ($(wildcard $(CROSS_COMPILE_TARGET)/usr/lib/libmfcr2.so.1)$(wildcard $(CROSS_COMPILE_TARGET)/usr/local/lib/libmfcr2.so.1),)
@@ -207,7 +212,7 @@
 	$(CC) $(SOLINK) -o $@ $<  $(ZAPPRI) $(ZAPR2) -ltonezone
 
 chan_sip.so: chan_sip.o
-	$(CC) $(SOLINK) -o $@ ${CYGSOLINK} chan_sip.o ${CYGSOLIB}
+	$(CC) $(SOLINK) -o $@ ${CYGSOLINK} chan_sip.o ${CYGSOLIB} ${NETSEC}
 
 chan_agent.so: chan_agent.o
 	$(CC) $(SOLINK) -o $@ ${CYGSOLINK} chan_agent.o ${CYGSOLIB} ${CYG_CHAN_AGENT}

Modified: branches/1.2-netsec/channels/chan_sip.c
URL: http://svn.digium.com/view/asterisk/branches/1.2-netsec/channels/chan_sip.c?rev=7927&r1=7926&r2=7927&view=diff
==============================================================================
--- branches/1.2-netsec/channels/chan_sip.c (original)
+++ branches/1.2-netsec/channels/chan_sip.c Mon Jan  9 21:10:34 2006
@@ -84,6 +84,10 @@
 
 #ifdef OSP_SUPPORT
 #include "asterisk/astosp.h"
+#endif
+
+#ifdef SIP_MIDCOM
+#include "asterisk/res_netsec.h"
 #endif
 
 #ifndef DEFAULT_USERAGENT
@@ -679,6 +683,10 @@
 	
 	struct ast_dsp *vad;			/*!< Voice Activation Detection dsp */
 	
+#ifdef SIP_MIDCOM
+  void *r;
+#endif
+	
 	struct sip_peer *peerpoke;		/*!< If this calls is to poke a peer, which one */
 	struct sip_registry *registry;		/*!< If this is a REGISTER call, to which registry */
 	struct ast_rtp *rtp;			/*!< RTP Session */
@@ -922,6 +930,25 @@
 static int transmit_state_notify(struct sip_pvt *p, int state, int full, int substate);
 static char *gettag(struct sip_request *req, char *header, char *tagbuf, int tagbufsize);
 
+#ifdef SIP_MIDCOM
+static void sip_rtp_get_peer_audio_helper(void *p, struct sockaddr_in *them);
+static void sip_rtp_get_peer_video_helper(void *p, struct sockaddr_in *them);
+static void sip_rtp_get_us_audio_helper(void *p, struct sockaddr_in *sin);
+static void sip_rtp_get_us_video_helper(void *p, struct sockaddr_in *vsin);
+static void sip_map_hook_struct(void *p, void *r);
+static void *sip_get_hook_struct(void *p);
+static int sip_get_flag_novideo(void *p);
+static int sip_cmp_sa_addr(void *p, struct sockaddr_in *addr);
+static void sip_get_recv_addr(void *p, struct in_addr *addr);
+static char *sip_get_username(void *p);
+static struct ast_channel *sip_channel_helper(void *p);
+static struct ast_channel *sip_bridged_channel_helper(void *p);
+static int sip_get_capability_helper(void *p);
+static void sip_softhangup_helper(void *p);
+
+extern struct ast_sip_hook_cb *m_cb;
+#endif
+
 /*! \brief Definition of this channel for PBX channel registration */
 static const struct ast_channel_tech sip_tech = {
 	.type = channeltype,
@@ -2094,6 +2121,11 @@
 
 	if (sip_debug_test_pvt(p))
 		ast_verbose("Destroying call '%s'\n", p->callid);
+
+#ifdef SIP_MIDCOM
+	if (m_cb)
+	  m_cb->__sip_destroy_hook(p);
+#endif
 
 	if (dumphistory)
 		sip_dump_history(p);
@@ -2419,6 +2451,12 @@
 	if (ast->_state != AST_STATE_UP)
 		needcancel = 1;
 
+#ifdef SIP_MIDCOM
+        /* For callee to shutdown, send "BYE" instead of "CANCEL"
+           -- this needs to be verified */
+        if (m_cb && ast_test_flag(p, SIP_OUTGOING)) needcancel = 0;
+#endif
+
 	/* Disconnect */
 	p = ast->tech_pvt;
 	if (p->vad) {
@@ -4339,8 +4377,22 @@
 		ast_rtp_get_us(p->vrtp, &vsin);
 
 	if (p->redirip.sin_addr.s_addr) {
+#ifdef SIP_MIDCOM
+	  if (m_cb && p->r) {
+	    struct sockaddr_in redirip_hook;
+	    char iabuf2[INET_ADDRSTRLEN];
+	    m_cb->ast_get_redirip_audio_hook(p->r, &redirip_hook);
+	    ast_log(LOG_DEBUG, "Replacing %s:%d by %s:%d in SDP before sending to %s\n", ast_inet_ntoa(iabuf, sizeof(iabuf), p->redirip.sin_addr), ntohs(p->redirip.sin_port), ast_inet_ntoa(iabuf2, sizeof(iabuf2), redirip_hook.sin_addr), ntohs(redirip_hook.sin_port), p->username);
+	    dest.sin_port = redirip_hook.sin_port;
+	    dest.sin_addr = redirip_hook.sin_addr;
+	  } else {
 		dest.sin_port = p->redirip.sin_port;
 		dest.sin_addr = p->redirip.sin_addr;
+	  }
+#else
+		dest.sin_port = p->redirip.sin_port;
+		dest.sin_addr = p->redirip.sin_addr;
+#endif
 		if (p->redircodecs)
 			capability = p->redircodecs;
 	} else {
@@ -4351,8 +4403,22 @@
 	/* Determine video destination */
 	if (p->vrtp) {
 		if (p->vredirip.sin_addr.s_addr) {
+#ifdef SIP_MIDCOM
+		  if (m_cb && p->r) {
+		    struct sockaddr_in vredirip_hook;
+		    char iabuf2[INET_ADDRSTRLEN];
+		    m_cb->ast_get_vredirip_video_hook(p->r, &vredirip_hook);
+		    ast_log(LOG_DEBUG, "Replacing %s:%d by %s:%d in video SDP before sending to %s\n", ast_inet_ntoa(iabuf, sizeof(iabuf), p->vredirip.sin_addr), ntohs(p->vredirip.sin_port), ast_inet_ntoa(iabuf2, sizeof(iabuf2), vredirip_hook.sin_addr), ntohs(vredirip_hook.sin_port), p->username);
+		    vdest.sin_port = vredirip_hook.sin_port;
+		    vdest.sin_addr = vredirip_hook.sin_addr;
+		  } else {
 			vdest.sin_port = p->vredirip.sin_port;
 			vdest.sin_addr = p->vredirip.sin_addr;
+		  }
+#else
+			vdest.sin_port = p->vredirip.sin_port;
+			vdest.sin_addr = p->vredirip.sin_addr;
+#endif
 		} else {
 			vdest.sin_addr = p->ourip;
 			vdest.sin_port = vsin.sin_port;
@@ -4509,6 +4575,14 @@
 	} else {
 		ast_log(LOG_ERROR, "Can't add SDP to response, since we have no RTP session allocated. Call-ID %s\n", p->callid);
 	}
+#ifdef SIP_MIDCOM
+	if (m_cb) {
+	  if (!m_cb->transmit_response_with_sdp_hook(p)) { 
+	    ast_log(LOG_NOTICE, "Failed transmit_response_with_sdp_hook()\n");
+	    return -1;
+	  }
+	}
+#endif
 	return send_response(p, &resp, retrans, seqno);
 }
 
@@ -4570,6 +4644,19 @@
 static int transmit_reinvite_with_sdp(struct sip_pvt *p)
 {
 	struct sip_request req;
+
+#ifdef SIP_MIDCOM
+	if (m_cb) {
+	  if (!m_cb->transmit_reinvite_with_sdp_hook(p)) { 
+	    ast_log(LOG_NOTICE, "Failed transmit_reinvite_with_sdp_hook()\n");
+	    if (p->owner)
+	      ast_queue_hangup(p->owner);
+	    else
+	      ast_set_flag(p, SIP_NEEDDESTROY);
+	  }
+	}
+#endif
+
 	if (ast_test_flag(p, SIP_REINVITE_UPDATE))
 		reqprep(&req, p, SIP_UPDATE, 0, 1);
 	else 
@@ -9467,6 +9554,16 @@
 		p->authtries = 0;
 		if (!strcasecmp(get_header(req, "Content-Type"), "application/sdp")) {
 			process_sdp(p, req);
+#ifdef SIP_MIDCOM
+			if (m_cb) {
+			  if (!m_cb->handle_response_invite_hook(p)) {
+			    if (p->owner)
+			      ast_queue_hangup(p->owner);
+			    else
+			      ast_set_flag(p, SIP_NEEDDESTROY);
+			  }
+			}
+#endif
 		}
 
 		/* Parse contact header for continued conversation */
@@ -10308,6 +10405,19 @@
 				ast_set_flag(p, SIP_NEEDDESTROY);	
 				return -1;
 			}
+#ifdef SIP_MIDCOM
+			if (m_cb) {
+			  if (!m_cb->handle_request_invite_hook((void *)p)) {
+			    ast_log(LOG_NOTICE, "Failed to NAT for (%s)\n", get_header(req, "From"));
+			    if (ignore)
+			      transmit_response(p, "403 Forbidden", req);
+			    else
+			      transmit_response_reliable(p, "403 Forbidden", req, 1);
+			    ast_set_flag(p, SIP_NEEDDESTROY);
+			    return 0;
+			  }
+			}
+#endif
 		} else {
 			p->jointcapability = p->capability;
 			ast_log(LOG_DEBUG, "Hm....  No sdp for the moment\n");
@@ -12655,8 +12765,13 @@
 	if (!p)
 		return NULL;
 	ast_mutex_lock(&p->lock);
-	if (p->rtp && ast_test_flag(p, SIP_CAN_REINVITE))
+	if (p->rtp && ast_test_flag(p, SIP_CAN_REINVITE)) {
 		rtp =  p->rtp;
+#ifdef SIP_MIDCOM
+		if (m_cb)
+		  m_cb->ast_rtp_nat_us_audio_hook(rtp, p->r); /* change the ip port in rtp */
+#endif
+	}
 	ast_mutex_unlock(&p->lock);
 	return rtp;
 }
@@ -12671,8 +12786,13 @@
 		return NULL;
 
 	ast_mutex_lock(&p->lock);
-	if (p->vrtp && ast_test_flag(p, SIP_CAN_REINVITE))
+	if (p->vrtp && ast_test_flag(p, SIP_CAN_REINVITE)) {
 		rtp = p->vrtp;
+#ifdef SIP_MIDCOM
+		if (m_cb)
+		  m_cb->ast_rtp_nat_us_video_hook(rtp, p->r); /* change the ip port in rtp */
+#endif
+	}
 	ast_mutex_unlock(&p->lock);
 	return rtp;
 }
@@ -12686,12 +12806,22 @@
 	if (!p) 
 		return -1;
 	ast_mutex_lock(&p->lock);
-	if (rtp)
+		if (rtp) {
 		ast_rtp_get_peer(rtp, &p->redirip);
+#ifdef SIP_MIDCOM
+		if (m_cb)
+		  m_cb->ast_rtp_get_their_nat_audio_hook(rtp, p->r);
+#endif
+		}
 	else
 		memset(&p->redirip, 0, sizeof(p->redirip));
-	if (vrtp)
+		if (vrtp) {
 		ast_rtp_get_peer(vrtp, &p->vredirip);
+#ifdef SIP_MIDCOM
+		if (m_cb)
+		  m_cb->ast_rtp_get_their_nat_video_hook(vrtp, p->r);
+#endif
+		}
 	else
 		memset(&p->vredirip, 0, sizeof(p->vredirip));
 	p->redircodecs = codecs;
@@ -12958,6 +13088,26 @@
 	set_rtp_peer: sip_set_rtp_peer,
 	get_codec: sip_get_codec,
 };
+
+#ifdef SIP_MIDCOM
+/*! \brief  sip_helper: Interface structure with callbacks used to connect to midcom module --*/
+static struct ast_sip_helper_cb sip_helper = {
+	ast_rtp_get_peer_audio_helper: sip_rtp_get_peer_audio_helper,
+	ast_rtp_get_peer_video_helper: sip_rtp_get_peer_video_helper,
+	ast_rtp_get_us_audio_helper: sip_rtp_get_us_audio_helper,
+	ast_rtp_get_us_video_helper: sip_rtp_get_us_video_helper,
+	ast_map_hook_struct: sip_map_hook_struct,
+	ast_get_hook_struct: sip_get_hook_struct,
+	ast_get_flag_novideo: sip_get_flag_novideo,
+	ast_cmp_sa_addr: sip_cmp_sa_addr,
+	ast_get_recv_addr: sip_get_recv_addr,
+	ast_get_username: sip_get_username,
+	ast_channel_helper: sip_channel_helper,
+	ast_bridged_channel_helper: sip_bridged_channel_helper,
+	ast_get_capability_helper: sip_get_capability_helper,
+	ast_softhangup_helper: sip_softhangup_helper,
+};
+#endif
 
 /*! \brief  sip_poke_all_peers: Send a poke to all known peers */
 static void sip_poke_all_peers(void)
@@ -13094,6 +13244,12 @@
 	/* Tell the RTP subdriver that we're here */
 	ast_rtp_proto_register(&sip_rtp);
 
+#ifdef SIP_MIDCOM
+	/* Register the sip helper functions */
+	if (m_cb)
+	  m_cb->ast_sip_helper_register(&sip_helper);
+#endif
+
 	/* Register dialplan applications */
 	ast_register_application(app_dtmfmode, sip_dtmfmode, synopsis_dtmfmode, descrip_dtmfmode);
 
@@ -13141,6 +13297,12 @@
 	ast_cli_unregister_multiple(my_clis, sizeof(my_clis) / sizeof(my_clis[0]));
 
 	ast_rtp_proto_unregister(&sip_rtp);
+
+#ifdef SIP_MIDCOM
+	/* Unregister the sip helper functions */
+	if (m_cb)
+	  m_cb->ast_sip_helper_unregister();
+#endif
 
 	ast_manager_unregister("SIPpeers");
 	ast_manager_unregister("SIPshowpeer");
@@ -13226,4 +13388,78 @@
 	return (char *) desc;
 }
 
-
+#ifdef SIP_MIDCOM
+static void sip_rtp_get_peer_audio_helper(void *p, struct sockaddr_in *them)
+{
+  ast_rtp_get_peer(((struct sip_pvt*)p)->rtp, them);
+}
+
+static void sip_rtp_get_peer_video_helper(void *p, struct sockaddr_in *them)
+{
+  ast_rtp_get_peer(((struct sip_pvt*)p)->vrtp, them);
+}
+
+static void sip_rtp_get_us_audio_helper(void *p, struct sockaddr_in *sin)
+{
+  ast_rtp_get_us(((struct sip_pvt*)p)->rtp, sin);
+  sin->sin_addr = ((struct sip_pvt*)p)->ourip;
+}
+
+static void sip_rtp_get_us_video_helper(void *p, struct sockaddr_in *vsin)
+{
+  ast_rtp_get_us(((struct sip_pvt*)p)->vrtp, vsin);
+  vsin->sin_addr = ((struct sip_pvt*)p)->ourip;
+}
+
+static void sip_map_hook_struct(void *p, void *r)
+{
+  ((struct sip_pvt*)p)->r = r;
+}
+
+static void *sip_get_hook_struct(void *p)
+{
+  return ((struct sip_pvt*)p)->r;
+}
+
+static int sip_get_flag_novideo(void *p)
+{
+  return ast_test_flag((struct sip_pvt*)p, SIP_NOVIDEO);
+}
+
+static int sip_cmp_sa_addr(void *p, struct sockaddr_in *addr)
+{
+  return (((struct sip_pvt*)p)->sa.sin_addr.s_addr == addr->sin_addr.s_addr);
+}
+
+static void sip_get_recv_addr(void *p, struct in_addr *addr)
+{
+  memcpy(addr, &((struct sip_pvt *)p)->recv.sin_addr, sizeof(struct in_addr));
+}
+
+static char *sip_get_username(void *p)
+{
+  return ((struct sip_pvt*)p)->username;
+}
+
+static struct ast_channel *sip_channel_helper(void *p)
+{
+  return ((struct sip_pvt*)p)->owner;
+}
+
+static struct ast_channel *sip_bridged_channel_helper(void *p)
+{
+  return ast_bridged_channel(((struct sip_pvt*)p)->owner);
+}
+
+static int sip_get_capability_helper(void *p)
+{
+  return ((struct sip_pvt*)p)->jointcapability;
+}
+
+static void sip_softhangup_helper(void *p)
+{
+  if (p && ((struct sip_pvt *)p)->owner)
+    ast_softhangup(((struct sip_pvt *)p)->owner, AST_SOFTHANGUP_APPUNLOAD);
+}
+#endif
+

Modified: branches/1.2-netsec/include/asterisk/rtp.h
URL: http://svn.digium.com/view/asterisk/branches/1.2-netsec/include/asterisk/rtp.h?rev=7927&r1=7926&r2=7927&view=diff
==============================================================================
--- branches/1.2-netsec/include/asterisk/rtp.h (original)
+++ branches/1.2-netsec/include/asterisk/rtp.h Mon Jan  9 21:10:34 2006
@@ -100,6 +100,11 @@
 
 void ast_rtp_get_us(struct ast_rtp *rtp, struct sockaddr_in *us);
 
+#ifdef MIDCOM
+void ast_rtp_nat_us(struct ast_rtp *rtp, struct sockaddr_in *our_nat);
+void ast_rtp_get_their_nat(struct ast_rtp *rtp, struct sockaddr_in *their_nat);
+#endif
+
 void ast_rtp_destroy(struct ast_rtp *rtp);
 
 void ast_rtp_reset(struct ast_rtp *rtp);

Modified: branches/1.2-netsec/rtp.c
URL: http://svn.digium.com/view/asterisk/branches/1.2-netsec/rtp.c?rev=7927&r1=7926&r2=7927&view=diff
==============================================================================
--- branches/1.2-netsec/rtp.c (original)
+++ branches/1.2-netsec/rtp.c Mon Jan  9 21:10:34 2006
@@ -125,6 +125,10 @@
 	int rtp_lookup_code_cache_code;
 	int rtp_lookup_code_cache_result;
 	int rtp_offered_from_local;
+
+#ifdef MIDCOM
+  struct sockaddr_in them_midcom_nat;
+#endif
 	struct ast_rtcp *rtcp;
 };
 
@@ -1041,6 +1045,20 @@
 	memcpy(us, &rtp->us, sizeof(rtp->us));
 }
 
+#ifdef MIDCOM /* RANCH */
+void ast_rtp_nat_us(struct ast_rtp *rtp, struct sockaddr_in *our_nat)
+{
+  memcpy(&rtp->them_midcom_nat, our_nat, sizeof(rtp->them_midcom_nat));
+}
+
+void ast_rtp_get_their_nat(struct ast_rtp *rtp, struct sockaddr_in *their_nat)
+{
+  their_nat->sin_family = AF_INET;
+  their_nat->sin_port = rtp->them_midcom_nat.sin_port;
+  their_nat->sin_addr = rtp->them_midcom_nat.sin_addr;
+}
+#endif
+
 void ast_rtp_stop(struct ast_rtp *rtp)
 {
 	memset(&rtp->them.sin_addr, 0, sizeof(rtp->them.sin_addr));
@@ -1515,7 +1533,6 @@
 	struct sockaddr_in t0, t1;
 	struct sockaddr_in vt0, vt1;
 	char iabuf[INET_ADDRSTRLEN];
-	
 	void *pvt0, *pvt1;
 	int codec0,codec1, oldcodec0, oldcodec1;
 	
@@ -1670,8 +1687,12 @@
 			if (option_debug) {
 				ast_log(LOG_DEBUG, "Oooh, '%s' changed end address to %s:%d (format %d)\n", 
 					c0->name, ast_inet_ntoa(iabuf, sizeof(iabuf), t0.sin_addr), ntohs(t0.sin_port), codec0);
+			ast_log(LOG_DEBUG, "Oooh, '%s' changed end vaddress to %s:%d (format %d)\n", 
+				c0->name, ast_inet_ntoa(iabuf, sizeof(iabuf), vt0.sin_addr), ntohs(vt0.sin_port), codec0);
 				ast_log(LOG_DEBUG, "Oooh, '%s' was %s:%d/(format %d)\n", 
 					c0->name, ast_inet_ntoa(iabuf, sizeof(iabuf), ac0.sin_addr), ntohs(ac0.sin_port), oldcodec0);
+			ast_log(LOG_DEBUG, "Oooh, '%s' wasv %s:%d/(format %d)\n", 
+				c0->name, ast_inet_ntoa(iabuf, sizeof(iabuf), vac0.sin_addr), ntohs(vac0.sin_port), oldcodec0);
 			}
 			if (pr1->set_rtp_peer(c1, t0.sin_addr.s_addr ? p0 : NULL, vt0.sin_addr.s_addr ? vp0 : NULL, codec0, ast_test_flag(p0, FLAG_NAT_ACTIVE)))
 				ast_log(LOG_WARNING, "Channel '%s' failed to update to '%s'\n", c1->name, c0->name);



More information about the asterisk-commits mailing list