File: nict_fsm.c

package info (click to toggle)
libosip 0.9.7-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 2,852 kB
  • ctags: 1,812
  • sloc: ansic: 20,266; sh: 9,218; makefile: 215
file content (357 lines) | stat: -rw-r--r-- 11,250 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/*
  The oSIP library implements the Session Initiation Protocol (SIP -rfc3261-)
  Copyright (C) 2001,2002,2003  Aymeric MOIZARD jack@atosc.org
  
  This library is free software; you can redistribute it and/or
  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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#include <osip/port.h>
#include <osip/osip.h>

#include "fsm.h"

statemachine_t *nict_fsm;

statemachine_t *
nict_get_fsm ()
{
  return nict_fsm;
}

void
nict_unload_fsm ()
{
  transition_t *transition;
  statemachine_t *statemachine = nict_get_fsm ();

  while (!list_eol (statemachine->transitions, 0))
    {
      transition = (transition_t *) list_get (statemachine->transitions, 0);
      list_remove (statemachine->transitions, 0);
      sfree (transition);
    }
  sfree (statemachine->transitions);
  sfree (statemachine);
}

void
nict_load_fsm ()
{
  transition_t *transition;

  nict_fsm = (statemachine_t *) smalloc (sizeof (statemachine_t));
  nict_fsm->transitions = (list_t *) smalloc (sizeof (list_t));
  list_init (nict_fsm->transitions);

  /* to avoid race conditions between timers and first request */
  transition = (transition_t *) smalloc (sizeof (transition_t));
  transition->state = NICT_PRE_TRYING;
  transition->type = SND_REQUEST;
  transition->method = (void (*)(void *, void *)) &nict_snd_request;
  list_add (nict_fsm->transitions, transition, -1);
  /*
     transition         = (transition_t *) smalloc(sizeof(transition_t));
     transition->state  = NICT_TRYING;
     transition->type   = SND_REQUEST;
     transition->method = (void(*)(void *,void *))&nict_snd_request;
     list_add(nict_fsm->transitions,transition,-1);
   */
  transition = (transition_t *) smalloc (sizeof (transition_t));
  transition->state = NICT_TRYING;
  transition->type = TIMEOUT_F;
  transition->method = (void (*)(void *, void *)) &nict_timeout_f_event;
  list_add (nict_fsm->transitions, transition, -1);

  transition = (transition_t *) smalloc (sizeof (transition_t));
  transition->state = NICT_TRYING;
  transition->type = TIMEOUT_E;
  transition->method = (void (*)(void *, void *)) &nict_timeout_e_event;
  list_add (nict_fsm->transitions, transition, -1);

  transition = (transition_t *) smalloc (sizeof (transition_t));
  transition->state = NICT_TRYING;
  transition->type = RCV_STATUS_1XX;
  transition->method = (void (*)(void *, void *)) &nict_rcv_1xx;
  list_add (nict_fsm->transitions, transition, -1);

  transition = (transition_t *) smalloc (sizeof (transition_t));
  transition->state = NICT_TRYING;
  transition->type = RCV_STATUS_2XX;
  transition->method = (void (*)(void *, void *)) &nict_rcv_23456xx;
  list_add (nict_fsm->transitions, transition, -1);

  transition = (transition_t *) smalloc (sizeof (transition_t));
  transition->state = NICT_TRYING;
  transition->type = RCV_STATUS_3456XX;
  transition->method = (void (*)(void *, void *)) &nict_rcv_23456xx;
  list_add (nict_fsm->transitions, transition, -1);

  transition = (transition_t *) smalloc (sizeof (transition_t));
  transition->state = NICT_PROCEEDING;
  transition->type = TIMEOUT_F;
  transition->method = (void (*)(void *, void *)) &nict_timeout_f_event;
  list_add (nict_fsm->transitions, transition, -1);

  transition = (transition_t *) smalloc (sizeof (transition_t));
  transition->state = NICT_PROCEEDING;
  transition->type = TIMEOUT_E;
  transition->method = (void (*)(void *, void *)) &nict_timeout_e_event;
  list_add (nict_fsm->transitions, transition, -1);

  transition = (transition_t *) smalloc (sizeof (transition_t));
  transition->state = NICT_PROCEEDING;
  transition->type = RCV_STATUS_1XX;
  transition->method = (void (*)(void *, void *)) &nict_rcv_1xx;
  list_add (nict_fsm->transitions, transition, -1);

  transition = (transition_t *) smalloc (sizeof (transition_t));
  transition->state = NICT_PROCEEDING;
  transition->type = RCV_STATUS_2XX;
  transition->method = (void (*)(void *, void *)) &nict_rcv_23456xx;
  list_add (nict_fsm->transitions, transition, -1);

  transition = (transition_t *) smalloc (sizeof (transition_t));
  transition->state = NICT_PROCEEDING;
  transition->type = RCV_STATUS_3456XX;
  transition->method = (void (*)(void *, void *)) &nict_rcv_23456xx;
  list_add (nict_fsm->transitions, transition, -1);

  transition = (transition_t *) smalloc (sizeof (transition_t));
  transition->state = NICT_COMPLETED;
  transition->type = TIMEOUT_K;
  transition->method = (void (*)(void *, void *)) &nict_timeout_k_event;
  list_add (nict_fsm->transitions, transition, -1);

  /* these ccan be used to announce retransmission of 2xx and 3456xx
     For the state machine, it is completely useless...
     transition         = (transition_t *) smalloc(sizeof(transition_t));
     transition->state  = NICT_COMPLETED;
     transition->type   = RCV_STATUS_2XX;
     transition->method = (void(*)(void *,void *))&nict_rcv_23456xx2;
     list_add(nict_fsm->transitions,transition,-1);

     transition         = (transition_t *) smalloc(sizeof(transition_t));
     transition->state  = NICT_COMPLETED;
     transition->type   = RCV_STATUS_3456XX;
     transition->method = (void(*)(void *,void *))&nict_rcv_23456xx2;
     list_add(nict_fsm->transitions,transition,-1);
   */

}


void
nict_snd_request (transaction_t * nict, sipevent_t * evt)
{
  int i;
  osip_t *osip = (osip_t *) nict->config;

  /* Here we have ict->orig_request == NULL */
  nict->orig_request = evt->sip;

  i = osip->cb_send_message (nict, evt->sip, nict->nict_context->destination,
			     nict->nict_context->port, nict->out_socket);

  if (i == 0)
    {
      /* invoke the right callback! */
      if (MSG_IS_REGISTER (evt->sip))
	{
	  if (osip->cb_nict_register_sent != NULL)
	    osip->cb_nict_register_sent (nict, nict->orig_request);
	}
      else if (MSG_IS_BYE (evt->sip))
	{
	  if (osip->cb_nict_bye_sent != NULL)
	    osip->cb_nict_bye_sent (nict, nict->orig_request);
	}
      else if (MSG_IS_OPTIONS (evt->sip))
	{
	  if (osip->cb_nict_options_sent != NULL)
	    osip->cb_nict_options_sent (nict, nict->orig_request);
	}
      else if (MSG_IS_INFO (evt->sip))
	{
	  if (osip->cb_nict_info_sent != NULL)
	    osip->cb_nict_info_sent (nict, nict->orig_request);
	}
      else if (MSG_IS_CANCEL (evt->sip))
	{
	  if (osip->cb_nict_cancel_sent != NULL)
	    osip->cb_nict_cancel_sent (nict, nict->orig_request);
	}
      else if (MSG_IS_NOTIFY (evt->sip))
	{
	  if (osip->cb_nict_notify_sent != NULL)
	    osip->cb_nict_notify_sent (nict, nict->orig_request);
	}
      else if (MSG_IS_SUBSCRIBE (evt->sip))
	{
	  if (osip->cb_nict_subscribe_sent != NULL)
	    osip->cb_nict_subscribe_sent (nict, nict->orig_request);
	}
      else
	{
	  if (osip->cb_nict_unknown_sent != NULL)
	    osip->cb_nict_unknown_sent (nict, nict->orig_request);
	}

      transaction_set_state (nict, NICT_TRYING);
    }
  else
    {
      osip->cb_nict_transport_error (nict, i);
      transaction_set_state (nict, NICT_TERMINATED);
      osip->cb_nict_kill_transaction (nict);
      /* TODO: MUST BE DELETED NOW */
    }
}

void
nict_timeout_e_event (transaction_t * nict, sipevent_t * evt)
{
  osip_t *osip = (osip_t *) nict->config;
  time_t now = time (NULL);
  int i;

  /* reset timer */
  if (nict->state == NICT_TRYING)
    {
      nict->nict_context->timer_e_length =
	nict->nict_context->timer_e_length * 2;
      if (nict->nict_context->timer_e_length > 4000)
	nict->nict_context->timer_e_length = 4000;
    }
  else				/* in PROCEEDING STATE, TIMER is always 4000 */
    nict->nict_context->timer_e_length = 4000;

  nict->nict_context->timer_e_start = now;

  /* retransmit REQUEST */
  i = osip->cb_send_message (nict, nict->orig_request,
			     nict->nict_context->destination,
			     nict->nict_context->port, nict->out_socket);
  if (i != 0)
    {
      osip->cb_nict_transport_error (nict, i);
      transaction_set_state (nict, NICT_TERMINATED);
      osip->cb_nict_kill_transaction (nict);
      /* TODO: MUST BE DELETED NOW */
      return;
    }
  if (osip->cb_nict_request_sent2 != NULL)
    osip->cb_nict_request_sent2 (nict, nict->orig_request);
}

void
nict_timeout_f_event (transaction_t * nict, sipevent_t * evt)
{
  osip_t *osip = (osip_t *) nict->config;

  nict->nict_context->timer_f_length = -1;
  nict->nict_context->timer_f_start = -1;

  transaction_set_state (nict, NICT_TERMINATED);
  osip->cb_nict_kill_transaction (nict);
}

void
nict_timeout_k_event (transaction_t * nict, sipevent_t * evt)
{
  osip_t *osip = (osip_t *) nict->config;

  nict->nict_context->timer_k_length = -1;
  nict->nict_context->timer_k_start = -1;

  transaction_set_state (nict, NICT_TERMINATED);
  osip->cb_nict_kill_transaction (nict);
}

void
nict_rcv_1xx (transaction_t * nict, sipevent_t * evt)
{
  osip_t *osip = (osip_t *) nict->config;

  /* leave this answer to the core application */

  if (nict->last_response != NULL)
    {
      msg_free (nict->last_response);
      sfree (nict->last_response);
    }
  nict->last_response = evt->sip;
  if (osip->cb_nict_1xx_received != NULL)
    osip->cb_nict_1xx_received (nict, evt->sip);
  transaction_set_state (nict, NICT_PROCEEDING);
}

void
nict_rcv_23456xx (transaction_t * nict, sipevent_t * evt)
{
  osip_t *osip = (osip_t *) nict->config;

  /* leave this answer to the core application */

  if (nict->last_response != NULL)
    {
      msg_free (nict->last_response);
      sfree (nict->last_response);
    }
  nict->last_response = evt->sip;

  if (EVT_IS_RCV_STATUS_2XX (evt))
    osip->cb_nict_2xx_received (nict, nict->last_response);
  else
    {
      if (MSG_IS_STATUS_3XX (nict->last_response))
	osip->cb_nict_3xx_received (nict, nict->last_response);
      else if (MSG_IS_STATUS_4XX (nict->last_response))
	osip->cb_nict_4xx_received (nict, nict->last_response);
      else if (MSG_IS_STATUS_5XX (nict->last_response))
	osip->cb_nict_5xx_received (nict, nict->last_response);
      else
	osip->cb_nict_6xx_received (nict, nict->last_response);
    }

  if (nict->state != NICT_COMPLETED)	/* reset timer K */
    nict->nict_context->timer_k_start = time (NULL);
  transaction_set_state (nict, NICT_COMPLETED);
}

/* See Rq in the state machine definition (above)
void
nict_rcv_23456xx2(transaction_t *nict, sipevent_t *evt)
{
  osip_t *osip = (osip_t*)nict->config;

  if (nict->last_response!=NULL)
    {
      msg_free(nict->last_response);
      sfree(nict->last_response);
    }
  nict->last_response = evt->sip;

  if (EVT_IS_RCV_STATUS_2XX(evt))
    {
      if (osip->cb_nict_2xx_received2!=NULL)
	osip->cb_nict_2xx_received2(nict, evt->sip);
    }
  else
    {
      if (osip->cb_nict_3456xx_received2!=NULL)
	osip->cb_nict_3456xx_received2(nict, evt->sip);
    }
}
*/