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 "tx_thread.h"
30 
31 
32 /**************************************************************************/
33 /*                                                                        */
34 /*  FUNCTION                                               RELEASE        */
35 /*                                                                        */
36 /*    _nx_tcp_client_socket_bind                          PORTABLE C      */
37 /*                                                           6.1.11       */
38 /*  AUTHOR                                                                */
39 /*                                                                        */
40 /*    Yuxin Zhou, Microsoft Corporation                                   */
41 /*                                                                        */
42 /*  DESCRIPTION                                                           */
43 /*                                                                        */
44 /*    This function binds the TCP socket structure to a specific TCP      */
45 /*    port.                                                               */
46 /*                                                                        */
47 /*  INPUT                                                                 */
48 /*                                                                        */
49 /*    socket_ptr                            Pointer to TCP socket         */
50 /*    port                                  16-bit TCP port number        */
51 /*    wait_option                           Suspension option             */
52 /*                                                                        */
53 /*  OUTPUT                                                                */
54 /*                                                                        */
55 /*    status                                Completion status             */
56 /*                                                                        */
57 /*  CALLS                                                                 */
58 /*                                                                        */
59 /*    _nx_tcp_free_port_find                Find free TCP port            */
60 /*    _nx_tcp_socket_thread_suspend         Suspend thread                */
61 /*    tx_mutex_get                          Obtain protection mutex       */
62 /*    tx_mutex_put                          Release protection mutex      */
63 /*                                                                        */
64 /*  CALLED BY                                                             */
65 /*                                                                        */
66 /*    Application Code                                                    */
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 /*  04-25-2022     Yuxin Zhou               Modified comment(s), and      */
76 /*                                            corrected the random value, */
77 /*                                            resulting in version 6.1.11 */
78 /*                                                                        */
79 /**************************************************************************/
_nx_tcp_client_socket_bind(NX_TCP_SOCKET * socket_ptr,UINT port,ULONG wait_option)80 UINT  _nx_tcp_client_socket_bind(NX_TCP_SOCKET *socket_ptr, UINT port, ULONG wait_option)
81 {
82 TX_INTERRUPT_SAVE_AREA
83 
84 UINT           index;
85 #ifdef NX_NAT_ENABLE
86 UINT           bound;
87 #endif /* NX_NAT_ENABLE */
88 NX_IP         *ip_ptr;
89 NX_TCP_SOCKET *search_ptr;
90 NX_TCP_SOCKET *end_ptr;
91 
92 
93     /* Setup the pointer to the associated IP instance.  */
94     ip_ptr =  socket_ptr -> nx_tcp_socket_ip_ptr;
95 
96     /* If trace is enabled, insert this event into the trace buffer.  */
97     NX_TRACE_IN_LINE_INSERT(NX_TRACE_TCP_CLIENT_SOCKET_BIND, ip_ptr, socket_ptr, port, wait_option, NX_TRACE_TCP_EVENTS, 0, 0);
98 
99     /* Obtain the IP mutex so we can figure out whether or not the port has already
100        been bound to.  */
101     tx_mutex_get(&(ip_ptr -> nx_ip_protection), TX_WAIT_FOREVER);
102 
103     /* Determine if the socket has already been bound to port or if a socket bind is
104        already pending from another thread.  */
105     if ((socket_ptr -> nx_tcp_socket_bound_next) ||
106         (socket_ptr -> nx_tcp_socket_bind_in_progress))
107     {
108 
109         /* Release the protection mutex.  */
110         tx_mutex_put(&(ip_ptr -> nx_ip_protection));
111 
112         /* Return an already bound error code.  */
113         return(NX_ALREADY_BOUND);
114     }
115 
116     /* Determine if the port needs to be allocated.  */
117     if (port == NX_ANY_PORT)
118     {
119 
120         /* Call the find routine to allocate a TCP port.  */
121         port = NX_SEARCH_PORT_START + (UINT)(((ULONG)NX_RAND()) % ((NX_MAX_PORT + 1) - NX_SEARCH_PORT_START));
122         if (_nx_tcp_free_port_find(ip_ptr, port, &port) != NX_SUCCESS)
123         {
124 
125             /* Release the protection mutex.  */
126             tx_mutex_put(&(ip_ptr -> nx_ip_protection));
127 
128             /* There was no free port, return an error code.  */
129             return(NX_NO_FREE_PORTS);
130         }
131     }
132 #ifdef NX_NAT_ENABLE
133     else
134     {
135 
136         /* Check if this IP interface has a NAT service. */
137         if (ip_ptr -> nx_ip_nat_port_verify)
138         {
139 
140             /* Yes, so check the port by NAT handler. If NAT does not use this port, allow NetX to use it.  */
141             bound = (ip_ptr -> nx_ip_nat_port_verify)(ip_ptr, NX_PROTOCOL_TCP, port);
142 
143             /* Check to see if the port has been used by NAT.  */
144             if (bound == NX_TRUE)
145             {
146 
147                 /* Release the protection.  */
148                 tx_mutex_put(&(ip_ptr -> nx_ip_protection));
149 
150                 /* Return the port unavailable error.  */
151                 return(NX_PORT_UNAVAILABLE);
152             }
153         }
154     }
155 #endif
156 
157     /* Save the port number in the TCP socket structure.  */
158     socket_ptr -> nx_tcp_socket_port =  port;
159 
160     /* Calculate the hash index in the TCP port array of the associated IP instance.  */
161     index =  (UINT)((port + (port >> 8)) & NX_TCP_PORT_TABLE_MASK);
162 
163     /* Pickup the head of the TCP ports bound list.  */
164     search_ptr =  ip_ptr -> nx_ip_tcp_port_table[index];
165 
166     /* Determine if we need to perform a list search.  */
167     if (search_ptr)
168     {
169 
170         /* Walk through the circular list of TCP sockets that are already
171            bound.  */
172         end_ptr = search_ptr;
173         do
174         {
175 
176             /* Determine if this entry is the same as the requested port.  */
177             if (search_ptr -> nx_tcp_socket_port == port)
178             {
179 
180                 /* Yes, the port has already been allocated.  */
181                 break;
182             }
183 
184             /* Move to the next entry in the list.  */
185             search_ptr =  search_ptr -> nx_tcp_socket_bound_next;
186         } while (search_ptr != end_ptr);
187     }
188 
189     /* Now determine if the port is available.  */
190     if ((search_ptr == NX_NULL) || (search_ptr -> nx_tcp_socket_port != port))
191     {
192 
193         /* Place this TCP socket structure on the list of bound ports.  */
194 
195         /* Disable interrupts.  */
196         TX_DISABLE
197 
198         /* Determine if the list is NULL.  */
199         if (search_ptr)
200         {
201 
202             /* There are already sockets on this list... just add this one
203                to the end.  */
204             socket_ptr -> nx_tcp_socket_bound_next =
205                 ip_ptr -> nx_ip_tcp_port_table[index];
206             socket_ptr -> nx_tcp_socket_bound_previous =
207                 (ip_ptr -> nx_ip_tcp_port_table[index]) -> nx_tcp_socket_bound_previous;
208             ((ip_ptr -> nx_ip_tcp_port_table[index]) -> nx_tcp_socket_bound_previous) -> nx_tcp_socket_bound_next =
209                 socket_ptr;
210             (ip_ptr -> nx_ip_tcp_port_table[index]) -> nx_tcp_socket_bound_previous =   socket_ptr;
211         }
212         else
213         {
214 
215             /* Nothing is on the TCP port list.  Add this TCP socket to an
216                empty list.  */
217             socket_ptr -> nx_tcp_socket_bound_next =      socket_ptr;
218             socket_ptr -> nx_tcp_socket_bound_previous =  socket_ptr;
219             ip_ptr -> nx_ip_tcp_port_table[index] =       socket_ptr;
220         }
221 
222         /* Restore interrupts.  */
223         TX_RESTORE
224 
225         /* Release the mutex protection.  */
226         tx_mutex_put(&(ip_ptr -> nx_ip_protection));
227 
228         /* Return success to the caller.  */
229         return(NX_SUCCESS);
230     }
231     else if (wait_option)
232     {
233 
234         /* Prepare for suspension of this thread.  */
235 
236         /* Increment the suspended thread count.  */
237         search_ptr -> nx_tcp_socket_bind_suspended_count++;
238 
239         /* Set the socket bind in progress flag (thread pointer).  */
240         socket_ptr -> nx_tcp_socket_bind_in_progress =  _tx_thread_current_ptr;
241 
242         /* Also remember the socket that has bound to the port, since the thread
243            is going to be suspended on that socket.  */
244         socket_ptr -> nx_tcp_socket_bound_previous =  search_ptr;
245 
246         /* Suspend the thread on this socket's connection attempt.  */
247         _nx_tcp_socket_thread_suspend(&(search_ptr -> nx_tcp_socket_bind_suspension_list), _nx_tcp_client_bind_cleanup, socket_ptr, &(ip_ptr -> nx_ip_protection), wait_option);
248 
249         /* Return the completion status.  */
250         return(_tx_thread_current_ptr -> tx_thread_suspend_status);
251     }
252     else
253     {
254 
255         /* Release the IP protection.  */
256         tx_mutex_put(&(ip_ptr -> nx_ip_protection));
257 
258         /* Return the port unavailable error.  */
259         return(NX_PORT_UNAVAILABLE);
260     }
261 }
262 
263