2 * QEMU VNC display driver
4 * Copyright (C) 2006 Anthony Liguori <anthony@codemonkey.ws>
5 * Copyright (C) 2006 Fabrice Bellard
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 #include "qemu_socket.h"
29 #define VNC_REFRESH_INTERVAL (1000 / 30)
31 #include "vnc_keysym.h"
36 #include <gnutls/gnutls.h>
37 #include <gnutls/x509.h>
38 #endif /* CONFIG_VNC_TLS */
40 // #define _VNC_DEBUG 1
43 #define VNC_DEBUG(fmt, ...) do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
45 #if CONFIG_VNC_TLS && _VNC_DEBUG >= 2
46 /* Very verbose, so only enabled for _VNC_DEBUG >= 2 */
47 static void vnc_debug_gnutls_log(int level, const char* str) {
48 VNC_DEBUG("%d %s", level, str);
50 #endif /* CONFIG_VNC_TLS && _VNC_DEBUG */
52 #define VNC_DEBUG(fmt, ...) do { } while (0)
63 typedef struct VncState VncState;
65 typedef int VncReadEvent(VncState *vs, char *data, size_t len);
67 typedef void VncWritePixels(VncState *vs, void *data, int size);
69 typedef void VncSendHextileTile(VncState *vs,
70 int x, int y, int w, int h,
73 int *has_bg, int *has_fg);
75 #define VNC_MAX_WIDTH 2048
76 #define VNC_MAX_HEIGHT 2048
77 #define VNC_DIRTY_WORDS (VNC_MAX_WIDTH / (16 * 32))
79 #define VNC_AUTH_CHALLENGE_SIZE 16
90 VNC_AUTH_VENCRYPT = 19
100 VNC_AUTH_VENCRYPT_PLAIN = 256,
101 VNC_AUTH_VENCRYPT_TLSNONE = 257,
102 VNC_AUTH_VENCRYPT_TLSVNC = 258,
103 VNC_AUTH_VENCRYPT_TLSPLAIN = 259,
104 VNC_AUTH_VENCRYPT_X509NONE = 260,
105 VNC_AUTH_VENCRYPT_X509VNC = 261,
106 VNC_AUTH_VENCRYPT_X509PLAIN = 262,
110 #define X509_CA_CERT_FILE "ca-cert.pem"
111 #define X509_CA_CRL_FILE "ca-crl.pem"
112 #define X509_SERVER_KEY_FILE "server-key.pem"
113 #define X509_SERVER_CERT_FILE "server-cert.pem"
116 #endif /* CONFIG_VNC_TLS */
127 uint32_t dirty_row[VNC_MAX_HEIGHT][VNC_DIRTY_WORDS];
129 int depth; /* internal VNC frame buffer byte per pixel */
132 int has_pointer_type_change;
146 char challenge[VNC_AUTH_CHALLENGE_SIZE];
150 gnutls_session_t tls_session;
155 kbd_layout_t *kbd_layout;
156 /* current output mode information */
157 VncWritePixels *write_pixels;
158 VncSendHextileTile *send_hextile_tile;
159 int pix_bpp, pix_big_endian;
160 int red_shift, red_max, red_shift1;
161 int green_shift, green_max, green_shift1;
162 int blue_shift, blue_max, blue_shift1;
164 VncReadEvent *read_handler;
165 size_t read_handler_expect;
167 uint8_t modifiers_state[256];
170 static VncState *vnc_state; /* needed for info vnc */
172 void do_info_vnc(void)
174 if (vnc_state == NULL)
175 term_printf("VNC server disabled\n");
177 term_printf("VNC server active on: ");
178 term_print_filename(vnc_state->display);
181 if (vnc_state->csock == -1)
182 term_printf("No client connected\n");
184 term_printf("Client connected\n");
189 1) Get the queue working for IO.
190 2) there is some weirdness when using the -S option (the screen is grey
191 and not totally invalidated
192 3) resolutions > 1024
195 static void vnc_write(VncState *vs, const void *data, size_t len);
196 static void vnc_write_u32(VncState *vs, uint32_t value);
197 static void vnc_write_s32(VncState *vs, int32_t value);
198 static void vnc_write_u16(VncState *vs, uint16_t value);
199 static void vnc_write_u8(VncState *vs, uint8_t value);
200 static void vnc_flush(VncState *vs);
201 static void vnc_update_client(void *opaque);
202 static void vnc_client_read(void *opaque);
204 static inline void vnc_set_bit(uint32_t *d, int k)
206 d[k >> 5] |= 1 << (k & 0x1f);
209 static inline void vnc_clear_bit(uint32_t *d, int k)
211 d[k >> 5] &= ~(1 << (k & 0x1f));
214 static inline void vnc_set_bits(uint32_t *d, int n, int nb_words)
224 d[j++] = (1 << n) - 1;
229 static inline int vnc_get_bit(const uint32_t *d, int k)
231 return (d[k >> 5] >> (k & 0x1f)) & 1;
234 static inline int vnc_and_bits(const uint32_t *d1, const uint32_t *d2,
238 for(i = 0; i < nb_words; i++) {
239 if ((d1[i] & d2[i]) != 0)
245 static void vnc_dpy_update(DisplayState *ds, int x, int y, int w, int h)
247 VncState *vs = ds->opaque;
253 for (i = 0; i < w; i += 16)
254 vnc_set_bit(vs->dirty_row[y], (x + i) / 16);
257 static void vnc_framebuffer_update(VncState *vs, int x, int y, int w, int h,
260 vnc_write_u16(vs, x);
261 vnc_write_u16(vs, y);
262 vnc_write_u16(vs, w);
263 vnc_write_u16(vs, h);
265 vnc_write_s32(vs, encoding);
268 static void vnc_dpy_resize(DisplayState *ds, int w, int h)
271 VncState *vs = ds->opaque;
273 ds->data = realloc(ds->data, w * h * vs->depth);
274 vs->old_data = realloc(vs->old_data, w * h * vs->depth);
276 if (ds->data == NULL || vs->old_data == NULL) {
277 fprintf(stderr, "vnc: memory allocation failed\n");
281 ds->depth = vs->depth * 8;
282 size_changed = ds->width != w || ds->height != h;
285 ds->linesize = w * vs->depth;
286 if (vs->csock != -1 && vs->has_resize && size_changed) {
287 vnc_write_u8(vs, 0); /* msg id */
289 vnc_write_u16(vs, 1); /* number of rects */
290 vnc_framebuffer_update(vs, 0, 0, ds->width, ds->height, -223);
292 vs->width = ds->width;
293 vs->height = ds->height;
298 static void vnc_write_pixels_copy(VncState *vs, void *pixels, int size)
300 vnc_write(vs, pixels, size);
303 /* slowest but generic code. */
304 static void vnc_convert_pixel(VncState *vs, uint8_t *buf, uint32_t v)
306 unsigned int r, g, b;
308 r = (v >> vs->red_shift1) & vs->red_max;
309 g = (v >> vs->green_shift1) & vs->green_max;
310 b = (v >> vs->blue_shift1) & vs->blue_max;
311 v = (r << vs->red_shift) |
312 (g << vs->green_shift) |
313 (b << vs->blue_shift);
314 switch(vs->pix_bpp) {
319 if (vs->pix_big_endian) {
329 if (vs->pix_big_endian) {
344 static void vnc_write_pixels_generic(VncState *vs, void *pixels1, int size)
346 uint32_t *pixels = pixels1;
351 for(i = 0; i < n; i++) {
352 vnc_convert_pixel(vs, buf, pixels[i]);
353 vnc_write(vs, buf, vs->pix_bpp);
357 static void send_framebuffer_update_raw(VncState *vs, int x, int y, int w, int h)
362 vnc_framebuffer_update(vs, x, y, w, h, 0);
364 row = vs->ds->data + y * vs->ds->linesize + x * vs->depth;
365 for (i = 0; i < h; i++) {
366 vs->write_pixels(vs, row, w * vs->depth);
367 row += vs->ds->linesize;
371 static void hextile_enc_cord(uint8_t *ptr, int x, int y, int w, int h)
373 ptr[0] = ((x & 0x0F) << 4) | (y & 0x0F);
374 ptr[1] = (((w - 1) & 0x0F) << 4) | ((h - 1) & 0x0F);
378 #include "vnchextile.h"
382 #include "vnchextile.h"
386 #include "vnchextile.h"
391 #include "vnchextile.h"
395 static void send_framebuffer_update_hextile(VncState *vs, int x, int y, int w, int h)
399 uint32_t last_fg32, last_bg32;
401 vnc_framebuffer_update(vs, x, y, w, h, 5);
404 for (j = y; j < (y + h); j += 16) {
405 for (i = x; i < (x + w); i += 16) {
406 vs->send_hextile_tile(vs, i, j,
407 MIN(16, x + w - i), MIN(16, y + h - j),
408 &last_bg32, &last_fg32, &has_bg, &has_fg);
413 static void send_framebuffer_update(VncState *vs, int x, int y, int w, int h)
416 send_framebuffer_update_hextile(vs, x, y, w, h);
418 send_framebuffer_update_raw(vs, x, y, w, h);
421 static void vnc_copy(DisplayState *ds, int src_x, int src_y, int dst_x, int dst_y, int w, int h)
428 int pitch = ds->linesize;
429 VncState *vs = ds->opaque;
431 vnc_update_client(vs);
438 src = (ds->linesize * (src_y + y) + vs->depth * src_x);
439 dst = (ds->linesize * (dst_y + y) + vs->depth * dst_x);
441 src_row = ds->data + src;
442 dst_row = ds->data + dst;
443 old_row = vs->old_data + dst;
445 for (y = 0; y < h; y++) {
446 memmove(old_row, src_row, w * vs->depth);
447 memmove(dst_row, src_row, w * vs->depth);
453 vnc_write_u8(vs, 0); /* msg id */
455 vnc_write_u16(vs, 1); /* number of rects */
456 vnc_framebuffer_update(vs, dst_x, dst_y, w, h, 1);
457 vnc_write_u16(vs, src_x);
458 vnc_write_u16(vs, src_y);
462 static int find_dirty_height(VncState *vs, int y, int last_x, int x)
466 for (h = 1; h < (vs->height - y); h++) {
468 if (!vnc_get_bit(vs->dirty_row[y + h], last_x))
470 for (tmp_x = last_x; tmp_x < x; tmp_x++)
471 vnc_clear_bit(vs->dirty_row[y + h], tmp_x);
477 static void vnc_update_client(void *opaque)
479 VncState *vs = opaque;
481 if (vs->need_update && vs->csock != -1) {
485 uint32_t width_mask[VNC_DIRTY_WORDS];
490 vnc_set_bits(width_mask, (vs->width / 16), VNC_DIRTY_WORDS);
492 /* Walk through the dirty map and eliminate tiles that
493 really aren't dirty */
495 old_row = vs->old_data;
497 for (y = 0; y < vs->height; y++) {
498 if (vnc_and_bits(vs->dirty_row[y], width_mask, VNC_DIRTY_WORDS)) {
505 for (x = 0; x < vs->ds->width; x += 16) {
506 if (memcmp(old_ptr, ptr, 16 * vs->depth) == 0) {
507 vnc_clear_bit(vs->dirty_row[y], (x / 16));
510 memcpy(old_ptr, ptr, 16 * vs->depth);
513 ptr += 16 * vs->depth;
514 old_ptr += 16 * vs->depth;
518 row += vs->ds->linesize;
519 old_row += vs->ds->linesize;
523 qemu_mod_timer(vs->timer, qemu_get_clock(rt_clock) + VNC_REFRESH_INTERVAL);
527 /* Count rectangles */
529 vnc_write_u8(vs, 0); /* msg id */
531 saved_offset = vs->output.offset;
532 vnc_write_u16(vs, 0);
534 for (y = 0; y < vs->height; y++) {
537 for (x = 0; x < vs->width / 16; x++) {
538 if (vnc_get_bit(vs->dirty_row[y], x)) {
542 vnc_clear_bit(vs->dirty_row[y], x);
545 int h = find_dirty_height(vs, y, last_x, x);
546 send_framebuffer_update(vs, last_x * 16, y, (x - last_x) * 16, h);
553 int h = find_dirty_height(vs, y, last_x, x);
554 send_framebuffer_update(vs, last_x * 16, y, (x - last_x) * 16, h);
558 vs->output.buffer[saved_offset] = (n_rectangles >> 8) & 0xFF;
559 vs->output.buffer[saved_offset + 1] = n_rectangles & 0xFF;
563 qemu_mod_timer(vs->timer, qemu_get_clock(rt_clock) + VNC_REFRESH_INTERVAL);
566 static void vnc_timer_init(VncState *vs)
568 if (vs->timer == NULL) {
569 vs->timer = qemu_new_timer(rt_clock, vnc_update_client, vs);
570 qemu_mod_timer(vs->timer, qemu_get_clock(rt_clock));
574 static void vnc_dpy_refresh(DisplayState *ds)
576 VncState *vs = ds->opaque;
581 static int vnc_listen_poll(void *opaque)
583 VncState *vs = opaque;
589 static void buffer_reserve(Buffer *buffer, size_t len)
591 if ((buffer->capacity - buffer->offset) < len) {
592 buffer->capacity += (len + 1024);
593 buffer->buffer = realloc(buffer->buffer, buffer->capacity);
594 if (buffer->buffer == NULL) {
595 fprintf(stderr, "vnc: out of memory\n");
601 static int buffer_empty(Buffer *buffer)
603 return buffer->offset == 0;
606 static char *buffer_end(Buffer *buffer)
608 return buffer->buffer + buffer->offset;
611 static void buffer_reset(Buffer *buffer)
616 static void buffer_append(Buffer *buffer, const void *data, size_t len)
618 memcpy(buffer->buffer + buffer->offset, data, len);
619 buffer->offset += len;
622 static int vnc_client_io_error(VncState *vs, int ret, int last_errno)
624 if (ret == 0 || ret == -1) {
625 if (ret == -1 && (last_errno == EINTR || last_errno == EAGAIN))
628 VNC_DEBUG("Closing down client sock %d %d\n", ret, ret < 0 ? last_errno : 0);
629 qemu_set_fd_handler2(vs->csock, NULL, NULL, NULL, NULL);
630 closesocket(vs->csock);
632 buffer_reset(&vs->input);
633 buffer_reset(&vs->output);
636 if (vs->tls_session) {
637 gnutls_deinit(vs->tls_session);
638 vs->tls_session = NULL;
640 vs->wiremode = VNC_WIREMODE_CLEAR;
641 #endif /* CONFIG_VNC_TLS */
647 static void vnc_client_error(VncState *vs)
649 vnc_client_io_error(vs, -1, EINVAL);
652 static void vnc_client_write(void *opaque)
655 VncState *vs = opaque;
658 if (vs->tls_session) {
659 ret = gnutls_write(vs->tls_session, vs->output.buffer, vs->output.offset);
661 if (ret == GNUTLS_E_AGAIN)
668 #endif /* CONFIG_VNC_TLS */
669 ret = send(vs->csock, vs->output.buffer, vs->output.offset, 0);
670 ret = vnc_client_io_error(vs, ret, socket_error());
674 memmove(vs->output.buffer, vs->output.buffer + ret, (vs->output.offset - ret));
675 vs->output.offset -= ret;
677 if (vs->output.offset == 0) {
678 qemu_set_fd_handler2(vs->csock, NULL, vnc_client_read, NULL, vs);
682 static void vnc_read_when(VncState *vs, VncReadEvent *func, size_t expecting)
684 vs->read_handler = func;
685 vs->read_handler_expect = expecting;
688 static void vnc_client_read(void *opaque)
690 VncState *vs = opaque;
693 buffer_reserve(&vs->input, 4096);
696 if (vs->tls_session) {
697 ret = gnutls_read(vs->tls_session, buffer_end(&vs->input), 4096);
699 if (ret == GNUTLS_E_AGAIN)
706 #endif /* CONFIG_VNC_TLS */
707 ret = recv(vs->csock, buffer_end(&vs->input), 4096, 0);
708 ret = vnc_client_io_error(vs, ret, socket_error());
712 vs->input.offset += ret;
714 while (vs->read_handler && vs->input.offset >= vs->read_handler_expect) {
715 size_t len = vs->read_handler_expect;
718 ret = vs->read_handler(vs, vs->input.buffer, len);
723 memmove(vs->input.buffer, vs->input.buffer + len, (vs->input.offset - len));
724 vs->input.offset -= len;
726 vs->read_handler_expect = ret;
731 static void vnc_write(VncState *vs, const void *data, size_t len)
733 buffer_reserve(&vs->output, len);
735 if (buffer_empty(&vs->output)) {
736 qemu_set_fd_handler2(vs->csock, NULL, vnc_client_read, vnc_client_write, vs);
739 buffer_append(&vs->output, data, len);
742 static void vnc_write_s32(VncState *vs, int32_t value)
744 vnc_write_u32(vs, *(uint32_t *)&value);
747 static void vnc_write_u32(VncState *vs, uint32_t value)
751 buf[0] = (value >> 24) & 0xFF;
752 buf[1] = (value >> 16) & 0xFF;
753 buf[2] = (value >> 8) & 0xFF;
754 buf[3] = value & 0xFF;
756 vnc_write(vs, buf, 4);
759 static void vnc_write_u16(VncState *vs, uint16_t value)
763 buf[0] = (value >> 8) & 0xFF;
764 buf[1] = value & 0xFF;
766 vnc_write(vs, buf, 2);
769 static void vnc_write_u8(VncState *vs, uint8_t value)
771 vnc_write(vs, (char *)&value, 1);
774 static void vnc_flush(VncState *vs)
776 if (vs->output.offset)
777 vnc_client_write(vs);
780 static uint8_t read_u8(uint8_t *data, size_t offset)
785 static uint16_t read_u16(uint8_t *data, size_t offset)
787 return ((data[offset] & 0xFF) << 8) | (data[offset + 1] & 0xFF);
790 static int32_t read_s32(uint8_t *data, size_t offset)
792 return (int32_t)((data[offset] << 24) | (data[offset + 1] << 16) |
793 (data[offset + 2] << 8) | data[offset + 3]);
796 static uint32_t read_u32(uint8_t *data, size_t offset)
798 return ((data[offset] << 24) | (data[offset + 1] << 16) |
799 (data[offset + 2] << 8) | data[offset + 3]);
803 ssize_t vnc_tls_push(gnutls_transport_ptr_t transport,
806 struct VncState *vs = (struct VncState *)transport;
810 ret = send(vs->csock, data, len, 0);
820 ssize_t vnc_tls_pull(gnutls_transport_ptr_t transport,
823 struct VncState *vs = (struct VncState *)transport;
827 ret = recv(vs->csock, data, len, 0);
835 #endif /* CONFIG_VNC_TLS */
837 static void client_cut_text(VncState *vs, size_t len, char *text)
841 static void check_pointer_type_change(VncState *vs, int absolute)
843 if (vs->has_pointer_type_change && vs->absolute != absolute) {
846 vnc_write_u16(vs, 1);
847 vnc_framebuffer_update(vs, absolute, 0,
848 vs->ds->width, vs->ds->height, -257);
851 vs->absolute = absolute;
854 static void pointer_event(VncState *vs, int button_mask, int x, int y)
859 if (button_mask & 0x01)
860 buttons |= MOUSE_EVENT_LBUTTON;
861 if (button_mask & 0x02)
862 buttons |= MOUSE_EVENT_MBUTTON;
863 if (button_mask & 0x04)
864 buttons |= MOUSE_EVENT_RBUTTON;
865 if (button_mask & 0x08)
867 if (button_mask & 0x10)
871 kbd_mouse_event(x * 0x7FFF / vs->ds->width,
872 y * 0x7FFF / vs->ds->height,
874 } else if (vs->has_pointer_type_change) {
878 kbd_mouse_event(x, y, dz, buttons);
880 if (vs->last_x != -1)
881 kbd_mouse_event(x - vs->last_x,
888 check_pointer_type_change(vs, kbd_mouse_is_absolute());
891 static void reset_keys(VncState *vs)
894 for(i = 0; i < 256; i++) {
895 if (vs->modifiers_state[i]) {
897 kbd_put_keycode(0xe0);
898 kbd_put_keycode(i | 0x80);
899 vs->modifiers_state[i] = 0;
904 static void do_key_event(VncState *vs, int down, uint32_t sym)
908 keycode = keysym2scancode(vs->kbd_layout, sym & 0xFFFF);
910 /* QEMU console switch */
912 case 0x2a: /* Left Shift */
913 case 0x36: /* Right Shift */
914 case 0x1d: /* Left CTRL */
915 case 0x9d: /* Right CTRL */
916 case 0x38: /* Left ALT */
917 case 0xb8: /* Right ALT */
919 vs->modifiers_state[keycode] = 1;
921 vs->modifiers_state[keycode] = 0;
923 case 0x02 ... 0x0a: /* '1' to '9' keys */
924 if (down && vs->modifiers_state[0x1d] && vs->modifiers_state[0x38]) {
925 /* Reset the modifiers sent to the current console */
927 console_select(keycode - 0x02);
933 if (is_graphic_console()) {
935 kbd_put_keycode(0xe0);
937 kbd_put_keycode(keycode & 0x7f);
939 kbd_put_keycode(keycode | 0x80);
941 /* QEMU console emulation */
944 case 0x2a: /* Left Shift */
945 case 0x36: /* Right Shift */
946 case 0x1d: /* Left CTRL */
947 case 0x9d: /* Right CTRL */
948 case 0x38: /* Left ALT */
949 case 0xb8: /* Right ALT */
952 kbd_put_keysym(QEMU_KEY_UP);
955 kbd_put_keysym(QEMU_KEY_DOWN);
958 kbd_put_keysym(QEMU_KEY_LEFT);
961 kbd_put_keysym(QEMU_KEY_RIGHT);
964 kbd_put_keysym(QEMU_KEY_DELETE);
967 kbd_put_keysym(QEMU_KEY_HOME);
970 kbd_put_keysym(QEMU_KEY_END);
973 kbd_put_keysym(QEMU_KEY_PAGEUP);
976 kbd_put_keysym(QEMU_KEY_PAGEDOWN);
986 static void key_event(VncState *vs, int down, uint32_t sym)
988 if (sym >= 'A' && sym <= 'Z')
989 sym = sym - 'A' + 'a';
990 do_key_event(vs, down, sym);
993 static void framebuffer_update_request(VncState *vs, int incremental,
994 int x_position, int y_position,
997 if (x_position > vs->ds->width)
998 x_position = vs->ds->width;
999 if (y_position > vs->ds->height)
1000 y_position = vs->ds->height;
1001 if (x_position + w >= vs->ds->width)
1002 w = vs->ds->width - x_position;
1003 if (y_position + h >= vs->ds->height)
1004 h = vs->ds->height - y_position;
1007 vs->need_update = 1;
1009 char *old_row = vs->old_data + y_position * vs->ds->linesize;
1011 for (i = 0; i < h; i++) {
1012 vnc_set_bits(vs->dirty_row[y_position + i],
1013 (vs->ds->width / 16), VNC_DIRTY_WORDS);
1014 memset(old_row, 42, vs->ds->width * vs->depth);
1015 old_row += vs->ds->linesize;
1020 static void set_encodings(VncState *vs, int32_t *encodings, size_t n_encodings)
1024 vs->has_hextile = 0;
1026 vs->has_pointer_type_change = 0;
1028 vs->ds->dpy_copy = NULL;
1030 for (i = n_encodings - 1; i >= 0; i--) {
1031 switch (encodings[i]) {
1033 vs->has_hextile = 0;
1035 case 1: /* CopyRect */
1036 vs->ds->dpy_copy = vnc_copy;
1038 case 5: /* Hextile */
1039 vs->has_hextile = 1;
1041 case -223: /* DesktopResize */
1045 vs->has_pointer_type_change = 1;
1052 check_pointer_type_change(vs, kbd_mouse_is_absolute());
1055 static int compute_nbits(unsigned int val)
1066 static void set_pixel_format(VncState *vs,
1067 int bits_per_pixel, int depth,
1068 int big_endian_flag, int true_color_flag,
1069 int red_max, int green_max, int blue_max,
1070 int red_shift, int green_shift, int blue_shift)
1072 int host_big_endian_flag;
1074 #ifdef WORDS_BIGENDIAN
1075 host_big_endian_flag = 1;
1077 host_big_endian_flag = 0;
1079 if (!true_color_flag) {
1081 vnc_client_error(vs);
1084 if (bits_per_pixel == 32 &&
1085 host_big_endian_flag == big_endian_flag &&
1086 red_max == 0xff && green_max == 0xff && blue_max == 0xff &&
1087 red_shift == 16 && green_shift == 8 && blue_shift == 0) {
1089 vs->write_pixels = vnc_write_pixels_copy;
1090 vs->send_hextile_tile = send_hextile_tile_32;
1092 if (bits_per_pixel == 16 &&
1093 host_big_endian_flag == big_endian_flag &&
1094 red_max == 31 && green_max == 63 && blue_max == 31 &&
1095 red_shift == 11 && green_shift == 5 && blue_shift == 0) {
1097 vs->write_pixels = vnc_write_pixels_copy;
1098 vs->send_hextile_tile = send_hextile_tile_16;
1100 if (bits_per_pixel == 8 &&
1101 red_max == 7 && green_max == 7 && blue_max == 3 &&
1102 red_shift == 5 && green_shift == 2 && blue_shift == 0) {
1104 vs->write_pixels = vnc_write_pixels_copy;
1105 vs->send_hextile_tile = send_hextile_tile_8;
1108 /* generic and slower case */
1109 if (bits_per_pixel != 8 &&
1110 bits_per_pixel != 16 &&
1111 bits_per_pixel != 32)
1114 vs->red_shift = red_shift;
1115 vs->red_max = red_max;
1116 vs->red_shift1 = 24 - compute_nbits(red_max);
1117 vs->green_shift = green_shift;
1118 vs->green_max = green_max;
1119 vs->green_shift1 = 16 - compute_nbits(green_max);
1120 vs->blue_shift = blue_shift;
1121 vs->blue_max = blue_max;
1122 vs->blue_shift1 = 8 - compute_nbits(blue_max);
1123 vs->pix_bpp = bits_per_pixel / 8;
1124 vs->pix_big_endian = big_endian_flag;
1125 vs->write_pixels = vnc_write_pixels_generic;
1126 vs->send_hextile_tile = send_hextile_tile_generic;
1129 vnc_dpy_resize(vs->ds, vs->ds->width, vs->ds->height);
1130 memset(vs->dirty_row, 0xFF, sizeof(vs->dirty_row));
1131 memset(vs->old_data, 42, vs->ds->linesize * vs->ds->height);
1133 vga_hw_invalidate();
1137 static int protocol_client_msg(VncState *vs, char *data, size_t len)
1147 set_pixel_format(vs, read_u8(data, 4), read_u8(data, 5),
1148 read_u8(data, 6), read_u8(data, 7),
1149 read_u16(data, 8), read_u16(data, 10),
1150 read_u16(data, 12), read_u8(data, 14),
1151 read_u8(data, 15), read_u8(data, 16));
1158 return 4 + (read_u16(data, 2) * 4);
1160 limit = read_u16(data, 2);
1161 for (i = 0; i < limit; i++) {
1162 int32_t val = read_s32(data, 4 + (i * 4));
1163 memcpy(data + 4 + (i * 4), &val, sizeof(val));
1166 set_encodings(vs, (int32_t *)(data + 4), limit);
1172 framebuffer_update_request(vs,
1173 read_u8(data, 1), read_u16(data, 2), read_u16(data, 4),
1174 read_u16(data, 6), read_u16(data, 8));
1180 key_event(vs, read_u8(data, 1), read_u32(data, 4));
1186 pointer_event(vs, read_u8(data, 1), read_u16(data, 2), read_u16(data, 4));
1193 return 8 + read_u32(data, 4);
1195 client_cut_text(vs, read_u32(data, 4), data + 8);
1198 printf("Msg: %d\n", data[0]);
1199 vnc_client_error(vs);
1203 vnc_read_when(vs, protocol_client_msg, 1);
1207 static int protocol_client_init(VncState *vs, char *data, size_t len)
1209 char pad[3] = { 0, 0, 0 };
1213 vs->width = vs->ds->width;
1214 vs->height = vs->ds->height;
1215 vnc_write_u16(vs, vs->ds->width);
1216 vnc_write_u16(vs, vs->ds->height);
1218 vnc_write_u8(vs, vs->depth * 8); /* bits-per-pixel */
1219 vnc_write_u8(vs, vs->depth * 8); /* depth */
1220 #ifdef WORDS_BIGENDIAN
1221 vnc_write_u8(vs, 1); /* big-endian-flag */
1223 vnc_write_u8(vs, 0); /* big-endian-flag */
1225 vnc_write_u8(vs, 1); /* true-color-flag */
1226 if (vs->depth == 4) {
1227 vnc_write_u16(vs, 0xFF); /* red-max */
1228 vnc_write_u16(vs, 0xFF); /* green-max */
1229 vnc_write_u16(vs, 0xFF); /* blue-max */
1230 vnc_write_u8(vs, 16); /* red-shift */
1231 vnc_write_u8(vs, 8); /* green-shift */
1232 vnc_write_u8(vs, 0); /* blue-shift */
1233 vs->send_hextile_tile = send_hextile_tile_32;
1234 } else if (vs->depth == 2) {
1235 vnc_write_u16(vs, 31); /* red-max */
1236 vnc_write_u16(vs, 63); /* green-max */
1237 vnc_write_u16(vs, 31); /* blue-max */
1238 vnc_write_u8(vs, 11); /* red-shift */
1239 vnc_write_u8(vs, 5); /* green-shift */
1240 vnc_write_u8(vs, 0); /* blue-shift */
1241 vs->send_hextile_tile = send_hextile_tile_16;
1242 } else if (vs->depth == 1) {
1243 /* XXX: change QEMU pixel 8 bit pixel format to match the VNC one ? */
1244 vnc_write_u16(vs, 7); /* red-max */
1245 vnc_write_u16(vs, 7); /* green-max */
1246 vnc_write_u16(vs, 3); /* blue-max */
1247 vnc_write_u8(vs, 5); /* red-shift */
1248 vnc_write_u8(vs, 2); /* green-shift */
1249 vnc_write_u8(vs, 0); /* blue-shift */
1250 vs->send_hextile_tile = send_hextile_tile_8;
1252 vs->write_pixels = vnc_write_pixels_copy;
1254 vnc_write(vs, pad, 3); /* padding */
1257 size = snprintf(buf, sizeof(buf), "QEMU (%s)", qemu_name);
1259 size = snprintf(buf, sizeof(buf), "QEMU");
1261 vnc_write_u32(vs, size);
1262 vnc_write(vs, buf, size);
1265 vnc_read_when(vs, protocol_client_msg, 1);
1270 static void make_challenge(VncState *vs)
1274 srand(time(NULL)+getpid()+getpid()*987654+rand());
1276 for (i = 0 ; i < sizeof(vs->challenge) ; i++)
1277 vs->challenge[i] = (int) (256.0*rand()/(RAND_MAX+1.0));
1280 static int protocol_client_auth_vnc(VncState *vs, char *data, size_t len)
1282 char response[VNC_AUTH_CHALLENGE_SIZE];
1286 if (!vs->password || !vs->password[0]) {
1287 VNC_DEBUG("No password configured on server");
1288 vnc_write_u32(vs, 1); /* Reject auth */
1289 if (vs->minor >= 8) {
1290 static const char err[] = "Authentication failed";
1291 vnc_write_u32(vs, sizeof(err));
1292 vnc_write(vs, err, sizeof(err));
1295 vnc_client_error(vs);
1299 memcpy(response, vs->challenge, VNC_AUTH_CHALLENGE_SIZE);
1301 /* Calculate the expected challenge response */
1302 pwlen = strlen(vs->password);
1303 for (i=0; i<sizeof(key); i++)
1304 key[i] = i<pwlen ? vs->password[i] : 0;
1306 for (j = 0; j < VNC_AUTH_CHALLENGE_SIZE; j += 8)
1307 des(response+j, response+j);
1309 /* Compare expected vs actual challenge response */
1310 if (memcmp(response, data, VNC_AUTH_CHALLENGE_SIZE) != 0) {
1311 VNC_DEBUG("Client challenge reponse did not match\n");
1312 vnc_write_u32(vs, 1); /* Reject auth */
1313 if (vs->minor >= 8) {
1314 static const char err[] = "Authentication failed";
1315 vnc_write_u32(vs, sizeof(err));
1316 vnc_write(vs, err, sizeof(err));
1319 vnc_client_error(vs);
1321 VNC_DEBUG("Accepting VNC challenge response\n");
1322 vnc_write_u32(vs, 0); /* Accept auth */
1325 vnc_read_when(vs, protocol_client_init, 1);
1330 static int start_auth_vnc(VncState *vs)
1333 /* Send client a 'random' challenge */
1334 vnc_write(vs, vs->challenge, sizeof(vs->challenge));
1337 vnc_read_when(vs, protocol_client_auth_vnc, sizeof(vs->challenge));
1343 #define DH_BITS 1024
1344 static gnutls_dh_params_t dh_params;
1346 static int vnc_tls_initialize(void)
1348 static int tlsinitialized = 0;
1353 if (gnutls_global_init () < 0)
1356 /* XXX ought to re-generate diffie-hellmen params periodically */
1357 if (gnutls_dh_params_init (&dh_params) < 0)
1359 if (gnutls_dh_params_generate2 (dh_params, DH_BITS) < 0)
1363 gnutls_global_set_log_level(10);
1364 gnutls_global_set_log_function(vnc_debug_gnutls_log);
1372 static gnutls_anon_server_credentials vnc_tls_initialize_anon_cred(void)
1374 gnutls_anon_server_credentials anon_cred;
1377 if ((ret = gnutls_anon_allocate_server_credentials(&anon_cred)) < 0) {
1378 VNC_DEBUG("Cannot allocate credentials %s\n", gnutls_strerror(ret));
1382 gnutls_anon_set_server_dh_params(anon_cred, dh_params);
1388 static gnutls_certificate_credentials_t vnc_tls_initialize_x509_cred(void)
1390 gnutls_certificate_credentials_t x509_cred;
1394 if ((ret = gnutls_certificate_allocate_credentials(&x509_cred)) < 0) {
1395 VNC_DEBUG("Cannot allocate credentials %s\n", gnutls_strerror(ret));
1398 if ((ret = gnutls_certificate_set_x509_trust_file(x509_cred, X509_CA_CERT_FILE, GNUTLS_X509_FMT_PEM)) < 0) {
1399 VNC_DEBUG("Cannot load CA certificate %s\n", gnutls_strerror(ret));
1400 gnutls_certificate_free_credentials(x509_cred);
1404 if ((ret = gnutls_certificate_set_x509_key_file (x509_cred, X509_SERVER_CERT_FILE,
1405 X509_SERVER_KEY_FILE,
1406 GNUTLS_X509_FMT_PEM)) < 0) {
1407 VNC_DEBUG("Cannot load certificate & key %s\n", gnutls_strerror(ret));
1408 gnutls_certificate_free_credentials(x509_cred);
1412 if (stat(X509_CA_CRL_FILE, &st) < 0) {
1413 if (errno != ENOENT) {
1414 gnutls_certificate_free_credentials(x509_cred);
1418 if ((ret = gnutls_certificate_set_x509_crl_file(x509_cred, X509_CA_CRL_FILE, GNUTLS_X509_FMT_PEM)) < 0) {
1419 VNC_DEBUG("Cannot load CRL %s\n", gnutls_strerror(ret));
1420 gnutls_certificate_free_credentials(x509_cred);
1425 gnutls_certificate_set_dh_params (x509_cred, dh_params);
1430 static int start_auth_vencrypt_subauth(VncState *vs)
1432 switch (vs->subauth) {
1433 case VNC_AUTH_VENCRYPT_TLSNONE:
1434 case VNC_AUTH_VENCRYPT_X509NONE:
1435 VNC_DEBUG("Accept TLS auth none\n");
1436 vnc_write_u32(vs, 0); /* Accept auth completion */
1437 vnc_read_when(vs, protocol_client_init, 1);
1440 case VNC_AUTH_VENCRYPT_TLSVNC:
1441 case VNC_AUTH_VENCRYPT_X509VNC:
1442 VNC_DEBUG("Start TLS auth VNC\n");
1443 return start_auth_vnc(vs);
1445 default: /* Should not be possible, but just in case */
1446 VNC_DEBUG("Reject auth %d\n", vs->auth);
1447 vnc_write_u8(vs, 1);
1448 if (vs->minor >= 8) {
1449 static const char err[] = "Unsupported authentication type";
1450 vnc_write_u32(vs, sizeof(err));
1451 vnc_write(vs, err, sizeof(err));
1453 vnc_client_error(vs);
1459 static void vnc_handshake_io(void *opaque);
1461 static int vnc_continue_handshake(struct VncState *vs) {
1464 if ((ret = gnutls_handshake(vs->tls_session)) < 0) {
1465 if (!gnutls_error_is_fatal(ret)) {
1466 VNC_DEBUG("Handshake interrupted (blocking)\n");
1467 if (!gnutls_record_get_direction(vs->tls_session))
1468 qemu_set_fd_handler(vs->csock, vnc_handshake_io, NULL, vs);
1470 qemu_set_fd_handler(vs->csock, NULL, vnc_handshake_io, vs);
1473 VNC_DEBUG("Handshake failed %s\n", gnutls_strerror(ret));
1474 vnc_client_error(vs);
1478 VNC_DEBUG("Handshake done, switching to TLS data mode\n");
1479 vs->wiremode = VNC_WIREMODE_TLS;
1480 qemu_set_fd_handler2(vs->csock, NULL, vnc_client_read, vnc_client_write, vs);
1482 return start_auth_vencrypt_subauth(vs);
1485 static void vnc_handshake_io(void *opaque) {
1486 struct VncState *vs = (struct VncState *)opaque;
1488 VNC_DEBUG("Handshake IO continue\n");
1489 vnc_continue_handshake(vs);
1492 #define NEED_X509_AUTH(vs) \
1493 ((vs)->subauth == VNC_AUTH_VENCRYPT_X509NONE || \
1494 (vs)->subauth == VNC_AUTH_VENCRYPT_X509VNC || \
1495 (vs)->subauth == VNC_AUTH_VENCRYPT_X509PLAIN)
1498 static int vnc_start_tls(struct VncState *vs) {
1499 static const int cert_type_priority[] = { GNUTLS_CRT_X509, 0 };
1500 static const int protocol_priority[]= { GNUTLS_TLS1_1, GNUTLS_TLS1_0, GNUTLS_SSL3, 0 };
1501 static const int kx_anon[] = {GNUTLS_KX_ANON_DH, 0};
1502 static const int kx_x509[] = {GNUTLS_KX_DHE_DSS, GNUTLS_KX_RSA, GNUTLS_KX_DHE_RSA, GNUTLS_KX_SRP, 0};
1504 VNC_DEBUG("Do TLS setup\n");
1505 if (vnc_tls_initialize() < 0) {
1506 VNC_DEBUG("Failed to init TLS\n");
1507 vnc_client_error(vs);
1510 if (vs->tls_session == NULL) {
1511 if (gnutls_init(&vs->tls_session, GNUTLS_SERVER) < 0) {
1512 vnc_client_error(vs);
1516 if (gnutls_set_default_priority(vs->tls_session) < 0) {
1517 gnutls_deinit(vs->tls_session);
1518 vs->tls_session = NULL;
1519 vnc_client_error(vs);
1523 if (gnutls_kx_set_priority(vs->tls_session, NEED_X509_AUTH(vs) ? kx_x509 : kx_anon) < 0) {
1524 gnutls_deinit(vs->tls_session);
1525 vs->tls_session = NULL;
1526 vnc_client_error(vs);
1530 if (gnutls_certificate_type_set_priority(vs->tls_session, cert_type_priority) < 0) {
1531 gnutls_deinit(vs->tls_session);
1532 vs->tls_session = NULL;
1533 vnc_client_error(vs);
1537 if (gnutls_protocol_set_priority(vs->tls_session, protocol_priority) < 0) {
1538 gnutls_deinit(vs->tls_session);
1539 vs->tls_session = NULL;
1540 vnc_client_error(vs);
1544 if (NEED_X509_AUTH(vs)) {
1545 gnutls_certificate_server_credentials x509_cred = vnc_tls_initialize_x509_cred();
1547 gnutls_deinit(vs->tls_session);
1548 vs->tls_session = NULL;
1549 vnc_client_error(vs);
1552 if (gnutls_credentials_set(vs->tls_session, GNUTLS_CRD_CERTIFICATE, x509_cred) < 0) {
1553 gnutls_deinit(vs->tls_session);
1554 vs->tls_session = NULL;
1555 gnutls_certificate_free_credentials(x509_cred);
1556 vnc_client_error(vs);
1560 gnutls_anon_server_credentials anon_cred = vnc_tls_initialize_anon_cred();
1562 gnutls_deinit(vs->tls_session);
1563 vs->tls_session = NULL;
1564 vnc_client_error(vs);
1567 if (gnutls_credentials_set(vs->tls_session, GNUTLS_CRD_ANON, anon_cred) < 0) {
1568 gnutls_deinit(vs->tls_session);
1569 vs->tls_session = NULL;
1570 gnutls_anon_free_server_credentials(anon_cred);
1571 vnc_client_error(vs);
1576 gnutls_transport_set_ptr(vs->tls_session, (gnutls_transport_ptr_t)vs);
1577 gnutls_transport_set_push_function(vs->tls_session, vnc_tls_push);
1578 gnutls_transport_set_pull_function(vs->tls_session, vnc_tls_pull);
1581 VNC_DEBUG("Start TLS handshake process\n");
1582 return vnc_continue_handshake(vs);
1585 static int protocol_client_vencrypt_auth(VncState *vs, char *data, size_t len)
1587 int auth = read_u32(data, 0);
1589 if (auth != vs->subauth) {
1590 VNC_DEBUG("Rejecting auth %d\n", auth);
1591 vnc_write_u8(vs, 0); /* Reject auth */
1593 vnc_client_error(vs);
1595 VNC_DEBUG("Accepting auth %d, starting handshake\n", auth);
1596 vnc_write_u8(vs, 1); /* Accept auth */
1599 if (vnc_start_tls(vs) < 0) {
1600 VNC_DEBUG("Failed to complete TLS\n");
1604 if (vs->wiremode == VNC_WIREMODE_TLS) {
1605 VNC_DEBUG("Starting VeNCrypt subauth\n");
1606 return start_auth_vencrypt_subauth(vs);
1608 VNC_DEBUG("TLS handshake blocked\n");
1615 static int protocol_client_vencrypt_init(VncState *vs, char *data, size_t len)
1619 VNC_DEBUG("Unsupported VeNCrypt protocol %d.%d\n", (int)data[0], (int)data[1]);
1620 vnc_write_u8(vs, 1); /* Reject version */
1622 vnc_client_error(vs);
1624 VNC_DEBUG("Sending allowed auth %d\n", vs->subauth);
1625 vnc_write_u8(vs, 0); /* Accept version */
1626 vnc_write_u8(vs, 1); /* Number of sub-auths */
1627 vnc_write_u32(vs, vs->subauth); /* The supported auth */
1629 vnc_read_when(vs, protocol_client_vencrypt_auth, 4);
1634 static int start_auth_vencrypt(VncState *vs)
1636 /* Send VeNCrypt version 0.2 */
1637 vnc_write_u8(vs, 0);
1638 vnc_write_u8(vs, 2);
1640 vnc_read_when(vs, protocol_client_vencrypt_init, 2);
1643 #endif /* CONFIG_VNC_TLS */
1645 static int protocol_client_auth(VncState *vs, char *data, size_t len)
1647 /* We only advertise 1 auth scheme at a time, so client
1648 * must pick the one we sent. Verify this */
1649 if (data[0] != vs->auth) { /* Reject auth */
1650 VNC_DEBUG("Reject auth %d\n", (int)data[0]);
1651 vnc_write_u32(vs, 1);
1652 if (vs->minor >= 8) {
1653 static const char err[] = "Authentication failed";
1654 vnc_write_u32(vs, sizeof(err));
1655 vnc_write(vs, err, sizeof(err));
1657 vnc_client_error(vs);
1658 } else { /* Accept requested auth */
1659 VNC_DEBUG("Client requested auth %d\n", (int)data[0]);
1662 VNC_DEBUG("Accept auth none\n");
1663 vnc_write_u32(vs, 0); /* Accept auth completion */
1664 vnc_read_when(vs, protocol_client_init, 1);
1668 VNC_DEBUG("Start VNC auth\n");
1669 return start_auth_vnc(vs);
1672 case VNC_AUTH_VENCRYPT:
1673 VNC_DEBUG("Accept VeNCrypt auth\n");;
1674 return start_auth_vencrypt(vs);
1675 #endif /* CONFIG_VNC_TLS */
1677 default: /* Should not be possible, but just in case */
1678 VNC_DEBUG("Reject auth %d\n", vs->auth);
1679 vnc_write_u8(vs, 1);
1680 if (vs->minor >= 8) {
1681 static const char err[] = "Authentication failed";
1682 vnc_write_u32(vs, sizeof(err));
1683 vnc_write(vs, err, sizeof(err));
1685 vnc_client_error(vs);
1691 static int protocol_version(VncState *vs, char *version, size_t len)
1695 memcpy(local, version, 12);
1698 if (sscanf(local, "RFB %03d.%03d\n", &vs->major, &vs->minor) != 2) {
1699 VNC_DEBUG("Malformed protocol version %s\n", local);
1700 vnc_client_error(vs);
1703 VNC_DEBUG("Client request protocol version %d.%d\n", vs->major, vs->minor);
1704 if (vs->major != 3 ||
1709 VNC_DEBUG("Unsupported client version\n");
1710 vnc_write_u32(vs, VNC_AUTH_INVALID);
1712 vnc_client_error(vs);
1715 /* Some broken client report v3.5 which spec requires to be treated
1716 * as equivalent to v3.3 by servers
1721 if (vs->minor == 3) {
1722 if (vs->auth == VNC_AUTH_NONE) {
1723 VNC_DEBUG("Tell client auth none\n");
1724 vnc_write_u32(vs, vs->auth);
1726 vnc_read_when(vs, protocol_client_init, 1);
1727 } else if (vs->auth == VNC_AUTH_VNC) {
1728 VNC_DEBUG("Tell client VNC auth\n");
1729 vnc_write_u32(vs, vs->auth);
1733 VNC_DEBUG("Unsupported auth %d for protocol 3.3\n", vs->auth);
1734 vnc_write_u32(vs, VNC_AUTH_INVALID);
1736 vnc_client_error(vs);
1739 VNC_DEBUG("Telling client we support auth %d\n", vs->auth);
1740 vnc_write_u8(vs, 1); /* num auth */
1741 vnc_write_u8(vs, vs->auth);
1742 vnc_read_when(vs, protocol_client_auth, 1);
1749 static void vnc_listen_read(void *opaque)
1751 VncState *vs = opaque;
1752 struct sockaddr_in addr;
1753 socklen_t addrlen = sizeof(addr);
1755 vs->csock = accept(vs->lsock, (struct sockaddr *)&addr, &addrlen);
1756 if (vs->csock != -1) {
1757 VNC_DEBUG("New client on socket %d\n", vs->csock);
1758 socket_set_nonblock(vs->csock);
1759 qemu_set_fd_handler2(vs->csock, NULL, vnc_client_read, NULL, opaque);
1760 vnc_write(vs, "RFB 003.008\n", 12);
1762 vnc_read_when(vs, protocol_version, 12);
1763 memset(vs->old_data, 0, vs->ds->linesize * vs->ds->height);
1764 memset(vs->dirty_row, 0xFF, sizeof(vs->dirty_row));
1766 vs->has_hextile = 0;
1767 vs->ds->dpy_copy = NULL;
1771 extern int parse_host_port(struct sockaddr_in *saddr, const char *str);
1773 void vnc_display_init(DisplayState *ds)
1777 vs = qemu_mallocz(sizeof(VncState));
1784 vs->password = NULL;
1794 if (!keyboard_layout)
1795 keyboard_layout = "en-us";
1797 vs->kbd_layout = init_keyboard_layout(keyboard_layout);
1798 if (!vs->kbd_layout)
1801 vs->ds->data = NULL;
1802 vs->ds->dpy_update = vnc_dpy_update;
1803 vs->ds->dpy_resize = vnc_dpy_resize;
1804 vs->ds->dpy_refresh = vnc_dpy_refresh;
1806 memset(vs->dirty_row, 0xFF, sizeof(vs->dirty_row));
1808 vnc_dpy_resize(vs->ds, 640, 400);
1811 void vnc_display_close(DisplayState *ds)
1813 VncState *vs = ds ? (VncState *)ds->opaque : vnc_state;
1816 qemu_free(vs->display);
1819 if (vs->lsock != -1) {
1820 qemu_set_fd_handler2(vs->lsock, NULL, NULL, NULL, NULL);
1824 if (vs->csock != -1) {
1825 qemu_set_fd_handler2(vs->csock, NULL, NULL, NULL, NULL);
1826 closesocket(vs->csock);
1828 buffer_reset(&vs->input);
1829 buffer_reset(&vs->output);
1830 vs->need_update = 0;
1832 if (vs->tls_session) {
1833 gnutls_deinit(vs->tls_session);
1834 vs->tls_session = NULL;
1836 vs->wiremode = VNC_WIREMODE_CLEAR;
1837 #endif /* CONFIG_VNC_TLS */
1839 vs->auth = VNC_AUTH_INVALID;
1841 vs->subauth = VNC_AUTH_INVALID;
1845 int vnc_display_password(DisplayState *ds, const char *password)
1847 VncState *vs = ds ? (VncState *)ds->opaque : vnc_state;
1850 qemu_free(vs->password);
1851 vs->password = NULL;
1853 if (password && password[0]) {
1854 if (!(vs->password = qemu_strdup(password)))
1861 int vnc_display_open(DisplayState *ds, const char *display)
1863 struct sockaddr *addr;
1864 struct sockaddr_in iaddr;
1866 struct sockaddr_un uaddr;
1868 int reuse_addr, ret;
1871 VncState *vs = ds ? (VncState *)ds->opaque : vnc_state;
1872 const char *options;
1875 int tls = 0, x509 = 0;
1878 vnc_display_close(ds);
1879 if (strcmp(display, "none") == 0)
1882 if (!(vs->display = strdup(display)))
1886 while ((options = strchr(options, ','))) {
1888 if (strncmp(options, "password", 8) == 0)
1889 password = 1; /* Require password auth */
1891 else if (strncmp(options, "tls", 3) == 0)
1892 tls = 1; /* Require TLS */
1893 else if (strncmp(options, "x509", 4) == 0)
1894 x509 = 1; /* Require x509 certificates */
1901 vs->auth = VNC_AUTH_VENCRYPT;
1903 VNC_DEBUG("Initializing VNC server with x509 password auth\n");
1904 vs->subauth = VNC_AUTH_VENCRYPT_X509VNC;
1906 VNC_DEBUG("Initializing VNC server with TLS password auth\n");
1907 vs->subauth = VNC_AUTH_VENCRYPT_TLSVNC;
1911 VNC_DEBUG("Initializing VNC server with password auth\n");
1912 vs->auth = VNC_AUTH_VNC;
1914 vs->subauth = VNC_AUTH_INVALID;
1920 vs->auth = VNC_AUTH_VENCRYPT;
1922 VNC_DEBUG("Initializing VNC server with x509 no auth\n");
1923 vs->subauth = VNC_AUTH_VENCRYPT_X509NONE;
1925 VNC_DEBUG("Initializing VNC server with TLS no auth\n");
1926 vs->subauth = VNC_AUTH_VENCRYPT_TLSNONE;
1930 VNC_DEBUG("Initializing VNC server with no auth\n");
1931 vs->auth = VNC_AUTH_NONE;
1933 vs->subauth = VNC_AUTH_INVALID;
1938 if (strstart(display, "unix:", &p)) {
1939 addr = (struct sockaddr *)&uaddr;
1940 addrlen = sizeof(uaddr);
1942 vs->lsock = socket(PF_UNIX, SOCK_STREAM, 0);
1943 if (vs->lsock == -1) {
1944 fprintf(stderr, "Could not create socket\n");
1950 uaddr.sun_family = AF_UNIX;
1951 memset(uaddr.sun_path, 0, 108);
1952 snprintf(uaddr.sun_path, 108, "%s", p);
1954 unlink(uaddr.sun_path);
1958 addr = (struct sockaddr *)&iaddr;
1959 addrlen = sizeof(iaddr);
1961 if (parse_host_port(&iaddr, display) < 0) {
1962 fprintf(stderr, "Could not parse VNC address\n");
1968 iaddr.sin_port = htons(ntohs(iaddr.sin_port) + 5900);
1970 vs->lsock = socket(PF_INET, SOCK_STREAM, 0);
1971 if (vs->lsock == -1) {
1972 fprintf(stderr, "Could not create socket\n");
1979 ret = setsockopt(vs->lsock, SOL_SOCKET, SO_REUSEADDR,
1980 (const char *)&reuse_addr, sizeof(reuse_addr));
1982 fprintf(stderr, "setsockopt() failed\n");
1991 if (bind(vs->lsock, addr, addrlen) == -1) {
1992 fprintf(stderr, "bind() failed\n");
2000 if (listen(vs->lsock, 1) == -1) {
2001 fprintf(stderr, "listen() failed\n");
2009 return qemu_set_fd_handler2(vs->lsock, vnc_listen_poll, vnc_listen_read, NULL, vs);