pstun.h

Go to the documentation of this file.
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  * $Revision: 20766 $
00027  * $Author: rjongbloed $
00028  * $Date: 2008-08-29 06:41:59 +0000 (Fri, 29 Aug 2008) $
00029  */
00030 
00031 #ifndef _PSTUN_H
00032 #define _PSTUN_H
00033 
00034 #ifdef P_USE_PRAGMA
00035 #pragma interface
00036 #endif
00037 
00038 #ifndef _PTLIB_H
00039 #include <ptlib.h>
00040 #endif
00041 
00042 #include <ptclib/pnat.h>
00043 #include <ptlib/sockets.h>
00044 
00045 
00048 class PSTUNUDPSocket : public PUDPSocket
00049 {
00050   PCLASSINFO(PSTUNUDPSocket, PUDPSocket);
00051   public:
00052     PSTUNUDPSocket();
00053 
00054     virtual PBoolean GetLocalAddress(
00055       Address & addr    
00056     );
00057     virtual PBoolean GetLocalAddress(
00058       Address & addr,    
00059       WORD & port        
00060     );
00061 
00062   protected:
00063     PIPSocket::Address externalIP;
00064 
00065   friend class PSTUNClient;
00066 };
00067 
00068 
00071 class PSTUNClient : public PNatMethod
00072 {
00073   PCLASSINFO(PSTUNClient, PNatMethod);
00074   public:
00075     enum {
00076       DefaultPort = 3478
00077     };
00078 
00079     PSTUNClient();
00080 
00081     PSTUNClient(
00082       const PString & server,
00083       WORD portBase = 0,
00084       WORD portMax = 0,
00085       WORD portPairBase = 0,
00086       WORD portPairMax = 0
00087     );
00088     PSTUNClient(
00089       const PIPSocket::Address & serverAddress,
00090       WORD serverPort = DefaultPort,
00091       WORD portBase = 0,
00092       WORD portMax = 0,
00093       WORD portPairBase = 0,
00094       WORD portPairMax = 0
00095     );
00096 
00097 
00098     void Initialise(
00099       const PString & server,
00100       WORD portBase = 0, 
00101       WORD portMax = 0,
00102       WORD portPairBase = 0, 
00103       WORD portPairMax = 0
00104     );
00105 
00108     static PStringList GetNatMethodName() { return PStringList("STUN"); }
00109 
00112     virtual PString GetName() const { return "STUN"; }
00113 
00116     virtual bool GetServerAddress(
00117       PIPSocket::Address & address,   
00118       WORD & port                     
00119     ) const;
00120 
00127     PBoolean SetServer(
00128       const PString & server
00129     );
00130 
00134     PBoolean SetServer(
00135       const PIPSocket::Address & serverAddress,
00136       WORD serverPort = 0
00137     );
00138 
00139     enum NatTypes {
00140       UnknownNat,
00141       OpenNat,
00142       ConeNat,
00143       RestrictedNat,
00144       PortRestrictedNat,
00145       SymmetricNat,
00146       SymmetricFirewall,
00147       BlockedNat,
00148       PartialBlockedNat,
00149       NumNatTypes
00150     };
00151 
00156     NatTypes GetNatType(
00157       PBoolean force = PFalse    
00158     );
00159 
00163     PString GetNatTypeName(
00164       PBoolean force = PFalse    
00165     ) { return GetNatTypeString(GetNatType(force)); }
00166 
00169     static PString GetNatTypeString(
00170       NatTypes type   
00171     );
00172 
00176     RTPSupportTypes GetRTPSupport(
00177       PBoolean force = PFalse    
00178     );
00179 
00187     virtual PBoolean GetExternalAddress(
00188       PIPSocket::Address & externalAddress, 
00189       const PTimeInterval & maxAge = 1000   
00190     );
00191 
00194     virtual bool GetInterfaceAddress(
00195       PIPSocket::Address & internalAddress
00196     ) const;
00197 
00202     void InvalidateCache();
00203 
00216     PBoolean CreateSocket(
00217       PUDPSocket * & socket,
00218       const PIPSocket::Address & binding = PIPSocket::GetDefaultIpAny(),
00219       WORD localPort = 0
00220     );
00221 
00235     virtual PBoolean CreateSocketPair(
00236       PUDPSocket * & socket1,
00237       PUDPSocket * & socket2,
00238       const PIPSocket::Address & binding = PIPSocket::GetDefaultIpAny()
00239     );
00240 
00243     const PTimeInterval GetTimeout() const { return replyTimeout; }
00244 
00247     void SetTimeout(
00248       const PTimeInterval & timeout   
00249     ) { replyTimeout = timeout; }
00250 
00253     PINDEX GetRetries() const { return pollRetries; }
00254 
00257     void SetRetries(
00258       PINDEX retries    
00259     ) { pollRetries = retries; }
00260 
00266     PINDEX GetSocketsForPairing() const { return numSocketsForPairing; }
00267 
00273     void SetSocketsForPairing(
00274       PINDEX numSockets   
00275     ) { numSocketsForPairing = numSockets; }
00276 
00284     virtual bool IsAvailable(
00285       const PIPSocket::Address & binding = PIPSocket::GetDefaultIpAny()  
00286     );
00287 
00288   protected:
00289     PString            serverHost;
00290     WORD               serverPort;
00291     PTimeInterval      replyTimeout;
00292     PINDEX             pollRetries;
00293     PINDEX             numSocketsForPairing;
00294 
00295     bool OpenSocket(PUDPSocket & socket, PortInfo & portInfo, const PIPSocket::Address & binding);
00296 
00297     NatTypes           natType;
00298     PIPSocket::Address cachedServerAddress;
00299     PIPSocket::Address cachedExternalAddress;
00300     PIPSocket::Address interfaceAddress;
00301     PTime              timeAddressObtained;
00302 };
00303 
00304 
00305 inline ostream & operator<<(ostream & strm, PSTUNClient::NatTypes type) { return strm << PSTUNClient::GetNatTypeString(type); }
00306 
00308 typedef PSTUNClient PNatMethod_STUN;
00309 PWLIB_STATIC_LOAD_PLUGIN(STUN, PNatMethod);
00310 
00311 
00312 #endif // _PSTUN_H
00313 
00314 
00315 // End of file ////////////////////////////////////////////////////////////////

Generated on Mon Feb 23 01:57:54 2009 for PTLib by  doxygen 1.5.1