sip_regc.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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_REG_H__
  20. #define __PJSIP_SIP_REG_H__
  21. /**
  22. * @file sip_regc.h
  23. * @brief SIP Registration Client
  24. */
  25. #include <pjsip/sip_types.h>
  26. #include <pjsip/sip_auth.h>
  27. #include <pjsip/sip_transport.h>
  28. /**
  29. * @defgroup PJSUA_REGC Client Registration
  30. * @ingroup PJSIP_HIGH_UA
  31. * @brief High Layer API for performing client registration.
  32. * @{
  33. *
  34. * This provides API for performing client registration. Application must
  35. * link with <b>pjsip-ua</b> static library to use this API.
  36. */
  37. PJ_BEGIN_DECL
  38. /** Typedef for client registration data. */
  39. typedef struct pjsip_regc pjsip_regc;
  40. /** Maximum contacts in registration. */
  41. #define PJSIP_REGC_MAX_CONTACT 10
  42. /** Expiration not specified. */
  43. #define PJSIP_REGC_EXPIRATION_NOT_SPECIFIED PJSIP_EXPIRES_NOT_SPECIFIED
  44. /** Buffer to hold all contacts. */
  45. #define PJSIP_REGC_CONTACT_BUF_SIZE 512
  46. /** Structure to hold parameters when calling application's callback.
  47. * The application's callback is called when the client registration process
  48. * has finished.
  49. */
  50. struct pjsip_regc_cbparam
  51. {
  52. pjsip_regc *regc; /**< Client registration structure. */
  53. void *token; /**< Arbitrary token set by application */
  54. /** Error status. If this value is non-PJ_SUCCESS, some error has occured.
  55. * Note that even when this contains PJ_SUCCESS the registration might
  56. * have failed; in this case the \a code field will contain non
  57. * successful (non-2xx status class) code
  58. */
  59. pj_status_t status;
  60. int code; /**< SIP status code received. */
  61. pj_str_t reason; /**< SIP reason phrase received. */
  62. pjsip_rx_data *rdata; /**< The complete received response. */
  63. unsigned expiration;/**< Next expiration interval,
  64. PJSIP_REGC_EXPIRATION_NOT_SPECIFIED
  65. if not specified. */
  66. int contact_cnt;/**<Number of contacts in response. */
  67. pjsip_contact_hdr *contact[PJSIP_REGC_MAX_CONTACT]; /**< Contacts. */
  68. pj_bool_t is_unreg; /**< Expire header, if any, set to zero?*/
  69. };
  70. /** Type declaration for callback to receive registration result. */
  71. typedef void pjsip_regc_cb(struct pjsip_regc_cbparam *param);
  72. /**
  73. * Structure to hold parameters when calling application's callback
  74. * specified in #pjsip_regc_set_reg_tsx_cb().
  75. * To update contact address, application can set the field contact_cnt
  76. * and contact inside the callback.
  77. */
  78. struct pjsip_regc_tsx_cb_param
  79. {
  80. /** cbparam */
  81. struct pjsip_regc_cbparam cbparam;
  82. /** Number of Contacts */
  83. int contact_cnt;
  84. /** Array of Contacts */
  85. pj_str_t contact[PJSIP_REGC_MAX_CONTACT];
  86. };
  87. /** Type declaration for callback set in #pjsip_regc_set_reg_tsx_cb(). */
  88. typedef void pjsip_regc_tsx_cb(struct pjsip_regc_tsx_cb_param *param);
  89. /**
  90. * Client registration information.
  91. */
  92. struct pjsip_regc_info
  93. {
  94. pj_str_t server_uri; /**< Server URI, */
  95. pj_str_t client_uri; /**< Client URI (From header). */
  96. pj_bool_t is_busy; /**< Have pending transaction? */
  97. pj_bool_t auto_reg; /**< Will register automatically? */
  98. unsigned interval; /**< Registration interval (seconds). */
  99. unsigned next_reg; /**< Time until next registration (seconds). */
  100. pjsip_transport *transport; /**< Last transport used, for informational
  101. purpose only (e.g: comparing pointers),
  102. the transport may be invalid already. */
  103. };
  104. /**
  105. * @see pjsip_regc_info
  106. */
  107. typedef struct pjsip_regc_info pjsip_regc_info;
  108. /**
  109. * Get the module instance for client registration module.
  110. *
  111. * @return client registration module.
  112. */
  113. PJ_DECL(pjsip_module*) pjsip_regc_get_module(void);
  114. /**
  115. * Create client registration structure.
  116. *
  117. * @param endpt Endpoint, used to allocate pool from.
  118. * @param token A data to be associated with the client registration struct.
  119. * @param cb Pointer to callback function to receive registration status.
  120. * @param p_regc Pointer to receive client registration structure.
  121. *
  122. * @return PJ_SUCCESS on success.
  123. */
  124. PJ_DECL(pj_status_t) pjsip_regc_create( pjsip_endpoint *endpt, void *token,
  125. pjsip_regc_cb *cb,
  126. pjsip_regc **p_regc);
  127. /**
  128. * Destroy client registration structure. If a registration transaction is
  129. * in progress, then the structure will be deleted only after a final response
  130. * has been received, and in this case, the callback won't be called.
  131. *
  132. * @param regc The client registration structure.
  133. *
  134. * @return PJ_SUCCESS on success.
  135. */
  136. PJ_DECL(pj_status_t) pjsip_regc_destroy(pjsip_regc *regc);
  137. /**
  138. * Destroy client registration structure. If a registration transaction is
  139. * in progress:
  140. * - if force is PJ_TRUE, then the structure will be deleted only after
  141. * a final response has been received, and in this case, the callback
  142. * won't be called (this behavior is the same as calling
  143. * pjsip_regc_destroye()).
  144. * - if force is PJ_FALSE, the function will return PJ_EBUSY
  145. *
  146. * @param regc The client registration structure.
  147. * @param force Specify if the function must destroy the structure.
  148. *
  149. * @return PJ_SUCCESS on success, or PJ_EBUSY.
  150. */
  151. PJ_DECL(pj_status_t) pjsip_regc_destroy2(pjsip_regc *regc, pj_bool_t force);
  152. /**
  153. * Get registration info.
  154. *
  155. * @param regc The client registration structure.
  156. * @param info Client registration info.
  157. *
  158. * @return PJ_SUCCESS on success.
  159. */
  160. PJ_DECL(pj_status_t) pjsip_regc_get_info( pjsip_regc *regc,
  161. pjsip_regc_info *info );
  162. /**
  163. * Get the memory pool associated with a registration client handle.
  164. *
  165. * @param regc The client registration structure.
  166. * @return pool handle.
  167. */
  168. PJ_DECL(pj_pool_t*) pjsip_regc_get_pool(pjsip_regc *regc);
  169. /**
  170. * Initialize client registration structure with various information needed to
  171. * perform the registration.
  172. *
  173. * @param regc The client registration structure.
  174. * @param srv_url Server URL.
  175. * @param from_url The person performing the registration, must be a SIP URL type.
  176. * @param to_url The address of record for which the registration is targetd, must
  177. * be a SIP/SIPS URL.
  178. * @param ccnt Number of contacts in the array.
  179. * @param contact Array of contacts, each contact item must be formatted
  180. * as described in RFC 3261 Section 20.10:
  181. * When the header field value contains a display
  182. * name, the URI including all URI parameters is
  183. * enclosed in "<" and ">". If no "<" and ">" are
  184. * present, all parameters after the URI are header
  185. * parameters, not URI parameters. The display name
  186. * can be tokens, or a quoted string, if a larger
  187. * character set is desired.
  188. * @param expires Default expiration interval (in seconds) to be applied for
  189. * contact URL that doesn't have expiration settings. If the
  190. * value PJSIP_REGC_EXPIRATION_NOT_SPECIFIED is given, then
  191. * no default expiration will be applied.
  192. * @return zero on success.
  193. */
  194. PJ_DECL(pj_status_t) pjsip_regc_init(pjsip_regc *regc,
  195. const pj_str_t *srv_url,
  196. const pj_str_t *from_url,
  197. const pj_str_t *to_url,
  198. int ccnt,
  199. const pj_str_t contact[],
  200. pj_uint32_t expires);
  201. /**
  202. * Increment busy counter temporarily, to prevent client registration
  203. * structure from being destroyed.
  204. *
  205. * @param regc The client registration structure.
  206. */
  207. PJ_DECL(void) pjsip_regc_add_ref( pjsip_regc *regc );
  208. /**
  209. * Decrement temporary busy counter. After this function
  210. * is called, client registration structure may have been destroyed
  211. * if there's a pending destroy.
  212. *
  213. * @param regc The client registration structure.
  214. *
  215. * @return PJ_SUCCESS on success. PJ_EGONE if the registration
  216. * structure has been destroyed inside the function.
  217. */
  218. PJ_DECL(pj_status_t) pjsip_regc_dec_ref( pjsip_regc *regc );
  219. /**
  220. * Set callback to be called when the registration received a final response.
  221. * This callback is different with the one specified during creation via
  222. * #pjsip_regc_create(). This callback will be called for any final response
  223. * (including 401/407/423) and before any subsequent requests are sent.
  224. * In case of unregistration, this callback will not be called.
  225. *
  226. * @param regc The client registration structure.
  227. * @param tsx_cb Pointer to callback function to receive registration status.
  228. *
  229. * @return PJ_SUCCESS on success.
  230. */
  231. PJ_DECL(pj_status_t) pjsip_regc_set_reg_tsx_cb(pjsip_regc *regc,
  232. pjsip_regc_tsx_cb *tsx_cb);
  233. /**
  234. * Set the "sent-by" field of the Via header for outgoing requests.
  235. *
  236. * @param regc The client registration structure.
  237. * @param via_addr Set via_addr to use for the Via header or NULL to use
  238. * the transport's published name.
  239. * @param via_tp via_addr will only be used if we are using via_tp
  240. * transport.
  241. *
  242. * @return PJ_SUCCESS on success.
  243. */
  244. PJ_DECL(pj_status_t) pjsip_regc_set_via_sent_by(pjsip_regc *regc,
  245. pjsip_host_port *via_addr,
  246. pjsip_transport *via_tp);
  247. /**
  248. * Set the number of seconds to refresh the client registration before
  249. * the registration expires.
  250. *
  251. * @param regc The registration structure.
  252. * @param delay The number of seconds to refresh the client
  253. * registration before the registration expires.
  254. *
  255. * @return PJ_SUCCESS on success.
  256. */
  257. PJ_DECL(pj_status_t)
  258. pjsip_regc_set_delay_before_refresh( pjsip_regc *regc,
  259. pj_uint32_t delay );
  260. /**
  261. * Set authentication credentials to use by this registration.
  262. *
  263. * @param regc The registration structure.
  264. * @param count Number of credentials in the array.
  265. * @param cred Array of credentials.
  266. *
  267. * @return PJ_SUCCESS on success.
  268. */
  269. PJ_DECL(pj_status_t) pjsip_regc_set_credentials( pjsip_regc *regc,
  270. int count,
  271. const pjsip_cred_info cred[] );
  272. /**
  273. * Set authentication preference.
  274. *
  275. * @param regc The registration structure.
  276. * @param pref Authentication preference.
  277. *
  278. * @return PJ_SUCCESS on success.
  279. */
  280. PJ_DECL(pj_status_t) pjsip_regc_set_prefs( pjsip_regc *regc,
  281. const pjsip_auth_clt_pref *pref);
  282. /**
  283. * Set route set to be used for outgoing requests.
  284. *
  285. * @param regc The client registration structure.
  286. * @param route_set List containing Route headers.
  287. *
  288. * @return PJ_SUCCESS on success.
  289. */
  290. PJ_DECL(pj_status_t) pjsip_regc_set_route_set(pjsip_regc *regc,
  291. const pjsip_route_hdr*route_set);
  292. /**
  293. * Lock/bind client registration to a specific transport/listener.
  294. * This is optional, as normally transport will be selected automatically
  295. * based on the destination of requests upon resolver completion.
  296. * When the client registration is explicitly bound to the specific
  297. * transport/listener, all UAC transactions originated by the client
  298. * registration will use the specified transport/listener when sending
  299. * outgoing requests.
  300. *
  301. * Note that this doesn't affect the Contact header set for this client
  302. * registration. Application must manually update the Contact header if
  303. * necessary, to adjust the address according to the transport being
  304. * selected.
  305. *
  306. * @param regc The client registration instance.
  307. * @param sel Transport selector containing the specification of
  308. * transport or listener to be used by this session
  309. * to send requests.
  310. *
  311. * @return PJ_SUCCESS on success, or the appropriate error code.
  312. */
  313. PJ_DECL(pj_status_t) pjsip_regc_set_transport(pjsip_regc *regc,
  314. const pjsip_tpselector *sel);
  315. /**
  316. * Release the reference to current transport being used by the regc, if any.
  317. * The regc keeps the reference to the last transport being used in order
  318. * to prevent it from being destroyed. In some situation however, such as
  319. * when the transport is disconnected, it is necessary to instruct the
  320. * regc to release this reference so that the transport can be destroyed.
  321. * See https://github.com/pjsip/pjproject/issues/1481 for background info.
  322. *
  323. * @param regc The client registration instance.
  324. *
  325. * @return PJ_SUCCESS on success, or the appropriate error code.
  326. */
  327. PJ_DECL(pj_status_t) pjsip_regc_release_transport(pjsip_regc *regc);
  328. /**
  329. * Add headers to be added to outgoing REGISTER requests.
  330. *
  331. * @param regc The client registration structure.
  332. * @param hdr_list List containing SIP headers to be added for all outgoing
  333. * REGISTER requests.
  334. *
  335. * @return PJ_SUCCESS on success.
  336. */
  337. PJ_DECL(pj_status_t) pjsip_regc_add_headers(pjsip_regc *regc,
  338. const pjsip_hdr *hdr_list);
  339. /**
  340. * Create REGISTER request for the specified client registration structure.
  341. *
  342. * After successfull registration, application can inspect the contacts in
  343. * the client registration structure to list what contacts are associaciated
  344. * with the address of record being targeted in the registration.
  345. *
  346. * @param regc The client registration structure.
  347. * @param autoreg If non zero, the library will automatically refresh the
  348. * next registration until application unregister.
  349. * @param p_tdata Pointer to receive the REGISTER request.
  350. *
  351. * @return PJ_SUCCESS on success.
  352. */
  353. PJ_DECL(pj_status_t) pjsip_regc_register(pjsip_regc *regc, pj_bool_t autoreg,
  354. pjsip_tx_data **p_tdata);
  355. /**
  356. * Create REGISTER request to unregister the contacts that were previously
  357. * registered by this client registration.
  358. *
  359. * @param regc The client registration structure.
  360. * @param p_tdata Pointer to receive the REGISTER request.
  361. *
  362. * @return PJ_SUCCESS on success.
  363. */
  364. PJ_DECL(pj_status_t) pjsip_regc_unregister(pjsip_regc *regc,
  365. pjsip_tx_data **p_tdata);
  366. /**
  367. * Create REGISTER request to unregister all contacts from server records.
  368. * Note that this will unregister all registered contact for the AOR
  369. * including contacts registered by other user agents. To only unregister
  370. * contact registered by this client registration instance, use
  371. * #pjsip_regc_unregister() instead.
  372. *
  373. * @param regc The client registration structure.
  374. * @param p_tdata Pointer to receive the REGISTER request.
  375. *
  376. * @return PJ_SUCCESS on success.
  377. */
  378. PJ_DECL(pj_status_t) pjsip_regc_unregister_all(pjsip_regc *regc,
  379. pjsip_tx_data **p_tdata);
  380. /**
  381. * Update Contact details in the client registration structure. For each
  382. * contact, if the contact is not found in existing contact, it will be
  383. * added to the Contact list. If it matches existing contact, nothing
  384. * will be added. This function will also mark existing contacts which
  385. * are not specified in the new contact list as to be removed, by adding
  386. * "expires=0" parameter to these contacts.
  387. *
  388. * Once the contact list has been updated, application must update the
  389. * registration by creating a new REGISTER request and send it to the
  390. * registrar. This request will contain both old and new contacts; the
  391. * old contacts will have it's expires parameter set to zero to instruct
  392. * the registrar to remove the bindings.
  393. *
  394. * @param regc The client registration structure.
  395. * @param ccnt Number of contacts.
  396. * @param contact Array of contacts, each contact item must be formatted
  397. * as described in RFC 3261 Section 20.10:
  398. * When the header field value contains a display
  399. * name, the URI including all URI parameters is
  400. * enclosed in "<" and ">". If no "<" and ">" are
  401. * present, all parameters after the URI are header
  402. * parameters, not URI parameters. The display name
  403. * can be tokens, or a quoted string, if a larger
  404. * character set is desired.
  405. * @return PJ_SUCCESS if sucessfull.
  406. */
  407. PJ_DECL(pj_status_t) pjsip_regc_update_contact( pjsip_regc *regc,
  408. int ccnt,
  409. const pj_str_t contact[] );
  410. /**
  411. * Update the expires value. The next REGISTER request will contain
  412. * new expires value for the registration.
  413. *
  414. * @param regc The client registration structure.
  415. * @param expires The new expires value.
  416. * @return zero on successfull.
  417. */
  418. PJ_DECL(pj_status_t) pjsip_regc_update_expires( pjsip_regc *regc,
  419. pj_uint32_t expires );
  420. /**
  421. * Sends outgoing REGISTER request.
  422. * The process will complete asynchronously, and application
  423. * will be notified via the callback when the process completes.
  424. *
  425. * @param regc The client registration structure.
  426. * @param tdata Transmit data.
  427. *
  428. * @return PJ_SUCCESS on success.
  429. */
  430. PJ_DECL(pj_status_t) pjsip_regc_send(pjsip_regc *regc, pjsip_tx_data *tdata);
  431. PJ_END_DECL
  432. /**
  433. * @}
  434. */
  435. #endif /* __PJSIP_REG_H__ */