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