xref: /openbmc/qemu/ui/vnc.c (revision df887684603a4b3b0c623090a6b419dc70f22c32)
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"
321de7afc9SPaolo Bonzini #include "qemu/sockets.h"
331de7afc9SPaolo Bonzini #include "qemu/timer.h"
341de7afc9SPaolo Bonzini #include "qemu/acl.h"
354db14629SGerd Hoffmann #include "qemu/config-file.h"
367b1b5d19SPaolo Bonzini #include "qapi/qmp/types.h"
372b54aa87SLuiz Capitulino #include "qmp-commands.h"
381de7afc9SPaolo Bonzini #include "qemu/osdep.h"
398d447d10SGerd Hoffmann #include "ui/input.h"
40fb6ba0d5SWenchao Xia #include "qapi-event.h"
413e230dd2SCorentin Chary 
420f7b2864SGerd Hoffmann #define VNC_REFRESH_INTERVAL_BASE GUI_REFRESH_INTERVAL_DEFAULT
433e230dd2SCorentin Chary #define VNC_REFRESH_INTERVAL_INC  50
440f7b2864SGerd Hoffmann #define VNC_REFRESH_INTERVAL_MAX  GUI_REFRESH_INTERVAL_IDLE
45999342a0SCorentin Chary static const struct timeval VNC_REFRESH_STATS = { 0, 500000 };
46999342a0SCorentin Chary static const struct timeval VNC_REFRESH_LOSSY = { 2, 0 };
473e230dd2SCorentin Chary 
483e230dd2SCorentin Chary #include "vnc_keysym.h"
493e230dd2SCorentin Chary #include "d3des.h"
503e230dd2SCorentin Chary 
51d616ccc5SGerd Hoffmann static QTAILQ_HEAD(, VncDisplay) vnc_displays =
52d616ccc5SGerd Hoffmann     QTAILQ_HEAD_INITIALIZER(vnc_displays);
533e230dd2SCorentin Chary 
543e230dd2SCorentin Chary static int vnc_cursor_define(VncState *vs);
557bc9318bSGerd Hoffmann static void vnc_release_modifiers(VncState *vs);
563e230dd2SCorentin Chary 
578cf36489SGerd Hoffmann static void vnc_set_share_mode(VncState *vs, VncShareMode mode)
588cf36489SGerd Hoffmann {
598cf36489SGerd Hoffmann #ifdef _VNC_DEBUG
608cf36489SGerd Hoffmann     static const char *mn[] = {
618cf36489SGerd Hoffmann         [0]                           = "undefined",
628cf36489SGerd Hoffmann         [VNC_SHARE_MODE_CONNECTING]   = "connecting",
638cf36489SGerd Hoffmann         [VNC_SHARE_MODE_SHARED]       = "shared",
648cf36489SGerd Hoffmann         [VNC_SHARE_MODE_EXCLUSIVE]    = "exclusive",
658cf36489SGerd Hoffmann         [VNC_SHARE_MODE_DISCONNECTED] = "disconnected",
668cf36489SGerd Hoffmann     };
678cf36489SGerd Hoffmann     fprintf(stderr, "%s/%d: %s -> %s\n", __func__,
688cf36489SGerd Hoffmann             vs->csock, mn[vs->share_mode], mn[mode]);
698cf36489SGerd Hoffmann #endif
708cf36489SGerd Hoffmann 
71e5f34cddSGerd Hoffmann     switch (vs->share_mode) {
72e5f34cddSGerd Hoffmann     case VNC_SHARE_MODE_CONNECTING:
73e5f34cddSGerd Hoffmann         vs->vd->num_connecting--;
74e5f34cddSGerd Hoffmann         break;
75e5f34cddSGerd Hoffmann     case VNC_SHARE_MODE_SHARED:
76e5f34cddSGerd Hoffmann         vs->vd->num_shared--;
77e5f34cddSGerd Hoffmann         break;
78e5f34cddSGerd Hoffmann     case VNC_SHARE_MODE_EXCLUSIVE:
798cf36489SGerd Hoffmann         vs->vd->num_exclusive--;
80e5f34cddSGerd Hoffmann         break;
81e5f34cddSGerd Hoffmann     default:
82e5f34cddSGerd Hoffmann         break;
838cf36489SGerd Hoffmann     }
84e5f34cddSGerd Hoffmann 
858cf36489SGerd Hoffmann     vs->share_mode = mode;
86e5f34cddSGerd Hoffmann 
87e5f34cddSGerd Hoffmann     switch (vs->share_mode) {
88e5f34cddSGerd Hoffmann     case VNC_SHARE_MODE_CONNECTING:
89e5f34cddSGerd Hoffmann         vs->vd->num_connecting++;
90e5f34cddSGerd Hoffmann         break;
91e5f34cddSGerd Hoffmann     case VNC_SHARE_MODE_SHARED:
92e5f34cddSGerd Hoffmann         vs->vd->num_shared++;
93e5f34cddSGerd Hoffmann         break;
94e5f34cddSGerd Hoffmann     case VNC_SHARE_MODE_EXCLUSIVE:
958cf36489SGerd Hoffmann         vs->vd->num_exclusive++;
96e5f34cddSGerd Hoffmann         break;
97e5f34cddSGerd Hoffmann     default:
98e5f34cddSGerd Hoffmann         break;
998cf36489SGerd Hoffmann     }
1008cf36489SGerd Hoffmann }
1018cf36489SGerd Hoffmann 
1023e230dd2SCorentin Chary static char *addr_to_string(const char *format,
1033e230dd2SCorentin Chary                             struct sockaddr_storage *sa,
1043e230dd2SCorentin Chary                             socklen_t salen) {
1053e230dd2SCorentin Chary     char *addr;
1063e230dd2SCorentin Chary     char host[NI_MAXHOST];
1073e230dd2SCorentin Chary     char serv[NI_MAXSERV];
1083e230dd2SCorentin Chary     int err;
1093e230dd2SCorentin Chary     size_t addrlen;
1103e230dd2SCorentin Chary 
1113e230dd2SCorentin Chary     if ((err = getnameinfo((struct sockaddr *)sa, salen,
1123e230dd2SCorentin Chary                            host, sizeof(host),
1133e230dd2SCorentin Chary                            serv, sizeof(serv),
1143e230dd2SCorentin Chary                            NI_NUMERICHOST | NI_NUMERICSERV)) != 0) {
1153e230dd2SCorentin Chary         VNC_DEBUG("Cannot resolve address %d: %s\n",
1163e230dd2SCorentin Chary                   err, gai_strerror(err));
1173e230dd2SCorentin Chary         return NULL;
1183e230dd2SCorentin Chary     }
1193e230dd2SCorentin Chary 
1203e230dd2SCorentin Chary     /* Enough for the existing format + the 2 vars we're
1213e230dd2SCorentin Chary      * substituting in. */
1223e230dd2SCorentin Chary     addrlen = strlen(format) + strlen(host) + strlen(serv);
1237267c094SAnthony Liguori     addr = g_malloc(addrlen + 1);
1243e230dd2SCorentin Chary     snprintf(addr, addrlen, format, host, serv);
1253e230dd2SCorentin Chary     addr[addrlen] = '\0';
1263e230dd2SCorentin Chary 
1273e230dd2SCorentin Chary     return addr;
1283e230dd2SCorentin Chary }
1293e230dd2SCorentin Chary 
1303e230dd2SCorentin Chary 
1313e230dd2SCorentin Chary char *vnc_socket_local_addr(const char *format, int fd) {
1323e230dd2SCorentin Chary     struct sockaddr_storage sa;
1333e230dd2SCorentin Chary     socklen_t salen;
1343e230dd2SCorentin Chary 
1353e230dd2SCorentin Chary     salen = sizeof(sa);
1363e230dd2SCorentin Chary     if (getsockname(fd, (struct sockaddr*)&sa, &salen) < 0)
1373e230dd2SCorentin Chary         return NULL;
1383e230dd2SCorentin Chary 
1393e230dd2SCorentin Chary     return addr_to_string(format, &sa, salen);
1403e230dd2SCorentin Chary }
1413e230dd2SCorentin Chary 
1423e230dd2SCorentin Chary char *vnc_socket_remote_addr(const char *format, int fd) {
1433e230dd2SCorentin Chary     struct sockaddr_storage sa;
1443e230dd2SCorentin Chary     socklen_t salen;
1453e230dd2SCorentin Chary 
1463e230dd2SCorentin Chary     salen = sizeof(sa);
1473e230dd2SCorentin Chary     if (getpeername(fd, (struct sockaddr*)&sa, &salen) < 0)
1483e230dd2SCorentin Chary         return NULL;
1493e230dd2SCorentin Chary 
1503e230dd2SCorentin Chary     return addr_to_string(format, &sa, salen);
1513e230dd2SCorentin Chary }
1523e230dd2SCorentin Chary 
153fb6ba0d5SWenchao Xia static VncBasicInfo *vnc_basic_info_get(struct sockaddr_storage *sa,
1543e230dd2SCorentin Chary                                         socklen_t salen)
1553e230dd2SCorentin Chary {
156fb6ba0d5SWenchao Xia     VncBasicInfo *info;
1573e230dd2SCorentin Chary     char host[NI_MAXHOST];
1583e230dd2SCorentin Chary     char serv[NI_MAXSERV];
1593e230dd2SCorentin Chary     int err;
1603e230dd2SCorentin Chary 
1613e230dd2SCorentin Chary     if ((err = getnameinfo((struct sockaddr *)sa, salen,
1623e230dd2SCorentin Chary                            host, sizeof(host),
1633e230dd2SCorentin Chary                            serv, sizeof(serv),
1643e230dd2SCorentin Chary                            NI_NUMERICHOST | NI_NUMERICSERV)) != 0) {
1653e230dd2SCorentin Chary         VNC_DEBUG("Cannot resolve address %d: %s\n",
1663e230dd2SCorentin Chary                   err, gai_strerror(err));
167fb6ba0d5SWenchao Xia         return NULL;
1683e230dd2SCorentin Chary     }
1693e230dd2SCorentin Chary 
170fb6ba0d5SWenchao Xia     info = g_malloc0(sizeof(VncBasicInfo));
171fb6ba0d5SWenchao Xia     info->host = g_strdup(host);
172fb6ba0d5SWenchao Xia     info->service = g_strdup(serv);
173fb6ba0d5SWenchao Xia     info->family = inet_netfamily(sa->ss_family);
174fb6ba0d5SWenchao Xia     return info;
1753e230dd2SCorentin Chary }
1763e230dd2SCorentin Chary 
177fb6ba0d5SWenchao Xia static VncBasicInfo *vnc_basic_info_get_from_server_addr(int fd)
1783e230dd2SCorentin Chary {
1793e230dd2SCorentin Chary     struct sockaddr_storage sa;
1803e230dd2SCorentin Chary     socklen_t salen;
1813e230dd2SCorentin Chary 
1823e230dd2SCorentin Chary     salen = sizeof(sa);
1833e230dd2SCorentin Chary     if (getsockname(fd, (struct sockaddr*)&sa, &salen) < 0) {
184fb6ba0d5SWenchao Xia         return NULL;
1853e230dd2SCorentin Chary     }
1863e230dd2SCorentin Chary 
187fb6ba0d5SWenchao Xia     return vnc_basic_info_get(&sa, salen);
1883e230dd2SCorentin Chary }
1893e230dd2SCorentin Chary 
190fb6ba0d5SWenchao Xia static VncBasicInfo *vnc_basic_info_get_from_remote_addr(int fd)
1913e230dd2SCorentin Chary {
1923e230dd2SCorentin Chary     struct sockaddr_storage sa;
1933e230dd2SCorentin Chary     socklen_t salen;
1943e230dd2SCorentin Chary 
1953e230dd2SCorentin Chary     salen = sizeof(sa);
1963e230dd2SCorentin Chary     if (getpeername(fd, (struct sockaddr*)&sa, &salen) < 0) {
197fb6ba0d5SWenchao Xia         return NULL;
1983e230dd2SCorentin Chary     }
1993e230dd2SCorentin Chary 
200fb6ba0d5SWenchao Xia     return vnc_basic_info_get(&sa, salen);
2013e230dd2SCorentin Chary }
2023e230dd2SCorentin Chary 
2033e230dd2SCorentin Chary static const char *vnc_auth_name(VncDisplay *vd) {
2043e230dd2SCorentin Chary     switch (vd->auth) {
2053e230dd2SCorentin Chary     case VNC_AUTH_INVALID:
2063e230dd2SCorentin Chary         return "invalid";
2073e230dd2SCorentin Chary     case VNC_AUTH_NONE:
2083e230dd2SCorentin Chary         return "none";
2093e230dd2SCorentin Chary     case VNC_AUTH_VNC:
2103e230dd2SCorentin Chary         return "vnc";
2113e230dd2SCorentin Chary     case VNC_AUTH_RA2:
2123e230dd2SCorentin Chary         return "ra2";
2133e230dd2SCorentin Chary     case VNC_AUTH_RA2NE:
2143e230dd2SCorentin Chary         return "ra2ne";
2153e230dd2SCorentin Chary     case VNC_AUTH_TIGHT:
2163e230dd2SCorentin Chary         return "tight";
2173e230dd2SCorentin Chary     case VNC_AUTH_ULTRA:
2183e230dd2SCorentin Chary         return "ultra";
2193e230dd2SCorentin Chary     case VNC_AUTH_TLS:
2203e230dd2SCorentin Chary         return "tls";
2213e230dd2SCorentin Chary     case VNC_AUTH_VENCRYPT:
2223e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS
2233e230dd2SCorentin Chary         switch (vd->subauth) {
2243e230dd2SCorentin Chary         case VNC_AUTH_VENCRYPT_PLAIN:
2253e230dd2SCorentin Chary             return "vencrypt+plain";
2263e230dd2SCorentin Chary         case VNC_AUTH_VENCRYPT_TLSNONE:
2273e230dd2SCorentin Chary             return "vencrypt+tls+none";
2283e230dd2SCorentin Chary         case VNC_AUTH_VENCRYPT_TLSVNC:
2293e230dd2SCorentin Chary             return "vencrypt+tls+vnc";
2303e230dd2SCorentin Chary         case VNC_AUTH_VENCRYPT_TLSPLAIN:
2313e230dd2SCorentin Chary             return "vencrypt+tls+plain";
2323e230dd2SCorentin Chary         case VNC_AUTH_VENCRYPT_X509NONE:
2333e230dd2SCorentin Chary             return "vencrypt+x509+none";
2343e230dd2SCorentin Chary         case VNC_AUTH_VENCRYPT_X509VNC:
2353e230dd2SCorentin Chary             return "vencrypt+x509+vnc";
2363e230dd2SCorentin Chary         case VNC_AUTH_VENCRYPT_X509PLAIN:
2373e230dd2SCorentin Chary             return "vencrypt+x509+plain";
2383e230dd2SCorentin Chary         case VNC_AUTH_VENCRYPT_TLSSASL:
2393e230dd2SCorentin Chary             return "vencrypt+tls+sasl";
2403e230dd2SCorentin Chary         case VNC_AUTH_VENCRYPT_X509SASL:
2413e230dd2SCorentin Chary             return "vencrypt+x509+sasl";
2423e230dd2SCorentin Chary         default:
2433e230dd2SCorentin Chary             return "vencrypt";
2443e230dd2SCorentin Chary         }
2453e230dd2SCorentin Chary #else
2463e230dd2SCorentin Chary         return "vencrypt";
2473e230dd2SCorentin Chary #endif
2483e230dd2SCorentin Chary     case VNC_AUTH_SASL:
2493e230dd2SCorentin Chary         return "sasl";
2503e230dd2SCorentin Chary     }
2513e230dd2SCorentin Chary     return "unknown";
2523e230dd2SCorentin Chary }
2533e230dd2SCorentin Chary 
254d616ccc5SGerd Hoffmann static VncServerInfo *vnc_server_info_get(VncDisplay *vd)
2553e230dd2SCorentin Chary {
256fb6ba0d5SWenchao Xia     VncServerInfo *info;
257d616ccc5SGerd Hoffmann     VncBasicInfo *bi = vnc_basic_info_get_from_server_addr(vd->lsock);
258fb6ba0d5SWenchao Xia     if (!bi) {
259fb6ba0d5SWenchao Xia         return NULL;
2603e230dd2SCorentin Chary     }
2613e230dd2SCorentin Chary 
262fb6ba0d5SWenchao Xia     info = g_malloc(sizeof(*info));
263fb6ba0d5SWenchao Xia     info->base = bi;
264fb6ba0d5SWenchao Xia     info->has_auth = true;
265d616ccc5SGerd Hoffmann     info->auth = g_strdup(vnc_auth_name(vd));
266fb6ba0d5SWenchao Xia     return info;
2673e230dd2SCorentin Chary }
2683e230dd2SCorentin Chary 
2693e230dd2SCorentin Chary static void vnc_client_cache_auth(VncState *client)
2703e230dd2SCorentin Chary {
2713e230dd2SCorentin Chary     if (!client->info) {
2723e230dd2SCorentin Chary         return;
2733e230dd2SCorentin Chary     }
2743e230dd2SCorentin Chary 
2753e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS
2763e230dd2SCorentin Chary     if (client->tls.session &&
2773e230dd2SCorentin Chary         client->tls.dname) {
278fb6ba0d5SWenchao Xia         client->info->has_x509_dname = true;
279fb6ba0d5SWenchao Xia         client->info->x509_dname = g_strdup(client->tls.dname);
2803e230dd2SCorentin Chary     }
2813e230dd2SCorentin Chary #endif
2823e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL
2833e230dd2SCorentin Chary     if (client->sasl.conn &&
2843e230dd2SCorentin Chary         client->sasl.username) {
285fb6ba0d5SWenchao Xia         client->info->has_sasl_username = true;
286fb6ba0d5SWenchao Xia         client->info->sasl_username = g_strdup(client->sasl.username);
2873e230dd2SCorentin Chary     }
2883e230dd2SCorentin Chary #endif
2893e230dd2SCorentin Chary }
2903e230dd2SCorentin Chary 
2913e230dd2SCorentin Chary static void vnc_client_cache_addr(VncState *client)
2923e230dd2SCorentin Chary {
293fb6ba0d5SWenchao Xia     VncBasicInfo *bi = vnc_basic_info_get_from_remote_addr(client->csock);
2943e230dd2SCorentin Chary 
295fb6ba0d5SWenchao Xia     if (bi) {
296fb6ba0d5SWenchao Xia         client->info = g_malloc0(sizeof(*client->info));
297fb6ba0d5SWenchao Xia         client->info->base = bi;
298fb6ba0d5SWenchao Xia     }
2993e230dd2SCorentin Chary }
3003e230dd2SCorentin Chary 
301fb6ba0d5SWenchao Xia static void vnc_qmp_event(VncState *vs, QAPIEvent event)
3023e230dd2SCorentin Chary {
303fb6ba0d5SWenchao Xia     VncServerInfo *si;
3043e230dd2SCorentin Chary 
3053e230dd2SCorentin Chary     if (!vs->info) {
3063e230dd2SCorentin Chary         return;
3073e230dd2SCorentin Chary     }
308fb6ba0d5SWenchao Xia     g_assert(vs->info->base);
3093e230dd2SCorentin Chary 
310d616ccc5SGerd Hoffmann     si = vnc_server_info_get(vs->vd);
311fb6ba0d5SWenchao Xia     if (!si) {
3123e230dd2SCorentin Chary         return;
3133e230dd2SCorentin Chary     }
3143e230dd2SCorentin Chary 
315fb6ba0d5SWenchao Xia     switch (event) {
316fb6ba0d5SWenchao Xia     case QAPI_EVENT_VNC_CONNECTED:
317fb6ba0d5SWenchao Xia         qapi_event_send_vnc_connected(si, vs->info->base, &error_abort);
318fb6ba0d5SWenchao Xia         break;
319fb6ba0d5SWenchao Xia     case QAPI_EVENT_VNC_INITIALIZED:
320fb6ba0d5SWenchao Xia         qapi_event_send_vnc_initialized(si, vs->info, &error_abort);
321fb6ba0d5SWenchao Xia         break;
322fb6ba0d5SWenchao Xia     case QAPI_EVENT_VNC_DISCONNECTED:
323fb6ba0d5SWenchao Xia         qapi_event_send_vnc_disconnected(si, vs->info, &error_abort);
324fb6ba0d5SWenchao Xia         break;
325fb6ba0d5SWenchao Xia     default:
326fb6ba0d5SWenchao Xia         break;
327fb6ba0d5SWenchao Xia     }
3283e230dd2SCorentin Chary 
329fb6ba0d5SWenchao Xia     qapi_free_VncServerInfo(si);
3303e230dd2SCorentin Chary }
3313e230dd2SCorentin Chary 
3322b54aa87SLuiz Capitulino static VncClientInfo *qmp_query_vnc_client(const VncState *client)
3333e230dd2SCorentin Chary {
3342b54aa87SLuiz Capitulino     struct sockaddr_storage sa;
3352b54aa87SLuiz Capitulino     socklen_t salen = sizeof(sa);
3362b54aa87SLuiz Capitulino     char host[NI_MAXHOST];
3372b54aa87SLuiz Capitulino     char serv[NI_MAXSERV];
3382b54aa87SLuiz Capitulino     VncClientInfo *info;
3393e230dd2SCorentin Chary 
3402b54aa87SLuiz Capitulino     if (getpeername(client->csock, (struct sockaddr *)&sa, &salen) < 0) {
3412b54aa87SLuiz Capitulino         return NULL;
3422b54aa87SLuiz Capitulino     }
3432b54aa87SLuiz Capitulino 
3442b54aa87SLuiz Capitulino     if (getnameinfo((struct sockaddr *)&sa, salen,
3452b54aa87SLuiz Capitulino                     host, sizeof(host),
3462b54aa87SLuiz Capitulino                     serv, sizeof(serv),
3472b54aa87SLuiz Capitulino                     NI_NUMERICHOST | NI_NUMERICSERV) < 0) {
3482b54aa87SLuiz Capitulino         return NULL;
3492b54aa87SLuiz Capitulino     }
3502b54aa87SLuiz Capitulino 
3512b54aa87SLuiz Capitulino     info = g_malloc0(sizeof(*info));
352a589569fSWenchao Xia     info->base = g_malloc0(sizeof(*info->base));
353a589569fSWenchao Xia     info->base->host = g_strdup(host);
354a589569fSWenchao Xia     info->base->service = g_strdup(serv);
355a589569fSWenchao Xia     info->base->family = inet_netfamily(sa.ss_family);
3563e230dd2SCorentin Chary 
3573e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS
3582b54aa87SLuiz Capitulino     if (client->tls.session && client->tls.dname) {
3592b54aa87SLuiz Capitulino         info->has_x509_dname = true;
3602b54aa87SLuiz Capitulino         info->x509_dname = g_strdup(client->tls.dname);
3612b54aa87SLuiz Capitulino     }
3623e230dd2SCorentin Chary #endif
3633e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL
3642b54aa87SLuiz Capitulino     if (client->sasl.conn && client->sasl.username) {
3652b54aa87SLuiz Capitulino         info->has_sasl_username = true;
3662b54aa87SLuiz Capitulino         info->sasl_username = g_strdup(client->sasl.username);
3672b54aa87SLuiz Capitulino     }
3683e230dd2SCorentin Chary #endif
3692b54aa87SLuiz Capitulino 
3702b54aa87SLuiz Capitulino     return info;
3713e230dd2SCorentin Chary }
3723e230dd2SCorentin Chary 
373d616ccc5SGerd Hoffmann static VncDisplay *vnc_display_find(const char *id)
374d616ccc5SGerd Hoffmann {
375d616ccc5SGerd Hoffmann     VncDisplay *vd;
376d616ccc5SGerd Hoffmann 
377d616ccc5SGerd Hoffmann     if (id == NULL) {
378d616ccc5SGerd Hoffmann         return QTAILQ_FIRST(&vnc_displays);
379d616ccc5SGerd Hoffmann     }
380d616ccc5SGerd Hoffmann     QTAILQ_FOREACH(vd, &vnc_displays, next) {
381d616ccc5SGerd Hoffmann         if (strcmp(id, vd->id) == 0) {
382d616ccc5SGerd Hoffmann             return vd;
383d616ccc5SGerd Hoffmann         }
384d616ccc5SGerd Hoffmann     }
385d616ccc5SGerd Hoffmann     return NULL;
386d616ccc5SGerd Hoffmann }
387d616ccc5SGerd Hoffmann 
3882d29a436SGerd Hoffmann static VncClientInfoList *qmp_query_client_list(VncDisplay *vd)
3892d29a436SGerd Hoffmann {
3902d29a436SGerd Hoffmann     VncClientInfoList *cinfo, *prev = NULL;
3912d29a436SGerd Hoffmann     VncState *client;
3922d29a436SGerd Hoffmann 
3932d29a436SGerd Hoffmann     QTAILQ_FOREACH(client, &vd->clients, next) {
3942d29a436SGerd Hoffmann         cinfo = g_new0(VncClientInfoList, 1);
3952d29a436SGerd Hoffmann         cinfo->value = qmp_query_vnc_client(client);
3962d29a436SGerd Hoffmann         cinfo->next = prev;
3972d29a436SGerd Hoffmann         prev = cinfo;
3982d29a436SGerd Hoffmann     }
3992d29a436SGerd Hoffmann     return prev;
4002d29a436SGerd Hoffmann }
4012d29a436SGerd Hoffmann 
4022b54aa87SLuiz Capitulino VncInfo *qmp_query_vnc(Error **errp)
4033e230dd2SCorentin Chary {
4042b54aa87SLuiz Capitulino     VncInfo *info = g_malloc0(sizeof(*info));
405d616ccc5SGerd Hoffmann     VncDisplay *vd = vnc_display_find(NULL);
4063e230dd2SCorentin Chary 
407d616ccc5SGerd Hoffmann     if (vd == NULL || vd->display == NULL) {
4082b54aa87SLuiz Capitulino         info->enabled = false;
4093e230dd2SCorentin Chary     } else {
4102b54aa87SLuiz Capitulino         struct sockaddr_storage sa;
4112b54aa87SLuiz Capitulino         socklen_t salen = sizeof(sa);
4122b54aa87SLuiz Capitulino         char host[NI_MAXHOST];
4132b54aa87SLuiz Capitulino         char serv[NI_MAXSERV];
4143e230dd2SCorentin Chary 
4152b54aa87SLuiz Capitulino         info->enabled = true;
4162b54aa87SLuiz Capitulino 
4172b54aa87SLuiz Capitulino         /* for compatibility with the original command */
4182b54aa87SLuiz Capitulino         info->has_clients = true;
4192d29a436SGerd Hoffmann         info->clients = qmp_query_client_list(vd);
4203e230dd2SCorentin Chary 
421d616ccc5SGerd Hoffmann         if (vd->lsock == -1) {
422417b0b88SPaolo Bonzini             return info;
423417b0b88SPaolo Bonzini         }
424417b0b88SPaolo Bonzini 
425d616ccc5SGerd Hoffmann         if (getsockname(vd->lsock, (struct sockaddr *)&sa,
4262b54aa87SLuiz Capitulino                         &salen) == -1) {
4272b54aa87SLuiz Capitulino             error_set(errp, QERR_UNDEFINED_ERROR);
4282b54aa87SLuiz Capitulino             goto out_error;
4292b54aa87SLuiz Capitulino         }
4303e230dd2SCorentin Chary 
4312b54aa87SLuiz Capitulino         if (getnameinfo((struct sockaddr *)&sa, salen,
4322b54aa87SLuiz Capitulino                         host, sizeof(host),
4332b54aa87SLuiz Capitulino                         serv, sizeof(serv),
4342b54aa87SLuiz Capitulino                         NI_NUMERICHOST | NI_NUMERICSERV) < 0) {
4352b54aa87SLuiz Capitulino             error_set(errp, QERR_UNDEFINED_ERROR);
4362b54aa87SLuiz Capitulino             goto out_error;
4373e230dd2SCorentin Chary         }
4382b54aa87SLuiz Capitulino 
4392b54aa87SLuiz Capitulino         info->has_host = true;
4402b54aa87SLuiz Capitulino         info->host = g_strdup(host);
4412b54aa87SLuiz Capitulino 
4422b54aa87SLuiz Capitulino         info->has_service = true;
4432b54aa87SLuiz Capitulino         info->service = g_strdup(serv);
4442b54aa87SLuiz Capitulino 
4452b54aa87SLuiz Capitulino         info->has_family = true;
446a589569fSWenchao Xia         info->family = inet_netfamily(sa.ss_family);
4472b54aa87SLuiz Capitulino 
4482b54aa87SLuiz Capitulino         info->has_auth = true;
449d616ccc5SGerd Hoffmann         info->auth = g_strdup(vnc_auth_name(vd));
4503e230dd2SCorentin Chary     }
4512b54aa87SLuiz Capitulino 
4522b54aa87SLuiz Capitulino     return info;
4532b54aa87SLuiz Capitulino 
4542b54aa87SLuiz Capitulino out_error:
4552b54aa87SLuiz Capitulino     qapi_free_VncInfo(info);
4562b54aa87SLuiz Capitulino     return NULL;
4573e230dd2SCorentin Chary }
4583e230dd2SCorentin Chary 
459*df887684SGerd Hoffmann static VncBasicInfoList *qmp_query_server_entry(int socket,
460*df887684SGerd Hoffmann                                                 VncBasicInfoList *prev)
461*df887684SGerd Hoffmann {
462*df887684SGerd Hoffmann     VncBasicInfoList *list;
463*df887684SGerd Hoffmann     VncBasicInfo *info;
464*df887684SGerd Hoffmann     struct sockaddr_storage sa;
465*df887684SGerd Hoffmann     socklen_t salen = sizeof(sa);
466*df887684SGerd Hoffmann     char host[NI_MAXHOST];
467*df887684SGerd Hoffmann     char serv[NI_MAXSERV];
468*df887684SGerd Hoffmann 
469*df887684SGerd Hoffmann     if (getsockname(socket, (struct sockaddr *)&sa, &salen) < 0 ||
470*df887684SGerd Hoffmann         getnameinfo((struct sockaddr *)&sa, salen,
471*df887684SGerd Hoffmann                     host, sizeof(host), serv, sizeof(serv),
472*df887684SGerd Hoffmann                     NI_NUMERICHOST | NI_NUMERICSERV) < 0) {
473*df887684SGerd Hoffmann         return prev;
474*df887684SGerd Hoffmann     }
475*df887684SGerd Hoffmann 
476*df887684SGerd Hoffmann     info = g_new0(VncBasicInfo, 1);
477*df887684SGerd Hoffmann     info->host = g_strdup(host);
478*df887684SGerd Hoffmann     info->service = g_strdup(serv);
479*df887684SGerd Hoffmann     info->family = inet_netfamily(sa.ss_family);
480*df887684SGerd Hoffmann 
481*df887684SGerd Hoffmann     list = g_new0(VncBasicInfoList, 1);
482*df887684SGerd Hoffmann     list->value = info;
483*df887684SGerd Hoffmann     list->next = prev;
484*df887684SGerd Hoffmann     return list;
485*df887684SGerd Hoffmann }
486*df887684SGerd Hoffmann 
487*df887684SGerd Hoffmann static void qmp_query_auth(VncDisplay *vd, VncInfo2 *info)
488*df887684SGerd Hoffmann {
489*df887684SGerd Hoffmann     switch (vd->auth) {
490*df887684SGerd Hoffmann     case VNC_AUTH_VNC:
491*df887684SGerd Hoffmann         info->auth = VNC_PRIMARY_AUTH_VNC;
492*df887684SGerd Hoffmann         break;
493*df887684SGerd Hoffmann     case VNC_AUTH_RA2:
494*df887684SGerd Hoffmann         info->auth = VNC_PRIMARY_AUTH_RA2;
495*df887684SGerd Hoffmann         break;
496*df887684SGerd Hoffmann     case VNC_AUTH_RA2NE:
497*df887684SGerd Hoffmann         info->auth = VNC_PRIMARY_AUTH_RA2NE;
498*df887684SGerd Hoffmann         break;
499*df887684SGerd Hoffmann     case VNC_AUTH_TIGHT:
500*df887684SGerd Hoffmann         info->auth = VNC_PRIMARY_AUTH_TIGHT;
501*df887684SGerd Hoffmann         break;
502*df887684SGerd Hoffmann     case VNC_AUTH_ULTRA:
503*df887684SGerd Hoffmann         info->auth = VNC_PRIMARY_AUTH_ULTRA;
504*df887684SGerd Hoffmann         break;
505*df887684SGerd Hoffmann     case VNC_AUTH_TLS:
506*df887684SGerd Hoffmann         info->auth = VNC_PRIMARY_AUTH_TLS;
507*df887684SGerd Hoffmann         break;
508*df887684SGerd Hoffmann     case VNC_AUTH_VENCRYPT:
509*df887684SGerd Hoffmann         info->auth = VNC_PRIMARY_AUTH_VENCRYPT;
510*df887684SGerd Hoffmann #ifdef CONFIG_VNC_TLS
511*df887684SGerd Hoffmann         info->has_vencrypt = true;
512*df887684SGerd Hoffmann         switch (vd->subauth) {
513*df887684SGerd Hoffmann         case VNC_AUTH_VENCRYPT_PLAIN:
514*df887684SGerd Hoffmann             info->vencrypt = VNC_VENCRYPT_SUB_AUTH_PLAIN;
515*df887684SGerd Hoffmann             break;
516*df887684SGerd Hoffmann         case VNC_AUTH_VENCRYPT_TLSNONE:
517*df887684SGerd Hoffmann             info->vencrypt = VNC_VENCRYPT_SUB_AUTH_TLS_NONE;
518*df887684SGerd Hoffmann             break;
519*df887684SGerd Hoffmann         case VNC_AUTH_VENCRYPT_TLSVNC:
520*df887684SGerd Hoffmann             info->vencrypt = VNC_VENCRYPT_SUB_AUTH_TLS_VNC;
521*df887684SGerd Hoffmann             break;
522*df887684SGerd Hoffmann         case VNC_AUTH_VENCRYPT_TLSPLAIN:
523*df887684SGerd Hoffmann             info->vencrypt = VNC_VENCRYPT_SUB_AUTH_TLS_PLAIN;
524*df887684SGerd Hoffmann             break;
525*df887684SGerd Hoffmann         case VNC_AUTH_VENCRYPT_X509NONE:
526*df887684SGerd Hoffmann             info->vencrypt = VNC_VENCRYPT_SUB_AUTH_X509_NONE;
527*df887684SGerd Hoffmann             break;
528*df887684SGerd Hoffmann         case VNC_AUTH_VENCRYPT_X509VNC:
529*df887684SGerd Hoffmann             info->vencrypt = VNC_VENCRYPT_SUB_AUTH_X509_VNC;
530*df887684SGerd Hoffmann             break;
531*df887684SGerd Hoffmann         case VNC_AUTH_VENCRYPT_X509PLAIN:
532*df887684SGerd Hoffmann             info->vencrypt = VNC_VENCRYPT_SUB_AUTH_X509_PLAIN;
533*df887684SGerd Hoffmann             break;
534*df887684SGerd Hoffmann         case VNC_AUTH_VENCRYPT_TLSSASL:
535*df887684SGerd Hoffmann             info->vencrypt = VNC_VENCRYPT_SUB_AUTH_TLS_SASL;
536*df887684SGerd Hoffmann             break;
537*df887684SGerd Hoffmann         case VNC_AUTH_VENCRYPT_X509SASL:
538*df887684SGerd Hoffmann             info->vencrypt = VNC_VENCRYPT_SUB_AUTH_X509_SASL;
539*df887684SGerd Hoffmann             break;
540*df887684SGerd Hoffmann         default:
541*df887684SGerd Hoffmann             info->has_vencrypt = false;
542*df887684SGerd Hoffmann             break;
543*df887684SGerd Hoffmann         }
544*df887684SGerd Hoffmann #endif
545*df887684SGerd Hoffmann         break;
546*df887684SGerd Hoffmann     case VNC_AUTH_SASL:
547*df887684SGerd Hoffmann         info->auth = VNC_PRIMARY_AUTH_SASL;
548*df887684SGerd Hoffmann         break;
549*df887684SGerd Hoffmann     case VNC_AUTH_NONE:
550*df887684SGerd Hoffmann     default:
551*df887684SGerd Hoffmann         info->auth = VNC_PRIMARY_AUTH_NONE;
552*df887684SGerd Hoffmann         break;
553*df887684SGerd Hoffmann     }
554*df887684SGerd Hoffmann }
555*df887684SGerd Hoffmann 
556*df887684SGerd Hoffmann VncInfo2List *qmp_query_vnc_servers(Error **errp)
557*df887684SGerd Hoffmann {
558*df887684SGerd Hoffmann     VncInfo2List *item, *prev = NULL;
559*df887684SGerd Hoffmann     VncInfo2 *info;
560*df887684SGerd Hoffmann     VncDisplay *vd;
561*df887684SGerd Hoffmann     DeviceState *dev;
562*df887684SGerd Hoffmann 
563*df887684SGerd Hoffmann     QTAILQ_FOREACH(vd, &vnc_displays, next) {
564*df887684SGerd Hoffmann         info = g_new0(VncInfo2, 1);
565*df887684SGerd Hoffmann         info->id = g_strdup(vd->id);
566*df887684SGerd Hoffmann         info->clients = qmp_query_client_list(vd);
567*df887684SGerd Hoffmann         qmp_query_auth(vd, info);
568*df887684SGerd Hoffmann         if (vd->dcl.con) {
569*df887684SGerd Hoffmann             dev = DEVICE(object_property_get_link(OBJECT(vd->dcl.con),
570*df887684SGerd Hoffmann                                                   "device", NULL));
571*df887684SGerd Hoffmann             info->has_display = true;
572*df887684SGerd Hoffmann             info->display = g_strdup(dev->id);
573*df887684SGerd Hoffmann         }
574*df887684SGerd Hoffmann         if (vd->lsock != -1) {
575*df887684SGerd Hoffmann             info->server = qmp_query_server_entry(vd->lsock,
576*df887684SGerd Hoffmann                                                   info->server);
577*df887684SGerd Hoffmann         }
578*df887684SGerd Hoffmann #ifdef CONFIG_VNC_WS
579*df887684SGerd Hoffmann         if (vd->lwebsock != -1) {
580*df887684SGerd Hoffmann             /* TODO */
581*df887684SGerd Hoffmann         }
582*df887684SGerd Hoffmann #endif
583*df887684SGerd Hoffmann 
584*df887684SGerd Hoffmann         item = g_new0(VncInfo2List, 1);
585*df887684SGerd Hoffmann         item->value = info;
586*df887684SGerd Hoffmann         item->next = prev;
587*df887684SGerd Hoffmann         prev = item;
588*df887684SGerd Hoffmann     }
589*df887684SGerd Hoffmann     return prev;
590*df887684SGerd Hoffmann }
591*df887684SGerd Hoffmann 
5923e230dd2SCorentin Chary /* TODO
5933e230dd2SCorentin Chary    1) Get the queue working for IO.
5943e230dd2SCorentin Chary    2) there is some weirdness when using the -S option (the screen is grey
5953e230dd2SCorentin Chary       and not totally invalidated
5963e230dd2SCorentin Chary    3) resolutions > 1024
5973e230dd2SCorentin Chary */
5983e230dd2SCorentin Chary 
59938ee14f4SGerd Hoffmann static int vnc_update_client(VncState *vs, int has_dirty, bool sync);
6003e230dd2SCorentin Chary static void vnc_disconnect_start(VncState *vs);
6013e230dd2SCorentin Chary 
6023e230dd2SCorentin Chary static void vnc_colordepth(VncState *vs);
6033e230dd2SCorentin Chary static void framebuffer_update_request(VncState *vs, int incremental,
6043e230dd2SCorentin Chary                                        int x_position, int y_position,
6053e230dd2SCorentin Chary                                        int w, int h);
6060f7b2864SGerd Hoffmann static void vnc_refresh(DisplayChangeListener *dcl);
6073e230dd2SCorentin Chary static int vnc_refresh_server_surface(VncDisplay *vd);
6083e230dd2SCorentin Chary 
609bea60dd7SPeter Lieven static void vnc_set_area_dirty(DECLARE_BITMAP(dirty[VNC_MAX_HEIGHT],
610bea60dd7SPeter Lieven                                VNC_MAX_WIDTH / VNC_DIRTY_PIXELS_PER_BIT),
611bea60dd7SPeter Lieven                                int width, int height,
612bea60dd7SPeter Lieven                                int x, int y, int w, int h) {
61391937225SPeter Lieven     /* this is needed this to ensure we updated all affected
61491937225SPeter Lieven      * blocks if x % VNC_DIRTY_PIXELS_PER_BIT != 0 */
615b4c85ddcSPeter Lieven     w += (x % VNC_DIRTY_PIXELS_PER_BIT);
616b4c85ddcSPeter Lieven     x -= (x % VNC_DIRTY_PIXELS_PER_BIT);
6173e230dd2SCorentin Chary 
6189f64916dSGerd Hoffmann     x = MIN(x, width);
6199f64916dSGerd Hoffmann     y = MIN(y, height);
6209f64916dSGerd Hoffmann     w = MIN(x + w, width) - x;
62191937225SPeter Lieven     h = MIN(y + h, height);
6223e230dd2SCorentin Chary 
623b4c85ddcSPeter Lieven     for (; y < h; y++) {
624bea60dd7SPeter Lieven         bitmap_set(dirty[y], x / VNC_DIRTY_PIXELS_PER_BIT,
62591937225SPeter Lieven                    DIV_ROUND_UP(w, VNC_DIRTY_PIXELS_PER_BIT));
626b4c85ddcSPeter Lieven     }
6273e230dd2SCorentin Chary }
6283e230dd2SCorentin Chary 
629bea60dd7SPeter Lieven static void vnc_dpy_update(DisplayChangeListener *dcl,
630bea60dd7SPeter Lieven                            int x, int y, int w, int h)
631bea60dd7SPeter Lieven {
632bea60dd7SPeter Lieven     VncDisplay *vd = container_of(dcl, VncDisplay, dcl);
633bea60dd7SPeter Lieven     struct VncSurface *s = &vd->guest;
634bea60dd7SPeter Lieven     int width = pixman_image_get_width(vd->server);
635bea60dd7SPeter Lieven     int height = pixman_image_get_height(vd->server);
636bea60dd7SPeter Lieven 
637bea60dd7SPeter Lieven     vnc_set_area_dirty(s->dirty, width, height, x, y, w, h);
638bea60dd7SPeter Lieven }
639bea60dd7SPeter Lieven 
6403e230dd2SCorentin Chary void vnc_framebuffer_update(VncState *vs, int x, int y, int w, int h,
6413e230dd2SCorentin Chary                             int32_t encoding)
6423e230dd2SCorentin Chary {
6433e230dd2SCorentin Chary     vnc_write_u16(vs, x);
6443e230dd2SCorentin Chary     vnc_write_u16(vs, y);
6453e230dd2SCorentin Chary     vnc_write_u16(vs, w);
6463e230dd2SCorentin Chary     vnc_write_u16(vs, h);
6473e230dd2SCorentin Chary 
6483e230dd2SCorentin Chary     vnc_write_s32(vs, encoding);
6493e230dd2SCorentin Chary }
6503e230dd2SCorentin Chary 
6513e230dd2SCorentin Chary void buffer_reserve(Buffer *buffer, size_t len)
6523e230dd2SCorentin Chary {
6533e230dd2SCorentin Chary     if ((buffer->capacity - buffer->offset) < len) {
6543e230dd2SCorentin Chary         buffer->capacity += (len + 1024);
6557267c094SAnthony Liguori         buffer->buffer = g_realloc(buffer->buffer, buffer->capacity);
6563e230dd2SCorentin Chary         if (buffer->buffer == NULL) {
6573e230dd2SCorentin Chary             fprintf(stderr, "vnc: out of memory\n");
6583e230dd2SCorentin Chary             exit(1);
6593e230dd2SCorentin Chary         }
6603e230dd2SCorentin Chary     }
6613e230dd2SCorentin Chary }
6623e230dd2SCorentin Chary 
66371a8cdecSBlue Swirl static int buffer_empty(Buffer *buffer)
6643e230dd2SCorentin Chary {
6653e230dd2SCorentin Chary     return buffer->offset == 0;
6663e230dd2SCorentin Chary }
6673e230dd2SCorentin Chary 
6687536ee4bSTim Hardeck uint8_t *buffer_end(Buffer *buffer)
6693e230dd2SCorentin Chary {
6703e230dd2SCorentin Chary     return buffer->buffer + buffer->offset;
6713e230dd2SCorentin Chary }
6723e230dd2SCorentin Chary 
6733e230dd2SCorentin Chary void buffer_reset(Buffer *buffer)
6743e230dd2SCorentin Chary {
6753e230dd2SCorentin Chary         buffer->offset = 0;
6763e230dd2SCorentin Chary }
6773e230dd2SCorentin Chary 
6783e230dd2SCorentin Chary void buffer_free(Buffer *buffer)
6793e230dd2SCorentin Chary {
6807267c094SAnthony Liguori     g_free(buffer->buffer);
6813e230dd2SCorentin Chary     buffer->offset = 0;
6823e230dd2SCorentin Chary     buffer->capacity = 0;
6833e230dd2SCorentin Chary     buffer->buffer = NULL;
6843e230dd2SCorentin Chary }
6853e230dd2SCorentin Chary 
6863e230dd2SCorentin Chary void buffer_append(Buffer *buffer, const void *data, size_t len)
6873e230dd2SCorentin Chary {
6883e230dd2SCorentin Chary     memcpy(buffer->buffer + buffer->offset, data, len);
6893e230dd2SCorentin Chary     buffer->offset += len;
6903e230dd2SCorentin Chary }
6913e230dd2SCorentin Chary 
69232ed2680STim Hardeck void buffer_advance(Buffer *buf, size_t len)
69332ed2680STim Hardeck {
69432ed2680STim Hardeck     memmove(buf->buffer, buf->buffer + len,
69532ed2680STim Hardeck             (buf->offset - len));
69632ed2680STim Hardeck     buf->offset -= len;
69732ed2680STim Hardeck }
69832ed2680STim Hardeck 
6993e230dd2SCorentin Chary static void vnc_desktop_resize(VncState *vs)
7003e230dd2SCorentin Chary {
7013e230dd2SCorentin Chary     if (vs->csock == -1 || !vnc_has_feature(vs, VNC_FEATURE_RESIZE)) {
7023e230dd2SCorentin Chary         return;
7033e230dd2SCorentin Chary     }
704bea60dd7SPeter Lieven     if (vs->client_width == pixman_image_get_width(vs->vd->server) &&
705bea60dd7SPeter Lieven         vs->client_height == pixman_image_get_height(vs->vd->server)) {
7063e230dd2SCorentin Chary         return;
7073e230dd2SCorentin Chary     }
708bea60dd7SPeter Lieven     vs->client_width = pixman_image_get_width(vs->vd->server);
709bea60dd7SPeter Lieven     vs->client_height = pixman_image_get_height(vs->vd->server);
710bd023f95SCorentin Chary     vnc_lock_output(vs);
7113e230dd2SCorentin Chary     vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE);
7123e230dd2SCorentin Chary     vnc_write_u8(vs, 0);
7133e230dd2SCorentin Chary     vnc_write_u16(vs, 1); /* number of rects */
7143e230dd2SCorentin Chary     vnc_framebuffer_update(vs, 0, 0, vs->client_width, vs->client_height,
7153e230dd2SCorentin Chary                            VNC_ENCODING_DESKTOPRESIZE);
716bd023f95SCorentin Chary     vnc_unlock_output(vs);
7173e230dd2SCorentin Chary     vnc_flush(vs);
7183e230dd2SCorentin Chary }
7193e230dd2SCorentin Chary 
720bd023f95SCorentin Chary static void vnc_abort_display_jobs(VncDisplay *vd)
721bd023f95SCorentin Chary {
722bd023f95SCorentin Chary     VncState *vs;
723bd023f95SCorentin Chary 
724bd023f95SCorentin Chary     QTAILQ_FOREACH(vs, &vd->clients, next) {
725bd023f95SCorentin Chary         vnc_lock_output(vs);
726bd023f95SCorentin Chary         vs->abort = true;
727bd023f95SCorentin Chary         vnc_unlock_output(vs);
728bd023f95SCorentin Chary     }
729bd023f95SCorentin Chary     QTAILQ_FOREACH(vs, &vd->clients, next) {
730bd023f95SCorentin Chary         vnc_jobs_join(vs);
731bd023f95SCorentin Chary     }
732bd023f95SCorentin Chary     QTAILQ_FOREACH(vs, &vd->clients, next) {
733bd023f95SCorentin Chary         vnc_lock_output(vs);
734bd023f95SCorentin Chary         vs->abort = false;
735bd023f95SCorentin Chary         vnc_unlock_output(vs);
736bd023f95SCorentin Chary     }
737bd023f95SCorentin Chary }
738bd023f95SCorentin Chary 
7399f64916dSGerd Hoffmann int vnc_server_fb_stride(VncDisplay *vd)
7409f64916dSGerd Hoffmann {
7419f64916dSGerd Hoffmann     return pixman_image_get_stride(vd->server);
7429f64916dSGerd Hoffmann }
7439f64916dSGerd Hoffmann 
7449f64916dSGerd Hoffmann void *vnc_server_fb_ptr(VncDisplay *vd, int x, int y)
7459f64916dSGerd Hoffmann {
7469f64916dSGerd Hoffmann     uint8_t *ptr;
7479f64916dSGerd Hoffmann 
7489f64916dSGerd Hoffmann     ptr  = (uint8_t *)pixman_image_get_data(vd->server);
7499f64916dSGerd Hoffmann     ptr += y * vnc_server_fb_stride(vd);
7509f64916dSGerd Hoffmann     ptr += x * VNC_SERVER_FB_BYTES;
7519f64916dSGerd Hoffmann     return ptr;
7529f64916dSGerd Hoffmann }
7539f64916dSGerd Hoffmann 
754c12aeb86SGerd Hoffmann static void vnc_dpy_switch(DisplayChangeListener *dcl,
755c12aeb86SGerd Hoffmann                            DisplaySurface *surface)
7563e230dd2SCorentin Chary {
75721ef45d7SGerd Hoffmann     VncDisplay *vd = container_of(dcl, VncDisplay, dcl);
7583e230dd2SCorentin Chary     VncState *vs;
759bea60dd7SPeter Lieven     int width, height;
7603e230dd2SCorentin Chary 
761bd023f95SCorentin Chary     vnc_abort_display_jobs(vd);
762bd023f95SCorentin Chary 
7633e230dd2SCorentin Chary     /* server surface */
7649f64916dSGerd Hoffmann     qemu_pixman_image_unref(vd->server);
765d39fa6d8SGerd Hoffmann     vd->ds = surface;
766bea60dd7SPeter Lieven     width = MIN(VNC_MAX_WIDTH, ROUND_UP(surface_width(vd->ds),
767bea60dd7SPeter Lieven                                         VNC_DIRTY_PIXELS_PER_BIT));
768bea60dd7SPeter Lieven     height = MIN(VNC_MAX_HEIGHT, surface_height(vd->ds));
7699f64916dSGerd Hoffmann     vd->server = pixman_image_create_bits(VNC_SERVER_FB_FORMAT,
770bea60dd7SPeter Lieven                                           width, height, NULL, 0);
7713e230dd2SCorentin Chary 
7723e230dd2SCorentin Chary     /* guest surface */
7739f64916dSGerd Hoffmann #if 0 /* FIXME */
7743e230dd2SCorentin Chary     if (ds_get_bytes_per_pixel(ds) != vd->guest.ds->pf.bytes_per_pixel)
7753e230dd2SCorentin Chary         console_color_init(ds);
7769f64916dSGerd Hoffmann #endif
7779f64916dSGerd Hoffmann     qemu_pixman_image_unref(vd->guest.fb);
778d39fa6d8SGerd Hoffmann     vd->guest.fb = pixman_image_ref(surface->image);
779d39fa6d8SGerd Hoffmann     vd->guest.format = surface->format;
780bea60dd7SPeter Lieven     memset(vd->guest.dirty, 0x00, sizeof(vd->guest.dirty));
781bea60dd7SPeter Lieven     vnc_set_area_dirty(vd->guest.dirty, width, height, 0, 0,
782bea60dd7SPeter Lieven                        width, height);
7833e230dd2SCorentin Chary 
7843e230dd2SCorentin Chary     QTAILQ_FOREACH(vs, &vd->clients, next) {
7853e230dd2SCorentin Chary         vnc_colordepth(vs);
7863e230dd2SCorentin Chary         vnc_desktop_resize(vs);
7873e230dd2SCorentin Chary         if (vs->vd->cursor) {
7883e230dd2SCorentin Chary             vnc_cursor_define(vs);
7893e230dd2SCorentin Chary         }
790bea60dd7SPeter Lieven         memset(vs->dirty, 0x00, sizeof(vs->dirty));
791bea60dd7SPeter Lieven         vnc_set_area_dirty(vs->dirty, width, height, 0, 0,
792bea60dd7SPeter Lieven                            width, height);
7933e230dd2SCorentin Chary     }
7943e230dd2SCorentin Chary }
7953e230dd2SCorentin Chary 
7963e230dd2SCorentin Chary /* fastest code */
7979f64916dSGerd Hoffmann static void vnc_write_pixels_copy(VncState *vs,
7983e230dd2SCorentin Chary                                   void *pixels, int size)
7993e230dd2SCorentin Chary {
8003e230dd2SCorentin Chary     vnc_write(vs, pixels, size);
8013e230dd2SCorentin Chary }
8023e230dd2SCorentin Chary 
8033e230dd2SCorentin Chary /* slowest but generic code. */
8043e230dd2SCorentin Chary void vnc_convert_pixel(VncState *vs, uint8_t *buf, uint32_t v)
8053e230dd2SCorentin Chary {
8063e230dd2SCorentin Chary     uint8_t r, g, b;
8073e230dd2SCorentin Chary 
8089f64916dSGerd Hoffmann #if VNC_SERVER_FB_FORMAT == PIXMAN_FORMAT(32, PIXMAN_TYPE_ARGB, 0, 8, 8, 8)
8099f64916dSGerd Hoffmann     r = (((v & 0x00ff0000) >> 16) << vs->client_pf.rbits) >> 8;
8109f64916dSGerd Hoffmann     g = (((v & 0x0000ff00) >>  8) << vs->client_pf.gbits) >> 8;
8119f64916dSGerd Hoffmann     b = (((v & 0x000000ff) >>  0) << vs->client_pf.bbits) >> 8;
8129f64916dSGerd Hoffmann #else
8139f64916dSGerd Hoffmann # error need some bits here if you change VNC_SERVER_FB_FORMAT
8149f64916dSGerd Hoffmann #endif
8159f64916dSGerd Hoffmann     v = (r << vs->client_pf.rshift) |
8169f64916dSGerd Hoffmann         (g << vs->client_pf.gshift) |
8179f64916dSGerd Hoffmann         (b << vs->client_pf.bshift);
8189f64916dSGerd Hoffmann     switch (vs->client_pf.bytes_per_pixel) {
8193e230dd2SCorentin Chary     case 1:
8203e230dd2SCorentin Chary         buf[0] = v;
8213e230dd2SCorentin Chary         break;
8223e230dd2SCorentin Chary     case 2:
8239f64916dSGerd Hoffmann         if (vs->client_be) {
8243e230dd2SCorentin Chary             buf[0] = v >> 8;
8253e230dd2SCorentin Chary             buf[1] = v;
8263e230dd2SCorentin Chary         } else {
8273e230dd2SCorentin Chary             buf[1] = v >> 8;
8283e230dd2SCorentin Chary             buf[0] = v;
8293e230dd2SCorentin Chary         }
8303e230dd2SCorentin Chary         break;
8313e230dd2SCorentin Chary     default:
8323e230dd2SCorentin Chary     case 4:
8339f64916dSGerd Hoffmann         if (vs->client_be) {
8343e230dd2SCorentin Chary             buf[0] = v >> 24;
8353e230dd2SCorentin Chary             buf[1] = v >> 16;
8363e230dd2SCorentin Chary             buf[2] = v >> 8;
8373e230dd2SCorentin Chary             buf[3] = v;
8383e230dd2SCorentin Chary         } else {
8393e230dd2SCorentin Chary             buf[3] = v >> 24;
8403e230dd2SCorentin Chary             buf[2] = v >> 16;
8413e230dd2SCorentin Chary             buf[1] = v >> 8;
8423e230dd2SCorentin Chary             buf[0] = v;
8433e230dd2SCorentin Chary         }
8443e230dd2SCorentin Chary         break;
8453e230dd2SCorentin Chary     }
8463e230dd2SCorentin Chary }
8473e230dd2SCorentin Chary 
8489f64916dSGerd Hoffmann static void vnc_write_pixels_generic(VncState *vs,
8493e230dd2SCorentin Chary                                      void *pixels1, int size)
8503e230dd2SCorentin Chary {
8513e230dd2SCorentin Chary     uint8_t buf[4];
8523e230dd2SCorentin Chary 
8539f64916dSGerd Hoffmann     if (VNC_SERVER_FB_BYTES == 4) {
8543e230dd2SCorentin Chary         uint32_t *pixels = pixels1;
8553e230dd2SCorentin Chary         int n, i;
8563e230dd2SCorentin Chary         n = size >> 2;
8573e230dd2SCorentin Chary         for (i = 0; i < n; i++) {
8583e230dd2SCorentin Chary             vnc_convert_pixel(vs, buf, pixels[i]);
8599f64916dSGerd Hoffmann             vnc_write(vs, buf, vs->client_pf.bytes_per_pixel);
8603e230dd2SCorentin Chary         }
8613e230dd2SCorentin Chary     }
8623e230dd2SCorentin Chary }
8633e230dd2SCorentin Chary 
8643e230dd2SCorentin Chary int vnc_raw_send_framebuffer_update(VncState *vs, int x, int y, int w, int h)
8653e230dd2SCorentin Chary {
8663e230dd2SCorentin Chary     int i;
8673e230dd2SCorentin Chary     uint8_t *row;
8683e230dd2SCorentin Chary     VncDisplay *vd = vs->vd;
8693e230dd2SCorentin Chary 
8709f64916dSGerd Hoffmann     row = vnc_server_fb_ptr(vd, x, y);
8713e230dd2SCorentin Chary     for (i = 0; i < h; i++) {
8729f64916dSGerd Hoffmann         vs->write_pixels(vs, row, w * VNC_SERVER_FB_BYTES);
8739f64916dSGerd Hoffmann         row += vnc_server_fb_stride(vd);
8743e230dd2SCorentin Chary     }
8753e230dd2SCorentin Chary     return 1;
8763e230dd2SCorentin Chary }
8773e230dd2SCorentin Chary 
878bd023f95SCorentin Chary int vnc_send_framebuffer_update(VncState *vs, int x, int y, int w, int h)
8793e230dd2SCorentin Chary {
8803e230dd2SCorentin Chary     int n = 0;
8813e230dd2SCorentin Chary 
8823e230dd2SCorentin Chary     switch(vs->vnc_encoding) {
8833e230dd2SCorentin Chary         case VNC_ENCODING_ZLIB:
8843e230dd2SCorentin Chary             n = vnc_zlib_send_framebuffer_update(vs, x, y, w, h);
8853e230dd2SCorentin Chary             break;
8863e230dd2SCorentin Chary         case VNC_ENCODING_HEXTILE:
8873e230dd2SCorentin Chary             vnc_framebuffer_update(vs, x, y, w, h, VNC_ENCODING_HEXTILE);
8883e230dd2SCorentin Chary             n = vnc_hextile_send_framebuffer_update(vs, x, y, w, h);
8893e230dd2SCorentin Chary             break;
8903e230dd2SCorentin Chary         case VNC_ENCODING_TIGHT:
8913e230dd2SCorentin Chary             n = vnc_tight_send_framebuffer_update(vs, x, y, w, h);
8923e230dd2SCorentin Chary             break;
893efe556adSCorentin Chary         case VNC_ENCODING_TIGHT_PNG:
894efe556adSCorentin Chary             n = vnc_tight_png_send_framebuffer_update(vs, x, y, w, h);
895efe556adSCorentin Chary             break;
896148954faSCorentin Chary         case VNC_ENCODING_ZRLE:
897148954faSCorentin Chary             n = vnc_zrle_send_framebuffer_update(vs, x, y, w, h);
898148954faSCorentin Chary             break;
899148954faSCorentin Chary         case VNC_ENCODING_ZYWRLE:
900148954faSCorentin Chary             n = vnc_zywrle_send_framebuffer_update(vs, x, y, w, h);
901148954faSCorentin Chary             break;
9023e230dd2SCorentin Chary         default:
9033e230dd2SCorentin Chary             vnc_framebuffer_update(vs, x, y, w, h, VNC_ENCODING_RAW);
9043e230dd2SCorentin Chary             n = vnc_raw_send_framebuffer_update(vs, x, y, w, h);
9053e230dd2SCorentin Chary             break;
9063e230dd2SCorentin Chary     }
9073e230dd2SCorentin Chary     return n;
9083e230dd2SCorentin Chary }
9093e230dd2SCorentin Chary 
9103e230dd2SCorentin Chary static void vnc_copy(VncState *vs, int src_x, int src_y, int dst_x, int dst_y, int w, int h)
9113e230dd2SCorentin Chary {
9123e230dd2SCorentin Chary     /* send bitblit op to the vnc client */
913bd023f95SCorentin Chary     vnc_lock_output(vs);
9143e230dd2SCorentin Chary     vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE);
9153e230dd2SCorentin Chary     vnc_write_u8(vs, 0);
9163e230dd2SCorentin Chary     vnc_write_u16(vs, 1); /* number of rects */
9173e230dd2SCorentin Chary     vnc_framebuffer_update(vs, dst_x, dst_y, w, h, VNC_ENCODING_COPYRECT);
9183e230dd2SCorentin Chary     vnc_write_u16(vs, src_x);
9193e230dd2SCorentin Chary     vnc_write_u16(vs, src_y);
920bd023f95SCorentin Chary     vnc_unlock_output(vs);
9213e230dd2SCorentin Chary     vnc_flush(vs);
9223e230dd2SCorentin Chary }
9233e230dd2SCorentin Chary 
9247c20b4a3SGerd Hoffmann static void vnc_dpy_copy(DisplayChangeListener *dcl,
9257c20b4a3SGerd Hoffmann                          int src_x, int src_y,
9267c20b4a3SGerd Hoffmann                          int dst_x, int dst_y, int w, int h)
9273e230dd2SCorentin Chary {
92821ef45d7SGerd Hoffmann     VncDisplay *vd = container_of(dcl, VncDisplay, dcl);
9293e230dd2SCorentin Chary     VncState *vs, *vn;
9303e230dd2SCorentin Chary     uint8_t *src_row;
9313e230dd2SCorentin Chary     uint8_t *dst_row;
9329f64916dSGerd Hoffmann     int i, x, y, pitch, inc, w_lim, s;
9333e230dd2SCorentin Chary     int cmp_bytes;
9343e230dd2SCorentin Chary 
9353e230dd2SCorentin Chary     vnc_refresh_server_surface(vd);
9363e230dd2SCorentin Chary     QTAILQ_FOREACH_SAFE(vs, &vd->clients, next, vn) {
9373e230dd2SCorentin Chary         if (vnc_has_feature(vs, VNC_FEATURE_COPYRECT)) {
9383e230dd2SCorentin Chary             vs->force_update = 1;
93938ee14f4SGerd Hoffmann             vnc_update_client(vs, 1, true);
9403e230dd2SCorentin Chary             /* vs might be free()ed here */
9413e230dd2SCorentin Chary         }
9423e230dd2SCorentin Chary     }
9433e230dd2SCorentin Chary 
9443e230dd2SCorentin Chary     /* do bitblit op on the local surface too */
9459f64916dSGerd Hoffmann     pitch = vnc_server_fb_stride(vd);
9469f64916dSGerd Hoffmann     src_row = vnc_server_fb_ptr(vd, src_x, src_y);
9479f64916dSGerd Hoffmann     dst_row = vnc_server_fb_ptr(vd, dst_x, dst_y);
9483e230dd2SCorentin Chary     y = dst_y;
9493e230dd2SCorentin Chary     inc = 1;
9503e230dd2SCorentin Chary     if (dst_y > src_y) {
9513e230dd2SCorentin Chary         /* copy backwards */
9523e230dd2SCorentin Chary         src_row += pitch * (h-1);
9533e230dd2SCorentin Chary         dst_row += pitch * (h-1);
9543e230dd2SCorentin Chary         pitch = -pitch;
9553e230dd2SCorentin Chary         y = dst_y + h - 1;
9563e230dd2SCorentin Chary         inc = -1;
9573e230dd2SCorentin Chary     }
958b4c85ddcSPeter Lieven     w_lim = w - (VNC_DIRTY_PIXELS_PER_BIT - (dst_x % VNC_DIRTY_PIXELS_PER_BIT));
959b4c85ddcSPeter Lieven     if (w_lim < 0) {
9603e230dd2SCorentin Chary         w_lim = w;
961b4c85ddcSPeter Lieven     } else {
962b4c85ddcSPeter Lieven         w_lim = w - (w_lim % VNC_DIRTY_PIXELS_PER_BIT);
963b4c85ddcSPeter Lieven     }
9643e230dd2SCorentin Chary     for (i = 0; i < h; i++) {
9653e230dd2SCorentin Chary         for (x = 0; x <= w_lim;
9663e230dd2SCorentin Chary                 x += s, src_row += cmp_bytes, dst_row += cmp_bytes) {
9673e230dd2SCorentin Chary             if (x == w_lim) {
9683e230dd2SCorentin Chary                 if ((s = w - w_lim) == 0)
9693e230dd2SCorentin Chary                     break;
9703e230dd2SCorentin Chary             } else if (!x) {
971b4c85ddcSPeter Lieven                 s = (VNC_DIRTY_PIXELS_PER_BIT -
972b4c85ddcSPeter Lieven                     (dst_x % VNC_DIRTY_PIXELS_PER_BIT));
9733e230dd2SCorentin Chary                 s = MIN(s, w_lim);
9743e230dd2SCorentin Chary             } else {
975b4c85ddcSPeter Lieven                 s = VNC_DIRTY_PIXELS_PER_BIT;
9763e230dd2SCorentin Chary             }
9779f64916dSGerd Hoffmann             cmp_bytes = s * VNC_SERVER_FB_BYTES;
9783e230dd2SCorentin Chary             if (memcmp(src_row, dst_row, cmp_bytes) == 0)
9793e230dd2SCorentin Chary                 continue;
9803e230dd2SCorentin Chary             memmove(dst_row, src_row, cmp_bytes);
9813e230dd2SCorentin Chary             QTAILQ_FOREACH(vs, &vd->clients, next) {
9823e230dd2SCorentin Chary                 if (!vnc_has_feature(vs, VNC_FEATURE_COPYRECT)) {
983b4c85ddcSPeter Lieven                     set_bit(((x + dst_x) / VNC_DIRTY_PIXELS_PER_BIT),
984b4c85ddcSPeter Lieven                             vs->dirty[y]);
9853e230dd2SCorentin Chary                 }
9863e230dd2SCorentin Chary             }
9873e230dd2SCorentin Chary         }
9889f64916dSGerd Hoffmann         src_row += pitch - w * VNC_SERVER_FB_BYTES;
9899f64916dSGerd Hoffmann         dst_row += pitch - w * VNC_SERVER_FB_BYTES;
9903e230dd2SCorentin Chary         y += inc;
9913e230dd2SCorentin Chary     }
9923e230dd2SCorentin Chary 
9933e230dd2SCorentin Chary     QTAILQ_FOREACH(vs, &vd->clients, next) {
9943e230dd2SCorentin Chary         if (vnc_has_feature(vs, VNC_FEATURE_COPYRECT)) {
9953e230dd2SCorentin Chary             vnc_copy(vs, src_x, src_y, dst_x, dst_y, w, h);
9963e230dd2SCorentin Chary         }
9973e230dd2SCorentin Chary     }
9983e230dd2SCorentin Chary }
9993e230dd2SCorentin Chary 
10007c20b4a3SGerd Hoffmann static void vnc_mouse_set(DisplayChangeListener *dcl,
10017c20b4a3SGerd Hoffmann                           int x, int y, int visible)
10023e230dd2SCorentin Chary {
10033e230dd2SCorentin Chary     /* can we ask the client(s) to move the pointer ??? */
10043e230dd2SCorentin Chary }
10053e230dd2SCorentin Chary 
10063e230dd2SCorentin Chary static int vnc_cursor_define(VncState *vs)
10073e230dd2SCorentin Chary {
10083e230dd2SCorentin Chary     QEMUCursor *c = vs->vd->cursor;
10093e230dd2SCorentin Chary     int isize;
10103e230dd2SCorentin Chary 
10113e230dd2SCorentin Chary     if (vnc_has_feature(vs, VNC_FEATURE_RICH_CURSOR)) {
1012d01f9595SCorentin Chary         vnc_lock_output(vs);
10133e230dd2SCorentin Chary         vnc_write_u8(vs,  VNC_MSG_SERVER_FRAMEBUFFER_UPDATE);
10143e230dd2SCorentin Chary         vnc_write_u8(vs,  0);  /*  padding     */
10153e230dd2SCorentin Chary         vnc_write_u16(vs, 1);  /*  # of rects  */
10163e230dd2SCorentin Chary         vnc_framebuffer_update(vs, c->hot_x, c->hot_y, c->width, c->height,
10173e230dd2SCorentin Chary                                VNC_ENCODING_RICH_CURSOR);
10189f64916dSGerd Hoffmann         isize = c->width * c->height * vs->client_pf.bytes_per_pixel;
10199f64916dSGerd Hoffmann         vnc_write_pixels_generic(vs, c->data, isize);
10203e230dd2SCorentin Chary         vnc_write(vs, vs->vd->cursor_mask, vs->vd->cursor_msize);
1021d01f9595SCorentin Chary         vnc_unlock_output(vs);
10223e230dd2SCorentin Chary         return 0;
10233e230dd2SCorentin Chary     }
10243e230dd2SCorentin Chary     return -1;
10253e230dd2SCorentin Chary }
10263e230dd2SCorentin Chary 
10277c20b4a3SGerd Hoffmann static void vnc_dpy_cursor_define(DisplayChangeListener *dcl,
10287c20b4a3SGerd Hoffmann                                   QEMUCursor *c)
10293e230dd2SCorentin Chary {
1030d616ccc5SGerd Hoffmann     VncDisplay *vd = container_of(dcl, VncDisplay, dcl);
10313e230dd2SCorentin Chary     VncState *vs;
10323e230dd2SCorentin Chary 
10333e230dd2SCorentin Chary     cursor_put(vd->cursor);
10347267c094SAnthony Liguori     g_free(vd->cursor_mask);
10353e230dd2SCorentin Chary 
10363e230dd2SCorentin Chary     vd->cursor = c;
10373e230dd2SCorentin Chary     cursor_get(vd->cursor);
10383e230dd2SCorentin Chary     vd->cursor_msize = cursor_get_mono_bpl(c) * c->height;
10397267c094SAnthony Liguori     vd->cursor_mask = g_malloc0(vd->cursor_msize);
10403e230dd2SCorentin Chary     cursor_get_mono_mask(c, 0, vd->cursor_mask);
10413e230dd2SCorentin Chary 
10423e230dd2SCorentin Chary     QTAILQ_FOREACH(vs, &vd->clients, next) {
10433e230dd2SCorentin Chary         vnc_cursor_define(vs);
10443e230dd2SCorentin Chary     }
10453e230dd2SCorentin Chary }
10463e230dd2SCorentin Chary 
10473e230dd2SCorentin Chary static int find_and_clear_dirty_height(struct VncState *vs,
10486c71a539SCorentin Chary                                        int y, int last_x, int x, int height)
10493e230dd2SCorentin Chary {
10503e230dd2SCorentin Chary     int h;
10513e230dd2SCorentin Chary 
10526c71a539SCorentin Chary     for (h = 1; h < (height - y); h++) {
1053bc2429b9SCorentin Chary         if (!test_bit(last_x, vs->dirty[y + h])) {
10543e230dd2SCorentin Chary             break;
1055bc2429b9SCorentin Chary         }
1056863d7c91SPeter Lieven         bitmap_clear(vs->dirty[y + h], last_x, x - last_x);
10573e230dd2SCorentin Chary     }
10583e230dd2SCorentin Chary 
10593e230dd2SCorentin Chary     return h;
10603e230dd2SCorentin Chary }
10613e230dd2SCorentin Chary 
106238ee14f4SGerd Hoffmann static int vnc_update_client(VncState *vs, int has_dirty, bool sync)
10633e230dd2SCorentin Chary {
106463658280SGerd Hoffmann     vs->has_dirty += has_dirty;
10653e230dd2SCorentin Chary     if (vs->need_update && vs->csock != -1) {
10663e230dd2SCorentin Chary         VncDisplay *vd = vs->vd;
1067bd023f95SCorentin Chary         VncJob *job;
10683e230dd2SCorentin Chary         int y;
10692f487a3dSPeter Lieven         int height, width;
1070bd023f95SCorentin Chary         int n = 0;
1071bd023f95SCorentin Chary 
10723e230dd2SCorentin Chary         if (vs->output.offset && !vs->audio_cap && !vs->force_update)
10733e230dd2SCorentin Chary             /* kernel send buffers are full -> drop frames to throttle */
10743e230dd2SCorentin Chary             return 0;
10753e230dd2SCorentin Chary 
107663658280SGerd Hoffmann         if (!vs->has_dirty && !vs->audio_cap && !vs->force_update)
10773e230dd2SCorentin Chary             return 0;
10783e230dd2SCorentin Chary 
10793e230dd2SCorentin Chary         /*
10803e230dd2SCorentin Chary          * Send screen updates to the vnc client using the server
10813e230dd2SCorentin Chary          * surface and server dirty map.  guest surface updates
10823e230dd2SCorentin Chary          * happening in parallel don't disturb us, the next pass will
10833e230dd2SCorentin Chary          * send them to the client.
10843e230dd2SCorentin Chary          */
1085bd023f95SCorentin Chary         job = vnc_job_new(vs);
10863e230dd2SCorentin Chary 
1087bea60dd7SPeter Lieven         height = pixman_image_get_height(vd->server);
1088bea60dd7SPeter Lieven         width = pixman_image_get_width(vd->server);
10893e230dd2SCorentin Chary 
109012b316d4SPeter Lieven         y = 0;
109112b316d4SPeter Lieven         for (;;) {
109212b316d4SPeter Lieven             int x, h;
109312b316d4SPeter Lieven             unsigned long x2;
109412b316d4SPeter Lieven             unsigned long offset = find_next_bit((unsigned long *) &vs->dirty,
109512b316d4SPeter Lieven                                                  height * VNC_DIRTY_BPL(vs),
109612b316d4SPeter Lieven                                                  y * VNC_DIRTY_BPL(vs));
109712b316d4SPeter Lieven             if (offset == height * VNC_DIRTY_BPL(vs)) {
109812b316d4SPeter Lieven                 /* no more dirty bits */
109912b316d4SPeter Lieven                 break;
11003e230dd2SCorentin Chary             }
110112b316d4SPeter Lieven             y = offset / VNC_DIRTY_BPL(vs);
110212b316d4SPeter Lieven             x = offset % VNC_DIRTY_BPL(vs);
110312b316d4SPeter Lieven             x2 = find_next_zero_bit((unsigned long *) &vs->dirty[y],
110412b316d4SPeter Lieven                                     VNC_DIRTY_BPL(vs), x);
110512b316d4SPeter Lieven             bitmap_clear(vs->dirty[y], x, x2 - x);
110612b316d4SPeter Lieven             h = find_and_clear_dirty_height(vs, y, x, x2, height);
11072f487a3dSPeter Lieven             x2 = MIN(x2, width / VNC_DIRTY_PIXELS_PER_BIT);
11082f487a3dSPeter Lieven             if (x2 > x) {
110912b316d4SPeter Lieven                 n += vnc_job_add_rect(job, x * VNC_DIRTY_PIXELS_PER_BIT, y,
111012b316d4SPeter Lieven                                       (x2 - x) * VNC_DIRTY_PIXELS_PER_BIT, h);
11113e230dd2SCorentin Chary             }
11122f487a3dSPeter Lieven         }
1113bd023f95SCorentin Chary 
1114bd023f95SCorentin Chary         vnc_job_push(job);
1115eb214ff8SGerd Hoffmann         if (sync) {
1116eb214ff8SGerd Hoffmann             vnc_jobs_join(vs);
1117eb214ff8SGerd Hoffmann         }
11183e230dd2SCorentin Chary         vs->force_update = 0;
111963658280SGerd Hoffmann         vs->has_dirty = 0;
1120bd023f95SCorentin Chary         return n;
11213e230dd2SCorentin Chary     }
11223e230dd2SCorentin Chary 
112338ee14f4SGerd Hoffmann     if (vs->csock == -1) {
11243e230dd2SCorentin Chary         vnc_disconnect_finish(vs);
112538ee14f4SGerd Hoffmann     } else if (sync) {
112638ee14f4SGerd Hoffmann         vnc_jobs_join(vs);
112738ee14f4SGerd Hoffmann     }
11283e230dd2SCorentin Chary 
11293e230dd2SCorentin Chary     return 0;
11303e230dd2SCorentin Chary }
11313e230dd2SCorentin Chary 
11323e230dd2SCorentin Chary /* audio */
11333e230dd2SCorentin Chary static void audio_capture_notify(void *opaque, audcnotification_e cmd)
11343e230dd2SCorentin Chary {
11353e230dd2SCorentin Chary     VncState *vs = opaque;
11363e230dd2SCorentin Chary 
11373e230dd2SCorentin Chary     switch (cmd) {
11383e230dd2SCorentin Chary     case AUD_CNOTIFY_DISABLE:
1139bd023f95SCorentin Chary         vnc_lock_output(vs);
11403e230dd2SCorentin Chary         vnc_write_u8(vs, VNC_MSG_SERVER_QEMU);
11413e230dd2SCorentin Chary         vnc_write_u8(vs, VNC_MSG_SERVER_QEMU_AUDIO);
11423e230dd2SCorentin Chary         vnc_write_u16(vs, VNC_MSG_SERVER_QEMU_AUDIO_END);
1143bd023f95SCorentin Chary         vnc_unlock_output(vs);
11443e230dd2SCorentin Chary         vnc_flush(vs);
11453e230dd2SCorentin Chary         break;
11463e230dd2SCorentin Chary 
11473e230dd2SCorentin Chary     case AUD_CNOTIFY_ENABLE:
1148bd023f95SCorentin Chary         vnc_lock_output(vs);
11493e230dd2SCorentin Chary         vnc_write_u8(vs, VNC_MSG_SERVER_QEMU);
11503e230dd2SCorentin Chary         vnc_write_u8(vs, VNC_MSG_SERVER_QEMU_AUDIO);
11513e230dd2SCorentin Chary         vnc_write_u16(vs, VNC_MSG_SERVER_QEMU_AUDIO_BEGIN);
1152bd023f95SCorentin Chary         vnc_unlock_output(vs);
11533e230dd2SCorentin Chary         vnc_flush(vs);
11543e230dd2SCorentin Chary         break;
11553e230dd2SCorentin Chary     }
11563e230dd2SCorentin Chary }
11573e230dd2SCorentin Chary 
11583e230dd2SCorentin Chary static void audio_capture_destroy(void *opaque)
11593e230dd2SCorentin Chary {
11603e230dd2SCorentin Chary }
11613e230dd2SCorentin Chary 
11623e230dd2SCorentin Chary static void audio_capture(void *opaque, void *buf, int size)
11633e230dd2SCorentin Chary {
11643e230dd2SCorentin Chary     VncState *vs = opaque;
11653e230dd2SCorentin Chary 
1166bd023f95SCorentin Chary     vnc_lock_output(vs);
11673e230dd2SCorentin Chary     vnc_write_u8(vs, VNC_MSG_SERVER_QEMU);
11683e230dd2SCorentin Chary     vnc_write_u8(vs, VNC_MSG_SERVER_QEMU_AUDIO);
11693e230dd2SCorentin Chary     vnc_write_u16(vs, VNC_MSG_SERVER_QEMU_AUDIO_DATA);
11703e230dd2SCorentin Chary     vnc_write_u32(vs, size);
11713e230dd2SCorentin Chary     vnc_write(vs, buf, size);
1172bd023f95SCorentin Chary     vnc_unlock_output(vs);
11733e230dd2SCorentin Chary     vnc_flush(vs);
11743e230dd2SCorentin Chary }
11753e230dd2SCorentin Chary 
11763e230dd2SCorentin Chary static void audio_add(VncState *vs)
11773e230dd2SCorentin Chary {
11783e230dd2SCorentin Chary     struct audio_capture_ops ops;
11793e230dd2SCorentin Chary 
11803e230dd2SCorentin Chary     if (vs->audio_cap) {
1181027a79c3SCole Robinson         error_report("audio already running");
11823e230dd2SCorentin Chary         return;
11833e230dd2SCorentin Chary     }
11843e230dd2SCorentin Chary 
11853e230dd2SCorentin Chary     ops.notify = audio_capture_notify;
11863e230dd2SCorentin Chary     ops.destroy = audio_capture_destroy;
11873e230dd2SCorentin Chary     ops.capture = audio_capture;
11883e230dd2SCorentin Chary 
11893e230dd2SCorentin Chary     vs->audio_cap = AUD_add_capture(&vs->as, &ops, vs);
11903e230dd2SCorentin Chary     if (!vs->audio_cap) {
1191027a79c3SCole Robinson         error_report("Failed to add audio capture");
11923e230dd2SCorentin Chary     }
11933e230dd2SCorentin Chary }
11943e230dd2SCorentin Chary 
11953e230dd2SCorentin Chary static void audio_del(VncState *vs)
11963e230dd2SCorentin Chary {
11973e230dd2SCorentin Chary     if (vs->audio_cap) {
11983e230dd2SCorentin Chary         AUD_del_capture(vs->audio_cap, vs);
11993e230dd2SCorentin Chary         vs->audio_cap = NULL;
12003e230dd2SCorentin Chary     }
12013e230dd2SCorentin Chary }
12023e230dd2SCorentin Chary 
12033e230dd2SCorentin Chary static void vnc_disconnect_start(VncState *vs)
12043e230dd2SCorentin Chary {
12053e230dd2SCorentin Chary     if (vs->csock == -1)
12063e230dd2SCorentin Chary         return;
12078cf36489SGerd Hoffmann     vnc_set_share_mode(vs, VNC_SHARE_MODE_DISCONNECTED);
12083e230dd2SCorentin Chary     qemu_set_fd_handler2(vs->csock, NULL, NULL, NULL, NULL);
12093e230dd2SCorentin Chary     closesocket(vs->csock);
12103e230dd2SCorentin Chary     vs->csock = -1;
12113e230dd2SCorentin Chary }
12123e230dd2SCorentin Chary 
12137536ee4bSTim Hardeck void vnc_disconnect_finish(VncState *vs)
12143e230dd2SCorentin Chary {
12157d964c9dSCorentin Chary     int i;
12167d964c9dSCorentin Chary 
1217bd023f95SCorentin Chary     vnc_jobs_join(vs); /* Wait encoding jobs */
1218bd023f95SCorentin Chary 
1219bd023f95SCorentin Chary     vnc_lock_output(vs);
1220fb6ba0d5SWenchao Xia     vnc_qmp_event(vs, QAPI_EVENT_VNC_DISCONNECTED);
12213e230dd2SCorentin Chary 
12223e230dd2SCorentin Chary     buffer_free(&vs->input);
12233e230dd2SCorentin Chary     buffer_free(&vs->output);
12247536ee4bSTim Hardeck #ifdef CONFIG_VNC_WS
12257536ee4bSTim Hardeck     buffer_free(&vs->ws_input);
12267536ee4bSTim Hardeck     buffer_free(&vs->ws_output);
12277536ee4bSTim Hardeck #endif /* CONFIG_VNC_WS */
12283e230dd2SCorentin Chary 
1229fb6ba0d5SWenchao Xia     qapi_free_VncClientInfo(vs->info);
12303e230dd2SCorentin Chary 
12313e230dd2SCorentin Chary     vnc_zlib_clear(vs);
12323e230dd2SCorentin Chary     vnc_tight_clear(vs);
1233148954faSCorentin Chary     vnc_zrle_clear(vs);
12343e230dd2SCorentin Chary 
12353e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS
12363e230dd2SCorentin Chary     vnc_tls_client_cleanup(vs);
12373e230dd2SCorentin Chary #endif /* CONFIG_VNC_TLS */
12383e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL
12393e230dd2SCorentin Chary     vnc_sasl_client_cleanup(vs);
12403e230dd2SCorentin Chary #endif /* CONFIG_VNC_SASL */
12413e230dd2SCorentin Chary     audio_del(vs);
12427bc9318bSGerd Hoffmann     vnc_release_modifiers(vs);
12433e230dd2SCorentin Chary 
12446fd8e79aSTim Hardeck     if (vs->initialized) {
12453e230dd2SCorentin Chary         QTAILQ_REMOVE(&vs->vd->clients, vs, next);
12466fd8e79aSTim Hardeck         qemu_remove_mouse_mode_change_notifier(&vs->mouse_mode_notifier);
12476fd8e79aSTim Hardeck     }
12483e230dd2SCorentin Chary 
12493e230dd2SCorentin Chary     if (vs->vd->lock_key_sync)
12503e230dd2SCorentin Chary         qemu_remove_led_event_handler(vs->led);
1251bd023f95SCorentin Chary     vnc_unlock_output(vs);
1252bd023f95SCorentin Chary 
1253bd023f95SCorentin Chary     qemu_mutex_destroy(&vs->output_mutex);
12546fd8e79aSTim Hardeck     if (vs->bh != NULL) {
1255175b2a6eSCorentin Chary         qemu_bh_delete(vs->bh);
12566fd8e79aSTim Hardeck     }
1257175b2a6eSCorentin Chary     buffer_free(&vs->jobs_buffer);
1258175b2a6eSCorentin Chary 
12597d964c9dSCorentin Chary     for (i = 0; i < VNC_STAT_ROWS; ++i) {
12607267c094SAnthony Liguori         g_free(vs->lossy_rect[i]);
12617d964c9dSCorentin Chary     }
12627267c094SAnthony Liguori     g_free(vs->lossy_rect);
12637267c094SAnthony Liguori     g_free(vs);
12643e230dd2SCorentin Chary }
12653e230dd2SCorentin Chary 
12663e230dd2SCorentin Chary int vnc_client_io_error(VncState *vs, int ret, int last_errno)
12673e230dd2SCorentin Chary {
12683e230dd2SCorentin Chary     if (ret == 0 || ret == -1) {
12693e230dd2SCorentin Chary         if (ret == -1) {
12703e230dd2SCorentin Chary             switch (last_errno) {
12713e230dd2SCorentin Chary                 case EINTR:
12723e230dd2SCorentin Chary                 case EAGAIN:
12733e230dd2SCorentin Chary #ifdef _WIN32
12743e230dd2SCorentin Chary                 case WSAEWOULDBLOCK:
12753e230dd2SCorentin Chary #endif
12763e230dd2SCorentin Chary                     return 0;
12773e230dd2SCorentin Chary                 default:
12783e230dd2SCorentin Chary                     break;
12793e230dd2SCorentin Chary             }
12803e230dd2SCorentin Chary         }
12813e230dd2SCorentin Chary 
12823e230dd2SCorentin Chary         VNC_DEBUG("Closing down client sock: ret %d, errno %d\n",
12833e230dd2SCorentin Chary                   ret, ret < 0 ? last_errno : 0);
12843e230dd2SCorentin Chary         vnc_disconnect_start(vs);
12853e230dd2SCorentin Chary 
12863e230dd2SCorentin Chary         return 0;
12873e230dd2SCorentin Chary     }
12883e230dd2SCorentin Chary     return ret;
12893e230dd2SCorentin Chary }
12903e230dd2SCorentin Chary 
12913e230dd2SCorentin Chary 
12923e230dd2SCorentin Chary void vnc_client_error(VncState *vs)
12933e230dd2SCorentin Chary {
12943e230dd2SCorentin Chary     VNC_DEBUG("Closing down client sock: protocol error\n");
12953e230dd2SCorentin Chary     vnc_disconnect_start(vs);
12963e230dd2SCorentin Chary }
12973e230dd2SCorentin Chary 
12980057a0d5STim Hardeck #ifdef CONFIG_VNC_TLS
12990057a0d5STim Hardeck static long vnc_client_write_tls(gnutls_session_t *session,
13000057a0d5STim Hardeck                                  const uint8_t *data,
13010057a0d5STim Hardeck                                  size_t datalen)
13020057a0d5STim Hardeck {
13030057a0d5STim Hardeck     long ret = gnutls_write(*session, data, datalen);
13040057a0d5STim Hardeck     if (ret < 0) {
13050057a0d5STim Hardeck         if (ret == GNUTLS_E_AGAIN) {
13060057a0d5STim Hardeck             errno = EAGAIN;
13070057a0d5STim Hardeck         } else {
13080057a0d5STim Hardeck             errno = EIO;
13090057a0d5STim Hardeck         }
13100057a0d5STim Hardeck         ret = -1;
13110057a0d5STim Hardeck     }
13120057a0d5STim Hardeck     return ret;
13130057a0d5STim Hardeck }
13140057a0d5STim Hardeck #endif /* CONFIG_VNC_TLS */
13153e230dd2SCorentin Chary 
13163e230dd2SCorentin Chary /*
13173e230dd2SCorentin Chary  * Called to write a chunk of data to the client socket. The data may
13183e230dd2SCorentin Chary  * be the raw data, or may have already been encoded by SASL.
13193e230dd2SCorentin Chary  * The data will be written either straight onto the socket, or
13203e230dd2SCorentin Chary  * written via the GNUTLS wrappers, if TLS/SSL encryption is enabled
13213e230dd2SCorentin Chary  *
13223e230dd2SCorentin Chary  * NB, it is theoretically possible to have 2 layers of encryption,
13233e230dd2SCorentin Chary  * both SASL, and this TLS layer. It is highly unlikely in practice
13243e230dd2SCorentin Chary  * though, since SASL encryption will typically be a no-op if TLS
13253e230dd2SCorentin Chary  * is active
13263e230dd2SCorentin Chary  *
13273e230dd2SCorentin Chary  * Returns the number of bytes written, which may be less than
13283e230dd2SCorentin Chary  * the requested 'datalen' if the socket would block. Returns
13293e230dd2SCorentin Chary  * -1 on error, and disconnects the client socket.
13303e230dd2SCorentin Chary  */
13313e230dd2SCorentin Chary long vnc_client_write_buf(VncState *vs, const uint8_t *data, size_t datalen)
13323e230dd2SCorentin Chary {
13333e230dd2SCorentin Chary     long ret;
13343e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS
13353e230dd2SCorentin Chary     if (vs->tls.session) {
13360057a0d5STim Hardeck         ret = vnc_client_write_tls(&vs->tls.session, data, datalen);
13370057a0d5STim Hardeck     } else {
13380057a0d5STim Hardeck #ifdef CONFIG_VNC_WS
13390057a0d5STim Hardeck         if (vs->ws_tls.session) {
13400057a0d5STim Hardeck             ret = vnc_client_write_tls(&vs->ws_tls.session, data, datalen);
13413e230dd2SCorentin Chary         } else
13420057a0d5STim Hardeck #endif /* CONFIG_VNC_WS */
13433e230dd2SCorentin Chary #endif /* CONFIG_VNC_TLS */
13440057a0d5STim Hardeck         {
13453e230dd2SCorentin Chary             ret = send(vs->csock, (const void *)data, datalen, 0);
13460057a0d5STim Hardeck         }
13470057a0d5STim Hardeck #ifdef CONFIG_VNC_TLS
13480057a0d5STim Hardeck     }
13490057a0d5STim Hardeck #endif /* CONFIG_VNC_TLS */
13503e230dd2SCorentin Chary     VNC_DEBUG("Wrote wire %p %zd -> %ld\n", data, datalen, ret);
13513e230dd2SCorentin Chary     return vnc_client_io_error(vs, ret, socket_error());
13523e230dd2SCorentin Chary }
13533e230dd2SCorentin Chary 
13543e230dd2SCorentin Chary 
13553e230dd2SCorentin Chary /*
13563e230dd2SCorentin Chary  * Called to write buffered data to the client socket, when not
13573e230dd2SCorentin Chary  * using any SASL SSF encryption layers. Will write as much data
13583e230dd2SCorentin Chary  * as possible without blocking. If all buffered data is written,
13593e230dd2SCorentin Chary  * will switch the FD poll() handler back to read monitoring.
13603e230dd2SCorentin Chary  *
13613e230dd2SCorentin Chary  * Returns the number of bytes written, which may be less than
13623e230dd2SCorentin Chary  * the buffered output data if the socket would block. Returns
13633e230dd2SCorentin Chary  * -1 on error, and disconnects the client socket.
13643e230dd2SCorentin Chary  */
13653e230dd2SCorentin Chary static long vnc_client_write_plain(VncState *vs)
13663e230dd2SCorentin Chary {
13673e230dd2SCorentin Chary     long ret;
13683e230dd2SCorentin Chary 
13693e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL
13703e230dd2SCorentin Chary     VNC_DEBUG("Write Plain: Pending output %p size %zd offset %zd. Wait SSF %d\n",
13713e230dd2SCorentin Chary               vs->output.buffer, vs->output.capacity, vs->output.offset,
13723e230dd2SCorentin Chary               vs->sasl.waitWriteSSF);
13733e230dd2SCorentin Chary 
13743e230dd2SCorentin Chary     if (vs->sasl.conn &&
13753e230dd2SCorentin Chary         vs->sasl.runSSF &&
13763e230dd2SCorentin Chary         vs->sasl.waitWriteSSF) {
13773e230dd2SCorentin Chary         ret = vnc_client_write_buf(vs, vs->output.buffer, vs->sasl.waitWriteSSF);
13783e230dd2SCorentin Chary         if (ret)
13793e230dd2SCorentin Chary             vs->sasl.waitWriteSSF -= ret;
13803e230dd2SCorentin Chary     } else
13813e230dd2SCorentin Chary #endif /* CONFIG_VNC_SASL */
13823e230dd2SCorentin Chary         ret = vnc_client_write_buf(vs, vs->output.buffer, vs->output.offset);
13833e230dd2SCorentin Chary     if (!ret)
13843e230dd2SCorentin Chary         return 0;
13853e230dd2SCorentin Chary 
138632ed2680STim Hardeck     buffer_advance(&vs->output, ret);
13873e230dd2SCorentin Chary 
13883e230dd2SCorentin Chary     if (vs->output.offset == 0) {
13893e230dd2SCorentin Chary         qemu_set_fd_handler2(vs->csock, NULL, vnc_client_read, NULL, vs);
13903e230dd2SCorentin Chary     }
13913e230dd2SCorentin Chary 
13923e230dd2SCorentin Chary     return ret;
13933e230dd2SCorentin Chary }
13943e230dd2SCorentin Chary 
13953e230dd2SCorentin Chary 
13963e230dd2SCorentin Chary /*
13973e230dd2SCorentin Chary  * First function called whenever there is data to be written to
13983e230dd2SCorentin Chary  * the client socket. Will delegate actual work according to whether
13993e230dd2SCorentin Chary  * SASL SSF layers are enabled (thus requiring encryption calls)
14003e230dd2SCorentin Chary  */
1401bd023f95SCorentin Chary static void vnc_client_write_locked(void *opaque)
14023e230dd2SCorentin Chary {
14033e230dd2SCorentin Chary     VncState *vs = opaque;
14043e230dd2SCorentin Chary 
14053e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL
14063e230dd2SCorentin Chary     if (vs->sasl.conn &&
14073e230dd2SCorentin Chary         vs->sasl.runSSF &&
14083e230dd2SCorentin Chary         !vs->sasl.waitWriteSSF) {
14093e230dd2SCorentin Chary         vnc_client_write_sasl(vs);
14103e230dd2SCorentin Chary     } else
14113e230dd2SCorentin Chary #endif /* CONFIG_VNC_SASL */
14127536ee4bSTim Hardeck     {
14137536ee4bSTim Hardeck #ifdef CONFIG_VNC_WS
14147536ee4bSTim Hardeck         if (vs->encode_ws) {
14157536ee4bSTim Hardeck             vnc_client_write_ws(vs);
14167536ee4bSTim Hardeck         } else
14177536ee4bSTim Hardeck #endif /* CONFIG_VNC_WS */
14187536ee4bSTim Hardeck         {
14193e230dd2SCorentin Chary             vnc_client_write_plain(vs);
14203e230dd2SCorentin Chary         }
14217536ee4bSTim Hardeck     }
14227536ee4bSTim Hardeck }
14233e230dd2SCorentin Chary 
1424bd023f95SCorentin Chary void vnc_client_write(void *opaque)
1425bd023f95SCorentin Chary {
1426bd023f95SCorentin Chary     VncState *vs = opaque;
1427bd023f95SCorentin Chary 
1428bd023f95SCorentin Chary     vnc_lock_output(vs);
14297536ee4bSTim Hardeck     if (vs->output.offset
14307536ee4bSTim Hardeck #ifdef CONFIG_VNC_WS
14317536ee4bSTim Hardeck             || vs->ws_output.offset
14327536ee4bSTim Hardeck #endif
14337536ee4bSTim Hardeck             ) {
1434bd023f95SCorentin Chary         vnc_client_write_locked(opaque);
1435ac71103dSYoshiaki Tamura     } else if (vs->csock != -1) {
1436bd023f95SCorentin Chary         qemu_set_fd_handler2(vs->csock, NULL, vnc_client_read, NULL, vs);
1437bd023f95SCorentin Chary     }
1438bd023f95SCorentin Chary     vnc_unlock_output(vs);
1439bd023f95SCorentin Chary }
1440bd023f95SCorentin Chary 
14413e230dd2SCorentin Chary void vnc_read_when(VncState *vs, VncReadEvent *func, size_t expecting)
14423e230dd2SCorentin Chary {
14433e230dd2SCorentin Chary     vs->read_handler = func;
14443e230dd2SCorentin Chary     vs->read_handler_expect = expecting;
14453e230dd2SCorentin Chary }
14463e230dd2SCorentin Chary 
14470057a0d5STim Hardeck #ifdef CONFIG_VNC_TLS
14480057a0d5STim Hardeck static long vnc_client_read_tls(gnutls_session_t *session, uint8_t *data,
14490057a0d5STim Hardeck                                 size_t datalen)
14500057a0d5STim Hardeck {
14510057a0d5STim Hardeck     long ret = gnutls_read(*session, data, datalen);
14520057a0d5STim Hardeck     if (ret < 0) {
14530057a0d5STim Hardeck         if (ret == GNUTLS_E_AGAIN) {
14540057a0d5STim Hardeck             errno = EAGAIN;
14550057a0d5STim Hardeck         } else {
14560057a0d5STim Hardeck             errno = EIO;
14570057a0d5STim Hardeck         }
14580057a0d5STim Hardeck         ret = -1;
14590057a0d5STim Hardeck     }
14600057a0d5STim Hardeck     return ret;
14610057a0d5STim Hardeck }
14620057a0d5STim Hardeck #endif /* CONFIG_VNC_TLS */
14633e230dd2SCorentin Chary 
14643e230dd2SCorentin Chary /*
14653e230dd2SCorentin Chary  * Called to read a chunk of data from the client socket. The data may
14663e230dd2SCorentin Chary  * be the raw data, or may need to be further decoded by SASL.
14673e230dd2SCorentin Chary  * The data will be read either straight from to the socket, or
14683e230dd2SCorentin Chary  * read via the GNUTLS wrappers, if TLS/SSL encryption is enabled
14693e230dd2SCorentin Chary  *
14703e230dd2SCorentin Chary  * NB, it is theoretically possible to have 2 layers of encryption,
14713e230dd2SCorentin Chary  * both SASL, and this TLS layer. It is highly unlikely in practice
14723e230dd2SCorentin Chary  * though, since SASL encryption will typically be a no-op if TLS
14733e230dd2SCorentin Chary  * is active
14743e230dd2SCorentin Chary  *
14753e230dd2SCorentin Chary  * Returns the number of bytes read, which may be less than
14763e230dd2SCorentin Chary  * the requested 'datalen' if the socket would block. Returns
14773e230dd2SCorentin Chary  * -1 on error, and disconnects the client socket.
14783e230dd2SCorentin Chary  */
14793e230dd2SCorentin Chary long vnc_client_read_buf(VncState *vs, uint8_t *data, size_t datalen)
14803e230dd2SCorentin Chary {
14813e230dd2SCorentin Chary     long ret;
14823e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS
14833e230dd2SCorentin Chary     if (vs->tls.session) {
14840057a0d5STim Hardeck         ret = vnc_client_read_tls(&vs->tls.session, data, datalen);
14850057a0d5STim Hardeck     } else {
14860057a0d5STim Hardeck #ifdef CONFIG_VNC_WS
14870057a0d5STim Hardeck         if (vs->ws_tls.session) {
14880057a0d5STim Hardeck             ret = vnc_client_read_tls(&vs->ws_tls.session, data, datalen);
14893e230dd2SCorentin Chary         } else
14900057a0d5STim Hardeck #endif /* CONFIG_VNC_WS */
14913e230dd2SCorentin Chary #endif /* CONFIG_VNC_TLS */
14920057a0d5STim Hardeck         {
149300aa0040SBlue Swirl             ret = qemu_recv(vs->csock, data, datalen, 0);
14940057a0d5STim Hardeck         }
14950057a0d5STim Hardeck #ifdef CONFIG_VNC_TLS
14960057a0d5STim Hardeck     }
14970057a0d5STim Hardeck #endif /* CONFIG_VNC_TLS */
14983e230dd2SCorentin Chary     VNC_DEBUG("Read wire %p %zd -> %ld\n", data, datalen, ret);
14993e230dd2SCorentin Chary     return vnc_client_io_error(vs, ret, socket_error());
15003e230dd2SCorentin Chary }
15013e230dd2SCorentin Chary 
15023e230dd2SCorentin Chary 
15033e230dd2SCorentin Chary /*
15043e230dd2SCorentin Chary  * Called to read data from the client socket to the input buffer,
15053e230dd2SCorentin Chary  * when not using any SASL SSF encryption layers. Will read as much
15063e230dd2SCorentin Chary  * data as possible without blocking.
15073e230dd2SCorentin Chary  *
15083e230dd2SCorentin Chary  * Returns the number of bytes read. Returns -1 on error, and
15093e230dd2SCorentin Chary  * disconnects the client socket.
15103e230dd2SCorentin Chary  */
15113e230dd2SCorentin Chary static long vnc_client_read_plain(VncState *vs)
15123e230dd2SCorentin Chary {
15133e230dd2SCorentin Chary     int ret;
15143e230dd2SCorentin Chary     VNC_DEBUG("Read plain %p size %zd offset %zd\n",
15153e230dd2SCorentin Chary               vs->input.buffer, vs->input.capacity, vs->input.offset);
15163e230dd2SCorentin Chary     buffer_reserve(&vs->input, 4096);
15173e230dd2SCorentin Chary     ret = vnc_client_read_buf(vs, buffer_end(&vs->input), 4096);
15183e230dd2SCorentin Chary     if (!ret)
15193e230dd2SCorentin Chary         return 0;
15203e230dd2SCorentin Chary     vs->input.offset += ret;
15213e230dd2SCorentin Chary     return ret;
15223e230dd2SCorentin Chary }
15233e230dd2SCorentin Chary 
1524175b2a6eSCorentin Chary static void vnc_jobs_bh(void *opaque)
1525175b2a6eSCorentin Chary {
1526175b2a6eSCorentin Chary     VncState *vs = opaque;
1527175b2a6eSCorentin Chary 
1528175b2a6eSCorentin Chary     vnc_jobs_consume_buffer(vs);
1529175b2a6eSCorentin Chary }
15303e230dd2SCorentin Chary 
15313e230dd2SCorentin Chary /*
15323e230dd2SCorentin Chary  * First function called whenever there is more data to be read from
15333e230dd2SCorentin Chary  * the client socket. Will delegate actual work according to whether
15343e230dd2SCorentin Chary  * SASL SSF layers are enabled (thus requiring decryption calls)
15353e230dd2SCorentin Chary  */
15363e230dd2SCorentin Chary void vnc_client_read(void *opaque)
15373e230dd2SCorentin Chary {
15383e230dd2SCorentin Chary     VncState *vs = opaque;
15393e230dd2SCorentin Chary     long ret;
15403e230dd2SCorentin Chary 
15413e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL
15423e230dd2SCorentin Chary     if (vs->sasl.conn && vs->sasl.runSSF)
15433e230dd2SCorentin Chary         ret = vnc_client_read_sasl(vs);
15443e230dd2SCorentin Chary     else
15453e230dd2SCorentin Chary #endif /* CONFIG_VNC_SASL */
15467536ee4bSTim Hardeck #ifdef CONFIG_VNC_WS
15477536ee4bSTim Hardeck         if (vs->encode_ws) {
15487536ee4bSTim Hardeck             ret = vnc_client_read_ws(vs);
15497536ee4bSTim Hardeck             if (ret == -1) {
15507536ee4bSTim Hardeck                 vnc_disconnect_start(vs);
15517536ee4bSTim Hardeck                 return;
15527536ee4bSTim Hardeck             } else if (ret == -2) {
15537536ee4bSTim Hardeck                 vnc_client_error(vs);
15547536ee4bSTim Hardeck                 return;
15557536ee4bSTim Hardeck             }
15567536ee4bSTim Hardeck         } else
15577536ee4bSTim Hardeck #endif /* CONFIG_VNC_WS */
15587536ee4bSTim Hardeck         {
15593e230dd2SCorentin Chary         ret = vnc_client_read_plain(vs);
15607536ee4bSTim Hardeck         }
15613e230dd2SCorentin Chary     if (!ret) {
15623e230dd2SCorentin Chary         if (vs->csock == -1)
15633e230dd2SCorentin Chary             vnc_disconnect_finish(vs);
15643e230dd2SCorentin Chary         return;
15653e230dd2SCorentin Chary     }
15663e230dd2SCorentin Chary 
15673e230dd2SCorentin Chary     while (vs->read_handler && vs->input.offset >= vs->read_handler_expect) {
15683e230dd2SCorentin Chary         size_t len = vs->read_handler_expect;
15693e230dd2SCorentin Chary         int ret;
15703e230dd2SCorentin Chary 
15713e230dd2SCorentin Chary         ret = vs->read_handler(vs, vs->input.buffer, len);
15723e230dd2SCorentin Chary         if (vs->csock == -1) {
15733e230dd2SCorentin Chary             vnc_disconnect_finish(vs);
15743e230dd2SCorentin Chary             return;
15753e230dd2SCorentin Chary         }
15763e230dd2SCorentin Chary 
15773e230dd2SCorentin Chary         if (!ret) {
157832ed2680STim Hardeck             buffer_advance(&vs->input, len);
15793e230dd2SCorentin Chary         } else {
15803e230dd2SCorentin Chary             vs->read_handler_expect = ret;
15813e230dd2SCorentin Chary         }
15823e230dd2SCorentin Chary     }
15833e230dd2SCorentin Chary }
15843e230dd2SCorentin Chary 
15853e230dd2SCorentin Chary void vnc_write(VncState *vs, const void *data, size_t len)
15863e230dd2SCorentin Chary {
15873e230dd2SCorentin Chary     buffer_reserve(&vs->output, len);
15883e230dd2SCorentin Chary 
15893e230dd2SCorentin Chary     if (vs->csock != -1 && buffer_empty(&vs->output)) {
15903e230dd2SCorentin Chary         qemu_set_fd_handler2(vs->csock, NULL, vnc_client_read, vnc_client_write, vs);
15913e230dd2SCorentin Chary     }
15923e230dd2SCorentin Chary 
15933e230dd2SCorentin Chary     buffer_append(&vs->output, data, len);
15943e230dd2SCorentin Chary }
15953e230dd2SCorentin Chary 
15963e230dd2SCorentin Chary void vnc_write_s32(VncState *vs, int32_t value)
15973e230dd2SCorentin Chary {
15983e230dd2SCorentin Chary     vnc_write_u32(vs, *(uint32_t *)&value);
15993e230dd2SCorentin Chary }
16003e230dd2SCorentin Chary 
16013e230dd2SCorentin Chary void vnc_write_u32(VncState *vs, uint32_t value)
16023e230dd2SCorentin Chary {
16033e230dd2SCorentin Chary     uint8_t buf[4];
16043e230dd2SCorentin Chary 
16053e230dd2SCorentin Chary     buf[0] = (value >> 24) & 0xFF;
16063e230dd2SCorentin Chary     buf[1] = (value >> 16) & 0xFF;
16073e230dd2SCorentin Chary     buf[2] = (value >>  8) & 0xFF;
16083e230dd2SCorentin Chary     buf[3] = value & 0xFF;
16093e230dd2SCorentin Chary 
16103e230dd2SCorentin Chary     vnc_write(vs, buf, 4);
16113e230dd2SCorentin Chary }
16123e230dd2SCorentin Chary 
16133e230dd2SCorentin Chary void vnc_write_u16(VncState *vs, uint16_t value)
16143e230dd2SCorentin Chary {
16153e230dd2SCorentin Chary     uint8_t buf[2];
16163e230dd2SCorentin Chary 
16173e230dd2SCorentin Chary     buf[0] = (value >> 8) & 0xFF;
16183e230dd2SCorentin Chary     buf[1] = value & 0xFF;
16193e230dd2SCorentin Chary 
16203e230dd2SCorentin Chary     vnc_write(vs, buf, 2);
16213e230dd2SCorentin Chary }
16223e230dd2SCorentin Chary 
16233e230dd2SCorentin Chary void vnc_write_u8(VncState *vs, uint8_t value)
16243e230dd2SCorentin Chary {
16253e230dd2SCorentin Chary     vnc_write(vs, (char *)&value, 1);
16263e230dd2SCorentin Chary }
16273e230dd2SCorentin Chary 
16283e230dd2SCorentin Chary void vnc_flush(VncState *vs)
16293e230dd2SCorentin Chary {
1630bd023f95SCorentin Chary     vnc_lock_output(vs);
16317536ee4bSTim Hardeck     if (vs->csock != -1 && (vs->output.offset
16327536ee4bSTim Hardeck #ifdef CONFIG_VNC_WS
16337536ee4bSTim Hardeck                 || vs->ws_output.offset
16347536ee4bSTim Hardeck #endif
16357536ee4bSTim Hardeck                 )) {
1636bd023f95SCorentin Chary         vnc_client_write_locked(vs);
1637bd023f95SCorentin Chary     }
1638bd023f95SCorentin Chary     vnc_unlock_output(vs);
16393e230dd2SCorentin Chary }
16403e230dd2SCorentin Chary 
164171a8cdecSBlue Swirl static uint8_t read_u8(uint8_t *data, size_t offset)
16423e230dd2SCorentin Chary {
16433e230dd2SCorentin Chary     return data[offset];
16443e230dd2SCorentin Chary }
16453e230dd2SCorentin Chary 
164671a8cdecSBlue Swirl static uint16_t read_u16(uint8_t *data, size_t offset)
16473e230dd2SCorentin Chary {
16483e230dd2SCorentin Chary     return ((data[offset] & 0xFF) << 8) | (data[offset + 1] & 0xFF);
16493e230dd2SCorentin Chary }
16503e230dd2SCorentin Chary 
165171a8cdecSBlue Swirl static int32_t read_s32(uint8_t *data, size_t offset)
16523e230dd2SCorentin Chary {
16533e230dd2SCorentin Chary     return (int32_t)((data[offset] << 24) | (data[offset + 1] << 16) |
16543e230dd2SCorentin Chary                      (data[offset + 2] << 8) | data[offset + 3]);
16553e230dd2SCorentin Chary }
16563e230dd2SCorentin Chary 
16573e230dd2SCorentin Chary uint32_t read_u32(uint8_t *data, size_t offset)
16583e230dd2SCorentin Chary {
16593e230dd2SCorentin Chary     return ((data[offset] << 24) | (data[offset + 1] << 16) |
16603e230dd2SCorentin Chary             (data[offset + 2] << 8) | data[offset + 3]);
16613e230dd2SCorentin Chary }
16623e230dd2SCorentin Chary 
16633e230dd2SCorentin Chary static void client_cut_text(VncState *vs, size_t len, uint8_t *text)
16643e230dd2SCorentin Chary {
16653e230dd2SCorentin Chary }
16663e230dd2SCorentin Chary 
16679e8dd451SJan Kiszka static void check_pointer_type_change(Notifier *notifier, void *data)
16683e230dd2SCorentin Chary {
16693e230dd2SCorentin Chary     VncState *vs = container_of(notifier, VncState, mouse_mode_notifier);
167014768ebaSGerd Hoffmann     int absolute = qemu_input_is_absolute();
16713e230dd2SCorentin Chary 
16723e230dd2SCorentin Chary     if (vnc_has_feature(vs, VNC_FEATURE_POINTER_TYPE_CHANGE) && vs->absolute != absolute) {
1673bd023f95SCorentin Chary         vnc_lock_output(vs);
16743e230dd2SCorentin Chary         vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE);
16753e230dd2SCorentin Chary         vnc_write_u8(vs, 0);
16763e230dd2SCorentin Chary         vnc_write_u16(vs, 1);
16773e230dd2SCorentin Chary         vnc_framebuffer_update(vs, absolute, 0,
1678bea60dd7SPeter Lieven                                pixman_image_get_width(vs->vd->server),
1679bea60dd7SPeter Lieven                                pixman_image_get_height(vs->vd->server),
16803e230dd2SCorentin Chary                                VNC_ENCODING_POINTER_TYPE_CHANGE);
1681bd023f95SCorentin Chary         vnc_unlock_output(vs);
16823e230dd2SCorentin Chary         vnc_flush(vs);
16833e230dd2SCorentin Chary     }
16843e230dd2SCorentin Chary     vs->absolute = absolute;
16853e230dd2SCorentin Chary }
16863e230dd2SCorentin Chary 
16873e230dd2SCorentin Chary static void pointer_event(VncState *vs, int button_mask, int x, int y)
16883e230dd2SCorentin Chary {
168914768ebaSGerd Hoffmann     static uint32_t bmap[INPUT_BUTTON_MAX] = {
169014768ebaSGerd Hoffmann         [INPUT_BUTTON_LEFT]       = 0x01,
169114768ebaSGerd Hoffmann         [INPUT_BUTTON_MIDDLE]     = 0x02,
169214768ebaSGerd Hoffmann         [INPUT_BUTTON_RIGHT]      = 0x04,
169314768ebaSGerd Hoffmann         [INPUT_BUTTON_WHEEL_UP]   = 0x08,
169414768ebaSGerd Hoffmann         [INPUT_BUTTON_WHEEL_DOWN] = 0x10,
169514768ebaSGerd Hoffmann     };
169614768ebaSGerd Hoffmann     QemuConsole *con = vs->vd->dcl.con;
1697bea60dd7SPeter Lieven     int width = pixman_image_get_width(vs->vd->server);
1698bea60dd7SPeter Lieven     int height = pixman_image_get_height(vs->vd->server);
16993e230dd2SCorentin Chary 
170014768ebaSGerd Hoffmann     if (vs->last_bmask != button_mask) {
170114768ebaSGerd Hoffmann         qemu_input_update_buttons(con, bmap, vs->last_bmask, button_mask);
170214768ebaSGerd Hoffmann         vs->last_bmask = button_mask;
170314768ebaSGerd Hoffmann     }
17043e230dd2SCorentin Chary 
17053e230dd2SCorentin Chary     if (vs->absolute) {
170614768ebaSGerd Hoffmann         qemu_input_queue_abs(con, INPUT_AXIS_X, x, width);
170714768ebaSGerd Hoffmann         qemu_input_queue_abs(con, INPUT_AXIS_Y, y, height);
17083e230dd2SCorentin Chary     } else if (vnc_has_feature(vs, VNC_FEATURE_POINTER_TYPE_CHANGE)) {
170914768ebaSGerd Hoffmann         qemu_input_queue_rel(con, INPUT_AXIS_X, x - 0x7FFF);
171014768ebaSGerd Hoffmann         qemu_input_queue_rel(con, INPUT_AXIS_Y, y - 0x7FFF);
17113e230dd2SCorentin Chary     } else {
171214768ebaSGerd Hoffmann         if (vs->last_x != -1) {
171314768ebaSGerd Hoffmann             qemu_input_queue_rel(con, INPUT_AXIS_X, x - vs->last_x);
171414768ebaSGerd Hoffmann             qemu_input_queue_rel(con, INPUT_AXIS_Y, y - vs->last_y);
171514768ebaSGerd Hoffmann         }
17163e230dd2SCorentin Chary         vs->last_x = x;
17173e230dd2SCorentin Chary         vs->last_y = y;
17183e230dd2SCorentin Chary     }
171914768ebaSGerd Hoffmann     qemu_input_event_sync();
17203e230dd2SCorentin Chary }
17213e230dd2SCorentin Chary 
17223e230dd2SCorentin Chary static void reset_keys(VncState *vs)
17233e230dd2SCorentin Chary {
17243e230dd2SCorentin Chary     int i;
17253e230dd2SCorentin Chary     for(i = 0; i < 256; i++) {
17263e230dd2SCorentin Chary         if (vs->modifiers_state[i]) {
17278d447d10SGerd Hoffmann             qemu_input_event_send_key_number(vs->vd->dcl.con, i, false);
17283e230dd2SCorentin Chary             vs->modifiers_state[i] = 0;
17293e230dd2SCorentin Chary         }
17303e230dd2SCorentin Chary     }
17313e230dd2SCorentin Chary }
17323e230dd2SCorentin Chary 
17333e230dd2SCorentin Chary static void press_key(VncState *vs, int keysym)
17343e230dd2SCorentin Chary {
17353e230dd2SCorentin Chary     int keycode = keysym2scancode(vs->vd->kbd_layout, keysym) & SCANCODE_KEYMASK;
17368d447d10SGerd Hoffmann     qemu_input_event_send_key_number(vs->vd->dcl.con, keycode, true);
17372deb4accSGerd Hoffmann     qemu_input_event_send_key_delay(0);
17388d447d10SGerd Hoffmann     qemu_input_event_send_key_number(vs->vd->dcl.con, keycode, false);
17392deb4accSGerd Hoffmann     qemu_input_event_send_key_delay(0);
17403e230dd2SCorentin Chary }
17413e230dd2SCorentin Chary 
1742ab99e5c1SLei Li static int current_led_state(VncState *vs)
1743ab99e5c1SLei Li {
1744ab99e5c1SLei Li     int ledstate = 0;
1745ab99e5c1SLei Li 
1746ab99e5c1SLei Li     if (vs->modifiers_state[0x46]) {
1747ab99e5c1SLei Li         ledstate |= QEMU_SCROLL_LOCK_LED;
1748ab99e5c1SLei Li     }
1749ab99e5c1SLei Li     if (vs->modifiers_state[0x45]) {
1750ab99e5c1SLei Li         ledstate |= QEMU_NUM_LOCK_LED;
1751ab99e5c1SLei Li     }
1752ab99e5c1SLei Li     if (vs->modifiers_state[0x3a]) {
1753ab99e5c1SLei Li         ledstate |= QEMU_CAPS_LOCK_LED;
1754ab99e5c1SLei Li     }
1755ab99e5c1SLei Li 
1756ab99e5c1SLei Li     return ledstate;
1757ab99e5c1SLei Li }
1758ab99e5c1SLei Li 
1759ab99e5c1SLei Li static void vnc_led_state_change(VncState *vs)
1760ab99e5c1SLei Li {
1761ab99e5c1SLei Li     int ledstate = 0;
1762ab99e5c1SLei Li 
1763ab99e5c1SLei Li     if (!vnc_has_feature(vs, VNC_FEATURE_LED_STATE)) {
1764ab99e5c1SLei Li         return;
1765ab99e5c1SLei Li     }
1766ab99e5c1SLei Li 
1767ab99e5c1SLei Li     ledstate = current_led_state(vs);
1768ab99e5c1SLei Li     vnc_lock_output(vs);
1769ab99e5c1SLei Li     vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE);
1770ab99e5c1SLei Li     vnc_write_u8(vs, 0);
1771ab99e5c1SLei Li     vnc_write_u16(vs, 1);
1772ab99e5c1SLei Li     vnc_framebuffer_update(vs, 0, 0, 1, 1, VNC_ENCODING_LED_STATE);
1773ab99e5c1SLei Li     vnc_write_u8(vs, ledstate);
1774ab99e5c1SLei Li     vnc_unlock_output(vs);
1775ab99e5c1SLei Li     vnc_flush(vs);
1776ab99e5c1SLei Li }
1777ab99e5c1SLei Li 
17783e230dd2SCorentin Chary static void kbd_leds(void *opaque, int ledstate)
17793e230dd2SCorentin Chary {
17803e230dd2SCorentin Chary     VncState *vs = opaque;
178196f3d174SLei Li     int caps, num, scr;
17821483adcfSLei Li     bool has_changed = (ledstate != current_led_state(vs));
17833e230dd2SCorentin Chary 
178440066175SGerd Hoffmann     trace_vnc_key_guest_leds((ledstate & QEMU_CAPS_LOCK_LED),
178540066175SGerd Hoffmann                              (ledstate & QEMU_NUM_LOCK_LED),
178640066175SGerd Hoffmann                              (ledstate & QEMU_SCROLL_LOCK_LED));
178740066175SGerd Hoffmann 
17883e230dd2SCorentin Chary     caps = ledstate & QEMU_CAPS_LOCK_LED ? 1 : 0;
17893e230dd2SCorentin Chary     num  = ledstate & QEMU_NUM_LOCK_LED  ? 1 : 0;
179096f3d174SLei Li     scr  = ledstate & QEMU_SCROLL_LOCK_LED ? 1 : 0;
17913e230dd2SCorentin Chary 
17923e230dd2SCorentin Chary     if (vs->modifiers_state[0x3a] != caps) {
17933e230dd2SCorentin Chary         vs->modifiers_state[0x3a] = caps;
17943e230dd2SCorentin Chary     }
17953e230dd2SCorentin Chary     if (vs->modifiers_state[0x45] != num) {
17963e230dd2SCorentin Chary         vs->modifiers_state[0x45] = num;
17973e230dd2SCorentin Chary     }
179896f3d174SLei Li     if (vs->modifiers_state[0x46] != scr) {
179996f3d174SLei Li         vs->modifiers_state[0x46] = scr;
180096f3d174SLei Li     }
1801ab99e5c1SLei Li 
1802ab99e5c1SLei Li     /* Sending the current led state message to the client */
18031483adcfSLei Li     if (has_changed) {
1804ab99e5c1SLei Li         vnc_led_state_change(vs);
1805ab99e5c1SLei Li     }
18063e230dd2SCorentin Chary }
18073e230dd2SCorentin Chary 
18083e230dd2SCorentin Chary static void do_key_event(VncState *vs, int down, int keycode, int sym)
18093e230dd2SCorentin Chary {
18103e230dd2SCorentin Chary     /* QEMU console switch */
18113e230dd2SCorentin Chary     switch(keycode) {
18123e230dd2SCorentin Chary     case 0x2a:                          /* Left Shift */
18133e230dd2SCorentin Chary     case 0x36:                          /* Right Shift */
18143e230dd2SCorentin Chary     case 0x1d:                          /* Left CTRL */
18153e230dd2SCorentin Chary     case 0x9d:                          /* Right CTRL */
18163e230dd2SCorentin Chary     case 0x38:                          /* Left ALT */
18173e230dd2SCorentin Chary     case 0xb8:                          /* Right ALT */
18183e230dd2SCorentin Chary         if (down)
18193e230dd2SCorentin Chary             vs->modifiers_state[keycode] = 1;
18203e230dd2SCorentin Chary         else
18213e230dd2SCorentin Chary             vs->modifiers_state[keycode] = 0;
18223e230dd2SCorentin Chary         break;
18233e230dd2SCorentin Chary     case 0x02 ... 0x0a: /* '1' to '9' keys */
18241d0d59feSGerd Hoffmann         if (vs->vd->dcl.con == NULL &&
18251d0d59feSGerd Hoffmann             down && vs->modifiers_state[0x1d] && vs->modifiers_state[0x38]) {
18263e230dd2SCorentin Chary             /* Reset the modifiers sent to the current console */
18273e230dd2SCorentin Chary             reset_keys(vs);
18283e230dd2SCorentin Chary             console_select(keycode - 0x02);
18293e230dd2SCorentin Chary             return;
18303e230dd2SCorentin Chary         }
18313e230dd2SCorentin Chary         break;
18323e230dd2SCorentin Chary     case 0x3a:                        /* CapsLock */
18333e230dd2SCorentin Chary     case 0x45:                        /* NumLock */
18343e230dd2SCorentin Chary         if (down)
18353e230dd2SCorentin Chary             vs->modifiers_state[keycode] ^= 1;
18363e230dd2SCorentin Chary         break;
18373e230dd2SCorentin Chary     }
18383e230dd2SCorentin Chary 
1839e7b2aaccSLei Li     /* Turn off the lock state sync logic if the client support the led
1840e7b2aaccSLei Li        state extension.
1841e7b2aaccSLei Li     */
18429892088bSGerd Hoffmann     if (down && vs->vd->lock_key_sync &&
1843e7b2aaccSLei Li         !vnc_has_feature(vs, VNC_FEATURE_LED_STATE) &&
18443e230dd2SCorentin Chary         keycode_is_keypad(vs->vd->kbd_layout, keycode)) {
18453e230dd2SCorentin Chary         /* If the numlock state needs to change then simulate an additional
18463e230dd2SCorentin Chary            keypress before sending this one.  This will happen if the user
18473e230dd2SCorentin Chary            toggles numlock away from the VNC window.
18483e230dd2SCorentin Chary         */
18493e230dd2SCorentin Chary         if (keysym_is_numlock(vs->vd->kbd_layout, sym & 0xFFFF)) {
18503e230dd2SCorentin Chary             if (!vs->modifiers_state[0x45]) {
185140066175SGerd Hoffmann                 trace_vnc_key_sync_numlock(true);
18523e230dd2SCorentin Chary                 vs->modifiers_state[0x45] = 1;
18533e230dd2SCorentin Chary                 press_key(vs, 0xff7f);
18543e230dd2SCorentin Chary             }
18553e230dd2SCorentin Chary         } else {
18563e230dd2SCorentin Chary             if (vs->modifiers_state[0x45]) {
185740066175SGerd Hoffmann                 trace_vnc_key_sync_numlock(false);
18583e230dd2SCorentin Chary                 vs->modifiers_state[0x45] = 0;
18593e230dd2SCorentin Chary                 press_key(vs, 0xff7f);
18603e230dd2SCorentin Chary             }
18613e230dd2SCorentin Chary         }
18623e230dd2SCorentin Chary     }
18633e230dd2SCorentin Chary 
18649892088bSGerd Hoffmann     if (down && vs->vd->lock_key_sync &&
1865e7b2aaccSLei Li         !vnc_has_feature(vs, VNC_FEATURE_LED_STATE) &&
18663e230dd2SCorentin Chary         ((sym >= 'A' && sym <= 'Z') || (sym >= 'a' && sym <= 'z'))) {
18673e230dd2SCorentin Chary         /* If the capslock state needs to change then simulate an additional
18683e230dd2SCorentin Chary            keypress before sending this one.  This will happen if the user
18693e230dd2SCorentin Chary            toggles capslock away from the VNC window.
18703e230dd2SCorentin Chary         */
18713e230dd2SCorentin Chary         int uppercase = !!(sym >= 'A' && sym <= 'Z');
18723e230dd2SCorentin Chary         int shift = !!(vs->modifiers_state[0x2a] | vs->modifiers_state[0x36]);
18733e230dd2SCorentin Chary         int capslock = !!(vs->modifiers_state[0x3a]);
18743e230dd2SCorentin Chary         if (capslock) {
18753e230dd2SCorentin Chary             if (uppercase == shift) {
187640066175SGerd Hoffmann                 trace_vnc_key_sync_capslock(false);
18773e230dd2SCorentin Chary                 vs->modifiers_state[0x3a] = 0;
18783e230dd2SCorentin Chary                 press_key(vs, 0xffe5);
18793e230dd2SCorentin Chary             }
18803e230dd2SCorentin Chary         } else {
18813e230dd2SCorentin Chary             if (uppercase != shift) {
188240066175SGerd Hoffmann                 trace_vnc_key_sync_capslock(true);
18833e230dd2SCorentin Chary                 vs->modifiers_state[0x3a] = 1;
18843e230dd2SCorentin Chary                 press_key(vs, 0xffe5);
18853e230dd2SCorentin Chary             }
18863e230dd2SCorentin Chary         }
18873e230dd2SCorentin Chary     }
18883e230dd2SCorentin Chary 
188981c0d5a6SGerd Hoffmann     if (qemu_console_is_graphic(NULL)) {
18908d447d10SGerd Hoffmann         qemu_input_event_send_key_number(vs->vd->dcl.con, keycode, down);
18913e230dd2SCorentin Chary     } else {
1892e26437c2SGerd Hoffmann         bool numlock = vs->modifiers_state[0x45];
1893e26437c2SGerd Hoffmann         bool control = (vs->modifiers_state[0x1d] ||
1894e26437c2SGerd Hoffmann                         vs->modifiers_state[0x9d]);
18953e230dd2SCorentin Chary         /* QEMU console emulation */
18963e230dd2SCorentin Chary         if (down) {
18973e230dd2SCorentin Chary             switch (keycode) {
18983e230dd2SCorentin Chary             case 0x2a:                          /* Left Shift */
18993e230dd2SCorentin Chary             case 0x36:                          /* Right Shift */
19003e230dd2SCorentin Chary             case 0x1d:                          /* Left CTRL */
19013e230dd2SCorentin Chary             case 0x9d:                          /* Right CTRL */
19023e230dd2SCorentin Chary             case 0x38:                          /* Left ALT */
19033e230dd2SCorentin Chary             case 0xb8:                          /* Right ALT */
19043e230dd2SCorentin Chary                 break;
19053e230dd2SCorentin Chary             case 0xc8:
19063e230dd2SCorentin Chary                 kbd_put_keysym(QEMU_KEY_UP);
19073e230dd2SCorentin Chary                 break;
19083e230dd2SCorentin Chary             case 0xd0:
19093e230dd2SCorentin Chary                 kbd_put_keysym(QEMU_KEY_DOWN);
19103e230dd2SCorentin Chary                 break;
19113e230dd2SCorentin Chary             case 0xcb:
19123e230dd2SCorentin Chary                 kbd_put_keysym(QEMU_KEY_LEFT);
19133e230dd2SCorentin Chary                 break;
19143e230dd2SCorentin Chary             case 0xcd:
19153e230dd2SCorentin Chary                 kbd_put_keysym(QEMU_KEY_RIGHT);
19163e230dd2SCorentin Chary                 break;
19173e230dd2SCorentin Chary             case 0xd3:
19183e230dd2SCorentin Chary                 kbd_put_keysym(QEMU_KEY_DELETE);
19193e230dd2SCorentin Chary                 break;
19203e230dd2SCorentin Chary             case 0xc7:
19213e230dd2SCorentin Chary                 kbd_put_keysym(QEMU_KEY_HOME);
19223e230dd2SCorentin Chary                 break;
19233e230dd2SCorentin Chary             case 0xcf:
19243e230dd2SCorentin Chary                 kbd_put_keysym(QEMU_KEY_END);
19253e230dd2SCorentin Chary                 break;
19263e230dd2SCorentin Chary             case 0xc9:
19273e230dd2SCorentin Chary                 kbd_put_keysym(QEMU_KEY_PAGEUP);
19283e230dd2SCorentin Chary                 break;
19293e230dd2SCorentin Chary             case 0xd1:
19303e230dd2SCorentin Chary                 kbd_put_keysym(QEMU_KEY_PAGEDOWN);
19313e230dd2SCorentin Chary                 break;
19323e230dd2SCorentin Chary 
19333e230dd2SCorentin Chary             case 0x47:
19343e230dd2SCorentin Chary                 kbd_put_keysym(numlock ? '7' : QEMU_KEY_HOME);
19353e230dd2SCorentin Chary                 break;
19363e230dd2SCorentin Chary             case 0x48:
19373e230dd2SCorentin Chary                 kbd_put_keysym(numlock ? '8' : QEMU_KEY_UP);
19383e230dd2SCorentin Chary                 break;
19393e230dd2SCorentin Chary             case 0x49:
19403e230dd2SCorentin Chary                 kbd_put_keysym(numlock ? '9' : QEMU_KEY_PAGEUP);
19413e230dd2SCorentin Chary                 break;
19423e230dd2SCorentin Chary             case 0x4b:
19433e230dd2SCorentin Chary                 kbd_put_keysym(numlock ? '4' : QEMU_KEY_LEFT);
19443e230dd2SCorentin Chary                 break;
19453e230dd2SCorentin Chary             case 0x4c:
19463e230dd2SCorentin Chary                 kbd_put_keysym('5');
19473e230dd2SCorentin Chary                 break;
19483e230dd2SCorentin Chary             case 0x4d:
19493e230dd2SCorentin Chary                 kbd_put_keysym(numlock ? '6' : QEMU_KEY_RIGHT);
19503e230dd2SCorentin Chary                 break;
19513e230dd2SCorentin Chary             case 0x4f:
19523e230dd2SCorentin Chary                 kbd_put_keysym(numlock ? '1' : QEMU_KEY_END);
19533e230dd2SCorentin Chary                 break;
19543e230dd2SCorentin Chary             case 0x50:
19553e230dd2SCorentin Chary                 kbd_put_keysym(numlock ? '2' : QEMU_KEY_DOWN);
19563e230dd2SCorentin Chary                 break;
19573e230dd2SCorentin Chary             case 0x51:
19583e230dd2SCorentin Chary                 kbd_put_keysym(numlock ? '3' : QEMU_KEY_PAGEDOWN);
19593e230dd2SCorentin Chary                 break;
19603e230dd2SCorentin Chary             case 0x52:
19613e230dd2SCorentin Chary                 kbd_put_keysym('0');
19623e230dd2SCorentin Chary                 break;
19633e230dd2SCorentin Chary             case 0x53:
19643e230dd2SCorentin Chary                 kbd_put_keysym(numlock ? '.' : QEMU_KEY_DELETE);
19653e230dd2SCorentin Chary                 break;
19663e230dd2SCorentin Chary 
19673e230dd2SCorentin Chary             case 0xb5:
19683e230dd2SCorentin Chary                 kbd_put_keysym('/');
19693e230dd2SCorentin Chary                 break;
19703e230dd2SCorentin Chary             case 0x37:
19713e230dd2SCorentin Chary                 kbd_put_keysym('*');
19723e230dd2SCorentin Chary                 break;
19733e230dd2SCorentin Chary             case 0x4a:
19743e230dd2SCorentin Chary                 kbd_put_keysym('-');
19753e230dd2SCorentin Chary                 break;
19763e230dd2SCorentin Chary             case 0x4e:
19773e230dd2SCorentin Chary                 kbd_put_keysym('+');
19783e230dd2SCorentin Chary                 break;
19793e230dd2SCorentin Chary             case 0x9c:
19803e230dd2SCorentin Chary                 kbd_put_keysym('\n');
19813e230dd2SCorentin Chary                 break;
19823e230dd2SCorentin Chary 
19833e230dd2SCorentin Chary             default:
1984e26437c2SGerd Hoffmann                 if (control) {
1985e26437c2SGerd Hoffmann                     kbd_put_keysym(sym & 0x1f);
1986e26437c2SGerd Hoffmann                 } else {
19873e230dd2SCorentin Chary                     kbd_put_keysym(sym);
1988e26437c2SGerd Hoffmann                 }
19893e230dd2SCorentin Chary                 break;
19903e230dd2SCorentin Chary             }
19913e230dd2SCorentin Chary         }
19923e230dd2SCorentin Chary     }
19933e230dd2SCorentin Chary }
19943e230dd2SCorentin Chary 
19957bc9318bSGerd Hoffmann static void vnc_release_modifiers(VncState *vs)
19967bc9318bSGerd Hoffmann {
19977bc9318bSGerd Hoffmann     static const int keycodes[] = {
19987bc9318bSGerd Hoffmann         /* shift, control, alt keys, both left & right */
19997bc9318bSGerd Hoffmann         0x2a, 0x36, 0x1d, 0x9d, 0x38, 0xb8,
20007bc9318bSGerd Hoffmann     };
20017bc9318bSGerd Hoffmann     int i, keycode;
20027bc9318bSGerd Hoffmann 
200381c0d5a6SGerd Hoffmann     if (!qemu_console_is_graphic(NULL)) {
20047bc9318bSGerd Hoffmann         return;
20057bc9318bSGerd Hoffmann     }
20067bc9318bSGerd Hoffmann     for (i = 0; i < ARRAY_SIZE(keycodes); i++) {
20077bc9318bSGerd Hoffmann         keycode = keycodes[i];
20087bc9318bSGerd Hoffmann         if (!vs->modifiers_state[keycode]) {
20097bc9318bSGerd Hoffmann             continue;
20107bc9318bSGerd Hoffmann         }
20118d447d10SGerd Hoffmann         qemu_input_event_send_key_number(vs->vd->dcl.con, keycode, false);
20127bc9318bSGerd Hoffmann     }
20137bc9318bSGerd Hoffmann }
20147bc9318bSGerd Hoffmann 
201540066175SGerd Hoffmann static const char *code2name(int keycode)
201640066175SGerd Hoffmann {
201740066175SGerd Hoffmann     return QKeyCode_lookup[qemu_input_key_number_to_qcode(keycode)];
201840066175SGerd Hoffmann }
201940066175SGerd Hoffmann 
20203e230dd2SCorentin Chary static void key_event(VncState *vs, int down, uint32_t sym)
20213e230dd2SCorentin Chary {
20223e230dd2SCorentin Chary     int keycode;
20233e230dd2SCorentin Chary     int lsym = sym;
20243e230dd2SCorentin Chary 
202581c0d5a6SGerd Hoffmann     if (lsym >= 'A' && lsym <= 'Z' && qemu_console_is_graphic(NULL)) {
20263e230dd2SCorentin Chary         lsym = lsym - 'A' + 'a';
20273e230dd2SCorentin Chary     }
20283e230dd2SCorentin Chary 
20293e230dd2SCorentin Chary     keycode = keysym2scancode(vs->vd->kbd_layout, lsym & 0xFFFF) & SCANCODE_KEYMASK;
203040066175SGerd Hoffmann     trace_vnc_key_event_map(down, sym, keycode, code2name(keycode));
20313e230dd2SCorentin Chary     do_key_event(vs, down, keycode, sym);
20323e230dd2SCorentin Chary }
20333e230dd2SCorentin Chary 
20343e230dd2SCorentin Chary static void ext_key_event(VncState *vs, int down,
20353e230dd2SCorentin Chary                           uint32_t sym, uint16_t keycode)
20363e230dd2SCorentin Chary {
20373e230dd2SCorentin Chary     /* if the user specifies a keyboard layout, always use it */
203840066175SGerd Hoffmann     if (keyboard_layout) {
20393e230dd2SCorentin Chary         key_event(vs, down, sym);
204040066175SGerd Hoffmann     } else {
204140066175SGerd Hoffmann         trace_vnc_key_event_ext(down, sym, keycode, code2name(keycode));
20423e230dd2SCorentin Chary         do_key_event(vs, down, keycode, sym);
20433e230dd2SCorentin Chary     }
204440066175SGerd Hoffmann }
20453e230dd2SCorentin Chary 
20463e230dd2SCorentin Chary static void framebuffer_update_request(VncState *vs, int incremental,
2047bea60dd7SPeter Lieven                                        int x, int y, int w, int h)
20483e230dd2SCorentin Chary {
2049bea60dd7SPeter Lieven     int width = pixman_image_get_width(vs->vd->server);
2050bea60dd7SPeter Lieven     int height = pixman_image_get_height(vs->vd->server);
20513e230dd2SCorentin Chary 
20523e230dd2SCorentin Chary     vs->need_update = 1;
2053bea60dd7SPeter Lieven 
2054bea60dd7SPeter Lieven     if (incremental) {
2055bea60dd7SPeter Lieven         return;
20563e230dd2SCorentin Chary     }
2057bea60dd7SPeter Lieven 
205807535a89SStephan Kulow     vs->force_update = 1;
2059bea60dd7SPeter Lieven     vnc_set_area_dirty(vs->dirty, width, height, x, y, w, h);
20603e230dd2SCorentin Chary }
20613e230dd2SCorentin Chary 
20623e230dd2SCorentin Chary static void send_ext_key_event_ack(VncState *vs)
20633e230dd2SCorentin Chary {
2064bd023f95SCorentin Chary     vnc_lock_output(vs);
20653e230dd2SCorentin Chary     vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE);
20663e230dd2SCorentin Chary     vnc_write_u8(vs, 0);
20673e230dd2SCorentin Chary     vnc_write_u16(vs, 1);
2068d39fa6d8SGerd Hoffmann     vnc_framebuffer_update(vs, 0, 0,
2069bea60dd7SPeter Lieven                            pixman_image_get_width(vs->vd->server),
2070bea60dd7SPeter Lieven                            pixman_image_get_height(vs->vd->server),
20713e230dd2SCorentin Chary                            VNC_ENCODING_EXT_KEY_EVENT);
2072bd023f95SCorentin Chary     vnc_unlock_output(vs);
20733e230dd2SCorentin Chary     vnc_flush(vs);
20743e230dd2SCorentin Chary }
20753e230dd2SCorentin Chary 
20763e230dd2SCorentin Chary static void send_ext_audio_ack(VncState *vs)
20773e230dd2SCorentin Chary {
2078bd023f95SCorentin Chary     vnc_lock_output(vs);
20793e230dd2SCorentin Chary     vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE);
20803e230dd2SCorentin Chary     vnc_write_u8(vs, 0);
20813e230dd2SCorentin Chary     vnc_write_u16(vs, 1);
2082d39fa6d8SGerd Hoffmann     vnc_framebuffer_update(vs, 0, 0,
2083bea60dd7SPeter Lieven                            pixman_image_get_width(vs->vd->server),
2084bea60dd7SPeter Lieven                            pixman_image_get_height(vs->vd->server),
20853e230dd2SCorentin Chary                            VNC_ENCODING_AUDIO);
2086bd023f95SCorentin Chary     vnc_unlock_output(vs);
20873e230dd2SCorentin Chary     vnc_flush(vs);
20883e230dd2SCorentin Chary }
20893e230dd2SCorentin Chary 
20903e230dd2SCorentin Chary static void set_encodings(VncState *vs, int32_t *encodings, size_t n_encodings)
20913e230dd2SCorentin Chary {
20923e230dd2SCorentin Chary     int i;
20933e230dd2SCorentin Chary     unsigned int enc = 0;
20943e230dd2SCorentin Chary 
20953e230dd2SCorentin Chary     vs->features = 0;
20963e230dd2SCorentin Chary     vs->vnc_encoding = 0;
2097d1af0e05SCorentin Chary     vs->tight.compression = 9;
2098d1af0e05SCorentin Chary     vs->tight.quality = -1; /* Lossless by default */
20993e230dd2SCorentin Chary     vs->absolute = -1;
21003e230dd2SCorentin Chary 
21013e230dd2SCorentin Chary     /*
21023e230dd2SCorentin Chary      * Start from the end because the encodings are sent in order of preference.
2103e5bed759SDong Xu Wang      * This way the preferred encoding (first encoding defined in the array)
21043e230dd2SCorentin Chary      * will be set at the end of the loop.
21053e230dd2SCorentin Chary      */
21063e230dd2SCorentin Chary     for (i = n_encodings - 1; i >= 0; i--) {
21073e230dd2SCorentin Chary         enc = encodings[i];
21083e230dd2SCorentin Chary         switch (enc) {
21093e230dd2SCorentin Chary         case VNC_ENCODING_RAW:
21103e230dd2SCorentin Chary             vs->vnc_encoding = enc;
21113e230dd2SCorentin Chary             break;
21123e230dd2SCorentin Chary         case VNC_ENCODING_COPYRECT:
21133e230dd2SCorentin Chary             vs->features |= VNC_FEATURE_COPYRECT_MASK;
21143e230dd2SCorentin Chary             break;
21153e230dd2SCorentin Chary         case VNC_ENCODING_HEXTILE:
21163e230dd2SCorentin Chary             vs->features |= VNC_FEATURE_HEXTILE_MASK;
21173e230dd2SCorentin Chary             vs->vnc_encoding = enc;
21183e230dd2SCorentin Chary             break;
21193e230dd2SCorentin Chary         case VNC_ENCODING_TIGHT:
21203e230dd2SCorentin Chary             vs->features |= VNC_FEATURE_TIGHT_MASK;
21213e230dd2SCorentin Chary             vs->vnc_encoding = enc;
21223e230dd2SCorentin Chary             break;
2123fe3e7f2dSJoel Martin #ifdef CONFIG_VNC_PNG
2124efe556adSCorentin Chary         case VNC_ENCODING_TIGHT_PNG:
2125efe556adSCorentin Chary             vs->features |= VNC_FEATURE_TIGHT_PNG_MASK;
2126efe556adSCorentin Chary             vs->vnc_encoding = enc;
2127efe556adSCorentin Chary             break;
2128fe3e7f2dSJoel Martin #endif
21293e230dd2SCorentin Chary         case VNC_ENCODING_ZLIB:
21303e230dd2SCorentin Chary             vs->features |= VNC_FEATURE_ZLIB_MASK;
21313e230dd2SCorentin Chary             vs->vnc_encoding = enc;
21323e230dd2SCorentin Chary             break;
2133148954faSCorentin Chary         case VNC_ENCODING_ZRLE:
2134148954faSCorentin Chary             vs->features |= VNC_FEATURE_ZRLE_MASK;
2135148954faSCorentin Chary             vs->vnc_encoding = enc;
2136148954faSCorentin Chary             break;
2137148954faSCorentin Chary         case VNC_ENCODING_ZYWRLE:
2138148954faSCorentin Chary             vs->features |= VNC_FEATURE_ZYWRLE_MASK;
2139148954faSCorentin Chary             vs->vnc_encoding = enc;
2140148954faSCorentin Chary             break;
21413e230dd2SCorentin Chary         case VNC_ENCODING_DESKTOPRESIZE:
21423e230dd2SCorentin Chary             vs->features |= VNC_FEATURE_RESIZE_MASK;
21433e230dd2SCorentin Chary             break;
21443e230dd2SCorentin Chary         case VNC_ENCODING_POINTER_TYPE_CHANGE:
21453e230dd2SCorentin Chary             vs->features |= VNC_FEATURE_POINTER_TYPE_CHANGE_MASK;
21463e230dd2SCorentin Chary             break;
21473e230dd2SCorentin Chary         case VNC_ENCODING_RICH_CURSOR:
21483e230dd2SCorentin Chary             vs->features |= VNC_FEATURE_RICH_CURSOR_MASK;
21493e230dd2SCorentin Chary             break;
21503e230dd2SCorentin Chary         case VNC_ENCODING_EXT_KEY_EVENT:
21513e230dd2SCorentin Chary             send_ext_key_event_ack(vs);
21523e230dd2SCorentin Chary             break;
21533e230dd2SCorentin Chary         case VNC_ENCODING_AUDIO:
21543e230dd2SCorentin Chary             send_ext_audio_ack(vs);
21553e230dd2SCorentin Chary             break;
21563e230dd2SCorentin Chary         case VNC_ENCODING_WMVi:
21573e230dd2SCorentin Chary             vs->features |= VNC_FEATURE_WMVI_MASK;
21583e230dd2SCorentin Chary             break;
2159ab99e5c1SLei Li         case VNC_ENCODING_LED_STATE:
2160ab99e5c1SLei Li             vs->features |= VNC_FEATURE_LED_STATE_MASK;
2161ab99e5c1SLei Li             break;
21623e230dd2SCorentin Chary         case VNC_ENCODING_COMPRESSLEVEL0 ... VNC_ENCODING_COMPRESSLEVEL0 + 9:
2163d1af0e05SCorentin Chary             vs->tight.compression = (enc & 0x0F);
21643e230dd2SCorentin Chary             break;
21653e230dd2SCorentin Chary         case VNC_ENCODING_QUALITYLEVEL0 ... VNC_ENCODING_QUALITYLEVEL0 + 9:
2166b31f519eSCorentin Chary             if (vs->vd->lossy) {
2167d1af0e05SCorentin Chary                 vs->tight.quality = (enc & 0x0F);
2168b31f519eSCorentin Chary             }
21693e230dd2SCorentin Chary             break;
21703e230dd2SCorentin Chary         default:
21713e230dd2SCorentin Chary             VNC_DEBUG("Unknown encoding: %d (0x%.8x): %d\n", i, enc, enc);
21723e230dd2SCorentin Chary             break;
21733e230dd2SCorentin Chary         }
21743e230dd2SCorentin Chary     }
21753e230dd2SCorentin Chary     vnc_desktop_resize(vs);
21769e8dd451SJan Kiszka     check_pointer_type_change(&vs->mouse_mode_notifier, NULL);
2177ab99e5c1SLei Li     vnc_led_state_change(vs);
21783e230dd2SCorentin Chary }
21793e230dd2SCorentin Chary 
21803e230dd2SCorentin Chary static void set_pixel_conversion(VncState *vs)
21813e230dd2SCorentin Chary {
21829f64916dSGerd Hoffmann     pixman_format_code_t fmt = qemu_pixman_get_format(&vs->client_pf);
21839f64916dSGerd Hoffmann 
21849f64916dSGerd Hoffmann     if (fmt == VNC_SERVER_FB_FORMAT) {
21853e230dd2SCorentin Chary         vs->write_pixels = vnc_write_pixels_copy;
21863e230dd2SCorentin Chary         vnc_hextile_set_pixel_conversion(vs, 0);
21873e230dd2SCorentin Chary     } else {
21883e230dd2SCorentin Chary         vs->write_pixels = vnc_write_pixels_generic;
21893e230dd2SCorentin Chary         vnc_hextile_set_pixel_conversion(vs, 1);
21903e230dd2SCorentin Chary     }
21913e230dd2SCorentin Chary }
21923e230dd2SCorentin Chary 
21933e230dd2SCorentin Chary static void set_pixel_format(VncState *vs,
21943e230dd2SCorentin Chary                              int bits_per_pixel, int depth,
21953e230dd2SCorentin Chary                              int big_endian_flag, int true_color_flag,
21963e230dd2SCorentin Chary                              int red_max, int green_max, int blue_max,
21973e230dd2SCorentin Chary                              int red_shift, int green_shift, int blue_shift)
21983e230dd2SCorentin Chary {
21993e230dd2SCorentin Chary     if (!true_color_flag) {
22003e230dd2SCorentin Chary         vnc_client_error(vs);
22013e230dd2SCorentin Chary         return;
22023e230dd2SCorentin Chary     }
22033e230dd2SCorentin Chary 
2204e6908bfeSPetr Matousek     switch (bits_per_pixel) {
2205e6908bfeSPetr Matousek     case 8:
2206e6908bfeSPetr Matousek     case 16:
2207e6908bfeSPetr Matousek     case 32:
2208e6908bfeSPetr Matousek         break;
2209e6908bfeSPetr Matousek     default:
2210e6908bfeSPetr Matousek         vnc_client_error(vs);
2211e6908bfeSPetr Matousek         return;
2212e6908bfeSPetr Matousek     }
2213e6908bfeSPetr Matousek 
22149f64916dSGerd Hoffmann     vs->client_pf.rmax = red_max;
22159f64916dSGerd Hoffmann     vs->client_pf.rbits = hweight_long(red_max);
22169f64916dSGerd Hoffmann     vs->client_pf.rshift = red_shift;
22179f64916dSGerd Hoffmann     vs->client_pf.rmask = red_max << red_shift;
22189f64916dSGerd Hoffmann     vs->client_pf.gmax = green_max;
22199f64916dSGerd Hoffmann     vs->client_pf.gbits = hweight_long(green_max);
22209f64916dSGerd Hoffmann     vs->client_pf.gshift = green_shift;
22219f64916dSGerd Hoffmann     vs->client_pf.gmask = green_max << green_shift;
22229f64916dSGerd Hoffmann     vs->client_pf.bmax = blue_max;
22239f64916dSGerd Hoffmann     vs->client_pf.bbits = hweight_long(blue_max);
22249f64916dSGerd Hoffmann     vs->client_pf.bshift = blue_shift;
22259f64916dSGerd Hoffmann     vs->client_pf.bmask = blue_max << blue_shift;
22269f64916dSGerd Hoffmann     vs->client_pf.bits_per_pixel = bits_per_pixel;
22279f64916dSGerd Hoffmann     vs->client_pf.bytes_per_pixel = bits_per_pixel / 8;
22289f64916dSGerd Hoffmann     vs->client_pf.depth = bits_per_pixel == 32 ? 24 : bits_per_pixel;
22299f64916dSGerd Hoffmann     vs->client_be = big_endian_flag;
22303e230dd2SCorentin Chary 
22313e230dd2SCorentin Chary     set_pixel_conversion(vs);
22323e230dd2SCorentin Chary 
22331d0d59feSGerd Hoffmann     graphic_hw_invalidate(vs->vd->dcl.con);
22341d0d59feSGerd Hoffmann     graphic_hw_update(vs->vd->dcl.con);
22353e230dd2SCorentin Chary }
22363e230dd2SCorentin Chary 
22373e230dd2SCorentin Chary static void pixel_format_message (VncState *vs) {
22383e230dd2SCorentin Chary     char pad[3] = { 0, 0, 0 };
22393e230dd2SCorentin Chary 
22409f64916dSGerd Hoffmann     vs->client_pf = qemu_default_pixelformat(32);
22419f64916dSGerd Hoffmann 
22429f64916dSGerd Hoffmann     vnc_write_u8(vs, vs->client_pf.bits_per_pixel); /* bits-per-pixel */
22439f64916dSGerd Hoffmann     vnc_write_u8(vs, vs->client_pf.depth); /* depth */
22443e230dd2SCorentin Chary 
22453e230dd2SCorentin Chary #ifdef HOST_WORDS_BIGENDIAN
22463e230dd2SCorentin Chary     vnc_write_u8(vs, 1);             /* big-endian-flag */
22473e230dd2SCorentin Chary #else
22483e230dd2SCorentin Chary     vnc_write_u8(vs, 0);             /* big-endian-flag */
22493e230dd2SCorentin Chary #endif
22503e230dd2SCorentin Chary     vnc_write_u8(vs, 1);             /* true-color-flag */
22519f64916dSGerd Hoffmann     vnc_write_u16(vs, vs->client_pf.rmax);     /* red-max */
22529f64916dSGerd Hoffmann     vnc_write_u16(vs, vs->client_pf.gmax);     /* green-max */
22539f64916dSGerd Hoffmann     vnc_write_u16(vs, vs->client_pf.bmax);     /* blue-max */
22549f64916dSGerd Hoffmann     vnc_write_u8(vs, vs->client_pf.rshift);    /* red-shift */
22559f64916dSGerd Hoffmann     vnc_write_u8(vs, vs->client_pf.gshift);    /* green-shift */
22569f64916dSGerd Hoffmann     vnc_write_u8(vs, vs->client_pf.bshift);    /* blue-shift */
22579f64916dSGerd Hoffmann     vnc_write(vs, pad, 3);           /* padding */
22583e230dd2SCorentin Chary 
22593e230dd2SCorentin Chary     vnc_hextile_set_pixel_conversion(vs, 0);
22603e230dd2SCorentin Chary     vs->write_pixels = vnc_write_pixels_copy;
22613e230dd2SCorentin Chary }
22623e230dd2SCorentin Chary 
22633e230dd2SCorentin Chary static void vnc_colordepth(VncState *vs)
22643e230dd2SCorentin Chary {
22653e230dd2SCorentin Chary     if (vnc_has_feature(vs, VNC_FEATURE_WMVI)) {
22663e230dd2SCorentin Chary         /* Sending a WMVi message to notify the client*/
2267bd023f95SCorentin Chary         vnc_lock_output(vs);
22683e230dd2SCorentin Chary         vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE);
22693e230dd2SCorentin Chary         vnc_write_u8(vs, 0);
22703e230dd2SCorentin Chary         vnc_write_u16(vs, 1); /* number of rects */
2271d39fa6d8SGerd Hoffmann         vnc_framebuffer_update(vs, 0, 0,
2272bea60dd7SPeter Lieven                                pixman_image_get_width(vs->vd->server),
2273bea60dd7SPeter Lieven                                pixman_image_get_height(vs->vd->server),
2274d39fa6d8SGerd Hoffmann                                VNC_ENCODING_WMVi);
22753e230dd2SCorentin Chary         pixel_format_message(vs);
2276bd023f95SCorentin Chary         vnc_unlock_output(vs);
22773e230dd2SCorentin Chary         vnc_flush(vs);
22783e230dd2SCorentin Chary     } else {
22793e230dd2SCorentin Chary         set_pixel_conversion(vs);
22803e230dd2SCorentin Chary     }
22813e230dd2SCorentin Chary }
22823e230dd2SCorentin Chary 
22833e230dd2SCorentin Chary static int protocol_client_msg(VncState *vs, uint8_t *data, size_t len)
22843e230dd2SCorentin Chary {
22853e230dd2SCorentin Chary     int i;
22863e230dd2SCorentin Chary     uint16_t limit;
22873e230dd2SCorentin Chary     VncDisplay *vd = vs->vd;
22883e230dd2SCorentin Chary 
22893e230dd2SCorentin Chary     if (data[0] > 3) {
22900f7b2864SGerd Hoffmann         update_displaychangelistener(&vd->dcl, VNC_REFRESH_INTERVAL_BASE);
22913e230dd2SCorentin Chary     }
22923e230dd2SCorentin Chary 
22933e230dd2SCorentin Chary     switch (data[0]) {
22943e230dd2SCorentin Chary     case VNC_MSG_CLIENT_SET_PIXEL_FORMAT:
22953e230dd2SCorentin Chary         if (len == 1)
22963e230dd2SCorentin Chary             return 20;
22973e230dd2SCorentin Chary 
22983e230dd2SCorentin Chary         set_pixel_format(vs, read_u8(data, 4), read_u8(data, 5),
22993e230dd2SCorentin Chary                          read_u8(data, 6), read_u8(data, 7),
23003e230dd2SCorentin Chary                          read_u16(data, 8), read_u16(data, 10),
23013e230dd2SCorentin Chary                          read_u16(data, 12), read_u8(data, 14),
23023e230dd2SCorentin Chary                          read_u8(data, 15), read_u8(data, 16));
23033e230dd2SCorentin Chary         break;
23043e230dd2SCorentin Chary     case VNC_MSG_CLIENT_SET_ENCODINGS:
23053e230dd2SCorentin Chary         if (len == 1)
23063e230dd2SCorentin Chary             return 4;
23073e230dd2SCorentin Chary 
23083e230dd2SCorentin Chary         if (len == 4) {
23093e230dd2SCorentin Chary             limit = read_u16(data, 2);
23103e230dd2SCorentin Chary             if (limit > 0)
23113e230dd2SCorentin Chary                 return 4 + (limit * 4);
23123e230dd2SCorentin Chary         } else
23133e230dd2SCorentin Chary             limit = read_u16(data, 2);
23143e230dd2SCorentin Chary 
23153e230dd2SCorentin Chary         for (i = 0; i < limit; i++) {
23163e230dd2SCorentin Chary             int32_t val = read_s32(data, 4 + (i * 4));
23173e230dd2SCorentin Chary             memcpy(data + 4 + (i * 4), &val, sizeof(val));
23183e230dd2SCorentin Chary         }
23193e230dd2SCorentin Chary 
23203e230dd2SCorentin Chary         set_encodings(vs, (int32_t *)(data + 4), limit);
23213e230dd2SCorentin Chary         break;
23223e230dd2SCorentin Chary     case VNC_MSG_CLIENT_FRAMEBUFFER_UPDATE_REQUEST:
23233e230dd2SCorentin Chary         if (len == 1)
23243e230dd2SCorentin Chary             return 10;
23253e230dd2SCorentin Chary 
23263e230dd2SCorentin Chary         framebuffer_update_request(vs,
23273e230dd2SCorentin Chary                                    read_u8(data, 1), read_u16(data, 2), read_u16(data, 4),
23283e230dd2SCorentin Chary                                    read_u16(data, 6), read_u16(data, 8));
23293e230dd2SCorentin Chary         break;
23303e230dd2SCorentin Chary     case VNC_MSG_CLIENT_KEY_EVENT:
23313e230dd2SCorentin Chary         if (len == 1)
23323e230dd2SCorentin Chary             return 8;
23333e230dd2SCorentin Chary 
23343e230dd2SCorentin Chary         key_event(vs, read_u8(data, 1), read_u32(data, 4));
23353e230dd2SCorentin Chary         break;
23363e230dd2SCorentin Chary     case VNC_MSG_CLIENT_POINTER_EVENT:
23373e230dd2SCorentin Chary         if (len == 1)
23383e230dd2SCorentin Chary             return 6;
23393e230dd2SCorentin Chary 
23403e230dd2SCorentin Chary         pointer_event(vs, read_u8(data, 1), read_u16(data, 2), read_u16(data, 4));
23413e230dd2SCorentin Chary         break;
23423e230dd2SCorentin Chary     case VNC_MSG_CLIENT_CUT_TEXT:
2343f9a70e79SPeter Lieven         if (len == 1) {
23443e230dd2SCorentin Chary             return 8;
2345f9a70e79SPeter Lieven         }
23463e230dd2SCorentin Chary         if (len == 8) {
23473e230dd2SCorentin Chary             uint32_t dlen = read_u32(data, 4);
2348f9a70e79SPeter Lieven             if (dlen > (1 << 20)) {
2349f9a70e79SPeter Lieven                 error_report("vnc: client_cut_text msg payload has %u bytes"
2350f9a70e79SPeter Lieven                              " which exceeds our limit of 1MB.", dlen);
2351f9a70e79SPeter Lieven                 vnc_client_error(vs);
2352f9a70e79SPeter Lieven                 break;
2353f9a70e79SPeter Lieven             }
2354f9a70e79SPeter Lieven             if (dlen > 0) {
23553e230dd2SCorentin Chary                 return 8 + dlen;
23563e230dd2SCorentin Chary             }
2357f9a70e79SPeter Lieven         }
23583e230dd2SCorentin Chary 
23593e230dd2SCorentin Chary         client_cut_text(vs, read_u32(data, 4), data + 8);
23603e230dd2SCorentin Chary         break;
23613e230dd2SCorentin Chary     case VNC_MSG_CLIENT_QEMU:
23623e230dd2SCorentin Chary         if (len == 1)
23633e230dd2SCorentin Chary             return 2;
23643e230dd2SCorentin Chary 
23653e230dd2SCorentin Chary         switch (read_u8(data, 1)) {
23663e230dd2SCorentin Chary         case VNC_MSG_CLIENT_QEMU_EXT_KEY_EVENT:
23673e230dd2SCorentin Chary             if (len == 2)
23683e230dd2SCorentin Chary                 return 12;
23693e230dd2SCorentin Chary 
23703e230dd2SCorentin Chary             ext_key_event(vs, read_u16(data, 2),
23713e230dd2SCorentin Chary                           read_u32(data, 4), read_u32(data, 8));
23723e230dd2SCorentin Chary             break;
23733e230dd2SCorentin Chary         case VNC_MSG_CLIENT_QEMU_AUDIO:
23743e230dd2SCorentin Chary             if (len == 2)
23753e230dd2SCorentin Chary                 return 4;
23763e230dd2SCorentin Chary 
23773e230dd2SCorentin Chary             switch (read_u16 (data, 2)) {
23783e230dd2SCorentin Chary             case VNC_MSG_CLIENT_QEMU_AUDIO_ENABLE:
23793e230dd2SCorentin Chary                 audio_add(vs);
23803e230dd2SCorentin Chary                 break;
23813e230dd2SCorentin Chary             case VNC_MSG_CLIENT_QEMU_AUDIO_DISABLE:
23823e230dd2SCorentin Chary                 audio_del(vs);
23833e230dd2SCorentin Chary                 break;
23843e230dd2SCorentin Chary             case VNC_MSG_CLIENT_QEMU_AUDIO_SET_FORMAT:
23853e230dd2SCorentin Chary                 if (len == 4)
23863e230dd2SCorentin Chary                     return 10;
23873e230dd2SCorentin Chary                 switch (read_u8(data, 4)) {
23883e230dd2SCorentin Chary                 case 0: vs->as.fmt = AUD_FMT_U8; break;
23893e230dd2SCorentin Chary                 case 1: vs->as.fmt = AUD_FMT_S8; break;
23903e230dd2SCorentin Chary                 case 2: vs->as.fmt = AUD_FMT_U16; break;
23913e230dd2SCorentin Chary                 case 3: vs->as.fmt = AUD_FMT_S16; break;
23923e230dd2SCorentin Chary                 case 4: vs->as.fmt = AUD_FMT_U32; break;
23933e230dd2SCorentin Chary                 case 5: vs->as.fmt = AUD_FMT_S32; break;
23943e230dd2SCorentin Chary                 default:
23953e230dd2SCorentin Chary                     printf("Invalid audio format %d\n", read_u8(data, 4));
23963e230dd2SCorentin Chary                     vnc_client_error(vs);
23973e230dd2SCorentin Chary                     break;
23983e230dd2SCorentin Chary                 }
23993e230dd2SCorentin Chary                 vs->as.nchannels = read_u8(data, 5);
24003e230dd2SCorentin Chary                 if (vs->as.nchannels != 1 && vs->as.nchannels != 2) {
24013e230dd2SCorentin Chary                     printf("Invalid audio channel coount %d\n",
24023e230dd2SCorentin Chary                            read_u8(data, 5));
24033e230dd2SCorentin Chary                     vnc_client_error(vs);
24043e230dd2SCorentin Chary                     break;
24053e230dd2SCorentin Chary                 }
24063e230dd2SCorentin Chary                 vs->as.freq = read_u32(data, 6);
24073e230dd2SCorentin Chary                 break;
24083e230dd2SCorentin Chary             default:
24093e230dd2SCorentin Chary                 printf ("Invalid audio message %d\n", read_u8(data, 4));
24103e230dd2SCorentin Chary                 vnc_client_error(vs);
24113e230dd2SCorentin Chary                 break;
24123e230dd2SCorentin Chary             }
24133e230dd2SCorentin Chary             break;
24143e230dd2SCorentin Chary 
24153e230dd2SCorentin Chary         default:
24163e230dd2SCorentin Chary             printf("Msg: %d\n", read_u16(data, 0));
24173e230dd2SCorentin Chary             vnc_client_error(vs);
24183e230dd2SCorentin Chary             break;
24193e230dd2SCorentin Chary         }
24203e230dd2SCorentin Chary         break;
24213e230dd2SCorentin Chary     default:
24223e230dd2SCorentin Chary         printf("Msg: %d\n", data[0]);
24233e230dd2SCorentin Chary         vnc_client_error(vs);
24243e230dd2SCorentin Chary         break;
24253e230dd2SCorentin Chary     }
24263e230dd2SCorentin Chary 
24273e230dd2SCorentin Chary     vnc_read_when(vs, protocol_client_msg, 1);
24283e230dd2SCorentin Chary     return 0;
24293e230dd2SCorentin Chary }
24303e230dd2SCorentin Chary 
24313e230dd2SCorentin Chary static int protocol_client_init(VncState *vs, uint8_t *data, size_t len)
24323e230dd2SCorentin Chary {
24333e230dd2SCorentin Chary     char buf[1024];
24348cf36489SGerd Hoffmann     VncShareMode mode;
24353e230dd2SCorentin Chary     int size;
24363e230dd2SCorentin Chary 
24378cf36489SGerd Hoffmann     mode = data[0] ? VNC_SHARE_MODE_SHARED : VNC_SHARE_MODE_EXCLUSIVE;
24388cf36489SGerd Hoffmann     switch (vs->vd->share_policy) {
24398cf36489SGerd Hoffmann     case VNC_SHARE_POLICY_IGNORE:
24408cf36489SGerd Hoffmann         /*
24418cf36489SGerd Hoffmann          * Ignore the shared flag.  Nothing to do here.
24428cf36489SGerd Hoffmann          *
24438cf36489SGerd Hoffmann          * Doesn't conform to the rfb spec but is traditional qemu
24448cf36489SGerd Hoffmann          * behavior, thus left here as option for compatibility
24458cf36489SGerd Hoffmann          * reasons.
24468cf36489SGerd Hoffmann          */
24478cf36489SGerd Hoffmann         break;
24488cf36489SGerd Hoffmann     case VNC_SHARE_POLICY_ALLOW_EXCLUSIVE:
24498cf36489SGerd Hoffmann         /*
24508cf36489SGerd Hoffmann          * Policy: Allow clients ask for exclusive access.
24518cf36489SGerd Hoffmann          *
24528cf36489SGerd Hoffmann          * Implementation: When a client asks for exclusive access,
24538cf36489SGerd Hoffmann          * disconnect all others. Shared connects are allowed as long
24548cf36489SGerd Hoffmann          * as no exclusive connection exists.
24558cf36489SGerd Hoffmann          *
24568cf36489SGerd Hoffmann          * This is how the rfb spec suggests to handle the shared flag.
24578cf36489SGerd Hoffmann          */
24588cf36489SGerd Hoffmann         if (mode == VNC_SHARE_MODE_EXCLUSIVE) {
24598cf36489SGerd Hoffmann             VncState *client;
24608cf36489SGerd Hoffmann             QTAILQ_FOREACH(client, &vs->vd->clients, next) {
24618cf36489SGerd Hoffmann                 if (vs == client) {
24628cf36489SGerd Hoffmann                     continue;
24638cf36489SGerd Hoffmann                 }
24648cf36489SGerd Hoffmann                 if (client->share_mode != VNC_SHARE_MODE_EXCLUSIVE &&
24658cf36489SGerd Hoffmann                     client->share_mode != VNC_SHARE_MODE_SHARED) {
24668cf36489SGerd Hoffmann                     continue;
24678cf36489SGerd Hoffmann                 }
24688cf36489SGerd Hoffmann                 vnc_disconnect_start(client);
24698cf36489SGerd Hoffmann             }
24708cf36489SGerd Hoffmann         }
24718cf36489SGerd Hoffmann         if (mode == VNC_SHARE_MODE_SHARED) {
24728cf36489SGerd Hoffmann             if (vs->vd->num_exclusive > 0) {
24738cf36489SGerd Hoffmann                 vnc_disconnect_start(vs);
24748cf36489SGerd Hoffmann                 return 0;
24758cf36489SGerd Hoffmann             }
24768cf36489SGerd Hoffmann         }
24778cf36489SGerd Hoffmann         break;
24788cf36489SGerd Hoffmann     case VNC_SHARE_POLICY_FORCE_SHARED:
24798cf36489SGerd Hoffmann         /*
24808cf36489SGerd Hoffmann          * Policy: Shared connects only.
24818cf36489SGerd Hoffmann          * Implementation: Disallow clients asking for exclusive access.
24828cf36489SGerd Hoffmann          *
24838cf36489SGerd Hoffmann          * Useful for shared desktop sessions where you don't want
24848cf36489SGerd Hoffmann          * someone forgetting to say -shared when running the vnc
24858cf36489SGerd Hoffmann          * client disconnect everybody else.
24868cf36489SGerd Hoffmann          */
24878cf36489SGerd Hoffmann         if (mode == VNC_SHARE_MODE_EXCLUSIVE) {
24888cf36489SGerd Hoffmann             vnc_disconnect_start(vs);
24898cf36489SGerd Hoffmann             return 0;
24908cf36489SGerd Hoffmann         }
24918cf36489SGerd Hoffmann         break;
24928cf36489SGerd Hoffmann     }
24938cf36489SGerd Hoffmann     vnc_set_share_mode(vs, mode);
24948cf36489SGerd Hoffmann 
2495e5f34cddSGerd Hoffmann     if (vs->vd->num_shared > vs->vd->connections_limit) {
2496e5f34cddSGerd Hoffmann         vnc_disconnect_start(vs);
2497e5f34cddSGerd Hoffmann         return 0;
2498e5f34cddSGerd Hoffmann     }
2499e5f34cddSGerd Hoffmann 
2500bea60dd7SPeter Lieven     vs->client_width = pixman_image_get_width(vs->vd->server);
2501bea60dd7SPeter Lieven     vs->client_height = pixman_image_get_height(vs->vd->server);
25023e230dd2SCorentin Chary     vnc_write_u16(vs, vs->client_width);
25033e230dd2SCorentin Chary     vnc_write_u16(vs, vs->client_height);
25043e230dd2SCorentin Chary 
25053e230dd2SCorentin Chary     pixel_format_message(vs);
25063e230dd2SCorentin Chary 
25073e230dd2SCorentin Chary     if (qemu_name)
25083e230dd2SCorentin Chary         size = snprintf(buf, sizeof(buf), "QEMU (%s)", qemu_name);
25093e230dd2SCorentin Chary     else
25103e230dd2SCorentin Chary         size = snprintf(buf, sizeof(buf), "QEMU");
25113e230dd2SCorentin Chary 
25123e230dd2SCorentin Chary     vnc_write_u32(vs, size);
25133e230dd2SCorentin Chary     vnc_write(vs, buf, size);
25143e230dd2SCorentin Chary     vnc_flush(vs);
25153e230dd2SCorentin Chary 
25163e230dd2SCorentin Chary     vnc_client_cache_auth(vs);
2517fb6ba0d5SWenchao Xia     vnc_qmp_event(vs, QAPI_EVENT_VNC_INITIALIZED);
25183e230dd2SCorentin Chary 
25193e230dd2SCorentin Chary     vnc_read_when(vs, protocol_client_msg, 1);
25203e230dd2SCorentin Chary 
25213e230dd2SCorentin Chary     return 0;
25223e230dd2SCorentin Chary }
25233e230dd2SCorentin Chary 
25243e230dd2SCorentin Chary void start_client_init(VncState *vs)
25253e230dd2SCorentin Chary {
25263e230dd2SCorentin Chary     vnc_read_when(vs, protocol_client_init, 1);
25273e230dd2SCorentin Chary }
25283e230dd2SCorentin Chary 
25293e230dd2SCorentin Chary static void make_challenge(VncState *vs)
25303e230dd2SCorentin Chary {
25313e230dd2SCorentin Chary     int i;
25323e230dd2SCorentin Chary 
25333e230dd2SCorentin Chary     srand(time(NULL)+getpid()+getpid()*987654+rand());
25343e230dd2SCorentin Chary 
25353e230dd2SCorentin Chary     for (i = 0 ; i < sizeof(vs->challenge) ; i++)
25363e230dd2SCorentin Chary         vs->challenge[i] = (int) (256.0*rand()/(RAND_MAX+1.0));
25373e230dd2SCorentin Chary }
25383e230dd2SCorentin Chary 
25393e230dd2SCorentin Chary static int protocol_client_auth_vnc(VncState *vs, uint8_t *data, size_t len)
25403e230dd2SCorentin Chary {
25413e230dd2SCorentin Chary     unsigned char response[VNC_AUTH_CHALLENGE_SIZE];
25423e230dd2SCorentin Chary     int i, j, pwlen;
25433e230dd2SCorentin Chary     unsigned char key[8];
25443c9405a0SGerd Hoffmann     time_t now = time(NULL);
25453e230dd2SCorentin Chary 
25461cd20f8bSAnthony Liguori     if (!vs->vd->password) {
25473e230dd2SCorentin Chary         VNC_DEBUG("No password configured on server");
25486bffdf0fSGerd Hoffmann         goto reject;
25493e230dd2SCorentin Chary     }
25503c9405a0SGerd Hoffmann     if (vs->vd->expires < now) {
25513c9405a0SGerd Hoffmann         VNC_DEBUG("Password is expired");
25523c9405a0SGerd Hoffmann         goto reject;
25533c9405a0SGerd Hoffmann     }
25543e230dd2SCorentin Chary 
25553e230dd2SCorentin Chary     memcpy(response, vs->challenge, VNC_AUTH_CHALLENGE_SIZE);
25563e230dd2SCorentin Chary 
25573e230dd2SCorentin Chary     /* Calculate the expected challenge response */
25583e230dd2SCorentin Chary     pwlen = strlen(vs->vd->password);
25593e230dd2SCorentin Chary     for (i=0; i<sizeof(key); i++)
25603e230dd2SCorentin Chary         key[i] = i<pwlen ? vs->vd->password[i] : 0;
25613e230dd2SCorentin Chary     deskey(key, EN0);
25623e230dd2SCorentin Chary     for (j = 0; j < VNC_AUTH_CHALLENGE_SIZE; j += 8)
25633e230dd2SCorentin Chary         des(response+j, response+j);
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     }
25766bffdf0fSGerd Hoffmann     return 0;
25776bffdf0fSGerd Hoffmann 
25786bffdf0fSGerd Hoffmann reject:
25793e230dd2SCorentin Chary     vnc_write_u32(vs, 1); /* Reject auth */
25803e230dd2SCorentin Chary     if (vs->minor >= 8) {
25813e230dd2SCorentin Chary         static const char err[] = "Authentication failed";
25823e230dd2SCorentin Chary         vnc_write_u32(vs, sizeof(err));
25833e230dd2SCorentin Chary         vnc_write(vs, err, sizeof(err));
25843e230dd2SCorentin Chary     }
25853e230dd2SCorentin Chary     vnc_flush(vs);
25863e230dd2SCorentin Chary     vnc_client_error(vs);
25873e230dd2SCorentin Chary     return 0;
25883e230dd2SCorentin Chary }
25893e230dd2SCorentin Chary 
25903e230dd2SCorentin Chary void start_auth_vnc(VncState *vs)
25913e230dd2SCorentin Chary {
25923e230dd2SCorentin Chary     make_challenge(vs);
25933e230dd2SCorentin Chary     /* Send client a 'random' challenge */
25943e230dd2SCorentin Chary     vnc_write(vs, vs->challenge, sizeof(vs->challenge));
25953e230dd2SCorentin Chary     vnc_flush(vs);
25963e230dd2SCorentin Chary 
25973e230dd2SCorentin Chary     vnc_read_when(vs, protocol_client_auth_vnc, sizeof(vs->challenge));
25983e230dd2SCorentin Chary }
25993e230dd2SCorentin Chary 
26003e230dd2SCorentin Chary 
26013e230dd2SCorentin Chary static int protocol_client_auth(VncState *vs, uint8_t *data, size_t len)
26023e230dd2SCorentin Chary {
26033e230dd2SCorentin Chary     /* We only advertise 1 auth scheme at a time, so client
26043e230dd2SCorentin Chary      * must pick the one we sent. Verify this */
26057e7e2ebcSDaniel P. Berrange     if (data[0] != vs->auth) { /* Reject auth */
26063e230dd2SCorentin Chary        VNC_DEBUG("Reject auth %d because it didn't match advertized\n", (int)data[0]);
26073e230dd2SCorentin Chary        vnc_write_u32(vs, 1);
26083e230dd2SCorentin Chary        if (vs->minor >= 8) {
26093e230dd2SCorentin Chary            static const char err[] = "Authentication failed";
26103e230dd2SCorentin Chary            vnc_write_u32(vs, sizeof(err));
26113e230dd2SCorentin Chary            vnc_write(vs, err, sizeof(err));
26123e230dd2SCorentin Chary        }
26133e230dd2SCorentin Chary        vnc_client_error(vs);
26143e230dd2SCorentin Chary     } else { /* Accept requested auth */
26153e230dd2SCorentin Chary        VNC_DEBUG("Client requested auth %d\n", (int)data[0]);
26167e7e2ebcSDaniel P. Berrange        switch (vs->auth) {
26173e230dd2SCorentin Chary        case VNC_AUTH_NONE:
26183e230dd2SCorentin Chary            VNC_DEBUG("Accept auth none\n");
26193e230dd2SCorentin Chary            if (vs->minor >= 8) {
26203e230dd2SCorentin Chary                vnc_write_u32(vs, 0); /* Accept auth completion */
26213e230dd2SCorentin Chary                vnc_flush(vs);
26223e230dd2SCorentin Chary            }
26233e230dd2SCorentin Chary            start_client_init(vs);
26243e230dd2SCorentin Chary            break;
26253e230dd2SCorentin Chary 
26263e230dd2SCorentin Chary        case VNC_AUTH_VNC:
26273e230dd2SCorentin Chary            VNC_DEBUG("Start VNC auth\n");
26283e230dd2SCorentin Chary            start_auth_vnc(vs);
26293e230dd2SCorentin Chary            break;
26303e230dd2SCorentin Chary 
26313e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS
26323e230dd2SCorentin Chary        case VNC_AUTH_VENCRYPT:
26333a93113aSDong Xu Wang            VNC_DEBUG("Accept VeNCrypt auth\n");
26343e230dd2SCorentin Chary            start_auth_vencrypt(vs);
26353e230dd2SCorentin Chary            break;
26363e230dd2SCorentin Chary #endif /* CONFIG_VNC_TLS */
26373e230dd2SCorentin Chary 
26383e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL
26393e230dd2SCorentin Chary        case VNC_AUTH_SASL:
26403e230dd2SCorentin Chary            VNC_DEBUG("Accept SASL auth\n");
26413e230dd2SCorentin Chary            start_auth_sasl(vs);
26423e230dd2SCorentin Chary            break;
26433e230dd2SCorentin Chary #endif /* CONFIG_VNC_SASL */
26443e230dd2SCorentin Chary 
26453e230dd2SCorentin Chary        default: /* Should not be possible, but just in case */
26467e7e2ebcSDaniel P. Berrange            VNC_DEBUG("Reject auth %d server code bug\n", vs->auth);
26473e230dd2SCorentin Chary            vnc_write_u8(vs, 1);
26483e230dd2SCorentin Chary            if (vs->minor >= 8) {
26493e230dd2SCorentin Chary                static const char err[] = "Authentication failed";
26503e230dd2SCorentin Chary                vnc_write_u32(vs, sizeof(err));
26513e230dd2SCorentin Chary                vnc_write(vs, err, sizeof(err));
26523e230dd2SCorentin Chary            }
26533e230dd2SCorentin Chary            vnc_client_error(vs);
26543e230dd2SCorentin Chary        }
26553e230dd2SCorentin Chary     }
26563e230dd2SCorentin Chary     return 0;
26573e230dd2SCorentin Chary }
26583e230dd2SCorentin Chary 
26593e230dd2SCorentin Chary static int protocol_version(VncState *vs, uint8_t *version, size_t len)
26603e230dd2SCorentin Chary {
26613e230dd2SCorentin Chary     char local[13];
26623e230dd2SCorentin Chary 
26633e230dd2SCorentin Chary     memcpy(local, version, 12);
26643e230dd2SCorentin Chary     local[12] = 0;
26653e230dd2SCorentin Chary 
26663e230dd2SCorentin Chary     if (sscanf(local, "RFB %03d.%03d\n", &vs->major, &vs->minor) != 2) {
26673e230dd2SCorentin Chary         VNC_DEBUG("Malformed protocol version %s\n", local);
26683e230dd2SCorentin Chary         vnc_client_error(vs);
26693e230dd2SCorentin Chary         return 0;
26703e230dd2SCorentin Chary     }
26713e230dd2SCorentin Chary     VNC_DEBUG("Client request protocol version %d.%d\n", vs->major, vs->minor);
26723e230dd2SCorentin Chary     if (vs->major != 3 ||
26733e230dd2SCorentin Chary         (vs->minor != 3 &&
26743e230dd2SCorentin Chary          vs->minor != 4 &&
26753e230dd2SCorentin Chary          vs->minor != 5 &&
26763e230dd2SCorentin Chary          vs->minor != 7 &&
26773e230dd2SCorentin Chary          vs->minor != 8)) {
26783e230dd2SCorentin Chary         VNC_DEBUG("Unsupported client version\n");
26793e230dd2SCorentin Chary         vnc_write_u32(vs, VNC_AUTH_INVALID);
26803e230dd2SCorentin Chary         vnc_flush(vs);
26813e230dd2SCorentin Chary         vnc_client_error(vs);
26823e230dd2SCorentin Chary         return 0;
26833e230dd2SCorentin Chary     }
26843e230dd2SCorentin Chary     /* Some broken clients report v3.4 or v3.5, which spec requires to be treated
26853e230dd2SCorentin Chary      * as equivalent to v3.3 by servers
26863e230dd2SCorentin Chary      */
26873e230dd2SCorentin Chary     if (vs->minor == 4 || vs->minor == 5)
26883e230dd2SCorentin Chary         vs->minor = 3;
26893e230dd2SCorentin Chary 
26903e230dd2SCorentin Chary     if (vs->minor == 3) {
26917e7e2ebcSDaniel P. Berrange         if (vs->auth == VNC_AUTH_NONE) {
26923e230dd2SCorentin Chary             VNC_DEBUG("Tell client auth none\n");
26937e7e2ebcSDaniel P. Berrange             vnc_write_u32(vs, vs->auth);
26943e230dd2SCorentin Chary             vnc_flush(vs);
26953e230dd2SCorentin Chary             start_client_init(vs);
26967e7e2ebcSDaniel P. Berrange        } else if (vs->auth == VNC_AUTH_VNC) {
26973e230dd2SCorentin Chary             VNC_DEBUG("Tell client VNC auth\n");
26987e7e2ebcSDaniel P. Berrange             vnc_write_u32(vs, vs->auth);
26993e230dd2SCorentin Chary             vnc_flush(vs);
27003e230dd2SCorentin Chary             start_auth_vnc(vs);
27013e230dd2SCorentin Chary        } else {
27027e7e2ebcSDaniel P. Berrange             VNC_DEBUG("Unsupported auth %d for protocol 3.3\n", vs->auth);
27033e230dd2SCorentin Chary             vnc_write_u32(vs, VNC_AUTH_INVALID);
27043e230dd2SCorentin Chary             vnc_flush(vs);
27053e230dd2SCorentin Chary             vnc_client_error(vs);
27063e230dd2SCorentin Chary        }
27073e230dd2SCorentin Chary     } else {
27087e7e2ebcSDaniel P. Berrange         VNC_DEBUG("Telling client we support auth %d\n", vs->auth);
27093e230dd2SCorentin Chary         vnc_write_u8(vs, 1); /* num auth */
27107e7e2ebcSDaniel P. Berrange         vnc_write_u8(vs, vs->auth);
27113e230dd2SCorentin Chary         vnc_read_when(vs, protocol_client_auth, 1);
27123e230dd2SCorentin Chary         vnc_flush(vs);
27133e230dd2SCorentin Chary     }
27143e230dd2SCorentin Chary 
27153e230dd2SCorentin Chary     return 0;
27163e230dd2SCorentin Chary }
27173e230dd2SCorentin Chary 
2718999342a0SCorentin Chary static VncRectStat *vnc_stat_rect(VncDisplay *vd, int x, int y)
2719999342a0SCorentin Chary {
2720999342a0SCorentin Chary     struct VncSurface *vs = &vd->guest;
2721999342a0SCorentin Chary 
2722999342a0SCorentin Chary     return &vs->stats[y / VNC_STAT_RECT][x / VNC_STAT_RECT];
2723999342a0SCorentin Chary }
2724999342a0SCorentin Chary 
27257d964c9dSCorentin Chary void vnc_sent_lossy_rect(VncState *vs, int x, int y, int w, int h)
27267d964c9dSCorentin Chary {
27277d964c9dSCorentin Chary     int i, j;
27287d964c9dSCorentin Chary 
27297d964c9dSCorentin Chary     w = (x + w) / VNC_STAT_RECT;
27307d964c9dSCorentin Chary     h = (y + h) / VNC_STAT_RECT;
27317d964c9dSCorentin Chary     x /= VNC_STAT_RECT;
27327d964c9dSCorentin Chary     y /= VNC_STAT_RECT;
27337d964c9dSCorentin Chary 
2734207f328aSCorentin Chary     for (j = y; j <= h; j++) {
2735207f328aSCorentin Chary         for (i = x; i <= w; i++) {
27367d964c9dSCorentin Chary             vs->lossy_rect[j][i] = 1;
27377d964c9dSCorentin Chary         }
27387d964c9dSCorentin Chary     }
27397d964c9dSCorentin Chary }
27407d964c9dSCorentin Chary 
27417d964c9dSCorentin Chary static int vnc_refresh_lossy_rect(VncDisplay *vd, int x, int y)
27427d964c9dSCorentin Chary {
27437d964c9dSCorentin Chary     VncState *vs;
27447d964c9dSCorentin Chary     int sty = y / VNC_STAT_RECT;
27457d964c9dSCorentin Chary     int stx = x / VNC_STAT_RECT;
27467d964c9dSCorentin Chary     int has_dirty = 0;
27477d964c9dSCorentin Chary 
27487d964c9dSCorentin Chary     y = y / VNC_STAT_RECT * VNC_STAT_RECT;
27497d964c9dSCorentin Chary     x = x / VNC_STAT_RECT * VNC_STAT_RECT;
27507d964c9dSCorentin Chary 
27517d964c9dSCorentin Chary     QTAILQ_FOREACH(vs, &vd->clients, next) {
2752bc2429b9SCorentin Chary         int j;
27537d964c9dSCorentin Chary 
27547d964c9dSCorentin Chary         /* kernel send buffers are full -> refresh later */
27557d964c9dSCorentin Chary         if (vs->output.offset) {
27567d964c9dSCorentin Chary             continue;
27577d964c9dSCorentin Chary         }
27587d964c9dSCorentin Chary 
27597d964c9dSCorentin Chary         if (!vs->lossy_rect[sty][stx]) {
27607d964c9dSCorentin Chary             continue;
27617d964c9dSCorentin Chary         }
2762207f328aSCorentin Chary 
27637d964c9dSCorentin Chary         vs->lossy_rect[sty][stx] = 0;
27647d964c9dSCorentin Chary         for (j = 0; j < VNC_STAT_RECT; ++j) {
2765b4c85ddcSPeter Lieven             bitmap_set(vs->dirty[y + j],
2766b4c85ddcSPeter Lieven                        x / VNC_DIRTY_PIXELS_PER_BIT,
2767b4c85ddcSPeter Lieven                        VNC_STAT_RECT / VNC_DIRTY_PIXELS_PER_BIT);
27687d964c9dSCorentin Chary         }
27697d964c9dSCorentin Chary         has_dirty++;
27707d964c9dSCorentin Chary     }
2771207f328aSCorentin Chary 
27727d964c9dSCorentin Chary     return has_dirty;
27737d964c9dSCorentin Chary }
27747d964c9dSCorentin Chary 
27757d964c9dSCorentin Chary static int vnc_update_stats(VncDisplay *vd,  struct timeval * tv)
2776999342a0SCorentin Chary {
27779f64916dSGerd Hoffmann     int width = pixman_image_get_width(vd->guest.fb);
27789f64916dSGerd Hoffmann     int height = pixman_image_get_height(vd->guest.fb);
2779999342a0SCorentin Chary     int x, y;
2780999342a0SCorentin Chary     struct timeval res;
27817d964c9dSCorentin Chary     int has_dirty = 0;
2782999342a0SCorentin Chary 
27839f64916dSGerd Hoffmann     for (y = 0; y < height; y += VNC_STAT_RECT) {
27849f64916dSGerd Hoffmann         for (x = 0; x < width; x += VNC_STAT_RECT) {
2785999342a0SCorentin Chary             VncRectStat *rect = vnc_stat_rect(vd, x, y);
2786999342a0SCorentin Chary 
2787999342a0SCorentin Chary             rect->updated = false;
2788999342a0SCorentin Chary         }
2789999342a0SCorentin Chary     }
2790999342a0SCorentin Chary 
2791ad620c29SBlue Swirl     qemu_timersub(tv, &VNC_REFRESH_STATS, &res);
2792999342a0SCorentin Chary 
2793999342a0SCorentin Chary     if (timercmp(&vd->guest.last_freq_check, &res, >)) {
27947d964c9dSCorentin Chary         return has_dirty;
2795999342a0SCorentin Chary     }
2796999342a0SCorentin Chary     vd->guest.last_freq_check = *tv;
2797999342a0SCorentin Chary 
27989f64916dSGerd Hoffmann     for (y = 0; y < height; y += VNC_STAT_RECT) {
27999f64916dSGerd Hoffmann         for (x = 0; x < width; x += VNC_STAT_RECT) {
2800999342a0SCorentin Chary             VncRectStat *rect= vnc_stat_rect(vd, x, y);
2801999342a0SCorentin Chary             int count = ARRAY_SIZE(rect->times);
2802999342a0SCorentin Chary             struct timeval min, max;
2803999342a0SCorentin Chary 
2804999342a0SCorentin Chary             if (!timerisset(&rect->times[count - 1])) {
2805999342a0SCorentin Chary                 continue ;
2806999342a0SCorentin Chary             }
2807999342a0SCorentin Chary 
2808999342a0SCorentin Chary             max = rect->times[(rect->idx + count - 1) % count];
2809ad620c29SBlue Swirl             qemu_timersub(tv, &max, &res);
2810999342a0SCorentin Chary 
2811999342a0SCorentin Chary             if (timercmp(&res, &VNC_REFRESH_LOSSY, >)) {
2812999342a0SCorentin Chary                 rect->freq = 0;
28137d964c9dSCorentin Chary                 has_dirty += vnc_refresh_lossy_rect(vd, x, y);
2814999342a0SCorentin Chary                 memset(rect->times, 0, sizeof (rect->times));
2815999342a0SCorentin Chary                 continue ;
2816999342a0SCorentin Chary             }
2817999342a0SCorentin Chary 
2818999342a0SCorentin Chary             min = rect->times[rect->idx];
2819999342a0SCorentin Chary             max = rect->times[(rect->idx + count - 1) % count];
2820ad620c29SBlue Swirl             qemu_timersub(&max, &min, &res);
2821999342a0SCorentin Chary 
2822999342a0SCorentin Chary             rect->freq = res.tv_sec + res.tv_usec / 1000000.;
2823999342a0SCorentin Chary             rect->freq /= count;
2824999342a0SCorentin Chary             rect->freq = 1. / rect->freq;
2825999342a0SCorentin Chary         }
2826999342a0SCorentin Chary     }
28277d964c9dSCorentin Chary     return has_dirty;
2828999342a0SCorentin Chary }
2829999342a0SCorentin Chary 
2830999342a0SCorentin Chary double vnc_update_freq(VncState *vs, int x, int y, int w, int h)
2831999342a0SCorentin Chary {
2832999342a0SCorentin Chary     int i, j;
2833999342a0SCorentin Chary     double total = 0;
2834999342a0SCorentin Chary     int num = 0;
2835999342a0SCorentin Chary 
2836999342a0SCorentin Chary     x =  (x / VNC_STAT_RECT) * VNC_STAT_RECT;
2837999342a0SCorentin Chary     y =  (y / VNC_STAT_RECT) * VNC_STAT_RECT;
2838999342a0SCorentin Chary 
2839999342a0SCorentin Chary     for (j = y; j <= y + h; j += VNC_STAT_RECT) {
2840999342a0SCorentin Chary         for (i = x; i <= x + w; i += VNC_STAT_RECT) {
2841999342a0SCorentin Chary             total += vnc_stat_rect(vs->vd, i, j)->freq;
2842999342a0SCorentin Chary             num++;
2843999342a0SCorentin Chary         }
2844999342a0SCorentin Chary     }
2845999342a0SCorentin Chary 
2846999342a0SCorentin Chary     if (num) {
2847999342a0SCorentin Chary         return total / num;
2848999342a0SCorentin Chary     } else {
2849999342a0SCorentin Chary         return 0;
2850999342a0SCorentin Chary     }
2851999342a0SCorentin Chary }
2852999342a0SCorentin Chary 
2853999342a0SCorentin Chary static void vnc_rect_updated(VncDisplay *vd, int x, int y, struct timeval * tv)
2854999342a0SCorentin Chary {
2855999342a0SCorentin Chary     VncRectStat *rect;
2856999342a0SCorentin Chary 
2857999342a0SCorentin Chary     rect = vnc_stat_rect(vd, x, y);
2858999342a0SCorentin Chary     if (rect->updated) {
2859999342a0SCorentin Chary         return ;
2860999342a0SCorentin Chary     }
2861999342a0SCorentin Chary     rect->times[rect->idx] = *tv;
2862999342a0SCorentin Chary     rect->idx = (rect->idx + 1) % ARRAY_SIZE(rect->times);
2863999342a0SCorentin Chary     rect->updated = true;
2864999342a0SCorentin Chary }
2865999342a0SCorentin Chary 
28663e230dd2SCorentin Chary static int vnc_refresh_server_surface(VncDisplay *vd)
28673e230dd2SCorentin Chary {
2868bea60dd7SPeter Lieven     int width = MIN(pixman_image_get_width(vd->guest.fb),
2869bea60dd7SPeter Lieven                     pixman_image_get_width(vd->server));
2870bea60dd7SPeter Lieven     int height = MIN(pixman_image_get_height(vd->guest.fb),
2871bea60dd7SPeter Lieven                      pixman_image_get_height(vd->server));
2872bea60dd7SPeter Lieven     int cmp_bytes, server_stride, min_stride, guest_stride, y = 0;
287312b316d4SPeter Lieven     uint8_t *guest_row0 = NULL, *server_row0;
28743e230dd2SCorentin Chary     VncState *vs;
28753e230dd2SCorentin Chary     int has_dirty = 0;
28769f64916dSGerd Hoffmann     pixman_image_t *tmpbuf = NULL;
28773e230dd2SCorentin Chary 
287880e0c8c3SCorentin Chary     struct timeval tv = { 0, 0 };
2879999342a0SCorentin Chary 
288080e0c8c3SCorentin Chary     if (!vd->non_adaptive) {
2881999342a0SCorentin Chary         gettimeofday(&tv, NULL);
28827d964c9dSCorentin Chary         has_dirty = vnc_update_stats(vd, &tv);
288380e0c8c3SCorentin Chary     }
2884999342a0SCorentin Chary 
28853e230dd2SCorentin Chary     /*
28863e230dd2SCorentin Chary      * Walk through the guest dirty map.
28873e230dd2SCorentin Chary      * Check and copy modified bits from guest to server surface.
28883e230dd2SCorentin Chary      * Update server dirty map.
28893e230dd2SCorentin Chary      */
2890bea60dd7SPeter Lieven     server_row0 = (uint8_t *)pixman_image_get_data(vd->server);
2891bea60dd7SPeter Lieven     server_stride = guest_stride = pixman_image_get_stride(vd->server);
2892bea60dd7SPeter Lieven     cmp_bytes = MIN(VNC_DIRTY_PIXELS_PER_BIT * VNC_SERVER_FB_BYTES,
2893bea60dd7SPeter Lieven                     server_stride);
28949f64916dSGerd Hoffmann     if (vd->guest.format != VNC_SERVER_FB_FORMAT) {
28959f64916dSGerd Hoffmann         int width = pixman_image_get_width(vd->server);
28969f64916dSGerd Hoffmann         tmpbuf = qemu_pixman_linebuf_create(VNC_SERVER_FB_FORMAT, width);
289712b316d4SPeter Lieven     } else {
289812b316d4SPeter Lieven         guest_row0 = (uint8_t *)pixman_image_get_data(vd->guest.fb);
289912b316d4SPeter Lieven         guest_stride = pixman_image_get_stride(vd->guest.fb);
29009f64916dSGerd Hoffmann     }
2901bea60dd7SPeter Lieven     min_stride = MIN(server_stride, guest_stride);
290212b316d4SPeter Lieven 
290312b316d4SPeter Lieven     for (;;) {
29043e230dd2SCorentin Chary         int x;
290512b316d4SPeter Lieven         uint8_t *guest_ptr, *server_ptr;
290612b316d4SPeter Lieven         unsigned long offset = find_next_bit((unsigned long *) &vd->guest.dirty,
290712b316d4SPeter Lieven                                              height * VNC_DIRTY_BPL(&vd->guest),
290812b316d4SPeter Lieven                                              y * VNC_DIRTY_BPL(&vd->guest));
290912b316d4SPeter Lieven         if (offset == height * VNC_DIRTY_BPL(&vd->guest)) {
291012b316d4SPeter Lieven             /* no more dirty bits */
291112b316d4SPeter Lieven             break;
291212b316d4SPeter Lieven         }
291312b316d4SPeter Lieven         y = offset / VNC_DIRTY_BPL(&vd->guest);
291412b316d4SPeter Lieven         x = offset % VNC_DIRTY_BPL(&vd->guest);
291512b316d4SPeter Lieven 
291612b316d4SPeter Lieven         server_ptr = server_row0 + y * server_stride + x * cmp_bytes;
29173e230dd2SCorentin Chary 
29189f64916dSGerd Hoffmann         if (vd->guest.format != VNC_SERVER_FB_FORMAT) {
2919bc210eb1SGerd Hoffmann             qemu_pixman_linebuf_fill(tmpbuf, vd->guest.fb, width, 0, y);
29209f64916dSGerd Hoffmann             guest_ptr = (uint8_t *)pixman_image_get_data(tmpbuf);
29219f64916dSGerd Hoffmann         } else {
292212b316d4SPeter Lieven             guest_ptr = guest_row0 + y * guest_stride;
29239f64916dSGerd Hoffmann         }
292412b316d4SPeter Lieven         guest_ptr += x * cmp_bytes;
29253e230dd2SCorentin Chary 
292612b316d4SPeter Lieven         for (; x < DIV_ROUND_UP(width, VNC_DIRTY_PIXELS_PER_BIT);
292712b316d4SPeter Lieven              x++, guest_ptr += cmp_bytes, server_ptr += cmp_bytes) {
2928bea60dd7SPeter Lieven             int _cmp_bytes = cmp_bytes;
292912b316d4SPeter Lieven             if (!test_and_clear_bit(x, vd->guest.dirty[y])) {
29303e230dd2SCorentin Chary                 continue;
2931b4c85ddcSPeter Lieven             }
2932bea60dd7SPeter Lieven             if ((x + 1) * cmp_bytes > min_stride) {
2933bea60dd7SPeter Lieven                 _cmp_bytes = min_stride - x * cmp_bytes;
2934bea60dd7SPeter Lieven             }
2935bea60dd7SPeter Lieven             if (memcmp(server_ptr, guest_ptr, _cmp_bytes) == 0) {
29363e230dd2SCorentin Chary                 continue;
2937b4c85ddcSPeter Lieven             }
2938bea60dd7SPeter Lieven             memcpy(server_ptr, guest_ptr, _cmp_bytes);
293912b316d4SPeter Lieven             if (!vd->non_adaptive) {
294012b316d4SPeter Lieven                 vnc_rect_updated(vd, x * VNC_DIRTY_PIXELS_PER_BIT,
294112b316d4SPeter Lieven                                  y, &tv);
294212b316d4SPeter Lieven             }
29433e230dd2SCorentin Chary             QTAILQ_FOREACH(vs, &vd->clients, next) {
294412b316d4SPeter Lieven                 set_bit(x, vs->dirty[y]);
29453e230dd2SCorentin Chary             }
29463e230dd2SCorentin Chary             has_dirty++;
29473e230dd2SCorentin Chary         }
294812b316d4SPeter Lieven 
294912b316d4SPeter Lieven         y++;
29503e230dd2SCorentin Chary     }
29519f64916dSGerd Hoffmann     qemu_pixman_image_unref(tmpbuf);
29523e230dd2SCorentin Chary     return has_dirty;
29533e230dd2SCorentin Chary }
29543e230dd2SCorentin Chary 
29550f7b2864SGerd Hoffmann static void vnc_refresh(DisplayChangeListener *dcl)
29563e230dd2SCorentin Chary {
29570f7b2864SGerd Hoffmann     VncDisplay *vd = container_of(dcl, VncDisplay, dcl);
29583e230dd2SCorentin Chary     VncState *vs, *vn;
29593e230dd2SCorentin Chary     int has_dirty, rects = 0;
29603e230dd2SCorentin Chary 
29619d6b2070SChenLiang     if (QTAILQ_EMPTY(&vd->clients)) {
29629d6b2070SChenLiang         update_displaychangelistener(&vd->dcl, VNC_REFRESH_INTERVAL_MAX);
29639d6b2070SChenLiang         return;
29649d6b2070SChenLiang     }
29659d6b2070SChenLiang 
29661d0d59feSGerd Hoffmann     graphic_hw_update(vd->dcl.con);
29673e230dd2SCorentin Chary 
2968bd023f95SCorentin Chary     if (vnc_trylock_display(vd)) {
29690f7b2864SGerd Hoffmann         update_displaychangelistener(&vd->dcl, VNC_REFRESH_INTERVAL_BASE);
2970bd023f95SCorentin Chary         return;
2971bd023f95SCorentin Chary     }
2972bd023f95SCorentin Chary 
29733e230dd2SCorentin Chary     has_dirty = vnc_refresh_server_surface(vd);
2974bd023f95SCorentin Chary     vnc_unlock_display(vd);
29753e230dd2SCorentin Chary 
29763e230dd2SCorentin Chary     QTAILQ_FOREACH_SAFE(vs, &vd->clients, next, vn) {
297738ee14f4SGerd Hoffmann         rects += vnc_update_client(vs, has_dirty, false);
29783e230dd2SCorentin Chary         /* vs might be free()ed here */
29793e230dd2SCorentin Chary     }
2980bd023f95SCorentin Chary 
29813e230dd2SCorentin Chary     if (has_dirty && rects) {
29820f7b2864SGerd Hoffmann         vd->dcl.update_interval /= 2;
29830f7b2864SGerd Hoffmann         if (vd->dcl.update_interval < VNC_REFRESH_INTERVAL_BASE) {
29840f7b2864SGerd Hoffmann             vd->dcl.update_interval = VNC_REFRESH_INTERVAL_BASE;
29850f7b2864SGerd Hoffmann         }
29863e230dd2SCorentin Chary     } else {
29870f7b2864SGerd Hoffmann         vd->dcl.update_interval += VNC_REFRESH_INTERVAL_INC;
29880f7b2864SGerd Hoffmann         if (vd->dcl.update_interval > VNC_REFRESH_INTERVAL_MAX) {
29890f7b2864SGerd Hoffmann             vd->dcl.update_interval = VNC_REFRESH_INTERVAL_MAX;
29903e230dd2SCorentin Chary         }
29913e230dd2SCorentin Chary     }
29923e230dd2SCorentin Chary }
29933e230dd2SCorentin Chary 
29942c8cf549SMichael Tokarev static void vnc_connect(VncDisplay *vd, int csock,
29952c8cf549SMichael Tokarev                         bool skipauth, bool websocket)
29963e230dd2SCorentin Chary {
29977267c094SAnthony Liguori     VncState *vs = g_malloc0(sizeof(VncState));
29987d964c9dSCorentin Chary     int i;
29997d964c9dSCorentin Chary 
30003e230dd2SCorentin Chary     vs->csock = csock;
3001d616ccc5SGerd Hoffmann     vs->vd = vd;
30027e7e2ebcSDaniel P. Berrange 
30037e7e2ebcSDaniel P. Berrange     if (skipauth) {
30047e7e2ebcSDaniel P. Berrange 	vs->auth = VNC_AUTH_NONE;
30057e7e2ebcSDaniel P. Berrange #ifdef CONFIG_VNC_TLS
30067e7e2ebcSDaniel P. Berrange 	vs->subauth = VNC_AUTH_INVALID;
30077e7e2ebcSDaniel P. Berrange #endif
30087e7e2ebcSDaniel P. Berrange     } else {
30097e7e2ebcSDaniel P. Berrange 	vs->auth = vd->auth;
30107e7e2ebcSDaniel P. Berrange #ifdef CONFIG_VNC_TLS
30117e7e2ebcSDaniel P. Berrange 	vs->subauth = vd->subauth;
30127e7e2ebcSDaniel P. Berrange #endif
30137e7e2ebcSDaniel P. Berrange     }
30147e7e2ebcSDaniel P. Berrange 
30157267c094SAnthony Liguori     vs->lossy_rect = g_malloc0(VNC_STAT_ROWS * sizeof (*vs->lossy_rect));
30167d964c9dSCorentin Chary     for (i = 0; i < VNC_STAT_ROWS; ++i) {
30177267c094SAnthony Liguori         vs->lossy_rect[i] = g_malloc0(VNC_STAT_COLS * sizeof (uint8_t));
30187d964c9dSCorentin Chary     }
30193e230dd2SCorentin Chary 
30203e230dd2SCorentin Chary     VNC_DEBUG("New client on socket %d\n", csock);
30210f7b2864SGerd Hoffmann     update_displaychangelistener(&vd->dcl, VNC_REFRESH_INTERVAL_BASE);
3022f9e8caccSStefan Hajnoczi     qemu_set_nonblock(vs->csock);
30237536ee4bSTim Hardeck #ifdef CONFIG_VNC_WS
30247536ee4bSTim Hardeck     if (websocket) {
30257536ee4bSTim Hardeck         vs->websocket = 1;
30260057a0d5STim Hardeck #ifdef CONFIG_VNC_TLS
30270057a0d5STim Hardeck         if (vd->tls.x509cert) {
30280057a0d5STim Hardeck             qemu_set_fd_handler2(vs->csock, NULL, vncws_tls_handshake_peek,
30290057a0d5STim Hardeck                                  NULL, vs);
30300057a0d5STim Hardeck         } else
30310057a0d5STim Hardeck #endif /* CONFIG_VNC_TLS */
30320057a0d5STim Hardeck         {
30330057a0d5STim Hardeck             qemu_set_fd_handler2(vs->csock, NULL, vncws_handshake_read,
30340057a0d5STim Hardeck                                  NULL, vs);
30350057a0d5STim Hardeck         }
30367536ee4bSTim Hardeck     } else
30377536ee4bSTim Hardeck #endif /* CONFIG_VNC_WS */
30387536ee4bSTim Hardeck     {
30393e230dd2SCorentin Chary         qemu_set_fd_handler2(vs->csock, NULL, vnc_client_read, NULL, vs);
30407536ee4bSTim Hardeck     }
30413e230dd2SCorentin Chary 
30423e230dd2SCorentin Chary     vnc_client_cache_addr(vs);
3043fb6ba0d5SWenchao Xia     vnc_qmp_event(vs, QAPI_EVENT_VNC_CONNECTED);
30448cf36489SGerd Hoffmann     vnc_set_share_mode(vs, VNC_SHARE_MODE_CONNECTING);
30453e230dd2SCorentin Chary 
30467536ee4bSTim Hardeck #ifdef CONFIG_VNC_WS
30477536ee4bSTim Hardeck     if (!vs->websocket)
30487536ee4bSTim Hardeck #endif
30497536ee4bSTim Hardeck     {
30507536ee4bSTim Hardeck         vnc_init_state(vs);
30517536ee4bSTim Hardeck     }
3052e5f34cddSGerd Hoffmann 
3053e5f34cddSGerd Hoffmann     if (vd->num_connecting > vd->connections_limit) {
3054e5f34cddSGerd Hoffmann         QTAILQ_FOREACH(vs, &vd->clients, next) {
3055e5f34cddSGerd Hoffmann             if (vs->share_mode == VNC_SHARE_MODE_CONNECTING) {
3056e5f34cddSGerd Hoffmann                 vnc_disconnect_start(vs);
3057e5f34cddSGerd Hoffmann                 return;
3058e5f34cddSGerd Hoffmann             }
3059e5f34cddSGerd Hoffmann         }
3060e5f34cddSGerd Hoffmann     }
30617536ee4bSTim Hardeck }
30627536ee4bSTim Hardeck 
30637536ee4bSTim Hardeck void vnc_init_state(VncState *vs)
30647536ee4bSTim Hardeck {
30656fd8e79aSTim Hardeck     vs->initialized = true;
30667536ee4bSTim Hardeck     VncDisplay *vd = vs->vd;
30677536ee4bSTim Hardeck 
30683e230dd2SCorentin Chary     vs->last_x = -1;
30693e230dd2SCorentin Chary     vs->last_y = -1;
30703e230dd2SCorentin Chary 
30713e230dd2SCorentin Chary     vs->as.freq = 44100;
30723e230dd2SCorentin Chary     vs->as.nchannels = 2;
30733e230dd2SCorentin Chary     vs->as.fmt = AUD_FMT_S16;
30743e230dd2SCorentin Chary     vs->as.endianness = 0;
30753e230dd2SCorentin Chary 
3076bd023f95SCorentin Chary     qemu_mutex_init(&vs->output_mutex);
3077175b2a6eSCorentin Chary     vs->bh = qemu_bh_new(vnc_jobs_bh, vs);
3078bd023f95SCorentin Chary 
3079e5f34cddSGerd Hoffmann     QTAILQ_INSERT_TAIL(&vd->clients, vs, next);
30803e230dd2SCorentin Chary 
30811d0d59feSGerd Hoffmann     graphic_hw_update(vd->dcl.con);
30823e230dd2SCorentin Chary 
30833e230dd2SCorentin Chary     vnc_write(vs, "RFB 003.008\n", 12);
30843e230dd2SCorentin Chary     vnc_flush(vs);
30853e230dd2SCorentin Chary     vnc_read_when(vs, protocol_version, 12);
30863e230dd2SCorentin Chary     reset_keys(vs);
30873e230dd2SCorentin Chary     if (vs->vd->lock_key_sync)
30883e230dd2SCorentin Chary         vs->led = qemu_add_led_event_handler(kbd_leds, vs);
30893e230dd2SCorentin Chary 
30903e230dd2SCorentin Chary     vs->mouse_mode_notifier.notify = check_pointer_type_change;
30913e230dd2SCorentin Chary     qemu_add_mouse_mode_change_notifier(&vs->mouse_mode_notifier);
30923e230dd2SCorentin Chary 
30933e230dd2SCorentin Chary     /* vs might be free()ed here */
30943e230dd2SCorentin Chary }
30953e230dd2SCorentin Chary 
30967536ee4bSTim Hardeck static void vnc_listen_read(void *opaque, bool websocket)
30973e230dd2SCorentin Chary {
30983e230dd2SCorentin Chary     VncDisplay *vs = opaque;
30993e230dd2SCorentin Chary     struct sockaddr_in addr;
31003e230dd2SCorentin Chary     socklen_t addrlen = sizeof(addr);
31017536ee4bSTim Hardeck     int csock;
31023e230dd2SCorentin Chary 
31033e230dd2SCorentin Chary     /* Catch-up */
31041d0d59feSGerd Hoffmann     graphic_hw_update(vs->dcl.con);
31057536ee4bSTim Hardeck #ifdef CONFIG_VNC_WS
31067536ee4bSTim Hardeck     if (websocket) {
31077536ee4bSTim Hardeck         csock = qemu_accept(vs->lwebsock, (struct sockaddr *)&addr, &addrlen);
31087536ee4bSTim Hardeck     } else
31097536ee4bSTim Hardeck #endif /* CONFIG_VNC_WS */
31107536ee4bSTim Hardeck     {
31117536ee4bSTim Hardeck         csock = qemu_accept(vs->lsock, (struct sockaddr *)&addr, &addrlen);
31127536ee4bSTim Hardeck     }
31133e230dd2SCorentin Chary 
31143e230dd2SCorentin Chary     if (csock != -1) {
311586152436SPeter Lieven         socket_set_nodelay(csock);
31162c8cf549SMichael Tokarev         vnc_connect(vs, csock, false, websocket);
31173e230dd2SCorentin Chary     }
31183e230dd2SCorentin Chary }
31193e230dd2SCorentin Chary 
31207536ee4bSTim Hardeck static void vnc_listen_regular_read(void *opaque)
31217536ee4bSTim Hardeck {
31222c8cf549SMichael Tokarev     vnc_listen_read(opaque, false);
31237536ee4bSTim Hardeck }
31247536ee4bSTim Hardeck 
31257536ee4bSTim Hardeck #ifdef CONFIG_VNC_WS
31267536ee4bSTim Hardeck static void vnc_listen_websocket_read(void *opaque)
31277536ee4bSTim Hardeck {
31282c8cf549SMichael Tokarev     vnc_listen_read(opaque, true);
31297536ee4bSTim Hardeck }
31307536ee4bSTim Hardeck #endif /* CONFIG_VNC_WS */
31317536ee4bSTim Hardeck 
31327c20b4a3SGerd Hoffmann static const DisplayChangeListenerOps dcl_ops = {
31337c20b4a3SGerd Hoffmann     .dpy_name             = "vnc",
31340f7b2864SGerd Hoffmann     .dpy_refresh          = vnc_refresh,
31357c20b4a3SGerd Hoffmann     .dpy_gfx_copy         = vnc_dpy_copy,
31367c20b4a3SGerd Hoffmann     .dpy_gfx_update       = vnc_dpy_update,
3137c12aeb86SGerd Hoffmann     .dpy_gfx_switch       = vnc_dpy_switch,
313834da30afSBenjamin Herrenschmidt     .dpy_gfx_check_format = qemu_pixman_check_format,
31397c20b4a3SGerd Hoffmann     .dpy_mouse_set        = vnc_mouse_set,
31407c20b4a3SGerd Hoffmann     .dpy_cursor_define    = vnc_dpy_cursor_define,
31417c20b4a3SGerd Hoffmann };
31427c20b4a3SGerd Hoffmann 
314314f7143eSGerd Hoffmann void vnc_display_init(const char *id)
31443e230dd2SCorentin Chary {
31454db14629SGerd Hoffmann     VncDisplay *vs;
31464db14629SGerd Hoffmann 
31474db14629SGerd Hoffmann     if (vnc_display_find(id) != NULL) {
31484db14629SGerd Hoffmann         return;
31494db14629SGerd Hoffmann     }
31504db14629SGerd Hoffmann     vs = g_malloc0(sizeof(*vs));
31513e230dd2SCorentin Chary 
315214f7143eSGerd Hoffmann     vs->id = strdup(id);
3153d616ccc5SGerd Hoffmann     QTAILQ_INSERT_TAIL(&vnc_displays, vs, next);
31543e230dd2SCorentin Chary 
31553e230dd2SCorentin Chary     vs->lsock = -1;
31567536ee4bSTim Hardeck #ifdef CONFIG_VNC_WS
31577536ee4bSTim Hardeck     vs->lwebsock = -1;
31587536ee4bSTim Hardeck #endif
31593e230dd2SCorentin Chary 
31603e230dd2SCorentin Chary     QTAILQ_INIT(&vs->clients);
31613c9405a0SGerd Hoffmann     vs->expires = TIME_MAX;
31623e230dd2SCorentin Chary 
316340066175SGerd Hoffmann     if (keyboard_layout) {
316440066175SGerd Hoffmann         trace_vnc_key_map_init(keyboard_layout);
31653e230dd2SCorentin Chary         vs->kbd_layout = init_keyboard_layout(name2keysym, keyboard_layout);
316640066175SGerd Hoffmann     } else {
31673e230dd2SCorentin Chary         vs->kbd_layout = init_keyboard_layout(name2keysym, "en-us");
316840066175SGerd Hoffmann     }
31693e230dd2SCorentin Chary 
31703e230dd2SCorentin Chary     if (!vs->kbd_layout)
31713e230dd2SCorentin Chary         exit(1);
31723e230dd2SCorentin Chary 
3173bd023f95SCorentin Chary     qemu_mutex_init(&vs->mutex);
3174bd023f95SCorentin Chary     vnc_start_worker_thread();
3175bd023f95SCorentin Chary 
317621ef45d7SGerd Hoffmann     vs->dcl.ops = &dcl_ops;
31775209089fSGerd Hoffmann     register_displaychangelistener(&vs->dcl);
31783e230dd2SCorentin Chary }
31793e230dd2SCorentin Chary 
31803e230dd2SCorentin Chary 
318114f7143eSGerd Hoffmann static void vnc_display_close(VncDisplay *vs)
31823e230dd2SCorentin Chary {
31833e230dd2SCorentin Chary     if (!vs)
31843e230dd2SCorentin Chary         return;
31857267c094SAnthony Liguori     g_free(vs->display);
31863e230dd2SCorentin Chary     vs->display = NULL;
31873e230dd2SCorentin Chary     if (vs->lsock != -1) {
31883e230dd2SCorentin Chary         qemu_set_fd_handler2(vs->lsock, NULL, NULL, NULL, NULL);
31893e230dd2SCorentin Chary         close(vs->lsock);
31903e230dd2SCorentin Chary         vs->lsock = -1;
31913e230dd2SCorentin Chary     }
31927536ee4bSTim Hardeck #ifdef CONFIG_VNC_WS
31937536ee4bSTim Hardeck     g_free(vs->ws_display);
31947536ee4bSTim Hardeck     vs->ws_display = NULL;
31957536ee4bSTim Hardeck     if (vs->lwebsock != -1) {
31967536ee4bSTim Hardeck         qemu_set_fd_handler2(vs->lwebsock, NULL, NULL, NULL, NULL);
31977536ee4bSTim Hardeck         close(vs->lwebsock);
31987536ee4bSTim Hardeck         vs->lwebsock = -1;
31997536ee4bSTim Hardeck     }
32007536ee4bSTim Hardeck #endif /* CONFIG_VNC_WS */
32013e230dd2SCorentin Chary     vs->auth = VNC_AUTH_INVALID;
32023e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS
32033e230dd2SCorentin Chary     vs->subauth = VNC_AUTH_INVALID;
32043e230dd2SCorentin Chary     vs->tls.x509verify = 0;
32053e230dd2SCorentin Chary #endif
32063e230dd2SCorentin Chary }
32073e230dd2SCorentin Chary 
320814f7143eSGerd Hoffmann int vnc_display_password(const char *id, const char *password)
32091cd20f8bSAnthony Liguori {
321014f7143eSGerd Hoffmann     VncDisplay *vs = vnc_display_find(id);
32111cd20f8bSAnthony Liguori 
32121cd20f8bSAnthony Liguori     if (!vs) {
3213a6aa9d3eSLuiz Capitulino         return -EINVAL;
32141cd20f8bSAnthony Liguori     }
3215cf864569SGerd Hoffmann     if (vs->auth == VNC_AUTH_NONE) {
3216cf864569SGerd Hoffmann         error_printf_unless_qmp("If you want use passwords please enable "
3217cf864569SGerd Hoffmann                                 "password auth using '-vnc ${dpy},password'.");
3218cf864569SGerd Hoffmann         return -EINVAL;
32191cd20f8bSAnthony Liguori     }
32201cd20f8bSAnthony Liguori 
32217267c094SAnthony Liguori     g_free(vs->password);
32227267c094SAnthony Liguori     vs->password = g_strdup(password);
3223a6aa9d3eSLuiz Capitulino 
3224a6aa9d3eSLuiz Capitulino     return 0;
32253e230dd2SCorentin Chary }
32263e230dd2SCorentin Chary 
322714f7143eSGerd Hoffmann int vnc_display_pw_expire(const char *id, time_t expires)
32283c9405a0SGerd Hoffmann {
322914f7143eSGerd Hoffmann     VncDisplay *vs = vnc_display_find(id);
32303c9405a0SGerd Hoffmann 
32311643f2b2SGerd Hoffmann     if (!vs) {
32321643f2b2SGerd Hoffmann         return -EINVAL;
32331643f2b2SGerd Hoffmann     }
32341643f2b2SGerd Hoffmann 
32353c9405a0SGerd Hoffmann     vs->expires = expires;
32363c9405a0SGerd Hoffmann     return 0;
32373c9405a0SGerd Hoffmann }
32383c9405a0SGerd Hoffmann 
323914f7143eSGerd Hoffmann char *vnc_display_local_addr(const char *id)
32403e230dd2SCorentin Chary {
324114f7143eSGerd Hoffmann     VncDisplay *vs = vnc_display_find(id);
32423e230dd2SCorentin Chary 
32433e230dd2SCorentin Chary     return vnc_socket_local_addr("%s:%s", vs->lsock);
32443e230dd2SCorentin Chary }
32453e230dd2SCorentin Chary 
32464db14629SGerd Hoffmann static QemuOptsList qemu_vnc_opts = {
32474db14629SGerd Hoffmann     .name = "vnc",
32484db14629SGerd Hoffmann     .head = QTAILQ_HEAD_INITIALIZER(qemu_vnc_opts.head),
32494db14629SGerd Hoffmann     .implied_opt_name = "vnc",
32504db14629SGerd Hoffmann     .desc = {
32514db14629SGerd Hoffmann         {
32524db14629SGerd Hoffmann             .name = "vnc",
32534db14629SGerd Hoffmann             .type = QEMU_OPT_STRING,
32544db14629SGerd Hoffmann         },{
32554db14629SGerd Hoffmann             .name = "websocket",
32564db14629SGerd Hoffmann             .type = QEMU_OPT_STRING,
32574db14629SGerd Hoffmann         },{
32584db14629SGerd Hoffmann             .name = "x509",
32594db14629SGerd Hoffmann             .type = QEMU_OPT_STRING,
32604db14629SGerd Hoffmann         },{
32614db14629SGerd Hoffmann             .name = "share",
32624db14629SGerd Hoffmann             .type = QEMU_OPT_STRING,
32634db14629SGerd Hoffmann         },{
32641d0d59feSGerd Hoffmann             .name = "display",
32651d0d59feSGerd Hoffmann             .type = QEMU_OPT_STRING,
32661d0d59feSGerd Hoffmann         },{
32671d0d59feSGerd Hoffmann             .name = "head",
32681d0d59feSGerd Hoffmann             .type = QEMU_OPT_NUMBER,
32691d0d59feSGerd Hoffmann         },{
3270e5f34cddSGerd Hoffmann             .name = "connections",
3271e5f34cddSGerd Hoffmann             .type = QEMU_OPT_NUMBER,
3272e5f34cddSGerd Hoffmann         },{
32734db14629SGerd Hoffmann             .name = "password",
32744db14629SGerd Hoffmann             .type = QEMU_OPT_BOOL,
32754db14629SGerd Hoffmann         },{
32764db14629SGerd Hoffmann             .name = "reverse",
32774db14629SGerd Hoffmann             .type = QEMU_OPT_BOOL,
32784db14629SGerd Hoffmann         },{
32794db14629SGerd Hoffmann             .name = "lock-key-sync",
32804db14629SGerd Hoffmann             .type = QEMU_OPT_BOOL,
32814db14629SGerd Hoffmann         },{
32824db14629SGerd Hoffmann             .name = "sasl",
32834db14629SGerd Hoffmann             .type = QEMU_OPT_BOOL,
32844db14629SGerd Hoffmann         },{
32854db14629SGerd Hoffmann             .name = "tls",
32864db14629SGerd Hoffmann             .type = QEMU_OPT_BOOL,
32874db14629SGerd Hoffmann         },{
32884db14629SGerd Hoffmann             .name = "x509verify",
32894db14629SGerd Hoffmann             .type = QEMU_OPT_BOOL,
32904db14629SGerd Hoffmann         },{
32914db14629SGerd Hoffmann             .name = "acl",
32924db14629SGerd Hoffmann             .type = QEMU_OPT_BOOL,
32934db14629SGerd Hoffmann         },{
32944db14629SGerd Hoffmann             .name = "lossy",
32954db14629SGerd Hoffmann             .type = QEMU_OPT_BOOL,
32964db14629SGerd Hoffmann         },{
32974db14629SGerd Hoffmann             .name = "non-adaptive",
32984db14629SGerd Hoffmann             .type = QEMU_OPT_BOOL,
32994db14629SGerd Hoffmann         },
33004db14629SGerd Hoffmann         { /* end of list */ }
33014db14629SGerd Hoffmann     },
33024db14629SGerd Hoffmann };
33034db14629SGerd Hoffmann 
33044db14629SGerd Hoffmann void vnc_display_open(const char *id, Error **errp)
33053e230dd2SCorentin Chary {
330614f7143eSGerd Hoffmann     VncDisplay *vs = vnc_display_find(id);
33074db14629SGerd Hoffmann     QemuOpts *opts = qemu_opts_find(&qemu_vnc_opts, id);
33081d0d59feSGerd Hoffmann     const char *display, *websocket, *share, *device_id;
33091d0d59feSGerd Hoffmann     QemuConsole *con;
33103e230dd2SCorentin Chary     int password = 0;
33113e230dd2SCorentin Chary     int reverse = 0;
33123e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS
33133e230dd2SCorentin Chary     int tls = 0, x509 = 0;
33144db14629SGerd Hoffmann     const char *path;
33153e230dd2SCorentin Chary #endif
33163e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL
33173e230dd2SCorentin Chary     int sasl = 0;
33183e230dd2SCorentin Chary     int saslErr;
33193e230dd2SCorentin Chary #endif
33202ded6ad7SBlue Swirl #if defined(CONFIG_VNC_TLS) || defined(CONFIG_VNC_SASL)
33213e230dd2SCorentin Chary     int acl = 0;
33222ded6ad7SBlue Swirl #endif
33233e230dd2SCorentin Chary     int lock_key_sync = 1;
33243e230dd2SCorentin Chary 
3325d616ccc5SGerd Hoffmann     if (!vs) {
33262d55f0e8SPaolo Bonzini         error_setg(errp, "VNC display not active");
33272d55f0e8SPaolo Bonzini         return;
33282d55f0e8SPaolo Bonzini     }
332914f7143eSGerd Hoffmann     vnc_display_close(vs);
33304db14629SGerd Hoffmann 
33314db14629SGerd Hoffmann     if (!opts) {
33322d55f0e8SPaolo Bonzini         return;
33334db14629SGerd Hoffmann     }
33344db14629SGerd Hoffmann     display = qemu_opt_get(opts, "vnc");
33354db14629SGerd Hoffmann     if (!display || strcmp(display, "none") == 0) {
33364db14629SGerd Hoffmann         return;
33374db14629SGerd Hoffmann     }
33381ce52c78SPaolo Bonzini     vs->display = g_strdup(display);
33393e230dd2SCorentin Chary 
33404db14629SGerd Hoffmann     password = qemu_opt_get_bool(opts, "password", false);
33414db14629SGerd Hoffmann     if (password && fips_get_state()) {
33422d55f0e8SPaolo Bonzini         error_setg(errp,
33430f66998fSPaul Moore                    "VNC password auth disabled due to FIPS mode, "
33440f66998fSPaul Moore                    "consider using the VeNCrypt or SASL authentication "
33452d55f0e8SPaolo Bonzini                    "methods as an alternative");
33461ce52c78SPaolo Bonzini         goto fail;
33470f66998fSPaul Moore     }
33484db14629SGerd Hoffmann 
33494db14629SGerd Hoffmann     reverse = qemu_opt_get_bool(opts, "reverse", false);
33504db14629SGerd Hoffmann     lock_key_sync = qemu_opt_get_bool(opts, "lock-key-sync", true);
33513e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL
33524db14629SGerd Hoffmann     sasl = qemu_opt_get_bool(opts, "sasl", false);
33533e230dd2SCorentin Chary #endif
33543e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS
33554db14629SGerd Hoffmann     tls  = qemu_opt_get_bool(opts, "tls", false);
33564db14629SGerd Hoffmann     path = qemu_opt_get(opts, "x509");
33574db14629SGerd Hoffmann     if (path) {
33584db14629SGerd Hoffmann         x509 = 1;
33594db14629SGerd Hoffmann         vs->tls.x509verify = qemu_opt_get_bool(opts, "x509verify", false);
33603e230dd2SCorentin Chary         if (vnc_tls_set_x509_creds_dir(vs, path) < 0) {
33614db14629SGerd Hoffmann             error_setg(errp, "Failed to find x509 certificates/keys in %s",
33624db14629SGerd Hoffmann                        path);
33631ce52c78SPaolo Bonzini             goto fail;
33643e230dd2SCorentin Chary         }
33653e230dd2SCorentin Chary     }
33663e230dd2SCorentin Chary #endif
33672ded6ad7SBlue Swirl #if defined(CONFIG_VNC_TLS) || defined(CONFIG_VNC_SASL)
33684db14629SGerd Hoffmann     acl = qemu_opt_get_bool(opts, "acl", false);
33692ded6ad7SBlue Swirl #endif
33704db14629SGerd Hoffmann 
33714db14629SGerd Hoffmann     share = qemu_opt_get(opts, "share");
33724db14629SGerd Hoffmann     if (share) {
33734db14629SGerd Hoffmann         if (strcmp(share, "ignore") == 0) {
33748cf36489SGerd Hoffmann             vs->share_policy = VNC_SHARE_POLICY_IGNORE;
33754db14629SGerd Hoffmann         } else if (strcmp(share, "allow-exclusive") == 0) {
33768cf36489SGerd Hoffmann             vs->share_policy = VNC_SHARE_POLICY_ALLOW_EXCLUSIVE;
33774db14629SGerd Hoffmann         } else if (strcmp(share, "force-shared") == 0) {
33788cf36489SGerd Hoffmann             vs->share_policy = VNC_SHARE_POLICY_FORCE_SHARED;
33798cf36489SGerd Hoffmann         } else {
33802d55f0e8SPaolo Bonzini             error_setg(errp, "unknown vnc share= option");
33811ce52c78SPaolo Bonzini             goto fail;
33828cf36489SGerd Hoffmann         }
33834db14629SGerd Hoffmann     } else {
33844db14629SGerd Hoffmann         vs->share_policy = VNC_SHARE_POLICY_ALLOW_EXCLUSIVE;
33853e230dd2SCorentin Chary     }
3386e5f34cddSGerd Hoffmann     vs->connections_limit = qemu_opt_get_number(opts, "connections", 32);
33873e230dd2SCorentin Chary 
33884db14629SGerd Hoffmann  #ifdef CONFIG_VNC_WS
33894db14629SGerd Hoffmann     websocket = qemu_opt_get(opts, "websocket");
33904db14629SGerd Hoffmann     if (websocket) {
33914db14629SGerd Hoffmann         /* extract the host specification from display */
33924db14629SGerd Hoffmann         char  *host = NULL, *host_end = NULL;
33934db14629SGerd Hoffmann         vs->websocket = 1;
33944db14629SGerd Hoffmann 
33954db14629SGerd Hoffmann         /* ipv6 hosts have colons */
33964db14629SGerd Hoffmann         host_end = strrchr(display, ':');
33974db14629SGerd Hoffmann         if (host_end) {
33984db14629SGerd Hoffmann             host = g_strndup(display, host_end - display + 1);
33994db14629SGerd Hoffmann         } else {
34004db14629SGerd Hoffmann             host = g_strdup(":");
34014db14629SGerd Hoffmann         }
34024db14629SGerd Hoffmann         vs->ws_display = g_strconcat(host, websocket, NULL);
34034db14629SGerd Hoffmann         g_free(host);
34044db14629SGerd Hoffmann     }
34054db14629SGerd Hoffmann #endif /* CONFIG_VNC_WS */
34064db14629SGerd Hoffmann 
34074db14629SGerd Hoffmann #ifdef CONFIG_VNC_JPEG
34084db14629SGerd Hoffmann     vs->lossy = qemu_opt_get_bool(opts, "lossy", false);
34094db14629SGerd Hoffmann #endif
34104db14629SGerd Hoffmann     vs->non_adaptive = qemu_opt_get_bool(opts, "non-adaptive", false);
3411e22492d3SPeter Lieven     /* adaptive updates are only used with tight encoding and
3412e22492d3SPeter Lieven      * if lossy updates are enabled so we can disable all the
3413e22492d3SPeter Lieven      * calculations otherwise */
3414e22492d3SPeter Lieven     if (!vs->lossy) {
3415e22492d3SPeter Lieven         vs->non_adaptive = true;
3416e22492d3SPeter Lieven     }
3417e22492d3SPeter Lieven 
34183e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS
34193e230dd2SCorentin Chary     if (acl && x509 && vs->tls.x509verify) {
3420c8496408SGerd Hoffmann         char *aclname;
3421c8496408SGerd Hoffmann 
3422c8496408SGerd Hoffmann         if (strcmp(vs->id, "default") == 0) {
3423c8496408SGerd Hoffmann             aclname = g_strdup("vnc.x509dname");
3424c8496408SGerd Hoffmann         } else {
3425c8496408SGerd Hoffmann             aclname = g_strdup_printf("vnc.%s.x509dname", vs->id);
3426c8496408SGerd Hoffmann         }
3427c8496408SGerd Hoffmann         vs->tls.acl = qemu_acl_init(aclname);
3428c8496408SGerd Hoffmann         if (!vs->tls.acl) {
34293e230dd2SCorentin Chary             fprintf(stderr, "Failed to create x509 dname ACL\n");
34303e230dd2SCorentin Chary             exit(1);
34313e230dd2SCorentin Chary         }
3432c8496408SGerd Hoffmann         g_free(aclname);
34333e230dd2SCorentin Chary     }
34343e230dd2SCorentin Chary #endif
34353e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL
34363e230dd2SCorentin Chary     if (acl && sasl) {
3437c8496408SGerd Hoffmann         char *aclname;
3438c8496408SGerd Hoffmann 
3439c8496408SGerd Hoffmann         if (strcmp(vs->id, "default") == 0) {
3440c8496408SGerd Hoffmann             aclname = g_strdup("vnc.username");
3441c8496408SGerd Hoffmann         } else {
3442c8496408SGerd Hoffmann             aclname = g_strdup_printf("vnc.%s.username", vs->id);
3443c8496408SGerd Hoffmann         }
3444c8496408SGerd Hoffmann         vs->sasl.acl = qemu_acl_init(aclname);
3445c8496408SGerd Hoffmann         if (!vs->sasl.acl) {
34463e230dd2SCorentin Chary             fprintf(stderr, "Failed to create username ACL\n");
34473e230dd2SCorentin Chary             exit(1);
34483e230dd2SCorentin Chary         }
3449c8496408SGerd Hoffmann         g_free(aclname);
34503e230dd2SCorentin Chary     }
34513e230dd2SCorentin Chary #endif
34523e230dd2SCorentin Chary 
34533e230dd2SCorentin Chary     /*
34543e230dd2SCorentin Chary      * Combinations we support here:
34553e230dd2SCorentin Chary      *
34563e230dd2SCorentin Chary      *  - no-auth                (clear text, no auth)
34573e230dd2SCorentin Chary      *  - password               (clear text, weak auth)
34583e230dd2SCorentin Chary      *  - sasl                   (encrypt, good auth *IF* using Kerberos via GSSAPI)
34593e230dd2SCorentin Chary      *  - tls                    (encrypt, weak anonymous creds, no auth)
34603e230dd2SCorentin Chary      *  - tls + password         (encrypt, weak anonymous creds, weak auth)
34613e230dd2SCorentin Chary      *  - tls + sasl             (encrypt, weak anonymous creds, good auth)
34623e230dd2SCorentin Chary      *  - tls + x509             (encrypt, good x509 creds, no auth)
34633e230dd2SCorentin Chary      *  - tls + x509 + password  (encrypt, good x509 creds, weak auth)
34643e230dd2SCorentin Chary      *  - tls + x509 + sasl      (encrypt, good x509 creds, good auth)
34653e230dd2SCorentin Chary      *
34663e230dd2SCorentin Chary      * NB1. TLS is a stackable auth scheme.
34673e230dd2SCorentin Chary      * NB2. the x509 schemes have option to validate a client cert dname
34683e230dd2SCorentin Chary      */
34693e230dd2SCorentin Chary     if (password) {
34703e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS
34713e230dd2SCorentin Chary         if (tls) {
34723e230dd2SCorentin Chary             vs->auth = VNC_AUTH_VENCRYPT;
34733e230dd2SCorentin Chary             if (x509) {
34743e230dd2SCorentin Chary                 VNC_DEBUG("Initializing VNC server with x509 password auth\n");
34753e230dd2SCorentin Chary                 vs->subauth = VNC_AUTH_VENCRYPT_X509VNC;
34763e230dd2SCorentin Chary             } else {
34773e230dd2SCorentin Chary                 VNC_DEBUG("Initializing VNC server with TLS password auth\n");
34783e230dd2SCorentin Chary                 vs->subauth = VNC_AUTH_VENCRYPT_TLSVNC;
34793e230dd2SCorentin Chary             }
34803e230dd2SCorentin Chary         } else {
34813e230dd2SCorentin Chary #endif /* CONFIG_VNC_TLS */
34823e230dd2SCorentin Chary             VNC_DEBUG("Initializing VNC server with password auth\n");
34833e230dd2SCorentin Chary             vs->auth = VNC_AUTH_VNC;
34843e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS
34853e230dd2SCorentin Chary             vs->subauth = VNC_AUTH_INVALID;
34863e230dd2SCorentin Chary         }
34873e230dd2SCorentin Chary #endif /* CONFIG_VNC_TLS */
34883e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL
34893e230dd2SCorentin Chary     } else if (sasl) {
34903e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS
34913e230dd2SCorentin Chary         if (tls) {
34923e230dd2SCorentin Chary             vs->auth = VNC_AUTH_VENCRYPT;
34933e230dd2SCorentin Chary             if (x509) {
34943e230dd2SCorentin Chary                 VNC_DEBUG("Initializing VNC server with x509 SASL auth\n");
34953e230dd2SCorentin Chary                 vs->subauth = VNC_AUTH_VENCRYPT_X509SASL;
34963e230dd2SCorentin Chary             } else {
34973e230dd2SCorentin Chary                 VNC_DEBUG("Initializing VNC server with TLS SASL auth\n");
34983e230dd2SCorentin Chary                 vs->subauth = VNC_AUTH_VENCRYPT_TLSSASL;
34993e230dd2SCorentin Chary             }
35003e230dd2SCorentin Chary         } else {
35013e230dd2SCorentin Chary #endif /* CONFIG_VNC_TLS */
35023e230dd2SCorentin Chary             VNC_DEBUG("Initializing VNC server with SASL auth\n");
35033e230dd2SCorentin Chary             vs->auth = VNC_AUTH_SASL;
35043e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS
35053e230dd2SCorentin Chary             vs->subauth = VNC_AUTH_INVALID;
35063e230dd2SCorentin Chary         }
35073e230dd2SCorentin Chary #endif /* CONFIG_VNC_TLS */
35083e230dd2SCorentin Chary #endif /* CONFIG_VNC_SASL */
35093e230dd2SCorentin Chary     } else {
35103e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS
35113e230dd2SCorentin Chary         if (tls) {
35123e230dd2SCorentin Chary             vs->auth = VNC_AUTH_VENCRYPT;
35133e230dd2SCorentin Chary             if (x509) {
35143e230dd2SCorentin Chary                 VNC_DEBUG("Initializing VNC server with x509 no auth\n");
35153e230dd2SCorentin Chary                 vs->subauth = VNC_AUTH_VENCRYPT_X509NONE;
35163e230dd2SCorentin Chary             } else {
35173e230dd2SCorentin Chary                 VNC_DEBUG("Initializing VNC server with TLS no auth\n");
35183e230dd2SCorentin Chary                 vs->subauth = VNC_AUTH_VENCRYPT_TLSNONE;
35193e230dd2SCorentin Chary             }
35203e230dd2SCorentin Chary         } else {
35213e230dd2SCorentin Chary #endif
35223e230dd2SCorentin Chary             VNC_DEBUG("Initializing VNC server with no auth\n");
35233e230dd2SCorentin Chary             vs->auth = VNC_AUTH_NONE;
35243e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS
35253e230dd2SCorentin Chary             vs->subauth = VNC_AUTH_INVALID;
35263e230dd2SCorentin Chary         }
35273e230dd2SCorentin Chary #endif
35283e230dd2SCorentin Chary     }
35293e230dd2SCorentin Chary 
35303e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL
35313e230dd2SCorentin Chary     if ((saslErr = sasl_server_init(NULL, "qemu")) != SASL_OK) {
35322d55f0e8SPaolo Bonzini         error_setg(errp, "Failed to initialize SASL auth: %s",
35333e230dd2SCorentin Chary                    sasl_errstring(saslErr, NULL, NULL));
35341ce52c78SPaolo Bonzini         goto fail;
35353e230dd2SCorentin Chary     }
35363e230dd2SCorentin Chary #endif
35373e230dd2SCorentin Chary     vs->lock_key_sync = lock_key_sync;
35383e230dd2SCorentin Chary 
35391d0d59feSGerd Hoffmann     device_id = qemu_opt_get(opts, "display");
35401d0d59feSGerd Hoffmann     if (device_id) {
35411d0d59feSGerd Hoffmann         DeviceState *dev;
35421d0d59feSGerd Hoffmann         int head = qemu_opt_get_number(opts, "head", 0);
35431d0d59feSGerd Hoffmann 
35441d0d59feSGerd Hoffmann         dev = qdev_find_recursive(sysbus_get_default(), device_id);
35451d0d59feSGerd Hoffmann         if (dev == NULL) {
35461d0d59feSGerd Hoffmann             error_set(errp, QERR_DEVICE_NOT_FOUND, device_id);
35471d0d59feSGerd Hoffmann             goto fail;
35481d0d59feSGerd Hoffmann         }
35491d0d59feSGerd Hoffmann 
35501d0d59feSGerd Hoffmann         con = qemu_console_lookup_by_device(dev, head);
35511d0d59feSGerd Hoffmann         if (con == NULL) {
35521d0d59feSGerd Hoffmann             error_setg(errp, "Device %s is not bound to a QemuConsole",
35531d0d59feSGerd Hoffmann                        device_id);
35541d0d59feSGerd Hoffmann             goto fail;
35551d0d59feSGerd Hoffmann         }
35561d0d59feSGerd Hoffmann     } else {
35571d0d59feSGerd Hoffmann         con = NULL;
35581d0d59feSGerd Hoffmann     }
35591d0d59feSGerd Hoffmann 
35601d0d59feSGerd Hoffmann     if (con != vs->dcl.con) {
35611d0d59feSGerd Hoffmann         unregister_displaychangelistener(&vs->dcl);
35621d0d59feSGerd Hoffmann         vs->dcl.con = con;
35631d0d59feSGerd Hoffmann         register_displaychangelistener(&vs->dcl);
35641d0d59feSGerd Hoffmann     }
35651d0d59feSGerd Hoffmann 
35663e230dd2SCorentin Chary     if (reverse) {
35673e230dd2SCorentin Chary         /* connect to viewer */
3568007fcd3eSPaolo Bonzini         int csock;
35693e230dd2SCorentin Chary         vs->lsock = -1;
35707536ee4bSTim Hardeck #ifdef CONFIG_VNC_WS
35717536ee4bSTim Hardeck         vs->lwebsock = -1;
35727536ee4bSTim Hardeck #endif
3573007fcd3eSPaolo Bonzini         if (strncmp(display, "unix:", 5) == 0) {
35742d55f0e8SPaolo Bonzini             csock = unix_connect(display+5, errp);
3575007fcd3eSPaolo Bonzini         } else {
35762d55f0e8SPaolo Bonzini             csock = inet_connect(display, errp);
35773e230dd2SCorentin Chary         }
3578007fcd3eSPaolo Bonzini         if (csock < 0) {
3579007fcd3eSPaolo Bonzini             goto fail;
3580007fcd3eSPaolo Bonzini         }
35812c8cf549SMichael Tokarev         vnc_connect(vs, csock, false, false);
35823e230dd2SCorentin Chary     } else {
35833e230dd2SCorentin Chary         /* listen for connects */
35843e230dd2SCorentin Chary         char *dpy;
35857267c094SAnthony Liguori         dpy = g_malloc(256);
35863e230dd2SCorentin Chary         if (strncmp(display, "unix:", 5) == 0) {
35873e230dd2SCorentin Chary             pstrcpy(dpy, 256, "unix:");
35882d55f0e8SPaolo Bonzini             vs->lsock = unix_listen(display+5, dpy+5, 256-5, errp);
35893e230dd2SCorentin Chary         } else {
3590029409e5SAmos Kong             vs->lsock = inet_listen(display, dpy, 256,
35912d55f0e8SPaolo Bonzini                                     SOCK_STREAM, 5900, errp);
3592c1c1619cSPaolo Bonzini             if (vs->lsock < 0) {
3593ae878b17SStefan Weil                 g_free(dpy);
35941ce52c78SPaolo Bonzini                 goto fail;
35951ce52c78SPaolo Bonzini             }
35967536ee4bSTim Hardeck #ifdef CONFIG_VNC_WS
35977536ee4bSTim Hardeck             if (vs->websocket) {
35987536ee4bSTim Hardeck                 if (vs->ws_display) {
35997536ee4bSTim Hardeck                     vs->lwebsock = inet_listen(vs->ws_display, NULL, 256,
36007536ee4bSTim Hardeck                         SOCK_STREAM, 0, errp);
36017536ee4bSTim Hardeck                 } else {
36027536ee4bSTim Hardeck                     vs->lwebsock = inet_listen(vs->display, NULL, 256,
36037536ee4bSTim Hardeck                         SOCK_STREAM, 5700, errp);
36047536ee4bSTim Hardeck                 }
36057536ee4bSTim Hardeck 
36067536ee4bSTim Hardeck                 if (vs->lwebsock < 0) {
36077536ee4bSTim Hardeck                     if (vs->lsock) {
36087536ee4bSTim Hardeck                         close(vs->lsock);
36097536ee4bSTim Hardeck                         vs->lsock = -1;
36107536ee4bSTim Hardeck                     }
36117536ee4bSTim Hardeck                     g_free(dpy);
36127536ee4bSTim Hardeck                     goto fail;
36137536ee4bSTim Hardeck                 }
36147536ee4bSTim Hardeck             }
36157536ee4bSTim Hardeck #endif /* CONFIG_VNC_WS */
36167536ee4bSTim Hardeck         }
3617ae878b17SStefan Weil         g_free(vs->display);
36183e230dd2SCorentin Chary         vs->display = dpy;
36197536ee4bSTim Hardeck         qemu_set_fd_handler2(vs->lsock, NULL,
36207536ee4bSTim Hardeck                 vnc_listen_regular_read, NULL, vs);
36217536ee4bSTim Hardeck #ifdef CONFIG_VNC_WS
36227536ee4bSTim Hardeck         if (vs->websocket) {
36237536ee4bSTim Hardeck             qemu_set_fd_handler2(vs->lwebsock, NULL,
36247536ee4bSTim Hardeck                     vnc_listen_websocket_read, NULL, vs);
36257536ee4bSTim Hardeck         }
36267536ee4bSTim Hardeck #endif /* CONFIG_VNC_WS */
36273e230dd2SCorentin Chary     }
36282d55f0e8SPaolo Bonzini     return;
36291ce52c78SPaolo Bonzini 
36301ce52c78SPaolo Bonzini fail:
36311ce52c78SPaolo Bonzini     g_free(vs->display);
36321ce52c78SPaolo Bonzini     vs->display = NULL;
36337536ee4bSTim Hardeck #ifdef CONFIG_VNC_WS
36347536ee4bSTim Hardeck     g_free(vs->ws_display);
36357536ee4bSTim Hardeck     vs->ws_display = NULL;
36367536ee4bSTim Hardeck #endif /* CONFIG_VNC_WS */
36373e230dd2SCorentin Chary }
363813661089SDaniel P. Berrange 
363914f7143eSGerd Hoffmann void vnc_display_add_client(const char *id, int csock, bool skipauth)
364013661089SDaniel P. Berrange {
364114f7143eSGerd Hoffmann     VncDisplay *vs = vnc_display_find(id);
364213661089SDaniel P. Berrange 
3643d616ccc5SGerd Hoffmann     if (!vs) {
3644d616ccc5SGerd Hoffmann         return;
3645d616ccc5SGerd Hoffmann     }
36462c8cf549SMichael Tokarev     vnc_connect(vs, csock, skipauth, false);
364713661089SDaniel P. Berrange }
36484db14629SGerd Hoffmann 
36494db14629SGerd Hoffmann QemuOpts *vnc_parse_func(const char *str)
36504db14629SGerd Hoffmann {
36514db14629SGerd Hoffmann     return qemu_opts_parse(qemu_find_opts("vnc"), str, 1);
36524db14629SGerd Hoffmann }
36534db14629SGerd Hoffmann 
36544db14629SGerd Hoffmann int vnc_init_func(QemuOpts *opts, void *opaque)
36554db14629SGerd Hoffmann {
36564db14629SGerd Hoffmann     Error *local_err = NULL;
36574db14629SGerd Hoffmann     QemuOptsList *olist = qemu_find_opts("vnc");
36584db14629SGerd Hoffmann     char *id = (char *)qemu_opts_id(opts);
36594db14629SGerd Hoffmann 
36604db14629SGerd Hoffmann     if (!id) {
36614db14629SGerd Hoffmann         /* auto-assign id if not present */
36624db14629SGerd Hoffmann         int i = 2;
36634db14629SGerd Hoffmann         id = g_strdup("default");
36644db14629SGerd Hoffmann         while (qemu_opts_find(olist, id)) {
36654db14629SGerd Hoffmann             g_free(id);
36664db14629SGerd Hoffmann             id = g_strdup_printf("vnc%d", i++);
36674db14629SGerd Hoffmann         }
36684db14629SGerd Hoffmann         qemu_opts_set_id(opts, id);
36694db14629SGerd Hoffmann     }
36704db14629SGerd Hoffmann 
36714db14629SGerd Hoffmann     vnc_display_init(id);
36724db14629SGerd Hoffmann     vnc_display_open(id, &local_err);
36734db14629SGerd Hoffmann     if (local_err != NULL) {
36744db14629SGerd Hoffmann         error_report("Failed to start VNC server on `%s': %s",
36754db14629SGerd Hoffmann                      qemu_opt_get(opts, "display"),
36764db14629SGerd Hoffmann                      error_get_pretty(local_err));
36774db14629SGerd Hoffmann         error_free(local_err);
36784db14629SGerd Hoffmann         exit(1);
36794db14629SGerd Hoffmann     }
36804db14629SGerd Hoffmann     return 0;
36814db14629SGerd Hoffmann }
36824db14629SGerd Hoffmann 
36834db14629SGerd Hoffmann static void vnc_register_config(void)
36844db14629SGerd Hoffmann {
36854db14629SGerd Hoffmann     qemu_add_opts(&qemu_vnc_opts);
36864db14629SGerd Hoffmann }
36874db14629SGerd Hoffmann machine_init(vnc_register_config);
3688