pnat.h
Go to the documentation of this file.
/*
* pnat.h
*
* NAT Strategy support for Portable Windows Library.
*
* Virteos is a Trade Mark of ISVO (Asia) Pte Ltd.
*
* Copyright (c) 2004 ISVO (Asia) Pte Ltd. All Rights Reserved.
*
* The contents of this file are subject to the Mozilla Public License
* Version 1.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
* the License for the specific language governing rights and limitations
* under the License.
*
*
* The Original Code is derived from and used in conjunction with the
* OpenH323 Project (www.openh323.org/)
*
* The Initial Developer of the Original Code is ISVO (Asia) Pte Ltd.
*
*
* Contributor(s): ______________________________________.
*
* $Revision: 26549 $
* $Author: rjongbloed $
* $Date: 2011-10-05 23:24:38 -0500 (Wed, 05 Oct 2011) $
*/
#include <ptlib/sockets.h>
#ifndef PTLIB_PNAT_H
#define PTLIB_PNAT_H
#include <ptlib/plugin.h>
#include <ptlib/pluginmgr.h>
class PNatMethod : public PObject
{
PCLASSINFO(PNatMethod,PObject);
public:
PNatMethod();
~PNatMethod();
virtual void PrintOn(
ostream & strm
) const;
static PNatMethod * Create(
const PString & name,
PPluginManager * pluginMgr = NULL
);
virtual PString GetName() const = 0;
virtual PString GetServer() const;
virtual bool GetServerAddress(
PIPSocket::Address & address,
WORD & port
) const = 0;
virtual PBoolean GetExternalAddress(
PIPSocket::Address & externalAddress,
const PTimeInterval & maxAge = 1000
) = 0;
virtual bool GetInterfaceAddress(
PIPSocket::Address & internalAddress
) const = 0;
virtual PBoolean CreateSocket(
PUDPSocket * & socket,
const PIPSocket::Address & binding = PIPSocket::GetDefaultIpAny(),
WORD localPort = 0
) = 0;
virtual PBoolean CreateSocketPair(
PUDPSocket * & socket1,
PUDPSocket * & socket2,
const PIPSocket::Address & binding = PIPSocket::GetDefaultIpAny()
) = 0;
virtual PBoolean CreateSocketPair(
PUDPSocket * & socket1,
PUDPSocket * & socket2,
const PIPSocket::Address & binding,
void * userData
);
virtual bool IsAvailable(
const PIPSocket::Address & binding = PIPSocket::GetDefaultIpAny()
) = 0;
virtual void Activate(bool active);
virtual void SetAlternateAddresses(
const PStringArray & addresses,
void * userData = NULL
);
enum RTPSupportTypes {
RTPSupported,
RTPIfSendMedia,
RTPUnsupported,
RTPUnknown,
NumRTPSupportTypes
};
virtual RTPSupportTypes GetRTPSupport(
PBoolean force = false
) = 0;
virtual void SetPortRanges(
WORD portBase,
WORD portMax = 0,
WORD portPairBase = 0,
WORD portPairMax = 0
);
protected:
struct PortInfo {
PortInfo(WORD port = 0)
: basePort(port)
, maxPort(port)
, currentPort(port)
{
}
PMutex mutex;
WORD basePort;
WORD maxPort;
WORD currentPort;
} singlePortInfo, pairedPortInfo;
WORD RandomPortPair(unsigned int start, unsigned int end);
};
PLIST(PNatList, PNatMethod);
class PNatStrategy : public PObject
{
PCLASSINFO(PNatStrategy,PObject);
public :
PNatStrategy();
~PNatStrategy();
void AddMethod(PNatMethod * method);
PNatMethod * GetMethod(const PIPSocket::Address & address = PIPSocket::GetDefaultIpAny());
PNatMethod * GetMethodByName(const PString & name);
PBoolean RemoveMethod(const PString & meth);
void SetPortRanges(
WORD portBase,
WORD portMax = 0,
WORD portPairBase = 0,
WORD portPairMax = 0
);
PNatList & GetNATList() { return natlist; };
PNatMethod * LoadNatMethod(const PString & name);
static PStringArray GetRegisteredList();
private:
PNatList natlist;
PPluginManager * pluginMgr;
};
//
// declare macros and structures needed for NAT plugins
//
template <class className> class PNatMethodServiceDescriptor : public PDevicePluginServiceDescriptor
{
public:
virtual PObject * CreateInstance(int /*userData*/) const { return (PNatMethod *)new className; }
virtual PStringArray GetDeviceNames(int /*userData*/) const { return className::GetNatMethodName(); }
virtual bool ValidateDeviceName(const PString & deviceName, int /*userData*/) const {
return (deviceName == GetDeviceNames(0)[0]);
}
};
#define PDECLARE_NAT_METHOD(method, cls) PFACTORY_CREATE(PFactory<PNatMethod>, cls, #method)
#define PCREATE_NAT_PLUGIN(name) \
static PNatMethodServiceDescriptor<PNatMethod_##name> PNatMethod_##name##_descriptor; \
PCREATE_PLUGIN_STATIC(name, PNatMethod, &PNatMethod_##name##_descriptor)
#if P_STUN
PFACTORY_LOAD(PSTUNClient);
#endif
#if P_TURN
PFACTORY_LOAD(PTURNClient);
#endif
#endif // PTLIB_PNAT_H
// End Of File ///////////////////////////////////////////////////////////////
PNatMethod::GetInterfaceAddress
virtual bool GetInterfaceAddress(PIPSocket::Address &internalAddress) const =0
Return the interface NAT router is using.
PNatMethod::RTPIfSendMedia
Definition: pnat.h:197
PNatMethod::SetPortRanges
virtual void SetPortRanges(WORD portBase, WORD portMax=0, WORD portPairBase=0, WORD portPairMax=0)
Set the port ranges to be used on local machine.
PTimeInterval
This class defines an arbitrary time interval to millisecond accuracy.
Definition: timeint.h:55
pluginmgr.h
PNatStrategy::GetMethod
PNatMethod * GetMethod(const PIPSocket::Address &address=PIPSocket::GetDefaultIpAny())
GetMethod This function retrieves the first available NAT Traversal Method.
PDevicePluginServiceDescriptor
Definition: plugin.h:109
PUDPSocket
A socket channel that uses the UDP transport on the Internet Protocol.
Definition: udpsock.h:47
PPluginManager
Definition: pluginmgr.h:57
PNatMethodServiceDescriptor
Definition: pnat.h:344
PSTUNClient
STUN client.
Definition: pstun.h:68
plugin.h
PNatMethod::GetName
virtual PString GetName() const =0
Get the NAT traversal method Name.
PNatMethod::PortInfo::mutex
PMutex mutex
Definition: pnat.h:237
PNatStrategy
PNatStrategy The main container for all NAT traversal Strategies.
Definition: pnat.h:263
PStringArray
This is an array collection class of PString objects.
Definition: pstring.h:2024
PNatMethod::NumRTPSupportTypes
Definition: pnat.h:200
PNatMethod::Activate
virtual void Activate(bool active)
Acrivate Activate/DeActivate the NAT Method on a call by call basis Default does notthing.
PNatStrategy::LoadNatMethod
PNatMethod * LoadNatMethod(const PString &name)
PBoolean
BOOL PBoolean
Definition: object.h:102
PNatMethod::CreateSocketPair
virtual PBoolean CreateSocketPair(PUDPSocket *&socket1, PUDPSocket *&socket2, const PIPSocket::Address &binding=PIPSocket::GetDefaultIpAny())=0
Create a socket pair.
PNatMethod::RTPSupportTypes
RTPSupportTypes
Definition: pnat.h:195
PNatMethod
PNatMethod Base Network Address Traversal Method class All NAT Traversal Methods are derived off this...
Definition: pnat.h:49
PNatMethod::IsAvailable
virtual bool IsAvailable(const PIPSocket::Address &binding=PIPSocket::GetDefaultIpAny())=0
Returns whether the Nat Method is ready and available in assisting in NAT Traversal.
PNatStrategy::GetRegisteredList
static PStringArray GetRegisteredList()
PLIST
PLIST(PNatList, PNatMethod)
PNatMethod::CreateSocket
virtual PBoolean CreateSocket(PUDPSocket *&socket, const PIPSocket::Address &binding=PIPSocket::GetDefaultIpAny(), WORD localPort=0)=0
Create a single socket.
PNatMethod::RandomPortPair
WORD RandomPortPair(unsigned int start, unsigned int end)
RandomPortPair This function returns a random port pair base number in the specified range for the cr...
PNatStrategy::AddMethod
void AddMethod(PNatMethod *method)
AddMethod This function is used to add the required NAT Traversal Method.
PNatMethod::GetServer
virtual PString GetServer() const
Get the current server address name.
PNatMethod::RTPSupported
Definition: pnat.h:196
PNatMethod::PortInfo::maxPort
WORD maxPort
Definition: pnat.h:239
PNatStrategy::RemoveMethod
PBoolean RemoveMethod(const PString &meth)
RemoveMethod This function removes a NAT method from the NATlist matching the supplied method name...
PString
The character string class.
Definition: pstring.h:108
PNatMethod::GetRTPSupport
virtual RTPSupportTypes GetRTPSupport(PBoolean force=false)=0
Return an indication if the current STUN type supports RTP Use the force variable to guarantee an up ...
PNatMethod::RTPUnsupported
Definition: pnat.h:198
PNatMethod::SetAlternateAddresses
virtual void SetAlternateAddresses(const PStringArray &addresses, void *userData=NULL)
Set Alternate Candidate (ICE) or probe (H.460.24A) addresses.
PNatMethod::GetExternalAddress
virtual PBoolean GetExternalAddress(PIPSocket::Address &externalAddress, const PTimeInterval &maxAge=1000)=0
Get the acquired External IP Address.
PNatStrategy::PNatStrategy
PNatStrategy()
Default Contructor.
PNatMethod::PrintOn
virtual void PrintOn(ostream &strm) const
Output the contents of the object to the stream.
PNatMethod::singlePortInfo
struct PNatMethod::PortInfo singlePortInfo
PNatMethod::pairedPortInfo
struct PNatMethod::PortInfo pairedPortInfo
PNatStrategy::~PNatStrategy
~PNatStrategy()
Deconstructor.
PNatMethod::GetServerAddress
virtual bool GetServerAddress(PIPSocket::Address &address, WORD &port) const =0
Get the current server address and port being used.
PIPSocket::GetDefaultIpAny
static PIPSocket::Address GetDefaultIpAny()
PNatStrategy::GetMethodByName
PNatMethod * GetMethodByName(const PString &name)
GetMethodByName This function retrieves the NAT Traversal Method with the given name.
PNatMethodServiceDescriptor::GetDeviceNames
virtual PStringArray GetDeviceNames(int) const
Definition: pnat.h:348
PIPSocket::Address
A class describing an IP address.
Definition: ipsock.h:75
PNatMethod::PNatMethod
PNatMethod()
Default Contructor.
PNatMethod::PortInfo::PortInfo
PortInfo(WORD port=0)
Definition: pnat.h:230
PNatStrategy::SetPortRanges
void SetPortRanges(WORD portBase, WORD portMax=0, WORD portPairBase=0, WORD portPairMax=0)
Set the port ranges to be used on local machine.
PNatMethod::~PNatMethod
~PNatMethod()
Deconstructor.
PNatMethod::RTPUnknown
Definition: pnat.h:199
PNatMethodServiceDescriptor::ValidateDeviceName
virtual bool ValidateDeviceName(const PString &deviceName, int) const
Definition: pnat.h:349
sockets.h
PMutex
Synonym for PTimedMutex.
PNatMethodServiceDescriptor::CreateInstance
virtual PObject * CreateInstance(int) const
Definition: pnat.h:347
PNatMethod::PortInfo::basePort
WORD basePort
Definition: pnat.h:238
PNatMethod::PortInfo::currentPort
WORD currentPort
Definition: pnat.h:240
PObject
Ultimate parent class for all objects in the class library.
Definition: object.h:1118
PNatMethod::PortInfo
Definition: pnat.h:229
PNatMethod::Create
static PNatMethod * Create(const PString &name, PPluginManager *pluginMgr=NULL)
Factory Create.
PFACTORY_LOAD
PFACTORY_LOAD(PSTUNClient)
PNatStrategy::GetNATList
PNatList & GetNATList()
Get Loaded NAT Method List.
Definition: pnat.h:326