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 #ifndef _PSTUN_H
00072 #define _PSTUN_H
00073
00074 #ifdef P_USE_PRAGMA
00075 #pragma interface
00076 #endif
00077
00078 #include <ptlib.h>
00079 #include <ptclib/pnat.h>
00080 #include <ptlib/sockets.h>
00081
00082
00085 class PSTUNUDPSocket : public PUDPSocket
00086 {
00087 PCLASSINFO(PSTUNUDPSocket, PUDPSocket);
00088 public:
00089 PSTUNUDPSocket();
00090
00091 virtual BOOL GetLocalAddress(
00092 Address & addr
00093 );
00094 virtual BOOL GetLocalAddress(
00095 Address & addr,
00096 WORD & port
00097 );
00098
00099 protected:
00100 PIPSocket::Address externalIP;
00101
00102 friend class PSTUNClient;
00103 };
00104
00105
00108 class PSTUNClient : public PNatMethod
00109 {
00110 PCLASSINFO(PSTUNClient, PNatMethod);
00111 public:
00112 enum {
00113 DefaultPort = 3478
00114 };
00115
00116 PSTUNClient(
00117 const PString & server,
00118 WORD portBase = 0,
00119 WORD portMax = 0,
00120 WORD portPairBase = 0,
00121 WORD portPairMax = 0
00122 );
00123 PSTUNClient(
00124 const PIPSocket::Address & serverAddress,
00125 WORD serverPort = DefaultPort,
00126 WORD portBase = 0,
00127 WORD portMax = 0,
00128 WORD portPairBase = 0,
00129 WORD portPairMax = 0
00130 );
00131
00132
00135 PString GetServer() const;
00136
00143 BOOL SetServer(
00144 const PString & server
00145 );
00146
00150 BOOL SetServer(
00151 const PIPSocket::Address & serverAddress,
00152 WORD serverPort = 0
00153 );
00154
00155 enum NatTypes {
00156 UnknownNat,
00157 OpenNat,
00158 ConeNat,
00159 RestrictedNat,
00160 PortRestrictedNat,
00161 SymmetricNat,
00162 SymmetricFirewall,
00163 BlockedNat,
00164 PartialBlockedNat,
00165 NumNatTypes
00166 };
00167
00172 NatTypes GetNatType(
00173 BOOL force = FALSE
00174 );
00175
00179 PString GetNatTypeName(
00180 BOOL force = FALSE
00181 ) { return GetNatTypeString(GetNatType(force)); }
00182
00185 static PString GetNatTypeString(
00186 NatTypes type
00187 );
00188
00189 enum RTPSupportTypes {
00190 RTPOK,
00191 RTPUnknown,
00192 RTPUnsupported,
00193 RTPIfSendMedia
00194 };
00195
00199 RTPSupportTypes IsSupportingRTP(
00200 BOOL force = FALSE
00201 );
00202
00210 virtual BOOL GetExternalAddress(
00211 PIPSocket::Address & externalAddress,
00212 const PTimeInterval & maxAge = 1000
00213 );
00214
00227 BOOL CreateSocket(
00228 PUDPSocket * & socket
00229 );
00230
00244 virtual BOOL CreateSocketPair(
00245 PUDPSocket * & socket1,
00246 PUDPSocket * & socket2
00247 );
00248
00251 const PTimeInterval GetTimeout() const { return replyTimeout; }
00252
00255 void SetTimeout(
00256 const PTimeInterval & timeout
00257 ) { replyTimeout = timeout; }
00258
00261 PINDEX GetRetries() const { return pollRetries; }
00262
00265 void SetRetries(
00266 PINDEX retries
00267 ) { pollRetries = retries; }
00268
00274 PINDEX GetSocketsForPairing() const { return numSocketsForPairing; }
00275
00281 void SetSocketsForPairing(
00282 PINDEX numSockets
00283 ) { numSocketsForPairing = numSockets; }
00284
00292 virtual BOOL IsAvailable();
00293
00294 protected:
00295 PIPSocket::Address serverAddress;
00296 WORD serverPort;
00297 PTimeInterval replyTimeout;
00298 PINDEX pollRetries;
00299 PINDEX numSocketsForPairing;
00300
00301 bool OpenSocket(PUDPSocket & socket, PortInfo & portInfo) const;
00302
00303 NatTypes natType;
00304 PIPSocket::Address cachedExternalAddress;
00305 PTime timeAddressObtained;
00306 };
00307
00308
00309 inline ostream & operator<<(ostream & strm, PSTUNClient::NatTypes type) { return strm << PSTUNClient::GetNatTypeString(type); }
00310
00311
00312 #endif // _PSTUN_H
00313
00314
00315