vnc.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  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. #ifndef __QEMU_VNC_H
  27. #define __QEMU_VNC_H
  28. #include "qemu-common.h"
  29. #include "qemu-queue.h"
  30. #ifdef CONFIG_VNC_THREAD
  31. #include "qemu-thread.h"
  32. #endif
  33. #include "console.h"
  34. #include "monitor.h"
  35. #include "audio/audio.h"
  36. #include "bitmap.h"
  37. #include <zlib.h>
  38. #include <stdbool.h>
  39. #include "keymaps.h"
  40. #include "vnc-palette.h"
  41. #include "vnc-enc-zrle.h"
  42. // #define _VNC_DEBUG 1
  43. #ifdef _VNC_DEBUG
  44. #define VNC_DEBUG(fmt, ...) do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
  45. #else
  46. #define VNC_DEBUG(fmt, ...) do { } while (0)
  47. #endif
  48. /*****************************************************************************
  49. *
  50. * Core data structures
  51. *
  52. *****************************************************************************/
  53. typedef struct Buffer
  54. {
  55. size_t capacity;
  56. size_t offset;
  57. uint8_t *buffer;
  58. } Buffer;
  59. typedef struct VncState VncState;
  60. typedef struct VncJob VncJob;
  61. typedef struct VncRect VncRect;
  62. typedef struct VncRectEntry VncRectEntry;
  63. typedef int VncReadEvent(VncState *vs, uint8_t *data, size_t len);
  64. typedef void VncWritePixels(VncState *vs, struct PixelFormat *pf, void *data, int size);
  65. typedef void VncSendHextileTile(VncState *vs,
  66. int x, int y, int w, int h,
  67. void *last_bg,
  68. void *last_fg,
  69. int *has_bg, int *has_fg);
  70. /* VNC_MAX_WIDTH must be a multiple of 16. */
  71. #define VNC_MAX_WIDTH 2560
  72. #define VNC_MAX_HEIGHT 2048
  73. /* VNC_DIRTY_BITS is the number of bits in the dirty bitmap. */
  74. #define VNC_DIRTY_BITS (VNC_MAX_WIDTH / 16)
  75. #define VNC_STAT_RECT 64
  76. #define VNC_STAT_COLS (VNC_MAX_WIDTH / VNC_STAT_RECT)
  77. #define VNC_STAT_ROWS (VNC_MAX_HEIGHT / VNC_STAT_RECT)
  78. #define VNC_AUTH_CHALLENGE_SIZE 16
  79. typedef struct VncDisplay VncDisplay;
  80. #ifdef CONFIG_VNC_TLS
  81. #include "vnc-tls.h"
  82. #include "vnc-auth-vencrypt.h"
  83. #endif
  84. #ifdef CONFIG_VNC_SASL
  85. #include "vnc-auth-sasl.h"
  86. #endif
  87. struct VncRectStat
  88. {
  89. /* time of last 10 updates, to find update frequency */
  90. struct timeval times[10];
  91. int idx;
  92. double freq; /* Update frequency (in Hz) */
  93. bool updated; /* Already updated during this refresh */
  94. };
  95. typedef struct VncRectStat VncRectStat;
  96. struct VncSurface
  97. {
  98. struct timeval last_freq_check;
  99. DECLARE_BITMAP(dirty[VNC_MAX_HEIGHT], VNC_MAX_WIDTH / 16);
  100. VncRectStat stats[VNC_STAT_ROWS][VNC_STAT_COLS];
  101. DisplaySurface *ds;
  102. };
  103. struct VncDisplay
  104. {
  105. QTAILQ_HEAD(, VncState) clients;
  106. QEMUTimer *timer;
  107. int timer_interval;
  108. int lsock;
  109. DisplayState *ds;
  110. kbd_layout_t *kbd_layout;
  111. int lock_key_sync;
  112. #ifdef CONFIG_VNC_THREAD
  113. QemuMutex mutex;
  114. #endif
  115. QEMUCursor *cursor;
  116. int cursor_msize;
  117. uint8_t *cursor_mask;
  118. struct VncSurface guest; /* guest visible surface (aka ds->surface) */
  119. DisplaySurface *server; /* vnc server surface */
  120. char *display;
  121. char *password;
  122. time_t expires;
  123. int auth;
  124. bool lossy;
  125. bool non_adaptive;
  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. typedef struct VncTight {
  135. int type;
  136. uint8_t quality;
  137. uint8_t compression;
  138. uint8_t pixel24;
  139. Buffer tight;
  140. Buffer tmp;
  141. Buffer zlib;
  142. Buffer gradient;
  143. #ifdef CONFIG_VNC_JPEG
  144. Buffer jpeg;
  145. #endif
  146. #ifdef CONFIG_VNC_PNG
  147. Buffer png;
  148. #endif
  149. int levels[4];
  150. z_stream stream[4];
  151. } VncTight;
  152. typedef struct VncHextile {
  153. VncSendHextileTile *send_tile;
  154. } VncHextile;
  155. typedef struct VncZlib {
  156. Buffer zlib;
  157. Buffer tmp;
  158. z_stream stream;
  159. int level;
  160. } VncZlib;
  161. typedef struct VncZrle {
  162. int type;
  163. Buffer fb;
  164. Buffer zrle;
  165. Buffer tmp;
  166. Buffer zlib;
  167. z_stream stream;
  168. VncPalette palette;
  169. } VncZrle;
  170. typedef struct VncZywrle {
  171. int buf[VNC_ZRLE_TILE_WIDTH * VNC_ZRLE_TILE_HEIGHT];
  172. } VncZywrle;
  173. #ifdef CONFIG_VNC_THREAD
  174. struct VncRect
  175. {
  176. int x;
  177. int y;
  178. int w;
  179. int h;
  180. };
  181. struct VncRectEntry
  182. {
  183. struct VncRect rect;
  184. QLIST_ENTRY(VncRectEntry) next;
  185. };
  186. struct VncJob
  187. {
  188. VncState *vs;
  189. QLIST_HEAD(, VncRectEntry) rectangles;
  190. QTAILQ_ENTRY(VncJob) next;
  191. };
  192. #else
  193. struct VncJob
  194. {
  195. VncState *vs;
  196. int rectangles;
  197. size_t saved_offset;
  198. };
  199. #endif
  200. struct VncState
  201. {
  202. int csock;
  203. DisplayState *ds;
  204. DECLARE_BITMAP(dirty[VNC_MAX_HEIGHT], VNC_DIRTY_BITS);
  205. uint8_t **lossy_rect; /* Not an Array to avoid costly memcpy in
  206. * vnc-jobs-async.c */
  207. VncDisplay *vd;
  208. int need_update;
  209. int force_update;
  210. uint32_t features;
  211. int absolute;
  212. int last_x;
  213. int last_y;
  214. int client_width;
  215. int client_height;
  216. uint32_t vnc_encoding;
  217. int major;
  218. int minor;
  219. int auth;
  220. char challenge[VNC_AUTH_CHALLENGE_SIZE];
  221. #ifdef CONFIG_VNC_TLS
  222. int subauth; /* Used by VeNCrypt */
  223. VncStateTLS tls;
  224. #endif
  225. #ifdef CONFIG_VNC_SASL
  226. VncStateSASL sasl;
  227. #endif
  228. QObject *info;
  229. Buffer output;
  230. Buffer input;
  231. /* current output mode information */
  232. VncWritePixels *write_pixels;
  233. DisplaySurface clientds;
  234. CaptureVoiceOut *audio_cap;
  235. struct audsettings as;
  236. VncReadEvent *read_handler;
  237. size_t read_handler_expect;
  238. /* input */
  239. uint8_t modifiers_state[256];
  240. QEMUPutLEDEntry *led;
  241. bool abort;
  242. #ifndef CONFIG_VNC_THREAD
  243. VncJob job;
  244. #else
  245. QemuMutex output_mutex;
  246. #endif
  247. /* Encoding specific, if you add something here, don't forget to
  248. * update vnc_async_encoding_start()
  249. */
  250. VncTight tight;
  251. VncZlib zlib;
  252. VncHextile hextile;
  253. VncZrle zrle;
  254. VncZywrle zywrle;
  255. Notifier mouse_mode_notifier;
  256. QTAILQ_ENTRY(VncState) next;
  257. };
  258. /*****************************************************************************
  259. *
  260. * Authentication modes
  261. *
  262. *****************************************************************************/
  263. enum {
  264. VNC_AUTH_INVALID = 0,
  265. VNC_AUTH_NONE = 1,
  266. VNC_AUTH_VNC = 2,
  267. VNC_AUTH_RA2 = 5,
  268. VNC_AUTH_RA2NE = 6,
  269. VNC_AUTH_TIGHT = 16,
  270. VNC_AUTH_ULTRA = 17,
  271. VNC_AUTH_TLS = 18, /* Supported in GTK-VNC & VINO */
  272. VNC_AUTH_VENCRYPT = 19, /* Supported in GTK-VNC & VeNCrypt */
  273. VNC_AUTH_SASL = 20, /* Supported in GTK-VNC & VINO */
  274. };
  275. enum {
  276. VNC_AUTH_VENCRYPT_PLAIN = 256,
  277. VNC_AUTH_VENCRYPT_TLSNONE = 257,
  278. VNC_AUTH_VENCRYPT_TLSVNC = 258,
  279. VNC_AUTH_VENCRYPT_TLSPLAIN = 259,
  280. VNC_AUTH_VENCRYPT_X509NONE = 260,
  281. VNC_AUTH_VENCRYPT_X509VNC = 261,
  282. VNC_AUTH_VENCRYPT_X509PLAIN = 262,
  283. VNC_AUTH_VENCRYPT_X509SASL = 263,
  284. VNC_AUTH_VENCRYPT_TLSSASL = 264,
  285. };
  286. /*****************************************************************************
  287. *
  288. * Encoding types
  289. *
  290. *****************************************************************************/
  291. #define VNC_ENCODING_RAW 0x00000000
  292. #define VNC_ENCODING_COPYRECT 0x00000001
  293. #define VNC_ENCODING_RRE 0x00000002
  294. #define VNC_ENCODING_CORRE 0x00000004
  295. #define VNC_ENCODING_HEXTILE 0x00000005
  296. #define VNC_ENCODING_ZLIB 0x00000006
  297. #define VNC_ENCODING_TIGHT 0x00000007
  298. #define VNC_ENCODING_ZLIBHEX 0x00000008
  299. #define VNC_ENCODING_TRLE 0x0000000f
  300. #define VNC_ENCODING_ZRLE 0x00000010
  301. #define VNC_ENCODING_ZYWRLE 0x00000011
  302. #define VNC_ENCODING_COMPRESSLEVEL0 0xFFFFFF00 /* -256 */
  303. #define VNC_ENCODING_QUALITYLEVEL0 0xFFFFFFE0 /* -32 */
  304. #define VNC_ENCODING_XCURSOR 0xFFFFFF10 /* -240 */
  305. #define VNC_ENCODING_RICH_CURSOR 0xFFFFFF11 /* -239 */
  306. #define VNC_ENCODING_POINTER_POS 0xFFFFFF18 /* -232 */
  307. #define VNC_ENCODING_LASTRECT 0xFFFFFF20 /* -224 */
  308. #define VNC_ENCODING_DESKTOPRESIZE 0xFFFFFF21 /* -223 */
  309. #define VNC_ENCODING_POINTER_TYPE_CHANGE 0XFFFFFEFF /* -257 */
  310. #define VNC_ENCODING_EXT_KEY_EVENT 0XFFFFFEFE /* -258 */
  311. #define VNC_ENCODING_AUDIO 0XFFFFFEFD /* -259 */
  312. #define VNC_ENCODING_TIGHT_PNG 0xFFFFFEFC /* -260 */
  313. #define VNC_ENCODING_WMVi 0x574D5669
  314. /*****************************************************************************
  315. *
  316. * Other tight constants
  317. *
  318. *****************************************************************************/
  319. /*
  320. * Vendors known by TightVNC: standard VNC/RealVNC, TridiaVNC, and TightVNC.
  321. */
  322. #define VNC_TIGHT_CCB_RESET_MASK (0x0f)
  323. #define VNC_TIGHT_CCB_TYPE_MASK (0x0f << 4)
  324. #define VNC_TIGHT_CCB_TYPE_FILL (0x08 << 4)
  325. #define VNC_TIGHT_CCB_TYPE_JPEG (0x09 << 4)
  326. #define VNC_TIGHT_CCB_TYPE_PNG (0x0A << 4)
  327. #define VNC_TIGHT_CCB_BASIC_MAX (0x07 << 4)
  328. #define VNC_TIGHT_CCB_BASIC_ZLIB (0x03 << 4)
  329. #define VNC_TIGHT_CCB_BASIC_FILTER (0x04 << 4)
  330. /*****************************************************************************
  331. *
  332. * Features
  333. *
  334. *****************************************************************************/
  335. #define VNC_FEATURE_RESIZE 0
  336. #define VNC_FEATURE_HEXTILE 1
  337. #define VNC_FEATURE_POINTER_TYPE_CHANGE 2
  338. #define VNC_FEATURE_WMVI 3
  339. #define VNC_FEATURE_TIGHT 4
  340. #define VNC_FEATURE_ZLIB 5
  341. #define VNC_FEATURE_COPYRECT 6
  342. #define VNC_FEATURE_RICH_CURSOR 7
  343. #define VNC_FEATURE_TIGHT_PNG 8
  344. #define VNC_FEATURE_ZRLE 9
  345. #define VNC_FEATURE_ZYWRLE 10
  346. #define VNC_FEATURE_RESIZE_MASK (1 << VNC_FEATURE_RESIZE)
  347. #define VNC_FEATURE_HEXTILE_MASK (1 << VNC_FEATURE_HEXTILE)
  348. #define VNC_FEATURE_POINTER_TYPE_CHANGE_MASK (1 << VNC_FEATURE_POINTER_TYPE_CHANGE)
  349. #define VNC_FEATURE_WMVI_MASK (1 << VNC_FEATURE_WMVI)
  350. #define VNC_FEATURE_TIGHT_MASK (1 << VNC_FEATURE_TIGHT)
  351. #define VNC_FEATURE_ZLIB_MASK (1 << VNC_FEATURE_ZLIB)
  352. #define VNC_FEATURE_COPYRECT_MASK (1 << VNC_FEATURE_COPYRECT)
  353. #define VNC_FEATURE_RICH_CURSOR_MASK (1 << VNC_FEATURE_RICH_CURSOR)
  354. #define VNC_FEATURE_TIGHT_PNG_MASK (1 << VNC_FEATURE_TIGHT_PNG)
  355. #define VNC_FEATURE_ZRLE_MASK (1 << VNC_FEATURE_ZRLE)
  356. #define VNC_FEATURE_ZYWRLE_MASK (1 << VNC_FEATURE_ZYWRLE)
  357. /* Client -> Server message IDs */
  358. #define VNC_MSG_CLIENT_SET_PIXEL_FORMAT 0
  359. #define VNC_MSG_CLIENT_SET_ENCODINGS 2
  360. #define VNC_MSG_CLIENT_FRAMEBUFFER_UPDATE_REQUEST 3
  361. #define VNC_MSG_CLIENT_KEY_EVENT 4
  362. #define VNC_MSG_CLIENT_POINTER_EVENT 5
  363. #define VNC_MSG_CLIENT_CUT_TEXT 6
  364. #define VNC_MSG_CLIENT_VMWARE_0 127
  365. #define VNC_MSG_CLIENT_CALL_CONTROL 249
  366. #define VNC_MSG_CLIENT_XVP 250
  367. #define VNC_MSG_CLIENT_SET_DESKTOP_SIZE 251
  368. #define VNC_MSG_CLIENT_TIGHT 252
  369. #define VNC_MSG_CLIENT_GII 253
  370. #define VNC_MSG_CLIENT_VMWARE_1 254
  371. #define VNC_MSG_CLIENT_QEMU 255
  372. /* Server -> Client message IDs */
  373. #define VNC_MSG_SERVER_FRAMEBUFFER_UPDATE 0
  374. #define VNC_MSG_SERVER_SET_COLOUR_MAP_ENTRIES 1
  375. #define VNC_MSG_SERVER_BELL 2
  376. #define VNC_MSG_SERVER_CUT_TEXT 3
  377. #define VNC_MSG_SERVER_VMWARE_0 127
  378. #define VNC_MSG_SERVER_CALL_CONTROL 249
  379. #define VNC_MSG_SERVER_XVP 250
  380. #define VNC_MSG_SERVER_TIGHT 252
  381. #define VNC_MSG_SERVER_GII 253
  382. #define VNC_MSG_SERVER_VMWARE_1 254
  383. #define VNC_MSG_SERVER_QEMU 255
  384. /* QEMU client -> server message IDs */
  385. #define VNC_MSG_CLIENT_QEMU_EXT_KEY_EVENT 0
  386. #define VNC_MSG_CLIENT_QEMU_AUDIO 1
  387. /* QEMU server -> client message IDs */
  388. #define VNC_MSG_SERVER_QEMU_AUDIO 1
  389. /* QEMU client -> server audio message IDs */
  390. #define VNC_MSG_CLIENT_QEMU_AUDIO_ENABLE 0
  391. #define VNC_MSG_CLIENT_QEMU_AUDIO_DISABLE 1
  392. #define VNC_MSG_CLIENT_QEMU_AUDIO_SET_FORMAT 2
  393. /* QEMU server -> client audio message IDs */
  394. #define VNC_MSG_SERVER_QEMU_AUDIO_END 0
  395. #define VNC_MSG_SERVER_QEMU_AUDIO_BEGIN 1
  396. #define VNC_MSG_SERVER_QEMU_AUDIO_DATA 2
  397. /*****************************************************************************
  398. *
  399. * Internal APIs
  400. *
  401. *****************************************************************************/
  402. /* Event loop functions */
  403. void vnc_client_read(void *opaque);
  404. void vnc_client_write(void *opaque);
  405. long vnc_client_read_buf(VncState *vs, uint8_t *data, size_t datalen);
  406. long vnc_client_write_buf(VncState *vs, const uint8_t *data, size_t datalen);
  407. /* Protocol I/O functions */
  408. void vnc_write(VncState *vs, const void *data, size_t len);
  409. void vnc_write_u32(VncState *vs, uint32_t value);
  410. void vnc_write_s32(VncState *vs, int32_t value);
  411. void vnc_write_u16(VncState *vs, uint16_t value);
  412. void vnc_write_u8(VncState *vs, uint8_t value);
  413. void vnc_flush(VncState *vs);
  414. void vnc_read_when(VncState *vs, VncReadEvent *func, size_t expecting);
  415. /* Buffer I/O functions */
  416. uint8_t read_u8(uint8_t *data, size_t offset);
  417. uint16_t read_u16(uint8_t *data, size_t offset);
  418. int32_t read_s32(uint8_t *data, size_t offset);
  419. uint32_t read_u32(uint8_t *data, size_t offset);
  420. /* Protocol stage functions */
  421. void vnc_client_error(VncState *vs);
  422. int vnc_client_io_error(VncState *vs, int ret, int last_errno);
  423. void start_client_init(VncState *vs);
  424. void start_auth_vnc(VncState *vs);
  425. /* Buffer management */
  426. void buffer_reserve(Buffer *buffer, size_t len);
  427. int buffer_empty(Buffer *buffer);
  428. uint8_t *buffer_end(Buffer *buffer);
  429. void buffer_reset(Buffer *buffer);
  430. void buffer_free(Buffer *buffer);
  431. void buffer_append(Buffer *buffer, const void *data, size_t len);
  432. /* Misc helpers */
  433. char *vnc_socket_local_addr(const char *format, int fd);
  434. char *vnc_socket_remote_addr(const char *format, int fd);
  435. static inline uint32_t vnc_has_feature(VncState *vs, int feature) {
  436. return (vs->features & (1 << feature));
  437. }
  438. /* Framebuffer */
  439. void vnc_framebuffer_update(VncState *vs, int x, int y, int w, int h,
  440. int32_t encoding);
  441. void vnc_convert_pixel(VncState *vs, uint8_t *buf, uint32_t v);
  442. double vnc_update_freq(VncState *vs, int x, int y, int w, int h);
  443. void vnc_sent_lossy_rect(VncState *vs, int x, int y, int w, int h);
  444. /* Encodings */
  445. int vnc_send_framebuffer_update(VncState *vs, int x, int y, int w, int h);
  446. int vnc_raw_send_framebuffer_update(VncState *vs, int x, int y, int w, int h);
  447. int vnc_hextile_send_framebuffer_update(VncState *vs, int x,
  448. int y, int w, int h);
  449. void vnc_hextile_set_pixel_conversion(VncState *vs, int generic);
  450. void *vnc_zlib_zalloc(void *x, unsigned items, unsigned size);
  451. void vnc_zlib_zfree(void *x, void *addr);
  452. int vnc_zlib_send_framebuffer_update(VncState *vs, int x, int y, int w, int h);
  453. void vnc_zlib_clear(VncState *vs);
  454. int vnc_tight_send_framebuffer_update(VncState *vs, int x, int y, int w, int h);
  455. int vnc_tight_png_send_framebuffer_update(VncState *vs, int x, int y,
  456. int w, int h);
  457. void vnc_tight_clear(VncState *vs);
  458. int vnc_zrle_send_framebuffer_update(VncState *vs, int x, int y, int w, int h);
  459. int vnc_zywrle_send_framebuffer_update(VncState *vs, int x, int y, int w, int h);
  460. void vnc_zrle_clear(VncState *vs);
  461. #endif /* __QEMU_VNC_H */