xref: /openbmc/qemu/ui/vnc.c (revision fb6ba0d5256cca2f1e844c03a7a51dd0a7982ac2)
13e230dd2SCorentin Chary /*
23e230dd2SCorentin Chary  * QEMU VNC display driver
33e230dd2SCorentin Chary  *
43e230dd2SCorentin Chary  * Copyright (C) 2006 Anthony Liguori <anthony@codemonkey.ws>
53e230dd2SCorentin Chary  * Copyright (C) 2006 Fabrice Bellard
63e230dd2SCorentin Chary  * Copyright (C) 2009 Red Hat, Inc
73e230dd2SCorentin Chary  *
83e230dd2SCorentin Chary  * Permission is hereby granted, free of charge, to any person obtaining a copy
93e230dd2SCorentin Chary  * of this software and associated documentation files (the "Software"), to deal
103e230dd2SCorentin Chary  * in the Software without restriction, including without limitation the rights
113e230dd2SCorentin Chary  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
123e230dd2SCorentin Chary  * copies of the Software, and to permit persons to whom the Software is
133e230dd2SCorentin Chary  * furnished to do so, subject to the following conditions:
143e230dd2SCorentin Chary  *
153e230dd2SCorentin Chary  * The above copyright notice and this permission notice shall be included in
163e230dd2SCorentin Chary  * all copies or substantial portions of the Software.
173e230dd2SCorentin Chary  *
183e230dd2SCorentin Chary  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
193e230dd2SCorentin Chary  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
203e230dd2SCorentin Chary  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
213e230dd2SCorentin Chary  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
223e230dd2SCorentin Chary  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
233e230dd2SCorentin Chary  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
243e230dd2SCorentin Chary  * THE SOFTWARE.
253e230dd2SCorentin Chary  */
263e230dd2SCorentin Chary 
273e230dd2SCorentin Chary #include "vnc.h"
28bd023f95SCorentin Chary #include "vnc-jobs.h"
2940066175SGerd Hoffmann #include "trace.h"
309c17d615SPaolo Bonzini #include "sysemu/sysemu.h"
311de7afc9SPaolo Bonzini #include "qemu/sockets.h"
321de7afc9SPaolo Bonzini #include "qemu/timer.h"
331de7afc9SPaolo Bonzini #include "qemu/acl.h"
347b1b5d19SPaolo Bonzini #include "qapi/qmp/types.h"
352b54aa87SLuiz Capitulino #include "qmp-commands.h"
361de7afc9SPaolo Bonzini #include "qemu/osdep.h"
378d447d10SGerd Hoffmann #include "ui/input.h"
38*fb6ba0d5SWenchao Xia #include "qapi-event.h"
393e230dd2SCorentin Chary 
400f7b2864SGerd Hoffmann #define VNC_REFRESH_INTERVAL_BASE GUI_REFRESH_INTERVAL_DEFAULT
413e230dd2SCorentin Chary #define VNC_REFRESH_INTERVAL_INC  50
420f7b2864SGerd Hoffmann #define VNC_REFRESH_INTERVAL_MAX  GUI_REFRESH_INTERVAL_IDLE
43999342a0SCorentin Chary static const struct timeval VNC_REFRESH_STATS = { 0, 500000 };
44999342a0SCorentin Chary static const struct timeval VNC_REFRESH_LOSSY = { 2, 0 };
453e230dd2SCorentin Chary 
463e230dd2SCorentin Chary #include "vnc_keysym.h"
473e230dd2SCorentin Chary #include "d3des.h"
483e230dd2SCorentin Chary 
493e230dd2SCorentin Chary static VncDisplay *vnc_display; /* needed for info vnc */
503e230dd2SCorentin Chary 
513e230dd2SCorentin Chary static int vnc_cursor_define(VncState *vs);
527bc9318bSGerd Hoffmann static void vnc_release_modifiers(VncState *vs);
533e230dd2SCorentin Chary 
548cf36489SGerd Hoffmann static void vnc_set_share_mode(VncState *vs, VncShareMode mode)
558cf36489SGerd Hoffmann {
568cf36489SGerd Hoffmann #ifdef _VNC_DEBUG
578cf36489SGerd Hoffmann     static const char *mn[] = {
588cf36489SGerd Hoffmann         [0]                           = "undefined",
598cf36489SGerd Hoffmann         [VNC_SHARE_MODE_CONNECTING]   = "connecting",
608cf36489SGerd Hoffmann         [VNC_SHARE_MODE_SHARED]       = "shared",
618cf36489SGerd Hoffmann         [VNC_SHARE_MODE_EXCLUSIVE]    = "exclusive",
628cf36489SGerd Hoffmann         [VNC_SHARE_MODE_DISCONNECTED] = "disconnected",
638cf36489SGerd Hoffmann     };
648cf36489SGerd Hoffmann     fprintf(stderr, "%s/%d: %s -> %s\n", __func__,
658cf36489SGerd Hoffmann             vs->csock, mn[vs->share_mode], mn[mode]);
668cf36489SGerd Hoffmann #endif
678cf36489SGerd Hoffmann 
688cf36489SGerd Hoffmann     if (vs->share_mode == VNC_SHARE_MODE_EXCLUSIVE) {
698cf36489SGerd Hoffmann         vs->vd->num_exclusive--;
708cf36489SGerd Hoffmann     }
718cf36489SGerd Hoffmann     vs->share_mode = mode;
728cf36489SGerd Hoffmann     if (vs->share_mode == VNC_SHARE_MODE_EXCLUSIVE) {
738cf36489SGerd Hoffmann         vs->vd->num_exclusive++;
748cf36489SGerd Hoffmann     }
758cf36489SGerd Hoffmann }
768cf36489SGerd Hoffmann 
773e230dd2SCorentin Chary static char *addr_to_string(const char *format,
783e230dd2SCorentin Chary                             struct sockaddr_storage *sa,
793e230dd2SCorentin Chary                             socklen_t salen) {
803e230dd2SCorentin Chary     char *addr;
813e230dd2SCorentin Chary     char host[NI_MAXHOST];
823e230dd2SCorentin Chary     char serv[NI_MAXSERV];
833e230dd2SCorentin Chary     int err;
843e230dd2SCorentin Chary     size_t addrlen;
853e230dd2SCorentin Chary 
863e230dd2SCorentin Chary     if ((err = getnameinfo((struct sockaddr *)sa, salen,
873e230dd2SCorentin Chary                            host, sizeof(host),
883e230dd2SCorentin Chary                            serv, sizeof(serv),
893e230dd2SCorentin Chary                            NI_NUMERICHOST | NI_NUMERICSERV)) != 0) {
903e230dd2SCorentin Chary         VNC_DEBUG("Cannot resolve address %d: %s\n",
913e230dd2SCorentin Chary                   err, gai_strerror(err));
923e230dd2SCorentin Chary         return NULL;
933e230dd2SCorentin Chary     }
943e230dd2SCorentin Chary 
953e230dd2SCorentin Chary     /* Enough for the existing format + the 2 vars we're
963e230dd2SCorentin Chary      * substituting in. */
973e230dd2SCorentin Chary     addrlen = strlen(format) + strlen(host) + strlen(serv);
987267c094SAnthony Liguori     addr = g_malloc(addrlen + 1);
993e230dd2SCorentin Chary     snprintf(addr, addrlen, format, host, serv);
1003e230dd2SCorentin Chary     addr[addrlen] = '\0';
1013e230dd2SCorentin Chary 
1023e230dd2SCorentin Chary     return addr;
1033e230dd2SCorentin Chary }
1043e230dd2SCorentin Chary 
1053e230dd2SCorentin Chary 
1063e230dd2SCorentin Chary char *vnc_socket_local_addr(const char *format, int fd) {
1073e230dd2SCorentin Chary     struct sockaddr_storage sa;
1083e230dd2SCorentin Chary     socklen_t salen;
1093e230dd2SCorentin Chary 
1103e230dd2SCorentin Chary     salen = sizeof(sa);
1113e230dd2SCorentin Chary     if (getsockname(fd, (struct sockaddr*)&sa, &salen) < 0)
1123e230dd2SCorentin Chary         return NULL;
1133e230dd2SCorentin Chary 
1143e230dd2SCorentin Chary     return addr_to_string(format, &sa, salen);
1153e230dd2SCorentin Chary }
1163e230dd2SCorentin Chary 
1173e230dd2SCorentin Chary char *vnc_socket_remote_addr(const char *format, int fd) {
1183e230dd2SCorentin Chary     struct sockaddr_storage sa;
1193e230dd2SCorentin Chary     socklen_t salen;
1203e230dd2SCorentin Chary 
1213e230dd2SCorentin Chary     salen = sizeof(sa);
1223e230dd2SCorentin Chary     if (getpeername(fd, (struct sockaddr*)&sa, &salen) < 0)
1233e230dd2SCorentin Chary         return NULL;
1243e230dd2SCorentin Chary 
1253e230dd2SCorentin Chary     return addr_to_string(format, &sa, salen);
1263e230dd2SCorentin Chary }
1273e230dd2SCorentin Chary 
128*fb6ba0d5SWenchao Xia static VncBasicInfo *vnc_basic_info_get(struct sockaddr_storage *sa,
1293e230dd2SCorentin Chary                                         socklen_t salen)
1303e230dd2SCorentin Chary {
131*fb6ba0d5SWenchao Xia     VncBasicInfo *info;
1323e230dd2SCorentin Chary     char host[NI_MAXHOST];
1333e230dd2SCorentin Chary     char serv[NI_MAXSERV];
1343e230dd2SCorentin Chary     int err;
1353e230dd2SCorentin Chary 
1363e230dd2SCorentin Chary     if ((err = getnameinfo((struct sockaddr *)sa, salen,
1373e230dd2SCorentin Chary                            host, sizeof(host),
1383e230dd2SCorentin Chary                            serv, sizeof(serv),
1393e230dd2SCorentin Chary                            NI_NUMERICHOST | NI_NUMERICSERV)) != 0) {
1403e230dd2SCorentin Chary         VNC_DEBUG("Cannot resolve address %d: %s\n",
1413e230dd2SCorentin Chary                   err, gai_strerror(err));
142*fb6ba0d5SWenchao Xia         return NULL;
1433e230dd2SCorentin Chary     }
1443e230dd2SCorentin Chary 
145*fb6ba0d5SWenchao Xia     info = g_malloc0(sizeof(VncBasicInfo));
146*fb6ba0d5SWenchao Xia     info->host = g_strdup(host);
147*fb6ba0d5SWenchao Xia     info->service = g_strdup(serv);
148*fb6ba0d5SWenchao Xia     info->family = inet_netfamily(sa->ss_family);
149*fb6ba0d5SWenchao Xia     return info;
1503e230dd2SCorentin Chary }
1513e230dd2SCorentin Chary 
152*fb6ba0d5SWenchao Xia static VncBasicInfo *vnc_basic_info_get_from_server_addr(int fd)
1533e230dd2SCorentin Chary {
1543e230dd2SCorentin Chary     struct sockaddr_storage sa;
1553e230dd2SCorentin Chary     socklen_t salen;
1563e230dd2SCorentin Chary 
1573e230dd2SCorentin Chary     salen = sizeof(sa);
1583e230dd2SCorentin Chary     if (getsockname(fd, (struct sockaddr*)&sa, &salen) < 0) {
159*fb6ba0d5SWenchao Xia         return NULL;
1603e230dd2SCorentin Chary     }
1613e230dd2SCorentin Chary 
162*fb6ba0d5SWenchao Xia     return vnc_basic_info_get(&sa, salen);
1633e230dd2SCorentin Chary }
1643e230dd2SCorentin Chary 
165*fb6ba0d5SWenchao Xia static VncBasicInfo *vnc_basic_info_get_from_remote_addr(int fd)
1663e230dd2SCorentin Chary {
1673e230dd2SCorentin Chary     struct sockaddr_storage sa;
1683e230dd2SCorentin Chary     socklen_t salen;
1693e230dd2SCorentin Chary 
1703e230dd2SCorentin Chary     salen = sizeof(sa);
1713e230dd2SCorentin Chary     if (getpeername(fd, (struct sockaddr*)&sa, &salen) < 0) {
172*fb6ba0d5SWenchao Xia         return NULL;
1733e230dd2SCorentin Chary     }
1743e230dd2SCorentin Chary 
175*fb6ba0d5SWenchao Xia     return vnc_basic_info_get(&sa, salen);
1763e230dd2SCorentin Chary }
1773e230dd2SCorentin Chary 
1783e230dd2SCorentin Chary static const char *vnc_auth_name(VncDisplay *vd) {
1793e230dd2SCorentin Chary     switch (vd->auth) {
1803e230dd2SCorentin Chary     case VNC_AUTH_INVALID:
1813e230dd2SCorentin Chary         return "invalid";
1823e230dd2SCorentin Chary     case VNC_AUTH_NONE:
1833e230dd2SCorentin Chary         return "none";
1843e230dd2SCorentin Chary     case VNC_AUTH_VNC:
1853e230dd2SCorentin Chary         return "vnc";
1863e230dd2SCorentin Chary     case VNC_AUTH_RA2:
1873e230dd2SCorentin Chary         return "ra2";
1883e230dd2SCorentin Chary     case VNC_AUTH_RA2NE:
1893e230dd2SCorentin Chary         return "ra2ne";
1903e230dd2SCorentin Chary     case VNC_AUTH_TIGHT:
1913e230dd2SCorentin Chary         return "tight";
1923e230dd2SCorentin Chary     case VNC_AUTH_ULTRA:
1933e230dd2SCorentin Chary         return "ultra";
1943e230dd2SCorentin Chary     case VNC_AUTH_TLS:
1953e230dd2SCorentin Chary         return "tls";
1963e230dd2SCorentin Chary     case VNC_AUTH_VENCRYPT:
1973e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS
1983e230dd2SCorentin Chary         switch (vd->subauth) {
1993e230dd2SCorentin Chary         case VNC_AUTH_VENCRYPT_PLAIN:
2003e230dd2SCorentin Chary             return "vencrypt+plain";
2013e230dd2SCorentin Chary         case VNC_AUTH_VENCRYPT_TLSNONE:
2023e230dd2SCorentin Chary             return "vencrypt+tls+none";
2033e230dd2SCorentin Chary         case VNC_AUTH_VENCRYPT_TLSVNC:
2043e230dd2SCorentin Chary             return "vencrypt+tls+vnc";
2053e230dd2SCorentin Chary         case VNC_AUTH_VENCRYPT_TLSPLAIN:
2063e230dd2SCorentin Chary             return "vencrypt+tls+plain";
2073e230dd2SCorentin Chary         case VNC_AUTH_VENCRYPT_X509NONE:
2083e230dd2SCorentin Chary             return "vencrypt+x509+none";
2093e230dd2SCorentin Chary         case VNC_AUTH_VENCRYPT_X509VNC:
2103e230dd2SCorentin Chary             return "vencrypt+x509+vnc";
2113e230dd2SCorentin Chary         case VNC_AUTH_VENCRYPT_X509PLAIN:
2123e230dd2SCorentin Chary             return "vencrypt+x509+plain";
2133e230dd2SCorentin Chary         case VNC_AUTH_VENCRYPT_TLSSASL:
2143e230dd2SCorentin Chary             return "vencrypt+tls+sasl";
2153e230dd2SCorentin Chary         case VNC_AUTH_VENCRYPT_X509SASL:
2163e230dd2SCorentin Chary             return "vencrypt+x509+sasl";
2173e230dd2SCorentin Chary         default:
2183e230dd2SCorentin Chary             return "vencrypt";
2193e230dd2SCorentin Chary         }
2203e230dd2SCorentin Chary #else
2213e230dd2SCorentin Chary         return "vencrypt";
2223e230dd2SCorentin Chary #endif
2233e230dd2SCorentin Chary     case VNC_AUTH_SASL:
2243e230dd2SCorentin Chary         return "sasl";
2253e230dd2SCorentin Chary     }
2263e230dd2SCorentin Chary     return "unknown";
2273e230dd2SCorentin Chary }
2283e230dd2SCorentin Chary 
229*fb6ba0d5SWenchao Xia static VncServerInfo *vnc_server_info_get(void)
2303e230dd2SCorentin Chary {
231*fb6ba0d5SWenchao Xia     VncServerInfo *info;
232*fb6ba0d5SWenchao Xia     VncBasicInfo *bi = vnc_basic_info_get_from_server_addr(vnc_display->lsock);
233*fb6ba0d5SWenchao Xia     if (!bi) {
234*fb6ba0d5SWenchao Xia         return NULL;
2353e230dd2SCorentin Chary     }
2363e230dd2SCorentin Chary 
237*fb6ba0d5SWenchao Xia     info = g_malloc(sizeof(*info));
238*fb6ba0d5SWenchao Xia     info->base = bi;
239*fb6ba0d5SWenchao Xia     info->has_auth = true;
240*fb6ba0d5SWenchao Xia     info->auth = g_strdup(vnc_auth_name(vnc_display));
241*fb6ba0d5SWenchao Xia     return info;
2423e230dd2SCorentin Chary }
2433e230dd2SCorentin Chary 
2443e230dd2SCorentin Chary static void vnc_client_cache_auth(VncState *client)
2453e230dd2SCorentin Chary {
2463e230dd2SCorentin Chary     if (!client->info) {
2473e230dd2SCorentin Chary         return;
2483e230dd2SCorentin Chary     }
2493e230dd2SCorentin Chary 
2503e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS
2513e230dd2SCorentin Chary     if (client->tls.session &&
2523e230dd2SCorentin Chary         client->tls.dname) {
253*fb6ba0d5SWenchao Xia         client->info->has_x509_dname = true;
254*fb6ba0d5SWenchao Xia         client->info->x509_dname = g_strdup(client->tls.dname);
2553e230dd2SCorentin Chary     }
2563e230dd2SCorentin Chary #endif
2573e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL
2583e230dd2SCorentin Chary     if (client->sasl.conn &&
2593e230dd2SCorentin Chary         client->sasl.username) {
260*fb6ba0d5SWenchao Xia         client->info->has_sasl_username = true;
261*fb6ba0d5SWenchao Xia         client->info->sasl_username = g_strdup(client->sasl.username);
2623e230dd2SCorentin Chary     }
2633e230dd2SCorentin Chary #endif
2643e230dd2SCorentin Chary }
2653e230dd2SCorentin Chary 
2663e230dd2SCorentin Chary static void vnc_client_cache_addr(VncState *client)
2673e230dd2SCorentin Chary {
268*fb6ba0d5SWenchao Xia     VncBasicInfo *bi = vnc_basic_info_get_from_remote_addr(client->csock);
2693e230dd2SCorentin Chary 
270*fb6ba0d5SWenchao Xia     if (bi) {
271*fb6ba0d5SWenchao Xia         client->info = g_malloc0(sizeof(*client->info));
272*fb6ba0d5SWenchao Xia         client->info->base = bi;
273*fb6ba0d5SWenchao Xia     }
2743e230dd2SCorentin Chary }
2753e230dd2SCorentin Chary 
276*fb6ba0d5SWenchao Xia static void vnc_qmp_event(VncState *vs, QAPIEvent event)
2773e230dd2SCorentin Chary {
278*fb6ba0d5SWenchao Xia     VncServerInfo *si;
2793e230dd2SCorentin Chary 
2803e230dd2SCorentin Chary     if (!vs->info) {
2813e230dd2SCorentin Chary         return;
2823e230dd2SCorentin Chary     }
283*fb6ba0d5SWenchao Xia     g_assert(vs->info->base);
2843e230dd2SCorentin Chary 
285*fb6ba0d5SWenchao Xia     si = vnc_server_info_get();
286*fb6ba0d5SWenchao Xia     if (!si) {
2873e230dd2SCorentin Chary         return;
2883e230dd2SCorentin Chary     }
2893e230dd2SCorentin Chary 
290*fb6ba0d5SWenchao Xia     switch (event) {
291*fb6ba0d5SWenchao Xia     case QAPI_EVENT_VNC_CONNECTED:
292*fb6ba0d5SWenchao Xia         qapi_event_send_vnc_connected(si, vs->info->base, &error_abort);
293*fb6ba0d5SWenchao Xia         break;
294*fb6ba0d5SWenchao Xia     case QAPI_EVENT_VNC_INITIALIZED:
295*fb6ba0d5SWenchao Xia         qapi_event_send_vnc_initialized(si, vs->info, &error_abort);
296*fb6ba0d5SWenchao Xia         break;
297*fb6ba0d5SWenchao Xia     case QAPI_EVENT_VNC_DISCONNECTED:
298*fb6ba0d5SWenchao Xia         qapi_event_send_vnc_disconnected(si, vs->info, &error_abort);
299*fb6ba0d5SWenchao Xia         break;
300*fb6ba0d5SWenchao Xia     default:
301*fb6ba0d5SWenchao Xia         break;
302*fb6ba0d5SWenchao Xia     }
3033e230dd2SCorentin Chary 
304*fb6ba0d5SWenchao Xia     qapi_free_VncServerInfo(si);
3053e230dd2SCorentin Chary }
3063e230dd2SCorentin Chary 
3072b54aa87SLuiz Capitulino static VncClientInfo *qmp_query_vnc_client(const VncState *client)
3083e230dd2SCorentin Chary {
3092b54aa87SLuiz Capitulino     struct sockaddr_storage sa;
3102b54aa87SLuiz Capitulino     socklen_t salen = sizeof(sa);
3112b54aa87SLuiz Capitulino     char host[NI_MAXHOST];
3122b54aa87SLuiz Capitulino     char serv[NI_MAXSERV];
3132b54aa87SLuiz Capitulino     VncClientInfo *info;
3143e230dd2SCorentin Chary 
3152b54aa87SLuiz Capitulino     if (getpeername(client->csock, (struct sockaddr *)&sa, &salen) < 0) {
3162b54aa87SLuiz Capitulino         return NULL;
3172b54aa87SLuiz Capitulino     }
3182b54aa87SLuiz Capitulino 
3192b54aa87SLuiz Capitulino     if (getnameinfo((struct sockaddr *)&sa, salen,
3202b54aa87SLuiz Capitulino                     host, sizeof(host),
3212b54aa87SLuiz Capitulino                     serv, sizeof(serv),
3222b54aa87SLuiz Capitulino                     NI_NUMERICHOST | NI_NUMERICSERV) < 0) {
3232b54aa87SLuiz Capitulino         return NULL;
3242b54aa87SLuiz Capitulino     }
3252b54aa87SLuiz Capitulino 
3262b54aa87SLuiz Capitulino     info = g_malloc0(sizeof(*info));
327a589569fSWenchao Xia     info->base = g_malloc0(sizeof(*info->base));
328a589569fSWenchao Xia     info->base->host = g_strdup(host);
329a589569fSWenchao Xia     info->base->service = g_strdup(serv);
330a589569fSWenchao Xia     info->base->family = inet_netfamily(sa.ss_family);
3313e230dd2SCorentin Chary 
3323e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS
3332b54aa87SLuiz Capitulino     if (client->tls.session && client->tls.dname) {
3342b54aa87SLuiz Capitulino         info->has_x509_dname = true;
3352b54aa87SLuiz Capitulino         info->x509_dname = g_strdup(client->tls.dname);
3362b54aa87SLuiz Capitulino     }
3373e230dd2SCorentin Chary #endif
3383e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL
3392b54aa87SLuiz Capitulino     if (client->sasl.conn && client->sasl.username) {
3402b54aa87SLuiz Capitulino         info->has_sasl_username = true;
3412b54aa87SLuiz Capitulino         info->sasl_username = g_strdup(client->sasl.username);
3422b54aa87SLuiz Capitulino     }
3433e230dd2SCorentin Chary #endif
3442b54aa87SLuiz Capitulino 
3452b54aa87SLuiz Capitulino     return info;
3463e230dd2SCorentin Chary }
3473e230dd2SCorentin Chary 
3482b54aa87SLuiz Capitulino VncInfo *qmp_query_vnc(Error **errp)
3493e230dd2SCorentin Chary {
3502b54aa87SLuiz Capitulino     VncInfo *info = g_malloc0(sizeof(*info));
3513e230dd2SCorentin Chary 
3523e230dd2SCorentin Chary     if (vnc_display == NULL || vnc_display->display == NULL) {
3532b54aa87SLuiz Capitulino         info->enabled = false;
3543e230dd2SCorentin Chary     } else {
3552b54aa87SLuiz Capitulino         VncClientInfoList *cur_item = NULL;
3562b54aa87SLuiz Capitulino         struct sockaddr_storage sa;
3572b54aa87SLuiz Capitulino         socklen_t salen = sizeof(sa);
3582b54aa87SLuiz Capitulino         char host[NI_MAXHOST];
3592b54aa87SLuiz Capitulino         char serv[NI_MAXSERV];
3603e230dd2SCorentin Chary         VncState *client;
3613e230dd2SCorentin Chary 
3622b54aa87SLuiz Capitulino         info->enabled = true;
3632b54aa87SLuiz Capitulino 
3642b54aa87SLuiz Capitulino         /* for compatibility with the original command */
3652b54aa87SLuiz Capitulino         info->has_clients = true;
3662b54aa87SLuiz Capitulino 
3673e230dd2SCorentin Chary         QTAILQ_FOREACH(client, &vnc_display->clients, next) {
3682b54aa87SLuiz Capitulino             VncClientInfoList *cinfo = g_malloc0(sizeof(*info));
3692b54aa87SLuiz Capitulino             cinfo->value = qmp_query_vnc_client(client);
3702b54aa87SLuiz Capitulino 
3712b54aa87SLuiz Capitulino             /* XXX: waiting for the qapi to support GSList */
3722b54aa87SLuiz Capitulino             if (!cur_item) {
3732b54aa87SLuiz Capitulino                 info->clients = cur_item = cinfo;
3742b54aa87SLuiz Capitulino             } else {
3752b54aa87SLuiz Capitulino                 cur_item->next = cinfo;
3762b54aa87SLuiz Capitulino                 cur_item = cinfo;
3773e230dd2SCorentin Chary             }
3783e230dd2SCorentin Chary         }
3793e230dd2SCorentin Chary 
380417b0b88SPaolo Bonzini         if (vnc_display->lsock == -1) {
381417b0b88SPaolo Bonzini             return info;
382417b0b88SPaolo Bonzini         }
383417b0b88SPaolo Bonzini 
3842b54aa87SLuiz Capitulino         if (getsockname(vnc_display->lsock, (struct sockaddr *)&sa,
3852b54aa87SLuiz Capitulino                         &salen) == -1) {
3862b54aa87SLuiz Capitulino             error_set(errp, QERR_UNDEFINED_ERROR);
3872b54aa87SLuiz Capitulino             goto out_error;
3882b54aa87SLuiz Capitulino         }
3893e230dd2SCorentin Chary 
3902b54aa87SLuiz Capitulino         if (getnameinfo((struct sockaddr *)&sa, salen,
3912b54aa87SLuiz Capitulino                         host, sizeof(host),
3922b54aa87SLuiz Capitulino                         serv, sizeof(serv),
3932b54aa87SLuiz Capitulino                         NI_NUMERICHOST | NI_NUMERICSERV) < 0) {
3942b54aa87SLuiz Capitulino             error_set(errp, QERR_UNDEFINED_ERROR);
3952b54aa87SLuiz Capitulino             goto out_error;
3963e230dd2SCorentin Chary         }
3972b54aa87SLuiz Capitulino 
3982b54aa87SLuiz Capitulino         info->has_host = true;
3992b54aa87SLuiz Capitulino         info->host = g_strdup(host);
4002b54aa87SLuiz Capitulino 
4012b54aa87SLuiz Capitulino         info->has_service = true;
4022b54aa87SLuiz Capitulino         info->service = g_strdup(serv);
4032b54aa87SLuiz Capitulino 
4042b54aa87SLuiz Capitulino         info->has_family = true;
405a589569fSWenchao Xia         info->family = inet_netfamily(sa.ss_family);
4062b54aa87SLuiz Capitulino 
4072b54aa87SLuiz Capitulino         info->has_auth = true;
4082b54aa87SLuiz Capitulino         info->auth = g_strdup(vnc_auth_name(vnc_display));
4093e230dd2SCorentin Chary     }
4102b54aa87SLuiz Capitulino 
4112b54aa87SLuiz Capitulino     return info;
4122b54aa87SLuiz Capitulino 
4132b54aa87SLuiz Capitulino out_error:
4142b54aa87SLuiz Capitulino     qapi_free_VncInfo(info);
4152b54aa87SLuiz Capitulino     return NULL;
4163e230dd2SCorentin Chary }
4173e230dd2SCorentin Chary 
4183e230dd2SCorentin Chary /* TODO
4193e230dd2SCorentin Chary    1) Get the queue working for IO.
4203e230dd2SCorentin Chary    2) there is some weirdness when using the -S option (the screen is grey
4213e230dd2SCorentin Chary       and not totally invalidated
4223e230dd2SCorentin Chary    3) resolutions > 1024
4233e230dd2SCorentin Chary */
4243e230dd2SCorentin Chary 
42538ee14f4SGerd Hoffmann static int vnc_update_client(VncState *vs, int has_dirty, bool sync);
4263e230dd2SCorentin Chary static void vnc_disconnect_start(VncState *vs);
4273e230dd2SCorentin Chary 
4283e230dd2SCorentin Chary static void vnc_colordepth(VncState *vs);
4293e230dd2SCorentin Chary static void framebuffer_update_request(VncState *vs, int incremental,
4303e230dd2SCorentin Chary                                        int x_position, int y_position,
4313e230dd2SCorentin Chary                                        int w, int h);
4320f7b2864SGerd Hoffmann static void vnc_refresh(DisplayChangeListener *dcl);
4333e230dd2SCorentin Chary static int vnc_refresh_server_surface(VncDisplay *vd);
4343e230dd2SCorentin Chary 
4357c20b4a3SGerd Hoffmann static void vnc_dpy_update(DisplayChangeListener *dcl,
4367c20b4a3SGerd Hoffmann                            int x, int y, int w, int h)
4373e230dd2SCorentin Chary {
43821ef45d7SGerd Hoffmann     VncDisplay *vd = container_of(dcl, VncDisplay, dcl);
4393e230dd2SCorentin Chary     struct VncSurface *s = &vd->guest;
440d39fa6d8SGerd Hoffmann     int width = surface_width(vd->ds);
441d39fa6d8SGerd Hoffmann     int height = surface_height(vd->ds);
4423e230dd2SCorentin Chary 
44391937225SPeter Lieven     /* this is needed this to ensure we updated all affected
44491937225SPeter Lieven      * blocks if x % VNC_DIRTY_PIXELS_PER_BIT != 0 */
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;
45191937225SPeter Lieven     h = MIN(y + h, height);
4523e230dd2SCorentin Chary 
453b4c85ddcSPeter Lieven     for (; y < h; y++) {
45491937225SPeter Lieven         bitmap_set(s->dirty[y], x / VNC_DIRTY_PIXELS_PER_BIT,
45591937225SPeter Lieven                    DIV_ROUND_UP(w, VNC_DIRTY_PIXELS_PER_BIT));
456b4c85ddcSPeter Lieven     }
4573e230dd2SCorentin Chary }
4583e230dd2SCorentin Chary 
4593e230dd2SCorentin Chary void vnc_framebuffer_update(VncState *vs, int x, int y, int w, int h,
4603e230dd2SCorentin Chary                             int32_t encoding)
4613e230dd2SCorentin Chary {
4623e230dd2SCorentin Chary     vnc_write_u16(vs, x);
4633e230dd2SCorentin Chary     vnc_write_u16(vs, y);
4643e230dd2SCorentin Chary     vnc_write_u16(vs, w);
4653e230dd2SCorentin Chary     vnc_write_u16(vs, h);
4663e230dd2SCorentin Chary 
4673e230dd2SCorentin Chary     vnc_write_s32(vs, encoding);
4683e230dd2SCorentin Chary }
4693e230dd2SCorentin Chary 
4703e230dd2SCorentin Chary void buffer_reserve(Buffer *buffer, size_t len)
4713e230dd2SCorentin Chary {
4723e230dd2SCorentin Chary     if ((buffer->capacity - buffer->offset) < len) {
4733e230dd2SCorentin Chary         buffer->capacity += (len + 1024);
4747267c094SAnthony Liguori         buffer->buffer = g_realloc(buffer->buffer, buffer->capacity);
4753e230dd2SCorentin Chary         if (buffer->buffer == NULL) {
4763e230dd2SCorentin Chary             fprintf(stderr, "vnc: out of memory\n");
4773e230dd2SCorentin Chary             exit(1);
4783e230dd2SCorentin Chary         }
4793e230dd2SCorentin Chary     }
4803e230dd2SCorentin Chary }
4813e230dd2SCorentin Chary 
48271a8cdecSBlue Swirl static int buffer_empty(Buffer *buffer)
4833e230dd2SCorentin Chary {
4843e230dd2SCorentin Chary     return buffer->offset == 0;
4853e230dd2SCorentin Chary }
4863e230dd2SCorentin Chary 
4877536ee4bSTim Hardeck uint8_t *buffer_end(Buffer *buffer)
4883e230dd2SCorentin Chary {
4893e230dd2SCorentin Chary     return buffer->buffer + buffer->offset;
4903e230dd2SCorentin Chary }
4913e230dd2SCorentin Chary 
4923e230dd2SCorentin Chary void buffer_reset(Buffer *buffer)
4933e230dd2SCorentin Chary {
4943e230dd2SCorentin Chary         buffer->offset = 0;
4953e230dd2SCorentin Chary }
4963e230dd2SCorentin Chary 
4973e230dd2SCorentin Chary void buffer_free(Buffer *buffer)
4983e230dd2SCorentin Chary {
4997267c094SAnthony Liguori     g_free(buffer->buffer);
5003e230dd2SCorentin Chary     buffer->offset = 0;
5013e230dd2SCorentin Chary     buffer->capacity = 0;
5023e230dd2SCorentin Chary     buffer->buffer = NULL;
5033e230dd2SCorentin Chary }
5043e230dd2SCorentin Chary 
5053e230dd2SCorentin Chary void buffer_append(Buffer *buffer, const void *data, size_t len)
5063e230dd2SCorentin Chary {
5073e230dd2SCorentin Chary     memcpy(buffer->buffer + buffer->offset, data, len);
5083e230dd2SCorentin Chary     buffer->offset += len;
5093e230dd2SCorentin Chary }
5103e230dd2SCorentin Chary 
51132ed2680STim Hardeck void buffer_advance(Buffer *buf, size_t len)
51232ed2680STim Hardeck {
51332ed2680STim Hardeck     memmove(buf->buffer, buf->buffer + len,
51432ed2680STim Hardeck             (buf->offset - len));
51532ed2680STim Hardeck     buf->offset -= len;
51632ed2680STim Hardeck }
51732ed2680STim Hardeck 
5183e230dd2SCorentin Chary static void vnc_desktop_resize(VncState *vs)
5193e230dd2SCorentin Chary {
520d39fa6d8SGerd Hoffmann     DisplaySurface *ds = vs->vd->ds;
5213e230dd2SCorentin Chary 
5223e230dd2SCorentin Chary     if (vs->csock == -1 || !vnc_has_feature(vs, VNC_FEATURE_RESIZE)) {
5233e230dd2SCorentin Chary         return;
5243e230dd2SCorentin Chary     }
525d39fa6d8SGerd Hoffmann     if (vs->client_width == surface_width(ds) &&
526d39fa6d8SGerd Hoffmann         vs->client_height == surface_height(ds)) {
5273e230dd2SCorentin Chary         return;
5283e230dd2SCorentin Chary     }
529d39fa6d8SGerd Hoffmann     vs->client_width = surface_width(ds);
530d39fa6d8SGerd Hoffmann     vs->client_height = surface_height(ds);
531bd023f95SCorentin Chary     vnc_lock_output(vs);
5323e230dd2SCorentin Chary     vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE);
5333e230dd2SCorentin Chary     vnc_write_u8(vs, 0);
5343e230dd2SCorentin Chary     vnc_write_u16(vs, 1); /* number of rects */
5353e230dd2SCorentin Chary     vnc_framebuffer_update(vs, 0, 0, vs->client_width, vs->client_height,
5363e230dd2SCorentin Chary                            VNC_ENCODING_DESKTOPRESIZE);
537bd023f95SCorentin Chary     vnc_unlock_output(vs);
5383e230dd2SCorentin Chary     vnc_flush(vs);
5393e230dd2SCorentin Chary }
5403e230dd2SCorentin Chary 
541bd023f95SCorentin Chary static void vnc_abort_display_jobs(VncDisplay *vd)
542bd023f95SCorentin Chary {
543bd023f95SCorentin Chary     VncState *vs;
544bd023f95SCorentin Chary 
545bd023f95SCorentin Chary     QTAILQ_FOREACH(vs, &vd->clients, next) {
546bd023f95SCorentin Chary         vnc_lock_output(vs);
547bd023f95SCorentin Chary         vs->abort = true;
548bd023f95SCorentin Chary         vnc_unlock_output(vs);
549bd023f95SCorentin Chary     }
550bd023f95SCorentin Chary     QTAILQ_FOREACH(vs, &vd->clients, next) {
551bd023f95SCorentin Chary         vnc_jobs_join(vs);
552bd023f95SCorentin Chary     }
553bd023f95SCorentin Chary     QTAILQ_FOREACH(vs, &vd->clients, next) {
554bd023f95SCorentin Chary         vnc_lock_output(vs);
555bd023f95SCorentin Chary         vs->abort = false;
556bd023f95SCorentin Chary         vnc_unlock_output(vs);
557bd023f95SCorentin Chary     }
558bd023f95SCorentin Chary }
559bd023f95SCorentin Chary 
5609f64916dSGerd Hoffmann int vnc_server_fb_stride(VncDisplay *vd)
5619f64916dSGerd Hoffmann {
5629f64916dSGerd Hoffmann     return pixman_image_get_stride(vd->server);
5639f64916dSGerd Hoffmann }
5649f64916dSGerd Hoffmann 
5659f64916dSGerd Hoffmann void *vnc_server_fb_ptr(VncDisplay *vd, int x, int y)
5669f64916dSGerd Hoffmann {
5679f64916dSGerd Hoffmann     uint8_t *ptr;
5689f64916dSGerd Hoffmann 
5699f64916dSGerd Hoffmann     ptr  = (uint8_t *)pixman_image_get_data(vd->server);
5709f64916dSGerd Hoffmann     ptr += y * vnc_server_fb_stride(vd);
5719f64916dSGerd Hoffmann     ptr += x * VNC_SERVER_FB_BYTES;
5729f64916dSGerd Hoffmann     return ptr;
5739f64916dSGerd Hoffmann }
57412b316d4SPeter Lieven /* this sets only the visible pixels of a dirty bitmap */
57512b316d4SPeter Lieven #define VNC_SET_VISIBLE_PIXELS_DIRTY(bitmap, w, h) {\
57612b316d4SPeter Lieven         int y;\
57712b316d4SPeter Lieven         memset(bitmap, 0x00, sizeof(bitmap));\
57812b316d4SPeter Lieven         for (y = 0; y < h; y++) {\
57912b316d4SPeter Lieven             bitmap_set(bitmap[y], 0,\
58012b316d4SPeter Lieven                        DIV_ROUND_UP(w, VNC_DIRTY_PIXELS_PER_BIT));\
58112b316d4SPeter Lieven         } \
58212b316d4SPeter Lieven     }
5839f64916dSGerd Hoffmann 
584c12aeb86SGerd Hoffmann static void vnc_dpy_switch(DisplayChangeListener *dcl,
585c12aeb86SGerd Hoffmann                            DisplaySurface *surface)
5863e230dd2SCorentin Chary {
58721ef45d7SGerd Hoffmann     VncDisplay *vd = container_of(dcl, VncDisplay, dcl);
5883e230dd2SCorentin Chary     VncState *vs;
5893e230dd2SCorentin Chary 
590bd023f95SCorentin Chary     vnc_abort_display_jobs(vd);
591bd023f95SCorentin Chary 
5923e230dd2SCorentin Chary     /* server surface */
5939f64916dSGerd Hoffmann     qemu_pixman_image_unref(vd->server);
594d39fa6d8SGerd Hoffmann     vd->ds = surface;
5959f64916dSGerd Hoffmann     vd->server = pixman_image_create_bits(VNC_SERVER_FB_FORMAT,
596d39fa6d8SGerd Hoffmann                                           surface_width(vd->ds),
597d39fa6d8SGerd Hoffmann                                           surface_height(vd->ds),
5989f64916dSGerd Hoffmann                                           NULL, 0);
5993e230dd2SCorentin Chary 
6003e230dd2SCorentin Chary     /* guest surface */
6019f64916dSGerd Hoffmann #if 0 /* FIXME */
6023e230dd2SCorentin Chary     if (ds_get_bytes_per_pixel(ds) != vd->guest.ds->pf.bytes_per_pixel)
6033e230dd2SCorentin Chary         console_color_init(ds);
6049f64916dSGerd Hoffmann #endif
6059f64916dSGerd Hoffmann     qemu_pixman_image_unref(vd->guest.fb);
606d39fa6d8SGerd Hoffmann     vd->guest.fb = pixman_image_ref(surface->image);
607d39fa6d8SGerd Hoffmann     vd->guest.format = surface->format;
60812b316d4SPeter Lieven     VNC_SET_VISIBLE_PIXELS_DIRTY(vd->guest.dirty,
60912b316d4SPeter Lieven                                  surface_width(vd->ds),
61012b316d4SPeter Lieven                                  surface_height(vd->ds));
6113e230dd2SCorentin Chary 
6123e230dd2SCorentin Chary     QTAILQ_FOREACH(vs, &vd->clients, next) {
6133e230dd2SCorentin Chary         vnc_colordepth(vs);
6143e230dd2SCorentin Chary         vnc_desktop_resize(vs);
6153e230dd2SCorentin Chary         if (vs->vd->cursor) {
6163e230dd2SCorentin Chary             vnc_cursor_define(vs);
6173e230dd2SCorentin Chary         }
61812b316d4SPeter Lieven         VNC_SET_VISIBLE_PIXELS_DIRTY(vs->dirty,
61912b316d4SPeter Lieven                                      surface_width(vd->ds),
62012b316d4SPeter Lieven                                      surface_height(vd->ds));
6213e230dd2SCorentin Chary     }
6223e230dd2SCorentin Chary }
6233e230dd2SCorentin Chary 
6243e230dd2SCorentin Chary /* fastest code */
6259f64916dSGerd Hoffmann static void vnc_write_pixels_copy(VncState *vs,
6263e230dd2SCorentin Chary                                   void *pixels, int size)
6273e230dd2SCorentin Chary {
6283e230dd2SCorentin Chary     vnc_write(vs, pixels, size);
6293e230dd2SCorentin Chary }
6303e230dd2SCorentin Chary 
6313e230dd2SCorentin Chary /* slowest but generic code. */
6323e230dd2SCorentin Chary void vnc_convert_pixel(VncState *vs, uint8_t *buf, uint32_t v)
6333e230dd2SCorentin Chary {
6343e230dd2SCorentin Chary     uint8_t r, g, b;
6353e230dd2SCorentin Chary 
6369f64916dSGerd Hoffmann #if VNC_SERVER_FB_FORMAT == PIXMAN_FORMAT(32, PIXMAN_TYPE_ARGB, 0, 8, 8, 8)
6379f64916dSGerd Hoffmann     r = (((v & 0x00ff0000) >> 16) << vs->client_pf.rbits) >> 8;
6389f64916dSGerd Hoffmann     g = (((v & 0x0000ff00) >>  8) << vs->client_pf.gbits) >> 8;
6399f64916dSGerd Hoffmann     b = (((v & 0x000000ff) >>  0) << vs->client_pf.bbits) >> 8;
6409f64916dSGerd Hoffmann #else
6419f64916dSGerd Hoffmann # error need some bits here if you change VNC_SERVER_FB_FORMAT
6429f64916dSGerd Hoffmann #endif
6439f64916dSGerd Hoffmann     v = (r << vs->client_pf.rshift) |
6449f64916dSGerd Hoffmann         (g << vs->client_pf.gshift) |
6459f64916dSGerd Hoffmann         (b << vs->client_pf.bshift);
6469f64916dSGerd Hoffmann     switch (vs->client_pf.bytes_per_pixel) {
6473e230dd2SCorentin Chary     case 1:
6483e230dd2SCorentin Chary         buf[0] = v;
6493e230dd2SCorentin Chary         break;
6503e230dd2SCorentin Chary     case 2:
6519f64916dSGerd Hoffmann         if (vs->client_be) {
6523e230dd2SCorentin Chary             buf[0] = v >> 8;
6533e230dd2SCorentin Chary             buf[1] = v;
6543e230dd2SCorentin Chary         } else {
6553e230dd2SCorentin Chary             buf[1] = v >> 8;
6563e230dd2SCorentin Chary             buf[0] = v;
6573e230dd2SCorentin Chary         }
6583e230dd2SCorentin Chary         break;
6593e230dd2SCorentin Chary     default:
6603e230dd2SCorentin Chary     case 4:
6619f64916dSGerd Hoffmann         if (vs->client_be) {
6623e230dd2SCorentin Chary             buf[0] = v >> 24;
6633e230dd2SCorentin Chary             buf[1] = v >> 16;
6643e230dd2SCorentin Chary             buf[2] = v >> 8;
6653e230dd2SCorentin Chary             buf[3] = v;
6663e230dd2SCorentin Chary         } else {
6673e230dd2SCorentin Chary             buf[3] = v >> 24;
6683e230dd2SCorentin Chary             buf[2] = v >> 16;
6693e230dd2SCorentin Chary             buf[1] = v >> 8;
6703e230dd2SCorentin Chary             buf[0] = v;
6713e230dd2SCorentin Chary         }
6723e230dd2SCorentin Chary         break;
6733e230dd2SCorentin Chary     }
6743e230dd2SCorentin Chary }
6753e230dd2SCorentin Chary 
6769f64916dSGerd Hoffmann static void vnc_write_pixels_generic(VncState *vs,
6773e230dd2SCorentin Chary                                      void *pixels1, int size)
6783e230dd2SCorentin Chary {
6793e230dd2SCorentin Chary     uint8_t buf[4];
6803e230dd2SCorentin Chary 
6819f64916dSGerd Hoffmann     if (VNC_SERVER_FB_BYTES == 4) {
6823e230dd2SCorentin Chary         uint32_t *pixels = pixels1;
6833e230dd2SCorentin Chary         int n, i;
6843e230dd2SCorentin Chary         n = size >> 2;
6853e230dd2SCorentin Chary         for (i = 0; i < n; i++) {
6863e230dd2SCorentin Chary             vnc_convert_pixel(vs, buf, pixels[i]);
6879f64916dSGerd Hoffmann             vnc_write(vs, buf, vs->client_pf.bytes_per_pixel);
6883e230dd2SCorentin Chary         }
6893e230dd2SCorentin Chary     }
6903e230dd2SCorentin Chary }
6913e230dd2SCorentin Chary 
6923e230dd2SCorentin Chary int vnc_raw_send_framebuffer_update(VncState *vs, int x, int y, int w, int h)
6933e230dd2SCorentin Chary {
6943e230dd2SCorentin Chary     int i;
6953e230dd2SCorentin Chary     uint8_t *row;
6963e230dd2SCorentin Chary     VncDisplay *vd = vs->vd;
6973e230dd2SCorentin Chary 
6989f64916dSGerd Hoffmann     row = vnc_server_fb_ptr(vd, x, y);
6993e230dd2SCorentin Chary     for (i = 0; i < h; i++) {
7009f64916dSGerd Hoffmann         vs->write_pixels(vs, row, w * VNC_SERVER_FB_BYTES);
7019f64916dSGerd Hoffmann         row += vnc_server_fb_stride(vd);
7023e230dd2SCorentin Chary     }
7033e230dd2SCorentin Chary     return 1;
7043e230dd2SCorentin Chary }
7053e230dd2SCorentin Chary 
706bd023f95SCorentin Chary int vnc_send_framebuffer_update(VncState *vs, int x, int y, int w, int h)
7073e230dd2SCorentin Chary {
7083e230dd2SCorentin Chary     int n = 0;
7093e230dd2SCorentin Chary 
7103e230dd2SCorentin Chary     switch(vs->vnc_encoding) {
7113e230dd2SCorentin Chary         case VNC_ENCODING_ZLIB:
7123e230dd2SCorentin Chary             n = vnc_zlib_send_framebuffer_update(vs, x, y, w, h);
7133e230dd2SCorentin Chary             break;
7143e230dd2SCorentin Chary         case VNC_ENCODING_HEXTILE:
7153e230dd2SCorentin Chary             vnc_framebuffer_update(vs, x, y, w, h, VNC_ENCODING_HEXTILE);
7163e230dd2SCorentin Chary             n = vnc_hextile_send_framebuffer_update(vs, x, y, w, h);
7173e230dd2SCorentin Chary             break;
7183e230dd2SCorentin Chary         case VNC_ENCODING_TIGHT:
7193e230dd2SCorentin Chary             n = vnc_tight_send_framebuffer_update(vs, x, y, w, h);
7203e230dd2SCorentin Chary             break;
721efe556adSCorentin Chary         case VNC_ENCODING_TIGHT_PNG:
722efe556adSCorentin Chary             n = vnc_tight_png_send_framebuffer_update(vs, x, y, w, h);
723efe556adSCorentin Chary             break;
724148954faSCorentin Chary         case VNC_ENCODING_ZRLE:
725148954faSCorentin Chary             n = vnc_zrle_send_framebuffer_update(vs, x, y, w, h);
726148954faSCorentin Chary             break;
727148954faSCorentin Chary         case VNC_ENCODING_ZYWRLE:
728148954faSCorentin Chary             n = vnc_zywrle_send_framebuffer_update(vs, x, y, w, h);
729148954faSCorentin Chary             break;
7303e230dd2SCorentin Chary         default:
7313e230dd2SCorentin Chary             vnc_framebuffer_update(vs, x, y, w, h, VNC_ENCODING_RAW);
7323e230dd2SCorentin Chary             n = vnc_raw_send_framebuffer_update(vs, x, y, w, h);
7333e230dd2SCorentin Chary             break;
7343e230dd2SCorentin Chary     }
7353e230dd2SCorentin Chary     return n;
7363e230dd2SCorentin Chary }
7373e230dd2SCorentin Chary 
7383e230dd2SCorentin Chary static void vnc_copy(VncState *vs, int src_x, int src_y, int dst_x, int dst_y, int w, int h)
7393e230dd2SCorentin Chary {
7403e230dd2SCorentin Chary     /* send bitblit op to the vnc client */
741bd023f95SCorentin Chary     vnc_lock_output(vs);
7423e230dd2SCorentin Chary     vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE);
7433e230dd2SCorentin Chary     vnc_write_u8(vs, 0);
7443e230dd2SCorentin Chary     vnc_write_u16(vs, 1); /* number of rects */
7453e230dd2SCorentin Chary     vnc_framebuffer_update(vs, dst_x, dst_y, w, h, VNC_ENCODING_COPYRECT);
7463e230dd2SCorentin Chary     vnc_write_u16(vs, src_x);
7473e230dd2SCorentin Chary     vnc_write_u16(vs, src_y);
748bd023f95SCorentin Chary     vnc_unlock_output(vs);
7493e230dd2SCorentin Chary     vnc_flush(vs);
7503e230dd2SCorentin Chary }
7513e230dd2SCorentin Chary 
7527c20b4a3SGerd Hoffmann static void vnc_dpy_copy(DisplayChangeListener *dcl,
7537c20b4a3SGerd Hoffmann                          int src_x, int src_y,
7547c20b4a3SGerd Hoffmann                          int dst_x, int dst_y, int w, int h)
7553e230dd2SCorentin Chary {
75621ef45d7SGerd Hoffmann     VncDisplay *vd = container_of(dcl, VncDisplay, dcl);
7573e230dd2SCorentin Chary     VncState *vs, *vn;
7583e230dd2SCorentin Chary     uint8_t *src_row;
7593e230dd2SCorentin Chary     uint8_t *dst_row;
7609f64916dSGerd Hoffmann     int i, x, y, pitch, inc, w_lim, s;
7613e230dd2SCorentin Chary     int cmp_bytes;
7623e230dd2SCorentin Chary 
7633e230dd2SCorentin Chary     vnc_refresh_server_surface(vd);
7643e230dd2SCorentin Chary     QTAILQ_FOREACH_SAFE(vs, &vd->clients, next, vn) {
7653e230dd2SCorentin Chary         if (vnc_has_feature(vs, VNC_FEATURE_COPYRECT)) {
7663e230dd2SCorentin Chary             vs->force_update = 1;
76738ee14f4SGerd Hoffmann             vnc_update_client(vs, 1, true);
7683e230dd2SCorentin Chary             /* vs might be free()ed here */
7693e230dd2SCorentin Chary         }
7703e230dd2SCorentin Chary     }
7713e230dd2SCorentin Chary 
7723e230dd2SCorentin Chary     /* do bitblit op on the local surface too */
7739f64916dSGerd Hoffmann     pitch = vnc_server_fb_stride(vd);
7749f64916dSGerd Hoffmann     src_row = vnc_server_fb_ptr(vd, src_x, src_y);
7759f64916dSGerd Hoffmann     dst_row = vnc_server_fb_ptr(vd, dst_x, dst_y);
7763e230dd2SCorentin Chary     y = dst_y;
7773e230dd2SCorentin Chary     inc = 1;
7783e230dd2SCorentin Chary     if (dst_y > src_y) {
7793e230dd2SCorentin Chary         /* copy backwards */
7803e230dd2SCorentin Chary         src_row += pitch * (h-1);
7813e230dd2SCorentin Chary         dst_row += pitch * (h-1);
7823e230dd2SCorentin Chary         pitch = -pitch;
7833e230dd2SCorentin Chary         y = dst_y + h - 1;
7843e230dd2SCorentin Chary         inc = -1;
7853e230dd2SCorentin Chary     }
786b4c85ddcSPeter Lieven     w_lim = w - (VNC_DIRTY_PIXELS_PER_BIT - (dst_x % VNC_DIRTY_PIXELS_PER_BIT));
787b4c85ddcSPeter Lieven     if (w_lim < 0) {
7883e230dd2SCorentin Chary         w_lim = w;
789b4c85ddcSPeter Lieven     } else {
790b4c85ddcSPeter Lieven         w_lim = w - (w_lim % VNC_DIRTY_PIXELS_PER_BIT);
791b4c85ddcSPeter Lieven     }
7923e230dd2SCorentin Chary     for (i = 0; i < h; i++) {
7933e230dd2SCorentin Chary         for (x = 0; x <= w_lim;
7943e230dd2SCorentin Chary                 x += s, src_row += cmp_bytes, dst_row += cmp_bytes) {
7953e230dd2SCorentin Chary             if (x == w_lim) {
7963e230dd2SCorentin Chary                 if ((s = w - w_lim) == 0)
7973e230dd2SCorentin Chary                     break;
7983e230dd2SCorentin Chary             } else if (!x) {
799b4c85ddcSPeter Lieven                 s = (VNC_DIRTY_PIXELS_PER_BIT -
800b4c85ddcSPeter Lieven                     (dst_x % VNC_DIRTY_PIXELS_PER_BIT));
8013e230dd2SCorentin Chary                 s = MIN(s, w_lim);
8023e230dd2SCorentin Chary             } else {
803b4c85ddcSPeter Lieven                 s = VNC_DIRTY_PIXELS_PER_BIT;
8043e230dd2SCorentin Chary             }
8059f64916dSGerd Hoffmann             cmp_bytes = s * VNC_SERVER_FB_BYTES;
8063e230dd2SCorentin Chary             if (memcmp(src_row, dst_row, cmp_bytes) == 0)
8073e230dd2SCorentin Chary                 continue;
8083e230dd2SCorentin Chary             memmove(dst_row, src_row, cmp_bytes);
8093e230dd2SCorentin Chary             QTAILQ_FOREACH(vs, &vd->clients, next) {
8103e230dd2SCorentin Chary                 if (!vnc_has_feature(vs, VNC_FEATURE_COPYRECT)) {
811b4c85ddcSPeter Lieven                     set_bit(((x + dst_x) / VNC_DIRTY_PIXELS_PER_BIT),
812b4c85ddcSPeter Lieven                             vs->dirty[y]);
8133e230dd2SCorentin Chary                 }
8143e230dd2SCorentin Chary             }
8153e230dd2SCorentin Chary         }
8169f64916dSGerd Hoffmann         src_row += pitch - w * VNC_SERVER_FB_BYTES;
8179f64916dSGerd Hoffmann         dst_row += pitch - w * VNC_SERVER_FB_BYTES;
8183e230dd2SCorentin Chary         y += inc;
8193e230dd2SCorentin Chary     }
8203e230dd2SCorentin Chary 
8213e230dd2SCorentin Chary     QTAILQ_FOREACH(vs, &vd->clients, next) {
8223e230dd2SCorentin Chary         if (vnc_has_feature(vs, VNC_FEATURE_COPYRECT)) {
8233e230dd2SCorentin Chary             vnc_copy(vs, src_x, src_y, dst_x, dst_y, w, h);
8243e230dd2SCorentin Chary         }
8253e230dd2SCorentin Chary     }
8263e230dd2SCorentin Chary }
8273e230dd2SCorentin Chary 
8287c20b4a3SGerd Hoffmann static void vnc_mouse_set(DisplayChangeListener *dcl,
8297c20b4a3SGerd Hoffmann                           int x, int y, int visible)
8303e230dd2SCorentin Chary {
8313e230dd2SCorentin Chary     /* can we ask the client(s) to move the pointer ??? */
8323e230dd2SCorentin Chary }
8333e230dd2SCorentin Chary 
8343e230dd2SCorentin Chary static int vnc_cursor_define(VncState *vs)
8353e230dd2SCorentin Chary {
8363e230dd2SCorentin Chary     QEMUCursor *c = vs->vd->cursor;
8373e230dd2SCorentin Chary     int isize;
8383e230dd2SCorentin Chary 
8393e230dd2SCorentin Chary     if (vnc_has_feature(vs, VNC_FEATURE_RICH_CURSOR)) {
840d01f9595SCorentin Chary         vnc_lock_output(vs);
8413e230dd2SCorentin Chary         vnc_write_u8(vs,  VNC_MSG_SERVER_FRAMEBUFFER_UPDATE);
8423e230dd2SCorentin Chary         vnc_write_u8(vs,  0);  /*  padding     */
8433e230dd2SCorentin Chary         vnc_write_u16(vs, 1);  /*  # of rects  */
8443e230dd2SCorentin Chary         vnc_framebuffer_update(vs, c->hot_x, c->hot_y, c->width, c->height,
8453e230dd2SCorentin Chary                                VNC_ENCODING_RICH_CURSOR);
8469f64916dSGerd Hoffmann         isize = c->width * c->height * vs->client_pf.bytes_per_pixel;
8479f64916dSGerd Hoffmann         vnc_write_pixels_generic(vs, c->data, isize);
8483e230dd2SCorentin Chary         vnc_write(vs, vs->vd->cursor_mask, vs->vd->cursor_msize);
849d01f9595SCorentin Chary         vnc_unlock_output(vs);
8503e230dd2SCorentin Chary         return 0;
8513e230dd2SCorentin Chary     }
8523e230dd2SCorentin Chary     return -1;
8533e230dd2SCorentin Chary }
8543e230dd2SCorentin Chary 
8557c20b4a3SGerd Hoffmann static void vnc_dpy_cursor_define(DisplayChangeListener *dcl,
8567c20b4a3SGerd Hoffmann                                   QEMUCursor *c)
8573e230dd2SCorentin Chary {
8583e230dd2SCorentin Chary     VncDisplay *vd = vnc_display;
8593e230dd2SCorentin Chary     VncState *vs;
8603e230dd2SCorentin Chary 
8613e230dd2SCorentin Chary     cursor_put(vd->cursor);
8627267c094SAnthony Liguori     g_free(vd->cursor_mask);
8633e230dd2SCorentin Chary 
8643e230dd2SCorentin Chary     vd->cursor = c;
8653e230dd2SCorentin Chary     cursor_get(vd->cursor);
8663e230dd2SCorentin Chary     vd->cursor_msize = cursor_get_mono_bpl(c) * c->height;
8677267c094SAnthony Liguori     vd->cursor_mask = g_malloc0(vd->cursor_msize);
8683e230dd2SCorentin Chary     cursor_get_mono_mask(c, 0, vd->cursor_mask);
8693e230dd2SCorentin Chary 
8703e230dd2SCorentin Chary     QTAILQ_FOREACH(vs, &vd->clients, next) {
8713e230dd2SCorentin Chary         vnc_cursor_define(vs);
8723e230dd2SCorentin Chary     }
8733e230dd2SCorentin Chary }
8743e230dd2SCorentin Chary 
8753e230dd2SCorentin Chary static int find_and_clear_dirty_height(struct VncState *vs,
8766c71a539SCorentin Chary                                        int y, int last_x, int x, int height)
8773e230dd2SCorentin Chary {
8783e230dd2SCorentin Chary     int h;
8793e230dd2SCorentin Chary 
8806c71a539SCorentin Chary     for (h = 1; h < (height - y); h++) {
881bc2429b9SCorentin Chary         if (!test_bit(last_x, vs->dirty[y + h])) {
8823e230dd2SCorentin Chary             break;
883bc2429b9SCorentin Chary         }
884863d7c91SPeter Lieven         bitmap_clear(vs->dirty[y + h], last_x, x - last_x);
8853e230dd2SCorentin Chary     }
8863e230dd2SCorentin Chary 
8873e230dd2SCorentin Chary     return h;
8883e230dd2SCorentin Chary }
8893e230dd2SCorentin Chary 
89038ee14f4SGerd Hoffmann static int vnc_update_client(VncState *vs, int has_dirty, bool sync)
8913e230dd2SCorentin Chary {
8923e230dd2SCorentin Chary     if (vs->need_update && vs->csock != -1) {
8933e230dd2SCorentin Chary         VncDisplay *vd = vs->vd;
894bd023f95SCorentin Chary         VncJob *job;
8953e230dd2SCorentin Chary         int y;
8962f487a3dSPeter Lieven         int height, width;
897bd023f95SCorentin Chary         int n = 0;
898bd023f95SCorentin Chary 
8993e230dd2SCorentin Chary         if (vs->output.offset && !vs->audio_cap && !vs->force_update)
9003e230dd2SCorentin Chary             /* kernel send buffers are full -> drop frames to throttle */
9013e230dd2SCorentin Chary             return 0;
9023e230dd2SCorentin Chary 
9033e230dd2SCorentin Chary         if (!has_dirty && !vs->audio_cap && !vs->force_update)
9043e230dd2SCorentin Chary             return 0;
9053e230dd2SCorentin Chary 
9063e230dd2SCorentin Chary         /*
9073e230dd2SCorentin Chary          * Send screen updates to the vnc client using the server
9083e230dd2SCorentin Chary          * surface and server dirty map.  guest surface updates
9093e230dd2SCorentin Chary          * happening in parallel don't disturb us, the next pass will
9103e230dd2SCorentin Chary          * send them to the client.
9113e230dd2SCorentin Chary          */
912bd023f95SCorentin Chary         job = vnc_job_new(vs);
9133e230dd2SCorentin Chary 
9149f64916dSGerd Hoffmann         height = MIN(pixman_image_get_height(vd->server), vs->client_height);
9152f487a3dSPeter Lieven         width = MIN(pixman_image_get_width(vd->server), vs->client_width);
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);
9342f487a3dSPeter Lieven             x2 = MIN(x2, width / VNC_DIRTY_PIXELS_PER_BIT);
9352f487a3dSPeter Lieven             if (x2 > x) {
93612b316d4SPeter Lieven                 n += vnc_job_add_rect(job, x * VNC_DIRTY_PIXELS_PER_BIT, y,
93712b316d4SPeter Lieven                                       (x2 - x) * VNC_DIRTY_PIXELS_PER_BIT, h);
9383e230dd2SCorentin Chary             }
9392f487a3dSPeter Lieven         }
940bd023f95SCorentin Chary 
941bd023f95SCorentin Chary         vnc_job_push(job);
942eb214ff8SGerd Hoffmann         if (sync) {
943eb214ff8SGerd Hoffmann             vnc_jobs_join(vs);
944eb214ff8SGerd Hoffmann         }
9453e230dd2SCorentin Chary         vs->force_update = 0;
946bd023f95SCorentin Chary         return n;
9473e230dd2SCorentin Chary     }
9483e230dd2SCorentin Chary 
94938ee14f4SGerd Hoffmann     if (vs->csock == -1) {
9503e230dd2SCorentin Chary         vnc_disconnect_finish(vs);
95138ee14f4SGerd Hoffmann     } else if (sync) {
95238ee14f4SGerd Hoffmann         vnc_jobs_join(vs);
95338ee14f4SGerd Hoffmann     }
9543e230dd2SCorentin Chary 
9553e230dd2SCorentin Chary     return 0;
9563e230dd2SCorentin Chary }
9573e230dd2SCorentin Chary 
9583e230dd2SCorentin Chary /* audio */
9593e230dd2SCorentin Chary static void audio_capture_notify(void *opaque, audcnotification_e cmd)
9603e230dd2SCorentin Chary {
9613e230dd2SCorentin Chary     VncState *vs = opaque;
9623e230dd2SCorentin Chary 
9633e230dd2SCorentin Chary     switch (cmd) {
9643e230dd2SCorentin Chary     case AUD_CNOTIFY_DISABLE:
965bd023f95SCorentin Chary         vnc_lock_output(vs);
9663e230dd2SCorentin Chary         vnc_write_u8(vs, VNC_MSG_SERVER_QEMU);
9673e230dd2SCorentin Chary         vnc_write_u8(vs, VNC_MSG_SERVER_QEMU_AUDIO);
9683e230dd2SCorentin Chary         vnc_write_u16(vs, VNC_MSG_SERVER_QEMU_AUDIO_END);
969bd023f95SCorentin Chary         vnc_unlock_output(vs);
9703e230dd2SCorentin Chary         vnc_flush(vs);
9713e230dd2SCorentin Chary         break;
9723e230dd2SCorentin Chary 
9733e230dd2SCorentin Chary     case AUD_CNOTIFY_ENABLE:
974bd023f95SCorentin Chary         vnc_lock_output(vs);
9753e230dd2SCorentin Chary         vnc_write_u8(vs, VNC_MSG_SERVER_QEMU);
9763e230dd2SCorentin Chary         vnc_write_u8(vs, VNC_MSG_SERVER_QEMU_AUDIO);
9773e230dd2SCorentin Chary         vnc_write_u16(vs, VNC_MSG_SERVER_QEMU_AUDIO_BEGIN);
978bd023f95SCorentin Chary         vnc_unlock_output(vs);
9793e230dd2SCorentin Chary         vnc_flush(vs);
9803e230dd2SCorentin Chary         break;
9813e230dd2SCorentin Chary     }
9823e230dd2SCorentin Chary }
9833e230dd2SCorentin Chary 
9843e230dd2SCorentin Chary static void audio_capture_destroy(void *opaque)
9853e230dd2SCorentin Chary {
9863e230dd2SCorentin Chary }
9873e230dd2SCorentin Chary 
9883e230dd2SCorentin Chary static void audio_capture(void *opaque, void *buf, int size)
9893e230dd2SCorentin Chary {
9903e230dd2SCorentin Chary     VncState *vs = opaque;
9913e230dd2SCorentin Chary 
992bd023f95SCorentin Chary     vnc_lock_output(vs);
9933e230dd2SCorentin Chary     vnc_write_u8(vs, VNC_MSG_SERVER_QEMU);
9943e230dd2SCorentin Chary     vnc_write_u8(vs, VNC_MSG_SERVER_QEMU_AUDIO);
9953e230dd2SCorentin Chary     vnc_write_u16(vs, VNC_MSG_SERVER_QEMU_AUDIO_DATA);
9963e230dd2SCorentin Chary     vnc_write_u32(vs, size);
9973e230dd2SCorentin Chary     vnc_write(vs, buf, size);
998bd023f95SCorentin Chary     vnc_unlock_output(vs);
9993e230dd2SCorentin Chary     vnc_flush(vs);
10003e230dd2SCorentin Chary }
10013e230dd2SCorentin Chary 
10023e230dd2SCorentin Chary static void audio_add(VncState *vs)
10033e230dd2SCorentin Chary {
10043e230dd2SCorentin Chary     struct audio_capture_ops ops;
10053e230dd2SCorentin Chary 
10063e230dd2SCorentin Chary     if (vs->audio_cap) {
1007027a79c3SCole Robinson         error_report("audio already running");
10083e230dd2SCorentin Chary         return;
10093e230dd2SCorentin Chary     }
10103e230dd2SCorentin Chary 
10113e230dd2SCorentin Chary     ops.notify = audio_capture_notify;
10123e230dd2SCorentin Chary     ops.destroy = audio_capture_destroy;
10133e230dd2SCorentin Chary     ops.capture = audio_capture;
10143e230dd2SCorentin Chary 
10153e230dd2SCorentin Chary     vs->audio_cap = AUD_add_capture(&vs->as, &ops, vs);
10163e230dd2SCorentin Chary     if (!vs->audio_cap) {
1017027a79c3SCole Robinson         error_report("Failed to add audio capture");
10183e230dd2SCorentin Chary     }
10193e230dd2SCorentin Chary }
10203e230dd2SCorentin Chary 
10213e230dd2SCorentin Chary static void audio_del(VncState *vs)
10223e230dd2SCorentin Chary {
10233e230dd2SCorentin Chary     if (vs->audio_cap) {
10243e230dd2SCorentin Chary         AUD_del_capture(vs->audio_cap, vs);
10253e230dd2SCorentin Chary         vs->audio_cap = NULL;
10263e230dd2SCorentin Chary     }
10273e230dd2SCorentin Chary }
10283e230dd2SCorentin Chary 
10293e230dd2SCorentin Chary static void vnc_disconnect_start(VncState *vs)
10303e230dd2SCorentin Chary {
10313e230dd2SCorentin Chary     if (vs->csock == -1)
10323e230dd2SCorentin Chary         return;
10338cf36489SGerd Hoffmann     vnc_set_share_mode(vs, VNC_SHARE_MODE_DISCONNECTED);
10343e230dd2SCorentin Chary     qemu_set_fd_handler2(vs->csock, NULL, NULL, NULL, NULL);
10353e230dd2SCorentin Chary     closesocket(vs->csock);
10363e230dd2SCorentin Chary     vs->csock = -1;
10373e230dd2SCorentin Chary }
10383e230dd2SCorentin Chary 
10397536ee4bSTim Hardeck void vnc_disconnect_finish(VncState *vs)
10403e230dd2SCorentin Chary {
10417d964c9dSCorentin Chary     int i;
10427d964c9dSCorentin Chary 
1043bd023f95SCorentin Chary     vnc_jobs_join(vs); /* Wait encoding jobs */
1044bd023f95SCorentin Chary 
1045bd023f95SCorentin Chary     vnc_lock_output(vs);
1046*fb6ba0d5SWenchao Xia     vnc_qmp_event(vs, QAPI_EVENT_VNC_DISCONNECTED);
10473e230dd2SCorentin Chary 
10483e230dd2SCorentin Chary     buffer_free(&vs->input);
10493e230dd2SCorentin Chary     buffer_free(&vs->output);
10507536ee4bSTim Hardeck #ifdef CONFIG_VNC_WS
10517536ee4bSTim Hardeck     buffer_free(&vs->ws_input);
10527536ee4bSTim Hardeck     buffer_free(&vs->ws_output);
10537536ee4bSTim Hardeck #endif /* CONFIG_VNC_WS */
10543e230dd2SCorentin Chary 
1055*fb6ba0d5SWenchao Xia     qapi_free_VncClientInfo(vs->info);
10563e230dd2SCorentin Chary 
10573e230dd2SCorentin Chary     vnc_zlib_clear(vs);
10583e230dd2SCorentin Chary     vnc_tight_clear(vs);
1059148954faSCorentin Chary     vnc_zrle_clear(vs);
10603e230dd2SCorentin Chary 
10613e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS
10623e230dd2SCorentin Chary     vnc_tls_client_cleanup(vs);
10633e230dd2SCorentin Chary #endif /* CONFIG_VNC_TLS */
10643e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL
10653e230dd2SCorentin Chary     vnc_sasl_client_cleanup(vs);
10663e230dd2SCorentin Chary #endif /* CONFIG_VNC_SASL */
10673e230dd2SCorentin Chary     audio_del(vs);
10687bc9318bSGerd Hoffmann     vnc_release_modifiers(vs);
10693e230dd2SCorentin Chary 
10706fd8e79aSTim Hardeck     if (vs->initialized) {
10713e230dd2SCorentin Chary         QTAILQ_REMOVE(&vs->vd->clients, vs, next);
10726fd8e79aSTim Hardeck         qemu_remove_mouse_mode_change_notifier(&vs->mouse_mode_notifier);
10736fd8e79aSTim Hardeck     }
10743e230dd2SCorentin Chary 
10753e230dd2SCorentin Chary     if (vs->vd->lock_key_sync)
10763e230dd2SCorentin Chary         qemu_remove_led_event_handler(vs->led);
1077bd023f95SCorentin Chary     vnc_unlock_output(vs);
1078bd023f95SCorentin Chary 
1079bd023f95SCorentin Chary     qemu_mutex_destroy(&vs->output_mutex);
10806fd8e79aSTim Hardeck     if (vs->bh != NULL) {
1081175b2a6eSCorentin Chary         qemu_bh_delete(vs->bh);
10826fd8e79aSTim Hardeck     }
1083175b2a6eSCorentin Chary     buffer_free(&vs->jobs_buffer);
1084175b2a6eSCorentin Chary 
10857d964c9dSCorentin Chary     for (i = 0; i < VNC_STAT_ROWS; ++i) {
10867267c094SAnthony Liguori         g_free(vs->lossy_rect[i]);
10877d964c9dSCorentin Chary     }
10887267c094SAnthony Liguori     g_free(vs->lossy_rect);
10897267c094SAnthony Liguori     g_free(vs);
10903e230dd2SCorentin Chary }
10913e230dd2SCorentin Chary 
10923e230dd2SCorentin Chary int vnc_client_io_error(VncState *vs, int ret, int last_errno)
10933e230dd2SCorentin Chary {
10943e230dd2SCorentin Chary     if (ret == 0 || ret == -1) {
10953e230dd2SCorentin Chary         if (ret == -1) {
10963e230dd2SCorentin Chary             switch (last_errno) {
10973e230dd2SCorentin Chary                 case EINTR:
10983e230dd2SCorentin Chary                 case EAGAIN:
10993e230dd2SCorentin Chary #ifdef _WIN32
11003e230dd2SCorentin Chary                 case WSAEWOULDBLOCK:
11013e230dd2SCorentin Chary #endif
11023e230dd2SCorentin Chary                     return 0;
11033e230dd2SCorentin Chary                 default:
11043e230dd2SCorentin Chary                     break;
11053e230dd2SCorentin Chary             }
11063e230dd2SCorentin Chary         }
11073e230dd2SCorentin Chary 
11083e230dd2SCorentin Chary         VNC_DEBUG("Closing down client sock: ret %d, errno %d\n",
11093e230dd2SCorentin Chary                   ret, ret < 0 ? last_errno : 0);
11103e230dd2SCorentin Chary         vnc_disconnect_start(vs);
11113e230dd2SCorentin Chary 
11123e230dd2SCorentin Chary         return 0;
11133e230dd2SCorentin Chary     }
11143e230dd2SCorentin Chary     return ret;
11153e230dd2SCorentin Chary }
11163e230dd2SCorentin Chary 
11173e230dd2SCorentin Chary 
11183e230dd2SCorentin Chary void vnc_client_error(VncState *vs)
11193e230dd2SCorentin Chary {
11203e230dd2SCorentin Chary     VNC_DEBUG("Closing down client sock: protocol error\n");
11213e230dd2SCorentin Chary     vnc_disconnect_start(vs);
11223e230dd2SCorentin Chary }
11233e230dd2SCorentin Chary 
11240057a0d5STim Hardeck #ifdef CONFIG_VNC_TLS
11250057a0d5STim Hardeck static long vnc_client_write_tls(gnutls_session_t *session,
11260057a0d5STim Hardeck                                  const uint8_t *data,
11270057a0d5STim Hardeck                                  size_t datalen)
11280057a0d5STim Hardeck {
11290057a0d5STim Hardeck     long ret = gnutls_write(*session, data, datalen);
11300057a0d5STim Hardeck     if (ret < 0) {
11310057a0d5STim Hardeck         if (ret == GNUTLS_E_AGAIN) {
11320057a0d5STim Hardeck             errno = EAGAIN;
11330057a0d5STim Hardeck         } else {
11340057a0d5STim Hardeck             errno = EIO;
11350057a0d5STim Hardeck         }
11360057a0d5STim Hardeck         ret = -1;
11370057a0d5STim Hardeck     }
11380057a0d5STim Hardeck     return ret;
11390057a0d5STim Hardeck }
11400057a0d5STim Hardeck #endif /* CONFIG_VNC_TLS */
11413e230dd2SCorentin Chary 
11423e230dd2SCorentin Chary /*
11433e230dd2SCorentin Chary  * Called to write a chunk of data to the client socket. The data may
11443e230dd2SCorentin Chary  * be the raw data, or may have already been encoded by SASL.
11453e230dd2SCorentin Chary  * The data will be written either straight onto the socket, or
11463e230dd2SCorentin Chary  * written via the GNUTLS wrappers, if TLS/SSL encryption is enabled
11473e230dd2SCorentin Chary  *
11483e230dd2SCorentin Chary  * NB, it is theoretically possible to have 2 layers of encryption,
11493e230dd2SCorentin Chary  * both SASL, and this TLS layer. It is highly unlikely in practice
11503e230dd2SCorentin Chary  * though, since SASL encryption will typically be a no-op if TLS
11513e230dd2SCorentin Chary  * is active
11523e230dd2SCorentin Chary  *
11533e230dd2SCorentin Chary  * Returns the number of bytes written, which may be less than
11543e230dd2SCorentin Chary  * the requested 'datalen' if the socket would block. Returns
11553e230dd2SCorentin Chary  * -1 on error, and disconnects the client socket.
11563e230dd2SCorentin Chary  */
11573e230dd2SCorentin Chary long vnc_client_write_buf(VncState *vs, const uint8_t *data, size_t datalen)
11583e230dd2SCorentin Chary {
11593e230dd2SCorentin Chary     long ret;
11603e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS
11613e230dd2SCorentin Chary     if (vs->tls.session) {
11620057a0d5STim Hardeck         ret = vnc_client_write_tls(&vs->tls.session, data, datalen);
11630057a0d5STim Hardeck     } else {
11640057a0d5STim Hardeck #ifdef CONFIG_VNC_WS
11650057a0d5STim Hardeck         if (vs->ws_tls.session) {
11660057a0d5STim Hardeck             ret = vnc_client_write_tls(&vs->ws_tls.session, data, datalen);
11673e230dd2SCorentin Chary         } else
11680057a0d5STim Hardeck #endif /* CONFIG_VNC_WS */
11693e230dd2SCorentin Chary #endif /* CONFIG_VNC_TLS */
11700057a0d5STim Hardeck         {
11713e230dd2SCorentin Chary             ret = send(vs->csock, (const void *)data, datalen, 0);
11720057a0d5STim Hardeck         }
11730057a0d5STim Hardeck #ifdef CONFIG_VNC_TLS
11740057a0d5STim Hardeck     }
11750057a0d5STim Hardeck #endif /* CONFIG_VNC_TLS */
11763e230dd2SCorentin Chary     VNC_DEBUG("Wrote wire %p %zd -> %ld\n", data, datalen, ret);
11773e230dd2SCorentin Chary     return vnc_client_io_error(vs, ret, socket_error());
11783e230dd2SCorentin Chary }
11793e230dd2SCorentin Chary 
11803e230dd2SCorentin Chary 
11813e230dd2SCorentin Chary /*
11823e230dd2SCorentin Chary  * Called to write buffered data to the client socket, when not
11833e230dd2SCorentin Chary  * using any SASL SSF encryption layers. Will write as much data
11843e230dd2SCorentin Chary  * as possible without blocking. If all buffered data is written,
11853e230dd2SCorentin Chary  * will switch the FD poll() handler back to read monitoring.
11863e230dd2SCorentin Chary  *
11873e230dd2SCorentin Chary  * Returns the number of bytes written, which may be less than
11883e230dd2SCorentin Chary  * the buffered output data if the socket would block. Returns
11893e230dd2SCorentin Chary  * -1 on error, and disconnects the client socket.
11903e230dd2SCorentin Chary  */
11913e230dd2SCorentin Chary static long vnc_client_write_plain(VncState *vs)
11923e230dd2SCorentin Chary {
11933e230dd2SCorentin Chary     long ret;
11943e230dd2SCorentin Chary 
11953e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL
11963e230dd2SCorentin Chary     VNC_DEBUG("Write Plain: Pending output %p size %zd offset %zd. Wait SSF %d\n",
11973e230dd2SCorentin Chary               vs->output.buffer, vs->output.capacity, vs->output.offset,
11983e230dd2SCorentin Chary               vs->sasl.waitWriteSSF);
11993e230dd2SCorentin Chary 
12003e230dd2SCorentin Chary     if (vs->sasl.conn &&
12013e230dd2SCorentin Chary         vs->sasl.runSSF &&
12023e230dd2SCorentin Chary         vs->sasl.waitWriteSSF) {
12033e230dd2SCorentin Chary         ret = vnc_client_write_buf(vs, vs->output.buffer, vs->sasl.waitWriteSSF);
12043e230dd2SCorentin Chary         if (ret)
12053e230dd2SCorentin Chary             vs->sasl.waitWriteSSF -= ret;
12063e230dd2SCorentin Chary     } else
12073e230dd2SCorentin Chary #endif /* CONFIG_VNC_SASL */
12083e230dd2SCorentin Chary         ret = vnc_client_write_buf(vs, vs->output.buffer, vs->output.offset);
12093e230dd2SCorentin Chary     if (!ret)
12103e230dd2SCorentin Chary         return 0;
12113e230dd2SCorentin Chary 
121232ed2680STim Hardeck     buffer_advance(&vs->output, ret);
12133e230dd2SCorentin Chary 
12143e230dd2SCorentin Chary     if (vs->output.offset == 0) {
12153e230dd2SCorentin Chary         qemu_set_fd_handler2(vs->csock, NULL, vnc_client_read, NULL, vs);
12163e230dd2SCorentin Chary     }
12173e230dd2SCorentin Chary 
12183e230dd2SCorentin Chary     return ret;
12193e230dd2SCorentin Chary }
12203e230dd2SCorentin Chary 
12213e230dd2SCorentin Chary 
12223e230dd2SCorentin Chary /*
12233e230dd2SCorentin Chary  * First function called whenever there is data to be written to
12243e230dd2SCorentin Chary  * the client socket. Will delegate actual work according to whether
12253e230dd2SCorentin Chary  * SASL SSF layers are enabled (thus requiring encryption calls)
12263e230dd2SCorentin Chary  */
1227bd023f95SCorentin Chary static void vnc_client_write_locked(void *opaque)
12283e230dd2SCorentin Chary {
12293e230dd2SCorentin Chary     VncState *vs = opaque;
12303e230dd2SCorentin Chary 
12313e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL
12323e230dd2SCorentin Chary     if (vs->sasl.conn &&
12333e230dd2SCorentin Chary         vs->sasl.runSSF &&
12343e230dd2SCorentin Chary         !vs->sasl.waitWriteSSF) {
12353e230dd2SCorentin Chary         vnc_client_write_sasl(vs);
12363e230dd2SCorentin Chary     } else
12373e230dd2SCorentin Chary #endif /* CONFIG_VNC_SASL */
12387536ee4bSTim Hardeck     {
12397536ee4bSTim Hardeck #ifdef CONFIG_VNC_WS
12407536ee4bSTim Hardeck         if (vs->encode_ws) {
12417536ee4bSTim Hardeck             vnc_client_write_ws(vs);
12427536ee4bSTim Hardeck         } else
12437536ee4bSTim Hardeck #endif /* CONFIG_VNC_WS */
12447536ee4bSTim Hardeck         {
12453e230dd2SCorentin Chary             vnc_client_write_plain(vs);
12463e230dd2SCorentin Chary         }
12477536ee4bSTim Hardeck     }
12487536ee4bSTim Hardeck }
12493e230dd2SCorentin Chary 
1250bd023f95SCorentin Chary void vnc_client_write(void *opaque)
1251bd023f95SCorentin Chary {
1252bd023f95SCorentin Chary     VncState *vs = opaque;
1253bd023f95SCorentin Chary 
1254bd023f95SCorentin Chary     vnc_lock_output(vs);
12557536ee4bSTim Hardeck     if (vs->output.offset
12567536ee4bSTim Hardeck #ifdef CONFIG_VNC_WS
12577536ee4bSTim Hardeck             || vs->ws_output.offset
12587536ee4bSTim Hardeck #endif
12597536ee4bSTim Hardeck             ) {
1260bd023f95SCorentin Chary         vnc_client_write_locked(opaque);
1261ac71103dSYoshiaki Tamura     } else if (vs->csock != -1) {
1262bd023f95SCorentin Chary         qemu_set_fd_handler2(vs->csock, NULL, vnc_client_read, NULL, vs);
1263bd023f95SCorentin Chary     }
1264bd023f95SCorentin Chary     vnc_unlock_output(vs);
1265bd023f95SCorentin Chary }
1266bd023f95SCorentin Chary 
12673e230dd2SCorentin Chary void vnc_read_when(VncState *vs, VncReadEvent *func, size_t expecting)
12683e230dd2SCorentin Chary {
12693e230dd2SCorentin Chary     vs->read_handler = func;
12703e230dd2SCorentin Chary     vs->read_handler_expect = expecting;
12713e230dd2SCorentin Chary }
12723e230dd2SCorentin Chary 
12730057a0d5STim Hardeck #ifdef CONFIG_VNC_TLS
12740057a0d5STim Hardeck static long vnc_client_read_tls(gnutls_session_t *session, uint8_t *data,
12750057a0d5STim Hardeck                                 size_t datalen)
12760057a0d5STim Hardeck {
12770057a0d5STim Hardeck     long ret = gnutls_read(*session, data, datalen);
12780057a0d5STim Hardeck     if (ret < 0) {
12790057a0d5STim Hardeck         if (ret == GNUTLS_E_AGAIN) {
12800057a0d5STim Hardeck             errno = EAGAIN;
12810057a0d5STim Hardeck         } else {
12820057a0d5STim Hardeck             errno = EIO;
12830057a0d5STim Hardeck         }
12840057a0d5STim Hardeck         ret = -1;
12850057a0d5STim Hardeck     }
12860057a0d5STim Hardeck     return ret;
12870057a0d5STim Hardeck }
12880057a0d5STim Hardeck #endif /* CONFIG_VNC_TLS */
12893e230dd2SCorentin Chary 
12903e230dd2SCorentin Chary /*
12913e230dd2SCorentin Chary  * Called to read a chunk of data from the client socket. The data may
12923e230dd2SCorentin Chary  * be the raw data, or may need to be further decoded by SASL.
12933e230dd2SCorentin Chary  * The data will be read either straight from to the socket, or
12943e230dd2SCorentin Chary  * read via the GNUTLS wrappers, if TLS/SSL encryption is enabled
12953e230dd2SCorentin Chary  *
12963e230dd2SCorentin Chary  * NB, it is theoretically possible to have 2 layers of encryption,
12973e230dd2SCorentin Chary  * both SASL, and this TLS layer. It is highly unlikely in practice
12983e230dd2SCorentin Chary  * though, since SASL encryption will typically be a no-op if TLS
12993e230dd2SCorentin Chary  * is active
13003e230dd2SCorentin Chary  *
13013e230dd2SCorentin Chary  * Returns the number of bytes read, which may be less than
13023e230dd2SCorentin Chary  * the requested 'datalen' if the socket would block. Returns
13033e230dd2SCorentin Chary  * -1 on error, and disconnects the client socket.
13043e230dd2SCorentin Chary  */
13053e230dd2SCorentin Chary long vnc_client_read_buf(VncState *vs, uint8_t *data, size_t datalen)
13063e230dd2SCorentin Chary {
13073e230dd2SCorentin Chary     long ret;
13083e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS
13093e230dd2SCorentin Chary     if (vs->tls.session) {
13100057a0d5STim Hardeck         ret = vnc_client_read_tls(&vs->tls.session, data, datalen);
13110057a0d5STim Hardeck     } else {
13120057a0d5STim Hardeck #ifdef CONFIG_VNC_WS
13130057a0d5STim Hardeck         if (vs->ws_tls.session) {
13140057a0d5STim Hardeck             ret = vnc_client_read_tls(&vs->ws_tls.session, data, datalen);
13153e230dd2SCorentin Chary         } else
13160057a0d5STim Hardeck #endif /* CONFIG_VNC_WS */
13173e230dd2SCorentin Chary #endif /* CONFIG_VNC_TLS */
13180057a0d5STim Hardeck         {
131900aa0040SBlue Swirl             ret = qemu_recv(vs->csock, data, datalen, 0);
13200057a0d5STim Hardeck         }
13210057a0d5STim Hardeck #ifdef CONFIG_VNC_TLS
13220057a0d5STim Hardeck     }
13230057a0d5STim Hardeck #endif /* CONFIG_VNC_TLS */
13243e230dd2SCorentin Chary     VNC_DEBUG("Read wire %p %zd -> %ld\n", data, datalen, ret);
13253e230dd2SCorentin Chary     return vnc_client_io_error(vs, ret, socket_error());
13263e230dd2SCorentin Chary }
13273e230dd2SCorentin Chary 
13283e230dd2SCorentin Chary 
13293e230dd2SCorentin Chary /*
13303e230dd2SCorentin Chary  * Called to read data from the client socket to the input buffer,
13313e230dd2SCorentin Chary  * when not using any SASL SSF encryption layers. Will read as much
13323e230dd2SCorentin Chary  * data as possible without blocking.
13333e230dd2SCorentin Chary  *
13343e230dd2SCorentin Chary  * Returns the number of bytes read. Returns -1 on error, and
13353e230dd2SCorentin Chary  * disconnects the client socket.
13363e230dd2SCorentin Chary  */
13373e230dd2SCorentin Chary static long vnc_client_read_plain(VncState *vs)
13383e230dd2SCorentin Chary {
13393e230dd2SCorentin Chary     int ret;
13403e230dd2SCorentin Chary     VNC_DEBUG("Read plain %p size %zd offset %zd\n",
13413e230dd2SCorentin Chary               vs->input.buffer, vs->input.capacity, vs->input.offset);
13423e230dd2SCorentin Chary     buffer_reserve(&vs->input, 4096);
13433e230dd2SCorentin Chary     ret = vnc_client_read_buf(vs, buffer_end(&vs->input), 4096);
13443e230dd2SCorentin Chary     if (!ret)
13453e230dd2SCorentin Chary         return 0;
13463e230dd2SCorentin Chary     vs->input.offset += ret;
13473e230dd2SCorentin Chary     return ret;
13483e230dd2SCorentin Chary }
13493e230dd2SCorentin Chary 
1350175b2a6eSCorentin Chary static void vnc_jobs_bh(void *opaque)
1351175b2a6eSCorentin Chary {
1352175b2a6eSCorentin Chary     VncState *vs = opaque;
1353175b2a6eSCorentin Chary 
1354175b2a6eSCorentin Chary     vnc_jobs_consume_buffer(vs);
1355175b2a6eSCorentin Chary }
13563e230dd2SCorentin Chary 
13573e230dd2SCorentin Chary /*
13583e230dd2SCorentin Chary  * First function called whenever there is more data to be read from
13593e230dd2SCorentin Chary  * the client socket. Will delegate actual work according to whether
13603e230dd2SCorentin Chary  * SASL SSF layers are enabled (thus requiring decryption calls)
13613e230dd2SCorentin Chary  */
13623e230dd2SCorentin Chary void vnc_client_read(void *opaque)
13633e230dd2SCorentin Chary {
13643e230dd2SCorentin Chary     VncState *vs = opaque;
13653e230dd2SCorentin Chary     long ret;
13663e230dd2SCorentin Chary 
13673e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL
13683e230dd2SCorentin Chary     if (vs->sasl.conn && vs->sasl.runSSF)
13693e230dd2SCorentin Chary         ret = vnc_client_read_sasl(vs);
13703e230dd2SCorentin Chary     else
13713e230dd2SCorentin Chary #endif /* CONFIG_VNC_SASL */
13727536ee4bSTim Hardeck #ifdef CONFIG_VNC_WS
13737536ee4bSTim Hardeck         if (vs->encode_ws) {
13747536ee4bSTim Hardeck             ret = vnc_client_read_ws(vs);
13757536ee4bSTim Hardeck             if (ret == -1) {
13767536ee4bSTim Hardeck                 vnc_disconnect_start(vs);
13777536ee4bSTim Hardeck                 return;
13787536ee4bSTim Hardeck             } else if (ret == -2) {
13797536ee4bSTim Hardeck                 vnc_client_error(vs);
13807536ee4bSTim Hardeck                 return;
13817536ee4bSTim Hardeck             }
13827536ee4bSTim Hardeck         } else
13837536ee4bSTim Hardeck #endif /* CONFIG_VNC_WS */
13847536ee4bSTim Hardeck         {
13853e230dd2SCorentin Chary         ret = vnc_client_read_plain(vs);
13867536ee4bSTim Hardeck         }
13873e230dd2SCorentin Chary     if (!ret) {
13883e230dd2SCorentin Chary         if (vs->csock == -1)
13893e230dd2SCorentin Chary             vnc_disconnect_finish(vs);
13903e230dd2SCorentin Chary         return;
13913e230dd2SCorentin Chary     }
13923e230dd2SCorentin Chary 
13933e230dd2SCorentin Chary     while (vs->read_handler && vs->input.offset >= vs->read_handler_expect) {
13943e230dd2SCorentin Chary         size_t len = vs->read_handler_expect;
13953e230dd2SCorentin Chary         int ret;
13963e230dd2SCorentin Chary 
13973e230dd2SCorentin Chary         ret = vs->read_handler(vs, vs->input.buffer, len);
13983e230dd2SCorentin Chary         if (vs->csock == -1) {
13993e230dd2SCorentin Chary             vnc_disconnect_finish(vs);
14003e230dd2SCorentin Chary             return;
14013e230dd2SCorentin Chary         }
14023e230dd2SCorentin Chary 
14033e230dd2SCorentin Chary         if (!ret) {
140432ed2680STim Hardeck             buffer_advance(&vs->input, len);
14053e230dd2SCorentin Chary         } else {
14063e230dd2SCorentin Chary             vs->read_handler_expect = ret;
14073e230dd2SCorentin Chary         }
14083e230dd2SCorentin Chary     }
14093e230dd2SCorentin Chary }
14103e230dd2SCorentin Chary 
14113e230dd2SCorentin Chary void vnc_write(VncState *vs, const void *data, size_t len)
14123e230dd2SCorentin Chary {
14133e230dd2SCorentin Chary     buffer_reserve(&vs->output, len);
14143e230dd2SCorentin Chary 
14153e230dd2SCorentin Chary     if (vs->csock != -1 && buffer_empty(&vs->output)) {
14163e230dd2SCorentin Chary         qemu_set_fd_handler2(vs->csock, NULL, vnc_client_read, vnc_client_write, vs);
14173e230dd2SCorentin Chary     }
14183e230dd2SCorentin Chary 
14193e230dd2SCorentin Chary     buffer_append(&vs->output, data, len);
14203e230dd2SCorentin Chary }
14213e230dd2SCorentin Chary 
14223e230dd2SCorentin Chary void vnc_write_s32(VncState *vs, int32_t value)
14233e230dd2SCorentin Chary {
14243e230dd2SCorentin Chary     vnc_write_u32(vs, *(uint32_t *)&value);
14253e230dd2SCorentin Chary }
14263e230dd2SCorentin Chary 
14273e230dd2SCorentin Chary void vnc_write_u32(VncState *vs, uint32_t value)
14283e230dd2SCorentin Chary {
14293e230dd2SCorentin Chary     uint8_t buf[4];
14303e230dd2SCorentin Chary 
14313e230dd2SCorentin Chary     buf[0] = (value >> 24) & 0xFF;
14323e230dd2SCorentin Chary     buf[1] = (value >> 16) & 0xFF;
14333e230dd2SCorentin Chary     buf[2] = (value >>  8) & 0xFF;
14343e230dd2SCorentin Chary     buf[3] = value & 0xFF;
14353e230dd2SCorentin Chary 
14363e230dd2SCorentin Chary     vnc_write(vs, buf, 4);
14373e230dd2SCorentin Chary }
14383e230dd2SCorentin Chary 
14393e230dd2SCorentin Chary void vnc_write_u16(VncState *vs, uint16_t value)
14403e230dd2SCorentin Chary {
14413e230dd2SCorentin Chary     uint8_t buf[2];
14423e230dd2SCorentin Chary 
14433e230dd2SCorentin Chary     buf[0] = (value >> 8) & 0xFF;
14443e230dd2SCorentin Chary     buf[1] = value & 0xFF;
14453e230dd2SCorentin Chary 
14463e230dd2SCorentin Chary     vnc_write(vs, buf, 2);
14473e230dd2SCorentin Chary }
14483e230dd2SCorentin Chary 
14493e230dd2SCorentin Chary void vnc_write_u8(VncState *vs, uint8_t value)
14503e230dd2SCorentin Chary {
14513e230dd2SCorentin Chary     vnc_write(vs, (char *)&value, 1);
14523e230dd2SCorentin Chary }
14533e230dd2SCorentin Chary 
14543e230dd2SCorentin Chary void vnc_flush(VncState *vs)
14553e230dd2SCorentin Chary {
1456bd023f95SCorentin Chary     vnc_lock_output(vs);
14577536ee4bSTim Hardeck     if (vs->csock != -1 && (vs->output.offset
14587536ee4bSTim Hardeck #ifdef CONFIG_VNC_WS
14597536ee4bSTim Hardeck                 || vs->ws_output.offset
14607536ee4bSTim Hardeck #endif
14617536ee4bSTim Hardeck                 )) {
1462bd023f95SCorentin Chary         vnc_client_write_locked(vs);
1463bd023f95SCorentin Chary     }
1464bd023f95SCorentin Chary     vnc_unlock_output(vs);
14653e230dd2SCorentin Chary }
14663e230dd2SCorentin Chary 
146771a8cdecSBlue Swirl static uint8_t read_u8(uint8_t *data, size_t offset)
14683e230dd2SCorentin Chary {
14693e230dd2SCorentin Chary     return data[offset];
14703e230dd2SCorentin Chary }
14713e230dd2SCorentin Chary 
147271a8cdecSBlue Swirl static uint16_t read_u16(uint8_t *data, size_t offset)
14733e230dd2SCorentin Chary {
14743e230dd2SCorentin Chary     return ((data[offset] & 0xFF) << 8) | (data[offset + 1] & 0xFF);
14753e230dd2SCorentin Chary }
14763e230dd2SCorentin Chary 
147771a8cdecSBlue Swirl static int32_t read_s32(uint8_t *data, size_t offset)
14783e230dd2SCorentin Chary {
14793e230dd2SCorentin Chary     return (int32_t)((data[offset] << 24) | (data[offset + 1] << 16) |
14803e230dd2SCorentin Chary                      (data[offset + 2] << 8) | data[offset + 3]);
14813e230dd2SCorentin Chary }
14823e230dd2SCorentin Chary 
14833e230dd2SCorentin Chary uint32_t read_u32(uint8_t *data, size_t offset)
14843e230dd2SCorentin Chary {
14853e230dd2SCorentin Chary     return ((data[offset] << 24) | (data[offset + 1] << 16) |
14863e230dd2SCorentin Chary             (data[offset + 2] << 8) | data[offset + 3]);
14873e230dd2SCorentin Chary }
14883e230dd2SCorentin Chary 
14893e230dd2SCorentin Chary static void client_cut_text(VncState *vs, size_t len, uint8_t *text)
14903e230dd2SCorentin Chary {
14913e230dd2SCorentin Chary }
14923e230dd2SCorentin Chary 
14939e8dd451SJan Kiszka static void check_pointer_type_change(Notifier *notifier, void *data)
14943e230dd2SCorentin Chary {
14953e230dd2SCorentin Chary     VncState *vs = container_of(notifier, VncState, mouse_mode_notifier);
149614768ebaSGerd Hoffmann     int absolute = qemu_input_is_absolute();
14973e230dd2SCorentin Chary 
14983e230dd2SCorentin Chary     if (vnc_has_feature(vs, VNC_FEATURE_POINTER_TYPE_CHANGE) && vs->absolute != absolute) {
1499bd023f95SCorentin Chary         vnc_lock_output(vs);
15003e230dd2SCorentin Chary         vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE);
15013e230dd2SCorentin Chary         vnc_write_u8(vs, 0);
15023e230dd2SCorentin Chary         vnc_write_u16(vs, 1);
15033e230dd2SCorentin Chary         vnc_framebuffer_update(vs, absolute, 0,
1504d39fa6d8SGerd Hoffmann                                surface_width(vs->vd->ds),
1505d39fa6d8SGerd Hoffmann                                surface_height(vs->vd->ds),
15063e230dd2SCorentin Chary                                VNC_ENCODING_POINTER_TYPE_CHANGE);
1507bd023f95SCorentin Chary         vnc_unlock_output(vs);
15083e230dd2SCorentin Chary         vnc_flush(vs);
15093e230dd2SCorentin Chary     }
15103e230dd2SCorentin Chary     vs->absolute = absolute;
15113e230dd2SCorentin Chary }
15123e230dd2SCorentin Chary 
15133e230dd2SCorentin Chary static void pointer_event(VncState *vs, int button_mask, int x, int y)
15143e230dd2SCorentin Chary {
151514768ebaSGerd Hoffmann     static uint32_t bmap[INPUT_BUTTON_MAX] = {
151614768ebaSGerd Hoffmann         [INPUT_BUTTON_LEFT]       = 0x01,
151714768ebaSGerd Hoffmann         [INPUT_BUTTON_MIDDLE]     = 0x02,
151814768ebaSGerd Hoffmann         [INPUT_BUTTON_RIGHT]      = 0x04,
151914768ebaSGerd Hoffmann         [INPUT_BUTTON_WHEEL_UP]   = 0x08,
152014768ebaSGerd Hoffmann         [INPUT_BUTTON_WHEEL_DOWN] = 0x10,
152114768ebaSGerd Hoffmann     };
152214768ebaSGerd Hoffmann     QemuConsole *con = vs->vd->dcl.con;
1523d39fa6d8SGerd Hoffmann     int width = surface_width(vs->vd->ds);
1524d39fa6d8SGerd Hoffmann     int height = surface_height(vs->vd->ds);
15253e230dd2SCorentin Chary 
152614768ebaSGerd Hoffmann     if (vs->last_bmask != button_mask) {
152714768ebaSGerd Hoffmann         qemu_input_update_buttons(con, bmap, vs->last_bmask, button_mask);
152814768ebaSGerd Hoffmann         vs->last_bmask = button_mask;
152914768ebaSGerd Hoffmann     }
15303e230dd2SCorentin Chary 
15313e230dd2SCorentin Chary     if (vs->absolute) {
153214768ebaSGerd Hoffmann         qemu_input_queue_abs(con, INPUT_AXIS_X, x, width);
153314768ebaSGerd Hoffmann         qemu_input_queue_abs(con, INPUT_AXIS_Y, y, height);
15343e230dd2SCorentin Chary     } else if (vnc_has_feature(vs, VNC_FEATURE_POINTER_TYPE_CHANGE)) {
153514768ebaSGerd Hoffmann         qemu_input_queue_rel(con, INPUT_AXIS_X, x - 0x7FFF);
153614768ebaSGerd Hoffmann         qemu_input_queue_rel(con, INPUT_AXIS_Y, y - 0x7FFF);
15373e230dd2SCorentin Chary     } else {
153814768ebaSGerd Hoffmann         if (vs->last_x != -1) {
153914768ebaSGerd Hoffmann             qemu_input_queue_rel(con, INPUT_AXIS_X, x - vs->last_x);
154014768ebaSGerd Hoffmann             qemu_input_queue_rel(con, INPUT_AXIS_Y, y - vs->last_y);
154114768ebaSGerd Hoffmann         }
15423e230dd2SCorentin Chary         vs->last_x = x;
15433e230dd2SCorentin Chary         vs->last_y = y;
15443e230dd2SCorentin Chary     }
154514768ebaSGerd Hoffmann     qemu_input_event_sync();
15463e230dd2SCorentin Chary }
15473e230dd2SCorentin Chary 
15483e230dd2SCorentin Chary static void reset_keys(VncState *vs)
15493e230dd2SCorentin Chary {
15503e230dd2SCorentin Chary     int i;
15513e230dd2SCorentin Chary     for(i = 0; i < 256; i++) {
15523e230dd2SCorentin Chary         if (vs->modifiers_state[i]) {
15538d447d10SGerd Hoffmann             qemu_input_event_send_key_number(vs->vd->dcl.con, i, false);
15543e230dd2SCorentin Chary             vs->modifiers_state[i] = 0;
15553e230dd2SCorentin Chary         }
15563e230dd2SCorentin Chary     }
15573e230dd2SCorentin Chary }
15583e230dd2SCorentin Chary 
15593e230dd2SCorentin Chary static void press_key(VncState *vs, int keysym)
15603e230dd2SCorentin Chary {
15613e230dd2SCorentin Chary     int keycode = keysym2scancode(vs->vd->kbd_layout, keysym) & SCANCODE_KEYMASK;
15628d447d10SGerd Hoffmann     qemu_input_event_send_key_number(vs->vd->dcl.con, keycode, true);
15632deb4accSGerd Hoffmann     qemu_input_event_send_key_delay(0);
15648d447d10SGerd Hoffmann     qemu_input_event_send_key_number(vs->vd->dcl.con, keycode, false);
15652deb4accSGerd Hoffmann     qemu_input_event_send_key_delay(0);
15663e230dd2SCorentin Chary }
15673e230dd2SCorentin Chary 
1568ab99e5c1SLei Li static int current_led_state(VncState *vs)
1569ab99e5c1SLei Li {
1570ab99e5c1SLei Li     int ledstate = 0;
1571ab99e5c1SLei Li 
1572ab99e5c1SLei Li     if (vs->modifiers_state[0x46]) {
1573ab99e5c1SLei Li         ledstate |= QEMU_SCROLL_LOCK_LED;
1574ab99e5c1SLei Li     }
1575ab99e5c1SLei Li     if (vs->modifiers_state[0x45]) {
1576ab99e5c1SLei Li         ledstate |= QEMU_NUM_LOCK_LED;
1577ab99e5c1SLei Li     }
1578ab99e5c1SLei Li     if (vs->modifiers_state[0x3a]) {
1579ab99e5c1SLei Li         ledstate |= QEMU_CAPS_LOCK_LED;
1580ab99e5c1SLei Li     }
1581ab99e5c1SLei Li 
1582ab99e5c1SLei Li     return ledstate;
1583ab99e5c1SLei Li }
1584ab99e5c1SLei Li 
1585ab99e5c1SLei Li static void vnc_led_state_change(VncState *vs)
1586ab99e5c1SLei Li {
1587ab99e5c1SLei Li     int ledstate = 0;
1588ab99e5c1SLei Li 
1589ab99e5c1SLei Li     if (!vnc_has_feature(vs, VNC_FEATURE_LED_STATE)) {
1590ab99e5c1SLei Li         return;
1591ab99e5c1SLei Li     }
1592ab99e5c1SLei Li 
1593ab99e5c1SLei Li     ledstate = current_led_state(vs);
1594ab99e5c1SLei Li     vnc_lock_output(vs);
1595ab99e5c1SLei Li     vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE);
1596ab99e5c1SLei Li     vnc_write_u8(vs, 0);
1597ab99e5c1SLei Li     vnc_write_u16(vs, 1);
1598ab99e5c1SLei Li     vnc_framebuffer_update(vs, 0, 0, 1, 1, VNC_ENCODING_LED_STATE);
1599ab99e5c1SLei Li     vnc_write_u8(vs, ledstate);
1600ab99e5c1SLei Li     vnc_unlock_output(vs);
1601ab99e5c1SLei Li     vnc_flush(vs);
1602ab99e5c1SLei Li }
1603ab99e5c1SLei Li 
16043e230dd2SCorentin Chary static void kbd_leds(void *opaque, int ledstate)
16053e230dd2SCorentin Chary {
16063e230dd2SCorentin Chary     VncState *vs = opaque;
160796f3d174SLei Li     int caps, num, scr;
16081483adcfSLei Li     bool has_changed = (ledstate != current_led_state(vs));
16093e230dd2SCorentin Chary 
161040066175SGerd Hoffmann     trace_vnc_key_guest_leds((ledstate & QEMU_CAPS_LOCK_LED),
161140066175SGerd Hoffmann                              (ledstate & QEMU_NUM_LOCK_LED),
161240066175SGerd Hoffmann                              (ledstate & QEMU_SCROLL_LOCK_LED));
161340066175SGerd Hoffmann 
16143e230dd2SCorentin Chary     caps = ledstate & QEMU_CAPS_LOCK_LED ? 1 : 0;
16153e230dd2SCorentin Chary     num  = ledstate & QEMU_NUM_LOCK_LED  ? 1 : 0;
161696f3d174SLei Li     scr  = ledstate & QEMU_SCROLL_LOCK_LED ? 1 : 0;
16173e230dd2SCorentin Chary 
16183e230dd2SCorentin Chary     if (vs->modifiers_state[0x3a] != caps) {
16193e230dd2SCorentin Chary         vs->modifiers_state[0x3a] = caps;
16203e230dd2SCorentin Chary     }
16213e230dd2SCorentin Chary     if (vs->modifiers_state[0x45] != num) {
16223e230dd2SCorentin Chary         vs->modifiers_state[0x45] = num;
16233e230dd2SCorentin Chary     }
162496f3d174SLei Li     if (vs->modifiers_state[0x46] != scr) {
162596f3d174SLei Li         vs->modifiers_state[0x46] = scr;
162696f3d174SLei Li     }
1627ab99e5c1SLei Li 
1628ab99e5c1SLei Li     /* Sending the current led state message to the client */
16291483adcfSLei Li     if (has_changed) {
1630ab99e5c1SLei Li         vnc_led_state_change(vs);
1631ab99e5c1SLei Li     }
16323e230dd2SCorentin Chary }
16333e230dd2SCorentin Chary 
16343e230dd2SCorentin Chary static void do_key_event(VncState *vs, int down, int keycode, int sym)
16353e230dd2SCorentin Chary {
16363e230dd2SCorentin Chary     /* QEMU console switch */
16373e230dd2SCorentin Chary     switch(keycode) {
16383e230dd2SCorentin Chary     case 0x2a:                          /* Left Shift */
16393e230dd2SCorentin Chary     case 0x36:                          /* Right Shift */
16403e230dd2SCorentin Chary     case 0x1d:                          /* Left CTRL */
16413e230dd2SCorentin Chary     case 0x9d:                          /* Right CTRL */
16423e230dd2SCorentin Chary     case 0x38:                          /* Left ALT */
16433e230dd2SCorentin Chary     case 0xb8:                          /* Right ALT */
16443e230dd2SCorentin Chary         if (down)
16453e230dd2SCorentin Chary             vs->modifiers_state[keycode] = 1;
16463e230dd2SCorentin Chary         else
16473e230dd2SCorentin Chary             vs->modifiers_state[keycode] = 0;
16483e230dd2SCorentin Chary         break;
16493e230dd2SCorentin Chary     case 0x02 ... 0x0a: /* '1' to '9' keys */
16503e230dd2SCorentin Chary         if (down && vs->modifiers_state[0x1d] && vs->modifiers_state[0x38]) {
16513e230dd2SCorentin Chary             /* Reset the modifiers sent to the current console */
16523e230dd2SCorentin Chary             reset_keys(vs);
16533e230dd2SCorentin Chary             console_select(keycode - 0x02);
16543e230dd2SCorentin Chary             return;
16553e230dd2SCorentin Chary         }
16563e230dd2SCorentin Chary         break;
16573e230dd2SCorentin Chary     case 0x3a:                        /* CapsLock */
16583e230dd2SCorentin Chary     case 0x45:                        /* NumLock */
16593e230dd2SCorentin Chary         if (down)
16603e230dd2SCorentin Chary             vs->modifiers_state[keycode] ^= 1;
16613e230dd2SCorentin Chary         break;
16623e230dd2SCorentin Chary     }
16633e230dd2SCorentin Chary 
1664e7b2aaccSLei Li     /* Turn off the lock state sync logic if the client support the led
1665e7b2aaccSLei Li        state extension.
1666e7b2aaccSLei Li     */
16679892088bSGerd Hoffmann     if (down && vs->vd->lock_key_sync &&
1668e7b2aaccSLei Li         !vnc_has_feature(vs, VNC_FEATURE_LED_STATE) &&
16693e230dd2SCorentin Chary         keycode_is_keypad(vs->vd->kbd_layout, keycode)) {
16703e230dd2SCorentin Chary         /* If the numlock state needs to change then simulate an additional
16713e230dd2SCorentin Chary            keypress before sending this one.  This will happen if the user
16723e230dd2SCorentin Chary            toggles numlock away from the VNC window.
16733e230dd2SCorentin Chary         */
16743e230dd2SCorentin Chary         if (keysym_is_numlock(vs->vd->kbd_layout, sym & 0xFFFF)) {
16753e230dd2SCorentin Chary             if (!vs->modifiers_state[0x45]) {
167640066175SGerd Hoffmann                 trace_vnc_key_sync_numlock(true);
16773e230dd2SCorentin Chary                 vs->modifiers_state[0x45] = 1;
16783e230dd2SCorentin Chary                 press_key(vs, 0xff7f);
16793e230dd2SCorentin Chary             }
16803e230dd2SCorentin Chary         } else {
16813e230dd2SCorentin Chary             if (vs->modifiers_state[0x45]) {
168240066175SGerd Hoffmann                 trace_vnc_key_sync_numlock(false);
16833e230dd2SCorentin Chary                 vs->modifiers_state[0x45] = 0;
16843e230dd2SCorentin Chary                 press_key(vs, 0xff7f);
16853e230dd2SCorentin Chary             }
16863e230dd2SCorentin Chary         }
16873e230dd2SCorentin Chary     }
16883e230dd2SCorentin Chary 
16899892088bSGerd Hoffmann     if (down && vs->vd->lock_key_sync &&
1690e7b2aaccSLei Li         !vnc_has_feature(vs, VNC_FEATURE_LED_STATE) &&
16913e230dd2SCorentin Chary         ((sym >= 'A' && sym <= 'Z') || (sym >= 'a' && sym <= 'z'))) {
16923e230dd2SCorentin Chary         /* If the capslock state needs to change then simulate an additional
16933e230dd2SCorentin Chary            keypress before sending this one.  This will happen if the user
16943e230dd2SCorentin Chary            toggles capslock away from the VNC window.
16953e230dd2SCorentin Chary         */
16963e230dd2SCorentin Chary         int uppercase = !!(sym >= 'A' && sym <= 'Z');
16973e230dd2SCorentin Chary         int shift = !!(vs->modifiers_state[0x2a] | vs->modifiers_state[0x36]);
16983e230dd2SCorentin Chary         int capslock = !!(vs->modifiers_state[0x3a]);
16993e230dd2SCorentin Chary         if (capslock) {
17003e230dd2SCorentin Chary             if (uppercase == shift) {
170140066175SGerd Hoffmann                 trace_vnc_key_sync_capslock(false);
17023e230dd2SCorentin Chary                 vs->modifiers_state[0x3a] = 0;
17033e230dd2SCorentin Chary                 press_key(vs, 0xffe5);
17043e230dd2SCorentin Chary             }
17053e230dd2SCorentin Chary         } else {
17063e230dd2SCorentin Chary             if (uppercase != shift) {
170740066175SGerd Hoffmann                 trace_vnc_key_sync_capslock(true);
17083e230dd2SCorentin Chary                 vs->modifiers_state[0x3a] = 1;
17093e230dd2SCorentin Chary                 press_key(vs, 0xffe5);
17103e230dd2SCorentin Chary             }
17113e230dd2SCorentin Chary         }
17123e230dd2SCorentin Chary     }
17133e230dd2SCorentin Chary 
171481c0d5a6SGerd Hoffmann     if (qemu_console_is_graphic(NULL)) {
17158d447d10SGerd Hoffmann         qemu_input_event_send_key_number(vs->vd->dcl.con, keycode, down);
17163e230dd2SCorentin Chary     } else {
1717e26437c2SGerd Hoffmann         bool numlock = vs->modifiers_state[0x45];
1718e26437c2SGerd Hoffmann         bool control = (vs->modifiers_state[0x1d] ||
1719e26437c2SGerd Hoffmann                         vs->modifiers_state[0x9d]);
17203e230dd2SCorentin Chary         /* QEMU console emulation */
17213e230dd2SCorentin Chary         if (down) {
17223e230dd2SCorentin Chary             switch (keycode) {
17233e230dd2SCorentin Chary             case 0x2a:                          /* Left Shift */
17243e230dd2SCorentin Chary             case 0x36:                          /* Right Shift */
17253e230dd2SCorentin Chary             case 0x1d:                          /* Left CTRL */
17263e230dd2SCorentin Chary             case 0x9d:                          /* Right CTRL */
17273e230dd2SCorentin Chary             case 0x38:                          /* Left ALT */
17283e230dd2SCorentin Chary             case 0xb8:                          /* Right ALT */
17293e230dd2SCorentin Chary                 break;
17303e230dd2SCorentin Chary             case 0xc8:
17313e230dd2SCorentin Chary                 kbd_put_keysym(QEMU_KEY_UP);
17323e230dd2SCorentin Chary                 break;
17333e230dd2SCorentin Chary             case 0xd0:
17343e230dd2SCorentin Chary                 kbd_put_keysym(QEMU_KEY_DOWN);
17353e230dd2SCorentin Chary                 break;
17363e230dd2SCorentin Chary             case 0xcb:
17373e230dd2SCorentin Chary                 kbd_put_keysym(QEMU_KEY_LEFT);
17383e230dd2SCorentin Chary                 break;
17393e230dd2SCorentin Chary             case 0xcd:
17403e230dd2SCorentin Chary                 kbd_put_keysym(QEMU_KEY_RIGHT);
17413e230dd2SCorentin Chary                 break;
17423e230dd2SCorentin Chary             case 0xd3:
17433e230dd2SCorentin Chary                 kbd_put_keysym(QEMU_KEY_DELETE);
17443e230dd2SCorentin Chary                 break;
17453e230dd2SCorentin Chary             case 0xc7:
17463e230dd2SCorentin Chary                 kbd_put_keysym(QEMU_KEY_HOME);
17473e230dd2SCorentin Chary                 break;
17483e230dd2SCorentin Chary             case 0xcf:
17493e230dd2SCorentin Chary                 kbd_put_keysym(QEMU_KEY_END);
17503e230dd2SCorentin Chary                 break;
17513e230dd2SCorentin Chary             case 0xc9:
17523e230dd2SCorentin Chary                 kbd_put_keysym(QEMU_KEY_PAGEUP);
17533e230dd2SCorentin Chary                 break;
17543e230dd2SCorentin Chary             case 0xd1:
17553e230dd2SCorentin Chary                 kbd_put_keysym(QEMU_KEY_PAGEDOWN);
17563e230dd2SCorentin Chary                 break;
17573e230dd2SCorentin Chary 
17583e230dd2SCorentin Chary             case 0x47:
17593e230dd2SCorentin Chary                 kbd_put_keysym(numlock ? '7' : QEMU_KEY_HOME);
17603e230dd2SCorentin Chary                 break;
17613e230dd2SCorentin Chary             case 0x48:
17623e230dd2SCorentin Chary                 kbd_put_keysym(numlock ? '8' : QEMU_KEY_UP);
17633e230dd2SCorentin Chary                 break;
17643e230dd2SCorentin Chary             case 0x49:
17653e230dd2SCorentin Chary                 kbd_put_keysym(numlock ? '9' : QEMU_KEY_PAGEUP);
17663e230dd2SCorentin Chary                 break;
17673e230dd2SCorentin Chary             case 0x4b:
17683e230dd2SCorentin Chary                 kbd_put_keysym(numlock ? '4' : QEMU_KEY_LEFT);
17693e230dd2SCorentin Chary                 break;
17703e230dd2SCorentin Chary             case 0x4c:
17713e230dd2SCorentin Chary                 kbd_put_keysym('5');
17723e230dd2SCorentin Chary                 break;
17733e230dd2SCorentin Chary             case 0x4d:
17743e230dd2SCorentin Chary                 kbd_put_keysym(numlock ? '6' : QEMU_KEY_RIGHT);
17753e230dd2SCorentin Chary                 break;
17763e230dd2SCorentin Chary             case 0x4f:
17773e230dd2SCorentin Chary                 kbd_put_keysym(numlock ? '1' : QEMU_KEY_END);
17783e230dd2SCorentin Chary                 break;
17793e230dd2SCorentin Chary             case 0x50:
17803e230dd2SCorentin Chary                 kbd_put_keysym(numlock ? '2' : QEMU_KEY_DOWN);
17813e230dd2SCorentin Chary                 break;
17823e230dd2SCorentin Chary             case 0x51:
17833e230dd2SCorentin Chary                 kbd_put_keysym(numlock ? '3' : QEMU_KEY_PAGEDOWN);
17843e230dd2SCorentin Chary                 break;
17853e230dd2SCorentin Chary             case 0x52:
17863e230dd2SCorentin Chary                 kbd_put_keysym('0');
17873e230dd2SCorentin Chary                 break;
17883e230dd2SCorentin Chary             case 0x53:
17893e230dd2SCorentin Chary                 kbd_put_keysym(numlock ? '.' : QEMU_KEY_DELETE);
17903e230dd2SCorentin Chary                 break;
17913e230dd2SCorentin Chary 
17923e230dd2SCorentin Chary             case 0xb5:
17933e230dd2SCorentin Chary                 kbd_put_keysym('/');
17943e230dd2SCorentin Chary                 break;
17953e230dd2SCorentin Chary             case 0x37:
17963e230dd2SCorentin Chary                 kbd_put_keysym('*');
17973e230dd2SCorentin Chary                 break;
17983e230dd2SCorentin Chary             case 0x4a:
17993e230dd2SCorentin Chary                 kbd_put_keysym('-');
18003e230dd2SCorentin Chary                 break;
18013e230dd2SCorentin Chary             case 0x4e:
18023e230dd2SCorentin Chary                 kbd_put_keysym('+');
18033e230dd2SCorentin Chary                 break;
18043e230dd2SCorentin Chary             case 0x9c:
18053e230dd2SCorentin Chary                 kbd_put_keysym('\n');
18063e230dd2SCorentin Chary                 break;
18073e230dd2SCorentin Chary 
18083e230dd2SCorentin Chary             default:
1809e26437c2SGerd Hoffmann                 if (control) {
1810e26437c2SGerd Hoffmann                     kbd_put_keysym(sym & 0x1f);
1811e26437c2SGerd Hoffmann                 } else {
18123e230dd2SCorentin Chary                     kbd_put_keysym(sym);
1813e26437c2SGerd Hoffmann                 }
18143e230dd2SCorentin Chary                 break;
18153e230dd2SCorentin Chary             }
18163e230dd2SCorentin Chary         }
18173e230dd2SCorentin Chary     }
18183e230dd2SCorentin Chary }
18193e230dd2SCorentin Chary 
18207bc9318bSGerd Hoffmann static void vnc_release_modifiers(VncState *vs)
18217bc9318bSGerd Hoffmann {
18227bc9318bSGerd Hoffmann     static const int keycodes[] = {
18237bc9318bSGerd Hoffmann         /* shift, control, alt keys, both left & right */
18247bc9318bSGerd Hoffmann         0x2a, 0x36, 0x1d, 0x9d, 0x38, 0xb8,
18257bc9318bSGerd Hoffmann     };
18267bc9318bSGerd Hoffmann     int i, keycode;
18277bc9318bSGerd Hoffmann 
182881c0d5a6SGerd Hoffmann     if (!qemu_console_is_graphic(NULL)) {
18297bc9318bSGerd Hoffmann         return;
18307bc9318bSGerd Hoffmann     }
18317bc9318bSGerd Hoffmann     for (i = 0; i < ARRAY_SIZE(keycodes); i++) {
18327bc9318bSGerd Hoffmann         keycode = keycodes[i];
18337bc9318bSGerd Hoffmann         if (!vs->modifiers_state[keycode]) {
18347bc9318bSGerd Hoffmann             continue;
18357bc9318bSGerd Hoffmann         }
18368d447d10SGerd Hoffmann         qemu_input_event_send_key_number(vs->vd->dcl.con, keycode, false);
18377bc9318bSGerd Hoffmann     }
18387bc9318bSGerd Hoffmann }
18397bc9318bSGerd Hoffmann 
184040066175SGerd Hoffmann static const char *code2name(int keycode)
184140066175SGerd Hoffmann {
184240066175SGerd Hoffmann     return QKeyCode_lookup[qemu_input_key_number_to_qcode(keycode)];
184340066175SGerd Hoffmann }
184440066175SGerd Hoffmann 
18453e230dd2SCorentin Chary static void key_event(VncState *vs, int down, uint32_t sym)
18463e230dd2SCorentin Chary {
18473e230dd2SCorentin Chary     int keycode;
18483e230dd2SCorentin Chary     int lsym = sym;
18493e230dd2SCorentin Chary 
185081c0d5a6SGerd Hoffmann     if (lsym >= 'A' && lsym <= 'Z' && qemu_console_is_graphic(NULL)) {
18513e230dd2SCorentin Chary         lsym = lsym - 'A' + 'a';
18523e230dd2SCorentin Chary     }
18533e230dd2SCorentin Chary 
18543e230dd2SCorentin Chary     keycode = keysym2scancode(vs->vd->kbd_layout, lsym & 0xFFFF) & SCANCODE_KEYMASK;
185540066175SGerd Hoffmann     trace_vnc_key_event_map(down, sym, keycode, code2name(keycode));
18563e230dd2SCorentin Chary     do_key_event(vs, down, keycode, sym);
18573e230dd2SCorentin Chary }
18583e230dd2SCorentin Chary 
18593e230dd2SCorentin Chary static void ext_key_event(VncState *vs, int down,
18603e230dd2SCorentin Chary                           uint32_t sym, uint16_t keycode)
18613e230dd2SCorentin Chary {
18623e230dd2SCorentin Chary     /* if the user specifies a keyboard layout, always use it */
186340066175SGerd Hoffmann     if (keyboard_layout) {
18643e230dd2SCorentin Chary         key_event(vs, down, sym);
186540066175SGerd Hoffmann     } else {
186640066175SGerd Hoffmann         trace_vnc_key_event_ext(down, sym, keycode, code2name(keycode));
18673e230dd2SCorentin Chary         do_key_event(vs, down, keycode, sym);
18683e230dd2SCorentin Chary     }
186940066175SGerd Hoffmann }
18703e230dd2SCorentin Chary 
18713e230dd2SCorentin Chary static void framebuffer_update_request(VncState *vs, int incremental,
18723e230dd2SCorentin Chary                                        int x_position, int y_position,
18733e230dd2SCorentin Chary                                        int w, int h)
18743e230dd2SCorentin Chary {
18756ed391bfSWen Congyang     int i;
1876b4c85ddcSPeter Lieven     const size_t width = surface_width(vs->vd->ds) / VNC_DIRTY_PIXELS_PER_BIT;
1877d39fa6d8SGerd Hoffmann     const size_t height = surface_height(vs->vd->ds);
18786ed391bfSWen Congyang 
1879d39fa6d8SGerd Hoffmann     if (y_position > height) {
1880d39fa6d8SGerd Hoffmann         y_position = height;
1881d39fa6d8SGerd Hoffmann     }
1882d39fa6d8SGerd Hoffmann     if (y_position + h >= height) {
1883d39fa6d8SGerd Hoffmann         h = height - y_position;
1884d39fa6d8SGerd Hoffmann     }
18853e230dd2SCorentin Chary 
18863e230dd2SCorentin Chary     vs->need_update = 1;
18873e230dd2SCorentin Chary     if (!incremental) {
18883e230dd2SCorentin Chary         vs->force_update = 1;
18893e230dd2SCorentin Chary         for (i = 0; i < h; i++) {
18906ed391bfSWen Congyang             bitmap_set(vs->dirty[y_position + i], 0, width);
18916ed391bfSWen Congyang             bitmap_clear(vs->dirty[y_position + i], width,
1892a0843a68SAnthony Liguori                          VNC_DIRTY_BITS - width);
18933e230dd2SCorentin Chary         }
18943e230dd2SCorentin Chary     }
18953e230dd2SCorentin Chary }
18963e230dd2SCorentin Chary 
18973e230dd2SCorentin Chary static void send_ext_key_event_ack(VncState *vs)
18983e230dd2SCorentin Chary {
1899bd023f95SCorentin Chary     vnc_lock_output(vs);
19003e230dd2SCorentin Chary     vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE);
19013e230dd2SCorentin Chary     vnc_write_u8(vs, 0);
19023e230dd2SCorentin Chary     vnc_write_u16(vs, 1);
1903d39fa6d8SGerd Hoffmann     vnc_framebuffer_update(vs, 0, 0,
1904d39fa6d8SGerd Hoffmann                            surface_width(vs->vd->ds),
1905d39fa6d8SGerd Hoffmann                            surface_height(vs->vd->ds),
19063e230dd2SCorentin Chary                            VNC_ENCODING_EXT_KEY_EVENT);
1907bd023f95SCorentin Chary     vnc_unlock_output(vs);
19083e230dd2SCorentin Chary     vnc_flush(vs);
19093e230dd2SCorentin Chary }
19103e230dd2SCorentin Chary 
19113e230dd2SCorentin Chary static void send_ext_audio_ack(VncState *vs)
19123e230dd2SCorentin Chary {
1913bd023f95SCorentin Chary     vnc_lock_output(vs);
19143e230dd2SCorentin Chary     vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE);
19153e230dd2SCorentin Chary     vnc_write_u8(vs, 0);
19163e230dd2SCorentin Chary     vnc_write_u16(vs, 1);
1917d39fa6d8SGerd Hoffmann     vnc_framebuffer_update(vs, 0, 0,
1918d39fa6d8SGerd Hoffmann                            surface_width(vs->vd->ds),
1919d39fa6d8SGerd Hoffmann                            surface_height(vs->vd->ds),
19203e230dd2SCorentin Chary                            VNC_ENCODING_AUDIO);
1921bd023f95SCorentin Chary     vnc_unlock_output(vs);
19223e230dd2SCorentin Chary     vnc_flush(vs);
19233e230dd2SCorentin Chary }
19243e230dd2SCorentin Chary 
19253e230dd2SCorentin Chary static void set_encodings(VncState *vs, int32_t *encodings, size_t n_encodings)
19263e230dd2SCorentin Chary {
19273e230dd2SCorentin Chary     int i;
19283e230dd2SCorentin Chary     unsigned int enc = 0;
19293e230dd2SCorentin Chary 
19303e230dd2SCorentin Chary     vs->features = 0;
19313e230dd2SCorentin Chary     vs->vnc_encoding = 0;
1932d1af0e05SCorentin Chary     vs->tight.compression = 9;
1933d1af0e05SCorentin Chary     vs->tight.quality = -1; /* Lossless by default */
19343e230dd2SCorentin Chary     vs->absolute = -1;
19353e230dd2SCorentin Chary 
19363e230dd2SCorentin Chary     /*
19373e230dd2SCorentin Chary      * Start from the end because the encodings are sent in order of preference.
1938e5bed759SDong Xu Wang      * This way the preferred encoding (first encoding defined in the array)
19393e230dd2SCorentin Chary      * will be set at the end of the loop.
19403e230dd2SCorentin Chary      */
19413e230dd2SCorentin Chary     for (i = n_encodings - 1; i >= 0; i--) {
19423e230dd2SCorentin Chary         enc = encodings[i];
19433e230dd2SCorentin Chary         switch (enc) {
19443e230dd2SCorentin Chary         case VNC_ENCODING_RAW:
19453e230dd2SCorentin Chary             vs->vnc_encoding = enc;
19463e230dd2SCorentin Chary             break;
19473e230dd2SCorentin Chary         case VNC_ENCODING_COPYRECT:
19483e230dd2SCorentin Chary             vs->features |= VNC_FEATURE_COPYRECT_MASK;
19493e230dd2SCorentin Chary             break;
19503e230dd2SCorentin Chary         case VNC_ENCODING_HEXTILE:
19513e230dd2SCorentin Chary             vs->features |= VNC_FEATURE_HEXTILE_MASK;
19523e230dd2SCorentin Chary             vs->vnc_encoding = enc;
19533e230dd2SCorentin Chary             break;
19543e230dd2SCorentin Chary         case VNC_ENCODING_TIGHT:
19553e230dd2SCorentin Chary             vs->features |= VNC_FEATURE_TIGHT_MASK;
19563e230dd2SCorentin Chary             vs->vnc_encoding = enc;
19573e230dd2SCorentin Chary             break;
1958fe3e7f2dSJoel Martin #ifdef CONFIG_VNC_PNG
1959efe556adSCorentin Chary         case VNC_ENCODING_TIGHT_PNG:
1960efe556adSCorentin Chary             vs->features |= VNC_FEATURE_TIGHT_PNG_MASK;
1961efe556adSCorentin Chary             vs->vnc_encoding = enc;
1962efe556adSCorentin Chary             break;
1963fe3e7f2dSJoel Martin #endif
19643e230dd2SCorentin Chary         case VNC_ENCODING_ZLIB:
19653e230dd2SCorentin Chary             vs->features |= VNC_FEATURE_ZLIB_MASK;
19663e230dd2SCorentin Chary             vs->vnc_encoding = enc;
19673e230dd2SCorentin Chary             break;
1968148954faSCorentin Chary         case VNC_ENCODING_ZRLE:
1969148954faSCorentin Chary             vs->features |= VNC_FEATURE_ZRLE_MASK;
1970148954faSCorentin Chary             vs->vnc_encoding = enc;
1971148954faSCorentin Chary             break;
1972148954faSCorentin Chary         case VNC_ENCODING_ZYWRLE:
1973148954faSCorentin Chary             vs->features |= VNC_FEATURE_ZYWRLE_MASK;
1974148954faSCorentin Chary             vs->vnc_encoding = enc;
1975148954faSCorentin Chary             break;
19763e230dd2SCorentin Chary         case VNC_ENCODING_DESKTOPRESIZE:
19773e230dd2SCorentin Chary             vs->features |= VNC_FEATURE_RESIZE_MASK;
19783e230dd2SCorentin Chary             break;
19793e230dd2SCorentin Chary         case VNC_ENCODING_POINTER_TYPE_CHANGE:
19803e230dd2SCorentin Chary             vs->features |= VNC_FEATURE_POINTER_TYPE_CHANGE_MASK;
19813e230dd2SCorentin Chary             break;
19823e230dd2SCorentin Chary         case VNC_ENCODING_RICH_CURSOR:
19833e230dd2SCorentin Chary             vs->features |= VNC_FEATURE_RICH_CURSOR_MASK;
19843e230dd2SCorentin Chary             break;
19853e230dd2SCorentin Chary         case VNC_ENCODING_EXT_KEY_EVENT:
19863e230dd2SCorentin Chary             send_ext_key_event_ack(vs);
19873e230dd2SCorentin Chary             break;
19883e230dd2SCorentin Chary         case VNC_ENCODING_AUDIO:
19893e230dd2SCorentin Chary             send_ext_audio_ack(vs);
19903e230dd2SCorentin Chary             break;
19913e230dd2SCorentin Chary         case VNC_ENCODING_WMVi:
19923e230dd2SCorentin Chary             vs->features |= VNC_FEATURE_WMVI_MASK;
19933e230dd2SCorentin Chary             break;
1994ab99e5c1SLei Li         case VNC_ENCODING_LED_STATE:
1995ab99e5c1SLei Li             vs->features |= VNC_FEATURE_LED_STATE_MASK;
1996ab99e5c1SLei Li             break;
19973e230dd2SCorentin Chary         case VNC_ENCODING_COMPRESSLEVEL0 ... VNC_ENCODING_COMPRESSLEVEL0 + 9:
1998d1af0e05SCorentin Chary             vs->tight.compression = (enc & 0x0F);
19993e230dd2SCorentin Chary             break;
20003e230dd2SCorentin Chary         case VNC_ENCODING_QUALITYLEVEL0 ... VNC_ENCODING_QUALITYLEVEL0 + 9:
2001b31f519eSCorentin Chary             if (vs->vd->lossy) {
2002d1af0e05SCorentin Chary                 vs->tight.quality = (enc & 0x0F);
2003b31f519eSCorentin Chary             }
20043e230dd2SCorentin Chary             break;
20053e230dd2SCorentin Chary         default:
20063e230dd2SCorentin Chary             VNC_DEBUG("Unknown encoding: %d (0x%.8x): %d\n", i, enc, enc);
20073e230dd2SCorentin Chary             break;
20083e230dd2SCorentin Chary         }
20093e230dd2SCorentin Chary     }
20103e230dd2SCorentin Chary     vnc_desktop_resize(vs);
20119e8dd451SJan Kiszka     check_pointer_type_change(&vs->mouse_mode_notifier, NULL);
2012ab99e5c1SLei Li     vnc_led_state_change(vs);
20133e230dd2SCorentin Chary }
20143e230dd2SCorentin Chary 
20153e230dd2SCorentin Chary static void set_pixel_conversion(VncState *vs)
20163e230dd2SCorentin Chary {
20179f64916dSGerd Hoffmann     pixman_format_code_t fmt = qemu_pixman_get_format(&vs->client_pf);
20189f64916dSGerd Hoffmann 
20199f64916dSGerd Hoffmann     if (fmt == VNC_SERVER_FB_FORMAT) {
20203e230dd2SCorentin Chary         vs->write_pixels = vnc_write_pixels_copy;
20213e230dd2SCorentin Chary         vnc_hextile_set_pixel_conversion(vs, 0);
20223e230dd2SCorentin Chary     } else {
20233e230dd2SCorentin Chary         vs->write_pixels = vnc_write_pixels_generic;
20243e230dd2SCorentin Chary         vnc_hextile_set_pixel_conversion(vs, 1);
20253e230dd2SCorentin Chary     }
20263e230dd2SCorentin Chary }
20273e230dd2SCorentin Chary 
20283e230dd2SCorentin Chary static void set_pixel_format(VncState *vs,
20293e230dd2SCorentin Chary                              int bits_per_pixel, int depth,
20303e230dd2SCorentin Chary                              int big_endian_flag, int true_color_flag,
20313e230dd2SCorentin Chary                              int red_max, int green_max, int blue_max,
20323e230dd2SCorentin Chary                              int red_shift, int green_shift, int blue_shift)
20333e230dd2SCorentin Chary {
20343e230dd2SCorentin Chary     if (!true_color_flag) {
20353e230dd2SCorentin Chary         vnc_client_error(vs);
20363e230dd2SCorentin Chary         return;
20373e230dd2SCorentin Chary     }
20383e230dd2SCorentin Chary 
20399f64916dSGerd Hoffmann     vs->client_pf.rmax = red_max;
20409f64916dSGerd Hoffmann     vs->client_pf.rbits = hweight_long(red_max);
20419f64916dSGerd Hoffmann     vs->client_pf.rshift = red_shift;
20429f64916dSGerd Hoffmann     vs->client_pf.rmask = red_max << red_shift;
20439f64916dSGerd Hoffmann     vs->client_pf.gmax = green_max;
20449f64916dSGerd Hoffmann     vs->client_pf.gbits = hweight_long(green_max);
20459f64916dSGerd Hoffmann     vs->client_pf.gshift = green_shift;
20469f64916dSGerd Hoffmann     vs->client_pf.gmask = green_max << green_shift;
20479f64916dSGerd Hoffmann     vs->client_pf.bmax = blue_max;
20489f64916dSGerd Hoffmann     vs->client_pf.bbits = hweight_long(blue_max);
20499f64916dSGerd Hoffmann     vs->client_pf.bshift = blue_shift;
20509f64916dSGerd Hoffmann     vs->client_pf.bmask = blue_max << blue_shift;
20519f64916dSGerd Hoffmann     vs->client_pf.bits_per_pixel = bits_per_pixel;
20529f64916dSGerd Hoffmann     vs->client_pf.bytes_per_pixel = bits_per_pixel / 8;
20539f64916dSGerd Hoffmann     vs->client_pf.depth = bits_per_pixel == 32 ? 24 : bits_per_pixel;
20549f64916dSGerd Hoffmann     vs->client_be = big_endian_flag;
20553e230dd2SCorentin Chary 
20563e230dd2SCorentin Chary     set_pixel_conversion(vs);
20573e230dd2SCorentin Chary 
20581dbfa005SGerd Hoffmann     graphic_hw_invalidate(NULL);
20591dbfa005SGerd Hoffmann     graphic_hw_update(NULL);
20603e230dd2SCorentin Chary }
20613e230dd2SCorentin Chary 
20623e230dd2SCorentin Chary static void pixel_format_message (VncState *vs) {
20633e230dd2SCorentin Chary     char pad[3] = { 0, 0, 0 };
20643e230dd2SCorentin Chary 
20659f64916dSGerd Hoffmann     vs->client_pf = qemu_default_pixelformat(32);
20669f64916dSGerd Hoffmann 
20679f64916dSGerd Hoffmann     vnc_write_u8(vs, vs->client_pf.bits_per_pixel); /* bits-per-pixel */
20689f64916dSGerd Hoffmann     vnc_write_u8(vs, vs->client_pf.depth); /* depth */
20693e230dd2SCorentin Chary 
20703e230dd2SCorentin Chary #ifdef HOST_WORDS_BIGENDIAN
20713e230dd2SCorentin Chary     vnc_write_u8(vs, 1);             /* big-endian-flag */
20723e230dd2SCorentin Chary #else
20733e230dd2SCorentin Chary     vnc_write_u8(vs, 0);             /* big-endian-flag */
20743e230dd2SCorentin Chary #endif
20753e230dd2SCorentin Chary     vnc_write_u8(vs, 1);             /* true-color-flag */
20769f64916dSGerd Hoffmann     vnc_write_u16(vs, vs->client_pf.rmax);     /* red-max */
20779f64916dSGerd Hoffmann     vnc_write_u16(vs, vs->client_pf.gmax);     /* green-max */
20789f64916dSGerd Hoffmann     vnc_write_u16(vs, vs->client_pf.bmax);     /* blue-max */
20799f64916dSGerd Hoffmann     vnc_write_u8(vs, vs->client_pf.rshift);    /* red-shift */
20809f64916dSGerd Hoffmann     vnc_write_u8(vs, vs->client_pf.gshift);    /* green-shift */
20819f64916dSGerd Hoffmann     vnc_write_u8(vs, vs->client_pf.bshift);    /* blue-shift */
20829f64916dSGerd Hoffmann     vnc_write(vs, pad, 3);           /* padding */
20833e230dd2SCorentin Chary 
20843e230dd2SCorentin Chary     vnc_hextile_set_pixel_conversion(vs, 0);
20853e230dd2SCorentin Chary     vs->write_pixels = vnc_write_pixels_copy;
20863e230dd2SCorentin Chary }
20873e230dd2SCorentin Chary 
20883e230dd2SCorentin Chary static void vnc_colordepth(VncState *vs)
20893e230dd2SCorentin Chary {
20903e230dd2SCorentin Chary     if (vnc_has_feature(vs, VNC_FEATURE_WMVI)) {
20913e230dd2SCorentin Chary         /* Sending a WMVi message to notify the client*/
2092bd023f95SCorentin Chary         vnc_lock_output(vs);
20933e230dd2SCorentin Chary         vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE);
20943e230dd2SCorentin Chary         vnc_write_u8(vs, 0);
20953e230dd2SCorentin Chary         vnc_write_u16(vs, 1); /* number of rects */
2096d39fa6d8SGerd Hoffmann         vnc_framebuffer_update(vs, 0, 0,
2097d39fa6d8SGerd Hoffmann                                surface_width(vs->vd->ds),
2098d39fa6d8SGerd Hoffmann                                surface_height(vs->vd->ds),
2099d39fa6d8SGerd Hoffmann                                VNC_ENCODING_WMVi);
21003e230dd2SCorentin Chary         pixel_format_message(vs);
2101bd023f95SCorentin Chary         vnc_unlock_output(vs);
21023e230dd2SCorentin Chary         vnc_flush(vs);
21033e230dd2SCorentin Chary     } else {
21043e230dd2SCorentin Chary         set_pixel_conversion(vs);
21053e230dd2SCorentin Chary     }
21063e230dd2SCorentin Chary }
21073e230dd2SCorentin Chary 
21083e230dd2SCorentin Chary static int protocol_client_msg(VncState *vs, uint8_t *data, size_t len)
21093e230dd2SCorentin Chary {
21103e230dd2SCorentin Chary     int i;
21113e230dd2SCorentin Chary     uint16_t limit;
21123e230dd2SCorentin Chary     VncDisplay *vd = vs->vd;
21133e230dd2SCorentin Chary 
21143e230dd2SCorentin Chary     if (data[0] > 3) {
21150f7b2864SGerd Hoffmann         update_displaychangelistener(&vd->dcl, VNC_REFRESH_INTERVAL_BASE);
21163e230dd2SCorentin Chary     }
21173e230dd2SCorentin Chary 
21183e230dd2SCorentin Chary     switch (data[0]) {
21193e230dd2SCorentin Chary     case VNC_MSG_CLIENT_SET_PIXEL_FORMAT:
21203e230dd2SCorentin Chary         if (len == 1)
21213e230dd2SCorentin Chary             return 20;
21223e230dd2SCorentin Chary 
21233e230dd2SCorentin Chary         set_pixel_format(vs, read_u8(data, 4), read_u8(data, 5),
21243e230dd2SCorentin Chary                          read_u8(data, 6), read_u8(data, 7),
21253e230dd2SCorentin Chary                          read_u16(data, 8), read_u16(data, 10),
21263e230dd2SCorentin Chary                          read_u16(data, 12), read_u8(data, 14),
21273e230dd2SCorentin Chary                          read_u8(data, 15), read_u8(data, 16));
21283e230dd2SCorentin Chary         break;
21293e230dd2SCorentin Chary     case VNC_MSG_CLIENT_SET_ENCODINGS:
21303e230dd2SCorentin Chary         if (len == 1)
21313e230dd2SCorentin Chary             return 4;
21323e230dd2SCorentin Chary 
21333e230dd2SCorentin Chary         if (len == 4) {
21343e230dd2SCorentin Chary             limit = read_u16(data, 2);
21353e230dd2SCorentin Chary             if (limit > 0)
21363e230dd2SCorentin Chary                 return 4 + (limit * 4);
21373e230dd2SCorentin Chary         } else
21383e230dd2SCorentin Chary             limit = read_u16(data, 2);
21393e230dd2SCorentin Chary 
21403e230dd2SCorentin Chary         for (i = 0; i < limit; i++) {
21413e230dd2SCorentin Chary             int32_t val = read_s32(data, 4 + (i * 4));
21423e230dd2SCorentin Chary             memcpy(data + 4 + (i * 4), &val, sizeof(val));
21433e230dd2SCorentin Chary         }
21443e230dd2SCorentin Chary 
21453e230dd2SCorentin Chary         set_encodings(vs, (int32_t *)(data + 4), limit);
21463e230dd2SCorentin Chary         break;
21473e230dd2SCorentin Chary     case VNC_MSG_CLIENT_FRAMEBUFFER_UPDATE_REQUEST:
21483e230dd2SCorentin Chary         if (len == 1)
21493e230dd2SCorentin Chary             return 10;
21503e230dd2SCorentin Chary 
21513e230dd2SCorentin Chary         framebuffer_update_request(vs,
21523e230dd2SCorentin Chary                                    read_u8(data, 1), read_u16(data, 2), read_u16(data, 4),
21533e230dd2SCorentin Chary                                    read_u16(data, 6), read_u16(data, 8));
21543e230dd2SCorentin Chary         break;
21553e230dd2SCorentin Chary     case VNC_MSG_CLIENT_KEY_EVENT:
21563e230dd2SCorentin Chary         if (len == 1)
21573e230dd2SCorentin Chary             return 8;
21583e230dd2SCorentin Chary 
21593e230dd2SCorentin Chary         key_event(vs, read_u8(data, 1), read_u32(data, 4));
21603e230dd2SCorentin Chary         break;
21613e230dd2SCorentin Chary     case VNC_MSG_CLIENT_POINTER_EVENT:
21623e230dd2SCorentin Chary         if (len == 1)
21633e230dd2SCorentin Chary             return 6;
21643e230dd2SCorentin Chary 
21653e230dd2SCorentin Chary         pointer_event(vs, read_u8(data, 1), read_u16(data, 2), read_u16(data, 4));
21663e230dd2SCorentin Chary         break;
21673e230dd2SCorentin Chary     case VNC_MSG_CLIENT_CUT_TEXT:
21683e230dd2SCorentin Chary         if (len == 1)
21693e230dd2SCorentin Chary             return 8;
21703e230dd2SCorentin Chary 
21713e230dd2SCorentin Chary         if (len == 8) {
21723e230dd2SCorentin Chary             uint32_t dlen = read_u32(data, 4);
21733e230dd2SCorentin Chary             if (dlen > 0)
21743e230dd2SCorentin Chary                 return 8 + dlen;
21753e230dd2SCorentin Chary         }
21763e230dd2SCorentin Chary 
21773e230dd2SCorentin Chary         client_cut_text(vs, read_u32(data, 4), data + 8);
21783e230dd2SCorentin Chary         break;
21793e230dd2SCorentin Chary     case VNC_MSG_CLIENT_QEMU:
21803e230dd2SCorentin Chary         if (len == 1)
21813e230dd2SCorentin Chary             return 2;
21823e230dd2SCorentin Chary 
21833e230dd2SCorentin Chary         switch (read_u8(data, 1)) {
21843e230dd2SCorentin Chary         case VNC_MSG_CLIENT_QEMU_EXT_KEY_EVENT:
21853e230dd2SCorentin Chary             if (len == 2)
21863e230dd2SCorentin Chary                 return 12;
21873e230dd2SCorentin Chary 
21883e230dd2SCorentin Chary             ext_key_event(vs, read_u16(data, 2),
21893e230dd2SCorentin Chary                           read_u32(data, 4), read_u32(data, 8));
21903e230dd2SCorentin Chary             break;
21913e230dd2SCorentin Chary         case VNC_MSG_CLIENT_QEMU_AUDIO:
21923e230dd2SCorentin Chary             if (len == 2)
21933e230dd2SCorentin Chary                 return 4;
21943e230dd2SCorentin Chary 
21953e230dd2SCorentin Chary             switch (read_u16 (data, 2)) {
21963e230dd2SCorentin Chary             case VNC_MSG_CLIENT_QEMU_AUDIO_ENABLE:
21973e230dd2SCorentin Chary                 audio_add(vs);
21983e230dd2SCorentin Chary                 break;
21993e230dd2SCorentin Chary             case VNC_MSG_CLIENT_QEMU_AUDIO_DISABLE:
22003e230dd2SCorentin Chary                 audio_del(vs);
22013e230dd2SCorentin Chary                 break;
22023e230dd2SCorentin Chary             case VNC_MSG_CLIENT_QEMU_AUDIO_SET_FORMAT:
22033e230dd2SCorentin Chary                 if (len == 4)
22043e230dd2SCorentin Chary                     return 10;
22053e230dd2SCorentin Chary                 switch (read_u8(data, 4)) {
22063e230dd2SCorentin Chary                 case 0: vs->as.fmt = AUD_FMT_U8; break;
22073e230dd2SCorentin Chary                 case 1: vs->as.fmt = AUD_FMT_S8; break;
22083e230dd2SCorentin Chary                 case 2: vs->as.fmt = AUD_FMT_U16; break;
22093e230dd2SCorentin Chary                 case 3: vs->as.fmt = AUD_FMT_S16; break;
22103e230dd2SCorentin Chary                 case 4: vs->as.fmt = AUD_FMT_U32; break;
22113e230dd2SCorentin Chary                 case 5: vs->as.fmt = AUD_FMT_S32; break;
22123e230dd2SCorentin Chary                 default:
22133e230dd2SCorentin Chary                     printf("Invalid audio format %d\n", read_u8(data, 4));
22143e230dd2SCorentin Chary                     vnc_client_error(vs);
22153e230dd2SCorentin Chary                     break;
22163e230dd2SCorentin Chary                 }
22173e230dd2SCorentin Chary                 vs->as.nchannels = read_u8(data, 5);
22183e230dd2SCorentin Chary                 if (vs->as.nchannels != 1 && vs->as.nchannels != 2) {
22193e230dd2SCorentin Chary                     printf("Invalid audio channel coount %d\n",
22203e230dd2SCorentin Chary                            read_u8(data, 5));
22213e230dd2SCorentin Chary                     vnc_client_error(vs);
22223e230dd2SCorentin Chary                     break;
22233e230dd2SCorentin Chary                 }
22243e230dd2SCorentin Chary                 vs->as.freq = read_u32(data, 6);
22253e230dd2SCorentin Chary                 break;
22263e230dd2SCorentin Chary             default:
22273e230dd2SCorentin Chary                 printf ("Invalid audio message %d\n", read_u8(data, 4));
22283e230dd2SCorentin Chary                 vnc_client_error(vs);
22293e230dd2SCorentin Chary                 break;
22303e230dd2SCorentin Chary             }
22313e230dd2SCorentin Chary             break;
22323e230dd2SCorentin Chary 
22333e230dd2SCorentin Chary         default:
22343e230dd2SCorentin Chary             printf("Msg: %d\n", read_u16(data, 0));
22353e230dd2SCorentin Chary             vnc_client_error(vs);
22363e230dd2SCorentin Chary             break;
22373e230dd2SCorentin Chary         }
22383e230dd2SCorentin Chary         break;
22393e230dd2SCorentin Chary     default:
22403e230dd2SCorentin Chary         printf("Msg: %d\n", data[0]);
22413e230dd2SCorentin Chary         vnc_client_error(vs);
22423e230dd2SCorentin Chary         break;
22433e230dd2SCorentin Chary     }
22443e230dd2SCorentin Chary 
22453e230dd2SCorentin Chary     vnc_read_when(vs, protocol_client_msg, 1);
22463e230dd2SCorentin Chary     return 0;
22473e230dd2SCorentin Chary }
22483e230dd2SCorentin Chary 
22493e230dd2SCorentin Chary static int protocol_client_init(VncState *vs, uint8_t *data, size_t len)
22503e230dd2SCorentin Chary {
22513e230dd2SCorentin Chary     char buf[1024];
22528cf36489SGerd Hoffmann     VncShareMode mode;
22533e230dd2SCorentin Chary     int size;
22543e230dd2SCorentin Chary 
22558cf36489SGerd Hoffmann     mode = data[0] ? VNC_SHARE_MODE_SHARED : VNC_SHARE_MODE_EXCLUSIVE;
22568cf36489SGerd Hoffmann     switch (vs->vd->share_policy) {
22578cf36489SGerd Hoffmann     case VNC_SHARE_POLICY_IGNORE:
22588cf36489SGerd Hoffmann         /*
22598cf36489SGerd Hoffmann          * Ignore the shared flag.  Nothing to do here.
22608cf36489SGerd Hoffmann          *
22618cf36489SGerd Hoffmann          * Doesn't conform to the rfb spec but is traditional qemu
22628cf36489SGerd Hoffmann          * behavior, thus left here as option for compatibility
22638cf36489SGerd Hoffmann          * reasons.
22648cf36489SGerd Hoffmann          */
22658cf36489SGerd Hoffmann         break;
22668cf36489SGerd Hoffmann     case VNC_SHARE_POLICY_ALLOW_EXCLUSIVE:
22678cf36489SGerd Hoffmann         /*
22688cf36489SGerd Hoffmann          * Policy: Allow clients ask for exclusive access.
22698cf36489SGerd Hoffmann          *
22708cf36489SGerd Hoffmann          * Implementation: When a client asks for exclusive access,
22718cf36489SGerd Hoffmann          * disconnect all others. Shared connects are allowed as long
22728cf36489SGerd Hoffmann          * as no exclusive connection exists.
22738cf36489SGerd Hoffmann          *
22748cf36489SGerd Hoffmann          * This is how the rfb spec suggests to handle the shared flag.
22758cf36489SGerd Hoffmann          */
22768cf36489SGerd Hoffmann         if (mode == VNC_SHARE_MODE_EXCLUSIVE) {
22778cf36489SGerd Hoffmann             VncState *client;
22788cf36489SGerd Hoffmann             QTAILQ_FOREACH(client, &vs->vd->clients, next) {
22798cf36489SGerd Hoffmann                 if (vs == client) {
22808cf36489SGerd Hoffmann                     continue;
22818cf36489SGerd Hoffmann                 }
22828cf36489SGerd Hoffmann                 if (client->share_mode != VNC_SHARE_MODE_EXCLUSIVE &&
22838cf36489SGerd Hoffmann                     client->share_mode != VNC_SHARE_MODE_SHARED) {
22848cf36489SGerd Hoffmann                     continue;
22858cf36489SGerd Hoffmann                 }
22868cf36489SGerd Hoffmann                 vnc_disconnect_start(client);
22878cf36489SGerd Hoffmann             }
22888cf36489SGerd Hoffmann         }
22898cf36489SGerd Hoffmann         if (mode == VNC_SHARE_MODE_SHARED) {
22908cf36489SGerd Hoffmann             if (vs->vd->num_exclusive > 0) {
22918cf36489SGerd Hoffmann                 vnc_disconnect_start(vs);
22928cf36489SGerd Hoffmann                 return 0;
22938cf36489SGerd Hoffmann             }
22948cf36489SGerd Hoffmann         }
22958cf36489SGerd Hoffmann         break;
22968cf36489SGerd Hoffmann     case VNC_SHARE_POLICY_FORCE_SHARED:
22978cf36489SGerd Hoffmann         /*
22988cf36489SGerd Hoffmann          * Policy: Shared connects only.
22998cf36489SGerd Hoffmann          * Implementation: Disallow clients asking for exclusive access.
23008cf36489SGerd Hoffmann          *
23018cf36489SGerd Hoffmann          * Useful for shared desktop sessions where you don't want
23028cf36489SGerd Hoffmann          * someone forgetting to say -shared when running the vnc
23038cf36489SGerd Hoffmann          * client disconnect everybody else.
23048cf36489SGerd Hoffmann          */
23058cf36489SGerd Hoffmann         if (mode == VNC_SHARE_MODE_EXCLUSIVE) {
23068cf36489SGerd Hoffmann             vnc_disconnect_start(vs);
23078cf36489SGerd Hoffmann             return 0;
23088cf36489SGerd Hoffmann         }
23098cf36489SGerd Hoffmann         break;
23108cf36489SGerd Hoffmann     }
23118cf36489SGerd Hoffmann     vnc_set_share_mode(vs, mode);
23128cf36489SGerd Hoffmann 
2313d39fa6d8SGerd Hoffmann     vs->client_width = surface_width(vs->vd->ds);
2314d39fa6d8SGerd Hoffmann     vs->client_height = surface_height(vs->vd->ds);
23153e230dd2SCorentin Chary     vnc_write_u16(vs, vs->client_width);
23163e230dd2SCorentin Chary     vnc_write_u16(vs, vs->client_height);
23173e230dd2SCorentin Chary 
23183e230dd2SCorentin Chary     pixel_format_message(vs);
23193e230dd2SCorentin Chary 
23203e230dd2SCorentin Chary     if (qemu_name)
23213e230dd2SCorentin Chary         size = snprintf(buf, sizeof(buf), "QEMU (%s)", qemu_name);
23223e230dd2SCorentin Chary     else
23233e230dd2SCorentin Chary         size = snprintf(buf, sizeof(buf), "QEMU");
23243e230dd2SCorentin Chary 
23253e230dd2SCorentin Chary     vnc_write_u32(vs, size);
23263e230dd2SCorentin Chary     vnc_write(vs, buf, size);
23273e230dd2SCorentin Chary     vnc_flush(vs);
23283e230dd2SCorentin Chary 
23293e230dd2SCorentin Chary     vnc_client_cache_auth(vs);
2330*fb6ba0d5SWenchao Xia     vnc_qmp_event(vs, QAPI_EVENT_VNC_INITIALIZED);
23313e230dd2SCorentin Chary 
23323e230dd2SCorentin Chary     vnc_read_when(vs, protocol_client_msg, 1);
23333e230dd2SCorentin Chary 
23343e230dd2SCorentin Chary     return 0;
23353e230dd2SCorentin Chary }
23363e230dd2SCorentin Chary 
23373e230dd2SCorentin Chary void start_client_init(VncState *vs)
23383e230dd2SCorentin Chary {
23393e230dd2SCorentin Chary     vnc_read_when(vs, protocol_client_init, 1);
23403e230dd2SCorentin Chary }
23413e230dd2SCorentin Chary 
23423e230dd2SCorentin Chary static void make_challenge(VncState *vs)
23433e230dd2SCorentin Chary {
23443e230dd2SCorentin Chary     int i;
23453e230dd2SCorentin Chary 
23463e230dd2SCorentin Chary     srand(time(NULL)+getpid()+getpid()*987654+rand());
23473e230dd2SCorentin Chary 
23483e230dd2SCorentin Chary     for (i = 0 ; i < sizeof(vs->challenge) ; i++)
23493e230dd2SCorentin Chary         vs->challenge[i] = (int) (256.0*rand()/(RAND_MAX+1.0));
23503e230dd2SCorentin Chary }
23513e230dd2SCorentin Chary 
23523e230dd2SCorentin Chary static int protocol_client_auth_vnc(VncState *vs, uint8_t *data, size_t len)
23533e230dd2SCorentin Chary {
23543e230dd2SCorentin Chary     unsigned char response[VNC_AUTH_CHALLENGE_SIZE];
23553e230dd2SCorentin Chary     int i, j, pwlen;
23563e230dd2SCorentin Chary     unsigned char key[8];
23573c9405a0SGerd Hoffmann     time_t now = time(NULL);
23583e230dd2SCorentin Chary 
23591cd20f8bSAnthony Liguori     if (!vs->vd->password) {
23603e230dd2SCorentin Chary         VNC_DEBUG("No password configured on server");
23616bffdf0fSGerd Hoffmann         goto reject;
23623e230dd2SCorentin Chary     }
23633c9405a0SGerd Hoffmann     if (vs->vd->expires < now) {
23643c9405a0SGerd Hoffmann         VNC_DEBUG("Password is expired");
23653c9405a0SGerd Hoffmann         goto reject;
23663c9405a0SGerd Hoffmann     }
23673e230dd2SCorentin Chary 
23683e230dd2SCorentin Chary     memcpy(response, vs->challenge, VNC_AUTH_CHALLENGE_SIZE);
23693e230dd2SCorentin Chary 
23703e230dd2SCorentin Chary     /* Calculate the expected challenge response */
23713e230dd2SCorentin Chary     pwlen = strlen(vs->vd->password);
23723e230dd2SCorentin Chary     for (i=0; i<sizeof(key); i++)
23733e230dd2SCorentin Chary         key[i] = i<pwlen ? vs->vd->password[i] : 0;
23743e230dd2SCorentin Chary     deskey(key, EN0);
23753e230dd2SCorentin Chary     for (j = 0; j < VNC_AUTH_CHALLENGE_SIZE; j += 8)
23763e230dd2SCorentin Chary         des(response+j, response+j);
23773e230dd2SCorentin Chary 
23783e230dd2SCorentin Chary     /* Compare expected vs actual challenge response */
23793e230dd2SCorentin Chary     if (memcmp(response, data, VNC_AUTH_CHALLENGE_SIZE) != 0) {
2380e5bed759SDong Xu Wang         VNC_DEBUG("Client challenge response did not match\n");
23816bffdf0fSGerd Hoffmann         goto reject;
23826bffdf0fSGerd Hoffmann     } else {
23836bffdf0fSGerd Hoffmann         VNC_DEBUG("Accepting VNC challenge response\n");
23846bffdf0fSGerd Hoffmann         vnc_write_u32(vs, 0); /* Accept auth */
23856bffdf0fSGerd Hoffmann         vnc_flush(vs);
23866bffdf0fSGerd Hoffmann 
23876bffdf0fSGerd Hoffmann         start_client_init(vs);
23886bffdf0fSGerd Hoffmann     }
23896bffdf0fSGerd Hoffmann     return 0;
23906bffdf0fSGerd Hoffmann 
23916bffdf0fSGerd Hoffmann reject:
23923e230dd2SCorentin Chary     vnc_write_u32(vs, 1); /* Reject auth */
23933e230dd2SCorentin Chary     if (vs->minor >= 8) {
23943e230dd2SCorentin Chary         static const char err[] = "Authentication failed";
23953e230dd2SCorentin Chary         vnc_write_u32(vs, sizeof(err));
23963e230dd2SCorentin Chary         vnc_write(vs, err, sizeof(err));
23973e230dd2SCorentin Chary     }
23983e230dd2SCorentin Chary     vnc_flush(vs);
23993e230dd2SCorentin Chary     vnc_client_error(vs);
24003e230dd2SCorentin Chary     return 0;
24013e230dd2SCorentin Chary }
24023e230dd2SCorentin Chary 
24033e230dd2SCorentin Chary void start_auth_vnc(VncState *vs)
24043e230dd2SCorentin Chary {
24053e230dd2SCorentin Chary     make_challenge(vs);
24063e230dd2SCorentin Chary     /* Send client a 'random' challenge */
24073e230dd2SCorentin Chary     vnc_write(vs, vs->challenge, sizeof(vs->challenge));
24083e230dd2SCorentin Chary     vnc_flush(vs);
24093e230dd2SCorentin Chary 
24103e230dd2SCorentin Chary     vnc_read_when(vs, protocol_client_auth_vnc, sizeof(vs->challenge));
24113e230dd2SCorentin Chary }
24123e230dd2SCorentin Chary 
24133e230dd2SCorentin Chary 
24143e230dd2SCorentin Chary static int protocol_client_auth(VncState *vs, uint8_t *data, size_t len)
24153e230dd2SCorentin Chary {
24163e230dd2SCorentin Chary     /* We only advertise 1 auth scheme at a time, so client
24173e230dd2SCorentin Chary      * must pick the one we sent. Verify this */
24187e7e2ebcSDaniel P. Berrange     if (data[0] != vs->auth) { /* Reject auth */
24193e230dd2SCorentin Chary        VNC_DEBUG("Reject auth %d because it didn't match advertized\n", (int)data[0]);
24203e230dd2SCorentin Chary        vnc_write_u32(vs, 1);
24213e230dd2SCorentin Chary        if (vs->minor >= 8) {
24223e230dd2SCorentin Chary            static const char err[] = "Authentication failed";
24233e230dd2SCorentin Chary            vnc_write_u32(vs, sizeof(err));
24243e230dd2SCorentin Chary            vnc_write(vs, err, sizeof(err));
24253e230dd2SCorentin Chary        }
24263e230dd2SCorentin Chary        vnc_client_error(vs);
24273e230dd2SCorentin Chary     } else { /* Accept requested auth */
24283e230dd2SCorentin Chary        VNC_DEBUG("Client requested auth %d\n", (int)data[0]);
24297e7e2ebcSDaniel P. Berrange        switch (vs->auth) {
24303e230dd2SCorentin Chary        case VNC_AUTH_NONE:
24313e230dd2SCorentin Chary            VNC_DEBUG("Accept auth none\n");
24323e230dd2SCorentin Chary            if (vs->minor >= 8) {
24333e230dd2SCorentin Chary                vnc_write_u32(vs, 0); /* Accept auth completion */
24343e230dd2SCorentin Chary                vnc_flush(vs);
24353e230dd2SCorentin Chary            }
24363e230dd2SCorentin Chary            start_client_init(vs);
24373e230dd2SCorentin Chary            break;
24383e230dd2SCorentin Chary 
24393e230dd2SCorentin Chary        case VNC_AUTH_VNC:
24403e230dd2SCorentin Chary            VNC_DEBUG("Start VNC auth\n");
24413e230dd2SCorentin Chary            start_auth_vnc(vs);
24423e230dd2SCorentin Chary            break;
24433e230dd2SCorentin Chary 
24443e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS
24453e230dd2SCorentin Chary        case VNC_AUTH_VENCRYPT:
24463a93113aSDong Xu Wang            VNC_DEBUG("Accept VeNCrypt auth\n");
24473e230dd2SCorentin Chary            start_auth_vencrypt(vs);
24483e230dd2SCorentin Chary            break;
24493e230dd2SCorentin Chary #endif /* CONFIG_VNC_TLS */
24503e230dd2SCorentin Chary 
24513e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL
24523e230dd2SCorentin Chary        case VNC_AUTH_SASL:
24533e230dd2SCorentin Chary            VNC_DEBUG("Accept SASL auth\n");
24543e230dd2SCorentin Chary            start_auth_sasl(vs);
24553e230dd2SCorentin Chary            break;
24563e230dd2SCorentin Chary #endif /* CONFIG_VNC_SASL */
24573e230dd2SCorentin Chary 
24583e230dd2SCorentin Chary        default: /* Should not be possible, but just in case */
24597e7e2ebcSDaniel P. Berrange            VNC_DEBUG("Reject auth %d server code bug\n", vs->auth);
24603e230dd2SCorentin Chary            vnc_write_u8(vs, 1);
24613e230dd2SCorentin Chary            if (vs->minor >= 8) {
24623e230dd2SCorentin Chary                static const char err[] = "Authentication failed";
24633e230dd2SCorentin Chary                vnc_write_u32(vs, sizeof(err));
24643e230dd2SCorentin Chary                vnc_write(vs, err, sizeof(err));
24653e230dd2SCorentin Chary            }
24663e230dd2SCorentin Chary            vnc_client_error(vs);
24673e230dd2SCorentin Chary        }
24683e230dd2SCorentin Chary     }
24693e230dd2SCorentin Chary     return 0;
24703e230dd2SCorentin Chary }
24713e230dd2SCorentin Chary 
24723e230dd2SCorentin Chary static int protocol_version(VncState *vs, uint8_t *version, size_t len)
24733e230dd2SCorentin Chary {
24743e230dd2SCorentin Chary     char local[13];
24753e230dd2SCorentin Chary 
24763e230dd2SCorentin Chary     memcpy(local, version, 12);
24773e230dd2SCorentin Chary     local[12] = 0;
24783e230dd2SCorentin Chary 
24793e230dd2SCorentin Chary     if (sscanf(local, "RFB %03d.%03d\n", &vs->major, &vs->minor) != 2) {
24803e230dd2SCorentin Chary         VNC_DEBUG("Malformed protocol version %s\n", local);
24813e230dd2SCorentin Chary         vnc_client_error(vs);
24823e230dd2SCorentin Chary         return 0;
24833e230dd2SCorentin Chary     }
24843e230dd2SCorentin Chary     VNC_DEBUG("Client request protocol version %d.%d\n", vs->major, vs->minor);
24853e230dd2SCorentin Chary     if (vs->major != 3 ||
24863e230dd2SCorentin Chary         (vs->minor != 3 &&
24873e230dd2SCorentin Chary          vs->minor != 4 &&
24883e230dd2SCorentin Chary          vs->minor != 5 &&
24893e230dd2SCorentin Chary          vs->minor != 7 &&
24903e230dd2SCorentin Chary          vs->minor != 8)) {
24913e230dd2SCorentin Chary         VNC_DEBUG("Unsupported client version\n");
24923e230dd2SCorentin Chary         vnc_write_u32(vs, VNC_AUTH_INVALID);
24933e230dd2SCorentin Chary         vnc_flush(vs);
24943e230dd2SCorentin Chary         vnc_client_error(vs);
24953e230dd2SCorentin Chary         return 0;
24963e230dd2SCorentin Chary     }
24973e230dd2SCorentin Chary     /* Some broken clients report v3.4 or v3.5, which spec requires to be treated
24983e230dd2SCorentin Chary      * as equivalent to v3.3 by servers
24993e230dd2SCorentin Chary      */
25003e230dd2SCorentin Chary     if (vs->minor == 4 || vs->minor == 5)
25013e230dd2SCorentin Chary         vs->minor = 3;
25023e230dd2SCorentin Chary 
25033e230dd2SCorentin Chary     if (vs->minor == 3) {
25047e7e2ebcSDaniel P. Berrange         if (vs->auth == VNC_AUTH_NONE) {
25053e230dd2SCorentin Chary             VNC_DEBUG("Tell client auth none\n");
25067e7e2ebcSDaniel P. Berrange             vnc_write_u32(vs, vs->auth);
25073e230dd2SCorentin Chary             vnc_flush(vs);
25083e230dd2SCorentin Chary             start_client_init(vs);
25097e7e2ebcSDaniel P. Berrange        } else if (vs->auth == VNC_AUTH_VNC) {
25103e230dd2SCorentin Chary             VNC_DEBUG("Tell client VNC auth\n");
25117e7e2ebcSDaniel P. Berrange             vnc_write_u32(vs, vs->auth);
25123e230dd2SCorentin Chary             vnc_flush(vs);
25133e230dd2SCorentin Chary             start_auth_vnc(vs);
25143e230dd2SCorentin Chary        } else {
25157e7e2ebcSDaniel P. Berrange             VNC_DEBUG("Unsupported auth %d for protocol 3.3\n", vs->auth);
25163e230dd2SCorentin Chary             vnc_write_u32(vs, VNC_AUTH_INVALID);
25173e230dd2SCorentin Chary             vnc_flush(vs);
25183e230dd2SCorentin Chary             vnc_client_error(vs);
25193e230dd2SCorentin Chary        }
25203e230dd2SCorentin Chary     } else {
25217e7e2ebcSDaniel P. Berrange         VNC_DEBUG("Telling client we support auth %d\n", vs->auth);
25223e230dd2SCorentin Chary         vnc_write_u8(vs, 1); /* num auth */
25237e7e2ebcSDaniel P. Berrange         vnc_write_u8(vs, vs->auth);
25243e230dd2SCorentin Chary         vnc_read_when(vs, protocol_client_auth, 1);
25253e230dd2SCorentin Chary         vnc_flush(vs);
25263e230dd2SCorentin Chary     }
25273e230dd2SCorentin Chary 
25283e230dd2SCorentin Chary     return 0;
25293e230dd2SCorentin Chary }
25303e230dd2SCorentin Chary 
2531999342a0SCorentin Chary static VncRectStat *vnc_stat_rect(VncDisplay *vd, int x, int y)
2532999342a0SCorentin Chary {
2533999342a0SCorentin Chary     struct VncSurface *vs = &vd->guest;
2534999342a0SCorentin Chary 
2535999342a0SCorentin Chary     return &vs->stats[y / VNC_STAT_RECT][x / VNC_STAT_RECT];
2536999342a0SCorentin Chary }
2537999342a0SCorentin Chary 
25387d964c9dSCorentin Chary void vnc_sent_lossy_rect(VncState *vs, int x, int y, int w, int h)
25397d964c9dSCorentin Chary {
25407d964c9dSCorentin Chary     int i, j;
25417d964c9dSCorentin Chary 
25427d964c9dSCorentin Chary     w = (x + w) / VNC_STAT_RECT;
25437d964c9dSCorentin Chary     h = (y + h) / VNC_STAT_RECT;
25447d964c9dSCorentin Chary     x /= VNC_STAT_RECT;
25457d964c9dSCorentin Chary     y /= VNC_STAT_RECT;
25467d964c9dSCorentin Chary 
2547207f328aSCorentin Chary     for (j = y; j <= h; j++) {
2548207f328aSCorentin Chary         for (i = x; i <= w; i++) {
25497d964c9dSCorentin Chary             vs->lossy_rect[j][i] = 1;
25507d964c9dSCorentin Chary         }
25517d964c9dSCorentin Chary     }
25527d964c9dSCorentin Chary }
25537d964c9dSCorentin Chary 
25547d964c9dSCorentin Chary static int vnc_refresh_lossy_rect(VncDisplay *vd, int x, int y)
25557d964c9dSCorentin Chary {
25567d964c9dSCorentin Chary     VncState *vs;
25577d964c9dSCorentin Chary     int sty = y / VNC_STAT_RECT;
25587d964c9dSCorentin Chary     int stx = x / VNC_STAT_RECT;
25597d964c9dSCorentin Chary     int has_dirty = 0;
25607d964c9dSCorentin Chary 
25617d964c9dSCorentin Chary     y = y / VNC_STAT_RECT * VNC_STAT_RECT;
25627d964c9dSCorentin Chary     x = x / VNC_STAT_RECT * VNC_STAT_RECT;
25637d964c9dSCorentin Chary 
25647d964c9dSCorentin Chary     QTAILQ_FOREACH(vs, &vd->clients, next) {
2565bc2429b9SCorentin Chary         int j;
25667d964c9dSCorentin Chary 
25677d964c9dSCorentin Chary         /* kernel send buffers are full -> refresh later */
25687d964c9dSCorentin Chary         if (vs->output.offset) {
25697d964c9dSCorentin Chary             continue;
25707d964c9dSCorentin Chary         }
25717d964c9dSCorentin Chary 
25727d964c9dSCorentin Chary         if (!vs->lossy_rect[sty][stx]) {
25737d964c9dSCorentin Chary             continue;
25747d964c9dSCorentin Chary         }
2575207f328aSCorentin Chary 
25767d964c9dSCorentin Chary         vs->lossy_rect[sty][stx] = 0;
25777d964c9dSCorentin Chary         for (j = 0; j < VNC_STAT_RECT; ++j) {
2578b4c85ddcSPeter Lieven             bitmap_set(vs->dirty[y + j],
2579b4c85ddcSPeter Lieven                        x / VNC_DIRTY_PIXELS_PER_BIT,
2580b4c85ddcSPeter Lieven                        VNC_STAT_RECT / VNC_DIRTY_PIXELS_PER_BIT);
25817d964c9dSCorentin Chary         }
25827d964c9dSCorentin Chary         has_dirty++;
25837d964c9dSCorentin Chary     }
2584207f328aSCorentin Chary 
25857d964c9dSCorentin Chary     return has_dirty;
25867d964c9dSCorentin Chary }
25877d964c9dSCorentin Chary 
25887d964c9dSCorentin Chary static int vnc_update_stats(VncDisplay *vd,  struct timeval * tv)
2589999342a0SCorentin Chary {
25909f64916dSGerd Hoffmann     int width = pixman_image_get_width(vd->guest.fb);
25919f64916dSGerd Hoffmann     int height = pixman_image_get_height(vd->guest.fb);
2592999342a0SCorentin Chary     int x, y;
2593999342a0SCorentin Chary     struct timeval res;
25947d964c9dSCorentin Chary     int has_dirty = 0;
2595999342a0SCorentin Chary 
25969f64916dSGerd Hoffmann     for (y = 0; y < height; y += VNC_STAT_RECT) {
25979f64916dSGerd Hoffmann         for (x = 0; x < width; x += VNC_STAT_RECT) {
2598999342a0SCorentin Chary             VncRectStat *rect = vnc_stat_rect(vd, x, y);
2599999342a0SCorentin Chary 
2600999342a0SCorentin Chary             rect->updated = false;
2601999342a0SCorentin Chary         }
2602999342a0SCorentin Chary     }
2603999342a0SCorentin Chary 
2604ad620c29SBlue Swirl     qemu_timersub(tv, &VNC_REFRESH_STATS, &res);
2605999342a0SCorentin Chary 
2606999342a0SCorentin Chary     if (timercmp(&vd->guest.last_freq_check, &res, >)) {
26077d964c9dSCorentin Chary         return has_dirty;
2608999342a0SCorentin Chary     }
2609999342a0SCorentin Chary     vd->guest.last_freq_check = *tv;
2610999342a0SCorentin Chary 
26119f64916dSGerd Hoffmann     for (y = 0; y < height; y += VNC_STAT_RECT) {
26129f64916dSGerd Hoffmann         for (x = 0; x < width; x += VNC_STAT_RECT) {
2613999342a0SCorentin Chary             VncRectStat *rect= vnc_stat_rect(vd, x, y);
2614999342a0SCorentin Chary             int count = ARRAY_SIZE(rect->times);
2615999342a0SCorentin Chary             struct timeval min, max;
2616999342a0SCorentin Chary 
2617999342a0SCorentin Chary             if (!timerisset(&rect->times[count - 1])) {
2618999342a0SCorentin Chary                 continue ;
2619999342a0SCorentin Chary             }
2620999342a0SCorentin Chary 
2621999342a0SCorentin Chary             max = rect->times[(rect->idx + count - 1) % count];
2622ad620c29SBlue Swirl             qemu_timersub(tv, &max, &res);
2623999342a0SCorentin Chary 
2624999342a0SCorentin Chary             if (timercmp(&res, &VNC_REFRESH_LOSSY, >)) {
2625999342a0SCorentin Chary                 rect->freq = 0;
26267d964c9dSCorentin Chary                 has_dirty += vnc_refresh_lossy_rect(vd, x, y);
2627999342a0SCorentin Chary                 memset(rect->times, 0, sizeof (rect->times));
2628999342a0SCorentin Chary                 continue ;
2629999342a0SCorentin Chary             }
2630999342a0SCorentin Chary 
2631999342a0SCorentin Chary             min = rect->times[rect->idx];
2632999342a0SCorentin Chary             max = rect->times[(rect->idx + count - 1) % count];
2633ad620c29SBlue Swirl             qemu_timersub(&max, &min, &res);
2634999342a0SCorentin Chary 
2635999342a0SCorentin Chary             rect->freq = res.tv_sec + res.tv_usec / 1000000.;
2636999342a0SCorentin Chary             rect->freq /= count;
2637999342a0SCorentin Chary             rect->freq = 1. / rect->freq;
2638999342a0SCorentin Chary         }
2639999342a0SCorentin Chary     }
26407d964c9dSCorentin Chary     return has_dirty;
2641999342a0SCorentin Chary }
2642999342a0SCorentin Chary 
2643999342a0SCorentin Chary double vnc_update_freq(VncState *vs, int x, int y, int w, int h)
2644999342a0SCorentin Chary {
2645999342a0SCorentin Chary     int i, j;
2646999342a0SCorentin Chary     double total = 0;
2647999342a0SCorentin Chary     int num = 0;
2648999342a0SCorentin Chary 
2649999342a0SCorentin Chary     x =  (x / VNC_STAT_RECT) * VNC_STAT_RECT;
2650999342a0SCorentin Chary     y =  (y / VNC_STAT_RECT) * VNC_STAT_RECT;
2651999342a0SCorentin Chary 
2652999342a0SCorentin Chary     for (j = y; j <= y + h; j += VNC_STAT_RECT) {
2653999342a0SCorentin Chary         for (i = x; i <= x + w; i += VNC_STAT_RECT) {
2654999342a0SCorentin Chary             total += vnc_stat_rect(vs->vd, i, j)->freq;
2655999342a0SCorentin Chary             num++;
2656999342a0SCorentin Chary         }
2657999342a0SCorentin Chary     }
2658999342a0SCorentin Chary 
2659999342a0SCorentin Chary     if (num) {
2660999342a0SCorentin Chary         return total / num;
2661999342a0SCorentin Chary     } else {
2662999342a0SCorentin Chary         return 0;
2663999342a0SCorentin Chary     }
2664999342a0SCorentin Chary }
2665999342a0SCorentin Chary 
2666999342a0SCorentin Chary static void vnc_rect_updated(VncDisplay *vd, int x, int y, struct timeval * tv)
2667999342a0SCorentin Chary {
2668999342a0SCorentin Chary     VncRectStat *rect;
2669999342a0SCorentin Chary 
2670999342a0SCorentin Chary     rect = vnc_stat_rect(vd, x, y);
2671999342a0SCorentin Chary     if (rect->updated) {
2672999342a0SCorentin Chary         return ;
2673999342a0SCorentin Chary     }
2674999342a0SCorentin Chary     rect->times[rect->idx] = *tv;
2675999342a0SCorentin Chary     rect->idx = (rect->idx + 1) % ARRAY_SIZE(rect->times);
2676999342a0SCorentin Chary     rect->updated = true;
2677999342a0SCorentin Chary }
2678999342a0SCorentin Chary 
26793e230dd2SCorentin Chary static int vnc_refresh_server_surface(VncDisplay *vd)
26803e230dd2SCorentin Chary {
26819f64916dSGerd Hoffmann     int width = pixman_image_get_width(vd->guest.fb);
26829f64916dSGerd Hoffmann     int height = pixman_image_get_height(vd->guest.fb);
26833e230dd2SCorentin Chary     int y;
268412b316d4SPeter Lieven     uint8_t *guest_row0 = NULL, *server_row0;
268512b316d4SPeter Lieven     int guest_stride = 0, server_stride;
26863e230dd2SCorentin Chary     int cmp_bytes;
26873e230dd2SCorentin Chary     VncState *vs;
26883e230dd2SCorentin Chary     int has_dirty = 0;
26899f64916dSGerd Hoffmann     pixman_image_t *tmpbuf = NULL;
26903e230dd2SCorentin Chary 
269180e0c8c3SCorentin Chary     struct timeval tv = { 0, 0 };
2692999342a0SCorentin Chary 
269380e0c8c3SCorentin Chary     if (!vd->non_adaptive) {
2694999342a0SCorentin Chary         gettimeofday(&tv, NULL);
26957d964c9dSCorentin Chary         has_dirty = vnc_update_stats(vd, &tv);
269680e0c8c3SCorentin Chary     }
2697999342a0SCorentin Chary 
26983e230dd2SCorentin Chary     /*
26993e230dd2SCorentin Chary      * Walk through the guest dirty map.
27003e230dd2SCorentin Chary      * Check and copy modified bits from guest to server surface.
27013e230dd2SCorentin Chary      * Update server dirty map.
27023e230dd2SCorentin Chary      */
27036cd859aaSPeter Lieven     cmp_bytes = VNC_DIRTY_PIXELS_PER_BIT * VNC_SERVER_FB_BYTES;
27049f64916dSGerd Hoffmann     if (cmp_bytes > vnc_server_fb_stride(vd)) {
27059f64916dSGerd Hoffmann         cmp_bytes = vnc_server_fb_stride(vd);
27069e4dd565SStefan Weil     }
27079f64916dSGerd Hoffmann     if (vd->guest.format != VNC_SERVER_FB_FORMAT) {
27089f64916dSGerd Hoffmann         int width = pixman_image_get_width(vd->server);
27099f64916dSGerd Hoffmann         tmpbuf = qemu_pixman_linebuf_create(VNC_SERVER_FB_FORMAT, width);
271012b316d4SPeter Lieven     } else {
271112b316d4SPeter Lieven         guest_row0 = (uint8_t *)pixman_image_get_data(vd->guest.fb);
271212b316d4SPeter Lieven         guest_stride = pixman_image_get_stride(vd->guest.fb);
27139f64916dSGerd Hoffmann     }
271412b316d4SPeter Lieven     server_row0 = (uint8_t *)pixman_image_get_data(vd->server);
271512b316d4SPeter Lieven     server_stride = pixman_image_get_stride(vd->server);
271612b316d4SPeter Lieven 
271712b316d4SPeter Lieven     y = 0;
271812b316d4SPeter Lieven     for (;;) {
27193e230dd2SCorentin Chary         int x;
272012b316d4SPeter Lieven         uint8_t *guest_ptr, *server_ptr;
272112b316d4SPeter Lieven         unsigned long offset = find_next_bit((unsigned long *) &vd->guest.dirty,
272212b316d4SPeter Lieven                                              height * VNC_DIRTY_BPL(&vd->guest),
272312b316d4SPeter Lieven                                              y * VNC_DIRTY_BPL(&vd->guest));
272412b316d4SPeter Lieven         if (offset == height * VNC_DIRTY_BPL(&vd->guest)) {
272512b316d4SPeter Lieven             /* no more dirty bits */
272612b316d4SPeter Lieven             break;
272712b316d4SPeter Lieven         }
272812b316d4SPeter Lieven         y = offset / VNC_DIRTY_BPL(&vd->guest);
272912b316d4SPeter Lieven         x = offset % VNC_DIRTY_BPL(&vd->guest);
273012b316d4SPeter Lieven 
273112b316d4SPeter Lieven         server_ptr = server_row0 + y * server_stride + x * cmp_bytes;
27323e230dd2SCorentin Chary 
27339f64916dSGerd Hoffmann         if (vd->guest.format != VNC_SERVER_FB_FORMAT) {
2734bc210eb1SGerd Hoffmann             qemu_pixman_linebuf_fill(tmpbuf, vd->guest.fb, width, 0, y);
27359f64916dSGerd Hoffmann             guest_ptr = (uint8_t *)pixman_image_get_data(tmpbuf);
27369f64916dSGerd Hoffmann         } else {
273712b316d4SPeter Lieven             guest_ptr = guest_row0 + y * guest_stride;
27389f64916dSGerd Hoffmann         }
273912b316d4SPeter Lieven         guest_ptr += x * cmp_bytes;
27403e230dd2SCorentin Chary 
274112b316d4SPeter Lieven         for (; x < DIV_ROUND_UP(width, VNC_DIRTY_PIXELS_PER_BIT);
274212b316d4SPeter Lieven              x++, guest_ptr += cmp_bytes, server_ptr += cmp_bytes) {
274312b316d4SPeter Lieven             if (!test_and_clear_bit(x, vd->guest.dirty[y])) {
27443e230dd2SCorentin Chary                 continue;
2745b4c85ddcSPeter Lieven             }
2746b4c85ddcSPeter Lieven             if (memcmp(server_ptr, guest_ptr, cmp_bytes) == 0) {
27473e230dd2SCorentin Chary                 continue;
2748b4c85ddcSPeter Lieven             }
27493e230dd2SCorentin Chary             memcpy(server_ptr, guest_ptr, cmp_bytes);
275012b316d4SPeter Lieven             if (!vd->non_adaptive) {
275112b316d4SPeter Lieven                 vnc_rect_updated(vd, x * VNC_DIRTY_PIXELS_PER_BIT,
275212b316d4SPeter Lieven                                  y, &tv);
275312b316d4SPeter Lieven             }
27543e230dd2SCorentin Chary             QTAILQ_FOREACH(vs, &vd->clients, next) {
275512b316d4SPeter Lieven                 set_bit(x, vs->dirty[y]);
27563e230dd2SCorentin Chary             }
27573e230dd2SCorentin Chary             has_dirty++;
27583e230dd2SCorentin Chary         }
275912b316d4SPeter Lieven 
276012b316d4SPeter Lieven         y++;
27613e230dd2SCorentin Chary     }
27629f64916dSGerd Hoffmann     qemu_pixman_image_unref(tmpbuf);
27633e230dd2SCorentin Chary     return has_dirty;
27643e230dd2SCorentin Chary }
27653e230dd2SCorentin Chary 
27660f7b2864SGerd Hoffmann static void vnc_refresh(DisplayChangeListener *dcl)
27673e230dd2SCorentin Chary {
27680f7b2864SGerd Hoffmann     VncDisplay *vd = container_of(dcl, VncDisplay, dcl);
27693e230dd2SCorentin Chary     VncState *vs, *vn;
27703e230dd2SCorentin Chary     int has_dirty, rects = 0;
27713e230dd2SCorentin Chary 
27721dbfa005SGerd Hoffmann     graphic_hw_update(NULL);
27733e230dd2SCorentin Chary 
2774bd023f95SCorentin Chary     if (vnc_trylock_display(vd)) {
27750f7b2864SGerd Hoffmann         update_displaychangelistener(&vd->dcl, VNC_REFRESH_INTERVAL_BASE);
2776bd023f95SCorentin Chary         return;
2777bd023f95SCorentin Chary     }
2778bd023f95SCorentin Chary 
27793e230dd2SCorentin Chary     has_dirty = vnc_refresh_server_surface(vd);
2780bd023f95SCorentin Chary     vnc_unlock_display(vd);
27813e230dd2SCorentin Chary 
27823e230dd2SCorentin Chary     QTAILQ_FOREACH_SAFE(vs, &vd->clients, next, vn) {
278338ee14f4SGerd Hoffmann         rects += vnc_update_client(vs, has_dirty, false);
27843e230dd2SCorentin Chary         /* vs might be free()ed here */
27853e230dd2SCorentin Chary     }
2786bd023f95SCorentin Chary 
27870f7b2864SGerd Hoffmann     if (QTAILQ_EMPTY(&vd->clients)) {
27880f7b2864SGerd Hoffmann         update_displaychangelistener(&vd->dcl, VNC_REFRESH_INTERVAL_MAX);
27893e230dd2SCorentin Chary         return;
27900f7b2864SGerd Hoffmann     }
27913e230dd2SCorentin Chary 
27923e230dd2SCorentin Chary     if (has_dirty && rects) {
27930f7b2864SGerd Hoffmann         vd->dcl.update_interval /= 2;
27940f7b2864SGerd Hoffmann         if (vd->dcl.update_interval < VNC_REFRESH_INTERVAL_BASE) {
27950f7b2864SGerd Hoffmann             vd->dcl.update_interval = VNC_REFRESH_INTERVAL_BASE;
27960f7b2864SGerd Hoffmann         }
27973e230dd2SCorentin Chary     } else {
27980f7b2864SGerd Hoffmann         vd->dcl.update_interval += VNC_REFRESH_INTERVAL_INC;
27990f7b2864SGerd Hoffmann         if (vd->dcl.update_interval > VNC_REFRESH_INTERVAL_MAX) {
28000f7b2864SGerd Hoffmann             vd->dcl.update_interval = VNC_REFRESH_INTERVAL_MAX;
28013e230dd2SCorentin Chary         }
28023e230dd2SCorentin Chary     }
28033e230dd2SCorentin Chary }
28043e230dd2SCorentin Chary 
28052c8cf549SMichael Tokarev static void vnc_connect(VncDisplay *vd, int csock,
28062c8cf549SMichael Tokarev                         bool skipauth, bool websocket)
28073e230dd2SCorentin Chary {
28087267c094SAnthony Liguori     VncState *vs = g_malloc0(sizeof(VncState));
28097d964c9dSCorentin Chary     int i;
28107d964c9dSCorentin Chary 
28113e230dd2SCorentin Chary     vs->csock = csock;
28127e7e2ebcSDaniel P. Berrange 
28137e7e2ebcSDaniel P. Berrange     if (skipauth) {
28147e7e2ebcSDaniel P. Berrange 	vs->auth = VNC_AUTH_NONE;
28157e7e2ebcSDaniel P. Berrange #ifdef CONFIG_VNC_TLS
28167e7e2ebcSDaniel P. Berrange 	vs->subauth = VNC_AUTH_INVALID;
28177e7e2ebcSDaniel P. Berrange #endif
28187e7e2ebcSDaniel P. Berrange     } else {
28197e7e2ebcSDaniel P. Berrange 	vs->auth = vd->auth;
28207e7e2ebcSDaniel P. Berrange #ifdef CONFIG_VNC_TLS
28217e7e2ebcSDaniel P. Berrange 	vs->subauth = vd->subauth;
28227e7e2ebcSDaniel P. Berrange #endif
28237e7e2ebcSDaniel P. Berrange     }
28247e7e2ebcSDaniel P. Berrange 
28257267c094SAnthony Liguori     vs->lossy_rect = g_malloc0(VNC_STAT_ROWS * sizeof (*vs->lossy_rect));
28267d964c9dSCorentin Chary     for (i = 0; i < VNC_STAT_ROWS; ++i) {
28277267c094SAnthony Liguori         vs->lossy_rect[i] = g_malloc0(VNC_STAT_COLS * sizeof (uint8_t));
28287d964c9dSCorentin Chary     }
28293e230dd2SCorentin Chary 
28303e230dd2SCorentin Chary     VNC_DEBUG("New client on socket %d\n", csock);
28310f7b2864SGerd Hoffmann     update_displaychangelistener(&vd->dcl, VNC_REFRESH_INTERVAL_BASE);
2832f9e8caccSStefan Hajnoczi     qemu_set_nonblock(vs->csock);
28337536ee4bSTim Hardeck #ifdef CONFIG_VNC_WS
28347536ee4bSTim Hardeck     if (websocket) {
28357536ee4bSTim Hardeck         vs->websocket = 1;
28360057a0d5STim Hardeck #ifdef CONFIG_VNC_TLS
28370057a0d5STim Hardeck         if (vd->tls.x509cert) {
28380057a0d5STim Hardeck             qemu_set_fd_handler2(vs->csock, NULL, vncws_tls_handshake_peek,
28390057a0d5STim Hardeck                                  NULL, vs);
28400057a0d5STim Hardeck         } else
28410057a0d5STim Hardeck #endif /* CONFIG_VNC_TLS */
28420057a0d5STim Hardeck         {
28430057a0d5STim Hardeck             qemu_set_fd_handler2(vs->csock, NULL, vncws_handshake_read,
28440057a0d5STim Hardeck                                  NULL, vs);
28450057a0d5STim Hardeck         }
28467536ee4bSTim Hardeck     } else
28477536ee4bSTim Hardeck #endif /* CONFIG_VNC_WS */
28487536ee4bSTim Hardeck     {
28493e230dd2SCorentin Chary         qemu_set_fd_handler2(vs->csock, NULL, vnc_client_read, NULL, vs);
28507536ee4bSTim Hardeck     }
28513e230dd2SCorentin Chary 
28523e230dd2SCorentin Chary     vnc_client_cache_addr(vs);
2853*fb6ba0d5SWenchao Xia     vnc_qmp_event(vs, QAPI_EVENT_VNC_CONNECTED);
28548cf36489SGerd Hoffmann     vnc_set_share_mode(vs, VNC_SHARE_MODE_CONNECTING);
28553e230dd2SCorentin Chary 
28563e230dd2SCorentin Chary     vs->vd = vd;
28577536ee4bSTim Hardeck 
28587536ee4bSTim Hardeck #ifdef CONFIG_VNC_WS
28597536ee4bSTim Hardeck     if (!vs->websocket)
28607536ee4bSTim Hardeck #endif
28617536ee4bSTim Hardeck     {
28627536ee4bSTim Hardeck         vnc_init_state(vs);
28637536ee4bSTim Hardeck     }
28647536ee4bSTim Hardeck }
28657536ee4bSTim Hardeck 
28667536ee4bSTim Hardeck void vnc_init_state(VncState *vs)
28677536ee4bSTim Hardeck {
28686fd8e79aSTim Hardeck     vs->initialized = true;
28697536ee4bSTim Hardeck     VncDisplay *vd = vs->vd;
28707536ee4bSTim Hardeck 
28713e230dd2SCorentin Chary     vs->last_x = -1;
28723e230dd2SCorentin Chary     vs->last_y = -1;
28733e230dd2SCorentin Chary 
28743e230dd2SCorentin Chary     vs->as.freq = 44100;
28753e230dd2SCorentin Chary     vs->as.nchannels = 2;
28763e230dd2SCorentin Chary     vs->as.fmt = AUD_FMT_S16;
28773e230dd2SCorentin Chary     vs->as.endianness = 0;
28783e230dd2SCorentin Chary 
2879bd023f95SCorentin Chary     qemu_mutex_init(&vs->output_mutex);
2880175b2a6eSCorentin Chary     vs->bh = qemu_bh_new(vnc_jobs_bh, vs);
2881bd023f95SCorentin Chary 
28823e230dd2SCorentin Chary     QTAILQ_INSERT_HEAD(&vd->clients, vs, next);
28833e230dd2SCorentin Chary 
28841dbfa005SGerd Hoffmann     graphic_hw_update(NULL);
28853e230dd2SCorentin Chary 
28863e230dd2SCorentin Chary     vnc_write(vs, "RFB 003.008\n", 12);
28873e230dd2SCorentin Chary     vnc_flush(vs);
28883e230dd2SCorentin Chary     vnc_read_when(vs, protocol_version, 12);
28893e230dd2SCorentin Chary     reset_keys(vs);
28903e230dd2SCorentin Chary     if (vs->vd->lock_key_sync)
28913e230dd2SCorentin Chary         vs->led = qemu_add_led_event_handler(kbd_leds, vs);
28923e230dd2SCorentin Chary 
28933e230dd2SCorentin Chary     vs->mouse_mode_notifier.notify = check_pointer_type_change;
28943e230dd2SCorentin Chary     qemu_add_mouse_mode_change_notifier(&vs->mouse_mode_notifier);
28953e230dd2SCorentin Chary 
28963e230dd2SCorentin Chary     /* vs might be free()ed here */
28973e230dd2SCorentin Chary }
28983e230dd2SCorentin Chary 
28997536ee4bSTim Hardeck static void vnc_listen_read(void *opaque, bool websocket)
29003e230dd2SCorentin Chary {
29013e230dd2SCorentin Chary     VncDisplay *vs = opaque;
29023e230dd2SCorentin Chary     struct sockaddr_in addr;
29033e230dd2SCorentin Chary     socklen_t addrlen = sizeof(addr);
29047536ee4bSTim Hardeck     int csock;
29053e230dd2SCorentin Chary 
29063e230dd2SCorentin Chary     /* Catch-up */
29071dbfa005SGerd Hoffmann     graphic_hw_update(NULL);
29087536ee4bSTim Hardeck #ifdef CONFIG_VNC_WS
29097536ee4bSTim Hardeck     if (websocket) {
29107536ee4bSTim Hardeck         csock = qemu_accept(vs->lwebsock, (struct sockaddr *)&addr, &addrlen);
29117536ee4bSTim Hardeck     } else
29127536ee4bSTim Hardeck #endif /* CONFIG_VNC_WS */
29137536ee4bSTim Hardeck     {
29147536ee4bSTim Hardeck         csock = qemu_accept(vs->lsock, (struct sockaddr *)&addr, &addrlen);
29157536ee4bSTim Hardeck     }
29163e230dd2SCorentin Chary 
29173e230dd2SCorentin Chary     if (csock != -1) {
29182c8cf549SMichael Tokarev         vnc_connect(vs, csock, false, websocket);
29193e230dd2SCorentin Chary     }
29203e230dd2SCorentin Chary }
29213e230dd2SCorentin Chary 
29227536ee4bSTim Hardeck static void vnc_listen_regular_read(void *opaque)
29237536ee4bSTim Hardeck {
29242c8cf549SMichael Tokarev     vnc_listen_read(opaque, false);
29257536ee4bSTim Hardeck }
29267536ee4bSTim Hardeck 
29277536ee4bSTim Hardeck #ifdef CONFIG_VNC_WS
29287536ee4bSTim Hardeck static void vnc_listen_websocket_read(void *opaque)
29297536ee4bSTim Hardeck {
29302c8cf549SMichael Tokarev     vnc_listen_read(opaque, true);
29317536ee4bSTim Hardeck }
29327536ee4bSTim Hardeck #endif /* CONFIG_VNC_WS */
29337536ee4bSTim Hardeck 
29347c20b4a3SGerd Hoffmann static const DisplayChangeListenerOps dcl_ops = {
29357c20b4a3SGerd Hoffmann     .dpy_name          = "vnc",
29360f7b2864SGerd Hoffmann     .dpy_refresh       = vnc_refresh,
29377c20b4a3SGerd Hoffmann     .dpy_gfx_copy      = vnc_dpy_copy,
29387c20b4a3SGerd Hoffmann     .dpy_gfx_update    = vnc_dpy_update,
2939c12aeb86SGerd Hoffmann     .dpy_gfx_switch    = vnc_dpy_switch,
29407c20b4a3SGerd Hoffmann     .dpy_mouse_set     = vnc_mouse_set,
29417c20b4a3SGerd Hoffmann     .dpy_cursor_define = vnc_dpy_cursor_define,
29427c20b4a3SGerd Hoffmann };
29437c20b4a3SGerd Hoffmann 
29443e230dd2SCorentin Chary void vnc_display_init(DisplayState *ds)
29453e230dd2SCorentin Chary {
29467267c094SAnthony Liguori     VncDisplay *vs = g_malloc0(sizeof(*vs));
29473e230dd2SCorentin Chary 
29483e230dd2SCorentin Chary     vnc_display = vs;
29493e230dd2SCorentin Chary 
29503e230dd2SCorentin Chary     vs->lsock = -1;
29517536ee4bSTim Hardeck #ifdef CONFIG_VNC_WS
29527536ee4bSTim Hardeck     vs->lwebsock = -1;
29537536ee4bSTim Hardeck #endif
29543e230dd2SCorentin Chary 
29553e230dd2SCorentin Chary     QTAILQ_INIT(&vs->clients);
29563c9405a0SGerd Hoffmann     vs->expires = TIME_MAX;
29573e230dd2SCorentin Chary 
295840066175SGerd Hoffmann     if (keyboard_layout) {
295940066175SGerd Hoffmann         trace_vnc_key_map_init(keyboard_layout);
29603e230dd2SCorentin Chary         vs->kbd_layout = init_keyboard_layout(name2keysym, keyboard_layout);
296140066175SGerd Hoffmann     } else {
29623e230dd2SCorentin Chary         vs->kbd_layout = init_keyboard_layout(name2keysym, "en-us");
296340066175SGerd Hoffmann     }
29643e230dd2SCorentin Chary 
29653e230dd2SCorentin Chary     if (!vs->kbd_layout)
29663e230dd2SCorentin Chary         exit(1);
29673e230dd2SCorentin Chary 
2968bd023f95SCorentin Chary     qemu_mutex_init(&vs->mutex);
2969bd023f95SCorentin Chary     vnc_start_worker_thread();
2970bd023f95SCorentin Chary 
297121ef45d7SGerd Hoffmann     vs->dcl.ops = &dcl_ops;
29725209089fSGerd Hoffmann     register_displaychangelistener(&vs->dcl);
29733e230dd2SCorentin Chary }
29743e230dd2SCorentin Chary 
29753e230dd2SCorentin Chary 
297671a8cdecSBlue Swirl static void vnc_display_close(DisplayState *ds)
29773e230dd2SCorentin Chary {
297821ef45d7SGerd Hoffmann     VncDisplay *vs = vnc_display;
29793e230dd2SCorentin Chary 
29803e230dd2SCorentin Chary     if (!vs)
29813e230dd2SCorentin Chary         return;
29827267c094SAnthony Liguori     g_free(vs->display);
29833e230dd2SCorentin Chary     vs->display = NULL;
29843e230dd2SCorentin Chary     if (vs->lsock != -1) {
29853e230dd2SCorentin Chary         qemu_set_fd_handler2(vs->lsock, NULL, NULL, NULL, NULL);
29863e230dd2SCorentin Chary         close(vs->lsock);
29873e230dd2SCorentin Chary         vs->lsock = -1;
29883e230dd2SCorentin Chary     }
29897536ee4bSTim Hardeck #ifdef CONFIG_VNC_WS
29907536ee4bSTim Hardeck     g_free(vs->ws_display);
29917536ee4bSTim Hardeck     vs->ws_display = NULL;
29927536ee4bSTim Hardeck     if (vs->lwebsock != -1) {
29937536ee4bSTim Hardeck         qemu_set_fd_handler2(vs->lwebsock, NULL, NULL, NULL, NULL);
29947536ee4bSTim Hardeck         close(vs->lwebsock);
29957536ee4bSTim Hardeck         vs->lwebsock = -1;
29967536ee4bSTim Hardeck     }
29977536ee4bSTim Hardeck #endif /* CONFIG_VNC_WS */
29983e230dd2SCorentin Chary     vs->auth = VNC_AUTH_INVALID;
29993e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS
30003e230dd2SCorentin Chary     vs->subauth = VNC_AUTH_INVALID;
30013e230dd2SCorentin Chary     vs->tls.x509verify = 0;
30023e230dd2SCorentin Chary #endif
30033e230dd2SCorentin Chary }
30043e230dd2SCorentin Chary 
30051cd20f8bSAnthony Liguori int vnc_display_password(DisplayState *ds, const char *password)
30061cd20f8bSAnthony Liguori {
300721ef45d7SGerd Hoffmann     VncDisplay *vs = vnc_display;
30081cd20f8bSAnthony Liguori 
30091cd20f8bSAnthony Liguori     if (!vs) {
3010a6aa9d3eSLuiz Capitulino         return -EINVAL;
30111cd20f8bSAnthony Liguori     }
3012cf864569SGerd Hoffmann     if (vs->auth == VNC_AUTH_NONE) {
3013cf864569SGerd Hoffmann         error_printf_unless_qmp("If you want use passwords please enable "
3014cf864569SGerd Hoffmann                                 "password auth using '-vnc ${dpy},password'.");
3015cf864569SGerd Hoffmann         return -EINVAL;
30161cd20f8bSAnthony Liguori     }
30171cd20f8bSAnthony Liguori 
30187267c094SAnthony Liguori     g_free(vs->password);
30197267c094SAnthony Liguori     vs->password = g_strdup(password);
3020a6aa9d3eSLuiz Capitulino 
3021a6aa9d3eSLuiz Capitulino     return 0;
30223e230dd2SCorentin Chary }
30233e230dd2SCorentin Chary 
30243c9405a0SGerd Hoffmann int vnc_display_pw_expire(DisplayState *ds, time_t expires)
30253c9405a0SGerd Hoffmann {
302621ef45d7SGerd Hoffmann     VncDisplay *vs = vnc_display;
30273c9405a0SGerd Hoffmann 
30281643f2b2SGerd Hoffmann     if (!vs) {
30291643f2b2SGerd Hoffmann         return -EINVAL;
30301643f2b2SGerd Hoffmann     }
30311643f2b2SGerd Hoffmann 
30323c9405a0SGerd Hoffmann     vs->expires = expires;
30333c9405a0SGerd Hoffmann     return 0;
30343c9405a0SGerd Hoffmann }
30353c9405a0SGerd Hoffmann 
30363e230dd2SCorentin Chary char *vnc_display_local_addr(DisplayState *ds)
30373e230dd2SCorentin Chary {
303821ef45d7SGerd Hoffmann     VncDisplay *vs = vnc_display;
30393e230dd2SCorentin Chary 
30403e230dd2SCorentin Chary     return vnc_socket_local_addr("%s:%s", vs->lsock);
30413e230dd2SCorentin Chary }
30423e230dd2SCorentin Chary 
30432d55f0e8SPaolo Bonzini void vnc_display_open(DisplayState *ds, const char *display, Error **errp)
30443e230dd2SCorentin Chary {
304521ef45d7SGerd Hoffmann     VncDisplay *vs = vnc_display;
30463e230dd2SCorentin Chary     const char *options;
30473e230dd2SCorentin Chary     int password = 0;
30483e230dd2SCorentin Chary     int reverse = 0;
30493e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS
30503e230dd2SCorentin Chary     int tls = 0, x509 = 0;
30513e230dd2SCorentin Chary #endif
30523e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL
30533e230dd2SCorentin Chary     int sasl = 0;
30543e230dd2SCorentin Chary     int saslErr;
30553e230dd2SCorentin Chary #endif
30562ded6ad7SBlue Swirl #if defined(CONFIG_VNC_TLS) || defined(CONFIG_VNC_SASL)
30573e230dd2SCorentin Chary     int acl = 0;
30582ded6ad7SBlue Swirl #endif
30593e230dd2SCorentin Chary     int lock_key_sync = 1;
30603e230dd2SCorentin Chary 
30612d55f0e8SPaolo Bonzini     if (!vnc_display) {
30622d55f0e8SPaolo Bonzini         error_setg(errp, "VNC display not active");
30632d55f0e8SPaolo Bonzini         return;
30642d55f0e8SPaolo Bonzini     }
30653e230dd2SCorentin Chary     vnc_display_close(ds);
30663e230dd2SCorentin Chary     if (strcmp(display, "none") == 0)
30672d55f0e8SPaolo Bonzini         return;
30683e230dd2SCorentin Chary 
30691ce52c78SPaolo Bonzini     vs->display = g_strdup(display);
30708cf36489SGerd Hoffmann     vs->share_policy = VNC_SHARE_POLICY_ALLOW_EXCLUSIVE;
30713e230dd2SCorentin Chary 
30723e230dd2SCorentin Chary     options = display;
30733e230dd2SCorentin Chary     while ((options = strchr(options, ','))) {
30743e230dd2SCorentin Chary         options++;
30753e230dd2SCorentin Chary         if (strncmp(options, "password", 8) == 0) {
30760f66998fSPaul Moore             if (fips_get_state()) {
30772d55f0e8SPaolo Bonzini                 error_setg(errp,
30780f66998fSPaul Moore                            "VNC password auth disabled due to FIPS mode, "
30790f66998fSPaul Moore                            "consider using the VeNCrypt or SASL authentication "
30802d55f0e8SPaolo Bonzini                            "methods as an alternative");
30811ce52c78SPaolo Bonzini                 goto fail;
30820f66998fSPaul Moore             }
30833e230dd2SCorentin Chary             password = 1; /* Require password auth */
30843e230dd2SCorentin Chary         } else if (strncmp(options, "reverse", 7) == 0) {
30853e230dd2SCorentin Chary             reverse = 1;
3086cee8e6adSStefan Hajnoczi         } else if (strncmp(options, "no-lock-key-sync", 16) == 0) {
30873e230dd2SCorentin Chary             lock_key_sync = 0;
30883e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL
30893e230dd2SCorentin Chary         } else if (strncmp(options, "sasl", 4) == 0) {
30903e230dd2SCorentin Chary             sasl = 1; /* Require SASL auth */
30913e230dd2SCorentin Chary #endif
30927536ee4bSTim Hardeck #ifdef CONFIG_VNC_WS
30937536ee4bSTim Hardeck         } else if (strncmp(options, "websocket", 9) == 0) {
30947536ee4bSTim Hardeck             char *start, *end;
30957536ee4bSTim Hardeck             vs->websocket = 1;
30967536ee4bSTim Hardeck 
30977536ee4bSTim Hardeck             /* Check for 'websocket=<port>' */
30987536ee4bSTim Hardeck             start = strchr(options, '=');
30997536ee4bSTim Hardeck             end = strchr(options, ',');
31007536ee4bSTim Hardeck             if (start && (!end || (start < end))) {
31017536ee4bSTim Hardeck                 int len = end ? end-(start+1) : strlen(start+1);
31027536ee4bSTim Hardeck                 if (len < 6) {
31037536ee4bSTim Hardeck                     /* extract the host specification from display */
31047536ee4bSTim Hardeck                     char  *host = NULL, *port = NULL, *host_end = NULL;
31057536ee4bSTim Hardeck                     port = g_strndup(start + 1, len);
31067536ee4bSTim Hardeck 
31077536ee4bSTim Hardeck                     /* ipv6 hosts have colons */
31087536ee4bSTim Hardeck                     end = strchr(display, ',');
31097536ee4bSTim Hardeck                     host_end = g_strrstr_len(display, end - display, ":");
31107536ee4bSTim Hardeck 
31117536ee4bSTim Hardeck                     if (host_end) {
31127536ee4bSTim Hardeck                         host = g_strndup(display, host_end - display + 1);
31137536ee4bSTim Hardeck                     } else {
31147536ee4bSTim Hardeck                         host = g_strndup(":", 1);
31157536ee4bSTim Hardeck                     }
31167536ee4bSTim Hardeck                     vs->ws_display = g_strconcat(host, port, NULL);
31177536ee4bSTim Hardeck                     g_free(host);
31187536ee4bSTim Hardeck                     g_free(port);
31197536ee4bSTim Hardeck                 }
31207536ee4bSTim Hardeck             }
31217536ee4bSTim Hardeck #endif /* CONFIG_VNC_WS */
31223e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS
31233e230dd2SCorentin Chary         } else if (strncmp(options, "tls", 3) == 0) {
31243e230dd2SCorentin Chary             tls = 1; /* Require TLS */
31253e230dd2SCorentin Chary         } else if (strncmp(options, "x509", 4) == 0) {
31263e230dd2SCorentin Chary             char *start, *end;
31273e230dd2SCorentin Chary             x509 = 1; /* Require x509 certificates */
31283e230dd2SCorentin Chary             if (strncmp(options, "x509verify", 10) == 0)
31293e230dd2SCorentin Chary                 vs->tls.x509verify = 1; /* ...and verify client certs */
31303e230dd2SCorentin Chary 
31313e230dd2SCorentin Chary             /* Now check for 'x509=/some/path' postfix
31323e230dd2SCorentin Chary              * and use that to setup x509 certificate/key paths */
31333e230dd2SCorentin Chary             start = strchr(options, '=');
31343e230dd2SCorentin Chary             end = strchr(options, ',');
31353e230dd2SCorentin Chary             if (start && (!end || (start < end))) {
31363e230dd2SCorentin Chary                 int len = end ? end-(start+1) : strlen(start+1);
31377267c094SAnthony Liguori                 char *path = g_strndup(start + 1, len);
31383e230dd2SCorentin Chary 
31393e230dd2SCorentin Chary                 VNC_DEBUG("Trying certificate path '%s'\n", path);
31403e230dd2SCorentin Chary                 if (vnc_tls_set_x509_creds_dir(vs, path) < 0) {
31412d55f0e8SPaolo Bonzini                     error_setg(errp, "Failed to find x509 certificates/keys in %s", path);
31427267c094SAnthony Liguori                     g_free(path);
31431ce52c78SPaolo Bonzini                     goto fail;
31443e230dd2SCorentin Chary                 }
31457267c094SAnthony Liguori                 g_free(path);
31463e230dd2SCorentin Chary             } else {
31472d55f0e8SPaolo Bonzini                 error_setg(errp, "No certificate path provided");
31481ce52c78SPaolo Bonzini                 goto fail;
31493e230dd2SCorentin Chary             }
31503e230dd2SCorentin Chary #endif
31512ded6ad7SBlue Swirl #if defined(CONFIG_VNC_TLS) || defined(CONFIG_VNC_SASL)
31523e230dd2SCorentin Chary         } else if (strncmp(options, "acl", 3) == 0) {
31533e230dd2SCorentin Chary             acl = 1;
31542ded6ad7SBlue Swirl #endif
31553e230dd2SCorentin Chary         } else if (strncmp(options, "lossy", 5) == 0) {
3156e22492d3SPeter Lieven #ifdef CONFIG_VNC_JPEG
31573e230dd2SCorentin Chary             vs->lossy = true;
3158e22492d3SPeter Lieven #endif
31597eff5742SCatalin Patulea         } else if (strncmp(options, "non-adaptive", 12) == 0) {
316080e0c8c3SCorentin Chary             vs->non_adaptive = true;
31618cf36489SGerd Hoffmann         } else if (strncmp(options, "share=", 6) == 0) {
31628cf36489SGerd Hoffmann             if (strncmp(options+6, "ignore", 6) == 0) {
31638cf36489SGerd Hoffmann                 vs->share_policy = VNC_SHARE_POLICY_IGNORE;
31648cf36489SGerd Hoffmann             } else if (strncmp(options+6, "allow-exclusive", 15) == 0) {
31658cf36489SGerd Hoffmann                 vs->share_policy = VNC_SHARE_POLICY_ALLOW_EXCLUSIVE;
31668cf36489SGerd Hoffmann             } else if (strncmp(options+6, "force-shared", 12) == 0) {
31678cf36489SGerd Hoffmann                 vs->share_policy = VNC_SHARE_POLICY_FORCE_SHARED;
31688cf36489SGerd Hoffmann             } else {
31692d55f0e8SPaolo Bonzini                 error_setg(errp, "unknown vnc share= option");
31701ce52c78SPaolo Bonzini                 goto fail;
31718cf36489SGerd Hoffmann             }
31723e230dd2SCorentin Chary         }
31733e230dd2SCorentin Chary     }
31743e230dd2SCorentin Chary 
3175e22492d3SPeter Lieven     /* adaptive updates are only used with tight encoding and
3176e22492d3SPeter Lieven      * if lossy updates are enabled so we can disable all the
3177e22492d3SPeter Lieven      * calculations otherwise */
3178e22492d3SPeter Lieven     if (!vs->lossy) {
3179e22492d3SPeter Lieven         vs->non_adaptive = true;
3180e22492d3SPeter Lieven     }
3181e22492d3SPeter Lieven 
31823e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS
31833e230dd2SCorentin Chary     if (acl && x509 && vs->tls.x509verify) {
31843e230dd2SCorentin Chary         if (!(vs->tls.acl = qemu_acl_init("vnc.x509dname"))) {
31853e230dd2SCorentin Chary             fprintf(stderr, "Failed to create x509 dname ACL\n");
31863e230dd2SCorentin Chary             exit(1);
31873e230dd2SCorentin Chary         }
31883e230dd2SCorentin Chary     }
31893e230dd2SCorentin Chary #endif
31903e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL
31913e230dd2SCorentin Chary     if (acl && sasl) {
31923e230dd2SCorentin Chary         if (!(vs->sasl.acl = qemu_acl_init("vnc.username"))) {
31933e230dd2SCorentin Chary             fprintf(stderr, "Failed to create username ACL\n");
31943e230dd2SCorentin Chary             exit(1);
31953e230dd2SCorentin Chary         }
31963e230dd2SCorentin Chary     }
31973e230dd2SCorentin Chary #endif
31983e230dd2SCorentin Chary 
31993e230dd2SCorentin Chary     /*
32003e230dd2SCorentin Chary      * Combinations we support here:
32013e230dd2SCorentin Chary      *
32023e230dd2SCorentin Chary      *  - no-auth                (clear text, no auth)
32033e230dd2SCorentin Chary      *  - password               (clear text, weak auth)
32043e230dd2SCorentin Chary      *  - sasl                   (encrypt, good auth *IF* using Kerberos via GSSAPI)
32053e230dd2SCorentin Chary      *  - tls                    (encrypt, weak anonymous creds, no auth)
32063e230dd2SCorentin Chary      *  - tls + password         (encrypt, weak anonymous creds, weak auth)
32073e230dd2SCorentin Chary      *  - tls + sasl             (encrypt, weak anonymous creds, good auth)
32083e230dd2SCorentin Chary      *  - tls + x509             (encrypt, good x509 creds, no auth)
32093e230dd2SCorentin Chary      *  - tls + x509 + password  (encrypt, good x509 creds, weak auth)
32103e230dd2SCorentin Chary      *  - tls + x509 + sasl      (encrypt, good x509 creds, good auth)
32113e230dd2SCorentin Chary      *
32123e230dd2SCorentin Chary      * NB1. TLS is a stackable auth scheme.
32133e230dd2SCorentin Chary      * NB2. the x509 schemes have option to validate a client cert dname
32143e230dd2SCorentin Chary      */
32153e230dd2SCorentin Chary     if (password) {
32163e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS
32173e230dd2SCorentin Chary         if (tls) {
32183e230dd2SCorentin Chary             vs->auth = VNC_AUTH_VENCRYPT;
32193e230dd2SCorentin Chary             if (x509) {
32203e230dd2SCorentin Chary                 VNC_DEBUG("Initializing VNC server with x509 password auth\n");
32213e230dd2SCorentin Chary                 vs->subauth = VNC_AUTH_VENCRYPT_X509VNC;
32223e230dd2SCorentin Chary             } else {
32233e230dd2SCorentin Chary                 VNC_DEBUG("Initializing VNC server with TLS password auth\n");
32243e230dd2SCorentin Chary                 vs->subauth = VNC_AUTH_VENCRYPT_TLSVNC;
32253e230dd2SCorentin Chary             }
32263e230dd2SCorentin Chary         } else {
32273e230dd2SCorentin Chary #endif /* CONFIG_VNC_TLS */
32283e230dd2SCorentin Chary             VNC_DEBUG("Initializing VNC server with password auth\n");
32293e230dd2SCorentin Chary             vs->auth = VNC_AUTH_VNC;
32303e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS
32313e230dd2SCorentin Chary             vs->subauth = VNC_AUTH_INVALID;
32323e230dd2SCorentin Chary         }
32333e230dd2SCorentin Chary #endif /* CONFIG_VNC_TLS */
32343e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL
32353e230dd2SCorentin Chary     } else if (sasl) {
32363e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS
32373e230dd2SCorentin Chary         if (tls) {
32383e230dd2SCorentin Chary             vs->auth = VNC_AUTH_VENCRYPT;
32393e230dd2SCorentin Chary             if (x509) {
32403e230dd2SCorentin Chary                 VNC_DEBUG("Initializing VNC server with x509 SASL auth\n");
32413e230dd2SCorentin Chary                 vs->subauth = VNC_AUTH_VENCRYPT_X509SASL;
32423e230dd2SCorentin Chary             } else {
32433e230dd2SCorentin Chary                 VNC_DEBUG("Initializing VNC server with TLS SASL auth\n");
32443e230dd2SCorentin Chary                 vs->subauth = VNC_AUTH_VENCRYPT_TLSSASL;
32453e230dd2SCorentin Chary             }
32463e230dd2SCorentin Chary         } else {
32473e230dd2SCorentin Chary #endif /* CONFIG_VNC_TLS */
32483e230dd2SCorentin Chary             VNC_DEBUG("Initializing VNC server with SASL auth\n");
32493e230dd2SCorentin Chary             vs->auth = VNC_AUTH_SASL;
32503e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS
32513e230dd2SCorentin Chary             vs->subauth = VNC_AUTH_INVALID;
32523e230dd2SCorentin Chary         }
32533e230dd2SCorentin Chary #endif /* CONFIG_VNC_TLS */
32543e230dd2SCorentin Chary #endif /* CONFIG_VNC_SASL */
32553e230dd2SCorentin Chary     } else {
32563e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS
32573e230dd2SCorentin Chary         if (tls) {
32583e230dd2SCorentin Chary             vs->auth = VNC_AUTH_VENCRYPT;
32593e230dd2SCorentin Chary             if (x509) {
32603e230dd2SCorentin Chary                 VNC_DEBUG("Initializing VNC server with x509 no auth\n");
32613e230dd2SCorentin Chary                 vs->subauth = VNC_AUTH_VENCRYPT_X509NONE;
32623e230dd2SCorentin Chary             } else {
32633e230dd2SCorentin Chary                 VNC_DEBUG("Initializing VNC server with TLS no auth\n");
32643e230dd2SCorentin Chary                 vs->subauth = VNC_AUTH_VENCRYPT_TLSNONE;
32653e230dd2SCorentin Chary             }
32663e230dd2SCorentin Chary         } else {
32673e230dd2SCorentin Chary #endif
32683e230dd2SCorentin Chary             VNC_DEBUG("Initializing VNC server with no auth\n");
32693e230dd2SCorentin Chary             vs->auth = VNC_AUTH_NONE;
32703e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS
32713e230dd2SCorentin Chary             vs->subauth = VNC_AUTH_INVALID;
32723e230dd2SCorentin Chary         }
32733e230dd2SCorentin Chary #endif
32743e230dd2SCorentin Chary     }
32753e230dd2SCorentin Chary 
32763e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL
32773e230dd2SCorentin Chary     if ((saslErr = sasl_server_init(NULL, "qemu")) != SASL_OK) {
32782d55f0e8SPaolo Bonzini         error_setg(errp, "Failed to initialize SASL auth: %s",
32793e230dd2SCorentin Chary                    sasl_errstring(saslErr, NULL, NULL));
32801ce52c78SPaolo Bonzini         goto fail;
32813e230dd2SCorentin Chary     }
32823e230dd2SCorentin Chary #endif
32833e230dd2SCorentin Chary     vs->lock_key_sync = lock_key_sync;
32843e230dd2SCorentin Chary 
32853e230dd2SCorentin Chary     if (reverse) {
32863e230dd2SCorentin Chary         /* connect to viewer */
3287007fcd3eSPaolo Bonzini         int csock;
32883e230dd2SCorentin Chary         vs->lsock = -1;
32897536ee4bSTim Hardeck #ifdef CONFIG_VNC_WS
32907536ee4bSTim Hardeck         vs->lwebsock = -1;
32917536ee4bSTim Hardeck #endif
3292007fcd3eSPaolo Bonzini         if (strncmp(display, "unix:", 5) == 0) {
32932d55f0e8SPaolo Bonzini             csock = unix_connect(display+5, errp);
3294007fcd3eSPaolo Bonzini         } else {
32952d55f0e8SPaolo Bonzini             csock = inet_connect(display, errp);
32963e230dd2SCorentin Chary         }
3297007fcd3eSPaolo Bonzini         if (csock < 0) {
3298007fcd3eSPaolo Bonzini             goto fail;
3299007fcd3eSPaolo Bonzini         }
33002c8cf549SMichael Tokarev         vnc_connect(vs, csock, false, false);
33013e230dd2SCorentin Chary     } else {
33023e230dd2SCorentin Chary         /* listen for connects */
33033e230dd2SCorentin Chary         char *dpy;
33047267c094SAnthony Liguori         dpy = g_malloc(256);
33053e230dd2SCorentin Chary         if (strncmp(display, "unix:", 5) == 0) {
33063e230dd2SCorentin Chary             pstrcpy(dpy, 256, "unix:");
33072d55f0e8SPaolo Bonzini             vs->lsock = unix_listen(display+5, dpy+5, 256-5, errp);
33083e230dd2SCorentin Chary         } else {
3309029409e5SAmos Kong             vs->lsock = inet_listen(display, dpy, 256,
33102d55f0e8SPaolo Bonzini                                     SOCK_STREAM, 5900, errp);
3311c1c1619cSPaolo Bonzini             if (vs->lsock < 0) {
3312ae878b17SStefan Weil                 g_free(dpy);
33131ce52c78SPaolo Bonzini                 goto fail;
33141ce52c78SPaolo Bonzini             }
33157536ee4bSTim Hardeck #ifdef CONFIG_VNC_WS
33167536ee4bSTim Hardeck             if (vs->websocket) {
33177536ee4bSTim Hardeck                 if (vs->ws_display) {
33187536ee4bSTim Hardeck                     vs->lwebsock = inet_listen(vs->ws_display, NULL, 256,
33197536ee4bSTim Hardeck                         SOCK_STREAM, 0, errp);
33207536ee4bSTim Hardeck                 } else {
33217536ee4bSTim Hardeck                     vs->lwebsock = inet_listen(vs->display, NULL, 256,
33227536ee4bSTim Hardeck                         SOCK_STREAM, 5700, errp);
33237536ee4bSTim Hardeck                 }
33247536ee4bSTim Hardeck 
33257536ee4bSTim Hardeck                 if (vs->lwebsock < 0) {
33267536ee4bSTim Hardeck                     if (vs->lsock) {
33277536ee4bSTim Hardeck                         close(vs->lsock);
33287536ee4bSTim Hardeck                         vs->lsock = -1;
33297536ee4bSTim Hardeck                     }
33307536ee4bSTim Hardeck                     g_free(dpy);
33317536ee4bSTim Hardeck                     goto fail;
33327536ee4bSTim Hardeck                 }
33337536ee4bSTim Hardeck             }
33347536ee4bSTim Hardeck #endif /* CONFIG_VNC_WS */
33357536ee4bSTim Hardeck         }
3336ae878b17SStefan Weil         g_free(vs->display);
33373e230dd2SCorentin Chary         vs->display = dpy;
33387536ee4bSTim Hardeck         qemu_set_fd_handler2(vs->lsock, NULL,
33397536ee4bSTim Hardeck                 vnc_listen_regular_read, NULL, vs);
33407536ee4bSTim Hardeck #ifdef CONFIG_VNC_WS
33417536ee4bSTim Hardeck         if (vs->websocket) {
33427536ee4bSTim Hardeck             qemu_set_fd_handler2(vs->lwebsock, NULL,
33437536ee4bSTim Hardeck                     vnc_listen_websocket_read, NULL, vs);
33447536ee4bSTim Hardeck         }
33457536ee4bSTim Hardeck #endif /* CONFIG_VNC_WS */
33463e230dd2SCorentin Chary     }
33472d55f0e8SPaolo Bonzini     return;
33481ce52c78SPaolo Bonzini 
33491ce52c78SPaolo Bonzini fail:
33501ce52c78SPaolo Bonzini     g_free(vs->display);
33511ce52c78SPaolo Bonzini     vs->display = NULL;
33527536ee4bSTim Hardeck #ifdef CONFIG_VNC_WS
33537536ee4bSTim Hardeck     g_free(vs->ws_display);
33547536ee4bSTim Hardeck     vs->ws_display = NULL;
33557536ee4bSTim Hardeck #endif /* CONFIG_VNC_WS */
33563e230dd2SCorentin Chary }
335713661089SDaniel P. Berrange 
33582c8cf549SMichael Tokarev void vnc_display_add_client(DisplayState *ds, int csock, bool skipauth)
335913661089SDaniel P. Berrange {
336021ef45d7SGerd Hoffmann     VncDisplay *vs = vnc_display;
336113661089SDaniel P. Berrange 
33622c8cf549SMichael Tokarev     vnc_connect(vs, csock, skipauth, false);
336313661089SDaniel P. Berrange }
3364