Skip to content

Commit 39fedac

Browse files
committed
Add uProxy transform support to WebRTC for Crosswalk 20
2 parents 6224e9b + 4d8c3fe commit 39fedac

18 files changed

Lines changed: 120 additions & 29 deletions

api/webrtcsdp.cc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ static const char kAttributeSctpPort[] = "sctp-port";
150150
// Experimental flags
151151
static const char kAttributeXGoogleFlag[] = "x-google-flag";
152152
static const char kValueConference[] = "conference";
153+
static const char kAttributeXuProxyTransform[] = "x-uproxy-transform";
153154

154155
// Candidate
155156
static const char kCandidateHost[] = "host";
@@ -2609,6 +2610,13 @@ bool ParseContent(const std::string& message,
26092610
sctp_port)) {
26102611
return false;
26112612
}
2613+
} else if (HasAttribute(line, kAttributeXuProxyTransform)) {
2614+
// uProxy-specific hack to allow shapeshifting
2615+
std::string transform;
2616+
if (!GetValue(line, kAttributeXuProxyTransform, &transform, error)) {
2617+
return false;
2618+
}
2619+
transport->uproxy_transform = transform;
26122620
} else if (IsRtp(protocol)) {
26132621
//
26142622
// RTP specific attrubtes

base/asyncpacketsocket.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ struct PacketOptions {
4040
DiffServCodePoint dscp;
4141
int packet_id; // 16 bits, -1 represents "not set".
4242
PacketTimeUpdateParams packet_time_params;
43+
std::string uproxy_transform;
4344
};
4445

4546
// This structure will have the information about when packet is actually

p2p/base/dtlstransportchannel.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,9 @@ class DtlsTransportChannelWrapper : public TransportChannelImpl {
167167
const std::string& ice_pwd) override {
168168
channel_->SetIceCredentials(ice_ufrag, ice_pwd);
169169
}
170+
void SetUproxyTransform(const std::string& transform) override {
171+
channel_->SetUproxyTransform(transform);
172+
}
170173
void SetRemoteIceCredentials(const std::string& ice_ufrag,
171174
const std::string& ice_pwd) override {
172175
channel_->SetRemoteIceCredentials(ice_ufrag, ice_pwd);

p2p/base/p2ptransportchannel.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@ void P2PTransportChannel::AddAllocatorSession(PortAllocatorSession* session) {
267267
// previous sessions.
268268
ports_.clear();
269269

270+
session->set_uproxy_transform(uproxy_transform_);
270271
session->SignalPortReady.connect(this, &P2PTransportChannel::OnPortReady);
271272
session->SignalCandidatesReady.connect(
272273
this, &P2PTransportChannel::OnCandidatesReady);
@@ -360,6 +361,11 @@ void P2PTransportChannel::SetIceCredentials(const std::string& ice_ufrag,
360361
// called.
361362
}
362363

364+
void P2PTransportChannel::SetUproxyTransform(const std::string& transform) {
365+
ASSERT(worker_thread_ == rtc::Thread::Current());
366+
uproxy_transform_ = transform;
367+
}
368+
363369
void P2PTransportChannel::SetRemoteIceCredentials(const std::string& ice_ufrag,
364370
const std::string& ice_pwd) {
365371
ASSERT(worker_thread_ == rtc::Thread::Current());

p2p/base/p2ptransportchannel.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ class P2PTransportChannel : public TransportChannelImpl,
8282
void SetIceTiebreaker(uint64_t tiebreaker) override;
8383
void SetIceCredentials(const std::string& ice_ufrag,
8484
const std::string& ice_pwd) override;
85+
void SetUproxyTransform(const std::string& transform) override;
8586
void SetRemoteIceCredentials(const std::string& ice_ufrag,
8687
const std::string& ice_pwd) override;
8788
void SetRemoteIceMode(IceMode mode) override;
@@ -300,6 +301,9 @@ class P2PTransportChannel : public TransportChannelImpl,
300301
int weak_ping_delay_ = WEAK_PING_DELAY;
301302
TransportChannelState state_ = TransportChannelState::STATE_INIT;
302303

304+
// uProxy hack
305+
std::string uproxy_transform_;
306+
303307
RTC_DISALLOW_COPY_AND_ASSIGN(P2PTransportChannel);
304308
};
305309

p2p/base/port.cc

Lines changed: 49 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,8 @@ Port::Port(rtc::Thread* thread,
135135
rtc::Network* network,
136136
const rtc::IPAddress& ip,
137137
const std::string& username_fragment,
138-
const std::string& password)
138+
const std::string& password,
139+
const std::string& uproxy_transform)
139140
: thread_(thread),
140141
factory_(factory),
141142
send_retransmit_count_attribute_(false),
@@ -147,6 +148,8 @@ Port::Port(rtc::Thread* thread,
147148
generation_(0),
148149
ice_username_fragment_(username_fragment),
149150
password_(password),
151+
uproxy_transform_(uproxy_transform),
152+
transform_key_(0),
150153
timeout_delay_(kPortTimeoutDelay),
151154
enable_port_packets_(false),
152155
ice_role_(ICEROLE_UNKNOWN),
@@ -164,7 +167,8 @@ Port::Port(rtc::Thread* thread,
164167
uint16_t min_port,
165168
uint16_t max_port,
166169
const std::string& username_fragment,
167-
const std::string& password)
170+
const std::string& password,
171+
const std::string& uproxy_transform)
168172
: thread_(thread),
169173
factory_(factory),
170174
type_(type),
@@ -177,6 +181,8 @@ Port::Port(rtc::Thread* thread,
177181
generation_(0),
178182
ice_username_fragment_(username_fragment),
179183
password_(password),
184+
uproxy_transform_(uproxy_transform),
185+
transform_key_(0),
180186
timeout_delay_(kPortTimeoutDelay),
181187
enable_port_packets_(false),
182188
ice_role_(ICEROLE_UNKNOWN),
@@ -196,6 +202,11 @@ void Port::Construct() {
196202
ice_username_fragment_ = rtc::CreateRandomString(ICE_UFRAG_LENGTH);
197203
password_ = rtc::CreateRandomString(ICE_PWD_LENGTH);
198204
}
205+
if (!uproxy_transform_.empty()) {
206+
int space_point = uproxy_transform_.find(" ");
207+
transform_name_ = uproxy_transform_.substr(0, space_point);
208+
transform_key_ = atoi(uproxy_transform_.substr(space_point + 1).c_str());
209+
}
199210
network_->SignalInactive.connect(this, &Port::OnNetworkInactive);
200211
// TODO(honghaiz): Make it configurable from user setting.
201212
network_cost_ =
@@ -276,19 +287,20 @@ void Port::AddConnection(Connection* conn) {
276287
}
277288

278289
void Port::OnReadPacket(
279-
const char* data, size_t size, const rtc::SocketAddress& addr,
290+
const char* raw_data, size_t size, const rtc::SocketAddress& addr,
280291
ProtocolType proto) {
292+
std::vector<char> data = ApplyTransform(raw_data, size, false);
281293
// If the user has enabled port packets, just hand this over.
282294
if (enable_port_packets_) {
283-
SignalReadPacket(this, data, size, addr);
295+
SignalReadPacket(this, data.data(), data.size(), addr);
284296
return;
285297
}
286298

287299
// If this is an authenticated STUN request, then signal unknown address and
288300
// send back a proper binding response.
289301
rtc::scoped_ptr<IceMessage> msg;
290302
std::string remote_username;
291-
if (!GetStunMessage(data, size, addr, msg.accept(), &remote_username)) {
303+
if (!GetStunMessage(data.data(), data.size(), addr, msg.accept(), &remote_username)) {
292304
LOG_J(LS_ERROR, this) << "Received non-STUN packet from unknown address ("
293305
<< addr.ToSensitiveString() << ")";
294306
} else if (!msg) {
@@ -426,6 +438,18 @@ bool Port::GetStunMessage(const char* data, size_t size,
426438
return true;
427439
}
428440

441+
std::vector<char> Port::ApplyTransform(const void* data, size_t size, bool forward) const {
442+
const char* input_bytes = reinterpret_cast<const char*>(data);
443+
std::vector<char> output(size);
444+
if (transform_name_ == "caesar") {
445+
int shift = forward ? transform_key_ : -transform_key_;
446+
for (size_t i = 0; i < size; ++i) {
447+
output[i] = input_bytes[i] + shift;
448+
}
449+
}
450+
return output;
451+
}
452+
429453
bool Port::IsCompatibleAddress(const rtc::SocketAddress& addr) {
430454
int family = ip().family();
431455
// We use single-stack sockets, so families must match.
@@ -575,7 +599,10 @@ void Port::SendBindingResponse(StunMessage* request,
575599
rtc::ByteBuffer buf;
576600
response.Write(&buf);
577601
rtc::PacketOptions options(DefaultDscpValue());
578-
auto err = SendTo(buf.Data(), buf.Length(), addr, options, false);
602+
603+
std::vector<char> data = ApplyTransform(buf.Data(), buf.Length(), true);
604+
605+
auto err = SendTo(data.data(), data.size(), addr, options, false);
579606
if (err < 0) {
580607
LOG_J(LS_ERROR, this)
581608
<< "Failed to send STUN ping response"
@@ -623,7 +650,10 @@ void Port::SendBindingErrorResponse(StunMessage* request,
623650
rtc::ByteBuffer buf;
624651
response.Write(&buf);
625652
rtc::PacketOptions options(DefaultDscpValue());
626-
SendTo(buf.Data(), buf.Length(), addr, options, false);
653+
654+
std::vector<char> data = ApplyTransform(buf.Data(), buf.Length(), true);
655+
656+
SendTo(data.data(), data.size(), addr, options, false);
627657
LOG_J(LS_INFO, this) << "Sending STUN binding error: reason=" << reason
628658
<< " to " << addr.ToSensitiveString();
629659
}
@@ -885,11 +915,12 @@ void Connection::set_use_candidate_attr(bool enable) {
885915
use_candidate_attr_ = enable;
886916
}
887917

888-
void Connection::OnSendStunPacket(const void* data, size_t size,
918+
void Connection::OnSendStunPacket(const void* raw_data, size_t size,
889919
StunRequest* req) {
920+
std::vector<char> data = port_->ApplyTransform(raw_data, size, true);
890921
rtc::PacketOptions options(port_->DefaultDscpValue());
891922
auto err = port_->SendTo(
892-
data, size, remote_candidate_.address(), options, false);
923+
data.data(), data.size(), remote_candidate_.address(), options, false);
893924
if (err < 0) {
894925
LOG_J(LS_WARNING, this) << "Failed to send STUN ping "
895926
<< " err=" << err
@@ -898,17 +929,18 @@ void Connection::OnSendStunPacket(const void* data, size_t size,
898929
}
899930

900931
void Connection::OnReadPacket(
901-
const char* data, size_t size, const rtc::PacketTime& packet_time) {
932+
const char* raw_data, size_t size, const rtc::PacketTime& packet_time) {
902933
rtc::scoped_ptr<IceMessage> msg;
903934
std::string remote_ufrag;
904935
const rtc::SocketAddress& addr(remote_candidate_.address());
905-
if (!port_->GetStunMessage(data, size, addr, msg.accept(), &remote_ufrag)) {
936+
std::vector<char> data = port_->ApplyTransform(raw_data, size, false);
937+
if (!port_->GetStunMessage(data.data(), data.size(), addr, msg.accept(), &remote_ufrag)) {
906938
// The packet did not parse as a valid STUN message
907939
// This is a data packet, pass it along.
908940
set_receiving(true);
909941
last_data_received_ = rtc::Time();
910942
recv_rate_tracker_.AddSamples(size);
911-
SignalReadPacket(this, data, size, packet_time);
943+
SignalReadPacket(this, data.data(), data.size(), packet_time);
912944

913945
// If timed out sending writability checks, start up again
914946
if (!pruned_ && (write_state_ == STATE_WRITE_TIMEOUT)) {
@@ -951,7 +983,7 @@ void Connection::OnReadPacket(
951983
case STUN_BINDING_RESPONSE:
952984
case STUN_BINDING_ERROR_RESPONSE:
953985
if (msg->ValidateMessageIntegrity(
954-
data, size, remote_candidate().password())) {
986+
data.data(), data.size(), remote_candidate().password())) {
955987
requests_.CheckResponse(msg.get());
956988
}
957989
// Otherwise silently discard the response message.
@@ -1430,14 +1462,16 @@ ProxyConnection::ProxyConnection(Port* port,
14301462
const Candidate& remote_candidate)
14311463
: Connection(port, index, remote_candidate) {}
14321464

1433-
int ProxyConnection::Send(const void* data, size_t size,
1465+
int ProxyConnection::Send(const void* raw_data, size_t size,
14341466
const rtc::PacketOptions& options) {
1467+
std::vector<char> data = port_->ApplyTransform(raw_data, size, true);
1468+
14351469
if (write_state_ == STATE_WRITE_INIT || write_state_ == STATE_WRITE_TIMEOUT) {
14361470
error_ = EWOULDBLOCK;
14371471
return SOCKET_ERROR;
14381472
}
14391473
sent_packets_total_++;
1440-
int sent = port_->SendTo(data, size, remote_candidate_.address(),
1474+
int sent = port_->SendTo(data.data(), data.size(), remote_candidate_.address(),
14411475
options, true);
14421476
if (sent <= 0) {
14431477
ASSERT(sent < 0);

p2p/base/port.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,8 @@ class Port : public PortInterface, public rtc::MessageHandler,
120120
rtc::Network* network,
121121
const rtc::IPAddress& ip,
122122
const std::string& username_fragment,
123-
const std::string& password);
123+
const std::string& password,
124+
const std::string& transform);
124125
Port(rtc::Thread* thread,
125126
const std::string& type,
126127
rtc::PacketSocketFactory* factory,
@@ -129,7 +130,8 @@ class Port : public PortInterface, public rtc::MessageHandler,
129130
uint16_t min_port,
130131
uint16_t max_port,
131132
const std::string& username_fragment,
132-
const std::string& password);
133+
const std::string& password,
134+
const std::string& transform);
133135
virtual ~Port();
134136

135137
virtual const std::string& Type() const { return type_; }
@@ -191,6 +193,8 @@ class Port : public PortInterface, public rtc::MessageHandler,
191193
const std::string username_fragment() const;
192194
const std::string& password() const { return password_; }
193195

196+
std::vector<char> ApplyTransform(const void* data, size_t size, bool forward) const;
197+
194198
// Fired when candidates are discovered by the port. When all candidates
195199
// are discovered that belong to port SignalAddressReady is fired.
196200
sigslot::signal2<Port*, const Candidate&> SignalCandidateReady;
@@ -381,6 +385,9 @@ class Port : public PortInterface, public rtc::MessageHandler,
381385
// username_fragment().
382386
std::string ice_username_fragment_;
383387
std::string password_;
388+
std::string uproxy_transform_;
389+
std::string transform_name_;
390+
int transform_key_;
384391
std::vector<Candidate> candidates_;
385392
AddressMap connections_;
386393
int timeout_delay_;

p2p/base/portallocator.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,8 @@ class PortAllocatorSession : public sigslot::has_slots<> {
140140

141141
virtual uint32_t generation() { return generation_; }
142142
virtual void set_generation(uint32_t generation) { generation_ = generation; }
143+
virtual void set_uproxy_transform(const std::string& transform) { uproxy_transform_ = transform;}
144+
const std::string& get_uproxy_transform() { return uproxy_transform_; }
143145
sigslot::signal1<PortAllocatorSession*> SignalDestroyed;
144146

145147
const std::string& ice_ufrag() const { return ice_ufrag_; }
@@ -159,6 +161,7 @@ class PortAllocatorSession : public sigslot::has_slots<> {
159161
uint32_t generation_;
160162
std::string ice_ufrag_;
161163
std::string ice_pwd_;
164+
std::string uproxy_transform_;
162165
};
163166

164167
class PortAllocator : public sigslot::has_slots<> {

p2p/base/relayport.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,8 @@ RelayPort::RelayPort(rtc::Thread* thread,
194194
min_port,
195195
max_port,
196196
username,
197-
password),
197+
password,
198+
"" /* uproxy_transform */),
198199
ready_(false),
199200
error_(0) {
200201
entries_.push_back(

p2p/base/stunport.cc

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,14 +166,16 @@ UDPPort::UDPPort(rtc::Thread* thread,
166166
rtc::AsyncPacketSocket* socket,
167167
const std::string& username,
168168
const std::string& password,
169+
const std::string& transform,
169170
const std::string& origin,
170171
bool emit_local_for_anyaddress)
171172
: Port(thread,
172173
factory,
173174
network,
174175
socket->GetLocalAddress().ipaddr(),
175176
username,
176-
password),
177+
password,
178+
transform),
177179
requests_(thread),
178180
socket_(socket),
179181
error_(0),
@@ -191,6 +193,7 @@ UDPPort::UDPPort(rtc::Thread* thread,
191193
uint16_t max_port,
192194
const std::string& username,
193195
const std::string& password,
196+
const std::string& transform,
194197
const std::string& origin,
195198
bool emit_local_for_anyaddress)
196199
: Port(thread,
@@ -201,7 +204,8 @@ UDPPort::UDPPort(rtc::Thread* thread,
201204
min_port,
202205
max_port,
203206
username,
204-
password),
207+
password,
208+
transform),
205209
requests_(thread),
206210
socket_(NULL),
207211
error_(0),

0 commit comments

Comments
 (0)