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