Disable the vnc CopyRect encoding
[qemu-kvm/fedora.git] / vnc.c
blob508f40df48b302d1ee7676f6ad05d55aafd6a682
1 /*
2 * QEMU VNC display driver
4 * Copyright (C) 2006 Anthony Liguori <anthony@codemonkey.ws>
5 * Copyright (C) 2006 Fabrice Bellard
6 * Copyright (C) 2009 Red Hat, Inc
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:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
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.
27 #include "vnc.h"
28 #include "sysemu.h"
29 #include "qemu_socket.h"
30 #include "qemu-timer.h"
31 #include "acl.h"
33 #define VNC_REFRESH_INTERVAL (1000 / 30)
35 #include "vnc_keysym.h"
36 #include "d3des.h"
38 #define count_bits(c, v) { \
39 for (c = 0; v; v >>= 1) \
40 { \
41 c += v & 1; \
42 } \
46 static VncDisplay *vnc_display; /* needed for info vnc */
47 static DisplayChangeListener *dcl;
49 static char *addr_to_string(const char *format,
50 struct sockaddr_storage *sa,
51 socklen_t salen) {
52 char *addr;
53 char host[NI_MAXHOST];
54 char serv[NI_MAXSERV];
55 int err;
57 if ((err = getnameinfo((struct sockaddr *)sa, salen,
58 host, sizeof(host),
59 serv, sizeof(serv),
60 NI_NUMERICHOST | NI_NUMERICSERV)) != 0) {
61 VNC_DEBUG("Cannot resolve address %d: %s\n",
62 err, gai_strerror(err));
63 return NULL;
66 if (asprintf(&addr, format, host, serv) < 0)
67 return NULL;
69 return addr;
73 char *vnc_socket_local_addr(const char *format, int fd) {
74 struct sockaddr_storage sa;
75 socklen_t salen;
77 salen = sizeof(sa);
78 if (getsockname(fd, (struct sockaddr*)&sa, &salen) < 0)
79 return NULL;
81 return addr_to_string(format, &sa, salen);
85 char *vnc_socket_remote_addr(const char *format, int fd) {
86 struct sockaddr_storage sa;
87 socklen_t salen;
89 salen = sizeof(sa);
90 if (getpeername(fd, (struct sockaddr*)&sa, &salen) < 0)
91 return NULL;
93 return addr_to_string(format, &sa, salen);
96 static const char *vnc_auth_name(VncDisplay *vd) {
97 switch (vd->auth) {
98 case VNC_AUTH_INVALID:
99 return "invalid";
100 case VNC_AUTH_NONE:
101 return "none";
102 case VNC_AUTH_VNC:
103 return "vnc";
104 case VNC_AUTH_RA2:
105 return "ra2";
106 case VNC_AUTH_RA2NE:
107 return "ra2ne";
108 case VNC_AUTH_TIGHT:
109 return "tight";
110 case VNC_AUTH_ULTRA:
111 return "ultra";
112 case VNC_AUTH_TLS:
113 return "tls";
114 case VNC_AUTH_VENCRYPT:
115 #ifdef CONFIG_VNC_TLS
116 switch (vd->subauth) {
117 case VNC_AUTH_VENCRYPT_PLAIN:
118 return "vencrypt+plain";
119 case VNC_AUTH_VENCRYPT_TLSNONE:
120 return "vencrypt+tls+none";
121 case VNC_AUTH_VENCRYPT_TLSVNC:
122 return "vencrypt+tls+vnc";
123 case VNC_AUTH_VENCRYPT_TLSPLAIN:
124 return "vencrypt+tls+plain";
125 case VNC_AUTH_VENCRYPT_X509NONE:
126 return "vencrypt+x509+none";
127 case VNC_AUTH_VENCRYPT_X509VNC:
128 return "vencrypt+x509+vnc";
129 case VNC_AUTH_VENCRYPT_X509PLAIN:
130 return "vencrypt+x509+plain";
131 case VNC_AUTH_VENCRYPT_TLSSASL:
132 return "vencrypt+tls+sasl";
133 case VNC_AUTH_VENCRYPT_X509SASL:
134 return "vencrypt+x509+sasl";
135 default:
136 return "vencrypt";
138 #else
139 return "vencrypt";
140 #endif
141 case VNC_AUTH_SASL:
142 return "sasl";
144 return "unknown";
147 #define VNC_SOCKET_FORMAT_PRETTY "local %s:%s"
149 static void do_info_vnc_client(VncState *client)
151 char *clientAddr =
152 vnc_socket_remote_addr(" address: %s:%s\n",
153 client->csock);
154 if (!clientAddr)
155 return;
157 term_puts("Client:\n");
158 term_puts(clientAddr);
159 free(clientAddr);
161 #ifdef CONFIG_VNC_TLS
162 if (client->tls.session &&
163 client->tls.dname)
164 term_printf(" x509 dname: %s\n", client->tls.dname);
165 else
166 term_puts(" x509 dname: none\n");
167 #endif
168 #ifdef CONFIG_VNC_SASL
169 if (client->sasl.conn &&
170 client->sasl.username)
171 term_printf(" username: %s\n", client->sasl.username);
172 else
173 term_puts(" username: none\n");
174 #endif
177 void do_info_vnc(void)
179 if (vnc_display == NULL || vnc_display->display == NULL) {
180 term_printf("Server: disabled\n");
181 } else {
182 char *serverAddr = vnc_socket_local_addr(" address: %s:%s\n",
183 vnc_display->lsock);
185 if (!serverAddr)
186 return;
188 term_puts("Server:\n");
189 term_puts(serverAddr);
190 free(serverAddr);
191 term_printf(" auth: %s\n", vnc_auth_name(vnc_display));
193 if (vnc_display->clients) {
194 VncState *client = vnc_display->clients;
195 while (client) {
196 do_info_vnc_client(client);
197 client = client->next;
199 } else {
200 term_printf("Client: none\n");
205 static inline uint32_t vnc_has_feature(VncState *vs, int feature) {
206 return (vs->features & (1 << feature));
209 /* TODO
210 1) Get the queue working for IO.
211 2) there is some weirdness when using the -S option (the screen is grey
212 and not totally invalidated
213 3) resolutions > 1024
216 static void vnc_update_client(void *opaque);
217 static void vnc_disconnect_start(VncState *vs);
218 static void vnc_disconnect_finish(VncState *vs);
220 static void vnc_colordepth(VncState *vs);
222 static inline void vnc_set_bit(uint32_t *d, int k)
224 d[k >> 5] |= 1 << (k & 0x1f);
227 static inline void vnc_clear_bit(uint32_t *d, int k)
229 d[k >> 5] &= ~(1 << (k & 0x1f));
232 static inline void vnc_set_bits(uint32_t *d, int n, int nb_words)
234 int j;
236 j = 0;
237 while (n >= 32) {
238 d[j++] = -1;
239 n -= 32;
241 if (n > 0)
242 d[j++] = (1 << n) - 1;
243 while (j < nb_words)
244 d[j++] = 0;
247 static inline int vnc_get_bit(const uint32_t *d, int k)
249 return (d[k >> 5] >> (k & 0x1f)) & 1;
252 static inline int vnc_and_bits(const uint32_t *d1, const uint32_t *d2,
253 int nb_words)
255 int i;
256 for(i = 0; i < nb_words; i++) {
257 if ((d1[i] & d2[i]) != 0)
258 return 1;
260 return 0;
263 static void vnc_update(VncState *vs, int x, int y, int w, int h)
265 int i;
267 h += y;
269 /* round x down to ensure the loop only spans one 16-pixel block per,
270 iteration. otherwise, if (x % 16) != 0, the last iteration may span
271 two 16-pixel blocks but we only mark the first as dirty
273 w += (x % 16);
274 x -= (x % 16);
276 x = MIN(x, vs->serverds.width);
277 y = MIN(y, vs->serverds.height);
278 w = MIN(x + w, vs->serverds.width) - x;
279 h = MIN(h, vs->serverds.height);
281 for (; y < h; y++)
282 for (i = 0; i < w; i += 16)
283 vnc_set_bit(vs->dirty_row[y], (x + i) / 16);
286 static void vnc_dpy_update(DisplayState *ds, int x, int y, int w, int h)
288 VncDisplay *vd = ds->opaque;
289 VncState *vs = vd->clients;
290 while (vs != NULL) {
291 vnc_update(vs, x, y, w, h);
292 vs = vs->next;
296 static void vnc_framebuffer_update(VncState *vs, int x, int y, int w, int h,
297 int32_t encoding)
299 vnc_write_u16(vs, x);
300 vnc_write_u16(vs, y);
301 vnc_write_u16(vs, w);
302 vnc_write_u16(vs, h);
304 vnc_write_s32(vs, encoding);
307 void buffer_reserve(Buffer *buffer, size_t len)
309 if ((buffer->capacity - buffer->offset) < len) {
310 buffer->capacity += (len + 1024);
311 buffer->buffer = qemu_realloc(buffer->buffer, buffer->capacity);
312 if (buffer->buffer == NULL) {
313 fprintf(stderr, "vnc: out of memory\n");
314 exit(1);
319 int buffer_empty(Buffer *buffer)
321 return buffer->offset == 0;
324 uint8_t *buffer_end(Buffer *buffer)
326 return buffer->buffer + buffer->offset;
329 void buffer_reset(Buffer *buffer)
331 buffer->offset = 0;
334 void buffer_append(Buffer *buffer, const void *data, size_t len)
336 memcpy(buffer->buffer + buffer->offset, data, len);
337 buffer->offset += len;
340 static void vnc_resize(VncState *vs)
342 DisplayState *ds = vs->ds;
344 int size_changed;
346 vs->old_data = qemu_realloc(vs->old_data, ds_get_linesize(ds) * ds_get_height(ds));
348 if (vs->old_data == NULL) {
349 fprintf(stderr, "vnc: memory allocation failed\n");
350 exit(1);
353 if (ds_get_bytes_per_pixel(ds) != vs->serverds.pf.bytes_per_pixel)
354 console_color_init(ds);
355 vnc_colordepth(vs);
356 size_changed = ds_get_width(ds) != vs->serverds.width ||
357 ds_get_height(ds) != vs->serverds.height;
358 vs->serverds = *(ds->surface);
359 if (size_changed) {
360 if (vs->csock != -1 && vnc_has_feature(vs, VNC_FEATURE_RESIZE)) {
361 vnc_write_u8(vs, 0); /* msg id */
362 vnc_write_u8(vs, 0);
363 vnc_write_u16(vs, 1); /* number of rects */
364 vnc_framebuffer_update(vs, 0, 0, ds_get_width(ds), ds_get_height(ds),
365 VNC_ENCODING_DESKTOPRESIZE);
366 vnc_flush(vs);
370 memset(vs->dirty_row, 0xFF, sizeof(vs->dirty_row));
371 memset(vs->old_data, 42, ds_get_linesize(vs->ds) * ds_get_height(vs->ds));
374 static void vnc_dpy_resize(DisplayState *ds)
376 VncDisplay *vd = ds->opaque;
377 VncState *vs = vd->clients;
378 while (vs != NULL) {
379 vnc_resize(vs);
380 vs = vs->next;
384 /* fastest code */
385 static void vnc_write_pixels_copy(VncState *vs, void *pixels, int size)
387 vnc_write(vs, pixels, size);
390 /* slowest but generic code. */
391 static void vnc_convert_pixel(VncState *vs, uint8_t *buf, uint32_t v)
393 uint8_t r, g, b;
395 r = ((((v & vs->serverds.pf.rmask) >> vs->serverds.pf.rshift) << vs->clientds.pf.rbits) >>
396 vs->serverds.pf.rbits);
397 g = ((((v & vs->serverds.pf.gmask) >> vs->serverds.pf.gshift) << vs->clientds.pf.gbits) >>
398 vs->serverds.pf.gbits);
399 b = ((((v & vs->serverds.pf.bmask) >> vs->serverds.pf.bshift) << vs->clientds.pf.bbits) >>
400 vs->serverds.pf.bbits);
401 v = (r << vs->clientds.pf.rshift) |
402 (g << vs->clientds.pf.gshift) |
403 (b << vs->clientds.pf.bshift);
404 switch(vs->clientds.pf.bytes_per_pixel) {
405 case 1:
406 buf[0] = v;
407 break;
408 case 2:
409 if (vs->clientds.flags & QEMU_BIG_ENDIAN_FLAG) {
410 buf[0] = v >> 8;
411 buf[1] = v;
412 } else {
413 buf[1] = v >> 8;
414 buf[0] = v;
416 break;
417 default:
418 case 4:
419 if (vs->clientds.flags & QEMU_BIG_ENDIAN_FLAG) {
420 buf[0] = v >> 24;
421 buf[1] = v >> 16;
422 buf[2] = v >> 8;
423 buf[3] = v;
424 } else {
425 buf[3] = v >> 24;
426 buf[2] = v >> 16;
427 buf[1] = v >> 8;
428 buf[0] = v;
430 break;
434 static void vnc_write_pixels_generic(VncState *vs, void *pixels1, int size)
436 uint8_t buf[4];
438 if (vs->serverds.pf.bytes_per_pixel == 4) {
439 uint32_t *pixels = pixels1;
440 int n, i;
441 n = size >> 2;
442 for(i = 0; i < n; i++) {
443 vnc_convert_pixel(vs, buf, pixels[i]);
444 vnc_write(vs, buf, vs->clientds.pf.bytes_per_pixel);
446 } else if (vs->serverds.pf.bytes_per_pixel == 2) {
447 uint16_t *pixels = pixels1;
448 int n, i;
449 n = size >> 1;
450 for(i = 0; i < n; i++) {
451 vnc_convert_pixel(vs, buf, pixels[i]);
452 vnc_write(vs, buf, vs->clientds.pf.bytes_per_pixel);
454 } else if (vs->serverds.pf.bytes_per_pixel == 1) {
455 uint8_t *pixels = pixels1;
456 int n, i;
457 n = size;
458 for(i = 0; i < n; i++) {
459 vnc_convert_pixel(vs, buf, pixels[i]);
460 vnc_write(vs, buf, vs->clientds.pf.bytes_per_pixel);
462 } else {
463 fprintf(stderr, "vnc_write_pixels_generic: VncState color depth not supported\n");
467 static void send_framebuffer_update_raw(VncState *vs, int x, int y, int w, int h)
469 int i;
470 uint8_t *row;
472 row = ds_get_data(vs->ds) + y * ds_get_linesize(vs->ds) + x * ds_get_bytes_per_pixel(vs->ds);
473 for (i = 0; i < h; i++) {
474 vs->write_pixels(vs, row, w * ds_get_bytes_per_pixel(vs->ds));
475 row += ds_get_linesize(vs->ds);
479 static void hextile_enc_cord(uint8_t *ptr, int x, int y, int w, int h)
481 ptr[0] = ((x & 0x0F) << 4) | (y & 0x0F);
482 ptr[1] = (((w - 1) & 0x0F) << 4) | ((h - 1) & 0x0F);
485 #define BPP 8
486 #include "vnchextile.h"
487 #undef BPP
489 #define BPP 16
490 #include "vnchextile.h"
491 #undef BPP
493 #define BPP 32
494 #include "vnchextile.h"
495 #undef BPP
497 #define GENERIC
498 #define BPP 8
499 #include "vnchextile.h"
500 #undef BPP
501 #undef GENERIC
503 #define GENERIC
504 #define BPP 16
505 #include "vnchextile.h"
506 #undef BPP
507 #undef GENERIC
509 #define GENERIC
510 #define BPP 32
511 #include "vnchextile.h"
512 #undef BPP
513 #undef GENERIC
515 static void send_framebuffer_update_hextile(VncState *vs, int x, int y, int w, int h)
517 int i, j;
518 int has_fg, has_bg;
519 uint8_t *last_fg, *last_bg;
521 last_fg = (uint8_t *) qemu_malloc(vs->serverds.pf.bytes_per_pixel);
522 last_bg = (uint8_t *) qemu_malloc(vs->serverds.pf.bytes_per_pixel);
523 has_fg = has_bg = 0;
524 for (j = y; j < (y + h); j += 16) {
525 for (i = x; i < (x + w); i += 16) {
526 vs->send_hextile_tile(vs, i, j,
527 MIN(16, x + w - i), MIN(16, y + h - j),
528 last_bg, last_fg, &has_bg, &has_fg);
531 free(last_fg);
532 free(last_bg);
536 static void vnc_zlib_init(VncState *vs)
538 int i;
539 for (i=0; i<(sizeof(vs->zlib_stream) / sizeof(z_stream)); i++)
540 vs->zlib_stream[i].opaque = NULL;
543 static void vnc_zlib_start(VncState *vs)
545 buffer_reset(&vs->zlib);
547 // make the output buffer be the zlib buffer, so we can compress it later
548 vs->zlib_tmp = vs->output;
549 vs->output = vs->zlib;
552 static int vnc_zlib_stop(VncState *vs, int stream_id)
554 z_streamp zstream = &vs->zlib_stream[stream_id];
555 int previous_out;
557 // switch back to normal output/zlib buffers
558 vs->zlib = vs->output;
559 vs->output = vs->zlib_tmp;
561 // compress the zlib buffer
563 // initialize the stream
564 // XXX need one stream per session
565 if (zstream->opaque != vs) {
566 int err;
568 VNC_DEBUG("VNC: initializing zlib stream %d\n", stream_id);
569 VNC_DEBUG("VNC: opaque = %p | vs = %p\n", zstream->opaque, vs);
570 zstream->zalloc = Z_NULL;
571 zstream->zfree = Z_NULL;
573 err = deflateInit2(zstream, vs->tight_compression, Z_DEFLATED, MAX_WBITS,
574 MAX_MEM_LEVEL, Z_DEFAULT_STRATEGY);
576 if (err != Z_OK) {
577 fprintf(stderr, "VNC: error initializing zlib\n");
578 return -1;
581 zstream->opaque = vs;
584 // XXX what to do if tight_compression changed in between?
586 // reserve memory in output buffer
587 buffer_reserve(&vs->output, vs->zlib.offset + 64);
589 // set pointers
590 zstream->next_in = vs->zlib.buffer;
591 zstream->avail_in = vs->zlib.offset;
592 zstream->next_out = vs->output.buffer + vs->output.offset;
593 zstream->avail_out = vs->output.capacity - vs->output.offset;
594 zstream->data_type = Z_BINARY;
595 previous_out = zstream->total_out;
597 // start encoding
598 if (deflate(zstream, Z_SYNC_FLUSH) != Z_OK) {
599 fprintf(stderr, "VNC: error during zlib compression\n");
600 return -1;
603 vs->output.offset = vs->output.capacity - zstream->avail_out;
604 return zstream->total_out - previous_out;
607 static void send_framebuffer_update_zlib(VncState *vs, int x, int y, int w, int h)
609 int old_offset, new_offset, bytes_written;
611 vnc_framebuffer_update(vs, x, y, w, h, VNC_ENCODING_ZLIB);
613 // remember where we put in the follow-up size
614 old_offset = vs->output.offset;
615 vnc_write_s32(vs, 0);
617 // compress the stream
618 vnc_zlib_start(vs);
619 send_framebuffer_update_raw(vs, x, y, w, h);
620 bytes_written = vnc_zlib_stop(vs, 0);
622 if (bytes_written == -1)
623 return;
625 // hack in the size
626 new_offset = vs->output.offset;
627 vs->output.offset = old_offset;
628 vnc_write_u32(vs, bytes_written);
629 vs->output.offset = new_offset;
632 static void send_framebuffer_update(VncState *vs, int x, int y, int w, int h)
634 switch(vs->vnc_encoding) {
635 case VNC_ENCODING_ZLIB:
636 send_framebuffer_update_zlib(vs, x, y, w, h);
637 break;
638 case VNC_ENCODING_HEXTILE:
639 vnc_framebuffer_update(vs, x, y, w, h, VNC_ENCODING_HEXTILE);
640 send_framebuffer_update_hextile(vs, x, y, w, h);
641 break;
642 default:
643 vnc_framebuffer_update(vs, x, y, w, h, VNC_ENCODING_RAW);
644 send_framebuffer_update_raw(vs, x, y, w, h);
645 break;
649 static void vnc_copy(VncState *vs, int src_x, int src_y, int dst_x, int dst_y, int w, int h)
652 uint8_t *src_row;
653 uint8_t *dst_row;
654 int y,pitch,depth;
656 vnc_update_client(vs);
658 /* send bitblit op to the vnc client */
659 vnc_write_u8(vs, 0); /* msg id */
660 vnc_write_u8(vs, 0);
661 vnc_write_u16(vs, 1); /* number of rects */
662 vnc_framebuffer_update(vs, dst_x, dst_y, w, h, VNC_ENCODING_COPYRECT);
663 vnc_write_u16(vs, src_x);
664 vnc_write_u16(vs, src_y);
665 vnc_flush(vs);
667 /* do bitblit op on the local surface too */
668 pitch = ds_get_linesize(vs->ds);
669 depth = ds_get_bytes_per_pixel(vs->ds);
670 src_row = ds_get_data(vs->ds) + pitch * src_y + depth * src_x;
671 dst_row = ds_get_data(vs->ds) + pitch * dst_y + depth * dst_x;
672 if (dst_y > src_y) {
673 /* copy backwards */
674 src_row += pitch * (h-1);
675 dst_row += pitch * (h-1);
676 pitch = -pitch;
678 for (y = 0; y < h; y++) {
679 memmove(dst_row, src_row, w * depth);
680 src_row += pitch;
681 dst_row += pitch;
685 static void vnc_dpy_copy(DisplayState *ds, int src_x, int src_y, int dst_x, int dst_y, int w, int h)
687 VncDisplay *vd = ds->opaque;
688 VncState *vs, *vn;
690 for (vs = vd->clients; vs != NULL; vs = vn) {
691 vn = vs->next;
692 if (vnc_has_feature(vs, VNC_FEATURE_COPYRECT)) {
693 vnc_update_client(vs);
694 /* vs might be free()ed here */
698 for (vs = vd->clients; vs != NULL; vs = vs->next) {
699 if (vnc_has_feature(vs, VNC_FEATURE_COPYRECT))
700 vnc_copy(vs, src_x, src_y, dst_x, dst_y, w, h);
701 else /* TODO */
702 vnc_update(vs, dst_x, dst_y, w, h);
706 static int find_dirty_height(VncState *vs, int y, int last_x, int x)
708 int h;
710 for (h = 1; h < (vs->serverds.height - y); h++) {
711 int tmp_x;
712 if (!vnc_get_bit(vs->dirty_row[y + h], last_x))
713 break;
714 for (tmp_x = last_x; tmp_x < x; tmp_x++)
715 vnc_clear_bit(vs->dirty_row[y + h], tmp_x);
718 return h;
721 static void vnc_update_client(void *opaque)
723 VncState *vs = opaque;
724 if (vs->need_update && vs->csock != -1) {
725 int y;
726 uint8_t *row;
727 char *old_row;
728 uint32_t width_mask[VNC_DIRTY_WORDS];
729 int n_rectangles;
730 int saved_offset;
731 int has_dirty = 0;
733 vga_hw_update();
735 vnc_set_bits(width_mask, (ds_get_width(vs->ds) / 16), VNC_DIRTY_WORDS);
737 /* Walk through the dirty map and eliminate tiles that
738 really aren't dirty */
739 row = ds_get_data(vs->ds);
740 old_row = vs->old_data;
742 for (y = 0; y < ds_get_height(vs->ds); y++) {
743 if (vnc_and_bits(vs->dirty_row[y], width_mask, VNC_DIRTY_WORDS)) {
744 int x;
745 uint8_t *ptr;
746 char *old_ptr;
748 ptr = row;
749 old_ptr = (char*)old_row;
751 for (x = 0; x < ds_get_width(vs->ds); x += 16) {
752 if (memcmp(old_ptr, ptr, 16 * ds_get_bytes_per_pixel(vs->ds)) == 0) {
753 vnc_clear_bit(vs->dirty_row[y], (x / 16));
754 } else {
755 has_dirty = 1;
756 memcpy(old_ptr, ptr, 16 * ds_get_bytes_per_pixel(vs->ds));
759 ptr += 16 * ds_get_bytes_per_pixel(vs->ds);
760 old_ptr += 16 * ds_get_bytes_per_pixel(vs->ds);
764 row += ds_get_linesize(vs->ds);
765 old_row += ds_get_linesize(vs->ds);
768 if (!has_dirty && !vs->audio_cap) {
769 qemu_mod_timer(vs->timer, qemu_get_clock(rt_clock) + VNC_REFRESH_INTERVAL);
770 return;
773 /* Count rectangles */
774 n_rectangles = 0;
775 vnc_write_u8(vs, 0); /* msg id */
776 vnc_write_u8(vs, 0);
777 saved_offset = vs->output.offset;
778 vnc_write_u16(vs, 0);
780 for (y = 0; y < vs->serverds.height; y++) {
781 int x;
782 int last_x = -1;
783 for (x = 0; x < vs->serverds.width / 16; x++) {
784 if (vnc_get_bit(vs->dirty_row[y], x)) {
785 if (last_x == -1) {
786 last_x = x;
788 vnc_clear_bit(vs->dirty_row[y], x);
789 } else {
790 if (last_x != -1) {
791 int h = find_dirty_height(vs, y, last_x, x);
792 send_framebuffer_update(vs, last_x * 16, y, (x - last_x) * 16, h);
793 n_rectangles++;
795 last_x = -1;
798 if (last_x != -1) {
799 int h = find_dirty_height(vs, y, last_x, x);
800 send_framebuffer_update(vs, last_x * 16, y, (x - last_x) * 16, h);
801 n_rectangles++;
804 vs->output.buffer[saved_offset] = (n_rectangles >> 8) & 0xFF;
805 vs->output.buffer[saved_offset + 1] = n_rectangles & 0xFF;
806 vnc_flush(vs);
810 if (vs->csock != -1) {
811 qemu_mod_timer(vs->timer, qemu_get_clock(rt_clock) + VNC_REFRESH_INTERVAL);
812 } else {
813 vnc_disconnect_finish(vs);
818 /* audio */
819 static void audio_capture_notify(void *opaque, audcnotification_e cmd)
821 VncState *vs = opaque;
823 switch (cmd) {
824 case AUD_CNOTIFY_DISABLE:
825 vnc_write_u8(vs, 255);
826 vnc_write_u8(vs, 1);
827 vnc_write_u16(vs, 0);
828 vnc_flush(vs);
829 break;
831 case AUD_CNOTIFY_ENABLE:
832 vnc_write_u8(vs, 255);
833 vnc_write_u8(vs, 1);
834 vnc_write_u16(vs, 1);
835 vnc_flush(vs);
836 break;
840 static void audio_capture_destroy(void *opaque)
844 static void audio_capture(void *opaque, void *buf, int size)
846 VncState *vs = opaque;
848 vnc_write_u8(vs, 255);
849 vnc_write_u8(vs, 1);
850 vnc_write_u16(vs, 2);
851 vnc_write_u32(vs, size);
852 vnc_write(vs, buf, size);
853 vnc_flush(vs);
856 static void audio_add(VncState *vs)
858 struct audio_capture_ops ops;
860 if (vs->audio_cap) {
861 term_printf ("audio already running\n");
862 return;
865 ops.notify = audio_capture_notify;
866 ops.destroy = audio_capture_destroy;
867 ops.capture = audio_capture;
869 vs->audio_cap = AUD_add_capture(NULL, &vs->as, &ops, vs);
870 if (!vs->audio_cap) {
871 term_printf ("Failed to add audio capture\n");
875 static void audio_del(VncState *vs)
877 if (vs->audio_cap) {
878 AUD_del_capture(vs->audio_cap, vs);
879 vs->audio_cap = NULL;
883 static void vnc_disconnect_start(VncState *vs)
885 if (vs->csock == -1)
886 return;
887 qemu_set_fd_handler2(vs->csock, NULL, NULL, NULL, NULL);
888 closesocket(vs->csock);
889 vs->csock = -1;
892 static void vnc_disconnect_finish(VncState *vs)
894 qemu_del_timer(vs->timer);
895 qemu_free_timer(vs->timer);
896 if (vs->input.buffer) qemu_free(vs->input.buffer);
897 if (vs->output.buffer) qemu_free(vs->output.buffer);
898 #ifdef CONFIG_VNC_TLS
899 vnc_tls_client_cleanup(vs);
900 #endif /* CONFIG_VNC_TLS */
901 #ifdef CONFIG_VNC_SASL
902 vnc_sasl_client_cleanup(vs);
903 #endif /* CONFIG_VNC_SASL */
904 audio_del(vs);
906 VncState *p, *parent = NULL;
907 for (p = vs->vd->clients; p != NULL; p = p->next) {
908 if (p == vs) {
909 if (parent)
910 parent->next = p->next;
911 else
912 vs->vd->clients = p->next;
913 break;
915 parent = p;
917 if (!vs->vd->clients)
918 dcl->idle = 1;
920 qemu_free(vs->old_data);
921 qemu_free(vs);
924 int vnc_client_io_error(VncState *vs, int ret, int last_errno)
926 if (ret == 0 || ret == -1) {
927 if (ret == -1) {
928 switch (last_errno) {
929 case EINTR:
930 case EAGAIN:
931 #ifdef _WIN32
932 case WSAEWOULDBLOCK:
933 #endif
934 return 0;
935 default:
936 break;
940 VNC_DEBUG("Closing down client sock %d %d\n", ret, ret < 0 ? last_errno : 0);
941 vnc_disconnect_start(vs);
943 return 0;
945 return ret;
949 void vnc_client_error(VncState *vs)
951 VNC_DEBUG("Closing down client sock: protocol error\n");
952 vnc_disconnect_start(vs);
957 * Called to write a chunk of data to the client socket. The data may
958 * be the raw data, or may have already been encoded by SASL.
959 * The data will be written either straight onto the socket, or
960 * written via the GNUTLS wrappers, if TLS/SSL encryption is enabled
962 * NB, it is theoretically possible to have 2 layers of encryption,
963 * both SASL, and this TLS layer. It is highly unlikely in practice
964 * though, since SASL encryption will typically be a no-op if TLS
965 * is active
967 * Returns the number of bytes written, which may be less than
968 * the requested 'datalen' if the socket would block. Returns
969 * -1 on error, and disconnects the client socket.
971 long vnc_client_write_buf(VncState *vs, const uint8_t *data, size_t datalen)
973 long ret;
974 #ifdef CONFIG_VNC_TLS
975 if (vs->tls.session) {
976 ret = gnutls_write(vs->tls.session, data, datalen);
977 if (ret < 0) {
978 if (ret == GNUTLS_E_AGAIN)
979 errno = EAGAIN;
980 else
981 errno = EIO;
982 ret = -1;
984 } else
985 #endif /* CONFIG_VNC_TLS */
986 ret = send(vs->csock, data, datalen, 0);
987 VNC_DEBUG("Wrote wire %p %d -> %ld\n", data, datalen, ret);
988 return vnc_client_io_error(vs, ret, socket_error());
993 * Called to write buffered data to the client socket, when not
994 * using any SASL SSF encryption layers. Will write as much data
995 * as possible without blocking. If all buffered data is written,
996 * will switch the FD poll() handler back to read monitoring.
998 * Returns the number of bytes written, which may be less than
999 * the buffered output data if the socket would block. Returns
1000 * -1 on error, and disconnects the client socket.
1002 static long vnc_client_write_plain(VncState *vs)
1004 long ret;
1006 #ifdef CONFIG_VNC_SASL
1007 VNC_DEBUG("Write Plain: Pending output %p size %d offset %d. Wait SSF %d\n",
1008 vs->output.buffer, vs->output.capacity, vs->output.offset,
1009 vs->sasl.waitWriteSSF);
1011 if (vs->sasl.conn &&
1012 vs->sasl.runSSF &&
1013 vs->sasl.waitWriteSSF) {
1014 ret = vnc_client_write_buf(vs, vs->output.buffer, vs->sasl.waitWriteSSF);
1015 if (ret)
1016 vs->sasl.waitWriteSSF -= ret;
1017 } else
1018 #endif /* CONFIG_VNC_SASL */
1019 ret = vnc_client_write_buf(vs, vs->output.buffer, vs->output.offset);
1020 if (!ret)
1021 return 0;
1023 memmove(vs->output.buffer, vs->output.buffer + ret, (vs->output.offset - ret));
1024 vs->output.offset -= ret;
1026 if (vs->output.offset == 0) {
1027 qemu_set_fd_handler2(vs->csock, NULL, vnc_client_read, NULL, vs);
1030 return ret;
1035 * First function called whenever there is data to be written to
1036 * the client socket. Will delegate actual work according to whether
1037 * SASL SSF layers are enabled (thus requiring encryption calls)
1039 void vnc_client_write(void *opaque)
1041 long ret;
1042 VncState *vs = opaque;
1044 #ifdef CONFIG_VNC_SASL
1045 if (vs->sasl.conn &&
1046 vs->sasl.runSSF &&
1047 !vs->sasl.waitWriteSSF)
1048 ret = vnc_client_write_sasl(vs);
1049 else
1050 #endif /* CONFIG_VNC_SASL */
1051 ret = vnc_client_write_plain(vs);
1054 void vnc_read_when(VncState *vs, VncReadEvent *func, size_t expecting)
1056 vs->read_handler = func;
1057 vs->read_handler_expect = expecting;
1062 * Called to read a chunk of data from the client socket. The data may
1063 * be the raw data, or may need to be further decoded by SASL.
1064 * The data will be read either straight from to the socket, or
1065 * read via the GNUTLS wrappers, if TLS/SSL encryption is enabled
1067 * NB, it is theoretically possible to have 2 layers of encryption,
1068 * both SASL, and this TLS layer. It is highly unlikely in practice
1069 * though, since SASL encryption will typically be a no-op if TLS
1070 * is active
1072 * Returns the number of bytes read, which may be less than
1073 * the requested 'datalen' if the socket would block. Returns
1074 * -1 on error, and disconnects the client socket.
1076 long vnc_client_read_buf(VncState *vs, uint8_t *data, size_t datalen)
1078 long ret;
1079 #ifdef CONFIG_VNC_TLS
1080 if (vs->tls.session) {
1081 ret = gnutls_read(vs->tls.session, data, datalen);
1082 if (ret < 0) {
1083 if (ret == GNUTLS_E_AGAIN)
1084 errno = EAGAIN;
1085 else
1086 errno = EIO;
1087 ret = -1;
1089 } else
1090 #endif /* CONFIG_VNC_TLS */
1091 ret = recv(vs->csock, data, datalen, 0);
1092 VNC_DEBUG("Read wire %p %d -> %ld\n", data, datalen, ret);
1093 return vnc_client_io_error(vs, ret, socket_error());
1098 * Called to read data from the client socket to the input buffer,
1099 * when not using any SASL SSF encryption layers. Will read as much
1100 * data as possible without blocking.
1102 * Returns the number of bytes read. Returns -1 on error, and
1103 * disconnects the client socket.
1105 static long vnc_client_read_plain(VncState *vs)
1107 int ret;
1108 VNC_DEBUG("Read plain %p size %d offset %d\n",
1109 vs->input.buffer, vs->input.capacity, vs->input.offset);
1110 buffer_reserve(&vs->input, 4096);
1111 ret = vnc_client_read_buf(vs, buffer_end(&vs->input), 4096);
1112 if (!ret)
1113 return 0;
1114 vs->input.offset += ret;
1115 return ret;
1120 * First function called whenever there is more data to be read from
1121 * the client socket. Will delegate actual work according to whether
1122 * SASL SSF layers are enabled (thus requiring decryption calls)
1124 void vnc_client_read(void *opaque)
1126 VncState *vs = opaque;
1127 long ret;
1129 #ifdef CONFIG_VNC_SASL
1130 if (vs->sasl.conn && vs->sasl.runSSF)
1131 ret = vnc_client_read_sasl(vs);
1132 else
1133 #endif /* CONFIG_VNC_SASL */
1134 ret = vnc_client_read_plain(vs);
1135 if (!ret) {
1136 if (vs->csock == -1)
1137 vnc_disconnect_finish(vs);
1138 return;
1141 while (vs->read_handler && vs->input.offset >= vs->read_handler_expect) {
1142 size_t len = vs->read_handler_expect;
1143 int ret;
1145 ret = vs->read_handler(vs, vs->input.buffer, len);
1146 if (vs->csock == -1) {
1147 vnc_disconnect_finish(vs);
1148 return;
1151 if (!ret) {
1152 memmove(vs->input.buffer, vs->input.buffer + len, (vs->input.offset - len));
1153 vs->input.offset -= len;
1154 } else {
1155 vs->read_handler_expect = ret;
1160 void vnc_write(VncState *vs, const void *data, size_t len)
1162 buffer_reserve(&vs->output, len);
1164 if (vs->csock != -1 && buffer_empty(&vs->output)) {
1165 qemu_set_fd_handler2(vs->csock, NULL, vnc_client_read, vnc_client_write, vs);
1168 buffer_append(&vs->output, data, len);
1171 void vnc_write_s32(VncState *vs, int32_t value)
1173 vnc_write_u32(vs, *(uint32_t *)&value);
1176 void vnc_write_u32(VncState *vs, uint32_t value)
1178 uint8_t buf[4];
1180 buf[0] = (value >> 24) & 0xFF;
1181 buf[1] = (value >> 16) & 0xFF;
1182 buf[2] = (value >> 8) & 0xFF;
1183 buf[3] = value & 0xFF;
1185 vnc_write(vs, buf, 4);
1188 void vnc_write_u16(VncState *vs, uint16_t value)
1190 uint8_t buf[2];
1192 buf[0] = (value >> 8) & 0xFF;
1193 buf[1] = value & 0xFF;
1195 vnc_write(vs, buf, 2);
1198 void vnc_write_u8(VncState *vs, uint8_t value)
1200 vnc_write(vs, (char *)&value, 1);
1203 void vnc_flush(VncState *vs)
1205 if (vs->csock != -1 && vs->output.offset)
1206 vnc_client_write(vs);
1209 uint8_t read_u8(uint8_t *data, size_t offset)
1211 return data[offset];
1214 uint16_t read_u16(uint8_t *data, size_t offset)
1216 return ((data[offset] & 0xFF) << 8) | (data[offset + 1] & 0xFF);
1219 int32_t read_s32(uint8_t *data, size_t offset)
1221 return (int32_t)((data[offset] << 24) | (data[offset + 1] << 16) |
1222 (data[offset + 2] << 8) | data[offset + 3]);
1225 uint32_t read_u32(uint8_t *data, size_t offset)
1227 return ((data[offset] << 24) | (data[offset + 1] << 16) |
1228 (data[offset + 2] << 8) | data[offset + 3]);
1231 static void client_cut_text(VncState *vs, size_t len, uint8_t *text)
1235 static void check_pointer_type_change(VncState *vs, int absolute)
1237 if (vnc_has_feature(vs, VNC_FEATURE_POINTER_TYPE_CHANGE) && vs->absolute != absolute) {
1238 vnc_write_u8(vs, 0);
1239 vnc_write_u8(vs, 0);
1240 vnc_write_u16(vs, 1);
1241 vnc_framebuffer_update(vs, absolute, 0,
1242 ds_get_width(vs->ds), ds_get_height(vs->ds),
1243 VNC_ENCODING_POINTER_TYPE_CHANGE);
1244 vnc_flush(vs);
1246 vs->absolute = absolute;
1249 static void pointer_event(VncState *vs, int button_mask, int x, int y)
1251 int buttons = 0;
1252 int dz = 0;
1254 if (button_mask & 0x01)
1255 buttons |= MOUSE_EVENT_LBUTTON;
1256 if (button_mask & 0x02)
1257 buttons |= MOUSE_EVENT_MBUTTON;
1258 if (button_mask & 0x04)
1259 buttons |= MOUSE_EVENT_RBUTTON;
1260 if (button_mask & 0x08)
1261 dz = -1;
1262 if (button_mask & 0x10)
1263 dz = 1;
1265 if (vs->absolute) {
1266 kbd_mouse_event(x * 0x7FFF / (ds_get_width(vs->ds) - 1),
1267 y * 0x7FFF / (ds_get_height(vs->ds) - 1),
1268 dz, buttons);
1269 } else if (vnc_has_feature(vs, VNC_FEATURE_POINTER_TYPE_CHANGE)) {
1270 x -= 0x7FFF;
1271 y -= 0x7FFF;
1273 kbd_mouse_event(x, y, dz, buttons);
1274 } else {
1275 if (vs->last_x != -1)
1276 kbd_mouse_event(x - vs->last_x,
1277 y - vs->last_y,
1278 dz, buttons);
1279 vs->last_x = x;
1280 vs->last_y = y;
1283 check_pointer_type_change(vs, kbd_mouse_is_absolute());
1286 static void reset_keys(VncState *vs)
1288 int i;
1289 for(i = 0; i < 256; i++) {
1290 if (vs->modifiers_state[i]) {
1291 if (i & 0x80)
1292 kbd_put_keycode(0xe0);
1293 kbd_put_keycode(i | 0x80);
1294 vs->modifiers_state[i] = 0;
1299 static void press_key(VncState *vs, int keysym)
1301 kbd_put_keycode(keysym2scancode(vs->vd->kbd_layout, keysym) & 0x7f);
1302 kbd_put_keycode(keysym2scancode(vs->vd->kbd_layout, keysym) | 0x80);
1305 static void do_key_event(VncState *vs, int down, int keycode, int sym)
1307 /* QEMU console switch */
1308 switch(keycode) {
1309 case 0x2a: /* Left Shift */
1310 case 0x36: /* Right Shift */
1311 case 0x1d: /* Left CTRL */
1312 case 0x9d: /* Right CTRL */
1313 case 0x38: /* Left ALT */
1314 case 0xb8: /* Right ALT */
1315 if (down)
1316 vs->modifiers_state[keycode] = 1;
1317 else
1318 vs->modifiers_state[keycode] = 0;
1319 break;
1320 case 0x02 ... 0x0a: /* '1' to '9' keys */
1321 if (down && vs->modifiers_state[0x1d] && vs->modifiers_state[0x38]) {
1322 /* Reset the modifiers sent to the current console */
1323 reset_keys(vs);
1324 console_select(keycode - 0x02);
1325 return;
1327 break;
1328 case 0x3a: /* CapsLock */
1329 case 0x45: /* NumLock */
1330 if (!down)
1331 vs->modifiers_state[keycode] ^= 1;
1332 break;
1335 if (keycode_is_keypad(vs->vd->kbd_layout, keycode)) {
1336 /* If the numlock state needs to change then simulate an additional
1337 keypress before sending this one. This will happen if the user
1338 toggles numlock away from the VNC window.
1340 if (keysym_is_numlock(vs->vd->kbd_layout, sym & 0xFFFF)) {
1341 if (!vs->modifiers_state[0x45]) {
1342 vs->modifiers_state[0x45] = 1;
1343 press_key(vs, 0xff7f);
1345 } else {
1346 if (vs->modifiers_state[0x45]) {
1347 vs->modifiers_state[0x45] = 0;
1348 press_key(vs, 0xff7f);
1353 if (is_graphic_console()) {
1354 if (keycode & 0x80)
1355 kbd_put_keycode(0xe0);
1356 if (down)
1357 kbd_put_keycode(keycode & 0x7f);
1358 else
1359 kbd_put_keycode(keycode | 0x80);
1360 } else {
1361 /* QEMU console emulation */
1362 if (down) {
1363 switch (keycode) {
1364 case 0x2a: /* Left Shift */
1365 case 0x36: /* Right Shift */
1366 case 0x1d: /* Left CTRL */
1367 case 0x9d: /* Right CTRL */
1368 case 0x38: /* Left ALT */
1369 case 0xb8: /* Right ALT */
1370 break;
1371 case 0xc8:
1372 case 0x48:
1373 kbd_put_keysym(QEMU_KEY_UP);
1374 break;
1375 case 0xd0:
1376 case 0x50:
1377 kbd_put_keysym(QEMU_KEY_DOWN);
1378 break;
1379 case 0xcb:
1380 case 0x4b:
1381 kbd_put_keysym(QEMU_KEY_LEFT);
1382 break;
1383 case 0xcd:
1384 case 0x4d:
1385 kbd_put_keysym(QEMU_KEY_RIGHT);
1386 break;
1387 case 0xd3:
1388 case 0x53:
1389 kbd_put_keysym(QEMU_KEY_DELETE);
1390 break;
1391 case 0xc7:
1392 case 0x47:
1393 kbd_put_keysym(QEMU_KEY_HOME);
1394 break;
1395 case 0xcf:
1396 case 0x4f:
1397 kbd_put_keysym(QEMU_KEY_END);
1398 break;
1399 case 0xc9:
1400 case 0x49:
1401 kbd_put_keysym(QEMU_KEY_PAGEUP);
1402 break;
1403 case 0xd1:
1404 case 0x51:
1405 kbd_put_keysym(QEMU_KEY_PAGEDOWN);
1406 break;
1407 default:
1408 kbd_put_keysym(sym);
1409 break;
1415 static void key_event(VncState *vs, int down, uint32_t sym)
1417 int keycode;
1419 if (sym >= 'A' && sym <= 'Z' && is_graphic_console())
1420 sym = sym - 'A' + 'a';
1422 keycode = keysym2scancode(vs->vd->kbd_layout, sym & 0xFFFF);
1423 do_key_event(vs, down, keycode, sym);
1426 static void ext_key_event(VncState *vs, int down,
1427 uint32_t sym, uint16_t keycode)
1429 /* if the user specifies a keyboard layout, always use it */
1430 if (keyboard_layout)
1431 key_event(vs, down, sym);
1432 else
1433 do_key_event(vs, down, keycode, sym);
1436 static void framebuffer_update_request(VncState *vs, int incremental,
1437 int x_position, int y_position,
1438 int w, int h)
1440 if (x_position > ds_get_width(vs->ds))
1441 x_position = ds_get_width(vs->ds);
1442 if (y_position > ds_get_height(vs->ds))
1443 y_position = ds_get_height(vs->ds);
1444 if (x_position + w >= ds_get_width(vs->ds))
1445 w = ds_get_width(vs->ds) - x_position;
1446 if (y_position + h >= ds_get_height(vs->ds))
1447 h = ds_get_height(vs->ds) - y_position;
1449 int i;
1450 vs->need_update = 1;
1451 if (!incremental) {
1452 char *old_row = vs->old_data + y_position * ds_get_linesize(vs->ds);
1454 for (i = 0; i < h; i++) {
1455 vnc_set_bits(vs->dirty_row[y_position + i],
1456 (ds_get_width(vs->ds) / 16), VNC_DIRTY_WORDS);
1457 memset(old_row, 42, ds_get_width(vs->ds) * ds_get_bytes_per_pixel(vs->ds));
1458 old_row += ds_get_linesize(vs->ds);
1463 static void send_ext_key_event_ack(VncState *vs)
1465 vnc_write_u8(vs, 0);
1466 vnc_write_u8(vs, 0);
1467 vnc_write_u16(vs, 1);
1468 vnc_framebuffer_update(vs, 0, 0, ds_get_width(vs->ds), ds_get_height(vs->ds),
1469 VNC_ENCODING_EXT_KEY_EVENT);
1470 vnc_flush(vs);
1473 static void send_ext_audio_ack(VncState *vs)
1475 vnc_write_u8(vs, 0);
1476 vnc_write_u8(vs, 0);
1477 vnc_write_u16(vs, 1);
1478 vnc_framebuffer_update(vs, 0, 0, ds_get_width(vs->ds), ds_get_height(vs->ds),
1479 VNC_ENCODING_AUDIO);
1480 vnc_flush(vs);
1483 static void set_encodings(VncState *vs, int32_t *encodings, size_t n_encodings)
1485 int i;
1486 unsigned int enc = 0;
1488 vnc_zlib_init(vs);
1489 vs->features = 0;
1490 vs->vnc_encoding = 0;
1491 vs->tight_compression = 9;
1492 vs->tight_quality = 9;
1493 vs->absolute = -1;
1495 for (i = n_encodings - 1; i >= 0; i--) {
1496 enc = encodings[i];
1497 switch (enc) {
1498 case VNC_ENCODING_RAW:
1499 vs->vnc_encoding = enc;
1500 break;
1501 case VNC_ENCODING_COPYRECT:
1502 /* Our CopyRect handling still seems to be broken, see:
1504 * https://bugzilla.redhat.com/503156
1507 #ifdef FIXME
1508 vs->features |= VNC_FEATURE_COPYRECT_MASK;
1509 #endif
1510 break;
1511 case VNC_ENCODING_HEXTILE:
1512 vs->features |= VNC_FEATURE_HEXTILE_MASK;
1513 vs->vnc_encoding = enc;
1514 break;
1515 case VNC_ENCODING_ZLIB:
1516 vs->features |= VNC_FEATURE_ZLIB_MASK;
1517 vs->vnc_encoding = enc;
1518 break;
1519 case VNC_ENCODING_DESKTOPRESIZE:
1520 vs->features |= VNC_FEATURE_RESIZE_MASK;
1521 break;
1522 case VNC_ENCODING_POINTER_TYPE_CHANGE:
1523 vs->features |= VNC_FEATURE_POINTER_TYPE_CHANGE_MASK;
1524 break;
1525 case VNC_ENCODING_EXT_KEY_EVENT:
1526 send_ext_key_event_ack(vs);
1527 break;
1528 case VNC_ENCODING_AUDIO:
1529 send_ext_audio_ack(vs);
1530 break;
1531 case VNC_ENCODING_WMVi:
1532 vs->features |= VNC_FEATURE_WMVI_MASK;
1533 break;
1534 case VNC_ENCODING_COMPRESSLEVEL0 ... VNC_ENCODING_COMPRESSLEVEL0 + 9:
1535 vs->tight_compression = (enc & 0x0F);
1536 break;
1537 case VNC_ENCODING_QUALITYLEVEL0 ... VNC_ENCODING_QUALITYLEVEL0 + 9:
1538 vs->tight_quality = (enc & 0x0F);
1539 break;
1540 default:
1541 VNC_DEBUG("Unknown encoding: %d (0x%.8x): %d\n", i, enc, enc);
1542 break;
1546 check_pointer_type_change(vs, kbd_mouse_is_absolute());
1549 static void set_pixel_conversion(VncState *vs)
1551 if ((vs->clientds.flags & QEMU_BIG_ENDIAN_FLAG) ==
1552 (vs->ds->surface->flags & QEMU_BIG_ENDIAN_FLAG) &&
1553 !memcmp(&(vs->clientds.pf), &(vs->ds->surface->pf), sizeof(PixelFormat))) {
1554 vs->write_pixels = vnc_write_pixels_copy;
1555 switch (vs->ds->surface->pf.bits_per_pixel) {
1556 case 8:
1557 vs->send_hextile_tile = send_hextile_tile_8;
1558 break;
1559 case 16:
1560 vs->send_hextile_tile = send_hextile_tile_16;
1561 break;
1562 case 32:
1563 vs->send_hextile_tile = send_hextile_tile_32;
1564 break;
1566 } else {
1567 vs->write_pixels = vnc_write_pixels_generic;
1568 switch (vs->ds->surface->pf.bits_per_pixel) {
1569 case 8:
1570 vs->send_hextile_tile = send_hextile_tile_generic_8;
1571 break;
1572 case 16:
1573 vs->send_hextile_tile = send_hextile_tile_generic_16;
1574 break;
1575 case 32:
1576 vs->send_hextile_tile = send_hextile_tile_generic_32;
1577 break;
1582 static void set_pixel_format(VncState *vs,
1583 int bits_per_pixel, int depth,
1584 int big_endian_flag, int true_color_flag,
1585 int red_max, int green_max, int blue_max,
1586 int red_shift, int green_shift, int blue_shift)
1588 if (!true_color_flag) {
1589 vnc_client_error(vs);
1590 return;
1593 vs->clientds = vs->serverds;
1594 vs->clientds.pf.rmax = red_max;
1595 count_bits(vs->clientds.pf.rbits, red_max);
1596 vs->clientds.pf.rshift = red_shift;
1597 vs->clientds.pf.rmask = red_max << red_shift;
1598 vs->clientds.pf.gmax = green_max;
1599 count_bits(vs->clientds.pf.gbits, green_max);
1600 vs->clientds.pf.gshift = green_shift;
1601 vs->clientds.pf.gmask = green_max << green_shift;
1602 vs->clientds.pf.bmax = blue_max;
1603 count_bits(vs->clientds.pf.bbits, blue_max);
1604 vs->clientds.pf.bshift = blue_shift;
1605 vs->clientds.pf.bmask = blue_max << blue_shift;
1606 vs->clientds.pf.bits_per_pixel = bits_per_pixel;
1607 vs->clientds.pf.bytes_per_pixel = bits_per_pixel / 8;
1608 vs->clientds.pf.depth = bits_per_pixel == 32 ? 24 : bits_per_pixel;
1609 vs->clientds.flags = big_endian_flag ? QEMU_BIG_ENDIAN_FLAG : 0x00;
1611 set_pixel_conversion(vs);
1613 vga_hw_invalidate();
1614 vga_hw_update();
1617 static void pixel_format_message (VncState *vs) {
1618 char pad[3] = { 0, 0, 0 };
1620 vnc_write_u8(vs, vs->ds->surface->pf.bits_per_pixel); /* bits-per-pixel */
1621 vnc_write_u8(vs, vs->ds->surface->pf.depth); /* depth */
1623 #ifdef WORDS_BIGENDIAN
1624 vnc_write_u8(vs, 1); /* big-endian-flag */
1625 #else
1626 vnc_write_u8(vs, 0); /* big-endian-flag */
1627 #endif
1628 vnc_write_u8(vs, 1); /* true-color-flag */
1629 vnc_write_u16(vs, vs->ds->surface->pf.rmax); /* red-max */
1630 vnc_write_u16(vs, vs->ds->surface->pf.gmax); /* green-max */
1631 vnc_write_u16(vs, vs->ds->surface->pf.bmax); /* blue-max */
1632 vnc_write_u8(vs, vs->ds->surface->pf.rshift); /* red-shift */
1633 vnc_write_u8(vs, vs->ds->surface->pf.gshift); /* green-shift */
1634 vnc_write_u8(vs, vs->ds->surface->pf.bshift); /* blue-shift */
1635 if (vs->ds->surface->pf.bits_per_pixel == 32)
1636 vs->send_hextile_tile = send_hextile_tile_32;
1637 else if (vs->ds->surface->pf.bits_per_pixel == 16)
1638 vs->send_hextile_tile = send_hextile_tile_16;
1639 else if (vs->ds->surface->pf.bits_per_pixel == 8)
1640 vs->send_hextile_tile = send_hextile_tile_8;
1641 vs->clientds = *(vs->ds->surface);
1642 vs->clientds.flags |= ~QEMU_ALLOCATED_FLAG;
1643 vs->write_pixels = vnc_write_pixels_copy;
1645 vnc_write(vs, pad, 3); /* padding */
1648 static void vnc_dpy_setdata(DisplayState *ds)
1650 /* We don't have to do anything */
1653 static void vnc_colordepth(VncState *vs)
1655 if (vnc_has_feature(vs, VNC_FEATURE_WMVI)) {
1656 /* Sending a WMVi message to notify the client*/
1657 vnc_write_u8(vs, 0); /* msg id */
1658 vnc_write_u8(vs, 0);
1659 vnc_write_u16(vs, 1); /* number of rects */
1660 vnc_framebuffer_update(vs, 0, 0, ds_get_width(vs->ds),
1661 ds_get_height(vs->ds), VNC_ENCODING_WMVi);
1662 pixel_format_message(vs);
1663 vnc_flush(vs);
1664 } else {
1665 set_pixel_conversion(vs);
1669 static int protocol_client_msg(VncState *vs, uint8_t *data, size_t len)
1671 int i;
1672 uint16_t limit;
1674 switch (data[0]) {
1675 case 0:
1676 if (len == 1)
1677 return 20;
1679 set_pixel_format(vs, read_u8(data, 4), read_u8(data, 5),
1680 read_u8(data, 6), read_u8(data, 7),
1681 read_u16(data, 8), read_u16(data, 10),
1682 read_u16(data, 12), read_u8(data, 14),
1683 read_u8(data, 15), read_u8(data, 16));
1684 break;
1685 case 2:
1686 if (len == 1)
1687 return 4;
1689 if (len == 4) {
1690 limit = read_u16(data, 2);
1691 if (limit > 0)
1692 return 4 + (limit * 4);
1693 } else
1694 limit = read_u16(data, 2);
1696 for (i = 0; i < limit; i++) {
1697 int32_t val = read_s32(data, 4 + (i * 4));
1698 memcpy(data + 4 + (i * 4), &val, sizeof(val));
1701 set_encodings(vs, (int32_t *)(data + 4), limit);
1702 break;
1703 case 3:
1704 if (len == 1)
1705 return 10;
1707 framebuffer_update_request(vs,
1708 read_u8(data, 1), read_u16(data, 2), read_u16(data, 4),
1709 read_u16(data, 6), read_u16(data, 8));
1710 break;
1711 case 4:
1712 if (len == 1)
1713 return 8;
1715 key_event(vs, read_u8(data, 1), read_u32(data, 4));
1716 break;
1717 case 5:
1718 if (len == 1)
1719 return 6;
1721 pointer_event(vs, read_u8(data, 1), read_u16(data, 2), read_u16(data, 4));
1722 break;
1723 case 6:
1724 if (len == 1)
1725 return 8;
1727 if (len == 8) {
1728 uint32_t dlen = read_u32(data, 4);
1729 if (dlen > 0)
1730 return 8 + dlen;
1733 client_cut_text(vs, read_u32(data, 4), data + 8);
1734 break;
1735 case 255:
1736 if (len == 1)
1737 return 2;
1739 switch (read_u8(data, 1)) {
1740 case 0:
1741 if (len == 2)
1742 return 12;
1744 ext_key_event(vs, read_u16(data, 2),
1745 read_u32(data, 4), read_u32(data, 8));
1746 break;
1747 case 1:
1748 if (len == 2)
1749 return 4;
1751 switch (read_u16 (data, 2)) {
1752 case 0:
1753 audio_add(vs);
1754 break;
1755 case 1:
1756 audio_del(vs);
1757 break;
1758 case 2:
1759 if (len == 4)
1760 return 10;
1761 switch (read_u8(data, 4)) {
1762 case 0: vs->as.fmt = AUD_FMT_U8; break;
1763 case 1: vs->as.fmt = AUD_FMT_S8; break;
1764 case 2: vs->as.fmt = AUD_FMT_U16; break;
1765 case 3: vs->as.fmt = AUD_FMT_S16; break;
1766 case 4: vs->as.fmt = AUD_FMT_U32; break;
1767 case 5: vs->as.fmt = AUD_FMT_S32; break;
1768 default:
1769 printf("Invalid audio format %d\n", read_u8(data, 4));
1770 vnc_client_error(vs);
1771 break;
1773 vs->as.nchannels = read_u8(data, 5);
1774 if (vs->as.nchannels != 1 && vs->as.nchannels != 2) {
1775 printf("Invalid audio channel coount %d\n",
1776 read_u8(data, 5));
1777 vnc_client_error(vs);
1778 break;
1780 vs->as.freq = read_u32(data, 6);
1781 break;
1782 default:
1783 printf ("Invalid audio message %d\n", read_u8(data, 4));
1784 vnc_client_error(vs);
1785 break;
1787 break;
1789 default:
1790 printf("Msg: %d\n", read_u16(data, 0));
1791 vnc_client_error(vs);
1792 break;
1794 break;
1795 default:
1796 printf("Msg: %d\n", data[0]);
1797 vnc_client_error(vs);
1798 break;
1801 vnc_read_when(vs, protocol_client_msg, 1);
1802 return 0;
1805 static int protocol_client_init(VncState *vs, uint8_t *data, size_t len)
1807 char buf[1024];
1808 int size;
1810 vnc_write_u16(vs, ds_get_width(vs->ds));
1811 vnc_write_u16(vs, ds_get_height(vs->ds));
1813 pixel_format_message(vs);
1815 if (qemu_name)
1816 size = snprintf(buf, sizeof(buf), "QEMU (%s)", qemu_name);
1817 else
1818 size = snprintf(buf, sizeof(buf), "QEMU");
1820 vnc_write_u32(vs, size);
1821 vnc_write(vs, buf, size);
1822 vnc_flush(vs);
1824 vnc_read_when(vs, protocol_client_msg, 1);
1826 return 0;
1829 void start_client_init(VncState *vs)
1831 vnc_read_when(vs, protocol_client_init, 1);
1834 static void make_challenge(VncState *vs)
1836 int i;
1838 srand(time(NULL)+getpid()+getpid()*987654+rand());
1840 for (i = 0 ; i < sizeof(vs->challenge) ; i++)
1841 vs->challenge[i] = (int) (256.0*rand()/(RAND_MAX+1.0));
1844 static int protocol_client_auth_vnc(VncState *vs, uint8_t *data, size_t len)
1846 unsigned char response[VNC_AUTH_CHALLENGE_SIZE];
1847 int i, j, pwlen;
1848 unsigned char key[8];
1850 if (!vs->vd->password || !vs->vd->password[0]) {
1851 VNC_DEBUG("No password configured on server");
1852 vnc_write_u32(vs, 1); /* Reject auth */
1853 if (vs->minor >= 8) {
1854 static const char err[] = "Authentication failed";
1855 vnc_write_u32(vs, sizeof(err));
1856 vnc_write(vs, err, sizeof(err));
1858 vnc_flush(vs);
1859 vnc_client_error(vs);
1860 return 0;
1863 memcpy(response, vs->challenge, VNC_AUTH_CHALLENGE_SIZE);
1865 /* Calculate the expected challenge response */
1866 pwlen = strlen(vs->vd->password);
1867 for (i=0; i<sizeof(key); i++)
1868 key[i] = i<pwlen ? vs->vd->password[i] : 0;
1869 deskey(key, EN0);
1870 for (j = 0; j < VNC_AUTH_CHALLENGE_SIZE; j += 8)
1871 des(response+j, response+j);
1873 /* Compare expected vs actual challenge response */
1874 if (memcmp(response, data, VNC_AUTH_CHALLENGE_SIZE) != 0) {
1875 VNC_DEBUG("Client challenge reponse did not match\n");
1876 vnc_write_u32(vs, 1); /* Reject auth */
1877 if (vs->minor >= 8) {
1878 static const char err[] = "Authentication failed";
1879 vnc_write_u32(vs, sizeof(err));
1880 vnc_write(vs, err, sizeof(err));
1882 vnc_flush(vs);
1883 vnc_client_error(vs);
1884 } else {
1885 VNC_DEBUG("Accepting VNC challenge response\n");
1886 vnc_write_u32(vs, 0); /* Accept auth */
1887 vnc_flush(vs);
1889 start_client_init(vs);
1891 return 0;
1894 void start_auth_vnc(VncState *vs)
1896 make_challenge(vs);
1897 /* Send client a 'random' challenge */
1898 vnc_write(vs, vs->challenge, sizeof(vs->challenge));
1899 vnc_flush(vs);
1901 vnc_read_when(vs, protocol_client_auth_vnc, sizeof(vs->challenge));
1905 static int protocol_client_auth(VncState *vs, uint8_t *data, size_t len)
1907 /* We only advertise 1 auth scheme at a time, so client
1908 * must pick the one we sent. Verify this */
1909 if (data[0] != vs->vd->auth) { /* Reject auth */
1910 VNC_DEBUG("Reject auth %d because it didn't match advertized\n", (int)data[0]);
1911 vnc_write_u32(vs, 1);
1912 if (vs->minor >= 8) {
1913 static const char err[] = "Authentication failed";
1914 vnc_write_u32(vs, sizeof(err));
1915 vnc_write(vs, err, sizeof(err));
1917 vnc_client_error(vs);
1918 } else { /* Accept requested auth */
1919 VNC_DEBUG("Client requested auth %d\n", (int)data[0]);
1920 switch (vs->vd->auth) {
1921 case VNC_AUTH_NONE:
1922 VNC_DEBUG("Accept auth none\n");
1923 if (vs->minor >= 8) {
1924 vnc_write_u32(vs, 0); /* Accept auth completion */
1925 vnc_flush(vs);
1927 start_client_init(vs);
1928 break;
1930 case VNC_AUTH_VNC:
1931 VNC_DEBUG("Start VNC auth\n");
1932 start_auth_vnc(vs);
1933 break;
1935 #ifdef CONFIG_VNC_TLS
1936 case VNC_AUTH_VENCRYPT:
1937 VNC_DEBUG("Accept VeNCrypt auth\n");;
1938 start_auth_vencrypt(vs);
1939 break;
1940 #endif /* CONFIG_VNC_TLS */
1942 #ifdef CONFIG_VNC_SASL
1943 case VNC_AUTH_SASL:
1944 VNC_DEBUG("Accept SASL auth\n");
1945 start_auth_sasl(vs);
1946 break;
1947 #endif /* CONFIG_VNC_SASL */
1949 default: /* Should not be possible, but just in case */
1950 VNC_DEBUG("Reject auth %d server code bug\n", vs->vd->auth);
1951 vnc_write_u8(vs, 1);
1952 if (vs->minor >= 8) {
1953 static const char err[] = "Authentication failed";
1954 vnc_write_u32(vs, sizeof(err));
1955 vnc_write(vs, err, sizeof(err));
1957 vnc_client_error(vs);
1960 return 0;
1963 static int protocol_version(VncState *vs, uint8_t *version, size_t len)
1965 char local[13];
1967 memcpy(local, version, 12);
1968 local[12] = 0;
1970 if (sscanf(local, "RFB %03d.%03d\n", &vs->major, &vs->minor) != 2) {
1971 VNC_DEBUG("Malformed protocol version %s\n", local);
1972 vnc_client_error(vs);
1973 return 0;
1975 VNC_DEBUG("Client request protocol version %d.%d\n", vs->major, vs->minor);
1976 if (vs->major != 3 ||
1977 (vs->minor != 3 &&
1978 vs->minor != 4 &&
1979 vs->minor != 5 &&
1980 vs->minor != 7 &&
1981 vs->minor != 8)) {
1982 VNC_DEBUG("Unsupported client version\n");
1983 vnc_write_u32(vs, VNC_AUTH_INVALID);
1984 vnc_flush(vs);
1985 vnc_client_error(vs);
1986 return 0;
1988 /* Some broken clients report v3.4 or v3.5, which spec requires to be treated
1989 * as equivalent to v3.3 by servers
1991 if (vs->minor == 4 || vs->minor == 5)
1992 vs->minor = 3;
1994 if (vs->minor == 3) {
1995 if (vs->vd->auth == VNC_AUTH_NONE) {
1996 VNC_DEBUG("Tell client auth none\n");
1997 vnc_write_u32(vs, vs->vd->auth);
1998 vnc_flush(vs);
1999 start_client_init(vs);
2000 } else if (vs->vd->auth == VNC_AUTH_VNC) {
2001 VNC_DEBUG("Tell client VNC auth\n");
2002 vnc_write_u32(vs, vs->vd->auth);
2003 vnc_flush(vs);
2004 start_auth_vnc(vs);
2005 } else {
2006 VNC_DEBUG("Unsupported auth %d for protocol 3.3\n", vs->vd->auth);
2007 vnc_write_u32(vs, VNC_AUTH_INVALID);
2008 vnc_flush(vs);
2009 vnc_client_error(vs);
2011 } else {
2012 VNC_DEBUG("Telling client we support auth %d\n", vs->vd->auth);
2013 vnc_write_u8(vs, 1); /* num auth */
2014 vnc_write_u8(vs, vs->vd->auth);
2015 vnc_read_when(vs, protocol_client_auth, 1);
2016 vnc_flush(vs);
2019 return 0;
2022 static void vnc_connect(VncDisplay *vd, int csock)
2024 VncState *vs = qemu_mallocz(sizeof(VncState));
2025 vs->csock = csock;
2027 VNC_DEBUG("New client on socket %d\n", csock);
2028 dcl->idle = 0;
2029 socket_set_nonblock(vs->csock);
2030 qemu_set_fd_handler2(vs->csock, NULL, vnc_client_read, NULL, vs);
2032 vs->vd = vd;
2033 vs->ds = vd->ds;
2034 vs->timer = qemu_new_timer(rt_clock, vnc_update_client, vs);
2035 vs->last_x = -1;
2036 vs->last_y = -1;
2038 vs->as.freq = 44100;
2039 vs->as.nchannels = 2;
2040 vs->as.fmt = AUD_FMT_S16;
2041 vs->as.endianness = 0;
2043 vnc_resize(vs);
2044 vnc_write(vs, "RFB 003.008\n", 12);
2045 vnc_flush(vs);
2046 vnc_read_when(vs, protocol_version, 12);
2047 memset(vs->old_data, 0, ds_get_linesize(vs->ds) * ds_get_height(vs->ds));
2048 memset(vs->dirty_row, 0xFF, sizeof(vs->dirty_row));
2049 reset_keys(vs);
2051 vs->next = vd->clients;
2052 vd->clients = vs;
2054 vnc_update_client(vs);
2055 /* vs might be free()ed here */
2058 static void vnc_listen_read(void *opaque)
2060 VncDisplay *vs = opaque;
2061 struct sockaddr_in addr;
2062 socklen_t addrlen = sizeof(addr);
2064 /* Catch-up */
2065 vga_hw_update();
2067 int csock = accept(vs->lsock, (struct sockaddr *)&addr, &addrlen);
2068 if (csock != -1) {
2069 vnc_connect(vs, csock);
2073 void vnc_display_init(DisplayState *ds)
2075 VncDisplay *vs = qemu_mallocz(sizeof(*vs));
2077 dcl = qemu_mallocz(sizeof(DisplayChangeListener));
2079 ds->opaque = vs;
2080 dcl->idle = 1;
2081 vnc_display = vs;
2083 vs->lsock = -1;
2085 vs->ds = ds;
2087 if (keyboard_layout)
2088 vs->kbd_layout = init_keyboard_layout(name2keysym, keyboard_layout);
2089 else
2090 vs->kbd_layout = init_keyboard_layout(name2keysym, "en-us");
2092 if (!vs->kbd_layout)
2093 exit(1);
2095 dcl->dpy_copy = vnc_dpy_copy;
2096 dcl->dpy_update = vnc_dpy_update;
2097 dcl->dpy_resize = vnc_dpy_resize;
2098 dcl->dpy_setdata = vnc_dpy_setdata;
2099 register_displaychangelistener(ds, dcl);
2103 void vnc_display_close(DisplayState *ds)
2105 VncDisplay *vs = ds ? (VncDisplay *)ds->opaque : vnc_display;
2107 if (!vs)
2108 return;
2109 if (vs->display) {
2110 qemu_free(vs->display);
2111 vs->display = NULL;
2113 if (vs->lsock != -1) {
2114 qemu_set_fd_handler2(vs->lsock, NULL, NULL, NULL, NULL);
2115 close(vs->lsock);
2116 vs->lsock = -1;
2118 vs->auth = VNC_AUTH_INVALID;
2119 #ifdef CONFIG_VNC_TLS
2120 vs->subauth = VNC_AUTH_INVALID;
2121 vs->tls.x509verify = 0;
2122 #endif
2125 int vnc_display_password(DisplayState *ds, const char *password)
2127 VncDisplay *vs = ds ? (VncDisplay *)ds->opaque : vnc_display;
2129 if (vs->password) {
2130 qemu_free(vs->password);
2131 vs->password = NULL;
2133 if (password && password[0]) {
2134 if (!(vs->password = qemu_strdup(password)))
2135 return -1;
2138 return 0;
2141 int vnc_display_open(DisplayState *ds, const char *display)
2143 VncDisplay *vs = ds ? (VncDisplay *)ds->opaque : vnc_display;
2144 const char *options;
2145 int password = 0;
2146 int reverse = 0;
2147 int to_port = 0;
2148 #ifdef CONFIG_VNC_TLS
2149 int tls = 0, x509 = 0;
2150 #endif
2151 #ifdef CONFIG_VNC_SASL
2152 int sasl = 0;
2153 int saslErr;
2154 #endif
2155 int acl = 0;
2157 if (!vnc_display)
2158 return -1;
2159 vnc_display_close(ds);
2160 if (strcmp(display, "none") == 0)
2161 return 0;
2163 if (!(vs->display = strdup(display)))
2164 return -1;
2166 options = display;
2167 while ((options = strchr(options, ','))) {
2168 options++;
2169 if (strncmp(options, "password", 8) == 0) {
2170 password = 1; /* Require password auth */
2171 } else if (strncmp(options, "reverse", 7) == 0) {
2172 reverse = 1;
2173 } else if (strncmp(options, "to=", 3) == 0) {
2174 to_port = atoi(options+3) + 5900;
2175 #ifdef CONFIG_VNC_SASL
2176 } else if (strncmp(options, "sasl", 4) == 0) {
2177 sasl = 1; /* Require SASL auth */
2178 #endif
2179 #ifdef CONFIG_VNC_TLS
2180 } else if (strncmp(options, "tls", 3) == 0) {
2181 tls = 1; /* Require TLS */
2182 } else if (strncmp(options, "x509", 4) == 0) {
2183 char *start, *end;
2184 x509 = 1; /* Require x509 certificates */
2185 if (strncmp(options, "x509verify", 10) == 0)
2186 vs->tls.x509verify = 1; /* ...and verify client certs */
2188 /* Now check for 'x509=/some/path' postfix
2189 * and use that to setup x509 certificate/key paths */
2190 start = strchr(options, '=');
2191 end = strchr(options, ',');
2192 if (start && (!end || (start < end))) {
2193 int len = end ? end-(start+1) : strlen(start+1);
2194 char *path = qemu_strndup(start + 1, len);
2196 VNC_DEBUG("Trying certificate path '%s'\n", path);
2197 if (vnc_tls_set_x509_creds_dir(vs, path) < 0) {
2198 fprintf(stderr, "Failed to find x509 certificates/keys in %s\n", path);
2199 qemu_free(path);
2200 qemu_free(vs->display);
2201 vs->display = NULL;
2202 return -1;
2204 qemu_free(path);
2205 } else {
2206 fprintf(stderr, "No certificate path provided\n");
2207 qemu_free(vs->display);
2208 vs->display = NULL;
2209 return -1;
2211 #endif
2212 } else if (strncmp(options, "acl", 3) == 0) {
2213 acl = 1;
2217 #ifdef CONFIG_VNC_TLS
2218 if (acl && x509 && vs->tls.x509verify) {
2219 if (!(vs->tls.acl = qemu_acl_init("vnc.x509dname"))) {
2220 fprintf(stderr, "Failed to create x509 dname ACL\n");
2221 exit(1);
2224 #endif
2225 #ifdef CONFIG_VNC_SASL
2226 if (acl && sasl) {
2227 if (!(vs->sasl.acl = qemu_acl_init("vnc.username"))) {
2228 fprintf(stderr, "Failed to create username ACL\n");
2229 exit(1);
2232 #endif
2235 * Combinations we support here:
2237 * - no-auth (clear text, no auth)
2238 * - password (clear text, weak auth)
2239 * - sasl (encrypt, good auth *IF* using Kerberos via GSSAPI)
2240 * - tls (encrypt, weak anonymous creds, no auth)
2241 * - tls + password (encrypt, weak anonymous creds, weak auth)
2242 * - tls + sasl (encrypt, weak anonymous creds, good auth)
2243 * - tls + x509 (encrypt, good x509 creds, no auth)
2244 * - tls + x509 + password (encrypt, good x509 creds, weak auth)
2245 * - tls + x509 + sasl (encrypt, good x509 creds, good auth)
2247 * NB1. TLS is a stackable auth scheme.
2248 * NB2. the x509 schemes have option to validate a client cert dname
2250 if (password) {
2251 #ifdef CONFIG_VNC_TLS
2252 if (tls) {
2253 vs->auth = VNC_AUTH_VENCRYPT;
2254 if (x509) {
2255 VNC_DEBUG("Initializing VNC server with x509 password auth\n");
2256 vs->subauth = VNC_AUTH_VENCRYPT_X509VNC;
2257 } else {
2258 VNC_DEBUG("Initializing VNC server with TLS password auth\n");
2259 vs->subauth = VNC_AUTH_VENCRYPT_TLSVNC;
2261 } else {
2262 #endif /* CONFIG_VNC_TLS */
2263 VNC_DEBUG("Initializing VNC server with password auth\n");
2264 vs->auth = VNC_AUTH_VNC;
2265 #ifdef CONFIG_VNC_TLS
2266 vs->subauth = VNC_AUTH_INVALID;
2268 #endif /* CONFIG_VNC_TLS */
2269 #ifdef CONFIG_VNC_SASL
2270 } else if (sasl) {
2271 #ifdef CONFIG_VNC_TLS
2272 if (tls) {
2273 vs->auth = VNC_AUTH_VENCRYPT;
2274 if (x509) {
2275 VNC_DEBUG("Initializing VNC server with x509 SASL auth\n");
2276 vs->subauth = VNC_AUTH_VENCRYPT_X509SASL;
2277 } else {
2278 VNC_DEBUG("Initializing VNC server with TLS SASL auth\n");
2279 vs->subauth = VNC_AUTH_VENCRYPT_TLSSASL;
2281 } else {
2282 #endif /* CONFIG_VNC_TLS */
2283 VNC_DEBUG("Initializing VNC server with SASL auth\n");
2284 vs->auth = VNC_AUTH_SASL;
2285 #ifdef CONFIG_VNC_TLS
2286 vs->subauth = VNC_AUTH_INVALID;
2288 #endif /* CONFIG_VNC_TLS */
2289 #endif /* CONFIG_VNC_SASL */
2290 } else {
2291 #ifdef CONFIG_VNC_TLS
2292 if (tls) {
2293 vs->auth = VNC_AUTH_VENCRYPT;
2294 if (x509) {
2295 VNC_DEBUG("Initializing VNC server with x509 no auth\n");
2296 vs->subauth = VNC_AUTH_VENCRYPT_X509NONE;
2297 } else {
2298 VNC_DEBUG("Initializing VNC server with TLS no auth\n");
2299 vs->subauth = VNC_AUTH_VENCRYPT_TLSNONE;
2301 } else {
2302 #endif
2303 VNC_DEBUG("Initializing VNC server with no auth\n");
2304 vs->auth = VNC_AUTH_NONE;
2305 #ifdef CONFIG_VNC_TLS
2306 vs->subauth = VNC_AUTH_INVALID;
2308 #endif
2311 #ifdef CONFIG_VNC_SASL
2312 if ((saslErr = sasl_server_init(NULL, "qemu")) != SASL_OK) {
2313 fprintf(stderr, "Failed to initialize SASL auth %s",
2314 sasl_errstring(saslErr, NULL, NULL));
2315 free(vs->display);
2316 vs->display = NULL;
2317 return -1;
2319 #endif
2321 if (reverse) {
2322 /* connect to viewer */
2323 if (strncmp(display, "unix:", 5) == 0)
2324 vs->lsock = unix_connect(display+5);
2325 else
2326 vs->lsock = inet_connect(display, SOCK_STREAM);
2327 if (-1 == vs->lsock) {
2328 free(vs->display);
2329 vs->display = NULL;
2330 return -1;
2331 } else {
2332 int csock = vs->lsock;
2333 vs->lsock = -1;
2334 vnc_connect(vs, csock);
2336 return 0;
2338 } else {
2339 /* listen for connects */
2340 char *dpy;
2341 dpy = qemu_malloc(256);
2342 if (strncmp(display, "unix:", 5) == 0) {
2343 pstrcpy(dpy, 256, "unix:");
2344 vs->lsock = unix_listen(display+5, dpy+5, 256-5);
2345 } else {
2346 vs->lsock = inet_listen(display, dpy, 256, SOCK_STREAM, 5900);
2348 if (-1 == vs->lsock) {
2349 free(dpy);
2350 return -1;
2351 } else {
2352 free(vs->display);
2353 vs->display = dpy;
2356 return qemu_set_fd_handler2(vs->lsock, NULL, vnc_listen_read, NULL, vs);