summaryrefslogtreecommitdiff
path: root/nx_secure/src/nx_secure_dtls_server_delete.c
blob: f5359983da07df17c65a65d347438daee4f8ba8b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/***************************************************************************
 * Copyright (c) 2024 Microsoft Corporation
 * Copyright (c) 2025-present Eclipse ThreadX Contributors
 *
 * This program and the accompanying materials are made available under the
 * terms of the MIT License which is available at
 * https://opensource.org/licenses/MIT.
 *
 * SPDX-License-Identifier: MIT
 **************************************************************************/


/**************************************************************************/
/**************************************************************************/
/**                                                                       */
/** NetX Secure Component                                                 */
/**                                                                       */
/**    Datagram Transport Layer Security (DTLS)                           */
/**                                                                       */
/**************************************************************************/
/**************************************************************************/

#define NX_SECURE_SOURCE_CODE

#include "nx_secure_dtls.h"

/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _nx_secure_dtls_server_delete                       PORTABLE C      */
/*                                                           6.4.3        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Timothy Stapko, Microsoft Corporation                               */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function frees up the resources used by a DTLS server instance */
/*    when that instance is no longer needed by the application.          */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    server_ptr                            DTLS server control block     */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    status                                Completion status             */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    _nx_secure_dtls_session_delete        Delete DTLS session           */
/*    nx_udp_socket_unbind                  Unbind the UDP socket         */
/*    nx_udp_socket_delete                  Delete the UDP socket         */
/*    tx_mutex_get                          Get protection mutex          */
/*    tx_mutex_put                          Put protection mutex          */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    Application Code                                                    */
/*                                                                        */
/**************************************************************************/
UINT _nx_secure_dtls_server_delete(NX_SECURE_DTLS_SERVER *server_ptr)
{
#ifdef NX_SECURE_ENABLE_DTLS
UINT i;
UINT status = NX_SUCCESS;

    /* Loop through all DTLS sessions and delete. */
    for(i = 0; i < server_ptr->nx_dtls_server_sessions_count; ++i)
    {
        _nx_secure_dtls_session_delete(&(server_ptr -> nx_dtls_server_sessions[i]));
    }

    /* Delete the UDP socket used by the server. */
    status = nx_udp_socket_delete(&(server_ptr -> nx_dtls_server_udp_socket));

    if (status)
    {
        return(status);
    }

    /* Get the protection. */
    tx_mutex_get(&_nx_secure_tls_protection, TX_WAIT_FOREVER);

    /* Remove the DTLS server instance from the created list. */
    /* See if the DTLS server instance is the only one on the list. */
    if (server_ptr == server_ptr -> nx_dtls_server_created_next)
    {

        /* Only created DTLS server instance, just set the created list to NULL. */
        _nx_secure_dtls_server_created_ptr = NX_NULL;
    }
    else
    {

        /* Otherwise, not the only created DTLS server, link-up the neighbors. */
        if (server_ptr -> nx_dtls_server_created_next != NX_NULL)
        {
            (server_ptr -> nx_dtls_server_created_next) -> nx_dtls_server_created_previous =
                    server_ptr -> nx_dtls_server_created_previous;
        }

        (server_ptr -> nx_dtls_server_created_previous) -> nx_dtls_server_created_next =
            server_ptr -> nx_dtls_server_created_next;

        /* See if we have to update the created list head pointer. */
        if (_nx_secure_dtls_server_created_ptr == server_ptr)
        {

            /* Yes, move the head pointer to the next link. */
            _nx_secure_dtls_server_created_ptr = server_ptr -> nx_dtls_server_created_next;
        }
    }
    _nx_secure_dtls_server_created_count--;

    /* Release the protection. */
    tx_mutex_put(&_nx_secure_tls_protection);

    return(NX_SUCCESS);
#else
    NX_PARAMETER_NOT_USED(server_ptr);

    return(NX_NOT_SUPPORTED);
#endif /* NX_SECURE_ENABLE_DTLS */
}