summaryrefslogtreecommitdiff
path: root/common/src/nxd_udp_packet_info_extract.c
blob: ccd24d80a36cbd8039a6030c3b25a8fdf1bcb8fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/***************************************************************************
 * Copyright (c) 2024 Microsoft Corporation
 * Copyright (c) 2025-present Eclipse ThreadX Contributors
 *
 * This program and the accompanying materials are made available under the
 * terms of the MIT License which is available at
 * https://opensource.org/licenses/MIT.
 *
 * SPDX-License-Identifier: MIT
 **************************************************************************/


/**************************************************************************/
/**************************************************************************/
/**                                                                       */
/** NetX Component                                                        */
/**                                                                       */
/**   User Datagram Protocol (UDP)                                        */
/**                                                                       */
/**************************************************************************/
/**************************************************************************/

#define NX_SOURCE_CODE


/* Include necessary system files.  */

#include "nx_api.h"
#include "nx_udp.h"
#include "nx_ipv4.h"
#ifdef FEATURE_NX_IPV6
#include "nx_ipv6.h"
#endif


/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _nxd_udp_packet_info_extract                        PORTABLE C      */
/*                                                           6.4.3        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Yuxin Zhou, Microsoft Corporation                                   */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function extracts the source IP address, protocol, (the        */
/*    protocol is always UDP), port number and the incoming interface     */
/*    from the incoming packet.                                           */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    packet_ptr                            Pointer to UDP packet         */
/*    ip_address                            Pointer to sender IP address  */
/*    protocol                              Pointer to packet protocol.   */
/*                                            Always 17 (UDP)             */
/*    port                                  Pointer to sender source port */
/*    interface_index                         Pointer to interface index  */
/*                                            packet received on          */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    status                                Completion status             */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    None                                                                */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    Application Code                                                    */
/*                                                                        */
/**************************************************************************/
UINT  _nxd_udp_packet_info_extract(NX_PACKET *packet_ptr, NXD_ADDRESS *ip_address,
                                   UINT *protocol, UINT *port, UINT *interface_index)
{

ULONG          *temp_ptr;
UINT            source_port;
NX_INTERFACE   *nx_interface;
#ifndef NX_DISABLE_IPV4
NX_IPV4_HEADER *ipv4_header;
#endif /* !NX_DISABLE_IPV4  */
#ifdef TX_ENABLE_EVENT_TRACE
ULONG           address = 0;
#endif  /* TX_ENABLE_EVENT_TRACE */
#ifdef FEATURE_NX_IPV6
NX_IPV6_HEADER *ipv6_header;
#endif /* FEATURE_NX_IPV6 */


    if (ip_address)
    {

#ifndef NX_DISABLE_IPV4
        if (packet_ptr -> nx_packet_ip_version == NX_IP_VERSION_V4)
        {

            /*lint -e{927} -e{826} suppress cast of pointer to pointer, since it is necessary  */
            ipv4_header = (NX_IPV4_HEADER *)packet_ptr -> nx_packet_ip_header;

            ip_address -> nxd_ip_version = NX_IP_VERSION_V4;

            /* At this point, the IP address in the IPv4 header is in host byte order. */
            ip_address -> nxd_ip_address.v4 = ipv4_header -> nx_ip_header_source_ip;

#ifdef TX_ENABLE_EVENT_TRACE
            address = ip_address -> nxd_ip_address.v4;
#endif  /* TX_ENABLE_EVENT_TRACE */
        }
        else
#endif /* !NX_DISABLE_IPV4  */

#ifdef FEATURE_NX_IPV6
        if (packet_ptr -> nx_packet_ip_version == NX_IP_VERSION_V6)
        {

            /*lint -e{927} -e{826} suppress cast of pointer to pointer, since it is necessary  */
            ipv6_header = (NX_IPV6_HEADER *)packet_ptr -> nx_packet_ip_header;

            ip_address -> nxd_ip_version = NX_IP_VERSION_V6;

            /* At this point, the IP address in the IPv6 header is in host byte order. */
            COPY_IPV6_ADDRESS(ipv6_header -> nx_ip_header_source_ip, ip_address -> nxd_ip_address.v6);

#ifdef TX_ENABLE_EVENT_TRACE
            address = ip_address -> nxd_ip_address.v6[3];
#endif /* TX_ENABLE_EVENT_TRACE*/
        }
        else
#endif /* FEATURE_NX_IPV6 */
        {

            /* Invalid IP version . */
            return(NX_INVALID_PACKET);
        }
    }

    /* Build an address to the current top of the packet.  */
    /*lint -e{927} -e{826} suppress cast of pointer to pointer, since it is necessary  */
    temp_ptr =  (ULONG *)packet_ptr -> nx_packet_prepend_ptr;

    /* Pickup the source port.  */
    source_port =  (UINT)(*(temp_ptr - 2) >> NX_SHIFT_BY_16);
    if (port != NX_NULL)
    {
        *port = source_port;
    }

    if (protocol != NX_NULL)
    {
        *protocol = 0x11;
    }

    /* If trace is enabled, insert this event into the trace buffer.  */
    NX_TRACE_IN_LINE_INSERT(NX_TRACE_UDP_SOURCE_EXTRACT, packet_ptr, address, source_port, 0, NX_TRACE_PACKET_EVENTS, 0, 0);

    if (interface_index == NX_NULL)
    {
        return(NX_SUCCESS);
    }

    /* Search for interface index number.  Initialize interface value as
       invalid (0xFFFFFFFF).  Once we find valid interface, we will update
       the returned value. */
    *interface_index = 0xFFFFFFFF;

    if (packet_ptr -> nx_packet_ip_version == NX_IP_VERSION_V4)
    {
        nx_interface = packet_ptr -> nx_packet_address.nx_packet_interface_ptr;
    }
#ifdef FEATURE_NX_IPV6
    else if (packet_ptr -> nx_packet_ip_version == NX_IP_VERSION_V6)
    {
        if (packet_ptr -> nx_packet_address.nx_packet_ipv6_address_ptr == NX_NULL)
        {

            /* No interface attached.  Done here, and return success. */
            return(NX_SUCCESS);
        }
        else
        {
            nx_interface = packet_ptr -> nx_packet_address.nx_packet_ipv6_address_ptr -> nxd_ipv6_address_attached;
        }
    }
#endif /* FEATURE_NX_IPV6 */
    else
    {
        return(NX_SUCCESS);
    }

    if (nx_interface == NX_NULL)
    {

        /* No interface attached.  Done here, and return success. */
        return(NX_SUCCESS);
    }

    *interface_index = (UINT)nx_interface -> nx_interface_index;

    return(NX_SUCCESS);
}