[asterisk-dev] stun support in asterisk ? (and a related hack for rtp.c)

Luigi Rizzo rizzo at icir.org
Wed Jul 11 06:02:37 CDT 2007


Hi,
i find myself more and more often behind a NAT box, with asterisk
unable to receive the incoming RTP stream because it sends out
the wrong IP/PORT in the sdp message.

So i was wondering if there is any work in progress for an stun
client (or other nat traversal trick, e.g. olle was suggesting ICE,
whatever it is :) in asterisk.  I see there are bits of code in
rtp.c but not clear on what they do.

As a temporary workaround, I implemented a simple hack inspired by
an idea from Olle at
    http://www.voip-info.org/wiki/view/Asterisk+SIP+chan_sip2
where the remote endpoint of the rtp is rewritten with the source
address from incoming RTP packets. This way, if asterisk receives
the audio stream, can reply back to the sender even if sdp had
a bogus address.

Of course this needs to be polished,
possibly applied to RTCP, and enabled through an rtp.conf option ,
but maybe it is of some interest for others as well ?

Index: main/rtp.c
===================================================================
--- main/rtp.c	(revision 72803)
+++ main/rtp.c	(working copy)
@@ -1237,6 +1237,17 @@
 	res = recvfrom(rtp->s, rtp->rawdata + AST_FRIENDLY_OFFSET, sizeof(rtp->rawdata) - AST_FRIENDLY_OFFSET,
 					0, (struct sockaddr *)&sin, &len);
 
+	if (rtp->them.sin_addr.s_addr != sin.sin_addr.s_addr) {
+		ast_log(LOG_WARNING, "RTP Read addr mismatch: got 0x%lx should be 0x%lx\n",
+			ntohl(sin.sin_addr.s_addr), ntohl(rtp->them.sin_addr.s_addr));
+		rtp->them.sin_addr.s_addr = sin.sin_addr.s_addr;
+	}
+	if (rtp->them.sin_port != sin.sin_port) {
+		ast_log(LOG_WARNING, "RTP Read port mismatch: got 0x%x should be 0x%x\n",
+			ntohs(sin.sin_port), ntohs(rtp->them.sin_port));
+		rtp->them.sin_port = sin.sin_port;
+	}
+
 	rtpheader = (unsigned int *)(rtp->rawdata + AST_FRIENDLY_OFFSET);
 	if (res < 0) {
 		if (errno == EBADF)


cheers
luigi



More information about the asterisk-dev mailing list