Version 0.1.0
[sipe-libnice.git] / stun / stunmessage.h
blobb31189ec4cc59b529a7e9f69d7ef853100db35ce
1 /*
2 * This file is part of the Nice GLib ICE library.
4 * (C) 2008-2009 Collabora Ltd.
5 * Contact: Youness Alaoui
6 * (C) 2008-2009 Nokia Corporation. All rights reserved.
7 * Contact: Rémi Denis-Courmont
9 * The contents of this file are subject to the Mozilla Public License Version
10 * 1.1 (the "License"); you may not use this file except in compliance with
11 * the License. You may obtain a copy of the License at
12 * http://www.mozilla.org/MPL/
14 * Software distributed under the License is distributed on an "AS IS" basis,
15 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
16 * for the specific language governing rights and limitations under the
17 * License.
19 * The Original Code is the Nice GLib ICE library.
21 * The Initial Developers of the Original Code are Collabora Ltd and Nokia
22 * Corporation. All Rights Reserved.
24 * Contributors:
25 * Youness Alaoui, Collabora Ltd.
26 * Rémi Denis-Courmont, Nokia
28 * Alternatively, the contents of this file may be used under the terms of the
29 * the GNU Lesser General Public License Version 2.1 (the "LGPL"), in which
30 * case the provisions of LGPL are applicable instead of those above. If you
31 * wish to allow use of your version of this file only under the terms of the
32 * LGPL and not to allow others to use your version of this file under the
33 * MPL, indicate your decision by deleting the provisions above and replace
34 * them with the notice and other provisions required by the LGPL. If you do
35 * not delete the provisions above, a recipient may use your version of this
36 * file under either the MPL or the LGPL.
39 #ifndef _STUN_MESSAGE_H
40 #define _STUN_MESSAGE_H
43 /**
44 * SECTION:stunmessage
45 * @short_description: STUN messages parsing and formatting functions
46 * @include: stun/stunmessage.h
47 * @see_also: #StunAgent
48 * @stability: Stable
50 * The STUN Messages API allows you to create STUN messages easily as well as to
51 * parse existing messages.
56 #ifdef _WIN32
57 #include "win32_common.h"
58 #else
59 #include <stdint.h>
60 #include <stdbool.h>
61 #endif
63 #include <sys/types.h>
65 #ifdef _WIN32
66 #include <winsock2.h>
67 #include <ws2tcpip.h>
68 #else
69 #include <sys/socket.h>
70 #include <arpa/inet.h>
71 #endif
73 #include "constants.h"
75 typedef struct _StunMessage StunMessage;
77 /**
78 * StunClass:
79 * @STUN_REQUEST: A STUN Request message
80 * @STUN_INDICATION: A STUN indication message
81 * @STUN_RESPONSE: A STUN Response message
82 * @STUN_ERROR: A STUN Error message
84 * This enum is used to represent the class of
85 * a STUN message, as defined in RFC5389
88 /* Message classes */
89 typedef enum
91 STUN_REQUEST=0,
92 STUN_INDICATION=1,
93 STUN_RESPONSE=2,
94 STUN_ERROR=3
95 } StunClass;
98 /**
99 * StunMethod:
100 * @STUN_BINDING: The Binding method as defined by the RFC5389
101 * @STUN_SHARED_SECRET: The Shared-Secret method as defined by the RFC3489
102 * @STUN_ALLOCATE: The Allocate method as defined by the TURN draft 12
103 * @STUN_SET_ACTIVE_DST: The Set-Active-Destination method as defined by
104 * the TURN draft 4
105 * @STUN_REFRESH: The Refresh method as defined by the TURN draft 12
106 * @STUN_SEND: The Send method as defined by the TURN draft 00
107 * @STUN_CONNECT: The Connect method as defined by the TURN draft 4
108 * @STUN_OLD_SET_ACTIVE_DST: The older Set-Active-Destination method as
109 * defined by the TURN draft 0
110 * @STUN_IND_SEND: The Send method used in indication messages as defined
111 * by the TURN draft 12
112 * @STUN_IND_DATA: The Data method used in indication messages as defined
113 * by the TURN draft 12
114 * @STUN_IND_CONNECT_STATUS: The Connect-Status method used in indication
115 * messages as defined by the TURN draft 4
116 * @STUN_CREATEPERMISSION: The CreatePermission method as defined by
117 * the TURN draft 12
118 * @STUN_CHANNELBIND: The ChannelBind method as defined by the TURN draft 12
120 * This enum is used to represent the method of
121 * a STUN message, as defined by various RFCs
123 /* Message methods */
124 typedef enum
126 STUN_BINDING=0x001, /* RFC5389 */
127 STUN_SHARED_SECRET=0x002, /* old RFC3489 */
128 STUN_ALLOCATE=0x003, /* TURN-12 */
129 STUN_SET_ACTIVE_DST=0x004, /* TURN-04 */
130 STUN_REFRESH=0x004, /* TURN-12 */
131 STUN_SEND=0x004, /* TURN-00 */
132 STUN_CONNECT=0x005, /* TURN-04 */
133 STUN_OLD_SET_ACTIVE_DST=0x006, /* TURN-00 */
134 STUN_IND_SEND=0x006, /* TURN-12 */
135 STUN_IND_DATA=0x007, /* TURN-12 */
136 STUN_IND_CONNECT_STATUS=0x008, /* TURN-04 */
137 STUN_CREATEPERMISSION= 0x008, /* TURN-12 */
138 STUN_CHANNELBIND= 0x009 /* TURN-12 */
139 } StunMethod;
142 * StunAttribute:
143 * @STUN_ATTRIBUTE_MAPPED_ADDRESS: The MAPPED-ADDRESS attribute as defined
144 * by RFC5389
145 * @STUN_ATTRIBUTE_RESPONSE_ADDRESS: The RESPONSE-ADDRESS attribute as defined
146 * by RFC3489
147 * @STUN_ATTRIBUTE_CHANGE_REQUEST: The CHANGE-REQUEST attribute as defined by
148 * RFC3489
149 * @STUN_ATTRIBUTE_SOURCE_ADDRESS: The SOURCE-ADDRESS attribute as defined by
150 * RFC3489
151 * @STUN_ATTRIBUTE_CHANGED_ADDRESS: The CHANGED-ADDRESS attribute as defined
152 * by RFC3489
153 * @STUN_ATTRIBUTE_USERNAME: The USERNAME attribute as defined by RFC5389
154 * @STUN_ATTRIBUTE_PASSWORD: The PASSWORD attribute as defined by RFC3489
155 * @STUN_ATTRIBUTE_MESSAGE_INTEGRITY: The MESSAGE-INTEGRITY attribute as defined
156 * by RFC5389
157 * @STUN_ATTRIBUTE_ERROR_CODE: The ERROR-CODE attribute as defined by RFC5389
158 * @STUN_ATTRIBUTE_UNKNOWN_ATTRIBUTES: The UNKNOWN-ATTRIBUTES attribute as
159 * defined by RFC5389
160 * @STUN_ATTRIBUTE_REFLECTED_FROM: The REFLECTED-FROM attribute as defined
161 * by RFC3489
162 * @STUN_ATTRIBUTE_CHANNEL_NUMBER: The CHANNEL-NUMBER attribute as defined by
163 * TURN draft 09 and 12
164 * @STUN_ATTRIBUTE_LIFETIME: The LIFETIME attribute as defined by TURN
165 * draft 04, 09 and 12
166 * @STUN_ATTRIBUTE_MAGIC_COOKIE: The MAGIC-COOKIE attribute as defined by
167 * the rosenberg-midcom TURN draft 08
168 * @STUN_ATTRIBUTE_BANDWIDTH: The BANDWIDTH attribute as defined by TURN draft 04
169 * @STUN_ATTRIBUTE_DESTINATION_ADDRESS: The DESTINATION-ADDRESS attribute as
170 * defined by the rosenberg-midcom TURN draft 08
171 * @STUN_ATTRIBUTE_REMOTE_ADDRESS: The REMOTE-ADDRESS attribute as defined by
172 * TURN draft 04
173 * @STUN_ATTRIBUTE_PEER_ADDRESS: The PEER-ADDRESS attribute as defined by
174 * TURN draft 09
175 * @STUN_ATTRIBUTE_XOR_PEER_ADDRESS: The XOR-PEER-ADDRESS attribute as defined
176 * by TURN draft 12
177 * @STUN_ATTRIBUTE_DATA: The DATA attribute as defined by TURN draft 04,
178 * 09 and 12
179 * @STUN_ATTRIBUTE_REALM: The REALM attribute as defined by RFC5389
180 * @STUN_ATTRIBUTE_NONCE: The NONCE attribute as defined by RFC5389
181 * @STUN_ATTRIBUTE_RELAY_ADDRESS: The RELAY-ADDRESS attribute as defined by
182 * TURN draft 04
183 * @STUN_ATTRIBUTE_RELAYED_ADDRESS: The RELAYED-ADDRESS attribute as defined by
184 * TURN draft 09
185 * @STUN_ATTRIBUTE_XOR_RELAYED_ADDRESS: The XOR-RELAYED-ADDRESS attribute as
186 * defined by TURN draft 12
187 * @STUN_ATTRIBUTE_REQUESTED_ADDRESS_TYPE: The REQUESTED-ADDRESS-TYPE attribute
188 * as defined by TURN-IPV6 draft 05
189 * @STUN_ATTRIBUTE_REQUESTED_PORT_PROPS: The REQUESTED-PORT-PROPS attribute
190 * as defined by TURN draft 04
191 * @STUN_ATTRIBUTE_REQUESTED_PROPS: The REQUESTED-PROPS attribute as defined
192 * by TURN draft 09
193 * @STUN_ATTRIBUTE_EVEN_PORT: The EVEN-PORT attribute as defined by TURN draft 12
194 * @STUN_ATTRIBUTE_REQUESTED_TRANSPORT: The REQUESTED-TRANSPORT attribute as
195 * defined by TURN draft 12
196 * @STUN_ATTRIBUTE_DONT_FRAGMENT: The DONT-FRAGMENT attribute as defined
197 * by TURN draft 12
198 * @STUN_ATTRIBUTE_XOR_MAPPED_ADDRESS: The XOR-MAPPED-ADDRESS attribute as
199 * defined by RFC5389
200 * @STUN_ATTRIBUTE_TIMER_VAL: The TIMER-VAL attribute as defined by TURN draft 04
201 * @STUN_ATTRIBUTE_REQUESTED_IP: The REQUESTED-IP attribute as defined by
202 * TURN draft 04
203 * @STUN_ATTRIBUTE_RESERVATION_TOKEN: The RESERVATION-TOKEN attribute as defined
204 * by TURN draft 09 and 12
205 * @STUN_ATTRIBUTE_CONNECT_STAT: The CONNECT-STAT attribute as defined by TURN
206 * draft 04
207 * @STUN_ATTRIBUTE_PRIORITY: The PRIORITY attribute as defined by ICE draft 19
208 * @STUN_ATTRIBUTE_USE_CANDIDATE: The USE-CANDIDATE attribute as defined by
209 * ICE draft 19
210 * @STUN_ATTRIBUTE_OPTIONS: The OPTIONS optional attribute as defined by
211 * libjingle
212 * @STUN_ATTRIBUTE_MS_VERSION: The MS-VERSION optional attribute as defined
213 * by [MS-TURN]
214 * @STUN_ATTRIBUTE_SOFTWARE: The SOFTWARE optional attribute as defined by RFC5389
215 * @STUN_ATTRIBUTE_ALTERNATE_SERVER: The ALTERNATE-SERVER optional attribute as
216 * defined by RFC5389
217 * @STUN_ATTRIBUTE_FINGERPRINT: The FINGERPRINT optional attribute as defined
218 * by RFC5389
219 * @STUN_ATTRIBUTE_ICE_CONTROLLED: The ICE-CONTROLLED optional attribute as
220 * defined by ICE draft 19
221 * @STUN_ATTRIBUTE_ICE_CONTROLLING: The ICE-CONTROLLING optional attribute as
222 * defined by ICE draft 19
223 * @STUN_ATTRIBUTE_MS_SEQUENCE_NUMBER: The MS-SEQUENCE NUMBER optional attribute
224 * as defined by [MS-TURN]
225 * @STUN_ATTRIBUTE_CANDIDATE_IDENTIFIER: The CANDIDATE-IDENTIFIER optional
226 * attribute as defined by [MS-ICE2]
228 * Known STUN attribute types as defined by various RFCs and drafts
230 /* Should be in sync with stun_is_unknown() */
231 typedef enum
233 /* Mandatory attributes */
234 /* 0x0000 */ /* reserved */
235 STUN_ATTRIBUTE_MAPPED_ADDRESS=0x0001, /* RFC5389 */
236 STUN_ATTRIBUTE_RESPONSE_ADDRESS=0x0002, /* old RFC3489 */
237 STUN_ATTRIBUTE_CHANGE_REQUEST=0x0003, /* old RFC3489 */
238 STUN_ATTRIBUTE_SOURCE_ADDRESS=0x0004, /* old RFC3489 */
239 STUN_ATTRIBUTE_CHANGED_ADDRESS=0x0005, /* old RFC3489 */
240 STUN_ATTRIBUTE_USERNAME=0x0006, /* RFC5389 */
241 STUN_ATTRIBUTE_PASSWORD=0x0007, /* old RFC3489 */
242 STUN_ATTRIBUTE_MESSAGE_INTEGRITY=0x0008, /* RFC5389 */
243 STUN_ATTRIBUTE_ERROR_CODE=0x0009, /* RFC5389 */
244 STUN_ATTRIBUTE_UNKNOWN_ATTRIBUTES=0x000A, /* RFC5389 */
245 STUN_ATTRIBUTE_REFLECTED_FROM=0x000B, /* old RFC3489 */
246 STUN_ATTRIBUTE_CHANNEL_NUMBER=0x000C, /* TURN-12 */
247 STUN_ATTRIBUTE_LIFETIME=0x000D, /* TURN-12 */
248 /* MS_ALTERNATE_SERVER is only used by Microsoft's dialect, probably should
249 * not to be placed in STUN_ALL_KNOWN_ATTRIBUTES */
250 STUN_ATTRIBUTE_MS_ALTERNATE_SERVER=0x000E, /* MS-TURN */
251 STUN_ATTRIBUTE_MAGIC_COOKIE=0x000F, /* midcom-TURN 08 */
252 STUN_ATTRIBUTE_BANDWIDTH=0x0010, /* TURN-04 */
253 STUN_ATTRIBUTE_DESTINATION_ADDRESS=0x0011, /* midcom-TURN 08 */
254 STUN_ATTRIBUTE_REMOTE_ADDRESS=0x0012, /* TURN-04 */
255 STUN_ATTRIBUTE_PEER_ADDRESS=0x0012, /* TURN-09 */
256 STUN_ATTRIBUTE_XOR_PEER_ADDRESS=0x0012, /* TURN-12 */
257 STUN_ATTRIBUTE_DATA=0x0013, /* TURN-12 */
258 STUN_ATTRIBUTE_REALM=0x0014, /* RFC5389 */
259 STUN_ATTRIBUTE_NONCE=0x0015, /* RFC5389 */
260 STUN_ATTRIBUTE_RELAY_ADDRESS=0x0016, /* TURN-04 */
261 STUN_ATTRIBUTE_RELAYED_ADDRESS=0x0016, /* TURN-09 */
262 STUN_ATTRIBUTE_XOR_RELAYED_ADDRESS=0x0016, /* TURN-12 */
263 STUN_ATTRIBUTE_REQUESTED_ADDRESS_TYPE=0x0017, /* TURN-IPv6-05 */
264 STUN_ATTRIBUTE_REQUESTED_PORT_PROPS=0x0018, /* TURN-04 */
265 STUN_ATTRIBUTE_REQUESTED_PROPS=0x0018, /* TURN-09 */
266 STUN_ATTRIBUTE_EVEN_PORT=0x0018, /* TURN-12 */
267 STUN_ATTRIBUTE_REQUESTED_TRANSPORT=0x0019, /* TURN-12 */
268 STUN_ATTRIBUTE_DONT_FRAGMENT=0x001A, /* TURN-12 */
269 /* 0x001B */ /* reserved */
270 /* 0x001C */ /* reserved */
271 /* 0x001D */ /* reserved */
272 /* 0x001E */ /* reserved */
273 /* 0x001F */ /* reserved */
274 STUN_ATTRIBUTE_XOR_MAPPED_ADDRESS=0x0020, /* RFC5389 */
275 STUN_ATTRIBUTE_TIMER_VAL=0x0021, /* TURN-04 */
276 STUN_ATTRIBUTE_REQUESTED_IP=0x0022, /* TURN-04 */
277 STUN_ATTRIBUTE_RESERVATION_TOKEN=0x0022, /* TURN-09 */
278 STUN_ATTRIBUTE_CONNECT_STAT=0x0023, /* TURN-04 */
279 STUN_ATTRIBUTE_PRIORITY=0x0024, /* ICE-19 */
280 STUN_ATTRIBUTE_USE_CANDIDATE=0x0025, /* ICE-19 */
281 /* 0x0026 */ /* reserved */
282 /* 0x0027 */ /* reserved */
283 /* 0x0028 */ /* reserved */
284 /* 0x0029 */ /* reserved */
285 /* 0x002A-0x7fff */ /* reserved */
287 /* Optional attributes */
288 /* 0x8000-0x8021 */ /* reserved */
289 STUN_ATTRIBUTE_OPTIONS=0x8001, /* libjingle */
290 STUN_ATTRIBUTE_MS_VERSION=0x8008, /* MS-TURN */
291 STUN_ATTRIBUTE_SOFTWARE=0x8022, /* RFC5389 */
292 STUN_ATTRIBUTE_ALTERNATE_SERVER=0x8023, /* RFC5389 */
293 /* 0x8024 */ /* reserved */
294 /* 0x8025 */ /* reserved */
295 /* 0x8026 */ /* reserved */
296 /* 0x8027 */ /* reserved */
297 STUN_ATTRIBUTE_FINGERPRINT=0x8028, /* RFC5389 */
298 STUN_ATTRIBUTE_ICE_CONTROLLED=0x8029, /* ICE-19 */
299 STUN_ATTRIBUTE_ICE_CONTROLLING=0x802A, /* ICE-19 */
300 /* 0x802B-0x804F */ /* reserved */
301 STUN_ATTRIBUTE_MS_SEQUENCE_NUMBER=0x8050, /* MS-TURN */
302 /* 0x8051-0x8053 */ /* reserved */
303 STUN_ATTRIBUTE_CANDIDATE_IDENTIFIER=0x8054 /* MS-ICE2 */
304 /* 0x8055-0xFFFF */ /* reserved */
305 } StunAttribute;
309 * STUN_ALL_KNOWN_ATTRIBUTES:
311 * An array containing all the currently known and defined mandatory attributes
312 * from StunAttribute
314 /* Should be in sync with StunAttribute */
315 static const uint16_t STUN_ALL_KNOWN_ATTRIBUTES[] =
317 STUN_ATTRIBUTE_MAPPED_ADDRESS,
318 STUN_ATTRIBUTE_RESPONSE_ADDRESS,
319 STUN_ATTRIBUTE_CHANGE_REQUEST,
320 STUN_ATTRIBUTE_SOURCE_ADDRESS,
321 STUN_ATTRIBUTE_CHANGED_ADDRESS,
322 STUN_ATTRIBUTE_USERNAME,
323 STUN_ATTRIBUTE_PASSWORD,
324 STUN_ATTRIBUTE_MESSAGE_INTEGRITY,
325 STUN_ATTRIBUTE_ERROR_CODE,
326 STUN_ATTRIBUTE_UNKNOWN_ATTRIBUTES,
327 STUN_ATTRIBUTE_REFLECTED_FROM,
328 STUN_ATTRIBUTE_CHANNEL_NUMBER,
329 STUN_ATTRIBUTE_LIFETIME,
330 STUN_ATTRIBUTE_MAGIC_COOKIE,
331 STUN_ATTRIBUTE_BANDWIDTH,
332 STUN_ATTRIBUTE_DESTINATION_ADDRESS,
333 STUN_ATTRIBUTE_REMOTE_ADDRESS,
334 STUN_ATTRIBUTE_PEER_ADDRESS,
335 STUN_ATTRIBUTE_XOR_PEER_ADDRESS,
336 STUN_ATTRIBUTE_DATA,
337 STUN_ATTRIBUTE_REALM,
338 STUN_ATTRIBUTE_NONCE,
339 STUN_ATTRIBUTE_RELAY_ADDRESS,
340 STUN_ATTRIBUTE_RELAYED_ADDRESS,
341 STUN_ATTRIBUTE_XOR_RELAYED_ADDRESS,
342 STUN_ATTRIBUTE_REQUESTED_ADDRESS_TYPE,
343 STUN_ATTRIBUTE_REQUESTED_PORT_PROPS,
344 STUN_ATTRIBUTE_REQUESTED_PROPS,
345 STUN_ATTRIBUTE_EVEN_PORT,
346 STUN_ATTRIBUTE_REQUESTED_TRANSPORT,
347 STUN_ATTRIBUTE_DONT_FRAGMENT,
348 STUN_ATTRIBUTE_XOR_MAPPED_ADDRESS,
349 STUN_ATTRIBUTE_TIMER_VAL,
350 STUN_ATTRIBUTE_REQUESTED_IP,
351 STUN_ATTRIBUTE_RESERVATION_TOKEN,
352 STUN_ATTRIBUTE_CONNECT_STAT,
353 STUN_ATTRIBUTE_PRIORITY,
354 STUN_ATTRIBUTE_USE_CANDIDATE,
359 * STUN_MSOC_KNOWN_ATTRIBUTES:
361 * An array containing all the currently known mandatory attributes used by
362 * Microsoft Office Communicator as defined in [MS-TURN]
364 static const uint16_t STUN_MSOC_KNOWN_ATTRIBUTES[] =
366 STUN_ATTRIBUTE_MAPPED_ADDRESS,
367 STUN_ATTRIBUTE_USERNAME,
368 STUN_ATTRIBUTE_MESSAGE_INTEGRITY,
369 STUN_ATTRIBUTE_ERROR_CODE,
370 STUN_ATTRIBUTE_UNKNOWN_ATTRIBUTES,
371 STUN_ATTRIBUTE_LIFETIME,
372 STUN_ATTRIBUTE_MS_ALTERNATE_SERVER,
373 STUN_ATTRIBUTE_MAGIC_COOKIE,
374 STUN_ATTRIBUTE_BANDWIDTH,
375 STUN_ATTRIBUTE_DESTINATION_ADDRESS,
376 STUN_ATTRIBUTE_REMOTE_ADDRESS,
377 STUN_ATTRIBUTE_DATA,
378 /* REALM and NONCE have swapped hexadecimal IDs in [MS-TURN]. Libnice users
379 * or developers can still use these enumeration values in their original
380 * meanings from StunAttribute anywhere in the code, as stun_message_find()
381 * and stun_message_append() will choose correct ID in MSOC compatibility
382 * modes. */
383 STUN_ATTRIBUTE_NONCE,
384 STUN_ATTRIBUTE_REALM,
389 * StunTransactionId:
391 * A type that holds a STUN transaction id.
393 typedef uint8_t StunTransactionId[STUN_MESSAGE_TRANS_ID_LEN];
397 * StunError:
398 * @STUN_ERROR_TRY_ALTERNATE: The ERROR-CODE value for the
399 * "Try Alternate" error as defined in RFC5389
400 * @STUN_ERROR_BAD_REQUEST: The ERROR-CODE value for the
401 * "Bad Request" error as defined in RFC5389
402 * @STUN_ERROR_UNAUTHORIZED: The ERROR-CODE value for the
403 * "Unauthorized" error as defined in RFC5389
404 * @STUN_ERROR_UNKNOWN_ATTRIBUTE: The ERROR-CODE value for the
405 * "Unknown Attribute" error as defined in RFC5389
406 * @STUN_ERROR_ALLOCATION_MISMATCH:The ERROR-CODE value for the
407 * "Allocation Mismatch" error as defined in TURN draft 12.
408 * Equivalent to the "No Binding" error defined in TURN draft 04.
409 * @STUN_ERROR_STALE_NONCE: The ERROR-CODE value for the
410 * "Stale Nonce" error as defined in RFC5389
411 * @STUN_ERROR_ACT_DST_ALREADY: The ERROR-CODE value for the
412 * "Active Destination Already Set" error as defined in TURN draft 04.
413 * @STUN_ERROR_UNSUPPORTED_FAMILY: The ERROR-CODE value for the
414 * "Address Family not Supported" error as defined in TURN IPV6 Draft 05.
415 * @STUN_ERROR_WRONG_CREDENTIALS: The ERROR-CODE value for the
416 * "Wrong Credentials" error as defined in TURN Draft 12.
417 * @STUN_ERROR_UNSUPPORTED_TRANSPORT:he ERROR-CODE value for the
418 * "Unsupported Transport Protocol" error as defined in TURN Draft 12.
419 * @STUN_ERROR_INVALID_IP: The ERROR-CODE value for the
420 * "Invalid IP Address" error as defined in TURN draft 04.
421 * @STUN_ERROR_INVALID_PORT: The ERROR-CODE value for the
422 * "Invalid Port" error as defined in TURN draft 04.
423 * @STUN_ERROR_OP_TCP_ONLY: The ERROR-CODE value for the
424 * "Operation for TCP Only" error as defined in TURN draft 04.
425 * @STUN_ERROR_CONN_ALREADY: The ERROR-CODE value for the
426 * "Connection Already Exists" error as defined in TURN draft 04.
427 * @STUN_ERROR_ALLOCATION_QUOTA_REACHED: The ERROR-CODE value for the
428 * "Allocation Quota Reached" error as defined in TURN draft 12.
429 * @STUN_ERROR_ROLE_CONFLICT:The ERROR-CODE value for the
430 * "Role Conflict" error as defined in ICE draft 19.
431 * @STUN_ERROR_SERVER_ERROR: The ERROR-CODE value for the
432 * "Server Error" error as defined in RFC5389
433 * @STUN_ERROR_SERVER_CAPACITY: The ERROR-CODE value for the
434 * "Insufficient Capacity" error as defined in TURN draft 04.
435 * @STUN_ERROR_INSUFFICIENT_CAPACITY: The ERROR-CODE value for the
436 * "Insufficient Capacity" error as defined in TURN draft 12.
437 * @STUN_ERROR_MAX: The maximum possible ERROR-CODE value as defined by RFC 5389.
439 * STUN error codes as defined by various RFCs and drafts
441 /* Should be in sync with stun_strerror() */
442 typedef enum
444 STUN_ERROR_TRY_ALTERNATE=300, /* RFC5389 */
445 STUN_ERROR_BAD_REQUEST=400, /* RFC5389 */
446 STUN_ERROR_UNAUTHORIZED=401, /* RFC5389 */
447 STUN_ERROR_UNKNOWN_ATTRIBUTE=420, /* RFC5389 */
448 STUN_ERROR_ALLOCATION_MISMATCH=437, /* TURN-12 */
449 STUN_ERROR_STALE_NONCE=438, /* RFC5389 */
450 STUN_ERROR_ACT_DST_ALREADY=439, /* TURN-04 */
451 STUN_ERROR_UNSUPPORTED_FAMILY=440, /* TURN-IPv6-05 */
452 STUN_ERROR_WRONG_CREDENTIALS=441, /* TURN-12 */
453 STUN_ERROR_UNSUPPORTED_TRANSPORT=442, /* TURN-12 */
454 STUN_ERROR_INVALID_IP=443, /* TURN-04 */
455 STUN_ERROR_INVALID_PORT=444, /* TURN-04 */
456 STUN_ERROR_OP_TCP_ONLY=445, /* TURN-04 */
457 STUN_ERROR_CONN_ALREADY=446, /* TURN-04 */
458 STUN_ERROR_ALLOCATION_QUOTA_REACHED=486, /* TURN-12 */
459 STUN_ERROR_ROLE_CONFLICT=487, /* ICE-19 */
460 STUN_ERROR_SERVER_ERROR=500, /* RFC5389 */
461 STUN_ERROR_SERVER_CAPACITY=507, /* TURN-04 */
462 STUN_ERROR_INSUFFICIENT_CAPACITY=508, /* TURN-12 */
463 STUN_ERROR_MAX=699
464 } StunError;
468 * StunMessageReturn:
469 * @STUN_MESSAGE_RETURN_SUCCESS: The operation was successful
470 * @STUN_MESSAGE_RETURN_NOT_FOUND: The attribute was not found
471 * @STUN_MESSAGE_RETURN_INVALID: The argument or data is invalid
472 * @STUN_MESSAGE_RETURN_NOT_ENOUGH_SPACE: There is not enough space in the
473 * message to append data to it, or not enough in an argument to fill it with
474 * the data requested.
475 * @STUN_MESSAGE_RETURN_UNSUPPORTED_ADDRESS: The address in the arguments or in
476 * the STUN message is not supported.
478 * The return value of most stun_message_* functions.
479 * This enum will report on whether an operation was successful or not
480 * and what error occured if any.
482 typedef enum
484 STUN_MESSAGE_RETURN_SUCCESS,
485 STUN_MESSAGE_RETURN_NOT_FOUND,
486 STUN_MESSAGE_RETURN_INVALID,
487 STUN_MESSAGE_RETURN_NOT_ENOUGH_SPACE,
488 STUN_MESSAGE_RETURN_UNSUPPORTED_ADDRESS
489 } StunMessageReturn;
491 #include "stunagent.h"
494 * STUN_MAX_MESSAGE_SIZE:
496 * The Maximum size of a STUN message
498 #define STUN_MAX_MESSAGE_SIZE 65552
501 * StunMessage:
502 * @agent: The agent that created or validated this message
503 * @buffer: The buffer containing the STUN message
504 * @buffer_len: The length of the buffer (not the size of the message)
505 * @key: The short term credentials key to use for authentication validation
506 * or that was used to finalize this message
507 * @key_len: The length of the associated key
508 * @long_term_key: The long term credential key to use for authentication
509 * validation or that was used to finalize this message
510 * @long_term_valid: Whether or not the #long_term_key variable contains valid
511 * data
513 * This structure represents a STUN message
515 struct _StunMessage {
516 StunAgent *agent;
517 uint8_t *buffer;
518 size_t buffer_len;
519 uint8_t *key;
520 size_t key_len;
521 uint8_t long_term_key[16];
522 bool long_term_valid;
526 * stun_message_init:
527 * @msg: The #StunMessage to initialize
528 * @c: STUN message class (host byte order)
529 * @m: STUN message method (host byte order)
530 * @id: 16-bytes transaction ID
532 * Initializes a STUN message buffer, with no attributes.
533 * Returns: %TRUE if the initialization was successful
535 bool stun_message_init (StunMessage *msg, StunClass c, StunMethod m,
536 const StunTransactionId id);
539 * stun_message_length:
540 * @msg: The #StunMessage
542 * Get the length of the message (including the header)
544 * Returns: The length of the message
546 uint16_t stun_message_length (const StunMessage *msg);
549 * stun_message_find:
550 * @msg: The #StunMessage
551 * @type: The #StunAttribute to find
552 * @palen: A pointer to store the length of the attribute
554 * Finds an attribute in a STUN message and fetches its content
556 * Returns: A pointer to the start of the attribute payload if found,
557 * otherwise NULL.
559 const void * stun_message_find (const StunMessage * msg, StunAttribute type,
560 uint16_t *palen);
564 * stun_message_find_flag:
565 * @msg: The #StunMessage
566 * @type: The #StunAttribute to find
568 * Looks for a flag attribute within a valid STUN message.
570 * Returns: A #StunMessageReturn value.
571 * %STUN_MESSAGE_RETURN_INVALID is returned if the attribute's size is not zero.
573 StunMessageReturn stun_message_find_flag (const StunMessage *msg,
574 StunAttribute type);
577 * stun_message_find32:
578 * @msg: The #StunMessage
579 * @type: The #StunAttribute to find
580 * @pval: A pointer where to store the value (host byte order)
582 * Extracts a 32-bits attribute from a STUN message.
584 * Returns: A #StunMessageReturn value.
585 * %STUN_MESSAGE_RETURN_INVALID is returned if the attribute's size is not
586 * 4 bytes.
588 StunMessageReturn stun_message_find32 (const StunMessage *msg,
589 StunAttribute type, uint32_t *pval);
592 * stun_message_find64:
593 * @msg: The #StunMessage
594 * @type: The #StunAttribute to find
595 * @pval: A pointer where to store the value (host byte order)
597 * Extracts a 64-bits attribute from a STUN message.
599 * Returns: A #StunMessageReturn value.
600 * %STUN_MESSAGE_RETURN_INVALID is returned if the attribute's size is not
601 * 8 bytes.
603 StunMessageReturn stun_message_find64 (const StunMessage *msg,
604 StunAttribute type, uint64_t *pval);
607 * stun_message_find_string:
608 * @msg: The #StunMessage
609 * @type: The #StunAttribute to find
610 * @buf: A pointer where to store the data
611 * @buflen: The length of the buffer
613 * Extracts an UTF-8 string from a valid STUN message.
615 * Returns: A #StunMessageReturn value.
616 * %STUN_MESSAGE_RETURN_INVALID is returned if the attribute is improperly
617 * encoded
618 * %STUN_MESSAGE_RETURN_NOT_ENOUGH_SPACE is return if the buffer size is too
619 * small to hold the string
621 <note>
622 <para>
623 The string will be NULL-terminated.
624 </para>
625 </note>
628 StunMessageReturn stun_message_find_string (const StunMessage *msg,
629 StunAttribute type, char *buf, size_t buflen);
632 * stun_message_find_addr:
633 * @msg: The #StunMessage
634 * @type: The #StunAttribute to find
635 * @addr: The #sockaddr to be filled
636 * @addrlen: The size of the @addr variable. Must be set to the size of the
637 * @addr socket address and will be set to the size of the extracted socket
638 * address.
640 * Extracts a network address attribute from a STUN message.
642 * Returns: A #StunMessageReturn value.
643 * %STUN_MESSAGE_RETURN_INVALID is returned if the attribute payload size is
644 * wrong or if the @addrlen is too small
645 * %STUN_MESSAGE_RETURN_UNSUPPORTED_ADDRESS if the address family is unknown.
647 StunMessageReturn stun_message_find_addr (const StunMessage *msg,
648 StunAttribute type, struct sockaddr *addr, socklen_t *addrlen);
651 * stun_message_find_xor_addr:
652 * @msg: The #StunMessage
653 * @type: The #StunAttribute to find
654 * @addr: The #sockaddr to be filled
655 * @addrlen: The size of the @addr variable. Must be set to the size of the
656 * @addr socket address and will be set to the size of the
657 * extracted socket address.
659 * Extracts an obfuscated network address attribute from a STUN message.
661 * Returns: A #StunMessageReturn value.
662 * %STUN_MESSAGE_RETURN_INVALID is returned if the attribute payload size is
663 * wrong or if the @addrlen is too small
664 * %STUN_MESSAGE_RETURN_UNSUPPORTED_ADDRESS if the address family is unknown.
666 StunMessageReturn stun_message_find_xor_addr (const StunMessage *msg,
667 StunAttribute type, struct sockaddr *addr, socklen_t *addrlen);
670 * stun_message_find_xor_addr_full:
671 * @msg: The #StunMessage
672 * @type: The #StunAttribute to find
673 * @addr: The #sockaddr to be filled
674 * @addrlen: The size of the @addr variable. Must be set to the size of the
675 * @addr socket address and will be set to the size of the
676 * extracted socket address.
677 * @magic_cookie: The magic cookie to use to XOR the address.
679 * Extracts an obfuscated network address attribute from a STUN message.
681 * Returns: A #StunMessageReturn value.
682 * %STUN_MESSAGE_RETURN_INVALID is returned if the attribute payload size is
683 * wrong or if the @addrlen is too small
684 * %STUN_MESSAGE_RETURN_UNSUPPORTED_ADDRESS if the address family is unknown.
686 StunMessageReturn stun_message_find_xor_addr_full (const StunMessage *msg,
687 StunAttribute type, struct sockaddr *addr,
688 socklen_t *addrlen, uint32_t magic_cookie);
692 * stun_message_find_error:
693 * @msg: The #StunMessage
694 * @code: A pointer where to store the value
696 * Extract the error response code from a STUN message
698 * Returns: A #StunMessageReturn value.
699 * %STUN_MESSAGE_RETURN_INVALID is returned if the value is invalid
701 StunMessageReturn stun_message_find_error (const StunMessage *msg, int *code);
705 * stun_message_append:
706 * @msg: The #StunMessage
707 * @type: The #StunAttribute to append
708 * @length: The length of the attribute
710 * Reserves room for appending an attribute to an unfinished STUN message.
712 * Returns: A pointer to an unitialized buffer of @length bytes to
713 * where the attribute payload must be written, or NULL if there is not
714 * enough room in the STUN message buffer.
716 void *stun_message_append (StunMessage *msg, StunAttribute type,
717 size_t length);
720 * stun_message_append_bytes:
721 * @msg: The #StunMessage
722 * @type: The #StunAttribute to append
723 * @data: The data to append
724 * @len: The length of the attribute
726 * Appends a binary value to a STUN message
728 * Returns: A #StunMessageReturn value.
730 StunMessageReturn stun_message_append_bytes (StunMessage *msg,
731 StunAttribute type, const void *data, size_t len);
734 * stun_message_append_flag:
735 * @msg: The #StunMessage
736 * @type: The #StunAttribute to append
738 * Appends an empty flag attribute to a STUN message
740 * Returns: A #StunMessageReturn value.
742 StunMessageReturn stun_message_append_flag (StunMessage *msg,
743 StunAttribute type);
746 * stun_message_append32:
747 * @msg: The #StunMessage
748 * @type: The #StunAttribute to append
749 * @value: The value to append (host byte order)
751 * Appends a 32-bits value attribute to a STUN message
753 * Returns: A #StunMessageReturn value.
755 StunMessageReturn stun_message_append32 (StunMessage *msg,
756 StunAttribute type, uint32_t value);
759 * stun_message_append64:
760 * @msg: The #StunMessage
761 * @type: The #StunAttribute to append
762 * @value: The value to append (host byte order)
764 * Appends a 64-bits value attribute to a STUN message
766 * Returns: A #StunMessageReturn value.
768 StunMessageReturn stun_message_append64 (StunMessage *msg,
769 StunAttribute type, uint64_t value);
772 * stun_message_append_string:
773 * @msg: The #StunMessage
774 * @type: The #StunAttribute to append
775 * @str: The string to append
777 * Adds an attribute from a NULL-terminated string to a STUN message
779 * Returns: A #StunMessageReturn value.
781 StunMessageReturn stun_message_append_string (StunMessage *msg,
782 StunAttribute type, const char *str);
785 * stun_message_append_addr:
786 * @msg: The #StunMessage
787 * @type: The #StunAttribute to append
788 * @addr: The #sockaddr to be append
789 * @addrlen: The size of the @addr variable.
791 * Append a network address attribute to a STUN message
793 * Returns: A #StunMessageReturn value.
794 * %STUN_MESSAGE_RETURN_INVALID is returned if the @addrlen is too small
795 * %STUN_MESSAGE_RETURN_UNSUPPORTED_ADDRESS if the address family is unknown.
797 StunMessageReturn stun_message_append_addr (StunMessage * msg,
798 StunAttribute type, const struct sockaddr *addr, socklen_t addrlen);
801 * stun_message_append_xor_addr:
802 * @msg: The #StunMessage
803 * @type: The #StunAttribute to append
804 * @addr: The #sockaddr to be append
805 * @addrlen: The size of the @addr variable.
807 * Append an obfuscated network address attribute to a STUN message
809 * Returns: A #StunMessageReturn value.
810 * %STUN_MESSAGE_RETURN_INVALID is returned if the @addrlen is too small
811 * %STUN_MESSAGE_RETURN_UNSUPPORTED_ADDRESS if the address family is unknown.
813 StunMessageReturn stun_message_append_xor_addr (StunMessage * msg,
814 StunAttribute type, const struct sockaddr *addr, socklen_t addrlen);
817 * stun_message_append_xor_addr_full:
818 * @msg: The #StunMessage
819 * @type: The #StunAttribute to append
820 * @addr: The #sockaddr to be append
821 * @addrlen: The size of the @addr variable.
822 * @magic_cookie: The magic cookie to use to XOR the address.
824 * Append an obfuscated network address attribute from a STUN message.
826 * Returns: A #StunMessageReturn value.
827 * %STUN_MESSAGE_RETURN_INVALID is returned if the @addrlen is too small
828 * %STUN_MESSAGE_RETURN_UNSUPPORTED_ADDRESS if the address family is unknown.
830 StunMessageReturn stun_message_append_xor_addr_full (StunMessage * msg,
831 StunAttribute type, const struct sockaddr *addr, socklen_t addrlen,
832 uint32_t magic_cookie);
835 * stun_message_append_error:
836 * @msg: The #StunMessage
837 * @code: The error code value
839 * Appends the ERROR-CODE attribute to the STUN message and fills it according
840 * to #code
842 * Returns: A #StunMessageReturn value.
844 StunMessageReturn stun_message_append_error (StunMessage * msg,
845 StunError code);
848 * STUN_MESSAGE_BUFFER_INCOMPLETE:
850 * Convenience macro for stun_message_validate_buffer_length() meaning that the
851 * data to validate does not hold a complete STUN message
853 #define STUN_MESSAGE_BUFFER_INCOMPLETE 0
856 * STUN_MESSAGE_BUFFER_INVALID:
858 * Convenience macro for stun_message_validate_buffer_length() meaning that the
859 * data to validate is not a valid STUN message
861 #define STUN_MESSAGE_BUFFER_INVALID -1
865 * stun_message_validate_buffer_length:
866 * @msg: The buffer to validate
867 * @length: The length of the buffer
868 * @has_padding: Set TRUE if attributes should be padded to multiple of 4 bytes
870 * This function will take a data buffer and will try to validate whether it is
871 * a STUN message or if it's not or if it's an incomplete STUN message and will
872 * provide us with the length of the STUN message.
874 * Returns: The length of the valid STUN message in the buffer.
875 * <para> See also: #STUN_MESSAGE_BUFFER_INCOMPLETE </para>
876 * <para> See also: #STUN_MESSAGE_BUFFER_INVALID </para>
878 int stun_message_validate_buffer_length (const uint8_t *msg, size_t length,
879 bool has_padding);
882 * stun_message_id:
883 * @msg: The #StunMessage
884 * @id: The #StunTransactionId to fill
886 * Retreive the STUN transaction id from a STUN message
888 void stun_message_id (const StunMessage *msg, StunTransactionId id);
891 * stun_message_get_class:
892 * @msg: The #StunMessage
894 * Retreive the STUN class from a STUN message
896 * Returns: The #StunClass
898 StunClass stun_message_get_class (const StunMessage *msg);
901 * stun_message_get_method:
902 * @msg: The #StunMessage
904 * Retreive the STUN method from a STUN message
906 * Returns: The #StunMethod
908 StunMethod stun_message_get_method (const StunMessage *msg);
911 * stun_message_has_attribute:
912 * @msg: The #StunMessage
913 * @type: The #StunAttribute to look for
915 * Checks if an attribute is present within a STUN message.
917 * Returns: %TRUE if the attribute is found, %FALSE otherwise
919 bool stun_message_has_attribute (const StunMessage *msg, StunAttribute type);
922 /* Defined in stun5389.c */
924 * stun_message_has_cookie:
925 * @msg: The #StunMessage
927 * Checks if the STUN message has a RFC5389 compatible cookie
929 * Returns: %TRUE if the cookie is present, %FALSE otherwise
931 bool stun_message_has_cookie (const StunMessage *msg);
935 * stun_optional:
936 * @t: An attribute type
938 * Helper function that checks whether a STUN attribute is a mandatory
939 * or an optional attribute
941 * Returns: %TRUE if the attribute is an optional one
943 bool stun_optional (uint16_t t);
946 * stun_strerror:
947 * @code: host-byte order error code
949 * Transforms a STUN error-code into a human readable string
951 * Returns: A static pointer to a NULL-terminated error message string.
953 const char *stun_strerror (StunError code);
956 #endif /* _STUN_MESSAGE_H */