vncserver.cc (8871:136328e218f2) vncserver.cc (9330:4a3269a11230)
1/*
2 * Copyright (c) 2010 ARM Limited
3 * All rights reserved
4 *
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder. You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions are
16 * met: redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer;
18 * redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution;
21 * neither the name of the copyright holders nor the names of its
22 * contributors may be used to endorse or promote products derived from
23 * this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 *
37 * Authors: Ali Saidi
38 * William Wang
39 */
40
41/** @file
42 * Implementiation of a VNC server
43 */
44
45#include <sys/ioctl.h>
46#include <sys/stat.h>
47#include <sys/termios.h>
48#include <sys/types.h>
49#include <fcntl.h>
50#include <poll.h>
51#include <unistd.h>
52
53#include <cerrno>
54#include <cstdio>
55
56#include "base/vnc/vncserver.hh"
57#include "base/atomicio.hh"
58#include "base/bitmap.hh"
59#include "base/misc.hh"
60#include "base/output.hh"
61#include "base/socket.hh"
62#include "base/trace.hh"
63#include "debug/VNC.hh"
64#include "sim/byteswap.hh"
65#include "sim/core.hh"
66
67using namespace std;
68
1/*
2 * Copyright (c) 2010 ARM Limited
3 * All rights reserved
4 *
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder. You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions are
16 * met: redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer;
18 * redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution;
21 * neither the name of the copyright holders nor the names of its
22 * contributors may be used to endorse or promote products derived from
23 * this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 *
37 * Authors: Ali Saidi
38 * William Wang
39 */
40
41/** @file
42 * Implementiation of a VNC server
43 */
44
45#include <sys/ioctl.h>
46#include <sys/stat.h>
47#include <sys/termios.h>
48#include <sys/types.h>
49#include <fcntl.h>
50#include <poll.h>
51#include <unistd.h>
52
53#include <cerrno>
54#include <cstdio>
55
56#include "base/vnc/vncserver.hh"
57#include "base/atomicio.hh"
58#include "base/bitmap.hh"
59#include "base/misc.hh"
60#include "base/output.hh"
61#include "base/socket.hh"
62#include "base/trace.hh"
63#include "debug/VNC.hh"
64#include "sim/byteswap.hh"
65#include "sim/core.hh"
66
67using namespace std;
68
69/** @file
70 * Implementiation of a VNC server
71 */
72
69/**
70 * Poll event for the listen socket
71 */
72VncServer::ListenEvent::ListenEvent(VncServer *vs, int fd, int e)
73 : PollEvent(fd, e), vncserver(vs)
74{
75}
76
77void
78VncServer::ListenEvent::process(int revent)
79{
80 vncserver->accept();
81}
82
83/**
84 * Poll event for the data socket
85 */
86VncServer::DataEvent::DataEvent(VncServer *vs, int fd, int e)
87 : PollEvent(fd, e), vncserver(vs)
88{
89}
90
91void
92VncServer::DataEvent::process(int revent)
93{
94 if (revent & POLLIN)
95 vncserver->data();
96 else if (revent & POLLNVAL)
97 vncserver->detach();
98}
99
100/**
101 * VncServer
102 */
103VncServer::VncServer(const Params *p)
73/**
74 * Poll event for the listen socket
75 */
76VncServer::ListenEvent::ListenEvent(VncServer *vs, int fd, int e)
77 : PollEvent(fd, e), vncserver(vs)
78{
79}
80
81void
82VncServer::ListenEvent::process(int revent)
83{
84 vncserver->accept();
85}
86
87/**
88 * Poll event for the data socket
89 */
90VncServer::DataEvent::DataEvent(VncServer *vs, int fd, int e)
91 : PollEvent(fd, e), vncserver(vs)
92{
93}
94
95void
96VncServer::DataEvent::process(int revent)
97{
98 if (revent & POLLIN)
99 vncserver->data();
100 else if (revent & POLLNVAL)
101 vncserver->detach();
102}
103
104/**
105 * VncServer
106 */
107VncServer::VncServer(const Params *p)
104 : SimObject(p), listenEvent(NULL), dataEvent(NULL), number(p->number),
105 dataFd(-1), _videoWidth(1), _videoHeight(1), clientRfb(0), keyboard(NULL),
106 mouse(NULL), sendUpdate(false), videoMode(VideoConvert::UnknownMode),
107 vc(NULL), captureEnabled(p->frame_capture), captureCurrentFrame(0),
108 captureLastHash(0), captureBitmap(0)
108 : VncInput(p), listenEvent(NULL), dataEvent(NULL), number(p->number),
109 dataFd(-1), sendUpdate(false)
109{
110 if (p->port)
111 listen(p->port);
112
113 curState = WaitForProtocolVersion;
114
115 // currently we only support this one pixel format
116 // unpacked 32bit rgb (rgb888 + 8 bits of nothing/alpha)
117 // keep it around for telling the client and making
118 // sure the client cooperates
119 pixelFormat.bpp = 32;
120 pixelFormat.depth = 24;
121 pixelFormat.bigendian = 0;
122 pixelFormat.truecolor = 1;
123 pixelFormat.redmax = 0xff;
124 pixelFormat.greenmax = 0xff;
125 pixelFormat.bluemax = 0xff;
126 pixelFormat.redshift = 16;
127 pixelFormat.greenshift = 8;
128 pixelFormat.blueshift = 0;
129
110{
111 if (p->port)
112 listen(p->port);
113
114 curState = WaitForProtocolVersion;
115
116 // currently we only support this one pixel format
117 // unpacked 32bit rgb (rgb888 + 8 bits of nothing/alpha)
118 // keep it around for telling the client and making
119 // sure the client cooperates
120 pixelFormat.bpp = 32;
121 pixelFormat.depth = 24;
122 pixelFormat.bigendian = 0;
123 pixelFormat.truecolor = 1;
124 pixelFormat.redmax = 0xff;
125 pixelFormat.greenmax = 0xff;
126 pixelFormat.bluemax = 0xff;
127 pixelFormat.redshift = 16;
128 pixelFormat.greenshift = 8;
129 pixelFormat.blueshift = 0;
130
130 if (captureEnabled) {
131 // remove existing frame output directory if it exists, then create a
132 // clean empty directory
133 const string FRAME_OUTPUT_SUBDIR = "frames_" + name();
134 simout.remove(FRAME_OUTPUT_SUBDIR, true);
135 captureOutputDirectory = simout.createSubdirectory(
136 FRAME_OUTPUT_SUBDIR);
137 }
138
139 DPRINTF(VNC, "Vnc server created at port %d\n", p->port);
140}
141
142VncServer::~VncServer()
143{
144 if (dataFd != -1)
145 ::close(dataFd);
146
147 if (listenEvent)
148 delete listenEvent;
149
150 if (dataEvent)
151 delete dataEvent;
152}
153
154
155//socket creation and vnc client attach
156void
157VncServer::listen(int port)
158{
159 if (ListenSocket::allDisabled()) {
160 warn_once("Sockets disabled, not accepting vnc client connections");
161 return;
162 }
163
164 while (!listener.listen(port, true)) {
165 DPRINTF(VNC,
166 "can't bind address vnc server port %d in use PID %d\n",
167 port, getpid());
168 port++;
169 }
170
171 int p1, p2;
172 p2 = name().rfind('.') - 1;
173 p1 = name().rfind('.', p2);
174 ccprintf(cerr, "Listening for %s connection on port %d\n",
175 name().substr(p1 + 1, p2 - p1), port);
176
177 listenEvent = new ListenEvent(this, listener.getfd(), POLLIN);
178 pollQueue.schedule(listenEvent);
179}
180
181// attach a vnc client
182void
183VncServer::accept()
184{
185 if (!listener.islistening())
186 panic("%s: cannot accept a connection if not listening!", name());
187
188 int fd = listener.accept(true);
189 if (dataFd != -1) {
190 char message[] = "vnc server already attached!\n";
191 atomic_write(fd, message, sizeof(message));
192 ::close(fd);
193 return;
194 }
195
196 dataFd = fd;
197
198 // Send our version number to the client
199 write((uint8_t*)vncVersion(), strlen(vncVersion()));
200
201 // read the client response
202 dataEvent = new DataEvent(this, dataFd, POLLIN);
203 pollQueue.schedule(dataEvent);
204
205 inform("VNC client attached\n");
206}
207
208// data called by data event
209void
210VncServer::data()
211{
212 // We have new data, see if we can handle it
213 size_t len;
214 DPRINTF(VNC, "Vnc client message recieved\n");
215
216 switch (curState) {
217 case WaitForProtocolVersion:
218 checkProtocolVersion();
219 break;
220 case WaitForSecurityResponse:
221 checkSecurity();
222 break;
223 case WaitForClientInit:
224 // Don't care about shared, just need to read it out of the socket
225 uint8_t shared;
226 len = read(&shared);
227 assert(len == 1);
228
229 // Send our idea of the frame buffer
230 sendServerInit();
231
232 break;
233 case NormalPhase:
234 uint8_t message_type;
235 len = read(&message_type);
236 if (!len) {
237 detach();
238 return;
239 }
240 assert(len == 1);
241
242 switch (message_type) {
243 case ClientSetPixelFormat:
244 setPixelFormat();
245 break;
246 case ClientSetEncodings:
247 setEncodings();
248 break;
249 case ClientFrameBufferUpdate:
250 requestFbUpdate();
251 break;
252 case ClientKeyEvent:
253 recvKeyboardInput();
254 break;
255 case ClientPointerEvent:
256 recvPointerInput();
257 break;
258 case ClientCutText:
259 recvCutText();
260 break;
261 default:
262 panic("Unimplemented message type recv from client: %d\n",
263 message_type);
264 break;
265 }
266 break;
267 default:
268 panic("Unknown vnc server state\n");
269 }
270}
271
272
273// read from socket
274size_t
275VncServer::read(uint8_t *buf, size_t len)
276{
277 if (dataFd < 0)
278 panic("vnc not properly attached.\n");
279
280 size_t ret;
281 do {
282 ret = ::read(dataFd, buf, len);
283 } while (ret == -1 && errno == EINTR);
284
285
286 if (ret <= 0){
287 DPRINTF(VNC, "Read failed.\n");
288 detach();
289 return 0;
290 }
291
292 return ret;
293}
294
295size_t
296VncServer::read1(uint8_t *buf, size_t len)
297{
298 size_t read_len M5_VAR_USED;
299 read_len = read(buf + 1, len - 1);
300 assert(read_len == len - 1);
301 return read_len;
302}
303
304
305template<typename T>
306size_t
307VncServer::read(T* val)
308{
309 return read((uint8_t*)val, sizeof(T));
310}
311
312// write to socket
313size_t
314VncServer::write(const uint8_t *buf, size_t len)
315{
316 if (dataFd < 0)
317 panic("Vnc client not properly attached.\n");
318
319 ssize_t ret;
320 ret = atomic_write(dataFd, buf, len);
321
322 if (ret < len)
323 detach();
324
325 return ret;
326}
327
328template<typename T>
329size_t
330VncServer::write(T* val)
331{
332 return write((uint8_t*)val, sizeof(T));
333}
334
335size_t
336VncServer::write(const char* str)
337{
338 return write((uint8_t*)str, strlen(str));
339}
340
341// detach a vnc client
342void
343VncServer::detach()
344{
345 if (dataFd != -1) {
346 ::close(dataFd);
347 dataFd = -1;
348 }
349
350 if (!dataEvent || !dataEvent->queued())
351 return;
352
353 pollQueue.remove(dataEvent);
354 delete dataEvent;
355 dataEvent = NULL;
356 curState = WaitForProtocolVersion;
357
358 inform("VNC client detached\n");
359 DPRINTF(VNC, "detach vnc client %d\n", number);
360}
361
362void
363VncServer::sendError(const char* error_msg)
364{
365 uint32_t len = strlen(error_msg);
366 write(&len);
367 write(error_msg);
368}
369
370void
371VncServer::checkProtocolVersion()
372{
373 assert(curState == WaitForProtocolVersion);
374
375 size_t len M5_VAR_USED;
376 char version_string[13];
377
378 // Null terminate the message so it's easier to work with
379 version_string[12] = 0;
380
381 len = read((uint8_t*)version_string, 12);
382 assert(len == 12);
383
384 uint32_t major, minor;
385
386 // Figure out the major/minor numbers
387 if (sscanf(version_string, "RFB %03d.%03d\n", &major, &minor) != 2) {
388 warn(" Malformed protocol version %s\n", version_string);
389 sendError("Malformed protocol version\n");
390 detach();
391 }
392
393 DPRINTF(VNC, "Client request protocol version %d.%d\n", major, minor);
394
395 // If it's not 3.X we don't support it
396 if (major != 3 || minor < 2) {
397 warn("Unsupported VNC client version... disconnecting\n");
398 uint8_t err = AuthInvalid;
399 write(&err);
400 detach();
401 }
402 // Auth is different based on version number
403 if (minor < 7) {
404 uint32_t sec_type = htobe((uint32_t)AuthNone);
405 write(&sec_type);
406 } else {
407 uint8_t sec_cnt = 1;
408 uint8_t sec_type = htobe((uint8_t)AuthNone);
409 write(&sec_cnt);
410 write(&sec_type);
411 }
412
413 // Wait for client to respond
414 curState = WaitForSecurityResponse;
415}
416
417void
418VncServer::checkSecurity()
419{
420 assert(curState == WaitForSecurityResponse);
421
422 uint8_t security_type;
423 size_t len M5_VAR_USED = read(&security_type);
424
425 assert(len == 1);
426
427 if (security_type != AuthNone) {
428 warn("Unknown VNC security type\n");
429 sendError("Unknown security type\n");
430 }
431
432 DPRINTF(VNC, "Sending security auth OK\n");
433
434 uint32_t success = htobe(VncOK);
435 write(&success);
436 curState = WaitForClientInit;
437}
438
439void
440VncServer::sendServerInit()
441{
442 ServerInitMsg msg;
443
444 DPRINTF(VNC, "Sending server init message to client\n");
445
446 msg.fbWidth = htobe(videoWidth());
447 msg.fbHeight = htobe(videoHeight());
448
449 msg.px.bpp = htobe(pixelFormat.bpp);
450 msg.px.depth = htobe(pixelFormat.depth);
451 msg.px.bigendian = htobe(pixelFormat.bigendian);
452 msg.px.truecolor = htobe(pixelFormat.truecolor);
453 msg.px.redmax = htobe(pixelFormat.redmax);
454 msg.px.greenmax = htobe(pixelFormat.greenmax);
455 msg.px.bluemax = htobe(pixelFormat.bluemax);
456 msg.px.redshift = htobe(pixelFormat.redshift);
457 msg.px.greenshift = htobe(pixelFormat.greenshift);
458 msg.px.blueshift = htobe(pixelFormat.blueshift);
459 memset(msg.px.padding, 0, 3);
460 msg.namelen = 2;
461 msg.namelen = htobe(msg.namelen);
462 memcpy(msg.name, "M5", 2);
463
464 write(&msg);
465 curState = NormalPhase;
466}
467
131 DPRINTF(VNC, "Vnc server created at port %d\n", p->port);
132}
133
134VncServer::~VncServer()
135{
136 if (dataFd != -1)
137 ::close(dataFd);
138
139 if (listenEvent)
140 delete listenEvent;
141
142 if (dataEvent)
143 delete dataEvent;
144}
145
146
147//socket creation and vnc client attach
148void
149VncServer::listen(int port)
150{
151 if (ListenSocket::allDisabled()) {
152 warn_once("Sockets disabled, not accepting vnc client connections");
153 return;
154 }
155
156 while (!listener.listen(port, true)) {
157 DPRINTF(VNC,
158 "can't bind address vnc server port %d in use PID %d\n",
159 port, getpid());
160 port++;
161 }
162
163 int p1, p2;
164 p2 = name().rfind('.') - 1;
165 p1 = name().rfind('.', p2);
166 ccprintf(cerr, "Listening for %s connection on port %d\n",
167 name().substr(p1 + 1, p2 - p1), port);
168
169 listenEvent = new ListenEvent(this, listener.getfd(), POLLIN);
170 pollQueue.schedule(listenEvent);
171}
172
173// attach a vnc client
174void
175VncServer::accept()
176{
177 if (!listener.islistening())
178 panic("%s: cannot accept a connection if not listening!", name());
179
180 int fd = listener.accept(true);
181 if (dataFd != -1) {
182 char message[] = "vnc server already attached!\n";
183 atomic_write(fd, message, sizeof(message));
184 ::close(fd);
185 return;
186 }
187
188 dataFd = fd;
189
190 // Send our version number to the client
191 write((uint8_t*)vncVersion(), strlen(vncVersion()));
192
193 // read the client response
194 dataEvent = new DataEvent(this, dataFd, POLLIN);
195 pollQueue.schedule(dataEvent);
196
197 inform("VNC client attached\n");
198}
199
200// data called by data event
201void
202VncServer::data()
203{
204 // We have new data, see if we can handle it
205 size_t len;
206 DPRINTF(VNC, "Vnc client message recieved\n");
207
208 switch (curState) {
209 case WaitForProtocolVersion:
210 checkProtocolVersion();
211 break;
212 case WaitForSecurityResponse:
213 checkSecurity();
214 break;
215 case WaitForClientInit:
216 // Don't care about shared, just need to read it out of the socket
217 uint8_t shared;
218 len = read(&shared);
219 assert(len == 1);
220
221 // Send our idea of the frame buffer
222 sendServerInit();
223
224 break;
225 case NormalPhase:
226 uint8_t message_type;
227 len = read(&message_type);
228 if (!len) {
229 detach();
230 return;
231 }
232 assert(len == 1);
233
234 switch (message_type) {
235 case ClientSetPixelFormat:
236 setPixelFormat();
237 break;
238 case ClientSetEncodings:
239 setEncodings();
240 break;
241 case ClientFrameBufferUpdate:
242 requestFbUpdate();
243 break;
244 case ClientKeyEvent:
245 recvKeyboardInput();
246 break;
247 case ClientPointerEvent:
248 recvPointerInput();
249 break;
250 case ClientCutText:
251 recvCutText();
252 break;
253 default:
254 panic("Unimplemented message type recv from client: %d\n",
255 message_type);
256 break;
257 }
258 break;
259 default:
260 panic("Unknown vnc server state\n");
261 }
262}
263
264
265// read from socket
266size_t
267VncServer::read(uint8_t *buf, size_t len)
268{
269 if (dataFd < 0)
270 panic("vnc not properly attached.\n");
271
272 size_t ret;
273 do {
274 ret = ::read(dataFd, buf, len);
275 } while (ret == -1 && errno == EINTR);
276
277
278 if (ret <= 0){
279 DPRINTF(VNC, "Read failed.\n");
280 detach();
281 return 0;
282 }
283
284 return ret;
285}
286
287size_t
288VncServer::read1(uint8_t *buf, size_t len)
289{
290 size_t read_len M5_VAR_USED;
291 read_len = read(buf + 1, len - 1);
292 assert(read_len == len - 1);
293 return read_len;
294}
295
296
297template<typename T>
298size_t
299VncServer::read(T* val)
300{
301 return read((uint8_t*)val, sizeof(T));
302}
303
304// write to socket
305size_t
306VncServer::write(const uint8_t *buf, size_t len)
307{
308 if (dataFd < 0)
309 panic("Vnc client not properly attached.\n");
310
311 ssize_t ret;
312 ret = atomic_write(dataFd, buf, len);
313
314 if (ret < len)
315 detach();
316
317 return ret;
318}
319
320template<typename T>
321size_t
322VncServer::write(T* val)
323{
324 return write((uint8_t*)val, sizeof(T));
325}
326
327size_t
328VncServer::write(const char* str)
329{
330 return write((uint8_t*)str, strlen(str));
331}
332
333// detach a vnc client
334void
335VncServer::detach()
336{
337 if (dataFd != -1) {
338 ::close(dataFd);
339 dataFd = -1;
340 }
341
342 if (!dataEvent || !dataEvent->queued())
343 return;
344
345 pollQueue.remove(dataEvent);
346 delete dataEvent;
347 dataEvent = NULL;
348 curState = WaitForProtocolVersion;
349
350 inform("VNC client detached\n");
351 DPRINTF(VNC, "detach vnc client %d\n", number);
352}
353
354void
355VncServer::sendError(const char* error_msg)
356{
357 uint32_t len = strlen(error_msg);
358 write(&len);
359 write(error_msg);
360}
361
362void
363VncServer::checkProtocolVersion()
364{
365 assert(curState == WaitForProtocolVersion);
366
367 size_t len M5_VAR_USED;
368 char version_string[13];
369
370 // Null terminate the message so it's easier to work with
371 version_string[12] = 0;
372
373 len = read((uint8_t*)version_string, 12);
374 assert(len == 12);
375
376 uint32_t major, minor;
377
378 // Figure out the major/minor numbers
379 if (sscanf(version_string, "RFB %03d.%03d\n", &major, &minor) != 2) {
380 warn(" Malformed protocol version %s\n", version_string);
381 sendError("Malformed protocol version\n");
382 detach();
383 }
384
385 DPRINTF(VNC, "Client request protocol version %d.%d\n", major, minor);
386
387 // If it's not 3.X we don't support it
388 if (major != 3 || minor < 2) {
389 warn("Unsupported VNC client version... disconnecting\n");
390 uint8_t err = AuthInvalid;
391 write(&err);
392 detach();
393 }
394 // Auth is different based on version number
395 if (minor < 7) {
396 uint32_t sec_type = htobe((uint32_t)AuthNone);
397 write(&sec_type);
398 } else {
399 uint8_t sec_cnt = 1;
400 uint8_t sec_type = htobe((uint8_t)AuthNone);
401 write(&sec_cnt);
402 write(&sec_type);
403 }
404
405 // Wait for client to respond
406 curState = WaitForSecurityResponse;
407}
408
409void
410VncServer::checkSecurity()
411{
412 assert(curState == WaitForSecurityResponse);
413
414 uint8_t security_type;
415 size_t len M5_VAR_USED = read(&security_type);
416
417 assert(len == 1);
418
419 if (security_type != AuthNone) {
420 warn("Unknown VNC security type\n");
421 sendError("Unknown security type\n");
422 }
423
424 DPRINTF(VNC, "Sending security auth OK\n");
425
426 uint32_t success = htobe(VncOK);
427 write(&success);
428 curState = WaitForClientInit;
429}
430
431void
432VncServer::sendServerInit()
433{
434 ServerInitMsg msg;
435
436 DPRINTF(VNC, "Sending server init message to client\n");
437
438 msg.fbWidth = htobe(videoWidth());
439 msg.fbHeight = htobe(videoHeight());
440
441 msg.px.bpp = htobe(pixelFormat.bpp);
442 msg.px.depth = htobe(pixelFormat.depth);
443 msg.px.bigendian = htobe(pixelFormat.bigendian);
444 msg.px.truecolor = htobe(pixelFormat.truecolor);
445 msg.px.redmax = htobe(pixelFormat.redmax);
446 msg.px.greenmax = htobe(pixelFormat.greenmax);
447 msg.px.bluemax = htobe(pixelFormat.bluemax);
448 msg.px.redshift = htobe(pixelFormat.redshift);
449 msg.px.greenshift = htobe(pixelFormat.greenshift);
450 msg.px.blueshift = htobe(pixelFormat.blueshift);
451 memset(msg.px.padding, 0, 3);
452 msg.namelen = 2;
453 msg.namelen = htobe(msg.namelen);
454 memcpy(msg.name, "M5", 2);
455
456 write(&msg);
457 curState = NormalPhase;
458}
459
468
469void
470VncServer::setPixelFormat()
471{
472 DPRINTF(VNC, "Received pixel format from client message\n");
473
474 PixelFormatMessage pfm;
475 read1((uint8_t*)&pfm, sizeof(PixelFormatMessage));
476
477 DPRINTF(VNC, " -- bpp = %d; depth = %d; be = %d\n", pfm.px.bpp,
478 pfm.px.depth, pfm.px.bigendian);
479 DPRINTF(VNC, " -- true color = %d red,green,blue max = %d,%d,%d\n",
480 pfm.px.truecolor, betoh(pfm.px.redmax), betoh(pfm.px.greenmax),
481 betoh(pfm.px.bluemax));
482 DPRINTF(VNC, " -- red,green,blue shift = %d,%d,%d\n", pfm.px.redshift,
483 pfm.px.greenshift, pfm.px.blueshift);
484
485 if (betoh(pfm.px.bpp) != pixelFormat.bpp ||
486 betoh(pfm.px.depth) != pixelFormat.depth ||
487 betoh(pfm.px.bigendian) != pixelFormat.bigendian ||
488 betoh(pfm.px.truecolor) != pixelFormat.truecolor ||
489 betoh(pfm.px.redmax) != pixelFormat.redmax ||
490 betoh(pfm.px.greenmax) != pixelFormat.greenmax ||
491 betoh(pfm.px.bluemax) != pixelFormat.bluemax ||
492 betoh(pfm.px.redshift) != pixelFormat.redshift ||
493 betoh(pfm.px.greenshift) != pixelFormat.greenshift ||
494 betoh(pfm.px.blueshift) != pixelFormat.blueshift)
495 fatal("VNC client doesn't support true color raw encoding\n");
496}
497
498void
499VncServer::setEncodings()
500{
501 DPRINTF(VNC, "Received supported encodings from client\n");
502
503 PixelEncodingsMessage pem;
504 read1((uint8_t*)&pem, sizeof(PixelEncodingsMessage));
505
506 pem.num_encodings = betoh(pem.num_encodings);
507
508 DPRINTF(VNC, " -- %d encoding present\n", pem.num_encodings);
509 supportsRawEnc = supportsResizeEnc = false;
510
511 for (int x = 0; x < pem.num_encodings; x++) {
512 int32_t encoding;
513 size_t len M5_VAR_USED;
514 len = read(&encoding);
515 assert(len == sizeof(encoding));
516 DPRINTF(VNC, " -- supports %d\n", betoh(encoding));
517
518 switch (betoh(encoding)) {
519 case EncodingRaw:
520 supportsRawEnc = true;
521 break;
522 case EncodingDesktopSize:
523 supportsResizeEnc = true;
524 break;
525 }
526 }
527
528 if (!supportsRawEnc)
529 fatal("VNC clients must always support raw encoding\n");
530}
531
532void
533VncServer::requestFbUpdate()
534{
535 DPRINTF(VNC, "Received frame buffer update request from client\n");
536
537 FrameBufferUpdateReq fbr;
538 read1((uint8_t*)&fbr, sizeof(FrameBufferUpdateReq));
539
540 fbr.x = betoh(fbr.x);
541 fbr.y = betoh(fbr.y);
542 fbr.width = betoh(fbr.width);
543 fbr.height = betoh(fbr.height);
544
545 DPRINTF(VNC, " -- x = %d y = %d w = %d h = %d\n", fbr.x, fbr.y, fbr.width,
546 fbr.height);
547
548 sendFrameBufferUpdate();
549}
550
551void
552VncServer::recvKeyboardInput()
553{
554 DPRINTF(VNC, "Received keyboard input from client\n");
555 KeyEventMessage kem;
556 read1((uint8_t*)&kem, sizeof(KeyEventMessage));
557
558 kem.key = betoh(kem.key);
559 DPRINTF(VNC, " -- received key code %d (%s)\n", kem.key, kem.down_flag ?
560 "down" : "up");
561
562 if (keyboard)
563 keyboard->keyPress(kem.key, kem.down_flag);
564}
565
566void
567VncServer::recvPointerInput()
568{
569 DPRINTF(VNC, "Received pointer input from client\n");
570 PointerEventMessage pem;
571
572 read1((uint8_t*)&pem, sizeof(PointerEventMessage));;
573
574 pem.x = betoh(pem.x);
575 pem.y = betoh(pem.y);
576 DPRINTF(VNC, " -- pointer at x = %d y = %d buttons = %#x\n", pem.x, pem.y,
577 pem.button_mask);
578
579 if (mouse)
580 mouse->mouseAt(pem.x, pem.y, pem.button_mask);
581}
582
583void
584VncServer::recvCutText()
585{
586 DPRINTF(VNC, "Received client copy buffer message\n");
587
588 ClientCutTextMessage cct;
589 read1((uint8_t*)&cct, sizeof(ClientCutTextMessage));
590
591 char str[1025];
592 size_t data_len = betoh(cct.length);
593 DPRINTF(VNC, "String length %d\n", data_len);
594 while (data_len > 0) {
595 size_t len;
596 size_t bytes_to_read = data_len > 1024 ? 1024 : data_len;
597 len = read((uint8_t*)&str, bytes_to_read);
598 str[bytes_to_read] = 0;
599 data_len -= len;
600 assert(data_len >= 0);
601 DPRINTF(VNC, "Buffer: %s\n", str);
602 }
603
604}
605
606
607void
608VncServer::sendFrameBufferUpdate()
609{
610
460void
461VncServer::setPixelFormat()
462{
463 DPRINTF(VNC, "Received pixel format from client message\n");
464
465 PixelFormatMessage pfm;
466 read1((uint8_t*)&pfm, sizeof(PixelFormatMessage));
467
468 DPRINTF(VNC, " -- bpp = %d; depth = %d; be = %d\n", pfm.px.bpp,
469 pfm.px.depth, pfm.px.bigendian);
470 DPRINTF(VNC, " -- true color = %d red,green,blue max = %d,%d,%d\n",
471 pfm.px.truecolor, betoh(pfm.px.redmax), betoh(pfm.px.greenmax),
472 betoh(pfm.px.bluemax));
473 DPRINTF(VNC, " -- red,green,blue shift = %d,%d,%d\n", pfm.px.redshift,
474 pfm.px.greenshift, pfm.px.blueshift);
475
476 if (betoh(pfm.px.bpp) != pixelFormat.bpp ||
477 betoh(pfm.px.depth) != pixelFormat.depth ||
478 betoh(pfm.px.bigendian) != pixelFormat.bigendian ||
479 betoh(pfm.px.truecolor) != pixelFormat.truecolor ||
480 betoh(pfm.px.redmax) != pixelFormat.redmax ||
481 betoh(pfm.px.greenmax) != pixelFormat.greenmax ||
482 betoh(pfm.px.bluemax) != pixelFormat.bluemax ||
483 betoh(pfm.px.redshift) != pixelFormat.redshift ||
484 betoh(pfm.px.greenshift) != pixelFormat.greenshift ||
485 betoh(pfm.px.blueshift) != pixelFormat.blueshift)
486 fatal("VNC client doesn't support true color raw encoding\n");
487}
488
489void
490VncServer::setEncodings()
491{
492 DPRINTF(VNC, "Received supported encodings from client\n");
493
494 PixelEncodingsMessage pem;
495 read1((uint8_t*)&pem, sizeof(PixelEncodingsMessage));
496
497 pem.num_encodings = betoh(pem.num_encodings);
498
499 DPRINTF(VNC, " -- %d encoding present\n", pem.num_encodings);
500 supportsRawEnc = supportsResizeEnc = false;
501
502 for (int x = 0; x < pem.num_encodings; x++) {
503 int32_t encoding;
504 size_t len M5_VAR_USED;
505 len = read(&encoding);
506 assert(len == sizeof(encoding));
507 DPRINTF(VNC, " -- supports %d\n", betoh(encoding));
508
509 switch (betoh(encoding)) {
510 case EncodingRaw:
511 supportsRawEnc = true;
512 break;
513 case EncodingDesktopSize:
514 supportsResizeEnc = true;
515 break;
516 }
517 }
518
519 if (!supportsRawEnc)
520 fatal("VNC clients must always support raw encoding\n");
521}
522
523void
524VncServer::requestFbUpdate()
525{
526 DPRINTF(VNC, "Received frame buffer update request from client\n");
527
528 FrameBufferUpdateReq fbr;
529 read1((uint8_t*)&fbr, sizeof(FrameBufferUpdateReq));
530
531 fbr.x = betoh(fbr.x);
532 fbr.y = betoh(fbr.y);
533 fbr.width = betoh(fbr.width);
534 fbr.height = betoh(fbr.height);
535
536 DPRINTF(VNC, " -- x = %d y = %d w = %d h = %d\n", fbr.x, fbr.y, fbr.width,
537 fbr.height);
538
539 sendFrameBufferUpdate();
540}
541
542void
543VncServer::recvKeyboardInput()
544{
545 DPRINTF(VNC, "Received keyboard input from client\n");
546 KeyEventMessage kem;
547 read1((uint8_t*)&kem, sizeof(KeyEventMessage));
548
549 kem.key = betoh(kem.key);
550 DPRINTF(VNC, " -- received key code %d (%s)\n", kem.key, kem.down_flag ?
551 "down" : "up");
552
553 if (keyboard)
554 keyboard->keyPress(kem.key, kem.down_flag);
555}
556
557void
558VncServer::recvPointerInput()
559{
560 DPRINTF(VNC, "Received pointer input from client\n");
561 PointerEventMessage pem;
562
563 read1((uint8_t*)&pem, sizeof(PointerEventMessage));;
564
565 pem.x = betoh(pem.x);
566 pem.y = betoh(pem.y);
567 DPRINTF(VNC, " -- pointer at x = %d y = %d buttons = %#x\n", pem.x, pem.y,
568 pem.button_mask);
569
570 if (mouse)
571 mouse->mouseAt(pem.x, pem.y, pem.button_mask);
572}
573
574void
575VncServer::recvCutText()
576{
577 DPRINTF(VNC, "Received client copy buffer message\n");
578
579 ClientCutTextMessage cct;
580 read1((uint8_t*)&cct, sizeof(ClientCutTextMessage));
581
582 char str[1025];
583 size_t data_len = betoh(cct.length);
584 DPRINTF(VNC, "String length %d\n", data_len);
585 while (data_len > 0) {
586 size_t len;
587 size_t bytes_to_read = data_len > 1024 ? 1024 : data_len;
588 len = read((uint8_t*)&str, bytes_to_read);
589 str[bytes_to_read] = 0;
590 data_len -= len;
591 assert(data_len >= 0);
592 DPRINTF(VNC, "Buffer: %s\n", str);
593 }
594
595}
596
597
598void
599VncServer::sendFrameBufferUpdate()
600{
601
611 if (!clientRfb || dataFd <= 0 || curState != NormalPhase || !sendUpdate) {
602 if (!fbPtr || dataFd <= 0 || curState != NormalPhase || !sendUpdate) {
612 DPRINTF(VNC, "NOT sending framebuffer update\n");
613 return;
614 }
615
616 assert(vc);
617
618 // The client will request data constantly, unless we throttle it
619 sendUpdate = false;
620
621 DPRINTF(VNC, "Sending framebuffer update\n");
622
623 FrameBufferUpdate fbu;
624 FrameBufferRect fbr;
625
626 fbu.type = ServerFrameBufferUpdate;
627 fbu.num_rects = 1;
628 fbr.x = 0;
629 fbr.y = 0;
630 fbr.width = videoWidth();
631 fbr.height = videoHeight();
632 fbr.encoding = EncodingRaw;
633
634 // fix up endian
635 fbu.num_rects = htobe(fbu.num_rects);
636 fbr.x = htobe(fbr.x);
637 fbr.y = htobe(fbr.y);
638 fbr.width = htobe(fbr.width);
639 fbr.height = htobe(fbr.height);
640 fbr.encoding = htobe(fbr.encoding);
641
642 // send headers to client
643 write(&fbu);
644 write(&fbr);
645
603 DPRINTF(VNC, "NOT sending framebuffer update\n");
604 return;
605 }
606
607 assert(vc);
608
609 // The client will request data constantly, unless we throttle it
610 sendUpdate = false;
611
612 DPRINTF(VNC, "Sending framebuffer update\n");
613
614 FrameBufferUpdate fbu;
615 FrameBufferRect fbr;
616
617 fbu.type = ServerFrameBufferUpdate;
618 fbu.num_rects = 1;
619 fbr.x = 0;
620 fbr.y = 0;
621 fbr.width = videoWidth();
622 fbr.height = videoHeight();
623 fbr.encoding = EncodingRaw;
624
625 // fix up endian
626 fbu.num_rects = htobe(fbu.num_rects);
627 fbr.x = htobe(fbr.x);
628 fbr.y = htobe(fbr.y);
629 fbr.width = htobe(fbr.width);
630 fbr.height = htobe(fbr.height);
631 fbr.encoding = htobe(fbr.encoding);
632
633 // send headers to client
634 write(&fbu);
635 write(&fbr);
636
646 assert(clientRfb);
637 assert(fbPtr);
647
638
648 uint8_t *tmp = vc->convert(clientRfb);
639 uint8_t *tmp = vc->convert(fbPtr);
649 write(tmp, videoWidth() * videoHeight() * sizeof(uint32_t));
650 delete [] tmp;
651
652}
653
654void
655VncServer::sendFrameBufferResized()
656{
640 write(tmp, videoWidth() * videoHeight() * sizeof(uint32_t));
641 delete [] tmp;
642
643}
644
645void
646VncServer::sendFrameBufferResized()
647{
657 assert(clientRfb && dataFd > 0 && curState == NormalPhase);
648 assert(fbPtr && dataFd > 0 && curState == NormalPhase);
658 DPRINTF(VNC, "Sending framebuffer resize\n");
659
660 FrameBufferUpdate fbu;
661 FrameBufferRect fbr;
662
663 fbu.type = ServerFrameBufferUpdate;
664 fbu.num_rects = 1;
665 fbr.x = 0;
666 fbr.y = 0;
667 fbr.width = videoWidth();
668 fbr.height = videoHeight();
669 fbr.encoding = EncodingDesktopSize;
670
671 // fix up endian
672 fbu.num_rects = htobe(fbu.num_rects);
673 fbr.x = htobe(fbr.x);
674 fbr.y = htobe(fbr.y);
675 fbr.width = htobe(fbr.width);
676 fbr.height = htobe(fbr.height);
677 fbr.encoding = htobe(fbr.encoding);
678
679 // send headers to client
680 write(&fbu);
681 write(&fbr);
682
683 // No actual data is sent in this message
684}
685
686void
649 DPRINTF(VNC, "Sending framebuffer resize\n");
650
651 FrameBufferUpdate fbu;
652 FrameBufferRect fbr;
653
654 fbu.type = ServerFrameBufferUpdate;
655 fbu.num_rects = 1;
656 fbr.x = 0;
657 fbr.y = 0;
658 fbr.width = videoWidth();
659 fbr.height = videoHeight();
660 fbr.encoding = EncodingDesktopSize;
661
662 // fix up endian
663 fbu.num_rects = htobe(fbu.num_rects);
664 fbr.x = htobe(fbr.x);
665 fbr.y = htobe(fbr.y);
666 fbr.width = htobe(fbr.width);
667 fbr.height = htobe(fbr.height);
668 fbr.encoding = htobe(fbr.encoding);
669
670 // send headers to client
671 write(&fbu);
672 write(&fbr);
673
674 // No actual data is sent in this message
675}
676
677void
687VncServer::setFrameBufferParams(VideoConvert::Mode mode, int width, int height)
678VncServer::setFrameBufferParams(VideoConvert::Mode mode, uint16_t width,
679 uint16_t height)
688{
680{
689 DPRINTF(VNC, "Updating video params: mode: %d width: %d height: %d\n", mode,
690 width, height);
681 VncInput::setFrameBufferParams(mode, width, height);
691
692 if (mode != videoMode || width != videoWidth() || height != videoHeight()) {
682
683 if (mode != videoMode || width != videoWidth() || height != videoHeight()) {
693 videoMode = mode;
694 _videoWidth = width;
695 _videoHeight = height;
696
697 if (vc)
698 delete vc;
699
700 vc = new VideoConvert(mode, VideoConvert::rgb8888, videoWidth(),
701 videoHeight());
702
703 if (captureEnabled) {
704 // create bitmap of the frame with new attributes
705 if (captureBitmap)
706 delete captureBitmap;
707
708 assert(clientRfb);
709 captureBitmap = new Bitmap(videoMode, width, height, clientRfb);
710 assert(captureBitmap);
711 }
712
713 if (dataFd > 0 && clientRfb && curState == NormalPhase) {
684 if (dataFd > 0 && fbPtr && curState == NormalPhase) {
714 if (supportsResizeEnc)
715 sendFrameBufferResized();
716 else
717 // The frame buffer changed size and we can't update the client
718 detach();
719 }
720 }
721}
722
723// create the VNC server object
724VncServer *
725VncServerParams::create()
726{
727 return new VncServer(this);
728}
729
685 if (supportsResizeEnc)
686 sendFrameBufferResized();
687 else
688 // The frame buffer changed size and we can't update the client
689 detach();
690 }
691 }
692}
693
694// create the VNC server object
695VncServer *
696VncServerParams::create()
697{
698 return new VncServer(this);
699}
700
730void
731VncServer::captureFrameBuffer()
732{
733 assert(captureBitmap);
734
735 // skip identical frames
736 uint64_t new_hash = captureBitmap->getHash();
737 if (captureLastHash == new_hash)
738 return;
739 captureLastHash = new_hash;
740
741 // get the filename for the current frame
742 char frameFilenameBuffer[64];
743 snprintf(frameFilenameBuffer, 64, "fb.%06d.%lld.bmp.gz",
744 captureCurrentFrame, static_cast<long long int>(curTick()));
745 const string frameFilename(frameFilenameBuffer);
746
747 // create the compressed framebuffer file
748 ostream *fb_out = simout.create(captureOutputDirectory + frameFilename,
749 true);
750 captureBitmap->write(fb_out);
751 simout.close(fb_out);
752
753 ++captureCurrentFrame;
754}