summaryrefslogtreecommitdiff
path: root/common/src/nx_tcp_socket_connection_reset.c
blob: 978ecb19683a4e97c4c4b9ca67e0df3f475f6164 (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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/***************************************************************************
 * 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 Component                                                        */
/**                                                                       */
/**   Transmission Control Protocol (TCP)                                 */
/**                                                                       */
/**************************************************************************/
/**************************************************************************/

#define NX_SOURCE_CODE


/* Include necessary system files.  */

#include "nx_api.h"
#include "nx_ip.h"
#include "nx_tcp.h"
#include "nx_ipv6.h"

/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _nx_tcp_socket_connection_reset                     PORTABLE C      */
/*                                                           6.1.12       */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Yuxin Zhou, Microsoft Corporation                                   */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function processes a reset (RST) request received from the     */
/*    other side of the connection.                                       */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    socket_ptr                            Pointer to socket             */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    None                                                                */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    _nx_tcp_socket_block_cleanup          Cleanup the socket block      */
/*    _nx_tcp_socket_transmit_queue_flush   Release transmitted packets   */
/*    _nx_tcp_socket_receive_queue_flush    Release received packets      */
/*    _nx_tcp_connect_cleanup               Resume thread suspended       */
/*                                            waiting for connection      */
/*    _nx_tcp_disconnect_cleanup            Resume thread suspended       */
/*                                            waiting for disconnection   */
/*    _nx_tcp_receive_cleanup               Resume threads suspended on   */
/*                                            the receive queue           */
/*    _nx_tcp_transmit_cleanup              Resume threads suspended on   */
/*                                            the transmit queue          */
/*    (application disconnect callback)                                   */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    _nx_tcp_fast_periodic_processing      Transmit retry timeout        */
/*    _nx_tcp_periodic_processing           Keepalive processing          */
/*    _nx_tcp_socket_packet_process         Socket packet processing      */
/*                                                                        */
/**************************************************************************/
VOID  _nx_tcp_socket_connection_reset(NX_TCP_SOCKET *socket_ptr)
{

UINT saved_state;

    /* Save the current state of the socket.  */
    saved_state =  socket_ptr -> nx_tcp_socket_state;

    /* Cleanup the transmission control block.  */
    _nx_tcp_socket_block_cleanup(socket_ptr);

    /* Check for queued sent packets and if found they need
       to be released.  */
    if (socket_ptr -> nx_tcp_socket_transmit_sent_count)
    {

        /* Release all transmit packets.  */
        _nx_tcp_socket_transmit_queue_flush(socket_ptr);
    }

    /* Clear all receive thread suspensions on this socket.  */
    while (socket_ptr -> nx_tcp_socket_receive_suspension_list)
    {

        /* Call the receive thread suspension cleanup routine.  */
        _nx_tcp_receive_cleanup(socket_ptr -> nx_tcp_socket_receive_suspension_list NX_CLEANUP_ARGUMENT);
    }

    /* Clear all transmit thread suspensions on this socket.  */
    while (socket_ptr -> nx_tcp_socket_transmit_suspension_list)
    {

        /* Call the receive thread suspension cleanup routine.  */
        _nx_tcp_transmit_cleanup(socket_ptr -> nx_tcp_socket_transmit_suspension_list NX_CLEANUP_ARGUMENT);
    }

    /* Check for suspended connect thread.  */
    if (socket_ptr -> nx_tcp_socket_connect_suspended_thread)
    {

        /* Call the connect thread suspension cleanup routine.  */
        _nx_tcp_connect_cleanup(socket_ptr -> nx_tcp_socket_connect_suspended_thread NX_CLEANUP_ARGUMENT);
    }

    /* Check for suspended disconnect thread.  */
    if (socket_ptr -> nx_tcp_socket_disconnect_suspended_thread)
    {

        /* Resume the thread suspended on the disconnect.  */
        _nx_tcp_disconnect_cleanup(socket_ptr -> nx_tcp_socket_disconnect_suspended_thread NX_CLEANUP_ARGUMENT);
    }

    /* Determine if the socket was in an established state.  */
    if (saved_state == NX_TCP_ESTABLISHED)
    {

        /* If given, call the application's disconnect callback function
           for disconnect.  */
        if (socket_ptr -> nx_tcp_disconnect_callback)
        {

            /* Call the application's disconnect handling function.  It is
               responsible for calling the socket disconnect function.  */
            (socket_ptr -> nx_tcp_disconnect_callback)(socket_ptr);
        }
    }

#ifndef NX_DISABLE_EXTENDED_NOTIFY_SUPPORT

    /* Is a disconnect complete callback registered with the TCP socket? */
    if (socket_ptr -> nx_tcp_disconnect_complete_notify)
    {

        /* Notify the application through the socket disconnect_complete callback.  */
        (socket_ptr -> nx_tcp_disconnect_complete_notify)(socket_ptr);
    }
#endif
}