blob: 7146abc928e647ade3e4812d0a45589d1b599eb8 [file] [log] [blame]
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
Pierre Ossmana4c0aac2017-02-19 15:50:29 +01002 * Copyright 2011-2017 Pierre Ossman for Cendio AB
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00003 *
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 Ossman0068a4f2015-11-09 15:48:19 +010021
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000022#include <rfb/Exception.h>
Pierre Ossmanc754cce2011-11-14 15:44:11 +000023#include <rfb/fenceTypes.h>
Pierre Ossman7638e9c2014-01-16 13:12:40 +010024#include <rfb/CMsgReader.h>
25#include <rfb/CMsgWriter.h>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000026#include <rfb/CSecurity.h>
Adam Tkac5a0caed2010-04-23 13:58:10 +000027#include <rfb/Security.h>
Pierre Ossman0068a4f2015-11-09 15:48:19 +010028#include <rfb/SecurityClient.h>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000029#include <rfb/CConnection.h>
30#include <rfb/util.h>
31
32#include <rfb/LogWriter.h>
33
Pierre Ossman0068a4f2015-11-09 15:48:19 +010034#include <rdr/InStream.h>
35#include <rdr/OutStream.h>
36
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000037using namespace rfb;
38
39static LogWriter vlog("CConnection");
40
41CConnection::CConnection()
Adam Tkacf324dc42010-04-23 14:10:17 +000042 : csecurity(0), is(0), os(0), reader_(0), writer_(0),
Adam Tkac05a0cd62010-07-20 15:07:44 +000043 shared(false),
Pierre Ossman9f273e92015-11-09 16:34:54 +010044 state_(RFBSTATE_UNINITIALISED), useProtocol3_3(false),
45 framebuffer(NULL), decoder(this)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000046{
47}
48
49CConnection::~CConnection()
50{
Pierre Ossman9f273e92015-11-09 16:34:54 +010051 setFramebuffer(NULL);
Pierre Ossman82d22e62018-09-21 15:26:37 +020052 if (csecurity)
53 delete csecurity;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000054 delete reader_;
55 reader_ = 0;
56 delete writer_;
57 writer_ = 0;
58}
59
60void CConnection::setStreams(rdr::InStream* is_, rdr::OutStream* os_)
61{
62 is = is_;
63 os = os_;
64}
65
Pierre Ossman9f273e92015-11-09 16:34:54 +010066void CConnection::setFramebuffer(ModifiablePixelBuffer* fb)
67{
Pierre Ossman504afa22015-11-12 12:21:58 +010068 decoder.flush();
69
Pierre Ossman9f273e92015-11-09 16:34:54 +010070 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. Hinz5d663052016-09-05 09:15:50 -040090 fb->width() - framebuffer->width(),
Pierre Ossman9f273e92015-11-09 16:34:54 +010091 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 Kaplinskya2adc8d2006-05-25 05:01:55 +0000107void CConnection::initialiseProtocol()
108{
109 state_ = RFBSTATE_PROTOCOL_VERSION;
110}
111
112void 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
129void CConnection::processVersionMsg()
130{
Pierre Ossmanea7ede92018-06-18 16:51:53 +0200131 char verStr[13];
132 int majorVersion;
133 int minorVersion;
134
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000135 vlog.debug("reading protocol version");
Pierre Ossmanea7ede92018-06-18 16:51:53 +0200136
137 if (!is->checkNoWait(12))
138 return;
139
140 is->readBytes(verStr, 12);
141 verStr[12] = '\0';
142
143 if (sscanf(verStr, "RFB %03d.%03d\n",
144 &majorVersion, &minorVersion) != 2) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000145 state_ = RFBSTATE_INVALID;
146 throw Exception("reading version failed: not an RFB server?");
147 }
Pierre Ossmanea7ede92018-06-18 16:51:53 +0200148
149 cp.setVersion(majorVersion, minorVersion);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000150
151 vlog.info("Server supports RFB protocol version %d.%d",
152 cp.majorVersion, cp.minorVersion);
153
154 // The only official RFB protocol versions are currently 3.3, 3.7 and 3.8
155 if (cp.beforeVersion(3,3)) {
Pierre Ossmana7bbe9c2015-03-03 16:17:51 +0100156 vlog.error("Server gave unsupported RFB protocol version %d.%d",
157 cp.majorVersion, cp.minorVersion);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000158 state_ = RFBSTATE_INVALID;
Pierre Ossmana7bbe9c2015-03-03 16:17:51 +0100159 throw Exception("Server gave unsupported RFB protocol version %d.%d",
160 cp.majorVersion, cp.minorVersion);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000161 } else if (useProtocol3_3 || cp.beforeVersion(3,7)) {
162 cp.setVersion(3,3);
163 } else if (cp.afterVersion(3,8)) {
164 cp.setVersion(3,8);
165 }
166
Pierre Ossmanea7ede92018-06-18 16:51:53 +0200167 sprintf(verStr, "RFB %03d.%03d\n", cp.majorVersion, cp.minorVersion);
168 os->writeBytes(verStr, 12);
169 os->flush();
170
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000171 state_ = RFBSTATE_SECURITY_TYPES;
172
173 vlog.info("Using RFB protocol version %d.%d",
174 cp.majorVersion, cp.minorVersion);
175}
176
177
178void CConnection::processSecurityTypesMsg()
179{
180 vlog.debug("processing security types message");
181
182 int secType = secTypeInvalid;
183
Adam Tkac05a0cd62010-07-20 15:07:44 +0000184 std::list<rdr::U8> secTypes;
Michal Srbdccb5f72017-03-27 13:55:46 +0300185 secTypes = security.GetEnabledSecTypes();
Adam Tkac05a0cd62010-07-20 15:07:44 +0000186
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000187 if (cp.isVersion(3,3)) {
188
189 // legacy 3.3 server may only offer "vnc authentication" or "none"
190
191 secType = is->readU32();
192 if (secType == secTypeInvalid) {
193 throwConnFailedException();
194
195 } else if (secType == secTypeNone || secType == secTypeVncAuth) {
Adam Tkac05a0cd62010-07-20 15:07:44 +0000196 std::list<rdr::U8>::iterator i;
197 for (i = secTypes.begin(); i != secTypes.end(); i++)
198 if (*i == secType) {
199 secType = *i;
200 break;
201 }
202
203 if (i == secTypes.end())
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000204 secType = secTypeInvalid;
205 } else {
206 vlog.error("Unknown 3.3 security type %d", secType);
207 throw Exception("Unknown 3.3 security type");
208 }
209
210 } else {
211
212 // >=3.7 server will offer us a list
213
214 int nServerSecTypes = is->readU8();
215 if (nServerSecTypes == 0)
216 throwConnFailedException();
217
Adam Tkac05a0cd62010-07-20 15:07:44 +0000218 std::list<rdr::U8>::iterator j;
Adam Tkac05a0cd62010-07-20 15:07:44 +0000219
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000220 for (int i = 0; i < nServerSecTypes; i++) {
221 rdr::U8 serverSecType = is->readU8();
222 vlog.debug("Server offers security type %s(%d)",
Adam Tkac7cb47d62011-02-21 12:55:24 +0000223 secTypeName(serverSecType), serverSecType);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000224
Adam Tkac7cb47d62011-02-21 12:55:24 +0000225 /*
226 * Use the first type sent by server which matches client's type.
227 * It means server's order specifies priority.
228 */
229 if (secType == secTypeInvalid) {
230 for (j = secTypes.begin(); j != secTypes.end(); j++)
231 if (*j == serverSecType) {
232 secType = *j;
233 break;
234 }
235 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000236 }
237
238 // Inform the server of our decision
239 if (secType != secTypeInvalid) {
240 os->writeU8(secType);
241 os->flush();
Pierre Ossman71d66662014-11-11 13:42:51 +0100242 vlog.info("Choosing security type %s(%d)",secTypeName(secType),secType);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000243 }
244 }
245
246 if (secType == secTypeInvalid) {
247 state_ = RFBSTATE_INVALID;
248 vlog.error("No matching security types");
249 throw Exception("No matching security types");
250 }
251
252 state_ = RFBSTATE_SECURITY;
Pierre Ossmanad2b3c42018-09-21 15:31:11 +0200253 csecurity = security.GetCSecurity(this, secType);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000254 processSecurityMsg();
255}
256
257void CConnection::processSecurityMsg()
258{
259 vlog.debug("processing security message");
Pierre Ossmanad2b3c42018-09-21 15:31:11 +0200260 if (csecurity->processMsg()) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000261 state_ = RFBSTATE_SECURITY_RESULT;
262 processSecurityResultMsg();
263 }
264}
265
266void CConnection::processSecurityResultMsg()
267{
268 vlog.debug("processing security result message");
269 int result;
Adam Tkacf324dc42010-04-23 14:10:17 +0000270 if (cp.beforeVersion(3,8) && csecurity->getType() == secTypeNone) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000271 result = secResultOK;
272 } else {
273 if (!is->checkNoWait(1)) return;
274 result = is->readU32();
275 }
276 switch (result) {
277 case secResultOK:
278 securityCompleted();
279 return;
280 case secResultFailed:
281 vlog.debug("auth failed");
282 break;
283 case secResultTooMany:
284 vlog.debug("auth failed - too many tries");
285 break;
286 default:
287 throw Exception("Unknown security result from server");
288 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000289 state_ = RFBSTATE_INVALID;
Pierre Ossman19225502017-10-12 15:05:07 +0200290 if (cp.beforeVersion(3,8))
291 throw AuthFailureException();
292 CharArray reason(is->readString());
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000293 throw AuthFailureException(reason.buf);
294}
295
296void CConnection::processInitMsg()
297{
298 vlog.debug("reading server initialisation");
299 reader_->readServerInit();
300}
301
302void CConnection::throwConnFailedException()
303{
304 state_ = RFBSTATE_INVALID;
305 CharArray reason;
306 reason.buf = is->readString();
307 throw ConnFailedException(reason.buf);
308}
309
310void CConnection::securityCompleted()
311{
312 state_ = RFBSTATE_INITIALISATION;
Pierre Ossman7638e9c2014-01-16 13:12:40 +0100313 reader_ = new CMsgReader(this, is);
314 writer_ = new CMsgWriter(&cp, os);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000315 vlog.debug("Authentication success!");
316 authSuccess();
317 writer_->writeClientInit(shared);
318}
319
Pierre Ossman3da238d2015-11-12 12:20:05 +0100320void CConnection::setDesktopSize(int w, int h)
321{
Pierre Ossman504afa22015-11-12 12:21:58 +0100322 decoder.flush();
323
Pierre Ossman3da238d2015-11-12 12:20:05 +0100324 CMsgHandler::setDesktopSize(w,h);
325}
326
327void CConnection::setExtendedDesktopSize(unsigned reason,
328 unsigned result,
329 int w, int h,
330 const ScreenSet& layout)
331{
Pierre Ossman504afa22015-11-12 12:21:58 +0100332 decoder.flush();
333
Pierre Ossman3da238d2015-11-12 12:20:05 +0100334 CMsgHandler::setExtendedDesktopSize(reason, result, w, h, layout);
335}
336
Pierre Ossmana4c0aac2017-02-19 15:50:29 +0100337void CConnection::readAndDecodeRect(const Rect& r, int encoding,
338 ModifiablePixelBuffer* pb)
339{
340 decoder.decodeRect(r, encoding, pb);
341 decoder.flush();
342}
343
Pierre Ossman3da238d2015-11-12 12:20:05 +0100344void CConnection::framebufferUpdateStart()
345{
346 CMsgHandler::framebufferUpdateStart();
347}
348
349void CConnection::framebufferUpdateEnd()
350{
Pierre Ossman504afa22015-11-12 12:21:58 +0100351 decoder.flush();
352
Pierre Ossman3da238d2015-11-12 12:20:05 +0100353 CMsgHandler::framebufferUpdateEnd();
354}
355
Pierre Ossman9f273e92015-11-09 16:34:54 +0100356void CConnection::dataRect(const Rect& r, int encoding)
357{
358 decoder.decodeRect(r, encoding, framebuffer);
359}
360
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000361void CConnection::authSuccess()
362{
363}
364
365void CConnection::serverInit()
366{
367 state_ = RFBSTATE_NORMAL;
368 vlog.debug("initialisation done");
369}
Pierre Ossmanc754cce2011-11-14 15:44:11 +0000370
371void CConnection::fence(rdr::U32 flags, unsigned len, const char data[])
372{
373 CMsgHandler::fence(flags, len, data);
374
375 if (!(flags & fenceFlagRequest))
376 return;
377
378 // We cannot guarantee any synchronisation at this level
379 flags = 0;
380
381 writer()->writeFence(flags, len, data);
382}