blob: 8c69e93f4521dcda0cf389dc67556a493e98c670 [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 "ice_options.h"
Adrien Béraud612b55b2023-05-29 10:42:04 -040020#include "ip_utils.h"
21
Adrien Béraud67ff0772023-07-12 11:22:29 -040022#include <msgpack.hpp>
23
Adrien Béraud612b55b2023-05-29 10:42:04 -040024#include <functional>
25#include <memory>
Adrien Béraud612b55b2023-05-29 10:42:04 -040026#include <vector>
Adrien Béraud67ff0772023-07-12 11:22:29 -040027#include <chrono>
Adrien Béraud612b55b2023-05-29 10:42:04 -040028
Andreas Traczyk24cc01e2023-08-23 18:55:15 -040029#if defined(_MSC_VER)
30#include <BaseTsd.h>
31using ssize_t = SSIZE_T;
32#endif
33
Adrien Béraud6de3f882023-07-06 12:56:29 -040034extern "C" {
35struct pj_ice_sess_cand;
36}
37
Adrien Béraud612b55b2023-05-29 10:42:04 -040038namespace dht {
39namespace log {
Adrien Béraud9132a812023-07-21 11:20:40 -040040struct Logger;
Adrien Béraud612b55b2023-05-29 10:42:04 -040041}
42}
43
Adrien Béraud1ae60aa2023-07-07 09:55:09 -040044namespace dhtnet {
Adrien Béraud612b55b2023-05-29 10:42:04 -040045
46using Logger = dht::log::Logger;
47
48namespace upnp {
49class Controller;
50}
51
52class IceTransport;
53
54using IceRecvCb = std::function<ssize_t(unsigned char* buf, size_t len)>;
55using IceCandidate = pj_ice_sess_cand;
56using onShutdownCb = std::function<void(void)>;
57
58struct ICESDP
59{
60 std::vector<IceCandidate> rem_candidates;
61 std::string rem_ufrag;
62 std::string rem_pwd;
63};
64
65struct SDP
66{
67 std::string ufrag;
68 std::string pwd;
69
70 std::vector<std::string> candidates;
71 MSGPACK_DEFINE(ufrag, pwd, candidates)
72};
73
74class IceTransport
75{
76public:
77 using Attribute = struct
78 {
79 std::string ufrag;
80 std::string pwd;
81 };
82
83 /**
84 * Constructor
85 */
Adrien Béraud89933c12023-07-26 14:53:30 -040086 IceTransport(std::string_view name, const std::shared_ptr<Logger>& logger = {});
Adrien Béraud612b55b2023-05-29 10:42:04 -040087 ~IceTransport();
88
89 const std::shared_ptr<Logger>& logger() const;
90
91 void initIceInstance(const IceTransportOptions& options);
92
93 /**
94 * Get current state
95 */
96 bool isInitiator() const;
97
98 /**
99 * Start transport negotiation between local candidates and given remote
100 * to find the right candidate pair.
101 * This function doesn't block, the callback on_negodone_cb will be called
102 * with the negotiation result when operation is really done.
103 * Return false if negotiation cannot be started else true.
104 */
105 bool startIce(const Attribute& rem_attrs, std::vector<IceCandidate>&& rem_candidates);
106 bool startIce(const SDP& sdp);
107
108 /**
109 * Cancel operations
110 */
111 void cancelOperations();
112
113 /**
114 * Returns true if ICE transport has been initialized
115 * [mutex protected]
116 */
117 bool isInitialized() const;
118
119 /**
120 * Returns true if ICE negotiation has been started
121 * [mutex protected]
122 */
123 bool isStarted() const;
124
125 /**
126 * Returns true if ICE negotiation has completed with success
127 * [mutex protected]
128 */
129 bool isRunning() const;
130
131 /**
132 * Returns true if ICE transport is in failure state
133 * [mutex protected]
134 */
135 bool isFailed() const;
136
137 IpAddr getLocalAddress(unsigned comp_id) const;
138
139 IpAddr getRemoteAddress(unsigned comp_id) const;
140
141 IpAddr getDefaultLocalAddress() const { return getLocalAddress(1); }
142
143 /**
144 * Return ICE session attributes
145 */
146 const Attribute getLocalAttributes() const;
147
148 /**
149 * Return ICE session attributes
150 */
151 std::vector<std::string> getLocalCandidates(unsigned comp_id) const;
152
153 /**
154 * Return ICE session attributes
155 */
156 std::vector<std::string> getLocalCandidates(unsigned streamIdx, unsigned compId) const;
157
158 bool parseIceAttributeLine(unsigned streamIdx,
159 const std::string& line,
160 IceCandidate& cand) const;
161
162 bool getCandidateFromSDP(const std::string& line, IceCandidate& cand) const;
163
164 // I/O methods
165
166 void setOnRecv(unsigned comp_id, IceRecvCb cb);
167 void setOnShutdown(onShutdownCb&& cb);
168
169 ssize_t recv(unsigned comp_id, unsigned char* buf, size_t len, std::error_code& ec);
170 ssize_t recvfrom(unsigned comp_id, char* buf, size_t len, std::error_code& ec);
171
172 ssize_t send(unsigned comp_id, const unsigned char* buf, size_t len);
173
174 bool waitForInitialization(std::chrono::milliseconds timeout);
175
176 int waitForNegotiation(std::chrono::milliseconds timeout);
177
178 ssize_t waitForData(unsigned comp_id, std::chrono::milliseconds timeout, std::error_code& ec);
179
180 unsigned getComponentCount() const;
181
182 // Set session state
183 bool setSlaveSession();
184 bool setInitiatorSession();
185
186 bool isTCPEnabled();
187
188 ICESDP parseIceCandidates(std::string_view sdp_msg);
189
190 void setDefaultRemoteAddress(unsigned comp_id, const IpAddr& addr);
191
192 std::string link() const;
193
194private:
195 class Impl;
196 std::unique_ptr<Impl> pimpl_;
197};
198
Adrien Béraud612b55b2023-05-29 10:42:04 -0400199}; // namespace jami