• Main Page
  • Data Structures
  • Files
  • File List
  • Globals

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.99"
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[12]; }  UInt96;
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 SA_MAPPEDADDRESS     0x0001
00115 #define SA_RESPONSEADDRESS   0x0002 
00116 #define SA_CHANGEREQUEST     0x0003 
00117 #define SA_SOURCEADDRESS     0x0004 
00118 #define SA_CHANGEDADDRESS    0x0005 
00119 #define SA_USERNAME          0x0006
00120 #define SA_PASSWORD          0x0007  
00121 #define SA_MESSAGEINTEGRITY  0x0008
00122 #define SA_ERRORCODE         0x0009
00123 #define SA_UNKNOWNATTRIBUTE  0x000A
00124 #define SA_REFLECTEDFROM     0x000B 
00125 #define SA_REALM             0x0014
00126 #define SA_NONCE             0x0015
00127 #define SA_XORMAPPEDADDRESS  0x0020
00128 
00129 #define SA_XORMAPPEDADDRESS2 0x8020 /* Non standard extention */
00130 #define SA_XORONLY           0x0021 /* deprecated */
00131 #define SA_SECONDARYADDRESS  0x0050 /* Non standard extention */
00132 
00133 #define SA_SOFTWARE          0x8022
00134 #define SA_ALTERNATESERVER   0x8023
00135 #define SA_FINGERPRINT       0x8028
00136 
00137 /* define turn attribute */
00138 #define TA_CHANNELNUMBER       0x000C
00139 #define TA_LIFETIME            0x000D
00140 #define TA_DEPRECATEDBANDWIDTH 0x0010
00141 #define TA_XORPEERADDRESS      0x0012
00142 #define TA_DATA                0x0013
00143 #define TA_XORRELAYEDADDRESS   0x0016
00144 #define TA_EVENPORT            0x0018
00145 #define TA_REQUESTEDTRANSPORT  0x0019
00146 #define TA_DONTFRAGMENT        0x001A
00147 #define TA_DEPRECATEDTIMERVAL  0x0021
00148 #define TA_RESERVATIONTOKEN    0x0022
00149 
00150 #define ICEA_PRIORITY          0x0024
00151 #define ICEA_USECANDIDATE      0x0025
00152 #define ICEA_ICECONTROLLED     0x8029
00153 #define ICEA_ICECONTROLLING    0x802a
00154 
00155 #define STUN_REQUEST           0x0000
00156 #define STUN_INDICATION        0x0010
00157 #define STUN_SUCCESS_RESP      0x0100
00158 #define STUN_ERR_RESP          0x0110
00159 
00160 #define STUN_IS_REQUEST(msg_type)       (((msg_type) & 0x0110) == 0x0000)
00161 #define STUN_IS_INDICATION(msg_type)    (((msg_type) & 0x0110) == 0x0010)
00162 #define STUN_IS_SUCCESS_RESP(msg_type)  (((msg_type) & 0x0110) == 0x0100)
00163 #define STUN_IS_ERR_RESP(msg_type)      (((msg_type) & 0x0110) == 0x0110)
00164 
00165 /* define types for a stun message */
00166 #define STUN_METHOD_BINDING           0x0001
00167 #define TURN_MEDHOD_ALLOCATE          0x0003 //(only request/response semantics defined)
00168 #define TURN_METHOD_REFRESH           0x0004 //(only request/response semantics defined)
00169 #define TURN_METHOD_CREATEPERMISSION  0x0008 //(only request/response semantics defined
00170 #define TURN_METHOD_CHANNELBIND       0x0009 //(only request/response semantics defined)
00171 
00172 //#define BindResponseMsg               0x0101
00173 //#define BindErrorResponseMsg          0x0111
00174 #define SharedSecretRequestMsg        0x0002
00175 #define SharedSecretResponseMsg       0x0102
00176 #define SharedSecretErrorResponseMsg  0x0112
00177 
00178 #define TURN_INDICATION_SEND          0x0006 //(only indication semantics defined)
00179 #define TURN_INDICATION_DATA          0x0007 //(only indication semantics defined)
00180 
00181 typedef struct 
00182 {
00183       UInt16 msgType;
00184       UInt16 msgLength;
00185       UInt32 magic_cookie;
00186       UInt96 tr_id;
00187 } StunMsgHdr;
00188 
00189 
00190 typedef struct
00191 {
00192       UInt16 type;
00193       UInt16 length;
00194 } StunAtrHdr;
00195 
00196 typedef struct
00197 {
00198       UInt16 port;
00199       UInt32 addr;
00200 } StunAddress4;
00201 
00202 typedef struct
00203 {
00204       UInt8 pad;
00205       UInt8 family;
00206       StunAddress4 ipv4;
00207 } StunAtrAddress4;
00208 
00209 typedef struct
00210 {
00211       UInt32 value;
00212 } StunAtrChangeRequest;
00213 
00214 typedef struct
00215 {
00216       UInt16 pad; /* all 0 */
00217       UInt8 errorClass;
00218       UInt8 number;
00219       char reason[STUN_MAX_STRING];
00220       UInt16 sizeReason;
00221 } StunAtrError;
00222 
00223 typedef struct
00224 {
00225       UInt16 attrType[STUN_MAX_UNKNOWN_ATTRIBUTES];
00226       UInt16 numAttributes;
00227 } StunAtrUnknown;
00228 
00229 typedef struct
00230 {
00231       UInt16 channelNumber;
00232       UInt16 rffu; /* Reserved For Future Use */
00233 } TurnAtrChannelNumber;
00234 
00235 typedef struct
00236 {
00237       UInt32 lifetime;
00238 } TurnAtrLifetime;
00239 
00240 typedef struct
00241 {
00242       char value[1500];      
00243       UInt16 sizeValue;
00244 } TurnAtrData;
00245 
00246 typedef struct
00247 {
00248       UInt8 proto;
00249       UInt8 pad1;
00250       UInt8 pad2;
00251       UInt8 pad3;
00252 } TurnAtrRequestedTransport;
00253 
00254 typedef struct
00255 {
00256       UInt64 value;
00257 } TurnAtrReservationToken;
00258 
00259 typedef struct
00260 {
00261       UInt32 fingerprint;
00262 } StunAtrFingerprint;
00263 
00264 
00265 typedef struct
00266 {
00267       char value[STUN_MAX_STRING];      
00268       UInt16 sizeValue;
00269 } StunAtrString;
00270 
00271 typedef struct
00272 {
00273       UInt32 priority;
00274 } IceAtrPriority;
00275 
00276 typedef struct
00277 {
00278       UInt64 value;
00279 } IceAtrIceControll;
00280 
00281 typedef struct
00282 {
00283       char hash[20];
00284 } StunAtrIntegrity;
00285 
00286 typedef enum 
00287 {
00288    HmacUnkown=0,
00289    HmacOK,
00290    HmacBadUserName,
00291    HmacUnkownUserName,
00292    HmacFailed
00293 } StunHmacStatus;
00294 
00295 
00296 typedef struct
00297 {
00298       UInt16 attrType[STUN_MAX_UNKNOWN_ATTRIBUTES];
00299       UInt16 numAttributes;
00300 } TurnAtrUnknown;
00301 
00302 typedef struct
00303 {
00304       StunMsgHdr msgHdr;
00305         
00306       bool_t hasMappedAddress;
00307       StunAtrAddress4  mappedAddress;
00308         
00309       bool_t hasResponseAddress;
00310       StunAtrAddress4  responseAddress;
00311         
00312       bool_t hasChangeRequest;
00313       StunAtrChangeRequest changeRequest;
00314         
00315       bool_t hasSourceAddress;
00316       StunAtrAddress4 sourceAddress;
00317         
00318       bool_t hasChangedAddress;
00319       StunAtrAddress4 changedAddress;
00320         
00321       bool_t hasUsername;
00322       StunAtrString username;
00323         
00324       bool_t hasPassword;
00325       StunAtrString password;
00326         
00327       bool_t hasMessageIntegrity;
00328       StunAtrIntegrity messageIntegrity;
00329         
00330       bool_t hasErrorCode;
00331       StunAtrError errorCode;
00332         
00333       bool_t hasUnknownAttributes;
00334       StunAtrUnknown unknownAttributes;
00335         
00336       bool_t hasReflectedFrom;
00337       StunAtrAddress4 reflectedFrom;
00338 
00339       bool_t hasRealm;
00340       StunAtrString realmName;
00341 
00342       bool_t hasNonce;
00343       StunAtrString nonceName;
00344 
00345       bool_t hasXorMappedAddress;
00346       StunAtrAddress4  xorMappedAddress;
00347         
00348       bool_t hasSoftware;
00349       StunAtrString softwareName;
00350 
00351       bool_t hasXorPeerAddress;
00352       StunAtrAddress4 xorPeerAddress;
00353 
00354       bool_t hasXorRelayedAddress;
00355       StunAtrAddress4 xorRelayedAddress;
00356 
00357       bool_t hasFingerprint;
00358       StunAtrFingerprint fingerprint;
00359 
00360       /* Turn elements */
00361       bool_t hasChannelNumberAttributes;
00362       TurnAtrChannelNumber channelNumberAttributes;
00363 
00364       bool_t hasLifetimeAttributes;
00365       TurnAtrLifetime lifetimeAttributes;
00366 
00367       bool_t hasData;
00368       TurnAtrData data;
00369 
00370       bool_t hasRequestedTransport;
00371       TurnAtrRequestedTransport requestedTransport;
00372 
00373       bool_t hasDontFragment;
00374 
00375       bool_t hasReservationToken;
00376       TurnAtrReservationToken reservationToken;
00377 
00378       bool_t hasPriority;
00379       IceAtrPriority priority;
00380 
00381       bool_t hasUseCandidate;
00382 
00383       bool_t hasIceControlled;
00384       IceAtrIceControll iceControlled;
00385 
00386       bool_t hasIceControlling;
00387       IceAtrIceControll iceControlling;
00388 } StunMessage; 
00389 
00390 
00391 /* Define enum with different types of NAT */
00392 typedef enum 
00393 {
00394    StunTypeUnknown=0,
00395    StunTypeOpen,
00396    StunTypeConeNat,
00397    StunTypeRestrictedNat,
00398    StunTypePortRestrictedNat,
00399    StunTypeSymNat,
00400    StunTypeSymFirewall,
00401    StunTypeBlocked,
00402    StunTypeFailure
00403 } NatType;
00404 
00405 
00406 #define MAX_MEDIA_RELAYS 500
00407 #define MAX_RTP_MSG_SIZE 1500
00408 #define MEDIA_RELAY_TIMEOUT 3*60
00409 
00410 typedef struct 
00411 {
00412       int relayPort;       /* media relay port */
00413       int fd;              /* media relay file descriptor */
00414       StunAddress4 destination; /* NAT IP:port */
00415       time_t expireTime;      /* if no activity after time, close the socket */
00416 } StunMediaRelay;
00417 
00418 typedef struct
00419 {
00420       StunAddress4 myAddr;
00421       StunAddress4 altAddr;
00422       Socket myFd;
00423       Socket altPortFd;
00424       Socket altIpFd;
00425       Socket altIpPortFd;
00426       bool_t relay; /* true if media relaying is to be done */
00427       StunMediaRelay relays[MAX_MEDIA_RELAYS];
00428 } StunServerInfo;
00429 
00430 void
00431 stunCalculateIntegrity_longterm(char* hmac, const char* input, int length,
00432                      const char *username, const char *realm, const char *password);
00433 void
00434 stunCalculateIntegrity_shortterm(char* hmac, const char* input, int length, const char* key);
00435 UInt32
00436 stunCalculateFingerprint(const char* input, int length);
00437 
00438 bool_t
00439 stunParseMessage( char* buf, 
00440                   unsigned int bufLen, 
00441                   StunMessage *message);
00442 
00443 void
00444 stunBuildReqSimple( StunMessage* msg,
00445                     const StunAtrString *username,
00446                     bool_t changePort, bool_t changeIp, unsigned int id );
00447 
00448 unsigned int
00449 stunEncodeMessage( const StunMessage *message, 
00450                    char* buf, 
00451                    unsigned int bufLen, 
00452                    const StunAtrString *password);
00453 
00454 void
00455 stunCreateUserName(const StunAddress4 *addr, StunAtrString* username);
00456 
00457 void 
00458 stunGetUserNameAndPassword(  const StunAddress4 *dest, 
00459                              StunAtrString* username,
00460                              StunAtrString* password);
00461 
00462 void
00463 stunCreatePassword(const StunAtrString *username, StunAtrString* password);
00464 
00465 int 
00466 stunRand(void);
00467 
00468 UInt64
00469 stunGetSystemTimeSecs(void);
00470 
00471 /* find the IP address of a the specified stun server - return false is fails parse  */
00472 bool_t  
00473 stunParseServerName( const char* serverName, StunAddress4 *stunServerAddr);
00474 
00475 bool_t 
00476 stunParseHostName( const char* peerName,
00477                    UInt32 *ip,
00478                    UInt16 *portVal,
00479                    UInt16 defaultPort );
00480 
00481 /* return true if all is OK 
00482    Create a media relay and do the STERN thing if startMediaPort is non-zero */
00483 bool_t
00484 stunInitServer(StunServerInfo *info, 
00485                const StunAddress4 *myAddr, 
00486                const StunAddress4 *altAddr,
00487                int startMediaPort);
00488 
00489 void
00490 stunStopServer(StunServerInfo *info);
00491 
00492 /* returns number of address found - take array or addres */
00493 int 
00494 stunFindLocalInterfaces(UInt32* addresses, int maxSize );
00495 
00496 int 
00497 stunTest( StunAddress4 *dest, int testNum, StunAddress4* srcAddr, StunAddress4 *sMappedAddr, StunAddress4* sChangedAddr);
00498 
00499 NatType
00500 stunNatType( StunAddress4 *dest, 
00501              bool_t* preservePort, /* if set, is return for if NAT preservers ports or not */
00502              bool_t* hairpin ,  /* if set, is the return for if NAT will hairpin packets */
00503              int port, /* port to use for the test, 0 to choose random port */
00504              StunAddress4* sAddr /* NIC to use */
00505    );
00506 
00507 bool_t
00508 stunServerProcessMsg( char* buf,
00509                       unsigned int bufLen,
00510                       StunAddress4 *from, 
00511                       StunAddress4 *myAddr,
00512                       StunAddress4 *altAddr, 
00513                       StunMessage *resp,
00514                       StunAddress4 *destination,
00515                       StunAtrString *hmacPassword,
00516                       bool_t* changePort,
00517                       bool_t* changeIp);
00518 
00519 int
00520 stunOpenSocket( StunAddress4 *dest, 
00521                 StunAddress4* mappedAddr, 
00522                 int port, 
00523                 StunAddress4* srcAddr);
00524 
00525 bool_t
00526 stunOpenSocketPair(StunAddress4 *dest,
00527                    StunAddress4* mapAddr_rtp, 
00528                    StunAddress4* mapAddr_rtcp, 
00529                    int* fd1, int* fd2, 
00530                    int srcPort,  StunAddress4* srcAddr);
00531 
00532 bool_t
00533 turnAllocateSocketPair(StunAddress4 *dest,
00534                    StunAddress4* mapAddr_rtp, 
00535                    StunAddress4* mapAddr_rtcp, 
00536                    int* fd1, int* fd2, 
00537                    int srcPort,  StunAddress4* srcAddr);
00538 
00539 #ifdef __cplusplus
00540 }
00541 #endif
00542 
00543 #endif
00544 

Generated on Sat Nov 27 2010 for oRTP by  doxygen 1.7.2