oRTP 0.22.0

- Main Page
- Data Structures
- Files

- File List
- Globals

stun.h
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 #ifdef __APPLE__
80 #include "TargetConditionals.h"
81 #endif
82
83 #ifdef __cplusplus
84 extern "C"
85 {
86 #endif
87
88 /* if you change this version, change in makefile too */
89 #define STUN_VERSION "0.99"
90
91 #define STUN_MAX_STRING 514
92 #define STUN_MAX_UNKNOWN_ATTRIBUTES 8
93 #define STUN_MAX_MESSAGE_SIZE 2048
94
95 #define STUN_PORT 3478
96
97 /* define some basic types */
98 #if 0
99 typedef unsigned char uint8_t;
100 typedef unsigned short uint16_t;
101 typedef unsigned int uint32_t;
102
103 #if defined(WIN32) || defined(_WIN32_WCE)
104 typedef unsigned __int64 uint64_t;
105 #else
106 typedef unsigned long long uint64_t;
107 #endif
108 #endif
109 typedef struct { unsigned char octet[12]; } UInt96;
110
111 /* define a structure to hold a stun address */
112 #define IPv4Family 0x01
113 #define IPv6Family 0x02
114
115 /* define flags */
116 #define ChangeIpFlag 0x04
117 #define ChangePortFlag 0x02
118
119 /* define stun attribute */
120 #define SA_MAPPEDADDRESS 0x0001
121 #define SA_RESPONSEADDRESS 0x0002
122 #define SA_CHANGEREQUEST 0x0003
123 #define SA_SOURCEADDRESS 0x0004
124 #define SA_CHANGEDADDRESS 0x0005
125 #define SA_USERNAME 0x0006
126 #define SA_PASSWORD 0x0007
127 #define SA_MESSAGEINTEGRITY 0x0008
128 #define SA_ERRORCODE 0x0009
129 #define SA_UNKNOWNATTRIBUTE 0x000A
130 #define SA_REFLECTEDFROM 0x000B
131 #define SA_REALM 0x0014
132 #define SA_NONCE 0x0015
133 #define SA_XORMAPPEDADDRESS 0x0020
134
135 #define SA_XORMAPPEDADDRESS2 0x8020 /* Non standard extention */
136 #define SA_XORONLY 0x0021 /* deprecated */
137 #define SA_SECONDARYADDRESS 0x0050 /* Non standard extention */
138
139 #define SA_SOFTWARE 0x8022
140 #define SA_ALTERNATESERVER 0x8023
141 #define SA_FINGERPRINT 0x8028
142
143 /* define turn attribute */
144 #define TA_CHANNELNUMBER 0x000C
145 #define TA_LIFETIME 0x000D
146 #define TA_DEPRECATEDBANDWIDTH 0x0010
147 #define TA_XORPEERADDRESS 0x0012
148 #define TA_DATA 0x0013
149 #define TA_XORRELAYEDADDRESS 0x0016
150 #define TA_EVENPORT 0x0018
151 #define TA_REQUESTEDTRANSPORT 0x0019
152 #define TA_DONTFRAGMENT 0x001A
153 #define TA_DEPRECATEDTIMERVAL 0x0021
154 #define TA_RESERVATIONTOKEN 0x0022
155
156 #define ICEA_PRIORITY 0x0024
157 #define ICEA_USECANDIDATE 0x0025
158 #define ICEA_ICECONTROLLED 0x8029
159 #define ICEA_ICECONTROLLING 0x802a
160
161 #define STUN_REQUEST 0x0000
162 #define STUN_INDICATION 0x0010
163 #define STUN_SUCCESS_RESP 0x0100
164 #define STUN_ERR_RESP 0x0110
165
166 #define STUN_IS_REQUEST(msg_type) (((msg_type) & 0x0110) == 0x0000)
167 #define STUN_IS_INDICATION(msg_type) (((msg_type) & 0x0110) == 0x0010)
168 #define STUN_IS_SUCCESS_RESP(msg_type) (((msg_type) & 0x0110) == 0x0100)
169 #define STUN_IS_ERR_RESP(msg_type) (((msg_type) & 0x0110) == 0x0110)
170
171 /* define types for a stun message */
172 #define STUN_METHOD_BINDING 0x0001
173 #define TURN_MEDHOD_ALLOCATE 0x0003 //(only request/response semantics defined)
174 #define TURN_METHOD_REFRESH 0x0004 //(only request/response semantics defined)
175 #define TURN_METHOD_CREATEPERMISSION 0x0008 //(only request/response semantics defined
176 #define TURN_METHOD_CHANNELBIND 0x0009 //(only request/response semantics defined)
177
178 //#define BindResponseMsg 0x0101
179 //#define BindErrorResponseMsg 0x0111
180 #define SharedSecretRequestMsg 0x0002
181 #define SharedSecretResponseMsg 0x0102
182 #define SharedSecretErrorResponseMsg 0x0112
183
184 #define TURN_INDICATION_SEND 0x0006 //(only indication semantics defined)
185 #define TURN_INDICATION_DATA 0x0007 //(only indication semantics defined)
186
187 typedef struct
188 {
189 uint16_t msgType;
190 uint16_t msgLength;
191 uint32_t magic_cookie;
192 UInt96 tr_id;
193 } StunMsgHdr;
194
195
196 typedef struct
197 {
198 uint16_t type;
199 uint16_t length;
200 } StunAtrHdr;
201
202 typedef struct
203 {
204 uint16_t port;
205 uint32_t addr;
206 } StunAddress4;
207
208 typedef struct
209 {
210 uint8_t pad;
211 uint8_t family;
212 StunAddress4 ipv4;
213 } StunAtrAddress4;
214
215 typedef struct
216 {
217 uint32_t value;
218 } StunAtrChangeRequest;
219
220 typedef struct
221 {
222 uint16_t pad; /* all 0 */
223 uint8_t errorClass;
224 uint8_t number;
225 char reason[STUN_MAX_STRING];
226 uint16_t sizeReason;
227 } StunAtrError;
228
229 typedef struct
230 {
231 uint16_t attrType[STUN_MAX_UNKNOWN_ATTRIBUTES];
232 uint16_t numAttributes;
233 } StunAtrUnknown;
234
235 typedef struct
236 {
237 uint16_t channelNumber;
238 uint16_t rffu; /* Reserved For Future Use */
239 } TurnAtrChannelNumber;
240
241 typedef struct
242 {
243 uint32_t lifetime;
244 } TurnAtrLifetime;
245
246 typedef struct
247 {
248 char value[1500];
249 uint16_t sizeValue;
250 } TurnAtrData;
251
252 typedef struct
253 {
254 uint8_t proto;
255 uint8_t pad1;
256 uint8_t pad2;
257 uint8_t pad3;
258 } TurnAtrRequestedTransport;
259
260 typedef struct
261 {
262 uint64_t value;
263 } TurnAtrReservationToken;
264
265 typedef struct
266 {
267 uint32_t fingerprint;
268 } StunAtrFingerprint;
269
270
271 typedef struct
272 {
273 char value[STUN_MAX_STRING];
274 uint16_t sizeValue;
275 } StunAtrString;
276
277 typedef struct
278 {
279 uint32_t priority;
280 } IceAtrPriority;
281
282 typedef struct
283 {
284 uint64_t value;
285 } IceAtrIceControll;
286
287 typedef struct
288 {
289 char hash[20];
290 } StunAtrIntegrity;
291
292 typedef enum
293 {
294 HmacUnkown=0,
295 HmacOK,
296 HmacBadUserName,
297 HmacUnkownUserName,
298 HmacFailed
299 } StunHmacStatus;
300
301
302 typedef struct
303 {
304 uint16_t attrType[STUN_MAX_UNKNOWN_ATTRIBUTES];
305 uint16_t numAttributes;
306 } TurnAtrUnknown;
307
308 typedef struct
309 {
310 StunMsgHdr msgHdr;
311
312 bool_t hasMappedAddress;
313 StunAtrAddress4 mappedAddress;
314
315 bool_t hasResponseAddress;
316 StunAtrAddress4 responseAddress;
317
318 bool_t hasChangeRequest;
319 StunAtrChangeRequest changeRequest;
320
321 bool_t hasSourceAddress;
322 StunAtrAddress4 sourceAddress;
323
324 bool_t hasChangedAddress;
325 StunAtrAddress4 changedAddress;
326
327 bool_t hasUsername;
328 StunAtrString username;
329
330 bool_t hasPassword;
331 StunAtrString password;
332
333 bool_t hasMessageIntegrity;
334 StunAtrIntegrity messageIntegrity;
335
336 bool_t hasErrorCode;
337 StunAtrError errorCode;
338
339 bool_t hasUnknownAttributes;
340 StunAtrUnknown unknownAttributes;
341
342 bool_t hasReflectedFrom;
343 StunAtrAddress4 reflectedFrom;
344
345 bool_t hasRealm;
346 StunAtrString realmName;
347
348 bool_t hasNonce;
349 StunAtrString nonceName;
350
351 bool_t hasXorMappedAddress;
352 StunAtrAddress4 xorMappedAddress;
353
354 bool_t hasSoftware;
355 StunAtrString softwareName;
356
357 bool_t hasXorPeerAddress;
358 StunAtrAddress4 xorPeerAddress;
359
360 bool_t hasXorRelayedAddress;
361 StunAtrAddress4 xorRelayedAddress;
362
363 bool_t hasFingerprint;
364 StunAtrFingerprint fingerprint;
365
366 /* Turn elements */
367 bool_t hasChannelNumberAttributes;
368 TurnAtrChannelNumber channelNumberAttributes;
369
370 bool_t hasLifetimeAttributes;
371 TurnAtrLifetime lifetimeAttributes;
372
373 bool_t hasData;
374 TurnAtrData data;
375
376 bool_t hasRequestedTransport;
377 TurnAtrRequestedTransport requestedTransport;
378
379 bool_t hasDontFragment;
380
381 bool_t hasReservationToken;
382 TurnAtrReservationToken reservationToken;
383
384 bool_t hasPriority;
385 IceAtrPriority priority;
386
387 bool_t hasUseCandidate;
388
389 bool_t hasIceControlled;
390 IceAtrIceControll iceControlled;
391
392 bool_t hasIceControlling;
393 IceAtrIceControll iceControlling;
394 } StunMessage;
395
396
397 /* Define enum with different types of NAT */
398 typedef enum
399 {
400 StunTypeUnknown=0,
401 StunTypeOpen,
402 StunTypeConeNat,
403 StunTypeRestrictedNat,
404 StunTypePortRestrictedNat,
405 StunTypeSymNat,
406 StunTypeSymFirewall,
407 StunTypeBlocked,
408 StunTypeFailure
409 } NatType;
410
411
412 #define MAX_MEDIA_RELAYS 500
413 #define MAX_RTP_MSG_SIZE 1500
414 #define MEDIA_RELAY_TIMEOUT 3*60
415
416 typedef struct
417 {
418 int relayPort; /* media relay port */
419 int fd; /* media relay file descriptor */
420 StunAddress4 destination; /* NAT IP:port */
421 time_t expireTime; /* if no activity after time, close the socket */
422 } StunMediaRelay;
423
424 typedef struct
425 {
426 StunAddress4 myAddr;
427 StunAddress4 altAddr;
428 Socket myFd;
429 Socket altPortFd;
430 Socket altIpFd;
431 Socket altIpPortFd;
432 bool_t relay; /* true if media relaying is to be done */
433 StunMediaRelay relays[MAX_MEDIA_RELAYS];
434 } StunServerInfo;
435
436 ORTP_PUBLIC void
437 stunCalculateIntegrity_longterm(char* hmac, const char* input, int length,
438 const char *username, const char *realm, const char *password);
439 ORTP_PUBLIC void
440 stunCalculateIntegrity_shortterm(char* hmac, const char* input, int length, const char* key);
441 ORTP_PUBLIC uint32_t
442 stunCalculateFingerprint(const char* input, int length);
443
444 ORTP_PUBLIC bool_t
445 stunParseMessage( char* buf,
446 unsigned int bufLen,
447 StunMessage *message);
448
449 ORTP_PUBLIC void
450 stunBuildReqSimple( StunMessage* msg,
451 const StunAtrString *username,
452 bool_t changePort, bool_t changeIp, unsigned int id );
453
454 ORTP_PUBLIC unsigned int
455 stunEncodeMessage( const StunMessage *message,
456 char* buf,
457 unsigned int bufLen,
458 const StunAtrString *password);
459
460 ORTP_PUBLIC void
461 stunCreateUserName(const StunAddress4 *addr, StunAtrString* username);
462
463 ORTP_PUBLIC void
464 stunGetUserNameAndPassword( const StunAddress4 *dest,
465 StunAtrString* username,
466 StunAtrString* password);
467
468 ORTP_PUBLIC void
469 stunCreatePassword(const StunAtrString *username, StunAtrString* password);
470
471 ORTP_PUBLIC int
472 stunRand(void);
473
474 ORTP_PUBLIC uint64_t
475 stunGetSystemTimeSecs(void);
476
477 /* find the IP address of a the specified stun server - return false is fails parse */
478 ORTP_PUBLIC bool_t
479 stunParseServerName( const char* serverName, StunAddress4 *stunServerAddr);
480
481 ORTP_PUBLIC bool_t
482 stunParseHostName( const char* peerName,
483 uint32_t *ip,
484 uint16_t *portVal,
485 uint16_t defaultPort );
486
487 /* return true if all is OK
488 Create a media relay and do the STERN thing if startMediaPort is non-zero */
489 ORTP_PUBLIC bool_t
490 stunInitServer(StunServerInfo *info,
491 const StunAddress4 *myAddr,
492 const StunAddress4 *altAddr,
493 int startMediaPort);
494
495 ORTP_PUBLIC void
496 stunStopServer(StunServerInfo *info);
497
498 /* returns number of address found - take array or addres */
499 ORTP_PUBLIC int
500 stunFindLocalInterfaces(uint32_t* addresses, int maxSize );
501
502 ORTP_PUBLIC int
503 stunTest( StunAddress4 *dest, int testNum, StunAddress4* srcAddr, StunAddress4 *sMappedAddr, StunAddress4* sChangedAddr);
504
505 ORTP_PUBLIC NatType
506 stunNatType( StunAddress4 *dest,
507 bool_t* preservePort, /* if set, is return for if NAT preservers ports or not */
508 bool_t* hairpin , /* if set, is the return for if NAT will hairpin packets */
509 int port, /* port to use for the test, 0 to choose random port */
510 StunAddress4* sAddr /* NIC to use */
511 );
512
513 ORTP_PUBLIC bool_t
514 stunServerProcessMsg( char* buf,
515 unsigned int bufLen,
516 StunAddress4 *from,
517 StunAddress4 *myAddr,
518 StunAddress4 *altAddr,
519 StunMessage *resp,
520 StunAddress4 *destination,
521 StunAtrString *hmacPassword,
522 bool_t* changePort,
523 bool_t* changeIp);
524
525 ORTP_PUBLIC int
526 stunOpenSocket( StunAddress4 *dest,
527 StunAddress4* mappedAddr,
528 int port,
529 StunAddress4* srcAddr);
530
531 ORTP_PUBLIC bool_t
532 stunOpenSocketPair(StunAddress4 *dest,
533 StunAddress4* mapAddr_rtp,
534 StunAddress4* mapAddr_rtcp,
535 int* fd1, int* fd2,
536 int srcPort, StunAddress4* srcAddr);
537
538 ORTP_PUBLIC bool_t
539 turnAllocateSocketPair(StunAddress4 *dest,
540 StunAddress4* mapAddr_rtp,
541 StunAddress4* mapAddr_rtcp,
542 int* fd1, int* fd2,
543 int srcPort, StunAddress4* srcAddr);
544
545 #ifdef __cplusplus
546 }
547 #endif
548
549 #endif
550