xref: /openbmc/qemu/ui/vnc.c (revision 863d7c91050551def59116f4f3b39fab7f1568f7)
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"
299c17d615SPaolo Bonzini #include "sysemu/sysemu.h"
301de7afc9SPaolo Bonzini #include "qemu/sockets.h"
311de7afc9SPaolo Bonzini #include "qemu/timer.h"
321de7afc9SPaolo Bonzini #include "qemu/acl.h"
337b1b5d19SPaolo Bonzini #include "qapi/qmp/types.h"
342b54aa87SLuiz Capitulino #include "qmp-commands.h"
351de7afc9SPaolo Bonzini #include "qemu/osdep.h"
368d447d10SGerd Hoffmann #include "ui/input.h"
373e230dd2SCorentin Chary 
380f7b2864SGerd Hoffmann #define VNC_REFRESH_INTERVAL_BASE GUI_REFRESH_INTERVAL_DEFAULT
393e230dd2SCorentin Chary #define VNC_REFRESH_INTERVAL_INC  50
400f7b2864SGerd Hoffmann #define VNC_REFRESH_INTERVAL_MAX  GUI_REFRESH_INTERVAL_IDLE
41999342a0SCorentin Chary static const struct timeval VNC_REFRESH_STATS = { 0, 500000 };
42999342a0SCorentin Chary static const struct timeval VNC_REFRESH_LOSSY = { 2, 0 };
433e230dd2SCorentin Chary 
443e230dd2SCorentin Chary #include "vnc_keysym.h"
453e230dd2SCorentin Chary #include "d3des.h"
463e230dd2SCorentin Chary 
473e230dd2SCorentin Chary static VncDisplay *vnc_display; /* needed for info vnc */
483e230dd2SCorentin Chary 
493e230dd2SCorentin Chary static int vnc_cursor_define(VncState *vs);
507bc9318bSGerd Hoffmann static void vnc_release_modifiers(VncState *vs);
513e230dd2SCorentin Chary 
528cf36489SGerd Hoffmann static void vnc_set_share_mode(VncState *vs, VncShareMode mode)
538cf36489SGerd Hoffmann {
548cf36489SGerd Hoffmann #ifdef _VNC_DEBUG
558cf36489SGerd Hoffmann     static const char *mn[] = {
568cf36489SGerd Hoffmann         [0]                           = "undefined",
578cf36489SGerd Hoffmann         [VNC_SHARE_MODE_CONNECTING]   = "connecting",
588cf36489SGerd Hoffmann         [VNC_SHARE_MODE_SHARED]       = "shared",
598cf36489SGerd Hoffmann         [VNC_SHARE_MODE_EXCLUSIVE]    = "exclusive",
608cf36489SGerd Hoffmann         [VNC_SHARE_MODE_DISCONNECTED] = "disconnected",
618cf36489SGerd Hoffmann     };
628cf36489SGerd Hoffmann     fprintf(stderr, "%s/%d: %s -> %s\n", __func__,
638cf36489SGerd Hoffmann             vs->csock, mn[vs->share_mode], mn[mode]);
648cf36489SGerd Hoffmann #endif
658cf36489SGerd Hoffmann 
668cf36489SGerd Hoffmann     if (vs->share_mode == VNC_SHARE_MODE_EXCLUSIVE) {
678cf36489SGerd Hoffmann         vs->vd->num_exclusive--;
688cf36489SGerd Hoffmann     }
698cf36489SGerd Hoffmann     vs->share_mode = mode;
708cf36489SGerd Hoffmann     if (vs->share_mode == VNC_SHARE_MODE_EXCLUSIVE) {
718cf36489SGerd Hoffmann         vs->vd->num_exclusive++;
728cf36489SGerd Hoffmann     }
738cf36489SGerd Hoffmann }
748cf36489SGerd Hoffmann 
753e230dd2SCorentin Chary static char *addr_to_string(const char *format,
763e230dd2SCorentin Chary                             struct sockaddr_storage *sa,
773e230dd2SCorentin Chary                             socklen_t salen) {
783e230dd2SCorentin Chary     char *addr;
793e230dd2SCorentin Chary     char host[NI_MAXHOST];
803e230dd2SCorentin Chary     char serv[NI_MAXSERV];
813e230dd2SCorentin Chary     int err;
823e230dd2SCorentin Chary     size_t addrlen;
833e230dd2SCorentin Chary 
843e230dd2SCorentin Chary     if ((err = getnameinfo((struct sockaddr *)sa, salen,
853e230dd2SCorentin Chary                            host, sizeof(host),
863e230dd2SCorentin Chary                            serv, sizeof(serv),
873e230dd2SCorentin Chary                            NI_NUMERICHOST | NI_NUMERICSERV)) != 0) {
883e230dd2SCorentin Chary         VNC_DEBUG("Cannot resolve address %d: %s\n",
893e230dd2SCorentin Chary                   err, gai_strerror(err));
903e230dd2SCorentin Chary         return NULL;
913e230dd2SCorentin Chary     }
923e230dd2SCorentin Chary 
933e230dd2SCorentin Chary     /* Enough for the existing format + the 2 vars we're
943e230dd2SCorentin Chary      * substituting in. */
953e230dd2SCorentin Chary     addrlen = strlen(format) + strlen(host) + strlen(serv);
967267c094SAnthony Liguori     addr = g_malloc(addrlen + 1);
973e230dd2SCorentin Chary     snprintf(addr, addrlen, format, host, serv);
983e230dd2SCorentin Chary     addr[addrlen] = '\0';
993e230dd2SCorentin Chary 
1003e230dd2SCorentin Chary     return addr;
1013e230dd2SCorentin Chary }
1023e230dd2SCorentin Chary 
1033e230dd2SCorentin Chary 
1043e230dd2SCorentin Chary char *vnc_socket_local_addr(const char *format, int fd) {
1053e230dd2SCorentin Chary     struct sockaddr_storage sa;
1063e230dd2SCorentin Chary     socklen_t salen;
1073e230dd2SCorentin Chary 
1083e230dd2SCorentin Chary     salen = sizeof(sa);
1093e230dd2SCorentin Chary     if (getsockname(fd, (struct sockaddr*)&sa, &salen) < 0)
1103e230dd2SCorentin Chary         return NULL;
1113e230dd2SCorentin Chary 
1123e230dd2SCorentin Chary     return addr_to_string(format, &sa, salen);
1133e230dd2SCorentin Chary }
1143e230dd2SCorentin Chary 
1153e230dd2SCorentin Chary char *vnc_socket_remote_addr(const char *format, int fd) {
1163e230dd2SCorentin Chary     struct sockaddr_storage sa;
1173e230dd2SCorentin Chary     socklen_t salen;
1183e230dd2SCorentin Chary 
1193e230dd2SCorentin Chary     salen = sizeof(sa);
1203e230dd2SCorentin Chary     if (getpeername(fd, (struct sockaddr*)&sa, &salen) < 0)
1213e230dd2SCorentin Chary         return NULL;
1223e230dd2SCorentin Chary 
1233e230dd2SCorentin Chary     return addr_to_string(format, &sa, salen);
1243e230dd2SCorentin Chary }
1253e230dd2SCorentin Chary 
1263e230dd2SCorentin Chary static int put_addr_qdict(QDict *qdict, struct sockaddr_storage *sa,
1273e230dd2SCorentin Chary                           socklen_t salen)
1283e230dd2SCorentin Chary {
1293e230dd2SCorentin Chary     char host[NI_MAXHOST];
1303e230dd2SCorentin Chary     char serv[NI_MAXSERV];
1313e230dd2SCorentin Chary     int err;
1323e230dd2SCorentin Chary 
1333e230dd2SCorentin Chary     if ((err = getnameinfo((struct sockaddr *)sa, salen,
1343e230dd2SCorentin Chary                            host, sizeof(host),
1353e230dd2SCorentin Chary                            serv, sizeof(serv),
1363e230dd2SCorentin Chary                            NI_NUMERICHOST | NI_NUMERICSERV)) != 0) {
1373e230dd2SCorentin Chary         VNC_DEBUG("Cannot resolve address %d: %s\n",
1383e230dd2SCorentin Chary                   err, gai_strerror(err));
1393e230dd2SCorentin Chary         return -1;
1403e230dd2SCorentin Chary     }
1413e230dd2SCorentin Chary 
1423e230dd2SCorentin Chary     qdict_put(qdict, "host", qstring_from_str(host));
1433e230dd2SCorentin Chary     qdict_put(qdict, "service", qstring_from_str(serv));
1443e230dd2SCorentin Chary     qdict_put(qdict, "family",qstring_from_str(inet_strfamily(sa->ss_family)));
1453e230dd2SCorentin Chary 
1463e230dd2SCorentin Chary     return 0;
1473e230dd2SCorentin Chary }
1483e230dd2SCorentin Chary 
1493e230dd2SCorentin Chary static int vnc_server_addr_put(QDict *qdict, int fd)
1503e230dd2SCorentin Chary {
1513e230dd2SCorentin Chary     struct sockaddr_storage sa;
1523e230dd2SCorentin Chary     socklen_t salen;
1533e230dd2SCorentin Chary 
1543e230dd2SCorentin Chary     salen = sizeof(sa);
1553e230dd2SCorentin Chary     if (getsockname(fd, (struct sockaddr*)&sa, &salen) < 0) {
1563e230dd2SCorentin Chary         return -1;
1573e230dd2SCorentin Chary     }
1583e230dd2SCorentin Chary 
1593e230dd2SCorentin Chary     return put_addr_qdict(qdict, &sa, salen);
1603e230dd2SCorentin Chary }
1613e230dd2SCorentin Chary 
1623e230dd2SCorentin Chary static int vnc_qdict_remote_addr(QDict *qdict, int fd)
1633e230dd2SCorentin Chary {
1643e230dd2SCorentin Chary     struct sockaddr_storage sa;
1653e230dd2SCorentin Chary     socklen_t salen;
1663e230dd2SCorentin Chary 
1673e230dd2SCorentin Chary     salen = sizeof(sa);
1683e230dd2SCorentin Chary     if (getpeername(fd, (struct sockaddr*)&sa, &salen) < 0) {
1693e230dd2SCorentin Chary         return -1;
1703e230dd2SCorentin Chary     }
1713e230dd2SCorentin Chary 
1723e230dd2SCorentin Chary     return put_addr_qdict(qdict, &sa, salen);
1733e230dd2SCorentin Chary }
1743e230dd2SCorentin Chary 
1753e230dd2SCorentin Chary static const char *vnc_auth_name(VncDisplay *vd) {
1763e230dd2SCorentin Chary     switch (vd->auth) {
1773e230dd2SCorentin Chary     case VNC_AUTH_INVALID:
1783e230dd2SCorentin Chary         return "invalid";
1793e230dd2SCorentin Chary     case VNC_AUTH_NONE:
1803e230dd2SCorentin Chary         return "none";
1813e230dd2SCorentin Chary     case VNC_AUTH_VNC:
1823e230dd2SCorentin Chary         return "vnc";
1833e230dd2SCorentin Chary     case VNC_AUTH_RA2:
1843e230dd2SCorentin Chary         return "ra2";
1853e230dd2SCorentin Chary     case VNC_AUTH_RA2NE:
1863e230dd2SCorentin Chary         return "ra2ne";
1873e230dd2SCorentin Chary     case VNC_AUTH_TIGHT:
1883e230dd2SCorentin Chary         return "tight";
1893e230dd2SCorentin Chary     case VNC_AUTH_ULTRA:
1903e230dd2SCorentin Chary         return "ultra";
1913e230dd2SCorentin Chary     case VNC_AUTH_TLS:
1923e230dd2SCorentin Chary         return "tls";
1933e230dd2SCorentin Chary     case VNC_AUTH_VENCRYPT:
1943e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS
1953e230dd2SCorentin Chary         switch (vd->subauth) {
1963e230dd2SCorentin Chary         case VNC_AUTH_VENCRYPT_PLAIN:
1973e230dd2SCorentin Chary             return "vencrypt+plain";
1983e230dd2SCorentin Chary         case VNC_AUTH_VENCRYPT_TLSNONE:
1993e230dd2SCorentin Chary             return "vencrypt+tls+none";
2003e230dd2SCorentin Chary         case VNC_AUTH_VENCRYPT_TLSVNC:
2013e230dd2SCorentin Chary             return "vencrypt+tls+vnc";
2023e230dd2SCorentin Chary         case VNC_AUTH_VENCRYPT_TLSPLAIN:
2033e230dd2SCorentin Chary             return "vencrypt+tls+plain";
2043e230dd2SCorentin Chary         case VNC_AUTH_VENCRYPT_X509NONE:
2053e230dd2SCorentin Chary             return "vencrypt+x509+none";
2063e230dd2SCorentin Chary         case VNC_AUTH_VENCRYPT_X509VNC:
2073e230dd2SCorentin Chary             return "vencrypt+x509+vnc";
2083e230dd2SCorentin Chary         case VNC_AUTH_VENCRYPT_X509PLAIN:
2093e230dd2SCorentin Chary             return "vencrypt+x509+plain";
2103e230dd2SCorentin Chary         case VNC_AUTH_VENCRYPT_TLSSASL:
2113e230dd2SCorentin Chary             return "vencrypt+tls+sasl";
2123e230dd2SCorentin Chary         case VNC_AUTH_VENCRYPT_X509SASL:
2133e230dd2SCorentin Chary             return "vencrypt+x509+sasl";
2143e230dd2SCorentin Chary         default:
2153e230dd2SCorentin Chary             return "vencrypt";
2163e230dd2SCorentin Chary         }
2173e230dd2SCorentin Chary #else
2183e230dd2SCorentin Chary         return "vencrypt";
2193e230dd2SCorentin Chary #endif
2203e230dd2SCorentin Chary     case VNC_AUTH_SASL:
2213e230dd2SCorentin Chary         return "sasl";
2223e230dd2SCorentin Chary     }
2233e230dd2SCorentin Chary     return "unknown";
2243e230dd2SCorentin Chary }
2253e230dd2SCorentin Chary 
2263e230dd2SCorentin Chary static int vnc_server_info_put(QDict *qdict)
2273e230dd2SCorentin Chary {
2283e230dd2SCorentin Chary     if (vnc_server_addr_put(qdict, vnc_display->lsock) < 0) {
2293e230dd2SCorentin Chary         return -1;
2303e230dd2SCorentin Chary     }
2313e230dd2SCorentin Chary 
2323e230dd2SCorentin Chary     qdict_put(qdict, "auth", qstring_from_str(vnc_auth_name(vnc_display)));
2333e230dd2SCorentin Chary     return 0;
2343e230dd2SCorentin Chary }
2353e230dd2SCorentin Chary 
2363e230dd2SCorentin Chary static void vnc_client_cache_auth(VncState *client)
2373e230dd2SCorentin Chary {
2382ded6ad7SBlue Swirl #if defined(CONFIG_VNC_TLS) || defined(CONFIG_VNC_SASL)
2393e230dd2SCorentin Chary     QDict *qdict;
2402ded6ad7SBlue Swirl #endif
2413e230dd2SCorentin Chary 
2423e230dd2SCorentin Chary     if (!client->info) {
2433e230dd2SCorentin Chary         return;
2443e230dd2SCorentin Chary     }
2453e230dd2SCorentin Chary 
2462ded6ad7SBlue Swirl #if defined(CONFIG_VNC_TLS) || defined(CONFIG_VNC_SASL)
2473e230dd2SCorentin Chary     qdict = qobject_to_qdict(client->info);
2482ded6ad7SBlue Swirl #endif
2493e230dd2SCorentin Chary 
2503e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS
2513e230dd2SCorentin Chary     if (client->tls.session &&
2523e230dd2SCorentin Chary         client->tls.dname) {
2533e230dd2SCorentin Chary         qdict_put(qdict, "x509_dname", qstring_from_str(client->tls.dname));
2543e230dd2SCorentin Chary     }
2553e230dd2SCorentin Chary #endif
2563e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL
2573e230dd2SCorentin Chary     if (client->sasl.conn &&
2583e230dd2SCorentin Chary         client->sasl.username) {
2593e230dd2SCorentin Chary         qdict_put(qdict, "sasl_username",
2603e230dd2SCorentin Chary                   qstring_from_str(client->sasl.username));
2613e230dd2SCorentin Chary     }
2623e230dd2SCorentin Chary #endif
2633e230dd2SCorentin Chary }
2643e230dd2SCorentin Chary 
2653e230dd2SCorentin Chary static void vnc_client_cache_addr(VncState *client)
2663e230dd2SCorentin Chary {
2673e230dd2SCorentin Chary     QDict *qdict;
2683e230dd2SCorentin Chary 
2693e230dd2SCorentin Chary     qdict = qdict_new();
2703e230dd2SCorentin Chary     if (vnc_qdict_remote_addr(qdict, client->csock) < 0) {
2713e230dd2SCorentin Chary         QDECREF(qdict);
2723e230dd2SCorentin Chary         /* XXX: how to report the error? */
2733e230dd2SCorentin Chary         return;
2743e230dd2SCorentin Chary     }
2753e230dd2SCorentin Chary 
2763e230dd2SCorentin Chary     client->info = QOBJECT(qdict);
2773e230dd2SCorentin Chary }
2783e230dd2SCorentin Chary 
2793e230dd2SCorentin Chary static void vnc_qmp_event(VncState *vs, MonitorEvent event)
2803e230dd2SCorentin Chary {
2813e230dd2SCorentin Chary     QDict *server;
2823e230dd2SCorentin Chary     QObject *data;
2833e230dd2SCorentin Chary 
2843e230dd2SCorentin Chary     if (!vs->info) {
2853e230dd2SCorentin Chary         return;
2863e230dd2SCorentin Chary     }
2873e230dd2SCorentin Chary 
2883e230dd2SCorentin Chary     server = qdict_new();
2893e230dd2SCorentin Chary     if (vnc_server_info_put(server) < 0) {
2903e230dd2SCorentin Chary         QDECREF(server);
2913e230dd2SCorentin Chary         return;
2923e230dd2SCorentin Chary     }
2933e230dd2SCorentin Chary 
2943e230dd2SCorentin Chary     data = qobject_from_jsonf("{ 'client': %p, 'server': %p }",
2953e230dd2SCorentin Chary                               vs->info, QOBJECT(server));
2963e230dd2SCorentin Chary 
2973e230dd2SCorentin Chary     monitor_protocol_event(event, data);
2983e230dd2SCorentin Chary 
2993e230dd2SCorentin Chary     qobject_incref(vs->info);
3003e230dd2SCorentin Chary     qobject_decref(data);
3013e230dd2SCorentin Chary }
3023e230dd2SCorentin Chary 
3032b54aa87SLuiz Capitulino static VncClientInfo *qmp_query_vnc_client(const VncState *client)
3043e230dd2SCorentin Chary {
3052b54aa87SLuiz Capitulino     struct sockaddr_storage sa;
3062b54aa87SLuiz Capitulino     socklen_t salen = sizeof(sa);
3072b54aa87SLuiz Capitulino     char host[NI_MAXHOST];
3082b54aa87SLuiz Capitulino     char serv[NI_MAXSERV];
3092b54aa87SLuiz Capitulino     VncClientInfo *info;
3103e230dd2SCorentin Chary 
3112b54aa87SLuiz Capitulino     if (getpeername(client->csock, (struct sockaddr *)&sa, &salen) < 0) {
3122b54aa87SLuiz Capitulino         return NULL;
3132b54aa87SLuiz Capitulino     }
3142b54aa87SLuiz Capitulino 
3152b54aa87SLuiz Capitulino     if (getnameinfo((struct sockaddr *)&sa, salen,
3162b54aa87SLuiz Capitulino                     host, sizeof(host),
3172b54aa87SLuiz Capitulino                     serv, sizeof(serv),
3182b54aa87SLuiz Capitulino                     NI_NUMERICHOST | NI_NUMERICSERV) < 0) {
3192b54aa87SLuiz Capitulino         return NULL;
3202b54aa87SLuiz Capitulino     }
3212b54aa87SLuiz Capitulino 
3222b54aa87SLuiz Capitulino     info = g_malloc0(sizeof(*info));
3232b54aa87SLuiz Capitulino     info->host = g_strdup(host);
3242b54aa87SLuiz Capitulino     info->service = g_strdup(serv);
3252b54aa87SLuiz Capitulino     info->family = g_strdup(inet_strfamily(sa.ss_family));
3263e230dd2SCorentin Chary 
3273e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS
3282b54aa87SLuiz Capitulino     if (client->tls.session && client->tls.dname) {
3292b54aa87SLuiz Capitulino         info->has_x509_dname = true;
3302b54aa87SLuiz Capitulino         info->x509_dname = g_strdup(client->tls.dname);
3312b54aa87SLuiz Capitulino     }
3323e230dd2SCorentin Chary #endif
3333e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL
3342b54aa87SLuiz Capitulino     if (client->sasl.conn && client->sasl.username) {
3352b54aa87SLuiz Capitulino         info->has_sasl_username = true;
3362b54aa87SLuiz Capitulino         info->sasl_username = g_strdup(client->sasl.username);
3372b54aa87SLuiz Capitulino     }
3383e230dd2SCorentin Chary #endif
3392b54aa87SLuiz Capitulino 
3402b54aa87SLuiz Capitulino     return info;
3413e230dd2SCorentin Chary }
3423e230dd2SCorentin Chary 
3432b54aa87SLuiz Capitulino VncInfo *qmp_query_vnc(Error **errp)
3443e230dd2SCorentin Chary {
3452b54aa87SLuiz Capitulino     VncInfo *info = g_malloc0(sizeof(*info));
3463e230dd2SCorentin Chary 
3473e230dd2SCorentin Chary     if (vnc_display == NULL || vnc_display->display == NULL) {
3482b54aa87SLuiz Capitulino         info->enabled = false;
3493e230dd2SCorentin Chary     } else {
3502b54aa87SLuiz Capitulino         VncClientInfoList *cur_item = NULL;
3512b54aa87SLuiz Capitulino         struct sockaddr_storage sa;
3522b54aa87SLuiz Capitulino         socklen_t salen = sizeof(sa);
3532b54aa87SLuiz Capitulino         char host[NI_MAXHOST];
3542b54aa87SLuiz Capitulino         char serv[NI_MAXSERV];
3553e230dd2SCorentin Chary         VncState *client;
3563e230dd2SCorentin Chary 
3572b54aa87SLuiz Capitulino         info->enabled = true;
3582b54aa87SLuiz Capitulino 
3592b54aa87SLuiz Capitulino         /* for compatibility with the original command */
3602b54aa87SLuiz Capitulino         info->has_clients = true;
3612b54aa87SLuiz Capitulino 
3623e230dd2SCorentin Chary         QTAILQ_FOREACH(client, &vnc_display->clients, next) {
3632b54aa87SLuiz Capitulino             VncClientInfoList *cinfo = g_malloc0(sizeof(*info));
3642b54aa87SLuiz Capitulino             cinfo->value = qmp_query_vnc_client(client);
3652b54aa87SLuiz Capitulino 
3662b54aa87SLuiz Capitulino             /* XXX: waiting for the qapi to support GSList */
3672b54aa87SLuiz Capitulino             if (!cur_item) {
3682b54aa87SLuiz Capitulino                 info->clients = cur_item = cinfo;
3692b54aa87SLuiz Capitulino             } else {
3702b54aa87SLuiz Capitulino                 cur_item->next = cinfo;
3712b54aa87SLuiz Capitulino                 cur_item = cinfo;
3723e230dd2SCorentin Chary             }
3733e230dd2SCorentin Chary         }
3743e230dd2SCorentin Chary 
375417b0b88SPaolo Bonzini         if (vnc_display->lsock == -1) {
376417b0b88SPaolo Bonzini             return info;
377417b0b88SPaolo Bonzini         }
378417b0b88SPaolo Bonzini 
3792b54aa87SLuiz Capitulino         if (getsockname(vnc_display->lsock, (struct sockaddr *)&sa,
3802b54aa87SLuiz Capitulino                         &salen) == -1) {
3812b54aa87SLuiz Capitulino             error_set(errp, QERR_UNDEFINED_ERROR);
3822b54aa87SLuiz Capitulino             goto out_error;
3832b54aa87SLuiz Capitulino         }
3843e230dd2SCorentin Chary 
3852b54aa87SLuiz Capitulino         if (getnameinfo((struct sockaddr *)&sa, salen,
3862b54aa87SLuiz Capitulino                         host, sizeof(host),
3872b54aa87SLuiz Capitulino                         serv, sizeof(serv),
3882b54aa87SLuiz Capitulino                         NI_NUMERICHOST | NI_NUMERICSERV) < 0) {
3892b54aa87SLuiz Capitulino             error_set(errp, QERR_UNDEFINED_ERROR);
3902b54aa87SLuiz Capitulino             goto out_error;
3913e230dd2SCorentin Chary         }
3922b54aa87SLuiz Capitulino 
3932b54aa87SLuiz Capitulino         info->has_host = true;
3942b54aa87SLuiz Capitulino         info->host = g_strdup(host);
3952b54aa87SLuiz Capitulino 
3962b54aa87SLuiz Capitulino         info->has_service = true;
3972b54aa87SLuiz Capitulino         info->service = g_strdup(serv);
3982b54aa87SLuiz Capitulino 
3992b54aa87SLuiz Capitulino         info->has_family = true;
4002b54aa87SLuiz Capitulino         info->family = g_strdup(inet_strfamily(sa.ss_family));
4012b54aa87SLuiz Capitulino 
4022b54aa87SLuiz Capitulino         info->has_auth = true;
4032b54aa87SLuiz Capitulino         info->auth = g_strdup(vnc_auth_name(vnc_display));
4043e230dd2SCorentin Chary     }
4052b54aa87SLuiz Capitulino 
4062b54aa87SLuiz Capitulino     return info;
4072b54aa87SLuiz Capitulino 
4082b54aa87SLuiz Capitulino out_error:
4092b54aa87SLuiz Capitulino     qapi_free_VncInfo(info);
4102b54aa87SLuiz Capitulino     return NULL;
4113e230dd2SCorentin Chary }
4123e230dd2SCorentin Chary 
4133e230dd2SCorentin Chary /* TODO
4143e230dd2SCorentin Chary    1) Get the queue working for IO.
4153e230dd2SCorentin Chary    2) there is some weirdness when using the -S option (the screen is grey
4163e230dd2SCorentin Chary       and not totally invalidated
4173e230dd2SCorentin Chary    3) resolutions > 1024
4183e230dd2SCorentin Chary */
4193e230dd2SCorentin Chary 
42038ee14f4SGerd Hoffmann static int vnc_update_client(VncState *vs, int has_dirty, bool sync);
4213e230dd2SCorentin Chary static void vnc_disconnect_start(VncState *vs);
4223e230dd2SCorentin Chary 
4233e230dd2SCorentin Chary static void vnc_colordepth(VncState *vs);
4243e230dd2SCorentin Chary static void framebuffer_update_request(VncState *vs, int incremental,
4253e230dd2SCorentin Chary                                        int x_position, int y_position,
4263e230dd2SCorentin Chary                                        int w, int h);
4270f7b2864SGerd Hoffmann static void vnc_refresh(DisplayChangeListener *dcl);
4283e230dd2SCorentin Chary static int vnc_refresh_server_surface(VncDisplay *vd);
4293e230dd2SCorentin Chary 
4307c20b4a3SGerd Hoffmann static void vnc_dpy_update(DisplayChangeListener *dcl,
4317c20b4a3SGerd Hoffmann                            int x, int y, int w, int h)
4323e230dd2SCorentin Chary {
4333e230dd2SCorentin Chary     int i;
43421ef45d7SGerd Hoffmann     VncDisplay *vd = container_of(dcl, VncDisplay, dcl);
4353e230dd2SCorentin Chary     struct VncSurface *s = &vd->guest;
436d39fa6d8SGerd Hoffmann     int width = surface_width(vd->ds);
437d39fa6d8SGerd Hoffmann     int height = surface_height(vd->ds);
4383e230dd2SCorentin Chary 
4393e230dd2SCorentin Chary     h += y;
4403e230dd2SCorentin Chary 
4413e230dd2SCorentin Chary     /* round x down to ensure the loop only spans one 16-pixel block per,
4423e230dd2SCorentin Chary        iteration.  otherwise, if (x % 16) != 0, the last iteration may span
4433e230dd2SCorentin Chary        two 16-pixel blocks but we only mark the first as dirty
4443e230dd2SCorentin Chary     */
445b4c85ddcSPeter Lieven     w += (x % VNC_DIRTY_PIXELS_PER_BIT);
446b4c85ddcSPeter Lieven     x -= (x % VNC_DIRTY_PIXELS_PER_BIT);
4473e230dd2SCorentin Chary 
4489f64916dSGerd Hoffmann     x = MIN(x, width);
4499f64916dSGerd Hoffmann     y = MIN(y, height);
4509f64916dSGerd Hoffmann     w = MIN(x + w, width) - x;
4519f64916dSGerd Hoffmann     h = MIN(h, height);
4523e230dd2SCorentin Chary 
453b4c85ddcSPeter Lieven     for (; y < h; y++) {
454b4c85ddcSPeter Lieven         for (i = 0; i < w; i += VNC_DIRTY_PIXELS_PER_BIT) {
455b4c85ddcSPeter Lieven             set_bit((x + i) / VNC_DIRTY_PIXELS_PER_BIT, s->dirty[y]);
456b4c85ddcSPeter Lieven         }
457b4c85ddcSPeter Lieven     }
4583e230dd2SCorentin Chary }
4593e230dd2SCorentin Chary 
4603e230dd2SCorentin Chary void vnc_framebuffer_update(VncState *vs, int x, int y, int w, int h,
4613e230dd2SCorentin Chary                             int32_t encoding)
4623e230dd2SCorentin Chary {
4633e230dd2SCorentin Chary     vnc_write_u16(vs, x);
4643e230dd2SCorentin Chary     vnc_write_u16(vs, y);
4653e230dd2SCorentin Chary     vnc_write_u16(vs, w);
4663e230dd2SCorentin Chary     vnc_write_u16(vs, h);
4673e230dd2SCorentin Chary 
4683e230dd2SCorentin Chary     vnc_write_s32(vs, encoding);
4693e230dd2SCorentin Chary }
4703e230dd2SCorentin Chary 
4713e230dd2SCorentin Chary void buffer_reserve(Buffer *buffer, size_t len)
4723e230dd2SCorentin Chary {
4733e230dd2SCorentin Chary     if ((buffer->capacity - buffer->offset) < len) {
4743e230dd2SCorentin Chary         buffer->capacity += (len + 1024);
4757267c094SAnthony Liguori         buffer->buffer = g_realloc(buffer->buffer, buffer->capacity);
4763e230dd2SCorentin Chary         if (buffer->buffer == NULL) {
4773e230dd2SCorentin Chary             fprintf(stderr, "vnc: out of memory\n");
4783e230dd2SCorentin Chary             exit(1);
4793e230dd2SCorentin Chary         }
4803e230dd2SCorentin Chary     }
4813e230dd2SCorentin Chary }
4823e230dd2SCorentin Chary 
48371a8cdecSBlue Swirl static int buffer_empty(Buffer *buffer)
4843e230dd2SCorentin Chary {
4853e230dd2SCorentin Chary     return buffer->offset == 0;
4863e230dd2SCorentin Chary }
4873e230dd2SCorentin Chary 
4887536ee4bSTim Hardeck uint8_t *buffer_end(Buffer *buffer)
4893e230dd2SCorentin Chary {
4903e230dd2SCorentin Chary     return buffer->buffer + buffer->offset;
4913e230dd2SCorentin Chary }
4923e230dd2SCorentin Chary 
4933e230dd2SCorentin Chary void buffer_reset(Buffer *buffer)
4943e230dd2SCorentin Chary {
4953e230dd2SCorentin Chary         buffer->offset = 0;
4963e230dd2SCorentin Chary }
4973e230dd2SCorentin Chary 
4983e230dd2SCorentin Chary void buffer_free(Buffer *buffer)
4993e230dd2SCorentin Chary {
5007267c094SAnthony Liguori     g_free(buffer->buffer);
5013e230dd2SCorentin Chary     buffer->offset = 0;
5023e230dd2SCorentin Chary     buffer->capacity = 0;
5033e230dd2SCorentin Chary     buffer->buffer = NULL;
5043e230dd2SCorentin Chary }
5053e230dd2SCorentin Chary 
5063e230dd2SCorentin Chary void buffer_append(Buffer *buffer, const void *data, size_t len)
5073e230dd2SCorentin Chary {
5083e230dd2SCorentin Chary     memcpy(buffer->buffer + buffer->offset, data, len);
5093e230dd2SCorentin Chary     buffer->offset += len;
5103e230dd2SCorentin Chary }
5113e230dd2SCorentin Chary 
51232ed2680STim Hardeck void buffer_advance(Buffer *buf, size_t len)
51332ed2680STim Hardeck {
51432ed2680STim Hardeck     memmove(buf->buffer, buf->buffer + len,
51532ed2680STim Hardeck             (buf->offset - len));
51632ed2680STim Hardeck     buf->offset -= len;
51732ed2680STim Hardeck }
51832ed2680STim Hardeck 
5193e230dd2SCorentin Chary static void vnc_desktop_resize(VncState *vs)
5203e230dd2SCorentin Chary {
521d39fa6d8SGerd Hoffmann     DisplaySurface *ds = vs->vd->ds;
5223e230dd2SCorentin Chary 
5233e230dd2SCorentin Chary     if (vs->csock == -1 || !vnc_has_feature(vs, VNC_FEATURE_RESIZE)) {
5243e230dd2SCorentin Chary         return;
5253e230dd2SCorentin Chary     }
526d39fa6d8SGerd Hoffmann     if (vs->client_width == surface_width(ds) &&
527d39fa6d8SGerd Hoffmann         vs->client_height == surface_height(ds)) {
5283e230dd2SCorentin Chary         return;
5293e230dd2SCorentin Chary     }
530d39fa6d8SGerd Hoffmann     vs->client_width = surface_width(ds);
531d39fa6d8SGerd Hoffmann     vs->client_height = surface_height(ds);
532bd023f95SCorentin Chary     vnc_lock_output(vs);
5333e230dd2SCorentin Chary     vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE);
5343e230dd2SCorentin Chary     vnc_write_u8(vs, 0);
5353e230dd2SCorentin Chary     vnc_write_u16(vs, 1); /* number of rects */
5363e230dd2SCorentin Chary     vnc_framebuffer_update(vs, 0, 0, vs->client_width, vs->client_height,
5373e230dd2SCorentin Chary                            VNC_ENCODING_DESKTOPRESIZE);
538bd023f95SCorentin Chary     vnc_unlock_output(vs);
5393e230dd2SCorentin Chary     vnc_flush(vs);
5403e230dd2SCorentin Chary }
5413e230dd2SCorentin Chary 
542bd023f95SCorentin Chary static void vnc_abort_display_jobs(VncDisplay *vd)
543bd023f95SCorentin Chary {
544bd023f95SCorentin Chary     VncState *vs;
545bd023f95SCorentin Chary 
546bd023f95SCorentin Chary     QTAILQ_FOREACH(vs, &vd->clients, next) {
547bd023f95SCorentin Chary         vnc_lock_output(vs);
548bd023f95SCorentin Chary         vs->abort = true;
549bd023f95SCorentin Chary         vnc_unlock_output(vs);
550bd023f95SCorentin Chary     }
551bd023f95SCorentin Chary     QTAILQ_FOREACH(vs, &vd->clients, next) {
552bd023f95SCorentin Chary         vnc_jobs_join(vs);
553bd023f95SCorentin Chary     }
554bd023f95SCorentin Chary     QTAILQ_FOREACH(vs, &vd->clients, next) {
555bd023f95SCorentin Chary         vnc_lock_output(vs);
556bd023f95SCorentin Chary         vs->abort = false;
557bd023f95SCorentin Chary         vnc_unlock_output(vs);
558bd023f95SCorentin Chary     }
559bd023f95SCorentin Chary }
560bd023f95SCorentin Chary 
5619f64916dSGerd Hoffmann int vnc_server_fb_stride(VncDisplay *vd)
5629f64916dSGerd Hoffmann {
5639f64916dSGerd Hoffmann     return pixman_image_get_stride(vd->server);
5649f64916dSGerd Hoffmann }
5659f64916dSGerd Hoffmann 
5669f64916dSGerd Hoffmann void *vnc_server_fb_ptr(VncDisplay *vd, int x, int y)
5679f64916dSGerd Hoffmann {
5689f64916dSGerd Hoffmann     uint8_t *ptr;
5699f64916dSGerd Hoffmann 
5709f64916dSGerd Hoffmann     ptr  = (uint8_t *)pixman_image_get_data(vd->server);
5719f64916dSGerd Hoffmann     ptr += y * vnc_server_fb_stride(vd);
5729f64916dSGerd Hoffmann     ptr += x * VNC_SERVER_FB_BYTES;
5739f64916dSGerd Hoffmann     return ptr;
5749f64916dSGerd Hoffmann }
57512b316d4SPeter Lieven /* this sets only the visible pixels of a dirty bitmap */
57612b316d4SPeter Lieven #define VNC_SET_VISIBLE_PIXELS_DIRTY(bitmap, w, h) {\
57712b316d4SPeter Lieven         int y;\
57812b316d4SPeter Lieven         memset(bitmap, 0x00, sizeof(bitmap));\
57912b316d4SPeter Lieven         for (y = 0; y < h; y++) {\
58012b316d4SPeter Lieven             bitmap_set(bitmap[y], 0,\
58112b316d4SPeter Lieven                        DIV_ROUND_UP(w, VNC_DIRTY_PIXELS_PER_BIT));\
58212b316d4SPeter Lieven         } \
58312b316d4SPeter Lieven     }
5849f64916dSGerd Hoffmann 
585c12aeb86SGerd Hoffmann static void vnc_dpy_switch(DisplayChangeListener *dcl,
586c12aeb86SGerd Hoffmann                            DisplaySurface *surface)
5873e230dd2SCorentin Chary {
58821ef45d7SGerd Hoffmann     VncDisplay *vd = container_of(dcl, VncDisplay, dcl);
5893e230dd2SCorentin Chary     VncState *vs;
5903e230dd2SCorentin Chary 
591bd023f95SCorentin Chary     vnc_abort_display_jobs(vd);
592bd023f95SCorentin Chary 
5933e230dd2SCorentin Chary     /* server surface */
5949f64916dSGerd Hoffmann     qemu_pixman_image_unref(vd->server);
595d39fa6d8SGerd Hoffmann     vd->ds = surface;
5969f64916dSGerd Hoffmann     vd->server = pixman_image_create_bits(VNC_SERVER_FB_FORMAT,
597d39fa6d8SGerd Hoffmann                                           surface_width(vd->ds),
598d39fa6d8SGerd Hoffmann                                           surface_height(vd->ds),
5999f64916dSGerd Hoffmann                                           NULL, 0);
6003e230dd2SCorentin Chary 
6013e230dd2SCorentin Chary     /* guest surface */
6029f64916dSGerd Hoffmann #if 0 /* FIXME */
6033e230dd2SCorentin Chary     if (ds_get_bytes_per_pixel(ds) != vd->guest.ds->pf.bytes_per_pixel)
6043e230dd2SCorentin Chary         console_color_init(ds);
6059f64916dSGerd Hoffmann #endif
6069f64916dSGerd Hoffmann     qemu_pixman_image_unref(vd->guest.fb);
607d39fa6d8SGerd Hoffmann     vd->guest.fb = pixman_image_ref(surface->image);
608d39fa6d8SGerd Hoffmann     vd->guest.format = surface->format;
60912b316d4SPeter Lieven     VNC_SET_VISIBLE_PIXELS_DIRTY(vd->guest.dirty,
61012b316d4SPeter Lieven                                  surface_width(vd->ds),
61112b316d4SPeter Lieven                                  surface_height(vd->ds));
6123e230dd2SCorentin Chary 
6133e230dd2SCorentin Chary     QTAILQ_FOREACH(vs, &vd->clients, next) {
6143e230dd2SCorentin Chary         vnc_colordepth(vs);
6153e230dd2SCorentin Chary         vnc_desktop_resize(vs);
6163e230dd2SCorentin Chary         if (vs->vd->cursor) {
6173e230dd2SCorentin Chary             vnc_cursor_define(vs);
6183e230dd2SCorentin Chary         }
61912b316d4SPeter Lieven         VNC_SET_VISIBLE_PIXELS_DIRTY(vs->dirty,
62012b316d4SPeter Lieven                                      surface_width(vd->ds),
62112b316d4SPeter Lieven                                      surface_height(vd->ds));
6223e230dd2SCorentin Chary     }
6233e230dd2SCorentin Chary }
6243e230dd2SCorentin Chary 
6253e230dd2SCorentin Chary /* fastest code */
6269f64916dSGerd Hoffmann static void vnc_write_pixels_copy(VncState *vs,
6273e230dd2SCorentin Chary                                   void *pixels, int size)
6283e230dd2SCorentin Chary {
6293e230dd2SCorentin Chary     vnc_write(vs, pixels, size);
6303e230dd2SCorentin Chary }
6313e230dd2SCorentin Chary 
6323e230dd2SCorentin Chary /* slowest but generic code. */
6333e230dd2SCorentin Chary void vnc_convert_pixel(VncState *vs, uint8_t *buf, uint32_t v)
6343e230dd2SCorentin Chary {
6353e230dd2SCorentin Chary     uint8_t r, g, b;
6363e230dd2SCorentin Chary 
6379f64916dSGerd Hoffmann #if VNC_SERVER_FB_FORMAT == PIXMAN_FORMAT(32, PIXMAN_TYPE_ARGB, 0, 8, 8, 8)
6389f64916dSGerd Hoffmann     r = (((v & 0x00ff0000) >> 16) << vs->client_pf.rbits) >> 8;
6399f64916dSGerd Hoffmann     g = (((v & 0x0000ff00) >>  8) << vs->client_pf.gbits) >> 8;
6409f64916dSGerd Hoffmann     b = (((v & 0x000000ff) >>  0) << vs->client_pf.bbits) >> 8;
6419f64916dSGerd Hoffmann #else
6429f64916dSGerd Hoffmann # error need some bits here if you change VNC_SERVER_FB_FORMAT
6439f64916dSGerd Hoffmann #endif
6449f64916dSGerd Hoffmann     v = (r << vs->client_pf.rshift) |
6459f64916dSGerd Hoffmann         (g << vs->client_pf.gshift) |
6469f64916dSGerd Hoffmann         (b << vs->client_pf.bshift);
6479f64916dSGerd Hoffmann     switch (vs->client_pf.bytes_per_pixel) {
6483e230dd2SCorentin Chary     case 1:
6493e230dd2SCorentin Chary         buf[0] = v;
6503e230dd2SCorentin Chary         break;
6513e230dd2SCorentin Chary     case 2:
6529f64916dSGerd Hoffmann         if (vs->client_be) {
6533e230dd2SCorentin Chary             buf[0] = v >> 8;
6543e230dd2SCorentin Chary             buf[1] = v;
6553e230dd2SCorentin Chary         } else {
6563e230dd2SCorentin Chary             buf[1] = v >> 8;
6573e230dd2SCorentin Chary             buf[0] = v;
6583e230dd2SCorentin Chary         }
6593e230dd2SCorentin Chary         break;
6603e230dd2SCorentin Chary     default:
6613e230dd2SCorentin Chary     case 4:
6629f64916dSGerd Hoffmann         if (vs->client_be) {
6633e230dd2SCorentin Chary             buf[0] = v >> 24;
6643e230dd2SCorentin Chary             buf[1] = v >> 16;
6653e230dd2SCorentin Chary             buf[2] = v >> 8;
6663e230dd2SCorentin Chary             buf[3] = v;
6673e230dd2SCorentin Chary         } else {
6683e230dd2SCorentin Chary             buf[3] = v >> 24;
6693e230dd2SCorentin Chary             buf[2] = v >> 16;
6703e230dd2SCorentin Chary             buf[1] = v >> 8;
6713e230dd2SCorentin Chary             buf[0] = v;
6723e230dd2SCorentin Chary         }
6733e230dd2SCorentin Chary         break;
6743e230dd2SCorentin Chary     }
6753e230dd2SCorentin Chary }
6763e230dd2SCorentin Chary 
6779f64916dSGerd Hoffmann static void vnc_write_pixels_generic(VncState *vs,
6783e230dd2SCorentin Chary                                      void *pixels1, int size)
6793e230dd2SCorentin Chary {
6803e230dd2SCorentin Chary     uint8_t buf[4];
6813e230dd2SCorentin Chary 
6829f64916dSGerd Hoffmann     if (VNC_SERVER_FB_BYTES == 4) {
6833e230dd2SCorentin Chary         uint32_t *pixels = pixels1;
6843e230dd2SCorentin Chary         int n, i;
6853e230dd2SCorentin Chary         n = size >> 2;
6863e230dd2SCorentin Chary         for (i = 0; i < n; i++) {
6873e230dd2SCorentin Chary             vnc_convert_pixel(vs, buf, pixels[i]);
6889f64916dSGerd Hoffmann             vnc_write(vs, buf, vs->client_pf.bytes_per_pixel);
6893e230dd2SCorentin Chary         }
6903e230dd2SCorentin Chary     }
6913e230dd2SCorentin Chary }
6923e230dd2SCorentin Chary 
6933e230dd2SCorentin Chary int vnc_raw_send_framebuffer_update(VncState *vs, int x, int y, int w, int h)
6943e230dd2SCorentin Chary {
6953e230dd2SCorentin Chary     int i;
6963e230dd2SCorentin Chary     uint8_t *row;
6973e230dd2SCorentin Chary     VncDisplay *vd = vs->vd;
6983e230dd2SCorentin Chary 
6999f64916dSGerd Hoffmann     row = vnc_server_fb_ptr(vd, x, y);
7003e230dd2SCorentin Chary     for (i = 0; i < h; i++) {
7019f64916dSGerd Hoffmann         vs->write_pixels(vs, row, w * VNC_SERVER_FB_BYTES);
7029f64916dSGerd Hoffmann         row += vnc_server_fb_stride(vd);
7033e230dd2SCorentin Chary     }
7043e230dd2SCorentin Chary     return 1;
7053e230dd2SCorentin Chary }
7063e230dd2SCorentin Chary 
707bd023f95SCorentin Chary int vnc_send_framebuffer_update(VncState *vs, int x, int y, int w, int h)
7083e230dd2SCorentin Chary {
7093e230dd2SCorentin Chary     int n = 0;
7103e230dd2SCorentin Chary 
7113e230dd2SCorentin Chary     switch(vs->vnc_encoding) {
7123e230dd2SCorentin Chary         case VNC_ENCODING_ZLIB:
7133e230dd2SCorentin Chary             n = vnc_zlib_send_framebuffer_update(vs, x, y, w, h);
7143e230dd2SCorentin Chary             break;
7153e230dd2SCorentin Chary         case VNC_ENCODING_HEXTILE:
7163e230dd2SCorentin Chary             vnc_framebuffer_update(vs, x, y, w, h, VNC_ENCODING_HEXTILE);
7173e230dd2SCorentin Chary             n = vnc_hextile_send_framebuffer_update(vs, x, y, w, h);
7183e230dd2SCorentin Chary             break;
7193e230dd2SCorentin Chary         case VNC_ENCODING_TIGHT:
7203e230dd2SCorentin Chary             n = vnc_tight_send_framebuffer_update(vs, x, y, w, h);
7213e230dd2SCorentin Chary             break;
722efe556adSCorentin Chary         case VNC_ENCODING_TIGHT_PNG:
723efe556adSCorentin Chary             n = vnc_tight_png_send_framebuffer_update(vs, x, y, w, h);
724efe556adSCorentin Chary             break;
725148954faSCorentin Chary         case VNC_ENCODING_ZRLE:
726148954faSCorentin Chary             n = vnc_zrle_send_framebuffer_update(vs, x, y, w, h);
727148954faSCorentin Chary             break;
728148954faSCorentin Chary         case VNC_ENCODING_ZYWRLE:
729148954faSCorentin Chary             n = vnc_zywrle_send_framebuffer_update(vs, x, y, w, h);
730148954faSCorentin Chary             break;
7313e230dd2SCorentin Chary         default:
7323e230dd2SCorentin Chary             vnc_framebuffer_update(vs, x, y, w, h, VNC_ENCODING_RAW);
7333e230dd2SCorentin Chary             n = vnc_raw_send_framebuffer_update(vs, x, y, w, h);
7343e230dd2SCorentin Chary             break;
7353e230dd2SCorentin Chary     }
7363e230dd2SCorentin Chary     return n;
7373e230dd2SCorentin Chary }
7383e230dd2SCorentin Chary 
7393e230dd2SCorentin Chary static void vnc_copy(VncState *vs, int src_x, int src_y, int dst_x, int dst_y, int w, int h)
7403e230dd2SCorentin Chary {
7413e230dd2SCorentin Chary     /* send bitblit op to the vnc client */
742bd023f95SCorentin Chary     vnc_lock_output(vs);
7433e230dd2SCorentin Chary     vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE);
7443e230dd2SCorentin Chary     vnc_write_u8(vs, 0);
7453e230dd2SCorentin Chary     vnc_write_u16(vs, 1); /* number of rects */
7463e230dd2SCorentin Chary     vnc_framebuffer_update(vs, dst_x, dst_y, w, h, VNC_ENCODING_COPYRECT);
7473e230dd2SCorentin Chary     vnc_write_u16(vs, src_x);
7483e230dd2SCorentin Chary     vnc_write_u16(vs, src_y);
749bd023f95SCorentin Chary     vnc_unlock_output(vs);
7503e230dd2SCorentin Chary     vnc_flush(vs);
7513e230dd2SCorentin Chary }
7523e230dd2SCorentin Chary 
7537c20b4a3SGerd Hoffmann static void vnc_dpy_copy(DisplayChangeListener *dcl,
7547c20b4a3SGerd Hoffmann                          int src_x, int src_y,
7557c20b4a3SGerd Hoffmann                          int dst_x, int dst_y, int w, int h)
7563e230dd2SCorentin Chary {
75721ef45d7SGerd Hoffmann     VncDisplay *vd = container_of(dcl, VncDisplay, dcl);
7583e230dd2SCorentin Chary     VncState *vs, *vn;
7593e230dd2SCorentin Chary     uint8_t *src_row;
7603e230dd2SCorentin Chary     uint8_t *dst_row;
7619f64916dSGerd Hoffmann     int i, x, y, pitch, inc, w_lim, s;
7623e230dd2SCorentin Chary     int cmp_bytes;
7633e230dd2SCorentin Chary 
7643e230dd2SCorentin Chary     vnc_refresh_server_surface(vd);
7653e230dd2SCorentin Chary     QTAILQ_FOREACH_SAFE(vs, &vd->clients, next, vn) {
7663e230dd2SCorentin Chary         if (vnc_has_feature(vs, VNC_FEATURE_COPYRECT)) {
7673e230dd2SCorentin Chary             vs->force_update = 1;
76838ee14f4SGerd Hoffmann             vnc_update_client(vs, 1, true);
7693e230dd2SCorentin Chary             /* vs might be free()ed here */
7703e230dd2SCorentin Chary         }
7713e230dd2SCorentin Chary     }
7723e230dd2SCorentin Chary 
7733e230dd2SCorentin Chary     /* do bitblit op on the local surface too */
7749f64916dSGerd Hoffmann     pitch = vnc_server_fb_stride(vd);
7759f64916dSGerd Hoffmann     src_row = vnc_server_fb_ptr(vd, src_x, src_y);
7769f64916dSGerd Hoffmann     dst_row = vnc_server_fb_ptr(vd, dst_x, dst_y);
7773e230dd2SCorentin Chary     y = dst_y;
7783e230dd2SCorentin Chary     inc = 1;
7793e230dd2SCorentin Chary     if (dst_y > src_y) {
7803e230dd2SCorentin Chary         /* copy backwards */
7813e230dd2SCorentin Chary         src_row += pitch * (h-1);
7823e230dd2SCorentin Chary         dst_row += pitch * (h-1);
7833e230dd2SCorentin Chary         pitch = -pitch;
7843e230dd2SCorentin Chary         y = dst_y + h - 1;
7853e230dd2SCorentin Chary         inc = -1;
7863e230dd2SCorentin Chary     }
787b4c85ddcSPeter Lieven     w_lim = w - (VNC_DIRTY_PIXELS_PER_BIT - (dst_x % VNC_DIRTY_PIXELS_PER_BIT));
788b4c85ddcSPeter Lieven     if (w_lim < 0) {
7893e230dd2SCorentin Chary         w_lim = w;
790b4c85ddcSPeter Lieven     } else {
791b4c85ddcSPeter Lieven         w_lim = w - (w_lim % VNC_DIRTY_PIXELS_PER_BIT);
792b4c85ddcSPeter Lieven     }
7933e230dd2SCorentin Chary     for (i = 0; i < h; i++) {
7943e230dd2SCorentin Chary         for (x = 0; x <= w_lim;
7953e230dd2SCorentin Chary                 x += s, src_row += cmp_bytes, dst_row += cmp_bytes) {
7963e230dd2SCorentin Chary             if (x == w_lim) {
7973e230dd2SCorentin Chary                 if ((s = w - w_lim) == 0)
7983e230dd2SCorentin Chary                     break;
7993e230dd2SCorentin Chary             } else if (!x) {
800b4c85ddcSPeter Lieven                 s = (VNC_DIRTY_PIXELS_PER_BIT -
801b4c85ddcSPeter Lieven                     (dst_x % VNC_DIRTY_PIXELS_PER_BIT));
8023e230dd2SCorentin Chary                 s = MIN(s, w_lim);
8033e230dd2SCorentin Chary             } else {
804b4c85ddcSPeter Lieven                 s = VNC_DIRTY_PIXELS_PER_BIT;
8053e230dd2SCorentin Chary             }
8069f64916dSGerd Hoffmann             cmp_bytes = s * VNC_SERVER_FB_BYTES;
8073e230dd2SCorentin Chary             if (memcmp(src_row, dst_row, cmp_bytes) == 0)
8083e230dd2SCorentin Chary                 continue;
8093e230dd2SCorentin Chary             memmove(dst_row, src_row, cmp_bytes);
8103e230dd2SCorentin Chary             QTAILQ_FOREACH(vs, &vd->clients, next) {
8113e230dd2SCorentin Chary                 if (!vnc_has_feature(vs, VNC_FEATURE_COPYRECT)) {
812b4c85ddcSPeter Lieven                     set_bit(((x + dst_x) / VNC_DIRTY_PIXELS_PER_BIT),
813b4c85ddcSPeter Lieven                             vs->dirty[y]);
8143e230dd2SCorentin Chary                 }
8153e230dd2SCorentin Chary             }
8163e230dd2SCorentin Chary         }
8179f64916dSGerd Hoffmann         src_row += pitch - w * VNC_SERVER_FB_BYTES;
8189f64916dSGerd Hoffmann         dst_row += pitch - w * VNC_SERVER_FB_BYTES;
8193e230dd2SCorentin Chary         y += inc;
8203e230dd2SCorentin Chary     }
8213e230dd2SCorentin Chary 
8223e230dd2SCorentin Chary     QTAILQ_FOREACH(vs, &vd->clients, next) {
8233e230dd2SCorentin Chary         if (vnc_has_feature(vs, VNC_FEATURE_COPYRECT)) {
8243e230dd2SCorentin Chary             vnc_copy(vs, src_x, src_y, dst_x, dst_y, w, h);
8253e230dd2SCorentin Chary         }
8263e230dd2SCorentin Chary     }
8273e230dd2SCorentin Chary }
8283e230dd2SCorentin Chary 
8297c20b4a3SGerd Hoffmann static void vnc_mouse_set(DisplayChangeListener *dcl,
8307c20b4a3SGerd Hoffmann                           int x, int y, int visible)
8313e230dd2SCorentin Chary {
8323e230dd2SCorentin Chary     /* can we ask the client(s) to move the pointer ??? */
8333e230dd2SCorentin Chary }
8343e230dd2SCorentin Chary 
8353e230dd2SCorentin Chary static int vnc_cursor_define(VncState *vs)
8363e230dd2SCorentin Chary {
8373e230dd2SCorentin Chary     QEMUCursor *c = vs->vd->cursor;
8383e230dd2SCorentin Chary     int isize;
8393e230dd2SCorentin Chary 
8403e230dd2SCorentin Chary     if (vnc_has_feature(vs, VNC_FEATURE_RICH_CURSOR)) {
841d01f9595SCorentin Chary         vnc_lock_output(vs);
8423e230dd2SCorentin Chary         vnc_write_u8(vs,  VNC_MSG_SERVER_FRAMEBUFFER_UPDATE);
8433e230dd2SCorentin Chary         vnc_write_u8(vs,  0);  /*  padding     */
8443e230dd2SCorentin Chary         vnc_write_u16(vs, 1);  /*  # of rects  */
8453e230dd2SCorentin Chary         vnc_framebuffer_update(vs, c->hot_x, c->hot_y, c->width, c->height,
8463e230dd2SCorentin Chary                                VNC_ENCODING_RICH_CURSOR);
8479f64916dSGerd Hoffmann         isize = c->width * c->height * vs->client_pf.bytes_per_pixel;
8489f64916dSGerd Hoffmann         vnc_write_pixels_generic(vs, c->data, isize);
8493e230dd2SCorentin Chary         vnc_write(vs, vs->vd->cursor_mask, vs->vd->cursor_msize);
850d01f9595SCorentin Chary         vnc_unlock_output(vs);
8513e230dd2SCorentin Chary         return 0;
8523e230dd2SCorentin Chary     }
8533e230dd2SCorentin Chary     return -1;
8543e230dd2SCorentin Chary }
8553e230dd2SCorentin Chary 
8567c20b4a3SGerd Hoffmann static void vnc_dpy_cursor_define(DisplayChangeListener *dcl,
8577c20b4a3SGerd Hoffmann                                   QEMUCursor *c)
8583e230dd2SCorentin Chary {
8593e230dd2SCorentin Chary     VncDisplay *vd = vnc_display;
8603e230dd2SCorentin Chary     VncState *vs;
8613e230dd2SCorentin Chary 
8623e230dd2SCorentin Chary     cursor_put(vd->cursor);
8637267c094SAnthony Liguori     g_free(vd->cursor_mask);
8643e230dd2SCorentin Chary 
8653e230dd2SCorentin Chary     vd->cursor = c;
8663e230dd2SCorentin Chary     cursor_get(vd->cursor);
8673e230dd2SCorentin Chary     vd->cursor_msize = cursor_get_mono_bpl(c) * c->height;
8687267c094SAnthony Liguori     vd->cursor_mask = g_malloc0(vd->cursor_msize);
8693e230dd2SCorentin Chary     cursor_get_mono_mask(c, 0, vd->cursor_mask);
8703e230dd2SCorentin Chary 
8713e230dd2SCorentin Chary     QTAILQ_FOREACH(vs, &vd->clients, next) {
8723e230dd2SCorentin Chary         vnc_cursor_define(vs);
8733e230dd2SCorentin Chary     }
8743e230dd2SCorentin Chary }
8753e230dd2SCorentin Chary 
8763e230dd2SCorentin Chary static int find_and_clear_dirty_height(struct VncState *vs,
8776c71a539SCorentin Chary                                        int y, int last_x, int x, int height)
8783e230dd2SCorentin Chary {
8793e230dd2SCorentin Chary     int h;
8803e230dd2SCorentin Chary 
8816c71a539SCorentin Chary     for (h = 1; h < (height - y); h++) {
882bc2429b9SCorentin Chary         if (!test_bit(last_x, vs->dirty[y + h])) {
8833e230dd2SCorentin Chary             break;
884bc2429b9SCorentin Chary         }
885*863d7c91SPeter Lieven         bitmap_clear(vs->dirty[y + h], last_x, x - last_x);
8863e230dd2SCorentin Chary     }
8873e230dd2SCorentin Chary 
8883e230dd2SCorentin Chary     return h;
8893e230dd2SCorentin Chary }
8903e230dd2SCorentin Chary 
89138ee14f4SGerd Hoffmann static int vnc_update_client(VncState *vs, int has_dirty, bool sync)
8923e230dd2SCorentin Chary {
8933e230dd2SCorentin Chary     if (vs->need_update && vs->csock != -1) {
8943e230dd2SCorentin Chary         VncDisplay *vd = vs->vd;
895bd023f95SCorentin Chary         VncJob *job;
8963e230dd2SCorentin Chary         int y;
89712b316d4SPeter Lieven         int height;
898bd023f95SCorentin Chary         int n = 0;
899bd023f95SCorentin Chary 
9003e230dd2SCorentin Chary         if (vs->output.offset && !vs->audio_cap && !vs->force_update)
9013e230dd2SCorentin Chary             /* kernel send buffers are full -> drop frames to throttle */
9023e230dd2SCorentin Chary             return 0;
9033e230dd2SCorentin Chary 
9043e230dd2SCorentin Chary         if (!has_dirty && !vs->audio_cap && !vs->force_update)
9053e230dd2SCorentin Chary             return 0;
9063e230dd2SCorentin Chary 
9073e230dd2SCorentin Chary         /*
9083e230dd2SCorentin Chary          * Send screen updates to the vnc client using the server
9093e230dd2SCorentin Chary          * surface and server dirty map.  guest surface updates
9103e230dd2SCorentin Chary          * happening in parallel don't disturb us, the next pass will
9113e230dd2SCorentin Chary          * send them to the client.
9123e230dd2SCorentin Chary          */
913bd023f95SCorentin Chary         job = vnc_job_new(vs);
9143e230dd2SCorentin Chary 
9159f64916dSGerd Hoffmann         height = MIN(pixman_image_get_height(vd->server), vs->client_height);
9163e230dd2SCorentin Chary 
91712b316d4SPeter Lieven         y = 0;
91812b316d4SPeter Lieven         for (;;) {
91912b316d4SPeter Lieven             int x, h;
92012b316d4SPeter Lieven             unsigned long x2;
92112b316d4SPeter Lieven             unsigned long offset = find_next_bit((unsigned long *) &vs->dirty,
92212b316d4SPeter Lieven                                                  height * VNC_DIRTY_BPL(vs),
92312b316d4SPeter Lieven                                                  y * VNC_DIRTY_BPL(vs));
92412b316d4SPeter Lieven             if (offset == height * VNC_DIRTY_BPL(vs)) {
92512b316d4SPeter Lieven                 /* no more dirty bits */
92612b316d4SPeter Lieven                 break;
9273e230dd2SCorentin Chary             }
92812b316d4SPeter Lieven             y = offset / VNC_DIRTY_BPL(vs);
92912b316d4SPeter Lieven             x = offset % VNC_DIRTY_BPL(vs);
93012b316d4SPeter Lieven             x2 = find_next_zero_bit((unsigned long *) &vs->dirty[y],
93112b316d4SPeter Lieven                                     VNC_DIRTY_BPL(vs), x);
93212b316d4SPeter Lieven             bitmap_clear(vs->dirty[y], x, x2 - x);
93312b316d4SPeter Lieven             h = find_and_clear_dirty_height(vs, y, x, x2, height);
93412b316d4SPeter Lieven             n += vnc_job_add_rect(job, x * VNC_DIRTY_PIXELS_PER_BIT, y,
93512b316d4SPeter Lieven                                   (x2 - x) * VNC_DIRTY_PIXELS_PER_BIT, h);
9363e230dd2SCorentin Chary         }
937bd023f95SCorentin Chary 
938bd023f95SCorentin Chary         vnc_job_push(job);
9393e230dd2SCorentin Chary         vs->force_update = 0;
940bd023f95SCorentin Chary         return n;
9413e230dd2SCorentin Chary     }
9423e230dd2SCorentin Chary 
94338ee14f4SGerd Hoffmann     if (vs->csock == -1) {
9443e230dd2SCorentin Chary         vnc_disconnect_finish(vs);
94538ee14f4SGerd Hoffmann     } else if (sync) {
94638ee14f4SGerd Hoffmann         vnc_jobs_join(vs);
94738ee14f4SGerd Hoffmann     }
9483e230dd2SCorentin Chary 
9493e230dd2SCorentin Chary     return 0;
9503e230dd2SCorentin Chary }
9513e230dd2SCorentin Chary 
9523e230dd2SCorentin Chary /* audio */
9533e230dd2SCorentin Chary static void audio_capture_notify(void *opaque, audcnotification_e cmd)
9543e230dd2SCorentin Chary {
9553e230dd2SCorentin Chary     VncState *vs = opaque;
9563e230dd2SCorentin Chary 
9573e230dd2SCorentin Chary     switch (cmd) {
9583e230dd2SCorentin Chary     case AUD_CNOTIFY_DISABLE:
959bd023f95SCorentin Chary         vnc_lock_output(vs);
9603e230dd2SCorentin Chary         vnc_write_u8(vs, VNC_MSG_SERVER_QEMU);
9613e230dd2SCorentin Chary         vnc_write_u8(vs, VNC_MSG_SERVER_QEMU_AUDIO);
9623e230dd2SCorentin Chary         vnc_write_u16(vs, VNC_MSG_SERVER_QEMU_AUDIO_END);
963bd023f95SCorentin Chary         vnc_unlock_output(vs);
9643e230dd2SCorentin Chary         vnc_flush(vs);
9653e230dd2SCorentin Chary         break;
9663e230dd2SCorentin Chary 
9673e230dd2SCorentin Chary     case AUD_CNOTIFY_ENABLE:
968bd023f95SCorentin Chary         vnc_lock_output(vs);
9693e230dd2SCorentin Chary         vnc_write_u8(vs, VNC_MSG_SERVER_QEMU);
9703e230dd2SCorentin Chary         vnc_write_u8(vs, VNC_MSG_SERVER_QEMU_AUDIO);
9713e230dd2SCorentin Chary         vnc_write_u16(vs, VNC_MSG_SERVER_QEMU_AUDIO_BEGIN);
972bd023f95SCorentin Chary         vnc_unlock_output(vs);
9733e230dd2SCorentin Chary         vnc_flush(vs);
9743e230dd2SCorentin Chary         break;
9753e230dd2SCorentin Chary     }
9763e230dd2SCorentin Chary }
9773e230dd2SCorentin Chary 
9783e230dd2SCorentin Chary static void audio_capture_destroy(void *opaque)
9793e230dd2SCorentin Chary {
9803e230dd2SCorentin Chary }
9813e230dd2SCorentin Chary 
9823e230dd2SCorentin Chary static void audio_capture(void *opaque, void *buf, int size)
9833e230dd2SCorentin Chary {
9843e230dd2SCorentin Chary     VncState *vs = opaque;
9853e230dd2SCorentin Chary 
986bd023f95SCorentin Chary     vnc_lock_output(vs);
9873e230dd2SCorentin Chary     vnc_write_u8(vs, VNC_MSG_SERVER_QEMU);
9883e230dd2SCorentin Chary     vnc_write_u8(vs, VNC_MSG_SERVER_QEMU_AUDIO);
9893e230dd2SCorentin Chary     vnc_write_u16(vs, VNC_MSG_SERVER_QEMU_AUDIO_DATA);
9903e230dd2SCorentin Chary     vnc_write_u32(vs, size);
9913e230dd2SCorentin Chary     vnc_write(vs, buf, size);
992bd023f95SCorentin Chary     vnc_unlock_output(vs);
9933e230dd2SCorentin Chary     vnc_flush(vs);
9943e230dd2SCorentin Chary }
9953e230dd2SCorentin Chary 
9963e230dd2SCorentin Chary static void audio_add(VncState *vs)
9973e230dd2SCorentin Chary {
9983e230dd2SCorentin Chary     struct audio_capture_ops ops;
9993e230dd2SCorentin Chary 
10003e230dd2SCorentin Chary     if (vs->audio_cap) {
10013e230dd2SCorentin Chary         monitor_printf(default_mon, "audio already running\n");
10023e230dd2SCorentin Chary         return;
10033e230dd2SCorentin Chary     }
10043e230dd2SCorentin Chary 
10053e230dd2SCorentin Chary     ops.notify = audio_capture_notify;
10063e230dd2SCorentin Chary     ops.destroy = audio_capture_destroy;
10073e230dd2SCorentin Chary     ops.capture = audio_capture;
10083e230dd2SCorentin Chary 
10093e230dd2SCorentin Chary     vs->audio_cap = AUD_add_capture(&vs->as, &ops, vs);
10103e230dd2SCorentin Chary     if (!vs->audio_cap) {
10113e230dd2SCorentin Chary         monitor_printf(default_mon, "Failed to add audio capture\n");
10123e230dd2SCorentin Chary     }
10133e230dd2SCorentin Chary }
10143e230dd2SCorentin Chary 
10153e230dd2SCorentin Chary static void audio_del(VncState *vs)
10163e230dd2SCorentin Chary {
10173e230dd2SCorentin Chary     if (vs->audio_cap) {
10183e230dd2SCorentin Chary         AUD_del_capture(vs->audio_cap, vs);
10193e230dd2SCorentin Chary         vs->audio_cap = NULL;
10203e230dd2SCorentin Chary     }
10213e230dd2SCorentin Chary }
10223e230dd2SCorentin Chary 
10233e230dd2SCorentin Chary static void vnc_disconnect_start(VncState *vs)
10243e230dd2SCorentin Chary {
10253e230dd2SCorentin Chary     if (vs->csock == -1)
10263e230dd2SCorentin Chary         return;
10278cf36489SGerd Hoffmann     vnc_set_share_mode(vs, VNC_SHARE_MODE_DISCONNECTED);
10283e230dd2SCorentin Chary     qemu_set_fd_handler2(vs->csock, NULL, NULL, NULL, NULL);
10293e230dd2SCorentin Chary     closesocket(vs->csock);
10303e230dd2SCorentin Chary     vs->csock = -1;
10313e230dd2SCorentin Chary }
10323e230dd2SCorentin Chary 
10337536ee4bSTim Hardeck void vnc_disconnect_finish(VncState *vs)
10343e230dd2SCorentin Chary {
10357d964c9dSCorentin Chary     int i;
10367d964c9dSCorentin Chary 
1037bd023f95SCorentin Chary     vnc_jobs_join(vs); /* Wait encoding jobs */
1038bd023f95SCorentin Chary 
1039bd023f95SCorentin Chary     vnc_lock_output(vs);
10403e230dd2SCorentin Chary     vnc_qmp_event(vs, QEVENT_VNC_DISCONNECTED);
10413e230dd2SCorentin Chary 
10423e230dd2SCorentin Chary     buffer_free(&vs->input);
10433e230dd2SCorentin Chary     buffer_free(&vs->output);
10447536ee4bSTim Hardeck #ifdef CONFIG_VNC_WS
10457536ee4bSTim Hardeck     buffer_free(&vs->ws_input);
10467536ee4bSTim Hardeck     buffer_free(&vs->ws_output);
10477536ee4bSTim Hardeck #endif /* CONFIG_VNC_WS */
10483e230dd2SCorentin Chary 
10493e230dd2SCorentin Chary     qobject_decref(vs->info);
10503e230dd2SCorentin Chary 
10513e230dd2SCorentin Chary     vnc_zlib_clear(vs);
10523e230dd2SCorentin Chary     vnc_tight_clear(vs);
1053148954faSCorentin Chary     vnc_zrle_clear(vs);
10543e230dd2SCorentin Chary 
10553e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS
10563e230dd2SCorentin Chary     vnc_tls_client_cleanup(vs);
10573e230dd2SCorentin Chary #endif /* CONFIG_VNC_TLS */
10583e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL
10593e230dd2SCorentin Chary     vnc_sasl_client_cleanup(vs);
10603e230dd2SCorentin Chary #endif /* CONFIG_VNC_SASL */
10613e230dd2SCorentin Chary     audio_del(vs);
10627bc9318bSGerd Hoffmann     vnc_release_modifiers(vs);
10633e230dd2SCorentin Chary 
10646fd8e79aSTim Hardeck     if (vs->initialized) {
10653e230dd2SCorentin Chary         QTAILQ_REMOVE(&vs->vd->clients, vs, next);
10666fd8e79aSTim Hardeck         qemu_remove_mouse_mode_change_notifier(&vs->mouse_mode_notifier);
10676fd8e79aSTim Hardeck     }
10683e230dd2SCorentin Chary 
10693e230dd2SCorentin Chary     if (vs->vd->lock_key_sync)
10703e230dd2SCorentin Chary         qemu_remove_led_event_handler(vs->led);
1071bd023f95SCorentin Chary     vnc_unlock_output(vs);
1072bd023f95SCorentin Chary 
1073bd023f95SCorentin Chary     qemu_mutex_destroy(&vs->output_mutex);
10746fd8e79aSTim Hardeck     if (vs->bh != NULL) {
1075175b2a6eSCorentin Chary         qemu_bh_delete(vs->bh);
10766fd8e79aSTim Hardeck     }
1077175b2a6eSCorentin Chary     buffer_free(&vs->jobs_buffer);
1078175b2a6eSCorentin Chary 
10797d964c9dSCorentin Chary     for (i = 0; i < VNC_STAT_ROWS; ++i) {
10807267c094SAnthony Liguori         g_free(vs->lossy_rect[i]);
10817d964c9dSCorentin Chary     }
10827267c094SAnthony Liguori     g_free(vs->lossy_rect);
10837267c094SAnthony Liguori     g_free(vs);
10843e230dd2SCorentin Chary }
10853e230dd2SCorentin Chary 
10863e230dd2SCorentin Chary int vnc_client_io_error(VncState *vs, int ret, int last_errno)
10873e230dd2SCorentin Chary {
10883e230dd2SCorentin Chary     if (ret == 0 || ret == -1) {
10893e230dd2SCorentin Chary         if (ret == -1) {
10903e230dd2SCorentin Chary             switch (last_errno) {
10913e230dd2SCorentin Chary                 case EINTR:
10923e230dd2SCorentin Chary                 case EAGAIN:
10933e230dd2SCorentin Chary #ifdef _WIN32
10943e230dd2SCorentin Chary                 case WSAEWOULDBLOCK:
10953e230dd2SCorentin Chary #endif
10963e230dd2SCorentin Chary                     return 0;
10973e230dd2SCorentin Chary                 default:
10983e230dd2SCorentin Chary                     break;
10993e230dd2SCorentin Chary             }
11003e230dd2SCorentin Chary         }
11013e230dd2SCorentin Chary 
11023e230dd2SCorentin Chary         VNC_DEBUG("Closing down client sock: ret %d, errno %d\n",
11033e230dd2SCorentin Chary                   ret, ret < 0 ? last_errno : 0);
11043e230dd2SCorentin Chary         vnc_disconnect_start(vs);
11053e230dd2SCorentin Chary 
11063e230dd2SCorentin Chary         return 0;
11073e230dd2SCorentin Chary     }
11083e230dd2SCorentin Chary     return ret;
11093e230dd2SCorentin Chary }
11103e230dd2SCorentin Chary 
11113e230dd2SCorentin Chary 
11123e230dd2SCorentin Chary void vnc_client_error(VncState *vs)
11133e230dd2SCorentin Chary {
11143e230dd2SCorentin Chary     VNC_DEBUG("Closing down client sock: protocol error\n");
11153e230dd2SCorentin Chary     vnc_disconnect_start(vs);
11163e230dd2SCorentin Chary }
11173e230dd2SCorentin Chary 
11180057a0d5STim Hardeck #ifdef CONFIG_VNC_TLS
11190057a0d5STim Hardeck static long vnc_client_write_tls(gnutls_session_t *session,
11200057a0d5STim Hardeck                                  const uint8_t *data,
11210057a0d5STim Hardeck                                  size_t datalen)
11220057a0d5STim Hardeck {
11230057a0d5STim Hardeck     long ret = gnutls_write(*session, data, datalen);
11240057a0d5STim Hardeck     if (ret < 0) {
11250057a0d5STim Hardeck         if (ret == GNUTLS_E_AGAIN) {
11260057a0d5STim Hardeck             errno = EAGAIN;
11270057a0d5STim Hardeck         } else {
11280057a0d5STim Hardeck             errno = EIO;
11290057a0d5STim Hardeck         }
11300057a0d5STim Hardeck         ret = -1;
11310057a0d5STim Hardeck     }
11320057a0d5STim Hardeck     return ret;
11330057a0d5STim Hardeck }
11340057a0d5STim Hardeck #endif /* CONFIG_VNC_TLS */
11353e230dd2SCorentin Chary 
11363e230dd2SCorentin Chary /*
11373e230dd2SCorentin Chary  * Called to write a chunk of data to the client socket. The data may
11383e230dd2SCorentin Chary  * be the raw data, or may have already been encoded by SASL.
11393e230dd2SCorentin Chary  * The data will be written either straight onto the socket, or
11403e230dd2SCorentin Chary  * written via the GNUTLS wrappers, if TLS/SSL encryption is enabled
11413e230dd2SCorentin Chary  *
11423e230dd2SCorentin Chary  * NB, it is theoretically possible to have 2 layers of encryption,
11433e230dd2SCorentin Chary  * both SASL, and this TLS layer. It is highly unlikely in practice
11443e230dd2SCorentin Chary  * though, since SASL encryption will typically be a no-op if TLS
11453e230dd2SCorentin Chary  * is active
11463e230dd2SCorentin Chary  *
11473e230dd2SCorentin Chary  * Returns the number of bytes written, which may be less than
11483e230dd2SCorentin Chary  * the requested 'datalen' if the socket would block. Returns
11493e230dd2SCorentin Chary  * -1 on error, and disconnects the client socket.
11503e230dd2SCorentin Chary  */
11513e230dd2SCorentin Chary long vnc_client_write_buf(VncState *vs, const uint8_t *data, size_t datalen)
11523e230dd2SCorentin Chary {
11533e230dd2SCorentin Chary     long ret;
11543e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS
11553e230dd2SCorentin Chary     if (vs->tls.session) {
11560057a0d5STim Hardeck         ret = vnc_client_write_tls(&vs->tls.session, data, datalen);
11570057a0d5STim Hardeck     } else {
11580057a0d5STim Hardeck #ifdef CONFIG_VNC_WS
11590057a0d5STim Hardeck         if (vs->ws_tls.session) {
11600057a0d5STim Hardeck             ret = vnc_client_write_tls(&vs->ws_tls.session, data, datalen);
11613e230dd2SCorentin Chary         } else
11620057a0d5STim Hardeck #endif /* CONFIG_VNC_WS */
11633e230dd2SCorentin Chary #endif /* CONFIG_VNC_TLS */
11640057a0d5STim Hardeck         {
11653e230dd2SCorentin Chary             ret = send(vs->csock, (const void *)data, datalen, 0);
11660057a0d5STim Hardeck         }
11670057a0d5STim Hardeck #ifdef CONFIG_VNC_TLS
11680057a0d5STim Hardeck     }
11690057a0d5STim Hardeck #endif /* CONFIG_VNC_TLS */
11703e230dd2SCorentin Chary     VNC_DEBUG("Wrote wire %p %zd -> %ld\n", data, datalen, ret);
11713e230dd2SCorentin Chary     return vnc_client_io_error(vs, ret, socket_error());
11723e230dd2SCorentin Chary }
11733e230dd2SCorentin Chary 
11743e230dd2SCorentin Chary 
11753e230dd2SCorentin Chary /*
11763e230dd2SCorentin Chary  * Called to write buffered data to the client socket, when not
11773e230dd2SCorentin Chary  * using any SASL SSF encryption layers. Will write as much data
11783e230dd2SCorentin Chary  * as possible without blocking. If all buffered data is written,
11793e230dd2SCorentin Chary  * will switch the FD poll() handler back to read monitoring.
11803e230dd2SCorentin Chary  *
11813e230dd2SCorentin Chary  * Returns the number of bytes written, which may be less than
11823e230dd2SCorentin Chary  * the buffered output data if the socket would block. Returns
11833e230dd2SCorentin Chary  * -1 on error, and disconnects the client socket.
11843e230dd2SCorentin Chary  */
11853e230dd2SCorentin Chary static long vnc_client_write_plain(VncState *vs)
11863e230dd2SCorentin Chary {
11873e230dd2SCorentin Chary     long ret;
11883e230dd2SCorentin Chary 
11893e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL
11903e230dd2SCorentin Chary     VNC_DEBUG("Write Plain: Pending output %p size %zd offset %zd. Wait SSF %d\n",
11913e230dd2SCorentin Chary               vs->output.buffer, vs->output.capacity, vs->output.offset,
11923e230dd2SCorentin Chary               vs->sasl.waitWriteSSF);
11933e230dd2SCorentin Chary 
11943e230dd2SCorentin Chary     if (vs->sasl.conn &&
11953e230dd2SCorentin Chary         vs->sasl.runSSF &&
11963e230dd2SCorentin Chary         vs->sasl.waitWriteSSF) {
11973e230dd2SCorentin Chary         ret = vnc_client_write_buf(vs, vs->output.buffer, vs->sasl.waitWriteSSF);
11983e230dd2SCorentin Chary         if (ret)
11993e230dd2SCorentin Chary             vs->sasl.waitWriteSSF -= ret;
12003e230dd2SCorentin Chary     } else
12013e230dd2SCorentin Chary #endif /* CONFIG_VNC_SASL */
12023e230dd2SCorentin Chary         ret = vnc_client_write_buf(vs, vs->output.buffer, vs->output.offset);
12033e230dd2SCorentin Chary     if (!ret)
12043e230dd2SCorentin Chary         return 0;
12053e230dd2SCorentin Chary 
120632ed2680STim Hardeck     buffer_advance(&vs->output, ret);
12073e230dd2SCorentin Chary 
12083e230dd2SCorentin Chary     if (vs->output.offset == 0) {
12093e230dd2SCorentin Chary         qemu_set_fd_handler2(vs->csock, NULL, vnc_client_read, NULL, vs);
12103e230dd2SCorentin Chary     }
12113e230dd2SCorentin Chary 
12123e230dd2SCorentin Chary     return ret;
12133e230dd2SCorentin Chary }
12143e230dd2SCorentin Chary 
12153e230dd2SCorentin Chary 
12163e230dd2SCorentin Chary /*
12173e230dd2SCorentin Chary  * First function called whenever there is data to be written to
12183e230dd2SCorentin Chary  * the client socket. Will delegate actual work according to whether
12193e230dd2SCorentin Chary  * SASL SSF layers are enabled (thus requiring encryption calls)
12203e230dd2SCorentin Chary  */
1221bd023f95SCorentin Chary static void vnc_client_write_locked(void *opaque)
12223e230dd2SCorentin Chary {
12233e230dd2SCorentin Chary     VncState *vs = opaque;
12243e230dd2SCorentin Chary 
12253e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL
12263e230dd2SCorentin Chary     if (vs->sasl.conn &&
12273e230dd2SCorentin Chary         vs->sasl.runSSF &&
12283e230dd2SCorentin Chary         !vs->sasl.waitWriteSSF) {
12293e230dd2SCorentin Chary         vnc_client_write_sasl(vs);
12303e230dd2SCorentin Chary     } else
12313e230dd2SCorentin Chary #endif /* CONFIG_VNC_SASL */
12327536ee4bSTim Hardeck     {
12337536ee4bSTim Hardeck #ifdef CONFIG_VNC_WS
12347536ee4bSTim Hardeck         if (vs->encode_ws) {
12357536ee4bSTim Hardeck             vnc_client_write_ws(vs);
12367536ee4bSTim Hardeck         } else
12377536ee4bSTim Hardeck #endif /* CONFIG_VNC_WS */
12387536ee4bSTim Hardeck         {
12393e230dd2SCorentin Chary             vnc_client_write_plain(vs);
12403e230dd2SCorentin Chary         }
12417536ee4bSTim Hardeck     }
12427536ee4bSTim Hardeck }
12433e230dd2SCorentin Chary 
1244bd023f95SCorentin Chary void vnc_client_write(void *opaque)
1245bd023f95SCorentin Chary {
1246bd023f95SCorentin Chary     VncState *vs = opaque;
1247bd023f95SCorentin Chary 
1248bd023f95SCorentin Chary     vnc_lock_output(vs);
12497536ee4bSTim Hardeck     if (vs->output.offset
12507536ee4bSTim Hardeck #ifdef CONFIG_VNC_WS
12517536ee4bSTim Hardeck             || vs->ws_output.offset
12527536ee4bSTim Hardeck #endif
12537536ee4bSTim Hardeck             ) {
1254bd023f95SCorentin Chary         vnc_client_write_locked(opaque);
1255ac71103dSYoshiaki Tamura     } else if (vs->csock != -1) {
1256bd023f95SCorentin Chary         qemu_set_fd_handler2(vs->csock, NULL, vnc_client_read, NULL, vs);
1257bd023f95SCorentin Chary     }
1258bd023f95SCorentin Chary     vnc_unlock_output(vs);
1259bd023f95SCorentin Chary }
1260bd023f95SCorentin Chary 
12613e230dd2SCorentin Chary void vnc_read_when(VncState *vs, VncReadEvent *func, size_t expecting)
12623e230dd2SCorentin Chary {
12633e230dd2SCorentin Chary     vs->read_handler = func;
12643e230dd2SCorentin Chary     vs->read_handler_expect = expecting;
12653e230dd2SCorentin Chary }
12663e230dd2SCorentin Chary 
12670057a0d5STim Hardeck #ifdef CONFIG_VNC_TLS
12680057a0d5STim Hardeck static long vnc_client_read_tls(gnutls_session_t *session, uint8_t *data,
12690057a0d5STim Hardeck                                 size_t datalen)
12700057a0d5STim Hardeck {
12710057a0d5STim Hardeck     long ret = gnutls_read(*session, data, datalen);
12720057a0d5STim Hardeck     if (ret < 0) {
12730057a0d5STim Hardeck         if (ret == GNUTLS_E_AGAIN) {
12740057a0d5STim Hardeck             errno = EAGAIN;
12750057a0d5STim Hardeck         } else {
12760057a0d5STim Hardeck             errno = EIO;
12770057a0d5STim Hardeck         }
12780057a0d5STim Hardeck         ret = -1;
12790057a0d5STim Hardeck     }
12800057a0d5STim Hardeck     return ret;
12810057a0d5STim Hardeck }
12820057a0d5STim Hardeck #endif /* CONFIG_VNC_TLS */
12833e230dd2SCorentin Chary 
12843e230dd2SCorentin Chary /*
12853e230dd2SCorentin Chary  * Called to read a chunk of data from the client socket. The data may
12863e230dd2SCorentin Chary  * be the raw data, or may need to be further decoded by SASL.
12873e230dd2SCorentin Chary  * The data will be read either straight from to the socket, or
12883e230dd2SCorentin Chary  * read via the GNUTLS wrappers, if TLS/SSL encryption is enabled
12893e230dd2SCorentin Chary  *
12903e230dd2SCorentin Chary  * NB, it is theoretically possible to have 2 layers of encryption,
12913e230dd2SCorentin Chary  * both SASL, and this TLS layer. It is highly unlikely in practice
12923e230dd2SCorentin Chary  * though, since SASL encryption will typically be a no-op if TLS
12933e230dd2SCorentin Chary  * is active
12943e230dd2SCorentin Chary  *
12953e230dd2SCorentin Chary  * Returns the number of bytes read, which may be less than
12963e230dd2SCorentin Chary  * the requested 'datalen' if the socket would block. Returns
12973e230dd2SCorentin Chary  * -1 on error, and disconnects the client socket.
12983e230dd2SCorentin Chary  */
12993e230dd2SCorentin Chary long vnc_client_read_buf(VncState *vs, uint8_t *data, size_t datalen)
13003e230dd2SCorentin Chary {
13013e230dd2SCorentin Chary     long ret;
13023e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS
13033e230dd2SCorentin Chary     if (vs->tls.session) {
13040057a0d5STim Hardeck         ret = vnc_client_read_tls(&vs->tls.session, data, datalen);
13050057a0d5STim Hardeck     } else {
13060057a0d5STim Hardeck #ifdef CONFIG_VNC_WS
13070057a0d5STim Hardeck         if (vs->ws_tls.session) {
13080057a0d5STim Hardeck             ret = vnc_client_read_tls(&vs->ws_tls.session, data, datalen);
13093e230dd2SCorentin Chary         } else
13100057a0d5STim Hardeck #endif /* CONFIG_VNC_WS */
13113e230dd2SCorentin Chary #endif /* CONFIG_VNC_TLS */
13120057a0d5STim Hardeck         {
131300aa0040SBlue Swirl             ret = qemu_recv(vs->csock, data, datalen, 0);
13140057a0d5STim Hardeck         }
13150057a0d5STim Hardeck #ifdef CONFIG_VNC_TLS
13160057a0d5STim Hardeck     }
13170057a0d5STim Hardeck #endif /* CONFIG_VNC_TLS */
13183e230dd2SCorentin Chary     VNC_DEBUG("Read wire %p %zd -> %ld\n", data, datalen, ret);
13193e230dd2SCorentin Chary     return vnc_client_io_error(vs, ret, socket_error());
13203e230dd2SCorentin Chary }
13213e230dd2SCorentin Chary 
13223e230dd2SCorentin Chary 
13233e230dd2SCorentin Chary /*
13243e230dd2SCorentin Chary  * Called to read data from the client socket to the input buffer,
13253e230dd2SCorentin Chary  * when not using any SASL SSF encryption layers. Will read as much
13263e230dd2SCorentin Chary  * data as possible without blocking.
13273e230dd2SCorentin Chary  *
13283e230dd2SCorentin Chary  * Returns the number of bytes read. Returns -1 on error, and
13293e230dd2SCorentin Chary  * disconnects the client socket.
13303e230dd2SCorentin Chary  */
13313e230dd2SCorentin Chary static long vnc_client_read_plain(VncState *vs)
13323e230dd2SCorentin Chary {
13333e230dd2SCorentin Chary     int ret;
13343e230dd2SCorentin Chary     VNC_DEBUG("Read plain %p size %zd offset %zd\n",
13353e230dd2SCorentin Chary               vs->input.buffer, vs->input.capacity, vs->input.offset);
13363e230dd2SCorentin Chary     buffer_reserve(&vs->input, 4096);
13373e230dd2SCorentin Chary     ret = vnc_client_read_buf(vs, buffer_end(&vs->input), 4096);
13383e230dd2SCorentin Chary     if (!ret)
13393e230dd2SCorentin Chary         return 0;
13403e230dd2SCorentin Chary     vs->input.offset += ret;
13413e230dd2SCorentin Chary     return ret;
13423e230dd2SCorentin Chary }
13433e230dd2SCorentin Chary 
1344175b2a6eSCorentin Chary static void vnc_jobs_bh(void *opaque)
1345175b2a6eSCorentin Chary {
1346175b2a6eSCorentin Chary     VncState *vs = opaque;
1347175b2a6eSCorentin Chary 
1348175b2a6eSCorentin Chary     vnc_jobs_consume_buffer(vs);
1349175b2a6eSCorentin Chary }
13503e230dd2SCorentin Chary 
13513e230dd2SCorentin Chary /*
13523e230dd2SCorentin Chary  * First function called whenever there is more data to be read from
13533e230dd2SCorentin Chary  * the client socket. Will delegate actual work according to whether
13543e230dd2SCorentin Chary  * SASL SSF layers are enabled (thus requiring decryption calls)
13553e230dd2SCorentin Chary  */
13563e230dd2SCorentin Chary void vnc_client_read(void *opaque)
13573e230dd2SCorentin Chary {
13583e230dd2SCorentin Chary     VncState *vs = opaque;
13593e230dd2SCorentin Chary     long ret;
13603e230dd2SCorentin Chary 
13613e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL
13623e230dd2SCorentin Chary     if (vs->sasl.conn && vs->sasl.runSSF)
13633e230dd2SCorentin Chary         ret = vnc_client_read_sasl(vs);
13643e230dd2SCorentin Chary     else
13653e230dd2SCorentin Chary #endif /* CONFIG_VNC_SASL */
13667536ee4bSTim Hardeck #ifdef CONFIG_VNC_WS
13677536ee4bSTim Hardeck         if (vs->encode_ws) {
13687536ee4bSTim Hardeck             ret = vnc_client_read_ws(vs);
13697536ee4bSTim Hardeck             if (ret == -1) {
13707536ee4bSTim Hardeck                 vnc_disconnect_start(vs);
13717536ee4bSTim Hardeck                 return;
13727536ee4bSTim Hardeck             } else if (ret == -2) {
13737536ee4bSTim Hardeck                 vnc_client_error(vs);
13747536ee4bSTim Hardeck                 return;
13757536ee4bSTim Hardeck             }
13767536ee4bSTim Hardeck         } else
13777536ee4bSTim Hardeck #endif /* CONFIG_VNC_WS */
13787536ee4bSTim Hardeck         {
13793e230dd2SCorentin Chary         ret = vnc_client_read_plain(vs);
13807536ee4bSTim Hardeck         }
13813e230dd2SCorentin Chary     if (!ret) {
13823e230dd2SCorentin Chary         if (vs->csock == -1)
13833e230dd2SCorentin Chary             vnc_disconnect_finish(vs);
13843e230dd2SCorentin Chary         return;
13853e230dd2SCorentin Chary     }
13863e230dd2SCorentin Chary 
13873e230dd2SCorentin Chary     while (vs->read_handler && vs->input.offset >= vs->read_handler_expect) {
13883e230dd2SCorentin Chary         size_t len = vs->read_handler_expect;
13893e230dd2SCorentin Chary         int ret;
13903e230dd2SCorentin Chary 
13913e230dd2SCorentin Chary         ret = vs->read_handler(vs, vs->input.buffer, len);
13923e230dd2SCorentin Chary         if (vs->csock == -1) {
13933e230dd2SCorentin Chary             vnc_disconnect_finish(vs);
13943e230dd2SCorentin Chary             return;
13953e230dd2SCorentin Chary         }
13963e230dd2SCorentin Chary 
13973e230dd2SCorentin Chary         if (!ret) {
139832ed2680STim Hardeck             buffer_advance(&vs->input, len);
13993e230dd2SCorentin Chary         } else {
14003e230dd2SCorentin Chary             vs->read_handler_expect = ret;
14013e230dd2SCorentin Chary         }
14023e230dd2SCorentin Chary     }
14033e230dd2SCorentin Chary }
14043e230dd2SCorentin Chary 
14053e230dd2SCorentin Chary void vnc_write(VncState *vs, const void *data, size_t len)
14063e230dd2SCorentin Chary {
14073e230dd2SCorentin Chary     buffer_reserve(&vs->output, len);
14083e230dd2SCorentin Chary 
14093e230dd2SCorentin Chary     if (vs->csock != -1 && buffer_empty(&vs->output)) {
14103e230dd2SCorentin Chary         qemu_set_fd_handler2(vs->csock, NULL, vnc_client_read, vnc_client_write, vs);
14113e230dd2SCorentin Chary     }
14123e230dd2SCorentin Chary 
14133e230dd2SCorentin Chary     buffer_append(&vs->output, data, len);
14143e230dd2SCorentin Chary }
14153e230dd2SCorentin Chary 
14163e230dd2SCorentin Chary void vnc_write_s32(VncState *vs, int32_t value)
14173e230dd2SCorentin Chary {
14183e230dd2SCorentin Chary     vnc_write_u32(vs, *(uint32_t *)&value);
14193e230dd2SCorentin Chary }
14203e230dd2SCorentin Chary 
14213e230dd2SCorentin Chary void vnc_write_u32(VncState *vs, uint32_t value)
14223e230dd2SCorentin Chary {
14233e230dd2SCorentin Chary     uint8_t buf[4];
14243e230dd2SCorentin Chary 
14253e230dd2SCorentin Chary     buf[0] = (value >> 24) & 0xFF;
14263e230dd2SCorentin Chary     buf[1] = (value >> 16) & 0xFF;
14273e230dd2SCorentin Chary     buf[2] = (value >>  8) & 0xFF;
14283e230dd2SCorentin Chary     buf[3] = value & 0xFF;
14293e230dd2SCorentin Chary 
14303e230dd2SCorentin Chary     vnc_write(vs, buf, 4);
14313e230dd2SCorentin Chary }
14323e230dd2SCorentin Chary 
14333e230dd2SCorentin Chary void vnc_write_u16(VncState *vs, uint16_t value)
14343e230dd2SCorentin Chary {
14353e230dd2SCorentin Chary     uint8_t buf[2];
14363e230dd2SCorentin Chary 
14373e230dd2SCorentin Chary     buf[0] = (value >> 8) & 0xFF;
14383e230dd2SCorentin Chary     buf[1] = value & 0xFF;
14393e230dd2SCorentin Chary 
14403e230dd2SCorentin Chary     vnc_write(vs, buf, 2);
14413e230dd2SCorentin Chary }
14423e230dd2SCorentin Chary 
14433e230dd2SCorentin Chary void vnc_write_u8(VncState *vs, uint8_t value)
14443e230dd2SCorentin Chary {
14453e230dd2SCorentin Chary     vnc_write(vs, (char *)&value, 1);
14463e230dd2SCorentin Chary }
14473e230dd2SCorentin Chary 
14483e230dd2SCorentin Chary void vnc_flush(VncState *vs)
14493e230dd2SCorentin Chary {
1450bd023f95SCorentin Chary     vnc_lock_output(vs);
14517536ee4bSTim Hardeck     if (vs->csock != -1 && (vs->output.offset
14527536ee4bSTim Hardeck #ifdef CONFIG_VNC_WS
14537536ee4bSTim Hardeck                 || vs->ws_output.offset
14547536ee4bSTim Hardeck #endif
14557536ee4bSTim Hardeck                 )) {
1456bd023f95SCorentin Chary         vnc_client_write_locked(vs);
1457bd023f95SCorentin Chary     }
1458bd023f95SCorentin Chary     vnc_unlock_output(vs);
14593e230dd2SCorentin Chary }
14603e230dd2SCorentin Chary 
146171a8cdecSBlue Swirl static uint8_t read_u8(uint8_t *data, size_t offset)
14623e230dd2SCorentin Chary {
14633e230dd2SCorentin Chary     return data[offset];
14643e230dd2SCorentin Chary }
14653e230dd2SCorentin Chary 
146671a8cdecSBlue Swirl static uint16_t read_u16(uint8_t *data, size_t offset)
14673e230dd2SCorentin Chary {
14683e230dd2SCorentin Chary     return ((data[offset] & 0xFF) << 8) | (data[offset + 1] & 0xFF);
14693e230dd2SCorentin Chary }
14703e230dd2SCorentin Chary 
147171a8cdecSBlue Swirl static int32_t read_s32(uint8_t *data, size_t offset)
14723e230dd2SCorentin Chary {
14733e230dd2SCorentin Chary     return (int32_t)((data[offset] << 24) | (data[offset + 1] << 16) |
14743e230dd2SCorentin Chary                      (data[offset + 2] << 8) | data[offset + 3]);
14753e230dd2SCorentin Chary }
14763e230dd2SCorentin Chary 
14773e230dd2SCorentin Chary uint32_t read_u32(uint8_t *data, size_t offset)
14783e230dd2SCorentin Chary {
14793e230dd2SCorentin Chary     return ((data[offset] << 24) | (data[offset + 1] << 16) |
14803e230dd2SCorentin Chary             (data[offset + 2] << 8) | data[offset + 3]);
14813e230dd2SCorentin Chary }
14823e230dd2SCorentin Chary 
14833e230dd2SCorentin Chary static void client_cut_text(VncState *vs, size_t len, uint8_t *text)
14843e230dd2SCorentin Chary {
14853e230dd2SCorentin Chary }
14863e230dd2SCorentin Chary 
14879e8dd451SJan Kiszka static void check_pointer_type_change(Notifier *notifier, void *data)
14883e230dd2SCorentin Chary {
14893e230dd2SCorentin Chary     VncState *vs = container_of(notifier, VncState, mouse_mode_notifier);
149014768ebaSGerd Hoffmann     int absolute = qemu_input_is_absolute();
14913e230dd2SCorentin Chary 
14923e230dd2SCorentin Chary     if (vnc_has_feature(vs, VNC_FEATURE_POINTER_TYPE_CHANGE) && vs->absolute != absolute) {
1493bd023f95SCorentin Chary         vnc_lock_output(vs);
14943e230dd2SCorentin Chary         vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE);
14953e230dd2SCorentin Chary         vnc_write_u8(vs, 0);
14963e230dd2SCorentin Chary         vnc_write_u16(vs, 1);
14973e230dd2SCorentin Chary         vnc_framebuffer_update(vs, absolute, 0,
1498d39fa6d8SGerd Hoffmann                                surface_width(vs->vd->ds),
1499d39fa6d8SGerd Hoffmann                                surface_height(vs->vd->ds),
15003e230dd2SCorentin Chary                                VNC_ENCODING_POINTER_TYPE_CHANGE);
1501bd023f95SCorentin Chary         vnc_unlock_output(vs);
15023e230dd2SCorentin Chary         vnc_flush(vs);
15033e230dd2SCorentin Chary     }
15043e230dd2SCorentin Chary     vs->absolute = absolute;
15053e230dd2SCorentin Chary }
15063e230dd2SCorentin Chary 
15073e230dd2SCorentin Chary static void pointer_event(VncState *vs, int button_mask, int x, int y)
15083e230dd2SCorentin Chary {
150914768ebaSGerd Hoffmann     static uint32_t bmap[INPUT_BUTTON_MAX] = {
151014768ebaSGerd Hoffmann         [INPUT_BUTTON_LEFT]       = 0x01,
151114768ebaSGerd Hoffmann         [INPUT_BUTTON_MIDDLE]     = 0x02,
151214768ebaSGerd Hoffmann         [INPUT_BUTTON_RIGHT]      = 0x04,
151314768ebaSGerd Hoffmann         [INPUT_BUTTON_WHEEL_UP]   = 0x08,
151414768ebaSGerd Hoffmann         [INPUT_BUTTON_WHEEL_DOWN] = 0x10,
151514768ebaSGerd Hoffmann     };
151614768ebaSGerd Hoffmann     QemuConsole *con = vs->vd->dcl.con;
1517d39fa6d8SGerd Hoffmann     int width = surface_width(vs->vd->ds);
1518d39fa6d8SGerd Hoffmann     int height = surface_height(vs->vd->ds);
15193e230dd2SCorentin Chary 
152014768ebaSGerd Hoffmann     if (vs->last_bmask != button_mask) {
152114768ebaSGerd Hoffmann         qemu_input_update_buttons(con, bmap, vs->last_bmask, button_mask);
152214768ebaSGerd Hoffmann         vs->last_bmask = button_mask;
152314768ebaSGerd Hoffmann     }
15243e230dd2SCorentin Chary 
15253e230dd2SCorentin Chary     if (vs->absolute) {
152614768ebaSGerd Hoffmann         qemu_input_queue_abs(con, INPUT_AXIS_X, x, width);
152714768ebaSGerd Hoffmann         qemu_input_queue_abs(con, INPUT_AXIS_Y, y, height);
15283e230dd2SCorentin Chary     } else if (vnc_has_feature(vs, VNC_FEATURE_POINTER_TYPE_CHANGE)) {
152914768ebaSGerd Hoffmann         qemu_input_queue_rel(con, INPUT_AXIS_X, x - 0x7FFF);
153014768ebaSGerd Hoffmann         qemu_input_queue_rel(con, INPUT_AXIS_Y, y - 0x7FFF);
15313e230dd2SCorentin Chary     } else {
153214768ebaSGerd Hoffmann         if (vs->last_x != -1) {
153314768ebaSGerd Hoffmann             qemu_input_queue_rel(con, INPUT_AXIS_X, x - vs->last_x);
153414768ebaSGerd Hoffmann             qemu_input_queue_rel(con, INPUT_AXIS_Y, y - vs->last_y);
153514768ebaSGerd Hoffmann         }
15363e230dd2SCorentin Chary         vs->last_x = x;
15373e230dd2SCorentin Chary         vs->last_y = y;
15383e230dd2SCorentin Chary     }
153914768ebaSGerd Hoffmann     qemu_input_event_sync();
15403e230dd2SCorentin Chary }
15413e230dd2SCorentin Chary 
15423e230dd2SCorentin Chary static void reset_keys(VncState *vs)
15433e230dd2SCorentin Chary {
15443e230dd2SCorentin Chary     int i;
15453e230dd2SCorentin Chary     for(i = 0; i < 256; i++) {
15463e230dd2SCorentin Chary         if (vs->modifiers_state[i]) {
15478d447d10SGerd Hoffmann             qemu_input_event_send_key_number(vs->vd->dcl.con, i, false);
15483e230dd2SCorentin Chary             vs->modifiers_state[i] = 0;
15493e230dd2SCorentin Chary         }
15503e230dd2SCorentin Chary     }
15513e230dd2SCorentin Chary }
15523e230dd2SCorentin Chary 
15533e230dd2SCorentin Chary static void press_key(VncState *vs, int keysym)
15543e230dd2SCorentin Chary {
15553e230dd2SCorentin Chary     int keycode = keysym2scancode(vs->vd->kbd_layout, keysym) & SCANCODE_KEYMASK;
15568d447d10SGerd Hoffmann     qemu_input_event_send_key_number(vs->vd->dcl.con, keycode, true);
15578d447d10SGerd Hoffmann     qemu_input_event_send_key_number(vs->vd->dcl.con, keycode, false);
15583e230dd2SCorentin Chary }
15593e230dd2SCorentin Chary 
1560ab99e5c1SLei Li static int current_led_state(VncState *vs)
1561ab99e5c1SLei Li {
1562ab99e5c1SLei Li     int ledstate = 0;
1563ab99e5c1SLei Li 
1564ab99e5c1SLei Li     if (vs->modifiers_state[0x46]) {
1565ab99e5c1SLei Li         ledstate |= QEMU_SCROLL_LOCK_LED;
1566ab99e5c1SLei Li     }
1567ab99e5c1SLei Li     if (vs->modifiers_state[0x45]) {
1568ab99e5c1SLei Li         ledstate |= QEMU_NUM_LOCK_LED;
1569ab99e5c1SLei Li     }
1570ab99e5c1SLei Li     if (vs->modifiers_state[0x3a]) {
1571ab99e5c1SLei Li         ledstate |= QEMU_CAPS_LOCK_LED;
1572ab99e5c1SLei Li     }
1573ab99e5c1SLei Li 
1574ab99e5c1SLei Li     return ledstate;
1575ab99e5c1SLei Li }
1576ab99e5c1SLei Li 
1577ab99e5c1SLei Li static void vnc_led_state_change(VncState *vs)
1578ab99e5c1SLei Li {
1579ab99e5c1SLei Li     int ledstate = 0;
1580ab99e5c1SLei Li 
1581ab99e5c1SLei Li     if (!vnc_has_feature(vs, VNC_FEATURE_LED_STATE)) {
1582ab99e5c1SLei Li         return;
1583ab99e5c1SLei Li     }
1584ab99e5c1SLei Li 
1585ab99e5c1SLei Li     ledstate = current_led_state(vs);
1586ab99e5c1SLei Li     vnc_lock_output(vs);
1587ab99e5c1SLei Li     vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE);
1588ab99e5c1SLei Li     vnc_write_u8(vs, 0);
1589ab99e5c1SLei Li     vnc_write_u16(vs, 1);
1590ab99e5c1SLei Li     vnc_framebuffer_update(vs, 0, 0, 1, 1, VNC_ENCODING_LED_STATE);
1591ab99e5c1SLei Li     vnc_write_u8(vs, ledstate);
1592ab99e5c1SLei Li     vnc_unlock_output(vs);
1593ab99e5c1SLei Li     vnc_flush(vs);
1594ab99e5c1SLei Li }
1595ab99e5c1SLei Li 
15963e230dd2SCorentin Chary static void kbd_leds(void *opaque, int ledstate)
15973e230dd2SCorentin Chary {
15983e230dd2SCorentin Chary     VncState *vs = opaque;
159996f3d174SLei Li     int caps, num, scr;
16001483adcfSLei Li     bool has_changed = (ledstate != current_led_state(vs));
16013e230dd2SCorentin Chary 
16023e230dd2SCorentin Chary     caps = ledstate & QEMU_CAPS_LOCK_LED ? 1 : 0;
16033e230dd2SCorentin Chary     num  = ledstate & QEMU_NUM_LOCK_LED  ? 1 : 0;
160496f3d174SLei Li     scr  = ledstate & QEMU_SCROLL_LOCK_LED ? 1 : 0;
16053e230dd2SCorentin Chary 
16063e230dd2SCorentin Chary     if (vs->modifiers_state[0x3a] != caps) {
16073e230dd2SCorentin Chary         vs->modifiers_state[0x3a] = caps;
16083e230dd2SCorentin Chary     }
16093e230dd2SCorentin Chary     if (vs->modifiers_state[0x45] != num) {
16103e230dd2SCorentin Chary         vs->modifiers_state[0x45] = num;
16113e230dd2SCorentin Chary     }
161296f3d174SLei Li     if (vs->modifiers_state[0x46] != scr) {
161396f3d174SLei Li         vs->modifiers_state[0x46] = scr;
161496f3d174SLei Li     }
1615ab99e5c1SLei Li 
1616ab99e5c1SLei Li     /* Sending the current led state message to the client */
16171483adcfSLei Li     if (has_changed) {
1618ab99e5c1SLei Li         vnc_led_state_change(vs);
1619ab99e5c1SLei Li     }
16203e230dd2SCorentin Chary }
16213e230dd2SCorentin Chary 
16223e230dd2SCorentin Chary static void do_key_event(VncState *vs, int down, int keycode, int sym)
16233e230dd2SCorentin Chary {
16243e230dd2SCorentin Chary     /* QEMU console switch */
16253e230dd2SCorentin Chary     switch(keycode) {
16263e230dd2SCorentin Chary     case 0x2a:                          /* Left Shift */
16273e230dd2SCorentin Chary     case 0x36:                          /* Right Shift */
16283e230dd2SCorentin Chary     case 0x1d:                          /* Left CTRL */
16293e230dd2SCorentin Chary     case 0x9d:                          /* Right CTRL */
16303e230dd2SCorentin Chary     case 0x38:                          /* Left ALT */
16313e230dd2SCorentin Chary     case 0xb8:                          /* Right ALT */
16323e230dd2SCorentin Chary         if (down)
16333e230dd2SCorentin Chary             vs->modifiers_state[keycode] = 1;
16343e230dd2SCorentin Chary         else
16353e230dd2SCorentin Chary             vs->modifiers_state[keycode] = 0;
16363e230dd2SCorentin Chary         break;
16373e230dd2SCorentin Chary     case 0x02 ... 0x0a: /* '1' to '9' keys */
16383e230dd2SCorentin Chary         if (down && vs->modifiers_state[0x1d] && vs->modifiers_state[0x38]) {
16393e230dd2SCorentin Chary             /* Reset the modifiers sent to the current console */
16403e230dd2SCorentin Chary             reset_keys(vs);
16413e230dd2SCorentin Chary             console_select(keycode - 0x02);
16423e230dd2SCorentin Chary             return;
16433e230dd2SCorentin Chary         }
16443e230dd2SCorentin Chary         break;
16453e230dd2SCorentin Chary     case 0x3a:                        /* CapsLock */
16463e230dd2SCorentin Chary     case 0x45:                        /* NumLock */
16473e230dd2SCorentin Chary         if (down)
16483e230dd2SCorentin Chary             vs->modifiers_state[keycode] ^= 1;
16493e230dd2SCorentin Chary         break;
16503e230dd2SCorentin Chary     }
16513e230dd2SCorentin Chary 
1652e7b2aaccSLei Li     /* Turn off the lock state sync logic if the client support the led
1653e7b2aaccSLei Li        state extension.
1654e7b2aaccSLei Li     */
16559892088bSGerd Hoffmann     if (down && vs->vd->lock_key_sync &&
1656e7b2aaccSLei Li         !vnc_has_feature(vs, VNC_FEATURE_LED_STATE) &&
16573e230dd2SCorentin Chary         keycode_is_keypad(vs->vd->kbd_layout, keycode)) {
16583e230dd2SCorentin Chary         /* If the numlock state needs to change then simulate an additional
16593e230dd2SCorentin Chary            keypress before sending this one.  This will happen if the user
16603e230dd2SCorentin Chary            toggles numlock away from the VNC window.
16613e230dd2SCorentin Chary         */
16623e230dd2SCorentin Chary         if (keysym_is_numlock(vs->vd->kbd_layout, sym & 0xFFFF)) {
16633e230dd2SCorentin Chary             if (!vs->modifiers_state[0x45]) {
16643e230dd2SCorentin Chary                 vs->modifiers_state[0x45] = 1;
16653e230dd2SCorentin Chary                 press_key(vs, 0xff7f);
16663e230dd2SCorentin Chary             }
16673e230dd2SCorentin Chary         } else {
16683e230dd2SCorentin Chary             if (vs->modifiers_state[0x45]) {
16693e230dd2SCorentin Chary                 vs->modifiers_state[0x45] = 0;
16703e230dd2SCorentin Chary                 press_key(vs, 0xff7f);
16713e230dd2SCorentin Chary             }
16723e230dd2SCorentin Chary         }
16733e230dd2SCorentin Chary     }
16743e230dd2SCorentin Chary 
16759892088bSGerd Hoffmann     if (down && vs->vd->lock_key_sync &&
1676e7b2aaccSLei Li         !vnc_has_feature(vs, VNC_FEATURE_LED_STATE) &&
16773e230dd2SCorentin Chary         ((sym >= 'A' && sym <= 'Z') || (sym >= 'a' && sym <= 'z'))) {
16783e230dd2SCorentin Chary         /* If the capslock state needs to change then simulate an additional
16793e230dd2SCorentin Chary            keypress before sending this one.  This will happen if the user
16803e230dd2SCorentin Chary            toggles capslock away from the VNC window.
16813e230dd2SCorentin Chary         */
16823e230dd2SCorentin Chary         int uppercase = !!(sym >= 'A' && sym <= 'Z');
16833e230dd2SCorentin Chary         int shift = !!(vs->modifiers_state[0x2a] | vs->modifiers_state[0x36]);
16843e230dd2SCorentin Chary         int capslock = !!(vs->modifiers_state[0x3a]);
16853e230dd2SCorentin Chary         if (capslock) {
16863e230dd2SCorentin Chary             if (uppercase == shift) {
16873e230dd2SCorentin Chary                 vs->modifiers_state[0x3a] = 0;
16883e230dd2SCorentin Chary                 press_key(vs, 0xffe5);
16893e230dd2SCorentin Chary             }
16903e230dd2SCorentin Chary         } else {
16913e230dd2SCorentin Chary             if (uppercase != shift) {
16923e230dd2SCorentin Chary                 vs->modifiers_state[0x3a] = 1;
16933e230dd2SCorentin Chary                 press_key(vs, 0xffe5);
16943e230dd2SCorentin Chary             }
16953e230dd2SCorentin Chary         }
16963e230dd2SCorentin Chary     }
16973e230dd2SCorentin Chary 
169881c0d5a6SGerd Hoffmann     if (qemu_console_is_graphic(NULL)) {
16998d447d10SGerd Hoffmann         qemu_input_event_send_key_number(vs->vd->dcl.con, keycode, down);
17003e230dd2SCorentin Chary     } else {
1701e26437c2SGerd Hoffmann         bool numlock = vs->modifiers_state[0x45];
1702e26437c2SGerd Hoffmann         bool control = (vs->modifiers_state[0x1d] ||
1703e26437c2SGerd Hoffmann                         vs->modifiers_state[0x9d]);
17043e230dd2SCorentin Chary         /* QEMU console emulation */
17053e230dd2SCorentin Chary         if (down) {
17063e230dd2SCorentin Chary             switch (keycode) {
17073e230dd2SCorentin Chary             case 0x2a:                          /* Left Shift */
17083e230dd2SCorentin Chary             case 0x36:                          /* Right Shift */
17093e230dd2SCorentin Chary             case 0x1d:                          /* Left CTRL */
17103e230dd2SCorentin Chary             case 0x9d:                          /* Right CTRL */
17113e230dd2SCorentin Chary             case 0x38:                          /* Left ALT */
17123e230dd2SCorentin Chary             case 0xb8:                          /* Right ALT */
17133e230dd2SCorentin Chary                 break;
17143e230dd2SCorentin Chary             case 0xc8:
17153e230dd2SCorentin Chary                 kbd_put_keysym(QEMU_KEY_UP);
17163e230dd2SCorentin Chary                 break;
17173e230dd2SCorentin Chary             case 0xd0:
17183e230dd2SCorentin Chary                 kbd_put_keysym(QEMU_KEY_DOWN);
17193e230dd2SCorentin Chary                 break;
17203e230dd2SCorentin Chary             case 0xcb:
17213e230dd2SCorentin Chary                 kbd_put_keysym(QEMU_KEY_LEFT);
17223e230dd2SCorentin Chary                 break;
17233e230dd2SCorentin Chary             case 0xcd:
17243e230dd2SCorentin Chary                 kbd_put_keysym(QEMU_KEY_RIGHT);
17253e230dd2SCorentin Chary                 break;
17263e230dd2SCorentin Chary             case 0xd3:
17273e230dd2SCorentin Chary                 kbd_put_keysym(QEMU_KEY_DELETE);
17283e230dd2SCorentin Chary                 break;
17293e230dd2SCorentin Chary             case 0xc7:
17303e230dd2SCorentin Chary                 kbd_put_keysym(QEMU_KEY_HOME);
17313e230dd2SCorentin Chary                 break;
17323e230dd2SCorentin Chary             case 0xcf:
17333e230dd2SCorentin Chary                 kbd_put_keysym(QEMU_KEY_END);
17343e230dd2SCorentin Chary                 break;
17353e230dd2SCorentin Chary             case 0xc9:
17363e230dd2SCorentin Chary                 kbd_put_keysym(QEMU_KEY_PAGEUP);
17373e230dd2SCorentin Chary                 break;
17383e230dd2SCorentin Chary             case 0xd1:
17393e230dd2SCorentin Chary                 kbd_put_keysym(QEMU_KEY_PAGEDOWN);
17403e230dd2SCorentin Chary                 break;
17413e230dd2SCorentin Chary 
17423e230dd2SCorentin Chary             case 0x47:
17433e230dd2SCorentin Chary                 kbd_put_keysym(numlock ? '7' : QEMU_KEY_HOME);
17443e230dd2SCorentin Chary                 break;
17453e230dd2SCorentin Chary             case 0x48:
17463e230dd2SCorentin Chary                 kbd_put_keysym(numlock ? '8' : QEMU_KEY_UP);
17473e230dd2SCorentin Chary                 break;
17483e230dd2SCorentin Chary             case 0x49:
17493e230dd2SCorentin Chary                 kbd_put_keysym(numlock ? '9' : QEMU_KEY_PAGEUP);
17503e230dd2SCorentin Chary                 break;
17513e230dd2SCorentin Chary             case 0x4b:
17523e230dd2SCorentin Chary                 kbd_put_keysym(numlock ? '4' : QEMU_KEY_LEFT);
17533e230dd2SCorentin Chary                 break;
17543e230dd2SCorentin Chary             case 0x4c:
17553e230dd2SCorentin Chary                 kbd_put_keysym('5');
17563e230dd2SCorentin Chary                 break;
17573e230dd2SCorentin Chary             case 0x4d:
17583e230dd2SCorentin Chary                 kbd_put_keysym(numlock ? '6' : QEMU_KEY_RIGHT);
17593e230dd2SCorentin Chary                 break;
17603e230dd2SCorentin Chary             case 0x4f:
17613e230dd2SCorentin Chary                 kbd_put_keysym(numlock ? '1' : QEMU_KEY_END);
17623e230dd2SCorentin Chary                 break;
17633e230dd2SCorentin Chary             case 0x50:
17643e230dd2SCorentin Chary                 kbd_put_keysym(numlock ? '2' : QEMU_KEY_DOWN);
17653e230dd2SCorentin Chary                 break;
17663e230dd2SCorentin Chary             case 0x51:
17673e230dd2SCorentin Chary                 kbd_put_keysym(numlock ? '3' : QEMU_KEY_PAGEDOWN);
17683e230dd2SCorentin Chary                 break;
17693e230dd2SCorentin Chary             case 0x52:
17703e230dd2SCorentin Chary                 kbd_put_keysym('0');
17713e230dd2SCorentin Chary                 break;
17723e230dd2SCorentin Chary             case 0x53:
17733e230dd2SCorentin Chary                 kbd_put_keysym(numlock ? '.' : QEMU_KEY_DELETE);
17743e230dd2SCorentin Chary                 break;
17753e230dd2SCorentin Chary 
17763e230dd2SCorentin Chary             case 0xb5:
17773e230dd2SCorentin Chary                 kbd_put_keysym('/');
17783e230dd2SCorentin Chary                 break;
17793e230dd2SCorentin Chary             case 0x37:
17803e230dd2SCorentin Chary                 kbd_put_keysym('*');
17813e230dd2SCorentin Chary                 break;
17823e230dd2SCorentin Chary             case 0x4a:
17833e230dd2SCorentin Chary                 kbd_put_keysym('-');
17843e230dd2SCorentin Chary                 break;
17853e230dd2SCorentin Chary             case 0x4e:
17863e230dd2SCorentin Chary                 kbd_put_keysym('+');
17873e230dd2SCorentin Chary                 break;
17883e230dd2SCorentin Chary             case 0x9c:
17893e230dd2SCorentin Chary                 kbd_put_keysym('\n');
17903e230dd2SCorentin Chary                 break;
17913e230dd2SCorentin Chary 
17923e230dd2SCorentin Chary             default:
1793e26437c2SGerd Hoffmann                 if (control) {
1794e26437c2SGerd Hoffmann                     kbd_put_keysym(sym & 0x1f);
1795e26437c2SGerd Hoffmann                 } else {
17963e230dd2SCorentin Chary                     kbd_put_keysym(sym);
1797e26437c2SGerd Hoffmann                 }
17983e230dd2SCorentin Chary                 break;
17993e230dd2SCorentin Chary             }
18003e230dd2SCorentin Chary         }
18013e230dd2SCorentin Chary     }
18023e230dd2SCorentin Chary }
18033e230dd2SCorentin Chary 
18047bc9318bSGerd Hoffmann static void vnc_release_modifiers(VncState *vs)
18057bc9318bSGerd Hoffmann {
18067bc9318bSGerd Hoffmann     static const int keycodes[] = {
18077bc9318bSGerd Hoffmann         /* shift, control, alt keys, both left & right */
18087bc9318bSGerd Hoffmann         0x2a, 0x36, 0x1d, 0x9d, 0x38, 0xb8,
18097bc9318bSGerd Hoffmann     };
18107bc9318bSGerd Hoffmann     int i, keycode;
18117bc9318bSGerd Hoffmann 
181281c0d5a6SGerd Hoffmann     if (!qemu_console_is_graphic(NULL)) {
18137bc9318bSGerd Hoffmann         return;
18147bc9318bSGerd Hoffmann     }
18157bc9318bSGerd Hoffmann     for (i = 0; i < ARRAY_SIZE(keycodes); i++) {
18167bc9318bSGerd Hoffmann         keycode = keycodes[i];
18177bc9318bSGerd Hoffmann         if (!vs->modifiers_state[keycode]) {
18187bc9318bSGerd Hoffmann             continue;
18197bc9318bSGerd Hoffmann         }
18208d447d10SGerd Hoffmann         qemu_input_event_send_key_number(vs->vd->dcl.con, keycode, false);
18217bc9318bSGerd Hoffmann     }
18227bc9318bSGerd Hoffmann }
18237bc9318bSGerd Hoffmann 
18243e230dd2SCorentin Chary static void key_event(VncState *vs, int down, uint32_t sym)
18253e230dd2SCorentin Chary {
18263e230dd2SCorentin Chary     int keycode;
18273e230dd2SCorentin Chary     int lsym = sym;
18283e230dd2SCorentin Chary 
182981c0d5a6SGerd Hoffmann     if (lsym >= 'A' && lsym <= 'Z' && qemu_console_is_graphic(NULL)) {
18303e230dd2SCorentin Chary         lsym = lsym - 'A' + 'a';
18313e230dd2SCorentin Chary     }
18323e230dd2SCorentin Chary 
18333e230dd2SCorentin Chary     keycode = keysym2scancode(vs->vd->kbd_layout, lsym & 0xFFFF) & SCANCODE_KEYMASK;
18343e230dd2SCorentin Chary     do_key_event(vs, down, keycode, sym);
18353e230dd2SCorentin Chary }
18363e230dd2SCorentin Chary 
18373e230dd2SCorentin Chary static void ext_key_event(VncState *vs, int down,
18383e230dd2SCorentin Chary                           uint32_t sym, uint16_t keycode)
18393e230dd2SCorentin Chary {
18403e230dd2SCorentin Chary     /* if the user specifies a keyboard layout, always use it */
18413e230dd2SCorentin Chary     if (keyboard_layout)
18423e230dd2SCorentin Chary         key_event(vs, down, sym);
18433e230dd2SCorentin Chary     else
18443e230dd2SCorentin Chary         do_key_event(vs, down, keycode, sym);
18453e230dd2SCorentin Chary }
18463e230dd2SCorentin Chary 
18473e230dd2SCorentin Chary static void framebuffer_update_request(VncState *vs, int incremental,
18483e230dd2SCorentin Chary                                        int x_position, int y_position,
18493e230dd2SCorentin Chary                                        int w, int h)
18503e230dd2SCorentin Chary {
18516ed391bfSWen Congyang     int i;
1852b4c85ddcSPeter Lieven     const size_t width = surface_width(vs->vd->ds) / VNC_DIRTY_PIXELS_PER_BIT;
1853d39fa6d8SGerd Hoffmann     const size_t height = surface_height(vs->vd->ds);
18546ed391bfSWen Congyang 
1855d39fa6d8SGerd Hoffmann     if (y_position > height) {
1856d39fa6d8SGerd Hoffmann         y_position = height;
1857d39fa6d8SGerd Hoffmann     }
1858d39fa6d8SGerd Hoffmann     if (y_position + h >= height) {
1859d39fa6d8SGerd Hoffmann         h = height - y_position;
1860d39fa6d8SGerd Hoffmann     }
18613e230dd2SCorentin Chary 
18623e230dd2SCorentin Chary     vs->need_update = 1;
18633e230dd2SCorentin Chary     if (!incremental) {
18643e230dd2SCorentin Chary         vs->force_update = 1;
18653e230dd2SCorentin Chary         for (i = 0; i < h; i++) {
18666ed391bfSWen Congyang             bitmap_set(vs->dirty[y_position + i], 0, width);
18676ed391bfSWen Congyang             bitmap_clear(vs->dirty[y_position + i], width,
1868a0843a68SAnthony Liguori                          VNC_DIRTY_BITS - width);
18693e230dd2SCorentin Chary         }
18703e230dd2SCorentin Chary     }
18713e230dd2SCorentin Chary }
18723e230dd2SCorentin Chary 
18733e230dd2SCorentin Chary static void send_ext_key_event_ack(VncState *vs)
18743e230dd2SCorentin Chary {
1875bd023f95SCorentin Chary     vnc_lock_output(vs);
18763e230dd2SCorentin Chary     vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE);
18773e230dd2SCorentin Chary     vnc_write_u8(vs, 0);
18783e230dd2SCorentin Chary     vnc_write_u16(vs, 1);
1879d39fa6d8SGerd Hoffmann     vnc_framebuffer_update(vs, 0, 0,
1880d39fa6d8SGerd Hoffmann                            surface_width(vs->vd->ds),
1881d39fa6d8SGerd Hoffmann                            surface_height(vs->vd->ds),
18823e230dd2SCorentin Chary                            VNC_ENCODING_EXT_KEY_EVENT);
1883bd023f95SCorentin Chary     vnc_unlock_output(vs);
18843e230dd2SCorentin Chary     vnc_flush(vs);
18853e230dd2SCorentin Chary }
18863e230dd2SCorentin Chary 
18873e230dd2SCorentin Chary static void send_ext_audio_ack(VncState *vs)
18883e230dd2SCorentin Chary {
1889bd023f95SCorentin Chary     vnc_lock_output(vs);
18903e230dd2SCorentin Chary     vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE);
18913e230dd2SCorentin Chary     vnc_write_u8(vs, 0);
18923e230dd2SCorentin Chary     vnc_write_u16(vs, 1);
1893d39fa6d8SGerd Hoffmann     vnc_framebuffer_update(vs, 0, 0,
1894d39fa6d8SGerd Hoffmann                            surface_width(vs->vd->ds),
1895d39fa6d8SGerd Hoffmann                            surface_height(vs->vd->ds),
18963e230dd2SCorentin Chary                            VNC_ENCODING_AUDIO);
1897bd023f95SCorentin Chary     vnc_unlock_output(vs);
18983e230dd2SCorentin Chary     vnc_flush(vs);
18993e230dd2SCorentin Chary }
19003e230dd2SCorentin Chary 
19013e230dd2SCorentin Chary static void set_encodings(VncState *vs, int32_t *encodings, size_t n_encodings)
19023e230dd2SCorentin Chary {
19033e230dd2SCorentin Chary     int i;
19043e230dd2SCorentin Chary     unsigned int enc = 0;
19053e230dd2SCorentin Chary 
19063e230dd2SCorentin Chary     vs->features = 0;
19073e230dd2SCorentin Chary     vs->vnc_encoding = 0;
1908d1af0e05SCorentin Chary     vs->tight.compression = 9;
1909d1af0e05SCorentin Chary     vs->tight.quality = -1; /* Lossless by default */
19103e230dd2SCorentin Chary     vs->absolute = -1;
19113e230dd2SCorentin Chary 
19123e230dd2SCorentin Chary     /*
19133e230dd2SCorentin Chary      * Start from the end because the encodings are sent in order of preference.
1914e5bed759SDong Xu Wang      * This way the preferred encoding (first encoding defined in the array)
19153e230dd2SCorentin Chary      * will be set at the end of the loop.
19163e230dd2SCorentin Chary      */
19173e230dd2SCorentin Chary     for (i = n_encodings - 1; i >= 0; i--) {
19183e230dd2SCorentin Chary         enc = encodings[i];
19193e230dd2SCorentin Chary         switch (enc) {
19203e230dd2SCorentin Chary         case VNC_ENCODING_RAW:
19213e230dd2SCorentin Chary             vs->vnc_encoding = enc;
19223e230dd2SCorentin Chary             break;
19233e230dd2SCorentin Chary         case VNC_ENCODING_COPYRECT:
19243e230dd2SCorentin Chary             vs->features |= VNC_FEATURE_COPYRECT_MASK;
19253e230dd2SCorentin Chary             break;
19263e230dd2SCorentin Chary         case VNC_ENCODING_HEXTILE:
19273e230dd2SCorentin Chary             vs->features |= VNC_FEATURE_HEXTILE_MASK;
19283e230dd2SCorentin Chary             vs->vnc_encoding = enc;
19293e230dd2SCorentin Chary             break;
19303e230dd2SCorentin Chary         case VNC_ENCODING_TIGHT:
19313e230dd2SCorentin Chary             vs->features |= VNC_FEATURE_TIGHT_MASK;
19323e230dd2SCorentin Chary             vs->vnc_encoding = enc;
19333e230dd2SCorentin Chary             break;
1934fe3e7f2dSJoel Martin #ifdef CONFIG_VNC_PNG
1935efe556adSCorentin Chary         case VNC_ENCODING_TIGHT_PNG:
1936efe556adSCorentin Chary             vs->features |= VNC_FEATURE_TIGHT_PNG_MASK;
1937efe556adSCorentin Chary             vs->vnc_encoding = enc;
1938efe556adSCorentin Chary             break;
1939fe3e7f2dSJoel Martin #endif
19403e230dd2SCorentin Chary         case VNC_ENCODING_ZLIB:
19413e230dd2SCorentin Chary             vs->features |= VNC_FEATURE_ZLIB_MASK;
19423e230dd2SCorentin Chary             vs->vnc_encoding = enc;
19433e230dd2SCorentin Chary             break;
1944148954faSCorentin Chary         case VNC_ENCODING_ZRLE:
1945148954faSCorentin Chary             vs->features |= VNC_FEATURE_ZRLE_MASK;
1946148954faSCorentin Chary             vs->vnc_encoding = enc;
1947148954faSCorentin Chary             break;
1948148954faSCorentin Chary         case VNC_ENCODING_ZYWRLE:
1949148954faSCorentin Chary             vs->features |= VNC_FEATURE_ZYWRLE_MASK;
1950148954faSCorentin Chary             vs->vnc_encoding = enc;
1951148954faSCorentin Chary             break;
19523e230dd2SCorentin Chary         case VNC_ENCODING_DESKTOPRESIZE:
19533e230dd2SCorentin Chary             vs->features |= VNC_FEATURE_RESIZE_MASK;
19543e230dd2SCorentin Chary             break;
19553e230dd2SCorentin Chary         case VNC_ENCODING_POINTER_TYPE_CHANGE:
19563e230dd2SCorentin Chary             vs->features |= VNC_FEATURE_POINTER_TYPE_CHANGE_MASK;
19573e230dd2SCorentin Chary             break;
19583e230dd2SCorentin Chary         case VNC_ENCODING_RICH_CURSOR:
19593e230dd2SCorentin Chary             vs->features |= VNC_FEATURE_RICH_CURSOR_MASK;
19603e230dd2SCorentin Chary             break;
19613e230dd2SCorentin Chary         case VNC_ENCODING_EXT_KEY_EVENT:
19623e230dd2SCorentin Chary             send_ext_key_event_ack(vs);
19633e230dd2SCorentin Chary             break;
19643e230dd2SCorentin Chary         case VNC_ENCODING_AUDIO:
19653e230dd2SCorentin Chary             send_ext_audio_ack(vs);
19663e230dd2SCorentin Chary             break;
19673e230dd2SCorentin Chary         case VNC_ENCODING_WMVi:
19683e230dd2SCorentin Chary             vs->features |= VNC_FEATURE_WMVI_MASK;
19693e230dd2SCorentin Chary             break;
1970ab99e5c1SLei Li         case VNC_ENCODING_LED_STATE:
1971ab99e5c1SLei Li             vs->features |= VNC_FEATURE_LED_STATE_MASK;
1972ab99e5c1SLei Li             break;
19733e230dd2SCorentin Chary         case VNC_ENCODING_COMPRESSLEVEL0 ... VNC_ENCODING_COMPRESSLEVEL0 + 9:
1974d1af0e05SCorentin Chary             vs->tight.compression = (enc & 0x0F);
19753e230dd2SCorentin Chary             break;
19763e230dd2SCorentin Chary         case VNC_ENCODING_QUALITYLEVEL0 ... VNC_ENCODING_QUALITYLEVEL0 + 9:
1977b31f519eSCorentin Chary             if (vs->vd->lossy) {
1978d1af0e05SCorentin Chary                 vs->tight.quality = (enc & 0x0F);
1979b31f519eSCorentin Chary             }
19803e230dd2SCorentin Chary             break;
19813e230dd2SCorentin Chary         default:
19823e230dd2SCorentin Chary             VNC_DEBUG("Unknown encoding: %d (0x%.8x): %d\n", i, enc, enc);
19833e230dd2SCorentin Chary             break;
19843e230dd2SCorentin Chary         }
19853e230dd2SCorentin Chary     }
19863e230dd2SCorentin Chary     vnc_desktop_resize(vs);
19879e8dd451SJan Kiszka     check_pointer_type_change(&vs->mouse_mode_notifier, NULL);
1988ab99e5c1SLei Li     vnc_led_state_change(vs);
19893e230dd2SCorentin Chary }
19903e230dd2SCorentin Chary 
19913e230dd2SCorentin Chary static void set_pixel_conversion(VncState *vs)
19923e230dd2SCorentin Chary {
19939f64916dSGerd Hoffmann     pixman_format_code_t fmt = qemu_pixman_get_format(&vs->client_pf);
19949f64916dSGerd Hoffmann 
19959f64916dSGerd Hoffmann     if (fmt == VNC_SERVER_FB_FORMAT) {
19963e230dd2SCorentin Chary         vs->write_pixels = vnc_write_pixels_copy;
19973e230dd2SCorentin Chary         vnc_hextile_set_pixel_conversion(vs, 0);
19983e230dd2SCorentin Chary     } else {
19993e230dd2SCorentin Chary         vs->write_pixels = vnc_write_pixels_generic;
20003e230dd2SCorentin Chary         vnc_hextile_set_pixel_conversion(vs, 1);
20013e230dd2SCorentin Chary     }
20023e230dd2SCorentin Chary }
20033e230dd2SCorentin Chary 
20043e230dd2SCorentin Chary static void set_pixel_format(VncState *vs,
20053e230dd2SCorentin Chary                              int bits_per_pixel, int depth,
20063e230dd2SCorentin Chary                              int big_endian_flag, int true_color_flag,
20073e230dd2SCorentin Chary                              int red_max, int green_max, int blue_max,
20083e230dd2SCorentin Chary                              int red_shift, int green_shift, int blue_shift)
20093e230dd2SCorentin Chary {
20103e230dd2SCorentin Chary     if (!true_color_flag) {
20113e230dd2SCorentin Chary         vnc_client_error(vs);
20123e230dd2SCorentin Chary         return;
20133e230dd2SCorentin Chary     }
20143e230dd2SCorentin Chary 
20159f64916dSGerd Hoffmann     vs->client_pf.rmax = red_max;
20169f64916dSGerd Hoffmann     vs->client_pf.rbits = hweight_long(red_max);
20179f64916dSGerd Hoffmann     vs->client_pf.rshift = red_shift;
20189f64916dSGerd Hoffmann     vs->client_pf.rmask = red_max << red_shift;
20199f64916dSGerd Hoffmann     vs->client_pf.gmax = green_max;
20209f64916dSGerd Hoffmann     vs->client_pf.gbits = hweight_long(green_max);
20219f64916dSGerd Hoffmann     vs->client_pf.gshift = green_shift;
20229f64916dSGerd Hoffmann     vs->client_pf.gmask = green_max << green_shift;
20239f64916dSGerd Hoffmann     vs->client_pf.bmax = blue_max;
20249f64916dSGerd Hoffmann     vs->client_pf.bbits = hweight_long(blue_max);
20259f64916dSGerd Hoffmann     vs->client_pf.bshift = blue_shift;
20269f64916dSGerd Hoffmann     vs->client_pf.bmask = blue_max << blue_shift;
20279f64916dSGerd Hoffmann     vs->client_pf.bits_per_pixel = bits_per_pixel;
20289f64916dSGerd Hoffmann     vs->client_pf.bytes_per_pixel = bits_per_pixel / 8;
20299f64916dSGerd Hoffmann     vs->client_pf.depth = bits_per_pixel == 32 ? 24 : bits_per_pixel;
20309f64916dSGerd Hoffmann     vs->client_be = big_endian_flag;
20313e230dd2SCorentin Chary 
20323e230dd2SCorentin Chary     set_pixel_conversion(vs);
20333e230dd2SCorentin Chary 
20341dbfa005SGerd Hoffmann     graphic_hw_invalidate(NULL);
20351dbfa005SGerd Hoffmann     graphic_hw_update(NULL);
20363e230dd2SCorentin Chary }
20373e230dd2SCorentin Chary 
20383e230dd2SCorentin Chary static void pixel_format_message (VncState *vs) {
20393e230dd2SCorentin Chary     char pad[3] = { 0, 0, 0 };
20403e230dd2SCorentin Chary 
20419f64916dSGerd Hoffmann     vs->client_pf = qemu_default_pixelformat(32);
20429f64916dSGerd Hoffmann 
20439f64916dSGerd Hoffmann     vnc_write_u8(vs, vs->client_pf.bits_per_pixel); /* bits-per-pixel */
20449f64916dSGerd Hoffmann     vnc_write_u8(vs, vs->client_pf.depth); /* depth */
20453e230dd2SCorentin Chary 
20463e230dd2SCorentin Chary #ifdef HOST_WORDS_BIGENDIAN
20473e230dd2SCorentin Chary     vnc_write_u8(vs, 1);             /* big-endian-flag */
20483e230dd2SCorentin Chary #else
20493e230dd2SCorentin Chary     vnc_write_u8(vs, 0);             /* big-endian-flag */
20503e230dd2SCorentin Chary #endif
20513e230dd2SCorentin Chary     vnc_write_u8(vs, 1);             /* true-color-flag */
20529f64916dSGerd Hoffmann     vnc_write_u16(vs, vs->client_pf.rmax);     /* red-max */
20539f64916dSGerd Hoffmann     vnc_write_u16(vs, vs->client_pf.gmax);     /* green-max */
20549f64916dSGerd Hoffmann     vnc_write_u16(vs, vs->client_pf.bmax);     /* blue-max */
20559f64916dSGerd Hoffmann     vnc_write_u8(vs, vs->client_pf.rshift);    /* red-shift */
20569f64916dSGerd Hoffmann     vnc_write_u8(vs, vs->client_pf.gshift);    /* green-shift */
20579f64916dSGerd Hoffmann     vnc_write_u8(vs, vs->client_pf.bshift);    /* blue-shift */
20589f64916dSGerd Hoffmann     vnc_write(vs, pad, 3);           /* padding */
20593e230dd2SCorentin Chary 
20603e230dd2SCorentin Chary     vnc_hextile_set_pixel_conversion(vs, 0);
20613e230dd2SCorentin Chary     vs->write_pixels = vnc_write_pixels_copy;
20623e230dd2SCorentin Chary }
20633e230dd2SCorentin Chary 
20643e230dd2SCorentin Chary static void vnc_colordepth(VncState *vs)
20653e230dd2SCorentin Chary {
20663e230dd2SCorentin Chary     if (vnc_has_feature(vs, VNC_FEATURE_WMVI)) {
20673e230dd2SCorentin Chary         /* Sending a WMVi message to notify the client*/
2068bd023f95SCorentin Chary         vnc_lock_output(vs);
20693e230dd2SCorentin Chary         vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE);
20703e230dd2SCorentin Chary         vnc_write_u8(vs, 0);
20713e230dd2SCorentin Chary         vnc_write_u16(vs, 1); /* number of rects */
2072d39fa6d8SGerd Hoffmann         vnc_framebuffer_update(vs, 0, 0,
2073d39fa6d8SGerd Hoffmann                                surface_width(vs->vd->ds),
2074d39fa6d8SGerd Hoffmann                                surface_height(vs->vd->ds),
2075d39fa6d8SGerd Hoffmann                                VNC_ENCODING_WMVi);
20763e230dd2SCorentin Chary         pixel_format_message(vs);
2077bd023f95SCorentin Chary         vnc_unlock_output(vs);
20783e230dd2SCorentin Chary         vnc_flush(vs);
20793e230dd2SCorentin Chary     } else {
20803e230dd2SCorentin Chary         set_pixel_conversion(vs);
20813e230dd2SCorentin Chary     }
20823e230dd2SCorentin Chary }
20833e230dd2SCorentin Chary 
20843e230dd2SCorentin Chary static int protocol_client_msg(VncState *vs, uint8_t *data, size_t len)
20853e230dd2SCorentin Chary {
20863e230dd2SCorentin Chary     int i;
20873e230dd2SCorentin Chary     uint16_t limit;
20883e230dd2SCorentin Chary     VncDisplay *vd = vs->vd;
20893e230dd2SCorentin Chary 
20903e230dd2SCorentin Chary     if (data[0] > 3) {
20910f7b2864SGerd Hoffmann         update_displaychangelistener(&vd->dcl, VNC_REFRESH_INTERVAL_BASE);
20923e230dd2SCorentin Chary     }
20933e230dd2SCorentin Chary 
20943e230dd2SCorentin Chary     switch (data[0]) {
20953e230dd2SCorentin Chary     case VNC_MSG_CLIENT_SET_PIXEL_FORMAT:
20963e230dd2SCorentin Chary         if (len == 1)
20973e230dd2SCorentin Chary             return 20;
20983e230dd2SCorentin Chary 
20993e230dd2SCorentin Chary         set_pixel_format(vs, read_u8(data, 4), read_u8(data, 5),
21003e230dd2SCorentin Chary                          read_u8(data, 6), read_u8(data, 7),
21013e230dd2SCorentin Chary                          read_u16(data, 8), read_u16(data, 10),
21023e230dd2SCorentin Chary                          read_u16(data, 12), read_u8(data, 14),
21033e230dd2SCorentin Chary                          read_u8(data, 15), read_u8(data, 16));
21043e230dd2SCorentin Chary         break;
21053e230dd2SCorentin Chary     case VNC_MSG_CLIENT_SET_ENCODINGS:
21063e230dd2SCorentin Chary         if (len == 1)
21073e230dd2SCorentin Chary             return 4;
21083e230dd2SCorentin Chary 
21093e230dd2SCorentin Chary         if (len == 4) {
21103e230dd2SCorentin Chary             limit = read_u16(data, 2);
21113e230dd2SCorentin Chary             if (limit > 0)
21123e230dd2SCorentin Chary                 return 4 + (limit * 4);
21133e230dd2SCorentin Chary         } else
21143e230dd2SCorentin Chary             limit = read_u16(data, 2);
21153e230dd2SCorentin Chary 
21163e230dd2SCorentin Chary         for (i = 0; i < limit; i++) {
21173e230dd2SCorentin Chary             int32_t val = read_s32(data, 4 + (i * 4));
21183e230dd2SCorentin Chary             memcpy(data + 4 + (i * 4), &val, sizeof(val));
21193e230dd2SCorentin Chary         }
21203e230dd2SCorentin Chary 
21213e230dd2SCorentin Chary         set_encodings(vs, (int32_t *)(data + 4), limit);
21223e230dd2SCorentin Chary         break;
21233e230dd2SCorentin Chary     case VNC_MSG_CLIENT_FRAMEBUFFER_UPDATE_REQUEST:
21243e230dd2SCorentin Chary         if (len == 1)
21253e230dd2SCorentin Chary             return 10;
21263e230dd2SCorentin Chary 
21273e230dd2SCorentin Chary         framebuffer_update_request(vs,
21283e230dd2SCorentin Chary                                    read_u8(data, 1), read_u16(data, 2), read_u16(data, 4),
21293e230dd2SCorentin Chary                                    read_u16(data, 6), read_u16(data, 8));
21303e230dd2SCorentin Chary         break;
21313e230dd2SCorentin Chary     case VNC_MSG_CLIENT_KEY_EVENT:
21323e230dd2SCorentin Chary         if (len == 1)
21333e230dd2SCorentin Chary             return 8;
21343e230dd2SCorentin Chary 
21353e230dd2SCorentin Chary         key_event(vs, read_u8(data, 1), read_u32(data, 4));
21363e230dd2SCorentin Chary         break;
21373e230dd2SCorentin Chary     case VNC_MSG_CLIENT_POINTER_EVENT:
21383e230dd2SCorentin Chary         if (len == 1)
21393e230dd2SCorentin Chary             return 6;
21403e230dd2SCorentin Chary 
21413e230dd2SCorentin Chary         pointer_event(vs, read_u8(data, 1), read_u16(data, 2), read_u16(data, 4));
21423e230dd2SCorentin Chary         break;
21433e230dd2SCorentin Chary     case VNC_MSG_CLIENT_CUT_TEXT:
21443e230dd2SCorentin Chary         if (len == 1)
21453e230dd2SCorentin Chary             return 8;
21463e230dd2SCorentin Chary 
21473e230dd2SCorentin Chary         if (len == 8) {
21483e230dd2SCorentin Chary             uint32_t dlen = read_u32(data, 4);
21493e230dd2SCorentin Chary             if (dlen > 0)
21503e230dd2SCorentin Chary                 return 8 + dlen;
21513e230dd2SCorentin Chary         }
21523e230dd2SCorentin Chary 
21533e230dd2SCorentin Chary         client_cut_text(vs, read_u32(data, 4), data + 8);
21543e230dd2SCorentin Chary         break;
21553e230dd2SCorentin Chary     case VNC_MSG_CLIENT_QEMU:
21563e230dd2SCorentin Chary         if (len == 1)
21573e230dd2SCorentin Chary             return 2;
21583e230dd2SCorentin Chary 
21593e230dd2SCorentin Chary         switch (read_u8(data, 1)) {
21603e230dd2SCorentin Chary         case VNC_MSG_CLIENT_QEMU_EXT_KEY_EVENT:
21613e230dd2SCorentin Chary             if (len == 2)
21623e230dd2SCorentin Chary                 return 12;
21633e230dd2SCorentin Chary 
21643e230dd2SCorentin Chary             ext_key_event(vs, read_u16(data, 2),
21653e230dd2SCorentin Chary                           read_u32(data, 4), read_u32(data, 8));
21663e230dd2SCorentin Chary             break;
21673e230dd2SCorentin Chary         case VNC_MSG_CLIENT_QEMU_AUDIO:
21683e230dd2SCorentin Chary             if (len == 2)
21693e230dd2SCorentin Chary                 return 4;
21703e230dd2SCorentin Chary 
21713e230dd2SCorentin Chary             switch (read_u16 (data, 2)) {
21723e230dd2SCorentin Chary             case VNC_MSG_CLIENT_QEMU_AUDIO_ENABLE:
21733e230dd2SCorentin Chary                 audio_add(vs);
21743e230dd2SCorentin Chary                 break;
21753e230dd2SCorentin Chary             case VNC_MSG_CLIENT_QEMU_AUDIO_DISABLE:
21763e230dd2SCorentin Chary                 audio_del(vs);
21773e230dd2SCorentin Chary                 break;
21783e230dd2SCorentin Chary             case VNC_MSG_CLIENT_QEMU_AUDIO_SET_FORMAT:
21793e230dd2SCorentin Chary                 if (len == 4)
21803e230dd2SCorentin Chary                     return 10;
21813e230dd2SCorentin Chary                 switch (read_u8(data, 4)) {
21823e230dd2SCorentin Chary                 case 0: vs->as.fmt = AUD_FMT_U8; break;
21833e230dd2SCorentin Chary                 case 1: vs->as.fmt = AUD_FMT_S8; break;
21843e230dd2SCorentin Chary                 case 2: vs->as.fmt = AUD_FMT_U16; break;
21853e230dd2SCorentin Chary                 case 3: vs->as.fmt = AUD_FMT_S16; break;
21863e230dd2SCorentin Chary                 case 4: vs->as.fmt = AUD_FMT_U32; break;
21873e230dd2SCorentin Chary                 case 5: vs->as.fmt = AUD_FMT_S32; break;
21883e230dd2SCorentin Chary                 default:
21893e230dd2SCorentin Chary                     printf("Invalid audio format %d\n", read_u8(data, 4));
21903e230dd2SCorentin Chary                     vnc_client_error(vs);
21913e230dd2SCorentin Chary                     break;
21923e230dd2SCorentin Chary                 }
21933e230dd2SCorentin Chary                 vs->as.nchannels = read_u8(data, 5);
21943e230dd2SCorentin Chary                 if (vs->as.nchannels != 1 && vs->as.nchannels != 2) {
21953e230dd2SCorentin Chary                     printf("Invalid audio channel coount %d\n",
21963e230dd2SCorentin Chary                            read_u8(data, 5));
21973e230dd2SCorentin Chary                     vnc_client_error(vs);
21983e230dd2SCorentin Chary                     break;
21993e230dd2SCorentin Chary                 }
22003e230dd2SCorentin Chary                 vs->as.freq = read_u32(data, 6);
22013e230dd2SCorentin Chary                 break;
22023e230dd2SCorentin Chary             default:
22033e230dd2SCorentin Chary                 printf ("Invalid audio message %d\n", read_u8(data, 4));
22043e230dd2SCorentin Chary                 vnc_client_error(vs);
22053e230dd2SCorentin Chary                 break;
22063e230dd2SCorentin Chary             }
22073e230dd2SCorentin Chary             break;
22083e230dd2SCorentin Chary 
22093e230dd2SCorentin Chary         default:
22103e230dd2SCorentin Chary             printf("Msg: %d\n", read_u16(data, 0));
22113e230dd2SCorentin Chary             vnc_client_error(vs);
22123e230dd2SCorentin Chary             break;
22133e230dd2SCorentin Chary         }
22143e230dd2SCorentin Chary         break;
22153e230dd2SCorentin Chary     default:
22163e230dd2SCorentin Chary         printf("Msg: %d\n", data[0]);
22173e230dd2SCorentin Chary         vnc_client_error(vs);
22183e230dd2SCorentin Chary         break;
22193e230dd2SCorentin Chary     }
22203e230dd2SCorentin Chary 
22213e230dd2SCorentin Chary     vnc_read_when(vs, protocol_client_msg, 1);
22223e230dd2SCorentin Chary     return 0;
22233e230dd2SCorentin Chary }
22243e230dd2SCorentin Chary 
22253e230dd2SCorentin Chary static int protocol_client_init(VncState *vs, uint8_t *data, size_t len)
22263e230dd2SCorentin Chary {
22273e230dd2SCorentin Chary     char buf[1024];
22288cf36489SGerd Hoffmann     VncShareMode mode;
22293e230dd2SCorentin Chary     int size;
22303e230dd2SCorentin Chary 
22318cf36489SGerd Hoffmann     mode = data[0] ? VNC_SHARE_MODE_SHARED : VNC_SHARE_MODE_EXCLUSIVE;
22328cf36489SGerd Hoffmann     switch (vs->vd->share_policy) {
22338cf36489SGerd Hoffmann     case VNC_SHARE_POLICY_IGNORE:
22348cf36489SGerd Hoffmann         /*
22358cf36489SGerd Hoffmann          * Ignore the shared flag.  Nothing to do here.
22368cf36489SGerd Hoffmann          *
22378cf36489SGerd Hoffmann          * Doesn't conform to the rfb spec but is traditional qemu
22388cf36489SGerd Hoffmann          * behavior, thus left here as option for compatibility
22398cf36489SGerd Hoffmann          * reasons.
22408cf36489SGerd Hoffmann          */
22418cf36489SGerd Hoffmann         break;
22428cf36489SGerd Hoffmann     case VNC_SHARE_POLICY_ALLOW_EXCLUSIVE:
22438cf36489SGerd Hoffmann         /*
22448cf36489SGerd Hoffmann          * Policy: Allow clients ask for exclusive access.
22458cf36489SGerd Hoffmann          *
22468cf36489SGerd Hoffmann          * Implementation: When a client asks for exclusive access,
22478cf36489SGerd Hoffmann          * disconnect all others. Shared connects are allowed as long
22488cf36489SGerd Hoffmann          * as no exclusive connection exists.
22498cf36489SGerd Hoffmann          *
22508cf36489SGerd Hoffmann          * This is how the rfb spec suggests to handle the shared flag.
22518cf36489SGerd Hoffmann          */
22528cf36489SGerd Hoffmann         if (mode == VNC_SHARE_MODE_EXCLUSIVE) {
22538cf36489SGerd Hoffmann             VncState *client;
22548cf36489SGerd Hoffmann             QTAILQ_FOREACH(client, &vs->vd->clients, next) {
22558cf36489SGerd Hoffmann                 if (vs == client) {
22568cf36489SGerd Hoffmann                     continue;
22578cf36489SGerd Hoffmann                 }
22588cf36489SGerd Hoffmann                 if (client->share_mode != VNC_SHARE_MODE_EXCLUSIVE &&
22598cf36489SGerd Hoffmann                     client->share_mode != VNC_SHARE_MODE_SHARED) {
22608cf36489SGerd Hoffmann                     continue;
22618cf36489SGerd Hoffmann                 }
22628cf36489SGerd Hoffmann                 vnc_disconnect_start(client);
22638cf36489SGerd Hoffmann             }
22648cf36489SGerd Hoffmann         }
22658cf36489SGerd Hoffmann         if (mode == VNC_SHARE_MODE_SHARED) {
22668cf36489SGerd Hoffmann             if (vs->vd->num_exclusive > 0) {
22678cf36489SGerd Hoffmann                 vnc_disconnect_start(vs);
22688cf36489SGerd Hoffmann                 return 0;
22698cf36489SGerd Hoffmann             }
22708cf36489SGerd Hoffmann         }
22718cf36489SGerd Hoffmann         break;
22728cf36489SGerd Hoffmann     case VNC_SHARE_POLICY_FORCE_SHARED:
22738cf36489SGerd Hoffmann         /*
22748cf36489SGerd Hoffmann          * Policy: Shared connects only.
22758cf36489SGerd Hoffmann          * Implementation: Disallow clients asking for exclusive access.
22768cf36489SGerd Hoffmann          *
22778cf36489SGerd Hoffmann          * Useful for shared desktop sessions where you don't want
22788cf36489SGerd Hoffmann          * someone forgetting to say -shared when running the vnc
22798cf36489SGerd Hoffmann          * client disconnect everybody else.
22808cf36489SGerd Hoffmann          */
22818cf36489SGerd Hoffmann         if (mode == VNC_SHARE_MODE_EXCLUSIVE) {
22828cf36489SGerd Hoffmann             vnc_disconnect_start(vs);
22838cf36489SGerd Hoffmann             return 0;
22848cf36489SGerd Hoffmann         }
22858cf36489SGerd Hoffmann         break;
22868cf36489SGerd Hoffmann     }
22878cf36489SGerd Hoffmann     vnc_set_share_mode(vs, mode);
22888cf36489SGerd Hoffmann 
2289d39fa6d8SGerd Hoffmann     vs->client_width = surface_width(vs->vd->ds);
2290d39fa6d8SGerd Hoffmann     vs->client_height = surface_height(vs->vd->ds);
22913e230dd2SCorentin Chary     vnc_write_u16(vs, vs->client_width);
22923e230dd2SCorentin Chary     vnc_write_u16(vs, vs->client_height);
22933e230dd2SCorentin Chary 
22943e230dd2SCorentin Chary     pixel_format_message(vs);
22953e230dd2SCorentin Chary 
22963e230dd2SCorentin Chary     if (qemu_name)
22973e230dd2SCorentin Chary         size = snprintf(buf, sizeof(buf), "QEMU (%s)", qemu_name);
22983e230dd2SCorentin Chary     else
22993e230dd2SCorentin Chary         size = snprintf(buf, sizeof(buf), "QEMU");
23003e230dd2SCorentin Chary 
23013e230dd2SCorentin Chary     vnc_write_u32(vs, size);
23023e230dd2SCorentin Chary     vnc_write(vs, buf, size);
23033e230dd2SCorentin Chary     vnc_flush(vs);
23043e230dd2SCorentin Chary 
23053e230dd2SCorentin Chary     vnc_client_cache_auth(vs);
23063e230dd2SCorentin Chary     vnc_qmp_event(vs, QEVENT_VNC_INITIALIZED);
23073e230dd2SCorentin Chary 
23083e230dd2SCorentin Chary     vnc_read_when(vs, protocol_client_msg, 1);
23093e230dd2SCorentin Chary 
23103e230dd2SCorentin Chary     return 0;
23113e230dd2SCorentin Chary }
23123e230dd2SCorentin Chary 
23133e230dd2SCorentin Chary void start_client_init(VncState *vs)
23143e230dd2SCorentin Chary {
23153e230dd2SCorentin Chary     vnc_read_when(vs, protocol_client_init, 1);
23163e230dd2SCorentin Chary }
23173e230dd2SCorentin Chary 
23183e230dd2SCorentin Chary static void make_challenge(VncState *vs)
23193e230dd2SCorentin Chary {
23203e230dd2SCorentin Chary     int i;
23213e230dd2SCorentin Chary 
23223e230dd2SCorentin Chary     srand(time(NULL)+getpid()+getpid()*987654+rand());
23233e230dd2SCorentin Chary 
23243e230dd2SCorentin Chary     for (i = 0 ; i < sizeof(vs->challenge) ; i++)
23253e230dd2SCorentin Chary         vs->challenge[i] = (int) (256.0*rand()/(RAND_MAX+1.0));
23263e230dd2SCorentin Chary }
23273e230dd2SCorentin Chary 
23283e230dd2SCorentin Chary static int protocol_client_auth_vnc(VncState *vs, uint8_t *data, size_t len)
23293e230dd2SCorentin Chary {
23303e230dd2SCorentin Chary     unsigned char response[VNC_AUTH_CHALLENGE_SIZE];
23313e230dd2SCorentin Chary     int i, j, pwlen;
23323e230dd2SCorentin Chary     unsigned char key[8];
23333c9405a0SGerd Hoffmann     time_t now = time(NULL);
23343e230dd2SCorentin Chary 
23351cd20f8bSAnthony Liguori     if (!vs->vd->password) {
23363e230dd2SCorentin Chary         VNC_DEBUG("No password configured on server");
23376bffdf0fSGerd Hoffmann         goto reject;
23383e230dd2SCorentin Chary     }
23393c9405a0SGerd Hoffmann     if (vs->vd->expires < now) {
23403c9405a0SGerd Hoffmann         VNC_DEBUG("Password is expired");
23413c9405a0SGerd Hoffmann         goto reject;
23423c9405a0SGerd Hoffmann     }
23433e230dd2SCorentin Chary 
23443e230dd2SCorentin Chary     memcpy(response, vs->challenge, VNC_AUTH_CHALLENGE_SIZE);
23453e230dd2SCorentin Chary 
23463e230dd2SCorentin Chary     /* Calculate the expected challenge response */
23473e230dd2SCorentin Chary     pwlen = strlen(vs->vd->password);
23483e230dd2SCorentin Chary     for (i=0; i<sizeof(key); i++)
23493e230dd2SCorentin Chary         key[i] = i<pwlen ? vs->vd->password[i] : 0;
23503e230dd2SCorentin Chary     deskey(key, EN0);
23513e230dd2SCorentin Chary     for (j = 0; j < VNC_AUTH_CHALLENGE_SIZE; j += 8)
23523e230dd2SCorentin Chary         des(response+j, response+j);
23533e230dd2SCorentin Chary 
23543e230dd2SCorentin Chary     /* Compare expected vs actual challenge response */
23553e230dd2SCorentin Chary     if (memcmp(response, data, VNC_AUTH_CHALLENGE_SIZE) != 0) {
2356e5bed759SDong Xu Wang         VNC_DEBUG("Client challenge response did not match\n");
23576bffdf0fSGerd Hoffmann         goto reject;
23586bffdf0fSGerd Hoffmann     } else {
23596bffdf0fSGerd Hoffmann         VNC_DEBUG("Accepting VNC challenge response\n");
23606bffdf0fSGerd Hoffmann         vnc_write_u32(vs, 0); /* Accept auth */
23616bffdf0fSGerd Hoffmann         vnc_flush(vs);
23626bffdf0fSGerd Hoffmann 
23636bffdf0fSGerd Hoffmann         start_client_init(vs);
23646bffdf0fSGerd Hoffmann     }
23656bffdf0fSGerd Hoffmann     return 0;
23666bffdf0fSGerd Hoffmann 
23676bffdf0fSGerd Hoffmann reject:
23683e230dd2SCorentin Chary     vnc_write_u32(vs, 1); /* Reject auth */
23693e230dd2SCorentin Chary     if (vs->minor >= 8) {
23703e230dd2SCorentin Chary         static const char err[] = "Authentication failed";
23713e230dd2SCorentin Chary         vnc_write_u32(vs, sizeof(err));
23723e230dd2SCorentin Chary         vnc_write(vs, err, sizeof(err));
23733e230dd2SCorentin Chary     }
23743e230dd2SCorentin Chary     vnc_flush(vs);
23753e230dd2SCorentin Chary     vnc_client_error(vs);
23763e230dd2SCorentin Chary     return 0;
23773e230dd2SCorentin Chary }
23783e230dd2SCorentin Chary 
23793e230dd2SCorentin Chary void start_auth_vnc(VncState *vs)
23803e230dd2SCorentin Chary {
23813e230dd2SCorentin Chary     make_challenge(vs);
23823e230dd2SCorentin Chary     /* Send client a 'random' challenge */
23833e230dd2SCorentin Chary     vnc_write(vs, vs->challenge, sizeof(vs->challenge));
23843e230dd2SCorentin Chary     vnc_flush(vs);
23853e230dd2SCorentin Chary 
23863e230dd2SCorentin Chary     vnc_read_when(vs, protocol_client_auth_vnc, sizeof(vs->challenge));
23873e230dd2SCorentin Chary }
23883e230dd2SCorentin Chary 
23893e230dd2SCorentin Chary 
23903e230dd2SCorentin Chary static int protocol_client_auth(VncState *vs, uint8_t *data, size_t len)
23913e230dd2SCorentin Chary {
23923e230dd2SCorentin Chary     /* We only advertise 1 auth scheme at a time, so client
23933e230dd2SCorentin Chary      * must pick the one we sent. Verify this */
23947e7e2ebcSDaniel P. Berrange     if (data[0] != vs->auth) { /* Reject auth */
23953e230dd2SCorentin Chary        VNC_DEBUG("Reject auth %d because it didn't match advertized\n", (int)data[0]);
23963e230dd2SCorentin Chary        vnc_write_u32(vs, 1);
23973e230dd2SCorentin Chary        if (vs->minor >= 8) {
23983e230dd2SCorentin Chary            static const char err[] = "Authentication failed";
23993e230dd2SCorentin Chary            vnc_write_u32(vs, sizeof(err));
24003e230dd2SCorentin Chary            vnc_write(vs, err, sizeof(err));
24013e230dd2SCorentin Chary        }
24023e230dd2SCorentin Chary        vnc_client_error(vs);
24033e230dd2SCorentin Chary     } else { /* Accept requested auth */
24043e230dd2SCorentin Chary        VNC_DEBUG("Client requested auth %d\n", (int)data[0]);
24057e7e2ebcSDaniel P. Berrange        switch (vs->auth) {
24063e230dd2SCorentin Chary        case VNC_AUTH_NONE:
24073e230dd2SCorentin Chary            VNC_DEBUG("Accept auth none\n");
24083e230dd2SCorentin Chary            if (vs->minor >= 8) {
24093e230dd2SCorentin Chary                vnc_write_u32(vs, 0); /* Accept auth completion */
24103e230dd2SCorentin Chary                vnc_flush(vs);
24113e230dd2SCorentin Chary            }
24123e230dd2SCorentin Chary            start_client_init(vs);
24133e230dd2SCorentin Chary            break;
24143e230dd2SCorentin Chary 
24153e230dd2SCorentin Chary        case VNC_AUTH_VNC:
24163e230dd2SCorentin Chary            VNC_DEBUG("Start VNC auth\n");
24173e230dd2SCorentin Chary            start_auth_vnc(vs);
24183e230dd2SCorentin Chary            break;
24193e230dd2SCorentin Chary 
24203e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS
24213e230dd2SCorentin Chary        case VNC_AUTH_VENCRYPT:
24223a93113aSDong Xu Wang            VNC_DEBUG("Accept VeNCrypt auth\n");
24233e230dd2SCorentin Chary            start_auth_vencrypt(vs);
24243e230dd2SCorentin Chary            break;
24253e230dd2SCorentin Chary #endif /* CONFIG_VNC_TLS */
24263e230dd2SCorentin Chary 
24273e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL
24283e230dd2SCorentin Chary        case VNC_AUTH_SASL:
24293e230dd2SCorentin Chary            VNC_DEBUG("Accept SASL auth\n");
24303e230dd2SCorentin Chary            start_auth_sasl(vs);
24313e230dd2SCorentin Chary            break;
24323e230dd2SCorentin Chary #endif /* CONFIG_VNC_SASL */
24333e230dd2SCorentin Chary 
24343e230dd2SCorentin Chary        default: /* Should not be possible, but just in case */
24357e7e2ebcSDaniel P. Berrange            VNC_DEBUG("Reject auth %d server code bug\n", vs->auth);
24363e230dd2SCorentin Chary            vnc_write_u8(vs, 1);
24373e230dd2SCorentin Chary            if (vs->minor >= 8) {
24383e230dd2SCorentin Chary                static const char err[] = "Authentication failed";
24393e230dd2SCorentin Chary                vnc_write_u32(vs, sizeof(err));
24403e230dd2SCorentin Chary                vnc_write(vs, err, sizeof(err));
24413e230dd2SCorentin Chary            }
24423e230dd2SCorentin Chary            vnc_client_error(vs);
24433e230dd2SCorentin Chary        }
24443e230dd2SCorentin Chary     }
24453e230dd2SCorentin Chary     return 0;
24463e230dd2SCorentin Chary }
24473e230dd2SCorentin Chary 
24483e230dd2SCorentin Chary static int protocol_version(VncState *vs, uint8_t *version, size_t len)
24493e230dd2SCorentin Chary {
24503e230dd2SCorentin Chary     char local[13];
24513e230dd2SCorentin Chary 
24523e230dd2SCorentin Chary     memcpy(local, version, 12);
24533e230dd2SCorentin Chary     local[12] = 0;
24543e230dd2SCorentin Chary 
24553e230dd2SCorentin Chary     if (sscanf(local, "RFB %03d.%03d\n", &vs->major, &vs->minor) != 2) {
24563e230dd2SCorentin Chary         VNC_DEBUG("Malformed protocol version %s\n", local);
24573e230dd2SCorentin Chary         vnc_client_error(vs);
24583e230dd2SCorentin Chary         return 0;
24593e230dd2SCorentin Chary     }
24603e230dd2SCorentin Chary     VNC_DEBUG("Client request protocol version %d.%d\n", vs->major, vs->minor);
24613e230dd2SCorentin Chary     if (vs->major != 3 ||
24623e230dd2SCorentin Chary         (vs->minor != 3 &&
24633e230dd2SCorentin Chary          vs->minor != 4 &&
24643e230dd2SCorentin Chary          vs->minor != 5 &&
24653e230dd2SCorentin Chary          vs->minor != 7 &&
24663e230dd2SCorentin Chary          vs->minor != 8)) {
24673e230dd2SCorentin Chary         VNC_DEBUG("Unsupported client version\n");
24683e230dd2SCorentin Chary         vnc_write_u32(vs, VNC_AUTH_INVALID);
24693e230dd2SCorentin Chary         vnc_flush(vs);
24703e230dd2SCorentin Chary         vnc_client_error(vs);
24713e230dd2SCorentin Chary         return 0;
24723e230dd2SCorentin Chary     }
24733e230dd2SCorentin Chary     /* Some broken clients report v3.4 or v3.5, which spec requires to be treated
24743e230dd2SCorentin Chary      * as equivalent to v3.3 by servers
24753e230dd2SCorentin Chary      */
24763e230dd2SCorentin Chary     if (vs->minor == 4 || vs->minor == 5)
24773e230dd2SCorentin Chary         vs->minor = 3;
24783e230dd2SCorentin Chary 
24793e230dd2SCorentin Chary     if (vs->minor == 3) {
24807e7e2ebcSDaniel P. Berrange         if (vs->auth == VNC_AUTH_NONE) {
24813e230dd2SCorentin Chary             VNC_DEBUG("Tell client auth none\n");
24827e7e2ebcSDaniel P. Berrange             vnc_write_u32(vs, vs->auth);
24833e230dd2SCorentin Chary             vnc_flush(vs);
24843e230dd2SCorentin Chary             start_client_init(vs);
24857e7e2ebcSDaniel P. Berrange        } else if (vs->auth == VNC_AUTH_VNC) {
24863e230dd2SCorentin Chary             VNC_DEBUG("Tell client VNC auth\n");
24877e7e2ebcSDaniel P. Berrange             vnc_write_u32(vs, vs->auth);
24883e230dd2SCorentin Chary             vnc_flush(vs);
24893e230dd2SCorentin Chary             start_auth_vnc(vs);
24903e230dd2SCorentin Chary        } else {
24917e7e2ebcSDaniel P. Berrange             VNC_DEBUG("Unsupported auth %d for protocol 3.3\n", vs->auth);
24923e230dd2SCorentin Chary             vnc_write_u32(vs, VNC_AUTH_INVALID);
24933e230dd2SCorentin Chary             vnc_flush(vs);
24943e230dd2SCorentin Chary             vnc_client_error(vs);
24953e230dd2SCorentin Chary        }
24963e230dd2SCorentin Chary     } else {
24977e7e2ebcSDaniel P. Berrange         VNC_DEBUG("Telling client we support auth %d\n", vs->auth);
24983e230dd2SCorentin Chary         vnc_write_u8(vs, 1); /* num auth */
24997e7e2ebcSDaniel P. Berrange         vnc_write_u8(vs, vs->auth);
25003e230dd2SCorentin Chary         vnc_read_when(vs, protocol_client_auth, 1);
25013e230dd2SCorentin Chary         vnc_flush(vs);
25023e230dd2SCorentin Chary     }
25033e230dd2SCorentin Chary 
25043e230dd2SCorentin Chary     return 0;
25053e230dd2SCorentin Chary }
25063e230dd2SCorentin Chary 
2507999342a0SCorentin Chary static VncRectStat *vnc_stat_rect(VncDisplay *vd, int x, int y)
2508999342a0SCorentin Chary {
2509999342a0SCorentin Chary     struct VncSurface *vs = &vd->guest;
2510999342a0SCorentin Chary 
2511999342a0SCorentin Chary     return &vs->stats[y / VNC_STAT_RECT][x / VNC_STAT_RECT];
2512999342a0SCorentin Chary }
2513999342a0SCorentin Chary 
25147d964c9dSCorentin Chary void vnc_sent_lossy_rect(VncState *vs, int x, int y, int w, int h)
25157d964c9dSCorentin Chary {
25167d964c9dSCorentin Chary     int i, j;
25177d964c9dSCorentin Chary 
25187d964c9dSCorentin Chary     w = (x + w) / VNC_STAT_RECT;
25197d964c9dSCorentin Chary     h = (y + h) / VNC_STAT_RECT;
25207d964c9dSCorentin Chary     x /= VNC_STAT_RECT;
25217d964c9dSCorentin Chary     y /= VNC_STAT_RECT;
25227d964c9dSCorentin Chary 
2523207f328aSCorentin Chary     for (j = y; j <= h; j++) {
2524207f328aSCorentin Chary         for (i = x; i <= w; i++) {
25257d964c9dSCorentin Chary             vs->lossy_rect[j][i] = 1;
25267d964c9dSCorentin Chary         }
25277d964c9dSCorentin Chary     }
25287d964c9dSCorentin Chary }
25297d964c9dSCorentin Chary 
25307d964c9dSCorentin Chary static int vnc_refresh_lossy_rect(VncDisplay *vd, int x, int y)
25317d964c9dSCorentin Chary {
25327d964c9dSCorentin Chary     VncState *vs;
25337d964c9dSCorentin Chary     int sty = y / VNC_STAT_RECT;
25347d964c9dSCorentin Chary     int stx = x / VNC_STAT_RECT;
25357d964c9dSCorentin Chary     int has_dirty = 0;
25367d964c9dSCorentin Chary 
25377d964c9dSCorentin Chary     y = y / VNC_STAT_RECT * VNC_STAT_RECT;
25387d964c9dSCorentin Chary     x = x / VNC_STAT_RECT * VNC_STAT_RECT;
25397d964c9dSCorentin Chary 
25407d964c9dSCorentin Chary     QTAILQ_FOREACH(vs, &vd->clients, next) {
2541bc2429b9SCorentin Chary         int j;
25427d964c9dSCorentin Chary 
25437d964c9dSCorentin Chary         /* kernel send buffers are full -> refresh later */
25447d964c9dSCorentin Chary         if (vs->output.offset) {
25457d964c9dSCorentin Chary             continue;
25467d964c9dSCorentin Chary         }
25477d964c9dSCorentin Chary 
25487d964c9dSCorentin Chary         if (!vs->lossy_rect[sty][stx]) {
25497d964c9dSCorentin Chary             continue;
25507d964c9dSCorentin Chary         }
2551207f328aSCorentin Chary 
25527d964c9dSCorentin Chary         vs->lossy_rect[sty][stx] = 0;
25537d964c9dSCorentin Chary         for (j = 0; j < VNC_STAT_RECT; ++j) {
2554b4c85ddcSPeter Lieven             bitmap_set(vs->dirty[y + j],
2555b4c85ddcSPeter Lieven                        x / VNC_DIRTY_PIXELS_PER_BIT,
2556b4c85ddcSPeter Lieven                        VNC_STAT_RECT / VNC_DIRTY_PIXELS_PER_BIT);
25577d964c9dSCorentin Chary         }
25587d964c9dSCorentin Chary         has_dirty++;
25597d964c9dSCorentin Chary     }
2560207f328aSCorentin Chary 
25617d964c9dSCorentin Chary     return has_dirty;
25627d964c9dSCorentin Chary }
25637d964c9dSCorentin Chary 
25647d964c9dSCorentin Chary static int vnc_update_stats(VncDisplay *vd,  struct timeval * tv)
2565999342a0SCorentin Chary {
25669f64916dSGerd Hoffmann     int width = pixman_image_get_width(vd->guest.fb);
25679f64916dSGerd Hoffmann     int height = pixman_image_get_height(vd->guest.fb);
2568999342a0SCorentin Chary     int x, y;
2569999342a0SCorentin Chary     struct timeval res;
25707d964c9dSCorentin Chary     int has_dirty = 0;
2571999342a0SCorentin Chary 
25729f64916dSGerd Hoffmann     for (y = 0; y < height; y += VNC_STAT_RECT) {
25739f64916dSGerd Hoffmann         for (x = 0; x < width; x += VNC_STAT_RECT) {
2574999342a0SCorentin Chary             VncRectStat *rect = vnc_stat_rect(vd, x, y);
2575999342a0SCorentin Chary 
2576999342a0SCorentin Chary             rect->updated = false;
2577999342a0SCorentin Chary         }
2578999342a0SCorentin Chary     }
2579999342a0SCorentin Chary 
2580ad620c29SBlue Swirl     qemu_timersub(tv, &VNC_REFRESH_STATS, &res);
2581999342a0SCorentin Chary 
2582999342a0SCorentin Chary     if (timercmp(&vd->guest.last_freq_check, &res, >)) {
25837d964c9dSCorentin Chary         return has_dirty;
2584999342a0SCorentin Chary     }
2585999342a0SCorentin Chary     vd->guest.last_freq_check = *tv;
2586999342a0SCorentin Chary 
25879f64916dSGerd Hoffmann     for (y = 0; y < height; y += VNC_STAT_RECT) {
25889f64916dSGerd Hoffmann         for (x = 0; x < width; x += VNC_STAT_RECT) {
2589999342a0SCorentin Chary             VncRectStat *rect= vnc_stat_rect(vd, x, y);
2590999342a0SCorentin Chary             int count = ARRAY_SIZE(rect->times);
2591999342a0SCorentin Chary             struct timeval min, max;
2592999342a0SCorentin Chary 
2593999342a0SCorentin Chary             if (!timerisset(&rect->times[count - 1])) {
2594999342a0SCorentin Chary                 continue ;
2595999342a0SCorentin Chary             }
2596999342a0SCorentin Chary 
2597999342a0SCorentin Chary             max = rect->times[(rect->idx + count - 1) % count];
2598ad620c29SBlue Swirl             qemu_timersub(tv, &max, &res);
2599999342a0SCorentin Chary 
2600999342a0SCorentin Chary             if (timercmp(&res, &VNC_REFRESH_LOSSY, >)) {
2601999342a0SCorentin Chary                 rect->freq = 0;
26027d964c9dSCorentin Chary                 has_dirty += vnc_refresh_lossy_rect(vd, x, y);
2603999342a0SCorentin Chary                 memset(rect->times, 0, sizeof (rect->times));
2604999342a0SCorentin Chary                 continue ;
2605999342a0SCorentin Chary             }
2606999342a0SCorentin Chary 
2607999342a0SCorentin Chary             min = rect->times[rect->idx];
2608999342a0SCorentin Chary             max = rect->times[(rect->idx + count - 1) % count];
2609ad620c29SBlue Swirl             qemu_timersub(&max, &min, &res);
2610999342a0SCorentin Chary 
2611999342a0SCorentin Chary             rect->freq = res.tv_sec + res.tv_usec / 1000000.;
2612999342a0SCorentin Chary             rect->freq /= count;
2613999342a0SCorentin Chary             rect->freq = 1. / rect->freq;
2614999342a0SCorentin Chary         }
2615999342a0SCorentin Chary     }
26167d964c9dSCorentin Chary     return has_dirty;
2617999342a0SCorentin Chary }
2618999342a0SCorentin Chary 
2619999342a0SCorentin Chary double vnc_update_freq(VncState *vs, int x, int y, int w, int h)
2620999342a0SCorentin Chary {
2621999342a0SCorentin Chary     int i, j;
2622999342a0SCorentin Chary     double total = 0;
2623999342a0SCorentin Chary     int num = 0;
2624999342a0SCorentin Chary 
2625999342a0SCorentin Chary     x =  (x / VNC_STAT_RECT) * VNC_STAT_RECT;
2626999342a0SCorentin Chary     y =  (y / VNC_STAT_RECT) * VNC_STAT_RECT;
2627999342a0SCorentin Chary 
2628999342a0SCorentin Chary     for (j = y; j <= y + h; j += VNC_STAT_RECT) {
2629999342a0SCorentin Chary         for (i = x; i <= x + w; i += VNC_STAT_RECT) {
2630999342a0SCorentin Chary             total += vnc_stat_rect(vs->vd, i, j)->freq;
2631999342a0SCorentin Chary             num++;
2632999342a0SCorentin Chary         }
2633999342a0SCorentin Chary     }
2634999342a0SCorentin Chary 
2635999342a0SCorentin Chary     if (num) {
2636999342a0SCorentin Chary         return total / num;
2637999342a0SCorentin Chary     } else {
2638999342a0SCorentin Chary         return 0;
2639999342a0SCorentin Chary     }
2640999342a0SCorentin Chary }
2641999342a0SCorentin Chary 
2642999342a0SCorentin Chary static void vnc_rect_updated(VncDisplay *vd, int x, int y, struct timeval * tv)
2643999342a0SCorentin Chary {
2644999342a0SCorentin Chary     VncRectStat *rect;
2645999342a0SCorentin Chary 
2646999342a0SCorentin Chary     rect = vnc_stat_rect(vd, x, y);
2647999342a0SCorentin Chary     if (rect->updated) {
2648999342a0SCorentin Chary         return ;
2649999342a0SCorentin Chary     }
2650999342a0SCorentin Chary     rect->times[rect->idx] = *tv;
2651999342a0SCorentin Chary     rect->idx = (rect->idx + 1) % ARRAY_SIZE(rect->times);
2652999342a0SCorentin Chary     rect->updated = true;
2653999342a0SCorentin Chary }
2654999342a0SCorentin Chary 
26553e230dd2SCorentin Chary static int vnc_refresh_server_surface(VncDisplay *vd)
26563e230dd2SCorentin Chary {
26579f64916dSGerd Hoffmann     int width = pixman_image_get_width(vd->guest.fb);
26589f64916dSGerd Hoffmann     int height = pixman_image_get_height(vd->guest.fb);
26593e230dd2SCorentin Chary     int y;
266012b316d4SPeter Lieven     uint8_t *guest_row0 = NULL, *server_row0;
266112b316d4SPeter Lieven     int guest_stride = 0, server_stride;
26623e230dd2SCorentin Chary     int cmp_bytes;
26633e230dd2SCorentin Chary     VncState *vs;
26643e230dd2SCorentin Chary     int has_dirty = 0;
26659f64916dSGerd Hoffmann     pixman_image_t *tmpbuf = NULL;
26663e230dd2SCorentin Chary 
266780e0c8c3SCorentin Chary     struct timeval tv = { 0, 0 };
2668999342a0SCorentin Chary 
266980e0c8c3SCorentin Chary     if (!vd->non_adaptive) {
2670999342a0SCorentin Chary         gettimeofday(&tv, NULL);
26717d964c9dSCorentin Chary         has_dirty = vnc_update_stats(vd, &tv);
267280e0c8c3SCorentin Chary     }
2673999342a0SCorentin Chary 
26743e230dd2SCorentin Chary     /*
26753e230dd2SCorentin Chary      * Walk through the guest dirty map.
26763e230dd2SCorentin Chary      * Check and copy modified bits from guest to server surface.
26773e230dd2SCorentin Chary      * Update server dirty map.
26783e230dd2SCorentin Chary      */
26796cd859aaSPeter Lieven     cmp_bytes = VNC_DIRTY_PIXELS_PER_BIT * VNC_SERVER_FB_BYTES;
26809f64916dSGerd Hoffmann     if (cmp_bytes > vnc_server_fb_stride(vd)) {
26819f64916dSGerd Hoffmann         cmp_bytes = vnc_server_fb_stride(vd);
26829e4dd565SStefan Weil     }
26839f64916dSGerd Hoffmann     if (vd->guest.format != VNC_SERVER_FB_FORMAT) {
26849f64916dSGerd Hoffmann         int width = pixman_image_get_width(vd->server);
26859f64916dSGerd Hoffmann         tmpbuf = qemu_pixman_linebuf_create(VNC_SERVER_FB_FORMAT, width);
268612b316d4SPeter Lieven     } else {
268712b316d4SPeter Lieven         guest_row0 = (uint8_t *)pixman_image_get_data(vd->guest.fb);
268812b316d4SPeter Lieven         guest_stride = pixman_image_get_stride(vd->guest.fb);
26899f64916dSGerd Hoffmann     }
269012b316d4SPeter Lieven     server_row0 = (uint8_t *)pixman_image_get_data(vd->server);
269112b316d4SPeter Lieven     server_stride = pixman_image_get_stride(vd->server);
269212b316d4SPeter Lieven 
269312b316d4SPeter Lieven     y = 0;
269412b316d4SPeter Lieven     for (;;) {
26953e230dd2SCorentin Chary         int x;
269612b316d4SPeter Lieven         uint8_t *guest_ptr, *server_ptr;
269712b316d4SPeter Lieven         unsigned long offset = find_next_bit((unsigned long *) &vd->guest.dirty,
269812b316d4SPeter Lieven                                              height * VNC_DIRTY_BPL(&vd->guest),
269912b316d4SPeter Lieven                                              y * VNC_DIRTY_BPL(&vd->guest));
270012b316d4SPeter Lieven         if (offset == height * VNC_DIRTY_BPL(&vd->guest)) {
270112b316d4SPeter Lieven             /* no more dirty bits */
270212b316d4SPeter Lieven             break;
270312b316d4SPeter Lieven         }
270412b316d4SPeter Lieven         y = offset / VNC_DIRTY_BPL(&vd->guest);
270512b316d4SPeter Lieven         x = offset % VNC_DIRTY_BPL(&vd->guest);
270612b316d4SPeter Lieven 
270712b316d4SPeter Lieven         server_ptr = server_row0 + y * server_stride + x * cmp_bytes;
27083e230dd2SCorentin Chary 
27099f64916dSGerd Hoffmann         if (vd->guest.format != VNC_SERVER_FB_FORMAT) {
2710bc210eb1SGerd Hoffmann             qemu_pixman_linebuf_fill(tmpbuf, vd->guest.fb, width, 0, y);
27119f64916dSGerd Hoffmann             guest_ptr = (uint8_t *)pixman_image_get_data(tmpbuf);
27129f64916dSGerd Hoffmann         } else {
271312b316d4SPeter Lieven             guest_ptr = guest_row0 + y * guest_stride;
27149f64916dSGerd Hoffmann         }
271512b316d4SPeter Lieven         guest_ptr += x * cmp_bytes;
27163e230dd2SCorentin Chary 
271712b316d4SPeter Lieven         for (; x < DIV_ROUND_UP(width, VNC_DIRTY_PIXELS_PER_BIT);
271812b316d4SPeter Lieven              x++, guest_ptr += cmp_bytes, server_ptr += cmp_bytes) {
271912b316d4SPeter Lieven             if (!test_and_clear_bit(x, vd->guest.dirty[y])) {
27203e230dd2SCorentin Chary                 continue;
2721b4c85ddcSPeter Lieven             }
2722b4c85ddcSPeter Lieven             if (memcmp(server_ptr, guest_ptr, cmp_bytes) == 0) {
27233e230dd2SCorentin Chary                 continue;
2724b4c85ddcSPeter Lieven             }
27253e230dd2SCorentin Chary             memcpy(server_ptr, guest_ptr, cmp_bytes);
272612b316d4SPeter Lieven             if (!vd->non_adaptive) {
272712b316d4SPeter Lieven                 vnc_rect_updated(vd, x * VNC_DIRTY_PIXELS_PER_BIT,
272812b316d4SPeter Lieven                                  y, &tv);
272912b316d4SPeter Lieven             }
27303e230dd2SCorentin Chary             QTAILQ_FOREACH(vs, &vd->clients, next) {
273112b316d4SPeter Lieven                 set_bit(x, vs->dirty[y]);
27323e230dd2SCorentin Chary             }
27333e230dd2SCorentin Chary             has_dirty++;
27343e230dd2SCorentin Chary         }
273512b316d4SPeter Lieven 
273612b316d4SPeter Lieven         y++;
27373e230dd2SCorentin Chary     }
27389f64916dSGerd Hoffmann     qemu_pixman_image_unref(tmpbuf);
27393e230dd2SCorentin Chary     return has_dirty;
27403e230dd2SCorentin Chary }
27413e230dd2SCorentin Chary 
27420f7b2864SGerd Hoffmann static void vnc_refresh(DisplayChangeListener *dcl)
27433e230dd2SCorentin Chary {
27440f7b2864SGerd Hoffmann     VncDisplay *vd = container_of(dcl, VncDisplay, dcl);
27453e230dd2SCorentin Chary     VncState *vs, *vn;
27463e230dd2SCorentin Chary     int has_dirty, rects = 0;
27473e230dd2SCorentin Chary 
27481dbfa005SGerd Hoffmann     graphic_hw_update(NULL);
27493e230dd2SCorentin Chary 
2750bd023f95SCorentin Chary     if (vnc_trylock_display(vd)) {
27510f7b2864SGerd Hoffmann         update_displaychangelistener(&vd->dcl, VNC_REFRESH_INTERVAL_BASE);
2752bd023f95SCorentin Chary         return;
2753bd023f95SCorentin Chary     }
2754bd023f95SCorentin Chary 
27553e230dd2SCorentin Chary     has_dirty = vnc_refresh_server_surface(vd);
2756bd023f95SCorentin Chary     vnc_unlock_display(vd);
27573e230dd2SCorentin Chary 
27583e230dd2SCorentin Chary     QTAILQ_FOREACH_SAFE(vs, &vd->clients, next, vn) {
275938ee14f4SGerd Hoffmann         rects += vnc_update_client(vs, has_dirty, false);
27603e230dd2SCorentin Chary         /* vs might be free()ed here */
27613e230dd2SCorentin Chary     }
2762bd023f95SCorentin Chary 
27630f7b2864SGerd Hoffmann     if (QTAILQ_EMPTY(&vd->clients)) {
27640f7b2864SGerd Hoffmann         update_displaychangelistener(&vd->dcl, VNC_REFRESH_INTERVAL_MAX);
27653e230dd2SCorentin Chary         return;
27660f7b2864SGerd Hoffmann     }
27673e230dd2SCorentin Chary 
27683e230dd2SCorentin Chary     if (has_dirty && rects) {
27690f7b2864SGerd Hoffmann         vd->dcl.update_interval /= 2;
27700f7b2864SGerd Hoffmann         if (vd->dcl.update_interval < VNC_REFRESH_INTERVAL_BASE) {
27710f7b2864SGerd Hoffmann             vd->dcl.update_interval = VNC_REFRESH_INTERVAL_BASE;
27720f7b2864SGerd Hoffmann         }
27733e230dd2SCorentin Chary     } else {
27740f7b2864SGerd Hoffmann         vd->dcl.update_interval += VNC_REFRESH_INTERVAL_INC;
27750f7b2864SGerd Hoffmann         if (vd->dcl.update_interval > VNC_REFRESH_INTERVAL_MAX) {
27760f7b2864SGerd Hoffmann             vd->dcl.update_interval = VNC_REFRESH_INTERVAL_MAX;
27773e230dd2SCorentin Chary         }
27783e230dd2SCorentin Chary     }
27793e230dd2SCorentin Chary }
27803e230dd2SCorentin Chary 
27812c8cf549SMichael Tokarev static void vnc_connect(VncDisplay *vd, int csock,
27822c8cf549SMichael Tokarev                         bool skipauth, bool websocket)
27833e230dd2SCorentin Chary {
27847267c094SAnthony Liguori     VncState *vs = g_malloc0(sizeof(VncState));
27857d964c9dSCorentin Chary     int i;
27867d964c9dSCorentin Chary 
27873e230dd2SCorentin Chary     vs->csock = csock;
27887e7e2ebcSDaniel P. Berrange 
27897e7e2ebcSDaniel P. Berrange     if (skipauth) {
27907e7e2ebcSDaniel P. Berrange 	vs->auth = VNC_AUTH_NONE;
27917e7e2ebcSDaniel P. Berrange #ifdef CONFIG_VNC_TLS
27927e7e2ebcSDaniel P. Berrange 	vs->subauth = VNC_AUTH_INVALID;
27937e7e2ebcSDaniel P. Berrange #endif
27947e7e2ebcSDaniel P. Berrange     } else {
27957e7e2ebcSDaniel P. Berrange 	vs->auth = vd->auth;
27967e7e2ebcSDaniel P. Berrange #ifdef CONFIG_VNC_TLS
27977e7e2ebcSDaniel P. Berrange 	vs->subauth = vd->subauth;
27987e7e2ebcSDaniel P. Berrange #endif
27997e7e2ebcSDaniel P. Berrange     }
28007e7e2ebcSDaniel P. Berrange 
28017267c094SAnthony Liguori     vs->lossy_rect = g_malloc0(VNC_STAT_ROWS * sizeof (*vs->lossy_rect));
28027d964c9dSCorentin Chary     for (i = 0; i < VNC_STAT_ROWS; ++i) {
28037267c094SAnthony Liguori         vs->lossy_rect[i] = g_malloc0(VNC_STAT_COLS * sizeof (uint8_t));
28047d964c9dSCorentin Chary     }
28053e230dd2SCorentin Chary 
28063e230dd2SCorentin Chary     VNC_DEBUG("New client on socket %d\n", csock);
28070f7b2864SGerd Hoffmann     update_displaychangelistener(&vd->dcl, VNC_REFRESH_INTERVAL_BASE);
2808f9e8caccSStefan Hajnoczi     qemu_set_nonblock(vs->csock);
28097536ee4bSTim Hardeck #ifdef CONFIG_VNC_WS
28107536ee4bSTim Hardeck     if (websocket) {
28117536ee4bSTim Hardeck         vs->websocket = 1;
28120057a0d5STim Hardeck #ifdef CONFIG_VNC_TLS
28130057a0d5STim Hardeck         if (vd->tls.x509cert) {
28140057a0d5STim Hardeck             qemu_set_fd_handler2(vs->csock, NULL, vncws_tls_handshake_peek,
28150057a0d5STim Hardeck                                  NULL, vs);
28160057a0d5STim Hardeck         } else
28170057a0d5STim Hardeck #endif /* CONFIG_VNC_TLS */
28180057a0d5STim Hardeck         {
28190057a0d5STim Hardeck             qemu_set_fd_handler2(vs->csock, NULL, vncws_handshake_read,
28200057a0d5STim Hardeck                                  NULL, vs);
28210057a0d5STim Hardeck         }
28227536ee4bSTim Hardeck     } else
28237536ee4bSTim Hardeck #endif /* CONFIG_VNC_WS */
28247536ee4bSTim Hardeck     {
28253e230dd2SCorentin Chary         qemu_set_fd_handler2(vs->csock, NULL, vnc_client_read, NULL, vs);
28267536ee4bSTim Hardeck     }
28273e230dd2SCorentin Chary 
28283e230dd2SCorentin Chary     vnc_client_cache_addr(vs);
28293e230dd2SCorentin Chary     vnc_qmp_event(vs, QEVENT_VNC_CONNECTED);
28308cf36489SGerd Hoffmann     vnc_set_share_mode(vs, VNC_SHARE_MODE_CONNECTING);
28313e230dd2SCorentin Chary 
28323e230dd2SCorentin Chary     vs->vd = vd;
28337536ee4bSTim Hardeck 
28347536ee4bSTim Hardeck #ifdef CONFIG_VNC_WS
28357536ee4bSTim Hardeck     if (!vs->websocket)
28367536ee4bSTim Hardeck #endif
28377536ee4bSTim Hardeck     {
28387536ee4bSTim Hardeck         vnc_init_state(vs);
28397536ee4bSTim Hardeck     }
28407536ee4bSTim Hardeck }
28417536ee4bSTim Hardeck 
28427536ee4bSTim Hardeck void vnc_init_state(VncState *vs)
28437536ee4bSTim Hardeck {
28446fd8e79aSTim Hardeck     vs->initialized = true;
28457536ee4bSTim Hardeck     VncDisplay *vd = vs->vd;
28467536ee4bSTim Hardeck 
28473e230dd2SCorentin Chary     vs->last_x = -1;
28483e230dd2SCorentin Chary     vs->last_y = -1;
28493e230dd2SCorentin Chary 
28503e230dd2SCorentin Chary     vs->as.freq = 44100;
28513e230dd2SCorentin Chary     vs->as.nchannels = 2;
28523e230dd2SCorentin Chary     vs->as.fmt = AUD_FMT_S16;
28533e230dd2SCorentin Chary     vs->as.endianness = 0;
28543e230dd2SCorentin Chary 
2855bd023f95SCorentin Chary     qemu_mutex_init(&vs->output_mutex);
2856175b2a6eSCorentin Chary     vs->bh = qemu_bh_new(vnc_jobs_bh, vs);
2857bd023f95SCorentin Chary 
28583e230dd2SCorentin Chary     QTAILQ_INSERT_HEAD(&vd->clients, vs, next);
28593e230dd2SCorentin Chary 
28601dbfa005SGerd Hoffmann     graphic_hw_update(NULL);
28613e230dd2SCorentin Chary 
28623e230dd2SCorentin Chary     vnc_write(vs, "RFB 003.008\n", 12);
28633e230dd2SCorentin Chary     vnc_flush(vs);
28643e230dd2SCorentin Chary     vnc_read_when(vs, protocol_version, 12);
28653e230dd2SCorentin Chary     reset_keys(vs);
28663e230dd2SCorentin Chary     if (vs->vd->lock_key_sync)
28673e230dd2SCorentin Chary         vs->led = qemu_add_led_event_handler(kbd_leds, vs);
28683e230dd2SCorentin Chary 
28693e230dd2SCorentin Chary     vs->mouse_mode_notifier.notify = check_pointer_type_change;
28703e230dd2SCorentin Chary     qemu_add_mouse_mode_change_notifier(&vs->mouse_mode_notifier);
28713e230dd2SCorentin Chary 
28723e230dd2SCorentin Chary     /* vs might be free()ed here */
28733e230dd2SCorentin Chary }
28743e230dd2SCorentin Chary 
28757536ee4bSTim Hardeck static void vnc_listen_read(void *opaque, bool websocket)
28763e230dd2SCorentin Chary {
28773e230dd2SCorentin Chary     VncDisplay *vs = opaque;
28783e230dd2SCorentin Chary     struct sockaddr_in addr;
28793e230dd2SCorentin Chary     socklen_t addrlen = sizeof(addr);
28807536ee4bSTim Hardeck     int csock;
28813e230dd2SCorentin Chary 
28823e230dd2SCorentin Chary     /* Catch-up */
28831dbfa005SGerd Hoffmann     graphic_hw_update(NULL);
28847536ee4bSTim Hardeck #ifdef CONFIG_VNC_WS
28857536ee4bSTim Hardeck     if (websocket) {
28867536ee4bSTim Hardeck         csock = qemu_accept(vs->lwebsock, (struct sockaddr *)&addr, &addrlen);
28877536ee4bSTim Hardeck     } else
28887536ee4bSTim Hardeck #endif /* CONFIG_VNC_WS */
28897536ee4bSTim Hardeck     {
28907536ee4bSTim Hardeck         csock = qemu_accept(vs->lsock, (struct sockaddr *)&addr, &addrlen);
28917536ee4bSTim Hardeck     }
28923e230dd2SCorentin Chary 
28933e230dd2SCorentin Chary     if (csock != -1) {
28942c8cf549SMichael Tokarev         vnc_connect(vs, csock, false, websocket);
28953e230dd2SCorentin Chary     }
28963e230dd2SCorentin Chary }
28973e230dd2SCorentin Chary 
28987536ee4bSTim Hardeck static void vnc_listen_regular_read(void *opaque)
28997536ee4bSTim Hardeck {
29002c8cf549SMichael Tokarev     vnc_listen_read(opaque, false);
29017536ee4bSTim Hardeck }
29027536ee4bSTim Hardeck 
29037536ee4bSTim Hardeck #ifdef CONFIG_VNC_WS
29047536ee4bSTim Hardeck static void vnc_listen_websocket_read(void *opaque)
29057536ee4bSTim Hardeck {
29062c8cf549SMichael Tokarev     vnc_listen_read(opaque, true);
29077536ee4bSTim Hardeck }
29087536ee4bSTim Hardeck #endif /* CONFIG_VNC_WS */
29097536ee4bSTim Hardeck 
29107c20b4a3SGerd Hoffmann static const DisplayChangeListenerOps dcl_ops = {
29117c20b4a3SGerd Hoffmann     .dpy_name          = "vnc",
29120f7b2864SGerd Hoffmann     .dpy_refresh       = vnc_refresh,
29137c20b4a3SGerd Hoffmann     .dpy_gfx_copy      = vnc_dpy_copy,
29147c20b4a3SGerd Hoffmann     .dpy_gfx_update    = vnc_dpy_update,
2915c12aeb86SGerd Hoffmann     .dpy_gfx_switch    = vnc_dpy_switch,
29167c20b4a3SGerd Hoffmann     .dpy_mouse_set     = vnc_mouse_set,
29177c20b4a3SGerd Hoffmann     .dpy_cursor_define = vnc_dpy_cursor_define,
29187c20b4a3SGerd Hoffmann };
29197c20b4a3SGerd Hoffmann 
29203e230dd2SCorentin Chary void vnc_display_init(DisplayState *ds)
29213e230dd2SCorentin Chary {
29227267c094SAnthony Liguori     VncDisplay *vs = g_malloc0(sizeof(*vs));
29233e230dd2SCorentin Chary 
29243e230dd2SCorentin Chary     vnc_display = vs;
29253e230dd2SCorentin Chary 
29263e230dd2SCorentin Chary     vs->lsock = -1;
29277536ee4bSTim Hardeck #ifdef CONFIG_VNC_WS
29287536ee4bSTim Hardeck     vs->lwebsock = -1;
29297536ee4bSTim Hardeck #endif
29303e230dd2SCorentin Chary 
29313e230dd2SCorentin Chary     QTAILQ_INIT(&vs->clients);
29323c9405a0SGerd Hoffmann     vs->expires = TIME_MAX;
29333e230dd2SCorentin Chary 
29343e230dd2SCorentin Chary     if (keyboard_layout)
29353e230dd2SCorentin Chary         vs->kbd_layout = init_keyboard_layout(name2keysym, keyboard_layout);
29363e230dd2SCorentin Chary     else
29373e230dd2SCorentin Chary         vs->kbd_layout = init_keyboard_layout(name2keysym, "en-us");
29383e230dd2SCorentin Chary 
29393e230dd2SCorentin Chary     if (!vs->kbd_layout)
29403e230dd2SCorentin Chary         exit(1);
29413e230dd2SCorentin Chary 
2942bd023f95SCorentin Chary     qemu_mutex_init(&vs->mutex);
2943bd023f95SCorentin Chary     vnc_start_worker_thread();
2944bd023f95SCorentin Chary 
294521ef45d7SGerd Hoffmann     vs->dcl.ops = &dcl_ops;
29465209089fSGerd Hoffmann     register_displaychangelistener(&vs->dcl);
29473e230dd2SCorentin Chary }
29483e230dd2SCorentin Chary 
29493e230dd2SCorentin Chary 
295071a8cdecSBlue Swirl static void vnc_display_close(DisplayState *ds)
29513e230dd2SCorentin Chary {
295221ef45d7SGerd Hoffmann     VncDisplay *vs = vnc_display;
29533e230dd2SCorentin Chary 
29543e230dd2SCorentin Chary     if (!vs)
29553e230dd2SCorentin Chary         return;
29563e230dd2SCorentin Chary     if (vs->display) {
29577267c094SAnthony Liguori         g_free(vs->display);
29583e230dd2SCorentin Chary         vs->display = NULL;
29593e230dd2SCorentin Chary     }
29603e230dd2SCorentin Chary     if (vs->lsock != -1) {
29613e230dd2SCorentin Chary         qemu_set_fd_handler2(vs->lsock, NULL, NULL, NULL, NULL);
29623e230dd2SCorentin Chary         close(vs->lsock);
29633e230dd2SCorentin Chary         vs->lsock = -1;
29643e230dd2SCorentin Chary     }
29657536ee4bSTim Hardeck #ifdef CONFIG_VNC_WS
29667536ee4bSTim Hardeck     g_free(vs->ws_display);
29677536ee4bSTim Hardeck     vs->ws_display = NULL;
29687536ee4bSTim Hardeck     if (vs->lwebsock != -1) {
29697536ee4bSTim Hardeck         qemu_set_fd_handler2(vs->lwebsock, NULL, NULL, NULL, NULL);
29707536ee4bSTim Hardeck         close(vs->lwebsock);
29717536ee4bSTim Hardeck         vs->lwebsock = -1;
29727536ee4bSTim Hardeck     }
29737536ee4bSTim Hardeck #endif /* CONFIG_VNC_WS */
29743e230dd2SCorentin Chary     vs->auth = VNC_AUTH_INVALID;
29753e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS
29763e230dd2SCorentin Chary     vs->subauth = VNC_AUTH_INVALID;
29773e230dd2SCorentin Chary     vs->tls.x509verify = 0;
29783e230dd2SCorentin Chary #endif
29793e230dd2SCorentin Chary }
29803e230dd2SCorentin Chary 
298171a8cdecSBlue Swirl static int vnc_display_disable_login(DisplayState *ds)
29823e230dd2SCorentin Chary {
298321ef45d7SGerd Hoffmann     VncDisplay *vs = vnc_display;
29843e230dd2SCorentin Chary 
29853e230dd2SCorentin Chary     if (!vs) {
29863e230dd2SCorentin Chary         return -1;
29873e230dd2SCorentin Chary     }
29883e230dd2SCorentin Chary 
29893e230dd2SCorentin Chary     if (vs->password) {
29907267c094SAnthony Liguori         g_free(vs->password);
29911cd20f8bSAnthony Liguori     }
29921cd20f8bSAnthony Liguori 
29931cd20f8bSAnthony Liguori     vs->password = NULL;
29947dfbfc79SDaniel P. Berrange     if (vs->auth == VNC_AUTH_NONE) {
29951cd20f8bSAnthony Liguori         vs->auth = VNC_AUTH_VNC;
29967dfbfc79SDaniel P. Berrange     }
29971cd20f8bSAnthony Liguori 
29981cd20f8bSAnthony Liguori     return 0;
29991cd20f8bSAnthony Liguori }
30001cd20f8bSAnthony Liguori 
30011cd20f8bSAnthony Liguori int vnc_display_password(DisplayState *ds, const char *password)
30021cd20f8bSAnthony Liguori {
300321ef45d7SGerd Hoffmann     VncDisplay *vs = vnc_display;
30041cd20f8bSAnthony Liguori 
30051cd20f8bSAnthony Liguori     if (!vs) {
3006a6aa9d3eSLuiz Capitulino         return -EINVAL;
30071cd20f8bSAnthony Liguori     }
30081cd20f8bSAnthony Liguori 
30091cd20f8bSAnthony Liguori     if (!password) {
30101cd20f8bSAnthony Liguori         /* This is not the intention of this interface but err on the side
30111cd20f8bSAnthony Liguori            of being safe */
3012a6aa9d3eSLuiz Capitulino         return vnc_display_disable_login(ds);
30131cd20f8bSAnthony Liguori     }
30141cd20f8bSAnthony Liguori 
30151cd20f8bSAnthony Liguori     if (vs->password) {
30167267c094SAnthony Liguori         g_free(vs->password);
30173e230dd2SCorentin Chary         vs->password = NULL;
30183e230dd2SCorentin Chary     }
30197267c094SAnthony Liguori     vs->password = g_strdup(password);
30207dfbfc79SDaniel P. Berrange     if (vs->auth == VNC_AUTH_NONE) {
30213e230dd2SCorentin Chary         vs->auth = VNC_AUTH_VNC;
30227dfbfc79SDaniel P. Berrange     }
3023a6aa9d3eSLuiz Capitulino 
3024a6aa9d3eSLuiz Capitulino     return 0;
30253e230dd2SCorentin Chary }
30263e230dd2SCorentin Chary 
30273c9405a0SGerd Hoffmann int vnc_display_pw_expire(DisplayState *ds, time_t expires)
30283c9405a0SGerd Hoffmann {
302921ef45d7SGerd Hoffmann     VncDisplay *vs = vnc_display;
30303c9405a0SGerd Hoffmann 
30311643f2b2SGerd Hoffmann     if (!vs) {
30321643f2b2SGerd Hoffmann         return -EINVAL;
30331643f2b2SGerd Hoffmann     }
30341643f2b2SGerd Hoffmann 
30353c9405a0SGerd Hoffmann     vs->expires = expires;
30363c9405a0SGerd Hoffmann     return 0;
30373c9405a0SGerd Hoffmann }
30383c9405a0SGerd Hoffmann 
30393e230dd2SCorentin Chary char *vnc_display_local_addr(DisplayState *ds)
30403e230dd2SCorentin Chary {
304121ef45d7SGerd Hoffmann     VncDisplay *vs = vnc_display;
30423e230dd2SCorentin Chary 
30433e230dd2SCorentin Chary     return vnc_socket_local_addr("%s:%s", vs->lsock);
30443e230dd2SCorentin Chary }
30453e230dd2SCorentin Chary 
30462d55f0e8SPaolo Bonzini void vnc_display_open(DisplayState *ds, const char *display, Error **errp)
30473e230dd2SCorentin Chary {
304821ef45d7SGerd Hoffmann     VncDisplay *vs = vnc_display;
30493e230dd2SCorentin Chary     const char *options;
30503e230dd2SCorentin Chary     int password = 0;
30513e230dd2SCorentin Chary     int reverse = 0;
30523e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS
30533e230dd2SCorentin Chary     int tls = 0, x509 = 0;
30543e230dd2SCorentin Chary #endif
30553e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL
30563e230dd2SCorentin Chary     int sasl = 0;
30573e230dd2SCorentin Chary     int saslErr;
30583e230dd2SCorentin Chary #endif
30592ded6ad7SBlue Swirl #if defined(CONFIG_VNC_TLS) || defined(CONFIG_VNC_SASL)
30603e230dd2SCorentin Chary     int acl = 0;
30612ded6ad7SBlue Swirl #endif
30623e230dd2SCorentin Chary     int lock_key_sync = 1;
30633e230dd2SCorentin Chary 
30642d55f0e8SPaolo Bonzini     if (!vnc_display) {
30652d55f0e8SPaolo Bonzini         error_setg(errp, "VNC display not active");
30662d55f0e8SPaolo Bonzini         return;
30672d55f0e8SPaolo Bonzini     }
30683e230dd2SCorentin Chary     vnc_display_close(ds);
30693e230dd2SCorentin Chary     if (strcmp(display, "none") == 0)
30702d55f0e8SPaolo Bonzini         return;
30713e230dd2SCorentin Chary 
30721ce52c78SPaolo Bonzini     vs->display = g_strdup(display);
30738cf36489SGerd Hoffmann     vs->share_policy = VNC_SHARE_POLICY_ALLOW_EXCLUSIVE;
30743e230dd2SCorentin Chary 
30753e230dd2SCorentin Chary     options = display;
30763e230dd2SCorentin Chary     while ((options = strchr(options, ','))) {
30773e230dd2SCorentin Chary         options++;
30783e230dd2SCorentin Chary         if (strncmp(options, "password", 8) == 0) {
30790f66998fSPaul Moore             if (fips_get_state()) {
30802d55f0e8SPaolo Bonzini                 error_setg(errp,
30810f66998fSPaul Moore                            "VNC password auth disabled due to FIPS mode, "
30820f66998fSPaul Moore                            "consider using the VeNCrypt or SASL authentication "
30832d55f0e8SPaolo Bonzini                            "methods as an alternative");
30841ce52c78SPaolo Bonzini                 goto fail;
30850f66998fSPaul Moore             }
30863e230dd2SCorentin Chary             password = 1; /* Require password auth */
30873e230dd2SCorentin Chary         } else if (strncmp(options, "reverse", 7) == 0) {
30883e230dd2SCorentin Chary             reverse = 1;
3089cee8e6adSStefan Hajnoczi         } else if (strncmp(options, "no-lock-key-sync", 16) == 0) {
30903e230dd2SCorentin Chary             lock_key_sync = 0;
30913e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL
30923e230dd2SCorentin Chary         } else if (strncmp(options, "sasl", 4) == 0) {
30933e230dd2SCorentin Chary             sasl = 1; /* Require SASL auth */
30943e230dd2SCorentin Chary #endif
30957536ee4bSTim Hardeck #ifdef CONFIG_VNC_WS
30967536ee4bSTim Hardeck         } else if (strncmp(options, "websocket", 9) == 0) {
30977536ee4bSTim Hardeck             char *start, *end;
30987536ee4bSTim Hardeck             vs->websocket = 1;
30997536ee4bSTim Hardeck 
31007536ee4bSTim Hardeck             /* Check for 'websocket=<port>' */
31017536ee4bSTim Hardeck             start = strchr(options, '=');
31027536ee4bSTim Hardeck             end = strchr(options, ',');
31037536ee4bSTim Hardeck             if (start && (!end || (start < end))) {
31047536ee4bSTim Hardeck                 int len = end ? end-(start+1) : strlen(start+1);
31057536ee4bSTim Hardeck                 if (len < 6) {
31067536ee4bSTim Hardeck                     /* extract the host specification from display */
31077536ee4bSTim Hardeck                     char  *host = NULL, *port = NULL, *host_end = NULL;
31087536ee4bSTim Hardeck                     port = g_strndup(start + 1, len);
31097536ee4bSTim Hardeck 
31107536ee4bSTim Hardeck                     /* ipv6 hosts have colons */
31117536ee4bSTim Hardeck                     end = strchr(display, ',');
31127536ee4bSTim Hardeck                     host_end = g_strrstr_len(display, end - display, ":");
31137536ee4bSTim Hardeck 
31147536ee4bSTim Hardeck                     if (host_end) {
31157536ee4bSTim Hardeck                         host = g_strndup(display, host_end - display + 1);
31167536ee4bSTim Hardeck                     } else {
31177536ee4bSTim Hardeck                         host = g_strndup(":", 1);
31187536ee4bSTim Hardeck                     }
31197536ee4bSTim Hardeck                     vs->ws_display = g_strconcat(host, port, NULL);
31207536ee4bSTim Hardeck                     g_free(host);
31217536ee4bSTim Hardeck                     g_free(port);
31227536ee4bSTim Hardeck                 }
31237536ee4bSTim Hardeck             }
31247536ee4bSTim Hardeck #endif /* CONFIG_VNC_WS */
31253e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS
31263e230dd2SCorentin Chary         } else if (strncmp(options, "tls", 3) == 0) {
31273e230dd2SCorentin Chary             tls = 1; /* Require TLS */
31283e230dd2SCorentin Chary         } else if (strncmp(options, "x509", 4) == 0) {
31293e230dd2SCorentin Chary             char *start, *end;
31303e230dd2SCorentin Chary             x509 = 1; /* Require x509 certificates */
31313e230dd2SCorentin Chary             if (strncmp(options, "x509verify", 10) == 0)
31323e230dd2SCorentin Chary                 vs->tls.x509verify = 1; /* ...and verify client certs */
31333e230dd2SCorentin Chary 
31343e230dd2SCorentin Chary             /* Now check for 'x509=/some/path' postfix
31353e230dd2SCorentin Chary              * and use that to setup x509 certificate/key paths */
31363e230dd2SCorentin Chary             start = strchr(options, '=');
31373e230dd2SCorentin Chary             end = strchr(options, ',');
31383e230dd2SCorentin Chary             if (start && (!end || (start < end))) {
31393e230dd2SCorentin Chary                 int len = end ? end-(start+1) : strlen(start+1);
31407267c094SAnthony Liguori                 char *path = g_strndup(start + 1, len);
31413e230dd2SCorentin Chary 
31423e230dd2SCorentin Chary                 VNC_DEBUG("Trying certificate path '%s'\n", path);
31433e230dd2SCorentin Chary                 if (vnc_tls_set_x509_creds_dir(vs, path) < 0) {
31442d55f0e8SPaolo Bonzini                     error_setg(errp, "Failed to find x509 certificates/keys in %s", path);
31457267c094SAnthony Liguori                     g_free(path);
31461ce52c78SPaolo Bonzini                     goto fail;
31473e230dd2SCorentin Chary                 }
31487267c094SAnthony Liguori                 g_free(path);
31493e230dd2SCorentin Chary             } else {
31502d55f0e8SPaolo Bonzini                 error_setg(errp, "No certificate path provided");
31511ce52c78SPaolo Bonzini                 goto fail;
31523e230dd2SCorentin Chary             }
31533e230dd2SCorentin Chary #endif
31542ded6ad7SBlue Swirl #if defined(CONFIG_VNC_TLS) || defined(CONFIG_VNC_SASL)
31553e230dd2SCorentin Chary         } else if (strncmp(options, "acl", 3) == 0) {
31563e230dd2SCorentin Chary             acl = 1;
31572ded6ad7SBlue Swirl #endif
31583e230dd2SCorentin Chary         } else if (strncmp(options, "lossy", 5) == 0) {
31593e230dd2SCorentin Chary             vs->lossy = true;
31607eff5742SCatalin Patulea         } else if (strncmp(options, "non-adaptive", 12) == 0) {
316180e0c8c3SCorentin Chary             vs->non_adaptive = true;
31628cf36489SGerd Hoffmann         } else if (strncmp(options, "share=", 6) == 0) {
31638cf36489SGerd Hoffmann             if (strncmp(options+6, "ignore", 6) == 0) {
31648cf36489SGerd Hoffmann                 vs->share_policy = VNC_SHARE_POLICY_IGNORE;
31658cf36489SGerd Hoffmann             } else if (strncmp(options+6, "allow-exclusive", 15) == 0) {
31668cf36489SGerd Hoffmann                 vs->share_policy = VNC_SHARE_POLICY_ALLOW_EXCLUSIVE;
31678cf36489SGerd Hoffmann             } else if (strncmp(options+6, "force-shared", 12) == 0) {
31688cf36489SGerd Hoffmann                 vs->share_policy = VNC_SHARE_POLICY_FORCE_SHARED;
31698cf36489SGerd Hoffmann             } else {
31702d55f0e8SPaolo Bonzini                 error_setg(errp, "unknown vnc share= option");
31711ce52c78SPaolo Bonzini                 goto fail;
31728cf36489SGerd Hoffmann             }
31733e230dd2SCorentin Chary         }
31743e230dd2SCorentin Chary     }
31753e230dd2SCorentin Chary 
31763e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS
31773e230dd2SCorentin Chary     if (acl && x509 && vs->tls.x509verify) {
31783e230dd2SCorentin Chary         if (!(vs->tls.acl = qemu_acl_init("vnc.x509dname"))) {
31793e230dd2SCorentin Chary             fprintf(stderr, "Failed to create x509 dname ACL\n");
31803e230dd2SCorentin Chary             exit(1);
31813e230dd2SCorentin Chary         }
31823e230dd2SCorentin Chary     }
31833e230dd2SCorentin Chary #endif
31843e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL
31853e230dd2SCorentin Chary     if (acl && sasl) {
31863e230dd2SCorentin Chary         if (!(vs->sasl.acl = qemu_acl_init("vnc.username"))) {
31873e230dd2SCorentin Chary             fprintf(stderr, "Failed to create username ACL\n");
31883e230dd2SCorentin Chary             exit(1);
31893e230dd2SCorentin Chary         }
31903e230dd2SCorentin Chary     }
31913e230dd2SCorentin Chary #endif
31923e230dd2SCorentin Chary 
31933e230dd2SCorentin Chary     /*
31943e230dd2SCorentin Chary      * Combinations we support here:
31953e230dd2SCorentin Chary      *
31963e230dd2SCorentin Chary      *  - no-auth                (clear text, no auth)
31973e230dd2SCorentin Chary      *  - password               (clear text, weak auth)
31983e230dd2SCorentin Chary      *  - sasl                   (encrypt, good auth *IF* using Kerberos via GSSAPI)
31993e230dd2SCorentin Chary      *  - tls                    (encrypt, weak anonymous creds, no auth)
32003e230dd2SCorentin Chary      *  - tls + password         (encrypt, weak anonymous creds, weak auth)
32013e230dd2SCorentin Chary      *  - tls + sasl             (encrypt, weak anonymous creds, good auth)
32023e230dd2SCorentin Chary      *  - tls + x509             (encrypt, good x509 creds, no auth)
32033e230dd2SCorentin Chary      *  - tls + x509 + password  (encrypt, good x509 creds, weak auth)
32043e230dd2SCorentin Chary      *  - tls + x509 + sasl      (encrypt, good x509 creds, good auth)
32053e230dd2SCorentin Chary      *
32063e230dd2SCorentin Chary      * NB1. TLS is a stackable auth scheme.
32073e230dd2SCorentin Chary      * NB2. the x509 schemes have option to validate a client cert dname
32083e230dd2SCorentin Chary      */
32093e230dd2SCorentin Chary     if (password) {
32103e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS
32113e230dd2SCorentin Chary         if (tls) {
32123e230dd2SCorentin Chary             vs->auth = VNC_AUTH_VENCRYPT;
32133e230dd2SCorentin Chary             if (x509) {
32143e230dd2SCorentin Chary                 VNC_DEBUG("Initializing VNC server with x509 password auth\n");
32153e230dd2SCorentin Chary                 vs->subauth = VNC_AUTH_VENCRYPT_X509VNC;
32163e230dd2SCorentin Chary             } else {
32173e230dd2SCorentin Chary                 VNC_DEBUG("Initializing VNC server with TLS password auth\n");
32183e230dd2SCorentin Chary                 vs->subauth = VNC_AUTH_VENCRYPT_TLSVNC;
32193e230dd2SCorentin Chary             }
32203e230dd2SCorentin Chary         } else {
32213e230dd2SCorentin Chary #endif /* CONFIG_VNC_TLS */
32223e230dd2SCorentin Chary             VNC_DEBUG("Initializing VNC server with password auth\n");
32233e230dd2SCorentin Chary             vs->auth = VNC_AUTH_VNC;
32243e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS
32253e230dd2SCorentin Chary             vs->subauth = VNC_AUTH_INVALID;
32263e230dd2SCorentin Chary         }
32273e230dd2SCorentin Chary #endif /* CONFIG_VNC_TLS */
32283e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL
32293e230dd2SCorentin Chary     } else if (sasl) {
32303e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS
32313e230dd2SCorentin Chary         if (tls) {
32323e230dd2SCorentin Chary             vs->auth = VNC_AUTH_VENCRYPT;
32333e230dd2SCorentin Chary             if (x509) {
32343e230dd2SCorentin Chary                 VNC_DEBUG("Initializing VNC server with x509 SASL auth\n");
32353e230dd2SCorentin Chary                 vs->subauth = VNC_AUTH_VENCRYPT_X509SASL;
32363e230dd2SCorentin Chary             } else {
32373e230dd2SCorentin Chary                 VNC_DEBUG("Initializing VNC server with TLS SASL auth\n");
32383e230dd2SCorentin Chary                 vs->subauth = VNC_AUTH_VENCRYPT_TLSSASL;
32393e230dd2SCorentin Chary             }
32403e230dd2SCorentin Chary         } else {
32413e230dd2SCorentin Chary #endif /* CONFIG_VNC_TLS */
32423e230dd2SCorentin Chary             VNC_DEBUG("Initializing VNC server with SASL auth\n");
32433e230dd2SCorentin Chary             vs->auth = VNC_AUTH_SASL;
32443e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS
32453e230dd2SCorentin Chary             vs->subauth = VNC_AUTH_INVALID;
32463e230dd2SCorentin Chary         }
32473e230dd2SCorentin Chary #endif /* CONFIG_VNC_TLS */
32483e230dd2SCorentin Chary #endif /* CONFIG_VNC_SASL */
32493e230dd2SCorentin Chary     } else {
32503e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS
32513e230dd2SCorentin Chary         if (tls) {
32523e230dd2SCorentin Chary             vs->auth = VNC_AUTH_VENCRYPT;
32533e230dd2SCorentin Chary             if (x509) {
32543e230dd2SCorentin Chary                 VNC_DEBUG("Initializing VNC server with x509 no auth\n");
32553e230dd2SCorentin Chary                 vs->subauth = VNC_AUTH_VENCRYPT_X509NONE;
32563e230dd2SCorentin Chary             } else {
32573e230dd2SCorentin Chary                 VNC_DEBUG("Initializing VNC server with TLS no auth\n");
32583e230dd2SCorentin Chary                 vs->subauth = VNC_AUTH_VENCRYPT_TLSNONE;
32593e230dd2SCorentin Chary             }
32603e230dd2SCorentin Chary         } else {
32613e230dd2SCorentin Chary #endif
32623e230dd2SCorentin Chary             VNC_DEBUG("Initializing VNC server with no auth\n");
32633e230dd2SCorentin Chary             vs->auth = VNC_AUTH_NONE;
32643e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS
32653e230dd2SCorentin Chary             vs->subauth = VNC_AUTH_INVALID;
32663e230dd2SCorentin Chary         }
32673e230dd2SCorentin Chary #endif
32683e230dd2SCorentin Chary     }
32693e230dd2SCorentin Chary 
32703e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL
32713e230dd2SCorentin Chary     if ((saslErr = sasl_server_init(NULL, "qemu")) != SASL_OK) {
32722d55f0e8SPaolo Bonzini         error_setg(errp, "Failed to initialize SASL auth: %s",
32733e230dd2SCorentin Chary                    sasl_errstring(saslErr, NULL, NULL));
32741ce52c78SPaolo Bonzini         goto fail;
32753e230dd2SCorentin Chary     }
32763e230dd2SCorentin Chary #endif
32773e230dd2SCorentin Chary     vs->lock_key_sync = lock_key_sync;
32783e230dd2SCorentin Chary 
32793e230dd2SCorentin Chary     if (reverse) {
32803e230dd2SCorentin Chary         /* connect to viewer */
3281007fcd3eSPaolo Bonzini         int csock;
32823e230dd2SCorentin Chary         vs->lsock = -1;
32837536ee4bSTim Hardeck #ifdef CONFIG_VNC_WS
32847536ee4bSTim Hardeck         vs->lwebsock = -1;
32857536ee4bSTim Hardeck #endif
3286007fcd3eSPaolo Bonzini         if (strncmp(display, "unix:", 5) == 0) {
32872d55f0e8SPaolo Bonzini             csock = unix_connect(display+5, errp);
3288007fcd3eSPaolo Bonzini         } else {
32892d55f0e8SPaolo Bonzini             csock = inet_connect(display, errp);
32903e230dd2SCorentin Chary         }
3291007fcd3eSPaolo Bonzini         if (csock < 0) {
3292007fcd3eSPaolo Bonzini             goto fail;
3293007fcd3eSPaolo Bonzini         }
32942c8cf549SMichael Tokarev         vnc_connect(vs, csock, false, false);
32953e230dd2SCorentin Chary     } else {
32963e230dd2SCorentin Chary         /* listen for connects */
32973e230dd2SCorentin Chary         char *dpy;
32987267c094SAnthony Liguori         dpy = g_malloc(256);
32993e230dd2SCorentin Chary         if (strncmp(display, "unix:", 5) == 0) {
33003e230dd2SCorentin Chary             pstrcpy(dpy, 256, "unix:");
33012d55f0e8SPaolo Bonzini             vs->lsock = unix_listen(display+5, dpy+5, 256-5, errp);
33023e230dd2SCorentin Chary         } else {
3303029409e5SAmos Kong             vs->lsock = inet_listen(display, dpy, 256,
33042d55f0e8SPaolo Bonzini                                     SOCK_STREAM, 5900, errp);
3305c1c1619cSPaolo Bonzini             if (vs->lsock < 0) {
3306ae878b17SStefan Weil                 g_free(dpy);
33071ce52c78SPaolo Bonzini                 goto fail;
33081ce52c78SPaolo Bonzini             }
33097536ee4bSTim Hardeck #ifdef CONFIG_VNC_WS
33107536ee4bSTim Hardeck             if (vs->websocket) {
33117536ee4bSTim Hardeck                 if (vs->ws_display) {
33127536ee4bSTim Hardeck                     vs->lwebsock = inet_listen(vs->ws_display, NULL, 256,
33137536ee4bSTim Hardeck                         SOCK_STREAM, 0, errp);
33147536ee4bSTim Hardeck                 } else {
33157536ee4bSTim Hardeck                     vs->lwebsock = inet_listen(vs->display, NULL, 256,
33167536ee4bSTim Hardeck                         SOCK_STREAM, 5700, errp);
33177536ee4bSTim Hardeck                 }
33187536ee4bSTim Hardeck 
33197536ee4bSTim Hardeck                 if (vs->lwebsock < 0) {
33207536ee4bSTim Hardeck                     if (vs->lsock) {
33217536ee4bSTim Hardeck                         close(vs->lsock);
33227536ee4bSTim Hardeck                         vs->lsock = -1;
33237536ee4bSTim Hardeck                     }
33247536ee4bSTim Hardeck                     g_free(dpy);
33257536ee4bSTim Hardeck                     goto fail;
33267536ee4bSTim Hardeck                 }
33277536ee4bSTim Hardeck             }
33287536ee4bSTim Hardeck #endif /* CONFIG_VNC_WS */
33297536ee4bSTim Hardeck         }
3330ae878b17SStefan Weil         g_free(vs->display);
33313e230dd2SCorentin Chary         vs->display = dpy;
33327536ee4bSTim Hardeck         qemu_set_fd_handler2(vs->lsock, NULL,
33337536ee4bSTim Hardeck                 vnc_listen_regular_read, NULL, vs);
33347536ee4bSTim Hardeck #ifdef CONFIG_VNC_WS
33357536ee4bSTim Hardeck         if (vs->websocket) {
33367536ee4bSTim Hardeck             qemu_set_fd_handler2(vs->lwebsock, NULL,
33377536ee4bSTim Hardeck                     vnc_listen_websocket_read, NULL, vs);
33387536ee4bSTim Hardeck         }
33397536ee4bSTim Hardeck #endif /* CONFIG_VNC_WS */
33403e230dd2SCorentin Chary     }
33412d55f0e8SPaolo Bonzini     return;
33421ce52c78SPaolo Bonzini 
33431ce52c78SPaolo Bonzini fail:
33441ce52c78SPaolo Bonzini     g_free(vs->display);
33451ce52c78SPaolo Bonzini     vs->display = NULL;
33467536ee4bSTim Hardeck #ifdef CONFIG_VNC_WS
33477536ee4bSTim Hardeck     g_free(vs->ws_display);
33487536ee4bSTim Hardeck     vs->ws_display = NULL;
33497536ee4bSTim Hardeck #endif /* CONFIG_VNC_WS */
33503e230dd2SCorentin Chary }
335113661089SDaniel P. Berrange 
33522c8cf549SMichael Tokarev void vnc_display_add_client(DisplayState *ds, int csock, bool skipauth)
335313661089SDaniel P. Berrange {
335421ef45d7SGerd Hoffmann     VncDisplay *vs = vnc_display;
335513661089SDaniel P. Berrange 
33562c8cf549SMichael Tokarev     vnc_connect(vs, csock, skipauth, false);
335713661089SDaniel P. Berrange }
3358