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" 293e230dd2SCorentin Chary #include "sysemu.h" 303e230dd2SCorentin Chary #include "qemu_socket.h" 313e230dd2SCorentin Chary #include "qemu-timer.h" 323e230dd2SCorentin Chary #include "acl.h" 333e230dd2SCorentin Chary #include "qemu-objects.h" 343e230dd2SCorentin Chary 353e230dd2SCorentin Chary #define VNC_REFRESH_INTERVAL_BASE 30 363e230dd2SCorentin Chary #define VNC_REFRESH_INTERVAL_INC 50 373e230dd2SCorentin Chary #define VNC_REFRESH_INTERVAL_MAX 2000 383e230dd2SCorentin Chary 393e230dd2SCorentin Chary #include "vnc_keysym.h" 403e230dd2SCorentin Chary #include "d3des.h" 413e230dd2SCorentin Chary 423e230dd2SCorentin Chary #define count_bits(c, v) { \ 433e230dd2SCorentin Chary for (c = 0; v; v >>= 1) \ 443e230dd2SCorentin Chary { \ 453e230dd2SCorentin Chary c += v & 1; \ 463e230dd2SCorentin Chary } \ 473e230dd2SCorentin Chary } 483e230dd2SCorentin Chary 493e230dd2SCorentin Chary static VncDisplay *vnc_display; /* needed for info vnc */ 503e230dd2SCorentin Chary static DisplayChangeListener *dcl; 513e230dd2SCorentin Chary 523e230dd2SCorentin Chary static int vnc_cursor_define(VncState *vs); 533e230dd2SCorentin Chary 543e230dd2SCorentin Chary static char *addr_to_string(const char *format, 553e230dd2SCorentin Chary struct sockaddr_storage *sa, 563e230dd2SCorentin Chary socklen_t salen) { 573e230dd2SCorentin Chary char *addr; 583e230dd2SCorentin Chary char host[NI_MAXHOST]; 593e230dd2SCorentin Chary char serv[NI_MAXSERV]; 603e230dd2SCorentin Chary int err; 613e230dd2SCorentin Chary size_t addrlen; 623e230dd2SCorentin Chary 633e230dd2SCorentin Chary if ((err = getnameinfo((struct sockaddr *)sa, salen, 643e230dd2SCorentin Chary host, sizeof(host), 653e230dd2SCorentin Chary serv, sizeof(serv), 663e230dd2SCorentin Chary NI_NUMERICHOST | NI_NUMERICSERV)) != 0) { 673e230dd2SCorentin Chary VNC_DEBUG("Cannot resolve address %d: %s\n", 683e230dd2SCorentin Chary err, gai_strerror(err)); 693e230dd2SCorentin Chary return NULL; 703e230dd2SCorentin Chary } 713e230dd2SCorentin Chary 723e230dd2SCorentin Chary /* Enough for the existing format + the 2 vars we're 733e230dd2SCorentin Chary * substituting in. */ 743e230dd2SCorentin Chary addrlen = strlen(format) + strlen(host) + strlen(serv); 753e230dd2SCorentin Chary addr = qemu_malloc(addrlen + 1); 763e230dd2SCorentin Chary snprintf(addr, addrlen, format, host, serv); 773e230dd2SCorentin Chary addr[addrlen] = '\0'; 783e230dd2SCorentin Chary 793e230dd2SCorentin Chary return addr; 803e230dd2SCorentin Chary } 813e230dd2SCorentin Chary 823e230dd2SCorentin Chary 833e230dd2SCorentin Chary char *vnc_socket_local_addr(const char *format, int fd) { 843e230dd2SCorentin Chary struct sockaddr_storage sa; 853e230dd2SCorentin Chary socklen_t salen; 863e230dd2SCorentin Chary 873e230dd2SCorentin Chary salen = sizeof(sa); 883e230dd2SCorentin Chary if (getsockname(fd, (struct sockaddr*)&sa, &salen) < 0) 893e230dd2SCorentin Chary return NULL; 903e230dd2SCorentin Chary 913e230dd2SCorentin Chary return addr_to_string(format, &sa, salen); 923e230dd2SCorentin Chary } 933e230dd2SCorentin Chary 943e230dd2SCorentin Chary char *vnc_socket_remote_addr(const char *format, int fd) { 953e230dd2SCorentin Chary struct sockaddr_storage sa; 963e230dd2SCorentin Chary socklen_t salen; 973e230dd2SCorentin Chary 983e230dd2SCorentin Chary salen = sizeof(sa); 993e230dd2SCorentin Chary if (getpeername(fd, (struct sockaddr*)&sa, &salen) < 0) 1003e230dd2SCorentin Chary return NULL; 1013e230dd2SCorentin Chary 1023e230dd2SCorentin Chary return addr_to_string(format, &sa, salen); 1033e230dd2SCorentin Chary } 1043e230dd2SCorentin Chary 1053e230dd2SCorentin Chary static int put_addr_qdict(QDict *qdict, struct sockaddr_storage *sa, 1063e230dd2SCorentin Chary socklen_t salen) 1073e230dd2SCorentin Chary { 1083e230dd2SCorentin Chary char host[NI_MAXHOST]; 1093e230dd2SCorentin Chary char serv[NI_MAXSERV]; 1103e230dd2SCorentin Chary int err; 1113e230dd2SCorentin Chary 1123e230dd2SCorentin Chary if ((err = getnameinfo((struct sockaddr *)sa, salen, 1133e230dd2SCorentin Chary host, sizeof(host), 1143e230dd2SCorentin Chary serv, sizeof(serv), 1153e230dd2SCorentin Chary NI_NUMERICHOST | NI_NUMERICSERV)) != 0) { 1163e230dd2SCorentin Chary VNC_DEBUG("Cannot resolve address %d: %s\n", 1173e230dd2SCorentin Chary err, gai_strerror(err)); 1183e230dd2SCorentin Chary return -1; 1193e230dd2SCorentin Chary } 1203e230dd2SCorentin Chary 1213e230dd2SCorentin Chary qdict_put(qdict, "host", qstring_from_str(host)); 1223e230dd2SCorentin Chary qdict_put(qdict, "service", qstring_from_str(serv)); 1233e230dd2SCorentin Chary qdict_put(qdict, "family",qstring_from_str(inet_strfamily(sa->ss_family))); 1243e230dd2SCorentin Chary 1253e230dd2SCorentin Chary return 0; 1263e230dd2SCorentin Chary } 1273e230dd2SCorentin Chary 1283e230dd2SCorentin Chary static int vnc_server_addr_put(QDict *qdict, int fd) 1293e230dd2SCorentin Chary { 1303e230dd2SCorentin Chary struct sockaddr_storage sa; 1313e230dd2SCorentin Chary socklen_t salen; 1323e230dd2SCorentin Chary 1333e230dd2SCorentin Chary salen = sizeof(sa); 1343e230dd2SCorentin Chary if (getsockname(fd, (struct sockaddr*)&sa, &salen) < 0) { 1353e230dd2SCorentin Chary return -1; 1363e230dd2SCorentin Chary } 1373e230dd2SCorentin Chary 1383e230dd2SCorentin Chary return put_addr_qdict(qdict, &sa, salen); 1393e230dd2SCorentin Chary } 1403e230dd2SCorentin Chary 1413e230dd2SCorentin Chary static int vnc_qdict_remote_addr(QDict *qdict, int fd) 1423e230dd2SCorentin Chary { 1433e230dd2SCorentin Chary struct sockaddr_storage sa; 1443e230dd2SCorentin Chary socklen_t salen; 1453e230dd2SCorentin Chary 1463e230dd2SCorentin Chary salen = sizeof(sa); 1473e230dd2SCorentin Chary if (getpeername(fd, (struct sockaddr*)&sa, &salen) < 0) { 1483e230dd2SCorentin Chary return -1; 1493e230dd2SCorentin Chary } 1503e230dd2SCorentin Chary 1513e230dd2SCorentin Chary return put_addr_qdict(qdict, &sa, salen); 1523e230dd2SCorentin Chary } 1533e230dd2SCorentin Chary 1543e230dd2SCorentin Chary static const char *vnc_auth_name(VncDisplay *vd) { 1553e230dd2SCorentin Chary switch (vd->auth) { 1563e230dd2SCorentin Chary case VNC_AUTH_INVALID: 1573e230dd2SCorentin Chary return "invalid"; 1583e230dd2SCorentin Chary case VNC_AUTH_NONE: 1593e230dd2SCorentin Chary return "none"; 1603e230dd2SCorentin Chary case VNC_AUTH_VNC: 1613e230dd2SCorentin Chary return "vnc"; 1623e230dd2SCorentin Chary case VNC_AUTH_RA2: 1633e230dd2SCorentin Chary return "ra2"; 1643e230dd2SCorentin Chary case VNC_AUTH_RA2NE: 1653e230dd2SCorentin Chary return "ra2ne"; 1663e230dd2SCorentin Chary case VNC_AUTH_TIGHT: 1673e230dd2SCorentin Chary return "tight"; 1683e230dd2SCorentin Chary case VNC_AUTH_ULTRA: 1693e230dd2SCorentin Chary return "ultra"; 1703e230dd2SCorentin Chary case VNC_AUTH_TLS: 1713e230dd2SCorentin Chary return "tls"; 1723e230dd2SCorentin Chary case VNC_AUTH_VENCRYPT: 1733e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS 1743e230dd2SCorentin Chary switch (vd->subauth) { 1753e230dd2SCorentin Chary case VNC_AUTH_VENCRYPT_PLAIN: 1763e230dd2SCorentin Chary return "vencrypt+plain"; 1773e230dd2SCorentin Chary case VNC_AUTH_VENCRYPT_TLSNONE: 1783e230dd2SCorentin Chary return "vencrypt+tls+none"; 1793e230dd2SCorentin Chary case VNC_AUTH_VENCRYPT_TLSVNC: 1803e230dd2SCorentin Chary return "vencrypt+tls+vnc"; 1813e230dd2SCorentin Chary case VNC_AUTH_VENCRYPT_TLSPLAIN: 1823e230dd2SCorentin Chary return "vencrypt+tls+plain"; 1833e230dd2SCorentin Chary case VNC_AUTH_VENCRYPT_X509NONE: 1843e230dd2SCorentin Chary return "vencrypt+x509+none"; 1853e230dd2SCorentin Chary case VNC_AUTH_VENCRYPT_X509VNC: 1863e230dd2SCorentin Chary return "vencrypt+x509+vnc"; 1873e230dd2SCorentin Chary case VNC_AUTH_VENCRYPT_X509PLAIN: 1883e230dd2SCorentin Chary return "vencrypt+x509+plain"; 1893e230dd2SCorentin Chary case VNC_AUTH_VENCRYPT_TLSSASL: 1903e230dd2SCorentin Chary return "vencrypt+tls+sasl"; 1913e230dd2SCorentin Chary case VNC_AUTH_VENCRYPT_X509SASL: 1923e230dd2SCorentin Chary return "vencrypt+x509+sasl"; 1933e230dd2SCorentin Chary default: 1943e230dd2SCorentin Chary return "vencrypt"; 1953e230dd2SCorentin Chary } 1963e230dd2SCorentin Chary #else 1973e230dd2SCorentin Chary return "vencrypt"; 1983e230dd2SCorentin Chary #endif 1993e230dd2SCorentin Chary case VNC_AUTH_SASL: 2003e230dd2SCorentin Chary return "sasl"; 2013e230dd2SCorentin Chary } 2023e230dd2SCorentin Chary return "unknown"; 2033e230dd2SCorentin Chary } 2043e230dd2SCorentin Chary 2053e230dd2SCorentin Chary static int vnc_server_info_put(QDict *qdict) 2063e230dd2SCorentin Chary { 2073e230dd2SCorentin Chary if (vnc_server_addr_put(qdict, vnc_display->lsock) < 0) { 2083e230dd2SCorentin Chary return -1; 2093e230dd2SCorentin Chary } 2103e230dd2SCorentin Chary 2113e230dd2SCorentin Chary qdict_put(qdict, "auth", qstring_from_str(vnc_auth_name(vnc_display))); 2123e230dd2SCorentin Chary return 0; 2133e230dd2SCorentin Chary } 2143e230dd2SCorentin Chary 2153e230dd2SCorentin Chary static void vnc_client_cache_auth(VncState *client) 2163e230dd2SCorentin Chary { 2173e230dd2SCorentin Chary QDict *qdict; 2183e230dd2SCorentin Chary 2193e230dd2SCorentin Chary if (!client->info) { 2203e230dd2SCorentin Chary return; 2213e230dd2SCorentin Chary } 2223e230dd2SCorentin Chary 2233e230dd2SCorentin Chary qdict = qobject_to_qdict(client->info); 2243e230dd2SCorentin Chary 2253e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS 2263e230dd2SCorentin Chary if (client->tls.session && 2273e230dd2SCorentin Chary client->tls.dname) { 2283e230dd2SCorentin Chary qdict_put(qdict, "x509_dname", qstring_from_str(client->tls.dname)); 2293e230dd2SCorentin Chary } 2303e230dd2SCorentin Chary #endif 2313e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL 2323e230dd2SCorentin Chary if (client->sasl.conn && 2333e230dd2SCorentin Chary client->sasl.username) { 2343e230dd2SCorentin Chary qdict_put(qdict, "sasl_username", 2353e230dd2SCorentin Chary qstring_from_str(client->sasl.username)); 2363e230dd2SCorentin Chary } 2373e230dd2SCorentin Chary #endif 2383e230dd2SCorentin Chary } 2393e230dd2SCorentin Chary 2403e230dd2SCorentin Chary static void vnc_client_cache_addr(VncState *client) 2413e230dd2SCorentin Chary { 2423e230dd2SCorentin Chary QDict *qdict; 2433e230dd2SCorentin Chary 2443e230dd2SCorentin Chary qdict = qdict_new(); 2453e230dd2SCorentin Chary if (vnc_qdict_remote_addr(qdict, client->csock) < 0) { 2463e230dd2SCorentin Chary QDECREF(qdict); 2473e230dd2SCorentin Chary /* XXX: how to report the error? */ 2483e230dd2SCorentin Chary return; 2493e230dd2SCorentin Chary } 2503e230dd2SCorentin Chary 2513e230dd2SCorentin Chary client->info = QOBJECT(qdict); 2523e230dd2SCorentin Chary } 2533e230dd2SCorentin Chary 2543e230dd2SCorentin Chary static void vnc_qmp_event(VncState *vs, MonitorEvent event) 2553e230dd2SCorentin Chary { 2563e230dd2SCorentin Chary QDict *server; 2573e230dd2SCorentin Chary QObject *data; 2583e230dd2SCorentin Chary 2593e230dd2SCorentin Chary if (!vs->info) { 2603e230dd2SCorentin Chary return; 2613e230dd2SCorentin Chary } 2623e230dd2SCorentin Chary 2633e230dd2SCorentin Chary server = qdict_new(); 2643e230dd2SCorentin Chary if (vnc_server_info_put(server) < 0) { 2653e230dd2SCorentin Chary QDECREF(server); 2663e230dd2SCorentin Chary return; 2673e230dd2SCorentin Chary } 2683e230dd2SCorentin Chary 2693e230dd2SCorentin Chary data = qobject_from_jsonf("{ 'client': %p, 'server': %p }", 2703e230dd2SCorentin Chary vs->info, QOBJECT(server)); 2713e230dd2SCorentin Chary 2723e230dd2SCorentin Chary monitor_protocol_event(event, data); 2733e230dd2SCorentin Chary 2743e230dd2SCorentin Chary qobject_incref(vs->info); 2753e230dd2SCorentin Chary qobject_decref(data); 2763e230dd2SCorentin Chary } 2773e230dd2SCorentin Chary 2783e230dd2SCorentin Chary static void info_vnc_iter(QObject *obj, void *opaque) 2793e230dd2SCorentin Chary { 2803e230dd2SCorentin Chary QDict *client; 2813e230dd2SCorentin Chary Monitor *mon = opaque; 2823e230dd2SCorentin Chary 2833e230dd2SCorentin Chary client = qobject_to_qdict(obj); 2843e230dd2SCorentin Chary monitor_printf(mon, "Client:\n"); 2853e230dd2SCorentin Chary monitor_printf(mon, " address: %s:%s\n", 2863e230dd2SCorentin Chary qdict_get_str(client, "host"), 2873e230dd2SCorentin Chary qdict_get_str(client, "service")); 2883e230dd2SCorentin Chary 2893e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS 2903e230dd2SCorentin Chary monitor_printf(mon, " x509_dname: %s\n", 2913e230dd2SCorentin Chary qdict_haskey(client, "x509_dname") ? 2923e230dd2SCorentin Chary qdict_get_str(client, "x509_dname") : "none"); 2933e230dd2SCorentin Chary #endif 2943e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL 2953e230dd2SCorentin Chary monitor_printf(mon, " username: %s\n", 2963e230dd2SCorentin Chary qdict_haskey(client, "sasl_username") ? 2973e230dd2SCorentin Chary qdict_get_str(client, "sasl_username") : "none"); 2983e230dd2SCorentin Chary #endif 2993e230dd2SCorentin Chary } 3003e230dd2SCorentin Chary 3013e230dd2SCorentin Chary void do_info_vnc_print(Monitor *mon, const QObject *data) 3023e230dd2SCorentin Chary { 3033e230dd2SCorentin Chary QDict *server; 3043e230dd2SCorentin Chary QList *clients; 3053e230dd2SCorentin Chary 3063e230dd2SCorentin Chary server = qobject_to_qdict(data); 3073e230dd2SCorentin Chary if (qdict_get_bool(server, "enabled") == 0) { 3083e230dd2SCorentin Chary monitor_printf(mon, "Server: disabled\n"); 3093e230dd2SCorentin Chary return; 3103e230dd2SCorentin Chary } 3113e230dd2SCorentin Chary 3123e230dd2SCorentin Chary monitor_printf(mon, "Server:\n"); 3133e230dd2SCorentin Chary monitor_printf(mon, " address: %s:%s\n", 3143e230dd2SCorentin Chary qdict_get_str(server, "host"), 3153e230dd2SCorentin Chary qdict_get_str(server, "service")); 3163e230dd2SCorentin Chary monitor_printf(mon, " auth: %s\n", qdict_get_str(server, "auth")); 3173e230dd2SCorentin Chary 3183e230dd2SCorentin Chary clients = qdict_get_qlist(server, "clients"); 3193e230dd2SCorentin Chary if (qlist_empty(clients)) { 3203e230dd2SCorentin Chary monitor_printf(mon, "Client: none\n"); 3213e230dd2SCorentin Chary } else { 3223e230dd2SCorentin Chary qlist_iter(clients, info_vnc_iter, mon); 3233e230dd2SCorentin Chary } 3243e230dd2SCorentin Chary } 3253e230dd2SCorentin Chary 3263e230dd2SCorentin Chary void do_info_vnc(Monitor *mon, QObject **ret_data) 3273e230dd2SCorentin Chary { 3283e230dd2SCorentin Chary if (vnc_display == NULL || vnc_display->display == NULL) { 3293e230dd2SCorentin Chary *ret_data = qobject_from_jsonf("{ 'enabled': false }"); 3303e230dd2SCorentin Chary } else { 3313e230dd2SCorentin Chary QList *clist; 3323e230dd2SCorentin Chary VncState *client; 3333e230dd2SCorentin Chary 3343e230dd2SCorentin Chary clist = qlist_new(); 3353e230dd2SCorentin Chary QTAILQ_FOREACH(client, &vnc_display->clients, next) { 3363e230dd2SCorentin Chary if (client->info) { 3373e230dd2SCorentin Chary /* incref so that it's not freed by upper layers */ 3383e230dd2SCorentin Chary qobject_incref(client->info); 3393e230dd2SCorentin Chary qlist_append_obj(clist, client->info); 3403e230dd2SCorentin Chary } 3413e230dd2SCorentin Chary } 3423e230dd2SCorentin Chary 3433e230dd2SCorentin Chary *ret_data = qobject_from_jsonf("{ 'enabled': true, 'clients': %p }", 3443e230dd2SCorentin Chary QOBJECT(clist)); 3453e230dd2SCorentin Chary assert(*ret_data != NULL); 3463e230dd2SCorentin Chary 3473e230dd2SCorentin Chary if (vnc_server_info_put(qobject_to_qdict(*ret_data)) < 0) { 3483e230dd2SCorentin Chary qobject_decref(*ret_data); 3493e230dd2SCorentin Chary *ret_data = NULL; 3503e230dd2SCorentin Chary } 3513e230dd2SCorentin Chary } 3523e230dd2SCorentin Chary } 3533e230dd2SCorentin Chary 3543e230dd2SCorentin Chary /* TODO 3553e230dd2SCorentin Chary 1) Get the queue working for IO. 3563e230dd2SCorentin Chary 2) there is some weirdness when using the -S option (the screen is grey 3573e230dd2SCorentin Chary and not totally invalidated 3583e230dd2SCorentin Chary 3) resolutions > 1024 3593e230dd2SCorentin Chary */ 3603e230dd2SCorentin Chary 3613e230dd2SCorentin Chary static int vnc_update_client(VncState *vs, int has_dirty); 362bd023f95SCorentin Chary static int vnc_update_client_sync(VncState *vs, int has_dirty); 3633e230dd2SCorentin Chary static void vnc_disconnect_start(VncState *vs); 3643e230dd2SCorentin Chary static void vnc_disconnect_finish(VncState *vs); 3653e230dd2SCorentin Chary static void vnc_init_timer(VncDisplay *vd); 3663e230dd2SCorentin Chary static void vnc_remove_timer(VncDisplay *vd); 3673e230dd2SCorentin Chary 3683e230dd2SCorentin Chary static void vnc_colordepth(VncState *vs); 3693e230dd2SCorentin Chary static void framebuffer_update_request(VncState *vs, int incremental, 3703e230dd2SCorentin Chary int x_position, int y_position, 3713e230dd2SCorentin Chary int w, int h); 3723e230dd2SCorentin Chary static void vnc_refresh(void *opaque); 3733e230dd2SCorentin Chary static int vnc_refresh_server_surface(VncDisplay *vd); 3743e230dd2SCorentin Chary 3753e230dd2SCorentin Chary static inline void vnc_set_bit(uint32_t *d, int k) 3763e230dd2SCorentin Chary { 3773e230dd2SCorentin Chary d[k >> 5] |= 1 << (k & 0x1f); 3783e230dd2SCorentin Chary } 3793e230dd2SCorentin Chary 3803e230dd2SCorentin Chary static inline void vnc_clear_bit(uint32_t *d, int k) 3813e230dd2SCorentin Chary { 3823e230dd2SCorentin Chary d[k >> 5] &= ~(1 << (k & 0x1f)); 3833e230dd2SCorentin Chary } 3843e230dd2SCorentin Chary 3853e230dd2SCorentin Chary static inline void vnc_set_bits(uint32_t *d, int n, int nb_words) 3863e230dd2SCorentin Chary { 3873e230dd2SCorentin Chary int j; 3883e230dd2SCorentin Chary 3893e230dd2SCorentin Chary j = 0; 3903e230dd2SCorentin Chary while (n >= 32) { 3913e230dd2SCorentin Chary d[j++] = -1; 3923e230dd2SCorentin Chary n -= 32; 3933e230dd2SCorentin Chary } 3943e230dd2SCorentin Chary if (n > 0) 3953e230dd2SCorentin Chary d[j++] = (1 << n) - 1; 3963e230dd2SCorentin Chary while (j < nb_words) 3973e230dd2SCorentin Chary d[j++] = 0; 3983e230dd2SCorentin Chary } 3993e230dd2SCorentin Chary 4003e230dd2SCorentin Chary static inline int vnc_get_bit(const uint32_t *d, int k) 4013e230dd2SCorentin Chary { 4023e230dd2SCorentin Chary return (d[k >> 5] >> (k & 0x1f)) & 1; 4033e230dd2SCorentin Chary } 4043e230dd2SCorentin Chary 4053e230dd2SCorentin Chary static inline int vnc_and_bits(const uint32_t *d1, const uint32_t *d2, 4063e230dd2SCorentin Chary int nb_words) 4073e230dd2SCorentin Chary { 4083e230dd2SCorentin Chary int i; 4093e230dd2SCorentin Chary for(i = 0; i < nb_words; i++) { 4103e230dd2SCorentin Chary if ((d1[i] & d2[i]) != 0) 4113e230dd2SCorentin Chary return 1; 4123e230dd2SCorentin Chary } 4133e230dd2SCorentin Chary return 0; 4143e230dd2SCorentin Chary } 4153e230dd2SCorentin Chary 4163e230dd2SCorentin Chary static void vnc_dpy_update(DisplayState *ds, int x, int y, int w, int h) 4173e230dd2SCorentin Chary { 4183e230dd2SCorentin Chary int i; 4193e230dd2SCorentin Chary VncDisplay *vd = ds->opaque; 4203e230dd2SCorentin Chary struct VncSurface *s = &vd->guest; 4213e230dd2SCorentin Chary 4223e230dd2SCorentin Chary h += y; 4233e230dd2SCorentin Chary 4243e230dd2SCorentin Chary /* round x down to ensure the loop only spans one 16-pixel block per, 4253e230dd2SCorentin Chary iteration. otherwise, if (x % 16) != 0, the last iteration may span 4263e230dd2SCorentin Chary two 16-pixel blocks but we only mark the first as dirty 4273e230dd2SCorentin Chary */ 4283e230dd2SCorentin Chary w += (x % 16); 4293e230dd2SCorentin Chary x -= (x % 16); 4303e230dd2SCorentin Chary 4313e230dd2SCorentin Chary x = MIN(x, s->ds->width); 4323e230dd2SCorentin Chary y = MIN(y, s->ds->height); 4333e230dd2SCorentin Chary w = MIN(x + w, s->ds->width) - x; 4343e230dd2SCorentin Chary h = MIN(h, s->ds->height); 4353e230dd2SCorentin Chary 4363e230dd2SCorentin Chary for (; y < h; y++) 4373e230dd2SCorentin Chary for (i = 0; i < w; i += 16) 4383e230dd2SCorentin Chary vnc_set_bit(s->dirty[y], (x + i) / 16); 4393e230dd2SCorentin Chary } 4403e230dd2SCorentin Chary 4413e230dd2SCorentin Chary void vnc_framebuffer_update(VncState *vs, int x, int y, int w, int h, 4423e230dd2SCorentin Chary int32_t encoding) 4433e230dd2SCorentin Chary { 4443e230dd2SCorentin Chary vnc_write_u16(vs, x); 4453e230dd2SCorentin Chary vnc_write_u16(vs, y); 4463e230dd2SCorentin Chary vnc_write_u16(vs, w); 4473e230dd2SCorentin Chary vnc_write_u16(vs, h); 4483e230dd2SCorentin Chary 4493e230dd2SCorentin Chary vnc_write_s32(vs, encoding); 4503e230dd2SCorentin Chary } 4513e230dd2SCorentin Chary 4523e230dd2SCorentin Chary void buffer_reserve(Buffer *buffer, size_t len) 4533e230dd2SCorentin Chary { 4543e230dd2SCorentin Chary if ((buffer->capacity - buffer->offset) < len) { 4553e230dd2SCorentin Chary buffer->capacity += (len + 1024); 4563e230dd2SCorentin Chary buffer->buffer = qemu_realloc(buffer->buffer, buffer->capacity); 4573e230dd2SCorentin Chary if (buffer->buffer == NULL) { 4583e230dd2SCorentin Chary fprintf(stderr, "vnc: out of memory\n"); 4593e230dd2SCorentin Chary exit(1); 4603e230dd2SCorentin Chary } 4613e230dd2SCorentin Chary } 4623e230dd2SCorentin Chary } 4633e230dd2SCorentin Chary 4643e230dd2SCorentin Chary int buffer_empty(Buffer *buffer) 4653e230dd2SCorentin Chary { 4663e230dd2SCorentin Chary return buffer->offset == 0; 4673e230dd2SCorentin Chary } 4683e230dd2SCorentin Chary 4693e230dd2SCorentin Chary uint8_t *buffer_end(Buffer *buffer) 4703e230dd2SCorentin Chary { 4713e230dd2SCorentin Chary return buffer->buffer + buffer->offset; 4723e230dd2SCorentin Chary } 4733e230dd2SCorentin Chary 4743e230dd2SCorentin Chary void buffer_reset(Buffer *buffer) 4753e230dd2SCorentin Chary { 4763e230dd2SCorentin Chary buffer->offset = 0; 4773e230dd2SCorentin Chary } 4783e230dd2SCorentin Chary 4793e230dd2SCorentin Chary void buffer_free(Buffer *buffer) 4803e230dd2SCorentin Chary { 4813e230dd2SCorentin Chary qemu_free(buffer->buffer); 4823e230dd2SCorentin Chary buffer->offset = 0; 4833e230dd2SCorentin Chary buffer->capacity = 0; 4843e230dd2SCorentin Chary buffer->buffer = NULL; 4853e230dd2SCorentin Chary } 4863e230dd2SCorentin Chary 4873e230dd2SCorentin Chary void buffer_append(Buffer *buffer, const void *data, size_t len) 4883e230dd2SCorentin Chary { 4893e230dd2SCorentin Chary memcpy(buffer->buffer + buffer->offset, data, len); 4903e230dd2SCorentin Chary buffer->offset += len; 4913e230dd2SCorentin Chary } 4923e230dd2SCorentin Chary 4933e230dd2SCorentin Chary static void vnc_desktop_resize(VncState *vs) 4943e230dd2SCorentin Chary { 4953e230dd2SCorentin Chary DisplayState *ds = vs->ds; 4963e230dd2SCorentin Chary 4973e230dd2SCorentin Chary if (vs->csock == -1 || !vnc_has_feature(vs, VNC_FEATURE_RESIZE)) { 4983e230dd2SCorentin Chary return; 4993e230dd2SCorentin Chary } 5003e230dd2SCorentin Chary if (vs->client_width == ds_get_width(ds) && 5013e230dd2SCorentin Chary vs->client_height == ds_get_height(ds)) { 5023e230dd2SCorentin Chary return; 5033e230dd2SCorentin Chary } 5043e230dd2SCorentin Chary vs->client_width = ds_get_width(ds); 5053e230dd2SCorentin Chary vs->client_height = ds_get_height(ds); 506bd023f95SCorentin Chary vnc_lock_output(vs); 5073e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE); 5083e230dd2SCorentin Chary vnc_write_u8(vs, 0); 5093e230dd2SCorentin Chary vnc_write_u16(vs, 1); /* number of rects */ 5103e230dd2SCorentin Chary vnc_framebuffer_update(vs, 0, 0, vs->client_width, vs->client_height, 5113e230dd2SCorentin Chary VNC_ENCODING_DESKTOPRESIZE); 512bd023f95SCorentin Chary vnc_unlock_output(vs); 5133e230dd2SCorentin Chary vnc_flush(vs); 5143e230dd2SCorentin Chary } 5153e230dd2SCorentin Chary 516bd023f95SCorentin Chary #ifdef CONFIG_VNC_THREAD 517bd023f95SCorentin Chary static void vnc_abort_display_jobs(VncDisplay *vd) 518bd023f95SCorentin Chary { 519bd023f95SCorentin Chary VncState *vs; 520bd023f95SCorentin Chary 521bd023f95SCorentin Chary QTAILQ_FOREACH(vs, &vd->clients, next) { 522bd023f95SCorentin Chary vnc_lock_output(vs); 523bd023f95SCorentin Chary vs->abort = true; 524bd023f95SCorentin Chary vnc_unlock_output(vs); 525bd023f95SCorentin Chary } 526bd023f95SCorentin Chary QTAILQ_FOREACH(vs, &vd->clients, next) { 527bd023f95SCorentin Chary vnc_jobs_join(vs); 528bd023f95SCorentin Chary } 529bd023f95SCorentin Chary QTAILQ_FOREACH(vs, &vd->clients, next) { 530bd023f95SCorentin Chary vnc_lock_output(vs); 531bd023f95SCorentin Chary vs->abort = false; 532bd023f95SCorentin Chary vnc_unlock_output(vs); 533bd023f95SCorentin Chary } 534bd023f95SCorentin Chary } 535bd023f95SCorentin Chary #else 536bd023f95SCorentin Chary static void vnc_abort_display_jobs(VncDisplay *vd) 537bd023f95SCorentin Chary { 538bd023f95SCorentin Chary } 539bd023f95SCorentin Chary #endif 540bd023f95SCorentin Chary 5413e230dd2SCorentin Chary static void vnc_dpy_resize(DisplayState *ds) 5423e230dd2SCorentin Chary { 5433e230dd2SCorentin Chary VncDisplay *vd = ds->opaque; 5443e230dd2SCorentin Chary VncState *vs; 5453e230dd2SCorentin Chary 546bd023f95SCorentin Chary vnc_abort_display_jobs(vd); 547bd023f95SCorentin Chary 5483e230dd2SCorentin Chary /* server surface */ 5493e230dd2SCorentin Chary if (!vd->server) 5503e230dd2SCorentin Chary vd->server = qemu_mallocz(sizeof(*vd->server)); 5513e230dd2SCorentin Chary if (vd->server->data) 5523e230dd2SCorentin Chary qemu_free(vd->server->data); 5533e230dd2SCorentin Chary *(vd->server) = *(ds->surface); 5543e230dd2SCorentin Chary vd->server->data = qemu_mallocz(vd->server->linesize * 5553e230dd2SCorentin Chary vd->server->height); 5563e230dd2SCorentin Chary 5573e230dd2SCorentin Chary /* guest surface */ 5583e230dd2SCorentin Chary if (!vd->guest.ds) 5593e230dd2SCorentin Chary vd->guest.ds = qemu_mallocz(sizeof(*vd->guest.ds)); 5603e230dd2SCorentin Chary if (ds_get_bytes_per_pixel(ds) != vd->guest.ds->pf.bytes_per_pixel) 5613e230dd2SCorentin Chary console_color_init(ds); 5623e230dd2SCorentin Chary *(vd->guest.ds) = *(ds->surface); 5633e230dd2SCorentin Chary memset(vd->guest.dirty, 0xFF, sizeof(vd->guest.dirty)); 5643e230dd2SCorentin Chary 5653e230dd2SCorentin Chary QTAILQ_FOREACH(vs, &vd->clients, next) { 5663e230dd2SCorentin Chary vnc_colordepth(vs); 5673e230dd2SCorentin Chary vnc_desktop_resize(vs); 5683e230dd2SCorentin Chary if (vs->vd->cursor) { 5693e230dd2SCorentin Chary vnc_cursor_define(vs); 5703e230dd2SCorentin Chary } 5713e230dd2SCorentin Chary memset(vs->dirty, 0xFF, sizeof(vs->dirty)); 5723e230dd2SCorentin Chary } 5733e230dd2SCorentin Chary } 5743e230dd2SCorentin Chary 5753e230dd2SCorentin Chary /* fastest code */ 5763e230dd2SCorentin Chary static void vnc_write_pixels_copy(VncState *vs, struct PixelFormat *pf, 5773e230dd2SCorentin Chary void *pixels, int size) 5783e230dd2SCorentin Chary { 5793e230dd2SCorentin Chary vnc_write(vs, pixels, size); 5803e230dd2SCorentin Chary } 5813e230dd2SCorentin Chary 5823e230dd2SCorentin Chary /* slowest but generic code. */ 5833e230dd2SCorentin Chary void vnc_convert_pixel(VncState *vs, uint8_t *buf, uint32_t v) 5843e230dd2SCorentin Chary { 5853e230dd2SCorentin Chary uint8_t r, g, b; 5863e230dd2SCorentin Chary VncDisplay *vd = vs->vd; 5873e230dd2SCorentin Chary 5883e230dd2SCorentin Chary r = ((((v & vd->server->pf.rmask) >> vd->server->pf.rshift) << vs->clientds.pf.rbits) >> 5893e230dd2SCorentin Chary vd->server->pf.rbits); 5903e230dd2SCorentin Chary g = ((((v & vd->server->pf.gmask) >> vd->server->pf.gshift) << vs->clientds.pf.gbits) >> 5913e230dd2SCorentin Chary vd->server->pf.gbits); 5923e230dd2SCorentin Chary b = ((((v & vd->server->pf.bmask) >> vd->server->pf.bshift) << vs->clientds.pf.bbits) >> 5933e230dd2SCorentin Chary vd->server->pf.bbits); 5943e230dd2SCorentin Chary v = (r << vs->clientds.pf.rshift) | 5953e230dd2SCorentin Chary (g << vs->clientds.pf.gshift) | 5963e230dd2SCorentin Chary (b << vs->clientds.pf.bshift); 5973e230dd2SCorentin Chary switch(vs->clientds.pf.bytes_per_pixel) { 5983e230dd2SCorentin Chary case 1: 5993e230dd2SCorentin Chary buf[0] = v; 6003e230dd2SCorentin Chary break; 6013e230dd2SCorentin Chary case 2: 6023e230dd2SCorentin Chary if (vs->clientds.flags & QEMU_BIG_ENDIAN_FLAG) { 6033e230dd2SCorentin Chary buf[0] = v >> 8; 6043e230dd2SCorentin Chary buf[1] = v; 6053e230dd2SCorentin Chary } else { 6063e230dd2SCorentin Chary buf[1] = v >> 8; 6073e230dd2SCorentin Chary buf[0] = v; 6083e230dd2SCorentin Chary } 6093e230dd2SCorentin Chary break; 6103e230dd2SCorentin Chary default: 6113e230dd2SCorentin Chary case 4: 6123e230dd2SCorentin Chary if (vs->clientds.flags & QEMU_BIG_ENDIAN_FLAG) { 6133e230dd2SCorentin Chary buf[0] = v >> 24; 6143e230dd2SCorentin Chary buf[1] = v >> 16; 6153e230dd2SCorentin Chary buf[2] = v >> 8; 6163e230dd2SCorentin Chary buf[3] = v; 6173e230dd2SCorentin Chary } else { 6183e230dd2SCorentin Chary buf[3] = v >> 24; 6193e230dd2SCorentin Chary buf[2] = v >> 16; 6203e230dd2SCorentin Chary buf[1] = v >> 8; 6213e230dd2SCorentin Chary buf[0] = v; 6223e230dd2SCorentin Chary } 6233e230dd2SCorentin Chary break; 6243e230dd2SCorentin Chary } 6253e230dd2SCorentin Chary } 6263e230dd2SCorentin Chary 6273e230dd2SCorentin Chary static void vnc_write_pixels_generic(VncState *vs, struct PixelFormat *pf, 6283e230dd2SCorentin Chary void *pixels1, int size) 6293e230dd2SCorentin Chary { 6303e230dd2SCorentin Chary uint8_t buf[4]; 6313e230dd2SCorentin Chary 6323e230dd2SCorentin Chary if (pf->bytes_per_pixel == 4) { 6333e230dd2SCorentin Chary uint32_t *pixels = pixels1; 6343e230dd2SCorentin Chary int n, i; 6353e230dd2SCorentin Chary n = size >> 2; 6363e230dd2SCorentin Chary for(i = 0; i < n; i++) { 6373e230dd2SCorentin Chary vnc_convert_pixel(vs, buf, pixels[i]); 6383e230dd2SCorentin Chary vnc_write(vs, buf, vs->clientds.pf.bytes_per_pixel); 6393e230dd2SCorentin Chary } 6403e230dd2SCorentin Chary } else if (pf->bytes_per_pixel == 2) { 6413e230dd2SCorentin Chary uint16_t *pixels = pixels1; 6423e230dd2SCorentin Chary int n, i; 6433e230dd2SCorentin Chary n = size >> 1; 6443e230dd2SCorentin Chary for(i = 0; i < n; i++) { 6453e230dd2SCorentin Chary vnc_convert_pixel(vs, buf, pixels[i]); 6463e230dd2SCorentin Chary vnc_write(vs, buf, vs->clientds.pf.bytes_per_pixel); 6473e230dd2SCorentin Chary } 6483e230dd2SCorentin Chary } else if (pf->bytes_per_pixel == 1) { 6493e230dd2SCorentin Chary uint8_t *pixels = pixels1; 6503e230dd2SCorentin Chary int n, i; 6513e230dd2SCorentin Chary n = size; 6523e230dd2SCorentin Chary for(i = 0; i < n; i++) { 6533e230dd2SCorentin Chary vnc_convert_pixel(vs, buf, pixels[i]); 6543e230dd2SCorentin Chary vnc_write(vs, buf, vs->clientds.pf.bytes_per_pixel); 6553e230dd2SCorentin Chary } 6563e230dd2SCorentin Chary } else { 6573e230dd2SCorentin Chary fprintf(stderr, "vnc_write_pixels_generic: VncState color depth not supported\n"); 6583e230dd2SCorentin Chary } 6593e230dd2SCorentin Chary } 6603e230dd2SCorentin Chary 6613e230dd2SCorentin Chary int vnc_raw_send_framebuffer_update(VncState *vs, int x, int y, int w, int h) 6623e230dd2SCorentin Chary { 6633e230dd2SCorentin Chary int i; 6643e230dd2SCorentin Chary uint8_t *row; 6653e230dd2SCorentin Chary VncDisplay *vd = vs->vd; 6663e230dd2SCorentin Chary 6673e230dd2SCorentin Chary row = vd->server->data + y * ds_get_linesize(vs->ds) + x * ds_get_bytes_per_pixel(vs->ds); 6683e230dd2SCorentin Chary for (i = 0; i < h; i++) { 6693e230dd2SCorentin Chary vs->write_pixels(vs, &vd->server->pf, row, w * ds_get_bytes_per_pixel(vs->ds)); 6703e230dd2SCorentin Chary row += ds_get_linesize(vs->ds); 6713e230dd2SCorentin Chary } 6723e230dd2SCorentin Chary return 1; 6733e230dd2SCorentin Chary } 6743e230dd2SCorentin Chary 675bd023f95SCorentin Chary int vnc_send_framebuffer_update(VncState *vs, int x, int y, int w, int h) 6763e230dd2SCorentin Chary { 6773e230dd2SCorentin Chary int n = 0; 6783e230dd2SCorentin Chary 6793e230dd2SCorentin Chary switch(vs->vnc_encoding) { 6803e230dd2SCorentin Chary case VNC_ENCODING_ZLIB: 6813e230dd2SCorentin Chary n = vnc_zlib_send_framebuffer_update(vs, x, y, w, h); 6823e230dd2SCorentin Chary break; 6833e230dd2SCorentin Chary case VNC_ENCODING_HEXTILE: 6843e230dd2SCorentin Chary vnc_framebuffer_update(vs, x, y, w, h, VNC_ENCODING_HEXTILE); 6853e230dd2SCorentin Chary n = vnc_hextile_send_framebuffer_update(vs, x, y, w, h); 6863e230dd2SCorentin Chary break; 6873e230dd2SCorentin Chary case VNC_ENCODING_TIGHT: 6883e230dd2SCorentin Chary n = vnc_tight_send_framebuffer_update(vs, x, y, w, h); 6893e230dd2SCorentin Chary break; 690efe556adSCorentin Chary case VNC_ENCODING_TIGHT_PNG: 691efe556adSCorentin Chary n = vnc_tight_png_send_framebuffer_update(vs, x, y, w, h); 692efe556adSCorentin Chary break; 6933e230dd2SCorentin Chary default: 6943e230dd2SCorentin Chary vnc_framebuffer_update(vs, x, y, w, h, VNC_ENCODING_RAW); 6953e230dd2SCorentin Chary n = vnc_raw_send_framebuffer_update(vs, x, y, w, h); 6963e230dd2SCorentin Chary break; 6973e230dd2SCorentin Chary } 6983e230dd2SCorentin Chary return n; 6993e230dd2SCorentin Chary } 7003e230dd2SCorentin Chary 7013e230dd2SCorentin Chary static void vnc_copy(VncState *vs, int src_x, int src_y, int dst_x, int dst_y, int w, int h) 7023e230dd2SCorentin Chary { 7033e230dd2SCorentin Chary /* send bitblit op to the vnc client */ 704bd023f95SCorentin Chary vnc_lock_output(vs); 7053e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE); 7063e230dd2SCorentin Chary vnc_write_u8(vs, 0); 7073e230dd2SCorentin Chary vnc_write_u16(vs, 1); /* number of rects */ 7083e230dd2SCorentin Chary vnc_framebuffer_update(vs, dst_x, dst_y, w, h, VNC_ENCODING_COPYRECT); 7093e230dd2SCorentin Chary vnc_write_u16(vs, src_x); 7103e230dd2SCorentin Chary vnc_write_u16(vs, src_y); 711bd023f95SCorentin Chary vnc_unlock_output(vs); 7123e230dd2SCorentin Chary vnc_flush(vs); 7133e230dd2SCorentin Chary } 7143e230dd2SCorentin Chary 7153e230dd2SCorentin Chary static void vnc_dpy_copy(DisplayState *ds, int src_x, int src_y, int dst_x, int dst_y, int w, int h) 7163e230dd2SCorentin Chary { 7173e230dd2SCorentin Chary VncDisplay *vd = ds->opaque; 7183e230dd2SCorentin Chary VncState *vs, *vn; 7193e230dd2SCorentin Chary uint8_t *src_row; 7203e230dd2SCorentin Chary uint8_t *dst_row; 7213e230dd2SCorentin Chary int i,x,y,pitch,depth,inc,w_lim,s; 7223e230dd2SCorentin Chary int cmp_bytes; 7233e230dd2SCorentin Chary 7243e230dd2SCorentin Chary vnc_refresh_server_surface(vd); 7253e230dd2SCorentin Chary QTAILQ_FOREACH_SAFE(vs, &vd->clients, next, vn) { 7263e230dd2SCorentin Chary if (vnc_has_feature(vs, VNC_FEATURE_COPYRECT)) { 7273e230dd2SCorentin Chary vs->force_update = 1; 728bd023f95SCorentin Chary vnc_update_client_sync(vs, 1); 7293e230dd2SCorentin Chary /* vs might be free()ed here */ 7303e230dd2SCorentin Chary } 7313e230dd2SCorentin Chary } 7323e230dd2SCorentin Chary 7333e230dd2SCorentin Chary /* do bitblit op on the local surface too */ 7343e230dd2SCorentin Chary pitch = ds_get_linesize(vd->ds); 7353e230dd2SCorentin Chary depth = ds_get_bytes_per_pixel(vd->ds); 7363e230dd2SCorentin Chary src_row = vd->server->data + pitch * src_y + depth * src_x; 7373e230dd2SCorentin Chary dst_row = vd->server->data + pitch * dst_y + depth * dst_x; 7383e230dd2SCorentin Chary y = dst_y; 7393e230dd2SCorentin Chary inc = 1; 7403e230dd2SCorentin Chary if (dst_y > src_y) { 7413e230dd2SCorentin Chary /* copy backwards */ 7423e230dd2SCorentin Chary src_row += pitch * (h-1); 7433e230dd2SCorentin Chary dst_row += pitch * (h-1); 7443e230dd2SCorentin Chary pitch = -pitch; 7453e230dd2SCorentin Chary y = dst_y + h - 1; 7463e230dd2SCorentin Chary inc = -1; 7473e230dd2SCorentin Chary } 7483e230dd2SCorentin Chary w_lim = w - (16 - (dst_x % 16)); 7493e230dd2SCorentin Chary if (w_lim < 0) 7503e230dd2SCorentin Chary w_lim = w; 7513e230dd2SCorentin Chary else 7523e230dd2SCorentin Chary w_lim = w - (w_lim % 16); 7533e230dd2SCorentin Chary for (i = 0; i < h; i++) { 7543e230dd2SCorentin Chary for (x = 0; x <= w_lim; 7553e230dd2SCorentin Chary x += s, src_row += cmp_bytes, dst_row += cmp_bytes) { 7563e230dd2SCorentin Chary if (x == w_lim) { 7573e230dd2SCorentin Chary if ((s = w - w_lim) == 0) 7583e230dd2SCorentin Chary break; 7593e230dd2SCorentin Chary } else if (!x) { 7603e230dd2SCorentin Chary s = (16 - (dst_x % 16)); 7613e230dd2SCorentin Chary s = MIN(s, w_lim); 7623e230dd2SCorentin Chary } else { 7633e230dd2SCorentin Chary s = 16; 7643e230dd2SCorentin Chary } 7653e230dd2SCorentin Chary cmp_bytes = s * depth; 7663e230dd2SCorentin Chary if (memcmp(src_row, dst_row, cmp_bytes) == 0) 7673e230dd2SCorentin Chary continue; 7683e230dd2SCorentin Chary memmove(dst_row, src_row, cmp_bytes); 7693e230dd2SCorentin Chary QTAILQ_FOREACH(vs, &vd->clients, next) { 7703e230dd2SCorentin Chary if (!vnc_has_feature(vs, VNC_FEATURE_COPYRECT)) { 7713e230dd2SCorentin Chary vnc_set_bit(vs->dirty[y], ((x + dst_x) / 16)); 7723e230dd2SCorentin Chary } 7733e230dd2SCorentin Chary } 7743e230dd2SCorentin Chary } 7753e230dd2SCorentin Chary src_row += pitch - w * depth; 7763e230dd2SCorentin Chary dst_row += pitch - w * depth; 7773e230dd2SCorentin Chary y += inc; 7783e230dd2SCorentin Chary } 7793e230dd2SCorentin Chary 7803e230dd2SCorentin Chary QTAILQ_FOREACH(vs, &vd->clients, next) { 7813e230dd2SCorentin Chary if (vnc_has_feature(vs, VNC_FEATURE_COPYRECT)) { 7823e230dd2SCorentin Chary vnc_copy(vs, src_x, src_y, dst_x, dst_y, w, h); 7833e230dd2SCorentin Chary } 7843e230dd2SCorentin Chary } 7853e230dd2SCorentin Chary } 7863e230dd2SCorentin Chary 7873e230dd2SCorentin Chary static void vnc_mouse_set(int x, int y, int visible) 7883e230dd2SCorentin Chary { 7893e230dd2SCorentin Chary /* can we ask the client(s) to move the pointer ??? */ 7903e230dd2SCorentin Chary } 7913e230dd2SCorentin Chary 7923e230dd2SCorentin Chary static int vnc_cursor_define(VncState *vs) 7933e230dd2SCorentin Chary { 7943e230dd2SCorentin Chary QEMUCursor *c = vs->vd->cursor; 7953e230dd2SCorentin Chary PixelFormat pf = qemu_default_pixelformat(32); 7963e230dd2SCorentin Chary int isize; 7973e230dd2SCorentin Chary 7983e230dd2SCorentin Chary if (vnc_has_feature(vs, VNC_FEATURE_RICH_CURSOR)) { 799*d01f9595SCorentin Chary vnc_lock_output(vs); 8003e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE); 8013e230dd2SCorentin Chary vnc_write_u8(vs, 0); /* padding */ 8023e230dd2SCorentin Chary vnc_write_u16(vs, 1); /* # of rects */ 8033e230dd2SCorentin Chary vnc_framebuffer_update(vs, c->hot_x, c->hot_y, c->width, c->height, 8043e230dd2SCorentin Chary VNC_ENCODING_RICH_CURSOR); 8053e230dd2SCorentin Chary isize = c->width * c->height * vs->clientds.pf.bytes_per_pixel; 8063e230dd2SCorentin Chary vnc_write_pixels_generic(vs, &pf, c->data, isize); 8073e230dd2SCorentin Chary vnc_write(vs, vs->vd->cursor_mask, vs->vd->cursor_msize); 808*d01f9595SCorentin Chary vnc_unlock_output(vs); 8093e230dd2SCorentin Chary return 0; 8103e230dd2SCorentin Chary } 8113e230dd2SCorentin Chary return -1; 8123e230dd2SCorentin Chary } 8133e230dd2SCorentin Chary 8143e230dd2SCorentin Chary static void vnc_dpy_cursor_define(QEMUCursor *c) 8153e230dd2SCorentin Chary { 8163e230dd2SCorentin Chary VncDisplay *vd = vnc_display; 8173e230dd2SCorentin Chary VncState *vs; 8183e230dd2SCorentin Chary 8193e230dd2SCorentin Chary cursor_put(vd->cursor); 8203e230dd2SCorentin Chary qemu_free(vd->cursor_mask); 8213e230dd2SCorentin Chary 8223e230dd2SCorentin Chary vd->cursor = c; 8233e230dd2SCorentin Chary cursor_get(vd->cursor); 8243e230dd2SCorentin Chary vd->cursor_msize = cursor_get_mono_bpl(c) * c->height; 8253e230dd2SCorentin Chary vd->cursor_mask = qemu_mallocz(vd->cursor_msize); 8263e230dd2SCorentin Chary cursor_get_mono_mask(c, 0, vd->cursor_mask); 8273e230dd2SCorentin Chary 8283e230dd2SCorentin Chary QTAILQ_FOREACH(vs, &vd->clients, next) { 8293e230dd2SCorentin Chary vnc_cursor_define(vs); 8303e230dd2SCorentin Chary } 8313e230dd2SCorentin Chary } 8323e230dd2SCorentin Chary 8333e230dd2SCorentin Chary static int find_and_clear_dirty_height(struct VncState *vs, 8343e230dd2SCorentin Chary int y, int last_x, int x) 8353e230dd2SCorentin Chary { 8363e230dd2SCorentin Chary int h; 8373e230dd2SCorentin Chary VncDisplay *vd = vs->vd; 8383e230dd2SCorentin Chary 8393e230dd2SCorentin Chary for (h = 1; h < (vd->server->height - y); h++) { 8403e230dd2SCorentin Chary int tmp_x; 8413e230dd2SCorentin Chary if (!vnc_get_bit(vs->dirty[y + h], last_x)) 8423e230dd2SCorentin Chary break; 8433e230dd2SCorentin Chary for (tmp_x = last_x; tmp_x < x; tmp_x++) 8443e230dd2SCorentin Chary vnc_clear_bit(vs->dirty[y + h], tmp_x); 8453e230dd2SCorentin Chary } 8463e230dd2SCorentin Chary 8473e230dd2SCorentin Chary return h; 8483e230dd2SCorentin Chary } 8493e230dd2SCorentin Chary 850bd023f95SCorentin Chary #ifdef CONFIG_VNC_THREAD 851bd023f95SCorentin Chary static int vnc_update_client_sync(VncState *vs, int has_dirty) 852bd023f95SCorentin Chary { 853bd023f95SCorentin Chary int ret = vnc_update_client(vs, has_dirty); 854bd023f95SCorentin Chary vnc_jobs_join(vs); 855bd023f95SCorentin Chary return ret; 856bd023f95SCorentin Chary } 857bd023f95SCorentin Chary #else 858bd023f95SCorentin Chary static int vnc_update_client_sync(VncState *vs, int has_dirty) 859bd023f95SCorentin Chary { 860bd023f95SCorentin Chary return vnc_update_client(vs, has_dirty); 861bd023f95SCorentin Chary } 862bd023f95SCorentin Chary #endif 863bd023f95SCorentin Chary 8643e230dd2SCorentin Chary static int vnc_update_client(VncState *vs, int has_dirty) 8653e230dd2SCorentin Chary { 8663e230dd2SCorentin Chary if (vs->need_update && vs->csock != -1) { 8673e230dd2SCorentin Chary VncDisplay *vd = vs->vd; 868bd023f95SCorentin Chary VncJob *job; 8693e230dd2SCorentin Chary int y; 8703e230dd2SCorentin Chary int width, height; 871bd023f95SCorentin Chary int n = 0; 872bd023f95SCorentin Chary 8733e230dd2SCorentin Chary 8743e230dd2SCorentin Chary if (vs->output.offset && !vs->audio_cap && !vs->force_update) 8753e230dd2SCorentin Chary /* kernel send buffers are full -> drop frames to throttle */ 8763e230dd2SCorentin Chary return 0; 8773e230dd2SCorentin Chary 8783e230dd2SCorentin Chary if (!has_dirty && !vs->audio_cap && !vs->force_update) 8793e230dd2SCorentin Chary return 0; 8803e230dd2SCorentin Chary 8813e230dd2SCorentin Chary /* 8823e230dd2SCorentin Chary * Send screen updates to the vnc client using the server 8833e230dd2SCorentin Chary * surface and server dirty map. guest surface updates 8843e230dd2SCorentin Chary * happening in parallel don't disturb us, the next pass will 8853e230dd2SCorentin Chary * send them to the client. 8863e230dd2SCorentin Chary */ 887bd023f95SCorentin Chary job = vnc_job_new(vs); 8883e230dd2SCorentin Chary 8893e230dd2SCorentin Chary width = MIN(vd->server->width, vs->client_width); 8903e230dd2SCorentin Chary height = MIN(vd->server->height, vs->client_height); 8913e230dd2SCorentin Chary 8923e230dd2SCorentin Chary for (y = 0; y < height; y++) { 8933e230dd2SCorentin Chary int x; 8943e230dd2SCorentin Chary int last_x = -1; 8953e230dd2SCorentin Chary for (x = 0; x < width / 16; x++) { 8963e230dd2SCorentin Chary if (vnc_get_bit(vs->dirty[y], x)) { 8973e230dd2SCorentin Chary if (last_x == -1) { 8983e230dd2SCorentin Chary last_x = x; 8993e230dd2SCorentin Chary } 9003e230dd2SCorentin Chary vnc_clear_bit(vs->dirty[y], x); 9013e230dd2SCorentin Chary } else { 9023e230dd2SCorentin Chary if (last_x != -1) { 9033e230dd2SCorentin Chary int h = find_and_clear_dirty_height(vs, y, last_x, x); 904bd023f95SCorentin Chary 905bd023f95SCorentin Chary n += vnc_job_add_rect(job, last_x * 16, y, 9063e230dd2SCorentin Chary (x - last_x) * 16, h); 9073e230dd2SCorentin Chary } 9083e230dd2SCorentin Chary last_x = -1; 9093e230dd2SCorentin Chary } 9103e230dd2SCorentin Chary } 9113e230dd2SCorentin Chary if (last_x != -1) { 9123e230dd2SCorentin Chary int h = find_and_clear_dirty_height(vs, y, last_x, x); 913bd023f95SCorentin Chary n += vnc_job_add_rect(job, last_x * 16, y, 9143e230dd2SCorentin Chary (x - last_x) * 16, h); 9153e230dd2SCorentin Chary } 9163e230dd2SCorentin Chary } 917bd023f95SCorentin Chary 918bd023f95SCorentin Chary vnc_job_push(job); 9193e230dd2SCorentin Chary vs->force_update = 0; 920bd023f95SCorentin Chary return n; 9213e230dd2SCorentin Chary } 9223e230dd2SCorentin Chary 9233e230dd2SCorentin Chary if (vs->csock == -1) 9243e230dd2SCorentin Chary vnc_disconnect_finish(vs); 9253e230dd2SCorentin Chary 9263e230dd2SCorentin Chary return 0; 9273e230dd2SCorentin Chary } 9283e230dd2SCorentin Chary 9293e230dd2SCorentin Chary /* audio */ 9303e230dd2SCorentin Chary static void audio_capture_notify(void *opaque, audcnotification_e cmd) 9313e230dd2SCorentin Chary { 9323e230dd2SCorentin Chary VncState *vs = opaque; 9333e230dd2SCorentin Chary 9343e230dd2SCorentin Chary switch (cmd) { 9353e230dd2SCorentin Chary case AUD_CNOTIFY_DISABLE: 936bd023f95SCorentin Chary vnc_lock_output(vs); 9373e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_QEMU); 9383e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_QEMU_AUDIO); 9393e230dd2SCorentin Chary vnc_write_u16(vs, VNC_MSG_SERVER_QEMU_AUDIO_END); 940bd023f95SCorentin Chary vnc_unlock_output(vs); 9413e230dd2SCorentin Chary vnc_flush(vs); 9423e230dd2SCorentin Chary break; 9433e230dd2SCorentin Chary 9443e230dd2SCorentin Chary case AUD_CNOTIFY_ENABLE: 945bd023f95SCorentin Chary vnc_lock_output(vs); 9463e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_QEMU); 9473e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_QEMU_AUDIO); 9483e230dd2SCorentin Chary vnc_write_u16(vs, VNC_MSG_SERVER_QEMU_AUDIO_BEGIN); 949bd023f95SCorentin Chary vnc_unlock_output(vs); 9503e230dd2SCorentin Chary vnc_flush(vs); 9513e230dd2SCorentin Chary break; 9523e230dd2SCorentin Chary } 9533e230dd2SCorentin Chary } 9543e230dd2SCorentin Chary 9553e230dd2SCorentin Chary static void audio_capture_destroy(void *opaque) 9563e230dd2SCorentin Chary { 9573e230dd2SCorentin Chary } 9583e230dd2SCorentin Chary 9593e230dd2SCorentin Chary static void audio_capture(void *opaque, void *buf, int size) 9603e230dd2SCorentin Chary { 9613e230dd2SCorentin Chary VncState *vs = opaque; 9623e230dd2SCorentin Chary 963bd023f95SCorentin Chary vnc_lock_output(vs); 9643e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_QEMU); 9653e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_QEMU_AUDIO); 9663e230dd2SCorentin Chary vnc_write_u16(vs, VNC_MSG_SERVER_QEMU_AUDIO_DATA); 9673e230dd2SCorentin Chary vnc_write_u32(vs, size); 9683e230dd2SCorentin Chary vnc_write(vs, buf, size); 969bd023f95SCorentin Chary vnc_unlock_output(vs); 9703e230dd2SCorentin Chary vnc_flush(vs); 9713e230dd2SCorentin Chary } 9723e230dd2SCorentin Chary 9733e230dd2SCorentin Chary static void audio_add(VncState *vs) 9743e230dd2SCorentin Chary { 9753e230dd2SCorentin Chary struct audio_capture_ops ops; 9763e230dd2SCorentin Chary 9773e230dd2SCorentin Chary if (vs->audio_cap) { 9783e230dd2SCorentin Chary monitor_printf(default_mon, "audio already running\n"); 9793e230dd2SCorentin Chary return; 9803e230dd2SCorentin Chary } 9813e230dd2SCorentin Chary 9823e230dd2SCorentin Chary ops.notify = audio_capture_notify; 9833e230dd2SCorentin Chary ops.destroy = audio_capture_destroy; 9843e230dd2SCorentin Chary ops.capture = audio_capture; 9853e230dd2SCorentin Chary 9863e230dd2SCorentin Chary vs->audio_cap = AUD_add_capture(&vs->as, &ops, vs); 9873e230dd2SCorentin Chary if (!vs->audio_cap) { 9883e230dd2SCorentin Chary monitor_printf(default_mon, "Failed to add audio capture\n"); 9893e230dd2SCorentin Chary } 9903e230dd2SCorentin Chary } 9913e230dd2SCorentin Chary 9923e230dd2SCorentin Chary static void audio_del(VncState *vs) 9933e230dd2SCorentin Chary { 9943e230dd2SCorentin Chary if (vs->audio_cap) { 9953e230dd2SCorentin Chary AUD_del_capture(vs->audio_cap, vs); 9963e230dd2SCorentin Chary vs->audio_cap = NULL; 9973e230dd2SCorentin Chary } 9983e230dd2SCorentin Chary } 9993e230dd2SCorentin Chary 10003e230dd2SCorentin Chary static void vnc_disconnect_start(VncState *vs) 10013e230dd2SCorentin Chary { 10023e230dd2SCorentin Chary if (vs->csock == -1) 10033e230dd2SCorentin Chary return; 10043e230dd2SCorentin Chary qemu_set_fd_handler2(vs->csock, NULL, NULL, NULL, NULL); 10053e230dd2SCorentin Chary closesocket(vs->csock); 10063e230dd2SCorentin Chary vs->csock = -1; 10073e230dd2SCorentin Chary } 10083e230dd2SCorentin Chary 10093e230dd2SCorentin Chary static void vnc_disconnect_finish(VncState *vs) 10103e230dd2SCorentin Chary { 1011bd023f95SCorentin Chary vnc_jobs_join(vs); /* Wait encoding jobs */ 1012bd023f95SCorentin Chary 1013bd023f95SCorentin Chary vnc_lock_output(vs); 10143e230dd2SCorentin Chary vnc_qmp_event(vs, QEVENT_VNC_DISCONNECTED); 10153e230dd2SCorentin Chary 10163e230dd2SCorentin Chary buffer_free(&vs->input); 10173e230dd2SCorentin Chary buffer_free(&vs->output); 10183e230dd2SCorentin Chary 10193e230dd2SCorentin Chary qobject_decref(vs->info); 10203e230dd2SCorentin Chary 10213e230dd2SCorentin Chary vnc_zlib_clear(vs); 10223e230dd2SCorentin Chary vnc_tight_clear(vs); 10233e230dd2SCorentin Chary 10243e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS 10253e230dd2SCorentin Chary vnc_tls_client_cleanup(vs); 10263e230dd2SCorentin Chary #endif /* CONFIG_VNC_TLS */ 10273e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL 10283e230dd2SCorentin Chary vnc_sasl_client_cleanup(vs); 10293e230dd2SCorentin Chary #endif /* CONFIG_VNC_SASL */ 10303e230dd2SCorentin Chary audio_del(vs); 10313e230dd2SCorentin Chary 10323e230dd2SCorentin Chary QTAILQ_REMOVE(&vs->vd->clients, vs, next); 10333e230dd2SCorentin Chary 10343e230dd2SCorentin Chary if (QTAILQ_EMPTY(&vs->vd->clients)) { 10353e230dd2SCorentin Chary dcl->idle = 1; 10363e230dd2SCorentin Chary } 10373e230dd2SCorentin Chary 10383e230dd2SCorentin Chary qemu_remove_mouse_mode_change_notifier(&vs->mouse_mode_notifier); 10393e230dd2SCorentin Chary vnc_remove_timer(vs->vd); 10403e230dd2SCorentin Chary if (vs->vd->lock_key_sync) 10413e230dd2SCorentin Chary qemu_remove_led_event_handler(vs->led); 1042bd023f95SCorentin Chary vnc_unlock_output(vs); 1043bd023f95SCorentin Chary 1044bd023f95SCorentin Chary #ifdef CONFIG_VNC_THREAD 1045bd023f95SCorentin Chary qemu_mutex_destroy(&vs->output_mutex); 1046bd023f95SCorentin Chary #endif 10473e230dd2SCorentin Chary qemu_free(vs); 10483e230dd2SCorentin Chary } 10493e230dd2SCorentin Chary 10503e230dd2SCorentin Chary int vnc_client_io_error(VncState *vs, int ret, int last_errno) 10513e230dd2SCorentin Chary { 10523e230dd2SCorentin Chary if (ret == 0 || ret == -1) { 10533e230dd2SCorentin Chary if (ret == -1) { 10543e230dd2SCorentin Chary switch (last_errno) { 10553e230dd2SCorentin Chary case EINTR: 10563e230dd2SCorentin Chary case EAGAIN: 10573e230dd2SCorentin Chary #ifdef _WIN32 10583e230dd2SCorentin Chary case WSAEWOULDBLOCK: 10593e230dd2SCorentin Chary #endif 10603e230dd2SCorentin Chary return 0; 10613e230dd2SCorentin Chary default: 10623e230dd2SCorentin Chary break; 10633e230dd2SCorentin Chary } 10643e230dd2SCorentin Chary } 10653e230dd2SCorentin Chary 10663e230dd2SCorentin Chary VNC_DEBUG("Closing down client sock: ret %d, errno %d\n", 10673e230dd2SCorentin Chary ret, ret < 0 ? last_errno : 0); 10683e230dd2SCorentin Chary vnc_disconnect_start(vs); 10693e230dd2SCorentin Chary 10703e230dd2SCorentin Chary return 0; 10713e230dd2SCorentin Chary } 10723e230dd2SCorentin Chary return ret; 10733e230dd2SCorentin Chary } 10743e230dd2SCorentin Chary 10753e230dd2SCorentin Chary 10763e230dd2SCorentin Chary void vnc_client_error(VncState *vs) 10773e230dd2SCorentin Chary { 10783e230dd2SCorentin Chary VNC_DEBUG("Closing down client sock: protocol error\n"); 10793e230dd2SCorentin Chary vnc_disconnect_start(vs); 10803e230dd2SCorentin Chary } 10813e230dd2SCorentin Chary 10823e230dd2SCorentin Chary 10833e230dd2SCorentin Chary /* 10843e230dd2SCorentin Chary * Called to write a chunk of data to the client socket. The data may 10853e230dd2SCorentin Chary * be the raw data, or may have already been encoded by SASL. 10863e230dd2SCorentin Chary * The data will be written either straight onto the socket, or 10873e230dd2SCorentin Chary * written via the GNUTLS wrappers, if TLS/SSL encryption is enabled 10883e230dd2SCorentin Chary * 10893e230dd2SCorentin Chary * NB, it is theoretically possible to have 2 layers of encryption, 10903e230dd2SCorentin Chary * both SASL, and this TLS layer. It is highly unlikely in practice 10913e230dd2SCorentin Chary * though, since SASL encryption will typically be a no-op if TLS 10923e230dd2SCorentin Chary * is active 10933e230dd2SCorentin Chary * 10943e230dd2SCorentin Chary * Returns the number of bytes written, which may be less than 10953e230dd2SCorentin Chary * the requested 'datalen' if the socket would block. Returns 10963e230dd2SCorentin Chary * -1 on error, and disconnects the client socket. 10973e230dd2SCorentin Chary */ 10983e230dd2SCorentin Chary long vnc_client_write_buf(VncState *vs, const uint8_t *data, size_t datalen) 10993e230dd2SCorentin Chary { 11003e230dd2SCorentin Chary long ret; 11013e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS 11023e230dd2SCorentin Chary if (vs->tls.session) { 11033e230dd2SCorentin Chary ret = gnutls_write(vs->tls.session, data, datalen); 11043e230dd2SCorentin Chary if (ret < 0) { 11053e230dd2SCorentin Chary if (ret == GNUTLS_E_AGAIN) 11063e230dd2SCorentin Chary errno = EAGAIN; 11073e230dd2SCorentin Chary else 11083e230dd2SCorentin Chary errno = EIO; 11093e230dd2SCorentin Chary ret = -1; 11103e230dd2SCorentin Chary } 11113e230dd2SCorentin Chary } else 11123e230dd2SCorentin Chary #endif /* CONFIG_VNC_TLS */ 11133e230dd2SCorentin Chary ret = send(vs->csock, (const void *)data, datalen, 0); 11143e230dd2SCorentin Chary VNC_DEBUG("Wrote wire %p %zd -> %ld\n", data, datalen, ret); 11153e230dd2SCorentin Chary return vnc_client_io_error(vs, ret, socket_error()); 11163e230dd2SCorentin Chary } 11173e230dd2SCorentin Chary 11183e230dd2SCorentin Chary 11193e230dd2SCorentin Chary /* 11203e230dd2SCorentin Chary * Called to write buffered data to the client socket, when not 11213e230dd2SCorentin Chary * using any SASL SSF encryption layers. Will write as much data 11223e230dd2SCorentin Chary * as possible without blocking. If all buffered data is written, 11233e230dd2SCorentin Chary * will switch the FD poll() handler back to read monitoring. 11243e230dd2SCorentin Chary * 11253e230dd2SCorentin Chary * Returns the number of bytes written, which may be less than 11263e230dd2SCorentin Chary * the buffered output data if the socket would block. Returns 11273e230dd2SCorentin Chary * -1 on error, and disconnects the client socket. 11283e230dd2SCorentin Chary */ 11293e230dd2SCorentin Chary static long vnc_client_write_plain(VncState *vs) 11303e230dd2SCorentin Chary { 11313e230dd2SCorentin Chary long ret; 11323e230dd2SCorentin Chary 11333e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL 11343e230dd2SCorentin Chary VNC_DEBUG("Write Plain: Pending output %p size %zd offset %zd. Wait SSF %d\n", 11353e230dd2SCorentin Chary vs->output.buffer, vs->output.capacity, vs->output.offset, 11363e230dd2SCorentin Chary vs->sasl.waitWriteSSF); 11373e230dd2SCorentin Chary 11383e230dd2SCorentin Chary if (vs->sasl.conn && 11393e230dd2SCorentin Chary vs->sasl.runSSF && 11403e230dd2SCorentin Chary vs->sasl.waitWriteSSF) { 11413e230dd2SCorentin Chary ret = vnc_client_write_buf(vs, vs->output.buffer, vs->sasl.waitWriteSSF); 11423e230dd2SCorentin Chary if (ret) 11433e230dd2SCorentin Chary vs->sasl.waitWriteSSF -= ret; 11443e230dd2SCorentin Chary } else 11453e230dd2SCorentin Chary #endif /* CONFIG_VNC_SASL */ 11463e230dd2SCorentin Chary ret = vnc_client_write_buf(vs, vs->output.buffer, vs->output.offset); 11473e230dd2SCorentin Chary if (!ret) 11483e230dd2SCorentin Chary return 0; 11493e230dd2SCorentin Chary 11503e230dd2SCorentin Chary memmove(vs->output.buffer, vs->output.buffer + ret, (vs->output.offset - ret)); 11513e230dd2SCorentin Chary vs->output.offset -= ret; 11523e230dd2SCorentin Chary 11533e230dd2SCorentin Chary if (vs->output.offset == 0) { 11543e230dd2SCorentin Chary qemu_set_fd_handler2(vs->csock, NULL, vnc_client_read, NULL, vs); 11553e230dd2SCorentin Chary } 11563e230dd2SCorentin Chary 11573e230dd2SCorentin Chary return ret; 11583e230dd2SCorentin Chary } 11593e230dd2SCorentin Chary 11603e230dd2SCorentin Chary 11613e230dd2SCorentin Chary /* 11623e230dd2SCorentin Chary * First function called whenever there is data to be written to 11633e230dd2SCorentin Chary * the client socket. Will delegate actual work according to whether 11643e230dd2SCorentin Chary * SASL SSF layers are enabled (thus requiring encryption calls) 11653e230dd2SCorentin Chary */ 1166bd023f95SCorentin Chary static void vnc_client_write_locked(void *opaque) 11673e230dd2SCorentin Chary { 11683e230dd2SCorentin Chary VncState *vs = opaque; 11693e230dd2SCorentin Chary 11703e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL 11713e230dd2SCorentin Chary if (vs->sasl.conn && 11723e230dd2SCorentin Chary vs->sasl.runSSF && 11733e230dd2SCorentin Chary !vs->sasl.waitWriteSSF) { 11743e230dd2SCorentin Chary vnc_client_write_sasl(vs); 11753e230dd2SCorentin Chary } else 11763e230dd2SCorentin Chary #endif /* CONFIG_VNC_SASL */ 11773e230dd2SCorentin Chary vnc_client_write_plain(vs); 11783e230dd2SCorentin Chary } 11793e230dd2SCorentin Chary 1180bd023f95SCorentin Chary void vnc_client_write(void *opaque) 1181bd023f95SCorentin Chary { 1182bd023f95SCorentin Chary VncState *vs = opaque; 1183bd023f95SCorentin Chary 1184bd023f95SCorentin Chary vnc_lock_output(vs); 1185bd023f95SCorentin Chary if (vs->output.offset) { 1186bd023f95SCorentin Chary vnc_client_write_locked(opaque); 1187bd023f95SCorentin Chary } else { 1188bd023f95SCorentin Chary qemu_set_fd_handler2(vs->csock, NULL, vnc_client_read, NULL, vs); 1189bd023f95SCorentin Chary } 1190bd023f95SCorentin Chary vnc_unlock_output(vs); 1191bd023f95SCorentin Chary } 1192bd023f95SCorentin Chary 11933e230dd2SCorentin Chary void vnc_read_when(VncState *vs, VncReadEvent *func, size_t expecting) 11943e230dd2SCorentin Chary { 11953e230dd2SCorentin Chary vs->read_handler = func; 11963e230dd2SCorentin Chary vs->read_handler_expect = expecting; 11973e230dd2SCorentin Chary } 11983e230dd2SCorentin Chary 11993e230dd2SCorentin Chary 12003e230dd2SCorentin Chary /* 12013e230dd2SCorentin Chary * Called to read a chunk of data from the client socket. The data may 12023e230dd2SCorentin Chary * be the raw data, or may need to be further decoded by SASL. 12033e230dd2SCorentin Chary * The data will be read either straight from to the socket, or 12043e230dd2SCorentin Chary * read via the GNUTLS wrappers, if TLS/SSL encryption is enabled 12053e230dd2SCorentin Chary * 12063e230dd2SCorentin Chary * NB, it is theoretically possible to have 2 layers of encryption, 12073e230dd2SCorentin Chary * both SASL, and this TLS layer. It is highly unlikely in practice 12083e230dd2SCorentin Chary * though, since SASL encryption will typically be a no-op if TLS 12093e230dd2SCorentin Chary * is active 12103e230dd2SCorentin Chary * 12113e230dd2SCorentin Chary * Returns the number of bytes read, which may be less than 12123e230dd2SCorentin Chary * the requested 'datalen' if the socket would block. Returns 12133e230dd2SCorentin Chary * -1 on error, and disconnects the client socket. 12143e230dd2SCorentin Chary */ 12153e230dd2SCorentin Chary long vnc_client_read_buf(VncState *vs, uint8_t *data, size_t datalen) 12163e230dd2SCorentin Chary { 12173e230dd2SCorentin Chary long ret; 12183e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS 12193e230dd2SCorentin Chary if (vs->tls.session) { 12203e230dd2SCorentin Chary ret = gnutls_read(vs->tls.session, data, datalen); 12213e230dd2SCorentin Chary if (ret < 0) { 12223e230dd2SCorentin Chary if (ret == GNUTLS_E_AGAIN) 12233e230dd2SCorentin Chary errno = EAGAIN; 12243e230dd2SCorentin Chary else 12253e230dd2SCorentin Chary errno = EIO; 12263e230dd2SCorentin Chary ret = -1; 12273e230dd2SCorentin Chary } 12283e230dd2SCorentin Chary } else 12293e230dd2SCorentin Chary #endif /* CONFIG_VNC_TLS */ 12303e230dd2SCorentin Chary ret = recv(vs->csock, (void *)data, datalen, 0); 12313e230dd2SCorentin Chary VNC_DEBUG("Read wire %p %zd -> %ld\n", data, datalen, ret); 12323e230dd2SCorentin Chary return vnc_client_io_error(vs, ret, socket_error()); 12333e230dd2SCorentin Chary } 12343e230dd2SCorentin Chary 12353e230dd2SCorentin Chary 12363e230dd2SCorentin Chary /* 12373e230dd2SCorentin Chary * Called to read data from the client socket to the input buffer, 12383e230dd2SCorentin Chary * when not using any SASL SSF encryption layers. Will read as much 12393e230dd2SCorentin Chary * data as possible without blocking. 12403e230dd2SCorentin Chary * 12413e230dd2SCorentin Chary * Returns the number of bytes read. Returns -1 on error, and 12423e230dd2SCorentin Chary * disconnects the client socket. 12433e230dd2SCorentin Chary */ 12443e230dd2SCorentin Chary static long vnc_client_read_plain(VncState *vs) 12453e230dd2SCorentin Chary { 12463e230dd2SCorentin Chary int ret; 12473e230dd2SCorentin Chary VNC_DEBUG("Read plain %p size %zd offset %zd\n", 12483e230dd2SCorentin Chary vs->input.buffer, vs->input.capacity, vs->input.offset); 12493e230dd2SCorentin Chary buffer_reserve(&vs->input, 4096); 12503e230dd2SCorentin Chary ret = vnc_client_read_buf(vs, buffer_end(&vs->input), 4096); 12513e230dd2SCorentin Chary if (!ret) 12523e230dd2SCorentin Chary return 0; 12533e230dd2SCorentin Chary vs->input.offset += ret; 12543e230dd2SCorentin Chary return ret; 12553e230dd2SCorentin Chary } 12563e230dd2SCorentin Chary 12573e230dd2SCorentin Chary 12583e230dd2SCorentin Chary /* 12593e230dd2SCorentin Chary * First function called whenever there is more data to be read from 12603e230dd2SCorentin Chary * the client socket. Will delegate actual work according to whether 12613e230dd2SCorentin Chary * SASL SSF layers are enabled (thus requiring decryption calls) 12623e230dd2SCorentin Chary */ 12633e230dd2SCorentin Chary void vnc_client_read(void *opaque) 12643e230dd2SCorentin Chary { 12653e230dd2SCorentin Chary VncState *vs = opaque; 12663e230dd2SCorentin Chary long ret; 12673e230dd2SCorentin Chary 12683e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL 12693e230dd2SCorentin Chary if (vs->sasl.conn && vs->sasl.runSSF) 12703e230dd2SCorentin Chary ret = vnc_client_read_sasl(vs); 12713e230dd2SCorentin Chary else 12723e230dd2SCorentin Chary #endif /* CONFIG_VNC_SASL */ 12733e230dd2SCorentin Chary ret = vnc_client_read_plain(vs); 12743e230dd2SCorentin Chary if (!ret) { 12753e230dd2SCorentin Chary if (vs->csock == -1) 12763e230dd2SCorentin Chary vnc_disconnect_finish(vs); 12773e230dd2SCorentin Chary return; 12783e230dd2SCorentin Chary } 12793e230dd2SCorentin Chary 12803e230dd2SCorentin Chary while (vs->read_handler && vs->input.offset >= vs->read_handler_expect) { 12813e230dd2SCorentin Chary size_t len = vs->read_handler_expect; 12823e230dd2SCorentin Chary int ret; 12833e230dd2SCorentin Chary 12843e230dd2SCorentin Chary ret = vs->read_handler(vs, vs->input.buffer, len); 12853e230dd2SCorentin Chary if (vs->csock == -1) { 12863e230dd2SCorentin Chary vnc_disconnect_finish(vs); 12873e230dd2SCorentin Chary return; 12883e230dd2SCorentin Chary } 12893e230dd2SCorentin Chary 12903e230dd2SCorentin Chary if (!ret) { 12913e230dd2SCorentin Chary memmove(vs->input.buffer, vs->input.buffer + len, (vs->input.offset - len)); 12923e230dd2SCorentin Chary vs->input.offset -= len; 12933e230dd2SCorentin Chary } else { 12943e230dd2SCorentin Chary vs->read_handler_expect = ret; 12953e230dd2SCorentin Chary } 12963e230dd2SCorentin Chary } 12973e230dd2SCorentin Chary } 12983e230dd2SCorentin Chary 12993e230dd2SCorentin Chary void vnc_write(VncState *vs, const void *data, size_t len) 13003e230dd2SCorentin Chary { 13013e230dd2SCorentin Chary buffer_reserve(&vs->output, len); 13023e230dd2SCorentin Chary 13033e230dd2SCorentin Chary if (vs->csock != -1 && buffer_empty(&vs->output)) { 13043e230dd2SCorentin Chary qemu_set_fd_handler2(vs->csock, NULL, vnc_client_read, vnc_client_write, vs); 13053e230dd2SCorentin Chary } 13063e230dd2SCorentin Chary 13073e230dd2SCorentin Chary buffer_append(&vs->output, data, len); 13083e230dd2SCorentin Chary } 13093e230dd2SCorentin Chary 13103e230dd2SCorentin Chary void vnc_write_s32(VncState *vs, int32_t value) 13113e230dd2SCorentin Chary { 13123e230dd2SCorentin Chary vnc_write_u32(vs, *(uint32_t *)&value); 13133e230dd2SCorentin Chary } 13143e230dd2SCorentin Chary 13153e230dd2SCorentin Chary void vnc_write_u32(VncState *vs, uint32_t value) 13163e230dd2SCorentin Chary { 13173e230dd2SCorentin Chary uint8_t buf[4]; 13183e230dd2SCorentin Chary 13193e230dd2SCorentin Chary buf[0] = (value >> 24) & 0xFF; 13203e230dd2SCorentin Chary buf[1] = (value >> 16) & 0xFF; 13213e230dd2SCorentin Chary buf[2] = (value >> 8) & 0xFF; 13223e230dd2SCorentin Chary buf[3] = value & 0xFF; 13233e230dd2SCorentin Chary 13243e230dd2SCorentin Chary vnc_write(vs, buf, 4); 13253e230dd2SCorentin Chary } 13263e230dd2SCorentin Chary 13273e230dd2SCorentin Chary void vnc_write_u16(VncState *vs, uint16_t value) 13283e230dd2SCorentin Chary { 13293e230dd2SCorentin Chary uint8_t buf[2]; 13303e230dd2SCorentin Chary 13313e230dd2SCorentin Chary buf[0] = (value >> 8) & 0xFF; 13323e230dd2SCorentin Chary buf[1] = value & 0xFF; 13333e230dd2SCorentin Chary 13343e230dd2SCorentin Chary vnc_write(vs, buf, 2); 13353e230dd2SCorentin Chary } 13363e230dd2SCorentin Chary 13373e230dd2SCorentin Chary void vnc_write_u8(VncState *vs, uint8_t value) 13383e230dd2SCorentin Chary { 13393e230dd2SCorentin Chary vnc_write(vs, (char *)&value, 1); 13403e230dd2SCorentin Chary } 13413e230dd2SCorentin Chary 13423e230dd2SCorentin Chary void vnc_flush(VncState *vs) 13433e230dd2SCorentin Chary { 1344bd023f95SCorentin Chary vnc_lock_output(vs); 1345bd023f95SCorentin Chary if (vs->csock != -1 && vs->output.offset) { 1346bd023f95SCorentin Chary vnc_client_write_locked(vs); 1347bd023f95SCorentin Chary } 1348bd023f95SCorentin Chary vnc_unlock_output(vs); 13493e230dd2SCorentin Chary } 13503e230dd2SCorentin Chary 13513e230dd2SCorentin Chary uint8_t read_u8(uint8_t *data, size_t offset) 13523e230dd2SCorentin Chary { 13533e230dd2SCorentin Chary return data[offset]; 13543e230dd2SCorentin Chary } 13553e230dd2SCorentin Chary 13563e230dd2SCorentin Chary uint16_t read_u16(uint8_t *data, size_t offset) 13573e230dd2SCorentin Chary { 13583e230dd2SCorentin Chary return ((data[offset] & 0xFF) << 8) | (data[offset + 1] & 0xFF); 13593e230dd2SCorentin Chary } 13603e230dd2SCorentin Chary 13613e230dd2SCorentin Chary int32_t read_s32(uint8_t *data, size_t offset) 13623e230dd2SCorentin Chary { 13633e230dd2SCorentin Chary return (int32_t)((data[offset] << 24) | (data[offset + 1] << 16) | 13643e230dd2SCorentin Chary (data[offset + 2] << 8) | data[offset + 3]); 13653e230dd2SCorentin Chary } 13663e230dd2SCorentin Chary 13673e230dd2SCorentin Chary uint32_t read_u32(uint8_t *data, size_t offset) 13683e230dd2SCorentin Chary { 13693e230dd2SCorentin Chary return ((data[offset] << 24) | (data[offset + 1] << 16) | 13703e230dd2SCorentin Chary (data[offset + 2] << 8) | data[offset + 3]); 13713e230dd2SCorentin Chary } 13723e230dd2SCorentin Chary 13733e230dd2SCorentin Chary static void client_cut_text(VncState *vs, size_t len, uint8_t *text) 13743e230dd2SCorentin Chary { 13753e230dd2SCorentin Chary } 13763e230dd2SCorentin Chary 13773e230dd2SCorentin Chary static void check_pointer_type_change(Notifier *notifier) 13783e230dd2SCorentin Chary { 13793e230dd2SCorentin Chary VncState *vs = container_of(notifier, VncState, mouse_mode_notifier); 13803e230dd2SCorentin Chary int absolute = kbd_mouse_is_absolute(); 13813e230dd2SCorentin Chary 13823e230dd2SCorentin Chary if (vnc_has_feature(vs, VNC_FEATURE_POINTER_TYPE_CHANGE) && vs->absolute != absolute) { 1383bd023f95SCorentin Chary vnc_lock_output(vs); 13843e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE); 13853e230dd2SCorentin Chary vnc_write_u8(vs, 0); 13863e230dd2SCorentin Chary vnc_write_u16(vs, 1); 13873e230dd2SCorentin Chary vnc_framebuffer_update(vs, absolute, 0, 13883e230dd2SCorentin Chary ds_get_width(vs->ds), ds_get_height(vs->ds), 13893e230dd2SCorentin Chary VNC_ENCODING_POINTER_TYPE_CHANGE); 1390bd023f95SCorentin Chary vnc_unlock_output(vs); 13913e230dd2SCorentin Chary vnc_flush(vs); 13923e230dd2SCorentin Chary } 13933e230dd2SCorentin Chary vs->absolute = absolute; 13943e230dd2SCorentin Chary } 13953e230dd2SCorentin Chary 13963e230dd2SCorentin Chary static void pointer_event(VncState *vs, int button_mask, int x, int y) 13973e230dd2SCorentin Chary { 13983e230dd2SCorentin Chary int buttons = 0; 13993e230dd2SCorentin Chary int dz = 0; 14003e230dd2SCorentin Chary 14013e230dd2SCorentin Chary if (button_mask & 0x01) 14023e230dd2SCorentin Chary buttons |= MOUSE_EVENT_LBUTTON; 14033e230dd2SCorentin Chary if (button_mask & 0x02) 14043e230dd2SCorentin Chary buttons |= MOUSE_EVENT_MBUTTON; 14053e230dd2SCorentin Chary if (button_mask & 0x04) 14063e230dd2SCorentin Chary buttons |= MOUSE_EVENT_RBUTTON; 14073e230dd2SCorentin Chary if (button_mask & 0x08) 14083e230dd2SCorentin Chary dz = -1; 14093e230dd2SCorentin Chary if (button_mask & 0x10) 14103e230dd2SCorentin Chary dz = 1; 14113e230dd2SCorentin Chary 14123e230dd2SCorentin Chary if (vs->absolute) { 14133e230dd2SCorentin Chary kbd_mouse_event(ds_get_width(vs->ds) > 1 ? 14143e230dd2SCorentin Chary x * 0x7FFF / (ds_get_width(vs->ds) - 1) : 0x4000, 14153e230dd2SCorentin Chary ds_get_height(vs->ds) > 1 ? 14163e230dd2SCorentin Chary y * 0x7FFF / (ds_get_height(vs->ds) - 1) : 0x4000, 14173e230dd2SCorentin Chary dz, buttons); 14183e230dd2SCorentin Chary } else if (vnc_has_feature(vs, VNC_FEATURE_POINTER_TYPE_CHANGE)) { 14193e230dd2SCorentin Chary x -= 0x7FFF; 14203e230dd2SCorentin Chary y -= 0x7FFF; 14213e230dd2SCorentin Chary 14223e230dd2SCorentin Chary kbd_mouse_event(x, y, dz, buttons); 14233e230dd2SCorentin Chary } else { 14243e230dd2SCorentin Chary if (vs->last_x != -1) 14253e230dd2SCorentin Chary kbd_mouse_event(x - vs->last_x, 14263e230dd2SCorentin Chary y - vs->last_y, 14273e230dd2SCorentin Chary dz, buttons); 14283e230dd2SCorentin Chary vs->last_x = x; 14293e230dd2SCorentin Chary vs->last_y = y; 14303e230dd2SCorentin Chary } 14313e230dd2SCorentin Chary } 14323e230dd2SCorentin Chary 14333e230dd2SCorentin Chary static void reset_keys(VncState *vs) 14343e230dd2SCorentin Chary { 14353e230dd2SCorentin Chary int i; 14363e230dd2SCorentin Chary for(i = 0; i < 256; i++) { 14373e230dd2SCorentin Chary if (vs->modifiers_state[i]) { 14383e230dd2SCorentin Chary if (i & SCANCODE_GREY) 14393e230dd2SCorentin Chary kbd_put_keycode(SCANCODE_EMUL0); 14403e230dd2SCorentin Chary kbd_put_keycode(i | SCANCODE_UP); 14413e230dd2SCorentin Chary vs->modifiers_state[i] = 0; 14423e230dd2SCorentin Chary } 14433e230dd2SCorentin Chary } 14443e230dd2SCorentin Chary } 14453e230dd2SCorentin Chary 14463e230dd2SCorentin Chary static void press_key(VncState *vs, int keysym) 14473e230dd2SCorentin Chary { 14483e230dd2SCorentin Chary int keycode = keysym2scancode(vs->vd->kbd_layout, keysym) & SCANCODE_KEYMASK; 14493e230dd2SCorentin Chary if (keycode & SCANCODE_GREY) 14503e230dd2SCorentin Chary kbd_put_keycode(SCANCODE_EMUL0); 14513e230dd2SCorentin Chary kbd_put_keycode(keycode & SCANCODE_KEYCODEMASK); 14523e230dd2SCorentin Chary if (keycode & SCANCODE_GREY) 14533e230dd2SCorentin Chary kbd_put_keycode(SCANCODE_EMUL0); 14543e230dd2SCorentin Chary kbd_put_keycode(keycode | SCANCODE_UP); 14553e230dd2SCorentin Chary } 14563e230dd2SCorentin Chary 14573e230dd2SCorentin Chary static void kbd_leds(void *opaque, int ledstate) 14583e230dd2SCorentin Chary { 14593e230dd2SCorentin Chary VncState *vs = opaque; 14603e230dd2SCorentin Chary int caps, num; 14613e230dd2SCorentin Chary 14623e230dd2SCorentin Chary caps = ledstate & QEMU_CAPS_LOCK_LED ? 1 : 0; 14633e230dd2SCorentin Chary num = ledstate & QEMU_NUM_LOCK_LED ? 1 : 0; 14643e230dd2SCorentin Chary 14653e230dd2SCorentin Chary if (vs->modifiers_state[0x3a] != caps) { 14663e230dd2SCorentin Chary vs->modifiers_state[0x3a] = caps; 14673e230dd2SCorentin Chary } 14683e230dd2SCorentin Chary if (vs->modifiers_state[0x45] != num) { 14693e230dd2SCorentin Chary vs->modifiers_state[0x45] = num; 14703e230dd2SCorentin Chary } 14713e230dd2SCorentin Chary } 14723e230dd2SCorentin Chary 14733e230dd2SCorentin Chary static void do_key_event(VncState *vs, int down, int keycode, int sym) 14743e230dd2SCorentin Chary { 14753e230dd2SCorentin Chary /* QEMU console switch */ 14763e230dd2SCorentin Chary switch(keycode) { 14773e230dd2SCorentin Chary case 0x2a: /* Left Shift */ 14783e230dd2SCorentin Chary case 0x36: /* Right Shift */ 14793e230dd2SCorentin Chary case 0x1d: /* Left CTRL */ 14803e230dd2SCorentin Chary case 0x9d: /* Right CTRL */ 14813e230dd2SCorentin Chary case 0x38: /* Left ALT */ 14823e230dd2SCorentin Chary case 0xb8: /* Right ALT */ 14833e230dd2SCorentin Chary if (down) 14843e230dd2SCorentin Chary vs->modifiers_state[keycode] = 1; 14853e230dd2SCorentin Chary else 14863e230dd2SCorentin Chary vs->modifiers_state[keycode] = 0; 14873e230dd2SCorentin Chary break; 14883e230dd2SCorentin Chary case 0x02 ... 0x0a: /* '1' to '9' keys */ 14893e230dd2SCorentin Chary if (down && vs->modifiers_state[0x1d] && vs->modifiers_state[0x38]) { 14903e230dd2SCorentin Chary /* Reset the modifiers sent to the current console */ 14913e230dd2SCorentin Chary reset_keys(vs); 14923e230dd2SCorentin Chary console_select(keycode - 0x02); 14933e230dd2SCorentin Chary return; 14943e230dd2SCorentin Chary } 14953e230dd2SCorentin Chary break; 14963e230dd2SCorentin Chary case 0x3a: /* CapsLock */ 14973e230dd2SCorentin Chary case 0x45: /* NumLock */ 14983e230dd2SCorentin Chary if (down) 14993e230dd2SCorentin Chary vs->modifiers_state[keycode] ^= 1; 15003e230dd2SCorentin Chary break; 15013e230dd2SCorentin Chary } 15023e230dd2SCorentin Chary 15033e230dd2SCorentin Chary if (vs->vd->lock_key_sync && 15043e230dd2SCorentin Chary keycode_is_keypad(vs->vd->kbd_layout, keycode)) { 15053e230dd2SCorentin Chary /* If the numlock state needs to change then simulate an additional 15063e230dd2SCorentin Chary keypress before sending this one. This will happen if the user 15073e230dd2SCorentin Chary toggles numlock away from the VNC window. 15083e230dd2SCorentin Chary */ 15093e230dd2SCorentin Chary if (keysym_is_numlock(vs->vd->kbd_layout, sym & 0xFFFF)) { 15103e230dd2SCorentin Chary if (!vs->modifiers_state[0x45]) { 15113e230dd2SCorentin Chary vs->modifiers_state[0x45] = 1; 15123e230dd2SCorentin Chary press_key(vs, 0xff7f); 15133e230dd2SCorentin Chary } 15143e230dd2SCorentin Chary } else { 15153e230dd2SCorentin Chary if (vs->modifiers_state[0x45]) { 15163e230dd2SCorentin Chary vs->modifiers_state[0x45] = 0; 15173e230dd2SCorentin Chary press_key(vs, 0xff7f); 15183e230dd2SCorentin Chary } 15193e230dd2SCorentin Chary } 15203e230dd2SCorentin Chary } 15213e230dd2SCorentin Chary 15223e230dd2SCorentin Chary if (vs->vd->lock_key_sync && 15233e230dd2SCorentin Chary ((sym >= 'A' && sym <= 'Z') || (sym >= 'a' && sym <= 'z'))) { 15243e230dd2SCorentin Chary /* If the capslock state needs to change then simulate an additional 15253e230dd2SCorentin Chary keypress before sending this one. This will happen if the user 15263e230dd2SCorentin Chary toggles capslock away from the VNC window. 15273e230dd2SCorentin Chary */ 15283e230dd2SCorentin Chary int uppercase = !!(sym >= 'A' && sym <= 'Z'); 15293e230dd2SCorentin Chary int shift = !!(vs->modifiers_state[0x2a] | vs->modifiers_state[0x36]); 15303e230dd2SCorentin Chary int capslock = !!(vs->modifiers_state[0x3a]); 15313e230dd2SCorentin Chary if (capslock) { 15323e230dd2SCorentin Chary if (uppercase == shift) { 15333e230dd2SCorentin Chary vs->modifiers_state[0x3a] = 0; 15343e230dd2SCorentin Chary press_key(vs, 0xffe5); 15353e230dd2SCorentin Chary } 15363e230dd2SCorentin Chary } else { 15373e230dd2SCorentin Chary if (uppercase != shift) { 15383e230dd2SCorentin Chary vs->modifiers_state[0x3a] = 1; 15393e230dd2SCorentin Chary press_key(vs, 0xffe5); 15403e230dd2SCorentin Chary } 15413e230dd2SCorentin Chary } 15423e230dd2SCorentin Chary } 15433e230dd2SCorentin Chary 15443e230dd2SCorentin Chary if (is_graphic_console()) { 15453e230dd2SCorentin Chary if (keycode & SCANCODE_GREY) 15463e230dd2SCorentin Chary kbd_put_keycode(SCANCODE_EMUL0); 15473e230dd2SCorentin Chary if (down) 15483e230dd2SCorentin Chary kbd_put_keycode(keycode & SCANCODE_KEYCODEMASK); 15493e230dd2SCorentin Chary else 15503e230dd2SCorentin Chary kbd_put_keycode(keycode | SCANCODE_UP); 15513e230dd2SCorentin Chary } else { 15523e230dd2SCorentin Chary /* QEMU console emulation */ 15533e230dd2SCorentin Chary if (down) { 15543e230dd2SCorentin Chary int numlock = vs->modifiers_state[0x45]; 15553e230dd2SCorentin Chary switch (keycode) { 15563e230dd2SCorentin Chary case 0x2a: /* Left Shift */ 15573e230dd2SCorentin Chary case 0x36: /* Right Shift */ 15583e230dd2SCorentin Chary case 0x1d: /* Left CTRL */ 15593e230dd2SCorentin Chary case 0x9d: /* Right CTRL */ 15603e230dd2SCorentin Chary case 0x38: /* Left ALT */ 15613e230dd2SCorentin Chary case 0xb8: /* Right ALT */ 15623e230dd2SCorentin Chary break; 15633e230dd2SCorentin Chary case 0xc8: 15643e230dd2SCorentin Chary kbd_put_keysym(QEMU_KEY_UP); 15653e230dd2SCorentin Chary break; 15663e230dd2SCorentin Chary case 0xd0: 15673e230dd2SCorentin Chary kbd_put_keysym(QEMU_KEY_DOWN); 15683e230dd2SCorentin Chary break; 15693e230dd2SCorentin Chary case 0xcb: 15703e230dd2SCorentin Chary kbd_put_keysym(QEMU_KEY_LEFT); 15713e230dd2SCorentin Chary break; 15723e230dd2SCorentin Chary case 0xcd: 15733e230dd2SCorentin Chary kbd_put_keysym(QEMU_KEY_RIGHT); 15743e230dd2SCorentin Chary break; 15753e230dd2SCorentin Chary case 0xd3: 15763e230dd2SCorentin Chary kbd_put_keysym(QEMU_KEY_DELETE); 15773e230dd2SCorentin Chary break; 15783e230dd2SCorentin Chary case 0xc7: 15793e230dd2SCorentin Chary kbd_put_keysym(QEMU_KEY_HOME); 15803e230dd2SCorentin Chary break; 15813e230dd2SCorentin Chary case 0xcf: 15823e230dd2SCorentin Chary kbd_put_keysym(QEMU_KEY_END); 15833e230dd2SCorentin Chary break; 15843e230dd2SCorentin Chary case 0xc9: 15853e230dd2SCorentin Chary kbd_put_keysym(QEMU_KEY_PAGEUP); 15863e230dd2SCorentin Chary break; 15873e230dd2SCorentin Chary case 0xd1: 15883e230dd2SCorentin Chary kbd_put_keysym(QEMU_KEY_PAGEDOWN); 15893e230dd2SCorentin Chary break; 15903e230dd2SCorentin Chary 15913e230dd2SCorentin Chary case 0x47: 15923e230dd2SCorentin Chary kbd_put_keysym(numlock ? '7' : QEMU_KEY_HOME); 15933e230dd2SCorentin Chary break; 15943e230dd2SCorentin Chary case 0x48: 15953e230dd2SCorentin Chary kbd_put_keysym(numlock ? '8' : QEMU_KEY_UP); 15963e230dd2SCorentin Chary break; 15973e230dd2SCorentin Chary case 0x49: 15983e230dd2SCorentin Chary kbd_put_keysym(numlock ? '9' : QEMU_KEY_PAGEUP); 15993e230dd2SCorentin Chary break; 16003e230dd2SCorentin Chary case 0x4b: 16013e230dd2SCorentin Chary kbd_put_keysym(numlock ? '4' : QEMU_KEY_LEFT); 16023e230dd2SCorentin Chary break; 16033e230dd2SCorentin Chary case 0x4c: 16043e230dd2SCorentin Chary kbd_put_keysym('5'); 16053e230dd2SCorentin Chary break; 16063e230dd2SCorentin Chary case 0x4d: 16073e230dd2SCorentin Chary kbd_put_keysym(numlock ? '6' : QEMU_KEY_RIGHT); 16083e230dd2SCorentin Chary break; 16093e230dd2SCorentin Chary case 0x4f: 16103e230dd2SCorentin Chary kbd_put_keysym(numlock ? '1' : QEMU_KEY_END); 16113e230dd2SCorentin Chary break; 16123e230dd2SCorentin Chary case 0x50: 16133e230dd2SCorentin Chary kbd_put_keysym(numlock ? '2' : QEMU_KEY_DOWN); 16143e230dd2SCorentin Chary break; 16153e230dd2SCorentin Chary case 0x51: 16163e230dd2SCorentin Chary kbd_put_keysym(numlock ? '3' : QEMU_KEY_PAGEDOWN); 16173e230dd2SCorentin Chary break; 16183e230dd2SCorentin Chary case 0x52: 16193e230dd2SCorentin Chary kbd_put_keysym('0'); 16203e230dd2SCorentin Chary break; 16213e230dd2SCorentin Chary case 0x53: 16223e230dd2SCorentin Chary kbd_put_keysym(numlock ? '.' : QEMU_KEY_DELETE); 16233e230dd2SCorentin Chary break; 16243e230dd2SCorentin Chary 16253e230dd2SCorentin Chary case 0xb5: 16263e230dd2SCorentin Chary kbd_put_keysym('/'); 16273e230dd2SCorentin Chary break; 16283e230dd2SCorentin Chary case 0x37: 16293e230dd2SCorentin Chary kbd_put_keysym('*'); 16303e230dd2SCorentin Chary break; 16313e230dd2SCorentin Chary case 0x4a: 16323e230dd2SCorentin Chary kbd_put_keysym('-'); 16333e230dd2SCorentin Chary break; 16343e230dd2SCorentin Chary case 0x4e: 16353e230dd2SCorentin Chary kbd_put_keysym('+'); 16363e230dd2SCorentin Chary break; 16373e230dd2SCorentin Chary case 0x9c: 16383e230dd2SCorentin Chary kbd_put_keysym('\n'); 16393e230dd2SCorentin Chary break; 16403e230dd2SCorentin Chary 16413e230dd2SCorentin Chary default: 16423e230dd2SCorentin Chary kbd_put_keysym(sym); 16433e230dd2SCorentin Chary break; 16443e230dd2SCorentin Chary } 16453e230dd2SCorentin Chary } 16463e230dd2SCorentin Chary } 16473e230dd2SCorentin Chary } 16483e230dd2SCorentin Chary 16493e230dd2SCorentin Chary static void key_event(VncState *vs, int down, uint32_t sym) 16503e230dd2SCorentin Chary { 16513e230dd2SCorentin Chary int keycode; 16523e230dd2SCorentin Chary int lsym = sym; 16533e230dd2SCorentin Chary 16543e230dd2SCorentin Chary if (lsym >= 'A' && lsym <= 'Z' && is_graphic_console()) { 16553e230dd2SCorentin Chary lsym = lsym - 'A' + 'a'; 16563e230dd2SCorentin Chary } 16573e230dd2SCorentin Chary 16583e230dd2SCorentin Chary keycode = keysym2scancode(vs->vd->kbd_layout, lsym & 0xFFFF) & SCANCODE_KEYMASK; 16593e230dd2SCorentin Chary do_key_event(vs, down, keycode, sym); 16603e230dd2SCorentin Chary } 16613e230dd2SCorentin Chary 16623e230dd2SCorentin Chary static void ext_key_event(VncState *vs, int down, 16633e230dd2SCorentin Chary uint32_t sym, uint16_t keycode) 16643e230dd2SCorentin Chary { 16653e230dd2SCorentin Chary /* if the user specifies a keyboard layout, always use it */ 16663e230dd2SCorentin Chary if (keyboard_layout) 16673e230dd2SCorentin Chary key_event(vs, down, sym); 16683e230dd2SCorentin Chary else 16693e230dd2SCorentin Chary do_key_event(vs, down, keycode, sym); 16703e230dd2SCorentin Chary } 16713e230dd2SCorentin Chary 16723e230dd2SCorentin Chary static void framebuffer_update_request(VncState *vs, int incremental, 16733e230dd2SCorentin Chary int x_position, int y_position, 16743e230dd2SCorentin Chary int w, int h) 16753e230dd2SCorentin Chary { 16763e230dd2SCorentin Chary if (y_position > ds_get_height(vs->ds)) 16773e230dd2SCorentin Chary y_position = ds_get_height(vs->ds); 16783e230dd2SCorentin Chary if (y_position + h >= ds_get_height(vs->ds)) 16793e230dd2SCorentin Chary h = ds_get_height(vs->ds) - y_position; 16803e230dd2SCorentin Chary 16813e230dd2SCorentin Chary int i; 16823e230dd2SCorentin Chary vs->need_update = 1; 16833e230dd2SCorentin Chary if (!incremental) { 16843e230dd2SCorentin Chary vs->force_update = 1; 16853e230dd2SCorentin Chary for (i = 0; i < h; i++) { 16863e230dd2SCorentin Chary vnc_set_bits(vs->dirty[y_position + i], 16873e230dd2SCorentin Chary (ds_get_width(vs->ds) / 16), VNC_DIRTY_WORDS); 16883e230dd2SCorentin Chary } 16893e230dd2SCorentin Chary } 16903e230dd2SCorentin Chary } 16913e230dd2SCorentin Chary 16923e230dd2SCorentin Chary static void send_ext_key_event_ack(VncState *vs) 16933e230dd2SCorentin Chary { 1694bd023f95SCorentin Chary vnc_lock_output(vs); 16953e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE); 16963e230dd2SCorentin Chary vnc_write_u8(vs, 0); 16973e230dd2SCorentin Chary vnc_write_u16(vs, 1); 16983e230dd2SCorentin Chary vnc_framebuffer_update(vs, 0, 0, ds_get_width(vs->ds), ds_get_height(vs->ds), 16993e230dd2SCorentin Chary VNC_ENCODING_EXT_KEY_EVENT); 1700bd023f95SCorentin Chary vnc_unlock_output(vs); 17013e230dd2SCorentin Chary vnc_flush(vs); 17023e230dd2SCorentin Chary } 17033e230dd2SCorentin Chary 17043e230dd2SCorentin Chary static void send_ext_audio_ack(VncState *vs) 17053e230dd2SCorentin Chary { 1706bd023f95SCorentin Chary vnc_lock_output(vs); 17073e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE); 17083e230dd2SCorentin Chary vnc_write_u8(vs, 0); 17093e230dd2SCorentin Chary vnc_write_u16(vs, 1); 17103e230dd2SCorentin Chary vnc_framebuffer_update(vs, 0, 0, ds_get_width(vs->ds), ds_get_height(vs->ds), 17113e230dd2SCorentin Chary VNC_ENCODING_AUDIO); 1712bd023f95SCorentin Chary vnc_unlock_output(vs); 17133e230dd2SCorentin Chary vnc_flush(vs); 17143e230dd2SCorentin Chary } 17153e230dd2SCorentin Chary 17163e230dd2SCorentin Chary static void set_encodings(VncState *vs, int32_t *encodings, size_t n_encodings) 17173e230dd2SCorentin Chary { 17183e230dd2SCorentin Chary int i; 17193e230dd2SCorentin Chary unsigned int enc = 0; 17203e230dd2SCorentin Chary 17213e230dd2SCorentin Chary vs->features = 0; 17223e230dd2SCorentin Chary vs->vnc_encoding = 0; 1723d1af0e05SCorentin Chary vs->tight.compression = 9; 1724d1af0e05SCorentin Chary vs->tight.quality = -1; /* Lossless by default */ 17253e230dd2SCorentin Chary vs->absolute = -1; 17263e230dd2SCorentin Chary 17273e230dd2SCorentin Chary /* 17283e230dd2SCorentin Chary * Start from the end because the encodings are sent in order of preference. 17293e230dd2SCorentin Chary * This way the prefered encoding (first encoding defined in the array) 17303e230dd2SCorentin Chary * will be set at the end of the loop. 17313e230dd2SCorentin Chary */ 17323e230dd2SCorentin Chary for (i = n_encodings - 1; i >= 0; i--) { 17333e230dd2SCorentin Chary enc = encodings[i]; 17343e230dd2SCorentin Chary switch (enc) { 17353e230dd2SCorentin Chary case VNC_ENCODING_RAW: 17363e230dd2SCorentin Chary vs->vnc_encoding = enc; 17373e230dd2SCorentin Chary break; 17383e230dd2SCorentin Chary case VNC_ENCODING_COPYRECT: 17393e230dd2SCorentin Chary vs->features |= VNC_FEATURE_COPYRECT_MASK; 17403e230dd2SCorentin Chary break; 17413e230dd2SCorentin Chary case VNC_ENCODING_HEXTILE: 17423e230dd2SCorentin Chary vs->features |= VNC_FEATURE_HEXTILE_MASK; 17433e230dd2SCorentin Chary vs->vnc_encoding = enc; 17443e230dd2SCorentin Chary break; 17453e230dd2SCorentin Chary case VNC_ENCODING_TIGHT: 17463e230dd2SCorentin Chary vs->features |= VNC_FEATURE_TIGHT_MASK; 17473e230dd2SCorentin Chary vs->vnc_encoding = enc; 17483e230dd2SCorentin Chary break; 1749efe556adSCorentin Chary case VNC_ENCODING_TIGHT_PNG: 1750efe556adSCorentin Chary vs->features |= VNC_FEATURE_TIGHT_PNG_MASK; 1751efe556adSCorentin Chary vs->vnc_encoding = enc; 1752efe556adSCorentin Chary break; 17533e230dd2SCorentin Chary case VNC_ENCODING_ZLIB: 17543e230dd2SCorentin Chary vs->features |= VNC_FEATURE_ZLIB_MASK; 17553e230dd2SCorentin Chary vs->vnc_encoding = enc; 17563e230dd2SCorentin Chary break; 17573e230dd2SCorentin Chary case VNC_ENCODING_DESKTOPRESIZE: 17583e230dd2SCorentin Chary vs->features |= VNC_FEATURE_RESIZE_MASK; 17593e230dd2SCorentin Chary break; 17603e230dd2SCorentin Chary case VNC_ENCODING_POINTER_TYPE_CHANGE: 17613e230dd2SCorentin Chary vs->features |= VNC_FEATURE_POINTER_TYPE_CHANGE_MASK; 17623e230dd2SCorentin Chary break; 17633e230dd2SCorentin Chary case VNC_ENCODING_RICH_CURSOR: 17643e230dd2SCorentin Chary vs->features |= VNC_FEATURE_RICH_CURSOR_MASK; 17653e230dd2SCorentin Chary break; 17663e230dd2SCorentin Chary case VNC_ENCODING_EXT_KEY_EVENT: 17673e230dd2SCorentin Chary send_ext_key_event_ack(vs); 17683e230dd2SCorentin Chary break; 17693e230dd2SCorentin Chary case VNC_ENCODING_AUDIO: 17703e230dd2SCorentin Chary send_ext_audio_ack(vs); 17713e230dd2SCorentin Chary break; 17723e230dd2SCorentin Chary case VNC_ENCODING_WMVi: 17733e230dd2SCorentin Chary vs->features |= VNC_FEATURE_WMVI_MASK; 17743e230dd2SCorentin Chary break; 17753e230dd2SCorentin Chary case VNC_ENCODING_COMPRESSLEVEL0 ... VNC_ENCODING_COMPRESSLEVEL0 + 9: 1776d1af0e05SCorentin Chary vs->tight.compression = (enc & 0x0F); 17773e230dd2SCorentin Chary break; 17783e230dd2SCorentin Chary case VNC_ENCODING_QUALITYLEVEL0 ... VNC_ENCODING_QUALITYLEVEL0 + 9: 1779d1af0e05SCorentin Chary vs->tight.quality = (enc & 0x0F); 17803e230dd2SCorentin Chary break; 17813e230dd2SCorentin Chary default: 17823e230dd2SCorentin Chary VNC_DEBUG("Unknown encoding: %d (0x%.8x): %d\n", i, enc, enc); 17833e230dd2SCorentin Chary break; 17843e230dd2SCorentin Chary } 17853e230dd2SCorentin Chary } 17863e230dd2SCorentin Chary vnc_desktop_resize(vs); 17873e230dd2SCorentin Chary check_pointer_type_change(&vs->mouse_mode_notifier); 17883e230dd2SCorentin Chary } 17893e230dd2SCorentin Chary 17903e230dd2SCorentin Chary static void set_pixel_conversion(VncState *vs) 17913e230dd2SCorentin Chary { 17923e230dd2SCorentin Chary if ((vs->clientds.flags & QEMU_BIG_ENDIAN_FLAG) == 17933e230dd2SCorentin Chary (vs->ds->surface->flags & QEMU_BIG_ENDIAN_FLAG) && 17943e230dd2SCorentin Chary !memcmp(&(vs->clientds.pf), &(vs->ds->surface->pf), sizeof(PixelFormat))) { 17953e230dd2SCorentin Chary vs->write_pixels = vnc_write_pixels_copy; 17963e230dd2SCorentin Chary vnc_hextile_set_pixel_conversion(vs, 0); 17973e230dd2SCorentin Chary } else { 17983e230dd2SCorentin Chary vs->write_pixels = vnc_write_pixels_generic; 17993e230dd2SCorentin Chary vnc_hextile_set_pixel_conversion(vs, 1); 18003e230dd2SCorentin Chary } 18013e230dd2SCorentin Chary } 18023e230dd2SCorentin Chary 18033e230dd2SCorentin Chary static void set_pixel_format(VncState *vs, 18043e230dd2SCorentin Chary int bits_per_pixel, int depth, 18053e230dd2SCorentin Chary int big_endian_flag, int true_color_flag, 18063e230dd2SCorentin Chary int red_max, int green_max, int blue_max, 18073e230dd2SCorentin Chary int red_shift, int green_shift, int blue_shift) 18083e230dd2SCorentin Chary { 18093e230dd2SCorentin Chary if (!true_color_flag) { 18103e230dd2SCorentin Chary vnc_client_error(vs); 18113e230dd2SCorentin Chary return; 18123e230dd2SCorentin Chary } 18133e230dd2SCorentin Chary 18143e230dd2SCorentin Chary vs->clientds = *(vs->vd->guest.ds); 18153e230dd2SCorentin Chary vs->clientds.pf.rmax = red_max; 18163e230dd2SCorentin Chary count_bits(vs->clientds.pf.rbits, red_max); 18173e230dd2SCorentin Chary vs->clientds.pf.rshift = red_shift; 18183e230dd2SCorentin Chary vs->clientds.pf.rmask = red_max << red_shift; 18193e230dd2SCorentin Chary vs->clientds.pf.gmax = green_max; 18203e230dd2SCorentin Chary count_bits(vs->clientds.pf.gbits, green_max); 18213e230dd2SCorentin Chary vs->clientds.pf.gshift = green_shift; 18223e230dd2SCorentin Chary vs->clientds.pf.gmask = green_max << green_shift; 18233e230dd2SCorentin Chary vs->clientds.pf.bmax = blue_max; 18243e230dd2SCorentin Chary count_bits(vs->clientds.pf.bbits, blue_max); 18253e230dd2SCorentin Chary vs->clientds.pf.bshift = blue_shift; 18263e230dd2SCorentin Chary vs->clientds.pf.bmask = blue_max << blue_shift; 18273e230dd2SCorentin Chary vs->clientds.pf.bits_per_pixel = bits_per_pixel; 18283e230dd2SCorentin Chary vs->clientds.pf.bytes_per_pixel = bits_per_pixel / 8; 18293e230dd2SCorentin Chary vs->clientds.pf.depth = bits_per_pixel == 32 ? 24 : bits_per_pixel; 18303e230dd2SCorentin Chary vs->clientds.flags = big_endian_flag ? QEMU_BIG_ENDIAN_FLAG : 0x00; 18313e230dd2SCorentin Chary 18323e230dd2SCorentin Chary set_pixel_conversion(vs); 18333e230dd2SCorentin Chary 18343e230dd2SCorentin Chary vga_hw_invalidate(); 18353e230dd2SCorentin Chary vga_hw_update(); 18363e230dd2SCorentin Chary } 18373e230dd2SCorentin Chary 18383e230dd2SCorentin Chary static void pixel_format_message (VncState *vs) { 18393e230dd2SCorentin Chary char pad[3] = { 0, 0, 0 }; 18403e230dd2SCorentin Chary 18413e230dd2SCorentin Chary vnc_write_u8(vs, vs->ds->surface->pf.bits_per_pixel); /* bits-per-pixel */ 18423e230dd2SCorentin Chary vnc_write_u8(vs, vs->ds->surface->pf.depth); /* depth */ 18433e230dd2SCorentin Chary 18443e230dd2SCorentin Chary #ifdef HOST_WORDS_BIGENDIAN 18453e230dd2SCorentin Chary vnc_write_u8(vs, 1); /* big-endian-flag */ 18463e230dd2SCorentin Chary #else 18473e230dd2SCorentin Chary vnc_write_u8(vs, 0); /* big-endian-flag */ 18483e230dd2SCorentin Chary #endif 18493e230dd2SCorentin Chary vnc_write_u8(vs, 1); /* true-color-flag */ 18503e230dd2SCorentin Chary vnc_write_u16(vs, vs->ds->surface->pf.rmax); /* red-max */ 18513e230dd2SCorentin Chary vnc_write_u16(vs, vs->ds->surface->pf.gmax); /* green-max */ 18523e230dd2SCorentin Chary vnc_write_u16(vs, vs->ds->surface->pf.bmax); /* blue-max */ 18533e230dd2SCorentin Chary vnc_write_u8(vs, vs->ds->surface->pf.rshift); /* red-shift */ 18543e230dd2SCorentin Chary vnc_write_u8(vs, vs->ds->surface->pf.gshift); /* green-shift */ 18553e230dd2SCorentin Chary vnc_write_u8(vs, vs->ds->surface->pf.bshift); /* blue-shift */ 18563e230dd2SCorentin Chary 18573e230dd2SCorentin Chary vnc_hextile_set_pixel_conversion(vs, 0); 18583e230dd2SCorentin Chary 18593e230dd2SCorentin Chary vs->clientds = *(vs->ds->surface); 18603e230dd2SCorentin Chary vs->clientds.flags &= ~QEMU_ALLOCATED_FLAG; 18613e230dd2SCorentin Chary vs->write_pixels = vnc_write_pixels_copy; 18623e230dd2SCorentin Chary 18633e230dd2SCorentin Chary vnc_write(vs, pad, 3); /* padding */ 18643e230dd2SCorentin Chary } 18653e230dd2SCorentin Chary 18663e230dd2SCorentin Chary static void vnc_dpy_setdata(DisplayState *ds) 18673e230dd2SCorentin Chary { 18683e230dd2SCorentin Chary /* We don't have to do anything */ 18693e230dd2SCorentin Chary } 18703e230dd2SCorentin Chary 18713e230dd2SCorentin Chary static void vnc_colordepth(VncState *vs) 18723e230dd2SCorentin Chary { 18733e230dd2SCorentin Chary if (vnc_has_feature(vs, VNC_FEATURE_WMVI)) { 18743e230dd2SCorentin Chary /* Sending a WMVi message to notify the client*/ 1875bd023f95SCorentin Chary vnc_lock_output(vs); 18763e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE); 18773e230dd2SCorentin Chary vnc_write_u8(vs, 0); 18783e230dd2SCorentin Chary vnc_write_u16(vs, 1); /* number of rects */ 18793e230dd2SCorentin Chary vnc_framebuffer_update(vs, 0, 0, ds_get_width(vs->ds), 18803e230dd2SCorentin Chary ds_get_height(vs->ds), VNC_ENCODING_WMVi); 18813e230dd2SCorentin Chary pixel_format_message(vs); 1882bd023f95SCorentin Chary vnc_unlock_output(vs); 18833e230dd2SCorentin Chary vnc_flush(vs); 18843e230dd2SCorentin Chary } else { 18853e230dd2SCorentin Chary set_pixel_conversion(vs); 18863e230dd2SCorentin Chary } 18873e230dd2SCorentin Chary } 18883e230dd2SCorentin Chary 18893e230dd2SCorentin Chary static int protocol_client_msg(VncState *vs, uint8_t *data, size_t len) 18903e230dd2SCorentin Chary { 18913e230dd2SCorentin Chary int i; 18923e230dd2SCorentin Chary uint16_t limit; 18933e230dd2SCorentin Chary VncDisplay *vd = vs->vd; 18943e230dd2SCorentin Chary 18953e230dd2SCorentin Chary if (data[0] > 3) { 18963e230dd2SCorentin Chary vd->timer_interval = VNC_REFRESH_INTERVAL_BASE; 18973e230dd2SCorentin Chary if (!qemu_timer_expired(vd->timer, qemu_get_clock(rt_clock) + vd->timer_interval)) 18983e230dd2SCorentin Chary qemu_mod_timer(vd->timer, qemu_get_clock(rt_clock) + vd->timer_interval); 18993e230dd2SCorentin Chary } 19003e230dd2SCorentin Chary 19013e230dd2SCorentin Chary switch (data[0]) { 19023e230dd2SCorentin Chary case VNC_MSG_CLIENT_SET_PIXEL_FORMAT: 19033e230dd2SCorentin Chary if (len == 1) 19043e230dd2SCorentin Chary return 20; 19053e230dd2SCorentin Chary 19063e230dd2SCorentin Chary set_pixel_format(vs, read_u8(data, 4), read_u8(data, 5), 19073e230dd2SCorentin Chary read_u8(data, 6), read_u8(data, 7), 19083e230dd2SCorentin Chary read_u16(data, 8), read_u16(data, 10), 19093e230dd2SCorentin Chary read_u16(data, 12), read_u8(data, 14), 19103e230dd2SCorentin Chary read_u8(data, 15), read_u8(data, 16)); 19113e230dd2SCorentin Chary break; 19123e230dd2SCorentin Chary case VNC_MSG_CLIENT_SET_ENCODINGS: 19133e230dd2SCorentin Chary if (len == 1) 19143e230dd2SCorentin Chary return 4; 19153e230dd2SCorentin Chary 19163e230dd2SCorentin Chary if (len == 4) { 19173e230dd2SCorentin Chary limit = read_u16(data, 2); 19183e230dd2SCorentin Chary if (limit > 0) 19193e230dd2SCorentin Chary return 4 + (limit * 4); 19203e230dd2SCorentin Chary } else 19213e230dd2SCorentin Chary limit = read_u16(data, 2); 19223e230dd2SCorentin Chary 19233e230dd2SCorentin Chary for (i = 0; i < limit; i++) { 19243e230dd2SCorentin Chary int32_t val = read_s32(data, 4 + (i * 4)); 19253e230dd2SCorentin Chary memcpy(data + 4 + (i * 4), &val, sizeof(val)); 19263e230dd2SCorentin Chary } 19273e230dd2SCorentin Chary 19283e230dd2SCorentin Chary set_encodings(vs, (int32_t *)(data + 4), limit); 19293e230dd2SCorentin Chary break; 19303e230dd2SCorentin Chary case VNC_MSG_CLIENT_FRAMEBUFFER_UPDATE_REQUEST: 19313e230dd2SCorentin Chary if (len == 1) 19323e230dd2SCorentin Chary return 10; 19333e230dd2SCorentin Chary 19343e230dd2SCorentin Chary framebuffer_update_request(vs, 19353e230dd2SCorentin Chary read_u8(data, 1), read_u16(data, 2), read_u16(data, 4), 19363e230dd2SCorentin Chary read_u16(data, 6), read_u16(data, 8)); 19373e230dd2SCorentin Chary break; 19383e230dd2SCorentin Chary case VNC_MSG_CLIENT_KEY_EVENT: 19393e230dd2SCorentin Chary if (len == 1) 19403e230dd2SCorentin Chary return 8; 19413e230dd2SCorentin Chary 19423e230dd2SCorentin Chary key_event(vs, read_u8(data, 1), read_u32(data, 4)); 19433e230dd2SCorentin Chary break; 19443e230dd2SCorentin Chary case VNC_MSG_CLIENT_POINTER_EVENT: 19453e230dd2SCorentin Chary if (len == 1) 19463e230dd2SCorentin Chary return 6; 19473e230dd2SCorentin Chary 19483e230dd2SCorentin Chary pointer_event(vs, read_u8(data, 1), read_u16(data, 2), read_u16(data, 4)); 19493e230dd2SCorentin Chary break; 19503e230dd2SCorentin Chary case VNC_MSG_CLIENT_CUT_TEXT: 19513e230dd2SCorentin Chary if (len == 1) 19523e230dd2SCorentin Chary return 8; 19533e230dd2SCorentin Chary 19543e230dd2SCorentin Chary if (len == 8) { 19553e230dd2SCorentin Chary uint32_t dlen = read_u32(data, 4); 19563e230dd2SCorentin Chary if (dlen > 0) 19573e230dd2SCorentin Chary return 8 + dlen; 19583e230dd2SCorentin Chary } 19593e230dd2SCorentin Chary 19603e230dd2SCorentin Chary client_cut_text(vs, read_u32(data, 4), data + 8); 19613e230dd2SCorentin Chary break; 19623e230dd2SCorentin Chary case VNC_MSG_CLIENT_QEMU: 19633e230dd2SCorentin Chary if (len == 1) 19643e230dd2SCorentin Chary return 2; 19653e230dd2SCorentin Chary 19663e230dd2SCorentin Chary switch (read_u8(data, 1)) { 19673e230dd2SCorentin Chary case VNC_MSG_CLIENT_QEMU_EXT_KEY_EVENT: 19683e230dd2SCorentin Chary if (len == 2) 19693e230dd2SCorentin Chary return 12; 19703e230dd2SCorentin Chary 19713e230dd2SCorentin Chary ext_key_event(vs, read_u16(data, 2), 19723e230dd2SCorentin Chary read_u32(data, 4), read_u32(data, 8)); 19733e230dd2SCorentin Chary break; 19743e230dd2SCorentin Chary case VNC_MSG_CLIENT_QEMU_AUDIO: 19753e230dd2SCorentin Chary if (len == 2) 19763e230dd2SCorentin Chary return 4; 19773e230dd2SCorentin Chary 19783e230dd2SCorentin Chary switch (read_u16 (data, 2)) { 19793e230dd2SCorentin Chary case VNC_MSG_CLIENT_QEMU_AUDIO_ENABLE: 19803e230dd2SCorentin Chary audio_add(vs); 19813e230dd2SCorentin Chary break; 19823e230dd2SCorentin Chary case VNC_MSG_CLIENT_QEMU_AUDIO_DISABLE: 19833e230dd2SCorentin Chary audio_del(vs); 19843e230dd2SCorentin Chary break; 19853e230dd2SCorentin Chary case VNC_MSG_CLIENT_QEMU_AUDIO_SET_FORMAT: 19863e230dd2SCorentin Chary if (len == 4) 19873e230dd2SCorentin Chary return 10; 19883e230dd2SCorentin Chary switch (read_u8(data, 4)) { 19893e230dd2SCorentin Chary case 0: vs->as.fmt = AUD_FMT_U8; break; 19903e230dd2SCorentin Chary case 1: vs->as.fmt = AUD_FMT_S8; break; 19913e230dd2SCorentin Chary case 2: vs->as.fmt = AUD_FMT_U16; break; 19923e230dd2SCorentin Chary case 3: vs->as.fmt = AUD_FMT_S16; break; 19933e230dd2SCorentin Chary case 4: vs->as.fmt = AUD_FMT_U32; break; 19943e230dd2SCorentin Chary case 5: vs->as.fmt = AUD_FMT_S32; break; 19953e230dd2SCorentin Chary default: 19963e230dd2SCorentin Chary printf("Invalid audio format %d\n", read_u8(data, 4)); 19973e230dd2SCorentin Chary vnc_client_error(vs); 19983e230dd2SCorentin Chary break; 19993e230dd2SCorentin Chary } 20003e230dd2SCorentin Chary vs->as.nchannels = read_u8(data, 5); 20013e230dd2SCorentin Chary if (vs->as.nchannels != 1 && vs->as.nchannels != 2) { 20023e230dd2SCorentin Chary printf("Invalid audio channel coount %d\n", 20033e230dd2SCorentin Chary read_u8(data, 5)); 20043e230dd2SCorentin Chary vnc_client_error(vs); 20053e230dd2SCorentin Chary break; 20063e230dd2SCorentin Chary } 20073e230dd2SCorentin Chary vs->as.freq = read_u32(data, 6); 20083e230dd2SCorentin Chary break; 20093e230dd2SCorentin Chary default: 20103e230dd2SCorentin Chary printf ("Invalid audio message %d\n", read_u8(data, 4)); 20113e230dd2SCorentin Chary vnc_client_error(vs); 20123e230dd2SCorentin Chary break; 20133e230dd2SCorentin Chary } 20143e230dd2SCorentin Chary break; 20153e230dd2SCorentin Chary 20163e230dd2SCorentin Chary default: 20173e230dd2SCorentin Chary printf("Msg: %d\n", read_u16(data, 0)); 20183e230dd2SCorentin Chary vnc_client_error(vs); 20193e230dd2SCorentin Chary break; 20203e230dd2SCorentin Chary } 20213e230dd2SCorentin Chary break; 20223e230dd2SCorentin Chary default: 20233e230dd2SCorentin Chary printf("Msg: %d\n", data[0]); 20243e230dd2SCorentin Chary vnc_client_error(vs); 20253e230dd2SCorentin Chary break; 20263e230dd2SCorentin Chary } 20273e230dd2SCorentin Chary 20283e230dd2SCorentin Chary vnc_read_when(vs, protocol_client_msg, 1); 20293e230dd2SCorentin Chary return 0; 20303e230dd2SCorentin Chary } 20313e230dd2SCorentin Chary 20323e230dd2SCorentin Chary static int protocol_client_init(VncState *vs, uint8_t *data, size_t len) 20333e230dd2SCorentin Chary { 20343e230dd2SCorentin Chary char buf[1024]; 20353e230dd2SCorentin Chary int size; 20363e230dd2SCorentin Chary 20373e230dd2SCorentin Chary vs->client_width = ds_get_width(vs->ds); 20383e230dd2SCorentin Chary vs->client_height = ds_get_height(vs->ds); 20393e230dd2SCorentin Chary vnc_write_u16(vs, vs->client_width); 20403e230dd2SCorentin Chary vnc_write_u16(vs, vs->client_height); 20413e230dd2SCorentin Chary 20423e230dd2SCorentin Chary pixel_format_message(vs); 20433e230dd2SCorentin Chary 20443e230dd2SCorentin Chary if (qemu_name) 20453e230dd2SCorentin Chary size = snprintf(buf, sizeof(buf), "QEMU (%s)", qemu_name); 20463e230dd2SCorentin Chary else 20473e230dd2SCorentin Chary size = snprintf(buf, sizeof(buf), "QEMU"); 20483e230dd2SCorentin Chary 20493e230dd2SCorentin Chary vnc_write_u32(vs, size); 20503e230dd2SCorentin Chary vnc_write(vs, buf, size); 20513e230dd2SCorentin Chary vnc_flush(vs); 20523e230dd2SCorentin Chary 20533e230dd2SCorentin Chary vnc_client_cache_auth(vs); 20543e230dd2SCorentin Chary vnc_qmp_event(vs, QEVENT_VNC_INITIALIZED); 20553e230dd2SCorentin Chary 20563e230dd2SCorentin Chary vnc_read_when(vs, protocol_client_msg, 1); 20573e230dd2SCorentin Chary 20583e230dd2SCorentin Chary return 0; 20593e230dd2SCorentin Chary } 20603e230dd2SCorentin Chary 20613e230dd2SCorentin Chary void start_client_init(VncState *vs) 20623e230dd2SCorentin Chary { 20633e230dd2SCorentin Chary vnc_read_when(vs, protocol_client_init, 1); 20643e230dd2SCorentin Chary } 20653e230dd2SCorentin Chary 20663e230dd2SCorentin Chary static void make_challenge(VncState *vs) 20673e230dd2SCorentin Chary { 20683e230dd2SCorentin Chary int i; 20693e230dd2SCorentin Chary 20703e230dd2SCorentin Chary srand(time(NULL)+getpid()+getpid()*987654+rand()); 20713e230dd2SCorentin Chary 20723e230dd2SCorentin Chary for (i = 0 ; i < sizeof(vs->challenge) ; i++) 20733e230dd2SCorentin Chary vs->challenge[i] = (int) (256.0*rand()/(RAND_MAX+1.0)); 20743e230dd2SCorentin Chary } 20753e230dd2SCorentin Chary 20763e230dd2SCorentin Chary static int protocol_client_auth_vnc(VncState *vs, uint8_t *data, size_t len) 20773e230dd2SCorentin Chary { 20783e230dd2SCorentin Chary unsigned char response[VNC_AUTH_CHALLENGE_SIZE]; 20793e230dd2SCorentin Chary int i, j, pwlen; 20803e230dd2SCorentin Chary unsigned char key[8]; 20813e230dd2SCorentin Chary 20823e230dd2SCorentin Chary if (!vs->vd->password || !vs->vd->password[0]) { 20833e230dd2SCorentin Chary VNC_DEBUG("No password configured on server"); 20843e230dd2SCorentin Chary vnc_write_u32(vs, 1); /* Reject auth */ 20853e230dd2SCorentin Chary if (vs->minor >= 8) { 20863e230dd2SCorentin Chary static const char err[] = "Authentication failed"; 20873e230dd2SCorentin Chary vnc_write_u32(vs, sizeof(err)); 20883e230dd2SCorentin Chary vnc_write(vs, err, sizeof(err)); 20893e230dd2SCorentin Chary } 20903e230dd2SCorentin Chary vnc_flush(vs); 20913e230dd2SCorentin Chary vnc_client_error(vs); 20923e230dd2SCorentin Chary return 0; 20933e230dd2SCorentin Chary } 20943e230dd2SCorentin Chary 20953e230dd2SCorentin Chary memcpy(response, vs->challenge, VNC_AUTH_CHALLENGE_SIZE); 20963e230dd2SCorentin Chary 20973e230dd2SCorentin Chary /* Calculate the expected challenge response */ 20983e230dd2SCorentin Chary pwlen = strlen(vs->vd->password); 20993e230dd2SCorentin Chary for (i=0; i<sizeof(key); i++) 21003e230dd2SCorentin Chary key[i] = i<pwlen ? vs->vd->password[i] : 0; 21013e230dd2SCorentin Chary deskey(key, EN0); 21023e230dd2SCorentin Chary for (j = 0; j < VNC_AUTH_CHALLENGE_SIZE; j += 8) 21033e230dd2SCorentin Chary des(response+j, response+j); 21043e230dd2SCorentin Chary 21053e230dd2SCorentin Chary /* Compare expected vs actual challenge response */ 21063e230dd2SCorentin Chary if (memcmp(response, data, VNC_AUTH_CHALLENGE_SIZE) != 0) { 21073e230dd2SCorentin Chary VNC_DEBUG("Client challenge reponse did not match\n"); 21083e230dd2SCorentin Chary vnc_write_u32(vs, 1); /* Reject auth */ 21093e230dd2SCorentin Chary if (vs->minor >= 8) { 21103e230dd2SCorentin Chary static const char err[] = "Authentication failed"; 21113e230dd2SCorentin Chary vnc_write_u32(vs, sizeof(err)); 21123e230dd2SCorentin Chary vnc_write(vs, err, sizeof(err)); 21133e230dd2SCorentin Chary } 21143e230dd2SCorentin Chary vnc_flush(vs); 21153e230dd2SCorentin Chary vnc_client_error(vs); 21163e230dd2SCorentin Chary } else { 21173e230dd2SCorentin Chary VNC_DEBUG("Accepting VNC challenge response\n"); 21183e230dd2SCorentin Chary vnc_write_u32(vs, 0); /* Accept auth */ 21193e230dd2SCorentin Chary vnc_flush(vs); 21203e230dd2SCorentin Chary 21213e230dd2SCorentin Chary start_client_init(vs); 21223e230dd2SCorentin Chary } 21233e230dd2SCorentin Chary return 0; 21243e230dd2SCorentin Chary } 21253e230dd2SCorentin Chary 21263e230dd2SCorentin Chary void start_auth_vnc(VncState *vs) 21273e230dd2SCorentin Chary { 21283e230dd2SCorentin Chary make_challenge(vs); 21293e230dd2SCorentin Chary /* Send client a 'random' challenge */ 21303e230dd2SCorentin Chary vnc_write(vs, vs->challenge, sizeof(vs->challenge)); 21313e230dd2SCorentin Chary vnc_flush(vs); 21323e230dd2SCorentin Chary 21333e230dd2SCorentin Chary vnc_read_when(vs, protocol_client_auth_vnc, sizeof(vs->challenge)); 21343e230dd2SCorentin Chary } 21353e230dd2SCorentin Chary 21363e230dd2SCorentin Chary 21373e230dd2SCorentin Chary static int protocol_client_auth(VncState *vs, uint8_t *data, size_t len) 21383e230dd2SCorentin Chary { 21393e230dd2SCorentin Chary /* We only advertise 1 auth scheme at a time, so client 21403e230dd2SCorentin Chary * must pick the one we sent. Verify this */ 21413e230dd2SCorentin Chary if (data[0] != vs->vd->auth) { /* Reject auth */ 21423e230dd2SCorentin Chary VNC_DEBUG("Reject auth %d because it didn't match advertized\n", (int)data[0]); 21433e230dd2SCorentin Chary vnc_write_u32(vs, 1); 21443e230dd2SCorentin Chary if (vs->minor >= 8) { 21453e230dd2SCorentin Chary static const char err[] = "Authentication failed"; 21463e230dd2SCorentin Chary vnc_write_u32(vs, sizeof(err)); 21473e230dd2SCorentin Chary vnc_write(vs, err, sizeof(err)); 21483e230dd2SCorentin Chary } 21493e230dd2SCorentin Chary vnc_client_error(vs); 21503e230dd2SCorentin Chary } else { /* Accept requested auth */ 21513e230dd2SCorentin Chary VNC_DEBUG("Client requested auth %d\n", (int)data[0]); 21523e230dd2SCorentin Chary switch (vs->vd->auth) { 21533e230dd2SCorentin Chary case VNC_AUTH_NONE: 21543e230dd2SCorentin Chary VNC_DEBUG("Accept auth none\n"); 21553e230dd2SCorentin Chary if (vs->minor >= 8) { 21563e230dd2SCorentin Chary vnc_write_u32(vs, 0); /* Accept auth completion */ 21573e230dd2SCorentin Chary vnc_flush(vs); 21583e230dd2SCorentin Chary } 21593e230dd2SCorentin Chary start_client_init(vs); 21603e230dd2SCorentin Chary break; 21613e230dd2SCorentin Chary 21623e230dd2SCorentin Chary case VNC_AUTH_VNC: 21633e230dd2SCorentin Chary VNC_DEBUG("Start VNC auth\n"); 21643e230dd2SCorentin Chary start_auth_vnc(vs); 21653e230dd2SCorentin Chary break; 21663e230dd2SCorentin Chary 21673e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS 21683e230dd2SCorentin Chary case VNC_AUTH_VENCRYPT: 21693e230dd2SCorentin Chary VNC_DEBUG("Accept VeNCrypt auth\n");; 21703e230dd2SCorentin Chary start_auth_vencrypt(vs); 21713e230dd2SCorentin Chary break; 21723e230dd2SCorentin Chary #endif /* CONFIG_VNC_TLS */ 21733e230dd2SCorentin Chary 21743e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL 21753e230dd2SCorentin Chary case VNC_AUTH_SASL: 21763e230dd2SCorentin Chary VNC_DEBUG("Accept SASL auth\n"); 21773e230dd2SCorentin Chary start_auth_sasl(vs); 21783e230dd2SCorentin Chary break; 21793e230dd2SCorentin Chary #endif /* CONFIG_VNC_SASL */ 21803e230dd2SCorentin Chary 21813e230dd2SCorentin Chary default: /* Should not be possible, but just in case */ 21823e230dd2SCorentin Chary VNC_DEBUG("Reject auth %d server code bug\n", vs->vd->auth); 21833e230dd2SCorentin Chary vnc_write_u8(vs, 1); 21843e230dd2SCorentin Chary if (vs->minor >= 8) { 21853e230dd2SCorentin Chary static const char err[] = "Authentication failed"; 21863e230dd2SCorentin Chary vnc_write_u32(vs, sizeof(err)); 21873e230dd2SCorentin Chary vnc_write(vs, err, sizeof(err)); 21883e230dd2SCorentin Chary } 21893e230dd2SCorentin Chary vnc_client_error(vs); 21903e230dd2SCorentin Chary } 21913e230dd2SCorentin Chary } 21923e230dd2SCorentin Chary return 0; 21933e230dd2SCorentin Chary } 21943e230dd2SCorentin Chary 21953e230dd2SCorentin Chary static int protocol_version(VncState *vs, uint8_t *version, size_t len) 21963e230dd2SCorentin Chary { 21973e230dd2SCorentin Chary char local[13]; 21983e230dd2SCorentin Chary 21993e230dd2SCorentin Chary memcpy(local, version, 12); 22003e230dd2SCorentin Chary local[12] = 0; 22013e230dd2SCorentin Chary 22023e230dd2SCorentin Chary if (sscanf(local, "RFB %03d.%03d\n", &vs->major, &vs->minor) != 2) { 22033e230dd2SCorentin Chary VNC_DEBUG("Malformed protocol version %s\n", local); 22043e230dd2SCorentin Chary vnc_client_error(vs); 22053e230dd2SCorentin Chary return 0; 22063e230dd2SCorentin Chary } 22073e230dd2SCorentin Chary VNC_DEBUG("Client request protocol version %d.%d\n", vs->major, vs->minor); 22083e230dd2SCorentin Chary if (vs->major != 3 || 22093e230dd2SCorentin Chary (vs->minor != 3 && 22103e230dd2SCorentin Chary vs->minor != 4 && 22113e230dd2SCorentin Chary vs->minor != 5 && 22123e230dd2SCorentin Chary vs->minor != 7 && 22133e230dd2SCorentin Chary vs->minor != 8)) { 22143e230dd2SCorentin Chary VNC_DEBUG("Unsupported client version\n"); 22153e230dd2SCorentin Chary vnc_write_u32(vs, VNC_AUTH_INVALID); 22163e230dd2SCorentin Chary vnc_flush(vs); 22173e230dd2SCorentin Chary vnc_client_error(vs); 22183e230dd2SCorentin Chary return 0; 22193e230dd2SCorentin Chary } 22203e230dd2SCorentin Chary /* Some broken clients report v3.4 or v3.5, which spec requires to be treated 22213e230dd2SCorentin Chary * as equivalent to v3.3 by servers 22223e230dd2SCorentin Chary */ 22233e230dd2SCorentin Chary if (vs->minor == 4 || vs->minor == 5) 22243e230dd2SCorentin Chary vs->minor = 3; 22253e230dd2SCorentin Chary 22263e230dd2SCorentin Chary if (vs->minor == 3) { 22273e230dd2SCorentin Chary if (vs->vd->auth == VNC_AUTH_NONE) { 22283e230dd2SCorentin Chary VNC_DEBUG("Tell client auth none\n"); 22293e230dd2SCorentin Chary vnc_write_u32(vs, vs->vd->auth); 22303e230dd2SCorentin Chary vnc_flush(vs); 22313e230dd2SCorentin Chary start_client_init(vs); 22323e230dd2SCorentin Chary } else if (vs->vd->auth == VNC_AUTH_VNC) { 22333e230dd2SCorentin Chary VNC_DEBUG("Tell client VNC auth\n"); 22343e230dd2SCorentin Chary vnc_write_u32(vs, vs->vd->auth); 22353e230dd2SCorentin Chary vnc_flush(vs); 22363e230dd2SCorentin Chary start_auth_vnc(vs); 22373e230dd2SCorentin Chary } else { 22383e230dd2SCorentin Chary VNC_DEBUG("Unsupported auth %d for protocol 3.3\n", vs->vd->auth); 22393e230dd2SCorentin Chary vnc_write_u32(vs, VNC_AUTH_INVALID); 22403e230dd2SCorentin Chary vnc_flush(vs); 22413e230dd2SCorentin Chary vnc_client_error(vs); 22423e230dd2SCorentin Chary } 22433e230dd2SCorentin Chary } else { 22443e230dd2SCorentin Chary VNC_DEBUG("Telling client we support auth %d\n", vs->vd->auth); 22453e230dd2SCorentin Chary vnc_write_u8(vs, 1); /* num auth */ 22463e230dd2SCorentin Chary vnc_write_u8(vs, vs->vd->auth); 22473e230dd2SCorentin Chary vnc_read_when(vs, protocol_client_auth, 1); 22483e230dd2SCorentin Chary vnc_flush(vs); 22493e230dd2SCorentin Chary } 22503e230dd2SCorentin Chary 22513e230dd2SCorentin Chary return 0; 22523e230dd2SCorentin Chary } 22533e230dd2SCorentin Chary 22543e230dd2SCorentin Chary static int vnc_refresh_server_surface(VncDisplay *vd) 22553e230dd2SCorentin Chary { 22563e230dd2SCorentin Chary int y; 22573e230dd2SCorentin Chary uint8_t *guest_row; 22583e230dd2SCorentin Chary uint8_t *server_row; 22593e230dd2SCorentin Chary int cmp_bytes; 22603e230dd2SCorentin Chary uint32_t width_mask[VNC_DIRTY_WORDS]; 22613e230dd2SCorentin Chary VncState *vs; 22623e230dd2SCorentin Chary int has_dirty = 0; 22633e230dd2SCorentin Chary 22643e230dd2SCorentin Chary /* 22653e230dd2SCorentin Chary * Walk through the guest dirty map. 22663e230dd2SCorentin Chary * Check and copy modified bits from guest to server surface. 22673e230dd2SCorentin Chary * Update server dirty map. 22683e230dd2SCorentin Chary */ 22693e230dd2SCorentin Chary vnc_set_bits(width_mask, (ds_get_width(vd->ds) / 16), VNC_DIRTY_WORDS); 22703e230dd2SCorentin Chary cmp_bytes = 16 * ds_get_bytes_per_pixel(vd->ds); 22713e230dd2SCorentin Chary guest_row = vd->guest.ds->data; 22723e230dd2SCorentin Chary server_row = vd->server->data; 22733e230dd2SCorentin Chary for (y = 0; y < vd->guest.ds->height; y++) { 22743e230dd2SCorentin Chary if (vnc_and_bits(vd->guest.dirty[y], width_mask, VNC_DIRTY_WORDS)) { 22753e230dd2SCorentin Chary int x; 22763e230dd2SCorentin Chary uint8_t *guest_ptr; 22773e230dd2SCorentin Chary uint8_t *server_ptr; 22783e230dd2SCorentin Chary 22793e230dd2SCorentin Chary guest_ptr = guest_row; 22803e230dd2SCorentin Chary server_ptr = server_row; 22813e230dd2SCorentin Chary 22823e230dd2SCorentin Chary for (x = 0; x < vd->guest.ds->width; 22833e230dd2SCorentin Chary x += 16, guest_ptr += cmp_bytes, server_ptr += cmp_bytes) { 22843e230dd2SCorentin Chary if (!vnc_get_bit(vd->guest.dirty[y], (x / 16))) 22853e230dd2SCorentin Chary continue; 22863e230dd2SCorentin Chary vnc_clear_bit(vd->guest.dirty[y], (x / 16)); 22873e230dd2SCorentin Chary if (memcmp(server_ptr, guest_ptr, cmp_bytes) == 0) 22883e230dd2SCorentin Chary continue; 22893e230dd2SCorentin Chary memcpy(server_ptr, guest_ptr, cmp_bytes); 22903e230dd2SCorentin Chary QTAILQ_FOREACH(vs, &vd->clients, next) { 22913e230dd2SCorentin Chary vnc_set_bit(vs->dirty[y], (x / 16)); 22923e230dd2SCorentin Chary } 22933e230dd2SCorentin Chary has_dirty++; 22943e230dd2SCorentin Chary } 22953e230dd2SCorentin Chary } 22963e230dd2SCorentin Chary guest_row += ds_get_linesize(vd->ds); 22973e230dd2SCorentin Chary server_row += ds_get_linesize(vd->ds); 22983e230dd2SCorentin Chary } 22993e230dd2SCorentin Chary return has_dirty; 23003e230dd2SCorentin Chary } 23013e230dd2SCorentin Chary 23023e230dd2SCorentin Chary static void vnc_refresh(void *opaque) 23033e230dd2SCorentin Chary { 23043e230dd2SCorentin Chary VncDisplay *vd = opaque; 23053e230dd2SCorentin Chary VncState *vs, *vn; 23063e230dd2SCorentin Chary int has_dirty, rects = 0; 23073e230dd2SCorentin Chary 23083e230dd2SCorentin Chary vga_hw_update(); 23093e230dd2SCorentin Chary 2310bd023f95SCorentin Chary if (vnc_trylock_display(vd)) { 2311bd023f95SCorentin Chary vd->timer_interval = VNC_REFRESH_INTERVAL_BASE; 2312bd023f95SCorentin Chary qemu_mod_timer(vd->timer, qemu_get_clock(rt_clock) + 2313bd023f95SCorentin Chary vd->timer_interval); 2314bd023f95SCorentin Chary return; 2315bd023f95SCorentin Chary } 2316bd023f95SCorentin Chary 23173e230dd2SCorentin Chary has_dirty = vnc_refresh_server_surface(vd); 2318bd023f95SCorentin Chary vnc_unlock_display(vd); 23193e230dd2SCorentin Chary 23203e230dd2SCorentin Chary QTAILQ_FOREACH_SAFE(vs, &vd->clients, next, vn) { 23213e230dd2SCorentin Chary rects += vnc_update_client(vs, has_dirty); 23223e230dd2SCorentin Chary /* vs might be free()ed here */ 23233e230dd2SCorentin Chary } 2324bd023f95SCorentin Chary 23253e230dd2SCorentin Chary /* vd->timer could be NULL now if the last client disconnected, 23263e230dd2SCorentin Chary * in this case don't update the timer */ 23273e230dd2SCorentin Chary if (vd->timer == NULL) 23283e230dd2SCorentin Chary return; 23293e230dd2SCorentin Chary 23303e230dd2SCorentin Chary if (has_dirty && rects) { 23313e230dd2SCorentin Chary vd->timer_interval /= 2; 23323e230dd2SCorentin Chary if (vd->timer_interval < VNC_REFRESH_INTERVAL_BASE) 23333e230dd2SCorentin Chary vd->timer_interval = VNC_REFRESH_INTERVAL_BASE; 23343e230dd2SCorentin Chary } else { 23353e230dd2SCorentin Chary vd->timer_interval += VNC_REFRESH_INTERVAL_INC; 23363e230dd2SCorentin Chary if (vd->timer_interval > VNC_REFRESH_INTERVAL_MAX) 23373e230dd2SCorentin Chary vd->timer_interval = VNC_REFRESH_INTERVAL_MAX; 23383e230dd2SCorentin Chary } 23393e230dd2SCorentin Chary qemu_mod_timer(vd->timer, qemu_get_clock(rt_clock) + vd->timer_interval); 23403e230dd2SCorentin Chary } 23413e230dd2SCorentin Chary 23423e230dd2SCorentin Chary static void vnc_init_timer(VncDisplay *vd) 23433e230dd2SCorentin Chary { 23443e230dd2SCorentin Chary vd->timer_interval = VNC_REFRESH_INTERVAL_BASE; 23453e230dd2SCorentin Chary if (vd->timer == NULL && !QTAILQ_EMPTY(&vd->clients)) { 23463e230dd2SCorentin Chary vd->timer = qemu_new_timer(rt_clock, vnc_refresh, vd); 23473e230dd2SCorentin Chary vnc_refresh(vd); 23483e230dd2SCorentin Chary } 23493e230dd2SCorentin Chary } 23503e230dd2SCorentin Chary 23513e230dd2SCorentin Chary static void vnc_remove_timer(VncDisplay *vd) 23523e230dd2SCorentin Chary { 23533e230dd2SCorentin Chary if (vd->timer != NULL && QTAILQ_EMPTY(&vd->clients)) { 23543e230dd2SCorentin Chary qemu_del_timer(vd->timer); 23553e230dd2SCorentin Chary qemu_free_timer(vd->timer); 23563e230dd2SCorentin Chary vd->timer = NULL; 23573e230dd2SCorentin Chary } 23583e230dd2SCorentin Chary } 23593e230dd2SCorentin Chary 23603e230dd2SCorentin Chary static void vnc_connect(VncDisplay *vd, int csock) 23613e230dd2SCorentin Chary { 23623e230dd2SCorentin Chary VncState *vs = qemu_mallocz(sizeof(VncState)); 23633e230dd2SCorentin Chary vs->csock = csock; 23643e230dd2SCorentin Chary 23653e230dd2SCorentin Chary VNC_DEBUG("New client on socket %d\n", csock); 23663e230dd2SCorentin Chary dcl->idle = 0; 23673e230dd2SCorentin Chary socket_set_nonblock(vs->csock); 23683e230dd2SCorentin Chary qemu_set_fd_handler2(vs->csock, NULL, vnc_client_read, NULL, vs); 23693e230dd2SCorentin Chary 23703e230dd2SCorentin Chary vnc_client_cache_addr(vs); 23713e230dd2SCorentin Chary vnc_qmp_event(vs, QEVENT_VNC_CONNECTED); 23723e230dd2SCorentin Chary 23733e230dd2SCorentin Chary vs->vd = vd; 23743e230dd2SCorentin Chary vs->ds = vd->ds; 23753e230dd2SCorentin Chary vs->last_x = -1; 23763e230dd2SCorentin Chary vs->last_y = -1; 23773e230dd2SCorentin Chary 23783e230dd2SCorentin Chary vs->as.freq = 44100; 23793e230dd2SCorentin Chary vs->as.nchannels = 2; 23803e230dd2SCorentin Chary vs->as.fmt = AUD_FMT_S16; 23813e230dd2SCorentin Chary vs->as.endianness = 0; 23823e230dd2SCorentin Chary 2383bd023f95SCorentin Chary #ifdef CONFIG_VNC_THREAD 2384bd023f95SCorentin Chary qemu_mutex_init(&vs->output_mutex); 2385bd023f95SCorentin Chary #endif 2386bd023f95SCorentin Chary 23873e230dd2SCorentin Chary QTAILQ_INSERT_HEAD(&vd->clients, vs, next); 23883e230dd2SCorentin Chary 23893e230dd2SCorentin Chary vga_hw_update(); 23903e230dd2SCorentin Chary 23913e230dd2SCorentin Chary vnc_write(vs, "RFB 003.008\n", 12); 23923e230dd2SCorentin Chary vnc_flush(vs); 23933e230dd2SCorentin Chary vnc_read_when(vs, protocol_version, 12); 23943e230dd2SCorentin Chary reset_keys(vs); 23953e230dd2SCorentin Chary if (vs->vd->lock_key_sync) 23963e230dd2SCorentin Chary vs->led = qemu_add_led_event_handler(kbd_leds, vs); 23973e230dd2SCorentin Chary 23983e230dd2SCorentin Chary vs->mouse_mode_notifier.notify = check_pointer_type_change; 23993e230dd2SCorentin Chary qemu_add_mouse_mode_change_notifier(&vs->mouse_mode_notifier); 24003e230dd2SCorentin Chary 24013e230dd2SCorentin Chary vnc_init_timer(vd); 24023e230dd2SCorentin Chary 24033e230dd2SCorentin Chary /* vs might be free()ed here */ 24043e230dd2SCorentin Chary } 24053e230dd2SCorentin Chary 24063e230dd2SCorentin Chary static void vnc_listen_read(void *opaque) 24073e230dd2SCorentin Chary { 24083e230dd2SCorentin Chary VncDisplay *vs = opaque; 24093e230dd2SCorentin Chary struct sockaddr_in addr; 24103e230dd2SCorentin Chary socklen_t addrlen = sizeof(addr); 24113e230dd2SCorentin Chary 24123e230dd2SCorentin Chary /* Catch-up */ 24133e230dd2SCorentin Chary vga_hw_update(); 24143e230dd2SCorentin Chary 24153e230dd2SCorentin Chary int csock = qemu_accept(vs->lsock, (struct sockaddr *)&addr, &addrlen); 24163e230dd2SCorentin Chary if (csock != -1) { 24173e230dd2SCorentin Chary vnc_connect(vs, csock); 24183e230dd2SCorentin Chary } 24193e230dd2SCorentin Chary } 24203e230dd2SCorentin Chary 24213e230dd2SCorentin Chary void vnc_display_init(DisplayState *ds) 24223e230dd2SCorentin Chary { 24233e230dd2SCorentin Chary VncDisplay *vs = qemu_mallocz(sizeof(*vs)); 24243e230dd2SCorentin Chary 24253e230dd2SCorentin Chary dcl = qemu_mallocz(sizeof(DisplayChangeListener)); 24263e230dd2SCorentin Chary 24273e230dd2SCorentin Chary ds->opaque = vs; 24283e230dd2SCorentin Chary dcl->idle = 1; 24293e230dd2SCorentin Chary vnc_display = vs; 24303e230dd2SCorentin Chary 24313e230dd2SCorentin Chary vs->lsock = -1; 24323e230dd2SCorentin Chary 24333e230dd2SCorentin Chary vs->ds = ds; 24343e230dd2SCorentin Chary QTAILQ_INIT(&vs->clients); 24353e230dd2SCorentin Chary 24363e230dd2SCorentin Chary if (keyboard_layout) 24373e230dd2SCorentin Chary vs->kbd_layout = init_keyboard_layout(name2keysym, keyboard_layout); 24383e230dd2SCorentin Chary else 24393e230dd2SCorentin Chary vs->kbd_layout = init_keyboard_layout(name2keysym, "en-us"); 24403e230dd2SCorentin Chary 24413e230dd2SCorentin Chary if (!vs->kbd_layout) 24423e230dd2SCorentin Chary exit(1); 24433e230dd2SCorentin Chary 2444bd023f95SCorentin Chary #ifdef CONFIG_VNC_THREAD 2445bd023f95SCorentin Chary qemu_mutex_init(&vs->mutex); 2446bd023f95SCorentin Chary vnc_start_worker_thread(); 2447bd023f95SCorentin Chary #endif 2448bd023f95SCorentin Chary 24493e230dd2SCorentin Chary dcl->dpy_copy = vnc_dpy_copy; 24503e230dd2SCorentin Chary dcl->dpy_update = vnc_dpy_update; 24513e230dd2SCorentin Chary dcl->dpy_resize = vnc_dpy_resize; 24523e230dd2SCorentin Chary dcl->dpy_setdata = vnc_dpy_setdata; 24533e230dd2SCorentin Chary register_displaychangelistener(ds, dcl); 24543e230dd2SCorentin Chary ds->mouse_set = vnc_mouse_set; 24553e230dd2SCorentin Chary ds->cursor_define = vnc_dpy_cursor_define; 24563e230dd2SCorentin Chary } 24573e230dd2SCorentin Chary 24583e230dd2SCorentin Chary 24593e230dd2SCorentin Chary void vnc_display_close(DisplayState *ds) 24603e230dd2SCorentin Chary { 24613e230dd2SCorentin Chary VncDisplay *vs = ds ? (VncDisplay *)ds->opaque : vnc_display; 24623e230dd2SCorentin Chary 24633e230dd2SCorentin Chary if (!vs) 24643e230dd2SCorentin Chary return; 24653e230dd2SCorentin Chary if (vs->display) { 24663e230dd2SCorentin Chary qemu_free(vs->display); 24673e230dd2SCorentin Chary vs->display = NULL; 24683e230dd2SCorentin Chary } 24693e230dd2SCorentin Chary if (vs->lsock != -1) { 24703e230dd2SCorentin Chary qemu_set_fd_handler2(vs->lsock, NULL, NULL, NULL, NULL); 24713e230dd2SCorentin Chary close(vs->lsock); 24723e230dd2SCorentin Chary vs->lsock = -1; 24733e230dd2SCorentin Chary } 24743e230dd2SCorentin Chary vs->auth = VNC_AUTH_INVALID; 24753e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS 24763e230dd2SCorentin Chary vs->subauth = VNC_AUTH_INVALID; 24773e230dd2SCorentin Chary vs->tls.x509verify = 0; 24783e230dd2SCorentin Chary #endif 24793e230dd2SCorentin Chary } 24803e230dd2SCorentin Chary 24813e230dd2SCorentin Chary int vnc_display_password(DisplayState *ds, const char *password) 24823e230dd2SCorentin Chary { 24833e230dd2SCorentin Chary VncDisplay *vs = ds ? (VncDisplay *)ds->opaque : vnc_display; 24843e230dd2SCorentin Chary 24853e230dd2SCorentin Chary if (!vs) { 24863e230dd2SCorentin Chary return -1; 24873e230dd2SCorentin Chary } 24883e230dd2SCorentin Chary 24893e230dd2SCorentin Chary if (vs->password) { 24903e230dd2SCorentin Chary qemu_free(vs->password); 24913e230dd2SCorentin Chary vs->password = NULL; 24923e230dd2SCorentin Chary } 24933e230dd2SCorentin Chary if (password && password[0]) { 24943e230dd2SCorentin Chary if (!(vs->password = qemu_strdup(password))) 24953e230dd2SCorentin Chary return -1; 24963e230dd2SCorentin Chary if (vs->auth == VNC_AUTH_NONE) { 24973e230dd2SCorentin Chary vs->auth = VNC_AUTH_VNC; 24983e230dd2SCorentin Chary } 24993e230dd2SCorentin Chary } else { 25003e230dd2SCorentin Chary vs->auth = VNC_AUTH_NONE; 25013e230dd2SCorentin Chary } 25023e230dd2SCorentin Chary 25033e230dd2SCorentin Chary return 0; 25043e230dd2SCorentin Chary } 25053e230dd2SCorentin Chary 25063e230dd2SCorentin Chary char *vnc_display_local_addr(DisplayState *ds) 25073e230dd2SCorentin Chary { 25083e230dd2SCorentin Chary VncDisplay *vs = ds ? (VncDisplay *)ds->opaque : vnc_display; 25093e230dd2SCorentin Chary 25103e230dd2SCorentin Chary return vnc_socket_local_addr("%s:%s", vs->lsock); 25113e230dd2SCorentin Chary } 25123e230dd2SCorentin Chary 25133e230dd2SCorentin Chary int vnc_display_open(DisplayState *ds, const char *display) 25143e230dd2SCorentin Chary { 25153e230dd2SCorentin Chary VncDisplay *vs = ds ? (VncDisplay *)ds->opaque : vnc_display; 25163e230dd2SCorentin Chary const char *options; 25173e230dd2SCorentin Chary int password = 0; 25183e230dd2SCorentin Chary int reverse = 0; 25193e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS 25203e230dd2SCorentin Chary int tls = 0, x509 = 0; 25213e230dd2SCorentin Chary #endif 25223e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL 25233e230dd2SCorentin Chary int sasl = 0; 25243e230dd2SCorentin Chary int saslErr; 25253e230dd2SCorentin Chary #endif 25263e230dd2SCorentin Chary int acl = 0; 25273e230dd2SCorentin Chary int lock_key_sync = 1; 25283e230dd2SCorentin Chary 25293e230dd2SCorentin Chary if (!vnc_display) 25303e230dd2SCorentin Chary return -1; 25313e230dd2SCorentin Chary vnc_display_close(ds); 25323e230dd2SCorentin Chary if (strcmp(display, "none") == 0) 25333e230dd2SCorentin Chary return 0; 25343e230dd2SCorentin Chary 25353e230dd2SCorentin Chary if (!(vs->display = strdup(display))) 25363e230dd2SCorentin Chary return -1; 25373e230dd2SCorentin Chary 25383e230dd2SCorentin Chary options = display; 25393e230dd2SCorentin Chary while ((options = strchr(options, ','))) { 25403e230dd2SCorentin Chary options++; 25413e230dd2SCorentin Chary if (strncmp(options, "password", 8) == 0) { 25423e230dd2SCorentin Chary password = 1; /* Require password auth */ 25433e230dd2SCorentin Chary } else if (strncmp(options, "reverse", 7) == 0) { 25443e230dd2SCorentin Chary reverse = 1; 25453e230dd2SCorentin Chary } else if (strncmp(options, "no-lock-key-sync", 9) == 0) { 25463e230dd2SCorentin Chary lock_key_sync = 0; 25473e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL 25483e230dd2SCorentin Chary } else if (strncmp(options, "sasl", 4) == 0) { 25493e230dd2SCorentin Chary sasl = 1; /* Require SASL auth */ 25503e230dd2SCorentin Chary #endif 25513e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS 25523e230dd2SCorentin Chary } else if (strncmp(options, "tls", 3) == 0) { 25533e230dd2SCorentin Chary tls = 1; /* Require TLS */ 25543e230dd2SCorentin Chary } else if (strncmp(options, "x509", 4) == 0) { 25553e230dd2SCorentin Chary char *start, *end; 25563e230dd2SCorentin Chary x509 = 1; /* Require x509 certificates */ 25573e230dd2SCorentin Chary if (strncmp(options, "x509verify", 10) == 0) 25583e230dd2SCorentin Chary vs->tls.x509verify = 1; /* ...and verify client certs */ 25593e230dd2SCorentin Chary 25603e230dd2SCorentin Chary /* Now check for 'x509=/some/path' postfix 25613e230dd2SCorentin Chary * and use that to setup x509 certificate/key paths */ 25623e230dd2SCorentin Chary start = strchr(options, '='); 25633e230dd2SCorentin Chary end = strchr(options, ','); 25643e230dd2SCorentin Chary if (start && (!end || (start < end))) { 25653e230dd2SCorentin Chary int len = end ? end-(start+1) : strlen(start+1); 25663e230dd2SCorentin Chary char *path = qemu_strndup(start + 1, len); 25673e230dd2SCorentin Chary 25683e230dd2SCorentin Chary VNC_DEBUG("Trying certificate path '%s'\n", path); 25693e230dd2SCorentin Chary if (vnc_tls_set_x509_creds_dir(vs, path) < 0) { 25703e230dd2SCorentin Chary fprintf(stderr, "Failed to find x509 certificates/keys in %s\n", path); 25713e230dd2SCorentin Chary qemu_free(path); 25723e230dd2SCorentin Chary qemu_free(vs->display); 25733e230dd2SCorentin Chary vs->display = NULL; 25743e230dd2SCorentin Chary return -1; 25753e230dd2SCorentin Chary } 25763e230dd2SCorentin Chary qemu_free(path); 25773e230dd2SCorentin Chary } else { 25783e230dd2SCorentin Chary fprintf(stderr, "No certificate path provided\n"); 25793e230dd2SCorentin Chary qemu_free(vs->display); 25803e230dd2SCorentin Chary vs->display = NULL; 25813e230dd2SCorentin Chary return -1; 25823e230dd2SCorentin Chary } 25833e230dd2SCorentin Chary #endif 25843e230dd2SCorentin Chary } else if (strncmp(options, "acl", 3) == 0) { 25853e230dd2SCorentin Chary acl = 1; 25863e230dd2SCorentin Chary } else if (strncmp(options, "lossy", 5) == 0) { 25873e230dd2SCorentin Chary vs->lossy = true; 25883e230dd2SCorentin Chary } 25893e230dd2SCorentin Chary } 25903e230dd2SCorentin Chary 25913e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS 25923e230dd2SCorentin Chary if (acl && x509 && vs->tls.x509verify) { 25933e230dd2SCorentin Chary if (!(vs->tls.acl = qemu_acl_init("vnc.x509dname"))) { 25943e230dd2SCorentin Chary fprintf(stderr, "Failed to create x509 dname ACL\n"); 25953e230dd2SCorentin Chary exit(1); 25963e230dd2SCorentin Chary } 25973e230dd2SCorentin Chary } 25983e230dd2SCorentin Chary #endif 25993e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL 26003e230dd2SCorentin Chary if (acl && sasl) { 26013e230dd2SCorentin Chary if (!(vs->sasl.acl = qemu_acl_init("vnc.username"))) { 26023e230dd2SCorentin Chary fprintf(stderr, "Failed to create username ACL\n"); 26033e230dd2SCorentin Chary exit(1); 26043e230dd2SCorentin Chary } 26053e230dd2SCorentin Chary } 26063e230dd2SCorentin Chary #endif 26073e230dd2SCorentin Chary 26083e230dd2SCorentin Chary /* 26093e230dd2SCorentin Chary * Combinations we support here: 26103e230dd2SCorentin Chary * 26113e230dd2SCorentin Chary * - no-auth (clear text, no auth) 26123e230dd2SCorentin Chary * - password (clear text, weak auth) 26133e230dd2SCorentin Chary * - sasl (encrypt, good auth *IF* using Kerberos via GSSAPI) 26143e230dd2SCorentin Chary * - tls (encrypt, weak anonymous creds, no auth) 26153e230dd2SCorentin Chary * - tls + password (encrypt, weak anonymous creds, weak auth) 26163e230dd2SCorentin Chary * - tls + sasl (encrypt, weak anonymous creds, good auth) 26173e230dd2SCorentin Chary * - tls + x509 (encrypt, good x509 creds, no auth) 26183e230dd2SCorentin Chary * - tls + x509 + password (encrypt, good x509 creds, weak auth) 26193e230dd2SCorentin Chary * - tls + x509 + sasl (encrypt, good x509 creds, good auth) 26203e230dd2SCorentin Chary * 26213e230dd2SCorentin Chary * NB1. TLS is a stackable auth scheme. 26223e230dd2SCorentin Chary * NB2. the x509 schemes have option to validate a client cert dname 26233e230dd2SCorentin Chary */ 26243e230dd2SCorentin Chary if (password) { 26253e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS 26263e230dd2SCorentin Chary if (tls) { 26273e230dd2SCorentin Chary vs->auth = VNC_AUTH_VENCRYPT; 26283e230dd2SCorentin Chary if (x509) { 26293e230dd2SCorentin Chary VNC_DEBUG("Initializing VNC server with x509 password auth\n"); 26303e230dd2SCorentin Chary vs->subauth = VNC_AUTH_VENCRYPT_X509VNC; 26313e230dd2SCorentin Chary } else { 26323e230dd2SCorentin Chary VNC_DEBUG("Initializing VNC server with TLS password auth\n"); 26333e230dd2SCorentin Chary vs->subauth = VNC_AUTH_VENCRYPT_TLSVNC; 26343e230dd2SCorentin Chary } 26353e230dd2SCorentin Chary } else { 26363e230dd2SCorentin Chary #endif /* CONFIG_VNC_TLS */ 26373e230dd2SCorentin Chary VNC_DEBUG("Initializing VNC server with password auth\n"); 26383e230dd2SCorentin Chary vs->auth = VNC_AUTH_VNC; 26393e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS 26403e230dd2SCorentin Chary vs->subauth = VNC_AUTH_INVALID; 26413e230dd2SCorentin Chary } 26423e230dd2SCorentin Chary #endif /* CONFIG_VNC_TLS */ 26433e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL 26443e230dd2SCorentin Chary } else if (sasl) { 26453e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS 26463e230dd2SCorentin Chary if (tls) { 26473e230dd2SCorentin Chary vs->auth = VNC_AUTH_VENCRYPT; 26483e230dd2SCorentin Chary if (x509) { 26493e230dd2SCorentin Chary VNC_DEBUG("Initializing VNC server with x509 SASL auth\n"); 26503e230dd2SCorentin Chary vs->subauth = VNC_AUTH_VENCRYPT_X509SASL; 26513e230dd2SCorentin Chary } else { 26523e230dd2SCorentin Chary VNC_DEBUG("Initializing VNC server with TLS SASL auth\n"); 26533e230dd2SCorentin Chary vs->subauth = VNC_AUTH_VENCRYPT_TLSSASL; 26543e230dd2SCorentin Chary } 26553e230dd2SCorentin Chary } else { 26563e230dd2SCorentin Chary #endif /* CONFIG_VNC_TLS */ 26573e230dd2SCorentin Chary VNC_DEBUG("Initializing VNC server with SASL auth\n"); 26583e230dd2SCorentin Chary vs->auth = VNC_AUTH_SASL; 26593e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS 26603e230dd2SCorentin Chary vs->subauth = VNC_AUTH_INVALID; 26613e230dd2SCorentin Chary } 26623e230dd2SCorentin Chary #endif /* CONFIG_VNC_TLS */ 26633e230dd2SCorentin Chary #endif /* CONFIG_VNC_SASL */ 26643e230dd2SCorentin Chary } else { 26653e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS 26663e230dd2SCorentin Chary if (tls) { 26673e230dd2SCorentin Chary vs->auth = VNC_AUTH_VENCRYPT; 26683e230dd2SCorentin Chary if (x509) { 26693e230dd2SCorentin Chary VNC_DEBUG("Initializing VNC server with x509 no auth\n"); 26703e230dd2SCorentin Chary vs->subauth = VNC_AUTH_VENCRYPT_X509NONE; 26713e230dd2SCorentin Chary } else { 26723e230dd2SCorentin Chary VNC_DEBUG("Initializing VNC server with TLS no auth\n"); 26733e230dd2SCorentin Chary vs->subauth = VNC_AUTH_VENCRYPT_TLSNONE; 26743e230dd2SCorentin Chary } 26753e230dd2SCorentin Chary } else { 26763e230dd2SCorentin Chary #endif 26773e230dd2SCorentin Chary VNC_DEBUG("Initializing VNC server with no auth\n"); 26783e230dd2SCorentin Chary vs->auth = VNC_AUTH_NONE; 26793e230dd2SCorentin Chary #ifdef CONFIG_VNC_TLS 26803e230dd2SCorentin Chary vs->subauth = VNC_AUTH_INVALID; 26813e230dd2SCorentin Chary } 26823e230dd2SCorentin Chary #endif 26833e230dd2SCorentin Chary } 26843e230dd2SCorentin Chary 26853e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL 26863e230dd2SCorentin Chary if ((saslErr = sasl_server_init(NULL, "qemu")) != SASL_OK) { 26873e230dd2SCorentin Chary fprintf(stderr, "Failed to initialize SASL auth %s", 26883e230dd2SCorentin Chary sasl_errstring(saslErr, NULL, NULL)); 26893e230dd2SCorentin Chary free(vs->display); 26903e230dd2SCorentin Chary vs->display = NULL; 26913e230dd2SCorentin Chary return -1; 26923e230dd2SCorentin Chary } 26933e230dd2SCorentin Chary #endif 26943e230dd2SCorentin Chary vs->lock_key_sync = lock_key_sync; 26953e230dd2SCorentin Chary 26963e230dd2SCorentin Chary if (reverse) { 26973e230dd2SCorentin Chary /* connect to viewer */ 26983e230dd2SCorentin Chary if (strncmp(display, "unix:", 5) == 0) 26993e230dd2SCorentin Chary vs->lsock = unix_connect(display+5); 27003e230dd2SCorentin Chary else 27013e230dd2SCorentin Chary vs->lsock = inet_connect(display, SOCK_STREAM); 27023e230dd2SCorentin Chary if (-1 == vs->lsock) { 27033e230dd2SCorentin Chary free(vs->display); 27043e230dd2SCorentin Chary vs->display = NULL; 27053e230dd2SCorentin Chary return -1; 27063e230dd2SCorentin Chary } else { 27073e230dd2SCorentin Chary int csock = vs->lsock; 27083e230dd2SCorentin Chary vs->lsock = -1; 27093e230dd2SCorentin Chary vnc_connect(vs, csock); 27103e230dd2SCorentin Chary } 27113e230dd2SCorentin Chary return 0; 27123e230dd2SCorentin Chary 27133e230dd2SCorentin Chary } else { 27143e230dd2SCorentin Chary /* listen for connects */ 27153e230dd2SCorentin Chary char *dpy; 27163e230dd2SCorentin Chary dpy = qemu_malloc(256); 27173e230dd2SCorentin Chary if (strncmp(display, "unix:", 5) == 0) { 27183e230dd2SCorentin Chary pstrcpy(dpy, 256, "unix:"); 27193e230dd2SCorentin Chary vs->lsock = unix_listen(display+5, dpy+5, 256-5); 27203e230dd2SCorentin Chary } else { 27213e230dd2SCorentin Chary vs->lsock = inet_listen(display, dpy, 256, SOCK_STREAM, 5900); 27223e230dd2SCorentin Chary } 27233e230dd2SCorentin Chary if (-1 == vs->lsock) { 27243e230dd2SCorentin Chary free(dpy); 27253e230dd2SCorentin Chary return -1; 27263e230dd2SCorentin Chary } else { 27273e230dd2SCorentin Chary free(vs->display); 27283e230dd2SCorentin Chary vs->display = dpy; 27293e230dd2SCorentin Chary } 27303e230dd2SCorentin Chary } 27313e230dd2SCorentin Chary return qemu_set_fd_handler2(vs->lsock, NULL, vnc_listen_read, NULL, vs); 27323e230dd2SCorentin Chary } 2733