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 #include "nx_ipv6.h"
31 
32 /**************************************************************************/
33 /*                                                                        */
34 /*  FUNCTION                                               RELEASE        */
35 /*                                                                        */
36 /*    _nx_tcp_socket_connection_reset                     PORTABLE C      */
37 /*                                                           6.1.12       */
38 /*  AUTHOR                                                                */
39 /*                                                                        */
40 /*    Yuxin Zhou, Microsoft Corporation                                   */
41 /*                                                                        */
42 /*  DESCRIPTION                                                           */
43 /*                                                                        */
44 /*    This function processes a reset (RST) request received from the     */
45 /*    other side of the connection.                                       */
46 /*                                                                        */
47 /*  INPUT                                                                 */
48 /*                                                                        */
49 /*    socket_ptr                            Pointer to socket             */
50 /*                                                                        */
51 /*  OUTPUT                                                                */
52 /*                                                                        */
53 /*    None                                                                */
54 /*                                                                        */
55 /*  CALLS                                                                 */
56 /*                                                                        */
57 /*    _nx_tcp_socket_block_cleanup          Cleanup the socket block      */
58 /*    _nx_tcp_socket_transmit_queue_flush   Release transmitted packets   */
59 /*    _nx_tcp_socket_receive_queue_flush    Release received packets      */
60 /*    _nx_tcp_connect_cleanup               Resume thread suspended       */
61 /*                                            waiting for connection      */
62 /*    _nx_tcp_disconnect_cleanup            Resume thread suspended       */
63 /*                                            waiting for disconnection   */
64 /*    _nx_tcp_receive_cleanup               Resume threads suspended on   */
65 /*                                            the receive queue           */
66 /*    _nx_tcp_transmit_cleanup              Resume threads suspended on   */
67 /*                                            the transmit queue          */
68 /*    (application disconnect callback)                                   */
69 /*                                                                        */
70 /*  CALLED BY                                                             */
71 /*                                                                        */
72 /*    _nx_tcp_fast_periodic_processing      Transmit retry timeout        */
73 /*    _nx_tcp_periodic_processing           Keepalive processing          */
74 /*    _nx_tcp_socket_packet_process         Socket packet processing      */
75 /*                                                                        */
76 /*  RELEASE HISTORY                                                       */
77 /*                                                                        */
78 /*    DATE              NAME                      DESCRIPTION             */
79 /*                                                                        */
80 /*  05-19-2020     Yuxin Zhou               Initial Version 6.0           */
81 /*  09-30-2020     Yuxin Zhou               Modified comment(s),          */
82 /*                                            resulting in version 6.1    */
83 /*  07-29-2022     Yuxin Zhou               Modified comment(s), and      */
84 /*                                            corrected the logic of      */
85 /*                                            flushing receive queue.     */
86 /*                                            resulting in version 6.1.12 */
87 /*                                                                        */
88 /**************************************************************************/
_nx_tcp_socket_connection_reset(NX_TCP_SOCKET * socket_ptr)89 VOID  _nx_tcp_socket_connection_reset(NX_TCP_SOCKET *socket_ptr)
90 {
91 
92 UINT saved_state;
93 
94     /* Save the current state of the socket.  */
95     saved_state =  socket_ptr -> nx_tcp_socket_state;
96 
97     /* Cleanup the transmission control block.  */
98     _nx_tcp_socket_block_cleanup(socket_ptr);
99 
100     /* Check for queued sent packets and if found they need
101        to be released.  */
102     if (socket_ptr -> nx_tcp_socket_transmit_sent_count)
103     {
104 
105         /* Release all transmit packets.  */
106         _nx_tcp_socket_transmit_queue_flush(socket_ptr);
107     }
108 
109     /* Clear all receive thread suspensions on this socket.  */
110     while (socket_ptr -> nx_tcp_socket_receive_suspension_list)
111     {
112 
113         /* Call the receive thread suspension cleanup routine.  */
114         _nx_tcp_receive_cleanup(socket_ptr -> nx_tcp_socket_receive_suspension_list NX_CLEANUP_ARGUMENT);
115     }
116 
117     /* Clear all transmit thread suspensions on this socket.  */
118     while (socket_ptr -> nx_tcp_socket_transmit_suspension_list)
119     {
120 
121         /* Call the receive thread suspension cleanup routine.  */
122         _nx_tcp_transmit_cleanup(socket_ptr -> nx_tcp_socket_transmit_suspension_list NX_CLEANUP_ARGUMENT);
123     }
124 
125     /* Check for suspended connect thread.  */
126     if (socket_ptr -> nx_tcp_socket_connect_suspended_thread)
127     {
128 
129         /* Call the connect thread suspension cleanup routine.  */
130         _nx_tcp_connect_cleanup(socket_ptr -> nx_tcp_socket_connect_suspended_thread NX_CLEANUP_ARGUMENT);
131     }
132 
133     /* Check for suspended disconnect thread.  */
134     if (socket_ptr -> nx_tcp_socket_disconnect_suspended_thread)
135     {
136 
137         /* Resume the thread suspended on the disconnect.  */
138         _nx_tcp_disconnect_cleanup(socket_ptr -> nx_tcp_socket_disconnect_suspended_thread NX_CLEANUP_ARGUMENT);
139     }
140 
141     /* Determine if the socket was in an established state.  */
142     if (saved_state == NX_TCP_ESTABLISHED)
143     {
144 
145         /* If given, call the application's disconnect callback function
146            for disconnect.  */
147         if (socket_ptr -> nx_tcp_disconnect_callback)
148         {
149 
150             /* Call the application's disconnect handling function.  It is
151                responsible for calling the socket disconnect function.  */
152             (socket_ptr -> nx_tcp_disconnect_callback)(socket_ptr);
153         }
154     }
155 
156 #ifndef NX_DISABLE_EXTENDED_NOTIFY_SUPPORT
157 
158     /* Is a disconnect complete callback registered with the TCP socket? */
159     if (socket_ptr -> nx_tcp_disconnect_complete_notify)
160     {
161 
162         /* Notify the application through the socket disconnect_complete callback.  */
163         (socket_ptr -> nx_tcp_disconnect_complete_notify)(socket_ptr);
164     }
165 #endif
166 }
167 
168