Browse Source

Prevent unresolved candidates from going through libnice

Paul-Louis Ageneau 5 years ago
parent
commit
f083815569
4 changed files with 13 additions and 3 deletions
  1. 2 0
      include/rtc/candidate.hpp
  2. 4 1
      src/candidate.cpp
  3. 3 2
      src/description.cpp
  4. 4 0
      src/icetransport.cpp

+ 2 - 0
include/rtc/candidate.hpp

@@ -31,11 +31,13 @@ public:
 
 	string candidate() const;
 	string mid() const;
+	bool isResolved() const;
 	operator string() const;
 
 private:
 	string mCandidate;
 	string mMid;
+	bool mIsResolved;
 };
 
 } // namespace rtc

+ 4 - 1
src/candidate.cpp

@@ -40,7 +40,7 @@ inline bool hasprefix(const string &str, const string &prefix) {
 
 namespace rtc {
 
-Candidate::Candidate(string candidate, string mid) {
+Candidate::Candidate(string candidate, string mid) : mIsResolved(false) {
 	const std::array prefixes{"a=", "candidate:"};
 	for (string prefix : prefixes)
 		if (hasprefix(candidate, prefix))
@@ -83,6 +83,7 @@ Candidate::Candidate(string candidate, string mid) {
 						if (!left.empty())
 							ss << left;
 						mCandidate = ss.str();
+						mIsResolved = true;
 						break;
 					}
 				}
@@ -96,6 +97,8 @@ string Candidate::candidate() const { return "candidate:" + mCandidate; }
 
 string Candidate::mid() const { return mMid; }
 
+bool Candidate::isResolved() const { return mIsResolved; }
+
 Candidate::operator string() const {
 	std::ostringstream line;
 	line << "a=" << candidate();

+ 3 - 2
src/description.cpp

@@ -82,7 +82,7 @@ Description::Description(const string &sdp, Type type, Role role)
 		} else if (hasprefix(line, "a=sctp-port")) {
 			mSctpPort = uint16_t(std::stoul(line.substr(line.find(':') + 1)));
 		} else if (hasprefix(line, "a=candidate")) {
-			mCandidates.emplace_back(Candidate(line.substr(2), mMid));
+			addCandidate(Candidate(line.substr(2), mMid));
 		} else if (hasprefix(line, "a=end-of-candidates")) {
 			mTrickle = false;
 		}
@@ -110,7 +110,8 @@ void Description::setFingerprint(string fingerprint) {
 void Description::setSctpPort(uint16_t port) { mSctpPort.emplace(port); }
 
 void Description::addCandidate(Candidate candidate) {
-	mCandidates.emplace_back(std::move(candidate));
+	if (candidate.isResolved())
+		mCandidates.emplace_back(std::move(candidate));
 }
 
 void Description::endCandidates() { mTrickle = false; }

+ 4 - 0
src/icetransport.cpp

@@ -159,6 +159,10 @@ void IceTransport::gatherLocalCandidates() {
 }
 
 bool IceTransport::addRemoteCandidate(const Candidate &candidate) {
+	// Don't try to pass unresolved candidates to libnice for more safety
+	if (!candidate.isResolved())
+		return false;
+
 	// Warning: the candidate string must start with "a=candidate:" and it must not end with a
 	// newline, else libnice will reject it.
 	string sdp(candidate);