blob: f8ea4d1dca952105c4688ad45f1a58be4c5b89b1 [file] [log] [blame]
Alexandre Lision8af73cb2013-12-10 14:11:20 -05001/* $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_SESSION_H__
21#define __PJNATH_STUN_SESSION_H__
22
23/**
24 * @file stun_session.h
25 * @brief STUN session management for client/server.
26 */
27
28#include <pjnath/stun_msg.h>
29#include <pjnath/stun_auth.h>
30#include <pjnath/stun_config.h>
31#include <pjnath/stun_transaction.h>
32#include <pj/list.h>
33#include <pj/lock.h>
34#include <pj/timer.h>
35
36PJ_BEGIN_DECL
37
38
39/* **************************************************************************/
40/**
41 * @addtogroup PJNATH_STUN_SESSION
42 * @{
43 *
44 * This is is a transport-independent object to manage a client or server
45 * STUN session. It has the following features:
46 *
47 * - <b>transport independent</b>:\n
48 * the object does not have it's own socket, but rather it provides
49 * functions and callbacks to send and receive packets. This way the
50 * object can be used by different transport types (e.g. UDP, TCP,
51 * TLS, etc.) as well as better integration to application which
52 * already has its own means to send and receive packets.
53 *
54 * - <b>authentication management</b>:\n
55 * the object manages STUN authentication throughout the lifetime of
56 * the session. For client sessions, once it's given a credential to
57 * authenticate itself with the server, the object will automatically
58 * add authentication info (the MESSAGE-INTEGRITY) to the request as
59 * well as authenticate the response. It will also handle long-term
60 * authentication challenges, including handling of nonce expiration,
61 * and retry the request automatically. For server sessions, it can
62 * be configured to authenticate incoming requests automatically.
63 *
64 * - <b>static or dynamic credential</b>:\n
65 * application may specify static or dynamic credential to be used by
66 * the STUN session. Static credential means a static combination of
67 * username and password (and these cannot change during the session
68 * duration), while dynamic credential provides callback to ask the
69 * application about which username/password to use everytime
70 * authentication is about to be performed.
71 *
72 * - <b>client transaction management</b>:\n
73 * outgoing requests may be sent with a STUN transaction for reliability,
74 * and the object will manage the transaction internally (including
75 * performing retransmissions). Application will be notified about the
76 * result of the request when the response arrives (or the transaction
77 * times out). When the request is challenged with authentication, the
78 * object will retry the request with new authentication info, and
79 * application will be notified about the final result of this request.
80 *
81 * - <b>server transaction management</b>:\n
82 * application may ask response to incoming requests to be cached by
83 * the object, and in this case the object will check for cached
84 * response everytime request is received. The cached response will be
85 * deleted once a timer expires.
86 *
87 * \section using_stun_sess_sec Using the STUN session
88 *
89 * The following steps describes how to use the STUN session:
90 *
91 * - <b>create the object configuration</b>:\n
92 * The #pj_stun_config contains the configuration to create the STUN
93 * session, such as the timer heap to register internal timers and
94 * various STUN timeout values. You can initialize this structure by
95 * calling #pj_stun_config_init()
96 *
97 * - <b>create the STUN session</b>:\n
98 * by calling #pj_stun_session_create(). Among other things, this
99 * function requires the instance of #pj_stun_config and also
100 * #pj_stun_session_cb structure which stores callbacks to send
101 * outgoing packets as well as to notify application about incoming
102 * STUN requests, responses, and indicates and other events.
103 *
104 * - <b>configure credential:</b>\n
105 * if authentication is required for the session, configure the
106 * credential with #pj_stun_session_set_credential()
107 *
108 * - <b>configuring other settings:</b>\n
109 * several APIs are provided to configure the behavior of the STUN
110 * session (for example, to set the SOFTWARE attribute value, controls
111 * the logging behavior, fine tune the mutex locking, etc.). Please see
112 * the API reference for more info.
113 *
114 * - <b>creating outgoing STUN requests or indications:</b>\n
115 * create the STUN message by using #pj_stun_session_create_req() or
116 * #pj_stun_session_create_ind(). This will create a transmit data
117 * buffer containing a blank STUN request or indication. You will then
118 * typically need to add STUN attributes that are relevant to the
119 * request or indication, but note that some default attributes will
120 * be added by the session later when the message is sent (such as
121 * SOFTWARE attribute and attributes related to authentication).
122 * The message is now ready to be sent.
123 *
124 * - <b>sending outgoing message:</b>\n
125 * use #pj_stun_session_send_msg() to send outgoing STUN messages (this
126 * includes STUN requests, indications, and responses). The function has
127 * options whether to retransmit the request (for non reliable transports)
128 * or to cache the response if we're sending response. This function in
129 * turn will call the \a on_send_msg() callback of #pj_stun_session_cb
130 * to request the application to send the packet.
131 *
132 * - <b>handling incoming packet:</b>\n
133 * call #pj_stun_session_on_rx_pkt() everytime the application receives
134 * a STUN packet. This function will decode the packet and process the
135 * packet according to the message, and normally this will cause one
136 * of the callback in the #pj_stun_session_cb to be called to notify
137 * the application about the event.
138 *
139 * - <b>handling incoming requests:</b>\n
140 * incoming requests are notified to application in the \a on_rx_request
141 * callback of the #pj_stun_session_cb. If authentication is enabled in
142 * the session, the application will only receive this callback after
143 * the incoming request has been authenticated (if the authentication
144 * fails, the session would respond automatically with 401 error and
145 * the callback will not be called). Application now must create and
146 * send response for this request.
147 *
148 * - <b>creating and sending response:</b>\n
149 * create the STUN response with #pj_stun_session_create_res(). This will
150 * create a transmit data buffer containing a blank STUN response. You
151 * will then typically need to add STUN attributes that are relevant to
152 * the response, but note that some default attributes will
153 * be added by the session later when the message is sent (such as
154 * SOFTWARE attribute and attributes related to authentication).
155 * The message is now ready to be sent. Use #pj_stun_session_send_msg()
156 * (as explained above) to send the response.
157 *
158 * - <b>convenient way to send response:</b>\n
159 * the #pj_stun_session_respond() is provided as a convenient way to
160 * create and send simple STUN responses, such as error responses.
161 *
162 * - <b>destroying the session:</b>\n
163 * once the session is done, use #pj_stun_session_destroy() to destroy
164 * the session.
165 */
166
167
168/** Forward declaration for pj_stun_tx_data */
169typedef struct pj_stun_tx_data pj_stun_tx_data;
170
171/** Forward declaration for pj_stun_rx_data */
172typedef struct pj_stun_rx_data pj_stun_rx_data;
173
174/** Forward declaration for pj_stun_session */
175typedef struct pj_stun_session pj_stun_session;
176
177
178/**
179 * This is the callback to be registered to pj_stun_session, to send
180 * outgoing message and to receive various notifications from the STUN
181 * session.
182 */
183typedef struct pj_stun_session_cb
184{
185 /**
186 * Callback to be called by the STUN session to send outgoing message.
187 *
188 * @param sess The STUN session.
189 * @param token The token associated with this outgoing message
190 * and was set by the application. This token was
191 * set by application in pj_stun_session_send_msg()
192 * for outgoing messages that are initiated by the
193 * application, or in pj_stun_session_on_rx_pkt()
194 * if this message is a response that was internally
195 * generated by the STUN session (for example, an
196 * 401/Unauthorized response). Application may use
197 * this facility for any purposes.
198 * @param pkt Packet to be sent.
199 * @param pkt_size Size of the packet to be sent.
200 * @param dst_addr The destination address.
201 * @param addr_len Length of destination address.
202 *
203 * @return The callback should return the status of the
204 * packet sending.
205 */
206 pj_status_t (*on_send_msg)(pj_stun_session *sess,
207 void *token,
208 const void *pkt,
209 pj_size_t pkt_size,
210 const pj_sockaddr_t *dst_addr,
211 unsigned addr_len);
212
213 /**
214 * Callback to be called on incoming STUN request message. This function
215 * is called when application calls pj_stun_session_on_rx_pkt() and when
216 * the STUN session has detected that the incoming STUN message is a
217 * STUN request message. In the
218 * callback processing, application MUST create a response by calling
219 * pj_stun_session_create_response() function and send the response
220 * with pj_stun_session_send_msg() function, before returning from
221 * the callback.
222 *
223 * @param sess The STUN session.
224 * @param pkt Pointer to the original STUN packet.
225 * @param pkt_len Length of the STUN packet.
226 * @param rdata Data containing incoming request message.
227 * @param token The token that was set by the application when
228 * calling pj_stun_session_on_rx_pkt() function.
229 * @param src_addr Source address of the packet.
230 * @param src_addr_len Length of the source address.
231 *
232 * @return The return value of this callback will be
233 * returned back to pj_stun_session_on_rx_pkt()
234 * function.
235 */
236 pj_status_t (*on_rx_request)(pj_stun_session *sess,
237 const pj_uint8_t *pkt,
238 unsigned pkt_len,
239 const pj_stun_rx_data *rdata,
240 void *token,
241 const pj_sockaddr_t *src_addr,
242 unsigned src_addr_len);
243
244 /**
245 * Callback to be called when response is received or the transaction
246 * has timed out. This callback is called either when application calls
247 * pj_stun_session_on_rx_pkt() with the packet containing a STUN
248 * response for the client transaction, or when the internal timer of
249 * the STUN client transaction has timed-out before a STUN response is
250 * received.
251 *
252 * @param sess The STUN session.
253 * @param status Status of the request. If the value if not
254 * PJ_SUCCESS, the transaction has timed-out
255 * or other error has occurred, and the response
256 * argument may be NULL.
257 * Note that when the status is not success, the
258 * response may contain non-NULL value if the
259 * response contains STUN ERROR-CODE attribute.
260 * @param token The token that was set by the application when
261 * calling pj_stun_session_send_msg() function.
262 * Please not that this token IS NOT the token
263 * that was given in pj_stun_session_on_rx_pkt().
264 * @param tdata The original STUN request.
265 * @param response The response message, on successful transaction,
266 * or otherwise MAY BE NULL if status is not success.
267 * Note that when the status is not success, this
268 * argument may contain non-NULL value if the
269 * response contains STUN ERROR-CODE attribute.
270 * @param src_addr The source address where the response was
271 * received, or NULL if the response is NULL.
272 * @param src_addr_len The length of the source address.
273 */
274 void (*on_request_complete)(pj_stun_session *sess,
275 pj_status_t status,
276 void *token,
277 pj_stun_tx_data *tdata,
278 const pj_stun_msg *response,
279 const pj_sockaddr_t *src_addr,
280 unsigned src_addr_len);
281
282
283 /**
284 * Callback to be called on incoming STUN request message. This function
285 * is called when application calls pj_stun_session_on_rx_pkt() and when
286 * the STUN session has detected that the incoming STUN message is a
287 * STUN indication message.
288 *
289 * @param sess The STUN session.
290 * @param pkt Pointer to the original STUN packet.
291 * @param pkt_len Length of the STUN packet.
292 * @param msg The parsed STUN indication.
293 * @param token The token that was set by the application when
294 * calling pj_stun_session_on_rx_pkt() function.
295 * @param src_addr Source address of the packet.
296 * @param src_addr_len Length of the source address.
297 *
298 * @return The return value of this callback will be
299 * returned back to pj_stun_session_on_rx_pkt()
300 * function.
301 */
302 pj_status_t (*on_rx_indication)(pj_stun_session *sess,
303 const pj_uint8_t *pkt,
304 unsigned pkt_len,
305 const pj_stun_msg *msg,
306 void *token,
307 const pj_sockaddr_t *src_addr,
308 unsigned src_addr_len);
309
310} pj_stun_session_cb;
311
312
313/**
314 * This structure describes incoming request message.
315 */
316struct pj_stun_rx_data
317{
318 /**
319 * The parsed request message.
320 */
321 pj_stun_msg *msg;
322
323 /**
324 * Credential information that is found and used to authenticate
325 * incoming request. Application may use this information when
326 * generating authentication for the outgoing response.
327 */
328 pj_stun_req_cred_info info;
329};
330
331
332/**
333 * This structure describe the outgoing STUN transmit data to carry the
334 * message to be sent.
335 */
336struct pj_stun_tx_data
337{
338 /** PJLIB list interface */
339 PJ_DECL_LIST_MEMBER(struct pj_stun_tx_data);
340
341 pj_pool_t *pool; /**< Pool. */
342 pj_stun_session *sess; /**< The STUN session. */
343 pj_stun_msg *msg; /**< The STUN message. */
344
345 void *token; /**< The token. */
346
347 pj_stun_client_tsx *client_tsx; /**< Client STUN transaction. */
348 pj_bool_t retransmit; /**< Retransmit request? */
349 pj_uint32_t msg_magic; /**< Message magic. */
350 pj_uint8_t msg_key[12]; /**< Message/transaction key. */
351
352 pj_stun_req_cred_info auth_info; /**< Credential info */
353
354 void *pkt; /**< The STUN packet. */
355 unsigned max_len; /**< Length of packet buffer. */
356 pj_size_t pkt_size; /**< The actual length of STUN pkt. */
357
358 unsigned addr_len; /**< Length of destination address. */
359 const pj_sockaddr_t *dst_addr; /**< Destination address. */
360
361 pj_timer_entry res_timer; /**< Response cache timer. */
362};
363
364
365/**
366 * These are the flags to control the message logging in the STUN session.
367 */
368typedef enum pj_stun_sess_msg_log_flag
369{
370 PJ_STUN_SESS_LOG_TX_REQ=1, /**< Log outgoing STUN requests. */
371 PJ_STUN_SESS_LOG_TX_RES=2, /**< Log outgoing STUN responses. */
372 PJ_STUN_SESS_LOG_TX_IND=4, /**< Log outgoing STUN indications. */
373
374 PJ_STUN_SESS_LOG_RX_REQ=8, /**< Log incoming STUN requests. */
375 PJ_STUN_SESS_LOG_RX_RES=16, /**< Log incoming STUN responses */
376 PJ_STUN_SESS_LOG_RX_IND=32 /**< Log incoming STUN indications */
377} pj_stun_sess_msg_log_flag;
378
379
380/**
381 * Create a STUN session.
382 *
383 * @param cfg The STUN endpoint, to be used to register timers etc.
384 * @param name Optional name to be associated with this instance. The
385 * name will be used for example for logging purpose.
386 * @param cb Session callback.
387 * @param fingerprint Enable message fingerprint for outgoing messages.
388 * @param grp_lock Optional group lock to be used by this session.
389 * If NULL, the session will create one itself.
390 * @param p_sess Pointer to receive STUN session instance.
391 *
392 * @return PJ_SUCCESS on success, or the appropriate error code.
393 */
394PJ_DECL(pj_status_t) pj_stun_session_create(pj_stun_config *cfg,
395 const char *name,
396 const pj_stun_session_cb *cb,
397 pj_bool_t fingerprint,
398 pj_grp_lock_t *grp_lock,
399 pj_stun_session **p_sess);
400
401/**
402 * Destroy the STUN session and all objects created in the context of
403 * this session.
404 *
405 * @param sess The STUN session instance.
406 *
407 * @return PJ_SUCCESS on success, or the appropriate error code.
408 * This function will return PJ_EPENDING if the operation
409 * cannot be performed immediately because callbacks are
410 * being called; in this case the session will be destroyed
411 * as soon as the last callback returns.
412 */
413PJ_DECL(pj_status_t) pj_stun_session_destroy(pj_stun_session *sess);
414
415/**
416 * Associated an arbitrary data with this STUN session. The user data may
417 * be retrieved later with pj_stun_session_get_user_data() function.
418 *
419 * @param sess The STUN session instance.
420 * @param user_data The user data.
421 *
422 * @return PJ_SUCCESS on success, or the appropriate error code.
423 */
424PJ_DECL(pj_status_t) pj_stun_session_set_user_data(pj_stun_session *sess,
425 void *user_data);
426
427/**
428 * Retrieve the user data previously associated to this STUN session with
429 * pj_stun_session_set_user_data().
430 *
431 * @param sess The STUN session instance.
432 *
433 * @return The user data associated with this STUN session.
434 */
435PJ_DECL(void*) pj_stun_session_get_user_data(pj_stun_session *sess);
436
437/**
438 * Get the group lock for this STUN session.
439 *
440 * @param sess The STUN session instance.
441 *
442 * @return The group lock.
443 */
444PJ_DECL(pj_grp_lock_t *) pj_stun_session_get_grp_lock(pj_stun_session *sess);
445
446/**
447 * Set SOFTWARE name to be included in all requests and responses.
448 *
449 * @param sess The STUN session instance.
450 * @param sw Software name string. If this argument is NULL or
451 * empty, the session will not include SOFTWARE attribute
452 * in STUN requests and responses.
453 *
454 * @return PJ_SUCCESS on success, or the appropriate error code.
455 */
456PJ_DECL(pj_status_t) pj_stun_session_set_software_name(pj_stun_session *sess,
457 const pj_str_t *sw);
458
459/**
460 * Set credential to be used by this session. Once credential is set, all
461 * outgoing messages will include MESSAGE-INTEGRITY, and all incoming
462 * message will be authenticated against this credential.
463 *
464 * To disable authentication after it has been set, call this function
465 * again with NULL as the argument.
466 *
467 * @param sess The STUN session instance.
468 * @param auth_type Type of authentication.
469 * @param cred The credential to be used by this session. If NULL
470 * is specified, authentication will be disabled.
471 *
472 * @return PJ_SUCCESS on success, or the appropriate error code.
473 */
474PJ_DECL(pj_status_t) pj_stun_session_set_credential(pj_stun_session *sess,
475 pj_stun_auth_type auth_type,
476 const pj_stun_auth_cred *cred);
477/**
478 * Configure message logging. By default all flags are enabled.
479 *
480 * @param sess The STUN session instance.
481 * @param flags Bitmask combination of #pj_stun_sess_msg_log_flag
482 */
483PJ_DECL(void) pj_stun_session_set_log(pj_stun_session *sess,
484 unsigned flags);
485/**
486 * Configure whether the STUN session should utilize FINGERPRINT in
487 * outgoing messages.
488 *
489 * @param sess The STUN session instance.
490 * @param use Boolean for the setting.
491 *
492 * @return The previous configured value of FINGERPRINT
493 * utilization of the sessoin.
494 */
495PJ_DECL(pj_bool_t) pj_stun_session_use_fingerprint(pj_stun_session *sess,
496 pj_bool_t use);
497
498/**
499 * Create a STUN request message. After the message has been successfully
500 * created, application can send the message by calling
501 * pj_stun_session_send_msg().
502 *
503 * @param sess The STUN session instance.
504 * @param msg_type The STUN request message type, from pj_stun_method_e or
505 * from pj_stun_msg_type.
506 * @param magic STUN magic, use PJ_STUN_MAGIC.
507 * @param tsx_id Optional transaction ID.
508 * @param p_tdata Pointer to receive STUN transmit data instance containing
509 * the request.
510 *
511 * @return PJ_SUCCESS on success, or the appropriate error code.
512 */
513PJ_DECL(pj_status_t) pj_stun_session_create_req(pj_stun_session *sess,
514 int msg_type,
515 pj_uint32_t magic,
516 const pj_uint8_t tsx_id[12],
517 pj_stun_tx_data **p_tdata);
518
519/**
520 * Create a STUN Indication message. After the message has been successfully
521 * created, application can send the message by calling
522 * pj_stun_session_send_msg().
523 *
524 * @param sess The STUN session instance.
525 * @param msg_type The STUN request message type, from pj_stun_method_e or
526 * from pj_stun_msg_type. This function will add the
527 * indication bit as necessary.
528 * @param p_tdata Pointer to receive STUN transmit data instance containing
529 * the message.
530 *
531 * @return PJ_SUCCESS on success, or the appropriate error code.
532 */
533PJ_DECL(pj_status_t) pj_stun_session_create_ind(pj_stun_session *sess,
534 int msg_type,
535 pj_stun_tx_data **p_tdata);
536
537/**
538 * Create a STUN response message. After the message has been
539 * successfully created, application can send the message by calling
540 * pj_stun_session_send_msg(). Alternatively application may use
541 * pj_stun_session_respond() to create and send response in one function
542 * call.
543 *
544 * @param sess The STUN session instance.
545 * @param rdata The STUN request where the response is to be created.
546 * @param err_code Error code to be set in the response, if error response
547 * is to be created, according to pj_stun_status enumeration.
548 * This argument MUST be zero if successful response is
549 * to be created.
550 * @param err_msg Optional pointer for the error message string, when
551 * creating error response. If the value is NULL and the
552 * \a err_code is non-zero, then default error message will
553 * be used.
554 * @param p_tdata Pointer to receive the response message created.
555 *
556 * @return PJ_SUCCESS on success, or the appropriate error code.
557 */
558PJ_DECL(pj_status_t) pj_stun_session_create_res(pj_stun_session *sess,
559 const pj_stun_rx_data *rdata,
560 unsigned err_code,
561 const pj_str_t *err_msg,
562 pj_stun_tx_data **p_tdata);
563
564/**
565 * Send STUN message to the specified destination. This function will encode
566 * the pj_stun_msg instance to a packet buffer, and add credential or
567 * fingerprint if necessary. If the message is a request, the session will
568 * also create and manage a STUN client transaction to be used to manage the
569 * retransmission of the request. After the message has been encoded and
570 * transaction is setup, the \a on_send_msg() callback of pj_stun_session_cb
571 * (which is registered when the STUN session is created) will be called
572 * to actually send the message to the wire.
573 *
574 * @param sess The STUN session instance.
575 * @param token Optional token which will be given back to application in
576 * \a on_send_msg() callback and \a on_request_complete()
577 * callback, if the message is a STUN request message.
578 * Internally this function will put the token in the
579 * \a token field of pj_stun_tx_data, hence it will
580 * overwrite any value that the application puts there.
581 * @param cache_res If the message is a response message for an incoming
582 * request, specify PJ_TRUE to instruct the STUN session
583 * to cache this response for subsequent incoming request
584 * retransmission. Otherwise this parameter will be ignored
585 * for non-response message.
586 * @param retransmit If the message is a request message, specify whether the
587 * request should be retransmitted. Normally application will
588 * specify TRUE if the underlying transport is UDP and FALSE
589 * if the underlying transport is TCP or TLS.
590 * @param dst_addr The destination socket address.
591 * @param addr_len Length of destination address.
592 * @param tdata The STUN transmit data containing the STUN message to
593 * be sent.
594 *
595 * @return PJ_SUCCESS on success, or the appropriate error code.
596 * This function will return PJNATH_ESTUNDESTROYED if
597 * application has destroyed the session in
598 * \a on_send_msg() callback.
599 */
600PJ_DECL(pj_status_t) pj_stun_session_send_msg(pj_stun_session *sess,
601 void *token,
602 pj_bool_t cache_res,
603 pj_bool_t retransmit,
604 const pj_sockaddr_t *dst_addr,
605 unsigned addr_len,
606 pj_stun_tx_data *tdata);
607
608/**
609 * This is a utility function to create and send response for an incoming
610 * STUN request. Internally this function calls pj_stun_session_create_res()
611 * and pj_stun_session_send_msg(). It is provided here as a matter of
612 * convenience.
613 *
614 * @param sess The STUN session instance.
615 * @param rdata The STUN request message to be responded.
616 * @param code Error code to be set in the response, if error response
617 * is to be created, according to pj_stun_status enumeration.
618 * This argument MUST be zero if successful response is
619 * to be created.
620 * @param err_msg Optional pointer for the error message string, when
621 * creating error response. If the value is NULL and the
622 * \a err_code is non-zero, then default error message will
623 * be used.
624 * @param token Optional token which will be given back to application in
625 * \a on_send_msg() callback and \a on_request_complete()
626 * callback, if the message is a STUN request message.
627 * Internally this function will put the token in the
628 * \a token field of pj_stun_tx_data, hence it will
629 * overwrite any value that the application puts there.
630 * @param cache Specify whether session should cache this response for
631 * future request retransmission. If TRUE, subsequent request
632 * retransmission will be handled by the session and it
633 * will not call request callback.
634 * @param dst_addr Destination address of the response (or equal to the
635 * source address of the original request).
636 * @param addr_len Address length.
637 *
638 * @return PJ_SUCCESS on success, or the appropriate error code.
639 * This function will return PJNATH_ESTUNDESTROYED if
640 * application has destroyed the session in
641 * \a on_send_msg() callback.
642 */
643PJ_DECL(pj_status_t) pj_stun_session_respond(pj_stun_session *sess,
644 const pj_stun_rx_data *rdata,
645 unsigned code,
646 const char *err_msg,
647 void *token,
648 pj_bool_t cache,
649 const pj_sockaddr_t *dst_addr,
650 unsigned addr_len);
651
652/**
653 * Cancel outgoing STUN transaction. This operation is only valid for outgoing
654 * STUN request, to cease retransmission of the request and destroy the
655 * STUN client transaction that is used to send the request.
656 *
657 * @param sess The STUN session instance.
658 * @param tdata The request message previously sent.
659 * @param notify Specify whether \a on_request_complete() callback should
660 * be called.
661 * @param status If \a on_request_complete() callback is to be called,
662 * specify the error status to be given when calling the
663 * callback. This error status MUST NOT be PJ_SUCCESS.
664 *
665 * @return PJ_SUCCESS if transaction is successfully cancelled.
666 * This function will return PJNATH_ESTUNDESTROYED if
667 * application has destroyed the session in
668 * \a on_request_complete() callback.
669 */
670PJ_DECL(pj_status_t) pj_stun_session_cancel_req(pj_stun_session *sess,
671 pj_stun_tx_data *tdata,
672 pj_bool_t notify,
673 pj_status_t status);
674
675/**
676 * Explicitly request retransmission of the request. Normally application
677 * doesn't need to do this, but this functionality is needed by ICE to
678 * speed up connectivity check completion.
679 *
680 * @param sess The STUN session instance.
681 * @param tdata The request message previously sent.
682 * @param mod_count Boolean flag to indicate whether transmission count
683 * needs to be incremented.
684 *
685 * @return PJ_SUCCESS on success, or the appropriate error.
686 * This function will return PJNATH_ESTUNDESTROYED if
687 * application has destroyed the session in \a on_send_msg()
688 * callback.
689 */
690PJ_DECL(pj_status_t) pj_stun_session_retransmit_req(pj_stun_session *sess,
691 pj_stun_tx_data *tdata,
692 pj_bool_t mod_count);
693
694
695/**
696 * Application must call this function to notify the STUN session about
697 * the arrival of STUN packet. The STUN packet MUST have been checked
698 * first with #pj_stun_msg_check() to verify that this is indeed a valid
699 * STUN packet.
700 *
701 * The STUN session will decode the packet into pj_stun_msg, and process
702 * the message accordingly. If the message is a response, it will search
703 * through the outstanding STUN client transactions for a matching
704 * transaction ID and hand over the response to the transaction.
705 *
706 * On successful message processing, application will be notified about
707 * the message via one of the pj_stun_session_cb callback.
708 *
709 * @param sess The STUN session instance.
710 * @param packet The packet containing STUN message.
711 * @param pkt_size Size of the packet.
712 * @param options Options, from #pj_stun_decode_options.
713 * @param parsed_len Optional pointer to receive the size of the parsed
714 * STUN message (useful if packet is received via a
715 * stream oriented protocol).
716 * @param token Optional token which will be given back to application
717 * in the \a on_rx_request(), \a on_rx_indication() and
718 * \a on_send_msg() callbacks. The token can be used to
719 * associate processing or incoming request or indication
720 * with some context.
721 * @param src_addr The source address of the packet, which will also
722 * be given back to application callbacks, along with
723 * source address length.
724 * @param src_addr_len Length of the source address.
725 *
726 * @return PJ_SUCCESS on success, or the appropriate error code.
727 * This function will return PJNATH_ESTUNDESTROYED if
728 * application has destroyed the session in one of the
729 * callback.
730 */
731PJ_DECL(pj_status_t) pj_stun_session_on_rx_pkt(pj_stun_session *sess,
732 const void *packet,
733 pj_size_t pkt_size,
734 unsigned options,
735 void *token,
736 pj_size_t *parsed_len,
737 const pj_sockaddr_t *src_addr,
738 unsigned src_addr_len);
739
740/**
741 * Destroy the transmit data. Call this function only when tdata has been
742 * created but application doesn't want to send the message (perhaps
743 * because of other error).
744 *
745 * @param sess The STUN session.
746 * @param tdata The transmit data.
747 *
748 * @return PJ_SUCCESS on success, or the appropriate error code.
749 */
750PJ_DECL(void) pj_stun_msg_destroy_tdata(pj_stun_session *sess,
751 pj_stun_tx_data *tdata);
752
753
754/**
755 * @}
756 */
757
758
759PJ_END_DECL
760
761#endif /* __PJNATH_STUN_SESSION_H__ */
762