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 #ifndef _PSTUN_H
00058 #define _PSTUN_H
00059
00060 #ifdef P_USE_PRAGMA
00061 #pragma interface
00062 #endif
00063
00064 #include <ptlib.h>
00065 #include <ptlib/sockets.h>
00066
00067
00070 class PSTUNUDPSocket : public PUDPSocket
00071 {
00072 PCLASSINFO(PSTUNUDPSocket, PUDPSocket);
00073 public:
00074 PSTUNUDPSocket();
00075
00076 virtual BOOL GetLocalAddress(
00077 Address & addr
00078 );
00079 virtual BOOL GetLocalAddress(
00080 Address & addr,
00081 WORD & port
00082 );
00083
00084 protected:
00085 PIPSocket::Address externalIP;
00086
00087 friend class PSTUNClient;
00088 };
00089
00090
00093 class PSTUNClient : public PObject
00094 {
00095 PCLASSINFO(PSTUNClient, PObject);
00096 public:
00097 enum {
00098 DefaultPort = 3478
00099 };
00100
00101 PSTUNClient(
00102 const PString & server,
00103 WORD portBase = 0,
00104 WORD portMax = 0,
00105 WORD portPairBase = 0,
00106 WORD portPairMax = 0
00107 );
00108 PSTUNClient(
00109 const PIPSocket::Address & serverAddress,
00110 WORD serverPort = DefaultPort,
00111 WORD portBase = 0,
00112 WORD portMax = 0,
00113 WORD portPairBase = 0,
00114 WORD portPairMax = 0
00115 );
00116
00117
00120 PString GetServer() const;
00121
00128 BOOL SetServer(
00129 const PString & server
00130 );
00131
00135 BOOL SetServer(
00136 const PIPSocket::Address & serverAddress,
00137 WORD serverPort = 0
00138 );
00139
00150 void SetPortRanges(
00151 WORD portBase,
00152 WORD portMax = 0,
00153 WORD portPairBase = 0,
00154 WORD portPairMax = 0
00155 );
00156
00157
00158 enum NatTypes {
00159 UnknownNat,
00160 OpenNat,
00161 ConeNat,
00162 RestrictedNat,
00163 PortRestrictedNat,
00164 SymmetricNat,
00165 SymmetricFirewall,
00166 BlockedNat,
00167 PartialBlockedNat,
00168 NumNatTypes
00169 };
00170
00175 NatTypes GetNatType(
00176 BOOL force = FALSE
00177 );
00178
00179 enum RTPSupportTypes {
00180 RTPOK,
00181 RTPUnknown,
00182 RTPUnsupported,
00183 RTPIfSendMedia
00184 };
00185
00189 RTPSupportTypes IsSupportingRTP(
00190 BOOL force = FALSE
00191 );
00192
00196 PString GetNatTypeName(
00197 BOOL force = FALSE
00198 );
00199
00207 BOOL GetExternalAddress(
00208 PIPSocket::Address & externalAddress,
00209 const PTimeInterval & maxAge = 1000
00210 );
00211
00224 BOOL CreateSocket(
00225 PUDPSocket * & socket
00226 );
00227
00241 BOOL CreateSocketPair(
00242 PUDPSocket * & socket1,
00243 PUDPSocket * & socket2
00244 );
00245
00246 protected:
00247 void Construct();
00248
00249 PIPSocket::Address serverAddress;
00250 WORD serverPort;
00251
00252 struct PortInfo {
00253 PMutex mutex;
00254 WORD basePort;
00255 WORD maxPort;
00256 WORD currentPort;
00257 } singlePortInfo, pairedPortInfo;
00258 bool OpenSocket(PUDPSocket & socket, PortInfo & portInfo) const;
00259
00260 int numSocketsForPairing;
00261
00262 NatTypes natType;
00263 PIPSocket::Address cachedExternalAddress;
00264 PTime timeAddressObtained;
00265 };
00266
00267
00268
00269 #endif // _PSTUN_H
00270
00271
00272