r2612 - trunk/libminisip/source/rtp

werner at minisip.org werner at minisip.org
Sun Jun 4 10:07:45 CEST 2006


Author: werner
Date: 2006-06-04 10:07:43 +0200 (Sun, 04 Jun 2006)
New Revision: 2612

Modified:
   trunk/libminisip/source/rtp/CryptoContext.cxx
   trunk/libminisip/source/rtp/RtpHeader.cxx
   trunk/libminisip/source/rtp/RtpPacket.cxx
   trunk/libminisip/source/rtp/SRtpPacket.cxx
Log:
Add ZRTP support, protected with #ifdef ZRTP_SUPPORT

Modified: trunk/libminisip/source/rtp/CryptoContext.cxx
===================================================================
--- trunk/libminisip/source/rtp/CryptoContext.cxx	2006-06-04 08:07:22 UTC (rev 2611)
+++ trunk/libminisip/source/rtp/CryptoContext.cxx	2006-06-04 08:07:43 UTC (rev 2612)
@@ -47,7 +47,7 @@
 encr(1), auth(1){}/*These should be set to 0, but for backward compatability they are set to 1  *///encryption(no_encr),authentication(no_auth)
 
 CryptoContext::CryptoContext( uint32_t ssrc, int roc, uint16_t seq_no,
-			        int key_deriv_rate,
+			        int64_t key_deriv_rate,
 				//enum encr_method encryption, 
 				uint8_t ealg,
 				//enum auth_method authentication,
@@ -254,14 +254,15 @@
 }
 
 /* used by the key derivation method */
-static void compute_iv( unsigned char * iv, uint64_t label, uint64_t index, int key_deriv_rate, unsigned char * master_salt ){
+static void compute_iv( unsigned char * iv, uint64_t label, uint64_t index, 
+			int64_t key_deriv_rate, unsigned char * master_salt ){
 
         uint64_t key_id;
 
         if( key_deriv_rate == 0 )
-                key_id = label << 48;
+	    key_id = label << 48;
         else
-                key_id = ((label << 48) || index / key_deriv_rate);
+	    key_id = ((label << 48) | (index / key_deriv_rate));
 
         //printf( "Key_ID: %llx\n", key_id );
 
@@ -295,7 +296,7 @@
 	compute_iv( iv, label, index, key_deriv_rate, master_salt );
 
 
-        aes = new AES( master_key, 16/*master_key_length*/ );
+        aes = new AES( master_key, master_key_length );
         aes->get_ctr_cipher_stream( k_e, n_e, iv );
 
    //     cerr << "Session Encryption Key: " << print_hex( k_e, n_e ) << endl;
@@ -304,7 +305,7 @@
 
         compute_iv( iv, label, index, key_deriv_rate, master_salt );
 
-        aes = new AES( master_key, 16/*master_key_length*/ );
+        aes = new AES( master_key, master_key_length );
         aes->get_ctr_cipher_stream( k_a, n_a, iv );
 
 //        cerr << "Session Authentication Key: " << print_hex( k_a, n_a ) << endl;
@@ -313,7 +314,7 @@
 
         compute_iv( iv, label, index, key_deriv_rate, master_salt );
 
-        aes = new AES( master_key, 16/*master_key_length*/ );
+        aes = new AES( master_key, master_key_length );
         aes->get_ctr_cipher_stream( k_s, n_s, iv );
 
 //        cerr << "Session Salt: " << print_hex( k_s, n_s ) << endl;

Modified: trunk/libminisip/source/rtp/RtpHeader.cxx
===================================================================
--- trunk/libminisip/source/rtp/RtpHeader.cxx	2006-06-04 08:07:22 UTC (rev 2611)
+++ trunk/libminisip/source/rtp/RtpHeader.cxx	2006-06-04 08:07:43 UTC (rev 2612)
@@ -1,165 +1,169 @@
-/*
- Copyright (C) 2004-2006 the Minisip Team
- 
- This library is free software; you can redistribute it and/or
- modify it under the terms of the GNU Lesser General Public
- License as published by the Free Software Foundation; either
- version 2.1 of the License, or (at your option) any later version.
- 
- This library is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- Lesser General Public License for more details.
- 
- You should have received a copy of the GNU Lesser General Public
- License along with this library; if not, write to the Free Software
- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
- */
-
-/* Copyright (C) 2004 
- *
- * Authors: Erik Eliasson <eliasson at it.kth.se>
- *          Johan Bilien <jobi at via.ecp.fr>
-*/
-
-#include <config.h>
-
-#include<libminisip/rtp/RtpHeader.h>
-#include<libminisip/rtp/RtpPacket.h>
-
-#ifdef DEBUG_OUTPUT
-#	include<iostream>
-#endif
-
-using namespace std;
-
-RtpHeader::RtpHeader(){
-	version=0;
-	extension=0;
-	CSRC_count=0;
-	marker=0;
-	payload_type=0;
-	sequence_number=0;
-	timestamp=0;
-	SSRC=0;
-
-#ifdef TCP_FRIENDLY
-	tcpFriendlyMode=false;
-	sending_timestamp=0;
-	rttestimate=0;
-#endif
-
-}
-
-void RtpHeader::setVersion(int v){
-	this->version = v;
-}
-
-void RtpHeader::setExtension(int x){
-	this->extension =  x;
-}
-
-void RtpHeader::setCSRCCount(int cc){
-	this->CSRC_count = cc;
-}
-
-void RtpHeader::setMarker(int m){
-	this->marker = m;
-}
-
-bool RtpHeader::getMarker(){
-	return marker == 1;
-}
-
-void RtpHeader::setPayloadType(int pt){
-	this->payload_type=pt;
-}
-
-int RtpHeader::getPayloadType(){
-	return payload_type;
-}
-
-
-void RtpHeader::setSeqNo(uint16_t seq_no){
-	this->sequence_number=seq_no;
-}
-
-uint16_t RtpHeader::getSeqNo(){
-	return sequence_number;
-}
-
-
-void RtpHeader::setTimestamp(uint32_t timestamp){
-	this->timestamp = timestamp;
-}
-
-uint32_t RtpHeader::getTimestamp(){
-	return timestamp;
-}
-
-void RtpHeader::setSSRC(uint32_t s){
-	this->SSRC = s;
-}
-
-uint32_t RtpHeader::getSSRC(){
-	return SSRC;
-}
-
-void RtpHeader::addCSRC(int c){
-	CSRC.push_back(c);
-}
-
-
-int RtpHeader::size(){
-	int s = 12+4*CSRC.size();
-	
-#ifdef TCP_FRIENDLY
-	s+=8; // 4(rtt)+4(sendts)
-#endif
-	return s;
-}
-
-char *RtpHeader::getBytes(){
-        uint8_t i;
-	char *ret = new char[size()];
-
-        ret[0] = ( ( version << 6 ) & 0xc0 ) |
-                 ( ( extension << 4 ) & 0x10 ) |
-                 ( ( CSRC_count & 0x0F ) );
-
-        ret[1] = ( ( marker << 7 ) & 0x80 ) |
-                 ( ( payload_type & 0x7F ) );
-
-        ret[2] = ( sequence_number >> 8 ) & 0xFF;
-        
-        ret[3] = ( sequence_number ) & 0xFF;
-        
-        ((uint32_t *)ret)[1] = hton32( timestamp );
-        ((uint32_t *)ret)[2] = hton32( SSRC );
-	
-	int i32=3; //index in the packet where we are adding headers. If
-	  	   //we are using tcp friendly mode we insert two extra 
-		   //32 bit headers and the CSRCs will not be in their
-		   //usual place.
-#ifdef TCP_FRIENDLY
-	if (tcpFriendlyMode){
-		((uint32_t *)ret)[i32++] = hton32(sending_timestamp);
-		((uint32_t *)ret)[i32++] = hton32(rttestimate);
-	}
-#endif
-	
-        for( i = 0; i < CSRC.size(); i++ )
-		((uint32_t *)ret)[i32+i]=hton32(CSRC[i]);
-        
-	return ret;
-}
-
-#ifdef DEBUG_OUTPUT
-void RtpHeader::printDebug(){
-	cerr << "\tversion: "<< version<<"\n\textension: "<< extension <<"\n\tCSRC count: "<< CSRC_count << "\n\tmarker: "<< marker << "\n\tpayload type: "<<payload_type <<"\n\tsequence number: "<<sequence_number << "\n\ttimestamp: "<<timestamp <<"\n\tSSRC: "<< SSRC << "\n"<< endl;
-
-	for (int i=0; i< CSRC_count; i++)
-		cerr << "\tCSRC "<<i+1 << ": "<<CSRC[i]<< endl;
-}
-#endif
-
+/*
+ Copyright (C) 2004-2006 the Minisip Team
+ 
+ This library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+ 
+ This library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ Lesser General Public License for more details.
+ 
+ You should have received a copy of the GNU Lesser General Public
+ License along with this library; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
+ */
+
+/* Copyright (C) 2004 
+ *
+ * Authors: Erik Eliasson <eliasson at it.kth.se>
+ *          Johan Bilien <jobi at via.ecp.fr>
+*/
+
+#include <config.h>
+
+#include<libminisip/rtp/RtpHeader.h>
+#include<libminisip/rtp/RtpPacket.h>
+
+#ifdef DEBUG_OUTPUT
+#	include<iostream>
+#endif
+
+using namespace std;
+
+RtpHeader::RtpHeader(){
+	version=0;
+	extension=0;
+	CSRC_count=0;
+	marker=0;
+	payload_type=0;
+	sequence_number=0;
+	timestamp=0;
+	SSRC=0;
+
+#ifdef TCP_FRIENDLY
+	tcpFriendlyMode=false;
+	sending_timestamp=0;
+	rttestimate=0;
+#endif
+
+}
+
+void RtpHeader::setVersion(int v){
+	this->version = v;
+}
+
+void RtpHeader::setExtension(int x){
+	this->extension =  x;
+}
+
+int RtpHeader::getExtension() {
+    return extension;
+}
+
+void RtpHeader::setCSRCCount(int cc){
+	this->CSRC_count = cc;
+}
+
+void RtpHeader::setMarker(int m){
+	this->marker = m;
+}
+
+bool RtpHeader::getMarker(){
+	return marker == 1;
+}
+
+void RtpHeader::setPayloadType(int pt){
+	this->payload_type=pt;
+}
+
+int RtpHeader::getPayloadType(){
+	return payload_type;
+}
+
+
+void RtpHeader::setSeqNo(uint16_t seq_no){
+	this->sequence_number=seq_no;
+}
+
+uint16_t RtpHeader::getSeqNo(){
+	return sequence_number;
+}
+
+
+void RtpHeader::setTimestamp(uint32_t timestamp){
+	this->timestamp = timestamp;
+}
+
+uint32_t RtpHeader::getTimestamp(){
+	return timestamp;
+}
+
+void RtpHeader::setSSRC(uint32_t s){
+	this->SSRC = s;
+}
+
+uint32_t RtpHeader::getSSRC(){
+	return SSRC;
+}
+
+void RtpHeader::addCSRC(int c){
+	CSRC.push_back(c);
+}
+
+
+int RtpHeader::size(){
+	int s = 12+4*CSRC.size();
+	
+#ifdef TCP_FRIENDLY
+	s+=8; // 4(rtt)+4(sendts)
+#endif
+	return s;
+}
+
+char *RtpHeader::getBytes(){
+        uint8_t i;
+	char *ret = new char[size()];
+
+        ret[0] = ( ( version << 6 ) & 0xc0 ) |
+                 ( ( extension << 4 ) & 0x10 ) |
+                 ( ( CSRC_count & 0x0F ) );
+
+        ret[1] = ( ( marker << 7 ) & 0x80 ) |
+                 ( ( payload_type & 0x7F ) );
+
+        ret[2] = ( sequence_number >> 8 ) & 0xFF;
+        
+        ret[3] = ( sequence_number ) & 0xFF;
+        
+        ((uint32_t *)ret)[1] = hton32( timestamp );
+        ((uint32_t *)ret)[2] = hton32( SSRC );
+	
+	int i32=3; //index in the packet where we are adding headers. If
+	  	   //we are using tcp friendly mode we insert two extra 
+		   //32 bit headers and the CSRCs will not be in their
+		   //usual place.
+#ifdef TCP_FRIENDLY
+	if (tcpFriendlyMode){
+		((uint32_t *)ret)[i32++] = hton32(sending_timestamp);
+		((uint32_t *)ret)[i32++] = hton32(rttestimate);
+	}
+#endif
+	
+        for( i = 0; i < CSRC.size(); i++ )
+		((uint32_t *)ret)[i32+i]=hton32(CSRC[i]);
+        
+	return ret;
+}
+
+#ifdef DEBUG_OUTPUT
+void RtpHeader::printDebug(){
+	cerr << "\tversion: "<< version<<"\n\textension: "<< extension <<"\n\tCSRC count: "<< CSRC_count << "\n\tmarker: "<< marker << "\n\tpayload type: "<<payload_type <<"\n\tsequence number: "<<sequence_number << "\n\ttimestamp: "<<timestamp <<"\n\tSSRC: "<< SSRC << "\n"<< endl;
+
+	for (int i=0; i< CSRC_count; i++)
+		cerr << "\tCSRC "<<i+1 << ": "<<CSRC[i]<< endl;
+}
+#endif
+

Modified: trunk/libminisip/source/rtp/RtpPacket.cxx
===================================================================
--- trunk/libminisip/source/rtp/RtpPacket.cxx	2006-06-04 08:07:22 UTC (rev 2611)
+++ trunk/libminisip/source/rtp/RtpPacket.cxx	2006-06-04 08:07:43 UTC (rev 2612)
@@ -1,181 +1,233 @@
-/*
- Copyright (C) 2004-2006 the Minisip Team
- 
- This library is free software; you can redistribute it and/or
- modify it under the terms of the GNU Lesser General Public
- License as published by the Free Software Foundation; either
- version 2.1 of the License, or (at your option) any later version.
- 
- This library is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- Lesser General Public License for more details.
- 
- You should have received a copy of the GNU Lesser General Public
- License along with this library; if not, write to the Free Software
- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
- */
-
-/* Copyright (C) 2004 
- *
- * Authors: Erik Eliasson <eliasson at it.kth.se>
- *          Johan Bilien <jobi at via.ecp.fr>
-*/
-
-#include<config.h>
-
-#include<stdio.h>
-
-#ifdef _MSC_VER
-#else
-#include<unistd.h>
-#endif
-
-#include<errno.h>
-#include<libminisip/rtp/RtpPacket.h>
-#include<libminisip/rtp/RtpHeader.h>
-
-#ifdef DEBUG_OUTPUT
-#include<iostream>
-#endif
-
-#include<libmutil/merror.h>
-#include<libmnetutil/UDPSocket.h>
-
-using namespace std;
-
-RtpPacket::RtpPacket(){
-	content_length=0;
-	content=NULL;	
-}
-
-RtpPacket::RtpPacket(unsigned char *content, int content_length, int seq_no, unsigned timestamp, unsigned ssrc):content_length(content_length){
-	header.setVersion(2);
-	header.setSeqNo(seq_no);
-	header.setTimestamp(timestamp);
-	header.SSRC = ssrc;
-	
-	if( content_length ){
-		this->content = new unsigned char[content_length];
-		memcpy(this->content, content, content_length);
-	}
-	else
-		this->content = NULL;
-}
-
-RtpPacket::RtpPacket(RtpHeader hdr, unsigned char *content, int content_length): header(hdr){
-	this->content_length = content_length;
-	
-	if( content_length ){
-		this->content = new unsigned char[content_length];
-		memcpy(this->content, content, content_length);
-	}
-	else
-		this->content = NULL;
-
-	header.setVersion(2);
-	
-
-}
-
-RtpHeader &RtpPacket::getHeader(){
-	return header;
-}
-
-
-RtpPacket::~RtpPacket(){
-	if (content!=NULL)
-		delete [] content;
-}
-
-void RtpPacket::sendTo(UDPSocket &udp_sock, IPAddress &to_addr, int port){
-	char *bytes = getBytes();
-	udp_sock.sendTo(to_addr, port, bytes, size());
-	delete [] bytes;
-}
-
-RtpPacket *RtpPacket::readPacket(UDPSocket &rtp_socket, int timeout){
-#define UDP_SIZE 65536
-	int i;
-	uint8_t buf[UDP_SIZE];
-        uint8_t j;
-        uint8_t cc;
-//	memset( buf, '\0', 2048 );
-	
-	i = rtp_socket.recv( (char *)buf, UDP_SIZE );
-
-	if( i < 0 ){
-#ifdef DEBUG_OUTPUT
-		merror("recvfrom:");
-#endif
-		return NULL;
-	}
-
-        if( i < 12 ){
-                /* too small to contain an RTP header */
-                return NULL;
-        }
-
-        cc = buf[0] & 0x0F;
-        if( i < 12 + cc * 4 ){
-                /* too small to contain an RTP header with cc CCSRC */
-                return NULL;
-        }
-	
-	RtpHeader hdr;
-	hdr.setVersion( ( buf[0] >> 6 ) & 0x03 );
-	hdr.setExtension(  ( buf[0] >> 4 ) & 0x01 );
-	hdr.setCSRCCount( cc );
-	hdr.setMarker( ( buf[1] >> 7 ) & 0x01  );
-	hdr.setPayloadType( buf[1] & 0x7F );
-	
-	hdr.setSeqNo( ( ((uint16_t)buf[2]) << 8 ) | buf[3] );
-	cerr << "GOT SEQN" << hdr.getSeqNo() << endl;
-	hdr.setTimestamp( U32_AT( buf + 4 ) );
-	hdr.setSSRC( U32_AT( buf + 8 ) );
-
-	for( j = 0 ; j < cc ; j++ )
-		hdr.addCSRC( U32_AT( buf + 12 + j*4 ) );
-                
-	int datalen = i - 12 - cc*4;
-	
-	RtpPacket * rtp = new RtpPacket(hdr, (unsigned char *)&buf[12+4*cc], datalen);
-	
-	return rtp;
-}
-
-char *RtpPacket::getBytes(){
-	char *ret = new char[header.size()+content_length];
-	
-	char *hdr = header.getBytes();
-	
-	memcpy(ret,hdr, header.size());
-	delete [] hdr;
-
-	memcpy(&ret[header.size()], content, content_length);
-	return ret;
-}
-
-int RtpPacket::size(){
-	return header.size()+content_length;
-}
-
-unsigned char *RtpPacket::getContent(){
-	return content;
-}
-
-int RtpPacket::getContentLength(){
-	return content_length;
-}
-
-#ifdef DEBUG_OUTPUT
-void RtpPacket::printDebug(){
-	cerr << "_RTP_Header_"<< endl;
-	header.printDebug();
-	cerr <<"_Content_"<< endl;
-	cerr <<"\tContent length: "<< content_length<< endl;
-	
-}
-#endif
-
-
+/*
+  Copyright (C) 2004-2006 the Minisip Team
+ 
+  This library is free software; you can redistribute it and/or
+  modify it under the terms of the GNU Lesser General Public
+  License as published by the Free Software Foundation; either
+  version 2.1 of the License, or (at your option) any later version.
+ 
+  This library is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+  Lesser General Public License for more details.
+ 
+  You should have received a copy of the GNU Lesser General Public
+  License along with this library; if not, write to the Free Software
+  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
+*/
+
+/* Copyright (C) 2004 
+ *
+ * Authors: Erik Eliasson <eliasson at it.kth.se>
+ *          Johan Bilien <jobi at via.ecp.fr>
+ */
+
+#include<config.h>
+
+#include<stdio.h>
+
+#ifdef _MSC_VER
+#else
+#include<unistd.h>
+#endif
+
+#include<errno.h>
+#include<libminisip/rtp/RtpPacket.h>
+#include<libminisip/rtp/RtpHeader.h>
+
+#ifdef DEBUG_OUTPUT
+#include<iostream>
+#endif
+
+#include<libmutil/merror.h>
+#include<libmnetutil/UDPSocket.h>
+
+using namespace std;
+
+RtpPacket::RtpPacket(){
+    content_length=0;
+    content=NULL;	
+}
+
+RtpPacket::RtpPacket(unsigned char *content, int content_length, 
+		     int seq_no, unsigned timestamp, unsigned ssrc):
+    content_length(content_length){
+
+    header.setVersion(2);
+    header.setSeqNo(seq_no);
+    header.setTimestamp(timestamp);
+    header.SSRC = ssrc;
+	
+    if( content_length ){
+	this->content = new unsigned char[content_length];
+	memcpy(this->content, content, content_length);
+    }
+    else
+	this->content = NULL;
+}
+
+RtpPacket::RtpPacket(RtpHeader hdr, unsigned char *content, int content_length): header(hdr) {
+
+    extensionLength = 0;
+    extensionHeader = NULL;
+
+    /*
+     * Check if packet contains an extension header. If yes
+     * set pointer to extension header, compute length and
+     * adjust content pointer and content_length
+     */
+    if (header.getExtension() && content_length >= 4) {
+	extensionLength = 4;
+	content_length -= 4;	// minimum size of extension header
+
+	short tmp = *((short *)(content+2));
+	ntoh16(tmp);
+	tmp *= 4;		// ext. header length is in words (4 bytes)
+	extensionLength += tmp;
+	content_length -= tmp;
+	
+	if (content_length >= 0) {
+	    extensionHeader = content;
+	}
+    }
+    this->content_length = content_length;
+	
+    if( content_length > 0 ){
+	this->content = new unsigned char[content_length];
+	memcpy(this->content, content+extensionLength, content_length);
+    }
+    else
+	this->content = NULL;
+
+    header.setVersion(2);
+}
+
+void RtpPacket::setExtHeader(unsigned char* data, int length) {
+
+    if (data == NULL || length == 0) {
+	return;
+    }
+    // Need to prepend extension header to packet payload. Get a bigger
+    // buffer.
+    unsigned char* cp = new unsigned char[content_length + length];
+    memcpy(cp, data, length);
+
+    if (content != NULL) {
+	memcpy(cp+length, content, content_length);
+	delete [] content;
+    }
+    content = cp;
+    content_length += length;
+    header.setExtension(1);
+}
+
+RtpHeader &RtpPacket::getHeader(){
+    return header;
+}
+
+RtpPacket::~RtpPacket(){
+    if (content!=NULL)
+	delete [] content;
+}
+
+void RtpPacket::sendTo(UDPSocket &udp_sock, IPAddress &to_addr, int port){
+    char *bytes = getBytes();
+    udp_sock.sendTo(to_addr, port, bytes, size());
+    delete [] bytes;
+}
+
+RtpPacket *RtpPacket::readPacket(UDPSocket &rtp_socket, int timeout){
+#define UDP_SIZE 65536
+    int i;
+    uint8_t buf[UDP_SIZE];
+    uint8_t j;
+    uint8_t cc;
+//	memset( buf, '\0', 2048 );
+	
+    i = rtp_socket.recv( (char *)buf, UDP_SIZE );
+
+    if( i < 0 ){
+#ifdef DEBUG_OUTPUT
+	merror("recvfrom:");
+#endif
+	return NULL;
+    }
+
+    if( i < 12 ){
+	/* too small to contain an RTP header */
+	return NULL;
+    }
+
+    cc = buf[0] & 0x0F;
+    if( i < 12 + cc * 4 ){
+	/* too small to contain an RTP header with cc CSRC */
+	return NULL;
+    }
+	
+    RtpHeader hdr;
+    hdr.setVersion( ( buf[0] >> 6 ) & 0x03 );
+    hdr.setExtension(  ( buf[0] >> 4 ) & 0x01 );
+    hdr.setCSRCCount( cc );
+    hdr.setMarker( ( buf[1] >> 7 ) & 0x01  );
+    hdr.setPayloadType( buf[1] & 0x7F );
+	
+    hdr.setSeqNo( ( ((uint16_t)buf[2]) << 8 ) | buf[3] );
+    cerr << "GOT SEQN" << hdr.getSeqNo() << endl;
+
+    int tmp = *((int *)(buf + 4));
+    ntoh32(tmp);
+    hdr.setTimestamp(tmp);
+
+    tmp = *((int *)(buf + 8));
+    ntoh32(tmp);
+    hdr.setSSRC(tmp);
+
+    for( j = 0 ; j < cc ; j++ ) {
+	tmp = *((int *)(buf + 12 + j*4));
+	ntoh32(tmp);
+	hdr.setSSRC(tmp);
+    }
+    int datalen = i - 12 - cc*4;
+	
+    RtpPacket * rtp = new RtpPacket(hdr, (unsigned char *)&buf[12+4*cc], datalen);
+	
+    return rtp;
+}
+
+char *RtpPacket::getBytes(){
+
+    int hdrSize = header.size();
+    char *ret = new char[hdrSize+content_length];
+	
+    char *hdr = header.getBytes();
+	
+    memcpy(ret, hdr, hdrSize);
+    delete [] hdr;
+
+    memcpy(&ret[hdrSize], content, content_length);
+    return ret;
+}
+
+int RtpPacket::size(){
+    return header.size()+content_length;
+}
+
+unsigned char *RtpPacket::getContent(){
+    return content;
+}
+
+int RtpPacket::getContentLength(){
+    return content_length;
+}
+
+#ifdef DEBUG_OUTPUT
+void RtpPacket::printDebug(){
+    cerr << "_RTP_Header_"<< endl;
+    header.printDebug();
+    cerr <<"_Content_"<< endl;
+    cerr <<"\tContent length: "<< content_length<< endl;
+	
+}
+#endif
+
+

Modified: trunk/libminisip/source/rtp/SRtpPacket.cxx
===================================================================
--- trunk/libminisip/source/rtp/SRtpPacket.cxx	2006-06-04 08:07:22 UTC (rev 2611)
+++ trunk/libminisip/source/rtp/SRtpPacket.cxx	2006-06-04 08:07:43 UTC (rev 2612)
@@ -1,271 +1,277 @@
-/*
- Copyright (C) 2004-2006 the Minisip Team
- 
- This library is free software; you can redistribute it and/or
- modify it under the terms of the GNU Lesser General Public
- License as published by the Free Software Foundation; either
- version 2.1 of the License, or (at your option) any later version.
- 
- This library is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- Lesser General Public License for more details.
- 
- You should have received a copy of the GNU Lesser General Public
- License along with this library; if not, write to the Free Software
- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
- */
-
-/* Copyright (C) 2004 
- *
- * Authors: Israel Abad <i_abad at terra.es>
- * 	    Erik Eliasson <eliasson at it.kth.se>
- *          Johan Bilien <jobi at via.ecp.fr>
-*/
-
-#include<config.h>
-
-#include<iostream>
-
-#ifdef LINUX
-#	include<netinet/in.h>
-#	include<sys/socket.h>
-#	include<sys/select.h>
-#	include <err.h>
-#endif
-
-#ifdef WIN32
-#	include<winsock2.h>
-#endif
-
-#include<stdio.h>
-
-#ifdef _MSC_VER
-
-#else
-#include<unistd.h>
-#endif
-
-
-#include<errno.h>
-
-#include<libminisip/rtp/SRtpPacket.h>
-#include<libminisip/rtp/CryptoContext.h>
-
-#include<libmutil/print_hex.h>
-#include<libmutil/merror.h>
-#include<libmutil/MemObject.h>
-
-#ifdef DEBUG_OUTPUT
-#include<iostream>
-#endif
-
-using namespace std;
-
-
-void SRtpPacket::protect( MRef<CryptoContext *> scontext ){
-	
-	/* Encrypt the packet */
-	uint64_t index = 
-		((uint64_t)scontext->get_roc() << 16) |
-		(uint64_t)(getHeader().getSeqNo());
-	scontext->rtp_encrypt( this, index );
-	encrypted = true;
-
-	/* Compute MAC */
-	tag_length = scontext->get_tag_length();
-	tag = new unsigned char[ tag_length ];
-	
-	scontext->rtp_authenticate( this, scontext->get_roc(), tag );
-
-	/* Update the ROC if necessary */
-	if( getHeader().getSeqNo() == 0xFFFF )
-		scontext->set_roc( scontext->get_roc() + 1 );
-}
-
-int SRtpPacket::unprotect( MRef<CryptoContext *> scontext ){
-	if( !scontext ){
-		// It's probably an RtpPacket
-		return 0;
-	}
-
-	content_length -= scontext->get_tag_length();
-	content_length -= scontext->get_mki_length();
-
-	tag = content + content_length;
-	mki = content + content_length + scontext->get_tag_length();
-
-	tag_length = scontext->get_tag_length();
-	mki_length = scontext->get_mki_length();
-	
-	/* Guess the index */
-	uint64_t guessed_index = 
-		scontext->guess_index( getHeader().getSeqNo() );
-
-	/* Replay control */
-	if( !scontext->check_replay( getHeader().getSeqNo() ) ){
-#ifdef DEBUG_OUTPUT
-		cerr << "replay check failed" <<endl;
-#endif
-		tag = NULL;
-		mki = NULL;
-		return 1;
-	}
-
-	/* Authentication control */
-	int length = get_tag_length();
-	
-	unsigned char * mac = new unsigned char[length];
-	scontext->rtp_authenticate( this, (uint32_t)( guessed_index >> 16 ), mac );
-	//cerr << "MAC computed: " << print_hex( mac, 4 )<< endl;
-	for( int i = 0; i < length; i++ ){
-		if( tag[i] != mac[i] )
-		{
-#ifdef DEBUG_OUTPUT
-			cerr << "authentication failed in stream: " << getHeader().getSSRC() <<endl;
-#endif
-			tag = NULL;
-			mki = NULL;
-			return 1;
-		}
-	}
-
-	delete [] mac;
-
-	/* Decrypt the content */
-	scontext->rtp_encrypt( this, guessed_index );
-	encrypted = false;
-
-	/* Update the Crypto-context */
-	scontext->update( getHeader().getSeqNo() );
-
-	/* don't delete the tag and mki */
-	tag = NULL;
-	mki = NULL;
-
-	return 0;
-}
-
-SRtpPacket::SRtpPacket(){
-        content_length=0;
-        content=NULL;
-	mki=NULL;
-	mki_length=0;
-	tag=NULL;
-	tag_length=0;
-}
-
-SRtpPacket::SRtpPacket( unsigned char *content, int content_length, int seq_no, unsigned timestamp, unsigned ssrc):RtpPacket( content, content_length, seq_no, timestamp, ssrc ),tag(NULL),tag_length(0),mki(NULL),mki_length(0){
-
-}
-
-SRtpPacket::SRtpPacket(RtpHeader hdr, unsigned char *content, int content_length, unsigned char * tag, int tag_length, unsigned char * mki, int mki_length):RtpPacket(hdr,content, content_length ), encrypted(true),tag_length(tag_length),mki_length(mki_length){
-
-	if(tag_length){
-		this->tag = new unsigned char[tag_length];
-		memcpy( this->tag, tag, tag_length );
-	}
-	else
-		this->tag = NULL;
-	
-	if(mki_length){
-		this->mki = new unsigned char[mki_length];
-		memcpy( this->mki, mki, mki_length );
-	}
-	else
-		this->mki = NULL;
-}
-
-
-SRtpPacket::~SRtpPacket(){
-	if( mki )
-		delete [] mki;
-	if( tag )
-		delete [] tag;
-}
-
-
-SRtpPacket *SRtpPacket::readPacket(UDPSocket &srtp_socket, int timeout){
-#define UDP_SIZE 65536
-        int i;
-        uint8_t buf[UDP_SIZE];
-        uint8_t j;
-        uint8_t cc;
-	//memset( buf, '\0', UDP_SIZE );
-
-        i = srtp_socket.recv( (char*)buf, UDP_SIZE );
-	
-        if( i < 0 ){
-#ifdef DEBUG_OUTPUT
-		merror("recvfrom:");
-#endif
-		return NULL;
-        }
-
-        if( i < 12 ){
-                /* too small to contain an RTP header */
-                return NULL;
-        }
-
-        cc = buf[0] & 0x0F;
-        if( i < 12 + cc * 4 ){
-                /* too small to contain an RTP header with cc CCSRC */
-                return NULL;
-        }
-
-        RtpHeader hdr;
-        hdr.setVersion( ( buf[0] >> 6 ) & 0x03 );
-        hdr.setExtension(  ( buf[0] >> 4 ) & 0x01 );
-        hdr.setCSRCCount( cc );
-        hdr.setMarker( ( buf[1] >> 7 ) & 0x01  );
-        hdr.setPayloadType( buf[1] & 0x7F );
-
-        hdr.setSeqNo( ( ((uint16_t)buf[2]) << 8 ) | buf[3] );
-        hdr.setTimestamp( U32_AT( buf + 4 ) );
-        hdr.setSSRC( U32_AT( buf + 8 ) );
-
-        for( j = 0 ; j < cc ; j++ )
-                hdr.addCSRC( U32_AT( buf + 12 + j*4 ) );
-        
-	int datalen = i - 12 - cc*4;
-
-	unsigned char *data = (unsigned char *)&buf[ 12 + 4*cc ];
-
-	SRtpPacket *srtp = new SRtpPacket( hdr, data, datalen, NULL, 0, NULL, 0 );
-        
-	return srtp;
-}
-
-char *SRtpPacket::getBytes(){
-	char * ret;
-	ret = new char[ size() ];
-	
-        char * hdr;
-	hdr = header.getBytes();
-
-        memcpy( ret, hdr, header.size() );
-        delete [] hdr;
-
-        memcpy( &ret[header.size()], content, content_length );
-	memcpy( &ret[header.size() + content_length], tag, tag_length );
-	memcpy( &ret[header.size() + content_length + tag_length],
-			mki, mki_length );
-
-        return ret;
-}
-
-unsigned char *SRtpPacket::get_tag(){
-        return tag;
-}
-
-unsigned int SRtpPacket::get_tag_length(){
-	return tag_length;
-}
-
-void SRtpPacket::set_tag(unsigned char *tag){
-        this->tag=tag;
-}
-
-int SRtpPacket::size(){
-	return header.size() + content_length + tag_length + mki_length;
-}
+/*
+  Copyright (C) 2004-2006 the Minisip Team
+ 
+  This library is free software; you can redistribute it and/or
+  modify it under the terms of the GNU Lesser General Public
+  License as published by the Free Software Foundation; either
+  version 2.1 of the License, or (at your option) any later version.
+ 
+  This library is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+  Lesser General Public License for more details.
+ 
+  You should have received a copy of the GNU Lesser General Public
+  License along with this library; if not, write to the Free Software
+  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
+*/
+
+/* Copyright (C) 2004 
+ *
+ * Authors: Israel Abad <i_abad at terra.es>
+ * 	    Erik Eliasson <eliasson at it.kth.se>
+ *          Johan Bilien <jobi at via.ecp.fr>
+ */
+
+#include<config.h>
+
+#include<iostream>
+
+#ifdef LINUX
+#	include<netinet/in.h>
+#	include<sys/socket.h>
+#	include<sys/select.h>
+#	include <err.h>
+#endif
+
+#ifdef WIN32
+#	include<winsock2.h>
+#endif
+
+#include<stdio.h>
+
+#ifdef _MSC_VER
+
+#else
+#include<unistd.h>
+#endif
+
+
+#include<errno.h>
+
+#include<libminisip/rtp/SRtpPacket.h>
+#include<libminisip/rtp/CryptoContext.h>
+
+#include<libmutil/print_hex.h>
+#include<libmutil/merror.h>
+#include<libmutil/MemObject.h>
+
+#ifdef DEBUG_OUTPUT
+#include<iostream>
+#endif
+
+using namespace std;
+
+
+void SRtpPacket::protect( MRef<CryptoContext *> scontext ){
+	
+    /* Encrypt the packet */
+    uint64_t index = 
+	((uint64_t)scontext->get_roc() << 16) |
+	(uint64_t)(getHeader().getSeqNo());
+    scontext->rtp_encrypt( this, index );
+    encrypted = true;
+
+    /* Compute MAC */
+    tag_length = scontext->get_tag_length();
+    tag = new unsigned char[ tag_length ];
+	
+    scontext->rtp_authenticate( this, scontext->get_roc(), tag );
+
+    /* Update the ROC if necessary */
+    if( getHeader().getSeqNo() == 0xFFFF )
+	scontext->set_roc( scontext->get_roc() + 1 );
+}
+
+int SRtpPacket::unprotect( MRef<CryptoContext *> scontext ){
+    if( !scontext ){
+	// It's probably an RtpPacket
+	return 0;
+    }
+
+    content_length -= scontext->get_tag_length();
+    content_length -= scontext->get_mki_length();
+
+    tag = content + content_length;
+    mki = content + content_length + scontext->get_tag_length();
+
+    tag_length = scontext->get_tag_length();
+    mki_length = scontext->get_mki_length();
+	
+    /* Guess the index */
+    uint64_t guessed_index = 
+	scontext->guess_index( getHeader().getSeqNo() );
+
+    /* Replay control */
+    if( !scontext->check_replay( getHeader().getSeqNo() ) ){
+#ifdef DEBUG_OUTPUT
+	cerr << "replay check failed" <<endl;
+#endif
+	tag = NULL;
+	mki = NULL;
+	return 1;
+    }
+
+    /* Authentication control */
+    int length = get_tag_length();
+	
+    unsigned char * mac = new unsigned char[length];
+    scontext->rtp_authenticate( this, (uint32_t)( guessed_index >> 16 ), mac );
+    //cerr << "MAC computed: " << print_hex( mac, 4 )<< endl;
+    for( int i = 0; i < length; i++ ){
+	if( tag[i] != mac[i] )
+	{
+#ifdef DEBUG_OUTPUT
+	    cerr << "authentication failed in stream: " << getHeader().getSSRC() <<endl;
+#endif
+	    tag = NULL;
+	    mki = NULL;
+	    return 1;
+	}
+    }
+
+    delete [] mac;
+
+    /* Decrypt the content */
+    scontext->rtp_encrypt( this, guessed_index );
+    encrypted = false;
+
+    /* Update the Crypto-context */
+    scontext->update( getHeader().getSeqNo() );
+
+    /* don't delete the tag and mki */
+    tag = NULL;
+    mki = NULL;
+
+    return 0;
+}
+
+SRtpPacket::SRtpPacket(){
+    content_length=0;
+    content=NULL;
+    mki=NULL;
+    mki_length=0;
+    tag=NULL;
+    tag_length=0;
+}
+
+SRtpPacket::SRtpPacket( unsigned char *content, int content_length, 
+			int seq_no, unsigned timestamp, unsigned ssrc):
+    RtpPacket( content, content_length, seq_no, timestamp, ssrc ), tag(NULL), tag_length(0), mki(NULL), mki_length(0){
+
+}
+
+SRtpPacket::SRtpPacket(RtpHeader hdr, unsigned char *content, int content_length,
+		       unsigned char * tag, int tag_length, 
+		       unsigned char * mki, int mki_length):
+    RtpPacket(hdr, content, content_length ), encrypted(true), tag_length(tag_length), mki_length(mki_length){
+
+    if(tag_length){
+	this->tag = new unsigned char[tag_length];
+	memcpy( this->tag, tag, tag_length );
+    }
+    else
+	this->tag = NULL;
+	
+    if(mki_length){
+	this->mki = new unsigned char[mki_length];
+	memcpy( this->mki, mki, mki_length );
+    }
+    else
+	this->mki = NULL;
+}
+
+
+SRtpPacket::~SRtpPacket(){
+    if( mki )
+	delete [] mki;
+    if( tag )
+	delete [] tag;
+}
+
+
+SRtpPacket *SRtpPacket::readPacket(UDPSocket &srtp_socket, MRef<IPAddress *> &from, int timeout) {
+#define UDP_SIZE 65536
+    int i;
+    uint8_t buf[UDP_SIZE];
+    uint8_t j;
+    uint8_t cc;
+    //memset( buf, '\0', UDP_SIZE );
+    int32_t port;
+
+    i = srtp_socket.recvFrom((char*)buf, UDP_SIZE, from, port);
+	
+    if( i < 0 ){
+#ifdef DEBUG_OUTPUT
+	merror("recvfrom:");
+#endif
+	return NULL;
+    }
+
+    if( i < 12 ){
+	/* too small to contain an RTP header */
+	return NULL;
+    }
+
+    cc = buf[0] & 0x0F;
+    if( i < 12 + cc * 4 ){
+	/* too small to contain an RTP header with cc CCSRC */
+	return NULL;
+    }
+
+    RtpHeader hdr;
+    hdr.setVersion( ( buf[0] >> 6 ) & 0x03 );
+    hdr.setExtension(  ( buf[0] >> 4 ) & 0x01 );
+    hdr.setCSRCCount( cc );
+    hdr.setMarker( ( buf[1] >> 7 ) & 0x01  );
+    hdr.setPayloadType( buf[1] & 0x7F );
+
+    hdr.setSeqNo( ( ((uint16_t)buf[2]) << 8 ) | buf[3] );
+    hdr.setTimestamp( U32_AT( buf + 4 ) );
+    hdr.setSSRC( U32_AT( buf + 8 ) );
+
+    for( j = 0 ; j < cc ; j++ )
+	hdr.addCSRC( U32_AT( buf + 12 + j*4 ) );
+        
+    int datalen = i - 12 - cc*4;
+
+    unsigned char *data = (unsigned char *)&buf[ 12 + 4*cc ];
+
+    SRtpPacket *srtp = new SRtpPacket( hdr, data, datalen, NULL, 0, NULL, 0 );
+        
+    return srtp;
+}
+
+char *SRtpPacket::getBytes(){
+    char * ret;
+    ret = new char[ size() ];
+	
+    char * hdr;
+    hdr = header.getBytes();
+
+    memcpy( ret, hdr, header.size() );
+    delete [] hdr;
+
+    memcpy( &ret[header.size()], content, content_length );
+    memcpy( &ret[header.size() + content_length], tag, tag_length );
+    memcpy( &ret[header.size() + content_length + tag_length],
+	    mki, mki_length );
+
+    return ret;
+}
+
+unsigned char *SRtpPacket::get_tag(){
+    return tag;
+}
+
+unsigned int SRtpPacket::get_tag_length(){
+    return tag_length;
+}
+
+void SRtpPacket::set_tag(unsigned char *tag){
+    this->tag=tag;
+}
+
+int SRtpPacket::size(){
+    return header.size() + content_length + tag_length + mki_length;
+}



More information about the Minisip-devel mailing list