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