1
/* $Id: stun_transaction.h 3553 2011-05-05 06:14:19Z nanang $ */
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>
35
/* **************************************************************************/
37
* @defgroup PJNATH_STUN_TRANSACTION STUN Client Transaction
38
* @brief STUN client transaction
39
* @ingroup PJNATH_STUN_BASE
42
The @ref PJNATH_STUN_TRANSACTION is used to manage outgoing STUN request,
43
for example to retransmit the request and to notify application about the
44
completion of the request.
46
The @ref PJNATH_STUN_TRANSACTION does not use any networking operations,
47
but instead application must supply the transaction with a callback to
48
be used by the transaction to send outgoing requests. This way the STUN
49
transaction is made more generic and can work with different types of
50
networking codes in application.
56
* Opaque declaration of STUN client transaction.
58
typedef struct pj_stun_client_tsx pj_stun_client_tsx;
61
* STUN client transaction callback.
63
typedef struct pj_stun_tsx_cb
66
* This callback is called when the STUN transaction completed.
68
* @param tsx The STUN transaction.
69
* @param status Status of the transaction. Status PJ_SUCCESS
70
* means that the request has received a successful
72
* @param response The STUN response, which value may be NULL if
73
* \a status is not PJ_SUCCESS.
74
* @param src_addr The source address of the response, if response
76
* @param src_addr_len The length of the source address.
78
void (*on_complete)(pj_stun_client_tsx *tsx,
80
const pj_stun_msg *response,
81
const pj_sockaddr_t *src_addr,
82
unsigned src_addr_len);
85
* This callback is called by the STUN transaction when it wants to send
88
* @param tsx The STUN transaction instance.
89
* @param stun_pkt The STUN packet to be sent.
90
* @param pkt_size Size of the STUN packet.
92
* @return If return value of the callback is not PJ_SUCCESS,
93
* the transaction will fail. Application MUST return
94
* PJNATH_ESTUNDESTROYED if it has destroyed the
95
* transaction in this callback.
97
pj_status_t (*on_send_msg)(pj_stun_client_tsx *tsx,
102
* This callback is called after the timer that was scheduled by
103
* #pj_stun_client_tsx_schedule_destroy() has elapsed. Application
104
* should call #pj_stun_client_tsx_destroy() upon receiving this
107
* This callback is optional if application will not call
108
* #pj_stun_client_tsx_schedule_destroy().
110
* @param tsx The STUN transaction instance.
112
void (*on_destroy)(pj_stun_client_tsx *tsx);
119
* Create an instance of STUN client transaction. The STUN client
120
* transaction is used to transmit outgoing STUN request and to
121
* ensure the reliability of the request by periodically retransmitting
122
* the request, if necessary.
124
* @param cfg The STUN endpoint, which will be used to retrieve
125
* various settings for the transaction.
126
* @param pool Pool to be used to allocate memory from.
127
* @param cb Callback structure, to be used by the transaction
128
* to send message and to notify the application about
129
* the completion of the transaction.
130
* @param p_tsx Pointer to receive the transaction instance.
132
* @return PJ_SUCCESS on success, or the appropriate error code.
134
PJ_DECL(pj_status_t) pj_stun_client_tsx_create( pj_stun_config *cfg,
136
const pj_stun_tsx_cb *cb,
137
pj_stun_client_tsx **p_tsx);
140
* Schedule timer to destroy the transaction after the transaction is
141
* complete. Application normally calls this function in the on_complete()
142
* callback. When this timer elapsed, the on_destroy() callback will be
145
* This is convenient to let the STUN transaction absorbs any response
146
* for the previous request retransmissions. If application doesn't want
147
* this, it can destroy the transaction immediately by calling
148
* #pj_stun_client_tsx_destroy().
150
* @param tsx The STUN transaction.
151
* @param delay The delay interval before on_destroy() callback
154
* @return PJ_SUCCESS on success, or the appropriate error code.
157
pj_stun_client_tsx_schedule_destroy(pj_stun_client_tsx *tsx,
158
const pj_time_val *delay);
162
* Destroy a STUN client transaction immediately. This function can be
163
* called at any time to stop the transaction and destroy it.
165
* @param tsx The STUN transaction.
167
* @return PJ_SUCCESS on success or PJ_EINVAL if the parameter
170
PJ_DECL(pj_status_t) pj_stun_client_tsx_destroy(pj_stun_client_tsx *tsx);
174
* Check if transaction has completed.
176
* @param tsx The STUN transaction.
178
* @return Non-zero if transaction has completed.
180
PJ_DECL(pj_bool_t) pj_stun_client_tsx_is_complete(pj_stun_client_tsx *tsx);
184
* Associate an arbitrary data with the STUN transaction. This data
185
* can be then retrieved later from the transaction, by using
186
* pj_stun_client_tsx_get_data() function.
188
* @param tsx The STUN client transaction.
189
* @param data Application data to be associated with the
192
* @return PJ_SUCCESS on success.
194
PJ_DECL(pj_status_t) pj_stun_client_tsx_set_data(pj_stun_client_tsx *tsx,
199
* Get the user data that was previously associated with the STUN
202
* @param tsx The STUN client transaction.
204
* @return The user data.
206
PJ_DECL(void*) pj_stun_client_tsx_get_data(pj_stun_client_tsx *tsx);
210
* Start the STUN client transaction by sending STUN request using
211
* this transaction. If reliable transport such as TCP or TLS is used,
212
* the retransmit flag should be set to PJ_FALSE because reliablity
213
* will be assured by the transport layer.
215
* @param tsx The STUN client transaction.
216
* @param retransmit Should this message be retransmitted by the
218
* @param pkt The STUN packet to send.
219
* @param pkt_len Length of STUN packet.
221
* @return PJ_SUCCESS on success, or PJNATH_ESTUNDESTROYED
222
* when the user has destroyed the transaction in
223
* \a on_send_msg() callback, or any other error code
224
* as returned by \a on_send_msg() callback.
226
PJ_DECL(pj_status_t) pj_stun_client_tsx_send_msg(pj_stun_client_tsx *tsx,
227
pj_bool_t retransmit,
232
* Request to retransmit the request. Normally application should not need
233
* to call this function since retransmission would be handled internally,
234
* but this functionality is needed by ICE.
236
* @param tsx The STUN client transaction instance.
238
* @return PJ_SUCCESS on success, or PJNATH_ESTUNDESTROYED
239
* when the user has destroyed the transaction in
240
* \a on_send_msg() callback, or any other error code
241
* as returned by \a on_send_msg() callback.
243
PJ_DECL(pj_status_t) pj_stun_client_tsx_retransmit(pj_stun_client_tsx *tsx);
247
* Notify the STUN transaction about the arrival of STUN response.
248
* If the STUN response contains a final error (300 and greater), the
249
* transaction will be terminated and callback will be called. If the
250
* STUN response contains response code 100-299, retransmission
251
* will cease, but application must still call this function again
252
* with a final response later to allow the transaction to complete.
254
* @param tsx The STUN client transaction instance.
255
* @param msg The incoming STUN message.
256
* @param src_addr The source address of the packet.
257
* @param src_addr_len The length of the source address.
259
* @return PJ_SUCCESS on success or the appropriate error code.
261
PJ_DECL(pj_status_t) pj_stun_client_tsx_on_rx_msg(pj_stun_client_tsx *tsx,
262
const pj_stun_msg *msg,
263
const pj_sockaddr_t*src_addr,
264
unsigned src_addr_len);
275
#endif /* __PJNATH_STUN_TRANSACTION_H__ */