SNode.C
Loading...
Searching...
No Matches
TLSShutdown.cpp
Go to the documentation of this file.
1/*
2 * SNode.C - a slim toolkit for network communication
3 * Copyright (C) Volker Christian <me@vchrist.at>
4 * 2020, 2021, 2022, 2023, 2024, 2025
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published
8 * by the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include "core/socket/stream/tls/TLSShutdown.h"
21
22#ifndef DOXYGEN_SHOULD_SKIP_THIS
23
24#include <openssl/ssl.h>
25
26#endif /* DOXYGEN_SHOULD_SKIP_THIS */
27
28namespace core::socket::stream::tls {
29
30 void TLSShutdown::doShutdown(const std::string& instanceName,
31 SSL* ssl,
32 const std::function<void(void)>& onSuccess,
33 const std::function<void(void)>& onTimeout,
34 const std::function<void(int)>& onStatus,
35 const utils::Timeval& timeout) {
36 new TLSShutdown(instanceName, ssl, onSuccess, onTimeout, onStatus, timeout);
37 }
38
39 TLSShutdown::TLSShutdown(const std::string& instanceName,
40 SSL* ssl,
41 const std::function<void(void)>& onSuccess,
42 const std::function<void(void)>& onTimeout,
43 const std::function<void(int)>& onStatus,
44 const utils::Timeval& timeout)
45 : ReadEventReceiver(instanceName + " SSL/TLS: Send close_notify", timeout)
46 , WriteEventReceiver(instanceName + " SSL/TLS: Send close_notify", timeout)
47 , ssl(ssl)
51 , timeoutTriggered(false)
52 , fd(SSL_get_fd(ssl)) {
53 const int ret = SSL_shutdown(ssl);
54
55 int sslErr = SSL_ERROR_NONE;
56 if (ret < 0) {
57 sslErr = SSL_get_error(ssl, ret);
58 }
59
61 delete this;
62 } else if (!WriteEventReceiver::enable(fd)) {
64 } else {
67
68 switch (sslErr) {
69 case SSL_ERROR_WANT_READ:
71 break;
72 case SSL_ERROR_WANT_WRITE:
74 break;
75 case SSL_ERROR_NONE:
76 case SSL_ERROR_ZERO_RETURN:
79 onSuccess();
80 break;
81 default:
84 onStatus(sslErr);
85 break;
86 }
87 }
88 }
89
91 const int ret = SSL_shutdown(ssl);
92
93 int sslErr = SSL_ERROR_NONE;
94 if (ret < 0) {
95 sslErr = SSL_get_error(ssl, ret);
96 }
97
98 switch (sslErr) {
99 case SSL_ERROR_WANT_READ:
100 break;
101 case SSL_ERROR_WANT_WRITE:
104 break;
105 case SSL_ERROR_NONE:
106 case SSL_ERROR_ZERO_RETURN:
109 onSuccess();
110 break;
111 default:
114 onStatus(sslErr);
115 break;
116 }
117 }
118
120 const int ret = SSL_shutdown(ssl);
121
122 int sslErr = SSL_ERROR_NONE;
123 if (ret < 0) {
124 sslErr = SSL_get_error(ssl, ret);
125 }
126
127 switch (sslErr) {
128 case SSL_ERROR_WANT_READ:
131 break;
132 case SSL_ERROR_WANT_WRITE:
133 break;
134 case SSL_ERROR_NONE:
135 case SSL_ERROR_ZERO_RETURN:
138 onSuccess();
139 break;
140 default:
143 onStatus(sslErr);
144 break;
145 }
146 }
147
149 if (!timeoutTriggered) {
150 timeoutTriggered = true;
153 onTimeout();
154 }
155 }
156
158 if (!timeoutTriggered) {
159 timeoutTriggered = true;
162 onTimeout();
163 }
164 }
165
166 void TLSShutdown::signalEvent([[maybe_unused]] int signum) { // Do nothing on signal event
167 }
168
170 delete this;
171 }
172
173} // namespace core::socket::stream::tls
static void doShutdown(const std::string &instanceName, SSL *ssl, const std::function< void(void)> &onSuccess, const std::function< void(void)> &onTimeout, const std::function< void(int)> &onStatus, const utils::Timeval &timeout)
TLSShutdown(const std::string &instanceName, SSL *ssl, const std::function< void(void)> &onSuccess, const std::function< void(void)> &onTimeout, const std::function< void(int)> &onStatus, const utils::Timeval &timeout)