xref: /openbmc/qemu/ui/vnc.c (revision fdd1ab6ad5c27a1564a1c73045908736b228458b)
13e230dd2SCorentin Chary /*
23e230dd2SCorentin Chary  * QEMU VNC display driver
33e230dd2SCorentin Chary  *
43e230dd2SCorentin Chary  * Copyright (C) 2006 Anthony Liguori <anthony@codemonkey.ws>
53e230dd2SCorentin Chary  * Copyright (C) 2006 Fabrice Bellard
63e230dd2SCorentin Chary  * Copyright (C) 2009 Red Hat, Inc
73e230dd2SCorentin Chary  *
83e230dd2SCorentin Chary  * Permission is hereby granted, free of charge, to any person obtaining a copy
93e230dd2SCorentin Chary  * of this software and associated documentation files (the "Software"), to deal
103e230dd2SCorentin Chary  * in the Software without restriction, including without limitation the rights
113e230dd2SCorentin Chary  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
123e230dd2SCorentin Chary  * copies of the Software, and to permit persons to whom the Software is
133e230dd2SCorentin Chary  * furnished to do so, subject to the following conditions:
143e230dd2SCorentin Chary  *
153e230dd2SCorentin Chary  * The above copyright notice and this permission notice shall be included in
163e230dd2SCorentin Chary  * all copies or substantial portions of the Software.
173e230dd2SCorentin Chary  *
183e230dd2SCorentin Chary  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
193e230dd2SCorentin Chary  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
203e230dd2SCorentin Chary  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
213e230dd2SCorentin Chary  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
223e230dd2SCorentin Chary  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
233e230dd2SCorentin Chary  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
243e230dd2SCorentin Chary  * THE SOFTWARE.
253e230dd2SCorentin Chary  */
263e230dd2SCorentin Chary 
273e230dd2SCorentin Chary #include "vnc.h"
28bd023f95SCorentin Chary #include "vnc-jobs.h"
2940066175SGerd Hoffmann #include "trace.h"
301d0d59feSGerd Hoffmann #include "hw/qdev.h"
319c17d615SPaolo Bonzini #include "sysemu/sysemu.h"
32d49b6836SMarkus Armbruster #include "qemu/error-report.h"
331de7afc9SPaolo Bonzini #include "qemu/sockets.h"
341de7afc9SPaolo Bonzini #include "qemu/timer.h"
351de7afc9SPaolo Bonzini #include "qemu/acl.h"
364db14629SGerd Hoffmann #include "qemu/config-file.h"
37cc7a8ea7SMarkus Armbruster #include "qapi/qmp/qerror.h"
387b1b5d19SPaolo Bonzini #include "qapi/qmp/types.h"
392b54aa87SLuiz Capitulino #include "qmp-commands.h"
401de7afc9SPaolo Bonzini #include "qemu/osdep.h"
418d447d10SGerd Hoffmann #include "ui/input.h"
42fb6ba0d5SWenchao Xia #include "qapi-event.h"
438e9b0d24SDaniel P. Berrange #include "crypto/hash.h"
443e230dd2SCorentin Chary 
450f7b2864SGerd Hoffmann #define VNC_REFRESH_INTERVAL_BASE GUI_REFRESH_INTERVAL_DEFAULT
463e230dd2SCorentin Chary #define VNC_REFRESH_INTERVAL_INC  50
470f7b2864SGerd Hoffmann #define VNC_REFRESH_INTERVAL_MAX  GUI_REFRESH_INTERVAL_IDLE
48999342a0SCorentin Chary static const struct timeval VNC_REFRESH_STATS = { 0, 500000 };
49999342a0SCorentin Chary static const struct timeval VNC_REFRESH_LOSSY = { 2, 0 };
503e230dd2SCorentin Chary 
513e230dd2SCorentin Chary #include "vnc_keysym.h"
52800567a6SDaniel P. Berrange #include "crypto/cipher.h"
533e230dd2SCorentin Chary 
54d616ccc5SGerd Hoffmann static QTAILQ_HEAD(, VncDisplay) vnc_displays =
55d616ccc5SGerd Hoffmann     QTAILQ_HEAD_INITIALIZER(vnc_displays);
563e230dd2SCorentin Chary 
573e230dd2SCorentin Chary static int vnc_cursor_define(VncState *vs);
587bc9318bSGerd Hoffmann static void vnc_release_modifiers(VncState *vs);
593e230dd2SCorentin Chary 
608cf36489SGerd Hoffmann static void vnc_set_share_mode(VncState *vs, VncShareMode mode)
618cf36489SGerd Hoffmann {
628cf36489SGerd Hoffmann #ifdef _VNC_DEBUG
638cf36489SGerd Hoffmann     static const char *mn[] = {
648cf36489SGerd Hoffmann         [0]                           = "undefined",
658cf36489SGerd Hoffmann         [VNC_SHARE_MODE_CONNECTING]   = "connecting",
668cf36489SGerd Hoffmann         [VNC_SHARE_MODE_SHARED]       = "shared",
678cf36489SGerd Hoffmann         [VNC_SHARE_MODE_EXCLUSIVE]    = "exclusive",
688cf36489SGerd Hoffmann         [VNC_SHARE_MODE_DISCONNECTED] = "disconnected",
698cf36489SGerd Hoffmann     };
708cf36489SGerd Hoffmann     fprintf(stderr, "%s/%d: %s -> %s\n", __func__,
718cf36489SGerd Hoffmann             vs->csock, mn[vs->share_mode], mn[mode]);
728cf36489SGerd Hoffmann #endif
738cf36489SGerd Hoffmann 
74e5f34cddSGerd Hoffmann     switch (vs->share_mode) {
75e5f34cddSGerd Hoffmann     case VNC_SHARE_MODE_CONNECTING:
76e5f34cddSGerd Hoffmann         vs->vd->num_connecting--;
77e5f34cddSGerd Hoffmann         break;
78e5f34cddSGerd Hoffmann     case VNC_SHARE_MODE_SHARED:
79e5f34cddSGerd Hoffmann         vs->vd->num_shared--;
80e5f34cddSGerd Hoffmann         break;
81e5f34cddSGerd Hoffmann     case VNC_SHARE_MODE_EXCLUSIVE:
828cf36489SGerd Hoffmann         vs->vd->num_exclusive--;
83e5f34cddSGerd Hoffmann         break;
84e5f34cddSGerd Hoffmann     default:
85e5f34cddSGerd Hoffmann         break;
868cf36489SGerd Hoffmann     }
87e5f34cddSGerd Hoffmann 
888cf36489SGerd Hoffmann     vs->share_mode = mode;
89e5f34cddSGerd Hoffmann 
90e5f34cddSGerd Hoffmann     switch (vs->share_mode) {
91e5f34cddSGerd Hoffmann     case VNC_SHARE_MODE_CONNECTING:
92e5f34cddSGerd Hoffmann         vs->vd->num_connecting++;
93e5f34cddSGerd Hoffmann         break;
94e5f34cddSGerd Hoffmann     case VNC_SHARE_MODE_SHARED:
95e5f34cddSGerd Hoffmann         vs->vd->num_shared++;
96e5f34cddSGerd Hoffmann         break;
97e5f34cddSGerd Hoffmann     case VNC_SHARE_MODE_EXCLUSIVE:
988cf36489SGerd Hoffmann         vs->vd->num_exclusive++;
99e5f34cddSGerd Hoffmann         break;
100e5f34cddSGerd Hoffmann     default:
101e5f34cddSGerd Hoffmann         break;
1028cf36489SGerd Hoffmann     }
1038cf36489SGerd Hoffmann }
1048cf36489SGerd Hoffmann 
1053e230dd2SCorentin Chary static char *addr_to_string(const char *format,
1063e230dd2SCorentin Chary                             struct sockaddr_storage *sa,
1073e230dd2SCorentin Chary                             socklen_t salen) {
1083e230dd2SCorentin Chary     char *addr;
1093e230dd2SCorentin Chary     char host[NI_MAXHOST];
1103e230dd2SCorentin Chary     char serv[NI_MAXSERV];
1113e230dd2SCorentin Chary     int err;
1123e230dd2SCorentin Chary     size_t addrlen;
1133e230dd2SCorentin Chary 
1143e230dd2SCorentin Chary     if ((err = getnameinfo((struct sockaddr *)sa, salen,
1153e230dd2SCorentin Chary                            host, sizeof(host),
1163e230dd2SCorentin Chary                            serv, sizeof(serv),
1173e230dd2SCorentin Chary                            NI_NUMERICHOST | NI_NUMERICSERV)) != 0) {
1183e230dd2SCorentin Chary         VNC_DEBUG("Cannot resolve address %d: %s\n",
1193e230dd2SCorentin Chary                   err, gai_strerror(err));
1203e230dd2SCorentin Chary         return NULL;
1213e230dd2SCorentin Chary     }
1223e230dd2SCorentin Chary 
1233e230dd2SCorentin Chary     /* Enough for the existing format + the 2 vars we're
1243e230dd2SCorentin Chary      * substituting in. */
1253e230dd2SCorentin Chary     addrlen = strlen(format) + strlen(host) + strlen(serv);
1267267c094SAnthony Liguori     addr = g_malloc(addrlen + 1);
1273e230dd2SCorentin Chary     snprintf(addr, addrlen, format, host, serv);
1283e230dd2SCorentin Chary     addr[addrlen] = '\0';
1293e230dd2SCorentin Chary 
1303e230dd2SCorentin Chary     return addr;
1313e230dd2SCorentin Chary }
1323e230dd2SCorentin Chary 
1333e230dd2SCorentin Chary 
1343e230dd2SCorentin Chary char *vnc_socket_local_addr(const char *format, int fd) {
1353e230dd2SCorentin Chary     struct sockaddr_storage sa;
1363e230dd2SCorentin Chary     socklen_t salen;
1373e230dd2SCorentin Chary 
1383e230dd2SCorentin Chary     salen = sizeof(sa);
1393e230dd2SCorentin Chary     if (getsockname(fd, (struct sockaddr*)&sa, &salen) < 0)
1403e230dd2SCorentin Chary         return NULL;
1413e230dd2SCorentin Chary 
1423e230dd2SCorentin Chary     return addr_to_string(format, &sa, salen);
1433e230dd2SCorentin Chary }
1443e230dd2SCorentin Chary 
1453e230dd2SCorentin Chary char *vnc_socket_remote_addr(const char *format, int fd) {
1463e230dd2SCorentin Chary     struct sockaddr_storage sa;
1473e230dd2SCorentin Chary     socklen_t salen;
1483e230dd2SCorentin Chary 
1493e230dd2SCorentin Chary     salen = sizeof(sa);
1503e230dd2SCorentin Chary     if (getpeername(fd, (struct sockaddr*)&sa, &salen) < 0)
1513e230dd2SCorentin Chary         return NULL;
1523e230dd2SCorentin Chary 
1533e230dd2SCorentin Chary     return addr_to_string(format, &sa, salen);
1543e230dd2SCorentin Chary }
1553e230dd2SCorentin Chary 
156fb6ba0d5SWenchao Xia static VncBasicInfo *vnc_basic_info_get(struct sockaddr_storage *sa,
1573e230dd2SCorentin Chary                                         socklen_t salen)
1583e230dd2SCorentin Chary {
159fb6ba0d5SWenchao Xia     VncBasicInfo *info;
1603e230dd2SCorentin Chary     char host[NI_MAXHOST];
1613e230dd2SCorentin Chary     char serv[NI_MAXSERV];
1623e230dd2SCorentin Chary     int err;
1633e230dd2SCorentin Chary 
1643e230dd2SCorentin Chary     if ((err = getnameinfo((struct sockaddr *)sa, salen,
1653e230dd2SCorentin Chary                            host, sizeof(host),
1663e230dd2SCorentin Chary                            serv, sizeof(serv),
1673e230dd2SCorentin Chary                            NI_NUMERICHOST | NI_NUMERICSERV)) != 0) {
1683e230dd2SCorentin Chary         VNC_DEBUG("Cannot resolve address %d: %s\n",
1693e230dd2SCorentin Chary                   err, gai_strerror(err));
170fb6ba0d5SWenchao Xia         return NULL;
1713e230dd2SCorentin Chary     }
1723e230dd2SCorentin Chary 
173fb6ba0d5SWenchao Xia     info = g_malloc0(sizeof(VncBasicInfo));
174fb6ba0d5SWenchao Xia     info->host = g_strdup(host);
175fb6ba0d5SWenchao Xia     info->service = g_strdup(serv);
176fb6ba0d5SWenchao Xia     info->family = inet_netfamily(sa->ss_family);
177fb6ba0d5SWenchao Xia     return info;
1783e230dd2SCorentin Chary }
1793e230dd2SCorentin Chary 
180fb6ba0d5SWenchao Xia static VncBasicInfo *vnc_basic_info_get_from_server_addr(int fd)
1813e230dd2SCorentin Chary {
1823e230dd2SCorentin Chary     struct sockaddr_storage sa;
1833e230dd2SCorentin Chary     socklen_t salen;
1843e230dd2SCorentin Chary 
1853e230dd2SCorentin Chary     salen = sizeof(sa);
1863e230dd2SCorentin Chary     if (getsockname(fd, (struct sockaddr*)&sa, &salen) < 0) {
187fb6ba0d5SWenchao Xia         return NULL;
1883e230dd2SCorentin Chary     }
1893e230dd2SCorentin Chary 
190fb6ba0d5SWenchao Xia     return vnc_basic_info_get(&sa, salen);
1913e230dd2SCorentin Chary }
1923e230dd2SCorentin Chary 
193fb6ba0d5SWenchao Xia static VncBasicInfo *vnc_basic_info_get_from_remote_addr(int fd)
1943e230dd2SCorentin Chary {
1953e230dd2SCorentin Chary     struct sockaddr_storage sa;
1963e230dd2SCorentin Chary     socklen_t salen;
1973e230dd2SCorentin Chary 
1983e230dd2SCorentin Chary     salen = sizeof(sa);
1993e230dd2SCorentin Chary     if (getpeername(fd, (struct sockaddr*)&sa, &salen) < 0) {
200fb6ba0d5SWenchao Xia         return NULL;
2013e230dd2SCorentin Chary     }
2023e230dd2SCorentin Chary 
203fb6ba0d5SWenchao Xia     return vnc_basic_info_get(&sa, salen);
2043e230dd2SCorentin Chary }
2053e230dd2SCorentin Chary 
2063e230dd2SCorentin Chary static const char *vnc_auth_name(VncDisplay *vd) {
2073e230dd2SCorentin Chary     switch (vd->auth) {
2083e230dd2SCorentin Chary     case VNC_AUTH_INVALID:
2093e230dd2SCorentin Chary         return "invalid";
2103e230dd2SCorentin Chary     case VNC_AUTH_NONE:
2113e230dd2SCorentin Chary         return "none";
2123e230dd2SCorentin Chary     case VNC_AUTH_VNC:
2133e230dd2SCorentin Chary         return "vnc";
2143e230dd2SCorentin Chary     case VNC_AUTH_RA2:
2153e230dd2SCorentin Chary         return "ra2";
2163e230dd2SCorentin Chary     case VNC_AUTH_RA2NE:
2173e230dd2SCorentin Chary         return "ra2ne";
2183e230dd2SCorentin Chary     case VNC_AUTH_TIGHT:
2193e230dd2SCorentin Chary         return "tight";
2203e230dd2SCorentin Chary     case VNC_AUTH_ULTRA:
2213e230dd2SCorentin Chary         return "ultra";
2223e230dd2SCorentin Chary     case VNC_AUTH_TLS:
2233e230dd2SCorentin Chary         return "tls";
2243e230dd2SCorentin Chary     case VNC_AUTH_VENCRYPT:
2253e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS
2263e230dd2SCorentin Chary         switch (vd->subauth) {
2273e230dd2SCorentin Chary         case VNC_AUTH_VENCRYPT_PLAIN:
2283e230dd2SCorentin Chary             return "vencrypt+plain";
2293e230dd2SCorentin Chary         case VNC_AUTH_VENCRYPT_TLSNONE:
2303e230dd2SCorentin Chary             return "vencrypt+tls+none";
2313e230dd2SCorentin Chary         case VNC_AUTH_VENCRYPT_TLSVNC:
2323e230dd2SCorentin Chary             return "vencrypt+tls+vnc";
2333e230dd2SCorentin Chary         case VNC_AUTH_VENCRYPT_TLSPLAIN:
2343e230dd2SCorentin Chary             return "vencrypt+tls+plain";
2353e230dd2SCorentin Chary         case VNC_AUTH_VENCRYPT_X509NONE:
2363e230dd2SCorentin Chary             return "vencrypt+x509+none";
2373e230dd2SCorentin Chary         case VNC_AUTH_VENCRYPT_X509VNC:
2383e230dd2SCorentin Chary             return "vencrypt+x509+vnc";
2393e230dd2SCorentin Chary         case VNC_AUTH_VENCRYPT_X509PLAIN:
2403e230dd2SCorentin Chary             return "vencrypt+x509+plain";
2413e230dd2SCorentin Chary         case VNC_AUTH_VENCRYPT_TLSSASL:
2423e230dd2SCorentin Chary             return "vencrypt+tls+sasl";
2433e230dd2SCorentin Chary         case VNC_AUTH_VENCRYPT_X509SASL:
2443e230dd2SCorentin Chary             return "vencrypt+x509+sasl";
2453e230dd2SCorentin Chary         default:
2463e230dd2SCorentin Chary             return "vencrypt";
2473e230dd2SCorentin Chary         }
2483e230dd2SCorentin Chary #else
2493e230dd2SCorentin Chary         return "vencrypt";
2503e230dd2SCorentin Chary #endif
2513e230dd2SCorentin Chary     case VNC_AUTH_SASL:
2523e230dd2SCorentin Chary         return "sasl";
2533e230dd2SCorentin Chary     }
2543e230dd2SCorentin Chary     return "unknown";
2553e230dd2SCorentin Chary }
2563e230dd2SCorentin Chary 
257d616ccc5SGerd Hoffmann static VncServerInfo *vnc_server_info_get(VncDisplay *vd)
2583e230dd2SCorentin Chary {
259fb6ba0d5SWenchao Xia     VncServerInfo *info;
260d616ccc5SGerd Hoffmann     VncBasicInfo *bi = vnc_basic_info_get_from_server_addr(vd->lsock);
261fb6ba0d5SWenchao Xia     if (!bi) {
262fb6ba0d5SWenchao Xia         return NULL;
2633e230dd2SCorentin Chary     }
2643e230dd2SCorentin Chary 
265fb6ba0d5SWenchao Xia     info = g_malloc(sizeof(*info));
266fb6ba0d5SWenchao Xia     info->base = bi;
267fb6ba0d5SWenchao Xia     info->has_auth = true;
268d616ccc5SGerd Hoffmann     info->auth = g_strdup(vnc_auth_name(vd));
269fb6ba0d5SWenchao Xia     return info;
2703e230dd2SCorentin Chary }
2713e230dd2SCorentin Chary 
2723e230dd2SCorentin Chary static void vnc_client_cache_auth(VncState *client)
2733e230dd2SCorentin Chary {
2743e230dd2SCorentin Chary     if (!client->info) {
2753e230dd2SCorentin Chary         return;
2763e230dd2SCorentin Chary     }
2773e230dd2SCorentin Chary 
2783e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS
2793e230dd2SCorentin Chary     if (client->tls.session &&
2803e230dd2SCorentin Chary         client->tls.dname) {
281fb6ba0d5SWenchao Xia         client->info->has_x509_dname = true;
282fb6ba0d5SWenchao Xia         client->info->x509_dname = g_strdup(client->tls.dname);
2833e230dd2SCorentin Chary     }
2843e230dd2SCorentin Chary #endif
2853e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL
2863e230dd2SCorentin Chary     if (client->sasl.conn &&
2873e230dd2SCorentin Chary         client->sasl.username) {
288fb6ba0d5SWenchao Xia         client->info->has_sasl_username = true;
289fb6ba0d5SWenchao Xia         client->info->sasl_username = g_strdup(client->sasl.username);
2903e230dd2SCorentin Chary     }
2913e230dd2SCorentin Chary #endif
2923e230dd2SCorentin Chary }
2933e230dd2SCorentin Chary 
2943e230dd2SCorentin Chary static void vnc_client_cache_addr(VncState *client)
2953e230dd2SCorentin Chary {
296fb6ba0d5SWenchao Xia     VncBasicInfo *bi = vnc_basic_info_get_from_remote_addr(client->csock);
2973e230dd2SCorentin Chary 
298fb6ba0d5SWenchao Xia     if (bi) {
299fb6ba0d5SWenchao Xia         client->info = g_malloc0(sizeof(*client->info));
300fb6ba0d5SWenchao Xia         client->info->base = bi;
301fb6ba0d5SWenchao Xia     }
3023e230dd2SCorentin Chary }
3033e230dd2SCorentin Chary 
304fb6ba0d5SWenchao Xia static void vnc_qmp_event(VncState *vs, QAPIEvent event)
3053e230dd2SCorentin Chary {
306fb6ba0d5SWenchao Xia     VncServerInfo *si;
3073e230dd2SCorentin Chary 
3083e230dd2SCorentin Chary     if (!vs->info) {
3093e230dd2SCorentin Chary         return;
3103e230dd2SCorentin Chary     }
311fb6ba0d5SWenchao Xia     g_assert(vs->info->base);
3123e230dd2SCorentin Chary 
313d616ccc5SGerd Hoffmann     si = vnc_server_info_get(vs->vd);
314fb6ba0d5SWenchao Xia     if (!si) {
3153e230dd2SCorentin Chary         return;
3163e230dd2SCorentin Chary     }
3173e230dd2SCorentin Chary 
318fb6ba0d5SWenchao Xia     switch (event) {
319fb6ba0d5SWenchao Xia     case QAPI_EVENT_VNC_CONNECTED:
320fb6ba0d5SWenchao Xia         qapi_event_send_vnc_connected(si, vs->info->base, &error_abort);
321fb6ba0d5SWenchao Xia         break;
322fb6ba0d5SWenchao Xia     case QAPI_EVENT_VNC_INITIALIZED:
323fb6ba0d5SWenchao Xia         qapi_event_send_vnc_initialized(si, vs->info, &error_abort);
324fb6ba0d5SWenchao Xia         break;
325fb6ba0d5SWenchao Xia     case QAPI_EVENT_VNC_DISCONNECTED:
326fb6ba0d5SWenchao Xia         qapi_event_send_vnc_disconnected(si, vs->info, &error_abort);
327fb6ba0d5SWenchao Xia         break;
328fb6ba0d5SWenchao Xia     default:
329fb6ba0d5SWenchao Xia         break;
330fb6ba0d5SWenchao Xia     }
3313e230dd2SCorentin Chary 
332fb6ba0d5SWenchao Xia     qapi_free_VncServerInfo(si);
3333e230dd2SCorentin Chary }
3343e230dd2SCorentin Chary 
3352b54aa87SLuiz Capitulino static VncClientInfo *qmp_query_vnc_client(const VncState *client)
3363e230dd2SCorentin Chary {
3372b54aa87SLuiz Capitulino     struct sockaddr_storage sa;
3382b54aa87SLuiz Capitulino     socklen_t salen = sizeof(sa);
3392b54aa87SLuiz Capitulino     char host[NI_MAXHOST];
3402b54aa87SLuiz Capitulino     char serv[NI_MAXSERV];
3412b54aa87SLuiz Capitulino     VncClientInfo *info;
3423e230dd2SCorentin Chary 
3432b54aa87SLuiz Capitulino     if (getpeername(client->csock, (struct sockaddr *)&sa, &salen) < 0) {
3442b54aa87SLuiz Capitulino         return NULL;
3452b54aa87SLuiz Capitulino     }
3462b54aa87SLuiz Capitulino 
3472b54aa87SLuiz Capitulino     if (getnameinfo((struct sockaddr *)&sa, salen,
3482b54aa87SLuiz Capitulino                     host, sizeof(host),
3492b54aa87SLuiz Capitulino                     serv, sizeof(serv),
3502b54aa87SLuiz Capitulino                     NI_NUMERICHOST | NI_NUMERICSERV) < 0) {
3512b54aa87SLuiz Capitulino         return NULL;
3522b54aa87SLuiz Capitulino     }
3532b54aa87SLuiz Capitulino 
3542b54aa87SLuiz Capitulino     info = g_malloc0(sizeof(*info));
355a589569fSWenchao Xia     info->base = g_malloc0(sizeof(*info->base));
356a589569fSWenchao Xia     info->base->host = g_strdup(host);
357a589569fSWenchao Xia     info->base->service = g_strdup(serv);
358a589569fSWenchao Xia     info->base->family = inet_netfamily(sa.ss_family);
3594478aa76SGerd Hoffmann     info->base->websocket = client->websocket;
3603e230dd2SCorentin Chary 
3613e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS
3622b54aa87SLuiz Capitulino     if (client->tls.session && client->tls.dname) {
3632b54aa87SLuiz Capitulino         info->has_x509_dname = true;
3642b54aa87SLuiz Capitulino         info->x509_dname = g_strdup(client->tls.dname);
3652b54aa87SLuiz Capitulino     }
3663e230dd2SCorentin Chary #endif
3673e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL
3682b54aa87SLuiz Capitulino     if (client->sasl.conn && client->sasl.username) {
3692b54aa87SLuiz Capitulino         info->has_sasl_username = true;
3702b54aa87SLuiz Capitulino         info->sasl_username = g_strdup(client->sasl.username);
3712b54aa87SLuiz Capitulino     }
3723e230dd2SCorentin Chary #endif
3732b54aa87SLuiz Capitulino 
3742b54aa87SLuiz Capitulino     return info;
3753e230dd2SCorentin Chary }
3763e230dd2SCorentin Chary 
377d616ccc5SGerd Hoffmann static VncDisplay *vnc_display_find(const char *id)
378d616ccc5SGerd Hoffmann {
379d616ccc5SGerd Hoffmann     VncDisplay *vd;
380d616ccc5SGerd Hoffmann 
381d616ccc5SGerd Hoffmann     if (id == NULL) {
382d616ccc5SGerd Hoffmann         return QTAILQ_FIRST(&vnc_displays);
383d616ccc5SGerd Hoffmann     }
384d616ccc5SGerd Hoffmann     QTAILQ_FOREACH(vd, &vnc_displays, next) {
385d616ccc5SGerd Hoffmann         if (strcmp(id, vd->id) == 0) {
386d616ccc5SGerd Hoffmann             return vd;
387d616ccc5SGerd Hoffmann         }
388d616ccc5SGerd Hoffmann     }
389d616ccc5SGerd Hoffmann     return NULL;
390d616ccc5SGerd Hoffmann }
391d616ccc5SGerd Hoffmann 
3922d29a436SGerd Hoffmann static VncClientInfoList *qmp_query_client_list(VncDisplay *vd)
3932d29a436SGerd Hoffmann {
3942d29a436SGerd Hoffmann     VncClientInfoList *cinfo, *prev = NULL;
3952d29a436SGerd Hoffmann     VncState *client;
3962d29a436SGerd Hoffmann 
3972d29a436SGerd Hoffmann     QTAILQ_FOREACH(client, &vd->clients, next) {
3982d29a436SGerd Hoffmann         cinfo = g_new0(VncClientInfoList, 1);
3992d29a436SGerd Hoffmann         cinfo->value = qmp_query_vnc_client(client);
4002d29a436SGerd Hoffmann         cinfo->next = prev;
4012d29a436SGerd Hoffmann         prev = cinfo;
4022d29a436SGerd Hoffmann     }
4032d29a436SGerd Hoffmann     return prev;
4042d29a436SGerd Hoffmann }
4052d29a436SGerd Hoffmann 
4062b54aa87SLuiz Capitulino VncInfo *qmp_query_vnc(Error **errp)
4073e230dd2SCorentin Chary {
4082b54aa87SLuiz Capitulino     VncInfo *info = g_malloc0(sizeof(*info));
409d616ccc5SGerd Hoffmann     VncDisplay *vd = vnc_display_find(NULL);
4103e230dd2SCorentin Chary 
411bf7aa45eSGerd Hoffmann     if (vd == NULL || !vd->enabled) {
4122b54aa87SLuiz Capitulino         info->enabled = false;
4133e230dd2SCorentin Chary     } else {
4142b54aa87SLuiz Capitulino         struct sockaddr_storage sa;
4152b54aa87SLuiz Capitulino         socklen_t salen = sizeof(sa);
4162b54aa87SLuiz Capitulino         char host[NI_MAXHOST];
4172b54aa87SLuiz Capitulino         char serv[NI_MAXSERV];
4183e230dd2SCorentin Chary 
4192b54aa87SLuiz Capitulino         info->enabled = true;
4202b54aa87SLuiz Capitulino 
4212b54aa87SLuiz Capitulino         /* for compatibility with the original command */
4222b54aa87SLuiz Capitulino         info->has_clients = true;
4232d29a436SGerd Hoffmann         info->clients = qmp_query_client_list(vd);
4243e230dd2SCorentin Chary 
425d616ccc5SGerd Hoffmann         if (vd->lsock == -1) {
426417b0b88SPaolo Bonzini             return info;
427417b0b88SPaolo Bonzini         }
428417b0b88SPaolo Bonzini 
429d616ccc5SGerd Hoffmann         if (getsockname(vd->lsock, (struct sockaddr *)&sa,
4302b54aa87SLuiz Capitulino                         &salen) == -1) {
431c6bd8c70SMarkus Armbruster             error_setg(errp, QERR_UNDEFINED_ERROR);
4322b54aa87SLuiz Capitulino             goto out_error;
4332b54aa87SLuiz Capitulino         }
4343e230dd2SCorentin Chary 
4352b54aa87SLuiz Capitulino         if (getnameinfo((struct sockaddr *)&sa, salen,
4362b54aa87SLuiz Capitulino                         host, sizeof(host),
4372b54aa87SLuiz Capitulino                         serv, sizeof(serv),
4382b54aa87SLuiz Capitulino                         NI_NUMERICHOST | NI_NUMERICSERV) < 0) {
439c6bd8c70SMarkus Armbruster             error_setg(errp, QERR_UNDEFINED_ERROR);
4402b54aa87SLuiz Capitulino             goto out_error;
4413e230dd2SCorentin Chary         }
4422b54aa87SLuiz Capitulino 
4432b54aa87SLuiz Capitulino         info->has_host = true;
4442b54aa87SLuiz Capitulino         info->host = g_strdup(host);
4452b54aa87SLuiz Capitulino 
4462b54aa87SLuiz Capitulino         info->has_service = true;
4472b54aa87SLuiz Capitulino         info->service = g_strdup(serv);
4482b54aa87SLuiz Capitulino 
4492b54aa87SLuiz Capitulino         info->has_family = true;
450a589569fSWenchao Xia         info->family = inet_netfamily(sa.ss_family);
4512b54aa87SLuiz Capitulino 
4522b54aa87SLuiz Capitulino         info->has_auth = true;
453d616ccc5SGerd Hoffmann         info->auth = g_strdup(vnc_auth_name(vd));
4543e230dd2SCorentin Chary     }
4552b54aa87SLuiz Capitulino 
4562b54aa87SLuiz Capitulino     return info;
4572b54aa87SLuiz Capitulino 
4582b54aa87SLuiz Capitulino out_error:
4592b54aa87SLuiz Capitulino     qapi_free_VncInfo(info);
4602b54aa87SLuiz Capitulino     return NULL;
4613e230dd2SCorentin Chary }
4623e230dd2SCorentin Chary 
463df887684SGerd Hoffmann static VncBasicInfoList *qmp_query_server_entry(int socket,
4644478aa76SGerd Hoffmann                                                 bool websocket,
465df887684SGerd Hoffmann                                                 VncBasicInfoList *prev)
466df887684SGerd Hoffmann {
467df887684SGerd Hoffmann     VncBasicInfoList *list;
468df887684SGerd Hoffmann     VncBasicInfo *info;
469df887684SGerd Hoffmann     struct sockaddr_storage sa;
470df887684SGerd Hoffmann     socklen_t salen = sizeof(sa);
471df887684SGerd Hoffmann     char host[NI_MAXHOST];
472df887684SGerd Hoffmann     char serv[NI_MAXSERV];
473df887684SGerd Hoffmann 
474df887684SGerd Hoffmann     if (getsockname(socket, (struct sockaddr *)&sa, &salen) < 0 ||
475df887684SGerd Hoffmann         getnameinfo((struct sockaddr *)&sa, salen,
476df887684SGerd Hoffmann                     host, sizeof(host), serv, sizeof(serv),
477df887684SGerd Hoffmann                     NI_NUMERICHOST | NI_NUMERICSERV) < 0) {
478df887684SGerd Hoffmann         return prev;
479df887684SGerd Hoffmann     }
480df887684SGerd Hoffmann 
481df887684SGerd Hoffmann     info = g_new0(VncBasicInfo, 1);
482df887684SGerd Hoffmann     info->host = g_strdup(host);
483df887684SGerd Hoffmann     info->service = g_strdup(serv);
484df887684SGerd Hoffmann     info->family = inet_netfamily(sa.ss_family);
4854478aa76SGerd Hoffmann     info->websocket = websocket;
486df887684SGerd Hoffmann 
487df887684SGerd Hoffmann     list = g_new0(VncBasicInfoList, 1);
488df887684SGerd Hoffmann     list->value = info;
489df887684SGerd Hoffmann     list->next = prev;
490df887684SGerd Hoffmann     return list;
491df887684SGerd Hoffmann }
492df887684SGerd Hoffmann 
493df887684SGerd Hoffmann static void qmp_query_auth(VncDisplay *vd, VncInfo2 *info)
494df887684SGerd Hoffmann {
495df887684SGerd Hoffmann     switch (vd->auth) {
496df887684SGerd Hoffmann     case VNC_AUTH_VNC:
497df887684SGerd Hoffmann         info->auth = VNC_PRIMARY_AUTH_VNC;
498df887684SGerd Hoffmann         break;
499df887684SGerd Hoffmann     case VNC_AUTH_RA2:
500df887684SGerd Hoffmann         info->auth = VNC_PRIMARY_AUTH_RA2;
501df887684SGerd Hoffmann         break;
502df887684SGerd Hoffmann     case VNC_AUTH_RA2NE:
503df887684SGerd Hoffmann         info->auth = VNC_PRIMARY_AUTH_RA2NE;
504df887684SGerd Hoffmann         break;
505df887684SGerd Hoffmann     case VNC_AUTH_TIGHT:
506df887684SGerd Hoffmann         info->auth = VNC_PRIMARY_AUTH_TIGHT;
507df887684SGerd Hoffmann         break;
508df887684SGerd Hoffmann     case VNC_AUTH_ULTRA:
509df887684SGerd Hoffmann         info->auth = VNC_PRIMARY_AUTH_ULTRA;
510df887684SGerd Hoffmann         break;
511df887684SGerd Hoffmann     case VNC_AUTH_TLS:
512df887684SGerd Hoffmann         info->auth = VNC_PRIMARY_AUTH_TLS;
513df887684SGerd Hoffmann         break;
514df887684SGerd Hoffmann     case VNC_AUTH_VENCRYPT:
515df887684SGerd Hoffmann         info->auth = VNC_PRIMARY_AUTH_VENCRYPT;
516df887684SGerd Hoffmann #ifdef CONFIG_VNC_TLS
517df887684SGerd Hoffmann         info->has_vencrypt = true;
518df887684SGerd Hoffmann         switch (vd->subauth) {
519df887684SGerd Hoffmann         case VNC_AUTH_VENCRYPT_PLAIN:
520df887684SGerd Hoffmann             info->vencrypt = VNC_VENCRYPT_SUB_AUTH_PLAIN;
521df887684SGerd Hoffmann             break;
522df887684SGerd Hoffmann         case VNC_AUTH_VENCRYPT_TLSNONE:
523df887684SGerd Hoffmann             info->vencrypt = VNC_VENCRYPT_SUB_AUTH_TLS_NONE;
524df887684SGerd Hoffmann             break;
525df887684SGerd Hoffmann         case VNC_AUTH_VENCRYPT_TLSVNC:
526df887684SGerd Hoffmann             info->vencrypt = VNC_VENCRYPT_SUB_AUTH_TLS_VNC;
527df887684SGerd Hoffmann             break;
528df887684SGerd Hoffmann         case VNC_AUTH_VENCRYPT_TLSPLAIN:
529df887684SGerd Hoffmann             info->vencrypt = VNC_VENCRYPT_SUB_AUTH_TLS_PLAIN;
530df887684SGerd Hoffmann             break;
531df887684SGerd Hoffmann         case VNC_AUTH_VENCRYPT_X509NONE:
532df887684SGerd Hoffmann             info->vencrypt = VNC_VENCRYPT_SUB_AUTH_X509_NONE;
533df887684SGerd Hoffmann             break;
534df887684SGerd Hoffmann         case VNC_AUTH_VENCRYPT_X509VNC:
535df887684SGerd Hoffmann             info->vencrypt = VNC_VENCRYPT_SUB_AUTH_X509_VNC;
536df887684SGerd Hoffmann             break;
537df887684SGerd Hoffmann         case VNC_AUTH_VENCRYPT_X509PLAIN:
538df887684SGerd Hoffmann             info->vencrypt = VNC_VENCRYPT_SUB_AUTH_X509_PLAIN;
539df887684SGerd Hoffmann             break;
540df887684SGerd Hoffmann         case VNC_AUTH_VENCRYPT_TLSSASL:
541df887684SGerd Hoffmann             info->vencrypt = VNC_VENCRYPT_SUB_AUTH_TLS_SASL;
542df887684SGerd Hoffmann             break;
543df887684SGerd Hoffmann         case VNC_AUTH_VENCRYPT_X509SASL:
544df887684SGerd Hoffmann             info->vencrypt = VNC_VENCRYPT_SUB_AUTH_X509_SASL;
545df887684SGerd Hoffmann             break;
546df887684SGerd Hoffmann         default:
547df887684SGerd Hoffmann             info->has_vencrypt = false;
548df887684SGerd Hoffmann             break;
549df887684SGerd Hoffmann         }
550df887684SGerd Hoffmann #endif
551df887684SGerd Hoffmann         break;
552df887684SGerd Hoffmann     case VNC_AUTH_SASL:
553df887684SGerd Hoffmann         info->auth = VNC_PRIMARY_AUTH_SASL;
554df887684SGerd Hoffmann         break;
555df887684SGerd Hoffmann     case VNC_AUTH_NONE:
556df887684SGerd Hoffmann     default:
557df887684SGerd Hoffmann         info->auth = VNC_PRIMARY_AUTH_NONE;
558df887684SGerd Hoffmann         break;
559df887684SGerd Hoffmann     }
560df887684SGerd Hoffmann }
561df887684SGerd Hoffmann 
562df887684SGerd Hoffmann VncInfo2List *qmp_query_vnc_servers(Error **errp)
563df887684SGerd Hoffmann {
564df887684SGerd Hoffmann     VncInfo2List *item, *prev = NULL;
565df887684SGerd Hoffmann     VncInfo2 *info;
566df887684SGerd Hoffmann     VncDisplay *vd;
567df887684SGerd Hoffmann     DeviceState *dev;
568df887684SGerd Hoffmann 
569df887684SGerd Hoffmann     QTAILQ_FOREACH(vd, &vnc_displays, next) {
570df887684SGerd Hoffmann         info = g_new0(VncInfo2, 1);
571df887684SGerd Hoffmann         info->id = g_strdup(vd->id);
572df887684SGerd Hoffmann         info->clients = qmp_query_client_list(vd);
573df887684SGerd Hoffmann         qmp_query_auth(vd, info);
574df887684SGerd Hoffmann         if (vd->dcl.con) {
575df887684SGerd Hoffmann             dev = DEVICE(object_property_get_link(OBJECT(vd->dcl.con),
576df887684SGerd Hoffmann                                                   "device", NULL));
577df887684SGerd Hoffmann             info->has_display = true;
578df887684SGerd Hoffmann             info->display = g_strdup(dev->id);
579df887684SGerd Hoffmann         }
580df887684SGerd Hoffmann         if (vd->lsock != -1) {
5814478aa76SGerd Hoffmann             info->server = qmp_query_server_entry(vd->lsock, false,
582df887684SGerd Hoffmann                                                   info->server);
583df887684SGerd Hoffmann         }
584df887684SGerd Hoffmann         if (vd->lwebsock != -1) {
5854478aa76SGerd Hoffmann             info->server = qmp_query_server_entry(vd->lwebsock, true,
5864478aa76SGerd Hoffmann                                                   info->server);
587df887684SGerd Hoffmann         }
588df887684SGerd Hoffmann 
589df887684SGerd Hoffmann         item = g_new0(VncInfo2List, 1);
590df887684SGerd Hoffmann         item->value = info;
591df887684SGerd Hoffmann         item->next = prev;
592df887684SGerd Hoffmann         prev = item;
593df887684SGerd Hoffmann     }
594df887684SGerd Hoffmann     return prev;
595df887684SGerd Hoffmann }
596df887684SGerd Hoffmann 
5973e230dd2SCorentin Chary /* TODO
5983e230dd2SCorentin Chary    1) Get the queue working for IO.
5993e230dd2SCorentin Chary    2) there is some weirdness when using the -S option (the screen is grey
6003e230dd2SCorentin Chary       and not totally invalidated
6013e230dd2SCorentin Chary    3) resolutions > 1024
6023e230dd2SCorentin Chary */
6033e230dd2SCorentin Chary 
60438ee14f4SGerd Hoffmann static int vnc_update_client(VncState *vs, int has_dirty, bool sync);
6053e230dd2SCorentin Chary static void vnc_disconnect_start(VncState *vs);
6063e230dd2SCorentin Chary 
6073e230dd2SCorentin Chary static void vnc_colordepth(VncState *vs);
6083e230dd2SCorentin Chary static void framebuffer_update_request(VncState *vs, int incremental,
6093e230dd2SCorentin Chary                                        int x_position, int y_position,
6103e230dd2SCorentin Chary                                        int w, int h);
6110f7b2864SGerd Hoffmann static void vnc_refresh(DisplayChangeListener *dcl);
6123e230dd2SCorentin Chary static int vnc_refresh_server_surface(VncDisplay *vd);
6133e230dd2SCorentin Chary 
614bea60dd7SPeter Lieven static void vnc_set_area_dirty(DECLARE_BITMAP(dirty[VNC_MAX_HEIGHT],
615bea60dd7SPeter Lieven                                VNC_MAX_WIDTH / VNC_DIRTY_PIXELS_PER_BIT),
616bea60dd7SPeter Lieven                                int width, int height,
617bea60dd7SPeter Lieven                                int x, int y, int w, int h) {
61891937225SPeter Lieven     /* this is needed this to ensure we updated all affected
61991937225SPeter Lieven      * blocks if x % VNC_DIRTY_PIXELS_PER_BIT != 0 */
620b4c85ddcSPeter Lieven     w += (x % VNC_DIRTY_PIXELS_PER_BIT);
621b4c85ddcSPeter Lieven     x -= (x % VNC_DIRTY_PIXELS_PER_BIT);
6223e230dd2SCorentin Chary 
6239f64916dSGerd Hoffmann     x = MIN(x, width);
6249f64916dSGerd Hoffmann     y = MIN(y, height);
6259f64916dSGerd Hoffmann     w = MIN(x + w, width) - x;
62691937225SPeter Lieven     h = MIN(y + h, height);
6273e230dd2SCorentin Chary 
628b4c85ddcSPeter Lieven     for (; y < h; y++) {
629bea60dd7SPeter Lieven         bitmap_set(dirty[y], x / VNC_DIRTY_PIXELS_PER_BIT,
63091937225SPeter Lieven                    DIV_ROUND_UP(w, VNC_DIRTY_PIXELS_PER_BIT));
631b4c85ddcSPeter Lieven     }
6323e230dd2SCorentin Chary }
6333e230dd2SCorentin Chary 
634bea60dd7SPeter Lieven static void vnc_dpy_update(DisplayChangeListener *dcl,
635bea60dd7SPeter Lieven                            int x, int y, int w, int h)
636bea60dd7SPeter Lieven {
637bea60dd7SPeter Lieven     VncDisplay *vd = container_of(dcl, VncDisplay, dcl);
638bea60dd7SPeter Lieven     struct VncSurface *s = &vd->guest;
639bea60dd7SPeter Lieven     int width = pixman_image_get_width(vd->server);
640bea60dd7SPeter Lieven     int height = pixman_image_get_height(vd->server);
641bea60dd7SPeter Lieven 
642bea60dd7SPeter Lieven     vnc_set_area_dirty(s->dirty, width, height, x, y, w, h);
643bea60dd7SPeter Lieven }
644bea60dd7SPeter Lieven 
6453e230dd2SCorentin Chary void vnc_framebuffer_update(VncState *vs, int x, int y, int w, int h,
6463e230dd2SCorentin Chary                             int32_t encoding)
6473e230dd2SCorentin Chary {
6483e230dd2SCorentin Chary     vnc_write_u16(vs, x);
6493e230dd2SCorentin Chary     vnc_write_u16(vs, y);
6503e230dd2SCorentin Chary     vnc_write_u16(vs, w);
6513e230dd2SCorentin Chary     vnc_write_u16(vs, h);
6523e230dd2SCorentin Chary 
6533e230dd2SCorentin Chary     vnc_write_s32(vs, encoding);
6543e230dd2SCorentin Chary }
6553e230dd2SCorentin Chary 
6563e230dd2SCorentin Chary void buffer_reserve(Buffer *buffer, size_t len)
6573e230dd2SCorentin Chary {
6583e230dd2SCorentin Chary     if ((buffer->capacity - buffer->offset) < len) {
6593e230dd2SCorentin Chary         buffer->capacity += (len + 1024);
6607267c094SAnthony Liguori         buffer->buffer = g_realloc(buffer->buffer, buffer->capacity);
6613e230dd2SCorentin Chary     }
6623e230dd2SCorentin Chary }
6633e230dd2SCorentin Chary 
66471a8cdecSBlue Swirl static int buffer_empty(Buffer *buffer)
6653e230dd2SCorentin Chary {
6663e230dd2SCorentin Chary     return buffer->offset == 0;
6673e230dd2SCorentin Chary }
6683e230dd2SCorentin Chary 
6697536ee4bSTim Hardeck uint8_t *buffer_end(Buffer *buffer)
6703e230dd2SCorentin Chary {
6713e230dd2SCorentin Chary     return buffer->buffer + buffer->offset;
6723e230dd2SCorentin Chary }
6733e230dd2SCorentin Chary 
6743e230dd2SCorentin Chary void buffer_reset(Buffer *buffer)
6753e230dd2SCorentin Chary {
6763e230dd2SCorentin Chary         buffer->offset = 0;
6773e230dd2SCorentin Chary }
6783e230dd2SCorentin Chary 
6793e230dd2SCorentin Chary void buffer_free(Buffer *buffer)
6803e230dd2SCorentin Chary {
6817267c094SAnthony Liguori     g_free(buffer->buffer);
6823e230dd2SCorentin Chary     buffer->offset = 0;
6833e230dd2SCorentin Chary     buffer->capacity = 0;
6843e230dd2SCorentin Chary     buffer->buffer = NULL;
6853e230dd2SCorentin Chary }
6863e230dd2SCorentin Chary 
6873e230dd2SCorentin Chary void buffer_append(Buffer *buffer, const void *data, size_t len)
6883e230dd2SCorentin Chary {
6893e230dd2SCorentin Chary     memcpy(buffer->buffer + buffer->offset, data, len);
6903e230dd2SCorentin Chary     buffer->offset += len;
6913e230dd2SCorentin Chary }
6923e230dd2SCorentin Chary 
69332ed2680STim Hardeck void buffer_advance(Buffer *buf, size_t len)
69432ed2680STim Hardeck {
69532ed2680STim Hardeck     memmove(buf->buffer, buf->buffer + len,
69632ed2680STim Hardeck             (buf->offset - len));
69732ed2680STim Hardeck     buf->offset -= len;
69832ed2680STim Hardeck }
69932ed2680STim Hardeck 
7003e230dd2SCorentin Chary static void vnc_desktop_resize(VncState *vs)
7013e230dd2SCorentin Chary {
7023e230dd2SCorentin Chary     if (vs->csock == -1 || !vnc_has_feature(vs, VNC_FEATURE_RESIZE)) {
7033e230dd2SCorentin Chary         return;
7043e230dd2SCorentin Chary     }
705bea60dd7SPeter Lieven     if (vs->client_width == pixman_image_get_width(vs->vd->server) &&
706bea60dd7SPeter Lieven         vs->client_height == pixman_image_get_height(vs->vd->server)) {
7073e230dd2SCorentin Chary         return;
7083e230dd2SCorentin Chary     }
709bea60dd7SPeter Lieven     vs->client_width = pixman_image_get_width(vs->vd->server);
710bea60dd7SPeter Lieven     vs->client_height = pixman_image_get_height(vs->vd->server);
711bd023f95SCorentin Chary     vnc_lock_output(vs);
7123e230dd2SCorentin Chary     vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE);
7133e230dd2SCorentin Chary     vnc_write_u8(vs, 0);
7143e230dd2SCorentin Chary     vnc_write_u16(vs, 1); /* number of rects */
7153e230dd2SCorentin Chary     vnc_framebuffer_update(vs, 0, 0, vs->client_width, vs->client_height,
7163e230dd2SCorentin Chary                            VNC_ENCODING_DESKTOPRESIZE);
717bd023f95SCorentin Chary     vnc_unlock_output(vs);
7183e230dd2SCorentin Chary     vnc_flush(vs);
7193e230dd2SCorentin Chary }
7203e230dd2SCorentin Chary 
721bd023f95SCorentin Chary static void vnc_abort_display_jobs(VncDisplay *vd)
722bd023f95SCorentin Chary {
723bd023f95SCorentin Chary     VncState *vs;
724bd023f95SCorentin Chary 
725bd023f95SCorentin Chary     QTAILQ_FOREACH(vs, &vd->clients, next) {
726bd023f95SCorentin Chary         vnc_lock_output(vs);
727bd023f95SCorentin Chary         vs->abort = true;
728bd023f95SCorentin Chary         vnc_unlock_output(vs);
729bd023f95SCorentin Chary     }
730bd023f95SCorentin Chary     QTAILQ_FOREACH(vs, &vd->clients, next) {
731bd023f95SCorentin Chary         vnc_jobs_join(vs);
732bd023f95SCorentin Chary     }
733bd023f95SCorentin Chary     QTAILQ_FOREACH(vs, &vd->clients, next) {
734bd023f95SCorentin Chary         vnc_lock_output(vs);
735bd023f95SCorentin Chary         vs->abort = false;
736bd023f95SCorentin Chary         vnc_unlock_output(vs);
737bd023f95SCorentin Chary     }
738bd023f95SCorentin Chary }
739bd023f95SCorentin Chary 
7409f64916dSGerd Hoffmann int vnc_server_fb_stride(VncDisplay *vd)
7419f64916dSGerd Hoffmann {
7429f64916dSGerd Hoffmann     return pixman_image_get_stride(vd->server);
7439f64916dSGerd Hoffmann }
7449f64916dSGerd Hoffmann 
7459f64916dSGerd Hoffmann void *vnc_server_fb_ptr(VncDisplay *vd, int x, int y)
7469f64916dSGerd Hoffmann {
7479f64916dSGerd Hoffmann     uint8_t *ptr;
7489f64916dSGerd Hoffmann 
7499f64916dSGerd Hoffmann     ptr  = (uint8_t *)pixman_image_get_data(vd->server);
7509f64916dSGerd Hoffmann     ptr += y * vnc_server_fb_stride(vd);
7519f64916dSGerd Hoffmann     ptr += x * VNC_SERVER_FB_BYTES;
7529f64916dSGerd Hoffmann     return ptr;
7539f64916dSGerd Hoffmann }
7549f64916dSGerd Hoffmann 
755c12aeb86SGerd Hoffmann static void vnc_dpy_switch(DisplayChangeListener *dcl,
756c12aeb86SGerd Hoffmann                            DisplaySurface *surface)
7573e230dd2SCorentin Chary {
75821ef45d7SGerd Hoffmann     VncDisplay *vd = container_of(dcl, VncDisplay, dcl);
7593e230dd2SCorentin Chary     VncState *vs;
760bea60dd7SPeter Lieven     int width, height;
7613e230dd2SCorentin Chary 
762bd023f95SCorentin Chary     vnc_abort_display_jobs(vd);
763bd023f95SCorentin Chary 
7643e230dd2SCorentin Chary     /* server surface */
7659f64916dSGerd Hoffmann     qemu_pixman_image_unref(vd->server);
766d39fa6d8SGerd Hoffmann     vd->ds = surface;
767bea60dd7SPeter Lieven     width = MIN(VNC_MAX_WIDTH, ROUND_UP(surface_width(vd->ds),
768bea60dd7SPeter Lieven                                         VNC_DIRTY_PIXELS_PER_BIT));
769bea60dd7SPeter Lieven     height = MIN(VNC_MAX_HEIGHT, surface_height(vd->ds));
7709f64916dSGerd Hoffmann     vd->server = pixman_image_create_bits(VNC_SERVER_FB_FORMAT,
771bea60dd7SPeter Lieven                                           width, height, NULL, 0);
7723e230dd2SCorentin Chary 
7733e230dd2SCorentin Chary     /* guest surface */
7749f64916dSGerd Hoffmann #if 0 /* FIXME */
7753e230dd2SCorentin Chary     if (ds_get_bytes_per_pixel(ds) != vd->guest.ds->pf.bytes_per_pixel)
7763e230dd2SCorentin Chary         console_color_init(ds);
7779f64916dSGerd Hoffmann #endif
7789f64916dSGerd Hoffmann     qemu_pixman_image_unref(vd->guest.fb);
779d39fa6d8SGerd Hoffmann     vd->guest.fb = pixman_image_ref(surface->image);
780d39fa6d8SGerd Hoffmann     vd->guest.format = surface->format;
781bea60dd7SPeter Lieven     memset(vd->guest.dirty, 0x00, sizeof(vd->guest.dirty));
782bea60dd7SPeter Lieven     vnc_set_area_dirty(vd->guest.dirty, width, height, 0, 0,
783bea60dd7SPeter Lieven                        width, height);
7843e230dd2SCorentin Chary 
7853e230dd2SCorentin Chary     QTAILQ_FOREACH(vs, &vd->clients, next) {
7863e230dd2SCorentin Chary         vnc_colordepth(vs);
7873e230dd2SCorentin Chary         vnc_desktop_resize(vs);
7883e230dd2SCorentin Chary         if (vs->vd->cursor) {
7893e230dd2SCorentin Chary             vnc_cursor_define(vs);
7903e230dd2SCorentin Chary         }
791bea60dd7SPeter Lieven         memset(vs->dirty, 0x00, sizeof(vs->dirty));
792bea60dd7SPeter Lieven         vnc_set_area_dirty(vs->dirty, width, height, 0, 0,
793bea60dd7SPeter Lieven                            width, height);
7943e230dd2SCorentin Chary     }
7953e230dd2SCorentin Chary }
7963e230dd2SCorentin Chary 
7973e230dd2SCorentin Chary /* fastest code */
7989f64916dSGerd Hoffmann static void vnc_write_pixels_copy(VncState *vs,
7993e230dd2SCorentin Chary                                   void *pixels, int size)
8003e230dd2SCorentin Chary {
8013e230dd2SCorentin Chary     vnc_write(vs, pixels, size);
8023e230dd2SCorentin Chary }
8033e230dd2SCorentin Chary 
8043e230dd2SCorentin Chary /* slowest but generic code. */
8053e230dd2SCorentin Chary void vnc_convert_pixel(VncState *vs, uint8_t *buf, uint32_t v)
8063e230dd2SCorentin Chary {
8073e230dd2SCorentin Chary     uint8_t r, g, b;
8083e230dd2SCorentin Chary 
8099f64916dSGerd Hoffmann #if VNC_SERVER_FB_FORMAT == PIXMAN_FORMAT(32, PIXMAN_TYPE_ARGB, 0, 8, 8, 8)
8109f64916dSGerd Hoffmann     r = (((v & 0x00ff0000) >> 16) << vs->client_pf.rbits) >> 8;
8119f64916dSGerd Hoffmann     g = (((v & 0x0000ff00) >>  8) << vs->client_pf.gbits) >> 8;
8129f64916dSGerd Hoffmann     b = (((v & 0x000000ff) >>  0) << vs->client_pf.bbits) >> 8;
8139f64916dSGerd Hoffmann #else
8149f64916dSGerd Hoffmann # error need some bits here if you change VNC_SERVER_FB_FORMAT
8159f64916dSGerd Hoffmann #endif
8169f64916dSGerd Hoffmann     v = (r << vs->client_pf.rshift) |
8179f64916dSGerd Hoffmann         (g << vs->client_pf.gshift) |
8189f64916dSGerd Hoffmann         (b << vs->client_pf.bshift);
8199f64916dSGerd Hoffmann     switch (vs->client_pf.bytes_per_pixel) {
8203e230dd2SCorentin Chary     case 1:
8213e230dd2SCorentin Chary         buf[0] = v;
8223e230dd2SCorentin Chary         break;
8233e230dd2SCorentin Chary     case 2:
8249f64916dSGerd Hoffmann         if (vs->client_be) {
8253e230dd2SCorentin Chary             buf[0] = v >> 8;
8263e230dd2SCorentin Chary             buf[1] = v;
8273e230dd2SCorentin Chary         } else {
8283e230dd2SCorentin Chary             buf[1] = v >> 8;
8293e230dd2SCorentin Chary             buf[0] = v;
8303e230dd2SCorentin Chary         }
8313e230dd2SCorentin Chary         break;
8323e230dd2SCorentin Chary     default:
8333e230dd2SCorentin Chary     case 4:
8349f64916dSGerd Hoffmann         if (vs->client_be) {
8353e230dd2SCorentin Chary             buf[0] = v >> 24;
8363e230dd2SCorentin Chary             buf[1] = v >> 16;
8373e230dd2SCorentin Chary             buf[2] = v >> 8;
8383e230dd2SCorentin Chary             buf[3] = v;
8393e230dd2SCorentin Chary         } else {
8403e230dd2SCorentin Chary             buf[3] = v >> 24;
8413e230dd2SCorentin Chary             buf[2] = v >> 16;
8423e230dd2SCorentin Chary             buf[1] = v >> 8;
8433e230dd2SCorentin Chary             buf[0] = v;
8443e230dd2SCorentin Chary         }
8453e230dd2SCorentin Chary         break;
8463e230dd2SCorentin Chary     }
8473e230dd2SCorentin Chary }
8483e230dd2SCorentin Chary 
8499f64916dSGerd Hoffmann static void vnc_write_pixels_generic(VncState *vs,
8503e230dd2SCorentin Chary                                      void *pixels1, int size)
8513e230dd2SCorentin Chary {
8523e230dd2SCorentin Chary     uint8_t buf[4];
8533e230dd2SCorentin Chary 
8549f64916dSGerd Hoffmann     if (VNC_SERVER_FB_BYTES == 4) {
8553e230dd2SCorentin Chary         uint32_t *pixels = pixels1;
8563e230dd2SCorentin Chary         int n, i;
8573e230dd2SCorentin Chary         n = size >> 2;
8583e230dd2SCorentin Chary         for (i = 0; i < n; i++) {
8593e230dd2SCorentin Chary             vnc_convert_pixel(vs, buf, pixels[i]);
8609f64916dSGerd Hoffmann             vnc_write(vs, buf, vs->client_pf.bytes_per_pixel);
8613e230dd2SCorentin Chary         }
8623e230dd2SCorentin Chary     }
8633e230dd2SCorentin Chary }
8643e230dd2SCorentin Chary 
8653e230dd2SCorentin Chary int vnc_raw_send_framebuffer_update(VncState *vs, int x, int y, int w, int h)
8663e230dd2SCorentin Chary {
8673e230dd2SCorentin Chary     int i;
8683e230dd2SCorentin Chary     uint8_t *row;
8693e230dd2SCorentin Chary     VncDisplay *vd = vs->vd;
8703e230dd2SCorentin Chary 
8719f64916dSGerd Hoffmann     row = vnc_server_fb_ptr(vd, x, y);
8723e230dd2SCorentin Chary     for (i = 0; i < h; i++) {
8739f64916dSGerd Hoffmann         vs->write_pixels(vs, row, w * VNC_SERVER_FB_BYTES);
8749f64916dSGerd Hoffmann         row += vnc_server_fb_stride(vd);
8753e230dd2SCorentin Chary     }
8763e230dd2SCorentin Chary     return 1;
8773e230dd2SCorentin Chary }
8783e230dd2SCorentin Chary 
879bd023f95SCorentin Chary int vnc_send_framebuffer_update(VncState *vs, int x, int y, int w, int h)
8803e230dd2SCorentin Chary {
8813e230dd2SCorentin Chary     int n = 0;
8823e230dd2SCorentin Chary 
8833e230dd2SCorentin Chary     switch(vs->vnc_encoding) {
8843e230dd2SCorentin Chary         case VNC_ENCODING_ZLIB:
8853e230dd2SCorentin Chary             n = vnc_zlib_send_framebuffer_update(vs, x, y, w, h);
8863e230dd2SCorentin Chary             break;
8873e230dd2SCorentin Chary         case VNC_ENCODING_HEXTILE:
8883e230dd2SCorentin Chary             vnc_framebuffer_update(vs, x, y, w, h, VNC_ENCODING_HEXTILE);
8893e230dd2SCorentin Chary             n = vnc_hextile_send_framebuffer_update(vs, x, y, w, h);
8903e230dd2SCorentin Chary             break;
8913e230dd2SCorentin Chary         case VNC_ENCODING_TIGHT:
8923e230dd2SCorentin Chary             n = vnc_tight_send_framebuffer_update(vs, x, y, w, h);
8933e230dd2SCorentin Chary             break;
894efe556adSCorentin Chary         case VNC_ENCODING_TIGHT_PNG:
895efe556adSCorentin Chary             n = vnc_tight_png_send_framebuffer_update(vs, x, y, w, h);
896efe556adSCorentin Chary             break;
897148954faSCorentin Chary         case VNC_ENCODING_ZRLE:
898148954faSCorentin Chary             n = vnc_zrle_send_framebuffer_update(vs, x, y, w, h);
899148954faSCorentin Chary             break;
900148954faSCorentin Chary         case VNC_ENCODING_ZYWRLE:
901148954faSCorentin Chary             n = vnc_zywrle_send_framebuffer_update(vs, x, y, w, h);
902148954faSCorentin Chary             break;
9033e230dd2SCorentin Chary         default:
9043e230dd2SCorentin Chary             vnc_framebuffer_update(vs, x, y, w, h, VNC_ENCODING_RAW);
9053e230dd2SCorentin Chary             n = vnc_raw_send_framebuffer_update(vs, x, y, w, h);
9063e230dd2SCorentin Chary             break;
9073e230dd2SCorentin Chary     }
9083e230dd2SCorentin Chary     return n;
9093e230dd2SCorentin Chary }
9103e230dd2SCorentin Chary 
9113e230dd2SCorentin Chary static void vnc_copy(VncState *vs, int src_x, int src_y, int dst_x, int dst_y, int w, int h)
9123e230dd2SCorentin Chary {
9133e230dd2SCorentin Chary     /* send bitblit op to the vnc client */
914bd023f95SCorentin Chary     vnc_lock_output(vs);
9153e230dd2SCorentin Chary     vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE);
9163e230dd2SCorentin Chary     vnc_write_u8(vs, 0);
9173e230dd2SCorentin Chary     vnc_write_u16(vs, 1); /* number of rects */
9183e230dd2SCorentin Chary     vnc_framebuffer_update(vs, dst_x, dst_y, w, h, VNC_ENCODING_COPYRECT);
9193e230dd2SCorentin Chary     vnc_write_u16(vs, src_x);
9203e230dd2SCorentin Chary     vnc_write_u16(vs, src_y);
921bd023f95SCorentin Chary     vnc_unlock_output(vs);
9223e230dd2SCorentin Chary     vnc_flush(vs);
9233e230dd2SCorentin Chary }
9243e230dd2SCorentin Chary 
9257c20b4a3SGerd Hoffmann static void vnc_dpy_copy(DisplayChangeListener *dcl,
9267c20b4a3SGerd Hoffmann                          int src_x, int src_y,
9277c20b4a3SGerd Hoffmann                          int dst_x, int dst_y, int w, int h)
9283e230dd2SCorentin Chary {
92921ef45d7SGerd Hoffmann     VncDisplay *vd = container_of(dcl, VncDisplay, dcl);
9303e230dd2SCorentin Chary     VncState *vs, *vn;
9313e230dd2SCorentin Chary     uint8_t *src_row;
9323e230dd2SCorentin Chary     uint8_t *dst_row;
9339f64916dSGerd Hoffmann     int i, x, y, pitch, inc, w_lim, s;
9343e230dd2SCorentin Chary     int cmp_bytes;
9353e230dd2SCorentin Chary 
9363e230dd2SCorentin Chary     vnc_refresh_server_surface(vd);
9373e230dd2SCorentin Chary     QTAILQ_FOREACH_SAFE(vs, &vd->clients, next, vn) {
9383e230dd2SCorentin Chary         if (vnc_has_feature(vs, VNC_FEATURE_COPYRECT)) {
9393e230dd2SCorentin Chary             vs->force_update = 1;
94038ee14f4SGerd Hoffmann             vnc_update_client(vs, 1, true);
9413e230dd2SCorentin Chary             /* vs might be free()ed here */
9423e230dd2SCorentin Chary         }
9433e230dd2SCorentin Chary     }
9443e230dd2SCorentin Chary 
9453e230dd2SCorentin Chary     /* do bitblit op on the local surface too */
9469f64916dSGerd Hoffmann     pitch = vnc_server_fb_stride(vd);
9479f64916dSGerd Hoffmann     src_row = vnc_server_fb_ptr(vd, src_x, src_y);
9489f64916dSGerd Hoffmann     dst_row = vnc_server_fb_ptr(vd, dst_x, dst_y);
9493e230dd2SCorentin Chary     y = dst_y;
9503e230dd2SCorentin Chary     inc = 1;
9513e230dd2SCorentin Chary     if (dst_y > src_y) {
9523e230dd2SCorentin Chary         /* copy backwards */
9533e230dd2SCorentin Chary         src_row += pitch * (h-1);
9543e230dd2SCorentin Chary         dst_row += pitch * (h-1);
9553e230dd2SCorentin Chary         pitch = -pitch;
9563e230dd2SCorentin Chary         y = dst_y + h - 1;
9573e230dd2SCorentin Chary         inc = -1;
9583e230dd2SCorentin Chary     }
959b4c85ddcSPeter Lieven     w_lim = w - (VNC_DIRTY_PIXELS_PER_BIT - (dst_x % VNC_DIRTY_PIXELS_PER_BIT));
960b4c85ddcSPeter Lieven     if (w_lim < 0) {
9613e230dd2SCorentin Chary         w_lim = w;
962b4c85ddcSPeter Lieven     } else {
963b4c85ddcSPeter Lieven         w_lim = w - (w_lim % VNC_DIRTY_PIXELS_PER_BIT);
964b4c85ddcSPeter Lieven     }
9653e230dd2SCorentin Chary     for (i = 0; i < h; i++) {
9663e230dd2SCorentin Chary         for (x = 0; x <= w_lim;
9673e230dd2SCorentin Chary                 x += s, src_row += cmp_bytes, dst_row += cmp_bytes) {
9683e230dd2SCorentin Chary             if (x == w_lim) {
9693e230dd2SCorentin Chary                 if ((s = w - w_lim) == 0)
9703e230dd2SCorentin Chary                     break;
9713e230dd2SCorentin Chary             } else if (!x) {
972b4c85ddcSPeter Lieven                 s = (VNC_DIRTY_PIXELS_PER_BIT -
973b4c85ddcSPeter Lieven                     (dst_x % VNC_DIRTY_PIXELS_PER_BIT));
9743e230dd2SCorentin Chary                 s = MIN(s, w_lim);
9753e230dd2SCorentin Chary             } else {
976b4c85ddcSPeter Lieven                 s = VNC_DIRTY_PIXELS_PER_BIT;
9773e230dd2SCorentin Chary             }
9789f64916dSGerd Hoffmann             cmp_bytes = s * VNC_SERVER_FB_BYTES;
9793e230dd2SCorentin Chary             if (memcmp(src_row, dst_row, cmp_bytes) == 0)
9803e230dd2SCorentin Chary                 continue;
9813e230dd2SCorentin Chary             memmove(dst_row, src_row, cmp_bytes);
9823e230dd2SCorentin Chary             QTAILQ_FOREACH(vs, &vd->clients, next) {
9833e230dd2SCorentin Chary                 if (!vnc_has_feature(vs, VNC_FEATURE_COPYRECT)) {
984b4c85ddcSPeter Lieven                     set_bit(((x + dst_x) / VNC_DIRTY_PIXELS_PER_BIT),
985b4c85ddcSPeter Lieven                             vs->dirty[y]);
9863e230dd2SCorentin Chary                 }
9873e230dd2SCorentin Chary             }
9883e230dd2SCorentin Chary         }
9899f64916dSGerd Hoffmann         src_row += pitch - w * VNC_SERVER_FB_BYTES;
9909f64916dSGerd Hoffmann         dst_row += pitch - w * VNC_SERVER_FB_BYTES;
9913e230dd2SCorentin Chary         y += inc;
9923e230dd2SCorentin Chary     }
9933e230dd2SCorentin Chary 
9943e230dd2SCorentin Chary     QTAILQ_FOREACH(vs, &vd->clients, next) {
9953e230dd2SCorentin Chary         if (vnc_has_feature(vs, VNC_FEATURE_COPYRECT)) {
9963e230dd2SCorentin Chary             vnc_copy(vs, src_x, src_y, dst_x, dst_y, w, h);
9973e230dd2SCorentin Chary         }
9983e230dd2SCorentin Chary     }
9993e230dd2SCorentin Chary }
10003e230dd2SCorentin Chary 
10017c20b4a3SGerd Hoffmann static void vnc_mouse_set(DisplayChangeListener *dcl,
10027c20b4a3SGerd Hoffmann                           int x, int y, int visible)
10033e230dd2SCorentin Chary {
10043e230dd2SCorentin Chary     /* can we ask the client(s) to move the pointer ??? */
10053e230dd2SCorentin Chary }
10063e230dd2SCorentin Chary 
10073e230dd2SCorentin Chary static int vnc_cursor_define(VncState *vs)
10083e230dd2SCorentin Chary {
10093e230dd2SCorentin Chary     QEMUCursor *c = vs->vd->cursor;
10103e230dd2SCorentin Chary     int isize;
10113e230dd2SCorentin Chary 
10123e230dd2SCorentin Chary     if (vnc_has_feature(vs, VNC_FEATURE_RICH_CURSOR)) {
1013d01f9595SCorentin Chary         vnc_lock_output(vs);
10143e230dd2SCorentin Chary         vnc_write_u8(vs,  VNC_MSG_SERVER_FRAMEBUFFER_UPDATE);
10153e230dd2SCorentin Chary         vnc_write_u8(vs,  0);  /*  padding     */
10163e230dd2SCorentin Chary         vnc_write_u16(vs, 1);  /*  # of rects  */
10173e230dd2SCorentin Chary         vnc_framebuffer_update(vs, c->hot_x, c->hot_y, c->width, c->height,
10183e230dd2SCorentin Chary                                VNC_ENCODING_RICH_CURSOR);
10199f64916dSGerd Hoffmann         isize = c->width * c->height * vs->client_pf.bytes_per_pixel;
10209f64916dSGerd Hoffmann         vnc_write_pixels_generic(vs, c->data, isize);
10213e230dd2SCorentin Chary         vnc_write(vs, vs->vd->cursor_mask, vs->vd->cursor_msize);
1022d01f9595SCorentin Chary         vnc_unlock_output(vs);
10233e230dd2SCorentin Chary         return 0;
10243e230dd2SCorentin Chary     }
10253e230dd2SCorentin Chary     return -1;
10263e230dd2SCorentin Chary }
10273e230dd2SCorentin Chary 
10287c20b4a3SGerd Hoffmann static void vnc_dpy_cursor_define(DisplayChangeListener *dcl,
10297c20b4a3SGerd Hoffmann                                   QEMUCursor *c)
10303e230dd2SCorentin Chary {
1031d616ccc5SGerd Hoffmann     VncDisplay *vd = container_of(dcl, VncDisplay, dcl);
10323e230dd2SCorentin Chary     VncState *vs;
10333e230dd2SCorentin Chary 
10343e230dd2SCorentin Chary     cursor_put(vd->cursor);
10357267c094SAnthony Liguori     g_free(vd->cursor_mask);
10363e230dd2SCorentin Chary 
10373e230dd2SCorentin Chary     vd->cursor = c;
10383e230dd2SCorentin Chary     cursor_get(vd->cursor);
10393e230dd2SCorentin Chary     vd->cursor_msize = cursor_get_mono_bpl(c) * c->height;
10407267c094SAnthony Liguori     vd->cursor_mask = g_malloc0(vd->cursor_msize);
10413e230dd2SCorentin Chary     cursor_get_mono_mask(c, 0, vd->cursor_mask);
10423e230dd2SCorentin Chary 
10433e230dd2SCorentin Chary     QTAILQ_FOREACH(vs, &vd->clients, next) {
10443e230dd2SCorentin Chary         vnc_cursor_define(vs);
10453e230dd2SCorentin Chary     }
10463e230dd2SCorentin Chary }
10473e230dd2SCorentin Chary 
10484769a881SChih-Min Chao static int find_and_clear_dirty_height(VncState *vs,
10496c71a539SCorentin Chary                                        int y, int last_x, int x, int height)
10503e230dd2SCorentin Chary {
10513e230dd2SCorentin Chary     int h;
10523e230dd2SCorentin Chary 
10536c71a539SCorentin Chary     for (h = 1; h < (height - y); h++) {
1054bc2429b9SCorentin Chary         if (!test_bit(last_x, vs->dirty[y + h])) {
10553e230dd2SCorentin Chary             break;
1056bc2429b9SCorentin Chary         }
1057863d7c91SPeter Lieven         bitmap_clear(vs->dirty[y + h], last_x, x - last_x);
10583e230dd2SCorentin Chary     }
10593e230dd2SCorentin Chary 
10603e230dd2SCorentin Chary     return h;
10613e230dd2SCorentin Chary }
10623e230dd2SCorentin Chary 
106338ee14f4SGerd Hoffmann static int vnc_update_client(VncState *vs, int has_dirty, bool sync)
10643e230dd2SCorentin Chary {
106563658280SGerd Hoffmann     vs->has_dirty += has_dirty;
10663e230dd2SCorentin Chary     if (vs->need_update && vs->csock != -1) {
10673e230dd2SCorentin Chary         VncDisplay *vd = vs->vd;
1068bd023f95SCorentin Chary         VncJob *job;
10693e230dd2SCorentin Chary         int y;
10702f487a3dSPeter Lieven         int height, width;
1071bd023f95SCorentin Chary         int n = 0;
1072bd023f95SCorentin Chary 
10733e230dd2SCorentin Chary         if (vs->output.offset && !vs->audio_cap && !vs->force_update)
10743e230dd2SCorentin Chary             /* kernel send buffers are full -> drop frames to throttle */
10753e230dd2SCorentin Chary             return 0;
10763e230dd2SCorentin Chary 
107763658280SGerd Hoffmann         if (!vs->has_dirty && !vs->audio_cap && !vs->force_update)
10783e230dd2SCorentin Chary             return 0;
10793e230dd2SCorentin Chary 
10803e230dd2SCorentin Chary         /*
10813e230dd2SCorentin Chary          * Send screen updates to the vnc client using the server
10823e230dd2SCorentin Chary          * surface and server dirty map.  guest surface updates
10833e230dd2SCorentin Chary          * happening in parallel don't disturb us, the next pass will
10843e230dd2SCorentin Chary          * send them to the client.
10853e230dd2SCorentin Chary          */
1086bd023f95SCorentin Chary         job = vnc_job_new(vs);
10873e230dd2SCorentin Chary 
1088bea60dd7SPeter Lieven         height = pixman_image_get_height(vd->server);
1089bea60dd7SPeter Lieven         width = pixman_image_get_width(vd->server);
10903e230dd2SCorentin Chary 
109112b316d4SPeter Lieven         y = 0;
109212b316d4SPeter Lieven         for (;;) {
109312b316d4SPeter Lieven             int x, h;
109412b316d4SPeter Lieven             unsigned long x2;
109512b316d4SPeter Lieven             unsigned long offset = find_next_bit((unsigned long *) &vs->dirty,
109612b316d4SPeter Lieven                                                  height * VNC_DIRTY_BPL(vs),
109712b316d4SPeter Lieven                                                  y * VNC_DIRTY_BPL(vs));
109812b316d4SPeter Lieven             if (offset == height * VNC_DIRTY_BPL(vs)) {
109912b316d4SPeter Lieven                 /* no more dirty bits */
110012b316d4SPeter Lieven                 break;
11013e230dd2SCorentin Chary             }
110212b316d4SPeter Lieven             y = offset / VNC_DIRTY_BPL(vs);
110312b316d4SPeter Lieven             x = offset % VNC_DIRTY_BPL(vs);
110412b316d4SPeter Lieven             x2 = find_next_zero_bit((unsigned long *) &vs->dirty[y],
110512b316d4SPeter Lieven                                     VNC_DIRTY_BPL(vs), x);
110612b316d4SPeter Lieven             bitmap_clear(vs->dirty[y], x, x2 - x);
110712b316d4SPeter Lieven             h = find_and_clear_dirty_height(vs, y, x, x2, height);
11082f487a3dSPeter Lieven             x2 = MIN(x2, width / VNC_DIRTY_PIXELS_PER_BIT);
11092f487a3dSPeter Lieven             if (x2 > x) {
111012b316d4SPeter Lieven                 n += vnc_job_add_rect(job, x * VNC_DIRTY_PIXELS_PER_BIT, y,
111112b316d4SPeter Lieven                                       (x2 - x) * VNC_DIRTY_PIXELS_PER_BIT, h);
11123e230dd2SCorentin Chary             }
11130e7d6f60SPeter Lieven             if (!x && x2 == width / VNC_DIRTY_PIXELS_PER_BIT) {
11140e7d6f60SPeter Lieven                 y += h;
11150e7d6f60SPeter Lieven                 if (y == height) {
11160e7d6f60SPeter Lieven                     break;
11170e7d6f60SPeter Lieven                 }
11180e7d6f60SPeter Lieven             }
11192f487a3dSPeter Lieven         }
1120bd023f95SCorentin Chary 
1121bd023f95SCorentin Chary         vnc_job_push(job);
1122eb214ff8SGerd Hoffmann         if (sync) {
1123eb214ff8SGerd Hoffmann             vnc_jobs_join(vs);
1124eb214ff8SGerd Hoffmann         }
11253e230dd2SCorentin Chary         vs->force_update = 0;
112663658280SGerd Hoffmann         vs->has_dirty = 0;
1127bd023f95SCorentin Chary         return n;
11283e230dd2SCorentin Chary     }
11293e230dd2SCorentin Chary 
113038ee14f4SGerd Hoffmann     if (vs->csock == -1) {
11313e230dd2SCorentin Chary         vnc_disconnect_finish(vs);
113238ee14f4SGerd Hoffmann     } else if (sync) {
113338ee14f4SGerd Hoffmann         vnc_jobs_join(vs);
113438ee14f4SGerd Hoffmann     }
11353e230dd2SCorentin Chary 
11363e230dd2SCorentin Chary     return 0;
11373e230dd2SCorentin Chary }
11383e230dd2SCorentin Chary 
11393e230dd2SCorentin Chary /* audio */
11403e230dd2SCorentin Chary static void audio_capture_notify(void *opaque, audcnotification_e cmd)
11413e230dd2SCorentin Chary {
11423e230dd2SCorentin Chary     VncState *vs = opaque;
11433e230dd2SCorentin Chary 
11443e230dd2SCorentin Chary     switch (cmd) {
11453e230dd2SCorentin Chary     case AUD_CNOTIFY_DISABLE:
1146bd023f95SCorentin Chary         vnc_lock_output(vs);
11473e230dd2SCorentin Chary         vnc_write_u8(vs, VNC_MSG_SERVER_QEMU);
11483e230dd2SCorentin Chary         vnc_write_u8(vs, VNC_MSG_SERVER_QEMU_AUDIO);
11493e230dd2SCorentin Chary         vnc_write_u16(vs, VNC_MSG_SERVER_QEMU_AUDIO_END);
1150bd023f95SCorentin Chary         vnc_unlock_output(vs);
11513e230dd2SCorentin Chary         vnc_flush(vs);
11523e230dd2SCorentin Chary         break;
11533e230dd2SCorentin Chary 
11543e230dd2SCorentin Chary     case AUD_CNOTIFY_ENABLE:
1155bd023f95SCorentin Chary         vnc_lock_output(vs);
11563e230dd2SCorentin Chary         vnc_write_u8(vs, VNC_MSG_SERVER_QEMU);
11573e230dd2SCorentin Chary         vnc_write_u8(vs, VNC_MSG_SERVER_QEMU_AUDIO);
11583e230dd2SCorentin Chary         vnc_write_u16(vs, VNC_MSG_SERVER_QEMU_AUDIO_BEGIN);
1159bd023f95SCorentin Chary         vnc_unlock_output(vs);
11603e230dd2SCorentin Chary         vnc_flush(vs);
11613e230dd2SCorentin Chary         break;
11623e230dd2SCorentin Chary     }
11633e230dd2SCorentin Chary }
11643e230dd2SCorentin Chary 
11653e230dd2SCorentin Chary static void audio_capture_destroy(void *opaque)
11663e230dd2SCorentin Chary {
11673e230dd2SCorentin Chary }
11683e230dd2SCorentin Chary 
11693e230dd2SCorentin Chary static void audio_capture(void *opaque, void *buf, int size)
11703e230dd2SCorentin Chary {
11713e230dd2SCorentin Chary     VncState *vs = opaque;
11723e230dd2SCorentin Chary 
1173bd023f95SCorentin Chary     vnc_lock_output(vs);
11743e230dd2SCorentin Chary     vnc_write_u8(vs, VNC_MSG_SERVER_QEMU);
11753e230dd2SCorentin Chary     vnc_write_u8(vs, VNC_MSG_SERVER_QEMU_AUDIO);
11763e230dd2SCorentin Chary     vnc_write_u16(vs, VNC_MSG_SERVER_QEMU_AUDIO_DATA);
11773e230dd2SCorentin Chary     vnc_write_u32(vs, size);
11783e230dd2SCorentin Chary     vnc_write(vs, buf, size);
1179bd023f95SCorentin Chary     vnc_unlock_output(vs);
11803e230dd2SCorentin Chary     vnc_flush(vs);
11813e230dd2SCorentin Chary }
11823e230dd2SCorentin Chary 
11833e230dd2SCorentin Chary static void audio_add(VncState *vs)
11843e230dd2SCorentin Chary {
11853e230dd2SCorentin Chary     struct audio_capture_ops ops;
11863e230dd2SCorentin Chary 
11873e230dd2SCorentin Chary     if (vs->audio_cap) {
1188027a79c3SCole Robinson         error_report("audio already running");
11893e230dd2SCorentin Chary         return;
11903e230dd2SCorentin Chary     }
11913e230dd2SCorentin Chary 
11923e230dd2SCorentin Chary     ops.notify = audio_capture_notify;
11933e230dd2SCorentin Chary     ops.destroy = audio_capture_destroy;
11943e230dd2SCorentin Chary     ops.capture = audio_capture;
11953e230dd2SCorentin Chary 
11963e230dd2SCorentin Chary     vs->audio_cap = AUD_add_capture(&vs->as, &ops, vs);
11973e230dd2SCorentin Chary     if (!vs->audio_cap) {
1198027a79c3SCole Robinson         error_report("Failed to add audio capture");
11993e230dd2SCorentin Chary     }
12003e230dd2SCorentin Chary }
12013e230dd2SCorentin Chary 
12023e230dd2SCorentin Chary static void audio_del(VncState *vs)
12033e230dd2SCorentin Chary {
12043e230dd2SCorentin Chary     if (vs->audio_cap) {
12053e230dd2SCorentin Chary         AUD_del_capture(vs->audio_cap, vs);
12063e230dd2SCorentin Chary         vs->audio_cap = NULL;
12073e230dd2SCorentin Chary     }
12083e230dd2SCorentin Chary }
12093e230dd2SCorentin Chary 
12103e230dd2SCorentin Chary static void vnc_disconnect_start(VncState *vs)
12113e230dd2SCorentin Chary {
12123e230dd2SCorentin Chary     if (vs->csock == -1)
12133e230dd2SCorentin Chary         return;
12148cf36489SGerd Hoffmann     vnc_set_share_mode(vs, VNC_SHARE_MODE_DISCONNECTED);
121582e1cc4bSFam Zheng     qemu_set_fd_handler(vs->csock, NULL, NULL, NULL);
12163e230dd2SCorentin Chary     closesocket(vs->csock);
12173e230dd2SCorentin Chary     vs->csock = -1;
12183e230dd2SCorentin Chary }
12193e230dd2SCorentin Chary 
12207536ee4bSTim Hardeck void vnc_disconnect_finish(VncState *vs)
12213e230dd2SCorentin Chary {
12227d964c9dSCorentin Chary     int i;
12237d964c9dSCorentin Chary 
1224bd023f95SCorentin Chary     vnc_jobs_join(vs); /* Wait encoding jobs */
1225bd023f95SCorentin Chary 
1226bd023f95SCorentin Chary     vnc_lock_output(vs);
1227fb6ba0d5SWenchao Xia     vnc_qmp_event(vs, QAPI_EVENT_VNC_DISCONNECTED);
12283e230dd2SCorentin Chary 
12293e230dd2SCorentin Chary     buffer_free(&vs->input);
12303e230dd2SCorentin Chary     buffer_free(&vs->output);
12317536ee4bSTim Hardeck     buffer_free(&vs->ws_input);
12327536ee4bSTim Hardeck     buffer_free(&vs->ws_output);
12333e230dd2SCorentin Chary 
1234fb6ba0d5SWenchao Xia     qapi_free_VncClientInfo(vs->info);
12353e230dd2SCorentin Chary 
12363e230dd2SCorentin Chary     vnc_zlib_clear(vs);
12373e230dd2SCorentin Chary     vnc_tight_clear(vs);
1238148954faSCorentin Chary     vnc_zrle_clear(vs);
12393e230dd2SCorentin Chary 
12403e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS
12413e230dd2SCorentin Chary     vnc_tls_client_cleanup(vs);
12423e230dd2SCorentin Chary #endif /* CONFIG_VNC_TLS */
12433e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL
12443e230dd2SCorentin Chary     vnc_sasl_client_cleanup(vs);
12453e230dd2SCorentin Chary #endif /* CONFIG_VNC_SASL */
12463e230dd2SCorentin Chary     audio_del(vs);
12477bc9318bSGerd Hoffmann     vnc_release_modifiers(vs);
12483e230dd2SCorentin Chary 
12496fd8e79aSTim Hardeck     if (vs->initialized) {
12503e230dd2SCorentin Chary         QTAILQ_REMOVE(&vs->vd->clients, vs, next);
12516fd8e79aSTim Hardeck         qemu_remove_mouse_mode_change_notifier(&vs->mouse_mode_notifier);
12526fd8e79aSTim Hardeck     }
12533e230dd2SCorentin Chary 
12543e230dd2SCorentin Chary     if (vs->vd->lock_key_sync)
12553e230dd2SCorentin Chary         qemu_remove_led_event_handler(vs->led);
1256bd023f95SCorentin Chary     vnc_unlock_output(vs);
1257bd023f95SCorentin Chary 
1258bd023f95SCorentin Chary     qemu_mutex_destroy(&vs->output_mutex);
12596fd8e79aSTim Hardeck     if (vs->bh != NULL) {
1260175b2a6eSCorentin Chary         qemu_bh_delete(vs->bh);
12616fd8e79aSTim Hardeck     }
1262175b2a6eSCorentin Chary     buffer_free(&vs->jobs_buffer);
1263175b2a6eSCorentin Chary 
12647d964c9dSCorentin Chary     for (i = 0; i < VNC_STAT_ROWS; ++i) {
12657267c094SAnthony Liguori         g_free(vs->lossy_rect[i]);
12667d964c9dSCorentin Chary     }
12677267c094SAnthony Liguori     g_free(vs->lossy_rect);
12687267c094SAnthony Liguori     g_free(vs);
12693e230dd2SCorentin Chary }
12703e230dd2SCorentin Chary 
1271*fdd1ab6aSDaniel P. Berrange ssize_t vnc_client_io_error(VncState *vs, ssize_t ret, int last_errno)
12723e230dd2SCorentin Chary {
12733e230dd2SCorentin Chary     if (ret == 0 || ret == -1) {
12743e230dd2SCorentin Chary         if (ret == -1) {
12753e230dd2SCorentin Chary             switch (last_errno) {
12763e230dd2SCorentin Chary                 case EINTR:
12773e230dd2SCorentin Chary                 case EAGAIN:
12783e230dd2SCorentin Chary #ifdef _WIN32
12793e230dd2SCorentin Chary                 case WSAEWOULDBLOCK:
12803e230dd2SCorentin Chary #endif
12813e230dd2SCorentin Chary                     return 0;
12823e230dd2SCorentin Chary                 default:
12833e230dd2SCorentin Chary                     break;
12843e230dd2SCorentin Chary             }
12853e230dd2SCorentin Chary         }
12863e230dd2SCorentin Chary 
1287*fdd1ab6aSDaniel P. Berrange         VNC_DEBUG("Closing down client sock: ret %zd, errno %d\n",
12883e230dd2SCorentin Chary                   ret, ret < 0 ? last_errno : 0);
12893e230dd2SCorentin Chary         vnc_disconnect_start(vs);
12903e230dd2SCorentin Chary 
12913e230dd2SCorentin Chary         return 0;
12923e230dd2SCorentin Chary     }
12933e230dd2SCorentin Chary     return ret;
12943e230dd2SCorentin Chary }
12953e230dd2SCorentin Chary 
12963e230dd2SCorentin Chary 
12973e230dd2SCorentin Chary void vnc_client_error(VncState *vs)
12983e230dd2SCorentin Chary {
12993e230dd2SCorentin Chary     VNC_DEBUG("Closing down client sock: protocol error\n");
13003e230dd2SCorentin Chary     vnc_disconnect_start(vs);
13013e230dd2SCorentin Chary }
13023e230dd2SCorentin Chary 
13030057a0d5STim Hardeck #ifdef CONFIG_VNC_TLS
1304*fdd1ab6aSDaniel P. Berrange static ssize_t vnc_client_write_tls(gnutls_session_t *session,
13050057a0d5STim Hardeck                                     const uint8_t *data,
13060057a0d5STim Hardeck                                     size_t datalen)
13070057a0d5STim Hardeck {
1308*fdd1ab6aSDaniel P. Berrange     ssize_t ret = gnutls_write(*session, data, datalen);
13090057a0d5STim Hardeck     if (ret < 0) {
13100057a0d5STim Hardeck         if (ret == GNUTLS_E_AGAIN) {
13110057a0d5STim Hardeck             errno = EAGAIN;
13120057a0d5STim Hardeck         } else {
13130057a0d5STim Hardeck             errno = EIO;
13140057a0d5STim Hardeck         }
13150057a0d5STim Hardeck         ret = -1;
13160057a0d5STim Hardeck     }
13170057a0d5STim Hardeck     return ret;
13180057a0d5STim Hardeck }
13190057a0d5STim Hardeck #endif /* CONFIG_VNC_TLS */
13203e230dd2SCorentin Chary 
13213e230dd2SCorentin Chary /*
13223e230dd2SCorentin Chary  * Called to write a chunk of data to the client socket. The data may
13233e230dd2SCorentin Chary  * be the raw data, or may have already been encoded by SASL.
13243e230dd2SCorentin Chary  * The data will be written either straight onto the socket, or
13253e230dd2SCorentin Chary  * written via the GNUTLS wrappers, if TLS/SSL encryption is enabled
13263e230dd2SCorentin Chary  *
13273e230dd2SCorentin Chary  * NB, it is theoretically possible to have 2 layers of encryption,
13283e230dd2SCorentin Chary  * both SASL, and this TLS layer. It is highly unlikely in practice
13293e230dd2SCorentin Chary  * though, since SASL encryption will typically be a no-op if TLS
13303e230dd2SCorentin Chary  * is active
13313e230dd2SCorentin Chary  *
13323e230dd2SCorentin Chary  * Returns the number of bytes written, which may be less than
13333e230dd2SCorentin Chary  * the requested 'datalen' if the socket would block. Returns
13343e230dd2SCorentin Chary  * -1 on error, and disconnects the client socket.
13353e230dd2SCorentin Chary  */
1336*fdd1ab6aSDaniel P. Berrange ssize_t vnc_client_write_buf(VncState *vs, const uint8_t *data, size_t datalen)
13373e230dd2SCorentin Chary {
1338*fdd1ab6aSDaniel P. Berrange     ssize_t ret;
13393e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS
13403e230dd2SCorentin Chary     if (vs->tls.session) {
13410057a0d5STim Hardeck         ret = vnc_client_write_tls(&vs->tls.session, data, datalen);
13420057a0d5STim Hardeck     } else {
13433e230dd2SCorentin Chary #endif /* CONFIG_VNC_TLS */
13443e230dd2SCorentin Chary         ret = send(vs->csock, (const void *)data, datalen, 0);
13450057a0d5STim Hardeck #ifdef CONFIG_VNC_TLS
13460057a0d5STim Hardeck     }
13470057a0d5STim Hardeck #endif /* CONFIG_VNC_TLS */
13483e230dd2SCorentin Chary     VNC_DEBUG("Wrote wire %p %zd -> %ld\n", data, datalen, ret);
13493e230dd2SCorentin Chary     return vnc_client_io_error(vs, ret, socket_error());
13503e230dd2SCorentin Chary }
13513e230dd2SCorentin Chary 
13523e230dd2SCorentin Chary 
13533e230dd2SCorentin Chary /*
13543e230dd2SCorentin Chary  * Called to write buffered data to the client socket, when not
13553e230dd2SCorentin Chary  * using any SASL SSF encryption layers. Will write as much data
13563e230dd2SCorentin Chary  * as possible without blocking. If all buffered data is written,
13573e230dd2SCorentin Chary  * will switch the FD poll() handler back to read monitoring.
13583e230dd2SCorentin Chary  *
13593e230dd2SCorentin Chary  * Returns the number of bytes written, which may be less than
13603e230dd2SCorentin Chary  * the buffered output data if the socket would block. Returns
13613e230dd2SCorentin Chary  * -1 on error, and disconnects the client socket.
13623e230dd2SCorentin Chary  */
1363*fdd1ab6aSDaniel P. Berrange static ssize_t vnc_client_write_plain(VncState *vs)
13643e230dd2SCorentin Chary {
1365*fdd1ab6aSDaniel P. Berrange     ssize_t ret;
13663e230dd2SCorentin Chary 
13673e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL
13683e230dd2SCorentin Chary     VNC_DEBUG("Write Plain: Pending output %p size %zd offset %zd. Wait SSF %d\n",
13693e230dd2SCorentin Chary               vs->output.buffer, vs->output.capacity, vs->output.offset,
13703e230dd2SCorentin Chary               vs->sasl.waitWriteSSF);
13713e230dd2SCorentin Chary 
13723e230dd2SCorentin Chary     if (vs->sasl.conn &&
13733e230dd2SCorentin Chary         vs->sasl.runSSF &&
13743e230dd2SCorentin Chary         vs->sasl.waitWriteSSF) {
13753e230dd2SCorentin Chary         ret = vnc_client_write_buf(vs, vs->output.buffer, vs->sasl.waitWriteSSF);
13763e230dd2SCorentin Chary         if (ret)
13773e230dd2SCorentin Chary             vs->sasl.waitWriteSSF -= ret;
13783e230dd2SCorentin Chary     } else
13793e230dd2SCorentin Chary #endif /* CONFIG_VNC_SASL */
13803e230dd2SCorentin Chary         ret = vnc_client_write_buf(vs, vs->output.buffer, vs->output.offset);
13813e230dd2SCorentin Chary     if (!ret)
13823e230dd2SCorentin Chary         return 0;
13833e230dd2SCorentin Chary 
138432ed2680STim Hardeck     buffer_advance(&vs->output, ret);
13853e230dd2SCorentin Chary 
13863e230dd2SCorentin Chary     if (vs->output.offset == 0) {
138782e1cc4bSFam Zheng         qemu_set_fd_handler(vs->csock, vnc_client_read, NULL, vs);
13883e230dd2SCorentin Chary     }
13893e230dd2SCorentin Chary 
13903e230dd2SCorentin Chary     return ret;
13913e230dd2SCorentin Chary }
13923e230dd2SCorentin Chary 
13933e230dd2SCorentin Chary 
13943e230dd2SCorentin Chary /*
13953e230dd2SCorentin Chary  * First function called whenever there is data to be written to
13963e230dd2SCorentin Chary  * the client socket. Will delegate actual work according to whether
13973e230dd2SCorentin Chary  * SASL SSF layers are enabled (thus requiring encryption calls)
13983e230dd2SCorentin Chary  */
1399bd023f95SCorentin Chary static void vnc_client_write_locked(void *opaque)
14003e230dd2SCorentin Chary {
14013e230dd2SCorentin Chary     VncState *vs = opaque;
14023e230dd2SCorentin Chary 
14033e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL
14043e230dd2SCorentin Chary     if (vs->sasl.conn &&
14053e230dd2SCorentin Chary         vs->sasl.runSSF &&
14063e230dd2SCorentin Chary         !vs->sasl.waitWriteSSF) {
14073e230dd2SCorentin Chary         vnc_client_write_sasl(vs);
14083e230dd2SCorentin Chary     } else
14093e230dd2SCorentin Chary #endif /* CONFIG_VNC_SASL */
14107536ee4bSTim Hardeck     {
14117536ee4bSTim Hardeck         if (vs->encode_ws) {
14127536ee4bSTim Hardeck             vnc_client_write_ws(vs);
14138e9b0d24SDaniel P. Berrange         } else {
14143e230dd2SCorentin Chary             vnc_client_write_plain(vs);
14153e230dd2SCorentin Chary         }
14167536ee4bSTim Hardeck     }
14177536ee4bSTim Hardeck }
14183e230dd2SCorentin Chary 
1419bd023f95SCorentin Chary void vnc_client_write(void *opaque)
1420bd023f95SCorentin Chary {
1421bd023f95SCorentin Chary     VncState *vs = opaque;
1422bd023f95SCorentin Chary 
1423bd023f95SCorentin Chary     vnc_lock_output(vs);
14248e9b0d24SDaniel P. Berrange     if (vs->output.offset || vs->ws_output.offset) {
1425bd023f95SCorentin Chary         vnc_client_write_locked(opaque);
1426ac71103dSYoshiaki Tamura     } else if (vs->csock != -1) {
142782e1cc4bSFam Zheng         qemu_set_fd_handler(vs->csock, vnc_client_read, NULL, vs);
1428bd023f95SCorentin Chary     }
1429bd023f95SCorentin Chary     vnc_unlock_output(vs);
1430bd023f95SCorentin Chary }
1431bd023f95SCorentin Chary 
14323e230dd2SCorentin Chary void vnc_read_when(VncState *vs, VncReadEvent *func, size_t expecting)
14333e230dd2SCorentin Chary {
14343e230dd2SCorentin Chary     vs->read_handler = func;
14353e230dd2SCorentin Chary     vs->read_handler_expect = expecting;
14363e230dd2SCorentin Chary }
14373e230dd2SCorentin Chary 
14380057a0d5STim Hardeck #ifdef CONFIG_VNC_TLS
1439*fdd1ab6aSDaniel P. Berrange static ssize_t vnc_client_read_tls(gnutls_session_t *session, uint8_t *data,
14400057a0d5STim Hardeck                                    size_t datalen)
14410057a0d5STim Hardeck {
1442*fdd1ab6aSDaniel P. Berrange     ssize_t ret = gnutls_read(*session, data, datalen);
14430057a0d5STim Hardeck     if (ret < 0) {
14440057a0d5STim Hardeck         if (ret == GNUTLS_E_AGAIN) {
14450057a0d5STim Hardeck             errno = EAGAIN;
14460057a0d5STim Hardeck         } else {
14470057a0d5STim Hardeck             errno = EIO;
14480057a0d5STim Hardeck         }
14490057a0d5STim Hardeck         ret = -1;
14500057a0d5STim Hardeck     }
14510057a0d5STim Hardeck     return ret;
14520057a0d5STim Hardeck }
14530057a0d5STim Hardeck #endif /* CONFIG_VNC_TLS */
14543e230dd2SCorentin Chary 
14553e230dd2SCorentin Chary /*
14563e230dd2SCorentin Chary  * Called to read a chunk of data from the client socket. The data may
14573e230dd2SCorentin Chary  * be the raw data, or may need to be further decoded by SASL.
14583e230dd2SCorentin Chary  * The data will be read either straight from to the socket, or
14593e230dd2SCorentin Chary  * read via the GNUTLS wrappers, if TLS/SSL encryption is enabled
14603e230dd2SCorentin Chary  *
14613e230dd2SCorentin Chary  * NB, it is theoretically possible to have 2 layers of encryption,
14623e230dd2SCorentin Chary  * both SASL, and this TLS layer. It is highly unlikely in practice
14633e230dd2SCorentin Chary  * though, since SASL encryption will typically be a no-op if TLS
14643e230dd2SCorentin Chary  * is active
14653e230dd2SCorentin Chary  *
14663e230dd2SCorentin Chary  * Returns the number of bytes read, which may be less than
14673e230dd2SCorentin Chary  * the requested 'datalen' if the socket would block. Returns
14683e230dd2SCorentin Chary  * -1 on error, and disconnects the client socket.
14693e230dd2SCorentin Chary  */
1470*fdd1ab6aSDaniel P. Berrange ssize_t vnc_client_read_buf(VncState *vs, uint8_t *data, size_t datalen)
14713e230dd2SCorentin Chary {
1472*fdd1ab6aSDaniel P. Berrange     ssize_t ret;
14733e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS
14743e230dd2SCorentin Chary     if (vs->tls.session) {
14750057a0d5STim Hardeck         ret = vnc_client_read_tls(&vs->tls.session, data, datalen);
14760057a0d5STim Hardeck     } else {
14773e230dd2SCorentin Chary #endif /* CONFIG_VNC_TLS */
147800aa0040SBlue Swirl         ret = qemu_recv(vs->csock, data, datalen, 0);
14790057a0d5STim Hardeck #ifdef CONFIG_VNC_TLS
14800057a0d5STim Hardeck     }
14810057a0d5STim Hardeck #endif /* CONFIG_VNC_TLS */
14823e230dd2SCorentin Chary     VNC_DEBUG("Read wire %p %zd -> %ld\n", data, datalen, ret);
14833e230dd2SCorentin Chary     return vnc_client_io_error(vs, ret, socket_error());
14843e230dd2SCorentin Chary }
14853e230dd2SCorentin Chary 
14863e230dd2SCorentin Chary 
14873e230dd2SCorentin Chary /*
14883e230dd2SCorentin Chary  * Called to read data from the client socket to the input buffer,
14893e230dd2SCorentin Chary  * when not using any SASL SSF encryption layers. Will read as much
14903e230dd2SCorentin Chary  * data as possible without blocking.
14913e230dd2SCorentin Chary  *
14923e230dd2SCorentin Chary  * Returns the number of bytes read. Returns -1 on error, and
14933e230dd2SCorentin Chary  * disconnects the client socket.
14943e230dd2SCorentin Chary  */
1495*fdd1ab6aSDaniel P. Berrange static ssize_t vnc_client_read_plain(VncState *vs)
14963e230dd2SCorentin Chary {
1497*fdd1ab6aSDaniel P. Berrange     ssize_t ret;
14983e230dd2SCorentin Chary     VNC_DEBUG("Read plain %p size %zd offset %zd\n",
14993e230dd2SCorentin Chary               vs->input.buffer, vs->input.capacity, vs->input.offset);
15003e230dd2SCorentin Chary     buffer_reserve(&vs->input, 4096);
15013e230dd2SCorentin Chary     ret = vnc_client_read_buf(vs, buffer_end(&vs->input), 4096);
15023e230dd2SCorentin Chary     if (!ret)
15033e230dd2SCorentin Chary         return 0;
15043e230dd2SCorentin Chary     vs->input.offset += ret;
15053e230dd2SCorentin Chary     return ret;
15063e230dd2SCorentin Chary }
15073e230dd2SCorentin Chary 
1508175b2a6eSCorentin Chary static void vnc_jobs_bh(void *opaque)
1509175b2a6eSCorentin Chary {
1510175b2a6eSCorentin Chary     VncState *vs = opaque;
1511175b2a6eSCorentin Chary 
1512175b2a6eSCorentin Chary     vnc_jobs_consume_buffer(vs);
1513175b2a6eSCorentin Chary }
15143e230dd2SCorentin Chary 
15153e230dd2SCorentin Chary /*
15163e230dd2SCorentin Chary  * First function called whenever there is more data to be read from
15173e230dd2SCorentin Chary  * the client socket. Will delegate actual work according to whether
15183e230dd2SCorentin Chary  * SASL SSF layers are enabled (thus requiring decryption calls)
15193e230dd2SCorentin Chary  */
15203e230dd2SCorentin Chary void vnc_client_read(void *opaque)
15213e230dd2SCorentin Chary {
15223e230dd2SCorentin Chary     VncState *vs = opaque;
1523*fdd1ab6aSDaniel P. Berrange     ssize_t ret;
15243e230dd2SCorentin Chary 
15253e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL
15263e230dd2SCorentin Chary     if (vs->sasl.conn && vs->sasl.runSSF)
15273e230dd2SCorentin Chary         ret = vnc_client_read_sasl(vs);
15283e230dd2SCorentin Chary     else
15293e230dd2SCorentin Chary #endif /* CONFIG_VNC_SASL */
15307536ee4bSTim Hardeck         if (vs->encode_ws) {
15317536ee4bSTim Hardeck             ret = vnc_client_read_ws(vs);
15327536ee4bSTim Hardeck             if (ret == -1) {
15337536ee4bSTim Hardeck                 vnc_disconnect_start(vs);
15347536ee4bSTim Hardeck                 return;
15357536ee4bSTim Hardeck             } else if (ret == -2) {
15367536ee4bSTim Hardeck                 vnc_client_error(vs);
15377536ee4bSTim Hardeck                 return;
15387536ee4bSTim Hardeck             }
15398e9b0d24SDaniel P. Berrange         } else {
15403e230dd2SCorentin Chary             ret = vnc_client_read_plain(vs);
15417536ee4bSTim Hardeck         }
15423e230dd2SCorentin Chary     if (!ret) {
15433e230dd2SCorentin Chary         if (vs->csock == -1)
15443e230dd2SCorentin Chary             vnc_disconnect_finish(vs);
15453e230dd2SCorentin Chary         return;
15463e230dd2SCorentin Chary     }
15473e230dd2SCorentin Chary 
15483e230dd2SCorentin Chary     while (vs->read_handler && vs->input.offset >= vs->read_handler_expect) {
15493e230dd2SCorentin Chary         size_t len = vs->read_handler_expect;
15503e230dd2SCorentin Chary         int ret;
15513e230dd2SCorentin Chary 
15523e230dd2SCorentin Chary         ret = vs->read_handler(vs, vs->input.buffer, len);
15533e230dd2SCorentin Chary         if (vs->csock == -1) {
15543e230dd2SCorentin Chary             vnc_disconnect_finish(vs);
15553e230dd2SCorentin Chary             return;
15563e230dd2SCorentin Chary         }
15573e230dd2SCorentin Chary 
15583e230dd2SCorentin Chary         if (!ret) {
155932ed2680STim Hardeck             buffer_advance(&vs->input, len);
15603e230dd2SCorentin Chary         } else {
15613e230dd2SCorentin Chary             vs->read_handler_expect = ret;
15623e230dd2SCorentin Chary         }
15633e230dd2SCorentin Chary     }
15643e230dd2SCorentin Chary }
15653e230dd2SCorentin Chary 
15663e230dd2SCorentin Chary void vnc_write(VncState *vs, const void *data, size_t len)
15673e230dd2SCorentin Chary {
15683e230dd2SCorentin Chary     buffer_reserve(&vs->output, len);
15693e230dd2SCorentin Chary 
15703e230dd2SCorentin Chary     if (vs->csock != -1 && buffer_empty(&vs->output)) {
157182e1cc4bSFam Zheng         qemu_set_fd_handler(vs->csock, vnc_client_read, vnc_client_write, vs);
15723e230dd2SCorentin Chary     }
15733e230dd2SCorentin Chary 
15743e230dd2SCorentin Chary     buffer_append(&vs->output, data, len);
15753e230dd2SCorentin Chary }
15763e230dd2SCorentin Chary 
15773e230dd2SCorentin Chary void vnc_write_s32(VncState *vs, int32_t value)
15783e230dd2SCorentin Chary {
15793e230dd2SCorentin Chary     vnc_write_u32(vs, *(uint32_t *)&value);
15803e230dd2SCorentin Chary }
15813e230dd2SCorentin Chary 
15823e230dd2SCorentin Chary void vnc_write_u32(VncState *vs, uint32_t value)
15833e230dd2SCorentin Chary {
15843e230dd2SCorentin Chary     uint8_t buf[4];
15853e230dd2SCorentin Chary 
15863e230dd2SCorentin Chary     buf[0] = (value >> 24) & 0xFF;
15873e230dd2SCorentin Chary     buf[1] = (value >> 16) & 0xFF;
15883e230dd2SCorentin Chary     buf[2] = (value >>  8) & 0xFF;
15893e230dd2SCorentin Chary     buf[3] = value & 0xFF;
15903e230dd2SCorentin Chary 
15913e230dd2SCorentin Chary     vnc_write(vs, buf, 4);
15923e230dd2SCorentin Chary }
15933e230dd2SCorentin Chary 
15943e230dd2SCorentin Chary void vnc_write_u16(VncState *vs, uint16_t value)
15953e230dd2SCorentin Chary {
15963e230dd2SCorentin Chary     uint8_t buf[2];
15973e230dd2SCorentin Chary 
15983e230dd2SCorentin Chary     buf[0] = (value >> 8) & 0xFF;
15993e230dd2SCorentin Chary     buf[1] = value & 0xFF;
16003e230dd2SCorentin Chary 
16013e230dd2SCorentin Chary     vnc_write(vs, buf, 2);
16023e230dd2SCorentin Chary }
16033e230dd2SCorentin Chary 
16043e230dd2SCorentin Chary void vnc_write_u8(VncState *vs, uint8_t value)
16053e230dd2SCorentin Chary {
16063e230dd2SCorentin Chary     vnc_write(vs, (char *)&value, 1);
16073e230dd2SCorentin Chary }
16083e230dd2SCorentin Chary 
16093e230dd2SCorentin Chary void vnc_flush(VncState *vs)
16103e230dd2SCorentin Chary {
1611bd023f95SCorentin Chary     vnc_lock_output(vs);
16128e9b0d24SDaniel P. Berrange     if (vs->csock != -1 && (vs->output.offset ||
16138e9b0d24SDaniel P. Berrange                             vs->ws_output.offset)) {
1614bd023f95SCorentin Chary         vnc_client_write_locked(vs);
1615bd023f95SCorentin Chary     }
1616bd023f95SCorentin Chary     vnc_unlock_output(vs);
16173e230dd2SCorentin Chary }
16183e230dd2SCorentin Chary 
161971a8cdecSBlue Swirl static uint8_t read_u8(uint8_t *data, size_t offset)
16203e230dd2SCorentin Chary {
16213e230dd2SCorentin Chary     return data[offset];
16223e230dd2SCorentin Chary }
16233e230dd2SCorentin Chary 
162471a8cdecSBlue Swirl static uint16_t read_u16(uint8_t *data, size_t offset)
16253e230dd2SCorentin Chary {
16263e230dd2SCorentin Chary     return ((data[offset] & 0xFF) << 8) | (data[offset + 1] & 0xFF);
16273e230dd2SCorentin Chary }
16283e230dd2SCorentin Chary 
162971a8cdecSBlue Swirl static int32_t read_s32(uint8_t *data, size_t offset)
16303e230dd2SCorentin Chary {
16313e230dd2SCorentin Chary     return (int32_t)((data[offset] << 24) | (data[offset + 1] << 16) |
16323e230dd2SCorentin Chary                      (data[offset + 2] << 8) | data[offset + 3]);
16333e230dd2SCorentin Chary }
16343e230dd2SCorentin Chary 
16353e230dd2SCorentin Chary uint32_t read_u32(uint8_t *data, size_t offset)
16363e230dd2SCorentin Chary {
16373e230dd2SCorentin Chary     return ((data[offset] << 24) | (data[offset + 1] << 16) |
16383e230dd2SCorentin Chary             (data[offset + 2] << 8) | data[offset + 3]);
16393e230dd2SCorentin Chary }
16403e230dd2SCorentin Chary 
16413e230dd2SCorentin Chary static void client_cut_text(VncState *vs, size_t len, uint8_t *text)
16423e230dd2SCorentin Chary {
16433e230dd2SCorentin Chary }
16443e230dd2SCorentin Chary 
16459e8dd451SJan Kiszka static void check_pointer_type_change(Notifier *notifier, void *data)
16463e230dd2SCorentin Chary {
16473e230dd2SCorentin Chary     VncState *vs = container_of(notifier, VncState, mouse_mode_notifier);
164814768ebaSGerd Hoffmann     int absolute = qemu_input_is_absolute();
16493e230dd2SCorentin Chary 
16503e230dd2SCorentin Chary     if (vnc_has_feature(vs, VNC_FEATURE_POINTER_TYPE_CHANGE) && vs->absolute != absolute) {
1651bd023f95SCorentin Chary         vnc_lock_output(vs);
16523e230dd2SCorentin Chary         vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE);
16533e230dd2SCorentin Chary         vnc_write_u8(vs, 0);
16543e230dd2SCorentin Chary         vnc_write_u16(vs, 1);
16553e230dd2SCorentin Chary         vnc_framebuffer_update(vs, absolute, 0,
1656bea60dd7SPeter Lieven                                pixman_image_get_width(vs->vd->server),
1657bea60dd7SPeter Lieven                                pixman_image_get_height(vs->vd->server),
16583e230dd2SCorentin Chary                                VNC_ENCODING_POINTER_TYPE_CHANGE);
1659bd023f95SCorentin Chary         vnc_unlock_output(vs);
16603e230dd2SCorentin Chary         vnc_flush(vs);
16613e230dd2SCorentin Chary     }
16623e230dd2SCorentin Chary     vs->absolute = absolute;
16633e230dd2SCorentin Chary }
16643e230dd2SCorentin Chary 
16653e230dd2SCorentin Chary static void pointer_event(VncState *vs, int button_mask, int x, int y)
16663e230dd2SCorentin Chary {
166714768ebaSGerd Hoffmann     static uint32_t bmap[INPUT_BUTTON_MAX] = {
166814768ebaSGerd Hoffmann         [INPUT_BUTTON_LEFT]       = 0x01,
166914768ebaSGerd Hoffmann         [INPUT_BUTTON_MIDDLE]     = 0x02,
167014768ebaSGerd Hoffmann         [INPUT_BUTTON_RIGHT]      = 0x04,
167114768ebaSGerd Hoffmann         [INPUT_BUTTON_WHEEL_UP]   = 0x08,
167214768ebaSGerd Hoffmann         [INPUT_BUTTON_WHEEL_DOWN] = 0x10,
167314768ebaSGerd Hoffmann     };
167414768ebaSGerd Hoffmann     QemuConsole *con = vs->vd->dcl.con;
1675bea60dd7SPeter Lieven     int width = pixman_image_get_width(vs->vd->server);
1676bea60dd7SPeter Lieven     int height = pixman_image_get_height(vs->vd->server);
16773e230dd2SCorentin Chary 
167814768ebaSGerd Hoffmann     if (vs->last_bmask != button_mask) {
167914768ebaSGerd Hoffmann         qemu_input_update_buttons(con, bmap, vs->last_bmask, button_mask);
168014768ebaSGerd Hoffmann         vs->last_bmask = button_mask;
168114768ebaSGerd Hoffmann     }
16823e230dd2SCorentin Chary 
16833e230dd2SCorentin Chary     if (vs->absolute) {
168414768ebaSGerd Hoffmann         qemu_input_queue_abs(con, INPUT_AXIS_X, x, width);
168514768ebaSGerd Hoffmann         qemu_input_queue_abs(con, INPUT_AXIS_Y, y, height);
16863e230dd2SCorentin Chary     } else if (vnc_has_feature(vs, VNC_FEATURE_POINTER_TYPE_CHANGE)) {
168714768ebaSGerd Hoffmann         qemu_input_queue_rel(con, INPUT_AXIS_X, x - 0x7FFF);
168814768ebaSGerd Hoffmann         qemu_input_queue_rel(con, INPUT_AXIS_Y, y - 0x7FFF);
16893e230dd2SCorentin Chary     } else {
169014768ebaSGerd Hoffmann         if (vs->last_x != -1) {
169114768ebaSGerd Hoffmann             qemu_input_queue_rel(con, INPUT_AXIS_X, x - vs->last_x);
169214768ebaSGerd Hoffmann             qemu_input_queue_rel(con, INPUT_AXIS_Y, y - vs->last_y);
169314768ebaSGerd Hoffmann         }
16943e230dd2SCorentin Chary         vs->last_x = x;
16953e230dd2SCorentin Chary         vs->last_y = y;
16963e230dd2SCorentin Chary     }
169714768ebaSGerd Hoffmann     qemu_input_event_sync();
16983e230dd2SCorentin Chary }
16993e230dd2SCorentin Chary 
17003e230dd2SCorentin Chary static void reset_keys(VncState *vs)
17013e230dd2SCorentin Chary {
17023e230dd2SCorentin Chary     int i;
17033e230dd2SCorentin Chary     for(i = 0; i < 256; i++) {
17043e230dd2SCorentin Chary         if (vs->modifiers_state[i]) {
17058d447d10SGerd Hoffmann             qemu_input_event_send_key_number(vs->vd->dcl.con, i, false);
17063e230dd2SCorentin Chary             vs->modifiers_state[i] = 0;
17073e230dd2SCorentin Chary         }
17083e230dd2SCorentin Chary     }
17093e230dd2SCorentin Chary }
17103e230dd2SCorentin Chary 
17113e230dd2SCorentin Chary static void press_key(VncState *vs, int keysym)
17123e230dd2SCorentin Chary {
17133e230dd2SCorentin Chary     int keycode = keysym2scancode(vs->vd->kbd_layout, keysym) & SCANCODE_KEYMASK;
17148d447d10SGerd Hoffmann     qemu_input_event_send_key_number(vs->vd->dcl.con, keycode, true);
17152deb4accSGerd Hoffmann     qemu_input_event_send_key_delay(0);
17168d447d10SGerd Hoffmann     qemu_input_event_send_key_number(vs->vd->dcl.con, keycode, false);
17172deb4accSGerd Hoffmann     qemu_input_event_send_key_delay(0);
17183e230dd2SCorentin Chary }
17193e230dd2SCorentin Chary 
1720ab99e5c1SLei Li static int current_led_state(VncState *vs)
1721ab99e5c1SLei Li {
1722ab99e5c1SLei Li     int ledstate = 0;
1723ab99e5c1SLei Li 
1724ab99e5c1SLei Li     if (vs->modifiers_state[0x46]) {
1725ab99e5c1SLei Li         ledstate |= QEMU_SCROLL_LOCK_LED;
1726ab99e5c1SLei Li     }
1727ab99e5c1SLei Li     if (vs->modifiers_state[0x45]) {
1728ab99e5c1SLei Li         ledstate |= QEMU_NUM_LOCK_LED;
1729ab99e5c1SLei Li     }
1730ab99e5c1SLei Li     if (vs->modifiers_state[0x3a]) {
1731ab99e5c1SLei Li         ledstate |= QEMU_CAPS_LOCK_LED;
1732ab99e5c1SLei Li     }
1733ab99e5c1SLei Li 
1734ab99e5c1SLei Li     return ledstate;
1735ab99e5c1SLei Li }
1736ab99e5c1SLei Li 
1737ab99e5c1SLei Li static void vnc_led_state_change(VncState *vs)
1738ab99e5c1SLei Li {
1739ab99e5c1SLei Li     int ledstate = 0;
1740ab99e5c1SLei Li 
1741ab99e5c1SLei Li     if (!vnc_has_feature(vs, VNC_FEATURE_LED_STATE)) {
1742ab99e5c1SLei Li         return;
1743ab99e5c1SLei Li     }
1744ab99e5c1SLei Li 
1745ab99e5c1SLei Li     ledstate = current_led_state(vs);
1746ab99e5c1SLei Li     vnc_lock_output(vs);
1747ab99e5c1SLei Li     vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE);
1748ab99e5c1SLei Li     vnc_write_u8(vs, 0);
1749ab99e5c1SLei Li     vnc_write_u16(vs, 1);
1750ab99e5c1SLei Li     vnc_framebuffer_update(vs, 0, 0, 1, 1, VNC_ENCODING_LED_STATE);
1751ab99e5c1SLei Li     vnc_write_u8(vs, ledstate);
1752ab99e5c1SLei Li     vnc_unlock_output(vs);
1753ab99e5c1SLei Li     vnc_flush(vs);
1754ab99e5c1SLei Li }
1755ab99e5c1SLei Li 
17563e230dd2SCorentin Chary static void kbd_leds(void *opaque, int ledstate)
17573e230dd2SCorentin Chary {
17583e230dd2SCorentin Chary     VncState *vs = opaque;
175996f3d174SLei Li     int caps, num, scr;
17601483adcfSLei Li     bool has_changed = (ledstate != current_led_state(vs));
17613e230dd2SCorentin Chary 
176240066175SGerd Hoffmann     trace_vnc_key_guest_leds((ledstate & QEMU_CAPS_LOCK_LED),
176340066175SGerd Hoffmann                              (ledstate & QEMU_NUM_LOCK_LED),
176440066175SGerd Hoffmann                              (ledstate & QEMU_SCROLL_LOCK_LED));
176540066175SGerd Hoffmann 
17663e230dd2SCorentin Chary     caps = ledstate & QEMU_CAPS_LOCK_LED ? 1 : 0;
17673e230dd2SCorentin Chary     num  = ledstate & QEMU_NUM_LOCK_LED  ? 1 : 0;
176896f3d174SLei Li     scr  = ledstate & QEMU_SCROLL_LOCK_LED ? 1 : 0;
17693e230dd2SCorentin Chary 
17703e230dd2SCorentin Chary     if (vs->modifiers_state[0x3a] != caps) {
17713e230dd2SCorentin Chary         vs->modifiers_state[0x3a] = caps;
17723e230dd2SCorentin Chary     }
17733e230dd2SCorentin Chary     if (vs->modifiers_state[0x45] != num) {
17743e230dd2SCorentin Chary         vs->modifiers_state[0x45] = num;
17753e230dd2SCorentin Chary     }
177696f3d174SLei Li     if (vs->modifiers_state[0x46] != scr) {
177796f3d174SLei Li         vs->modifiers_state[0x46] = scr;
177896f3d174SLei Li     }
1779ab99e5c1SLei Li 
1780ab99e5c1SLei Li     /* Sending the current led state message to the client */
17811483adcfSLei Li     if (has_changed) {
1782ab99e5c1SLei Li         vnc_led_state_change(vs);
1783ab99e5c1SLei Li     }
17843e230dd2SCorentin Chary }
17853e230dd2SCorentin Chary 
17863e230dd2SCorentin Chary static void do_key_event(VncState *vs, int down, int keycode, int sym)
17873e230dd2SCorentin Chary {
17883e230dd2SCorentin Chary     /* QEMU console switch */
17893e230dd2SCorentin Chary     switch(keycode) {
17903e230dd2SCorentin Chary     case 0x2a:                          /* Left Shift */
17913e230dd2SCorentin Chary     case 0x36:                          /* Right Shift */
17923e230dd2SCorentin Chary     case 0x1d:                          /* Left CTRL */
17933e230dd2SCorentin Chary     case 0x9d:                          /* Right CTRL */
17943e230dd2SCorentin Chary     case 0x38:                          /* Left ALT */
17953e230dd2SCorentin Chary     case 0xb8:                          /* Right ALT */
17963e230dd2SCorentin Chary         if (down)
17973e230dd2SCorentin Chary             vs->modifiers_state[keycode] = 1;
17983e230dd2SCorentin Chary         else
17993e230dd2SCorentin Chary             vs->modifiers_state[keycode] = 0;
18003e230dd2SCorentin Chary         break;
18013e230dd2SCorentin Chary     case 0x02 ... 0x0a: /* '1' to '9' keys */
18021d0d59feSGerd Hoffmann         if (vs->vd->dcl.con == NULL &&
18031d0d59feSGerd Hoffmann             down && vs->modifiers_state[0x1d] && vs->modifiers_state[0x38]) {
18043e230dd2SCorentin Chary             /* Reset the modifiers sent to the current console */
18053e230dd2SCorentin Chary             reset_keys(vs);
18063e230dd2SCorentin Chary             console_select(keycode - 0x02);
18073e230dd2SCorentin Chary             return;
18083e230dd2SCorentin Chary         }
18093e230dd2SCorentin Chary         break;
18103e230dd2SCorentin Chary     case 0x3a:                        /* CapsLock */
18113e230dd2SCorentin Chary     case 0x45:                        /* NumLock */
18123e230dd2SCorentin Chary         if (down)
18133e230dd2SCorentin Chary             vs->modifiers_state[keycode] ^= 1;
18143e230dd2SCorentin Chary         break;
18153e230dd2SCorentin Chary     }
18163e230dd2SCorentin Chary 
1817e7b2aaccSLei Li     /* Turn off the lock state sync logic if the client support the led
1818e7b2aaccSLei Li        state extension.
1819e7b2aaccSLei Li     */
18209892088bSGerd Hoffmann     if (down && vs->vd->lock_key_sync &&
1821e7b2aaccSLei Li         !vnc_has_feature(vs, VNC_FEATURE_LED_STATE) &&
18223e230dd2SCorentin Chary         keycode_is_keypad(vs->vd->kbd_layout, keycode)) {
18233e230dd2SCorentin Chary         /* If the numlock state needs to change then simulate an additional
18243e230dd2SCorentin Chary            keypress before sending this one.  This will happen if the user
18253e230dd2SCorentin Chary            toggles numlock away from the VNC window.
18263e230dd2SCorentin Chary         */
18273e230dd2SCorentin Chary         if (keysym_is_numlock(vs->vd->kbd_layout, sym & 0xFFFF)) {
18283e230dd2SCorentin Chary             if (!vs->modifiers_state[0x45]) {
182940066175SGerd Hoffmann                 trace_vnc_key_sync_numlock(true);
18303e230dd2SCorentin Chary                 vs->modifiers_state[0x45] = 1;
18313e230dd2SCorentin Chary                 press_key(vs, 0xff7f);
18323e230dd2SCorentin Chary             }
18333e230dd2SCorentin Chary         } else {
18343e230dd2SCorentin Chary             if (vs->modifiers_state[0x45]) {
183540066175SGerd Hoffmann                 trace_vnc_key_sync_numlock(false);
18363e230dd2SCorentin Chary                 vs->modifiers_state[0x45] = 0;
18373e230dd2SCorentin Chary                 press_key(vs, 0xff7f);
18383e230dd2SCorentin Chary             }
18393e230dd2SCorentin Chary         }
18403e230dd2SCorentin Chary     }
18413e230dd2SCorentin Chary 
18429892088bSGerd Hoffmann     if (down && vs->vd->lock_key_sync &&
1843e7b2aaccSLei Li         !vnc_has_feature(vs, VNC_FEATURE_LED_STATE) &&
18443e230dd2SCorentin Chary         ((sym >= 'A' && sym <= 'Z') || (sym >= 'a' && sym <= 'z'))) {
18453e230dd2SCorentin Chary         /* If the capslock state needs to change then simulate an additional
18463e230dd2SCorentin Chary            keypress before sending this one.  This will happen if the user
18473e230dd2SCorentin Chary            toggles capslock away from the VNC window.
18483e230dd2SCorentin Chary         */
18493e230dd2SCorentin Chary         int uppercase = !!(sym >= 'A' && sym <= 'Z');
18503e230dd2SCorentin Chary         int shift = !!(vs->modifiers_state[0x2a] | vs->modifiers_state[0x36]);
18513e230dd2SCorentin Chary         int capslock = !!(vs->modifiers_state[0x3a]);
18523e230dd2SCorentin Chary         if (capslock) {
18533e230dd2SCorentin Chary             if (uppercase == shift) {
185440066175SGerd Hoffmann                 trace_vnc_key_sync_capslock(false);
18553e230dd2SCorentin Chary                 vs->modifiers_state[0x3a] = 0;
18563e230dd2SCorentin Chary                 press_key(vs, 0xffe5);
18573e230dd2SCorentin Chary             }
18583e230dd2SCorentin Chary         } else {
18593e230dd2SCorentin Chary             if (uppercase != shift) {
186040066175SGerd Hoffmann                 trace_vnc_key_sync_capslock(true);
18613e230dd2SCorentin Chary                 vs->modifiers_state[0x3a] = 1;
18623e230dd2SCorentin Chary                 press_key(vs, 0xffe5);
18633e230dd2SCorentin Chary             }
18643e230dd2SCorentin Chary         }
18653e230dd2SCorentin Chary     }
18663e230dd2SCorentin Chary 
186781c0d5a6SGerd Hoffmann     if (qemu_console_is_graphic(NULL)) {
18688d447d10SGerd Hoffmann         qemu_input_event_send_key_number(vs->vd->dcl.con, keycode, down);
18693e230dd2SCorentin Chary     } else {
1870e26437c2SGerd Hoffmann         bool numlock = vs->modifiers_state[0x45];
1871e26437c2SGerd Hoffmann         bool control = (vs->modifiers_state[0x1d] ||
1872e26437c2SGerd Hoffmann                         vs->modifiers_state[0x9d]);
18733e230dd2SCorentin Chary         /* QEMU console emulation */
18743e230dd2SCorentin Chary         if (down) {
18753e230dd2SCorentin Chary             switch (keycode) {
18763e230dd2SCorentin Chary             case 0x2a:                          /* Left Shift */
18773e230dd2SCorentin Chary             case 0x36:                          /* Right Shift */
18783e230dd2SCorentin Chary             case 0x1d:                          /* Left CTRL */
18793e230dd2SCorentin Chary             case 0x9d:                          /* Right CTRL */
18803e230dd2SCorentin Chary             case 0x38:                          /* Left ALT */
18813e230dd2SCorentin Chary             case 0xb8:                          /* Right ALT */
18823e230dd2SCorentin Chary                 break;
18833e230dd2SCorentin Chary             case 0xc8:
18843e230dd2SCorentin Chary                 kbd_put_keysym(QEMU_KEY_UP);
18853e230dd2SCorentin Chary                 break;
18863e230dd2SCorentin Chary             case 0xd0:
18873e230dd2SCorentin Chary                 kbd_put_keysym(QEMU_KEY_DOWN);
18883e230dd2SCorentin Chary                 break;
18893e230dd2SCorentin Chary             case 0xcb:
18903e230dd2SCorentin Chary                 kbd_put_keysym(QEMU_KEY_LEFT);
18913e230dd2SCorentin Chary                 break;
18923e230dd2SCorentin Chary             case 0xcd:
18933e230dd2SCorentin Chary                 kbd_put_keysym(QEMU_KEY_RIGHT);
18943e230dd2SCorentin Chary                 break;
18953e230dd2SCorentin Chary             case 0xd3:
18963e230dd2SCorentin Chary                 kbd_put_keysym(QEMU_KEY_DELETE);
18973e230dd2SCorentin Chary                 break;
18983e230dd2SCorentin Chary             case 0xc7:
18993e230dd2SCorentin Chary                 kbd_put_keysym(QEMU_KEY_HOME);
19003e230dd2SCorentin Chary                 break;
19013e230dd2SCorentin Chary             case 0xcf:
19023e230dd2SCorentin Chary                 kbd_put_keysym(QEMU_KEY_END);
19033e230dd2SCorentin Chary                 break;
19043e230dd2SCorentin Chary             case 0xc9:
19053e230dd2SCorentin Chary                 kbd_put_keysym(QEMU_KEY_PAGEUP);
19063e230dd2SCorentin Chary                 break;
19073e230dd2SCorentin Chary             case 0xd1:
19083e230dd2SCorentin Chary                 kbd_put_keysym(QEMU_KEY_PAGEDOWN);
19093e230dd2SCorentin Chary                 break;
19103e230dd2SCorentin Chary 
19113e230dd2SCorentin Chary             case 0x47:
19123e230dd2SCorentin Chary                 kbd_put_keysym(numlock ? '7' : QEMU_KEY_HOME);
19133e230dd2SCorentin Chary                 break;
19143e230dd2SCorentin Chary             case 0x48:
19153e230dd2SCorentin Chary                 kbd_put_keysym(numlock ? '8' : QEMU_KEY_UP);
19163e230dd2SCorentin Chary                 break;
19173e230dd2SCorentin Chary             case 0x49:
19183e230dd2SCorentin Chary                 kbd_put_keysym(numlock ? '9' : QEMU_KEY_PAGEUP);
19193e230dd2SCorentin Chary                 break;
19203e230dd2SCorentin Chary             case 0x4b:
19213e230dd2SCorentin Chary                 kbd_put_keysym(numlock ? '4' : QEMU_KEY_LEFT);
19223e230dd2SCorentin Chary                 break;
19233e230dd2SCorentin Chary             case 0x4c:
19243e230dd2SCorentin Chary                 kbd_put_keysym('5');
19253e230dd2SCorentin Chary                 break;
19263e230dd2SCorentin Chary             case 0x4d:
19273e230dd2SCorentin Chary                 kbd_put_keysym(numlock ? '6' : QEMU_KEY_RIGHT);
19283e230dd2SCorentin Chary                 break;
19293e230dd2SCorentin Chary             case 0x4f:
19303e230dd2SCorentin Chary                 kbd_put_keysym(numlock ? '1' : QEMU_KEY_END);
19313e230dd2SCorentin Chary                 break;
19323e230dd2SCorentin Chary             case 0x50:
19333e230dd2SCorentin Chary                 kbd_put_keysym(numlock ? '2' : QEMU_KEY_DOWN);
19343e230dd2SCorentin Chary                 break;
19353e230dd2SCorentin Chary             case 0x51:
19363e230dd2SCorentin Chary                 kbd_put_keysym(numlock ? '3' : QEMU_KEY_PAGEDOWN);
19373e230dd2SCorentin Chary                 break;
19383e230dd2SCorentin Chary             case 0x52:
19393e230dd2SCorentin Chary                 kbd_put_keysym('0');
19403e230dd2SCorentin Chary                 break;
19413e230dd2SCorentin Chary             case 0x53:
19423e230dd2SCorentin Chary                 kbd_put_keysym(numlock ? '.' : QEMU_KEY_DELETE);
19433e230dd2SCorentin Chary                 break;
19443e230dd2SCorentin Chary 
19453e230dd2SCorentin Chary             case 0xb5:
19463e230dd2SCorentin Chary                 kbd_put_keysym('/');
19473e230dd2SCorentin Chary                 break;
19483e230dd2SCorentin Chary             case 0x37:
19493e230dd2SCorentin Chary                 kbd_put_keysym('*');
19503e230dd2SCorentin Chary                 break;
19513e230dd2SCorentin Chary             case 0x4a:
19523e230dd2SCorentin Chary                 kbd_put_keysym('-');
19533e230dd2SCorentin Chary                 break;
19543e230dd2SCorentin Chary             case 0x4e:
19553e230dd2SCorentin Chary                 kbd_put_keysym('+');
19563e230dd2SCorentin Chary                 break;
19573e230dd2SCorentin Chary             case 0x9c:
19583e230dd2SCorentin Chary                 kbd_put_keysym('\n');
19593e230dd2SCorentin Chary                 break;
19603e230dd2SCorentin Chary 
19613e230dd2SCorentin Chary             default:
1962e26437c2SGerd Hoffmann                 if (control) {
1963e26437c2SGerd Hoffmann                     kbd_put_keysym(sym & 0x1f);
1964e26437c2SGerd Hoffmann                 } else {
19653e230dd2SCorentin Chary                     kbd_put_keysym(sym);
1966e26437c2SGerd Hoffmann                 }
19673e230dd2SCorentin Chary                 break;
19683e230dd2SCorentin Chary             }
19693e230dd2SCorentin Chary         }
19703e230dd2SCorentin Chary     }
19713e230dd2SCorentin Chary }
19723e230dd2SCorentin Chary 
19737bc9318bSGerd Hoffmann static void vnc_release_modifiers(VncState *vs)
19747bc9318bSGerd Hoffmann {
19757bc9318bSGerd Hoffmann     static const int keycodes[] = {
19767bc9318bSGerd Hoffmann         /* shift, control, alt keys, both left & right */
19777bc9318bSGerd Hoffmann         0x2a, 0x36, 0x1d, 0x9d, 0x38, 0xb8,
19787bc9318bSGerd Hoffmann     };
19797bc9318bSGerd Hoffmann     int i, keycode;
19807bc9318bSGerd Hoffmann 
198181c0d5a6SGerd Hoffmann     if (!qemu_console_is_graphic(NULL)) {
19827bc9318bSGerd Hoffmann         return;
19837bc9318bSGerd Hoffmann     }
19847bc9318bSGerd Hoffmann     for (i = 0; i < ARRAY_SIZE(keycodes); i++) {
19857bc9318bSGerd Hoffmann         keycode = keycodes[i];
19867bc9318bSGerd Hoffmann         if (!vs->modifiers_state[keycode]) {
19877bc9318bSGerd Hoffmann             continue;
19887bc9318bSGerd Hoffmann         }
19898d447d10SGerd Hoffmann         qemu_input_event_send_key_number(vs->vd->dcl.con, keycode, false);
19907bc9318bSGerd Hoffmann     }
19917bc9318bSGerd Hoffmann }
19927bc9318bSGerd Hoffmann 
199340066175SGerd Hoffmann static const char *code2name(int keycode)
199440066175SGerd Hoffmann {
199540066175SGerd Hoffmann     return QKeyCode_lookup[qemu_input_key_number_to_qcode(keycode)];
199640066175SGerd Hoffmann }
199740066175SGerd Hoffmann 
19983e230dd2SCorentin Chary static void key_event(VncState *vs, int down, uint32_t sym)
19993e230dd2SCorentin Chary {
20003e230dd2SCorentin Chary     int keycode;
20013e230dd2SCorentin Chary     int lsym = sym;
20023e230dd2SCorentin Chary 
200381c0d5a6SGerd Hoffmann     if (lsym >= 'A' && lsym <= 'Z' && qemu_console_is_graphic(NULL)) {
20043e230dd2SCorentin Chary         lsym = lsym - 'A' + 'a';
20053e230dd2SCorentin Chary     }
20063e230dd2SCorentin Chary 
20073e230dd2SCorentin Chary     keycode = keysym2scancode(vs->vd->kbd_layout, lsym & 0xFFFF) & SCANCODE_KEYMASK;
200840066175SGerd Hoffmann     trace_vnc_key_event_map(down, sym, keycode, code2name(keycode));
20093e230dd2SCorentin Chary     do_key_event(vs, down, keycode, sym);
20103e230dd2SCorentin Chary }
20113e230dd2SCorentin Chary 
20123e230dd2SCorentin Chary static void ext_key_event(VncState *vs, int down,
20133e230dd2SCorentin Chary                           uint32_t sym, uint16_t keycode)
20143e230dd2SCorentin Chary {
20153e230dd2SCorentin Chary     /* if the user specifies a keyboard layout, always use it */
201640066175SGerd Hoffmann     if (keyboard_layout) {
20173e230dd2SCorentin Chary         key_event(vs, down, sym);
201840066175SGerd Hoffmann     } else {
201940066175SGerd Hoffmann         trace_vnc_key_event_ext(down, sym, keycode, code2name(keycode));
20203e230dd2SCorentin Chary         do_key_event(vs, down, keycode, sym);
20213e230dd2SCorentin Chary     }
202240066175SGerd Hoffmann }
20233e230dd2SCorentin Chary 
20243e230dd2SCorentin Chary static void framebuffer_update_request(VncState *vs, int incremental,
2025bea60dd7SPeter Lieven                                        int x, int y, int w, int h)
20263e230dd2SCorentin Chary {
2027bea60dd7SPeter Lieven     int width = pixman_image_get_width(vs->vd->server);
2028bea60dd7SPeter Lieven     int height = pixman_image_get_height(vs->vd->server);
20293e230dd2SCorentin Chary 
20303e230dd2SCorentin Chary     vs->need_update = 1;
2031bea60dd7SPeter Lieven 
2032bea60dd7SPeter Lieven     if (incremental) {
2033bea60dd7SPeter Lieven         return;
20343e230dd2SCorentin Chary     }
2035bea60dd7SPeter Lieven 
203607535a89SStephan Kulow     vs->force_update = 1;
2037bea60dd7SPeter Lieven     vnc_set_area_dirty(vs->dirty, width, height, x, y, w, h);
20383e230dd2SCorentin Chary }
20393e230dd2SCorentin Chary 
20403e230dd2SCorentin Chary static void send_ext_key_event_ack(VncState *vs)
20413e230dd2SCorentin Chary {
2042bd023f95SCorentin Chary     vnc_lock_output(vs);
20433e230dd2SCorentin Chary     vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE);
20443e230dd2SCorentin Chary     vnc_write_u8(vs, 0);
20453e230dd2SCorentin Chary     vnc_write_u16(vs, 1);
2046d39fa6d8SGerd Hoffmann     vnc_framebuffer_update(vs, 0, 0,
2047bea60dd7SPeter Lieven                            pixman_image_get_width(vs->vd->server),
2048bea60dd7SPeter Lieven                            pixman_image_get_height(vs->vd->server),
20493e230dd2SCorentin Chary                            VNC_ENCODING_EXT_KEY_EVENT);
2050bd023f95SCorentin Chary     vnc_unlock_output(vs);
20513e230dd2SCorentin Chary     vnc_flush(vs);
20523e230dd2SCorentin Chary }
20533e230dd2SCorentin Chary 
20543e230dd2SCorentin Chary static void send_ext_audio_ack(VncState *vs)
20553e230dd2SCorentin Chary {
2056bd023f95SCorentin Chary     vnc_lock_output(vs);
20573e230dd2SCorentin Chary     vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE);
20583e230dd2SCorentin Chary     vnc_write_u8(vs, 0);
20593e230dd2SCorentin Chary     vnc_write_u16(vs, 1);
2060d39fa6d8SGerd Hoffmann     vnc_framebuffer_update(vs, 0, 0,
2061bea60dd7SPeter Lieven                            pixman_image_get_width(vs->vd->server),
2062bea60dd7SPeter Lieven                            pixman_image_get_height(vs->vd->server),
20633e230dd2SCorentin Chary                            VNC_ENCODING_AUDIO);
2064bd023f95SCorentin Chary     vnc_unlock_output(vs);
20653e230dd2SCorentin Chary     vnc_flush(vs);
20663e230dd2SCorentin Chary }
20673e230dd2SCorentin Chary 
20683e230dd2SCorentin Chary static void set_encodings(VncState *vs, int32_t *encodings, size_t n_encodings)
20693e230dd2SCorentin Chary {
20703e230dd2SCorentin Chary     int i;
20713e230dd2SCorentin Chary     unsigned int enc = 0;
20723e230dd2SCorentin Chary 
20733e230dd2SCorentin Chary     vs->features = 0;
20743e230dd2SCorentin Chary     vs->vnc_encoding = 0;
2075d1af0e05SCorentin Chary     vs->tight.compression = 9;
2076d1af0e05SCorentin Chary     vs->tight.quality = -1; /* Lossless by default */
20773e230dd2SCorentin Chary     vs->absolute = -1;
20783e230dd2SCorentin Chary 
20793e230dd2SCorentin Chary     /*
20803e230dd2SCorentin Chary      * Start from the end because the encodings are sent in order of preference.
2081e5bed759SDong Xu Wang      * This way the preferred encoding (first encoding defined in the array)
20823e230dd2SCorentin Chary      * will be set at the end of the loop.
20833e230dd2SCorentin Chary      */
20843e230dd2SCorentin Chary     for (i = n_encodings - 1; i >= 0; i--) {
20853e230dd2SCorentin Chary         enc = encodings[i];
20863e230dd2SCorentin Chary         switch (enc) {
20873e230dd2SCorentin Chary         case VNC_ENCODING_RAW:
20883e230dd2SCorentin Chary             vs->vnc_encoding = enc;
20893e230dd2SCorentin Chary             break;
20903e230dd2SCorentin Chary         case VNC_ENCODING_COPYRECT:
20913e230dd2SCorentin Chary             vs->features |= VNC_FEATURE_COPYRECT_MASK;
20923e230dd2SCorentin Chary             break;
20933e230dd2SCorentin Chary         case VNC_ENCODING_HEXTILE:
20943e230dd2SCorentin Chary             vs->features |= VNC_FEATURE_HEXTILE_MASK;
20953e230dd2SCorentin Chary             vs->vnc_encoding = enc;
20963e230dd2SCorentin Chary             break;
20973e230dd2SCorentin Chary         case VNC_ENCODING_TIGHT:
20983e230dd2SCorentin Chary             vs->features |= VNC_FEATURE_TIGHT_MASK;
20993e230dd2SCorentin Chary             vs->vnc_encoding = enc;
21003e230dd2SCorentin Chary             break;
2101fe3e7f2dSJoel Martin #ifdef CONFIG_VNC_PNG
2102efe556adSCorentin Chary         case VNC_ENCODING_TIGHT_PNG:
2103efe556adSCorentin Chary             vs->features |= VNC_FEATURE_TIGHT_PNG_MASK;
2104efe556adSCorentin Chary             vs->vnc_encoding = enc;
2105efe556adSCorentin Chary             break;
2106fe3e7f2dSJoel Martin #endif
21073e230dd2SCorentin Chary         case VNC_ENCODING_ZLIB:
21083e230dd2SCorentin Chary             vs->features |= VNC_FEATURE_ZLIB_MASK;
21093e230dd2SCorentin Chary             vs->vnc_encoding = enc;
21103e230dd2SCorentin Chary             break;
2111148954faSCorentin Chary         case VNC_ENCODING_ZRLE:
2112148954faSCorentin Chary             vs->features |= VNC_FEATURE_ZRLE_MASK;
2113148954faSCorentin Chary             vs->vnc_encoding = enc;
2114148954faSCorentin Chary             break;
2115148954faSCorentin Chary         case VNC_ENCODING_ZYWRLE:
2116148954faSCorentin Chary             vs->features |= VNC_FEATURE_ZYWRLE_MASK;
2117148954faSCorentin Chary             vs->vnc_encoding = enc;
2118148954faSCorentin Chary             break;
21193e230dd2SCorentin Chary         case VNC_ENCODING_DESKTOPRESIZE:
21203e230dd2SCorentin Chary             vs->features |= VNC_FEATURE_RESIZE_MASK;
21213e230dd2SCorentin Chary             break;
21223e230dd2SCorentin Chary         case VNC_ENCODING_POINTER_TYPE_CHANGE:
21233e230dd2SCorentin Chary             vs->features |= VNC_FEATURE_POINTER_TYPE_CHANGE_MASK;
21243e230dd2SCorentin Chary             break;
21253e230dd2SCorentin Chary         case VNC_ENCODING_RICH_CURSOR:
21263e230dd2SCorentin Chary             vs->features |= VNC_FEATURE_RICH_CURSOR_MASK;
21273e230dd2SCorentin Chary             break;
21283e230dd2SCorentin Chary         case VNC_ENCODING_EXT_KEY_EVENT:
21293e230dd2SCorentin Chary             send_ext_key_event_ack(vs);
21303e230dd2SCorentin Chary             break;
21313e230dd2SCorentin Chary         case VNC_ENCODING_AUDIO:
21323e230dd2SCorentin Chary             send_ext_audio_ack(vs);
21333e230dd2SCorentin Chary             break;
21343e230dd2SCorentin Chary         case VNC_ENCODING_WMVi:
21353e230dd2SCorentin Chary             vs->features |= VNC_FEATURE_WMVI_MASK;
21363e230dd2SCorentin Chary             break;
2137ab99e5c1SLei Li         case VNC_ENCODING_LED_STATE:
2138ab99e5c1SLei Li             vs->features |= VNC_FEATURE_LED_STATE_MASK;
2139ab99e5c1SLei Li             break;
21403e230dd2SCorentin Chary         case VNC_ENCODING_COMPRESSLEVEL0 ... VNC_ENCODING_COMPRESSLEVEL0 + 9:
2141d1af0e05SCorentin Chary             vs->tight.compression = (enc & 0x0F);
21423e230dd2SCorentin Chary             break;
21433e230dd2SCorentin Chary         case VNC_ENCODING_QUALITYLEVEL0 ... VNC_ENCODING_QUALITYLEVEL0 + 9:
2144b31f519eSCorentin Chary             if (vs->vd->lossy) {
2145d1af0e05SCorentin Chary                 vs->tight.quality = (enc & 0x0F);
2146b31f519eSCorentin Chary             }
21473e230dd2SCorentin Chary             break;
21483e230dd2SCorentin Chary         default:
21493e230dd2SCorentin Chary             VNC_DEBUG("Unknown encoding: %d (0x%.8x): %d\n", i, enc, enc);
21503e230dd2SCorentin Chary             break;
21513e230dd2SCorentin Chary         }
21523e230dd2SCorentin Chary     }
21533e230dd2SCorentin Chary     vnc_desktop_resize(vs);
21549e8dd451SJan Kiszka     check_pointer_type_change(&vs->mouse_mode_notifier, NULL);
2155ab99e5c1SLei Li     vnc_led_state_change(vs);
21563e230dd2SCorentin Chary }
21573e230dd2SCorentin Chary 
21583e230dd2SCorentin Chary static void set_pixel_conversion(VncState *vs)
21593e230dd2SCorentin Chary {
21609f64916dSGerd Hoffmann     pixman_format_code_t fmt = qemu_pixman_get_format(&vs->client_pf);
21619f64916dSGerd Hoffmann 
21629f64916dSGerd Hoffmann     if (fmt == VNC_SERVER_FB_FORMAT) {
21633e230dd2SCorentin Chary         vs->write_pixels = vnc_write_pixels_copy;
21643e230dd2SCorentin Chary         vnc_hextile_set_pixel_conversion(vs, 0);
21653e230dd2SCorentin Chary     } else {
21663e230dd2SCorentin Chary         vs->write_pixels = vnc_write_pixels_generic;
21673e230dd2SCorentin Chary         vnc_hextile_set_pixel_conversion(vs, 1);
21683e230dd2SCorentin Chary     }
21693e230dd2SCorentin Chary }
21703e230dd2SCorentin Chary 
21713e230dd2SCorentin Chary static void set_pixel_format(VncState *vs,
21723e230dd2SCorentin Chary                              int bits_per_pixel, int depth,
21733e230dd2SCorentin Chary                              int big_endian_flag, int true_color_flag,
21743e230dd2SCorentin Chary                              int red_max, int green_max, int blue_max,
21753e230dd2SCorentin Chary                              int red_shift, int green_shift, int blue_shift)
21763e230dd2SCorentin Chary {
21773e230dd2SCorentin Chary     if (!true_color_flag) {
21783e230dd2SCorentin Chary         vnc_client_error(vs);
21793e230dd2SCorentin Chary         return;
21803e230dd2SCorentin Chary     }
21813e230dd2SCorentin Chary 
2182e6908bfeSPetr Matousek     switch (bits_per_pixel) {
2183e6908bfeSPetr Matousek     case 8:
2184e6908bfeSPetr Matousek     case 16:
2185e6908bfeSPetr Matousek     case 32:
2186e6908bfeSPetr Matousek         break;
2187e6908bfeSPetr Matousek     default:
2188e6908bfeSPetr Matousek         vnc_client_error(vs);
2189e6908bfeSPetr Matousek         return;
2190e6908bfeSPetr Matousek     }
2191e6908bfeSPetr Matousek 
21929f64916dSGerd Hoffmann     vs->client_pf.rmax = red_max;
21939f64916dSGerd Hoffmann     vs->client_pf.rbits = hweight_long(red_max);
21949f64916dSGerd Hoffmann     vs->client_pf.rshift = red_shift;
21959f64916dSGerd Hoffmann     vs->client_pf.rmask = red_max << red_shift;
21969f64916dSGerd Hoffmann     vs->client_pf.gmax = green_max;
21979f64916dSGerd Hoffmann     vs->client_pf.gbits = hweight_long(green_max);
21989f64916dSGerd Hoffmann     vs->client_pf.gshift = green_shift;
21999f64916dSGerd Hoffmann     vs->client_pf.gmask = green_max << green_shift;
22009f64916dSGerd Hoffmann     vs->client_pf.bmax = blue_max;
22019f64916dSGerd Hoffmann     vs->client_pf.bbits = hweight_long(blue_max);
22029f64916dSGerd Hoffmann     vs->client_pf.bshift = blue_shift;
22039f64916dSGerd Hoffmann     vs->client_pf.bmask = blue_max << blue_shift;
22049f64916dSGerd Hoffmann     vs->client_pf.bits_per_pixel = bits_per_pixel;
22059f64916dSGerd Hoffmann     vs->client_pf.bytes_per_pixel = bits_per_pixel / 8;
22069f64916dSGerd Hoffmann     vs->client_pf.depth = bits_per_pixel == 32 ? 24 : bits_per_pixel;
22079f64916dSGerd Hoffmann     vs->client_be = big_endian_flag;
22083e230dd2SCorentin Chary 
22093e230dd2SCorentin Chary     set_pixel_conversion(vs);
22103e230dd2SCorentin Chary 
22111d0d59feSGerd Hoffmann     graphic_hw_invalidate(vs->vd->dcl.con);
22121d0d59feSGerd Hoffmann     graphic_hw_update(vs->vd->dcl.con);
22133e230dd2SCorentin Chary }
22143e230dd2SCorentin Chary 
22153e230dd2SCorentin Chary static void pixel_format_message (VncState *vs) {
22163e230dd2SCorentin Chary     char pad[3] = { 0, 0, 0 };
22173e230dd2SCorentin Chary 
22189f64916dSGerd Hoffmann     vs->client_pf = qemu_default_pixelformat(32);
22199f64916dSGerd Hoffmann 
22209f64916dSGerd Hoffmann     vnc_write_u8(vs, vs->client_pf.bits_per_pixel); /* bits-per-pixel */
22219f64916dSGerd Hoffmann     vnc_write_u8(vs, vs->client_pf.depth); /* depth */
22223e230dd2SCorentin Chary 
22233e230dd2SCorentin Chary #ifdef HOST_WORDS_BIGENDIAN
22243e230dd2SCorentin Chary     vnc_write_u8(vs, 1);             /* big-endian-flag */
22253e230dd2SCorentin Chary #else
22263e230dd2SCorentin Chary     vnc_write_u8(vs, 0);             /* big-endian-flag */
22273e230dd2SCorentin Chary #endif
22283e230dd2SCorentin Chary     vnc_write_u8(vs, 1);             /* true-color-flag */
22299f64916dSGerd Hoffmann     vnc_write_u16(vs, vs->client_pf.rmax);     /* red-max */
22309f64916dSGerd Hoffmann     vnc_write_u16(vs, vs->client_pf.gmax);     /* green-max */
22319f64916dSGerd Hoffmann     vnc_write_u16(vs, vs->client_pf.bmax);     /* blue-max */
22329f64916dSGerd Hoffmann     vnc_write_u8(vs, vs->client_pf.rshift);    /* red-shift */
22339f64916dSGerd Hoffmann     vnc_write_u8(vs, vs->client_pf.gshift);    /* green-shift */
22349f64916dSGerd Hoffmann     vnc_write_u8(vs, vs->client_pf.bshift);    /* blue-shift */
22359f64916dSGerd Hoffmann     vnc_write(vs, pad, 3);           /* padding */
22363e230dd2SCorentin Chary 
22373e230dd2SCorentin Chary     vnc_hextile_set_pixel_conversion(vs, 0);
22383e230dd2SCorentin Chary     vs->write_pixels = vnc_write_pixels_copy;
22393e230dd2SCorentin Chary }
22403e230dd2SCorentin Chary 
22413e230dd2SCorentin Chary static void vnc_colordepth(VncState *vs)
22423e230dd2SCorentin Chary {
22433e230dd2SCorentin Chary     if (vnc_has_feature(vs, VNC_FEATURE_WMVI)) {
22443e230dd2SCorentin Chary         /* Sending a WMVi message to notify the client*/
2245bd023f95SCorentin Chary         vnc_lock_output(vs);
22463e230dd2SCorentin Chary         vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE);
22473e230dd2SCorentin Chary         vnc_write_u8(vs, 0);
22483e230dd2SCorentin Chary         vnc_write_u16(vs, 1); /* number of rects */
2249d39fa6d8SGerd Hoffmann         vnc_framebuffer_update(vs, 0, 0,
2250bea60dd7SPeter Lieven                                pixman_image_get_width(vs->vd->server),
2251bea60dd7SPeter Lieven                                pixman_image_get_height(vs->vd->server),
2252d39fa6d8SGerd Hoffmann                                VNC_ENCODING_WMVi);
22533e230dd2SCorentin Chary         pixel_format_message(vs);
2254bd023f95SCorentin Chary         vnc_unlock_output(vs);
22553e230dd2SCorentin Chary         vnc_flush(vs);
22563e230dd2SCorentin Chary     } else {
22573e230dd2SCorentin Chary         set_pixel_conversion(vs);
22583e230dd2SCorentin Chary     }
22593e230dd2SCorentin Chary }
22603e230dd2SCorentin Chary 
22613e230dd2SCorentin Chary static int protocol_client_msg(VncState *vs, uint8_t *data, size_t len)
22623e230dd2SCorentin Chary {
22633e230dd2SCorentin Chary     int i;
22643e230dd2SCorentin Chary     uint16_t limit;
22653e230dd2SCorentin Chary     VncDisplay *vd = vs->vd;
22663e230dd2SCorentin Chary 
22673e230dd2SCorentin Chary     if (data[0] > 3) {
22680f7b2864SGerd Hoffmann         update_displaychangelistener(&vd->dcl, VNC_REFRESH_INTERVAL_BASE);
22693e230dd2SCorentin Chary     }
22703e230dd2SCorentin Chary 
22713e230dd2SCorentin Chary     switch (data[0]) {
22723e230dd2SCorentin Chary     case VNC_MSG_CLIENT_SET_PIXEL_FORMAT:
22733e230dd2SCorentin Chary         if (len == 1)
22743e230dd2SCorentin Chary             return 20;
22753e230dd2SCorentin Chary 
22763e230dd2SCorentin Chary         set_pixel_format(vs, read_u8(data, 4), read_u8(data, 5),
22773e230dd2SCorentin Chary                          read_u8(data, 6), read_u8(data, 7),
22783e230dd2SCorentin Chary                          read_u16(data, 8), read_u16(data, 10),
22793e230dd2SCorentin Chary                          read_u16(data, 12), read_u8(data, 14),
22803e230dd2SCorentin Chary                          read_u8(data, 15), read_u8(data, 16));
22813e230dd2SCorentin Chary         break;
22823e230dd2SCorentin Chary     case VNC_MSG_CLIENT_SET_ENCODINGS:
22833e230dd2SCorentin Chary         if (len == 1)
22843e230dd2SCorentin Chary             return 4;
22853e230dd2SCorentin Chary 
22863e230dd2SCorentin Chary         if (len == 4) {
22873e230dd2SCorentin Chary             limit = read_u16(data, 2);
22883e230dd2SCorentin Chary             if (limit > 0)
22893e230dd2SCorentin Chary                 return 4 + (limit * 4);
22903e230dd2SCorentin Chary         } else
22913e230dd2SCorentin Chary             limit = read_u16(data, 2);
22923e230dd2SCorentin Chary 
22933e230dd2SCorentin Chary         for (i = 0; i < limit; i++) {
22943e230dd2SCorentin Chary             int32_t val = read_s32(data, 4 + (i * 4));
22953e230dd2SCorentin Chary             memcpy(data + 4 + (i * 4), &val, sizeof(val));
22963e230dd2SCorentin Chary         }
22973e230dd2SCorentin Chary 
22983e230dd2SCorentin Chary         set_encodings(vs, (int32_t *)(data + 4), limit);
22993e230dd2SCorentin Chary         break;
23003e230dd2SCorentin Chary     case VNC_MSG_CLIENT_FRAMEBUFFER_UPDATE_REQUEST:
23013e230dd2SCorentin Chary         if (len == 1)
23023e230dd2SCorentin Chary             return 10;
23033e230dd2SCorentin Chary 
23043e230dd2SCorentin Chary         framebuffer_update_request(vs,
23053e230dd2SCorentin Chary                                    read_u8(data, 1), read_u16(data, 2), read_u16(data, 4),
23063e230dd2SCorentin Chary                                    read_u16(data, 6), read_u16(data, 8));
23073e230dd2SCorentin Chary         break;
23083e230dd2SCorentin Chary     case VNC_MSG_CLIENT_KEY_EVENT:
23093e230dd2SCorentin Chary         if (len == 1)
23103e230dd2SCorentin Chary             return 8;
23113e230dd2SCorentin Chary 
23123e230dd2SCorentin Chary         key_event(vs, read_u8(data, 1), read_u32(data, 4));
23133e230dd2SCorentin Chary         break;
23143e230dd2SCorentin Chary     case VNC_MSG_CLIENT_POINTER_EVENT:
23153e230dd2SCorentin Chary         if (len == 1)
23163e230dd2SCorentin Chary             return 6;
23173e230dd2SCorentin Chary 
23183e230dd2SCorentin Chary         pointer_event(vs, read_u8(data, 1), read_u16(data, 2), read_u16(data, 4));
23193e230dd2SCorentin Chary         break;
23203e230dd2SCorentin Chary     case VNC_MSG_CLIENT_CUT_TEXT:
2321f9a70e79SPeter Lieven         if (len == 1) {
23223e230dd2SCorentin Chary             return 8;
2323f9a70e79SPeter Lieven         }
23243e230dd2SCorentin Chary         if (len == 8) {
23253e230dd2SCorentin Chary             uint32_t dlen = read_u32(data, 4);
2326f9a70e79SPeter Lieven             if (dlen > (1 << 20)) {
2327f9a70e79SPeter Lieven                 error_report("vnc: client_cut_text msg payload has %u bytes"
2328f9a70e79SPeter Lieven                              " which exceeds our limit of 1MB.", dlen);
2329f9a70e79SPeter Lieven                 vnc_client_error(vs);
2330f9a70e79SPeter Lieven                 break;
2331f9a70e79SPeter Lieven             }
2332f9a70e79SPeter Lieven             if (dlen > 0) {
23333e230dd2SCorentin Chary                 return 8 + dlen;
23343e230dd2SCorentin Chary             }
2335f9a70e79SPeter Lieven         }
23363e230dd2SCorentin Chary 
23373e230dd2SCorentin Chary         client_cut_text(vs, read_u32(data, 4), data + 8);
23383e230dd2SCorentin Chary         break;
23393e230dd2SCorentin Chary     case VNC_MSG_CLIENT_QEMU:
23403e230dd2SCorentin Chary         if (len == 1)
23413e230dd2SCorentin Chary             return 2;
23423e230dd2SCorentin Chary 
23433e230dd2SCorentin Chary         switch (read_u8(data, 1)) {
23443e230dd2SCorentin Chary         case VNC_MSG_CLIENT_QEMU_EXT_KEY_EVENT:
23453e230dd2SCorentin Chary             if (len == 2)
23463e230dd2SCorentin Chary                 return 12;
23473e230dd2SCorentin Chary 
23483e230dd2SCorentin Chary             ext_key_event(vs, read_u16(data, 2),
23493e230dd2SCorentin Chary                           read_u32(data, 4), read_u32(data, 8));
23503e230dd2SCorentin Chary             break;
23513e230dd2SCorentin Chary         case VNC_MSG_CLIENT_QEMU_AUDIO:
23523e230dd2SCorentin Chary             if (len == 2)
23533e230dd2SCorentin Chary                 return 4;
23543e230dd2SCorentin Chary 
23553e230dd2SCorentin Chary             switch (read_u16 (data, 2)) {
23563e230dd2SCorentin Chary             case VNC_MSG_CLIENT_QEMU_AUDIO_ENABLE:
23573e230dd2SCorentin Chary                 audio_add(vs);
23583e230dd2SCorentin Chary                 break;
23593e230dd2SCorentin Chary             case VNC_MSG_CLIENT_QEMU_AUDIO_DISABLE:
23603e230dd2SCorentin Chary                 audio_del(vs);
23613e230dd2SCorentin Chary                 break;
23623e230dd2SCorentin Chary             case VNC_MSG_CLIENT_QEMU_AUDIO_SET_FORMAT:
23633e230dd2SCorentin Chary                 if (len == 4)
23643e230dd2SCorentin Chary                     return 10;
23653e230dd2SCorentin Chary                 switch (read_u8(data, 4)) {
23663e230dd2SCorentin Chary                 case 0: vs->as.fmt = AUD_FMT_U8; break;
23673e230dd2SCorentin Chary                 case 1: vs->as.fmt = AUD_FMT_S8; break;
23683e230dd2SCorentin Chary                 case 2: vs->as.fmt = AUD_FMT_U16; break;
23693e230dd2SCorentin Chary                 case 3: vs->as.fmt = AUD_FMT_S16; break;
23703e230dd2SCorentin Chary                 case 4: vs->as.fmt = AUD_FMT_U32; break;
23713e230dd2SCorentin Chary                 case 5: vs->as.fmt = AUD_FMT_S32; break;
23723e230dd2SCorentin Chary                 default:
2373153130cdSDaniel P. Berrange                     VNC_DEBUG("Invalid audio format %d\n", read_u8(data, 4));
23743e230dd2SCorentin Chary                     vnc_client_error(vs);
23753e230dd2SCorentin Chary                     break;
23763e230dd2SCorentin Chary                 }
23773e230dd2SCorentin Chary                 vs->as.nchannels = read_u8(data, 5);
23783e230dd2SCorentin Chary                 if (vs->as.nchannels != 1 && vs->as.nchannels != 2) {
2379153130cdSDaniel P. Berrange                     VNC_DEBUG("Invalid audio channel coount %d\n",
23803e230dd2SCorentin Chary                               read_u8(data, 5));
23813e230dd2SCorentin Chary                     vnc_client_error(vs);
23823e230dd2SCorentin Chary                     break;
23833e230dd2SCorentin Chary                 }
23843e230dd2SCorentin Chary                 vs->as.freq = read_u32(data, 6);
23853e230dd2SCorentin Chary                 break;
23863e230dd2SCorentin Chary             default:
2387153130cdSDaniel P. Berrange                 VNC_DEBUG("Invalid audio message %d\n", read_u8(data, 4));
23883e230dd2SCorentin Chary                 vnc_client_error(vs);
23893e230dd2SCorentin Chary                 break;
23903e230dd2SCorentin Chary             }
23913e230dd2SCorentin Chary             break;
23923e230dd2SCorentin Chary 
23933e230dd2SCorentin Chary         default:
2394153130cdSDaniel P. Berrange             VNC_DEBUG("Msg: %d\n", read_u16(data, 0));
23953e230dd2SCorentin Chary             vnc_client_error(vs);
23963e230dd2SCorentin Chary             break;
23973e230dd2SCorentin Chary         }
23983e230dd2SCorentin Chary         break;
23993e230dd2SCorentin Chary     default:
2400153130cdSDaniel P. Berrange         VNC_DEBUG("Msg: %d\n", data[0]);
24013e230dd2SCorentin Chary         vnc_client_error(vs);
24023e230dd2SCorentin Chary         break;
24033e230dd2SCorentin Chary     }
24043e230dd2SCorentin Chary 
24053e230dd2SCorentin Chary     vnc_read_when(vs, protocol_client_msg, 1);
24063e230dd2SCorentin Chary     return 0;
24073e230dd2SCorentin Chary }
24083e230dd2SCorentin Chary 
24093e230dd2SCorentin Chary static int protocol_client_init(VncState *vs, uint8_t *data, size_t len)
24103e230dd2SCorentin Chary {
24113e230dd2SCorentin Chary     char buf[1024];
24128cf36489SGerd Hoffmann     VncShareMode mode;
24133e230dd2SCorentin Chary     int size;
24143e230dd2SCorentin Chary 
24158cf36489SGerd Hoffmann     mode = data[0] ? VNC_SHARE_MODE_SHARED : VNC_SHARE_MODE_EXCLUSIVE;
24168cf36489SGerd Hoffmann     switch (vs->vd->share_policy) {
24178cf36489SGerd Hoffmann     case VNC_SHARE_POLICY_IGNORE:
24188cf36489SGerd Hoffmann         /*
24198cf36489SGerd Hoffmann          * Ignore the shared flag.  Nothing to do here.
24208cf36489SGerd Hoffmann          *
24218cf36489SGerd Hoffmann          * Doesn't conform to the rfb spec but is traditional qemu
24228cf36489SGerd Hoffmann          * behavior, thus left here as option for compatibility
24238cf36489SGerd Hoffmann          * reasons.
24248cf36489SGerd Hoffmann          */
24258cf36489SGerd Hoffmann         break;
24268cf36489SGerd Hoffmann     case VNC_SHARE_POLICY_ALLOW_EXCLUSIVE:
24278cf36489SGerd Hoffmann         /*
24288cf36489SGerd Hoffmann          * Policy: Allow clients ask for exclusive access.
24298cf36489SGerd Hoffmann          *
24308cf36489SGerd Hoffmann          * Implementation: When a client asks for exclusive access,
24318cf36489SGerd Hoffmann          * disconnect all others. Shared connects are allowed as long
24328cf36489SGerd Hoffmann          * as no exclusive connection exists.
24338cf36489SGerd Hoffmann          *
24348cf36489SGerd Hoffmann          * This is how the rfb spec suggests to handle the shared flag.
24358cf36489SGerd Hoffmann          */
24368cf36489SGerd Hoffmann         if (mode == VNC_SHARE_MODE_EXCLUSIVE) {
24378cf36489SGerd Hoffmann             VncState *client;
24388cf36489SGerd Hoffmann             QTAILQ_FOREACH(client, &vs->vd->clients, next) {
24398cf36489SGerd Hoffmann                 if (vs == client) {
24408cf36489SGerd Hoffmann                     continue;
24418cf36489SGerd Hoffmann                 }
24428cf36489SGerd Hoffmann                 if (client->share_mode != VNC_SHARE_MODE_EXCLUSIVE &&
24438cf36489SGerd Hoffmann                     client->share_mode != VNC_SHARE_MODE_SHARED) {
24448cf36489SGerd Hoffmann                     continue;
24458cf36489SGerd Hoffmann                 }
24468cf36489SGerd Hoffmann                 vnc_disconnect_start(client);
24478cf36489SGerd Hoffmann             }
24488cf36489SGerd Hoffmann         }
24498cf36489SGerd Hoffmann         if (mode == VNC_SHARE_MODE_SHARED) {
24508cf36489SGerd Hoffmann             if (vs->vd->num_exclusive > 0) {
24518cf36489SGerd Hoffmann                 vnc_disconnect_start(vs);
24528cf36489SGerd Hoffmann                 return 0;
24538cf36489SGerd Hoffmann             }
24548cf36489SGerd Hoffmann         }
24558cf36489SGerd Hoffmann         break;
24568cf36489SGerd Hoffmann     case VNC_SHARE_POLICY_FORCE_SHARED:
24578cf36489SGerd Hoffmann         /*
24588cf36489SGerd Hoffmann          * Policy: Shared connects only.
24598cf36489SGerd Hoffmann          * Implementation: Disallow clients asking for exclusive access.
24608cf36489SGerd Hoffmann          *
24618cf36489SGerd Hoffmann          * Useful for shared desktop sessions where you don't want
24628cf36489SGerd Hoffmann          * someone forgetting to say -shared when running the vnc
24638cf36489SGerd Hoffmann          * client disconnect everybody else.
24648cf36489SGerd Hoffmann          */
24658cf36489SGerd Hoffmann         if (mode == VNC_SHARE_MODE_EXCLUSIVE) {
24668cf36489SGerd Hoffmann             vnc_disconnect_start(vs);
24678cf36489SGerd Hoffmann             return 0;
24688cf36489SGerd Hoffmann         }
24698cf36489SGerd Hoffmann         break;
24708cf36489SGerd Hoffmann     }
24718cf36489SGerd Hoffmann     vnc_set_share_mode(vs, mode);
24728cf36489SGerd Hoffmann 
2473e5f34cddSGerd Hoffmann     if (vs->vd->num_shared > vs->vd->connections_limit) {
2474e5f34cddSGerd Hoffmann         vnc_disconnect_start(vs);
2475e5f34cddSGerd Hoffmann         return 0;
2476e5f34cddSGerd Hoffmann     }
2477e5f34cddSGerd Hoffmann 
2478bea60dd7SPeter Lieven     vs->client_width = pixman_image_get_width(vs->vd->server);
2479bea60dd7SPeter Lieven     vs->client_height = pixman_image_get_height(vs->vd->server);
24803e230dd2SCorentin Chary     vnc_write_u16(vs, vs->client_width);
24813e230dd2SCorentin Chary     vnc_write_u16(vs, vs->client_height);
24823e230dd2SCorentin Chary 
24833e230dd2SCorentin Chary     pixel_format_message(vs);
24843e230dd2SCorentin Chary 
24853e230dd2SCorentin Chary     if (qemu_name)
24863e230dd2SCorentin Chary         size = snprintf(buf, sizeof(buf), "QEMU (%s)", qemu_name);
24873e230dd2SCorentin Chary     else
24883e230dd2SCorentin Chary         size = snprintf(buf, sizeof(buf), "QEMU");
24893e230dd2SCorentin Chary 
24903e230dd2SCorentin Chary     vnc_write_u32(vs, size);
24913e230dd2SCorentin Chary     vnc_write(vs, buf, size);
24923e230dd2SCorentin Chary     vnc_flush(vs);
24933e230dd2SCorentin Chary 
24943e230dd2SCorentin Chary     vnc_client_cache_auth(vs);
2495fb6ba0d5SWenchao Xia     vnc_qmp_event(vs, QAPI_EVENT_VNC_INITIALIZED);
24963e230dd2SCorentin Chary 
24973e230dd2SCorentin Chary     vnc_read_when(vs, protocol_client_msg, 1);
24983e230dd2SCorentin Chary 
24993e230dd2SCorentin Chary     return 0;
25003e230dd2SCorentin Chary }
25013e230dd2SCorentin Chary 
25023e230dd2SCorentin Chary void start_client_init(VncState *vs)
25033e230dd2SCorentin Chary {
25043e230dd2SCorentin Chary     vnc_read_when(vs, protocol_client_init, 1);
25053e230dd2SCorentin Chary }
25063e230dd2SCorentin Chary 
25073e230dd2SCorentin Chary static void make_challenge(VncState *vs)
25083e230dd2SCorentin Chary {
25093e230dd2SCorentin Chary     int i;
25103e230dd2SCorentin Chary 
25113e230dd2SCorentin Chary     srand(time(NULL)+getpid()+getpid()*987654+rand());
25123e230dd2SCorentin Chary 
25133e230dd2SCorentin Chary     for (i = 0 ; i < sizeof(vs->challenge) ; i++)
25143e230dd2SCorentin Chary         vs->challenge[i] = (int) (256.0*rand()/(RAND_MAX+1.0));
25153e230dd2SCorentin Chary }
25163e230dd2SCorentin Chary 
25173e230dd2SCorentin Chary static int protocol_client_auth_vnc(VncState *vs, uint8_t *data, size_t len)
25183e230dd2SCorentin Chary {
25193e230dd2SCorentin Chary     unsigned char response[VNC_AUTH_CHALLENGE_SIZE];
2520800567a6SDaniel P. Berrange     size_t i, pwlen;
25213e230dd2SCorentin Chary     unsigned char key[8];
25223c9405a0SGerd Hoffmann     time_t now = time(NULL);
252360928458SGonglei     QCryptoCipher *cipher = NULL;
2524800567a6SDaniel P. Berrange     Error *err = NULL;
25253e230dd2SCorentin Chary 
25261cd20f8bSAnthony Liguori     if (!vs->vd->password) {
25273e230dd2SCorentin Chary         VNC_DEBUG("No password configured on server");
25286bffdf0fSGerd Hoffmann         goto reject;
25293e230dd2SCorentin Chary     }
25303c9405a0SGerd Hoffmann     if (vs->vd->expires < now) {
25313c9405a0SGerd Hoffmann         VNC_DEBUG("Password is expired");
25323c9405a0SGerd Hoffmann         goto reject;
25333c9405a0SGerd Hoffmann     }
25343e230dd2SCorentin Chary 
25353e230dd2SCorentin Chary     memcpy(response, vs->challenge, VNC_AUTH_CHALLENGE_SIZE);
25363e230dd2SCorentin Chary 
25373e230dd2SCorentin Chary     /* Calculate the expected challenge response */
25383e230dd2SCorentin Chary     pwlen = strlen(vs->vd->password);
25393e230dd2SCorentin Chary     for (i=0; i<sizeof(key); i++)
25403e230dd2SCorentin Chary         key[i] = i<pwlen ? vs->vd->password[i] : 0;
2541800567a6SDaniel P. Berrange 
2542800567a6SDaniel P. Berrange     cipher = qcrypto_cipher_new(
2543800567a6SDaniel P. Berrange         QCRYPTO_CIPHER_ALG_DES_RFB,
2544800567a6SDaniel P. Berrange         QCRYPTO_CIPHER_MODE_ECB,
2545800567a6SDaniel P. Berrange         key, G_N_ELEMENTS(key),
2546800567a6SDaniel P. Berrange         &err);
2547800567a6SDaniel P. Berrange     if (!cipher) {
2548800567a6SDaniel P. Berrange         VNC_DEBUG("Cannot initialize cipher %s",
2549800567a6SDaniel P. Berrange                   error_get_pretty(err));
2550800567a6SDaniel P. Berrange         error_free(err);
2551800567a6SDaniel P. Berrange         goto reject;
2552800567a6SDaniel P. Berrange     }
2553800567a6SDaniel P. Berrange 
2554a1695137SWolfgang Bumiller     if (qcrypto_cipher_encrypt(cipher,
2555800567a6SDaniel P. Berrange                                vs->challenge,
2556800567a6SDaniel P. Berrange                                response,
2557800567a6SDaniel P. Berrange                                VNC_AUTH_CHALLENGE_SIZE,
2558800567a6SDaniel P. Berrange                                &err) < 0) {
2559800567a6SDaniel P. Berrange         VNC_DEBUG("Cannot encrypt challenge %s",
2560800567a6SDaniel P. Berrange                   error_get_pretty(err));
2561800567a6SDaniel P. Berrange         error_free(err);
2562800567a6SDaniel P. Berrange         goto reject;
2563800567a6SDaniel P. Berrange     }
25643e230dd2SCorentin Chary 
25653e230dd2SCorentin Chary     /* Compare expected vs actual challenge response */
25663e230dd2SCorentin Chary     if (memcmp(response, data, VNC_AUTH_CHALLENGE_SIZE) != 0) {
2567e5bed759SDong Xu Wang         VNC_DEBUG("Client challenge response did not match\n");
25686bffdf0fSGerd Hoffmann         goto reject;
25696bffdf0fSGerd Hoffmann     } else {
25706bffdf0fSGerd Hoffmann         VNC_DEBUG("Accepting VNC challenge response\n");
25716bffdf0fSGerd Hoffmann         vnc_write_u32(vs, 0); /* Accept auth */
25726bffdf0fSGerd Hoffmann         vnc_flush(vs);
25736bffdf0fSGerd Hoffmann 
25746bffdf0fSGerd Hoffmann         start_client_init(vs);
25756bffdf0fSGerd Hoffmann     }
257660928458SGonglei 
257760928458SGonglei     qcrypto_cipher_free(cipher);
25786bffdf0fSGerd Hoffmann     return 0;
25796bffdf0fSGerd Hoffmann 
25806bffdf0fSGerd Hoffmann reject:
25813e230dd2SCorentin Chary     vnc_write_u32(vs, 1); /* Reject auth */
25823e230dd2SCorentin Chary     if (vs->minor >= 8) {
25833e230dd2SCorentin Chary         static const char err[] = "Authentication failed";
25843e230dd2SCorentin Chary         vnc_write_u32(vs, sizeof(err));
25853e230dd2SCorentin Chary         vnc_write(vs, err, sizeof(err));
25863e230dd2SCorentin Chary     }
25873e230dd2SCorentin Chary     vnc_flush(vs);
25883e230dd2SCorentin Chary     vnc_client_error(vs);
258960928458SGonglei     qcrypto_cipher_free(cipher);
25903e230dd2SCorentin Chary     return 0;
25913e230dd2SCorentin Chary }
25923e230dd2SCorentin Chary 
25933e230dd2SCorentin Chary void start_auth_vnc(VncState *vs)
25943e230dd2SCorentin Chary {
25953e230dd2SCorentin Chary     make_challenge(vs);
25963e230dd2SCorentin Chary     /* Send client a 'random' challenge */
25973e230dd2SCorentin Chary     vnc_write(vs, vs->challenge, sizeof(vs->challenge));
25983e230dd2SCorentin Chary     vnc_flush(vs);
25993e230dd2SCorentin Chary 
26003e230dd2SCorentin Chary     vnc_read_when(vs, protocol_client_auth_vnc, sizeof(vs->challenge));
26013e230dd2SCorentin Chary }
26023e230dd2SCorentin Chary 
26033e230dd2SCorentin Chary 
26043e230dd2SCorentin Chary static int protocol_client_auth(VncState *vs, uint8_t *data, size_t len)
26053e230dd2SCorentin Chary {
26063e230dd2SCorentin Chary     /* We only advertise 1 auth scheme at a time, so client
26073e230dd2SCorentin Chary      * must pick the one we sent. Verify this */
26087e7e2ebcSDaniel P. Berrange     if (data[0] != vs->auth) { /* Reject auth */
26093e230dd2SCorentin Chary        VNC_DEBUG("Reject auth %d because it didn't match advertized\n", (int)data[0]);
26103e230dd2SCorentin Chary        vnc_write_u32(vs, 1);
26113e230dd2SCorentin Chary        if (vs->minor >= 8) {
26123e230dd2SCorentin Chary            static const char err[] = "Authentication failed";
26133e230dd2SCorentin Chary            vnc_write_u32(vs, sizeof(err));
26143e230dd2SCorentin Chary            vnc_write(vs, err, sizeof(err));
26153e230dd2SCorentin Chary        }
26163e230dd2SCorentin Chary        vnc_client_error(vs);
26173e230dd2SCorentin Chary     } else { /* Accept requested auth */
26183e230dd2SCorentin Chary        VNC_DEBUG("Client requested auth %d\n", (int)data[0]);
26197e7e2ebcSDaniel P. Berrange        switch (vs->auth) {
26203e230dd2SCorentin Chary        case VNC_AUTH_NONE:
26213e230dd2SCorentin Chary            VNC_DEBUG("Accept auth none\n");
26223e230dd2SCorentin Chary            if (vs->minor >= 8) {
26233e230dd2SCorentin Chary                vnc_write_u32(vs, 0); /* Accept auth completion */
26243e230dd2SCorentin Chary                vnc_flush(vs);
26253e230dd2SCorentin Chary            }
26263e230dd2SCorentin Chary            start_client_init(vs);
26273e230dd2SCorentin Chary            break;
26283e230dd2SCorentin Chary 
26293e230dd2SCorentin Chary        case VNC_AUTH_VNC:
26303e230dd2SCorentin Chary            VNC_DEBUG("Start VNC auth\n");
26313e230dd2SCorentin Chary            start_auth_vnc(vs);
26323e230dd2SCorentin Chary            break;
26333e230dd2SCorentin Chary 
26343e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS
26353e230dd2SCorentin Chary        case VNC_AUTH_VENCRYPT:
26363a93113aSDong Xu Wang            VNC_DEBUG("Accept VeNCrypt auth\n");
26373e230dd2SCorentin Chary            start_auth_vencrypt(vs);
26383e230dd2SCorentin Chary            break;
26393e230dd2SCorentin Chary #endif /* CONFIG_VNC_TLS */
26403e230dd2SCorentin Chary 
26413e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL
26423e230dd2SCorentin Chary        case VNC_AUTH_SASL:
26433e230dd2SCorentin Chary            VNC_DEBUG("Accept SASL auth\n");
26443e230dd2SCorentin Chary            start_auth_sasl(vs);
26453e230dd2SCorentin Chary            break;
26463e230dd2SCorentin Chary #endif /* CONFIG_VNC_SASL */
26473e230dd2SCorentin Chary 
26483e230dd2SCorentin Chary        default: /* Should not be possible, but just in case */
26497e7e2ebcSDaniel P. Berrange            VNC_DEBUG("Reject auth %d server code bug\n", vs->auth);
26503e230dd2SCorentin Chary            vnc_write_u8(vs, 1);
26513e230dd2SCorentin Chary            if (vs->minor >= 8) {
26523e230dd2SCorentin Chary                static const char err[] = "Authentication failed";
26533e230dd2SCorentin Chary                vnc_write_u32(vs, sizeof(err));
26543e230dd2SCorentin Chary                vnc_write(vs, err, sizeof(err));
26553e230dd2SCorentin Chary            }
26563e230dd2SCorentin Chary            vnc_client_error(vs);
26573e230dd2SCorentin Chary        }
26583e230dd2SCorentin Chary     }
26593e230dd2SCorentin Chary     return 0;
26603e230dd2SCorentin Chary }
26613e230dd2SCorentin Chary 
26623e230dd2SCorentin Chary static int protocol_version(VncState *vs, uint8_t *version, size_t len)
26633e230dd2SCorentin Chary {
26643e230dd2SCorentin Chary     char local[13];
26653e230dd2SCorentin Chary 
26663e230dd2SCorentin Chary     memcpy(local, version, 12);
26673e230dd2SCorentin Chary     local[12] = 0;
26683e230dd2SCorentin Chary 
26693e230dd2SCorentin Chary     if (sscanf(local, "RFB %03d.%03d\n", &vs->major, &vs->minor) != 2) {
26703e230dd2SCorentin Chary         VNC_DEBUG("Malformed protocol version %s\n", local);
26713e230dd2SCorentin Chary         vnc_client_error(vs);
26723e230dd2SCorentin Chary         return 0;
26733e230dd2SCorentin Chary     }
26743e230dd2SCorentin Chary     VNC_DEBUG("Client request protocol version %d.%d\n", vs->major, vs->minor);
26753e230dd2SCorentin Chary     if (vs->major != 3 ||
26763e230dd2SCorentin Chary         (vs->minor != 3 &&
26773e230dd2SCorentin Chary          vs->minor != 4 &&
26783e230dd2SCorentin Chary          vs->minor != 5 &&
26793e230dd2SCorentin Chary          vs->minor != 7 &&
26803e230dd2SCorentin Chary          vs->minor != 8)) {
26813e230dd2SCorentin Chary         VNC_DEBUG("Unsupported client version\n");
26823e230dd2SCorentin Chary         vnc_write_u32(vs, VNC_AUTH_INVALID);
26833e230dd2SCorentin Chary         vnc_flush(vs);
26843e230dd2SCorentin Chary         vnc_client_error(vs);
26853e230dd2SCorentin Chary         return 0;
26863e230dd2SCorentin Chary     }
26873e230dd2SCorentin Chary     /* Some broken clients report v3.4 or v3.5, which spec requires to be treated
26883e230dd2SCorentin Chary      * as equivalent to v3.3 by servers
26893e230dd2SCorentin Chary      */
26903e230dd2SCorentin Chary     if (vs->minor == 4 || vs->minor == 5)
26913e230dd2SCorentin Chary         vs->minor = 3;
26923e230dd2SCorentin Chary 
26933e230dd2SCorentin Chary     if (vs->minor == 3) {
26947e7e2ebcSDaniel P. Berrange         if (vs->auth == VNC_AUTH_NONE) {
26953e230dd2SCorentin Chary             VNC_DEBUG("Tell client auth none\n");
26967e7e2ebcSDaniel P. Berrange             vnc_write_u32(vs, vs->auth);
26973e230dd2SCorentin Chary             vnc_flush(vs);
26983e230dd2SCorentin Chary             start_client_init(vs);
26997e7e2ebcSDaniel P. Berrange        } else if (vs->auth == VNC_AUTH_VNC) {
27003e230dd2SCorentin Chary             VNC_DEBUG("Tell client VNC auth\n");
27017e7e2ebcSDaniel P. Berrange             vnc_write_u32(vs, vs->auth);
27023e230dd2SCorentin Chary             vnc_flush(vs);
27033e230dd2SCorentin Chary             start_auth_vnc(vs);
27043e230dd2SCorentin Chary        } else {
27057e7e2ebcSDaniel P. Berrange             VNC_DEBUG("Unsupported auth %d for protocol 3.3\n", vs->auth);
27063e230dd2SCorentin Chary             vnc_write_u32(vs, VNC_AUTH_INVALID);
27073e230dd2SCorentin Chary             vnc_flush(vs);
27083e230dd2SCorentin Chary             vnc_client_error(vs);
27093e230dd2SCorentin Chary        }
27103e230dd2SCorentin Chary     } else {
27117e7e2ebcSDaniel P. Berrange         VNC_DEBUG("Telling client we support auth %d\n", vs->auth);
27123e230dd2SCorentin Chary         vnc_write_u8(vs, 1); /* num auth */
27137e7e2ebcSDaniel P. Berrange         vnc_write_u8(vs, vs->auth);
27143e230dd2SCorentin Chary         vnc_read_when(vs, protocol_client_auth, 1);
27153e230dd2SCorentin Chary         vnc_flush(vs);
27163e230dd2SCorentin Chary     }
27173e230dd2SCorentin Chary 
27183e230dd2SCorentin Chary     return 0;
27193e230dd2SCorentin Chary }
27203e230dd2SCorentin Chary 
2721999342a0SCorentin Chary static VncRectStat *vnc_stat_rect(VncDisplay *vd, int x, int y)
2722999342a0SCorentin Chary {
2723999342a0SCorentin Chary     struct VncSurface *vs = &vd->guest;
2724999342a0SCorentin Chary 
2725999342a0SCorentin Chary     return &vs->stats[y / VNC_STAT_RECT][x / VNC_STAT_RECT];
2726999342a0SCorentin Chary }
2727999342a0SCorentin Chary 
27287d964c9dSCorentin Chary void vnc_sent_lossy_rect(VncState *vs, int x, int y, int w, int h)
27297d964c9dSCorentin Chary {
27307d964c9dSCorentin Chary     int i, j;
27317d964c9dSCorentin Chary 
27327d964c9dSCorentin Chary     w = (x + w) / VNC_STAT_RECT;
27337d964c9dSCorentin Chary     h = (y + h) / VNC_STAT_RECT;
27347d964c9dSCorentin Chary     x /= VNC_STAT_RECT;
27357d964c9dSCorentin Chary     y /= VNC_STAT_RECT;
27367d964c9dSCorentin Chary 
2737207f328aSCorentin Chary     for (j = y; j <= h; j++) {
2738207f328aSCorentin Chary         for (i = x; i <= w; i++) {
27397d964c9dSCorentin Chary             vs->lossy_rect[j][i] = 1;
27407d964c9dSCorentin Chary         }
27417d964c9dSCorentin Chary     }
27427d964c9dSCorentin Chary }
27437d964c9dSCorentin Chary 
27447d964c9dSCorentin Chary static int vnc_refresh_lossy_rect(VncDisplay *vd, int x, int y)
27457d964c9dSCorentin Chary {
27467d964c9dSCorentin Chary     VncState *vs;
27477d964c9dSCorentin Chary     int sty = y / VNC_STAT_RECT;
27487d964c9dSCorentin Chary     int stx = x / VNC_STAT_RECT;
27497d964c9dSCorentin Chary     int has_dirty = 0;
27507d964c9dSCorentin Chary 
27517d964c9dSCorentin Chary     y = y / VNC_STAT_RECT * VNC_STAT_RECT;
27527d964c9dSCorentin Chary     x = x / VNC_STAT_RECT * VNC_STAT_RECT;
27537d964c9dSCorentin Chary 
27547d964c9dSCorentin Chary     QTAILQ_FOREACH(vs, &vd->clients, next) {
2755bc2429b9SCorentin Chary         int j;
27567d964c9dSCorentin Chary 
27577d964c9dSCorentin Chary         /* kernel send buffers are full -> refresh later */
27587d964c9dSCorentin Chary         if (vs->output.offset) {
27597d964c9dSCorentin Chary             continue;
27607d964c9dSCorentin Chary         }
27617d964c9dSCorentin Chary 
27627d964c9dSCorentin Chary         if (!vs->lossy_rect[sty][stx]) {
27637d964c9dSCorentin Chary             continue;
27647d964c9dSCorentin Chary         }
2765207f328aSCorentin Chary 
27667d964c9dSCorentin Chary         vs->lossy_rect[sty][stx] = 0;
27677d964c9dSCorentin Chary         for (j = 0; j < VNC_STAT_RECT; ++j) {
2768b4c85ddcSPeter Lieven             bitmap_set(vs->dirty[y + j],
2769b4c85ddcSPeter Lieven                        x / VNC_DIRTY_PIXELS_PER_BIT,
2770b4c85ddcSPeter Lieven                        VNC_STAT_RECT / VNC_DIRTY_PIXELS_PER_BIT);
27717d964c9dSCorentin Chary         }
27727d964c9dSCorentin Chary         has_dirty++;
27737d964c9dSCorentin Chary     }
2774207f328aSCorentin Chary 
27757d964c9dSCorentin Chary     return has_dirty;
27767d964c9dSCorentin Chary }
27777d964c9dSCorentin Chary 
27787d964c9dSCorentin Chary static int vnc_update_stats(VncDisplay *vd,  struct timeval * tv)
2779999342a0SCorentin Chary {
27809f64916dSGerd Hoffmann     int width = pixman_image_get_width(vd->guest.fb);
27819f64916dSGerd Hoffmann     int height = pixman_image_get_height(vd->guest.fb);
2782999342a0SCorentin Chary     int x, y;
2783999342a0SCorentin Chary     struct timeval res;
27847d964c9dSCorentin Chary     int has_dirty = 0;
2785999342a0SCorentin Chary 
27869f64916dSGerd Hoffmann     for (y = 0; y < height; y += VNC_STAT_RECT) {
27879f64916dSGerd Hoffmann         for (x = 0; x < width; x += VNC_STAT_RECT) {
2788999342a0SCorentin Chary             VncRectStat *rect = vnc_stat_rect(vd, x, y);
2789999342a0SCorentin Chary 
2790999342a0SCorentin Chary             rect->updated = false;
2791999342a0SCorentin Chary         }
2792999342a0SCorentin Chary     }
2793999342a0SCorentin Chary 
2794ad620c29SBlue Swirl     qemu_timersub(tv, &VNC_REFRESH_STATS, &res);
2795999342a0SCorentin Chary 
2796999342a0SCorentin Chary     if (timercmp(&vd->guest.last_freq_check, &res, >)) {
27977d964c9dSCorentin Chary         return has_dirty;
2798999342a0SCorentin Chary     }
2799999342a0SCorentin Chary     vd->guest.last_freq_check = *tv;
2800999342a0SCorentin Chary 
28019f64916dSGerd Hoffmann     for (y = 0; y < height; y += VNC_STAT_RECT) {
28029f64916dSGerd Hoffmann         for (x = 0; x < width; x += VNC_STAT_RECT) {
2803999342a0SCorentin Chary             VncRectStat *rect= vnc_stat_rect(vd, x, y);
2804999342a0SCorentin Chary             int count = ARRAY_SIZE(rect->times);
2805999342a0SCorentin Chary             struct timeval min, max;
2806999342a0SCorentin Chary 
2807999342a0SCorentin Chary             if (!timerisset(&rect->times[count - 1])) {
2808999342a0SCorentin Chary                 continue ;
2809999342a0SCorentin Chary             }
2810999342a0SCorentin Chary 
2811999342a0SCorentin Chary             max = rect->times[(rect->idx + count - 1) % count];
2812ad620c29SBlue Swirl             qemu_timersub(tv, &max, &res);
2813999342a0SCorentin Chary 
2814999342a0SCorentin Chary             if (timercmp(&res, &VNC_REFRESH_LOSSY, >)) {
2815999342a0SCorentin Chary                 rect->freq = 0;
28167d964c9dSCorentin Chary                 has_dirty += vnc_refresh_lossy_rect(vd, x, y);
2817999342a0SCorentin Chary                 memset(rect->times, 0, sizeof (rect->times));
2818999342a0SCorentin Chary                 continue ;
2819999342a0SCorentin Chary             }
2820999342a0SCorentin Chary 
2821999342a0SCorentin Chary             min = rect->times[rect->idx];
2822999342a0SCorentin Chary             max = rect->times[(rect->idx + count - 1) % count];
2823ad620c29SBlue Swirl             qemu_timersub(&max, &min, &res);
2824999342a0SCorentin Chary 
2825999342a0SCorentin Chary             rect->freq = res.tv_sec + res.tv_usec / 1000000.;
2826999342a0SCorentin Chary             rect->freq /= count;
2827999342a0SCorentin Chary             rect->freq = 1. / rect->freq;
2828999342a0SCorentin Chary         }
2829999342a0SCorentin Chary     }
28307d964c9dSCorentin Chary     return has_dirty;
2831999342a0SCorentin Chary }
2832999342a0SCorentin Chary 
2833999342a0SCorentin Chary double vnc_update_freq(VncState *vs, int x, int y, int w, int h)
2834999342a0SCorentin Chary {
2835999342a0SCorentin Chary     int i, j;
2836999342a0SCorentin Chary     double total = 0;
2837999342a0SCorentin Chary     int num = 0;
2838999342a0SCorentin Chary 
2839999342a0SCorentin Chary     x =  (x / VNC_STAT_RECT) * VNC_STAT_RECT;
2840999342a0SCorentin Chary     y =  (y / VNC_STAT_RECT) * VNC_STAT_RECT;
2841999342a0SCorentin Chary 
2842999342a0SCorentin Chary     for (j = y; j <= y + h; j += VNC_STAT_RECT) {
2843999342a0SCorentin Chary         for (i = x; i <= x + w; i += VNC_STAT_RECT) {
2844999342a0SCorentin Chary             total += vnc_stat_rect(vs->vd, i, j)->freq;
2845999342a0SCorentin Chary             num++;
2846999342a0SCorentin Chary         }
2847999342a0SCorentin Chary     }
2848999342a0SCorentin Chary 
2849999342a0SCorentin Chary     if (num) {
2850999342a0SCorentin Chary         return total / num;
2851999342a0SCorentin Chary     } else {
2852999342a0SCorentin Chary         return 0;
2853999342a0SCorentin Chary     }
2854999342a0SCorentin Chary }
2855999342a0SCorentin Chary 
2856999342a0SCorentin Chary static void vnc_rect_updated(VncDisplay *vd, int x, int y, struct timeval * tv)
2857999342a0SCorentin Chary {
2858999342a0SCorentin Chary     VncRectStat *rect;
2859999342a0SCorentin Chary 
2860999342a0SCorentin Chary     rect = vnc_stat_rect(vd, x, y);
2861999342a0SCorentin Chary     if (rect->updated) {
2862999342a0SCorentin Chary         return ;
2863999342a0SCorentin Chary     }
2864999342a0SCorentin Chary     rect->times[rect->idx] = *tv;
2865999342a0SCorentin Chary     rect->idx = (rect->idx + 1) % ARRAY_SIZE(rect->times);
2866999342a0SCorentin Chary     rect->updated = true;
2867999342a0SCorentin Chary }
2868999342a0SCorentin Chary 
28693e230dd2SCorentin Chary static int vnc_refresh_server_surface(VncDisplay *vd)
28703e230dd2SCorentin Chary {
2871bea60dd7SPeter Lieven     int width = MIN(pixman_image_get_width(vd->guest.fb),
2872bea60dd7SPeter Lieven                     pixman_image_get_width(vd->server));
2873bea60dd7SPeter Lieven     int height = MIN(pixman_image_get_height(vd->guest.fb),
2874bea60dd7SPeter Lieven                      pixman_image_get_height(vd->server));
2875eb8934b0SGerd Hoffmann     int cmp_bytes, server_stride, line_bytes, guest_ll, guest_stride, y = 0;
287612b316d4SPeter Lieven     uint8_t *guest_row0 = NULL, *server_row0;
28773e230dd2SCorentin Chary     VncState *vs;
28783e230dd2SCorentin Chary     int has_dirty = 0;
28799f64916dSGerd Hoffmann     pixman_image_t *tmpbuf = NULL;
28803e230dd2SCorentin Chary 
288180e0c8c3SCorentin Chary     struct timeval tv = { 0, 0 };
2882999342a0SCorentin Chary 
288380e0c8c3SCorentin Chary     if (!vd->non_adaptive) {
2884999342a0SCorentin Chary         gettimeofday(&tv, NULL);
28857d964c9dSCorentin Chary         has_dirty = vnc_update_stats(vd, &tv);
288680e0c8c3SCorentin Chary     }
2887999342a0SCorentin Chary 
28883e230dd2SCorentin Chary     /*
28893e230dd2SCorentin Chary      * Walk through the guest dirty map.
28903e230dd2SCorentin Chary      * Check and copy modified bits from guest to server surface.
28913e230dd2SCorentin Chary      * Update server dirty map.
28923e230dd2SCorentin Chary      */
2893bea60dd7SPeter Lieven     server_row0 = (uint8_t *)pixman_image_get_data(vd->server);
2894eb8934b0SGerd Hoffmann     server_stride = guest_stride = guest_ll =
2895eb8934b0SGerd Hoffmann         pixman_image_get_stride(vd->server);
2896bea60dd7SPeter Lieven     cmp_bytes = MIN(VNC_DIRTY_PIXELS_PER_BIT * VNC_SERVER_FB_BYTES,
2897bea60dd7SPeter Lieven                     server_stride);
28989f64916dSGerd Hoffmann     if (vd->guest.format != VNC_SERVER_FB_FORMAT) {
28999f64916dSGerd Hoffmann         int width = pixman_image_get_width(vd->server);
29009f64916dSGerd Hoffmann         tmpbuf = qemu_pixman_linebuf_create(VNC_SERVER_FB_FORMAT, width);
290112b316d4SPeter Lieven     } else {
2902eb8934b0SGerd Hoffmann         int guest_bpp =
2903eb8934b0SGerd Hoffmann             PIXMAN_FORMAT_BPP(pixman_image_get_format(vd->guest.fb));
290412b316d4SPeter Lieven         guest_row0 = (uint8_t *)pixman_image_get_data(vd->guest.fb);
290512b316d4SPeter Lieven         guest_stride = pixman_image_get_stride(vd->guest.fb);
2906eb8934b0SGerd Hoffmann         guest_ll = pixman_image_get_width(vd->guest.fb) * ((guest_bpp + 7) / 8);
29079f64916dSGerd Hoffmann     }
2908eb8934b0SGerd Hoffmann     line_bytes = MIN(server_stride, guest_ll);
290912b316d4SPeter Lieven 
291012b316d4SPeter Lieven     for (;;) {
29113e230dd2SCorentin Chary         int x;
291212b316d4SPeter Lieven         uint8_t *guest_ptr, *server_ptr;
291312b316d4SPeter Lieven         unsigned long offset = find_next_bit((unsigned long *) &vd->guest.dirty,
291412b316d4SPeter Lieven                                              height * VNC_DIRTY_BPL(&vd->guest),
291512b316d4SPeter Lieven                                              y * VNC_DIRTY_BPL(&vd->guest));
291612b316d4SPeter Lieven         if (offset == height * VNC_DIRTY_BPL(&vd->guest)) {
291712b316d4SPeter Lieven             /* no more dirty bits */
291812b316d4SPeter Lieven             break;
291912b316d4SPeter Lieven         }
292012b316d4SPeter Lieven         y = offset / VNC_DIRTY_BPL(&vd->guest);
292112b316d4SPeter Lieven         x = offset % VNC_DIRTY_BPL(&vd->guest);
292212b316d4SPeter Lieven 
292312b316d4SPeter Lieven         server_ptr = server_row0 + y * server_stride + x * cmp_bytes;
29243e230dd2SCorentin Chary 
29259f64916dSGerd Hoffmann         if (vd->guest.format != VNC_SERVER_FB_FORMAT) {
2926bc210eb1SGerd Hoffmann             qemu_pixman_linebuf_fill(tmpbuf, vd->guest.fb, width, 0, y);
29279f64916dSGerd Hoffmann             guest_ptr = (uint8_t *)pixman_image_get_data(tmpbuf);
29289f64916dSGerd Hoffmann         } else {
292912b316d4SPeter Lieven             guest_ptr = guest_row0 + y * guest_stride;
29309f64916dSGerd Hoffmann         }
293112b316d4SPeter Lieven         guest_ptr += x * cmp_bytes;
29323e230dd2SCorentin Chary 
293312b316d4SPeter Lieven         for (; x < DIV_ROUND_UP(width, VNC_DIRTY_PIXELS_PER_BIT);
293412b316d4SPeter Lieven              x++, guest_ptr += cmp_bytes, server_ptr += cmp_bytes) {
2935bea60dd7SPeter Lieven             int _cmp_bytes = cmp_bytes;
293612b316d4SPeter Lieven             if (!test_and_clear_bit(x, vd->guest.dirty[y])) {
29373e230dd2SCorentin Chary                 continue;
2938b4c85ddcSPeter Lieven             }
2939eb8934b0SGerd Hoffmann             if ((x + 1) * cmp_bytes > line_bytes) {
2940eb8934b0SGerd Hoffmann                 _cmp_bytes = line_bytes - x * cmp_bytes;
2941bea60dd7SPeter Lieven             }
2942eb8934b0SGerd Hoffmann             assert(_cmp_bytes >= 0);
2943bea60dd7SPeter Lieven             if (memcmp(server_ptr, guest_ptr, _cmp_bytes) == 0) {
29443e230dd2SCorentin Chary                 continue;
2945b4c85ddcSPeter Lieven             }
2946bea60dd7SPeter Lieven             memcpy(server_ptr, guest_ptr, _cmp_bytes);
294712b316d4SPeter Lieven             if (!vd->non_adaptive) {
294812b316d4SPeter Lieven                 vnc_rect_updated(vd, x * VNC_DIRTY_PIXELS_PER_BIT,
294912b316d4SPeter Lieven                                  y, &tv);
295012b316d4SPeter Lieven             }
29513e230dd2SCorentin Chary             QTAILQ_FOREACH(vs, &vd->clients, next) {
295212b316d4SPeter Lieven                 set_bit(x, vs->dirty[y]);
29533e230dd2SCorentin Chary             }
29543e230dd2SCorentin Chary             has_dirty++;
29553e230dd2SCorentin Chary         }
295612b316d4SPeter Lieven 
295712b316d4SPeter Lieven         y++;
29583e230dd2SCorentin Chary     }
29599f64916dSGerd Hoffmann     qemu_pixman_image_unref(tmpbuf);
29603e230dd2SCorentin Chary     return has_dirty;
29613e230dd2SCorentin Chary }
29623e230dd2SCorentin Chary 
29630f7b2864SGerd Hoffmann static void vnc_refresh(DisplayChangeListener *dcl)
29643e230dd2SCorentin Chary {
29650f7b2864SGerd Hoffmann     VncDisplay *vd = container_of(dcl, VncDisplay, dcl);
29663e230dd2SCorentin Chary     VncState *vs, *vn;
29673e230dd2SCorentin Chary     int has_dirty, rects = 0;
29683e230dd2SCorentin Chary 
29699d6b2070SChenLiang     if (QTAILQ_EMPTY(&vd->clients)) {
29709d6b2070SChenLiang         update_displaychangelistener(&vd->dcl, VNC_REFRESH_INTERVAL_MAX);
29719d6b2070SChenLiang         return;
29729d6b2070SChenLiang     }
29739d6b2070SChenLiang 
29741d0d59feSGerd Hoffmann     graphic_hw_update(vd->dcl.con);
29753e230dd2SCorentin Chary 
2976bd023f95SCorentin Chary     if (vnc_trylock_display(vd)) {
29770f7b2864SGerd Hoffmann         update_displaychangelistener(&vd->dcl, VNC_REFRESH_INTERVAL_BASE);
2978bd023f95SCorentin Chary         return;
2979bd023f95SCorentin Chary     }
2980bd023f95SCorentin Chary 
29813e230dd2SCorentin Chary     has_dirty = vnc_refresh_server_surface(vd);
2982bd023f95SCorentin Chary     vnc_unlock_display(vd);
29833e230dd2SCorentin Chary 
29843e230dd2SCorentin Chary     QTAILQ_FOREACH_SAFE(vs, &vd->clients, next, vn) {
298538ee14f4SGerd Hoffmann         rects += vnc_update_client(vs, has_dirty, false);
29863e230dd2SCorentin Chary         /* vs might be free()ed here */
29873e230dd2SCorentin Chary     }
2988bd023f95SCorentin Chary 
29893e230dd2SCorentin Chary     if (has_dirty && rects) {
29900f7b2864SGerd Hoffmann         vd->dcl.update_interval /= 2;
29910f7b2864SGerd Hoffmann         if (vd->dcl.update_interval < VNC_REFRESH_INTERVAL_BASE) {
29920f7b2864SGerd Hoffmann             vd->dcl.update_interval = VNC_REFRESH_INTERVAL_BASE;
29930f7b2864SGerd Hoffmann         }
29943e230dd2SCorentin Chary     } else {
29950f7b2864SGerd Hoffmann         vd->dcl.update_interval += VNC_REFRESH_INTERVAL_INC;
29960f7b2864SGerd Hoffmann         if (vd->dcl.update_interval > VNC_REFRESH_INTERVAL_MAX) {
29970f7b2864SGerd Hoffmann             vd->dcl.update_interval = VNC_REFRESH_INTERVAL_MAX;
29983e230dd2SCorentin Chary         }
29993e230dd2SCorentin Chary     }
30003e230dd2SCorentin Chary }
30013e230dd2SCorentin Chary 
30022c8cf549SMichael Tokarev static void vnc_connect(VncDisplay *vd, int csock,
30032c8cf549SMichael Tokarev                         bool skipauth, bool websocket)
30043e230dd2SCorentin Chary {
30057267c094SAnthony Liguori     VncState *vs = g_malloc0(sizeof(VncState));
30067d964c9dSCorentin Chary     int i;
30077d964c9dSCorentin Chary 
30083e230dd2SCorentin Chary     vs->csock = csock;
3009d616ccc5SGerd Hoffmann     vs->vd = vd;
30107e7e2ebcSDaniel P. Berrange 
30117e7e2ebcSDaniel P. Berrange     if (skipauth) {
30127e7e2ebcSDaniel P. Berrange 	vs->auth = VNC_AUTH_NONE;
30137e7e2ebcSDaniel P. Berrange 	vs->subauth = VNC_AUTH_INVALID;
30147e7e2ebcSDaniel P. Berrange     } else {
3015f9148c8aSDaniel P. Berrange         if (websocket) {
3016f9148c8aSDaniel P. Berrange             vs->auth = vd->ws_auth;
3017f9148c8aSDaniel P. Berrange             vs->subauth = VNC_AUTH_INVALID;
3018f9148c8aSDaniel P. Berrange         } else {
30197e7e2ebcSDaniel P. Berrange             vs->auth = vd->auth;
30207e7e2ebcSDaniel P. Berrange             vs->subauth = vd->subauth;
30217e7e2ebcSDaniel P. Berrange         }
3022f9148c8aSDaniel P. Berrange     }
3023f9148c8aSDaniel P. Berrange     VNC_DEBUG("Client sock=%d ws=%d auth=%d subauth=%d\n",
3024f9148c8aSDaniel P. Berrange               csock, websocket, vs->auth, vs->subauth);
30257e7e2ebcSDaniel P. Berrange 
30267267c094SAnthony Liguori     vs->lossy_rect = g_malloc0(VNC_STAT_ROWS * sizeof (*vs->lossy_rect));
30277d964c9dSCorentin Chary     for (i = 0; i < VNC_STAT_ROWS; ++i) {
30287267c094SAnthony Liguori         vs->lossy_rect[i] = g_malloc0(VNC_STAT_COLS * sizeof (uint8_t));
30297d964c9dSCorentin Chary     }
30303e230dd2SCorentin Chary 
30313e230dd2SCorentin Chary     VNC_DEBUG("New client on socket %d\n", csock);
30320f7b2864SGerd Hoffmann     update_displaychangelistener(&vd->dcl, VNC_REFRESH_INTERVAL_BASE);
3033f9e8caccSStefan Hajnoczi     qemu_set_nonblock(vs->csock);
30347536ee4bSTim Hardeck     if (websocket) {
30357536ee4bSTim Hardeck         vs->websocket = 1;
30360057a0d5STim Hardeck #ifdef CONFIG_VNC_TLS
3037f9148c8aSDaniel P. Berrange         if (vd->ws_tls) {
303882e1cc4bSFam Zheng             qemu_set_fd_handler(vs->csock, vncws_tls_handshake_io, NULL, vs);
30390057a0d5STim Hardeck         } else
30400057a0d5STim Hardeck #endif /* CONFIG_VNC_TLS */
30410057a0d5STim Hardeck         {
304282e1cc4bSFam Zheng             qemu_set_fd_handler(vs->csock, vncws_handshake_read, NULL, vs);
30430057a0d5STim Hardeck         }
30447536ee4bSTim Hardeck     } else
30457536ee4bSTim Hardeck     {
304682e1cc4bSFam Zheng         qemu_set_fd_handler(vs->csock, vnc_client_read, NULL, vs);
30477536ee4bSTim Hardeck     }
30483e230dd2SCorentin Chary 
30493e230dd2SCorentin Chary     vnc_client_cache_addr(vs);
3050fb6ba0d5SWenchao Xia     vnc_qmp_event(vs, QAPI_EVENT_VNC_CONNECTED);
30518cf36489SGerd Hoffmann     vnc_set_share_mode(vs, VNC_SHARE_MODE_CONNECTING);
30523e230dd2SCorentin Chary 
30538e9b0d24SDaniel P. Berrange     if (!vs->websocket) {
30547536ee4bSTim Hardeck         vnc_init_state(vs);
30557536ee4bSTim Hardeck     }
3056e5f34cddSGerd Hoffmann 
3057e5f34cddSGerd Hoffmann     if (vd->num_connecting > vd->connections_limit) {
3058e5f34cddSGerd Hoffmann         QTAILQ_FOREACH(vs, &vd->clients, next) {
3059e5f34cddSGerd Hoffmann             if (vs->share_mode == VNC_SHARE_MODE_CONNECTING) {
3060e5f34cddSGerd Hoffmann                 vnc_disconnect_start(vs);
3061e5f34cddSGerd Hoffmann                 return;
3062e5f34cddSGerd Hoffmann             }
3063e5f34cddSGerd Hoffmann         }
3064e5f34cddSGerd Hoffmann     }
30657536ee4bSTim Hardeck }
30667536ee4bSTim Hardeck 
30677536ee4bSTim Hardeck void vnc_init_state(VncState *vs)
30687536ee4bSTim Hardeck {
30696fd8e79aSTim Hardeck     vs->initialized = true;
30707536ee4bSTim Hardeck     VncDisplay *vd = vs->vd;
30717536ee4bSTim Hardeck 
30723e230dd2SCorentin Chary     vs->last_x = -1;
30733e230dd2SCorentin Chary     vs->last_y = -1;
30743e230dd2SCorentin Chary 
30753e230dd2SCorentin Chary     vs->as.freq = 44100;
30763e230dd2SCorentin Chary     vs->as.nchannels = 2;
30773e230dd2SCorentin Chary     vs->as.fmt = AUD_FMT_S16;
30783e230dd2SCorentin Chary     vs->as.endianness = 0;
30793e230dd2SCorentin Chary 
3080bd023f95SCorentin Chary     qemu_mutex_init(&vs->output_mutex);
3081175b2a6eSCorentin Chary     vs->bh = qemu_bh_new(vnc_jobs_bh, vs);
3082bd023f95SCorentin Chary 
3083e5f34cddSGerd Hoffmann     QTAILQ_INSERT_TAIL(&vd->clients, vs, next);
30843e230dd2SCorentin Chary 
30851d0d59feSGerd Hoffmann     graphic_hw_update(vd->dcl.con);
30863e230dd2SCorentin Chary 
30873e230dd2SCorentin Chary     vnc_write(vs, "RFB 003.008\n", 12);
30883e230dd2SCorentin Chary     vnc_flush(vs);
30893e230dd2SCorentin Chary     vnc_read_when(vs, protocol_version, 12);
30903e230dd2SCorentin Chary     reset_keys(vs);
30913e230dd2SCorentin Chary     if (vs->vd->lock_key_sync)
30923e230dd2SCorentin Chary         vs->led = qemu_add_led_event_handler(kbd_leds, vs);
30933e230dd2SCorentin Chary 
30943e230dd2SCorentin Chary     vs->mouse_mode_notifier.notify = check_pointer_type_change;
30953e230dd2SCorentin Chary     qemu_add_mouse_mode_change_notifier(&vs->mouse_mode_notifier);
30963e230dd2SCorentin Chary 
30973e230dd2SCorentin Chary     /* vs might be free()ed here */
30983e230dd2SCorentin Chary }
30993e230dd2SCorentin Chary 
31007536ee4bSTim Hardeck static void vnc_listen_read(void *opaque, bool websocket)
31013e230dd2SCorentin Chary {
31023e230dd2SCorentin Chary     VncDisplay *vs = opaque;
31033e230dd2SCorentin Chary     struct sockaddr_in addr;
31043e230dd2SCorentin Chary     socklen_t addrlen = sizeof(addr);
31057536ee4bSTim Hardeck     int csock;
31063e230dd2SCorentin Chary 
31073e230dd2SCorentin Chary     /* Catch-up */
31081d0d59feSGerd Hoffmann     graphic_hw_update(vs->dcl.con);
31097536ee4bSTim Hardeck     if (websocket) {
31107536ee4bSTim Hardeck         csock = qemu_accept(vs->lwebsock, (struct sockaddr *)&addr, &addrlen);
31118e9b0d24SDaniel P. Berrange     } else {
31127536ee4bSTim Hardeck         csock = qemu_accept(vs->lsock, (struct sockaddr *)&addr, &addrlen);
31137536ee4bSTim Hardeck     }
31143e230dd2SCorentin Chary 
31153e230dd2SCorentin Chary     if (csock != -1) {
311686152436SPeter Lieven         socket_set_nodelay(csock);
31172c8cf549SMichael Tokarev         vnc_connect(vs, csock, false, websocket);
31183e230dd2SCorentin Chary     }
31193e230dd2SCorentin Chary }
31203e230dd2SCorentin Chary 
31217536ee4bSTim Hardeck static void vnc_listen_regular_read(void *opaque)
31227536ee4bSTim Hardeck {
31232c8cf549SMichael Tokarev     vnc_listen_read(opaque, false);
31247536ee4bSTim Hardeck }
31257536ee4bSTim Hardeck 
31267536ee4bSTim Hardeck static void vnc_listen_websocket_read(void *opaque)
31277536ee4bSTim Hardeck {
31282c8cf549SMichael Tokarev     vnc_listen_read(opaque, true);
31297536ee4bSTim Hardeck }
31307536ee4bSTim Hardeck 
31317c20b4a3SGerd Hoffmann static const DisplayChangeListenerOps dcl_ops = {
31327c20b4a3SGerd Hoffmann     .dpy_name             = "vnc",
31330f7b2864SGerd Hoffmann     .dpy_refresh          = vnc_refresh,
31347c20b4a3SGerd Hoffmann     .dpy_gfx_copy         = vnc_dpy_copy,
31357c20b4a3SGerd Hoffmann     .dpy_gfx_update       = vnc_dpy_update,
3136c12aeb86SGerd Hoffmann     .dpy_gfx_switch       = vnc_dpy_switch,
313734da30afSBenjamin Herrenschmidt     .dpy_gfx_check_format = qemu_pixman_check_format,
31387c20b4a3SGerd Hoffmann     .dpy_mouse_set        = vnc_mouse_set,
31397c20b4a3SGerd Hoffmann     .dpy_cursor_define    = vnc_dpy_cursor_define,
31407c20b4a3SGerd Hoffmann };
31417c20b4a3SGerd Hoffmann 
314214f7143eSGerd Hoffmann void vnc_display_init(const char *id)
31433e230dd2SCorentin Chary {
31444db14629SGerd Hoffmann     VncDisplay *vs;
31454db14629SGerd Hoffmann 
31464db14629SGerd Hoffmann     if (vnc_display_find(id) != NULL) {
31474db14629SGerd Hoffmann         return;
31484db14629SGerd Hoffmann     }
31494db14629SGerd Hoffmann     vs = g_malloc0(sizeof(*vs));
31503e230dd2SCorentin Chary 
315114f7143eSGerd Hoffmann     vs->id = strdup(id);
3152d616ccc5SGerd Hoffmann     QTAILQ_INSERT_TAIL(&vnc_displays, vs, next);
31533e230dd2SCorentin Chary 
31543e230dd2SCorentin Chary     vs->lsock = -1;
31557536ee4bSTim Hardeck     vs->lwebsock = -1;
31563e230dd2SCorentin Chary 
31573e230dd2SCorentin Chary     QTAILQ_INIT(&vs->clients);
31583c9405a0SGerd Hoffmann     vs->expires = TIME_MAX;
31593e230dd2SCorentin Chary 
316040066175SGerd Hoffmann     if (keyboard_layout) {
316140066175SGerd Hoffmann         trace_vnc_key_map_init(keyboard_layout);
31623e230dd2SCorentin Chary         vs->kbd_layout = init_keyboard_layout(name2keysym, keyboard_layout);
316340066175SGerd Hoffmann     } else {
31643e230dd2SCorentin Chary         vs->kbd_layout = init_keyboard_layout(name2keysym, "en-us");
316540066175SGerd Hoffmann     }
31663e230dd2SCorentin Chary 
31673e230dd2SCorentin Chary     if (!vs->kbd_layout)
31683e230dd2SCorentin Chary         exit(1);
31693e230dd2SCorentin Chary 
3170bd023f95SCorentin Chary     qemu_mutex_init(&vs->mutex);
3171bd023f95SCorentin Chary     vnc_start_worker_thread();
3172bd023f95SCorentin Chary 
317321ef45d7SGerd Hoffmann     vs->dcl.ops = &dcl_ops;
31745209089fSGerd Hoffmann     register_displaychangelistener(&vs->dcl);
31753e230dd2SCorentin Chary }
31763e230dd2SCorentin Chary 
31773e230dd2SCorentin Chary 
317814f7143eSGerd Hoffmann static void vnc_display_close(VncDisplay *vs)
31793e230dd2SCorentin Chary {
31803e230dd2SCorentin Chary     if (!vs)
31813e230dd2SCorentin Chary         return;
3182bf7aa45eSGerd Hoffmann     vs->enabled = false;
3183bf7aa45eSGerd Hoffmann     vs->is_unix = false;
31843e230dd2SCorentin Chary     if (vs->lsock != -1) {
318582e1cc4bSFam Zheng         qemu_set_fd_handler(vs->lsock, NULL, NULL, NULL);
31863e230dd2SCorentin Chary         close(vs->lsock);
31873e230dd2SCorentin Chary         vs->lsock = -1;
31883e230dd2SCorentin Chary     }
3189bf7aa45eSGerd Hoffmann     vs->ws_enabled = false;
31907536ee4bSTim Hardeck     if (vs->lwebsock != -1) {
319182e1cc4bSFam Zheng         qemu_set_fd_handler(vs->lwebsock, NULL, NULL, NULL);
31927536ee4bSTim Hardeck         close(vs->lwebsock);
31937536ee4bSTim Hardeck         vs->lwebsock = -1;
31947536ee4bSTim Hardeck     }
31953e230dd2SCorentin Chary     vs->auth = VNC_AUTH_INVALID;
31963e230dd2SCorentin Chary     vs->subauth = VNC_AUTH_INVALID;
3197d169f04bSDaniel P. Berrange #ifdef CONFIG_VNC_TLS
31983e230dd2SCorentin Chary     vs->tls.x509verify = 0;
31993e230dd2SCorentin Chary #endif
32003e230dd2SCorentin Chary }
32013e230dd2SCorentin Chary 
320214f7143eSGerd Hoffmann int vnc_display_password(const char *id, const char *password)
32031cd20f8bSAnthony Liguori {
320414f7143eSGerd Hoffmann     VncDisplay *vs = vnc_display_find(id);
32051cd20f8bSAnthony Liguori 
32061cd20f8bSAnthony Liguori     if (!vs) {
3207a6aa9d3eSLuiz Capitulino         return -EINVAL;
32081cd20f8bSAnthony Liguori     }
3209cf864569SGerd Hoffmann     if (vs->auth == VNC_AUTH_NONE) {
3210cf864569SGerd Hoffmann         error_printf_unless_qmp("If you want use passwords please enable "
3211cf864569SGerd Hoffmann                                 "password auth using '-vnc ${dpy},password'.");
3212cf864569SGerd Hoffmann         return -EINVAL;
32131cd20f8bSAnthony Liguori     }
32141cd20f8bSAnthony Liguori 
32157267c094SAnthony Liguori     g_free(vs->password);
32167267c094SAnthony Liguori     vs->password = g_strdup(password);
3217a6aa9d3eSLuiz Capitulino 
3218a6aa9d3eSLuiz Capitulino     return 0;
32193e230dd2SCorentin Chary }
32203e230dd2SCorentin Chary 
322114f7143eSGerd Hoffmann int vnc_display_pw_expire(const char *id, time_t expires)
32223c9405a0SGerd Hoffmann {
322314f7143eSGerd Hoffmann     VncDisplay *vs = vnc_display_find(id);
32243c9405a0SGerd Hoffmann 
32251643f2b2SGerd Hoffmann     if (!vs) {
32261643f2b2SGerd Hoffmann         return -EINVAL;
32271643f2b2SGerd Hoffmann     }
32281643f2b2SGerd Hoffmann 
32293c9405a0SGerd Hoffmann     vs->expires = expires;
32303c9405a0SGerd Hoffmann     return 0;
32313c9405a0SGerd Hoffmann }
32323c9405a0SGerd Hoffmann 
323314f7143eSGerd Hoffmann char *vnc_display_local_addr(const char *id)
32343e230dd2SCorentin Chary {
323514f7143eSGerd Hoffmann     VncDisplay *vs = vnc_display_find(id);
32363e230dd2SCorentin Chary 
32379e0ff75eSGerd Hoffmann     assert(vs);
32383e230dd2SCorentin Chary     return vnc_socket_local_addr("%s:%s", vs->lsock);
32393e230dd2SCorentin Chary }
32403e230dd2SCorentin Chary 
32414db14629SGerd Hoffmann static QemuOptsList qemu_vnc_opts = {
32424db14629SGerd Hoffmann     .name = "vnc",
32434db14629SGerd Hoffmann     .head = QTAILQ_HEAD_INITIALIZER(qemu_vnc_opts.head),
32444db14629SGerd Hoffmann     .implied_opt_name = "vnc",
32454db14629SGerd Hoffmann     .desc = {
32464db14629SGerd Hoffmann         {
32474db14629SGerd Hoffmann             .name = "vnc",
32484db14629SGerd Hoffmann             .type = QEMU_OPT_STRING,
32494db14629SGerd Hoffmann         },{
32504db14629SGerd Hoffmann             .name = "websocket",
32514db14629SGerd Hoffmann             .type = QEMU_OPT_STRING,
32524db14629SGerd Hoffmann         },{
32534db14629SGerd Hoffmann             .name = "x509",
32544db14629SGerd Hoffmann             .type = QEMU_OPT_STRING,
32554db14629SGerd Hoffmann         },{
32564db14629SGerd Hoffmann             .name = "share",
32574db14629SGerd Hoffmann             .type = QEMU_OPT_STRING,
32584db14629SGerd Hoffmann         },{
32591d0d59feSGerd Hoffmann             .name = "display",
32601d0d59feSGerd Hoffmann             .type = QEMU_OPT_STRING,
32611d0d59feSGerd Hoffmann         },{
32621d0d59feSGerd Hoffmann             .name = "head",
32631d0d59feSGerd Hoffmann             .type = QEMU_OPT_NUMBER,
32641d0d59feSGerd Hoffmann         },{
3265e5f34cddSGerd Hoffmann             .name = "connections",
3266e5f34cddSGerd Hoffmann             .type = QEMU_OPT_NUMBER,
3267e5f34cddSGerd Hoffmann         },{
326888428b7aSGonglei             .name = "to",
326988428b7aSGonglei             .type = QEMU_OPT_NUMBER,
327088428b7aSGonglei         },{
327188428b7aSGonglei             .name = "ipv4",
327288428b7aSGonglei             .type = QEMU_OPT_BOOL,
327388428b7aSGonglei         },{
327488428b7aSGonglei             .name = "ipv6",
327588428b7aSGonglei             .type = QEMU_OPT_BOOL,
327688428b7aSGonglei         },{
32774db14629SGerd Hoffmann             .name = "password",
32784db14629SGerd Hoffmann             .type = QEMU_OPT_BOOL,
32794db14629SGerd Hoffmann         },{
32804db14629SGerd Hoffmann             .name = "reverse",
32814db14629SGerd Hoffmann             .type = QEMU_OPT_BOOL,
32824db14629SGerd Hoffmann         },{
32834db14629SGerd Hoffmann             .name = "lock-key-sync",
32844db14629SGerd Hoffmann             .type = QEMU_OPT_BOOL,
32854db14629SGerd Hoffmann         },{
32864db14629SGerd Hoffmann             .name = "sasl",
32874db14629SGerd Hoffmann             .type = QEMU_OPT_BOOL,
32884db14629SGerd Hoffmann         },{
32894db14629SGerd Hoffmann             .name = "tls",
32904db14629SGerd Hoffmann             .type = QEMU_OPT_BOOL,
32914db14629SGerd Hoffmann         },{
32924db14629SGerd Hoffmann             .name = "x509verify",
32938c7d0645SDaniel P. Berrange             .type = QEMU_OPT_STRING,
32944db14629SGerd Hoffmann         },{
32954db14629SGerd Hoffmann             .name = "acl",
32964db14629SGerd Hoffmann             .type = QEMU_OPT_BOOL,
32974db14629SGerd Hoffmann         },{
32984db14629SGerd Hoffmann             .name = "lossy",
32994db14629SGerd Hoffmann             .type = QEMU_OPT_BOOL,
33004db14629SGerd Hoffmann         },{
33014db14629SGerd Hoffmann             .name = "non-adaptive",
33024db14629SGerd Hoffmann             .type = QEMU_OPT_BOOL,
33034db14629SGerd Hoffmann         },
33044db14629SGerd Hoffmann         { /* end of list */ }
33054db14629SGerd Hoffmann     },
33064db14629SGerd Hoffmann };
33074db14629SGerd Hoffmann 
33080dd72e15SDaniel P. Berrange 
33090dd72e15SDaniel P. Berrange static void
33100dd72e15SDaniel P. Berrange vnc_display_setup_auth(VncDisplay *vs,
33110dd72e15SDaniel P. Berrange                        bool password,
33120dd72e15SDaniel P. Berrange                        bool sasl,
33130dd72e15SDaniel P. Berrange                        bool tls,
3314f9148c8aSDaniel P. Berrange                        bool x509,
3315f9148c8aSDaniel P. Berrange                        bool websocket)
33160dd72e15SDaniel P. Berrange {
33170dd72e15SDaniel P. Berrange     /*
33180dd72e15SDaniel P. Berrange      * We have a choice of 3 authentication options
33190dd72e15SDaniel P. Berrange      *
33200dd72e15SDaniel P. Berrange      *   1. none
33210dd72e15SDaniel P. Berrange      *   2. vnc
33220dd72e15SDaniel P. Berrange      *   3. sasl
33230dd72e15SDaniel P. Berrange      *
33240dd72e15SDaniel P. Berrange      * The channel can be run in 2 modes
33250dd72e15SDaniel P. Berrange      *
33260dd72e15SDaniel P. Berrange      *   1. clear
33270dd72e15SDaniel P. Berrange      *   2. tls
33280dd72e15SDaniel P. Berrange      *
33290dd72e15SDaniel P. Berrange      * And TLS can use 2 types of credentials
33300dd72e15SDaniel P. Berrange      *
33310dd72e15SDaniel P. Berrange      *   1. anon
33320dd72e15SDaniel P. Berrange      *   2. x509
33330dd72e15SDaniel P. Berrange      *
33340dd72e15SDaniel P. Berrange      * We thus have 9 possible logical combinations
33350dd72e15SDaniel P. Berrange      *
33360dd72e15SDaniel P. Berrange      *   1. clear + none
33370dd72e15SDaniel P. Berrange      *   2. clear + vnc
33380dd72e15SDaniel P. Berrange      *   3. clear + sasl
33390dd72e15SDaniel P. Berrange      *   4. tls + anon + none
33400dd72e15SDaniel P. Berrange      *   5. tls + anon + vnc
33410dd72e15SDaniel P. Berrange      *   6. tls + anon + sasl
33420dd72e15SDaniel P. Berrange      *   7. tls + x509 + none
33430dd72e15SDaniel P. Berrange      *   8. tls + x509 + vnc
33440dd72e15SDaniel P. Berrange      *   9. tls + x509 + sasl
33450dd72e15SDaniel P. Berrange      *
33460dd72e15SDaniel P. Berrange      * These need to be mapped into the VNC auth schemes
33470dd72e15SDaniel P. Berrange      * in an appropriate manner. In regular VNC, all the
33480dd72e15SDaniel P. Berrange      * TLS options get mapped into VNC_AUTH_VENCRYPT
33490dd72e15SDaniel P. Berrange      * sub-auth types.
3350f9148c8aSDaniel P. Berrange      *
3351f9148c8aSDaniel P. Berrange      * In websockets, the https:// protocol already provides
3352f9148c8aSDaniel P. Berrange      * TLS support, so there is no need to make use of the
3353f9148c8aSDaniel P. Berrange      * VeNCrypt extension. Furthermore, websockets browser
3354f9148c8aSDaniel P. Berrange      * clients could not use VeNCrypt even if they wanted to,
3355f9148c8aSDaniel P. Berrange      * as they cannot control when the TLS handshake takes
3356f9148c8aSDaniel P. Berrange      * place. Thus there is no option but to rely on https://,
3357f9148c8aSDaniel P. Berrange      * meaning combinations 4->6 and 7->9 will be mapped to
3358f9148c8aSDaniel P. Berrange      * VNC auth schemes in the same way as combos 1->3.
3359f9148c8aSDaniel P. Berrange      *
3360f9148c8aSDaniel P. Berrange      * Regardless of fact that we have a different mapping to
3361f9148c8aSDaniel P. Berrange      * VNC auth mechs for plain VNC vs websockets VNC, the end
3362f9148c8aSDaniel P. Berrange      * result has the same security characteristics.
33630dd72e15SDaniel P. Berrange      */
33640dd72e15SDaniel P. Berrange     if (password) {
33650dd72e15SDaniel P. Berrange         if (tls) {
33660dd72e15SDaniel P. Berrange             vs->auth = VNC_AUTH_VENCRYPT;
3367f9148c8aSDaniel P. Berrange             if (websocket) {
3368f9148c8aSDaniel P. Berrange                 vs->ws_tls = true;
3369f9148c8aSDaniel P. Berrange             }
33700dd72e15SDaniel P. Berrange             if (x509) {
33710dd72e15SDaniel P. Berrange                 VNC_DEBUG("Initializing VNC server with x509 password auth\n");
33720dd72e15SDaniel P. Berrange                 vs->subauth = VNC_AUTH_VENCRYPT_X509VNC;
33730dd72e15SDaniel P. Berrange             } else {
33740dd72e15SDaniel P. Berrange                 VNC_DEBUG("Initializing VNC server with TLS password auth\n");
33750dd72e15SDaniel P. Berrange                 vs->subauth = VNC_AUTH_VENCRYPT_TLSVNC;
33760dd72e15SDaniel P. Berrange             }
33770dd72e15SDaniel P. Berrange         } else {
33780dd72e15SDaniel P. Berrange             VNC_DEBUG("Initializing VNC server with password auth\n");
33790dd72e15SDaniel P. Berrange             vs->auth = VNC_AUTH_VNC;
33800dd72e15SDaniel P. Berrange             vs->subauth = VNC_AUTH_INVALID;
33810dd72e15SDaniel P. Berrange         }
3382f9148c8aSDaniel P. Berrange         if (websocket) {
3383f9148c8aSDaniel P. Berrange             vs->ws_auth = VNC_AUTH_VNC;
3384f9148c8aSDaniel P. Berrange         } else {
3385f9148c8aSDaniel P. Berrange             vs->ws_auth = VNC_AUTH_INVALID;
3386f9148c8aSDaniel P. Berrange         }
33870dd72e15SDaniel P. Berrange     } else if (sasl) {
33880dd72e15SDaniel P. Berrange         if (tls) {
33890dd72e15SDaniel P. Berrange             vs->auth = VNC_AUTH_VENCRYPT;
3390f9148c8aSDaniel P. Berrange             if (websocket) {
3391f9148c8aSDaniel P. Berrange                 vs->ws_tls = true;
3392f9148c8aSDaniel P. Berrange             }
33930dd72e15SDaniel P. Berrange             if (x509) {
33940dd72e15SDaniel P. Berrange                 VNC_DEBUG("Initializing VNC server with x509 SASL auth\n");
33950dd72e15SDaniel P. Berrange                 vs->subauth = VNC_AUTH_VENCRYPT_X509SASL;
33960dd72e15SDaniel P. Berrange             } else {
33970dd72e15SDaniel P. Berrange                 VNC_DEBUG("Initializing VNC server with TLS SASL auth\n");
33980dd72e15SDaniel P. Berrange                 vs->subauth = VNC_AUTH_VENCRYPT_TLSSASL;
33990dd72e15SDaniel P. Berrange             }
34000dd72e15SDaniel P. Berrange         } else {
34010dd72e15SDaniel P. Berrange             VNC_DEBUG("Initializing VNC server with SASL auth\n");
34020dd72e15SDaniel P. Berrange             vs->auth = VNC_AUTH_SASL;
34030dd72e15SDaniel P. Berrange             vs->subauth = VNC_AUTH_INVALID;
34040dd72e15SDaniel P. Berrange         }
3405f9148c8aSDaniel P. Berrange         if (websocket) {
3406f9148c8aSDaniel P. Berrange             vs->ws_auth = VNC_AUTH_SASL;
3407f9148c8aSDaniel P. Berrange         } else {
3408f9148c8aSDaniel P. Berrange             vs->ws_auth = VNC_AUTH_INVALID;
3409f9148c8aSDaniel P. Berrange         }
34100dd72e15SDaniel P. Berrange     } else {
34110dd72e15SDaniel P. Berrange         if (tls) {
34120dd72e15SDaniel P. Berrange             vs->auth = VNC_AUTH_VENCRYPT;
3413f9148c8aSDaniel P. Berrange             if (websocket) {
3414f9148c8aSDaniel P. Berrange                 vs->ws_tls = true;
3415f9148c8aSDaniel P. Berrange             }
34160dd72e15SDaniel P. Berrange             if (x509) {
34170dd72e15SDaniel P. Berrange                 VNC_DEBUG("Initializing VNC server with x509 no auth\n");
34180dd72e15SDaniel P. Berrange                 vs->subauth = VNC_AUTH_VENCRYPT_X509NONE;
34190dd72e15SDaniel P. Berrange             } else {
34200dd72e15SDaniel P. Berrange                 VNC_DEBUG("Initializing VNC server with TLS no auth\n");
34210dd72e15SDaniel P. Berrange                 vs->subauth = VNC_AUTH_VENCRYPT_TLSNONE;
34220dd72e15SDaniel P. Berrange             }
34230dd72e15SDaniel P. Berrange         } else {
34240dd72e15SDaniel P. Berrange             VNC_DEBUG("Initializing VNC server with no auth\n");
34250dd72e15SDaniel P. Berrange             vs->auth = VNC_AUTH_NONE;
34260dd72e15SDaniel P. Berrange             vs->subauth = VNC_AUTH_INVALID;
34270dd72e15SDaniel P. Berrange         }
3428f9148c8aSDaniel P. Berrange         if (websocket) {
3429f9148c8aSDaniel P. Berrange             vs->ws_auth = VNC_AUTH_NONE;
3430f9148c8aSDaniel P. Berrange         } else {
3431f9148c8aSDaniel P. Berrange             vs->ws_auth = VNC_AUTH_INVALID;
3432f9148c8aSDaniel P. Berrange         }
34330dd72e15SDaniel P. Berrange     }
34340dd72e15SDaniel P. Berrange }
34350dd72e15SDaniel P. Berrange 
34364db14629SGerd Hoffmann void vnc_display_open(const char *id, Error **errp)
34373e230dd2SCorentin Chary {
343814f7143eSGerd Hoffmann     VncDisplay *vs = vnc_display_find(id);
34394db14629SGerd Hoffmann     QemuOpts *opts = qemu_opts_find(&qemu_vnc_opts, id);
3440e5560329SGerd Hoffmann     QemuOpts *sopts, *wsopts;
3441e2a11d9dSGonglei     const char *share, *device_id;
34421d0d59feSGerd Hoffmann     QemuConsole *con;
3443a2c72de0SGonglei     bool password = false;
3444a2c72de0SGonglei     bool reverse = false;
3445e2a11d9dSGonglei     const char *vnc;
3446e2a11d9dSGonglei     const char *has_to;
3447e5560329SGerd Hoffmann     char *h;
3448e2a11d9dSGonglei     bool has_ipv4 = false;
3449e2a11d9dSGonglei     bool has_ipv6 = false;
34504478aa76SGerd Hoffmann     const char *websocket;
3451a2c72de0SGonglei     bool tls = false, x509 = false;
3452d169f04bSDaniel P. Berrange #ifdef CONFIG_VNC_TLS
34534db14629SGerd Hoffmann     const char *path;
34543e230dd2SCorentin Chary #endif
3455a2c72de0SGonglei     bool sasl = false;
3456d169f04bSDaniel P. Berrange #ifdef CONFIG_VNC_SASL
34573e230dd2SCorentin Chary     int saslErr;
34583e230dd2SCorentin Chary #endif
34592ded6ad7SBlue Swirl #if defined(CONFIG_VNC_TLS) || defined(CONFIG_VNC_SASL)
34603e230dd2SCorentin Chary     int acl = 0;
34612ded6ad7SBlue Swirl #endif
34623e230dd2SCorentin Chary     int lock_key_sync = 1;
34633e230dd2SCorentin Chary 
3464d616ccc5SGerd Hoffmann     if (!vs) {
34652d55f0e8SPaolo Bonzini         error_setg(errp, "VNC display not active");
34662d55f0e8SPaolo Bonzini         return;
34672d55f0e8SPaolo Bonzini     }
346814f7143eSGerd Hoffmann     vnc_display_close(vs);
34694db14629SGerd Hoffmann 
34704db14629SGerd Hoffmann     if (!opts) {
34712d55f0e8SPaolo Bonzini         return;
34724db14629SGerd Hoffmann     }
3473e2a11d9dSGonglei     vnc = qemu_opt_get(opts, "vnc");
3474e2a11d9dSGonglei     if (!vnc || strcmp(vnc, "none") == 0) {
34754db14629SGerd Hoffmann         return;
34764db14629SGerd Hoffmann     }
3477e2a11d9dSGonglei 
3478e5560329SGerd Hoffmann     sopts = qemu_opts_create(&socket_optslist, NULL, 0, &error_abort);
3479e5560329SGerd Hoffmann     wsopts = qemu_opts_create(&socket_optslist, NULL, 0, &error_abort);
3480e5560329SGerd Hoffmann 
3481e5560329SGerd Hoffmann     h = strrchr(vnc, ':');
3482e5560329SGerd Hoffmann     if (h) {
3483274c3b52SJán Tomko         char *host;
3484274c3b52SJán Tomko         size_t hlen = h - vnc;
3485274c3b52SJán Tomko 
3486274c3b52SJán Tomko         if (vnc[0] == '[' && vnc[hlen - 1] == ']') {
3487274c3b52SJán Tomko             host = g_strndup(vnc + 1, hlen - 2);
3488274c3b52SJán Tomko         } else {
3489274c3b52SJán Tomko             host = g_strndup(vnc, hlen);
3490274c3b52SJán Tomko         }
3491e5560329SGerd Hoffmann         qemu_opt_set(sopts, "host", host, &error_abort);
3492e5560329SGerd Hoffmann         qemu_opt_set(wsopts, "host", host, &error_abort);
3493e5560329SGerd Hoffmann         qemu_opt_set(sopts, "port", h+1, &error_abort);
3494e5560329SGerd Hoffmann         g_free(host);
3495e5560329SGerd Hoffmann     } else {
3496e5560329SGerd Hoffmann         error_setg(errp, "no vnc port specified");
3497e5560329SGerd Hoffmann         goto fail;
3498e2a11d9dSGonglei     }
3499e5560329SGerd Hoffmann 
3500e5560329SGerd Hoffmann     has_to = qemu_opt_get(opts, "to");
3501e2a11d9dSGonglei     has_ipv4 = qemu_opt_get_bool(opts, "ipv4", false);
3502e2a11d9dSGonglei     has_ipv6 = qemu_opt_get_bool(opts, "ipv6", false);
3503e5560329SGerd Hoffmann     if (has_to) {
3504e5560329SGerd Hoffmann         qemu_opt_set(sopts, "to", has_to, &error_abort);
3505e5560329SGerd Hoffmann         qemu_opt_set(wsopts, "to", has_to, &error_abort);
3506e5560329SGerd Hoffmann     }
3507e5560329SGerd Hoffmann     if (has_ipv4) {
3508e5560329SGerd Hoffmann         qemu_opt_set(sopts, "ipv4", "on", &error_abort);
3509e5560329SGerd Hoffmann         qemu_opt_set(wsopts, "ipv4", "on", &error_abort);
3510e5560329SGerd Hoffmann     }
3511e5560329SGerd Hoffmann     if (has_ipv6) {
3512e5560329SGerd Hoffmann         qemu_opt_set(sopts, "ipv6", "on", &error_abort);
3513e5560329SGerd Hoffmann         qemu_opt_set(wsopts, "ipv6", "on", &error_abort);
3514e5560329SGerd Hoffmann     }
35153e230dd2SCorentin Chary 
35164db14629SGerd Hoffmann     password = qemu_opt_get_bool(opts, "password", false);
3517800567a6SDaniel P. Berrange     if (password) {
3518800567a6SDaniel P. Berrange         if (fips_get_state()) {
35192d55f0e8SPaolo Bonzini             error_setg(errp,
35200f66998fSPaul Moore                        "VNC password auth disabled due to FIPS mode, "
35210f66998fSPaul Moore                        "consider using the VeNCrypt or SASL authentication "
35222d55f0e8SPaolo Bonzini                        "methods as an alternative");
35231ce52c78SPaolo Bonzini             goto fail;
35240f66998fSPaul Moore         }
3525800567a6SDaniel P. Berrange         if (!qcrypto_cipher_supports(
3526800567a6SDaniel P. Berrange                 QCRYPTO_CIPHER_ALG_DES_RFB)) {
3527800567a6SDaniel P. Berrange             error_setg(errp,
3528800567a6SDaniel P. Berrange                        "Cipher backend does not support DES RFB algorithm");
3529800567a6SDaniel P. Berrange             goto fail;
3530800567a6SDaniel P. Berrange         }
3531800567a6SDaniel P. Berrange     }
35324db14629SGerd Hoffmann 
35334db14629SGerd Hoffmann     reverse = qemu_opt_get_bool(opts, "reverse", false);
35344db14629SGerd Hoffmann     lock_key_sync = qemu_opt_get_bool(opts, "lock-key-sync", true);
35354db14629SGerd Hoffmann     sasl = qemu_opt_get_bool(opts, "sasl", false);
3536d169f04bSDaniel P. Berrange #ifndef CONFIG_VNC_SASL
3537d169f04bSDaniel P. Berrange     if (sasl) {
3538d169f04bSDaniel P. Berrange         error_setg(errp, "VNC SASL auth requires cyrus-sasl support");
3539d169f04bSDaniel P. Berrange         goto fail;
3540d169f04bSDaniel P. Berrange     }
3541d169f04bSDaniel P. Berrange #endif /* CONFIG_VNC_SASL */
35424db14629SGerd Hoffmann     tls  = qemu_opt_get_bool(opts, "tls", false);
3543d169f04bSDaniel P. Berrange #ifdef CONFIG_VNC_TLS
35444db14629SGerd Hoffmann     path = qemu_opt_get(opts, "x509");
35458c7d0645SDaniel P. Berrange     if (!path) {
35468c7d0645SDaniel P. Berrange         path = qemu_opt_get(opts, "x509verify");
35478c7d0645SDaniel P. Berrange         if (path) {
35488c7d0645SDaniel P. Berrange             vs->tls.x509verify = true;
35498c7d0645SDaniel P. Berrange         }
35508c7d0645SDaniel P. Berrange     }
35514db14629SGerd Hoffmann     if (path) {
3552a2c72de0SGonglei         x509 = true;
35533e230dd2SCorentin Chary         if (vnc_tls_set_x509_creds_dir(vs, path) < 0) {
35544db14629SGerd Hoffmann             error_setg(errp, "Failed to find x509 certificates/keys in %s",
35554db14629SGerd Hoffmann                        path);
35561ce52c78SPaolo Bonzini             goto fail;
35573e230dd2SCorentin Chary         }
35583e230dd2SCorentin Chary     }
3559d169f04bSDaniel P. Berrange #else /* ! CONFIG_VNC_TLS */
3560d169f04bSDaniel P. Berrange     if (tls) {
3561d169f04bSDaniel P. Berrange         error_setg(errp, "VNC TLS auth requires gnutls support");
3562d169f04bSDaniel P. Berrange         goto fail;
3563d169f04bSDaniel P. Berrange     }
3564d169f04bSDaniel P. Berrange #endif /* ! CONFIG_VNC_TLS */
35652ded6ad7SBlue Swirl #if defined(CONFIG_VNC_TLS) || defined(CONFIG_VNC_SASL)
35664db14629SGerd Hoffmann     acl = qemu_opt_get_bool(opts, "acl", false);
35672ded6ad7SBlue Swirl #endif
35684db14629SGerd Hoffmann 
35694db14629SGerd Hoffmann     share = qemu_opt_get(opts, "share");
35704db14629SGerd Hoffmann     if (share) {
35714db14629SGerd Hoffmann         if (strcmp(share, "ignore") == 0) {
35728cf36489SGerd Hoffmann             vs->share_policy = VNC_SHARE_POLICY_IGNORE;
35734db14629SGerd Hoffmann         } else if (strcmp(share, "allow-exclusive") == 0) {
35748cf36489SGerd Hoffmann             vs->share_policy = VNC_SHARE_POLICY_ALLOW_EXCLUSIVE;
35754db14629SGerd Hoffmann         } else if (strcmp(share, "force-shared") == 0) {
35768cf36489SGerd Hoffmann             vs->share_policy = VNC_SHARE_POLICY_FORCE_SHARED;
35778cf36489SGerd Hoffmann         } else {
35782d55f0e8SPaolo Bonzini             error_setg(errp, "unknown vnc share= option");
35791ce52c78SPaolo Bonzini             goto fail;
35808cf36489SGerd Hoffmann         }
35814db14629SGerd Hoffmann     } else {
35824db14629SGerd Hoffmann         vs->share_policy = VNC_SHARE_POLICY_ALLOW_EXCLUSIVE;
35833e230dd2SCorentin Chary     }
3584e5f34cddSGerd Hoffmann     vs->connections_limit = qemu_opt_get_number(opts, "connections", 32);
35853e230dd2SCorentin Chary 
35864db14629SGerd Hoffmann     websocket = qemu_opt_get(opts, "websocket");
35874db14629SGerd Hoffmann     if (websocket) {
3588bf7aa45eSGerd Hoffmann         vs->ws_enabled = true;
3589e5560329SGerd Hoffmann         qemu_opt_set(wsopts, "port", websocket, &error_abort);
35908e9b0d24SDaniel P. Berrange         if (!qcrypto_hash_supports(QCRYPTO_HASH_ALG_SHA1)) {
35918e9b0d24SDaniel P. Berrange             error_setg(errp, "SHA1 hash support is required for websockets");
3592d169f04bSDaniel P. Berrange             goto fail;
35938e9b0d24SDaniel P. Berrange         }
35944db14629SGerd Hoffmann     }
35954db14629SGerd Hoffmann 
35964db14629SGerd Hoffmann #ifdef CONFIG_VNC_JPEG
35974db14629SGerd Hoffmann     vs->lossy = qemu_opt_get_bool(opts, "lossy", false);
35984db14629SGerd Hoffmann #endif
35994db14629SGerd Hoffmann     vs->non_adaptive = qemu_opt_get_bool(opts, "non-adaptive", false);
3600e22492d3SPeter Lieven     /* adaptive updates are only used with tight encoding and
3601e22492d3SPeter Lieven      * if lossy updates are enabled so we can disable all the
3602e22492d3SPeter Lieven      * calculations otherwise */
3603e22492d3SPeter Lieven     if (!vs->lossy) {
3604e22492d3SPeter Lieven         vs->non_adaptive = true;
3605e22492d3SPeter Lieven     }
3606e22492d3SPeter Lieven 
36073e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS
36083e230dd2SCorentin Chary     if (acl && x509 && vs->tls.x509verify) {
3609c8496408SGerd Hoffmann         char *aclname;
3610c8496408SGerd Hoffmann 
3611c8496408SGerd Hoffmann         if (strcmp(vs->id, "default") == 0) {
3612c8496408SGerd Hoffmann             aclname = g_strdup("vnc.x509dname");
3613c8496408SGerd Hoffmann         } else {
3614c8496408SGerd Hoffmann             aclname = g_strdup_printf("vnc.%s.x509dname", vs->id);
3615c8496408SGerd Hoffmann         }
3616c8496408SGerd Hoffmann         vs->tls.acl = qemu_acl_init(aclname);
3617c8496408SGerd Hoffmann         g_free(aclname);
36183e230dd2SCorentin Chary     }
36193e230dd2SCorentin Chary #endif
36203e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL
36213e230dd2SCorentin Chary     if (acl && sasl) {
3622c8496408SGerd Hoffmann         char *aclname;
3623c8496408SGerd Hoffmann 
3624c8496408SGerd Hoffmann         if (strcmp(vs->id, "default") == 0) {
3625c8496408SGerd Hoffmann             aclname = g_strdup("vnc.username");
3626c8496408SGerd Hoffmann         } else {
3627c8496408SGerd Hoffmann             aclname = g_strdup_printf("vnc.%s.username", vs->id);
3628c8496408SGerd Hoffmann         }
3629c8496408SGerd Hoffmann         vs->sasl.acl = qemu_acl_init(aclname);
3630c8496408SGerd Hoffmann         g_free(aclname);
36313e230dd2SCorentin Chary     }
36323e230dd2SCorentin Chary #endif
36333e230dd2SCorentin Chary 
3634f9148c8aSDaniel P. Berrange     vnc_display_setup_auth(vs, password, sasl, tls, x509, websocket);
36353e230dd2SCorentin Chary 
36363e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL
36373e230dd2SCorentin Chary     if ((saslErr = sasl_server_init(NULL, "qemu")) != SASL_OK) {
36382d55f0e8SPaolo Bonzini         error_setg(errp, "Failed to initialize SASL auth: %s",
36393e230dd2SCorentin Chary                    sasl_errstring(saslErr, NULL, NULL));
36401ce52c78SPaolo Bonzini         goto fail;
36413e230dd2SCorentin Chary     }
36423e230dd2SCorentin Chary #endif
36433e230dd2SCorentin Chary     vs->lock_key_sync = lock_key_sync;
36443e230dd2SCorentin Chary 
36451d0d59feSGerd Hoffmann     device_id = qemu_opt_get(opts, "display");
36461d0d59feSGerd Hoffmann     if (device_id) {
36471d0d59feSGerd Hoffmann         DeviceState *dev;
36481d0d59feSGerd Hoffmann         int head = qemu_opt_get_number(opts, "head", 0);
36491d0d59feSGerd Hoffmann 
36501d0d59feSGerd Hoffmann         dev = qdev_find_recursive(sysbus_get_default(), device_id);
36511d0d59feSGerd Hoffmann         if (dev == NULL) {
3652f3cf80e8SMarkus Armbruster             error_setg(errp, "Device '%s' not found", device_id);
36531d0d59feSGerd Hoffmann             goto fail;
36541d0d59feSGerd Hoffmann         }
36551d0d59feSGerd Hoffmann 
36561d0d59feSGerd Hoffmann         con = qemu_console_lookup_by_device(dev, head);
36571d0d59feSGerd Hoffmann         if (con == NULL) {
36581d0d59feSGerd Hoffmann             error_setg(errp, "Device %s is not bound to a QemuConsole",
36591d0d59feSGerd Hoffmann                        device_id);
36601d0d59feSGerd Hoffmann             goto fail;
36611d0d59feSGerd Hoffmann         }
36621d0d59feSGerd Hoffmann     } else {
36631d0d59feSGerd Hoffmann         con = NULL;
36641d0d59feSGerd Hoffmann     }
36651d0d59feSGerd Hoffmann 
36661d0d59feSGerd Hoffmann     if (con != vs->dcl.con) {
36671d0d59feSGerd Hoffmann         unregister_displaychangelistener(&vs->dcl);
36681d0d59feSGerd Hoffmann         vs->dcl.con = con;
36691d0d59feSGerd Hoffmann         register_displaychangelistener(&vs->dcl);
36701d0d59feSGerd Hoffmann     }
36711d0d59feSGerd Hoffmann 
36723e230dd2SCorentin Chary     if (reverse) {
36733e230dd2SCorentin Chary         /* connect to viewer */
3674007fcd3eSPaolo Bonzini         int csock;
36753e230dd2SCorentin Chary         vs->lsock = -1;
36767536ee4bSTim Hardeck         vs->lwebsock = -1;
3677e5560329SGerd Hoffmann         if (strncmp(vnc, "unix:", 5) == 0) {
3678e5560329SGerd Hoffmann             csock = unix_connect(vnc+5, errp);
3679007fcd3eSPaolo Bonzini         } else {
3680e5560329SGerd Hoffmann             csock = inet_connect(vnc, errp);
36813e230dd2SCorentin Chary         }
3682007fcd3eSPaolo Bonzini         if (csock < 0) {
3683007fcd3eSPaolo Bonzini             goto fail;
3684007fcd3eSPaolo Bonzini         }
36852c8cf549SMichael Tokarev         vnc_connect(vs, csock, false, false);
36863e230dd2SCorentin Chary     } else {
36873e230dd2SCorentin Chary         /* listen for connects */
3688e5560329SGerd Hoffmann         if (strncmp(vnc, "unix:", 5) == 0) {
3689e5560329SGerd Hoffmann             vs->lsock = unix_listen(vnc+5, NULL, 0, errp);
36903d00ac1aSCole Robinson             if (vs->lsock < 0) {
36913d00ac1aSCole Robinson                 goto fail;
36923d00ac1aSCole Robinson             }
3693bf7aa45eSGerd Hoffmann             vs->is_unix = true;
36943e230dd2SCorentin Chary         } else {
3695e5560329SGerd Hoffmann             vs->lsock = inet_listen_opts(sopts, 5900, errp);
3696c1c1619cSPaolo Bonzini             if (vs->lsock < 0) {
36971ce52c78SPaolo Bonzini                 goto fail;
36981ce52c78SPaolo Bonzini             }
3699bf7aa45eSGerd Hoffmann             if (vs->ws_enabled) {
3700e5560329SGerd Hoffmann                 vs->lwebsock = inet_listen_opts(wsopts, 0, errp);
37017536ee4bSTim Hardeck                 if (vs->lwebsock < 0) {
3702b3c33f91SGonglei                     if (vs->lsock != -1) {
37037536ee4bSTim Hardeck                         close(vs->lsock);
37047536ee4bSTim Hardeck                         vs->lsock = -1;
37057536ee4bSTim Hardeck                     }
37067536ee4bSTim Hardeck                     goto fail;
37077536ee4bSTim Hardeck                 }
37087536ee4bSTim Hardeck             }
37097536ee4bSTim Hardeck         }
3710bf7aa45eSGerd Hoffmann         vs->enabled = true;
371182e1cc4bSFam Zheng         qemu_set_fd_handler(vs->lsock, vnc_listen_regular_read, NULL, vs);
3712bf7aa45eSGerd Hoffmann         if (vs->ws_enabled) {
371382e1cc4bSFam Zheng             qemu_set_fd_handler(vs->lwebsock, vnc_listen_websocket_read,
371482e1cc4bSFam Zheng                                 NULL, vs);
37157536ee4bSTim Hardeck         }
37163e230dd2SCorentin Chary     }
3717e5560329SGerd Hoffmann     qemu_opts_del(sopts);
3718e5560329SGerd Hoffmann     qemu_opts_del(wsopts);
37192d55f0e8SPaolo Bonzini     return;
37201ce52c78SPaolo Bonzini 
37211ce52c78SPaolo Bonzini fail:
3722e5560329SGerd Hoffmann     qemu_opts_del(sopts);
3723e5560329SGerd Hoffmann     qemu_opts_del(wsopts);
3724bf7aa45eSGerd Hoffmann     vs->enabled = false;
3725bf7aa45eSGerd Hoffmann     vs->ws_enabled = false;
37263e230dd2SCorentin Chary }
372713661089SDaniel P. Berrange 
372814f7143eSGerd Hoffmann void vnc_display_add_client(const char *id, int csock, bool skipauth)
372913661089SDaniel P. Berrange {
373014f7143eSGerd Hoffmann     VncDisplay *vs = vnc_display_find(id);
373113661089SDaniel P. Berrange 
3732d616ccc5SGerd Hoffmann     if (!vs) {
3733d616ccc5SGerd Hoffmann         return;
3734d616ccc5SGerd Hoffmann     }
37352c8cf549SMichael Tokarev     vnc_connect(vs, csock, skipauth, false);
373613661089SDaniel P. Berrange }
37374db14629SGerd Hoffmann 
37389634f4e3SGerd Hoffmann static void vnc_auto_assign_id(QemuOptsList *olist, QemuOpts *opts)
37392779672fSGonglei {
37402779672fSGonglei     int i = 2;
37412779672fSGonglei     char *id;
37422779672fSGonglei 
37432779672fSGonglei     id = g_strdup("default");
37442779672fSGonglei     while (qemu_opts_find(olist, id)) {
37452779672fSGonglei         g_free(id);
37462779672fSGonglei         id = g_strdup_printf("vnc%d", i++);
37472779672fSGonglei     }
37482779672fSGonglei     qemu_opts_set_id(opts, id);
37492779672fSGonglei }
37502779672fSGonglei 
375170b94331SMarkus Armbruster QemuOpts *vnc_parse(const char *str, Error **errp)
37524db14629SGerd Hoffmann {
37534db14629SGerd Hoffmann     QemuOptsList *olist = qemu_find_opts("vnc");
375470b94331SMarkus Armbruster     QemuOpts *opts = qemu_opts_parse(olist, str, true, errp);
375581607cbfSGonglei     const char *id;
37564db14629SGerd Hoffmann 
375781607cbfSGonglei     if (!opts) {
375881607cbfSGonglei         return NULL;
375981607cbfSGonglei     }
376081607cbfSGonglei 
376181607cbfSGonglei     id = qemu_opts_id(opts);
37624db14629SGerd Hoffmann     if (!id) {
37634db14629SGerd Hoffmann         /* auto-assign id if not present */
37642779672fSGonglei         vnc_auto_assign_id(olist, opts);
37659634f4e3SGerd Hoffmann     }
37669634f4e3SGerd Hoffmann     return opts;
37674db14629SGerd Hoffmann }
37684db14629SGerd Hoffmann 
376928d0de7aSMarkus Armbruster int vnc_init_func(void *opaque, QemuOpts *opts, Error **errp)
37709634f4e3SGerd Hoffmann {
37719634f4e3SGerd Hoffmann     Error *local_err = NULL;
37729634f4e3SGerd Hoffmann     char *id = (char *)qemu_opts_id(opts);
37739634f4e3SGerd Hoffmann 
37749634f4e3SGerd Hoffmann     assert(id);
37754db14629SGerd Hoffmann     vnc_display_init(id);
37764db14629SGerd Hoffmann     vnc_display_open(id, &local_err);
37774db14629SGerd Hoffmann     if (local_err != NULL) {
3778bc119048SCole Robinson         error_report("Failed to start VNC server: %s",
37794db14629SGerd Hoffmann                      error_get_pretty(local_err));
37804db14629SGerd Hoffmann         error_free(local_err);
37814db14629SGerd Hoffmann         exit(1);
37824db14629SGerd Hoffmann     }
37834db14629SGerd Hoffmann     return 0;
37844db14629SGerd Hoffmann }
37854db14629SGerd Hoffmann 
37864db14629SGerd Hoffmann static void vnc_register_config(void)
37874db14629SGerd Hoffmann {
37884db14629SGerd Hoffmann     qemu_add_opts(&qemu_vnc_opts);
37894db14629SGerd Hoffmann }
37904db14629SGerd Hoffmann machine_init(vnc_register_config);
3791