blob: 68872837ef1bbe529c5ed1daa1ce4ffeb1e48cea [file] [log] [blame]
Tristan Matthews0a329cc2013-07-17 13:20:14 -04001/* $Id$ */
2/*
3 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
4 * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20#ifndef __PJNATH_STUN_MSG_H__
21#define __PJNATH_STUN_MSG_H__
22
23/**
24 * @file stun_msg.h
25 * @brief STUN message components.
26 */
27
28#include <pjnath/types.h>
29#include <pj/sock.h>
30
31
32PJ_BEGIN_DECL
33
34
35/* **************************************************************************/
36/**
37 * @defgroup PJNATH_STUN_MSG STUN Message Representation and Parsing
38 * @ingroup PJNATH_STUN_BASE
39 * @brief Low-level representation and parsing of STUN messages.
40 * @{
41 */
42
43
44/**
45 * STUN magic cookie.
46 */
47#define PJ_STUN_MAGIC 0x2112A442
48
49
50/**
51 * STUN method constants.
52 */
53enum pj_stun_method_e
54{
55 /**
56 * STUN Binding method as defined by RFC 3489-bis.
57 */
58 PJ_STUN_BINDING_METHOD = 1,
59
60 /**
61 * STUN Shared Secret method as defined by RFC 3489-bis.
62 */
63 PJ_STUN_SHARED_SECRET_METHOD = 2,
64
65 /**
66 * STUN/TURN Allocate method as defined by draft-ietf-behave-turn
67 */
68 PJ_STUN_ALLOCATE_METHOD = 3,
69
70 /**
71 * STUN/TURN Refresh method as defined by draft-ietf-behave-turn
72 */
73 PJ_STUN_REFRESH_METHOD = 4,
74
75 /**
76 * STUN/TURN Send indication as defined by draft-ietf-behave-turn
77 */
78 PJ_STUN_SEND_METHOD = 6,
79
80 /**
81 * STUN/TURN Data indication as defined by draft-ietf-behave-turn
82 */
83 PJ_STUN_DATA_METHOD = 7,
84
85 /**
86 * STUN/TURN CreatePermission method as defined by draft-ietf-behave-turn
87 */
88 PJ_STUN_CREATE_PERM_METHOD = 8,
89
90 /**
91 * STUN/TURN ChannelBind as defined by draft-ietf-behave-turn
92 */
93 PJ_STUN_CHANNEL_BIND_METHOD = 9,
94
95 /**
96 * All known methods.
97 */
98 PJ_STUN_METHOD_MAX
99};
100
101
102/**
103 * Retrieve the STUN method from the message-type field of the STUN
104 * message.
105 */
106#define PJ_STUN_GET_METHOD(msg_type) ((msg_type) & 0xFEEF)
107
108
109/**
110 * STUN message classes constants.
111 */
112enum pj_stun_msg_class_e
113{
114 /**
115 * This specifies that the message type is a STUN request message.
116 */
117 PJ_STUN_REQUEST_CLASS = 0,
118
119 /**
120 * This specifies that the message type is a STUN indication message.
121 */
122 PJ_STUN_INDICATION_CLASS = 1,
123
124 /**
125 * This specifies that the message type is a STUN successful response.
126 */
127 PJ_STUN_SUCCESS_CLASS = 2,
128
129 /**
130 * This specifies that the message type is a STUN error response.
131 */
132 PJ_STUN_ERROR_CLASS = 3
133};
134
135
136/**
137 * Determine if the message type is a request.
138 */
139#define PJ_STUN_IS_REQUEST(msg_type) (((msg_type) & 0x0110) == 0x0000)
140
141
142/**
143 * Determine if the message type is a successful response.
144 */
145#define PJ_STUN_IS_SUCCESS_RESPONSE(msg_type) (((msg_type) & 0x0110) == 0x0100)
146
147/**
148 * The response bit in the message type.
149 */
150#define PJ_STUN_SUCCESS_RESPONSE_BIT (0x0100)
151
152
153/**
154 * Determine if the message type is an error response.
155 */
156#define PJ_STUN_IS_ERROR_RESPONSE(msg_type) (((msg_type) & 0x0110) == 0x0110)
157
158/**
159 * The error response bit in the message type.
160 */
161#define PJ_STUN_ERROR_RESPONSE_BIT (0x0110)
162
163/**
164 * Determine if the message type is a response.
165 */
166#define PJ_STUN_IS_RESPONSE(msg_type) (((msg_type) & 0x0100) == 0x0100)
167
168
169/**
170 * Determine if the message type is an indication message.
171 */
172#define PJ_STUN_IS_INDICATION(msg_type) (((msg_type) & 0x0110) == 0x0010)
173
174/**
175 * The error response bit in the message type.
176 */
177#define PJ_STUN_INDICATION_BIT (0x0010)
178
179
180/**
181 * This enumeration describes STUN message types.
182 */
183typedef enum pj_stun_msg_type
184{
185 /**
186 * STUN BINDING request.
187 */
188 PJ_STUN_BINDING_REQUEST = 0x0001,
189
190 /**
191 * Successful response to STUN BINDING-REQUEST.
192 */
193 PJ_STUN_BINDING_RESPONSE = 0x0101,
194
195 /**
196 * Error response to STUN BINDING-REQUEST.
197 */
198 PJ_STUN_BINDING_ERROR_RESPONSE = 0x0111,
199
200 /**
201 * Binding Indication (ICE)
202 */
203 PJ_STUN_BINDING_INDICATION = 0x0011,
204
205 /**
206 * STUN SHARED-SECRET reqeust.
207 */
208 PJ_STUN_SHARED_SECRET_REQUEST = 0x0002,
209
210 /**
211 * Successful response to STUN SHARED-SECRET reqeust.
212 */
213 PJ_STUN_SHARED_SECRET_RESPONSE = 0x0102,
214
215 /**
216 * Error response to STUN SHARED-SECRET reqeust.
217 */
218 PJ_STUN_SHARED_SECRET_ERROR_RESPONSE = 0x0112,
219
220
221 /**
222 * STUN/TURN Allocate Request
223 */
224 PJ_STUN_ALLOCATE_REQUEST = 0x0003,
225
226 /**
227 * Successful response to STUN/TURN Allocate Request
228 */
229 PJ_STUN_ALLOCATE_RESPONSE = 0x0103,
230
231 /**
232 * Failure response to STUN/TURN Allocate Request
233 */
234 PJ_STUN_ALLOCATE_ERROR_RESPONSE = 0x0113,
235
236
237 /**
238 * STUN/TURN REFRESH Request
239 */
240 PJ_STUN_REFRESH_REQUEST = 0x0004,
241
242 /**
243 * Successful response to STUN REFRESH request
244 */
245 PJ_STUN_REFRESH_RESPONSE = 0x0104,
246
247 /**
248 * Error response to STUN REFRESH request.
249 */
250 PJ_STUN_REFRESH_ERROR_RESPONSE = 0x0114,
251
252
253 /**
254 * TURN Send indication
255 */
256 PJ_STUN_SEND_INDICATION = 0x0016,
257
258
259 /**
260 * TURN Data indication
261 */
262 PJ_STUN_DATA_INDICATION = 0x0017,
263
264
265 /**
266 * TURN CreatePermission request
267 */
268 PJ_STUN_CREATE_PERM_REQUEST = 0x0008,
269
270 /**
271 * TURN CreatePermission successful response.
272 */
273 PJ_STUN_CREATE_PERM_RESPONSE = 0x0108,
274
275 /**
276 * TURN CreatePermission failure response
277 */
278 PJ_STUN_CREATE_PERM_ERROR_RESPONSE = 0x0118,
279
280
281 /**
282 * STUN/TURN ChannelBind Request
283 */
284 PJ_STUN_CHANNEL_BIND_REQUEST = 0x0009,
285
286 /**
287 * Successful response to STUN ChannelBind request
288 */
289 PJ_STUN_CHANNEL_BIND_RESPONSE = 0x0109,
290
291 /**
292 * Error response to STUN ChannelBind request.
293 */
294 PJ_STUN_CHANNEL_BIND_ERROR_RESPONSE = 0x0119
295
296} pj_stun_msg_type;
297
298
299
300/**
301 * This enumeration describes STUN attribute types.
302 */
303typedef enum pj_stun_attr_type
304{
305 PJ_STUN_ATTR_MAPPED_ADDR = 0x0001,/**< MAPPED-ADDRESS. */
306 PJ_STUN_ATTR_RESPONSE_ADDR = 0x0002,/**< RESPONSE-ADDRESS (deprcatd)*/
307 PJ_STUN_ATTR_CHANGE_REQUEST = 0x0003,/**< CHANGE-REQUEST (deprecated)*/
308 PJ_STUN_ATTR_SOURCE_ADDR = 0x0004,/**< SOURCE-ADDRESS (deprecated)*/
309 PJ_STUN_ATTR_CHANGED_ADDR = 0x0005,/**< CHANGED-ADDRESS (deprecatd)*/
310 PJ_STUN_ATTR_USERNAME = 0x0006,/**< USERNAME attribute. */
311 PJ_STUN_ATTR_PASSWORD = 0x0007,/**< was PASSWORD attribute. */
312 PJ_STUN_ATTR_MESSAGE_INTEGRITY = 0x0008,/**< MESSAGE-INTEGRITY. */
313 PJ_STUN_ATTR_ERROR_CODE = 0x0009,/**< ERROR-CODE. */
314 PJ_STUN_ATTR_UNKNOWN_ATTRIBUTES = 0x000A,/**< UNKNOWN-ATTRIBUTES. */
315 PJ_STUN_ATTR_REFLECTED_FROM = 0x000B,/**< REFLECTED-FROM (deprecatd)*/
316 PJ_STUN_ATTR_CHANNEL_NUMBER = 0x000C,/**< TURN CHANNEL-NUMBER */
317 PJ_STUN_ATTR_LIFETIME = 0x000D,/**< TURN LIFETIME attr. */
318 PJ_STUN_ATTR_MAGIC_COOKIE = 0x000F,/**< MAGIC-COOKIE attr (deprec)*/
319 PJ_STUN_ATTR_BANDWIDTH = 0x0010,/**< TURN BANDWIDTH (deprec) */
320 PJ_STUN_ATTR_XOR_PEER_ADDR = 0x0012,/**< TURN XOR-PEER-ADDRESS */
321 PJ_STUN_ATTR_DATA = 0x0013,/**< DATA attribute. */
322 PJ_STUN_ATTR_REALM = 0x0014,/**< REALM attribute. */
323 PJ_STUN_ATTR_NONCE = 0x0015,/**< NONCE attribute. */
324 PJ_STUN_ATTR_XOR_RELAYED_ADDR = 0x0016,/**< TURN XOR-RELAYED-ADDRESS */
325 PJ_STUN_ATTR_REQ_ADDR_TYPE = 0x0017,/**< REQUESTED-ADDRESS-TYPE */
326 PJ_STUN_ATTR_EVEN_PORT = 0x0018,/**< TURN EVEN-PORT */
327 PJ_STUN_ATTR_REQ_TRANSPORT = 0x0019,/**< TURN REQUESTED-TRANSPORT */
328 PJ_STUN_ATTR_DONT_FRAGMENT = 0x001A,/**< TURN DONT-FRAGMENT */
329 PJ_STUN_ATTR_XOR_MAPPED_ADDR = 0x0020,/**< XOR-MAPPED-ADDRESS */
330 PJ_STUN_ATTR_TIMER_VAL = 0x0021,/**< TIMER-VAL attribute. */
331 PJ_STUN_ATTR_RESERVATION_TOKEN = 0x0022,/**< TURN RESERVATION-TOKEN */
332 PJ_STUN_ATTR_XOR_REFLECTED_FROM = 0x0023,/**< XOR-REFLECTED-FROM */
333 PJ_STUN_ATTR_PRIORITY = 0x0024,/**< PRIORITY */
334 PJ_STUN_ATTR_USE_CANDIDATE = 0x0025,/**< USE-CANDIDATE */
335 PJ_STUN_ATTR_ICMP = 0x0030,/**< ICMP (TURN) */
336
337 PJ_STUN_ATTR_END_MANDATORY_ATTR,
338
339 PJ_STUN_ATTR_START_EXTENDED_ATTR= 0x8021,
340
341 PJ_STUN_ATTR_SOFTWARE = 0x8022,/**< SOFTWARE attribute. */
342 PJ_STUN_ATTR_ALTERNATE_SERVER = 0x8023,/**< ALTERNATE-SERVER. */
343 PJ_STUN_ATTR_REFRESH_INTERVAL = 0x8024,/**< REFRESH-INTERVAL. */
344 PJ_STUN_ATTR_FINGERPRINT = 0x8028,/**< FINGERPRINT attribute. */
345 PJ_STUN_ATTR_ICE_CONTROLLED = 0x8029,/**< ICE-CCONTROLLED attribute.*/
346 PJ_STUN_ATTR_ICE_CONTROLLING = 0x802a,/**< ICE-CCONTROLLING attribute*/
347
348 PJ_STUN_ATTR_END_EXTENDED_ATTR
349
350} pj_stun_attr_type;
351
352
353/**
354 * STUN error codes, which goes into STUN ERROR-CODE attribute.
355 */
356typedef enum pj_stun_status
357{
358 PJ_STUN_SC_TRY_ALTERNATE = 300, /**< Try Alternate */
359 PJ_STUN_SC_BAD_REQUEST = 400, /**< Bad Request */
360 PJ_STUN_SC_UNAUTHORIZED = 401, /**< Unauthorized */
361 PJ_STUN_SC_FORBIDDEN = 403, /**< Forbidden (TURN) */
362 PJ_STUN_SC_UNKNOWN_ATTRIBUTE = 420, /**< Unknown Attribute */
363#if 0
364 /* These were obsolete in recent rfc3489bis */
365 //PJ_STUN_SC_STALE_CREDENTIALS = 430, /**< Stale Credentials */
366 //PJ_STUN_SC_INTEGRITY_CHECK_FAILURE= 431, /**< Integrity Chk Fail */
367 //PJ_STUN_SC_MISSING_USERNAME = 432, /**< Missing Username */
368 //PJ_STUN_SC_USE_TLS = 433, /**< Use TLS */
369 //PJ_STUN_SC_MISSING_REALM = 434, /**< Missing Realm */
370 //PJ_STUN_SC_MISSING_NONCE = 435, /**< Missing Nonce */
371 //PJ_STUN_SC_UNKNOWN_USERNAME = 436, /**< Unknown Username */
372#endif
373 PJ_STUN_SC_ALLOCATION_MISMATCH = 437, /**< TURN Alloc Mismatch */
374 PJ_STUN_SC_STALE_NONCE = 438, /**< Stale Nonce */
375 PJ_STUN_SC_TRANSITIONING = 439, /**< Transitioning. */
376 PJ_STUN_SC_WRONG_CREDENTIALS = 441, /**< TURN Wrong Credentials */
377 PJ_STUN_SC_UNSUPP_TRANSPORT_PROTO = 442, /**< Unsupported Transport or
378 Protocol (TURN) */
379 PJ_STUN_SC_OPER_TCP_ONLY = 445, /**< Operation for TCP Only */
380 PJ_STUN_SC_CONNECTION_FAILURE = 446, /**< Connection Failure */
381 PJ_STUN_SC_CONNECTION_TIMEOUT = 447, /**< Connection Timeout */
382 PJ_STUN_SC_ALLOCATION_QUOTA_REACHED = 486, /**< Allocation Quota Reached
383 (TURN) */
384 PJ_STUN_SC_ROLE_CONFLICT = 487, /**< Role Conflict */
385 PJ_STUN_SC_SERVER_ERROR = 500, /**< Server Error */
386 PJ_STUN_SC_INSUFFICIENT_CAPACITY = 508, /**< Insufficient Capacity
387 (TURN) */
388 PJ_STUN_SC_GLOBAL_FAILURE = 600 /**< Global Failure */
389} pj_stun_status;
390
391
392/**
393 * This structure describes STUN message header. A STUN message has the
394 * following format:
395 *
396 * \verbatim
397
398 0 1 2 3
399 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
400 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
401 |0 0| STUN Message Type | Message Length |
402 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
403 | Magic Cookie |
404 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
405 |
406 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
407 Transaction ID
408 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
409 |
410 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
411
412 \endverbatim
413 */
414typedef struct pj_stun_msg_hdr
415{
416 /**
417 * STUN message type, which the first two bits must be zeroes.
418 */
419 pj_uint16_t type;
420
421 /**
422 * The message length is the size, in bytes, of the message not
423 * including the 20 byte STUN header.
424 */
425 pj_uint16_t length;
426
427 /**
428 * The magic cookie is a fixed value, 0x2112A442 (PJ_STUN_MAGIC constant).
429 * In the previous version of this specification [15] this field was part
430 * of the transaction ID.
431 */
432 pj_uint32_t magic;
433
434 /**
435 * The transaction ID is a 96 bit identifier. STUN transactions are
436 * identified by their unique 96-bit transaction ID. For request/
437 * response transactions, the transaction ID is chosen by the STUN
438 * client and MUST be unique for each new STUN transaction generated by
439 * that STUN client. The transaction ID MUST be uniformly and randomly
440 * distributed between 0 and 2**96 - 1.
441 */
442 pj_uint8_t tsx_id[12];
443
444} pj_stun_msg_hdr;
445
446
447/**
448 * This structre describes STUN attribute header. Each attribute is
449 * TLV encoded, with a 16 bit type, 16 bit length, and variable value.
450 * Each STUN attribute ends on a 32 bit boundary:
451 *
452 * \verbatim
453
454 0 1 2 3
455 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
456 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
457 | Type | Length |
458 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
459
460 \endverbatim
461 */
462typedef struct pj_stun_attr_hdr
463{
464 /**
465 * STUN attribute type.
466 */
467 pj_uint16_t type;
468
469 /**
470 * The Length refers to the length of the actual useful content of the
471 * Value portion of the attribute, measured in bytes. The value
472 * in the Length field refers to the length of the Value part of the
473 * attribute prior to padding - i.e., the useful content.
474 */
475 pj_uint16_t length;
476
477} pj_stun_attr_hdr;
478
479
480/**
481 * This structure describes STUN generic IP address attribute, used for
482 * example to represent STUN MAPPED-ADDRESS attribute.
483 *
484 * The generic IP address attribute indicates the transport address.
485 * It consists of an eight bit address family, and a sixteen bit port,
486 * followed by a fixed length value representing the IP address. If the
487 * address family is IPv4, the address is 32 bits, in network byte
488 * order. If the address family is IPv6, the address is 128 bits in
489 * network byte order.
490 *
491 * The format of the generic IP address attribute is:
492 *
493 * \verbatim
494
495 0 1 2 3
496 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
497 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
498 |x x x x x x x x| Family | Port |
499 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
500 | Address (variable)
501 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
502
503 \endverbatim
504 */
505typedef struct pj_stun_sockaddr_attr
506{
507 /**
508 * Standard STUN attribute header.
509 */
510 pj_stun_attr_hdr hdr;
511
512 /**
513 * Flag to indicate whether this attribute should be sent in XOR-ed
514 * format, or has been received in XOR-ed format.
515 */
516 pj_bool_t xor_ed;
517
518 /**
519 * The socket address
520 */
521 pj_sockaddr sockaddr;
522
523} pj_stun_sockaddr_attr;
524
525
526/**
527 * This structure represents a generic STUN attributes with no payload,
528 * and it is used for example by ICE USE-CANDIDATE attribute.
529 */
530typedef struct pj_stun_empty_attr
531{
532 /**
533 * Standard STUN attribute header.
534 */
535 pj_stun_attr_hdr hdr;
536
537} pj_stun_empty_attr;
538
539
540/**
541 * This structure represents generic STUN string attributes, such as STUN
542 * USERNAME, PASSWORD, SOFTWARE, REALM, and NONCE attributes.
543 */
544typedef struct pj_stun_string_attr
545{
546 /**
547 * Standard STUN attribute header.
548 */
549 pj_stun_attr_hdr hdr;
550
551 /**
552 * The string value.
553 */
554 pj_str_t value;
555
556} pj_stun_string_attr;
557
558
559/**
560 * This structure represents a generic STUN attributes with 32bit (unsigned)
561 * integer value, such as STUN FINGERPRINT and REFRESH-INTERVAL attributes.
562 */
563typedef struct pj_stun_uint_attr
564{
565 /**
566 * Standard STUN attribute header.
567 */
568 pj_stun_attr_hdr hdr;
569
570 /**
571 * The 32bit value, in host byte order.
572 */
573 pj_uint32_t value;
574
575} pj_stun_uint_attr;
576
577
578/**
579 * This structure represents a generic STUN attributes with 64bit (unsigned)
580 * integer value, such as ICE-CONTROLLED and ICE-CONTROLLING attributes.
581 */
582typedef struct pj_stun_uint64_attr
583{
584 /**
585 * Standard STUN attribute header.
586 */
587 pj_stun_attr_hdr hdr;
588
589 /**
590 * The 64bit value, in host byte order, represented with pj_timestamp.
591 */
592 pj_timestamp value;
593
594} pj_stun_uint64_attr;
595
596
597/**
598 * This structure represents generic STUN attributes to hold a raw binary
599 * data.
600 */
601typedef struct pj_stun_binary_attr
602{
603 /**
604 * Standard STUN attribute header.
605 */
606 pj_stun_attr_hdr hdr;
607
608 /**
609 * Special signature to indicate that this is a valid attribute even
610 * though we don't have meta-data to describe this attribute.
611 */
612 pj_uint32_t magic;
613
614 /**
615 * Length of the data.
616 */
617 unsigned length;
618
619 /**
620 * The raw data.
621 */
622 pj_uint8_t *data;
623
624} pj_stun_binary_attr;
625
626
627/**
628 * This structure describes STUN MESSAGE-INTEGRITY attribute.
629 * The MESSAGE-INTEGRITY attribute contains an HMAC-SHA1 [10] of the
630 * STUN message. The MESSAGE-INTEGRITY attribute can be present in any
631 * STUN message type. Since it uses the SHA1 hash, the HMAC will be 20
632 * bytes.
633 */
634typedef struct pj_stun_msgint_attr
635{
636 /**
637 * Standard STUN attribute header.
638 */
639 pj_stun_attr_hdr hdr;
640
641 /**
642 * The 20 bytes hmac value.
643 */
644 pj_uint8_t hmac[20];
645
646} pj_stun_msgint_attr;
647
648
649/**
650 * This structure describes STUN FINGERPRINT attribute. The FINGERPRINT
651 * attribute can be present in all STUN messages. It is computed as
652 * the CRC-32 of the STUN message up to (but excluding) the FINGERPRINT
653 * attribute itself, xor-d with the 32 bit value 0x5354554e
654 */
655typedef struct pj_stun_uint_attr pj_stun_fingerprint_attr;
656
657
658/**
659 * This structure represents STUN ERROR-CODE attribute. The ERROR-CODE
660 * attribute is present in the Binding Error Response and Shared Secret
661 * Error Response. It is a numeric value in the range of 100 to 699
662 * plus a textual reason phrase encoded in UTF-8
663 *
664 * \verbatim
665
666 0 1 2 3
667 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
668 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
669 | 0 |Class| Number |
670 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
671 | Reason Phrase (variable) ..
672 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
673
674 \endverbatim
675 */
676typedef struct pj_stun_errcode_attr
677{
678 /**
679 * Standard STUN attribute header.
680 */
681 pj_stun_attr_hdr hdr;
682
683 /**
684 * STUN error code.
685 */
686 int err_code;
687
688 /**
689 * The reason phrase.
690 */
691 pj_str_t reason;
692
693} pj_stun_errcode_attr;
694
695
696/**
697 * This describes STUN REALM attribute.
698 * The REALM attribute is present in requests and responses. It
699 * contains text which meets the grammar for "realm" as described in RFC
700 * 3261 [11], and will thus contain a quoted string (including the
701 * quotes).
702 */
703typedef struct pj_stun_string_attr pj_stun_realm_attr;
704
705
706/**
707 * This describes STUN NONCE attribute.
708 * The NONCE attribute is present in requests and in error responses.
709 * It contains a sequence of qdtext or quoted-pair, which are defined in
710 * RFC 3261 [11]. See RFC 2617 [7] for guidance on selection of nonce
711 * values in a server.
712 */
713typedef struct pj_stun_string_attr pj_stun_nonce_attr;
714
715
716/**
717 * This describes STUN UNKNOWN-ATTRIBUTES attribute.
718 * The UNKNOWN-ATTRIBUTES attribute is present only in an error response
719 * when the response code in the ERROR-CODE attribute is 420.
720 * The attribute contains a list of 16 bit values, each of which
721 * represents an attribute type that was not understood by the server.
722 * If the number of unknown attributes is an odd number, one of the
723 * attributes MUST be repeated in the list, so that the total length of
724 * the list is a multiple of 4 bytes.
725 */
726typedef struct pj_stun_unknown_attr
727{
728 /**
729 * Standard STUN attribute header.
730 */
731 pj_stun_attr_hdr hdr;
732
733 /**
734 * Number of unknown attributes in the array.
735 */
736 unsigned attr_count;
737
738 /**
739 * Array of unknown attribute IDs.
740 */
741 pj_uint16_t attrs[PJ_STUN_MAX_ATTR];
742
743} pj_stun_unknown_attr;
744
745
746/**
747 * This structure describes STUN MAPPED-ADDRESS attribute.
748 * The MAPPED-ADDRESS attribute indicates the mapped transport address.
749 */
750typedef struct pj_stun_sockaddr_attr pj_stun_mapped_addr_attr;
751
752
753/**
754 * This describes STUN XOR-MAPPED-ADDRESS attribute (which has the same
755 * format as STUN MAPPED-ADDRESS attribute).
756 * The XOR-MAPPED-ADDRESS attribute is present in responses. It
757 * provides the same information that would present in the MAPPED-
758 * ADDRESS attribute but because the NAT's public IP address is
759 * obfuscated through the XOR function, STUN messages are able to pass
760 * through NATs which would otherwise interfere with STUN.
761 */
762typedef struct pj_stun_sockaddr_attr pj_stun_xor_mapped_addr_attr;
763
764
765/**
766 * This describes STUN SOFTWARE attribute.
767 * The SOFTWARE attribute contains a textual description of the software
768 * being used by the agent sending the message. It is used by clients
769 * and servers. Its value SHOULD include manufacturer and version
770 * number. */
771typedef struct pj_stun_string_attr pj_stun_software_attr;
772
773
774/**
775 * This describes STUN ALTERNATE-SERVER attribute.
776 * The alternate server represents an alternate transport address for a
777 * different STUN server to try. It is encoded in the same way as
778 * MAPPED-ADDRESS.
779 */
780typedef struct pj_stun_sockaddr_attr pj_stun_alt_server_attr;
781
782
783/**
784 * This describes STUN REFRESH-INTERVAL attribute.
785 * The REFRESH-INTERVAL indicates the number of milliseconds that the
786 * server suggests the client should use between refreshes of the NAT
787 * bindings between the client and server.
788 */
789typedef struct pj_stun_uint_attr pj_stun_refresh_interval_attr;
790
791
792/**
793 * This structure describes STUN RESPONSE-ADDRESS attribute.
794 * The RESPONSE-ADDRESS attribute indicates where the response to a
795 * Binding Request should be sent. Its syntax is identical to MAPPED-
796 * ADDRESS.
797 *
798 * Note that the usage of this attribute has been deprecated by the
799 * RFC 3489-bis standard.
800 */
801typedef struct pj_stun_sockaddr_attr pj_stun_response_addr_attr;
802
803
804/**
805 * This structure describes STUN CHANGED-ADDRESS attribute.
806 * The CHANGED-ADDRESS attribute indicates the IP address and port where
807 * responses would have been sent from if the "change IP" and "change
808 * port" flags had been set in the CHANGE-REQUEST attribute of the
809 * Binding Request. The attribute is always present in a Binding
810 * Response, independent of the value of the flags. Its syntax is
811 * identical to MAPPED-ADDRESS.
812 *
813 * Note that the usage of this attribute has been deprecated by the
814 * RFC 3489-bis standard.
815 */
816typedef struct pj_stun_sockaddr_attr pj_stun_changed_addr_attr;
817
818
819/**
820 * This structure describes STUN CHANGE-REQUEST attribute.
821 * The CHANGE-REQUEST attribute is used by the client to request that
822 * the server use a different address and/or port when sending the
823 * response.
824 *
825 * Bit 29 of the value is the "change IP" flag. If true, it requests
826 * the server to send the Binding Response with a different IP address
827 * than the one the Binding Request was received on.
828 *
829 * Bit 30 of the value is the "change port" flag. If true, it requests
830 * the server to send the Binding Response with a different port than
831 * the one the Binding Request was received on.
832 *
833 * Note that the usage of this attribute has been deprecated by the
834 * RFC 3489-bis standard.
835 */
836typedef struct pj_stun_uint_attr pj_stun_change_request_attr;
837
838/**
839 * This structure describes STUN SOURCE-ADDRESS attribute.
840 * The SOURCE-ADDRESS attribute is present in Binding Responses. It
841 * indicates the source IP address and port that the server is sending
842 * the response from. Its syntax is identical to that of MAPPED-
843 * ADDRESS.
844 *
845 * Note that the usage of this attribute has been deprecated by the
846 * RFC 3489-bis standard.
847 */
848typedef struct pj_stun_sockaddr_attr pj_stun_src_addr_attr;
849
850
851/**
852 * This describes the STUN REFLECTED-FROM attribute.
853 * The REFLECTED-FROM attribute is present only in Binding Responses,
854 * when the Binding Request contained a RESPONSE-ADDRESS attribute. The
855 * attribute contains the identity (in terms of IP address) of the
856 * source where the request came from. Its purpose is to provide
857 * traceability, so that a STUN server cannot be used as a reflector for
858 * denial-of-service attacks.
859 */
860typedef struct pj_stun_sockaddr_attr pj_stun_reflected_from_attr;
861
862
863/**
864 * This describes STUN USERNAME attribute.
865 * The USERNAME attribute is used for message integrity. It identifies
866 * the shared secret used in the message integrity check. Consequently,
867 * the USERNAME MUST be included in any request that contains the
868 * MESSAGE-INTEGRITY attribute.
869 */
870typedef struct pj_stun_string_attr pj_stun_username_attr;
871
872
873/**
874 * This describes STUN PASSWORD attribute.
875 * If the message type is Shared Secret Response it MUST include the
876 * PASSWORD attribute.
877 */
878typedef struct pj_stun_string_attr pj_stun_password_attr;
879
880
881/**
882 * This describes TURN CHANNEL-NUMBER attribute. In this library,
883 * this attribute is represented with 32bit integer. Application may
884 * use #PJ_STUN_GET_CH_NB() and #PJ_STUN_SET_CH_NB() to extract/set
885 * channel number value from the 32bit integral value.
886 *
887 * The CHANNEL-NUMBER attribute contains the number of the channel.
888 * It is a 16-bit unsigned integer, followed by a two-octet RFFU field
889 * which MUST be set to 0 on transmission and ignored on reception.
890
891 \verbatim
892 0 1 2 3
893 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
894 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
895 | Channel Number | RFFU |
896 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
897 \endverbatim
898 */
899typedef struct pj_stun_uint_attr pj_stun_channel_number_attr;
900
901/**
902 * Get 16bit channel number from 32bit integral value.
903 * Note that uint32 attributes are always stored in host byte order
904 * after they have been parsed from the PDU, so no need to do ntohs()
905 * here.
906 */
907#define PJ_STUN_GET_CH_NB(u32) ((pj_uint16_t)(u32>>16))
908
909/**
910 * Convert 16bit channel number into 32bit integral value.
911 * Note that uint32 attributes will be converted to network byte order
912 * when the attribute is written to packet, so no need to do htons()
913 * here.
914 */
915#define PJ_STUN_SET_CH_NB(chnum) (((pj_uint32_t)chnum) << 16)
916
917
918/**
919 * This describes STUN LIFETIME attribute.
920 * The lifetime attribute represents the duration for which the server
921 * will maintain an allocation in the absence of data traffic either
922 * from or to the client. It is a 32 bit value representing the number
923 * of seconds remaining until expiration.
924 */
925typedef struct pj_stun_uint_attr pj_stun_lifetime_attr;
926
927
928/**
929 * This describes STUN BANDWIDTH attribute.
930 * The bandwidth attribute represents the peak bandwidth, measured in
931 * kbits per second, that the client expects to use on the binding. The
932 * value represents the sum in the receive and send directions.
933 */
934typedef struct pj_stun_uint_attr pj_stun_bandwidth_attr;
935
936
937/**
938 * This describes the STUN XOR-PEER-ADDRESS attribute.
939 * The XOR-PEER-ADDRESS specifies the address and port of the peer as seen
940 * from the TURN server. It is encoded in the same way as XOR-MAPPED-
941 * ADDRESS.
942 */
943typedef struct pj_stun_sockaddr_attr pj_stun_xor_peer_addr_attr;
944
945
946/**
947 * This describes the STUN DATA attribute.
948 * The DATA attribute is present in Send Indications and Data
949 * Indications. It contains raw payload data that is to be sent (in the
950 * case of a Send Request) or was received (in the case of a Data
951 * Indication)..
952 */
953typedef struct pj_stun_binary_attr pj_stun_data_attr;
954
955
956/**
957 * This describes the STUN XOR-RELAYED-ADDRESS attribute. The
958 * XOR-RELAYED-ADDRESS is present in Allocate responses. It specifies the
959 * address and port that the server allocated to the client. It is
960 * encoded in the same way as XOR-MAPPED-ADDRESS.
961 */
962typedef struct pj_stun_sockaddr_attr pj_stun_xor_relayed_addr_attr;
963
964
965/**
966 * This describes the REQUESTED-ADDRESS-TYPE attribute.
967 * The REQUESTED-ADDRESS-TYPE attribute is used by clients to request
968 * the allocation of a specific address type from a server. The
969 * following is the format of the REQUESTED-ADDRESS-TYPE attribute.
970
971 \verbatim
972
973 0 1 2 3
974 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
975 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
976 | Family | Reserved |
977 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
978
979 \endverbatim
980 */
981typedef struct pj_stun_uint_attr pj_stun_req_addr_type_attr;
982
983
984/**
985 * This describes the TURN REQUESTED-TRANSPORT attribute, encoded in
986 * STUN generic integer attribute.
987 *
988 * This attribute allows the client to request that the port in the
989 * relayed-transport-address be even, and (optionally) that the server
990 * reserve the next-higher port number. The attribute is 8 bits long.
991 * Its format is:
992
993\verbatim
994 0
995 0 1 2 3 4 5 6 7
996 +-+-+-+-+-+-+-+-+
997 |R| RFFU |
998 +-+-+-+-+-+-+-+-+
999
1000\endverbatim
1001
1002 * The attribute contains a single 1-bit flag:
1003 *
1004 * R: If 1, the server is requested to reserve the next higher port
1005 * number (on the same IP address) for a subsequent allocation. If
1006 * 0, no such reservation is requested.
1007 *
1008 * The other 7 bits of the attribute must be set to zero on transmission
1009 * and ignored on reception.
1010 */
1011typedef struct pj_stun_uint_attr pj_stun_even_port_attr;
1012
1013
1014/**
1015 * This describes the TURN REQUESTED-TRANSPORT attribute, encoded in
1016 * STUN generic integer attribute.
1017 *
1018 * This attribute is used by the client to request a specific transport
1019 * protocol for the allocated transport address. It has the following
1020 * format:
1021
1022 \verbatim
1023
1024 0 1 2 3
1025 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
1026 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1027 | Protocol | RFFU |
1028 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1029
1030
1031 \endverbatim
1032
1033 * The Protocol field specifies the desired protocol. The codepoints
1034 * used in this field are taken from those allowed in the Protocol field
1035 * in the IPv4 header and the NextHeader field in the IPv6 header
1036 * [Protocol-Numbers]. This specification only allows the use of
1037 * codepoint 17 (User Datagram Protocol).
1038 *
1039 * The RFFU field is set to zero on transmission and ignored on
1040 * receiption. It is reserved for future uses.
1041 */
1042typedef struct pj_stun_uint_attr pj_stun_req_transport_attr;
1043
1044/**
1045 * Get protocol value from 32bit TURN REQUESTED-TRANSPORT attribute.
1046 */
1047#define PJ_STUN_GET_RT_PROTO(u32) (u32 >> 24)
1048
1049/**
1050 * Convert protocol value to be placed in 32bit TURN REQUESTED-TRANSPORT
1051 * attribute.
1052 */
1053#define PJ_STUN_SET_RT_PROTO(proto) (((pj_uint32_t)(proto)) << 24)
1054
1055
1056/**
1057 * This describes the TURN DONT-FRAGMENT attribute.
1058 *
1059 * This attribute is used by the client to request that the server set
1060 * the DF (Don't Fragment) bit in the IP header when relaying the
1061 * application data onward to the peer. This attribute has no value
1062 * part and thus the attribute length field is 0.
1063 */
1064typedef struct pj_stun_empty_attr pj_stun_dont_fragment_attr;
1065
1066
1067/**
1068 * This describes the TURN RESERVATION-TOKEN attribute.
1069 * The RESERVATION-TOKEN attribute contains a token that uniquely
1070 * identifies a relayed transport address being held in reserve by the
1071 * server. The server includes this attribute in a success response to
1072 * tell the client about the token, and the client includes this
1073 * attribute in a subsequent Allocate request to request the server use
1074 * that relayed transport address for the allocation.
1075 *
1076 * The attribute value is a 64-bit-long field containing the token
1077 * value.
1078 */
1079typedef struct pj_stun_uint64_attr pj_stun_res_token_attr;
1080
1081/**
1082 * This describes the XOR-REFLECTED-FROM attribute, as described by
1083 * draft-macdonald-behave-nat-behavior-discovery-00.
1084 * The XOR-REFLECTED-FROM attribute is used in place of the REFLECTED-
1085 * FROM attribute. It provides the same information, but because the
1086 * NAT's public address is obfuscated through the XOR function, It can
1087 * pass through a NAT that would otherwise attempt to translate it to
1088 * the private network address. XOR-REFLECTED-FROM has identical syntax
1089 * to XOR-MAPPED-ADDRESS.
1090 */
1091typedef struct pj_stun_sockaddr_attr pj_stun_xor_reflected_from_attr;
1092
1093/**
1094 * This describes the PRIORITY attribute from draft-ietf-mmusic-ice-13.
1095 * The PRIORITY attribute indicates the priority that is to be
1096 * associated with a peer reflexive candidate, should one be discovered
1097 * by this check. It is a 32 bit unsigned integer, and has an attribute
1098 * type of 0x0024.
1099 */
1100typedef struct pj_stun_uint_attr pj_stun_priority_attr;
1101
1102/**
1103 * This describes the USE-CANDIDATE attribute from draft-ietf-mmusic-ice-13.
1104 * The USE-CANDIDATE attribute indicates that the candidate pair
1105 * resulting from this check should be used for transmission of media.
1106 * The attribute has no content (the Length field of the attribute is
1107 * zero); it serves as a flag.
1108 */
1109typedef struct pj_stun_empty_attr pj_stun_use_candidate_attr;
1110
1111/**
1112 * This describes the STUN TIMER-VAL attribute.
1113 * The TIMER-VAL attribute is used only in conjunction with the Set
1114 * Active Destination response. It conveys from the server, to the
1115 * client, the value of the timer used in the server state machine.
1116 */
1117typedef struct pj_stun_uint_attr pj_stun_timer_val_attr;
1118
1119/**
1120 * This describes ICE-CONTROLLING attribute.
1121 */
1122typedef struct pj_stun_uint64_attr pj_stun_ice_controlling_attr;
1123
1124/**
1125 * This describes ICE-CONTROLLED attribute.
1126 */
1127typedef struct pj_stun_uint64_attr pj_stun_ice_controlled_attr;
1128
1129/**
1130 * This describes TURN ICMP attribute
1131 */
1132typedef struct pj_stun_uint_attr pj_stun_icmp_attr;
1133
1134/**
1135 * This structure describes a parsed STUN message. All integral fields
1136 * in this structure (including IP addresses) will be in the host
1137 * byte order.
1138 */
1139typedef struct pj_stun_msg
1140{
1141 /**
1142 * STUN message header.
1143 */
1144 pj_stun_msg_hdr hdr;
1145
1146 /**
1147 * Number of attributes in the STUN message.
1148 */
1149 unsigned attr_count;
1150
1151 /**
1152 * Array of STUN attributes.
1153 */
1154 pj_stun_attr_hdr *attr[PJ_STUN_MAX_ATTR];
1155
1156} pj_stun_msg;
1157
1158
1159/** STUN decoding options */
1160enum pj_stun_decode_options
1161{
1162 /**
1163 * Tell the decoder that the message was received from datagram
1164 * oriented transport (such as UDP).
1165 */
1166 PJ_STUN_IS_DATAGRAM = 1,
1167
1168 /**
1169 * Tell pj_stun_msg_decode() to check the validity of the STUN
1170 * message by calling pj_stun_msg_check() before starting to
1171 * decode the packet.
1172 */
1173 PJ_STUN_CHECK_PACKET = 2,
1174
1175 /**
1176 * This option current is only valid for #pj_stun_session_on_rx_pkt().
1177 * When specified, it tells the session NOT to authenticate the
1178 * message.
1179 */
1180 PJ_STUN_NO_AUTHENTICATE = 4,
1181
1182 /**
1183 * Disable FINGERPRINT verification. This option can be used when calling
1184 * #pj_stun_msg_check() and #pj_stun_msg_decode() to disable the
1185 * verification of FINGERPRINT, for example when the STUN usage says when
1186 * FINGERPRINT mechanism shall not be used.
1187 */
1188 PJ_STUN_NO_FINGERPRINT_CHECK = 8
1189};
1190
1191
1192/**
1193 * Get STUN message method name.
1194 *
1195 * @param msg_type The STUN message type (in host byte order)
1196 *
1197 * @return The STUN message method name string.
1198 */
1199PJ_DECL(const char*) pj_stun_get_method_name(unsigned msg_type);
1200
1201
1202/**
1203 * Get STUN message class name.
1204 *
1205 * @param msg_type The STUN message type (in host byte order)
1206 *
1207 * @return The STUN message class name string.
1208 */
1209PJ_DECL(const char*) pj_stun_get_class_name(unsigned msg_type);
1210
1211
1212/**
1213 * Get STUN attribute name.
1214 *
1215 * @return attr_type The STUN attribute type (in host byte order).
1216 *
1217 * @return The STUN attribute type name string.
1218 */
1219PJ_DECL(const char*) pj_stun_get_attr_name(unsigned attr_type);
1220
1221
1222/**
1223 * Get STUN standard reason phrase for the specified error code.
1224 *
1225 * @param err_code The STUN error code.
1226 *
1227 * @return The STUN error reason phrase.
1228 */
1229PJ_DECL(pj_str_t) pj_stun_get_err_reason(int err_code);
1230
1231
1232/**
1233 * Internal: set the padding character for string attribute.
1234 * The default padding character is PJ_STUN_STRING_ATTR_PAD_CHR.
1235 *
1236 * @return The previous padding character.
1237 */
1238PJ_DECL(int) pj_stun_set_padding_char(int chr);
1239
1240
1241/**
1242 * Initialize a generic STUN message.
1243 *
1244 * @param msg The message structure to be initialized.
1245 * @param msg_type The 14bit message type (see pj_stun_msg_type
1246 * constants).
1247 * @param magic Magic value to be put to the mesage; for requests,
1248 * the value normally should be PJ_STUN_MAGIC.
1249 * @param tsx_id Optional transaction ID, or NULL to let the
1250 * function generates a random transaction ID.
1251 *
1252 * @return PJ_SUCCESS on success.
1253 */
1254PJ_DECL(pj_status_t) pj_stun_msg_init(pj_stun_msg *msg,
1255 unsigned msg_type,
1256 pj_uint32_t magic,
1257 const pj_uint8_t tsx_id[12]);
1258
1259/**
1260 * Create a generic STUN message.
1261 *
1262 * @param pool Pool to create the STUN message.
1263 * @param msg_type The 14bit message type.
1264 * @param magic Magic value to be put to the mesage; for requests,
1265 * the value should be PJ_STUN_MAGIC.
1266 * @param tsx_id Optional transaction ID, or NULL to let the
1267 * function generates a random transaction ID.
1268 * @param p_msg Pointer to receive the message.
1269 *
1270 * @return PJ_SUCCESS on success.
1271 */
1272PJ_DECL(pj_status_t) pj_stun_msg_create(pj_pool_t *pool,
1273 unsigned msg_type,
1274 pj_uint32_t magic,
1275 const pj_uint8_t tsx_id[12],
1276 pj_stun_msg **p_msg);
1277
1278/**
1279 * Clone a STUN message with all of its attributes.
1280 *
1281 * @param pool Pool to allocate memory for the new message.
1282 * @param msg The message to be cloned.
1283 *
1284 * @return The duplicate message.
1285 */
1286PJ_DECL(pj_stun_msg*) pj_stun_msg_clone(pj_pool_t *pool,
1287 const pj_stun_msg *msg);
1288
1289/**
1290 * Create STUN response message.
1291 *
1292 * @param pool Pool to create the mesage.
1293 * @param req_msg The request message.
1294 * @param err_code STUN error code. If this value is not zero,
1295 * then error response will be created, otherwise
1296 * successful response will be created.
1297 * @param err_msg Optional error message to explain err_code.
1298 * If this value is NULL and err_code is not zero,
1299 * the error string will be taken from the default
1300 * STUN error message.
1301 * @param p_response Pointer to receive the response.
1302 *
1303 * @return PJ_SUCCESS on success, or the appropriate error.
1304 */
1305PJ_DECL(pj_status_t) pj_stun_msg_create_response(pj_pool_t *pool,
1306 const pj_stun_msg *req_msg,
1307 unsigned err_code,
1308 const pj_str_t *err_msg,
1309 pj_stun_msg **p_response);
1310
1311
1312/**
1313 * Add STUN attribute to STUN message.
1314 *
1315 * @param msg The STUN message.
1316 * @param attr The STUN attribute to be added to the message.
1317 *
1318 * @return PJ_SUCCESS on success, or PJ_ETOOMANY if there are
1319 * already too many attributes in the message.
1320 */
1321PJ_DECL(pj_status_t) pj_stun_msg_add_attr(pj_stun_msg *msg,
1322 pj_stun_attr_hdr *attr);
1323
1324
1325/**
1326 * Print the STUN message structure to a packet buffer, ready to be
1327 * sent to remote destination. This function will take care about
1328 * calculating the MESSAGE-INTEGRITY digest as well as FINGERPRINT
1329 * value, if these attributes are present in the message.
1330 *
1331 * If application wants to apply credential to the message, it MUST
1332 * include a blank MESSAGE-INTEGRITY attribute in the message as the
1333 * last attribute or the attribute before FINGERPRINT. This function will
1334 * calculate the HMAC digest from the message using the supplied key in
1335 * the parameter. The key should be set to the password if short term
1336 * credential is used, or calculated from the MD5 hash of the realm,
1337 * username, and password using #pj_stun_create_key() if long term
1338 * credential is used.
1339 *
1340 * If FINGERPRINT attribute is present, this function will calculate
1341 * the FINGERPRINT CRC attribute for the message. The FINGERPRINT MUST
1342 * be added as the last attribute of the message.
1343 *
1344 * @param msg The STUN message to be printed. Upon return,
1345 * some fields in the header (such as message
1346 * length) will be updated.
1347 * @param pkt_buf The buffer to be filled with the packet.
1348 * @param buf_size Size of the buffer.
1349 * @param options Options, which currently must be zero.
1350 * @param key Authentication key to calculate MESSAGE-INTEGRITY
1351 * value. Application can create this key by using
1352 * #pj_stun_create_key() function.
1353 * @param p_msg_len Upon return, it will be filed with the size of
1354 * the packet in bytes, or negative value on error.
1355 *
1356 * @return PJ_SUCCESS on success or the appropriate error code.
1357 */
1358PJ_DECL(pj_status_t) pj_stun_msg_encode(pj_stun_msg *msg,
1359 pj_uint8_t *pkt_buf,
1360 pj_size_t buf_size,
1361 unsigned options,
1362 const pj_str_t *key,
1363 pj_size_t *p_msg_len);
1364
1365/**
1366 * Check that the PDU is potentially a valid STUN message. This function
1367 * is useful when application needs to multiplex STUN packets with other
1368 * application traffic. When this function returns PJ_SUCCESS, there is a
1369 * big chance that the packet is a STUN packet.
1370 *
1371 * Note that we cannot be sure that the PDU is a really valid STUN message
1372 * until we actually parse the PDU.
1373 *
1374 * @param pdu The packet buffer.
1375 * @param pdu_len The length of the packet buffer.
1376 * @param options Additional options to be applied in the checking,
1377 * which can be taken from pj_stun_decode_options. One
1378 * of the useful option is PJ_STUN_IS_DATAGRAM which
1379 * means that the pdu represents a whole STUN packet.
1380 *
1381 * @return PJ_SUCCESS if the PDU is a potentially valid STUN
1382 * message.
1383 */
1384PJ_DECL(pj_status_t) pj_stun_msg_check(const pj_uint8_t *pdu,
1385 pj_size_t pdu_len, unsigned options);
1386
1387
1388/**
1389 * Decode incoming packet into STUN message.
1390 *
1391 * @param pool Pool to allocate the message.
1392 * @param pdu The incoming packet to be parsed.
1393 * @param pdu_len The length of the incoming packet.
1394 * @param options Parsing flags, according to pj_stun_decode_options.
1395 * @param p_msg Pointer to receive the parsed message.
1396 * @param p_parsed_len Optional pointer to receive how many bytes have
1397 * been parsed for the STUN message. This is useful
1398 * when the packet is received over stream oriented
1399 * transport.
1400 * @param p_response Optional pointer to receive an instance of response
1401 * message, if one can be created. If the packet being
1402 * decoded is a request message, and it contains error,
1403 * and a response can be created, then the STUN
1404 * response message will be returned on this argument.
1405 *
1406 * @return PJ_SUCCESS if a STUN message has been successfully
1407 * decoded.
1408 */
1409PJ_DECL(pj_status_t) pj_stun_msg_decode(pj_pool_t *pool,
1410 const pj_uint8_t *pdu,
1411 pj_size_t pdu_len,
1412 unsigned options,
1413 pj_stun_msg **p_msg,
1414 pj_size_t *p_parsed_len,
1415 pj_stun_msg **p_response);
1416
1417/**
1418 * Dump STUN message to a printable string output.
1419 *
1420 * @param msg The STUN message
1421 * @param buffer Buffer where the printable string output will
1422 * be printed on.
1423 * @param length Specify the maximum length of the buffer.
1424 * @param printed_len Optional pointer, which on output will be filled
1425 * up with the actual length of the output string.
1426 *
1427 * @return The message string output.
1428 */
1429#if PJ_LOG_MAX_LEVEL > 0
1430PJ_DECL(char*) pj_stun_msg_dump(const pj_stun_msg *msg,
1431 char *buffer,
1432 unsigned length,
1433 unsigned *printed_len);
1434#else
1435# define pj_stun_msg_dump(msg, buf, length, printed_len) ""
1436#endif
1437
1438
1439/**
1440 * Find STUN attribute in the STUN message, starting from the specified
1441 * index.
1442 *
1443 * @param msg The STUN message.
1444 * @param attr_type The attribute type to be found, from pj_stun_attr_type.
1445 * @param start_index The start index of the attribute in the message.
1446 * Specify zero to start searching from the first
1447 * attribute.
1448 *
1449 * @return The attribute instance, or NULL if it cannot be
1450 * found.
1451 */
1452PJ_DECL(pj_stun_attr_hdr*) pj_stun_msg_find_attr(const pj_stun_msg *msg,
1453 int attr_type,
1454 unsigned start_index);
1455
1456
1457/**
1458 * Clone a STUN attribute.
1459 *
1460 * @param pool Pool to allocate memory.
1461 * @param attr Attribute to clone.
1462 *
1463 * @return Duplicate attribute.
1464 */
1465PJ_DECL(pj_stun_attr_hdr*) pj_stun_attr_clone(pj_pool_t *pool,
1466 const pj_stun_attr_hdr *attr);
1467
1468
1469/**
1470 * Initialize generic STUN IP address attribute. The \a addr_len and
1471 * \a addr parameters specify whether the address is IPv4 or IPv4
1472 * address.
1473 *
1474 * @param attr The socket address attribute to initialize.
1475 * @param attr_type Attribute type, from #pj_stun_attr_type.
1476 * @param xor_ed If non-zero, the port and address will be XOR-ed
1477 * with magic, to make the XOR-MAPPED-ADDRESS attribute.
1478 * @param addr A pj_sockaddr_in or pj_sockaddr_in6 structure.
1479 * @param addr_len Length of \a addr parameter.
1480 *
1481 * @return PJ_SUCCESS on success or the appropriate error code.
1482 */
1483PJ_DECL(pj_status_t) pj_stun_sockaddr_attr_init(pj_stun_sockaddr_attr *attr,
1484 int attr_type,
1485 pj_bool_t xor_ed,
1486 const pj_sockaddr_t *addr,
1487 unsigned addr_len);
1488
1489/**
1490 * Create a generic STUN IP address attribute. The \a addr_len and
1491 * \a addr parameters specify whether the address is IPv4 or IPv4
1492 * address.
1493 *
1494 * @param pool The pool to allocate memory from.
1495 * @param attr_type Attribute type, from #pj_stun_attr_type.
1496 * @param xor_ed If non-zero, the port and address will be XOR-ed
1497 * with magic, to make the XOR-MAPPED-ADDRESS attribute.
1498 * @param addr A pj_sockaddr_in or pj_sockaddr_in6 structure.
1499 * @param addr_len Length of \a addr parameter.
1500 * @param p_attr Pointer to receive the attribute.
1501 *
1502 * @return PJ_SUCCESS on success or the appropriate error code.
1503 */
1504PJ_DECL(pj_status_t) pj_stun_sockaddr_attr_create(pj_pool_t *pool,
1505 int attr_type,
1506 pj_bool_t xor_ed,
1507 const pj_sockaddr_t *addr,
1508 unsigned addr_len,
1509 pj_stun_sockaddr_attr **p_attr);
1510
1511
1512/**
1513 * Create and add generic STUN IP address attribute to a STUN message.
1514 * The \a addr_len and \a addr parameters specify whether the address is
1515 * IPv4 or IPv4 address.
1516 *
1517 * @param pool The pool to allocate memory from.
1518 * @param msg The STUN message.
1519 * @param attr_type Attribute type, from #pj_stun_attr_type.
1520 * @param xor_ed If non-zero, the port and address will be XOR-ed
1521 * with magic, to make the XOR-MAPPED-ADDRESS attribute.
1522 * @param addr A pj_sockaddr_in or pj_sockaddr_in6 structure.
1523 * @param addr_len Length of \a addr parameter.
1524 *
1525 * @return PJ_SUCCESS on success or the appropriate error code.
1526 */
1527PJ_DECL(pj_status_t) pj_stun_msg_add_sockaddr_attr(pj_pool_t *pool,
1528 pj_stun_msg *msg,
1529 int attr_type,
1530 pj_bool_t xor_ed,
1531 const pj_sockaddr_t *addr,
1532 unsigned addr_len);
1533
1534/**
1535 * Initialize a STUN generic string attribute.
1536 *
1537 * @param attr The string attribute to be initialized.
1538 * @param pool Pool to duplicate the value into the attribute,
1539 * if value is not NULL or empty.
1540 * @param attr_type Attribute type, from #pj_stun_attr_type.
1541 * @param value The string value to be assigned to the attribute.
1542 *
1543 * @return PJ_SUCCESS on success or the appropriate error code.
1544 */
1545PJ_DECL(pj_status_t) pj_stun_string_attr_init(pj_stun_string_attr *attr,
1546 pj_pool_t *pool,
1547 int attr_type,
1548 const pj_str_t *value);
1549
1550/**
1551 * Create a STUN generic string attribute.
1552 *
1553 * @param pool The pool to allocate memory from.
1554 * @param attr_type Attribute type, from #pj_stun_attr_type.
1555 * @param value The string value to be assigned to the attribute.
1556 * @param p_attr Pointer to receive the attribute.
1557 *
1558 * @return PJ_SUCCESS on success or the appropriate error code.
1559 */
1560PJ_DECL(pj_status_t) pj_stun_string_attr_create(pj_pool_t *pool,
1561 int attr_type,
1562 const pj_str_t *value,
1563 pj_stun_string_attr **p_attr);
1564
1565/**
1566 * Create and add STUN generic string attribute to the message.
1567 *
1568 * @param pool The pool to allocate memory from.
1569 * @param msg The STUN message.
1570 * @param attr_type Attribute type, from #pj_stun_attr_type.
1571 * @param value The string value to be assigned to the attribute.
1572 *
1573 * @return PJ_SUCCESS on success or the appropriate error code.
1574 */
1575PJ_DECL(pj_status_t) pj_stun_msg_add_string_attr(pj_pool_t *pool,
1576 pj_stun_msg *msg,
1577 int attr_type,
1578 const pj_str_t *value);
1579
1580/**
1581 * Create a STUN generic 32bit value attribute.
1582 *
1583 * @param pool The pool to allocate memory from.
1584 * @param attr_type Attribute type, from #pj_stun_attr_type.
1585 * @param value The 32bit value to be assigned to the attribute.
1586 * @param p_attr Pointer to receive the attribute.
1587 *
1588 * @return PJ_SUCCESS on success or the appropriate error code.
1589 */
1590PJ_DECL(pj_status_t) pj_stun_uint_attr_create(pj_pool_t *pool,
1591 int attr_type,
1592 pj_uint32_t value,
1593 pj_stun_uint_attr **p_attr);
1594
1595/**
1596 * Create and add STUN generic 32bit value attribute to the message.
1597 *
1598 * @param pool The pool to allocate memory from.
1599 * @param msg The STUN message
1600 * @param attr_type Attribute type, from #pj_stun_attr_type.
1601 * @param value The 32bit value to be assigned to the attribute.
1602 *
1603 * @return PJ_SUCCESS on success or the appropriate error code.
1604 */
1605PJ_DECL(pj_status_t) pj_stun_msg_add_uint_attr(pj_pool_t *pool,
1606 pj_stun_msg *msg,
1607 int attr_type,
1608 pj_uint32_t value);
1609
1610
1611/**
1612 * Create a STUN generic 64bit value attribute.
1613 *
1614 * @param pool Pool to allocate memory from.
1615 * @param attr_type Attribute type, from #pj_stun_attr_type.
1616 * @param value Optional value to be assigned.
1617 * @param p_attr Pointer to receive the attribute.
1618 *
1619 * @return PJ_SUCCESS on success or the appropriate error code.
1620 */
1621PJ_DECL(pj_status_t) pj_stun_uint64_attr_create(pj_pool_t *pool,
1622 int attr_type,
1623 const pj_timestamp *value,
1624 pj_stun_uint64_attr **p_attr);
1625
1626
1627/**
1628 * Create and add STUN generic 64bit value attribute to the message.
1629 *
1630 * @param pool The pool to allocate memory from.
1631 * @param msg The STUN message
1632 * @param attr_type Attribute type, from #pj_stun_attr_type.
1633 * @param value The 64bit value to be assigned to the attribute.
1634 *
1635 * @return PJ_SUCCESS on success or the appropriate error code.
1636 */
1637PJ_DECL(pj_status_t) pj_stun_msg_add_uint64_attr(pj_pool_t *pool,
1638 pj_stun_msg *msg,
1639 int attr_type,
1640 const pj_timestamp *value);
1641
1642/**
1643 * Create a STUN MESSAGE-INTEGRITY attribute.
1644 *
1645 * @param pool The pool to allocate memory from.
1646 * @param p_attr Pointer to receive the attribute.
1647 *
1648 * @return PJ_SUCCESS on success or the appropriate error code.
1649 */
1650PJ_DECL(pj_status_t) pj_stun_msgint_attr_create(pj_pool_t *pool,
1651 pj_stun_msgint_attr **p_attr);
1652
1653/**
1654 * Create and add STUN MESSAGE-INTEGRITY attribute.
1655 *
1656 * @param pool The pool to allocate memory from.
1657 * @param msg The STUN message
1658 *
1659 * @return PJ_SUCCESS on success or the appropriate error code.
1660 */
1661PJ_DECL(pj_status_t) pj_stun_msg_add_msgint_attr(pj_pool_t *pool,
1662 pj_stun_msg *msg);
1663
1664/**
1665 * Create a STUN ERROR-CODE attribute.
1666 *
1667 * @param pool The pool to allocate memory from.
1668 * @param err_code STUN error code.
1669 * @param err_reason Optional STUN error reason. If NULL is given, the
1670 * standard error reason will be given.
1671 * @param p_attr Pointer to receive the attribute.
1672 *
1673 * @return PJ_SUCCESS on success or the appropriate error code.
1674 */
1675PJ_DECL(pj_status_t) pj_stun_errcode_attr_create(pj_pool_t *pool,
1676 int err_code,
1677 const pj_str_t *err_reason,
1678 pj_stun_errcode_attr **p_attr);
1679
1680
1681/**
1682 * Create and add STUN ERROR-CODE attribute to the message.
1683 *
1684 * @param pool The pool to allocate memory from.
1685 * @param msg The STUN mesage.
1686 * @param err_code STUN error code.
1687 * @param err_reason Optional STUN error reason. If NULL is given, the
1688 * standard error reason will be given.
1689 *
1690 * @return PJ_SUCCESS on success or the appropriate error code.
1691 */
1692PJ_DECL(pj_status_t) pj_stun_msg_add_errcode_attr(pj_pool_t *pool,
1693 pj_stun_msg *msg,
1694 int err_code,
1695 const pj_str_t *err_reason);
1696
1697/**
1698 * Create instance of STUN UNKNOWN-ATTRIBUTES attribute and copy the
1699 * unknown attribute array to the attribute.
1700 *
1701 * @param pool The pool to allocate memory from.
1702 * @param attr_cnt Number of attributes in the array (can be zero).
1703 * @param attr Optional array of attributes.
1704 * @param p_attr Pointer to receive the attribute.
1705 *
1706 * @return PJ_SUCCESS on success or the appropriate error code.
1707 */
1708PJ_DECL(pj_status_t) pj_stun_unknown_attr_create(pj_pool_t *pool,
1709 unsigned attr_cnt,
1710 const pj_uint16_t attr[],
1711 pj_stun_unknown_attr **p_attr);
1712
1713/**
1714 * Create and add STUN UNKNOWN-ATTRIBUTES attribute to the message.
1715 *
1716 * @param pool The pool to allocate memory from.
1717 * @param msg The STUN message.
1718 * @param attr_cnt Number of attributes in the array (can be zero).
1719 * @param attr Optional array of attribute types.
1720 *
1721 * @return PJ_SUCCESS on success or the appropriate error code.
1722 */
1723PJ_DECL(pj_status_t) pj_stun_msg_add_unknown_attr(pj_pool_t *pool,
1724 pj_stun_msg *msg,
1725 unsigned attr_cnt,
1726 const pj_uint16_t attr[]);
1727
1728/**
1729 * Initialize STUN binary attribute.
1730 *
1731 * @param attr The attribute to be initialized.
1732 * @param pool Pool to copy data, if the data and length are set.
1733 * @param attr_type The attribute type, from #pj_stun_attr_type.
1734 * @param data Data to be coped to the attribute, or NULL
1735 * if no data to be copied now.
1736 * @param length Length of data, or zero if no data is to be
1737 * copied now.
1738 *
1739 * @return PJ_SUCCESS on success or the appropriate error code.
1740 */
1741PJ_DECL(pj_status_t) pj_stun_binary_attr_init(pj_stun_binary_attr *attr,
1742 pj_pool_t *pool,
1743 int attr_type,
1744 const pj_uint8_t *data,
1745 unsigned length);
1746
1747/**
1748 * Create STUN binary attribute.
1749 *
1750 * @param pool The pool to allocate memory from.
1751 * @param attr_type The attribute type, from #pj_stun_attr_type.
1752 * @param data Data to be coped to the attribute, or NULL
1753 * if no data to be copied now.
1754 * @param length Length of data, or zero if no data is to be
1755 * copied now.
1756 * @param p_attr Pointer to receive the attribute.
1757 *
1758 * @return PJ_SUCCESS on success or the appropriate error code.
1759 */
1760PJ_DECL(pj_status_t) pj_stun_binary_attr_create(pj_pool_t *pool,
1761 int attr_type,
1762 const pj_uint8_t *data,
1763 unsigned length,
1764 pj_stun_binary_attr **p_attr);
1765
1766/**
1767 * Create STUN binary attribute and add the attribute to the message.
1768 *
1769 * @param pool The pool to allocate memory from.
1770 * @param msg The STUN message.
1771 * @param attr_type The attribute type, from #pj_stun_attr_type.
1772 * @param data Data to be coped to the attribute, or NULL
1773 * if no data to be copied now.
1774 * @param length Length of data, or zero if no data is to be
1775 * copied now.
1776 *
1777 * @return PJ_SUCCESS on success or the appropriate error code.
1778 */
1779PJ_DECL(pj_status_t) pj_stun_msg_add_binary_attr(pj_pool_t *pool,
1780 pj_stun_msg *msg,
1781 int attr_type,
1782 const pj_uint8_t *data,
1783 unsigned length);
1784
1785/**
1786 * Create STUN empty attribute.
1787 *
1788 * @param pool The pool to allocate memory from.
1789 * @param attr_type The attribute type, from #pj_stun_attr_type.
1790 * @param p_attr Pointer to receive the attribute.
1791 *
1792 * @return PJ_SUCCESS on success or the appropriate error code.
1793 */
1794PJ_DECL(pj_status_t) pj_stun_empty_attr_create(pj_pool_t *pool,
1795 int attr_type,
1796 pj_stun_empty_attr **p_attr);
1797
1798/**
1799 * Create STUN empty attribute and add the attribute to the message.
1800 *
1801 * @param pool The pool to allocate memory from.
1802 * @param msg The STUN message.
1803 * @param attr_type The attribute type, from #pj_stun_attr_type.
1804 *
1805 * @return PJ_SUCCESS on success or the appropriate error code.
1806 */
1807PJ_DECL(pj_status_t) pj_stun_msg_add_empty_attr(pj_pool_t *pool,
1808 pj_stun_msg *msg,
1809 int attr_type);
1810
1811/**
1812 * @}
1813 */
1814
1815
1816PJ_END_DECL
1817
1818
1819#endif /* __PJNATH_STUN_MSG_H__ */
1820