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