Revert "Revert "and added files""
[bcm963xx.git] / userapps / opensource / libosip2 / src / osip2 / ist.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
25 int
26 __osip_ist_init (osip_ist_t ** ist, osip_t * osip, osip_message_t * invite)
27 {
28   int i;
29
30   OSIP_TRACE (osip_trace
31               (__FILE__, __LINE__, OSIP_INFO2, NULL,
32                "allocating IST context\n"));
33
34   *ist = (osip_ist_t *) osip_malloc (sizeof (osip_ist_t));
35   if (*ist == NULL)
36     return -1;
37   memset (*ist, 0, sizeof (osip_ist_t));
38   /* for INVITE retransmissions */
39   {
40     osip_via_t *via;
41     char *proto;
42
43     i = osip_message_get_via (invite, 0, &via); /* get top via */
44     if (i != 0)
45       goto ii_error_1;
46     proto = via_get_protocol (via);
47     if (proto == NULL)
48       goto ii_error_1;
49
50     i = osip_strncasecmp (proto, "TCP", 3);
51     if (i != 0)
52       {                         /* for other reliable protocol than TCP, the timer
53                                    must be desactived by the external application */
54         (*ist)->timer_g_length = DEFAULT_T1;
55         (*ist)->timer_i_length = DEFAULT_T4;
56         (*ist)->timer_g_start.tv_sec = -1;      /* not started */
57         (*ist)->timer_i_start.tv_sec = -1;      /* not started */
58       }
59     else
60       {                         /* TCP is used: */
61         (*ist)->timer_g_length = -1;    /* A is not ACTIVE */
62         (*ist)->timer_i_length = 0;     /* MUST do the transition immediatly */
63         (*ist)->timer_g_start.tv_sec = -1;      /* not started */
64         (*ist)->timer_i_start.tv_sec = -1;      /* not started */
65       }
66   }
67
68   (*ist)->timer_h_length = 64 * DEFAULT_T1;
69   (*ist)->timer_h_start.tv_sec = -1;    /* not started */
70
71   return 0;
72
73 ii_error_1:
74   osip_free (*ist);
75   return -1;
76 }
77
78 int
79 __osip_ist_free (osip_ist_t * ist)
80 {
81   if (ist == NULL)
82     return -1;
83   OSIP_TRACE (osip_trace
84               (__FILE__, __LINE__, OSIP_INFO2, NULL, "free ist ressource\n"));
85   osip_free (ist);
86   return 0;
87 }
88
89 osip_event_t *
90 __osip_ist_need_timer_g_event (osip_ist_t * ist, state_t state, int transactionid)
91 {
92   struct timeval now;
93   gettimeofday (&now, NULL);
94
95   if (ist == NULL)
96     return NULL;
97   if (state == IST_COMPLETED)
98     {
99       if (ist->timer_g_start.tv_sec == -1)
100         return NULL;
101       if (timercmp (&now, &ist->timer_g_start, >))
102         return __osip_event_new (TIMEOUT_G, transactionid);
103     }
104   return NULL;
105 }
106
107 osip_event_t *
108 __osip_ist_need_timer_h_event (osip_ist_t * ist, state_t state,
109                                int transactionid)
110 {
111   struct timeval now;
112   gettimeofday (&now, NULL);
113
114   if (ist == NULL)
115     return NULL;
116   if (state == IST_COMPLETED)
117     {
118       /* may need timer H */
119       if (ist->timer_h_start.tv_sec == -1)
120         return NULL;
121       if (timercmp (&now, &ist->timer_h_start, >))
122         return __osip_event_new (TIMEOUT_H, transactionid);
123     }
124   return NULL;
125 }
126
127 osip_event_t *
128 __osip_ist_need_timer_i_event (osip_ist_t * ist, state_t state,
129                                int transactionid)
130 {
131   struct timeval now;
132   gettimeofday (&now, NULL);
133
134   if (ist == NULL)
135     return NULL;
136   if (state == IST_CONFIRMED)
137     {
138       /* may need timer I */
139       if (ist->timer_i_start.tv_sec == -1)
140         return NULL;
141       if (timercmp (&now, &ist->timer_i_start, >))
142         return __osip_event_new (TIMEOUT_I, transactionid);
143     }
144   return NULL;
145 }
146