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