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_udp.h"
29 #include "nx_ipv4.h"
30 
31 
32 
33 /**************************************************************************/
34 /*                                                                        */
35 /*  FUNCTION                                               RELEASE        */
36 /*                                                                        */
37 /*    _nx_udp_socket_extract                              PORTABLE C      */
38 /*                                                           6.1          */
39 /*  AUTHOR                                                                */
40 /*                                                                        */
41 /*    Yuxin Zhou, Microsoft Corporation                                   */
42 /*                                                                        */
43 /*  DESCRIPTION                                                           */
44 /*                                                                        */
45 /*    This function extracts the source IP and UDP port number from the   */
46 /*    supplied packet.                                                    */
47 /*                                                                        */
48 /*  INPUT                                                                 */
49 /*                                                                        */
50 /*    packet_ptr                            Pointer to UDP packet pointer */
51 /*    ip_address                            Pointer to packet source IP   */
52 /*                                            address                     */
53 /*    port                                  Pointer to source UDP port    */
54 /*                                                                        */
55 /*  OUTPUT                                                                */
56 /*                                                                        */
57 /*    NX_SUCCESS                            Successful completion status  */
58 /*                                                                        */
59 /*  CALLS                                                                 */
60 /*                                                                        */
61 /*    None                                                                */
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 /**************************************************************************/
_nx_udp_source_extract(NX_PACKET * packet_ptr,ULONG * ip_address,UINT * port)76 UINT  _nx_udp_source_extract(NX_PACKET *packet_ptr, ULONG *ip_address, UINT *port)
77 {
78 
79 #ifndef NX_DISABLE_IPV4
80 ULONG          *temp_ptr;
81 NX_IPV4_HEADER *ipv4_header;
82 
83     /* Build an address to the current top of the packet.  */
84     /*lint -e{927} -e{826} suppress cast of pointer to pointer, since it is necessary  */
85     temp_ptr =  (ULONG *)packet_ptr -> nx_packet_prepend_ptr;
86 
87     /* Pickup the source port.  */
88     *port =  (UINT)(*(temp_ptr - 2) >> NX_SHIFT_BY_16);
89 
90     /* Obtain the IPv4 header. */
91     /*lint -e{927} -e{826} suppress cast of pointer to pointer, since it is necessary  */
92     ipv4_header = (NX_IPV4_HEADER *)packet_ptr -> nx_packet_ip_header;
93 
94     /* Pickup the source IP address.  */
95     *ip_address =  ipv4_header -> nx_ip_header_source_ip;
96 
97     /* If trace is enabled, insert this event into the trace buffer.  */
98     NX_TRACE_IN_LINE_INSERT(NX_TRACE_UDP_SOURCE_EXTRACT, packet_ptr, *ip_address, *port, 0, NX_TRACE_UDP_EVENTS, 0, 0);
99 
100     return(NX_SUCCESS);
101 #else
102     NX_PARAMETER_NOT_USED(packet_ptr);
103     NX_PARAMETER_NOT_USED(ip_address);
104     NX_PARAMETER_NOT_USED(port);
105 
106     return(NX_NOT_SUPPORTED);
107 #endif /* !NX_DISABLE_IPV4  */
108 }
109 
110