PageRenderTime 51ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/pjsip_android/apps/pjsip/project/pjnath/include/pjnath/turn_sock.h

http://csipsimple.googlecode.com/
C Header | 397 lines | 63 code | 43 blank | 291 comment | 0 complexity | 253549665d5bcc95a63a684f37523904 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-3.0, GPL-2.0, BSD-3-Clause, LGPL-2.1
  1. /* $Id: turn_sock.h 3553 2011-05-05 06:14:19Z nanang $ */
  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_TURN_SOCK_H__
  21. #define __PJNATH_TURN_SOCK_H__
  22. /**
  23. * @file turn_sock.h
  24. * @brief TURN relay using UDP client as transport protocol
  25. */
  26. #include <pjnath/turn_session.h>
  27. #include <pj/sock_qos.h>
  28. PJ_BEGIN_DECL
  29. /* **************************************************************************/
  30. /**
  31. @addtogroup PJNATH_TURN_SOCK
  32. @{
  33. This is a ready to use object for relaying application data via a TURN server,
  34. by managing all the operations in \ref turn_op_sec.
  35. \section turnsock_using_sec Using TURN transport
  36. This object provides a thin wrapper to the \ref PJNATH_TURN_SESSION, hence the
  37. API is very much the same (apart from the obvious difference in the names).
  38. Please see \ref PJNATH_TURN_SESSION for the documentation on how to use the
  39. session.
  40. \section turnsock_samples_sec Samples
  41. The \ref turn_client_sample is a sample application to use the
  42. \ref PJNATH_TURN_SOCK.
  43. Also see <b>\ref samples_page</b> for other samples.
  44. */
  45. /**
  46. * Opaque declaration for TURN client.
  47. */
  48. typedef struct pj_turn_sock pj_turn_sock;
  49. /**
  50. * This structure contains callbacks that will be called by the TURN
  51. * transport.
  52. */
  53. typedef struct pj_turn_sock_cb
  54. {
  55. /**
  56. * Notification when incoming data has been received from the remote
  57. * peer via the TURN server. The data reported in this callback will
  58. * be the exact data as sent by the peer (e.g. the TURN encapsulation
  59. * such as Data Indication or ChannelData will be removed before this
  60. * function is called).
  61. *
  62. * @param turn_sock The TURN client transport.
  63. * @param data The data as received from the peer.
  64. * @param data_len Length of the data.
  65. * @param peer_addr The peer address.
  66. * @param addr_len The length of the peer address.
  67. */
  68. void (*on_rx_data)(pj_turn_sock *turn_sock,
  69. void *pkt,
  70. unsigned pkt_len,
  71. const pj_sockaddr_t *peer_addr,
  72. unsigned addr_len);
  73. /**
  74. * Notification when TURN session state has changed. Application should
  75. * implement this callback to monitor the progress of the TURN session.
  76. *
  77. * @param turn_sock The TURN client transport.
  78. * @param old_state Previous state.
  79. * @param new_state Current state.
  80. */
  81. void (*on_state)(pj_turn_sock *turn_sock,
  82. pj_turn_state_t old_state,
  83. pj_turn_state_t new_state);
  84. } pj_turn_sock_cb;
  85. /**
  86. * This structure describes options that can be specified when creating
  87. * the TURN socket. Application should call #pj_turn_sock_cfg_default()
  88. * to initialize this structure with its default values before using it.
  89. */
  90. typedef struct pj_turn_sock_cfg
  91. {
  92. /**
  93. * QoS traffic type to be set on this transport. When application wants
  94. * to apply QoS tagging to the transport, it's preferable to set this
  95. * field rather than \a qos_param fields since this is more portable.
  96. *
  97. * Default value is PJ_QOS_TYPE_BEST_EFFORT.
  98. */
  99. pj_qos_type qos_type;
  100. /**
  101. * Set the low level QoS parameters to the transport. This is a lower
  102. * level operation than setting the \a qos_type field and may not be
  103. * supported on all platforms.
  104. *
  105. * By default all settings in this structure are not set.
  106. */
  107. pj_qos_params qos_params;
  108. /**
  109. * Specify if STUN socket should ignore any errors when setting the QoS
  110. * traffic type/parameters.
  111. *
  112. * Default: PJ_TRUE
  113. */
  114. pj_bool_t qos_ignore_error;
  115. } pj_turn_sock_cfg;
  116. /**
  117. * Initialize pj_turn_sock_cfg structure with default values.
  118. */
  119. PJ_DECL(void) pj_turn_sock_cfg_default(pj_turn_sock_cfg *cfg);
  120. /**
  121. * Create a TURN transport instance with the specified address family and
  122. * connection type. Once TURN transport instance is created, application
  123. * must call pj_turn_sock_alloc() to allocate a relay address in the TURN
  124. * server.
  125. *
  126. * @param cfg The STUN configuration which contains among other
  127. * things the ioqueue and timer heap instance for
  128. * the operation of this transport.
  129. * @param af Address family of the client connection. Currently
  130. * pj_AF_INET() and pj_AF_INET6() are supported.
  131. * @param conn_type Connection type to the TURN server. Both TCP and
  132. * UDP are supported.
  133. * @param cb Callback to receive events from the TURN transport.
  134. * @param setting Optional settings to be specified to the transport.
  135. * If this parameter is NULL, default values will be
  136. * used.
  137. * @param user_data Arbitrary application data to be associated with
  138. * this transport.
  139. * @param p_turn_sock Pointer to receive the created instance of the
  140. * TURN transport.
  141. *
  142. * @return PJ_SUCCESS if the operation has been successful,
  143. * or the appropriate error code on failure.
  144. */
  145. PJ_DECL(pj_status_t) pj_turn_sock_create(pj_stun_config *cfg,
  146. int af,
  147. pj_turn_tp_type conn_type,
  148. const pj_turn_sock_cb *cb,
  149. const pj_turn_sock_cfg *setting,
  150. void *user_data,
  151. pj_turn_sock **p_turn_sock);
  152. /**
  153. * Destroy the TURN transport instance. This will gracefully close the
  154. * connection between the client and the TURN server. Although this
  155. * function will return immediately, the TURN socket deletion may continue
  156. * in the background and the application may still get state changes
  157. * notifications from this transport.
  158. *
  159. * @param turn_sock The TURN transport instance.
  160. */
  161. PJ_DECL(void) pj_turn_sock_destroy(pj_turn_sock *turn_sock);
  162. /**
  163. * Associate a user data with this TURN transport. The user data may then
  164. * be retrieved later with #pj_turn_sock_get_user_data().
  165. *
  166. * @param turn_sock The TURN transport instance.
  167. * @param user_data Arbitrary data.
  168. *
  169. * @return PJ_SUCCESS if the operation has been successful,
  170. * or the appropriate error code on failure.
  171. */
  172. PJ_DECL(pj_status_t) pj_turn_sock_set_user_data(pj_turn_sock *turn_sock,
  173. void *user_data);
  174. /**
  175. * Retrieve the previously assigned user data associated with this TURN
  176. * transport.
  177. *
  178. * @param turn_sock The TURN transport instance.
  179. *
  180. * @return The user/application data.
  181. */
  182. PJ_DECL(void*) pj_turn_sock_get_user_data(pj_turn_sock *turn_sock);
  183. /**
  184. * Get the TURN transport info. The transport info contains, among other
  185. * things, the allocated relay address.
  186. *
  187. * @param turn_sock The TURN transport instance.
  188. * @param info Pointer to be filled with TURN transport info.
  189. *
  190. * @return PJ_SUCCESS if the operation has been successful,
  191. * or the appropriate error code on failure.
  192. */
  193. PJ_DECL(pj_status_t) pj_turn_sock_get_info(pj_turn_sock *turn_sock,
  194. pj_turn_session_info *info);
  195. /**
  196. * Acquire the internal mutex of the TURN transport. Application may need
  197. * to call this function to synchronize access to other objects alongside
  198. * the TURN transport, to avoid deadlock.
  199. *
  200. * @param turn_sock The TURN transport instance.
  201. *
  202. * @return PJ_SUCCESS if the operation has been successful,
  203. * or the appropriate error code on failure.
  204. */
  205. PJ_DECL(pj_status_t) pj_turn_sock_lock(pj_turn_sock *turn_sock);
  206. /**
  207. * Release the internal mutex previously held with pj_turn_sock_lock().
  208. *
  209. * @param turn_sock The TURN transport instance.
  210. *
  211. * @return PJ_SUCCESS if the operation has been successful,
  212. * or the appropriate error code on failure.
  213. */
  214. PJ_DECL(pj_status_t) pj_turn_sock_unlock(pj_turn_sock *turn_sock);
  215. /**
  216. * Set STUN message logging for this TURN session.
  217. * See #pj_stun_session_set_log().
  218. *
  219. * @param turn_sock The TURN transport instance.
  220. * @param flags Bitmask combination of #pj_stun_sess_msg_log_flag
  221. */
  222. PJ_DECL(void) pj_turn_sock_set_log(pj_turn_sock *turn_sock,
  223. unsigned flags);
  224. /**
  225. * Configure the SOFTWARE name to be sent in all STUN requests by the
  226. * TURN session.
  227. *
  228. * @param turn_sock The TURN transport instance.
  229. * @param sw Software name string. If this argument is NULL or
  230. * empty, the session will not include SOFTWARE attribute
  231. * in STUN requests and responses.
  232. *
  233. * @return PJ_SUCCESS on success, or the appropriate error code.
  234. */
  235. PJ_DECL(pj_status_t) pj_turn_sock_set_software_name(pj_turn_sock *turn_sock,
  236. const pj_str_t *sw);
  237. /**
  238. * Allocate a relay address/resource in the TURN server. This function
  239. * will resolve the TURN server using DNS SRV (if desired) and send TURN
  240. * \a Allocate request using the specified credential to allocate a relay
  241. * address in the server. This function completes asynchronously, and
  242. * application will be notified when the allocation process has been
  243. * successful in the \a on_state() callback when the state is set to
  244. * PJ_TURN_STATE_READY. If the allocation fails, the state will be set
  245. * to PJ_TURN_STATE_DEALLOCATING or greater.
  246. *
  247. * @param turn_sock The TURN transport instance.
  248. * @param domain The domain, hostname, or IP address of the TURN
  249. * server. When this parameter contains domain name,
  250. * the \a resolver parameter must be set to activate
  251. * DNS SRV resolution.
  252. * @param default_port The default TURN port number to use when DNS SRV
  253. * resolution is not used. If DNS SRV resolution is
  254. * used, the server port number will be set from the
  255. * DNS SRV records.
  256. * @param resolver If this parameter is not NULL, then the \a domain
  257. * parameter will be first resolved with DNS SRV and
  258. * then fallback to using DNS A/AAAA resolution when
  259. * DNS SRV resolution fails. If this parameter is
  260. * NULL, the \a domain parameter will be resolved as
  261. * hostname.
  262. * @param cred The STUN credential to be used for the TURN server.
  263. * @param param Optional TURN allocation parameter.
  264. *
  265. * @return PJ_SUCCESS if the operation has been successfully
  266. * queued, or the appropriate error code on failure.
  267. * When this function returns PJ_SUCCESS, the final
  268. * result of the allocation process will be notified
  269. * to application in \a on_state() callback.
  270. *
  271. */
  272. PJ_DECL(pj_status_t) pj_turn_sock_alloc(pj_turn_sock *turn_sock,
  273. const pj_str_t *domain,
  274. int default_port,
  275. pj_dns_resolver *resolver,
  276. const pj_stun_auth_cred *cred,
  277. const pj_turn_alloc_param *param);
  278. /**
  279. * Create or renew permission in the TURN server for the specified peer IP
  280. * addresses. Application must install permission for a particular (peer)
  281. * IP address before it sends any data to that IP address, or otherwise
  282. * the TURN server will drop the data.
  283. *
  284. * @param turn_sock The TURN transport instance.
  285. * @param addr_cnt Number of IP addresses.
  286. * @param addr Array of peer IP addresses. Only the address family
  287. * and IP address portion of the socket address matter.
  288. * @param options Specify 1 to let the TURN client session automatically
  289. * renew the permission later when they are about to
  290. * expire.
  291. *
  292. * @return PJ_SUCCESS if the operation has been successfully
  293. * issued, or the appropriate error code. Note that
  294. * the operation itself will complete asynchronously.
  295. */
  296. PJ_DECL(pj_status_t) pj_turn_sock_set_perm(pj_turn_sock *turn_sock,
  297. unsigned addr_cnt,
  298. const pj_sockaddr addr[],
  299. unsigned options);
  300. /**
  301. * Send a data to the specified peer address via the TURN relay. This
  302. * function will encapsulate the data as STUN Send Indication or TURN
  303. * ChannelData packet and send the message to the TURN server. The TURN
  304. * server then will send the data to the peer.
  305. *
  306. * The allocation (pj_turn_sock_alloc()) must have been successfully
  307. * created before application can relay any data.
  308. *
  309. * @param turn_sock The TURN transport instance.
  310. * @param pkt The data/packet to be sent to peer.
  311. * @param pkt_len Length of the data.
  312. * @param peer_addr The remote peer address (the ultimate destination
  313. * of the data, and not the TURN server address).
  314. * @param addr_len Length of the address.
  315. *
  316. * @return PJ_SUCCESS if the operation has been successful,
  317. * or the appropriate error code on failure.
  318. */
  319. PJ_DECL(pj_status_t) pj_turn_sock_sendto(pj_turn_sock *turn_sock,
  320. const pj_uint8_t *pkt,
  321. unsigned pkt_len,
  322. const pj_sockaddr_t *peer_addr,
  323. unsigned addr_len);
  324. /**
  325. * Optionally establish channel binding for the specified a peer address.
  326. * This function will assign a unique channel number for the peer address
  327. * and request channel binding to the TURN server for this address. When
  328. * a channel has been bound to a peer, the TURN transport and TURN server
  329. * will exchange data using ChannelData encapsulation format, which has
  330. * lower bandwidth overhead than Send Indication (the default format used
  331. * when peer address is not bound to a channel).
  332. *
  333. * @param turn_sock The TURN transport instance.
  334. * @param peer The remote peer address.
  335. * @param addr_len Length of the address.
  336. *
  337. * @return PJ_SUCCESS if the operation has been successful,
  338. * or the appropriate error code on failure.
  339. */
  340. PJ_DECL(pj_status_t) pj_turn_sock_bind_channel(pj_turn_sock *turn_sock,
  341. const pj_sockaddr_t *peer,
  342. unsigned addr_len);
  343. /**
  344. * @}
  345. */
  346. PJ_END_DECL
  347. #endif /* __PJNATH_TURN_SOCK_H__ */