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 /* */
35 /* FUNCTION RELEASE */
36 /* */
37 /* _nx_tcp_client_socket_unbind PORTABLE C */
38 /* 6.1 */
39 /* AUTHOR */
40 /* */
41 /* Yuxin Zhou, Microsoft Corporation */
42 /* */
43 /* DESCRIPTION */
44 /* */
45 /* This function unbinds the TCP client socket structure from the */
46 /* previously bound TCP port. */
47 /* */
48 /* INPUT */
49 /* */
50 /* socket_ptr Pointer to TCP client socket */
51 /* */
52 /* OUTPUT */
53 /* */
54 /* status Completion status */
55 /* */
56 /* CALLS */
57 /* */
58 /* _nx_tcp_client_bind_cleanup Remove and cleanup bind req */
59 /* _nx_tcp_socket_thread_resume Resume thread suspended on */
60 /* port */
61 /* _nx_tcp_socket_block_cleanup Cleanup the socket block */
62 /* tx_mutex_get Obtain protection mutex */
63 /* tx_mutex_put Release protection mutex */
64 /* */
65 /* CALLED BY */
66 /* */
67 /* Application Code */
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 /* */
77 /**************************************************************************/
_nx_tcp_client_socket_unbind(NX_TCP_SOCKET * socket_ptr)78 UINT _nx_tcp_client_socket_unbind(NX_TCP_SOCKET *socket_ptr)
79 {
80 TX_INTERRUPT_SAVE_AREA
81
82 UINT index;
83 UINT port;
84 NX_IP *ip_ptr;
85 NX_TCP_SOCKET *new_socket_ptr;
86
87
88 /* Setup the pointer to the associated IP instance. */
89 ip_ptr = socket_ptr -> nx_tcp_socket_ip_ptr;
90
91 /* If trace is enabled, insert this event into the trace buffer. */
92 NX_TRACE_IN_LINE_INSERT(NX_TRACE_TCP_CLIENT_SOCKET_UNBIND, ip_ptr, socket_ptr, 0, 0, NX_TRACE_TCP_EVENTS, 0, 0);
93
94 /* Obtain the IP mutex so we can figure out whether or not the port has already
95 been bound to. */
96 tx_mutex_get(&(ip_ptr -> nx_ip_protection), TX_WAIT_FOREVER);
97
98 /* Determine if the socket is still in the timed wait state. */
99 if (socket_ptr -> nx_tcp_socket_state == NX_TCP_TIMED_WAIT)
100 {
101
102 /* Cleanup the transmission control block. */
103 _nx_tcp_socket_block_cleanup(socket_ptr);
104 }
105
106 /* Determine if the socket is still in the closed state. */
107 if (socket_ptr -> nx_tcp_socket_state != NX_TCP_CLOSED)
108 {
109
110 /* No, release the IP protection. */
111 tx_mutex_put(&(ip_ptr -> nx_ip_protection));
112
113 /* Return an error code. */
114 return(NX_NOT_CLOSED);
115 }
116
117 /* Determine if the socket is bound to port. */
118 if (!socket_ptr -> nx_tcp_socket_bound_next)
119 {
120
121 /* Determine if there is a special condition for the socket not being in
122 a bound condition... i.e. the socket is in a pending-to-be-bound condition
123 in a call from a different thread. */
124 if (socket_ptr -> nx_tcp_socket_bind_in_progress)
125 {
126
127 /* Execute the bind suspension cleanup routine. */
128 _nx_tcp_client_bind_cleanup(socket_ptr -> nx_tcp_socket_bind_in_progress NX_CLEANUP_ARGUMENT);
129
130 /* Release the protection mutex. */
131 tx_mutex_put(&(ip_ptr -> nx_ip_protection));
132
133 /* Return success. */
134 return(NX_SUCCESS);
135 }
136 else
137 {
138
139 /* Release the protection mutex. */
140 tx_mutex_put(&(ip_ptr -> nx_ip_protection));
141
142 /* Return a not bound error code. */
143 return(NX_NOT_BOUND);
144 }
145 }
146
147 /* Otherwise, the socket is bound. We need to remove this socket from the
148 port and check for any other TCP socket bind requests that are queued. */
149
150 /* Pickup the port number in the TCP socket structure. */
151 port = socket_ptr -> nx_tcp_socket_port;
152
153 /* Calculate the hash index in the TCP port array of the associated IP instance. */
154 index = (UINT)((port + (port >> 8)) & NX_TCP_PORT_TABLE_MASK);
155
156 /* Disable interrupts while we unlink the current socket. */
157 TX_DISABLE
158
159 /* The socket is off the bound list... we need to check for queued receive packets and
160 if found they need to be released. */
161 if (socket_ptr -> nx_tcp_socket_receive_queue_count)
162 {
163
164 /* Remove all packets on the socket's receive queue. */
165 _nx_tcp_socket_receive_queue_flush(socket_ptr);
166 }
167
168 /* Determine if this is the only socket bound on this port list. */
169 if (socket_ptr -> nx_tcp_socket_bound_next == socket_ptr)
170 {
171
172 /* Yes, this is the only socket on the port list. */
173
174 /* Clear the list head pointer and the next pointer in the socket. */
175 ip_ptr -> nx_ip_tcp_port_table[index] = NX_NULL;
176 socket_ptr -> nx_tcp_socket_bound_next = NX_NULL;
177 }
178 else
179 {
180
181 /* Relink the neighbors of this TCP socket. */
182
183 /* Update the links of the adjacent sockets. */
184 (socket_ptr -> nx_tcp_socket_bound_next) -> nx_tcp_socket_bound_previous =
185 socket_ptr -> nx_tcp_socket_bound_previous;
186 (socket_ptr -> nx_tcp_socket_bound_previous) -> nx_tcp_socket_bound_next =
187 socket_ptr -> nx_tcp_socket_bound_next;
188
189 /* Determine if the head of the port list points to the socket being removed.
190 If so, we need to move the head pointer. */
191 if (ip_ptr -> nx_ip_tcp_port_table[index] == socket_ptr)
192 {
193
194 /* Yes, we need to move the port list head pointer. */
195 ip_ptr -> nx_ip_tcp_port_table[index] = socket_ptr -> nx_tcp_socket_bound_next;
196 }
197
198 /* Clear the next pointer in the socket to indicate it is no longer bound. */
199 socket_ptr -> nx_tcp_socket_bound_next = NX_NULL;
200 }
201
202 /* Restore interrupts. */
203 TX_RESTORE
204
205 /* Determine if there are any threads suspended on trying to bind to the
206 same port. */
207 if (socket_ptr -> nx_tcp_socket_bind_suspension_list)
208 {
209
210 /* Pickup the new socket structure to link to the port list. */
211 new_socket_ptr = (NX_TCP_SOCKET *)(socket_ptr -> nx_tcp_socket_bind_suspension_list) -> tx_thread_suspend_control_block;
212
213 /* Clear the new socket's bind in progress flag. */
214 new_socket_ptr -> nx_tcp_socket_bind_in_progress = NX_NULL;
215
216 /* Inherit the suspension list from the previously bound socket. Decrement the suspension count
217 early since this thread will be resumed and removed from the list later. */
218 new_socket_ptr -> nx_tcp_socket_bind_suspension_list =
219 socket_ptr -> nx_tcp_socket_bind_suspension_list;
220 new_socket_ptr -> nx_tcp_socket_bind_suspended_count = socket_ptr -> nx_tcp_socket_bind_suspended_count - 1;
221
222 /* Clear the original socket's information. */
223 socket_ptr -> nx_tcp_socket_bind_suspension_list = NX_NULL;
224 socket_ptr -> nx_tcp_socket_bind_suspended_count = 0;
225
226 /* Disable interrupts. */
227 TX_DISABLE
228
229 /* Link the new socket to the bound list. */
230 if (ip_ptr -> nx_ip_tcp_port_table[index])
231 {
232
233 /* There are already sockets on this list... just add this one
234 to the end. */
235 new_socket_ptr -> nx_tcp_socket_bound_next =
236 ip_ptr -> nx_ip_tcp_port_table[index];
237 new_socket_ptr -> nx_tcp_socket_bound_previous =
238 (ip_ptr -> nx_ip_tcp_port_table[index]) -> nx_tcp_socket_bound_previous;
239 ((ip_ptr -> nx_ip_tcp_port_table[index]) -> nx_tcp_socket_bound_previous) -> nx_tcp_socket_bound_next =
240 new_socket_ptr;
241 (ip_ptr -> nx_ip_tcp_port_table[index]) -> nx_tcp_socket_bound_previous = new_socket_ptr;
242 }
243 else
244 {
245
246 /* Nothing is on the TCP port list. Add this TCP socket to an
247 empty list. */
248 new_socket_ptr -> nx_tcp_socket_bound_next = new_socket_ptr;
249 new_socket_ptr -> nx_tcp_socket_bound_previous = new_socket_ptr;
250 ip_ptr -> nx_ip_tcp_port_table[index] = new_socket_ptr;
251 }
252
253 /* Restore interrupts. */
254 TX_RESTORE
255
256 /* Resume the thread suspended on the bind call. */
257 _nx_tcp_socket_thread_resume(&(new_socket_ptr -> nx_tcp_socket_bind_suspension_list), NX_SUCCESS);
258 }
259
260 /* Release the protection mutex. */
261 tx_mutex_put(&(ip_ptr -> nx_ip_protection));
262
263 /* Return success. */
264 return(NX_SUCCESS);
265 }
266
267