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