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 ////////////////////////////////////////////////////////////////
virtual bool IsAvailable(const PIPSocket::Address &binding=PIPSocket::GetDefaultIpAny())
Returns whether the Nat Method is ready and available in assisting in NAT Traversal.
ostream & operator<<(ostream &stream, const PString &string)
Definition: pstring.h:1655
This class defines an arbitrary time interval to millisecond accuracy.
Definition: timeint.h:55
PINDEX GetSocketsForPairing() const
Get the number of sockets to create in attempt to get a port pair.
Definition: pstun.h:263
Definition: pstun.h:146
virtual bool GetServerAddress(PIPSocket::Address &address, WORD &port) const
Get the current server address and port being used.
This class defines an absolute time and date.
Definition: ptime.h:53
static PStringList GetNatMethodName()
Get the NAT Method Name.
Definition: pstun.h:105
virtual PBoolean CreateSocketPair(PUDPSocket *&socket1, PUDPSocket *&socket2, const PIPSocket::Address &binding=PIPSocket::GetDefaultIpAny())
Create a socket pair.
A socket channel that uses the UDP transport on the Internet Protocol.
Definition: udpsock.h:47
PBoolean CreateSocket(PUDPSocket *&socket, const PIPSocket::Address &binding=PIPSocket::GetDefaultIpAny(), WORD localPort=0)
Create a single socket.
STUN client.
Definition: pstun.h:68
PINDEX numSocketsForPairing
Definition: pstun.h:290
Definition: pstun.h:137
virtual PString GetLocalAddress()
Get the Internet Protocol address and port for the local host.
void SetRetries(PINDEX retries)
Set the number of retries for responses from STUN server.
Definition: pstun.h:254
void SetSocketsForPairing(PINDEX numSockets)
Set the number of sockets to create in attempt to get a port pair.
Definition: pstun.h:270
void SetTimeout(const PTimeInterval &timeout)
Set the timeout for responses from STUN server.
Definition: pstun.h:244
PBoolean SetServer(const PString &server)
Set the STUN server to use.
Definition: pstun.h:139
WORD port
Port to be used by the socket when opening the channel.
Definition: socket.h:444
Definition: pstun.h:145
PIPSocket::Address cachedExternalAddress
Definition: pstun.h:296
BOOL PBoolean
Definition: object.h:102
static PString GetNatTypeString(NatTypes type)
Get NatTypes enumeration as an English string for the type.
virtual PBoolean GetExternalAddress(PIPSocket::Address &externalAddress, const PTimeInterval &maxAge=1000)
Determine the external router address.
PTimeInterval replyTimeout
Definition: pstun.h:288
NatTypes natType
Definition: pstun.h:294
RTPSupportTypes
Definition: pnat.h:195
PIPSocket::Address cachedServerAddress
Definition: pstun.h:295
WORD serverPort
Definition: pstun.h:287
PNatMethod Base Network Address Traversal Method class All NAT Traversal Methods are derived off this...
Definition: pnat.h:49
bool OpenSocket(PUDPSocket &socket, PortInfo &portInfo, const PIPSocket::Address &binding)
PINDEX pollRetries
Definition: pstun.h:289
virtual bool GetInterfaceAddress(PIPSocket::Address &internalAddress) const
Return the interface NAT router is using.
void Initialise(const PString &server, WORD portBase=0, WORD portMax=0, WORD portPairBase=0, WORD portPairMax=0)
PString serverHost
Definition: pstun.h:286
PINDEX GetRetries() const
Get the number of retries for responses from STUN server.
Definition: pstun.h:250
Definition: pstun.h:142
The character string class.
Definition: pstring.h:108
PString GetNatTypeName(PBoolean force=false)
Determine via the STUN protocol the NAT type for the router.
Definition: pstun.h:160
Definition: pstun.h:73
RTPSupportTypes GetRTPSupport(PBoolean force=false)
Return an indication if the current STUN type supports RTP Use the force variable to guarantee an up ...
Definition: pstun.h:143
This is a list collection class of PString objects.
Definition: pstring.h:2184
static PIPSocket::Address GetDefaultIpAny()
PIPSocket::Address interfaceAddress
Definition: pstun.h:297
A class describing an IP address.
Definition: ipsock.h:75
Definition: pstun.h:141
Definition: pstun.h:144
NatTypes GetNatType(PBoolean force=false)
Determine via the STUN protocol the NAT type for the router.
NatTypes
Definition: pstun.h:136
PTime timeAddressObtained
Definition: pstun.h:298
Definition: pstun.h:138
const PTimeInterval GetTimeout() const
Get the timeout for responses from STUN server.
Definition: pstun.h:240
void InvalidateCache()
Invalidates the cached addresses and modes.
Definition: pstun.h:140
virtual PString GetName() const
Get the NAT traversal method name.
Definition: pstun.h:109
PIPSocket::Address externalIP
Definition: pstun.h:60
UDP socket that has been created by the STUN client.
Definition: pstun.h:45
Definition: pnat.h:229