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_wait2                      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 2 state,        */
46 /*    which is the state after the initial FIN was issued and the other   */
47 /*    side of the connection issued an ACK.  If a FIN is received in      */
48 /*    this state, an ACK is sent back the disconnection is complete.      */
49 /*                                                                        */
50 /*  INPUT                                                                 */
51 /*                                                                        */
52 /*    socket_ptr                            Pointer to owning socket      */
53 /*                                                                        */
54 /*  OUTPUT                                                                */
55 /*                                                                        */
56 /*    None                                                                */
57 /*                                                                        */
58 /*  CALLS                                                                 */
59 /*                                                                        */
60 /*    _nx_tcp_packet_send_ack               Send ACK message              */
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_wait2(NX_TCP_SOCKET * socket_ptr)75 VOID  _nx_tcp_socket_state_fin_wait2(NX_TCP_SOCKET *socket_ptr)
76 {
77 
78 
79     /* Determine if a FIN has been previously detected in the _nx_tcp_socket_state_data_check
80        routine and if the socket's sequence number matches the expected FIN sequence number.  */
81     if ((socket_ptr -> nx_tcp_socket_fin_received) &&
82         (socket_ptr -> nx_tcp_socket_fin_sequence == socket_ptr -> nx_tcp_socket_rx_sequence))
83     {
84 
85         /* Return to the proper socket state.  */
86 
87         /* Yes, return the socket to the TIMED WAIT state. */
88 
89         /* If trace is enabled, insert this event into the trace buffer.  */
90         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);
91 
92         /* Set the socket state to TIMED WAIT now.  */
93         socket_ptr -> nx_tcp_socket_state = NX_TCP_TIMED_WAIT;
94 
95         /* Set the timeout as 2MSL (Maximum Segment Lifetime).  */
96         socket_ptr -> nx_tcp_socket_timeout = _nx_tcp_2MSL_timer_rate;
97 
98         /* Send ACK back to the other side of the connection.  */
99 
100         /* Increment the received sequence number.  */
101         socket_ptr -> nx_tcp_socket_rx_sequence++;
102 
103         /* Send ACK message.  */
104         _nx_tcp_packet_send_ack(socket_ptr, socket_ptr -> nx_tcp_socket_tx_sequence);
105 
106         /* Determine if we need to wake a thread suspended on the connection.  */
107         if (socket_ptr -> nx_tcp_socket_disconnect_suspended_thread)
108         {
109 
110             /* Resume the thread suspended for the disconnect.  */
111             _nx_tcp_socket_thread_resume(&(socket_ptr -> nx_tcp_socket_disconnect_suspended_thread), NX_SUCCESS);
112         }
113 
114         /* If given, call the application's disconnect callback function
115            for disconnect.  */
116         if (socket_ptr -> nx_tcp_disconnect_callback)
117         {
118 
119             /* Call the application's disconnect handling function.  It is
120                responsible for calling the socket disconnect function.  */
121             (socket_ptr -> nx_tcp_disconnect_callback)(socket_ptr);
122         }
123 
124 #ifndef NX_DISABLE_EXTENDED_NOTIFY_SUPPORT
125 
126         /* Is a timed wait callback registered for this socket?  */
127         if (socket_ptr -> nx_tcp_timed_wait_callback)
128         {
129 
130             /* Call the timed wait callback for this socket to let the host
131                know the socket can now be put in the timed wait state (if
132                the RE-USE ADDRESS socket option is not enabled). */
133             (socket_ptr -> nx_tcp_timed_wait_callback)(socket_ptr);
134         }
135 
136         /* Is a disconnect complete callback registered with the TCP socket? */
137         if (socket_ptr -> nx_tcp_disconnect_complete_notify)
138         {
139 
140             /* Call the application's disconnect_complete callback function.    */
141             (socket_ptr -> nx_tcp_disconnect_complete_notify)(socket_ptr);
142         }
143 #endif
144     }
145 }
146 
147