PTLib  Version 2.10.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
pstun.h
Go to the documentation of this file.
1 /*
2  * pstun.h
3  *
4  * STUN client
5  *
6  * Portable Windows Library
7  *
8  * Copyright (c) 2003 Equivalence Pty. Ltd.
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  * The Original Code is Portable Windows Library.
21  *
22  * The Initial Developer of the Original Code is Equivalence Pty. Ltd.
23  *
24  * Contributor(s): ______________________________________.
25  *
26  * $Revision: 24177 $
27  * $Author: rjongbloed $
28  * $Date: 2010-04-05 06:52:04 -0500 (Mon, 05 Apr 2010) $
29  */
30 
31 #ifndef PTLIB_PSTUN_H
32 #define PTLIB_PSTUN_H
33 
34 #ifdef P_USE_PRAGMA
35 #pragma interface
36 #endif
37 
38 
39 #include <ptclib/pnat.h>
40 #include <ptlib/sockets.h>
41 
42 
45 class PSTUNUDPSocket : public PUDPSocket
46 {
47  PCLASSINFO(PSTUNUDPSocket, PUDPSocket);
48  public:
50 
51  virtual PBoolean GetLocalAddress(
52  Address & addr
53  );
54  virtual PBoolean GetLocalAddress(
55  Address & addr,
56  WORD & port
57  );
58 
59  protected:
61 
62  friend class PSTUNClient;
63 };
64 
65 
68 class PSTUNClient : public PNatMethod
69 {
70  PCLASSINFO(PSTUNClient, PNatMethod);
71  public:
72  enum {
73  DefaultPort = 3478
74  };
75 
76  PSTUNClient();
77 
79  const PString & server,
80  WORD portBase = 0,
81  WORD portMax = 0,
82  WORD portPairBase = 0,
83  WORD portPairMax = 0
84  );
86  const PIPSocket::Address & serverAddress,
87  WORD serverPort = DefaultPort,
88  WORD portBase = 0,
89  WORD portMax = 0,
90  WORD portPairBase = 0,
91  WORD portPairMax = 0
92  );
93 
94 
95  void Initialise(
96  const PString & server,
97  WORD portBase = 0,
98  WORD portMax = 0,
99  WORD portPairBase = 0,
100  WORD portPairMax = 0
101  );
102 
105  static PStringList GetNatMethodName() { return PStringList("STUN"); }
106 
109  virtual PString GetName() const { return "STUN"; }
110 
113  virtual bool GetServerAddress(
114  PIPSocket::Address & address,
115  WORD & port
116  ) const;
117 
125  const PString & server
126  );
127 
132  const PIPSocket::Address & serverAddress,
133  WORD serverPort = 0
134  );
135 
136  enum NatTypes {
147  };
148 
154  PBoolean force = false
155  );
156 
161  PBoolean force = false
162  ) { return GetNatTypeString(GetNatType(force)); }
163 
166  static PString GetNatTypeString(
167  NatTypes type
168  );
169 
174  PBoolean force = false
175  );
176 
185  PIPSocket::Address & externalAddress,
186  const PTimeInterval & maxAge = 1000
187  );
188 
191  virtual bool GetInterfaceAddress(
192  PIPSocket::Address & internalAddress
193  ) const;
194 
199  void InvalidateCache();
200 
214  PUDPSocket * & socket,
216  WORD localPort = 0
217  );
218 
232  virtual PBoolean CreateSocketPair(
233  PUDPSocket * & socket1,
234  PUDPSocket * & socket2,
236  );
237 
240  const PTimeInterval GetTimeout() const { return replyTimeout; }
241 
245  const PTimeInterval & timeout
246  ) { replyTimeout = timeout; }
247 
250  PINDEX GetRetries() const { return pollRetries; }
251 
255  PINDEX retries
256  ) { pollRetries = retries; }
257 
263  PINDEX GetSocketsForPairing() const { return numSocketsForPairing; }
264 
271  PINDEX numSockets
272  ) { numSocketsForPairing = numSockets; }
273 
281  virtual bool IsAvailable(
283  );
284 
285  protected:
289  PINDEX pollRetries;
291 
292  bool OpenSocket(PUDPSocket & socket, PortInfo & portInfo, const PIPSocket::Address & binding);
293 
299 };
300 
301 
302 inline ostream & operator<<(ostream & strm, PSTUNClient::NatTypes type) { return strm << PSTUNClient::GetNatTypeString(type); }
303 
304 
305 #endif // PTLIB_PSTUN_H
306 
307 
308 // End of file ////////////////////////////////////////////////////////////////