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