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
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089 #ifndef _PSTUN_H
00090 #define _PSTUN_H
00091
00092 #ifdef P_USE_PRAGMA
00093 #pragma interface
00094 #endif
00095
00096 #ifndef _PTLIB_H
00097 #include <ptlib.h>
00098 #endif
00099
00100 #include <ptclib/pnat.h>
00101 #include <ptlib/sockets.h>
00102
00103
00106 class PSTUNUDPSocket : public PUDPSocket
00107 {
00108 PCLASSINFO(PSTUNUDPSocket, PUDPSocket);
00109 public:
00110 PSTUNUDPSocket();
00111
00112 virtual BOOL GetLocalAddress(
00113 Address & addr
00114 );
00115 virtual BOOL GetLocalAddress(
00116 Address & addr,
00117 WORD & port
00118 );
00119
00120 protected:
00121 PIPSocket::Address externalIP;
00122
00123 friend class PSTUNClient;
00124 };
00125
00126
00129 class PSTUNClient : public PNatMethod
00130 {
00131 PCLASSINFO(PSTUNClient, PNatMethod);
00132 public:
00133 enum {
00134 DefaultPort = 3478
00135 };
00136
00137 PSTUNClient();
00138
00139 PSTUNClient(
00140 const PString & server,
00141 WORD portBase = 0,
00142 WORD portMax = 0,
00143 WORD portPairBase = 0,
00144 WORD portPairMax = 0
00145 );
00146 PSTUNClient(
00147 const PIPSocket::Address & serverAddress,
00148 WORD serverPort = DefaultPort,
00149 WORD portBase = 0,
00150 WORD portMax = 0,
00151 WORD portPairBase = 0,
00152 WORD portPairMax = 0
00153 );
00154
00155
00156 void Initialise(
00157 const PString & server,
00158 WORD portBase = 0,
00159 WORD portMax = 0,
00160 WORD portPairBase = 0,
00161 WORD portPairMax = 0
00162 );
00163
00166 static PStringList GetNatMethodName() { return PStringList("STUN"); }
00167
00168 virtual PStringList GetName() const
00169 { return GetNatMethodName(); }
00170
00173 PString GetServer() const;
00174
00175 void GetServer(PIPSocket::Address & address, WORD & port) const;
00176
00183 BOOL SetServer(
00184 const PString & server
00185 );
00186
00190 BOOL SetServer(
00191 const PIPSocket::Address & serverAddress,
00192 WORD serverPort = 0
00193 );
00194
00195 enum NatTypes {
00196 UnknownNat,
00197 OpenNat,
00198 ConeNat,
00199 RestrictedNat,
00200 PortRestrictedNat,
00201 SymmetricNat,
00202 SymmetricFirewall,
00203 BlockedNat,
00204 PartialBlockedNat,
00205 NumNatTypes
00206 };
00207
00212 NatTypes GetNatType(
00213 BOOL force = FALSE
00214 );
00215
00219 PString GetNatTypeName(
00220 BOOL force = FALSE
00221 ) { return GetNatTypeString(GetNatType(force)); }
00222
00225 static PString GetNatTypeString(
00226 NatTypes type
00227 );
00228
00229 enum RTPSupportTypes {
00230 RTPOK,
00231 RTPUnknown,
00232 RTPUnsupported,
00233 RTPIfSendMedia
00234 };
00235
00239 RTPSupportTypes IsSupportingRTP(
00240 BOOL force = FALSE
00241 );
00242
00250 virtual BOOL GetExternalAddress(
00251 PIPSocket::Address & externalAddress,
00252 const PTimeInterval & maxAge = 1000
00253 );
00254
00267 BOOL CreateSocket(
00268 PUDPSocket * & socket,
00269 const PIPSocket::Address & binding = PIPSocket::GetDefaultIpAny(),
00270 WORD localPort = 0
00271 );
00272
00286 virtual BOOL CreateSocketPair(
00287 PUDPSocket * & socket1,
00288 PUDPSocket * & socket2,
00289 const PIPSocket::Address & binding = PIPSocket::GetDefaultIpAny()
00290 );
00291
00294 const PTimeInterval GetTimeout() const { return replyTimeout; }
00295
00298 void SetTimeout(
00299 const PTimeInterval & timeout
00300 ) { replyTimeout = timeout; }
00301
00304 PINDEX GetRetries() const { return pollRetries; }
00305
00308 void SetRetries(
00309 PINDEX retries
00310 ) { pollRetries = retries; }
00311
00317 PINDEX GetSocketsForPairing() const { return numSocketsForPairing; }
00318
00324 void SetSocketsForPairing(
00325 PINDEX numSockets
00326 ) { numSocketsForPairing = numSockets; }
00327
00335 virtual BOOL IsAvailable();
00336
00337 protected:
00338 PIPSocket::Address serverAddress;
00339 WORD serverPort;
00340 PTimeInterval replyTimeout;
00341 PINDEX pollRetries;
00342 PINDEX numSocketsForPairing;
00343
00344 bool OpenSocket(PUDPSocket & socket, PortInfo & portInfo, const PIPSocket::Address & binding) const;
00345
00346 NatTypes natType;
00347 PIPSocket::Address cachedExternalAddress;
00348 PTime timeAddressObtained;
00349 };
00350
00351
00352 inline ostream & operator<<(ostream & strm, PSTUNClient::NatTypes type) { return strm << PSTUNClient::GetNatTypeString(type); }
00353
00355 typedef PSTUNClient PNatMethod_STUN;
00356 PWLIB_STATIC_LOAD_PLUGIN(STUN, PNatMethod);
00357
00358
00359 #endif // _PSTUN_H
00360
00361
00362