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 "nx_ip.h"
30 #include "nx_tcp.h"
31
32
33 /**************************************************************************/
34 /* */
35 /* FUNCTION RELEASE */
36 /* */
37 /* _nx_tcp_socket_state_fin_wait1 PORTABLE C */
38 /* 6.1 */
39 /* AUTHOR */
40 /* */
41 /* Yuxin Zhou, Microsoft Corporation */
42 /* */
43 /* DESCRIPTION */
44 /* */
45 /* This function processes packets during the FIN WAIT 1 state, */
46 /* which is the state after the initial FIN was issued in an active */
47 /* disconnect issued by the application. */
48 /* */
49 /* INPUT */
50 /* */
51 /* socket_ptr Pointer to owning socket */
52 /* */
53 /* OUTPUT */
54 /* */
55 /* None */
56 /* */
57 /* CALLS */
58 /* */
59 /* _nx_tcp_packet_send_ack Send ACK message */
60 /* _nx_tcp_socket_thread_resume Resume suspended thread */
61 /* */
62 /* CALLED BY */
63 /* */
64 /* _nx_tcp_socket_packet_process Process TCP packet for socket */
65 /* */
66 /* RELEASE HISTORY */
67 /* */
68 /* DATE NAME DESCRIPTION */
69 /* */
70 /* 05-19-2020 Yuxin Zhou Initial Version 6.0 */
71 /* 09-30-2020 Yuxin Zhou Modified comment(s), */
72 /* resulting in version 6.1 */
73 /* */
74 /**************************************************************************/
_nx_tcp_socket_state_fin_wait1(NX_TCP_SOCKET * socket_ptr)75 VOID _nx_tcp_socket_state_fin_wait1(NX_TCP_SOCKET *socket_ptr)
76 {
77
78
79 /* Determine if the peer has proper ACK number but FIN is not sent,
80 move into the FIN WAIT 2 state and do nothing else. */
81 if ((socket_ptr -> nx_tcp_socket_fin_acked) &&
82 (socket_ptr -> nx_tcp_socket_fin_received == NX_FALSE))
83 {
84
85 /* If trace is enabled, insert this event into the trace buffer. */
86 NX_TRACE_IN_LINE_INSERT(NX_TRACE_INTERNAL_TCP_STATE_CHANGE, socket_ptr -> nx_tcp_socket_ip_ptr, socket_ptr, socket_ptr -> nx_tcp_socket_state, NX_TCP_FIN_WAIT_2, NX_TRACE_INTERNAL_EVENTS, 0, 0);
87
88 /* We have a legitimate ACK message. Simply move into the WAIT FIN 2 state
89 for the other side to finish its processing and disconnect. */
90 socket_ptr -> nx_tcp_socket_state = NX_TCP_FIN_WAIT_2;
91
92 /* Otherwise, simply clear the FIN timeout. */
93 socket_ptr -> nx_tcp_socket_timeout = 0;
94 }
95 else if ((socket_ptr -> nx_tcp_socket_fin_acked) &&
96 (socket_ptr -> nx_tcp_socket_fin_sequence == socket_ptr -> nx_tcp_socket_rx_sequence))
97 {
98
99 /* Return to the proper socket state. */
100
101 /* Yes, return the socket to the TIMED WAIT state. */
102
103 /* If trace is enabled, insert this event into the trace buffer. */
104 NX_TRACE_IN_LINE_INSERT(NX_TRACE_INTERNAL_TCP_STATE_CHANGE, socket_ptr -> nx_tcp_socket_ip_ptr, socket_ptr, socket_ptr -> nx_tcp_socket_state, NX_TCP_TIMED_WAIT, NX_TRACE_INTERNAL_EVENTS, 0, 0);
105
106 /* Set the socket state to TIMED WAIT now. */
107 socket_ptr -> nx_tcp_socket_state = NX_TCP_TIMED_WAIT;
108
109 /* Set the timeout as 2MSL (Maximum Segment Lifetime). */
110 socket_ptr -> nx_tcp_socket_timeout = _nx_tcp_2MSL_timer_rate;
111
112 /* Send ACK back to the other side of the connection. */
113
114 /* Increment the received sequence number. */
115 socket_ptr -> nx_tcp_socket_rx_sequence++;
116
117 /* Send ACK message. */
118 _nx_tcp_packet_send_ack(socket_ptr, socket_ptr -> nx_tcp_socket_tx_sequence);
119
120 /* Determine if we need to wake a thread suspended on the connection. */
121 if (socket_ptr -> nx_tcp_socket_disconnect_suspended_thread)
122 {
123
124 /* Resume the thread suspended for the disconnect. */
125 _nx_tcp_socket_thread_resume(&(socket_ptr -> nx_tcp_socket_disconnect_suspended_thread), NX_SUCCESS);
126 }
127
128 /* If given, call the application's disconnect callback function
129 for disconnect. */
130 if (socket_ptr -> nx_tcp_disconnect_callback)
131 {
132
133 /* Call the application's disconnect handling function. It is
134 responsible for calling the socket disconnect function. */
135 (socket_ptr -> nx_tcp_disconnect_callback)(socket_ptr);
136 }
137
138 #ifndef NX_DISABLE_EXTENDED_NOTIFY_SUPPORT
139
140 /* Is a timed wait callback registered for this socket? */
141 if (socket_ptr -> nx_tcp_timed_wait_callback)
142 {
143
144 /* Call the timed wait callback for this socket to let the host
145 know the socket can now be put in the timed wait state (if
146 the RE-USE ADDRESS socket option is not enabled). */
147 (socket_ptr -> nx_tcp_timed_wait_callback)(socket_ptr);
148 }
149
150 /* Is a disconnect complete callback registered with the TCP socket? */
151 if (socket_ptr -> nx_tcp_disconnect_complete_notify)
152 {
153
154 /* Call the application's disconnect_complete callback function. */
155 (socket_ptr -> nx_tcp_disconnect_complete_notify)(socket_ptr);
156 }
157 #endif
158 }
159 else if ((socket_ptr -> nx_tcp_socket_fin_received) &&
160 (socket_ptr -> nx_tcp_socket_fin_sequence == socket_ptr -> nx_tcp_socket_rx_sequence))
161 {
162
163 /* If trace is enabled, insert this event into the trace buffer. */
164 NX_TRACE_IN_LINE_INSERT(NX_TRACE_INTERNAL_TCP_STATE_CHANGE, socket_ptr -> nx_tcp_socket_ip_ptr, socket_ptr, socket_ptr -> nx_tcp_socket_state, NX_TCP_CLOSING, NX_TRACE_INTERNAL_EVENTS, 0, 0);
165
166 /* Move to the CLOSING state for simultaneous close situation. */
167 socket_ptr -> nx_tcp_socket_state = NX_TCP_CLOSING;
168
169 /* Send ACK back to the other side of the connection. */
170
171 /* Increment the received sequence number. */
172 socket_ptr -> nx_tcp_socket_rx_sequence++;
173
174 /* Send ACK message. */
175 _nx_tcp_packet_send_ack(socket_ptr, socket_ptr -> nx_tcp_socket_tx_sequence);
176 }
177 }
178
179