00001 #ifndef STUN_H
00002 #define STUN_H
00003
00004 #include <iostream>
00005 #include <time.h>
00006
00007
00008 #define STUN_VERSION "0.96"
00009
00010 #define STUN_MAX_STRING 256
00011 #define STUN_MAX_UNKNOWN_ATTRIBUTES 8
00012 #define STUN_MAX_MESSAGE_SIZE 2048
00013
00014 #define STUN_PORT 3478
00015
00016
00017 typedef unsigned char UInt8;
00018 typedef unsigned short UInt16;
00019 typedef unsigned int UInt32;
00020 #if defined( WIN32 )
00021 typedef unsigned __int64 UInt64;
00022 #else
00023 typedef unsigned long long UInt64;
00024 #endif
00025 typedef struct { unsigned char octet[16]; } UInt128;
00026
00028 const UInt8 IPv4Family = 0x01;
00029 const UInt8 IPv6Family = 0x02;
00030
00031
00032 const UInt32 ChangeIpFlag = 0x04;
00033 const UInt32 ChangePortFlag = 0x02;
00034
00035
00036 const UInt16 MappedAddress = 0x0001;
00037 const UInt16 ResponseAddress = 0x0002;
00038 const UInt16 ChangeRequest = 0x0003;
00039 const UInt16 SourceAddress = 0x0004;
00040 const UInt16 ChangedAddress = 0x0005;
00041 const UInt16 Username = 0x0006;
00042 const UInt16 Password = 0x0007;
00043 const UInt16 MessageIntegrity = 0x0008;
00044 const UInt16 ErrorCode = 0x0009;
00045 const UInt16 UnknownAttribute = 0x000A;
00046 const UInt16 ReflectedFrom = 0x000B;
00047 const UInt16 XorMappedAddress = 0x8020;
00048 const UInt16 XorOnly = 0x0021;
00049 const UInt16 ServerName = 0x8022;
00050 const UInt16 SecondaryAddress = 0x8050;
00051
00052
00053 const UInt16 BindRequestMsg = 0x0001;
00054 const UInt16 BindResponseMsg = 0x0101;
00055 const UInt16 BindErrorResponseMsg = 0x0111;
00056 const UInt16 SharedSecretRequestMsg = 0x0002;
00057 const UInt16 SharedSecretResponseMsg = 0x0102;
00058 const UInt16 SharedSecretErrorResponseMsg = 0x0112;
00059
00060 typedef struct
00061 {
00062 UInt16 msgType;
00063 UInt16 msgLength;
00064 UInt128 id;
00065 } StunMsgHdr;
00066
00067
00068 typedef struct
00069 {
00070 UInt16 type;
00071 UInt16 length;
00072 } StunAtrHdr;
00073
00074 typedef struct
00075 {
00076 UInt16 port;
00077 UInt32 addr;
00078 } StunAddress4;
00079
00080 typedef struct
00081 {
00082 UInt8 pad;
00083 UInt8 family;
00084 StunAddress4 ipv4;
00085 } StunAtrAddress4;
00086
00087 typedef struct
00088 {
00089 UInt32 value;
00090 } StunAtrChangeRequest;
00091
00092 typedef struct
00093 {
00094 UInt16 pad;
00095 UInt8 errorClass;
00096 UInt8 number;
00097 char reason[STUN_MAX_STRING];
00098 UInt16 sizeReason;
00099 } StunAtrError;
00100
00101 typedef struct
00102 {
00103 UInt16 attrType[STUN_MAX_UNKNOWN_ATTRIBUTES];
00104 UInt16 numAttributes;
00105 } StunAtrUnknown;
00106
00107 typedef struct
00108 {
00109 char value[STUN_MAX_STRING];
00110 UInt16 sizeValue;
00111 } StunAtrString;
00112
00113 typedef struct
00114 {
00115 char hash[20];
00116 } StunAtrIntegrity;
00117
00118 typedef enum
00119 {
00120 HmacUnkown=0,
00121 HmacOK,
00122 HmacBadUserName,
00123 HmacUnkownUserName,
00124 HmacFailed,
00125 } StunHmacStatus;
00126
00127 typedef struct
00128 {
00129 StunMsgHdr msgHdr;
00130
00131 bool hasMappedAddress;
00132 StunAtrAddress4 mappedAddress;
00133
00134 bool hasResponseAddress;
00135 StunAtrAddress4 responseAddress;
00136
00137 bool hasChangeRequest;
00138 StunAtrChangeRequest changeRequest;
00139
00140 bool hasSourceAddress;
00141 StunAtrAddress4 sourceAddress;
00142
00143 bool hasChangedAddress;
00144 StunAtrAddress4 changedAddress;
00145
00146 bool hasUsername;
00147 StunAtrString username;
00148
00149 bool hasPassword;
00150 StunAtrString password;
00151
00152 bool hasMessageIntegrity;
00153 StunAtrIntegrity messageIntegrity;
00154
00155 bool hasErrorCode;
00156 StunAtrError errorCode;
00157
00158 bool hasUnknownAttributes;
00159 StunAtrUnknown unknownAttributes;
00160
00161 bool hasReflectedFrom;
00162 StunAtrAddress4 reflectedFrom;
00163
00164 bool hasXorMappedAddress;
00165 StunAtrAddress4 xorMappedAddress;
00166
00167 bool xorOnly;
00168
00169 bool hasServerName;
00170 StunAtrString serverName;
00171
00172 bool hasSecondaryAddress;
00173 StunAtrAddress4 secondaryAddress;
00174 } StunMessage;
00175
00176
00177
00178 typedef enum
00179 {
00180 StunTypeUnknown=0,
00181 StunTypeFailure,
00182 StunTypeOpen,
00183 StunTypeBlocked,
00184
00185 StunTypeIndependentFilter,
00186 StunTypeDependentFilter,
00187 StunTypePortDependedFilter,
00188 StunTypeDependentMapping,
00189
00190
00191
00192
00193
00194
00195 StunTypeFirewall,
00196 } NatType;
00197
00198 #ifdef WIN32
00199 typedef SOCKET Socket;
00200 #else
00201 typedef int Socket;
00202 #endif
00203
00204 #define MAX_MEDIA_RELAYS 500
00205 #define MAX_RTP_MSG_SIZE 1500
00206 #define MEDIA_RELAY_TIMEOUT 3*60
00207
00208 typedef struct
00209 {
00210 int relayPort;
00211 int fd;
00212 StunAddress4 destination;
00213 time_t expireTime;
00214 } StunMediaRelay;
00215
00216 typedef struct
00217 {
00218 StunAddress4 myAddr;
00219 StunAddress4 altAddr;
00220 Socket myFd;
00221 Socket altPortFd;
00222 Socket altIpFd;
00223 Socket altIpPortFd;
00224 bool relay;
00225 StunMediaRelay relays[MAX_MEDIA_RELAYS];
00226 } StunServerInfo;
00227
00228 bool
00229 stunParseMessage( char* buf,
00230 unsigned int bufLen,
00231 StunMessage& message,
00232 bool verbose );
00233
00234 void
00235 stunBuildReqSimple( StunMessage* msg,
00236 const StunAtrString& username,
00237 bool changePort, bool changeIp, unsigned int id=0 );
00238
00239 unsigned int
00240 stunEncodeMessage( const StunMessage& message,
00241 char* buf,
00242 unsigned int bufLen,
00243 const StunAtrString& password,
00244 bool verbose);
00245
00246 void
00247 stunCreateUserName(const StunAddress4& addr, StunAtrString* username);
00248
00249 void
00250 stunGetUserNameAndPassword( const StunAddress4& dest,
00251 StunAtrString* username,
00252 StunAtrString* password);
00253
00254 void
00255 stunCreatePassword(const StunAtrString& username, StunAtrString* password);
00256
00257 int
00258 stunRand();
00259
00260 UInt64
00261 stunGetSystemTimeSecs();
00262
00264 bool
00265 stunParseServerName( char* serverName, StunAddress4& stunServerAddr);
00266
00267 bool
00268 stunParseHostName( char* peerName,
00269 UInt32& ip,
00270 UInt16& portVal,
00271 UInt16 defaultPort );
00272
00275 bool
00276 stunInitServer(StunServerInfo& info,
00277 const StunAddress4& myAddr,
00278 const StunAddress4& altAddr,
00279 int startMediaPort,
00280 bool verbose);
00281
00282 void
00283 stunStopServer(StunServerInfo& info);
00284
00286 bool
00287 stunServerProcess(StunServerInfo& info, bool verbose);
00288
00290 int
00291 stunFindLocalInterfaces(UInt32* addresses, int maxSize );
00292
00293 void
00294 stunTest( StunAddress4& dest, int testNum, bool verbose, StunAddress4* srcAddr=0 );
00295
00296 NatType
00297 stunNatType( StunAddress4& dest, bool verbose,
00298 bool* preservePort=0,
00299 bool* hairpin=0 ,
00300 int port=0,
00301 StunAddress4* sAddr=0
00302 );
00303
00305 std::ostream&
00306 operator<<( std::ostream& strm, const StunAddress4& addr);
00307
00308 std::ostream&
00309 operator<< ( std::ostream& strm, const UInt128& );
00310
00311
00312 bool
00313 stunServerProcessMsg( char* buf,
00314 unsigned int bufLen,
00315 StunAddress4& from,
00316 StunAddress4& myAddr,
00317 StunAddress4& altAddr,
00318 StunMessage* resp,
00319 StunAddress4* destination,
00320 StunAtrString* hmacPassword,
00321 bool* changePort,
00322 bool* changeIp,
00323 bool verbose);
00324
00325 int
00326 stunOpenSocket( StunAddress4& dest,
00327 StunAddress4* mappedAddr,
00328 int port=0,
00329 StunAddress4* srcAddr=0,
00330 bool verbose=false );
00331
00332 bool
00333 stunOpenSocketPair( StunAddress4& dest, StunAddress4* mappedAddr,
00334 int* fd1, int* fd2,
00335 int srcPort=0, StunAddress4* srcAddr=0,
00336 bool verbose=false);
00337
00338 int
00339 stunRandomPort();
00340
00341 #endif
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355