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: 19008 $
00027  * $Author: rjongbloed $
00028  * $Date: 2007-11-29 09:17:41 +0000 (Thu, 29 Nov 2007) $
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 
00110     virtual PStringList GetName() const
00111       { return GetNatMethodName(); }
00112 
00115     PString GetServer() const;
00116     
00117     void GetServer(PIPSocket::Address & address, WORD & port) const;
00118 
00125     PBoolean SetServer(
00126       const PString & server
00127     );
00128 
00132     PBoolean SetServer(
00133       const PIPSocket::Address & serverAddress,
00134       WORD serverPort = 0
00135     );
00136 
00137     enum NatTypes {
00138       UnknownNat,
00139       OpenNat,
00140       ConeNat,
00141       RestrictedNat,
00142       PortRestrictedNat,
00143       SymmetricNat,
00144       SymmetricFirewall,
00145       BlockedNat,
00146       PartialBlockedNat,
00147       NumNatTypes
00148     };
00149 
00154     NatTypes GetNatType(
00155       PBoolean force = PFalse    
00156     );
00157 
00161     PString GetNatTypeName(
00162       PBoolean force = PFalse    
00163     ) { return GetNatTypeString(GetNatType(force)); }
00164 
00167     static PString GetNatTypeString(
00168       NatTypes type   
00169     );
00170 
00171     enum RTPSupportTypes {
00172       RTPOK,
00173       RTPUnknown,
00174       RTPUnsupported,
00175       RTPIfSendMedia
00176     };
00177 
00181     RTPSupportTypes IsSupportingRTP(
00182       PBoolean force = PFalse    
00183     );
00184 
00192     virtual PBoolean GetExternalAddress(
00193       PIPSocket::Address & externalAddress, 
00194       const PTimeInterval & maxAge = 1000   
00195     );
00196     
00201     void InvalidateExternalAddressCache();
00202 
00215     PBoolean CreateSocket(
00216       PUDPSocket * & socket,
00217       const PIPSocket::Address & binding = PIPSocket::GetDefaultIpAny(),
00218       WORD localPort = 0
00219     );
00220 
00234     virtual PBoolean CreateSocketPair(
00235       PUDPSocket * & socket1,
00236       PUDPSocket * & socket2,
00237       const PIPSocket::Address & binding = PIPSocket::GetDefaultIpAny()
00238     );
00239 
00242     const PTimeInterval GetTimeout() const { return replyTimeout; }
00243 
00246     void SetTimeout(
00247       const PTimeInterval & timeout   
00248     ) { replyTimeout = timeout; }
00249 
00252     PINDEX GetRetries() const { return pollRetries; }
00253 
00256     void SetRetries(
00257       PINDEX retries    
00258     ) { pollRetries = retries; }
00259 
00265     PINDEX GetSocketsForPairing() const { return numSocketsForPairing; }
00266 
00272     void SetSocketsForPairing(
00273       PINDEX numSockets   
00274     ) { numSocketsForPairing = numSockets; }
00275 
00283     virtual PBoolean IsAvailable();
00284 
00285   protected:
00286     PIPSocket::Address serverAddress;
00287     WORD               serverPort;
00288     PTimeInterval      replyTimeout;
00289     PINDEX             pollRetries;
00290     PINDEX             numSocketsForPairing;
00291 
00292     bool OpenSocket(PUDPSocket & socket, PortInfo & portInfo, const PIPSocket::Address & binding) const;
00293 
00294     NatTypes           natType;
00295     PIPSocket::Address cachedExternalAddress;
00296     PTime              timeAddressObtained;
00297 };
00298 
00299 
00300 inline ostream & operator<<(ostream & strm, PSTUNClient::NatTypes type) { return strm << PSTUNClient::GetNatTypeString(type); }
00301 
00303 typedef PSTUNClient PNatMethod_STUN;
00304 PWLIB_STATIC_LOAD_PLUGIN(STUN, PNatMethod);
00305 
00306 
00307 #endif // _PSTUN_H
00308 
00309 
00310 // End of file ////////////////////////////////////////////////////////////////

Generated on Mon Dec 10 11:18:57 2007 for PTLib by  doxygen 1.5.1