Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Class Members | File Members

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.9  2004/11/25 07:23:46  csoutheren
00028  * Added IsSupportingRTP function to simplify detecting when STUN supports RTP
00029  *
00030  * Revision 1.8  2004/03/14 05:47:52  rjongbloed
00031  * Fixed incorrect detection of symmetric NAT (eg Linux masquerading) and also
00032  *   some NAT systems which are partially blocked due to firewall rules.
00033  *
00034  * Revision 1.7  2004/02/24 11:15:48  rjongbloed
00035  * Added function to get external router address, also did a bunch of documentation.
00036  *
00037  * Revision 1.6  2004/01/17 17:54:02  rjongbloed
00038  * Added function to get server name from STUN client.
00039  *
00040  * Revision 1.5  2003/10/05 00:56:25  rjongbloed
00041  * Rewrite of STUN to not to use imported code with undesirable license.
00042  *
00043  * Revision 1.4  2003/02/05 06:26:49  robertj
00044  * More work in making the STUN usable for Symmetric NAT systems.
00045  *
00046  * Revision 1.3  2003/02/04 07:01:02  robertj
00047  * Added ip/port version of constructor.
00048  *
00049  * Revision 1.2  2003/02/04 05:05:55  craigs
00050  * Added new functions
00051  *
00052  * Revision 1.1  2003/02/04 03:31:04  robertj
00053  * Added STUN
00054  *
00055  */
00056 
00057 #ifndef _PSTUN_H
00058 #define _PSTUN_H
00059 
00060 #ifdef P_USE_PRAGMA
00061 #pragma interface
00062 #endif
00063 
00064 #include <ptlib.h>
00065 #include <ptlib/sockets.h>
00066 
00067 
00070 class PSTUNUDPSocket : public PUDPSocket
00071 {
00072   PCLASSINFO(PSTUNUDPSocket, PUDPSocket);
00073   public:
00074     PSTUNUDPSocket();
00075 
00076     virtual BOOL GetLocalAddress(
00077       Address & addr    
00078     );
00079     virtual BOOL GetLocalAddress(
00080       Address & addr,    
00081       WORD & port        
00082     );
00083 
00084   protected:
00085     PIPSocket::Address externalIP;
00086 
00087   friend class PSTUNClient;
00088 };
00089 
00090 
00093 class PSTUNClient : public PObject
00094 {
00095   PCLASSINFO(PSTUNClient, PObject);
00096   public:
00097     enum {
00098       DefaultPort = 3478
00099     };
00100 
00101     PSTUNClient(
00102       const PString & server,
00103       WORD portBase = 0,
00104       WORD portMax = 0,
00105       WORD portPairBase = 0,
00106       WORD portPairMax = 0
00107     );
00108     PSTUNClient(
00109       const PIPSocket::Address & serverAddress,
00110       WORD serverPort = DefaultPort,
00111       WORD portBase = 0,
00112       WORD portMax = 0,
00113       WORD portPairBase = 0,
00114       WORD portPairMax = 0
00115     );
00116 
00117 
00120     PString GetServer() const;
00121 
00128     BOOL SetServer(
00129       const PString & server
00130     );
00131 
00135     BOOL SetServer(
00136       const PIPSocket::Address & serverAddress,
00137       WORD serverPort = 0
00138     );
00139 
00150     void SetPortRanges(
00151       WORD portBase,          
00152       WORD portMax = 0,       
00153       WORD portPairBase = 0,  
00154       WORD portPairMax = 0    
00155     );
00156 
00157 
00158     enum NatTypes {
00159       UnknownNat,
00160       OpenNat,
00161       ConeNat,
00162       RestrictedNat,
00163       PortRestrictedNat,
00164       SymmetricNat,
00165       SymmetricFirewall,
00166       BlockedNat,
00167       PartialBlockedNat,
00168       NumNatTypes
00169     };
00170 
00175     NatTypes GetNatType(
00176       BOOL force = FALSE    
00177     );
00178 
00179     enum RTPSupportTypes {
00180       RTPOK,
00181       RTPUnknown,
00182       RTPUnsupported,
00183       RTPIfSendMedia
00184     };
00185 
00189     RTPSupportTypes IsSupportingRTP(
00190       BOOL force = FALSE    
00191     );
00192 
00196     PString GetNatTypeName(
00197       BOOL force = FALSE    
00198     );
00199 
00207     BOOL GetExternalAddress(
00208       PIPSocket::Address & externalAddress, // External address of router
00209       const PTimeInterval & maxAge = 1000   // Maximum age for caching
00210     );
00211 
00224     BOOL CreateSocket(
00225       PUDPSocket * & socket
00226     );
00227 
00241     BOOL CreateSocketPair(
00242       PUDPSocket * & socket1,
00243       PUDPSocket * & socket2
00244     );
00245 
00246   protected:
00247     void Construct();
00248 
00249     PIPSocket::Address serverAddress;
00250     WORD               serverPort;
00251 
00252     struct PortInfo {
00253       PMutex mutex;
00254       WORD   basePort;
00255       WORD   maxPort;
00256       WORD   currentPort;
00257     } singlePortInfo, pairedPortInfo;
00258     bool OpenSocket(PUDPSocket & socket, PortInfo & portInfo) const;
00259 
00260     int  numSocketsForPairing;
00261 
00262     NatTypes natType;
00263     PIPSocket::Address cachedExternalAddress;
00264     PTime              timeAddressObtained;
00265 };
00266 
00267 
00268 
00269 #endif // _PSTUN_H
00270 
00271 
00272 // End of file ////////////////////////////////////////////////////////////////

Generated on Tue Mar 15 10:44:52 2005 for PWLib by  doxygen 1.4.0