Revert "Revert "and added files""
[bcm963xx.git] / userapps / opensource / libosip2 / src / osip2 / nist.c
1 /*
2   The oSIP library implements the Session Initiation Protocol (SIP -rfc3261-)
3   Copyright (C) 2001,2002,2003  Aymeric MOIZARD jack@atosc.org
4   
5   This library is free software; you can redistribute it and/or
6   modify it under the terms of the GNU Lesser General Public
7   License as published by the Free Software Foundation; either
8   version 2.1 of the License, or (at your option) any later version.
9   
10   This library 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 GNU
13   Lesser General Public License for more details.
14   
15   You should have received a copy of the GNU Lesser General Public
16   License along with this library; if not, write to the Free Software
17   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18 */
19
20 #include <osip2/internal.h>
21 #include <osip2/osip.h>
22
23 #include "fsm.h"
24 #include "xixt.h"
25
26 int
27 __osip_nist_init (osip_nist_t ** nist, osip_t * osip, osip_message_t * invite)
28 {
29   int i;
30
31   OSIP_TRACE (osip_trace
32               (__FILE__, __LINE__, OSIP_INFO2, NULL,
33                "allocating NIST context\n"));
34
35   *nist = (osip_nist_t *) osip_malloc (sizeof (osip_nist_t));
36   if (*nist == NULL)
37     return -1;
38   memset (*nist, 0, sizeof (osip_nist_t));
39   /* for INVITE retransmissions */
40   {
41     osip_via_t *via;
42     char *proto;
43
44     i = osip_message_get_via (invite, 0, &via); /* get top via */
45     if (i != 0)
46       goto ii_error_1;
47     proto = via_get_protocol (via);
48     if (proto == NULL)
49       goto ii_error_1;
50
51     i = osip_strncasecmp (proto, "TCP", 3);
52     if (i != 0)
53       {
54         (*nist)->timer_j_length = 64 * DEFAULT_T1;
55         (*nist)->timer_j_start.tv_sec = -1;     /* not started */
56       }
57     else
58       {                         /* TCP is used: */
59         (*nist)->timer_j_length = 0;    /* MUST do the transition immediatly */
60         (*nist)->timer_j_start.tv_sec = -1;     /* not started */
61       }
62   }
63
64   return 0;
65
66 ii_error_1:
67   osip_free (*nist);
68   return -1;
69 }
70
71 int
72 __osip_nist_free (osip_nist_t * nist)
73 {
74   if (nist == NULL)
75     return -1;
76   OSIP_TRACE (osip_trace
77               (__FILE__, __LINE__, OSIP_INFO2, NULL,
78                "free nist ressource\n"));
79
80   osip_free (nist);
81   return 0;
82 }
83
84
85
86 osip_event_t *
87 __osip_nist_need_timer_j_event (osip_nist_t * nist, state_t state, int transactionid)
88 {
89   struct timeval now;
90   gettimeofday (&now, NULL);
91
92   if (nist == NULL)
93     return NULL;
94   if (state == NIST_COMPLETED)
95     {
96       if (nist->timer_j_start.tv_sec == -1)
97         return NULL;
98       if (timercmp (&now, &nist->timer_j_start, >))
99         return __osip_event_new (TIMEOUT_J, transactionid);
100     }
101   return NULL;
102 }
103