stun_transaction.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /*
  2. * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
  3. * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #ifndef __PJNATH_STUN_TRANSACTION_H__
  20. #define __PJNATH_STUN_TRANSACTION_H__
  21. /**
  22. * @file stun_transaction.h
  23. * @brief STUN transaction
  24. */
  25. #include <pjnath/stun_msg.h>
  26. #include <pjnath/stun_config.h>
  27. #include <pj/lock.h>
  28. PJ_BEGIN_DECL
  29. /* **************************************************************************/
  30. /**
  31. * @defgroup PJNATH_STUN_TRANSACTION STUN Client Transaction
  32. * @brief STUN client transaction
  33. * @ingroup PJNATH_STUN_BASE
  34. * @{
  35. *
  36. The @ref PJNATH_STUN_TRANSACTION is used to manage outgoing STUN request,
  37. for example to retransmit the request and to notify application about the
  38. completion of the request.
  39. The @ref PJNATH_STUN_TRANSACTION does not use any networking operations,
  40. but instead application must supply the transaction with a callback to
  41. be used by the transaction to send outgoing requests. This way the STUN
  42. transaction is made more generic and can work with different types of
  43. networking codes in application.
  44. */
  45. /**
  46. * Opaque declaration of STUN client transaction.
  47. */
  48. typedef struct pj_stun_client_tsx pj_stun_client_tsx;
  49. /**
  50. * STUN client transaction callback.
  51. */
  52. typedef struct pj_stun_tsx_cb
  53. {
  54. /**
  55. * This callback is called when the STUN transaction completed.
  56. *
  57. * @param tsx The STUN transaction.
  58. * @param status Status of the transaction. Status PJ_SUCCESS
  59. * means that the request has received a successful
  60. * response.
  61. * @param response The STUN response, which value may be NULL if
  62. * \a status is not PJ_SUCCESS.
  63. * @param src_addr The source address of the response, if response
  64. * is not NULL.
  65. * @param src_addr_len The length of the source address.
  66. */
  67. void (*on_complete)(pj_stun_client_tsx *tsx,
  68. pj_status_t status,
  69. const pj_stun_msg *response,
  70. const pj_sockaddr_t *src_addr,
  71. unsigned src_addr_len);
  72. /**
  73. * This callback is called by the STUN transaction when it wants to send
  74. * outgoing message.
  75. *
  76. * @param tsx The STUN transaction instance.
  77. * @param stun_pkt The STUN packet to be sent.
  78. * @param pkt_size Size of the STUN packet.
  79. *
  80. * @return If return value of the callback is not PJ_SUCCESS,
  81. * the transaction will fail. Application MUST return
  82. * PJNATH_ESTUNDESTROYED if it has destroyed the
  83. * transaction in this callback.
  84. */
  85. pj_status_t (*on_send_msg)(pj_stun_client_tsx *tsx,
  86. const void *stun_pkt,
  87. pj_size_t pkt_size);
  88. /**
  89. * This callback is called after the timer that was scheduled by
  90. * #pj_stun_client_tsx_schedule_destroy() has elapsed. Application
  91. * should call #pj_stun_client_tsx_destroy() upon receiving this
  92. * callback.
  93. *
  94. * This callback is optional if application will not call
  95. * #pj_stun_client_tsx_schedule_destroy().
  96. *
  97. * @param tsx The STUN transaction instance.
  98. */
  99. void (*on_destroy)(pj_stun_client_tsx *tsx);
  100. } pj_stun_tsx_cb;
  101. /**
  102. * Create an instance of STUN client transaction. The STUN client
  103. * transaction is used to transmit outgoing STUN request and to
  104. * ensure the reliability of the request by periodically retransmitting
  105. * the request, if necessary.
  106. *
  107. * @param cfg The STUN endpoint, which will be used to retrieve
  108. * various settings for the transaction.
  109. * @param pool Pool to be used to allocate memory from.
  110. * @param grp_lock Group lock to synchronize.
  111. * @param cb Callback structure, to be used by the transaction
  112. * to send message and to notify the application about
  113. * the completion of the transaction.
  114. * @param p_tsx Pointer to receive the transaction instance.
  115. *
  116. * @return PJ_SUCCESS on success, or the appropriate error code.
  117. */
  118. PJ_DECL(pj_status_t) pj_stun_client_tsx_create( pj_stun_config *cfg,
  119. pj_pool_t *pool,
  120. pj_grp_lock_t *grp_lock,
  121. const pj_stun_tsx_cb *cb,
  122. pj_stun_client_tsx **p_tsx);
  123. /**
  124. * Schedule timer to destroy the transaction after the transaction is
  125. * complete. Application normally calls this function in the on_complete()
  126. * callback. When this timer elapsed, the on_destroy() callback will be
  127. * called.
  128. *
  129. * This is convenient to let the STUN transaction absorbs any response
  130. * for the previous request retransmissions. If application doesn't want
  131. * this, it can destroy the transaction immediately by calling
  132. * #pj_stun_client_tsx_destroy().
  133. *
  134. * @param tsx The STUN transaction.
  135. * @param delay The delay interval before on_destroy() callback
  136. * is called.
  137. *
  138. * @return PJ_SUCCESS on success, or the appropriate error code.
  139. */
  140. PJ_DECL(pj_status_t)
  141. pj_stun_client_tsx_schedule_destroy(pj_stun_client_tsx *tsx,
  142. const pj_time_val *delay);
  143. /**
  144. * Destroy the STUN transaction immediately after the transaction is complete.
  145. * Application normally calls this function in the on_complete() callback.
  146. *
  147. * @param tsx The STUN transaction.
  148. *
  149. * @return PJ_SUCCESS on success, or the appropriate error code.
  150. */
  151. PJ_DECL(pj_status_t) pj_stun_client_tsx_destroy(pj_stun_client_tsx *tsx);
  152. /**
  153. * Stop the client transaction.
  154. *
  155. * @param tsx The STUN transaction.
  156. *
  157. * @return PJ_SUCCESS on success or PJ_EINVAL if the parameter
  158. * is NULL.
  159. */
  160. PJ_DECL(pj_status_t) pj_stun_client_tsx_stop(pj_stun_client_tsx *tsx);
  161. /**
  162. * Check if transaction has completed.
  163. *
  164. * @param tsx The STUN transaction.
  165. *
  166. * @return Non-zero if transaction has completed.
  167. */
  168. PJ_DECL(pj_bool_t) pj_stun_client_tsx_is_complete(pj_stun_client_tsx *tsx);
  169. /**
  170. * Associate an arbitrary data with the STUN transaction. This data
  171. * can be then retrieved later from the transaction, by using
  172. * pj_stun_client_tsx_get_data() function.
  173. *
  174. * @param tsx The STUN client transaction.
  175. * @param data Application data to be associated with the
  176. * STUN transaction.
  177. *
  178. * @return PJ_SUCCESS on success.
  179. */
  180. PJ_DECL(pj_status_t) pj_stun_client_tsx_set_data(pj_stun_client_tsx *tsx,
  181. void *data);
  182. /**
  183. * Get the user data that was previously associated with the STUN
  184. * transaction.
  185. *
  186. * @param tsx The STUN client transaction.
  187. *
  188. * @return The user data.
  189. */
  190. PJ_DECL(void*) pj_stun_client_tsx_get_data(pj_stun_client_tsx *tsx);
  191. /**
  192. * Start the STUN client transaction by sending STUN request using
  193. * this transaction. If reliable transport such as TCP or TLS is used,
  194. * the retransmit flag should be set to PJ_FALSE because reliablity
  195. * will be assured by the transport layer.
  196. *
  197. * @param tsx The STUN client transaction.
  198. * @param retransmit Should this message be retransmitted by the
  199. * STUN transaction.
  200. * @param pkt The STUN packet to send.
  201. * @param pkt_len Length of STUN packet.
  202. *
  203. * @return PJ_SUCCESS on success, or PJNATH_ESTUNDESTROYED
  204. * when the user has destroyed the transaction in
  205. * \a on_send_msg() callback, or any other error code
  206. * as returned by \a on_send_msg() callback.
  207. */
  208. PJ_DECL(pj_status_t) pj_stun_client_tsx_send_msg(pj_stun_client_tsx *tsx,
  209. pj_bool_t retransmit,
  210. void *pkt,
  211. unsigned pkt_len);
  212. /**
  213. * Request to retransmit the request. Normally application should not need
  214. * to call this function since retransmission would be handled internally,
  215. * but this functionality is needed by ICE.
  216. *
  217. * @param tsx The STUN client transaction instance.
  218. * @param mod_count Boolean flag to indicate whether transmission count
  219. * needs to be incremented.
  220. *
  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.
  225. */
  226. PJ_DECL(pj_status_t) pj_stun_client_tsx_retransmit(pj_stun_client_tsx *tsx,
  227. pj_bool_t mod_count);
  228. /**
  229. * Notify the STUN transaction about the arrival of STUN response.
  230. * If the STUN response contains a final error (300 and greater), the
  231. * transaction will be terminated and callback will be called. If the
  232. * STUN response contains response code 100-299, retransmission
  233. * will cease, but application must still call this function again
  234. * with a final response later to allow the transaction to complete.
  235. *
  236. * @param tsx The STUN client transaction instance.
  237. * @param msg The incoming STUN message.
  238. * @param src_addr The source address of the packet.
  239. * @param src_addr_len The length of the source address.
  240. *
  241. * @return PJ_SUCCESS on success or the appropriate error code.
  242. */
  243. PJ_DECL(pj_status_t) pj_stun_client_tsx_on_rx_msg(pj_stun_client_tsx *tsx,
  244. const pj_stun_msg *msg,
  245. const pj_sockaddr_t*src_addr,
  246. unsigned src_addr_len);
  247. /**
  248. * @}
  249. */
  250. PJ_END_DECL
  251. #endif /* __PJNATH_STUN_TRANSACTION_H__ */