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 #ifdef NX_ENABLE_HTTP_PROXY
30 #include "nx_http_proxy_client.h"
31 #endif /* NX_ENABLE_HTTP_PROXY */
32 
33 
34 /**************************************************************************/
35 /*                                                                        */
36 /*  FUNCTION                                               RELEASE        */
37 /*                                                                        */
38 /*    _nx_tcp_socket_state_wait                           PORTABLE C      */
39 /*                                                           6.2.0        */
40 /*  AUTHOR                                                                */
41 /*                                                                        */
42 /*    Yuxin Zhou, Microsoft Corporation                                   */
43 /*                                                                        */
44 /*  DESCRIPTION                                                           */
45 /*                                                                        */
46 /*    This function waits for the specified socket to reach the specified */
47 /*    TCP state.                                                          */
48 /*                                                                        */
49 /*  INPUT                                                                 */
50 /*                                                                        */
51 /*    socket_ptr                            Pointer to socket             */
52 /*    desired_state                         Desired TCP state             */
53 /*    wait_option                           Suspension option             */
54 /*                                                                        */
55 /*  OUTPUT                                                                */
56 /*                                                                        */
57 /*    status                                Completion status             */
58 /*                                                                        */
59 /*  CALLS                                                                 */
60 /*                                                                        */
61 /*    tx_thread_sleep                       Sleep to wait for state change*/
62 /*                                                                        */
63 /*  CALLED BY                                                             */
64 /*                                                                        */
65 /*    Application                                                         */
66 /*                                                                        */
67 /*  RELEASE HISTORY                                                       */
68 /*                                                                        */
69 /*    DATE              NAME                      DESCRIPTION             */
70 /*                                                                        */
71 /*  05-19-2020     Yuxin Zhou               Initial Version 6.0           */
72 /*  09-30-2020     Yuxin Zhou               Modified comment(s),          */
73 /*                                            resulting in version 6.1    */
74 /*  10-31-2022     Wenhui Xie               Modified comment(s), and      */
75 /*                                            supported HTTP Proxy,       */
76 /*                                            resulting in version 6.2.0  */
77 /*                                                                        */
78 /**************************************************************************/
_nx_tcp_socket_state_wait(NX_TCP_SOCKET * socket_ptr,UINT desired_state,ULONG wait_option)79 UINT  _nx_tcp_socket_state_wait(NX_TCP_SOCKET *socket_ptr, UINT desired_state, ULONG wait_option)
80 {
81 
82     /* If trace is enabled, insert this event into the trace buffer.  */
83     NX_TRACE_IN_LINE_INSERT(NX_TRACE_TCP_SOCKET_STATE_WAIT, socket_ptr -> nx_tcp_socket_ip_ptr, socket_ptr, desired_state, socket_ptr -> nx_tcp_socket_state, NX_TRACE_TCP_EVENTS, 0, 0);
84 
85     /* Loop to wait for the desired socket state.   */
86     for (;;)
87     {
88 
89         /* Determine if the socket pointer is still valid.  */
90         if (socket_ptr -> nx_tcp_socket_id != NX_TCP_ID)
91         {
92 
93             /* Not still valid, return an error.  */
94             return(NX_PTR_ERROR);
95         }
96 
97         /* Determine if the desired state is present.  */
98         if (socket_ptr -> nx_tcp_socket_state == desired_state)
99         {
100 
101 #ifdef NX_ENABLE_HTTP_PROXY
102             if ((desired_state != NX_TCP_ESTABLISHED) ||
103                 (!socket_ptr -> nx_tcp_socket_ip_ptr -> nx_ip_http_proxy_enable) ||
104                 (!socket_ptr -> nx_tcp_socket_client_type) ||
105                 (socket_ptr -> nx_tcp_socket_http_proxy_state == NX_HTTP_PROXY_STATE_CONNECTED))
106 #endif /* NX_ENABLE_HTTP_PROXY */
107             {
108                 /* The desired state is present, return success!  */
109                 return(NX_SUCCESS);
110             }
111         }
112 
113         /* Check to see if there is more time to wait.  */
114         if (wait_option)
115         {
116 
117             /* Yes, there is more time... sleep for a tick.  */
118             tx_thread_sleep(1);
119 
120             /* Decrease the wait time.  */
121             wait_option--;
122         }
123         else
124         {
125 
126             /* Timeout, just return an error.  */
127             return(NX_NOT_SUCCESSFUL);
128         }
129     }
130 }
131 
132