Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 1 | /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved. |
Pierre Ossman | a4c0aac | 2017-02-19 15:50:29 +0100 | [diff] [blame] | 2 | * Copyright 2011-2017 Pierre Ossman for Cendio AB |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 3 | * |
| 4 | * This is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This software is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this software; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
| 17 | * USA. |
| 18 | */ |
| 19 | #include <stdio.h> |
| 20 | #include <string.h> |
Pierre Ossman | 0068a4f | 2015-11-09 15:48:19 +0100 | [diff] [blame] | 21 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 22 | #include <rfb/Exception.h> |
Pierre Ossman | c754cce | 2011-11-14 15:44:11 +0000 | [diff] [blame] | 23 | #include <rfb/fenceTypes.h> |
Pierre Ossman | 7638e9c | 2014-01-16 13:12:40 +0100 | [diff] [blame] | 24 | #include <rfb/CMsgReader.h> |
| 25 | #include <rfb/CMsgWriter.h> |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 26 | #include <rfb/CSecurity.h> |
Adam Tkac | 5a0caed | 2010-04-23 13:58:10 +0000 | [diff] [blame] | 27 | #include <rfb/Security.h> |
Pierre Ossman | 0068a4f | 2015-11-09 15:48:19 +0100 | [diff] [blame] | 28 | #include <rfb/SecurityClient.h> |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 29 | #include <rfb/CConnection.h> |
| 30 | #include <rfb/util.h> |
| 31 | |
| 32 | #include <rfb/LogWriter.h> |
| 33 | |
Pierre Ossman | 0068a4f | 2015-11-09 15:48:19 +0100 | [diff] [blame] | 34 | #include <rdr/InStream.h> |
| 35 | #include <rdr/OutStream.h> |
| 36 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 37 | using namespace rfb; |
| 38 | |
| 39 | static LogWriter vlog("CConnection"); |
| 40 | |
| 41 | CConnection::CConnection() |
Adam Tkac | f324dc4 | 2010-04-23 14:10:17 +0000 | [diff] [blame] | 42 | : csecurity(0), is(0), os(0), reader_(0), writer_(0), |
Adam Tkac | 05a0cd6 | 2010-07-20 15:07:44 +0000 | [diff] [blame] | 43 | shared(false), |
Pierre Ossman | 9f273e9 | 2015-11-09 16:34:54 +0100 | [diff] [blame] | 44 | state_(RFBSTATE_UNINITIALISED), useProtocol3_3(false), |
| 45 | framebuffer(NULL), decoder(this) |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 46 | { |
Adam Tkac | bfd66c1 | 2010-10-01 08:33:29 +0000 | [diff] [blame] | 47 | security = new SecurityClient(); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | CConnection::~CConnection() |
| 51 | { |
Pierre Ossman | 9f273e9 | 2015-11-09 16:34:54 +0100 | [diff] [blame] | 52 | setFramebuffer(NULL); |
Adam Tkac | f324dc4 | 2010-04-23 14:10:17 +0000 | [diff] [blame] | 53 | if (csecurity) csecurity->destroy(); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 54 | delete reader_; |
| 55 | reader_ = 0; |
| 56 | delete writer_; |
| 57 | writer_ = 0; |
| 58 | } |
| 59 | |
| 60 | void CConnection::setStreams(rdr::InStream* is_, rdr::OutStream* os_) |
| 61 | { |
| 62 | is = is_; |
| 63 | os = os_; |
| 64 | } |
| 65 | |
Pierre Ossman | 9f273e9 | 2015-11-09 16:34:54 +0100 | [diff] [blame] | 66 | void CConnection::setFramebuffer(ModifiablePixelBuffer* fb) |
| 67 | { |
Pierre Ossman | 504afa2 | 2015-11-12 12:21:58 +0100 | [diff] [blame] | 68 | decoder.flush(); |
| 69 | |
Pierre Ossman | 9f273e9 | 2015-11-09 16:34:54 +0100 | [diff] [blame] | 70 | if ((framebuffer != NULL) && (fb != NULL)) { |
| 71 | Rect rect; |
| 72 | |
| 73 | const rdr::U8* data; |
| 74 | int stride; |
| 75 | |
| 76 | const rdr::U8 black[4] = { 0, 0, 0, 0 }; |
| 77 | |
| 78 | // Copy still valid area |
| 79 | |
| 80 | rect.setXYWH(0, 0, |
| 81 | __rfbmin(fb->width(), framebuffer->width()), |
| 82 | __rfbmin(fb->height(), framebuffer->height())); |
| 83 | data = framebuffer->getBuffer(framebuffer->getRect(), &stride); |
| 84 | fb->imageRect(rect, data, stride); |
| 85 | |
| 86 | // Black out any new areas |
| 87 | |
| 88 | if (fb->width() > framebuffer->width()) { |
| 89 | rect.setXYWH(framebuffer->width(), 0, |
Brian P. Hinz | 5d66305 | 2016-09-05 09:15:50 -0400 | [diff] [blame] | 90 | fb->width() - framebuffer->width(), |
Pierre Ossman | 9f273e9 | 2015-11-09 16:34:54 +0100 | [diff] [blame] | 91 | fb->height()); |
| 92 | fb->fillRect(rect, black); |
| 93 | } |
| 94 | |
| 95 | if (fb->height() > framebuffer->height()) { |
| 96 | rect.setXYWH(0, framebuffer->height(), |
| 97 | fb->width(), |
| 98 | fb->height() - framebuffer->height()); |
| 99 | fb->fillRect(rect, black); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | delete framebuffer; |
| 104 | framebuffer = fb; |
| 105 | } |
| 106 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 107 | void CConnection::initialiseProtocol() |
| 108 | { |
| 109 | state_ = RFBSTATE_PROTOCOL_VERSION; |
| 110 | } |
| 111 | |
| 112 | void CConnection::processMsg() |
| 113 | { |
| 114 | switch (state_) { |
| 115 | |
| 116 | case RFBSTATE_PROTOCOL_VERSION: processVersionMsg(); break; |
| 117 | case RFBSTATE_SECURITY_TYPES: processSecurityTypesMsg(); break; |
| 118 | case RFBSTATE_SECURITY: processSecurityMsg(); break; |
| 119 | case RFBSTATE_SECURITY_RESULT: processSecurityResultMsg(); break; |
| 120 | case RFBSTATE_INITIALISATION: processInitMsg(); break; |
| 121 | case RFBSTATE_NORMAL: reader_->readMsg(); break; |
| 122 | case RFBSTATE_UNINITIALISED: |
| 123 | throw Exception("CConnection::processMsg: not initialised yet?"); |
| 124 | default: |
| 125 | throw Exception("CConnection::processMsg: invalid state"); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | void CConnection::processVersionMsg() |
| 130 | { |
| 131 | vlog.debug("reading protocol version"); |
| 132 | bool done; |
| 133 | if (!cp.readVersion(is, &done)) { |
| 134 | state_ = RFBSTATE_INVALID; |
| 135 | throw Exception("reading version failed: not an RFB server?"); |
| 136 | } |
| 137 | if (!done) return; |
| 138 | |
| 139 | vlog.info("Server supports RFB protocol version %d.%d", |
| 140 | cp.majorVersion, cp.minorVersion); |
| 141 | |
| 142 | // The only official RFB protocol versions are currently 3.3, 3.7 and 3.8 |
| 143 | if (cp.beforeVersion(3,3)) { |
Pierre Ossman | a7bbe9c | 2015-03-03 16:17:51 +0100 | [diff] [blame] | 144 | vlog.error("Server gave unsupported RFB protocol version %d.%d", |
| 145 | cp.majorVersion, cp.minorVersion); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 146 | state_ = RFBSTATE_INVALID; |
Pierre Ossman | a7bbe9c | 2015-03-03 16:17:51 +0100 | [diff] [blame] | 147 | throw Exception("Server gave unsupported RFB protocol version %d.%d", |
| 148 | cp.majorVersion, cp.minorVersion); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 149 | } else if (useProtocol3_3 || cp.beforeVersion(3,7)) { |
| 150 | cp.setVersion(3,3); |
| 151 | } else if (cp.afterVersion(3,8)) { |
| 152 | cp.setVersion(3,8); |
| 153 | } |
| 154 | |
| 155 | cp.writeVersion(os); |
| 156 | state_ = RFBSTATE_SECURITY_TYPES; |
| 157 | |
| 158 | vlog.info("Using RFB protocol version %d.%d", |
| 159 | cp.majorVersion, cp.minorVersion); |
| 160 | } |
| 161 | |
| 162 | |
| 163 | void CConnection::processSecurityTypesMsg() |
| 164 | { |
| 165 | vlog.debug("processing security types message"); |
| 166 | |
| 167 | int secType = secTypeInvalid; |
| 168 | |
Adam Tkac | 05a0cd6 | 2010-07-20 15:07:44 +0000 | [diff] [blame] | 169 | std::list<rdr::U8> secTypes; |
| 170 | secTypes = security->GetEnabledSecTypes(); |
| 171 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 172 | if (cp.isVersion(3,3)) { |
| 173 | |
| 174 | // legacy 3.3 server may only offer "vnc authentication" or "none" |
| 175 | |
| 176 | secType = is->readU32(); |
| 177 | if (secType == secTypeInvalid) { |
| 178 | throwConnFailedException(); |
| 179 | |
| 180 | } else if (secType == secTypeNone || secType == secTypeVncAuth) { |
Adam Tkac | 05a0cd6 | 2010-07-20 15:07:44 +0000 | [diff] [blame] | 181 | std::list<rdr::U8>::iterator i; |
| 182 | for (i = secTypes.begin(); i != secTypes.end(); i++) |
| 183 | if (*i == secType) { |
| 184 | secType = *i; |
| 185 | break; |
| 186 | } |
| 187 | |
| 188 | if (i == secTypes.end()) |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 189 | secType = secTypeInvalid; |
| 190 | } else { |
| 191 | vlog.error("Unknown 3.3 security type %d", secType); |
| 192 | throw Exception("Unknown 3.3 security type"); |
| 193 | } |
| 194 | |
| 195 | } else { |
| 196 | |
| 197 | // >=3.7 server will offer us a list |
| 198 | |
| 199 | int nServerSecTypes = is->readU8(); |
| 200 | if (nServerSecTypes == 0) |
| 201 | throwConnFailedException(); |
| 202 | |
Adam Tkac | 05a0cd6 | 2010-07-20 15:07:44 +0000 | [diff] [blame] | 203 | std::list<rdr::U8>::iterator j; |
Adam Tkac | 05a0cd6 | 2010-07-20 15:07:44 +0000 | [diff] [blame] | 204 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 205 | for (int i = 0; i < nServerSecTypes; i++) { |
| 206 | rdr::U8 serverSecType = is->readU8(); |
| 207 | vlog.debug("Server offers security type %s(%d)", |
Adam Tkac | 7cb47d6 | 2011-02-21 12:55:24 +0000 | [diff] [blame] | 208 | secTypeName(serverSecType), serverSecType); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 209 | |
Adam Tkac | 7cb47d6 | 2011-02-21 12:55:24 +0000 | [diff] [blame] | 210 | /* |
| 211 | * Use the first type sent by server which matches client's type. |
| 212 | * It means server's order specifies priority. |
| 213 | */ |
| 214 | if (secType == secTypeInvalid) { |
| 215 | for (j = secTypes.begin(); j != secTypes.end(); j++) |
| 216 | if (*j == serverSecType) { |
| 217 | secType = *j; |
| 218 | break; |
| 219 | } |
| 220 | } |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | // Inform the server of our decision |
| 224 | if (secType != secTypeInvalid) { |
| 225 | os->writeU8(secType); |
| 226 | os->flush(); |
Pierre Ossman | 71d6666 | 2014-11-11 13:42:51 +0100 | [diff] [blame] | 227 | vlog.info("Choosing security type %s(%d)",secTypeName(secType),secType); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 228 | } |
| 229 | } |
| 230 | |
| 231 | if (secType == secTypeInvalid) { |
| 232 | state_ = RFBSTATE_INVALID; |
| 233 | vlog.error("No matching security types"); |
| 234 | throw Exception("No matching security types"); |
| 235 | } |
| 236 | |
| 237 | state_ = RFBSTATE_SECURITY; |
Adam Tkac | f324dc4 | 2010-04-23 14:10:17 +0000 | [diff] [blame] | 238 | csecurity = security->GetCSecurity(secType); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 239 | processSecurityMsg(); |
| 240 | } |
| 241 | |
| 242 | void CConnection::processSecurityMsg() |
| 243 | { |
| 244 | vlog.debug("processing security message"); |
Adam Tkac | f324dc4 | 2010-04-23 14:10:17 +0000 | [diff] [blame] | 245 | if (csecurity->processMsg(this)) { |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 246 | state_ = RFBSTATE_SECURITY_RESULT; |
| 247 | processSecurityResultMsg(); |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | void CConnection::processSecurityResultMsg() |
| 252 | { |
| 253 | vlog.debug("processing security result message"); |
| 254 | int result; |
Adam Tkac | f324dc4 | 2010-04-23 14:10:17 +0000 | [diff] [blame] | 255 | if (cp.beforeVersion(3,8) && csecurity->getType() == secTypeNone) { |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 256 | result = secResultOK; |
| 257 | } else { |
| 258 | if (!is->checkNoWait(1)) return; |
| 259 | result = is->readU32(); |
| 260 | } |
| 261 | switch (result) { |
| 262 | case secResultOK: |
| 263 | securityCompleted(); |
| 264 | return; |
| 265 | case secResultFailed: |
| 266 | vlog.debug("auth failed"); |
| 267 | break; |
| 268 | case secResultTooMany: |
| 269 | vlog.debug("auth failed - too many tries"); |
| 270 | break; |
| 271 | default: |
| 272 | throw Exception("Unknown security result from server"); |
| 273 | } |
| 274 | CharArray reason; |
| 275 | if (cp.beforeVersion(3,8)) |
Adam Tkac | d36b626 | 2009-09-04 10:57:20 +0000 | [diff] [blame] | 276 | reason.buf = strDup("Authentication failure"); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 277 | else |
| 278 | reason.buf = is->readString(); |
| 279 | state_ = RFBSTATE_INVALID; |
| 280 | throw AuthFailureException(reason.buf); |
| 281 | } |
| 282 | |
| 283 | void CConnection::processInitMsg() |
| 284 | { |
| 285 | vlog.debug("reading server initialisation"); |
| 286 | reader_->readServerInit(); |
| 287 | } |
| 288 | |
| 289 | void CConnection::throwConnFailedException() |
| 290 | { |
| 291 | state_ = RFBSTATE_INVALID; |
| 292 | CharArray reason; |
| 293 | reason.buf = is->readString(); |
| 294 | throw ConnFailedException(reason.buf); |
| 295 | } |
| 296 | |
| 297 | void CConnection::securityCompleted() |
| 298 | { |
| 299 | state_ = RFBSTATE_INITIALISATION; |
Pierre Ossman | 7638e9c | 2014-01-16 13:12:40 +0100 | [diff] [blame] | 300 | reader_ = new CMsgReader(this, is); |
| 301 | writer_ = new CMsgWriter(&cp, os); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 302 | vlog.debug("Authentication success!"); |
| 303 | authSuccess(); |
| 304 | writer_->writeClientInit(shared); |
| 305 | } |
| 306 | |
Pierre Ossman | 3da238d | 2015-11-12 12:20:05 +0100 | [diff] [blame] | 307 | void CConnection::setDesktopSize(int w, int h) |
| 308 | { |
Pierre Ossman | 504afa2 | 2015-11-12 12:21:58 +0100 | [diff] [blame] | 309 | decoder.flush(); |
| 310 | |
Pierre Ossman | 3da238d | 2015-11-12 12:20:05 +0100 | [diff] [blame] | 311 | CMsgHandler::setDesktopSize(w,h); |
| 312 | } |
| 313 | |
| 314 | void CConnection::setExtendedDesktopSize(unsigned reason, |
| 315 | unsigned result, |
| 316 | int w, int h, |
| 317 | const ScreenSet& layout) |
| 318 | { |
Pierre Ossman | 504afa2 | 2015-11-12 12:21:58 +0100 | [diff] [blame] | 319 | decoder.flush(); |
| 320 | |
Pierre Ossman | 3da238d | 2015-11-12 12:20:05 +0100 | [diff] [blame] | 321 | CMsgHandler::setExtendedDesktopSize(reason, result, w, h, layout); |
| 322 | } |
| 323 | |
Pierre Ossman | a4c0aac | 2017-02-19 15:50:29 +0100 | [diff] [blame] | 324 | void CConnection::readAndDecodeRect(const Rect& r, int encoding, |
| 325 | ModifiablePixelBuffer* pb) |
| 326 | { |
| 327 | decoder.decodeRect(r, encoding, pb); |
| 328 | decoder.flush(); |
| 329 | } |
| 330 | |
Pierre Ossman | 3da238d | 2015-11-12 12:20:05 +0100 | [diff] [blame] | 331 | void CConnection::framebufferUpdateStart() |
| 332 | { |
| 333 | CMsgHandler::framebufferUpdateStart(); |
| 334 | } |
| 335 | |
| 336 | void CConnection::framebufferUpdateEnd() |
| 337 | { |
Pierre Ossman | 504afa2 | 2015-11-12 12:21:58 +0100 | [diff] [blame] | 338 | decoder.flush(); |
| 339 | |
Pierre Ossman | 3da238d | 2015-11-12 12:20:05 +0100 | [diff] [blame] | 340 | CMsgHandler::framebufferUpdateEnd(); |
| 341 | } |
| 342 | |
Pierre Ossman | 9f273e9 | 2015-11-09 16:34:54 +0100 | [diff] [blame] | 343 | void CConnection::dataRect(const Rect& r, int encoding) |
| 344 | { |
| 345 | decoder.decodeRect(r, encoding, framebuffer); |
| 346 | } |
| 347 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 348 | void CConnection::authSuccess() |
| 349 | { |
| 350 | } |
| 351 | |
| 352 | void CConnection::serverInit() |
| 353 | { |
| 354 | state_ = RFBSTATE_NORMAL; |
| 355 | vlog.debug("initialisation done"); |
| 356 | } |
Pierre Ossman | c754cce | 2011-11-14 15:44:11 +0000 | [diff] [blame] | 357 | |
| 358 | void CConnection::fence(rdr::U32 flags, unsigned len, const char data[]) |
| 359 | { |
| 360 | CMsgHandler::fence(flags, len, data); |
| 361 | |
| 362 | if (!(flags & fenceFlagRequest)) |
| 363 | return; |
| 364 | |
| 365 | // We cannot guarantee any synchronisation at this level |
| 366 | flags = 0; |
| 367 | |
| 368 | writer()->writeFence(flags, len, data); |
| 369 | } |