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_tcp.h"
30 #include "nx_ipv6.h"
31 #ifdef NX_ENABLE_HTTP_PROXY
32 #include "nx_http_proxy_client.h"
33 #endif /* NX_ENABLE_HTTP_PROXY */
34 
35 /**************************************************************************/
36 /*                                                                        */
37 /*  FUNCTION                                               RELEASE        */
38 /*                                                                        */
39 /*    _nx_tcp_socket_block_cleanup                        PORTABLE C      */
40 /*                                                           6.2.0        */
41 /*  AUTHOR                                                                */
42 /*                                                                        */
43 /*    Yuxin Zhou, Microsoft Corporation                                   */
44 /*                                                                        */
45 /*  DESCRIPTION                                                           */
46 /*                                                                        */
47 /*    This function cleans up the transmission control block.             */
48 /*                                                                        */
49 /*  INPUT                                                                 */
50 /*                                                                        */
51 /*    socket_ptr                            Pointer to owning socket      */
52 /*                                                                        */
53 /*  OUTPUT                                                                */
54 /*                                                                        */
55 /*    None                                                                */
56 /*                                                                        */
57 /*  CALLS                                                                 */
58 /*                                                                        */
59 /*    _nx_http_proxy_client_cleanup         Clean up HTTP Proxy           */
60 /*                                                                        */
61 /*  CALLED BY                                                             */
62 /*                                                                        */
63 /*    _nx_tcp_fast_periodic_processing      Process TCP packet for socket */
64 /*    _nx_tcp_socket_connection_reset       Reset TCP connection          */
65 /*    _nx_tcp_socket_disconnect             Close TCP conenction          */
66 /*    _nx_tcp_socket_state_last_ack         Process data on LAST ACK state*/
67 /*    _nx_tcp_client_socket_unbind          Ubind the TCP client socket   */
68 /*                                                                        */
69 /*  RELEASE HISTORY                                                       */
70 /*                                                                        */
71 /*    DATE              NAME                      DESCRIPTION             */
72 /*                                                                        */
73 /*  05-19-2020     Yuxin Zhou               Initial Version 6.0           */
74 /*  09-30-2020     Yuxin Zhou               Modified comment(s),          */
75 /*                                            resulting in version 6.1    */
76 /*  10-31-2022     Wenhui Xie               Modified comment(s), and      */
77 /*                                            supported HTTP Proxy,       */
78 /*                                            resulting in version 6.2.0  */
79 /*                                                                        */
80 /**************************************************************************/
_nx_tcp_socket_block_cleanup(NX_TCP_SOCKET * socket_ptr)81 VOID  _nx_tcp_socket_block_cleanup(NX_TCP_SOCKET *socket_ptr)
82 {
83 
84     /* Clean up the connect IP address.  */
85 
86     socket_ptr -> nx_tcp_socket_connect_ip.nxd_ip_version = 0;
87 #ifdef FEATURE_NX_IPV6
88     /* Clean up the IP address field. */
89     SET_UNSPECIFIED_ADDRESS(socket_ptr -> nx_tcp_socket_connect_ip.nxd_ip_address.v6);
90 #else /* FEATURE_NX_IPV6 */
91     socket_ptr -> nx_tcp_socket_connect_ip.nxd_ip_address.v4 = 0;
92 #endif /* FEATURE_NX_IPV6 */
93 
94     /* Clean up the connect port.  */
95     socket_ptr -> nx_tcp_socket_connect_port = 0;
96 
97     /* Reset zero window probe flag. */
98     socket_ptr -> nx_tcp_socket_zero_window_probe_has_data = NX_FALSE;
99 
100     /* Simply clear the timeout.  */
101     socket_ptr -> nx_tcp_socket_timeout = 0;
102 
103     /* Reset duplicated ack received. */
104     socket_ptr -> nx_tcp_socket_duplicated_ack_received = 0;
105 
106     /* Reset fast recovery stage. */
107     socket_ptr -> nx_tcp_socket_fast_recovery = NX_FALSE;
108 
109     /* Connection needs to be closed down immediately.  */
110     if (socket_ptr -> nx_tcp_socket_client_type)
111     {
112 
113         /* If trace is enabled, insert this event into the trace buffer.  */
114         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_CLOSED, NX_TRACE_INTERNAL_EVENTS, 0, 0);
115 
116         /* Client socket, return to a CLOSED state.  */
117         socket_ptr -> nx_tcp_socket_state =  NX_TCP_CLOSED;
118 
119 #ifdef NX_ENABLE_HTTP_PROXY
120         if (socket_ptr -> nx_tcp_socket_ip_ptr -> nx_ip_http_proxy_enable)
121         {
122 
123             /* Clean up the HTTP Proxy.  */
124             _nx_http_proxy_client_cleanup(socket_ptr);
125         }
126 #endif /* NX_ENABLE_HTTP_PROXY */
127     }
128     else
129     {
130 
131         /* If trace is enabled, insert this event into the trace buffer.  */
132         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_LISTEN_STATE, NX_TRACE_INTERNAL_EVENTS, 0, 0);
133 
134         /* Server socket, return to LISTEN state.  */
135         socket_ptr -> nx_tcp_socket_state =  NX_TCP_LISTEN_STATE;
136     }
137 }
138 
139