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 
30 
31 /**************************************************************************/
32 /*                                                                        */
33 /*  FUNCTION                                               RELEASE        */
34 /*                                                                        */
35 /*    _nxd_udp_socket_source_send                         PORTABLE C      */
36 /*                                                           6.1          */
37 /*  AUTHOR                                                                */
38 /*                                                                        */
39 /*    Yuxin Zhou, Microsoft Corporation                                   */
40 /*                                                                        */
41 /*  DESCRIPTION                                                           */
42 /*                                                                        */
43 /*    This function sends UDP packet using specified local interface      */
44 /*    IPv4 or IPv6 address as source.                                     */
45 /*                                                                        */
46 /*  INPUT                                                                 */
47 /*                                                                        */
48 /*    socket_ptr                            Pointer to UDP socket         */
49 /*    packet_ptr                            Pointer to UDP packet         */
50 /*    ip_address                            IP address                    */
51 /*    port                                  16-bit UDP port number        */
52 /*    address_index                         Index of IPv4 or IPv6 address */
53 /*                                            to use as the source address*/
54 /*                                                                        */
55 /*  OUTPUT                                                                */
56 /*                                                                        */
57 /*    status                                Completion status             */
58 /*                                                                        */
59 /*  CALLS                                                                 */
60 /*                                                                        */
61 /*    _nxd_udp_socket_send                  Send the UDP packet           */
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 /**************************************************************************/
_nxd_udp_socket_source_send(NX_UDP_SOCKET * socket_ptr,NX_PACKET * packet_ptr,NXD_ADDRESS * ip_address,UINT port,UINT address_index)76 UINT  _nxd_udp_socket_source_send(NX_UDP_SOCKET *socket_ptr, NX_PACKET *packet_ptr,
77                                   NXD_ADDRESS *ip_address, UINT port, UINT address_index)
78 {
79 UINT   status;
80 NX_IP *ip_ptr;
81 
82     /* Setup the pointer to the associated IP instance.  */
83     ip_ptr =  socket_ptr -> nx_udp_socket_ip_ptr;
84 
85     /* Store address information into the packet structure. */
86 #ifndef NX_DISABLE_IPV4
87     if (ip_address -> nxd_ip_version == NX_IP_VERSION_V4)
88     {
89         packet_ptr -> nx_packet_address.nx_packet_interface_ptr = &(ip_ptr -> nx_ip_interface[address_index]);
90     }
91 #endif /* !NX_DISABLE_IPV4  */
92 
93 #ifdef FEATURE_NX_IPV6
94     if (ip_address -> nxd_ip_version == NX_IP_VERSION_V6)
95     {
96         packet_ptr -> nx_packet_address.nx_packet_ipv6_address_ptr = &ip_ptr -> nx_ipv6_address[address_index];
97 
98         if (packet_ptr -> nx_packet_address.nx_packet_ipv6_address_ptr -> nxd_ipv6_address_state != NX_IPV6_ADDR_STATE_VALID)
99         {
100             return(NX_NO_INTERFACE_ADDRESS);
101         }
102     }
103 #endif /* FEATURE_NX_IPV6 */
104 
105     /* Call udp_socket_send service */
106     status = _nxd_udp_socket_send(socket_ptr, packet_ptr, ip_address, port);
107 
108     return(status);
109 }
110 
111