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 
30 
31 /* Bring in externs for caller checking code.  */
32 
33 NX_CALLER_CHECKING_EXTERNS
34 
35 
36 /**************************************************************************/
37 /*                                                                        */
38 /*  FUNCTION                                               RELEASE        */
39 /*                                                                        */
40 /*    _nxde_tcp_client_socket_connect                     PORTABLE C      */
41 /*                                                           6.1          */
42 /*  AUTHOR                                                                */
43 /*                                                                        */
44 /*    Yuxin Zhou, Microsoft Corporation                                   */
45 /*                                                                        */
46 /*  DESCRIPTION                                                           */
47 /*                                                                        */
48 /*    This function checks for errors in the TCP client socket connect    */
49 /*    function call.                                                      */
50 /*                                                                        */
51 /*  INPUT                                                                 */
52 /*                                                                        */
53 /*    socket_ptr                            Pointer to TCP client socket  */
54 /*    server_ip                             IP address of server          */
55 /*    server_port                           Port number of server         */
56 /*    wait_option                           Suspension option             */
57 /*                                                                        */
58 /*  OUTPUT                                                                */
59 /*                                                                        */
60 /*    status                                Acctual completion status     */
61 /*    NX_PTR_ERROR                          Invalid pointer input         */
62 /*    NX_NOT_ENABLED                        TCP not enabled               */
63 /*    NX_IP_ADDRESS_ERROR                   Invalid TCP server IP address */
64 /*    NX_INVALID_PORT                       Invalid TCP server port       */
65 /*                                                                        */
66 /*  CALLS                                                                 */
67 /*                                                                        */
68 /*    _nxd_tcp_client_socket_connect        Actual client socket connect  */
69 /*                                            function                    */
70 /*                                                                        */
71 /*  CALLED BY                                                             */
72 /*                                                                        */
73 /*    Application Code                                                    */
74 /*                                                                        */
75 /*  RELEASE HISTORY                                                       */
76 /*                                                                        */
77 /*    DATE              NAME                      DESCRIPTION             */
78 /*                                                                        */
79 /*  05-19-2020     Yuxin Zhou               Initial Version 6.0           */
80 /*  09-30-2020     Yuxin Zhou               Modified comment(s),          */
81 /*                                            resulting in version 6.1    */
82 /*                                                                        */
83 /**************************************************************************/
_nxde_tcp_client_socket_connect(NX_TCP_SOCKET * socket_ptr,NXD_ADDRESS * server_ip,UINT server_port,ULONG wait_option)84 UINT  _nxde_tcp_client_socket_connect(NX_TCP_SOCKET *socket_ptr,
85                                       NXD_ADDRESS *server_ip,
86                                       UINT server_port, ULONG wait_option)
87 {
88 
89 UINT status;
90 
91 
92     /* Check for invalid input pointers.  */
93     if ((socket_ptr == NX_NULL) || (socket_ptr -> nx_tcp_socket_id != NX_TCP_ID))
94     {
95         return(NX_PTR_ERROR);
96     }
97 
98     /* Verify TCP is enabled.  */
99     if (!(socket_ptr -> nx_tcp_socket_ip_ptr) -> nx_ip_tcp_packet_receive)
100     {
101         return(NX_NOT_ENABLED);
102     }
103 
104 
105     /* Check for valid TCP server address. */
106     if (server_ip == NX_NULL)
107     {
108         return(NX_IP_ADDRESS_ERROR);
109     }
110 
111     /* Check that the server IP address version is either IPv4 or IPv6. */
112     if ((server_ip -> nxd_ip_version != NX_IP_VERSION_V4) &&
113         (server_ip -> nxd_ip_version != NX_IP_VERSION_V6))
114     {
115 
116         return(NX_IP_ADDRESS_ERROR);
117     }
118 
119 #ifndef NX_DISABLE_IPV4
120     /* Check for a valid server IP address if the server_ip is version IPv4.  */
121     if (server_ip -> nxd_ip_version == NX_IP_VERSION_V4)
122     {
123         if (((server_ip -> nxd_ip_address.v4 & NX_IP_CLASS_A_MASK) != NX_IP_CLASS_A_TYPE) &&
124             ((server_ip -> nxd_ip_address.v4 & NX_IP_CLASS_B_MASK) != NX_IP_CLASS_B_TYPE) &&
125             ((server_ip -> nxd_ip_address.v4 & NX_IP_CLASS_C_MASK) != NX_IP_CLASS_C_TYPE))
126         {
127             return(NX_IP_ADDRESS_ERROR);
128         }
129     }
130 #endif /* !NX_DISABLE_IPV4  */
131 
132     /* Check for an invalid port.  */
133     if (((ULONG)server_port) > (ULONG)NX_MAX_PORT)
134     {
135         return(NX_INVALID_PORT);
136     }
137 
138     /* Check for appropriate caller.  */
139     NX_THREADS_ONLY_CALLER_CHECKING
140 
141     /* Call actual TCP client socket connect function.  */
142     status =  _nxd_tcp_client_socket_connect(socket_ptr, server_ip, server_port, wait_option);
143 
144     /* Return completion status.  */
145     return(status);
146 }
147 
148