13e230dd2SCorentin Chary /* 23e230dd2SCorentin Chary * QEMU VNC display driver 33e230dd2SCorentin Chary * 43e230dd2SCorentin Chary * Copyright (C) 2006 Anthony Liguori <anthony@codemonkey.ws> 53e230dd2SCorentin Chary * Copyright (C) 2006 Fabrice Bellard 63e230dd2SCorentin Chary * Copyright (C) 2009 Red Hat, Inc 73e230dd2SCorentin Chary * 83e230dd2SCorentin Chary * Permission is hereby granted, free of charge, to any person obtaining a copy 93e230dd2SCorentin Chary * of this software and associated documentation files (the "Software"), to deal 103e230dd2SCorentin Chary * in the Software without restriction, including without limitation the rights 113e230dd2SCorentin Chary * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 123e230dd2SCorentin Chary * copies of the Software, and to permit persons to whom the Software is 133e230dd2SCorentin Chary * furnished to do so, subject to the following conditions: 143e230dd2SCorentin Chary * 153e230dd2SCorentin Chary * The above copyright notice and this permission notice shall be included in 163e230dd2SCorentin Chary * all copies or substantial portions of the Software. 173e230dd2SCorentin Chary * 183e230dd2SCorentin Chary * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 193e230dd2SCorentin Chary * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 203e230dd2SCorentin Chary * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 213e230dd2SCorentin Chary * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 223e230dd2SCorentin Chary * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 233e230dd2SCorentin Chary * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 243e230dd2SCorentin Chary * THE SOFTWARE. 253e230dd2SCorentin Chary */ 263e230dd2SCorentin Chary 273e230dd2SCorentin Chary #include "vnc.h" 28bd023f95SCorentin Chary #include "vnc-jobs.h" 2940066175SGerd Hoffmann #include "trace.h" 309c17d615SPaolo Bonzini #include "sysemu/sysemu.h" 311de7afc9SPaolo Bonzini #include "qemu/sockets.h" 321de7afc9SPaolo Bonzini #include "qemu/timer.h" 331de7afc9SPaolo Bonzini #include "qemu/acl.h" 347b1b5d19SPaolo Bonzini #include "qapi/qmp/types.h" 352b54aa87SLuiz Capitulino #include "qmp-commands.h" 361de7afc9SPaolo Bonzini #include "qemu/osdep.h" 378d447d10SGerd Hoffmann #include "ui/input.h" 38fb6ba0d5SWenchao Xia #include "qapi-event.h" 393e230dd2SCorentin Chary 400f7b2864SGerd Hoffmann #define VNC_REFRESH_INTERVAL_BASE GUI_REFRESH_INTERVAL_DEFAULT 413e230dd2SCorentin Chary #define VNC_REFRESH_INTERVAL_INC 50 420f7b2864SGerd Hoffmann #define VNC_REFRESH_INTERVAL_MAX GUI_REFRESH_INTERVAL_IDLE 43999342a0SCorentin Chary static const struct timeval VNC_REFRESH_STATS = { 0, 500000 }; 44999342a0SCorentin Chary static const struct timeval VNC_REFRESH_LOSSY = { 2, 0 }; 453e230dd2SCorentin Chary 463e230dd2SCorentin Chary #include "vnc_keysym.h" 473e230dd2SCorentin Chary #include "d3des.h" 483e230dd2SCorentin Chary 493e230dd2SCorentin Chary static VncDisplay *vnc_display; /* needed for info vnc */ 503e230dd2SCorentin Chary 513e230dd2SCorentin Chary static int vnc_cursor_define(VncState *vs); 527bc9318bSGerd Hoffmann static void vnc_release_modifiers(VncState *vs); 533e230dd2SCorentin Chary 548cf36489SGerd Hoffmann static void vnc_set_share_mode(VncState *vs, VncShareMode mode) 558cf36489SGerd Hoffmann { 568cf36489SGerd Hoffmann #ifdef _VNC_DEBUG 578cf36489SGerd Hoffmann static const char *mn[] = { 588cf36489SGerd Hoffmann [0] = "undefined", 598cf36489SGerd Hoffmann [VNC_SHARE_MODE_CONNECTING] = "connecting", 608cf36489SGerd Hoffmann [VNC_SHARE_MODE_SHARED] = "shared", 618cf36489SGerd Hoffmann [VNC_SHARE_MODE_EXCLUSIVE] = "exclusive", 628cf36489SGerd Hoffmann [VNC_SHARE_MODE_DISCONNECTED] = "disconnected", 638cf36489SGerd Hoffmann }; 648cf36489SGerd Hoffmann fprintf(stderr, "%s/%d: %s -> %s\n", __func__, 658cf36489SGerd Hoffmann vs->csock, mn[vs->share_mode], mn[mode]); 668cf36489SGerd Hoffmann #endif 678cf36489SGerd Hoffmann 688cf36489SGerd Hoffmann if (vs->share_mode == VNC_SHARE_MODE_EXCLUSIVE) { 698cf36489SGerd Hoffmann vs->vd->num_exclusive--; 708cf36489SGerd Hoffmann } 718cf36489SGerd Hoffmann vs->share_mode = mode; 728cf36489SGerd Hoffmann if (vs->share_mode == VNC_SHARE_MODE_EXCLUSIVE) { 738cf36489SGerd Hoffmann vs->vd->num_exclusive++; 748cf36489SGerd Hoffmann } 758cf36489SGerd Hoffmann } 768cf36489SGerd Hoffmann 773e230dd2SCorentin Chary static char *addr_to_string(const char *format, 783e230dd2SCorentin Chary struct sockaddr_storage *sa, 793e230dd2SCorentin Chary socklen_t salen) { 803e230dd2SCorentin Chary char *addr; 813e230dd2SCorentin Chary char host[NI_MAXHOST]; 823e230dd2SCorentin Chary char serv[NI_MAXSERV]; 833e230dd2SCorentin Chary int err; 843e230dd2SCorentin Chary size_t addrlen; 853e230dd2SCorentin Chary 863e230dd2SCorentin Chary if ((err = getnameinfo((struct sockaddr *)sa, salen, 873e230dd2SCorentin Chary host, sizeof(host), 883e230dd2SCorentin Chary serv, sizeof(serv), 893e230dd2SCorentin Chary NI_NUMERICHOST | NI_NUMERICSERV)) != 0) { 903e230dd2SCorentin Chary VNC_DEBUG("Cannot resolve address %d: %s\n", 913e230dd2SCorentin Chary err, gai_strerror(err)); 923e230dd2SCorentin Chary return NULL; 933e230dd2SCorentin Chary } 943e230dd2SCorentin Chary 953e230dd2SCorentin Chary /* Enough for the existing format + the 2 vars we're 963e230dd2SCorentin Chary * substituting in. */ 973e230dd2SCorentin Chary addrlen = strlen(format) + strlen(host) + strlen(serv); 987267c094SAnthony Liguori addr = g_malloc(addrlen + 1); 993e230dd2SCorentin Chary snprintf(addr, addrlen, format, host, serv); 1003e230dd2SCorentin Chary addr[addrlen] = '\0'; 1013e230dd2SCorentin Chary 1023e230dd2SCorentin Chary return addr; 1033e230dd2SCorentin Chary } 1043e230dd2SCorentin Chary 1053e230dd2SCorentin Chary 1063e230dd2SCorentin Chary char *vnc_socket_local_addr(const char *format, int fd) { 1073e230dd2SCorentin Chary struct sockaddr_storage sa; 1083e230dd2SCorentin Chary socklen_t salen; 1093e230dd2SCorentin Chary 1103e230dd2SCorentin Chary salen = sizeof(sa); 1113e230dd2SCorentin Chary if (getsockname(fd, (struct sockaddr*)&sa, &salen) < 0) 1123e230dd2SCorentin Chary return NULL; 1133e230dd2SCorentin Chary 1143e230dd2SCorentin Chary return addr_to_string(format, &sa, salen); 1153e230dd2SCorentin Chary } 1163e230dd2SCorentin Chary 1173e230dd2SCorentin Chary char *vnc_socket_remote_addr(const char *format, int fd) { 1183e230dd2SCorentin Chary struct sockaddr_storage sa; 1193e230dd2SCorentin Chary socklen_t salen; 1203e230dd2SCorentin Chary 1213e230dd2SCorentin Chary salen = sizeof(sa); 1223e230dd2SCorentin Chary if (getpeername(fd, (struct sockaddr*)&sa, &salen) < 0) 1233e230dd2SCorentin Chary return NULL; 1243e230dd2SCorentin Chary 1253e230dd2SCorentin Chary return addr_to_string(format, &sa, salen); 1263e230dd2SCorentin Chary } 1273e230dd2SCorentin Chary 128fb6ba0d5SWenchao Xia static VncBasicInfo *vnc_basic_info_get(struct sockaddr_storage *sa, 1293e230dd2SCorentin Chary socklen_t salen) 1303e230dd2SCorentin Chary { 131fb6ba0d5SWenchao Xia VncBasicInfo *info; 1323e230dd2SCorentin Chary char host[NI_MAXHOST]; 1333e230dd2SCorentin Chary char serv[NI_MAXSERV]; 1343e230dd2SCorentin Chary int err; 1353e230dd2SCorentin Chary 1363e230dd2SCorentin Chary if ((err = getnameinfo((struct sockaddr *)sa, salen, 1373e230dd2SCorentin Chary host, sizeof(host), 1383e230dd2SCorentin Chary serv, sizeof(serv), 1393e230dd2SCorentin Chary NI_NUMERICHOST | NI_NUMERICSERV)) != 0) { 1403e230dd2SCorentin Chary VNC_DEBUG("Cannot resolve address %d: %s\n", 1413e230dd2SCorentin Chary err, gai_strerror(err)); 142fb6ba0d5SWenchao Xia return NULL; 1433e230dd2SCorentin Chary } 1443e230dd2SCorentin Chary 145fb6ba0d5SWenchao Xia info = g_malloc0(sizeof(VncBasicInfo)); 146fb6ba0d5SWenchao Xia info->host = g_strdup(host); 147fb6ba0d5SWenchao Xia info->service = g_strdup(serv); 148fb6ba0d5SWenchao Xia info->family = inet_netfamily(sa->ss_family); 149fb6ba0d5SWenchao Xia return info; 1503e230dd2SCorentin Chary } 1513e230dd2SCorentin Chary 152fb6ba0d5SWenchao Xia static VncBasicInfo *vnc_basic_info_get_from_server_addr(int fd) 1533e230dd2SCorentin Chary { 1543e230dd2SCorentin Chary struct sockaddr_storage sa; 1553e230dd2SCorentin Chary socklen_t salen; 1563e230dd2SCorentin Chary 1573e230dd2SCorentin Chary salen = sizeof(sa); 1583e230dd2SCorentin Chary if (getsockname(fd, (struct sockaddr*)&sa, &salen) < 0) { 159fb6ba0d5SWenchao Xia return NULL; 1603e230dd2SCorentin Chary } 1613e230dd2SCorentin Chary 162fb6ba0d5SWenchao Xia return vnc_basic_info_get(&sa, salen); 1633e230dd2SCorentin Chary } 1643e230dd2SCorentin Chary 165fb6ba0d5SWenchao Xia static VncBasicInfo *vnc_basic_info_get_from_remote_addr(int fd) 1663e230dd2SCorentin Chary { 1673e230dd2SCorentin Chary struct sockaddr_storage sa; 1683e230dd2SCorentin Chary socklen_t salen; 1693e230dd2SCorentin Chary 1703e230dd2SCorentin Chary salen = sizeof(sa); 1713e230dd2SCorentin Chary if (getpeername(fd, (struct sockaddr*)&sa, &salen) < 0) { 172fb6ba0d5SWenchao Xia return NULL; 1733e230dd2SCorentin Chary } 1743e230dd2SCorentin Chary 175fb6ba0d5SWenchao Xia return vnc_basic_info_get(&sa, salen); 1763e230dd2SCorentin Chary } 1773e230dd2SCorentin Chary 1783e230dd2SCorentin Chary static const char *vnc_auth_name(VncDisplay *vd) { 1793e230dd2SCorentin Chary switch (vd->auth) { 1803e230dd2SCorentin Chary case VNC_AUTH_INVALID: 1813e230dd2SCorentin Chary return "invalid"; 1823e230dd2SCorentin Chary case VNC_AUTH_NONE: 1833e230dd2SCorentin Chary return "none"; 1843e230dd2SCorentin Chary case VNC_AUTH_VNC: 1853e230dd2SCorentin Chary return "vnc"; 1863e230dd2SCorentin Chary case VNC_AUTH_RA2: 1873e230dd2SCorentin Chary return "ra2"; 1883e230dd2SCorentin Chary case VNC_AUTH_RA2NE: 1893e230dd2SCorentin Chary return "ra2ne"; 1903e230dd2SCorentin Chary case VNC_AUTH_TIGHT: 1913e230dd2SCorentin Chary return "tight"; 1923e230dd2SCorentin Chary case VNC_AUTH_ULTRA: 1933e230dd2SCorentin Chary return "ultra"; 1943e230dd2SCorentin Chary case VNC_AUTH_TLS: 1953e230dd2SCorentin Chary return "tls"; 1963e230dd2SCorentin Chary case VNC_AUTH_VENCRYPT: 1973e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS 1983e230dd2SCorentin Chary switch (vd->subauth) { 1993e230dd2SCorentin Chary case VNC_AUTH_VENCRYPT_PLAIN: 2003e230dd2SCorentin Chary return "vencrypt+plain"; 2013e230dd2SCorentin Chary case VNC_AUTH_VENCRYPT_TLSNONE: 2023e230dd2SCorentin Chary return "vencrypt+tls+none"; 2033e230dd2SCorentin Chary case VNC_AUTH_VENCRYPT_TLSVNC: 2043e230dd2SCorentin Chary return "vencrypt+tls+vnc"; 2053e230dd2SCorentin Chary case VNC_AUTH_VENCRYPT_TLSPLAIN: 2063e230dd2SCorentin Chary return "vencrypt+tls+plain"; 2073e230dd2SCorentin Chary case VNC_AUTH_VENCRYPT_X509NONE: 2083e230dd2SCorentin Chary return "vencrypt+x509+none"; 2093e230dd2SCorentin Chary case VNC_AUTH_VENCRYPT_X509VNC: 2103e230dd2SCorentin Chary return "vencrypt+x509+vnc"; 2113e230dd2SCorentin Chary case VNC_AUTH_VENCRYPT_X509PLAIN: 2123e230dd2SCorentin Chary return "vencrypt+x509+plain"; 2133e230dd2SCorentin Chary case VNC_AUTH_VENCRYPT_TLSSASL: 2143e230dd2SCorentin Chary return "vencrypt+tls+sasl"; 2153e230dd2SCorentin Chary case VNC_AUTH_VENCRYPT_X509SASL: 2163e230dd2SCorentin Chary return "vencrypt+x509+sasl"; 2173e230dd2SCorentin Chary default: 2183e230dd2SCorentin Chary return "vencrypt"; 2193e230dd2SCorentin Chary } 2203e230dd2SCorentin Chary #else 2213e230dd2SCorentin Chary return "vencrypt"; 2223e230dd2SCorentin Chary #endif 2233e230dd2SCorentin Chary case VNC_AUTH_SASL: 2243e230dd2SCorentin Chary return "sasl"; 2253e230dd2SCorentin Chary } 2263e230dd2SCorentin Chary return "unknown"; 2273e230dd2SCorentin Chary } 2283e230dd2SCorentin Chary 229fb6ba0d5SWenchao Xia static VncServerInfo *vnc_server_info_get(void) 2303e230dd2SCorentin Chary { 231fb6ba0d5SWenchao Xia VncServerInfo *info; 232fb6ba0d5SWenchao Xia VncBasicInfo *bi = vnc_basic_info_get_from_server_addr(vnc_display->lsock); 233fb6ba0d5SWenchao Xia if (!bi) { 234fb6ba0d5SWenchao Xia return NULL; 2353e230dd2SCorentin Chary } 2363e230dd2SCorentin Chary 237fb6ba0d5SWenchao Xia info = g_malloc(sizeof(*info)); 238fb6ba0d5SWenchao Xia info->base = bi; 239fb6ba0d5SWenchao Xia info->has_auth = true; 240fb6ba0d5SWenchao Xia info->auth = g_strdup(vnc_auth_name(vnc_display)); 241fb6ba0d5SWenchao Xia return info; 2423e230dd2SCorentin Chary } 2433e230dd2SCorentin Chary 2443e230dd2SCorentin Chary static void vnc_client_cache_auth(VncState *client) 2453e230dd2SCorentin Chary { 2463e230dd2SCorentin Chary if (!client->info) { 2473e230dd2SCorentin Chary return; 2483e230dd2SCorentin Chary } 2493e230dd2SCorentin Chary 2503e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS 2513e230dd2SCorentin Chary if (client->tls.session && 2523e230dd2SCorentin Chary client->tls.dname) { 253fb6ba0d5SWenchao Xia client->info->has_x509_dname = true; 254fb6ba0d5SWenchao Xia client->info->x509_dname = g_strdup(client->tls.dname); 2553e230dd2SCorentin Chary } 2563e230dd2SCorentin Chary #endif 2573e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL 2583e230dd2SCorentin Chary if (client->sasl.conn && 2593e230dd2SCorentin Chary client->sasl.username) { 260fb6ba0d5SWenchao Xia client->info->has_sasl_username = true; 261fb6ba0d5SWenchao Xia client->info->sasl_username = g_strdup(client->sasl.username); 2623e230dd2SCorentin Chary } 2633e230dd2SCorentin Chary #endif 2643e230dd2SCorentin Chary } 2653e230dd2SCorentin Chary 2663e230dd2SCorentin Chary static void vnc_client_cache_addr(VncState *client) 2673e230dd2SCorentin Chary { 268fb6ba0d5SWenchao Xia VncBasicInfo *bi = vnc_basic_info_get_from_remote_addr(client->csock); 2693e230dd2SCorentin Chary 270fb6ba0d5SWenchao Xia if (bi) { 271fb6ba0d5SWenchao Xia client->info = g_malloc0(sizeof(*client->info)); 272fb6ba0d5SWenchao Xia client->info->base = bi; 273fb6ba0d5SWenchao Xia } 2743e230dd2SCorentin Chary } 2753e230dd2SCorentin Chary 276fb6ba0d5SWenchao Xia static void vnc_qmp_event(VncState *vs, QAPIEvent event) 2773e230dd2SCorentin Chary { 278fb6ba0d5SWenchao Xia VncServerInfo *si; 2793e230dd2SCorentin Chary 2803e230dd2SCorentin Chary if (!vs->info) { 2813e230dd2SCorentin Chary return; 2823e230dd2SCorentin Chary } 283fb6ba0d5SWenchao Xia g_assert(vs->info->base); 2843e230dd2SCorentin Chary 285fb6ba0d5SWenchao Xia si = vnc_server_info_get(); 286fb6ba0d5SWenchao Xia if (!si) { 2873e230dd2SCorentin Chary return; 2883e230dd2SCorentin Chary } 2893e230dd2SCorentin Chary 290fb6ba0d5SWenchao Xia switch (event) { 291fb6ba0d5SWenchao Xia case QAPI_EVENT_VNC_CONNECTED: 292fb6ba0d5SWenchao Xia qapi_event_send_vnc_connected(si, vs->info->base, &error_abort); 293fb6ba0d5SWenchao Xia break; 294fb6ba0d5SWenchao Xia case QAPI_EVENT_VNC_INITIALIZED: 295fb6ba0d5SWenchao Xia qapi_event_send_vnc_initialized(si, vs->info, &error_abort); 296fb6ba0d5SWenchao Xia break; 297fb6ba0d5SWenchao Xia case QAPI_EVENT_VNC_DISCONNECTED: 298fb6ba0d5SWenchao Xia qapi_event_send_vnc_disconnected(si, vs->info, &error_abort); 299fb6ba0d5SWenchao Xia break; 300fb6ba0d5SWenchao Xia default: 301fb6ba0d5SWenchao Xia break; 302fb6ba0d5SWenchao Xia } 3033e230dd2SCorentin Chary 304fb6ba0d5SWenchao Xia qapi_free_VncServerInfo(si); 3053e230dd2SCorentin Chary } 3063e230dd2SCorentin Chary 3072b54aa87SLuiz Capitulino static VncClientInfo *qmp_query_vnc_client(const VncState *client) 3083e230dd2SCorentin Chary { 3092b54aa87SLuiz Capitulino struct sockaddr_storage sa; 3102b54aa87SLuiz Capitulino socklen_t salen = sizeof(sa); 3112b54aa87SLuiz Capitulino char host[NI_MAXHOST]; 3122b54aa87SLuiz Capitulino char serv[NI_MAXSERV]; 3132b54aa87SLuiz Capitulino VncClientInfo *info; 3143e230dd2SCorentin Chary 3152b54aa87SLuiz Capitulino if (getpeername(client->csock, (struct sockaddr *)&sa, &salen) < 0) { 3162b54aa87SLuiz Capitulino return NULL; 3172b54aa87SLuiz Capitulino } 3182b54aa87SLuiz Capitulino 3192b54aa87SLuiz Capitulino if (getnameinfo((struct sockaddr *)&sa, salen, 3202b54aa87SLuiz Capitulino host, sizeof(host), 3212b54aa87SLuiz Capitulino serv, sizeof(serv), 3222b54aa87SLuiz Capitulino NI_NUMERICHOST | NI_NUMERICSERV) < 0) { 3232b54aa87SLuiz Capitulino return NULL; 3242b54aa87SLuiz Capitulino } 3252b54aa87SLuiz Capitulino 3262b54aa87SLuiz Capitulino info = g_malloc0(sizeof(*info)); 327a589569fSWenchao Xia info->base = g_malloc0(sizeof(*info->base)); 328a589569fSWenchao Xia info->base->host = g_strdup(host); 329a589569fSWenchao Xia info->base->service = g_strdup(serv); 330a589569fSWenchao Xia info->base->family = inet_netfamily(sa.ss_family); 3313e230dd2SCorentin Chary 3323e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS 3332b54aa87SLuiz Capitulino if (client->tls.session && client->tls.dname) { 3342b54aa87SLuiz Capitulino info->has_x509_dname = true; 3352b54aa87SLuiz Capitulino info->x509_dname = g_strdup(client->tls.dname); 3362b54aa87SLuiz Capitulino } 3373e230dd2SCorentin Chary #endif 3383e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL 3392b54aa87SLuiz Capitulino if (client->sasl.conn && client->sasl.username) { 3402b54aa87SLuiz Capitulino info->has_sasl_username = true; 3412b54aa87SLuiz Capitulino info->sasl_username = g_strdup(client->sasl.username); 3422b54aa87SLuiz Capitulino } 3433e230dd2SCorentin Chary #endif 3442b54aa87SLuiz Capitulino 3452b54aa87SLuiz Capitulino return info; 3463e230dd2SCorentin Chary } 3473e230dd2SCorentin Chary 3482b54aa87SLuiz Capitulino VncInfo *qmp_query_vnc(Error **errp) 3493e230dd2SCorentin Chary { 3502b54aa87SLuiz Capitulino VncInfo *info = g_malloc0(sizeof(*info)); 3513e230dd2SCorentin Chary 3523e230dd2SCorentin Chary if (vnc_display == NULL || vnc_display->display == NULL) { 3532b54aa87SLuiz Capitulino info->enabled = false; 3543e230dd2SCorentin Chary } else { 3552b54aa87SLuiz Capitulino VncClientInfoList *cur_item = NULL; 3562b54aa87SLuiz Capitulino struct sockaddr_storage sa; 3572b54aa87SLuiz Capitulino socklen_t salen = sizeof(sa); 3582b54aa87SLuiz Capitulino char host[NI_MAXHOST]; 3592b54aa87SLuiz Capitulino char serv[NI_MAXSERV]; 3603e230dd2SCorentin Chary VncState *client; 3613e230dd2SCorentin Chary 3622b54aa87SLuiz Capitulino info->enabled = true; 3632b54aa87SLuiz Capitulino 3642b54aa87SLuiz Capitulino /* for compatibility with the original command */ 3652b54aa87SLuiz Capitulino info->has_clients = true; 3662b54aa87SLuiz Capitulino 3673e230dd2SCorentin Chary QTAILQ_FOREACH(client, &vnc_display->clients, next) { 3682b54aa87SLuiz Capitulino VncClientInfoList *cinfo = g_malloc0(sizeof(*info)); 3692b54aa87SLuiz Capitulino cinfo->value = qmp_query_vnc_client(client); 3702b54aa87SLuiz Capitulino 3712b54aa87SLuiz Capitulino /* XXX: waiting for the qapi to support GSList */ 3722b54aa87SLuiz Capitulino if (!cur_item) { 3732b54aa87SLuiz Capitulino info->clients = cur_item = cinfo; 3742b54aa87SLuiz Capitulino } else { 3752b54aa87SLuiz Capitulino cur_item->next = cinfo; 3762b54aa87SLuiz Capitulino cur_item = cinfo; 3773e230dd2SCorentin Chary } 3783e230dd2SCorentin Chary } 3793e230dd2SCorentin Chary 380417b0b88SPaolo Bonzini if (vnc_display->lsock == -1) { 381417b0b88SPaolo Bonzini return info; 382417b0b88SPaolo Bonzini } 383417b0b88SPaolo Bonzini 3842b54aa87SLuiz Capitulino if (getsockname(vnc_display->lsock, (struct sockaddr *)&sa, 3852b54aa87SLuiz Capitulino &salen) == -1) { 3862b54aa87SLuiz Capitulino error_set(errp, QERR_UNDEFINED_ERROR); 3872b54aa87SLuiz Capitulino goto out_error; 3882b54aa87SLuiz Capitulino } 3893e230dd2SCorentin Chary 3902b54aa87SLuiz Capitulino if (getnameinfo((struct sockaddr *)&sa, salen, 3912b54aa87SLuiz Capitulino host, sizeof(host), 3922b54aa87SLuiz Capitulino serv, sizeof(serv), 3932b54aa87SLuiz Capitulino NI_NUMERICHOST | NI_NUMERICSERV) < 0) { 3942b54aa87SLuiz Capitulino error_set(errp, QERR_UNDEFINED_ERROR); 3952b54aa87SLuiz Capitulino goto out_error; 3963e230dd2SCorentin Chary } 3972b54aa87SLuiz Capitulino 3982b54aa87SLuiz Capitulino info->has_host = true; 3992b54aa87SLuiz Capitulino info->host = g_strdup(host); 4002b54aa87SLuiz Capitulino 4012b54aa87SLuiz Capitulino info->has_service = true; 4022b54aa87SLuiz Capitulino info->service = g_strdup(serv); 4032b54aa87SLuiz Capitulino 4042b54aa87SLuiz Capitulino info->has_family = true; 405a589569fSWenchao Xia info->family = inet_netfamily(sa.ss_family); 4062b54aa87SLuiz Capitulino 4072b54aa87SLuiz Capitulino info->has_auth = true; 4082b54aa87SLuiz Capitulino info->auth = g_strdup(vnc_auth_name(vnc_display)); 4093e230dd2SCorentin Chary } 4102b54aa87SLuiz Capitulino 4112b54aa87SLuiz Capitulino return info; 4122b54aa87SLuiz Capitulino 4132b54aa87SLuiz Capitulino out_error: 4142b54aa87SLuiz Capitulino qapi_free_VncInfo(info); 4152b54aa87SLuiz Capitulino return NULL; 4163e230dd2SCorentin Chary } 4173e230dd2SCorentin Chary 4183e230dd2SCorentin Chary /* TODO 4193e230dd2SCorentin Chary 1) Get the queue working for IO. 4203e230dd2SCorentin Chary 2) there is some weirdness when using the -S option (the screen is grey 4213e230dd2SCorentin Chary and not totally invalidated 4223e230dd2SCorentin Chary 3) resolutions > 1024 4233e230dd2SCorentin Chary */ 4243e230dd2SCorentin Chary 42538ee14f4SGerd Hoffmann static int vnc_update_client(VncState *vs, int has_dirty, bool sync); 4263e230dd2SCorentin Chary static void vnc_disconnect_start(VncState *vs); 4273e230dd2SCorentin Chary 4283e230dd2SCorentin Chary static void vnc_colordepth(VncState *vs); 4293e230dd2SCorentin Chary static void framebuffer_update_request(VncState *vs, int incremental, 4303e230dd2SCorentin Chary int x_position, int y_position, 4313e230dd2SCorentin Chary int w, int h); 4320f7b2864SGerd Hoffmann static void vnc_refresh(DisplayChangeListener *dcl); 4333e230dd2SCorentin Chary static int vnc_refresh_server_surface(VncDisplay *vd); 4343e230dd2SCorentin Chary 435bea60dd7SPeter Lieven static void vnc_set_area_dirty(DECLARE_BITMAP(dirty[VNC_MAX_HEIGHT], 436bea60dd7SPeter Lieven VNC_MAX_WIDTH / VNC_DIRTY_PIXELS_PER_BIT), 437bea60dd7SPeter Lieven int width, int height, 438bea60dd7SPeter Lieven int x, int y, int w, int h) { 43991937225SPeter Lieven /* this is needed this to ensure we updated all affected 44091937225SPeter Lieven * blocks if x % VNC_DIRTY_PIXELS_PER_BIT != 0 */ 441b4c85ddcSPeter Lieven w += (x % VNC_DIRTY_PIXELS_PER_BIT); 442b4c85ddcSPeter Lieven x -= (x % VNC_DIRTY_PIXELS_PER_BIT); 4433e230dd2SCorentin Chary 4449f64916dSGerd Hoffmann x = MIN(x, width); 4459f64916dSGerd Hoffmann y = MIN(y, height); 4469f64916dSGerd Hoffmann w = MIN(x + w, width) - x; 44791937225SPeter Lieven h = MIN(y + h, height); 4483e230dd2SCorentin Chary 449b4c85ddcSPeter Lieven for (; y < h; y++) { 450bea60dd7SPeter Lieven bitmap_set(dirty[y], x / VNC_DIRTY_PIXELS_PER_BIT, 45191937225SPeter Lieven DIV_ROUND_UP(w, VNC_DIRTY_PIXELS_PER_BIT)); 452b4c85ddcSPeter Lieven } 4533e230dd2SCorentin Chary } 4543e230dd2SCorentin Chary 455bea60dd7SPeter Lieven static void vnc_dpy_update(DisplayChangeListener *dcl, 456bea60dd7SPeter Lieven int x, int y, int w, int h) 457bea60dd7SPeter Lieven { 458bea60dd7SPeter Lieven VncDisplay *vd = container_of(dcl, VncDisplay, dcl); 459bea60dd7SPeter Lieven struct VncSurface *s = &vd->guest; 460bea60dd7SPeter Lieven int width = pixman_image_get_width(vd->server); 461bea60dd7SPeter Lieven int height = pixman_image_get_height(vd->server); 462bea60dd7SPeter Lieven 463bea60dd7SPeter Lieven vnc_set_area_dirty(s->dirty, width, height, x, y, w, h); 464bea60dd7SPeter Lieven } 465bea60dd7SPeter Lieven 4663e230dd2SCorentin Chary void vnc_framebuffer_update(VncState *vs, int x, int y, int w, int h, 4673e230dd2SCorentin Chary int32_t encoding) 4683e230dd2SCorentin Chary { 4693e230dd2SCorentin Chary vnc_write_u16(vs, x); 4703e230dd2SCorentin Chary vnc_write_u16(vs, y); 4713e230dd2SCorentin Chary vnc_write_u16(vs, w); 4723e230dd2SCorentin Chary vnc_write_u16(vs, h); 4733e230dd2SCorentin Chary 4743e230dd2SCorentin Chary vnc_write_s32(vs, encoding); 4753e230dd2SCorentin Chary } 4763e230dd2SCorentin Chary 4773e230dd2SCorentin Chary void buffer_reserve(Buffer *buffer, size_t len) 4783e230dd2SCorentin Chary { 4793e230dd2SCorentin Chary if ((buffer->capacity - buffer->offset) < len) { 4803e230dd2SCorentin Chary buffer->capacity += (len + 1024); 4817267c094SAnthony Liguori buffer->buffer = g_realloc(buffer->buffer, buffer->capacity); 4823e230dd2SCorentin Chary if (buffer->buffer == NULL) { 4833e230dd2SCorentin Chary fprintf(stderr, "vnc: out of memory\n"); 4843e230dd2SCorentin Chary exit(1); 4853e230dd2SCorentin Chary } 4863e230dd2SCorentin Chary } 4873e230dd2SCorentin Chary } 4883e230dd2SCorentin Chary 48971a8cdecSBlue Swirl static int buffer_empty(Buffer *buffer) 4903e230dd2SCorentin Chary { 4913e230dd2SCorentin Chary return buffer->offset == 0; 4923e230dd2SCorentin Chary } 4933e230dd2SCorentin Chary 4947536ee4bSTim Hardeck uint8_t *buffer_end(Buffer *buffer) 4953e230dd2SCorentin Chary { 4963e230dd2SCorentin Chary return buffer->buffer + buffer->offset; 4973e230dd2SCorentin Chary } 4983e230dd2SCorentin Chary 4993e230dd2SCorentin Chary void buffer_reset(Buffer *buffer) 5003e230dd2SCorentin Chary { 5013e230dd2SCorentin Chary buffer->offset = 0; 5023e230dd2SCorentin Chary } 5033e230dd2SCorentin Chary 5043e230dd2SCorentin Chary void buffer_free(Buffer *buffer) 5053e230dd2SCorentin Chary { 5067267c094SAnthony Liguori g_free(buffer->buffer); 5073e230dd2SCorentin Chary buffer->offset = 0; 5083e230dd2SCorentin Chary buffer->capacity = 0; 5093e230dd2SCorentin Chary buffer->buffer = NULL; 5103e230dd2SCorentin Chary } 5113e230dd2SCorentin Chary 5123e230dd2SCorentin Chary void buffer_append(Buffer *buffer, const void *data, size_t len) 5133e230dd2SCorentin Chary { 5143e230dd2SCorentin Chary memcpy(buffer->buffer + buffer->offset, data, len); 5153e230dd2SCorentin Chary buffer->offset += len; 5163e230dd2SCorentin Chary } 5173e230dd2SCorentin Chary 51832ed2680STim Hardeck void buffer_advance(Buffer *buf, size_t len) 51932ed2680STim Hardeck { 52032ed2680STim Hardeck memmove(buf->buffer, buf->buffer + len, 52132ed2680STim Hardeck (buf->offset - len)); 52232ed2680STim Hardeck buf->offset -= len; 52332ed2680STim Hardeck } 52432ed2680STim Hardeck 5253e230dd2SCorentin Chary static void vnc_desktop_resize(VncState *vs) 5263e230dd2SCorentin Chary { 5273e230dd2SCorentin Chary if (vs->csock == -1 || !vnc_has_feature(vs, VNC_FEATURE_RESIZE)) { 5283e230dd2SCorentin Chary return; 5293e230dd2SCorentin Chary } 530bea60dd7SPeter Lieven if (vs->client_width == pixman_image_get_width(vs->vd->server) && 531bea60dd7SPeter Lieven vs->client_height == pixman_image_get_height(vs->vd->server)) { 5323e230dd2SCorentin Chary return; 5333e230dd2SCorentin Chary } 534bea60dd7SPeter Lieven vs->client_width = pixman_image_get_width(vs->vd->server); 535bea60dd7SPeter Lieven vs->client_height = pixman_image_get_height(vs->vd->server); 536bd023f95SCorentin Chary vnc_lock_output(vs); 5373e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE); 5383e230dd2SCorentin Chary vnc_write_u8(vs, 0); 5393e230dd2SCorentin Chary vnc_write_u16(vs, 1); /* number of rects */ 5403e230dd2SCorentin Chary vnc_framebuffer_update(vs, 0, 0, vs->client_width, vs->client_height, 5413e230dd2SCorentin Chary VNC_ENCODING_DESKTOPRESIZE); 542bd023f95SCorentin Chary vnc_unlock_output(vs); 5433e230dd2SCorentin Chary vnc_flush(vs); 5443e230dd2SCorentin Chary } 5453e230dd2SCorentin Chary 546bd023f95SCorentin Chary static void vnc_abort_display_jobs(VncDisplay *vd) 547bd023f95SCorentin Chary { 548bd023f95SCorentin Chary VncState *vs; 549bd023f95SCorentin Chary 550bd023f95SCorentin Chary QTAILQ_FOREACH(vs, &vd->clients, next) { 551bd023f95SCorentin Chary vnc_lock_output(vs); 552bd023f95SCorentin Chary vs->abort = true; 553bd023f95SCorentin Chary vnc_unlock_output(vs); 554bd023f95SCorentin Chary } 555bd023f95SCorentin Chary QTAILQ_FOREACH(vs, &vd->clients, next) { 556bd023f95SCorentin Chary vnc_jobs_join(vs); 557bd023f95SCorentin Chary } 558bd023f95SCorentin Chary QTAILQ_FOREACH(vs, &vd->clients, next) { 559bd023f95SCorentin Chary vnc_lock_output(vs); 560bd023f95SCorentin Chary vs->abort = false; 561bd023f95SCorentin Chary vnc_unlock_output(vs); 562bd023f95SCorentin Chary } 563bd023f95SCorentin Chary } 564bd023f95SCorentin Chary 5659f64916dSGerd Hoffmann int vnc_server_fb_stride(VncDisplay *vd) 5669f64916dSGerd Hoffmann { 5679f64916dSGerd Hoffmann return pixman_image_get_stride(vd->server); 5689f64916dSGerd Hoffmann } 5699f64916dSGerd Hoffmann 5709f64916dSGerd Hoffmann void *vnc_server_fb_ptr(VncDisplay *vd, int x, int y) 5719f64916dSGerd Hoffmann { 5729f64916dSGerd Hoffmann uint8_t *ptr; 5739f64916dSGerd Hoffmann 5749f64916dSGerd Hoffmann ptr = (uint8_t *)pixman_image_get_data(vd->server); 5759f64916dSGerd Hoffmann ptr += y * vnc_server_fb_stride(vd); 5769f64916dSGerd Hoffmann ptr += x * VNC_SERVER_FB_BYTES; 5779f64916dSGerd Hoffmann return ptr; 5789f64916dSGerd Hoffmann } 5799f64916dSGerd Hoffmann 580c12aeb86SGerd Hoffmann static void vnc_dpy_switch(DisplayChangeListener *dcl, 581c12aeb86SGerd Hoffmann DisplaySurface *surface) 5823e230dd2SCorentin Chary { 58321ef45d7SGerd Hoffmann VncDisplay *vd = container_of(dcl, VncDisplay, dcl); 5843e230dd2SCorentin Chary VncState *vs; 585bea60dd7SPeter Lieven int width, height; 5863e230dd2SCorentin Chary 587bd023f95SCorentin Chary vnc_abort_display_jobs(vd); 588bd023f95SCorentin Chary 5893e230dd2SCorentin Chary /* server surface */ 5909f64916dSGerd Hoffmann qemu_pixman_image_unref(vd->server); 591d39fa6d8SGerd Hoffmann vd->ds = surface; 592bea60dd7SPeter Lieven width = MIN(VNC_MAX_WIDTH, ROUND_UP(surface_width(vd->ds), 593bea60dd7SPeter Lieven VNC_DIRTY_PIXELS_PER_BIT)); 594bea60dd7SPeter Lieven height = MIN(VNC_MAX_HEIGHT, surface_height(vd->ds)); 5959f64916dSGerd Hoffmann vd->server = pixman_image_create_bits(VNC_SERVER_FB_FORMAT, 596bea60dd7SPeter Lieven width, height, NULL, 0); 5973e230dd2SCorentin Chary 5983e230dd2SCorentin Chary /* guest surface */ 5999f64916dSGerd Hoffmann #if 0 /* FIXME */ 6003e230dd2SCorentin Chary if (ds_get_bytes_per_pixel(ds) != vd->guest.ds->pf.bytes_per_pixel) 6013e230dd2SCorentin Chary console_color_init(ds); 6029f64916dSGerd Hoffmann #endif 6039f64916dSGerd Hoffmann qemu_pixman_image_unref(vd->guest.fb); 604d39fa6d8SGerd Hoffmann vd->guest.fb = pixman_image_ref(surface->image); 605d39fa6d8SGerd Hoffmann vd->guest.format = surface->format; 606bea60dd7SPeter Lieven memset(vd->guest.dirty, 0x00, sizeof(vd->guest.dirty)); 607bea60dd7SPeter Lieven vnc_set_area_dirty(vd->guest.dirty, width, height, 0, 0, 608bea60dd7SPeter Lieven width, height); 6093e230dd2SCorentin Chary 6103e230dd2SCorentin Chary QTAILQ_FOREACH(vs, &vd->clients, next) { 6113e230dd2SCorentin Chary vnc_colordepth(vs); 6123e230dd2SCorentin Chary vnc_desktop_resize(vs); 6133e230dd2SCorentin Chary if (vs->vd->cursor) { 6143e230dd2SCorentin Chary vnc_cursor_define(vs); 6153e230dd2SCorentin Chary } 616bea60dd7SPeter Lieven memset(vs->dirty, 0x00, sizeof(vs->dirty)); 617bea60dd7SPeter Lieven vnc_set_area_dirty(vs->dirty, width, height, 0, 0, 618bea60dd7SPeter Lieven width, height); 6193e230dd2SCorentin Chary } 6203e230dd2SCorentin Chary } 6213e230dd2SCorentin Chary 6223e230dd2SCorentin Chary /* fastest code */ 6239f64916dSGerd Hoffmann static void vnc_write_pixels_copy(VncState *vs, 6243e230dd2SCorentin Chary void *pixels, int size) 6253e230dd2SCorentin Chary { 6263e230dd2SCorentin Chary vnc_write(vs, pixels, size); 6273e230dd2SCorentin Chary } 6283e230dd2SCorentin Chary 6293e230dd2SCorentin Chary /* slowest but generic code. */ 6303e230dd2SCorentin Chary void vnc_convert_pixel(VncState *vs, uint8_t *buf, uint32_t v) 6313e230dd2SCorentin Chary { 6323e230dd2SCorentin Chary uint8_t r, g, b; 6333e230dd2SCorentin Chary 6349f64916dSGerd Hoffmann #if VNC_SERVER_FB_FORMAT == PIXMAN_FORMAT(32, PIXMAN_TYPE_ARGB, 0, 8, 8, 8) 6359f64916dSGerd Hoffmann r = (((v & 0x00ff0000) >> 16) << vs->client_pf.rbits) >> 8; 6369f64916dSGerd Hoffmann g = (((v & 0x0000ff00) >> 8) << vs->client_pf.gbits) >> 8; 6379f64916dSGerd Hoffmann b = (((v & 0x000000ff) >> 0) << vs->client_pf.bbits) >> 8; 6389f64916dSGerd Hoffmann #else 6399f64916dSGerd Hoffmann # error need some bits here if you change VNC_SERVER_FB_FORMAT 6409f64916dSGerd Hoffmann #endif 6419f64916dSGerd Hoffmann v = (r << vs->client_pf.rshift) | 6429f64916dSGerd Hoffmann (g << vs->client_pf.gshift) | 6439f64916dSGerd Hoffmann (b << vs->client_pf.bshift); 6449f64916dSGerd Hoffmann switch (vs->client_pf.bytes_per_pixel) { 6453e230dd2SCorentin Chary case 1: 6463e230dd2SCorentin Chary buf[0] = v; 6473e230dd2SCorentin Chary break; 6483e230dd2SCorentin Chary case 2: 6499f64916dSGerd Hoffmann if (vs->client_be) { 6503e230dd2SCorentin Chary buf[0] = v >> 8; 6513e230dd2SCorentin Chary buf[1] = v; 6523e230dd2SCorentin Chary } else { 6533e230dd2SCorentin Chary buf[1] = v >> 8; 6543e230dd2SCorentin Chary buf[0] = v; 6553e230dd2SCorentin Chary } 6563e230dd2SCorentin Chary break; 6573e230dd2SCorentin Chary default: 6583e230dd2SCorentin Chary case 4: 6599f64916dSGerd Hoffmann if (vs->client_be) { 6603e230dd2SCorentin Chary buf[0] = v >> 24; 6613e230dd2SCorentin Chary buf[1] = v >> 16; 6623e230dd2SCorentin Chary buf[2] = v >> 8; 6633e230dd2SCorentin Chary buf[3] = v; 6643e230dd2SCorentin Chary } else { 6653e230dd2SCorentin Chary buf[3] = v >> 24; 6663e230dd2SCorentin Chary buf[2] = v >> 16; 6673e230dd2SCorentin Chary buf[1] = v >> 8; 6683e230dd2SCorentin Chary buf[0] = v; 6693e230dd2SCorentin Chary } 6703e230dd2SCorentin Chary break; 6713e230dd2SCorentin Chary } 6723e230dd2SCorentin Chary } 6733e230dd2SCorentin Chary 6749f64916dSGerd Hoffmann static void vnc_write_pixels_generic(VncState *vs, 6753e230dd2SCorentin Chary void *pixels1, int size) 6763e230dd2SCorentin Chary { 6773e230dd2SCorentin Chary uint8_t buf[4]; 6783e230dd2SCorentin Chary 6799f64916dSGerd Hoffmann if (VNC_SERVER_FB_BYTES == 4) { 6803e230dd2SCorentin Chary uint32_t *pixels = pixels1; 6813e230dd2SCorentin Chary int n, i; 6823e230dd2SCorentin Chary n = size >> 2; 6833e230dd2SCorentin Chary for (i = 0; i < n; i++) { 6843e230dd2SCorentin Chary vnc_convert_pixel(vs, buf, pixels[i]); 6859f64916dSGerd Hoffmann vnc_write(vs, buf, vs->client_pf.bytes_per_pixel); 6863e230dd2SCorentin Chary } 6873e230dd2SCorentin Chary } 6883e230dd2SCorentin Chary } 6893e230dd2SCorentin Chary 6903e230dd2SCorentin Chary int vnc_raw_send_framebuffer_update(VncState *vs, int x, int y, int w, int h) 6913e230dd2SCorentin Chary { 6923e230dd2SCorentin Chary int i; 6933e230dd2SCorentin Chary uint8_t *row; 6943e230dd2SCorentin Chary VncDisplay *vd = vs->vd; 6953e230dd2SCorentin Chary 6969f64916dSGerd Hoffmann row = vnc_server_fb_ptr(vd, x, y); 6973e230dd2SCorentin Chary for (i = 0; i < h; i++) { 6989f64916dSGerd Hoffmann vs->write_pixels(vs, row, w * VNC_SERVER_FB_BYTES); 6999f64916dSGerd Hoffmann row += vnc_server_fb_stride(vd); 7003e230dd2SCorentin Chary } 7013e230dd2SCorentin Chary return 1; 7023e230dd2SCorentin Chary } 7033e230dd2SCorentin Chary 704bd023f95SCorentin Chary int vnc_send_framebuffer_update(VncState *vs, int x, int y, int w, int h) 7053e230dd2SCorentin Chary { 7063e230dd2SCorentin Chary int n = 0; 7073e230dd2SCorentin Chary 7083e230dd2SCorentin Chary switch(vs->vnc_encoding) { 7093e230dd2SCorentin Chary case VNC_ENCODING_ZLIB: 7103e230dd2SCorentin Chary n = vnc_zlib_send_framebuffer_update(vs, x, y, w, h); 7113e230dd2SCorentin Chary break; 7123e230dd2SCorentin Chary case VNC_ENCODING_HEXTILE: 7133e230dd2SCorentin Chary vnc_framebuffer_update(vs, x, y, w, h, VNC_ENCODING_HEXTILE); 7143e230dd2SCorentin Chary n = vnc_hextile_send_framebuffer_update(vs, x, y, w, h); 7153e230dd2SCorentin Chary break; 7163e230dd2SCorentin Chary case VNC_ENCODING_TIGHT: 7173e230dd2SCorentin Chary n = vnc_tight_send_framebuffer_update(vs, x, y, w, h); 7183e230dd2SCorentin Chary break; 719efe556adSCorentin Chary case VNC_ENCODING_TIGHT_PNG: 720efe556adSCorentin Chary n = vnc_tight_png_send_framebuffer_update(vs, x, y, w, h); 721efe556adSCorentin Chary break; 722148954faSCorentin Chary case VNC_ENCODING_ZRLE: 723148954faSCorentin Chary n = vnc_zrle_send_framebuffer_update(vs, x, y, w, h); 724148954faSCorentin Chary break; 725148954faSCorentin Chary case VNC_ENCODING_ZYWRLE: 726148954faSCorentin Chary n = vnc_zywrle_send_framebuffer_update(vs, x, y, w, h); 727148954faSCorentin Chary break; 7283e230dd2SCorentin Chary default: 7293e230dd2SCorentin Chary vnc_framebuffer_update(vs, x, y, w, h, VNC_ENCODING_RAW); 7303e230dd2SCorentin Chary n = vnc_raw_send_framebuffer_update(vs, x, y, w, h); 7313e230dd2SCorentin Chary break; 7323e230dd2SCorentin Chary } 7333e230dd2SCorentin Chary return n; 7343e230dd2SCorentin Chary } 7353e230dd2SCorentin Chary 7363e230dd2SCorentin Chary static void vnc_copy(VncState *vs, int src_x, int src_y, int dst_x, int dst_y, int w, int h) 7373e230dd2SCorentin Chary { 7383e230dd2SCorentin Chary /* send bitblit op to the vnc client */ 739bd023f95SCorentin Chary vnc_lock_output(vs); 7403e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE); 7413e230dd2SCorentin Chary vnc_write_u8(vs, 0); 7423e230dd2SCorentin Chary vnc_write_u16(vs, 1); /* number of rects */ 7433e230dd2SCorentin Chary vnc_framebuffer_update(vs, dst_x, dst_y, w, h, VNC_ENCODING_COPYRECT); 7443e230dd2SCorentin Chary vnc_write_u16(vs, src_x); 7453e230dd2SCorentin Chary vnc_write_u16(vs, src_y); 746bd023f95SCorentin Chary vnc_unlock_output(vs); 7473e230dd2SCorentin Chary vnc_flush(vs); 7483e230dd2SCorentin Chary } 7493e230dd2SCorentin Chary 7507c20b4a3SGerd Hoffmann static void vnc_dpy_copy(DisplayChangeListener *dcl, 7517c20b4a3SGerd Hoffmann int src_x, int src_y, 7527c20b4a3SGerd Hoffmann int dst_x, int dst_y, int w, int h) 7533e230dd2SCorentin Chary { 75421ef45d7SGerd Hoffmann VncDisplay *vd = container_of(dcl, VncDisplay, dcl); 7553e230dd2SCorentin Chary VncState *vs, *vn; 7563e230dd2SCorentin Chary uint8_t *src_row; 7573e230dd2SCorentin Chary uint8_t *dst_row; 7589f64916dSGerd Hoffmann int i, x, y, pitch, inc, w_lim, s; 7593e230dd2SCorentin Chary int cmp_bytes; 7603e230dd2SCorentin Chary 7613e230dd2SCorentin Chary vnc_refresh_server_surface(vd); 7623e230dd2SCorentin Chary QTAILQ_FOREACH_SAFE(vs, &vd->clients, next, vn) { 7633e230dd2SCorentin Chary if (vnc_has_feature(vs, VNC_FEATURE_COPYRECT)) { 7643e230dd2SCorentin Chary vs->force_update = 1; 76538ee14f4SGerd Hoffmann vnc_update_client(vs, 1, true); 7663e230dd2SCorentin Chary /* vs might be free()ed here */ 7673e230dd2SCorentin Chary } 7683e230dd2SCorentin Chary } 7693e230dd2SCorentin Chary 7703e230dd2SCorentin Chary /* do bitblit op on the local surface too */ 7719f64916dSGerd Hoffmann pitch = vnc_server_fb_stride(vd); 7729f64916dSGerd Hoffmann src_row = vnc_server_fb_ptr(vd, src_x, src_y); 7739f64916dSGerd Hoffmann dst_row = vnc_server_fb_ptr(vd, dst_x, dst_y); 7743e230dd2SCorentin Chary y = dst_y; 7753e230dd2SCorentin Chary inc = 1; 7763e230dd2SCorentin Chary if (dst_y > src_y) { 7773e230dd2SCorentin Chary /* copy backwards */ 7783e230dd2SCorentin Chary src_row += pitch * (h-1); 7793e230dd2SCorentin Chary dst_row += pitch * (h-1); 7803e230dd2SCorentin Chary pitch = -pitch; 7813e230dd2SCorentin Chary y = dst_y + h - 1; 7823e230dd2SCorentin Chary inc = -1; 7833e230dd2SCorentin Chary } 784b4c85ddcSPeter Lieven w_lim = w - (VNC_DIRTY_PIXELS_PER_BIT - (dst_x % VNC_DIRTY_PIXELS_PER_BIT)); 785b4c85ddcSPeter Lieven if (w_lim < 0) { 7863e230dd2SCorentin Chary w_lim = w; 787b4c85ddcSPeter Lieven } else { 788b4c85ddcSPeter Lieven w_lim = w - (w_lim % VNC_DIRTY_PIXELS_PER_BIT); 789b4c85ddcSPeter Lieven } 7903e230dd2SCorentin Chary for (i = 0; i < h; i++) { 7913e230dd2SCorentin Chary for (x = 0; x <= w_lim; 7923e230dd2SCorentin Chary x += s, src_row += cmp_bytes, dst_row += cmp_bytes) { 7933e230dd2SCorentin Chary if (x == w_lim) { 7943e230dd2SCorentin Chary if ((s = w - w_lim) == 0) 7953e230dd2SCorentin Chary break; 7963e230dd2SCorentin Chary } else if (!x) { 797b4c85ddcSPeter Lieven s = (VNC_DIRTY_PIXELS_PER_BIT - 798b4c85ddcSPeter Lieven (dst_x % VNC_DIRTY_PIXELS_PER_BIT)); 7993e230dd2SCorentin Chary s = MIN(s, w_lim); 8003e230dd2SCorentin Chary } else { 801b4c85ddcSPeter Lieven s = VNC_DIRTY_PIXELS_PER_BIT; 8023e230dd2SCorentin Chary } 8039f64916dSGerd Hoffmann cmp_bytes = s * VNC_SERVER_FB_BYTES; 8043e230dd2SCorentin Chary if (memcmp(src_row, dst_row, cmp_bytes) == 0) 8053e230dd2SCorentin Chary continue; 8063e230dd2SCorentin Chary memmove(dst_row, src_row, cmp_bytes); 8073e230dd2SCorentin Chary QTAILQ_FOREACH(vs, &vd->clients, next) { 8083e230dd2SCorentin Chary if (!vnc_has_feature(vs, VNC_FEATURE_COPYRECT)) { 809b4c85ddcSPeter Lieven set_bit(((x + dst_x) / VNC_DIRTY_PIXELS_PER_BIT), 810b4c85ddcSPeter Lieven vs->dirty[y]); 8113e230dd2SCorentin Chary } 8123e230dd2SCorentin Chary } 8133e230dd2SCorentin Chary } 8149f64916dSGerd Hoffmann src_row += pitch - w * VNC_SERVER_FB_BYTES; 8159f64916dSGerd Hoffmann dst_row += pitch - w * VNC_SERVER_FB_BYTES; 8163e230dd2SCorentin Chary y += inc; 8173e230dd2SCorentin Chary } 8183e230dd2SCorentin Chary 8193e230dd2SCorentin Chary QTAILQ_FOREACH(vs, &vd->clients, next) { 8203e230dd2SCorentin Chary if (vnc_has_feature(vs, VNC_FEATURE_COPYRECT)) { 8213e230dd2SCorentin Chary vnc_copy(vs, src_x, src_y, dst_x, dst_y, w, h); 8223e230dd2SCorentin Chary } 8233e230dd2SCorentin Chary } 8243e230dd2SCorentin Chary } 8253e230dd2SCorentin Chary 8267c20b4a3SGerd Hoffmann static void vnc_mouse_set(DisplayChangeListener *dcl, 8277c20b4a3SGerd Hoffmann int x, int y, int visible) 8283e230dd2SCorentin Chary { 8293e230dd2SCorentin Chary /* can we ask the client(s) to move the pointer ??? */ 8303e230dd2SCorentin Chary } 8313e230dd2SCorentin Chary 8323e230dd2SCorentin Chary static int vnc_cursor_define(VncState *vs) 8333e230dd2SCorentin Chary { 8343e230dd2SCorentin Chary QEMUCursor *c = vs->vd->cursor; 8353e230dd2SCorentin Chary int isize; 8363e230dd2SCorentin Chary 8373e230dd2SCorentin Chary if (vnc_has_feature(vs, VNC_FEATURE_RICH_CURSOR)) { 838d01f9595SCorentin Chary vnc_lock_output(vs); 8393e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE); 8403e230dd2SCorentin Chary vnc_write_u8(vs, 0); /* padding */ 8413e230dd2SCorentin Chary vnc_write_u16(vs, 1); /* # of rects */ 8423e230dd2SCorentin Chary vnc_framebuffer_update(vs, c->hot_x, c->hot_y, c->width, c->height, 8433e230dd2SCorentin Chary VNC_ENCODING_RICH_CURSOR); 8449f64916dSGerd Hoffmann isize = c->width * c->height * vs->client_pf.bytes_per_pixel; 8459f64916dSGerd Hoffmann vnc_write_pixels_generic(vs, c->data, isize); 8463e230dd2SCorentin Chary vnc_write(vs, vs->vd->cursor_mask, vs->vd->cursor_msize); 847d01f9595SCorentin Chary vnc_unlock_output(vs); 8483e230dd2SCorentin Chary return 0; 8493e230dd2SCorentin Chary } 8503e230dd2SCorentin Chary return -1; 8513e230dd2SCorentin Chary } 8523e230dd2SCorentin Chary 8537c20b4a3SGerd Hoffmann static void vnc_dpy_cursor_define(DisplayChangeListener *dcl, 8547c20b4a3SGerd Hoffmann QEMUCursor *c) 8553e230dd2SCorentin Chary { 8563e230dd2SCorentin Chary VncDisplay *vd = vnc_display; 8573e230dd2SCorentin Chary VncState *vs; 8583e230dd2SCorentin Chary 8593e230dd2SCorentin Chary cursor_put(vd->cursor); 8607267c094SAnthony Liguori g_free(vd->cursor_mask); 8613e230dd2SCorentin Chary 8623e230dd2SCorentin Chary vd->cursor = c; 8633e230dd2SCorentin Chary cursor_get(vd->cursor); 8643e230dd2SCorentin Chary vd->cursor_msize = cursor_get_mono_bpl(c) * c->height; 8657267c094SAnthony Liguori vd->cursor_mask = g_malloc0(vd->cursor_msize); 8663e230dd2SCorentin Chary cursor_get_mono_mask(c, 0, vd->cursor_mask); 8673e230dd2SCorentin Chary 8683e230dd2SCorentin Chary QTAILQ_FOREACH(vs, &vd->clients, next) { 8693e230dd2SCorentin Chary vnc_cursor_define(vs); 8703e230dd2SCorentin Chary } 8713e230dd2SCorentin Chary } 8723e230dd2SCorentin Chary 8733e230dd2SCorentin Chary static int find_and_clear_dirty_height(struct VncState *vs, 8746c71a539SCorentin Chary int y, int last_x, int x, int height) 8753e230dd2SCorentin Chary { 8763e230dd2SCorentin Chary int h; 8773e230dd2SCorentin Chary 8786c71a539SCorentin Chary for (h = 1; h < (height - y); h++) { 879bc2429b9SCorentin Chary if (!test_bit(last_x, vs->dirty[y + h])) { 8803e230dd2SCorentin Chary break; 881bc2429b9SCorentin Chary } 882863d7c91SPeter Lieven bitmap_clear(vs->dirty[y + h], last_x, x - last_x); 8833e230dd2SCorentin Chary } 8843e230dd2SCorentin Chary 8853e230dd2SCorentin Chary return h; 8863e230dd2SCorentin Chary } 8873e230dd2SCorentin Chary 88838ee14f4SGerd Hoffmann static int vnc_update_client(VncState *vs, int has_dirty, bool sync) 8893e230dd2SCorentin Chary { 890*63658280SGerd Hoffmann vs->has_dirty += has_dirty; 8913e230dd2SCorentin Chary if (vs->need_update && vs->csock != -1) { 8923e230dd2SCorentin Chary VncDisplay *vd = vs->vd; 893bd023f95SCorentin Chary VncJob *job; 8943e230dd2SCorentin Chary int y; 8952f487a3dSPeter Lieven int height, width; 896bd023f95SCorentin Chary int n = 0; 897bd023f95SCorentin Chary 8983e230dd2SCorentin Chary if (vs->output.offset && !vs->audio_cap && !vs->force_update) 8993e230dd2SCorentin Chary /* kernel send buffers are full -> drop frames to throttle */ 9003e230dd2SCorentin Chary return 0; 9013e230dd2SCorentin Chary 902*63658280SGerd Hoffmann if (!vs->has_dirty && !vs->audio_cap && !vs->force_update) 9033e230dd2SCorentin Chary return 0; 9043e230dd2SCorentin Chary 9053e230dd2SCorentin Chary /* 9063e230dd2SCorentin Chary * Send screen updates to the vnc client using the server 9073e230dd2SCorentin Chary * surface and server dirty map. guest surface updates 9083e230dd2SCorentin Chary * happening in parallel don't disturb us, the next pass will 9093e230dd2SCorentin Chary * send them to the client. 9103e230dd2SCorentin Chary */ 911bd023f95SCorentin Chary job = vnc_job_new(vs); 9123e230dd2SCorentin Chary 913bea60dd7SPeter Lieven height = pixman_image_get_height(vd->server); 914bea60dd7SPeter Lieven width = pixman_image_get_width(vd->server); 9153e230dd2SCorentin Chary 91612b316d4SPeter Lieven y = 0; 91712b316d4SPeter Lieven for (;;) { 91812b316d4SPeter Lieven int x, h; 91912b316d4SPeter Lieven unsigned long x2; 92012b316d4SPeter Lieven unsigned long offset = find_next_bit((unsigned long *) &vs->dirty, 92112b316d4SPeter Lieven height * VNC_DIRTY_BPL(vs), 92212b316d4SPeter Lieven y * VNC_DIRTY_BPL(vs)); 92312b316d4SPeter Lieven if (offset == height * VNC_DIRTY_BPL(vs)) { 92412b316d4SPeter Lieven /* no more dirty bits */ 92512b316d4SPeter Lieven break; 9263e230dd2SCorentin Chary } 92712b316d4SPeter Lieven y = offset / VNC_DIRTY_BPL(vs); 92812b316d4SPeter Lieven x = offset % VNC_DIRTY_BPL(vs); 92912b316d4SPeter Lieven x2 = find_next_zero_bit((unsigned long *) &vs->dirty[y], 93012b316d4SPeter Lieven VNC_DIRTY_BPL(vs), x); 93112b316d4SPeter Lieven bitmap_clear(vs->dirty[y], x, x2 - x); 93212b316d4SPeter Lieven h = find_and_clear_dirty_height(vs, y, x, x2, height); 9332f487a3dSPeter Lieven x2 = MIN(x2, width / VNC_DIRTY_PIXELS_PER_BIT); 9342f487a3dSPeter Lieven if (x2 > x) { 93512b316d4SPeter Lieven n += vnc_job_add_rect(job, x * VNC_DIRTY_PIXELS_PER_BIT, y, 93612b316d4SPeter Lieven (x2 - x) * VNC_DIRTY_PIXELS_PER_BIT, h); 9373e230dd2SCorentin Chary } 9382f487a3dSPeter Lieven } 939bd023f95SCorentin Chary 940bd023f95SCorentin Chary vnc_job_push(job); 941eb214ff8SGerd Hoffmann if (sync) { 942eb214ff8SGerd Hoffmann vnc_jobs_join(vs); 943eb214ff8SGerd Hoffmann } 9443e230dd2SCorentin Chary vs->force_update = 0; 945*63658280SGerd Hoffmann vs->has_dirty = 0; 946bd023f95SCorentin Chary return n; 9473e230dd2SCorentin Chary } 9483e230dd2SCorentin Chary 94938ee14f4SGerd Hoffmann if (vs->csock == -1) { 9503e230dd2SCorentin Chary vnc_disconnect_finish(vs); 95138ee14f4SGerd Hoffmann } else if (sync) { 95238ee14f4SGerd Hoffmann vnc_jobs_join(vs); 95338ee14f4SGerd Hoffmann } 9543e230dd2SCorentin Chary 9553e230dd2SCorentin Chary return 0; 9563e230dd2SCorentin Chary } 9573e230dd2SCorentin Chary 9583e230dd2SCorentin Chary /* audio */ 9593e230dd2SCorentin Chary static void audio_capture_notify(void *opaque, audcnotification_e cmd) 9603e230dd2SCorentin Chary { 9613e230dd2SCorentin Chary VncState *vs = opaque; 9623e230dd2SCorentin Chary 9633e230dd2SCorentin Chary switch (cmd) { 9643e230dd2SCorentin Chary case AUD_CNOTIFY_DISABLE: 965bd023f95SCorentin Chary vnc_lock_output(vs); 9663e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_QEMU); 9673e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_QEMU_AUDIO); 9683e230dd2SCorentin Chary vnc_write_u16(vs, VNC_MSG_SERVER_QEMU_AUDIO_END); 969bd023f95SCorentin Chary vnc_unlock_output(vs); 9703e230dd2SCorentin Chary vnc_flush(vs); 9713e230dd2SCorentin Chary break; 9723e230dd2SCorentin Chary 9733e230dd2SCorentin Chary case AUD_CNOTIFY_ENABLE: 974bd023f95SCorentin Chary vnc_lock_output(vs); 9753e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_QEMU); 9763e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_QEMU_AUDIO); 9773e230dd2SCorentin Chary vnc_write_u16(vs, VNC_MSG_SERVER_QEMU_AUDIO_BEGIN); 978bd023f95SCorentin Chary vnc_unlock_output(vs); 9793e230dd2SCorentin Chary vnc_flush(vs); 9803e230dd2SCorentin Chary break; 9813e230dd2SCorentin Chary } 9823e230dd2SCorentin Chary } 9833e230dd2SCorentin Chary 9843e230dd2SCorentin Chary static void audio_capture_destroy(void *opaque) 9853e230dd2SCorentin Chary { 9863e230dd2SCorentin Chary } 9873e230dd2SCorentin Chary 9883e230dd2SCorentin Chary static void audio_capture(void *opaque, void *buf, int size) 9893e230dd2SCorentin Chary { 9903e230dd2SCorentin Chary VncState *vs = opaque; 9913e230dd2SCorentin Chary 992bd023f95SCorentin Chary vnc_lock_output(vs); 9933e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_QEMU); 9943e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_QEMU_AUDIO); 9953e230dd2SCorentin Chary vnc_write_u16(vs, VNC_MSG_SERVER_QEMU_AUDIO_DATA); 9963e230dd2SCorentin Chary vnc_write_u32(vs, size); 9973e230dd2SCorentin Chary vnc_write(vs, buf, size); 998bd023f95SCorentin Chary vnc_unlock_output(vs); 9993e230dd2SCorentin Chary vnc_flush(vs); 10003e230dd2SCorentin Chary } 10013e230dd2SCorentin Chary 10023e230dd2SCorentin Chary static void audio_add(VncState *vs) 10033e230dd2SCorentin Chary { 10043e230dd2SCorentin Chary struct audio_capture_ops ops; 10053e230dd2SCorentin Chary 10063e230dd2SCorentin Chary if (vs->audio_cap) { 1007027a79c3SCole Robinson error_report("audio already running"); 10083e230dd2SCorentin Chary return; 10093e230dd2SCorentin Chary } 10103e230dd2SCorentin Chary 10113e230dd2SCorentin Chary ops.notify = audio_capture_notify; 10123e230dd2SCorentin Chary ops.destroy = audio_capture_destroy; 10133e230dd2SCorentin Chary ops.capture = audio_capture; 10143e230dd2SCorentin Chary 10153e230dd2SCorentin Chary vs->audio_cap = AUD_add_capture(&vs->as, &ops, vs); 10163e230dd2SCorentin Chary if (!vs->audio_cap) { 1017027a79c3SCole Robinson error_report("Failed to add audio capture"); 10183e230dd2SCorentin Chary } 10193e230dd2SCorentin Chary } 10203e230dd2SCorentin Chary 10213e230dd2SCorentin Chary static void audio_del(VncState *vs) 10223e230dd2SCorentin Chary { 10233e230dd2SCorentin Chary if (vs->audio_cap) { 10243e230dd2SCorentin Chary AUD_del_capture(vs->audio_cap, vs); 10253e230dd2SCorentin Chary vs->audio_cap = NULL; 10263e230dd2SCorentin Chary } 10273e230dd2SCorentin Chary } 10283e230dd2SCorentin Chary 10293e230dd2SCorentin Chary static void vnc_disconnect_start(VncState *vs) 10303e230dd2SCorentin Chary { 10313e230dd2SCorentin Chary if (vs->csock == -1) 10323e230dd2SCorentin Chary return; 10338cf36489SGerd Hoffmann vnc_set_share_mode(vs, VNC_SHARE_MODE_DISCONNECTED); 10343e230dd2SCorentin Chary qemu_set_fd_handler2(vs->csock, NULL, NULL, NULL, NULL); 10353e230dd2SCorentin Chary closesocket(vs->csock); 10363e230dd2SCorentin Chary vs->csock = -1; 10373e230dd2SCorentin Chary } 10383e230dd2SCorentin Chary 10397536ee4bSTim Hardeck void vnc_disconnect_finish(VncState *vs) 10403e230dd2SCorentin Chary { 10417d964c9dSCorentin Chary int i; 10427d964c9dSCorentin Chary 1043bd023f95SCorentin Chary vnc_jobs_join(vs); /* Wait encoding jobs */ 1044bd023f95SCorentin Chary 1045bd023f95SCorentin Chary vnc_lock_output(vs); 1046fb6ba0d5SWenchao Xia vnc_qmp_event(vs, QAPI_EVENT_VNC_DISCONNECTED); 10473e230dd2SCorentin Chary 10483e230dd2SCorentin Chary buffer_free(&vs->input); 10493e230dd2SCorentin Chary buffer_free(&vs->output); 10507536ee4bSTim Hardeck #ifdef CONFIG_VNC_WS 10517536ee4bSTim Hardeck buffer_free(&vs->ws_input); 10527536ee4bSTim Hardeck buffer_free(&vs->ws_output); 10537536ee4bSTim Hardeck #endif /* CONFIG_VNC_WS */ 10543e230dd2SCorentin Chary 1055fb6ba0d5SWenchao Xia qapi_free_VncClientInfo(vs->info); 10563e230dd2SCorentin Chary 10573e230dd2SCorentin Chary vnc_zlib_clear(vs); 10583e230dd2SCorentin Chary vnc_tight_clear(vs); 1059148954faSCorentin Chary vnc_zrle_clear(vs); 10603e230dd2SCorentin Chary 10613e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS 10623e230dd2SCorentin Chary vnc_tls_client_cleanup(vs); 10633e230dd2SCorentin Chary #endif /* CONFIG_VNC_TLS */ 10643e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL 10653e230dd2SCorentin Chary vnc_sasl_client_cleanup(vs); 10663e230dd2SCorentin Chary #endif /* CONFIG_VNC_SASL */ 10673e230dd2SCorentin Chary audio_del(vs); 10687bc9318bSGerd Hoffmann vnc_release_modifiers(vs); 10693e230dd2SCorentin Chary 10706fd8e79aSTim Hardeck if (vs->initialized) { 10713e230dd2SCorentin Chary QTAILQ_REMOVE(&vs->vd->clients, vs, next); 10726fd8e79aSTim Hardeck qemu_remove_mouse_mode_change_notifier(&vs->mouse_mode_notifier); 10736fd8e79aSTim Hardeck } 10743e230dd2SCorentin Chary 10753e230dd2SCorentin Chary if (vs->vd->lock_key_sync) 10763e230dd2SCorentin Chary qemu_remove_led_event_handler(vs->led); 1077bd023f95SCorentin Chary vnc_unlock_output(vs); 1078bd023f95SCorentin Chary 1079bd023f95SCorentin Chary qemu_mutex_destroy(&vs->output_mutex); 10806fd8e79aSTim Hardeck if (vs->bh != NULL) { 1081175b2a6eSCorentin Chary qemu_bh_delete(vs->bh); 10826fd8e79aSTim Hardeck } 1083175b2a6eSCorentin Chary buffer_free(&vs->jobs_buffer); 1084175b2a6eSCorentin Chary 10857d964c9dSCorentin Chary for (i = 0; i < VNC_STAT_ROWS; ++i) { 10867267c094SAnthony Liguori g_free(vs->lossy_rect[i]); 10877d964c9dSCorentin Chary } 10887267c094SAnthony Liguori g_free(vs->lossy_rect); 10897267c094SAnthony Liguori g_free(vs); 10903e230dd2SCorentin Chary } 10913e230dd2SCorentin Chary 10923e230dd2SCorentin Chary int vnc_client_io_error(VncState *vs, int ret, int last_errno) 10933e230dd2SCorentin Chary { 10943e230dd2SCorentin Chary if (ret == 0 || ret == -1) { 10953e230dd2SCorentin Chary if (ret == -1) { 10963e230dd2SCorentin Chary switch (last_errno) { 10973e230dd2SCorentin Chary case EINTR: 10983e230dd2SCorentin Chary case EAGAIN: 10993e230dd2SCorentin Chary #ifdef _WIN32 11003e230dd2SCorentin Chary case WSAEWOULDBLOCK: 11013e230dd2SCorentin Chary #endif 11023e230dd2SCorentin Chary return 0; 11033e230dd2SCorentin Chary default: 11043e230dd2SCorentin Chary break; 11053e230dd2SCorentin Chary } 11063e230dd2SCorentin Chary } 11073e230dd2SCorentin Chary 11083e230dd2SCorentin Chary VNC_DEBUG("Closing down client sock: ret %d, errno %d\n", 11093e230dd2SCorentin Chary ret, ret < 0 ? last_errno : 0); 11103e230dd2SCorentin Chary vnc_disconnect_start(vs); 11113e230dd2SCorentin Chary 11123e230dd2SCorentin Chary return 0; 11133e230dd2SCorentin Chary } 11143e230dd2SCorentin Chary return ret; 11153e230dd2SCorentin Chary } 11163e230dd2SCorentin Chary 11173e230dd2SCorentin Chary 11183e230dd2SCorentin Chary void vnc_client_error(VncState *vs) 11193e230dd2SCorentin Chary { 11203e230dd2SCorentin Chary VNC_DEBUG("Closing down client sock: protocol error\n"); 11213e230dd2SCorentin Chary vnc_disconnect_start(vs); 11223e230dd2SCorentin Chary } 11233e230dd2SCorentin Chary 11240057a0d5STim Hardeck #ifdef CONFIG_VNC_TLS 11250057a0d5STim Hardeck static long vnc_client_write_tls(gnutls_session_t *session, 11260057a0d5STim Hardeck const uint8_t *data, 11270057a0d5STim Hardeck size_t datalen) 11280057a0d5STim Hardeck { 11290057a0d5STim Hardeck long ret = gnutls_write(*session, data, datalen); 11300057a0d5STim Hardeck if (ret < 0) { 11310057a0d5STim Hardeck if (ret == GNUTLS_E_AGAIN) { 11320057a0d5STim Hardeck errno = EAGAIN; 11330057a0d5STim Hardeck } else { 11340057a0d5STim Hardeck errno = EIO; 11350057a0d5STim Hardeck } 11360057a0d5STim Hardeck ret = -1; 11370057a0d5STim Hardeck } 11380057a0d5STim Hardeck return ret; 11390057a0d5STim Hardeck } 11400057a0d5STim Hardeck #endif /* CONFIG_VNC_TLS */ 11413e230dd2SCorentin Chary 11423e230dd2SCorentin Chary /* 11433e230dd2SCorentin Chary * Called to write a chunk of data to the client socket. The data may 11443e230dd2SCorentin Chary * be the raw data, or may have already been encoded by SASL. 11453e230dd2SCorentin Chary * The data will be written either straight onto the socket, or 11463e230dd2SCorentin Chary * written via the GNUTLS wrappers, if TLS/SSL encryption is enabled 11473e230dd2SCorentin Chary * 11483e230dd2SCorentin Chary * NB, it is theoretically possible to have 2 layers of encryption, 11493e230dd2SCorentin Chary * both SASL, and this TLS layer. It is highly unlikely in practice 11503e230dd2SCorentin Chary * though, since SASL encryption will typically be a no-op if TLS 11513e230dd2SCorentin Chary * is active 11523e230dd2SCorentin Chary * 11533e230dd2SCorentin Chary * Returns the number of bytes written, which may be less than 11543e230dd2SCorentin Chary * the requested 'datalen' if the socket would block. Returns 11553e230dd2SCorentin Chary * -1 on error, and disconnects the client socket. 11563e230dd2SCorentin Chary */ 11573e230dd2SCorentin Chary long vnc_client_write_buf(VncState *vs, const uint8_t *data, size_t datalen) 11583e230dd2SCorentin Chary { 11593e230dd2SCorentin Chary long ret; 11603e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS 11613e230dd2SCorentin Chary if (vs->tls.session) { 11620057a0d5STim Hardeck ret = vnc_client_write_tls(&vs->tls.session, data, datalen); 11630057a0d5STim Hardeck } else { 11640057a0d5STim Hardeck #ifdef CONFIG_VNC_WS 11650057a0d5STim Hardeck if (vs->ws_tls.session) { 11660057a0d5STim Hardeck ret = vnc_client_write_tls(&vs->ws_tls.session, data, datalen); 11673e230dd2SCorentin Chary } else 11680057a0d5STim Hardeck #endif /* CONFIG_VNC_WS */ 11693e230dd2SCorentin Chary #endif /* CONFIG_VNC_TLS */ 11700057a0d5STim Hardeck { 11713e230dd2SCorentin Chary ret = send(vs->csock, (const void *)data, datalen, 0); 11720057a0d5STim Hardeck } 11730057a0d5STim Hardeck #ifdef CONFIG_VNC_TLS 11740057a0d5STim Hardeck } 11750057a0d5STim Hardeck #endif /* CONFIG_VNC_TLS */ 11763e230dd2SCorentin Chary VNC_DEBUG("Wrote wire %p %zd -> %ld\n", data, datalen, ret); 11773e230dd2SCorentin Chary return vnc_client_io_error(vs, ret, socket_error()); 11783e230dd2SCorentin Chary } 11793e230dd2SCorentin Chary 11803e230dd2SCorentin Chary 11813e230dd2SCorentin Chary /* 11823e230dd2SCorentin Chary * Called to write buffered data to the client socket, when not 11833e230dd2SCorentin Chary * using any SASL SSF encryption layers. Will write as much data 11843e230dd2SCorentin Chary * as possible without blocking. If all buffered data is written, 11853e230dd2SCorentin Chary * will switch the FD poll() handler back to read monitoring. 11863e230dd2SCorentin Chary * 11873e230dd2SCorentin Chary * Returns the number of bytes written, which may be less than 11883e230dd2SCorentin Chary * the buffered output data if the socket would block. Returns 11893e230dd2SCorentin Chary * -1 on error, and disconnects the client socket. 11903e230dd2SCorentin Chary */ 11913e230dd2SCorentin Chary static long vnc_client_write_plain(VncState *vs) 11923e230dd2SCorentin Chary { 11933e230dd2SCorentin Chary long ret; 11943e230dd2SCorentin Chary 11953e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL 11963e230dd2SCorentin Chary VNC_DEBUG("Write Plain: Pending output %p size %zd offset %zd. Wait SSF %d\n", 11973e230dd2SCorentin Chary vs->output.buffer, vs->output.capacity, vs->output.offset, 11983e230dd2SCorentin Chary vs->sasl.waitWriteSSF); 11993e230dd2SCorentin Chary 12003e230dd2SCorentin Chary if (vs->sasl.conn && 12013e230dd2SCorentin Chary vs->sasl.runSSF && 12023e230dd2SCorentin Chary vs->sasl.waitWriteSSF) { 12033e230dd2SCorentin Chary ret = vnc_client_write_buf(vs, vs->output.buffer, vs->sasl.waitWriteSSF); 12043e230dd2SCorentin Chary if (ret) 12053e230dd2SCorentin Chary vs->sasl.waitWriteSSF -= ret; 12063e230dd2SCorentin Chary } else 12073e230dd2SCorentin Chary #endif /* CONFIG_VNC_SASL */ 12083e230dd2SCorentin Chary ret = vnc_client_write_buf(vs, vs->output.buffer, vs->output.offset); 12093e230dd2SCorentin Chary if (!ret) 12103e230dd2SCorentin Chary return 0; 12113e230dd2SCorentin Chary 121232ed2680STim Hardeck buffer_advance(&vs->output, ret); 12133e230dd2SCorentin Chary 12143e230dd2SCorentin Chary if (vs->output.offset == 0) { 12153e230dd2SCorentin Chary qemu_set_fd_handler2(vs->csock, NULL, vnc_client_read, NULL, vs); 12163e230dd2SCorentin Chary } 12173e230dd2SCorentin Chary 12183e230dd2SCorentin Chary return ret; 12193e230dd2SCorentin Chary } 12203e230dd2SCorentin Chary 12213e230dd2SCorentin Chary 12223e230dd2SCorentin Chary /* 12233e230dd2SCorentin Chary * First function called whenever there is data to be written to 12243e230dd2SCorentin Chary * the client socket. Will delegate actual work according to whether 12253e230dd2SCorentin Chary * SASL SSF layers are enabled (thus requiring encryption calls) 12263e230dd2SCorentin Chary */ 1227bd023f95SCorentin Chary static void vnc_client_write_locked(void *opaque) 12283e230dd2SCorentin Chary { 12293e230dd2SCorentin Chary VncState *vs = opaque; 12303e230dd2SCorentin Chary 12313e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL 12323e230dd2SCorentin Chary if (vs->sasl.conn && 12333e230dd2SCorentin Chary vs->sasl.runSSF && 12343e230dd2SCorentin Chary !vs->sasl.waitWriteSSF) { 12353e230dd2SCorentin Chary vnc_client_write_sasl(vs); 12363e230dd2SCorentin Chary } else 12373e230dd2SCorentin Chary #endif /* CONFIG_VNC_SASL */ 12387536ee4bSTim Hardeck { 12397536ee4bSTim Hardeck #ifdef CONFIG_VNC_WS 12407536ee4bSTim Hardeck if (vs->encode_ws) { 12417536ee4bSTim Hardeck vnc_client_write_ws(vs); 12427536ee4bSTim Hardeck } else 12437536ee4bSTim Hardeck #endif /* CONFIG_VNC_WS */ 12447536ee4bSTim Hardeck { 12453e230dd2SCorentin Chary vnc_client_write_plain(vs); 12463e230dd2SCorentin Chary } 12477536ee4bSTim Hardeck } 12487536ee4bSTim Hardeck } 12493e230dd2SCorentin Chary 1250bd023f95SCorentin Chary void vnc_client_write(void *opaque) 1251bd023f95SCorentin Chary { 1252bd023f95SCorentin Chary VncState *vs = opaque; 1253bd023f95SCorentin Chary 1254bd023f95SCorentin Chary vnc_lock_output(vs); 12557536ee4bSTim Hardeck if (vs->output.offset 12567536ee4bSTim Hardeck #ifdef CONFIG_VNC_WS 12577536ee4bSTim Hardeck || vs->ws_output.offset 12587536ee4bSTim Hardeck #endif 12597536ee4bSTim Hardeck ) { 1260bd023f95SCorentin Chary vnc_client_write_locked(opaque); 1261ac71103dSYoshiaki Tamura } else if (vs->csock != -1) { 1262bd023f95SCorentin Chary qemu_set_fd_handler2(vs->csock, NULL, vnc_client_read, NULL, vs); 1263bd023f95SCorentin Chary } 1264bd023f95SCorentin Chary vnc_unlock_output(vs); 1265bd023f95SCorentin Chary } 1266bd023f95SCorentin Chary 12673e230dd2SCorentin Chary void vnc_read_when(VncState *vs, VncReadEvent *func, size_t expecting) 12683e230dd2SCorentin Chary { 12693e230dd2SCorentin Chary vs->read_handler = func; 12703e230dd2SCorentin Chary vs->read_handler_expect = expecting; 12713e230dd2SCorentin Chary } 12723e230dd2SCorentin Chary 12730057a0d5STim Hardeck #ifdef CONFIG_VNC_TLS 12740057a0d5STim Hardeck static long vnc_client_read_tls(gnutls_session_t *session, uint8_t *data, 12750057a0d5STim Hardeck size_t datalen) 12760057a0d5STim Hardeck { 12770057a0d5STim Hardeck long ret = gnutls_read(*session, data, datalen); 12780057a0d5STim Hardeck if (ret < 0) { 12790057a0d5STim Hardeck if (ret == GNUTLS_E_AGAIN) { 12800057a0d5STim Hardeck errno = EAGAIN; 12810057a0d5STim Hardeck } else { 12820057a0d5STim Hardeck errno = EIO; 12830057a0d5STim Hardeck } 12840057a0d5STim Hardeck ret = -1; 12850057a0d5STim Hardeck } 12860057a0d5STim Hardeck return ret; 12870057a0d5STim Hardeck } 12880057a0d5STim Hardeck #endif /* CONFIG_VNC_TLS */ 12893e230dd2SCorentin Chary 12903e230dd2SCorentin Chary /* 12913e230dd2SCorentin Chary * Called to read a chunk of data from the client socket. The data may 12923e230dd2SCorentin Chary * be the raw data, or may need to be further decoded by SASL. 12933e230dd2SCorentin Chary * The data will be read either straight from to the socket, or 12943e230dd2SCorentin Chary * read via the GNUTLS wrappers, if TLS/SSL encryption is enabled 12953e230dd2SCorentin Chary * 12963e230dd2SCorentin Chary * NB, it is theoretically possible to have 2 layers of encryption, 12973e230dd2SCorentin Chary * both SASL, and this TLS layer. It is highly unlikely in practice 12983e230dd2SCorentin Chary * though, since SASL encryption will typically be a no-op if TLS 12993e230dd2SCorentin Chary * is active 13003e230dd2SCorentin Chary * 13013e230dd2SCorentin Chary * Returns the number of bytes read, which may be less than 13023e230dd2SCorentin Chary * the requested 'datalen' if the socket would block. Returns 13033e230dd2SCorentin Chary * -1 on error, and disconnects the client socket. 13043e230dd2SCorentin Chary */ 13053e230dd2SCorentin Chary long vnc_client_read_buf(VncState *vs, uint8_t *data, size_t datalen) 13063e230dd2SCorentin Chary { 13073e230dd2SCorentin Chary long ret; 13083e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS 13093e230dd2SCorentin Chary if (vs->tls.session) { 13100057a0d5STim Hardeck ret = vnc_client_read_tls(&vs->tls.session, data, datalen); 13110057a0d5STim Hardeck } else { 13120057a0d5STim Hardeck #ifdef CONFIG_VNC_WS 13130057a0d5STim Hardeck if (vs->ws_tls.session) { 13140057a0d5STim Hardeck ret = vnc_client_read_tls(&vs->ws_tls.session, data, datalen); 13153e230dd2SCorentin Chary } else 13160057a0d5STim Hardeck #endif /* CONFIG_VNC_WS */ 13173e230dd2SCorentin Chary #endif /* CONFIG_VNC_TLS */ 13180057a0d5STim Hardeck { 131900aa0040SBlue Swirl ret = qemu_recv(vs->csock, data, datalen, 0); 13200057a0d5STim Hardeck } 13210057a0d5STim Hardeck #ifdef CONFIG_VNC_TLS 13220057a0d5STim Hardeck } 13230057a0d5STim Hardeck #endif /* CONFIG_VNC_TLS */ 13243e230dd2SCorentin Chary VNC_DEBUG("Read wire %p %zd -> %ld\n", data, datalen, ret); 13253e230dd2SCorentin Chary return vnc_client_io_error(vs, ret, socket_error()); 13263e230dd2SCorentin Chary } 13273e230dd2SCorentin Chary 13283e230dd2SCorentin Chary 13293e230dd2SCorentin Chary /* 13303e230dd2SCorentin Chary * Called to read data from the client socket to the input buffer, 13313e230dd2SCorentin Chary * when not using any SASL SSF encryption layers. Will read as much 13323e230dd2SCorentin Chary * data as possible without blocking. 13333e230dd2SCorentin Chary * 13343e230dd2SCorentin Chary * Returns the number of bytes read. Returns -1 on error, and 13353e230dd2SCorentin Chary * disconnects the client socket. 13363e230dd2SCorentin Chary */ 13373e230dd2SCorentin Chary static long vnc_client_read_plain(VncState *vs) 13383e230dd2SCorentin Chary { 13393e230dd2SCorentin Chary int ret; 13403e230dd2SCorentin Chary VNC_DEBUG("Read plain %p size %zd offset %zd\n", 13413e230dd2SCorentin Chary vs->input.buffer, vs->input.capacity, vs->input.offset); 13423e230dd2SCorentin Chary buffer_reserve(&vs->input, 4096); 13433e230dd2SCorentin Chary ret = vnc_client_read_buf(vs, buffer_end(&vs->input), 4096); 13443e230dd2SCorentin Chary if (!ret) 13453e230dd2SCorentin Chary return 0; 13463e230dd2SCorentin Chary vs->input.offset += ret; 13473e230dd2SCorentin Chary return ret; 13483e230dd2SCorentin Chary } 13493e230dd2SCorentin Chary 1350175b2a6eSCorentin Chary static void vnc_jobs_bh(void *opaque) 1351175b2a6eSCorentin Chary { 1352175b2a6eSCorentin Chary VncState *vs = opaque; 1353175b2a6eSCorentin Chary 1354175b2a6eSCorentin Chary vnc_jobs_consume_buffer(vs); 1355175b2a6eSCorentin Chary } 13563e230dd2SCorentin Chary 13573e230dd2SCorentin Chary /* 13583e230dd2SCorentin Chary * First function called whenever there is more data to be read from 13593e230dd2SCorentin Chary * the client socket. Will delegate actual work according to whether 13603e230dd2SCorentin Chary * SASL SSF layers are enabled (thus requiring decryption calls) 13613e230dd2SCorentin Chary */ 13623e230dd2SCorentin Chary void vnc_client_read(void *opaque) 13633e230dd2SCorentin Chary { 13643e230dd2SCorentin Chary VncState *vs = opaque; 13653e230dd2SCorentin Chary long ret; 13663e230dd2SCorentin Chary 13673e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL 13683e230dd2SCorentin Chary if (vs->sasl.conn && vs->sasl.runSSF) 13693e230dd2SCorentin Chary ret = vnc_client_read_sasl(vs); 13703e230dd2SCorentin Chary else 13713e230dd2SCorentin Chary #endif /* CONFIG_VNC_SASL */ 13727536ee4bSTim Hardeck #ifdef CONFIG_VNC_WS 13737536ee4bSTim Hardeck if (vs->encode_ws) { 13747536ee4bSTim Hardeck ret = vnc_client_read_ws(vs); 13757536ee4bSTim Hardeck if (ret == -1) { 13767536ee4bSTim Hardeck vnc_disconnect_start(vs); 13777536ee4bSTim Hardeck return; 13787536ee4bSTim Hardeck } else if (ret == -2) { 13797536ee4bSTim Hardeck vnc_client_error(vs); 13807536ee4bSTim Hardeck return; 13817536ee4bSTim Hardeck } 13827536ee4bSTim Hardeck } else 13837536ee4bSTim Hardeck #endif /* CONFIG_VNC_WS */ 13847536ee4bSTim Hardeck { 13853e230dd2SCorentin Chary ret = vnc_client_read_plain(vs); 13867536ee4bSTim Hardeck } 13873e230dd2SCorentin Chary if (!ret) { 13883e230dd2SCorentin Chary if (vs->csock == -1) 13893e230dd2SCorentin Chary vnc_disconnect_finish(vs); 13903e230dd2SCorentin Chary return; 13913e230dd2SCorentin Chary } 13923e230dd2SCorentin Chary 13933e230dd2SCorentin Chary while (vs->read_handler && vs->input.offset >= vs->read_handler_expect) { 13943e230dd2SCorentin Chary size_t len = vs->read_handler_expect; 13953e230dd2SCorentin Chary int ret; 13963e230dd2SCorentin Chary 13973e230dd2SCorentin Chary ret = vs->read_handler(vs, vs->input.buffer, len); 13983e230dd2SCorentin Chary if (vs->csock == -1) { 13993e230dd2SCorentin Chary vnc_disconnect_finish(vs); 14003e230dd2SCorentin Chary return; 14013e230dd2SCorentin Chary } 14023e230dd2SCorentin Chary 14033e230dd2SCorentin Chary if (!ret) { 140432ed2680STim Hardeck buffer_advance(&vs->input, len); 14053e230dd2SCorentin Chary } else { 14063e230dd2SCorentin Chary vs->read_handler_expect = ret; 14073e230dd2SCorentin Chary } 14083e230dd2SCorentin Chary } 14093e230dd2SCorentin Chary } 14103e230dd2SCorentin Chary 14113e230dd2SCorentin Chary void vnc_write(VncState *vs, const void *data, size_t len) 14123e230dd2SCorentin Chary { 14133e230dd2SCorentin Chary buffer_reserve(&vs->output, len); 14143e230dd2SCorentin Chary 14153e230dd2SCorentin Chary if (vs->csock != -1 && buffer_empty(&vs->output)) { 14163e230dd2SCorentin Chary qemu_set_fd_handler2(vs->csock, NULL, vnc_client_read, vnc_client_write, vs); 14173e230dd2SCorentin Chary } 14183e230dd2SCorentin Chary 14193e230dd2SCorentin Chary buffer_append(&vs->output, data, len); 14203e230dd2SCorentin Chary } 14213e230dd2SCorentin Chary 14223e230dd2SCorentin Chary void vnc_write_s32(VncState *vs, int32_t value) 14233e230dd2SCorentin Chary { 14243e230dd2SCorentin Chary vnc_write_u32(vs, *(uint32_t *)&value); 14253e230dd2SCorentin Chary } 14263e230dd2SCorentin Chary 14273e230dd2SCorentin Chary void vnc_write_u32(VncState *vs, uint32_t value) 14283e230dd2SCorentin Chary { 14293e230dd2SCorentin Chary uint8_t buf[4]; 14303e230dd2SCorentin Chary 14313e230dd2SCorentin Chary buf[0] = (value >> 24) & 0xFF; 14323e230dd2SCorentin Chary buf[1] = (value >> 16) & 0xFF; 14333e230dd2SCorentin Chary buf[2] = (value >> 8) & 0xFF; 14343e230dd2SCorentin Chary buf[3] = value & 0xFF; 14353e230dd2SCorentin Chary 14363e230dd2SCorentin Chary vnc_write(vs, buf, 4); 14373e230dd2SCorentin Chary } 14383e230dd2SCorentin Chary 14393e230dd2SCorentin Chary void vnc_write_u16(VncState *vs, uint16_t value) 14403e230dd2SCorentin Chary { 14413e230dd2SCorentin Chary uint8_t buf[2]; 14423e230dd2SCorentin Chary 14433e230dd2SCorentin Chary buf[0] = (value >> 8) & 0xFF; 14443e230dd2SCorentin Chary buf[1] = value & 0xFF; 14453e230dd2SCorentin Chary 14463e230dd2SCorentin Chary vnc_write(vs, buf, 2); 14473e230dd2SCorentin Chary } 14483e230dd2SCorentin Chary 14493e230dd2SCorentin Chary void vnc_write_u8(VncState *vs, uint8_t value) 14503e230dd2SCorentin Chary { 14513e230dd2SCorentin Chary vnc_write(vs, (char *)&value, 1); 14523e230dd2SCorentin Chary } 14533e230dd2SCorentin Chary 14543e230dd2SCorentin Chary void vnc_flush(VncState *vs) 14553e230dd2SCorentin Chary { 1456bd023f95SCorentin Chary vnc_lock_output(vs); 14577536ee4bSTim Hardeck if (vs->csock != -1 && (vs->output.offset 14587536ee4bSTim Hardeck #ifdef CONFIG_VNC_WS 14597536ee4bSTim Hardeck || vs->ws_output.offset 14607536ee4bSTim Hardeck #endif 14617536ee4bSTim Hardeck )) { 1462bd023f95SCorentin Chary vnc_client_write_locked(vs); 1463bd023f95SCorentin Chary } 1464bd023f95SCorentin Chary vnc_unlock_output(vs); 14653e230dd2SCorentin Chary } 14663e230dd2SCorentin Chary 146771a8cdecSBlue Swirl static uint8_t read_u8(uint8_t *data, size_t offset) 14683e230dd2SCorentin Chary { 14693e230dd2SCorentin Chary return data[offset]; 14703e230dd2SCorentin Chary } 14713e230dd2SCorentin Chary 147271a8cdecSBlue Swirl static uint16_t read_u16(uint8_t *data, size_t offset) 14733e230dd2SCorentin Chary { 14743e230dd2SCorentin Chary return ((data[offset] & 0xFF) << 8) | (data[offset + 1] & 0xFF); 14753e230dd2SCorentin Chary } 14763e230dd2SCorentin Chary 147771a8cdecSBlue Swirl static int32_t read_s32(uint8_t *data, size_t offset) 14783e230dd2SCorentin Chary { 14793e230dd2SCorentin Chary return (int32_t)((data[offset] << 24) | (data[offset + 1] << 16) | 14803e230dd2SCorentin Chary (data[offset + 2] << 8) | data[offset + 3]); 14813e230dd2SCorentin Chary } 14823e230dd2SCorentin Chary 14833e230dd2SCorentin Chary uint32_t read_u32(uint8_t *data, size_t offset) 14843e230dd2SCorentin Chary { 14853e230dd2SCorentin Chary return ((data[offset] << 24) | (data[offset + 1] << 16) | 14863e230dd2SCorentin Chary (data[offset + 2] << 8) | data[offset + 3]); 14873e230dd2SCorentin Chary } 14883e230dd2SCorentin Chary 14893e230dd2SCorentin Chary static void client_cut_text(VncState *vs, size_t len, uint8_t *text) 14903e230dd2SCorentin Chary { 14913e230dd2SCorentin Chary } 14923e230dd2SCorentin Chary 14939e8dd451SJan Kiszka static void check_pointer_type_change(Notifier *notifier, void *data) 14943e230dd2SCorentin Chary { 14953e230dd2SCorentin Chary VncState *vs = container_of(notifier, VncState, mouse_mode_notifier); 149614768ebaSGerd Hoffmann int absolute = qemu_input_is_absolute(); 14973e230dd2SCorentin Chary 14983e230dd2SCorentin Chary if (vnc_has_feature(vs, VNC_FEATURE_POINTER_TYPE_CHANGE) && vs->absolute != absolute) { 1499bd023f95SCorentin Chary vnc_lock_output(vs); 15003e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE); 15013e230dd2SCorentin Chary vnc_write_u8(vs, 0); 15023e230dd2SCorentin Chary vnc_write_u16(vs, 1); 15033e230dd2SCorentin Chary vnc_framebuffer_update(vs, absolute, 0, 1504bea60dd7SPeter Lieven pixman_image_get_width(vs->vd->server), 1505bea60dd7SPeter Lieven pixman_image_get_height(vs->vd->server), 15063e230dd2SCorentin Chary VNC_ENCODING_POINTER_TYPE_CHANGE); 1507bd023f95SCorentin Chary vnc_unlock_output(vs); 15083e230dd2SCorentin Chary vnc_flush(vs); 15093e230dd2SCorentin Chary } 15103e230dd2SCorentin Chary vs->absolute = absolute; 15113e230dd2SCorentin Chary } 15123e230dd2SCorentin Chary 15133e230dd2SCorentin Chary static void pointer_event(VncState *vs, int button_mask, int x, int y) 15143e230dd2SCorentin Chary { 151514768ebaSGerd Hoffmann static uint32_t bmap[INPUT_BUTTON_MAX] = { 151614768ebaSGerd Hoffmann [INPUT_BUTTON_LEFT] = 0x01, 151714768ebaSGerd Hoffmann [INPUT_BUTTON_MIDDLE] = 0x02, 151814768ebaSGerd Hoffmann [INPUT_BUTTON_RIGHT] = 0x04, 151914768ebaSGerd Hoffmann [INPUT_BUTTON_WHEEL_UP] = 0x08, 152014768ebaSGerd Hoffmann [INPUT_BUTTON_WHEEL_DOWN] = 0x10, 152114768ebaSGerd Hoffmann }; 152214768ebaSGerd Hoffmann QemuConsole *con = vs->vd->dcl.con; 1523bea60dd7SPeter Lieven int width = pixman_image_get_width(vs->vd->server); 1524bea60dd7SPeter Lieven int height = pixman_image_get_height(vs->vd->server); 15253e230dd2SCorentin Chary 152614768ebaSGerd Hoffmann if (vs->last_bmask != button_mask) { 152714768ebaSGerd Hoffmann qemu_input_update_buttons(con, bmap, vs->last_bmask, button_mask); 152814768ebaSGerd Hoffmann vs->last_bmask = button_mask; 152914768ebaSGerd Hoffmann } 15303e230dd2SCorentin Chary 15313e230dd2SCorentin Chary if (vs->absolute) { 153214768ebaSGerd Hoffmann qemu_input_queue_abs(con, INPUT_AXIS_X, x, width); 153314768ebaSGerd Hoffmann qemu_input_queue_abs(con, INPUT_AXIS_Y, y, height); 15343e230dd2SCorentin Chary } else if (vnc_has_feature(vs, VNC_FEATURE_POINTER_TYPE_CHANGE)) { 153514768ebaSGerd Hoffmann qemu_input_queue_rel(con, INPUT_AXIS_X, x - 0x7FFF); 153614768ebaSGerd Hoffmann qemu_input_queue_rel(con, INPUT_AXIS_Y, y - 0x7FFF); 15373e230dd2SCorentin Chary } else { 153814768ebaSGerd Hoffmann if (vs->last_x != -1) { 153914768ebaSGerd Hoffmann qemu_input_queue_rel(con, INPUT_AXIS_X, x - vs->last_x); 154014768ebaSGerd Hoffmann qemu_input_queue_rel(con, INPUT_AXIS_Y, y - vs->last_y); 154114768ebaSGerd Hoffmann } 15423e230dd2SCorentin Chary vs->last_x = x; 15433e230dd2SCorentin Chary vs->last_y = y; 15443e230dd2SCorentin Chary } 154514768ebaSGerd Hoffmann qemu_input_event_sync(); 15463e230dd2SCorentin Chary } 15473e230dd2SCorentin Chary 15483e230dd2SCorentin Chary static void reset_keys(VncState *vs) 15493e230dd2SCorentin Chary { 15503e230dd2SCorentin Chary int i; 15513e230dd2SCorentin Chary for(i = 0; i < 256; i++) { 15523e230dd2SCorentin Chary if (vs->modifiers_state[i]) { 15538d447d10SGerd Hoffmann qemu_input_event_send_key_number(vs->vd->dcl.con, i, false); 15543e230dd2SCorentin Chary vs->modifiers_state[i] = 0; 15553e230dd2SCorentin Chary } 15563e230dd2SCorentin Chary } 15573e230dd2SCorentin Chary } 15583e230dd2SCorentin Chary 15593e230dd2SCorentin Chary static void press_key(VncState *vs, int keysym) 15603e230dd2SCorentin Chary { 15613e230dd2SCorentin Chary int keycode = keysym2scancode(vs->vd->kbd_layout, keysym) & SCANCODE_KEYMASK; 15628d447d10SGerd Hoffmann qemu_input_event_send_key_number(vs->vd->dcl.con, keycode, true); 15632deb4accSGerd Hoffmann qemu_input_event_send_key_delay(0); 15648d447d10SGerd Hoffmann qemu_input_event_send_key_number(vs->vd->dcl.con, keycode, false); 15652deb4accSGerd Hoffmann qemu_input_event_send_key_delay(0); 15663e230dd2SCorentin Chary } 15673e230dd2SCorentin Chary 1568ab99e5c1SLei Li static int current_led_state(VncState *vs) 1569ab99e5c1SLei Li { 1570ab99e5c1SLei Li int ledstate = 0; 1571ab99e5c1SLei Li 1572ab99e5c1SLei Li if (vs->modifiers_state[0x46]) { 1573ab99e5c1SLei Li ledstate |= QEMU_SCROLL_LOCK_LED; 1574ab99e5c1SLei Li } 1575ab99e5c1SLei Li if (vs->modifiers_state[0x45]) { 1576ab99e5c1SLei Li ledstate |= QEMU_NUM_LOCK_LED; 1577ab99e5c1SLei Li } 1578ab99e5c1SLei Li if (vs->modifiers_state[0x3a]) { 1579ab99e5c1SLei Li ledstate |= QEMU_CAPS_LOCK_LED; 1580ab99e5c1SLei Li } 1581ab99e5c1SLei Li 1582ab99e5c1SLei Li return ledstate; 1583ab99e5c1SLei Li } 1584ab99e5c1SLei Li 1585ab99e5c1SLei Li static void vnc_led_state_change(VncState *vs) 1586ab99e5c1SLei Li { 1587ab99e5c1SLei Li int ledstate = 0; 1588ab99e5c1SLei Li 1589ab99e5c1SLei Li if (!vnc_has_feature(vs, VNC_FEATURE_LED_STATE)) { 1590ab99e5c1SLei Li return; 1591ab99e5c1SLei Li } 1592ab99e5c1SLei Li 1593ab99e5c1SLei Li ledstate = current_led_state(vs); 1594ab99e5c1SLei Li vnc_lock_output(vs); 1595ab99e5c1SLei Li vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE); 1596ab99e5c1SLei Li vnc_write_u8(vs, 0); 1597ab99e5c1SLei Li vnc_write_u16(vs, 1); 1598ab99e5c1SLei Li vnc_framebuffer_update(vs, 0, 0, 1, 1, VNC_ENCODING_LED_STATE); 1599ab99e5c1SLei Li vnc_write_u8(vs, ledstate); 1600ab99e5c1SLei Li vnc_unlock_output(vs); 1601ab99e5c1SLei Li vnc_flush(vs); 1602ab99e5c1SLei Li } 1603ab99e5c1SLei Li 16043e230dd2SCorentin Chary static void kbd_leds(void *opaque, int ledstate) 16053e230dd2SCorentin Chary { 16063e230dd2SCorentin Chary VncState *vs = opaque; 160796f3d174SLei Li int caps, num, scr; 16081483adcfSLei Li bool has_changed = (ledstate != current_led_state(vs)); 16093e230dd2SCorentin Chary 161040066175SGerd Hoffmann trace_vnc_key_guest_leds((ledstate & QEMU_CAPS_LOCK_LED), 161140066175SGerd Hoffmann (ledstate & QEMU_NUM_LOCK_LED), 161240066175SGerd Hoffmann (ledstate & QEMU_SCROLL_LOCK_LED)); 161340066175SGerd Hoffmann 16143e230dd2SCorentin Chary caps = ledstate & QEMU_CAPS_LOCK_LED ? 1 : 0; 16153e230dd2SCorentin Chary num = ledstate & QEMU_NUM_LOCK_LED ? 1 : 0; 161696f3d174SLei Li scr = ledstate & QEMU_SCROLL_LOCK_LED ? 1 : 0; 16173e230dd2SCorentin Chary 16183e230dd2SCorentin Chary if (vs->modifiers_state[0x3a] != caps) { 16193e230dd2SCorentin Chary vs->modifiers_state[0x3a] = caps; 16203e230dd2SCorentin Chary } 16213e230dd2SCorentin Chary if (vs->modifiers_state[0x45] != num) { 16223e230dd2SCorentin Chary vs->modifiers_state[0x45] = num; 16233e230dd2SCorentin Chary } 162496f3d174SLei Li if (vs->modifiers_state[0x46] != scr) { 162596f3d174SLei Li vs->modifiers_state[0x46] = scr; 162696f3d174SLei Li } 1627ab99e5c1SLei Li 1628ab99e5c1SLei Li /* Sending the current led state message to the client */ 16291483adcfSLei Li if (has_changed) { 1630ab99e5c1SLei Li vnc_led_state_change(vs); 1631ab99e5c1SLei Li } 16323e230dd2SCorentin Chary } 16333e230dd2SCorentin Chary 16343e230dd2SCorentin Chary static void do_key_event(VncState *vs, int down, int keycode, int sym) 16353e230dd2SCorentin Chary { 16363e230dd2SCorentin Chary /* QEMU console switch */ 16373e230dd2SCorentin Chary switch(keycode) { 16383e230dd2SCorentin Chary case 0x2a: /* Left Shift */ 16393e230dd2SCorentin Chary case 0x36: /* Right Shift */ 16403e230dd2SCorentin Chary case 0x1d: /* Left CTRL */ 16413e230dd2SCorentin Chary case 0x9d: /* Right CTRL */ 16423e230dd2SCorentin Chary case 0x38: /* Left ALT */ 16433e230dd2SCorentin Chary case 0xb8: /* Right ALT */ 16443e230dd2SCorentin Chary if (down) 16453e230dd2SCorentin Chary vs->modifiers_state[keycode] = 1; 16463e230dd2SCorentin Chary else 16473e230dd2SCorentin Chary vs->modifiers_state[keycode] = 0; 16483e230dd2SCorentin Chary break; 16493e230dd2SCorentin Chary case 0x02 ... 0x0a: /* '1' to '9' keys */ 16503e230dd2SCorentin Chary if (down && vs->modifiers_state[0x1d] && vs->modifiers_state[0x38]) { 16513e230dd2SCorentin Chary /* Reset the modifiers sent to the current console */ 16523e230dd2SCorentin Chary reset_keys(vs); 16533e230dd2SCorentin Chary console_select(keycode - 0x02); 16543e230dd2SCorentin Chary return; 16553e230dd2SCorentin Chary } 16563e230dd2SCorentin Chary break; 16573e230dd2SCorentin Chary case 0x3a: /* CapsLock */ 16583e230dd2SCorentin Chary case 0x45: /* NumLock */ 16593e230dd2SCorentin Chary if (down) 16603e230dd2SCorentin Chary vs->modifiers_state[keycode] ^= 1; 16613e230dd2SCorentin Chary break; 16623e230dd2SCorentin Chary } 16633e230dd2SCorentin Chary 1664e7b2aaccSLei Li /* Turn off the lock state sync logic if the client support the led 1665e7b2aaccSLei Li state extension. 1666e7b2aaccSLei Li */ 16679892088bSGerd Hoffmann if (down && vs->vd->lock_key_sync && 1668e7b2aaccSLei Li !vnc_has_feature(vs, VNC_FEATURE_LED_STATE) && 16693e230dd2SCorentin Chary keycode_is_keypad(vs->vd->kbd_layout, keycode)) { 16703e230dd2SCorentin Chary /* If the numlock state needs to change then simulate an additional 16713e230dd2SCorentin Chary keypress before sending this one. This will happen if the user 16723e230dd2SCorentin Chary toggles numlock away from the VNC window. 16733e230dd2SCorentin Chary */ 16743e230dd2SCorentin Chary if (keysym_is_numlock(vs->vd->kbd_layout, sym & 0xFFFF)) { 16753e230dd2SCorentin Chary if (!vs->modifiers_state[0x45]) { 167640066175SGerd Hoffmann trace_vnc_key_sync_numlock(true); 16773e230dd2SCorentin Chary vs->modifiers_state[0x45] = 1; 16783e230dd2SCorentin Chary press_key(vs, 0xff7f); 16793e230dd2SCorentin Chary } 16803e230dd2SCorentin Chary } else { 16813e230dd2SCorentin Chary if (vs->modifiers_state[0x45]) { 168240066175SGerd Hoffmann trace_vnc_key_sync_numlock(false); 16833e230dd2SCorentin Chary vs->modifiers_state[0x45] = 0; 16843e230dd2SCorentin Chary press_key(vs, 0xff7f); 16853e230dd2SCorentin Chary } 16863e230dd2SCorentin Chary } 16873e230dd2SCorentin Chary } 16883e230dd2SCorentin Chary 16899892088bSGerd Hoffmann if (down && vs->vd->lock_key_sync && 1690e7b2aaccSLei Li !vnc_has_feature(vs, VNC_FEATURE_LED_STATE) && 16913e230dd2SCorentin Chary ((sym >= 'A' && sym <= 'Z') || (sym >= 'a' && sym <= 'z'))) { 16923e230dd2SCorentin Chary /* If the capslock state needs to change then simulate an additional 16933e230dd2SCorentin Chary keypress before sending this one. This will happen if the user 16943e230dd2SCorentin Chary toggles capslock away from the VNC window. 16953e230dd2SCorentin Chary */ 16963e230dd2SCorentin Chary int uppercase = !!(sym >= 'A' && sym <= 'Z'); 16973e230dd2SCorentin Chary int shift = !!(vs->modifiers_state[0x2a] | vs->modifiers_state[0x36]); 16983e230dd2SCorentin Chary int capslock = !!(vs->modifiers_state[0x3a]); 16993e230dd2SCorentin Chary if (capslock) { 17003e230dd2SCorentin Chary if (uppercase == shift) { 170140066175SGerd Hoffmann trace_vnc_key_sync_capslock(false); 17023e230dd2SCorentin Chary vs->modifiers_state[0x3a] = 0; 17033e230dd2SCorentin Chary press_key(vs, 0xffe5); 17043e230dd2SCorentin Chary } 17053e230dd2SCorentin Chary } else { 17063e230dd2SCorentin Chary if (uppercase != shift) { 170740066175SGerd Hoffmann trace_vnc_key_sync_capslock(true); 17083e230dd2SCorentin Chary vs->modifiers_state[0x3a] = 1; 17093e230dd2SCorentin Chary press_key(vs, 0xffe5); 17103e230dd2SCorentin Chary } 17113e230dd2SCorentin Chary } 17123e230dd2SCorentin Chary } 17133e230dd2SCorentin Chary 171481c0d5a6SGerd Hoffmann if (qemu_console_is_graphic(NULL)) { 17158d447d10SGerd Hoffmann qemu_input_event_send_key_number(vs->vd->dcl.con, keycode, down); 17163e230dd2SCorentin Chary } else { 1717e26437c2SGerd Hoffmann bool numlock = vs->modifiers_state[0x45]; 1718e26437c2SGerd Hoffmann bool control = (vs->modifiers_state[0x1d] || 1719e26437c2SGerd Hoffmann vs->modifiers_state[0x9d]); 17203e230dd2SCorentin Chary /* QEMU console emulation */ 17213e230dd2SCorentin Chary if (down) { 17223e230dd2SCorentin Chary switch (keycode) { 17233e230dd2SCorentin Chary case 0x2a: /* Left Shift */ 17243e230dd2SCorentin Chary case 0x36: /* Right Shift */ 17253e230dd2SCorentin Chary case 0x1d: /* Left CTRL */ 17263e230dd2SCorentin Chary case 0x9d: /* Right CTRL */ 17273e230dd2SCorentin Chary case 0x38: /* Left ALT */ 17283e230dd2SCorentin Chary case 0xb8: /* Right ALT */ 17293e230dd2SCorentin Chary break; 17303e230dd2SCorentin Chary case 0xc8: 17313e230dd2SCorentin Chary kbd_put_keysym(QEMU_KEY_UP); 17323e230dd2SCorentin Chary break; 17333e230dd2SCorentin Chary case 0xd0: 17343e230dd2SCorentin Chary kbd_put_keysym(QEMU_KEY_DOWN); 17353e230dd2SCorentin Chary break; 17363e230dd2SCorentin Chary case 0xcb: 17373e230dd2SCorentin Chary kbd_put_keysym(QEMU_KEY_LEFT); 17383e230dd2SCorentin Chary break; 17393e230dd2SCorentin Chary case 0xcd: 17403e230dd2SCorentin Chary kbd_put_keysym(QEMU_KEY_RIGHT); 17413e230dd2SCorentin Chary break; 17423e230dd2SCorentin Chary case 0xd3: 17433e230dd2SCorentin Chary kbd_put_keysym(QEMU_KEY_DELETE); 17443e230dd2SCorentin Chary break; 17453e230dd2SCorentin Chary case 0xc7: 17463e230dd2SCorentin Chary kbd_put_keysym(QEMU_KEY_HOME); 17473e230dd2SCorentin Chary break; 17483e230dd2SCorentin Chary case 0xcf: 17493e230dd2SCorentin Chary kbd_put_keysym(QEMU_KEY_END); 17503e230dd2SCorentin Chary break; 17513e230dd2SCorentin Chary case 0xc9: 17523e230dd2SCorentin Chary kbd_put_keysym(QEMU_KEY_PAGEUP); 17533e230dd2SCorentin Chary break; 17543e230dd2SCorentin Chary case 0xd1: 17553e230dd2SCorentin Chary kbd_put_keysym(QEMU_KEY_PAGEDOWN); 17563e230dd2SCorentin Chary break; 17573e230dd2SCorentin Chary 17583e230dd2SCorentin Chary case 0x47: 17593e230dd2SCorentin Chary kbd_put_keysym(numlock ? '7' : QEMU_KEY_HOME); 17603e230dd2SCorentin Chary break; 17613e230dd2SCorentin Chary case 0x48: 17623e230dd2SCorentin Chary kbd_put_keysym(numlock ? '8' : QEMU_KEY_UP); 17633e230dd2SCorentin Chary break; 17643e230dd2SCorentin Chary case 0x49: 17653e230dd2SCorentin Chary kbd_put_keysym(numlock ? '9' : QEMU_KEY_PAGEUP); 17663e230dd2SCorentin Chary break; 17673e230dd2SCorentin Chary case 0x4b: 17683e230dd2SCorentin Chary kbd_put_keysym(numlock ? '4' : QEMU_KEY_LEFT); 17693e230dd2SCorentin Chary break; 17703e230dd2SCorentin Chary case 0x4c: 17713e230dd2SCorentin Chary kbd_put_keysym('5'); 17723e230dd2SCorentin Chary break; 17733e230dd2SCorentin Chary case 0x4d: 17743e230dd2SCorentin Chary kbd_put_keysym(numlock ? '6' : QEMU_KEY_RIGHT); 17753e230dd2SCorentin Chary break; 17763e230dd2SCorentin Chary case 0x4f: 17773e230dd2SCorentin Chary kbd_put_keysym(numlock ? '1' : QEMU_KEY_END); 17783e230dd2SCorentin Chary break; 17793e230dd2SCorentin Chary case 0x50: 17803e230dd2SCorentin Chary kbd_put_keysym(numlock ? '2' : QEMU_KEY_DOWN); 17813e230dd2SCorentin Chary break; 17823e230dd2SCorentin Chary case 0x51: 17833e230dd2SCorentin Chary kbd_put_keysym(numlock ? '3' : QEMU_KEY_PAGEDOWN); 17843e230dd2SCorentin Chary break; 17853e230dd2SCorentin Chary case 0x52: 17863e230dd2SCorentin Chary kbd_put_keysym('0'); 17873e230dd2SCorentin Chary break; 17883e230dd2SCorentin Chary case 0x53: 17893e230dd2SCorentin Chary kbd_put_keysym(numlock ? '.' : QEMU_KEY_DELETE); 17903e230dd2SCorentin Chary break; 17913e230dd2SCorentin Chary 17923e230dd2SCorentin Chary case 0xb5: 17933e230dd2SCorentin Chary kbd_put_keysym('/'); 17943e230dd2SCorentin Chary break; 17953e230dd2SCorentin Chary case 0x37: 17963e230dd2SCorentin Chary kbd_put_keysym('*'); 17973e230dd2SCorentin Chary break; 17983e230dd2SCorentin Chary case 0x4a: 17993e230dd2SCorentin Chary kbd_put_keysym('-'); 18003e230dd2SCorentin Chary break; 18013e230dd2SCorentin Chary case 0x4e: 18023e230dd2SCorentin Chary kbd_put_keysym('+'); 18033e230dd2SCorentin Chary break; 18043e230dd2SCorentin Chary case 0x9c: 18053e230dd2SCorentin Chary kbd_put_keysym('\n'); 18063e230dd2SCorentin Chary break; 18073e230dd2SCorentin Chary 18083e230dd2SCorentin Chary default: 1809e26437c2SGerd Hoffmann if (control) { 1810e26437c2SGerd Hoffmann kbd_put_keysym(sym & 0x1f); 1811e26437c2SGerd Hoffmann } else { 18123e230dd2SCorentin Chary kbd_put_keysym(sym); 1813e26437c2SGerd Hoffmann } 18143e230dd2SCorentin Chary break; 18153e230dd2SCorentin Chary } 18163e230dd2SCorentin Chary } 18173e230dd2SCorentin Chary } 18183e230dd2SCorentin Chary } 18193e230dd2SCorentin Chary 18207bc9318bSGerd Hoffmann static void vnc_release_modifiers(VncState *vs) 18217bc9318bSGerd Hoffmann { 18227bc9318bSGerd Hoffmann static const int keycodes[] = { 18237bc9318bSGerd Hoffmann /* shift, control, alt keys, both left & right */ 18247bc9318bSGerd Hoffmann 0x2a, 0x36, 0x1d, 0x9d, 0x38, 0xb8, 18257bc9318bSGerd Hoffmann }; 18267bc9318bSGerd Hoffmann int i, keycode; 18277bc9318bSGerd Hoffmann 182881c0d5a6SGerd Hoffmann if (!qemu_console_is_graphic(NULL)) { 18297bc9318bSGerd Hoffmann return; 18307bc9318bSGerd Hoffmann } 18317bc9318bSGerd Hoffmann for (i = 0; i < ARRAY_SIZE(keycodes); i++) { 18327bc9318bSGerd Hoffmann keycode = keycodes[i]; 18337bc9318bSGerd Hoffmann if (!vs->modifiers_state[keycode]) { 18347bc9318bSGerd Hoffmann continue; 18357bc9318bSGerd Hoffmann } 18368d447d10SGerd Hoffmann qemu_input_event_send_key_number(vs->vd->dcl.con, keycode, false); 18377bc9318bSGerd Hoffmann } 18387bc9318bSGerd Hoffmann } 18397bc9318bSGerd Hoffmann 184040066175SGerd Hoffmann static const char *code2name(int keycode) 184140066175SGerd Hoffmann { 184240066175SGerd Hoffmann return QKeyCode_lookup[qemu_input_key_number_to_qcode(keycode)]; 184340066175SGerd Hoffmann } 184440066175SGerd Hoffmann 18453e230dd2SCorentin Chary static void key_event(VncState *vs, int down, uint32_t sym) 18463e230dd2SCorentin Chary { 18473e230dd2SCorentin Chary int keycode; 18483e230dd2SCorentin Chary int lsym = sym; 18493e230dd2SCorentin Chary 185081c0d5a6SGerd Hoffmann if (lsym >= 'A' && lsym <= 'Z' && qemu_console_is_graphic(NULL)) { 18513e230dd2SCorentin Chary lsym = lsym - 'A' + 'a'; 18523e230dd2SCorentin Chary } 18533e230dd2SCorentin Chary 18543e230dd2SCorentin Chary keycode = keysym2scancode(vs->vd->kbd_layout, lsym & 0xFFFF) & SCANCODE_KEYMASK; 185540066175SGerd Hoffmann trace_vnc_key_event_map(down, sym, keycode, code2name(keycode)); 18563e230dd2SCorentin Chary do_key_event(vs, down, keycode, sym); 18573e230dd2SCorentin Chary } 18583e230dd2SCorentin Chary 18593e230dd2SCorentin Chary static void ext_key_event(VncState *vs, int down, 18603e230dd2SCorentin Chary uint32_t sym, uint16_t keycode) 18613e230dd2SCorentin Chary { 18623e230dd2SCorentin Chary /* if the user specifies a keyboard layout, always use it */ 186340066175SGerd Hoffmann if (keyboard_layout) { 18643e230dd2SCorentin Chary key_event(vs, down, sym); 186540066175SGerd Hoffmann } else { 186640066175SGerd Hoffmann trace_vnc_key_event_ext(down, sym, keycode, code2name(keycode)); 18673e230dd2SCorentin Chary do_key_event(vs, down, keycode, sym); 18683e230dd2SCorentin Chary } 186940066175SGerd Hoffmann } 18703e230dd2SCorentin Chary 18713e230dd2SCorentin Chary static void framebuffer_update_request(VncState *vs, int incremental, 1872bea60dd7SPeter Lieven int x, int y, int w, int h) 18733e230dd2SCorentin Chary { 1874bea60dd7SPeter Lieven int width = pixman_image_get_width(vs->vd->server); 1875bea60dd7SPeter Lieven int height = pixman_image_get_height(vs->vd->server); 18763e230dd2SCorentin Chary 18773e230dd2SCorentin Chary vs->need_update = 1; 1878bea60dd7SPeter Lieven 1879bea60dd7SPeter Lieven if (incremental) { 1880bea60dd7SPeter Lieven return; 18813e230dd2SCorentin Chary } 1882bea60dd7SPeter Lieven 188307535a89SStephan Kulow vs->force_update = 1; 1884bea60dd7SPeter Lieven vnc_set_area_dirty(vs->dirty, width, height, x, y, w, h); 18853e230dd2SCorentin Chary } 18863e230dd2SCorentin Chary 18873e230dd2SCorentin Chary static void send_ext_key_event_ack(VncState *vs) 18883e230dd2SCorentin Chary { 1889bd023f95SCorentin Chary vnc_lock_output(vs); 18903e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE); 18913e230dd2SCorentin Chary vnc_write_u8(vs, 0); 18923e230dd2SCorentin Chary vnc_write_u16(vs, 1); 1893d39fa6d8SGerd Hoffmann vnc_framebuffer_update(vs, 0, 0, 1894bea60dd7SPeter Lieven pixman_image_get_width(vs->vd->server), 1895bea60dd7SPeter Lieven pixman_image_get_height(vs->vd->server), 18963e230dd2SCorentin Chary VNC_ENCODING_EXT_KEY_EVENT); 1897bd023f95SCorentin Chary vnc_unlock_output(vs); 18983e230dd2SCorentin Chary vnc_flush(vs); 18993e230dd2SCorentin Chary } 19003e230dd2SCorentin Chary 19013e230dd2SCorentin Chary static void send_ext_audio_ack(VncState *vs) 19023e230dd2SCorentin Chary { 1903bd023f95SCorentin Chary vnc_lock_output(vs); 19043e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE); 19053e230dd2SCorentin Chary vnc_write_u8(vs, 0); 19063e230dd2SCorentin Chary vnc_write_u16(vs, 1); 1907d39fa6d8SGerd Hoffmann vnc_framebuffer_update(vs, 0, 0, 1908bea60dd7SPeter Lieven pixman_image_get_width(vs->vd->server), 1909bea60dd7SPeter Lieven pixman_image_get_height(vs->vd->server), 19103e230dd2SCorentin Chary VNC_ENCODING_AUDIO); 1911bd023f95SCorentin Chary vnc_unlock_output(vs); 19123e230dd2SCorentin Chary vnc_flush(vs); 19133e230dd2SCorentin Chary } 19143e230dd2SCorentin Chary 19153e230dd2SCorentin Chary static void set_encodings(VncState *vs, int32_t *encodings, size_t n_encodings) 19163e230dd2SCorentin Chary { 19173e230dd2SCorentin Chary int i; 19183e230dd2SCorentin Chary unsigned int enc = 0; 19193e230dd2SCorentin Chary 19203e230dd2SCorentin Chary vs->features = 0; 19213e230dd2SCorentin Chary vs->vnc_encoding = 0; 1922d1af0e05SCorentin Chary vs->tight.compression = 9; 1923d1af0e05SCorentin Chary vs->tight.quality = -1; /* Lossless by default */ 19243e230dd2SCorentin Chary vs->absolute = -1; 19253e230dd2SCorentin Chary 19263e230dd2SCorentin Chary /* 19273e230dd2SCorentin Chary * Start from the end because the encodings are sent in order of preference. 1928e5bed759SDong Xu Wang * This way the preferred encoding (first encoding defined in the array) 19293e230dd2SCorentin Chary * will be set at the end of the loop. 19303e230dd2SCorentin Chary */ 19313e230dd2SCorentin Chary for (i = n_encodings - 1; i >= 0; i--) { 19323e230dd2SCorentin Chary enc = encodings[i]; 19333e230dd2SCorentin Chary switch (enc) { 19343e230dd2SCorentin Chary case VNC_ENCODING_RAW: 19353e230dd2SCorentin Chary vs->vnc_encoding = enc; 19363e230dd2SCorentin Chary break; 19373e230dd2SCorentin Chary case VNC_ENCODING_COPYRECT: 19383e230dd2SCorentin Chary vs->features |= VNC_FEATURE_COPYRECT_MASK; 19393e230dd2SCorentin Chary break; 19403e230dd2SCorentin Chary case VNC_ENCODING_HEXTILE: 19413e230dd2SCorentin Chary vs->features |= VNC_FEATURE_HEXTILE_MASK; 19423e230dd2SCorentin Chary vs->vnc_encoding = enc; 19433e230dd2SCorentin Chary break; 19443e230dd2SCorentin Chary case VNC_ENCODING_TIGHT: 19453e230dd2SCorentin Chary vs->features |= VNC_FEATURE_TIGHT_MASK; 19463e230dd2SCorentin Chary vs->vnc_encoding = enc; 19473e230dd2SCorentin Chary break; 1948fe3e7f2dSJoel Martin #ifdef CONFIG_VNC_PNG 1949efe556adSCorentin Chary case VNC_ENCODING_TIGHT_PNG: 1950efe556adSCorentin Chary vs->features |= VNC_FEATURE_TIGHT_PNG_MASK; 1951efe556adSCorentin Chary vs->vnc_encoding = enc; 1952efe556adSCorentin Chary break; 1953fe3e7f2dSJoel Martin #endif 19543e230dd2SCorentin Chary case VNC_ENCODING_ZLIB: 19553e230dd2SCorentin Chary vs->features |= VNC_FEATURE_ZLIB_MASK; 19563e230dd2SCorentin Chary vs->vnc_encoding = enc; 19573e230dd2SCorentin Chary break; 1958148954faSCorentin Chary case VNC_ENCODING_ZRLE: 1959148954faSCorentin Chary vs->features |= VNC_FEATURE_ZRLE_MASK; 1960148954faSCorentin Chary vs->vnc_encoding = enc; 1961148954faSCorentin Chary break; 1962148954faSCorentin Chary case VNC_ENCODING_ZYWRLE: 1963148954faSCorentin Chary vs->features |= VNC_FEATURE_ZYWRLE_MASK; 1964148954faSCorentin Chary vs->vnc_encoding = enc; 1965148954faSCorentin Chary break; 19663e230dd2SCorentin Chary case VNC_ENCODING_DESKTOPRESIZE: 19673e230dd2SCorentin Chary vs->features |= VNC_FEATURE_RESIZE_MASK; 19683e230dd2SCorentin Chary break; 19693e230dd2SCorentin Chary case VNC_ENCODING_POINTER_TYPE_CHANGE: 19703e230dd2SCorentin Chary vs->features |= VNC_FEATURE_POINTER_TYPE_CHANGE_MASK; 19713e230dd2SCorentin Chary break; 19723e230dd2SCorentin Chary case VNC_ENCODING_RICH_CURSOR: 19733e230dd2SCorentin Chary vs->features |= VNC_FEATURE_RICH_CURSOR_MASK; 19743e230dd2SCorentin Chary break; 19753e230dd2SCorentin Chary case VNC_ENCODING_EXT_KEY_EVENT: 19763e230dd2SCorentin Chary send_ext_key_event_ack(vs); 19773e230dd2SCorentin Chary break; 19783e230dd2SCorentin Chary case VNC_ENCODING_AUDIO: 19793e230dd2SCorentin Chary send_ext_audio_ack(vs); 19803e230dd2SCorentin Chary break; 19813e230dd2SCorentin Chary case VNC_ENCODING_WMVi: 19823e230dd2SCorentin Chary vs->features |= VNC_FEATURE_WMVI_MASK; 19833e230dd2SCorentin Chary break; 1984ab99e5c1SLei Li case VNC_ENCODING_LED_STATE: 1985ab99e5c1SLei Li vs->features |= VNC_FEATURE_LED_STATE_MASK; 1986ab99e5c1SLei Li break; 19873e230dd2SCorentin Chary case VNC_ENCODING_COMPRESSLEVEL0 ... VNC_ENCODING_COMPRESSLEVEL0 + 9: 1988d1af0e05SCorentin Chary vs->tight.compression = (enc & 0x0F); 19893e230dd2SCorentin Chary break; 19903e230dd2SCorentin Chary case VNC_ENCODING_QUALITYLEVEL0 ... VNC_ENCODING_QUALITYLEVEL0 + 9: 1991b31f519eSCorentin Chary if (vs->vd->lossy) { 1992d1af0e05SCorentin Chary vs->tight.quality = (enc & 0x0F); 1993b31f519eSCorentin Chary } 19943e230dd2SCorentin Chary break; 19953e230dd2SCorentin Chary default: 19963e230dd2SCorentin Chary VNC_DEBUG("Unknown encoding: %d (0x%.8x): %d\n", i, enc, enc); 19973e230dd2SCorentin Chary break; 19983e230dd2SCorentin Chary } 19993e230dd2SCorentin Chary } 20003e230dd2SCorentin Chary vnc_desktop_resize(vs); 20019e8dd451SJan Kiszka check_pointer_type_change(&vs->mouse_mode_notifier, NULL); 2002ab99e5c1SLei Li vnc_led_state_change(vs); 20033e230dd2SCorentin Chary } 20043e230dd2SCorentin Chary 20053e230dd2SCorentin Chary static void set_pixel_conversion(VncState *vs) 20063e230dd2SCorentin Chary { 20079f64916dSGerd Hoffmann pixman_format_code_t fmt = qemu_pixman_get_format(&vs->client_pf); 20089f64916dSGerd Hoffmann 20099f64916dSGerd Hoffmann if (fmt == VNC_SERVER_FB_FORMAT) { 20103e230dd2SCorentin Chary vs->write_pixels = vnc_write_pixels_copy; 20113e230dd2SCorentin Chary vnc_hextile_set_pixel_conversion(vs, 0); 20123e230dd2SCorentin Chary } else { 20133e230dd2SCorentin Chary vs->write_pixels = vnc_write_pixels_generic; 20143e230dd2SCorentin Chary vnc_hextile_set_pixel_conversion(vs, 1); 20153e230dd2SCorentin Chary } 20163e230dd2SCorentin Chary } 20173e230dd2SCorentin Chary 20183e230dd2SCorentin Chary static void set_pixel_format(VncState *vs, 20193e230dd2SCorentin Chary int bits_per_pixel, int depth, 20203e230dd2SCorentin Chary int big_endian_flag, int true_color_flag, 20213e230dd2SCorentin Chary int red_max, int green_max, int blue_max, 20223e230dd2SCorentin Chary int red_shift, int green_shift, int blue_shift) 20233e230dd2SCorentin Chary { 20243e230dd2SCorentin Chary if (!true_color_flag) { 20253e230dd2SCorentin Chary vnc_client_error(vs); 20263e230dd2SCorentin Chary return; 20273e230dd2SCorentin Chary } 20283e230dd2SCorentin Chary 20299f64916dSGerd Hoffmann vs->client_pf.rmax = red_max; 20309f64916dSGerd Hoffmann vs->client_pf.rbits = hweight_long(red_max); 20319f64916dSGerd Hoffmann vs->client_pf.rshift = red_shift; 20329f64916dSGerd Hoffmann vs->client_pf.rmask = red_max << red_shift; 20339f64916dSGerd Hoffmann vs->client_pf.gmax = green_max; 20349f64916dSGerd Hoffmann vs->client_pf.gbits = hweight_long(green_max); 20359f64916dSGerd Hoffmann vs->client_pf.gshift = green_shift; 20369f64916dSGerd Hoffmann vs->client_pf.gmask = green_max << green_shift; 20379f64916dSGerd Hoffmann vs->client_pf.bmax = blue_max; 20389f64916dSGerd Hoffmann vs->client_pf.bbits = hweight_long(blue_max); 20399f64916dSGerd Hoffmann vs->client_pf.bshift = blue_shift; 20409f64916dSGerd Hoffmann vs->client_pf.bmask = blue_max << blue_shift; 20419f64916dSGerd Hoffmann vs->client_pf.bits_per_pixel = bits_per_pixel; 20429f64916dSGerd Hoffmann vs->client_pf.bytes_per_pixel = bits_per_pixel / 8; 20439f64916dSGerd Hoffmann vs->client_pf.depth = bits_per_pixel == 32 ? 24 : bits_per_pixel; 20449f64916dSGerd Hoffmann vs->client_be = big_endian_flag; 20453e230dd2SCorentin Chary 20463e230dd2SCorentin Chary set_pixel_conversion(vs); 20473e230dd2SCorentin Chary 20481dbfa005SGerd Hoffmann graphic_hw_invalidate(NULL); 20491dbfa005SGerd Hoffmann graphic_hw_update(NULL); 20503e230dd2SCorentin Chary } 20513e230dd2SCorentin Chary 20523e230dd2SCorentin Chary static void pixel_format_message (VncState *vs) { 20533e230dd2SCorentin Chary char pad[3] = { 0, 0, 0 }; 20543e230dd2SCorentin Chary 20559f64916dSGerd Hoffmann vs->client_pf = qemu_default_pixelformat(32); 20569f64916dSGerd Hoffmann 20579f64916dSGerd Hoffmann vnc_write_u8(vs, vs->client_pf.bits_per_pixel); /* bits-per-pixel */ 20589f64916dSGerd Hoffmann vnc_write_u8(vs, vs->client_pf.depth); /* depth */ 20593e230dd2SCorentin Chary 20603e230dd2SCorentin Chary #ifdef HOST_WORDS_BIGENDIAN 20613e230dd2SCorentin Chary vnc_write_u8(vs, 1); /* big-endian-flag */ 20623e230dd2SCorentin Chary #else 20633e230dd2SCorentin Chary vnc_write_u8(vs, 0); /* big-endian-flag */ 20643e230dd2SCorentin Chary #endif 20653e230dd2SCorentin Chary vnc_write_u8(vs, 1); /* true-color-flag */ 20669f64916dSGerd Hoffmann vnc_write_u16(vs, vs->client_pf.rmax); /* red-max */ 20679f64916dSGerd Hoffmann vnc_write_u16(vs, vs->client_pf.gmax); /* green-max */ 20689f64916dSGerd Hoffmann vnc_write_u16(vs, vs->client_pf.bmax); /* blue-max */ 20699f64916dSGerd Hoffmann vnc_write_u8(vs, vs->client_pf.rshift); /* red-shift */ 20709f64916dSGerd Hoffmann vnc_write_u8(vs, vs->client_pf.gshift); /* green-shift */ 20719f64916dSGerd Hoffmann vnc_write_u8(vs, vs->client_pf.bshift); /* blue-shift */ 20729f64916dSGerd Hoffmann vnc_write(vs, pad, 3); /* padding */ 20733e230dd2SCorentin Chary 20743e230dd2SCorentin Chary vnc_hextile_set_pixel_conversion(vs, 0); 20753e230dd2SCorentin Chary vs->write_pixels = vnc_write_pixels_copy; 20763e230dd2SCorentin Chary } 20773e230dd2SCorentin Chary 20783e230dd2SCorentin Chary static void vnc_colordepth(VncState *vs) 20793e230dd2SCorentin Chary { 20803e230dd2SCorentin Chary if (vnc_has_feature(vs, VNC_FEATURE_WMVI)) { 20813e230dd2SCorentin Chary /* Sending a WMVi message to notify the client*/ 2082bd023f95SCorentin Chary vnc_lock_output(vs); 20833e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE); 20843e230dd2SCorentin Chary vnc_write_u8(vs, 0); 20853e230dd2SCorentin Chary vnc_write_u16(vs, 1); /* number of rects */ 2086d39fa6d8SGerd Hoffmann vnc_framebuffer_update(vs, 0, 0, 2087bea60dd7SPeter Lieven pixman_image_get_width(vs->vd->server), 2088bea60dd7SPeter Lieven pixman_image_get_height(vs->vd->server), 2089d39fa6d8SGerd Hoffmann VNC_ENCODING_WMVi); 20903e230dd2SCorentin Chary pixel_format_message(vs); 2091bd023f95SCorentin Chary vnc_unlock_output(vs); 20923e230dd2SCorentin Chary vnc_flush(vs); 20933e230dd2SCorentin Chary } else { 20943e230dd2SCorentin Chary set_pixel_conversion(vs); 20953e230dd2SCorentin Chary } 20963e230dd2SCorentin Chary } 20973e230dd2SCorentin Chary 20983e230dd2SCorentin Chary static int protocol_client_msg(VncState *vs, uint8_t *data, size_t len) 20993e230dd2SCorentin Chary { 21003e230dd2SCorentin Chary int i; 21013e230dd2SCorentin Chary uint16_t limit; 21023e230dd2SCorentin Chary VncDisplay *vd = vs->vd; 21033e230dd2SCorentin Chary 21043e230dd2SCorentin Chary if (data[0] > 3) { 21050f7b2864SGerd Hoffmann update_displaychangelistener(&vd->dcl, VNC_REFRESH_INTERVAL_BASE); 21063e230dd2SCorentin Chary } 21073e230dd2SCorentin Chary 21083e230dd2SCorentin Chary switch (data[0]) { 21093e230dd2SCorentin Chary case VNC_MSG_CLIENT_SET_PIXEL_FORMAT: 21103e230dd2SCorentin Chary if (len == 1) 21113e230dd2SCorentin Chary return 20; 21123e230dd2SCorentin Chary 21133e230dd2SCorentin Chary set_pixel_format(vs, read_u8(data, 4), read_u8(data, 5), 21143e230dd2SCorentin Chary read_u8(data, 6), read_u8(data, 7), 21153e230dd2SCorentin Chary read_u16(data, 8), read_u16(data, 10), 21163e230dd2SCorentin Chary read_u16(data, 12), read_u8(data, 14), 21173e230dd2SCorentin Chary read_u8(data, 15), read_u8(data, 16)); 21183e230dd2SCorentin Chary break; 21193e230dd2SCorentin Chary case VNC_MSG_CLIENT_SET_ENCODINGS: 21203e230dd2SCorentin Chary if (len == 1) 21213e230dd2SCorentin Chary return 4; 21223e230dd2SCorentin Chary 21233e230dd2SCorentin Chary if (len == 4) { 21243e230dd2SCorentin Chary limit = read_u16(data, 2); 21253e230dd2SCorentin Chary if (limit > 0) 21263e230dd2SCorentin Chary return 4 + (limit * 4); 21273e230dd2SCorentin Chary } else 21283e230dd2SCorentin Chary limit = read_u16(data, 2); 21293e230dd2SCorentin Chary 21303e230dd2SCorentin Chary for (i = 0; i < limit; i++) { 21313e230dd2SCorentin Chary int32_t val = read_s32(data, 4 + (i * 4)); 21323e230dd2SCorentin Chary memcpy(data + 4 + (i * 4), &val, sizeof(val)); 21333e230dd2SCorentin Chary } 21343e230dd2SCorentin Chary 21353e230dd2SCorentin Chary set_encodings(vs, (int32_t *)(data + 4), limit); 21363e230dd2SCorentin Chary break; 21373e230dd2SCorentin Chary case VNC_MSG_CLIENT_FRAMEBUFFER_UPDATE_REQUEST: 21383e230dd2SCorentin Chary if (len == 1) 21393e230dd2SCorentin Chary return 10; 21403e230dd2SCorentin Chary 21413e230dd2SCorentin Chary framebuffer_update_request(vs, 21423e230dd2SCorentin Chary read_u8(data, 1), read_u16(data, 2), read_u16(data, 4), 21433e230dd2SCorentin Chary read_u16(data, 6), read_u16(data, 8)); 21443e230dd2SCorentin Chary break; 21453e230dd2SCorentin Chary case VNC_MSG_CLIENT_KEY_EVENT: 21463e230dd2SCorentin Chary if (len == 1) 21473e230dd2SCorentin Chary return 8; 21483e230dd2SCorentin Chary 21493e230dd2SCorentin Chary key_event(vs, read_u8(data, 1), read_u32(data, 4)); 21503e230dd2SCorentin Chary break; 21513e230dd2SCorentin Chary case VNC_MSG_CLIENT_POINTER_EVENT: 21523e230dd2SCorentin Chary if (len == 1) 21533e230dd2SCorentin Chary return 6; 21543e230dd2SCorentin Chary 21553e230dd2SCorentin Chary pointer_event(vs, read_u8(data, 1), read_u16(data, 2), read_u16(data, 4)); 21563e230dd2SCorentin Chary break; 21573e230dd2SCorentin Chary case VNC_MSG_CLIENT_CUT_TEXT: 2158f9a70e79SPeter Lieven if (len == 1) { 21593e230dd2SCorentin Chary return 8; 2160f9a70e79SPeter Lieven } 21613e230dd2SCorentin Chary if (len == 8) { 21623e230dd2SCorentin Chary uint32_t dlen = read_u32(data, 4); 2163f9a70e79SPeter Lieven if (dlen > (1 << 20)) { 2164f9a70e79SPeter Lieven error_report("vnc: client_cut_text msg payload has %u bytes" 2165f9a70e79SPeter Lieven " which exceeds our limit of 1MB.", dlen); 2166f9a70e79SPeter Lieven vnc_client_error(vs); 2167f9a70e79SPeter Lieven break; 2168f9a70e79SPeter Lieven } 2169f9a70e79SPeter Lieven if (dlen > 0) { 21703e230dd2SCorentin Chary return 8 + dlen; 21713e230dd2SCorentin Chary } 2172f9a70e79SPeter Lieven } 21733e230dd2SCorentin Chary 21743e230dd2SCorentin Chary client_cut_text(vs, read_u32(data, 4), data + 8); 21753e230dd2SCorentin Chary break; 21763e230dd2SCorentin Chary case VNC_MSG_CLIENT_QEMU: 21773e230dd2SCorentin Chary if (len == 1) 21783e230dd2SCorentin Chary return 2; 21793e230dd2SCorentin Chary 21803e230dd2SCorentin Chary switch (read_u8(data, 1)) { 21813e230dd2SCorentin Chary case VNC_MSG_CLIENT_QEMU_EXT_KEY_EVENT: 21823e230dd2SCorentin Chary if (len == 2) 21833e230dd2SCorentin Chary return 12; 21843e230dd2SCorentin Chary 21853e230dd2SCorentin Chary ext_key_event(vs, read_u16(data, 2), 21863e230dd2SCorentin Chary read_u32(data, 4), read_u32(data, 8)); 21873e230dd2SCorentin Chary break; 21883e230dd2SCorentin Chary case VNC_MSG_CLIENT_QEMU_AUDIO: 21893e230dd2SCorentin Chary if (len == 2) 21903e230dd2SCorentin Chary return 4; 21913e230dd2SCorentin Chary 21923e230dd2SCorentin Chary switch (read_u16 (data, 2)) { 21933e230dd2SCorentin Chary case VNC_MSG_CLIENT_QEMU_AUDIO_ENABLE: 21943e230dd2SCorentin Chary audio_add(vs); 21953e230dd2SCorentin Chary break; 21963e230dd2SCorentin Chary case VNC_MSG_CLIENT_QEMU_AUDIO_DISABLE: 21973e230dd2SCorentin Chary audio_del(vs); 21983e230dd2SCorentin Chary break; 21993e230dd2SCorentin Chary case VNC_MSG_CLIENT_QEMU_AUDIO_SET_FORMAT: 22003e230dd2SCorentin Chary if (len == 4) 22013e230dd2SCorentin Chary return 10; 22023e230dd2SCorentin Chary switch (read_u8(data, 4)) { 22033e230dd2SCorentin Chary case 0: vs->as.fmt = AUD_FMT_U8; break; 22043e230dd2SCorentin Chary case 1: vs->as.fmt = AUD_FMT_S8; break; 22053e230dd2SCorentin Chary case 2: vs->as.fmt = AUD_FMT_U16; break; 22063e230dd2SCorentin Chary case 3: vs->as.fmt = AUD_FMT_S16; break; 22073e230dd2SCorentin Chary case 4: vs->as.fmt = AUD_FMT_U32; break; 22083e230dd2SCorentin Chary case 5: vs->as.fmt = AUD_FMT_S32; break; 22093e230dd2SCorentin Chary default: 22103e230dd2SCorentin Chary printf("Invalid audio format %d\n", read_u8(data, 4)); 22113e230dd2SCorentin Chary vnc_client_error(vs); 22123e230dd2SCorentin Chary break; 22133e230dd2SCorentin Chary } 22143e230dd2SCorentin Chary vs->as.nchannels = read_u8(data, 5); 22153e230dd2SCorentin Chary if (vs->as.nchannels != 1 && vs->as.nchannels != 2) { 22163e230dd2SCorentin Chary printf("Invalid audio channel coount %d\n", 22173e230dd2SCorentin Chary read_u8(data, 5)); 22183e230dd2SCorentin Chary vnc_client_error(vs); 22193e230dd2SCorentin Chary break; 22203e230dd2SCorentin Chary } 22213e230dd2SCorentin Chary vs->as.freq = read_u32(data, 6); 22223e230dd2SCorentin Chary break; 22233e230dd2SCorentin Chary default: 22243e230dd2SCorentin Chary printf ("Invalid audio message %d\n", read_u8(data, 4)); 22253e230dd2SCorentin Chary vnc_client_error(vs); 22263e230dd2SCorentin Chary break; 22273e230dd2SCorentin Chary } 22283e230dd2SCorentin Chary break; 22293e230dd2SCorentin Chary 22303e230dd2SCorentin Chary default: 22313e230dd2SCorentin Chary printf("Msg: %d\n", read_u16(data, 0)); 22323e230dd2SCorentin Chary vnc_client_error(vs); 22333e230dd2SCorentin Chary break; 22343e230dd2SCorentin Chary } 22353e230dd2SCorentin Chary break; 22363e230dd2SCorentin Chary default: 22373e230dd2SCorentin Chary printf("Msg: %d\n", data[0]); 22383e230dd2SCorentin Chary vnc_client_error(vs); 22393e230dd2SCorentin Chary break; 22403e230dd2SCorentin Chary } 22413e230dd2SCorentin Chary 22423e230dd2SCorentin Chary vnc_read_when(vs, protocol_client_msg, 1); 22433e230dd2SCorentin Chary return 0; 22443e230dd2SCorentin Chary } 22453e230dd2SCorentin Chary 22463e230dd2SCorentin Chary static int protocol_client_init(VncState *vs, uint8_t *data, size_t len) 22473e230dd2SCorentin Chary { 22483e230dd2SCorentin Chary char buf[1024]; 22498cf36489SGerd Hoffmann VncShareMode mode; 22503e230dd2SCorentin Chary int size; 22513e230dd2SCorentin Chary 22528cf36489SGerd Hoffmann mode = data[0] ? VNC_SHARE_MODE_SHARED : VNC_SHARE_MODE_EXCLUSIVE; 22538cf36489SGerd Hoffmann switch (vs->vd->share_policy) { 22548cf36489SGerd Hoffmann case VNC_SHARE_POLICY_IGNORE: 22558cf36489SGerd Hoffmann /* 22568cf36489SGerd Hoffmann * Ignore the shared flag. Nothing to do here. 22578cf36489SGerd Hoffmann * 22588cf36489SGerd Hoffmann * Doesn't conform to the rfb spec but is traditional qemu 22598cf36489SGerd Hoffmann * behavior, thus left here as option for compatibility 22608cf36489SGerd Hoffmann * reasons. 22618cf36489SGerd Hoffmann */ 22628cf36489SGerd Hoffmann break; 22638cf36489SGerd Hoffmann case VNC_SHARE_POLICY_ALLOW_EXCLUSIVE: 22648cf36489SGerd Hoffmann /* 22658cf36489SGerd Hoffmann * Policy: Allow clients ask for exclusive access. 22668cf36489SGerd Hoffmann * 22678cf36489SGerd Hoffmann * Implementation: When a client asks for exclusive access, 22688cf36489SGerd Hoffmann * disconnect all others. Shared connects are allowed as long 22698cf36489SGerd Hoffmann * as no exclusive connection exists. 22708cf36489SGerd Hoffmann * 22718cf36489SGerd Hoffmann * This is how the rfb spec suggests to handle the shared flag. 22728cf36489SGerd Hoffmann */ 22738cf36489SGerd Hoffmann if (mode == VNC_SHARE_MODE_EXCLUSIVE) { 22748cf36489SGerd Hoffmann VncState *client; 22758cf36489SGerd Hoffmann QTAILQ_FOREACH(client, &vs->vd->clients, next) { 22768cf36489SGerd Hoffmann if (vs == client) { 22778cf36489SGerd Hoffmann continue; 22788cf36489SGerd Hoffmann } 22798cf36489SGerd Hoffmann if (client->share_mode != VNC_SHARE_MODE_EXCLUSIVE && 22808cf36489SGerd Hoffmann client->share_mode != VNC_SHARE_MODE_SHARED) { 22818cf36489SGerd Hoffmann continue; 22828cf36489SGerd Hoffmann } 22838cf36489SGerd Hoffmann vnc_disconnect_start(client); 22848cf36489SGerd Hoffmann } 22858cf36489SGerd Hoffmann } 22868cf36489SGerd Hoffmann if (mode == VNC_SHARE_MODE_SHARED) { 22878cf36489SGerd Hoffmann if (vs->vd->num_exclusive > 0) { 22888cf36489SGerd Hoffmann vnc_disconnect_start(vs); 22898cf36489SGerd Hoffmann return 0; 22908cf36489SGerd Hoffmann } 22918cf36489SGerd Hoffmann } 22928cf36489SGerd Hoffmann break; 22938cf36489SGerd Hoffmann case VNC_SHARE_POLICY_FORCE_SHARED: 22948cf36489SGerd Hoffmann /* 22958cf36489SGerd Hoffmann * Policy: Shared connects only. 22968cf36489SGerd Hoffmann * Implementation: Disallow clients asking for exclusive access. 22978cf36489SGerd Hoffmann * 22988cf36489SGerd Hoffmann * Useful for shared desktop sessions where you don't want 22998cf36489SGerd Hoffmann * someone forgetting to say -shared when running the vnc 23008cf36489SGerd Hoffmann * client disconnect everybody else. 23018cf36489SGerd Hoffmann */ 23028cf36489SGerd Hoffmann if (mode == VNC_SHARE_MODE_EXCLUSIVE) { 23038cf36489SGerd Hoffmann vnc_disconnect_start(vs); 23048cf36489SGerd Hoffmann return 0; 23058cf36489SGerd Hoffmann } 23068cf36489SGerd Hoffmann break; 23078cf36489SGerd Hoffmann } 23088cf36489SGerd Hoffmann vnc_set_share_mode(vs, mode); 23098cf36489SGerd Hoffmann 2310bea60dd7SPeter Lieven vs->client_width = pixman_image_get_width(vs->vd->server); 2311bea60dd7SPeter Lieven vs->client_height = pixman_image_get_height(vs->vd->server); 23123e230dd2SCorentin Chary vnc_write_u16(vs, vs->client_width); 23133e230dd2SCorentin Chary vnc_write_u16(vs, vs->client_height); 23143e230dd2SCorentin Chary 23153e230dd2SCorentin Chary pixel_format_message(vs); 23163e230dd2SCorentin Chary 23173e230dd2SCorentin Chary if (qemu_name) 23183e230dd2SCorentin Chary size = snprintf(buf, sizeof(buf), "QEMU (%s)", qemu_name); 23193e230dd2SCorentin Chary else 23203e230dd2SCorentin Chary size = snprintf(buf, sizeof(buf), "QEMU"); 23213e230dd2SCorentin Chary 23223e230dd2SCorentin Chary vnc_write_u32(vs, size); 23233e230dd2SCorentin Chary vnc_write(vs, buf, size); 23243e230dd2SCorentin Chary vnc_flush(vs); 23253e230dd2SCorentin Chary 23263e230dd2SCorentin Chary vnc_client_cache_auth(vs); 2327fb6ba0d5SWenchao Xia vnc_qmp_event(vs, QAPI_EVENT_VNC_INITIALIZED); 23283e230dd2SCorentin Chary 23293e230dd2SCorentin Chary vnc_read_when(vs, protocol_client_msg, 1); 23303e230dd2SCorentin Chary 23313e230dd2SCorentin Chary return 0; 23323e230dd2SCorentin Chary } 23333e230dd2SCorentin Chary 23343e230dd2SCorentin Chary void start_client_init(VncState *vs) 23353e230dd2SCorentin Chary { 23363e230dd2SCorentin Chary vnc_read_when(vs, protocol_client_init, 1); 23373e230dd2SCorentin Chary } 23383e230dd2SCorentin Chary 23393e230dd2SCorentin Chary static void make_challenge(VncState *vs) 23403e230dd2SCorentin Chary { 23413e230dd2SCorentin Chary int i; 23423e230dd2SCorentin Chary 23433e230dd2SCorentin Chary srand(time(NULL)+getpid()+getpid()*987654+rand()); 23443e230dd2SCorentin Chary 23453e230dd2SCorentin Chary for (i = 0 ; i < sizeof(vs->challenge) ; i++) 23463e230dd2SCorentin Chary vs->challenge[i] = (int) (256.0*rand()/(RAND_MAX+1.0)); 23473e230dd2SCorentin Chary } 23483e230dd2SCorentin Chary 23493e230dd2SCorentin Chary static int protocol_client_auth_vnc(VncState *vs, uint8_t *data, size_t len) 23503e230dd2SCorentin Chary { 23513e230dd2SCorentin Chary unsigned char response[VNC_AUTH_CHALLENGE_SIZE]; 23523e230dd2SCorentin Chary int i, j, pwlen; 23533e230dd2SCorentin Chary unsigned char key[8]; 23543c9405a0SGerd Hoffmann time_t now = time(NULL); 23553e230dd2SCorentin Chary 23561cd20f8bSAnthony Liguori if (!vs->vd->password) { 23573e230dd2SCorentin Chary VNC_DEBUG("No password configured on server"); 23586bffdf0fSGerd Hoffmann goto reject; 23593e230dd2SCorentin Chary } 23603c9405a0SGerd Hoffmann if (vs->vd->expires < now) { 23613c9405a0SGerd Hoffmann VNC_DEBUG("Password is expired"); 23623c9405a0SGerd Hoffmann goto reject; 23633c9405a0SGerd Hoffmann } 23643e230dd2SCorentin Chary 23653e230dd2SCorentin Chary memcpy(response, vs->challenge, VNC_AUTH_CHALLENGE_SIZE); 23663e230dd2SCorentin Chary 23673e230dd2SCorentin Chary /* Calculate the expected challenge response */ 23683e230dd2SCorentin Chary pwlen = strlen(vs->vd->password); 23693e230dd2SCorentin Chary for (i=0; i<sizeof(key); i++) 23703e230dd2SCorentin Chary key[i] = i<pwlen ? vs->vd->password[i] : 0; 23713e230dd2SCorentin Chary deskey(key, EN0); 23723e230dd2SCorentin Chary for (j = 0; j < VNC_AUTH_CHALLENGE_SIZE; j += 8) 23733e230dd2SCorentin Chary des(response+j, response+j); 23743e230dd2SCorentin Chary 23753e230dd2SCorentin Chary /* Compare expected vs actual challenge response */ 23763e230dd2SCorentin Chary if (memcmp(response, data, VNC_AUTH_CHALLENGE_SIZE) != 0) { 2377e5bed759SDong Xu Wang VNC_DEBUG("Client challenge response did not match\n"); 23786bffdf0fSGerd Hoffmann goto reject; 23796bffdf0fSGerd Hoffmann } else { 23806bffdf0fSGerd Hoffmann VNC_DEBUG("Accepting VNC challenge response\n"); 23816bffdf0fSGerd Hoffmann vnc_write_u32(vs, 0); /* Accept auth */ 23826bffdf0fSGerd Hoffmann vnc_flush(vs); 23836bffdf0fSGerd Hoffmann 23846bffdf0fSGerd Hoffmann start_client_init(vs); 23856bffdf0fSGerd Hoffmann } 23866bffdf0fSGerd Hoffmann return 0; 23876bffdf0fSGerd Hoffmann 23886bffdf0fSGerd Hoffmann reject: 23893e230dd2SCorentin Chary vnc_write_u32(vs, 1); /* Reject auth */ 23903e230dd2SCorentin Chary if (vs->minor >= 8) { 23913e230dd2SCorentin Chary static const char err[] = "Authentication failed"; 23923e230dd2SCorentin Chary vnc_write_u32(vs, sizeof(err)); 23933e230dd2SCorentin Chary vnc_write(vs, err, sizeof(err)); 23943e230dd2SCorentin Chary } 23953e230dd2SCorentin Chary vnc_flush(vs); 23963e230dd2SCorentin Chary vnc_client_error(vs); 23973e230dd2SCorentin Chary return 0; 23983e230dd2SCorentin Chary } 23993e230dd2SCorentin Chary 24003e230dd2SCorentin Chary void start_auth_vnc(VncState *vs) 24013e230dd2SCorentin Chary { 24023e230dd2SCorentin Chary make_challenge(vs); 24033e230dd2SCorentin Chary /* Send client a 'random' challenge */ 24043e230dd2SCorentin Chary vnc_write(vs, vs->challenge, sizeof(vs->challenge)); 24053e230dd2SCorentin Chary vnc_flush(vs); 24063e230dd2SCorentin Chary 24073e230dd2SCorentin Chary vnc_read_when(vs, protocol_client_auth_vnc, sizeof(vs->challenge)); 24083e230dd2SCorentin Chary } 24093e230dd2SCorentin Chary 24103e230dd2SCorentin Chary 24113e230dd2SCorentin Chary static int protocol_client_auth(VncState *vs, uint8_t *data, size_t len) 24123e230dd2SCorentin Chary { 24133e230dd2SCorentin Chary /* We only advertise 1 auth scheme at a time, so client 24143e230dd2SCorentin Chary * must pick the one we sent. Verify this */ 24157e7e2ebcSDaniel P. Berrange if (data[0] != vs->auth) { /* Reject auth */ 24163e230dd2SCorentin Chary VNC_DEBUG("Reject auth %d because it didn't match advertized\n", (int)data[0]); 24173e230dd2SCorentin Chary vnc_write_u32(vs, 1); 24183e230dd2SCorentin Chary if (vs->minor >= 8) { 24193e230dd2SCorentin Chary static const char err[] = "Authentication failed"; 24203e230dd2SCorentin Chary vnc_write_u32(vs, sizeof(err)); 24213e230dd2SCorentin Chary vnc_write(vs, err, sizeof(err)); 24223e230dd2SCorentin Chary } 24233e230dd2SCorentin Chary vnc_client_error(vs); 24243e230dd2SCorentin Chary } else { /* Accept requested auth */ 24253e230dd2SCorentin Chary VNC_DEBUG("Client requested auth %d\n", (int)data[0]); 24267e7e2ebcSDaniel P. Berrange switch (vs->auth) { 24273e230dd2SCorentin Chary case VNC_AUTH_NONE: 24283e230dd2SCorentin Chary VNC_DEBUG("Accept auth none\n"); 24293e230dd2SCorentin Chary if (vs->minor >= 8) { 24303e230dd2SCorentin Chary vnc_write_u32(vs, 0); /* Accept auth completion */ 24313e230dd2SCorentin Chary vnc_flush(vs); 24323e230dd2SCorentin Chary } 24333e230dd2SCorentin Chary start_client_init(vs); 24343e230dd2SCorentin Chary break; 24353e230dd2SCorentin Chary 24363e230dd2SCorentin Chary case VNC_AUTH_VNC: 24373e230dd2SCorentin Chary VNC_DEBUG("Start VNC auth\n"); 24383e230dd2SCorentin Chary start_auth_vnc(vs); 24393e230dd2SCorentin Chary break; 24403e230dd2SCorentin Chary 24413e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS 24423e230dd2SCorentin Chary case VNC_AUTH_VENCRYPT: 24433a93113aSDong Xu Wang VNC_DEBUG("Accept VeNCrypt auth\n"); 24443e230dd2SCorentin Chary start_auth_vencrypt(vs); 24453e230dd2SCorentin Chary break; 24463e230dd2SCorentin Chary #endif /* CONFIG_VNC_TLS */ 24473e230dd2SCorentin Chary 24483e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL 24493e230dd2SCorentin Chary case VNC_AUTH_SASL: 24503e230dd2SCorentin Chary VNC_DEBUG("Accept SASL auth\n"); 24513e230dd2SCorentin Chary start_auth_sasl(vs); 24523e230dd2SCorentin Chary break; 24533e230dd2SCorentin Chary #endif /* CONFIG_VNC_SASL */ 24543e230dd2SCorentin Chary 24553e230dd2SCorentin Chary default: /* Should not be possible, but just in case */ 24567e7e2ebcSDaniel P. Berrange VNC_DEBUG("Reject auth %d server code bug\n", vs->auth); 24573e230dd2SCorentin Chary vnc_write_u8(vs, 1); 24583e230dd2SCorentin Chary if (vs->minor >= 8) { 24593e230dd2SCorentin Chary static const char err[] = "Authentication failed"; 24603e230dd2SCorentin Chary vnc_write_u32(vs, sizeof(err)); 24613e230dd2SCorentin Chary vnc_write(vs, err, sizeof(err)); 24623e230dd2SCorentin Chary } 24633e230dd2SCorentin Chary vnc_client_error(vs); 24643e230dd2SCorentin Chary } 24653e230dd2SCorentin Chary } 24663e230dd2SCorentin Chary return 0; 24673e230dd2SCorentin Chary } 24683e230dd2SCorentin Chary 24693e230dd2SCorentin Chary static int protocol_version(VncState *vs, uint8_t *version, size_t len) 24703e230dd2SCorentin Chary { 24713e230dd2SCorentin Chary char local[13]; 24723e230dd2SCorentin Chary 24733e230dd2SCorentin Chary memcpy(local, version, 12); 24743e230dd2SCorentin Chary local[12] = 0; 24753e230dd2SCorentin Chary 24763e230dd2SCorentin Chary if (sscanf(local, "RFB %03d.%03d\n", &vs->major, &vs->minor) != 2) { 24773e230dd2SCorentin Chary VNC_DEBUG("Malformed protocol version %s\n", local); 24783e230dd2SCorentin Chary vnc_client_error(vs); 24793e230dd2SCorentin Chary return 0; 24803e230dd2SCorentin Chary } 24813e230dd2SCorentin Chary VNC_DEBUG("Client request protocol version %d.%d\n", vs->major, vs->minor); 24823e230dd2SCorentin Chary if (vs->major != 3 || 24833e230dd2SCorentin Chary (vs->minor != 3 && 24843e230dd2SCorentin Chary vs->minor != 4 && 24853e230dd2SCorentin Chary vs->minor != 5 && 24863e230dd2SCorentin Chary vs->minor != 7 && 24873e230dd2SCorentin Chary vs->minor != 8)) { 24883e230dd2SCorentin Chary VNC_DEBUG("Unsupported client version\n"); 24893e230dd2SCorentin Chary vnc_write_u32(vs, VNC_AUTH_INVALID); 24903e230dd2SCorentin Chary vnc_flush(vs); 24913e230dd2SCorentin Chary vnc_client_error(vs); 24923e230dd2SCorentin Chary return 0; 24933e230dd2SCorentin Chary } 24943e230dd2SCorentin Chary /* Some broken clients report v3.4 or v3.5, which spec requires to be treated 24953e230dd2SCorentin Chary * as equivalent to v3.3 by servers 24963e230dd2SCorentin Chary */ 24973e230dd2SCorentin Chary if (vs->minor == 4 || vs->minor == 5) 24983e230dd2SCorentin Chary vs->minor = 3; 24993e230dd2SCorentin Chary 25003e230dd2SCorentin Chary if (vs->minor == 3) { 25017e7e2ebcSDaniel P. Berrange if (vs->auth == VNC_AUTH_NONE) { 25023e230dd2SCorentin Chary VNC_DEBUG("Tell client auth none\n"); 25037e7e2ebcSDaniel P. Berrange vnc_write_u32(vs, vs->auth); 25043e230dd2SCorentin Chary vnc_flush(vs); 25053e230dd2SCorentin Chary start_client_init(vs); 25067e7e2ebcSDaniel P. Berrange } else if (vs->auth == VNC_AUTH_VNC) { 25073e230dd2SCorentin Chary VNC_DEBUG("Tell client VNC auth\n"); 25087e7e2ebcSDaniel P. Berrange vnc_write_u32(vs, vs->auth); 25093e230dd2SCorentin Chary vnc_flush(vs); 25103e230dd2SCorentin Chary start_auth_vnc(vs); 25113e230dd2SCorentin Chary } else { 25127e7e2ebcSDaniel P. Berrange VNC_DEBUG("Unsupported auth %d for protocol 3.3\n", vs->auth); 25133e230dd2SCorentin Chary vnc_write_u32(vs, VNC_AUTH_INVALID); 25143e230dd2SCorentin Chary vnc_flush(vs); 25153e230dd2SCorentin Chary vnc_client_error(vs); 25163e230dd2SCorentin Chary } 25173e230dd2SCorentin Chary } else { 25187e7e2ebcSDaniel P. Berrange VNC_DEBUG("Telling client we support auth %d\n", vs->auth); 25193e230dd2SCorentin Chary vnc_write_u8(vs, 1); /* num auth */ 25207e7e2ebcSDaniel P. Berrange vnc_write_u8(vs, vs->auth); 25213e230dd2SCorentin Chary vnc_read_when(vs, protocol_client_auth, 1); 25223e230dd2SCorentin Chary vnc_flush(vs); 25233e230dd2SCorentin Chary } 25243e230dd2SCorentin Chary 25253e230dd2SCorentin Chary return 0; 25263e230dd2SCorentin Chary } 25273e230dd2SCorentin Chary 2528999342a0SCorentin Chary static VncRectStat *vnc_stat_rect(VncDisplay *vd, int x, int y) 2529999342a0SCorentin Chary { 2530999342a0SCorentin Chary struct VncSurface *vs = &vd->guest; 2531999342a0SCorentin Chary 2532999342a0SCorentin Chary return &vs->stats[y / VNC_STAT_RECT][x / VNC_STAT_RECT]; 2533999342a0SCorentin Chary } 2534999342a0SCorentin Chary 25357d964c9dSCorentin Chary void vnc_sent_lossy_rect(VncState *vs, int x, int y, int w, int h) 25367d964c9dSCorentin Chary { 25377d964c9dSCorentin Chary int i, j; 25387d964c9dSCorentin Chary 25397d964c9dSCorentin Chary w = (x + w) / VNC_STAT_RECT; 25407d964c9dSCorentin Chary h = (y + h) / VNC_STAT_RECT; 25417d964c9dSCorentin Chary x /= VNC_STAT_RECT; 25427d964c9dSCorentin Chary y /= VNC_STAT_RECT; 25437d964c9dSCorentin Chary 2544207f328aSCorentin Chary for (j = y; j <= h; j++) { 2545207f328aSCorentin Chary for (i = x; i <= w; i++) { 25467d964c9dSCorentin Chary vs->lossy_rect[j][i] = 1; 25477d964c9dSCorentin Chary } 25487d964c9dSCorentin Chary } 25497d964c9dSCorentin Chary } 25507d964c9dSCorentin Chary 25517d964c9dSCorentin Chary static int vnc_refresh_lossy_rect(VncDisplay *vd, int x, int y) 25527d964c9dSCorentin Chary { 25537d964c9dSCorentin Chary VncState *vs; 25547d964c9dSCorentin Chary int sty = y / VNC_STAT_RECT; 25557d964c9dSCorentin Chary int stx = x / VNC_STAT_RECT; 25567d964c9dSCorentin Chary int has_dirty = 0; 25577d964c9dSCorentin Chary 25587d964c9dSCorentin Chary y = y / VNC_STAT_RECT * VNC_STAT_RECT; 25597d964c9dSCorentin Chary x = x / VNC_STAT_RECT * VNC_STAT_RECT; 25607d964c9dSCorentin Chary 25617d964c9dSCorentin Chary QTAILQ_FOREACH(vs, &vd->clients, next) { 2562bc2429b9SCorentin Chary int j; 25637d964c9dSCorentin Chary 25647d964c9dSCorentin Chary /* kernel send buffers are full -> refresh later */ 25657d964c9dSCorentin Chary if (vs->output.offset) { 25667d964c9dSCorentin Chary continue; 25677d964c9dSCorentin Chary } 25687d964c9dSCorentin Chary 25697d964c9dSCorentin Chary if (!vs->lossy_rect[sty][stx]) { 25707d964c9dSCorentin Chary continue; 25717d964c9dSCorentin Chary } 2572207f328aSCorentin Chary 25737d964c9dSCorentin Chary vs->lossy_rect[sty][stx] = 0; 25747d964c9dSCorentin Chary for (j = 0; j < VNC_STAT_RECT; ++j) { 2575b4c85ddcSPeter Lieven bitmap_set(vs->dirty[y + j], 2576b4c85ddcSPeter Lieven x / VNC_DIRTY_PIXELS_PER_BIT, 2577b4c85ddcSPeter Lieven VNC_STAT_RECT / VNC_DIRTY_PIXELS_PER_BIT); 25787d964c9dSCorentin Chary } 25797d964c9dSCorentin Chary has_dirty++; 25807d964c9dSCorentin Chary } 2581207f328aSCorentin Chary 25827d964c9dSCorentin Chary return has_dirty; 25837d964c9dSCorentin Chary } 25847d964c9dSCorentin Chary 25857d964c9dSCorentin Chary static int vnc_update_stats(VncDisplay *vd, struct timeval * tv) 2586999342a0SCorentin Chary { 25879f64916dSGerd Hoffmann int width = pixman_image_get_width(vd->guest.fb); 25889f64916dSGerd Hoffmann int height = pixman_image_get_height(vd->guest.fb); 2589999342a0SCorentin Chary int x, y; 2590999342a0SCorentin Chary struct timeval res; 25917d964c9dSCorentin Chary int has_dirty = 0; 2592999342a0SCorentin Chary 25939f64916dSGerd Hoffmann for (y = 0; y < height; y += VNC_STAT_RECT) { 25949f64916dSGerd Hoffmann for (x = 0; x < width; x += VNC_STAT_RECT) { 2595999342a0SCorentin Chary VncRectStat *rect = vnc_stat_rect(vd, x, y); 2596999342a0SCorentin Chary 2597999342a0SCorentin Chary rect->updated = false; 2598999342a0SCorentin Chary } 2599999342a0SCorentin Chary } 2600999342a0SCorentin Chary 2601ad620c29SBlue Swirl qemu_timersub(tv, &VNC_REFRESH_STATS, &res); 2602999342a0SCorentin Chary 2603999342a0SCorentin Chary if (timercmp(&vd->guest.last_freq_check, &res, >)) { 26047d964c9dSCorentin Chary return has_dirty; 2605999342a0SCorentin Chary } 2606999342a0SCorentin Chary vd->guest.last_freq_check = *tv; 2607999342a0SCorentin Chary 26089f64916dSGerd Hoffmann for (y = 0; y < height; y += VNC_STAT_RECT) { 26099f64916dSGerd Hoffmann for (x = 0; x < width; x += VNC_STAT_RECT) { 2610999342a0SCorentin Chary VncRectStat *rect= vnc_stat_rect(vd, x, y); 2611999342a0SCorentin Chary int count = ARRAY_SIZE(rect->times); 2612999342a0SCorentin Chary struct timeval min, max; 2613999342a0SCorentin Chary 2614999342a0SCorentin Chary if (!timerisset(&rect->times[count - 1])) { 2615999342a0SCorentin Chary continue ; 2616999342a0SCorentin Chary } 2617999342a0SCorentin Chary 2618999342a0SCorentin Chary max = rect->times[(rect->idx + count - 1) % count]; 2619ad620c29SBlue Swirl qemu_timersub(tv, &max, &res); 2620999342a0SCorentin Chary 2621999342a0SCorentin Chary if (timercmp(&res, &VNC_REFRESH_LOSSY, >)) { 2622999342a0SCorentin Chary rect->freq = 0; 26237d964c9dSCorentin Chary has_dirty += vnc_refresh_lossy_rect(vd, x, y); 2624999342a0SCorentin Chary memset(rect->times, 0, sizeof (rect->times)); 2625999342a0SCorentin Chary continue ; 2626999342a0SCorentin Chary } 2627999342a0SCorentin Chary 2628999342a0SCorentin Chary min = rect->times[rect->idx]; 2629999342a0SCorentin Chary max = rect->times[(rect->idx + count - 1) % count]; 2630ad620c29SBlue Swirl qemu_timersub(&max, &min, &res); 2631999342a0SCorentin Chary 2632999342a0SCorentin Chary rect->freq = res.tv_sec + res.tv_usec / 1000000.; 2633999342a0SCorentin Chary rect->freq /= count; 2634999342a0SCorentin Chary rect->freq = 1. / rect->freq; 2635999342a0SCorentin Chary } 2636999342a0SCorentin Chary } 26377d964c9dSCorentin Chary return has_dirty; 2638999342a0SCorentin Chary } 2639999342a0SCorentin Chary 2640999342a0SCorentin Chary double vnc_update_freq(VncState *vs, int x, int y, int w, int h) 2641999342a0SCorentin Chary { 2642999342a0SCorentin Chary int i, j; 2643999342a0SCorentin Chary double total = 0; 2644999342a0SCorentin Chary int num = 0; 2645999342a0SCorentin Chary 2646999342a0SCorentin Chary x = (x / VNC_STAT_RECT) * VNC_STAT_RECT; 2647999342a0SCorentin Chary y = (y / VNC_STAT_RECT) * VNC_STAT_RECT; 2648999342a0SCorentin Chary 2649999342a0SCorentin Chary for (j = y; j <= y + h; j += VNC_STAT_RECT) { 2650999342a0SCorentin Chary for (i = x; i <= x + w; i += VNC_STAT_RECT) { 2651999342a0SCorentin Chary total += vnc_stat_rect(vs->vd, i, j)->freq; 2652999342a0SCorentin Chary num++; 2653999342a0SCorentin Chary } 2654999342a0SCorentin Chary } 2655999342a0SCorentin Chary 2656999342a0SCorentin Chary if (num) { 2657999342a0SCorentin Chary return total / num; 2658999342a0SCorentin Chary } else { 2659999342a0SCorentin Chary return 0; 2660999342a0SCorentin Chary } 2661999342a0SCorentin Chary } 2662999342a0SCorentin Chary 2663999342a0SCorentin Chary static void vnc_rect_updated(VncDisplay *vd, int x, int y, struct timeval * tv) 2664999342a0SCorentin Chary { 2665999342a0SCorentin Chary VncRectStat *rect; 2666999342a0SCorentin Chary 2667999342a0SCorentin Chary rect = vnc_stat_rect(vd, x, y); 2668999342a0SCorentin Chary if (rect->updated) { 2669999342a0SCorentin Chary return ; 2670999342a0SCorentin Chary } 2671999342a0SCorentin Chary rect->times[rect->idx] = *tv; 2672999342a0SCorentin Chary rect->idx = (rect->idx + 1) % ARRAY_SIZE(rect->times); 2673999342a0SCorentin Chary rect->updated = true; 2674999342a0SCorentin Chary } 2675999342a0SCorentin Chary 26763e230dd2SCorentin Chary static int vnc_refresh_server_surface(VncDisplay *vd) 26773e230dd2SCorentin Chary { 2678bea60dd7SPeter Lieven int width = MIN(pixman_image_get_width(vd->guest.fb), 2679bea60dd7SPeter Lieven pixman_image_get_width(vd->server)); 2680bea60dd7SPeter Lieven int height = MIN(pixman_image_get_height(vd->guest.fb), 2681bea60dd7SPeter Lieven pixman_image_get_height(vd->server)); 2682bea60dd7SPeter Lieven int cmp_bytes, server_stride, min_stride, guest_stride, y = 0; 268312b316d4SPeter Lieven uint8_t *guest_row0 = NULL, *server_row0; 26843e230dd2SCorentin Chary VncState *vs; 26853e230dd2SCorentin Chary int has_dirty = 0; 26869f64916dSGerd Hoffmann pixman_image_t *tmpbuf = NULL; 26873e230dd2SCorentin Chary 268880e0c8c3SCorentin Chary struct timeval tv = { 0, 0 }; 2689999342a0SCorentin Chary 269080e0c8c3SCorentin Chary if (!vd->non_adaptive) { 2691999342a0SCorentin Chary gettimeofday(&tv, NULL); 26927d964c9dSCorentin Chary has_dirty = vnc_update_stats(vd, &tv); 269380e0c8c3SCorentin Chary } 2694999342a0SCorentin Chary 26953e230dd2SCorentin Chary /* 26963e230dd2SCorentin Chary * Walk through the guest dirty map. 26973e230dd2SCorentin Chary * Check and copy modified bits from guest to server surface. 26983e230dd2SCorentin Chary * Update server dirty map. 26993e230dd2SCorentin Chary */ 2700bea60dd7SPeter Lieven server_row0 = (uint8_t *)pixman_image_get_data(vd->server); 2701bea60dd7SPeter Lieven server_stride = guest_stride = pixman_image_get_stride(vd->server); 2702bea60dd7SPeter Lieven cmp_bytes = MIN(VNC_DIRTY_PIXELS_PER_BIT * VNC_SERVER_FB_BYTES, 2703bea60dd7SPeter Lieven server_stride); 27049f64916dSGerd Hoffmann if (vd->guest.format != VNC_SERVER_FB_FORMAT) { 27059f64916dSGerd Hoffmann int width = pixman_image_get_width(vd->server); 27069f64916dSGerd Hoffmann tmpbuf = qemu_pixman_linebuf_create(VNC_SERVER_FB_FORMAT, width); 270712b316d4SPeter Lieven } else { 270812b316d4SPeter Lieven guest_row0 = (uint8_t *)pixman_image_get_data(vd->guest.fb); 270912b316d4SPeter Lieven guest_stride = pixman_image_get_stride(vd->guest.fb); 27109f64916dSGerd Hoffmann } 2711bea60dd7SPeter Lieven min_stride = MIN(server_stride, guest_stride); 271212b316d4SPeter Lieven 271312b316d4SPeter Lieven for (;;) { 27143e230dd2SCorentin Chary int x; 271512b316d4SPeter Lieven uint8_t *guest_ptr, *server_ptr; 271612b316d4SPeter Lieven unsigned long offset = find_next_bit((unsigned long *) &vd->guest.dirty, 271712b316d4SPeter Lieven height * VNC_DIRTY_BPL(&vd->guest), 271812b316d4SPeter Lieven y * VNC_DIRTY_BPL(&vd->guest)); 271912b316d4SPeter Lieven if (offset == height * VNC_DIRTY_BPL(&vd->guest)) { 272012b316d4SPeter Lieven /* no more dirty bits */ 272112b316d4SPeter Lieven break; 272212b316d4SPeter Lieven } 272312b316d4SPeter Lieven y = offset / VNC_DIRTY_BPL(&vd->guest); 272412b316d4SPeter Lieven x = offset % VNC_DIRTY_BPL(&vd->guest); 272512b316d4SPeter Lieven 272612b316d4SPeter Lieven server_ptr = server_row0 + y * server_stride + x * cmp_bytes; 27273e230dd2SCorentin Chary 27289f64916dSGerd Hoffmann if (vd->guest.format != VNC_SERVER_FB_FORMAT) { 2729bc210eb1SGerd Hoffmann qemu_pixman_linebuf_fill(tmpbuf, vd->guest.fb, width, 0, y); 27309f64916dSGerd Hoffmann guest_ptr = (uint8_t *)pixman_image_get_data(tmpbuf); 27319f64916dSGerd Hoffmann } else { 273212b316d4SPeter Lieven guest_ptr = guest_row0 + y * guest_stride; 27339f64916dSGerd Hoffmann } 273412b316d4SPeter Lieven guest_ptr += x * cmp_bytes; 27353e230dd2SCorentin Chary 273612b316d4SPeter Lieven for (; x < DIV_ROUND_UP(width, VNC_DIRTY_PIXELS_PER_BIT); 273712b316d4SPeter Lieven x++, guest_ptr += cmp_bytes, server_ptr += cmp_bytes) { 2738bea60dd7SPeter Lieven int _cmp_bytes = cmp_bytes; 273912b316d4SPeter Lieven if (!test_and_clear_bit(x, vd->guest.dirty[y])) { 27403e230dd2SCorentin Chary continue; 2741b4c85ddcSPeter Lieven } 2742bea60dd7SPeter Lieven if ((x + 1) * cmp_bytes > min_stride) { 2743bea60dd7SPeter Lieven _cmp_bytes = min_stride - x * cmp_bytes; 2744bea60dd7SPeter Lieven } 2745bea60dd7SPeter Lieven if (memcmp(server_ptr, guest_ptr, _cmp_bytes) == 0) { 27463e230dd2SCorentin Chary continue; 2747b4c85ddcSPeter Lieven } 2748bea60dd7SPeter Lieven memcpy(server_ptr, guest_ptr, _cmp_bytes); 274912b316d4SPeter Lieven if (!vd->non_adaptive) { 275012b316d4SPeter Lieven vnc_rect_updated(vd, x * VNC_DIRTY_PIXELS_PER_BIT, 275112b316d4SPeter Lieven y, &tv); 275212b316d4SPeter Lieven } 27533e230dd2SCorentin Chary QTAILQ_FOREACH(vs, &vd->clients, next) { 275412b316d4SPeter Lieven set_bit(x, vs->dirty[y]); 27553e230dd2SCorentin Chary } 27563e230dd2SCorentin Chary has_dirty++; 27573e230dd2SCorentin Chary } 275812b316d4SPeter Lieven 275912b316d4SPeter Lieven y++; 27603e230dd2SCorentin Chary } 27619f64916dSGerd Hoffmann qemu_pixman_image_unref(tmpbuf); 27623e230dd2SCorentin Chary return has_dirty; 27633e230dd2SCorentin Chary } 27643e230dd2SCorentin Chary 27650f7b2864SGerd Hoffmann static void vnc_refresh(DisplayChangeListener *dcl) 27663e230dd2SCorentin Chary { 27670f7b2864SGerd Hoffmann VncDisplay *vd = container_of(dcl, VncDisplay, dcl); 27683e230dd2SCorentin Chary VncState *vs, *vn; 27693e230dd2SCorentin Chary int has_dirty, rects = 0; 27703e230dd2SCorentin Chary 27711dbfa005SGerd Hoffmann graphic_hw_update(NULL); 27723e230dd2SCorentin Chary 2773bd023f95SCorentin Chary if (vnc_trylock_display(vd)) { 27740f7b2864SGerd Hoffmann update_displaychangelistener(&vd->dcl, VNC_REFRESH_INTERVAL_BASE); 2775bd023f95SCorentin Chary return; 2776bd023f95SCorentin Chary } 2777bd023f95SCorentin Chary 27783e230dd2SCorentin Chary has_dirty = vnc_refresh_server_surface(vd); 2779bd023f95SCorentin Chary vnc_unlock_display(vd); 27803e230dd2SCorentin Chary 27813e230dd2SCorentin Chary QTAILQ_FOREACH_SAFE(vs, &vd->clients, next, vn) { 278238ee14f4SGerd Hoffmann rects += vnc_update_client(vs, has_dirty, false); 27833e230dd2SCorentin Chary /* vs might be free()ed here */ 27843e230dd2SCorentin Chary } 2785bd023f95SCorentin Chary 27860f7b2864SGerd Hoffmann if (QTAILQ_EMPTY(&vd->clients)) { 27870f7b2864SGerd Hoffmann update_displaychangelistener(&vd->dcl, VNC_REFRESH_INTERVAL_MAX); 27883e230dd2SCorentin Chary return; 27890f7b2864SGerd Hoffmann } 27903e230dd2SCorentin Chary 27913e230dd2SCorentin Chary if (has_dirty && rects) { 27920f7b2864SGerd Hoffmann vd->dcl.update_interval /= 2; 27930f7b2864SGerd Hoffmann if (vd->dcl.update_interval < VNC_REFRESH_INTERVAL_BASE) { 27940f7b2864SGerd Hoffmann vd->dcl.update_interval = VNC_REFRESH_INTERVAL_BASE; 27950f7b2864SGerd Hoffmann } 27963e230dd2SCorentin Chary } else { 27970f7b2864SGerd Hoffmann vd->dcl.update_interval += VNC_REFRESH_INTERVAL_INC; 27980f7b2864SGerd Hoffmann if (vd->dcl.update_interval > VNC_REFRESH_INTERVAL_MAX) { 27990f7b2864SGerd Hoffmann vd->dcl.update_interval = VNC_REFRESH_INTERVAL_MAX; 28003e230dd2SCorentin Chary } 28013e230dd2SCorentin Chary } 28023e230dd2SCorentin Chary } 28033e230dd2SCorentin Chary 28042c8cf549SMichael Tokarev static void vnc_connect(VncDisplay *vd, int csock, 28052c8cf549SMichael Tokarev bool skipauth, bool websocket) 28063e230dd2SCorentin Chary { 28077267c094SAnthony Liguori VncState *vs = g_malloc0(sizeof(VncState)); 28087d964c9dSCorentin Chary int i; 28097d964c9dSCorentin Chary 28103e230dd2SCorentin Chary vs->csock = csock; 28117e7e2ebcSDaniel P. Berrange 28127e7e2ebcSDaniel P. Berrange if (skipauth) { 28137e7e2ebcSDaniel P. Berrange vs->auth = VNC_AUTH_NONE; 28147e7e2ebcSDaniel P. Berrange #ifdef CONFIG_VNC_TLS 28157e7e2ebcSDaniel P. Berrange vs->subauth = VNC_AUTH_INVALID; 28167e7e2ebcSDaniel P. Berrange #endif 28177e7e2ebcSDaniel P. Berrange } else { 28187e7e2ebcSDaniel P. Berrange vs->auth = vd->auth; 28197e7e2ebcSDaniel P. Berrange #ifdef CONFIG_VNC_TLS 28207e7e2ebcSDaniel P. Berrange vs->subauth = vd->subauth; 28217e7e2ebcSDaniel P. Berrange #endif 28227e7e2ebcSDaniel P. Berrange } 28237e7e2ebcSDaniel P. Berrange 28247267c094SAnthony Liguori vs->lossy_rect = g_malloc0(VNC_STAT_ROWS * sizeof (*vs->lossy_rect)); 28257d964c9dSCorentin Chary for (i = 0; i < VNC_STAT_ROWS; ++i) { 28267267c094SAnthony Liguori vs->lossy_rect[i] = g_malloc0(VNC_STAT_COLS * sizeof (uint8_t)); 28277d964c9dSCorentin Chary } 28283e230dd2SCorentin Chary 28293e230dd2SCorentin Chary VNC_DEBUG("New client on socket %d\n", csock); 28300f7b2864SGerd Hoffmann update_displaychangelistener(&vd->dcl, VNC_REFRESH_INTERVAL_BASE); 2831f9e8caccSStefan Hajnoczi qemu_set_nonblock(vs->csock); 28327536ee4bSTim Hardeck #ifdef CONFIG_VNC_WS 28337536ee4bSTim Hardeck if (websocket) { 28347536ee4bSTim Hardeck vs->websocket = 1; 28350057a0d5STim Hardeck #ifdef CONFIG_VNC_TLS 28360057a0d5STim Hardeck if (vd->tls.x509cert) { 28370057a0d5STim Hardeck qemu_set_fd_handler2(vs->csock, NULL, vncws_tls_handshake_peek, 28380057a0d5STim Hardeck NULL, vs); 28390057a0d5STim Hardeck } else 28400057a0d5STim Hardeck #endif /* CONFIG_VNC_TLS */ 28410057a0d5STim Hardeck { 28420057a0d5STim Hardeck qemu_set_fd_handler2(vs->csock, NULL, vncws_handshake_read, 28430057a0d5STim Hardeck NULL, vs); 28440057a0d5STim Hardeck } 28457536ee4bSTim Hardeck } else 28467536ee4bSTim Hardeck #endif /* CONFIG_VNC_WS */ 28477536ee4bSTim Hardeck { 28483e230dd2SCorentin Chary qemu_set_fd_handler2(vs->csock, NULL, vnc_client_read, NULL, vs); 28497536ee4bSTim Hardeck } 28503e230dd2SCorentin Chary 28513e230dd2SCorentin Chary vnc_client_cache_addr(vs); 2852fb6ba0d5SWenchao Xia vnc_qmp_event(vs, QAPI_EVENT_VNC_CONNECTED); 28538cf36489SGerd Hoffmann vnc_set_share_mode(vs, VNC_SHARE_MODE_CONNECTING); 28543e230dd2SCorentin Chary 28553e230dd2SCorentin Chary vs->vd = vd; 28567536ee4bSTim Hardeck 28577536ee4bSTim Hardeck #ifdef CONFIG_VNC_WS 28587536ee4bSTim Hardeck if (!vs->websocket) 28597536ee4bSTim Hardeck #endif 28607536ee4bSTim Hardeck { 28617536ee4bSTim Hardeck vnc_init_state(vs); 28627536ee4bSTim Hardeck } 28637536ee4bSTim Hardeck } 28647536ee4bSTim Hardeck 28657536ee4bSTim Hardeck void vnc_init_state(VncState *vs) 28667536ee4bSTim Hardeck { 28676fd8e79aSTim Hardeck vs->initialized = true; 28687536ee4bSTim Hardeck VncDisplay *vd = vs->vd; 28697536ee4bSTim Hardeck 28703e230dd2SCorentin Chary vs->last_x = -1; 28713e230dd2SCorentin Chary vs->last_y = -1; 28723e230dd2SCorentin Chary 28733e230dd2SCorentin Chary vs->as.freq = 44100; 28743e230dd2SCorentin Chary vs->as.nchannels = 2; 28753e230dd2SCorentin Chary vs->as.fmt = AUD_FMT_S16; 28763e230dd2SCorentin Chary vs->as.endianness = 0; 28773e230dd2SCorentin Chary 2878bd023f95SCorentin Chary qemu_mutex_init(&vs->output_mutex); 2879175b2a6eSCorentin Chary vs->bh = qemu_bh_new(vnc_jobs_bh, vs); 2880bd023f95SCorentin Chary 28813e230dd2SCorentin Chary QTAILQ_INSERT_HEAD(&vd->clients, vs, next); 28823e230dd2SCorentin Chary 28831dbfa005SGerd Hoffmann graphic_hw_update(NULL); 28843e230dd2SCorentin Chary 28853e230dd2SCorentin Chary vnc_write(vs, "RFB 003.008\n", 12); 28863e230dd2SCorentin Chary vnc_flush(vs); 28873e230dd2SCorentin Chary vnc_read_when(vs, protocol_version, 12); 28883e230dd2SCorentin Chary reset_keys(vs); 28893e230dd2SCorentin Chary if (vs->vd->lock_key_sync) 28903e230dd2SCorentin Chary vs->led = qemu_add_led_event_handler(kbd_leds, vs); 28913e230dd2SCorentin Chary 28923e230dd2SCorentin Chary vs->mouse_mode_notifier.notify = check_pointer_type_change; 28933e230dd2SCorentin Chary qemu_add_mouse_mode_change_notifier(&vs->mouse_mode_notifier); 28943e230dd2SCorentin Chary 28953e230dd2SCorentin Chary /* vs might be free()ed here */ 28963e230dd2SCorentin Chary } 28973e230dd2SCorentin Chary 28987536ee4bSTim Hardeck static void vnc_listen_read(void *opaque, bool websocket) 28993e230dd2SCorentin Chary { 29003e230dd2SCorentin Chary VncDisplay *vs = opaque; 29013e230dd2SCorentin Chary struct sockaddr_in addr; 29023e230dd2SCorentin Chary socklen_t addrlen = sizeof(addr); 29037536ee4bSTim Hardeck int csock; 29043e230dd2SCorentin Chary 29053e230dd2SCorentin Chary /* Catch-up */ 29061dbfa005SGerd Hoffmann graphic_hw_update(NULL); 29077536ee4bSTim Hardeck #ifdef CONFIG_VNC_WS 29087536ee4bSTim Hardeck if (websocket) { 29097536ee4bSTim Hardeck csock = qemu_accept(vs->lwebsock, (struct sockaddr *)&addr, &addrlen); 29107536ee4bSTim Hardeck } else 29117536ee4bSTim Hardeck #endif /* CONFIG_VNC_WS */ 29127536ee4bSTim Hardeck { 29137536ee4bSTim Hardeck csock = qemu_accept(vs->lsock, (struct sockaddr *)&addr, &addrlen); 29147536ee4bSTim Hardeck } 29153e230dd2SCorentin Chary 29163e230dd2SCorentin Chary if (csock != -1) { 29172c8cf549SMichael Tokarev vnc_connect(vs, csock, false, websocket); 29183e230dd2SCorentin Chary } 29193e230dd2SCorentin Chary } 29203e230dd2SCorentin Chary 29217536ee4bSTim Hardeck static void vnc_listen_regular_read(void *opaque) 29227536ee4bSTim Hardeck { 29232c8cf549SMichael Tokarev vnc_listen_read(opaque, false); 29247536ee4bSTim Hardeck } 29257536ee4bSTim Hardeck 29267536ee4bSTim Hardeck #ifdef CONFIG_VNC_WS 29277536ee4bSTim Hardeck static void vnc_listen_websocket_read(void *opaque) 29287536ee4bSTim Hardeck { 29292c8cf549SMichael Tokarev vnc_listen_read(opaque, true); 29307536ee4bSTim Hardeck } 29317536ee4bSTim Hardeck #endif /* CONFIG_VNC_WS */ 29327536ee4bSTim Hardeck 29337c20b4a3SGerd Hoffmann static const DisplayChangeListenerOps dcl_ops = { 29347c20b4a3SGerd Hoffmann .dpy_name = "vnc", 29350f7b2864SGerd Hoffmann .dpy_refresh = vnc_refresh, 29367c20b4a3SGerd Hoffmann .dpy_gfx_copy = vnc_dpy_copy, 29377c20b4a3SGerd Hoffmann .dpy_gfx_update = vnc_dpy_update, 2938c12aeb86SGerd Hoffmann .dpy_gfx_switch = vnc_dpy_switch, 29397c20b4a3SGerd Hoffmann .dpy_mouse_set = vnc_mouse_set, 29407c20b4a3SGerd Hoffmann .dpy_cursor_define = vnc_dpy_cursor_define, 29417c20b4a3SGerd Hoffmann }; 29427c20b4a3SGerd Hoffmann 29433e230dd2SCorentin Chary void vnc_display_init(DisplayState *ds) 29443e230dd2SCorentin Chary { 29457267c094SAnthony Liguori VncDisplay *vs = g_malloc0(sizeof(*vs)); 29463e230dd2SCorentin Chary 29473e230dd2SCorentin Chary vnc_display = vs; 29483e230dd2SCorentin Chary 29493e230dd2SCorentin Chary vs->lsock = -1; 29507536ee4bSTim Hardeck #ifdef CONFIG_VNC_WS 29517536ee4bSTim Hardeck vs->lwebsock = -1; 29527536ee4bSTim Hardeck #endif 29533e230dd2SCorentin Chary 29543e230dd2SCorentin Chary QTAILQ_INIT(&vs->clients); 29553c9405a0SGerd Hoffmann vs->expires = TIME_MAX; 29563e230dd2SCorentin Chary 295740066175SGerd Hoffmann if (keyboard_layout) { 295840066175SGerd Hoffmann trace_vnc_key_map_init(keyboard_layout); 29593e230dd2SCorentin Chary vs->kbd_layout = init_keyboard_layout(name2keysym, keyboard_layout); 296040066175SGerd Hoffmann } else { 29613e230dd2SCorentin Chary vs->kbd_layout = init_keyboard_layout(name2keysym, "en-us"); 296240066175SGerd Hoffmann } 29633e230dd2SCorentin Chary 29643e230dd2SCorentin Chary if (!vs->kbd_layout) 29653e230dd2SCorentin Chary exit(1); 29663e230dd2SCorentin Chary 2967bd023f95SCorentin Chary qemu_mutex_init(&vs->mutex); 2968bd023f95SCorentin Chary vnc_start_worker_thread(); 2969bd023f95SCorentin Chary 297021ef45d7SGerd Hoffmann vs->dcl.ops = &dcl_ops; 29715209089fSGerd Hoffmann register_displaychangelistener(&vs->dcl); 29723e230dd2SCorentin Chary } 29733e230dd2SCorentin Chary 29743e230dd2SCorentin Chary 297571a8cdecSBlue Swirl static void vnc_display_close(DisplayState *ds) 29763e230dd2SCorentin Chary { 297721ef45d7SGerd Hoffmann VncDisplay *vs = vnc_display; 29783e230dd2SCorentin Chary 29793e230dd2SCorentin Chary if (!vs) 29803e230dd2SCorentin Chary return; 29817267c094SAnthony Liguori g_free(vs->display); 29823e230dd2SCorentin Chary vs->display = NULL; 29833e230dd2SCorentin Chary if (vs->lsock != -1) { 29843e230dd2SCorentin Chary qemu_set_fd_handler2(vs->lsock, NULL, NULL, NULL, NULL); 29853e230dd2SCorentin Chary close(vs->lsock); 29863e230dd2SCorentin Chary vs->lsock = -1; 29873e230dd2SCorentin Chary } 29887536ee4bSTim Hardeck #ifdef CONFIG_VNC_WS 29897536ee4bSTim Hardeck g_free(vs->ws_display); 29907536ee4bSTim Hardeck vs->ws_display = NULL; 29917536ee4bSTim Hardeck if (vs->lwebsock != -1) { 29927536ee4bSTim Hardeck qemu_set_fd_handler2(vs->lwebsock, NULL, NULL, NULL, NULL); 29937536ee4bSTim Hardeck close(vs->lwebsock); 29947536ee4bSTim Hardeck vs->lwebsock = -1; 29957536ee4bSTim Hardeck } 29967536ee4bSTim Hardeck #endif /* CONFIG_VNC_WS */ 29973e230dd2SCorentin Chary vs->auth = VNC_AUTH_INVALID; 29983e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS 29993e230dd2SCorentin Chary vs->subauth = VNC_AUTH_INVALID; 30003e230dd2SCorentin Chary vs->tls.x509verify = 0; 30013e230dd2SCorentin Chary #endif 30023e230dd2SCorentin Chary } 30033e230dd2SCorentin Chary 30041cd20f8bSAnthony Liguori int vnc_display_password(DisplayState *ds, const char *password) 30051cd20f8bSAnthony Liguori { 300621ef45d7SGerd Hoffmann VncDisplay *vs = vnc_display; 30071cd20f8bSAnthony Liguori 30081cd20f8bSAnthony Liguori if (!vs) { 3009a6aa9d3eSLuiz Capitulino return -EINVAL; 30101cd20f8bSAnthony Liguori } 3011cf864569SGerd Hoffmann if (vs->auth == VNC_AUTH_NONE) { 3012cf864569SGerd Hoffmann error_printf_unless_qmp("If you want use passwords please enable " 3013cf864569SGerd Hoffmann "password auth using '-vnc ${dpy},password'."); 3014cf864569SGerd Hoffmann return -EINVAL; 30151cd20f8bSAnthony Liguori } 30161cd20f8bSAnthony Liguori 30177267c094SAnthony Liguori g_free(vs->password); 30187267c094SAnthony Liguori vs->password = g_strdup(password); 3019a6aa9d3eSLuiz Capitulino 3020a6aa9d3eSLuiz Capitulino return 0; 30213e230dd2SCorentin Chary } 30223e230dd2SCorentin Chary 30233c9405a0SGerd Hoffmann int vnc_display_pw_expire(DisplayState *ds, time_t expires) 30243c9405a0SGerd Hoffmann { 302521ef45d7SGerd Hoffmann VncDisplay *vs = vnc_display; 30263c9405a0SGerd Hoffmann 30271643f2b2SGerd Hoffmann if (!vs) { 30281643f2b2SGerd Hoffmann return -EINVAL; 30291643f2b2SGerd Hoffmann } 30301643f2b2SGerd Hoffmann 30313c9405a0SGerd Hoffmann vs->expires = expires; 30323c9405a0SGerd Hoffmann return 0; 30333c9405a0SGerd Hoffmann } 30343c9405a0SGerd Hoffmann 30353e230dd2SCorentin Chary char *vnc_display_local_addr(DisplayState *ds) 30363e230dd2SCorentin Chary { 303721ef45d7SGerd Hoffmann VncDisplay *vs = vnc_display; 30383e230dd2SCorentin Chary 30393e230dd2SCorentin Chary return vnc_socket_local_addr("%s:%s", vs->lsock); 30403e230dd2SCorentin Chary } 30413e230dd2SCorentin Chary 30422d55f0e8SPaolo Bonzini void vnc_display_open(DisplayState *ds, const char *display, Error **errp) 30433e230dd2SCorentin Chary { 304421ef45d7SGerd Hoffmann VncDisplay *vs = vnc_display; 30453e230dd2SCorentin Chary const char *options; 30463e230dd2SCorentin Chary int password = 0; 30473e230dd2SCorentin Chary int reverse = 0; 30483e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS 30493e230dd2SCorentin Chary int tls = 0, x509 = 0; 30503e230dd2SCorentin Chary #endif 30513e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL 30523e230dd2SCorentin Chary int sasl = 0; 30533e230dd2SCorentin Chary int saslErr; 30543e230dd2SCorentin Chary #endif 30552ded6ad7SBlue Swirl #if defined(CONFIG_VNC_TLS) || defined(CONFIG_VNC_SASL) 30563e230dd2SCorentin Chary int acl = 0; 30572ded6ad7SBlue Swirl #endif 30583e230dd2SCorentin Chary int lock_key_sync = 1; 30593e230dd2SCorentin Chary 30602d55f0e8SPaolo Bonzini if (!vnc_display) { 30612d55f0e8SPaolo Bonzini error_setg(errp, "VNC display not active"); 30622d55f0e8SPaolo Bonzini return; 30632d55f0e8SPaolo Bonzini } 30643e230dd2SCorentin Chary vnc_display_close(ds); 30653e230dd2SCorentin Chary if (strcmp(display, "none") == 0) 30662d55f0e8SPaolo Bonzini return; 30673e230dd2SCorentin Chary 30681ce52c78SPaolo Bonzini vs->display = g_strdup(display); 30698cf36489SGerd Hoffmann vs->share_policy = VNC_SHARE_POLICY_ALLOW_EXCLUSIVE; 30703e230dd2SCorentin Chary 30713e230dd2SCorentin Chary options = display; 30723e230dd2SCorentin Chary while ((options = strchr(options, ','))) { 30733e230dd2SCorentin Chary options++; 30743e230dd2SCorentin Chary if (strncmp(options, "password", 8) == 0) { 30750f66998fSPaul Moore if (fips_get_state()) { 30762d55f0e8SPaolo Bonzini error_setg(errp, 30770f66998fSPaul Moore "VNC password auth disabled due to FIPS mode, " 30780f66998fSPaul Moore "consider using the VeNCrypt or SASL authentication " 30792d55f0e8SPaolo Bonzini "methods as an alternative"); 30801ce52c78SPaolo Bonzini goto fail; 30810f66998fSPaul Moore } 30823e230dd2SCorentin Chary password = 1; /* Require password auth */ 30833e230dd2SCorentin Chary } else if (strncmp(options, "reverse", 7) == 0) { 30843e230dd2SCorentin Chary reverse = 1; 3085cee8e6adSStefan Hajnoczi } else if (strncmp(options, "no-lock-key-sync", 16) == 0) { 30863e230dd2SCorentin Chary lock_key_sync = 0; 30873e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL 30883e230dd2SCorentin Chary } else if (strncmp(options, "sasl", 4) == 0) { 30893e230dd2SCorentin Chary sasl = 1; /* Require SASL auth */ 30903e230dd2SCorentin Chary #endif 30917536ee4bSTim Hardeck #ifdef CONFIG_VNC_WS 30927536ee4bSTim Hardeck } else if (strncmp(options, "websocket", 9) == 0) { 30937536ee4bSTim Hardeck char *start, *end; 30947536ee4bSTim Hardeck vs->websocket = 1; 30957536ee4bSTim Hardeck 30967536ee4bSTim Hardeck /* Check for 'websocket=<port>' */ 30977536ee4bSTim Hardeck start = strchr(options, '='); 30987536ee4bSTim Hardeck end = strchr(options, ','); 30997536ee4bSTim Hardeck if (start && (!end || (start < end))) { 31007536ee4bSTim Hardeck int len = end ? end-(start+1) : strlen(start+1); 31017536ee4bSTim Hardeck if (len < 6) { 31027536ee4bSTim Hardeck /* extract the host specification from display */ 31037536ee4bSTim Hardeck char *host = NULL, *port = NULL, *host_end = NULL; 31047536ee4bSTim Hardeck port = g_strndup(start + 1, len); 31057536ee4bSTim Hardeck 31067536ee4bSTim Hardeck /* ipv6 hosts have colons */ 31077536ee4bSTim Hardeck end = strchr(display, ','); 31087536ee4bSTim Hardeck host_end = g_strrstr_len(display, end - display, ":"); 31097536ee4bSTim Hardeck 31107536ee4bSTim Hardeck if (host_end) { 31117536ee4bSTim Hardeck host = g_strndup(display, host_end - display + 1); 31127536ee4bSTim Hardeck } else { 31137536ee4bSTim Hardeck host = g_strndup(":", 1); 31147536ee4bSTim Hardeck } 31157536ee4bSTim Hardeck vs->ws_display = g_strconcat(host, port, NULL); 31167536ee4bSTim Hardeck g_free(host); 31177536ee4bSTim Hardeck g_free(port); 31187536ee4bSTim Hardeck } 31197536ee4bSTim Hardeck } 31207536ee4bSTim Hardeck #endif /* CONFIG_VNC_WS */ 31213e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS 31223e230dd2SCorentin Chary } else if (strncmp(options, "tls", 3) == 0) { 31233e230dd2SCorentin Chary tls = 1; /* Require TLS */ 31243e230dd2SCorentin Chary } else if (strncmp(options, "x509", 4) == 0) { 31253e230dd2SCorentin Chary char *start, *end; 31263e230dd2SCorentin Chary x509 = 1; /* Require x509 certificates */ 31273e230dd2SCorentin Chary if (strncmp(options, "x509verify", 10) == 0) 31283e230dd2SCorentin Chary vs->tls.x509verify = 1; /* ...and verify client certs */ 31293e230dd2SCorentin Chary 31303e230dd2SCorentin Chary /* Now check for 'x509=/some/path' postfix 31313e230dd2SCorentin Chary * and use that to setup x509 certificate/key paths */ 31323e230dd2SCorentin Chary start = strchr(options, '='); 31333e230dd2SCorentin Chary end = strchr(options, ','); 31343e230dd2SCorentin Chary if (start && (!end || (start < end))) { 31353e230dd2SCorentin Chary int len = end ? end-(start+1) : strlen(start+1); 31367267c094SAnthony Liguori char *path = g_strndup(start + 1, len); 31373e230dd2SCorentin Chary 31383e230dd2SCorentin Chary VNC_DEBUG("Trying certificate path '%s'\n", path); 31393e230dd2SCorentin Chary if (vnc_tls_set_x509_creds_dir(vs, path) < 0) { 31402d55f0e8SPaolo Bonzini error_setg(errp, "Failed to find x509 certificates/keys in %s", path); 31417267c094SAnthony Liguori g_free(path); 31421ce52c78SPaolo Bonzini goto fail; 31433e230dd2SCorentin Chary } 31447267c094SAnthony Liguori g_free(path); 31453e230dd2SCorentin Chary } else { 31462d55f0e8SPaolo Bonzini error_setg(errp, "No certificate path provided"); 31471ce52c78SPaolo Bonzini goto fail; 31483e230dd2SCorentin Chary } 31493e230dd2SCorentin Chary #endif 31502ded6ad7SBlue Swirl #if defined(CONFIG_VNC_TLS) || defined(CONFIG_VNC_SASL) 31513e230dd2SCorentin Chary } else if (strncmp(options, "acl", 3) == 0) { 31523e230dd2SCorentin Chary acl = 1; 31532ded6ad7SBlue Swirl #endif 31543e230dd2SCorentin Chary } else if (strncmp(options, "lossy", 5) == 0) { 3155e22492d3SPeter Lieven #ifdef CONFIG_VNC_JPEG 31563e230dd2SCorentin Chary vs->lossy = true; 3157e22492d3SPeter Lieven #endif 31587eff5742SCatalin Patulea } else if (strncmp(options, "non-adaptive", 12) == 0) { 315980e0c8c3SCorentin Chary vs->non_adaptive = true; 31608cf36489SGerd Hoffmann } else if (strncmp(options, "share=", 6) == 0) { 31618cf36489SGerd Hoffmann if (strncmp(options+6, "ignore", 6) == 0) { 31628cf36489SGerd Hoffmann vs->share_policy = VNC_SHARE_POLICY_IGNORE; 31638cf36489SGerd Hoffmann } else if (strncmp(options+6, "allow-exclusive", 15) == 0) { 31648cf36489SGerd Hoffmann vs->share_policy = VNC_SHARE_POLICY_ALLOW_EXCLUSIVE; 31658cf36489SGerd Hoffmann } else if (strncmp(options+6, "force-shared", 12) == 0) { 31668cf36489SGerd Hoffmann vs->share_policy = VNC_SHARE_POLICY_FORCE_SHARED; 31678cf36489SGerd Hoffmann } else { 31682d55f0e8SPaolo Bonzini error_setg(errp, "unknown vnc share= option"); 31691ce52c78SPaolo Bonzini goto fail; 31708cf36489SGerd Hoffmann } 31713e230dd2SCorentin Chary } 31723e230dd2SCorentin Chary } 31733e230dd2SCorentin Chary 3174e22492d3SPeter Lieven /* adaptive updates are only used with tight encoding and 3175e22492d3SPeter Lieven * if lossy updates are enabled so we can disable all the 3176e22492d3SPeter Lieven * calculations otherwise */ 3177e22492d3SPeter Lieven if (!vs->lossy) { 3178e22492d3SPeter Lieven vs->non_adaptive = true; 3179e22492d3SPeter Lieven } 3180e22492d3SPeter Lieven 31813e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS 31823e230dd2SCorentin Chary if (acl && x509 && vs->tls.x509verify) { 31833e230dd2SCorentin Chary if (!(vs->tls.acl = qemu_acl_init("vnc.x509dname"))) { 31843e230dd2SCorentin Chary fprintf(stderr, "Failed to create x509 dname ACL\n"); 31853e230dd2SCorentin Chary exit(1); 31863e230dd2SCorentin Chary } 31873e230dd2SCorentin Chary } 31883e230dd2SCorentin Chary #endif 31893e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL 31903e230dd2SCorentin Chary if (acl && sasl) { 31913e230dd2SCorentin Chary if (!(vs->sasl.acl = qemu_acl_init("vnc.username"))) { 31923e230dd2SCorentin Chary fprintf(stderr, "Failed to create username ACL\n"); 31933e230dd2SCorentin Chary exit(1); 31943e230dd2SCorentin Chary } 31953e230dd2SCorentin Chary } 31963e230dd2SCorentin Chary #endif 31973e230dd2SCorentin Chary 31983e230dd2SCorentin Chary /* 31993e230dd2SCorentin Chary * Combinations we support here: 32003e230dd2SCorentin Chary * 32013e230dd2SCorentin Chary * - no-auth (clear text, no auth) 32023e230dd2SCorentin Chary * - password (clear text, weak auth) 32033e230dd2SCorentin Chary * - sasl (encrypt, good auth *IF* using Kerberos via GSSAPI) 32043e230dd2SCorentin Chary * - tls (encrypt, weak anonymous creds, no auth) 32053e230dd2SCorentin Chary * - tls + password (encrypt, weak anonymous creds, weak auth) 32063e230dd2SCorentin Chary * - tls + sasl (encrypt, weak anonymous creds, good auth) 32073e230dd2SCorentin Chary * - tls + x509 (encrypt, good x509 creds, no auth) 32083e230dd2SCorentin Chary * - tls + x509 + password (encrypt, good x509 creds, weak auth) 32093e230dd2SCorentin Chary * - tls + x509 + sasl (encrypt, good x509 creds, good auth) 32103e230dd2SCorentin Chary * 32113e230dd2SCorentin Chary * NB1. TLS is a stackable auth scheme. 32123e230dd2SCorentin Chary * NB2. the x509 schemes have option to validate a client cert dname 32133e230dd2SCorentin Chary */ 32143e230dd2SCorentin Chary if (password) { 32153e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS 32163e230dd2SCorentin Chary if (tls) { 32173e230dd2SCorentin Chary vs->auth = VNC_AUTH_VENCRYPT; 32183e230dd2SCorentin Chary if (x509) { 32193e230dd2SCorentin Chary VNC_DEBUG("Initializing VNC server with x509 password auth\n"); 32203e230dd2SCorentin Chary vs->subauth = VNC_AUTH_VENCRYPT_X509VNC; 32213e230dd2SCorentin Chary } else { 32223e230dd2SCorentin Chary VNC_DEBUG("Initializing VNC server with TLS password auth\n"); 32233e230dd2SCorentin Chary vs->subauth = VNC_AUTH_VENCRYPT_TLSVNC; 32243e230dd2SCorentin Chary } 32253e230dd2SCorentin Chary } else { 32263e230dd2SCorentin Chary #endif /* CONFIG_VNC_TLS */ 32273e230dd2SCorentin Chary VNC_DEBUG("Initializing VNC server with password auth\n"); 32283e230dd2SCorentin Chary vs->auth = VNC_AUTH_VNC; 32293e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS 32303e230dd2SCorentin Chary vs->subauth = VNC_AUTH_INVALID; 32313e230dd2SCorentin Chary } 32323e230dd2SCorentin Chary #endif /* CONFIG_VNC_TLS */ 32333e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL 32343e230dd2SCorentin Chary } else if (sasl) { 32353e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS 32363e230dd2SCorentin Chary if (tls) { 32373e230dd2SCorentin Chary vs->auth = VNC_AUTH_VENCRYPT; 32383e230dd2SCorentin Chary if (x509) { 32393e230dd2SCorentin Chary VNC_DEBUG("Initializing VNC server with x509 SASL auth\n"); 32403e230dd2SCorentin Chary vs->subauth = VNC_AUTH_VENCRYPT_X509SASL; 32413e230dd2SCorentin Chary } else { 32423e230dd2SCorentin Chary VNC_DEBUG("Initializing VNC server with TLS SASL auth\n"); 32433e230dd2SCorentin Chary vs->subauth = VNC_AUTH_VENCRYPT_TLSSASL; 32443e230dd2SCorentin Chary } 32453e230dd2SCorentin Chary } else { 32463e230dd2SCorentin Chary #endif /* CONFIG_VNC_TLS */ 32473e230dd2SCorentin Chary VNC_DEBUG("Initializing VNC server with SASL auth\n"); 32483e230dd2SCorentin Chary vs->auth = VNC_AUTH_SASL; 32493e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS 32503e230dd2SCorentin Chary vs->subauth = VNC_AUTH_INVALID; 32513e230dd2SCorentin Chary } 32523e230dd2SCorentin Chary #endif /* CONFIG_VNC_TLS */ 32533e230dd2SCorentin Chary #endif /* CONFIG_VNC_SASL */ 32543e230dd2SCorentin Chary } else { 32553e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS 32563e230dd2SCorentin Chary if (tls) { 32573e230dd2SCorentin Chary vs->auth = VNC_AUTH_VENCRYPT; 32583e230dd2SCorentin Chary if (x509) { 32593e230dd2SCorentin Chary VNC_DEBUG("Initializing VNC server with x509 no auth\n"); 32603e230dd2SCorentin Chary vs->subauth = VNC_AUTH_VENCRYPT_X509NONE; 32613e230dd2SCorentin Chary } else { 32623e230dd2SCorentin Chary VNC_DEBUG("Initializing VNC server with TLS no auth\n"); 32633e230dd2SCorentin Chary vs->subauth = VNC_AUTH_VENCRYPT_TLSNONE; 32643e230dd2SCorentin Chary } 32653e230dd2SCorentin Chary } else { 32663e230dd2SCorentin Chary #endif 32673e230dd2SCorentin Chary VNC_DEBUG("Initializing VNC server with no auth\n"); 32683e230dd2SCorentin Chary vs->auth = VNC_AUTH_NONE; 32693e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS 32703e230dd2SCorentin Chary vs->subauth = VNC_AUTH_INVALID; 32713e230dd2SCorentin Chary } 32723e230dd2SCorentin Chary #endif 32733e230dd2SCorentin Chary } 32743e230dd2SCorentin Chary 32753e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL 32763e230dd2SCorentin Chary if ((saslErr = sasl_server_init(NULL, "qemu")) != SASL_OK) { 32772d55f0e8SPaolo Bonzini error_setg(errp, "Failed to initialize SASL auth: %s", 32783e230dd2SCorentin Chary sasl_errstring(saslErr, NULL, NULL)); 32791ce52c78SPaolo Bonzini goto fail; 32803e230dd2SCorentin Chary } 32813e230dd2SCorentin Chary #endif 32823e230dd2SCorentin Chary vs->lock_key_sync = lock_key_sync; 32833e230dd2SCorentin Chary 32843e230dd2SCorentin Chary if (reverse) { 32853e230dd2SCorentin Chary /* connect to viewer */ 3286007fcd3eSPaolo Bonzini int csock; 32873e230dd2SCorentin Chary vs->lsock = -1; 32887536ee4bSTim Hardeck #ifdef CONFIG_VNC_WS 32897536ee4bSTim Hardeck vs->lwebsock = -1; 32907536ee4bSTim Hardeck #endif 3291007fcd3eSPaolo Bonzini if (strncmp(display, "unix:", 5) == 0) { 32922d55f0e8SPaolo Bonzini csock = unix_connect(display+5, errp); 3293007fcd3eSPaolo Bonzini } else { 32942d55f0e8SPaolo Bonzini csock = inet_connect(display, errp); 32953e230dd2SCorentin Chary } 3296007fcd3eSPaolo Bonzini if (csock < 0) { 3297007fcd3eSPaolo Bonzini goto fail; 3298007fcd3eSPaolo Bonzini } 32992c8cf549SMichael Tokarev vnc_connect(vs, csock, false, false); 33003e230dd2SCorentin Chary } else { 33013e230dd2SCorentin Chary /* listen for connects */ 33023e230dd2SCorentin Chary char *dpy; 33037267c094SAnthony Liguori dpy = g_malloc(256); 33043e230dd2SCorentin Chary if (strncmp(display, "unix:", 5) == 0) { 33053e230dd2SCorentin Chary pstrcpy(dpy, 256, "unix:"); 33062d55f0e8SPaolo Bonzini vs->lsock = unix_listen(display+5, dpy+5, 256-5, errp); 33073e230dd2SCorentin Chary } else { 3308029409e5SAmos Kong vs->lsock = inet_listen(display, dpy, 256, 33092d55f0e8SPaolo Bonzini SOCK_STREAM, 5900, errp); 3310c1c1619cSPaolo Bonzini if (vs->lsock < 0) { 3311ae878b17SStefan Weil g_free(dpy); 33121ce52c78SPaolo Bonzini goto fail; 33131ce52c78SPaolo Bonzini } 33147536ee4bSTim Hardeck #ifdef CONFIG_VNC_WS 33157536ee4bSTim Hardeck if (vs->websocket) { 33167536ee4bSTim Hardeck if (vs->ws_display) { 33177536ee4bSTim Hardeck vs->lwebsock = inet_listen(vs->ws_display, NULL, 256, 33187536ee4bSTim Hardeck SOCK_STREAM, 0, errp); 33197536ee4bSTim Hardeck } else { 33207536ee4bSTim Hardeck vs->lwebsock = inet_listen(vs->display, NULL, 256, 33217536ee4bSTim Hardeck SOCK_STREAM, 5700, errp); 33227536ee4bSTim Hardeck } 33237536ee4bSTim Hardeck 33247536ee4bSTim Hardeck if (vs->lwebsock < 0) { 33257536ee4bSTim Hardeck if (vs->lsock) { 33267536ee4bSTim Hardeck close(vs->lsock); 33277536ee4bSTim Hardeck vs->lsock = -1; 33287536ee4bSTim Hardeck } 33297536ee4bSTim Hardeck g_free(dpy); 33307536ee4bSTim Hardeck goto fail; 33317536ee4bSTim Hardeck } 33327536ee4bSTim Hardeck } 33337536ee4bSTim Hardeck #endif /* CONFIG_VNC_WS */ 33347536ee4bSTim Hardeck } 3335ae878b17SStefan Weil g_free(vs->display); 33363e230dd2SCorentin Chary vs->display = dpy; 33377536ee4bSTim Hardeck qemu_set_fd_handler2(vs->lsock, NULL, 33387536ee4bSTim Hardeck vnc_listen_regular_read, NULL, vs); 33397536ee4bSTim Hardeck #ifdef CONFIG_VNC_WS 33407536ee4bSTim Hardeck if (vs->websocket) { 33417536ee4bSTim Hardeck qemu_set_fd_handler2(vs->lwebsock, NULL, 33427536ee4bSTim Hardeck vnc_listen_websocket_read, NULL, vs); 33437536ee4bSTim Hardeck } 33447536ee4bSTim Hardeck #endif /* CONFIG_VNC_WS */ 33453e230dd2SCorentin Chary } 33462d55f0e8SPaolo Bonzini return; 33471ce52c78SPaolo Bonzini 33481ce52c78SPaolo Bonzini fail: 33491ce52c78SPaolo Bonzini g_free(vs->display); 33501ce52c78SPaolo Bonzini vs->display = NULL; 33517536ee4bSTim Hardeck #ifdef CONFIG_VNC_WS 33527536ee4bSTim Hardeck g_free(vs->ws_display); 33537536ee4bSTim Hardeck vs->ws_display = NULL; 33547536ee4bSTim Hardeck #endif /* CONFIG_VNC_WS */ 33553e230dd2SCorentin Chary } 335613661089SDaniel P. Berrange 33572c8cf549SMichael Tokarev void vnc_display_add_client(DisplayState *ds, int csock, bool skipauth) 335813661089SDaniel P. Berrange { 335921ef45d7SGerd Hoffmann VncDisplay *vs = vnc_display; 336013661089SDaniel P. Berrange 33612c8cf549SMichael Tokarev vnc_connect(vs, csock, skipauth, false); 336213661089SDaniel P. Berrange } 3363