sptk2 logo
SPTK Home Page
SSLSocket.h
1/*
2╔══════════════════════════════════════════════════════════════════════════════╗
3║ SIMPLY POWERFUL TOOLKIT (SPTK) ║
4╟──────────────────────────────────────────────────────────────────────────────╢
5║ copyright © 1999-2022 Alexey Parshin. All rights reserved. ║
6║ email alexeyp@gmail.com ║
7╚══════════════════════════════════════════════════════════════════════════════╝
8┌──────────────────────────────────────────────────────────────────────────────┐
9│ This library is free software; you can redistribute it and/or modify it │
10│ under the terms of the GNU Library General Public License as published by │
11│ the Free Software Foundation; either version 2 of the License, or (at your │
12│ option) any later version. │
13│ │
14│ This library is distributed in the hope that it will be useful, but │
15│ WITHOUT ANY WARRANTY; without even the implied warranty of │
16│ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library │
17│ General Public License for more details. │
18│ │
19│ You should have received a copy of the GNU Library General Public License │
20│ along with this library; if not, write to the Free Software Foundation, │
21│ Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. │
22│ │
23│ Please report all bugs and problems to alexeyp@gmail.com. │
24└──────────────────────────────────────────────────────────────────────────────┘
25*/
26
27#pragma once
28
29#include <sptk5/sptk.h>
30
31#include <memory>
32#include <sptk5/net/SSLContext.h>
33#include <sptk5/net/SSLKeys.h>
34#include <sptk5/net/TCPSocket.h>
35
36namespace sptk {
37
46class SP_EXPORT SSLSocket : public TCPSocket
47 , public std::mutex
48{
49public:
53 size_t socketBytes() override;
54
60 [[noreturn]] void throwSSLError(const String& function, int resultCode) const;
61
66 explicit SSLSocket(const String& cipherList = "ALL");
67
71 ~SSLSocket() override;
72
84 void loadKeys(const SSLKeys& keys);
85
91 void setSNIHostName(const String& sniHostName);
92
99 void attach(SOCKET socketHandle, bool accept) override;
100
106 void close() noexcept override;
107
111 SSL* handle()
112 {
113 return m_ssl;
114 }
115
122 size_t recv(uint8_t* buffer, size_t size) override;
123
130 size_t send(const uint8_t* buffer, size_t len) override;
131
132protected:
137
148 void _open(const Host& host, OpenMode openMode, bool blockingMode, std::chrono::milliseconds timeout) override;
149
157 void _open(const struct sockaddr_in& address, OpenMode openMode, bool blockingMode, std::chrono::milliseconds timeout) override;
158
165 virtual String getSSLError(const std::string& function, int32_t SSLError) const;
166
167private:
168 SharedSSLContext m_sslContext {nullptr};
169 SSL* m_ssl {nullptr};
170 SSLKeys m_keys;
171
172 String m_sniHostName;
173 String m_cipherList;
174
175 void openSocketFD(bool blockingMode, const std::chrono::milliseconds& timeout);
176
177 bool tryConnect(const DateTime& timeoutAt);
178};
179
183} // namespace sptk
OpenMode
Definition: BaseSocket.h:93
Definition: Host.h:55
Definition: SSLKeys.h:38
Definition: SSLSocket.h:48
~SSLSocket() override
void close() noexcept override
void initContextAndSocket()
SSLSocket(const String &cipherList="ALL")
size_t recv(uint8_t *buffer, size_t size) override
void _open(const struct sockaddr_in &address, OpenMode openMode, bool blockingMode, std::chrono::milliseconds timeout) override
void _open(const Host &host, OpenMode openMode, bool blockingMode, std::chrono::milliseconds timeout) override
void loadKeys(const SSLKeys &keys)
size_t send(const uint8_t *buffer, size_t len) override
virtual String getSSLError(const std::string &function, int32_t SSLError) const
void attach(SOCKET socketHandle, bool accept) override
size_t socketBytes() override
void setSNIHostName(const String &sniHostName)
void throwSSLError(const String &function, int resultCode) const
Definition: String.h:49
Definition: TCPSocket.h:165

Fri Oct 14 2022 09:58:32: SPTK 5.4.1