PTLib  Version 2.10.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 ///////////////////////////////////////////////////////////////