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  * $Log: pstun.h,v $
00027  * Revision 1.13  2006/12/23 15:08:00  shorne
00028  * Now Factory loaded for ease of addition of new NAT Methods
00029  *
00030  * Revision 1.12  2005/11/30 12:47:37  csoutheren
00031  * Removed tabs, reformatted some code, and changed tags for Doxygen
00032  *
00033  * Revision 1.11  2005/07/13 11:15:15  csoutheren
00034  * Backported NAT abstraction files from isvo branch
00035  *
00036  * Revision 1.10  2005/06/20 10:55:16  rjongbloed
00037  * Changed the timeout and retries so if there is a blocking firewall it does not take 15 seconds to find out!
00038  * Added access functions so timeout and retries are application configurable.
00039  * Added function (and << operator) to get NAT type enum as string.
00040  *
00041  * Revision 1.9.4.1  2005/04/25 13:21:36  shorne
00042  * Add Support for other NAT methods
00043  *
00044  * Revision 1.9  2004/11/25 07:23:46  csoutheren
00045  * Added IsSupportingRTP function to simplify detecting when STUN supports RTP
00046  *
00047  * Revision 1.8  2004/03/14 05:47:52  rjongbloed
00048  * Fixed incorrect detection of symmetric NAT (eg Linux masquerading) and also
00049  *   some NAT systems which are partially blocked due to firewall rules.
00050  *
00051  * Revision 1.7  2004/02/24 11:15:48  rjongbloed
00052  * Added function to get external router address, also did a bunch of documentation.
00053  *
00054  * Revision 1.6  2004/01/17 17:54:02  rjongbloed
00055  * Added function to get server name from STUN client.
00056  *
00057  * Revision 1.5  2003/10/05 00:56:25  rjongbloed
00058  * Rewrite of STUN to not to use imported code with undesirable license.
00059  *
00060  * Revision 1.4  2003/02/05 06:26:49  robertj
00061  * More work in making the STUN usable for Symmetric NAT systems.
00062  *
00063  * Revision 1.3  2003/02/04 07:01:02  robertj
00064  * Added ip/port version of constructor.
00065  *
00066  * Revision 1.2  2003/02/04 05:05:55  craigs
00067  * Added new functions
00068  *
00069  * Revision 1.1  2003/02/04 03:31:04  robertj
00070  * Added STUN
00071  *
00072  */
00073 
00074 #ifndef _PSTUN_H
00075 #define _PSTUN_H
00076 
00077 #ifdef P_USE_PRAGMA
00078 #pragma interface
00079 #endif
00080 
00081 #include <ptlib.h>
00082 #include <ptclib/pnat.h>
00083 #include <ptlib/sockets.h>
00084 
00085 
00088 class PSTUNUDPSocket : public PUDPSocket
00089 {
00090   PCLASSINFO(PSTUNUDPSocket, PUDPSocket);
00091   public:
00092     PSTUNUDPSocket();
00093 
00094     virtual BOOL GetLocalAddress(
00095       Address & addr    
00096     );
00097     virtual BOOL GetLocalAddress(
00098       Address & addr,    
00099       WORD & port        
00100     );
00101 
00102   protected:
00103     PIPSocket::Address externalIP;
00104 
00105   friend class PSTUNClient;
00106 };
00107 
00108 
00111 class PSTUNClient : public PNatMethod
00112 {
00113   PCLASSINFO(PSTUNClient, PNatMethod);
00114   public:
00115     enum {
00116       DefaultPort = 3478
00117     };
00118 
00119         PSTUNClient();
00120 
00121     PSTUNClient(
00122       const PString & server,
00123       WORD portBase = 0,
00124       WORD portMax = 0,
00125       WORD portPairBase = 0,
00126       WORD portPairMax = 0
00127     );
00128     PSTUNClient(
00129       const PIPSocket::Address & serverAddress,
00130       WORD serverPort = DefaultPort,
00131       WORD portBase = 0,
00132       WORD portMax = 0,
00133       WORD portPairBase = 0,
00134       WORD portPairMax = 0
00135     );
00136 
00137 
00138     void Initialise(const PString & server,
00139                      WORD portBase = 0, 
00140                                          WORD portMax = 0,
00141                      WORD portPairBase = 0, 
00142                                          WORD portPairMax = 0);
00143 
00146         static PStringList GetNatMethodName() { return PStringList("STUN"); }
00147 
00148 
00151     PString GetServer() const;
00152 
00159     BOOL SetServer(
00160       const PString & server
00161     );
00162 
00166     BOOL SetServer(
00167       const PIPSocket::Address & serverAddress,
00168       WORD serverPort = 0
00169     );
00170 
00171     enum NatTypes {
00172       UnknownNat,
00173       OpenNat,
00174       ConeNat,
00175       RestrictedNat,
00176       PortRestrictedNat,
00177       SymmetricNat,
00178       SymmetricFirewall,
00179       BlockedNat,
00180       PartialBlockedNat,
00181       NumNatTypes
00182     };
00183 
00188     NatTypes GetNatType(
00189       BOOL force = FALSE    
00190     );
00191 
00195     PString GetNatTypeName(
00196       BOOL force = FALSE    
00197     ) { return GetNatTypeString(GetNatType(force)); }
00198 
00201     static PString GetNatTypeString(
00202       NatTypes type   
00203     );
00204 
00205     enum RTPSupportTypes {
00206       RTPOK,
00207       RTPUnknown,
00208       RTPUnsupported,
00209       RTPIfSendMedia
00210     };
00211 
00215     RTPSupportTypes IsSupportingRTP(
00216       BOOL force = FALSE    
00217     );
00218 
00226     virtual BOOL GetExternalAddress(
00227       PIPSocket::Address & externalAddress, 
00228       const PTimeInterval & maxAge = 1000   
00229     );
00230 
00243     BOOL CreateSocket(
00244       PUDPSocket * & socket
00245     );
00246 
00260     virtual BOOL CreateSocketPair(
00261       PUDPSocket * & socket1,
00262       PUDPSocket * & socket2
00263     );
00264 
00267     const PTimeInterval GetTimeout() const { return replyTimeout; }
00268 
00271     void SetTimeout(
00272       const PTimeInterval & timeout   
00273     ) { replyTimeout = timeout; }
00274 
00277     PINDEX GetRetries() const { return pollRetries; }
00278 
00281     void SetRetries(
00282       PINDEX retries    
00283     ) { pollRetries = retries; }
00284 
00290     PINDEX GetSocketsForPairing() const { return numSocketsForPairing; }
00291 
00297     void SetSocketsForPairing(
00298       PINDEX numSockets   
00299     ) { numSocketsForPairing = numSockets; }
00300 
00308     virtual BOOL IsAvailable();
00309 
00310   protected:
00311     PIPSocket::Address serverAddress;
00312     WORD               serverPort;
00313     PTimeInterval      replyTimeout;
00314     PINDEX             pollRetries;
00315     PINDEX             numSocketsForPairing;
00316 
00317     bool OpenSocket(PUDPSocket & socket, PortInfo & portInfo) const;
00318 
00319     NatTypes           natType;
00320     PIPSocket::Address cachedExternalAddress;
00321     PTime              timeAddressObtained;
00322 };
00323 
00324 
00325 inline ostream & operator<<(ostream & strm, PSTUNClient::NatTypes type) { return strm << PSTUNClient::GetNatTypeString(type); }
00326 
00328 typedef PSTUNClient PNatMethod_STUN;
00329 PWLIB_STATIC_LOAD_PLUGIN(STUN, PNatMethod);
00330 
00331 
00332 #endif // _PSTUN_H
00333 
00334 
00335 // End of file ////////////////////////////////////////////////////////////////

Generated on Mon Sep 1 09:41:07 2008 for PWLib by  doxygen 1.5.6