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  * $Log: pnat.h,v $
00030  * Revision 1.11  2007/08/22 05:04:39  rjongbloed
00031  * Added ability to set a specific local port for STUN created sockets.
00032  *
00033  * Revision 1.10  2007/07/22 03:07:30  rjongbloed
00034  * Added parameter so can bind STUN socket to specific interface.
00035  *
00036  * Revision 1.9  2007/04/18 23:49:50  csoutheren
00037  * Add usage of precompiled headers
00038  *
00039  * Revision 1.8  2007/02/11 13:07:37  shorne
00040  * Added GetName function
00041  *
00042  * Revision 1.7  2007/01/05 04:09:05  dereksmithies
00043  * Get it to compile on linux correctly.
00044  * Previous code failed, with message about pasting STUN and > together.
00045  *
00046  * Revision 1.6  2006/12/23 15:08:00  shorne
00047  * Now Factory loaded for ease of addition of new NAT Methods
00048  *
00049  * Revision 1.5  2006/02/26 09:29:10  shorne
00050  * Added GetMethodName and GetList functions
00051  *
00052  * Revision 1.4  2006/01/26 03:23:41  shorne
00053  * Fix compile error when merging code
00054  *
00055  * Revision 1.3  2005/11/30 12:47:37  csoutheren
00056  * Removed tabs, reformatted some code, and changed tags for Doxygen
00057  *
00058  * Revision 1.2  2005/07/13 11:15:14  csoutheren
00059  * Backported NAT abstraction files from isvo branch
00060  *
00061  * Revision 1.1.2.1  2005/04/25 13:23:19  shorne
00062  * Initial version
00063  *
00064  *
00065 */
00066 
00067 #ifndef _PTLIB_H
00068 #include <ptlib.h>
00069 #endif
00070 
00071 #include <ptlib/sockets.h>
00072 
00073 #ifndef P_NATMETHOD
00074 #define P_NATMETHOD
00075 
00076 #include <ptlib/plugin.h>
00077 #include <ptlib/pluginmgr.h>
00078 
00086 class PNatMethod  : public PObject
00087 {
00088   PCLASSINFO(PNatMethod,PObject);
00089 
00090 public:
00091 
00096   PNatMethod();
00097 
00100   ~PNatMethod();
00102 
00103 
00106     static PNatMethod * Create(
00107       const PString & name,        
00108       PPluginManager * pluginMgr = NULL   
00109     );
00110 
00113 
00117    virtual BOOL GetExternalAddress(
00118       PIPSocket::Address & externalAddress, 
00119       const PTimeInterval & maxAge = 1000   
00120    ) = 0;
00121 
00125    virtual BOOL CreateSocketPair(
00126       PUDPSocket * & socket1,
00127       PUDPSocket * & socket2,
00128       const PIPSocket::Address & binding = PIPSocket::GetDefaultIpAny()
00129    ) = 0;
00130 
00138    virtual BOOL IsAvailable() { return FALSE; };
00139 
00150    virtual void SetPortRanges(
00151       WORD portBase,          
00152       WORD portMax = 0,       
00153       WORD portPairBase = 0,  
00154       WORD portPairMax = 0    
00155    );
00156 
00159    static PStringList GetNatMethodName() { return PStringList(); };
00160 
00161    virtual PStringList GetName() const
00162       { return GetNatMethodName(); }
00163 
00165 
00166 protected:
00167   struct PortInfo {
00168       PortInfo(WORD port = 0)
00169         : basePort(port)
00170         , maxPort(port)
00171         , currentPort(port)
00172       {
00173       }
00174 
00175       PMutex mutex;
00176       WORD   basePort;
00177       WORD   maxPort;
00178       WORD   currentPort;
00179     } singlePortInfo, pairedPortInfo;
00180 
00181 };
00182 
00184 
00185 PLIST(PNatList, PNatMethod);
00186 
00188 
00194 class PNatStrategy : public PObject
00195 {
00196   PCLASSINFO(PNatStrategy,PObject);
00197 
00198 public :
00199 
00204   PNatStrategy();
00205 
00208   ~PNatStrategy();
00210 
00218   void AddMethod(PNatMethod * method);
00219 
00225   PNatMethod * GetMethod();
00226 
00227 
00231   BOOL RemoveMethod(const PString & meth);
00232 
00243     void SetPortRanges(
00244       WORD portBase,          
00245       WORD portMax = 0,       
00246       WORD portPairBase = 0,  
00247       WORD portPairMax = 0    
00248     );
00249 
00252     PNatList GetNATList() {  return natlist; };
00253 
00254         PNatMethod * LoadNatMethod(const PString & name);
00255 
00256     PStringList GetRegisteredList();
00257 
00259 
00260 private:
00261   PNatList natlist;
00262 };
00263 
00265 //
00266 // declare macros and structures needed for NAT plugins
00267 //
00268 
00269 typedef PFactory<PNatMethod> NatFactory;
00270 
00271 template <class className> class PNatMethodServiceDescriptor : public PDevicePluginServiceDescriptor
00272 {
00273   public:
00274     virtual PObject *   CreateInstance(int /*userData*/) const { return new className; }
00275     virtual PStringList GetDeviceNames(int /*userData*/) const { return className::GetNatMethodName(); }
00276 };
00277 
00278 #define PCREATE_NAT_PLUGIN(name) \
00279   static PNatMethodServiceDescriptor<PNatMethod_##name> PNatMethod_##name##_descriptor; \
00280   PCREATE_PLUGIN(name, PNatMethod, &PNatMethod_##name##_descriptor)
00281 
00282 #endif

Generated on Fri Mar 7 06:25:02 2008 for PTLib by  doxygen 1.5.1