Skip to content

Commit 7982799

Browse files
Merge branch 'v0.16'
2 parents a801a38 + 3de8410 commit 7982799

10 files changed

+79
-54
lines changed

.github/workflows/build-gnutls.yml

-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ on:
44
branches:
55
- master
66
pull_request:
7-
branches:
8-
- master
97
jobs:
108
build-linux:
119
runs-on: ubuntu-latest

.github/workflows/build-nice.yml

-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ on:
44
branches:
55
- master
66
pull_request:
7-
branches:
8-
- master
97
jobs:
108
build-media:
119
runs-on: ubuntu-latest

.github/workflows/build-openssl.yml

-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ on:
44
branches:
55
- master
66
pull_request:
7-
branches:
8-
- master
97
jobs:
108
build-linux:
119
runs-on: ubuntu-latest

src/impl/datachannel.cpp

+26-12
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,13 @@ struct CloseMessage {
7575
};
7676
#pragma pack(pop)
7777

78-
LogCounter COUNTER_USERNEG_OPEN_MESSAGE(
79-
plog::warning, "Number of open messages for a user-negotiated DataChannel received");
78+
bool DataChannel::IsOpenMessage(message_ptr message) {
79+
if (message->type != Message::Control)
80+
return false;
81+
82+
auto raw = reinterpret_cast<const uint8_t *>(message->data());
83+
return !message->empty() && raw[0] == MESSAGE_OPEN;
84+
}
8085

8186
DataChannel::DataChannel(weak_ptr<PeerConnection> pc, uint16_t stream, string label,
8287
string protocol, Reliability reliability)
@@ -193,8 +198,7 @@ void DataChannel::open(shared_ptr<SctpTransport> transport) {
193198
}
194199

195200
void DataChannel::processOpenMessage(message_ptr) {
196-
PLOG_DEBUG << "Received an open message for a user-negotiated DataChannel, ignoring";
197-
COUNTER_USERNEG_OPEN_MESSAGE++;
201+
PLOG_WARNING << "Received an open message for a user-negotiated DataChannel, ignoring";
198202
}
199203

200204
bool DataChannel::outgoing(message_ptr message) {
@@ -218,7 +222,7 @@ bool DataChannel::outgoing(message_ptr message) {
218222
}
219223

220224
void DataChannel::incoming(message_ptr message) {
221-
if (!message)
225+
if (!message || mIsClosed)
222226
return;
223227

224228
switch (message->type) {
@@ -261,12 +265,6 @@ NegotiatedDataChannel::NegotiatedDataChannel(weak_ptr<PeerConnection> pc, uint16
261265
string label, string protocol, Reliability reliability)
262266
: DataChannel(pc, stream, std::move(label), std::move(protocol), std::move(reliability)) {}
263267

264-
NegotiatedDataChannel::NegotiatedDataChannel(weak_ptr<PeerConnection> pc,
265-
weak_ptr<SctpTransport> transport, uint16_t stream)
266-
: DataChannel(pc, stream, "", "", {}) {
267-
mSctpTransport = transport;
268-
}
269-
270268
NegotiatedDataChannel::~NegotiatedDataChannel() {}
271269

272270
void NegotiatedDataChannel::open(shared_ptr<SctpTransport> transport) {
@@ -314,7 +312,23 @@ void NegotiatedDataChannel::open(shared_ptr<SctpTransport> transport) {
314312
transport->send(make_message(buffer.begin(), buffer.end(), Message::Control, mStream));
315313
}
316314

317-
void NegotiatedDataChannel::processOpenMessage(message_ptr message) {
315+
void NegotiatedDataChannel::processOpenMessage(message_ptr) {
316+
PLOG_WARNING << "Received an open message for a locally-created DataChannel, ignoring";
317+
}
318+
319+
IncomingDataChannel::IncomingDataChannel(weak_ptr<PeerConnection> pc,
320+
weak_ptr<SctpTransport> transport, uint16_t stream)
321+
: DataChannel(pc, stream, "", "", {}) {
322+
mSctpTransport = transport;
323+
}
324+
325+
IncomingDataChannel::~IncomingDataChannel() {}
326+
327+
void IncomingDataChannel::open(shared_ptr<SctpTransport>) {
328+
// Ignore
329+
}
330+
331+
void IncomingDataChannel::processOpenMessage(message_ptr message) {
318332
std::unique_lock lock(mMutex);
319333
auto transport = mSctpTransport.lock();
320334
if (!transport)

src/impl/datachannel.hpp

+12-3
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ namespace rtc::impl {
3535
struct PeerConnection;
3636

3737
struct DataChannel : Channel, std::enable_shared_from_this<DataChannel> {
38+
static bool IsOpenMessage(message_ptr message);
39+
3840
DataChannel(weak_ptr<PeerConnection> pc, uint16_t stream, string label, string protocol,
3941
Reliability reliability);
4042
virtual ~DataChannel();
@@ -82,11 +84,18 @@ struct DataChannel : Channel, std::enable_shared_from_this<DataChannel> {
8284
struct NegotiatedDataChannel final : public DataChannel {
8385
NegotiatedDataChannel(weak_ptr<PeerConnection> pc, uint16_t stream, string label,
8486
string protocol, Reliability reliability);
85-
NegotiatedDataChannel(weak_ptr<PeerConnection> pc, weak_ptr<SctpTransport> transport,
86-
uint16_t stream);
8787
~NegotiatedDataChannel();
8888

89-
void open(impl_ptr<SctpTransport> transport) override;
89+
void open(shared_ptr<SctpTransport> transport) override;
90+
void processOpenMessage(message_ptr message) override;
91+
};
92+
93+
struct IncomingDataChannel final : public DataChannel {
94+
IncomingDataChannel(weak_ptr<PeerConnection> pc, weak_ptr<SctpTransport> transport,
95+
uint16_t stream);
96+
~IncomingDataChannel();
97+
98+
void open(shared_ptr<SctpTransport> transport) override;
9099
void processOpenMessage(message_ptr message) override;
91100
};
92101

src/impl/peerconnection.cpp

+29-24
Original file line numberDiff line numberDiff line change
@@ -419,42 +419,47 @@ void PeerConnection::forwardMessage(message_ptr message) {
419419
return;
420420
}
421421

422-
uint16_t stream = uint16_t(message->stream);
422+
const uint16_t stream = uint16_t(message->stream);
423423
auto channel = findDataChannel(stream);
424-
if (!channel) {
424+
425+
if (DataChannel::IsOpenMessage(message)) {
425426
auto iceTransport = getIceTransport();
426427
auto sctpTransport = getSctpTransport();
427428
if (!iceTransport || !sctpTransport)
428429
return;
429430

430-
// See https://www.rfc-editor.org/rfc/rfc8832.html
431-
const byte dataChannelOpenMessage{0x03};
432-
uint16_t remoteParity = (iceTransport->role() == Description::Role::Active) ? 1 : 0;
433-
if (message->type == Message::Control) {
434-
if (message->size() == 0 || *message->data() != dataChannelOpenMessage)
435-
return; // ignore
436-
437-
if (stream % 2 != remoteParity) {
438-
// The odd/even rule is violated, close the DataChannel
439-
sctpTransport->closeStream(message->stream);
440-
return;
441-
}
431+
const uint16_t remoteParity = (iceTransport->role() == Description::Role::Active) ? 1 : 0;
432+
if (stream % 2 != remoteParity) {
433+
// The odd/even rule is violated, close the DataChannel
434+
PLOG_WARNING << "Got open message violating the odd/even rule on stream " << stream;
435+
sctpTransport->closeStream(message->stream);
436+
return;
437+
}
442438

443-
channel =
444-
std::make_shared<NegotiatedDataChannel>(weak_from_this(), sctpTransport, stream);
445-
channel->openCallback = weak_bind(&PeerConnection::triggerDataChannel, this,
446-
weak_ptr<DataChannel>{channel});
439+
if (channel && channel->isOpen()) {
440+
PLOG_WARNING << "Got open message on stream " << stream
441+
<< " for an already open DataChannel, closing it first";
442+
channel->close();
443+
}
447444

448-
std::unique_lock lock(mDataChannelsMutex); // we are going to emplace
449-
mDataChannels.emplace(stream, channel);
445+
channel = std::make_shared<IncomingDataChannel>(weak_from_this(), sctpTransport, stream);
446+
channel->openCallback =
447+
weak_bind(&PeerConnection::triggerDataChannel, this, weak_ptr<DataChannel>{channel});
450448

451-
} else {
452-
// Invalid, close the DataChannel
449+
std::unique_lock lock(mDataChannelsMutex); // we are going to emplace
450+
mDataChannels.emplace(stream, channel);
451+
}
452+
453+
if (!channel) {
454+
// Invalid, close the DataChannel
455+
PLOG_WARNING << "Got unexpected message on stream " << stream;
456+
if (auto sctpTransport = getSctpTransport())
453457
sctpTransport->closeStream(message->stream);
454-
return;
455-
}
458+
459+
return;
456460
}
457461

462+
// Forward the message
458463
channel->incoming(message);
459464
}
460465

test/capi_connectivity.cpp

+1-3
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ static void RTC_API openCallback(int id, void *ptr) {
9797
static void RTC_API closedCallback(int id, void *ptr) {
9898
Peer *peer = (Peer *)ptr;
9999
peer->connected = false;
100+
printf("DataChannel %d: Closed\n", peer == peer1 ? 1 : 2);
100101
}
101102

102103
static void RTC_API messageCallback(int id, const char *message, int size, void *ptr) {
@@ -152,9 +153,6 @@ static void RTC_API dataChannelCallback(int pc, int dc, void *ptr) {
152153
rtcSetMessageCallback(dc, messageCallback);
153154

154155
peer->dc = dc;
155-
156-
const char *message = peer == peer1 ? "Hello from 1" : "Hello from 2";
157-
rtcSendMessage(peer->dc, message, -1); // negative size indicates a null-terminated string
158156
}
159157

160158
static Peer *createPeer(const rtcConfiguration *config) {

test/connectivity.cpp

+5-2
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,10 @@ void test_connectivity() {
108108
}
109109

110110
dc->onOpen([wdc = make_weak_ptr(dc)]() {
111-
if (auto dc = wdc.lock())
111+
if (auto dc = wdc.lock()) {
112+
cout << "DataChannel 2: Open" << endl;
112113
dc->send("Hello from 2");
114+
}
113115
});
114116

115117
dc->onClosed([]() {
@@ -157,7 +159,8 @@ void test_connectivity() {
157159
if (!adc2 || !adc2->isOpen() || !dc1->isOpen())
158160
throw runtime_error("DataChannel is not open");
159161

160-
if (dc1->maxMessageSize() != CUSTOM_MAX_MESSAGE_SIZE || dc2->maxMessageSize() != CUSTOM_MAX_MESSAGE_SIZE)
162+
if (dc1->maxMessageSize() != CUSTOM_MAX_MESSAGE_SIZE ||
163+
dc2->maxMessageSize() != CUSTOM_MAX_MESSAGE_SIZE)
161164
throw runtime_error("DataChannel max message size is incorrect");
162165

163166
if (auto addr = pc1.localAddress())

test/turn_connectivity.cpp

+5-3
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,10 @@ void test_turn_connectivity() {
105105
}
106106

107107
dc->onOpen([wdc = make_weak_ptr(dc)]() {
108-
if (auto dc = wdc.lock())
108+
if (auto dc = wdc.lock()) {
109+
cout << "DataChannel 2: Open" << endl;
109110
dc->send("Hello from 2");
111+
}
110112
});
111113

112114
dc->onMessage([](variant<binary, string> message) {
@@ -168,7 +170,7 @@ void test_turn_connectivity() {
168170
cout << "Local candidate 1: " << local << endl;
169171
cout << "Remote candidate 1: " << remote << endl;
170172

171-
if(local.type() != Candidate::Type::Relayed || remote.type() != Candidate::Type::Relayed)
173+
if (local.type() != Candidate::Type::Relayed || remote.type() != Candidate::Type::Relayed)
172174
throw runtime_error("Connection is not relayed as expected");
173175

174176
if (!pc2.getSelectedCandidatePair(&local, &remote))
@@ -177,7 +179,7 @@ void test_turn_connectivity() {
177179
cout << "Local candidate 2: " << local << endl;
178180
cout << "Remote candidate 2: " << remote << endl;
179181

180-
if(local.type() != Candidate::Type::Relayed || remote.type() != Candidate::Type::Relayed)
182+
if (local.type() != Candidate::Type::Relayed || remote.type() != Candidate::Type::Relayed)
181183
throw runtime_error("Connection is not relayed as expected");
182184

183185
// Try to open a second data channel with another label

0 commit comments

Comments
 (0)