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 
32 /**************************************************************************/
33 /*                                                                        */
34 /*  FUNCTION                                               RELEASE        */
35 /*                                                                        */
36 /*    _nx_tcp_server_socket_unaccept                      PORTABLE C      */
37 /*                                                           6.1          */
38 /*  AUTHOR                                                                */
39 /*                                                                        */
40 /*    Yuxin Zhou, Microsoft Corporation                                   */
41 /*                                                                        */
42 /*  DESCRIPTION                                                           */
43 /*                                                                        */
44 /*    This function removes the server socket from association with the   */
45 /*    port receiving an earlier passive connection.  It is left in a      */
46 /*    state identical to the state after it was created.                  */
47 /*                                                                        */
48 /*  INPUT                                                                 */
49 /*                                                                        */
50 /*    socket_ptr                            Pointer to new TCP socket     */
51 /*                                                                        */
52 /*  OUTPUT                                                                */
53 /*                                                                        */
54 /*    status                                Completion status             */
55 /*                                                                        */
56 /*  CALLS                                                                 */
57 /*                                                                        */
58 /*    _nx_tcp_socket_receive_queue_flush    Release all receive packets   */
59 /*    tx_mutex_get                          Obtain a protection mutex     */
60 /*    tx_mutex_put                          Release a protection mutex    */
61 /*                                                                        */
62 /*  CALLED BY                                                             */
63 /*                                                                        */
64 /*    Application Code                                                    */
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_server_socket_unaccept(NX_TCP_SOCKET * socket_ptr)75 UINT  _nx_tcp_server_socket_unaccept(NX_TCP_SOCKET *socket_ptr)
76 {
77 
78 struct NX_TCP_LISTEN_STRUCT *listen_ptr;
79 NX_IP                       *ip_ptr;
80 UINT                         index;
81 UINT                         port;
82 
83 
84     /* Pickup the associated IP structure.  */
85     ip_ptr =  socket_ptr -> nx_tcp_socket_ip_ptr;
86 
87     /* If trace is enabled, insert this event into the trace buffer.  */
88     NX_TRACE_IN_LINE_INSERT(NX_TRACE_TCP_SERVER_SOCKET_UNACCEPT, ip_ptr, socket_ptr, socket_ptr -> nx_tcp_socket_state, 0, NX_TRACE_TCP_EVENTS, 0, 0);
89 
90     /* Obtain the IP mutex so we can access the IP and socket data structures.  */
91     tx_mutex_get(&(ip_ptr -> nx_ip_protection), TX_WAIT_FOREVER);
92 
93     /* Determine if the socket is in a state of disconnect.  */
94     if ((socket_ptr -> nx_tcp_socket_state >= NX_TCP_CLOSE_WAIT) ||
95         ((socket_ptr -> nx_tcp_socket_state == NX_TCP_CLOSED) && (socket_ptr -> nx_tcp_socket_bound_next)))
96     {
97 
98         /* If trace is enabled, insert this event into the trace buffer.  */
99         NX_TRACE_IN_LINE_INSERT(NX_TRACE_INTERNAL_TCP_STATE_CHANGE, ip_ptr, socket_ptr, socket_ptr -> nx_tcp_socket_state, NX_TCP_LISTEN_STATE, NX_TRACE_INTERNAL_EVENTS, 0, 0);
100 
101         /* Force to the listen state.  */
102         socket_ptr -> nx_tcp_socket_state =  NX_TCP_LISTEN_STATE;
103 
104         /* Ensure the connect information is cleared.  */
105         socket_ptr -> nx_tcp_socket_connect_ip.nxd_ip_version =    0;
106 #ifdef FEATURE_NX_IPV6
107         /* Zero out the IP address entry. */
108         SET_UNSPECIFIED_ADDRESS(socket_ptr -> nx_tcp_socket_connect_ip.nxd_ip_address.v6);
109 #else /* FEATURE_NX_IPV6 */
110         socket_ptr -> nx_tcp_socket_connect_ip.nxd_ip_address.v4 = 0;
111 #endif /* FEATURE_NX_IPV6 */
112 
113         socket_ptr -> nx_tcp_socket_connect_port =  0;
114     }
115 
116     /* Determine if the socket is in the listen state now.  */
117     if (socket_ptr -> nx_tcp_socket_state != NX_TCP_LISTEN_STATE)
118     {
119 
120         /* Release the IP protection.  */
121         tx_mutex_put(&(ip_ptr -> nx_ip_protection));
122 
123         /* Return an error code.  */
124         return(NX_NOT_LISTEN_STATE);
125     }
126 
127     /* Check for a thread suspended for disconnect processing to complete.  */
128     if (socket_ptr -> nx_tcp_socket_disconnect_suspended_thread)
129     {
130 
131         /* Call the disconnect thread suspension cleanup routine.  */
132         _nx_tcp_disconnect_cleanup(socket_ptr -> nx_tcp_socket_disconnect_suspended_thread NX_CLEANUP_ARGUMENT);
133     }
134 
135     /* Remove the TCP socket form the associated port.  */
136 
137     /* Pickup the port number in the TCP socket structure.  */
138     port =  socket_ptr -> nx_tcp_socket_port;
139 
140     /* Calculate the hash index in the TCP port array of the associated IP instance.  */
141     index =  (UINT)((port + (port >> 8)) & NX_TCP_PORT_TABLE_MASK);
142 
143     /* Determine if this is the only socket bound on this port list.  */
144     if (socket_ptr -> nx_tcp_socket_bound_next == socket_ptr)
145     {
146 
147         /* Yes, this is the only socket on the port list.  */
148 
149         /* Clear the list head pointer and the next pointer in the socket.  */
150         ip_ptr -> nx_ip_tcp_port_table[index] =   NX_NULL;
151         socket_ptr -> nx_tcp_socket_bound_next =  NX_NULL;
152     }
153     else if (socket_ptr -> nx_tcp_socket_bound_next)
154     {
155 
156         /* Relink the neighbors of this TCP socket.  */
157 
158         /* Update the links of the adjacent sockets.  */
159         (socket_ptr -> nx_tcp_socket_bound_next) -> nx_tcp_socket_bound_previous = socket_ptr -> nx_tcp_socket_bound_previous;
160         (socket_ptr -> nx_tcp_socket_bound_previous) -> nx_tcp_socket_bound_next =  socket_ptr -> nx_tcp_socket_bound_next;
161 
162         /* Determine if the head of the port list points to the socket being removed.
163            If so, we need to move the head pointer.  */
164         if (ip_ptr -> nx_ip_tcp_port_table[index] == socket_ptr)
165         {
166 
167             /* Yes, we need to move the port list head pointer.  */
168             ip_ptr -> nx_ip_tcp_port_table[index] =  socket_ptr -> nx_tcp_socket_bound_next;
169         }
170 
171         /* Clear the next pointer in the socket to indicate it is no longer bound.  */
172         socket_ptr -> nx_tcp_socket_bound_next =  NX_NULL;
173     }
174     else
175     {
176 
177         /* Not bound, so search through the active listen requests to see if this
178            socket is an active listen socket.  */
179         listen_ptr =  ip_ptr -> nx_ip_tcp_active_listen_requests;
180         if (listen_ptr)
181         {
182 
183             /* Search the active listen requests for this port.  */
184             do
185             {
186 
187                 /* Determine if we are releasing a socket that is listening.  */
188                 if (listen_ptr -> nx_tcp_listen_socket_ptr == socket_ptr)
189                 {
190 
191                     /* Remove the socket from the listener.  A relisten will be required to receive another
192                        connection.  */
193                     listen_ptr -> nx_tcp_listen_socket_ptr =  NX_NULL;
194                     break;
195                 }
196 
197                 /* Move to the next listen request.  */
198                 listen_ptr =  listen_ptr -> nx_tcp_listen_next;
199             } while (listen_ptr != ip_ptr -> nx_ip_tcp_active_listen_requests);
200         }
201     }
202 
203     /* If trace is enabled, insert this event into the trace buffer.  */
204     NX_TRACE_IN_LINE_INSERT(NX_TRACE_INTERNAL_TCP_STATE_CHANGE, ip_ptr, socket_ptr, socket_ptr -> nx_tcp_socket_state, NX_TCP_CLOSED, NX_TRACE_INTERNAL_EVENTS, 0, 0);
205 
206     /* Adjust the socket back to default states.  */
207     socket_ptr -> nx_tcp_socket_state =        NX_TCP_CLOSED;
208     socket_ptr -> nx_tcp_socket_client_type =  NX_TRUE;
209 
210     /* Socket is no longer active. Clear the timeout. */
211     socket_ptr -> nx_tcp_socket_timeout =  0;
212 
213     /* The socket is off the bound list...  we need to check for queued receive packets and
214        if found they need to be released.  */
215     if (socket_ptr -> nx_tcp_socket_receive_queue_count)
216     {
217 
218         /* Release queued receive packets.  */
219         _nx_tcp_socket_receive_queue_flush(socket_ptr);
220     }
221 
222     /* Release the IP protection.  */
223     tx_mutex_put(&(ip_ptr -> nx_ip_protection));
224 
225     /* Return success.  */
226     return(NX_SUCCESS);
227 }
228 
229