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