2 * This file is part of the Nice GLib ICE library.
4 * (C) 2008 Collabora Ltd.
5 * (C) 2008 Nokia Corporation. All rights reserved.
6 * Contact: Youness Alaoui
8 * The contents of this file are subject to the Mozilla Public License Version
9 * 1.1 (the "License"); you may not use this file except in compliance with
10 * the License. You may obtain a copy of the License at
11 * http://www.mozilla.org/MPL/
13 * Software distributed under the License is distributed on an "AS IS" basis,
14 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
15 * for the specific language governing rights and limitations under the
18 * The Original Code is the Nice GLib ICE library.
20 * The Initial Developers of the Original Code are Collabora Ltd and Nokia
21 * Corporation. All Rights Reserved.
24 * Alternatively, the contents of this file may be used under the terms of the
25 * the GNU Lesser General Public License Version 2.1 (the "LGPL"), in which
26 * case the provisions of LGPL are applicable instead of those above. If you
27 * wish to allow use of your version of this file only under the terms of the
28 * LGPL and not to allow others to use your version of this file under the
29 * MPL, indicate your decision by deleting the provisions above and replace
30 * them with the notice and other provisions required by the LGPL. If you do
31 * not delete the provisions above, a recipient may use your version of this
32 * file under either the MPL or the LGPL.
35 #ifndef _STUN_MESSAGE_H
36 #define _STUN_MESSAGE_H
40 #include "win32_common.h"
46 #include <sys/types.h>
47 #include "constants.h"
53 #include <sys/socket.h>
54 #include <arpa/inet.h>
57 typedef struct stun_message_t StunMessage
;
61 * @STUN_REQUEST: A STUN Request message
62 * @STUN_INDICATION: A STUN indication message
63 * @STUN_RESPONSE: A STUN Response message
64 * @STUN_ERROR: A STUN Error message
66 * This enum is used to represent the class of
67 * a STUN message, as defined in RFC5389
82 * @STUN_BINDING: The Binding method as defined by the RFC5389
83 * @STUN_SHARED_SECRET: The Shared-Secret method as defined by the RFC3489
84 * @STUN_ALLOCATE: The Allocate method as defined by the TURN draft 12
85 * @STUN_SET_ACTIVE_DST: The Set-Active-Destination method as defined by
87 * @STUN_REFRESH: The Refresh method as defined by the TURN draft 12
88 * @STUN_SEND: The Send method as defined by the TURN draft 00
89 * @STUN_CONNECT: The Connect method as defined by the TURN draft 4
90 * @STUN_OLD_SET_ACTIVE_DST: The older Set-Active-Destination method as
91 * defined by the TURN draft 0
92 * @STUN_IND_SEND: The Send method used in indication messages as defined
93 * by the TURN draft 12
94 * @STUN_IND_DATA: The Data method used in indication messages as defined
95 * by the TURN draft 12
96 * @STUN_IND_CONNECT_STATUS: The Connect-Status method used in indication
97 * messages as defined by the TURN draft 4
98 * @STUN_CREATEPERMISSION: The CreatePermission method as defined by
100 * @STUN_CHANNELBIND: The ChannelBind method as defined by the TURN draft 12
102 * This enum is used to represent the method of
103 * a STUN message, as defined by various RFCs
105 /* Message methods */
108 STUN_BINDING
=0x001, /* RFC5389 */
109 STUN_SHARED_SECRET
=0x002, /* old RFC3489 */
110 STUN_ALLOCATE
=0x003, /* TURN-12 */
111 STUN_SET_ACTIVE_DST
=0x004, /* TURN-04 */
112 STUN_REFRESH
=0x004, /* TURN-12 */
113 STUN_SEND
=0x004, /* TURN-00 */
114 STUN_CONNECT
=0x005, /* TURN-04 */
115 STUN_OLD_SET_ACTIVE_DST
=0x006, /* TURN-00 */
116 STUN_IND_SEND
=0x006, /* TURN-12 */
117 STUN_IND_DATA
=0x007, /* TURN-12 */
118 STUN_IND_CONNECT_STATUS
=0x008, /* TURN-04 */
119 STUN_CREATEPERMISSION
= 0x008, /* TURN-12 */
120 STUN_CHANNELBIND
= 0x009 /* TURN-12 */
126 * @STUN_ATTRIBUTE_MAPPED_ADDRESS: The MAPPED-ADDRESS attribute as defined
128 * @STUN_ATTRIBUTE_RESPONSE_ADDRESS: The RESPONSE-ADDRESS attribute as defined
130 * @STUN_ATTRIBUTE_CHANGE_REQUEST: The CHANGE-REQUEST attribute as defined by
132 * @STUN_ATTRIBUTE_SOURCE_ADDRESS: The SOURCE-ADDRESS attribute as defined by
134 * @STUN_ATTRIBUTE_CHANGED_ADDRESS: The CHANGED-ADDRESS attribute as defined
136 * @STUN_ATTRIBUTE_USERNAME: The USERNAME attribute as defined by RFC5389
137 * @STUN_ATTRIBUTE_PASSWORD: The PASSWORD attribute as defined by RFC3489
138 * @STUN_ATTRIBUTE_MESSAGE_INTEGRITY: The MESSAGE-INTEGRITY attribute as defined
140 * @STUN_ATTRIBUTE_ERROR_CODE: The ERROR-CODE attribute as defined by RFC5389
141 * @STUN_ATTRIBUTE_UNKNOWN_ATTRIBUTES: The UNKNOWN-ATTRIBUTES attribute as
143 * @STUN_ATTRIBUTE_REFLECTED_FROM: The REFLECTED-FROM attribute as defined
145 * @STUN_ATTRIBUTE_CHANNEL_NUMBER: The CHANNEL-NUMBER attribute as defined by
146 * TURN draft 09 and 12
147 * @STUN_ATTRIBUTE_LIFETIME: The LIFETIME attribute as defined by TURN
148 * draft 04, 09 and 12
149 * @STUN_ATTRIBUTE_MAGIC_COOKIE: The MAGIC-COOKIE attribute as defined by
150 * the rosenberg-midcom TURN draft 08
151 * @STUN_ATTRIBUTE_BANDWIDTH: The BANDWIDTH attribute as defined by TURN draft 04
152 * @STUN_ATTRIBUTE_DESTINATION_ADDRESS: The DESTINATION-ADDRESS attribute as
153 * defined by the rosenberg-midcom TURN draft 08
154 * @STUN_ATTRIBUTE_REMOTE_ADDRESS: The REMOTE-ADDRESS attribute as defined by
156 * @STUN_ATTRIBUTE_PEER_ADDRESS: The PEER-ADDRESS attribute as defined by
158 * @STUN_ATTRIBUTE_XOR_PEER_ADDRESS: The XOR-PEER-ADDRESS attribute as defined
160 * @STUN_ATTRIBUTE_DATA: The DATA attribute as defined by TURN draft 04,
162 * @STUN_ATTRIBUTE_REALM: The REALM attribute as defined by RFC5389
163 * @STUN_ATTRIBUTE_NONCE: The NONCE attribute as defined by RFC5389
164 * @STUN_ATTRIBUTE_RELAY_ADDRESS: The RELAY-ADDRESS attribute as defined by
166 * @STUN_ATTRIBUTE_RELAYED_ADDRESS: The RELAYED-ADDRESS attribute as defined by
168 * @STUN_ATTRIBUTE_XOR_RELAYED_ADDRESS: The XOR-RELAYED-ADDRESS attribute as
169 * defined by TURN draft 12
170 * @STUN_ATTRIBUTE_REQUESTED_ADDRESS_TYPE: The REQUESTED-ADDRESS-TYPE attribute
171 * as defined by TURN-IPV6 draft 05
172 * @STUN_ATTRIBUTE_REQUESTED_PORT_PROPS: The REQUESTED-PORT-PROPS attribute
173 * as defined by TURN draft 04
174 * @STUN_ATTRIBUTE_REQUESTED_PROPS: The REQUESTED-PROPS attribute as defined
176 * @STUN_ATTRIBUTE_EVEN_PORT: The EVEN-PORT attribute as defined by TURN draft 12
177 * @STUN_ATTRIBUTE_REQUESTED_TRANSPORT: The REQUESTED-TRANSPORT attribute as
178 * defined by TURN draft 12
179 * @STUN_ATTRIBUTE_DONT_FRAGMENT: The DONT-FRAGMENT attribute as defined
181 * @STUN_ATTRIBUTE_XOR_MAPPED_ADDRESS: The XOR-MAPPED-ADDRESS attribute as
183 * @STUN_ATTRIBUTE_TIMER_VAL: The TIMER-VAL attribute as defined by TURN draft 04
184 * @STUN_ATTRIBUTE_REQUESTED_IP: The REQUESTED-IP attribute as defined by
186 * @STUN_ATTRIBUTE_RESERVATION_TOKEN: The RESERVATION-TOKEN attribute as defined
187 * by TURN draft 09 and 12
188 * @STUN_ATTRIBUTE_CONNECT_STAT: The CONNECT-STAT attribute as defined by TURN
190 * @STUN_ATTRIBUTE_PRIORITY: The PRIORITY attribute as defined by ICE draft 19
191 * @STUN_ATTRIBUTE_USE_CANDIDATE: The USE-CANDIDATE attribute as defined by
193 * @STUN_ATTRIBUTE_OPTIONS: The OPTIONS optional attribute as defined by
195 * @STUN_ATTRIBUTE_SOFTWARE: The SOFTWARE optional attribute as defined by RFC5389
196 * @STUN_ATTRIBUTE_ALTERNATE_SERVER: The ALTERNATE-SERVER optional attribute as
198 * @STUN_ATTRIBUTE_FINGERPRINT: The FINGERPRINT optional attribute as defined by RFC5389
199 * @STUN_ATTRIBUTE_ICE_CONTROLLED: The ICE-CONTROLLED optional attribute as
200 * defined by ICE draft 19
201 * @STUN_ATTRIBUTE_ICE_CONTROLLING: The ICE-CONTROLLING optional attribute as
202 * defined by ICE draft 19
204 * Known STUN attribute types as defined by various RFCs and drafts
206 /* Should be in sync with stun_is_unknown() */
209 /* Mandatory attributes */
210 /* 0x0000 */ /* reserved */
211 STUN_ATTRIBUTE_MAPPED_ADDRESS
=0x0001, /* RFC5389 */
212 STUN_ATTRIBUTE_RESPONSE_ADDRESS
=0x0002, /* old RFC3489 */
213 STUN_ATTRIBUTE_CHANGE_REQUEST
=0x0003, /* old RFC3489 */
214 STUN_ATTRIBUTE_SOURCE_ADDRESS
=0x0004, /* old RFC3489 */
215 STUN_ATTRIBUTE_CHANGED_ADDRESS
=0x0005, /* old RFC3489 */
216 STUN_ATTRIBUTE_USERNAME
=0x0006, /* RFC5389 */
217 STUN_ATTRIBUTE_PASSWORD
=0x0007, /* old RFC3489 */
218 STUN_ATTRIBUTE_MESSAGE_INTEGRITY
=0x0008, /* RFC5389 */
219 STUN_ATTRIBUTE_ERROR_CODE
=0x0009, /* RFC5389 */
220 STUN_ATTRIBUTE_UNKNOWN_ATTRIBUTES
=0x000A, /* RFC5389 */
221 STUN_ATTRIBUTE_REFLECTED_FROM
=0x000B, /* old RFC3489 */
222 STUN_ATTRIBUTE_CHANNEL_NUMBER
=0x000C, /* TURN-12 */
223 STUN_ATTRIBUTE_LIFETIME
=0x000D, /* TURN-12 */
224 /* 0x000E */ /* reserved (was ALTERNATE-SERVER from midcom-TURN 08 */
225 STUN_ATTRIBUTE_MAGIC_COOKIE
=0x000F, /* midcom-TURN 08 */
226 STUN_ATTRIBUTE_BANDWIDTH
=0x0010, /* TURN-04 */
227 STUN_ATTRIBUTE_DESTINATION_ADDRESS
=0x0011, /* midcom-TURN 08 */
228 STUN_ATTRIBUTE_REMOTE_ADDRESS
=0x0012, /* TURN-04 */
229 STUN_ATTRIBUTE_PEER_ADDRESS
=0x0012, /* TURN-09 */
230 STUN_ATTRIBUTE_XOR_PEER_ADDRESS
=0x0012, /* TURN-12 */
231 STUN_ATTRIBUTE_DATA
=0x0013, /* TURN-12 */
232 STUN_ATTRIBUTE_REALM
=0x0014, /* RFC5389 */
233 STUN_ATTRIBUTE_NONCE
=0x0015, /* RFC5389 */
234 STUN_ATTRIBUTE_RELAY_ADDRESS
=0x0016, /* TURN-04 */
235 STUN_ATTRIBUTE_RELAYED_ADDRESS
=0x0016, /* TURN-09 */
236 STUN_ATTRIBUTE_XOR_RELAYED_ADDRESS
=0x0016, /* TURN-12 */
237 STUN_ATTRIBUTE_REQUESTED_ADDRESS_TYPE
=0x0017, /* TURN-IPv6-05 */
238 STUN_ATTRIBUTE_REQUESTED_PORT_PROPS
=0x0018, /* TURN-04 */
239 STUN_ATTRIBUTE_REQUESTED_PROPS
=0x0018, /* TURN-09 */
240 STUN_ATTRIBUTE_EVEN_PORT
=0x0018, /* TURN-12 */
241 STUN_ATTRIBUTE_REQUESTED_TRANSPORT
=0x0019, /* TURN-12 */
242 STUN_ATTRIBUTE_DONT_FRAGMENT
=0x001A, /* TURN-12 */
243 /* 0x001B */ /* reserved */
244 /* 0x001C */ /* reserved */
245 /* 0x001D */ /* reserved */
246 /* 0x001E */ /* reserved */
247 /* 0x001F */ /* reserved */
248 STUN_ATTRIBUTE_XOR_MAPPED_ADDRESS
=0x0020, /* RFC5389 */
249 STUN_ATTRIBUTE_TIMER_VAL
=0x0021, /* TURN-04 */
250 STUN_ATTRIBUTE_REQUESTED_IP
=0x0022, /* TURN-04 */
251 STUN_ATTRIBUTE_RESERVATION_TOKEN
=0x0022, /* TURN-09 */
252 STUN_ATTRIBUTE_CONNECT_STAT
=0x0023, /* TURN-04 */
253 STUN_ATTRIBUTE_PRIORITY
=0x0024, /* ICE-19 */
254 STUN_ATTRIBUTE_USE_CANDIDATE
=0x0025, /* ICE-19 */
255 /* 0x0026 */ /* reserved */
256 /* 0x0027 */ /* reserved */
257 /* 0x0028 */ /* reserved */
258 /* 0x0029 */ /* reserved */
259 /* 0x002A-0x7fff */ /* reserved */
261 /* Optional attributes */
262 /* 0x8000-0x8021 */ /* reserved */
263 STUN_ATTRIBUTE_OPTIONS
=0x8001, /* libjingle */
264 STUN_ATTRIBUTE_SOFTWARE
=0x8022, /* RFC5389 */
265 STUN_ATTRIBUTE_ALTERNATE_SERVER
=0x8023, /* RFC5389 */
266 /* 0x8024 */ /* reserved */
267 /* 0x8025 */ /* reserved */
268 /* 0x8026 */ /* reserved */
269 /* 0x8027 */ /* reserved */
270 STUN_ATTRIBUTE_FINGERPRINT
=0x8028, /* RFC5389 */
271 STUN_ATTRIBUTE_ICE_CONTROLLED
=0x8029, /* ICE-19 */
272 STUN_ATTRIBUTE_ICE_CONTROLLING
=0x802A, /* ICE-19 */
273 /* 0x802B-0xFFFF */ /* reserved */
278 * STUN_ALL_KNOWN_ATTRIBUTES:
280 * An array containing all the currently known and defined mandatory attributes
283 /* Should be in sync with StunAttribute */
284 static const uint16_t STUN_ALL_KNOWN_ATTRIBUTES
[] =
286 STUN_ATTRIBUTE_MAPPED_ADDRESS
,
287 STUN_ATTRIBUTE_RESPONSE_ADDRESS
,
288 STUN_ATTRIBUTE_CHANGE_REQUEST
,
289 STUN_ATTRIBUTE_SOURCE_ADDRESS
,
290 STUN_ATTRIBUTE_CHANGED_ADDRESS
,
291 STUN_ATTRIBUTE_USERNAME
,
292 STUN_ATTRIBUTE_PASSWORD
,
293 STUN_ATTRIBUTE_MESSAGE_INTEGRITY
,
294 STUN_ATTRIBUTE_ERROR_CODE
,
295 STUN_ATTRIBUTE_UNKNOWN_ATTRIBUTES
,
296 STUN_ATTRIBUTE_REFLECTED_FROM
,
297 STUN_ATTRIBUTE_CHANNEL_NUMBER
,
298 STUN_ATTRIBUTE_LIFETIME
,
299 STUN_ATTRIBUTE_MAGIC_COOKIE
,
300 STUN_ATTRIBUTE_BANDWIDTH
,
301 STUN_ATTRIBUTE_DESTINATION_ADDRESS
,
302 STUN_ATTRIBUTE_REMOTE_ADDRESS
,
303 STUN_ATTRIBUTE_PEER_ADDRESS
,
304 STUN_ATTRIBUTE_XOR_PEER_ADDRESS
,
306 STUN_ATTRIBUTE_REALM
,
307 STUN_ATTRIBUTE_NONCE
,
308 STUN_ATTRIBUTE_RELAY_ADDRESS
,
309 STUN_ATTRIBUTE_RELAYED_ADDRESS
,
310 STUN_ATTRIBUTE_XOR_RELAYED_ADDRESS
,
311 STUN_ATTRIBUTE_REQUESTED_ADDRESS_TYPE
,
312 STUN_ATTRIBUTE_REQUESTED_PORT_PROPS
,
313 STUN_ATTRIBUTE_REQUESTED_PROPS
,
314 STUN_ATTRIBUTE_EVEN_PORT
,
315 STUN_ATTRIBUTE_REQUESTED_TRANSPORT
,
316 STUN_ATTRIBUTE_DONT_FRAGMENT
,
317 STUN_ATTRIBUTE_XOR_MAPPED_ADDRESS
,
318 STUN_ATTRIBUTE_TIMER_VAL
,
319 STUN_ATTRIBUTE_REQUESTED_IP
,
320 STUN_ATTRIBUTE_RESERVATION_TOKEN
,
321 STUN_ATTRIBUTE_CONNECT_STAT
,
322 STUN_ATTRIBUTE_PRIORITY
,
323 STUN_ATTRIBUTE_USE_CANDIDATE
,
329 * A type that holds a STUN transaction id.
331 typedef uint8_t StunTransactionId
[STUN_MESSAGE_TRANS_ID_LEN
];
336 * @STUN_ERROR_TRY_ALTERNATE: The ERROR-CODE value for the
337 * "Try Alternate" error as defined in RFC5389
338 * @STUN_ERROR_BAD_REQUEST: The ERROR-CODE value for the
339 * "Bad Request" error as defined in RFC5389
340 * @STUN_ERROR_UNAUTHORIZED: The ERROR-CODE value for the
341 * "Unauthorized" error as defined in RFC5389
342 * @STUN_ERROR_UNKNOWN_ATTRIBUTE: The ERROR-CODE value for the
343 * "Unknown Attribute" error as defined in RFC5389
344 * @STUN_ERROR_ALLOCATION_MISMATCH:The ERROR-CODE value for the
345 * "Allocation Mismatch" error as defined in TURN draft 12.
346 * Equivalent to the "No Binding" error defined in TURN draft 04.
347 * @STUN_ERROR_STALE_NONCE: The ERROR-CODE value for the
348 * "Stale Nonce" error as defined in RFC5389
349 * @STUN_ERROR_ACT_DST_ALREADY: The ERROR-CODE value for the
350 * "Active Destination Already Set" error as defined in TURN draft 04.
351 * @STUN_ERROR_UNSUPPORTED_FAMILY: The ERROR-CODE value for the
352 * "Address Family not Supported" error as defined in TURN IPV6 Draft 05.
353 * @STUN_ERROR_UNSUPPORTED_TRANSPORT:he ERROR-CODE value for the
354 * "Unsupported Transport Protocol" error as defined in TURN Draft 12.
355 * @STUN_ERROR_INVALID_IP: The ERROR-CODE value for the
356 * "Invalid IP Address" error as defined in TURN draft 04.
357 * @STUN_ERROR_INVALID_PORT: The ERROR-CODE value for the
358 * "Invalid Port" error as defined in TURN draft 04.
359 * @STUN_ERROR_OP_TCP_ONLY: The ERROR-CODE value for the
360 * "Operation for TCP Only" error as defined in TURN draft 04.
361 * @STUN_ERROR_CONN_ALREADY: The ERROR-CODE value for the
362 * "Connection Already Exists" error as defined in TURN draft 04.
363 * @STUN_ERROR_ALLOCATION_QUOTA_REACHED: The ERROR-CODE value for the
364 * "Allocation Quota Reached" error as defined in TURN draft 12.
365 * @STUN_ERROR_ROLE_CONFLICT:The ERROR-CODE value for the
366 * "Role Conflict" error as defined in ICE draft 19.
367 * @STUN_ERROR_SERVER_ERROR: The ERROR-CODE value for the
368 * "Server Error" error as defined in RFC5389
369 * @STUN_ERROR_SERVER_CAPACITY: The ERROR-CODE value for the
370 * "Insufficient Capacity" error as defined in TURN draft 04.
371 * @STUN_ERROR_INSUFFICIENT_CAPACITY: The ERROR-CODE value for the
372 * "Insufficient Capacity" error as defined in TURN draft 12.
373 * @STUN_ERROR_MAX: The maximum possible ERROR-CODE value as defined by RFC 5389.
375 * STUN error codes as defined by various RFCs and drafts
377 /* Should be in sync with stun_strerror() */
380 STUN_ERROR_TRY_ALTERNATE
=300, /* RFC5389 */
381 STUN_ERROR_BAD_REQUEST
=400, /* RFC5389 */
382 STUN_ERROR_UNAUTHORIZED
=401, /* RFC5389 */
383 STUN_ERROR_UNKNOWN_ATTRIBUTE
=420, /* RFC5389 */
384 STUN_ERROR_ALLOCATION_MISMATCH
=437, /* TURN-12 */
385 STUN_ERROR_STALE_NONCE
=438, /* RFC5389 */
386 STUN_ERROR_ACT_DST_ALREADY
=439, /* TURN-04 */
387 STUN_ERROR_UNSUPPORTED_FAMILY
=440, /* TURN-IPv6-05 */
388 STUN_ERROR_WRONG_CREDENTIALS
=441, /* TURN-12 */
389 STUN_ERROR_UNSUPPORTED_TRANSPORT
=442, /* TURN-12 */
390 STUN_ERROR_INVALID_IP
=443, /* TURN-04 */
391 STUN_ERROR_INVALID_PORT
=444, /* TURN-04 */
392 STUN_ERROR_OP_TCP_ONLY
=445, /* TURN-04 */
393 STUN_ERROR_CONN_ALREADY
=446, /* TURN-04 */
394 STUN_ERROR_ALLOCATION_QUOTA_REACHED
=486, /* TURN-12 */
395 STUN_ERROR_ROLE_CONFLICT
=487, /* ICE-19 */
396 STUN_ERROR_SERVER_ERROR
=500, /* RFC5389 */
397 STUN_ERROR_SERVER_CAPACITY
=507, /* TURN-04 */
398 STUN_ERROR_INSUFFICIENT_CAPACITY
=508, /* TURN-12 */
405 STUN_MESSAGE_RETURN_SUCCESS
,
406 STUN_MESSAGE_RETURN_NOT_FOUND
,
407 STUN_MESSAGE_RETURN_INVALID
,
408 STUN_MESSAGE_RETURN_NOT_ENOUGH_SPACE
,
409 STUN_MESSAGE_RETURN_UNSUPPORTED_ADDRESS
412 #include "stunagent.h"
413 #include "stunhmac.h"
414 #include "stuncrc32.h"
416 #include "stun5389.h"
418 struct stun_message_t
{
424 uint8_t long_term_key
[16];
425 bool long_term_valid
;
429 * Initializes a STUN message buffer, with no attributes.
430 * @param c STUN message class (host byte order)
431 * @param m STUN message method (host byte order)
432 * @param id 16-bytes transaction ID
433 * @return TRUE if the initialization was successful
435 bool stun_message_init (StunMessage
*msg
, StunClass c
, StunMethod m
,
436 const StunTransactionId id
);
439 * Returns the length of the stun message
440 * @param msg the STUN message
442 uint16_t stun_message_length (const StunMessage
*msg
);
445 * Looks for an attribute in a *valid* STUN message.
446 * @param msg message buffer
447 * @param type STUN attribute type (host byte order)
448 * @param palen [OUT] pointer to store the byte length of the attribute
449 * @return a pointer to the start of the attribute payload if found,
452 const void * stun_message_find (const StunMessage
* msg
, StunAttribute type
,
457 * Looks for a flag attribute within a valid STUN message.
458 * @param msg valid STUN message buffer
459 * @param type STUN attribute type (host byte order)
460 * @return 0 if flag is present, ENOENT if it is not, EINVAL if flag payload
463 StunMessageReturn
stun_message_find_flag (const StunMessage
*msg
,
467 * Extracts a 32-bits attribute from a valid STUN message.
468 * @param msg valid STUN message buffer
469 * @param type STUN attribute type (host byte order)
470 * @param pval [OUT] where to store the host byte ordered value
472 * @return 0 on success, ENOENT if attribute not found,
473 * EINVAL if attribute payload was not 32-bits.
474 * In case of error, @a *pval is not modified.
476 StunMessageReturn
stun_message_find32 (const StunMessage
*msg
,
477 StunAttribute type
, uint32_t *pval
);
480 * Extracts a 64-bits attribute from a valid STUN message.
481 * @param msg valid STUN message buffer
482 * @param type STUN attribute type (host byte order)
483 * @param pval [OUT] where to store the host byte ordered value
484 * @return 0 on success, ENOENT if attribute not found,
485 * EINVAL if attribute payload was not 64-bits.
486 * In case of error, @a *pval is not modified.
488 StunMessageReturn
stun_message_find64 (const StunMessage
*msg
,
489 StunAttribute type
, uint64_t *pval
);
492 * Extracts an UTF-8 string from a valid STUN message.
493 * @param msg valid STUN message buffer
494 * @param type STUN attribute type (host byte order)
495 * @param buf buffer to store the extracted string
496 * @param maxcp maximum number of code points allowed
497 * (@a buf should be (6*maxcp+1) bytes long)
499 * @return 0 on success, ENOENT if attribute not found, EINVAL if attribute
500 * improperly encoded, ENOBUFS if the buffer size was too small.
502 * @note A nul-byte is appended at the end.
504 StunMessageReturn
stun_message_find_string (const StunMessage
*msg
,
505 StunAttribute type
, char *buf
, size_t buflen
);
508 * Extracts a network address attribute from a valid STUN message.
509 * @param msg valid STUN message buffer
510 * @param type STUN attribute type (host byte order)
511 * @param addr [OUT] where to store the socket address
512 * @param addrlen [IN/OUT] pointer to the size of the socket address
513 * buffer upon entry, set to the length of the extracted socket
514 * address upon return,
515 * @return 0 on success, ENOENT if attribute not found,
516 * EINVAL if attribute payload size was wrong or addrlen too small,
517 * EAFNOSUPPORT if address family is unknown.
519 StunMessageReturn
stun_message_find_addr (const StunMessage
*msg
,
520 StunAttribute type
, struct sockaddr
*addr
, socklen_t
*addrlen
);
523 * Extracts an obfuscated network address attribute from a valid STUN message.
524 * @param msg valid STUN message buffer
525 * @param type STUN attribute type (host byte order)
526 * @param addr [OUT] where to store the socket address
527 * @param addrlen [IN/OUT] pointer to the size of the socket address
528 * buffer upon entry, set to the length of the extracted socket
529 * address upon return,
530 * @return 0 on success, ENOENT if attribute not found,
531 * EINVAL if attribute payload size was wrong or addrlen too small,
532 * EAFNOSUPPORT if address family is unknown.
534 StunMessageReturn
stun_message_find_xor_addr (const StunMessage
*msg
,
535 StunAttribute type
, struct sockaddr
*addr
, socklen_t
*addrlen
);
537 StunMessageReturn
stun_message_find_xor_addr_full (const StunMessage
*msg
,
538 StunAttribute type
, struct sockaddr
*addr
,
539 socklen_t
*addrlen
, uint32_t magic_cookie
);
542 StunMessageReturn
stun_message_find_error (const StunMessage
*msg
, int *code
);
544 void *stun_message_append (StunMessage
*msg
, StunAttribute type
,
547 StunMessageReturn
stun_message_append_bytes (StunMessage
*msg
,
548 StunAttribute type
, const void *data
, size_t len
);
551 * Appends an empty ("flag") attribute to a STUN message.
552 * @param msg STUN message buffer
553 * @param msize STUN message buffer size
554 * @param type attribute type (host byte order)
555 * @return 0 on success, ENOBUFS on error.
557 StunMessageReturn
stun_message_append_flag (StunMessage
*msg
,
561 * Appends an attribute consisting of a 32-bits value to a STUN message.
562 * @param msg STUN message buffer
563 * @param msize STUN message buffer size
564 * @param type attribute type (host byte order)
565 * @param value payload (host byte order)
566 * @return 0 on success, ENOBUFS on error.
568 StunMessageReturn
stun_message_append32 (StunMessage
*msg
,
569 StunAttribute type
, uint32_t value
);
572 * Appends an attribute consisting of a 64-bits value to a STUN message.
573 * @param msg STUN message buffer
574 * @param msize STUN message buffer size
575 * @param type attribute type (host byte order)
576 * @param value payload (host byte order)
577 * @return 0 on success, ENOBUFS on error.
579 StunMessageReturn
stun_message_append64 (StunMessage
*msg
,
580 StunAttribute type
, uint64_t value
);
583 * Appends an attribute from a nul-terminated string.
584 * @param msg STUN message buffer
585 * @param msize STUN message buffer size
586 * @param type attribute type (host byte order)
587 * @param str nul-terminated string
588 * @return 0 on success, ENOBUFS on error.
590 StunMessageReturn
stun_message_append_string (StunMessage
*msg
,
591 StunAttribute type
, const char *str
);
594 * Appends an attribute consisting of a network address to a STUN message.
595 * @param msg STUN message buffer
596 * @param msize STUN message buffer size
597 * @param type attribyte type (host byte order)
598 * @param addr socket address to convert into an attribute
599 * @param addrlen byte length of socket address
600 * @return 0 on success, ENOBUFS if the message buffer overflowed,
601 * EAFNOSUPPORT is the socket address family is not supported,
602 * EINVAL if the socket address length is too small w.r.t. the address family.
604 StunMessageReturn
stun_message_append_addr (StunMessage
* msg
,
605 StunAttribute type
, const struct sockaddr
*addr
, socklen_t addrlen
);
608 * Appends an attribute consisting of a xor'ed network address.
609 * @param msg STUN message buffer
610 * @param msize STUN message buffer size
611 * @param type attribyte type (host byte order)
612 * @param addr socket address to convert into an attribute
613 * @param addrlen byte length of socket address
614 * @return 0 on success, ENOBUFS if the message buffer overflowed,
615 * EAFNOSUPPORT is the socket address family is not supported,
616 * EINVAL if the socket address length is too small w.r.t. the address family.
618 StunMessageReturn
stun_message_append_xor_addr (StunMessage
* msg
,
619 StunAttribute type
, const struct sockaddr
*addr
, socklen_t addrlen
);
621 StunMessageReturn
stun_message_append_xor_addr_full (StunMessage
* msg
,
622 StunAttribute type
, const struct sockaddr
*addr
, socklen_t addrlen
,
623 uint32_t magic_cookie
);
626 * Appends an ERROR-CODE attribute.
627 * @param msg STUN message buffer
628 * @param msize STUN message buffer size
629 * @param code STUN host-byte order integer error code
630 * @return 0 on success, or ENOBUFS otherwise
632 StunMessageReturn
stun_message_append_error (StunMessage
* msg
,
635 #define STUN_MESSAGE_BUFFER_INCOMPLETE 0
636 #define STUN_MESSAGE_BUFFER_INVALID -1
639 int stun_message_validate_buffer_length (const uint8_t *msg
, size_t length
);
641 void stun_message_id (const StunMessage
*msg
, StunTransactionId id
);
643 StunClass
stun_message_get_class (const StunMessage
*msg
);
644 StunMethod
stun_message_get_method (const StunMessage
*msg
);
645 bool stun_message_has_attribute (const StunMessage
*msg
, StunAttribute type
);
647 #endif /* _STUN_MESSAGE_H */