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