You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

VNCSConnectionST.cxx 31KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126
  1. /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
  2. * Copyright 2009-2018 Pierre Ossman for Cendio AB
  3. * Copyright 2018 Peter Astrand for Cendio AB
  4. *
  5. * This is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This software is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this software; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  18. * USA.
  19. */
  20. #include <network/TcpSocket.h>
  21. #include <rfb/ComparingUpdateTracker.h>
  22. #include <rfb/Encoder.h>
  23. #include <rfb/KeyRemapper.h>
  24. #include <rfb/LogWriter.h>
  25. #include <rfb/Security.h>
  26. #include <rfb/ServerCore.h>
  27. #include <rfb/SMsgWriter.h>
  28. #include <rfb/VNCServerST.h>
  29. #include <rfb/VNCSConnectionST.h>
  30. #include <rfb/screenTypes.h>
  31. #include <rfb/fenceTypes.h>
  32. #include <rfb/ledStates.h>
  33. #define XK_LATIN1
  34. #define XK_MISCELLANY
  35. #define XK_XKB_KEYS
  36. #include <rfb/keysymdef.h>
  37. using namespace rfb;
  38. static LogWriter vlog("VNCSConnST");
  39. static Cursor emptyCursor(0, 0, Point(0, 0), NULL);
  40. VNCSConnectionST::VNCSConnectionST(VNCServerST* server_, network::Socket *s,
  41. bool reverse)
  42. : sock(s), reverseConnection(reverse),
  43. inProcessMessages(false),
  44. pendingSyncFence(false), syncFence(false), fenceFlags(0),
  45. fenceDataLen(0), fenceData(NULL), congestionTimer(this),
  46. losslessTimer(this), server(server_),
  47. updateRenderedCursor(false), removeRenderedCursor(false),
  48. continuousUpdates(false), encodeManager(this), idleTimer(this),
  49. pointerEventTime(0), clientHasCursor(false)
  50. {
  51. setStreams(&sock->inStream(), &sock->outStream());
  52. peerEndpoint.buf = sock->getPeerEndpoint();
  53. // Configure the socket
  54. setSocketTimeouts();
  55. // Kick off the idle timer
  56. if (rfb::Server::idleTimeout) {
  57. // minimum of 15 seconds while authenticating
  58. if (rfb::Server::idleTimeout < 15)
  59. idleTimer.start(secsToMillis(15));
  60. else
  61. idleTimer.start(secsToMillis(rfb::Server::idleTimeout));
  62. }
  63. }
  64. VNCSConnectionST::~VNCSConnectionST()
  65. {
  66. // If we reach here then VNCServerST is deleting us!
  67. if (closeReason.buf)
  68. vlog.info("closing %s: %s", peerEndpoint.buf, closeReason.buf);
  69. // Release any keys the client still had pressed
  70. while (!pressedKeys.empty()) {
  71. rdr::U32 keysym, keycode;
  72. keysym = pressedKeys.begin()->second;
  73. keycode = pressedKeys.begin()->first;
  74. pressedKeys.erase(pressedKeys.begin());
  75. vlog.debug("Releasing key 0x%x / 0x%x on client disconnect",
  76. keysym, keycode);
  77. server->keyEvent(keysym, keycode, false);
  78. }
  79. delete [] fenceData;
  80. }
  81. // SConnection methods
  82. bool VNCSConnectionST::accessCheck(AccessRights ar) const
  83. {
  84. // Reverse connections are user initiated, so they are implicitly
  85. // allowed to bypass the query
  86. if (reverseConnection)
  87. ar &= ~AccessNoQuery;
  88. return SConnection::accessCheck(ar);
  89. }
  90. void VNCSConnectionST::close(const char* reason)
  91. {
  92. // Log the reason for the close
  93. if (!closeReason.buf)
  94. closeReason.buf = strDup(reason);
  95. else
  96. vlog.debug("second close: %s (%s)", peerEndpoint.buf, reason);
  97. // Just shutdown the socket and mark our state as closing. Eventually the
  98. // calling code will call VNCServerST's removeSocket() method causing us to
  99. // be deleted.
  100. sock->shutdown();
  101. SConnection::close(reason);
  102. }
  103. // Methods called from VNCServerST
  104. bool VNCSConnectionST::init()
  105. {
  106. try {
  107. initialiseProtocol();
  108. } catch (rdr::Exception& e) {
  109. close(e.str());
  110. return false;
  111. }
  112. return true;
  113. }
  114. void VNCSConnectionST::processMessages()
  115. {
  116. if (state() == RFBSTATE_CLOSING) return;
  117. try {
  118. // - Now set appropriate socket timeouts and process data
  119. setSocketTimeouts();
  120. inProcessMessages = true;
  121. // Get the underlying TCP layer to build large packets if we send
  122. // multiple small responses.
  123. sock->cork(true);
  124. while (getInStream()->checkNoWait(1)) {
  125. if (pendingSyncFence) {
  126. syncFence = true;
  127. pendingSyncFence = false;
  128. }
  129. processMsg();
  130. if (syncFence) {
  131. writer()->writeFence(fenceFlags, fenceDataLen, fenceData);
  132. syncFence = false;
  133. }
  134. }
  135. // Flush out everything in case we go idle after this.
  136. sock->cork(false);
  137. inProcessMessages = false;
  138. // If there were anything requiring an update, try to send it here.
  139. // We wait until now with this to aggregate responses and to give
  140. // higher priority to user actions such as keyboard and pointer events.
  141. writeFramebufferUpdate();
  142. } catch (rdr::EndOfStream&) {
  143. close("Clean disconnection");
  144. } catch (rdr::Exception &e) {
  145. close(e.str());
  146. }
  147. }
  148. void VNCSConnectionST::flushSocket()
  149. {
  150. if (state() == RFBSTATE_CLOSING) return;
  151. try {
  152. setSocketTimeouts();
  153. sock->outStream().flush();
  154. // Flushing the socket might release an update that was previously
  155. // delayed because of congestion.
  156. if (sock->outStream().bufferUsage() == 0)
  157. writeFramebufferUpdate();
  158. } catch (rdr::Exception &e) {
  159. close(e.str());
  160. }
  161. }
  162. void VNCSConnectionST::pixelBufferChange()
  163. {
  164. try {
  165. if (!authenticated()) return;
  166. if (client.width() && client.height() &&
  167. (server->getPixelBuffer()->width() != client.width() ||
  168. server->getPixelBuffer()->height() != client.height()))
  169. {
  170. // We need to clip the next update to the new size, but also add any
  171. // extra bits if it's bigger. If we wanted to do this exactly, something
  172. // like the code below would do it, but at the moment we just update the
  173. // entire new size. However, we do need to clip the damagedCursorRegion
  174. // because that might be added to updates in writeFramebufferUpdate().
  175. //updates.intersect(server->pb->getRect());
  176. //
  177. //if (server->pb->width() > client.width())
  178. // updates.add_changed(Rect(client.width(), 0, server->pb->width(),
  179. // server->pb->height()));
  180. //if (server->pb->height() > client.height())
  181. // updates.add_changed(Rect(0, client.height(), client.width(),
  182. // server->pb->height()));
  183. damagedCursorRegion.assign_intersect(server->getPixelBuffer()->getRect());
  184. client.setDimensions(server->getPixelBuffer()->width(),
  185. server->getPixelBuffer()->height(),
  186. server->getScreenLayout());
  187. if (state() == RFBSTATE_NORMAL) {
  188. if (!client.supportsDesktopSize()) {
  189. close("Client does not support desktop resize");
  190. return;
  191. }
  192. writer()->writeDesktopSize(reasonServer);
  193. }
  194. // Drop any lossy tracking that is now outside the framebuffer
  195. encodeManager.pruneLosslessRefresh(Region(server->getPixelBuffer()->getRect()));
  196. }
  197. // Just update the whole screen at the moment because we're too lazy to
  198. // work out what's actually changed.
  199. updates.clear();
  200. updates.add_changed(server->getPixelBuffer()->getRect());
  201. writeFramebufferUpdate();
  202. } catch(rdr::Exception &e) {
  203. close(e.str());
  204. }
  205. }
  206. void VNCSConnectionST::writeFramebufferUpdateOrClose()
  207. {
  208. try {
  209. writeFramebufferUpdate();
  210. } catch(rdr::Exception &e) {
  211. close(e.str());
  212. }
  213. }
  214. void VNCSConnectionST::screenLayoutChangeOrClose(rdr::U16 reason)
  215. {
  216. try {
  217. screenLayoutChange(reason);
  218. writeFramebufferUpdate();
  219. } catch(rdr::Exception &e) {
  220. close(e.str());
  221. }
  222. }
  223. void VNCSConnectionST::bellOrClose()
  224. {
  225. try {
  226. if (state() == RFBSTATE_NORMAL) writer()->writeBell();
  227. } catch(rdr::Exception& e) {
  228. close(e.str());
  229. }
  230. }
  231. void VNCSConnectionST::serverCutTextOrClose(const char *str, int len)
  232. {
  233. try {
  234. if (!accessCheck(AccessCutText)) return;
  235. if (!rfb::Server::sendCutText) return;
  236. if (state() == RFBSTATE_NORMAL)
  237. writer()->writeServerCutText(str, len);
  238. } catch(rdr::Exception& e) {
  239. close(e.str());
  240. }
  241. }
  242. void VNCSConnectionST::setDesktopNameOrClose(const char *name)
  243. {
  244. try {
  245. setDesktopName(name);
  246. writeFramebufferUpdate();
  247. } catch(rdr::Exception& e) {
  248. close(e.str());
  249. }
  250. }
  251. void VNCSConnectionST::setCursorOrClose()
  252. {
  253. try {
  254. setCursor();
  255. writeFramebufferUpdate();
  256. } catch(rdr::Exception& e) {
  257. close(e.str());
  258. }
  259. }
  260. void VNCSConnectionST::setLEDStateOrClose(unsigned int state)
  261. {
  262. try {
  263. setLEDState(state);
  264. writeFramebufferUpdate();
  265. } catch(rdr::Exception& e) {
  266. close(e.str());
  267. }
  268. }
  269. bool VNCSConnectionST::getComparerState()
  270. {
  271. // We interpret a low compression level as an indication that the client
  272. // wants to prioritise CPU usage over bandwidth, and hence disable the
  273. // comparing update tracker.
  274. return (client.compressLevel == -1) || (client.compressLevel > 1);
  275. }
  276. // renderedCursorChange() is called whenever the server-side rendered cursor
  277. // changes shape or position. It ensures that the next update will clean up
  278. // the old rendered cursor and if necessary draw the new rendered cursor.
  279. void VNCSConnectionST::renderedCursorChange()
  280. {
  281. if (state() != RFBSTATE_NORMAL) return;
  282. // Are we switching between client-side and server-side cursor?
  283. if (clientHasCursor == needRenderedCursor())
  284. setCursorOrClose();
  285. bool hasRenderedCursor = !damagedCursorRegion.is_empty();
  286. if (hasRenderedCursor)
  287. removeRenderedCursor = true;
  288. if (needRenderedCursor()) {
  289. updateRenderedCursor = true;
  290. writeFramebufferUpdateOrClose();
  291. }
  292. }
  293. // needRenderedCursor() returns true if this client needs the server-side
  294. // rendered cursor. This may be because it does not support local cursor or
  295. // because the current cursor position has not been set by this client.
  296. // Unfortunately we can't know for sure when the current cursor position has
  297. // been set by this client. We guess that this is the case when the current
  298. // cursor position is the same as the last pointer event from this client, or
  299. // if it is a very short time since this client's last pointer event (up to a
  300. // second). [ Ideally we should do finer-grained timing here and make the time
  301. // configurable, but I don't think it's that important. ]
  302. bool VNCSConnectionST::needRenderedCursor()
  303. {
  304. if (state() != RFBSTATE_NORMAL)
  305. return false;
  306. if (!client.supportsLocalCursor())
  307. return true;
  308. if (!server->getCursorPos().equals(pointerEventPos) &&
  309. (time(0) - pointerEventTime) > 0)
  310. return true;
  311. return false;
  312. }
  313. void VNCSConnectionST::approveConnectionOrClose(bool accept,
  314. const char* reason)
  315. {
  316. try {
  317. approveConnection(accept, reason);
  318. } catch (rdr::Exception& e) {
  319. close(e.str());
  320. }
  321. }
  322. // -=- Callbacks from SConnection
  323. void VNCSConnectionST::authSuccess()
  324. {
  325. if (rfb::Server::idleTimeout)
  326. idleTimer.start(secsToMillis(rfb::Server::idleTimeout));
  327. // - Set the connection parameters appropriately
  328. client.setDimensions(server->getPixelBuffer()->width(),
  329. server->getPixelBuffer()->height(),
  330. server->getScreenLayout());
  331. client.setName(server->getName());
  332. client.setLEDState(server->getLEDState());
  333. // - Set the default pixel format
  334. client.setPF(server->getPixelBuffer()->getPF());
  335. char buffer[256];
  336. client.pf().print(buffer, 256);
  337. vlog.info("Server default pixel format %s", buffer);
  338. // - Mark the entire display as "dirty"
  339. updates.add_changed(server->getPixelBuffer()->getRect());
  340. }
  341. void VNCSConnectionST::queryConnection(const char* userName)
  342. {
  343. server->queryConnection(this, userName);
  344. }
  345. void VNCSConnectionST::clientInit(bool shared)
  346. {
  347. if (rfb::Server::idleTimeout)
  348. idleTimer.start(secsToMillis(rfb::Server::idleTimeout));
  349. if (rfb::Server::alwaysShared || reverseConnection) shared = true;
  350. if (!accessCheck(AccessNonShared)) shared = true;
  351. if (rfb::Server::neverShared) shared = false;
  352. SConnection::clientInit(shared);
  353. server->clientReady(this, shared);
  354. }
  355. void VNCSConnectionST::setPixelFormat(const PixelFormat& pf)
  356. {
  357. SConnection::setPixelFormat(pf);
  358. char buffer[256];
  359. pf.print(buffer, 256);
  360. vlog.info("Client pixel format %s", buffer);
  361. setCursor();
  362. }
  363. void VNCSConnectionST::pointerEvent(const Point& pos, int buttonMask)
  364. {
  365. if (rfb::Server::idleTimeout)
  366. idleTimer.start(secsToMillis(rfb::Server::idleTimeout));
  367. pointerEventTime = time(0);
  368. if (!accessCheck(AccessPtrEvents)) return;
  369. if (!rfb::Server::acceptPointerEvents) return;
  370. pointerEventPos = pos;
  371. server->pointerEvent(this, pointerEventPos, buttonMask);
  372. }
  373. class VNCSConnectionSTShiftPresser {
  374. public:
  375. VNCSConnectionSTShiftPresser(VNCServerST* server_)
  376. : server(server_), pressed(false) {}
  377. ~VNCSConnectionSTShiftPresser() {
  378. if (pressed) {
  379. vlog.debug("Releasing fake Shift_L");
  380. server->keyEvent(XK_Shift_L, 0, false);
  381. }
  382. }
  383. void press() {
  384. vlog.debug("Pressing fake Shift_L");
  385. server->keyEvent(XK_Shift_L, 0, true);
  386. pressed = true;
  387. }
  388. VNCServerST* server;
  389. bool pressed;
  390. };
  391. // keyEvent() - record in the pressedKeys which keys were pressed. Allow
  392. // multiple down events (for autorepeat), but only allow a single up event.
  393. void VNCSConnectionST::keyEvent(rdr::U32 keysym, rdr::U32 keycode, bool down) {
  394. rdr::U32 lookup;
  395. if (rfb::Server::idleTimeout)
  396. idleTimer.start(secsToMillis(rfb::Server::idleTimeout));
  397. if (!accessCheck(AccessKeyEvents)) return;
  398. if (!rfb::Server::acceptKeyEvents) return;
  399. if (down)
  400. vlog.debug("Key pressed: 0x%x / 0x%x", keysym, keycode);
  401. else
  402. vlog.debug("Key released: 0x%x / 0x%x", keysym, keycode);
  403. // Avoid lock keys if we don't know the server state
  404. if ((server->getLEDState() == ledUnknown) &&
  405. ((keysym == XK_Caps_Lock) ||
  406. (keysym == XK_Num_Lock) ||
  407. (keysym == XK_Scroll_Lock))) {
  408. vlog.debug("Ignoring lock key (e.g. caps lock)");
  409. return;
  410. }
  411. // Lock key heuristics
  412. // (only for clients that do not support the LED state extension)
  413. if (!client.supportsLEDState()) {
  414. // Always ignore ScrollLock as we don't have a heuristic
  415. // for that
  416. if (keysym == XK_Scroll_Lock) {
  417. vlog.debug("Ignoring lock key (e.g. caps lock)");
  418. return;
  419. }
  420. if (down && (server->getLEDState() != ledUnknown)) {
  421. // CapsLock synchronisation heuristic
  422. // (this assumes standard interaction between CapsLock the Shift
  423. // keys and normal characters)
  424. if (((keysym >= XK_A) && (keysym <= XK_Z)) ||
  425. ((keysym >= XK_a) && (keysym <= XK_z))) {
  426. bool uppercase, shift, lock;
  427. uppercase = (keysym >= XK_A) && (keysym <= XK_Z);
  428. shift = isShiftPressed();
  429. lock = server->getLEDState() & ledCapsLock;
  430. if (lock == (uppercase == shift)) {
  431. vlog.debug("Inserting fake CapsLock to get in sync with client");
  432. server->keyEvent(XK_Caps_Lock, 0, true);
  433. server->keyEvent(XK_Caps_Lock, 0, false);
  434. }
  435. }
  436. // NumLock synchronisation heuristic
  437. // (this is more cautious because of the differences between Unix,
  438. // Windows and macOS)
  439. if (((keysym >= XK_KP_Home) && (keysym <= XK_KP_Delete)) ||
  440. ((keysym >= XK_KP_0) && (keysym <= XK_KP_9)) ||
  441. (keysym == XK_KP_Separator) || (keysym == XK_KP_Decimal)) {
  442. bool number, shift, lock;
  443. number = ((keysym >= XK_KP_0) && (keysym <= XK_KP_9)) ||
  444. (keysym == XK_KP_Separator) || (keysym == XK_KP_Decimal);
  445. shift = isShiftPressed();
  446. lock = server->getLEDState() & ledNumLock;
  447. if (shift) {
  448. // We don't know the appropriate NumLock state for when Shift
  449. // is pressed as it could be one of:
  450. //
  451. // a) A Unix client where Shift negates NumLock
  452. //
  453. // b) A Windows client where Shift only cancels NumLock
  454. //
  455. // c) A macOS client where Shift doesn't have any effect
  456. //
  457. } else if (lock == (number == shift)) {
  458. vlog.debug("Inserting fake NumLock to get in sync with client");
  459. server->keyEvent(XK_Num_Lock, 0, true);
  460. server->keyEvent(XK_Num_Lock, 0, false);
  461. }
  462. }
  463. }
  464. }
  465. // Turn ISO_Left_Tab into shifted Tab.
  466. VNCSConnectionSTShiftPresser shiftPresser(server);
  467. if (keysym == XK_ISO_Left_Tab) {
  468. if (!isShiftPressed())
  469. shiftPresser.press();
  470. keysym = XK_Tab;
  471. }
  472. // We need to be able to track keys, so generate a fake index when we
  473. // aren't given a keycode
  474. if (keycode == 0)
  475. lookup = 0x80000000 | keysym;
  476. else
  477. lookup = keycode;
  478. // We force the same keysym for an already down key for the
  479. // sake of sanity
  480. if (pressedKeys.find(lookup) != pressedKeys.end())
  481. keysym = pressedKeys[lookup];
  482. if (down) {
  483. pressedKeys[lookup] = keysym;
  484. } else {
  485. if (!pressedKeys.erase(lookup))
  486. return;
  487. }
  488. server->keyEvent(keysym, keycode, down);
  489. }
  490. void VNCSConnectionST::clientCutText(const char* str, int len)
  491. {
  492. if (!accessCheck(AccessCutText)) return;
  493. if (!rfb::Server::acceptCutText) return;
  494. server->clientCutText(str, len);
  495. }
  496. void VNCSConnectionST::framebufferUpdateRequest(const Rect& r,bool incremental)
  497. {
  498. Rect safeRect;
  499. if (!accessCheck(AccessView)) return;
  500. SConnection::framebufferUpdateRequest(r, incremental);
  501. // Check that the client isn't sending crappy requests
  502. if (!r.enclosed_by(Rect(0, 0, client.width(), client.height()))) {
  503. vlog.error("FramebufferUpdateRequest %dx%d at %d,%d exceeds framebuffer %dx%d",
  504. r.width(), r.height(), r.tl.x, r.tl.y,
  505. client.width(), client.height());
  506. safeRect = r.intersect(Rect(0, 0, client.width(), client.height()));
  507. } else {
  508. safeRect = r;
  509. }
  510. // Just update the requested region.
  511. // Framebuffer update will be sent a bit later, see processMessages().
  512. Region reqRgn(r);
  513. if (!incremental || !continuousUpdates)
  514. requested.assign_union(reqRgn);
  515. if (!incremental) {
  516. // Non-incremental update - treat as if area requested has changed
  517. updates.add_changed(reqRgn);
  518. // And send the screen layout to the client (which, unlike the
  519. // framebuffer dimensions, the client doesn't get during init)
  520. if (client.supportsEncoding(pseudoEncodingExtendedDesktopSize))
  521. writer()->writeDesktopSize(reasonServer);
  522. // We do not send a DesktopSize since it only contains the
  523. // framebuffer size (which the client already should know) and
  524. // because some clients don't handle extra DesktopSize events
  525. // very well.
  526. }
  527. }
  528. void VNCSConnectionST::setDesktopSize(int fb_width, int fb_height,
  529. const ScreenSet& layout)
  530. {
  531. unsigned int result;
  532. if (!accessCheck(AccessSetDesktopSize)) return;
  533. if (!rfb::Server::acceptSetDesktopSize) return;
  534. result = server->setDesktopSize(this, fb_width, fb_height, layout);
  535. writer()->writeDesktopSize(reasonClient, result);
  536. }
  537. void VNCSConnectionST::fence(rdr::U32 flags, unsigned len, const char data[])
  538. {
  539. rdr::U8 type;
  540. if (flags & fenceFlagRequest) {
  541. if (flags & fenceFlagSyncNext) {
  542. pendingSyncFence = true;
  543. fenceFlags = flags & (fenceFlagBlockBefore | fenceFlagBlockAfter | fenceFlagSyncNext);
  544. fenceDataLen = len;
  545. delete [] fenceData;
  546. fenceData = NULL;
  547. if (len > 0) {
  548. fenceData = new char[len];
  549. memcpy(fenceData, data, len);
  550. }
  551. return;
  552. }
  553. // We handle everything synchronously so we trivially honor these modes
  554. flags = flags & (fenceFlagBlockBefore | fenceFlagBlockAfter);
  555. writer()->writeFence(flags, len, data);
  556. return;
  557. }
  558. if (len < 1)
  559. vlog.error("Fence response of unexpected size received");
  560. type = data[0];
  561. switch (type) {
  562. case 0:
  563. // Initial dummy fence;
  564. break;
  565. case 1:
  566. congestion.gotPong();
  567. break;
  568. default:
  569. vlog.error("Fence response of unexpected type received");
  570. }
  571. }
  572. void VNCSConnectionST::enableContinuousUpdates(bool enable,
  573. int x, int y, int w, int h)
  574. {
  575. Rect rect;
  576. if (!client.supportsFence() || !client.supportsContinuousUpdates())
  577. throw Exception("Client tried to enable continuous updates when not allowed");
  578. continuousUpdates = enable;
  579. rect.setXYWH(x, y, w, h);
  580. cuRegion.reset(rect);
  581. if (enable) {
  582. requested.clear();
  583. } else {
  584. writer()->writeEndOfContinuousUpdates();
  585. }
  586. }
  587. // supportsLocalCursor() is called whenever the status of
  588. // client.supportsLocalCursor() has changed. If the client does now support local
  589. // cursor, we make sure that the old server-side rendered cursor is cleaned up
  590. // and the cursor is sent to the client.
  591. void VNCSConnectionST::supportsLocalCursor()
  592. {
  593. bool hasRenderedCursor = !damagedCursorRegion.is_empty();
  594. if (hasRenderedCursor && !needRenderedCursor())
  595. removeRenderedCursor = true;
  596. setCursor();
  597. }
  598. void VNCSConnectionST::supportsFence()
  599. {
  600. char type = 0;
  601. writer()->writeFence(fenceFlagRequest, sizeof(type), &type);
  602. }
  603. void VNCSConnectionST::supportsContinuousUpdates()
  604. {
  605. // We refuse to use continuous updates if we cannot monitor the buffer
  606. // usage using fences.
  607. if (!client.supportsFence())
  608. return;
  609. writer()->writeEndOfContinuousUpdates();
  610. }
  611. void VNCSConnectionST::supportsLEDState()
  612. {
  613. if (client.ledState() == ledUnknown)
  614. return;
  615. writer()->writeLEDState();
  616. }
  617. bool VNCSConnectionST::handleTimeout(Timer* t)
  618. {
  619. try {
  620. if ((t == &congestionTimer) ||
  621. (t == &losslessTimer))
  622. writeFramebufferUpdate();
  623. } catch (rdr::Exception& e) {
  624. close(e.str());
  625. }
  626. if (t == &idleTimer)
  627. close("Idle timeout");
  628. return false;
  629. }
  630. bool VNCSConnectionST::isShiftPressed()
  631. {
  632. std::map<rdr::U32, rdr::U32>::const_iterator iter;
  633. for (iter = pressedKeys.begin(); iter != pressedKeys.end(); ++iter) {
  634. if (iter->second == XK_Shift_L)
  635. return true;
  636. if (iter->second == XK_Shift_R)
  637. return true;
  638. }
  639. return false;
  640. }
  641. void VNCSConnectionST::writeRTTPing()
  642. {
  643. char type;
  644. if (!client.supportsFence())
  645. return;
  646. congestion.updatePosition(sock->outStream().length());
  647. // We need to make sure any old update are already processed by the
  648. // time we get the response back. This allows us to reliably throttle
  649. // back on client overload, as well as network overload.
  650. type = 1;
  651. writer()->writeFence(fenceFlagRequest | fenceFlagBlockBefore,
  652. sizeof(type), &type);
  653. congestion.sentPing();
  654. }
  655. bool VNCSConnectionST::isCongested()
  656. {
  657. int eta;
  658. congestionTimer.stop();
  659. // Stuff still waiting in the send buffer?
  660. sock->outStream().flush();
  661. congestion.debugTrace("congestion-trace.csv", sock->getFd());
  662. if (sock->outStream().bufferUsage() > 0)
  663. return true;
  664. if (!client.supportsFence())
  665. return false;
  666. congestion.updatePosition(sock->outStream().length());
  667. if (!congestion.isCongested())
  668. return false;
  669. eta = congestion.getUncongestedETA();
  670. if (eta >= 0)
  671. congestionTimer.start(eta);
  672. return true;
  673. }
  674. void VNCSConnectionST::writeFramebufferUpdate()
  675. {
  676. congestion.updatePosition(sock->outStream().length());
  677. // We're in the middle of processing a command that's supposed to be
  678. // synchronised. Allowing an update to slip out right now might violate
  679. // that synchronisation.
  680. if (syncFence)
  681. return;
  682. // We try to aggregate responses, so don't send out anything whilst we
  683. // still have incoming messages. processMessages() will give us another
  684. // chance to run once things are idle.
  685. if (inProcessMessages)
  686. return;
  687. if (state() != RFBSTATE_NORMAL)
  688. return;
  689. if (requested.is_empty() && !continuousUpdates)
  690. return;
  691. // Check that we actually have some space on the link and retry in a
  692. // bit if things are congested.
  693. if (isCongested())
  694. return;
  695. // Updates often consists of many small writes, and in continuous
  696. // mode, we will also have small fence messages around the update. We
  697. // need to aggregate these in order to not clog up TCP's congestion
  698. // window.
  699. sock->cork(true);
  700. // First take care of any updates that cannot contain framebuffer data
  701. // changes.
  702. writeNoDataUpdate();
  703. // Then real data (if possible)
  704. writeDataUpdate();
  705. sock->cork(false);
  706. congestion.updatePosition(sock->outStream().length());
  707. }
  708. void VNCSConnectionST::writeNoDataUpdate()
  709. {
  710. if (!writer()->needNoDataUpdate())
  711. return;
  712. writer()->writeNoDataUpdate();
  713. // Make sure no data update is sent until next request
  714. requested.clear();
  715. }
  716. void VNCSConnectionST::writeDataUpdate()
  717. {
  718. Region req;
  719. UpdateInfo ui;
  720. bool needNewUpdateInfo;
  721. const RenderedCursor *cursor;
  722. // See what the client has requested (if anything)
  723. if (continuousUpdates)
  724. req = cuRegion.union_(requested);
  725. else
  726. req = requested;
  727. if (req.is_empty())
  728. return;
  729. // Get the lists of updates. Prior to exporting the data to the `ui' object,
  730. // getUpdateInfo() will normalize the `updates' object such way that its
  731. // `changed' and `copied' regions would not intersect.
  732. updates.getUpdateInfo(&ui, req);
  733. needNewUpdateInfo = false;
  734. // If the previous position of the rendered cursor overlaps the source of the
  735. // copy, then when the copy happens the corresponding rectangle in the
  736. // destination will be wrong, so add it to the changed region.
  737. if (!ui.copied.is_empty() && !damagedCursorRegion.is_empty()) {
  738. Region bogusCopiedCursor;
  739. bogusCopiedCursor = damagedCursorRegion;
  740. bogusCopiedCursor.translate(ui.copy_delta);
  741. bogusCopiedCursor.assign_intersect(server->getPixelBuffer()->getRect());
  742. if (!ui.copied.intersect(bogusCopiedCursor).is_empty()) {
  743. updates.add_changed(bogusCopiedCursor);
  744. needNewUpdateInfo = true;
  745. }
  746. }
  747. // If we need to remove the old rendered cursor, just add the region to
  748. // the changed region.
  749. if (removeRenderedCursor) {
  750. updates.add_changed(damagedCursorRegion);
  751. needNewUpdateInfo = true;
  752. damagedCursorRegion.clear();
  753. removeRenderedCursor = false;
  754. }
  755. // If we need a full cursor update then make sure its entire region
  756. // is marked as changed.
  757. if (updateRenderedCursor) {
  758. updates.add_changed(server->getRenderedCursor()->getEffectiveRect());
  759. needNewUpdateInfo = true;
  760. updateRenderedCursor = false;
  761. }
  762. // The `updates' object could change, make sure we have valid update info.
  763. if (needNewUpdateInfo)
  764. updates.getUpdateInfo(&ui, req);
  765. // If there are queued updates then we cannot safely send an update
  766. // without risking a partially updated screen
  767. if (!server->getPendingRegion().is_empty()) {
  768. req.clear();
  769. ui.changed.clear();
  770. ui.copied.clear();
  771. }
  772. // Does the client need a server-side rendered cursor?
  773. cursor = NULL;
  774. if (needRenderedCursor()) {
  775. Rect renderedCursorRect;
  776. cursor = server->getRenderedCursor();
  777. renderedCursorRect = cursor->getEffectiveRect();
  778. // Check that we don't try to copy over the cursor area, and
  779. // if that happens we need to treat it as changed so that we can
  780. // re-render it
  781. if (!ui.copied.intersect(renderedCursorRect).is_empty()) {
  782. ui.changed.assign_union(ui.copied.intersect(renderedCursorRect));
  783. ui.copied.assign_subtract(renderedCursorRect);
  784. }
  785. // Track where we've rendered the cursor
  786. damagedCursorRegion.assign_union(ui.changed.intersect(renderedCursorRect));
  787. }
  788. // If we don't have a normal update, then try a lossless refresh
  789. if (ui.is_empty() && !writer()->needFakeUpdate()) {
  790. writeLosslessRefresh();
  791. return;
  792. }
  793. // We have something to send, so let's get to it
  794. writeRTTPing();
  795. encodeManager.writeUpdate(ui, server->getPixelBuffer(), cursor);
  796. writeRTTPing();
  797. // The request might be for just part of the screen, so we cannot
  798. // just clear the entire update tracker.
  799. updates.subtract(req);
  800. requested.clear();
  801. }
  802. void VNCSConnectionST::writeLosslessRefresh()
  803. {
  804. Region req, pending;
  805. const RenderedCursor *cursor;
  806. int nextRefresh, nextUpdate;
  807. size_t bandwidth, maxUpdateSize;
  808. if (continuousUpdates)
  809. req = cuRegion.union_(requested);
  810. else
  811. req = requested;
  812. // If there are queued updates then we could not safely send an
  813. // update without risking a partially updated screen, however we
  814. // might still be able to send a lossless refresh
  815. pending = server->getPendingRegion();
  816. if (!pending.is_empty()) {
  817. UpdateInfo ui;
  818. // Don't touch the updates pending in the server core
  819. req.assign_subtract(pending);
  820. // Or any updates pending just for this connection
  821. updates.getUpdateInfo(&ui, req);
  822. req.assign_subtract(ui.changed);
  823. req.assign_subtract(ui.copied);
  824. }
  825. // Any lossy area we can refresh?
  826. if (!encodeManager.needsLosslessRefresh(req))
  827. return;
  828. // Right away? Or later?
  829. nextRefresh = encodeManager.getNextLosslessRefresh(req);
  830. if (nextRefresh > 0) {
  831. losslessTimer.start(nextRefresh);
  832. return;
  833. }
  834. // Prepare the cursor in case it overlaps with a region getting
  835. // refreshed
  836. cursor = NULL;
  837. if (needRenderedCursor())
  838. cursor = server->getRenderedCursor();
  839. // FIXME: If continuous updates aren't used then the client might
  840. // be slower than frameRate in its requests and we could
  841. // afford a larger update size
  842. nextUpdate = server->msToNextUpdate();
  843. // Don't bother if we're about to send a real update
  844. if (nextUpdate == 0)
  845. return;
  846. // FIXME: Bandwidth estimation without congestion control
  847. bandwidth = congestion.getBandwidth();
  848. // FIXME: Hard coded value for maximum CPU throughput
  849. if (bandwidth > 5000000)
  850. bandwidth = 5000000;
  851. maxUpdateSize = bandwidth * nextUpdate / 1000;
  852. writeRTTPing();
  853. encodeManager.writeLosslessRefresh(req, server->getPixelBuffer(),
  854. cursor, maxUpdateSize);
  855. writeRTTPing();
  856. requested.clear();
  857. }
  858. void VNCSConnectionST::screenLayoutChange(rdr::U16 reason)
  859. {
  860. if (!authenticated())
  861. return;
  862. client.setDimensions(client.width(), client.height(),
  863. server->getScreenLayout());
  864. if (state() != RFBSTATE_NORMAL)
  865. return;
  866. writer()->writeDesktopSize(reason);
  867. }
  868. // setCursor() is called whenever the cursor has changed shape or pixel format.
  869. // If the client supports local cursor then it will arrange for the cursor to
  870. // be sent to the client.
  871. void VNCSConnectionST::setCursor()
  872. {
  873. if (state() != RFBSTATE_NORMAL)
  874. return;
  875. // We need to blank out the client's cursor or there will be two
  876. if (needRenderedCursor()) {
  877. client.setCursor(emptyCursor);
  878. clientHasCursor = false;
  879. } else {
  880. client.setCursor(*server->getCursor());
  881. clientHasCursor = true;
  882. }
  883. if (client.supportsLocalCursor())
  884. writer()->writeCursor();
  885. }
  886. void VNCSConnectionST::setDesktopName(const char *name)
  887. {
  888. client.setName(name);
  889. if (state() != RFBSTATE_NORMAL)
  890. return;
  891. if (client.supportsEncoding(pseudoEncodingDesktopName))
  892. writer()->writeSetDesktopName();
  893. }
  894. void VNCSConnectionST::setLEDState(unsigned int ledstate)
  895. {
  896. if (state() != RFBSTATE_NORMAL)
  897. return;
  898. client.setLEDState(ledstate);
  899. if (client.supportsLEDState())
  900. writer()->writeLEDState();
  901. }
  902. void VNCSConnectionST::setSocketTimeouts()
  903. {
  904. int timeoutms = rfb::Server::clientWaitTimeMillis;
  905. if (timeoutms == 0)
  906. timeoutms = -1;
  907. sock->inStream().setTimeout(timeoutms);
  908. sock->outStream().setTimeout(timeoutms);
  909. }