sip_util.h 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888
  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 __PJSIP_SIP_MISC_H__
  20. #define __PJSIP_SIP_MISC_H__
  21. #include <pjsip/sip_msg.h>
  22. #include <pjsip/sip_transport.h>
  23. #include <pjsip/sip_resolve.h>
  24. PJ_BEGIN_DECL
  25. /**
  26. * @defgroup PJSIP_ENDPT_TARGET_URI Target URI Management
  27. * @ingroup PJSIP_CORE_CORE
  28. * @brief Management of target URI's in case of redirection
  29. * @{
  30. * This module provides utility functions to manage target set for UAC.
  31. * The target set is provided as pjsip_target_set structure. Initially,
  32. * the target set for UAC contains only one target, that is the target of
  33. * the initial request. When 3xx/redirection class response is received,
  34. * the UAC can use the functionality of this module to add the URI's listed
  35. * in the Contact header(s) in the response to the target set, and retry
  36. * sending the request to the next destination/target. The UAC may retry
  37. * this sequentially until one of the target answers with succesful/2xx
  38. * response, or one target returns global error/6xx response, or all targets
  39. * are exhausted.
  40. *
  41. * This module is currently used by the \ref PJSIP_INV.
  42. */
  43. /**
  44. * This structure describes a target, which can be chained together to form
  45. * a target set. Each target contains an URI, priority (as q-value), and
  46. * the last status code and reason phrase received from the target, if the
  47. * target has been contacted. If the target has not been contacted, the
  48. * status code field will be zero.
  49. */
  50. typedef struct pjsip_target
  51. {
  52. PJ_DECL_LIST_MEMBER(struct pjsip_target);/**< Standard list element */
  53. pjsip_uri *uri; /**< The target URI */
  54. int q1000; /**< q-value multiplied by 1000 */
  55. pjsip_status_code code; /**< Last status code received */
  56. pj_str_t reason; /**< Last reason phrase received */
  57. } pjsip_target;
  58. /**
  59. * This describes a target set. A target set contains a linked-list of
  60. * pjsip_target.
  61. */
  62. typedef struct pjsip_target_set
  63. {
  64. pjsip_target head; /**< Target linked-list head */
  65. pjsip_target *current; /**< Current target. */
  66. } pjsip_target_set;
  67. /**
  68. * These enumerations specify the action to be performed to a redirect
  69. * response.
  70. */
  71. typedef enum pjsip_redirect_op
  72. {
  73. /**
  74. * Reject the redirection to the current target. The UAC will
  75. * select the next target from the target set if exists.
  76. */
  77. PJSIP_REDIRECT_REJECT,
  78. /**
  79. * Accept the redirection to the current target. The INVITE request
  80. * will be resent to the current target.
  81. */
  82. PJSIP_REDIRECT_ACCEPT,
  83. /**
  84. * Accept the redirection to the current target and replace the To
  85. * header in the INVITE request with the current target. The INVITE
  86. * request will be resent to the current target.
  87. */
  88. PJSIP_REDIRECT_ACCEPT_REPLACE,
  89. /**
  90. * Defer the redirection decision, for example to request permission
  91. * from the end user.
  92. */
  93. PJSIP_REDIRECT_PENDING,
  94. /**
  95. * Stop the whole redirection process altogether. This will cause
  96. * the invite session to be disconnected.
  97. */
  98. PJSIP_REDIRECT_STOP
  99. } pjsip_redirect_op;
  100. /**
  101. * Initialize target set. This will empty the list of targets in the
  102. * target set.
  103. *
  104. * @param tset The target set.
  105. */
  106. PJ_INLINE(void) pjsip_target_set_init(pjsip_target_set *tset)
  107. {
  108. pj_list_init(&tset->head);
  109. tset->current = NULL;
  110. }
  111. /**
  112. * Add an URI to the target set, if the URI is not already in the target set.
  113. * The URI comparison rule of pjsip_uri_cmp() will be used to determine the
  114. * equality of this URI compared to existing URI's in the target set. The
  115. * URI will be cloned using the specified memory pool before it is added to
  116. * the list.
  117. *
  118. * The first URI added to the target set will also be made current target
  119. * by this function.
  120. *
  121. * @param tset The target set.
  122. * @param pool The memory pool to be used to duplicate the URI.
  123. * @param uri The URI to be checked and added.
  124. * @param q1000 The q-value multiplied by 1000.
  125. *
  126. * @return PJ_SUCCESS if the URI was added to the target set,
  127. * or PJ_EEXISTS if the URI already exists in the target
  128. * set, or other error codes.
  129. */
  130. PJ_DECL(pj_status_t) pjsip_target_set_add_uri(pjsip_target_set *tset,
  131. pj_pool_t *pool,
  132. const pjsip_uri *uri,
  133. int q1000);
  134. /**
  135. * Extract URI's in the Contact headers of the specified (response) message
  136. * and add them to the target set. This function will also check if the
  137. * URI's already exist in the target set before adding them to the list.
  138. *
  139. * @param tset The target set.
  140. * @param pool The memory pool to be used to duplicate the URI's.
  141. * @param msg SIP message from which the Contact headers will be
  142. * scanned and the URI's to be extracted, checked, and
  143. * added to the target set.
  144. *
  145. * @return PJ_SUCCESS if at least one URI was added to the
  146. * target set, or PJ_EEXISTS if all URI's in the message
  147. * already exists in the target set or if the message
  148. * doesn't contain usable Contact headers, or other error
  149. * codes.
  150. */
  151. PJ_DECL(pj_status_t) pjsip_target_set_add_from_msg(pjsip_target_set *tset,
  152. pj_pool_t *pool,
  153. const pjsip_msg *msg);
  154. /**
  155. * Get the next target to be retried. This function will scan the target set
  156. * for target which hasn't been tried, and return one target with the
  157. * highest q-value, if such target exists. This function will return NULL
  158. * if there is one target with 2xx or 6xx code or if all targets have been
  159. * tried.
  160. *
  161. * @param tset The target set.
  162. *
  163. * @return The next target to be tried, or NULL if all targets have
  164. * been tried or at least one target returns 2xx or 6xx
  165. * response.
  166. */
  167. PJ_DECL(pjsip_target*)
  168. pjsip_target_set_get_next(const pjsip_target_set *tset);
  169. /**
  170. * Set the specified target as the current target in the target set. The
  171. * current target may be used by application to keep track on which target
  172. * is currently being operated on.
  173. *
  174. * @param tset The target set.
  175. * @param target The target to be set as current target.
  176. *
  177. * @return PJ_SUCCESS or the appropriate error code.
  178. */
  179. PJ_DECL(pj_status_t) pjsip_target_set_set_current(pjsip_target_set *tset,
  180. pjsip_target *target);
  181. /**
  182. * Set the status code and reason phrase of the specified target.
  183. *
  184. * @param target The target.
  185. * @param pool The memory pool to be used to duplicate the reason phrase.
  186. * @param status_code The SIP status code to be set to the target.
  187. * @param reason The reason phrase to be set to the target.
  188. *
  189. * @return PJ_SUCCESS on successful operation or the appropriate
  190. * error code.
  191. */
  192. PJ_DECL(pj_status_t) pjsip_target_assign_status(pjsip_target *target,
  193. pj_pool_t *pool,
  194. int status_code,
  195. const pj_str_t *reason);
  196. /**
  197. * @}
  198. */
  199. /**
  200. * @defgroup PJSIP_ENDPT_STATELESS Message Creation and Stateless Operations
  201. * @ingroup PJSIP_CORE_CORE
  202. * @brief Utilities to create various messages and base function to send messages.
  203. * @{
  204. */
  205. /**
  206. * Create an independent request message. This can be used to build any
  207. * request outside a dialog, such as OPTIONS, MESSAGE, etc. To create a request
  208. * inside a dialog, application should use #pjsip_dlg_create_request.
  209. *
  210. * This function adds the following headers in the request:
  211. * - From, To, Call-ID, and CSeq,
  212. * - Contact header, if contact is specified.
  213. * - A blank Via header.
  214. * - Additional request headers (such as Max-Forwards) which are copied
  215. * from endpoint configuration.
  216. *
  217. * In addition, the function adds a unique tag in the From header.
  218. *
  219. * Once a transmit data is created, the reference counter is initialized to 1.
  220. *
  221. * @param endpt Endpoint instance.
  222. * @param method SIP Method.
  223. * @param target Target URI.
  224. * @param from URL to put in From header.
  225. * @param to URL to put in To header.
  226. * @param contact Contact to be put as Contact header value, hence
  227. * the format must follow RFC 3261 Section 20.10:
  228. * When the header field value contains a display
  229. * name, the URI including all URI parameters is
  230. * enclosed in "<" and ">". If no "<" and ">" are
  231. * present, all parameters after the URI are header
  232. * parameters, not URI parameters. The display name
  233. * can be tokens, or a quoted string, if a larger
  234. * character set is desired.
  235. * @param call_id Optional Call-ID (put NULL to generate unique Call-ID).
  236. * @param cseq Optional CSeq (put -1 to generate random CSeq).
  237. * @param text Optional text body (put NULL to omit body).
  238. * @param p_tdata Pointer to receive the transmit data.
  239. *
  240. * @return PJ_SUCCESS, or the appropriate error code.
  241. */
  242. PJ_DECL(pj_status_t) pjsip_endpt_create_request( pjsip_endpoint *endpt,
  243. const pjsip_method *method,
  244. const pj_str_t *target,
  245. const pj_str_t *from,
  246. const pj_str_t *to,
  247. const pj_str_t *contact,
  248. const pj_str_t *call_id,
  249. int cseq,
  250. const pj_str_t *text,
  251. pjsip_tx_data **p_tdata);
  252. /**
  253. * Create an independent request message from the specified headers. This
  254. * function will clone the headers and put them in the request.
  255. *
  256. * This function adds the following headers in the request:
  257. * - From, To, Call-ID, and CSeq,
  258. * - Contact header, if contact is specified.
  259. * - A blank Via header.
  260. * - Additional request headers (such as Max-Forwards) which are copied
  261. * from endpoint configuration.
  262. *
  263. * In addition, the function adds a unique tag in the From header.
  264. *
  265. * Once a transmit data is created, the reference counter is initialized to 1.
  266. *
  267. * @param endpt Endpoint instance.
  268. * @param method SIP Method.
  269. * @param target Target URI.
  270. * @param from From header.
  271. * @param to To header.
  272. * @param contact Contact header.
  273. * @param call_id Optional Call-ID (put NULL to generate unique Call-ID).
  274. * @param cseq Optional CSeq (put -1 to generate random CSeq).
  275. * @param text Optional text body (put NULL to omit body).
  276. * @param p_tdata Pointer to receive the transmit data.
  277. *
  278. * @return PJ_SUCCESS, or the appropriate error code.
  279. */
  280. PJ_DECL(pj_status_t)
  281. pjsip_endpt_create_request_from_hdr( pjsip_endpoint *endpt,
  282. const pjsip_method *method,
  283. const pjsip_uri *target,
  284. const pjsip_from_hdr *from,
  285. const pjsip_to_hdr *to,
  286. const pjsip_contact_hdr *contact,
  287. const pjsip_cid_hdr *call_id,
  288. int cseq,
  289. const pj_str_t *text,
  290. pjsip_tx_data **p_tdata);
  291. /**
  292. * Construct a minimal response message for the received request. This function
  293. * will construct all the Via, Record-Route, Call-ID, From, To, CSeq, and
  294. * Call-ID headers from the request.
  295. *
  296. * Once a transmit data is created, the reference counter is initialized to 1.
  297. *
  298. * @param endpt The endpoint.
  299. * @param rdata The request receive data.
  300. * @param st_code Status code to be put in the response.
  301. * @param st_text Optional status text, or NULL to get the default text.
  302. * @param p_tdata Pointer to receive the transmit data.
  303. *
  304. * @return PJ_SUCCESS, or the appropriate error code.
  305. */
  306. PJ_DECL(pj_status_t) pjsip_endpt_create_response( pjsip_endpoint *endpt,
  307. const pjsip_rx_data *rdata,
  308. int st_code,
  309. const pj_str_t *st_text,
  310. pjsip_tx_data **p_tdata);
  311. /**
  312. * Construct a full ACK request for the received non-2xx final response.
  313. * This utility function is normally called by the transaction to construct
  314. * an ACK request to 3xx-6xx final response.
  315. * The generation of ACK message for 2xx final response is different than
  316. * this one.
  317. *
  318. * Once a transmit data is created, the reference counter is initialized to 1.
  319. *
  320. * @param endpt The endpoint.
  321. * @param tdata This contains the original INVITE request
  322. * @param rdata The final response.
  323. * @param ack The ACK request created.
  324. *
  325. * @return PJ_SUCCESS, or the appropriate error code.
  326. */
  327. PJ_DECL(pj_status_t) pjsip_endpt_create_ack( pjsip_endpoint *endpt,
  328. const pjsip_tx_data *tdata,
  329. const pjsip_rx_data *rdata,
  330. pjsip_tx_data **ack);
  331. /**
  332. * Construct CANCEL request for the previously sent request.
  333. *
  334. * Once a transmit data is created, the reference counter is initialized to 1.
  335. *
  336. * @param endpt The endpoint.
  337. * @param tdata The transmit buffer for the request being cancelled.
  338. * @param p_tdata Pointer to receive the transmit data.
  339. *
  340. * @return PJ_SUCCESS, or the appropriate error code.
  341. */
  342. PJ_DECL(pj_status_t) pjsip_endpt_create_cancel( pjsip_endpoint *endpt,
  343. const pjsip_tx_data *tdata,
  344. pjsip_tx_data **p_tdata);
  345. /**
  346. * Get destination address and port and transport type information for the
  347. * specified URI.
  348. *
  349. * @param target_uri The destination URI.
  350. * @param request_uri Optional request URI to be considered. May be NULL.
  351. * @param pool Pool to allocate memory from.
  352. * @param dest_info To be filled with destination info.
  353. *
  354. * @return PJ_SUCCESS or the appropriate error code.
  355. */
  356. PJ_DECL(pj_status_t) pjsip_get_dest_info(const pjsip_uri *target_uri,
  357. const pjsip_uri *request_uri,
  358. pj_pool_t *pool,
  359. pjsip_host_info *dest_info);
  360. /**
  361. * Find which destination to be used to send the request message, based
  362. * on the request URI and Route headers in the message. The procedure
  363. * used here follows the guidelines on sending the request in RFC 3261
  364. * chapter 8.1.2.
  365. *
  366. * Note there was a change in the behavior of this function since version
  367. * 0.5.10.2. Previously this function may modify the request when strict
  368. * route is present (to update request URI and route-set). This is no
  369. * longer the case now, and this process is done in separate function
  370. * (see #pjsip_process_route_set()).
  371. *
  372. * @param tdata The transmit data containing the request message.
  373. * @param dest_info On return, it contains information about destination
  374. * host to contact, along with the preferable transport
  375. * type, if any. Caller will then normally proceed with
  376. * resolving this host with server resolution procedure
  377. * described in RFC 3263.
  378. *
  379. * @return PJ_SUCCESS, or the appropriate error code.
  380. *
  381. * @see pjsip_process_route_set
  382. */
  383. PJ_DECL(pj_status_t) pjsip_get_request_dest(const pjsip_tx_data *tdata,
  384. pjsip_host_info *dest_info );
  385. /**
  386. * Process route-set found in the request and calculate destination to be
  387. * used to send the request message, based on the request URI and Route
  388. * headers in the message. The procedure used here follows the guidelines
  389. * on sending the request in RFC 3261 chapter 8.1.2.
  390. *
  391. * This function may modify the message (request line and Route headers),
  392. * if the Route information specifies strict routing and the request
  393. * URI in the message is different than the calculated target URI. In that
  394. * case, the target URI will be put as the request URI of the request and
  395. * current request URI will be put as the last entry of the Route headers.
  396. *
  397. * @param tdata The transmit data containing the request message.
  398. * @param dest_info On return, it contains information about destination
  399. * host to contact, along with the preferable transport
  400. * type, if any. Caller will then normally proceed with
  401. * resolving this host with server resolution procedure
  402. * described in RFC 3263.
  403. *
  404. * @return PJ_SUCCESS, or the appropriate error code.
  405. *
  406. * @see pjsip_get_request_addr
  407. */
  408. PJ_DECL(pj_status_t) pjsip_process_route_set(pjsip_tx_data *tdata,
  409. pjsip_host_info *dest_info );
  410. /**
  411. * Swap the request URI and strict route back to the original position
  412. * before #pjsip_process_route_set() function is called. If no strict
  413. * route URI was found by #pjsip_process_route_set(), this function will
  414. * do nothing.
  415. *
  416. * This function should only used internally by PJSIP client authentication
  417. * module.
  418. *
  419. * @param tdata Transmit data containing request message.
  420. */
  421. PJ_DECL(void) pjsip_restore_strict_route_set(pjsip_tx_data *tdata);
  422. /**
  423. * This structure holds the state of outgoing stateless request.
  424. */
  425. typedef struct pjsip_send_state
  426. {
  427. /** Application token, which was specified when the function
  428. * #pjsip_endpt_send_request_stateless() is called.
  429. */
  430. void *token;
  431. /** Endpoint instance.
  432. */
  433. pjsip_endpoint *endpt;
  434. /** Transmit data buffer being sent.
  435. */
  436. pjsip_tx_data *tdata;
  437. /** Current transport being used.
  438. */
  439. pjsip_transport *cur_transport;
  440. /** The application callback which was specified when the function
  441. * #pjsip_endpt_send_request_stateless() was called.
  442. */
  443. void (*app_cb)(struct pjsip_send_state*,
  444. pj_ssize_t sent,
  445. pj_bool_t *cont);
  446. } pjsip_send_state;
  447. /**
  448. * Declaration for callback function to be specified in
  449. * #pjsip_endpt_send_request_stateless(), #pjsip_endpt_send_response(), or
  450. * #pjsip_endpt_send_response2().
  451. *
  452. * @param st Structure to keep transmission state.
  453. * @param sent Number of bytes sent.
  454. * @param cont When current transmission fails, specify whether
  455. * the function should fallback to next destination.
  456. */
  457. typedef void (*pjsip_send_callback)(pjsip_send_state *st, pj_ssize_t sent,
  458. pj_bool_t *cont);
  459. /**
  460. * Send outgoing request statelessly The function will take care of which
  461. * destination and transport to use based on the information in the message,
  462. * taking care of URI in the request line and Route header.
  463. *
  464. * This function is different than #pjsip_transport_send() in that this
  465. * function adds/modify the Via header as necessary.
  466. *
  467. * @param endpt The endpoint instance.
  468. * @param tdata The transmit data to be sent.
  469. * @param token Arbitrary token to be given back on the callback.
  470. * @param cb Optional callback to notify transmission status (also
  471. * gives chance for application to discontinue retrying
  472. * sending to alternate address).
  473. *
  474. * @return PJ_SUCCESS, or the appropriate error code.
  475. */
  476. PJ_DECL(pj_status_t)
  477. pjsip_endpt_send_request_stateless( pjsip_endpoint *endpt,
  478. pjsip_tx_data *tdata,
  479. void *token,
  480. pjsip_send_callback cb);
  481. /**
  482. * This is a low-level function to send raw data to a destination.
  483. *
  484. * See also #pjsip_endpt_send_raw_to_uri().
  485. *
  486. * @param endpt The SIP endpoint instance.
  487. * @param tp_type Transport type.
  488. * @param sel Optional pointer to transport selector instance if
  489. * application wants to use a specific transport instance
  490. * rather then letting transport manager finds the suitable
  491. * transport..
  492. * @param raw_data The data to be sent.
  493. * @param data_len The length of the data.
  494. * @param addr Destination address.
  495. * @param addr_len Length of destination address.
  496. * @param token Arbitrary token to be returned back to callback.
  497. * @param cb Optional callback to be called to notify caller about
  498. * the completion status of the pending send operation.
  499. *
  500. * @return If the message has been sent successfully, this function
  501. * will return PJ_SUCCESS and the callback will not be
  502. * called. If message cannot be sent immediately, this
  503. * function will return PJ_EPENDING, and application will
  504. * be notified later about the completion via the callback.
  505. * Any statuses other than PJ_SUCCESS or PJ_EPENDING
  506. * indicates immediate failure, and in this case the
  507. * callback will not be called.
  508. */
  509. PJ_DECL(pj_status_t) pjsip_endpt_send_raw(pjsip_endpoint *endpt,
  510. pjsip_transport_type_e tp_type,
  511. const pjsip_tpselector *sel,
  512. const void *raw_data,
  513. pj_size_t data_len,
  514. const pj_sockaddr_t *addr,
  515. int addr_len,
  516. void *token,
  517. pjsip_tp_send_callback cb);
  518. /**
  519. * Send raw data to the specified destination URI. The actual destination
  520. * address will be calculated from the URI, using normal SIP URI to host
  521. * resolution.
  522. *
  523. * See also #pjsip_endpt_send_raw().
  524. *
  525. * @param endpt The SIP endpoint instance.
  526. * @param dst_uri Destination address URI.
  527. * @param sel Optional pointer to transport selector instance if
  528. * application wants to use a specific transport instance
  529. * rather then letting transport manager finds the suitable
  530. * transport..
  531. * @param raw_data The data to be sent.
  532. * @param data_len The length of the data.
  533. * @param token Arbitrary token to be returned back to callback.
  534. * @param cb Optional callback to be called to notify caller about
  535. * the completion status of the pending send operation.
  536. *
  537. * @return If the message has been sent successfully, this function
  538. * will return PJ_SUCCESS and the callback will not be
  539. * called. If message cannot be sent immediately, this
  540. * function will return PJ_EPENDING, and application will
  541. * be notified later about the completion via the callback.
  542. * Any statuses other than PJ_SUCCESS or PJ_EPENDING
  543. * indicates immediate failure, and in this case the
  544. * callback will not be called.
  545. */
  546. PJ_DECL(pj_status_t) pjsip_endpt_send_raw_to_uri(pjsip_endpoint *endpt,
  547. const pj_str_t *dst_uri,
  548. const pjsip_tpselector *sel,
  549. const void *raw_data,
  550. pj_size_t data_len,
  551. void *token,
  552. pjsip_tp_send_callback cb);
  553. /**
  554. * This structure describes destination information to send response.
  555. * It is initialized by calling #pjsip_get_response_addr().
  556. *
  557. * If the response message should be sent using transport from which
  558. * the request was received, then transport, addr, and addr_len fields
  559. * are initialized.
  560. *
  561. * The dst_host field is also initialized. It should be used when server
  562. * fails to send the response using the transport from which the request
  563. * was received, or when the transport is NULL, which means server
  564. * must send the response to this address (this situation occurs when
  565. * maddr parameter is set, or when rport param is not set in the request).
  566. */
  567. typedef struct pjsip_response_addr
  568. {
  569. pjsip_transport *transport; /**< Immediate transport to be used. */
  570. pj_sockaddr addr; /**< Immediate address to send to. */
  571. int addr_len; /**< Address length. */
  572. pjsip_host_info dst_host; /**< Destination host to contact. */
  573. } pjsip_response_addr;
  574. /**
  575. * Determine which address (and transport) to use to send response message
  576. * based on the received request. This function follows the specification
  577. * in section 18.2.2 of RFC 3261 and RFC 3581 for calculating the destination
  578. * address and transport.
  579. *
  580. * The information about destination to send the response will be returned
  581. * in res_addr argument. Please see #pjsip_response_addr for more info.
  582. *
  583. * @param pool The pool.
  584. * @param rdata The incoming request received by the server.
  585. * @param res_addr On return, it will be initialized with information about
  586. * destination address and transport to send the response.
  587. *
  588. * @return zero (PJ_OK) if successfull.
  589. */
  590. PJ_DECL(pj_status_t) pjsip_get_response_addr(pj_pool_t *pool,
  591. pjsip_rx_data *rdata,
  592. pjsip_response_addr *res_addr);
  593. /**
  594. * Send response in tdata statelessly. The function will take care of which
  595. * response destination and transport to use based on the information in the
  596. * Via header (such as the presence of rport, symmetric transport, etc.).
  597. *
  598. * This function will create a new ephemeral transport if no existing
  599. * transports can be used to send the message to the destination. The ephemeral
  600. * transport will be destroyed after some period if it is not used to send any
  601. * more messages.
  602. *
  603. * The behavior of this function complies with section 18.2.2 of RFC 3261
  604. * and RFC 3581.
  605. *
  606. * @param endpt The endpoint instance.
  607. * @param res_addr The information about the address and transport to send
  608. * the response to. Application can get this information
  609. * by calling #pjsip_get_response_addr().
  610. * @param tdata The response message to be sent.
  611. * @param token Token to be passed back when the callback is called.
  612. * @param cb Optional callback to notify the transmission status
  613. * to application, and to inform whether next address or
  614. * transport will be tried.
  615. *
  616. * @return PJ_SUCCESS if response has been successfully created and
  617. * sent to transport layer, or a non-zero error code.
  618. * However, even when it returns PJ_SUCCESS, there is no
  619. * guarantee that the response has been successfully sent.
  620. */
  621. PJ_DECL(pj_status_t) pjsip_endpt_send_response( pjsip_endpoint *endpt,
  622. pjsip_response_addr *res_addr,
  623. pjsip_tx_data *tdata,
  624. void *token,
  625. pjsip_send_callback cb);
  626. /**
  627. * This is a convenient function which wraps #pjsip_get_response_addr() and
  628. * #pjsip_endpt_send_response() in a single function.
  629. *
  630. * @param endpt The endpoint instance.
  631. * @param rdata The original request to be responded.
  632. * @param tdata The response message to be sent.
  633. * @param token Token to be passed back when the callback is called.
  634. * @param cb Optional callback to notify the transmission status
  635. * to application, and to inform whether next address or
  636. * transport will be tried.
  637. *
  638. * @return PJ_SUCCESS if response has been successfully created and
  639. * sent to transport layer, or a non-zero error code.
  640. * However, even when it returns PJ_SUCCESS, there is no
  641. * guarantee that the response has been successfully sent.
  642. */
  643. PJ_DECL(pj_status_t) pjsip_endpt_send_response2(pjsip_endpoint *endpt,
  644. pjsip_rx_data *rdata,
  645. pjsip_tx_data *tdata,
  646. void *token,
  647. pjsip_send_callback cb);
  648. /**
  649. * This composite function sends response message statelessly to an incoming
  650. * request message. Internally it calls #pjsip_endpt_create_response() and
  651. * #pjsip_endpt_send_response().
  652. *
  653. * @param endpt The endpoint instance.
  654. * @param rdata The incoming request message.
  655. * @param st_code Status code of the response.
  656. * @param st_text Optional status text of the response.
  657. * @param hdr_list Optional header list to be added to the response.
  658. * @param body Optional message body to be added to the response.
  659. *
  660. * @return PJ_SUCCESS if response message has successfully been
  661. * sent.
  662. */
  663. PJ_DECL(pj_status_t) pjsip_endpt_respond_stateless(pjsip_endpoint *endpt,
  664. pjsip_rx_data *rdata,
  665. int st_code,
  666. const pj_str_t *st_text,
  667. const pjsip_hdr *hdr_list,
  668. const pjsip_msg_body *body);
  669. /**
  670. * @}
  671. */
  672. /**
  673. * @defgroup PJSIP_TRANSACT_UTIL Stateful Operations
  674. * @ingroup PJSIP_TRANSACT
  675. * @brief Utility function to send requests/responses statefully.
  676. * @{
  677. */
  678. /**
  679. * This composite function creates and sends response statefully for the
  680. * incoming request.
  681. *
  682. * @param endpt The endpoint instance.
  683. * @param tsx_user The module to be registered as transaction user.
  684. * @param rdata The incoming request message.
  685. * @param st_code Status code of the response.
  686. * @param st_text Optional status text of the response.
  687. * @param hdr_list Optional header list to be added to the response.
  688. * @param body Optional message body to be added to the response.
  689. * @param p_tsx Optional pointer to receive the transaction which was
  690. * created to send the response.
  691. *
  692. * @return PJ_SUCCESS if response message has successfully been
  693. * created.
  694. */
  695. PJ_DECL(pj_status_t) pjsip_endpt_respond( pjsip_endpoint *endpt,
  696. pjsip_module *tsx_user,
  697. pjsip_rx_data *rdata,
  698. int st_code,
  699. const pj_str_t *st_text,
  700. const pjsip_hdr *hdr_list,
  701. const pjsip_msg_body *body,
  702. pjsip_transaction **p_tsx );
  703. /**
  704. * Type of callback to be specified in #pjsip_endpt_send_request().
  705. *
  706. * @param token The token that was given in #pjsip_endpt_send_request()
  707. * @param e Completion event.
  708. */
  709. typedef void (*pjsip_endpt_send_callback)(void *token, pjsip_event *e);
  710. /**
  711. * Send outgoing request and initiate UAC transaction for the request.
  712. * This is an auxiliary function to be used by application to send arbitrary
  713. * requests outside a dialog. To send a request within a dialog, application
  714. * should use #pjsip_dlg_send_request instead.
  715. *
  716. * @param endpt The endpoint instance.
  717. * @param tdata The transmit data to be sent.
  718. * @param timeout Optional timeout for final response to be received, or -1
  719. * if the transaction should not have a timeout restriction.
  720. * The value is in miliseconds. Note that this is not
  721. * implemented yet, so application needs to use its own timer
  722. * to handle timeout.
  723. * @param token Optional token to be associated with the transaction, and
  724. * to be passed to the callback.
  725. * @param cb Optional callback to be called when the transaction has
  726. * received a final response. The callback will be called with
  727. * the previously registered token and the event that triggers
  728. * the completion of the transaction.
  729. *
  730. * @return PJ_SUCCESS, or the appropriate error code.
  731. */
  732. PJ_DECL(pj_status_t) pjsip_endpt_send_request( pjsip_endpoint *endpt,
  733. pjsip_tx_data *tdata,
  734. pj_int32_t timeout,
  735. void *token,
  736. pjsip_endpt_send_callback cb);
  737. /**
  738. * @}
  739. */
  740. /**
  741. * @defgroup PJSIP_PROXY_CORE Core Proxy Layer
  742. * @brief Core proxy operations
  743. * @{
  744. */
  745. /**
  746. * Create new request message to be forwarded upstream to new destination URI
  747. * in uri. The new request is a full/deep clone of the request received in
  748. * rdata, unless if other copy mechanism is specified in the options.
  749. * The branch parameter, if not NULL, will be used as the branch-param in
  750. * the Via header. If it is NULL, then a unique branch parameter will be used.
  751. *
  752. * Note: this function DOES NOT perform Route information preprocessing as
  753. * described in RFC 3261 Section 16.4. Application must take care of
  754. * removing/updating the Route headers according of the rules as
  755. * described in that section.
  756. *
  757. * @param endpt The endpoint instance.
  758. * @param rdata The incoming request message.
  759. * @param uri The URI where the request will be forwarded to.
  760. * @param branch Optional branch parameter. Application may specify its
  761. * own branch, for example if it wishes to perform loop
  762. * detection. If the branch parameter is not specified,
  763. * this function will generate its own by calling
  764. * #pjsip_calculate_branch_id() function.
  765. * @param options Optional option flags when duplicating the message.
  766. * @param tdata The result.
  767. *
  768. * @return PJ_SUCCESS on success.
  769. */
  770. PJ_DECL(pj_status_t) pjsip_endpt_create_request_fwd(pjsip_endpoint *endpt,
  771. pjsip_rx_data *rdata,
  772. const pjsip_uri *uri,
  773. const pj_str_t *branch,
  774. unsigned options,
  775. pjsip_tx_data **tdata);
  776. /**
  777. * Create new response message to be forwarded downstream by the proxy from
  778. * the response message found in rdata. Note that this function practically
  779. * will clone the response as is, i.e. without checking the validity of the
  780. * response or removing top most Via header. This function will perform
  781. * full/deep clone of the response, unless other copy mechanism is used in
  782. * the options.
  783. *
  784. * @param endpt The endpoint instance.
  785. * @param rdata The incoming response message.
  786. * @param options Optional option flags when duplicate the message.
  787. * @param tdata The result
  788. *
  789. * @return PJ_SUCCESS on success.
  790. */
  791. PJ_DECL(pj_status_t) pjsip_endpt_create_response_fwd( pjsip_endpoint *endpt,
  792. pjsip_rx_data *rdata,
  793. unsigned options,
  794. pjsip_tx_data **tdata);
  795. /**
  796. * Create a globally unique branch parameter based on the information in
  797. * the incoming request message, for the purpose of creating a new request
  798. * for forwarding. This is the default implementation used by
  799. * #pjsip_endpt_create_request_fwd() function if the branch parameter is
  800. * not specified.
  801. *
  802. * The default implementation here will just create an MD5 hash of the
  803. * top-most Via.
  804. *
  805. * Note that the returned string was allocated from rdata's pool.
  806. *
  807. * @param rdata The incoming request message.
  808. *
  809. * @return Unique branch-ID string.
  810. */
  811. PJ_DECL(pj_str_t) pjsip_calculate_branch_id( pjsip_rx_data *rdata );
  812. /**
  813. * @}
  814. */
  815. PJ_END_DECL
  816. #endif /* __PJSIP_SIP_MISC_H__ */