res_pjsip_session.h 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2013, Digium, Inc.
  5. *
  6. * Mark Michelson <mmichelson@digium.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. #ifndef _RES_PJSIP_SESSION_H
  19. #define _RES_PJSIP_SESSION_H
  20. /* Needed for pj_timer_entry definition */
  21. #include <pjlib.h>
  22. #include "asterisk/linkedlists.h"
  23. /* Needed for AST_MAX_EXTENSION constant */
  24. #include "asterisk/channel.h"
  25. /* Needed for ast_sockaddr struct */
  26. #include "asterisk/netsock2.h"
  27. /* Needed for ast_sdp_srtp struct */
  28. #include "asterisk/sdp_srtp.h"
  29. /* Forward declarations */
  30. struct ast_sip_endpoint;
  31. struct ast_sip_transport;
  32. struct pjsip_inv_session;
  33. struct ast_channel;
  34. struct ast_datastore;
  35. struct ast_datastore_info;
  36. struct ao2_container;
  37. struct pjsip_tx_data;
  38. struct pjsip_rx_data;
  39. struct ast_party_id;
  40. struct pjmedia_sdp_media;
  41. struct pjmedia_sdp_session;
  42. struct ast_dsp;
  43. struct ast_udptl;
  44. /*! \brief T.38 states for a session */
  45. enum ast_sip_session_t38state {
  46. T38_DISABLED = 0, /*!< Not enabled */
  47. T38_LOCAL_REINVITE, /*!< Offered from local - REINVITE */
  48. T38_PEER_REINVITE, /*!< Offered from peer - REINVITE */
  49. T38_ENABLED, /*!< Negotiated (enabled) */
  50. T38_REJECTED, /*!< Refused */
  51. T38_MAX_ENUM, /*!< Not an actual state; used as max value in the enum */
  52. };
  53. struct ast_sip_session_sdp_handler;
  54. /*!
  55. * \brief A structure containing SIP session media information
  56. */
  57. struct ast_sip_session_media {
  58. union {
  59. /*! \brief RTP instance itself */
  60. struct ast_rtp_instance *rtp;
  61. /*! \brief UDPTL instance itself */
  62. struct ast_udptl *udptl;
  63. };
  64. /*! \brief Direct media address */
  65. struct ast_sockaddr direct_media_addr;
  66. /*! \brief SDP handler that setup the RTP */
  67. struct ast_sip_session_sdp_handler *handler;
  68. /*! \brief Holds SRTP information */
  69. struct ast_sdp_srtp *srtp;
  70. /*! \brief What type of encryption is in use on this stream */
  71. enum ast_sip_session_media_encryption encryption;
  72. /*! \brief The media transport in use for this stream */
  73. pj_str_t transport;
  74. /*! \brief Scheduler ID for RTP keepalive */
  75. int keepalive_sched_id;
  76. /*! \brief Scheduler ID for RTP timeout */
  77. int timeout_sched_id;
  78. /*! \brief Stream is on hold */
  79. unsigned int held:1;
  80. /*! \brief Does remote support rtcp_mux */
  81. unsigned int remote_rtcp_mux:1;
  82. /*! \brief Does remote support ice */
  83. unsigned int remote_ice:1;
  84. /*! \brief Stream type this session media handles */
  85. char stream_type[1];
  86. };
  87. /*!
  88. * \brief Opaque structure representing a request that could not be sent
  89. * due to an outstanding INVITE transaction
  90. */
  91. struct ast_sip_session_delayed_request;
  92. /*! \brief Opaque struct controlling the suspension of the session's serializer. */
  93. struct ast_sip_session_suspender;
  94. /*!
  95. * \brief A structure describing a SIP session
  96. *
  97. * For the sake of brevity, a "SIP session" in Asterisk is referring to
  98. * a dialog initiated by an INVITE. While "session" is typically interpreted
  99. * to refer to the negotiated media within a SIP dialog, we have opted
  100. * to use the term "SIP session" to refer to the INVITE dialog itself.
  101. */
  102. struct ast_sip_session {
  103. /*! Dialplan extension where incoming call is destined */
  104. char exten[AST_MAX_EXTENSION];
  105. /*! The endpoint with which Asterisk is communicating */
  106. struct ast_sip_endpoint *endpoint;
  107. /*! The contact associated with this session */
  108. struct ast_sip_contact *contact;
  109. /*! The PJSIP details of the session, which includes the dialog */
  110. struct pjsip_inv_session *inv_session;
  111. /*! The Asterisk channel associated with the session */
  112. struct ast_channel *channel;
  113. /*! Registered session supplements */
  114. AST_LIST_HEAD(, ast_sip_session_supplement) supplements;
  115. /*! Datastores added to the session by supplements to the session */
  116. struct ao2_container *datastores;
  117. /*! Media streams */
  118. struct ao2_container *media;
  119. /*! Serializer for tasks relating to this SIP session */
  120. struct ast_taskprocessor *serializer;
  121. /*! Non-null if the session serializer is suspended or being suspended. */
  122. struct ast_sip_session_suspender *suspended;
  123. /*! Requests that could not be sent due to current inv_session state */
  124. AST_LIST_HEAD_NOLOCK(, ast_sip_session_delayed_request) delayed_requests;
  125. /*! When we need to reschedule a reinvite, we use this structure to do it */
  126. pj_timer_entry rescheduled_reinvite;
  127. /*! Format capabilities pertaining to direct media */
  128. struct ast_format_cap *direct_media_cap;
  129. /*! When we need to forcefully end the session */
  130. pj_timer_entry scheduled_termination;
  131. /*! Identity of endpoint this session deals with */
  132. struct ast_party_id id;
  133. /*! Requested capabilities */
  134. struct ast_format_cap *req_caps;
  135. /*! Optional DSP, used only for inband DTMF/Fax-CNG detection if configured */
  136. struct ast_dsp *dsp;
  137. /*! Whether the termination of the session should be deferred */
  138. unsigned int defer_terminate:1;
  139. /*! Termination requested while termination deferred */
  140. unsigned int terminate_while_deferred:1;
  141. /*! Deferred incoming re-invite */
  142. pjsip_rx_data *deferred_reinvite;
  143. /*! Current T.38 state */
  144. enum ast_sip_session_t38state t38state;
  145. /*! The AOR associated with this session */
  146. struct ast_sip_aor *aor;
  147. /*! From header saved at invite creation */
  148. pjsip_fromto_hdr *saved_from_hdr;
  149. /*! Whether the end of the session should be deferred */
  150. unsigned int defer_end:1;
  151. /*! Session end (remote hangup) requested while termination deferred */
  152. unsigned int ended_while_deferred:1;
  153. /*! DTMF mode to use with this session, from endpoint but can change */
  154. enum ast_sip_dtmf_mode dtmf;
  155. /*! Initial incoming INVITE Request-URI. NULL otherwise. */
  156. pjsip_uri *request_uri;
  157. };
  158. typedef int (*ast_sip_session_request_creation_cb)(struct ast_sip_session *session, pjsip_tx_data *tdata);
  159. typedef int (*ast_sip_session_response_cb)(struct ast_sip_session *session, pjsip_rx_data *rdata);
  160. typedef int (*ast_sip_session_sdp_creation_cb)(struct ast_sip_session *session, pjmedia_sdp_session *sdp);
  161. /*!
  162. * \brief Describes when a supplement should be called into on incoming responses.
  163. *
  164. * In most cases, session supplements will not need to worry about this because in most cases,
  165. * the correct value will be automatically applied. However, there are rare circumstances
  166. * when a supplement will want to specify when it should be called.
  167. *
  168. * The values below are listed in chronological order.
  169. */
  170. enum ast_sip_session_response_priority {
  171. /*!
  172. * When processing 3XX responses, the supplement is called into before
  173. * the redirecting information is processed.
  174. */
  175. AST_SIP_SESSION_BEFORE_REDIRECTING = (1 << 0),
  176. /*!
  177. * For responses to INVITE transactions, the supplement is called into
  178. * before media is negotiated.
  179. *
  180. * This priority is applied by default to any session supplement that
  181. * does not specify a response priority.
  182. */
  183. AST_SIP_SESSION_BEFORE_MEDIA = (1 << 1),
  184. /*!
  185. * For INVITE transactions, the supplement is called into after media
  186. * is negotiated.
  187. */
  188. AST_SIP_SESSION_AFTER_MEDIA = (1 << 2),
  189. };
  190. /*!
  191. * \brief A supplement to SIP message processing
  192. *
  193. * These can be registered by any module in order to add
  194. * processing to incoming and outgoing SIP requests and responses
  195. */
  196. struct ast_sip_session_supplement {
  197. /*! Method on which to call the callbacks. If NULL, call on all methods */
  198. const char *method;
  199. /*! Priority for this supplement. Lower numbers are visited before higher numbers */
  200. enum ast_sip_supplement_priority priority;
  201. /*!
  202. * \brief Notification that the session has begun
  203. * This method will always be called from a SIP servant thread.
  204. */
  205. void (*session_begin)(struct ast_sip_session *session);
  206. /*!
  207. * \brief Notification that the session has ended
  208. *
  209. * This method may or may not be called from a SIP servant thread. Do
  210. * not make assumptions about being able to call PJSIP methods from within
  211. * this method.
  212. */
  213. void (*session_end)(struct ast_sip_session *session);
  214. /*!
  215. * \brief Notification that the session is being destroyed
  216. */
  217. void (*session_destroy)(struct ast_sip_session *session);
  218. /*!
  219. * \brief Called on incoming SIP request
  220. * This method can indicate a failure in processing in its return. If there
  221. * is a failure, it is required that this method sends a response to the request.
  222. * This method is always called from a SIP servant thread.
  223. *
  224. * \note
  225. * The following PJSIP methods will not work properly:
  226. * pjsip_rdata_get_dlg()
  227. * pjsip_rdata_get_tsx()
  228. * The reason is that the rdata passed into this function is a cloned rdata structure,
  229. * and its module data is not copied during the cloning operation.
  230. * If you need to get the dialog, you can get it via session->inv_session->dlg.
  231. *
  232. * \note
  233. * There is no guarantee that a channel will be present on the session when this is called.
  234. */
  235. int (*incoming_request)(struct ast_sip_session *session, struct pjsip_rx_data *rdata);
  236. /*!
  237. * \brief Called on an incoming SIP response
  238. * This method is always called from a SIP servant thread.
  239. *
  240. * \note
  241. * The following PJSIP methods will not work properly:
  242. * pjsip_rdata_get_dlg()
  243. * pjsip_rdata_get_tsx()
  244. * The reason is that the rdata passed into this function is a cloned rdata structure,
  245. * and its module data is not copied during the cloning operation.
  246. * If you need to get the dialog, you can get it via session->inv_session->dlg.
  247. *
  248. * \note
  249. * There is no guarantee that a channel will be present on the session when this is called.
  250. */
  251. void (*incoming_response)(struct ast_sip_session *session, struct pjsip_rx_data *rdata);
  252. /*!
  253. * \brief Called on an outgoing SIP request
  254. * This method is always called from a SIP servant thread.
  255. */
  256. void (*outgoing_request)(struct ast_sip_session *session, struct pjsip_tx_data *tdata);
  257. /*!
  258. * \brief Called on an outgoing SIP response
  259. * This method is always called from a SIP servant thread.
  260. */
  261. void (*outgoing_response)(struct ast_sip_session *session, struct pjsip_tx_data *tdata);
  262. /*! Next item in the list */
  263. AST_LIST_ENTRY(ast_sip_session_supplement) next;
  264. /*!
  265. * Determines when the supplement is processed when handling a response.
  266. * Defaults to AST_SIP_SESSION_BEFORE_MEDIA
  267. */
  268. enum ast_sip_session_response_priority response_priority;
  269. };
  270. enum ast_sip_session_sdp_stream_defer {
  271. /*! The stream was not handled by this handler. If there are other registered handlers for this stream type, they will be called. */
  272. AST_SIP_SESSION_SDP_DEFER_NOT_HANDLED,
  273. /*! There was an error encountered. No further operations will take place and the current negotiation will be abandoned. */
  274. AST_SIP_SESSION_SDP_DEFER_ERROR,
  275. /*! Re-invite is not needed */
  276. AST_SIP_SESSION_SDP_DEFER_NOT_NEEDED,
  277. /*! Re-invite should be deferred and will be resumed later. No further operations will take place. */
  278. AST_SIP_SESSION_SDP_DEFER_NEEDED,
  279. };
  280. /*!
  281. * \brief A handler for SDPs in SIP sessions
  282. *
  283. * An SDP handler is registered by a module that is interested in being the
  284. * responsible party for specific types of SDP streams.
  285. */
  286. struct ast_sip_session_sdp_handler {
  287. /*! An identifier for this handler */
  288. const char *id;
  289. /*!
  290. * \brief Determine whether a stream requires that the re-invite be deferred.
  291. * If a stream can not be immediately negotiated the re-invite can be deferred and
  292. * resumed at a later time. It is up to the handler which caused deferral to occur
  293. * to resume it.
  294. *
  295. * \param session The session for which the media is being re-invited
  296. * \param session_media The media being reinvited
  297. * \param sdp The entire SDP. Useful for getting "global" information, such as connections or attributes
  298. * \param stream PJSIP incoming SDP media lines to parse by handler.
  299. *
  300. * \return enum ast_sip_session_defer_stream
  301. *
  302. * \note This is optional, if not implemented the stream is assumed to not be deferred.
  303. */
  304. enum ast_sip_session_sdp_stream_defer (*defer_incoming_sdp_stream)(struct ast_sip_session *session, struct ast_sip_session_media *session_media, const struct pjmedia_sdp_session *sdp, const struct pjmedia_sdp_media *stream);
  305. /*!
  306. * \brief Set session details based on a stream in an incoming SDP offer or answer
  307. * \param session The session for which the media is being negotiated
  308. * \param session_media The media to be setup for this session
  309. * \param sdp The entire SDP. Useful for getting "global" information, such as connections or attributes
  310. * \param stream The stream on which to operate
  311. * \retval 0 The stream was not handled by this handler. If there are other registered handlers for this stream type, they will be called.
  312. * \retval <0 There was an error encountered. No further operation will take place and the current negotiation will be abandoned.
  313. * \retval >0 The stream was handled by this handler. No further handler of this stream type will be called.
  314. */
  315. int (*negotiate_incoming_sdp_stream)(struct ast_sip_session *session, struct ast_sip_session_media *session_media, const struct pjmedia_sdp_session *sdp, const struct pjmedia_sdp_media *stream);
  316. /*!
  317. * \brief Create an SDP media stream and add it to the outgoing SDP offer or answer
  318. * \param session The session for which media is being added
  319. * \param session_media The media to be setup for this session
  320. * \param stream The stream on which to operate
  321. * \retval 0 The stream was not handled by this handler. If there are other registered handlers for this stream type, they will be called.
  322. * \retval <0 There was an error encountered. No further operation will take place and the current negotiation will be abandoned.
  323. * \retval >0 The stream was handled by this handler. No further handler of this stream type will be called.
  324. */
  325. int (*handle_incoming_sdp_stream)(struct ast_sip_session *session, struct ast_sip_session_media *session_media, const struct pjmedia_sdp_session *sdp, struct pjmedia_sdp_media *stream);
  326. /*!
  327. * \brief Create an SDP media stream and add it to the outgoing SDP offer or answer
  328. * \param session The session for which media is being added
  329. * \param session_media The media to be setup for this session
  330. * \param sdp The entire SDP as currently built
  331. * \retval 0 This handler has no stream to add. If there are other registered handlers for this stream type, they will be called.
  332. * \retval <0 There was an error encountered. No further operation will take place and the current SDP negotiation will be abandoned.
  333. * \retval >0 The handler has a stream to be added to the SDP. No further handler of this stream type will be called.
  334. */
  335. int (*create_outgoing_sdp_stream)(struct ast_sip_session *session, struct ast_sip_session_media *session_media, struct pjmedia_sdp_session *sdp);
  336. /*!
  337. * \brief Update media stream with external address if applicable
  338. * \param tdata The outgoing message itself
  339. * \param stream The stream on which to operate
  340. * \param transport The transport the SDP is going out on
  341. */
  342. void (*change_outgoing_sdp_stream_media_address)(struct pjsip_tx_data *tdata, struct pjmedia_sdp_media *stream, struct ast_sip_transport *transport);
  343. /*!
  344. * \brief Apply a negotiated SDP media stream
  345. * \param session The session for which media is being applied
  346. * \param session_media The media to be setup for this session
  347. * \param local The entire local negotiated SDP
  348. * \param local_stream The local stream which to apply
  349. * \param remote The entire remote negotiated SDP
  350. * \param remote_stream The remote stream which to apply
  351. * \retval 0 The stream was not applied by this handler. If there are other registered handlers for this stream type, they will be called.
  352. * \retval <0 There was an error encountered. No further operation will take place and the current application will be abandoned.
  353. * \retval >0 The stream was handled by this handler. No further handler of this stream type will be called.
  354. */
  355. int (*apply_negotiated_sdp_stream)(struct ast_sip_session *session, struct ast_sip_session_media *session_media, const struct pjmedia_sdp_session *local, const struct pjmedia_sdp_media *local_stream,
  356. const struct pjmedia_sdp_session *remote, const struct pjmedia_sdp_media *remote_stream);
  357. /*!
  358. * \brief Stop a session_media created by this handler but do not destroy resources
  359. * \param session The session for which media is being stopped
  360. * \param session_media The media to destroy
  361. */
  362. void (*stream_stop)(struct ast_sip_session_media *session_media);
  363. /*!
  364. * \brief Destroy a session_media created by this handler
  365. * \param session The session for which media is being destroyed
  366. * \param session_media The media to destroy
  367. */
  368. void (*stream_destroy)(struct ast_sip_session_media *session_media);
  369. /*! Next item in the list. */
  370. AST_LIST_ENTRY(ast_sip_session_sdp_handler) next;
  371. };
  372. /*!
  373. * \brief A structure which contains a channel implementation and session
  374. */
  375. struct ast_sip_channel_pvt {
  376. /*! \brief Pointer to channel specific implementation information, must be ao2 object */
  377. void *pvt;
  378. /*! \brief Pointer to session */
  379. struct ast_sip_session *session;
  380. };
  381. /*!
  382. * \brief Allocate a new SIP channel pvt structure
  383. *
  384. * \param pvt Pointer to channel specific implementation
  385. * \param session Pointer to SIP session
  386. *
  387. * \retval non-NULL success
  388. * \retval NULL failure
  389. */
  390. struct ast_sip_channel_pvt *ast_sip_channel_pvt_alloc(void *pvt, struct ast_sip_session *session);
  391. /*!
  392. * \brief Allocate a new SIP session
  393. *
  394. * This will take care of allocating the datastores container on the session as well
  395. * as placing all registered supplements onto the session.
  396. *
  397. * The endpoint that is passed in will have its reference count increased by one since
  398. * the session will be keeping a reference to the endpoint. The session will relinquish
  399. * this reference when the session is destroyed.
  400. *
  401. * \param endpoint The endpoint that this session communicates with
  402. * \param contact The contact associated with this session
  403. * \param inv_session The PJSIP INVITE session data
  404. * \param rdata INVITE request received (NULL if for outgoing allocation)
  405. */
  406. struct ast_sip_session *ast_sip_session_alloc(struct ast_sip_endpoint *endpoint,
  407. struct ast_sip_contact *contact, pjsip_inv_session *inv, pjsip_rx_data *rdata);
  408. /*!
  409. * \brief Request and wait for the session serializer to be suspended.
  410. * \since 12.7.0
  411. *
  412. * \param session Which session to suspend the serializer.
  413. *
  414. * \note No channel locks can be held while calling without risk of deadlock.
  415. *
  416. * \return Nothing
  417. */
  418. void ast_sip_session_suspend(struct ast_sip_session *session);
  419. /*!
  420. * \brief Request the session serializer be unsuspended.
  421. * \since 12.7.0
  422. *
  423. * \param session Which session to unsuspend the serializer.
  424. *
  425. * \return Nothing
  426. */
  427. void ast_sip_session_unsuspend(struct ast_sip_session *session);
  428. /*!
  429. * \brief Create a new outgoing SIP session
  430. *
  431. * The endpoint that is passed in will have its reference count increased by one since
  432. * the session will be keeping a reference to the endpoint. The session will relinquish
  433. * this reference when the session is destroyed.
  434. *
  435. * \param endpoint The endpoint that this session uses for settings
  436. * \param contact The contact that this session will communicate with
  437. * \param location Name of the location to call, be it named location or explicit URI. Overrides contact if present.
  438. * \param request_user Optional request user to place in the request URI if permitted
  439. * \param req_caps The requested capabilities
  440. */
  441. struct ast_sip_session *ast_sip_session_create_outgoing(struct ast_sip_endpoint *endpoint,
  442. struct ast_sip_contact *contact, const char *location, const char *request_user,
  443. struct ast_format_cap *req_caps);
  444. /*!
  445. * \brief Terminate a session and, if possible, send the provided response code
  446. *
  447. * \param session The session to terminate
  448. * \param response The response code to use for termination if possible
  449. *
  450. * \warning Calling this function MAY cause the last session reference to be
  451. * released and the session destructor to be called. If you need to do something
  452. * with session after this call, be sure to bump the ref count before calling terminate.
  453. */
  454. void ast_sip_session_terminate(struct ast_sip_session *session, int response);
  455. /*!
  456. * \brief Defer local termination of a session until remote side terminates, or an amount of time passes
  457. *
  458. * \param session The session to defer termination on
  459. *
  460. * \retval 0 Success
  461. * \retval -1 Failure
  462. */
  463. int ast_sip_session_defer_termination(struct ast_sip_session *session);
  464. /*!
  465. * \brief Cancel a pending deferred termination.
  466. *
  467. * \param session The session to cancel a deferred termination on.
  468. */
  469. void ast_sip_session_defer_termination_cancel(struct ast_sip_session *session);
  470. /*!
  471. * \brief End the session if it had been previously deferred
  472. *
  473. * \param session The session to end if it had been deferred
  474. */
  475. void ast_sip_session_end_if_deferred(struct ast_sip_session *session);
  476. /*!
  477. * \brief Register an SDP handler
  478. *
  479. * An SDP handler is responsible for parsing incoming SDP streams and ensuring that
  480. * Asterisk can cope with the contents. Similarly, the SDP handler will be
  481. * responsible for constructing outgoing SDP streams.
  482. *
  483. * Multiple handlers for the same stream type may be registered. They will be
  484. * visited in the order they were registered. Handlers will be visited for each
  485. * stream type until one claims to have handled the stream.
  486. *
  487. * \param handler The SDP handler to register
  488. * \param stream_type The type of media stream for which to call the handler
  489. * \retval 0 Success
  490. * \retval -1 Failure
  491. */
  492. int ast_sip_session_register_sdp_handler(struct ast_sip_session_sdp_handler *handler, const char *stream_type);
  493. /*!
  494. * \brief Unregister an SDP handler
  495. *
  496. * \param handler The SDP handler to unregister
  497. * \param stream_type Stream type for which the SDP handler was registered
  498. */
  499. void ast_sip_session_unregister_sdp_handler(struct ast_sip_session_sdp_handler *handler, const char *stream_type);
  500. /*!
  501. * \brief Register a supplement to SIP session processing
  502. *
  503. * This allows for someone to insert themselves in the processing of SIP
  504. * requests and responses. This, for example could allow for a module to
  505. * set channel data based on headers in an incoming message. Similarly,
  506. * a module could reject an incoming request if desired.
  507. *
  508. * \param supplement The supplement to register
  509. * \param module Referenced module(NULL safe)
  510. * \retval 0 Success
  511. * \retval -1 Failure
  512. */
  513. int ast_sip_session_register_supplement_with_module(struct ast_module *module, struct ast_sip_session_supplement *supplement);
  514. #define ast_sip_session_register_supplement(supplement) \
  515. ast_sip_session_register_supplement_with_module(ast_module_info->self, supplement)
  516. /*!
  517. * \brief Unregister a an supplement to SIP session processing
  518. *
  519. * \param supplement The supplement to unregister
  520. */
  521. void ast_sip_session_unregister_supplement(struct ast_sip_session_supplement *supplement);
  522. /*!
  523. * \brief Add supplements to a SIP session
  524. *
  525. * \param session The session to initialize
  526. */
  527. int ast_sip_session_add_supplements(struct ast_sip_session *session);
  528. /*!
  529. * \brief Alternative for ast_datastore_alloc()
  530. *
  531. * There are two major differences between this and ast_datastore_alloc()
  532. * 1) This allocates a refcounted object
  533. * 2) This will fill in a uid if one is not provided
  534. *
  535. * DO NOT call ast_datastore_free() on a datastore allocated in this
  536. * way since that function will attempt to free the datastore rather
  537. * than play nicely with its refcount.
  538. *
  539. * \param info Callbacks for datastore
  540. * \param uid Identifier for datastore
  541. * \retval NULL Failed to allocate datastore
  542. * \retval non-NULL Newly allocated datastore
  543. */
  544. struct ast_datastore *ast_sip_session_alloc_datastore(const struct ast_datastore_info *info, const char *uid);
  545. /*!
  546. * \brief Add a datastore to a SIP session
  547. *
  548. * Note that SIP uses reference counted datastores. The datastore passed into this function
  549. * must have been allocated using ao2_alloc() or there will be serious problems.
  550. *
  551. * \param session The session to add the datastore to
  552. * \param datastore The datastore to be added to the session
  553. * \retval 0 Success
  554. * \retval -1 Failure
  555. */
  556. int ast_sip_session_add_datastore(struct ast_sip_session *session, struct ast_datastore *datastore);
  557. /*!
  558. * \brief Retrieve a session datastore
  559. *
  560. * The datastore retrieved will have its reference count incremented. When the caller is done
  561. * with the datastore, the reference counted needs to be decremented using ao2_ref().
  562. *
  563. * \param session The session from which to retrieve the datastore
  564. * \param name The name of the datastore to retrieve
  565. * \retval NULL Failed to find the specified datastore
  566. * \retval non-NULL The specified datastore
  567. */
  568. struct ast_datastore *ast_sip_session_get_datastore(struct ast_sip_session *session, const char *name);
  569. /*!
  570. * \brief Remove a session datastore from the session
  571. *
  572. * This operation may cause the datastore's free() callback to be called if the reference
  573. * count reaches zero.
  574. *
  575. * \param session The session to remove the datastore from
  576. * \param name The name of the datastore to remove
  577. */
  578. void ast_sip_session_remove_datastore(struct ast_sip_session *session, const char *name);
  579. /*!
  580. * \brief Send a reinvite or UPDATE on a session
  581. *
  582. * This method will inspect the session in order to construct an appropriate
  583. * session refresh request. As with any outgoing request in res_pjsip_session,
  584. * this will call into registered supplements in case they wish to add anything.
  585. *
  586. * Note: The on_request_creation callback may or may not be called in the same
  587. * thread where this function is called. Request creation may need to be delayed
  588. * due to the current INVITE transaction state.
  589. *
  590. * \param session The session on which the reinvite will be sent
  591. * \param on_request_creation Callback called when request is created
  592. * \param on_sdp_creation Callback called when SDP is created
  593. * \param on_response Callback called when response for request is received
  594. * \param method The method that should be used when constructing the session refresh
  595. * \param generate_new_sdp Boolean to indicate if a new SDP should be created
  596. * \retval 0 Successfully sent refresh
  597. * \retval -1 Failure to send refresh
  598. */
  599. int ast_sip_session_refresh(struct ast_sip_session *session,
  600. ast_sip_session_request_creation_cb on_request_creation,
  601. ast_sip_session_sdp_creation_cb on_sdp_creation,
  602. ast_sip_session_response_cb on_response,
  603. enum ast_sip_session_refresh_method method,
  604. int generate_new_sdp);
  605. /*!
  606. * \brief Regenerate SDP Answer
  607. *
  608. * This method is used when an SDP offer has been received but an SDP answer
  609. * has not been sent yet. It requests that a new local SDP be created and
  610. * set as the SDP answer. As with any outgoing request in res_pjsip_session,
  611. * this will call into registered supplements in case they wish to add anything.
  612. *
  613. * \param session The session on which the answer will be updated
  614. * \param on_sdp_creation Callback called when SDP is created
  615. * \param generate_new_sdp Boolean to indicate if a new SDP should be created
  616. * \retval 0 Successfully updated the SDP answer
  617. * \retval -1 Failure to updated the SDP answer
  618. */
  619. int ast_sip_session_regenerate_answer(struct ast_sip_session *session,
  620. ast_sip_session_sdp_creation_cb on_sdp_creation);
  621. /*!
  622. * \brief Send a SIP response
  623. *
  624. * This will send the SIP response specified in tdata and
  625. * call into any registered supplements' outgoing_response callback.
  626. *
  627. * \param session The session on which to send the response.
  628. * \param tdata The response to send
  629. */
  630. void ast_sip_session_send_response(struct ast_sip_session *session, pjsip_tx_data *tdata);
  631. /*!
  632. * \brief Send a SIP request
  633. *
  634. * This will send the SIP request specified in tdata and
  635. * call into any registered supplements' outgoing_request callback.
  636. *
  637. * \param session The session to which to send the request
  638. * \param tdata The request to send
  639. */
  640. void ast_sip_session_send_request(struct ast_sip_session *session, pjsip_tx_data *tdata);
  641. /*!
  642. * \brief Creates an INVITE request.
  643. *
  644. * \param session Starting session for the INVITE
  645. * \param tdata The created request.
  646. */
  647. int ast_sip_session_create_invite(struct ast_sip_session *session, pjsip_tx_data **tdata);
  648. /*!
  649. * \brief Send a SIP request and get called back when a response is received
  650. *
  651. * This will send the request out exactly the same as ast_sip_send_request() does.
  652. * The difference is that when a response arrives, the specified callback will be
  653. * called into
  654. *
  655. * \param session The session on which to send the request
  656. * \param tdata The request to send
  657. * \param on_response Callback to be called when a response is received
  658. */
  659. void ast_sip_session_send_request_with_cb(struct ast_sip_session *session, pjsip_tx_data *tdata,
  660. ast_sip_session_response_cb on_response);
  661. /*!
  662. * \brief Retrieves a session from a dialog
  663. *
  664. * \param dlg The dialog to retrieve the session from
  665. *
  666. * \retval non-NULL if session exists
  667. * \retval NULL if no session
  668. *
  669. * \note The reference count of the session is increased when returned
  670. *
  671. * \note This function *must* be called with the dialog locked
  672. */
  673. struct ast_sip_session *ast_sip_dialog_get_session(pjsip_dialog *dlg);
  674. /*!
  675. * \brief Resumes processing of a deferred incoming re-invite
  676. *
  677. * \param session The session which has a pending incoming re-invite
  678. *
  679. * \note When resuming a re-invite it is given to the pjsip stack as if it
  680. * had just been received from a transport, this means that the deferral
  681. * callback will be called again.
  682. */
  683. void ast_sip_session_resume_reinvite(struct ast_sip_session *session);
  684. /*! \brief Determines whether the res_pjsip_session module is loaded */
  685. #define CHECK_PJSIP_SESSION_MODULE_LOADED() \
  686. do { \
  687. CHECK_PJSIP_MODULE_LOADED(); \
  688. if (!ast_module_check("res_pjsip_session.so")) { \
  689. return AST_MODULE_LOAD_DECLINE; \
  690. } \
  691. } while(0)
  692. #endif /* _RES_PJSIP_SESSION_H */