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 | { |
| 67 | if ((framebuffer != NULL) && (fb != NULL)) { |
| 68 | Rect rect; |
| 69 | |
| 70 | const rdr::U8* data; |
| 71 | int stride; |
| 72 | |
| 73 | const rdr::U8 black[4] = { 0, 0, 0, 0 }; |
| 74 | |
| 75 | // Copy still valid area |
| 76 | |
| 77 | rect.setXYWH(0, 0, |
| 78 | __rfbmin(fb->width(), framebuffer->width()), |
| 79 | __rfbmin(fb->height(), framebuffer->height())); |
| 80 | data = framebuffer->getBuffer(framebuffer->getRect(), &stride); |
| 81 | fb->imageRect(rect, data, stride); |
| 82 | |
| 83 | // Black out any new areas |
| 84 | |
| 85 | if (fb->width() > framebuffer->width()) { |
| 86 | rect.setXYWH(framebuffer->width(), 0, |
| 87 | fb->width() - fb->width(), |
| 88 | fb->height()); |
| 89 | fb->fillRect(rect, black); |
| 90 | } |
| 91 | |
| 92 | if (fb->height() > framebuffer->height()) { |
| 93 | rect.setXYWH(0, framebuffer->height(), |
| 94 | fb->width(), |
| 95 | fb->height() - framebuffer->height()); |
| 96 | fb->fillRect(rect, black); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | delete framebuffer; |
| 101 | framebuffer = fb; |
| 102 | } |
| 103 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 104 | void CConnection::initialiseProtocol() |
| 105 | { |
| 106 | state_ = RFBSTATE_PROTOCOL_VERSION; |
| 107 | } |
| 108 | |
| 109 | void CConnection::processMsg() |
| 110 | { |
| 111 | switch (state_) { |
| 112 | |
| 113 | case RFBSTATE_PROTOCOL_VERSION: processVersionMsg(); break; |
| 114 | case RFBSTATE_SECURITY_TYPES: processSecurityTypesMsg(); break; |
| 115 | case RFBSTATE_SECURITY: processSecurityMsg(); break; |
| 116 | case RFBSTATE_SECURITY_RESULT: processSecurityResultMsg(); break; |
| 117 | case RFBSTATE_INITIALISATION: processInitMsg(); break; |
| 118 | case RFBSTATE_NORMAL: reader_->readMsg(); break; |
| 119 | case RFBSTATE_UNINITIALISED: |
| 120 | throw Exception("CConnection::processMsg: not initialised yet?"); |
| 121 | default: |
| 122 | throw Exception("CConnection::processMsg: invalid state"); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | void CConnection::processVersionMsg() |
| 127 | { |
| 128 | vlog.debug("reading protocol version"); |
| 129 | bool done; |
| 130 | if (!cp.readVersion(is, &done)) { |
| 131 | state_ = RFBSTATE_INVALID; |
| 132 | throw Exception("reading version failed: not an RFB server?"); |
| 133 | } |
| 134 | if (!done) return; |
| 135 | |
| 136 | vlog.info("Server supports RFB protocol version %d.%d", |
| 137 | cp.majorVersion, cp.minorVersion); |
| 138 | |
| 139 | // The only official RFB protocol versions are currently 3.3, 3.7 and 3.8 |
| 140 | if (cp.beforeVersion(3,3)) { |
Pierre Ossman | a7bbe9c | 2015-03-03 16:17:51 +0100 | [diff] [blame] | 141 | vlog.error("Server gave unsupported RFB protocol version %d.%d", |
| 142 | cp.majorVersion, cp.minorVersion); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 143 | state_ = RFBSTATE_INVALID; |
Pierre Ossman | a7bbe9c | 2015-03-03 16:17:51 +0100 | [diff] [blame] | 144 | throw Exception("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 | } else if (useProtocol3_3 || cp.beforeVersion(3,7)) { |
| 147 | cp.setVersion(3,3); |
| 148 | } else if (cp.afterVersion(3,8)) { |
| 149 | cp.setVersion(3,8); |
| 150 | } |
| 151 | |
| 152 | cp.writeVersion(os); |
| 153 | state_ = RFBSTATE_SECURITY_TYPES; |
| 154 | |
| 155 | vlog.info("Using RFB protocol version %d.%d", |
| 156 | cp.majorVersion, cp.minorVersion); |
| 157 | } |
| 158 | |
| 159 | |
| 160 | void CConnection::processSecurityTypesMsg() |
| 161 | { |
| 162 | vlog.debug("processing security types message"); |
| 163 | |
| 164 | int secType = secTypeInvalid; |
| 165 | |
Adam Tkac | 05a0cd6 | 2010-07-20 15:07:44 +0000 | [diff] [blame] | 166 | std::list<rdr::U8> secTypes; |
| 167 | secTypes = security->GetEnabledSecTypes(); |
| 168 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 169 | if (cp.isVersion(3,3)) { |
| 170 | |
| 171 | // legacy 3.3 server may only offer "vnc authentication" or "none" |
| 172 | |
| 173 | secType = is->readU32(); |
| 174 | if (secType == secTypeInvalid) { |
| 175 | throwConnFailedException(); |
| 176 | |
| 177 | } else if (secType == secTypeNone || secType == secTypeVncAuth) { |
Adam Tkac | 05a0cd6 | 2010-07-20 15:07:44 +0000 | [diff] [blame] | 178 | std::list<rdr::U8>::iterator i; |
| 179 | for (i = secTypes.begin(); i != secTypes.end(); i++) |
| 180 | if (*i == secType) { |
| 181 | secType = *i; |
| 182 | break; |
| 183 | } |
| 184 | |
| 185 | if (i == secTypes.end()) |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 186 | secType = secTypeInvalid; |
| 187 | } else { |
| 188 | vlog.error("Unknown 3.3 security type %d", secType); |
| 189 | throw Exception("Unknown 3.3 security type"); |
| 190 | } |
| 191 | |
| 192 | } else { |
| 193 | |
| 194 | // >=3.7 server will offer us a list |
| 195 | |
| 196 | int nServerSecTypes = is->readU8(); |
| 197 | if (nServerSecTypes == 0) |
| 198 | throwConnFailedException(); |
| 199 | |
Adam Tkac | 05a0cd6 | 2010-07-20 15:07:44 +0000 | [diff] [blame] | 200 | std::list<rdr::U8>::iterator j; |
Adam Tkac | 05a0cd6 | 2010-07-20 15:07:44 +0000 | [diff] [blame] | 201 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 202 | for (int i = 0; i < nServerSecTypes; i++) { |
| 203 | rdr::U8 serverSecType = is->readU8(); |
| 204 | vlog.debug("Server offers security type %s(%d)", |
Adam Tkac | 7cb47d6 | 2011-02-21 12:55:24 +0000 | [diff] [blame] | 205 | secTypeName(serverSecType), serverSecType); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 206 | |
Adam Tkac | 7cb47d6 | 2011-02-21 12:55:24 +0000 | [diff] [blame] | 207 | /* |
| 208 | * Use the first type sent by server which matches client's type. |
| 209 | * It means server's order specifies priority. |
| 210 | */ |
| 211 | if (secType == secTypeInvalid) { |
| 212 | for (j = secTypes.begin(); j != secTypes.end(); j++) |
| 213 | if (*j == serverSecType) { |
| 214 | secType = *j; |
| 215 | break; |
| 216 | } |
| 217 | } |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | // Inform the server of our decision |
| 221 | if (secType != secTypeInvalid) { |
| 222 | os->writeU8(secType); |
| 223 | os->flush(); |
Pierre Ossman | 71d6666 | 2014-11-11 13:42:51 +0100 | [diff] [blame] | 224 | vlog.info("Choosing security type %s(%d)",secTypeName(secType),secType); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 225 | } |
| 226 | } |
| 227 | |
| 228 | if (secType == secTypeInvalid) { |
| 229 | state_ = RFBSTATE_INVALID; |
| 230 | vlog.error("No matching security types"); |
| 231 | throw Exception("No matching security types"); |
| 232 | } |
| 233 | |
| 234 | state_ = RFBSTATE_SECURITY; |
Adam Tkac | f324dc4 | 2010-04-23 14:10:17 +0000 | [diff] [blame] | 235 | csecurity = security->GetCSecurity(secType); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 236 | processSecurityMsg(); |
| 237 | } |
| 238 | |
| 239 | void CConnection::processSecurityMsg() |
| 240 | { |
| 241 | vlog.debug("processing security message"); |
Adam Tkac | f324dc4 | 2010-04-23 14:10:17 +0000 | [diff] [blame] | 242 | if (csecurity->processMsg(this)) { |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 243 | state_ = RFBSTATE_SECURITY_RESULT; |
| 244 | processSecurityResultMsg(); |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | void CConnection::processSecurityResultMsg() |
| 249 | { |
| 250 | vlog.debug("processing security result message"); |
| 251 | int result; |
Adam Tkac | f324dc4 | 2010-04-23 14:10:17 +0000 | [diff] [blame] | 252 | if (cp.beforeVersion(3,8) && csecurity->getType() == secTypeNone) { |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 253 | result = secResultOK; |
| 254 | } else { |
| 255 | if (!is->checkNoWait(1)) return; |
| 256 | result = is->readU32(); |
| 257 | } |
| 258 | switch (result) { |
| 259 | case secResultOK: |
| 260 | securityCompleted(); |
| 261 | return; |
| 262 | case secResultFailed: |
| 263 | vlog.debug("auth failed"); |
| 264 | break; |
| 265 | case secResultTooMany: |
| 266 | vlog.debug("auth failed - too many tries"); |
| 267 | break; |
| 268 | default: |
| 269 | throw Exception("Unknown security result from server"); |
| 270 | } |
| 271 | CharArray reason; |
| 272 | if (cp.beforeVersion(3,8)) |
Adam Tkac | d36b626 | 2009-09-04 10:57:20 +0000 | [diff] [blame] | 273 | reason.buf = strDup("Authentication failure"); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 274 | else |
| 275 | reason.buf = is->readString(); |
| 276 | state_ = RFBSTATE_INVALID; |
| 277 | throw AuthFailureException(reason.buf); |
| 278 | } |
| 279 | |
| 280 | void CConnection::processInitMsg() |
| 281 | { |
| 282 | vlog.debug("reading server initialisation"); |
| 283 | reader_->readServerInit(); |
| 284 | } |
| 285 | |
| 286 | void CConnection::throwConnFailedException() |
| 287 | { |
| 288 | state_ = RFBSTATE_INVALID; |
| 289 | CharArray reason; |
| 290 | reason.buf = is->readString(); |
| 291 | throw ConnFailedException(reason.buf); |
| 292 | } |
| 293 | |
| 294 | void CConnection::securityCompleted() |
| 295 | { |
| 296 | state_ = RFBSTATE_INITIALISATION; |
Pierre Ossman | 7638e9c | 2014-01-16 13:12:40 +0100 | [diff] [blame] | 297 | reader_ = new CMsgReader(this, is); |
| 298 | writer_ = new CMsgWriter(&cp, os); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 299 | vlog.debug("Authentication success!"); |
| 300 | authSuccess(); |
| 301 | writer_->writeClientInit(shared); |
| 302 | } |
| 303 | |
Pierre Ossman | 3da238d | 2015-11-12 12:20:05 +0100 | [diff] [blame^] | 304 | void CConnection::setDesktopSize(int w, int h) |
| 305 | { |
| 306 | CMsgHandler::setDesktopSize(w,h); |
| 307 | } |
| 308 | |
| 309 | void CConnection::setExtendedDesktopSize(unsigned reason, |
| 310 | unsigned result, |
| 311 | int w, int h, |
| 312 | const ScreenSet& layout) |
| 313 | { |
| 314 | CMsgHandler::setExtendedDesktopSize(reason, result, w, h, layout); |
| 315 | } |
| 316 | |
| 317 | void CConnection::framebufferUpdateStart() |
| 318 | { |
| 319 | CMsgHandler::framebufferUpdateStart(); |
| 320 | } |
| 321 | |
| 322 | void CConnection::framebufferUpdateEnd() |
| 323 | { |
| 324 | CMsgHandler::framebufferUpdateEnd(); |
| 325 | } |
| 326 | |
Pierre Ossman | 9f273e9 | 2015-11-09 16:34:54 +0100 | [diff] [blame] | 327 | void CConnection::dataRect(const Rect& r, int encoding) |
| 328 | { |
| 329 | decoder.decodeRect(r, encoding, framebuffer); |
| 330 | } |
| 331 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 332 | void CConnection::authSuccess() |
| 333 | { |
| 334 | } |
| 335 | |
| 336 | void CConnection::serverInit() |
| 337 | { |
| 338 | state_ = RFBSTATE_NORMAL; |
| 339 | vlog.debug("initialisation done"); |
| 340 | } |
Pierre Ossman | c754cce | 2011-11-14 15:44:11 +0000 | [diff] [blame] | 341 | |
| 342 | void CConnection::fence(rdr::U32 flags, unsigned len, const char data[]) |
| 343 | { |
| 344 | CMsgHandler::fence(flags, len, data); |
| 345 | |
| 346 | if (!(flags & fenceFlagRequest)) |
| 347 | return; |
| 348 | |
| 349 | // We cannot guarantee any synchronisation at this level |
| 350 | flags = 0; |
| 351 | |
| 352 | writer()->writeFence(flags, len, data); |
| 353 | } |