pstun.h
Go to the documentation of this file.
/*
* pstun.h
*
* STUN client
*
* Portable Windows Library
*
* Copyright (c) 2003 Equivalence Pty. Ltd.
*
* 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 Portable Windows Library.
*
* The Initial Developer of the Original Code is Equivalence Pty. Ltd.
*
* Contributor(s): ______________________________________.
*
* $Revision: 24177 $
* $Author: rjongbloed $
* $Date: 2010-04-05 06:52:04 -0500 (Mon, 05 Apr 2010) $
*/
#ifndef PTLIB_PSTUN_H
#define PTLIB_PSTUN_H
#ifdef P_USE_PRAGMA
#pragma interface
#endif
#include <ptclib/pnat.h>
#include <ptlib/sockets.h>
class PSTUNUDPSocket : public PUDPSocket
{
PCLASSINFO(PSTUNUDPSocket, PUDPSocket);
public:
PSTUNUDPSocket();
virtual PBoolean GetLocalAddress(
Address & addr
);
virtual PBoolean GetLocalAddress(
Address & addr,
WORD & port
);
protected:
PIPSocket::Address externalIP;
friend class PSTUNClient;
};
class PSTUNClient : public PNatMethod
{
PCLASSINFO(PSTUNClient, PNatMethod);
public:
enum {
DefaultPort = 3478
};
PSTUNClient();
PSTUNClient(
const PString & server,
WORD portBase = 0,
WORD portMax = 0,
WORD portPairBase = 0,
WORD portPairMax = 0
);
PSTUNClient(
const PIPSocket::Address & serverAddress,
WORD serverPort = DefaultPort,
WORD portBase = 0,
WORD portMax = 0,
WORD portPairBase = 0,
WORD portPairMax = 0
);
void Initialise(
const PString & server,
WORD portBase = 0,
WORD portMax = 0,
WORD portPairBase = 0,
WORD portPairMax = 0
);
static PStringList GetNatMethodName() { return PStringList("STUN"); }
virtual PString GetName() const { return "STUN"; }
virtual bool GetServerAddress(
PIPSocket::Address & address,
WORD & port
) const;
PBoolean SetServer(
const PString & server
);
PBoolean SetServer(
const PIPSocket::Address & serverAddress,
WORD serverPort = 0
);
enum NatTypes {
UnknownNat,
OpenNat,
ConeNat,
RestrictedNat,
PortRestrictedNat,
SymmetricNat,
SymmetricFirewall,
BlockedNat,
PartialBlockedNat,
NumNatTypes
};
NatTypes GetNatType(
PBoolean force = false
);
PString GetNatTypeName(
PBoolean force = false
) { return GetNatTypeString(GetNatType(force)); }
static PString GetNatTypeString(
NatTypes type
);
RTPSupportTypes GetRTPSupport(
PBoolean force = false
);
virtual PBoolean GetExternalAddress(
PIPSocket::Address & externalAddress,
const PTimeInterval & maxAge = 1000
);
virtual bool GetInterfaceAddress(
PIPSocket::Address & internalAddress
) const;
void InvalidateCache();
PBoolean CreateSocket(
PUDPSocket * & socket,
const PIPSocket::Address & binding = PIPSocket::GetDefaultIpAny(),
WORD localPort = 0
);
virtual PBoolean CreateSocketPair(
PUDPSocket * & socket1,
PUDPSocket * & socket2,
const PIPSocket::Address & binding = PIPSocket::GetDefaultIpAny()
);
const PTimeInterval GetTimeout() const { return replyTimeout; }
void SetTimeout(
const PTimeInterval & timeout
) { replyTimeout = timeout; }
PINDEX GetRetries() const { return pollRetries; }
void SetRetries(
PINDEX retries
) { pollRetries = retries; }
PINDEX GetSocketsForPairing() const { return numSocketsForPairing; }
void SetSocketsForPairing(
PINDEX numSockets
) { numSocketsForPairing = numSockets; }
virtual bool IsAvailable(
const PIPSocket::Address & binding = PIPSocket::GetDefaultIpAny()
);
protected:
PString serverHost;
WORD serverPort;
PTimeInterval replyTimeout;
PINDEX pollRetries;
PINDEX numSocketsForPairing;
bool OpenSocket(PUDPSocket & socket, PortInfo & portInfo, const PIPSocket::Address & binding);
NatTypes natType;
PIPSocket::Address cachedServerAddress;
PIPSocket::Address cachedExternalAddress;
PIPSocket::Address interfaceAddress;
PTime timeAddressObtained;
};
inline ostream & operator<<(ostream & strm, PSTUNClient::NatTypes type) { return strm << PSTUNClient::GetNatTypeString(type); }
#endif // PTLIB_PSTUN_H
// End of file ////////////////////////////////////////////////////////////////
pnat.h
PSTUNClient::IsAvailable
virtual bool IsAvailable(const PIPSocket::Address &binding=PIPSocket::GetDefaultIpAny())
Returns whether the Nat Method is ready and available in assisting in NAT Traversal.
PSTUNUDPSocket::PSTUNUDPSocket
PSTUNUDPSocket()
PTimeInterval
This class defines an arbitrary time interval to millisecond accuracy.
Definition: timeint.h:55
PSTUNClient::GetSocketsForPairing
PINDEX GetSocketsForPairing() const
Get the number of sockets to create in attempt to get a port pair.
Definition: pstun.h:263
PSTUNClient::NumNatTypes
Definition: pstun.h:146
PSTUNClient::GetServerAddress
virtual bool GetServerAddress(PIPSocket::Address &address, WORD &port) const
Get the current server address and port being used.
PTime
This class defines an absolute time and date.
Definition: ptime.h:53
PSTUNClient::GetNatMethodName
static PStringList GetNatMethodName()
Get the NAT Method Name.
Definition: pstun.h:105
PSTUNClient::CreateSocketPair
virtual PBoolean CreateSocketPair(PUDPSocket *&socket1, PUDPSocket *&socket2, const PIPSocket::Address &binding=PIPSocket::GetDefaultIpAny())
Create a socket pair.
PUDPSocket
A socket channel that uses the UDP transport on the Internet Protocol.
Definition: udpsock.h:47
PSTUNClient::CreateSocket
PBoolean CreateSocket(PUDPSocket *&socket, const PIPSocket::Address &binding=PIPSocket::GetDefaultIpAny(), WORD localPort=0)
Create a single socket.
PSTUNClient
STUN client.
Definition: pstun.h:68
PSTUNClient::numSocketsForPairing
PINDEX numSocketsForPairing
Definition: pstun.h:290
PSTUNClient::UnknownNat
Definition: pstun.h:137
PIPSocket::GetLocalAddress
virtual PString GetLocalAddress()
Get the Internet Protocol address and port for the local host.
PSTUNClient::SetRetries
void SetRetries(PINDEX retries)
Set the number of retries for responses from STUN server.
Definition: pstun.h:254
PSTUNClient::SetSocketsForPairing
void SetSocketsForPairing(PINDEX numSockets)
Set the number of sockets to create in attempt to get a port pair.
Definition: pstun.h:270
PSTUNClient::SetTimeout
void SetTimeout(const PTimeInterval &timeout)
Set the timeout for responses from STUN server.
Definition: pstun.h:244
PSTUNClient::SetServer
PBoolean SetServer(const PString &server)
Set the STUN server to use.
PSTUNClient::ConeNat
Definition: pstun.h:139
PSocket::port
WORD port
Port to be used by the socket when opening the channel.
Definition: socket.h:444
PSTUNClient::PartialBlockedNat
Definition: pstun.h:145
PSTUNClient::cachedExternalAddress
PIPSocket::Address cachedExternalAddress
Definition: pstun.h:296
PBoolean
BOOL PBoolean
Definition: object.h:102
PSTUNClient::GetNatTypeString
static PString GetNatTypeString(NatTypes type)
Get NatTypes enumeration as an English string for the type.
PSTUNClient::GetExternalAddress
virtual PBoolean GetExternalAddress(PIPSocket::Address &externalAddress, const PTimeInterval &maxAge=1000)
Determine the external router address.
PSTUNClient::replyTimeout
PTimeInterval replyTimeout
Definition: pstun.h:288
PSTUNClient::natType
NatTypes natType
Definition: pstun.h:294
PNatMethod::RTPSupportTypes
RTPSupportTypes
Definition: pnat.h:195
PSTUNClient::cachedServerAddress
PIPSocket::Address cachedServerAddress
Definition: pstun.h:295
PSTUNClient::serverPort
WORD serverPort
Definition: pstun.h:287
PNatMethod
PNatMethod Base Network Address Traversal Method class All NAT Traversal Methods are derived off this...
Definition: pnat.h:49
PSTUNClient::OpenSocket
bool OpenSocket(PUDPSocket &socket, PortInfo &portInfo, const PIPSocket::Address &binding)
PSTUNClient::pollRetries
PINDEX pollRetries
Definition: pstun.h:289
PSTUNClient::GetInterfaceAddress
virtual bool GetInterfaceAddress(PIPSocket::Address &internalAddress) const
Return the interface NAT router is using.
PSTUNClient::Initialise
void Initialise(const PString &server, WORD portBase=0, WORD portMax=0, WORD portPairBase=0, WORD portPairMax=0)
PSTUNClient::serverHost
PString serverHost
Definition: pstun.h:286
PSTUNClient::GetRetries
PINDEX GetRetries() const
Get the number of retries for responses from STUN server.
Definition: pstun.h:250
PSTUNClient::SymmetricNat
Definition: pstun.h:142
PString
The character string class.
Definition: pstring.h:108
PSTUNClient::GetNatTypeName
PString GetNatTypeName(PBoolean force=false)
Determine via the STUN protocol the NAT type for the router.
Definition: pstun.h:160
PSTUNClient::DefaultPort
Definition: pstun.h:73
PSTUNClient::GetRTPSupport
RTPSupportTypes GetRTPSupport(PBoolean force=false)
Return an indication if the current STUN type supports RTP Use the force variable to guarantee an up ...
PSTUNClient::SymmetricFirewall
Definition: pstun.h:143
PStringList
This is a list collection class of PString objects.
Definition: pstring.h:2184
PIPSocket::GetDefaultIpAny
static PIPSocket::Address GetDefaultIpAny()
PSTUNClient::interfaceAddress
PIPSocket::Address interfaceAddress
Definition: pstun.h:297
PIPSocket::Address
A class describing an IP address.
Definition: ipsock.h:75
PSTUNClient::PortRestrictedNat
Definition: pstun.h:141
PSTUNClient::BlockedNat
Definition: pstun.h:144
PSTUNClient::GetNatType
NatTypes GetNatType(PBoolean force=false)
Determine via the STUN protocol the NAT type for the router.
PSTUNClient::NatTypes
NatTypes
Definition: pstun.h:136
PSTUNClient::timeAddressObtained
PTime timeAddressObtained
Definition: pstun.h:298
PSTUNClient::OpenNat
Definition: pstun.h:138
sockets.h
PSTUNClient::GetTimeout
const PTimeInterval GetTimeout() const
Get the timeout for responses from STUN server.
Definition: pstun.h:240
operator<<
ostream & operator<<(ostream &strm, PSTUNClient::NatTypes type)
Definition: pstun.h:302
PSTUNClient::InvalidateCache
void InvalidateCache()
Invalidates the cached addresses and modes.
PSTUNClient::RestrictedNat
Definition: pstun.h:140
PSTUNClient::GetName
virtual PString GetName() const
Get the NAT traversal method name.
Definition: pstun.h:109
PSTUNUDPSocket::externalIP
PIPSocket::Address externalIP
Definition: pstun.h:60
PSTUNUDPSocket
UDP socket that has been created by the STUN client.
Definition: pstun.h:45
PNatMethod::PortInfo
Definition: pnat.h:229
PSTUNClient::PSTUNClient
PSTUNClient()