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_ip.h"
29 #include "nx_tcp.h"
30 #include "nx_packet.h"
31 #ifdef FEATURE_NX_IPV6
32 #include "nx_ipv6.h"
33 #endif /* FEATURE_NX_IPV6 */
34 
35 /* Bring in externs for caller checking code.  */
36 
37 NX_CALLER_CHECKING_EXTERNS
38 
39 
40 /**************************************************************************/
41 /*                                                                        */
42 /*  FUNCTION                                               RELEASE        */
43 /*                                                                        */
44 /*    _nxe_tcp_socket_send                                PORTABLE C      */
45 /*                                                           6.1          */
46 /*  AUTHOR                                                                */
47 /*                                                                        */
48 /*    Yuxin Zhou, Microsoft Corporation                                   */
49 /*                                                                        */
50 /*  DESCRIPTION                                                           */
51 /*                                                                        */
52 /*    This function checks for errors in the TCP socket send              */
53 /*    function call.                                                      */
54 /*                                                                        */
55 /*  INPUT                                                                 */
56 /*                                                                        */
57 /*    socket_ptr                            Pointer to socket             */
58 /*    packet_ptr                            Pointer to packet to send     */
59 /*    wait_option                           Suspension option             */
60 /*                                                                        */
61 /*  OUTPUT                                                                */
62 /*                                                                        */
63 /*    status                                Completion status             */
64 /*                                                                        */
65 /*  CALLS                                                                 */
66 /*                                                                        */
67 /*    _nx_tcp_socket_send                   Actual TCP socket send        */
68 /*                                            function                    */
69 /*                                                                        */
70 /*  CALLED BY                                                             */
71 /*                                                                        */
72 /*    Application                                                         */
73 /*                                                                        */
74 /*  RELEASE HISTORY                                                       */
75 /*                                                                        */
76 /*    DATE              NAME                      DESCRIPTION             */
77 /*                                                                        */
78 /*  05-19-2020     Yuxin Zhou               Initial Version 6.0           */
79 /*  09-30-2020     Yuxin Zhou               Modified comment(s),          */
80 /*                                            resulting in version 6.1    */
81 /*                                                                        */
82 /**************************************************************************/
_nxe_tcp_socket_send(NX_TCP_SOCKET * socket_ptr,NX_PACKET ** packet_ptr_ptr,ULONG wait_option)83 UINT  _nxe_tcp_socket_send(NX_TCP_SOCKET *socket_ptr, NX_PACKET **packet_ptr_ptr, ULONG wait_option)
84 {
85 
86 NX_PACKET *packet_ptr;
87 UINT       status;
88 UINT       ip_header_size;
89 
90     /* Setup packet pointer.  */
91     packet_ptr =  *packet_ptr_ptr;
92 
93     /* Check for invalid input pointers.  */
94     if ((socket_ptr == NX_NULL) || (socket_ptr -> nx_tcp_socket_id != NX_TCP_ID))
95     {
96         return(NX_PTR_ERROR);
97     }
98 
99     /* Check for an invalid packet pointer.  */
100     /*lint -e{923} suppress cast of ULONG to pointer.  */
101     if ((packet_ptr == NX_NULL) || (packet_ptr -> nx_packet_union_next.nx_packet_tcp_queue_next != ((NX_PACKET *)NX_PACKET_ALLOCATED)))
102     {
103         return(NX_INVALID_PACKET);
104     }
105 
106     /* Check to see if TCP is enabled.  */
107     if (!(socket_ptr -> nx_tcp_socket_ip_ptr) -> nx_ip_tcp_packet_receive)
108     {
109         return(NX_NOT_ENABLED);
110     }
111 
112     /* Check for an invalid packet prepend pointer.  */
113 #ifndef NX_DISABLE_IPV4
114     if (socket_ptr -> nx_tcp_socket_connect_ip.nxd_ip_version == NX_IP_VERSION_V4)
115     {
116         ip_header_size = (UINT)sizeof(NX_IPV4_HEADER);
117     }
118     else
119 #endif /* !NX_DISABLE_IPV4  */
120 
121 #ifdef FEATURE_NX_IPV6
122     if (socket_ptr -> nx_tcp_socket_connect_ip.nxd_ip_version == NX_IP_VERSION_V6)
123     {
124         ip_header_size = (UINT)sizeof(NX_IPV6_HEADER);
125     }
126     else
127 #endif /* FEATURE_NX_IPV6 */
128     {
129         return(NX_NOT_CONNECTED);
130     }
131 
132     /*lint -e{946} -e{947} suppress pointer subtraction, since it is necessary. */
133     if ((INT)(packet_ptr -> nx_packet_prepend_ptr - packet_ptr -> nx_packet_data_start) < (INT)(ip_header_size + sizeof(NX_TCP_HEADER)))
134     {
135 
136 #ifndef NX_DISABLE_TCP_INFO
137 
138         /* Increment the TCP invalid packet count.  */
139         (socket_ptr -> nx_tcp_socket_ip_ptr) -> nx_ip_tcp_invalid_packets++;
140 #endif
141 
142         /* Return error code.  */
143         return(NX_UNDERFLOW);
144     }
145 
146     /* Check for an invalid packet append pointer.  */
147     /*lint -e{946} suppress pointer subtraction, since it is necessary. */
148     if (packet_ptr -> nx_packet_append_ptr > packet_ptr -> nx_packet_data_end)
149     {
150 
151 #ifndef NX_DISABLE_TCP_INFO
152 
153         /* Increment the TCP invalid packet count.  */
154         (socket_ptr -> nx_tcp_socket_ip_ptr) -> nx_ip_tcp_invalid_packets++;
155 #endif
156 
157         /* Return error code.  */
158         return(NX_OVERFLOW);
159     }
160 
161     /* Check for appropriate caller.  */
162     NX_THREADS_ONLY_CALLER_CHECKING
163 
164     /* Call actual TCP socket send function.  */
165     status =  _nx_tcp_socket_send(socket_ptr, packet_ptr, wait_option);
166 
167     /* Determine if the packet send was successful.  */
168     if (status == NX_SUCCESS)
169     {
170 
171         /* Yes, now clear the application's packet pointer so it can't be accidentally
172            used again by the application.  This is only done when error checking is
173            enabled.  */
174         *packet_ptr_ptr =  NX_NULL;
175     }
176 
177     /* Return completion status.  */
178     return(status);
179 }
180 
181