# 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: 24177 $
    00027  * $Author: rjongbloed $
    00028  * $Date: 2010-04-05 06:52:04 -0500 (Mon, 05 Apr 2010) $
    00029  */
    00030
    00031 #ifndef PTLIB_PSTUN_H
    00032 #define PTLIB_PSTUN_H
    00033
    00034 #ifdef P_USE_PRAGMA
    00035 #pragma interface
    00036 #endif
    00037
    00038
    00039 #include <ptclib/pnat.h>
    00040 #include <ptlib/sockets.h>
    00041
    00042
    00045 class PSTUNUDPSocket : public PUDPSocket
    00046 {
    00047   PCLASSINFO(PSTUNUDPSocket, PUDPSocket);
    00048   public:
    00049     PSTUNUDPSocket();
    00050
    00051     virtual PBoolean GetLocalAddress(
    00052       Address & addr
    00053     );
    00054     virtual PBoolean GetLocalAddress(
    00055       Address & addr,
    00056       WORD & port
    00057     );
    00058
    00059   protected:
    00060     PIPSocket::Address externalIP;
    00061
    00062   friend class PSTUNClient;
    00063 };
    00064
    00065
    00068 class PSTUNClient : public PNatMethod
    00069 {
    00070   PCLASSINFO(PSTUNClient, PNatMethod);
    00071   public:
    00072     enum {
    00073       DefaultPort = 3478
    00074     };
    00075
    00076     PSTUNClient();
    00077
    00078     PSTUNClient(
    00079       const PString & server,
    00080       WORD portBase = 0,
    00081       WORD portMax = 0,
    00082       WORD portPairBase = 0,
    00083       WORD portPairMax = 0
    00084     );
    00085     PSTUNClient(
    00086       const PIPSocket::Address & serverAddress,
    00087       WORD serverPort = DefaultPort,
    00088       WORD portBase = 0,
    00089       WORD portMax = 0,
    00090       WORD portPairBase = 0,
    00091       WORD portPairMax = 0
    00092     );
    00093
    00094
    00095     void Initialise(
    00096       const PString & server,
    00097       WORD portBase = 0,
    00098       WORD portMax = 0,
    00099       WORD portPairBase = 0,
    00100       WORD portPairMax = 0
    00101     );
    00102
    00105     static PStringList GetNatMethodName() { return PStringList("STUN"); }
    00106
    00109     virtual PString GetName() const { return "STUN"; }
    00110
    00113     virtual bool GetServerAddress(
    00114       PIPSocket::Address & address,
    00115       WORD & port
    00116     ) const;
    00117
    00124     PBoolean SetServer(
    00125       const PString & server
    00126     );
    00127
    00131     PBoolean SetServer(
    00132       const PIPSocket::Address & serverAddress,
    00133       WORD serverPort = 0
    00134     );
    00135
    00136     enum NatTypes {
    00137       UnknownNat,
    00138       OpenNat,
    00139       ConeNat,
    00140       RestrictedNat,
    00141       PortRestrictedNat,
    00142       SymmetricNat,
    00143       SymmetricFirewall,
    00144       BlockedNat,
    00145       PartialBlockedNat,
    00146       NumNatTypes
    00147     };
    00148
    00153     NatTypes GetNatType(
    00154       PBoolean force = false
    00155     );
    00156
    00160     PString GetNatTypeName(
    00161       PBoolean force = false
    00162     ) { return GetNatTypeString(GetNatType(force)); }
    00163
    00166     static PString GetNatTypeString(
    00167       NatTypes type
    00168     );
    00169
    00173     RTPSupportTypes GetRTPSupport(
    00174       PBoolean force = false
    00175     );
    00176
    00184     virtual PBoolean GetExternalAddress(
    00185       PIPSocket::Address & externalAddress,
    00186       const PTimeInterval & maxAge = 1000
    00187     );
    00188
    00191     virtual bool GetInterfaceAddress(
    00192       PIPSocket::Address & internalAddress
    00193     ) const;
    00194
    00199     void InvalidateCache();
    00200
    00213     PBoolean CreateSocket(
    00214       PUDPSocket * & socket,
    00215       const PIPSocket::Address & binding = PIPSocket::GetDefaultIpAny(),
    00216       WORD localPort = 0
    00217     );
    00218
    00232     virtual PBoolean CreateSocketPair(
    00233       PUDPSocket * & socket1,
    00234       PUDPSocket * & socket2,
    00235       const PIPSocket::Address & binding = PIPSocket::GetDefaultIpAny()
    00236     );
    00237
    00240     const PTimeInterval GetTimeout() const { return replyTimeout; }
    00241
    00244     void SetTimeout(
    00245       const PTimeInterval & timeout
    00246     ) { replyTimeout = timeout; }
    00247
    00250     PINDEX GetRetries() const { return pollRetries; }
    00251
    00254     void SetRetries(
    00255       PINDEX retries
    00256     ) { pollRetries = retries; }
    00257
    00263     PINDEX GetSocketsForPairing() const { return numSocketsForPairing; }
    00264
    00270     void SetSocketsForPairing(
    00271       PINDEX numSockets
    00272     ) { numSocketsForPairing = numSockets; }
    00273
    00281     virtual bool IsAvailable(
    00282       const PIPSocket::Address & binding = PIPSocket::GetDefaultIpAny()
    00283     );
    00284
    00285   protected:
    00286     PString            serverHost;
    00287     WORD               serverPort;
    00288     PTimeInterval      replyTimeout;
    00289     PINDEX             pollRetries;
    00290     PINDEX             numSocketsForPairing;
    00291
    00292     bool OpenSocket(PUDPSocket & socket, PortInfo & portInfo, const PIPSocket::Address & binding);
    00293
    00294     NatTypes           natType;
    00295     PIPSocket::Address cachedServerAddress;
    00296     PIPSocket::Address cachedExternalAddress;
    00297     PIPSocket::Address interfaceAddress;
    00298     PTime              timeAddressObtained;
    00299 };
    00300
    00301
    00302 inline ostream & operator<<(ostream & strm, PSTUNClient::NatTypes type) { return strm << PSTUNClient::GetNatTypeString(type); }
    00303
    00304
    00305 #endif // PTLIB_PSTUN_H
    00306
    00307
    00308 // End of file ////////////////////////////////////////////////////////////////