and added files
[bcm963xx.git] / userapps / opensource / libosip2 / src / osip2 / nict.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_nict_init (osip_nict_t ** nict, osip_t * osip,
28                   osip_message_t * request)
29 {
30   osip_route_t *route;
31   int i;
32   time_t now;
33
34   OSIP_TRACE (osip_trace
35               (__FILE__, __LINE__, OSIP_INFO2, NULL,
36                "allocating NICT context\n"));
37
38   *nict = (osip_nict_t *) osip_malloc (sizeof (osip_nict_t));
39   if (*nict == NULL)
40     return -1;
41   now = time (NULL);
42   memset (*nict, 0, sizeof (osip_nict_t));
43   /* for REQUEST retransmissions */
44   {
45     osip_via_t *via;
46     char *proto;
47
48     i = osip_message_get_via (request, 0, &via);        /* get top via */
49     if (i != 0)
50       goto ii_error_1;
51     proto = via_get_protocol (via);
52     if (proto == NULL)
53       goto ii_error_1;
54
55     i = osip_strncasecmp (proto, "TCP", 3);
56     if (i != 0)
57       {
58         (*nict)->timer_e_length = DEFAULT_T1;
59         (*nict)->timer_k_length = DEFAULT_T4;
60         gettimeofday (&(*nict)->timer_e_start, NULL);
61         add_gettimeofday (&(*nict)->timer_e_start, (*nict)->timer_e_length);
62         (*nict)->timer_k_start.tv_sec = -1;     /* not started */
63       }
64     else
65       {                         /* TCP is used: */
66         (*nict)->timer_e_length = -1;   /* E is not ACTIVE */
67         (*nict)->timer_k_length = 0;    /* MUST do the transition immediatly */
68         (*nict)->timer_e_start.tv_sec = -1;
69         (*nict)->timer_k_start.tv_sec = -1;     /* not started */
70       }
71   }
72
73   /* for PROXY, the destination MUST be set by the application layer,
74      this one may not be correct. */
75   osip_message_get_route (request, 0, &route);
76   if (route != NULL)
77     {
78       int port = 5060;
79
80       if (route->url->port != NULL)
81         port = osip_atoi (route->url->port);
82       osip_nict_set_destination ((*nict), osip_strdup (route->url->host),
83                                  port);
84     }
85   else
86     (*nict)->port = 5060;
87
88   (*nict)->timer_f_length = 64 * DEFAULT_T1;
89   gettimeofday (&(*nict)->timer_f_start, NULL);
90   add_gettimeofday (&(*nict)->timer_f_start, (*nict)->timer_f_length);
91
92   /* Oups! a Bug! */
93   /*  (*nict)->port  = 5060; */
94
95   return 0;
96
97 ii_error_1:
98   osip_free (*nict);
99   return -1;
100 }
101
102 int
103 __osip_nict_free (osip_nict_t * nict)
104 {
105   if (nict == NULL)
106     return -1;
107   OSIP_TRACE (osip_trace
108               (__FILE__, __LINE__, OSIP_INFO2, NULL,
109                "free nict ressource\n"));
110
111   osip_free (nict->destination);
112   osip_free (nict);
113   return 0;
114 }
115
116 int
117 osip_nict_set_destination (osip_nict_t * nict, char *destination, int port)
118 {
119   if (nict == NULL)
120     return -1;
121   if (nict->destination != NULL)
122     osip_free (nict->destination);
123   nict->destination = destination;
124   nict->port = port;
125   return 0;
126 }
127
128 osip_event_t *
129 __osip_nict_need_timer_e_event (osip_nict_t * nict, state_t state, int transactionid)
130 {
131   struct timeval now;
132   gettimeofday (&now, NULL);
133
134   if (nict == NULL)
135     return NULL;
136   if (state == NICT_PROCEEDING || state == NICT_TRYING)
137     {
138       if (nict->timer_e_start.tv_sec == -1)
139         return NULL;
140       if (timercmp (&now, &nict->timer_e_start, >))
141         return __osip_event_new (TIMEOUT_E, transactionid);
142     }
143   return NULL;
144 }
145
146 osip_event_t *
147 __osip_nict_need_timer_f_event (osip_nict_t * nict, state_t state,
148                                 int transactionid)
149 {
150   struct timeval now;
151   gettimeofday (&now, NULL);
152
153   if (nict == NULL)
154     return NULL;
155   if (state == NICT_PROCEEDING || state == NICT_TRYING)
156     {
157       if (nict->timer_f_start.tv_sec == -1)
158         return NULL;
159       if (timercmp (&now, &nict->timer_f_start, >))
160         return __osip_event_new (TIMEOUT_F, transactionid);
161     }
162   return NULL;
163 }
164
165 osip_event_t *
166 __osip_nict_need_timer_k_event (osip_nict_t * nict, state_t state,
167                                 int transactionid)
168 {
169   struct timeval now;
170   gettimeofday (&now, NULL);
171
172   if (nict == NULL)
173     return NULL;
174   if (state == NICT_COMPLETED)
175     {
176       if (nict->timer_k_start.tv_sec == -1)
177         return NULL;
178       if (timercmp (&now, &nict->timer_k_start, >))
179         return __osip_event_new (TIMEOUT_K, transactionid);
180     }
181   return NULL;
182 }
183