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 /**   User Datagram Protocol (UDP)                                        */
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_udp.h"
30 
31 /**************************************************************************/
32 /*                                                                        */
33 /*  FUNCTION                                               RELEASE        */
34 /*                                                                        */
35 /*    _nxe_udp_source_extract                             PORTABLE C      */
36 /*                                                           6.1          */
37 /*  AUTHOR                                                                */
38 /*                                                                        */
39 /*    Yuxin Zhou, Microsoft Corporation                                   */
40 /*                                                                        */
41 /*  DESCRIPTION                                                           */
42 /*                                                                        */
43 /*    This function checks for errors in the extract UDP source call.     */
44 /*                                                                        */
45 /*  INPUT                                                                 */
46 /*                                                                        */
47 /*    packet_ptr                            Pointer to UDP packet pointer */
48 /*    ip_address                            Pointer to packet source IP   */
49 /*                                            address                     */
50 /*    port                                  Pointer to source UDP port    */
51 /*                                                                        */
52 /*  OUTPUT                                                                */
53 /*                                                                        */
54 /*    status                                Actual completion status      */
55 /*    NX_PTR_ERROR                          Invalid pointer input         */
56 /*    NX_INVALID_PACKET                     Malformed packet to process   */
57 /*                                                                        */
58 /*  CALLS                                                                 */
59 /*                                                                        */
60 /*    _nx_udp_source_extract                Actual UDP source extract     */
61 /*                                            function                    */
62 /*                                                                        */
63 /*  CALLED BY                                                             */
64 /*                                                                        */
65 /*    Application Code                                                    */
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 /*                                                                        */
75 /**************************************************************************/
_nxe_udp_source_extract(NX_PACKET * packet_ptr,ULONG * ip_address,UINT * port)76 UINT  _nxe_udp_source_extract(NX_PACKET *packet_ptr, ULONG *ip_address, UINT *port)
77 {
78 
79 #ifndef NX_DISABLE_IPV4
80 UINT status;
81 
82 
83     /* Check for invalid input pointers.  */
84     if ((packet_ptr == NX_NULL) || (ip_address == NX_NULL) || (port == NX_NULL))
85     {
86 
87         return(NX_PTR_ERROR);
88     }
89 
90     /* Check for invalid packet pointer.  */
91     if (packet_ptr -> nx_packet_ip_header == NX_NULL)
92     {
93 
94         return(NX_INVALID_PACKET);
95     }
96 
97 
98     if (packet_ptr -> nx_packet_ip_version != NX_IP_VERSION_V4)
99     {
100 
101         return(NX_INVALID_PACKET);
102     }
103 
104     /* Check to see if the packet has enough room in front for backing up.  */
105     /*lint -e{946} -e{947} suppress pointer subtraction, since it is necessary. */
106     if ((UINT)(packet_ptr -> nx_packet_prepend_ptr - packet_ptr -> nx_packet_data_start) <
107         (sizeof(NX_UDP_HEADER) + sizeof(NX_IPV4_HEADER)))
108     {
109 
110         return(NX_INVALID_PACKET);
111     }
112 
113     /* Call actual UDP source extract function.  */
114     status =  _nx_udp_source_extract(packet_ptr, ip_address, port);
115 
116     /* Return completion status.  */
117     return(status);
118 #else
119     NX_PARAMETER_NOT_USED(packet_ptr);
120     NX_PARAMETER_NOT_USED(ip_address);
121     NX_PARAMETER_NOT_USED(port);
122 
123     return(NX_NOT_SUPPORTED);
124 #endif /* !NX_DISABLE_IPV4  */
125 }
126 
127