# 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: 26549 $
    00030  * $Author: rjongbloed $
    00031  * $Date: 2011-10-05 23:24:38 -0500 (Wed, 05 Oct 2011) $
    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 // 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 PDECLARE_NAT_METHOD(method, cls) PFACTORY_CREATE(PFactory<PNatMethod>, cls, #method)
    00355
    00356 #define PCREATE_NAT_PLUGIN(name) \
    00357   static PNatMethodServiceDescriptor<PNatMethod_##name> PNatMethod_##name##_descriptor; \
    00358   PCREATE_PLUGIN_STATIC(name, PNatMethod, &PNatMethod_##name##_descriptor)
    00359
    00360
    00361 #if P_STUN
    00362 PFACTORY_LOAD(PSTUNClient);
    00363 #endif
    00364
    00365 #if P_TURN
    00366 PFACTORY_LOAD(PTURNClient);
    00367 #endif
    00368
    00369
    00370 #endif // PTLIB_PNAT_H
    00371
    00372
    00373 // End Of File ///////////////////////////////////////////////////////////////