1
10
13
14
20
21
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
245
246
247
248
250
251
252
253
254
255
256
257
261
268
269
270
271
272
273
274
275
276
277
278
280
281
282
283
284
285
286
287
289
290
294
301
302
303
304
305
306
307
308
309
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
332
333
334
335
336
337
339
345
346
347
353
354
355
361
362
363
369
370
371
372
373
374
375
376
...
...
...
#define NX_SOURCE_CODE
#include "nx_api.h"
#include "nx_ip.h"
#include "nx_tcp.h"
#include "tx_thread.h"
#include "nx_ipv6.h"
5 includes
...
...
UINT _nx_tcp_socket_disconnect(NX_TCP_SOCKET *socket_ptr, ULONG wait_option)
{
#ifndef NX_DISABLE_RESET_DISCONNECT
NX_TCP_HEADER tcp_header;
#endif
#ifdef NX_ENABLE_TCPIP_OFFLOAD
NX_INTERFACE *interface_ptr;
#endif
UINT status;
NX_IP *ip_ptr;
ip_ptr = socket_ptr -> nx_tcp_socket_ip_ptr;
NX_TRACE_IN_LINE_INSERT(NX_TRACE_TCP_SOCKET_DISCONNECT, ip_ptr, socket_ptr, wait_option, socket_ptr -> nx_tcp_socket_state, NX_TRACE_TCP_EVENTS, 0, 0);
status = NX_SUCCESS;
tx_mutex_get(&(ip_ptr -> nx_ip_protection), TX_WAIT_FOREVER);
#ifndef NX_DISABLE_TCP_INFO
ip_ptr -> nx_ip_tcp_disconnections++;/* ... */
#endif
if ((socket_ptr -> nx_tcp_socket_state != NX_TCP_ESTABLISHED) &&
(socket_ptr -> nx_tcp_socket_state != NX_TCP_SYN_SENT) &&
(socket_ptr -> nx_tcp_socket_state != NX_TCP_SYN_RECEIVED) &&
(socket_ptr -> nx_tcp_socket_state != NX_TCP_CLOSE_WAIT))
{
tx_mutex_put(&(ip_ptr -> nx_ip_protection));
return(NX_NOT_CONNECTED);
}if ((socket_ptr -> nx_tcp_socket_state != NX_TCP_ESTABLISHED) && (socket_ptr -> nx_tcp_socket_state != NX_TCP_SYN_SENT) && (socket_ptr -> nx_tcp_socket_state != NX_TCP_SYN_RECEIVED) && (socket_ptr -> nx_tcp_socket_state != NX_TCP_CLOSE_WAIT)) { ... }
#ifdef NX_ENABLE_TCP_KEEPALIVE
/* ... */
socket_ptr -> nx_tcp_socket_keepalive_timeout = 0;
socket_ptr -> nx_tcp_socket_keepalive_retries = 0;/* ... */
#endif
#ifdef NX_ENABLE_TCPIP_OFFLOAD
interface_ptr = socket_ptr -> nx_tcp_socket_connect_interface;
if (interface_ptr &&
(interface_ptr -> nx_interface_capability_flag & NX_INTERFACE_CAPABILITY_TCPIP_OFFLOAD) &&
(interface_ptr -> nx_interface_tcpip_offload_handler))
{
interface_ptr -> nx_interface_tcpip_offload_handler(ip_ptr, interface_ptr, socket_ptr,
NX_TCPIP_OFFLOAD_TCP_SOCKET_DISCONNECT,
NX_NULL, NX_NULL, NX_NULL, 0, NX_NULL,
wait_option);
socket_ptr -> nx_tcp_socket_state = NX_TCP_CLOSED;
}if (interface_ptr && (interface_ptr -> nx_interface_capability_flag & NX_INTERFACE_CAPABILITY_TCPIP_OFFLOAD) && (interface_ptr -> nx_interface_tcpip_offload_handler)) { ... }
else/* ... */
#endif
if ((socket_ptr -> nx_tcp_socket_state == NX_TCP_SYN_SENT) ||
(socket_ptr -> nx_tcp_socket_state == NX_TCP_SYN_RECEIVED))
{
if (socket_ptr -> nx_tcp_socket_client_type)
{
NX_TRACE_IN_LINE_INSERT(NX_TRACE_INTERNAL_TCP_STATE_CHANGE, ip_ptr, socket_ptr, socket_ptr -> nx_tcp_socket_state, NX_TCP_CLOSED, NX_TRACE_INTERNAL_EVENTS, 0, 0);
if (socket_ptr -> nx_tcp_socket_state == NX_TCP_SYN_RECEIVED)
{
socket_ptr -> nx_tcp_socket_timeout = socket_ptr -> nx_tcp_socket_timeout_rate;
socket_ptr -> nx_tcp_socket_timeout_retries = 0;
socket_ptr -> nx_tcp_socket_tx_sequence++;
_nx_tcp_packet_send_fin(socket_ptr, (socket_ptr -> nx_tcp_socket_tx_sequence - 1));
}if (socket_ptr -> nx_tcp_socket_state == NX_TCP_SYN_RECEIVED) { ... }
socket_ptr -> nx_tcp_socket_state = NX_TCP_CLOSED;
}if (socket_ptr -> nx_tcp_socket_client_type) { ... }
else
{
NX_TRACE_IN_LINE_INSERT(NX_TRACE_INTERNAL_TCP_STATE_CHANGE, ip_ptr, socket_ptr, socket_ptr -> nx_tcp_socket_state, NX_TCP_LISTEN_STATE, NX_TRACE_INTERNAL_EVENTS, 0, 0);
if ((socket_ptr -> nx_tcp_socket_state == NX_TCP_SYN_RECEIVED) &&
(socket_ptr -> nx_tcp_socket_connect_interface != NX_NULL))
{
socket_ptr -> nx_tcp_socket_timeout = socket_ptr -> nx_tcp_socket_timeout_rate;
socket_ptr -> nx_tcp_socket_timeout_retries = 0;
socket_ptr -> nx_tcp_socket_tx_sequence++;
_nx_tcp_packet_send_fin(socket_ptr, (socket_ptr -> nx_tcp_socket_tx_sequence - 1));
}if ((socket_ptr -> nx_tcp_socket_state == NX_TCP_SYN_RECEIVED) && (socket_ptr -> nx_tcp_socket_connect_interface != NX_NULL)) { ... }
socket_ptr -> nx_tcp_socket_state = NX_TCP_LISTEN_STATE;
socket_ptr -> nx_tcp_socket_rx_sequence--;
}else { ... }
socket_ptr -> nx_tcp_socket_timeout = 0;
}else if ((socket_ptr -> nx_tcp_socket_state == NX_TCP_SYN_SENT) || (socket_ptr -> nx_tcp_socket_state == NX_TCP_SYN_RECEIVED)) { ... }
#ifndef NX_DISABLE_RESET_DISCONNECT
/* ... */
else if (wait_option == NX_NO_WAIT)
{
tcp_header.nx_tcp_header_word_3 = NX_TCP_ACK_BIT;
tcp_header.nx_tcp_acknowledgment_number = socket_ptr -> nx_tcp_socket_tx_sequence;
tcp_header.nx_tcp_sequence_number = socket_ptr -> nx_tcp_socket_rx_sequence;
_nx_tcp_packet_send_rst(socket_ptr, &tcp_header);
_nx_tcp_socket_block_cleanup(socket_ptr);
status = NX_IN_PROGRESS;
}else if (wait_option == NX_NO_WAIT) { ... }
/* ... */#endif
/* ... */
else if (socket_ptr -> nx_tcp_socket_state != NX_TCP_CLOSE_WAIT)
{
/* ... */
NX_TRACE_IN_LINE_INSERT(NX_TRACE_INTERNAL_TCP_STATE_CHANGE, ip_ptr, socket_ptr, socket_ptr -> nx_tcp_socket_state, NX_TCP_FIN_WAIT_1, NX_TRACE_INTERNAL_EVENTS, 0, 0);
socket_ptr -> nx_tcp_socket_state = NX_TCP_FIN_WAIT_1;
/* ... */
if (socket_ptr -> nx_tcp_socket_transmit_sent_head == NX_NULL)
{
socket_ptr -> nx_tcp_socket_timeout = socket_ptr -> nx_tcp_socket_timeout_rate;
socket_ptr -> nx_tcp_socket_timeout_retries = 0;
}if (socket_ptr -> nx_tcp_socket_transmit_sent_head == NX_NULL) { ... }
socket_ptr -> nx_tcp_socket_tx_sequence++;
_nx_tcp_packet_send_fin(socket_ptr, (socket_ptr -> nx_tcp_socket_tx_sequence - 1));
}else if (socket_ptr -> nx_tcp_socket_state != NX_TCP_CLOSE_WAIT) { ... }
else
{
/* ... */
NX_TRACE_IN_LINE_INSERT(NX_TRACE_INTERNAL_TCP_STATE_CHANGE, ip_ptr, socket_ptr, socket_ptr -> nx_tcp_socket_state, NX_TCP_LAST_ACK, NX_TRACE_INTERNAL_EVENTS, 0, 0);
socket_ptr -> nx_tcp_socket_state = NX_TCP_LAST_ACK;
/* ... */
/* ... */
if (socket_ptr -> nx_tcp_socket_transmit_sent_head == NX_NULL)
{
socket_ptr -> nx_tcp_socket_timeout = socket_ptr -> nx_tcp_socket_timeout_rate;
socket_ptr -> nx_tcp_socket_timeout_retries = 0;
}if (socket_ptr -> nx_tcp_socket_transmit_sent_head == NX_NULL) { ... }
socket_ptr -> nx_tcp_socket_tx_sequence++;
_nx_tcp_packet_send_fin(socket_ptr, (socket_ptr -> nx_tcp_socket_tx_sequence - 1));
}else { ... }
/* ... */
if ((socket_ptr -> nx_tcp_socket_state != NX_TCP_CLOSED) && (socket_ptr -> nx_tcp_socket_state != NX_TCP_LISTEN_STATE) &&
#ifdef NX_DISABLE_RESET_DISCONNECT
(wait_option) &&
#endif
(_tx_thread_current_ptr != &(ip_ptr -> nx_ip_thread)))
{
_nx_tcp_socket_thread_suspend(&(socket_ptr -> nx_tcp_socket_disconnect_suspended_thread), _nx_tcp_disconnect_cleanup, socket_ptr, &(ip_ptr -> nx_ip_protection), wait_option);
tx_mutex_get(&(ip_ptr -> nx_ip_protection), TX_WAIT_FOREVER);
if (socket_ptr -> nx_tcp_socket_state != NX_TCP_TIMED_WAIT)
{
_nx_tcp_socket_block_cleanup(socket_ptr);
}if (socket_ptr -> nx_tcp_socket_state != NX_TCP_TIMED_WAIT) { ... }
status = _tx_thread_current_ptr -> tx_thread_suspend_status;
}if ((socket_ptr -> nx_tcp_socket_state != NX_TCP_CLOSED) && (socket_ptr -> nx_tcp_socket_state != NX_TCP_LISTEN_STATE) && #ifdef NX_DISABLE_RESET_DISCONNECT (wait_option) && #endif (_tx_thread_current_ptr != &(ip_ptr -> nx_ip_thread))) { ... }
/* ... */
if (socket_ptr -> nx_tcp_socket_transmit_sent_head)
{
_nx_tcp_socket_transmit_queue_flush(socket_ptr);
}if (socket_ptr -> nx_tcp_socket_transmit_sent_head) { ... }
if (socket_ptr -> nx_tcp_socket_connect_suspended_thread)
{
_nx_tcp_connect_cleanup(socket_ptr -> nx_tcp_socket_connect_suspended_thread NX_CLEANUP_ARGUMENT);
}if (socket_ptr -> nx_tcp_socket_connect_suspended_thread) { ... }
while (socket_ptr -> nx_tcp_socket_receive_suspension_list)
{
_nx_tcp_receive_cleanup(socket_ptr -> nx_tcp_socket_receive_suspension_list NX_CLEANUP_ARGUMENT);
}while (socket_ptr -> nx_tcp_socket_receive_suspension_list) { ... }
while (socket_ptr -> nx_tcp_socket_transmit_suspension_list)
{
_nx_tcp_transmit_cleanup(socket_ptr -> nx_tcp_socket_transmit_suspension_list NX_CLEANUP_ARGUMENT);
}while (socket_ptr -> nx_tcp_socket_transmit_suspension_list) { ... }
tx_mutex_put(&(ip_ptr -> nx_ip_protection));
return(status);
}{ ... }