blob: 7403e9912caf221fc9b53db9916ff658370730c4 [file] [log] [blame]
Adrien Béraud612b55b2023-05-29 10:42:04 -04001/*
2 * Copyright (C) 2004-2023 Savoir-faire Linux Inc.
3 *
4 * Author: Guillaume Roguez <guillaume.roguez@savoirfairelinux.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * 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 General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21#pragma once
22
23#include "ice_options.h"
Adrien Béraud612b55b2023-05-29 10:42:04 -040024#include "ip_utils.h"
25
Adrien Béraud67ff0772023-07-12 11:22:29 -040026#include <msgpack.hpp>
27
Adrien Béraud612b55b2023-05-29 10:42:04 -040028#include <functional>
29#include <memory>
Adrien Béraud612b55b2023-05-29 10:42:04 -040030#include <vector>
Adrien Béraud67ff0772023-07-12 11:22:29 -040031#include <chrono>
Adrien Béraud612b55b2023-05-29 10:42:04 -040032
Adrien Béraud6de3f882023-07-06 12:56:29 -040033extern "C" {
34struct pj_ice_sess_cand;
35}
36
Adrien Béraud612b55b2023-05-29 10:42:04 -040037namespace dht {
38namespace log {
39class Logger;
40}
41}
42
Adrien Béraud1ae60aa2023-07-07 09:55:09 -040043namespace dhtnet {
Adrien Béraud612b55b2023-05-29 10:42:04 -040044
45using Logger = dht::log::Logger;
46
47namespace upnp {
48class Controller;
49}
50
51class IceTransport;
52
53using IceRecvCb = std::function<ssize_t(unsigned char* buf, size_t len)>;
54using IceCandidate = pj_ice_sess_cand;
55using onShutdownCb = std::function<void(void)>;
56
57struct ICESDP
58{
59 std::vector<IceCandidate> rem_candidates;
60 std::string rem_ufrag;
61 std::string rem_pwd;
62};
63
64struct SDP
65{
66 std::string ufrag;
67 std::string pwd;
68
69 std::vector<std::string> candidates;
70 MSGPACK_DEFINE(ufrag, pwd, candidates)
71};
72
73class IceTransport
74{
75public:
76 using Attribute = struct
77 {
78 std::string ufrag;
79 std::string pwd;
80 };
81
82 /**
83 * Constructor
84 */
85 IceTransport(std::string_view name);
86 ~IceTransport();
87
88 const std::shared_ptr<Logger>& logger() const;
89
90 void initIceInstance(const IceTransportOptions& options);
91
92 /**
93 * Get current state
94 */
95 bool isInitiator() const;
96
97 /**
98 * Start transport negotiation between local candidates and given remote
99 * to find the right candidate pair.
100 * This function doesn't block, the callback on_negodone_cb will be called
101 * with the negotiation result when operation is really done.
102 * Return false if negotiation cannot be started else true.
103 */
104 bool startIce(const Attribute& rem_attrs, std::vector<IceCandidate>&& rem_candidates);
105 bool startIce(const SDP& sdp);
106
107 /**
108 * Cancel operations
109 */
110 void cancelOperations();
111
112 /**
113 * Returns true if ICE transport has been initialized
114 * [mutex protected]
115 */
116 bool isInitialized() const;
117
118 /**
119 * Returns true if ICE negotiation has been started
120 * [mutex protected]
121 */
122 bool isStarted() const;
123
124 /**
125 * Returns true if ICE negotiation has completed with success
126 * [mutex protected]
127 */
128 bool isRunning() const;
129
130 /**
131 * Returns true if ICE transport is in failure state
132 * [mutex protected]
133 */
134 bool isFailed() const;
135
136 IpAddr getLocalAddress(unsigned comp_id) const;
137
138 IpAddr getRemoteAddress(unsigned comp_id) const;
139
140 IpAddr getDefaultLocalAddress() const { return getLocalAddress(1); }
141
142 /**
143 * Return ICE session attributes
144 */
145 const Attribute getLocalAttributes() const;
146
147 /**
148 * Return ICE session attributes
149 */
150 std::vector<std::string> getLocalCandidates(unsigned comp_id) const;
151
152 /**
153 * Return ICE session attributes
154 */
155 std::vector<std::string> getLocalCandidates(unsigned streamIdx, unsigned compId) const;
156
157 bool parseIceAttributeLine(unsigned streamIdx,
158 const std::string& line,
159 IceCandidate& cand) const;
160
161 bool getCandidateFromSDP(const std::string& line, IceCandidate& cand) const;
162
163 // I/O methods
164
165 void setOnRecv(unsigned comp_id, IceRecvCb cb);
166 void setOnShutdown(onShutdownCb&& cb);
167
168 ssize_t recv(unsigned comp_id, unsigned char* buf, size_t len, std::error_code& ec);
169 ssize_t recvfrom(unsigned comp_id, char* buf, size_t len, std::error_code& ec);
170
171 ssize_t send(unsigned comp_id, const unsigned char* buf, size_t len);
172
173 bool waitForInitialization(std::chrono::milliseconds timeout);
174
175 int waitForNegotiation(std::chrono::milliseconds timeout);
176
177 ssize_t waitForData(unsigned comp_id, std::chrono::milliseconds timeout, std::error_code& ec);
178
179 unsigned getComponentCount() const;
180
181 // Set session state
182 bool setSlaveSession();
183 bool setInitiatorSession();
184
185 bool isTCPEnabled();
186
187 ICESDP parseIceCandidates(std::string_view sdp_msg);
188
189 void setDefaultRemoteAddress(unsigned comp_id, const IpAddr& addr);
190
191 std::string link() const;
192
193private:
194 class Impl;
195 std::unique_ptr<Impl> pimpl_;
196};
197
Adrien Béraud612b55b2023-05-29 10:42:04 -0400198}; // namespace jami