turn_session.h 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968
  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_TURN_SESSION_H__
  20. #define __PJNATH_TURN_SESSION_H__
  21. /**
  22. * @file turn_session.h
  23. * @brief Transport independent TURN client session.
  24. */
  25. #include <pjnath/stun_session.h>
  26. #include <pjlib-util/resolver.h>
  27. PJ_BEGIN_DECL
  28. /* **************************************************************************/
  29. /**
  30. @addtogroup PJNATH_TURN_SESSION
  31. @{
  32. The \ref PJNATH_TURN_SESSION is a transport-independent object to
  33. manage a client TURN session. It contains the core logic for manage
  34. the TURN client session as listed in \ref turn_op_sec, but
  35. in transport-independent manner (i.e. it doesn't have a socket), so
  36. that developer can integrate TURN client functionality into existing
  37. framework that already has its own means to send and receive data,
  38. or to support new transport types to TURN, such as TLS.
  39. \section turn_sess_using_sec Using the TURN session
  40. These steps describes how to use the TURN session:
  41. - <b>Creating the session</b>:\n
  42. use #pj_turn_session_create() to create the session.
  43. - <b>Configuring credential</b>:\n
  44. all TURN operations requires the use of authentication (it uses STUN
  45. long term autentication method). Use #pj_turn_session_set_credential()
  46. to configure the TURN credential to be used by the session.
  47. - <b>Configuring server</b>:\n
  48. application must call #pj_turn_session_set_server() before it can send
  49. Allocate request (with pj_turn_session_alloc()). This function will
  50. resolve the TURN server using DNS SRV resolution if the \a resolver
  51. is set. The server resolution process will complete asynchronously,
  52. and application will be notified in \a on_state() callback of the
  53. #pj_turn_session_cb structurewith the session state set to
  54. PJ_TURN_STATE_RESOLVED.
  55. - <b>Creating allocation</b>:\n
  56. create one "relay port" (or called <b>relayed-transport-address</b>
  57. in TURN terminology) in the TURN server by using #pj_turn_session_alloc().
  58. This will send Allocate request to the server. This function will complete
  59. immediately, and application will be notified about the allocation
  60. result in the \a on_state() callback of the #pj_turn_session_cb structure.
  61. - <b>Getting the allocation result</b>:\n
  62. if allocation is successful, the session state will progress to
  63. \a PJ_TURN_STATE_READY, otherwise the state will be
  64. \a PJ_TURN_STATE_DEALLOCATED or higher. Session state progression is
  65. reported in the \a on_state() callback of the #pj_turn_session_cb
  66. structure. On successful allocation, application may retrieve the
  67. allocation info by calling #pj_turn_session_get_info().
  68. - <b>Sending data through the relay</b>.\n
  69. Once allocation has been created, client may send data to any remote
  70. endpoints (called peers in TURN terminology) via the "relay port". It does
  71. so by calling #pj_turn_session_sendto(), giving the peer address
  72. in the function argument. But note that at this point peers are not allowed
  73. to send data towards the client (via the "relay port") before permission is
  74. installed for that peer.
  75. - <b>Creating permissions</b>.\n
  76. Permission needs to be created in the TURN server so that a peer can send
  77. data to the client via the relay port (a peer in this case is identified by
  78. its IP address). Without this, when the TURN server receives data from the
  79. peer in the "relay port", it will drop this data. Create the permission by
  80. calling #pj_turn_session_set_perm(), specifying the peer IP address in the
  81. argument (the port part of the address is ignored). More than one IP
  82. addresses may be specified.
  83. - <b>Receiving data from peers</b>.\n
  84. Once permission has been installed for the peer, any data received by the
  85. TURN server (from that peer) in the "relay port" will be relayed back to
  86. client by the server, and application will be notified via \a on_rx_data
  87. callback of the #pj_turn_session_cb.
  88. - <b>Using ChannelData</b>.\n
  89. TURN provides optimized framing to the data by using ChannelData
  90. packetization. The client activates this format for the specified peer by
  91. calling #pj_turn_session_bind_channel(). Data sent or received to/for
  92. this peer will then use ChannelData format instead of Send or Data
  93. Indications.
  94. - <b>Refreshing the allocation, permissions, and channel bindings</b>.\n
  95. Allocations, permissions, and channel bindings will be refreshed by the
  96. session automatically when they about to expire.
  97. - <b>Destroying the allocation</b>.\n
  98. Once the "relay port" is no longer needed, client destroys the allocation
  99. by calling #pj_turn_session_shutdown(). This function will return
  100. immediately, and application will be notified about the deallocation
  101. result in the \a on_state() callback of the #pj_turn_session_cb structure.
  102. Once the state has reached PJ_TURN_STATE_DESTROYING, application must
  103. assume that the session will be destroyed shortly after.
  104. */
  105. /**
  106. * Opaque declaration for TURN client session.
  107. */
  108. typedef struct pj_turn_session pj_turn_session;
  109. /**
  110. * TURN transport types, which will be used both to specify the connection
  111. * type for reaching TURN server and the type of allocation transport to be
  112. * requested to server (the REQUESTED-TRANSPORT attribute).
  113. */
  114. typedef enum pj_turn_tp_type
  115. {
  116. /**
  117. * UDP transport, which value corresponds to IANA protocol number.
  118. */
  119. PJ_TURN_TP_UDP = 17,
  120. /**
  121. * TCP transport, which value corresponds to IANA protocol number.
  122. */
  123. PJ_TURN_TP_TCP = 6,
  124. /**
  125. * TLS transport. The TLS transport will only be used as the connection
  126. * type to reach the server and never as the allocation transport type.
  127. * The value corresponds to IANA protocol number.
  128. */
  129. PJ_TURN_TP_TLS = 56
  130. } pj_turn_tp_type;
  131. /** TURN session state */
  132. typedef enum pj_turn_state_t
  133. {
  134. /**
  135. * TURN session has just been created.
  136. */
  137. PJ_TURN_STATE_NULL,
  138. /**
  139. * TURN server has been configured and now is being resolved via
  140. * DNS SRV resolution.
  141. */
  142. PJ_TURN_STATE_RESOLVING,
  143. /**
  144. * TURN server has been resolved. If there is pending allocation to
  145. * be done, it will be invoked immediately.
  146. */
  147. PJ_TURN_STATE_RESOLVED,
  148. /**
  149. * TURN session has issued ALLOCATE request and is waiting for response
  150. * from the TURN server.
  151. */
  152. PJ_TURN_STATE_ALLOCATING,
  153. /**
  154. * TURN session has successfully allocated relay resoruce and now is
  155. * ready to be used.
  156. */
  157. PJ_TURN_STATE_READY,
  158. /**
  159. * TURN session has issued deallocate request and is waiting for a
  160. * response from the TURN server.
  161. */
  162. PJ_TURN_STATE_DEALLOCATING,
  163. /**
  164. * Deallocate response has been received. Normally the session will
  165. * proceed to DESTROYING state immediately.
  166. */
  167. PJ_TURN_STATE_DEALLOCATED,
  168. /**
  169. * TURN session is being destroyed.
  170. */
  171. PJ_TURN_STATE_DESTROYING
  172. } pj_turn_state_t;
  173. #pragma pack(1)
  174. /**
  175. * This structure ChannelData header. All the fields are in network byte
  176. * order when it's on the wire.
  177. */
  178. typedef struct pj_turn_channel_data
  179. {
  180. pj_uint16_t ch_number; /**< Channel number. */
  181. pj_uint16_t length; /**< Payload length. */
  182. } pj_turn_channel_data;
  183. #pragma pack()
  184. /**
  185. * Callback to receive events from TURN session.
  186. */
  187. typedef struct pj_turn_session_cb
  188. {
  189. /**
  190. * This callback will be called by the TURN session whenever it
  191. * needs to send data or outgoing messages. Since the TURN session
  192. * doesn't have a socket on its own, this callback must be implemented.
  193. *
  194. * If the callback \a on_stun_send_pkt() is implemented, outgoing
  195. * messages will use that callback instead.
  196. *
  197. * @param sess The TURN session.
  198. * @param pkt The packet/data to be sent.
  199. * @param pkt_len Length of the packet/data.
  200. * @param dst_addr Destination address of the packet.
  201. * @param addr_len Length of the destination address.
  202. *
  203. * @return The callback should return the status of the
  204. * send operation.
  205. */
  206. pj_status_t (*on_send_pkt)(pj_turn_session *sess,
  207. const pj_uint8_t *pkt,
  208. unsigned pkt_len,
  209. const pj_sockaddr_t *dst_addr,
  210. unsigned addr_len);
  211. /**
  212. * This callback will be called by the TURN session whenever it
  213. * needs to send outgoing STUN requests/messages for TURN signalling
  214. * purposes (data sending will not invoke this callback). If this
  215. * callback is not implemented, the callback \a on_send_pkt()
  216. * will be called instead.
  217. *
  218. * @param sess The TURN session.
  219. * @param pkt The packet/data to be sent.
  220. * @param pkt_len Length of the packet/data.
  221. * @param dst_addr Destination address of the packet.
  222. * @param addr_len Length of the destination address.
  223. *
  224. * @return The callback should return the status of the
  225. * send operation.
  226. */
  227. pj_status_t (*on_stun_send_pkt)(pj_turn_session *sess,
  228. const pj_uint8_t *pkt,
  229. unsigned pkt_len,
  230. const pj_sockaddr_t *dst_addr,
  231. unsigned addr_len);
  232. /**
  233. * Notification when peer address has been bound successfully to
  234. * a channel number.
  235. *
  236. * This callback is optional since the nature of this callback is
  237. * for information only.
  238. *
  239. * @param sess The TURN session.
  240. * @param peer_addr The peer address.
  241. * @param addr_len Length of the peer address.
  242. * @param ch_num The channel number associated with this peer address.
  243. */
  244. void (*on_channel_bound)(pj_turn_session *sess,
  245. const pj_sockaddr_t *peer_addr,
  246. unsigned addr_len,
  247. unsigned ch_num);
  248. /**
  249. * Notification when incoming data has been received, either through
  250. * Data indication or ChannelData message from the TURN server.
  251. *
  252. * @param sess The TURN session.
  253. * @param pkt The data/payload of the Data Indication or ChannelData
  254. * packet.
  255. * @param pkt_len Length of the data/payload.
  256. * @param peer_addr Peer address where this payload was received by
  257. * the TURN server.
  258. * @param addr_len Length of the peer address.
  259. */
  260. void (*on_rx_data)(pj_turn_session *sess,
  261. void *pkt,
  262. unsigned pkt_len,
  263. const pj_sockaddr_t *peer_addr,
  264. unsigned addr_len);
  265. /**
  266. * Notification when TURN session state has changed. Application should
  267. * implement this callback at least to know that the TURN session is
  268. * going to be destroyed.
  269. *
  270. * @param sess The TURN session.
  271. * @param old_state The previous state of the session.
  272. * @param new_state The current state of the session.
  273. */
  274. void (*on_state)(pj_turn_session *sess,
  275. pj_turn_state_t old_state,
  276. pj_turn_state_t new_state);
  277. /**
  278. * Notification when TURN client received a ConnectionAttempt Indication
  279. * from the TURN server, which indicates that peer initiates a TCP
  280. * connection to allocated slot in the TURN server. Application must
  281. * implement this callback if it uses RFC 6062 (TURN TCP allocations).
  282. *
  283. * After receiving this callback, application should establish a new TCP
  284. * connection to the TURN server and send ConnectionBind request (using
  285. * pj_turn_session_connection_bind()). After the connection binding
  286. * succeeds, this new connection will become a data only connection.
  287. *
  288. * @param sess The TURN session.
  289. * @param conn_id The connection ID assigned by TURN server.
  290. * @param peer_addr Peer address that tried to connect to the TURN server.
  291. * @param addr_len Length of the peer address.
  292. */
  293. void (*on_connection_attempt)(pj_turn_session *sess,
  294. pj_uint32_t conn_id,
  295. const pj_sockaddr_t *peer_addr,
  296. unsigned addr_len);
  297. /**
  298. * Notification for ConnectionBind request sent using
  299. * pj_turn_session_connection_bind().
  300. *
  301. * @param sess The TURN session.
  302. * @param status The status code.
  303. * @param conn_id The connection ID.
  304. * @param peer_addr Peer address.
  305. * @param addr_len Length of the peer address.
  306. */
  307. void (*on_connection_bind_status)(pj_turn_session *sess,
  308. pj_status_t status,
  309. pj_uint32_t conn_id,
  310. const pj_sockaddr_t *peer_addr,
  311. unsigned addr_len);
  312. /**
  313. * Notification for Connect request sent using
  314. * pj_turn_session_connect().
  315. *
  316. * @param sess The TURN session.
  317. * @param status The status code.
  318. * @param conn_id The connection ID.
  319. * @param peer_addr Peer address.
  320. * @param addr_len Length of the peer address.
  321. */
  322. void (*on_connect_complete)(pj_turn_session *sess,
  323. pj_status_t status,
  324. pj_uint32_t conn_id,
  325. const pj_sockaddr_t *peer_addr,
  326. unsigned addr_len);
  327. } pj_turn_session_cb;
  328. /**
  329. * Allocation parameter, which can be given when application calls
  330. * pj_turn_session_alloc() to allocate relay address in the TURN server.
  331. * Application should call pj_turn_alloc_param_default() to initialize
  332. * this structure with the default values.
  333. */
  334. typedef struct pj_turn_alloc_param
  335. {
  336. /**
  337. * The requested BANDWIDTH. Default is zero to not request any
  338. * specific bandwidth. Note that this attribute has been deprecated
  339. * after TURN-08 draft, hence application should only use this
  340. * attribute when talking to TURN-07 or older version.
  341. */
  342. int bandwidth;
  343. /**
  344. * The requested LIFETIME. Default is zero to not request any
  345. * explicit allocation lifetime.
  346. */
  347. int lifetime;
  348. /**
  349. * If set to non-zero, the TURN session will periodically send blank
  350. * Send Indication every PJ_TURN_KEEP_ALIVE_SEC to refresh local
  351. * NAT bindings. Default is zero.
  352. */
  353. int ka_interval;
  354. /**
  355. * The requested ADDRESS-FAMILY. Default is zero to request relay with
  356. * address family matched to the one specified in TURN session creation.
  357. * Valid values are zero, pj_AF_INET(), and pj_AF_INET6().
  358. *
  359. * Default value is zero.
  360. */
  361. int af;
  362. /**
  363. * Type of connection to from TURN server to peer. Supported values are
  364. * PJ_TURN_TP_UDP (RFC 5766) and PJ_TURN_TP_TCP (RFC 6062)
  365. *
  366. * Default is PJ_TURN_TP_UDP.
  367. */
  368. pj_turn_tp_type peer_conn_type;
  369. } pj_turn_alloc_param;
  370. /**
  371. * This structure describes TURN session info.
  372. */
  373. typedef struct pj_turn_session_info
  374. {
  375. /**
  376. * Session state.
  377. */
  378. pj_turn_state_t state;
  379. /**
  380. * Last error (if session was terminated because of error)
  381. */
  382. pj_status_t last_status;
  383. /**
  384. * Type of connection to the TURN server.
  385. */
  386. pj_turn_tp_type conn_type;
  387. /**
  388. * The selected TURN server address.
  389. */
  390. pj_sockaddr server;
  391. /**
  392. * Mapped address, as reported by the TURN server.
  393. */
  394. pj_sockaddr mapped_addr;
  395. /**
  396. * The relay address
  397. */
  398. pj_sockaddr relay_addr;
  399. /**
  400. * Current seconds before allocation expires.
  401. */
  402. int lifetime;
  403. } pj_turn_session_info;
  404. /**
  405. * Parameters for function pj_turn_session_on_rx_pkt2().
  406. */
  407. typedef struct pj_turn_session_on_rx_pkt_param
  408. {
  409. /**
  410. * The packet as received from the TURN server. This should contain
  411. * either STUN encapsulated message or a ChannelData packet.
  412. */
  413. void *pkt;
  414. /**
  415. * The length of the packet.
  416. */
  417. pj_size_t pkt_len;
  418. /**
  419. * The number of parsed or processed data from the packet.
  420. */
  421. pj_size_t parsed_len;
  422. /**
  423. * Source address where the packet is received from.
  424. */
  425. const pj_sockaddr_t *src_addr;
  426. /**
  427. * Length of the source address.
  428. */
  429. unsigned src_addr_len;
  430. } pj_turn_session_on_rx_pkt_param;
  431. /**
  432. * Initialize pj_turn_alloc_param with the default values.
  433. *
  434. * @param prm The TURN allocation parameter to be initialized.
  435. */
  436. PJ_DECL(void) pj_turn_alloc_param_default(pj_turn_alloc_param *prm);
  437. /**
  438. * Duplicate pj_turn_alloc_param.
  439. *
  440. * @param pool Pool to allocate memory (currently not used)
  441. * @param dst Destination parameter.
  442. * @param src Source parameter.
  443. */
  444. PJ_DECL(void) pj_turn_alloc_param_copy(pj_pool_t *pool,
  445. pj_turn_alloc_param *dst,
  446. const pj_turn_alloc_param *src);
  447. /**
  448. * Get string representation for the given TURN state.
  449. *
  450. * @param state The TURN session state.
  451. *
  452. * @return The state name as NULL terminated string.
  453. */
  454. PJ_DECL(const char*) pj_turn_state_name(pj_turn_state_t state);
  455. /**
  456. * Create a TURN session instance with the specified address family and
  457. * connection type. Once TURN session instance is created, application
  458. * must call pj_turn_session_alloc() to allocate a relay address in the TURN
  459. * server.
  460. *
  461. * @param cfg The STUN configuration which contains among other
  462. * things the ioqueue and timer heap instance for
  463. * the operation of this session.
  464. * @param name Optional name to identify this session in the log.
  465. * @param af Address family of the client connection. Currently
  466. * pj_AF_INET() and pj_AF_INET6() are supported.
  467. * @param conn_type Connection type to the TURN server.
  468. * @param grp_lock Optional group lock object to be used by this session.
  469. * If this value is NULL, the session will create
  470. * a group lock internally.
  471. * @param cb Callback to receive events from the TURN session.
  472. * @param options Option flags, currently this value must be zero.
  473. * @param user_data Arbitrary application data to be associated with
  474. * this transport.
  475. * @param p_sess Pointer to receive the created instance of the
  476. * TURN session.
  477. *
  478. * @return PJ_SUCCESS if the operation has been successful,
  479. * or the appropriate error code on failure.
  480. */
  481. PJ_DECL(pj_status_t) pj_turn_session_create(const pj_stun_config *cfg,
  482. const char *name,
  483. int af,
  484. pj_turn_tp_type conn_type,
  485. pj_grp_lock_t *grp_lock,
  486. const pj_turn_session_cb *cb,
  487. unsigned options,
  488. void *user_data,
  489. pj_turn_session **p_sess);
  490. /**
  491. * Shutdown TURN client session. This will gracefully deallocate and
  492. * destroy the client session.
  493. *
  494. * @param sess The TURN client session.
  495. *
  496. * @return PJ_SUCCESS if the operation has been successful,
  497. * or the appropriate error code on failure.
  498. */
  499. PJ_DECL(pj_status_t) pj_turn_session_shutdown(pj_turn_session *sess);
  500. /**
  501. * Shutdown TURN client session. This will gracefully deallocate and
  502. * destroy the client session.
  503. *
  504. * @param sess The TURN client session.
  505. * @param last_err Optional error code to be set to the session,
  506. * which would be returned back in the \a info
  507. * parameter of #pj_turn_session_get_info(). If
  508. * this argument value is PJ_SUCCESS, the error
  509. * code will not be set. If the session already
  510. * has an error code set, this function will not
  511. * overwrite that error code either.
  512. *
  513. * @return PJ_SUCCESS if the operation has been successful,
  514. * or the appropriate error code on failure.
  515. */
  516. PJ_DECL(pj_status_t) pj_turn_session_shutdown2(pj_turn_session *sess,
  517. pj_status_t last_err);
  518. /**
  519. * Forcefully destroy the TURN session. This will destroy the session
  520. * immediately. If there is an active allocation, the server will not
  521. * be notified about the client destruction.
  522. *
  523. * @param sess The TURN client session.
  524. * @param last_err Optional error code to be set to the session,
  525. * which would be returned back in the \a info
  526. * parameter of #pj_turn_session_get_info(). If
  527. * this argument value is PJ_SUCCESS, the error
  528. * code will not be set. If the session already
  529. * has an error code set, this function will not
  530. * overwrite that error code either.
  531. *
  532. * @return PJ_SUCCESS if the operation has been successful,
  533. * or the appropriate error code on failure.
  534. */
  535. PJ_DECL(pj_status_t) pj_turn_session_destroy(pj_turn_session *sess,
  536. pj_status_t last_err);
  537. /**
  538. * Get the information about this TURN session and the allocation, if
  539. * any.
  540. *
  541. * @param sess The TURN client session.
  542. * @param info The structure to be initialized with the TURN
  543. * session info.
  544. *
  545. * @return PJ_SUCCESS if the operation has been successful,
  546. * or the appropriate error code on failure.
  547. */
  548. PJ_DECL(pj_status_t) pj_turn_session_get_info(pj_turn_session *sess,
  549. pj_turn_session_info *info);
  550. /**
  551. * Associate a user data with this TURN session. The user data may then
  552. * be retrieved later with pj_turn_session_get_user_data().
  553. *
  554. * @param sess The TURN client session.
  555. * @param user_data Arbitrary data.
  556. *
  557. * @return PJ_SUCCESS if the operation has been successful,
  558. * or the appropriate error code on failure.
  559. */
  560. PJ_DECL(pj_status_t) pj_turn_session_set_user_data(pj_turn_session *sess,
  561. void *user_data);
  562. /**
  563. * Retrieve the previously assigned user data associated with this TURN
  564. * session.
  565. *
  566. * @param sess The TURN client session.
  567. *
  568. * @return The user/application data.
  569. */
  570. PJ_DECL(void*) pj_turn_session_get_user_data(pj_turn_session *sess);
  571. /**
  572. * Get the group lock for this TURN session.
  573. *
  574. * @param sess The TURN client session.
  575. *
  576. * @return The group lock.
  577. */
  578. PJ_DECL(pj_grp_lock_t *) pj_turn_session_get_grp_lock(pj_turn_session *sess);
  579. /**
  580. * Configure message logging. By default all flags are enabled.
  581. *
  582. * @param sess The TURN client session.
  583. * @param flags Bitmask combination of #pj_stun_sess_msg_log_flag
  584. */
  585. PJ_DECL(void) pj_turn_session_set_log(pj_turn_session *sess,
  586. unsigned flags);
  587. /**
  588. * Configure the SOFTWARE name to be sent in all STUN requests by the
  589. * TURN session.
  590. *
  591. * @param sess The TURN client session.
  592. * @param sw Software name string. If this argument is NULL or
  593. * empty, the session will not include SOFTWARE attribute
  594. * in STUN requests and responses.
  595. *
  596. * @return PJ_SUCCESS on success, or the appropriate error code.
  597. */
  598. PJ_DECL(pj_status_t) pj_turn_session_set_software_name(pj_turn_session *sess,
  599. const pj_str_t *sw);
  600. /**
  601. * Set the server or domain name of the server. Before the application
  602. * can send Allocate request (with pj_turn_session_alloc()), it must first
  603. * resolve the server address(es) using this function. This function will
  604. * resolve the TURN server using DNS SRV resolution if the \a resolver
  605. * is set. The server resolution process will complete asynchronously,
  606. * and application will be notified in \a on_state() callback with the
  607. * session state set to PJ_TURN_STATE_RESOLVED.
  608. *
  609. * Application may call with pj_turn_session_alloc() before the server
  610. * resolution completes. In this case, the operation will be queued by
  611. * the session, and it will be sent once the server resolution completes.
  612. *
  613. * @param sess The TURN client session.
  614. * @param domain The domain, hostname, or IP address of the TURN
  615. * server. When this parameter contains domain name,
  616. * the \a resolver parameter must be set to activate
  617. * DNS SRV resolution.
  618. * @param default_port The default TURN port number to use when DNS SRV
  619. * resolution is not used. If DNS SRV resolution is
  620. * used, the server port number will be set from the
  621. * DNS SRV records.
  622. * @param resolver If this parameter is not NULL, then the \a domain
  623. * parameter will be first resolved with DNS SRV and
  624. * then fallback to using DNS A/AAAA resolution when
  625. * DNS SRV resolution fails. If this parameter is
  626. * NULL, the \a domain parameter will be resolved as
  627. * hostname.
  628. *
  629. * @return PJ_SUCCESS if the operation has been successfully
  630. * queued, or the appropriate error code on failure.
  631. * When this function returns PJ_SUCCESS, the final
  632. * result of the resolution process will be notified
  633. * to application in \a on_state() callback.
  634. */
  635. PJ_DECL(pj_status_t) pj_turn_session_set_server(pj_turn_session *sess,
  636. const pj_str_t *domain,
  637. int default_port,
  638. pj_dns_resolver *resolver);
  639. /**
  640. * Set credential to be used to authenticate against TURN server.
  641. * Application must call this function before sending Allocate request
  642. * with pj_turn_session_alloc().
  643. *
  644. * @param sess The TURN client session
  645. * @param cred STUN credential to be used.
  646. *
  647. * @return PJ_SUCCESS if the operation has been successful,
  648. * or the appropriate error code on failure.
  649. */
  650. PJ_DECL(pj_status_t) pj_turn_session_set_credential(pj_turn_session *sess,
  651. const pj_stun_auth_cred *cred);
  652. /**
  653. * Allocate a relay address/resource in the TURN server by sending TURN
  654. * Allocate request. Application must first initiate the server resolution
  655. * process with pj_turn_session_set_server() and set the credential to be
  656. * used with pj_turn_session_set_credential() before calling this function.
  657. *
  658. * This function will complete asynchronously, and the application will be
  659. * notified about the allocation result in \a on_state() callback. The
  660. * TURN session state will move to PJ_TURN_STATE_READY if allocation is
  661. * successful, and PJ_TURN_STATE_DEALLOCATING or greater state if allocation
  662. * has failed.
  663. *
  664. * Once allocation has been successful, the TURN session will keep this
  665. * allocation alive until the session is destroyed, by sending periodic
  666. * allocation refresh to the TURN server.
  667. *
  668. * @param sess The TURN client session.
  669. * @param param Optional TURN allocation parameter.
  670. *
  671. * @return PJ_SUCCESS if the operation has been successfully
  672. * initiated or the appropriate error code on failure.
  673. */
  674. PJ_DECL(pj_status_t) pj_turn_session_alloc(pj_turn_session *sess,
  675. const pj_turn_alloc_param *param);
  676. /**
  677. * Create or renew permission in the TURN server for the specified peer IP
  678. * addresses. Application must install permission for a particular (peer)
  679. * IP address before it sends any data to that IP address, or otherwise
  680. * the TURN server will drop the data.
  681. *
  682. * @param sess The TURN client session.
  683. * @param addr_cnt Number of IP addresses.
  684. * @param addr Array of peer IP addresses. Only the address family
  685. * and IP address portion of the socket address matter.
  686. * @param options Specify 1 to let the TURN client session automatically
  687. * renew the permission later when they are about to
  688. * expire.
  689. *
  690. * @return PJ_SUCCESS if the operation has been successfully
  691. * issued, or the appropriate error code. Note that
  692. * the operation itself will complete asynchronously.
  693. */
  694. PJ_DECL(pj_status_t) pj_turn_session_set_perm(pj_turn_session *sess,
  695. unsigned addr_cnt,
  696. const pj_sockaddr addr[],
  697. unsigned options);
  698. /**
  699. * Send a data to the specified peer address via the TURN relay. This
  700. * function will encapsulate the data as STUN Send Indication or TURN
  701. * ChannelData packet and send the message to the TURN server. The TURN
  702. * server then will send the data to the peer.
  703. *
  704. * The allocation (pj_turn_session_alloc()) must have been successfully
  705. * created before application can relay any data.
  706. *
  707. * Since TURN session is transport independent, this function will
  708. * ultimately call \a on_send_pkt() callback to request the application
  709. * to actually send the packet containing the data to the TURN server.
  710. *
  711. * @param sess The TURN client session.
  712. * @param pkt The data/packet to be sent to peer.
  713. * @param pkt_len Length of the data.
  714. * @param peer_addr The remote peer address (the ultimate destination
  715. * of the data, and not the TURN server address).
  716. * @param addr_len Length of the address.
  717. *
  718. * @return If the callback \a on_send_pkt() is called, this
  719. * will contain the return value of the callback.
  720. * Otherwise, it will indicate failure with
  721. * the appropriate error code.
  722. */
  723. PJ_DECL(pj_status_t) pj_turn_session_sendto(pj_turn_session *sess,
  724. const pj_uint8_t *pkt,
  725. unsigned pkt_len,
  726. const pj_sockaddr_t *peer_addr,
  727. unsigned addr_len);
  728. /**
  729. * Optionally establish channel binding for the specified a peer address.
  730. * This function will assign a unique channel number for the peer address
  731. * and request channel binding to the TURN server for this address. When
  732. * a channel has been bound to a peer, the TURN client and TURN server
  733. * will exchange data using ChannelData encapsulation format, which has
  734. * lower bandwidth overhead than Send Indication (the default format used
  735. * when peer address is not bound to a channel).
  736. *
  737. * This function will complete asynchronously, and application will be
  738. * notified about the result in \a on_channel_bound() callback.
  739. *
  740. * @param sess The TURN client session.
  741. * @param peer The remote peer address.
  742. * @param addr_len Length of the address.
  743. *
  744. * @return PJ_SUCCESS if the operation has been successfully
  745. * initiated, or the appropriate error code on failure.
  746. */
  747. PJ_DECL(pj_status_t) pj_turn_session_bind_channel(pj_turn_session *sess,
  748. const pj_sockaddr_t *peer,
  749. unsigned addr_len);
  750. /**
  751. * Notify TURN client session upon receiving a packet from server. Since
  752. * the TURN session is transport independent, it does not read packet from
  753. * any sockets, and rather relies on application giving it packets that
  754. * are received from the TURN server. The session then processes this packet
  755. * and decides whether it is part of TURN protocol exchange or if it is a
  756. * data to be reported back to user, which in this case it will call the
  757. * \a on_rx_data() callback.
  758. *
  759. * @param sess The TURN client session.
  760. * @param pkt The packet as received from the TURN server. This
  761. * should contain either STUN encapsulated message or
  762. * a ChannelData packet.
  763. * @param pkt_len The length of the packet.
  764. * @param parsed_len Optional argument to receive the number of parsed
  765. * or processed data from the packet.
  766. *
  767. * @return The function may return non-PJ_SUCCESS if it receives
  768. * non-STUN and non-ChannelData packet, or if the
  769. * \a on_rx_data() returns non-PJ_SUCCESS;
  770. */
  771. PJ_DECL(pj_status_t) pj_turn_session_on_rx_pkt(pj_turn_session *sess,
  772. void *pkt,
  773. pj_size_t pkt_len,
  774. pj_size_t *parsed_len);
  775. /**
  776. * Notify TURN client session upon receiving a packet from server. Since
  777. * the TURN session is transport independent, it does not read packet from
  778. * any sockets, and rather relies on application giving it packets that
  779. * are received from the TURN server. The session then processes this packet
  780. * and decides whether it is part of TURN protocol exchange or if it is a
  781. * data to be reported back to user, which in this case it will call the
  782. * \a on_rx_data() callback.
  783. *
  784. * This function is variant of pj_turn_session_on_rx_pkt() with additional
  785. * parameters such as source address. Source address will allow STUN/TURN
  786. * session to resend the request (e.g: with updated authentication) to the
  787. * provided source address which may be different to the initial connection,
  788. * for example in RFC 6062 scenario that there can be some data connection
  789. * and a control connection.
  790. *
  791. * @param sess The TURN client session.
  792. * @param prm The function parameters, e.g: packet, source address.
  793. *
  794. * @return The function may return non-PJ_SUCCESS if it receives
  795. * non-STUN and non-ChannelData packet, or if the
  796. * \a on_rx_data() returns non-PJ_SUCCESS;
  797. */
  798. PJ_DECL(pj_status_t) pj_turn_session_on_rx_pkt2(
  799. pj_turn_session *sess,
  800. pj_turn_session_on_rx_pkt_param *prm);
  801. /**
  802. * Initiate connection binding to the specified peer using ConnectionBind
  803. * request. Application must call this function when it uses RFC 6062
  804. * (TURN TCP allocations) to establish a data connection with peer after
  805. * opening/accepting connection to/from peer. The connection binding status
  806. * will be notified via on_connection_bind_status callback.
  807. *
  808. * @param sess The TURN session.
  809. * @param pool The memory pool.
  810. * @param conn_id The connection ID assigned by TURN server.
  811. * @param peer_addr Peer address.
  812. * @param addr_len Length of the peer address.
  813. *
  814. * @return PJ_SUCCESS if the operation has been successfully
  815. * issued, or the appropriate error code. Note that
  816. * the operation itself will complete asynchronously.
  817. */
  818. PJ_DECL(pj_status_t) pj_turn_session_connection_bind(
  819. pj_turn_session *sess,
  820. pj_pool_t *pool,
  821. pj_uint32_t conn_id,
  822. const pj_sockaddr_t *peer_addr,
  823. unsigned addr_len);
  824. /**
  825. * Initiate connection to the specified peer using Connect request.
  826. * Application must call this function when it uses RFC 6062 (TURN TCP
  827. * allocations) to initiate a data connection to a peer. When Connect response
  828. * received, on_connect_complete will be called, application must implement
  829. * this callback and initiate a new data connection to the specified peer.
  830. *
  831. * According to RFC 6062, a control connection must be a TCP connection,
  832. * and application must send TCP Allocate request
  833. * (with pj_turn_session_alloc(), set TURN allocation parameter peer_conn_type
  834. * to PJ_TURN_TP_TCP) before calling this function.
  835. *
  836. * @param sess The TURN client session.
  837. * @param peer_addr Peer address.
  838. * @param addr_len Length of the peer address.
  839. *
  840. * @return PJ_SUCCESS if the operation has been successfully
  841. * issued, or the appropriate error code. Note that
  842. * the operation itself will complete asynchronously.
  843. */
  844. PJ_DECL(pj_status_t) pj_turn_session_connect(pj_turn_session *sess,
  845. const pj_sockaddr_t *peer_addr,
  846. unsigned addr_len);
  847. /**
  848. * @}
  849. */
  850. PJ_END_DECL
  851. #endif /* __PJNATH_TURN_SESSION_H__ */