00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
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
00267
00268
00269 typedef PFactory<PNatMethod> NatFactory;
00270
00271 template <class className> class PNatMethodServiceDescriptor : public PDevicePluginServiceDescriptor
00272 {
00273 public:
00274 virtual PObject * CreateInstance(int ) const { return new className; }
00275 virtual PStringList GetDeviceNames(int ) 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