00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 #ifndef _PSTUN_H
00032 #define _PSTUN_H
00033
00034 #ifdef P_USE_PRAGMA
00035 #pragma interface
00036 #endif
00037
00038 #ifndef _PTLIB_H
00039 #include <ptlib.h>
00040 #endif
00041
00042 #include <ptclib/pnat.h>
00043 #include <ptlib/sockets.h>
00044
00045
00048 class PSTUNUDPSocket : public PUDPSocket
00049 {
00050 PCLASSINFO(PSTUNUDPSocket, PUDPSocket);
00051 public:
00052 PSTUNUDPSocket();
00053
00054 virtual PBoolean GetLocalAddress(
00055 Address & addr
00056 );
00057 virtual PBoolean GetLocalAddress(
00058 Address & addr,
00059 WORD & port
00060 );
00061
00062 protected:
00063 PIPSocket::Address externalIP;
00064
00065 friend class PSTUNClient;
00066 };
00067
00068
00071 class PSTUNClient : public PNatMethod
00072 {
00073 PCLASSINFO(PSTUNClient, PNatMethod);
00074 public:
00075 enum {
00076 DefaultPort = 3478
00077 };
00078
00079 PSTUNClient();
00080
00081 PSTUNClient(
00082 const PString & server,
00083 WORD portBase = 0,
00084 WORD portMax = 0,
00085 WORD portPairBase = 0,
00086 WORD portPairMax = 0
00087 );
00088 PSTUNClient(
00089 const PIPSocket::Address & serverAddress,
00090 WORD serverPort = DefaultPort,
00091 WORD portBase = 0,
00092 WORD portMax = 0,
00093 WORD portPairBase = 0,
00094 WORD portPairMax = 0
00095 );
00096
00097
00098 void Initialise(
00099 const PString & server,
00100 WORD portBase = 0,
00101 WORD portMax = 0,
00102 WORD portPairBase = 0,
00103 WORD portPairMax = 0
00104 );
00105
00108 static PStringList GetNatMethodName() { return PStringList("STUN"); }
00109
00112 virtual PString GetName() const { return "STUN"; }
00113
00116 virtual bool GetServerAddress(
00117 PIPSocket::Address & address,
00118 WORD & port
00119 ) const;
00120
00127 PBoolean SetServer(
00128 const PString & server
00129 );
00130
00134 PBoolean SetServer(
00135 const PIPSocket::Address & serverAddress,
00136 WORD serverPort = 0
00137 );
00138
00139 enum NatTypes {
00140 UnknownNat,
00141 OpenNat,
00142 ConeNat,
00143 RestrictedNat,
00144 PortRestrictedNat,
00145 SymmetricNat,
00146 SymmetricFirewall,
00147 BlockedNat,
00148 PartialBlockedNat,
00149 NumNatTypes
00150 };
00151
00156 NatTypes GetNatType(
00157 PBoolean force = PFalse
00158 );
00159
00163 PString GetNatTypeName(
00164 PBoolean force = PFalse
00165 ) { return GetNatTypeString(GetNatType(force)); }
00166
00169 static PString GetNatTypeString(
00170 NatTypes type
00171 );
00172
00176 RTPSupportTypes GetRTPSupport(
00177 PBoolean force = PFalse
00178 );
00179
00187 virtual PBoolean GetExternalAddress(
00188 PIPSocket::Address & externalAddress,
00189 const PTimeInterval & maxAge = 1000
00190 );
00191
00194 virtual bool GetInterfaceAddress(
00195 PIPSocket::Address & internalAddress
00196 ) const;
00197
00202 void InvalidateExternalAddressCache();
00203
00216 PBoolean CreateSocket(
00217 PUDPSocket * & socket,
00218 const PIPSocket::Address & binding = PIPSocket::GetDefaultIpAny(),
00219 WORD localPort = 0
00220 );
00221
00235 virtual PBoolean CreateSocketPair(
00236 PUDPSocket * & socket1,
00237 PUDPSocket * & socket2,
00238 const PIPSocket::Address & binding = PIPSocket::GetDefaultIpAny()
00239 );
00240
00243 const PTimeInterval GetTimeout() const { return replyTimeout; }
00244
00247 void SetTimeout(
00248 const PTimeInterval & timeout
00249 ) { replyTimeout = timeout; }
00250
00253 PINDEX GetRetries() const { return pollRetries; }
00254
00257 void SetRetries(
00258 PINDEX retries
00259 ) { pollRetries = retries; }
00260
00266 PINDEX GetSocketsForPairing() const { return numSocketsForPairing; }
00267
00273 void SetSocketsForPairing(
00274 PINDEX numSockets
00275 ) { numSocketsForPairing = numSockets; }
00276
00284 virtual bool IsAvailable(
00285 const PIPSocket::Address & binding = PIPSocket::GetDefaultIpAny()
00286 );
00287
00288 protected:
00289 PIPSocket::Address serverAddress;
00290 WORD serverPort;
00291 PTimeInterval replyTimeout;
00292 PINDEX pollRetries;
00293 PINDEX numSocketsForPairing;
00294
00295 bool OpenSocket(PUDPSocket & socket, PortInfo & portInfo, const PIPSocket::Address & binding) const;
00296
00297 NatTypes natType;
00298 PIPSocket::Address cachedExternalAddress;
00299 PIPSocket::Address interfaceAddress;
00300 PTime timeAddressObtained;
00301 };
00302
00303
00304 inline ostream & operator<<(ostream & strm, PSTUNClient::NatTypes type) { return strm << PSTUNClient::GetNatTypeString(type); }
00305
00307 typedef PSTUNClient PNatMethod_STUN;
00308 PWLIB_STATIC_LOAD_PLUGIN(STUN, PNatMethod);
00309
00310
00311 #endif // _PSTUN_H
00312
00313
00314