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 Secure Component                                                 */
16 /**                                                                       */
17 /**    Datagram Transport Layer Security (DTLS)                           */
18 /**                                                                       */
19 /**************************************************************************/
20 /**************************************************************************/
21 
22 #define NX_SECURE_SOURCE_CODE
23 
24 #include "nx_secure_dtls.h"
25 
26 /**************************************************************************/
27 /*                                                                        */
28 /*  FUNCTION                                               RELEASE        */
29 /*                                                                        */
30 /*    _nx_secure_dtls_server_delete                       PORTABLE C      */
31 /*                                                           6.1          */
32 /*  AUTHOR                                                                */
33 /*                                                                        */
34 /*    Timothy Stapko, Microsoft Corporation                               */
35 /*                                                                        */
36 /*  DESCRIPTION                                                           */
37 /*                                                                        */
38 /*    This function frees up the resources used by a DTLS server instance */
39 /*    when that instance is no longer needed by the application.          */
40 /*                                                                        */
41 /*  INPUT                                                                 */
42 /*                                                                        */
43 /*    server_ptr                            DTLS server control block     */
44 /*                                                                        */
45 /*  OUTPUT                                                                */
46 /*                                                                        */
47 /*    status                                Completion status             */
48 /*                                                                        */
49 /*  CALLS                                                                 */
50 /*                                                                        */
51 /*    _nx_secure_dtls_session_delete        Delete DTLS session           */
52 /*    nx_udp_socket_unbind                  Unbind the UDP socket         */
53 /*    nx_udp_socket_delete                  Delete the UDP socket         */
54 /*    tx_mutex_get                          Get protection mutex          */
55 /*    tx_mutex_put                          Put protection mutex          */
56 /*                                                                        */
57 /*  CALLED BY                                                             */
58 /*                                                                        */
59 /*    Application Code                                                    */
60 /*                                                                        */
61 /*  RELEASE HISTORY                                                       */
62 /*                                                                        */
63 /*    DATE              NAME                      DESCRIPTION             */
64 /*                                                                        */
65 /*  05-19-2020     Timothy Stapko           Initial Version 6.0           */
66 /*  09-30-2020     Timothy Stapko           Modified comment(s),          */
67 /*                                            resulting in version 6.1    */
68 /*                                                                        */
69 /**************************************************************************/
_nx_secure_dtls_server_delete(NX_SECURE_DTLS_SERVER * server_ptr)70 UINT _nx_secure_dtls_server_delete(NX_SECURE_DTLS_SERVER *server_ptr)
71 {
72 #ifdef NX_SECURE_ENABLE_DTLS
73 UINT i;
74 UINT status = NX_SUCCESS;
75 
76     /* Loop through all DTLS sessions and delete. */
77     for(i = 0; i < server_ptr->nx_dtls_server_sessions_count; ++i)
78     {
79         _nx_secure_dtls_session_delete(&(server_ptr -> nx_dtls_server_sessions[i]));
80     }
81 
82     /* Delete the UDP socket used by the server. */
83     status = nx_udp_socket_delete(&(server_ptr -> nx_dtls_server_udp_socket));
84 
85     if (status)
86     {
87         return(status);
88     }
89 
90     /* Get the protection. */
91     tx_mutex_get(&_nx_secure_tls_protection, TX_WAIT_FOREVER);
92 
93     /* Remove the DTLS server instance from the created list. */
94     /* See if the DTLS server instance is the only one on the list. */
95     if (server_ptr == server_ptr -> nx_dtls_server_created_next)
96     {
97 
98         /* Only created DTLS server instance, just set the created list to NULL. */
99         _nx_secure_dtls_server_created_ptr = NX_NULL;
100     }
101     else
102     {
103 
104         /* Otherwise, not the only created DTLS server, link-up the neighbors. */
105         if (server_ptr -> nx_dtls_server_created_next != NX_NULL)
106         {
107             (server_ptr -> nx_dtls_server_created_next) -> nx_dtls_server_created_previous =
108                     server_ptr -> nx_dtls_server_created_previous;
109         }
110 
111         (server_ptr -> nx_dtls_server_created_previous) -> nx_dtls_server_created_next =
112             server_ptr -> nx_dtls_server_created_next;
113 
114         /* See if we have to update the created list head pointer. */
115         if (_nx_secure_dtls_server_created_ptr == server_ptr)
116         {
117 
118             /* Yes, move the head pointer to the next link. */
119             _nx_secure_dtls_server_created_ptr = server_ptr -> nx_dtls_server_created_next;
120         }
121     }
122     _nx_secure_dtls_server_created_count--;
123 
124     /* Release the protection. */
125     tx_mutex_put(&_nx_secure_tls_protection);
126 
127     return(NX_SUCCESS);
128 #else
129     NX_PARAMETER_NOT_USED(server_ptr);
130 
131     return(NX_NOT_SUPPORTED);
132 #endif /* NX_SECURE_ENABLE_DTLS */
133 }
134 
135