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