include/ortp/stun.h

00001  /*
00002   The oRTP library is an RTP (Realtime Transport Protocol - rfc3550) stack.
00003   Copyright (C) 2001  Simon MORLAT simon.morlat@linphone.org
00004 
00005   This library is free software; you can redistribute it and/or
00006   modify it under the terms of the GNU Lesser General Public
00007   License as published by the Free Software Foundation; either
00008   version 2.1 of the License, or (at your option) any later version.
00009 
00010   This library is distributed in the hope that it will be useful,
00011   but WITHOUT ANY WARRANTY; without even the implied warranty of
00012   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013   Lesser General Public License for more details.
00014 
00015   You should have received a copy of the GNU Lesser General Public
00016   License along with this library; if not, write to the Free Software
00017   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018 */
00019 
00020 /* ====================================================================
00021  * The Vovida Software License, Version 1.0 
00022  * 
00023  * Copyright (c) 2000 Vovida Networks, Inc.  All rights reserved.
00024  * 
00025  * Redistribution and use in source and binary forms, with or without
00026  * modification, are permitted provided that the following conditions
00027  * are met:
00028  * 
00029  * 1. Redistributions of source code must retain the above copyright
00030  *    notice, this list of conditions and the following disclaimer.
00031  * 
00032  * 2. Redistributions in binary form must reproduce the above copyright
00033  *    notice, this list of conditions and the following disclaimer in
00034  *    the documentation and/or other materials provided with the
00035  *    distribution.
00036  * 
00037  * 3. The names "VOCAL", "Vovida Open Communication Application Library",
00038  *    and "Vovida Open Communication Application Library (VOCAL)" must
00039  *    not be used to endorse or promote products derived from this
00040  *    software without prior written permission. For written
00041  *    permission, please contact vocal@vovida.org.
00042  *
00043  * 4. Products derived from this software may not be called "VOCAL", nor
00044  *    may "VOCAL" appear in their name, without prior written
00045  *    permission of Vovida Networks, Inc.
00046  * 
00047  * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
00048  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00049  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
00050  * NON-INFRINGEMENT ARE DISCLAIMED.  IN NO EVENT SHALL VOVIDA
00051  * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
00052  * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
00053  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00054  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00055  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
00056  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00057  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
00058  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
00059  * DAMAGE.
00060  * 
00061  * ====================================================================
00062  * 
00063  * This software consists of voluntary contributions made by Vovida
00064  * Networks, Inc. and many individuals on behalf of Vovida Networks,
00065  * Inc.  For more information on Vovida Networks, Inc., please see
00066  * <http://www.vovida.org/>.
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 /* if you change this version, change in makefile too  */
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 /* define some basic types */
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 /* define a structure to hold a stun address  */
00101 #define  IPv4Family  0x01
00102 #define  IPv6Family  0x02
00103 
00104 /* define  flags  */
00105 #define ChangeIpFlag    0x04
00106 #define ChangePortFlag  0x02
00107 
00108 /* define  stun attribute */
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 /* Non standard extention */
00124 
00125 /* define types for a stun message */
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; /* all 0 */
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 /* Define enum with different types of NAT */
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;       /* media relay port */
00272       int fd;              /* media relay file descriptor */
00273       StunAddress4 destination; /* NAT IP:port */
00274       time_t expireTime;      /* if no activity after time, close the socket */
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; /* true if media relaying is to be done */
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 /* find the IP address of a the specified stun server - return false is fails parse  */
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 /* return true if all is OK 
00335    Create a media relay and do the STERN thing if startMediaPort is non-zero */
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 /* no usefull here */
00347 /* return true if all is OK */
00348 bool_t
00349 stunServerProcess(StunServerInfo *info, bool_t verbose);
00350 #endif
00351 
00352 /* returns number of address found - take array or addres */
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, /* if set, is return for if NAT preservers ports or not */
00362              bool_t* hairpin ,  /* if set, is the return for if NAT will hairpin packets */
00363              int port, /* port to use for the test, 0 to choose random port */
00364              StunAddress4* sAddr /* NIC to use */
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 

Generated on Thu Apr 5 18:59:20 2007 for oRTP by  doxygen 1.5.1