blob: 7fbeff4b654a651d0d81debc66383a7477d96e00 [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
Adrien Béraud75754b22023-10-17 09:16:06 -040050static constexpr int ICE_COMP_ID_SIP_TRANSPORT {1};
51
Adrien Béraud612b55b2023-05-29 10:42:04 -040052//==============================================================================
53
54class IceSocketEndpoint : public GenericSocket<uint8_t>
55{
56public:
57 using SocketType = GenericSocket<uint8_t>;
58 explicit IceSocketEndpoint(std::shared_ptr<IceTransport> ice, bool isSender);
59 ~IceSocketEndpoint();
60
61 void shutdown() override;
Adrien Béraudc3777bb2023-11-06 09:27:04 -050062 bool isReliable() const override { return ice_ ? ice_->isTCPEnabled() : false; }
Adrien Béraud612b55b2023-05-29 10:42:04 -040063 bool isInitiator() const override { return ice_ ? ice_->isInitiator() : true; }
64 int maxPayload() const override
65 {
66 return 65536 /* The max for a RTP packet used to wrap data here */;
67 }
68 int waitForData(std::chrono::milliseconds timeout, std::error_code& ec) const override;
69 std::size_t read(ValueType* buf, std::size_t len, std::error_code& ec) override;
70 std::size_t write(const ValueType* buf, std::size_t len, std::error_code& ec) override;
71
72 std::shared_ptr<IceTransport> underlyingICE() const { return ice_; }
73
74 void setOnRecv(RecvCb&& cb) override
75 {
76 if (ice_)
77 ice_->setOnRecv(compId_, cb);
78 }
79
80private:
81 std::shared_ptr<IceTransport> ice_ {nullptr};
82 std::atomic_bool iceStopped {false};
83 std::atomic_bool iceIsSender {false};
84 uint8_t compId_ {1};
85};
86
87//==============================================================================
88
89/// Implement a TLS session IO over a system socket
90class TlsSocketEndpoint : public GenericSocket<uint8_t>
91{
92public:
93 using SocketType = GenericSocket<uint8_t>;
94 using Identity = std::pair<std::shared_ptr<dht::crypto::PrivateKey>,
95 std::shared_ptr<dht::crypto::Certificate>>;
96
97 TlsSocketEndpoint(std::unique_ptr<IceSocketEndpoint>&& tr,
98 tls::CertificateStore& certStore,
Adrien Béraud3f93ddf2023-07-21 14:46:22 -040099 const std::shared_ptr<asio::io_context>& ioContext,
Adrien Béraud612b55b2023-05-29 10:42:04 -0400100 const Identity& local_identity,
101 const std::shared_future<tls::DhParams>& dh_params,
102 const dht::crypto::Certificate& peer_cert);
103 TlsSocketEndpoint(std::unique_ptr<IceSocketEndpoint>&& tr,
104 tls::CertificateStore& certStore,
Adrien Béraud3f93ddf2023-07-21 14:46:22 -0400105 const std::shared_ptr<asio::io_context>& ioContext,
Adrien Béraud612b55b2023-05-29 10:42:04 -0400106 const Identity& local_identity,
107 const std::shared_future<tls::DhParams>& dh_params,
108 std::function<bool(const dht::crypto::Certificate&)>&& cert_check);
109 ~TlsSocketEndpoint();
110
111 bool isReliable() const override { return true; }
112 bool isInitiator() const override;
113 int maxPayload() const override;
114 void shutdown() override;
115 std::size_t read(ValueType* buf, std::size_t len, std::error_code& ec) override;
116 std::size_t write(const ValueType* buf, std::size_t len, std::error_code& ec) override;
117
118 std::shared_ptr<dht::crypto::Certificate> peerCertificate() const;
119
120 void setOnRecv(RecvCb&&) override
121 {
122 throw std::logic_error("TlsSocketEndpoint::setOnRecv not implemented");
123 }
124 int waitForData(std::chrono::milliseconds timeout, std::error_code&) const override;
125
Adrien Béraud612b55b2023-05-29 10:42:04 -0400126 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