1 /* 2 * QEMU VNC display driver 3 * 4 * Copyright (C) 2006 Anthony Liguori <anthony@codemonkey.ws> 5 * Copyright (C) 2006 Fabrice Bellard 6 * Copyright (C) 2009 Red Hat, Inc 7 * 8 * Permission is hereby granted, free of charge, to any person obtaining a copy 9 * of this software and associated documentation files (the "Software"), to deal 10 * in the Software without restriction, including without limitation the rights 11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 * copies of the Software, and to permit persons to whom the Software is 13 * furnished to do so, subject to the following conditions: 14 * 15 * The above copyright notice and this permission notice shall be included in 16 * all copies or substantial portions of the Software. 17 * 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 * THE SOFTWARE. 25 */ 26 27 #ifndef __QEMU_VNC_H 28 #define __QEMU_VNC_H 29 30 #include "qemu-common.h" 31 #include "qemu-queue.h" 32 #ifdef CONFIG_VNC_THREAD 33 #include "qemu-thread.h" 34 #endif 35 #include "console.h" 36 #include "monitor.h" 37 #include "audio/audio.h" 38 #include <zlib.h> 39 #include <stdbool.h> 40 41 #include "keymaps.h" 42 43 // #define _VNC_DEBUG 1 44 45 #ifdef _VNC_DEBUG 46 #define VNC_DEBUG(fmt, ...) do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0) 47 #else 48 #define VNC_DEBUG(fmt, ...) do { } while (0) 49 #endif 50 51 /***************************************************************************** 52 * 53 * Core data structures 54 * 55 *****************************************************************************/ 56 57 typedef struct Buffer 58 { 59 size_t capacity; 60 size_t offset; 61 uint8_t *buffer; 62 } Buffer; 63 64 typedef struct VncState VncState; 65 typedef struct VncJob VncJob; 66 typedef struct VncRect VncRect; 67 typedef struct VncRectEntry VncRectEntry; 68 69 typedef int VncReadEvent(VncState *vs, uint8_t *data, size_t len); 70 71 typedef void VncWritePixels(VncState *vs, struct PixelFormat *pf, void *data, int size); 72 73 typedef void VncSendHextileTile(VncState *vs, 74 int x, int y, int w, int h, 75 void *last_bg, 76 void *last_fg, 77 int *has_bg, int *has_fg); 78 79 #define VNC_MAX_WIDTH 2560 80 #define VNC_MAX_HEIGHT 2048 81 #define VNC_DIRTY_WORDS (VNC_MAX_WIDTH / (16 * 32)) 82 83 #define VNC_AUTH_CHALLENGE_SIZE 16 84 85 typedef struct VncDisplay VncDisplay; 86 87 #ifdef CONFIG_VNC_TLS 88 #include "vnc-tls.h" 89 #include "vnc-auth-vencrypt.h" 90 #endif 91 #ifdef CONFIG_VNC_SASL 92 #include "vnc-auth-sasl.h" 93 #endif 94 95 struct VncSurface 96 { 97 uint32_t dirty[VNC_MAX_HEIGHT][VNC_DIRTY_WORDS]; 98 DisplaySurface *ds; 99 }; 100 101 struct VncDisplay 102 { 103 QTAILQ_HEAD(, VncState) clients; 104 QEMUTimer *timer; 105 int timer_interval; 106 int lsock; 107 DisplayState *ds; 108 kbd_layout_t *kbd_layout; 109 int lock_key_sync; 110 #ifdef CONFIG_VNC_THREAD 111 QemuMutex mutex; 112 #endif 113 114 QEMUCursor *cursor; 115 int cursor_msize; 116 uint8_t *cursor_mask; 117 118 struct VncSurface guest; /* guest visible surface (aka ds->surface) */ 119 DisplaySurface *server; /* vnc server surface */ 120 121 char *display; 122 char *password; 123 time_t expires; 124 int auth; 125 bool lossy; 126 #ifdef CONFIG_VNC_TLS 127 int subauth; /* Used by VeNCrypt */ 128 VncDisplayTLS tls; 129 #endif 130 #ifdef CONFIG_VNC_SASL 131 VncDisplaySASL sasl; 132 #endif 133 }; 134 135 typedef struct VncTight { 136 int type; 137 uint8_t quality; 138 uint8_t compression; 139 uint8_t pixel24; 140 Buffer tight; 141 Buffer tmp; 142 Buffer zlib; 143 Buffer gradient; 144 #ifdef CONFIG_VNC_JPEG 145 Buffer jpeg; 146 #endif 147 #ifdef CONFIG_VNC_PNG 148 Buffer png; 149 #endif 150 int levels[4]; 151 z_stream stream[4]; 152 } VncTight; 153 154 typedef struct VncHextile { 155 VncSendHextileTile *send_tile; 156 } VncHextile; 157 158 typedef struct VncZlib { 159 Buffer zlib; 160 Buffer tmp; 161 z_stream stream; 162 int level; 163 } VncZlib; 164 165 #ifdef CONFIG_VNC_THREAD 166 struct VncRect 167 { 168 int x; 169 int y; 170 int w; 171 int h; 172 }; 173 174 struct VncRectEntry 175 { 176 struct VncRect rect; 177 QLIST_ENTRY(VncRectEntry) next; 178 }; 179 180 struct VncJob 181 { 182 VncState *vs; 183 184 QLIST_HEAD(, VncRectEntry) rectangles; 185 QTAILQ_ENTRY(VncJob) next; 186 }; 187 #else 188 struct VncJob 189 { 190 VncState *vs; 191 int rectangles; 192 size_t saved_offset; 193 }; 194 #endif 195 196 struct VncState 197 { 198 int csock; 199 200 DisplayState *ds; 201 uint32_t dirty[VNC_MAX_HEIGHT][VNC_DIRTY_WORDS]; 202 203 VncDisplay *vd; 204 int need_update; 205 int force_update; 206 uint32_t features; 207 int absolute; 208 int last_x; 209 int last_y; 210 int client_width; 211 int client_height; 212 213 uint32_t vnc_encoding; 214 215 int major; 216 int minor; 217 218 char challenge[VNC_AUTH_CHALLENGE_SIZE]; 219 #ifdef CONFIG_VNC_TLS 220 VncStateTLS tls; 221 #endif 222 #ifdef CONFIG_VNC_SASL 223 VncStateSASL sasl; 224 #endif 225 226 QObject *info; 227 228 Buffer output; 229 Buffer input; 230 /* current output mode information */ 231 VncWritePixels *write_pixels; 232 DisplaySurface clientds; 233 234 CaptureVoiceOut *audio_cap; 235 struct audsettings as; 236 237 VncReadEvent *read_handler; 238 size_t read_handler_expect; 239 /* input */ 240 uint8_t modifiers_state[256]; 241 QEMUPutLEDEntry *led; 242 243 bool abort; 244 #ifndef CONFIG_VNC_THREAD 245 VncJob job; 246 #else 247 QemuMutex output_mutex; 248 #endif 249 250 /* Encoding specific, if you add something here, don't forget to 251 * update vnc_async_encoding_start() 252 */ 253 VncTight tight; 254 VncZlib zlib; 255 VncHextile hextile; 256 257 258 Notifier mouse_mode_notifier; 259 260 QTAILQ_ENTRY(VncState) next; 261 }; 262 263 264 /***************************************************************************** 265 * 266 * Authentication modes 267 * 268 *****************************************************************************/ 269 270 enum { 271 VNC_AUTH_INVALID = 0, 272 VNC_AUTH_NONE = 1, 273 VNC_AUTH_VNC = 2, 274 VNC_AUTH_RA2 = 5, 275 VNC_AUTH_RA2NE = 6, 276 VNC_AUTH_TIGHT = 16, 277 VNC_AUTH_ULTRA = 17, 278 VNC_AUTH_TLS = 18, /* Supported in GTK-VNC & VINO */ 279 VNC_AUTH_VENCRYPT = 19, /* Supported in GTK-VNC & VeNCrypt */ 280 VNC_AUTH_SASL = 20, /* Supported in GTK-VNC & VINO */ 281 }; 282 283 enum { 284 VNC_AUTH_VENCRYPT_PLAIN = 256, 285 VNC_AUTH_VENCRYPT_TLSNONE = 257, 286 VNC_AUTH_VENCRYPT_TLSVNC = 258, 287 VNC_AUTH_VENCRYPT_TLSPLAIN = 259, 288 VNC_AUTH_VENCRYPT_X509NONE = 260, 289 VNC_AUTH_VENCRYPT_X509VNC = 261, 290 VNC_AUTH_VENCRYPT_X509PLAIN = 262, 291 VNC_AUTH_VENCRYPT_X509SASL = 263, 292 VNC_AUTH_VENCRYPT_TLSSASL = 264, 293 }; 294 295 296 /***************************************************************************** 297 * 298 * Encoding types 299 * 300 *****************************************************************************/ 301 302 #define VNC_ENCODING_RAW 0x00000000 303 #define VNC_ENCODING_COPYRECT 0x00000001 304 #define VNC_ENCODING_RRE 0x00000002 305 #define VNC_ENCODING_CORRE 0x00000004 306 #define VNC_ENCODING_HEXTILE 0x00000005 307 #define VNC_ENCODING_ZLIB 0x00000006 308 #define VNC_ENCODING_TIGHT 0x00000007 309 #define VNC_ENCODING_ZLIBHEX 0x00000008 310 #define VNC_ENCODING_TRLE 0x0000000f 311 #define VNC_ENCODING_ZRLE 0x00000010 312 #define VNC_ENCODING_ZYWRLE 0x00000011 313 #define VNC_ENCODING_COMPRESSLEVEL0 0xFFFFFF00 /* -256 */ 314 #define VNC_ENCODING_QUALITYLEVEL0 0xFFFFFFE0 /* -32 */ 315 #define VNC_ENCODING_XCURSOR 0xFFFFFF10 /* -240 */ 316 #define VNC_ENCODING_RICH_CURSOR 0xFFFFFF11 /* -239 */ 317 #define VNC_ENCODING_POINTER_POS 0xFFFFFF18 /* -232 */ 318 #define VNC_ENCODING_LASTRECT 0xFFFFFF20 /* -224 */ 319 #define VNC_ENCODING_DESKTOPRESIZE 0xFFFFFF21 /* -223 */ 320 #define VNC_ENCODING_POINTER_TYPE_CHANGE 0XFFFFFEFF /* -257 */ 321 #define VNC_ENCODING_EXT_KEY_EVENT 0XFFFFFEFE /* -258 */ 322 #define VNC_ENCODING_AUDIO 0XFFFFFEFD /* -259 */ 323 #define VNC_ENCODING_TIGHT_PNG 0xFFFFFEFC /* -260 */ 324 #define VNC_ENCODING_WMVi 0x574D5669 325 326 /***************************************************************************** 327 * 328 * Other tight constants 329 * 330 *****************************************************************************/ 331 332 /* 333 * Vendors known by TightVNC: standard VNC/RealVNC, TridiaVNC, and TightVNC. 334 */ 335 336 #define VNC_TIGHT_CCB_RESET_MASK (0x0f) 337 #define VNC_TIGHT_CCB_TYPE_MASK (0x0f << 4) 338 #define VNC_TIGHT_CCB_TYPE_FILL (0x08 << 4) 339 #define VNC_TIGHT_CCB_TYPE_JPEG (0x09 << 4) 340 #define VNC_TIGHT_CCB_TYPE_PNG (0x0A << 4) 341 #define VNC_TIGHT_CCB_BASIC_MAX (0x07 << 4) 342 #define VNC_TIGHT_CCB_BASIC_ZLIB (0x03 << 4) 343 #define VNC_TIGHT_CCB_BASIC_FILTER (0x04 << 4) 344 345 /***************************************************************************** 346 * 347 * Features 348 * 349 *****************************************************************************/ 350 351 #define VNC_FEATURE_RESIZE 0 352 #define VNC_FEATURE_HEXTILE 1 353 #define VNC_FEATURE_POINTER_TYPE_CHANGE 2 354 #define VNC_FEATURE_WMVI 3 355 #define VNC_FEATURE_TIGHT 4 356 #define VNC_FEATURE_ZLIB 5 357 #define VNC_FEATURE_COPYRECT 6 358 #define VNC_FEATURE_RICH_CURSOR 7 359 #define VNC_FEATURE_TIGHT_PNG 8 360 361 #define VNC_FEATURE_RESIZE_MASK (1 << VNC_FEATURE_RESIZE) 362 #define VNC_FEATURE_HEXTILE_MASK (1 << VNC_FEATURE_HEXTILE) 363 #define VNC_FEATURE_POINTER_TYPE_CHANGE_MASK (1 << VNC_FEATURE_POINTER_TYPE_CHANGE) 364 #define VNC_FEATURE_WMVI_MASK (1 << VNC_FEATURE_WMVI) 365 #define VNC_FEATURE_TIGHT_MASK (1 << VNC_FEATURE_TIGHT) 366 #define VNC_FEATURE_ZLIB_MASK (1 << VNC_FEATURE_ZLIB) 367 #define VNC_FEATURE_COPYRECT_MASK (1 << VNC_FEATURE_COPYRECT) 368 #define VNC_FEATURE_RICH_CURSOR_MASK (1 << VNC_FEATURE_RICH_CURSOR) 369 #define VNC_FEATURE_TIGHT_PNG_MASK (1 << VNC_FEATURE_TIGHT_PNG) 370 371 372 /* Client -> Server message IDs */ 373 #define VNC_MSG_CLIENT_SET_PIXEL_FORMAT 0 374 #define VNC_MSG_CLIENT_SET_ENCODINGS 2 375 #define VNC_MSG_CLIENT_FRAMEBUFFER_UPDATE_REQUEST 3 376 #define VNC_MSG_CLIENT_KEY_EVENT 4 377 #define VNC_MSG_CLIENT_POINTER_EVENT 5 378 #define VNC_MSG_CLIENT_CUT_TEXT 6 379 #define VNC_MSG_CLIENT_VMWARE_0 127 380 #define VNC_MSG_CLIENT_CALL_CONTROL 249 381 #define VNC_MSG_CLIENT_XVP 250 382 #define VNC_MSG_CLIENT_SET_DESKTOP_SIZE 251 383 #define VNC_MSG_CLIENT_TIGHT 252 384 #define VNC_MSG_CLIENT_GII 253 385 #define VNC_MSG_CLIENT_VMWARE_1 254 386 #define VNC_MSG_CLIENT_QEMU 255 387 388 /* Server -> Client message IDs */ 389 #define VNC_MSG_SERVER_FRAMEBUFFER_UPDATE 0 390 #define VNC_MSG_SERVER_SET_COLOUR_MAP_ENTRIES 1 391 #define VNC_MSG_SERVER_BELL 2 392 #define VNC_MSG_SERVER_CUT_TEXT 3 393 #define VNC_MSG_SERVER_VMWARE_0 127 394 #define VNC_MSG_SERVER_CALL_CONTROL 249 395 #define VNC_MSG_SERVER_XVP 250 396 #define VNC_MSG_SERVER_TIGHT 252 397 #define VNC_MSG_SERVER_GII 253 398 #define VNC_MSG_SERVER_VMWARE_1 254 399 #define VNC_MSG_SERVER_QEMU 255 400 401 402 403 /* QEMU client -> server message IDs */ 404 #define VNC_MSG_CLIENT_QEMU_EXT_KEY_EVENT 0 405 #define VNC_MSG_CLIENT_QEMU_AUDIO 1 406 407 /* QEMU server -> client message IDs */ 408 #define VNC_MSG_SERVER_QEMU_AUDIO 1 409 410 411 412 /* QEMU client -> server audio message IDs */ 413 #define VNC_MSG_CLIENT_QEMU_AUDIO_ENABLE 0 414 #define VNC_MSG_CLIENT_QEMU_AUDIO_DISABLE 1 415 #define VNC_MSG_CLIENT_QEMU_AUDIO_SET_FORMAT 2 416 417 /* QEMU server -> client audio message IDs */ 418 #define VNC_MSG_SERVER_QEMU_AUDIO_END 0 419 #define VNC_MSG_SERVER_QEMU_AUDIO_BEGIN 1 420 #define VNC_MSG_SERVER_QEMU_AUDIO_DATA 2 421 422 423 /***************************************************************************** 424 * 425 * Internal APIs 426 * 427 *****************************************************************************/ 428 429 /* Event loop functions */ 430 void vnc_client_read(void *opaque); 431 void vnc_client_write(void *opaque); 432 433 long vnc_client_read_buf(VncState *vs, uint8_t *data, size_t datalen); 434 long vnc_client_write_buf(VncState *vs, const uint8_t *data, size_t datalen); 435 436 /* Protocol I/O functions */ 437 void vnc_write(VncState *vs, const void *data, size_t len); 438 void vnc_write_u32(VncState *vs, uint32_t value); 439 void vnc_write_s32(VncState *vs, int32_t value); 440 void vnc_write_u16(VncState *vs, uint16_t value); 441 void vnc_write_u8(VncState *vs, uint8_t value); 442 void vnc_flush(VncState *vs); 443 void vnc_read_when(VncState *vs, VncReadEvent *func, size_t expecting); 444 445 446 /* Buffer I/O functions */ 447 uint8_t read_u8(uint8_t *data, size_t offset); 448 uint16_t read_u16(uint8_t *data, size_t offset); 449 int32_t read_s32(uint8_t *data, size_t offset); 450 uint32_t read_u32(uint8_t *data, size_t offset); 451 452 /* Protocol stage functions */ 453 void vnc_client_error(VncState *vs); 454 int vnc_client_io_error(VncState *vs, int ret, int last_errno); 455 456 void start_client_init(VncState *vs); 457 void start_auth_vnc(VncState *vs); 458 459 /* Buffer management */ 460 void buffer_reserve(Buffer *buffer, size_t len); 461 int buffer_empty(Buffer *buffer); 462 uint8_t *buffer_end(Buffer *buffer); 463 void buffer_reset(Buffer *buffer); 464 void buffer_free(Buffer *buffer); 465 void buffer_append(Buffer *buffer, const void *data, size_t len); 466 467 468 /* Misc helpers */ 469 470 char *vnc_socket_local_addr(const char *format, int fd); 471 char *vnc_socket_remote_addr(const char *format, int fd); 472 473 static inline uint32_t vnc_has_feature(VncState *vs, int feature) { 474 return (vs->features & (1 << feature)); 475 } 476 477 /* Framebuffer */ 478 void vnc_framebuffer_update(VncState *vs, int x, int y, int w, int h, 479 int32_t encoding); 480 481 void vnc_convert_pixel(VncState *vs, uint8_t *buf, uint32_t v); 482 483 /* Encodings */ 484 int vnc_send_framebuffer_update(VncState *vs, int x, int y, int w, int h); 485 486 int vnc_raw_send_framebuffer_update(VncState *vs, int x, int y, int w, int h); 487 488 int vnc_hextile_send_framebuffer_update(VncState *vs, int x, 489 int y, int w, int h); 490 void vnc_hextile_set_pixel_conversion(VncState *vs, int generic); 491 492 void *vnc_zlib_zalloc(void *x, unsigned items, unsigned size); 493 void vnc_zlib_zfree(void *x, void *addr); 494 int vnc_zlib_send_framebuffer_update(VncState *vs, int x, int y, int w, int h); 495 void vnc_zlib_clear(VncState *vs); 496 497 int vnc_tight_send_framebuffer_update(VncState *vs, int x, int y, int w, int h); 498 int vnc_tight_png_send_framebuffer_update(VncState *vs, int x, int y, 499 int w, int h); 500 void vnc_tight_clear(VncState *vs); 501 502 #endif /* __QEMU_VNC_H */ 503