|
1 /* |
|
2 Copyright (c) 2007, Adobe Systems, Incorporated |
|
3 All rights reserved. |
|
4 |
|
5 Redistribution and use in source and binary forms, with or without |
|
6 modification, are permitted provided that the following conditions are |
|
7 met: |
|
8 |
|
9 * Redistributions of source code must retain the above copyright |
|
10 notice, this list of conditions and the following disclaimer. |
|
11 |
|
12 * Redistributions in binary form must reproduce the above copyright |
|
13 notice, this list of conditions and the following disclaimer in the |
|
14 documentation and/or other materials provided with the distribution. |
|
15 |
|
16 * Neither the name of Adobe Systems, Network Resonance nor the names of its |
|
17 contributors may be used to endorse or promote products derived from |
|
18 this software without specific prior written permission. |
|
19 |
|
20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
21 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
22 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
23 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|
24 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
25 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
26 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
27 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
28 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
29 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
30 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
31 */ |
|
32 |
|
33 |
|
34 #ifndef _STUN_H |
|
35 #define _STUN_H |
|
36 |
|
37 #ifdef WIN32 |
|
38 #include <winsock2.h> |
|
39 #else |
|
40 #include <sys/param.h> |
|
41 #include <sys/socket.h> |
|
42 #include <net/if.h> |
|
43 #ifndef LINUX |
|
44 #if !defined(__OpenBSD__) && !defined(__NetBSD__) |
|
45 #include <net/if_var.h> |
|
46 #endif |
|
47 #include <net/if_dl.h> |
|
48 #include <net/if_types.h> |
|
49 #endif |
|
50 #ifndef BSD |
|
51 #include <net/route.h> |
|
52 #endif |
|
53 #include <netinet/in.h> |
|
54 #ifndef LINUX |
|
55 #include <netinet/in_var.h> |
|
56 #endif |
|
57 #include <arpa/inet.h> |
|
58 #include <netdb.h> |
|
59 #endif |
|
60 #include <time.h> |
|
61 |
|
62 #include "nr_api.h" |
|
63 #include "stun_msg.h" |
|
64 #include "stun_build.h" |
|
65 #include "stun_codec.h" |
|
66 #include "stun_hint.h" |
|
67 #include "stun_util.h" |
|
68 #include "nr_socket.h" |
|
69 #include "stun_client_ctx.h" |
|
70 #include "stun_server_ctx.h" |
|
71 #include "stun_proc.h" |
|
72 |
|
73 #define NR_STUN_VERSION "rfc3489bis-11" |
|
74 #define NR_STUN_PORT 3478 |
|
75 |
|
76 /* STUN attributes */ |
|
77 #define NR_STUN_ATTR_MAPPED_ADDRESS 0x0001 |
|
78 #define NR_STUN_ATTR_USERNAME 0x0006 |
|
79 #define NR_STUN_ATTR_MESSAGE_INTEGRITY 0x0008 |
|
80 #define NR_STUN_ATTR_ERROR_CODE 0x0009 |
|
81 #define NR_STUN_ATTR_UNKNOWN_ATTRIBUTES 0x000A |
|
82 #define NR_STUN_ATTR_REALM 0x0014 |
|
83 #define NR_STUN_ATTR_NONCE 0x0015 |
|
84 #define NR_STUN_ATTR_XOR_MAPPED_ADDRESS 0x0020 |
|
85 #define NR_STUN_ATTR_SERVER 0x8022 |
|
86 #define NR_STUN_ATTR_ALTERNATE_SERVER 0x8023 |
|
87 #define NR_STUN_ATTR_FINGERPRINT 0x8028 |
|
88 |
|
89 /* for backwards compatibility with obsolete versions of the STUN spec */ |
|
90 #define NR_STUN_ATTR_OLD_XOR_MAPPED_ADDRESS 0x8020 |
|
91 |
|
92 #ifdef USE_STUND_0_96 |
|
93 #define NR_STUN_ATTR_OLD_CHANGE_REQUEST 0x0003 |
|
94 #endif /* USE_STUND_0_96 */ |
|
95 |
|
96 #ifdef USE_RFC_3489_BACKWARDS_COMPATIBLE |
|
97 /* for backwards compatibility with obsolete versions of the STUN spec */ |
|
98 #define NR_STUN_ATTR_OLD_PASSWORD 0x0007 |
|
99 #define NR_STUN_ATTR_OLD_RESPONSE_ADDRESS 0x0002 |
|
100 #define NR_STUN_ATTR_OLD_SOURCE_ADDRESS 0x0004 |
|
101 #define NR_STUN_ATTR_OLD_CHANGED_ADDRESS 0x0005 |
|
102 #endif /* USE_RFC_3489_BACKWARDS_COMPATIBLE */ |
|
103 |
|
104 #ifdef USE_ICE |
|
105 /* ICE attributes */ |
|
106 #define NR_STUN_ATTR_PRIORITY 0x0024 |
|
107 #define NR_STUN_ATTR_USE_CANDIDATE 0x0025 |
|
108 #define NR_STUN_ATTR_ICE_CONTROLLED 0x8029 |
|
109 #define NR_STUN_ATTR_ICE_CONTROLLING 0x802A |
|
110 #endif /* USE_ICE */ |
|
111 |
|
112 #ifdef USE_TURN |
|
113 /* TURN attributes */ |
|
114 #define NR_STUN_ATTR_LIFETIME 0x000d |
|
115 #define NR_STUN_ATTR_XOR_PEER_ADDRESS 0x0012 |
|
116 #define NR_STUN_ATTR_DATA 0x0013 |
|
117 #define NR_STUN_ATTR_XOR_RELAY_ADDRESS 0x0016 |
|
118 #define NR_STUN_ATTR_REQUESTED_TRANSPORT 0x0019 |
|
119 |
|
120 #define NR_STUN_ATTR_REQUESTED_TRANSPORT_UDP 17 |
|
121 #endif /* USE_TURN */ |
|
122 |
|
123 /* |
|
124 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
|
125 * |M|M|M|M|M|C|M|M|M|C|M|M|M|M| |
|
126 * |1|1|9|8|7|1|6|5|4|0|3|2|1|0| |
|
127 * |1|0| | | | | | | | | | | | | |
|
128 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
|
129 * |
|
130 * Figure 3: Format of STUN Message Type Field |
|
131 */ |
|
132 #define NR_STUN_METHOD_TYPE_BITS(m) \ |
|
133 ((((m) & 0xf80) << 2) | (((m) & 0x070) << 1) | ((m) & 0x00f)) |
|
134 |
|
135 #define NR_STUN_CLASS_TYPE_BITS(c) \ |
|
136 ((((c) & 0x002) << 7) | (((c) & 0x001) << 4)) |
|
137 |
|
138 #define NR_STUN_GET_TYPE_METHOD(t) \ |
|
139 ((((t) >> 2) & 0xf80) | (((t) >> 1) & 0x070) | ((t) & 0x00f)) |
|
140 |
|
141 #define NR_STUN_GET_TYPE_CLASS(t) \ |
|
142 ((((t) >> 7) & 0x002) | (((t) >> 4) & 0x001)) |
|
143 |
|
144 #define NR_STUN_TYPE(m,c) (NR_STUN_METHOD_TYPE_BITS((m)) | NR_STUN_CLASS_TYPE_BITS((c))) |
|
145 |
|
146 /* building blocks for message types */ |
|
147 #define NR_METHOD_BINDING 0x001 |
|
148 #define NR_CLASS_REQUEST 0x0 |
|
149 #define NR_CLASS_INDICATION 0x1 |
|
150 #define NR_CLASS_RESPONSE 0x2 |
|
151 #define NR_CLASS_ERROR_RESPONSE 0x3 |
|
152 |
|
153 /* define types for STUN messages */ |
|
154 #define NR_STUN_MSG_BINDING_REQUEST NR_STUN_TYPE(NR_METHOD_BINDING, \ |
|
155 NR_CLASS_REQUEST) |
|
156 #define NR_STUN_MSG_BINDING_INDICATION NR_STUN_TYPE(NR_METHOD_BINDING, \ |
|
157 NR_CLASS_INDICATION) |
|
158 #define NR_STUN_MSG_BINDING_RESPONSE NR_STUN_TYPE(NR_METHOD_BINDING, \ |
|
159 NR_CLASS_RESPONSE) |
|
160 #define NR_STUN_MSG_BINDING_ERROR_RESPONSE NR_STUN_TYPE(NR_METHOD_BINDING, \ |
|
161 NR_CLASS_ERROR_RESPONSE) |
|
162 |
|
163 #ifdef USE_TURN |
|
164 /* building blocks for TURN message types */ |
|
165 #define NR_METHOD_ALLOCATE 0x003 |
|
166 #define NR_METHOD_REFRESH 0x004 |
|
167 |
|
168 #define NR_METHOD_SEND 0x006 |
|
169 #define NR_METHOD_DATA 0x007 |
|
170 #define NR_METHOD_CREATE_PERMISSION 0x008 |
|
171 #define NR_METHOD_CHANNEL_BIND 0x009 |
|
172 |
|
173 /* define types for a TURN message */ |
|
174 #define NR_STUN_MSG_ALLOCATE_REQUEST NR_STUN_TYPE(NR_METHOD_ALLOCATE, \ |
|
175 NR_CLASS_REQUEST) |
|
176 #define NR_STUN_MSG_ALLOCATE_RESPONSE NR_STUN_TYPE(NR_METHOD_ALLOCATE, \ |
|
177 NR_CLASS_RESPONSE) |
|
178 #define NR_STUN_MSG_ALLOCATE_ERROR_RESPONSE NR_STUN_TYPE(NR_METHOD_ALLOCATE, \ |
|
179 NR_CLASS_ERROR_RESPONSE) |
|
180 #define NR_STUN_MSG_REFRESH_REQUEST NR_STUN_TYPE(NR_METHOD_REFRESH, \ |
|
181 NR_CLASS_REQUEST) |
|
182 #define NR_STUN_MSG_REFRESH_RESPONSE NR_STUN_TYPE(NR_METHOD_REFRESH, \ |
|
183 NR_CLASS_RESPONSE) |
|
184 #define NR_STUN_MSG_REFRESH_ERROR_RESPONSE NR_STUN_TYPE(NR_METHOD_REFRESH, \ |
|
185 NR_CLASS_ERROR_RESPONSE) |
|
186 |
|
187 #define NR_STUN_MSG_SEND_INDICATION NR_STUN_TYPE(NR_METHOD_SEND, \ |
|
188 NR_CLASS_INDICATION) |
|
189 #define NR_STUN_MSG_DATA_INDICATION NR_STUN_TYPE(NR_METHOD_DATA, \ |
|
190 NR_CLASS_INDICATION) |
|
191 |
|
192 #define NR_STUN_MSG_PERMISSION_REQUEST NR_STUN_TYPE(NR_METHOD_CREATE_PERMISSION, \ |
|
193 NR_CLASS_REQUEST) |
|
194 #define NR_STUN_MSG_PERMISSION_RESPONSE NR_STUN_TYPE(NR_METHOD_CREATE_PERMISSION, \ |
|
195 NR_CLASS_RESPONSE) |
|
196 #define NR_STUN_MSG_PERMISSION_ERROR_RESPONSE NR_STUN_TYPE(NR_METHOD_CREATE_PERMISSION, \ |
|
197 NR_CLASS_ERROR_RESPONSE) |
|
198 |
|
199 #define NR_STUN_MSG_CHANNEL_BIND_REQUEST NR_STUN_TYPE(NR_METHOD_CHANNEL_BIND, \ |
|
200 NR_CLASS_REQUEST) |
|
201 #define NR_STUN_MSG_CHANNEL_BIND_RESPONSE NR_STUN_TYPE(NR_METHOD_CHANNEL_BIND, \ |
|
202 NR_CLASS_RESPONSE) |
|
203 #define NR_STUN_MSG_CHANNEL_BIND_ERROR_RESPONSE NR_STUN_TYPE(NR_METHOD_CHANNEL_BIND, \ |
|
204 NR_CLASS_ERROR_RESPONSE) |
|
205 |
|
206 |
|
207 #endif /* USE_TURN */ |
|
208 |
|
209 |
|
210 #define NR_STUN_AUTH_RULE_OPTIONAL (1<<0) |
|
211 #define NR_STUN_AUTH_RULE_SHORT_TERM (1<<8) |
|
212 #define NR_STUN_AUTH_RULE_LONG_TERM (1<<9) |
|
213 |
|
214 #endif |