[asterisk-commits] phsultan: branch phsultan/jingle-support r258025 - in /team/phsultan/jingle-s...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Tue Apr 20 08:41:15 CDT 2010


Author: phsultan
Date: Tue Apr 20 08:41:13 2010
New Revision: 258025

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=258025
Log:
Add new ICE related STUN attributes, fix STUN packets reads and connectivity
checks.

The following standard attributes have been added :
- ICE_CONTROLLING
- ICE_CONTROLLED
- PRIORITY
- USE_CANDIDATE

STUN packets coming to the RTCP socket were ignored, this is now fixed.

Connectivity checks as described in the ICE draft (to be RFC 5245) are now
implemented. We answer STUN Binding Requests, and we send STUN Binding Requests
to each advertized candidate.

Modified:
    team/phsultan/jingle-support/channels/chan_jingle.c
    team/phsultan/jingle-support/include/asterisk/stun.h
    team/phsultan/jingle-support/main/stun.c
    team/phsultan/jingle-support/res/res_rtp_asterisk.c

Modified: team/phsultan/jingle-support/channels/chan_jingle.c
URL: http://svnview.digium.com/svn/asterisk/team/phsultan/jingle-support/channels/chan_jingle.c?view=diff&rev=258025&r1=258024&r2=258025
==============================================================================
--- team/phsultan/jingle-support/channels/chan_jingle.c (original)
+++ team/phsultan/jingle-support/channels/chan_jingle.c Tue Apr 20 08:41:13 2010
@@ -1113,7 +1113,8 @@
 
 static int jingle_update_stun(struct jingle *client, struct jingle_pvt *p)
 {
-	struct jingle_candidate *tmp;
+	struct jingle_candidate *theirs;
+	struct jingle_candidate *ours;
 	struct hostent *hp;
 	struct ast_hostent ahp;
 	struct sockaddr_in sin;
@@ -1121,21 +1122,27 @@
 	if (time(NULL) == p->laststun)
 		return 0;
 
-	tmp = p->theircandidates;
+	theirs = p->theircandidates;
+	ours = p->ourcandidates;
 	p->laststun = time(NULL);
-	while (tmp) {
+	while (ours) {
 		struct stun_credentials credentials;
-		hp = ast_gethostbyname(tmp->ip, &ahp);
-		sin.sin_family = AF_INET;
-		memcpy(&sin.sin_addr, hp->h_addr, sizeof(sin.sin_addr));
-		sin.sin_port = htons(tmp->port);
 		ast_copy_string(credentials.local_ufrag, p->local_ufrag, sizeof(credentials.local_ufrag));
 		ast_copy_string(credentials.remote_ufrag, p->remote_ufrag, sizeof(credentials.remote_ufrag));
 		ast_copy_string(credentials.local_password, p->local_password, sizeof(credentials.local_password));
 		ast_copy_string(credentials.remote_password, p->remote_password, sizeof(credentials.remote_password));
-
-		ast_rtp_instance_stun_request(p->rtp, &sin, &credentials);
-		tmp = tmp->next;
+		credentials.priority = ours->priority;
+		credentials.initiator = p->initiator;
+
+		while (theirs) {
+			hp = ast_gethostbyname(theirs->ip, &ahp);
+			sin.sin_family = AF_INET;
+			memcpy(&sin.sin_addr, hp->h_addr, sizeof(sin.sin_addr));
+			sin.sin_port = htons(theirs->port);
+			ast_rtp_instance_stun_request(p->rtp, &sin, &credentials);
+			theirs = theirs->next;
+		}
+		ours = ours->next;
 	}
 	return 1;
 }

Modified: team/phsultan/jingle-support/include/asterisk/stun.h
URL: http://svnview.digium.com/svn/asterisk/team/phsultan/jingle-support/include/asterisk/stun.h?view=diff&rev=258025&r1=258024&r2=258025
==============================================================================
--- team/phsultan/jingle-support/include/asterisk/stun.h (original)
+++ team/phsultan/jingle-support/include/asterisk/stun.h Tue Apr 20 08:41:13 2010
@@ -49,6 +49,8 @@
 	char remote_ufrag[AST_STUN_MAXLEN];
 	char local_password[AST_STUN_MAXLEN];
 	char remote_password[AST_STUN_MAXLEN];
+	unsigned long priority;
+	int initiator;
 };
 
 /*! \brief Generic STUN request

Modified: team/phsultan/jingle-support/main/stun.c
URL: http://svnview.digium.com/svn/asterisk/team/phsultan/jingle-support/main/stun.c?view=diff&rev=258025&r1=258024&r2=258025
==============================================================================
--- team/phsultan/jingle-support/main/stun.c (original)
+++ team/phsultan/jingle-support/main/stun.c Tue Apr 20 08:41:13 2010
@@ -41,7 +41,7 @@
 #include <openssl/hmac.h>
 #endif
 
-static int stundebug;			/*!< Are we debugging stun? */
+static int stundebug = 1;			/*!< Are we debugging stun? */
 
 /*!
  * \brief STUN support code
@@ -113,10 +113,15 @@
 #define STUN_REALM		0x0014
 #define STUN_NONCE		0x0015
 #define STUN_XOR_MAPPED_ADDRESS	0x0020
+#define STUN_PRIORITY		0x0024
+#define STUN_USE_CANDIDATE	0x0025
+
 /*! \brief Optional attribute range (0x8000-0xFFFF) */
 #define STUN_SOFTWARE		0x8022
 #define STUN_ALTERNATE_SERVER	0x8023
 #define STUN_FINGERPRINT	0x8028
+#define STUN_CONTROLLED		0x8029
+#define STUN_CONTROLLING	0x802a
 
 #define AST_STUN_MESSAGE_INTEGRITY	1
 #define AST_STUN_FINGERPRINT		2
@@ -185,8 +190,16 @@
 		return "Reflected From (old RFC3489 attribute)";
 	case STUN_XOR_MAPPED_ADDRESS:
 		return "XOR Mapped Address";
+	case STUN_PRIORITY:
+		return "Priority";
+	case STUN_USE_CANDIDATE:
+		return "Use Candidate";
 	case STUN_FINGERPRINT:
 		return "Fingerprint";
+	case STUN_CONTROLLED:
+		return "Controlled";
+	case STUN_CONTROLLING:
+		return "Controlling";
 	}
 	return "Non-RFC5389 Attribute";
 }
@@ -241,6 +254,32 @@
 				    stun_attr2str(ntohs(attr->attr)), ntohs(attr->attr), ntohs(attr->len));
 	}
 	return 0;
+}
+
+static void append_attr_empty(struct stun_attr **attr, int attrval, int *len, int *left)
+{
+	int size = sizeof(**attr);
+	if (*left > size) {
+		(*attr)->attr = htons(attrval);
+		(*attr)->len = htons(0);
+		(*attr) = (struct stun_attr *)((*attr)->value);
+		*len += size;
+		*left -= size;
+	}
+}
+
+static void append_attr_integer(struct stun_attr **attr, int attrval, unsigned long value, int *len, int *left)
+{
+	int size = sizeof(**attr) + sizeof(unsigned long);
+	unsigned long aux = htonl(value);
+	if (*left > size) {
+		(*attr)->attr = htons(attrval);
+		(*attr)->len = htons(sizeof(unsigned long));
+		memcpy((*attr)->value, &aux, sizeof(unsigned long));
+		(*attr) = (struct stun_attr *)((*attr)->value + sizeof(unsigned long));
+		*len += size;
+		*left -= size;
+	}
 }
 
 /*! \brief append a string to a STUN message
@@ -521,6 +560,8 @@
 	req->msglen = 0;
 	attr = (struct stun_attr *)req->ies;
 	if (credentials) {
+		append_attr_integer(&attr, STUN_PRIORITY, credentials->priority, &reqlen, &reqleft);
+		append_attr_empty(&attr, credentials->initiator?STUN_CONTROLLING:STUN_CONTROLLED, &reqlen, &reqleft);
 		/* username is "remote_ufrag:local_ufrag" for STUN requests */
 		snprintf(username, sizeof(username), "%s:%s", credentials->remote_ufrag, credentials->local_ufrag);
 		append_attr_string(&attr, STUN_USERNAME, username, &reqlen, &reqleft);

Modified: team/phsultan/jingle-support/res/res_rtp_asterisk.c
URL: http://svnview.digium.com/svn/asterisk/team/phsultan/jingle-support/res/res_rtp_asterisk.c?view=diff&rev=258025&r1=258024&r2=258025
==============================================================================
--- team/phsultan/jingle-support/res/res_rtp_asterisk.c (original)
+++ team/phsultan/jingle-support/res/res_rtp_asterisk.c Tue Apr 20 08:41:13 2010
@@ -1562,7 +1562,7 @@
 	struct ast_frame *f = &ast_null_frame;
 
 	/* Read in RTCP data from the socket */
-	if ((res = recvfrom(rtp->rtcp->s, rtcpdata + AST_FRIENDLY_OFFSET, sizeof(rtcpdata) - sizeof(unsigned int) * AST_FRIENDLY_OFFSET, 0, (struct sockaddr *)&sin, &len)) < 0) {
+	if ((res = recvfrom(rtp->rtcp->s, (unsigned char*)rtcpdata + AST_FRIENDLY_OFFSET, sizeof(rtcpdata) - sizeof(unsigned int) * AST_FRIENDLY_OFFSET, 0, (struct sockaddr *)&sin, &len)) < 0) {
 		ast_assert(errno != EBADF);
 		if (errno != EAGAIN) {
 			ast_log(LOG_WARNING, "RTCP Read error: %s.  Hanging up.\n", strerror(errno));
@@ -1572,7 +1572,7 @@
 	}
 
 	/* Handle STUN packets */
-	if ((rtcpheader[0] & 0xC0000000) == 0) {
+	if ((ntohl(rtcpheader[0]) & 0xC0000000) == 0) {
 		ast_stun_handle_packet(rtp->rtcp->s, &sin, (unsigned char *)rtcpdata + AST_FRIENDLY_OFFSET, res, NULL, NULL, credentials);
 		return &ast_null_frame;
 	}




More information about the asterisk-commits mailing list