nua_dialog.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681
  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_dialog.c
  25. * @brief Dialog and dialog usage handling
  26. *
  27. * @author Pekka Pessi <Pekka.Pessi@nokia.com>
  28. *
  29. * @date Created: Wed Mar 8 11:48:49 EET 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. #include <sofia-sip/su_string.h>
  38. #include <sofia-sip/su_uniqueid.h>
  39. #include <sofia-sip/sip_protos.h>
  40. #include <sofia-sip/sip_status.h>
  41. #define NUA_OWNER_T su_home_t
  42. #include "nua_dialog.h"
  43. #define SU_LOG (nua_log)
  44. #include <sofia-sip/su_debug.h>
  45. #ifndef NONE
  46. #ifndef _MSC_VER
  47. #define NONE ((void *)-1)
  48. #else
  49. #define NONE ((void *)(INT_PTR)-1)
  50. #endif
  51. #endif
  52. /* ======================================================================== */
  53. /* Dialog handling */
  54. static void nua_dialog_usage_remove_at(nua_owner_t*, nua_dialog_state_t*,
  55. nua_dialog_usage_t**,
  56. nua_client_request_t *cr,
  57. nua_server_request_t *sr);
  58. static void nua_dialog_log_usage(nua_owner_t *, nua_dialog_state_t *);
  59. /**@internal
  60. * UAS tag and route.
  61. *
  62. * Update dialog tags and route on the UAS side.
  63. *
  64. * @param own dialog owner
  65. * @param ds dialog state
  66. * @param sip SIP message containing response used to update dialog
  67. * @param rtag if true, set remote tag within the leg
  68. */
  69. void nua_dialog_uas_route(nua_owner_t *own,
  70. nua_dialog_state_t *ds,
  71. sip_t const *sip,
  72. int rtag)
  73. {
  74. int established = nua_dialog_is_established(ds);
  75. if (!established && sip->sip_from->a_tag)
  76. ds->ds_remote_tag = su_strdup(own, sip->sip_from->a_tag);
  77. if (ds->ds_leg == NULL)
  78. return;
  79. nta_leg_server_route(ds->ds_leg, sip->sip_record_route, sip->sip_contact);
  80. ds->ds_route = ds->ds_route || sip->sip_record_route || sip->sip_contact;
  81. if (rtag && !established && sip->sip_from->a_tag)
  82. nta_leg_rtag(ds->ds_leg, sip->sip_from->a_tag);
  83. }
  84. /**@internal
  85. * UAC tag and route.
  86. *
  87. * Update dialog tags and route on the UAC side.
  88. *
  89. * @param own dialog owner
  90. * @param ds dialog state
  91. * @param sip SIP message containing response used to update dialog
  92. * @param rtag if true, set remote tag within the leg
  93. * @param initial if true, @a sip is response to initial transaction
  94. */
  95. void nua_dialog_uac_route(nua_owner_t *own,
  96. nua_dialog_state_t *ds,
  97. sip_t const *sip,
  98. int rtag,
  99. int initial)
  100. {
  101. int established = nua_dialog_is_established(ds);
  102. int status = sip->sip_status->st_status;
  103. if (!established && sip->sip_to->a_tag)
  104. ds->ds_remote_tag = su_strdup(own, sip->sip_to->a_tag);
  105. if (ds->ds_leg == NULL)
  106. return;
  107. if (initial && status >= 200)
  108. nta_leg_client_reroute(ds->ds_leg, sip->sip_record_route, sip->sip_contact, 1);
  109. else
  110. nta_leg_client_reroute(ds->ds_leg, sip->sip_record_route, sip->sip_contact, 0);
  111. ds->ds_route = ds->ds_route || sip->sip_record_route || sip->sip_contact;
  112. if (rtag && !established && sip->sip_to->a_tag)
  113. nta_leg_rtag(ds->ds_leg, sip->sip_to->a_tag);
  114. }
  115. /**@internal Store information from remote endpoint. */
  116. void nua_dialog_store_peer_info(nua_owner_t *own,
  117. nua_dialog_state_t *ds,
  118. sip_t const *sip)
  119. {
  120. nua_dialog_peer_info_t *nr = ds->ds_remote_ua;
  121. nua_dialog_usage_t *du;
  122. nua_dialog_peer_info_t old[1];
  123. *old = *nr;
  124. if (sip && sip->sip_status &&
  125. sip->sip_status->st_status >= 300 &&
  126. sip->sip_status->st_status <= 399)
  127. sip = NULL; /* Redirected */
  128. if (sip == NULL) {
  129. nr->nr_via = NULL, su_free(own, old->nr_via);
  130. nr->nr_allow = NULL, su_free(own, old->nr_allow);
  131. nr->nr_accept = NULL, su_free(own, old->nr_accept);
  132. nr->nr_require = NULL, su_free(own, old->nr_require);
  133. nr->nr_supported = NULL, su_free(own, old->nr_supported);
  134. nr->nr_user_agent = NULL, su_free(own, old->nr_user_agent);
  135. return;
  136. }
  137. if (sip->sip_allow) {
  138. nr->nr_allow = sip_allow_dup(own, sip->sip_allow);
  139. su_free(own, old->nr_allow);
  140. }
  141. if (sip->sip_accept) {
  142. nr->nr_accept = sip_accept_dup(own, sip->sip_accept);
  143. su_free(own, old->nr_accept);
  144. }
  145. if (sip->sip_require) {
  146. nr->nr_require = sip_require_dup(own, sip->sip_require);
  147. su_free(own, old->nr_require);
  148. }
  149. if (sip->sip_supported) {
  150. nr->nr_supported = sip_supported_dup(own, sip->sip_supported);
  151. su_free(own, old->nr_supported);
  152. }
  153. if (sip->sip_via) {
  154. nr->nr_via = sip_via_dup(own, sip->sip_via);
  155. su_free(own, old->nr_via);
  156. }
  157. if (sip->sip_user_agent) {
  158. nr->nr_user_agent = sip_user_agent_dup(own, sip->sip_user_agent);
  159. su_free(own, old->nr_user_agent);
  160. }
  161. else if (sip->sip_server) {
  162. nr->nr_user_agent = sip_user_agent_dup(own, sip->sip_server);
  163. su_free(own, old->nr_user_agent);
  164. }
  165. for (du = ds->ds_usage; du; du = du->du_next) {
  166. if (du->du_class->usage_peer_info)
  167. du->du_class->usage_peer_info(du, ds, sip);
  168. }
  169. }
  170. /** Remove dialog information. */
  171. int nua_dialog_zap(nua_owner_t *own,
  172. nua_dialog_state_t *ds)
  173. {
  174. /* zap peer info */
  175. nua_dialog_store_peer_info(own, ds, NULL);
  176. /* Local Contact */
  177. msg_header_free(own, (msg_header_t *)ds->ds_ltarget), ds->ds_ltarget = NULL;
  178. /* Leg */
  179. nta_leg_destroy(ds->ds_leg), ds->ds_leg = NULL;
  180. /* Remote tag */
  181. su_free(own, (void *)ds->ds_remote_tag), ds->ds_remote_tag = NULL;
  182. /* Ready to set route/remote target */
  183. ds->ds_route = 0;
  184. return 0;
  185. }
  186. /** Remove dialog (if there is no other usages). */
  187. int nua_dialog_remove(nua_owner_t *own,
  188. nua_dialog_state_t *ds,
  189. nua_dialog_usage_t *usage)
  190. {
  191. if (ds->ds_usage == usage && (usage == NULL || usage->du_next == NULL)) {
  192. return nua_dialog_zap(own, ds);
  193. }
  194. return 0;
  195. }
  196. /** @internal Get dialog usage slot. */
  197. nua_dialog_usage_t **
  198. nua_dialog_usage_at(nua_dialog_state_t const *ds,
  199. nua_usage_class const *kind,
  200. sip_event_t const *event)
  201. {
  202. static nua_dialog_usage_t *none = NULL;
  203. if (ds) {
  204. nua_dialog_usage_t *du, * const * prev;
  205. sip_event_t const *o;
  206. for (prev = &ds->ds_usage; (du = *prev); prev = &du->du_next) {
  207. if (du->du_class != kind)
  208. continue;
  209. if (event == NONE)
  210. return (nua_dialog_usage_t **)prev;
  211. o = du->du_event;
  212. if (!event && !o)
  213. return (nua_dialog_usage_t **)prev;
  214. if (event != o) {
  215. if (event == NULL || o == NULL)
  216. continue;
  217. if (!su_strmatch(event->o_type, o->o_type))
  218. continue;
  219. if (!su_casematch(event->o_id, o->o_id)) {
  220. if (event->o_id || !su_strmatch(event->o_type, "refer"))
  221. continue;
  222. }
  223. }
  224. return (nua_dialog_usage_t **)prev;
  225. }
  226. }
  227. return &none;
  228. }
  229. /** @internal Get a dialog usage */
  230. nua_dialog_usage_t *nua_dialog_usage_get(nua_dialog_state_t const *ds,
  231. nua_usage_class const *kind,
  232. sip_event_t const *event)
  233. {
  234. return *nua_dialog_usage_at(ds, kind, event);
  235. }
  236. /** @internal Get dialog usage name */
  237. char const *nua_dialog_usage_name(nua_dialog_usage_t const *du)
  238. {
  239. if (du == NULL)
  240. return "<NULL>";
  241. return du->du_class->usage_name(du);
  242. }
  243. /** @internal Add dialog usage */
  244. nua_dialog_usage_t *nua_dialog_usage_add(nua_owner_t *own,
  245. struct nua_dialog_state *ds,
  246. nua_usage_class const *uclass,
  247. sip_event_t const *event)
  248. {
  249. if (ds) {
  250. sip_event_t *o;
  251. nua_dialog_usage_t *du, **prev_du;
  252. prev_du = nua_dialog_usage_at(ds, uclass, event);
  253. du = *prev_du;
  254. if (du) { /* Already exists */
  255. SU_DEBUG_5(("nua(%p): adding already existing %s usage%s%s\n",
  256. (void *)own, nua_dialog_usage_name(du),
  257. event ? " with event " : "", event ? event->o_type : ""));
  258. if (prev_du != &ds->ds_usage) {
  259. /* Move as a first usage in the list */
  260. *prev_du = du->du_next;
  261. du->du_next = ds->ds_usage;
  262. ds->ds_usage = du;
  263. }
  264. return du;
  265. }
  266. o = event ? sip_event_dup(own, event) : NULL;
  267. if (o != NULL || event == NULL)
  268. du = su_zalloc(own, sizeof *du + uclass->usage_size);
  269. if (du) {
  270. su_home_ref(own);
  271. du->du_dialog = ds;
  272. du->du_class = uclass;
  273. du->du_event = o;
  274. if (uclass->usage_add(own, ds, du) < 0) {
  275. su_free(own, o);
  276. su_free(own, du);
  277. return NULL;
  278. }
  279. SU_DEBUG_5(("nua(%p): adding %s usage%s%s\n",
  280. (void *)own, nua_dialog_usage_name(du),
  281. o ? " with event " : "", o ? o->o_type :""));
  282. du->du_next = ds->ds_usage, ds->ds_usage = du;
  283. return du;
  284. }
  285. su_free(own, o);
  286. }
  287. return NULL;
  288. }
  289. /** @internal Remove dialog usage. */
  290. void nua_dialog_usage_remove(nua_owner_t *own,
  291. nua_dialog_state_t *ds,
  292. nua_dialog_usage_t *du,
  293. nua_client_request_t *cr,
  294. nua_server_request_t *sr)
  295. {
  296. nua_dialog_usage_t **at;
  297. assert(own); assert(ds); assert(du);
  298. for (at = &ds->ds_usage; *at; at = &(*at)->du_next)
  299. if (du == *at)
  300. break;
  301. assert(*at);
  302. nua_dialog_usage_remove_at(own, ds, at, cr, sr);
  303. }
  304. /** @internal Remove dialog usage.
  305. *
  306. * Zap dialog state (leg, tag and route) if no usages remain.
  307. */
  308. static void
  309. nua_dialog_usage_remove_at(nua_owner_t *own,
  310. nua_dialog_state_t *ds,
  311. nua_dialog_usage_t **at,
  312. nua_client_request_t *cr0,
  313. nua_server_request_t *sr0)
  314. {
  315. int unref = 0;
  316. nua_dialog_usage_t *du = NULL;
  317. if (*at) {
  318. sip_event_t const *o = NULL;
  319. nua_client_request_t *cr, *cr_next;
  320. nua_server_request_t *sr, *sr_next;
  321. du = *at;
  322. *at = du->du_next;
  323. o = du->du_event;
  324. SU_DEBUG_5(("nua(%p): removing %s usage%s%s\n",
  325. (void *)own, nua_dialog_usage_name(du),
  326. o ? " with event " : "", o ? o->o_type :""));
  327. du->du_class->usage_remove(own, ds, du, cr0, sr0);
  328. /* Clean reference to saved client request */
  329. if (du->du_cr)
  330. nua_client_bind(du->du_cr, NULL);
  331. /* Clean references from queued client requests */
  332. for (cr = ds->ds_cr; cr; cr = cr_next) {
  333. cr_next = cr->cr_next;
  334. if (cr->cr_usage == du)
  335. cr->cr_usage = NULL;
  336. }
  337. /* Clean references from queued server requests */
  338. for (sr = ds->ds_sr; sr; sr = sr_next) {
  339. sr_next = sr->sr_next;
  340. if (sr->sr_usage == du) {
  341. sr->sr_usage = NULL;
  342. if (sr != sr0)
  343. nua_server_request_destroy(sr);
  344. }
  345. }
  346. unref = 1;
  347. }
  348. /* Zap dialog if there are no more usages */
  349. if (ds->ds_terminating)
  350. ;
  351. else if (ds->ds_usage == NULL) {
  352. nua_dialog_remove(own, ds, NULL);
  353. ds->ds_has_events = 0;
  354. if (unref) {
  355. su_home_unref(own);
  356. su_free(own, du);
  357. }
  358. return;
  359. }
  360. else {
  361. nua_dialog_log_usage(own, ds);
  362. }
  363. if (unref) {
  364. su_home_unref(own);
  365. su_free(own, du);
  366. }
  367. }
  368. static
  369. void nua_dialog_log_usage(nua_owner_t *own, nua_dialog_state_t *ds)
  370. {
  371. nua_dialog_usage_t *du;
  372. if (SU_LOG->log_level >= 3) {
  373. char buffer[160];
  374. size_t l = 0, N = sizeof buffer;
  375. ssize_t n;
  376. buffer[0] = '\0';
  377. for (du = ds->ds_usage; du; du = du->du_next) {
  378. msg_header_t const *h = (void *)du->du_event;
  379. if (!h)
  380. continue;
  381. n = sip_event_e(buffer + l, N - l, h, 0);
  382. if (n == -1)
  383. break;
  384. l += (size_t)n;
  385. if (du->du_next && l + 2 < sizeof(buffer)) {
  386. strcpy(buffer + l, ", ");
  387. l += 2;
  388. }
  389. }
  390. SU_DEBUG_3(("nua(%p): handle with %s%s%s\n", (void *)own,
  391. ds->ds_has_session ? "session and " : "",
  392. ds->ds_has_events ? "events " : "",
  393. buffer));
  394. }
  395. }
  396. /** Deinitialize dialog and its usage. @internal */
  397. void nua_dialog_deinit(nua_owner_t *own,
  398. nua_dialog_state_t *ds)
  399. {
  400. ds->ds_terminating = 1;
  401. while (ds->ds_usage) {
  402. nua_dialog_usage_remove_at(own, ds, &ds->ds_usage, NULL, NULL);
  403. }
  404. nua_dialog_remove(own, ds, NULL);
  405. ds->ds_has_events = 0;
  406. ds->ds_terminating = 0;
  407. }
  408. void nua_dialog_update_params(nua_dialog_state_t *ds,
  409. nua_handle_preferences_t const *changed,
  410. nua_handle_preferences_t const *params,
  411. nua_handle_preferences_t const *defaults)
  412. {
  413. nua_dialog_usage_t *usage;
  414. for (usage = ds->ds_usage; usage; usage = usage->du_next) {
  415. usage->du_class->usage_update_params(usage, changed, params, defaults);
  416. }
  417. }
  418. void nua_base_usage_update_params(nua_dialog_usage_t const *du,
  419. nua_handle_preferences_t const *changed,
  420. nua_handle_preferences_t const *params,
  421. nua_handle_preferences_t const *defaults)
  422. {
  423. (void)du, (void)changed, (void)params, (void)defaults;
  424. }
  425. /**@internal
  426. * Set refresh value suitably.
  427. *
  428. * The refresh time is set either around half of the @a delta interval or,
  429. * if @a delta is less than 5 minutes but longer than 90 seconds, 30..60
  430. * seconds before end of interval.
  431. *
  432. * If @a delta is 0, the dialog usage is never refreshed.
  433. */
  434. void nua_dialog_usage_set_refresh(nua_dialog_usage_t *du, unsigned delta)
  435. {
  436. if (delta == 0)
  437. nua_dialog_usage_reset_refresh(du);
  438. else if (delta > 90 && delta < 5 * 60)
  439. /* refresh 30..60 seconds before deadline */
  440. nua_dialog_usage_set_refresh_range(du, delta - 60, delta - 30);
  441. else {
  442. /* By default, refresh around half time before deadline */
  443. unsigned min = (delta + 2) / 4;
  444. unsigned max = (delta + 2) / 4 + (delta + 1) / 2;
  445. if (min == 0)
  446. min = 1;
  447. nua_dialog_usage_set_refresh_range(du, min, max);
  448. }
  449. }
  450. /**@internal Set refresh in range min..max seconds in the future. */
  451. void nua_dialog_usage_set_refresh_range(nua_dialog_usage_t *du,
  452. unsigned min, unsigned max)
  453. {
  454. sip_time_t now = sip_now(), target;
  455. unsigned delta;
  456. if (max < min)
  457. max = min;
  458. if (min != max)
  459. delta = su_randint(min, max);
  460. else
  461. delta = min;
  462. if (now + delta >= now)
  463. target = now + delta;
  464. else
  465. target = SIP_TIME_MAX;
  466. SU_DEBUG_7(("nua(): refresh %s after %lu seconds (in [%u..%u])\n",
  467. nua_dialog_usage_name(du), target - now, min, max));
  468. du->du_refquested = now;
  469. du->du_refresh = target;
  470. }
  471. /** Set absolute refresh time */
  472. void nua_dialog_usage_set_refresh_at(nua_dialog_usage_t *du,
  473. sip_time_t target)
  474. {
  475. SU_DEBUG_7(("nua(): refresh %s after %lu seconds\n",
  476. nua_dialog_usage_name(du), target - sip_now()));
  477. du->du_refresh = target;
  478. }
  479. /**@internal Do not refresh. */
  480. void nua_dialog_usage_reset_refresh(nua_dialog_usage_t *du)
  481. {
  482. if (du) {
  483. du->du_refquested = sip_now();
  484. du->du_refresh = 0;
  485. }
  486. }
  487. /** @internal Refresh usage. */
  488. void nua_dialog_usage_refresh(nua_owner_t *owner,
  489. nua_dialog_state_t *ds,
  490. nua_dialog_usage_t *du,
  491. sip_time_t now)
  492. {
  493. assert(du && du->du_class->usage_refresh);
  494. du->du_class->usage_refresh(owner, ds, du, now);
  495. }
  496. /** Terminate all dialog usages gracefully. */
  497. int nua_dialog_shutdown(nua_owner_t *owner, nua_dialog_state_t *ds)
  498. {
  499. nua_dialog_usage_t *du;
  500. ds->ds_terminating = 1;
  501. do {
  502. for (du = ds->ds_usage; du; du = du->du_next) {
  503. if (!du->du_shutdown) {
  504. nua_dialog_usage_shutdown(owner, ds, du);
  505. break;
  506. }
  507. }
  508. } while (du);
  509. return 1;
  510. }
  511. /** Shutdown (gracefully terminate) usage.
  512. *
  513. * @retval >0 shutdown done
  514. * @retval 0 shutdown in progress
  515. * @retval <0 try again later
  516. */
  517. int nua_dialog_usage_shutdown(nua_owner_t *owner,
  518. nua_dialog_state_t *ds,
  519. nua_dialog_usage_t *du)
  520. {
  521. if (du) {
  522. nua_dialog_usage_reset_refresh(du);
  523. du->du_shutdown = 1;
  524. assert(du->du_class->usage_shutdown);
  525. return du->du_class->usage_shutdown(owner, ds, du);
  526. }
  527. else
  528. return 200;
  529. }
  530. /** Repeat shutdown of all usages.
  531. *
  532. * @note Caller must have a reference to nh
  533. */
  534. int nua_dialog_repeat_shutdown(nua_owner_t *owner, nua_dialog_state_t *ds)
  535. {
  536. nua_dialog_usage_t *du;
  537. nua_server_request_t *sr, *sr_next;
  538. for (sr = ds->ds_sr; sr; sr = sr_next) {
  539. sr_next = sr->sr_next;
  540. if (nua_server_request_is_pending(sr)) {
  541. SR_STATUS1(sr, SIP_410_GONE); /* 410 terminates dialog */
  542. nua_server_respond(sr, NULL);
  543. if (nua_server_report(sr) > 2) {
  544. /*
  545. Dialog was destroyed if nua_base_server_report() returned 3 or 4.
  546. All references (sr) are cleared from queued server requests at this point.
  547. See nua_dialog_deinit() and nua_dialog_usage_remove_at()
  548. */
  549. break;
  550. }
  551. }
  552. }
  553. for (du = ds->ds_usage; du ;) {
  554. nua_dialog_usage_t *du_next = du->du_next;
  555. nua_dialog_usage_shutdown(owner, ds, du);
  556. if (du_next == NULL)
  557. break;
  558. for (du = ds->ds_usage; du; du = du->du_next) {
  559. if (du == du_next)
  560. break;
  561. else if (!du->du_shutdown)
  562. break;
  563. }
  564. }
  565. return ds->ds_usage != NULL;
  566. }