stun_session.h 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763
  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_SESSION_H__
  20. #define __PJNATH_STUN_SESSION_H__
  21. /**
  22. * @file stun_session.h
  23. * @brief STUN session management for client/server.
  24. */
  25. #include <pjnath/stun_msg.h>
  26. #include <pjnath/stun_auth.h>
  27. #include <pjnath/stun_config.h>
  28. #include <pjnath/stun_transaction.h>
  29. #include <pj/list.h>
  30. #include <pj/lock.h>
  31. #include <pj/timer.h>
  32. PJ_BEGIN_DECL
  33. /* **************************************************************************/
  34. /**
  35. * @addtogroup PJNATH_STUN_SESSION
  36. * @{
  37. *
  38. * This is is a transport-independent object to manage a client or server
  39. * STUN session. It has the following features:
  40. *
  41. * - <b>transport independent</b>:\n
  42. * the object does not have it's own socket, but rather it provides
  43. * functions and callbacks to send and receive packets. This way the
  44. * object can be used by different transport types (e.g. UDP, TCP,
  45. * TLS, etc.) as well as better integration to application which
  46. * already has its own means to send and receive packets.
  47. *
  48. * - <b>authentication management</b>:\n
  49. * the object manages STUN authentication throughout the lifetime of
  50. * the session. For client sessions, once it's given a credential to
  51. * authenticate itself with the server, the object will automatically
  52. * add authentication info (the MESSAGE-INTEGRITY) to the request as
  53. * well as authenticate the response. It will also handle long-term
  54. * authentication challenges, including handling of nonce expiration,
  55. * and retry the request automatically. For server sessions, it can
  56. * be configured to authenticate incoming requests automatically.
  57. *
  58. * - <b>static or dynamic credential</b>:\n
  59. * application may specify static or dynamic credential to be used by
  60. * the STUN session. Static credential means a static combination of
  61. * username and password (and these cannot change during the session
  62. * duration), while dynamic credential provides callback to ask the
  63. * application about which username/password to use everytime
  64. * authentication is about to be performed.
  65. *
  66. * - <b>client transaction management</b>:\n
  67. * outgoing requests may be sent with a STUN transaction for reliability,
  68. * and the object will manage the transaction internally (including
  69. * performing retransmissions). Application will be notified about the
  70. * result of the request when the response arrives (or the transaction
  71. * times out). When the request is challenged with authentication, the
  72. * object will retry the request with new authentication info, and
  73. * application will be notified about the final result of this request.
  74. *
  75. * - <b>server transaction management</b>:\n
  76. * application may ask response to incoming requests to be cached by
  77. * the object, and in this case the object will check for cached
  78. * response everytime request is received. The cached response will be
  79. * deleted once a timer expires.
  80. *
  81. * \section using_stun_sess_sec Using the STUN session
  82. *
  83. * The following steps describes how to use the STUN session:
  84. *
  85. * - <b>create the object configuration</b>:\n
  86. * The #pj_stun_config contains the configuration to create the STUN
  87. * session, such as the timer heap to register internal timers and
  88. * various STUN timeout values. You can initialize this structure by
  89. * calling #pj_stun_config_init()
  90. *
  91. * - <b>create the STUN session</b>:\n
  92. * by calling #pj_stun_session_create(). Among other things, this
  93. * function requires the instance of #pj_stun_config and also
  94. * #pj_stun_session_cb structure which stores callbacks to send
  95. * outgoing packets as well as to notify application about incoming
  96. * STUN requests, responses, and indicates and other events.
  97. *
  98. * - <b>configure credential:</b>\n
  99. * if authentication is required for the session, configure the
  100. * credential with #pj_stun_session_set_credential()
  101. *
  102. * - <b>configuring other settings:</b>\n
  103. * several APIs are provided to configure the behavior of the STUN
  104. * session (for example, to set the SOFTWARE attribute value, controls
  105. * the logging behavior, fine tune the mutex locking, etc.). Please see
  106. * the API reference for more info.
  107. *
  108. * - <b>creating outgoing STUN requests or indications:</b>\n
  109. * create the STUN message by using #pj_stun_session_create_req() or
  110. * #pj_stun_session_create_ind(). This will create a transmit data
  111. * buffer containing a blank STUN request or indication. You will then
  112. * typically need to add STUN attributes that are relevant to the
  113. * request or indication, but note that some default attributes will
  114. * be added by the session later when the message is sent (such as
  115. * SOFTWARE attribute and attributes related to authentication).
  116. * The message is now ready to be sent.
  117. *
  118. * - <b>sending outgoing message:</b>\n
  119. * use #pj_stun_session_send_msg() to send outgoing STUN messages (this
  120. * includes STUN requests, indications, and responses). The function has
  121. * options whether to retransmit the request (for non reliable transports)
  122. * or to cache the response if we're sending response. This function in
  123. * turn will call the \a on_send_msg() callback of #pj_stun_session_cb
  124. * to request the application to send the packet.
  125. *
  126. * - <b>handling incoming packet:</b>\n
  127. * call #pj_stun_session_on_rx_pkt() everytime the application receives
  128. * a STUN packet. This function will decode the packet and process the
  129. * packet according to the message, and normally this will cause one
  130. * of the callback in the #pj_stun_session_cb to be called to notify
  131. * the application about the event.
  132. *
  133. * - <b>handling incoming requests:</b>\n
  134. * incoming requests are notified to application in the \a on_rx_request
  135. * callback of the #pj_stun_session_cb. If authentication is enabled in
  136. * the session, the application will only receive this callback after
  137. * the incoming request has been authenticated (if the authentication
  138. * fails, the session would respond automatically with 401 error and
  139. * the callback will not be called). Application now must create and
  140. * send response for this request.
  141. *
  142. * - <b>creating and sending response:</b>\n
  143. * create the STUN response with #pj_stun_session_create_res(). This will
  144. * create a transmit data buffer containing a blank STUN response. You
  145. * will then typically need to add STUN attributes that are relevant to
  146. * the response, but note that some default attributes will
  147. * be added by the session later when the message is sent (such as
  148. * SOFTWARE attribute and attributes related to authentication).
  149. * The message is now ready to be sent. Use #pj_stun_session_send_msg()
  150. * (as explained above) to send the response.
  151. *
  152. * - <b>convenient way to send response:</b>\n
  153. * the #pj_stun_session_respond() is provided as a convenient way to
  154. * create and send simple STUN responses, such as error responses.
  155. *
  156. * - <b>destroying the session:</b>\n
  157. * once the session is done, use #pj_stun_session_destroy() to destroy
  158. * the session.
  159. */
  160. /** Forward declaration for pj_stun_tx_data */
  161. typedef struct pj_stun_tx_data pj_stun_tx_data;
  162. /** Forward declaration for pj_stun_rx_data */
  163. typedef struct pj_stun_rx_data pj_stun_rx_data;
  164. /** Forward declaration for pj_stun_session */
  165. typedef struct pj_stun_session pj_stun_session;
  166. /**
  167. * This is the callback to be registered to pj_stun_session, to send
  168. * outgoing message and to receive various notifications from the STUN
  169. * session.
  170. */
  171. typedef struct pj_stun_session_cb
  172. {
  173. /**
  174. * Callback to be called by the STUN session to send outgoing message.
  175. *
  176. * @param sess The STUN session.
  177. * @param token The token associated with this outgoing message
  178. * and was set by the application. This token was
  179. * set by application in pj_stun_session_send_msg()
  180. * for outgoing messages that are initiated by the
  181. * application, or in pj_stun_session_on_rx_pkt()
  182. * if this message is a response that was internally
  183. * generated by the STUN session (for example, an
  184. * 401/Unauthorized response). Application may use
  185. * this facility for any purposes.
  186. * @param pkt Packet to be sent.
  187. * @param pkt_size Size of the packet to be sent.
  188. * @param dst_addr The destination address.
  189. * @param addr_len Length of destination address.
  190. *
  191. * @return The callback should return the status of the
  192. * packet sending.
  193. */
  194. pj_status_t (*on_send_msg)(pj_stun_session *sess,
  195. void *token,
  196. const void *pkt,
  197. pj_size_t pkt_size,
  198. const pj_sockaddr_t *dst_addr,
  199. unsigned addr_len);
  200. /**
  201. * Callback to be called on incoming STUN request message. This function
  202. * is called when application calls pj_stun_session_on_rx_pkt() and when
  203. * the STUN session has detected that the incoming STUN message is a
  204. * STUN request message. In the
  205. * callback processing, application MUST create a response by calling
  206. * pj_stun_session_create_response() function and send the response
  207. * with pj_stun_session_send_msg() function, before returning from
  208. * the callback.
  209. *
  210. * @param sess The STUN session.
  211. * @param pkt Pointer to the original STUN packet.
  212. * @param pkt_len Length of the STUN packet.
  213. * @param rdata Data containing incoming request message.
  214. * @param token The token that was set by the application when
  215. * calling pj_stun_session_on_rx_pkt() function.
  216. * @param src_addr Source address of the packet.
  217. * @param src_addr_len Length of the source address.
  218. *
  219. * @return The return value of this callback will be
  220. * returned back to pj_stun_session_on_rx_pkt()
  221. * function.
  222. */
  223. pj_status_t (*on_rx_request)(pj_stun_session *sess,
  224. const pj_uint8_t *pkt,
  225. unsigned pkt_len,
  226. const pj_stun_rx_data *rdata,
  227. void *token,
  228. const pj_sockaddr_t *src_addr,
  229. unsigned src_addr_len);
  230. /**
  231. * Callback to be called when response is received or the transaction
  232. * has timed out. This callback is called either when application calls
  233. * pj_stun_session_on_rx_pkt() with the packet containing a STUN
  234. * response for the client transaction, or when the internal timer of
  235. * the STUN client transaction has timed-out before a STUN response is
  236. * received.
  237. *
  238. * @param sess The STUN session.
  239. * @param status Status of the request. If the value if not
  240. * PJ_SUCCESS, the transaction has timed-out
  241. * or other error has occurred, and the response
  242. * argument may be NULL.
  243. * Note that when the status is not success, the
  244. * response may contain non-NULL value if the
  245. * response contains STUN ERROR-CODE attribute.
  246. * @param token The token that was set by the application when
  247. * calling pj_stun_session_send_msg() function.
  248. * Please not that this token IS NOT the token
  249. * that was given in pj_stun_session_on_rx_pkt().
  250. * @param tdata The original STUN request.
  251. * @param response The response message, on successful transaction,
  252. * or otherwise MAY BE NULL if status is not success.
  253. * Note that when the status is not success, this
  254. * argument may contain non-NULL value if the
  255. * response contains STUN ERROR-CODE attribute.
  256. * @param src_addr The source address where the response was
  257. * received, or NULL if the response is NULL.
  258. * @param src_addr_len The length of the source address.
  259. */
  260. void (*on_request_complete)(pj_stun_session *sess,
  261. pj_status_t status,
  262. void *token,
  263. pj_stun_tx_data *tdata,
  264. const pj_stun_msg *response,
  265. const pj_sockaddr_t *src_addr,
  266. unsigned src_addr_len);
  267. /**
  268. * Callback to be called on incoming STUN request message. This function
  269. * is called when application calls pj_stun_session_on_rx_pkt() and when
  270. * the STUN session has detected that the incoming STUN message is a
  271. * STUN indication message.
  272. *
  273. * @param sess The STUN session.
  274. * @param pkt Pointer to the original STUN packet.
  275. * @param pkt_len Length of the STUN packet.
  276. * @param msg The parsed STUN indication.
  277. * @param token The token that was set by the application when
  278. * calling pj_stun_session_on_rx_pkt() function.
  279. * @param src_addr Source address of the packet.
  280. * @param src_addr_len Length of the source address.
  281. *
  282. * @return The return value of this callback will be
  283. * returned back to pj_stun_session_on_rx_pkt()
  284. * function.
  285. */
  286. pj_status_t (*on_rx_indication)(pj_stun_session *sess,
  287. const pj_uint8_t *pkt,
  288. unsigned pkt_len,
  289. const pj_stun_msg *msg,
  290. void *token,
  291. const pj_sockaddr_t *src_addr,
  292. unsigned src_addr_len);
  293. } pj_stun_session_cb;
  294. /**
  295. * This structure describes incoming request message.
  296. */
  297. struct pj_stun_rx_data
  298. {
  299. /**
  300. * The parsed request message.
  301. */
  302. pj_stun_msg *msg;
  303. /**
  304. * Credential information that is found and used to authenticate
  305. * incoming request. Application may use this information when
  306. * generating authentication for the outgoing response.
  307. */
  308. pj_stun_req_cred_info info;
  309. };
  310. /**
  311. * This structure describe the outgoing STUN transmit data to carry the
  312. * message to be sent.
  313. */
  314. struct pj_stun_tx_data
  315. {
  316. /** PJLIB list interface */
  317. PJ_DECL_LIST_MEMBER(struct pj_stun_tx_data);
  318. pj_pool_t *pool; /**< Pool. */
  319. pj_stun_session *sess; /**< The STUN session. */
  320. pj_stun_msg *msg; /**< The STUN message. */
  321. pj_bool_t is_destroying; /**< Is destroying? */
  322. void *token; /**< The token. */
  323. pj_stun_client_tsx *client_tsx; /**< Client STUN transaction. */
  324. pj_bool_t retransmit; /**< Retransmit request? */
  325. pj_uint32_t msg_magic; /**< Message magic. */
  326. pj_uint8_t msg_key[12]; /**< Message/transaction key. */
  327. pj_grp_lock_t *grp_lock; /**< Group lock (for resp cache). */
  328. pj_stun_req_cred_info auth_info; /**< Credential info */
  329. void *pkt; /**< The STUN packet. */
  330. unsigned max_len; /**< Length of packet buffer. */
  331. pj_size_t pkt_size; /**< The actual length of STUN pkt. */
  332. unsigned addr_len; /**< Length of destination address. */
  333. const pj_sockaddr_t *dst_addr; /**< Destination address. */
  334. pj_timer_entry res_timer; /**< Response cache timer. */
  335. };
  336. /**
  337. * These are the flags to control the message logging in the STUN session.
  338. */
  339. typedef enum pj_stun_sess_msg_log_flag
  340. {
  341. PJ_STUN_SESS_LOG_TX_REQ=1, /**< Log outgoing STUN requests. */
  342. PJ_STUN_SESS_LOG_TX_RES=2, /**< Log outgoing STUN responses. */
  343. PJ_STUN_SESS_LOG_TX_IND=4, /**< Log outgoing STUN indications. */
  344. PJ_STUN_SESS_LOG_RX_REQ=8, /**< Log incoming STUN requests. */
  345. PJ_STUN_SESS_LOG_RX_RES=16, /**< Log incoming STUN responses */
  346. PJ_STUN_SESS_LOG_RX_IND=32 /**< Log incoming STUN indications */
  347. } pj_stun_sess_msg_log_flag;
  348. /**
  349. * Create a STUN session.
  350. *
  351. * @param cfg The STUN endpoint, to be used to register timers etc.
  352. * @param name Optional name to be associated with this instance. The
  353. * name will be used for example for logging purpose.
  354. * @param cb Session callback.
  355. * @param fingerprint Enable message fingerprint for outgoing messages.
  356. * @param grp_lock Optional group lock to be used by this session.
  357. * If NULL, the session will create one itself.
  358. * @param p_sess Pointer to receive STUN session instance.
  359. *
  360. * @return PJ_SUCCESS on success, or the appropriate error code.
  361. */
  362. PJ_DECL(pj_status_t) pj_stun_session_create(pj_stun_config *cfg,
  363. const char *name,
  364. const pj_stun_session_cb *cb,
  365. pj_bool_t fingerprint,
  366. pj_grp_lock_t *grp_lock,
  367. pj_stun_session **p_sess);
  368. /**
  369. * Destroy the STUN session and all objects created in the context of
  370. * this session.
  371. *
  372. * @param sess The STUN session instance.
  373. *
  374. * @return PJ_SUCCESS on success, or the appropriate error code.
  375. * This function will return PJ_EPENDING if the operation
  376. * cannot be performed immediately because callbacks are
  377. * being called; in this case the session will be destroyed
  378. * as soon as the last callback returns.
  379. */
  380. PJ_DECL(pj_status_t) pj_stun_session_destroy(pj_stun_session *sess);
  381. /**
  382. * Associated an arbitrary data with this STUN session. The user data may
  383. * be retrieved later with pj_stun_session_get_user_data() function.
  384. *
  385. * @param sess The STUN session instance.
  386. * @param user_data The user data.
  387. *
  388. * @return PJ_SUCCESS on success, or the appropriate error code.
  389. */
  390. PJ_DECL(pj_status_t) pj_stun_session_set_user_data(pj_stun_session *sess,
  391. void *user_data);
  392. /**
  393. * Retrieve the user data previously associated to this STUN session with
  394. * pj_stun_session_set_user_data().
  395. *
  396. * @param sess The STUN session instance.
  397. *
  398. * @return The user data associated with this STUN session.
  399. */
  400. PJ_DECL(void*) pj_stun_session_get_user_data(pj_stun_session *sess);
  401. /**
  402. * Get the group lock for this STUN session.
  403. *
  404. * @param sess The STUN session instance.
  405. *
  406. * @return The group lock.
  407. */
  408. PJ_DECL(pj_grp_lock_t *) pj_stun_session_get_grp_lock(pj_stun_session *sess);
  409. /**
  410. * Set SOFTWARE name to be included in all requests and responses.
  411. *
  412. * @param sess The STUN session instance.
  413. * @param sw Software name string. If this argument is NULL or
  414. * empty, the session will not include SOFTWARE attribute
  415. * in STUN requests and responses.
  416. *
  417. * @return PJ_SUCCESS on success, or the appropriate error code.
  418. */
  419. PJ_DECL(pj_status_t) pj_stun_session_set_software_name(pj_stun_session *sess,
  420. const pj_str_t *sw);
  421. /**
  422. * Set credential to be used by this session. Once credential is set, all
  423. * outgoing messages will include MESSAGE-INTEGRITY, and all incoming
  424. * message will be authenticated against this credential.
  425. *
  426. * To disable authentication after it has been set, call this function
  427. * again with NULL as the argument.
  428. *
  429. * @param sess The STUN session instance.
  430. * @param auth_type Type of authentication.
  431. * @param cred The credential to be used by this session. If NULL
  432. * is specified, authentication will be disabled.
  433. *
  434. * @return PJ_SUCCESS on success, or the appropriate error code.
  435. */
  436. PJ_DECL(pj_status_t) pj_stun_session_set_credential(pj_stun_session *sess,
  437. pj_stun_auth_type auth_type,
  438. const pj_stun_auth_cred *cred);
  439. /**
  440. * Configure message logging. By default all flags are enabled.
  441. *
  442. * @param sess The STUN session instance.
  443. * @param flags Bitmask combination of #pj_stun_sess_msg_log_flag
  444. */
  445. PJ_DECL(void) pj_stun_session_set_log(pj_stun_session *sess,
  446. unsigned flags);
  447. /**
  448. * Configure whether the STUN session should utilize FINGERPRINT in
  449. * outgoing messages.
  450. *
  451. * @param sess The STUN session instance.
  452. * @param use Boolean for the setting.
  453. *
  454. * @return The previous configured value of FINGERPRINT
  455. * utilization of the sessoin.
  456. */
  457. PJ_DECL(pj_bool_t) pj_stun_session_use_fingerprint(pj_stun_session *sess,
  458. pj_bool_t use);
  459. /**
  460. * Create a STUN request message. After the message has been successfully
  461. * created, application can send the message by calling
  462. * pj_stun_session_send_msg().
  463. *
  464. * @param sess The STUN session instance.
  465. * @param msg_type The STUN request message type, from pj_stun_method_e or
  466. * from pj_stun_msg_type.
  467. * @param magic STUN magic, use PJ_STUN_MAGIC.
  468. * @param tsx_id Optional transaction ID.
  469. * @param p_tdata Pointer to receive STUN transmit data instance containing
  470. * the request.
  471. *
  472. * @return PJ_SUCCESS on success, or the appropriate error code.
  473. */
  474. PJ_DECL(pj_status_t) pj_stun_session_create_req(pj_stun_session *sess,
  475. int msg_type,
  476. pj_uint32_t magic,
  477. const pj_uint8_t tsx_id[12],
  478. pj_stun_tx_data **p_tdata);
  479. /**
  480. * Create a STUN Indication message. After the message has been successfully
  481. * created, application can send the message by calling
  482. * pj_stun_session_send_msg().
  483. *
  484. * @param sess The STUN session instance.
  485. * @param msg_type The STUN request message type, from pj_stun_method_e or
  486. * from pj_stun_msg_type. This function will add the
  487. * indication bit as necessary.
  488. * @param p_tdata Pointer to receive STUN transmit data instance containing
  489. * the message.
  490. *
  491. * @return PJ_SUCCESS on success, or the appropriate error code.
  492. */
  493. PJ_DECL(pj_status_t) pj_stun_session_create_ind(pj_stun_session *sess,
  494. int msg_type,
  495. pj_stun_tx_data **p_tdata);
  496. /**
  497. * Create a STUN response message. After the message has been
  498. * successfully created, application can send the message by calling
  499. * pj_stun_session_send_msg(). Alternatively application may use
  500. * pj_stun_session_respond() to create and send response in one function
  501. * call.
  502. *
  503. * @param sess The STUN session instance.
  504. * @param rdata The STUN request where the response is to be created.
  505. * @param err_code Error code to be set in the response, if error response
  506. * is to be created, according to pj_stun_status enumeration.
  507. * This argument MUST be zero if successful response is
  508. * to be created.
  509. * @param err_msg Optional pointer for the error message string, when
  510. * creating error response. If the value is NULL and the
  511. * \a err_code is non-zero, then default error message will
  512. * be used.
  513. * @param p_tdata Pointer to receive the response message created.
  514. *
  515. * @return PJ_SUCCESS on success, or the appropriate error code.
  516. */
  517. PJ_DECL(pj_status_t) pj_stun_session_create_res(pj_stun_session *sess,
  518. const pj_stun_rx_data *rdata,
  519. unsigned err_code,
  520. const pj_str_t *err_msg,
  521. pj_stun_tx_data **p_tdata);
  522. /**
  523. * Send STUN message to the specified destination. This function will encode
  524. * the pj_stun_msg instance to a packet buffer, and add credential or
  525. * fingerprint if necessary. If the message is a request, the session will
  526. * also create and manage a STUN client transaction to be used to manage the
  527. * retransmission of the request. After the message has been encoded and
  528. * transaction is setup, the \a on_send_msg() callback of pj_stun_session_cb
  529. * (which is registered when the STUN session is created) will be called
  530. * to actually send the message to the wire.
  531. *
  532. * @param sess The STUN session instance.
  533. * @param token Optional token which will be given back to application in
  534. * \a on_send_msg() callback and \a on_request_complete()
  535. * callback, if the message is a STUN request message.
  536. * Internally this function will put the token in the
  537. * \a token field of pj_stun_tx_data, hence it will
  538. * overwrite any value that the application puts there.
  539. * @param cache_res If the message is a response message for an incoming
  540. * request, specify PJ_TRUE to instruct the STUN session
  541. * to cache this response for subsequent incoming request
  542. * retransmission. Otherwise this parameter will be ignored
  543. * for non-response message.
  544. * @param retransmit If the message is a request message, specify whether the
  545. * request should be retransmitted. Normally application will
  546. * specify TRUE if the underlying transport is UDP and FALSE
  547. * if the underlying transport is TCP or TLS.
  548. * @param dst_addr The destination socket address.
  549. * @param addr_len Length of destination address.
  550. * @param tdata The STUN transmit data containing the STUN message to
  551. * be sent.
  552. *
  553. * @return PJ_SUCCESS on success, or the appropriate error code.
  554. * This function will return PJNATH_ESTUNDESTROYED if
  555. * application has destroyed the session in
  556. * \a on_send_msg() callback.
  557. */
  558. PJ_DECL(pj_status_t) pj_stun_session_send_msg(pj_stun_session *sess,
  559. void *token,
  560. pj_bool_t cache_res,
  561. pj_bool_t retransmit,
  562. const pj_sockaddr_t *dst_addr,
  563. unsigned addr_len,
  564. pj_stun_tx_data *tdata);
  565. /**
  566. * This is a utility function to create and send response for an incoming
  567. * STUN request. Internally this function calls pj_stun_session_create_res()
  568. * and pj_stun_session_send_msg(). It is provided here as a matter of
  569. * convenience.
  570. *
  571. * @param sess The STUN session instance.
  572. * @param rdata The STUN request message to be responded.
  573. * @param code Error code to be set in the response, if error response
  574. * is to be created, according to pj_stun_status enumeration.
  575. * This argument MUST be zero if successful response is
  576. * to be created.
  577. * @param err_msg Optional pointer for the error message string, when
  578. * creating error response. If the value is NULL and the
  579. * \a err_code is non-zero, then default error message will
  580. * be used.
  581. * @param token Optional token which will be given back to application in
  582. * \a on_send_msg() callback and \a on_request_complete()
  583. * callback, if the message is a STUN request message.
  584. * Internally this function will put the token in the
  585. * \a token field of pj_stun_tx_data, hence it will
  586. * overwrite any value that the application puts there.
  587. * @param cache Specify whether session should cache this response for
  588. * future request retransmission. If TRUE, subsequent request
  589. * retransmission will be handled by the session and it
  590. * will not call request callback.
  591. * @param dst_addr Destination address of the response (or equal to the
  592. * source address of the original request).
  593. * @param addr_len Address length.
  594. *
  595. * @return PJ_SUCCESS on success, or the appropriate error code.
  596. * This function will return PJNATH_ESTUNDESTROYED if
  597. * application has destroyed the session in
  598. * \a on_send_msg() callback.
  599. */
  600. PJ_DECL(pj_status_t) pj_stun_session_respond(pj_stun_session *sess,
  601. const pj_stun_rx_data *rdata,
  602. unsigned code,
  603. const char *err_msg,
  604. void *token,
  605. pj_bool_t cache,
  606. const pj_sockaddr_t *dst_addr,
  607. unsigned addr_len);
  608. /**
  609. * Cancel outgoing STUN transaction. This operation is only valid for outgoing
  610. * STUN request, to cease retransmission of the request and destroy the
  611. * STUN client transaction that is used to send the request.
  612. *
  613. * @param sess The STUN session instance.
  614. * @param tdata The request message previously sent.
  615. * @param notify Specify whether \a on_request_complete() callback should
  616. * be called.
  617. * @param status If \a on_request_complete() callback is to be called,
  618. * specify the error status to be given when calling the
  619. * callback. This error status MUST NOT be PJ_SUCCESS.
  620. *
  621. * @return PJ_SUCCESS if transaction is successfully cancelled.
  622. * This function will return PJNATH_ESTUNDESTROYED if
  623. * application has destroyed the session in
  624. * \a on_request_complete() callback.
  625. */
  626. PJ_DECL(pj_status_t) pj_stun_session_cancel_req(pj_stun_session *sess,
  627. pj_stun_tx_data *tdata,
  628. pj_bool_t notify,
  629. pj_status_t status);
  630. /**
  631. * Explicitly request retransmission of the request. Normally application
  632. * doesn't need to do this, but this functionality is needed by ICE to
  633. * speed up connectivity check completion.
  634. *
  635. * @param sess The STUN session instance.
  636. * @param tdata The request message previously sent.
  637. * @param mod_count Boolean flag to indicate whether transmission count
  638. * needs to be incremented.
  639. *
  640. * @return PJ_SUCCESS on success, or the appropriate error.
  641. * This function will return PJNATH_ESTUNDESTROYED if
  642. * application has destroyed the session in \a on_send_msg()
  643. * callback.
  644. */
  645. PJ_DECL(pj_status_t) pj_stun_session_retransmit_req(pj_stun_session *sess,
  646. pj_stun_tx_data *tdata,
  647. pj_bool_t mod_count);
  648. /**
  649. * Application must call this function to notify the STUN session about
  650. * the arrival of STUN packet. The STUN packet MUST have been checked
  651. * first with #pj_stun_msg_check() to verify that this is indeed a valid
  652. * STUN packet.
  653. *
  654. * The STUN session will decode the packet into pj_stun_msg, and process
  655. * the message accordingly. If the message is a response, it will search
  656. * through the outstanding STUN client transactions for a matching
  657. * transaction ID and hand over the response to the transaction.
  658. *
  659. * On successful message processing, application will be notified about
  660. * the message via one of the pj_stun_session_cb callback.
  661. *
  662. * @param sess The STUN session instance.
  663. * @param packet The packet containing STUN message.
  664. * @param pkt_size Size of the packet.
  665. * @param options Options, from #pj_stun_decode_options.
  666. * @param parsed_len Optional pointer to receive the size of the parsed
  667. * STUN message (useful if packet is received via a
  668. * stream oriented protocol).
  669. * @param token Optional token which will be given back to application
  670. * in the \a on_rx_request(), \a on_rx_indication() and
  671. * \a on_send_msg() callbacks. The token can be used to
  672. * associate processing or incoming request or indication
  673. * with some context.
  674. * @param src_addr The source address of the packet, which will also
  675. * be given back to application callbacks, along with
  676. * source address length.
  677. * @param src_addr_len Length of the source address.
  678. *
  679. * @return PJ_SUCCESS on success, or the appropriate error code.
  680. * This function will return PJNATH_ESTUNDESTROYED if
  681. * application has destroyed the session in one of the
  682. * callback.
  683. */
  684. PJ_DECL(pj_status_t) pj_stun_session_on_rx_pkt(pj_stun_session *sess,
  685. const void *packet,
  686. pj_size_t pkt_size,
  687. unsigned options,
  688. void *token,
  689. pj_size_t *parsed_len,
  690. const pj_sockaddr_t *src_addr,
  691. unsigned src_addr_len);
  692. /**
  693. * Destroy the transmit data. Call this function only when tdata has been
  694. * created but application doesn't want to send the message (perhaps
  695. * because of other error).
  696. *
  697. * @param sess The STUN session.
  698. * @param tdata The transmit data.
  699. *
  700. */
  701. PJ_DECL(void) pj_stun_msg_destroy_tdata(pj_stun_session *sess,
  702. pj_stun_tx_data *tdata);
  703. /**
  704. * @}
  705. */
  706. PJ_END_DECL
  707. #endif /* __PJNATH_STUN_SESSION_H__ */