From 1ea0f9f2109c12057c5fddc67ae3929928a3d7b1 Mon Sep 17 00:00:00 2001 From: Pierre Ossman Date: Mon, 4 Jan 2021 13:04:26 +0100 Subject: [PATCH] Handle unsolicited clipboard transfers The extended clipboard protocol has the ability for the peer to request things to be sent automatically, without a request message. Make sure we honor such settings. --- common/rfb/CConnection.cxx | 31 +++++++++++++++++++++++++++---- common/rfb/CConnection.h | 1 + common/rfb/ClientParams.cxx | 12 ++++++++++++ common/rfb/ClientParams.h | 1 + common/rfb/SConnection.cxx | 37 +++++++++++++++++++++++++++++++------ common/rfb/SConnection.h | 1 + common/rfb/ServerParams.cxx | 12 ++++++++++++ common/rfb/ServerParams.h | 1 + 8 files changed, 86 insertions(+), 10 deletions(-) diff --git a/common/rfb/CConnection.cxx b/common/rfb/CConnection.cxx index bdde3253..538d90a8 100644 --- a/common/rfb/CConnection.cxx +++ b/common/rfb/CConnection.cxx @@ -578,13 +578,25 @@ void CConnection::requestClipboard() void CConnection::announceClipboard(bool available) { hasLocalClipboard = available; + unsolicitedClipboardAttempt = false; + + // Attempt an unsolicited transfer? + if (available && + (server.clipboardSize(rfb::clipboardUTF8) > 0) && + (server.clipboardFlags() & rfb::clipboardProvide)) { + vlog.debug("Attempting unsolicited clipboard transfer..."); + unsolicitedClipboardAttempt = true; + handleClipboardRequest(); + return; + } - if (server.clipboardFlags() & rfb::clipboardNotify) + if (server.clipboardFlags() & rfb::clipboardNotify) { writer()->writeClipboardNotify(available ? rfb::clipboardUTF8 : 0); - else { - if (available) - handleClipboardRequest(); + return; } + + if (available) + handleClipboardRequest(); } void CConnection::sendClipboardData(const char* data) @@ -593,6 +605,17 @@ void CConnection::sendClipboardData(const char* data) CharArray filtered(convertCRLF(data)); size_t sizes[1] = { strlen(filtered.buf) + 1 }; const rdr::U8* data[1] = { (const rdr::U8*)filtered.buf }; + + if (unsolicitedClipboardAttempt) { + unsolicitedClipboardAttempt = false; + if (sizes[0] > server.clipboardSize(rfb::clipboardUTF8)) { + vlog.debug("Clipboard was too large for unsolicited clipboard transfer"); + if (server.clipboardFlags() & rfb::clipboardNotify) + writer()->writeClipboardNotify(rfb::clipboardUTF8); + return; + } + } + writer()->writeClipboardProvide(rfb::clipboardUTF8, sizes, data); } else { CharArray latin1(utf8ToLatin1(data)); diff --git a/common/rfb/CConnection.h b/common/rfb/CConnection.h index f01d5d36..ca76f530 100644 --- a/common/rfb/CConnection.h +++ b/common/rfb/CConnection.h @@ -287,6 +287,7 @@ namespace rfb { char* serverClipboard; bool hasLocalClipboard; + bool unsolicitedClipboardAttempt; }; } #endif diff --git a/common/rfb/ClientParams.cxx b/common/rfb/ClientParams.cxx index 987abe32..6f075a24 100644 --- a/common/rfb/ClientParams.cxx +++ b/common/rfb/ClientParams.cxx @@ -143,6 +143,18 @@ void ClientParams::setLEDState(unsigned int state) ledState_ = state; } +rdr::U32 ClientParams::clipboardSize(unsigned int format) const +{ + int i; + + for (i = 0;i < 16;i++) { + if (((unsigned)1 << i) == format) + return clipSizes[i]; + } + + throw Exception("Invalid clipboard format 0x%x", format); +} + void ClientParams::setClipboardCaps(rdr::U32 flags, const rdr::U32* lengths) { int i, num; diff --git a/common/rfb/ClientParams.h b/common/rfb/ClientParams.h index aab3d644..3894ef2d 100644 --- a/common/rfb/ClientParams.h +++ b/common/rfb/ClientParams.h @@ -85,6 +85,7 @@ namespace rfb { void setLEDState(unsigned int state); rdr::U32 clipboardFlags() const { return clipFlags; } + rdr::U32 clipboardSize(unsigned int format) const; void setClipboardCaps(rdr::U32 flags, const rdr::U32* lengths); // Wrappers to check for functionality rather than specific diff --git a/common/rfb/SConnection.cxx b/common/rfb/SConnection.cxx index 4869199a..1b5f513e 100644 --- a/common/rfb/SConnection.cxx +++ b/common/rfb/SConnection.cxx @@ -54,7 +54,8 @@ SConnection::SConnection() is(0), os(0), reader_(0), writer_(0), ssecurity(0), state_(RFBSTATE_UNINITIALISED), preferredEncoding(encodingRaw), - clientClipboard(NULL), hasLocalClipboard(false) + clientClipboard(NULL), hasLocalClipboard(false), + unsolicitedClipboardAttempt(false) { defaultMajorVersion = 3; defaultMinorVersion = 8; @@ -503,14 +504,27 @@ void SConnection::requestClipboard() void SConnection::announceClipboard(bool available) { hasLocalClipboard = available; + unsolicitedClipboardAttempt = false; - if (client.supportsEncoding(pseudoEncodingExtendedClipboard) && - (client.clipboardFlags() & rfb::clipboardNotify)) - writer()->writeClipboardNotify(available ? rfb::clipboardUTF8 : 0); - else { - if (available) + if (client.supportsEncoding(pseudoEncodingExtendedClipboard)) { + // Attempt an unsolicited transfer? + if (available && + (client.clipboardSize(rfb::clipboardUTF8) > 0) && + (client.clipboardFlags() & rfb::clipboardProvide)) { + vlog.debug("Attempting unsolicited clipboard transfer..."); + unsolicitedClipboardAttempt = true; handleClipboardRequest(); + return; + } + + if (client.clipboardFlags() & rfb::clipboardNotify) { + writer()->writeClipboardNotify(available ? rfb::clipboardUTF8 : 0); + return; + } } + + if (available) + handleClipboardRequest(); } void SConnection::sendClipboardData(const char* data) @@ -520,6 +534,17 @@ void SConnection::sendClipboardData(const char* data) CharArray filtered(convertCRLF(data)); size_t sizes[1] = { strlen(filtered.buf) + 1 }; const rdr::U8* data[1] = { (const rdr::U8*)filtered.buf }; + + if (unsolicitedClipboardAttempt) { + unsolicitedClipboardAttempt = false; + if (sizes[0] > client.clipboardSize(rfb::clipboardUTF8)) { + vlog.debug("Clipboard was too large for unsolicited clipboard transfer"); + if (client.clipboardFlags() & rfb::clipboardNotify) + writer()->writeClipboardNotify(rfb::clipboardUTF8); + return; + } + } + writer()->writeClipboardProvide(rfb::clipboardUTF8, sizes, data); } else { CharArray latin1(utf8ToLatin1(data)); diff --git a/common/rfb/SConnection.h b/common/rfb/SConnection.h index db3ab08c..1119c7c9 100644 --- a/common/rfb/SConnection.h +++ b/common/rfb/SConnection.h @@ -255,6 +255,7 @@ namespace rfb { char* clientClipboard; bool hasLocalClipboard; + bool unsolicitedClipboardAttempt; }; } #endif diff --git a/common/rfb/ServerParams.cxx b/common/rfb/ServerParams.cxx index a2e3aa83..a9a96b6e 100644 --- a/common/rfb/ServerParams.cxx +++ b/common/rfb/ServerParams.cxx @@ -87,6 +87,18 @@ void ServerParams::setLEDState(unsigned int state) ledState_ = state; } +rdr::U32 ServerParams::clipboardSize(unsigned int format) const +{ + int i; + + for (i = 0;i < 16;i++) { + if (((unsigned)1 << i) == format) + return clipSizes[i]; + } + + throw Exception("Invalid clipboard format 0x%x", format); +} + void ServerParams::setClipboardCaps(rdr::U32 flags, const rdr::U32* lengths) { int i, num; diff --git a/common/rfb/ServerParams.h b/common/rfb/ServerParams.h index c84f6252..ce0c722f 100644 --- a/common/rfb/ServerParams.h +++ b/common/rfb/ServerParams.h @@ -70,6 +70,7 @@ namespace rfb { void setLEDState(unsigned int state); rdr::U32 clipboardFlags() const { return clipFlags; } + rdr::U32 clipboardSize(unsigned int format) const; void setClipboardCaps(rdr::U32 flags, const rdr::U32* lengths); bool supportsQEMUKeyEvent; -- 2.39.5