Blame view

3rdparty/libosip2-5.3.0/src/osip2/ist_fsm.c 7.99 KB
73ef4ff3   Hu Chunming   提交三方库
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
  /*
    The oSIP library implements the Session Initiation Protocol (SIP -rfc3261-)
    Copyright (C) 2001-2020 Aymeric MOIZARD amoizard@antisip.com
  
    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 <osip2/internal.h>
  #include <osip2/osip.h>
  
  #include "fsm.h"
  
  transition_t ist_transition[11] = {{IST_PRE_PROCEEDING, RCV_REQINVITE, (void (*)(void *, void *)) & ist_rcv_invite, &ist_transition[1], NULL},
                                     {IST_PROCEEDING, RCV_REQINVITE, (void (*)(void *, void *)) & ist_rcv_invite, &ist_transition[2], NULL},
                                     {IST_COMPLETED, RCV_REQINVITE, (void (*)(void *, void *)) & ist_rcv_invite, &ist_transition[3], NULL},
                                     {IST_COMPLETED, TIMEOUT_G, (void (*)(void *, void *)) & osip_ist_timeout_g_event, &ist_transition[4], NULL},
                                     {IST_COMPLETED, TIMEOUT_H, (void (*)(void *, void *)) & osip_ist_timeout_h_event, &ist_transition[5], NULL},
                                     {IST_PROCEEDING, SND_STATUS_1XX, (void (*)(void *, void *)) & ist_snd_1xx, &ist_transition[6], NULL},
                                     {IST_PROCEEDING, SND_STATUS_2XX, (void (*)(void *, void *)) & ist_snd_2xx, &ist_transition[7], NULL},
                                     {IST_PROCEEDING, SND_STATUS_3456XX, (void (*)(void *, void *)) & ist_snd_3456xx, &ist_transition[8], NULL},
                                     {IST_COMPLETED, RCV_REQACK, (void (*)(void *, void *)) & ist_rcv_ack, &ist_transition[9], NULL},
                                     {IST_CONFIRMED, RCV_REQACK, (void (*)(void *, void *)) & ist_rcv_ack, &ist_transition[10], NULL},
                                     {IST_CONFIRMED, TIMEOUT_I, (void (*)(void *, void *)) & osip_ist_timeout_i_event, NULL, NULL}};
  
  osip_statemachine_t ist_fsm = {ist_transition};
  
  static void ist_handle_transport_error(osip_transaction_t *ist, int err) {
    __osip_transport_error_callback(OSIP_IST_TRANSPORT_ERROR, ist, err);
    __osip_transaction_set_state(ist, IST_TERMINATED);
    __osip_kill_transaction_callback(OSIP_IST_KILL_TRANSACTION, ist);
    /* TODO: MUST BE DELETED NOW */
  }
  
  void ist_rcv_invite(osip_transaction_t *ist, osip_event_t *evt) {
    int i;
  
    if (ist->state == IST_PRE_PROCEEDING) { /* announce new INVITE */
      /* Here we have ist->orig_request == NULL */
      ist->orig_request = evt->sip;
  
      __osip_message_callback(OSIP_IST_INVITE_RECEIVED, ist, evt->sip);
  
    } else { /* IST_PROCEEDING or IST_COMPLETED */
  
      /* delete retransmission */
      osip_message_free(evt->sip);
  
      __osip_message_callback(OSIP_IST_INVITE_RECEIVED_AGAIN, ist, ist->orig_request);
  
      if (ist->last_response != NULL) { /* retransmit last response */
        i = __osip_transaction_snd_xxx(ist, ist->last_response);
  
        if (i != 0) {
          ist_handle_transport_error(ist, i);
          return;
  
        } else {
          if (MSG_IS_STATUS_1XX(ist->last_response))
            __osip_message_callback(OSIP_IST_STATUS_1XX_SENT, ist, ist->last_response);
  
          else if (MSG_IS_STATUS_2XX(ist->last_response))
            __osip_message_callback(OSIP_IST_STATUS_2XX_SENT_AGAIN, ist, ist->last_response);
  
          else
            __osip_message_callback(OSIP_IST_STATUS_3456XX_SENT_AGAIN, ist, ist->last_response);
        }
      }
  
      return;
    }
  
    /* we come here only if it was the first INVITE received */
    __osip_transaction_set_state(ist, IST_PROCEEDING);
  }
  
  void osip_ist_timeout_g_event(osip_transaction_t *ist, osip_event_t *evt) {
    int i;
  
    ist->ist_context->timer_g_length = ist->ist_context->timer_g_length * 2;
  
    if (ist->ist_context->timer_g_length > DEFAULT_T2)
      ist->ist_context->timer_g_length = DEFAULT_T2;
  
    osip_gettimeofday(&ist->ist_context->timer_g_start, NULL);
    add_gettimeofday(&ist->ist_context->timer_g_start, ist->ist_context->timer_g_length);
  
    i = __osip_transaction_snd_xxx(ist, ist->last_response);
  
    if (i != 0) {
      ist_handle_transport_error(ist, i);
      return;
    }
  
    __osip_message_callback(OSIP_IST_STATUS_3456XX_SENT_AGAIN, ist, ist->last_response);
  }
  
  void osip_ist_timeout_h_event(osip_transaction_t *ist, osip_event_t *evt) {
    ist->ist_context->timer_h_length = -1;
    ist->ist_context->timer_h_start.tv_sec = -1;
  
    __osip_transaction_set_state(ist, IST_TERMINATED);
    __osip_kill_transaction_callback(OSIP_IST_KILL_TRANSACTION, ist);
  }
  
  void osip_ist_timeout_i_event(osip_transaction_t *ist, osip_event_t *evt) {
    ist->ist_context->timer_i_length = -1;
    ist->ist_context->timer_i_start.tv_sec = -1;
  
    __osip_transaction_set_state(ist, IST_TERMINATED);
    __osip_kill_transaction_callback(OSIP_IST_KILL_TRANSACTION, ist);
  }
  
  void ist_snd_1xx(osip_transaction_t *ist, osip_event_t *evt) {
    int i;
  
    if (ist->last_response != NULL) {
      osip_message_free(ist->last_response);
    }
  
    ist->last_response = evt->sip;
  
    i = __osip_transaction_snd_xxx(ist, evt->sip);
  
    if (i != 0) {
      ist_handle_transport_error(ist, i);
      return;
  
    } else
      __osip_message_callback(OSIP_IST_STATUS_1XX_SENT, ist, ist->last_response);
  
    /* we are already in the proper state */
    return;
  }
  
  void ist_snd_2xx(osip_transaction_t *ist, osip_event_t *evt) {
    int i;
  
    if (ist->last_response != NULL) {
      osip_message_free(ist->last_response);
    }
  
    ist->last_response = evt->sip;
  
    i = __osip_transaction_snd_xxx(ist, evt->sip);
  
    if (i != 0) {
      ist_handle_transport_error(ist, i);
      return;
  
    } else {
      __osip_message_callback(OSIP_IST_STATUS_2XX_SENT, ist, ist->last_response);
      __osip_transaction_set_state(ist, IST_TERMINATED);
      __osip_kill_transaction_callback(OSIP_IST_KILL_TRANSACTION, ist);
    }
  
    return;
  }
  
  void ist_snd_3456xx(osip_transaction_t *ist, osip_event_t *evt) {
    int i;
  
    if (ist->last_response != NULL) {
      osip_message_free(ist->last_response);
    }
  
    ist->last_response = evt->sip;
  
    i = __osip_transaction_snd_xxx(ist, evt->sip);
  
    if (i != 0) {
      ist_handle_transport_error(ist, i);
      return;
  
    } else {
      if (MSG_IS_STATUS_3XX(ist->last_response))
        __osip_message_callback(OSIP_IST_STATUS_3XX_SENT, ist, ist->last_response);
  
      else if (MSG_IS_STATUS_4XX(ist->last_response))
        __osip_message_callback(OSIP_IST_STATUS_4XX_SENT, ist, ist->last_response);
  
      else if (MSG_IS_STATUS_5XX(ist->last_response))
        __osip_message_callback(OSIP_IST_STATUS_5XX_SENT, ist, ist->last_response);
  
      else
        __osip_message_callback(OSIP_IST_STATUS_6XX_SENT, ist, ist->last_response);
    }
  
    if (ist->ist_context->timer_g_length != -1) {
      osip_gettimeofday(&ist->ist_context->timer_g_start, NULL);
      add_gettimeofday(&ist->ist_context->timer_g_start, ist->ist_context->timer_g_length);
    }
  
    osip_gettimeofday(&ist->ist_context->timer_h_start, NULL);
    add_gettimeofday(&ist->ist_context->timer_h_start, ist->ist_context->timer_h_length);
    __osip_transaction_set_state(ist, IST_COMPLETED);
    return;
  }
  
  void ist_rcv_ack(osip_transaction_t *ist, osip_event_t *evt) {
    if (ist->ack != NULL) {
      osip_message_free(ist->ack);
    }
  
    ist->ack = evt->sip;
  
    if (ist->state == IST_COMPLETED)
      __osip_message_callback(OSIP_IST_ACK_RECEIVED, ist, ist->ack);
  
    else /* IST_CONFIRMED */
      __osip_message_callback(OSIP_IST_ACK_RECEIVED_AGAIN, ist, ist->ack);
  
    /* set the timer to 0 for reliable, and T4 for unreliable (already set) */
    osip_gettimeofday(&ist->ist_context->timer_i_start, NULL);
    add_gettimeofday(&ist->ist_context->timer_i_start, ist->ist_context->timer_i_length);
    __osip_transaction_set_state(ist, IST_CONFIRMED);
  }