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