r3475 - in trunk/libmsip: . source
mikma at minisip.org
mikma at minisip.org
Sat Nov 17 23:51:39 CET 2007
Author: mikma
Date: 2007-11-17 23:51:39 +0100 (Sat, 17 Nov 2007)
New Revision: 3475
Added:
trunk/libmsip/source/SipTransport.cxx
trunk/libmsip/source/SipTransport.h
trunk/libmsip/source/SipTransportUdp.cxx
trunk/libmsip/source/SipTransportUdp.h
Modified:
trunk/libmsip/Makefile.am
trunk/libmsip/source/SipLayerTransport.cxx
trunk/libmsip/source/SipLayerTransport.h
trunk/libmsip/source/SipSocketServer.h
Log:
Refactor SIP transport protocols.
Step 1, SipTransport, SipTransportRegistry, and SipTransportUdp.
Modified: trunk/libmsip/Makefile.am
===================================================================
--- trunk/libmsip/Makefile.am 2007-11-17 19:09:51 UTC (rev 3474)
+++ trunk/libmsip/Makefile.am 2007-11-17 22:51:39 UTC (rev 3475)
@@ -41,6 +41,10 @@
source/SipSMCommand.cxx \
source/SipCommandString.cxx \
source/SipTimers.cxx \
+ source/SipTransport.h \
+ source/SipTransport.cxx \
+ source/SipTransportUdp.h \
+ source/SipTransportUdp.cxx \
source/SipMessageContentIM.cxx \
source/SipMessageContentFactory.cxx \
source/SipMessageContentMime.cxx \
Modified: trunk/libmsip/source/SipLayerTransport.cxx
===================================================================
--- trunk/libmsip/source/SipLayerTransport.cxx 2007-11-17 19:09:51 UTC (rev 3474)
+++ trunk/libmsip/source/SipLayerTransport.cxx 2007-11-17 22:51:39 UTC (rev 3475)
@@ -58,6 +58,7 @@
#include<libmsip/SipCommandString.h>
#include"SipCommandDispatcher.h"
#include<libmsip/SipHeaderFrom.h>
+#include"SipTransport.h"
#include<cctype>
#include<string>
@@ -533,7 +534,7 @@
string srv;
uint16_t port = 0;
- if( transport == "TLS" || transport == "tls") { srv = "_sips._tls"; }
+ if( transport == "TLS" || transport == "tls") { srv = "_sips._tcp"; }
else if( transport == "TCP" || transport == "tcp") { srv = "_sip._tcp"; }
else { //if( trans == "UDP" || trans == "udp") {
srv = "_sip._udp";
@@ -1203,56 +1204,6 @@
}
-/**
- *
- * @param externalPort If the application wishes to override what port
- * should be reported for the socket it is possible to specify such a port
- * number here. This can be used for example to implement support for
- * passing NATs with the help of a STUN server.
- */
-MRef<SipSocketServer *> SipLayerTransport::createUdpServer( bool ipv6, const string &ipString, int32_t prefPort, int32_t externalPort)
-{
- int32_t port = prefPort;
- int triesLeft=10;
- MRef<SipSocketServer *> server;
- bool fail;
-
- do {
- fail=false;
- try {
-
- MRef<DatagramSocket *> sock = new UDPSocket( port, ipv6 );
- server = new DatagramSocketServer( this, sock );
-
- // IPv6 doesn't need different external udp port
- // since it never is NATed.
- if( !ipv6 && externalPort ){
- server->setExternalPort( externalPort );
- }
- server->setExternalIp( ipString );
-
-
- } catch(const BindFailed &bf){
- fail=true;
-
- // If the port is already in use, try random port number in the range 2048 to 63488
- port = rand()%(0xFFFF-4096) + 2048;
- triesLeft--;
- if (!triesLeft)
- throw;
-
- }
- }while(fail);
-
- if (externalPort>0)
- contactUdpPort=externalPort;
- else
- contactUdpPort=port;
-
-
- return server;
-}
-
MRef<SipSocketServer *> SipLayerTransport::createTcpServer( bool ipv6, const string &ipString, int32_t prefPort)
{
MRef<ServerSocket *> sock;
@@ -1321,13 +1272,22 @@
else
ipString = config->localIpString;
*/
+ MRef<SipTransport*> udp =
+ SipTransportRegistry::getInstance()->findTransport("udp");
- server = createUdpServer( false, ipString, localUdpPort, externalContactUdpPort );
+ server = udp->createServer( this, false, ipString, localUdpPort );
+
+ if( externalContactUdpPort ){
+ server->setExternalPort( externalContactUdpPort );
+ }
addServer( server );
+ contactUdpPort = server->getExternalPort();
if( /*config->localIp6String*/ ip6String != "" ){
MRef<SipSocketServer *> server6;
- server6 = createUdpServer( true, /*config->localIp6String*/ ip6String,localUdpPort,externalContactUdpPort );
+ server6 = udp->createServer( this, true, ip6String,localUdpPort );
+ // IPv6 doesn't need different external udp port
+ // since it never is NATed.
addServer( server6 );
}
}
Modified: trunk/libmsip/source/SipLayerTransport.h
===================================================================
--- trunk/libmsip/source/SipLayerTransport.h 2007-11-17 19:09:51 UTC (rev 3474)
+++ trunk/libmsip/source/SipLayerTransport.h 2007-11-17 22:51:39 UTC (rev 3475)
@@ -146,7 +146,6 @@
int contactUdpPort;
int contactTcpPort;
int contactTlsPort;
- MRef<SipSocketServer *> createUdpServer( bool ipv6, const std::string &ipStrinag, int32_t prefPort, int32_t externalPort );
MRef<SipSocketServer *> createTcpServer( bool ipv6, const std::string &ipString, int32_t prefPort );
MRef<SipSocketServer *> createTlsServer( bool ipv6, const std::string &ipString, int32_t prefPort, MRef<CertificateChain *> certChain, MRef<CertificateSet *> cert_db );
Modified: trunk/libmsip/source/SipSocketServer.h
===================================================================
--- trunk/libmsip/source/SipSocketServer.h 2007-11-17 19:09:51 UTC (rev 3474)
+++ trunk/libmsip/source/SipSocketServer.h 2007-11-17 22:51:39 UTC (rev 3475)
@@ -32,6 +32,7 @@
#include<libmnetutil/Socket.h>
#include<libmnetutil/ServerSocket.h>
#include<libmnetutil/SocketServer.h>
+#include<libmnetutil/DatagramSocket.h>
#include<libmutil/Thread.h>
class SipLayerTransport;
Added: trunk/libmsip/source/SipTransport.cxx
===================================================================
--- trunk/libmsip/source/SipTransport.cxx (rev 0)
+++ trunk/libmsip/source/SipTransport.cxx 2007-11-17 22:51:39 UTC (rev 3475)
@@ -0,0 +1,64 @@
+/*
+ Copyright (C) 2006-2007 Mikael Magnusson
+
+ 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
+*/
+
+/*
+ * Authors: Mikael Magnusson <mikma at users.sourceforge.net>
+*/
+
+#include<config.h>
+
+#include"SipTransport.h"
+#include"SipTransportUdp.h"
+
+using namespace std;
+
+SipTransport::SipTransport(): MPlugin(){
+}
+
+SipTransport::SipTransport( MRef<Library *> lib ): MPlugin( lib ){
+}
+
+SipTransportRegistry::SipTransportRegistry(){
+ registerPlugin( new SipTransportUdp( NULL ) );
+}
+
+MRef<SipTransport*> SipTransportRegistry::findTransport( string protocol, bool secure ) const{
+ list< MRef<MPlugin*> >::const_iterator iter;
+ list< MRef<MPlugin*> >::const_iterator stop = plugins.end();
+
+ for( iter = plugins.begin(); iter != stop; iter++ ){
+ MRef<MPlugin*> plugin = *iter;
+
+ if( !plugin )
+ continue;
+
+ MRef<SipTransport*> transport =
+ dynamic_cast<SipTransport*>( *plugin );
+
+ if( !transport )
+ continue;
+
+ if( transport->isSecure() == secure &&
+ transport->getProtocol() == protocol ){
+ mdbg << "SipTransport: tranport found!!! = " << protocol << endl;
+ return transport;
+ }
+ }
+
+ return NULL;
+}
Property changes on: trunk/libmsip/source/SipTransport.cxx
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Name: svn:eol-style
+ native
Added: trunk/libmsip/source/SipTransport.h
===================================================================
--- trunk/libmsip/source/SipTransport.h (rev 0)
+++ trunk/libmsip/source/SipTransport.h 2007-11-17 22:51:39 UTC (rev 3475)
@@ -0,0 +1,74 @@
+/*
+ Copyright (C) 2006-2007 Mikael Magnusson
+
+ 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
+*/
+
+/*
+ * Authors: Mikael Magnusson <mikma at users.sourceforge.net>
+*/
+
+#ifndef SipTransport_H
+#define SipTransport_H
+
+#include<libmsip/libmsip_config.h>
+
+#include<libmutil/MemObject.h>
+#include<libmutil/MPlugin.h>
+#include<libmutil/MSingleton.h>
+
+#include<libmnetutil/StreamSocket.h>
+#include<libmcrypto/cert.h>
+
+#include"SipSocketServer.h"
+#include"SipLayerTransport.h"
+
+/**
+ * A plugin representing a SIP transport protocol,
+ * for example UDP, TCP or TLS
+ */
+class LIBMSIP_API SipTransport: public MPlugin{
+ public:
+ /** @return true if it is a encrypted SIPS transport */
+ virtual bool isSecure() const=0;
+
+ /** @return transport protocol id in lower case, such as udp */
+ virtual std::string getProtocol() const=0;
+
+ virtual MRef<SipSocketServer *> createServer( MRef<SipLayerTransport*> receiver, bool ipv6, const std::string &ipString, int32_t prefPort, MRef<CertificateSet *> cert_db = NULL, MRef<CertificateChain *> certChain = NULL ) = 0;
+
+ // MPlugin
+ std::string getPluginType() const { return "SipTransport"; }
+
+ protected:
+ SipTransport( MRef<Library *> lib );
+ SipTransport();
+};
+
+
+class LIBMSIP_API SipTransportRegistry: public MPluginRegistry, public MSingleton<SipTransportRegistry>{
+ public:
+ virtual std::string getPluginType(){ return "SipTransport"; }
+
+ MRef<SipTransport*> findTransport( std::string protocol, bool secure=false ) const;
+
+ protected:
+ SipTransportRegistry();
+
+ private:
+ friend class MSingleton<SipTransportRegistry>;
+};
+
+#endif
Property changes on: trunk/libmsip/source/SipTransport.h
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Name: svn:eol-style
+ native
Added: trunk/libmsip/source/SipTransportUdp.cxx
===================================================================
--- trunk/libmsip/source/SipTransportUdp.cxx (rev 0)
+++ trunk/libmsip/source/SipTransportUdp.cxx 2007-11-17 22:51:39 UTC (rev 3475)
@@ -0,0 +1,100 @@
+/*
+ Copyright (C) 2005, 2004 Erik Eliasson, Johan Bilien
+ Copyright (C) 2005-2007 Mikael Magnusson
+
+ 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
+*/
+
+/*
+ * Authors: Erik Eliasson <eliasson at it.kth.se>
+ * Johan Bilien <jobi at via.ecp.fr>
+ * Mikael Magnusson <mikma at users.sourceforge.net>
+*/
+
+#include<config.h>
+#include<libmnetutil/NetworkException.h>
+#include<libmnetutil/UDPSocket.h>
+#include"SipTransportUdp.h"
+
+static std::list<std::string> pluginList;
+static int initialized;
+
+using namespace std;
+
+extern "C" LIBMSIP_API
+std::list<std::string> *mudp_LTX_listPlugins( MRef<Library*> lib ){
+ if( !initialized ){
+ pluginList.push_back("getPlugin");
+ initialized = true;
+ }
+
+ return &pluginList;
+}
+
+extern "C" LIBMSIP_API
+MPlugin * mudp_LTX_getPlugin( MRef<Library*> lib ){
+ return new SipTransportUdp( lib );
+}
+
+
+SipTransportUdp::SipTransportUdp( MRef<Library*> lib ) : SipTransport( lib ){
+}
+
+SipTransportUdp::~SipTransportUdp(){
+}
+
+
+
+/**
+ *
+ * @param externalPort If the application wishes to override what port
+ * should be reported for the socket it is possible to specify such a port
+ * number here. This can be used for example to implement support for
+ * passing NATs with the help of a STUN server.
+ */
+MRef<SipSocketServer *> SipTransportUdp::createServer( MRef<SipLayerTransport*> receiver, bool ipv6, const string &ipString, int32_t prefPort, MRef<CertificateSet *> cert_db, MRef<CertificateChain *> certChain )
+{
+ int32_t port = prefPort;
+ int triesLeft=10;
+ MRef<SipSocketServer *> server;
+ bool fail;
+
+ do {
+ fail=false;
+ try {
+
+ MRef<DatagramSocket *> sock = new UDPSocket( port, ipv6 );
+ server = new DatagramSocketServer( receiver, sock );
+ server->setExternalIp( ipString );
+
+
+ } catch(const BindFailed &bf){
+ fail=true;
+
+ // If the port is already in use, try random port number in the range 2048 to 63488
+ port = rand()%(0xFFFF-4096) + 2048;
+ triesLeft--;
+ if (!triesLeft)
+ throw;
+
+ }
+ }while(fail);
+
+ return server;
+}
+
+uint32_t SipTransportUdp::getVersion() const{
+ return 0x00000001;
+}
Property changes on: trunk/libmsip/source/SipTransportUdp.cxx
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Name: svn:eol-style
+ native
Added: trunk/libmsip/source/SipTransportUdp.h
===================================================================
--- trunk/libmsip/source/SipTransportUdp.h (rev 0)
+++ trunk/libmsip/source/SipTransportUdp.h 2007-11-17 22:51:39 UTC (rev 3475)
@@ -0,0 +1,54 @@
+/*
+ Copyright (C) 2006-2007 Mikael Magnusson
+
+ 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
+*/
+
+/*
+ * Authors: Mikael Magnusson <mikma at users.sourceforge.net>
+*/
+
+#ifndef SIP_TRANSPORT_UDP_H
+#define SIP_TRANSPORT_UDP_H
+
+#include<libmsip/libmsip_config.h>
+
+#include<string>
+#include"SipTransport.h"
+
+class SipTransportUdp: public SipTransport{
+ public:
+ SipTransportUdp( MRef<Library *> lib );
+ virtual ~SipTransportUdp();
+
+ // SipTransportUdp
+ virtual bool isSecure() const { return false; }
+
+ virtual std::string getProtocol() const { return "udp"; }
+
+ virtual MRef<SipSocketServer *> createServer( MRef<SipLayerTransport*> receiver, bool ipv6, const std::string &ipString, int32_t prefPort, MRef<CertificateSet *> cert_db = NULL, MRef<CertificateChain *> certChain = NULL );
+
+ // MPlugin
+ virtual std::string getName() const { return "UDP"; }
+
+ virtual uint32_t getVersion() const;
+
+ virtual std::string getDescription() const { return "SIP Transport UDP"; };
+
+ // MObject
+ virtual std::string getMemObjectType() const { return getName(); }
+};
+
+#endif
Property changes on: trunk/libmsip/source/SipTransportUdp.h
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Name: svn:eol-style
+ native
More information about the Minisip-devel
mailing list