| 1 | /* |
|---|
| 2 | The oRTP library is an RTP (Realtime Transport Protocol - rfc3550) stack. |
|---|
| 3 | Copyright (C) 2001 Simon MORLAT simon.morlat@linphone.org |
|---|
| 4 | |
|---|
| 5 | This library is free software; you can redistribute it and/or |
|---|
| 6 | modify it under the terms of the GNU Lesser General Public |
|---|
| 7 | License as published by the Free Software Foundation; either |
|---|
| 8 | version 2.1 of the License, or (at your option) any later version. |
|---|
| 9 | |
|---|
| 10 | This library is distributed in the hope that it will be useful, |
|---|
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|---|
| 13 | Lesser General Public License for more details. |
|---|
| 14 | |
|---|
| 15 | You should have received a copy of the GNU Lesser General Public |
|---|
| 16 | License along with this library; if not, write to the Free Software |
|---|
| 17 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|---|
| 18 | */ |
|---|
| 19 | |
|---|
| 20 | /* ==================================================================== |
|---|
| 21 | * The Vovida Software License, Version 1.0 |
|---|
| 22 | * |
|---|
| 23 | * Copyright (c) 2000 Vovida Networks, Inc. All rights reserved. |
|---|
| 24 | * |
|---|
| 25 | * Redistribution and use in source and binary forms, with or without |
|---|
| 26 | * modification, are permitted provided that the following conditions |
|---|
| 27 | * are met: |
|---|
| 28 | * |
|---|
| 29 | * 1. Redistributions of source code must retain the above copyright |
|---|
| 30 | * notice, this list of conditions and the following disclaimer. |
|---|
| 31 | * |
|---|
| 32 | * 2. Redistributions in binary form must reproduce the above copyright |
|---|
| 33 | * notice, this list of conditions and the following disclaimer in |
|---|
| 34 | * the documentation and/or other materials provided with the |
|---|
| 35 | * distribution. |
|---|
| 36 | * |
|---|
| 37 | * 3. The names "VOCAL", "Vovida Open Communication Application Library", |
|---|
| 38 | * and "Vovida Open Communication Application Library (VOCAL)" must |
|---|
| 39 | * not be used to endorse or promote products derived from this |
|---|
| 40 | * software without prior written permission. For written |
|---|
| 41 | * permission, please contact vocal@vovida.org. |
|---|
| 42 | * |
|---|
| 43 | * 4. Products derived from this software may not be called "VOCAL", nor |
|---|
| 44 | * may "VOCAL" appear in their name, without prior written |
|---|
| 45 | * permission of Vovida Networks, Inc. |
|---|
| 46 | * |
|---|
| 47 | * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED |
|---|
| 48 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
|---|
| 49 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND |
|---|
| 50 | * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL VOVIDA |
|---|
| 51 | * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES |
|---|
| 52 | * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL, |
|---|
| 53 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
|---|
| 54 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
|---|
| 55 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
|---|
| 56 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|---|
| 57 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE |
|---|
| 58 | * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH |
|---|
| 59 | * DAMAGE. |
|---|
| 60 | * |
|---|
| 61 | * ==================================================================== |
|---|
| 62 | * |
|---|
| 63 | * This software consists of voluntary contributions made by Vovida |
|---|
| 64 | * Networks, Inc. and many individuals on behalf of Vovida Networks, |
|---|
| 65 | * Inc. For more information on Vovida Networks, Inc., please see |
|---|
| 66 | * <http://www.vovida.org/>. |
|---|
| 67 | * |
|---|
| 68 | */ |
|---|
| 69 | |
|---|
| 70 | |
|---|
| 71 | #ifndef __STUN_H__ |
|---|
| 72 | #define __STUN_H__ |
|---|
| 73 | |
|---|
| 74 | #include <stdio.h> |
|---|
| 75 | #include <time.h> |
|---|
| 76 | #include <ortp/port.h> |
|---|
| 77 | #include <ortp/stun_udp.h> |
|---|
| 78 | |
|---|
| 79 | |
|---|
| 80 | #ifdef __cplusplus |
|---|
| 81 | extern "C" |
|---|
| 82 | { |
|---|
| 83 | #endif |
|---|
| 84 | |
|---|
| 85 | /* if you change this version, change in makefile too */ |
|---|
| 86 | #define STUN_VERSION "0.94" |
|---|
| 87 | |
|---|
| 88 | #define STUN_MAX_STRING 256 |
|---|
| 89 | #define STUN_MAX_UNKNOWN_ATTRIBUTES 8 |
|---|
| 90 | #define STUN_MAX_MESSAGE_SIZE 2048 |
|---|
| 91 | |
|---|
| 92 | #define STUN_PORT 3478 |
|---|
| 93 | |
|---|
| 94 | /* define some basic types */ |
|---|
| 95 | typedef unsigned char UInt8; |
|---|
| 96 | typedef unsigned short UInt16; |
|---|
| 97 | typedef unsigned int UInt32; |
|---|
| 98 | #if defined(WIN32) || defined(_WIN32_WCE) |
|---|
| 99 | typedef unsigned __int64 UInt64; |
|---|
| 100 | #else |
|---|
| 101 | typedef unsigned long long UInt64; |
|---|
| 102 | #endif |
|---|
| 103 | typedef struct { unsigned char octet[16]; } UInt128; |
|---|
| 104 | |
|---|
| 105 | /* define a structure to hold a stun address */ |
|---|
| 106 | #define IPv4Family 0x01 |
|---|
| 107 | #define IPv6Family 0x02 |
|---|
| 108 | |
|---|
| 109 | /* define flags */ |
|---|
| 110 | #define ChangeIpFlag 0x04 |
|---|
| 111 | #define ChangePortFlag 0x02 |
|---|
| 112 | |
|---|
| 113 | /* define stun attribute */ |
|---|
| 114 | #define MappedAddress 0x0001 |
|---|
| 115 | #define ResponseAddress 0x0002 |
|---|
| 116 | #define ChangeRequest 0x0003 |
|---|
| 117 | #define SourceAddress 0x0004 |
|---|
| 118 | #define ChangedAddress 0x0005 |
|---|
| 119 | #define STUNUsername 0x0006 /* Username is too common: rename to avoid conflict */ |
|---|
| 120 | #define STUNPassword 0x0007 /* Password is too common: rename to avoid conflict */ |
|---|
| 121 | #define MessageIntegrity 0x0008 |
|---|
| 122 | #define ErrorCode 0x0009 |
|---|
| 123 | #define UnknownAttribute 0x000A |
|---|
| 124 | #define ReflectedFrom 0x000B |
|---|
| 125 | #define XorMappedAddress 0x0020 |
|---|
| 126 | #define XorOnly 0x0021 |
|---|
| 127 | #define ServerName 0x0022 |
|---|
| 128 | #define SecondaryAddress 0x0050 /* Non standard extention */ |
|---|
| 129 | |
|---|
| 130 | /* define types for a stun message */ |
|---|
| 131 | #define BindRequestMsg 0x0001 |
|---|
| 132 | #define BindResponseMsg 0x0101 |
|---|
| 133 | #define BindErrorResponseMsg 0x0111 |
|---|
| 134 | #define SharedSecretRequestMsg 0x0002 |
|---|
| 135 | #define SharedSecretResponseMsg 0x0102 |
|---|
| 136 | #define SharedSecretErrorResponseMsg 0x0112 |
|---|
| 137 | |
|---|
| 138 | typedef struct |
|---|
| 139 | { |
|---|
| 140 | UInt16 msgType; |
|---|
| 141 | UInt16 msgLength; |
|---|
| 142 | UInt128 id; |
|---|
| 143 | } StunMsgHdr; |
|---|
| 144 | |
|---|
| 145 | |
|---|
| 146 | typedef struct |
|---|
| 147 | { |
|---|
| 148 | UInt16 type; |
|---|
| 149 | UInt16 length; |
|---|
| 150 | } StunAtrHdr; |
|---|
| 151 | |
|---|
| 152 | typedef struct |
|---|
| 153 | { |
|---|
| 154 | UInt16 port; |
|---|
| 155 | UInt32 addr; |
|---|
| 156 | } StunAddress4; |
|---|
| 157 | |
|---|
| 158 | typedef struct |
|---|
| 159 | { |
|---|
| 160 | UInt8 pad; |
|---|
| 161 | UInt8 family; |
|---|
| 162 | StunAddress4 ipv4; |
|---|
| 163 | } StunAtrAddress4; |
|---|
| 164 | |
|---|
| 165 | typedef struct |
|---|
| 166 | { |
|---|
| 167 | UInt32 value; |
|---|
| 168 | } StunAtrChangeRequest; |
|---|
| 169 | |
|---|
| 170 | typedef struct |
|---|
| 171 | { |
|---|
| 172 | UInt16 pad; /* all 0 */ |
|---|
| 173 | UInt8 errorClass; |
|---|
| 174 | UInt8 number; |
|---|
| 175 | char reason[STUN_MAX_STRING]; |
|---|
| 176 | UInt16 sizeReason; |
|---|
| 177 | } StunAtrError; |
|---|
| 178 | |
|---|
| 179 | typedef struct |
|---|
| 180 | { |
|---|
| 181 | UInt16 attrType[STUN_MAX_UNKNOWN_ATTRIBUTES]; |
|---|
| 182 | UInt16 numAttributes; |
|---|
| 183 | } StunAtrUnknown; |
|---|
| 184 | |
|---|
| 185 | typedef struct |
|---|
| 186 | { |
|---|
| 187 | char value[STUN_MAX_STRING]; |
|---|
| 188 | UInt16 sizeValue; |
|---|
| 189 | } StunAtrString; |
|---|
| 190 | |
|---|
| 191 | typedef struct |
|---|
| 192 | { |
|---|
| 193 | char hash[20]; |
|---|
| 194 | } StunAtrIntegrity; |
|---|
| 195 | |
|---|
| 196 | typedef enum |
|---|
| 197 | { |
|---|
| 198 | HmacUnkown=0, |
|---|
| 199 | HmacOK, |
|---|
| 200 | HmacBadUserName, |
|---|
| 201 | HmacUnkownUserName, |
|---|
| 202 | HmacFailed |
|---|
| 203 | } StunHmacStatus; |
|---|
| 204 | |
|---|
| 205 | typedef struct |
|---|
| 206 | { |
|---|
| 207 | StunMsgHdr msgHdr; |
|---|
| 208 | |
|---|
| 209 | bool_t hasMappedAddress; |
|---|
| 210 | StunAtrAddress4 mappedAddress; |
|---|
| 211 | |
|---|
| 212 | bool_t hasResponseAddress; |
|---|
| 213 | StunAtrAddress4 responseAddress; |
|---|
| 214 | |
|---|
| 215 | bool_t hasChangeRequest; |
|---|
| 216 | StunAtrChangeRequest changeRequest; |
|---|
| 217 | |
|---|
| 218 | bool_t hasSourceAddress; |
|---|
| 219 | StunAtrAddress4 sourceAddress; |
|---|
| 220 | |
|---|
| 221 | bool_t hasChangedAddress; |
|---|
| 222 | StunAtrAddress4 changedAddress; |
|---|
| 223 | |
|---|
| 224 | bool_t hasUsername; |
|---|
| 225 | StunAtrString username; |
|---|
| 226 | |
|---|
| 227 | bool_t hasPassword; |
|---|
| 228 | StunAtrString password; |
|---|
| 229 | |
|---|
| 230 | bool_t hasMessageIntegrity; |
|---|
| 231 | StunAtrIntegrity messageIntegrity; |
|---|
| 232 | |
|---|
| 233 | bool_t hasErrorCode; |
|---|
| 234 | StunAtrError errorCode; |
|---|
| 235 | |
|---|
| 236 | bool_t hasUnknownAttributes; |
|---|
| 237 | StunAtrUnknown unknownAttributes; |
|---|
| 238 | |
|---|
| 239 | bool_t hasReflectedFrom; |
|---|
| 240 | StunAtrAddress4 reflectedFrom; |
|---|
| 241 | |
|---|
| 242 | bool_t hasXorMappedAddress; |
|---|
| 243 | StunAtrAddress4 xorMappedAddress; |
|---|
| 244 | |
|---|
| 245 | bool_t xorOnly; |
|---|
| 246 | |
|---|
| 247 | bool_t hasServerName; |
|---|
| 248 | StunAtrString serverName; |
|---|
| 249 | |
|---|
| 250 | bool_t hasSecondaryAddress; |
|---|
| 251 | StunAtrAddress4 secondaryAddress; |
|---|
| 252 | } StunMessage; |
|---|
| 253 | |
|---|
| 254 | |
|---|
| 255 | /* Define enum with different types of NAT */ |
|---|
| 256 | typedef enum |
|---|
| 257 | { |
|---|
| 258 | StunTypeUnknown=0, |
|---|
| 259 | StunTypeOpen, |
|---|
| 260 | StunTypeConeNat, |
|---|
| 261 | StunTypeRestrictedNat, |
|---|
| 262 | StunTypePortRestrictedNat, |
|---|
| 263 | StunTypeSymNat, |
|---|
| 264 | StunTypeSymFirewall, |
|---|
| 265 | StunTypeBlocked, |
|---|
| 266 | StunTypeFailure |
|---|
| 267 | } NatType; |
|---|
| 268 | |
|---|
| 269 | |
|---|
| 270 | #define MAX_MEDIA_RELAYS 500 |
|---|
| 271 | #define MAX_RTP_MSG_SIZE 1500 |
|---|
| 272 | #define MEDIA_RELAY_TIMEOUT 3*60 |
|---|
| 273 | |
|---|
| 274 | typedef struct |
|---|
| 275 | { |
|---|
| 276 | int relayPort; /* media relay port */ |
|---|
| 277 | int fd; /* media relay file descriptor */ |
|---|
| 278 | StunAddress4 destination; /* NAT IP:port */ |
|---|
| 279 | time_t expireTime; /* if no activity after time, close the socket */ |
|---|
| 280 | } StunMediaRelay; |
|---|
| 281 | |
|---|
| 282 | typedef struct |
|---|
| 283 | { |
|---|
| 284 | StunAddress4 myAddr; |
|---|
| 285 | StunAddress4 altAddr; |
|---|
| 286 | Socket myFd; |
|---|
| 287 | Socket altPortFd; |
|---|
| 288 | Socket altIpFd; |
|---|
| 289 | Socket altIpPortFd; |
|---|
| 290 | bool_t relay; /* true if media relaying is to be done */ |
|---|
| 291 | StunMediaRelay relays[MAX_MEDIA_RELAYS]; |
|---|
| 292 | } StunServerInfo; |
|---|
| 293 | |
|---|
| 294 | bool_t |
|---|
| 295 | stunParseMessage( char* buf, |
|---|
| 296 | unsigned int bufLen, |
|---|
| 297 | StunMessage *message, |
|---|
| 298 | bool_t verbose ); |
|---|
| 299 | |
|---|
| 300 | void |
|---|
| 301 | stunBuildReqSimple( StunMessage* msg, |
|---|
| 302 | const StunAtrString *username, |
|---|
| 303 | bool_t changePort, bool_t changeIp, unsigned int id ); |
|---|
| 304 | |
|---|
| 305 | unsigned int |
|---|
| 306 | stunEncodeMessage( const StunMessage *message, |
|---|
| 307 | char* buf, |
|---|
| 308 | unsigned int bufLen, |
|---|
| 309 | const StunAtrString *password, |
|---|
| 310 | bool_t verbose); |
|---|
| 311 | |
|---|
| 312 | void |
|---|
| 313 | stunCreateUserName(const StunAddress4 *addr, StunAtrString* username); |
|---|
| 314 | |
|---|
| 315 | void |
|---|
| 316 | stunGetUserNameAndPassword( const StunAddress4 *dest, |
|---|
| 317 | StunAtrString* username, |
|---|
| 318 | StunAtrString* password); |
|---|
| 319 | |
|---|
| 320 | void |
|---|
| 321 | stunCreatePassword(const StunAtrString *username, StunAtrString* password); |
|---|
| 322 | |
|---|
| 323 | int |
|---|
| 324 | stunRand(void); |
|---|
| 325 | |
|---|
| 326 | UInt64 |
|---|
| 327 | stunGetSystemTimeSecs(void); |
|---|
| 328 | |
|---|
| 329 | /* find the IP address of a the specified stun server - return false is fails parse */ |
|---|
| 330 | bool_t |
|---|
| 331 | stunParseServerName( char* serverName, StunAddress4 *stunServerAddr); |
|---|
| 332 | |
|---|
| 333 | bool_t |
|---|
| 334 | stunParseHostName( char* peerName, |
|---|
| 335 | UInt32 *ip, |
|---|
| 336 | UInt16 *portVal, |
|---|
| 337 | UInt16 defaultPort ); |
|---|
| 338 | |
|---|
| 339 | /* return true if all is OK |
|---|
| 340 | Create a media relay and do the STERN thing if startMediaPort is non-zero */ |
|---|
| 341 | bool_t |
|---|
| 342 | stunInitServer(StunServerInfo *info, |
|---|
| 343 | const StunAddress4 *myAddr, |
|---|
| 344 | const StunAddress4 *altAddr, |
|---|
| 345 | int startMediaPort, |
|---|
| 346 | bool_t verbose); |
|---|
| 347 | |
|---|
| 348 | void |
|---|
| 349 | stunStopServer(StunServerInfo *info); |
|---|
| 350 | |
|---|
| 351 | #if 0 /* no usefull here */ |
|---|
| 352 | /* return true if all is OK */ |
|---|
| 353 | bool_t |
|---|
| 354 | stunServerProcess(StunServerInfo *info, bool_t verbose); |
|---|
| 355 | #endif |
|---|
| 356 | |
|---|
| 357 | /* returns number of address found - take array or addres */ |
|---|
| 358 | int |
|---|
| 359 | stunFindLocalInterfaces(UInt32* addresses, int maxSize ); |
|---|
| 360 | |
|---|
| 361 | int |
|---|
| 362 | stunTest( StunAddress4 *dest, int testNum, bool_t verbose, StunAddress4* srcAddr, StunAddress4 *sMappedAddr, StunAddress4* sChangedAddr); |
|---|
| 363 | |
|---|
| 364 | NatType |
|---|
| 365 | stunNatType( StunAddress4 *dest, bool_t verbose, |
|---|
| 366 | bool_t* preservePort, /* if set, is return for if NAT preservers ports or not */ |
|---|
| 367 | bool_t* hairpin , /* if set, is the return for if NAT will hairpin packets */ |
|---|
| 368 | int port, /* port to use for the test, 0 to choose random port */ |
|---|
| 369 | StunAddress4* sAddr /* NIC to use */ |
|---|
| 370 | ); |
|---|
| 371 | |
|---|
| 372 | bool_t |
|---|
| 373 | stunServerProcessMsg( char* buf, |
|---|
| 374 | unsigned int bufLen, |
|---|
| 375 | StunAddress4 *from, |
|---|
| 376 | StunAddress4 *secondary, |
|---|
| 377 | StunAddress4 *myAddr, |
|---|
| 378 | StunAddress4 *altAddr, |
|---|
| 379 | StunMessage *resp, |
|---|
| 380 | StunAddress4 *destination, |
|---|
| 381 | StunAtrString *hmacPassword, |
|---|
| 382 | bool_t* changePort, |
|---|
| 383 | bool_t* changeIp, |
|---|
| 384 | bool_t verbose); |
|---|
| 385 | |
|---|
| 386 | int |
|---|
| 387 | stunOpenSocket( StunAddress4 *dest, |
|---|
| 388 | StunAddress4* mappedAddr, |
|---|
| 389 | int port, |
|---|
| 390 | StunAddress4* srcAddr, |
|---|
| 391 | bool_t verbose ); |
|---|
| 392 | |
|---|
| 393 | bool_t |
|---|
| 394 | stunOpenSocketPair(StunAddress4 *dest, |
|---|
| 395 | StunAddress4* mapAddr_rtp, |
|---|
| 396 | StunAddress4* mapAddr_rtcp, |
|---|
| 397 | int* fd1, int* fd2, |
|---|
| 398 | int srcPort, StunAddress4* srcAddr, |
|---|
| 399 | bool_t verbose); |
|---|
| 400 | |
|---|
| 401 | #ifdef __cplusplus |
|---|
| 402 | } |
|---|
| 403 | #endif |
|---|
| 404 | |
|---|
| 405 | #endif |
|---|
| 406 | |
|---|