sip_time.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * This file is part of the Sofia-SIP package
  3. *
  4. * Copyright (C) 2005 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 sip_time.c
  25. * @brief SIP time handling
  26. *
  27. * Functions for handling time and dates in SIP.
  28. *
  29. * @author Pekka Pessi <Pekka.Pessi@nokia.com>.
  30. *
  31. * @date Created: Wed Apr 11 18:57:06 2001 ppessi
  32. */
  33. #include "config.h"
  34. #include <stddef.h>
  35. #include <stdlib.h>
  36. #include <string.h>
  37. #include <limits.h>
  38. #include <assert.h>
  39. #include "sofia-sip/sip_parser.h"
  40. #include <sofia-sip/sip_util.h>
  41. #include <sofia-sip/msg_date.h>
  42. #include <sofia-sip/su_time.h>
  43. /** Return current time as seconds since Epoch. */
  44. sip_time_t sip_now(void)
  45. {
  46. return su_now().tv_sec;
  47. }
  48. /**@ingroup sip_expires
  49. *
  50. * Calculate the expiration time for a SIP @Contact.
  51. *
  52. * @param m @Contact header
  53. * @param ex @Expires header
  54. * @param date @Date header
  55. * @param def default expiration time
  56. * @param now current time.
  57. *
  58. * @note If @a m is NULL, the function calculates the expiration time
  59. * based on the @Expires and @Date headers.
  60. *
  61. * @note If @a now is 0, the function gets the current time using sip_now().
  62. *
  63. * @return
  64. * The expiration time in seconds.
  65. */
  66. sip_time_t sip_contact_expires(sip_contact_t const *m,
  67. sip_expires_t const *ex,
  68. sip_date_t const *date,
  69. sip_time_t def,
  70. sip_time_t now)
  71. {
  72. sip_time_t time = 0, delta = def;
  73. /* "Contact: *" */
  74. if (m && m->m_url->url_type == url_any)
  75. return 0;
  76. if (m && m->m_expires) {
  77. msg_param_t expires = m->m_expires;
  78. if (msg_date_delta_d(&expires, &time, &delta) < 0)
  79. return def;
  80. }
  81. else if (ex) {
  82. time = ex->ex_date;
  83. delta = ex->ex_delta;
  84. }
  85. if (time) {
  86. if (date)
  87. now = date->d_time;
  88. else if (now == 0)
  89. now = sip_now();
  90. if (time > now)
  91. delta = time - now;
  92. else
  93. delta = 0;
  94. }
  95. return delta;
  96. }