1
/* $Id: stun_transaction.h 4360 2013-02-21 11:26:35Z bennylp $ */
3
* Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
4
* Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
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.
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.
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
20
#ifndef __PJNATH_STUN_TRANSACTION_H__
21
#define __PJNATH_STUN_TRANSACTION_H__
24
* @file stun_transaction.h
25
* @brief STUN transaction
28
#include <pjnath/stun_msg.h>
29
#include <pjnath/stun_config.h>
36
/* **************************************************************************/
38
* @defgroup PJNATH_STUN_TRANSACTION STUN Client Transaction
39
* @brief STUN client transaction
40
* @ingroup PJNATH_STUN_BASE
43
The @ref PJNATH_STUN_TRANSACTION is used to manage outgoing STUN request,
44
for example to retransmit the request and to notify application about the
45
completion of the request.
47
The @ref PJNATH_STUN_TRANSACTION does not use any networking operations,
48
but instead application must supply the transaction with a callback to
49
be used by the transaction to send outgoing requests. This way the STUN
50
transaction is made more generic and can work with different types of
51
networking codes in application.
57
* Opaque declaration of STUN client transaction.
59
typedef struct pj_stun_client_tsx pj_stun_client_tsx;
62
* STUN client transaction callback.
64
typedef struct pj_stun_tsx_cb
67
* This callback is called when the STUN transaction completed.
69
* @param tsx The STUN transaction.
70
* @param status Status of the transaction. Status PJ_SUCCESS
71
* means that the request has received a successful
73
* @param response The STUN response, which value may be NULL if
74
* \a status is not PJ_SUCCESS.
75
* @param src_addr The source address of the response, if response
77
* @param src_addr_len The length of the source address.
79
void (*on_complete)(pj_stun_client_tsx *tsx,
81
const pj_stun_msg *response,
82
const pj_sockaddr_t *src_addr,
83
unsigned src_addr_len);
86
* This callback is called by the STUN transaction when it wants to send
89
* @param tsx The STUN transaction instance.
90
* @param stun_pkt The STUN packet to be sent.
91
* @param pkt_size Size of the STUN packet.
93
* @return If return value of the callback is not PJ_SUCCESS,
94
* the transaction will fail. Application MUST return
95
* PJNATH_ESTUNDESTROYED if it has destroyed the
96
* transaction in this callback.
98
pj_status_t (*on_send_msg)(pj_stun_client_tsx *tsx,
103
* This callback is called after the timer that was scheduled by
104
* #pj_stun_client_tsx_schedule_destroy() has elapsed. Application
105
* should call #pj_stun_client_tsx_destroy() upon receiving this
108
* This callback is optional if application will not call
109
* #pj_stun_client_tsx_schedule_destroy().
111
* @param tsx The STUN transaction instance.
113
void (*on_destroy)(pj_stun_client_tsx *tsx);
120
* Create an instance of STUN client transaction. The STUN client
121
* transaction is used to transmit outgoing STUN request and to
122
* ensure the reliability of the request by periodically retransmitting
123
* the request, if necessary.
125
* @param cfg The STUN endpoint, which will be used to retrieve
126
* various settings for the transaction.
127
* @param pool Pool to be used to allocate memory from.
128
* @param grp_lock Group lock to synchronize.
129
* @param cb Callback structure, to be used by the transaction
130
* to send message and to notify the application about
131
* the completion of the transaction.
132
* @param p_tsx Pointer to receive the transaction instance.
134
* @return PJ_SUCCESS on success, or the appropriate error code.
136
PJ_DECL(pj_status_t) pj_stun_client_tsx_create( pj_stun_config *cfg,
138
pj_grp_lock_t *grp_lock,
139
const pj_stun_tsx_cb *cb,
140
pj_stun_client_tsx **p_tsx);
143
* Schedule timer to destroy the transaction after the transaction is
144
* complete. Application normally calls this function in the on_complete()
145
* callback. When this timer elapsed, the on_destroy() callback will be
148
* This is convenient to let the STUN transaction absorbs any response
149
* for the previous request retransmissions. If application doesn't want
150
* this, it can destroy the transaction immediately by calling
151
* #pj_stun_client_tsx_destroy().
153
* @param tsx The STUN transaction.
154
* @param delay The delay interval before on_destroy() callback
157
* @return PJ_SUCCESS on success, or the appropriate error code.
160
pj_stun_client_tsx_schedule_destroy(pj_stun_client_tsx *tsx,
161
const pj_time_val *delay);
165
* Stop the client transaction.
167
* @param tsx The STUN transaction.
169
* @return PJ_SUCCESS on success or PJ_EINVAL if the parameter
172
PJ_DECL(pj_status_t) pj_stun_client_tsx_stop(pj_stun_client_tsx *tsx);
176
* Check if transaction has completed.
178
* @param tsx The STUN transaction.
180
* @return Non-zero if transaction has completed.
182
PJ_DECL(pj_bool_t) pj_stun_client_tsx_is_complete(pj_stun_client_tsx *tsx);
186
* Associate an arbitrary data with the STUN transaction. This data
187
* can be then retrieved later from the transaction, by using
188
* pj_stun_client_tsx_get_data() function.
190
* @param tsx The STUN client transaction.
191
* @param data Application data to be associated with the
194
* @return PJ_SUCCESS on success.
196
PJ_DECL(pj_status_t) pj_stun_client_tsx_set_data(pj_stun_client_tsx *tsx,
201
* Get the user data that was previously associated with the STUN
204
* @param tsx The STUN client transaction.
206
* @return The user data.
208
PJ_DECL(void*) pj_stun_client_tsx_get_data(pj_stun_client_tsx *tsx);
212
* Start the STUN client transaction by sending STUN request using
213
* this transaction. If reliable transport such as TCP or TLS is used,
214
* the retransmit flag should be set to PJ_FALSE because reliablity
215
* will be assured by the transport layer.
217
* @param tsx The STUN client transaction.
218
* @param retransmit Should this message be retransmitted by the
220
* @param pkt The STUN packet to send.
221
* @param pkt_len Length of STUN packet.
223
* @return PJ_SUCCESS on success, or PJNATH_ESTUNDESTROYED
224
* when the user has destroyed the transaction in
225
* \a on_send_msg() callback, or any other error code
226
* as returned by \a on_send_msg() callback.
228
PJ_DECL(pj_status_t) pj_stun_client_tsx_send_msg(pj_stun_client_tsx *tsx,
229
pj_bool_t retransmit,
234
* Request to retransmit the request. Normally application should not need
235
* to call this function since retransmission would be handled internally,
236
* but this functionality is needed by ICE.
238
* @param tsx The STUN client transaction instance.
239
* @param mod_count Boolean flag to indicate whether transmission count
240
* needs to be incremented.
242
* @return PJ_SUCCESS on success, or PJNATH_ESTUNDESTROYED
243
* when the user has destroyed the transaction in
244
* \a on_send_msg() callback, or any other error code
245
* as returned by \a on_send_msg() callback.
247
PJ_DECL(pj_status_t) pj_stun_client_tsx_retransmit(pj_stun_client_tsx *tsx,
248
pj_bool_t mod_count);
252
* Notify the STUN transaction about the arrival of STUN response.
253
* If the STUN response contains a final error (300 and greater), the
254
* transaction will be terminated and callback will be called. If the
255
* STUN response contains response code 100-299, retransmission
256
* will cease, but application must still call this function again
257
* with a final response later to allow the transaction to complete.
259
* @param tsx The STUN client transaction instance.
260
* @param msg The incoming STUN message.
261
* @param src_addr The source address of the packet.
262
* @param src_addr_len The length of the source address.
264
* @return PJ_SUCCESS on success or the appropriate error code.
266
PJ_DECL(pj_status_t) pj_stun_client_tsx_on_rx_msg(pj_stun_client_tsx *tsx,
267
const pj_stun_msg *msg,
268
const pj_sockaddr_t*src_addr,
269
unsigned src_addr_len);
280
#endif /* __PJNATH_STUN_TRANSACTION_H__ */