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 "tx_thread.h"
31 
32 
33 /**************************************************************************/
34 /*                                                                        */
35 /*  FUNCTION                                               RELEASE        */
36 /*                                                                        */
37 /*    _nx_udp_socket_delete                               PORTABLE C      */
38 /*                                                           6.1          */
39 /*  AUTHOR                                                                */
40 /*                                                                        */
41 /*    Yuxin Zhou, Microsoft Corporation                                   */
42 /*                                                                        */
43 /*  DESCRIPTION                                                           */
44 /*                                                                        */
45 /*    This function deletes a previously created socket and unbound       */
46 /*    socket.  If the socket is still bound, an error is returned.        */
47 /*                                                                        */
48 /*  INPUT                                                                 */
49 /*                                                                        */
50 /*    socket_ptr                            Pointer to UDP socket         */
51 /*                                                                        */
52 /*  OUTPUT                                                                */
53 /*                                                                        */
54 /*    status                                Completion status             */
55 /*                                                                        */
56 /*  CALLS                                                                 */
57 /*                                                                        */
58 /*    tx_mutex_get                          Obtain protection mutex       */
59 /*    tx_mutex_put                          Release protection mutex      */
60 /*    _tx_thread_system_preempt_check       Check for preemption          */
61 /*                                                                        */
62 /*  CALLED BY                                                             */
63 /*                                                                        */
64 /*    Application Code                                                    */
65 /*                                                                        */
66 /*  RELEASE HISTORY                                                       */
67 /*                                                                        */
68 /*    DATE              NAME                      DESCRIPTION             */
69 /*                                                                        */
70 /*  05-19-2020     Yuxin Zhou               Initial Version 6.0           */
71 /*  09-30-2020     Yuxin Zhou               Modified comment(s),          */
72 /*                                            resulting in version 6.1    */
73 /*                                                                        */
74 /**************************************************************************/
_nx_udp_socket_delete(NX_UDP_SOCKET * socket_ptr)75 UINT  _nx_udp_socket_delete(NX_UDP_SOCKET *socket_ptr)
76 {
77 
78 TX_INTERRUPT_SAVE_AREA
79 
80 NX_IP *ip_ptr;
81 
82 
83     /* Setup the pointer to the associated IP instance.  */
84     ip_ptr =  socket_ptr -> nx_udp_socket_ip_ptr;
85 
86     /* If trace is enabled, insert this event into the trace buffer.  */
87     NX_TRACE_IN_LINE_INSERT(NX_TRACE_UDP_SOCKET_DELETE, ip_ptr, socket_ptr, 0, 0, NX_TRACE_UDP_EVENTS, 0, 0);
88 
89     /* Obtain the IP mutex so we can process the socket delete request.  */
90     tx_mutex_get(&(ip_ptr -> nx_ip_protection), TX_WAIT_FOREVER);
91 
92     /* Determine if the socket is still bound to port.  */
93     if (socket_ptr -> nx_udp_socket_bound_next)
94     {
95 
96         /* Release the protection mutex.  */
97         tx_mutex_put(&(ip_ptr -> nx_ip_protection));
98 
99         /* Return a still bound error code.  */
100         return(NX_STILL_BOUND);
101     }
102 
103     /* Disable interrupts.  */
104     TX_DISABLE
105 
106     /* Now, remove the UDP socket from the created socket list.  */
107 
108     /* Clear the socket ID to make it invalid.  */
109     socket_ptr -> nx_udp_socket_id =  0;
110 
111     /* See if the socket is the only one on the list.  */
112     if (socket_ptr == socket_ptr -> nx_udp_socket_created_next)
113     {
114 
115         /* Only created socket, just set the created list to NULL.  */
116         ip_ptr -> nx_ip_udp_created_sockets_ptr =  NX_NULL;
117     }
118     else
119     {
120 
121         /* Link-up the neighbors.  */
122         (socket_ptr -> nx_udp_socket_created_next) -> nx_udp_socket_created_previous =
123             socket_ptr -> nx_udp_socket_created_previous;
124         (socket_ptr -> nx_udp_socket_created_previous) -> nx_udp_socket_created_next =
125             socket_ptr -> nx_udp_socket_created_next;
126 
127         /* See if we have to update the created list head pointer.  */
128         if (ip_ptr -> nx_ip_udp_created_sockets_ptr == socket_ptr)
129         {
130 
131             /* Yes, move the head pointer to the next link. */
132             ip_ptr -> nx_ip_udp_created_sockets_ptr =  socket_ptr -> nx_udp_socket_created_next;
133         }
134     }
135 
136     /* Decrease the created sockets count.  */
137     ip_ptr -> nx_ip_udp_created_sockets_count--;
138 
139     /* Restore interrupts.  */
140     TX_RESTORE
141 
142     /* If trace is enabled, unregister this object.  */
143     NX_TRACE_OBJECT_UNREGISTER(socket_ptr);
144 
145     /* Release the IP protection mutex.  */
146     tx_mutex_put(&(ip_ptr -> nx_ip_protection));
147 
148     /* Check for preemption.  */
149     _tx_thread_system_preempt_check();
150 
151     /* Return success.  */
152     return(NX_SUCCESS);
153 }
154 
155