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 /**   User Datagram Protocol (UDP)                                        */
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_udp.h"
30 #include "nx_ipv6.h"
31 #include "nx_ipv4.h"
32 
33 
34 /**************************************************************************/
35 /*                                                                        */
36 /*  FUNCTION                                               RELEASE        */
37 /*                                                                        */
38 /*    _nxd_udp_socket_extract                             PORTABLE C      */
39 /*                                                           6.1          */
40 /*  AUTHOR                                                                */
41 /*                                                                        */
42 /*    Yuxin Zhou, Microsoft Corporation                                   */
43 /*                                                                        */
44 /*  DESCRIPTION                                                           */
45 /*                                                                        */
46 /*    This function extracts the source IP address and UDP port number    */
47 /*    from the packet received on a host UDP socket. It is up to the      */
48 /*    caller to verify the source port and ip address are valid (non null)*/
49 /*    data.                                                               */
50 /*                                                                        */
51 /*  INPUT                                                                 */
52 /*                                                                        */
53 /*    packet_ptr                            Pointer to UDP packet pointer */
54 /*    ip_address                            Pointer to source IP address  */
55 /*    port                                  Pointer to source UDP port    */
56 /*                                                                        */
57 /*  OUTPUT                                                                */
58 /*                                                                        */
59 /*    status                                Actual completion status      */
60 /*                                                                        */
61 /*  CALLS                                                                 */
62 /*                                                                        */
63 /*    None                                                                */
64 /*                                                                        */
65 /*  CALLED BY                                                             */
66 /*                                                                        */
67 /*    Application Code                                                    */
68 /*                                                                        */
69 /*  RELEASE HISTORY                                                       */
70 /*                                                                        */
71 /*    DATE              NAME                      DESCRIPTION             */
72 /*                                                                        */
73 /*  05-19-2020     Yuxin Zhou               Initial Version 6.0           */
74 /*  09-30-2020     Yuxin Zhou               Modified comment(s),          */
75 /*                                            resulting in version 6.1    */
76 /*                                                                        */
77 /**************************************************************************/
_nxd_udp_source_extract(NX_PACKET * packet_ptr,NXD_ADDRESS * ip_address,UINT * port)78 UINT  _nxd_udp_source_extract(NX_PACKET *packet_ptr, NXD_ADDRESS *ip_address, UINT *port)
79 {
80 
81 #ifdef TX_ENABLE_EVENT_TRACE
82 ULONG  ip_address_word3 = 0;
83 ULONG  ip_version;
84 #endif
85 ULONG *temp_ptr;
86 
87 
88     /* Build an address to the current top of the packet.  */
89     /*lint -e{927} -e{826} suppress cast of pointer to pointer, since it is necessary  */
90     temp_ptr =  (ULONG *)packet_ptr -> nx_packet_prepend_ptr;
91 
92     /* Pickup the source port from the UDP header.  */
93     *port =  (UINT)(*(temp_ptr - 2) >> NX_SHIFT_BY_16);
94 
95     /* Determine IPv4 or IPv6 connectivity. */
96     ip_address -> nxd_ip_version = packet_ptr -> nx_packet_ip_version;
97 
98 #ifndef NX_DISABLE_IPV4
99     if (ip_address -> nxd_ip_version == NX_IP_VERSION_V4)
100     {
101 
102     NX_IPV4_HEADER *ipv4_header;
103 
104         /* Obtain the IPv4 header. */
105         /*lint -e{927} -e{826} suppress cast of pointer to pointer, since it is necessary  */
106         ipv4_header = (NX_IPV4_HEADER *)packet_ptr -> nx_packet_ip_header;
107 
108         /* Pickup the source IP address.  */
109         ip_address -> nxd_ip_address.v4 =  ipv4_header -> nx_ip_header_source_ip;
110 
111 #ifdef TX_ENABLE_EVENT_TRACE
112         ip_version = NX_IP_VERSION_V4;
113         ip_address_word3 = ip_address -> nxd_ip_address.v4;
114 #endif /* TX_ENABLE_EVENT_TRACE */
115     }
116 #endif /* NX_DISABLE_IPV4 */
117 #ifdef FEATURE_NX_IPV6
118     if (ip_address -> nxd_ip_version == NX_IP_VERSION_V6)
119     {
120     NX_IPV6_HEADER *ipv6_header;
121 
122 
123         /* Obtain the IPv6 header. */
124         /*lint -e{927} -e{826} suppress cast of pointer to pointer, since it is necessary  */
125         ipv6_header = (NX_IPV6_HEADER *)packet_ptr -> nx_packet_ip_header;
126 
127         COPY_IPV6_ADDRESS(ipv6_header -> nx_ip_header_source_ip,
128                           ip_address -> nxd_ip_address.v6);
129 
130 #ifdef TX_ENABLE_EVENT_TRACE
131         ip_version = NX_IP_VERSION_V6;
132         ip_address_word3 = ip_address -> nxd_ip_address.v6[3];
133 #endif /* TX_ENABLE_EVENT_TRACE */
134     }
135 #endif /* FEATURE_NX_IPV6 */
136 
137 #ifdef TX_ENABLE_EVENT_TRACE
138 
139     /* If trace is enabled, insert this event into the trace buffer.  */
140     NX_TRACE_IN_LINE_INSERT(NXD_TRACE_UDP_SOURCE_EXTRACT, packet_ptr, ip_version, ip_address_word3, *port, NX_TRACE_UDP_EVENTS, 0, 0);
141 #endif /* TX_ENABLE_EVENT_TRACE */
142 
143     return(NX_SUCCESS);
144 }
145 
146