nua_registrar.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /*
  2. * This file is part of the Sofia-SIP package
  3. *
  4. * Copyright (C) 2006 Nokia Corporation.
  5. *
  6. * Contact: Pekka Pessi <pekka.pessi@nokia.com>
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public License
  10. * as published by the Free Software Foundation; either version 2.1 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with this library; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  21. * 02110-1301 USA
  22. *
  23. */
  24. /**@CFILE nua_registrar.c
  25. * @brief REGISTER UAS
  26. *
  27. * @author Michael Jerris
  28. *
  29. * @date Created: Tue Oct 3 10:14:54 EEST 2006 ppessi
  30. */
  31. #include "config.h"
  32. #include <stddef.h>
  33. #include <stdlib.h>
  34. #include <string.h>
  35. #include <limits.h>
  36. #include <assert.h>
  37. #define TP_CLIENT_T struct nua_handle_s
  38. #define TP_STACK_T struct nta_agent_s
  39. #include <sofia-sip/su_string.h>
  40. #include <sofia-sip/sip_protos.h>
  41. #include <sofia-sip/sip_status.h>
  42. #include <sofia-sip/sip_util.h>
  43. #define NTA_INCOMING_MAGIC_T struct nua_handle_s
  44. #define NTA_RELIABLE_MAGIC_T struct nua_handle_s
  45. #include "nua_stack.h"
  46. #include <sofia-sip/tport.h>
  47. #include <sofia-sip/nta_tport.h>
  48. /* ---------------------------------------------------------------------- */
  49. /* Registrar usage */
  50. struct registrar_usage
  51. {
  52. tport_t *tport; /**< */
  53. int pending; /**< Waiting for tport to close */
  54. };
  55. static char const *nua_registrar_usage_name(nua_dialog_usage_t const *du)
  56. {
  57. return "registrar";
  58. }
  59. static int nua_registrar_usage_add(nua_handle_t *nh,
  60. nua_dialog_state_t *ds,
  61. nua_dialog_usage_t *du)
  62. {
  63. return 0;
  64. }
  65. static void nua_registrar_usage_remove(nua_handle_t *nh,
  66. nua_dialog_state_t *ds,
  67. nua_dialog_usage_t *du,
  68. nua_client_request_t *cr,
  69. nua_server_request_t *sr)
  70. {
  71. struct registrar_usage *ru;
  72. ru = nua_dialog_usage_private(du);
  73. if (!ru)
  74. return;
  75. if (ru->pending)
  76. tport_release(ru->tport, ru->pending, NULL, NULL, nh, 0), ru->pending = 0;
  77. tport_unref(ru->tport), ru->tport = NULL;
  78. }
  79. static void nua_registrar_usage_refresh(nua_handle_t *nh,
  80. nua_dialog_state_t *ds,
  81. nua_dialog_usage_t *du,
  82. sip_time_t now)
  83. {
  84. }
  85. /** Terminate registration usage.
  86. *
  87. * @retval >0 shutdown done
  88. * @retval 0 shutdown in progress
  89. * @retval <0 try again later
  90. */
  91. static int nua_registrar_usage_shutdown(nua_handle_t *nh,
  92. nua_dialog_state_t *ds,
  93. nua_dialog_usage_t *du)
  94. {
  95. return 1;
  96. }
  97. static nua_usage_class const nua_registrar_usage[1] = {
  98. {
  99. sizeof (struct registrar_usage), sizeof nua_registrar_usage,
  100. nua_registrar_usage_add,
  101. nua_registrar_usage_remove,
  102. nua_registrar_usage_name,
  103. nua_base_usage_update_params,
  104. NULL,
  105. nua_registrar_usage_refresh,
  106. nua_registrar_usage_shutdown
  107. }};
  108. /* ======================================================================== */
  109. /* REGISTER */
  110. /** @NUA_EVENT nua_i_register
  111. *
  112. * Incoming REGISTER request.
  113. *
  114. * In order to receive #nua_i_register events, the application must enable
  115. * the REGISTER method with NUTAG_ALLOW() tag, e.g.,
  116. * @verbatim
  117. * nua_set_params(nua;
  118. * NUTAG_APPL_METHOD("REGISTER"),
  119. * NUTAG_ALLOW("REGISTER"),
  120. * TAG_END());
  121. * @endverbatim
  122. *
  123. * The nua_response() call responding to a REGISTER request must include
  124. * NUTAG_WITH() (or NUTAG_WITH_THIS()/NUTAG_WITH_SAVED()) tag. Note that
  125. * a successful response to REGISTER @b MUST include the @Contact header
  126. * bound to the the AoR URI (in @To header).
  127. *
  128. * The REGISTER request does not create a dialog. Currently the processing
  129. * of incoming REGISTER creates a new handle for each incoming request which
  130. * is not assiciated with an existing dialog. If the handle @a nh is not
  131. * bound, you should probably destroy it after responding to the REGISTER
  132. * request.
  133. *
  134. * @param status status code of response sent automatically by stack
  135. * @param phrase a short textual description of @a status code
  136. * @param nh operation handle associated with the request
  137. * @param hmagic application context associated with the handle
  138. * (usually NULL)
  139. * @param sip incoming REGISTER request
  140. * @param tags empty
  141. *
  142. * @sa nua_respond(), @RFC3261 section 10.3,
  143. * @Expires, @Contact, @CallID, @CSeq,
  144. * @Path, @RFC3327, @ServiceRoute, @RFC3608, @RFC3680,
  145. * nua_register(), #nua_i_register, nua_unregister(), #nua_i_unregister
  146. *
  147. * @since New in @VERSION_1_12_4
  148. * @END_NUA_EVENT
  149. */
  150. static int nua_registrar_server_preprocess(nua_server_request_t *sr);
  151. static int nua_registrar_server_report(nua_server_request_t *, tagi_t const *);
  152. nua_server_methods_t const nua_register_server_methods =
  153. {
  154. SIP_METHOD_REGISTER,
  155. nua_i_register, /* Event */
  156. {
  157. 0, /* Do not create dialog */
  158. 0, /* Initial request */
  159. 0, /* Not a target refresh request */
  160. 0, /* Do not add Contact */
  161. },
  162. nua_base_server_init,
  163. nua_registrar_server_preprocess,
  164. nua_base_server_params,
  165. nua_base_server_respond,
  166. nua_registrar_server_report,
  167. };
  168. static void
  169. registrar_tport_error(nta_agent_t *nta, nua_handle_t *nh,
  170. tport_t *tp, msg_t *msg, int error)
  171. {
  172. nua_dialog_state_t *ds = nh->nh_ds;
  173. nua_dialog_usage_t *du;
  174. struct registrar_usage *ru;
  175. SU_DEBUG_3(("tport error %d: %s\n", error, su_strerror(error)));
  176. du = nua_dialog_usage_get(ds, nua_registrar_usage, NULL);
  177. if (du == NULL)
  178. return;
  179. ru = nua_dialog_usage_private(du);
  180. if (ru->tport) {
  181. tport_release(ru->tport, ru->pending, NULL, NULL, nh, 0), ru->pending = 0;
  182. tport_unref(ru->tport), ru->tport = NULL;
  183. }
  184. nua_stack_event(nh->nh_nua, nh, NULL,
  185. nua_i_media_error, 500, "Transport error detected",
  186. NULL);
  187. }
  188. static int
  189. nua_registrar_server_preprocess(nua_server_request_t *sr)
  190. {
  191. nua_handle_t *nh = sr->sr_owner;
  192. nua_dialog_state_t *ds = sr->sr_owner->nh_ds;
  193. nua_dialog_usage_t *du;
  194. struct registrar_usage *ru;
  195. tport_t *tport;
  196. tport = nta_incoming_transport(nh->nh_nua->nua_nta, sr->sr_irq, sr->sr_request.msg);
  197. if (!tport_is_tcp(tport)) {
  198. tport_unref(tport);
  199. return 0;
  200. }
  201. du = nua_dialog_usage_get(ds, nua_registrar_usage, NULL);
  202. if (du == NULL)
  203. du = nua_dialog_usage_add(nh, ds, nua_registrar_usage, NULL);
  204. if (du == NULL)
  205. return SR_STATUS1(sr, SIP_500_INTERNAL_SERVER_ERROR);
  206. ru = nua_dialog_usage_private(du);
  207. if (ru->tport && ru->tport != tport) {
  208. tport_release(ru->tport, ru->pending, NULL, NULL, nh, 0), ru->pending = 0;
  209. tport_unref(ru->tport), ru->tport = NULL;
  210. }
  211. ru->tport = tport;
  212. ru->pending = tport_pend(tport, NULL, registrar_tport_error, nh);
  213. tport_set_params(tport,
  214. TPTAG_SDWN_ERROR(1),
  215. TAG_END());
  216. return 0;
  217. }
  218. static int
  219. nua_registrar_server_report(nua_server_request_t *sr, tagi_t const *tags)
  220. {
  221. return nua_base_server_report(sr, tags);
  222. }