a-oRTP  5.2.0
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 #ifndef __STUN_H__
71 #define __STUN_H__
72 
73 #include <stdio.h>
74 #include <time.h>
75 #include <ortp/port.h>
76 #include <ortp/stun_udp.h>
77 
78 #ifdef __APPLE__
79 #include "TargetConditionals.h"
80 #endif
81 
82 #ifdef __cplusplus
83 extern "C" {
84 #endif
85 
86 /* if you change this version, change in makefile too */
87 #define STUN_VERSION "5.2.0"
88 
89 /* from https://tools.ietf.org/html/rfc5389
90  REALM, SERVER, reason phrases, and NONCE limited to 127
91  characters. USERNAME to 513 bytes.
92 
93  127 char in UTF8 is 763 bytes
94  */
95 #define STUN_MAX_STRING 763
96 #define STUN_MAX_UNKNOWN_ATTRIBUTES 8
97 #define STUN_MAX_MESSAGE_SIZE 2048
98 
99 #define STUN_PORT 3478
100 
101 /* define some basic types */
102 #if 0
103 typedef unsigned char uint8_t;
104 typedef unsigned short uint16_t;
105 typedef unsigned int uint32_t;
106 
107 #if defined(WIN32) || defined(_WIN32_WCE)
108 typedef unsigned __int64 uint64_t;
109 #else
110 typedef unsigned long long uint64_t;
111 #endif
112 #endif
113 
114 /* define a structure to hold a stun address */
115 #define IPv4Family 0x01
116 #define IPv6Family 0x02
117 
118 /* define flags */
119 #define ChangeIpFlag 0x04
120 #define ChangePortFlag 0x02
121 
122 #define NoReservation 0x00
123 #define ReserveNextPort 0x80
124 
125 /* define stun attribute */
126 #define SA_MAPPEDADDRESS 0x0001
127 #define SA_RESPONSEADDRESS 0x0002
128 #define SA_CHANGEREQUEST 0x0003
129 #define SA_SOURCEADDRESS 0x0004
130 #define SA_CHANGEDADDRESS 0x0005
131 #define SA_USERNAME 0x0006
132 #define SA_PASSWORD 0x0007
133 #define SA_MESSAGEINTEGRITY 0x0008
134 #define SA_ERRORCODE 0x0009
135 #define SA_UNKNOWNATTRIBUTE 0x000A
136 #define SA_REFLECTEDFROM 0x000B
137 #define SA_REALM 0x0014
138 #define SA_NONCE 0x0015
139 #define SA_XORMAPPEDADDRESS 0x0020
140 
141 #define SA_XORMAPPEDADDRESS2 0x8020 /* Non standard extention */
142 #define SA_XORONLY 0x0021 /* deprecated */
143 #define SA_SECONDARYADDRESS 0x0050 /* Non standard extention */
144 
145 #define SA_SOFTWARE 0x8022
146 #define SA_ALTERNATESERVER 0x8023
147 #define SA_FINGERPRINT 0x8028
148 
149 /* define turn attribute */
150 #define TA_CHANNELNUMBER 0x000C
151 #define TA_LIFETIME 0x000D
152 #define TA_DEPRECATEDBANDWIDTH 0x0010
153 #define TA_XORPEERADDRESS 0x0012
154 #define TA_DATA 0x0013
155 #define TA_XORRELAYEDADDRESS 0x0016
156 #define RFC6156A_REQUESTADDRESSFAMILY 0x0017
157 #define TA_EVENPORT 0x0018
158 #define TA_REQUESTEDTRANSPORT 0x0019
159 #define TA_DONTFRAGMENT 0x001A
160 #define TA_DEPRECATEDTIMERVAL 0x0021
161 #define TA_RESERVATIONTOKEN 0x0022
162 
163 #define ICEA_PRIORITY 0x0024
164 #define ICEA_USECANDIDATE 0x0025
165 #define ICEA_ICECONTROLLED 0x8029
166 #define ICEA_ICECONTROLLING 0x802a
167 
168 #define RFC5780_RESPONSE_ORIGIN 0x802b
169 #define RFC5780_OTHER_ADDRESS 0x802c
170 
171 #define ICE_NETWORK_COST 0xc057
172 
173 #define STUN_REQUEST 0x0000
174 #define STUN_INDICATION 0x0010
175 #define STUN_SUCCESS_RESP 0x0100
176 #define STUN_ERR_RESP 0x0110
177 
178 #define STUN_IS_REQUEST(msg_type) (((msg_type) &0x0110) == 0x0000)
179 #define STUN_IS_INDICATION(msg_type) (((msg_type) &0x0110) == 0x0010)
180 #define STUN_IS_SUCCESS_RESP(msg_type) (((msg_type) &0x0110) == 0x0100)
181 #define STUN_IS_ERR_RESP(msg_type) (((msg_type) &0x0110) == 0x0110)
182 
183 /* define types for a stun message */
184 #define STUN_METHOD_BINDING 0x0001
185 #define TURN_MEDHOD_ALLOCATE 0x0003 //(only request/response semantics defined)
186 #define TURN_METHOD_REFRESH 0x0004 //(only request/response semantics defined)
187 #define TURN_METHOD_CREATEPERMISSION 0x0008 //(only request/response semantics defined
188 #define TURN_METHOD_CHANNELBIND 0x0009 //(only request/response semantics defined)
189 
190 //#define BindResponseMsg 0x0101
191 //#define BindErrorResponseMsg 0x0111
192 #define SharedSecretRequestMsg 0x0002
193 #define SharedSecretResponseMsg 0x0102
194 #define SharedSecretErrorResponseMsg 0x0112
195 
196 #define TURN_INDICATION_SEND 0x0006 //(only indication semantics defined)
197 #define TURN_INDICATION_DATA 0x0007 //(only indication semantics defined)
198 
199 #define TURN_DEFAULT_ALLOCATION_LIFETIME 600 /* seconds */
200 #define TURN_PERMISSION_LIFETIME 300 /* seconds */
201 #define TURN_PERMISSION_LIFETIME_RETRANSMISSION 3 /* seconds: interval for retry */
202 #define TURN_CHAN_BINDING_LIFETIME 600 /* seconds */
203 
204 typedef struct {
205  uint16_t msgType;
206  uint16_t msgLength;
207  uint32_t magic_cookie;
208  StunTrId tr_id;
209 } StunMsgHdr;
210 
211 typedef struct {
212  uint16_t chanId;
213  uint16_t msgLength;
215 
216 typedef struct {
217  uint16_t type;
218  uint16_t length;
219 } StunAtrHdr;
220 
221 typedef struct {
222  uint32_t value;
224 
225 typedef struct {
226  uint16_t pad; /* all 0 */
227  uint8_t errorClass;
228  uint8_t number;
229  char reason[STUN_MAX_STRING];
230  uint16_t sizeReason;
231 } StunAtrError;
232 
233 typedef struct {
234  uint16_t attrType[STUN_MAX_UNKNOWN_ATTRIBUTES];
235  uint16_t numAttributes;
237 
238 typedef struct {
239  uint16_t channelNumber;
240  uint16_t rffu; /* Reserved For Future Use */
242 
243 typedef struct {
244  uint32_t lifetime;
246 
247 typedef struct {
248  char value[1500];
249  uint16_t sizeValue;
250 } TurnAtrData;
251 
252 typedef struct {
253  uint8_t proto;
254  uint8_t pad1;
255  uint8_t pad2;
256  uint8_t pad3;
258 
259 typedef struct {
260  uint64_t value;
262 
263 typedef struct {
264  uint32_t fingerprint;
266 
267 typedef struct {
268  char value[STUN_MAX_STRING];
269  uint16_t sizeValue;
270 } StunAtrString;
271 
272 typedef struct {
273  uint32_t priority;
275 
276 typedef struct {
277  uint64_t value;
279 
280 typedef struct {
281  char hash[20];
283 
284 typedef enum { HmacUnkown = 0, HmacOK, HmacBadUserName, HmacUnkownUserName, HmacFailed } StunHmacStatus;
285 
286 typedef struct {
287  uint16_t attrType[STUN_MAX_UNKNOWN_ATTRIBUTES];
288  uint16_t numAttributes;
290 
291 typedef struct {
292  uint8_t value;
293  uint8_t pad1;
294  uint8_t pad2;
295  uint8_t pad3;
297 
298 typedef struct {
299  uint8_t family;
300  uint8_t pad1;
301  uint8_t pad2;
302  uint8_t pad3;
304 
305 typedef struct {
306  uint16_t networkid;
307  uint16_t networkcost;
309 
310 typedef struct {
311  StunMsgHdr msgHdr;
312 
313  bool_t hasMappedAddress;
314  StunIPAddress mappedAddress;
315 
316  bool_t hasResponseAddress;
317  StunIPAddress responseAddress;
318 
319  bool_t hasChangeRequest;
320  StunAtrChangeRequest changeRequest;
321 
322  bool_t hasSourceAddress;
323  StunIPAddress sourceAddress;
324 
325  bool_t hasChangedAddress;
326  StunIPAddress changedAddress;
327 
328  bool_t hasUsername;
329  StunAtrString username;
330 
331  bool_t hasPassword;
332  StunAtrString password;
333 
334  bool_t hasMessageIntegrity;
335  StunAtrIntegrity messageIntegrity;
336 
337  bool_t hasErrorCode;
338  StunAtrError errorCode;
339 
340  bool_t hasUnknownAttributes;
341  StunAtrUnknown unknownAttributes;
342 
343  bool_t hasReflectedFrom;
344  StunIPAddress reflectedFrom;
345 
346  bool_t hasRealm;
347  StunAtrString realmName;
348 
349  bool_t hasNonce;
350  StunAtrString nonceName;
351 
352  bool_t hasXorMappedAddress;
353  StunIPAddress xorMappedAddress;
354 
355  bool_t hasSoftware;
356  StunAtrString softwareName;
357 
358  bool_t hasXorPeerAddress;
359  StunIPAddress xorPeerAddress;
360 
361  bool_t hasXorRelayedAddress;
362  StunIPAddress xorRelayedAddress;
363 
364  bool_t hasFingerprint;
365  StunAtrFingerprint fingerprint;
366 
367  /* Turn elements */
368  bool_t hasChannelNumberAttributes;
369  TurnAtrChannelNumber channelNumberAttributes;
370 
371  bool_t hasLifetimeAttributes;
372  TurnAtrLifetime lifetimeAttributes;
373 
374  bool_t hasData;
375  TurnAtrData data;
376 
377  bool_t hasRequestedTransport;
378  TurnAtrRequestedTransport requestedTransport;
379 
380  bool_t hasDontFragment;
381 
382  bool_t hasReservationToken;
383  TurnAtrReservationToken reservationToken;
384 
385  bool_t hasPriority;
386  IceAtrPriority priority;
387 
388  bool_t hasUseCandidate;
389 
390  bool_t hasIceControlled;
391  IceAtrIceControll iceControlled;
392 
393  bool_t hasIceControlling;
394  IceAtrIceControll iceControlling;
395 
396  bool_t hasResponseOrigin;
397  StunIPAddress responseOrigin;
398 
399  bool_t hasOtherAddress;
400  StunIPAddress otherAddress;
401 
402  bool_t hasEvenPort;
403  TurnAtrEvenPort evenPort;
404 
405  bool_t hasRequestAddressFamily;
406  Rfc6156AtrRequestAddressFamily requestAddressFamily;
407 
408  bool_t hasNetworkCost;
409  IceAtrNetworkCost networkCost;
410 } StunMessage;
411 
412 /* Define enum with different types of NAT */
413 typedef enum { StunTypeUnknown = 0, StunTypeOpen, StunTypeConeNat, StunTypeRestrictedNat, StunTypePortRestrictedNat, StunTypeSymNat, StunTypeSymFirewall, StunTypeBlocked, StunTypeFailure } NatType;
414 
415 #define MAX_MEDIA_RELAYS 500
416 #define MAX_RTP_MSG_SIZE 1500
417 #define MEDIA_RELAY_TIMEOUT 3 * 60
418 
419 typedef struct {
420  int relayPort; /* media relay port */
421  int fd; /* media relay file descriptor */
422  StunIPAddress destination; /* NAT IP:port */
423  time_t expireTime; /* if no activity after time, close the socket */
425 
426 typedef struct {
427  StunIPAddress myAddr;
428  StunIPAddress altAddr;
429  Socket myFd;
430  Socket altPortFd;
431  Socket altIpFd;
432  Socket altIpPortFd;
433  bool_t relay; /* true if media relaying is to be done */
434  StunMediaRelay relays[MAX_MEDIA_RELAYS];
436 
437 void stunCalculateIntegrity_longterm(char* hmac, const char* input, int length, const char* username, const char* realm, const char* password);
438 void stunCalculateIntegrity_shortterm(char* hmac, const char* input, int length, const char* key);
439 uint32_t stunCalculateFingerprint(const char* input, int length);
440 
441 bool_t stunParseMessage(char* buf, unsigned int bufLen, StunMessage* message);
442 
443 void stunBuildReqSimple(StunMessage* msg, const StunAtrString* username, bool_t changePort, bool_t changeIp, unsigned int id);
444 
445 unsigned int stunEncodeMessage(const StunMessage* message, char* buf, unsigned int bufLen, const StunAtrString* password);
446 
447 uint64_t stunGetSystemTimeSecs(void);
448 
449 NatType stunNatType(StunIPAddress* dest, bool_t* preservePort, /* if set, is return for if NAT preservers ports or not */
450  bool_t* hairpin, /* if set, is the return for if NAT will hairpin packets */
451  StunIPAddress* sAddr /* NIC to use */
452 );
453 
454 int stunOpenSocket(StunIPAddress* dest, StunIPAddress* mappedAddr, StunIPAddress* srcAddr);
455 
456 bool_t stunOpenSocketPair(StunIPAddress* dest, StunIPAddress* mapAddr_rtp, StunIPAddress* mapAddr_rtcp, int* fd1, int* fd2, StunIPAddress* srcAddr);
457 
458 #ifdef __cplusplus
459 }
460 #endif
461 
462 #endif
Definition: stun.h:276
Definition: stun.h:305
Definition: stun.h:272
Definition: stun.h:298
Definition: stun.h:221
Definition: stun.h:225
Definition: stun.h:263
Definition: stun.h:216
Definition: stun.h:280
Definition: stun.h:267
Definition: stun.h:233
Definition: stun_udp.h:95
Definition: stun.h:419
Definition: stun.h:310
Definition: stun.h:204
Definition: stun.h:426
Definition: stun_udp.h:103
Definition: stun.h:238
Definition: stun.h:247
Definition: stun.h:291
Definition: stun.h:243
Definition: stun.h:252
Definition: stun.h:259
Definition: stun.h:286
Definition: stun.h:211