1 /**************************************************************************/
2 /* */
3 /* Copyright (c) Microsoft Corporation. All rights reserved. */
4 /* */
5 /* This software is licensed under the Microsoft Software License */
6 /* Terms for Microsoft Azure RTOS. Full text of the license can be */
7 /* found in the LICENSE file at https://aka.ms/AzureRTOS_EULA */
8 /* and in the root directory of this software. */
9 /* */
10 /**************************************************************************/
11
12
13 /**************************************************************************/
14 /**************************************************************************/
15 /** */
16 /** NetX Component */
17 /** */
18 /** Transmission Control Protocol (TCP) */
19 /** */
20 /**************************************************************************/
21 /**************************************************************************/
22
23 #define NX_SOURCE_CODE
24
25
26 /* Include necessary system files. */
27
28 #include "nx_api.h"
29 #include "tx_thread.h"
30 #include "tx_timer.h"
31 #include "nx_ip.h"
32 #include "nx_tcp.h"
33
34
35 /**************************************************************************/
36 /* */
37 /* FUNCTION RELEASE */
38 /* */
39 /* _nx_tcp_connect_cleanup PORTABLE C */
40 /* 6.1 */
41 /* AUTHOR */
42 /* */
43 /* Yuxin Zhou, Microsoft Corporation */
44 /* */
45 /* DESCRIPTION */
46 /* */
47 /* This function processes TCP connect timeout and thread terminate */
48 /* actions that require the TCP socket data structures to be cleaned */
49 /* up. */
50 /* */
51 /* INPUT */
52 /* */
53 /* thread_ptr Pointer to suspended thread's */
54 /* control block */
55 /* */
56 /* OUTPUT */
57 /* */
58 /* None */
59 /* */
60 /* CALLS */
61 /* */
62 /* tx_event_flags_set Set event flag */
63 /* _tx_thread_system_resume Resume thread service */
64 /* */
65 /* CALLED BY */
66 /* */
67 /* _nx_tcp_deferred_cleanup_check Deferred cleanup processing */
68 /* _nx_tcp_socket_connection_reset Socket reset processing */
69 /* _nx_tcp_socket_disconnect Socket disconnect processing */
70 /* _tx_thread_timeout Thread timeout processing */
71 /* _tx_thread_terminate Thread terminate processing */
72 /* */
73 /* RELEASE HISTORY */
74 /* */
75 /* DATE NAME DESCRIPTION */
76 /* */
77 /* 05-19-2020 Yuxin Zhou Initial Version 6.0 */
78 /* 09-30-2020 Yuxin Zhou Modified comment(s), */
79 /* resulting in version 6.1 */
80 /* */
81 /**************************************************************************/
_nx_tcp_connect_cleanup(TX_THREAD * thread_ptr NX_CLEANUP_PARAMETER)82 VOID _nx_tcp_connect_cleanup(TX_THREAD *thread_ptr NX_CLEANUP_PARAMETER)
83 {
84
85 TX_INTERRUPT_SAVE_AREA
86
87 NX_IP *ip_ptr;
88 NX_TCP_SOCKET *socket_ptr; /* Working socket pointer */
89
90 NX_CLEANUP_EXTENSION
91
92 /* Disable interrupts. */
93 TX_DISABLE
94
95 /* Setup pointer to TCP socket control block. */
96 socket_ptr = (NX_TCP_SOCKET *)thread_ptr -> tx_thread_suspend_control_block;
97
98 /* Determine if the socket pointer is valid. */
99 if ((!socket_ptr) || (socket_ptr -> nx_tcp_socket_id != NX_TCP_ID))
100 {
101
102 /* Restore interrupts. */
103 TX_RESTORE
104
105 return;
106 }
107
108 /* Determine if the cleanup is still required. */
109 if (!(thread_ptr -> tx_thread_suspend_cleanup))
110 {
111
112 /* Restore interrupts. */
113 TX_RESTORE
114
115 return;
116 }
117
118 /* Pickup the IP pointer. */
119 ip_ptr = socket_ptr -> nx_tcp_socket_ip_ptr;
120
121 /* Determine if the caller is an ISR or the system timer thread. */
122 #ifndef TX_TIMER_PROCESS_IN_ISR
123 if ((TX_THREAD_GET_SYSTEM_STATE()) || (_tx_thread_current_ptr == &_tx_timer_thread))
124 #else
125 if (TX_THREAD_GET_SYSTEM_STATE())
126 #endif
127 {
128
129 /* Yes, defer the processing to the NetX IP thread. */
130
131 /* Yes, change the suspend cleanup routine to indicate the cleanup is deferred. */
132 thread_ptr -> tx_thread_suspend_cleanup = _nx_tcp_cleanup_deferred;
133
134 /* Restore interrupts. */
135 TX_RESTORE
136
137 /* Set the deferred cleanup flag for the IP thread. */
138 tx_event_flags_set(&(ip_ptr -> nx_ip_events), NX_IP_TCP_CLEANUP_DEFERRED, TX_OR);
139
140 /* Return to caller. */
141 return;
142 }
143 else
144 {
145
146 /* Yes, we still have thread suspension! */
147
148 /* Clear the suspension cleanup flag. */
149 thread_ptr -> tx_thread_suspend_cleanup = TX_NULL;
150
151 /* Clear the suspension pointer. */
152 socket_ptr -> nx_tcp_socket_connect_suspended_thread = NX_NULL;
153
154 /* Clear the timeout. */
155 socket_ptr -> nx_tcp_socket_timeout = 0;
156
157 /* Return to the proper socket state. */
158 if (socket_ptr -> nx_tcp_socket_client_type)
159 {
160
161 /* If trace is enabled, insert this event into the trace buffer. */
162 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);
163
164 /* Client socket, return to a CLOSED state. */
165 socket_ptr -> nx_tcp_socket_state = NX_TCP_CLOSED;
166 }
167 else
168 {
169
170 /* If trace is enabled, insert this event into the trace buffer. */
171 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);
172
173 /* Server socket, return to LISTEN state. */
174 socket_ptr -> nx_tcp_socket_state = NX_TCP_LISTEN_STATE;
175
176 /* Move back the acknowledgment number just in case there is a retry. */
177 socket_ptr -> nx_tcp_socket_rx_sequence--;
178 }
179
180 /* Now we need to determine if this cleanup is from a terminate, timeout,
181 or from a wait abort. */
182 if (thread_ptr -> tx_thread_state == TX_TCP_IP)
183 {
184
185 /* Thread still suspended on the TCP socket. Setup return error status and
186 resume the thread. */
187
188 /* Setup return status. */
189 thread_ptr -> tx_thread_suspend_status = NX_NOT_CONNECTED;
190
191 /* Temporarily disable preemption. */
192 _tx_thread_preempt_disable++;
193
194 /* Restore interrupts. */
195 TX_RESTORE
196
197 /* Resume the thread! Check for preemption even though we are executing
198 from the system timer thread right now which normally executes at the
199 highest priority. */
200 _tx_thread_system_resume(thread_ptr);
201
202 /* Finished, just return. */
203 return;
204 }
205 }
206
207 /* Restore interrupts. */
208 TX_RESTORE
209 }
210
211