sip_time.c 2.61 KB
Newer Older
Pekka Pessi's avatar
Pekka Pessi committed
1 2 3 4 5 6 7
/*
 * This file is part of the Sofia-SIP package
 *
 * Copyright (C) 2005 Nokia Corporation.
 *
 * Contact: Pekka Pessi <pekka.pessi@nokia.com>
 *
8
 * This library is free software; you can redistribute it and/or
Pekka Pessi's avatar
Pekka Pessi committed
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 */

25
/**@CFILE sip_time.c
Pekka Pessi's avatar
Pekka Pessi committed
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
 * @brief SIP time handling
 *
 * Functions for handling time and dates in SIP.
 *
 * @author Pekka Pessi <Pekka.Pessi@nokia.com>.
 *
 * @date Created: Wed Apr 11 18:57:06 2001 ppessi
 */

#include "config.h"

#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <assert.h>

43 44 45 46
#include "sofia-sip/sip_parser.h"
#include <sofia-sip/sip_util.h>
#include <sofia-sip/msg_date.h>
#include <sofia-sip/su_time.h>
Pekka Pessi's avatar
Pekka Pessi committed
47 48 49 50 51 52 53 54 55

/** Return current time as seconds since Epoch. */
sip_time_t sip_now(void)
{
  return su_now().tv_sec;
}

/**@ingroup sip_expires
 *
56
 * Calculate the expiration time for a SIP @Contact.
Pekka Pessi's avatar
Pekka Pessi committed
57
 *
58 59 60 61 62
 * @param m     @Contact header
 * @param ex    @Expires header
 * @param date  @Date header
 * @param def   default expiration time
 * @param now   current time.
Pekka Pessi's avatar
Pekka Pessi committed
63 64
 *
 * @note If @a m is NULL, the function calculates the expiration time
65
 *       based on the @Expires and @Date headers.
Pekka Pessi's avatar
Pekka Pessi committed
66 67 68
 *
 * @note If @a now is 0, the function gets the current time using sip_now().
 *
69 70
 * @return
 *   The expiration time in seconds.
Pekka Pessi's avatar
Pekka Pessi committed
71 72 73 74 75 76 77 78 79 80 81 82 83 84
 */
sip_time_t sip_contact_expires(sip_contact_t const *m,
			       sip_expires_t const *ex,
			       sip_date_t const *date,
			       sip_time_t def,
			       sip_time_t now)
{
  sip_time_t time = 0, delta = def;

  /* "Contact: *" */
  if (m && m->m_url->url_type == url_any)
    return 0;

  if (m && m->m_expires) {
Pekka Pessi's avatar
Pekka Pessi committed
85
    msg_param_t expires = m->m_expires;
Pekka Pessi's avatar
Pekka Pessi committed
86 87 88 89
    if (msg_date_delta_d(&expires, &time, &delta) < 0)
      return def;
  }
  else if (ex) {
90
    time = ex->ex_date;
Pekka Pessi's avatar
Pekka Pessi committed
91 92 93 94
    delta = ex->ex_delta;
  }

  if (time) {
95
    if (date)
Pekka Pessi's avatar
Pekka Pessi committed
96 97 98 99 100 101 102 103 104 105 106 107
      now = date->d_time;
    else if (now == 0)
      now = sip_now();

    if (time > now)
      delta = time - now;
    else
      delta = 0;
  }

  return delta;
}