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