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 /*    _nx_udp_socket_create                               PORTABLE C      */
37 /*                                                           6.1          */
38 /*  AUTHOR                                                                */
39 /*                                                                        */
40 /*    Yuxin Zhou, Microsoft Corporation                                   */
41 /*                                                                        */
42 /*  DESCRIPTION                                                           */
43 /*                                                                        */
44 /*    This function creates a UDP socket for the specified IP instance.   */
45 /*                                                                        */
46 /*  INPUT                                                                 */
47 /*                                                                        */
48 /*    ip_ptr                                IP instance pointer           */
49 /*    socket_ptr                            Pointer to new UDP socket     */
50 /*    name                                  Name of new UDP socket        */
51 /*    type_of_service                       Type of service for this UDP  */
52 /*                                            socket                      */
53 /*    fragment                              Flag to enable IP fragmenting */
54 /*    time_to_live                          Time to live value for socket */
55 /*    queue_maximum                         Maximum depth of receive queue*/
56 /*                                                                        */
57 /*  OUTPUT                                                                */
58 /*                                                                        */
59 /*    status                                Completion status             */
60 /*                                                                        */
61 /*  CALLS                                                                 */
62 /*                                                                        */
63 /*    tx_mutex_get                          Obtain protection mutex       */
64 /*    tx_mutex_put                          Release protection mutex      */
65 /*                                                                        */
66 /*  CALLED BY                                                             */
67 /*                                                                        */
68 /*    Application Code                                                    */
69 /*                                                                        */
70 /*  RELEASE HISTORY                                                       */
71 /*                                                                        */
72 /*    DATE              NAME                      DESCRIPTION             */
73 /*                                                                        */
74 /*  05-19-2020     Yuxin Zhou               Initial Version 6.0           */
75 /*  09-30-2020     Yuxin Zhou               Modified comment(s),          */
76 /*                                            resulting in version 6.1    */
77 /*                                                                        */
78 /**************************************************************************/
_nx_udp_socket_create(NX_IP * ip_ptr,NX_UDP_SOCKET * socket_ptr,CHAR * name,ULONG type_of_service,ULONG fragment,UINT time_to_live,ULONG queue_maximum)79 UINT  _nx_udp_socket_create(NX_IP *ip_ptr, NX_UDP_SOCKET *socket_ptr, CHAR *name,
80                             ULONG type_of_service, ULONG fragment, UINT time_to_live, ULONG queue_maximum)
81 {
82 TX_INTERRUPT_SAVE_AREA
83 
84 NX_UDP_SOCKET *tail_ptr;
85 
86 
87     /* Initialize the TCP control block to zero.  */
88     memset((void *)socket_ptr, 0, sizeof(NX_UDP_SOCKET));
89 
90     /* Fill in the basic information in the new UDP socket structure.  */
91 
92     /* Remember the associated IP structure.  */
93     socket_ptr -> nx_udp_socket_ip_ptr =  ip_ptr;
94 
95     /* Save the UDP socket's name.  */
96     socket_ptr -> nx_udp_socket_name =  name;
97 
98     /* Save the type of service input parameter.  */
99     socket_ptr -> nx_udp_socket_type_of_service =  type_of_service;
100 
101     /* Save the fragment input parameter.  */
102     socket_ptr -> nx_udp_socket_fragment_enable =  fragment & NX_DONT_FRAGMENT;
103 
104     /* Save the time-to-live input parameter.  */
105     socket_ptr -> nx_udp_socket_time_to_live =  time_to_live;
106 
107     /* By default, have UDP checksum logic enabled.  To disable checksum logic, the
108        application must call the nx_udp_checksum disable function for this UDP socket.  */
109     socket_ptr -> nx_udp_socket_disable_checksum =  NX_FALSE;
110 
111     /* Clear the socket bind in progress flag.  */
112     socket_ptr -> nx_udp_socket_bind_in_progress =  NX_FALSE;
113 
114     /* Set various list pointers to NULL.  */
115     socket_ptr -> nx_udp_socket_bound_next =            NX_NULL;
116     socket_ptr -> nx_udp_socket_bound_previous =        NX_NULL;
117     socket_ptr -> nx_udp_socket_bind_suspension_list =  NX_NULL;
118     socket_ptr -> nx_udp_socket_bind_suspended_count =  0;
119 
120     /* Initialize the receive queue parameters.  */
121     socket_ptr -> nx_udp_socket_receive_count =         0;
122     socket_ptr -> nx_udp_socket_queue_maximum =         queue_maximum;
123     socket_ptr -> nx_udp_socket_receive_head =          NX_NULL;
124     socket_ptr -> nx_udp_socket_receive_tail =          NX_NULL;
125 
126     /* Clear the receive notify function pointer.  */
127     socket_ptr -> nx_udp_receive_callback =             NX_NULL;
128 
129     /* If trace is enabled, register this object.  */
130     NX_TRACE_OBJECT_REGISTER(NX_TRACE_OBJECT_TYPE_UDP_SOCKET, socket_ptr, name, type_of_service, queue_maximum);
131 
132     /* If trace is enabled, insert this event into the trace buffer.  */
133     NX_TRACE_IN_LINE_INSERT(NX_TRACE_UDP_SOCKET_CREATE, ip_ptr, socket_ptr, type_of_service, queue_maximum, NX_TRACE_IP_EVENTS, 0, 0);
134 
135     /* Obtain the IP mutex so we can add socket to IP structure.  */
136     tx_mutex_get(&(ip_ptr -> nx_ip_protection), TX_WAIT_FOREVER);
137 
138     /* Disable interrupts while we link the new UDP socket to the IP structure.  */
139     TX_DISABLE
140 
141     /* Load the UDP ID field in the UDP control block.  */
142     socket_ptr -> nx_udp_socket_id =  NX_UDP_ID;
143 
144     /* Place the new UDP control block on the list of created UDP sockets for this IP.  First,
145        check for an empty list.  */
146     if (ip_ptr -> nx_ip_udp_created_sockets_ptr)
147     {
148 
149         /* Pickup tail pointer.  */
150         tail_ptr =  (ip_ptr -> nx_ip_udp_created_sockets_ptr) -> nx_udp_socket_created_previous;
151 
152         /* Place the new UDP socket control block in the list.  */
153         (ip_ptr -> nx_ip_udp_created_sockets_ptr) -> nx_udp_socket_created_previous =  socket_ptr;
154         tail_ptr ->  nx_udp_socket_created_next =  socket_ptr;
155 
156         /* Setup this UDP socket's created links.  */
157         socket_ptr -> nx_udp_socket_created_previous =  tail_ptr;
158         socket_ptr -> nx_udp_socket_created_next =      ip_ptr -> nx_ip_udp_created_sockets_ptr;
159     }
160     else
161     {
162 
163         /* The created UDP socket list is empty.  Add UDP socket control block to empty list.  */
164         ip_ptr -> nx_ip_udp_created_sockets_ptr =       socket_ptr;
165         socket_ptr -> nx_udp_socket_created_previous =  socket_ptr;
166         socket_ptr -> nx_udp_socket_created_next =      socket_ptr;
167     }
168 
169     /* Increment the created UDP socket counter.  */
170     ip_ptr -> nx_ip_udp_created_sockets_count++;
171 
172     /* Restore previous interrupt posture.  */
173     TX_RESTORE
174 
175     /* Release the IP protection mutex.  */
176     tx_mutex_put(&(ip_ptr -> nx_ip_protection));
177 
178     /* Return successful completion.  */
179     return(NX_SUCCESS);
180 }
181 
182