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 #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
00101 virtual PBoolean GetExternalAddress(
00102 PIPSocket::Address & externalAddress,
00103 const PTimeInterval & maxAge = 1000
00104 ) = 0;
00105
00108 virtual bool GetInterfaceAddress(
00109 PIPSocket::Address & internalAddress
00110 ) const = 0;
00111
00125 virtual PBoolean CreateSocket(
00126 PUDPSocket * & socket,
00127 const PIPSocket::Address & binding = PIPSocket::GetDefaultIpAny(),
00128 WORD localPort = 0
00129 ) = 0;
00130
00144 virtual PBoolean CreateSocketPair(
00145 PUDPSocket * & socket1,
00146 PUDPSocket * & socket2,
00147 const PIPSocket::Address & binding = PIPSocket::GetDefaultIpAny()
00148 ) = 0;
00149
00163 virtual PBoolean CreateSocketPair(
00164 PUDPSocket * & socket1,
00165 PUDPSocket * & socket2,
00166 const PIPSocket::Address & binding,
00167 void * userData
00168 );
00169
00177 virtual bool IsAvailable(
00178 const PIPSocket::Address & binding = PIPSocket::GetDefaultIpAny()
00179 ) = 0;
00180
00185 virtual void Activate(bool active);
00186
00190 virtual void SetAlternateAddresses(
00191 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 = false
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
00342
00343
00344 template <class className> class PNatMethodServiceDescriptor : public PDevicePluginServiceDescriptor
00345 {
00346 public:
00347 virtual PObject * CreateInstance(int ) const { return (PNatMethod *)new className; }
00348 virtual PStringArray GetDeviceNames(int ) const { return className::GetNatMethodName(); }
00349 virtual bool ValidateDeviceName(const PString & deviceName, int ) 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