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
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
|
/*
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 nict_transition[12] = {
{NICT_PRE_TRYING, SND_REQUEST, (void (*)(void *, void *)) & nict_snd_request, &nict_transition[1], NULL}, {NICT_TRYING, TIMEOUT_F, (void (*)(void *, void *)) & osip_nict_timeout_f_event, &nict_transition[2], NULL},
{NICT_TRYING, TIMEOUT_E, (void (*)(void *, void *)) & osip_nict_timeout_e_event, &nict_transition[3], NULL}, {NICT_TRYING, RCV_STATUS_1XX, (void (*)(void *, void *)) & nict_rcv_1xx, &nict_transition[4], NULL},
{NICT_TRYING, RCV_STATUS_2XX, (void (*)(void *, void *)) & nict_rcv_23456xx, &nict_transition[5], NULL}, {NICT_TRYING, RCV_STATUS_3456XX, (void (*)(void *, void *)) & nict_rcv_23456xx, &nict_transition[6], NULL},
{NICT_PROCEEDING, TIMEOUT_F, (void (*)(void *, void *)) & osip_nict_timeout_f_event, &nict_transition[7], NULL}, {NICT_PROCEEDING, TIMEOUT_E, (void (*)(void *, void *)) & osip_nict_timeout_e_event, &nict_transition[8], NULL},
{NICT_PROCEEDING, RCV_STATUS_1XX, (void (*)(void *, void *)) & nict_rcv_1xx, &nict_transition[9], NULL}, {NICT_PROCEEDING, RCV_STATUS_2XX, (void (*)(void *, void *)) & nict_rcv_23456xx, &nict_transition[10], NULL},
{NICT_PROCEEDING, RCV_STATUS_3456XX, (void (*)(void *, void *)) & nict_rcv_23456xx, &nict_transition[11], NULL}, {NICT_COMPLETED, TIMEOUT_K, (void (*)(void *, void *)) & osip_nict_timeout_k_event, NULL, NULL}};
osip_statemachine_t nict_fsm = {nict_transition};
static void nict_handle_transport_error(osip_transaction_t *nict, int err) {
__osip_transport_error_callback(OSIP_NICT_TRANSPORT_ERROR, nict, err);
__osip_transaction_set_state(nict, NICT_TERMINATED);
__osip_kill_transaction_callback(OSIP_NICT_KILL_TRANSACTION, nict);
/* TODO: MUST BE DELETED NOW */
}
void nict_snd_request(osip_transaction_t *nict, osip_event_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))
__osip_message_callback(OSIP_NICT_REGISTER_SENT, nict, nict->orig_request);
else if (MSG_IS_BYE(evt->sip))
__osip_message_callback(OSIP_NICT_BYE_SENT, nict, nict->orig_request);
else if (MSG_IS_OPTIONS(evt->sip))
__osip_message_callback(OSIP_NICT_OPTIONS_SENT, nict, nict->orig_request);
else if (MSG_IS_INFO(evt->sip))
__osip_message_callback(OSIP_NICT_INFO_SENT, nict, nict->orig_request);
else if (MSG_IS_CANCEL(evt->sip))
__osip_message_callback(OSIP_NICT_CANCEL_SENT, nict, nict->orig_request);
else if (MSG_IS_NOTIFY(evt->sip))
__osip_message_callback(OSIP_NICT_NOTIFY_SENT, nict, nict->orig_request);
else if (MSG_IS_SUBSCRIBE(evt->sip))
__osip_message_callback(OSIP_NICT_SUBSCRIBE_SENT, nict, nict->orig_request);
else
__osip_message_callback(OSIP_NICT_UNKNOWN_REQUEST_SENT, nict, nict->orig_request);
#ifndef USE_BLOCKINGSOCKET
/*
stop timer E in reliable transport - non blocking socket:
the message was just sent
*/
{
osip_via_t *via;
char *proto;
int k;
k = osip_message_get_via(nict->orig_request, 0, &via); /* get top via */
if (k < 0) {
nict_handle_transport_error(nict, -1);
return;
}
proto = via_get_protocol(via);
if (proto == NULL) {
nict_handle_transport_error(nict, -1);
return;
}
if (i == 0) { /* but message was really sent */
if (osip_strcasecmp(proto, "TCP") != 0 && osip_strcasecmp(proto, "TLS") != 0 && osip_strcasecmp(proto, "SCTP") != 0) {
} else { /* reliable protocol is used: */
nict->nict_context->timer_e_length = -1; /* E is not ACTIVE */
nict->nict_context->timer_e_start.tv_sec = -1;
}
} else {
if (osip_strcasecmp(proto, "TCP") != 0 && osip_strcasecmp(proto, "TLS") != 0 && osip_strcasecmp(proto, "SCTP") != 0) {
} else { /* reliable protocol is used: */
nict->nict_context->timer_e_length = DEFAULT_T1_TCP_PROGRESS;
}
}
}
#endif
if (nict->nict_context->timer_e_length > 0) {
osip_gettimeofday(&nict->nict_context->timer_e_start, NULL);
add_gettimeofday(&nict->nict_context->timer_e_start, nict->nict_context->timer_e_length);
}
__osip_transaction_set_state(nict, NICT_TRYING);
} else {
nict_handle_transport_error(nict, i);
}
}
void osip_nict_timeout_e_event(osip_transaction_t *nict, osip_event_t *evt) {
osip_t *osip = (osip_t *) nict->config;
int i;
/* reset timer */
if (nict->state == NICT_TRYING) {
struct timeval now;
struct timeval duration;
long elapsed;
osip_gettimeofday(&now, NULL);
osip_timersub(&now, &nict->created_time, &duration);
elapsed = duration.tv_sec * 1000 + duration.tv_usec / 1000;
if (elapsed < DEFAULT_T1)
nict->nict_context->timer_e_length = DEFAULT_T1;
else if (elapsed < 2 * DEFAULT_T1)
nict->nict_context->timer_e_length = 2 * DEFAULT_T1;
else if (elapsed < 4 * DEFAULT_T1)
nict->nict_context->timer_e_length = 4 * DEFAULT_T1;
else
nict->nict_context->timer_e_length = nict->nict_context->timer_e_length * 2;
if (nict->nict_context->timer_e_length > DEFAULT_T2)
nict->nict_context->timer_e_length = DEFAULT_T2;
} else /* in PROCEEDING STATE, TIMER is always DEFAULT_T2 */
nict->nict_context->timer_e_length = DEFAULT_T2;
osip_gettimeofday(&nict->nict_context->timer_e_start, NULL);
add_gettimeofday(&nict->nict_context->timer_e_start, nict->nict_context->timer_e_length);
/* 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) {
nict_handle_transport_error(nict, i);
return;
}
#ifndef USE_BLOCKINGSOCKET
/*
stop timer E in reliable transport - non blocking socket:
the message was just sent
*/
if (i == 0) { /* but message was really sent */
osip_via_t *via;
char *proto;
i = osip_message_get_via(nict->orig_request, 0, &via); /* get top via */
if (i < 0) {
nict_handle_transport_error(nict, -1);
return;
}
proto = via_get_protocol(via);
if (proto == NULL) {
nict_handle_transport_error(nict, -1);
return;
}
if (osip_strcasecmp(proto, "TCP") != 0 && osip_strcasecmp(proto, "TLS") != 0 && osip_strcasecmp(proto, "SCTP") != 0) {
} else { /* reliable protocol is used: */
nict->nict_context->timer_e_length = -1; /* E is not ACTIVE */
nict->nict_context->timer_e_start.tv_sec = -1;
}
}
#endif
if (i == 0) /* but message was really sent */
__osip_message_callback(OSIP_NICT_REQUEST_SENT_AGAIN, nict, nict->orig_request);
}
void osip_nict_timeout_f_event(osip_transaction_t *nict, osip_event_t *evt) {
nict->nict_context->timer_f_length = -1;
nict->nict_context->timer_f_start.tv_sec = -1;
if (nict->out_socket == -999) {
nict_handle_transport_error(nict, -1);
} else {
__osip_message_callback(OSIP_NICT_STATUS_TIMEOUT, nict, evt->sip);
__osip_transaction_set_state(nict, NICT_TERMINATED);
__osip_kill_transaction_callback(OSIP_NICT_KILL_TRANSACTION, nict);
}
}
void osip_nict_timeout_k_event(osip_transaction_t *nict, osip_event_t *evt) {
nict->nict_context->timer_k_length = -1;
nict->nict_context->timer_k_start.tv_sec = -1;
__osip_transaction_set_state(nict, NICT_TERMINATED);
__osip_kill_transaction_callback(OSIP_NICT_KILL_TRANSACTION, nict);
}
void nict_rcv_1xx(osip_transaction_t *nict, osip_event_t *evt) {
/* leave this answer to the core application */
if (nict->last_response != NULL) {
osip_message_free(nict->last_response);
}
nict->last_response = evt->sip;
/* for unreliable transport increase the retransmission timeout */
if (nict->nict_context->timer_e_length > 0) {
nict->nict_context->timer_e_length = DEFAULT_T2;
osip_gettimeofday(&nict->nict_context->timer_e_start, NULL);
add_gettimeofday(&nict->nict_context->timer_e_start, nict->nict_context->timer_e_length);
}
__osip_message_callback(OSIP_NICT_STATUS_1XX_RECEIVED, nict, evt->sip);
__osip_transaction_set_state(nict, NICT_PROCEEDING);
}
void nict_rcv_23456xx(osip_transaction_t *nict, osip_event_t *evt) {
/* leave this answer to the core application */
if (nict->last_response != NULL) {
osip_message_free(nict->last_response);
}
nict->last_response = evt->sip;
if (EVT_IS_RCV_STATUS_2XX(evt))
__osip_message_callback(OSIP_NICT_STATUS_2XX_RECEIVED, nict, nict->last_response);
else if (MSG_IS_STATUS_3XX(nict->last_response))
__osip_message_callback(OSIP_NICT_STATUS_3XX_RECEIVED, nict, nict->last_response);
else if (MSG_IS_STATUS_4XX(nict->last_response))
__osip_message_callback(OSIP_NICT_STATUS_4XX_RECEIVED, nict, nict->last_response);
else if (MSG_IS_STATUS_5XX(nict->last_response))
__osip_message_callback(OSIP_NICT_STATUS_5XX_RECEIVED, nict, nict->last_response);
else
__osip_message_callback(OSIP_NICT_STATUS_6XX_RECEIVED, nict, nict->last_response);
if (nict->state != NICT_COMPLETED) { /* reset timer K */
osip_gettimeofday(&nict->nict_context->timer_k_start, NULL);
add_gettimeofday(&nict->nict_context->timer_k_start, nict->nict_context->timer_k_length);
}
__osip_transaction_set_state(nict, NICT_COMPLETED);
}
|