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
00110 virtual PStringList GetName() const
00111 { return GetNatMethodName(); }
00112
00115 PString GetServer() const;
00116
00117 void GetServer(PIPSocket::Address & address, WORD & port) const;
00118
00125 PBoolean SetServer(
00126 const PString & server
00127 );
00128
00132 PBoolean SetServer(
00133 const PIPSocket::Address & serverAddress,
00134 WORD serverPort = 0
00135 );
00136
00137 enum NatTypes {
00138 UnknownNat,
00139 OpenNat,
00140 ConeNat,
00141 RestrictedNat,
00142 PortRestrictedNat,
00143 SymmetricNat,
00144 SymmetricFirewall,
00145 BlockedNat,
00146 PartialBlockedNat,
00147 NumNatTypes
00148 };
00149
00154 NatTypes GetNatType(
00155 PBoolean force = PFalse
00156 );
00157
00161 PString GetNatTypeName(
00162 PBoolean force = PFalse
00163 ) { return GetNatTypeString(GetNatType(force)); }
00164
00167 static PString GetNatTypeString(
00168 NatTypes type
00169 );
00170
00171 enum RTPSupportTypes {
00172 RTPSupported,
00173 RTPIfSendMedia,
00174 RTPUnsupported,
00175 RTPUnknown,
00176 NumRTPSupportTypes
00177 };
00178
00182 RTPSupportTypes GetRTPSupport(
00183 PBoolean force = PFalse
00184 );
00185
00193 virtual PBoolean GetExternalAddress(
00194 PIPSocket::Address & externalAddress,
00195 const PTimeInterval & maxAge = 1000
00196 );
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
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