1 /***************************************************************************
2 * Copyright (c) 2024 Microsoft Corporation
3 *
4 * This program and the accompanying materials are made available under the
5 * terms of the MIT License which is available at
6 * https://opensource.org/licenses/MIT.
7 *
8 * SPDX-License-Identifier: MIT
9 **************************************************************************/
10
11
12 /**************************************************************************/
13 /**************************************************************************/
14 /** */
15 /** NetX Component */
16 /** */
17 /** Transmission Control Protocol (TCP) */
18 /** */
19 /**************************************************************************/
20 /**************************************************************************/
21
22 #define NX_SOURCE_CODE
23
24
25 /* Include necessary system files. */
26
27 #include "nx_api.h"
28 #include "nx_ip.h"
29 #include "nx_tcp.h"
30
31
32 /**************************************************************************/
33 /* */
34 /* FUNCTION RELEASE */
35 /* */
36 /* _nx_tcp_socket_state_fin_wait2 PORTABLE C */
37 /* 6.1 */
38 /* AUTHOR */
39 /* */
40 /* Yuxin Zhou, Microsoft Corporation */
41 /* */
42 /* DESCRIPTION */
43 /* */
44 /* This function processes packets during the FIN WAIT 2 state, */
45 /* which is the state after the initial FIN was issued and the other */
46 /* side of the connection issued an ACK. If a FIN is received in */
47 /* this state, an ACK is sent back the disconnection is complete. */
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 /* */
61 /* CALLED BY */
62 /* */
63 /* _nx_tcp_socket_packet_process Process TCP packet for socket */
64 /* */
65 /* RELEASE HISTORY */
66 /* */
67 /* DATE NAME DESCRIPTION */
68 /* */
69 /* 05-19-2020 Yuxin Zhou Initial Version 6.0 */
70 /* 09-30-2020 Yuxin Zhou Modified comment(s), */
71 /* resulting in version 6.1 */
72 /* */
73 /**************************************************************************/
_nx_tcp_socket_state_fin_wait2(NX_TCP_SOCKET * socket_ptr)74 VOID _nx_tcp_socket_state_fin_wait2(NX_TCP_SOCKET *socket_ptr)
75 {
76
77
78 /* Determine if a FIN has been previously detected in the _nx_tcp_socket_state_data_check
79 routine and if the socket's sequence number matches the expected FIN sequence number. */
80 if ((socket_ptr -> nx_tcp_socket_fin_received) &&
81 (socket_ptr -> nx_tcp_socket_fin_sequence == socket_ptr -> nx_tcp_socket_rx_sequence))
82 {
83
84 /* Return to the proper socket state. */
85
86 /* Yes, return the socket to the TIMED WAIT state. */
87
88 /* If trace is enabled, insert this event into the trace buffer. */
89 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);
90
91 /* Set the socket state to TIMED WAIT now. */
92 socket_ptr -> nx_tcp_socket_state = NX_TCP_TIMED_WAIT;
93
94 /* Set the timeout as 2MSL (Maximum Segment Lifetime). */
95 socket_ptr -> nx_tcp_socket_timeout = _nx_tcp_2MSL_timer_rate;
96
97 /* Send ACK back to the other side of the connection. */
98
99 /* Increment the received sequence number. */
100 socket_ptr -> nx_tcp_socket_rx_sequence++;
101
102 /* Send ACK message. */
103 _nx_tcp_packet_send_ack(socket_ptr, socket_ptr -> nx_tcp_socket_tx_sequence);
104
105 /* Determine if we need to wake a thread suspended on the connection. */
106 if (socket_ptr -> nx_tcp_socket_disconnect_suspended_thread)
107 {
108
109 /* Resume the thread suspended for the disconnect. */
110 _nx_tcp_socket_thread_resume(&(socket_ptr -> nx_tcp_socket_disconnect_suspended_thread), NX_SUCCESS);
111 }
112
113 /* If given, call the application's disconnect callback function
114 for disconnect. */
115 if (socket_ptr -> nx_tcp_disconnect_callback)
116 {
117
118 /* Call the application's disconnect handling function. It is
119 responsible for calling the socket disconnect function. */
120 (socket_ptr -> nx_tcp_disconnect_callback)(socket_ptr);
121 }
122
123 #ifndef NX_DISABLE_EXTENDED_NOTIFY_SUPPORT
124
125 /* Is a timed wait callback registered for this socket? */
126 if (socket_ptr -> nx_tcp_timed_wait_callback)
127 {
128
129 /* Call the timed wait callback for this socket to let the host
130 know the socket can now be put in the timed wait state (if
131 the RE-USE ADDRESS socket option is not enabled). */
132 (socket_ptr -> nx_tcp_timed_wait_callback)(socket_ptr);
133 }
134
135 /* Is a disconnect complete callback registered with the TCP socket? */
136 if (socket_ptr -> nx_tcp_disconnect_complete_notify)
137 {
138
139 /* Call the application's disconnect_complete callback function. */
140 (socket_ptr -> nx_tcp_disconnect_complete_notify)(socket_ptr);
141 }
142 #endif
143 }
144 }
145
146