PTLib  Version 2.10.10
pnat.h
Go to the documentation of this file.
1 /*
2  * pnat.h
3  *
4  * NAT Strategy support for Portable Windows Library.
5  *
6  * Virteos is a Trade Mark of ISVO (Asia) Pte Ltd.
7  *
8  * Copyright (c) 2004 ISVO (Asia) Pte Ltd. All Rights Reserved.
9  *
10  * The contents of this file are subject to the Mozilla Public License
11  * Version 1.0 (the "License"); you may not use this file except in
12  * compliance with the License. You may obtain a copy of the License at
13  * http://www.mozilla.org/MPL/
14  *
15  * Software distributed under the License is distributed on an "AS IS"
16  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
17  * the License for the specific language governing rights and limitations
18  * under the License.
19  *
20  *
21  * The Original Code is derived from and used in conjunction with the
22  * OpenH323 Project (www.openh323.org/)
23  *
24  * The Initial Developer of the Original Code is ISVO (Asia) Pte Ltd.
25  *
26  *
27  * Contributor(s): ______________________________________.
28  *
29  * $Revision: 26549 $
30  * $Author: rjongbloed $
31  * $Date: 2011-10-05 23:24:38 -0500 (Wed, 05 Oct 2011) $
32  */
33 
34 #include <ptlib/sockets.h>
35 
36 #ifndef PTLIB_PNAT_H
37 #define PTLIB_PNAT_H
38 
39 #include <ptlib/plugin.h>
40 #include <ptlib/pluginmgr.h>
41 
49 class PNatMethod : public PObject
50 {
51  PCLASSINFO(PNatMethod,PObject);
52 
53  public:
58  PNatMethod();
59 
62  ~PNatMethod();
64 
65 
68  virtual void PrintOn(
69  ostream & strm
70  ) const;
72 
73 
78  static PNatMethod * Create(
79  const PString & name,
80  PPluginManager * pluginMgr = NULL
81  );
82 
85  virtual PString GetName() const = 0;
86 
90  virtual PString GetServer() const;
91 
94  virtual bool GetServerAddress(
95  PIPSocket::Address & address,
96  WORD & port
97  ) const = 0;
98 
102  PIPSocket::Address & externalAddress,
103  const PTimeInterval & maxAge = 1000
104  ) = 0;
105 
108  virtual bool GetInterfaceAddress(
109  PIPSocket::Address & internalAddress
110  ) const = 0;
111 
125  virtual PBoolean CreateSocket(
126  PUDPSocket * & socket,
128  WORD localPort = 0
129  ) = 0;
130 
144  virtual PBoolean CreateSocketPair(
145  PUDPSocket * & socket1,
146  PUDPSocket * & socket2,
148  ) = 0;
149 
163  virtual PBoolean CreateSocketPair(
164  PUDPSocket * & socket1,
165  PUDPSocket * & socket2,
166  const PIPSocket::Address & binding,
167  void * userData
168  );
169 
177  virtual bool IsAvailable(
179  ) = 0;
180 
185  virtual void Activate(bool active);
186 
190  virtual void SetAlternateAddresses(
191  const PStringArray & addresses,
192  void * userData = NULL
193  );
194 
201  };
202 
207  PBoolean force = false
208  ) = 0;
209 
220  virtual void SetPortRanges(
221  WORD portBase,
222  WORD portMax = 0,
223  WORD portPairBase = 0,
224  WORD portPairMax = 0
225  );
227 
228  protected:
229  struct PortInfo {
230  PortInfo(WORD port = 0)
231  : basePort(port)
232  , maxPort(port)
233  , currentPort(port)
234  {
235  }
236 
238  WORD basePort;
239  WORD maxPort;
242 
249  WORD RandomPortPair(unsigned int start, unsigned int end);
250 };
251 
253 
254 PLIST(PNatList, PNatMethod);
255 
257 
263 class PNatStrategy : public PObject
264 {
265  PCLASSINFO(PNatStrategy,PObject);
266 
267 public :
268 
273  PNatStrategy();
274 
277  ~PNatStrategy();
279 
287  void AddMethod(PNatMethod * method);
288 
295 
300  PNatMethod * GetMethodByName(const PString & name);
301 
305  PBoolean RemoveMethod(const PString & meth);
306 
317  void SetPortRanges(
318  WORD portBase,
319  WORD portMax = 0,
320  WORD portPairBase = 0,
321  WORD portPairMax = 0
322  );
323 
326  PNatList & GetNATList() { return natlist; };
327 
328  PNatMethod * LoadNatMethod(const PString & name);
329 
331 
333 
334 private:
335  PNatList natlist;
336  PPluginManager * pluginMgr;
337 };
338 
340 //
341 // declare macros and structures needed for NAT plugins
342 //
343 
344 template <class className> class PNatMethodServiceDescriptor : public PDevicePluginServiceDescriptor
345 {
346  public:
347  virtual PObject * CreateInstance(int /*userData*/) const { return (PNatMethod *)new className; }
348  virtual PStringArray GetDeviceNames(int /*userData*/) const { return className::GetNatMethodName(); }
349  virtual bool ValidateDeviceName(const PString & deviceName, int /*userData*/) const {
350  return (deviceName == GetDeviceNames(0)[0]);
351  }
352 };
353 
354 #define PDECLARE_NAT_METHOD(method, cls) PFACTORY_CREATE(PFactory<PNatMethod>, cls, #method)
355 
356 #define PCREATE_NAT_PLUGIN(name) \
357  static PNatMethodServiceDescriptor<PNatMethod_##name> PNatMethod_##name##_descriptor; \
358  PCREATE_PLUGIN_STATIC(name, PNatMethod, &PNatMethod_##name##_descriptor)
359 
360 
361 #if P_STUN
363 #endif
364 
365 #if P_TURN
366 PFACTORY_LOAD(PTURNClient);
367 #endif
368 
369 
370 #endif // PTLIB_PNAT_H
371 
372 
373 // End Of File ///////////////////////////////////////////////////////////////
virtual bool GetInterfaceAddress(PIPSocket::Address &internalAddress) const =0
Return the interface NAT router is using.
Definition: pnat.h:197
virtual void SetPortRanges(WORD portBase, WORD portMax=0, WORD portPairBase=0, WORD portPairMax=0)
Set the port ranges to be used on local machine.
This class defines an arbitrary time interval to millisecond accuracy.
Definition: timeint.h:55
PNatMethod * GetMethod(const PIPSocket::Address &address=PIPSocket::GetDefaultIpAny())
GetMethod This function retrieves the first available NAT Traversal Method.
Definition: plugin.h:109
A socket channel that uses the UDP transport on the Internet Protocol.
Definition: udpsock.h:47
Definition: pluginmgr.h:57
Definition: pnat.h:344
STUN client.
Definition: pstun.h:68
virtual PString GetName() const =0
Get the NAT traversal method Name.
PMutex mutex
Definition: pnat.h:237
PNatStrategy The main container for all NAT traversal Strategies.
Definition: pnat.h:263
This is an array collection class of PString objects.
Definition: pstring.h:2024
Definition: pnat.h:200
virtual void Activate(bool active)
Acrivate Activate/DeActivate the NAT Method on a call by call basis Default does notthing.
PNatMethod * LoadNatMethod(const PString &name)
BOOL PBoolean
Definition: object.h:102
virtual PBoolean CreateSocketPair(PUDPSocket *&socket1, PUDPSocket *&socket2, const PIPSocket::Address &binding=PIPSocket::GetDefaultIpAny())=0
Create a socket pair.
RTPSupportTypes
Definition: pnat.h:195
PNatMethod Base Network Address Traversal Method class All NAT Traversal Methods are derived off this...
Definition: pnat.h:49
virtual bool IsAvailable(const PIPSocket::Address &binding=PIPSocket::GetDefaultIpAny())=0
Returns whether the Nat Method is ready and available in assisting in NAT Traversal.
static PStringArray GetRegisteredList()
PLIST(PNatList, PNatMethod)
virtual PBoolean CreateSocket(PUDPSocket *&socket, const PIPSocket::Address &binding=PIPSocket::GetDefaultIpAny(), WORD localPort=0)=0
Create a single socket.
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...
void AddMethod(PNatMethod *method)
AddMethod This function is used to add the required NAT Traversal Method.
virtual PString GetServer() const
Get the current server address name.
Definition: pnat.h:196
WORD maxPort
Definition: pnat.h:239
PBoolean RemoveMethod(const PString &meth)
RemoveMethod This function removes a NAT method from the NATlist matching the supplied method name...
The character string class.
Definition: pstring.h:108
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 ...
Definition: pnat.h:198
virtual void SetAlternateAddresses(const PStringArray &addresses, void *userData=NULL)
Set Alternate Candidate (ICE) or probe (H.460.24A) addresses.
virtual PBoolean GetExternalAddress(PIPSocket::Address &externalAddress, const PTimeInterval &maxAge=1000)=0
Get the acquired External IP Address.
PNatStrategy()
Default Contructor.
virtual void PrintOn(ostream &strm) const
Output the contents of the object to the stream.
struct PNatMethod::PortInfo singlePortInfo
struct PNatMethod::PortInfo pairedPortInfo
~PNatStrategy()
Deconstructor.
virtual bool GetServerAddress(PIPSocket::Address &address, WORD &port) const =0
Get the current server address and port being used.
static PIPSocket::Address GetDefaultIpAny()
PNatMethod * GetMethodByName(const PString &name)
GetMethodByName This function retrieves the NAT Traversal Method with the given name.
virtual PStringArray GetDeviceNames(int) const
Definition: pnat.h:348
A class describing an IP address.
Definition: ipsock.h:75
PNatMethod()
Default Contructor.
PortInfo(WORD port=0)
Definition: pnat.h:230
void SetPortRanges(WORD portBase, WORD portMax=0, WORD portPairBase=0, WORD portPairMax=0)
Set the port ranges to be used on local machine.
~PNatMethod()
Deconstructor.
Definition: pnat.h:199
virtual bool ValidateDeviceName(const PString &deviceName, int) const
Definition: pnat.h:349
Synonym for PTimedMutex.
virtual PObject * CreateInstance(int) const
Definition: pnat.h:347
WORD basePort
Definition: pnat.h:238
WORD currentPort
Definition: pnat.h:240
Ultimate parent class for all objects in the class library.
Definition: object.h:1118
Definition: pnat.h:229
static PNatMethod * Create(const PString &name, PPluginManager *pluginMgr=NULL)
Factory Create.
PFACTORY_LOAD(PSTUNClient)
PNatList & GetNATList()
Get Loaded NAT Method List.
Definition: pnat.h:326