PWLib
1.10.10
|
00001 /* 00002 * pstun.h 00003 * 00004 * STUN client 00005 * 00006 * Portable Windows Library 00007 * 00008 * Copyright (c) 2003 Equivalence Pty. Ltd. 00009 * 00010 * The contents of this file are subject to the Mozilla Public License 00011 * Version 1.0 (the "License"); you may not use this file except in 00012 * compliance with the License. You may obtain a copy of the License at 00013 * http://www.mozilla.org/MPL/ 00014 * 00015 * Software distributed under the License is distributed on an "AS IS" 00016 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See 00017 * the License for the specific language governing rights and limitations 00018 * under the License. 00019 * 00020 * The Original Code is Portable Windows Library. 00021 * 00022 * The Initial Developer of the Original Code is Equivalence Pty. Ltd. 00023 * 00024 * Contributor(s): ______________________________________. 00025 * 00026 * $Log: pstun.h,v $ 00027 * Revision 1.12 2005/11/30 12:47:37 csoutheren 00028 * Removed tabs, reformatted some code, and changed tags for Doxygen 00029 * 00030 * Revision 1.11 2005/07/13 11:15:15 csoutheren 00031 * Backported NAT abstraction files from isvo branch 00032 * 00033 * Revision 1.10 2005/06/20 10:55:16 rjongbloed 00034 * Changed the timeout and retries so if there is a blocking firewall it does not take 15 seconds to find out! 00035 * Added access functions so timeout and retries are application configurable. 00036 * Added function (and << operator) to get NAT type enum as string. 00037 * 00038 * Revision 1.9.4.1 2005/04/25 13:21:36 shorne 00039 * Add Support for other NAT methods 00040 * 00041 * Revision 1.9 2004/11/25 07:23:46 csoutheren 00042 * Added IsSupportingRTP function to simplify detecting when STUN supports RTP 00043 * 00044 * Revision 1.8 2004/03/14 05:47:52 rjongbloed 00045 * Fixed incorrect detection of symmetric NAT (eg Linux masquerading) and also 00046 * some NAT systems which are partially blocked due to firewall rules. 00047 * 00048 * Revision 1.7 2004/02/24 11:15:48 rjongbloed 00049 * Added function to get external router address, also did a bunch of documentation. 00050 * 00051 * Revision 1.6 2004/01/17 17:54:02 rjongbloed 00052 * Added function to get server name from STUN client. 00053 * 00054 * Revision 1.5 2003/10/05 00:56:25 rjongbloed 00055 * Rewrite of STUN to not to use imported code with undesirable license. 00056 * 00057 * Revision 1.4 2003/02/05 06:26:49 robertj 00058 * More work in making the STUN usable for Symmetric NAT systems. 00059 * 00060 * Revision 1.3 2003/02/04 07:01:02 robertj 00061 * Added ip/port version of constructor. 00062 * 00063 * Revision 1.2 2003/02/04 05:05:55 craigs 00064 * Added new functions 00065 * 00066 * Revision 1.1 2003/02/04 03:31:04 robertj 00067 * Added STUN 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 // End of file ////////////////////////////////////////////////////////////////