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