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 #ifndef _PSTUN_H
00075 #define _PSTUN_H
00076
00077 #ifdef P_USE_PRAGMA
00078 #pragma interface
00079 #endif
00080
00081 #include <ptlib.h>
00082 #include <ptclib/pnat.h>
00083 #include <ptlib/sockets.h>
00084
00085
00088 class PSTUNUDPSocket : public PUDPSocket
00089 {
00090 PCLASSINFO(PSTUNUDPSocket, PUDPSocket);
00091 public:
00092 PSTUNUDPSocket();
00093
00094 virtual BOOL GetLocalAddress(
00095 Address & addr
00096 );
00097 virtual BOOL GetLocalAddress(
00098 Address & addr,
00099 WORD & port
00100 );
00101
00102 protected:
00103 PIPSocket::Address externalIP;
00104
00105 friend class PSTUNClient;
00106 };
00107
00108
00111 class PSTUNClient : public PNatMethod
00112 {
00113 PCLASSINFO(PSTUNClient, PNatMethod);
00114 public:
00115 enum {
00116 DefaultPort = 3478
00117 };
00118
00119 PSTUNClient();
00120
00121 PSTUNClient(
00122 const PString & server,
00123 WORD portBase = 0,
00124 WORD portMax = 0,
00125 WORD portPairBase = 0,
00126 WORD portPairMax = 0
00127 );
00128 PSTUNClient(
00129 const PIPSocket::Address & serverAddress,
00130 WORD serverPort = DefaultPort,
00131 WORD portBase = 0,
00132 WORD portMax = 0,
00133 WORD portPairBase = 0,
00134 WORD portPairMax = 0
00135 );
00136
00137
00138 void Initialise(const PString & server,
00139 WORD portBase = 0,
00140 WORD portMax = 0,
00141 WORD portPairBase = 0,
00142 WORD portPairMax = 0);
00143
00146 static PStringList GetNatMethodName() { return PStringList("STUN"); }
00147
00148
00151 PString GetServer() const;
00152
00159 BOOL SetServer(
00160 const PString & server
00161 );
00162
00166 BOOL SetServer(
00167 const PIPSocket::Address & serverAddress,
00168 WORD serverPort = 0
00169 );
00170
00171 enum NatTypes {
00172 UnknownNat,
00173 OpenNat,
00174 ConeNat,
00175 RestrictedNat,
00176 PortRestrictedNat,
00177 SymmetricNat,
00178 SymmetricFirewall,
00179 BlockedNat,
00180 PartialBlockedNat,
00181 NumNatTypes
00182 };
00183
00188 NatTypes GetNatType(
00189 BOOL force = FALSE
00190 );
00191
00195 PString GetNatTypeName(
00196 BOOL force = FALSE
00197 ) { return GetNatTypeString(GetNatType(force)); }
00198
00201 static PString GetNatTypeString(
00202 NatTypes type
00203 );
00204
00205 enum RTPSupportTypes {
00206 RTPOK,
00207 RTPUnknown,
00208 RTPUnsupported,
00209 RTPIfSendMedia
00210 };
00211
00215 RTPSupportTypes IsSupportingRTP(
00216 BOOL force = FALSE
00217 );
00218
00226 virtual BOOL GetExternalAddress(
00227 PIPSocket::Address & externalAddress,
00228 const PTimeInterval & maxAge = 1000
00229 );
00230
00243 BOOL CreateSocket(
00244 PUDPSocket * & socket
00245 );
00246
00260 virtual BOOL CreateSocketPair(
00261 PUDPSocket * & socket1,
00262 PUDPSocket * & socket2
00263 );
00264
00267 const PTimeInterval GetTimeout() const { return replyTimeout; }
00268
00271 void SetTimeout(
00272 const PTimeInterval & timeout
00273 ) { replyTimeout = timeout; }
00274
00277 PINDEX GetRetries() const { return pollRetries; }
00278
00281 void SetRetries(
00282 PINDEX retries
00283 ) { pollRetries = retries; }
00284
00290 PINDEX GetSocketsForPairing() const { return numSocketsForPairing; }
00291
00297 void SetSocketsForPairing(
00298 PINDEX numSockets
00299 ) { numSocketsForPairing = numSockets; }
00300
00308 virtual BOOL IsAvailable();
00309
00310 protected:
00311 PIPSocket::Address serverAddress;
00312 WORD serverPort;
00313 PTimeInterval replyTimeout;
00314 PINDEX pollRetries;
00315 PINDEX numSocketsForPairing;
00316
00317 bool OpenSocket(PUDPSocket & socket, PortInfo & portInfo) const;
00318
00319 NatTypes natType;
00320 PIPSocket::Address cachedExternalAddress;
00321 PTime timeAddressObtained;
00322 };
00323
00324
00325 inline ostream & operator<<(ostream & strm, PSTUNClient::NatTypes type) { return strm << PSTUNClient::GetNatTypeString(type); }
00326
00328 typedef PSTUNClient PNatMethod_STUN;
00329 PWLIB_STATIC_LOAD_PLUGIN(STUN, PNatMethod);
00330
00331
00332 #endif // _PSTUN_H
00333
00334
00335