From 264585c060bb3ea075e7977731c04983e4e7b9e1 Mon Sep 17 00:00:00 2001 From: achingbrain Date: Sun, 9 Jun 2024 10:26:31 +0100 Subject: [PATCH 1/2] feat: pass custom ICE ufrag and pwd as local description init Adds an optional second argument to `rtc::PeerConnection::setLocalDescription` that can contain an ICE ufrag and pwd that if passed will be used in place of the randomly generated versions. Refs: #1166 Refs: https://github.com/paullouisageneau/libdatachannel/pull/1201#discussion_r1633621803 Co-authored-by: Paul-Louis Ageneau --- include/rtc/peerconnection.hpp | 7 ++++++- src/impl/icetransport.cpp | 10 ++++++++++ src/impl/icetransport.hpp | 1 + src/peerconnection.cpp | 7 ++++++- 4 files changed, 23 insertions(+), 2 deletions(-) diff --git a/include/rtc/peerconnection.hpp b/include/rtc/peerconnection.hpp index 9e49f80e1..1648fbe37 100644 --- a/include/rtc/peerconnection.hpp +++ b/include/rtc/peerconnection.hpp @@ -35,6 +35,11 @@ struct RTC_CPP_EXPORT DataChannelInit { string protocol = ""; }; +struct RTC_CPP_EXPORT LocalDescriptionInit { + optional iceUfrag; + optional icePwd; +}; + class RTC_CPP_EXPORT PeerConnection final : CheshireCat { public: enum class State : int { @@ -90,7 +95,7 @@ class RTC_CPP_EXPORT PeerConnection final : CheshireCat { uint16_t maxDataChannelId() const; bool getSelectedCandidatePair(Candidate *local, Candidate *remote); - void setLocalDescription(Description::Type type = Description::Type::Unspec); + void setLocalDescription(Description::Type type = Description::Type::Unspec, LocalDescriptionInit init = {}); void setRemoteDescription(Description description); void addRemoteCandidate(Candidate candidate); void gatherLocalCandidates(std::vector additionalIceServers = {}); diff --git a/src/impl/icetransport.cpp b/src/impl/icetransport.cpp index a27a2ed32..a386da170 100644 --- a/src/impl/icetransport.cpp +++ b/src/impl/icetransport.cpp @@ -140,6 +140,12 @@ IceTransport::IceTransport(const Configuration &config, candidate_callback candi addIceServer(server); } +void IceTransport::setIceAttributes(string uFrag, string pwd) { + if (juice_set_local_ice_attributes(mAgent.get(), uFrag.c_str(), pwd.c_str()) < 0) { + throw std::invalid_argument("Invalid ICE attributes"); + } +} + void IceTransport::addIceServer(IceServer server) { if (server.hostname.empty()) return; @@ -569,6 +575,10 @@ IceTransport::IceTransport(const Configuration &config, candidate_callback candi RecvCallback, this); } +void IceTransport::setIceAttributes(string, string) { + PLOG_WARNING << "Setting custom ICE attributes is not supported with libnice, please use libjuice"; +} + void IceTransport::addIceServer(IceServer server) { if (server.hostname.empty()) return; diff --git a/src/impl/icetransport.hpp b/src/impl/icetransport.hpp index 4133bcc2f..69d0c99af 100644 --- a/src/impl/icetransport.hpp +++ b/src/impl/icetransport.hpp @@ -51,6 +51,7 @@ class IceTransport : public Transport { void setRemoteDescription(const Description &description); bool addRemoteCandidate(const Candidate &candidate); void gatherLocalCandidates(string mid, std::vector additionalIceServers = {}); + void setIceAttributes(string uFrag, string pwd); optional getLocalAddress() const; optional getRemoteAddress() const; diff --git a/src/peerconnection.cpp b/src/peerconnection.cpp index 0e146834b..77fa3ef79 100644 --- a/src/peerconnection.cpp +++ b/src/peerconnection.cpp @@ -76,7 +76,7 @@ bool PeerConnection::hasMedia() const { return local && local->hasAudioOrVideo(); } -void PeerConnection::setLocalDescription(Description::Type type) { +void PeerConnection::setLocalDescription(Description::Type type, LocalDescriptionInit init) { std::unique_lock signalingLock(impl()->signalingMutex); PLOG_VERBOSE << "Setting local description, type=" << Description::typeToString(type); @@ -140,6 +140,11 @@ void PeerConnection::setLocalDescription(Description::Type type) { if (!iceTransport) return; // closed + if (init.iceUfrag && init.icePwd) { + PLOG_DEBUG << "Using custom ICE attributes, ufrag=\"" << init.iceUfrag.value() << "\", pwd=\"" << init.icePwd.value() << "\""; + iceTransport->setIceAttributes(init.iceUfrag.value(), init.icePwd.value()); + } + Description local = iceTransport->getLocalDescription(type); impl()->processLocalDescription(std::move(local)); From f0432d1f304b6e42389bbe36cd83c12b1316c57d Mon Sep 17 00:00:00 2001 From: Paul-Louis Ageneau Date: Fri, 14 Jun 2024 12:03:47 +0200 Subject: [PATCH 2/2] Update src/impl/icetransport.cpp --- src/impl/icetransport.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/impl/icetransport.cpp b/src/impl/icetransport.cpp index a386da170..f53768640 100644 --- a/src/impl/icetransport.cpp +++ b/src/impl/icetransport.cpp @@ -575,7 +575,7 @@ IceTransport::IceTransport(const Configuration &config, candidate_callback candi RecvCallback, this); } -void IceTransport::setIceAttributes(string, string) { +void IceTransport::setIceAttributes([[maybe_unused]] string uFrag, [[maybe_unused]] string pwd) { PLOG_WARNING << "Setting custom ICE attributes is not supported with libnice, please use libjuice"; }