blob: e92e24986f096a111c0064f9744ec4b5d86dde85 [file] [log] [blame]
Adrien Béraud612b55b2023-05-29 10:42:04 -04001/*
2 * Copyright (C) 2004-2023 Savoir-faire Linux Inc.
3 *
Adrien Béraudcb753622023-07-17 22:32:49 -04004 * This program is free software: you can redistribute it and/or modify
Adrien Béraud612b55b2023-05-29 10:42:04 -04005 * it under the terms of the GNU General Public License as published by
Adrien Béraudcb753622023-07-17 22:32:49 -04006 * the Free Software Foundation, either version 3 of the License, or
Adrien Béraud612b55b2023-05-29 10:42:04 -04007 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Adrien Béraudcb753622023-07-17 22:32:49 -040011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Adrien Béraud612b55b2023-05-29 10:42:04 -040012 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
Adrien Béraudcb753622023-07-17 22:32:49 -040015 * along with this program. If not, see <https://www.gnu.org/licenses/>.
Adrien Béraud612b55b2023-05-29 10:42:04 -040016 */
Adrien Béraud612b55b2023-05-29 10:42:04 -040017#pragma once
18
19#include "ip_utils.h"
20#include "certstore.h"
21#include "opendht/crypto.h"
22#include "ice_transport.h"
23#include "tls_session.h"
24
25#include <functional>
26#include <future>
27#include <limits>
28#include <map>
29#include <memory>
30#include <string>
31#include <utility>
32#include <vector>
33
34namespace dht {
35namespace crypto {
36struct PrivateKey;
37struct Certificate;
38} // namespace crypto
39} // namespace dht
40
Adrien Béraud1ae60aa2023-07-07 09:55:09 -040041namespace dhtnet {
Adrien Béraud612b55b2023-05-29 10:42:04 -040042namespace tls {
43class DhParams;
44}
45
46using OnStateChangeCb = std::function<bool(tls::TlsSessionState state)>;
47using OnReadyCb = std::function<void(bool ok)>;
48using onShutdownCb = std::function<void(void)>;
49
50//==============================================================================
51
52class IceSocketEndpoint : public GenericSocket<uint8_t>
53{
54public:
55 using SocketType = GenericSocket<uint8_t>;
56 explicit IceSocketEndpoint(std::shared_ptr<IceTransport> ice, bool isSender);
57 ~IceSocketEndpoint();
58
59 void shutdown() override;
60 bool isReliable() const override { return ice_ ? ice_->isRunning() : false; }
61 bool isInitiator() const override { return ice_ ? ice_->isInitiator() : true; }
62 int maxPayload() const override
63 {
64 return 65536 /* The max for a RTP packet used to wrap data here */;
65 }
66 int waitForData(std::chrono::milliseconds timeout, std::error_code& ec) const override;
67 std::size_t read(ValueType* buf, std::size_t len, std::error_code& ec) override;
68 std::size_t write(const ValueType* buf, std::size_t len, std::error_code& ec) override;
69
70 std::shared_ptr<IceTransport> underlyingICE() const { return ice_; }
71
72 void setOnRecv(RecvCb&& cb) override
73 {
74 if (ice_)
75 ice_->setOnRecv(compId_, cb);
76 }
77
78private:
79 std::shared_ptr<IceTransport> ice_ {nullptr};
80 std::atomic_bool iceStopped {false};
81 std::atomic_bool iceIsSender {false};
82 uint8_t compId_ {1};
83};
84
85//==============================================================================
86
87/// Implement a TLS session IO over a system socket
88class TlsSocketEndpoint : public GenericSocket<uint8_t>
89{
90public:
91 using SocketType = GenericSocket<uint8_t>;
92 using Identity = std::pair<std::shared_ptr<dht::crypto::PrivateKey>,
93 std::shared_ptr<dht::crypto::Certificate>>;
94
95 TlsSocketEndpoint(std::unique_ptr<IceSocketEndpoint>&& tr,
96 tls::CertificateStore& certStore,
Adrien Béraud3f93ddf2023-07-21 14:46:22 -040097 const std::shared_ptr<asio::io_context>& ioContext,
Adrien Béraud612b55b2023-05-29 10:42:04 -040098 const Identity& local_identity,
99 const std::shared_future<tls::DhParams>& dh_params,
100 const dht::crypto::Certificate& peer_cert);
101 TlsSocketEndpoint(std::unique_ptr<IceSocketEndpoint>&& tr,
102 tls::CertificateStore& certStore,
Adrien Béraud3f93ddf2023-07-21 14:46:22 -0400103 const std::shared_ptr<asio::io_context>& ioContext,
Adrien Béraud612b55b2023-05-29 10:42:04 -0400104 const Identity& local_identity,
105 const std::shared_future<tls::DhParams>& dh_params,
106 std::function<bool(const dht::crypto::Certificate&)>&& cert_check);
107 ~TlsSocketEndpoint();
108
109 bool isReliable() const override { return true; }
110 bool isInitiator() const override;
111 int maxPayload() const override;
112 void shutdown() override;
113 std::size_t read(ValueType* buf, std::size_t len, std::error_code& ec) override;
114 std::size_t write(const ValueType* buf, std::size_t len, std::error_code& ec) override;
115
116 std::shared_ptr<dht::crypto::Certificate> peerCertificate() const;
117
118 void setOnRecv(RecvCb&&) override
119 {
120 throw std::logic_error("TlsSocketEndpoint::setOnRecv not implemented");
121 }
122 int waitForData(std::chrono::milliseconds timeout, std::error_code&) const override;
123
124 void waitForReady(const std::chrono::milliseconds& timeout = {});
125
126 void setOnStateChange(OnStateChangeCb&& cb);
127 void setOnReady(OnReadyCb&& cb);
128
129 IpAddr getLocalAddress() const;
130 IpAddr getRemoteAddress() const;
131
132 void monitor() const;
133
134private:
135 class Impl;
136 std::unique_ptr<Impl> pimpl_;
137};
138
Sébastien Blin464bdff2023-07-19 08:02:53 -0400139} // namespace dhtnet