00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
#ifndef _PSTUN_H
00055
#define _PSTUN_H
00056
00057
#ifdef P_USE_PRAGMA
00058
#pragma interface
00059
#endif
00060
00061
#include <ptlib.h>
00062
#include <ptlib/sockets.h>
00063
00064
00067 class PSTUNUDPSocket :
public PUDPSocket
00068 {
00069
PCLASSINFO(
PSTUNUDPSocket,
PUDPSocket);
00070
public:
00071
PSTUNUDPSocket();
00072
00073
virtual BOOL
GetLocalAddress(
00074 Address & addr
00075 );
00076
virtual BOOL GetLocalAddress(
00077 Address & addr,
00078 WORD & port
00079 );
00080
00081
protected:
00082 PIPSocket::Address externalIP;
00083
00084
friend class PSTUNClient;
00085 };
00086
00087
00090 class PSTUNClient :
public PObject
00091 {
00092
PCLASSINFO(
PSTUNClient,
PObject);
00093
public:
00094
enum {
00095
DefaultPort = 3478
00096 };
00097
00098
PSTUNClient(
00099
const PString & server,
00100 WORD portBase = 0,
00101 WORD portMax = 0,
00102 WORD portPairBase = 0,
00103 WORD portPairMax = 0
00104 );
00105
PSTUNClient(
00106
const PIPSocket::Address &
serverAddress,
00107 WORD
serverPort =
DefaultPort,
00108 WORD portBase = 0,
00109 WORD portMax = 0,
00110 WORD portPairBase = 0,
00111 WORD portPairMax = 0
00112 );
00113
00114
00117
PString GetServer()
const;
00118
00125 BOOL
SetServer(
00126
const PString & server
00127 );
00128
00132 BOOL SetServer(
00133
const PIPSocket::Address & serverAddress,
00134 WORD
serverPort = 0
00135 );
00136
00147
void SetPortRanges(
00148 WORD portBase,
00149 WORD portMax = 0,
00150 WORD portPairBase = 0,
00151 WORD portPairMax = 0
00152 );
00153
00154
00155 enum NatTypes {
00156
UnknownNat,
00157
OpenNat,
00158
ConeNat,
00159
RestrictedNat,
00160
PortRestrictedNat,
00161
SymmetricNat,
00162
SymmetricFirewall,
00163
BlockedNat,
00164
PartialBlockedNat,
00165
NumNatTypes
00166 };
00167
00172 NatTypes GetNatType(
00173 BOOL force = FALSE
00174 );
00175
00179
PString GetNatTypeName(
00180 BOOL force = FALSE
00181 );
00182
00190 BOOL GetExternalAddress(
00191
PIPSocket::Address & externalAddress,
00192
const PTimeInterval & maxAge = 1000
00193 );
00194
00207 BOOL CreateSocket(
00208
PUDPSocket * & socket
00209 );
00210
00224 BOOL CreateSocketPair(
00225
PUDPSocket * & socket1,
00226
PUDPSocket * & socket2
00227 );
00228
00229
protected:
00230
void Construct();
00231
00232 PIPSocket::Address serverAddress;
00233 WORD serverPort;
00234
00235 struct PortInfo {
00236 PMutex mutex;
00237 WORD
basePort;
00238 WORD
maxPort;
00239 WORD
currentPort;
00240 }
singlePortInfo,
pairedPortInfo;
00241
bool OpenSocket(
PUDPSocket & socket,
PortInfo & portInfo)
const;
00242
00243 int numSocketsForPairing;
00244
00245 NatTypes natType;
00246 PIPSocket::Address cachedExternalAddress;
00247 PTime timeAddressObtained;
00248 };
00249
00250
00251
00252
#endif // _PSTUN_H
00253
00254
00255