stun.h

Go to the documentation of this file.
00001 #ifndef STUN_H
00002 #define STUN_H
00003 
00004 #include <iostream>
00005 #include <time.h>
00006 
00007 // if you change this version, change in makefile too 
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 // define some basic types
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 // define  flags  
00032 const UInt32 ChangeIpFlag   = 0x04;
00033 const UInt32 ChangePortFlag = 0x02;
00034 
00035 // define  stun attribute
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; // Non standard extention
00051 
00052 // define types for a stun message 
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; // all 0
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 // Define enum with different types of NAT 
00178 typedef enum 
00179 {
00180    StunTypeUnknown=0,
00181    StunTypeFailure,
00182    StunTypeOpen,
00183    StunTypeBlocked,
00184 
00185    StunTypeIndependentFilter,
00186    StunTypeDependentFilter,
00187    StunTypePortDependedFilter,
00188    StunTypeDependentMapping,
00189 
00190    //StunTypeConeNat,
00191    //StunTypeRestrictedNat,
00192    //StunTypePortRestrictedNat,
00193    //StunTypeSymNat,
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;       // media relay port
00211       int fd;              // media relay file descriptor
00212       StunAddress4 destination; // NAT IP:port
00213       time_t expireTime;      // if no activity after time, close the socket 
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; // true if media relaying is to be done
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, // if set, is return for if NAT preservers ports or not
00299              bool* hairpin=0 ,  // if set, is the return for if NAT will hairpin packets
00300              int port=0, // port to use for the test, 0 to choose random port
00301              StunAddress4* sAddr=0 // NIC to use 
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  * The Vovida Software License, Version 1.0  *  * Copyright (c) 2000 Vovida Networks, Inc.  All rights reserved. *  * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: *  * 1. Redistributions of source code must retain the above copyright *    notice, this list of conditions and the following disclaimer. *  * 2. Redistributions in binary form must reproduce the above copyright *    notice, this list of conditions and the following disclaimer in *    the documentation and/or other materials provided with the *    distribution. *  * 3. The names "VOCAL", "Vovida Open Communication Application Library", *    and "Vovida Open Communication Application Library (VOCAL)" must *    not be used to endorse or promote products derived from this *    software without prior written permission. For written *    permission, please contact vocal@vovida.org. * * 4. Products derived from this software may not be called "VOCAL", nor *    may "VOCAL" appear in their name, without prior written *    permission of Vovida Networks, Inc. *  * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND * NON-INFRINGEMENT ARE DISCLAIMED.  IN NO EVENT SHALL VOVIDA * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH * DAMAGE. *  * ==================================================================== *  * This software consists of voluntary contributions made by Vovida * Networks, Inc. and many individuals on behalf of Vovida Networks, * Inc.  For more information on Vovida Networks, Inc., please see * <http://www.vovida.org/>.
00346  *
00347  */
00348 
00349 // Local Variables:
00350 // mode:c++
00351 // c-file-style:"ellemtel"
00352 // c-file-offsets:((case-label . +))
00353 // indent-tabs-mode:nil
00354 // End:
00355 
Generated on Wed May 26 16:21:15 2010 for OverSim by  doxygen 1.6.3