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
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071 #ifndef __STUN_H__
00072 #define __STUN_H__
00073
00074 #include <stdio.h>
00075 #include <time.h>
00076 #include <ortp/port.h>
00077 #include <ortp/stun_udp.h>
00078
00079
00080
00081 #define STUN_VERSION "0.94"
00082
00083 #define STUN_MAX_STRING 256
00084 #define STUN_MAX_UNKNOWN_ATTRIBUTES 8
00085 #define STUN_MAX_MESSAGE_SIZE 2048
00086
00087 #define STUN_PORT 3478
00088
00089
00090 typedef unsigned char UInt8;
00091 typedef unsigned short UInt16;
00092 typedef unsigned int UInt32;
00093 #if defined(WIN32) || defined(_WIN32_WCE)
00094 typedef unsigned __int64 UInt64;
00095 #else
00096 typedef unsigned long long UInt64;
00097 #endif
00098 typedef struct { unsigned char octet[16]; } UInt128;
00099
00100
00101 #define IPv4Family 0x01
00102 #define IPv6Family 0x02
00103
00104
00105 #define ChangeIpFlag 0x04
00106 #define ChangePortFlag 0x02
00107
00108
00109 #define MappedAddress 0x0001
00110 #define ResponseAddress 0x0002
00111 #define ChangeRequest 0x0003
00112 #define SourceAddress 0x0004
00113 #define ChangedAddress 0x0005
00114 #define Username 0x0006
00115 #define Password 0x0007
00116 #define MessageIntegrity 0x0008
00117 #define ErrorCode 0x0009
00118 #define UnknownAttribute 0x000A
00119 #define ReflectedFrom 0x000B
00120 #define XorMappedAddress 0x0020
00121 #define XorOnly 0x0021
00122 #define ServerName 0x0022
00123 #define SecondaryAddress 0x0050
00124
00125
00126 #define BindRequestMsg 0x0001
00127 #define BindResponseMsg 0x0101
00128 #define BindErrorResponseMsg 0x0111
00129 #define SharedSecretRequestMsg 0x0002
00130 #define SharedSecretResponseMsg 0x0102
00131 #define SharedSecretErrorResponseMsg 0x0112
00132
00133 typedef struct
00134 {
00135 UInt16 msgType;
00136 UInt16 msgLength;
00137 UInt128 id;
00138 } StunMsgHdr;
00139
00140
00141 typedef struct
00142 {
00143 UInt16 type;
00144 UInt16 length;
00145 } StunAtrHdr;
00146
00147 typedef struct
00148 {
00149 UInt16 port;
00150 UInt32 addr;
00151 } StunAddress4;
00152
00153 typedef struct
00154 {
00155 UInt8 pad;
00156 UInt8 family;
00157 StunAddress4 ipv4;
00158 } StunAtrAddress4;
00159
00160 typedef struct
00161 {
00162 UInt32 value;
00163 } StunAtrChangeRequest;
00164
00165 typedef struct
00166 {
00167 UInt16 pad;
00168 UInt8 errorClass;
00169 UInt8 number;
00170 char reason[STUN_MAX_STRING];
00171 UInt16 sizeReason;
00172 } StunAtrError;
00173
00174 typedef struct
00175 {
00176 UInt16 attrType[STUN_MAX_UNKNOWN_ATTRIBUTES];
00177 UInt16 numAttributes;
00178 } StunAtrUnknown;
00179
00180 typedef struct
00181 {
00182 char value[STUN_MAX_STRING];
00183 UInt16 sizeValue;
00184 } StunAtrString;
00185
00186 typedef struct
00187 {
00188 char hash[20];
00189 } StunAtrIntegrity;
00190
00191 typedef enum
00192 {
00193 HmacUnkown=0,
00194 HmacOK,
00195 HmacBadUserName,
00196 HmacUnkownUserName,
00197 HmacFailed
00198 } StunHmacStatus;
00199
00200 typedef struct
00201 {
00202 StunMsgHdr msgHdr;
00203
00204 bool_t hasMappedAddress;
00205 StunAtrAddress4 mappedAddress;
00206
00207 bool_t hasResponseAddress;
00208 StunAtrAddress4 responseAddress;
00209
00210 bool_t hasChangeRequest;
00211 StunAtrChangeRequest changeRequest;
00212
00213 bool_t hasSourceAddress;
00214 StunAtrAddress4 sourceAddress;
00215
00216 bool_t hasChangedAddress;
00217 StunAtrAddress4 changedAddress;
00218
00219 bool_t hasUsername;
00220 StunAtrString username;
00221
00222 bool_t hasPassword;
00223 StunAtrString password;
00224
00225 bool_t hasMessageIntegrity;
00226 StunAtrIntegrity messageIntegrity;
00227
00228 bool_t hasErrorCode;
00229 StunAtrError errorCode;
00230
00231 bool_t hasUnknownAttributes;
00232 StunAtrUnknown unknownAttributes;
00233
00234 bool_t hasReflectedFrom;
00235 StunAtrAddress4 reflectedFrom;
00236
00237 bool_t hasXorMappedAddress;
00238 StunAtrAddress4 xorMappedAddress;
00239
00240 bool_t xorOnly;
00241
00242 bool_t hasServerName;
00243 StunAtrString serverName;
00244
00245 bool_t hasSecondaryAddress;
00246 StunAtrAddress4 secondaryAddress;
00247 } StunMessage;
00248
00249
00250
00251 typedef enum
00252 {
00253 StunTypeUnknown=0,
00254 StunTypeOpen,
00255 StunTypeConeNat,
00256 StunTypeRestrictedNat,
00257 StunTypePortRestrictedNat,
00258 StunTypeSymNat,
00259 StunTypeSymFirewall,
00260 StunTypeBlocked,
00261 StunTypeFailure
00262 } NatType;
00263
00264
00265 #define MAX_MEDIA_RELAYS 500
00266 #define MAX_RTP_MSG_SIZE 1500
00267 #define MEDIA_RELAY_TIMEOUT 3*60
00268
00269 typedef struct
00270 {
00271 int relayPort;
00272 int fd;
00273 StunAddress4 destination;
00274 time_t expireTime;
00275 } StunMediaRelay;
00276
00277 typedef struct
00278 {
00279 StunAddress4 myAddr;
00280 StunAddress4 altAddr;
00281 Socket myFd;
00282 Socket altPortFd;
00283 Socket altIpFd;
00284 Socket altIpPortFd;
00285 bool_t relay;
00286 StunMediaRelay relays[MAX_MEDIA_RELAYS];
00287 } StunServerInfo;
00288
00289 bool_t
00290 stunParseMessage( char* buf,
00291 unsigned int bufLen,
00292 StunMessage *message,
00293 bool_t verbose );
00294
00295 void
00296 stunBuildReqSimple( StunMessage* msg,
00297 const StunAtrString *username,
00298 bool_t changePort, bool_t changeIp, unsigned int id );
00299
00300 unsigned int
00301 stunEncodeMessage( const StunMessage *message,
00302 char* buf,
00303 unsigned int bufLen,
00304 const StunAtrString *password,
00305 bool_t verbose);
00306
00307 void
00308 stunCreateUserName(const StunAddress4 *addr, StunAtrString* username);
00309
00310 void
00311 stunGetUserNameAndPassword( const StunAddress4 *dest,
00312 StunAtrString* username,
00313 StunAtrString* password);
00314
00315 void
00316 stunCreatePassword(const StunAtrString *username, StunAtrString* password);
00317
00318 int
00319 stunRand(void);
00320
00321 UInt64
00322 stunGetSystemTimeSecs(void);
00323
00324
00325 bool_t
00326 stunParseServerName( char* serverName, StunAddress4 *stunServerAddr);
00327
00328 bool_t
00329 stunParseHostName( char* peerName,
00330 UInt32 *ip,
00331 UInt16 *portVal,
00332 UInt16 defaultPort );
00333
00334
00335
00336 bool_t
00337 stunInitServer(StunServerInfo *info,
00338 const StunAddress4 *myAddr,
00339 const StunAddress4 *altAddr,
00340 int startMediaPort,
00341 bool_t verbose);
00342
00343 void
00344 stunStopServer(StunServerInfo *info);
00345
00346 #if 0
00347
00348 bool_t
00349 stunServerProcess(StunServerInfo *info, bool_t verbose);
00350 #endif
00351
00352
00353 int
00354 stunFindLocalInterfaces(UInt32* addresses, int maxSize );
00355
00356 int
00357 stunTest( StunAddress4 *dest, int testNum, bool_t verbose, StunAddress4* srcAddr, StunAddress4 *sMappedAddr, StunAddress4* sChangedAddr);
00358
00359 NatType
00360 stunNatType( StunAddress4 *dest, bool_t verbose,
00361 bool_t* preservePort,
00362 bool_t* hairpin ,
00363 int port,
00364 StunAddress4* sAddr
00365 );
00366
00367 bool_t
00368 stunServerProcessMsg( char* buf,
00369 unsigned int bufLen,
00370 StunAddress4 *from,
00371 StunAddress4 *secondary,
00372 StunAddress4 *myAddr,
00373 StunAddress4 *altAddr,
00374 StunMessage *resp,
00375 StunAddress4 *destination,
00376 StunAtrString *hmacPassword,
00377 bool_t* changePort,
00378 bool_t* changeIp,
00379 bool_t verbose);
00380
00381 int
00382 stunOpenSocket( StunAddress4 *dest,
00383 StunAddress4* mappedAddr,
00384 int port,
00385 StunAddress4* srcAddr,
00386 bool_t verbose );
00387
00388 bool_t
00389 stunOpenSocketPair(StunAddress4 *dest,
00390 StunAddress4* mapAddr_rtp,
00391 StunAddress4* mapAddr_rtcp,
00392 int* fd1, int* fd2,
00393 int srcPort, StunAddress4* srcAddr,
00394 bool_t verbose);
00395
00396 #endif
00397