pnat.h

Go to the documentation of this file.
00001 /*
00002  * pnat.h
00003  *
00004  * NAT Strategy support for Portable Windows Library.
00005  *
00006  * Virteos is a Trade Mark of ISVO (Asia) Pte Ltd.
00007  *
00008  * Copyright (c) 2004 ISVO (Asia) Pte Ltd. All Rights Reserved.
00009  *
00010  * The contents of this file are subject to the Mozilla Public License
00011  * Version 1.0 (the "License"); you may not use this file except in
00012  * compliance with the License. You may obtain a copy of the License at
00013  * http://www.mozilla.org/MPL/
00014  *
00015  * Software distributed under the License is distributed on an "AS IS"
00016  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
00017  * the License for the specific language governing rights and limitations
00018  * under the License.
00019  *
00020  *
00021  * The Original Code is derived from and used in conjunction with the 
00022  * OpenH323 Project (www.openh323.org/)
00023  *
00024  * The Initial Developer of the Original Code is ISVO (Asia) Pte Ltd.
00025  *
00026  *
00027  * Contributor(s): ______________________________________.
00028  *
00029  * $Revision: 23095 $
00030  * $Author: shorne $
00031  * $Date: 2009-07-10 14:14:43 +0000 (Fri, 10 Jul 2009) $
00032  */
00033 
00034 #include <ptlib/sockets.h>
00035 
00036 #ifndef PTLIB_PNAT_H
00037 #define PTLIB_PNAT_H
00038 
00039 #include <ptlib/plugin.h>
00040 #include <ptlib/pluginmgr.h>
00041 
00049 class PNatMethod  : public PObject
00050 {
00051     PCLASSINFO(PNatMethod,PObject);
00052 
00053   public:
00058     PNatMethod();
00059 
00062     ~PNatMethod();
00064 
00065 
00068     virtual void PrintOn(
00069       ostream & strm
00070     ) const;
00072 
00073 
00078     static PNatMethod * Create(
00079       const PString & name,        
00080       PPluginManager * pluginMgr = NULL   
00081     );
00082 
00085     virtual PString GetName() const = 0;
00086 
00090     virtual PString GetServer() const;
00091 
00094     virtual bool GetServerAddress(
00095       PIPSocket::Address & address,   
00096       WORD & port                     
00097     ) const = 0;
00098 
00102     virtual PBoolean GetExternalAddress(
00103       PIPSocket::Address & externalAddress, 
00104       const PTimeInterval & maxAge = 1000   
00105     ) = 0;
00106 
00109     virtual bool GetInterfaceAddress(
00110       PIPSocket::Address & internalAddress
00111     ) const = 0;
00112 
00126     virtual PBoolean CreateSocket(
00127       PUDPSocket * & socket,
00128       const PIPSocket::Address & binding = PIPSocket::GetDefaultIpAny(),
00129       WORD localPort = 0
00130     ) = 0;
00131 
00145     virtual PBoolean CreateSocketPair(
00146       PUDPSocket * & socket1,
00147       PUDPSocket * & socket2,
00148       const PIPSocket::Address & binding = PIPSocket::GetDefaultIpAny()
00149     ) = 0;
00150 
00164     virtual PBoolean CreateSocketPair(
00165       PUDPSocket * & socket1,
00166       PUDPSocket * & socket2,
00167       const PIPSocket::Address & binding,
00168       void * userData
00169     );
00170 
00178     virtual bool IsAvailable(
00179       const PIPSocket::Address & binding = PIPSocket::GetDefaultIpAny()  
00180     ) = 0;
00181 
00186     virtual void Activate(bool active);
00187 
00191     virtual void SetAlternateAddresses(const PStringArray & addresses,   
00192                                        void * userData = NULL
00193                                       );
00194 
00195     enum RTPSupportTypes {
00196       RTPSupported,
00197       RTPIfSendMedia,
00198       RTPUnsupported,
00199       RTPUnknown,
00200       NumRTPSupportTypes
00201     };
00202 
00206     virtual RTPSupportTypes GetRTPSupport(
00207       PBoolean force = PFalse    
00208     ) = 0;
00209 
00220     virtual void SetPortRanges(
00221       WORD portBase,          
00222       WORD portMax = 0,       
00223       WORD portPairBase = 0,  
00224       WORD portPairMax = 0    
00225     );
00227 
00228   protected:
00229     struct PortInfo {
00230       PortInfo(WORD port = 0)
00231         : basePort(port)
00232         , maxPort(port)
00233         , currentPort(port)
00234       {
00235       }
00236 
00237       PMutex mutex;
00238       WORD   basePort;
00239       WORD   maxPort;
00240       WORD   currentPort;
00241     } singlePortInfo, pairedPortInfo;
00242 
00249         WORD RandomPortPair(unsigned int start, unsigned int end);
00250 };
00251 
00253 
00254 PLIST(PNatList, PNatMethod);
00255 
00257 
00263 class PNatStrategy : public PObject
00264 {
00265   PCLASSINFO(PNatStrategy,PObject);
00266 
00267 public :
00268 
00273   PNatStrategy();
00274 
00277   ~PNatStrategy();
00279 
00287   void AddMethod(PNatMethod * method);
00288 
00294   PNatMethod * GetMethod(const PIPSocket::Address & address = PIPSocket::GetDefaultIpAny());
00295 
00300   PNatMethod * GetMethodByName(const PString & name);
00301 
00305   PBoolean RemoveMethod(const PString & meth);
00306 
00317     void SetPortRanges(
00318       WORD portBase,          
00319       WORD portMax = 0,       
00320       WORD portPairBase = 0,  
00321       WORD portPairMax = 0    
00322     );
00323 
00326     PNatList & GetNATList() {  return natlist; };
00327 
00328         PNatMethod * LoadNatMethod(const PString & name);
00329 
00330     static PStringArray GetRegisteredList();
00331 
00333 
00334 private:
00335   PNatList natlist;
00336   PPluginManager * pluginMgr;
00337 };
00338 
00340 //
00341 // declare macros and structures needed for NAT plugins
00342 //
00343 
00344 template <class className> class PNatMethodServiceDescriptor : public PDevicePluginServiceDescriptor
00345 {
00346   public:
00347     virtual PObject *    CreateInstance(int /*userData*/) const { return (PNatMethod *)new className; }
00348     virtual PStringArray GetDeviceNames(int /*userData*/) const { return className::GetNatMethodName(); }
00349     virtual bool  ValidateDeviceName(const PString & deviceName, int /*userData*/) const { 
00350               return (deviceName == GetDeviceNames(0)[0]); 
00351         } 
00352 };
00353 
00354 #define PCREATE_NAT_PLUGIN(name) \
00355   static PNatMethodServiceDescriptor<PNatMethod_##name> PNatMethod_##name##_descriptor; \
00356   PCREATE_PLUGIN_STATIC(name, PNatMethod, &PNatMethod_##name##_descriptor)
00357 
00358 #endif // PTLIB_PNAT_H
00359 
00360 
00361 // End Of File ///////////////////////////////////////////////////////////////

Generated on Mon Aug 3 20:41:50 2009 for PTLib by  doxygen 1.5.1