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