blob: d7a9d852fa48e0377b904ad2cd75adfae9e7384c [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{
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 Ossmana7bbe9c2015-03-03 16:17:51 +0100144 vlog.error("Server gave unsupported RFB protocol version %d.%d",
145 cp.majorVersion, cp.minorVersion);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000146 state_ = RFBSTATE_INVALID;
Pierre Ossmana7bbe9c2015-03-03 16:17:51 +0100147 throw Exception("Server gave unsupported RFB protocol version %d.%d",
148 cp.majorVersion, cp.minorVersion);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000149 } 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
163void CConnection::processSecurityTypesMsg()
164{
165 vlog.debug("processing security types message");
166
167 int secType = secTypeInvalid;
168
Adam Tkac05a0cd62010-07-20 15:07:44 +0000169 std::list<rdr::U8> secTypes;
Michal Srbdccb5f72017-03-27 13:55:46 +0300170 secTypes = security.GetEnabledSecTypes();
Adam Tkac05a0cd62010-07-20 15:07:44 +0000171
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000172 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 Tkac05a0cd62010-07-20 15:07:44 +0000181 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 Kaplinskya2adc8d2006-05-25 05:01:55 +0000189 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 Tkac05a0cd62010-07-20 15:07:44 +0000203 std::list<rdr::U8>::iterator j;
Adam Tkac05a0cd62010-07-20 15:07:44 +0000204
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000205 for (int i = 0; i < nServerSecTypes; i++) {
206 rdr::U8 serverSecType = is->readU8();
207 vlog.debug("Server offers security type %s(%d)",
Adam Tkac7cb47d62011-02-21 12:55:24 +0000208 secTypeName(serverSecType), serverSecType);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000209
Adam Tkac7cb47d62011-02-21 12:55:24 +0000210 /*
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 Kaplinskya2adc8d2006-05-25 05:01:55 +0000221 }
222
223 // Inform the server of our decision
224 if (secType != secTypeInvalid) {
225 os->writeU8(secType);
226 os->flush();
Pierre Ossman71d66662014-11-11 13:42:51 +0100227 vlog.info("Choosing security type %s(%d)",secTypeName(secType),secType);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000228 }
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;
Michal Srbdccb5f72017-03-27 13:55:46 +0300238 csecurity = security.GetCSecurity(secType);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000239 processSecurityMsg();
240}
241
242void CConnection::processSecurityMsg()
243{
244 vlog.debug("processing security message");
Adam Tkacf324dc42010-04-23 14:10:17 +0000245 if (csecurity->processMsg(this)) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000246 state_ = RFBSTATE_SECURITY_RESULT;
247 processSecurityResultMsg();
248 }
249}
250
251void CConnection::processSecurityResultMsg()
252{
253 vlog.debug("processing security result message");
254 int result;
Adam Tkacf324dc42010-04-23 14:10:17 +0000255 if (cp.beforeVersion(3,8) && csecurity->getType() == secTypeNone) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000256 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 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000274 state_ = RFBSTATE_INVALID;
Pierre Ossman19225502017-10-12 15:05:07 +0200275 if (cp.beforeVersion(3,8))
276 throw AuthFailureException();
277 CharArray reason(is->readString());
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000278 throw AuthFailureException(reason.buf);
279}
280
281void CConnection::processInitMsg()
282{
283 vlog.debug("reading server initialisation");
284 reader_->readServerInit();
285}
286
287void CConnection::throwConnFailedException()
288{
289 state_ = RFBSTATE_INVALID;
290 CharArray reason;
291 reason.buf = is->readString();
292 throw ConnFailedException(reason.buf);
293}
294
295void CConnection::securityCompleted()
296{
297 state_ = RFBSTATE_INITIALISATION;
Pierre Ossman7638e9c2014-01-16 13:12:40 +0100298 reader_ = new CMsgReader(this, is);
299 writer_ = new CMsgWriter(&cp, os);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000300 vlog.debug("Authentication success!");
301 authSuccess();
302 writer_->writeClientInit(shared);
303}
304
Pierre Ossman3da238d2015-11-12 12:20:05 +0100305void CConnection::setDesktopSize(int w, int h)
306{
Pierre Ossman504afa22015-11-12 12:21:58 +0100307 decoder.flush();
308
Pierre Ossman3da238d2015-11-12 12:20:05 +0100309 CMsgHandler::setDesktopSize(w,h);
310}
311
312void CConnection::setExtendedDesktopSize(unsigned reason,
313 unsigned result,
314 int w, int h,
315 const ScreenSet& layout)
316{
Pierre Ossman504afa22015-11-12 12:21:58 +0100317 decoder.flush();
318
Pierre Ossman3da238d2015-11-12 12:20:05 +0100319 CMsgHandler::setExtendedDesktopSize(reason, result, w, h, layout);
320}
321
Pierre Ossmana4c0aac2017-02-19 15:50:29 +0100322void CConnection::readAndDecodeRect(const Rect& r, int encoding,
323 ModifiablePixelBuffer* pb)
324{
325 decoder.decodeRect(r, encoding, pb);
326 decoder.flush();
327}
328
Pierre Ossman3da238d2015-11-12 12:20:05 +0100329void CConnection::framebufferUpdateStart()
330{
331 CMsgHandler::framebufferUpdateStart();
332}
333
334void CConnection::framebufferUpdateEnd()
335{
Pierre Ossman504afa22015-11-12 12:21:58 +0100336 decoder.flush();
337
Pierre Ossman3da238d2015-11-12 12:20:05 +0100338 CMsgHandler::framebufferUpdateEnd();
339}
340
Pierre Ossman9f273e92015-11-09 16:34:54 +0100341void CConnection::dataRect(const Rect& r, int encoding)
342{
343 decoder.decodeRect(r, encoding, framebuffer);
344}
345
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000346void CConnection::authSuccess()
347{
348}
349
350void CConnection::serverInit()
351{
352 state_ = RFBSTATE_NORMAL;
353 vlog.debug("initialisation done");
354}
Pierre Ossmanc754cce2011-11-14 15:44:11 +0000355
356void CConnection::fence(rdr::U32 flags, unsigned len, const char data[])
357{
358 CMsgHandler::fence(flags, len, data);
359
360 if (!(flags & fenceFlagRequest))
361 return;
362
363 // We cannot guarantee any synchronisation at this level
364 flags = 0;
365
366 writer()->writeFence(flags, len, data);
367}