oRTP: include/ortp/stun.h Source File

- Main Page
- Data Structures
- Files

# 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 #ifdef __cplusplus
    00081 extern "C"
    00082 {
    00083 #endif
    00084
    00085 /* if you change this version, change in makefile too  */
    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 /* define some basic types */
    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 /* define a structure to hold a stun address  */
    00106 #define  IPv4Family  0x01
    00107 #define  IPv6Family  0x02
    00108
    00109 /* define  flags  */
    00110 #define ChangeIpFlag    0x04
    00111 #define ChangePortFlag  0x02
    00112
    00113 /* define  stun attribute */
    00114 #define MappedAddress     0x0001
    00115 #define ResponseAddress   0x0002
    00116 #define ChangeRequest     0x0003
    00117 #define SourceAddress     0x0004
    00118 #define ChangedAddress    0x0005
    00119 #define STUNUsername      0x0006 /* Username is too common: rename to avoid conflict */
    00120 #define STUNPassword      0x0007 /* Password is too common: rename to avoid conflict */
    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 /* Non standard extention */
    00129
    00130 /* define types for a stun message */
    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; /* all 0 */
    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 /* Define enum with different types of NAT */
    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;       /* media relay port */
    00277       int fd;              /* media relay file descriptor */
    00278       StunAddress4 destination; /* NAT IP:port */
    00279       time_t expireTime;      /* if no activity after time, close the socket */
    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; /* true if media relaying is to be done */
    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 /* find the IP address of a the specified stun server - return false is fails parse  */
    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 /* return true if all is OK
    00340    Create a media relay and do the STERN thing if startMediaPort is non-zero */
    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 /* no usefull here */
    00352 /* return true if all is OK */
    00353 bool_t
    00354 stunServerProcess(StunServerInfo *info, bool_t verbose);
    00355 #endif
    00356
    00357 /* returns number of address found - take array or addres */
    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, /* if set, is return for if NAT preservers ports or not */
    00367              bool_t* hairpin ,  /* if set, is the return for if NAT will hairpin packets */
    00368              int port, /* port to use for the test, 0 to choose random port */
    00369              StunAddress4* sAddr /* NIC to use */
    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