xref: /openbmc/qemu/ui/vnc.c (revision 3ab72385b21d8d66df3f5fea42097ce264dc9d6b)
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 
27e16f4c87SPeter Maydell #include "qemu/osdep.h"
283e230dd2SCorentin Chary #include "vnc.h"
29bd023f95SCorentin Chary #include "vnc-jobs.h"
3040066175SGerd Hoffmann #include "trace.h"
319c17d615SPaolo Bonzini #include "sysemu/sysemu.h"
32d49b6836SMarkus Armbruster #include "qemu/error-report.h"
33922a01a0SMarkus Armbruster #include "qemu/option.h"
341de7afc9SPaolo Bonzini #include "qemu/sockets.h"
351de7afc9SPaolo Bonzini #include "qemu/timer.h"
361de7afc9SPaolo Bonzini #include "qemu/acl.h"
374db14629SGerd Hoffmann #include "qemu/config-file.h"
38eb815e24SMarkus Armbruster #include "qapi/qapi-events.h"
39e688df6bSMarkus Armbruster #include "qapi/error.h"
409af23989SMarkus Armbruster #include "qapi/qapi-commands-ui.h"
418d447d10SGerd Hoffmann #include "ui/input.h"
428e9b0d24SDaniel P. Berrange #include "crypto/hash.h"
433e305e4aSDaniel P. Berrange #include "crypto/tlscredsanon.h"
443e305e4aSDaniel P. Berrange #include "crypto/tlscredsx509.h"
453e305e4aSDaniel P. Berrange #include "qom/object_interfaces.h"
46f348b6d1SVeronia Bahaa #include "qemu/cutils.h"
4757a6d6d5SDaniel P. Berrange #include "io/dns-resolver.h"
483e230dd2SCorentin Chary 
490f7b2864SGerd Hoffmann #define VNC_REFRESH_INTERVAL_BASE GUI_REFRESH_INTERVAL_DEFAULT
503e230dd2SCorentin Chary #define VNC_REFRESH_INTERVAL_INC  50
510f7b2864SGerd Hoffmann #define VNC_REFRESH_INTERVAL_MAX  GUI_REFRESH_INTERVAL_IDLE
52999342a0SCorentin Chary static const struct timeval VNC_REFRESH_STATS = { 0, 500000 };
53999342a0SCorentin Chary static const struct timeval VNC_REFRESH_LOSSY = { 2, 0 };
543e230dd2SCorentin Chary 
553e230dd2SCorentin Chary #include "vnc_keysym.h"
56800567a6SDaniel P. Berrange #include "crypto/cipher.h"
573e230dd2SCorentin Chary 
58d616ccc5SGerd Hoffmann static QTAILQ_HEAD(, VncDisplay) vnc_displays =
59d616ccc5SGerd Hoffmann     QTAILQ_HEAD_INITIALIZER(vnc_displays);
603e230dd2SCorentin Chary 
613e230dd2SCorentin Chary static int vnc_cursor_define(VncState *vs);
627bc9318bSGerd Hoffmann static void vnc_release_modifiers(VncState *vs);
63e2b72cb6SDaniel P. Berrange static void vnc_update_throttle_offset(VncState *vs);
643e230dd2SCorentin Chary 
658cf36489SGerd Hoffmann static void vnc_set_share_mode(VncState *vs, VncShareMode mode)
668cf36489SGerd Hoffmann {
678cf36489SGerd Hoffmann #ifdef _VNC_DEBUG
688cf36489SGerd Hoffmann     static const char *mn[] = {
698cf36489SGerd Hoffmann         [0]                           = "undefined",
708cf36489SGerd Hoffmann         [VNC_SHARE_MODE_CONNECTING]   = "connecting",
718cf36489SGerd Hoffmann         [VNC_SHARE_MODE_SHARED]       = "shared",
728cf36489SGerd Hoffmann         [VNC_SHARE_MODE_EXCLUSIVE]    = "exclusive",
738cf36489SGerd Hoffmann         [VNC_SHARE_MODE_DISCONNECTED] = "disconnected",
748cf36489SGerd Hoffmann     };
7504d2529dSDaniel P. Berrange     fprintf(stderr, "%s/%p: %s -> %s\n", __func__,
7604d2529dSDaniel P. Berrange             vs->ioc, mn[vs->share_mode], mn[mode]);
778cf36489SGerd Hoffmann #endif
788cf36489SGerd Hoffmann 
79e5f34cddSGerd Hoffmann     switch (vs->share_mode) {
80e5f34cddSGerd Hoffmann     case VNC_SHARE_MODE_CONNECTING:
81e5f34cddSGerd Hoffmann         vs->vd->num_connecting--;
82e5f34cddSGerd Hoffmann         break;
83e5f34cddSGerd Hoffmann     case VNC_SHARE_MODE_SHARED:
84e5f34cddSGerd Hoffmann         vs->vd->num_shared--;
85e5f34cddSGerd Hoffmann         break;
86e5f34cddSGerd Hoffmann     case VNC_SHARE_MODE_EXCLUSIVE:
878cf36489SGerd Hoffmann         vs->vd->num_exclusive--;
88e5f34cddSGerd Hoffmann         break;
89e5f34cddSGerd Hoffmann     default:
90e5f34cddSGerd Hoffmann         break;
918cf36489SGerd Hoffmann     }
92e5f34cddSGerd Hoffmann 
938cf36489SGerd Hoffmann     vs->share_mode = mode;
94e5f34cddSGerd Hoffmann 
95e5f34cddSGerd Hoffmann     switch (vs->share_mode) {
96e5f34cddSGerd Hoffmann     case VNC_SHARE_MODE_CONNECTING:
97e5f34cddSGerd Hoffmann         vs->vd->num_connecting++;
98e5f34cddSGerd Hoffmann         break;
99e5f34cddSGerd Hoffmann     case VNC_SHARE_MODE_SHARED:
100e5f34cddSGerd Hoffmann         vs->vd->num_shared++;
101e5f34cddSGerd Hoffmann         break;
102e5f34cddSGerd Hoffmann     case VNC_SHARE_MODE_EXCLUSIVE:
1038cf36489SGerd Hoffmann         vs->vd->num_exclusive++;
104e5f34cddSGerd Hoffmann         break;
105e5f34cddSGerd Hoffmann     default:
106e5f34cddSGerd Hoffmann         break;
1078cf36489SGerd Hoffmann     }
1088cf36489SGerd Hoffmann }
1098cf36489SGerd Hoffmann 
1103e230dd2SCorentin Chary 
111bd269ebcSMarkus Armbruster static void vnc_init_basic_info(SocketAddress *addr,
11298481bfcSEric Blake                                 VncBasicInfo *info,
11398481bfcSEric Blake                                 Error **errp)
1143e230dd2SCorentin Chary {
11504d2529dSDaniel P. Berrange     switch (addr->type) {
116bd269ebcSMarkus Armbruster     case SOCKET_ADDRESS_TYPE_INET:
117bd269ebcSMarkus Armbruster         info->host = g_strdup(addr->u.inet.host);
118bd269ebcSMarkus Armbruster         info->service = g_strdup(addr->u.inet.port);
119bd269ebcSMarkus Armbruster         if (addr->u.inet.ipv6) {
12004d2529dSDaniel P. Berrange             info->family = NETWORK_ADDRESS_FAMILY_IPV6;
12104d2529dSDaniel P. Berrange         } else {
12204d2529dSDaniel P. Berrange             info->family = NETWORK_ADDRESS_FAMILY_IPV4;
12304d2529dSDaniel P. Berrange         }
12404d2529dSDaniel P. Berrange         break;
1253e230dd2SCorentin Chary 
126bd269ebcSMarkus Armbruster     case SOCKET_ADDRESS_TYPE_UNIX:
12704d2529dSDaniel P. Berrange         info->host = g_strdup("");
128bd269ebcSMarkus Armbruster         info->service = g_strdup(addr->u.q_unix.path);
12904d2529dSDaniel P. Berrange         info->family = NETWORK_ADDRESS_FAMILY_UNIX;
13004d2529dSDaniel P. Berrange         break;
13104d2529dSDaniel P. Berrange 
132bd269ebcSMarkus Armbruster     case SOCKET_ADDRESS_TYPE_VSOCK:
133bd269ebcSMarkus Armbruster     case SOCKET_ADDRESS_TYPE_FD:
134a6c76285SMarkus Armbruster         error_setg(errp, "Unsupported socket address type %s",
135977c736fSMarkus Armbruster                    SocketAddressType_str(addr->type));
13604d2529dSDaniel P. Berrange         break;
137a6c76285SMarkus Armbruster     default:
138a6c76285SMarkus Armbruster         abort();
13904d2529dSDaniel P. Berrange     }
14004d2529dSDaniel P. Berrange 
14198481bfcSEric Blake     return;
1423e230dd2SCorentin Chary }
1433e230dd2SCorentin Chary 
14404d2529dSDaniel P. Berrange static void vnc_init_basic_info_from_server_addr(QIOChannelSocket *ioc,
14504d2529dSDaniel P. Berrange                                                  VncBasicInfo *info,
14698481bfcSEric Blake                                                  Error **errp)
1473e230dd2SCorentin Chary {
148bd269ebcSMarkus Armbruster     SocketAddress *addr = NULL;
1493e230dd2SCorentin Chary 
150624cdd46SDaniel P. Berrange     if (!ioc) {
151624cdd46SDaniel P. Berrange         error_setg(errp, "No listener socket available");
152624cdd46SDaniel P. Berrange         return;
153624cdd46SDaniel P. Berrange     }
154624cdd46SDaniel P. Berrange 
15504d2529dSDaniel P. Berrange     addr = qio_channel_socket_get_local_address(ioc, errp);
15604d2529dSDaniel P. Berrange     if (!addr) {
15798481bfcSEric Blake         return;
1583e230dd2SCorentin Chary     }
1593e230dd2SCorentin Chary 
16004d2529dSDaniel P. Berrange     vnc_init_basic_info(addr, info, errp);
161bd269ebcSMarkus Armbruster     qapi_free_SocketAddress(addr);
1623e230dd2SCorentin Chary }
1633e230dd2SCorentin Chary 
16404d2529dSDaniel P. Berrange static void vnc_init_basic_info_from_remote_addr(QIOChannelSocket *ioc,
16504d2529dSDaniel P. Berrange                                                  VncBasicInfo *info,
16698481bfcSEric Blake                                                  Error **errp)
1673e230dd2SCorentin Chary {
168bd269ebcSMarkus Armbruster     SocketAddress *addr = NULL;
1693e230dd2SCorentin Chary 
17004d2529dSDaniel P. Berrange     addr = qio_channel_socket_get_remote_address(ioc, errp);
17104d2529dSDaniel P. Berrange     if (!addr) {
17298481bfcSEric Blake         return;
1733e230dd2SCorentin Chary     }
1743e230dd2SCorentin Chary 
17504d2529dSDaniel P. Berrange     vnc_init_basic_info(addr, info, errp);
176bd269ebcSMarkus Armbruster     qapi_free_SocketAddress(addr);
1773e230dd2SCorentin Chary }
1783e230dd2SCorentin Chary 
1793e230dd2SCorentin Chary static const char *vnc_auth_name(VncDisplay *vd) {
1803e230dd2SCorentin Chary     switch (vd->auth) {
1813e230dd2SCorentin Chary     case VNC_AUTH_INVALID:
1823e230dd2SCorentin Chary         return "invalid";
1833e230dd2SCorentin Chary     case VNC_AUTH_NONE:
1843e230dd2SCorentin Chary         return "none";
1853e230dd2SCorentin Chary     case VNC_AUTH_VNC:
1863e230dd2SCorentin Chary         return "vnc";
1873e230dd2SCorentin Chary     case VNC_AUTH_RA2:
1883e230dd2SCorentin Chary         return "ra2";
1893e230dd2SCorentin Chary     case VNC_AUTH_RA2NE:
1903e230dd2SCorentin Chary         return "ra2ne";
1913e230dd2SCorentin Chary     case VNC_AUTH_TIGHT:
1923e230dd2SCorentin Chary         return "tight";
1933e230dd2SCorentin Chary     case VNC_AUTH_ULTRA:
1943e230dd2SCorentin Chary         return "ultra";
1953e230dd2SCorentin Chary     case VNC_AUTH_TLS:
1963e230dd2SCorentin Chary         return "tls";
1973e230dd2SCorentin Chary     case VNC_AUTH_VENCRYPT:
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     case VNC_AUTH_SASL:
2213e230dd2SCorentin Chary         return "sasl";
2223e230dd2SCorentin Chary     }
2233e230dd2SCorentin Chary     return "unknown";
2243e230dd2SCorentin Chary }
2253e230dd2SCorentin Chary 
226d616ccc5SGerd Hoffmann static VncServerInfo *vnc_server_info_get(VncDisplay *vd)
2273e230dd2SCorentin Chary {
228fb6ba0d5SWenchao Xia     VncServerInfo *info;
22998481bfcSEric Blake     Error *err = NULL;
2303e230dd2SCorentin Chary 
23113e1d0e7SDaniel P. Berrange     if (!vd->listener || !vd->listener->nsioc) {
2324ee74fa7SDaniel P. Berrange         return NULL;
2334ee74fa7SDaniel P. Berrange     }
2344ee74fa7SDaniel P. Berrange 
2353e7f136dSDaniel P. Berrange     info = g_malloc0(sizeof(*info));
23613e1d0e7SDaniel P. Berrange     vnc_init_basic_info_from_server_addr(vd->listener->sioc[0],
237ddf21908SEric Blake                                          qapi_VncServerInfo_base(info), &err);
238fb6ba0d5SWenchao Xia     info->has_auth = true;
239d616ccc5SGerd Hoffmann     info->auth = g_strdup(vnc_auth_name(vd));
24098481bfcSEric Blake     if (err) {
24198481bfcSEric Blake         qapi_free_VncServerInfo(info);
24298481bfcSEric Blake         info = NULL;
24398481bfcSEric Blake         error_free(err);
24498481bfcSEric Blake     }
245fb6ba0d5SWenchao Xia     return info;
2463e230dd2SCorentin Chary }
2473e230dd2SCorentin Chary 
2483e230dd2SCorentin Chary static void vnc_client_cache_auth(VncState *client)
2493e230dd2SCorentin Chary {
2503e230dd2SCorentin Chary     if (!client->info) {
2513e230dd2SCorentin Chary         return;
2523e230dd2SCorentin Chary     }
2533e230dd2SCorentin Chary 
2543e305e4aSDaniel P. Berrange     if (client->tls) {
2553e305e4aSDaniel P. Berrange         client->info->x509_dname =
2563e305e4aSDaniel P. Berrange             qcrypto_tls_session_get_peer_name(client->tls);
2573e305e4aSDaniel P. Berrange         client->info->has_x509_dname =
2583e305e4aSDaniel P. Berrange             client->info->x509_dname != NULL;
2593e230dd2SCorentin Chary     }
2603e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL
2613e230dd2SCorentin Chary     if (client->sasl.conn &&
2623e230dd2SCorentin Chary         client->sasl.username) {
263fb6ba0d5SWenchao Xia         client->info->has_sasl_username = true;
264fb6ba0d5SWenchao Xia         client->info->sasl_username = g_strdup(client->sasl.username);
2653e230dd2SCorentin Chary     }
2663e230dd2SCorentin Chary #endif
2673e230dd2SCorentin Chary }
2683e230dd2SCorentin Chary 
2693e230dd2SCorentin Chary static void vnc_client_cache_addr(VncState *client)
2703e230dd2SCorentin Chary {
27198481bfcSEric Blake     Error *err = NULL;
2723e230dd2SCorentin Chary 
273fb6ba0d5SWenchao Xia     client->info = g_malloc0(sizeof(*client->info));
27404d2529dSDaniel P. Berrange     vnc_init_basic_info_from_remote_addr(client->sioc,
275ddf21908SEric Blake                                          qapi_VncClientInfo_base(client->info),
27698481bfcSEric Blake                                          &err);
27798481bfcSEric Blake     if (err) {
27898481bfcSEric Blake         qapi_free_VncClientInfo(client->info);
27998481bfcSEric Blake         client->info = NULL;
28098481bfcSEric Blake         error_free(err);
281fb6ba0d5SWenchao Xia     }
2823e230dd2SCorentin Chary }
2833e230dd2SCorentin Chary 
284fb6ba0d5SWenchao Xia static void vnc_qmp_event(VncState *vs, QAPIEvent event)
2853e230dd2SCorentin Chary {
286fb6ba0d5SWenchao Xia     VncServerInfo *si;
2873e230dd2SCorentin Chary 
2883e230dd2SCorentin Chary     if (!vs->info) {
2893e230dd2SCorentin Chary         return;
2903e230dd2SCorentin Chary     }
2913e230dd2SCorentin Chary 
292d616ccc5SGerd Hoffmann     si = vnc_server_info_get(vs->vd);
293fb6ba0d5SWenchao Xia     if (!si) {
2943e230dd2SCorentin Chary         return;
2953e230dd2SCorentin Chary     }
2963e230dd2SCorentin Chary 
297fb6ba0d5SWenchao Xia     switch (event) {
298fb6ba0d5SWenchao Xia     case QAPI_EVENT_VNC_CONNECTED:
299*3ab72385SPeter Xu         qapi_event_send_vnc_connected(si, qapi_VncClientInfo_base(vs->info));
300fb6ba0d5SWenchao Xia         break;
301fb6ba0d5SWenchao Xia     case QAPI_EVENT_VNC_INITIALIZED:
302*3ab72385SPeter Xu         qapi_event_send_vnc_initialized(si, vs->info);
303fb6ba0d5SWenchao Xia         break;
304fb6ba0d5SWenchao Xia     case QAPI_EVENT_VNC_DISCONNECTED:
305*3ab72385SPeter Xu         qapi_event_send_vnc_disconnected(si, vs->info);
306fb6ba0d5SWenchao Xia         break;
307fb6ba0d5SWenchao Xia     default:
308fb6ba0d5SWenchao Xia         break;
309fb6ba0d5SWenchao Xia     }
3103e230dd2SCorentin Chary 
311fb6ba0d5SWenchao Xia     qapi_free_VncServerInfo(si);
3123e230dd2SCorentin Chary }
3133e230dd2SCorentin Chary 
3142b54aa87SLuiz Capitulino static VncClientInfo *qmp_query_vnc_client(const VncState *client)
3153e230dd2SCorentin Chary {
3162b54aa87SLuiz Capitulino     VncClientInfo *info;
31704d2529dSDaniel P. Berrange     Error *err = NULL;
3182b54aa87SLuiz Capitulino 
3192b54aa87SLuiz Capitulino     info = g_malloc0(sizeof(*info));
32004d2529dSDaniel P. Berrange 
32104d2529dSDaniel P. Berrange     vnc_init_basic_info_from_remote_addr(client->sioc,
32204d2529dSDaniel P. Berrange                                          qapi_VncClientInfo_base(info),
32304d2529dSDaniel P. Berrange                                          &err);
32404d2529dSDaniel P. Berrange     if (err) {
32504d2529dSDaniel P. Berrange         error_free(err);
32604d2529dSDaniel P. Berrange         qapi_free_VncClientInfo(info);
32704d2529dSDaniel P. Berrange         return NULL;
32804d2529dSDaniel P. Berrange     }
32904d2529dSDaniel P. Berrange 
330ddf21908SEric Blake     info->websocket = client->websocket;
3313e230dd2SCorentin Chary 
3323e305e4aSDaniel P. Berrange     if (client->tls) {
3333e305e4aSDaniel P. Berrange         info->x509_dname = qcrypto_tls_session_get_peer_name(client->tls);
3343e305e4aSDaniel P. Berrange         info->has_x509_dname = info->x509_dname != NULL;
3352b54aa87SLuiz Capitulino     }
3363e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL
3372b54aa87SLuiz Capitulino     if (client->sasl.conn && client->sasl.username) {
3382b54aa87SLuiz Capitulino         info->has_sasl_username = true;
3392b54aa87SLuiz Capitulino         info->sasl_username = g_strdup(client->sasl.username);
3402b54aa87SLuiz Capitulino     }
3413e230dd2SCorentin Chary #endif
3422b54aa87SLuiz Capitulino 
3432b54aa87SLuiz Capitulino     return info;
3443e230dd2SCorentin Chary }
3453e230dd2SCorentin Chary 
346d616ccc5SGerd Hoffmann static VncDisplay *vnc_display_find(const char *id)
347d616ccc5SGerd Hoffmann {
348d616ccc5SGerd Hoffmann     VncDisplay *vd;
349d616ccc5SGerd Hoffmann 
350d616ccc5SGerd Hoffmann     if (id == NULL) {
351d616ccc5SGerd Hoffmann         return QTAILQ_FIRST(&vnc_displays);
352d616ccc5SGerd Hoffmann     }
353d616ccc5SGerd Hoffmann     QTAILQ_FOREACH(vd, &vnc_displays, next) {
354d616ccc5SGerd Hoffmann         if (strcmp(id, vd->id) == 0) {
355d616ccc5SGerd Hoffmann             return vd;
356d616ccc5SGerd Hoffmann         }
357d616ccc5SGerd Hoffmann     }
358d616ccc5SGerd Hoffmann     return NULL;
359d616ccc5SGerd Hoffmann }
360d616ccc5SGerd Hoffmann 
3612d29a436SGerd Hoffmann static VncClientInfoList *qmp_query_client_list(VncDisplay *vd)
3622d29a436SGerd Hoffmann {
3632d29a436SGerd Hoffmann     VncClientInfoList *cinfo, *prev = NULL;
3642d29a436SGerd Hoffmann     VncState *client;
3652d29a436SGerd Hoffmann 
3662d29a436SGerd Hoffmann     QTAILQ_FOREACH(client, &vd->clients, next) {
3672d29a436SGerd Hoffmann         cinfo = g_new0(VncClientInfoList, 1);
3682d29a436SGerd Hoffmann         cinfo->value = qmp_query_vnc_client(client);
3692d29a436SGerd Hoffmann         cinfo->next = prev;
3702d29a436SGerd Hoffmann         prev = cinfo;
3712d29a436SGerd Hoffmann     }
3722d29a436SGerd Hoffmann     return prev;
3732d29a436SGerd Hoffmann }
3742d29a436SGerd Hoffmann 
3752b54aa87SLuiz Capitulino VncInfo *qmp_query_vnc(Error **errp)
3763e230dd2SCorentin Chary {
3772b54aa87SLuiz Capitulino     VncInfo *info = g_malloc0(sizeof(*info));
378d616ccc5SGerd Hoffmann     VncDisplay *vd = vnc_display_find(NULL);
379bd269ebcSMarkus Armbruster     SocketAddress *addr = NULL;
3803e230dd2SCorentin Chary 
38113e1d0e7SDaniel P. Berrange     if (vd == NULL || !vd->listener || !vd->listener->nsioc) {
3822b54aa87SLuiz Capitulino         info->enabled = false;
3833e230dd2SCorentin Chary     } else {
3842b54aa87SLuiz Capitulino         info->enabled = true;
3852b54aa87SLuiz Capitulino 
3862b54aa87SLuiz Capitulino         /* for compatibility with the original command */
3872b54aa87SLuiz Capitulino         info->has_clients = true;
3882d29a436SGerd Hoffmann         info->clients = qmp_query_client_list(vd);
3893e230dd2SCorentin Chary 
39013e1d0e7SDaniel P. Berrange         addr = qio_channel_socket_get_local_address(vd->listener->sioc[0],
39113e1d0e7SDaniel P. Berrange                                                     errp);
39204d2529dSDaniel P. Berrange         if (!addr) {
3932b54aa87SLuiz Capitulino             goto out_error;
3942b54aa87SLuiz Capitulino         }
3953e230dd2SCorentin Chary 
39604d2529dSDaniel P. Berrange         switch (addr->type) {
397bd269ebcSMarkus Armbruster         case SOCKET_ADDRESS_TYPE_INET:
398bd269ebcSMarkus Armbruster             info->host = g_strdup(addr->u.inet.host);
399bd269ebcSMarkus Armbruster             info->service = g_strdup(addr->u.inet.port);
400bd269ebcSMarkus Armbruster             if (addr->u.inet.ipv6) {
40104d2529dSDaniel P. Berrange                 info->family = NETWORK_ADDRESS_FAMILY_IPV6;
40204d2529dSDaniel P. Berrange             } else {
40304d2529dSDaniel P. Berrange                 info->family = NETWORK_ADDRESS_FAMILY_IPV4;
40404d2529dSDaniel P. Berrange             }
40504d2529dSDaniel P. Berrange             break;
40604d2529dSDaniel P. Berrange 
407bd269ebcSMarkus Armbruster         case SOCKET_ADDRESS_TYPE_UNIX:
40804d2529dSDaniel P. Berrange             info->host = g_strdup("");
409bd269ebcSMarkus Armbruster             info->service = g_strdup(addr->u.q_unix.path);
41004d2529dSDaniel P. Berrange             info->family = NETWORK_ADDRESS_FAMILY_UNIX;
41104d2529dSDaniel P. Berrange             break;
41204d2529dSDaniel P. Berrange 
413bd269ebcSMarkus Armbruster         case SOCKET_ADDRESS_TYPE_VSOCK:
414bd269ebcSMarkus Armbruster         case SOCKET_ADDRESS_TYPE_FD:
415a6c76285SMarkus Armbruster             error_setg(errp, "Unsupported socket address type %s",
416977c736fSMarkus Armbruster                        SocketAddressType_str(addr->type));
4172b54aa87SLuiz Capitulino             goto out_error;
418a6c76285SMarkus Armbruster         default:
419a6c76285SMarkus Armbruster             abort();
4203e230dd2SCorentin Chary         }
4212b54aa87SLuiz Capitulino 
4222b54aa87SLuiz Capitulino         info->has_host = true;
4232b54aa87SLuiz Capitulino         info->has_service = true;
4242b54aa87SLuiz Capitulino         info->has_family = true;
4252b54aa87SLuiz Capitulino 
4262b54aa87SLuiz Capitulino         info->has_auth = true;
427d616ccc5SGerd Hoffmann         info->auth = g_strdup(vnc_auth_name(vd));
4283e230dd2SCorentin Chary     }
4292b54aa87SLuiz Capitulino 
430bd269ebcSMarkus Armbruster     qapi_free_SocketAddress(addr);
4312b54aa87SLuiz Capitulino     return info;
4322b54aa87SLuiz Capitulino 
4332b54aa87SLuiz Capitulino out_error:
434bd269ebcSMarkus Armbruster     qapi_free_SocketAddress(addr);
4352b54aa87SLuiz Capitulino     qapi_free_VncInfo(info);
4362b54aa87SLuiz Capitulino     return NULL;
4373e230dd2SCorentin Chary }
4383e230dd2SCorentin Chary 
4392a7e6857SDaniel P. Berrange 
4402a7e6857SDaniel P. Berrange static void qmp_query_auth(int auth, int subauth,
4412a7e6857SDaniel P. Berrange                            VncPrimaryAuth *qmp_auth,
4422a7e6857SDaniel P. Berrange                            VncVencryptSubAuth *qmp_vencrypt,
4432a7e6857SDaniel P. Berrange                            bool *qmp_has_vencrypt);
4442a7e6857SDaniel P. Berrange 
4452a7e6857SDaniel P. Berrange static VncServerInfo2List *qmp_query_server_entry(QIOChannelSocket *ioc,
4464478aa76SGerd Hoffmann                                                   bool websocket,
4472a7e6857SDaniel P. Berrange                                                   int auth,
4482a7e6857SDaniel P. Berrange                                                   int subauth,
4492a7e6857SDaniel P. Berrange                                                   VncServerInfo2List *prev)
450df887684SGerd Hoffmann {
4512a7e6857SDaniel P. Berrange     VncServerInfo2List *list;
4522a7e6857SDaniel P. Berrange     VncServerInfo2 *info;
45304d2529dSDaniel P. Berrange     Error *err = NULL;
454bd269ebcSMarkus Armbruster     SocketAddress *addr;
455df887684SGerd Hoffmann 
45604d2529dSDaniel P. Berrange     addr = qio_channel_socket_get_local_address(ioc, &err);
45704d2529dSDaniel P. Berrange     if (!addr) {
45804d2529dSDaniel P. Berrange         error_free(err);
459df887684SGerd Hoffmann         return prev;
460df887684SGerd Hoffmann     }
461df887684SGerd Hoffmann 
4622a7e6857SDaniel P. Berrange     info = g_new0(VncServerInfo2, 1);
4632a7e6857SDaniel P. Berrange     vnc_init_basic_info(addr, qapi_VncServerInfo2_base(info), &err);
464bd269ebcSMarkus Armbruster     qapi_free_SocketAddress(addr);
46504d2529dSDaniel P. Berrange     if (err) {
4662a7e6857SDaniel P. Berrange         qapi_free_VncServerInfo2(info);
46704d2529dSDaniel P. Berrange         error_free(err);
46804d2529dSDaniel P. Berrange         return prev;
46904d2529dSDaniel P. Berrange     }
4704478aa76SGerd Hoffmann     info->websocket = websocket;
471df887684SGerd Hoffmann 
4722a7e6857SDaniel P. Berrange     qmp_query_auth(auth, subauth, &info->auth,
4732a7e6857SDaniel P. Berrange                    &info->vencrypt, &info->has_vencrypt);
4742a7e6857SDaniel P. Berrange 
4752a7e6857SDaniel P. Berrange     list = g_new0(VncServerInfo2List, 1);
476df887684SGerd Hoffmann     list->value = info;
477df887684SGerd Hoffmann     list->next = prev;
478df887684SGerd Hoffmann     return list;
479df887684SGerd Hoffmann }
480df887684SGerd Hoffmann 
4812a7e6857SDaniel P. Berrange static void qmp_query_auth(int auth, int subauth,
4822a7e6857SDaniel P. Berrange                            VncPrimaryAuth *qmp_auth,
4832a7e6857SDaniel P. Berrange                            VncVencryptSubAuth *qmp_vencrypt,
4842a7e6857SDaniel P. Berrange                            bool *qmp_has_vencrypt)
485df887684SGerd Hoffmann {
4862a7e6857SDaniel P. Berrange     switch (auth) {
487df887684SGerd Hoffmann     case VNC_AUTH_VNC:
4882a7e6857SDaniel P. Berrange         *qmp_auth = VNC_PRIMARY_AUTH_VNC;
489df887684SGerd Hoffmann         break;
490df887684SGerd Hoffmann     case VNC_AUTH_RA2:
4912a7e6857SDaniel P. Berrange         *qmp_auth = VNC_PRIMARY_AUTH_RA2;
492df887684SGerd Hoffmann         break;
493df887684SGerd Hoffmann     case VNC_AUTH_RA2NE:
4942a7e6857SDaniel P. Berrange         *qmp_auth = VNC_PRIMARY_AUTH_RA2NE;
495df887684SGerd Hoffmann         break;
496df887684SGerd Hoffmann     case VNC_AUTH_TIGHT:
4972a7e6857SDaniel P. Berrange         *qmp_auth = VNC_PRIMARY_AUTH_TIGHT;
498df887684SGerd Hoffmann         break;
499df887684SGerd Hoffmann     case VNC_AUTH_ULTRA:
5002a7e6857SDaniel P. Berrange         *qmp_auth = VNC_PRIMARY_AUTH_ULTRA;
501df887684SGerd Hoffmann         break;
502df887684SGerd Hoffmann     case VNC_AUTH_TLS:
5032a7e6857SDaniel P. Berrange         *qmp_auth = VNC_PRIMARY_AUTH_TLS;
504df887684SGerd Hoffmann         break;
505df887684SGerd Hoffmann     case VNC_AUTH_VENCRYPT:
5062a7e6857SDaniel P. Berrange         *qmp_auth = VNC_PRIMARY_AUTH_VENCRYPT;
5072a7e6857SDaniel P. Berrange         *qmp_has_vencrypt = true;
5082a7e6857SDaniel P. Berrange         switch (subauth) {
509df887684SGerd Hoffmann         case VNC_AUTH_VENCRYPT_PLAIN:
5102a7e6857SDaniel P. Berrange             *qmp_vencrypt = VNC_VENCRYPT_SUB_AUTH_PLAIN;
511df887684SGerd Hoffmann             break;
512df887684SGerd Hoffmann         case VNC_AUTH_VENCRYPT_TLSNONE:
5132a7e6857SDaniel P. Berrange             *qmp_vencrypt = VNC_VENCRYPT_SUB_AUTH_TLS_NONE;
514df887684SGerd Hoffmann             break;
515df887684SGerd Hoffmann         case VNC_AUTH_VENCRYPT_TLSVNC:
5162a7e6857SDaniel P. Berrange             *qmp_vencrypt = VNC_VENCRYPT_SUB_AUTH_TLS_VNC;
517df887684SGerd Hoffmann             break;
518df887684SGerd Hoffmann         case VNC_AUTH_VENCRYPT_TLSPLAIN:
5192a7e6857SDaniel P. Berrange             *qmp_vencrypt = VNC_VENCRYPT_SUB_AUTH_TLS_PLAIN;
520df887684SGerd Hoffmann             break;
521df887684SGerd Hoffmann         case VNC_AUTH_VENCRYPT_X509NONE:
5222a7e6857SDaniel P. Berrange             *qmp_vencrypt = VNC_VENCRYPT_SUB_AUTH_X509_NONE;
523df887684SGerd Hoffmann             break;
524df887684SGerd Hoffmann         case VNC_AUTH_VENCRYPT_X509VNC:
5252a7e6857SDaniel P. Berrange             *qmp_vencrypt = VNC_VENCRYPT_SUB_AUTH_X509_VNC;
526df887684SGerd Hoffmann             break;
527df887684SGerd Hoffmann         case VNC_AUTH_VENCRYPT_X509PLAIN:
5282a7e6857SDaniel P. Berrange             *qmp_vencrypt = VNC_VENCRYPT_SUB_AUTH_X509_PLAIN;
529df887684SGerd Hoffmann             break;
530df887684SGerd Hoffmann         case VNC_AUTH_VENCRYPT_TLSSASL:
5312a7e6857SDaniel P. Berrange             *qmp_vencrypt = VNC_VENCRYPT_SUB_AUTH_TLS_SASL;
532df887684SGerd Hoffmann             break;
533df887684SGerd Hoffmann         case VNC_AUTH_VENCRYPT_X509SASL:
5342a7e6857SDaniel P. Berrange             *qmp_vencrypt = VNC_VENCRYPT_SUB_AUTH_X509_SASL;
535df887684SGerd Hoffmann             break;
536df887684SGerd Hoffmann         default:
5372a7e6857SDaniel P. Berrange             *qmp_has_vencrypt = false;
538df887684SGerd Hoffmann             break;
539df887684SGerd Hoffmann         }
540df887684SGerd Hoffmann         break;
541df887684SGerd Hoffmann     case VNC_AUTH_SASL:
5422a7e6857SDaniel P. Berrange         *qmp_auth = VNC_PRIMARY_AUTH_SASL;
543df887684SGerd Hoffmann         break;
544df887684SGerd Hoffmann     case VNC_AUTH_NONE:
545df887684SGerd Hoffmann     default:
5462a7e6857SDaniel P. Berrange         *qmp_auth = VNC_PRIMARY_AUTH_NONE;
547df887684SGerd Hoffmann         break;
548df887684SGerd Hoffmann     }
549df887684SGerd Hoffmann }
550df887684SGerd Hoffmann 
551df887684SGerd Hoffmann VncInfo2List *qmp_query_vnc_servers(Error **errp)
552df887684SGerd Hoffmann {
553df887684SGerd Hoffmann     VncInfo2List *item, *prev = NULL;
554df887684SGerd Hoffmann     VncInfo2 *info;
555df887684SGerd Hoffmann     VncDisplay *vd;
556df887684SGerd Hoffmann     DeviceState *dev;
5574ee74fa7SDaniel P. Berrange     size_t i;
558df887684SGerd Hoffmann 
559df887684SGerd Hoffmann     QTAILQ_FOREACH(vd, &vnc_displays, next) {
560df887684SGerd Hoffmann         info = g_new0(VncInfo2, 1);
561df887684SGerd Hoffmann         info->id = g_strdup(vd->id);
562df887684SGerd Hoffmann         info->clients = qmp_query_client_list(vd);
5632a7e6857SDaniel P. Berrange         qmp_query_auth(vd->auth, vd->subauth, &info->auth,
5642a7e6857SDaniel P. Berrange                        &info->vencrypt, &info->has_vencrypt);
565df887684SGerd Hoffmann         if (vd->dcl.con) {
566df887684SGerd Hoffmann             dev = DEVICE(object_property_get_link(OBJECT(vd->dcl.con),
567df887684SGerd Hoffmann                                                   "device", NULL));
568df887684SGerd Hoffmann             info->has_display = true;
569df887684SGerd Hoffmann             info->display = g_strdup(dev->id);
570df887684SGerd Hoffmann         }
57113e1d0e7SDaniel P. Berrange         for (i = 0; vd->listener != NULL && i < vd->listener->nsioc; i++) {
57204d2529dSDaniel P. Berrange             info->server = qmp_query_server_entry(
57313e1d0e7SDaniel P. Berrange                 vd->listener->sioc[i], false, vd->auth, vd->subauth,
57413e1d0e7SDaniel P. Berrange                 info->server);
575df887684SGerd Hoffmann         }
57613e1d0e7SDaniel P. Berrange         for (i = 0; vd->wslistener != NULL && i < vd->wslistener->nsioc; i++) {
57704d2529dSDaniel P. Berrange             info->server = qmp_query_server_entry(
57813e1d0e7SDaniel P. Berrange                 vd->wslistener->sioc[i], true, vd->ws_auth,
5794ee74fa7SDaniel P. Berrange                 vd->ws_subauth, info->server);
580df887684SGerd Hoffmann         }
581df887684SGerd Hoffmann 
582df887684SGerd Hoffmann         item = g_new0(VncInfo2List, 1);
583df887684SGerd Hoffmann         item->value = info;
584df887684SGerd Hoffmann         item->next = prev;
585df887684SGerd Hoffmann         prev = item;
586df887684SGerd Hoffmann     }
587df887684SGerd Hoffmann     return prev;
588df887684SGerd Hoffmann }
589df887684SGerd Hoffmann 
5903e230dd2SCorentin Chary /* TODO
5913e230dd2SCorentin Chary    1) Get the queue working for IO.
5923e230dd2SCorentin Chary    2) there is some weirdness when using the -S option (the screen is grey
5933e230dd2SCorentin Chary       and not totally invalidated
5943e230dd2SCorentin Chary    3) resolutions > 1024
5953e230dd2SCorentin Chary */
5963e230dd2SCorentin Chary 
5976af998dbSDaniel P. Berrange static int vnc_update_client(VncState *vs, int has_dirty);
5983e230dd2SCorentin Chary static void vnc_disconnect_start(VncState *vs);
5993e230dd2SCorentin Chary 
6003e230dd2SCorentin Chary static void vnc_colordepth(VncState *vs);
6013e230dd2SCorentin Chary static void framebuffer_update_request(VncState *vs, int incremental,
6023e230dd2SCorentin Chary                                        int x_position, int y_position,
6033e230dd2SCorentin Chary                                        int w, int h);
6040f7b2864SGerd Hoffmann static void vnc_refresh(DisplayChangeListener *dcl);
6053e230dd2SCorentin Chary static int vnc_refresh_server_surface(VncDisplay *vd);
6063e230dd2SCorentin Chary 
607d05959c2SGerd Hoffmann static int vnc_width(VncDisplay *vd)
608d05959c2SGerd Hoffmann {
609d05959c2SGerd Hoffmann     return MIN(VNC_MAX_WIDTH, ROUND_UP(surface_width(vd->ds),
610d05959c2SGerd Hoffmann                                        VNC_DIRTY_PIXELS_PER_BIT));
611d05959c2SGerd Hoffmann }
612d05959c2SGerd Hoffmann 
613d05959c2SGerd Hoffmann static int vnc_height(VncDisplay *vd)
614d05959c2SGerd Hoffmann {
615d05959c2SGerd Hoffmann     return MIN(VNC_MAX_HEIGHT, surface_height(vd->ds));
616d05959c2SGerd Hoffmann }
617d05959c2SGerd Hoffmann 
618bea60dd7SPeter Lieven static void vnc_set_area_dirty(DECLARE_BITMAP(dirty[VNC_MAX_HEIGHT],
619bea60dd7SPeter Lieven                                VNC_MAX_WIDTH / VNC_DIRTY_PIXELS_PER_BIT),
620f7b3d68cSGerd Hoffmann                                VncDisplay *vd,
621f7b3d68cSGerd Hoffmann                                int x, int y, int w, int h)
622f7b3d68cSGerd Hoffmann {
623f7b3d68cSGerd Hoffmann     int width = vnc_width(vd);
624f7b3d68cSGerd Hoffmann     int height = vnc_height(vd);
625f7b3d68cSGerd Hoffmann 
62691937225SPeter Lieven     /* this is needed this to ensure we updated all affected
62791937225SPeter Lieven      * blocks if x % VNC_DIRTY_PIXELS_PER_BIT != 0 */
628b4c85ddcSPeter Lieven     w += (x % VNC_DIRTY_PIXELS_PER_BIT);
629b4c85ddcSPeter Lieven     x -= (x % VNC_DIRTY_PIXELS_PER_BIT);
6303e230dd2SCorentin Chary 
6319f64916dSGerd Hoffmann     x = MIN(x, width);
6329f64916dSGerd Hoffmann     y = MIN(y, height);
6339f64916dSGerd Hoffmann     w = MIN(x + w, width) - x;
63491937225SPeter Lieven     h = MIN(y + h, height);
6353e230dd2SCorentin Chary 
636b4c85ddcSPeter Lieven     for (; y < h; y++) {
637bea60dd7SPeter Lieven         bitmap_set(dirty[y], x / VNC_DIRTY_PIXELS_PER_BIT,
63891937225SPeter Lieven                    DIV_ROUND_UP(w, VNC_DIRTY_PIXELS_PER_BIT));
639b4c85ddcSPeter Lieven     }
6403e230dd2SCorentin Chary }
6413e230dd2SCorentin Chary 
642bea60dd7SPeter Lieven static void vnc_dpy_update(DisplayChangeListener *dcl,
643bea60dd7SPeter Lieven                            int x, int y, int w, int h)
644bea60dd7SPeter Lieven {
645bea60dd7SPeter Lieven     VncDisplay *vd = container_of(dcl, VncDisplay, dcl);
646bea60dd7SPeter Lieven     struct VncSurface *s = &vd->guest;
647bea60dd7SPeter Lieven 
648f7b3d68cSGerd Hoffmann     vnc_set_area_dirty(s->dirty, vd, x, y, w, h);
649bea60dd7SPeter Lieven }
650bea60dd7SPeter Lieven 
6513e230dd2SCorentin Chary void vnc_framebuffer_update(VncState *vs, int x, int y, int w, int h,
6523e230dd2SCorentin Chary                             int32_t encoding)
6533e230dd2SCorentin Chary {
6543e230dd2SCorentin Chary     vnc_write_u16(vs, x);
6553e230dd2SCorentin Chary     vnc_write_u16(vs, y);
6563e230dd2SCorentin Chary     vnc_write_u16(vs, w);
6573e230dd2SCorentin Chary     vnc_write_u16(vs, h);
6583e230dd2SCorentin Chary 
6593e230dd2SCorentin Chary     vnc_write_s32(vs, encoding);
6603e230dd2SCorentin Chary }
6613e230dd2SCorentin Chary 
66232ed2680STim Hardeck 
6633e230dd2SCorentin Chary static void vnc_desktop_resize(VncState *vs)
6643e230dd2SCorentin Chary {
66504d2529dSDaniel P. Berrange     if (vs->ioc == NULL || !vnc_has_feature(vs, VNC_FEATURE_RESIZE)) {
6663e230dd2SCorentin Chary         return;
6673e230dd2SCorentin Chary     }
668bea60dd7SPeter Lieven     if (vs->client_width == pixman_image_get_width(vs->vd->server) &&
669bea60dd7SPeter Lieven         vs->client_height == pixman_image_get_height(vs->vd->server)) {
6703e230dd2SCorentin Chary         return;
6713e230dd2SCorentin Chary     }
6724c956bd8SDaniel P. Berrange 
6734c956bd8SDaniel P. Berrange     assert(pixman_image_get_width(vs->vd->server) < 65536 &&
6744c956bd8SDaniel P. Berrange            pixman_image_get_width(vs->vd->server) >= 0);
6754c956bd8SDaniel P. Berrange     assert(pixman_image_get_height(vs->vd->server) < 65536 &&
6764c956bd8SDaniel P. Berrange            pixman_image_get_height(vs->vd->server) >= 0);
677bea60dd7SPeter Lieven     vs->client_width = pixman_image_get_width(vs->vd->server);
678bea60dd7SPeter Lieven     vs->client_height = pixman_image_get_height(vs->vd->server);
679bd023f95SCorentin Chary     vnc_lock_output(vs);
6803e230dd2SCorentin Chary     vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE);
6813e230dd2SCorentin Chary     vnc_write_u8(vs, 0);
6823e230dd2SCorentin Chary     vnc_write_u16(vs, 1); /* number of rects */
6833e230dd2SCorentin Chary     vnc_framebuffer_update(vs, 0, 0, vs->client_width, vs->client_height,
6843e230dd2SCorentin Chary                            VNC_ENCODING_DESKTOPRESIZE);
685bd023f95SCorentin Chary     vnc_unlock_output(vs);
6863e230dd2SCorentin Chary     vnc_flush(vs);
6873e230dd2SCorentin Chary }
6883e230dd2SCorentin Chary 
689bd023f95SCorentin Chary static void vnc_abort_display_jobs(VncDisplay *vd)
690bd023f95SCorentin Chary {
691bd023f95SCorentin Chary     VncState *vs;
692bd023f95SCorentin Chary 
693bd023f95SCorentin Chary     QTAILQ_FOREACH(vs, &vd->clients, next) {
694bd023f95SCorentin Chary         vnc_lock_output(vs);
695bd023f95SCorentin Chary         vs->abort = true;
696bd023f95SCorentin Chary         vnc_unlock_output(vs);
697bd023f95SCorentin Chary     }
698bd023f95SCorentin Chary     QTAILQ_FOREACH(vs, &vd->clients, next) {
699bd023f95SCorentin Chary         vnc_jobs_join(vs);
700bd023f95SCorentin Chary     }
701bd023f95SCorentin Chary     QTAILQ_FOREACH(vs, &vd->clients, next) {
702bd023f95SCorentin Chary         vnc_lock_output(vs);
703bd023f95SCorentin Chary         vs->abort = false;
704bd023f95SCorentin Chary         vnc_unlock_output(vs);
705bd023f95SCorentin Chary     }
706bd023f95SCorentin Chary }
707bd023f95SCorentin Chary 
7089f64916dSGerd Hoffmann int vnc_server_fb_stride(VncDisplay *vd)
7099f64916dSGerd Hoffmann {
7109f64916dSGerd Hoffmann     return pixman_image_get_stride(vd->server);
7119f64916dSGerd Hoffmann }
7129f64916dSGerd Hoffmann 
7139f64916dSGerd Hoffmann void *vnc_server_fb_ptr(VncDisplay *vd, int x, int y)
7149f64916dSGerd Hoffmann {
7159f64916dSGerd Hoffmann     uint8_t *ptr;
7169f64916dSGerd Hoffmann 
7179f64916dSGerd Hoffmann     ptr  = (uint8_t *)pixman_image_get_data(vd->server);
7189f64916dSGerd Hoffmann     ptr += y * vnc_server_fb_stride(vd);
7199f64916dSGerd Hoffmann     ptr += x * VNC_SERVER_FB_BYTES;
7209f64916dSGerd Hoffmann     return ptr;
7219f64916dSGerd Hoffmann }
7229f64916dSGerd Hoffmann 
723453f842bSGerd Hoffmann static void vnc_update_server_surface(VncDisplay *vd)
724453f842bSGerd Hoffmann {
725b69a553bSDaniel P. Berrange     int width, height;
726b69a553bSDaniel P. Berrange 
727453f842bSGerd Hoffmann     qemu_pixman_image_unref(vd->server);
728453f842bSGerd Hoffmann     vd->server = NULL;
729453f842bSGerd Hoffmann 
730c7628bffSGerd Hoffmann     if (QTAILQ_EMPTY(&vd->clients)) {
731c7628bffSGerd Hoffmann         return;
732c7628bffSGerd Hoffmann     }
733c7628bffSGerd Hoffmann 
734b69a553bSDaniel P. Berrange     width = vnc_width(vd);
735b69a553bSDaniel P. Berrange     height = vnc_height(vd);
736453f842bSGerd Hoffmann     vd->server = pixman_image_create_bits(VNC_SERVER_FB_FORMAT,
737b69a553bSDaniel P. Berrange                                           width, height,
738453f842bSGerd Hoffmann                                           NULL, 0);
739b69a553bSDaniel P. Berrange 
740b69a553bSDaniel P. Berrange     memset(vd->guest.dirty, 0x00, sizeof(vd->guest.dirty));
741b69a553bSDaniel P. Berrange     vnc_set_area_dirty(vd->guest.dirty, vd, 0, 0,
742b69a553bSDaniel P. Berrange                        width, height);
743453f842bSGerd Hoffmann }
744453f842bSGerd Hoffmann 
745c12aeb86SGerd Hoffmann static void vnc_dpy_switch(DisplayChangeListener *dcl,
746c12aeb86SGerd Hoffmann                            DisplaySurface *surface)
7473e230dd2SCorentin Chary {
7482e5567c9SGerd Hoffmann     static const char placeholder_msg[] =
7492e5567c9SGerd Hoffmann         "Display output is not active.";
7502e5567c9SGerd Hoffmann     static DisplaySurface *placeholder;
75121ef45d7SGerd Hoffmann     VncDisplay *vd = container_of(dcl, VncDisplay, dcl);
7523e230dd2SCorentin Chary     VncState *vs;
7533e230dd2SCorentin Chary 
7542e5567c9SGerd Hoffmann     if (surface == NULL) {
7552e5567c9SGerd Hoffmann         if (placeholder == NULL) {
7562e5567c9SGerd Hoffmann             placeholder = qemu_create_message_surface(640, 480, placeholder_msg);
7572e5567c9SGerd Hoffmann         }
7582e5567c9SGerd Hoffmann         surface = placeholder;
7592e5567c9SGerd Hoffmann     }
7602e5567c9SGerd Hoffmann 
761bd023f95SCorentin Chary     vnc_abort_display_jobs(vd);
762453f842bSGerd Hoffmann     vd->ds = surface;
763bd023f95SCorentin Chary 
7643e230dd2SCorentin Chary     /* server surface */
765453f842bSGerd Hoffmann     vnc_update_server_surface(vd);
7663e230dd2SCorentin Chary 
7673e230dd2SCorentin Chary     /* guest surface */
7689f64916dSGerd Hoffmann     qemu_pixman_image_unref(vd->guest.fb);
769d39fa6d8SGerd Hoffmann     vd->guest.fb = pixman_image_ref(surface->image);
770d39fa6d8SGerd Hoffmann     vd->guest.format = surface->format;
7713e230dd2SCorentin Chary 
7723e230dd2SCorentin Chary     QTAILQ_FOREACH(vs, &vd->clients, next) {
7733e230dd2SCorentin Chary         vnc_colordepth(vs);
7743e230dd2SCorentin Chary         vnc_desktop_resize(vs);
7753e230dd2SCorentin Chary         if (vs->vd->cursor) {
7763e230dd2SCorentin Chary             vnc_cursor_define(vs);
7773e230dd2SCorentin Chary         }
778bea60dd7SPeter Lieven         memset(vs->dirty, 0x00, sizeof(vs->dirty));
779f7b3d68cSGerd Hoffmann         vnc_set_area_dirty(vs->dirty, vd, 0, 0,
780b69a553bSDaniel P. Berrange                            vnc_width(vd),
781b69a553bSDaniel P. Berrange                            vnc_height(vd));
782e2b72cb6SDaniel P. Berrange         vnc_update_throttle_offset(vs);
7833e230dd2SCorentin Chary     }
7843e230dd2SCorentin Chary }
7853e230dd2SCorentin Chary 
7863e230dd2SCorentin Chary /* fastest code */
7879f64916dSGerd Hoffmann static void vnc_write_pixels_copy(VncState *vs,
7883e230dd2SCorentin Chary                                   void *pixels, int size)
7893e230dd2SCorentin Chary {
7903e230dd2SCorentin Chary     vnc_write(vs, pixels, size);
7913e230dd2SCorentin Chary }
7923e230dd2SCorentin Chary 
7933e230dd2SCorentin Chary /* slowest but generic code. */
7943e230dd2SCorentin Chary void vnc_convert_pixel(VncState *vs, uint8_t *buf, uint32_t v)
7953e230dd2SCorentin Chary {
7963e230dd2SCorentin Chary     uint8_t r, g, b;
7973e230dd2SCorentin Chary 
7989f64916dSGerd Hoffmann #if VNC_SERVER_FB_FORMAT == PIXMAN_FORMAT(32, PIXMAN_TYPE_ARGB, 0, 8, 8, 8)
7999f64916dSGerd Hoffmann     r = (((v & 0x00ff0000) >> 16) << vs->client_pf.rbits) >> 8;
8009f64916dSGerd Hoffmann     g = (((v & 0x0000ff00) >>  8) << vs->client_pf.gbits) >> 8;
8019f64916dSGerd Hoffmann     b = (((v & 0x000000ff) >>  0) << vs->client_pf.bbits) >> 8;
8029f64916dSGerd Hoffmann #else
8039f64916dSGerd Hoffmann # error need some bits here if you change VNC_SERVER_FB_FORMAT
8049f64916dSGerd Hoffmann #endif
8059f64916dSGerd Hoffmann     v = (r << vs->client_pf.rshift) |
8069f64916dSGerd Hoffmann         (g << vs->client_pf.gshift) |
8079f64916dSGerd Hoffmann         (b << vs->client_pf.bshift);
8089f64916dSGerd Hoffmann     switch (vs->client_pf.bytes_per_pixel) {
8093e230dd2SCorentin Chary     case 1:
8103e230dd2SCorentin Chary         buf[0] = v;
8113e230dd2SCorentin Chary         break;
8123e230dd2SCorentin Chary     case 2:
8139f64916dSGerd Hoffmann         if (vs->client_be) {
8143e230dd2SCorentin Chary             buf[0] = v >> 8;
8153e230dd2SCorentin Chary             buf[1] = v;
8163e230dd2SCorentin Chary         } else {
8173e230dd2SCorentin Chary             buf[1] = v >> 8;
8183e230dd2SCorentin Chary             buf[0] = v;
8193e230dd2SCorentin Chary         }
8203e230dd2SCorentin Chary         break;
8213e230dd2SCorentin Chary     default:
8223e230dd2SCorentin Chary     case 4:
8239f64916dSGerd Hoffmann         if (vs->client_be) {
8243e230dd2SCorentin Chary             buf[0] = v >> 24;
8253e230dd2SCorentin Chary             buf[1] = v >> 16;
8263e230dd2SCorentin Chary             buf[2] = v >> 8;
8273e230dd2SCorentin Chary             buf[3] = v;
8283e230dd2SCorentin Chary         } else {
8293e230dd2SCorentin Chary             buf[3] = v >> 24;
8303e230dd2SCorentin Chary             buf[2] = v >> 16;
8313e230dd2SCorentin Chary             buf[1] = v >> 8;
8323e230dd2SCorentin Chary             buf[0] = v;
8333e230dd2SCorentin Chary         }
8343e230dd2SCorentin Chary         break;
8353e230dd2SCorentin Chary     }
8363e230dd2SCorentin Chary }
8373e230dd2SCorentin Chary 
8389f64916dSGerd Hoffmann static void vnc_write_pixels_generic(VncState *vs,
8393e230dd2SCorentin Chary                                      void *pixels1, int size)
8403e230dd2SCorentin Chary {
8413e230dd2SCorentin Chary     uint8_t buf[4];
8423e230dd2SCorentin Chary 
8439f64916dSGerd Hoffmann     if (VNC_SERVER_FB_BYTES == 4) {
8443e230dd2SCorentin Chary         uint32_t *pixels = pixels1;
8453e230dd2SCorentin Chary         int n, i;
8463e230dd2SCorentin Chary         n = size >> 2;
8473e230dd2SCorentin Chary         for (i = 0; i < n; i++) {
8483e230dd2SCorentin Chary             vnc_convert_pixel(vs, buf, pixels[i]);
8499f64916dSGerd Hoffmann             vnc_write(vs, buf, vs->client_pf.bytes_per_pixel);
8503e230dd2SCorentin Chary         }
8513e230dd2SCorentin Chary     }
8523e230dd2SCorentin Chary }
8533e230dd2SCorentin Chary 
8543e230dd2SCorentin Chary int vnc_raw_send_framebuffer_update(VncState *vs, int x, int y, int w, int h)
8553e230dd2SCorentin Chary {
8563e230dd2SCorentin Chary     int i;
8573e230dd2SCorentin Chary     uint8_t *row;
8583e230dd2SCorentin Chary     VncDisplay *vd = vs->vd;
8593e230dd2SCorentin Chary 
8609f64916dSGerd Hoffmann     row = vnc_server_fb_ptr(vd, x, y);
8613e230dd2SCorentin Chary     for (i = 0; i < h; i++) {
8629f64916dSGerd Hoffmann         vs->write_pixels(vs, row, w * VNC_SERVER_FB_BYTES);
8639f64916dSGerd Hoffmann         row += vnc_server_fb_stride(vd);
8643e230dd2SCorentin Chary     }
8653e230dd2SCorentin Chary     return 1;
8663e230dd2SCorentin Chary }
8673e230dd2SCorentin Chary 
868bd023f95SCorentin Chary int vnc_send_framebuffer_update(VncState *vs, int x, int y, int w, int h)
8693e230dd2SCorentin Chary {
8703e230dd2SCorentin Chary     int n = 0;
871de3f7de7SPeter Lieven     bool encode_raw = false;
872de3f7de7SPeter Lieven     size_t saved_offs = vs->output.offset;
8733e230dd2SCorentin Chary 
8743e230dd2SCorentin Chary     switch(vs->vnc_encoding) {
8753e230dd2SCorentin Chary         case VNC_ENCODING_ZLIB:
8763e230dd2SCorentin Chary             n = vnc_zlib_send_framebuffer_update(vs, x, y, w, h);
8773e230dd2SCorentin Chary             break;
8783e230dd2SCorentin Chary         case VNC_ENCODING_HEXTILE:
8793e230dd2SCorentin Chary             vnc_framebuffer_update(vs, x, y, w, h, VNC_ENCODING_HEXTILE);
8803e230dd2SCorentin Chary             n = vnc_hextile_send_framebuffer_update(vs, x, y, w, h);
8813e230dd2SCorentin Chary             break;
8823e230dd2SCorentin Chary         case VNC_ENCODING_TIGHT:
8833e230dd2SCorentin Chary             n = vnc_tight_send_framebuffer_update(vs, x, y, w, h);
8843e230dd2SCorentin Chary             break;
885efe556adSCorentin Chary         case VNC_ENCODING_TIGHT_PNG:
886efe556adSCorentin Chary             n = vnc_tight_png_send_framebuffer_update(vs, x, y, w, h);
887efe556adSCorentin Chary             break;
888148954faSCorentin Chary         case VNC_ENCODING_ZRLE:
889148954faSCorentin Chary             n = vnc_zrle_send_framebuffer_update(vs, x, y, w, h);
890148954faSCorentin Chary             break;
891148954faSCorentin Chary         case VNC_ENCODING_ZYWRLE:
892148954faSCorentin Chary             n = vnc_zywrle_send_framebuffer_update(vs, x, y, w, h);
893148954faSCorentin Chary             break;
8943e230dd2SCorentin Chary         default:
895de3f7de7SPeter Lieven             encode_raw = true;
8963e230dd2SCorentin Chary             break;
8973e230dd2SCorentin Chary     }
898de3f7de7SPeter Lieven 
899de3f7de7SPeter Lieven     /* If the client has the same pixel format as our internal buffer and
900de3f7de7SPeter Lieven      * a RAW encoding would need less space fall back to RAW encoding to
901de3f7de7SPeter Lieven      * save bandwidth and processing power in the client. */
902de3f7de7SPeter Lieven     if (!encode_raw && vs->write_pixels == vnc_write_pixels_copy &&
903de3f7de7SPeter Lieven         12 + h * w * VNC_SERVER_FB_BYTES <= (vs->output.offset - saved_offs)) {
904de3f7de7SPeter Lieven         vs->output.offset = saved_offs;
905de3f7de7SPeter Lieven         encode_raw = true;
906de3f7de7SPeter Lieven     }
907de3f7de7SPeter Lieven 
908de3f7de7SPeter Lieven     if (encode_raw) {
909de3f7de7SPeter Lieven         vnc_framebuffer_update(vs, x, y, w, h, VNC_ENCODING_RAW);
910de3f7de7SPeter Lieven         n = vnc_raw_send_framebuffer_update(vs, x, y, w, h);
911de3f7de7SPeter Lieven     }
912de3f7de7SPeter Lieven 
9133e230dd2SCorentin Chary     return n;
9143e230dd2SCorentin Chary }
9153e230dd2SCorentin Chary 
9167c20b4a3SGerd Hoffmann static void vnc_mouse_set(DisplayChangeListener *dcl,
9177c20b4a3SGerd Hoffmann                           int x, int y, int visible)
9183e230dd2SCorentin Chary {
9193e230dd2SCorentin Chary     /* can we ask the client(s) to move the pointer ??? */
9203e230dd2SCorentin Chary }
9213e230dd2SCorentin Chary 
9223e230dd2SCorentin Chary static int vnc_cursor_define(VncState *vs)
9233e230dd2SCorentin Chary {
9243e230dd2SCorentin Chary     QEMUCursor *c = vs->vd->cursor;
9253e230dd2SCorentin Chary     int isize;
9263e230dd2SCorentin Chary 
9273e230dd2SCorentin Chary     if (vnc_has_feature(vs, VNC_FEATURE_RICH_CURSOR)) {
928d01f9595SCorentin Chary         vnc_lock_output(vs);
9293e230dd2SCorentin Chary         vnc_write_u8(vs,  VNC_MSG_SERVER_FRAMEBUFFER_UPDATE);
9303e230dd2SCorentin Chary         vnc_write_u8(vs,  0);  /*  padding     */
9313e230dd2SCorentin Chary         vnc_write_u16(vs, 1);  /*  # of rects  */
9323e230dd2SCorentin Chary         vnc_framebuffer_update(vs, c->hot_x, c->hot_y, c->width, c->height,
9333e230dd2SCorentin Chary                                VNC_ENCODING_RICH_CURSOR);
9349f64916dSGerd Hoffmann         isize = c->width * c->height * vs->client_pf.bytes_per_pixel;
9359f64916dSGerd Hoffmann         vnc_write_pixels_generic(vs, c->data, isize);
9363e230dd2SCorentin Chary         vnc_write(vs, vs->vd->cursor_mask, vs->vd->cursor_msize);
937d01f9595SCorentin Chary         vnc_unlock_output(vs);
9383e230dd2SCorentin Chary         return 0;
9393e230dd2SCorentin Chary     }
9403e230dd2SCorentin Chary     return -1;
9413e230dd2SCorentin Chary }
9423e230dd2SCorentin Chary 
9437c20b4a3SGerd Hoffmann static void vnc_dpy_cursor_define(DisplayChangeListener *dcl,
9447c20b4a3SGerd Hoffmann                                   QEMUCursor *c)
9453e230dd2SCorentin Chary {
946d616ccc5SGerd Hoffmann     VncDisplay *vd = container_of(dcl, VncDisplay, dcl);
9473e230dd2SCorentin Chary     VncState *vs;
9483e230dd2SCorentin Chary 
9493e230dd2SCorentin Chary     cursor_put(vd->cursor);
9507267c094SAnthony Liguori     g_free(vd->cursor_mask);
9513e230dd2SCorentin Chary 
9523e230dd2SCorentin Chary     vd->cursor = c;
9533e230dd2SCorentin Chary     cursor_get(vd->cursor);
9543e230dd2SCorentin Chary     vd->cursor_msize = cursor_get_mono_bpl(c) * c->height;
9557267c094SAnthony Liguori     vd->cursor_mask = g_malloc0(vd->cursor_msize);
9563e230dd2SCorentin Chary     cursor_get_mono_mask(c, 0, vd->cursor_mask);
9573e230dd2SCorentin Chary 
9583e230dd2SCorentin Chary     QTAILQ_FOREACH(vs, &vd->clients, next) {
9593e230dd2SCorentin Chary         vnc_cursor_define(vs);
9603e230dd2SCorentin Chary     }
9613e230dd2SCorentin Chary }
9623e230dd2SCorentin Chary 
9634769a881SChih-Min Chao static int find_and_clear_dirty_height(VncState *vs,
9646c71a539SCorentin Chary                                        int y, int last_x, int x, int height)
9653e230dd2SCorentin Chary {
9663e230dd2SCorentin Chary     int h;
9673e230dd2SCorentin Chary 
9686c71a539SCorentin Chary     for (h = 1; h < (height - y); h++) {
969bc2429b9SCorentin Chary         if (!test_bit(last_x, vs->dirty[y + h])) {
9703e230dd2SCorentin Chary             break;
971bc2429b9SCorentin Chary         }
972863d7c91SPeter Lieven         bitmap_clear(vs->dirty[y + h], last_x, x - last_x);
9733e230dd2SCorentin Chary     }
9743e230dd2SCorentin Chary 
9753e230dd2SCorentin Chary     return h;
9763e230dd2SCorentin Chary }
9773e230dd2SCorentin Chary 
978e2b72cb6SDaniel P. Berrange /*
979e2b72cb6SDaniel P. Berrange  * Figure out how much pending data we should allow in the output
980e2b72cb6SDaniel P. Berrange  * buffer before we throttle incremental display updates, and/or
981e2b72cb6SDaniel P. Berrange  * drop audio samples.
982e2b72cb6SDaniel P. Berrange  *
983e2b72cb6SDaniel P. Berrange  * We allow for equiv of 1 full display's worth of FB updates,
984e2b72cb6SDaniel P. Berrange  * and 1 second of audio samples. If audio backlog was larger
985e2b72cb6SDaniel P. Berrange  * than that the client would already suffering awful audio
986e2b72cb6SDaniel P. Berrange  * glitches, so dropping samples is no worse really).
987e2b72cb6SDaniel P. Berrange  */
988e2b72cb6SDaniel P. Berrange static void vnc_update_throttle_offset(VncState *vs)
989e2b72cb6SDaniel P. Berrange {
990e2b72cb6SDaniel P. Berrange     size_t offset =
991e2b72cb6SDaniel P. Berrange         vs->client_width * vs->client_height * vs->client_pf.bytes_per_pixel;
992e2b72cb6SDaniel P. Berrange 
993e2b72cb6SDaniel P. Berrange     if (vs->audio_cap) {
994e2b72cb6SDaniel P. Berrange         int bps;
995e2b72cb6SDaniel P. Berrange         switch (vs->as.fmt) {
996e2b72cb6SDaniel P. Berrange         default:
997e2b72cb6SDaniel P. Berrange         case  AUD_FMT_U8:
998e2b72cb6SDaniel P. Berrange         case  AUD_FMT_S8:
999e2b72cb6SDaniel P. Berrange             bps = 1;
1000e2b72cb6SDaniel P. Berrange             break;
1001e2b72cb6SDaniel P. Berrange         case  AUD_FMT_U16:
1002e2b72cb6SDaniel P. Berrange         case  AUD_FMT_S16:
1003e2b72cb6SDaniel P. Berrange             bps = 2;
1004e2b72cb6SDaniel P. Berrange             break;
1005e2b72cb6SDaniel P. Berrange         case  AUD_FMT_U32:
1006e2b72cb6SDaniel P. Berrange         case  AUD_FMT_S32:
1007e2b72cb6SDaniel P. Berrange             bps = 4;
1008e2b72cb6SDaniel P. Berrange             break;
1009e2b72cb6SDaniel P. Berrange         }
1010cf070658SDaniel P. Berrangé         offset += vs->as.freq * bps * vs->as.nchannels;
1011e2b72cb6SDaniel P. Berrange     }
1012e2b72cb6SDaniel P. Berrange 
1013e2b72cb6SDaniel P. Berrange     /* Put a floor of 1MB on offset, so that if we have a large pending
1014e2b72cb6SDaniel P. Berrange      * buffer and the display is resized to a small size & back again
1015e2b72cb6SDaniel P. Berrange      * we don't suddenly apply a tiny send limit
1016e2b72cb6SDaniel P. Berrange      */
1017e2b72cb6SDaniel P. Berrange     offset = MAX(offset, 1024 * 1024);
1018e2b72cb6SDaniel P. Berrange 
10196aa22a29SDaniel P. Berrange     if (vs->throttle_output_offset != offset) {
10206aa22a29SDaniel P. Berrange         trace_vnc_client_throttle_threshold(
10216aa22a29SDaniel P. Berrange             vs, vs->ioc, vs->throttle_output_offset, offset, vs->client_width,
10226aa22a29SDaniel P. Berrange             vs->client_height, vs->client_pf.bytes_per_pixel, vs->audio_cap);
10236aa22a29SDaniel P. Berrange     }
10246aa22a29SDaniel P. Berrange 
1025e2b72cb6SDaniel P. Berrange     vs->throttle_output_offset = offset;
1026e2b72cb6SDaniel P. Berrange }
1027e2b72cb6SDaniel P. Berrange 
10280bad8342SDaniel P. Berrange static bool vnc_should_update(VncState *vs)
10290bad8342SDaniel P. Berrange {
10300bad8342SDaniel P. Berrange     switch (vs->update) {
10310bad8342SDaniel P. Berrange     case VNC_STATE_UPDATE_NONE:
10320bad8342SDaniel P. Berrange         break;
10330bad8342SDaniel P. Berrange     case VNC_STATE_UPDATE_INCREMENTAL:
1034e2b72cb6SDaniel P. Berrange         /* Only allow incremental updates if the pending send queue
1035ada8d2e4SDaniel P. Berrange          * is less than the permitted threshold, and the job worker
1036ada8d2e4SDaniel P. Berrange          * is completely idle.
10370bad8342SDaniel P. Berrange          */
1038ada8d2e4SDaniel P. Berrange         if (vs->output.offset < vs->throttle_output_offset &&
1039ada8d2e4SDaniel P. Berrange             vs->job_update == VNC_STATE_UPDATE_NONE) {
10400bad8342SDaniel P. Berrange             return true;
10410bad8342SDaniel P. Berrange         }
10426aa22a29SDaniel P. Berrange         trace_vnc_client_throttle_incremental(
10436aa22a29SDaniel P. Berrange             vs, vs->ioc, vs->job_update, vs->output.offset);
10440bad8342SDaniel P. Berrange         break;
10450bad8342SDaniel P. Berrange     case VNC_STATE_UPDATE_FORCE:
1046ada8d2e4SDaniel P. Berrange         /* Only allow forced updates if the pending send queue
1047ada8d2e4SDaniel P. Berrange          * does not contain a previous forced update, and the
1048ada8d2e4SDaniel P. Berrange          * job worker is completely idle.
1049ada8d2e4SDaniel P. Berrange          *
1050ada8d2e4SDaniel P. Berrange          * Note this means we'll queue a forced update, even if
1051ada8d2e4SDaniel P. Berrange          * the output buffer size is otherwise over the throttle
1052ada8d2e4SDaniel P. Berrange          * output limit.
1053ada8d2e4SDaniel P. Berrange          */
1054ada8d2e4SDaniel P. Berrange         if (vs->force_update_offset == 0 &&
1055ada8d2e4SDaniel P. Berrange             vs->job_update == VNC_STATE_UPDATE_NONE) {
10560bad8342SDaniel P. Berrange             return true;
10570bad8342SDaniel P. Berrange         }
10586aa22a29SDaniel P. Berrange         trace_vnc_client_throttle_forced(
10596aa22a29SDaniel P. Berrange             vs, vs->ioc, vs->job_update, vs->force_update_offset);
1060ada8d2e4SDaniel P. Berrange         break;
1061ada8d2e4SDaniel P. Berrange     }
10620bad8342SDaniel P. Berrange     return false;
10630bad8342SDaniel P. Berrange }
10640bad8342SDaniel P. Berrange 
10656af998dbSDaniel P. Berrange static int vnc_update_client(VncState *vs, int has_dirty)
10663e230dd2SCorentin Chary {
10673e230dd2SCorentin Chary     VncDisplay *vd = vs->vd;
1068bd023f95SCorentin Chary     VncJob *job;
10693e230dd2SCorentin Chary     int y;
10702f487a3dSPeter Lieven     int height, width;
1071bd023f95SCorentin Chary     int n = 0;
1072bd023f95SCorentin Chary 
1073b939eb89SDaniel P. Berrange     if (vs->disconnecting) {
1074b939eb89SDaniel P. Berrange         vnc_disconnect_finish(vs);
1075b939eb89SDaniel P. Berrange         return 0;
1076b939eb89SDaniel P. Berrange     }
1077b939eb89SDaniel P. Berrange 
1078b939eb89SDaniel P. Berrange     vs->has_dirty += has_dirty;
10790bad8342SDaniel P. Berrange     if (!vnc_should_update(vs)) {
10803e230dd2SCorentin Chary         return 0;
1081b939eb89SDaniel P. Berrange     }
10823e230dd2SCorentin Chary 
1083fef1bbadSDaniel P. Berrange     if (!vs->has_dirty && vs->update != VNC_STATE_UPDATE_FORCE) {
10843e230dd2SCorentin Chary         return 0;
1085b939eb89SDaniel P. Berrange     }
10863e230dd2SCorentin Chary 
10873e230dd2SCorentin Chary     /*
10883e230dd2SCorentin Chary      * Send screen updates to the vnc client using the server
10893e230dd2SCorentin Chary      * surface and server dirty map.  guest surface updates
10903e230dd2SCorentin Chary      * happening in parallel don't disturb us, the next pass will
10913e230dd2SCorentin Chary      * send them to the client.
10923e230dd2SCorentin Chary      */
1093bd023f95SCorentin Chary     job = vnc_job_new(vs);
10943e230dd2SCorentin Chary 
1095bea60dd7SPeter Lieven     height = pixman_image_get_height(vd->server);
1096bea60dd7SPeter Lieven     width = pixman_image_get_width(vd->server);
10973e230dd2SCorentin Chary 
109812b316d4SPeter Lieven     y = 0;
109912b316d4SPeter Lieven     for (;;) {
110012b316d4SPeter Lieven         int x, h;
110112b316d4SPeter Lieven         unsigned long x2;
110212b316d4SPeter Lieven         unsigned long offset = find_next_bit((unsigned long *) &vs->dirty,
110312b316d4SPeter Lieven                                              height * VNC_DIRTY_BPL(vs),
110412b316d4SPeter Lieven                                              y * VNC_DIRTY_BPL(vs));
110512b316d4SPeter Lieven         if (offset == height * VNC_DIRTY_BPL(vs)) {
110612b316d4SPeter Lieven             /* no more dirty bits */
110712b316d4SPeter Lieven             break;
11083e230dd2SCorentin Chary         }
110912b316d4SPeter Lieven         y = offset / VNC_DIRTY_BPL(vs);
111012b316d4SPeter Lieven         x = offset % VNC_DIRTY_BPL(vs);
111112b316d4SPeter Lieven         x2 = find_next_zero_bit((unsigned long *) &vs->dirty[y],
111212b316d4SPeter Lieven                                 VNC_DIRTY_BPL(vs), x);
111312b316d4SPeter Lieven         bitmap_clear(vs->dirty[y], x, x2 - x);
111412b316d4SPeter Lieven         h = find_and_clear_dirty_height(vs, y, x, x2, height);
11152f487a3dSPeter Lieven         x2 = MIN(x2, width / VNC_DIRTY_PIXELS_PER_BIT);
11162f487a3dSPeter Lieven         if (x2 > x) {
111712b316d4SPeter Lieven             n += vnc_job_add_rect(job, x * VNC_DIRTY_PIXELS_PER_BIT, y,
111812b316d4SPeter Lieven                                   (x2 - x) * VNC_DIRTY_PIXELS_PER_BIT, h);
11193e230dd2SCorentin Chary         }
11200e7d6f60SPeter Lieven         if (!x && x2 == width / VNC_DIRTY_PIXELS_PER_BIT) {
11210e7d6f60SPeter Lieven             y += h;
11220e7d6f60SPeter Lieven             if (y == height) {
11230e7d6f60SPeter Lieven                 break;
11240e7d6f60SPeter Lieven             }
11250e7d6f60SPeter Lieven         }
11262f487a3dSPeter Lieven     }
1127bd023f95SCorentin Chary 
1128ada8d2e4SDaniel P. Berrange     vs->job_update = vs->update;
1129728a7ac9SDaniel P. Berrange     vs->update = VNC_STATE_UPDATE_NONE;
1130ada8d2e4SDaniel P. Berrange     vnc_job_push(job);
113163658280SGerd Hoffmann     vs->has_dirty = 0;
1132bd023f95SCorentin Chary     return n;
11333e230dd2SCorentin Chary }
11343e230dd2SCorentin Chary 
11353e230dd2SCorentin Chary /* audio */
11363e230dd2SCorentin Chary static void audio_capture_notify(void *opaque, audcnotification_e cmd)
11373e230dd2SCorentin Chary {
11383e230dd2SCorentin Chary     VncState *vs = opaque;
11393e230dd2SCorentin Chary 
1140f31f9c10SGerd Hoffmann     assert(vs->magic == VNC_MAGIC);
11413e230dd2SCorentin Chary     switch (cmd) {
11423e230dd2SCorentin Chary     case AUD_CNOTIFY_DISABLE:
1143bd023f95SCorentin Chary         vnc_lock_output(vs);
11443e230dd2SCorentin Chary         vnc_write_u8(vs, VNC_MSG_SERVER_QEMU);
11453e230dd2SCorentin Chary         vnc_write_u8(vs, VNC_MSG_SERVER_QEMU_AUDIO);
11463e230dd2SCorentin Chary         vnc_write_u16(vs, VNC_MSG_SERVER_QEMU_AUDIO_END);
1147bd023f95SCorentin Chary         vnc_unlock_output(vs);
11483e230dd2SCorentin Chary         vnc_flush(vs);
11493e230dd2SCorentin Chary         break;
11503e230dd2SCorentin Chary 
11513e230dd2SCorentin Chary     case AUD_CNOTIFY_ENABLE:
1152bd023f95SCorentin Chary         vnc_lock_output(vs);
11533e230dd2SCorentin Chary         vnc_write_u8(vs, VNC_MSG_SERVER_QEMU);
11543e230dd2SCorentin Chary         vnc_write_u8(vs, VNC_MSG_SERVER_QEMU_AUDIO);
11553e230dd2SCorentin Chary         vnc_write_u16(vs, VNC_MSG_SERVER_QEMU_AUDIO_BEGIN);
1156bd023f95SCorentin Chary         vnc_unlock_output(vs);
11573e230dd2SCorentin Chary         vnc_flush(vs);
11583e230dd2SCorentin Chary         break;
11593e230dd2SCorentin Chary     }
11603e230dd2SCorentin Chary }
11613e230dd2SCorentin Chary 
11623e230dd2SCorentin Chary static void audio_capture_destroy(void *opaque)
11633e230dd2SCorentin Chary {
11643e230dd2SCorentin Chary }
11653e230dd2SCorentin Chary 
11663e230dd2SCorentin Chary static void audio_capture(void *opaque, void *buf, int size)
11673e230dd2SCorentin Chary {
11683e230dd2SCorentin Chary     VncState *vs = opaque;
11693e230dd2SCorentin Chary 
1170f31f9c10SGerd Hoffmann     assert(vs->magic == VNC_MAGIC);
1171bd023f95SCorentin Chary     vnc_lock_output(vs);
1172e2b72cb6SDaniel P. Berrange     if (vs->output.offset < vs->throttle_output_offset) {
11733e230dd2SCorentin Chary         vnc_write_u8(vs, VNC_MSG_SERVER_QEMU);
11743e230dd2SCorentin Chary         vnc_write_u8(vs, VNC_MSG_SERVER_QEMU_AUDIO);
11753e230dd2SCorentin Chary         vnc_write_u16(vs, VNC_MSG_SERVER_QEMU_AUDIO_DATA);
11763e230dd2SCorentin Chary         vnc_write_u32(vs, size);
11773e230dd2SCorentin Chary         vnc_write(vs, buf, size);
11786aa22a29SDaniel P. Berrange     } else {
11796aa22a29SDaniel P. Berrange         trace_vnc_client_throttle_audio(vs, vs->ioc, vs->output.offset);
1180e2b72cb6SDaniel P. Berrange     }
1181bd023f95SCorentin Chary     vnc_unlock_output(vs);
11823e230dd2SCorentin Chary     vnc_flush(vs);
11833e230dd2SCorentin Chary }
11843e230dd2SCorentin Chary 
11853e230dd2SCorentin Chary static void audio_add(VncState *vs)
11863e230dd2SCorentin Chary {
11873e230dd2SCorentin Chary     struct audio_capture_ops ops;
11883e230dd2SCorentin Chary 
11893e230dd2SCorentin Chary     if (vs->audio_cap) {
1190027a79c3SCole Robinson         error_report("audio already running");
11913e230dd2SCorentin Chary         return;
11923e230dd2SCorentin Chary     }
11933e230dd2SCorentin Chary 
11943e230dd2SCorentin Chary     ops.notify = audio_capture_notify;
11953e230dd2SCorentin Chary     ops.destroy = audio_capture_destroy;
11963e230dd2SCorentin Chary     ops.capture = audio_capture;
11973e230dd2SCorentin Chary 
11983e230dd2SCorentin Chary     vs->audio_cap = AUD_add_capture(&vs->as, &ops, vs);
11993e230dd2SCorentin Chary     if (!vs->audio_cap) {
1200027a79c3SCole Robinson         error_report("Failed to add audio capture");
12013e230dd2SCorentin Chary     }
12023e230dd2SCorentin Chary }
12033e230dd2SCorentin Chary 
12043e230dd2SCorentin Chary static void audio_del(VncState *vs)
12053e230dd2SCorentin Chary {
12063e230dd2SCorentin Chary     if (vs->audio_cap) {
12073e230dd2SCorentin Chary         AUD_del_capture(vs->audio_cap, vs);
12083e230dd2SCorentin Chary         vs->audio_cap = NULL;
12093e230dd2SCorentin Chary     }
12103e230dd2SCorentin Chary }
12113e230dd2SCorentin Chary 
12123e230dd2SCorentin Chary static void vnc_disconnect_start(VncState *vs)
12133e230dd2SCorentin Chary {
121404d2529dSDaniel P. Berrange     if (vs->disconnecting) {
12153e230dd2SCorentin Chary         return;
121604d2529dSDaniel P. Berrange     }
1217ad6374c4SDaniel P. Berrange     trace_vnc_client_disconnect_start(vs, vs->ioc);
12188cf36489SGerd Hoffmann     vnc_set_share_mode(vs, VNC_SHARE_MODE_DISCONNECTED);
121904d2529dSDaniel P. Berrange     if (vs->ioc_tag) {
122004d2529dSDaniel P. Berrange         g_source_remove(vs->ioc_tag);
1221a75d6f07SBrandon Carpenter         vs->ioc_tag = 0;
122204d2529dSDaniel P. Berrange     }
122304d2529dSDaniel P. Berrange     qio_channel_close(vs->ioc, NULL);
122404d2529dSDaniel P. Berrange     vs->disconnecting = TRUE;
12253e230dd2SCorentin Chary }
12263e230dd2SCorentin Chary 
12277536ee4bSTim Hardeck void vnc_disconnect_finish(VncState *vs)
12283e230dd2SCorentin Chary {
12297d964c9dSCorentin Chary     int i;
12307d964c9dSCorentin Chary 
1231ad6374c4SDaniel P. Berrange     trace_vnc_client_disconnect_finish(vs, vs->ioc);
1232ad6374c4SDaniel P. Berrange 
1233bd023f95SCorentin Chary     vnc_jobs_join(vs); /* Wait encoding jobs */
1234bd023f95SCorentin Chary 
1235bd023f95SCorentin Chary     vnc_lock_output(vs);
1236fb6ba0d5SWenchao Xia     vnc_qmp_event(vs, QAPI_EVENT_VNC_DISCONNECTED);
12373e230dd2SCorentin Chary 
12383e230dd2SCorentin Chary     buffer_free(&vs->input);
12393e230dd2SCorentin Chary     buffer_free(&vs->output);
12403e230dd2SCorentin Chary 
1241fb6ba0d5SWenchao Xia     qapi_free_VncClientInfo(vs->info);
12423e230dd2SCorentin Chary 
12433e230dd2SCorentin Chary     vnc_zlib_clear(vs);
12443e230dd2SCorentin Chary     vnc_tight_clear(vs);
1245148954faSCorentin Chary     vnc_zrle_clear(vs);
12463e230dd2SCorentin Chary 
12473e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL
12483e230dd2SCorentin Chary     vnc_sasl_client_cleanup(vs);
12493e230dd2SCorentin Chary #endif /* CONFIG_VNC_SASL */
12503e230dd2SCorentin Chary     audio_del(vs);
12517bc9318bSGerd Hoffmann     vnc_release_modifiers(vs);
12523e230dd2SCorentin Chary 
125390cd03a3SDaniel P. Berrange     if (vs->mouse_mode_notifier.notify != NULL) {
12546fd8e79aSTim Hardeck         qemu_remove_mouse_mode_change_notifier(&vs->mouse_mode_notifier);
125590cd03a3SDaniel P. Berrange     }
125690cd03a3SDaniel P. Berrange     QTAILQ_REMOVE(&vs->vd->clients, vs, next);
1257c7628bffSGerd Hoffmann     if (QTAILQ_EMPTY(&vs->vd->clients)) {
1258c7628bffSGerd Hoffmann         /* last client gone */
1259c7628bffSGerd Hoffmann         vnc_update_server_surface(vs->vd);
1260c7628bffSGerd Hoffmann     }
12613e230dd2SCorentin Chary 
1262bd023f95SCorentin Chary     vnc_unlock_output(vs);
1263bd023f95SCorentin Chary 
1264bd023f95SCorentin Chary     qemu_mutex_destroy(&vs->output_mutex);
12656fd8e79aSTim Hardeck     if (vs->bh != NULL) {
1266175b2a6eSCorentin Chary         qemu_bh_delete(vs->bh);
12676fd8e79aSTim Hardeck     }
1268175b2a6eSCorentin Chary     buffer_free(&vs->jobs_buffer);
1269175b2a6eSCorentin Chary 
12707d964c9dSCorentin Chary     for (i = 0; i < VNC_STAT_ROWS; ++i) {
12717267c094SAnthony Liguori         g_free(vs->lossy_rect[i]);
12727d964c9dSCorentin Chary     }
12737267c094SAnthony Liguori     g_free(vs->lossy_rect);
127404d2529dSDaniel P. Berrange 
127504d2529dSDaniel P. Berrange     object_unref(OBJECT(vs->ioc));
127604d2529dSDaniel P. Berrange     vs->ioc = NULL;
127704d2529dSDaniel P. Berrange     object_unref(OBJECT(vs->sioc));
127804d2529dSDaniel P. Berrange     vs->sioc = NULL;
1279f31f9c10SGerd Hoffmann     vs->magic = 0;
12807267c094SAnthony Liguori     g_free(vs);
12813e230dd2SCorentin Chary }
12823e230dd2SCorentin Chary 
128330b80fd5SDaniel P. Berrange size_t vnc_client_io_error(VncState *vs, ssize_t ret, Error **errp)
12843e230dd2SCorentin Chary {
128504d2529dSDaniel P. Berrange     if (ret <= 0) {
128604d2529dSDaniel P. Berrange         if (ret == 0) {
1287ad6374c4SDaniel P. Berrange             trace_vnc_client_eof(vs, vs->ioc);
1288537848eeSMichael Tokarev             vnc_disconnect_start(vs);
128904d2529dSDaniel P. Berrange         } else if (ret != QIO_CHANNEL_ERR_BLOCK) {
1290ad6374c4SDaniel P. Berrange             trace_vnc_client_io_error(vs, vs->ioc,
1291ad6374c4SDaniel P. Berrange                                       errp ? error_get_pretty(*errp) :
1292ad6374c4SDaniel P. Berrange                                       "Unknown");
1293537848eeSMichael Tokarev             vnc_disconnect_start(vs);
12943e230dd2SCorentin Chary         }
12953e230dd2SCorentin Chary 
129604d2529dSDaniel P. Berrange         if (errp) {
129704d2529dSDaniel P. Berrange             error_free(*errp);
129804d2529dSDaniel P. Berrange             *errp = NULL;
129904d2529dSDaniel P. Berrange         }
13003e230dd2SCorentin Chary         return 0;
13013e230dd2SCorentin Chary     }
13023e230dd2SCorentin Chary     return ret;
13033e230dd2SCorentin Chary }
13043e230dd2SCorentin Chary 
13053e230dd2SCorentin Chary 
13063e230dd2SCorentin Chary void vnc_client_error(VncState *vs)
13073e230dd2SCorentin Chary {
13083e230dd2SCorentin Chary     VNC_DEBUG("Closing down client sock: protocol error\n");
13093e230dd2SCorentin Chary     vnc_disconnect_start(vs);
13103e230dd2SCorentin Chary }
13113e230dd2SCorentin Chary 
13123e305e4aSDaniel P. Berrange 
13133e230dd2SCorentin Chary /*
13143e230dd2SCorentin Chary  * Called to write a chunk of data to the client socket. The data may
13153e230dd2SCorentin Chary  * be the raw data, or may have already been encoded by SASL.
13163e230dd2SCorentin Chary  * The data will be written either straight onto the socket, or
13173e230dd2SCorentin Chary  * written via the GNUTLS wrappers, if TLS/SSL encryption is enabled
13183e230dd2SCorentin Chary  *
13193e230dd2SCorentin Chary  * NB, it is theoretically possible to have 2 layers of encryption,
13203e230dd2SCorentin Chary  * both SASL, and this TLS layer. It is highly unlikely in practice
13213e230dd2SCorentin Chary  * though, since SASL encryption will typically be a no-op if TLS
13223e230dd2SCorentin Chary  * is active
13233e230dd2SCorentin Chary  *
13243e230dd2SCorentin Chary  * Returns the number of bytes written, which may be less than
13253e230dd2SCorentin Chary  * the requested 'datalen' if the socket would block. Returns
132630b80fd5SDaniel P. Berrange  * 0 on I/O error, and disconnects the client socket.
13273e230dd2SCorentin Chary  */
132830b80fd5SDaniel P. Berrange size_t vnc_client_write_buf(VncState *vs, const uint8_t *data, size_t datalen)
13293e230dd2SCorentin Chary {
133004d2529dSDaniel P. Berrange     Error *err = NULL;
1331fdd1ab6aSDaniel P. Berrange     ssize_t ret;
133204d2529dSDaniel P. Berrange     ret = qio_channel_write(
133304d2529dSDaniel P. Berrange         vs->ioc, (const char *)data, datalen, &err);
13343e230dd2SCorentin Chary     VNC_DEBUG("Wrote wire %p %zd -> %ld\n", data, datalen, ret);
133504d2529dSDaniel P. Berrange     return vnc_client_io_error(vs, ret, &err);
13363e230dd2SCorentin Chary }
13373e230dd2SCorentin Chary 
13383e230dd2SCorentin Chary 
13393e230dd2SCorentin Chary /*
13403e230dd2SCorentin Chary  * Called to write buffered data to the client socket, when not
13413e230dd2SCorentin Chary  * using any SASL SSF encryption layers. Will write as much data
13423e230dd2SCorentin Chary  * as possible without blocking. If all buffered data is written,
13433e230dd2SCorentin Chary  * will switch the FD poll() handler back to read monitoring.
13443e230dd2SCorentin Chary  *
13453e230dd2SCorentin Chary  * Returns the number of bytes written, which may be less than
13463e230dd2SCorentin Chary  * the buffered output data if the socket would block.  Returns
134730b80fd5SDaniel P. Berrange  * 0 on I/O error, and disconnects the client socket.
13483e230dd2SCorentin Chary  */
134930b80fd5SDaniel P. Berrange static size_t vnc_client_write_plain(VncState *vs)
13503e230dd2SCorentin Chary {
13516aa22a29SDaniel P. Berrange     size_t offset;
135230b80fd5SDaniel P. Berrange     size_t ret;
13533e230dd2SCorentin Chary 
13543e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL
13553e230dd2SCorentin Chary     VNC_DEBUG("Write Plain: Pending output %p size %zd offset %zd. Wait SSF %d\n",
13563e230dd2SCorentin Chary               vs->output.buffer, vs->output.capacity, vs->output.offset,
13573e230dd2SCorentin Chary               vs->sasl.waitWriteSSF);
13583e230dd2SCorentin Chary 
13593e230dd2SCorentin Chary     if (vs->sasl.conn &&
13603e230dd2SCorentin Chary         vs->sasl.runSSF &&
13613e230dd2SCorentin Chary         vs->sasl.waitWriteSSF) {
13623e230dd2SCorentin Chary         ret = vnc_client_write_buf(vs, vs->output.buffer, vs->sasl.waitWriteSSF);
13633e230dd2SCorentin Chary         if (ret)
13643e230dd2SCorentin Chary             vs->sasl.waitWriteSSF -= ret;
13653e230dd2SCorentin Chary     } else
13663e230dd2SCorentin Chary #endif /* CONFIG_VNC_SASL */
13673e230dd2SCorentin Chary         ret = vnc_client_write_buf(vs, vs->output.buffer, vs->output.offset);
13683e230dd2SCorentin Chary     if (!ret)
13693e230dd2SCorentin Chary         return 0;
13703e230dd2SCorentin Chary 
1371ada8d2e4SDaniel P. Berrange     if (ret >= vs->force_update_offset) {
13726aa22a29SDaniel P. Berrange         if (vs->force_update_offset != 0) {
13736aa22a29SDaniel P. Berrange             trace_vnc_client_unthrottle_forced(vs, vs->ioc);
13746aa22a29SDaniel P. Berrange         }
1375ada8d2e4SDaniel P. Berrange         vs->force_update_offset = 0;
1376ada8d2e4SDaniel P. Berrange     } else {
1377ada8d2e4SDaniel P. Berrange         vs->force_update_offset -= ret;
1378ada8d2e4SDaniel P. Berrange     }
13796aa22a29SDaniel P. Berrange     offset = vs->output.offset;
138032ed2680STim Hardeck     buffer_advance(&vs->output, ret);
13816aa22a29SDaniel P. Berrange     if (offset >= vs->throttle_output_offset &&
13826aa22a29SDaniel P. Berrange         vs->output.offset < vs->throttle_output_offset) {
13836aa22a29SDaniel P. Berrange         trace_vnc_client_unthrottle_incremental(vs, vs->ioc, vs->output.offset);
13846aa22a29SDaniel P. Berrange     }
13853e230dd2SCorentin Chary 
13863e230dd2SCorentin Chary     if (vs->output.offset == 0) {
138704d2529dSDaniel P. Berrange         if (vs->ioc_tag) {
138804d2529dSDaniel P. Berrange             g_source_remove(vs->ioc_tag);
138904d2529dSDaniel P. Berrange         }
139004d2529dSDaniel P. Berrange         vs->ioc_tag = qio_channel_add_watch(
139104d2529dSDaniel P. Berrange             vs->ioc, G_IO_IN, vnc_client_io, vs, NULL);
13923e230dd2SCorentin Chary     }
13933e230dd2SCorentin Chary 
13943e230dd2SCorentin Chary     return ret;
13953e230dd2SCorentin Chary }
13963e230dd2SCorentin Chary 
13973e230dd2SCorentin Chary 
13983e230dd2SCorentin Chary /*
13993e230dd2SCorentin Chary  * First function called whenever there is data to be written to
14003e230dd2SCorentin Chary  * the client socket. Will delegate actual work according to whether
14013e230dd2SCorentin Chary  * SASL SSF layers are enabled (thus requiring encryption calls)
14023e230dd2SCorentin Chary  */
140304d2529dSDaniel P. Berrange static void vnc_client_write_locked(VncState *vs)
14043e230dd2SCorentin Chary {
14053e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL
14063e230dd2SCorentin Chary     if (vs->sasl.conn &&
14073e230dd2SCorentin Chary         vs->sasl.runSSF &&
14083e230dd2SCorentin Chary         !vs->sasl.waitWriteSSF) {
14093e230dd2SCorentin Chary         vnc_client_write_sasl(vs);
14103e230dd2SCorentin Chary     } else
14113e230dd2SCorentin Chary #endif /* CONFIG_VNC_SASL */
14127536ee4bSTim Hardeck     {
14133e230dd2SCorentin Chary         vnc_client_write_plain(vs);
14143e230dd2SCorentin Chary     }
14157536ee4bSTim Hardeck }
14163e230dd2SCorentin Chary 
141704d2529dSDaniel P. Berrange static void vnc_client_write(VncState *vs)
1418bd023f95SCorentin Chary {
1419f31f9c10SGerd Hoffmann     assert(vs->magic == VNC_MAGIC);
1420bd023f95SCorentin Chary     vnc_lock_output(vs);
1421d5f04223SDaniel P. Berrange     if (vs->output.offset) {
142204d2529dSDaniel P. Berrange         vnc_client_write_locked(vs);
142304d2529dSDaniel P. Berrange     } else if (vs->ioc != NULL) {
142404d2529dSDaniel P. Berrange         if (vs->ioc_tag) {
142504d2529dSDaniel P. Berrange             g_source_remove(vs->ioc_tag);
142604d2529dSDaniel P. Berrange         }
142704d2529dSDaniel P. Berrange         vs->ioc_tag = qio_channel_add_watch(
142804d2529dSDaniel P. Berrange             vs->ioc, G_IO_IN, vnc_client_io, vs, NULL);
1429bd023f95SCorentin Chary     }
1430bd023f95SCorentin Chary     vnc_unlock_output(vs);
1431bd023f95SCorentin Chary }
1432bd023f95SCorentin Chary 
14333e230dd2SCorentin Chary void vnc_read_when(VncState *vs, VncReadEvent *func, size_t expecting)
14343e230dd2SCorentin Chary {
14353e230dd2SCorentin Chary     vs->read_handler = func;
14363e230dd2SCorentin Chary     vs->read_handler_expect = expecting;
14373e230dd2SCorentin Chary }
14383e230dd2SCorentin Chary 
14393e230dd2SCorentin Chary 
14403e230dd2SCorentin Chary /*
14413e230dd2SCorentin Chary  * Called to read a chunk of data from the client socket. The data may
14423e230dd2SCorentin Chary  * be the raw data, or may need to be further decoded by SASL.
14433e230dd2SCorentin Chary  * The data will be read either straight from to the socket, or
14443e230dd2SCorentin Chary  * read via the GNUTLS wrappers, if TLS/SSL encryption is enabled
14453e230dd2SCorentin Chary  *
14463e230dd2SCorentin Chary  * NB, it is theoretically possible to have 2 layers of encryption,
14473e230dd2SCorentin Chary  * both SASL, and this TLS layer. It is highly unlikely in practice
14483e230dd2SCorentin Chary  * though, since SASL encryption will typically be a no-op if TLS
14493e230dd2SCorentin Chary  * is active
14503e230dd2SCorentin Chary  *
14513e230dd2SCorentin Chary  * Returns the number of bytes read, which may be less than
14523e230dd2SCorentin Chary  * the requested 'datalen' if the socket would block. Returns
145330b80fd5SDaniel P. Berrange  * 0 on I/O error or EOF, and disconnects the client socket.
14543e230dd2SCorentin Chary  */
145530b80fd5SDaniel P. Berrange size_t vnc_client_read_buf(VncState *vs, uint8_t *data, size_t datalen)
14563e230dd2SCorentin Chary {
1457fdd1ab6aSDaniel P. Berrange     ssize_t ret;
145804d2529dSDaniel P. Berrange     Error *err = NULL;
145904d2529dSDaniel P. Berrange     ret = qio_channel_read(
146004d2529dSDaniel P. Berrange         vs->ioc, (char *)data, datalen, &err);
14613e230dd2SCorentin Chary     VNC_DEBUG("Read wire %p %zd -> %ld\n", data, datalen, ret);
146204d2529dSDaniel P. Berrange     return vnc_client_io_error(vs, ret, &err);
14633e230dd2SCorentin Chary }
14643e230dd2SCorentin Chary 
14653e230dd2SCorentin Chary 
14663e230dd2SCorentin Chary /*
14673e230dd2SCorentin Chary  * Called to read data from the client socket to the input buffer,
14683e230dd2SCorentin Chary  * when not using any SASL SSF encryption layers. Will read as much
14693e230dd2SCorentin Chary  * data as possible without blocking.
14703e230dd2SCorentin Chary  *
147130b80fd5SDaniel P. Berrange  * Returns the number of bytes read, which may be less than
147230b80fd5SDaniel P. Berrange  * the requested 'datalen' if the socket would block. Returns
147330b80fd5SDaniel P. Berrange  * 0 on I/O error or EOF, and disconnects the client socket.
14743e230dd2SCorentin Chary  */
147530b80fd5SDaniel P. Berrange static size_t vnc_client_read_plain(VncState *vs)
14763e230dd2SCorentin Chary {
147730b80fd5SDaniel P. Berrange     size_t ret;
14783e230dd2SCorentin Chary     VNC_DEBUG("Read plain %p size %zd offset %zd\n",
14793e230dd2SCorentin Chary               vs->input.buffer, vs->input.capacity, vs->input.offset);
14803e230dd2SCorentin Chary     buffer_reserve(&vs->input, 4096);
14813e230dd2SCorentin Chary     ret = vnc_client_read_buf(vs, buffer_end(&vs->input), 4096);
14823e230dd2SCorentin Chary     if (!ret)
14833e230dd2SCorentin Chary         return 0;
14843e230dd2SCorentin Chary     vs->input.offset += ret;
14853e230dd2SCorentin Chary     return ret;
14863e230dd2SCorentin Chary }
14873e230dd2SCorentin Chary 
1488175b2a6eSCorentin Chary static void vnc_jobs_bh(void *opaque)
1489175b2a6eSCorentin Chary {
1490175b2a6eSCorentin Chary     VncState *vs = opaque;
1491175b2a6eSCorentin Chary 
1492f31f9c10SGerd Hoffmann     assert(vs->magic == VNC_MAGIC);
1493175b2a6eSCorentin Chary     vnc_jobs_consume_buffer(vs);
1494175b2a6eSCorentin Chary }
14953e230dd2SCorentin Chary 
14963e230dd2SCorentin Chary /*
14973e230dd2SCorentin Chary  * First function called whenever there is more data to be read from
14983e230dd2SCorentin Chary  * the client socket. Will delegate actual work according to whether
14993e230dd2SCorentin Chary  * SASL SSF layers are enabled (thus requiring decryption calls)
1500ea697449SDaniel P. Berrange  * Returns 0 on success, -1 if client disconnected
15013e230dd2SCorentin Chary  */
1502ea697449SDaniel P. Berrange static int vnc_client_read(VncState *vs)
15033e230dd2SCorentin Chary {
150430b80fd5SDaniel P. Berrange     size_t ret;
15053e230dd2SCorentin Chary 
15063e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL
15073e230dd2SCorentin Chary     if (vs->sasl.conn && vs->sasl.runSSF)
15083e230dd2SCorentin Chary         ret = vnc_client_read_sasl(vs);
15093e230dd2SCorentin Chary     else
15103e230dd2SCorentin Chary #endif /* CONFIG_VNC_SASL */
15113e230dd2SCorentin Chary         ret = vnc_client_read_plain(vs);
15123e230dd2SCorentin Chary     if (!ret) {
151304d2529dSDaniel P. Berrange         if (vs->disconnecting) {
15143e230dd2SCorentin Chary             vnc_disconnect_finish(vs);
1515ea697449SDaniel P. Berrange             return -1;
151604d2529dSDaniel P. Berrange         }
1517ea697449SDaniel P. Berrange         return 0;
15183e230dd2SCorentin Chary     }
15193e230dd2SCorentin Chary 
15203e230dd2SCorentin Chary     while (vs->read_handler && vs->input.offset >= vs->read_handler_expect) {
15213e230dd2SCorentin Chary         size_t len = vs->read_handler_expect;
15223e230dd2SCorentin Chary         int ret;
15233e230dd2SCorentin Chary 
15243e230dd2SCorentin Chary         ret = vs->read_handler(vs, vs->input.buffer, len);
152504d2529dSDaniel P. Berrange         if (vs->disconnecting) {
15263e230dd2SCorentin Chary             vnc_disconnect_finish(vs);
1527ea697449SDaniel P. Berrange             return -1;
15283e230dd2SCorentin Chary         }
15293e230dd2SCorentin Chary 
15303e230dd2SCorentin Chary         if (!ret) {
153132ed2680STim Hardeck             buffer_advance(&vs->input, len);
15323e230dd2SCorentin Chary         } else {
15333e230dd2SCorentin Chary             vs->read_handler_expect = ret;
15343e230dd2SCorentin Chary         }
15353e230dd2SCorentin Chary     }
1536ea697449SDaniel P. Berrange     return 0;
15373e230dd2SCorentin Chary }
15383e230dd2SCorentin Chary 
153904d2529dSDaniel P. Berrange gboolean vnc_client_io(QIOChannel *ioc G_GNUC_UNUSED,
154004d2529dSDaniel P. Berrange                        GIOCondition condition, void *opaque)
154104d2529dSDaniel P. Berrange {
154204d2529dSDaniel P. Berrange     VncState *vs = opaque;
1543f31f9c10SGerd Hoffmann 
1544f31f9c10SGerd Hoffmann     assert(vs->magic == VNC_MAGIC);
154504d2529dSDaniel P. Berrange     if (condition & G_IO_IN) {
1546ea697449SDaniel P. Berrange         if (vnc_client_read(vs) < 0) {
15471bc3117aSGerd Hoffmann             /* vs is free()ed here */
15481bc3117aSGerd Hoffmann             return TRUE;
1549ea697449SDaniel P. Berrange         }
155004d2529dSDaniel P. Berrange     }
155104d2529dSDaniel P. Berrange     if (condition & G_IO_OUT) {
155204d2529dSDaniel P. Berrange         vnc_client_write(vs);
155304d2529dSDaniel P. Berrange     }
15541bc3117aSGerd Hoffmann 
1555d49b87f0SKlim Kireev     if (vs->disconnecting) {
1556d49b87f0SKlim Kireev         if (vs->ioc_tag != 0) {
1557d49b87f0SKlim Kireev             g_source_remove(vs->ioc_tag);
1558d49b87f0SKlim Kireev         }
1559d49b87f0SKlim Kireev         vs->ioc_tag = 0;
1560d49b87f0SKlim Kireev     }
156104d2529dSDaniel P. Berrange     return TRUE;
156204d2529dSDaniel P. Berrange }
156304d2529dSDaniel P. Berrange 
156404d2529dSDaniel P. Berrange 
1565f887cf16SDaniel P. Berrange /*
1566f887cf16SDaniel P. Berrange  * Scale factor to apply to vs->throttle_output_offset when checking for
1567f887cf16SDaniel P. Berrange  * hard limit. Worst case normal usage could be x2, if we have a complete
1568f887cf16SDaniel P. Berrange  * incremental update and complete forced update in the output buffer.
1569f887cf16SDaniel P. Berrange  * So x3 should be good enough, but we pick x5 to be conservative and thus
1570f887cf16SDaniel P. Berrange  * (hopefully) never trigger incorrectly.
1571f887cf16SDaniel P. Berrange  */
1572f887cf16SDaniel P. Berrange #define VNC_THROTTLE_OUTPUT_LIMIT_SCALE 5
1573f887cf16SDaniel P. Berrange 
15743e230dd2SCorentin Chary void vnc_write(VncState *vs, const void *data, size_t len)
15753e230dd2SCorentin Chary {
1576f31f9c10SGerd Hoffmann     assert(vs->magic == VNC_MAGIC);
1577f887cf16SDaniel P. Berrange     if (vs->disconnecting) {
1578f887cf16SDaniel P. Berrange         return;
1579f887cf16SDaniel P. Berrange     }
1580f887cf16SDaniel P. Berrange     /* Protection against malicious client/guest to prevent our output
1581f887cf16SDaniel P. Berrange      * buffer growing without bound if client stops reading data. This
1582f887cf16SDaniel P. Berrange      * should rarely trigger, because we have earlier throttling code
1583f887cf16SDaniel P. Berrange      * which stops issuing framebuffer updates and drops audio data
1584f887cf16SDaniel P. Berrange      * if the throttle_output_offset value is exceeded. So we only reach
1585f887cf16SDaniel P. Berrange      * this higher level if a huge number of pseudo-encodings get
1586f887cf16SDaniel P. Berrange      * triggered while data can't be sent on the socket.
1587f887cf16SDaniel P. Berrange      *
1588f887cf16SDaniel P. Berrange      * NB throttle_output_offset can be zero during early protocol
1589f887cf16SDaniel P. Berrange      * handshake, or from the job thread's VncState clone
1590f887cf16SDaniel P. Berrange      */
1591f887cf16SDaniel P. Berrange     if (vs->throttle_output_offset != 0 &&
1592dffa1de0SDaniel P. Berrangé         (vs->output.offset / VNC_THROTTLE_OUTPUT_LIMIT_SCALE) >
1593dffa1de0SDaniel P. Berrangé         vs->throttle_output_offset) {
15946aa22a29SDaniel P. Berrange         trace_vnc_client_output_limit(vs, vs->ioc, vs->output.offset,
15956aa22a29SDaniel P. Berrange                                       vs->throttle_output_offset);
1596f887cf16SDaniel P. Berrange         vnc_disconnect_start(vs);
1597f887cf16SDaniel P. Berrange         return;
1598f887cf16SDaniel P. Berrange     }
15993e230dd2SCorentin Chary     buffer_reserve(&vs->output, len);
16003e230dd2SCorentin Chary 
160104d2529dSDaniel P. Berrange     if (vs->ioc != NULL && buffer_empty(&vs->output)) {
160204d2529dSDaniel P. Berrange         if (vs->ioc_tag) {
160304d2529dSDaniel P. Berrange             g_source_remove(vs->ioc_tag);
160404d2529dSDaniel P. Berrange         }
160504d2529dSDaniel P. Berrange         vs->ioc_tag = qio_channel_add_watch(
160604d2529dSDaniel P. Berrange             vs->ioc, G_IO_IN | G_IO_OUT, vnc_client_io, vs, NULL);
16073e230dd2SCorentin Chary     }
16083e230dd2SCorentin Chary 
16093e230dd2SCorentin Chary     buffer_append(&vs->output, data, len);
16103e230dd2SCorentin Chary }
16113e230dd2SCorentin Chary 
16123e230dd2SCorentin Chary void vnc_write_s32(VncState *vs, int32_t value)
16133e230dd2SCorentin Chary {
16143e230dd2SCorentin Chary     vnc_write_u32(vs, *(uint32_t *)&value);
16153e230dd2SCorentin Chary }
16163e230dd2SCorentin Chary 
16173e230dd2SCorentin Chary void vnc_write_u32(VncState *vs, uint32_t value)
16183e230dd2SCorentin Chary {
16193e230dd2SCorentin Chary     uint8_t buf[4];
16203e230dd2SCorentin Chary 
16213e230dd2SCorentin Chary     buf[0] = (value >> 24) & 0xFF;
16223e230dd2SCorentin Chary     buf[1] = (value >> 16) & 0xFF;
16233e230dd2SCorentin Chary     buf[2] = (value >>  8) & 0xFF;
16243e230dd2SCorentin Chary     buf[3] = value & 0xFF;
16253e230dd2SCorentin Chary 
16263e230dd2SCorentin Chary     vnc_write(vs, buf, 4);
16273e230dd2SCorentin Chary }
16283e230dd2SCorentin Chary 
16293e230dd2SCorentin Chary void vnc_write_u16(VncState *vs, uint16_t value)
16303e230dd2SCorentin Chary {
16313e230dd2SCorentin Chary     uint8_t buf[2];
16323e230dd2SCorentin Chary 
16333e230dd2SCorentin Chary     buf[0] = (value >> 8) & 0xFF;
16343e230dd2SCorentin Chary     buf[1] = value & 0xFF;
16353e230dd2SCorentin Chary 
16363e230dd2SCorentin Chary     vnc_write(vs, buf, 2);
16373e230dd2SCorentin Chary }
16383e230dd2SCorentin Chary 
16393e230dd2SCorentin Chary void vnc_write_u8(VncState *vs, uint8_t value)
16403e230dd2SCorentin Chary {
16413e230dd2SCorentin Chary     vnc_write(vs, (char *)&value, 1);
16423e230dd2SCorentin Chary }
16433e230dd2SCorentin Chary 
16443e230dd2SCorentin Chary void vnc_flush(VncState *vs)
16453e230dd2SCorentin Chary {
1646bd023f95SCorentin Chary     vnc_lock_output(vs);
1647d5f04223SDaniel P. Berrange     if (vs->ioc != NULL && vs->output.offset) {
1648bd023f95SCorentin Chary         vnc_client_write_locked(vs);
1649bd023f95SCorentin Chary     }
1650d49b87f0SKlim Kireev     if (vs->disconnecting) {
1651d49b87f0SKlim Kireev         if (vs->ioc_tag != 0) {
1652d49b87f0SKlim Kireev             g_source_remove(vs->ioc_tag);
1653d49b87f0SKlim Kireev         }
1654d49b87f0SKlim Kireev         vs->ioc_tag = 0;
1655d49b87f0SKlim Kireev     }
1656bd023f95SCorentin Chary     vnc_unlock_output(vs);
16573e230dd2SCorentin Chary }
16583e230dd2SCorentin Chary 
165971a8cdecSBlue Swirl static uint8_t read_u8(uint8_t *data, size_t offset)
16603e230dd2SCorentin Chary {
16613e230dd2SCorentin Chary     return data[offset];
16623e230dd2SCorentin Chary }
16633e230dd2SCorentin Chary 
166471a8cdecSBlue Swirl static uint16_t read_u16(uint8_t *data, size_t offset)
16653e230dd2SCorentin Chary {
16663e230dd2SCorentin Chary     return ((data[offset] & 0xFF) << 8) | (data[offset + 1] & 0xFF);
16673e230dd2SCorentin Chary }
16683e230dd2SCorentin Chary 
166971a8cdecSBlue Swirl static int32_t read_s32(uint8_t *data, size_t offset)
16703e230dd2SCorentin Chary {
16713e230dd2SCorentin Chary     return (int32_t)((data[offset] << 24) | (data[offset + 1] << 16) |
16723e230dd2SCorentin Chary                      (data[offset + 2] << 8) | data[offset + 3]);
16733e230dd2SCorentin Chary }
16743e230dd2SCorentin Chary 
16753e230dd2SCorentin Chary uint32_t read_u32(uint8_t *data, size_t offset)
16763e230dd2SCorentin Chary {
16773e230dd2SCorentin Chary     return ((data[offset] << 24) | (data[offset + 1] << 16) |
16783e230dd2SCorentin Chary             (data[offset + 2] << 8) | data[offset + 3]);
16793e230dd2SCorentin Chary }
16803e230dd2SCorentin Chary 
16813e230dd2SCorentin Chary static void client_cut_text(VncState *vs, size_t len, uint8_t *text)
16823e230dd2SCorentin Chary {
16833e230dd2SCorentin Chary }
16843e230dd2SCorentin Chary 
16859e8dd451SJan Kiszka static void check_pointer_type_change(Notifier *notifier, void *data)
16863e230dd2SCorentin Chary {
16873e230dd2SCorentin Chary     VncState *vs = container_of(notifier, VncState, mouse_mode_notifier);
168814768ebaSGerd Hoffmann     int absolute = qemu_input_is_absolute();
16893e230dd2SCorentin Chary 
16903e230dd2SCorentin Chary     if (vnc_has_feature(vs, VNC_FEATURE_POINTER_TYPE_CHANGE) && vs->absolute != absolute) {
1691bd023f95SCorentin Chary         vnc_lock_output(vs);
16923e230dd2SCorentin Chary         vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE);
16933e230dd2SCorentin Chary         vnc_write_u8(vs, 0);
16943e230dd2SCorentin Chary         vnc_write_u16(vs, 1);
16953e230dd2SCorentin Chary         vnc_framebuffer_update(vs, absolute, 0,
1696bea60dd7SPeter Lieven                                pixman_image_get_width(vs->vd->server),
1697bea60dd7SPeter Lieven                                pixman_image_get_height(vs->vd->server),
16983e230dd2SCorentin Chary                                VNC_ENCODING_POINTER_TYPE_CHANGE);
1699bd023f95SCorentin Chary         vnc_unlock_output(vs);
17003e230dd2SCorentin Chary         vnc_flush(vs);
17013e230dd2SCorentin Chary     }
17023e230dd2SCorentin Chary     vs->absolute = absolute;
17033e230dd2SCorentin Chary }
17043e230dd2SCorentin Chary 
17053e230dd2SCorentin Chary static void pointer_event(VncState *vs, int button_mask, int x, int y)
17063e230dd2SCorentin Chary {
17077fb1cf16SEric Blake     static uint32_t bmap[INPUT_BUTTON__MAX] = {
170814768ebaSGerd Hoffmann         [INPUT_BUTTON_LEFT]       = 0x01,
170914768ebaSGerd Hoffmann         [INPUT_BUTTON_MIDDLE]     = 0x02,
171014768ebaSGerd Hoffmann         [INPUT_BUTTON_RIGHT]      = 0x04,
1711f22d0af0SGerd Hoffmann         [INPUT_BUTTON_WHEEL_UP]   = 0x08,
1712f22d0af0SGerd Hoffmann         [INPUT_BUTTON_WHEEL_DOWN] = 0x10,
171314768ebaSGerd Hoffmann     };
171414768ebaSGerd Hoffmann     QemuConsole *con = vs->vd->dcl.con;
1715bea60dd7SPeter Lieven     int width = pixman_image_get_width(vs->vd->server);
1716bea60dd7SPeter Lieven     int height = pixman_image_get_height(vs->vd->server);
17173e230dd2SCorentin Chary 
171814768ebaSGerd Hoffmann     if (vs->last_bmask != button_mask) {
171914768ebaSGerd Hoffmann         qemu_input_update_buttons(con, bmap, vs->last_bmask, button_mask);
172014768ebaSGerd Hoffmann         vs->last_bmask = button_mask;
172114768ebaSGerd Hoffmann     }
17223e230dd2SCorentin Chary 
17233e230dd2SCorentin Chary     if (vs->absolute) {
17249cfa7ab9SPhilippe Voinov         qemu_input_queue_abs(con, INPUT_AXIS_X, x, 0, width);
17259cfa7ab9SPhilippe Voinov         qemu_input_queue_abs(con, INPUT_AXIS_Y, y, 0, height);
17263e230dd2SCorentin Chary     } else if (vnc_has_feature(vs, VNC_FEATURE_POINTER_TYPE_CHANGE)) {
172714768ebaSGerd Hoffmann         qemu_input_queue_rel(con, INPUT_AXIS_X, x - 0x7FFF);
172814768ebaSGerd Hoffmann         qemu_input_queue_rel(con, INPUT_AXIS_Y, y - 0x7FFF);
17293e230dd2SCorentin Chary     } else {
173014768ebaSGerd Hoffmann         if (vs->last_x != -1) {
173114768ebaSGerd Hoffmann             qemu_input_queue_rel(con, INPUT_AXIS_X, x - vs->last_x);
173214768ebaSGerd Hoffmann             qemu_input_queue_rel(con, INPUT_AXIS_Y, y - vs->last_y);
173314768ebaSGerd Hoffmann         }
17343e230dd2SCorentin Chary         vs->last_x = x;
17353e230dd2SCorentin Chary         vs->last_y = y;
17363e230dd2SCorentin Chary     }
173714768ebaSGerd Hoffmann     qemu_input_event_sync();
17383e230dd2SCorentin Chary }
17393e230dd2SCorentin Chary 
17403e230dd2SCorentin Chary static void reset_keys(VncState *vs)
17413e230dd2SCorentin Chary {
17423e230dd2SCorentin Chary     int i;
17433e230dd2SCorentin Chary     for(i = 0; i < 256; i++) {
17443e230dd2SCorentin Chary         if (vs->modifiers_state[i]) {
17458d447d10SGerd Hoffmann             qemu_input_event_send_key_number(vs->vd->dcl.con, i, false);
1746c5ce8333SGerd Hoffmann             qemu_input_event_send_key_delay(vs->vd->key_delay_ms);
17473e230dd2SCorentin Chary             vs->modifiers_state[i] = 0;
17483e230dd2SCorentin Chary         }
17493e230dd2SCorentin Chary     }
17503e230dd2SCorentin Chary }
17513e230dd2SCorentin Chary 
17523e230dd2SCorentin Chary static void press_key(VncState *vs, int keysym)
17533e230dd2SCorentin Chary {
1754abb4f2c9SGerd Hoffmann     int keycode = keysym2scancode(vs->vd->kbd_layout, keysym,
1755abb4f2c9SGerd Hoffmann                                   false, false, false) & SCANCODE_KEYMASK;
17568d447d10SGerd Hoffmann     qemu_input_event_send_key_number(vs->vd->dcl.con, keycode, true);
1757c5ce8333SGerd Hoffmann     qemu_input_event_send_key_delay(vs->vd->key_delay_ms);
17588d447d10SGerd Hoffmann     qemu_input_event_send_key_number(vs->vd->dcl.con, keycode, false);
1759c5ce8333SGerd Hoffmann     qemu_input_event_send_key_delay(vs->vd->key_delay_ms);
17603e230dd2SCorentin Chary }
17613e230dd2SCorentin Chary 
1762ab99e5c1SLei Li static void vnc_led_state_change(VncState *vs)
1763ab99e5c1SLei Li {
1764ab99e5c1SLei Li     if (!vnc_has_feature(vs, VNC_FEATURE_LED_STATE)) {
1765ab99e5c1SLei Li         return;
1766ab99e5c1SLei Li     }
1767ab99e5c1SLei Li 
1768ab99e5c1SLei Li     vnc_lock_output(vs);
1769ab99e5c1SLei Li     vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE);
1770ab99e5c1SLei Li     vnc_write_u8(vs, 0);
1771ab99e5c1SLei Li     vnc_write_u16(vs, 1);
1772ab99e5c1SLei Li     vnc_framebuffer_update(vs, 0, 0, 1, 1, VNC_ENCODING_LED_STATE);
1773a54f0d2bSPierre Ossman     vnc_write_u8(vs, vs->vd->ledstate);
1774ab99e5c1SLei Li     vnc_unlock_output(vs);
1775ab99e5c1SLei Li     vnc_flush(vs);
1776ab99e5c1SLei Li }
1777ab99e5c1SLei Li 
17783e230dd2SCorentin Chary static void kbd_leds(void *opaque, int ledstate)
17793e230dd2SCorentin Chary {
1780a54f0d2bSPierre Ossman     VncDisplay *vd = opaque;
1781a54f0d2bSPierre Ossman     VncState *client;
17823e230dd2SCorentin Chary 
178340066175SGerd Hoffmann     trace_vnc_key_guest_leds((ledstate & QEMU_CAPS_LOCK_LED),
178440066175SGerd Hoffmann                              (ledstate & QEMU_NUM_LOCK_LED),
178540066175SGerd Hoffmann                              (ledstate & QEMU_SCROLL_LOCK_LED));
178640066175SGerd Hoffmann 
1787a54f0d2bSPierre Ossman     if (ledstate == vd->ledstate) {
1788a54f0d2bSPierre Ossman         return;
178996f3d174SLei Li     }
1790ab99e5c1SLei Li 
1791a54f0d2bSPierre Ossman     vd->ledstate = ledstate;
1792a54f0d2bSPierre Ossman 
1793a54f0d2bSPierre Ossman     QTAILQ_FOREACH(client, &vd->clients, next) {
1794a54f0d2bSPierre Ossman         vnc_led_state_change(client);
1795ab99e5c1SLei Li     }
17963e230dd2SCorentin Chary }
17973e230dd2SCorentin Chary 
17983e230dd2SCorentin Chary static void do_key_event(VncState *vs, int down, int keycode, int sym)
17993e230dd2SCorentin Chary {
18003e230dd2SCorentin Chary     /* QEMU console switch */
18013e230dd2SCorentin Chary     switch(keycode) {
18023e230dd2SCorentin Chary     case 0x2a:                          /* Left Shift */
18033e230dd2SCorentin Chary     case 0x36:                          /* Right Shift */
18043e230dd2SCorentin Chary     case 0x1d:                          /* Left CTRL */
18053e230dd2SCorentin Chary     case 0x9d:                          /* Right CTRL */
18063e230dd2SCorentin Chary     case 0x38:                          /* Left ALT */
18073e230dd2SCorentin Chary     case 0xb8:                          /* Right ALT */
18083e230dd2SCorentin Chary         if (down)
18093e230dd2SCorentin Chary             vs->modifiers_state[keycode] = 1;
18103e230dd2SCorentin Chary         else
18113e230dd2SCorentin Chary             vs->modifiers_state[keycode] = 0;
18123e230dd2SCorentin Chary         break;
18133e230dd2SCorentin Chary     case 0x02 ... 0x0a: /* '1' to '9' keys */
18141d0d59feSGerd Hoffmann         if (vs->vd->dcl.con == NULL &&
18151d0d59feSGerd Hoffmann             down && vs->modifiers_state[0x1d] && vs->modifiers_state[0x38]) {
18163e230dd2SCorentin Chary             /* Reset the modifiers sent to the current console */
18173e230dd2SCorentin Chary             reset_keys(vs);
18183e230dd2SCorentin Chary             console_select(keycode - 0x02);
18193e230dd2SCorentin Chary             return;
18203e230dd2SCorentin Chary         }
18213e230dd2SCorentin Chary         break;
18223e230dd2SCorentin Chary     case 0x3a:                        /* CapsLock */
18233e230dd2SCorentin Chary     case 0x45:                        /* NumLock */
18243e230dd2SCorentin Chary         if (down)
18253e230dd2SCorentin Chary             vs->modifiers_state[keycode] ^= 1;
18263e230dd2SCorentin Chary         break;
18273e230dd2SCorentin Chary     }
18283e230dd2SCorentin Chary 
1829e7b2aaccSLei Li     /* Turn off the lock state sync logic if the client support the led
1830e7b2aaccSLei Li        state extension.
1831e7b2aaccSLei Li     */
18329892088bSGerd Hoffmann     if (down && vs->vd->lock_key_sync &&
1833e7b2aaccSLei Li         !vnc_has_feature(vs, VNC_FEATURE_LED_STATE) &&
18343e230dd2SCorentin Chary         keycode_is_keypad(vs->vd->kbd_layout, keycode)) {
18353e230dd2SCorentin Chary         /* If the numlock state needs to change then simulate an additional
18363e230dd2SCorentin Chary            keypress before sending this one.  This will happen if the user
18373e230dd2SCorentin Chary            toggles numlock away from the VNC window.
18383e230dd2SCorentin Chary         */
18393e230dd2SCorentin Chary         if (keysym_is_numlock(vs->vd->kbd_layout, sym & 0xFFFF)) {
18403e230dd2SCorentin Chary             if (!vs->modifiers_state[0x45]) {
184140066175SGerd Hoffmann                 trace_vnc_key_sync_numlock(true);
18423e230dd2SCorentin Chary                 vs->modifiers_state[0x45] = 1;
18433e230dd2SCorentin Chary                 press_key(vs, 0xff7f);
18443e230dd2SCorentin Chary             }
18453e230dd2SCorentin Chary         } else {
18463e230dd2SCorentin Chary             if (vs->modifiers_state[0x45]) {
184740066175SGerd Hoffmann                 trace_vnc_key_sync_numlock(false);
18483e230dd2SCorentin Chary                 vs->modifiers_state[0x45] = 0;
18493e230dd2SCorentin Chary                 press_key(vs, 0xff7f);
18503e230dd2SCorentin Chary             }
18513e230dd2SCorentin Chary         }
18523e230dd2SCorentin Chary     }
18533e230dd2SCorentin Chary 
18549892088bSGerd Hoffmann     if (down && vs->vd->lock_key_sync &&
1855e7b2aaccSLei Li         !vnc_has_feature(vs, VNC_FEATURE_LED_STATE) &&
18563e230dd2SCorentin Chary         ((sym >= 'A' && sym <= 'Z') || (sym >= 'a' && sym <= 'z'))) {
18573e230dd2SCorentin Chary         /* If the capslock state needs to change then simulate an additional
18583e230dd2SCorentin Chary            keypress before sending this one.  This will happen if the user
18593e230dd2SCorentin Chary            toggles capslock away from the VNC window.
18603e230dd2SCorentin Chary         */
18613e230dd2SCorentin Chary         int uppercase = !!(sym >= 'A' && sym <= 'Z');
18623e230dd2SCorentin Chary         int shift = !!(vs->modifiers_state[0x2a] | vs->modifiers_state[0x36]);
18633e230dd2SCorentin Chary         int capslock = !!(vs->modifiers_state[0x3a]);
18643e230dd2SCorentin Chary         if (capslock) {
18653e230dd2SCorentin Chary             if (uppercase == shift) {
186640066175SGerd Hoffmann                 trace_vnc_key_sync_capslock(false);
18673e230dd2SCorentin Chary                 vs->modifiers_state[0x3a] = 0;
18683e230dd2SCorentin Chary                 press_key(vs, 0xffe5);
18693e230dd2SCorentin Chary             }
18703e230dd2SCorentin Chary         } else {
18713e230dd2SCorentin Chary             if (uppercase != shift) {
187240066175SGerd Hoffmann                 trace_vnc_key_sync_capslock(true);
18733e230dd2SCorentin Chary                 vs->modifiers_state[0x3a] = 1;
18743e230dd2SCorentin Chary                 press_key(vs, 0xffe5);
18753e230dd2SCorentin Chary             }
18763e230dd2SCorentin Chary         }
18773e230dd2SCorentin Chary     }
18783e230dd2SCorentin Chary 
187981c0d5a6SGerd Hoffmann     if (qemu_console_is_graphic(NULL)) {
18808d447d10SGerd Hoffmann         qemu_input_event_send_key_number(vs->vd->dcl.con, keycode, down);
1881c5ce8333SGerd Hoffmann         qemu_input_event_send_key_delay(vs->vd->key_delay_ms);
18823e230dd2SCorentin Chary     } else {
1883e26437c2SGerd Hoffmann         bool numlock = vs->modifiers_state[0x45];
1884e26437c2SGerd Hoffmann         bool control = (vs->modifiers_state[0x1d] ||
1885e26437c2SGerd Hoffmann                         vs->modifiers_state[0x9d]);
18863e230dd2SCorentin Chary         /* QEMU console emulation */
18873e230dd2SCorentin Chary         if (down) {
18883e230dd2SCorentin Chary             switch (keycode) {
18893e230dd2SCorentin Chary             case 0x2a:                          /* Left Shift */
18903e230dd2SCorentin Chary             case 0x36:                          /* Right Shift */
18913e230dd2SCorentin Chary             case 0x1d:                          /* Left CTRL */
18923e230dd2SCorentin Chary             case 0x9d:                          /* Right CTRL */
18933e230dd2SCorentin Chary             case 0x38:                          /* Left ALT */
18943e230dd2SCorentin Chary             case 0xb8:                          /* Right ALT */
18953e230dd2SCorentin Chary                 break;
18963e230dd2SCorentin Chary             case 0xc8:
18973e230dd2SCorentin Chary                 kbd_put_keysym(QEMU_KEY_UP);
18983e230dd2SCorentin Chary                 break;
18993e230dd2SCorentin Chary             case 0xd0:
19003e230dd2SCorentin Chary                 kbd_put_keysym(QEMU_KEY_DOWN);
19013e230dd2SCorentin Chary                 break;
19023e230dd2SCorentin Chary             case 0xcb:
19033e230dd2SCorentin Chary                 kbd_put_keysym(QEMU_KEY_LEFT);
19043e230dd2SCorentin Chary                 break;
19053e230dd2SCorentin Chary             case 0xcd:
19063e230dd2SCorentin Chary                 kbd_put_keysym(QEMU_KEY_RIGHT);
19073e230dd2SCorentin Chary                 break;
19083e230dd2SCorentin Chary             case 0xd3:
19093e230dd2SCorentin Chary                 kbd_put_keysym(QEMU_KEY_DELETE);
19103e230dd2SCorentin Chary                 break;
19113e230dd2SCorentin Chary             case 0xc7:
19123e230dd2SCorentin Chary                 kbd_put_keysym(QEMU_KEY_HOME);
19133e230dd2SCorentin Chary                 break;
19143e230dd2SCorentin Chary             case 0xcf:
19153e230dd2SCorentin Chary                 kbd_put_keysym(QEMU_KEY_END);
19163e230dd2SCorentin Chary                 break;
19173e230dd2SCorentin Chary             case 0xc9:
19183e230dd2SCorentin Chary                 kbd_put_keysym(QEMU_KEY_PAGEUP);
19193e230dd2SCorentin Chary                 break;
19203e230dd2SCorentin Chary             case 0xd1:
19213e230dd2SCorentin Chary                 kbd_put_keysym(QEMU_KEY_PAGEDOWN);
19223e230dd2SCorentin Chary                 break;
19233e230dd2SCorentin Chary 
19243e230dd2SCorentin Chary             case 0x47:
19253e230dd2SCorentin Chary                 kbd_put_keysym(numlock ? '7' : QEMU_KEY_HOME);
19263e230dd2SCorentin Chary                 break;
19273e230dd2SCorentin Chary             case 0x48:
19283e230dd2SCorentin Chary                 kbd_put_keysym(numlock ? '8' : QEMU_KEY_UP);
19293e230dd2SCorentin Chary                 break;
19303e230dd2SCorentin Chary             case 0x49:
19313e230dd2SCorentin Chary                 kbd_put_keysym(numlock ? '9' : QEMU_KEY_PAGEUP);
19323e230dd2SCorentin Chary                 break;
19333e230dd2SCorentin Chary             case 0x4b:
19343e230dd2SCorentin Chary                 kbd_put_keysym(numlock ? '4' : QEMU_KEY_LEFT);
19353e230dd2SCorentin Chary                 break;
19363e230dd2SCorentin Chary             case 0x4c:
19373e230dd2SCorentin Chary                 kbd_put_keysym('5');
19383e230dd2SCorentin Chary                 break;
19393e230dd2SCorentin Chary             case 0x4d:
19403e230dd2SCorentin Chary                 kbd_put_keysym(numlock ? '6' : QEMU_KEY_RIGHT);
19413e230dd2SCorentin Chary                 break;
19423e230dd2SCorentin Chary             case 0x4f:
19433e230dd2SCorentin Chary                 kbd_put_keysym(numlock ? '1' : QEMU_KEY_END);
19443e230dd2SCorentin Chary                 break;
19453e230dd2SCorentin Chary             case 0x50:
19463e230dd2SCorentin Chary                 kbd_put_keysym(numlock ? '2' : QEMU_KEY_DOWN);
19473e230dd2SCorentin Chary                 break;
19483e230dd2SCorentin Chary             case 0x51:
19493e230dd2SCorentin Chary                 kbd_put_keysym(numlock ? '3' : QEMU_KEY_PAGEDOWN);
19503e230dd2SCorentin Chary                 break;
19513e230dd2SCorentin Chary             case 0x52:
19523e230dd2SCorentin Chary                 kbd_put_keysym('0');
19533e230dd2SCorentin Chary                 break;
19543e230dd2SCorentin Chary             case 0x53:
19553e230dd2SCorentin Chary                 kbd_put_keysym(numlock ? '.' : QEMU_KEY_DELETE);
19563e230dd2SCorentin Chary                 break;
19573e230dd2SCorentin Chary 
19583e230dd2SCorentin Chary             case 0xb5:
19593e230dd2SCorentin Chary                 kbd_put_keysym('/');
19603e230dd2SCorentin Chary                 break;
19613e230dd2SCorentin Chary             case 0x37:
19623e230dd2SCorentin Chary                 kbd_put_keysym('*');
19633e230dd2SCorentin Chary                 break;
19643e230dd2SCorentin Chary             case 0x4a:
19653e230dd2SCorentin Chary                 kbd_put_keysym('-');
19663e230dd2SCorentin Chary                 break;
19673e230dd2SCorentin Chary             case 0x4e:
19683e230dd2SCorentin Chary                 kbd_put_keysym('+');
19693e230dd2SCorentin Chary                 break;
19703e230dd2SCorentin Chary             case 0x9c:
19713e230dd2SCorentin Chary                 kbd_put_keysym('\n');
19723e230dd2SCorentin Chary                 break;
19733e230dd2SCorentin Chary 
19743e230dd2SCorentin Chary             default:
1975e26437c2SGerd Hoffmann                 if (control) {
1976e26437c2SGerd Hoffmann                     kbd_put_keysym(sym & 0x1f);
1977e26437c2SGerd Hoffmann                 } else {
19783e230dd2SCorentin Chary                     kbd_put_keysym(sym);
1979e26437c2SGerd Hoffmann                 }
19803e230dd2SCorentin Chary                 break;
19813e230dd2SCorentin Chary             }
19823e230dd2SCorentin Chary         }
19833e230dd2SCorentin Chary     }
19843e230dd2SCorentin Chary }
19853e230dd2SCorentin Chary 
19867bc9318bSGerd Hoffmann static void vnc_release_modifiers(VncState *vs)
19877bc9318bSGerd Hoffmann {
19887bc9318bSGerd Hoffmann     static const int keycodes[] = {
19897bc9318bSGerd Hoffmann         /* shift, control, alt keys, both left & right */
19907bc9318bSGerd Hoffmann         0x2a, 0x36, 0x1d, 0x9d, 0x38, 0xb8,
19917bc9318bSGerd Hoffmann     };
19927bc9318bSGerd Hoffmann     int i, keycode;
19937bc9318bSGerd Hoffmann 
199481c0d5a6SGerd Hoffmann     if (!qemu_console_is_graphic(NULL)) {
19957bc9318bSGerd Hoffmann         return;
19967bc9318bSGerd Hoffmann     }
19977bc9318bSGerd Hoffmann     for (i = 0; i < ARRAY_SIZE(keycodes); i++) {
19987bc9318bSGerd Hoffmann         keycode = keycodes[i];
19997bc9318bSGerd Hoffmann         if (!vs->modifiers_state[keycode]) {
20007bc9318bSGerd Hoffmann             continue;
20017bc9318bSGerd Hoffmann         }
20028d447d10SGerd Hoffmann         qemu_input_event_send_key_number(vs->vd->dcl.con, keycode, false);
2003c5ce8333SGerd Hoffmann         qemu_input_event_send_key_delay(vs->vd->key_delay_ms);
20047bc9318bSGerd Hoffmann     }
20057bc9318bSGerd Hoffmann }
20067bc9318bSGerd Hoffmann 
200740066175SGerd Hoffmann static const char *code2name(int keycode)
200840066175SGerd Hoffmann {
2009977c736fSMarkus Armbruster     return QKeyCode_str(qemu_input_key_number_to_qcode(keycode));
201040066175SGerd Hoffmann }
201140066175SGerd Hoffmann 
20123e230dd2SCorentin Chary static void key_event(VncState *vs, int down, uint32_t sym)
20133e230dd2SCorentin Chary {
2014abb4f2c9SGerd Hoffmann     bool shift = vs->modifiers_state[0x2a] || vs->modifiers_state[0x36];
2015abb4f2c9SGerd Hoffmann     bool altgr = vs->modifiers_state[0xb8];
2016abb4f2c9SGerd Hoffmann     bool ctrl  = vs->modifiers_state[0x1d] || vs->modifiers_state[0x9d];
20173e230dd2SCorentin Chary     int keycode;
20183e230dd2SCorentin Chary     int lsym = sym;
20193e230dd2SCorentin Chary 
202081c0d5a6SGerd Hoffmann     if (lsym >= 'A' && lsym <= 'Z' && qemu_console_is_graphic(NULL)) {
20213e230dd2SCorentin Chary         lsym = lsym - 'A' + 'a';
20223e230dd2SCorentin Chary     }
20233e230dd2SCorentin Chary 
2024abb4f2c9SGerd Hoffmann     keycode = keysym2scancode(vs->vd->kbd_layout, lsym & 0xFFFF,
2025abb4f2c9SGerd Hoffmann                               shift, altgr, ctrl) & SCANCODE_KEYMASK;
202640066175SGerd Hoffmann     trace_vnc_key_event_map(down, sym, keycode, code2name(keycode));
20273e230dd2SCorentin Chary     do_key_event(vs, down, keycode, sym);
20283e230dd2SCorentin Chary }
20293e230dd2SCorentin Chary 
20303e230dd2SCorentin Chary static void ext_key_event(VncState *vs, int down,
20313e230dd2SCorentin Chary                           uint32_t sym, uint16_t keycode)
20323e230dd2SCorentin Chary {
20333e230dd2SCorentin Chary     /* if the user specifies a keyboard layout, always use it */
203440066175SGerd Hoffmann     if (keyboard_layout) {
20353e230dd2SCorentin Chary         key_event(vs, down, sym);
203640066175SGerd Hoffmann     } else {
203740066175SGerd Hoffmann         trace_vnc_key_event_ext(down, sym, keycode, code2name(keycode));
20383e230dd2SCorentin Chary         do_key_event(vs, down, keycode, sym);
20393e230dd2SCorentin Chary     }
204040066175SGerd Hoffmann }
20413e230dd2SCorentin Chary 
20423e230dd2SCorentin Chary static void framebuffer_update_request(VncState *vs, int incremental,
2043bea60dd7SPeter Lieven                                        int x, int y, int w, int h)
20443e230dd2SCorentin Chary {
2045bea60dd7SPeter Lieven     if (incremental) {
2046fef1bbadSDaniel P. Berrange         if (vs->update != VNC_STATE_UPDATE_FORCE) {
2047fef1bbadSDaniel P. Berrange             vs->update = VNC_STATE_UPDATE_INCREMENTAL;
20483e230dd2SCorentin Chary         }
2049fef1bbadSDaniel P. Berrange     } else {
2050fef1bbadSDaniel P. Berrange         vs->update = VNC_STATE_UPDATE_FORCE;
2051f7b3d68cSGerd Hoffmann         vnc_set_area_dirty(vs->dirty, vs->vd, x, y, w, h);
20523e230dd2SCorentin Chary     }
2053fef1bbadSDaniel P. Berrange }
20543e230dd2SCorentin Chary 
20553e230dd2SCorentin Chary static void send_ext_key_event_ack(VncState *vs)
20563e230dd2SCorentin Chary {
2057bd023f95SCorentin Chary     vnc_lock_output(vs);
20583e230dd2SCorentin Chary     vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE);
20593e230dd2SCorentin Chary     vnc_write_u8(vs, 0);
20603e230dd2SCorentin Chary     vnc_write_u16(vs, 1);
2061d39fa6d8SGerd Hoffmann     vnc_framebuffer_update(vs, 0, 0,
2062bea60dd7SPeter Lieven                            pixman_image_get_width(vs->vd->server),
2063bea60dd7SPeter Lieven                            pixman_image_get_height(vs->vd->server),
20643e230dd2SCorentin Chary                            VNC_ENCODING_EXT_KEY_EVENT);
2065bd023f95SCorentin Chary     vnc_unlock_output(vs);
20663e230dd2SCorentin Chary     vnc_flush(vs);
20673e230dd2SCorentin Chary }
20683e230dd2SCorentin Chary 
20693e230dd2SCorentin Chary static void send_ext_audio_ack(VncState *vs)
20703e230dd2SCorentin Chary {
2071bd023f95SCorentin Chary     vnc_lock_output(vs);
20723e230dd2SCorentin Chary     vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE);
20733e230dd2SCorentin Chary     vnc_write_u8(vs, 0);
20743e230dd2SCorentin Chary     vnc_write_u16(vs, 1);
2075d39fa6d8SGerd Hoffmann     vnc_framebuffer_update(vs, 0, 0,
2076bea60dd7SPeter Lieven                            pixman_image_get_width(vs->vd->server),
2077bea60dd7SPeter Lieven                            pixman_image_get_height(vs->vd->server),
20783e230dd2SCorentin Chary                            VNC_ENCODING_AUDIO);
2079bd023f95SCorentin Chary     vnc_unlock_output(vs);
20803e230dd2SCorentin Chary     vnc_flush(vs);
20813e230dd2SCorentin Chary }
20823e230dd2SCorentin Chary 
20833e230dd2SCorentin Chary static void set_encodings(VncState *vs, int32_t *encodings, size_t n_encodings)
20843e230dd2SCorentin Chary {
20853e230dd2SCorentin Chary     int i;
20863e230dd2SCorentin Chary     unsigned int enc = 0;
20873e230dd2SCorentin Chary 
20883e230dd2SCorentin Chary     vs->features = 0;
20893e230dd2SCorentin Chary     vs->vnc_encoding = 0;
2090d1af0e05SCorentin Chary     vs->tight.compression = 9;
2091d1af0e05SCorentin Chary     vs->tight.quality = -1; /* Lossless by default */
20923e230dd2SCorentin Chary     vs->absolute = -1;
20933e230dd2SCorentin Chary 
20943e230dd2SCorentin Chary     /*
20953e230dd2SCorentin Chary      * Start from the end because the encodings are sent in order of preference.
2096e5bed759SDong Xu Wang      * This way the preferred encoding (first encoding defined in the array)
20973e230dd2SCorentin Chary      * will be set at the end of the loop.
20983e230dd2SCorentin Chary      */
20993e230dd2SCorentin Chary     for (i = n_encodings - 1; i >= 0; i--) {
21003e230dd2SCorentin Chary         enc = encodings[i];
21013e230dd2SCorentin Chary         switch (enc) {
21023e230dd2SCorentin Chary         case VNC_ENCODING_RAW:
21033e230dd2SCorentin Chary             vs->vnc_encoding = enc;
21043e230dd2SCorentin Chary             break;
21053e230dd2SCorentin Chary         case VNC_ENCODING_COPYRECT:
21063e230dd2SCorentin Chary             vs->features |= VNC_FEATURE_COPYRECT_MASK;
21073e230dd2SCorentin Chary             break;
21083e230dd2SCorentin Chary         case VNC_ENCODING_HEXTILE:
21093e230dd2SCorentin Chary             vs->features |= VNC_FEATURE_HEXTILE_MASK;
21103e230dd2SCorentin Chary             vs->vnc_encoding = enc;
21113e230dd2SCorentin Chary             break;
21123e230dd2SCorentin Chary         case VNC_ENCODING_TIGHT:
21133e230dd2SCorentin Chary             vs->features |= VNC_FEATURE_TIGHT_MASK;
21143e230dd2SCorentin Chary             vs->vnc_encoding = enc;
21153e230dd2SCorentin Chary             break;
2116fe3e7f2dSJoel Martin #ifdef CONFIG_VNC_PNG
2117efe556adSCorentin Chary         case VNC_ENCODING_TIGHT_PNG:
2118efe556adSCorentin Chary             vs->features |= VNC_FEATURE_TIGHT_PNG_MASK;
2119efe556adSCorentin Chary             vs->vnc_encoding = enc;
2120efe556adSCorentin Chary             break;
2121fe3e7f2dSJoel Martin #endif
21223e230dd2SCorentin Chary         case VNC_ENCODING_ZLIB:
21233e230dd2SCorentin Chary             vs->features |= VNC_FEATURE_ZLIB_MASK;
21243e230dd2SCorentin Chary             vs->vnc_encoding = enc;
21253e230dd2SCorentin Chary             break;
2126148954faSCorentin Chary         case VNC_ENCODING_ZRLE:
2127148954faSCorentin Chary             vs->features |= VNC_FEATURE_ZRLE_MASK;
2128148954faSCorentin Chary             vs->vnc_encoding = enc;
2129148954faSCorentin Chary             break;
2130148954faSCorentin Chary         case VNC_ENCODING_ZYWRLE:
2131148954faSCorentin Chary             vs->features |= VNC_FEATURE_ZYWRLE_MASK;
2132148954faSCorentin Chary             vs->vnc_encoding = enc;
2133148954faSCorentin Chary             break;
21343e230dd2SCorentin Chary         case VNC_ENCODING_DESKTOPRESIZE:
21353e230dd2SCorentin Chary             vs->features |= VNC_FEATURE_RESIZE_MASK;
21363e230dd2SCorentin Chary             break;
21373e230dd2SCorentin Chary         case VNC_ENCODING_POINTER_TYPE_CHANGE:
21383e230dd2SCorentin Chary             vs->features |= VNC_FEATURE_POINTER_TYPE_CHANGE_MASK;
21393e230dd2SCorentin Chary             break;
21403e230dd2SCorentin Chary         case VNC_ENCODING_RICH_CURSOR:
21413e230dd2SCorentin Chary             vs->features |= VNC_FEATURE_RICH_CURSOR_MASK;
214291ec41dcSFrediano Ziglio             if (vs->vd->cursor) {
214391ec41dcSFrediano Ziglio                 vnc_cursor_define(vs);
214491ec41dcSFrediano Ziglio             }
21453e230dd2SCorentin Chary             break;
21463e230dd2SCorentin Chary         case VNC_ENCODING_EXT_KEY_EVENT:
21473e230dd2SCorentin Chary             send_ext_key_event_ack(vs);
21483e230dd2SCorentin Chary             break;
21493e230dd2SCorentin Chary         case VNC_ENCODING_AUDIO:
21503e230dd2SCorentin Chary             send_ext_audio_ack(vs);
21513e230dd2SCorentin Chary             break;
21523e230dd2SCorentin Chary         case VNC_ENCODING_WMVi:
21533e230dd2SCorentin Chary             vs->features |= VNC_FEATURE_WMVI_MASK;
21543e230dd2SCorentin Chary             break;
2155ab99e5c1SLei Li         case VNC_ENCODING_LED_STATE:
2156ab99e5c1SLei Li             vs->features |= VNC_FEATURE_LED_STATE_MASK;
2157ab99e5c1SLei Li             break;
21583e230dd2SCorentin Chary         case VNC_ENCODING_COMPRESSLEVEL0 ... VNC_ENCODING_COMPRESSLEVEL0 + 9:
2159d1af0e05SCorentin Chary             vs->tight.compression = (enc & 0x0F);
21603e230dd2SCorentin Chary             break;
21613e230dd2SCorentin Chary         case VNC_ENCODING_QUALITYLEVEL0 ... VNC_ENCODING_QUALITYLEVEL0 + 9:
2162b31f519eSCorentin Chary             if (vs->vd->lossy) {
2163d1af0e05SCorentin Chary                 vs->tight.quality = (enc & 0x0F);
2164b31f519eSCorentin Chary             }
21653e230dd2SCorentin Chary             break;
21663e230dd2SCorentin Chary         default:
21673e230dd2SCorentin Chary             VNC_DEBUG("Unknown encoding: %d (0x%.8x): %d\n", i, enc, enc);
21683e230dd2SCorentin Chary             break;
21693e230dd2SCorentin Chary         }
21703e230dd2SCorentin Chary     }
21713e230dd2SCorentin Chary     vnc_desktop_resize(vs);
21729e8dd451SJan Kiszka     check_pointer_type_change(&vs->mouse_mode_notifier, NULL);
2173ab99e5c1SLei Li     vnc_led_state_change(vs);
21743e230dd2SCorentin Chary }
21753e230dd2SCorentin Chary 
21763e230dd2SCorentin Chary static void set_pixel_conversion(VncState *vs)
21773e230dd2SCorentin Chary {
21789f64916dSGerd Hoffmann     pixman_format_code_t fmt = qemu_pixman_get_format(&vs->client_pf);
21799f64916dSGerd Hoffmann 
21809f64916dSGerd Hoffmann     if (fmt == VNC_SERVER_FB_FORMAT) {
21813e230dd2SCorentin Chary         vs->write_pixels = vnc_write_pixels_copy;
21823e230dd2SCorentin Chary         vnc_hextile_set_pixel_conversion(vs, 0);
21833e230dd2SCorentin Chary     } else {
21843e230dd2SCorentin Chary         vs->write_pixels = vnc_write_pixels_generic;
21853e230dd2SCorentin Chary         vnc_hextile_set_pixel_conversion(vs, 1);
21863e230dd2SCorentin Chary     }
21873e230dd2SCorentin Chary }
21883e230dd2SCorentin Chary 
21890c426e45SAlexander Graf static void send_color_map(VncState *vs)
21900c426e45SAlexander Graf {
21910c426e45SAlexander Graf     int i;
21920c426e45SAlexander Graf 
21930c426e45SAlexander Graf     vnc_write_u8(vs, VNC_MSG_SERVER_SET_COLOUR_MAP_ENTRIES);
21940c426e45SAlexander Graf     vnc_write_u8(vs,  0);    /* padding     */
21950c426e45SAlexander Graf     vnc_write_u16(vs, 0);    /* first color */
21960c426e45SAlexander Graf     vnc_write_u16(vs, 256);  /* # of colors */
21970c426e45SAlexander Graf 
21980c426e45SAlexander Graf     for (i = 0; i < 256; i++) {
21990c426e45SAlexander Graf         PixelFormat *pf = &vs->client_pf;
22000c426e45SAlexander Graf 
22010c426e45SAlexander Graf         vnc_write_u16(vs, (((i >> pf->rshift) & pf->rmax) << (16 - pf->rbits)));
22020c426e45SAlexander Graf         vnc_write_u16(vs, (((i >> pf->gshift) & pf->gmax) << (16 - pf->gbits)));
22030c426e45SAlexander Graf         vnc_write_u16(vs, (((i >> pf->bshift) & pf->bmax) << (16 - pf->bbits)));
22040c426e45SAlexander Graf     }
22050c426e45SAlexander Graf }
22060c426e45SAlexander Graf 
2207ec9fb41aSGerd Hoffmann static void set_pixel_format(VncState *vs, int bits_per_pixel,
22083e230dd2SCorentin Chary                              int big_endian_flag, int true_color_flag,
22093e230dd2SCorentin Chary                              int red_max, int green_max, int blue_max,
22103e230dd2SCorentin Chary                              int red_shift, int green_shift, int blue_shift)
22113e230dd2SCorentin Chary {
22123e230dd2SCorentin Chary     if (!true_color_flag) {
22130c426e45SAlexander Graf         /* Expose a reasonable default 256 color map */
22140c426e45SAlexander Graf         bits_per_pixel = 8;
22150c426e45SAlexander Graf         red_max = 7;
22160c426e45SAlexander Graf         green_max = 7;
22170c426e45SAlexander Graf         blue_max = 3;
22180c426e45SAlexander Graf         red_shift = 0;
22190c426e45SAlexander Graf         green_shift = 3;
22200c426e45SAlexander Graf         blue_shift = 6;
22213e230dd2SCorentin Chary     }
22223e230dd2SCorentin Chary 
2223e6908bfeSPetr Matousek     switch (bits_per_pixel) {
2224e6908bfeSPetr Matousek     case 8:
2225e6908bfeSPetr Matousek     case 16:
2226e6908bfeSPetr Matousek     case 32:
2227e6908bfeSPetr Matousek         break;
2228e6908bfeSPetr Matousek     default:
2229e6908bfeSPetr Matousek         vnc_client_error(vs);
2230e6908bfeSPetr Matousek         return;
2231e6908bfeSPetr Matousek     }
2232e6908bfeSPetr Matousek 
22334c65fed8SPrasad J Pandit     vs->client_pf.rmax = red_max ? red_max : 0xFF;
22347c9209e7SCédric Le Goater     vs->client_pf.rbits = ctpopl(red_max);
22359f64916dSGerd Hoffmann     vs->client_pf.rshift = red_shift;
22369f64916dSGerd Hoffmann     vs->client_pf.rmask = red_max << red_shift;
22374c65fed8SPrasad J Pandit     vs->client_pf.gmax = green_max ? green_max : 0xFF;
22387c9209e7SCédric Le Goater     vs->client_pf.gbits = ctpopl(green_max);
22399f64916dSGerd Hoffmann     vs->client_pf.gshift = green_shift;
22409f64916dSGerd Hoffmann     vs->client_pf.gmask = green_max << green_shift;
22414c65fed8SPrasad J Pandit     vs->client_pf.bmax = blue_max ? blue_max : 0xFF;
22427c9209e7SCédric Le Goater     vs->client_pf.bbits = ctpopl(blue_max);
22439f64916dSGerd Hoffmann     vs->client_pf.bshift = blue_shift;
22449f64916dSGerd Hoffmann     vs->client_pf.bmask = blue_max << blue_shift;
22459f64916dSGerd Hoffmann     vs->client_pf.bits_per_pixel = bits_per_pixel;
22469f64916dSGerd Hoffmann     vs->client_pf.bytes_per_pixel = bits_per_pixel / 8;
22479f64916dSGerd Hoffmann     vs->client_pf.depth = bits_per_pixel == 32 ? 24 : bits_per_pixel;
22489f64916dSGerd Hoffmann     vs->client_be = big_endian_flag;
22493e230dd2SCorentin Chary 
22500c426e45SAlexander Graf     if (!true_color_flag) {
22510c426e45SAlexander Graf         send_color_map(vs);
22520c426e45SAlexander Graf     }
22530c426e45SAlexander Graf 
22543e230dd2SCorentin Chary     set_pixel_conversion(vs);
22553e230dd2SCorentin Chary 
22561d0d59feSGerd Hoffmann     graphic_hw_invalidate(vs->vd->dcl.con);
22571d0d59feSGerd Hoffmann     graphic_hw_update(vs->vd->dcl.con);
22583e230dd2SCorentin Chary }
22593e230dd2SCorentin Chary 
22603e230dd2SCorentin Chary static void pixel_format_message (VncState *vs) {
22613e230dd2SCorentin Chary     char pad[3] = { 0, 0, 0 };
22623e230dd2SCorentin Chary 
22639f64916dSGerd Hoffmann     vs->client_pf = qemu_default_pixelformat(32);
22649f64916dSGerd Hoffmann 
22659f64916dSGerd Hoffmann     vnc_write_u8(vs, vs->client_pf.bits_per_pixel); /* bits-per-pixel */
22669f64916dSGerd Hoffmann     vnc_write_u8(vs, vs->client_pf.depth); /* depth */
22673e230dd2SCorentin Chary 
22683e230dd2SCorentin Chary #ifdef HOST_WORDS_BIGENDIAN
22693e230dd2SCorentin Chary     vnc_write_u8(vs, 1);             /* big-endian-flag */
22703e230dd2SCorentin Chary #else
22713e230dd2SCorentin Chary     vnc_write_u8(vs, 0);             /* big-endian-flag */
22723e230dd2SCorentin Chary #endif
22733e230dd2SCorentin Chary     vnc_write_u8(vs, 1);             /* true-color-flag */
22749f64916dSGerd Hoffmann     vnc_write_u16(vs, vs->client_pf.rmax);     /* red-max */
22759f64916dSGerd Hoffmann     vnc_write_u16(vs, vs->client_pf.gmax);     /* green-max */
22769f64916dSGerd Hoffmann     vnc_write_u16(vs, vs->client_pf.bmax);     /* blue-max */
22779f64916dSGerd Hoffmann     vnc_write_u8(vs, vs->client_pf.rshift);    /* red-shift */
22789f64916dSGerd Hoffmann     vnc_write_u8(vs, vs->client_pf.gshift);    /* green-shift */
22799f64916dSGerd Hoffmann     vnc_write_u8(vs, vs->client_pf.bshift);    /* blue-shift */
22809f64916dSGerd Hoffmann     vnc_write(vs, pad, 3);           /* padding */
22813e230dd2SCorentin Chary 
22823e230dd2SCorentin Chary     vnc_hextile_set_pixel_conversion(vs, 0);
22833e230dd2SCorentin Chary     vs->write_pixels = vnc_write_pixels_copy;
22843e230dd2SCorentin Chary }
22853e230dd2SCorentin Chary 
22863e230dd2SCorentin Chary static void vnc_colordepth(VncState *vs)
22873e230dd2SCorentin Chary {
22883e230dd2SCorentin Chary     if (vnc_has_feature(vs, VNC_FEATURE_WMVI)) {
22893e230dd2SCorentin Chary         /* Sending a WMVi message to notify the client*/
2290bd023f95SCorentin Chary         vnc_lock_output(vs);
22913e230dd2SCorentin Chary         vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE);
22923e230dd2SCorentin Chary         vnc_write_u8(vs, 0);
22933e230dd2SCorentin Chary         vnc_write_u16(vs, 1); /* number of rects */
2294d39fa6d8SGerd Hoffmann         vnc_framebuffer_update(vs, 0, 0,
2295bea60dd7SPeter Lieven                                pixman_image_get_width(vs->vd->server),
2296bea60dd7SPeter Lieven                                pixman_image_get_height(vs->vd->server),
2297d39fa6d8SGerd Hoffmann                                VNC_ENCODING_WMVi);
22983e230dd2SCorentin Chary         pixel_format_message(vs);
2299bd023f95SCorentin Chary         vnc_unlock_output(vs);
23003e230dd2SCorentin Chary         vnc_flush(vs);
23013e230dd2SCorentin Chary     } else {
23023e230dd2SCorentin Chary         set_pixel_conversion(vs);
23033e230dd2SCorentin Chary     }
23043e230dd2SCorentin Chary }
23053e230dd2SCorentin Chary 
23063e230dd2SCorentin Chary static int protocol_client_msg(VncState *vs, uint8_t *data, size_t len)
23073e230dd2SCorentin Chary {
23083e230dd2SCorentin Chary     int i;
23093e230dd2SCorentin Chary     uint16_t limit;
2310cf070658SDaniel P. Berrangé     uint32_t freq;
23113e230dd2SCorentin Chary     VncDisplay *vd = vs->vd;
23123e230dd2SCorentin Chary 
23133e230dd2SCorentin Chary     if (data[0] > 3) {
23140f7b2864SGerd Hoffmann         update_displaychangelistener(&vd->dcl, VNC_REFRESH_INTERVAL_BASE);
23153e230dd2SCorentin Chary     }
23163e230dd2SCorentin Chary 
23173e230dd2SCorentin Chary     switch (data[0]) {
23183e230dd2SCorentin Chary     case VNC_MSG_CLIENT_SET_PIXEL_FORMAT:
23193e230dd2SCorentin Chary         if (len == 1)
23203e230dd2SCorentin Chary             return 20;
23213e230dd2SCorentin Chary 
2322ec9fb41aSGerd Hoffmann         set_pixel_format(vs, read_u8(data, 4),
23233e230dd2SCorentin Chary                          read_u8(data, 6), read_u8(data, 7),
23243e230dd2SCorentin Chary                          read_u16(data, 8), read_u16(data, 10),
23253e230dd2SCorentin Chary                          read_u16(data, 12), read_u8(data, 14),
23263e230dd2SCorentin Chary                          read_u8(data, 15), read_u8(data, 16));
23273e230dd2SCorentin Chary         break;
23283e230dd2SCorentin Chary     case VNC_MSG_CLIENT_SET_ENCODINGS:
23293e230dd2SCorentin Chary         if (len == 1)
23303e230dd2SCorentin Chary             return 4;
23313e230dd2SCorentin Chary 
23323e230dd2SCorentin Chary         if (len == 4) {
23333e230dd2SCorentin Chary             limit = read_u16(data, 2);
23343e230dd2SCorentin Chary             if (limit > 0)
23353e230dd2SCorentin Chary                 return 4 + (limit * 4);
23363e230dd2SCorentin Chary         } else
23373e230dd2SCorentin Chary             limit = read_u16(data, 2);
23383e230dd2SCorentin Chary 
23393e230dd2SCorentin Chary         for (i = 0; i < limit; i++) {
23403e230dd2SCorentin Chary             int32_t val = read_s32(data, 4 + (i * 4));
23413e230dd2SCorentin Chary             memcpy(data + 4 + (i * 4), &val, sizeof(val));
23423e230dd2SCorentin Chary         }
23433e230dd2SCorentin Chary 
23443e230dd2SCorentin Chary         set_encodings(vs, (int32_t *)(data + 4), limit);
23453e230dd2SCorentin Chary         break;
23463e230dd2SCorentin Chary     case VNC_MSG_CLIENT_FRAMEBUFFER_UPDATE_REQUEST:
23473e230dd2SCorentin Chary         if (len == 1)
23483e230dd2SCorentin Chary             return 10;
23493e230dd2SCorentin Chary 
23503e230dd2SCorentin Chary         framebuffer_update_request(vs,
23513e230dd2SCorentin Chary                                    read_u8(data, 1), read_u16(data, 2), read_u16(data, 4),
23523e230dd2SCorentin Chary                                    read_u16(data, 6), read_u16(data, 8));
23533e230dd2SCorentin Chary         break;
23543e230dd2SCorentin Chary     case VNC_MSG_CLIENT_KEY_EVENT:
23553e230dd2SCorentin Chary         if (len == 1)
23563e230dd2SCorentin Chary             return 8;
23573e230dd2SCorentin Chary 
23583e230dd2SCorentin Chary         key_event(vs, read_u8(data, 1), read_u32(data, 4));
23593e230dd2SCorentin Chary         break;
23603e230dd2SCorentin Chary     case VNC_MSG_CLIENT_POINTER_EVENT:
23613e230dd2SCorentin Chary         if (len == 1)
23623e230dd2SCorentin Chary             return 6;
23633e230dd2SCorentin Chary 
23643e230dd2SCorentin Chary         pointer_event(vs, read_u8(data, 1), read_u16(data, 2), read_u16(data, 4));
23653e230dd2SCorentin Chary         break;
23663e230dd2SCorentin Chary     case VNC_MSG_CLIENT_CUT_TEXT:
2367f9a70e79SPeter Lieven         if (len == 1) {
23683e230dd2SCorentin Chary             return 8;
2369f9a70e79SPeter Lieven         }
23703e230dd2SCorentin Chary         if (len == 8) {
23713e230dd2SCorentin Chary             uint32_t dlen = read_u32(data, 4);
2372f9a70e79SPeter Lieven             if (dlen > (1 << 20)) {
2373f9a70e79SPeter Lieven                 error_report("vnc: client_cut_text msg payload has %u bytes"
2374f9a70e79SPeter Lieven                              " which exceeds our limit of 1MB.", dlen);
2375f9a70e79SPeter Lieven                 vnc_client_error(vs);
2376f9a70e79SPeter Lieven                 break;
2377f9a70e79SPeter Lieven             }
2378f9a70e79SPeter Lieven             if (dlen > 0) {
23793e230dd2SCorentin Chary                 return 8 + dlen;
23803e230dd2SCorentin Chary             }
2381f9a70e79SPeter Lieven         }
23823e230dd2SCorentin Chary 
23833e230dd2SCorentin Chary         client_cut_text(vs, read_u32(data, 4), data + 8);
23843e230dd2SCorentin Chary         break;
23853e230dd2SCorentin Chary     case VNC_MSG_CLIENT_QEMU:
23863e230dd2SCorentin Chary         if (len == 1)
23873e230dd2SCorentin Chary             return 2;
23883e230dd2SCorentin Chary 
23893e230dd2SCorentin Chary         switch (read_u8(data, 1)) {
23903e230dd2SCorentin Chary         case VNC_MSG_CLIENT_QEMU_EXT_KEY_EVENT:
23913e230dd2SCorentin Chary             if (len == 2)
23923e230dd2SCorentin Chary                 return 12;
23933e230dd2SCorentin Chary 
23943e230dd2SCorentin Chary             ext_key_event(vs, read_u16(data, 2),
23953e230dd2SCorentin Chary                           read_u32(data, 4), read_u32(data, 8));
23963e230dd2SCorentin Chary             break;
23973e230dd2SCorentin Chary         case VNC_MSG_CLIENT_QEMU_AUDIO:
23983e230dd2SCorentin Chary             if (len == 2)
23993e230dd2SCorentin Chary                 return 4;
24003e230dd2SCorentin Chary 
24013e230dd2SCorentin Chary             switch (read_u16 (data, 2)) {
24023e230dd2SCorentin Chary             case VNC_MSG_CLIENT_QEMU_AUDIO_ENABLE:
24033e230dd2SCorentin Chary                 audio_add(vs);
24043e230dd2SCorentin Chary                 break;
24053e230dd2SCorentin Chary             case VNC_MSG_CLIENT_QEMU_AUDIO_DISABLE:
24063e230dd2SCorentin Chary                 audio_del(vs);
24073e230dd2SCorentin Chary                 break;
24083e230dd2SCorentin Chary             case VNC_MSG_CLIENT_QEMU_AUDIO_SET_FORMAT:
24093e230dd2SCorentin Chary                 if (len == 4)
24103e230dd2SCorentin Chary                     return 10;
24113e230dd2SCorentin Chary                 switch (read_u8(data, 4)) {
24123e230dd2SCorentin Chary                 case 0: vs->as.fmt = AUD_FMT_U8; break;
24133e230dd2SCorentin Chary                 case 1: vs->as.fmt = AUD_FMT_S8; break;
24143e230dd2SCorentin Chary                 case 2: vs->as.fmt = AUD_FMT_U16; break;
24153e230dd2SCorentin Chary                 case 3: vs->as.fmt = AUD_FMT_S16; break;
24163e230dd2SCorentin Chary                 case 4: vs->as.fmt = AUD_FMT_U32; break;
24173e230dd2SCorentin Chary                 case 5: vs->as.fmt = AUD_FMT_S32; break;
24183e230dd2SCorentin Chary                 default:
2419153130cdSDaniel P. Berrange                     VNC_DEBUG("Invalid audio format %d\n", read_u8(data, 4));
24203e230dd2SCorentin Chary                     vnc_client_error(vs);
24213e230dd2SCorentin Chary                     break;
24223e230dd2SCorentin Chary                 }
24233e230dd2SCorentin Chary                 vs->as.nchannels = read_u8(data, 5);
24243e230dd2SCorentin Chary                 if (vs->as.nchannels != 1 && vs->as.nchannels != 2) {
2425090fdc83SMarc-André Lureau                     VNC_DEBUG("Invalid audio channel count %d\n",
24263e230dd2SCorentin Chary                               read_u8(data, 5));
24273e230dd2SCorentin Chary                     vnc_client_error(vs);
24283e230dd2SCorentin Chary                     break;
24293e230dd2SCorentin Chary                 }
2430cf070658SDaniel P. Berrangé                 freq = read_u32(data, 6);
2431cf070658SDaniel P. Berrangé                 /* No official limit for protocol, but 48khz is a sensible
2432cf070658SDaniel P. Berrangé                  * upper bound for trustworthy clients, and this limit
2433cf070658SDaniel P. Berrangé                  * protects calculations involving 'vs->as.freq' later.
2434cf070658SDaniel P. Berrangé                  */
2435cf070658SDaniel P. Berrangé                 if (freq > 48000) {
2436cf070658SDaniel P. Berrangé                     VNC_DEBUG("Invalid audio frequency %u > 48000", freq);
2437cf070658SDaniel P. Berrangé                     vnc_client_error(vs);
2438cf070658SDaniel P. Berrangé                     break;
2439cf070658SDaniel P. Berrangé                 }
2440cf070658SDaniel P. Berrangé                 vs->as.freq = freq;
24413e230dd2SCorentin Chary                 break;
24423e230dd2SCorentin Chary             default:
2443153130cdSDaniel P. Berrange                 VNC_DEBUG("Invalid audio message %d\n", read_u8(data, 4));
24443e230dd2SCorentin Chary                 vnc_client_error(vs);
24453e230dd2SCorentin Chary                 break;
24463e230dd2SCorentin Chary             }
24473e230dd2SCorentin Chary             break;
24483e230dd2SCorentin Chary 
24493e230dd2SCorentin Chary         default:
2450153130cdSDaniel P. Berrange             VNC_DEBUG("Msg: %d\n", read_u16(data, 0));
24513e230dd2SCorentin Chary             vnc_client_error(vs);
24523e230dd2SCorentin Chary             break;
24533e230dd2SCorentin Chary         }
24543e230dd2SCorentin Chary         break;
24553e230dd2SCorentin Chary     default:
2456153130cdSDaniel P. Berrange         VNC_DEBUG("Msg: %d\n", data[0]);
24573e230dd2SCorentin Chary         vnc_client_error(vs);
24583e230dd2SCorentin Chary         break;
24593e230dd2SCorentin Chary     }
24603e230dd2SCorentin Chary 
2461e2b72cb6SDaniel P. Berrange     vnc_update_throttle_offset(vs);
24623e230dd2SCorentin Chary     vnc_read_when(vs, protocol_client_msg, 1);
24633e230dd2SCorentin Chary     return 0;
24643e230dd2SCorentin Chary }
24653e230dd2SCorentin Chary 
24663e230dd2SCorentin Chary static int protocol_client_init(VncState *vs, uint8_t *data, size_t len)
24673e230dd2SCorentin Chary {
24683e230dd2SCorentin Chary     char buf[1024];
24698cf36489SGerd Hoffmann     VncShareMode mode;
24703e230dd2SCorentin Chary     int size;
24713e230dd2SCorentin Chary 
24728cf36489SGerd Hoffmann     mode = data[0] ? VNC_SHARE_MODE_SHARED : VNC_SHARE_MODE_EXCLUSIVE;
24738cf36489SGerd Hoffmann     switch (vs->vd->share_policy) {
24748cf36489SGerd Hoffmann     case VNC_SHARE_POLICY_IGNORE:
24758cf36489SGerd Hoffmann         /*
24768cf36489SGerd Hoffmann          * Ignore the shared flag.  Nothing to do here.
24778cf36489SGerd Hoffmann          *
24788cf36489SGerd Hoffmann          * Doesn't conform to the rfb spec but is traditional qemu
24798cf36489SGerd Hoffmann          * behavior, thus left here as option for compatibility
24808cf36489SGerd Hoffmann          * reasons.
24818cf36489SGerd Hoffmann          */
24828cf36489SGerd Hoffmann         break;
24838cf36489SGerd Hoffmann     case VNC_SHARE_POLICY_ALLOW_EXCLUSIVE:
24848cf36489SGerd Hoffmann         /*
24858cf36489SGerd Hoffmann          * Policy: Allow clients ask for exclusive access.
24868cf36489SGerd Hoffmann          *
24878cf36489SGerd Hoffmann          * Implementation: When a client asks for exclusive access,
24888cf36489SGerd Hoffmann          * disconnect all others. Shared connects are allowed as long
24898cf36489SGerd Hoffmann          * as no exclusive connection exists.
24908cf36489SGerd Hoffmann          *
24918cf36489SGerd Hoffmann          * This is how the rfb spec suggests to handle the shared flag.
24928cf36489SGerd Hoffmann          */
24938cf36489SGerd Hoffmann         if (mode == VNC_SHARE_MODE_EXCLUSIVE) {
24948cf36489SGerd Hoffmann             VncState *client;
24958cf36489SGerd Hoffmann             QTAILQ_FOREACH(client, &vs->vd->clients, next) {
24968cf36489SGerd Hoffmann                 if (vs == client) {
24978cf36489SGerd Hoffmann                     continue;
24988cf36489SGerd Hoffmann                 }
24998cf36489SGerd Hoffmann                 if (client->share_mode != VNC_SHARE_MODE_EXCLUSIVE &&
25008cf36489SGerd Hoffmann                     client->share_mode != VNC_SHARE_MODE_SHARED) {
25018cf36489SGerd Hoffmann                     continue;
25028cf36489SGerd Hoffmann                 }
25038cf36489SGerd Hoffmann                 vnc_disconnect_start(client);
25048cf36489SGerd Hoffmann             }
25058cf36489SGerd Hoffmann         }
25068cf36489SGerd Hoffmann         if (mode == VNC_SHARE_MODE_SHARED) {
25078cf36489SGerd Hoffmann             if (vs->vd->num_exclusive > 0) {
25088cf36489SGerd Hoffmann                 vnc_disconnect_start(vs);
25098cf36489SGerd Hoffmann                 return 0;
25108cf36489SGerd Hoffmann             }
25118cf36489SGerd Hoffmann         }
25128cf36489SGerd Hoffmann         break;
25138cf36489SGerd Hoffmann     case VNC_SHARE_POLICY_FORCE_SHARED:
25148cf36489SGerd Hoffmann         /*
25158cf36489SGerd Hoffmann          * Policy: Shared connects only.
25168cf36489SGerd Hoffmann          * Implementation: Disallow clients asking for exclusive access.
25178cf36489SGerd Hoffmann          *
25188cf36489SGerd Hoffmann          * Useful for shared desktop sessions where you don't want
25198cf36489SGerd Hoffmann          * someone forgetting to say -shared when running the vnc
25208cf36489SGerd Hoffmann          * client disconnect everybody else.
25218cf36489SGerd Hoffmann          */
25228cf36489SGerd Hoffmann         if (mode == VNC_SHARE_MODE_EXCLUSIVE) {
25238cf36489SGerd Hoffmann             vnc_disconnect_start(vs);
25248cf36489SGerd Hoffmann             return 0;
25258cf36489SGerd Hoffmann         }
25268cf36489SGerd Hoffmann         break;
25278cf36489SGerd Hoffmann     }
25288cf36489SGerd Hoffmann     vnc_set_share_mode(vs, mode);
25298cf36489SGerd Hoffmann 
2530e5f34cddSGerd Hoffmann     if (vs->vd->num_shared > vs->vd->connections_limit) {
2531e5f34cddSGerd Hoffmann         vnc_disconnect_start(vs);
2532e5f34cddSGerd Hoffmann         return 0;
2533e5f34cddSGerd Hoffmann     }
2534e5f34cddSGerd Hoffmann 
25354c956bd8SDaniel P. Berrange     assert(pixman_image_get_width(vs->vd->server) < 65536 &&
25364c956bd8SDaniel P. Berrange            pixman_image_get_width(vs->vd->server) >= 0);
25374c956bd8SDaniel P. Berrange     assert(pixman_image_get_height(vs->vd->server) < 65536 &&
25384c956bd8SDaniel P. Berrange            pixman_image_get_height(vs->vd->server) >= 0);
2539bea60dd7SPeter Lieven     vs->client_width = pixman_image_get_width(vs->vd->server);
2540bea60dd7SPeter Lieven     vs->client_height = pixman_image_get_height(vs->vd->server);
25413e230dd2SCorentin Chary     vnc_write_u16(vs, vs->client_width);
25423e230dd2SCorentin Chary     vnc_write_u16(vs, vs->client_height);
25433e230dd2SCorentin Chary 
25443e230dd2SCorentin Chary     pixel_format_message(vs);
25453e230dd2SCorentin Chary 
254697efe4f9SThomas Huth     if (qemu_name) {
25473e230dd2SCorentin Chary         size = snprintf(buf, sizeof(buf), "QEMU (%s)", qemu_name);
254897efe4f9SThomas Huth         if (size > sizeof(buf)) {
254997efe4f9SThomas Huth             size = sizeof(buf);
255097efe4f9SThomas Huth         }
255197efe4f9SThomas Huth     } else {
25523e230dd2SCorentin Chary         size = snprintf(buf, sizeof(buf), "QEMU");
255397efe4f9SThomas Huth     }
25543e230dd2SCorentin Chary 
25553e230dd2SCorentin Chary     vnc_write_u32(vs, size);
25563e230dd2SCorentin Chary     vnc_write(vs, buf, size);
25573e230dd2SCorentin Chary     vnc_flush(vs);
25583e230dd2SCorentin Chary 
25593e230dd2SCorentin Chary     vnc_client_cache_auth(vs);
2560fb6ba0d5SWenchao Xia     vnc_qmp_event(vs, QAPI_EVENT_VNC_INITIALIZED);
25613e230dd2SCorentin Chary 
25623e230dd2SCorentin Chary     vnc_read_when(vs, protocol_client_msg, 1);
25633e230dd2SCorentin Chary 
25643e230dd2SCorentin Chary     return 0;
25653e230dd2SCorentin Chary }
25663e230dd2SCorentin Chary 
25673e230dd2SCorentin Chary void start_client_init(VncState *vs)
25683e230dd2SCorentin Chary {
25693e230dd2SCorentin Chary     vnc_read_when(vs, protocol_client_init, 1);
25703e230dd2SCorentin Chary }
25713e230dd2SCorentin Chary 
25723e230dd2SCorentin Chary static void make_challenge(VncState *vs)
25733e230dd2SCorentin Chary {
25743e230dd2SCorentin Chary     int i;
25753e230dd2SCorentin Chary 
25763e230dd2SCorentin Chary     srand(time(NULL)+getpid()+getpid()*987654+rand());
25773e230dd2SCorentin Chary 
25783e230dd2SCorentin Chary     for (i = 0 ; i < sizeof(vs->challenge) ; i++)
25793e230dd2SCorentin Chary         vs->challenge[i] = (int) (256.0*rand()/(RAND_MAX+1.0));
25803e230dd2SCorentin Chary }
25813e230dd2SCorentin Chary 
25823e230dd2SCorentin Chary static int protocol_client_auth_vnc(VncState *vs, uint8_t *data, size_t len)
25833e230dd2SCorentin Chary {
25843e230dd2SCorentin Chary     unsigned char response[VNC_AUTH_CHALLENGE_SIZE];
2585800567a6SDaniel P. Berrange     size_t i, pwlen;
25863e230dd2SCorentin Chary     unsigned char key[8];
25873c9405a0SGerd Hoffmann     time_t now = time(NULL);
258860928458SGonglei     QCryptoCipher *cipher = NULL;
2589800567a6SDaniel P. Berrange     Error *err = NULL;
25903e230dd2SCorentin Chary 
25911cd20f8bSAnthony Liguori     if (!vs->vd->password) {
25927364dbdaSDaniel P. Berrange         trace_vnc_auth_fail(vs, vs->auth, "password is not set", "");
25936bffdf0fSGerd Hoffmann         goto reject;
25943e230dd2SCorentin Chary     }
25953c9405a0SGerd Hoffmann     if (vs->vd->expires < now) {
25967364dbdaSDaniel P. Berrange         trace_vnc_auth_fail(vs, vs->auth, "password is expired", "");
25973c9405a0SGerd Hoffmann         goto reject;
25983c9405a0SGerd Hoffmann     }
25993e230dd2SCorentin Chary 
26003e230dd2SCorentin Chary     memcpy(response, vs->challenge, VNC_AUTH_CHALLENGE_SIZE);
26013e230dd2SCorentin Chary 
26023e230dd2SCorentin Chary     /* Calculate the expected challenge response */
26033e230dd2SCorentin Chary     pwlen = strlen(vs->vd->password);
26043e230dd2SCorentin Chary     for (i=0; i<sizeof(key); i++)
26053e230dd2SCorentin Chary         key[i] = i<pwlen ? vs->vd->password[i] : 0;
2606800567a6SDaniel P. Berrange 
2607800567a6SDaniel P. Berrange     cipher = qcrypto_cipher_new(
2608800567a6SDaniel P. Berrange         QCRYPTO_CIPHER_ALG_DES_RFB,
2609800567a6SDaniel P. Berrange         QCRYPTO_CIPHER_MODE_ECB,
2610800567a6SDaniel P. Berrange         key, G_N_ELEMENTS(key),
2611800567a6SDaniel P. Berrange         &err);
2612800567a6SDaniel P. Berrange     if (!cipher) {
26137364dbdaSDaniel P. Berrange         trace_vnc_auth_fail(vs, vs->auth, "cannot create cipher",
2614800567a6SDaniel P. Berrange                             error_get_pretty(err));
2615800567a6SDaniel P. Berrange         error_free(err);
2616800567a6SDaniel P. Berrange         goto reject;
2617800567a6SDaniel P. Berrange     }
2618800567a6SDaniel P. Berrange 
2619a1695137SWolfgang Bumiller     if (qcrypto_cipher_encrypt(cipher,
2620800567a6SDaniel P. Berrange                                vs->challenge,
2621800567a6SDaniel P. Berrange                                response,
2622800567a6SDaniel P. Berrange                                VNC_AUTH_CHALLENGE_SIZE,
2623800567a6SDaniel P. Berrange                                &err) < 0) {
26247364dbdaSDaniel P. Berrange         trace_vnc_auth_fail(vs, vs->auth, "cannot encrypt challenge response",
2625800567a6SDaniel P. Berrange                             error_get_pretty(err));
2626800567a6SDaniel P. Berrange         error_free(err);
2627800567a6SDaniel P. Berrange         goto reject;
2628800567a6SDaniel P. Berrange     }
26293e230dd2SCorentin Chary 
26303e230dd2SCorentin Chary     /* Compare expected vs actual challenge response */
26313e230dd2SCorentin Chary     if (memcmp(response, data, VNC_AUTH_CHALLENGE_SIZE) != 0) {
26327364dbdaSDaniel P. Berrange         trace_vnc_auth_fail(vs, vs->auth, "mis-matched challenge response", "");
26336bffdf0fSGerd Hoffmann         goto reject;
26346bffdf0fSGerd Hoffmann     } else {
26357364dbdaSDaniel P. Berrange         trace_vnc_auth_pass(vs, vs->auth);
26366bffdf0fSGerd Hoffmann         vnc_write_u32(vs, 0); /* Accept auth */
26376bffdf0fSGerd Hoffmann         vnc_flush(vs);
26386bffdf0fSGerd Hoffmann 
26396bffdf0fSGerd Hoffmann         start_client_init(vs);
26406bffdf0fSGerd Hoffmann     }
264160928458SGonglei 
264260928458SGonglei     qcrypto_cipher_free(cipher);
26436bffdf0fSGerd Hoffmann     return 0;
26446bffdf0fSGerd Hoffmann 
26456bffdf0fSGerd Hoffmann reject:
26463e230dd2SCorentin Chary     vnc_write_u32(vs, 1); /* Reject auth */
26473e230dd2SCorentin Chary     if (vs->minor >= 8) {
26483e230dd2SCorentin Chary         static const char err[] = "Authentication failed";
26493e230dd2SCorentin Chary         vnc_write_u32(vs, sizeof(err));
26503e230dd2SCorentin Chary         vnc_write(vs, err, sizeof(err));
26513e230dd2SCorentin Chary     }
26523e230dd2SCorentin Chary     vnc_flush(vs);
26533e230dd2SCorentin Chary     vnc_client_error(vs);
265460928458SGonglei     qcrypto_cipher_free(cipher);
26553e230dd2SCorentin Chary     return 0;
26563e230dd2SCorentin Chary }
26573e230dd2SCorentin Chary 
26583e230dd2SCorentin Chary void start_auth_vnc(VncState *vs)
26593e230dd2SCorentin Chary {
26603e230dd2SCorentin Chary     make_challenge(vs);
26613e230dd2SCorentin Chary     /* Send client a 'random' challenge */
26623e230dd2SCorentin Chary     vnc_write(vs, vs->challenge, sizeof(vs->challenge));
26633e230dd2SCorentin Chary     vnc_flush(vs);
26643e230dd2SCorentin Chary 
26653e230dd2SCorentin Chary     vnc_read_when(vs, protocol_client_auth_vnc, sizeof(vs->challenge));
26663e230dd2SCorentin Chary }
26673e230dd2SCorentin Chary 
26683e230dd2SCorentin Chary 
26693e230dd2SCorentin Chary static int protocol_client_auth(VncState *vs, uint8_t *data, size_t len)
26703e230dd2SCorentin Chary {
26713e230dd2SCorentin Chary     /* We only advertise 1 auth scheme at a time, so client
26723e230dd2SCorentin Chary      * must pick the one we sent. Verify this */
26737e7e2ebcSDaniel P. Berrange     if (data[0] != vs->auth) { /* Reject auth */
26747364dbdaSDaniel P. Berrange        trace_vnc_auth_reject(vs, vs->auth, (int)data[0]);
26753e230dd2SCorentin Chary        vnc_write_u32(vs, 1);
26763e230dd2SCorentin Chary        if (vs->minor >= 8) {
26773e230dd2SCorentin Chary            static const char err[] = "Authentication failed";
26783e230dd2SCorentin Chary            vnc_write_u32(vs, sizeof(err));
26793e230dd2SCorentin Chary            vnc_write(vs, err, sizeof(err));
26803e230dd2SCorentin Chary        }
26813e230dd2SCorentin Chary        vnc_client_error(vs);
26823e230dd2SCorentin Chary     } else { /* Accept requested auth */
26837364dbdaSDaniel P. Berrange        trace_vnc_auth_start(vs, vs->auth);
26847e7e2ebcSDaniel P. Berrange        switch (vs->auth) {
26853e230dd2SCorentin Chary        case VNC_AUTH_NONE:
26863e230dd2SCorentin Chary            if (vs->minor >= 8) {
26873e230dd2SCorentin Chary                vnc_write_u32(vs, 0); /* Accept auth completion */
26883e230dd2SCorentin Chary                vnc_flush(vs);
26893e230dd2SCorentin Chary            }
26907364dbdaSDaniel P. Berrange            trace_vnc_auth_pass(vs, vs->auth);
26913e230dd2SCorentin Chary            start_client_init(vs);
26923e230dd2SCorentin Chary            break;
26933e230dd2SCorentin Chary 
26943e230dd2SCorentin Chary        case VNC_AUTH_VNC:
26953e230dd2SCorentin Chary            start_auth_vnc(vs);
26963e230dd2SCorentin Chary            break;
26973e230dd2SCorentin Chary 
26983e230dd2SCorentin Chary        case VNC_AUTH_VENCRYPT:
26993e230dd2SCorentin Chary            start_auth_vencrypt(vs);
27003e230dd2SCorentin Chary            break;
27013e230dd2SCorentin Chary 
27023e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL
27033e230dd2SCorentin Chary        case VNC_AUTH_SASL:
27043e230dd2SCorentin Chary            start_auth_sasl(vs);
27053e230dd2SCorentin Chary            break;
27063e230dd2SCorentin Chary #endif /* CONFIG_VNC_SASL */
27073e230dd2SCorentin Chary 
27083e230dd2SCorentin Chary        default: /* Should not be possible, but just in case */
27097364dbdaSDaniel P. Berrange            trace_vnc_auth_fail(vs, vs->auth, "Unhandled auth method", "");
27103e230dd2SCorentin Chary            vnc_write_u8(vs, 1);
27113e230dd2SCorentin Chary            if (vs->minor >= 8) {
27123e230dd2SCorentin Chary                static const char err[] = "Authentication failed";
27133e230dd2SCorentin Chary                vnc_write_u32(vs, sizeof(err));
27143e230dd2SCorentin Chary                vnc_write(vs, err, sizeof(err));
27153e230dd2SCorentin Chary            }
27163e230dd2SCorentin Chary            vnc_client_error(vs);
27173e230dd2SCorentin Chary        }
27183e230dd2SCorentin Chary     }
27193e230dd2SCorentin Chary     return 0;
27203e230dd2SCorentin Chary }
27213e230dd2SCorentin Chary 
27223e230dd2SCorentin Chary static int protocol_version(VncState *vs, uint8_t *version, size_t len)
27233e230dd2SCorentin Chary {
27243e230dd2SCorentin Chary     char local[13];
27253e230dd2SCorentin Chary 
27263e230dd2SCorentin Chary     memcpy(local, version, 12);
27273e230dd2SCorentin Chary     local[12] = 0;
27283e230dd2SCorentin Chary 
27293e230dd2SCorentin Chary     if (sscanf(local, "RFB %03d.%03d\n", &vs->major, &vs->minor) != 2) {
27303e230dd2SCorentin Chary         VNC_DEBUG("Malformed protocol version %s\n", local);
27313e230dd2SCorentin Chary         vnc_client_error(vs);
27323e230dd2SCorentin Chary         return 0;
27333e230dd2SCorentin Chary     }
27343e230dd2SCorentin Chary     VNC_DEBUG("Client request protocol version %d.%d\n", vs->major, vs->minor);
27353e230dd2SCorentin Chary     if (vs->major != 3 ||
27363e230dd2SCorentin Chary         (vs->minor != 3 &&
27373e230dd2SCorentin Chary          vs->minor != 4 &&
27383e230dd2SCorentin Chary          vs->minor != 5 &&
27393e230dd2SCorentin Chary          vs->minor != 7 &&
27403e230dd2SCorentin Chary          vs->minor != 8)) {
27413e230dd2SCorentin Chary         VNC_DEBUG("Unsupported client version\n");
27423e230dd2SCorentin Chary         vnc_write_u32(vs, VNC_AUTH_INVALID);
27433e230dd2SCorentin Chary         vnc_flush(vs);
27443e230dd2SCorentin Chary         vnc_client_error(vs);
27453e230dd2SCorentin Chary         return 0;
27463e230dd2SCorentin Chary     }
27473e230dd2SCorentin Chary     /* Some broken clients report v3.4 or v3.5, which spec requires to be treated
27483e230dd2SCorentin Chary      * as equivalent to v3.3 by servers
27493e230dd2SCorentin Chary      */
27503e230dd2SCorentin Chary     if (vs->minor == 4 || vs->minor == 5)
27513e230dd2SCorentin Chary         vs->minor = 3;
27523e230dd2SCorentin Chary 
27533e230dd2SCorentin Chary     if (vs->minor == 3) {
27547364dbdaSDaniel P. Berrange         trace_vnc_auth_start(vs, vs->auth);
27557e7e2ebcSDaniel P. Berrange         if (vs->auth == VNC_AUTH_NONE) {
27567e7e2ebcSDaniel P. Berrange             vnc_write_u32(vs, vs->auth);
27573e230dd2SCorentin Chary             vnc_flush(vs);
27587364dbdaSDaniel P. Berrange             trace_vnc_auth_pass(vs, vs->auth);
27593e230dd2SCorentin Chary             start_client_init(vs);
27607e7e2ebcSDaniel P. Berrange        } else if (vs->auth == VNC_AUTH_VNC) {
27613e230dd2SCorentin Chary             VNC_DEBUG("Tell client VNC auth\n");
27627e7e2ebcSDaniel P. Berrange             vnc_write_u32(vs, vs->auth);
27633e230dd2SCorentin Chary             vnc_flush(vs);
27643e230dd2SCorentin Chary             start_auth_vnc(vs);
27653e230dd2SCorentin Chary        } else {
27667364dbdaSDaniel P. Berrange             trace_vnc_auth_fail(vs, vs->auth,
27677364dbdaSDaniel P. Berrange                                 "Unsupported auth method for v3.3", "");
27683e230dd2SCorentin Chary             vnc_write_u32(vs, VNC_AUTH_INVALID);
27693e230dd2SCorentin Chary             vnc_flush(vs);
27703e230dd2SCorentin Chary             vnc_client_error(vs);
27713e230dd2SCorentin Chary        }
27723e230dd2SCorentin Chary     } else {
27733e230dd2SCorentin Chary         vnc_write_u8(vs, 1); /* num auth */
27747e7e2ebcSDaniel P. Berrange         vnc_write_u8(vs, vs->auth);
27753e230dd2SCorentin Chary         vnc_read_when(vs, protocol_client_auth, 1);
27763e230dd2SCorentin Chary         vnc_flush(vs);
27773e230dd2SCorentin Chary     }
27783e230dd2SCorentin Chary 
27793e230dd2SCorentin Chary     return 0;
27803e230dd2SCorentin Chary }
27813e230dd2SCorentin Chary 
2782999342a0SCorentin Chary static VncRectStat *vnc_stat_rect(VncDisplay *vd, int x, int y)
2783999342a0SCorentin Chary {
2784999342a0SCorentin Chary     struct VncSurface *vs = &vd->guest;
2785999342a0SCorentin Chary 
2786999342a0SCorentin Chary     return &vs->stats[y / VNC_STAT_RECT][x / VNC_STAT_RECT];
2787999342a0SCorentin Chary }
2788999342a0SCorentin Chary 
27897d964c9dSCorentin Chary void vnc_sent_lossy_rect(VncState *vs, int x, int y, int w, int h)
27907d964c9dSCorentin Chary {
27917d964c9dSCorentin Chary     int i, j;
27927d964c9dSCorentin Chary 
27937d964c9dSCorentin Chary     w = (x + w) / VNC_STAT_RECT;
27947d964c9dSCorentin Chary     h = (y + h) / VNC_STAT_RECT;
27957d964c9dSCorentin Chary     x /= VNC_STAT_RECT;
27967d964c9dSCorentin Chary     y /= VNC_STAT_RECT;
27977d964c9dSCorentin Chary 
2798207f328aSCorentin Chary     for (j = y; j <= h; j++) {
2799207f328aSCorentin Chary         for (i = x; i <= w; i++) {
28007d964c9dSCorentin Chary             vs->lossy_rect[j][i] = 1;
28017d964c9dSCorentin Chary         }
28027d964c9dSCorentin Chary     }
28037d964c9dSCorentin Chary }
28047d964c9dSCorentin Chary 
28057d964c9dSCorentin Chary static int vnc_refresh_lossy_rect(VncDisplay *vd, int x, int y)
28067d964c9dSCorentin Chary {
28077d964c9dSCorentin Chary     VncState *vs;
28087d964c9dSCorentin Chary     int sty = y / VNC_STAT_RECT;
28097d964c9dSCorentin Chary     int stx = x / VNC_STAT_RECT;
28107d964c9dSCorentin Chary     int has_dirty = 0;
28117d964c9dSCorentin Chary 
28125a3804dbSMarc-André Lureau     y = QEMU_ALIGN_DOWN(y, VNC_STAT_RECT);
28135a3804dbSMarc-André Lureau     x = QEMU_ALIGN_DOWN(x, VNC_STAT_RECT);
28147d964c9dSCorentin Chary 
28157d964c9dSCorentin Chary     QTAILQ_FOREACH(vs, &vd->clients, next) {
2816bc2429b9SCorentin Chary         int j;
28177d964c9dSCorentin Chary 
28187d964c9dSCorentin Chary         /* kernel send buffers are full -> refresh later */
28197d964c9dSCorentin Chary         if (vs->output.offset) {
28207d964c9dSCorentin Chary             continue;
28217d964c9dSCorentin Chary         }
28227d964c9dSCorentin Chary 
28237d964c9dSCorentin Chary         if (!vs->lossy_rect[sty][stx]) {
28247d964c9dSCorentin Chary             continue;
28257d964c9dSCorentin Chary         }
2826207f328aSCorentin Chary 
28277d964c9dSCorentin Chary         vs->lossy_rect[sty][stx] = 0;
28287d964c9dSCorentin Chary         for (j = 0; j < VNC_STAT_RECT; ++j) {
2829b4c85ddcSPeter Lieven             bitmap_set(vs->dirty[y + j],
2830b4c85ddcSPeter Lieven                        x / VNC_DIRTY_PIXELS_PER_BIT,
2831b4c85ddcSPeter Lieven                        VNC_STAT_RECT / VNC_DIRTY_PIXELS_PER_BIT);
28327d964c9dSCorentin Chary         }
28337d964c9dSCorentin Chary         has_dirty++;
28347d964c9dSCorentin Chary     }
2835207f328aSCorentin Chary 
28367d964c9dSCorentin Chary     return has_dirty;
28377d964c9dSCorentin Chary }
28387d964c9dSCorentin Chary 
28397d964c9dSCorentin Chary static int vnc_update_stats(VncDisplay *vd,  struct timeval * tv)
2840999342a0SCorentin Chary {
2841eebe0b79SGerd Hoffmann     int width = MIN(pixman_image_get_width(vd->guest.fb),
2842eebe0b79SGerd Hoffmann                     pixman_image_get_width(vd->server));
2843eebe0b79SGerd Hoffmann     int height = MIN(pixman_image_get_height(vd->guest.fb),
2844eebe0b79SGerd Hoffmann                      pixman_image_get_height(vd->server));
2845999342a0SCorentin Chary     int x, y;
2846999342a0SCorentin Chary     struct timeval res;
28477d964c9dSCorentin Chary     int has_dirty = 0;
2848999342a0SCorentin Chary 
28499f64916dSGerd Hoffmann     for (y = 0; y < height; y += VNC_STAT_RECT) {
28509f64916dSGerd Hoffmann         for (x = 0; x < width; x += VNC_STAT_RECT) {
2851999342a0SCorentin Chary             VncRectStat *rect = vnc_stat_rect(vd, x, y);
2852999342a0SCorentin Chary 
2853999342a0SCorentin Chary             rect->updated = false;
2854999342a0SCorentin Chary         }
2855999342a0SCorentin Chary     }
2856999342a0SCorentin Chary 
2857ad620c29SBlue Swirl     qemu_timersub(tv, &VNC_REFRESH_STATS, &res);
2858999342a0SCorentin Chary 
2859999342a0SCorentin Chary     if (timercmp(&vd->guest.last_freq_check, &res, >)) {
28607d964c9dSCorentin Chary         return has_dirty;
2861999342a0SCorentin Chary     }
2862999342a0SCorentin Chary     vd->guest.last_freq_check = *tv;
2863999342a0SCorentin Chary 
28649f64916dSGerd Hoffmann     for (y = 0; y < height; y += VNC_STAT_RECT) {
28659f64916dSGerd Hoffmann         for (x = 0; x < width; x += VNC_STAT_RECT) {
2866999342a0SCorentin Chary             VncRectStat *rect= vnc_stat_rect(vd, x, y);
2867999342a0SCorentin Chary             int count = ARRAY_SIZE(rect->times);
2868999342a0SCorentin Chary             struct timeval min, max;
2869999342a0SCorentin Chary 
2870999342a0SCorentin Chary             if (!timerisset(&rect->times[count - 1])) {
2871999342a0SCorentin Chary                 continue ;
2872999342a0SCorentin Chary             }
2873999342a0SCorentin Chary 
2874999342a0SCorentin Chary             max = rect->times[(rect->idx + count - 1) % count];
2875ad620c29SBlue Swirl             qemu_timersub(tv, &max, &res);
2876999342a0SCorentin Chary 
2877999342a0SCorentin Chary             if (timercmp(&res, &VNC_REFRESH_LOSSY, >)) {
2878999342a0SCorentin Chary                 rect->freq = 0;
28797d964c9dSCorentin Chary                 has_dirty += vnc_refresh_lossy_rect(vd, x, y);
2880999342a0SCorentin Chary                 memset(rect->times, 0, sizeof (rect->times));
2881999342a0SCorentin Chary                 continue ;
2882999342a0SCorentin Chary             }
2883999342a0SCorentin Chary 
2884999342a0SCorentin Chary             min = rect->times[rect->idx];
2885999342a0SCorentin Chary             max = rect->times[(rect->idx + count - 1) % count];
2886ad620c29SBlue Swirl             qemu_timersub(&max, &min, &res);
2887999342a0SCorentin Chary 
2888999342a0SCorentin Chary             rect->freq = res.tv_sec + res.tv_usec / 1000000.;
2889999342a0SCorentin Chary             rect->freq /= count;
2890999342a0SCorentin Chary             rect->freq = 1. / rect->freq;
2891999342a0SCorentin Chary         }
2892999342a0SCorentin Chary     }
28937d964c9dSCorentin Chary     return has_dirty;
2894999342a0SCorentin Chary }
2895999342a0SCorentin Chary 
2896999342a0SCorentin Chary double vnc_update_freq(VncState *vs, int x, int y, int w, int h)
2897999342a0SCorentin Chary {
2898999342a0SCorentin Chary     int i, j;
2899999342a0SCorentin Chary     double total = 0;
2900999342a0SCorentin Chary     int num = 0;
2901999342a0SCorentin Chary 
29025a3804dbSMarc-André Lureau     x =  QEMU_ALIGN_DOWN(x, VNC_STAT_RECT);
29035a3804dbSMarc-André Lureau     y =  QEMU_ALIGN_DOWN(y, VNC_STAT_RECT);
2904999342a0SCorentin Chary 
2905999342a0SCorentin Chary     for (j = y; j <= y + h; j += VNC_STAT_RECT) {
2906999342a0SCorentin Chary         for (i = x; i <= x + w; i += VNC_STAT_RECT) {
2907999342a0SCorentin Chary             total += vnc_stat_rect(vs->vd, i, j)->freq;
2908999342a0SCorentin Chary             num++;
2909999342a0SCorentin Chary         }
2910999342a0SCorentin Chary     }
2911999342a0SCorentin Chary 
2912999342a0SCorentin Chary     if (num) {
2913999342a0SCorentin Chary         return total / num;
2914999342a0SCorentin Chary     } else {
2915999342a0SCorentin Chary         return 0;
2916999342a0SCorentin Chary     }
2917999342a0SCorentin Chary }
2918999342a0SCorentin Chary 
2919999342a0SCorentin Chary static void vnc_rect_updated(VncDisplay *vd, int x, int y, struct timeval * tv)
2920999342a0SCorentin Chary {
2921999342a0SCorentin Chary     VncRectStat *rect;
2922999342a0SCorentin Chary 
2923999342a0SCorentin Chary     rect = vnc_stat_rect(vd, x, y);
2924999342a0SCorentin Chary     if (rect->updated) {
2925999342a0SCorentin Chary         return ;
2926999342a0SCorentin Chary     }
2927999342a0SCorentin Chary     rect->times[rect->idx] = *tv;
2928999342a0SCorentin Chary     rect->idx = (rect->idx + 1) % ARRAY_SIZE(rect->times);
2929999342a0SCorentin Chary     rect->updated = true;
2930999342a0SCorentin Chary }
2931999342a0SCorentin Chary 
29323e230dd2SCorentin Chary static int vnc_refresh_server_surface(VncDisplay *vd)
29333e230dd2SCorentin Chary {
2934bea60dd7SPeter Lieven     int width = MIN(pixman_image_get_width(vd->guest.fb),
2935bea60dd7SPeter Lieven                     pixman_image_get_width(vd->server));
2936bea60dd7SPeter Lieven     int height = MIN(pixman_image_get_height(vd->guest.fb),
2937bea60dd7SPeter Lieven                      pixman_image_get_height(vd->server));
2938eb8934b0SGerd Hoffmann     int cmp_bytes, server_stride, line_bytes, guest_ll, guest_stride, y = 0;
293912b316d4SPeter Lieven     uint8_t *guest_row0 = NULL, *server_row0;
29403e230dd2SCorentin Chary     VncState *vs;
29413e230dd2SCorentin Chary     int has_dirty = 0;
29429f64916dSGerd Hoffmann     pixman_image_t *tmpbuf = NULL;
29433e230dd2SCorentin Chary 
294480e0c8c3SCorentin Chary     struct timeval tv = { 0, 0 };
2945999342a0SCorentin Chary 
294680e0c8c3SCorentin Chary     if (!vd->non_adaptive) {
2947999342a0SCorentin Chary         gettimeofday(&tv, NULL);
29487d964c9dSCorentin Chary         has_dirty = vnc_update_stats(vd, &tv);
294980e0c8c3SCorentin Chary     }
2950999342a0SCorentin Chary 
29513e230dd2SCorentin Chary     /*
29523e230dd2SCorentin Chary      * Walk through the guest dirty map.
29533e230dd2SCorentin Chary      * Check and copy modified bits from guest to server surface.
29543e230dd2SCorentin Chary      * Update server dirty map.
29553e230dd2SCorentin Chary      */
2956bea60dd7SPeter Lieven     server_row0 = (uint8_t *)pixman_image_get_data(vd->server);
2957eb8934b0SGerd Hoffmann     server_stride = guest_stride = guest_ll =
2958eb8934b0SGerd Hoffmann         pixman_image_get_stride(vd->server);
2959bea60dd7SPeter Lieven     cmp_bytes = MIN(VNC_DIRTY_PIXELS_PER_BIT * VNC_SERVER_FB_BYTES,
2960bea60dd7SPeter Lieven                     server_stride);
29619f64916dSGerd Hoffmann     if (vd->guest.format != VNC_SERVER_FB_FORMAT) {
29629f64916dSGerd Hoffmann         int width = pixman_image_get_width(vd->server);
29639f64916dSGerd Hoffmann         tmpbuf = qemu_pixman_linebuf_create(VNC_SERVER_FB_FORMAT, width);
296412b316d4SPeter Lieven     } else {
2965eb8934b0SGerd Hoffmann         int guest_bpp =
2966eb8934b0SGerd Hoffmann             PIXMAN_FORMAT_BPP(pixman_image_get_format(vd->guest.fb));
296712b316d4SPeter Lieven         guest_row0 = (uint8_t *)pixman_image_get_data(vd->guest.fb);
296812b316d4SPeter Lieven         guest_stride = pixman_image_get_stride(vd->guest.fb);
2969949ed4c2SPhilippe Mathieu-Daudé         guest_ll = pixman_image_get_width(vd->guest.fb)
2970949ed4c2SPhilippe Mathieu-Daudé                    * DIV_ROUND_UP(guest_bpp, 8);
29719f64916dSGerd Hoffmann     }
2972eb8934b0SGerd Hoffmann     line_bytes = MIN(server_stride, guest_ll);
297312b316d4SPeter Lieven 
297412b316d4SPeter Lieven     for (;;) {
29753e230dd2SCorentin Chary         int x;
297612b316d4SPeter Lieven         uint8_t *guest_ptr, *server_ptr;
297712b316d4SPeter Lieven         unsigned long offset = find_next_bit((unsigned long *) &vd->guest.dirty,
297812b316d4SPeter Lieven                                              height * VNC_DIRTY_BPL(&vd->guest),
297912b316d4SPeter Lieven                                              y * VNC_DIRTY_BPL(&vd->guest));
298012b316d4SPeter Lieven         if (offset == height * VNC_DIRTY_BPL(&vd->guest)) {
298112b316d4SPeter Lieven             /* no more dirty bits */
298212b316d4SPeter Lieven             break;
298312b316d4SPeter Lieven         }
298412b316d4SPeter Lieven         y = offset / VNC_DIRTY_BPL(&vd->guest);
298512b316d4SPeter Lieven         x = offset % VNC_DIRTY_BPL(&vd->guest);
298612b316d4SPeter Lieven 
298712b316d4SPeter Lieven         server_ptr = server_row0 + y * server_stride + x * cmp_bytes;
29883e230dd2SCorentin Chary 
29899f64916dSGerd Hoffmann         if (vd->guest.format != VNC_SERVER_FB_FORMAT) {
2990bc210eb1SGerd Hoffmann             qemu_pixman_linebuf_fill(tmpbuf, vd->guest.fb, width, 0, y);
29919f64916dSGerd Hoffmann             guest_ptr = (uint8_t *)pixman_image_get_data(tmpbuf);
29929f64916dSGerd Hoffmann         } else {
299312b316d4SPeter Lieven             guest_ptr = guest_row0 + y * guest_stride;
29949f64916dSGerd Hoffmann         }
299512b316d4SPeter Lieven         guest_ptr += x * cmp_bytes;
29963e230dd2SCorentin Chary 
299712b316d4SPeter Lieven         for (; x < DIV_ROUND_UP(width, VNC_DIRTY_PIXELS_PER_BIT);
299812b316d4SPeter Lieven              x++, guest_ptr += cmp_bytes, server_ptr += cmp_bytes) {
2999bea60dd7SPeter Lieven             int _cmp_bytes = cmp_bytes;
300012b316d4SPeter Lieven             if (!test_and_clear_bit(x, vd->guest.dirty[y])) {
30013e230dd2SCorentin Chary                 continue;
3002b4c85ddcSPeter Lieven             }
3003eb8934b0SGerd Hoffmann             if ((x + 1) * cmp_bytes > line_bytes) {
3004eb8934b0SGerd Hoffmann                 _cmp_bytes = line_bytes - x * cmp_bytes;
3005bea60dd7SPeter Lieven             }
3006eb8934b0SGerd Hoffmann             assert(_cmp_bytes >= 0);
3007bea60dd7SPeter Lieven             if (memcmp(server_ptr, guest_ptr, _cmp_bytes) == 0) {
30083e230dd2SCorentin Chary                 continue;
3009b4c85ddcSPeter Lieven             }
3010bea60dd7SPeter Lieven             memcpy(server_ptr, guest_ptr, _cmp_bytes);
301112b316d4SPeter Lieven             if (!vd->non_adaptive) {
301212b316d4SPeter Lieven                 vnc_rect_updated(vd, x * VNC_DIRTY_PIXELS_PER_BIT,
301312b316d4SPeter Lieven                                  y, &tv);
301412b316d4SPeter Lieven             }
30153e230dd2SCorentin Chary             QTAILQ_FOREACH(vs, &vd->clients, next) {
301612b316d4SPeter Lieven                 set_bit(x, vs->dirty[y]);
30173e230dd2SCorentin Chary             }
30183e230dd2SCorentin Chary             has_dirty++;
30193e230dd2SCorentin Chary         }
302012b316d4SPeter Lieven 
302112b316d4SPeter Lieven         y++;
30223e230dd2SCorentin Chary     }
30239f64916dSGerd Hoffmann     qemu_pixman_image_unref(tmpbuf);
30243e230dd2SCorentin Chary     return has_dirty;
30253e230dd2SCorentin Chary }
30263e230dd2SCorentin Chary 
30270f7b2864SGerd Hoffmann static void vnc_refresh(DisplayChangeListener *dcl)
30283e230dd2SCorentin Chary {
30290f7b2864SGerd Hoffmann     VncDisplay *vd = container_of(dcl, VncDisplay, dcl);
30303e230dd2SCorentin Chary     VncState *vs, *vn;
30313e230dd2SCorentin Chary     int has_dirty, rects = 0;
30323e230dd2SCorentin Chary 
30339d6b2070SChenLiang     if (QTAILQ_EMPTY(&vd->clients)) {
30349d6b2070SChenLiang         update_displaychangelistener(&vd->dcl, VNC_REFRESH_INTERVAL_MAX);
30359d6b2070SChenLiang         return;
30369d6b2070SChenLiang     }
30379d6b2070SChenLiang 
30381d0d59feSGerd Hoffmann     graphic_hw_update(vd->dcl.con);
30393e230dd2SCorentin Chary 
3040bd023f95SCorentin Chary     if (vnc_trylock_display(vd)) {
30410f7b2864SGerd Hoffmann         update_displaychangelistener(&vd->dcl, VNC_REFRESH_INTERVAL_BASE);
3042bd023f95SCorentin Chary         return;
3043bd023f95SCorentin Chary     }
3044bd023f95SCorentin Chary 
30453e230dd2SCorentin Chary     has_dirty = vnc_refresh_server_surface(vd);
3046bd023f95SCorentin Chary     vnc_unlock_display(vd);
30473e230dd2SCorentin Chary 
30483e230dd2SCorentin Chary     QTAILQ_FOREACH_SAFE(vs, &vd->clients, next, vn) {
30496af998dbSDaniel P. Berrange         rects += vnc_update_client(vs, has_dirty);
30503e230dd2SCorentin Chary         /* vs might be free()ed here */
30513e230dd2SCorentin Chary     }
3052bd023f95SCorentin Chary 
30533e230dd2SCorentin Chary     if (has_dirty && rects) {
30540f7b2864SGerd Hoffmann         vd->dcl.update_interval /= 2;
30550f7b2864SGerd Hoffmann         if (vd->dcl.update_interval < VNC_REFRESH_INTERVAL_BASE) {
30560f7b2864SGerd Hoffmann             vd->dcl.update_interval = VNC_REFRESH_INTERVAL_BASE;
30570f7b2864SGerd Hoffmann         }
30583e230dd2SCorentin Chary     } else {
30590f7b2864SGerd Hoffmann         vd->dcl.update_interval += VNC_REFRESH_INTERVAL_INC;
30600f7b2864SGerd Hoffmann         if (vd->dcl.update_interval > VNC_REFRESH_INTERVAL_MAX) {
30610f7b2864SGerd Hoffmann             vd->dcl.update_interval = VNC_REFRESH_INTERVAL_MAX;
30623e230dd2SCorentin Chary         }
30633e230dd2SCorentin Chary     }
30643e230dd2SCorentin Chary }
30653e230dd2SCorentin Chary 
306604d2529dSDaniel P. Berrange static void vnc_connect(VncDisplay *vd, QIOChannelSocket *sioc,
30672c8cf549SMichael Tokarev                         bool skipauth, bool websocket)
30683e230dd2SCorentin Chary {
3069fedf0d35SMarkus Armbruster     VncState *vs = g_new0(VncState, 1);
307090cd03a3SDaniel P. Berrange     bool first_client = QTAILQ_EMPTY(&vd->clients);
30717d964c9dSCorentin Chary     int i;
30727d964c9dSCorentin Chary 
3073ad6374c4SDaniel P. Berrange     trace_vnc_client_connect(vs, sioc);
3074f31f9c10SGerd Hoffmann     vs->magic = VNC_MAGIC;
307504d2529dSDaniel P. Berrange     vs->sioc = sioc;
307604d2529dSDaniel P. Berrange     object_ref(OBJECT(vs->sioc));
307704d2529dSDaniel P. Berrange     vs->ioc = QIO_CHANNEL(sioc);
307804d2529dSDaniel P. Berrange     object_ref(OBJECT(vs->ioc));
3079d616ccc5SGerd Hoffmann     vs->vd = vd;
30807e7e2ebcSDaniel P. Berrange 
308104d2529dSDaniel P. Berrange     buffer_init(&vs->input,          "vnc-input/%p", sioc);
308204d2529dSDaniel P. Berrange     buffer_init(&vs->output,         "vnc-output/%p", sioc);
308304d2529dSDaniel P. Berrange     buffer_init(&vs->jobs_buffer,    "vnc-jobs_buffer/%p", sioc);
3084543b9580SGerd Hoffmann 
308504d2529dSDaniel P. Berrange     buffer_init(&vs->tight.tight,    "vnc-tight/%p", sioc);
308604d2529dSDaniel P. Berrange     buffer_init(&vs->tight.zlib,     "vnc-tight-zlib/%p", sioc);
308704d2529dSDaniel P. Berrange     buffer_init(&vs->tight.gradient, "vnc-tight-gradient/%p", sioc);
3088543b9580SGerd Hoffmann #ifdef CONFIG_VNC_JPEG
308904d2529dSDaniel P. Berrange     buffer_init(&vs->tight.jpeg,     "vnc-tight-jpeg/%p", sioc);
3090543b9580SGerd Hoffmann #endif
3091543b9580SGerd Hoffmann #ifdef CONFIG_VNC_PNG
309204d2529dSDaniel P. Berrange     buffer_init(&vs->tight.png,      "vnc-tight-png/%p", sioc);
3093543b9580SGerd Hoffmann #endif
309404d2529dSDaniel P. Berrange     buffer_init(&vs->zlib.zlib,      "vnc-zlib/%p", sioc);
309504d2529dSDaniel P. Berrange     buffer_init(&vs->zrle.zrle,      "vnc-zrle/%p", sioc);
309604d2529dSDaniel P. Berrange     buffer_init(&vs->zrle.fb,        "vnc-zrle-fb/%p", sioc);
309704d2529dSDaniel P. Berrange     buffer_init(&vs->zrle.zlib,      "vnc-zrle-zlib/%p", sioc);
3098543b9580SGerd Hoffmann 
30997e7e2ebcSDaniel P. Berrange     if (skipauth) {
31007e7e2ebcSDaniel P. Berrange 	vs->auth = VNC_AUTH_NONE;
31017e7e2ebcSDaniel P. Berrange 	vs->subauth = VNC_AUTH_INVALID;
31027e7e2ebcSDaniel P. Berrange     } else {
3103f9148c8aSDaniel P. Berrange         if (websocket) {
3104f9148c8aSDaniel P. Berrange             vs->auth = vd->ws_auth;
3105f9148c8aSDaniel P. Berrange             vs->subauth = VNC_AUTH_INVALID;
3106f9148c8aSDaniel P. Berrange         } else {
31077e7e2ebcSDaniel P. Berrange             vs->auth = vd->auth;
31087e7e2ebcSDaniel P. Berrange             vs->subauth = vd->subauth;
31097e7e2ebcSDaniel P. Berrange         }
3110f9148c8aSDaniel P. Berrange     }
311104d2529dSDaniel P. Berrange     VNC_DEBUG("Client sioc=%p ws=%d auth=%d subauth=%d\n",
311204d2529dSDaniel P. Berrange               sioc, websocket, vs->auth, vs->subauth);
31137e7e2ebcSDaniel P. Berrange 
31147267c094SAnthony Liguori     vs->lossy_rect = g_malloc0(VNC_STAT_ROWS * sizeof (*vs->lossy_rect));
31157d964c9dSCorentin Chary     for (i = 0; i < VNC_STAT_ROWS; ++i) {
3116fedf0d35SMarkus Armbruster         vs->lossy_rect[i] = g_new0(uint8_t, VNC_STAT_COLS);
31177d964c9dSCorentin Chary     }
31183e230dd2SCorentin Chary 
311904d2529dSDaniel P. Berrange     VNC_DEBUG("New client on socket %p\n", vs->sioc);
31200f7b2864SGerd Hoffmann     update_displaychangelistener(&vd->dcl, VNC_REFRESH_INTERVAL_BASE);
312104d2529dSDaniel P. Berrange     qio_channel_set_blocking(vs->ioc, false, NULL);
3122a75d6f07SBrandon Carpenter     if (vs->ioc_tag) {
3123a75d6f07SBrandon Carpenter         g_source_remove(vs->ioc_tag);
3124a75d6f07SBrandon Carpenter     }
31257536ee4bSTim Hardeck     if (websocket) {
31267536ee4bSTim Hardeck         vs->websocket = 1;
312738e5756aSDaniel P. Berrange         if (vd->tlscreds) {
312804d2529dSDaniel P. Berrange             vs->ioc_tag = qio_channel_add_watch(
312904d2529dSDaniel P. Berrange                 vs->ioc, G_IO_IN, vncws_tls_handshake_io, vs, NULL);
31303e305e4aSDaniel P. Berrange         } else {
313104d2529dSDaniel P. Berrange             vs->ioc_tag = qio_channel_add_watch(
313204d2529dSDaniel P. Berrange                 vs->ioc, G_IO_IN, vncws_handshake_io, vs, NULL);
31330057a0d5STim Hardeck         }
313404d2529dSDaniel P. Berrange     } else {
313504d2529dSDaniel P. Berrange         vs->ioc_tag = qio_channel_add_watch(
313604d2529dSDaniel P. Berrange             vs->ioc, G_IO_IN, vnc_client_io, vs, NULL);
31377536ee4bSTim Hardeck     }
31383e230dd2SCorentin Chary 
31393e230dd2SCorentin Chary     vnc_client_cache_addr(vs);
3140fb6ba0d5SWenchao Xia     vnc_qmp_event(vs, QAPI_EVENT_VNC_CONNECTED);
31418cf36489SGerd Hoffmann     vnc_set_share_mode(vs, VNC_SHARE_MODE_CONNECTING);
31423e230dd2SCorentin Chary 
31433e230dd2SCorentin Chary     vs->last_x = -1;
31443e230dd2SCorentin Chary     vs->last_y = -1;
31453e230dd2SCorentin Chary 
31463e230dd2SCorentin Chary     vs->as.freq = 44100;
31473e230dd2SCorentin Chary     vs->as.nchannels = 2;
31483e230dd2SCorentin Chary     vs->as.fmt = AUD_FMT_S16;
31493e230dd2SCorentin Chary     vs->as.endianness = 0;
31503e230dd2SCorentin Chary 
3151bd023f95SCorentin Chary     qemu_mutex_init(&vs->output_mutex);
3152175b2a6eSCorentin Chary     vs->bh = qemu_bh_new(vnc_jobs_bh, vs);
3153bd023f95SCorentin Chary 
3154e5f34cddSGerd Hoffmann     QTAILQ_INSERT_TAIL(&vd->clients, vs, next);
3155c7628bffSGerd Hoffmann     if (first_client) {
3156c7628bffSGerd Hoffmann         vnc_update_server_surface(vd);
3157c7628bffSGerd Hoffmann     }
31583e230dd2SCorentin Chary 
31591d0d59feSGerd Hoffmann     graphic_hw_update(vd->dcl.con);
31603e230dd2SCorentin Chary 
316190cd03a3SDaniel P. Berrange     if (!vs->websocket) {
3162dbee9897SDaniel P. Berrange         vnc_start_protocol(vs);
316390cd03a3SDaniel P. Berrange     }
316490cd03a3SDaniel P. Berrange 
316590cd03a3SDaniel P. Berrange     if (vd->num_connecting > vd->connections_limit) {
316690cd03a3SDaniel P. Berrange         QTAILQ_FOREACH(vs, &vd->clients, next) {
316790cd03a3SDaniel P. Berrange             if (vs->share_mode == VNC_SHARE_MODE_CONNECTING) {
316890cd03a3SDaniel P. Berrange                 vnc_disconnect_start(vs);
316990cd03a3SDaniel P. Berrange                 return;
317090cd03a3SDaniel P. Berrange             }
317190cd03a3SDaniel P. Berrange         }
317290cd03a3SDaniel P. Berrange     }
317390cd03a3SDaniel P. Berrange }
317490cd03a3SDaniel P. Berrange 
3175dbee9897SDaniel P. Berrange void vnc_start_protocol(VncState *vs)
317690cd03a3SDaniel P. Berrange {
31773e230dd2SCorentin Chary     vnc_write(vs, "RFB 003.008\n", 12);
31783e230dd2SCorentin Chary     vnc_flush(vs);
31793e230dd2SCorentin Chary     vnc_read_when(vs, protocol_version, 12);
31803e230dd2SCorentin Chary 
31813e230dd2SCorentin Chary     vs->mouse_mode_notifier.notify = check_pointer_type_change;
31823e230dd2SCorentin Chary     qemu_add_mouse_mode_change_notifier(&vs->mouse_mode_notifier);
31833e230dd2SCorentin Chary }
31843e230dd2SCorentin Chary 
318513e1d0e7SDaniel P. Berrange static void vnc_listen_io(QIONetListener *listener,
318613e1d0e7SDaniel P. Berrange                           QIOChannelSocket *cioc,
318704d2529dSDaniel P. Berrange                           void *opaque)
31883e230dd2SCorentin Chary {
3189bf01c179SDaniel P. Berrange     VncDisplay *vd = opaque;
319013e1d0e7SDaniel P. Berrange     bool isWebsock = listener == vd->wslistener;
31914ee74fa7SDaniel P. Berrange 
319213e1d0e7SDaniel P. Berrange     qio_channel_set_name(QIO_CHANNEL(cioc),
31934ee74fa7SDaniel P. Berrange                          isWebsock ? "vnc-ws-server" : "vnc-server");
319413e1d0e7SDaniel P. Berrange     qio_channel_set_delay(QIO_CHANNEL(cioc), false);
319513e1d0e7SDaniel P. Berrange     vnc_connect(vd, cioc, false, isWebsock);
31967536ee4bSTim Hardeck }
31977536ee4bSTim Hardeck 
31987c20b4a3SGerd Hoffmann static const DisplayChangeListenerOps dcl_ops = {
31997c20b4a3SGerd Hoffmann     .dpy_name             = "vnc",
32000f7b2864SGerd Hoffmann     .dpy_refresh          = vnc_refresh,
32017c20b4a3SGerd Hoffmann     .dpy_gfx_update       = vnc_dpy_update,
3202c12aeb86SGerd Hoffmann     .dpy_gfx_switch       = vnc_dpy_switch,
320334da30afSBenjamin Herrenschmidt     .dpy_gfx_check_format = qemu_pixman_check_format,
32047c20b4a3SGerd Hoffmann     .dpy_mouse_set        = vnc_mouse_set,
32057c20b4a3SGerd Hoffmann     .dpy_cursor_define    = vnc_dpy_cursor_define,
32067c20b4a3SGerd Hoffmann };
32077c20b4a3SGerd Hoffmann 
320814f7143eSGerd Hoffmann void vnc_display_init(const char *id)
32093e230dd2SCorentin Chary {
3210bf01c179SDaniel P. Berrange     VncDisplay *vd;
32114db14629SGerd Hoffmann 
32124db14629SGerd Hoffmann     if (vnc_display_find(id) != NULL) {
32134db14629SGerd Hoffmann         return;
32144db14629SGerd Hoffmann     }
3215bf01c179SDaniel P. Berrange     vd = g_malloc0(sizeof(*vd));
32163e230dd2SCorentin Chary 
3217bf01c179SDaniel P. Berrange     vd->id = strdup(id);
3218bf01c179SDaniel P. Berrange     QTAILQ_INSERT_TAIL(&vnc_displays, vd, next);
32193e230dd2SCorentin Chary 
3220bf01c179SDaniel P. Berrange     QTAILQ_INIT(&vd->clients);
3221bf01c179SDaniel P. Berrange     vd->expires = TIME_MAX;
32223e230dd2SCorentin Chary 
322340066175SGerd Hoffmann     if (keyboard_layout) {
322440066175SGerd Hoffmann         trace_vnc_key_map_init(keyboard_layout);
3225bf01c179SDaniel P. Berrange         vd->kbd_layout = init_keyboard_layout(name2keysym, keyboard_layout);
322640066175SGerd Hoffmann     } else {
3227bf01c179SDaniel P. Berrange         vd->kbd_layout = init_keyboard_layout(name2keysym, "en-us");
322840066175SGerd Hoffmann     }
32293e230dd2SCorentin Chary 
3230bf01c179SDaniel P. Berrange     if (!vd->kbd_layout) {
32313e230dd2SCorentin Chary         exit(1);
3232bf01c179SDaniel P. Berrange     }
32333e230dd2SCorentin Chary 
3234bf01c179SDaniel P. Berrange     vd->share_policy = VNC_SHARE_POLICY_ALLOW_EXCLUSIVE;
3235bf01c179SDaniel P. Berrange     vd->connections_limit = 32;
323612e29b16SDaniel P. Berrange 
3237bf01c179SDaniel P. Berrange     qemu_mutex_init(&vd->mutex);
3238bd023f95SCorentin Chary     vnc_start_worker_thread();
3239bd023f95SCorentin Chary 
3240bf01c179SDaniel P. Berrange     vd->dcl.ops = &dcl_ops;
3241bf01c179SDaniel P. Berrange     register_displaychangelistener(&vd->dcl);
32423e230dd2SCorentin Chary }
32433e230dd2SCorentin Chary 
32443e230dd2SCorentin Chary 
3245bf01c179SDaniel P. Berrange static void vnc_display_close(VncDisplay *vd)
32463e230dd2SCorentin Chary {
3247bf01c179SDaniel P. Berrange     if (!vd) {
32483e230dd2SCorentin Chary         return;
324904d2529dSDaniel P. Berrange     }
3250bf01c179SDaniel P. Berrange     vd->is_unix = false;
32514ee74fa7SDaniel P. Berrange 
325213e1d0e7SDaniel P. Berrange     if (vd->listener) {
325313e1d0e7SDaniel P. Berrange         qio_net_listener_disconnect(vd->listener);
325413e1d0e7SDaniel P. Berrange         object_unref(OBJECT(vd->listener));
325504d2529dSDaniel P. Berrange     }
325613e1d0e7SDaniel P. Berrange     vd->listener = NULL;
325713e1d0e7SDaniel P. Berrange 
325813e1d0e7SDaniel P. Berrange     if (vd->wslistener) {
325913e1d0e7SDaniel P. Berrange         qio_net_listener_disconnect(vd->wslistener);
326013e1d0e7SDaniel P. Berrange         object_unref(OBJECT(vd->wslistener));
32617536ee4bSTim Hardeck     }
326213e1d0e7SDaniel P. Berrange     vd->wslistener = NULL;
32634ee74fa7SDaniel P. Berrange 
3264bf01c179SDaniel P. Berrange     vd->auth = VNC_AUTH_INVALID;
3265bf01c179SDaniel P. Berrange     vd->subauth = VNC_AUTH_INVALID;
3266bf01c179SDaniel P. Berrange     if (vd->tlscreds) {
3267bf01c179SDaniel P. Berrange         object_unparent(OBJECT(vd->tlscreds));
3268bf01c179SDaniel P. Berrange         vd->tlscreds = NULL;
3269bf01c179SDaniel P. Berrange     }
3270bf01c179SDaniel P. Berrange     g_free(vd->tlsaclname);
3271bf01c179SDaniel P. Berrange     vd->tlsaclname = NULL;
3272a54f0d2bSPierre Ossman     if (vd->lock_key_sync) {
3273a54f0d2bSPierre Ossman         qemu_remove_led_event_handler(vd->led);
32742dc120beSGerd Hoffmann         vd->led = NULL;
3275a54f0d2bSPierre Ossman     }
32763e230dd2SCorentin Chary }
32773e230dd2SCorentin Chary 
327814f7143eSGerd Hoffmann int vnc_display_password(const char *id, const char *password)
32791cd20f8bSAnthony Liguori {
3280bf01c179SDaniel P. Berrange     VncDisplay *vd = vnc_display_find(id);
32811cd20f8bSAnthony Liguori 
3282bf01c179SDaniel P. Berrange     if (!vd) {
3283a6aa9d3eSLuiz Capitulino         return -EINVAL;
32841cd20f8bSAnthony Liguori     }
3285bf01c179SDaniel P. Berrange     if (vd->auth == VNC_AUTH_NONE) {
3286cf864569SGerd Hoffmann         error_printf_unless_qmp("If you want use passwords please enable "
32877ea7d36eSMarkus Armbruster                                 "password auth using '-vnc ${dpy},password'.\n");
3288cf864569SGerd Hoffmann         return -EINVAL;
32891cd20f8bSAnthony Liguori     }
32901cd20f8bSAnthony Liguori 
3291bf01c179SDaniel P. Berrange     g_free(vd->password);
3292bf01c179SDaniel P. Berrange     vd->password = g_strdup(password);
3293a6aa9d3eSLuiz Capitulino 
3294a6aa9d3eSLuiz Capitulino     return 0;
32953e230dd2SCorentin Chary }
32963e230dd2SCorentin Chary 
329714f7143eSGerd Hoffmann int vnc_display_pw_expire(const char *id, time_t expires)
32983c9405a0SGerd Hoffmann {
3299bf01c179SDaniel P. Berrange     VncDisplay *vd = vnc_display_find(id);
33003c9405a0SGerd Hoffmann 
3301bf01c179SDaniel P. Berrange     if (!vd) {
33021643f2b2SGerd Hoffmann         return -EINVAL;
33031643f2b2SGerd Hoffmann     }
33041643f2b2SGerd Hoffmann 
3305bf01c179SDaniel P. Berrange     vd->expires = expires;
33063c9405a0SGerd Hoffmann     return 0;
33073c9405a0SGerd Hoffmann }
33083c9405a0SGerd Hoffmann 
3309bf01c179SDaniel P. Berrange static void vnc_display_print_local_addr(VncDisplay *vd)
33103e230dd2SCorentin Chary {
3311bd269ebcSMarkus Armbruster     SocketAddress *addr;
331204d2529dSDaniel P. Berrange     Error *err = NULL;
33133e230dd2SCorentin Chary 
331413e1d0e7SDaniel P. Berrange     if (!vd->listener || !vd->listener->nsioc) {
33154ee74fa7SDaniel P. Berrange         return;
33164ee74fa7SDaniel P. Berrange     }
33174ee74fa7SDaniel P. Berrange 
331813e1d0e7SDaniel P. Berrange     addr = qio_channel_socket_get_local_address(vd->listener->sioc[0], &err);
331904d2529dSDaniel P. Berrange     if (!addr) {
332033df7bf3SPaolo Bonzini         return;
332104d2529dSDaniel P. Berrange     }
332204d2529dSDaniel P. Berrange 
3323bd269ebcSMarkus Armbruster     if (addr->type != SOCKET_ADDRESS_TYPE_INET) {
3324bd269ebcSMarkus Armbruster         qapi_free_SocketAddress(addr);
332533df7bf3SPaolo Bonzini         return;
332604d2529dSDaniel P. Berrange     }
332733df7bf3SPaolo Bonzini     error_printf_unless_qmp("VNC server running on %s:%s\n",
3328bd269ebcSMarkus Armbruster                             addr->u.inet.host,
3329bd269ebcSMarkus Armbruster                             addr->u.inet.port);
3330bd269ebcSMarkus Armbruster     qapi_free_SocketAddress(addr);
33313e230dd2SCorentin Chary }
33323e230dd2SCorentin Chary 
33334db14629SGerd Hoffmann static QemuOptsList qemu_vnc_opts = {
33344db14629SGerd Hoffmann     .name = "vnc",
33354db14629SGerd Hoffmann     .head = QTAILQ_HEAD_INITIALIZER(qemu_vnc_opts.head),
33364db14629SGerd Hoffmann     .implied_opt_name = "vnc",
33374db14629SGerd Hoffmann     .desc = {
33384db14629SGerd Hoffmann         {
33394db14629SGerd Hoffmann             .name = "vnc",
33404db14629SGerd Hoffmann             .type = QEMU_OPT_STRING,
33414db14629SGerd Hoffmann         },{
33424db14629SGerd Hoffmann             .name = "websocket",
33434db14629SGerd Hoffmann             .type = QEMU_OPT_STRING,
33444db14629SGerd Hoffmann         },{
33453e305e4aSDaniel P. Berrange             .name = "tls-creds",
33463e305e4aSDaniel P. Berrange             .type = QEMU_OPT_STRING,
33473e305e4aSDaniel P. Berrange         },{
33484db14629SGerd Hoffmann             .name = "share",
33494db14629SGerd Hoffmann             .type = QEMU_OPT_STRING,
33504db14629SGerd Hoffmann         },{
33511d0d59feSGerd Hoffmann             .name = "display",
33521d0d59feSGerd Hoffmann             .type = QEMU_OPT_STRING,
33531d0d59feSGerd Hoffmann         },{
33541d0d59feSGerd Hoffmann             .name = "head",
33551d0d59feSGerd Hoffmann             .type = QEMU_OPT_NUMBER,
33561d0d59feSGerd Hoffmann         },{
3357e5f34cddSGerd Hoffmann             .name = "connections",
3358e5f34cddSGerd Hoffmann             .type = QEMU_OPT_NUMBER,
3359e5f34cddSGerd Hoffmann         },{
336088428b7aSGonglei             .name = "to",
336188428b7aSGonglei             .type = QEMU_OPT_NUMBER,
336288428b7aSGonglei         },{
336388428b7aSGonglei             .name = "ipv4",
336488428b7aSGonglei             .type = QEMU_OPT_BOOL,
336588428b7aSGonglei         },{
336688428b7aSGonglei             .name = "ipv6",
336788428b7aSGonglei             .type = QEMU_OPT_BOOL,
336888428b7aSGonglei         },{
33694db14629SGerd Hoffmann             .name = "password",
33704db14629SGerd Hoffmann             .type = QEMU_OPT_BOOL,
33714db14629SGerd Hoffmann         },{
33724db14629SGerd Hoffmann             .name = "reverse",
33734db14629SGerd Hoffmann             .type = QEMU_OPT_BOOL,
33744db14629SGerd Hoffmann         },{
33754db14629SGerd Hoffmann             .name = "lock-key-sync",
33764db14629SGerd Hoffmann             .type = QEMU_OPT_BOOL,
33774db14629SGerd Hoffmann         },{
3378c5ce8333SGerd Hoffmann             .name = "key-delay-ms",
3379c5ce8333SGerd Hoffmann             .type = QEMU_OPT_NUMBER,
3380c5ce8333SGerd Hoffmann         },{
33814db14629SGerd Hoffmann             .name = "sasl",
33824db14629SGerd Hoffmann             .type = QEMU_OPT_BOOL,
33834db14629SGerd Hoffmann         },{
33844db14629SGerd Hoffmann             .name = "acl",
33854db14629SGerd Hoffmann             .type = QEMU_OPT_BOOL,
33864db14629SGerd Hoffmann         },{
33874db14629SGerd Hoffmann             .name = "lossy",
33884db14629SGerd Hoffmann             .type = QEMU_OPT_BOOL,
33894db14629SGerd Hoffmann         },{
33904db14629SGerd Hoffmann             .name = "non-adaptive",
33914db14629SGerd Hoffmann             .type = QEMU_OPT_BOOL,
33924db14629SGerd Hoffmann         },
33934db14629SGerd Hoffmann         { /* end of list */ }
33944db14629SGerd Hoffmann     },
33954db14629SGerd Hoffmann };
33964db14629SGerd Hoffmann 
33970dd72e15SDaniel P. Berrange 
33983e305e4aSDaniel P. Berrange static int
3399eda24e18SDaniel P. Berrange vnc_display_setup_auth(int *auth,
3400eda24e18SDaniel P. Berrange                        int *subauth,
3401eda24e18SDaniel P. Berrange                        QCryptoTLSCreds *tlscreds,
34020dd72e15SDaniel P. Berrange                        bool password,
34030dd72e15SDaniel P. Berrange                        bool sasl,
34043e305e4aSDaniel P. Berrange                        bool websocket,
34053e305e4aSDaniel P. Berrange                        Error **errp)
34060dd72e15SDaniel P. Berrange {
34070dd72e15SDaniel P. Berrange     /*
34080dd72e15SDaniel P. Berrange      * We have a choice of 3 authentication options
34090dd72e15SDaniel P. Berrange      *
34100dd72e15SDaniel P. Berrange      *   1. none
34110dd72e15SDaniel P. Berrange      *   2. vnc
34120dd72e15SDaniel P. Berrange      *   3. sasl
34130dd72e15SDaniel P. Berrange      *
34140dd72e15SDaniel P. Berrange      * The channel can be run in 2 modes
34150dd72e15SDaniel P. Berrange      *
34160dd72e15SDaniel P. Berrange      *   1. clear
34170dd72e15SDaniel P. Berrange      *   2. tls
34180dd72e15SDaniel P. Berrange      *
34190dd72e15SDaniel P. Berrange      * And TLS can use 2 types of credentials
34200dd72e15SDaniel P. Berrange      *
34210dd72e15SDaniel P. Berrange      *   1. anon
34220dd72e15SDaniel P. Berrange      *   2. x509
34230dd72e15SDaniel P. Berrange      *
34240dd72e15SDaniel P. Berrange      * We thus have 9 possible logical combinations
34250dd72e15SDaniel P. Berrange      *
34260dd72e15SDaniel P. Berrange      *   1. clear + none
34270dd72e15SDaniel P. Berrange      *   2. clear + vnc
34280dd72e15SDaniel P. Berrange      *   3. clear + sasl
34290dd72e15SDaniel P. Berrange      *   4. tls + anon + none
34300dd72e15SDaniel P. Berrange      *   5. tls + anon + vnc
34310dd72e15SDaniel P. Berrange      *   6. tls + anon + sasl
34320dd72e15SDaniel P. Berrange      *   7. tls + x509 + none
34330dd72e15SDaniel P. Berrange      *   8. tls + x509 + vnc
34340dd72e15SDaniel P. Berrange      *   9. tls + x509 + sasl
34350dd72e15SDaniel P. Berrange      *
34360dd72e15SDaniel P. Berrange      * These need to be mapped into the VNC auth schemes
34370dd72e15SDaniel P. Berrange      * in an appropriate manner. In regular VNC, all the
34380dd72e15SDaniel P. Berrange      * TLS options get mapped into VNC_AUTH_VENCRYPT
34390dd72e15SDaniel P. Berrange      * sub-auth types.
3440f9148c8aSDaniel P. Berrange      *
3441f9148c8aSDaniel P. Berrange      * In websockets, the https:// protocol already provides
3442f9148c8aSDaniel P. Berrange      * TLS support, so there is no need to make use of the
3443f9148c8aSDaniel P. Berrange      * VeNCrypt extension. Furthermore, websockets browser
3444f9148c8aSDaniel P. Berrange      * clients could not use VeNCrypt even if they wanted to,
3445f9148c8aSDaniel P. Berrange      * as they cannot control when the TLS handshake takes
3446f9148c8aSDaniel P. Berrange      * place. Thus there is no option but to rely on https://,
3447f9148c8aSDaniel P. Berrange      * meaning combinations 4->6 and 7->9 will be mapped to
3448f9148c8aSDaniel P. Berrange      * VNC auth schemes in the same way as combos 1->3.
3449f9148c8aSDaniel P. Berrange      *
3450f9148c8aSDaniel P. Berrange      * Regardless of fact that we have a different mapping to
3451f9148c8aSDaniel P. Berrange      * VNC auth mechs for plain VNC vs websockets VNC, the end
3452f9148c8aSDaniel P. Berrange      * result has the same security characteristics.
34530dd72e15SDaniel P. Berrange      */
3454eda24e18SDaniel P. Berrange     if (websocket || !tlscreds) {
34550dd72e15SDaniel P. Berrange         if (password) {
34560dd72e15SDaniel P. Berrange             VNC_DEBUG("Initializing VNC server with password auth\n");
3457eda24e18SDaniel P. Berrange             *auth = VNC_AUTH_VNC;
34580dd72e15SDaniel P. Berrange         } else if (sasl) {
34590dd72e15SDaniel P. Berrange             VNC_DEBUG("Initializing VNC server with SASL auth\n");
3460eda24e18SDaniel P. Berrange             *auth = VNC_AUTH_SASL;
34610dd72e15SDaniel P. Berrange         } else {
34620dd72e15SDaniel P. Berrange             VNC_DEBUG("Initializing VNC server with no auth\n");
3463eda24e18SDaniel P. Berrange             *auth = VNC_AUTH_NONE;
34640dd72e15SDaniel P. Berrange         }
3465eda24e18SDaniel P. Berrange         *subauth = VNC_AUTH_INVALID;
3466f9148c8aSDaniel P. Berrange     } else {
3467eda24e18SDaniel P. Berrange         bool is_x509 = object_dynamic_cast(OBJECT(tlscreds),
3468eda24e18SDaniel P. Berrange                                            TYPE_QCRYPTO_TLS_CREDS_X509) != NULL;
3469eda24e18SDaniel P. Berrange         bool is_anon = object_dynamic_cast(OBJECT(tlscreds),
3470eda24e18SDaniel P. Berrange                                            TYPE_QCRYPTO_TLS_CREDS_ANON) != NULL;
3471eda24e18SDaniel P. Berrange 
3472eda24e18SDaniel P. Berrange         if (!is_x509 && !is_anon) {
3473eda24e18SDaniel P. Berrange             error_setg(errp,
3474eda24e18SDaniel P. Berrange                        "Unsupported TLS cred type %s",
3475eda24e18SDaniel P. Berrange                        object_get_typename(OBJECT(tlscreds)));
3476eda24e18SDaniel P. Berrange             return -1;
3477eda24e18SDaniel P. Berrange         }
3478eda24e18SDaniel P. Berrange         *auth = VNC_AUTH_VENCRYPT;
3479eda24e18SDaniel P. Berrange         if (password) {
3480eda24e18SDaniel P. Berrange             if (is_x509) {
3481eda24e18SDaniel P. Berrange                 VNC_DEBUG("Initializing VNC server with x509 password auth\n");
3482eda24e18SDaniel P. Berrange                 *subauth = VNC_AUTH_VENCRYPT_X509VNC;
3483eda24e18SDaniel P. Berrange             } else {
3484eda24e18SDaniel P. Berrange                 VNC_DEBUG("Initializing VNC server with TLS password auth\n");
3485eda24e18SDaniel P. Berrange                 *subauth = VNC_AUTH_VENCRYPT_TLSVNC;
3486eda24e18SDaniel P. Berrange             }
3487eda24e18SDaniel P. Berrange 
3488eda24e18SDaniel P. Berrange         } else if (sasl) {
3489eda24e18SDaniel P. Berrange             if (is_x509) {
3490eda24e18SDaniel P. Berrange                 VNC_DEBUG("Initializing VNC server with x509 SASL auth\n");
3491eda24e18SDaniel P. Berrange                 *subauth = VNC_AUTH_VENCRYPT_X509SASL;
3492eda24e18SDaniel P. Berrange             } else {
3493eda24e18SDaniel P. Berrange                 VNC_DEBUG("Initializing VNC server with TLS SASL auth\n");
3494eda24e18SDaniel P. Berrange                 *subauth = VNC_AUTH_VENCRYPT_TLSSASL;
3495eda24e18SDaniel P. Berrange             }
3496eda24e18SDaniel P. Berrange         } else {
3497eda24e18SDaniel P. Berrange             if (is_x509) {
3498eda24e18SDaniel P. Berrange                 VNC_DEBUG("Initializing VNC server with x509 no auth\n");
3499eda24e18SDaniel P. Berrange                 *subauth = VNC_AUTH_VENCRYPT_X509NONE;
3500eda24e18SDaniel P. Berrange             } else {
3501eda24e18SDaniel P. Berrange                 VNC_DEBUG("Initializing VNC server with TLS no auth\n");
3502eda24e18SDaniel P. Berrange                 *subauth = VNC_AUTH_VENCRYPT_TLSNONE;
3503eda24e18SDaniel P. Berrange             }
3504f9148c8aSDaniel P. Berrange         }
35050dd72e15SDaniel P. Berrange     }
35063e305e4aSDaniel P. Berrange     return 0;
35070dd72e15SDaniel P. Berrange }
35080dd72e15SDaniel P. Berrange 
35093e305e4aSDaniel P. Berrange 
3510275e0d61SDaniel P. Berrange static int vnc_display_get_address(const char *addrstr,
3511275e0d61SDaniel P. Berrange                                    bool websocket,
3512e5766eb4SGerd Hoffmann                                    bool reverse,
3513275e0d61SDaniel P. Berrange                                    int displaynum,
3514275e0d61SDaniel P. Berrange                                    int to,
3515275e0d61SDaniel P. Berrange                                    bool has_ipv4,
3516275e0d61SDaniel P. Berrange                                    bool has_ipv6,
3517275e0d61SDaniel P. Berrange                                    bool ipv4,
3518275e0d61SDaniel P. Berrange                                    bool ipv6,
3519bd269ebcSMarkus Armbruster                                    SocketAddress **retaddr,
3520275e0d61SDaniel P. Berrange                                    Error **errp)
3521275e0d61SDaniel P. Berrange {
3522275e0d61SDaniel P. Berrange     int ret = -1;
3523bd269ebcSMarkus Armbruster     SocketAddress *addr = NULL;
3524275e0d61SDaniel P. Berrange 
3525bd269ebcSMarkus Armbruster     addr = g_new0(SocketAddress, 1);
3526275e0d61SDaniel P. Berrange 
3527275e0d61SDaniel P. Berrange     if (strncmp(addrstr, "unix:", 5) == 0) {
3528bd269ebcSMarkus Armbruster         addr->type = SOCKET_ADDRESS_TYPE_UNIX;
3529bd269ebcSMarkus Armbruster         addr->u.q_unix.path = g_strdup(addrstr + 5);
3530275e0d61SDaniel P. Berrange 
3531275e0d61SDaniel P. Berrange         if (websocket) {
3532275e0d61SDaniel P. Berrange             error_setg(errp, "UNIX sockets not supported with websock");
3533275e0d61SDaniel P. Berrange             goto cleanup;
3534275e0d61SDaniel P. Berrange         }
3535275e0d61SDaniel P. Berrange 
3536275e0d61SDaniel P. Berrange         if (to) {
3537275e0d61SDaniel P. Berrange             error_setg(errp, "Port range not support with UNIX socket");
3538275e0d61SDaniel P. Berrange             goto cleanup;
3539275e0d61SDaniel P. Berrange         }
3540275e0d61SDaniel P. Berrange         ret = 0;
3541275e0d61SDaniel P. Berrange     } else {
3542275e0d61SDaniel P. Berrange         const char *port;
3543275e0d61SDaniel P. Berrange         size_t hostlen;
3544275e0d61SDaniel P. Berrange         unsigned long long baseport = 0;
3545275e0d61SDaniel P. Berrange         InetSocketAddress *inet;
3546275e0d61SDaniel P. Berrange 
3547275e0d61SDaniel P. Berrange         port = strrchr(addrstr, ':');
3548275e0d61SDaniel P. Berrange         if (!port) {
3549275e0d61SDaniel P. Berrange             if (websocket) {
3550275e0d61SDaniel P. Berrange                 hostlen = 0;
3551275e0d61SDaniel P. Berrange                 port = addrstr;
3552275e0d61SDaniel P. Berrange             } else {
3553275e0d61SDaniel P. Berrange                 error_setg(errp, "no vnc port specified");
3554275e0d61SDaniel P. Berrange                 goto cleanup;
3555275e0d61SDaniel P. Berrange             }
3556275e0d61SDaniel P. Berrange         } else {
3557275e0d61SDaniel P. Berrange             hostlen = port - addrstr;
3558275e0d61SDaniel P. Berrange             port++;
3559275e0d61SDaniel P. Berrange             if (*port == '\0') {
3560275e0d61SDaniel P. Berrange                 error_setg(errp, "vnc port cannot be empty");
3561275e0d61SDaniel P. Berrange                 goto cleanup;
3562275e0d61SDaniel P. Berrange             }
3563275e0d61SDaniel P. Berrange         }
3564275e0d61SDaniel P. Berrange 
3565bd269ebcSMarkus Armbruster         addr->type = SOCKET_ADDRESS_TYPE_INET;
3566bd269ebcSMarkus Armbruster         inet = &addr->u.inet;
3567275e0d61SDaniel P. Berrange         if (addrstr[0] == '[' && addrstr[hostlen - 1] == ']') {
3568275e0d61SDaniel P. Berrange             inet->host = g_strndup(addrstr + 1, hostlen - 2);
3569275e0d61SDaniel P. Berrange         } else {
3570275e0d61SDaniel P. Berrange             inet->host = g_strndup(addrstr, hostlen);
3571275e0d61SDaniel P. Berrange         }
3572275e0d61SDaniel P. Berrange         /* plain VNC port is just an offset, for websocket
3573275e0d61SDaniel P. Berrange          * port is absolute */
3574275e0d61SDaniel P. Berrange         if (websocket) {
3575275e0d61SDaniel P. Berrange             if (g_str_equal(addrstr, "") ||
3576275e0d61SDaniel P. Berrange                 g_str_equal(addrstr, "on")) {
3577396f935aSDaniel P. Berrange                 if (displaynum == -1) {
3578396f935aSDaniel P. Berrange                     error_setg(errp, "explicit websocket port is required");
3579396f935aSDaniel P. Berrange                     goto cleanup;
3580396f935aSDaniel P. Berrange                 }
3581275e0d61SDaniel P. Berrange                 inet->port = g_strdup_printf(
3582275e0d61SDaniel P. Berrange                     "%d", displaynum + 5700);
3583275e0d61SDaniel P. Berrange                 if (to) {
3584275e0d61SDaniel P. Berrange                     inet->has_to = true;
3585275e0d61SDaniel P. Berrange                     inet->to = to + 5700;
3586275e0d61SDaniel P. Berrange                 }
3587275e0d61SDaniel P. Berrange             } else {
3588275e0d61SDaniel P. Berrange                 inet->port = g_strdup(port);
3589275e0d61SDaniel P. Berrange             }
3590275e0d61SDaniel P. Berrange         } else {
3591e5766eb4SGerd Hoffmann             int offset = reverse ? 0 : 5900;
3592275e0d61SDaniel P. Berrange             if (parse_uint_full(port, &baseport, 10) < 0) {
3593275e0d61SDaniel P. Berrange                 error_setg(errp, "can't convert to a number: %s", port);
3594275e0d61SDaniel P. Berrange                 goto cleanup;
3595275e0d61SDaniel P. Berrange             }
3596275e0d61SDaniel P. Berrange             if (baseport > 65535 ||
3597e5766eb4SGerd Hoffmann                 baseport + offset > 65535) {
3598275e0d61SDaniel P. Berrange                 error_setg(errp, "port %s out of range", port);
3599275e0d61SDaniel P. Berrange                 goto cleanup;
3600275e0d61SDaniel P. Berrange             }
3601275e0d61SDaniel P. Berrange             inet->port = g_strdup_printf(
3602e5766eb4SGerd Hoffmann                 "%d", (int)baseport + offset);
3603275e0d61SDaniel P. Berrange 
3604275e0d61SDaniel P. Berrange             if (to) {
3605275e0d61SDaniel P. Berrange                 inet->has_to = true;
3606e5766eb4SGerd Hoffmann                 inet->to = to + offset;
3607275e0d61SDaniel P. Berrange             }
3608275e0d61SDaniel P. Berrange         }
3609275e0d61SDaniel P. Berrange 
3610275e0d61SDaniel P. Berrange         inet->ipv4 = ipv4;
3611275e0d61SDaniel P. Berrange         inet->has_ipv4 = has_ipv4;
3612275e0d61SDaniel P. Berrange         inet->ipv6 = ipv6;
3613275e0d61SDaniel P. Berrange         inet->has_ipv6 = has_ipv6;
3614275e0d61SDaniel P. Berrange 
3615275e0d61SDaniel P. Berrange         ret = baseport;
3616275e0d61SDaniel P. Berrange     }
3617275e0d61SDaniel P. Berrange 
3618275e0d61SDaniel P. Berrange     *retaddr = addr;
3619275e0d61SDaniel P. Berrange 
3620275e0d61SDaniel P. Berrange  cleanup:
3621275e0d61SDaniel P. Berrange     if (ret < 0) {
3622bd269ebcSMarkus Armbruster         qapi_free_SocketAddress(addr);
3623275e0d61SDaniel P. Berrange     }
3624275e0d61SDaniel P. Berrange     return ret;
3625275e0d61SDaniel P. Berrange }
3626275e0d61SDaniel P. Berrange 
36279f26f325SPhilippe Mathieu-Daudé static void vnc_free_addresses(SocketAddress ***retsaddr,
36289f26f325SPhilippe Mathieu-Daudé                                size_t *retnsaddr)
36299f26f325SPhilippe Mathieu-Daudé {
36309f26f325SPhilippe Mathieu-Daudé     size_t i;
36319f26f325SPhilippe Mathieu-Daudé 
36329f26f325SPhilippe Mathieu-Daudé     for (i = 0; i < *retnsaddr; i++) {
36339f26f325SPhilippe Mathieu-Daudé         qapi_free_SocketAddress((*retsaddr)[i]);
36349f26f325SPhilippe Mathieu-Daudé     }
36359f26f325SPhilippe Mathieu-Daudé     g_free(*retsaddr);
36369f26f325SPhilippe Mathieu-Daudé 
36379f26f325SPhilippe Mathieu-Daudé     *retsaddr = NULL;
36389f26f325SPhilippe Mathieu-Daudé     *retnsaddr = 0;
36399f26f325SPhilippe Mathieu-Daudé }
36409f26f325SPhilippe Mathieu-Daudé 
3641275e0d61SDaniel P. Berrange static int vnc_display_get_addresses(QemuOpts *opts,
3642e5766eb4SGerd Hoffmann                                      bool reverse,
3643bd269ebcSMarkus Armbruster                                      SocketAddress ***retsaddr,
3644396f935aSDaniel P. Berrange                                      size_t *retnsaddr,
3645bd269ebcSMarkus Armbruster                                      SocketAddress ***retwsaddr,
3646396f935aSDaniel P. Berrange                                      size_t *retnwsaddr,
3647275e0d61SDaniel P. Berrange                                      Error **errp)
3648275e0d61SDaniel P. Berrange {
3649bd269ebcSMarkus Armbruster     SocketAddress *saddr = NULL;
3650bd269ebcSMarkus Armbruster     SocketAddress *wsaddr = NULL;
3651396f935aSDaniel P. Berrange     QemuOptsIter addriter;
3652396f935aSDaniel P. Berrange     const char *addr;
3653275e0d61SDaniel P. Berrange     int to = qemu_opt_get_number(opts, "to", 0);
3654275e0d61SDaniel P. Berrange     bool has_ipv4 = qemu_opt_get(opts, "ipv4");
3655275e0d61SDaniel P. Berrange     bool has_ipv6 = qemu_opt_get(opts, "ipv6");
3656275e0d61SDaniel P. Berrange     bool ipv4 = qemu_opt_get_bool(opts, "ipv4", false);
3657275e0d61SDaniel P. Berrange     bool ipv6 = qemu_opt_get_bool(opts, "ipv6", false);
3658396f935aSDaniel P. Berrange     int displaynum = -1;
3659396f935aSDaniel P. Berrange     int ret = -1;
3660275e0d61SDaniel P. Berrange 
3661275e0d61SDaniel P. Berrange     *retsaddr = NULL;
3662396f935aSDaniel P. Berrange     *retnsaddr = 0;
3663275e0d61SDaniel P. Berrange     *retwsaddr = NULL;
3664396f935aSDaniel P. Berrange     *retnwsaddr = 0;
3665275e0d61SDaniel P. Berrange 
3666396f935aSDaniel P. Berrange     addr = qemu_opt_get(opts, "vnc");
3667396f935aSDaniel P. Berrange     if (addr == NULL || g_str_equal(addr, "none")) {
3668396f935aSDaniel P. Berrange         ret = 0;
3669396f935aSDaniel P. Berrange         goto cleanup;
3670396f935aSDaniel P. Berrange     }
3671396f935aSDaniel P. Berrange     if (qemu_opt_get(opts, "websocket") &&
3672275e0d61SDaniel P. Berrange         !qcrypto_hash_supports(QCRYPTO_HASH_ALG_SHA1)) {
3673275e0d61SDaniel P. Berrange         error_setg(errp,
3674275e0d61SDaniel P. Berrange                    "SHA1 hash support is required for websockets");
3675396f935aSDaniel P. Berrange         goto cleanup;
3676275e0d61SDaniel P. Berrange     }
3677275e0d61SDaniel P. Berrange 
3678396f935aSDaniel P. Berrange     qemu_opt_iter_init(&addriter, opts, "vnc");
3679396f935aSDaniel P. Berrange     while ((addr = qemu_opt_iter_next(&addriter)) != NULL) {
3680396f935aSDaniel P. Berrange         int rv;
3681e5766eb4SGerd Hoffmann         rv = vnc_display_get_address(addr, false, reverse, 0, to,
3682275e0d61SDaniel P. Berrange                                      has_ipv4, has_ipv6,
3683275e0d61SDaniel P. Berrange                                      ipv4, ipv6,
3684275e0d61SDaniel P. Berrange                                      &saddr, errp);
3685396f935aSDaniel P. Berrange         if (rv < 0) {
3686396f935aSDaniel P. Berrange             goto cleanup;
3687275e0d61SDaniel P. Berrange         }
3688396f935aSDaniel P. Berrange         /* Historical compat - first listen address can be used
3689396f935aSDaniel P. Berrange          * to set the default websocket port
3690396f935aSDaniel P. Berrange          */
3691396f935aSDaniel P. Berrange         if (displaynum == -1) {
3692396f935aSDaniel P. Berrange             displaynum = rv;
3693396f935aSDaniel P. Berrange         }
3694bd269ebcSMarkus Armbruster         *retsaddr = g_renew(SocketAddress *, *retsaddr, *retnsaddr + 1);
3695396f935aSDaniel P. Berrange         (*retsaddr)[(*retnsaddr)++] = saddr;
3696396f935aSDaniel P. Berrange     }
3697396f935aSDaniel P. Berrange 
3698396f935aSDaniel P. Berrange     /* If we had multiple primary displays, we don't do defaults
3699396f935aSDaniel P. Berrange      * for websocket, and require explicit config instead. */
3700396f935aSDaniel P. Berrange     if (*retnsaddr > 1) {
3701396f935aSDaniel P. Berrange         displaynum = -1;
3702396f935aSDaniel P. Berrange     }
3703396f935aSDaniel P. Berrange 
3704396f935aSDaniel P. Berrange     qemu_opt_iter_init(&addriter, opts, "websocket");
3705396f935aSDaniel P. Berrange     while ((addr = qemu_opt_iter_next(&addriter)) != NULL) {
3706e5766eb4SGerd Hoffmann         if (vnc_display_get_address(addr, true, reverse, displaynum, to,
3707275e0d61SDaniel P. Berrange                                     has_ipv4, has_ipv6,
3708275e0d61SDaniel P. Berrange                                     ipv4, ipv6,
3709275e0d61SDaniel P. Berrange                                     &wsaddr, errp) < 0) {
3710396f935aSDaniel P. Berrange             goto cleanup;
3711275e0d61SDaniel P. Berrange         }
3712396f935aSDaniel P. Berrange 
3713396f935aSDaniel P. Berrange         /* Historical compat - if only a single listen address was
3714396f935aSDaniel P. Berrange          * provided, then this is used to set the default listen
3715396f935aSDaniel P. Berrange          * address for websocket too
3716396f935aSDaniel P. Berrange          */
3717396f935aSDaniel P. Berrange         if (*retnsaddr == 1 &&
3718bd269ebcSMarkus Armbruster             (*retsaddr)[0]->type == SOCKET_ADDRESS_TYPE_INET &&
3719bd269ebcSMarkus Armbruster             wsaddr->type == SOCKET_ADDRESS_TYPE_INET &&
3720bd269ebcSMarkus Armbruster             g_str_equal(wsaddr->u.inet.host, "") &&
3721bd269ebcSMarkus Armbruster             !g_str_equal((*retsaddr)[0]->u.inet.host, "")) {
3722bd269ebcSMarkus Armbruster             g_free(wsaddr->u.inet.host);
3723bd269ebcSMarkus Armbruster             wsaddr->u.inet.host = g_strdup((*retsaddr)[0]->u.inet.host);
3724275e0d61SDaniel P. Berrange         }
3725275e0d61SDaniel P. Berrange 
3726bd269ebcSMarkus Armbruster         *retwsaddr = g_renew(SocketAddress *, *retwsaddr, *retnwsaddr + 1);
3727396f935aSDaniel P. Berrange         (*retwsaddr)[(*retnwsaddr)++] = wsaddr;
3728396f935aSDaniel P. Berrange     }
3729396f935aSDaniel P. Berrange 
3730396f935aSDaniel P. Berrange     ret = 0;
3731396f935aSDaniel P. Berrange  cleanup:
3732396f935aSDaniel P. Berrange     if (ret < 0) {
37339f26f325SPhilippe Mathieu-Daudé         vnc_free_addresses(retsaddr, retnsaddr);
37349f26f325SPhilippe Mathieu-Daudé         vnc_free_addresses(retwsaddr, retnwsaddr);
3735396f935aSDaniel P. Berrange     }
3736396f935aSDaniel P. Berrange     return ret;
3737275e0d61SDaniel P. Berrange }
3738275e0d61SDaniel P. Berrange 
37398bd22f47SDaniel P. Berrange static int vnc_display_connect(VncDisplay *vd,
3740bd269ebcSMarkus Armbruster                                SocketAddress **saddr,
3741396f935aSDaniel P. Berrange                                size_t nsaddr,
3742bd269ebcSMarkus Armbruster                                SocketAddress **wsaddr,
3743396f935aSDaniel P. Berrange                                size_t nwsaddr,
37448bd22f47SDaniel P. Berrange                                Error **errp)
37458bd22f47SDaniel P. Berrange {
37468bd22f47SDaniel P. Berrange     /* connect to viewer */
37478bd22f47SDaniel P. Berrange     QIOChannelSocket *sioc = NULL;
3748396f935aSDaniel P. Berrange     if (nwsaddr != 0) {
37498bd22f47SDaniel P. Berrange         error_setg(errp, "Cannot use websockets in reverse mode");
37508bd22f47SDaniel P. Berrange         return -1;
37518bd22f47SDaniel P. Berrange     }
3752396f935aSDaniel P. Berrange     if (nsaddr != 1) {
3753396f935aSDaniel P. Berrange         error_setg(errp, "Expected a single address in reverse mode");
3754396f935aSDaniel P. Berrange         return -1;
3755396f935aSDaniel P. Berrange     }
3756bd269ebcSMarkus Armbruster     /* TODO SOCKET_ADDRESS_TYPE_FD when fd has AF_UNIX */
3757bd269ebcSMarkus Armbruster     vd->is_unix = saddr[0]->type == SOCKET_ADDRESS_TYPE_UNIX;
37588bd22f47SDaniel P. Berrange     sioc = qio_channel_socket_new();
37598bd22f47SDaniel P. Berrange     qio_channel_set_name(QIO_CHANNEL(sioc), "vnc-reverse");
3760396f935aSDaniel P. Berrange     if (qio_channel_socket_connect_sync(sioc, saddr[0], errp) < 0) {
37618bd22f47SDaniel P. Berrange         return -1;
37628bd22f47SDaniel P. Berrange     }
37638bd22f47SDaniel P. Berrange     vnc_connect(vd, sioc, false, false);
37648bd22f47SDaniel P. Berrange     object_unref(OBJECT(sioc));
37658bd22f47SDaniel P. Berrange     return 0;
37668bd22f47SDaniel P. Berrange }
37678bd22f47SDaniel P. Berrange 
37688bd22f47SDaniel P. Berrange 
37698bd22f47SDaniel P. Berrange static int vnc_display_listen(VncDisplay *vd,
3770bd269ebcSMarkus Armbruster                               SocketAddress **saddr,
3771396f935aSDaniel P. Berrange                               size_t nsaddr,
3772bd269ebcSMarkus Armbruster                               SocketAddress **wsaddr,
3773396f935aSDaniel P. Berrange                               size_t nwsaddr,
37748bd22f47SDaniel P. Berrange                               Error **errp)
37758bd22f47SDaniel P. Berrange {
3776396f935aSDaniel P. Berrange     size_t i;
37778bd22f47SDaniel P. Berrange 
377813e1d0e7SDaniel P. Berrange     if (nsaddr) {
377913e1d0e7SDaniel P. Berrange         vd->listener = qio_net_listener_new();
378013e1d0e7SDaniel P. Berrange         qio_net_listener_set_name(vd->listener, "vnc-listen");
3781396f935aSDaniel P. Berrange         for (i = 0; i < nsaddr; i++) {
378213e1d0e7SDaniel P. Berrange             if (qio_net_listener_open_sync(vd->listener,
378313e1d0e7SDaniel P. Berrange                                            saddr[i],
37848bd22f47SDaniel P. Berrange                                            errp) < 0)  {
37858bd22f47SDaniel P. Berrange                 return -1;
37868bd22f47SDaniel P. Berrange             }
3787396f935aSDaniel P. Berrange         }
378813e1d0e7SDaniel P. Berrange 
378913e1d0e7SDaniel P. Berrange         qio_net_listener_set_client_func(vd->listener,
379013e1d0e7SDaniel P. Berrange                                          vnc_listen_io, vd, NULL);
379113e1d0e7SDaniel P. Berrange     }
379213e1d0e7SDaniel P. Berrange 
379313e1d0e7SDaniel P. Berrange     if (nwsaddr) {
379413e1d0e7SDaniel P. Berrange         vd->wslistener = qio_net_listener_new();
379513e1d0e7SDaniel P. Berrange         qio_net_listener_set_name(vd->wslistener, "vnc-ws-listen");
3796396f935aSDaniel P. Berrange         for (i = 0; i < nwsaddr; i++) {
379713e1d0e7SDaniel P. Berrange             if (qio_net_listener_open_sync(vd->wslistener,
379813e1d0e7SDaniel P. Berrange                                            wsaddr[i],
37998bd22f47SDaniel P. Berrange                                            errp) < 0)  {
38008bd22f47SDaniel P. Berrange                 return -1;
38018bd22f47SDaniel P. Berrange             }
3802396f935aSDaniel P. Berrange         }
38038bd22f47SDaniel P. Berrange 
380413e1d0e7SDaniel P. Berrange         qio_net_listener_set_client_func(vd->wslistener,
380513e1d0e7SDaniel P. Berrange                                          vnc_listen_io, vd, NULL);
380613e1d0e7SDaniel P. Berrange     }
380713e1d0e7SDaniel P. Berrange 
38088bd22f47SDaniel P. Berrange     return 0;
38098bd22f47SDaniel P. Berrange }
38108bd22f47SDaniel P. Berrange 
38118bd22f47SDaniel P. Berrange 
38124db14629SGerd Hoffmann void vnc_display_open(const char *id, Error **errp)
38133e230dd2SCorentin Chary {
3814bf01c179SDaniel P. Berrange     VncDisplay *vd = vnc_display_find(id);
38154db14629SGerd Hoffmann     QemuOpts *opts = qemu_opts_find(&qemu_vnc_opts, id);
3816bd269ebcSMarkus Armbruster     SocketAddress **saddr = NULL, **wsaddr = NULL;
3817396f935aSDaniel P. Berrange     size_t nsaddr, nwsaddr;
3818e2a11d9dSGonglei     const char *share, *device_id;
38191d0d59feSGerd Hoffmann     QemuConsole *con;
3820a2c72de0SGonglei     bool password = false;
3821a2c72de0SGonglei     bool reverse = false;
38223e305e4aSDaniel P. Berrange     const char *credid;
3823a2c72de0SGonglei     bool sasl = false;
3824d169f04bSDaniel P. Berrange #ifdef CONFIG_VNC_SASL
38253e230dd2SCorentin Chary     int saslErr;
38263e230dd2SCorentin Chary #endif
38273e230dd2SCorentin Chary     int acl = 0;
38283e230dd2SCorentin Chary     int lock_key_sync = 1;
3829c5ce8333SGerd Hoffmann     int key_delay_ms;
38303e230dd2SCorentin Chary 
3831bf01c179SDaniel P. Berrange     if (!vd) {
38322d55f0e8SPaolo Bonzini         error_setg(errp, "VNC display not active");
38332d55f0e8SPaolo Bonzini         return;
38342d55f0e8SPaolo Bonzini     }
3835bf01c179SDaniel P. Berrange     vnc_display_close(vd);
38364db14629SGerd Hoffmann 
38374db14629SGerd Hoffmann     if (!opts) {
38382d55f0e8SPaolo Bonzini         return;
38394db14629SGerd Hoffmann     }
3840275e0d61SDaniel P. Berrange 
3841e5766eb4SGerd Hoffmann     reverse = qemu_opt_get_bool(opts, "reverse", false);
3842e5766eb4SGerd Hoffmann     if (vnc_display_get_addresses(opts, reverse, &saddr, &nsaddr,
3843396f935aSDaniel P. Berrange                                   &wsaddr, &nwsaddr, errp) < 0) {
3844275e0d61SDaniel P. Berrange         goto fail;
3845275e0d61SDaniel P. Berrange     }
3846275e0d61SDaniel P. Berrange 
38474db14629SGerd Hoffmann     password = qemu_opt_get_bool(opts, "password", false);
3848800567a6SDaniel P. Berrange     if (password) {
3849800567a6SDaniel P. Berrange         if (fips_get_state()) {
38502d55f0e8SPaolo Bonzini             error_setg(errp,
38510f66998fSPaul Moore                        "VNC password auth disabled due to FIPS mode, "
38520f66998fSPaul Moore                        "consider using the VeNCrypt or SASL authentication "
38532d55f0e8SPaolo Bonzini                        "methods as an alternative");
38541ce52c78SPaolo Bonzini             goto fail;
38550f66998fSPaul Moore         }
3856800567a6SDaniel P. Berrange         if (!qcrypto_cipher_supports(
3857f844836dSGonglei                 QCRYPTO_CIPHER_ALG_DES_RFB, QCRYPTO_CIPHER_MODE_ECB)) {
3858800567a6SDaniel P. Berrange             error_setg(errp,
3859800567a6SDaniel P. Berrange                        "Cipher backend does not support DES RFB algorithm");
3860800567a6SDaniel P. Berrange             goto fail;
3861800567a6SDaniel P. Berrange         }
3862800567a6SDaniel P. Berrange     }
38634db14629SGerd Hoffmann 
38644db14629SGerd Hoffmann     lock_key_sync = qemu_opt_get_bool(opts, "lock-key-sync", true);
3865d3b0db6dSAlexander Graf     key_delay_ms = qemu_opt_get_number(opts, "key-delay-ms", 10);
38664db14629SGerd Hoffmann     sasl = qemu_opt_get_bool(opts, "sasl", false);
3867d169f04bSDaniel P. Berrange #ifndef CONFIG_VNC_SASL
3868d169f04bSDaniel P. Berrange     if (sasl) {
3869d169f04bSDaniel P. Berrange         error_setg(errp, "VNC SASL auth requires cyrus-sasl support");
3870d169f04bSDaniel P. Berrange         goto fail;
3871d169f04bSDaniel P. Berrange     }
3872d169f04bSDaniel P. Berrange #endif /* CONFIG_VNC_SASL */
38733e305e4aSDaniel P. Berrange     credid = qemu_opt_get(opts, "tls-creds");
38743e305e4aSDaniel P. Berrange     if (credid) {
38753e305e4aSDaniel P. Berrange         Object *creds;
38763e305e4aSDaniel P. Berrange         creds = object_resolve_path_component(
38773e305e4aSDaniel P. Berrange             object_get_objects_root(), credid);
38783e305e4aSDaniel P. Berrange         if (!creds) {
38793e305e4aSDaniel P. Berrange             error_setg(errp, "No TLS credentials with id '%s'",
38803e305e4aSDaniel P. Berrange                        credid);
38813e305e4aSDaniel P. Berrange             goto fail;
38823e305e4aSDaniel P. Berrange         }
3883bf01c179SDaniel P. Berrange         vd->tlscreds = (QCryptoTLSCreds *)
38843e305e4aSDaniel P. Berrange             object_dynamic_cast(creds,
38853e305e4aSDaniel P. Berrange                                 TYPE_QCRYPTO_TLS_CREDS);
3886bf01c179SDaniel P. Berrange         if (!vd->tlscreds) {
38873e305e4aSDaniel P. Berrange             error_setg(errp, "Object with id '%s' is not TLS credentials",
38883e305e4aSDaniel P. Berrange                        credid);
38893e305e4aSDaniel P. Berrange             goto fail;
38903e305e4aSDaniel P. Berrange         }
3891bf01c179SDaniel P. Berrange         object_ref(OBJECT(vd->tlscreds));
38923e305e4aSDaniel P. Berrange 
3893bf01c179SDaniel P. Berrange         if (vd->tlscreds->endpoint != QCRYPTO_TLS_CREDS_ENDPOINT_SERVER) {
38943e305e4aSDaniel P. Berrange             error_setg(errp,
38953e305e4aSDaniel P. Berrange                        "Expecting TLS credentials with a server endpoint");
38963e305e4aSDaniel P. Berrange             goto fail;
38973e305e4aSDaniel P. Berrange         }
3898d169f04bSDaniel P. Berrange     }
38994db14629SGerd Hoffmann     acl = qemu_opt_get_bool(opts, "acl", false);
39004db14629SGerd Hoffmann 
39014db14629SGerd Hoffmann     share = qemu_opt_get(opts, "share");
39024db14629SGerd Hoffmann     if (share) {
39034db14629SGerd Hoffmann         if (strcmp(share, "ignore") == 0) {
3904bf01c179SDaniel P. Berrange             vd->share_policy = VNC_SHARE_POLICY_IGNORE;
39054db14629SGerd Hoffmann         } else if (strcmp(share, "allow-exclusive") == 0) {
3906bf01c179SDaniel P. Berrange             vd->share_policy = VNC_SHARE_POLICY_ALLOW_EXCLUSIVE;
39074db14629SGerd Hoffmann         } else if (strcmp(share, "force-shared") == 0) {
3908bf01c179SDaniel P. Berrange             vd->share_policy = VNC_SHARE_POLICY_FORCE_SHARED;
39098cf36489SGerd Hoffmann         } else {
39102d55f0e8SPaolo Bonzini             error_setg(errp, "unknown vnc share= option");
39111ce52c78SPaolo Bonzini             goto fail;
39128cf36489SGerd Hoffmann         }
39134db14629SGerd Hoffmann     } else {
3914bf01c179SDaniel P. Berrange         vd->share_policy = VNC_SHARE_POLICY_ALLOW_EXCLUSIVE;
39153e230dd2SCorentin Chary     }
3916bf01c179SDaniel P. Berrange     vd->connections_limit = qemu_opt_get_number(opts, "connections", 32);
39173e230dd2SCorentin Chary 
39184db14629SGerd Hoffmann #ifdef CONFIG_VNC_JPEG
3919bf01c179SDaniel P. Berrange     vd->lossy = qemu_opt_get_bool(opts, "lossy", false);
39204db14629SGerd Hoffmann #endif
3921bf01c179SDaniel P. Berrange     vd->non_adaptive = qemu_opt_get_bool(opts, "non-adaptive", false);
3922e22492d3SPeter Lieven     /* adaptive updates are only used with tight encoding and
3923e22492d3SPeter Lieven      * if lossy updates are enabled so we can disable all the
3924e22492d3SPeter Lieven      * calculations otherwise */
3925bf01c179SDaniel P. Berrange     if (!vd->lossy) {
3926bf01c179SDaniel P. Berrange         vd->non_adaptive = true;
3927e22492d3SPeter Lieven     }
3928e22492d3SPeter Lieven 
39293e305e4aSDaniel P. Berrange     if (acl) {
3930bf01c179SDaniel P. Berrange         if (strcmp(vd->id, "default") == 0) {
3931bf01c179SDaniel P. Berrange             vd->tlsaclname = g_strdup("vnc.x509dname");
3932c8496408SGerd Hoffmann         } else {
3933bf01c179SDaniel P. Berrange             vd->tlsaclname = g_strdup_printf("vnc.%s.x509dname", vd->id);
3934c8496408SGerd Hoffmann         }
3935bf01c179SDaniel P. Berrange         qemu_acl_init(vd->tlsaclname);
39363e230dd2SCorentin Chary     }
39373e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL
39383e230dd2SCorentin Chary     if (acl && sasl) {
3939c8496408SGerd Hoffmann         char *aclname;
3940c8496408SGerd Hoffmann 
3941bf01c179SDaniel P. Berrange         if (strcmp(vd->id, "default") == 0) {
3942c8496408SGerd Hoffmann             aclname = g_strdup("vnc.username");
3943c8496408SGerd Hoffmann         } else {
3944bf01c179SDaniel P. Berrange             aclname = g_strdup_printf("vnc.%s.username", vd->id);
3945c8496408SGerd Hoffmann         }
3946bf01c179SDaniel P. Berrange         vd->sasl.acl = qemu_acl_init(aclname);
3947c8496408SGerd Hoffmann         g_free(aclname);
39483e230dd2SCorentin Chary     }
39493e230dd2SCorentin Chary #endif
39503e230dd2SCorentin Chary 
3951eda24e18SDaniel P. Berrange     if (vnc_display_setup_auth(&vd->auth, &vd->subauth,
3952eda24e18SDaniel P. Berrange                                vd->tlscreds, password,
3953eda24e18SDaniel P. Berrange                                sasl, false, errp) < 0) {
3954eda24e18SDaniel P. Berrange         goto fail;
3955eda24e18SDaniel P. Berrange     }
39567364dbdaSDaniel P. Berrange     trace_vnc_auth_init(vd, 0, vd->auth, vd->subauth);
3957eda24e18SDaniel P. Berrange 
3958eda24e18SDaniel P. Berrange     if (vnc_display_setup_auth(&vd->ws_auth, &vd->ws_subauth,
3959eda24e18SDaniel P. Berrange                                vd->tlscreds, password,
3960eda24e18SDaniel P. Berrange                                sasl, true, errp) < 0) {
39613e305e4aSDaniel P. Berrange         goto fail;
39623e305e4aSDaniel P. Berrange     }
39637364dbdaSDaniel P. Berrange     trace_vnc_auth_init(vd, 1, vd->ws_auth, vd->ws_subauth);
39643e230dd2SCorentin Chary 
39653e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL
39663e230dd2SCorentin Chary     if ((saslErr = sasl_server_init(NULL, "qemu")) != SASL_OK) {
39672d55f0e8SPaolo Bonzini         error_setg(errp, "Failed to initialize SASL auth: %s",
39683e230dd2SCorentin Chary                    sasl_errstring(saslErr, NULL, NULL));
39691ce52c78SPaolo Bonzini         goto fail;
39703e230dd2SCorentin Chary     }
39713e230dd2SCorentin Chary #endif
3972bf01c179SDaniel P. Berrange     vd->lock_key_sync = lock_key_sync;
3973a54f0d2bSPierre Ossman     if (lock_key_sync) {
3974a54f0d2bSPierre Ossman         vd->led = qemu_add_led_event_handler(kbd_leds, vd);
3975a54f0d2bSPierre Ossman     }
3976a54f0d2bSPierre Ossman     vd->ledstate = 0;
3977bf01c179SDaniel P. Berrange     vd->key_delay_ms = key_delay_ms;
39783e230dd2SCorentin Chary 
39791d0d59feSGerd Hoffmann     device_id = qemu_opt_get(opts, "display");
39801d0d59feSGerd Hoffmann     if (device_id) {
39811d0d59feSGerd Hoffmann         int head = qemu_opt_get_number(opts, "head", 0);
3982f2c1d54cSGerd Hoffmann         Error *err = NULL;
39831d0d59feSGerd Hoffmann 
3984f2c1d54cSGerd Hoffmann         con = qemu_console_lookup_by_device_name(device_id, head, &err);
3985f2c1d54cSGerd Hoffmann         if (err) {
3986f2c1d54cSGerd Hoffmann             error_propagate(errp, err);
39871d0d59feSGerd Hoffmann             goto fail;
39881d0d59feSGerd Hoffmann         }
39891d0d59feSGerd Hoffmann     } else {
39901d0d59feSGerd Hoffmann         con = NULL;
39911d0d59feSGerd Hoffmann     }
39921d0d59feSGerd Hoffmann 
3993bf01c179SDaniel P. Berrange     if (con != vd->dcl.con) {
3994bf01c179SDaniel P. Berrange         unregister_displaychangelistener(&vd->dcl);
3995bf01c179SDaniel P. Berrange         vd->dcl.con = con;
3996bf01c179SDaniel P. Berrange         register_displaychangelistener(&vd->dcl);
39971d0d59feSGerd Hoffmann     }
39981d0d59feSGerd Hoffmann 
3999fa03cb7fSMarc-André Lureau     if (saddr == NULL) {
4000fa03cb7fSMarc-André Lureau         goto cleanup;
4001fa03cb7fSMarc-André Lureau     }
4002fa03cb7fSMarc-André Lureau 
40033e230dd2SCorentin Chary     if (reverse) {
4004396f935aSDaniel P. Berrange         if (vnc_display_connect(vd, saddr, nsaddr, wsaddr, nwsaddr, errp) < 0) {
4005e0d03b8cSDaniel P. Berrange             goto fail;
40063e230dd2SCorentin Chary         }
40073e230dd2SCorentin Chary     } else {
4008396f935aSDaniel P. Berrange         if (vnc_display_listen(vd, saddr, nsaddr, wsaddr, nwsaddr, errp) < 0) {
40093d00ac1aSCole Robinson             goto fail;
40103d00ac1aSCole Robinson         }
40113e230dd2SCorentin Chary     }
4012e0d03b8cSDaniel P. Berrange 
4013275e0d61SDaniel P. Berrange     if (qemu_opt_get(opts, "to")) {
4014bf01c179SDaniel P. Berrange         vnc_display_print_local_addr(vd);
401533df7bf3SPaolo Bonzini     }
401633df7bf3SPaolo Bonzini 
4017396f935aSDaniel P. Berrange  cleanup:
40189f26f325SPhilippe Mathieu-Daudé     vnc_free_addresses(&saddr, &nsaddr);
40199f26f325SPhilippe Mathieu-Daudé     vnc_free_addresses(&wsaddr, &nwsaddr);
40202d55f0e8SPaolo Bonzini     return;
40211ce52c78SPaolo Bonzini 
40221ce52c78SPaolo Bonzini fail:
40234ee74fa7SDaniel P. Berrange     vnc_display_close(vd);
4024396f935aSDaniel P. Berrange     goto cleanup;
40253e230dd2SCorentin Chary }
402613661089SDaniel P. Berrange 
402714f7143eSGerd Hoffmann void vnc_display_add_client(const char *id, int csock, bool skipauth)
402813661089SDaniel P. Berrange {
4029bf01c179SDaniel P. Berrange     VncDisplay *vd = vnc_display_find(id);
403004d2529dSDaniel P. Berrange     QIOChannelSocket *sioc;
403113661089SDaniel P. Berrange 
4032bf01c179SDaniel P. Berrange     if (!vd) {
4033d616ccc5SGerd Hoffmann         return;
4034d616ccc5SGerd Hoffmann     }
403504d2529dSDaniel P. Berrange 
403604d2529dSDaniel P. Berrange     sioc = qio_channel_socket_new_fd(csock, NULL);
403704d2529dSDaniel P. Berrange     if (sioc) {
403810bcfe58SDaniel P. Berrange         qio_channel_set_name(QIO_CHANNEL(sioc), "vnc-server");
4039bf01c179SDaniel P. Berrange         vnc_connect(vd, sioc, skipauth, false);
404004d2529dSDaniel P. Berrange         object_unref(OBJECT(sioc));
404104d2529dSDaniel P. Berrange     }
404213661089SDaniel P. Berrange }
40434db14629SGerd Hoffmann 
40449634f4e3SGerd Hoffmann static void vnc_auto_assign_id(QemuOptsList *olist, QemuOpts *opts)
40452779672fSGonglei {
40462779672fSGonglei     int i = 2;
40472779672fSGonglei     char *id;
40482779672fSGonglei 
40492779672fSGonglei     id = g_strdup("default");
40502779672fSGonglei     while (qemu_opts_find(olist, id)) {
40512779672fSGonglei         g_free(id);
40522779672fSGonglei         id = g_strdup_printf("vnc%d", i++);
40532779672fSGonglei     }
40542779672fSGonglei     qemu_opts_set_id(opts, id);
40552779672fSGonglei }
40562779672fSGonglei 
405770b94331SMarkus Armbruster QemuOpts *vnc_parse(const char *str, Error **errp)
40584db14629SGerd Hoffmann {
40594db14629SGerd Hoffmann     QemuOptsList *olist = qemu_find_opts("vnc");
406070b94331SMarkus Armbruster     QemuOpts *opts = qemu_opts_parse(olist, str, true, errp);
406181607cbfSGonglei     const char *id;
40624db14629SGerd Hoffmann 
406381607cbfSGonglei     if (!opts) {
406481607cbfSGonglei         return NULL;
406581607cbfSGonglei     }
406681607cbfSGonglei 
406781607cbfSGonglei     id = qemu_opts_id(opts);
40684db14629SGerd Hoffmann     if (!id) {
40694db14629SGerd Hoffmann         /* auto-assign id if not present */
40702779672fSGonglei         vnc_auto_assign_id(olist, opts);
40719634f4e3SGerd Hoffmann     }
40729634f4e3SGerd Hoffmann     return opts;
40734db14629SGerd Hoffmann }
40744db14629SGerd Hoffmann 
407528d0de7aSMarkus Armbruster int vnc_init_func(void *opaque, QemuOpts *opts, Error **errp)
40769634f4e3SGerd Hoffmann {
40779634f4e3SGerd Hoffmann     Error *local_err = NULL;
40789634f4e3SGerd Hoffmann     char *id = (char *)qemu_opts_id(opts);
40799634f4e3SGerd Hoffmann 
40809634f4e3SGerd Hoffmann     assert(id);
40814db14629SGerd Hoffmann     vnc_display_init(id);
40824db14629SGerd Hoffmann     vnc_display_open(id, &local_err);
40834db14629SGerd Hoffmann     if (local_err != NULL) {
4084c29b77f9SMarkus Armbruster         error_reportf_err(local_err, "Failed to start VNC server: ");
40854db14629SGerd Hoffmann         exit(1);
40864db14629SGerd Hoffmann     }
40874db14629SGerd Hoffmann     return 0;
40884db14629SGerd Hoffmann }
40894db14629SGerd Hoffmann 
40904db14629SGerd Hoffmann static void vnc_register_config(void)
40914db14629SGerd Hoffmann {
40924db14629SGerd Hoffmann     qemu_add_opts(&qemu_vnc_opts);
40934db14629SGerd Hoffmann }
409434294e2fSEduardo Habkost opts_init(vnc_register_config);
4095