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.18  2007/09/25 14:25:32  hfriederich
00028  * Allow to get STUN server address as PIPSocket::Address instance
00029  *
00030  * Revision 1.17  2007/08/22 05:04:39  rjongbloed
00031  * Added ability to set a specific local port for STUN created sockets.
00032  *
00033  * Revision 1.16  2007/07/22 03:07:31  rjongbloed
00034  * Added parameter so can bind STUN socket to specific interface.
00035  *
00036  * Revision 1.15  2007/04/19 04:33:53  csoutheren
00037  * Fixed problems with pre-compiled headers
00038  *
00039  * Revision 1.14  2007/02/11 13:13:18  shorne
00040  * Added GetName function
00041  *
00042  * Revision 1.13  2006/12/23 15:08:00  shorne
00043  * Now Factory loaded for ease of addition of new NAT Methods
00044  *
00045  * Revision 1.12  2005/11/30 12:47:37  csoutheren
00046  * Removed tabs, reformatted some code, and changed tags for Doxygen
00047  *
00048  * Revision 1.11  2005/07/13 11:15:15  csoutheren
00049  * Backported NAT abstraction files from isvo branch
00050  *
00051  * Revision 1.10  2005/06/20 10:55:16  rjongbloed
00052  * Changed the timeout and retries so if there is a blocking firewall it does not take 15 seconds to find out!
00053  * Added access functions so timeout and retries are application configurable.
00054  * Added function (and << operator) to get NAT type enum as string.
00055  *
00056  * Revision 1.9.4.1  2005/04/25 13:21:36  shorne
00057  * Add Support for other NAT methods
00058  *
00059  * Revision 1.9  2004/11/25 07:23:46  csoutheren
00060  * Added IsSupportingRTP function to simplify detecting when STUN supports RTP
00061  *
00062  * Revision 1.8  2004/03/14 05:47:52  rjongbloed
00063  * Fixed incorrect detection of symmetric NAT (eg Linux masquerading) and also
00064  *   some NAT systems which are partially blocked due to firewall rules.
00065  *
00066  * Revision 1.7  2004/02/24 11:15:48  rjongbloed
00067  * Added function to get external router address, also did a bunch of documentation.
00068  *
00069  * Revision 1.6  2004/01/17 17:54:02  rjongbloed
00070  * Added function to get server name from STUN client.
00071  *
00072  * Revision 1.5  2003/10/05 00:56:25  rjongbloed
00073  * Rewrite of STUN to not to use imported code with undesirable license.
00074  *
00075  * Revision 1.4  2003/02/05 06:26:49  robertj
00076  * More work in making the STUN usable for Symmetric NAT systems.
00077  *
00078  * Revision 1.3  2003/02/04 07:01:02  robertj
00079  * Added ip/port version of constructor.
00080  *
00081  * Revision 1.2  2003/02/04 05:05:55  craigs
00082  * Added new functions
00083  *
00084  * Revision 1.1  2003/02/04 03:31:04  robertj
00085  * Added STUN
00086  *
00087  */
00088 
00089 #ifndef _PSTUN_H
00090 #define _PSTUN_H
00091 
00092 #ifdef P_USE_PRAGMA
00093 #pragma interface
00094 #endif
00095 
00096 #ifndef _PTLIB_H
00097 #include <ptlib.h>
00098 #endif
00099 
00100 #include <ptclib/pnat.h>
00101 #include <ptlib/sockets.h>
00102 
00103 
00106 class PSTUNUDPSocket : public PUDPSocket
00107 {
00108   PCLASSINFO(PSTUNUDPSocket, PUDPSocket);
00109   public:
00110     PSTUNUDPSocket();
00111 
00112     virtual BOOL GetLocalAddress(
00113       Address & addr    
00114     );
00115     virtual BOOL GetLocalAddress(
00116       Address & addr,    
00117       WORD & port        
00118     );
00119 
00120   protected:
00121     PIPSocket::Address externalIP;
00122 
00123   friend class PSTUNClient;
00124 };
00125 
00126 
00129 class PSTUNClient : public PNatMethod
00130 {
00131   PCLASSINFO(PSTUNClient, PNatMethod);
00132   public:
00133     enum {
00134       DefaultPort = 3478
00135     };
00136 
00137     PSTUNClient();
00138 
00139     PSTUNClient(
00140       const PString & server,
00141       WORD portBase = 0,
00142       WORD portMax = 0,
00143       WORD portPairBase = 0,
00144       WORD portPairMax = 0
00145     );
00146     PSTUNClient(
00147       const PIPSocket::Address & serverAddress,
00148       WORD serverPort = DefaultPort,
00149       WORD portBase = 0,
00150       WORD portMax = 0,
00151       WORD portPairBase = 0,
00152       WORD portPairMax = 0
00153     );
00154 
00155 
00156     void Initialise(
00157       const PString & server,
00158       WORD portBase = 0, 
00159       WORD portMax = 0,
00160       WORD portPairBase = 0, 
00161       WORD portPairMax = 0
00162     );
00163 
00166     static PStringList GetNatMethodName() { return PStringList("STUN"); }
00167 
00168     virtual PStringList GetName() const
00169       { return GetNatMethodName(); }
00170 
00173     PString GetServer() const;
00174     
00175     void GetServer(PIPSocket::Address & address, WORD & port) const;
00176 
00183     BOOL SetServer(
00184       const PString & server
00185     );
00186 
00190     BOOL SetServer(
00191       const PIPSocket::Address & serverAddress,
00192       WORD serverPort = 0
00193     );
00194 
00195     enum NatTypes {
00196       UnknownNat,
00197       OpenNat,
00198       ConeNat,
00199       RestrictedNat,
00200       PortRestrictedNat,
00201       SymmetricNat,
00202       SymmetricFirewall,
00203       BlockedNat,
00204       PartialBlockedNat,
00205       NumNatTypes
00206     };
00207 
00212     NatTypes GetNatType(
00213       BOOL force = FALSE    
00214     );
00215 
00219     PString GetNatTypeName(
00220       BOOL force = FALSE    
00221     ) { return GetNatTypeString(GetNatType(force)); }
00222 
00225     static PString GetNatTypeString(
00226       NatTypes type   
00227     );
00228 
00229     enum RTPSupportTypes {
00230       RTPOK,
00231       RTPUnknown,
00232       RTPUnsupported,
00233       RTPIfSendMedia
00234     };
00235 
00239     RTPSupportTypes IsSupportingRTP(
00240       BOOL force = FALSE    
00241     );
00242 
00250     virtual BOOL GetExternalAddress(
00251       PIPSocket::Address & externalAddress, 
00252       const PTimeInterval & maxAge = 1000   
00253     );
00254 
00267     BOOL CreateSocket(
00268       PUDPSocket * & socket,
00269       const PIPSocket::Address & binding = PIPSocket::GetDefaultIpAny(),
00270       WORD localPort = 0
00271     );
00272 
00286     virtual BOOL CreateSocketPair(
00287       PUDPSocket * & socket1,
00288       PUDPSocket * & socket2,
00289       const PIPSocket::Address & binding = PIPSocket::GetDefaultIpAny()
00290     );
00291 
00294     const PTimeInterval GetTimeout() const { return replyTimeout; }
00295 
00298     void SetTimeout(
00299       const PTimeInterval & timeout   
00300     ) { replyTimeout = timeout; }
00301 
00304     PINDEX GetRetries() const { return pollRetries; }
00305 
00308     void SetRetries(
00309       PINDEX retries    
00310     ) { pollRetries = retries; }
00311 
00317     PINDEX GetSocketsForPairing() const { return numSocketsForPairing; }
00318 
00324     void SetSocketsForPairing(
00325       PINDEX numSockets   
00326     ) { numSocketsForPairing = numSockets; }
00327 
00335     virtual BOOL IsAvailable();
00336 
00337   protected:
00338     PIPSocket::Address serverAddress;
00339     WORD               serverPort;
00340     PTimeInterval      replyTimeout;
00341     PINDEX             pollRetries;
00342     PINDEX             numSocketsForPairing;
00343 
00344     bool OpenSocket(PUDPSocket & socket, PortInfo & portInfo, const PIPSocket::Address & binding) const;
00345 
00346     NatTypes           natType;
00347     PIPSocket::Address cachedExternalAddress;
00348     PTime              timeAddressObtained;
00349 };
00350 
00351 
00352 inline ostream & operator<<(ostream & strm, PSTUNClient::NatTypes type) { return strm << PSTUNClient::GetNatTypeString(type); }
00353 
00355 typedef PSTUNClient PNatMethod_STUN;
00356 PWLIB_STATIC_LOAD_PLUGIN(STUN, PNatMethod);
00357 
00358 
00359 #endif // _PSTUN_H
00360 
00361 
00362 // End of file ////////////////////////////////////////////////////////////////

Generated on Fri Mar 7 06:25:02 2008 for PTLib by  doxygen 1.5.1