/home/tomh/openhip/hip-0.5/src/include/hip/hip_stun.h

Go to the documentation of this file.
00001 /*
00002  * Host Identity Protocol
00003  * 
00004  *  STUN client based on the code provided by Vovida Networks, Inc.
00005  *  (see end of file)
00006  *  Translated from C++ code to C code by Vivien Schmitt
00007  *
00008  *  hip_stun.h
00009  *
00010  * Author : Vivien Schmitt, <schmitt@netlab.nec.de>
00011  *
00012  * STUN client used for NAT detection
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 /* if you change this version, change in makefile too */
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 /* define some basic types*/
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 /* __WIN32__ */
00046 typedef struct { unsigned char octet[16]; }  UInt128;
00047 
00048 /*/ define a structure to hold a stun address */
00049 extern UInt8  IPv4Family;
00050 extern UInt8  IPv6Family;
00051 
00052 /* define  flags  */
00053 extern UInt32 ChangeIpFlag;
00054 extern UInt32 ChangePortFlag;
00055 
00056 /* define  stun attribute*/
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; /* Non standard extention*/
00072 
00073 /* define types for a stun message */
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; /* all 0*/
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 /* Define enum with different types of NAT */
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;       /* media relay port*/
00221       int fd;              /* media relay file descriptor*/
00222       StunAddress4 destination; /* NAT IP:port*/
00223       time_t expireTime;      /* if no activity after time, close the socket */
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; /* true if media relaying is to be done */
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 /*/ find the IP address of a the specified stun server - return false is fails parse */
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 /*/ return true if all is OK
00288 int
00289 stunInitServer(StunServerInfo *info, 
00290                const StunAddress4 *myAddr, 
00291                const StunAddress4 *altAddr,
00292                int startMediaPort,
00293                int verbose);
00294 
00295 void
00296 stunStopServer(StunServerInfo *info);
00297 
00298 /*/ return true if all is OK */
00299 int
00300 stunServerProcess(StunServerInfo *info, int verbose);
00301 
00302 /*/ returns number of address found - take array or addres */
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, /* if set, is return for if NAT preservers ports or not */
00312              int* hairpin ,  /* if set, is the return for if NAT will hairpin packets */
00313              int port, /* port to use for the test, 0 to choose random port */
00314              StunAddress4* sAddr /* NIC to use  */
00315    );
00316 
00317 /*/ prints a StunAddress */
00318 /*std::ostream &
00319 operator<<( std::ostream& strm, const StunAddress4& addr);
00320 
00321 std::ostream& 
00322 operator<< ( std::ostream& strm, const UInt128& );
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  * The Vovida Software License, Version 1.0 
00360  * 
00361  * Copyright (c) 2000 Vovida Networks, Inc.  All rights reserved.
00362  * 
00363  * Redistribution and use in source and binary forms, with or without
00364  * modification, are permitted provided that the following conditions
00365  * are met:
00366  * 
00367  * 1. Redistributions of source code must retain the above copyright
00368  *    notice, this list of conditions and the following disclaimer.
00369  * 
00370  * 2. Redistributions in binary form must reproduce the above copyright
00371  *    notice, this list of conditions and the following disclaimer in
00372  *    the documentation and/or other materials provided with the
00373  *    distribution.
00374  * 
00375  * 3. The names "VOCAL", "Vovida Open Communication Application Library",
00376  *    and "Vovida Open Communication Application Library (VOCAL)" must
00377  *    not be used to endorse or promote products derived from this
00378  *    software without prior written permission. For written
00379  *    permission, please contact vocal@vovida.org.
00380  *
00381  * 4. Products derived from this software may not be called "VOCAL", nor
00382  *    may "VOCAL" appear in their name, without prior written
00383  *    permission of Vovida Networks, Inc.
00384  * 
00385  * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
00386  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00387  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
00388  * NON-INFRINGEMENT ARE DISCLAIMED.  IN NO EVENT SHALL VOVIDA
00389  * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
00390  * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
00391  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00392  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00393  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
00394  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00395  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
00396  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
00397  * DAMAGE.
00398  * 
00399  * ====================================================================
00400  * 
00401  * This software consists of voluntary contributions made by Vovida
00402  * Networks, Inc. and many individuals on behalf of Vovida Networks,
00403  * Inc.  For more information on Vovida Networks, Inc., please see
00404  * <http://www.vovida.org/>.
00405  *
00406  */

Generated on Mon Oct 22 11:43:43 2007 for OpenHIP by  doxygen 1.5.1