r3458 - in trunk/libmsip: include/libmsip source
erik at minisip.org
erik at minisip.org
Fri Nov 2 20:24:29 CET 2007
Author: erik
Date: 2007-11-02 20:24:29 +0100 (Fri, 02 Nov 2007)
New Revision: 3458
Modified:
trunk/libmsip/include/libmsip/SipStack.h
trunk/libmsip/source/SipAuthenticationDigest.cxx
trunk/libmsip/source/SipDialogConfig.cxx
trunk/libmsip/source/SipLayerTransport.cxx
trunk/libmsip/source/SipLayerTransport.h
trunk/libmsip/source/SipStack.cxx
trunk/libmsip/source/SipStackInternal.cxx
trunk/libmsip/source/SipStackInternal.h
Log:
* Moved getLocalSipPort from SipStackConfig to the SipStack object.
The method should return the port actually used, and not always the
prefered one. The method should therefore not be in the
configuration class.
* Moved the implementation of start{Udp,Tcp,Tls}Server and
create{Udp,Tcp,Tls}Server into the transport layer class
where it should be.
Modified: trunk/libmsip/include/libmsip/SipStack.h
===================================================================
--- trunk/libmsip/include/libmsip/SipStack.h 2007-11-02 17:31:17 UTC (rev 3457)
+++ trunk/libmsip/include/libmsip/SipStack.h 2007-11-02 19:24:29 UTC (rev 3458)
@@ -165,11 +165,6 @@
int32_t preferedLocalTcpPort;
int32_t preferedLocalTlsPort;
- /**
- @return the port in use, depending on the transport.
- @param usesStun (default false), found in SipSoftPhoneConfiguration::useSTUN
- */
- int32_t getLocalSipPort(bool usesStun=false, const std::string &transport="UDP");
bool autoAnswer;
@@ -275,6 +270,13 @@
void startTcpServer();
void startTlsServer();
+ /**
+ @return the port in use, depending on the transport.
+ @param usesStun (default false), found in SipSoftPhoneConfiguration::useSTUN
+ */
+ int32_t getLocalSipPort(bool usesStun=false, const std::string &transport="UDP");
+
+
void setDebugPrintPackets(bool enable);
bool getDebugPrintPackets();
Modified: trunk/libmsip/source/SipAuthenticationDigest.cxx
===================================================================
--- trunk/libmsip/source/SipAuthenticationDigest.cxx 2007-11-02 17:31:17 UTC (rev 3457)
+++ trunk/libmsip/source/SipAuthenticationDigest.cxx 2007-11-02 19:24:29 UTC (rev 3458)
@@ -84,7 +84,7 @@
}
string SipAuthenticationDigest::md5ToString(unsigned char *md5){
- char *digits = {"0123456789abcdef"};
+ const char *digits = {"0123456789abcdef"};
char strsum[33] = {'\0'};
for (int i=0 ; i<16; i++){
int32_t intval = md5[i];
Modified: trunk/libmsip/source/SipDialogConfig.cxx
===================================================================
--- trunk/libmsip/source/SipDialogConfig.cxx 2007-11-02 17:31:17 UTC (rev 3457)
+++ trunk/libmsip/source/SipDialogConfig.cxx 2007-11-02 19:24:29 UTC (rev 3458)
@@ -343,24 +343,6 @@
}
-int32_t SipStackConfig::getLocalSipPort(bool usesStun, const string &transport ) {
- int32_t localSipPort;
-
- if(transport=="TCP" || transport=="tcp")
- localSipPort = preferedLocalTcpPort;
- else if(transport=="TLS" || transport=="tls")
- localSipPort = preferedLocalTlsPort;
- else{ /* UDP, may use STUN */
- if( usesStun ){
- localSipPort = externalContactUdpPort;
- } else {
- localSipPort = preferedLocalUdpPort;
- }
- }
- return localSipPort;
-}
-
-
SipDialogConfig::SipDialogConfig(MRef<SipStack*> stack) {
// inherited = new SipStackConfig; /// We want do do a "deep copy" here. This is so that
// /// we have a local copy that we can modify and that
@@ -409,7 +391,7 @@
else
transport = fromUri.getTransport();
- port =sipStack->getStackConfig()->getLocalSipPort(useStun, transport);
+ port =sipStack->getLocalSipPort(useStun, transport);
contactUri.setParams( fromUri.getUserName(),
sipStack->getStackConfig()->externalContactIP,
Modified: trunk/libmsip/source/SipLayerTransport.cxx
===================================================================
--- trunk/libmsip/source/SipLayerTransport.cxx 2007-11-02 17:31:17 UTC (rev 3457)
+++ trunk/libmsip/source/SipLayerTransport.cxx 2007-11-02 19:24:29 UTC (rev 3458)
@@ -43,10 +43,13 @@
#include<libmsip/SipHeaderContact.h>
#include<libmsip/SipHeaderTo.h>
-#include<libmcrypto/TlsSocket.h>
+#include<libmcrypto/TlsServerSocket.h>
#include<libmnetutil/ServerSocket.h>
#include<libmnetutil/NetworkException.h>
#include<libmnetutil/NetworkFunctions.h>
+#include<libmnetutil/UDPSocket.h>
+#include<libmcrypto/TlsSocket.h>
+#include<libmnetutil/NetworkException.h>
#include<libmutil/Timestamp.h>
#include<libmutil/MemObject.h>
#include<libmutil/mtime.h>
@@ -339,6 +342,9 @@
MRef<CertificateSet *> cert_db_):
cert_chain(cchain), cert_db(cert_db_), tls_ctx(NULL)
{
+ contactUdpPort=0;
+ contactTcpPort=0;
+ contactTlsPort=0;
manager = new SocketServer();
manager->start();
}
@@ -1197,4 +1203,180 @@
}
+/**
+ *
+ * @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;
+ MRef<SipSocketServer *> server;
+ int32_t port = prefPort;
+ bool fail;
+ int triesLeft=10;
+
+ do {
+ fail=false;
+ try {
+
+ sock = ServerSocket::create( port, ipv6 );
+ server = new StreamSocketServer( this, sock );
+ server->setExternalIp( ipString );
+
+ } catch ( const BindFailed &bf ){
+ fail=true;
+ triesLeft--;
+ if (!triesLeft)
+ throw;
+ }
+ } while ( fail );
+
+ contactTcpPort = port;
+
+ return server;
+}
+
+MRef<SipSocketServer *> SipLayerTransport::createTlsServer( bool ipv6, const string &ipString, int32_t prefPort, MRef<CertificateChain *> certChain, MRef<CertificateSet *> cert_db)
+{
+ MRef<ServerSocket *> sock;
+ MRef<SipSocketServer *> server;
+ int32_t port = prefPort;
+ bool fail;
+ int triesLeft=10;
+
+ do {
+ fail=false;
+ try{
+ sock = TLSServerSocket::create( ipv6, port, /*config->cert*/certChain->getFirst(),
+ /*config->*/cert_db );
+ server = new StreamSocketServer( this, sock );
+ server->setExternalIp( ipString );
+ } catch (const BindFailed &bf){
+ fail=true;
+ triesLeft--;
+ if (!triesLeft)
+ throw;
+ }
+
+ } while (fail);
+
+ contactTlsPort = port;
+
+ return server;
+}
+
+void SipLayerTransport::startUdpServer(const string &ipString, const string &ip6String, int32_t localUdpPort, int32_t externalContactUdpPort)
+{
+ MRef<SipSocketServer *> server;
+/*
+ string ipString;
+ if( config->externalContactIP.size()>0 )
+ ipString = config->externalContactIP;
+ else
+ ipString = config->localIpString;
+*/
+
+ server = createUdpServer( false, ipString, localUdpPort, externalContactUdpPort );
+ addServer( server );
+
+ if( /*config->localIp6String*/ ip6String != "" ){
+ MRef<SipSocketServer *> server6;
+ server6 = createUdpServer( true, /*config->localIp6String*/ ip6String,localUdpPort,externalContactUdpPort );
+ addServer( server6 );
+ }
+}
+
+
+void SipLayerTransport::startTcpServer( const string & ipString, const string & ip6String, int32_t prefPort)
+{
+ MRef<SipSocketServer *> server;
+
+ server = createTcpServer( false, /*config->localIpString*/ ipString, prefPort);
+ dispatcher->getLayerTransport()->addServer( server );
+
+ if( /*config->localIp6String*/ ip6String != "" ){
+ MRef<SipSocketServer *> server6;
+
+ server6 = createTcpServer( true, /*config->localIp6String*/ ip6String, prefPort );
+ addServer( server6 );
+ }
+}
+
+void SipLayerTransport::startTlsServer( const string &ipString, const string &ip6String, int32_t prefPort, MRef<CertificateChain *> certChain, MRef<CertificateSet *> cert_db){
+ MRef<SipSocketServer *> server;
+
+ if( certChain.isNull() || certChain->getFirst().isNull() ){
+ merr << "You need a personal certificate to run "
+ "a TLS server. Please specify one in "
+ "the certificate settings. minisip will "
+ "now disable the TLS server." << endl;
+ return;
+ }
+
+ server = createTlsServer( false, /*config->localIpString*/ ipString, prefPort, certChain, cert_db);
+ addServer( server );
+
+ if( /*config->localIp6String*/ ip6String != "" ){
+ MRef<SipSocketServer *> server6;
+
+ server6 = createTlsServer( true, /*config->localIp6String*/ ip6String, prefPort, certChain, cert_db );
+ dispatcher->getLayerTransport()->addServer( server6 );
+ }
+}
+
+
+int32_t SipLayerTransport::getLocalSipPort( const string &transport ) {
+ if (transport=="TCP" || transport=="tcp")
+ return contactTcpPort;
+ if (transport=="TLS" || transport=="tls")
+ return contactTlsPort;
+ return contactUdpPort;
+}
+
+
Modified: trunk/libmsip/source/SipLayerTransport.h
===================================================================
--- trunk/libmsip/source/SipLayerTransport.h 2007-11-02 17:31:17 UTC (rev 3457)
+++ trunk/libmsip/source/SipLayerTransport.h 2007-11-02 19:24:29 UTC (rev 3458)
@@ -80,7 +80,14 @@
void datagramSocketRead(MRef<DatagramSocket *> sock);
+ void startUdpServer( const std::string & ipString, const std::string & ip6String, int32_t prefUdpPort, int32_t externalContactUdpPort);
+ void startTcpServer( const std::string & ipString, const std::string & ip6String, int32_t prefPort );
+ void startTlsServer( const std::string & ipString, const std::string & ip6String, int32_t prefPort, MRef<CertificateChain *> certChain, MRef<CertificateSet *> cert_db );
+
+ int32_t getLocalSipPort( const std::string &transport );
+
protected:
+
void sendMessage(MRef<SipMessage*> pack,
const std::string &toaddr,
int32_t port,
@@ -126,6 +133,23 @@
MRef<SipSocketServer *> server,
MRef<Socket *> socket);
+
+ /**
+ * Cache what UDP/TCP/TLS server ports are used.
+ * These values are used when creating SIP via headers.
+ *
+ * Note that the values are the one that will be added
+ * to the Via header, and NAT handling techniques may
+ * modify these values so that they differ from
+ * the low layer socket values.
+ */
+ 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 );
+
Mutex serversLock;
std::list<MRef<SipSocketServer *> > servers;
MRef<SocketServer*> manager;
Modified: trunk/libmsip/source/SipStack.cxx
===================================================================
--- trunk/libmsip/source/SipStack.cxx 2007-11-02 17:31:17 UTC (rev 3457)
+++ trunk/libmsip/source/SipStack.cxx 2007-11-02 19:24:29 UTC (rev 3458)
@@ -201,6 +201,10 @@
STACK->startTlsServer();
}
+int32_t SipStack::getLocalSipPort(bool usesStun, const string &transport ) {
+ return STACK->getLocalSipPort(usesStun, transport);
+}
+
void SipStack::setDebugPrintPackets(bool enable){
set_debug_print_packets(enable);
}
Modified: trunk/libmsip/source/SipStackInternal.cxx
===================================================================
--- trunk/libmsip/source/SipStackInternal.cxx 2007-11-02 17:31:17 UTC (rev 3457)
+++ trunk/libmsip/source/SipStackInternal.cxx 2007-11-02 19:24:29 UTC (rev 3458)
@@ -63,8 +63,6 @@
#include<libmsip/SipHeaderTo.h>
#include<libmsip/SipHeaderWWWAuthenticate.h>
#include<libmsip/SipCommandString.h>
-#include<libmnetutil/UDPSocket.h>
-#include<libmnetutil/NetworkException.h>
#include<libmutil/massert.h>
@@ -381,99 +379,8 @@
}
-MRef<SipSocketServer *> SipStackInternal::createUdpServer( bool ipv6, const string &ipString )
-{
- int32_t port = config->preferedLocalUdpPort;
- int triesLeft=10;
- MRef<SipSocketServer *> server;
- bool fail;
-
- do {
- fail=false;
- try {
-
- MRef<DatagramSocket *> sock = new UDPSocket( port, ipv6 );
- server = new DatagramSocketServer( dispatcher->getLayerTransport(), sock );
- // IPv6 doesn't need different external udp port
- // since it never is NATed.
- if( !ipv6 && config->externalContactUdpPort ){
- server->setExternalPort( config->externalContactUdpPort );
- }
- 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;
-}
-
-MRef<SipSocketServer *> SipStackInternal::createTcpServer( bool ipv6, const string &ipString )
-{
- MRef<ServerSocket *> sock;
- MRef<SipSocketServer *> server;
- int32_t port = config->preferedLocalTcpPort;
- bool fail;
- int triesLeft=10;
-
- do {
- fail=false;
- try {
-
- sock = ServerSocket::create( port, ipv6 );
- server = new StreamSocketServer( dispatcher->getLayerTransport(), sock );
- server->setExternalIp( ipString );
-
- } catch ( const BindFailed &bf ){
- fail=true;
- triesLeft--;
- if (!triesLeft)
- throw;
- }
- } while ( fail );
-
- return server;
-}
-
-MRef<SipSocketServer *> SipStackInternal::createTlsServer( bool ipv6, const string &ipString )
-{
- MRef<ServerSocket *> sock;
- MRef<SipSocketServer *> server;
- int32_t port = config->preferedLocalTlsPort;
- bool fail;
- int triesLeft=10;
-
- do {
- fail=false;
- try{
- sock = TLSServerSocket::create( ipv6, port, config->cert->getFirst(),
- config->cert_db );
- server = new StreamSocketServer( dispatcher->getLayerTransport(), sock );
- server->setExternalIp( ipString );
- } catch (const BindFailed &bf){
- fail=true;
- triesLeft--;
- if (!triesLeft)
- throw;
- }
-
- } while (fail);
-
- return server;
-}
-
void SipStackInternal::startUdpServer()
{
- MRef<SipSocketServer *> server;
string ipString;
if( config->externalContactIP.size()>0 )
@@ -481,51 +388,32 @@
else
ipString = config->localIpString;
- server = createUdpServer( false, ipString );
- dispatcher->getLayerTransport()->addServer( server );
-
- if( config->localIp6String != "" ){
- MRef<SipSocketServer *> server6;
-
- server6 = createUdpServer( true, config->localIp6String );
- dispatcher->getLayerTransport()->addServer( server6 );
- }
+ dispatcher->getLayerTransport()->startUdpServer( ipString,
+ config->localIp6String,
+ config->preferedLocalUdpPort,
+ config->externalContactUdpPort );
}
void SipStackInternal::startTcpServer()
{
- MRef<SipSocketServer *> server;
-
- server = createTcpServer( false, config->localIpString);
- dispatcher->getLayerTransport()->addServer( server );
-
- if( config->localIp6String != "" ){
- MRef<SipSocketServer *> server6;
-
- server6 = createTcpServer( true, config->localIp6String );
- dispatcher->getLayerTransport()->addServer( server6 );
- }
+ dispatcher->getLayerTransport()->startTcpServer( config->localIpString, config->localIp6String, config->preferedLocalTcpPort );
+
}
void SipStackInternal::startTlsServer(){
- MRef<SipSocketServer *> server;
- if( config->cert->getFirst().isNull() ){
- merr << "You need a personal certificate to run "
- "a TLS server. Please specify one in "
- "the certificate settings. minisip will "
- "now disable the TLS server." << endl;
- return;
- }
+ dispatcher->getLayerTransport()->startTlsServer(
+ config->localIpString,
+ config->localIp6String,
+ config->preferedLocalTlsPort,
+ config->cert,
+ config->cert_db );
+}
- server = createTlsServer( false, config->localIpString );
- dispatcher->getLayerTransport()->addServer( server );
+int32_t SipStackInternal::getLocalSipPort(bool usesStun, const string &transport ) {
- if( config->localIp6String != "" ){
- MRef<SipSocketServer *> server6;
+ return dispatcher->getLayerTransport()->getLocalSipPort( transport );
- server6 = createTlsServer( true, config->localIp6String );
- dispatcher->getLayerTransport()->addServer( server6 );
- }
}
+
Modified: trunk/libmsip/source/SipStackInternal.h
===================================================================
--- trunk/libmsip/source/SipStackInternal.h 2007-11-02 17:31:17 UTC (rev 3457)
+++ trunk/libmsip/source/SipStackInternal.h 2007-11-02 19:24:29 UTC (rev 3458)
@@ -86,21 +86,16 @@
std::string getStackStatusDebugString();
- MRef<SipSocketServer *> createUdpServer( bool ipv6, const std::string &ipString );
- MRef<SipSocketServer *> createTcpServer( bool ipv6, const std::string &ipString );
- MRef<SipSocketServer *> createTlsServer( bool ipv6, const std::string &ipString );
-
void startUdpServer();
void startTcpServer();
void startTlsServer();
+ int32_t getLocalSipPort(bool usesStun, const std::string &transport);
+
void free();
private:
-// std::string getDialogDebugString(MRef<SipDialog*> d, list<MRef<SipTransaction*> > &trans,
-// list <TPRequest<string,MRef<StateMachine<SipSMCommand,string>*> > > &torequests);
-
MRef<SipTimers*> timers;
MRef<SipStackConfig *> config;
MRef<CommandReceiver*> callback;
More information about the Minisip-devel
mailing list